diff options
author | Aaron Marcher <me@drkhsh.at> | 2018-05-19 13:34:18 +0200 |
---|---|---|
committer | Aaron Marcher <me@drkhsh.at> | 2018-05-19 13:34:18 +0200 |
commit | f31b113e7e85bf7057cb88a70d88c5ce9325e208 (patch) | |
tree | db5b1109c60c85286c21e285a305d5ced0243b0f /components/ip.c | |
parent | d47d78255ef8ede773c69a97e2cb5de78cee7da9 (diff) |
Simplify ipv* functions
Diffstat (limited to 'components/ip.c')
-rw-r--r-- | components/ip.c | 45 |
1 files changed, 12 insertions, 33 deletions
diff --git a/components/ip.c b/components/ip.c index fce2b66..0b6293e 100644 --- a/components/ip.c +++ b/components/ip.c @@ -11,8 +11,8 @@ #include "../util.h" -const char * -ipv4(const char *iface) +static const char * +ip(const char *iface, unsigned short sa_family) { struct ifaddrs *ifaddr, *ifa; int s; @@ -27,10 +27,10 @@ ipv4(const char *iface) if (!ifa->ifa_addr) { continue; } - s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, - NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), + host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if (!strcmp(ifa->ifa_name, iface) && - (ifa->ifa_addr->sa_family == AF_INET)) { + (ifa->ifa_addr->sa_family == sa_family)) { if (s != 0) { warn("getnameinfo: %s", gai_strerror(s)); return NULL; @@ -45,34 +45,13 @@ ipv4(const char *iface) } const char * -ipv6(const char *iface) +ipv4(const char *iface) { - struct ifaddrs *ifaddr, *ifa; - int s; - char host[NI_MAXHOST]; - - if (getifaddrs(&ifaddr) < 0) { - warn("getifaddrs:"); - return NULL; - } - - for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - if (!ifa->ifa_addr) { - continue; - } - s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host, - NI_MAXHOST, NULL, 0, NI_NUMERICHOST); - if (!strcmp(ifa->ifa_name, iface) && - (ifa->ifa_addr->sa_family == AF_INET6)) { - if (s != 0) { - warn("getnameinfo: %s", gai_strerror(s)); - return NULL; - } - return bprintf("%s", host); - } - } - - freeifaddrs(ifaddr); + return ip(iface, AF_INET); +} - return NULL; +const char * +ipv6(const char *iface) +{ + return ip(iface, AF_INET6); } |