Make the packet-pretty-printing more usable (concise)
[oweals/gnunet.git] / src / vpn / gnunet-dns-parser.c
1 #include "platform.h"
2 #include "gnunet-dns-parser.h"
3 #include "gnunet-vpn-packet.h"
4
5 unsigned int parse_dns_name(unsigned char* d, const unsigned char* src, unsigned short idx) {/*{{{*/
6         unsigned char* dest = d;
7
8         int len = src[idx++];
9         while (len != 0) {
10                 if (len & 0xC0) { /* Compressed name, offset in this and the next octet */
11                         unsigned short offset = ((len & 0x3F) << 8) | src[idx++];
12                         parse_dns_name(dest, src, offset - 12); /* 12 for the Header of the DNS-Packet, idx starts at 0 which is 12 bytes from the start of the packet */
13                         return idx;
14                 }
15                 memcpy(dest, src+idx, len);
16                 idx += len;
17                 dest += len;
18                 *dest = '.';
19                 dest++;
20                 len = src[idx++];
21         };
22         *dest = 0;
23
24         return idx;
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 }/*}}}*/