Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 30 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;
}

#ifdef __APPLE__
static int32_t Apple_SetIPv4MulticastInterface(int fd, IPv4MulticastOption* option)
{
if (option->InterfaceIndex != 0)
{
// On Apple platforms, IP_MULTICAST_IF with interface index uses the interface index directly
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,14 @@ int32_t SystemNative_SetIPv4MulticastOption(intptr_t socket, int32_t multicastOp
return Error_EINVAL;
}

#ifdef __APPLE__
// Special handling for IP_MULTICAST_IF on Apple platforms
if (optionName == SocketOptionName_SO_IP_MULTICAST_IF)
{
return Apple_SetIPv4MulticastInterface(fd, option);
}
#endif

#if HAVE_IP_MREQN
struct ip_mreqn opt;
memset(&opt, 0, sizeof(struct ip_mreqn));
Expand Down
Loading