2 #include <netinet/in.h>
3 #include <sys/socket.h>
15 * Return the IP address for the given interface or "no IP" if the
16 * interface is up and running but hasn't got an IP address yet
19 const char *get_ip_addr(const char *interface) {
20 static char part[512];
21 socklen_t len = sizeof(struct sockaddr_in);
22 memset(part, 0, sizeof(part));
24 struct ifaddrs *ifaddr, *addrp;
32 /* Skip until we are at the AF_INET address of interface */
36 (strcmp(addrp->ifa_name, interface) != 0 ||
37 addrp->ifa_addr == NULL ||
38 addrp->ifa_addr->sa_family != AF_INET));
40 addrp = addrp->ifa_next) {
41 /* Check if the interface is down */
42 if (strcmp(addrp->ifa_name, interface) != 0)
45 if ((addrp->ifa_flags & IFF_RUNNING) == 0) {
53 return (found ? "no IP" : NULL);
57 if ((ret = getnameinfo(addrp->ifa_addr, len, part, sizeof(part), NULL, 0, NI_NUMERICHOST)) != 0) {
58 fprintf(stderr, "getnameinfo(): %s\n", gai_strerror(ret));