socket-io: make it possible to specify an interface index as a MulticastInterface...
authorMaster T <tom.deseyn@gmail.com>
Sun, 24 Nov 2013 09:23:36 +0000 (10:23 +0100)
committerMaster T <tom.deseyn@gmail.com>
Sun, 24 Nov 2013 09:23:36 +0000 (10:23 +0100)
On windows, a value to IP_MULTICAST_IF with a most significant byte of zero is interpreted as an interface index instead of an ip address.
This makes it possible to set the interface index from .NET as follows:
socket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(ifindex));

For this to work on other platforms, we need to detect this case and handle it with an ip_mreqn struct.

mono/metadata/socket-io.c

index 6509d091a9401bc753bab6b2dd1b5cfcb591dc39..3947cd5d1cff1aaf8ef533e1715ca53783677755 100644 (file)
@@ -2278,6 +2278,23 @@ void ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal(SOCKET sock, g
                        linger.l_linger = 0;
                        ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
                        break;
+               case SocketOptionName_MulticastInterface:
+#ifndef HOST_WIN32
+#ifdef HAVE_STRUCT_IP_MREQN
+                       int_val = GUINT32_FROM_BE (int_val);
+                       if ((int_val & 0xff000000) == 0) {
+                               /* int_val is interface index */
+                               struct ip_mreqn mreq = {{0}};
+                               mreq.imr_ifindex = int_val;
+                               ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &mreq, sizeof (mreq));
+                               break;
+                       }
+                       int_val = GUINT32_TO_BE (int_val);
+#endif /* HAVE_STRUCT_IP_MREQN */
+#endif /* HOST_WIN32 */
+                       /* int_val is in_addr */
+                       ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
+                       break;
                case SocketOptionName_DontFragment:
 #ifdef HAVE_IP_MTU_DISCOVER
                        /* Fiddle with the value slightly if we're