From 4a6deddf7b3e38203d27a10baf7ba21ff90d96d3 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Wed, 22 Jul 2009 18:27:55 +0200 Subject: [PATCH] Obtain IP address in a portable way, patch by Baptiste Daroussin --- src/get_ip_addr.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/get_ip_addr.c b/src/get_ip_addr.c index ba39a3d..354ae08 100644 --- a/src/get_ip_addr.c +++ b/src/get_ip_addr.c @@ -1,12 +1,12 @@ // vim:ts=8:expandtab #include #include -#include #include #include #include #include #include +#include #include "i3status.h" @@ -17,24 +17,40 @@ */ const char *get_ip_addr(const char *interface) { static char part[512]; - struct ifreq ifr; socklen_t len = sizeof(struct sockaddr_in); memset(part, 0, sizeof(part)); - /* First check if the interface is running */ - (void)strcpy(ifr.ifr_name, interface); - if (ioctl(general_socket, SIOCGIFFLAGS, &ifr) < 0 || - !(ifr.ifr_flags & IFF_RUNNING)) - return NULL; + struct ifaddrs *ifaddr, *addrp; - /* Interface is up, get the IP address */ - (void)strcpy(ifr.ifr_name, interface); - ifr.ifr_addr.sa_family = AF_INET; - if (ioctl(general_socket, SIOCGIFADDR, &ifr) < 0) + getifaddrs(&ifaddr); + + if (ifaddr == NULL) { + (void)snprintf(part, sizeof(part), "E: down"); + return part; + } + + addrp = ifaddr; + + /* Skip until we are at the AF_INET address of eth_interface */ + for (addrp = ifaddr; + + (addrp != NULL && + (strcmp(addrp->ifa_name, eth_interface) != 0 || + addrp->ifa_addr == NULL || + addrp->ifa_addr->sa_family != AF_INET)); + + addrp = addrp->ifa_next) { + /* Check if the interface is down */ + if (strcmp(addrp->ifa_name, eth_interface) == 0 && + (addrp->ifa_flags & IFF_RUNNING) == 0) + return NULL; + } + + if (addrp == NULL) return "no IP"; int ret; - if ((ret = getnameinfo(&ifr.ifr_addr, len, part, sizeof(part), NULL, 0, NI_NUMERICHOST)) != 0) { + if ((ret = getnameinfo(addrp->ifa_addr, len, part, sizeof(part), NULL, 0, NI_NUMERICHOST)) != 0) { fprintf(stderr, "getnameinfo(): %s\n", gai_strerror(ret)); return "no IP"; } -- 2.39.5