I want to make it as generic as possible so that it can work on other private IP addresses as well, like 192.168.1.7 or 172.16.1.2. In that case, binding 192.168.1.6 and 192.168.1.255 won't work.
| [reply] |
Well, as far as I know you can only broadcast to one interface at a time. So, you have to decide which subnet is the intended destination so the kernel knows which interface to use for it, either by inspecting all the available networks and guessing, or by having the user provide you with the subnet-specific broadcast address. You could inspect the routing table looking for the default gateway and use that network, but this would give unwanted results if you ran it *on* the gateway host because then you'd be broadcasting to the Internet. You could guess if you *were* the gateway based on whether any interface has a ".1" IP address, but then that fails for your virtual machine scenario where the workstation is a client on the wireless network but also a gateway for the VMs' network.
The other option is to create one socket bound to each subnet and then send the broadcast packet to each of them.
Or, who knows, there may be tricks I don't know about to get a single broadcast message out via multiple interfaces at once. IPv6 has an awful lot of features I haven't read up on.
| [reply] |