From d2ee8a5ca165a62ec7fcc2f276cb179da8975042 Mon Sep 17 00:00:00 2001 From: WillBux Date: Fri, 13 Nov 2026 16:42:16 +0100 Subject: [PATCH 1/2] fix: interface index in join/leave_multicast_v4_n on macOS --- src/socket.rs | 25 +++++++++++++++++++++++++ src/sys/unix.rs | 14 ++++++++++++++ tests/socket.rs | 23 +++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/src/socket.rs b/src/socket.rs index 7ea57f4b..953bf1a4 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -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", @@ -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( @@ -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", @@ -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( diff --git a/src/sys/unix.rs b/src/sys/unix.rs index 8027d325..d4781d62 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -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::().read() }, + } +} + #[cfg(all( feature = "all", any(target_os = "android", target_os = "fuchsia", target_os = "linux") diff --git a/tests/socket.rs b/tests/socket.rs index 0f390e3a..41a9fd9c 100644 --- a/tests/socket.rs +++ b/tests/socket.rs @@ -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(c"lo0".as_ptr()) }; + 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(); +} From d80fcb0d11f31db9f4f36065e1d50ae7e2800798 Mon Sep 17 00:00:00 2001 From: WillBux Date: Fri, 13 Nov 2026 16:51:51 +0100 Subject: [PATCH 2/2] msrv fix --- tests/socket.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/socket.rs b/tests/socket.rs index 41a9fd9c..98b830ff 100644 --- a/tests/socket.rs +++ b/tests/socket.rs @@ -2009,7 +2009,7 @@ 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(c"lo0".as_ptr()) }; + 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);