Is getifaddrs giving me duplicate network interface addresses?

c++, network-programming

Solution

`getifaddr` is non-standard. I suppose you are using it on a linux system. The man page there has the note:

The addresses returned on Linux will usually be the IPv4 and IPv6 addresses assigned to the interface, but also one AF_PACKET address per interface containing lower-level details about the interface and its physical layer. In this case, the ifa_data field may contain a pointer to a struct net_device_stats, defined in , which contains various interface attributes and statistics.

You probably should check `ifa_addr->sa_family` if this has the family that you are expecting.

Problem

I am looking for a particular network address using a small snippet like: ``` char name[INET_ADDRSTRLEN]; struct ifaddrs *iflist; if (getifaddrs(&iflist) < 0) perror("getifaddrs"); struct in_addr addr; for (struct ifaddrs *p = iflist; p; p = p->ifa_next) { if (strcmp(p->ifa_name, "lo") == 0) { addr = reinterpret_cast<struct sockaddr_in*>(p->ifa_addr)->sin_addr; if (inet_ntop(AF_INET, &addr, name, sizeof(name)) == NULL) { perror("inet_ntop"); continue; } cout << name << " ---> " << if_nametoindex("lo") << " : " << addr.s_addr << endl; } } ``` And the output i get is: ``` 1.0.0.0 ---> 1 : 1 127.0.0.1 ---> 1 : 16777343 ``` I don't understand the first result..this doesnt happen with something like eth# but it does with another interface called bond0. What is this?

Original source