diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketOptionNameTest.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketOptionNameTest.cs index 2d7139a05803cb..26752dd31f5dcb 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketOptionNameTest.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SocketOptionNameTest.cs @@ -67,7 +67,6 @@ public void MulticastOption_CreateSocketSetGetOption_GroupAndInterfaceIndex_SetS [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoNorServerCore))] // Skip on Nano: https://github.com/dotnet/runtime/issues/26286 [ActiveIssue("https://github.com/dotnet/runtime/issues/104547", typeof(PlatformDetection), nameof(PlatformDetection.IsQemuLinux))] - [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 4439e8cebfeaa4..d789d0e2f5d807 100644 --- a/src/native/libs/Common/pal_config.h.in +++ b/src/native/libs/Common/pal_config.h.in @@ -67,6 +67,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 cae1285e7e74e4..0ba5b87ebef1f7 100644 --- a/src/native/libs/System.Native/pal_networking.c +++ b/src/native/libs/System.Native/pal_networking.c @@ -1119,6 +1119,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) @@ -1134,6 +1135,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 477f9f1f14a037..7a511c559585f8 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(