2 #include <sys/socket.h>
7 #include <linux/if_tun.h>
19 * Creates a tun-interface called dev;
20 * if *dev == 0, uses the name supplied by the kernel
21 * returns the fd to the tun or -1
23 int init_tun(char *dev) { /*{{{*/
27 if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
28 debug(1, 0, "opening /dev/net/tun: %s\n", strerror(errno));
32 memset(&ifr, 0, sizeof(ifr));
34 ifr.ifr_flags = IFF_TUN;
36 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
38 if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
40 debug(1, 0, "ioctl'ing /dev/net/tun: %s\n", strerror(errno));
43 strcpy(dev, ifr.ifr_name);
51 r = read(fd, buf, 1024);
53 fprintf(stderr, "n2o read: %s\n", strerror(errno));
63 fprintf(stderr, "n2o write: %s\n", strerror(errno));
76 r = read(0, buf, 1024);
78 fprintf(stderr, "o2n read: %s\n", strerror(errno));
86 w = write(fd, buf, r);
88 fprintf(stderr, "o2n write: %s\n", strerror(errno));