2 #include <sys/socket.h>
7 #include <linux/if_tun.h>
15 int tun_alloc(char *dev) {
19 if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
20 fprintf(stderr, "open: %s\n", strerror(errno));
24 memset(&ifr, 0, sizeof(ifr));
26 ifr.ifr_flags = IFF_TUN;
28 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
30 if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
32 fprintf(stderr, "ioctl: %s\n", strerror(errno));
35 strcpy(dev, ifr.ifr_name);
43 r = read(fd, buf, 1024);
45 fprintf(stderr, "n2o read: %s\n", strerror(errno));
55 fprintf(stderr, "n2o write: %s\n", strerror(errno));
68 r = read(0, buf, 1024);
70 fprintf(stderr, "o2n read: %s\n", strerror(errno));
78 w = write(fd, buf, r);
80 fprintf(stderr, "o2n write: %s\n", strerror(errno));
89 int main(int argc, char** argv) {
93 memset(name, 0, IFNAMSIZ);
95 strncpy(name, "mynet", IFNAMSIZ);
96 fprintf(stderr, "fd = %d, name = %s\n", fd = tun_alloc(name), name);