]> git.sur5r.net Git - i3/i3status/blobdiff - src/get_ip_addr.c
Add debian changelog entry to keep updates from overwriting local versions
[i3/i3status] / src / get_ip_addr.c
index 354ae08696f33768825e01c5c1bdad9cf9fa1119..6ddd35a125ef6c6d50e9ea522f1eff29bfa10b4e 100644 (file)
@@ -1,5 +1,5 @@
 // vim:ts=8:expandtab
-#include <net/if.h>
+#include <netinet/in.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <stdlib.h>
@@ -7,6 +7,7 @@
 #include <string.h>
 #include <netdb.h>
 #include <ifaddrs.h>
+#include <net/if.h>
 
 #include "i3status.h"
 
@@ -31,30 +32,36 @@ const char *get_ip_addr(const char *interface) {
 
         addrp = ifaddr;
 
-        /* Skip until we are at the AF_INET address of eth_interface */
+        /* Skip until we are at the AF_INET address of interface */
         for (addrp = ifaddr;
 
              (addrp != NULL &&
-              (strcmp(addrp->ifa_name, eth_interface) != 0 ||
+              (strcmp(addrp->ifa_name, 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)
+                if (strcmp(addrp->ifa_name, interface) == 0 &&
+                    (addrp->ifa_flags & IFF_RUNNING) == 0) {
+                        freeifaddrs(ifaddr);
                         return NULL;
+                }
         }
 
-        if (addrp == NULL)
+        if (addrp == NULL) {
+                freeifaddrs(ifaddr);
                 return "no IP";
+        }
 
         int ret;
         if ((ret = getnameinfo(addrp->ifa_addr, len, part, sizeof(part), NULL, 0, NI_NUMERICHOST)) != 0) {
                 fprintf(stderr, "getnameinfo(): %s\n", gai_strerror(ret));
+                freeifaddrs(ifaddr);
                 return "no IP";
         }
 
+        freeifaddrs(ifaddr);
         return part;
 }