add 'up' for whether a interface is up or down

This commit is contained in:
sewn
2024-03-16 00:20:54 +03:00
committed by drkhsh
parent b6267f7d0b
commit af508f0b4c
3 changed files with 28 additions and 0 deletions

View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */
#include <ifaddrs.h>
#include <netdb.h>
#include <net/if.h>
#include <stdio.h>
#include <string.h>
#if defined(__OpenBSD__)
@ -59,3 +60,28 @@ ipv6(const char *interface)
{
return ip(interface, AF_INET6);
}
const char *
up(const char *interface)
{
struct ifaddrs *ifaddr, *ifa;
if (getifaddrs(&ifaddr) < 0) {
warn("getifaddrs:");
return NULL;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr)
continue;
if (!strcmp(ifa->ifa_name, interface)) {
freeifaddrs(ifaddr);
return ifa->ifa_flags & IFF_UP ? "up" : "down";
}
}
freeifaddrs(ifaddr);
return NULL;
}