nietras
nietras

Reputation: 4048

Send UDP broadcast (255.255.255.255) from specific adapter (e.g. 192.168.101.1) only; on Windows

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?

Upvotes: 2

Views: 10191

Answers (5)

El Ronaldo
El Ronaldo

Reputation: 402

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.

Upvotes: 0

brickner
brickner

Reputation: 6585

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.

Upvotes: 1

JimB
JimB

Reputation: 1057

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

Upvotes: -2

Erich Mirabal
Erich Mirabal

Reputation: 10038

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.

Upvotes: 0

Chris Becke
Chris Becke

Reputation: 36026

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);

Upvotes: 2

Related Questions