diff options
author | Philipp Tölke <toelke@in.tum.de> | 2010-06-28 12:37:34 +0000 |
---|---|---|
committer | Philipp Tölke <toelke@in.tum.de> | 2010-06-28 12:37:34 +0000 |
commit | bada4affc0b7d79f9fabba26ab2fb780ed22ae6a (patch) | |
tree | 780974f763e1c0c02bedbe6ecfdb5bafd3830911 | |
parent | 0e116f0428b7c2d14c85ff6b795c30009e229e19 (diff) | |
download | gnunet-bada4affc0b7d79f9fabba26ab2fb780ed22ae6a.tar.gz gnunet-bada4affc0b7d79f9fabba26ab2fb780ed22ae6a.zip |
a first simple test-program for the vpn-functionality
-rw-r--r-- | src/vpn/tun.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/vpn/tun.c b/src/vpn/tun.c new file mode 100644 index 000000000..6e184b079 --- /dev/null +++ b/src/vpn/tun.c | |||
@@ -0,0 +1,104 @@ | |||
1 | #include <sys/types.h> | ||
2 | #include <sys/socket.h> | ||
3 | #include <sys/ioctl.h> | ||
4 | #include <sys/stat.h> | ||
5 | |||
6 | #include <linux/if.h> | ||
7 | #include <linux/if_tun.h> | ||
8 | |||
9 | #include <fcntl.h> | ||
10 | #include <unistd.h> | ||
11 | #include <stdio.h> | ||
12 | #include <string.h> | ||
13 | #include <errno.h> | ||
14 | |||
15 | int tun_alloc(char *dev) { | ||
16 | struct ifreq ifr; | ||
17 | int fd, err; | ||
18 | |||
19 | if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) { | ||
20 | fprintf(stderr, "open: %s\n", strerror(errno)); | ||
21 | return -1; | ||
22 | } | ||
23 | |||
24 | memset(&ifr, 0, sizeof(ifr)); | ||
25 | |||
26 | ifr.ifr_flags = IFF_TUN; | ||
27 | if(*dev) | ||
28 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); | ||
29 | |||
30 | if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){ | ||
31 | close(fd); | ||
32 | fprintf(stderr, "ioctl: %s\n", strerror(errno)); | ||
33 | return err; | ||
34 | } | ||
35 | strcpy(dev, ifr.ifr_name); | ||
36 | return fd; | ||
37 | } | ||
38 | |||
39 | void n2o(fd) { | ||
40 | char buf[1024]; | ||
41 | int r, w; | ||
42 | for(;;) { | ||
43 | r = read(fd, buf, 1024); | ||
44 | if (r < 0) { | ||
45 | fprintf(stderr, "n2o read: %s\n", strerror(errno)); | ||
46 | exit(1); | ||
47 | } | ||
48 | if (r == 0) { | ||
49 | close(fd); | ||
50 | exit(0); | ||
51 | } | ||
52 | while (r > 0) { | ||
53 | w = write(1, buf, r); | ||
54 | if (w < 0) { | ||
55 | fprintf(stderr, "n2o write: %s\n", strerror(errno)); | ||
56 | close(fd); | ||
57 | exit(1); | ||
58 | } | ||
59 | r -= w; | ||
60 | } | ||
61 | } | ||
62 | } | ||
63 | |||
64 | void o2n(fd) { | ||
65 | char buf[1024]; | ||
66 | int r, w; | ||
67 | for(;;) { | ||
68 | r = read(0, buf, 1024); | ||
69 | if (r < 0) { | ||
70 | fprintf(stderr, "o2n read: %s\n", strerror(errno)); | ||
71 | exit(1); | ||
72 | } | ||
73 | if (r == 0) { | ||
74 | close(fd); | ||
75 | exit(0); | ||
76 | } | ||
77 | while (r > 0) { | ||
78 | w = write(fd, buf, r); | ||
79 | if (w < 0) { | ||
80 | fprintf(stderr, "o2n write: %s\n", strerror(errno)); | ||
81 | close(fd); | ||
82 | exit(1); | ||
83 | } | ||
84 | r -= w; | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | |||
89 | int main(int argc, char** argv) { | ||
90 | char name[IFNAMSIZ]; | ||
91 | int fd; | ||
92 | |||
93 | memset(name, 0, IFNAMSIZ); | ||
94 | |||
95 | strncpy(name, "mynet", IFNAMSIZ); | ||
96 | fprintf(stderr, "fd = %d, name = %s\n", fd = tun_alloc(name), name); | ||
97 | |||
98 | if (fork() == 0) | ||
99 | n2o(fd); | ||
100 | |||
101 | o2n(fd); | ||
102 | |||
103 | return 0; | ||
104 | } | ||