diff options
author | Aaron Marcher <me@drkhsh.at> | 2017-08-14 18:00:46 +0200 |
---|---|---|
committer | Aaron Marcher <me@drkhsh.at> | 2017-08-14 18:00:46 +0200 |
commit | 8f5219b6dab5a1c9b7f92362a34aacfd393fa0e4 (patch) | |
tree | cc3aa76f993504020773a4c6510b048bf8e0acc4 | |
parent | fcc5c683a6cb6efa380d82e6af34b31181ea1751 (diff) |
Added IPv6 address function
- Renamed "ip" function to "ipv4"
- Added "ipv6" function
- Adjusted README and config.def.h accordingly
-rw-r--r-- | README | 2 | ||||
-rw-r--r-- | config.def.h | 3 | ||||
-rw-r--r-- | slstatus.c | 40 |
3 files changed, 39 insertions, 6 deletions
@@ -15,7 +15,7 @@ Features - Available entropy - Username/GID/UID - Hostname -- IP address +- IP address (IPv4 and IPv6) - Kernel version - Keyboard indicators - Load average diff --git a/config.def.h b/config.def.h index a55dafa..f4a6bed 100644 --- a/config.def.h +++ b/config.def.h @@ -25,7 +25,8 @@ static const char unknown_str[] = "n/a"; * entropy available entropy NULL * gid GID of current user NULL * hostname hostname NULL - * ip IPv4 address interface name + * ipv4 IPv4 address interface name + * ipv6 IPv6 address interface name * kernel_release `uname -r` NULL * keyboard_indicators caps/num lock indicators NULL * load_avg load average NULL @@ -48,7 +48,8 @@ static const char *disk_used(const char *mnt); static const char *entropy(void); static const char *gid(void); static const char *hostname(void); -static const char *ip(const char *iface); +static const char *ipv4(const char *iface); +static const char *ipv6(const char *iface); static const char *kernel_release(void); static const char *keyboard_indicators(void); static const char *load_avg(void); @@ -294,14 +295,14 @@ hostname(void) } static const char * -ip(const char *iface) +ipv4(const char *iface) { struct ifaddrs *ifaddr, *ifa; int s; char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { - warn("Failed to get IP address for interface %s", iface); + warn("Failed to get IPv4 address for interface %s", iface); return unknown_str; } @@ -312,7 +313,38 @@ ip(const char *iface) s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) { if (s != 0) { - warnx("Failed to get IP address for interface %s", iface); + warnx("Failed to get IPv4 address for interface %s", iface); + return unknown_str; + } + return bprintf("%s", host); + } + } + + freeifaddrs(ifaddr); + + return unknown_str; +} + +static const char * +ipv6(const char *iface) +{ + struct ifaddrs *ifaddr, *ifa; + int s; + char host[NI_MAXHOST]; + + if (getifaddrs(&ifaddr) == -1) { + warn("Failed to get IPv6 address for interface %s", iface); + return unknown_str; + } + + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr == NULL) { + continue; + } + s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET6)) { + if (s != 0) { + warnx("Failed to get IPv6 address for interface %s", iface); return unknown_str; } return bprintf("%s", host); |