
You can use Ethernet Broadcast address to contact to all your network infrastructure devices which often will help out, like DHCP (even DHCPv6) and so on. ICMP and even ARP are good sources for network sideband information. However you should be a bit more sensible than the famous This gives you a statistical hint on where the majority of the Internet is located from your point of view.

Hence it does not take long on a LAN to just ping them all. You can listen for IGMP or IBGP frames to find out some suitable gateway. Looking at the prefix gives you a hint about if it has some internal IP or a global one. IPv6 routers advertise themselves to give you the right IPv6 prefix. This gives you a good impression which one is your real public interface, even if your default route points elsewhere. This even might be on a route where your default route does not point to. You can try to enumerate all the UPnP gateways on your network and this way find out a proper route for some "external" thing. The way of the drunken man who tries to drown himself in alcohol Here already seems to be an answer for this approach. If you do know what you do not want, you can enumerate all interfaces and ignore all the false ones. Enumerate all interfaces and eliminate those you do not want

So you can call your script/program /path/to/your/script like this I've done a quick example below which will print all of the machine's IPv4 address, (you should also check the getifaddrs was successful ie returns 0).

However getifaddress() will let you do the same thing easily, it works fine for me on os x 10.5 and should be the same below. I found the ioctl solution problematic on os x (which is POSIX compliant so should be similiar to linux).
