Skip to content

Commit

Permalink
Merge pull request #815 from tmds/master
Browse files Browse the repository at this point in the history
socket-io: interface index handling
  • Loading branch information
kumpera committed Nov 27, 2013
2 parents 56ccb68 + 962a30b commit 3c3e109
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
48 changes: 48 additions & 0 deletions mcs/class/System/Test/System.Net.Sockets/SocketTest.cs 100644 → 100755
Expand Up @@ -4143,6 +4143,54 @@ public void SetSocketOption3_Socket_Closed ()
}
}

[Test]
public void SetSocketOption_MulticastInterfaceIndex_Any ()
{
IPAddress ip = IPAddress.Parse ("239.255.255.250");
int index = 0;
using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
}
}

[Test]
public void SetSocketOption_MulticastInterfaceIndex_Loopback ()
{
IPAddress ip = IPAddress.Parse ("239.255.255.250");
int index = 1;
using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
}
}

[Test]
public void SetSocketOption_MulticastInterfaceIndex_Invalid ()
{
IPAddress ip = IPAddress.Parse ("239.255.255.250");
int index = 31415;
using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
try
{
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
Assert.Fail ("#1");
}
catch
{}
try
{
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
Assert.Fail ("#2");
}
catch
{}
}
}

[Test]
public void Shutdown_NoConnect ()
{
Expand Down
22 changes: 21 additions & 1 deletion mono/metadata/socket-io.c
Expand Up @@ -2234,12 +2234,15 @@ void ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal(SOCKET sock, g
if(address) {
mreq.imr_address = ipaddress_to_struct_in_addr (address);
}

field = mono_class_get_field_from_name(obj_val->vtable->klass, "iface_index");
mreq.imr_ifindex = *(gint32 *)(((char *)obj_val)+field->offset);
#else
if(address) {
mreq.imr_interface = ipaddress_to_struct_in_addr (address);
}
#endif /* HAVE_STRUCT_IP_MREQN */

ret = _wapi_setsockopt (sock, system_level,
system_name, &mreq,
sizeof (mreq));
Expand Down Expand Up @@ -2278,6 +2281,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
Expand Down

0 comments on commit 3c3e109

Please sign in to comment.