2017-09-17 17:45:03 +02:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2018-03-28 19:46:27 +02:00
|
|
|
#include <errno.h>
|
2017-09-17 16:18:17 +02:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2018-05-02 08:21:32 +02:00
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
2017-09-17 16:18:17 +02:00
|
|
|
|
2017-09-24 15:33:01 +02:00
|
|
|
#include "../util.h"
|
2017-09-17 16:18:17 +02:00
|
|
|
|
|
|
|
const char *
|
|
|
|
ipv4(const char *iface)
|
|
|
|
{
|
|
|
|
struct ifaddrs *ifaddr, *ifa;
|
|
|
|
int s;
|
|
|
|
char host[NI_MAXHOST];
|
|
|
|
|
2018-05-06 22:28:56 +02:00
|
|
|
if (getifaddrs(&ifaddr) < 0) {
|
2018-03-28 19:46:27 +02:00
|
|
|
fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
|
2017-09-17 16:18:17 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
2018-05-06 22:28:56 +02:00
|
|
|
if (!ifa->ifa_addr) {
|
2017-09-17 16:18:17 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-05-06 22:28:56 +02:00
|
|
|
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host,
|
|
|
|
NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
|
|
|
if (!strcmp(ifa->ifa_name, iface) &&
|
|
|
|
(ifa->ifa_addr->sa_family == AF_INET)) {
|
2017-09-17 16:18:17 +02:00
|
|
|
if (s != 0) {
|
2018-03-28 19:46:27 +02:00
|
|
|
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
|
2017-09-17 16:18:17 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return bprintf("%s", host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifaddr);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ipv6(const char *iface)
|
|
|
|
{
|
|
|
|
struct ifaddrs *ifaddr, *ifa;
|
|
|
|
int s;
|
|
|
|
char host[NI_MAXHOST];
|
|
|
|
|
2018-05-06 22:28:56 +02:00
|
|
|
if (getifaddrs(&ifaddr) < 0) {
|
2018-03-28 19:46:27 +02:00
|
|
|
fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
|
2017-09-17 16:18:17 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
2018-05-06 22:28:56 +02:00
|
|
|
if (!ifa->ifa_addr) {
|
2017-09-17 16:18:17 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-05-06 22:28:56 +02:00
|
|
|
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)) {
|
2017-09-17 16:18:17 +02:00
|
|
|
if (s != 0) {
|
2018-03-28 19:46:27 +02:00
|
|
|
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
|
2017-09-17 16:18:17 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return bprintf("%s", host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifaddr);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|