Skip to content

Commit 5cc12a8

Browse files
committed
Fix IPv4 multicast on macOS by handling ip_mreqn struct compatibility
1 parent b8fe805 commit 5cc12a8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/native/libs/System.Native/pal_networking.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,28 @@ int32_t SystemNative_GetIPv4MulticastOption(intptr_t socket, int32_t multicastOp
11861186
return Error_SUCCESS;
11871187
}
11881188

1189+
#ifdef __APPLE__
1190+
static int32_t Apple_SetIPv4MulticastInterface(int fd, IPv4MulticastOption* option)
1191+
{
1192+
if (option->InterfaceIndex != 0)
1193+
{
1194+
// On Apple platforms, IP_MULTICAST_IF with interface index uses the interface index directly
1195+
uint32_t ifindex = (uint32_t)option->InterfaceIndex;
1196+
int err = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IFINDEX, &ifindex, sizeof(ifindex));
1197+
return err == 0 ? Error_SUCCESS : SystemNative_ConvertErrorPlatformToPal(errno);
1198+
}
1199+
else
1200+
{
1201+
struct ip_mreq opt;
1202+
memset(&opt, 0, sizeof(struct ip_mreq));
1203+
opt.imr_multiaddr.s_addr = option->MulticastAddress;
1204+
opt.imr_interface.s_addr = option->LocalAddress;
1205+
int err = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &opt, sizeof(opt));
1206+
return err == 0 ? Error_SUCCESS : SystemNative_ConvertErrorPlatformToPal(errno);
1207+
}
1208+
}
1209+
#endif
1210+
11891211
int32_t SystemNative_SetIPv4MulticastOption(intptr_t socket, int32_t multicastOption, IPv4MulticastOption* option)
11901212
{
11911213
if (option == NULL)
@@ -1201,6 +1223,14 @@ int32_t SystemNative_SetIPv4MulticastOption(intptr_t socket, int32_t multicastOp
12011223
return Error_EINVAL;
12021224
}
12031225

1226+
#ifdef __APPLE__
1227+
// Special handling for IP_MULTICAST_IF on Apple platforms
1228+
if (optionName == SocketOptionName_SO_IP_MULTICAST_IF)
1229+
{
1230+
return Apple_SetIPv4MulticastInterface(fd, option);
1231+
}
1232+
#endif
1233+
12041234
#if HAVE_IP_MREQN
12051235
struct ip_mreqn opt;
12061236
memset(&opt, 0, sizeof(struct ip_mreqn));

0 commit comments

Comments
 (0)