URI:
       ip.c - slstatus - status monitor
  HTML git clone git://git.suckless.org/slstatus
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       ip.c (1743B)
       ---
            1 /* See LICENSE file for copyright and license details. */
            2 #include <ifaddrs.h>
            3 #include <netdb.h>
            4 #include <net/if.h>
            5 #include <stdio.h>
            6 #include <string.h>
            7 #if defined(__OpenBSD__)
            8         #include <sys/socket.h>
            9         #include <sys/types.h>
           10 #elif defined(__FreeBSD__)
           11         #include <netinet/in.h>
           12         #include <sys/socket.h>
           13 #endif
           14 
           15 #include "../slstatus.h"
           16 #include "../util.h"
           17 
           18 static const char *
           19 ip(const char *interface, unsigned short sa_family)
           20 {
           21         struct ifaddrs *ifaddr, *ifa;
           22         int s;
           23         char host[NI_MAXHOST];
           24 
           25         if (getifaddrs(&ifaddr) < 0) {
           26                 warn("getifaddrs:");
           27                 return NULL;
           28         }
           29 
           30         for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
           31                 if (!ifa->ifa_addr)
           32                         continue;
           33                 if (strcmp(ifa->ifa_name, interface) ||
           34                     ifa->ifa_addr->sa_family != sa_family)
           35                         continue;
           36 
           37                 s = getnameinfo(ifa->ifa_addr,
           38                                 (sa_family == AF_INET) ? sizeof(struct sockaddr_in)
           39                                                        : sizeof(struct sockaddr_in6),
           40                                 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
           41                 freeifaddrs(ifaddr);
           42                 if (s != 0) {
           43                         warn("getnameinfo: %s", gai_strerror(s));
           44                         return NULL;
           45                 }
           46                 return bprintf("%s", host);
           47         }
           48 
           49         freeifaddrs(ifaddr);
           50 
           51         return NULL;
           52 }
           53 
           54 const char *
           55 ipv4(const char *interface)
           56 {
           57         return ip(interface, AF_INET);
           58 }
           59 
           60 const char *
           61 ipv6(const char *interface)
           62 {
           63         return ip(interface, AF_INET6);
           64 }
           65 
           66 const char *
           67 up(const char *interface)
           68 {
           69         struct ifaddrs *ifaddr, *ifa;
           70 
           71         if (getifaddrs(&ifaddr) < 0) {
           72                 warn("getifaddrs:");
           73                 return NULL;
           74         }
           75 
           76         for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
           77                 if (!ifa->ifa_addr)
           78                         continue;
           79 
           80                 if (!strcmp(ifa->ifa_name, interface)) {
           81                         freeifaddrs(ifaddr);
           82                         return ifa->ifa_flags & IFF_UP ? "up" : "down";
           83                 }
           84         }
           85 
           86         freeifaddrs(ifaddr);
           87 
           88         return NULL;
           89 }