summaryrefslogtreecommitdiff
path: root/src/libbcnm/bcnm_if_getstate.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2020-02-10 16:00:28 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2020-02-10 16:00:28 +0000
commit55bc8fead207fcb2aafe414ce84073f147ebce39 (patch)
tree377bbd9c61e9ac6c5c78e215ef961e8922dfc986 /src/libbcnm/bcnm_if_getstate.c
parentfe28e76256c4ff7d1af53ad3940bf737f6000107 (diff)
downloadbcnm-55bc8fead207fcb2aafe414ce84073f147ebce39.tar.xz
Add bcnm-waitif
Diffstat (limited to 'src/libbcnm/bcnm_if_getstate.c')
-rw-r--r--src/libbcnm/bcnm_if_getstate.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/libbcnm/bcnm_if_getstate.c b/src/libbcnm/bcnm_if_getstate.c
new file mode 100644
index 0000000..af50268
--- /dev/null
+++ b/src/libbcnm/bcnm_if_getstate.c
@@ -0,0 +1,30 @@
+/* ISC license. */
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+
+#include <bcnm/if.h>
+
+int bcnm_if_getstate (char const *ifname)
+{
+ struct ifreq blah ;
+ int sfd ;
+ int e = errno ;
+ size_t len = strlen(ifname) ;
+ if (len >= IFNAMSIZ) return (errno = ENAMETOOLONG, -1) ;
+ memcpy(blah.ifr_name, ifname, len+1) ;
+ sfd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0) ;
+ if (sfd < 0) return -1 ;
+ if (ioctl(sfd, SIOCGIFFLAGS, &blah) < 0)
+ return errno == ENODEV ? (errno = e, 0) : -1 ;
+ close(sfd) ;
+ return 1 | !!(blah.ifr_flags & IFF_UP) << 1 | !!(blah.ifr_flags & IFF_RUNNING) << 2 ;
+}