I’m working on a communication protocol that should support self configuration by broadcasting / multicasting the peers’ address over the local network. The intuitive way would be to broadcast the address, but as it turns out, it’s pretty hard to reliably figure out the local IP address of the current machine (depending on the configuration, you might get “127.0.0.1” or another useless address).
The alternative is to not include the host address in the broadcast message, but to have the receivers call recvfrom on their socket which not only returns the received data, but also the sender’s address. As I see it, that call is available on both Unix and Windows (one of my requirements) and probably some more platforms. My question now is, are there situations where this might fail and recvfrom returns an unreachable or otherwise useless address?
If you limit this technique to only broadcast UDP, you should be fine. The only things that tend to mess with this are things like dual NAT or hairpin NAT. That’s just not done with broadcasts that are local only anyway.