2 This file is part of GNUnet
3 (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file dns/dnsparser.c
23 * @brief helper library to parse DNS packets.
24 * @author Philipp Toelke
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dnsparser_lib.h"
33 GNUNET_NETWORK_STRUCT_BEGIN
34 /* FIXME: replace this one with the one from tcpip_tun.h! */
37 uint16_t id GNUNET_PACKED;
38 struct GNUNET_DNSPARSER_Flags flags;
39 uint16_t query_count GNUNET_PACKED; // number of questions
40 uint16_t answer_rcount GNUNET_PACKED; // number of answers
41 uint16_t authority_rcount GNUNET_PACKED; // number of authority-records
42 uint16_t additional_rcount GNUNET_PACKED; // number of additional records
47 uint16_t type GNUNET_PACKED;
48 uint16_t class GNUNET_PACKED;
53 uint16_t type GNUNET_PACKED;
54 uint16_t class GNUNET_PACKED;
55 uint32_t ttl GNUNET_PACKED;
56 uint16_t data_len GNUNET_PACKED;
61 uint32_t serial GNUNET_PACKED;
62 uint32_t refresh GNUNET_PACKED;
63 uint32_t retry GNUNET_PACKED;
64 uint32_t expire GNUNET_PACKED;
65 uint32_t minimum GNUNET_PACKED;
68 GNUNET_NETWORK_STRUCT_END
72 * Parse name inside of a DNS query or record.
74 * @param udp_payload entire UDP payload
75 * @param udp_payload_length length of udp_payload
76 * @param off pointer to the offset of the name to parse in the udp_payload (to be
77 * incremented by the size of the name)
78 * @param depth current depth of our recursion (to prevent stack overflow)
79 * @return name as 0-terminated C string on success, NULL if the payload is malformed
82 parse_name (const char *udp_payload,
83 size_t udp_payload_length,
87 const uint8_t *input = (const uint8_t *) udp_payload;
94 ret = GNUNET_strdup ("");
97 if (*off >= udp_payload_length)
107 if (*off + 1 + len > udp_payload_length)
109 GNUNET_asprintf (&tmp,
113 &udp_payload[*off + 1]);
118 else if ((64 | 128) == (len & (64 | 128)) )
121 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
122 /* pointer to string */
123 if (*off + 1 > udp_payload_length)
125 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
126 xstr = parse_name (udp_payload,
132 GNUNET_asprintf (&tmp,
139 if (strlen (ret) > udp_payload_length)
140 goto error; /* we are looping (building an infinite string) */
142 /* pointers always terminate names */
147 /* neither pointer nor inline string, not supported... */
152 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
161 * Parse a DNS query entry.
163 * @param udp_payload entire UDP payload
164 * @param udp_payload_length length of udp_payload
165 * @param off pointer to the offset of the query to parse in the udp_payload (to be
166 * incremented by the size of the query)
167 * @param q where to write the query information
168 * @return GNUNET_OK on success, GNUNET_SYSERR if the query is malformed
171 parse_query (const char *udp_payload,
172 size_t udp_payload_length,
174 struct GNUNET_DNSPARSER_Query *q)
177 struct query_line ql;
179 name = parse_name (udp_payload,
183 return GNUNET_SYSERR;
185 if (*off + sizeof (struct query_line) > udp_payload_length)
186 return GNUNET_SYSERR;
187 memcpy (&ql, &udp_payload[*off], sizeof (ql));
189 q->type = ntohs (ql.type);
190 q->class = ntohs (ql.class);
196 * Parse a DNS record entry.
198 * @param udp_payload entire UDP payload
199 * @param udp_payload_length length of udp_payload
200 * @param off pointer to the offset of the record to parse in the udp_payload (to be
201 * incremented by the size of the record)
202 * @param r where to write the record information
203 * @return GNUNET_OK on success, GNUNET_SYSERR if the record is malformed
206 parse_record (const char *udp_payload,
207 size_t udp_payload_length,
209 struct GNUNET_DNSPARSER_Record *r)
212 struct record_line rl;
218 name = parse_name (udp_payload,
222 return GNUNET_SYSERR;
224 if (*off + sizeof (struct record_line) > udp_payload_length)
225 return GNUNET_SYSERR;
226 memcpy (&rl, &udp_payload[*off], sizeof (rl));
227 (*off) += sizeof (rl);
228 r->type = ntohs (rl.type);
229 r->class = ntohs (rl.class);
230 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
232 data_len = ntohs (rl.data_len);
233 if (*off + data_len > udp_payload_length)
234 return GNUNET_SYSERR;
237 case GNUNET_DNSPARSER_TYPE_NS:
238 case GNUNET_DNSPARSER_TYPE_CNAME:
239 case GNUNET_DNSPARSER_TYPE_PTR:
241 r->data.hostname = parse_name (udp_payload,
244 if ( (NULL == r->data.hostname) ||
245 (old_off + data_len != *off) )
246 return GNUNET_SYSERR;
248 case GNUNET_DNSPARSER_TYPE_SOA:
250 r->data.soa = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_SoaRecord));
251 r->data.soa->mname = parse_name (udp_payload,
254 r->data.soa->rname = parse_name (udp_payload,
257 if ( (NULL == r->data.soa->mname) ||
258 (NULL == r->data.soa->rname) ||
259 (*off + sizeof (soa) > udp_payload_length) )
260 return GNUNET_SYSERR;
261 memcpy (&soa, &udp_payload[*off], sizeof (soa));
262 r->data.soa->serial = ntohl (soa.serial);
263 r->data.soa->refresh = ntohl (soa.refresh);
264 r->data.soa->retry = ntohl (soa.retry);
265 r->data.soa->expire = ntohl (soa.expire);
266 r->data.soa->minimum_ttl = ntohl (soa.minimum);
267 (*off) += sizeof (soa);
268 if (old_off + data_len != *off)
269 return GNUNET_SYSERR;
271 case GNUNET_DNSPARSER_TYPE_MX:
273 if (*off + sizeof (uint16_t) > udp_payload_length)
274 return GNUNET_SYSERR;
275 memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
276 (*off) += sizeof (uint16_t);
277 r->data.mx = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_MxRecord));
278 r->data.mx->preference = ntohs (mxpref);
279 r->data.mx->mxhost = parse_name (udp_payload,
282 if (old_off + data_len != *off)
283 return GNUNET_SYSERR;
286 r->data.raw.data = GNUNET_malloc (data_len);
287 r->data.raw.data_len = data_len;
288 memcpy (r->data.raw.data, &udp_payload[*off], data_len);
297 * Parse a UDP payload of a DNS packet in to a nice struct for further
298 * processing and manipulation.
300 * @param udp_payload wire-format of the DNS packet
301 * @param udp_payload_length number of bytes in udp_payload
302 * @return NULL on error, otherwise the parsed packet
304 struct GNUNET_DNSPARSER_Packet *
305 GNUNET_DNSPARSER_parse (const char *udp_payload,
306 size_t udp_payload_length)
308 struct GNUNET_DNSPARSER_Packet *p;
309 const struct dns_header *dns;
314 if (udp_payload_length < sizeof (struct dns_header))
316 dns = (const struct dns_header *) udp_payload;
317 off = sizeof (struct dns_header);
318 p = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_Packet));
319 p->flags = dns->flags;
321 n = ntohs (dns->query_count);
324 p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
328 parse_query (udp_payload,
334 n = ntohs (dns->answer_rcount);
337 p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
341 parse_record (udp_payload,
347 n = ntohs (dns->authority_rcount);
350 p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
351 p->num_authority_records = n;
354 parse_record (udp_payload,
357 &p->authority_records[i]))
360 n = ntohs (dns->additional_rcount);
363 p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
364 p->num_additional_records = n;
367 parse_record (udp_payload,
370 &p->additional_records[i]))
375 GNUNET_DNSPARSER_free_packet (p);
381 * Free SOA information record.
383 * @param soa record to free
386 free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
390 GNUNET_free_non_null (soa->mname);
391 GNUNET_free_non_null (soa->rname);
397 * Free MX information record.
399 * @param mx record to free
402 free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
406 GNUNET_free_non_null (mx->mxhost);
412 free_record (struct GNUNET_DNSPARSER_Record *r)
414 GNUNET_free_non_null (r->name);
417 case GNUNET_DNSPARSER_TYPE_MX:
418 free_mx (r->data.mx);
420 case GNUNET_DNSPARSER_TYPE_SOA:
421 free_soa (r->data.soa);
423 case GNUNET_DNSPARSER_TYPE_NS:
424 case GNUNET_DNSPARSER_TYPE_CNAME:
425 case GNUNET_DNSPARSER_TYPE_PTR:
426 GNUNET_free_non_null (r->data.hostname);
429 GNUNET_free_non_null (r->data.raw.data);
436 * Free memory taken by a packet.
438 * @param p packet to free
441 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
445 for (i=0;i<p->num_queries;i++)
446 GNUNET_free_non_null (p->queries[i].name);
447 GNUNET_free_non_null (p->queries);
448 for (i=0;i<p->num_answers;i++)
449 free_record (&p->answers[i]);
450 GNUNET_free_non_null (p->answers);
451 for (i=0;i<p->num_authority_records;i++)
452 free_record (&p->authority_records[i]);
453 GNUNET_free_non_null (p->authority_records);
454 for (i=0;i<p->num_additional_records;i++)
455 free_record (&p->additional_records[i]);
456 GNUNET_free_non_null (p->additional_records);
461 /* ********************** DNS packet assembly code **************** */
465 * Add a DNS name to the UDP packet at the given location.
467 * @param dst where to write the name
468 * @param dst_len number of bytes in dst
469 * @param off pointer to offset where to write the name (increment by bytes used)
470 * must not be changed if there is an error
471 * @param name name to write
472 * @return GNUNET_SYSERR if 'name' is invalid
473 * GNUNET_NO if 'name' did not fit
474 * GNUNET_OK if 'name' was added to 'dst'
488 return GNUNET_SYSERR;
490 if (start + strlen (name) + 2 > dst_len)
495 dot = strchr (name, '.');
500 if ( (len >= 64) || (len == 0) )
501 return GNUNET_NO; /* segment too long or empty */
502 dst[pos++] = (char) (uint8_t) len;
503 memcpy (&dst[pos], name, len);
505 name += len + 1; /* also skip dot */
508 dst[pos++] = '\0'; /* terminator */
515 * Add a DNS query to the UDP packet at the given location.
517 * @param dst where to write the query
518 * @param dst_len number of bytes in dst
519 * @param off pointer to offset where to write the query (increment by bytes used)
520 * must not be changed if there is an error
521 * @param query query to write
522 * @return GNUNET_SYSERR if 'query' is invalid
523 * GNUNET_NO if 'query' did not fit
524 * GNUNET_OK if 'query' was added to 'dst'
527 add_query (char *dst,
530 const struct GNUNET_DNSPARSER_Query *query)
533 struct query_line ql;
535 ret = add_name (dst, dst_len - sizeof (struct query_line), off, query->name);
536 if (ret != GNUNET_OK)
538 ql.type = htons (query->type);
539 ql.class = htons (query->class);
540 memcpy (&dst[*off], &ql, sizeof (ql));
541 (*off) += sizeof (ql);
547 * Add an MX record to the UDP packet at the given location.
549 * @param dst where to write the mx record
550 * @param dst_len number of bytes in dst
551 * @param off pointer to offset where to write the mx information (increment by bytes used);
552 * can also change if there was an error
553 * @param mx mx information to write
554 * @return GNUNET_SYSERR if 'mx' is invalid
555 * GNUNET_NO if 'mx' did not fit
556 * GNUNET_OK if 'mx' was added to 'dst'
562 const struct GNUNET_DNSPARSER_MxRecord *mx)
566 if (*off + sizeof (uint16_t) > dst_len)
568 mxpref = htons (mx->preference);
569 memcpy (&dst[*off], &mxpref, sizeof (mxpref));
570 (*off) += sizeof (mxpref);
571 return add_name (dst, dst_len, off, mx->mxhost);
576 * Add an SOA record to the UDP packet at the given location.
578 * @param dst where to write the SOA record
579 * @param dst_len number of bytes in dst
580 * @param off pointer to offset where to write the SOA information (increment by bytes used)
581 * can also change if there was an error
582 * @param soa SOA information to write
583 * @return GNUNET_SYSERR if 'soa' is invalid
584 * GNUNET_NO if 'soa' did not fit
585 * GNUNET_OK if 'soa' was added to 'dst'
591 const struct GNUNET_DNSPARSER_SoaRecord *soa)
596 if ( (GNUNET_OK != (ret = add_name (dst,
600 (GNUNET_OK != (ret = add_name (dst,
605 if (*off + sizeof (soa) > dst_len)
607 sd.serial = htonl (soa->serial);
608 sd.refresh = htonl (soa->refresh);
609 sd.retry = htonl (soa->retry);
610 sd.expire = htonl (soa->expire);
611 sd.minimum = htonl (soa->minimum_ttl);
612 memcpy (&dst[*off], &sd, sizeof (sd));
613 (*off) += sizeof (sd);
619 * Add a DNS record to the UDP packet at the given location.
621 * @param dst where to write the query
622 * @param dst_len number of bytes in dst
623 * @param off pointer to offset where to write the query (increment by bytes used)
624 * must not be changed if there is an error
625 * @param record record to write
626 * @return GNUNET_SYSERR if 'record' is invalid
627 * GNUNET_NO if 'record' did not fit
628 * GNUNET_OK if 'record' was added to 'dst'
631 add_record (char *dst,
634 const struct GNUNET_DNSPARSER_Record *record)
639 struct record_line rl;
642 ret = add_name (dst, dst_len - sizeof (struct record_line), off, record->name);
643 if (ret != GNUNET_OK)
645 /* '*off' is now the position where we will need to write the record line */
647 pos = *off + sizeof (struct record_line);
648 switch (record->type)
650 case GNUNET_DNSPARSER_TYPE_MX:
651 ret = add_mx (dst, dst_len, &pos, record->data.mx);
653 case GNUNET_DNSPARSER_TYPE_SOA:
654 ret = add_soa (dst, dst_len, &pos, record->data.soa);
656 case GNUNET_DNSPARSER_TYPE_NS:
657 case GNUNET_DNSPARSER_TYPE_CNAME:
658 case GNUNET_DNSPARSER_TYPE_PTR:
659 ret = add_name (dst, dst_len, &pos, record->data.hostname);
662 if (pos + record->data.raw.data_len > dst_len)
667 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
668 pos += record->data.raw.data_len;
672 if (ret != GNUNET_OK)
678 if (pos - (*off + sizeof (struct record_line)) > UINT16_MAX)
680 /* record data too long */
684 rl.type = htons (record->type);
685 rl.class = htons (record->class);
686 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value / 1000); /* in seconds */
687 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct record_line))));
688 memcpy (&dst[*off], &rl, sizeof (struct record_line));
695 * Given a DNS packet, generate the corresponding UDP payload.
696 * Note that we do not attempt to pack the strings with pointers
697 * as this would complicate the code and this is about being
698 * simple and secure, not fast, fancy and broken like bind.
700 * @param p packet to pack
701 * @param max maximum allowed size for the resulting UDP payload
702 * @param buf set to a buffer with the packed message
703 * @param buf_length set to the length of buf
704 * @return GNUNET_SYSERR if 'p' is invalid
705 * GNUNET_NO if 'p' was truncated (but there is still a result in 'buf')
706 * GNUNET_OK if 'p' was packed completely into '*buf'
709 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
714 struct dns_header dns;
721 if ( (p->num_queries > UINT16_MAX) ||
722 (p->num_answers > UINT16_MAX) ||
723 (p->num_authority_records > UINT16_MAX) ||
724 (p->num_additional_records > UINT16_MAX) )
725 return GNUNET_SYSERR;
727 dns.flags = p->flags;
728 dns.query_count = htons (p->num_queries);
729 dns.answer_rcount = htons (p->num_answers);
730 dns.authority_rcount = htons (p->num_authority_records);
731 dns.additional_rcount = htons (p->num_additional_records);
733 off = sizeof (struct dns_header);
735 for (i=0;i<p->num_queries;i++)
737 ret = add_query (tmp, sizeof (tmp), &off, &p->queries[i]);
738 if (GNUNET_SYSERR == ret)
739 return GNUNET_SYSERR;
740 if (GNUNET_NO == ret)
742 dns.query_count = htons ((uint16_t) (i-1));
747 for (i=0;i<p->num_answers;i++)
749 ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
750 if (GNUNET_SYSERR == ret)
751 return GNUNET_SYSERR;
752 if (GNUNET_NO == ret)
754 dns.answer_rcount = htons ((uint16_t) (i-1));
759 for (i=0;i<p->num_authority_records;i++)
761 ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
762 if (GNUNET_SYSERR == ret)
763 return GNUNET_SYSERR;
764 if (GNUNET_NO == ret)
766 dns.authority_rcount = htons ((uint16_t) (i-1));
771 for (i=0;i<p->num_additional_records;i++)
773 ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
774 if (GNUNET_SYSERR == ret)
775 return GNUNET_SYSERR;
776 if (GNUNET_NO == ret)
778 dns.additional_rcount = htons (i-1);
784 if (GNUNET_YES == trc)
785 dns.flags.message_truncated = 1;
786 memcpy (tmp, &dns, sizeof (struct dns_header));
788 *buf = GNUNET_malloc (off);
790 memcpy (*buf, tmp, off);
791 if (GNUNET_YES == trc)
825 /* legacy code follows */
828 * Parse a name from DNS to a normal .-delimited, 0-terminated string.
830 * @param d The destination of the name. Should have at least 255 bytes allocated.
831 * @param src The DNS-Packet
832 * @param idx The offset inside the Packet from which on the name should be read
833 * @returns The offset of the first unparsed byte (the byte right behind the name)
836 parse_dns_name (char *d, const unsigned char *src, unsigned short idx)
840 int len = src[idx++];
845 { /* Compressed name, offset in this and the next octet */
846 unsigned short offset = ((len & 0x3F) << 8) | src[idx++];
848 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 */
851 memcpy (dest, src + idx, len);
866 * Parse a complete DNS-Record from raw DNS-data to a struct dns_record
868 * @param data The DNS-data
869 * @param dst Pointer to count pointers; individual pointers will be allocated
870 * @param count Number of records to parse
871 * @param idx The offset inside the Packet from which on the name should be read
872 * @returns The offset of the first unparsed byte (the byte right behind the last record)
874 static unsigned short
875 parse_dns_record (unsigned char *data, /*{{{ */
876 struct dns_record **dst, unsigned short count,
882 for (i = 0; i < count; i++)
884 dst[i] = GNUNET_malloc (sizeof (struct dns_record));
885 dst[i]->name = alloca (255); // see RFC1035, no name can be longer than this.
886 char *name = dst[i]->name;
888 _idx = parse_dns_name (name, data, idx);
889 dst[i]->namelen = _idx - idx;
891 dst[i]->name = GNUNET_malloc (dst[i]->namelen);
892 memcpy (dst[i]->name, name, dst[i]->namelen);
896 dst[i]->type = *((unsigned short *) (data + idx));
898 dst[i]->class = *((unsigned short *) (data + idx));
900 dst[i]->ttl = *((unsigned int *) (data + idx));
902 dst[i]->data_len = *((unsigned short *) (data + idx));
904 dst[i]->data = GNUNET_malloc (ntohs (dst[i]->data_len));
905 memcpy (dst[i]->data, data + idx, ntohs (dst[i]->data_len));
906 idx += ntohs (dst[i]->data_len);
912 * Parse a raw DNS-Packet into an usable struct
914 struct dns_pkt_parsed *
915 parse_dns_packet (struct dns_pkt *pkt)
917 struct dns_pkt_parsed *ppkt = GNUNET_malloc (sizeof (struct dns_pkt_parsed));
919 memcpy (&ppkt->s, &pkt->s, sizeof pkt->s);
921 unsigned short qdcount = ntohs (ppkt->s.qdcount);
922 unsigned short ancount = ntohs (ppkt->s.ancount);
923 unsigned short nscount = ntohs (ppkt->s.nscount);
924 unsigned short arcount = ntohs (ppkt->s.arcount);
926 ppkt->queries = GNUNET_malloc (qdcount * sizeof (struct dns_query *));
927 ppkt->answers = GNUNET_malloc (ancount * sizeof (struct dns_record *));
928 ppkt->nameservers = GNUNET_malloc (nscount * sizeof (struct dns_record *));
929 ppkt->additional = GNUNET_malloc (arcount * sizeof (struct dns_record *));
931 unsigned short idx = 0, _idx; /* This keeps track how far we have parsed the data */
933 /* Parse the Query */
936 for (i = 0; i < qdcount; i++)
938 ppkt->queries[i] = GNUNET_malloc (sizeof (struct dns_query));
939 char *name = alloca (255); /* see RFC1035, it can't be more than this. */
941 _idx = parse_dns_name (name, pkt->data, idx);
942 ppkt->queries[i]->namelen = _idx - idx;
945 ppkt->queries[i]->name = GNUNET_malloc (ppkt->queries[i]->namelen);
946 memcpy (ppkt->queries[i]->name, name, ppkt->queries[i]->namelen);
948 ppkt->queries[i]->qtype = *((unsigned short *) (pkt->data + idx));
950 ppkt->queries[i]->qclass = *((unsigned short *) (pkt->data + idx));
954 idx = parse_dns_record (pkt->data, ppkt->answers, ancount, idx);
955 idx = parse_dns_record (pkt->data, ppkt->nameservers, nscount, idx);
956 idx = parse_dns_record (pkt->data, ppkt->additional, arcount, idx);
961 unparse_dns_name (char *dest, char *src, size_t len)
969 while (*src != '.' && *src != 0)
986 unparse_dns_packet (struct dns_pkt_parsed *ppkt)
988 size_t size = sizeof (struct dns_pkt) - 1;
991 for (i = 0; i < ntohs (ppkt->s.qdcount); i++)
992 size += ppkt->queries[i]->namelen + 1;
994 for (i = 0; i < ntohs (ppkt->s.ancount); i++)
996 size += ppkt->answers[i]->namelen + 1;
997 size += ppkt->answers[i]->data_len;
999 for (i = 0; i < ntohs (ppkt->s.nscount); i++)
1001 size += ppkt->nameservers[i]->namelen + 1;
1002 size += ppkt->nameservers[i]->data_len;
1004 for (i = 0; i < ntohs (ppkt->s.arcount); i++)
1006 size += ppkt->additional[i]->namelen + 1;
1007 size += ppkt->additional[i]->data_len;
1011 4 * ntohs (ppkt->s.qdcount) + 10 * (ntohs (ppkt->s.ancount) +
1012 ntohs (ppkt->s.arcount) +
1013 ntohs (ppkt->s.nscount));
1015 struct dns_pkt *pkt = GNUNET_malloc (size);
1016 char *pkt_c = (char *) pkt;
1018 memcpy (&pkt->s, &ppkt->s, sizeof ppkt->s);
1019 size_t idx = sizeof ppkt->s;
1021 for (i = 0; i < ntohs (ppkt->s.qdcount); i++)
1023 unparse_dns_name (&pkt_c[idx], ppkt->queries[i]->name,
1024 ppkt->queries[i]->namelen);
1025 idx += ppkt->queries[i]->namelen;
1026 struct dns_query_line *d = (struct dns_query_line *) &pkt_c[idx];
1028 d->class = ppkt->queries[i]->qclass;
1029 d->type = ppkt->queries[i]->qtype;
1030 idx += sizeof (struct dns_query_line);
1033 for (i = 0; i < ntohs (ppkt->s.ancount); i++)
1035 unparse_dns_name (&pkt_c[idx], ppkt->answers[i]->name,
1036 ppkt->answers[i]->namelen);
1037 idx += ppkt->answers[i]->namelen;
1038 struct dns_record_line *r = (struct dns_record_line *) &pkt_c[idx];
1040 r->type = ppkt->answers[i]->type;
1041 r->class = ppkt->answers[i]->class;
1042 r->ttl = ppkt->answers[i]->ttl;
1043 r->data_len = ppkt->answers[i]->data_len;
1044 idx += sizeof (struct dns_record_line);
1045 memcpy (&r->data, ppkt->answers[i]->data, ppkt->answers[i]->data_len);
1046 idx += ppkt->answers[i]->data_len;
1049 for (i = 0; i < ntohs (ppkt->s.nscount); i++)
1051 unparse_dns_name (&pkt_c[idx], ppkt->nameservers[i]->name,
1052 ppkt->nameservers[i]->namelen);
1053 idx += ppkt->nameservers[i]->namelen;
1054 struct dns_record_line *r = (struct dns_record_line *) &pkt_c[idx];
1056 r->type = ppkt->nameservers[i]->type;
1057 r->class = ppkt->nameservers[i]->class;
1058 r->ttl = ppkt->nameservers[i]->ttl;
1059 r->data_len = ppkt->nameservers[i]->data_len;
1060 idx += sizeof (struct dns_record_line);
1061 memcpy (&r->data, ppkt->nameservers[i]->data,
1062 ppkt->nameservers[i]->data_len);
1063 idx += ppkt->nameservers[i]->data_len;
1066 for (i = 0; i < ntohs (ppkt->s.arcount); i++)
1068 unparse_dns_name (&pkt_c[idx], ppkt->additional[i]->name,
1069 ppkt->additional[i]->namelen);
1070 idx += ppkt->additional[i]->namelen;
1071 struct dns_record_line *r = (struct dns_record_line *) &pkt_c[idx];
1073 r->type = ppkt->additional[i]->type;
1074 r->class = ppkt->additional[i]->class;
1075 r->ttl = ppkt->additional[i]->ttl;
1076 r->data_len = ppkt->additional[i]->data_len;
1077 idx += sizeof (struct dns_record_line);
1078 memcpy (&r->data, ppkt->additional[i]->data, ppkt->additional[i]->data_len);
1079 idx += ppkt->additional[i]->data_len;
1086 free_parsed_dns_packet (struct dns_pkt_parsed *ppkt)
1088 unsigned short qdcount = ntohs (ppkt->s.qdcount);
1089 unsigned short ancount = ntohs (ppkt->s.ancount);
1090 unsigned short nscount = ntohs (ppkt->s.nscount);
1091 unsigned short arcount = ntohs (ppkt->s.arcount);
1095 for (i = 0; i < qdcount; i++)
1097 GNUNET_free (ppkt->queries[i]->name);
1098 GNUNET_free (ppkt->queries[i]);
1100 GNUNET_free (ppkt->queries);
1101 for (i = 0; i < ancount; i++)
1103 GNUNET_free (ppkt->answers[i]->name);
1104 GNUNET_free (ppkt->answers[i]->data);
1105 GNUNET_free (ppkt->answers[i]);
1107 GNUNET_free (ppkt->answers);
1108 for (i = 0; i < nscount; i++)
1110 GNUNET_free (ppkt->nameservers[i]->name);
1111 GNUNET_free (ppkt->nameservers[i]->data);
1112 GNUNET_free (ppkt->nameservers[i]);
1114 GNUNET_free (ppkt->nameservers);
1115 for (i = 0; i < arcount; i++)
1117 GNUNET_free (ppkt->additional[i]->name);
1118 GNUNET_free (ppkt->additional[i]->data);
1119 GNUNET_free (ppkt->additional[i]);
1121 GNUNET_free (ppkt->additional);