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 (NULL != tok);
270 GNUNET_assert ('_' == *tok);
271 r->data.srv->service = GNUNET_strdup (&tok[1]);
272 tok = strtok (NULL, ".");
273 if ( (NULL == tok) || ('_' != *tok) )
275 GNUNET_free (r->data.srv);
277 return GNUNET_SYSERR;
279 r->data.srv->proto = GNUNET_strdup (&tok[1]);
280 tok = strtok (NULL, ".");
283 GNUNET_free (r->data.srv);
285 return GNUNET_SYSERR;
287 r->data.srv->domain_name = GNUNET_strdup (tok);
289 r->data.srv->target = parse_name (udp_payload,
292 if (old_off + data_len != *off)
293 return GNUNET_SYSERR;
296 r->data.raw.data = GNUNET_malloc (data_len);
297 r->data.raw.data_len = data_len;
298 memcpy (r->data.raw.data, &udp_payload[*off], data_len);
307 * Parse a UDP payload of a DNS packet in to a nice struct for further
308 * processing and manipulation.
310 * @param udp_payload wire-format of the DNS packet
311 * @param udp_payload_length number of bytes in udp_payload
312 * @return NULL on error, otherwise the parsed packet
314 struct GNUNET_DNSPARSER_Packet *
315 GNUNET_DNSPARSER_parse (const char *udp_payload,
316 size_t udp_payload_length)
318 struct GNUNET_DNSPARSER_Packet *p;
319 const struct GNUNET_TUN_DnsHeader *dns;
324 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
326 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
327 off = sizeof (struct GNUNET_TUN_DnsHeader);
328 p = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_Packet));
329 p->flags = dns->flags;
331 n = ntohs (dns->query_count);
334 p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
338 parse_query (udp_payload,
344 n = ntohs (dns->answer_rcount);
347 p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
351 parse_record (udp_payload,
357 n = ntohs (dns->authority_rcount);
360 p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
361 p->num_authority_records = n;
364 parse_record (udp_payload,
367 &p->authority_records[i]))
370 n = ntohs (dns->additional_rcount);
373 p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
374 p->num_additional_records = n;
377 parse_record (udp_payload,
380 &p->additional_records[i]))
385 GNUNET_DNSPARSER_free_packet (p);
391 * Free SOA information record.
393 * @param soa record to free
396 free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
400 GNUNET_free_non_null (soa->mname);
401 GNUNET_free_non_null (soa->rname);
407 * Free SRV information record.
409 * @param srv record to free
412 free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
416 GNUNET_free_non_null (srv->target);
417 GNUNET_free_non_null (srv->domain_name);
418 GNUNET_free_non_null (srv->proto);
419 GNUNET_free_non_null (srv->service);
425 * Free MX information record.
427 * @param mx record to free
430 free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
434 GNUNET_free_non_null (mx->mxhost);
440 free_record (struct GNUNET_DNSPARSER_Record *r)
442 GNUNET_free_non_null (r->name);
445 case GNUNET_DNSPARSER_TYPE_MX:
446 free_mx (r->data.mx);
448 case GNUNET_DNSPARSER_TYPE_SOA:
449 free_soa (r->data.soa);
451 case GNUNET_DNSPARSER_TYPE_SRV:
452 free_srv (r->data.srv);
454 case GNUNET_DNSPARSER_TYPE_NS:
455 case GNUNET_DNSPARSER_TYPE_CNAME:
456 case GNUNET_DNSPARSER_TYPE_PTR:
457 GNUNET_free_non_null (r->data.hostname);
460 GNUNET_free_non_null (r->data.raw.data);
467 * Free memory taken by a packet.
469 * @param p packet to free
472 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
476 for (i=0;i<p->num_queries;i++)
477 GNUNET_free_non_null (p->queries[i].name);
478 GNUNET_free_non_null (p->queries);
479 for (i=0;i<p->num_answers;i++)
480 free_record (&p->answers[i]);
481 GNUNET_free_non_null (p->answers);
482 for (i=0;i<p->num_authority_records;i++)
483 free_record (&p->authority_records[i]);
484 GNUNET_free_non_null (p->authority_records);
485 for (i=0;i<p->num_additional_records;i++)
486 free_record (&p->additional_records[i]);
487 GNUNET_free_non_null (p->additional_records);
492 /* ********************** DNS packet assembly code **************** */
496 * Add a DNS name to the UDP packet at the given location.
498 * @param dst where to write the name
499 * @param dst_len number of bytes in dst
500 * @param off pointer to offset where to write the name (increment by bytes used)
501 * must not be changed if there is an error
502 * @param name name to write
503 * @return GNUNET_SYSERR if 'name' is invalid
504 * GNUNET_NO if 'name' did not fit
505 * GNUNET_OK if 'name' was added to 'dst'
519 return GNUNET_SYSERR;
521 if (start + strlen (name) + 2 > dst_len)
526 dot = strchr (name, '.');
531 if ( (len >= 64) || (len == 0) )
532 return GNUNET_NO; /* segment too long or empty */
533 dst[pos++] = (char) (uint8_t) len;
534 memcpy (&dst[pos], name, len);
536 name += len + 1; /* also skip dot */
539 dst[pos++] = '\0'; /* terminator */
546 * Add a DNS query to the UDP packet at the given location.
548 * @param dst where to write the query
549 * @param dst_len number of bytes in dst
550 * @param off pointer to offset where to write the query (increment by bytes used)
551 * must not be changed if there is an error
552 * @param query query to write
553 * @return GNUNET_SYSERR if 'query' is invalid
554 * GNUNET_NO if 'query' did not fit
555 * GNUNET_OK if 'query' was added to 'dst'
558 add_query (char *dst,
561 const struct GNUNET_DNSPARSER_Query *query)
564 struct query_line ql;
566 ret = add_name (dst, dst_len - sizeof (struct query_line), off, query->name);
567 if (ret != GNUNET_OK)
569 ql.type = htons (query->type);
570 ql.class = htons (query->class);
571 memcpy (&dst[*off], &ql, sizeof (ql));
572 (*off) += sizeof (ql);
578 * Add an MX record to the UDP packet at the given location.
580 * @param dst where to write the mx record
581 * @param dst_len number of bytes in dst
582 * @param off pointer to offset where to write the mx information (increment by bytes used);
583 * can also change if there was an error
584 * @param mx mx information to write
585 * @return GNUNET_SYSERR if 'mx' is invalid
586 * GNUNET_NO if 'mx' did not fit
587 * GNUNET_OK if 'mx' was added to 'dst'
593 const struct GNUNET_DNSPARSER_MxRecord *mx)
597 if (*off + sizeof (uint16_t) > dst_len)
599 mxpref = htons (mx->preference);
600 memcpy (&dst[*off], &mxpref, sizeof (mxpref));
601 (*off) += sizeof (mxpref);
602 return add_name (dst, dst_len, off, mx->mxhost);
607 * Add an SOA record to the UDP packet at the given location.
609 * @param dst where to write the SOA record
610 * @param dst_len number of bytes in dst
611 * @param off pointer to offset where to write the SOA information (increment by bytes used)
612 * can also change if there was an error
613 * @param soa SOA information to write
614 * @return GNUNET_SYSERR if 'soa' is invalid
615 * GNUNET_NO if 'soa' did not fit
616 * GNUNET_OK if 'soa' was added to 'dst'
622 const struct GNUNET_DNSPARSER_SoaRecord *soa)
627 if ( (GNUNET_OK != (ret = add_name (dst,
631 (GNUNET_OK != (ret = add_name (dst,
636 if (*off + sizeof (struct soa_data) > dst_len)
638 sd.serial = htonl (soa->serial);
639 sd.refresh = htonl (soa->refresh);
640 sd.retry = htonl (soa->retry);
641 sd.expire = htonl (soa->expire);
642 sd.minimum = htonl (soa->minimum_ttl);
643 memcpy (&dst[*off], &sd, sizeof (sd));
644 (*off) += sizeof (sd);
650 * Add an SRV record to the UDP packet at the given location.
652 * @param dst where to write the SRV record
653 * @param dst_len number of bytes in dst
654 * @param off pointer to offset where to write the SRV information (increment by bytes used)
655 * can also change if there was an error
656 * @param srv SRV information to write
657 * @return GNUNET_SYSERR if 'srv' is invalid
658 * GNUNET_NO if 'srv' did not fit
659 * GNUNET_OK if 'srv' was added to 'dst'
665 const struct GNUNET_DNSPARSER_SrvRecord *srv)
670 if (*off + sizeof (struct srv_data) > dst_len)
672 sd.prio = htons (srv->priority);
673 sd.weight = htons (srv->weight);
674 sd.port = htons (srv->port);
675 memcpy (&dst[*off], &sd, sizeof (sd));
676 (*off) += sizeof (sd);
677 if (GNUNET_OK != (ret = add_name (dst,
687 * Add a DNS record to the UDP packet at the given location.
689 * @param dst where to write the query
690 * @param dst_len number of bytes in dst
691 * @param off pointer to offset where to write the query (increment by bytes used)
692 * must not be changed if there is an error
693 * @param record record to write
694 * @return GNUNET_SYSERR if 'record' is invalid
695 * GNUNET_NO if 'record' did not fit
696 * GNUNET_OK if 'record' was added to 'dst'
699 add_record (char *dst,
702 const struct GNUNET_DNSPARSER_Record *record)
707 struct record_line rl;
711 /* for SRV records, we can create the name from the details
712 of the record if needed */
714 if ( (GNUNET_DNSPARSER_TYPE_SRV == record->type) &&
716 GNUNET_asprintf (&name,
718 record->data.srv->service,
719 record->data.srv->proto,
720 record->data.srv->domain_name);
721 ret = add_name (dst, dst_len - sizeof (struct record_line), off, name);
722 if (name != record->name)
724 if (GNUNET_OK != ret)
726 /* '*off' is now the position where we will need to write the record line */
728 pos = *off + sizeof (struct record_line);
729 switch (record->type)
731 case GNUNET_DNSPARSER_TYPE_MX:
732 ret = add_mx (dst, dst_len, &pos, record->data.mx);
734 case GNUNET_DNSPARSER_TYPE_SOA:
735 ret = add_soa (dst, dst_len, &pos, record->data.soa);
737 case GNUNET_DNSPARSER_TYPE_NS:
738 case GNUNET_DNSPARSER_TYPE_CNAME:
739 case GNUNET_DNSPARSER_TYPE_PTR:
740 ret = add_name (dst, dst_len, &pos, record->data.hostname);
742 case GNUNET_DNSPARSER_TYPE_SRV:
743 ret = add_srv (dst, dst_len, &pos, record->data.srv);
746 if (pos + record->data.raw.data_len > dst_len)
751 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
752 pos += record->data.raw.data_len;
756 if (GNUNET_OK != ret)
762 if (pos - (*off + sizeof (struct record_line)) > UINT16_MAX)
764 /* record data too long */
768 rl.type = htons (record->type);
769 rl.class = htons (record->class);
770 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value / 1000); /* in seconds */
771 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct record_line))));
772 memcpy (&dst[*off], &rl, sizeof (struct record_line));
779 * Given a DNS packet, generate the corresponding UDP payload.
780 * Note that we do not attempt to pack the strings with pointers
781 * as this would complicate the code and this is about being
782 * simple and secure, not fast, fancy and broken like bind.
784 * @param p packet to pack
785 * @param max maximum allowed size for the resulting UDP payload
786 * @param buf set to a buffer with the packed message
787 * @param buf_length set to the length of buf
788 * @return GNUNET_SYSERR if 'p' is invalid
789 * GNUNET_NO if 'p' was truncated (but there is still a result in 'buf')
790 * GNUNET_OK if 'p' was packed completely into '*buf'
793 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
798 struct GNUNET_TUN_DnsHeader dns;
805 if ( (p->num_queries > UINT16_MAX) ||
806 (p->num_answers > UINT16_MAX) ||
807 (p->num_authority_records > UINT16_MAX) ||
808 (p->num_additional_records > UINT16_MAX) )
809 return GNUNET_SYSERR;
811 dns.flags = p->flags;
812 dns.query_count = htons (p->num_queries);
813 dns.answer_rcount = htons (p->num_answers);
814 dns.authority_rcount = htons (p->num_authority_records);
815 dns.additional_rcount = htons (p->num_additional_records);
817 off = sizeof (struct GNUNET_TUN_DnsHeader);
819 for (i=0;i<p->num_queries;i++)
821 ret = add_query (tmp, sizeof (tmp), &off, &p->queries[i]);
822 if (GNUNET_SYSERR == ret)
823 return GNUNET_SYSERR;
824 if (GNUNET_NO == ret)
826 dns.query_count = htons ((uint16_t) (i-1));
831 for (i=0;i<p->num_answers;i++)
833 ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
834 if (GNUNET_SYSERR == ret)
835 return GNUNET_SYSERR;
836 if (GNUNET_NO == ret)
838 dns.answer_rcount = htons ((uint16_t) (i-1));
843 for (i=0;i<p->num_authority_records;i++)
845 ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
846 if (GNUNET_SYSERR == ret)
847 return GNUNET_SYSERR;
848 if (GNUNET_NO == ret)
850 dns.authority_rcount = htons ((uint16_t) (i-1));
855 for (i=0;i<p->num_additional_records;i++)
857 ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
858 if (GNUNET_SYSERR == ret)
859 return GNUNET_SYSERR;
860 if (GNUNET_NO == ret)
862 dns.additional_rcount = htons (i-1);
868 if (GNUNET_YES == trc)
869 dns.flags.message_truncated = 1;
870 memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
872 *buf = GNUNET_malloc (off);
874 memcpy (*buf, tmp, off);
875 if (GNUNET_YES == trc)
880 /* end of dnsparser.c */