forked from br101/horst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifctrl-ioctl.c
52 lines (42 loc) · 991 Bytes
/
ifctrl-ioctl.c
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <errno.h>
#include <net/if.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <err.h>
#include "ifctrl.h"
bool ifctrl_flags(const char *const interface, bool up, bool promisc)
{
int fd;
struct ifreq ifreq;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
return false;
memset(&ifreq, 0, sizeof(ifreq));
strncpy(ifreq.ifr_name, interface, IF_NAMESIZE - 1);
if (ioctl(fd, SIOCGIFFLAGS, &ifreq) == -1) {
warn("Could not get flags for %s", interface);
int orig_errno = errno;
close(fd);
errno = orig_errno;
return false;
}
if (up)
ifreq.ifr_flags |= IFF_UP;
else
ifreq.ifr_flags &= ~IFF_UP;
if (promisc)
ifreq.ifr_flags |= IFF_PROMISC;
else
ifreq.ifr_flags &= ~IFF_PROMISC;
if (ioctl(fd, SIOCSIFFLAGS, &ifreq) == -1) {
warn("Could not set flags for %s", interface);
int orig_errno = errno;
close(fd);
errno = orig_errno;
return false;
}
if (close(fd))
return false;
return true;
}