Receiving Udp Broadcast Packets on Linux

UDP Broadcast packets is seen by tcpdump but not received by linux socket

On Linux you need to bind to a broadcast address of the interface (e.g. 192.168.253.255) or INADDR_BROADCAST, in order to receive broadcasts.

   rwhoBroadcastAddr.sin_addr.s_addr = INADDR_BROADCAST;

To avoid "address in use" errors, set socket option SO_REUSEADDR:

   int on = 1;
if (setsockopt(rwho_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
perror("SO_REUSEADDR");
}

Note: on Linux, SO_BROADCAST is only needed for sending broadcasts, it's not needed for receiving them (don't know about other OS).

Finally: you don't need to fill in rwhoSrcBroadcastAddr, it will be filled by recvfrom (is an output parameter).

How to receive a UDP broadcast sent to 255.255.255.255 using boost asio?

I finally discovered why my code was never seeing the broadcast packets even though tcpdump proved that they were being received. After finding this StackOverflow question:

Disable reverse path filtering from Linux kernel space

it seems that I just needed to disable Reverse Path Filtering on both hosts like this:

echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/eth0/rp_filter

and then my code worked perfectly with no modifications. Hopefully this will help other people wondering why they can't get UDP broadcasts to the network broadcast address (255.255.255.255) to work.

Unable to receive UDP broadcast messages

So I finally found the problem, it was dumb. I post the solution here to maybe help someone else.

My subnet mask was defaulted to 255.0.0.0, which is incorrect to get a broadcast packet. Changing the subnet mask to 255.255.255.0 allows the code to get broadcast packets.

Receive UDP broadast with no address assigned to interface with Linux/C/IPv4

I solved it. "reverse path filtering" was activated, causing all IPv4 packets which would take a different return path (or which have no route) to be dropped.
If no address is configured for that interface, every packet gets dropped.

echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter

did the trick.

UDP broadcast being sent, but not received


machineAddr.sin_port = htons(portNumber);
inet_pton(AF_INET, GetIP().c_str(), &(machineAddr.sin_addr));

:

bind(linuxSocket, reinterpret_cast<sockaddr*>(&machineAddr),

This binds the socket to only accept packets sent to portNumber at the machine address returned by GetIP, which is probably not what you want, as you also want to receive packets sent to the port at the broadcast address. You probably want to set sin_addr to be INADDR_ANY, the wildcard address, which will allow the socket to receive packets sent to the port at any address that gets to the machine somehow.

Receiving UDP broadcast

As it turns out my code is perfectly fine, as I thought it would be. There was a problem with the network setup itself.

For posterity, I had set up two static IP'd computers on their own hub, instead of using the built in DHCP server on the server machine to allocate the IP address for the other computer. Pretty localized for my problem but you never know..

UDP Broadcast receive - Bind to several NIC's

If I understand you correctly, you have two NICs, connected to two physical networks (i.e. network cables, hubs), with each having a separate IP address from the same subnet address range?

The short answer is that your network configuration is wrong. If they really are separate physical networks, then they should have different subnet addresses. It depends on what you mean with separate physical networks, separate hardware? You can NOT have two separate subnets with the same subnet address. Thats why I am saying your network configuration is wrong.

However, The impression I get is that you are trying to bridge the two networks, so that the two NICs belong to the same subnet (not separate). Well, then you should bridge them. You bridge the two NICs together and assign ONE IP address to the bridge. Then you will be able to receive your packets on both NICs.

In linux:

brctl addbr br0
ifconfig eth0 0.0.0.0 down
ifconfig eth1 0.0.0.0 down
brctl addif br0 eth0
brctl addif br0 eth1
ifconfig eth0 up
ifconfig eth1 up
ifconfig br0 up
ifconfig br0 192.168.225.107 (or 192.168.225.108, whatever you prefer)


Related Topics



Leave a reply



Submit