2 This file is part of GNUnet
3 Copyright (C) 2010-2014, 2018 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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/dnsparser.c
23 * @brief helper library to parse DNS packets.
24 * @author Philipp Toelke
25 * @author Christian Grothoff
31 #elif HAVE_IDN2_IDN2_H
32 #include <idn2/idn2.h>
44 #include "gnunet_util_lib.h"
48 * Check if a label in UTF-8 format can be coded into valid IDNA.
49 * This can fail if the ASCII-conversion becomes longer than 63 characters.
51 * @param label label to check (UTF-8 string)
52 * @return #GNUNET_OK if the label can be converted to IDNA,
53 * #GNUNET_SYSERR if the label is not valid for DNS names
56 GNUNET_DNSPARSER_check_label (const char *label)
61 if (NULL != strchr (label, '.'))
62 return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */
63 if (0 == strcmp (label, "@"))
64 return GNUNET_SYSERR; /* '@' is reserved for the empty label,
65 see #GNUNET_GNS_EMPTY_LABEL_AT */
66 if (IDNA_SUCCESS != idna_to_ascii_8z (label, &output, IDNA_ALLOW_UNASSIGNED))
68 slen = strlen (output);
74 return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK;
79 * Check if a label in UTF-8 format can be coded into valid IDNA.
80 * This can fail if the ASCII-conversion becomes longer than 253 characters.
82 * @param name name to check (UTF-8 string)
83 * @return #GNUNET_OK if the label can be converted to IDNA,
84 * #GNUNET_SYSERR if the label is not valid for DNS names
87 GNUNET_DNSPARSER_check_name (const char *name)
94 ldup = GNUNET_strdup (name);
95 for (tok = strtok (ldup, "."); NULL != tok; tok = strtok (NULL, "."))
96 if (GNUNET_OK != GNUNET_DNSPARSER_check_label (tok))
102 if (IDNA_SUCCESS != idna_to_ascii_8z (name, &output, IDNA_ALLOW_UNASSIGNED))
103 return GNUNET_SYSERR;
104 slen = strlen (output);
110 return (slen > 253) ? GNUNET_SYSERR : GNUNET_OK;
115 * Free SOA information record.
117 * @param soa record to free
120 GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
124 GNUNET_free_non_null (soa->mname);
125 GNUNET_free_non_null (soa->rname);
131 * Free CERT information record.
133 * @param cert record to free
136 GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert)
140 GNUNET_free_non_null (cert->certificate_data);
146 * Free SRV information record.
148 * @param srv record to free
151 GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
155 GNUNET_free_non_null (srv->target);
161 * Free MX information record.
163 * @param mx record to free
166 GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
170 GNUNET_free_non_null (mx->mxhost);
176 * Free the given DNS record.
178 * @param r record to free
181 GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
183 GNUNET_free_non_null (r->name);
186 case GNUNET_DNSPARSER_TYPE_MX:
187 GNUNET_DNSPARSER_free_mx (r->data.mx);
189 case GNUNET_DNSPARSER_TYPE_SOA:
190 GNUNET_DNSPARSER_free_soa (r->data.soa);
192 case GNUNET_DNSPARSER_TYPE_SRV:
193 GNUNET_DNSPARSER_free_srv (r->data.srv);
195 case GNUNET_DNSPARSER_TYPE_CERT:
196 GNUNET_DNSPARSER_free_cert (r->data.cert);
198 case GNUNET_DNSPARSER_TYPE_NS:
199 case GNUNET_DNSPARSER_TYPE_CNAME:
200 case GNUNET_DNSPARSER_TYPE_PTR:
201 GNUNET_free_non_null (r->data.hostname);
204 GNUNET_free_non_null (r->data.raw.data);
211 * Parse name inside of a DNS query or record.
213 * @param udp_payload entire UDP payload
214 * @param udp_payload_length length of @a udp_payload
215 * @param off pointer to the offset of the name to parse in the udp_payload (to be
216 * incremented by the size of the name)
217 * @param depth current depth of our recursion (to prevent stack overflow)
218 * @return name as 0-terminated C string on success, NULL if the payload is malformed
221 parse_name (const char *udp_payload,
222 size_t udp_payload_length,
226 const uint8_t *input = (const uint8_t *) udp_payload;
235 ret = GNUNET_strdup ("");
238 if (*off >= udp_payload_length)
251 if (*off + 1 + len > udp_payload_length)
256 GNUNET_asprintf (&tmp, "%.*s", (int) len, &udp_payload[*off + 1]);
258 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
260 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
261 _ ("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
265 GNUNET_asprintf (&tmp,
269 &udp_payload[*off + 1]);
274 GNUNET_asprintf (&tmp, "%s%s.", ret, utf8);
285 else if ((64 | 128) == (len & (64 | 128)))
290 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
292 /* pointer to string */
293 if (*off + 1 > udp_payload_length)
298 xoff = ((len - (64 | 128)) << 8) + input[*off + 1];
299 xstr = parse_name (udp_payload, udp_payload_length, &xoff, depth + 1);
305 GNUNET_asprintf (&tmp, "%s%s.", ret, xstr);
309 if (strlen (ret) > udp_payload_length)
312 goto error; /* we are looping (building an infinite string) */
315 /* pointers always terminate names */
320 /* neither pointer nor inline string, not supported... */
325 if (0 < strlen (ret))
326 ret[strlen (ret) - 1] = '\0'; /* eat tailing '.' */
336 * Parse name inside of a DNS query or record.
338 * @param udp_payload entire UDP payload
339 * @param udp_payload_length length of @a udp_payload
340 * @param off pointer to the offset of the name to parse in the udp_payload (to be
341 * incremented by the size of the name)
342 * @return name as 0-terminated C string on success, NULL if the payload is malformed
345 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
346 size_t udp_payload_length,
349 return parse_name (udp_payload, udp_payload_length, off, 0);
354 * Parse a DNS query entry.
356 * @param udp_payload entire UDP payload
357 * @param udp_payload_length length of @a udp_payload
358 * @param off pointer to the offset of the query to parse in the udp_payload (to be
359 * incremented by the size of the query)
360 * @param q where to write the query information
361 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
364 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
365 size_t udp_payload_length,
367 struct GNUNET_DNSPARSER_Query *q)
370 struct GNUNET_TUN_DnsQueryLine ql;
372 name = GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
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);
413 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
415 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
416 if ((NULL == soa->mname) || (NULL == soa->rname) ||
417 (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length))
420 GNUNET_DNSPARSER_free_soa (soa);
424 GNUNET_memcpy (&soa_bin,
426 sizeof (struct GNUNET_TUN_DnsSoaRecord));
427 soa->serial = ntohl (soa_bin.serial);
428 soa->refresh = ntohl (soa_bin.refresh);
429 soa->retry = ntohl (soa_bin.retry);
430 soa->expire = ntohl (soa_bin.expire);
431 soa->minimum_ttl = ntohl (soa_bin.minimum);
432 (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
438 * Parse a DNS MX record.
440 * @param udp_payload reference to UDP packet
441 * @param udp_payload_length length of @a udp_payload
442 * @param off pointer to the offset of the query to parse in the MX record (to be
443 * incremented by the size of the record), unchanged on error
444 * @return the parsed MX record, NULL on error
446 struct GNUNET_DNSPARSER_MxRecord *
447 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
448 size_t udp_payload_length,
451 struct GNUNET_DNSPARSER_MxRecord *mx;
456 if (*off + sizeof (uint16_t) > udp_payload_length)
461 GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
462 (*off) += sizeof (uint16_t);
463 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
464 mx->preference = ntohs (mxpref);
466 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
467 if (NULL == mx->mxhost)
470 GNUNET_DNSPARSER_free_mx (mx);
479 * Parse a DNS SRV record.
481 * @param udp_payload reference to UDP packet
482 * @param udp_payload_length length of @a udp_payload
483 * @param off pointer to the offset of the query to parse in the SRV record (to be
484 * incremented by the size of the record), unchanged on error
485 * @return the parsed SRV record, NULL on error
487 struct GNUNET_DNSPARSER_SrvRecord *
488 GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
489 size_t udp_payload_length,
492 struct GNUNET_DNSPARSER_SrvRecord *srv;
493 struct GNUNET_TUN_DnsSrvRecord srv_bin;
497 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
499 GNUNET_memcpy (&srv_bin,
501 sizeof (struct GNUNET_TUN_DnsSrvRecord));
502 (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
503 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
504 srv->priority = ntohs (srv_bin.prio);
505 srv->weight = ntohs (srv_bin.weight);
506 srv->port = ntohs (srv_bin.port);
508 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
509 if (NULL == srv->target)
511 GNUNET_DNSPARSER_free_srv (srv);
520 * Parse a DNS CERT record.
522 * @param udp_payload reference to UDP packet
523 * @param udp_payload_length length of @a udp_payload
524 * @param off pointer to the offset of the query to parse in the CERT record (to be
525 * incremented by the size of the record), unchanged on error
526 * @return the parsed CERT record, NULL on error
528 struct GNUNET_DNSPARSER_CertRecord *
529 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
530 size_t udp_payload_length,
533 struct GNUNET_DNSPARSER_CertRecord *cert;
534 struct GNUNET_TUN_DnsCertRecord dcert;
536 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
541 GNUNET_memcpy (&dcert,
543 sizeof (struct GNUNET_TUN_DnsCertRecord));
544 (*off) += sizeof (struct GNUNET_TUN_DnsCertRecord);
545 cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
546 cert->cert_type = ntohs (dcert.cert_type);
547 cert->cert_tag = ntohs (dcert.cert_tag);
548 cert->algorithm = dcert.algorithm;
549 cert->certificate_size = udp_payload_length - (*off);
550 cert->certificate_data = GNUNET_malloc (cert->certificate_size);
551 GNUNET_memcpy (cert->certificate_data,
553 cert->certificate_size);
554 (*off) += cert->certificate_size;
560 * Parse a DNS record entry.
562 * @param udp_payload entire UDP payload
563 * @param udp_payload_length length of @a udp_payload
564 * @param off pointer to the offset of the record to parse in the udp_payload (to be
565 * incremented by the size of the record)
566 * @param r where to write the record information
567 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
570 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
571 size_t udp_payload_length,
573 struct GNUNET_DNSPARSER_Record *r)
576 struct GNUNET_TUN_DnsRecordLine rl;
580 name = GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
584 return GNUNET_SYSERR;
587 if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
590 return GNUNET_SYSERR;
592 GNUNET_memcpy (&rl, &udp_payload[*off], sizeof (rl));
593 (*off) += sizeof (rl);
594 r->type = ntohs (rl.type);
595 r->dns_traffic_class = ntohs (rl.dns_traffic_class);
596 r->expiration_time = GNUNET_TIME_relative_to_absolute (
597 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, ntohl (rl.ttl)));
598 data_len = ntohs (rl.data_len);
599 if (*off + data_len > udp_payload_length)
602 return GNUNET_SYSERR;
607 case GNUNET_DNSPARSER_TYPE_NS:
608 case GNUNET_DNSPARSER_TYPE_CNAME:
609 case GNUNET_DNSPARSER_TYPE_DNAME:
610 case GNUNET_DNSPARSER_TYPE_PTR:
612 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
613 if ((NULL == r->data.hostname) || (old_off + data_len != *off))
614 return GNUNET_SYSERR;
616 case GNUNET_DNSPARSER_TYPE_SOA:
618 GNUNET_DNSPARSER_parse_soa (udp_payload, udp_payload_length, off);
619 if ((NULL == r->data.soa) || (old_off + data_len != *off))
622 return GNUNET_SYSERR;
625 case GNUNET_DNSPARSER_TYPE_MX:
627 GNUNET_DNSPARSER_parse_mx (udp_payload, udp_payload_length, off);
628 if ((NULL == r->data.mx) || (old_off + data_len != *off))
631 return GNUNET_SYSERR;
634 case GNUNET_DNSPARSER_TYPE_SRV:
636 GNUNET_DNSPARSER_parse_srv (udp_payload, udp_payload_length, off);
637 if ((NULL == r->data.srv) || (old_off + data_len != *off))
640 return GNUNET_SYSERR;
644 r->data.raw.data = GNUNET_malloc (data_len);
645 r->data.raw.data_len = data_len;
646 GNUNET_memcpy (r->data.raw.data, &udp_payload[*off], data_len);
655 * Parse a UDP payload of a DNS packet in to a nice struct for further
656 * processing and manipulation.
658 * @param udp_payload wire-format of the DNS packet
659 * @param udp_payload_length number of bytes in @a udp_payload
660 * @return NULL on error, otherwise the parsed packet
662 struct GNUNET_DNSPARSER_Packet *
663 GNUNET_DNSPARSER_parse (const char *udp_payload, size_t udp_payload_length)
665 struct GNUNET_DNSPARSER_Packet *p;
666 const struct GNUNET_TUN_DnsHeader *dns;
670 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
672 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
673 off = sizeof (struct GNUNET_TUN_DnsHeader);
674 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
675 p->flags = dns->flags;
677 n = ntohs (dns->query_count);
680 p->queries = GNUNET_new_array (n, struct GNUNET_DNSPARSER_Query);
682 for (unsigned int i = 0; i < n; i++)
683 if (GNUNET_OK != GNUNET_DNSPARSER_parse_query (udp_payload,
689 n = ntohs (dns->answer_rcount);
692 p->answers = GNUNET_new_array (n, struct GNUNET_DNSPARSER_Record);
694 for (unsigned int i = 0; i < n; i++)
695 if (GNUNET_OK != GNUNET_DNSPARSER_parse_record (udp_payload,
701 n = ntohs (dns->authority_rcount);
704 p->authority_records = GNUNET_new_array (n, struct GNUNET_DNSPARSER_Record);
705 p->num_authority_records = n;
706 for (unsigned int i = 0; i < n; i++)
707 if (GNUNET_OK != GNUNET_DNSPARSER_parse_record (udp_payload,
710 &p->authority_records[i]))
713 n = ntohs (dns->additional_rcount);
716 p->additional_records =
717 GNUNET_new_array (n, struct GNUNET_DNSPARSER_Record);
718 p->num_additional_records = n;
719 for (unsigned int i = 0; i < n; i++)
722 GNUNET_DNSPARSER_parse_record (udp_payload,
725 &p->additional_records[i]))
732 GNUNET_DNSPARSER_free_packet (p);
738 * Duplicate (deep-copy) the given DNS record
740 * @param r the record
741 * @return the newly allocated record
743 struct GNUNET_DNSPARSER_Record *
744 GNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r)
746 struct GNUNET_DNSPARSER_Record *dup = GNUNET_memdup (r, sizeof (*r));
748 dup->name = GNUNET_strdup (r->name);
751 case GNUNET_DNSPARSER_TYPE_NS:
752 case GNUNET_DNSPARSER_TYPE_CNAME:
753 case GNUNET_DNSPARSER_TYPE_PTR: {
754 dup->data.hostname = GNUNET_strdup (r->data.hostname);
757 case GNUNET_DNSPARSER_TYPE_SOA: {
758 dup->data.soa = GNUNET_DNSPARSER_duplicate_soa_record (r->data.soa);
761 case GNUNET_DNSPARSER_TYPE_CERT: {
762 dup->data.cert = GNUNET_DNSPARSER_duplicate_cert_record (r->data.cert);
765 case GNUNET_DNSPARSER_TYPE_MX: {
766 dup->data.mx = GNUNET_DNSPARSER_duplicate_mx_record (r->data.mx);
769 case GNUNET_DNSPARSER_TYPE_SRV: {
770 dup->data.srv = GNUNET_DNSPARSER_duplicate_srv_record (r->data.srv);
774 dup->data.raw.data = GNUNET_memdup (r->data.raw.data, r->data.raw.data_len);
782 * Duplicate (deep-copy) the given DNS record
784 * @param r the record
785 * @return the newly allocated record
787 struct GNUNET_DNSPARSER_SoaRecord *
788 GNUNET_DNSPARSER_duplicate_soa_record (
789 const struct GNUNET_DNSPARSER_SoaRecord *r)
791 struct GNUNET_DNSPARSER_SoaRecord *dup = GNUNET_memdup (r, sizeof (*r));
793 dup->mname = GNUNET_strdup (r->mname);
794 dup->rname = GNUNET_strdup (r->rname);
800 * Duplicate (deep-copy) the given DNS record
802 * @param r the record
803 * @return the newly allocated record
805 struct GNUNET_DNSPARSER_CertRecord *
806 GNUNET_DNSPARSER_duplicate_cert_record (
807 const struct GNUNET_DNSPARSER_CertRecord *r)
809 struct GNUNET_DNSPARSER_CertRecord *dup = GNUNET_memdup (r, sizeof (*r));
811 dup->certificate_data = GNUNET_strdup (r->certificate_data);
817 * Duplicate (deep-copy) the given DNS record
819 * @param r the record
820 * @return the newly allocated record
822 struct GNUNET_DNSPARSER_MxRecord *
823 GNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r)
825 struct GNUNET_DNSPARSER_MxRecord *dup = GNUNET_memdup (r, sizeof (*r));
827 dup->mxhost = GNUNET_strdup (r->mxhost);
833 * Duplicate (deep-copy) the given DNS record
835 * @param r the record
836 * @return the newly allocated record
838 struct GNUNET_DNSPARSER_SrvRecord *
839 GNUNET_DNSPARSER_duplicate_srv_record (
840 const struct GNUNET_DNSPARSER_SrvRecord *r)
842 struct GNUNET_DNSPARSER_SrvRecord *dup = GNUNET_memdup (r, sizeof (*r));
844 dup->target = GNUNET_strdup (r->target);
850 * Free memory taken by a packet.
852 * @param p packet to free
855 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
857 for (unsigned int i = 0; i < p->num_queries; i++)
858 GNUNET_free_non_null (p->queries[i].name);
859 GNUNET_free_non_null (p->queries);
860 for (unsigned int i = 0; i < p->num_answers; i++)
861 GNUNET_DNSPARSER_free_record (&p->answers[i]);
862 GNUNET_free_non_null (p->answers);
863 for (unsigned int i = 0; i < p->num_authority_records; i++)
864 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
865 GNUNET_free_non_null (p->authority_records);
866 for (unsigned int i = 0; i < p->num_additional_records; i++)
867 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
868 GNUNET_free_non_null (p->additional_records);
873 /* ********************** DNS packet assembly code **************** */
877 * Add a DNS name to the UDP packet at the given location, converting
878 * the name to IDNA notation as necessary.
880 * @param dst where to write the name (UDP packet)
881 * @param dst_len number of bytes in @a dst
882 * @param off pointer to offset where to write the name (increment by bytes used)
883 * must not be changed if there is an error
884 * @param name name to write
885 * @return #GNUNET_SYSERR if @a name is invalid
886 * #GNUNET_NO if @a name did not fit
887 * #GNUNET_OK if @a name was added to @a dst
890 GNUNET_DNSPARSER_builder_add_name (char *dst,
896 const char *idna_name;
904 return GNUNET_SYSERR;
907 (rc = idna_to_ascii_8z (name, &idna_start, IDNA_ALLOW_UNASSIGNED)))
909 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
911 "Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
916 idna_name = idna_start;
918 if (start + strlen (idna_name) + 2 > dst_len)
923 dot = strchr (idna_name, '.');
925 len = strlen (idna_name);
927 len = dot - idna_name;
928 if ((len >= 64) || (0 == len))
930 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
931 "Invalid DNS name `%s': label with %u characters encountered\n",
934 goto fail; /* label too long or empty */
936 dst[pos++] = (char) (uint8_t) len;
937 GNUNET_memcpy (&dst[pos], idna_name, len);
939 idna_name += len + 1; /* also skip dot */
940 } while (NULL != dot);
941 dst[pos++] = '\0'; /* terminator */
944 idn_free (idna_start);
951 idn_free (idna_start);
960 * Add a DNS query to the UDP packet at the given location.
962 * @param dst where to write the query
963 * @param dst_len number of bytes in @a dst
964 * @param off pointer to offset where to write the query (increment by bytes used)
965 * must not be changed if there is an error
966 * @param query query to write
967 * @return #GNUNET_SYSERR if @a query is invalid
968 * #GNUNET_NO if @a query did not fit
969 * #GNUNET_OK if @a query was added to @a dst
972 GNUNET_DNSPARSER_builder_add_query (char *dst,
975 const struct GNUNET_DNSPARSER_Query *query)
978 struct GNUNET_TUN_DnsQueryLine ql;
980 ret = GNUNET_DNSPARSER_builder_add_name (dst,
983 struct GNUNET_TUN_DnsQueryLine),
986 if (ret != GNUNET_OK)
988 ql.type = htons (query->type);
989 ql.dns_traffic_class = htons (query->dns_traffic_class);
990 GNUNET_memcpy (&dst[*off], &ql, sizeof (ql));
991 (*off) += sizeof (ql);
997 * Add an MX record to the UDP packet at the given location.
999 * @param dst where to write the mx record
1000 * @param dst_len number of bytes in @a dst
1001 * @param off pointer to offset where to write the mx information (increment by bytes used);
1002 * can also change if there was an error
1003 * @param mx mx information to write
1004 * @return #GNUNET_SYSERR if @a mx is invalid
1005 * #GNUNET_NO if @a mx did not fit
1006 * #GNUNET_OK if @a mx was added to @a dst
1009 GNUNET_DNSPARSER_builder_add_mx (char *dst,
1012 const struct GNUNET_DNSPARSER_MxRecord *mx)
1016 if (*off + sizeof (uint16_t) > dst_len)
1018 mxpref = htons (mx->preference);
1019 GNUNET_memcpy (&dst[*off], &mxpref, sizeof (mxpref));
1020 (*off) += sizeof (mxpref);
1021 return GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, mx->mxhost);
1026 * Add a CERT record to the UDP packet at the given location.
1028 * @param dst where to write the CERT record
1029 * @param dst_len number of bytes in @a dst
1030 * @param off pointer to offset where to write the CERT information (increment by bytes used);
1031 * can also change if there was an error
1032 * @param cert CERT information to write
1033 * @return #GNUNET_SYSERR if @a cert is invalid
1034 * #GNUNET_NO if @a cert did not fit
1035 * #GNUNET_OK if @a cert was added to @a dst
1038 GNUNET_DNSPARSER_builder_add_cert (
1042 const struct GNUNET_DNSPARSER_CertRecord *cert)
1044 struct GNUNET_TUN_DnsCertRecord dcert;
1047 #pragma clang diagnostic push
1048 #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
1050 if ((cert->cert_type > UINT16_MAX) || (cert->algorithm > UINT8_MAX))
1053 return GNUNET_SYSERR;
1056 #pragma clang diagnostic pop
1058 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size >
1061 dcert.cert_type = htons ((uint16_t) cert->cert_type);
1062 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
1063 dcert.algorithm = (uint8_t) cert->algorithm;
1064 GNUNET_memcpy (&dst[*off], &dcert, sizeof (dcert));
1065 (*off) += sizeof (dcert);
1066 GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
1067 (*off) += cert->certificate_size;
1073 * Add an SOA record to the UDP packet at the given location.
1075 * @param dst where to write the SOA record
1076 * @param dst_len number of bytes in @a dst
1077 * @param off pointer to offset where to write the SOA information (increment by bytes used)
1078 * can also change if there was an error
1079 * @param soa SOA information to write
1080 * @return #GNUNET_SYSERR if @a soa is invalid
1081 * #GNUNET_NO if @a soa did not fit
1082 * #GNUNET_OK if @a soa was added to @a dst
1085 GNUNET_DNSPARSER_builder_add_soa (char *dst,
1088 const struct GNUNET_DNSPARSER_SoaRecord *soa)
1090 struct GNUNET_TUN_DnsSoaRecord sd;
1095 GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, soa->mname))) ||
1098 GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, soa->rname))))
1100 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1102 sd.serial = htonl (soa->serial);
1103 sd.refresh = htonl (soa->refresh);
1104 sd.retry = htonl (soa->retry);
1105 sd.expire = htonl (soa->expire);
1106 sd.minimum = htonl (soa->minimum_ttl);
1107 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1108 (*off) += sizeof (sd);
1114 * Add an SRV record to the UDP packet at the given location.
1116 * @param dst where to write the SRV record
1117 * @param dst_len number of bytes in @a dst
1118 * @param off pointer to offset where to write the SRV information (increment by bytes used)
1119 * can also change if there was an error
1120 * @param srv SRV information to write
1121 * @return #GNUNET_SYSERR if @a srv is invalid
1122 * #GNUNET_NO if @a srv did not fit
1123 * #GNUNET_OK if @a srv was added to @a dst
1126 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1129 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1131 struct GNUNET_TUN_DnsSrvRecord sd;
1134 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1136 sd.prio = htons (srv->priority);
1137 sd.weight = htons (srv->weight);
1138 sd.port = htons (srv->port);
1139 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1140 (*off) += sizeof (sd);
1143 GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, srv->target)))
1150 * Add a DNS record to the UDP packet at the given location.
1152 * @param dst where to write the query
1153 * @param dst_len number of bytes in @a dst
1154 * @param off pointer to offset where to write the query (increment by bytes used)
1155 * must not be changed if there is an error
1156 * @param record record to write
1157 * @return #GNUNET_SYSERR if @a record is invalid
1158 * #GNUNET_NO if @a record did not fit
1159 * #GNUNET_OK if @a record was added to @a dst
1162 add_record (char *dst,
1165 const struct GNUNET_DNSPARSER_Record *record)
1170 struct GNUNET_TUN_DnsRecordLine rl;
1173 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1176 struct GNUNET_TUN_DnsRecordLine),
1179 if (GNUNET_OK != ret)
1181 /* '*off' is now the position where we will need to write the record line */
1183 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1184 switch (record->type)
1186 case GNUNET_DNSPARSER_TYPE_MX:
1187 ret = GNUNET_DNSPARSER_builder_add_mx (dst, dst_len, &pos, record->data.mx);
1189 case GNUNET_DNSPARSER_TYPE_CERT:
1191 GNUNET_DNSPARSER_builder_add_cert (dst, dst_len, &pos, record->data.cert);
1193 case GNUNET_DNSPARSER_TYPE_SOA:
1195 GNUNET_DNSPARSER_builder_add_soa (dst, dst_len, &pos, record->data.soa);
1197 case GNUNET_DNSPARSER_TYPE_NS:
1198 case GNUNET_DNSPARSER_TYPE_CNAME:
1199 case GNUNET_DNSPARSER_TYPE_PTR:
1200 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1203 record->data.hostname);
1205 case GNUNET_DNSPARSER_TYPE_SRV:
1207 GNUNET_DNSPARSER_builder_add_srv (dst, dst_len, &pos, record->data.srv);
1210 if (pos + record->data.raw.data_len > dst_len)
1215 GNUNET_memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
1216 pos += record->data.raw.data_len;
1220 if (GNUNET_OK != ret)
1226 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1228 /* record data too long */
1232 rl.type = htons (record->type);
1233 rl.dns_traffic_class = htons (record->dns_traffic_class);
1235 GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us /
1236 1000LL / 1000LL); /* in seconds */
1237 rl.data_len = htons (
1238 (uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1239 GNUNET_memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
1246 * Given a DNS packet @a p, generate the corresponding UDP payload.
1247 * Note that we do not attempt to pack the strings with pointers
1248 * as this would complicate the code and this is about being
1249 * simple and secure, not fast, fancy and broken like bind.
1251 * @param p packet to pack
1252 * @param max maximum allowed size for the resulting UDP payload
1253 * @param buf set to a buffer with the packed message
1254 * @param buf_length set to the length of @a buf
1255 * @return #GNUNET_SYSERR if @a p is invalid
1256 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1257 * #GNUNET_OK if @a p was packed completely into @a buf
1260 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1265 struct GNUNET_TUN_DnsHeader dns;
1271 if ((p->num_queries > UINT16_MAX) || (p->num_answers > UINT16_MAX) ||
1272 (p->num_authority_records > UINT16_MAX) ||
1273 (p->num_additional_records > UINT16_MAX))
1274 return GNUNET_SYSERR;
1276 dns.flags = p->flags;
1277 dns.query_count = htons (p->num_queries);
1278 dns.answer_rcount = htons (p->num_answers);
1279 dns.authority_rcount = htons (p->num_authority_records);
1280 dns.additional_rcount = htons (p->num_additional_records);
1282 off = sizeof (struct GNUNET_TUN_DnsHeader);
1284 for (unsigned int i = 0; i < p->num_queries; i++)
1286 ret = GNUNET_DNSPARSER_builder_add_query (tmp,
1290 if (GNUNET_SYSERR == ret)
1291 return GNUNET_SYSERR;
1292 if (GNUNET_NO == ret)
1294 dns.query_count = htons ((uint16_t) (i - 1));
1299 for (unsigned int i = 0; i < p->num_answers; i++)
1301 ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
1302 if (GNUNET_SYSERR == ret)
1303 return GNUNET_SYSERR;
1304 if (GNUNET_NO == ret)
1306 dns.answer_rcount = htons ((uint16_t) (i - 1));
1311 for (unsigned int i = 0; i < p->num_authority_records; i++)
1313 ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
1314 if (GNUNET_SYSERR == ret)
1315 return GNUNET_SYSERR;
1316 if (GNUNET_NO == ret)
1318 dns.authority_rcount = htons ((uint16_t) (i - 1));
1323 for (unsigned int i = 0; i < p->num_additional_records; i++)
1325 ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
1326 if (GNUNET_SYSERR == ret)
1327 return GNUNET_SYSERR;
1328 if (GNUNET_NO == ret)
1330 dns.additional_rcount = htons (i - 1);
1336 if (GNUNET_YES == trc)
1337 dns.flags.message_truncated = 1;
1338 GNUNET_memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
1340 *buf = GNUNET_malloc (off);
1342 GNUNET_memcpy (*buf, tmp, off);
1343 if (GNUNET_YES == trc)
1350 * Convert a block of binary data to HEX.
1352 * @param data binary data to convert
1353 * @param data_size number of bytes in @a data
1354 * @return HEX string (lower case)
1357 GNUNET_DNSPARSER_bin_to_hex (const void *data, size_t data_size)
1361 const uint8_t *idata;
1364 ret = GNUNET_malloc (data_size * 2 + 1);
1365 for (off = 0; off < data_size; off++)
1366 sprintf (&ret[off * 2], "%02x", idata[off]);
1372 * Convert a HEX string to block of binary data.
1374 * @param hex HEX string to convert (may contain mixed case)
1375 * @param data where to write result, must be
1376 * at least `strlen(hex)/2` bytes long
1377 * @return number of bytes written to data
1380 GNUNET_DNSPARSER_hex_to_bin (const char *hex, void *data)
1388 data_size = strlen (hex) / 2;
1391 for (off = 0; off < data_size; off++)
1393 in[0] = tolower ((unsigned char) hex[off * 2]);
1394 in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
1395 if (1 != sscanf (in, "%x", &h))
1397 idata[off] = (uint8_t) h;
1403 /* end of dnsparser.c */