aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPhilipp Tölke <toelke@in.tum.de>2010-10-05 18:20:20 +0000
committerPhilipp Tölke <toelke@in.tum.de>2010-10-05 18:20:20 +0000
commit852b4f4ff636f3005b03be5e2195a00dd0d1b7e9 (patch)
treef92b6c510fd0356c22d557fea56de709d97c3e0f /src
parentbd4b7b43485ed8cf1817f41cd8793885a3a5b5c5 (diff)
downloadgnunet-852b4f4ff636f3005b03be5e2195a00dd0d1b7e9.tar.gz
gnunet-852b4f4ff636f3005b03be5e2195a00dd0d1b7e9.zip
parse a complete dns-packet-struct to another struct so as to completly have
it in memory
Diffstat (limited to 'src')
-rw-r--r--src/vpn/gnunet-dns-parser.c68
-rw-r--r--src/vpn/gnunet-dns-parser.h9
-rw-r--r--src/vpn/gnunet-vpn-packet.h10
-rw-r--r--src/vpn/gnunet-vpn-pretty-print.c87
4 files changed, 86 insertions, 88 deletions
diff --git a/src/vpn/gnunet-dns-parser.c b/src/vpn/gnunet-dns-parser.c
index 3425e78f6..be4770e24 100644
--- a/src/vpn/gnunet-dns-parser.c
+++ b/src/vpn/gnunet-dns-parser.c
@@ -1,5 +1,6 @@
1#include "platform.h" 1#include "platform.h"
2#include "gnunet-dns-parser.h" 2#include "gnunet-dns-parser.h"
3#include "gnunet-vpn-packet.h"
3 4
4unsigned int parse_dns_name(unsigned char* d, const unsigned char* src, unsigned short idx) {/*{{{*/ 5unsigned int parse_dns_name(unsigned char* d, const unsigned char* src, unsigned short idx) {/*{{{*/
5 unsigned char* dest = d; 6 unsigned char* dest = d;
@@ -23,3 +24,70 @@ unsigned int parse_dns_name(unsigned char* d, const unsigned char* src, unsigned
23 return idx; 24 return idx;
24} 25}
25/*}}}*/ 26/*}}}*/
27
28unsigned short parse_dns_record(unsigned char* data, struct dns_record** dst, unsigned short count, unsigned short idx) {/*{{{*/
29 int i;
30 unsigned short _idx;
31 for (i = 0; i < count; i++) {
32 dst[i] = GNUNET_malloc(sizeof(struct dns_record));
33 dst[i]->name = alloca(255); // see RFC1035
34 unsigned char* name = dst[i]->name;
35
36 _idx = parse_dns_name(name, data, idx);
37 dst[i]->namelen = _idx - idx;
38 idx = _idx;
39
40 dst[i]->type = *((unsigned short*)(data+idx));
41 idx += 2;
42 dst[i]->class = *((unsigned short*)(data+idx));
43 idx += 2;
44 dst[i]->ttl = *((unsigned int*)(data+idx));
45 idx += 4;
46 dst[i]->data_len = *((unsigned short*)(data+idx));
47 idx += 2;
48 dst[i]->data = GNUNET_malloc(ntohs(dst[i]->data_len));
49 memcpy(dst[i]->data, data+idx, ntohs(dst[i]->data_len));
50 idx += ntohs(dst[i]->data_len);
51 }
52 return idx;
53}/*}}}*/
54
55struct dns_pkt_parsed* parse_dns_packet(struct dns_pkt* pkt) {
56 struct dns_pkt_parsed* ppkt = GNUNET_malloc(sizeof(struct dns_pkt_parsed));
57 memcpy(&ppkt->s, &pkt->s, sizeof pkt->s);
58
59 unsigned short qdcount = ntohs(ppkt->s.qdcount);
60 unsigned short ancount = ntohs(ppkt->s.ancount);
61 unsigned short nscount = ntohs(ppkt->s.nscount);
62 unsigned short arcount = ntohs(ppkt->s.arcount);
63
64 ppkt->queries = GNUNET_malloc(qdcount*sizeof(struct dns_query*));
65 ppkt->answers = GNUNET_malloc(ancount*sizeof(struct dns_record*));
66 ppkt->nameservers = GNUNET_malloc(nscount*sizeof(struct dns_record*));
67 ppkt->additional = GNUNET_malloc(arcount*sizeof(struct dns_record*));
68
69 unsigned short idx = 0, _idx; /* This keeps track how far we have parsed the data */
70
71 int i;
72 for (i = 0; i < qdcount; i++) { /*{{{*/
73 ppkt->queries[i] = GNUNET_malloc(sizeof(struct dns_query));
74 unsigned char* name = alloca(255); /* see RFC1035, it can't be more than this. */
75
76 _idx = parse_dns_name(name, pkt->data, idx);
77 ppkt->queries[i]->namelen = _idx - idx;
78 idx = _idx;
79
80 ppkt->queries[i]->name = GNUNET_malloc(ppkt->queries[i]->namelen + 1);
81 memcpy(ppkt->queries[i]->name, name, ppkt->queries[i]->namelen + 1);
82
83 ppkt->queries[i]->qtype = *((unsigned short*)(pkt->data+idx));
84 idx += 2;
85 ppkt->queries[i]->qclass = *((unsigned short*)(pkt->data+idx));
86 idx += 2;
87 }
88 /*}}}*/
89 idx = parse_dns_record(pkt->data, ppkt->answers, ancount, idx);
90 idx = parse_dns_record(pkt->data, ppkt->nameservers, nscount, idx);
91 idx = parse_dns_record(pkt->data, ppkt->additional, arcount, idx);
92 return ppkt;
93}
diff --git a/src/vpn/gnunet-dns-parser.h b/src/vpn/gnunet-dns-parser.h
index 77dcd6d4e..1ee82e7a3 100644
--- a/src/vpn/gnunet-dns-parser.h
+++ b/src/vpn/gnunet-dns-parser.h
@@ -1,10 +1,9 @@
1#ifndef _GNVPN_DNSP_H_ 1#ifndef _GNVPN_DNSP_H_
2#define _GNVPN_DNSP_H_ 2#define _GNVPN_DNSP_H_
3 3
4/** 4#include "platform.h"
5 * Parses the dns-name pointed to by src+idx returning idx so, that src+idx points 5#include "gnunet-vpn-packet.h"
6 * to the first unused char. 6
7 */ 7struct dns_pkt_parsed* parse_dns_packet(struct dns_pkt* pkt);
8unsigned int parse_dns_name(unsigned char* dest, const unsigned char* src, unsigned short idx);
9 8
10#endif 9#endif
diff --git a/src/vpn/gnunet-vpn-packet.h b/src/vpn/gnunet-vpn-packet.h
index 0659215cb..61f274e95 100644
--- a/src/vpn/gnunet-vpn-packet.h
+++ b/src/vpn/gnunet-vpn-packet.h
@@ -92,20 +92,22 @@ struct dns_pkt {
92 92
93struct dns_pkt_parsed { 93struct dns_pkt_parsed {
94 struct dns_static s; 94 struct dns_static s;
95 struct dns_query* queries; 95 struct dns_query** queries;
96 struct dns_record* answers; 96 struct dns_record** answers;
97 struct dns_record* nameservers; 97 struct dns_record** nameservers;
98 struct dns_record* additional; 98 struct dns_record** additional;
99}; 99};
100 100
101struct dns_query { 101struct dns_query {
102 unsigned char* name; 102 unsigned char* name;
103 unsigned char namelen;
103 unsigned short qtype; 104 unsigned short qtype;
104 unsigned short qclass; 105 unsigned short qclass;
105}; 106};
106 107
107struct dns_record { 108struct dns_record {
108 unsigned char* name; 109 unsigned char* name;
110 unsigned char namelen;
109 unsigned short type; 111 unsigned short type;
110 unsigned short class; 112 unsigned short class;
111 unsigned int ttl; 113 unsigned int ttl;
diff --git a/src/vpn/gnunet-vpn-pretty-print.c b/src/vpn/gnunet-vpn-pretty-print.c
index e8fe4c722..0d165a650 100644
--- a/src/vpn/gnunet-vpn-pretty-print.c
+++ b/src/vpn/gnunet-vpn-pretty-print.c
@@ -260,7 +260,9 @@ static char* dns_classes(short class) { /* {{{ */
260} 260}
261/*}}}*/ 261/*}}}*/
262 262
263void pkt_printf_dns(struct dns_pkt* pkt) {{{ 263void pkt_printf_dns(struct dns_pkt* upkt) {{{
264 struct dns_pkt_parsed* pkt = parse_dns_packet(upkt);
265
264 printf("DNS-Packet:\n"); 266 printf("DNS-Packet:\n");
265 printf("\tid: %d\n", ntohs(pkt->s.id)); 267 printf("\tid: %d\n", ntohs(pkt->s.id));
266 printf("\t%d: %s\n", pkt->s.qr, pkt->s.qr == 0 ? "query" : "response"); 268 printf("\t%d: %s\n", pkt->s.qr, pkt->s.qr == 0 ? "query" : "response");
@@ -275,97 +277,24 @@ void pkt_printf_dns(struct dns_pkt* pkt) {{{
275 printf("\t#ns: %d\n", nscount); 277 printf("\t#ns: %d\n", nscount);
276 printf("\t#ar: %d\n", arcount); 278 printf("\t#ar: %d\n", arcount);
277 279
278 struct dns_query** queries = alloca(qdcount*sizeof(struct dns_query*));
279 struct dns_record** answers = alloca(ancount*sizeof(struct dns_record*));
280 struct dns_record** nameserver = alloca(nscount*sizeof(struct dns_record*));
281 struct dns_record** additional = alloca(arcount*sizeof(struct dns_record*));
282 unsigned short idx = 0;
283
284 int i; 280 int i;
285 for (i = 0; i < qdcount; i++) { /*{{{*/ 281 for (i = 0; i < qdcount; i++) { /*{{{*/
286 queries[i] = alloca(sizeof(struct dns_query)); 282 printf("query for %s type=%d (%s) class=%d (%s)\n", pkt->queries[i]->name, ntohs(pkt->queries[i]->qtype), dns_types(ntohs(pkt->queries[i]->qtype)), ntohs(pkt->queries[i]->qclass), dns_classes(ntohs(pkt->queries[i]->qclass)));
287 queries[i]->name = alloca(255); // see RFC1035
288 unsigned char* name = queries[i]->name;
289
290 idx = parse_dns_name(name, pkt->data, idx);
291
292 printf("%d\n", idx);
293 queries[i]->qtype = *((unsigned short*)(pkt->data+idx));
294 idx += 2;
295 queries[i]->qclass = *((unsigned short*)(pkt->data+idx));
296 idx += 2;
297 printf("query for %s type=%d (%s) class=%d (%s)\n", queries[i]->name, ntohs(queries[i]->qtype), dns_types(ntohs(queries[i]->qtype)), ntohs(queries[i]->qclass), dns_classes(ntohs(queries[i]->qclass)));
298 } 283 }
299 /*}}}*/ 284 /*}}}*/
300 for (i = 0; i < ancount; i++) { /*{{{*/ 285 for (i = 0; i < ancount; i++) { /*{{{*/
301 answers[i] = alloca(sizeof(struct dns_record)); 286 printf("answer for %s type=%d (%s) class=%d (%s) ttl=%d data_len=%d\n", pkt->answers[i]->name, ntohs(pkt->answers[i]->type), dns_types(ntohs(pkt->answers[i]->type)), ntohs(pkt->answers[i]->class), dns_classes(ntohs(pkt->answers[i]->class)), ntohl(pkt->answers[i]->ttl), ntohs(pkt->answers[i]->data_len));
302 answers[i]->name = alloca(255); // see RFC1035
303 unsigned char* name = answers[i]->name;
304
305 idx = parse_dns_name(name, pkt->data, idx);
306
307 printf("%d\n", idx);
308 answers[i]->type = *((unsigned short*)(pkt->data+idx));
309 idx += 2;
310 answers[i]->class = *((unsigned short*)(pkt->data+idx));
311 idx += 2;
312 answers[i]->ttl = *((unsigned int*)(pkt->data+idx));
313 idx += 4;
314 answers[i]->data_len = *((unsigned short*)(pkt->data+idx));
315 idx += 2;
316 answers[i]->data = alloca(ntohs(answers[i]->data_len));
317 memcpy(answers[i]->data, pkt->data+idx, ntohs(answers[i]->data_len));
318 idx += ntohs(answers[i]->data_len);
319
320 printf("answer for %s type=%d (%s) class=%d (%s) ttl=%d data_len=%d\n", answers[i]->name, ntohs(answers[i]->type), dns_types(ntohs(answers[i]->type)), ntohs(answers[i]->class), dns_classes(ntohs(answers[i]->class)), ntohl(answers[i]->ttl), ntohs(answers[i]->data_len));
321 } 287 }
322 /*}}}*/ 288 /*}}}*/
323 for (i = 0; i < nscount; i++) { /*{{{*/ 289 for (i = 0; i < nscount; i++) { /*{{{*/
324 nameserver[i] = alloca(sizeof(struct dns_record)); 290 printf("nameservers for %s type=%d (%s) class=%d (%s) ttl=%d data_len=%d\n", pkt->nameservers[i]->name, ntohs(pkt->nameservers[i]->type), dns_types(ntohs(pkt->nameservers[i]->type)), ntohs(pkt->nameservers[i]->class), dns_classes(ntohs(pkt->nameservers[i]->class)), ntohl(pkt->nameservers[i]->ttl), ntohs(pkt->nameservers[i]->data_len));
325 nameserver[i]->name = alloca(255); // see RFC1035
326 unsigned char* name = nameserver[i]->name;
327
328 idx = parse_dns_name(name, pkt->data, idx);
329
330 printf("%d\n", idx);
331 nameserver[i]->type = *((unsigned short*)(pkt->data+idx));
332 idx += 2;
333 nameserver[i]->class = *((unsigned short*)(pkt->data+idx));
334 idx += 2;
335 nameserver[i]->ttl = *((unsigned int*)(pkt->data+idx));
336 idx += 4;
337 nameserver[i]->data_len = *((unsigned short*)(pkt->data+idx));
338 idx += 2;
339 nameserver[i]->data = alloca(ntohs(nameserver[i]->data_len));
340 memcpy(nameserver[i]->data, pkt->data+idx, ntohs(nameserver[i]->data_len));
341 idx += ntohs(nameserver[i]->data_len);
342
343 printf("nameserver for %s type=%d (%s) class=%d (%s) ttl=%d data_len=%d\n", nameserver[i]->name, ntohs(nameserver[i]->type), dns_types(ntohs(nameserver[i]->type)), ntohs(nameserver[i]->class), dns_classes(ntohs(nameserver[i]->class)), ntohl(nameserver[i]->ttl), ntohs(nameserver[i]->data_len));
344 } 291 }
345 /*}}}*/ 292 /*}}}*/
346 for (i = 0; i < arcount; i++) { /*{{{*/ 293 for (i = 0; i < arcount; i++) { /*{{{*/
347 additional[i] = alloca(sizeof(struct dns_query)); 294 printf("additional record for %s type=%d (%s) class=%d (%s) ttl=%d data_len=%d\n", pkt->additional[i]->name, ntohs(pkt->additional[i]->type), dns_types(ntohs(pkt->additional[i]->type)), ntohs(pkt->additional[i]->class), dns_classes(ntohs(pkt->additional[i]->class)), ntohl(pkt->additional[i]->ttl), ntohs(pkt->additional[i]->data_len));
348 additional[i]->name = alloca(255); // see RFC1035
349 unsigned char* name = additional[i]->name;
350
351 idx = parse_dns_name(name, pkt->data, idx);
352
353 printf("%d\n", idx);
354 additional[i]->type = *((unsigned short*)(pkt->data+idx));
355 idx += 2;
356 additional[i]->class = *((unsigned short*)(pkt->data+idx));
357 idx += 2;
358 additional[i]->ttl = *((unsigned int*)(pkt->data+idx));
359 idx += 4;
360 additional[i]->data_len = *((unsigned short*)(pkt->data+idx));
361 idx += 2;
362 additional[i]->data = alloca(ntohs(additional[i]->data_len));
363 memcpy(additional[i]->data, pkt->data+idx, ntohs(additional[i]->data_len));
364 idx += ntohs(additional[i]->data_len);
365
366 printf("additional record for %s type=%d (%s) class=%d (%s) ttl=%d data_len=%d\n", additional[i]->name, ntohs(additional[i]->type), dns_types(ntohs(additional[i]->type)), ntohs(additional[i]->class), dns_classes(ntohs(additional[i]->class)), ntohl(additional[i]->ttl), ntohs(additional[i]->data_len));
367 } 295 }
368 /*}}}*/ 296 /*}}}*/
297 GNUNET_free(pkt);
369}}} 298}}}
370 299
371void pkt_printf_udp_dns(struct udp_dns* pkt) {{{ 300void pkt_printf_udp_dns(struct udp_dns* pkt) {{{