Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/native/libs/Common/pal_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/native/libs/System.Native/pal_networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,28 @@ int32_t SystemNative_GetIPv4MulticastOption(intptr_t socket, int32_t multicastOp
return Error_SUCCESS;
}

#if HAVE_IP_MULTICAST_IFINDEX
static int32_t SetIPv4MulticastInterfaceByIndexOrIPAddress(int fd, IPv4MulticastOption* option)
{
if (option->InterfaceIndex != 0)
{
// Use IP_MULTICAST_IFINDEX when interface index is specified
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);
}
else
{
struct ip_mreq opt;
memset(&opt, 0, sizeof(struct ip_mreq));
opt.imr_multiaddr.s_addr = option->MulticastAddress;
opt.imr_interface.s_addr = option->LocalAddress;
int err = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &opt, sizeof(opt));
return err == 0 ? Error_SUCCESS : SystemNative_ConvertErrorPlatformToPal(errno);
}
}
#endif

int32_t SystemNative_SetIPv4MulticastOption(intptr_t socket, int32_t multicastOption, IPv4MulticastOption* option)
{
if (option == NULL)
Expand All @@ -1201,6 +1223,13 @@ int32_t SystemNative_SetIPv4MulticastOption(intptr_t socket, int32_t multicastOp
return Error_EINVAL;
}

#if HAVE_IP_MULTICAST_IFINDEX
if (optionName == SocketOptionName_SO_IP_MULTICAST_IF)
{
return SetIPv4MulticastInterfaceByIndexOrIPAddress(fd, option);
}
#endif

#if HAVE_IP_MREQN
struct ip_mreqn opt;
memset(&opt, 0, sizeof(struct ip_mreqn));
Expand Down
12 changes: 12 additions & 0 deletions src/native/libs/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ check_c_source_compiles(
"
HAVE_IP_MREQN)

check_c_source_compiles(
"
#include <sys/socket.h>
#include <${SOCKET_INCLUDES}>
int main(void)
{
int opt = IP_MULTICAST_IFINDEX;
return 0;
}
"
HAVE_IP_MULTICAST_IFINDEX)

# /in_pktinfo

check_c_source_compiles(
Expand Down
Loading