diff options
author | Philipp Tölke <toelke@in.tum.de> | 2010-07-20 05:45:18 +0000 |
---|---|---|
committer | Philipp Tölke <toelke@in.tum.de> | 2010-07-20 05:45:18 +0000 |
commit | 425e80a6d25faa505513cacd10a3667a48c62d1e (patch) | |
tree | 173b3f8ade639be6d3a876bd088305e0dad69eca /src/vpn | |
parent | 775bf0a991b97e92a5da28a8d3cebcb364f83fb7 (diff) | |
download | gnunet-425e80a6d25faa505513cacd10a3667a48c62d1e.tar.gz gnunet-425e80a6d25faa505513cacd10a3667a48c62d1e.zip |
Begin implementing the gnunet-vpn-helper
Diffstat (limited to 'src/vpn')
-rw-r--r-- | src/vpn/Makefile.am | 17 | ||||
-rw-r--r-- | src/vpn/gnunet-vpn-helper-p.h | 9 | ||||
-rw-r--r-- | src/vpn/gnunet-vpn-helper.c | 82 | ||||
-rw-r--r-- | src/vpn/tun.c | 72 |
4 files changed, 116 insertions, 64 deletions
diff --git a/src/vpn/Makefile.am b/src/vpn/Makefile.am index 1fff59309..480d58145 100644 --- a/src/vpn/Makefile.am +++ b/src/vpn/Makefile.am | |||
@@ -23,12 +23,17 @@ bin_PROGRAMS = \ | |||
23 | 23 | ||
24 | 24 | ||
25 | gnunet_vpn_helper_SOURCES = \ | 25 | gnunet_vpn_helper_SOURCES = \ |
26 | debug.c debug.h \ | 26 | gnunet-vpn-helper.c \ |
27 | packet.h packet.c \ | 27 | gnunet-vpn-helper-p.h \ |
28 | pretty-print.c pretty-print.h \ | 28 | tun.h tun.c |
29 | tcp.c tcp.h \ | 29 | |
30 | test.c \ | 30 | # debug.c debug.h \ |
31 | tun.c tun.h | 31 | # packet.h packet.c \ |
32 | # pretty-print.c pretty-print.h \ | ||
33 | # tcp.c tcp.h \ | ||
34 | # test.c \ | ||
35 | # tun.c tun.h \ | ||
36 | # udp.c udp.h | ||
32 | 37 | ||
33 | gnunet_daemon_vpn_SOURCES = \ | 38 | gnunet_daemon_vpn_SOURCES = \ |
34 | gnunet-daemon-vpn.c | 39 | gnunet-daemon-vpn.c |
diff --git a/src/vpn/gnunet-vpn-helper-p.h b/src/vpn/gnunet-vpn-helper-p.h new file mode 100644 index 000000000..d2fac593a --- /dev/null +++ b/src/vpn/gnunet-vpn-helper-p.h | |||
@@ -0,0 +1,9 @@ | |||
1 | #ifndef GN_VPN_HELPER_P_H | ||
2 | #define GN_VPN_HELPER_P_H | ||
3 | |||
4 | struct suid_packet { | ||
5 | unsigned int size; | ||
6 | unsigned char data[1]; | ||
7 | }; | ||
8 | |||
9 | #endif | ||
diff --git a/src/vpn/gnunet-vpn-helper.c b/src/vpn/gnunet-vpn-helper.c new file mode 100644 index 000000000..66af3ca51 --- /dev/null +++ b/src/vpn/gnunet-vpn-helper.c | |||
@@ -0,0 +1,82 @@ | |||
1 | #define _GNU_SOURCE | ||
2 | #include <arpa/inet.h> | ||
3 | #include <linux/if.h> | ||
4 | |||
5 | #include <sys/types.h> | ||
6 | #include <sys/socket.h> | ||
7 | #include <sys/ioctl.h> | ||
8 | |||
9 | #include <string.h> | ||
10 | |||
11 | #include <stdio.h> | ||
12 | #include <unistd.h> | ||
13 | |||
14 | #include "gnunet-vpn-helper-p.h" | ||
15 | #include "tun.h" | ||
16 | |||
17 | #ifndef _LINUX_IN6_H | ||
18 | // This is in linux/include/net/ipv6.h. | ||
19 | |||
20 | struct in6_ifreq { | ||
21 | struct in6_addr ifr6_addr; | ||
22 | __u32 ifr6_prefixlen; | ||
23 | unsigned int ifr6_ifindex; | ||
24 | }; | ||
25 | |||
26 | #endif | ||
27 | |||
28 | static void set_address(char* dev, char* address, unsigned long prefix_len) { /* {{{ */ | ||
29 | int fd = socket(AF_INET6, SOCK_DGRAM, 0); | ||
30 | |||
31 | struct ifreq ifr; | ||
32 | struct in6_ifreq ifr6; | ||
33 | |||
34 | struct sockaddr_in6 sa6; | ||
35 | memset(&sa6, 0, sizeof(struct sockaddr_in6)); | ||
36 | |||
37 | sa6.sin6_family = AF_INET6; | ||
38 | |||
39 | /* FIXME */ inet_pton(AF_INET6, address, sa6.sin6_addr.s6_addr); | ||
40 | |||
41 | memcpy((char *) &ifr6.ifr6_addr, (char *) &sa6.sin6_addr, sizeof(struct in6_addr)); | ||
42 | |||
43 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); | ||
44 | |||
45 | if (ioctl(fd, SIOGIFINDEX, &ifr) < 0) { | ||
46 | perror("SIOGIFINDEX"); | ||
47 | } | ||
48 | |||
49 | ifr6.ifr6_ifindex = ifr.ifr_ifindex; | ||
50 | ifr6.ifr6_prefixlen = prefix_len; | ||
51 | |||
52 | if (ioctl(fd, SIOCSIFADDR, &ifr6) < 0) { | ||
53 | perror("SIOCSIFADDR"); | ||
54 | } | ||
55 | |||
56 | /* FIXME */ ioctl(fd, SIOCGIFFLAGS, &ifr); | ||
57 | ifr.ifr_flags |= IFF_UP | IFF_RUNNING; | ||
58 | /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr); | ||
59 | } /* }}} */ | ||
60 | |||
61 | int main(int argc, char** argv) { | ||
62 | char dev[IFNAMSIZ]; | ||
63 | memset(dev, 0, IFNAMSIZ); | ||
64 | |||
65 | int fd_tun = init_tun(dev); | ||
66 | fprintf(stderr, "Initialized the interface %s as %d.\n", dev, fd_tun); | ||
67 | |||
68 | // TODO: get this out of argv | ||
69 | char address[] = "1234::1"; | ||
70 | unsigned long prefix_len = 8; | ||
71 | |||
72 | set_address(dev, address, prefix_len); | ||
73 | |||
74 | uid_t uid = getuid (); | ||
75 | if (setresuid (uid, uid, uid) != 0 ) | ||
76 | fprintf (stderr, "Failed to setresuid: %m\n"); | ||
77 | |||
78 | // Wait | ||
79 | read(0, dev, 10); | ||
80 | |||
81 | return 0; | ||
82 | } | ||
diff --git a/src/vpn/tun.c b/src/vpn/tun.c index d3c38bb4d..e3854495f 100644 --- a/src/vpn/tun.c +++ b/src/vpn/tun.c | |||
@@ -13,83 +13,39 @@ | |||
13 | #include <errno.h> | 13 | #include <errno.h> |
14 | #include <stdlib.h> | 14 | #include <stdlib.h> |
15 | 15 | ||
16 | #include "debug.h" | ||
17 | |||
18 | /** | 16 | /** |
19 | * Creates a tun-interface called dev; | 17 | * Creates a tun-interface called dev; |
18 | * dev is asumed to point to a char[IFNAMSIZ] | ||
20 | * if *dev == 0, uses the name supplied by the kernel | 19 | * if *dev == 0, uses the name supplied by the kernel |
21 | * returns the fd to the tun or -1 | 20 | * returns the fd to the tun or -1 |
22 | */ | 21 | */ |
23 | int init_tun(char *dev) { /*{{{*/ | 22 | int init_tun(char *dev) {{{ |
23 | if (!dev) { | ||
24 | errno = EINVAL; | ||
25 | return -1; | ||
26 | } | ||
27 | |||
24 | struct ifreq ifr; | 28 | struct ifreq ifr; |
25 | int fd, err; | 29 | int fd, err; |
26 | 30 | ||
27 | if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) { | 31 | if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) { |
28 | debug(1, 0, "opening /dev/net/tun: %s\n", strerror(errno)); | 32 | fprintf(stderr, "opening /dev/net/tun: %m\n"); |
29 | return -1; | 33 | return -1; |
30 | } | 34 | } |
31 | 35 | ||
32 | memset(&ifr, 0, sizeof(ifr)); | 36 | memset(&ifr, 0, sizeof(ifr)); |
33 | 37 | ||
34 | ifr.ifr_flags = IFF_TUN; | 38 | ifr.ifr_flags = IFF_TUN; |
35 | if(dev) | 39 | |
40 | if (*dev) | ||
36 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); | 41 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); |
37 | 42 | ||
38 | if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){ | 43 | if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){ |
39 | close(fd); | 44 | close(fd); |
40 | debug(1, 0, "ioctl'ing /dev/net/tun: %s\n", strerror(errno)); | 45 | fprintf(stderr, "ioctl'ing /dev/net/tun: %m\n"); |
41 | return err; | 46 | return err; |
42 | } | 47 | } |
48 | |||
43 | strcpy(dev, ifr.ifr_name); | 49 | strcpy(dev, ifr.ifr_name); |
44 | return fd; | 50 | return fd; |
45 | } /*}}}*/ | 51 | }}} |
46 | |||
47 | void n2o(int fd) { | ||
48 | char buf[1024]; | ||
49 | int r, w; | ||
50 | for(;;) { | ||
51 | r = read(fd, buf, 1024); | ||
52 | if (r < 0) { | ||
53 | fprintf(stderr, "n2o read: %s\n", strerror(errno)); | ||
54 | exit(1); | ||
55 | } | ||
56 | if (r == 0) { | ||
57 | close(fd); | ||
58 | exit(0); | ||
59 | } | ||
60 | while (r > 0) { | ||
61 | w = write(1, buf, r); | ||
62 | if (w < 0) { | ||
63 | fprintf(stderr, "n2o write: %s\n", strerror(errno)); | ||
64 | close(fd); | ||
65 | exit(1); | ||
66 | } | ||
67 | r -= w; | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | |||
72 | void o2n(int fd) { | ||
73 | char buf[1024]; | ||
74 | int r, w; | ||
75 | for(;;) { | ||
76 | r = read(0, buf, 1024); | ||
77 | if (r < 0) { | ||
78 | fprintf(stderr, "o2n read: %s\n", strerror(errno)); | ||
79 | exit(1); | ||
80 | } | ||
81 | if (r == 0) { | ||
82 | close(fd); | ||
83 | exit(0); | ||
84 | } | ||
85 | while (r > 0) { | ||
86 | w = write(fd, buf, r); | ||
87 | if (w < 0) { | ||
88 | fprintf(stderr, "o2n write: %s\n", strerror(errno)); | ||
89 | close(fd); | ||
90 | exit(1); | ||
91 | } | ||
92 | r -= w; | ||
93 | } | ||
94 | } | ||
95 | } | ||