-
Notifications
You must be signed in to change notification settings - Fork 14
/
iftap.c
67 lines (52 loc) · 1.21 KB
/
iftap.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* iftap.c */
#include "iftap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <err.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <arpa/inet.h>
int
tap_alloc (char * dev)
{
/* Create Tunnel Interface Linux */
int fd;
struct ifreq ifr;
if ((fd = open ("/dev/net/tun", O_RDWR)) < 0)
err (EXIT_FAILURE,
"cannot create a control cahnnel of the tun intface.");
memset (&ifr, 0, sizeof (ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
strncpy (ifr.ifr_name, dev, IFNAMSIZ);
if (ioctl (fd, TUNSETIFF, (void *) &ifr) < 0) {
close (fd);
err (EXIT_FAILURE,
"cannot create %s interface.", dev);
}
return fd;
}
int
tap_up (char * dev)
{
int udp_fd;
struct ifreq ifr;
/* Make Tunnel interface up state */
if ((udp_fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
err (EXIT_FAILURE,
"failt to create control socket of tap interface.");
memset (&ifr, 0, sizeof (ifr));
ifr.ifr_flags = IFF_UP;
strncpy (ifr.ifr_name, dev, IFNAMSIZ);
if (ioctl (udp_fd, SIOCSIFFLAGS, (void *)&ifr) < 0) {
err (EXIT_FAILURE,
"failed to make %s up.", dev);
close (udp_fd);
return -1;
}
close (udp_fd);
return 0;
}