2 This file is part of GNUnet
3 Copyright (C) 2010-2014 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file dns/dnsparser.c
21 * @brief helper library to parse DNS packets.
22 * @author Philipp Toelke
23 * @author Christian Grothoff
30 #include "gnunet_util_lib.h"
34 * Check if a label in UTF-8 format can be coded into valid IDNA.
35 * This can fail if the ASCII-conversion becomes longer than 63 characters.
37 * @param label label to check (UTF-8 string)
38 * @return #GNUNET_OK if the label can be converted to IDNA,
39 * #GNUNET_SYSERR if the label is not valid for DNS names
42 GNUNET_DNSPARSER_check_label (const char *label)
47 if (NULL != strchr (label, '.'))
48 return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */
50 idna_to_ascii_8z (label, &output, IDNA_ALLOW_UNASSIGNED))
52 slen = strlen (output);
58 return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK;
63 * Check if a label in UTF-8 format can be coded into valid IDNA.
64 * This can fail if the ASCII-conversion becomes longer than 253 characters.
66 * @param name name to check (UTF-8 string)
67 * @return #GNUNET_OK if the label can be converted to IDNA,
68 * #GNUNET_SYSERR if the label is not valid for DNS names
71 GNUNET_DNSPARSER_check_name (const char *name)
78 ldup = GNUNET_strdup (name);
79 for (tok = strtok (ldup, "."); NULL != tok; tok = strtok (NULL, "."))
81 GNUNET_DNSPARSER_check_label (tok))
88 idna_to_ascii_8z (name, &output, IDNA_ALLOW_UNASSIGNED))
90 slen = strlen (output);
96 return (slen > 253) ? GNUNET_SYSERR : GNUNET_OK;
101 * Free SOA information record.
103 * @param soa record to free
106 GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
110 GNUNET_free_non_null (soa->mname);
111 GNUNET_free_non_null (soa->rname);
117 * Free CERT information record.
119 * @param cert record to free
122 GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert)
126 GNUNET_free_non_null (cert->certificate_data);
132 * Free SRV information record.
134 * @param srv record to free
137 GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
141 GNUNET_free_non_null (srv->target);
147 * Free MX information record.
149 * @param mx record to free
152 GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
156 GNUNET_free_non_null (mx->mxhost);
162 * Free the given DNS record.
164 * @param r record to free
167 GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
169 GNUNET_free_non_null (r->name);
172 case GNUNET_DNSPARSER_TYPE_MX:
173 GNUNET_DNSPARSER_free_mx (r->data.mx);
175 case GNUNET_DNSPARSER_TYPE_SOA:
176 GNUNET_DNSPARSER_free_soa (r->data.soa);
178 case GNUNET_DNSPARSER_TYPE_SRV:
179 GNUNET_DNSPARSER_free_srv (r->data.srv);
181 case GNUNET_DNSPARSER_TYPE_CERT:
182 GNUNET_DNSPARSER_free_cert (r->data.cert);
184 case GNUNET_DNSPARSER_TYPE_NS:
185 case GNUNET_DNSPARSER_TYPE_CNAME:
186 case GNUNET_DNSPARSER_TYPE_PTR:
187 GNUNET_free_non_null (r->data.hostname);
190 GNUNET_free_non_null (r->data.raw.data);
197 * Parse name inside of a DNS query or record.
199 * @param udp_payload entire UDP payload
200 * @param udp_payload_length length of @a udp_payload
201 * @param off pointer to the offset of the name to parse in the udp_payload (to be
202 * incremented by the size of the name)
203 * @param depth current depth of our recursion (to prevent stack overflow)
204 * @return name as 0-terminated C string on success, NULL if the payload is malformed
207 parse_name (const char *udp_payload,
208 size_t udp_payload_length,
212 const uint8_t *input = (const uint8_t *) udp_payload;
221 ret = GNUNET_strdup ("");
224 if (*off >= udp_payload_length)
237 if (*off + 1 + len > udp_payload_length)
242 GNUNET_asprintf (&tmp,
245 &udp_payload[*off + 1]);
247 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
249 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
250 _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
254 GNUNET_asprintf (&tmp,
258 &udp_payload[*off + 1]);
263 GNUNET_asprintf (&tmp,
277 else if ((64 | 128) == (len & (64 | 128)) )
282 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
284 /* pointer to string */
285 if (*off + 1 > udp_payload_length)
290 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
291 xstr = parse_name (udp_payload,
300 GNUNET_asprintf (&tmp,
307 if (strlen (ret) > udp_payload_length)
310 goto error; /* we are looping (building an infinite string) */
313 /* pointers always terminate names */
318 /* neither pointer nor inline string, not supported... */
324 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
334 * Parse name inside of a DNS query or record.
336 * @param udp_payload entire UDP payload
337 * @param udp_payload_length length of @a udp_payload
338 * @param off pointer to the offset of the name to parse in the udp_payload (to be
339 * incremented by the size of the name)
340 * @return name as 0-terminated C string on success, NULL if the payload is malformed
343 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
344 size_t udp_payload_length,
347 return parse_name (udp_payload, udp_payload_length, off, 0);
352 * Parse a DNS query entry.
354 * @param udp_payload entire UDP payload
355 * @param udp_payload_length length of @a udp_payload
356 * @param off pointer to the offset of the query to parse in the udp_payload (to be
357 * incremented by the size of the query)
358 * @param q where to write the query information
359 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
362 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
363 size_t udp_payload_length,
365 struct GNUNET_DNSPARSER_Query *q)
368 struct GNUNET_TUN_DnsQueryLine ql;
370 name = GNUNET_DNSPARSER_parse_name (udp_payload,
376 return GNUNET_SYSERR;
379 if (*off + sizeof (struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
382 return GNUNET_SYSERR;
384 GNUNET_memcpy (&ql, &udp_payload[*off], sizeof (ql));
386 q->type = ntohs (ql.type);
387 q->dns_traffic_class = ntohs (ql.dns_traffic_class);
393 * Parse a DNS SOA record.
395 * @param udp_payload reference to UDP packet
396 * @param udp_payload_length length of @a udp_payload
397 * @param off pointer to the offset of the query to parse in the SOA record (to be
398 * incremented by the size of the record), unchanged on error
399 * @return the parsed SOA record, NULL on error
401 struct GNUNET_DNSPARSER_SoaRecord *
402 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
403 size_t udp_payload_length,
406 struct GNUNET_DNSPARSER_SoaRecord *soa;
407 struct GNUNET_TUN_DnsSoaRecord soa_bin;
411 soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
412 soa->mname = GNUNET_DNSPARSER_parse_name (udp_payload,
415 soa->rname = GNUNET_DNSPARSER_parse_name (udp_payload,
418 if ( (NULL == soa->mname) ||
419 (NULL == soa->rname) ||
420 (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length) )
423 GNUNET_DNSPARSER_free_soa (soa);
427 GNUNET_memcpy (&soa_bin,
429 sizeof (struct GNUNET_TUN_DnsSoaRecord));
430 soa->serial = ntohl (soa_bin.serial);
431 soa->refresh = ntohl (soa_bin.refresh);
432 soa->retry = ntohl (soa_bin.retry);
433 soa->expire = ntohl (soa_bin.expire);
434 soa->minimum_ttl = ntohl (soa_bin.minimum);
435 (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
441 * Parse a DNS MX record.
443 * @param udp_payload reference to UDP packet
444 * @param udp_payload_length length of @a udp_payload
445 * @param off pointer to the offset of the query to parse in the MX record (to be
446 * incremented by the size of the record), unchanged on error
447 * @return the parsed MX record, NULL on error
449 struct GNUNET_DNSPARSER_MxRecord *
450 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
451 size_t udp_payload_length,
454 struct GNUNET_DNSPARSER_MxRecord *mx;
459 if (*off + sizeof (uint16_t) > udp_payload_length)
464 GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
465 (*off) += sizeof (uint16_t);
466 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
467 mx->preference = ntohs (mxpref);
468 mx->mxhost = GNUNET_DNSPARSER_parse_name (udp_payload,
471 if (NULL == mx->mxhost)
474 GNUNET_DNSPARSER_free_mx (mx);
483 * Parse a DNS SRV record.
485 * @param udp_payload reference to UDP packet
486 * @param udp_payload_length length of @a udp_payload
487 * @param off pointer to the offset of the query to parse in the SRV record (to be
488 * incremented by the size of the record), unchanged on error
489 * @return the parsed SRV record, NULL on error
491 struct GNUNET_DNSPARSER_SrvRecord *
492 GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
493 size_t udp_payload_length,
496 struct GNUNET_DNSPARSER_SrvRecord *srv;
497 struct GNUNET_TUN_DnsSrvRecord srv_bin;
501 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
503 GNUNET_memcpy (&srv_bin,
505 sizeof (struct GNUNET_TUN_DnsSrvRecord));
506 (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
507 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
508 srv->priority = ntohs (srv_bin.prio);
509 srv->weight = ntohs (srv_bin.weight);
510 srv->port = ntohs (srv_bin.port);
511 srv->target = GNUNET_DNSPARSER_parse_name (udp_payload,
514 if (NULL == srv->target)
516 GNUNET_DNSPARSER_free_srv (srv);
525 * Parse a DNS CERT record.
527 * @param udp_payload reference to UDP packet
528 * @param udp_payload_length length of @a udp_payload
529 * @param off pointer to the offset of the query to parse in the CERT record (to be
530 * incremented by the size of the record), unchanged on error
531 * @return the parsed CERT record, NULL on error
533 struct GNUNET_DNSPARSER_CertRecord *
534 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
535 size_t udp_payload_length,
538 struct GNUNET_DNSPARSER_CertRecord *cert;
539 struct GNUNET_TUN_DnsCertRecord dcert;
541 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
546 GNUNET_memcpy (&dcert,
548 sizeof (struct GNUNET_TUN_DnsCertRecord));
549 (*off) += sizeof (struct GNUNET_TUN_DnsCertRecord);
550 cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
551 cert->cert_type = ntohs (dcert.cert_type);
552 cert->cert_tag = ntohs (dcert.cert_tag);
553 cert->algorithm = dcert.algorithm;
554 cert->certificate_size = udp_payload_length - (*off);
555 cert->certificate_data = GNUNET_malloc (cert->certificate_size);
556 GNUNET_memcpy (cert->certificate_data,
558 cert->certificate_size);
559 (*off) += cert->certificate_size;
565 * Parse a DNS record entry.
567 * @param udp_payload entire UDP payload
568 * @param udp_payload_length length of @a udp_payload
569 * @param off pointer to the offset of the record to parse in the udp_payload (to be
570 * incremented by the size of the record)
571 * @param r where to write the record information
572 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
575 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
576 size_t udp_payload_length,
578 struct GNUNET_DNSPARSER_Record *r)
581 struct GNUNET_TUN_DnsRecordLine rl;
585 name = GNUNET_DNSPARSER_parse_name (udp_payload,
591 return GNUNET_SYSERR;
594 if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
597 return GNUNET_SYSERR;
599 GNUNET_memcpy (&rl, &udp_payload[*off], sizeof (rl));
600 (*off) += sizeof (rl);
601 r->type = ntohs (rl.type);
602 r->dns_traffic_class = ntohs (rl.dns_traffic_class);
603 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
605 data_len = ntohs (rl.data_len);
606 if (*off + data_len > udp_payload_length)
609 return GNUNET_SYSERR;
614 case GNUNET_DNSPARSER_TYPE_NS:
615 case GNUNET_DNSPARSER_TYPE_CNAME:
616 case GNUNET_DNSPARSER_TYPE_DNAME:
617 case GNUNET_DNSPARSER_TYPE_PTR:
618 r->data.hostname = GNUNET_DNSPARSER_parse_name (udp_payload,
621 if ( (NULL == r->data.hostname) ||
622 (old_off + data_len != *off) )
623 return GNUNET_SYSERR;
625 case GNUNET_DNSPARSER_TYPE_SOA:
626 r->data.soa = GNUNET_DNSPARSER_parse_soa (udp_payload,
629 if ( (NULL == r->data.soa) ||
630 (old_off + data_len != *off) )
633 return GNUNET_SYSERR;
636 case GNUNET_DNSPARSER_TYPE_MX:
637 r->data.mx = GNUNET_DNSPARSER_parse_mx (udp_payload,
640 if ( (NULL == r->data.mx) ||
641 (old_off + data_len != *off) )
644 return GNUNET_SYSERR;
647 case GNUNET_DNSPARSER_TYPE_SRV:
648 r->data.srv = GNUNET_DNSPARSER_parse_srv (udp_payload,
651 if ( (NULL == r->data.srv) ||
652 (old_off + data_len != *off) )
655 return GNUNET_SYSERR;
659 r->data.raw.data = GNUNET_malloc (data_len);
660 r->data.raw.data_len = data_len;
661 GNUNET_memcpy (r->data.raw.data,
672 * Parse a UDP payload of a DNS packet in to a nice struct for further
673 * processing and manipulation.
675 * @param udp_payload wire-format of the DNS packet
676 * @param udp_payload_length number of bytes in @a udp_payload
677 * @return NULL on error, otherwise the parsed packet
679 struct GNUNET_DNSPARSER_Packet *
680 GNUNET_DNSPARSER_parse (const char *udp_payload,
681 size_t udp_payload_length)
683 struct GNUNET_DNSPARSER_Packet *p;
684 const struct GNUNET_TUN_DnsHeader *dns;
688 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
690 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
691 off = sizeof (struct GNUNET_TUN_DnsHeader);
692 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
693 p->flags = dns->flags;
695 n = ntohs (dns->query_count);
698 p->queries = GNUNET_new_array (n,
699 struct GNUNET_DNSPARSER_Query);
701 for (unsigned int i=0;i<n;i++)
703 GNUNET_DNSPARSER_parse_query (udp_payload,
709 n = ntohs (dns->answer_rcount);
712 p->answers = GNUNET_new_array (n,
713 struct GNUNET_DNSPARSER_Record);
715 for (unsigned int i=0;i<n;i++)
717 GNUNET_DNSPARSER_parse_record (udp_payload,
723 n = ntohs (dns->authority_rcount);
726 p->authority_records = GNUNET_new_array (n,
727 struct GNUNET_DNSPARSER_Record);
728 p->num_authority_records = n;
729 for (unsigned int i=0;i<n;i++)
731 GNUNET_DNSPARSER_parse_record (udp_payload,
734 &p->authority_records[i]))
737 n = ntohs (dns->additional_rcount);
740 p->additional_records = GNUNET_new_array (n,
741 struct GNUNET_DNSPARSER_Record);
742 p->num_additional_records = n;
743 for (unsigned int i=0;i<n;i++)
746 GNUNET_DNSPARSER_parse_record (udp_payload,
749 &p->additional_records[i]))
756 GNUNET_DNSPARSER_free_packet (p);
762 * Duplicate (deep-copy) the given DNS record
764 * @param r the record
765 * @return the newly allocated record
767 struct GNUNET_DNSPARSER_Record *
768 GNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r)
770 struct GNUNET_DNSPARSER_Record *dup = GNUNET_memdup (r, sizeof (*r));
772 dup->name = GNUNET_strdup (r->name);
775 case GNUNET_DNSPARSER_TYPE_NS:
776 case GNUNET_DNSPARSER_TYPE_CNAME:
777 case GNUNET_DNSPARSER_TYPE_PTR:
779 dup->data.hostname = GNUNET_strdup (r->data.hostname);
782 case GNUNET_DNSPARSER_TYPE_SOA:
784 dup->data.soa = GNUNET_DNSPARSER_duplicate_soa_record (r->data.soa);
787 case GNUNET_DNSPARSER_TYPE_CERT:
789 dup->data.cert = GNUNET_DNSPARSER_duplicate_cert_record (r->data.cert);
792 case GNUNET_DNSPARSER_TYPE_MX:
794 dup->data.mx = GNUNET_DNSPARSER_duplicate_mx_record (r->data.mx);
797 case GNUNET_DNSPARSER_TYPE_SRV:
799 dup->data.srv = GNUNET_DNSPARSER_duplicate_srv_record (r->data.srv);
804 dup->data.raw.data = GNUNET_memdup (r->data.raw.data,
805 r->data.raw.data_len);
813 * Duplicate (deep-copy) the given DNS record
815 * @param r the record
816 * @return the newly allocated record
818 struct GNUNET_DNSPARSER_SoaRecord *
819 GNUNET_DNSPARSER_duplicate_soa_record (const struct GNUNET_DNSPARSER_SoaRecord *r)
821 struct GNUNET_DNSPARSER_SoaRecord *dup = GNUNET_memdup (r, sizeof (*r));
823 dup->mname = GNUNET_strdup (r->mname);
824 dup->rname = GNUNET_strdup (r->rname);
830 * Duplicate (deep-copy) the given DNS record
832 * @param r the record
833 * @return the newly allocated record
835 struct GNUNET_DNSPARSER_CertRecord *
836 GNUNET_DNSPARSER_duplicate_cert_record (const struct GNUNET_DNSPARSER_CertRecord *r)
838 struct GNUNET_DNSPARSER_CertRecord *dup = GNUNET_memdup (r, sizeof (*r));
840 dup->certificate_data = GNUNET_strdup (r->certificate_data);
846 * Duplicate (deep-copy) the given DNS record
848 * @param r the record
849 * @return the newly allocated record
851 struct GNUNET_DNSPARSER_MxRecord *
852 GNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r)
854 struct GNUNET_DNSPARSER_MxRecord *dup = GNUNET_memdup (r, sizeof (*r));
856 dup->mxhost = GNUNET_strdup (r->mxhost);
862 * Duplicate (deep-copy) the given DNS record
864 * @param r the record
865 * @return the newly allocated record
867 struct GNUNET_DNSPARSER_SrvRecord *
868 GNUNET_DNSPARSER_duplicate_srv_record (const struct GNUNET_DNSPARSER_SrvRecord *r)
870 struct GNUNET_DNSPARSER_SrvRecord *dup = GNUNET_memdup (r, sizeof (*r));
872 dup->target = GNUNET_strdup (r->target);
878 * Free memory taken by a packet.
880 * @param p packet to free
883 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
885 for (unsigned int i=0;i<p->num_queries;i++)
886 GNUNET_free_non_null (p->queries[i].name);
887 GNUNET_free_non_null (p->queries);
888 for (unsigned int i=0;i<p->num_answers;i++)
889 GNUNET_DNSPARSER_free_record (&p->answers[i]);
890 GNUNET_free_non_null (p->answers);
891 for (unsigned int i=0;i<p->num_authority_records;i++)
892 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
893 GNUNET_free_non_null (p->authority_records);
894 for (unsigned int i=0;i<p->num_additional_records;i++)
895 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
896 GNUNET_free_non_null (p->additional_records);
901 /* ********************** DNS packet assembly code **************** */
905 * Add a DNS name to the UDP packet at the given location, converting
906 * the name to IDNA notation as necessary.
908 * @param dst where to write the name (UDP packet)
909 * @param dst_len number of bytes in @a dst
910 * @param off pointer to offset where to write the name (increment by bytes used)
911 * must not be changed if there is an error
912 * @param name name to write
913 * @return #GNUNET_SYSERR if @a name is invalid
914 * #GNUNET_NO if @a name did not fit
915 * #GNUNET_OK if @a name was added to @a dst
918 GNUNET_DNSPARSER_builder_add_name (char *dst,
924 const char *idna_name;
932 return GNUNET_SYSERR;
935 (rc = idna_to_ascii_8z (name,
937 IDNA_ALLOW_UNASSIGNED)))
939 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
940 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
945 idna_name = idna_start;
947 if (start + strlen (idna_name) + 2 > dst_len)
952 dot = strchr (idna_name, '.');
954 len = strlen (idna_name);
956 len = dot - idna_name;
957 if ( (len >= 64) || (0 == len) )
959 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
960 "Invalid DNS name `%s': label with %u characters encountered\n",
963 goto fail; /* label too long or empty */
965 dst[pos++] = (char) (uint8_t) len;
966 GNUNET_memcpy (&dst[pos],
970 idna_name += len + 1; /* also skip dot */
973 dst[pos++] = '\0'; /* terminator */
976 idn_free (idna_start);
983 idn_free (idna_start);
992 * Add a DNS query to the UDP packet at the given location.
994 * @param dst where to write the query
995 * @param dst_len number of bytes in @a dst
996 * @param off pointer to offset where to write the query (increment by bytes used)
997 * must not be changed if there is an error
998 * @param query query to write
999 * @return #GNUNET_SYSERR if @a query is invalid
1000 * #GNUNET_NO if @a query did not fit
1001 * #GNUNET_OK if @a query was added to @a dst
1004 GNUNET_DNSPARSER_builder_add_query (char *dst,
1007 const struct GNUNET_DNSPARSER_Query *query)
1010 struct GNUNET_TUN_DnsQueryLine ql;
1012 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
1013 if (ret != GNUNET_OK)
1015 ql.type = htons (query->type);
1016 ql.dns_traffic_class = htons (query->dns_traffic_class);
1017 GNUNET_memcpy (&dst[*off], &ql, sizeof (ql));
1018 (*off) += sizeof (ql);
1024 * Add an MX record to the UDP packet at the given location.
1026 * @param dst where to write the mx record
1027 * @param dst_len number of bytes in @a dst
1028 * @param off pointer to offset where to write the mx information (increment by bytes used);
1029 * can also change if there was an error
1030 * @param mx mx information to write
1031 * @return #GNUNET_SYSERR if @a mx is invalid
1032 * #GNUNET_NO if @a mx did not fit
1033 * #GNUNET_OK if @a mx was added to @a dst
1036 GNUNET_DNSPARSER_builder_add_mx (char *dst,
1039 const struct GNUNET_DNSPARSER_MxRecord *mx)
1043 if (*off + sizeof (uint16_t) > dst_len)
1045 mxpref = htons (mx->preference);
1046 GNUNET_memcpy (&dst[*off],
1049 (*off) += sizeof (mxpref);
1050 return GNUNET_DNSPARSER_builder_add_name (dst,
1058 * Add a CERT record to the UDP packet at the given location.
1060 * @param dst where to write the CERT record
1061 * @param dst_len number of bytes in @a dst
1062 * @param off pointer to offset where to write the CERT information (increment by bytes used);
1063 * can also change if there was an error
1064 * @param cert CERT information to write
1065 * @return #GNUNET_SYSERR if @a cert is invalid
1066 * #GNUNET_NO if @a cert did not fit
1067 * #GNUNET_OK if @a cert was added to @a dst
1070 GNUNET_DNSPARSER_builder_add_cert (char *dst,
1073 const struct GNUNET_DNSPARSER_CertRecord *cert)
1075 struct GNUNET_TUN_DnsCertRecord dcert;
1077 if ( (cert->cert_type > UINT16_MAX) ||
1078 (cert->algorithm > UINT8_MAX) )
1081 return GNUNET_SYSERR;
1083 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size > dst_len)
1085 dcert.cert_type = htons ((uint16_t) cert->cert_type);
1086 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
1087 dcert.algorithm = (uint8_t) cert->algorithm;
1088 GNUNET_memcpy (&dst[*off], &dcert, sizeof (dcert));
1089 (*off) += sizeof (dcert);
1090 GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
1091 (*off) += cert->certificate_size;
1097 * Add an SOA record to the UDP packet at the given location.
1099 * @param dst where to write the SOA record
1100 * @param dst_len number of bytes in @a dst
1101 * @param off pointer to offset where to write the SOA information (increment by bytes used)
1102 * can also change if there was an error
1103 * @param soa SOA information to write
1104 * @return #GNUNET_SYSERR if @a soa is invalid
1105 * #GNUNET_NO if @a soa did not fit
1106 * #GNUNET_OK if @a soa was added to @a dst
1109 GNUNET_DNSPARSER_builder_add_soa (char *dst,
1112 const struct GNUNET_DNSPARSER_SoaRecord *soa)
1114 struct GNUNET_TUN_DnsSoaRecord sd;
1117 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1121 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1126 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1128 sd.serial = htonl (soa->serial);
1129 sd.refresh = htonl (soa->refresh);
1130 sd.retry = htonl (soa->retry);
1131 sd.expire = htonl (soa->expire);
1132 sd.minimum = htonl (soa->minimum_ttl);
1133 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1134 (*off) += sizeof (sd);
1140 * Add an SRV record to the UDP packet at the given location.
1142 * @param dst where to write the SRV record
1143 * @param dst_len number of bytes in @a dst
1144 * @param off pointer to offset where to write the SRV information (increment by bytes used)
1145 * can also change if there was an error
1146 * @param srv SRV information to write
1147 * @return #GNUNET_SYSERR if @a srv is invalid
1148 * #GNUNET_NO if @a srv did not fit
1149 * #GNUNET_OK if @a srv was added to @a dst
1152 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1155 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1157 struct GNUNET_TUN_DnsSrvRecord sd;
1160 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1162 sd.prio = htons (srv->priority);
1163 sd.weight = htons (srv->weight);
1164 sd.port = htons (srv->port);
1165 GNUNET_memcpy (&dst[*off],
1168 (*off) += sizeof (sd);
1169 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1179 * Add a DNS record to the UDP packet at the given location.
1181 * @param dst where to write the query
1182 * @param dst_len number of bytes in @a dst
1183 * @param off pointer to offset where to write the query (increment by bytes used)
1184 * must not be changed if there is an error
1185 * @param record record to write
1186 * @return #GNUNET_SYSERR if @a record is invalid
1187 * #GNUNET_NO if @a record did not fit
1188 * #GNUNET_OK if @a record was added to @a dst
1191 add_record (char *dst,
1194 const struct GNUNET_DNSPARSER_Record *record)
1199 struct GNUNET_TUN_DnsRecordLine rl;
1202 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1203 dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine),
1206 if (GNUNET_OK != ret)
1208 /* '*off' is now the position where we will need to write the record line */
1210 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1211 switch (record->type)
1213 case GNUNET_DNSPARSER_TYPE_MX:
1214 ret = GNUNET_DNSPARSER_builder_add_mx (dst,
1219 case GNUNET_DNSPARSER_TYPE_CERT:
1220 ret = GNUNET_DNSPARSER_builder_add_cert (dst,
1225 case GNUNET_DNSPARSER_TYPE_SOA:
1226 ret = GNUNET_DNSPARSER_builder_add_soa (dst,
1231 case GNUNET_DNSPARSER_TYPE_NS:
1232 case GNUNET_DNSPARSER_TYPE_CNAME:
1233 case GNUNET_DNSPARSER_TYPE_PTR:
1234 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1237 record->data.hostname);
1239 case GNUNET_DNSPARSER_TYPE_SRV:
1240 ret = GNUNET_DNSPARSER_builder_add_srv (dst,
1246 if (pos + record->data.raw.data_len > dst_len)
1251 GNUNET_memcpy (&dst[pos],
1252 record->data.raw.data,
1253 record->data.raw.data_len);
1254 pos += record->data.raw.data_len;
1258 if (GNUNET_OK != ret)
1264 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1266 /* record data too long */
1270 rl.type = htons (record->type);
1271 rl.dns_traffic_class = htons (record->dns_traffic_class);
1272 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1273 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1274 GNUNET_memcpy (&dst[*off],
1276 sizeof (struct GNUNET_TUN_DnsRecordLine));
1283 * Given a DNS packet @a p, generate the corresponding UDP payload.
1284 * Note that we do not attempt to pack the strings with pointers
1285 * as this would complicate the code and this is about being
1286 * simple and secure, not fast, fancy and broken like bind.
1288 * @param p packet to pack
1289 * @param max maximum allowed size for the resulting UDP payload
1290 * @param buf set to a buffer with the packed message
1291 * @param buf_length set to the length of @a buf
1292 * @return #GNUNET_SYSERR if @a p is invalid
1293 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1294 * #GNUNET_OK if @a p was packed completely into @a buf
1297 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1302 struct GNUNET_TUN_DnsHeader dns;
1308 if ( (p->num_queries > UINT16_MAX) ||
1309 (p->num_answers > UINT16_MAX) ||
1310 (p->num_authority_records > UINT16_MAX) ||
1311 (p->num_additional_records > UINT16_MAX) )
1312 return GNUNET_SYSERR;
1314 dns.flags = p->flags;
1315 dns.query_count = htons (p->num_queries);
1316 dns.answer_rcount = htons (p->num_answers);
1317 dns.authority_rcount = htons (p->num_authority_records);
1318 dns.additional_rcount = htons (p->num_additional_records);
1320 off = sizeof (struct GNUNET_TUN_DnsHeader);
1322 for (unsigned int i=0;i<p->num_queries;i++)
1324 ret = GNUNET_DNSPARSER_builder_add_query (tmp,
1328 if (GNUNET_SYSERR == ret)
1329 return GNUNET_SYSERR;
1330 if (GNUNET_NO == ret)
1332 dns.query_count = htons ((uint16_t) (i-1));
1337 for (unsigned int i=0;i<p->num_answers;i++)
1339 ret = add_record (tmp,
1343 if (GNUNET_SYSERR == ret)
1344 return GNUNET_SYSERR;
1345 if (GNUNET_NO == ret)
1347 dns.answer_rcount = htons ((uint16_t) (i-1));
1352 for (unsigned int i=0;i<p->num_authority_records;i++)
1354 ret = add_record (tmp,
1357 &p->authority_records[i]);
1358 if (GNUNET_SYSERR == ret)
1359 return GNUNET_SYSERR;
1360 if (GNUNET_NO == ret)
1362 dns.authority_rcount = htons ((uint16_t) (i-1));
1367 for (unsigned int i=0;i<p->num_additional_records;i++)
1369 ret = add_record (tmp,
1372 &p->additional_records[i]);
1373 if (GNUNET_SYSERR == ret)
1374 return GNUNET_SYSERR;
1375 if (GNUNET_NO == ret)
1377 dns.additional_rcount = htons (i-1);
1383 if (GNUNET_YES == trc)
1384 dns.flags.message_truncated = 1;
1387 sizeof (struct GNUNET_TUN_DnsHeader));
1389 *buf = GNUNET_malloc (off);
1391 GNUNET_memcpy (*buf,
1394 if (GNUNET_YES == trc)
1401 * Convert a block of binary data to HEX.
1403 * @param data binary data to convert
1404 * @param data_size number of bytes in @a data
1405 * @return HEX string (lower case)
1408 GNUNET_DNSPARSER_bin_to_hex (const void *data,
1413 const uint8_t *idata;
1416 ret = GNUNET_malloc (data_size * 2 + 1);
1417 for (off = 0; off < data_size; off++)
1418 sprintf (&ret[off * 2],
1426 * Convert a HEX string to block of binary data.
1428 * @param hex HEX string to convert (may contain mixed case)
1429 * @param data where to write result, must be
1430 * at least `strlen(hex)/2` bytes long
1431 * @return number of bytes written to data
1434 GNUNET_DNSPARSER_hex_to_bin (const char *hex,
1443 data_size = strlen (hex) / 2;
1446 for (off = 0; off < data_size; off++)
1448 in[0] = tolower ((unsigned char) hex[off * 2]);
1449 in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
1450 if (1 != sscanf (in, "%x", &h))
1452 idata[off] = (uint8_t) h;
1458 /* end of dnsparser.c */