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
32 #include "gnunet_util_lib.h"
33 #include "gnunet_dnsparser_lib.h"
34 #include "dnsparser.h"
38 * Check if a label in UTF-8 format can be coded into valid IDNA.
39 * This can fail if the ASCII-conversion becomes longer than 63 characters.
41 * @param label label to check (UTF-8 string)
42 * @return GNUNET_OK if the label can be converted to IDNA,
43 * GNUNET_SYSERR if the label is not valid for DNS names
46 GNUNET_DNSPARSER_check_label (const char *label)
51 idna_to_ascii_8z (label, &output, IDNA_USE_STD3_ASCII_RULES))
63 * Parse name inside of a DNS query or record.
65 * @param udp_payload entire UDP payload
66 * @param udp_payload_length length of udp_payload
67 * @param off pointer to the offset of the name to parse in the udp_payload (to be
68 * incremented by the size of the name)
69 * @param depth current depth of our recursion (to prevent stack overflow)
70 * @return name as 0-terminated C string on success, NULL if the payload is malformed
73 parse_name (const char *udp_payload,
74 size_t udp_payload_length,
78 const uint8_t *input = (const uint8_t *) udp_payload;
85 ret = GNUNET_strdup ("");
88 if (*off >= udp_payload_length)
98 if (*off + 1 + len > udp_payload_length)
100 GNUNET_asprintf (&tmp,
104 &udp_payload[*off + 1]);
109 else if ((64 | 128) == (len & (64 | 128)) )
112 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
113 /* pointer to string */
114 if (*off + 1 > udp_payload_length)
116 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
117 xstr = parse_name (udp_payload,
123 GNUNET_asprintf (&tmp,
130 if (strlen (ret) > udp_payload_length)
131 goto error; /* we are looping (building an infinite string) */
133 /* pointers always terminate names */
138 /* neither pointer nor inline string, not supported... */
143 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
152 * Parse a DNS query entry.
154 * @param udp_payload entire UDP payload
155 * @param udp_payload_length length of udp_payload
156 * @param off pointer to the offset of the query to parse in the udp_payload (to be
157 * incremented by the size of the query)
158 * @param q where to write the query information
159 * @return GNUNET_OK on success, GNUNET_SYSERR if the query is malformed
162 parse_query (const char *udp_payload,
163 size_t udp_payload_length,
165 struct GNUNET_DNSPARSER_Query *q)
168 struct query_line ql;
170 name = parse_name (udp_payload,
174 return GNUNET_SYSERR;
176 if (*off + sizeof (struct query_line) > udp_payload_length)
177 return GNUNET_SYSERR;
178 memcpy (&ql, &udp_payload[*off], sizeof (ql));
180 q->type = ntohs (ql.type);
181 q->class = ntohs (ql.class);
187 * Parse a DNS record entry.
189 * @param udp_payload entire UDP payload
190 * @param udp_payload_length length of udp_payload
191 * @param off pointer to the offset of the record to parse in the udp_payload (to be
192 * incremented by the size of the record)
193 * @param r where to write the record information
194 * @return GNUNET_OK on success, GNUNET_SYSERR if the record is malformed
197 parse_record (const char *udp_payload,
198 size_t udp_payload_length,
200 struct GNUNET_DNSPARSER_Record *r)
203 struct record_line rl;
212 name = parse_name (udp_payload,
216 return GNUNET_SYSERR;
218 if (*off + sizeof (struct record_line) > udp_payload_length)
219 return GNUNET_SYSERR;
220 memcpy (&rl, &udp_payload[*off], sizeof (rl));
221 (*off) += sizeof (rl);
222 r->type = ntohs (rl.type);
223 r->class = ntohs (rl.class);
224 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
226 data_len = ntohs (rl.data_len);
227 if (*off + data_len > udp_payload_length)
228 return GNUNET_SYSERR;
231 case GNUNET_DNSPARSER_TYPE_NS:
232 case GNUNET_DNSPARSER_TYPE_CNAME:
233 case GNUNET_DNSPARSER_TYPE_PTR:
235 r->data.hostname = parse_name (udp_payload,
238 if ( (NULL == r->data.hostname) ||
239 (old_off + data_len != *off) )
240 return GNUNET_SYSERR;
242 case GNUNET_DNSPARSER_TYPE_SOA:
244 r->data.soa = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_SoaRecord));
245 r->data.soa->mname = parse_name (udp_payload,
248 r->data.soa->rname = parse_name (udp_payload,
251 if ( (NULL == r->data.soa->mname) ||
252 (NULL == r->data.soa->rname) ||
253 (*off + sizeof (struct soa_data) > udp_payload_length) )
254 return GNUNET_SYSERR;
255 memcpy (&soa, &udp_payload[*off], sizeof (struct soa_data));
256 r->data.soa->serial = ntohl (soa.serial);
257 r->data.soa->refresh = ntohl (soa.refresh);
258 r->data.soa->retry = ntohl (soa.retry);
259 r->data.soa->expire = ntohl (soa.expire);
260 r->data.soa->minimum_ttl = ntohl (soa.minimum);
261 (*off) += sizeof (struct soa_data);
262 if (old_off + data_len != *off)
263 return GNUNET_SYSERR;
265 case GNUNET_DNSPARSER_TYPE_MX:
267 if (*off + sizeof (uint16_t) > udp_payload_length)
268 return GNUNET_SYSERR;
269 memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
270 (*off) += sizeof (uint16_t);
271 r->data.mx = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_MxRecord));
272 r->data.mx->preference = ntohs (mxpref);
273 r->data.mx->mxhost = parse_name (udp_payload,
276 if (old_off + data_len != *off)
277 return GNUNET_SYSERR;
279 case GNUNET_DNSPARSER_TYPE_SRV:
281 return GNUNET_SYSERR; /* all valid srv names must start with "_" */
282 if (NULL == strstr (r->name, "._"))
283 return GNUNET_SYSERR; /* necessary string from "._$PROTO" not present */
285 if (*off + sizeof (struct srv_data) > udp_payload_length)
286 return GNUNET_SYSERR;
287 memcpy (&srv, &udp_payload[*off], sizeof (struct srv_data));
288 (*off) += sizeof (struct srv_data);
289 r->data.srv = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_SrvRecord));
290 r->data.srv->priority = ntohs (srv.prio);
291 r->data.srv->weight = ntohs (srv.weight);
292 r->data.srv->port = ntohs (srv.port);
293 /* parse 'data.hostname' into components, which are
294 "_$SERVICE._$PROTO.$DOMAIN_NAME" */
295 ndup = GNUNET_strdup (r->name);
296 tok = strtok (ndup, ".");
297 GNUNET_assert (NULL != tok);
298 GNUNET_assert ('_' == *tok);
299 r->data.srv->service = GNUNET_strdup (&tok[1]);
300 tok = strtok (NULL, ".");
301 if ( (NULL == tok) || ('_' != *tok) )
303 GNUNET_free (r->data.srv);
305 return GNUNET_SYSERR;
307 r->data.srv->proto = GNUNET_strdup (&tok[1]);
308 tok = strtok (NULL, ".");
311 GNUNET_free (r->data.srv);
313 return GNUNET_SYSERR;
315 r->data.srv->domain_name = GNUNET_strdup (tok);
317 r->data.srv->target = parse_name (udp_payload,
320 if (old_off + data_len != *off)
321 return GNUNET_SYSERR;
324 r->data.raw.data = GNUNET_malloc (data_len);
325 r->data.raw.data_len = data_len;
326 memcpy (r->data.raw.data, &udp_payload[*off], data_len);
335 * Parse a UDP payload of a DNS packet in to a nice struct for further
336 * processing and manipulation.
338 * @param udp_payload wire-format of the DNS packet
339 * @param udp_payload_length number of bytes in udp_payload
340 * @return NULL on error, otherwise the parsed packet
342 struct GNUNET_DNSPARSER_Packet *
343 GNUNET_DNSPARSER_parse (const char *udp_payload,
344 size_t udp_payload_length)
346 struct GNUNET_DNSPARSER_Packet *p;
347 const struct GNUNET_TUN_DnsHeader *dns;
352 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
354 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
355 off = sizeof (struct GNUNET_TUN_DnsHeader);
356 p = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_Packet));
357 p->flags = dns->flags;
359 n = ntohs (dns->query_count);
362 p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
366 parse_query (udp_payload,
372 n = ntohs (dns->answer_rcount);
375 p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
379 parse_record (udp_payload,
385 n = ntohs (dns->authority_rcount);
388 p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
389 p->num_authority_records = n;
392 parse_record (udp_payload,
395 &p->authority_records[i]))
398 n = ntohs (dns->additional_rcount);
401 p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
402 p->num_additional_records = n;
405 parse_record (udp_payload,
408 &p->additional_records[i]))
413 GNUNET_DNSPARSER_free_packet (p);
419 * Free SOA information record.
421 * @param soa record to free
424 free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
428 GNUNET_free_non_null (soa->mname);
429 GNUNET_free_non_null (soa->rname);
435 * Free SRV information record.
437 * @param srv record to free
440 free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
444 GNUNET_free_non_null (srv->target);
445 GNUNET_free_non_null (srv->domain_name);
446 GNUNET_free_non_null (srv->proto);
447 GNUNET_free_non_null (srv->service);
453 * Free MX information record.
455 * @param mx record to free
458 free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
462 GNUNET_free_non_null (mx->mxhost);
468 free_record (struct GNUNET_DNSPARSER_Record *r)
470 GNUNET_free_non_null (r->name);
473 case GNUNET_DNSPARSER_TYPE_MX:
474 free_mx (r->data.mx);
476 case GNUNET_DNSPARSER_TYPE_SOA:
477 free_soa (r->data.soa);
479 case GNUNET_DNSPARSER_TYPE_SRV:
480 free_srv (r->data.srv);
482 case GNUNET_DNSPARSER_TYPE_NS:
483 case GNUNET_DNSPARSER_TYPE_CNAME:
484 case GNUNET_DNSPARSER_TYPE_PTR:
485 GNUNET_free_non_null (r->data.hostname);
488 GNUNET_free_non_null (r->data.raw.data);
495 * Free memory taken by a packet.
497 * @param p packet to free
500 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
504 for (i=0;i<p->num_queries;i++)
505 GNUNET_free_non_null (p->queries[i].name);
506 GNUNET_free_non_null (p->queries);
507 for (i=0;i<p->num_answers;i++)
508 free_record (&p->answers[i]);
509 GNUNET_free_non_null (p->answers);
510 for (i=0;i<p->num_authority_records;i++)
511 free_record (&p->authority_records[i]);
512 GNUNET_free_non_null (p->authority_records);
513 for (i=0;i<p->num_additional_records;i++)
514 free_record (&p->additional_records[i]);
515 GNUNET_free_non_null (p->additional_records);
520 /* ********************** DNS packet assembly code **************** */
524 * Add a DNS name to the UDP packet at the given location.
526 * @param dst where to write the name
527 * @param dst_len number of bytes in dst
528 * @param off pointer to offset where to write the name (increment by bytes used)
529 * must not be changed if there is an error
530 * @param name name to write
531 * @return GNUNET_SYSERR if 'name' is invalid
532 * GNUNET_NO if 'name' did not fit
533 * GNUNET_OK if 'name' was added to 'dst'
547 return GNUNET_SYSERR;
549 if (start + strlen (name) + 2 > dst_len)
554 dot = strchr (name, '.');
559 if ( (len >= 64) || (len == 0) )
560 return GNUNET_NO; /* segment too long or empty */
561 dst[pos++] = (char) (uint8_t) len;
562 memcpy (&dst[pos], name, len);
564 name += len + 1; /* also skip dot */
567 dst[pos++] = '\0'; /* terminator */
574 * Add a DNS query to the UDP packet at the given location.
576 * @param dst where to write the query
577 * @param dst_len number of bytes in dst
578 * @param off pointer to offset where to write the query (increment by bytes used)
579 * must not be changed if there is an error
580 * @param query query to write
581 * @return GNUNET_SYSERR if 'query' is invalid
582 * GNUNET_NO if 'query' did not fit
583 * GNUNET_OK if 'query' was added to 'dst'
586 add_query (char *dst,
589 const struct GNUNET_DNSPARSER_Query *query)
592 struct query_line ql;
594 ret = add_name (dst, dst_len - sizeof (struct query_line), off, query->name);
595 if (ret != GNUNET_OK)
597 ql.type = htons (query->type);
598 ql.class = htons (query->class);
599 memcpy (&dst[*off], &ql, sizeof (ql));
600 (*off) += sizeof (ql);
606 * Add an MX record to the UDP packet at the given location.
608 * @param dst where to write the mx record
609 * @param dst_len number of bytes in dst
610 * @param off pointer to offset where to write the mx information (increment by bytes used);
611 * can also change if there was an error
612 * @param mx mx information to write
613 * @return GNUNET_SYSERR if 'mx' is invalid
614 * GNUNET_NO if 'mx' did not fit
615 * GNUNET_OK if 'mx' was added to 'dst'
621 const struct GNUNET_DNSPARSER_MxRecord *mx)
625 if (*off + sizeof (uint16_t) > dst_len)
627 mxpref = htons (mx->preference);
628 memcpy (&dst[*off], &mxpref, sizeof (mxpref));
629 (*off) += sizeof (mxpref);
630 return add_name (dst, dst_len, off, mx->mxhost);
635 * Add an SOA record to the UDP packet at the given location.
637 * @param dst where to write the SOA record
638 * @param dst_len number of bytes in dst
639 * @param off pointer to offset where to write the SOA information (increment by bytes used)
640 * can also change if there was an error
641 * @param soa SOA information to write
642 * @return GNUNET_SYSERR if 'soa' is invalid
643 * GNUNET_NO if 'soa' did not fit
644 * GNUNET_OK if 'soa' was added to 'dst'
650 const struct GNUNET_DNSPARSER_SoaRecord *soa)
655 if ( (GNUNET_OK != (ret = add_name (dst,
659 (GNUNET_OK != (ret = add_name (dst,
664 if (*off + sizeof (struct soa_data) > dst_len)
666 sd.serial = htonl (soa->serial);
667 sd.refresh = htonl (soa->refresh);
668 sd.retry = htonl (soa->retry);
669 sd.expire = htonl (soa->expire);
670 sd.minimum = htonl (soa->minimum_ttl);
671 memcpy (&dst[*off], &sd, sizeof (sd));
672 (*off) += sizeof (sd);
678 * Add an SRV record to the UDP packet at the given location.
680 * @param dst where to write the SRV record
681 * @param dst_len number of bytes in dst
682 * @param off pointer to offset where to write the SRV information (increment by bytes used)
683 * can also change if there was an error
684 * @param srv SRV information to write
685 * @return GNUNET_SYSERR if 'srv' is invalid
686 * GNUNET_NO if 'srv' did not fit
687 * GNUNET_OK if 'srv' was added to 'dst'
693 const struct GNUNET_DNSPARSER_SrvRecord *srv)
698 if (*off + sizeof (struct srv_data) > dst_len)
700 sd.prio = htons (srv->priority);
701 sd.weight = htons (srv->weight);
702 sd.port = htons (srv->port);
703 memcpy (&dst[*off], &sd, sizeof (sd));
704 (*off) += sizeof (sd);
705 if (GNUNET_OK != (ret = add_name (dst,
715 * Add a DNS record to the UDP packet at the given location.
717 * @param dst where to write the query
718 * @param dst_len number of bytes in dst
719 * @param off pointer to offset where to write the query (increment by bytes used)
720 * must not be changed if there is an error
721 * @param record record to write
722 * @return GNUNET_SYSERR if 'record' is invalid
723 * GNUNET_NO if 'record' did not fit
724 * GNUNET_OK if 'record' was added to 'dst'
727 add_record (char *dst,
730 const struct GNUNET_DNSPARSER_Record *record)
735 struct record_line rl;
739 /* for SRV records, we can create the name from the details
740 of the record if needed */
742 if ( (GNUNET_DNSPARSER_TYPE_SRV == record->type) &&
744 GNUNET_asprintf (&name,
746 record->data.srv->service,
747 record->data.srv->proto,
748 record->data.srv->domain_name);
749 ret = add_name (dst, dst_len - sizeof (struct record_line), off, name);
750 if (name != record->name)
752 if (GNUNET_OK != ret)
754 /* '*off' is now the position where we will need to write the record line */
756 pos = *off + sizeof (struct record_line);
757 switch (record->type)
759 case GNUNET_DNSPARSER_TYPE_MX:
760 ret = add_mx (dst, dst_len, &pos, record->data.mx);
762 case GNUNET_DNSPARSER_TYPE_SOA:
763 ret = add_soa (dst, dst_len, &pos, record->data.soa);
765 case GNUNET_DNSPARSER_TYPE_NS:
766 case GNUNET_DNSPARSER_TYPE_CNAME:
767 case GNUNET_DNSPARSER_TYPE_PTR:
768 ret = add_name (dst, dst_len, &pos, record->data.hostname);
770 case GNUNET_DNSPARSER_TYPE_SRV:
771 ret = add_srv (dst, dst_len, &pos, record->data.srv);
774 if (pos + record->data.raw.data_len > dst_len)
779 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
780 pos += record->data.raw.data_len;
784 if (GNUNET_OK != ret)
790 if (pos - (*off + sizeof (struct record_line)) > UINT16_MAX)
792 /* record data too long */
796 rl.type = htons (record->type);
797 rl.class = htons (record->class);
798 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value / 1000); /* in seconds */
799 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct record_line))));
800 memcpy (&dst[*off], &rl, sizeof (struct record_line));
807 * Given a DNS packet, generate the corresponding UDP payload.
808 * Note that we do not attempt to pack the strings with pointers
809 * as this would complicate the code and this is about being
810 * simple and secure, not fast, fancy and broken like bind.
812 * @param p packet to pack
813 * @param max maximum allowed size for the resulting UDP payload
814 * @param buf set to a buffer with the packed message
815 * @param buf_length set to the length of buf
816 * @return GNUNET_SYSERR if 'p' is invalid
817 * GNUNET_NO if 'p' was truncated (but there is still a result in 'buf')
818 * GNUNET_OK if 'p' was packed completely into '*buf'
821 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
826 struct GNUNET_TUN_DnsHeader dns;
833 if ( (p->num_queries > UINT16_MAX) ||
834 (p->num_answers > UINT16_MAX) ||
835 (p->num_authority_records > UINT16_MAX) ||
836 (p->num_additional_records > UINT16_MAX) )
837 return GNUNET_SYSERR;
839 dns.flags = p->flags;
840 dns.query_count = htons (p->num_queries);
841 dns.answer_rcount = htons (p->num_answers);
842 dns.authority_rcount = htons (p->num_authority_records);
843 dns.additional_rcount = htons (p->num_additional_records);
845 off = sizeof (struct GNUNET_TUN_DnsHeader);
847 for (i=0;i<p->num_queries;i++)
849 ret = add_query (tmp, sizeof (tmp), &off, &p->queries[i]);
850 if (GNUNET_SYSERR == ret)
851 return GNUNET_SYSERR;
852 if (GNUNET_NO == ret)
854 dns.query_count = htons ((uint16_t) (i-1));
859 for (i=0;i<p->num_answers;i++)
861 ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
862 if (GNUNET_SYSERR == ret)
863 return GNUNET_SYSERR;
864 if (GNUNET_NO == ret)
866 dns.answer_rcount = htons ((uint16_t) (i-1));
871 for (i=0;i<p->num_authority_records;i++)
873 ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
874 if (GNUNET_SYSERR == ret)
875 return GNUNET_SYSERR;
876 if (GNUNET_NO == ret)
878 dns.authority_rcount = htons ((uint16_t) (i-1));
883 for (i=0;i<p->num_additional_records;i++)
885 ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
886 if (GNUNET_SYSERR == ret)
887 return GNUNET_SYSERR;
888 if (GNUNET_NO == ret)
890 dns.additional_rcount = htons (i-1);
896 if (GNUNET_YES == trc)
897 dns.flags.message_truncated = 1;
898 memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
900 *buf = GNUNET_malloc (off);
902 memcpy (*buf, tmp, off);
903 if (GNUNET_YES == trc)
908 /* end of dnsparser.c */