summaryrefslogtreecommitdiff
path: root/components/ip.c
diff options
context:
space:
mode:
authorsewn <sewn@disroot.org>2024-03-16 00:20:54 +0300
committerdrkhsh <me@drkhsh.at>2025-04-30 03:09:55 +0200
commitaf508f0b4cf88b1921c59fdf5dd1a98c08223fa5 (patch)
tree56b37faf5e15573e3b9b2a11e233bbd62df3cf5f /components/ip.c
parentb6267f7d0bedf5789206adf21f33cfe1a872289c (diff)
add 'up' for whether a interface is up or down
Diffstat (limited to 'components/ip.c')
-rw-r--r--components/ip.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/components/ip.c b/components/ip.c
index 9476549..2cdad46 100644
--- a/components/ip.c
+++ b/components/ip.c
@@ -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;
+}