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. */ /* See LICENSE file for copyright and license details. */
#include <ifaddrs.h> #include <ifaddrs.h>
#include <netdb.h> #include <netdb.h>
#include <net/if.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#if defined(__OpenBSD__) #if defined(__OpenBSD__)
@ -59,3 +60,28 @@ ipv6(const char *interface)
{ {
return ip(interface, AF_INET6); 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;
}

View File

@ -56,6 +56,7 @@ static const char unknown_str[] = "n/a";
* thermal zone on FreeBSD * thermal zone on FreeBSD
* (tz0, tz1, etc.) * (tz0, tz1, etc.)
* uid UID of current user NULL * uid UID of current user NULL
* up interface is running interface name (eth0)
* uptime system uptime NULL * uptime system uptime NULL
* username username of current user NULL * username username of current user NULL
* vol_perc OSS/ALSA volume in percent mixer file (/dev/mixer) * vol_perc OSS/ALSA volume in percent mixer file (/dev/mixer)

View File

@ -30,6 +30,7 @@ const char *hostname(const char *unused);
/* ip */ /* ip */
const char *ipv4(const char *interface); const char *ipv4(const char *interface);
const char *ipv6(const char *interface); const char *ipv6(const char *interface);
const char *up(const char *interface);
/* kernel_release */ /* kernel_release */
const char *kernel_release(const char *unused); const char *kernel_release(const char *unused);