blob: af502683151ba5f05767f95bf31ec8e38f4530f1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 ;
}
|