Skip to content
Open
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
25 changes: 25 additions & 0 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,10 @@ impl Socket {
/// The address must be a valid multicast address, and `interface` specifies
/// the local interface with which the system should join the multicast
/// group. See [`InterfaceIndexOrAddress`].
///
/// On Apple platforms an [`InterfaceIndexOrAddress::Index`] is joined with
/// `MCAST_JOIN_GROUP`, because `IP_ADD_MEMBERSHIP` ignores the index there and
/// picks the interface by address.
#[cfg(not(any(
target_os = "aix",
target_os = "haiku",
Expand All @@ -1363,6 +1367,15 @@ impl Socket {
multiaddr: &Ipv4Addr,
interface: &InterfaceIndexOrAddress,
) -> io::Result<()> {
// Index 0 means no interface, so the kernel picks one by route as before.
#[cfg(target_vendor = "apple")]
if let InterfaceIndexOrAddress::Index(index @ 1..) = interface {
let req = sys::to_group_req(multiaddr, *index);
return unsafe {
setsockopt(self.as_raw(), sys::IPPROTO_IP, sys::MCAST_JOIN_GROUP, req)
};
}

let mreqn = sys::to_mreqn(multiaddr, interface);
unsafe {
setsockopt(
Expand All @@ -1379,6 +1392,9 @@ impl Socket {
/// For more information about this option, see [`join_multicast_v4_n`].
///
/// [`join_multicast_v4_n`]: Socket::join_multicast_v4_n
///
/// On Apple platforms an [`InterfaceIndexOrAddress::Index`] is left with
/// `MCAST_LEAVE_GROUP`, see [`join_multicast_v4_n`].
#[cfg(not(any(
target_os = "aix",
target_os = "haiku",
Expand All @@ -1399,6 +1415,15 @@ impl Socket {
multiaddr: &Ipv4Addr,
interface: &InterfaceIndexOrAddress,
) -> io::Result<()> {
// Index 0 means no interface, so the kernel picks one by route as before.
#[cfg(target_vendor = "apple")]
if let InterfaceIndexOrAddress::Index(index @ 1..) = interface {
let req = sys::to_group_req(multiaddr, *index);
return unsafe {
setsockopt(self.as_raw(), sys::IPPROTO_IP, sys::MCAST_LEAVE_GROUP, req)
};
}

let mreqn = sys::to_mreqn(multiaddr, interface);
unsafe {
setsockopt(
Expand Down
14 changes: 14 additions & 0 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,20 @@ pub(crate) const fn to_mreqn(
}
}

#[cfg(target_vendor = "apple")]
pub(crate) use libc::{group_req, MCAST_JOIN_GROUP, MCAST_LEAVE_GROUP};

/// The `group_req` that joins or leaves `multiaddr` on interface `interface_index`.
#[cfg(target_vendor = "apple")]
pub(crate) fn to_group_req(multiaddr: &Ipv4Addr, interface_index: u32) -> group_req {
let group = SockAddr::from(std::net::SocketAddrV4::new(*multiaddr, 0));
group_req {
gr_interface: interface_index,
// SAFETY: `SockAddrStorage` is `repr(transparent)` over `sockaddr_storage`.
gr_group: unsafe { group.as_ptr().cast::<libc::sockaddr_storage>().read() },
}
}

#[cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
Expand Down
23 changes: 23 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2002,3 +2002,26 @@ fn min_hopcount_v6_preserves_outbound_hops() {
assert_eq!(socket.min_hopcount_v6().unwrap(), 197);
assert_eq!(socket.unicast_hops_v6().unwrap(), 61);
}

#[test]
#[cfg(target_vendor = "apple")]
fn join_and_leave_multicast_v4_by_index() {
use socket2::InterfaceIndexOrAddress;

let group = Ipv4Addr::new(224, 0, 0, 251);
let lo0 = unsafe { libc::if_nametoindex(b"lo0\0".as_ptr().cast()) };
assert_ne!(lo0, 0);
let socket = Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap();
let by_index = InterfaceIndexOrAddress::Index(lo0);

// Crossing index and lo0's address only works if the index really picks lo0,
// not an interface the kernel chose by route.
socket.join_multicast_v4_n(&group, &by_index).unwrap();
socket
.leave_multicast_v4(&group, &Ipv4Addr::LOCALHOST)
.unwrap();
socket
.join_multicast_v4(&group, &Ipv4Addr::LOCALHOST)
.unwrap();
socket.leave_multicast_v4_n(&group, &by_index).unwrap();
}
Loading