diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2020-02-10 16:00:28 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2020-02-10 16:00:28 +0000 |
commit | 55bc8fead207fcb2aafe414ce84073f147ebce39 (patch) | |
tree | 377bbd9c61e9ac6c5c78e215ef961e8922dfc986 /src/libbcnm/bcnm_if_getstate.c | |
parent | fe28e76256c4ff7d1af53ad3940bf737f6000107 (diff) | |
download | bcnm-55bc8fead207fcb2aafe414ce84073f147ebce39.tar.xz |
Add bcnm-waitif
Diffstat (limited to 'src/libbcnm/bcnm_if_getstate.c')
-rw-r--r-- | src/libbcnm/bcnm_if_getstate.c | 30 |
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 ; +} |