Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving broadcasted packets when listening to a specific port

when setting up a socket via dgram.createSocket('udp4'); and NOT binding it to a specific port I do receive broadcasted packets which were sent to 255.255.255.255 But when I'm binding the same socket to the specific IP of my interface this.server.bind(67, host); I will NOT receive these broadcasted packets. Is this a normal behavior?

like image 666
soupdiver Avatar asked Sep 04 '12 20:09

soupdiver


2 Answers

The Javadoc for java.net.DatagramSocket says:

In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address. In some implementations, broadcast packets may also be received when a DatagramSocket is bound to a more specific address.

This is a pretty clear indication that the behaviour is platform-dependent, so Javascript sockets would be afflicted the same way.

According to my testing, Windows Vista 64 does not behave as described in the second sentence (i.e. does not receive broadcasts unless bound to INADDR_ANY).

like image 59
user207421 Avatar answered Oct 20 '22 23:10

user207421


Yes, this is expected behavior because, without a port or interface, dgram.createSocket() will:

bind to the "all interfaces" address on a random port (it does the right thing for both udp4 and udp6 sockets).

-- from the dgram API documentation on nodejs.org

which allows you to capture broadcast packets to any port on any available interface.

Proving both an interface and port to createSocket() restricts you to only capturing packets that have been broadcast to the network bound to the specified interface on the specified port.

You should note that to receive broadcasts to a specific interface, they will need to be sent to the broadcast address on/for the network which that interface serves.

So if your network is:

10.1.1.0 

with a netmask of:

255.255.255.252 

which is also:

10.1.1.0/29 (CIDR notation)

the correct broadcast address is:

10.1.1.7

Given your network address and netmask, tuxgraphics.org's "Network and IP address calculator" is a tool you can use to identify the correct broadcast address for your specific network.

like image 2
Rob Raisch Avatar answered Oct 21 '22 01:10

Rob Raisch