I have an interface eth0 set up as 168.152.32.222 with netmask 255.255.255.0.
My server application wants to receive now UDP datagrams with the following destination addresses on eth0:
- 168.152.32.222
- 168.152.32.255
- 255.255.255.255
This seems like a rather reasonable request, yet I can't get the socket bound properly.
- If I bind to 168.152.32.222, I don't receive the broadcasts.
- If I bind to INADDR_ANY, I get datagrams from all interfaces.
- If I bind to INADDR_ANY and SO_BINDTODEVICE, I get packets which arrive on eth0 for different subnets, like 168.152.47.x.
In theory, I could just accept the datagrams and then filter all wrong packets out on my own with the netmask again - but seriously, some lower layer should do this, that's somehow the point of the netmask.
How to not receive UDP datagrams for the wrong subnet but still broadcasts?
A solution outside the server application is acceptable, too. I'm considering filtering the packets with iptables, not sure if this is feasible though and it would be the last resort.