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..98b830ff 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(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(); +}