blob: ec8632f1ce052b678b1eeb76fbdd83b5c66e1815 (
plain) (
blame)
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
|
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
/**
* Creates a tun-interface called dev;
* dev is asumed to point to a char[IFNAMSIZ]
* if *dev == 0, uses the name supplied by the kernel
* returns the fd to the tun or -1
*/
int init_tun(char *dev) {{{
if (!dev) {
errno = EINVAL;
return -1;
}
struct ifreq ifr;
int fd, err;
if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
fprintf(stderr, "opening /dev/net/tun: %s\n", strerror(errno));
return -1;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN;
if (*dev)
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
close(fd);
fprintf(stderr, "ioctl'ing /dev/net/tun: %m\n");
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}}}
|