renamed files to fit into the convention
[oweals/gnunet.git] / src / vpn / gnunet-vpn-tun.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <sys/ioctl.h>
4 #include <sys/stat.h>
5
6 #include <linux/if.h>
7 #include <linux/if_tun.h>
8
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <stdlib.h>
15
16 /**
17  * Creates a tun-interface called dev;
18  * dev is asumed to point to a char[IFNAMSIZ]
19  * if *dev == 0, uses the name supplied by the kernel
20  * returns the fd to the tun or -1
21  */
22 int init_tun(char *dev) {{{
23         if (!dev) {
24                 errno = EINVAL;
25                 return -1;
26         }
27
28         struct ifreq ifr;
29         int fd, err;
30
31         if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
32                 fprintf(stderr, "opening /dev/net/tun: %m\n");
33                 return -1;
34         }
35
36         memset(&ifr, 0, sizeof(ifr));
37
38         ifr.ifr_flags = IFF_TUN;
39
40         if (*dev)
41                 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
42
43         if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
44                 close(fd);
45                 fprintf(stderr, "ioctl'ing /dev/net/tun: %m\n");
46                 return err;
47         }
48
49         strcpy(dev, ifr.ifr_name);
50         return fd;
51 }}}