Actually, the whole heuristic just picks a proper interface. It contains no heuristic for a proper IP address of the interface. It just prints the first one. getifaddrs() returns linked list of struct ifaddrs. for (p = addrs; p; p = p->ifa_next) goex through addresses, but once any single address is found (if (addr)), it is printed and function returns. If I comment out the "return" there, it will print all addresses. We always need to go through all returned addresses and pick the most proper one.