Skip to content

add IP_MULTICAST_IF for sockopt #1363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,32 @@ impl Ipv6MembershipRequest {
}
}

cfg_if! {
if #[cfg(not(all(target_os = "linux",
target_env = "gnu",
any(target_arch = "mips",
target_arch = "mips64",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "powerpc64"))))] {
/// Request for multicast socket's outgoing interface
///
/// The value is a `libc::in_addr` structure, which is the most common.
/// `ip_mreqn` or `ip_mreq` structure are not supported at this point, one
/// reason is that if we use enum to represent all possible structures,
/// `#[repr(transparent)]` does not support enum.
#[repr(transparent)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the option work with IPv6? If so you should use an enum type. Or maybe define different Nix sockopts, one for each supported address type.

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct IpMulticastIfInAddr(libc::in_addr);

impl IpMulticastIfInAddr {
pub fn new(interface: Ipv4Addr) -> Self {
IpMulticastIfInAddr(interface.0)
}
}
}
}

/// Create a buffer large enough for storing some control messages as returned
/// by [`recvmsg`](fn.recvmsg.html).
///
Expand Down
11 changes: 11 additions & 0 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,17 @@ cfg_if! {
sockopt_impl!(SetOnly, Ipv6DropMembership, libc::IPPROTO_IPV6, libc::IPV6_LEAVE_GROUP, super::Ipv6MembershipRequest);
}
}
cfg_if! {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cfg_if is helpful when you expect to have else or else if blocks. But in cases like this, it's simpler to do:

#[cfg(...)]
sockopt_impl!(...)

if #[cfg(not(all(target_os = "linux",
target_env = "gnu",
any(target_arch = "mips",
target_arch = "mips64",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "powerpc64"))))] {
sockopt_impl!(Both, IpMulticastIf, libc::IPPROTO_IP, libc::IP_MULTICAST_IF, super::IpMulticastIfInAddr);
}
}
sockopt_impl!(Both, IpMulticastTtl, libc::IPPROTO_IP, libc::IP_MULTICAST_TTL, u8);
sockopt_impl!(Both, IpMulticastLoop, libc::IPPROTO_IP, libc::IP_MULTICAST_LOOP, bool);
sockopt_impl!(Both, ReceiveTimeout, libc::SOL_SOCKET, libc::SO_RCVTIMEO, TimeVal);
Expand Down
32 changes: 32 additions & 0 deletions test/sys/test_sockopt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use rand::{thread_rng, Rng};
use nix::sys::socket::{socket, sockopt, getsockopt, setsockopt, AddressFamily, SockType, SockFlag, SockProtocol};
#[cfg(not(all(target_os = "linux",
target_env = "gnu",
any(target_arch = "mips",
target_arch = "mips64",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "powerpc64"))))]
use nix::{ifaddrs::getifaddrs, sys::socket::{IpAddr, IpMulticastIfInAddr, SockAddr}};

#[cfg(any(target_os = "android", target_os = "linux"))]
use crate::*;

Expand Down Expand Up @@ -94,3 +103,26 @@ fn test_so_tcp_keepalive() {
assert_eq!(getsockopt(fd, sockopt::TcpKeepInterval).unwrap(), x + 1);
}
}

#[test]
#[cfg(not(all(target_os = "linux",
target_env = "gnu",
any(target_arch = "mips",
target_arch = "mips64",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "powerpc64"))))]
fn test_so_multicast_if() {
let fd = socket(AddressFamily::Inet, SockType::Datagram, SockFlag::empty(), None).unwrap();
let addrs = getifaddrs().unwrap();

for if_addr in addrs {
if let Some(SockAddr::Inet(inet_addr)) = if_addr.address {
if let IpAddr::V4(ipv4_addr) = inet_addr.ip() {
let request = IpMulticastIfInAddr::new(ipv4_addr);
setsockopt(fd, sockopt::IpMulticastIf, &request).unwrap();
assert_eq!(getsockopt(fd, sockopt::IpMulticastIf).unwrap(), request);
}
}
}
}