for w32 port
[oweals/gnunet.git] / src / vpn / gnunet-vpn-tun.c
1 #include "platform.h"
2 #include <linux/if_tun.h>
3
4 /**
5  * Creates a tun-interface called dev;
6  * dev is asumed to point to a char[IFNAMSIZ]
7  * if *dev == 0, uses the name supplied by the kernel
8  * returns the fd to the tun or -1
9  */
10 int init_tun(char *dev) {{{
11         if (!dev) {
12                 errno = EINVAL;
13                 return -1;
14         }
15
16         struct ifreq ifr;
17         int fd, err;
18
19         if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
20                 fprintf(stderr, "opening /dev/net/tun: %s\n", strerror(errno));
21                 return -1;
22         }
23
24         memset(&ifr, 0, sizeof(ifr));
25
26         ifr.ifr_flags = IFF_TUN;
27
28         if (*dev)
29                 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
30
31         if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
32                 close(fd);
33                 fprintf(stderr, "ioctl'ing /dev/net/tun: %s\n", strerror(errno));
34                 return err;
35         }
36
37         strcpy(dev, ifr.ifr_name);
38         return fd;
39 }}}