diff options
author | Philipp Tölke <toelke@in.tum.de> | 2010-06-28 12:37:43 +0000 |
---|---|---|
committer | Philipp Tölke <toelke@in.tum.de> | 2010-06-28 12:37:43 +0000 |
commit | 7a2378763af1b240ac943a3d499f792bde48d02a (patch) | |
tree | 3df258ebddc68120950234399d28d284bc159ecf /src | |
parent | 0bf6a2223e06988b5c84bb110aff12150b54ed93 (diff) | |
download | gnunet-7a2378763af1b240ac943a3d499f792bde48d02a.tar.gz gnunet-7a2378763af1b240ac943a3d499f792bde48d02a.zip |
vpn: tcp-natting in one direction
untested and most likly unworking code
Diffstat (limited to 'src')
-rw-r--r-- | src/vpn/Makefile | 14 | ||||
-rw-r--r-- | src/vpn/packet.c | 14 | ||||
-rw-r--r-- | src/vpn/packet.h | 2 | ||||
-rw-r--r-- | src/vpn/tcp.c | 146 | ||||
-rw-r--r-- | src/vpn/tcp.h | 8 | ||||
-rw-r--r-- | src/vpn/test.c | 3 |
6 files changed, 176 insertions, 11 deletions
diff --git a/src/vpn/Makefile b/src/vpn/Makefile index 783c66d92..d2d4342bb 100644 --- a/src/vpn/Makefile +++ b/src/vpn/Makefile | |||
@@ -3,11 +3,19 @@ CXXFLAGS = ${CFLAGS} | |||
3 | 3 | ||
4 | LDFLAGS = | 4 | LDFLAGS = |
5 | 5 | ||
6 | all:default | 6 | .PHONY: .dependencies |
7 | 7 | ||
8 | default: test | 8 | all: default |
9 | 9 | ||
10 | test: test.o tun.o packet.o debug.o pretty-print.o | 10 | default: .dependencies test |
11 | |||
12 | test: test.o tun.o packet.o debug.o pretty-print.o tcp.o | ||
13 | |||
14 | .dependencies: | ||
15 | echo > .dependencies | ||
16 | gcc -M *.c >> .dependencies | ||
17 | |||
18 | -include .dependencies | ||
11 | 19 | ||
12 | clean: | 20 | clean: |
13 | rm -f *.o | 21 | rm -f *.o |
diff --git a/src/vpn/packet.c b/src/vpn/packet.c index 37ce44863..2d52dbc81 100644 --- a/src/vpn/packet.c +++ b/src/vpn/packet.c | |||
@@ -9,12 +9,12 @@ | |||
9 | #include "debug.h" | 9 | #include "debug.h" |
10 | #include "packet.h" | 10 | #include "packet.h" |
11 | 11 | ||
12 | long payload(struct ip6_pkt* pkt) {{{ | 12 | long payload(struct ip6_hdr* hdr) {{{ |
13 | return (pkt->hdr.paylgth[0] << 8) + pkt->hdr.paylgth[1]; | 13 | return (hdr->paylgth[0] << 8) + hdr->paylgth[1]; |
14 | }}} | 14 | }}} |
15 | 15 | ||
16 | void send_pkt(int fd, struct ip6_pkt* pkt) {{{ | 16 | void send_pkt(int fd, struct ip6_pkt* pkt) {{{ |
17 | int sz = payload(pkt); | 17 | int sz = payload(&(pkt->hdr)); |
18 | int w = 0; | 18 | int w = 0; |
19 | char* buf = (char*)malloc(sz+40); | 19 | char* buf = (char*)malloc(sz+40); |
20 | 20 | ||
@@ -120,8 +120,8 @@ struct ip6_pkt* parse_ip6(struct pkt_tun* pkt) {{{ | |||
120 | pkt6->hdr.dadr[w] = pkt->data[24+w]; | 120 | pkt6->hdr.dadr[w] = pkt->data[24+w]; |
121 | } | 121 | } |
122 | 122 | ||
123 | pkt6->data = (unsigned char*)malloc(payload(pkt6)); | 123 | pkt6->data = (unsigned char*)malloc(payload(&(pkt6->hdr))); |
124 | memcpy(pkt6->data, pkt->data+40, payload(pkt6)); | 124 | memcpy(pkt6->data, pkt->data+40, payload(&(pkt6->hdr))); |
125 | 125 | ||
126 | return pkt6; | 126 | return pkt6; |
127 | }}} | 127 | }}} |
@@ -150,8 +150,8 @@ struct ip6_tcp* parse_ip6_tcp(struct ip6_pkt* pkt) {{{ | |||
150 | res->data.opt = (unsigned char*) malloc((res->data.off - 5)*4); | 150 | res->data.opt = (unsigned char*) malloc((res->data.off - 5)*4); |
151 | memcpy(res->data.opt, pkt->data+20, (res->data.off - 5)*4); | 151 | memcpy(res->data.opt, pkt->data+20, (res->data.off - 5)*4); |
152 | 152 | ||
153 | res->data.data = (unsigned char*) malloc(payload(pkt) - 4*(res->data.off)); | 153 | res->data.data = (unsigned char*) malloc(payload(&(pkt->hdr)) - 4*(res->data.off)); |
154 | memcpy(res->data.data, pkt->data+4*(res->data.off), payload(pkt) - 4*(res->data.off)); | 154 | memcpy(res->data.data, pkt->data+4*(res->data.off), payload(&(pkt->hdr)) - 4*(res->data.off)); |
155 | 155 | ||
156 | return res; | 156 | return res; |
157 | }}} | 157 | }}} |
diff --git a/src/vpn/packet.h b/src/vpn/packet.h index 504690936..4044eecb0 100644 --- a/src/vpn/packet.h +++ b/src/vpn/packet.h | |||
@@ -45,6 +45,6 @@ extern struct ip6_pkt* parse_ip6(struct pkt_tun* pkt); | |||
45 | 45 | ||
46 | struct ip6_tcp* parse_ip6_tcp(struct ip6_pkt*); | 46 | struct ip6_tcp* parse_ip6_tcp(struct ip6_pkt*); |
47 | 47 | ||
48 | extern long payload(struct ip6_pkt* pkt); | 48 | extern long payload(struct ip6_hdr* pkt); |
49 | 49 | ||
50 | #endif | 50 | #endif |
diff --git a/src/vpn/tcp.c b/src/vpn/tcp.c new file mode 100644 index 000000000..cd40f280a --- /dev/null +++ b/src/vpn/tcp.c | |||
@@ -0,0 +1,146 @@ | |||
1 | #include "debug.h" | ||
2 | #include "packet.h" | ||
3 | #include "tcp.h" | ||
4 | |||
5 | #include <errno.h> | ||
6 | #include <netinet/in.h> | ||
7 | #include <netinet/ip.h> | ||
8 | #include <signal.h> | ||
9 | #include <stdlib.h> | ||
10 | #include <arpa/inet.h> | ||
11 | #include <string.h> | ||
12 | #include <sys/socket.h> | ||
13 | #include <sys/types.h> | ||
14 | #include <unistd.h> | ||
15 | |||
16 | struct le { | ||
17 | int spt, dpt; | ||
18 | |||
19 | unsigned char sadr[16]; | ||
20 | unsigned char dadr[16]; | ||
21 | |||
22 | int socket; | ||
23 | |||
24 | struct le* next; | ||
25 | }; | ||
26 | |||
27 | static struct le* le_head = 0; | ||
28 | |||
29 | static int look_for_child(struct ip6_tcp* pkt) { | ||
30 | struct le* cur; | ||
31 | |||
32 | for(cur = le_head; cur != 0; cur = cur->next) { | ||
33 | if (cur->spt == pkt->data.spt &&/*{{{*/ | ||
34 | cur->dpt == pkt->data.spt && | ||
35 | cur->sadr[0] == pkt->hdr.sadr[0] && | ||
36 | cur->sadr[1] == pkt->hdr.sadr[1] && | ||
37 | cur->sadr[2] == pkt->hdr.sadr[2] && | ||
38 | cur->sadr[3] == pkt->hdr.sadr[3] && | ||
39 | cur->sadr[4] == pkt->hdr.sadr[4] && | ||
40 | cur->sadr[5] == pkt->hdr.sadr[5] && | ||
41 | cur->sadr[6] == pkt->hdr.sadr[6] && | ||
42 | cur->sadr[7] == pkt->hdr.sadr[7] && | ||
43 | cur->sadr[8] == pkt->hdr.sadr[8] && | ||
44 | cur->sadr[9] == pkt->hdr.sadr[9] && | ||
45 | cur->sadr[10] == pkt->hdr.sadr[10] && | ||
46 | cur->sadr[11] == pkt->hdr.sadr[11] && | ||
47 | cur->sadr[12] == pkt->hdr.sadr[12] && | ||
48 | cur->sadr[13] == pkt->hdr.sadr[13] && | ||
49 | cur->sadr[14] == pkt->hdr.sadr[14] && | ||
50 | cur->sadr[15] == pkt->hdr.sadr[15] && | ||
51 | |||
52 | cur->dadr[0] == pkt->hdr.dadr[0] && | ||
53 | cur->dadr[1] == pkt->hdr.dadr[1] && | ||
54 | cur->dadr[2] == pkt->hdr.dadr[2] && | ||
55 | cur->dadr[3] == pkt->hdr.dadr[3] && | ||
56 | cur->dadr[4] == pkt->hdr.dadr[4] && | ||
57 | cur->dadr[5] == pkt->hdr.dadr[5] && | ||
58 | cur->dadr[6] == pkt->hdr.dadr[6] && | ||
59 | cur->dadr[7] == pkt->hdr.dadr[7] && | ||
60 | cur->dadr[8] == pkt->hdr.dadr[8] && | ||
61 | cur->dadr[9] == pkt->hdr.dadr[9] && | ||
62 | cur->dadr[10] == pkt->hdr.dadr[10] && | ||
63 | cur->dadr[11] == pkt->hdr.dadr[11] && | ||
64 | cur->dadr[12] == pkt->hdr.dadr[12] && | ||
65 | cur->dadr[13] == pkt->hdr.dadr[13] && | ||
66 | cur->dadr[14] == pkt->hdr.dadr[14] && | ||
67 | cur->dadr[15] == pkt->hdr.dadr[15])/*}}}*/ | ||
68 | return cur->socket; | ||
69 | } | ||
70 | return -1; | ||
71 | } | ||
72 | |||
73 | static struct le* new_le() {{{ | ||
74 | struct le* res = (struct le*) malloc(sizeof(struct le)); | ||
75 | |||
76 | struct le** cur; | ||
77 | |||
78 | for(cur = &le_head; *cur != 0; cur = &((*cur)->next)) {} | ||
79 | |||
80 | *cur = res; | ||
81 | |||
82 | return res; | ||
83 | }}} | ||
84 | |||
85 | static int nat(struct ip6_tcp* pkt) {{{ | ||
86 | unsigned char adr1[] = { 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}; | ||
87 | unsigned char adr2[] = { 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}; | ||
88 | |||
89 | if (strncmp((char*)adr1, (char*)pkt->hdr.dadr, 16)) { | ||
90 | int sock = socket(AF_INET, SOCK_STREAM, 0); | ||
91 | struct sockaddr_in info; | ||
92 | memset(&info, 0, sizeof(info)); | ||
93 | |||
94 | info.sin_family = AF_INET; | ||
95 | info.sin_port = pkt->data.dpt; | ||
96 | inet_pton(AF_INET, "94.142.241.111", &info.sin_addr.s_addr); | ||
97 | |||
98 | connect(sock, (const struct sockaddr*)&info, sizeof(info)); | ||
99 | return sock; | ||
100 | } else if (strncmp((char*)adr2, (char*)pkt->hdr.dadr, 16)) { | ||
101 | int sock = socket(AF_INET6, SOCK_STREAM, 0); | ||
102 | |||
103 | struct sockaddr_in6 info; | ||
104 | memset(&info, 0, sizeof(info)); | ||
105 | |||
106 | info.sin6_family = AF_INET6; | ||
107 | info.sin6_port = pkt->data.dpt; | ||
108 | |||
109 | inet_pton(AF_INET6, "2a02:898:17:8000::42", info.sin6_addr.s6_addr); | ||
110 | |||
111 | connect(sock, (const struct sockaddr*)&info, sizeof(info)); | ||
112 | |||
113 | return sock; | ||
114 | } | ||
115 | return -1; | ||
116 | }}} | ||
117 | |||
118 | void handle_tcp(struct ip6_tcp* pkt) { | ||
119 | signal(SIGCHLD, SIG_IGN); | ||
120 | |||
121 | int fd = look_for_child(pkt); | ||
122 | |||
123 | if (fd == -1) { | ||
124 | struct le* le = new_le(); | ||
125 | le->spt = pkt->data.spt; | ||
126 | le->dpt = pkt->data.dpt; | ||
127 | |||
128 | memcpy(le->sadr, pkt->hdr.sadr, 16); | ||
129 | memcpy(le->dadr, pkt->hdr.dadr, 16); | ||
130 | |||
131 | le->socket = nat(pkt); | ||
132 | fd = le->socket; | ||
133 | } | ||
134 | |||
135 | int size = payload((&pkt->hdr)) - pkt->data.off; | ||
136 | |||
137 | int w = 0; | ||
138 | while (size > 0) { | ||
139 | w = write(fd, pkt->data.data, size - w); | ||
140 | if (w < 0) { | ||
141 | debug(1, 0, "writing: %s\n", strerror(errno)); | ||
142 | } else { | ||
143 | size -= w; | ||
144 | } | ||
145 | } | ||
146 | } | ||
diff --git a/src/vpn/tcp.h b/src/vpn/tcp.h new file mode 100644 index 000000000..c933b1b4c --- /dev/null +++ b/src/vpn/tcp.h | |||
@@ -0,0 +1,8 @@ | |||
1 | #ifndef _GNTUN_TCP_H_ | ||
2 | #define _GNTUN_TCP_H_ | ||
3 | |||
4 | #include "packet.h" | ||
5 | |||
6 | extern void handle_tcp(struct ip6_tcp*); | ||
7 | |||
8 | #endif | ||
diff --git a/src/vpn/test.c b/src/vpn/test.c index acdf4249a..3672b9df2 100644 --- a/src/vpn/test.c +++ b/src/vpn/test.c | |||
@@ -1,6 +1,7 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include <sys/socket.h> | 3 | #include <sys/socket.h> |
4 | #include <string.h> | ||
4 | 5 | ||
5 | #include <linux/if.h> | 6 | #include <linux/if.h> |
6 | 7 | ||
@@ -8,6 +9,7 @@ | |||
8 | #include "tun.h" | 9 | #include "tun.h" |
9 | #include "debug.h" | 10 | #include "debug.h" |
10 | #include "pretty-print.h" | 11 | #include "pretty-print.h" |
12 | #include "tcp.h" | ||
11 | 13 | ||
12 | int main(int c, char** v) { | 14 | int main(int c, char** v) { |
13 | char dev[IFNAMSIZ]; | 15 | char dev[IFNAMSIZ]; |
@@ -32,6 +34,7 @@ int main(int c, char** v) { | |||
32 | pkt_printf(pkt6); | 34 | pkt_printf(pkt6); |
33 | struct ip6_tcp* pkt6_tcp = parse_ip6_tcp(pkt6); | 35 | struct ip6_tcp* pkt6_tcp = parse_ip6_tcp(pkt6); |
34 | pkt_printf_ip6tcp(pkt6_tcp); | 36 | pkt_printf_ip6tcp(pkt6_tcp); |
37 | handle_tcp(pkt6_tcp); | ||
35 | break; | 38 | break; |
36 | } | 39 | } |
37 | break; | 40 | break; |