2 #include <sys/socket.h>
7 #include <linux/if_tun.h>
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
22 int init_tun(char *dev) {{{
31 if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
32 fprintf(stderr, "opening /dev/net/tun: %m\n");
36 memset(&ifr, 0, sizeof(ifr));
38 ifr.ifr_flags = IFF_TUN;
41 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
43 if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
45 fprintf(stderr, "ioctl'ing /dev/net/tun: %m\n");
49 strcpy(dev, ifr.ifr_name);