2017-09-17 15:45:03 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2017-09-17 14:18:17 +00:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2018-05-02 06:21:32 +00:00
|
|
|
#if defined(__OpenBSD__)
|
2018-05-07 09:21:59 +00:00
|
|
|
#include <sys/socket.h>
|
2022-10-27 21:18:30 +00:00
|
|
|
#include <sys/types.h>
|
2019-01-30 11:21:54 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
2018-05-02 06:21:32 +00:00
|
|
|
#endif
|
2017-09-17 14:18:17 +00:00
|
|
|
|
2022-10-26 20:14:53 +00:00
|
|
|
#include "../slstatus.h"
|
2022-10-27 21:18:30 +00:00
|
|
|
#include "../util.h"
|
2017-09-17 14:18:17 +00:00
|
|
|
|
2018-05-19 11:34:18 +00:00
|
|
|
static const char *
|
2018-07-06 06:08:48 +00:00
|
|
|
ip(const char *interface, unsigned short sa_family)
|
2017-09-17 14:18:17 +00:00
|
|
|
{
|
|
|
|
struct ifaddrs *ifaddr, *ifa;
|
|
|
|
int s;
|
|
|
|
char host[NI_MAXHOST];
|
|
|
|
|
2018-05-06 20:28:56 +00:00
|
|
|
if (getifaddrs(&ifaddr) < 0) {
|
2018-05-18 08:59:05 +00:00
|
|
|
warn("getifaddrs:");
|
2017-09-17 14:18:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
2022-10-27 22:51:34 +00:00
|
|
|
if (!ifa->ifa_addr)
|
2017-09-17 14:18:17 +00:00
|
|
|
continue;
|
2022-10-27 22:51:34 +00:00
|
|
|
|
2018-05-19 11:34:18 +00:00
|
|
|
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6),
|
|
|
|
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
|
2018-07-06 06:08:48 +00:00
|
|
|
if (!strcmp(ifa->ifa_name, interface) &&
|
2018-05-19 11:34:18 +00:00
|
|
|
(ifa->ifa_addr->sa_family == sa_family)) {
|
2018-05-20 13:53:48 +00:00
|
|
|
freeifaddrs(ifaddr);
|
2017-09-17 14:18:17 +00:00
|
|
|
if (s != 0) {
|
2018-05-18 08:59:05 +00:00
|
|
|
warn("getnameinfo: %s", gai_strerror(s));
|
2017-09-17 14:18:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return bprintf("%s", host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifaddr);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2018-07-06 06:08:48 +00:00
|
|
|
ipv4(const char *interface)
|
2017-09-17 14:18:17 +00:00
|
|
|
{
|
2018-07-06 06:08:48 +00:00
|
|
|
return ip(interface, AF_INET);
|
2018-05-19 11:34:18 +00:00
|
|
|
}
|
2017-09-17 14:18:17 +00:00
|
|
|
|
2018-05-19 11:34:18 +00:00
|
|
|
const char *
|
2018-07-06 06:08:48 +00:00
|
|
|
ipv6(const char *interface)
|
2018-05-19 11:34:18 +00:00
|
|
|
{
|
2018-07-06 06:08:48 +00:00
|
|
|
return ip(interface, AF_INET6);
|
2017-09-17 14:18:17 +00:00
|
|
|
}
|