2 #include <linux/if_tun.h>
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
10 int init_tun(char *dev) {{{
19 if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
20 fprintf(stderr, "opening /dev/net/tun: %s\n", strerror(errno));
24 memset(&ifr, 0, sizeof(ifr));
26 ifr.ifr_flags = IFF_TUN;
29 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
31 if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
33 fprintf(stderr, "ioctl'ing /dev/net/tun: %s\n", strerror(errno));
37 strcpy(dev, ifr.ifr_name);