2 #include "gnunet-dns-parser.h"
3 #include "gnunet-vpn-packet.h"
5 unsigned int parse_dns_name(char* d, const unsigned char* src, unsigned short idx) {/*{{{*/
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 */
15 memcpy(dest, src+idx, len);
28 unsigned short parse_dns_record(unsigned char* data, struct dns_record** dst, unsigned short count, 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 char* name = dst[i]->name;
36 _idx = parse_dns_name(name, data, idx);
37 dst[i]->namelen = _idx - idx;
40 dst[i]->type = *((unsigned short*)(data+idx));
42 dst[i]->class = *((unsigned short*)(data+idx));
44 dst[i]->ttl = *((unsigned int*)(data+idx));
46 dst[i]->data_len = *((unsigned short*)(data+idx));
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);
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);
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);
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*));
69 unsigned short idx = 0, _idx; /* This keeps track how far we have parsed the data */
72 for (i = 0; i < qdcount; i++) { /*{{{*/
73 ppkt->queries[i] = GNUNET_malloc(sizeof(struct dns_query));
74 char* name = alloca(255); /* see RFC1035, it can't be more than this. */
76 _idx = parse_dns_name(name, pkt->data, idx);
77 ppkt->queries[i]->namelen = _idx - idx;
80 ppkt->queries[i]->name = GNUNET_malloc(ppkt->queries[i]->namelen + 1);
81 memcpy(ppkt->queries[i]->name, name, ppkt->queries[i]->namelen + 1);
83 ppkt->queries[i]->qtype = *((unsigned short*)(pkt->data+idx));
85 ppkt->queries[i]->qclass = *((unsigned short*)(pkt->data+idx));
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);