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?
getifaddris non-standard. I suppose you are using it on a linux system. The man page there has the note:You probably should check
ifa_addr->sa_familyif this has the family that you are expecting.