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"
30 #include "dnsparser.h"
35 * Parse name inside of a DNS query or record.
37 * @param udp_payload entire UDP payload
38 * @param udp_payload_length length of udp_payload
39 * @param off pointer to the offset of the name to parse in the udp_payload (to be
40 * incremented by the size of the name)
41 * @param depth current depth of our recursion (to prevent stack overflow)
42 * @return name as 0-terminated C string on success, NULL if the payload is malformed
45 parse_name (const char *udp_payload,
46 size_t udp_payload_length,
50 const uint8_t *input = (const uint8_t *) udp_payload;
57 ret = GNUNET_strdup ("");
60 if (*off >= udp_payload_length)
70 if (*off + 1 + len > udp_payload_length)
72 GNUNET_asprintf (&tmp,
76 &udp_payload[*off + 1]);
81 else if ((64 | 128) == (len & (64 | 128)) )
84 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
85 /* pointer to string */
86 if (*off + 1 > udp_payload_length)
88 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
89 xstr = parse_name (udp_payload,
95 GNUNET_asprintf (&tmp,
102 if (strlen (ret) > udp_payload_length)
103 goto error; /* we are looping (building an infinite string) */
105 /* pointers always terminate names */
110 /* neither pointer nor inline string, not supported... */
115 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
124 * Parse a DNS query entry.
126 * @param udp_payload entire UDP payload
127 * @param udp_payload_length length of udp_payload
128 * @param off pointer to the offset of the query to parse in the udp_payload (to be
129 * incremented by the size of the query)
130 * @param q where to write the query information
131 * @return GNUNET_OK on success, GNUNET_SYSERR if the query is malformed
134 parse_query (const char *udp_payload,
135 size_t udp_payload_length,
137 struct GNUNET_DNSPARSER_Query *q)
140 struct query_line ql;
142 name = parse_name (udp_payload,
146 return GNUNET_SYSERR;
148 if (*off + sizeof (struct query_line) > udp_payload_length)
149 return GNUNET_SYSERR;
150 memcpy (&ql, &udp_payload[*off], sizeof (ql));
152 q->type = ntohs (ql.type);
153 q->class = ntohs (ql.class);
159 * Parse a DNS record entry.
161 * @param udp_payload entire UDP payload
162 * @param udp_payload_length length of udp_payload
163 * @param off pointer to the offset of the record to parse in the udp_payload (to be
164 * incremented by the size of the record)
165 * @param r where to write the record information
166 * @return GNUNET_OK on success, GNUNET_SYSERR if the record is malformed
169 parse_record (const char *udp_payload,
170 size_t udp_payload_length,
172 struct GNUNET_DNSPARSER_Record *r)
175 struct record_line rl;
184 name = parse_name (udp_payload,
188 return GNUNET_SYSERR;
190 if (*off + sizeof (struct record_line) > udp_payload_length)
191 return GNUNET_SYSERR;
192 memcpy (&rl, &udp_payload[*off], sizeof (rl));
193 (*off) += sizeof (rl);
194 r->type = ntohs (rl.type);
195 r->class = ntohs (rl.class);
196 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
198 data_len = ntohs (rl.data_len);
199 if (*off + data_len > udp_payload_length)
200 return GNUNET_SYSERR;
203 case GNUNET_DNSPARSER_TYPE_NS:
204 case GNUNET_DNSPARSER_TYPE_CNAME:
205 case GNUNET_DNSPARSER_TYPE_PTR:
207 r->data.hostname = parse_name (udp_payload,
210 if ( (NULL == r->data.hostname) ||
211 (old_off + data_len != *off) )
212 return GNUNET_SYSERR;
214 case GNUNET_DNSPARSER_TYPE_SOA:
216 r->data.soa = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_SoaRecord));
217 r->data.soa->mname = parse_name (udp_payload,
220 r->data.soa->rname = parse_name (udp_payload,
223 if ( (NULL == r->data.soa->mname) ||
224 (NULL == r->data.soa->rname) ||
225 (*off + sizeof (struct soa_data) > udp_payload_length) )
226 return GNUNET_SYSERR;
227 memcpy (&soa, &udp_payload[*off], sizeof (struct soa_data));
228 r->data.soa->serial = ntohl (soa.serial);
229 r->data.soa->refresh = ntohl (soa.refresh);
230 r->data.soa->retry = ntohl (soa.retry);
231 r->data.soa->expire = ntohl (soa.expire);
232 r->data.soa->minimum_ttl = ntohl (soa.minimum);
233 (*off) += sizeof (struct soa_data);
234 if (old_off + data_len != *off)
235 return GNUNET_SYSERR;
237 case GNUNET_DNSPARSER_TYPE_MX:
239 if (*off + sizeof (uint16_t) > udp_payload_length)
240 return GNUNET_SYSERR;
241 memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
242 (*off) += sizeof (uint16_t);
243 r->data.mx = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_MxRecord));
244 r->data.mx->preference = ntohs (mxpref);
245 r->data.mx->mxhost = parse_name (udp_payload,
248 if (old_off + data_len != *off)
249 return GNUNET_SYSERR;
251 case GNUNET_DNSPARSER_TYPE_SRV:
253 return GNUNET_SYSERR; /* all valid srv names must start with "_" */
254 if (NULL == strstr (r->name, "._"))
255 return GNUNET_SYSERR; /* necessary string from "._$PROTO" not present */
257 if (*off + sizeof (struct srv_data) > udp_payload_length)
258 return GNUNET_SYSERR;
259 memcpy (&srv, &udp_payload[*off], sizeof (struct srv_data));
260 (*off) += sizeof (struct srv_data);
261 r->data.srv = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_SrvRecord));
262 r->data.srv->priority = ntohs (srv.prio);
263 r->data.srv->weight = ntohs (srv.weight);
264 r->data.srv->port = ntohs (srv.port);
265 /* parse 'data.hostname' into components, which are
266 "_$SERVICE._$PROTO.$DOMAIN_NAME" */
267 ndup = GNUNET_strdup (r->name);
268 tok = strtok (ndup, ".");
269 GNUNET_assert ('_' == *tok);
270 r->data.srv->service = GNUNET_strdup (&tok[1]);
271 tok = strtok (NULL, ".");
274 GNUNET_free (r->data.srv);
276 return GNUNET_SYSERR;
278 r->data.srv->proto = GNUNET_strdup (&tok[1]);
279 tok = strtok (NULL, ".");
282 GNUNET_free (r->data.srv);
284 return GNUNET_SYSERR;
286 r->data.srv->domain_name = GNUNET_strdup (tok);
288 r->data.srv->target = parse_name (udp_payload,
291 if (old_off + data_len != *off)
292 return GNUNET_SYSERR;
295 r->data.raw.data = GNUNET_malloc (data_len);
296 r->data.raw.data_len = data_len;
297 memcpy (r->data.raw.data, &udp_payload[*off], data_len);
306 * Parse a UDP payload of a DNS packet in to a nice struct for further
307 * processing and manipulation.
309 * @param udp_payload wire-format of the DNS packet
310 * @param udp_payload_length number of bytes in udp_payload
311 * @return NULL on error, otherwise the parsed packet
313 struct GNUNET_DNSPARSER_Packet *
314 GNUNET_DNSPARSER_parse (const char *udp_payload,
315 size_t udp_payload_length)
317 struct GNUNET_DNSPARSER_Packet *p;
318 const struct GNUNET_TUN_DnsHeader *dns;
323 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
325 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
326 off = sizeof (struct GNUNET_TUN_DnsHeader);
327 p = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_Packet));
328 p->flags = dns->flags;
330 n = ntohs (dns->query_count);
333 p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
337 parse_query (udp_payload,
343 n = ntohs (dns->answer_rcount);
346 p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
350 parse_record (udp_payload,
356 n = ntohs (dns->authority_rcount);
359 p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
360 p->num_authority_records = n;
363 parse_record (udp_payload,
366 &p->authority_records[i]))
369 n = ntohs (dns->additional_rcount);
372 p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
373 p->num_additional_records = n;
376 parse_record (udp_payload,
379 &p->additional_records[i]))
384 GNUNET_DNSPARSER_free_packet (p);
390 * Free SOA information record.
392 * @param soa record to free
395 free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
399 GNUNET_free_non_null (soa->mname);
400 GNUNET_free_non_null (soa->rname);
406 * Free SRV information record.
408 * @param srv record to free
411 free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
415 GNUNET_free_non_null (srv->target);
416 GNUNET_free_non_null (srv->domain_name);
417 GNUNET_free_non_null (srv->proto);
418 GNUNET_free_non_null (srv->service);
424 * Free MX information record.
426 * @param mx record to free
429 free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
433 GNUNET_free_non_null (mx->mxhost);
439 free_record (struct GNUNET_DNSPARSER_Record *r)
441 GNUNET_free_non_null (r->name);
444 case GNUNET_DNSPARSER_TYPE_MX:
445 free_mx (r->data.mx);
447 case GNUNET_DNSPARSER_TYPE_SOA:
448 free_soa (r->data.soa);
450 case GNUNET_DNSPARSER_TYPE_SRV:
451 free_srv (r->data.srv);
453 case GNUNET_DNSPARSER_TYPE_NS:
454 case GNUNET_DNSPARSER_TYPE_CNAME:
455 case GNUNET_DNSPARSER_TYPE_PTR:
456 GNUNET_free_non_null (r->data.hostname);
459 GNUNET_free_non_null (r->data.raw.data);
466 * Free memory taken by a packet.
468 * @param p packet to free
471 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
475 for (i=0;i<p->num_queries;i++)
476 GNUNET_free_non_null (p->queries[i].name);
477 GNUNET_free_non_null (p->queries);
478 for (i=0;i<p->num_answers;i++)
479 free_record (&p->answers[i]);
480 GNUNET_free_non_null (p->answers);
481 for (i=0;i<p->num_authority_records;i++)
482 free_record (&p->authority_records[i]);
483 GNUNET_free_non_null (p->authority_records);
484 for (i=0;i<p->num_additional_records;i++)
485 free_record (&p->additional_records[i]);
486 GNUNET_free_non_null (p->additional_records);
491 /* ********************** DNS packet assembly code **************** */
495 * Add a DNS name to the UDP packet at the given location.
497 * @param dst where to write the name
498 * @param dst_len number of bytes in dst
499 * @param off pointer to offset where to write the name (increment by bytes used)
500 * must not be changed if there is an error
501 * @param name name to write
502 * @return GNUNET_SYSERR if 'name' is invalid
503 * GNUNET_NO if 'name' did not fit
504 * GNUNET_OK if 'name' was added to 'dst'
518 return GNUNET_SYSERR;
520 if (start + strlen (name) + 2 > dst_len)
525 dot = strchr (name, '.');
530 if ( (len >= 64) || (len == 0) )
531 return GNUNET_NO; /* segment too long or empty */
532 dst[pos++] = (char) (uint8_t) len;
533 memcpy (&dst[pos], name, len);
535 name += len + 1; /* also skip dot */
538 dst[pos++] = '\0'; /* terminator */
545 * Add a DNS query to the UDP packet at the given location.
547 * @param dst where to write the query
548 * @param dst_len number of bytes in dst
549 * @param off pointer to offset where to write the query (increment by bytes used)
550 * must not be changed if there is an error
551 * @param query query to write
552 * @return GNUNET_SYSERR if 'query' is invalid
553 * GNUNET_NO if 'query' did not fit
554 * GNUNET_OK if 'query' was added to 'dst'
557 add_query (char *dst,
560 const struct GNUNET_DNSPARSER_Query *query)
563 struct query_line ql;
565 ret = add_name (dst, dst_len - sizeof (struct query_line), off, query->name);
566 if (ret != GNUNET_OK)
568 ql.type = htons (query->type);
569 ql.class = htons (query->class);
570 memcpy (&dst[*off], &ql, sizeof (ql));
571 (*off) += sizeof (ql);
577 * Add an MX record to the UDP packet at the given location.
579 * @param dst where to write the mx record
580 * @param dst_len number of bytes in dst
581 * @param off pointer to offset where to write the mx information (increment by bytes used);
582 * can also change if there was an error
583 * @param mx mx information to write
584 * @return GNUNET_SYSERR if 'mx' is invalid
585 * GNUNET_NO if 'mx' did not fit
586 * GNUNET_OK if 'mx' was added to 'dst'
592 const struct GNUNET_DNSPARSER_MxRecord *mx)
596 if (*off + sizeof (uint16_t) > dst_len)
598 mxpref = htons (mx->preference);
599 memcpy (&dst[*off], &mxpref, sizeof (mxpref));
600 (*off) += sizeof (mxpref);
601 return add_name (dst, dst_len, off, mx->mxhost);
606 * Add an SOA record to the UDP packet at the given location.
608 * @param dst where to write the SOA record
609 * @param dst_len number of bytes in dst
610 * @param off pointer to offset where to write the SOA information (increment by bytes used)
611 * can also change if there was an error
612 * @param soa SOA information to write
613 * @return GNUNET_SYSERR if 'soa' is invalid
614 * GNUNET_NO if 'soa' did not fit
615 * GNUNET_OK if 'soa' was added to 'dst'
621 const struct GNUNET_DNSPARSER_SoaRecord *soa)
626 if ( (GNUNET_OK != (ret = add_name (dst,
630 (GNUNET_OK != (ret = add_name (dst,
635 if (*off + sizeof (struct soa_data) > dst_len)
637 sd.serial = htonl (soa->serial);
638 sd.refresh = htonl (soa->refresh);
639 sd.retry = htonl (soa->retry);
640 sd.expire = htonl (soa->expire);
641 sd.minimum = htonl (soa->minimum_ttl);
642 memcpy (&dst[*off], &sd, sizeof (sd));
643 (*off) += sizeof (sd);
649 * Add an SRV record to the UDP packet at the given location.
651 * @param dst where to write the SRV record
652 * @param dst_len number of bytes in dst
653 * @param off pointer to offset where to write the SRV information (increment by bytes used)
654 * can also change if there was an error
655 * @param srv SRV information to write
656 * @return GNUNET_SYSERR if 'srv' is invalid
657 * GNUNET_NO if 'srv' did not fit
658 * GNUNET_OK if 'srv' was added to 'dst'
664 const struct GNUNET_DNSPARSER_SrvRecord *srv)
669 if (*off + sizeof (struct srv_data) > dst_len)
671 sd.prio = htons (srv->priority);
672 sd.weight = htons (srv->weight);
673 sd.port = htons (srv->port);
674 memcpy (&dst[*off], &sd, sizeof (sd));
675 (*off) += sizeof (sd);
676 if (GNUNET_OK != (ret = add_name (dst,
686 * Add a DNS record to the UDP packet at the given location.
688 * @param dst where to write the query
689 * @param dst_len number of bytes in dst
690 * @param off pointer to offset where to write the query (increment by bytes used)
691 * must not be changed if there is an error
692 * @param record record to write
693 * @return GNUNET_SYSERR if 'record' is invalid
694 * GNUNET_NO if 'record' did not fit
695 * GNUNET_OK if 'record' was added to 'dst'
698 add_record (char *dst,
701 const struct GNUNET_DNSPARSER_Record *record)
706 struct record_line rl;
710 /* for SRV records, we can create the name from the details
711 of the record if needed */
713 if ( (GNUNET_DNSPARSER_TYPE_SRV == record->type) &&
715 GNUNET_asprintf (&name,
717 record->data.srv->service,
718 record->data.srv->proto,
719 record->data.srv->domain_name);
720 ret = add_name (dst, dst_len - sizeof (struct record_line), off, name);
721 if (name != record->name)
723 if (GNUNET_OK != ret)
725 /* '*off' is now the position where we will need to write the record line */
727 pos = *off + sizeof (struct record_line);
728 switch (record->type)
730 case GNUNET_DNSPARSER_TYPE_MX:
731 ret = add_mx (dst, dst_len, &pos, record->data.mx);
733 case GNUNET_DNSPARSER_TYPE_SOA:
734 ret = add_soa (dst, dst_len, &pos, record->data.soa);
736 case GNUNET_DNSPARSER_TYPE_NS:
737 case GNUNET_DNSPARSER_TYPE_CNAME:
738 case GNUNET_DNSPARSER_TYPE_PTR:
739 ret = add_name (dst, dst_len, &pos, record->data.hostname);
741 case GNUNET_DNSPARSER_TYPE_SRV:
742 ret = add_srv (dst, dst_len, &pos, record->data.srv);
745 if (pos + record->data.raw.data_len > dst_len)
750 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
751 pos += record->data.raw.data_len;
755 if (GNUNET_OK != ret)
761 if (pos - (*off + sizeof (struct record_line)) > UINT16_MAX)
763 /* record data too long */
767 rl.type = htons (record->type);
768 rl.class = htons (record->class);
769 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value / 1000); /* in seconds */
770 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct record_line))));
771 memcpy (&dst[*off], &rl, sizeof (struct record_line));
778 * Given a DNS packet, generate the corresponding UDP payload.
779 * Note that we do not attempt to pack the strings with pointers
780 * as this would complicate the code and this is about being
781 * simple and secure, not fast, fancy and broken like bind.
783 * @param p packet to pack
784 * @param max maximum allowed size for the resulting UDP payload
785 * @param buf set to a buffer with the packed message
786 * @param buf_length set to the length of buf
787 * @return GNUNET_SYSERR if 'p' is invalid
788 * GNUNET_NO if 'p' was truncated (but there is still a result in 'buf')
789 * GNUNET_OK if 'p' was packed completely into '*buf'
792 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
797 struct GNUNET_TUN_DnsHeader dns;
804 if ( (p->num_queries > UINT16_MAX) ||
805 (p->num_answers > UINT16_MAX) ||
806 (p->num_authority_records > UINT16_MAX) ||
807 (p->num_additional_records > UINT16_MAX) )
808 return GNUNET_SYSERR;
810 dns.flags = p->flags;
811 dns.query_count = htons (p->num_queries);
812 dns.answer_rcount = htons (p->num_answers);
813 dns.authority_rcount = htons (p->num_authority_records);
814 dns.additional_rcount = htons (p->num_additional_records);
816 off = sizeof (struct GNUNET_TUN_DnsHeader);
818 for (i=0;i<p->num_queries;i++)
820 ret = add_query (tmp, sizeof (tmp), &off, &p->queries[i]);
821 if (GNUNET_SYSERR == ret)
822 return GNUNET_SYSERR;
823 if (GNUNET_NO == ret)
825 dns.query_count = htons ((uint16_t) (i-1));
830 for (i=0;i<p->num_answers;i++)
832 ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
833 if (GNUNET_SYSERR == ret)
834 return GNUNET_SYSERR;
835 if (GNUNET_NO == ret)
837 dns.answer_rcount = htons ((uint16_t) (i-1));
842 for (i=0;i<p->num_authority_records;i++)
844 ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
845 if (GNUNET_SYSERR == ret)
846 return GNUNET_SYSERR;
847 if (GNUNET_NO == ret)
849 dns.authority_rcount = htons ((uint16_t) (i-1));
854 for (i=0;i<p->num_additional_records;i++)
856 ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
857 if (GNUNET_SYSERR == ret)
858 return GNUNET_SYSERR;
859 if (GNUNET_NO == ret)
861 dns.additional_rcount = htons (i-1);
867 if (GNUNET_YES == trc)
868 dns.flags.message_truncated = 1;
869 memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
871 *buf = GNUNET_malloc (off);
873 memcpy (*buf, tmp, off);
874 if (GNUNET_YES == trc)
879 /* end of dnsparser.c */