Skip to content

Commit

Permalink
utils: add socket methods to join and leave ethernet multicast addresses
Browse files Browse the repository at this point in the history
These methods will be used by the upcoming IS-IS implementation.

Signed-off-by: Renato Westphal <[email protected]>
  • Loading branch information
rwestphal committed Sep 10, 2024
1 parent f7fb618 commit 30e721f
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions holo-utils/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
//

use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::os::raw::{c_int, c_void};
use std::os::raw::{c_int, c_ushort, c_void};
use std::os::unix::io::AsRawFd;

use libc::ip_mreqn;
use libc::{ip_mreqn, packet_mreq};
use serde::{Deserialize, Serialize};
// Normal build: re-export standard socket types.
#[cfg(not(feature = "testing"))]
Expand Down Expand Up @@ -136,6 +136,48 @@ pub trait SocketExt: Sized + AsRawFd {
std::mem::size_of::<i32>() as libc::socklen_t,
)
}

// Executes an operation of the PACKET_ADD_MEMBERSHIP type.
fn join_packet_multicast(&self, addr: [u8; 6], ifindex: u32) -> Result<()> {
let mut optval = packet_mreq {
mr_ifindex: ifindex as c_int,
mr_type: libc::PACKET_MR_MULTICAST as c_ushort,
mr_alen: 6,
mr_address: [0; 8],
};
optval.mr_address[..6].copy_from_slice(&addr);

setsockopt(
self,
libc::SOL_PACKET,
libc::PACKET_ADD_MEMBERSHIP,
&optval as *const _ as *const c_void,
std::mem::size_of::<packet_mreq>() as libc::socklen_t,
)
}

// Executes an operation of the PACKET_DROP_MEMBERSHIP type.
fn leave_packet_multicast(
&self,
addr: [u8; 6],
ifindex: u32,
) -> Result<()> {
let mut optval = packet_mreq {
mr_ifindex: ifindex as c_int,
mr_type: libc::PACKET_MR_MULTICAST as c_ushort,
mr_alen: 6,
mr_address: [0; 8],
};
optval.mr_address[..6].copy_from_slice(&addr);

setsockopt(
self,
libc::SOL_PACKET,
libc::PACKET_DROP_MEMBERSHIP,
&optval as *const _ as *const c_void,
std::mem::size_of::<packet_mreq>() as libc::socklen_t,
)
}
}

// Extension methods for UdpSocket.
Expand Down

0 comments on commit 30e721f

Please sign in to comment.