1
0

Simplify ipv* functions

This commit is contained in:
Aaron Marcher 2018-05-19 13:34:18 +02:00
parent d47d78255e
commit f31b113e7e

View File

@ -11,8 +11,8 @@
#include "../util.h" #include "../util.h"
const char * static const char *
ipv4(const char *iface) ip(const char *iface, unsigned short sa_family)
{ {
struct ifaddrs *ifaddr, *ifa; struct ifaddrs *ifaddr, *ifa;
int s; int s;
@ -27,10 +27,10 @@ ipv4(const char *iface)
if (!ifa->ifa_addr) { if (!ifa->ifa_addr) {
continue; continue;
} }
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6),
NI_MAXHOST, NULL, 0, NI_NUMERICHOST); host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (!strcmp(ifa->ifa_name, iface) && if (!strcmp(ifa->ifa_name, iface) &&
(ifa->ifa_addr->sa_family == AF_INET)) { (ifa->ifa_addr->sa_family == sa_family)) {
if (s != 0) { if (s != 0) {
warn("getnameinfo: %s", gai_strerror(s)); warn("getnameinfo: %s", gai_strerror(s));
return NULL; return NULL;
@ -44,35 +44,14 @@ ipv4(const char *iface)
return NULL; return NULL;
} }
const char *
ipv4(const char *iface)
{
return ip(iface, AF_INET);
}
const char * const char *
ipv6(const char *iface) ipv6(const char *iface)
{ {
struct ifaddrs *ifaddr, *ifa; return ip(iface, AF_INET6);
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 NULL;
} }