diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketOptionNameTest.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketOptionNameTest.cs index 9df0b3c398b69a..61aa6941fcf821 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketOptionNameTest.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketOptionNameTest.cs @@ -70,7 +70,6 @@ public void MulticastOption_CreateSocketSetGetOption_GroupAndInterfaceIndex_SetS } [ConditionalFact(nameof(CanRunMulticastTests))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/113827", typeof(PlatformDetection), nameof(PlatformDetection.IsAppleMobile))] public async Task MulticastInterface_Set_AnyInterface_Succeeds() { // On all platforms, index 0 means "any interface" diff --git a/src/native/libs/Common/pal_config.h.in b/src/native/libs/Common/pal_config.h.in index 580cd68a574335..2429c6e78e39c2 100644 --- a/src/native/libs/Common/pal_config.h.in +++ b/src/native/libs/Common/pal_config.h.in @@ -70,6 +70,7 @@ #cmakedefine01 HAVE_SUPPORT_FOR_DUAL_MODE_IPV4_PACKET_INFO #cmakedefine01 HAVE_IN_PKTINFO #cmakedefine01 HAVE_IP_MREQN +#cmakedefine01 HAVE_IP_MULTICAST_IFINDEX #cmakedefine01 HAVE_NETINET_TCP_VAR_H #cmakedefine01 HAVE_NETINET_UDP_VAR_H #cmakedefine01 HAVE_NETINET_IP_VAR_H diff --git a/src/native/libs/System.Native/pal_networking.c b/src/native/libs/System.Native/pal_networking.c index ed3c90d6d6c14e..38486322526b08 100644 --- a/src/native/libs/System.Native/pal_networking.c +++ b/src/native/libs/System.Native/pal_networking.c @@ -1186,6 +1186,7 @@ int32_t SystemNative_GetIPv4MulticastOption(intptr_t socket, int32_t multicastOp return Error_SUCCESS; } + int32_t SystemNative_SetIPv4MulticastOption(intptr_t socket, int32_t multicastOption, IPv4MulticastOption* option) { if (option == NULL) @@ -1201,6 +1202,16 @@ int32_t SystemNative_SetIPv4MulticastOption(intptr_t socket, int32_t multicastOp return Error_EINVAL; } +#if HAVE_IP_MULTICAST_IFINDEX + // Use IP_MULTICAST_IFINDEX when available for interface index specification + if (optionName == SocketOptionName_SO_IP_MULTICAST_IF) + { + uint32_t ifindex = (uint32_t)option->InterfaceIndex; + int err = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IFINDEX, &ifindex, sizeof(ifindex)); + return err == 0 ? Error_SUCCESS : SystemNative_ConvertErrorPlatformToPal(errno); + } +#endif + #if HAVE_IP_MREQN struct ip_mreqn opt; memset(&opt, 0, sizeof(struct ip_mreqn)); diff --git a/src/native/libs/configure.cmake b/src/native/libs/configure.cmake index 5348adb6b2e0b5..1f3aeca478ed2b 100644 --- a/src/native/libs/configure.cmake +++ b/src/native/libs/configure.cmake @@ -82,6 +82,18 @@ check_c_source_compiles( " HAVE_IP_MREQN) +check_c_source_compiles( + " + #include + #include <${SOCKET_INCLUDES}> + int main(void) + { + int opt = IP_MULTICAST_IFINDEX; + return 0; + } + " + HAVE_IP_MULTICAST_IFINDEX) + # /in_pktinfo check_c_source_compiles(