2

Solution for Windows XP or higher. Preferably in C# or else in C++.

We do not want to broadcast using a subnet directed broadcast (e.g. 192.168.101.255) since the device we are trying to contact is not responding to this. Instead, we want to be able to send UDP datagram with a destination of 255.255.255.255 from a specific NIC/IPAddress only, such that the broadcast is NOT send out on other NICs.

This means we have to circumvent the IP stack, which is, thus, the question. How can we circumvent the IP stack on windows to send a UDP/IP compliant datagram from a specific NIC/MAC address only?

nietras
  • 3,949
  • 1
  • 34
  • 38

5 Answers5

3

Just bind() the socket to the desired interface instead of using INADDR_ANY ?

// Make a UDP socket
SOCKET s = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
// Bind it to a particular interface
sockaddr_in name={0};
name.sin_family = AF_INET;
name.sin_addr.s_addr = inet_addr("192.168.101.3"); // whatever the ip address of the NIC is.
name.sin_port = htons(PORT);
bind(s,(sockaddr*)name);
Chris Becke
  • 34,244
  • 12
  • 79
  • 148
  • 1
    Is this possible directly in C#? Or would you have to create your own wrapper in e.g. C++/CLI? – nietras Jun 02 '09 at 15:20
  • var localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.103.1"), 0); using (var socket = new UdpClient(localEndPoint)) { var remoteEndPoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 3956); var datagram = new byte[] { 0x11, 0x22, 0x33, 0x44 }; // Trying to send broadcast datagram from the local end point only // but the send broadcasts the packet from all NICs according to Wireshark socket.Send(datagram, datagram.Length, remoteEndPoint); } – nietras Jun 03 '09 at 09:24
  • Hmm how do you paste code correctly? Anyhoo, this does not work so the answer given does not work. – nietras Jun 03 '09 at 09:26
2

In order to use WinPcap to send a raw pcaket in C#, you can use Pcap.Net. It's a wrapper for WinPcap written in C++/CLI and C# and includes a packet interpretation and creation framework.

brickner
  • 6,595
  • 3
  • 41
  • 54
1

I've not tried this, but I know that WinPCap allows you to do a raw send. Since it works at a pretty low level, it might allow you to send low enough on the stack to bypass the full broadcast. There are various C# wrappers out there, and of course you can use the normal C/C++ code available out there. I think the trick might be to bind to the right adapter you want to send out of and it might just work.

Erich Mirabal
  • 9,860
  • 3
  • 34
  • 39
0

The broadcast address 255.255.255.255 is too general. You have to craft a different broadcast address for each network interface separately.

JimB
  • 971
  • 8
  • 20
0

I just posted an answer at a similar question, based on Chris Becke's response. It is certainly do-able in C#, probably in many fewer lines.

I'm not sure why OP thought it didn't work, except perhaps that Chris did not "dot all the i's". The code I showed in that post was copied from a working sample program.

El Ronaldo
  • 383
  • 1
  • 3
  • 11