I am writing a C library which involves telling other computers on the subnet to send me messages. Therefore I must announce to them my IP address. This library should work on Linux, OS X and Windows. Currently I'm thinking mostly about the POSIX layer.
Given that a computer can have more than one address (for example if it has more than one network interface),
What's the best way to find my default IP address (e.g. in the simplest most common case of a computer with a single connection to the subnet.) Currently I'm cycling through the system NICs using
getifaddrs
and returning the first one that is associated (with preference to non-loopback), but I think this is probably not adequate.What would be a good API for allowing users of the library to choose which interface to send on? I assume some sort of enumeration of the NICs and IPs of the computer, then selecting the NIC by name and finding its IP. This seems pretty complicated from a user standpoint though. Perhaps some way to determine the system's route to the subnet by netmask, given the IP of another computer on the subnet? I have little idea how to do that, though.
For 2, we can eventually assume there may be a GUI or command-line option allowing users to choose the NIC, but in general I want to make it as easy as possible for it to automatically tell other computers how to direct UDP messages to it.
Any opinions appreciated, thanks!