UDP message sent off wrong network interface

This is a funny one and I wonder what is exactly causing such misbehavior. An UDP broadcasting socket is being bound to IP address 192.168.0.2:

__E(m_Socket.Create(SOCK_DGRAM, IPPROTO_UDP) != INVALID_SOCKET);
static const DWORD g_nBroadcastValue = 1;
__E(m_Socket.SetOption(SOL_SOCKET, SO_BROADCAST, &g_nBroadcastValue, sizeof g_nBroadcastValue));
ZeroMemory(&m_LocalAddress, sizeof m_LocalAddress);
m_LocalAddress.sin_family = AF_INET;
m_LocalAddress.sin_port = htons(nPort); // 7365
m_LocalAddress.sin_addr.S_un.S_addr = htonl(nLocalIpAddress); // 192.168.0.2
_Z4(atlTraceGeneral, 4, _T("m_Socket %d, m_LocalAddress.sin_addr %hs\n"), m_Socket, inet_ntoa(m_LocalAddress.sin_addr));
__E(bind(m_Socket, (SOCKADDR*) &m_LocalAddress, sizeof m_LocalAddress) != SOCKET_ERROR)

Then off this socket a broadcast message is sent using sendto:

SOCKADDR_IN RemoteAddress;
ZeroMemory(&RemoteAddress, sizeof RemoteAddress);
RemoteAddress.sin_family = AF_INET;
RemoteAddress.sin_port = htons(7364);
RemoteAddress.sin_addr.S_un.S_addr = INADDR_BROADCAST;
_Z4(atlTraceGeneral, 4, _T("BroadcastSocketData.m_Socket %d\n"), BroadcastSocketData.m_Socket);
__E(sendto(BroadcastSocketData.m_Socket, NULL, 0, 0, (const SOCKADDR*) &RemoteAddress, sizeof RemoteAddress) != SOCKET_ERROR)

The problem however is that WireShark shows the message going off the wrong network interface, with IP address 192.168.56.1, while 192.68.0.2 is an IP address on a different network adapter:

C:\>ipconfig /all

Ethernet adapter Home Area Connection:

        Connection-specific DNS Suffix  . :
        Description . . . . . . . . . . . : Intel(R) 82566DC-2 Gigabit Network Connection
        Dhcp Enabled. . . . . . . . . . . : No
        IP Address. . . . . . . . . . . . : 192.168.0.2
        Subnet Mask . . . . . . . . . . . : 255.255.0.0
        Default Gateway . . . . . . . . . : 192.168.0.1
        DNS Servers . . . . . . . . . . . : 192.168.0.1
                                            4.2.2.4

Ethernet adapter VirtualBox Host-Only Network:

        Connection-specific DNS Suffix  . :
        Description . . . . . . . . . . . : VirtualBox Host-Only Ethernet Adapter
        Dhcp Enabled. . . . . . . . . . . : No
        IP Address. . . . . . . . . . . . : 192.168.56.1
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        IP Address. . . . . . . . . . . . : fe80::a00:27ff:fe00:5ce8%7
        Default Gateway . . . . . . . . . :
        DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                            fec0:0:0:ffff::2%1
                                            fec0:0:0:ffff::3%1

The problem appears to be in collision of networks: 192.168.0.0/16 and 192.168.56.0/24. It seems that nevertheless the socket is bound to proper NIC, where the message is expected to be sent through, Windows Sockets takes a random matching  adapter for the transmission.

A correction of the IP address from 192.168.56.1 to 192.169.56.1. immediately fixes the problem. There however has been no problems with other applications, the collision did not bring any other issues.

Some more information on the topic: Windows XP Professional, 32-bit, Service Pack 3.

See also on MSDN Forums: http://social.msdn.microsoft.com/Forums/…/

Leave a Reply