diff options
author | Philipp Tölke <toelke@in.tum.de> | 2010-06-28 12:37:38 +0000 |
---|---|---|
committer | Philipp Tölke <toelke@in.tum.de> | 2010-06-28 12:37:38 +0000 |
commit | 947e2bd0312aacc2e1eb57b56c094386208c3931 (patch) | |
tree | 0c0a868f786101e3726fc3daeed57a12de10491a | |
parent | 9da3a783a66c1d1bf57cd838625c95e5ba083c62 (diff) | |
download | gnunet-947e2bd0312aacc2e1eb57b56c094386208c3931.tar.gz gnunet-947e2bd0312aacc2e1eb57b56c094386208c3931.zip |
vpn: pull the pretty-printer out of the packet-file
-rw-r--r-- | src/vpn/Makefile | 2 | ||||
-rw-r--r-- | src/vpn/packet.c | 138 | ||||
-rw-r--r-- | src/vpn/packet.h | 2 | ||||
-rw-r--r-- | src/vpn/pretty-print.c | 141 | ||||
-rw-r--r-- | src/vpn/pretty-print.h | 12 |
5 files changed, 158 insertions, 137 deletions
diff --git a/src/vpn/Makefile b/src/vpn/Makefile index 848faf24d..783c66d92 100644 --- a/src/vpn/Makefile +++ b/src/vpn/Makefile | |||
@@ -7,7 +7,7 @@ all:default | |||
7 | 7 | ||
8 | default: test | 8 | default: test |
9 | 9 | ||
10 | test: test.o tun.o packet.o debug.o | 10 | test: test.o tun.o packet.o debug.o pretty-print.o |
11 | 11 | ||
12 | clean: | 12 | clean: |
13 | rm -f *.o | 13 | rm -f *.o |
diff --git a/src/vpn/packet.c b/src/vpn/packet.c index 9a895224a..8125cbb0f 100644 --- a/src/vpn/packet.c +++ b/src/vpn/packet.c | |||
@@ -1,6 +1,5 @@ | |||
1 | #include <errno.h> | 1 | #include <errno.h> |
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include <stdio.h> | ||
4 | #include <string.h> | 3 | #include <string.h> |
5 | #include <unistd.h> | 4 | #include <unistd.h> |
6 | #include <sys/uio.h> | 5 | #include <sys/uio.h> |
@@ -10,32 +9,10 @@ | |||
10 | #include "debug.h" | 9 | #include "debug.h" |
11 | #include "packet.h" | 10 | #include "packet.h" |
12 | 11 | ||
13 | static long payload(struct ip6_pkt* pkt) { | 12 | long payload(struct ip6_pkt* pkt) { |
14 | return (pkt->paylgth[0] << 8) + pkt->paylgth[1]; | 13 | return (pkt->hdr.paylgth[0] << 8) + pkt->hdr.paylgth[1]; |
15 | } | 14 | } |
16 | 15 | ||
17 | static char* pretty = /*{{{*/ | ||
18 | /* 0 1 2 3 4 5 6 | ||
19 | 0123456789012345678901234567890123456789012345678901234567890123456789 */ | ||
20 | "IPv6-Paket from xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx \n" //60 | ||
21 | " to xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx \n" //120 | ||
22 | " flow 0xXXX ( ) \n" //180 | ||
23 | " length 0xXX ( ) \n" //240 | ||
24 | " nexthdr 0xXX ( \n" //300 | ||
25 | " hoplmt 0xXX ( ) \n" //360 | ||
26 | "first 128 bytes of payload: \n" //420 | ||
27 | /* 0 1 2 3 4 5 6 | ||
28 | 0123456789012345678901234567890123456789012345678901234567890123456789 */ | ||
29 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //490 | ||
30 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //560 | ||
31 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //630 | ||
32 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //700 | ||
33 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //770 | ||
34 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //840 | ||
35 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //910 | ||
36 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n";//980 | ||
37 | /*}}}*/ | ||
38 | |||
39 | void send_pkt(int fd, struct ip6_pkt* pkt) {{{ | 16 | void send_pkt(int fd, struct ip6_pkt* pkt) {{{ |
40 | int sz = payload(pkt); | 17 | int sz = payload(pkt); |
41 | int w = 0; | 18 | int w = 0; |
@@ -148,114 +125,3 @@ struct ip6_pkt* parse_ip6(struct pkt_tun* pkt) {{{ | |||
148 | 125 | ||
149 | return pkt6; | 126 | return pkt6; |
150 | }}} | 127 | }}} |
151 | |||
152 | static void pp_ip6adr(unsigned char* adr, char* dest) {{{ | ||
153 | char tmp[3]; | ||
154 | |||
155 | sprintf(tmp, "%02X", adr[0]); | ||
156 | memcpy(dest+0, tmp, 2); | ||
157 | sprintf(tmp, "%02X", adr[1]); | ||
158 | memcpy(dest+2, tmp, 2); | ||
159 | |||
160 | sprintf(tmp, "%02X", adr[2]); | ||
161 | memcpy(dest+5, tmp, 2); | ||
162 | sprintf(tmp, "%02X", adr[3]); | ||
163 | memcpy(dest+7, tmp, 2); | ||
164 | |||
165 | sprintf(tmp, "%02X", adr[4]); | ||
166 | memcpy(dest+10, tmp, 2); | ||
167 | sprintf(tmp, "%02X", adr[5]); | ||
168 | memcpy(dest+12, tmp, 2); | ||
169 | |||
170 | sprintf(tmp, "%02X", adr[6]); | ||
171 | memcpy(dest+15, tmp, 2); | ||
172 | sprintf(tmp, "%02X", adr[7]); | ||
173 | memcpy(dest+17, tmp, 2); | ||
174 | |||
175 | sprintf(tmp, "%02X", adr[8]); | ||
176 | memcpy(dest+20, tmp, 2); | ||
177 | sprintf(tmp, "%02X", adr[9]); | ||
178 | memcpy(dest+22, tmp, 2); | ||
179 | |||
180 | sprintf(tmp, "%02X", adr[10]); | ||
181 | memcpy(dest+25, tmp, 2); | ||
182 | sprintf(tmp, "%02X", adr[11]); | ||
183 | memcpy(dest+27, tmp, 2); | ||
184 | |||
185 | sprintf(tmp, "%02X", adr[12]); | ||
186 | memcpy(dest+30, tmp, 2); | ||
187 | sprintf(tmp, "%02X", adr[13]); | ||
188 | memcpy(dest+32, tmp, 2); | ||
189 | |||
190 | sprintf(tmp, "%02X", adr[14]); | ||
191 | memcpy(dest+35, tmp, 2); | ||
192 | sprintf(tmp, "%02X", adr[15]); | ||
193 | memcpy(dest+37, tmp, 2); | ||
194 | }}} | ||
195 | |||
196 | void pp_hexdump(unsigned char* data, char* dest, int max) {{{ | ||
197 | char tmp[3]; | ||
198 | char tmp2[2]; | ||
199 | int off = 0; | ||
200 | int to = max > 16 ? 16 : max; | ||
201 | for (int i = 0; i < to; i++) { | ||
202 | if (i == 8) off = 1; | ||
203 | sprintf(tmp, "%02x", data[i]); | ||
204 | memcpy(dest+(3*i)+off, tmp, 2); | ||
205 | if (isprint(data[i])) { | ||
206 | sprintf(tmp2, "%c", data[i]); | ||
207 | memcpy(dest+51+i, tmp2, 1); | ||
208 | } | ||
209 | } | ||
210 | }}} | ||
211 | |||
212 | void pp_write_header(char* dest, struct ip6_pkt* pkt) { | ||
213 | switch (pkt->nxthdr) { | ||
214 | case 0x3a: | ||
215 | memcpy(dest, "ICMPv6)", 7); | ||
216 | break; | ||
217 | default: | ||
218 | memcpy(dest, "unknown)", 8); | ||
219 | break; | ||
220 | } | ||
221 | } | ||
222 | |||
223 | void pkt_printf(struct ip6_pkt* pkt) { | ||
224 | char* buf = (char*)malloc(strlen(pretty)+1); | ||
225 | char tmp[9]; | ||
226 | |||
227 | memcpy(buf, pretty, strlen(pretty)+1); | ||
228 | |||
229 | pp_ip6adr(pkt->sadr, buf+16); | ||
230 | pp_ip6adr(pkt->dadr, buf+76); | ||
231 | |||
232 | int flow = (pkt->flowlbl[0] << 16) + (pkt->flowlbl[1] << 8) + (pkt->flowlbl[2]); | ||
233 | sprintf(tmp, "%03x", flow); | ||
234 | memcpy(buf+138, tmp, 3); | ||
235 | sprintf(tmp, "%-8d", flow); | ||
236 | memcpy(buf+143, tmp, 8); | ||
237 | |||
238 | int length = (pkt->paylgth[0] << 8) + (pkt->paylgth[1]); | ||
239 | sprintf(tmp, "%02x", length); | ||
240 | memcpy(buf+198, tmp, 2); | ||
241 | sprintf(tmp, "%-3d", length); | ||
242 | memcpy(buf+203, tmp, 3); | ||
243 | |||
244 | sprintf(tmp, "%02x", pkt->nxthdr); | ||
245 | memcpy(buf+258, tmp, 2); | ||
246 | pp_write_header(buf+263, pkt); | ||
247 | |||
248 | sprintf(tmp, "%02x", pkt->hoplmt); | ||
249 | memcpy(buf+318, tmp, 2); | ||
250 | sprintf(tmp, "%-3d", pkt->hoplmt); | ||
251 | memcpy(buf+323, tmp, 3); | ||
252 | |||
253 | int size = payload(pkt); | ||
254 | for(int i = 0; i < 8; i++) { | ||
255 | if (16*i > size) break; | ||
256 | pp_hexdump(pkt->data + (16*i), buf + 420 + (i*70), size - 16*i); | ||
257 | } | ||
258 | |||
259 | printf(buf); | ||
260 | free(buf); | ||
261 | } | ||
diff --git a/src/vpn/packet.h b/src/vpn/packet.h index 9d5153f13..b86f4a65c 100644 --- a/src/vpn/packet.h +++ b/src/vpn/packet.h | |||
@@ -26,4 +26,6 @@ extern int recv_pkt(int fd, struct pkt_tun** pkt); | |||
26 | extern struct ip6_pkt* parse_ip6(struct pkt_tun* pkt); | 26 | extern struct ip6_pkt* parse_ip6(struct pkt_tun* pkt); |
27 | extern void pkt_printf(struct ip6_pkt* pkt); | 27 | extern void pkt_printf(struct ip6_pkt* pkt); |
28 | 28 | ||
29 | extern long payload(struct ip6_pkt* pkt); | ||
30 | |||
29 | #endif | 31 | #endif |
diff --git a/src/vpn/pretty-print.c b/src/vpn/pretty-print.c new file mode 100644 index 000000000..945ac92a7 --- /dev/null +++ b/src/vpn/pretty-print.c | |||
@@ -0,0 +1,141 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <stdio.h> | ||
3 | #include <string.h> | ||
4 | #include <ctype.h> | ||
5 | |||
6 | #include "packet.h" | ||
7 | |||
8 | static char* pretty = /*{{{*/ | ||
9 | /* 0 1 2 3 4 5 6 | ||
10 | 0123456789012345678901234567890123456789012345678901234567890123456789 */ | ||
11 | "IPv6-Paket from xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx \n" //60 | ||
12 | " to xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx \n" //120 | ||
13 | /* 0 1 2 3 4 5 6 | ||
14 | 0123456789012345678901234567890123456789012345678901234567890123456789 */ | ||
15 | " flow 0xXXX ( ) \n" //180 | ||
16 | " length 0xXX ( ) \n" //240 | ||
17 | " nexthdr 0xXX ( \n" //300 | ||
18 | " hoplmt 0xXX ( ) \n" //360 | ||
19 | "first 128 bytes of payload: \n" //420 | ||
20 | /* 0 1 2 3 4 5 6 | ||
21 | 0123456789012345678901234567890123456789012345678901234567890123456789 */ | ||
22 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //490 | ||
23 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //560 | ||
24 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //630 | ||
25 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //700 | ||
26 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //770 | ||
27 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //840 | ||
28 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n" //910 | ||
29 | "XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | ................ \n";//980 | ||
30 | /*}}}*/ | ||
31 | |||
32 | static void pp_ip6adr(unsigned char* adr, char* dest) {{{ | ||
33 | char tmp[3]; | ||
34 | |||
35 | sprintf(tmp, "%02X", adr[0]); | ||
36 | memcpy(dest+0, tmp, 2); | ||
37 | sprintf(tmp, "%02X", adr[1]); | ||
38 | memcpy(dest+2, tmp, 2); | ||
39 | |||
40 | sprintf(tmp, "%02X", adr[2]); | ||
41 | memcpy(dest+5, tmp, 2); | ||
42 | sprintf(tmp, "%02X", adr[3]); | ||
43 | memcpy(dest+7, tmp, 2); | ||
44 | |||
45 | sprintf(tmp, "%02X", adr[4]); | ||
46 | memcpy(dest+10, tmp, 2); | ||
47 | sprintf(tmp, "%02X", adr[5]); | ||
48 | memcpy(dest+12, tmp, 2); | ||
49 | |||
50 | sprintf(tmp, "%02X", adr[6]); | ||
51 | memcpy(dest+15, tmp, 2); | ||
52 | sprintf(tmp, "%02X", adr[7]); | ||
53 | memcpy(dest+17, tmp, 2); | ||
54 | |||
55 | sprintf(tmp, "%02X", adr[8]); | ||
56 | memcpy(dest+20, tmp, 2); | ||
57 | sprintf(tmp, "%02X", adr[9]); | ||
58 | memcpy(dest+22, tmp, 2); | ||
59 | |||
60 | sprintf(tmp, "%02X", adr[10]); | ||
61 | memcpy(dest+25, tmp, 2); | ||
62 | sprintf(tmp, "%02X", adr[11]); | ||
63 | memcpy(dest+27, tmp, 2); | ||
64 | |||
65 | sprintf(tmp, "%02X", adr[12]); | ||
66 | memcpy(dest+30, tmp, 2); | ||
67 | sprintf(tmp, "%02X", adr[13]); | ||
68 | memcpy(dest+32, tmp, 2); | ||
69 | |||
70 | sprintf(tmp, "%02X", adr[14]); | ||
71 | memcpy(dest+35, tmp, 2); | ||
72 | sprintf(tmp, "%02X", adr[15]); | ||
73 | memcpy(dest+37, tmp, 2); | ||
74 | }}} | ||
75 | |||
76 | void pp_hexdump(unsigned char* data, char* dest, int max) {{{ | ||
77 | char tmp[3]; | ||
78 | char tmp2[2]; | ||
79 | int off = 0; | ||
80 | int to = max > 16 ? 16 : max; | ||
81 | for (int i = 0; i < to; i++) { | ||
82 | if (i == 8) off = 1; | ||
83 | sprintf(tmp, "%02x", data[i]); | ||
84 | memcpy(dest+(3*i)+off, tmp, 2); | ||
85 | if (isprint(data[i])) { | ||
86 | sprintf(tmp2, "%c", data[i]); | ||
87 | memcpy(dest+51+i, tmp2, 1); | ||
88 | } | ||
89 | } | ||
90 | }}} | ||
91 | |||
92 | void pp_write_header(char* dest, struct ip6_pkt* pkt) {{{ | ||
93 | switch (pkt->hdr.nxthdr) { | ||
94 | case 0x3a: | ||
95 | memcpy(dest, "ICMPv6)", 7); | ||
96 | break; | ||
97 | default: | ||
98 | memcpy(dest, "unknown)", 8); | ||
99 | break; | ||
100 | } | ||
101 | }}} | ||
102 | |||
103 | void pkt_printf(struct ip6_pkt* pkt) {{{ | ||
104 | char* buf = (char*)malloc(strlen(pretty)+1); | ||
105 | char tmp[9]; | ||
106 | |||
107 | memcpy(buf, pretty, strlen(pretty)+1); | ||
108 | |||
109 | pp_ip6adr(pkt->hdr.sadr, buf+16); | ||
110 | pp_ip6adr(pkt->hdr.dadr, buf+76); | ||
111 | |||
112 | int flow = (pkt->hdr.flowlbl[0] << 16) + (pkt->hdr.flowlbl[1] << 8) + (pkt->hdr.flowlbl[2]); | ||
113 | sprintf(tmp, "%03x", flow); | ||
114 | memcpy(buf+138, tmp, 3); | ||
115 | sprintf(tmp, "%-8d", flow); | ||
116 | memcpy(buf+143, tmp, 8); | ||
117 | |||
118 | int length = (pkt->hdr.paylgth[0] << 8) + (pkt->hdr.paylgth[1]); | ||
119 | sprintf(tmp, "%02x", length); | ||
120 | memcpy(buf+198, tmp, 2); | ||
121 | sprintf(tmp, "%-3d", length); | ||
122 | memcpy(buf+203, tmp, 3); | ||
123 | |||
124 | sprintf(tmp, "%02x", pkt->hdr.nxthdr); | ||
125 | memcpy(buf+258, tmp, 2); | ||
126 | pp_write_header(buf+263, pkt); | ||
127 | |||
128 | sprintf(tmp, "%02x", pkt->hdr.hoplmt); | ||
129 | memcpy(buf+318, tmp, 2); | ||
130 | sprintf(tmp, "%-3d", pkt->hdr.hoplmt); | ||
131 | memcpy(buf+323, tmp, 3); | ||
132 | |||
133 | int size = payload(pkt); | ||
134 | for(int i = 0; i < 8; i++) { | ||
135 | if (16*i > size) break; | ||
136 | pp_hexdump(pkt->data + (16*i), buf + 420 + (i*70), size - 16*i); | ||
137 | } | ||
138 | |||
139 | printf(buf); | ||
140 | free(buf); | ||
141 | }}} | ||
diff --git a/src/vpn/pretty-print.h b/src/vpn/pretty-print.h new file mode 100644 index 000000000..191e2e2b5 --- /dev/null +++ b/src/vpn/pretty-print.h | |||
@@ -0,0 +1,12 @@ | |||
1 | #ifndef _GNTUN_PP_H_ | ||
2 | #define _GNTUN_PP_H_ | ||
3 | |||
4 | extern static void pp_ip6adr(unsigned char* adr, char* dest); | ||
5 | |||
6 | extern void pp_hexdump(unsigned char* data, char* dest, int max); | ||
7 | |||
8 | extern void pp_write_header(char* dest, struct ip6_pkt* pkt); | ||
9 | |||
10 | extern void pkt_printf(struct ip6_pkt* pkt); | ||
11 | |||
12 | #endif | ||