diff options
author | Philipp Tölke <toelke@in.tum.de> | 2010-10-05 18:20:20 +0000 |
---|---|---|
committer | Philipp Tölke <toelke@in.tum.de> | 2010-10-05 18:20:20 +0000 |
commit | 852b4f4ff636f3005b03be5e2195a00dd0d1b7e9 (patch) | |
tree | f92b6c510fd0356c22d557fea56de709d97c3e0f /src/vpn/gnunet-dns-parser.c | |
parent | bd4b7b43485ed8cf1817f41cd8793885a3a5b5c5 (diff) | |
download | gnunet-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/vpn/gnunet-dns-parser.c')
-rw-r--r-- | src/vpn/gnunet-dns-parser.c | 68 |
1 files changed, 68 insertions, 0 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 | ||
4 | unsigned int parse_dns_name(unsigned char* d, const unsigned char* src, unsigned short idx) {/*{{{*/ | 5 | unsigned 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 | |||
28 | unsigned 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 | |||
55 | struct 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 | } | ||