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! */
35 struct GNUNET_TUN_DnsHeader
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 GNUNET_TUN_DnsHeader *dns;
314 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
316 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
317 off = sizeof (struct GNUNET_TUN_DnsHeader);
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 GNUNET_TUN_DnsHeader 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 GNUNET_TUN_DnsHeader);
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 GNUNET_TUN_DnsHeader));
788 *buf = GNUNET_malloc (off);
790 memcpy (*buf, tmp, off);
791 if (GNUNET_YES == trc)
796 /* end of dnsparser.c */