I think there is no way to enumerate each network interface on my system and their assigned IP address using just sockets. Is this correct?
I mean, in Linux this could be:
eth0: 192.168.1.5
wlan0: 192.168.0.5
lo: 127.0.0.1
I don't care about interface names, just the IP addresses assigned.
I recall to have done this in the past in Windows, using Win32 (though I don't remember how). But is there a method to do this in a portable way?
Take a look at the
ioctl()
function. If I recall correctly, you can use it to obtain information on any interface, as well as obtaining the available interfaces.I don't remember the correct invocation though. As with
fcntl()
, it takes a request argument plus variable parameters that determine its behaviour.On Linux, that information is accessed through NETLINK sockets. See the manual pages for libnetlink(3), netlink(7) and rtnetlink(7).
A somewhat more portable way of doing this is via ioctl()'s. SIOCGIFCONF is the one you want. See the manual page for netdevice(7). This one works on some other *nixes.
Here's a good start:
Use
gethostname()
to retreive the machine's local DNS name, and then pass that name togethostbyname()
to get its local IP addresses.If the OS supports IPv6, then look at the
getaddrinfo()
function instead ofgethostbyname()
.