Skip to content

Commit

Permalink
utils: expose multicast helpers for raw sockets
Browse files Browse the repository at this point in the history
We shouldn't limit multicast setsockopt helpers to UDP sockets since
it's also possible to do multicasting using raw sockets (as in the
case of the upcoming VRRP implementation).

The methods moved from the `UdpSocketExt` trait to the `SocketExt`
trait were renamed to avoid conflicts with the native methods in the
socket2's `Socket` struct.

Signed-off-by: Renato Westphal <[email protected]>
  • Loading branch information
rwestphal committed Dec 6, 2024
1 parent 4ba065b commit bd3fb71
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion holo-rip/src/ripng/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl NetworkVersion for Ripng {
) -> std::io::Result<()> {
#[cfg(not(feature = "testing"))]
{
socket.set_multicast_if_v6(ifindex)
socket.set_multicast_ifindex_v6(ifindex)
}
#[cfg(feature = "testing")]
{
Expand Down
2 changes: 1 addition & 1 deletion holo-rip/src/ripv2/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl NetworkVersion for Ripv2 {
) -> std::io::Result<()> {
#[cfg(not(feature = "testing"))]
{
socket.set_multicast_if_v4(ifindex)
socket.set_multicast_ifindex_v4(ifindex)
}
#[cfg(feature = "testing")]
{
Expand Down
60 changes: 30 additions & 30 deletions holo-utils/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ pub trait SocketExt: Sized + AsRawFd {
)
}

// Sets the value of the IP_MULTICAST_IF option for this socket.
fn set_multicast_ifindex_v4(&self, ifindex: u32) -> Result<()> {
let optval = ip_mreqn {
imr_multiaddr: libc::in_addr { s_addr: 0 },
imr_address: libc::in_addr { s_addr: 0 },
imr_ifindex: ifindex as i32,
};

setsockopt(
self,
libc::IPPROTO_IP,
libc::IP_MULTICAST_IF,
&optval as *const _ as *const c_void,
std::mem::size_of::<ip_mreqn>() as libc::socklen_t,
)
}

// Sets the value of the IPV6_TCLASS option for this socket.
fn set_ipv6_tclass(&self, dscp: u8) -> Result<()> {
let optval = dscp as c_int;
Expand Down Expand Up @@ -137,6 +154,19 @@ pub trait SocketExt: Sized + AsRawFd {
)
}

// Sets the value of the IPV6_MULTICAST_IF option for this socket.
fn set_multicast_ifindex_v6(&self, ifindex: u32) -> Result<()> {
let optval = ifindex as i32;

setsockopt(
self,
libc::IPPROTO_IPV6,
libc::IPV6_MULTICAST_IF,
&optval as *const _ as *const c_void,
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 {
Expand Down Expand Up @@ -242,36 +272,6 @@ pub trait UdpSocketExt: SocketExt {
)
}

// Sets the value of the IP_MULTICAST_IF option for this socket.
fn set_multicast_if_v4(&self, ifindex: u32) -> Result<()> {
let optval = ip_mreqn {
imr_multiaddr: libc::in_addr { s_addr: 0 },
imr_address: libc::in_addr { s_addr: 0 },
imr_ifindex: ifindex as i32,
};

setsockopt(
self,
libc::IPPROTO_IP,
libc::IP_MULTICAST_IF,
&optval as *const _ as *const c_void,
std::mem::size_of::<ip_mreqn>() as libc::socklen_t,
)
}

// Sets the value of the IPV6_MULTICAST_IF option for this socket.
fn set_multicast_if_v6(&self, ifindex: u32) -> Result<()> {
let optval = ifindex as i32;

setsockopt(
self,
libc::IPPROTO_IPV6,
libc::IPV6_MULTICAST_IF,
&optval as *const _ as *const c_void,
std::mem::size_of::<i32>() as libc::socklen_t,
)
}

// Sets the value of the IPV6_MULTICAST_HOPS option for this socket.
fn set_ipv6_multicast_hopcount(&self, hopcount: u8) -> Result<()> {
let optval = hopcount as c_int;
Expand Down

0 comments on commit bd3fb71

Please sign in to comment.