{"id":1038,"date":"2009-09-19T21:42:51","date_gmt":"2009-09-19T19:42:51","guid":{"rendered":"https:\/\/alax.info\/blog\/?p=1038"},"modified":"2009-09-22T16:49:48","modified_gmt":"2009-09-22T14:49:48","slug":"udp-message-sent-off-wrong-network-interface","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/1038","title":{"rendered":"UDP message sent off wrong network interface"},"content":{"rendered":"<p>This is a funny one and I wonder what is exactly causing such misbehavior. An UDP broadcasting socket is being bound to IP address <strong>192.168.0.2<\/strong>:<\/p>\n<pre>__E<span style=\"color: #808030;\">(<\/span>m_Socket<span style=\"color: #808030;\">.<\/span>Create<span style=\"color: #808030;\">(<\/span>SOCK_DGRAM<span style=\"color: #808030;\">,<\/span> IPPROTO_UDP<span style=\"color: #808030;\">)<\/span> <span style=\"color: #808030;\">!<\/span><span style=\"color: #808030;\">=<\/span> INVALID_SOCKET<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\n<span style=\"color: #800000; font-weight: bold;\">static<\/span> <span style=\"color: #800000; font-weight: bold;\">const<\/span> <span style=\"color: #603000;\">DWORD<\/span> g_nBroadcastValue <span style=\"color: #808030;\">=<\/span> <span style=\"color: #008c00;\">1<\/span><span style=\"color: #800080;\">;<\/span>\r\n__E<span style=\"color: #808030;\">(<\/span>m_Socket<span style=\"color: #808030;\">.<\/span>SetOption<span style=\"color: #808030;\">(<\/span>SOL_SOCKET<span style=\"color: #808030;\">,<\/span> SO_BROADCAST<span style=\"color: #808030;\">,<\/span> <span style=\"color: #808030;\">&amp;<\/span>g_nBroadcastValue<span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000; font-weight: bold;\">sizeof<\/span> g_nBroadcastValue<span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\n<span style=\"color: #400000;\">ZeroMemory<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #808030;\">&amp;<\/span>m_LocalAddress<span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000; font-weight: bold;\">sizeof<\/span> m_LocalAddress<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\nm_LocalAddress<span style=\"color: #808030;\">.<\/span>sin_family <span style=\"color: #808030;\">=<\/span> AF_INET<span style=\"color: #800080;\">;<\/span>\r\nm_LocalAddress<span style=\"color: #808030;\">.<\/span>sin_port <span style=\"color: #808030;\">=<\/span> <span style=\"color: #400000;\">htons<\/span><span style=\"color: #808030;\">(<\/span>nPort<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #696969;\">\/\/ <strong><span style=\"color: #ff0000;\">7365<\/span><\/strong><\/span>\r\nm_LocalAddress<span style=\"color: #808030;\">.<\/span>sin_addr<span style=\"color: #808030;\">.<\/span>S_un<span style=\"color: #808030;\">.<\/span>S_addr <span style=\"color: #808030;\">=<\/span> <span style=\"color: #400000;\">htonl<\/span><span style=\"color: #808030;\">(<\/span>nLocalIpAddress<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #696969;\">\/\/ <strong><span style=\"color: #ff0000;\">192.168.0.2<\/span><\/strong><\/span>\r\n_Z4<span style=\"color: #808030;\">(<\/span>atlTraceGeneral<span style=\"color: #808030;\">,<\/span> <span style=\"color: #008c00;\">4<\/span><span style=\"color: #808030;\">,<\/span> _T<span style=\"color: #808030;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #0000e6;\">m_Socket <\/span><span style=\"color: #0f69ff;\">%d<\/span><span style=\"color: #0000e6;\">, m_LocalAddress.sin_addr %hs<\/span><span style=\"color: #0f69ff;\">\\n<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">,<\/span> m_Socket<span style=\"color: #808030;\">,<\/span> <span style=\"color: #400000;\">inet_ntoa<\/span><span style=\"color: #808030;\">(<\/span>m_LocalAddress<span style=\"color: #808030;\">.<\/span>sin_addr<span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\n__E<span style=\"color: #808030;\">(<\/span><span style=\"color: #400000;\">bind<\/span><span style=\"color: #808030;\">(<\/span>m_Socket<span style=\"color: #808030;\">,<\/span> <span style=\"color: #808030;\">(<\/span>SOCKADDR<span style=\"color: #808030;\">*<\/span><span style=\"color: #808030;\">)<\/span> <span style=\"color: #808030;\">&amp;<\/span>m_LocalAddress<span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000; font-weight: bold;\">sizeof<\/span> m_LocalAddress<span style=\"color: #808030;\">)<\/span> <span style=\"color: #808030;\">!<\/span><span style=\"color: #808030;\">=<\/span> SOCKET_ERROR<span style=\"color: #808030;\">)<\/span><\/pre>\n<p>Then off this socket a broadcast message is sent using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms740148%28VS.85%29.aspx\">sendto<\/a>:<\/p>\n<pre>SOCKADDR_IN RemoteAddress<span style=\"color: #800080;\">;<\/span>\r\n<span style=\"color: #400000;\">ZeroMemory<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #808030;\">&amp;<\/span>RemoteAddress<span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000; font-weight: bold;\">sizeof<\/span> RemoteAddress<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\nRemoteAddress<span style=\"color: #808030;\">.<\/span>sin_family <span style=\"color: #808030;\">=<\/span> AF_INET<span style=\"color: #800080;\">;<\/span>\r\nRemoteAddress<span style=\"color: #808030;\">.<\/span>sin_port <span style=\"color: #808030;\">=<\/span> <span style=\"color: #400000;\">htons<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #008c00;\">7364<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\nRemoteAddress<span style=\"color: #808030;\">.<\/span>sin_addr<span style=\"color: #808030;\">.<\/span>S_un<span style=\"color: #808030;\">.<\/span>S_addr <span style=\"color: #808030;\">=<\/span> INADDR_BROADCAST<span style=\"color: #800080;\">;<\/span>\r\n_Z4<span style=\"color: #808030;\">(<\/span>atlTraceGeneral<span style=\"color: #808030;\">,<\/span> <span style=\"color: #008c00;\">4<\/span><span style=\"color: #808030;\">,<\/span> _T<span style=\"color: #808030;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #0000e6;\">BroadcastSocketData.m_Socket <\/span><span style=\"color: #0f69ff;\">%d<\/span><span style=\"color: #0f69ff;\">\\n<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">,<\/span> BroadcastSocketData<span style=\"color: #808030;\">.<\/span>m_Socket<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\n__E<span style=\"color: #808030;\">(<\/span><span style=\"color: #400000;\">sendto<\/span><span style=\"color: #808030;\">(<\/span>BroadcastSocketData<span style=\"color: #808030;\">.<\/span>m_Socket<span style=\"color: #808030;\">,<\/span> <span style=\"color: #7d0045;\">NULL<\/span><span style=\"color: #808030;\">,<\/span> <span style=\"color: #008c00;\">0<\/span><span style=\"color: #808030;\">,<\/span> <span style=\"color: #008c00;\">0<\/span><span style=\"color: #808030;\">,<\/span> <span style=\"color: #808030;\">(<\/span><span style=\"color: #800000; font-weight: bold;\">const<\/span> SOCKADDR<span style=\"color: #808030;\">*<\/span><span style=\"color: #808030;\">)<\/span> <span style=\"color: #808030;\">&amp;<\/span>RemoteAddress<span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000; font-weight: bold;\">sizeof<\/span> RemoteAddress<span style=\"color: #808030;\">)<\/span> <span style=\"color: #808030;\">!<\/span><span style=\"color: #808030;\">=<\/span> SOCKET_ERROR<span style=\"color: #808030;\">)<\/span><\/pre>\n<p>The problem however is that <a href=\"http:\/\/www.wireshark.org\/\">WireShark<\/a> shows the message going off the wrong network interface, with IP address <strong>192.168.56.1<\/strong>, while <strong>192.68.0.2<\/strong> is an IP address on a different network adapter:<\/p>\n<pre>C:\\&gt;ipconfig \/all\r\n\r\nEthernet adapter Home Area Connection:\r\n\r\n        Connection-specific DNS Suffix  . :\r\n        Description . . . . . . . . . . . : Intel(R) 82566DC-2 Gigabit Network Connection\r\n        Dhcp Enabled. . . . . . . . . . . : No\r\n        IP Address. . . . . . . . . . . . : 192.168.0.2\r\n        Subnet Mask . . . . . . . . . . . : 255.255.0.0\r\n        Default Gateway . . . . . . . . . : 192.168.0.1\r\n        DNS Servers . . . . . . . . . . . : 192.168.0.1\r\n                                            4.2.2.4\r\n\r\nEthernet adapter VirtualBox Host-Only Network:\r\n\r\n        Connection-specific DNS Suffix  . :\r\n        Description . . . . . . . . . . . : VirtualBox Host-Only Ethernet Adapter\r\n        Dhcp Enabled. . . . . . . . . . . : No\r\n        IP Address. . . . . . . . . . . . : 192.168.56.1\r\n        Subnet Mask . . . . . . . . . . . : 255.255.255.0\r\n        IP Address. . . . . . . . . . . . : fe80::a00:27ff:fe00:5ce8%7\r\n        Default Gateway . . . . . . . . . :\r\n        DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1\r\n                                            fec0:0:0:ffff::2%1\r\n                                            fec0:0:0:ffff::3%1<\/pre>\n<p>The problem appears to be in collision of networks: <strong>192.168.0.0\/16<\/strong> and <strong>192.168.56.0\/24<\/strong>. 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\u00a0 adapter for the transmission.<\/p>\n<p>A correction of the IP address from <strong>192.16<span style=\"text-decoration: underline;\">8<\/span>.56.1<\/strong> to <strong>192.16<span style=\"text-decoration: underline;\">9<\/span>.56.1<\/strong>. immediately fixes the problem. There however has been no problems with other applications, the collision did not bring any other issues.<\/p>\n<p>Some more information on the topic: Windows XP Professional, 32-bit, Service Pack 3.<\/p>\n<p>See also on MSDN Forums: <a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/windowsgeneraldevelopmentissues\/thread\/6099ec30-bd94-4e1d-a3ed-9a7b92d1bb86\/\">http:\/\/social.msdn.microsoft.com\/Forums\/&#8230;\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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, &amp;g_nBroadcastValue, sizeof g_nBroadcastValue)); ZeroMemory(&amp;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); \/\/&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/1038\">Read the full article<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[256,254,253,250,255,251,252],"class_list":["post-1038","post","type-post","status-publish","format-standard","hentry","category-technology","tag-adapter","tag-broadcast","tag-datagram","tag-network","tag-nic","tag-socket","tag-udp"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/1038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/comments?post=1038"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/1038\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=1038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=1038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=1038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}