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>
41 #include "gnunet_util_lib.h"
45 * Check if a label in UTF-8 format can be coded into valid IDNA.
46 * This can fail if the ASCII-conversion becomes longer than 63 characters.
48 * @param label label to check (UTF-8 string)
49 * @return #GNUNET_OK if the label can be converted to IDNA,
50 * #GNUNET_SYSERR if the label is not valid for DNS names
53 GNUNET_DNSPARSER_check_label (const char *label)
58 if (NULL != strchr (label, '.'))
59 return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */
60 if (0 == strcmp (label, "@")) /* '@' is reserved for the empty label, see #GNUNET_GNS_EMPTY_LABEL_AT */
62 if (IDNA_SUCCESS != idna_to_ascii_8z (label, &output, IDNA_ALLOW_UNASSIGNED))
64 slen = strlen (output);
66 return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK;
71 * Check if a label in UTF-8 format can be coded into valid IDNA.
72 * This can fail if the ASCII-conversion becomes longer than 253 characters.
74 * @param name name to check (UTF-8 string)
75 * @return #GNUNET_OK if the label can be converted to IDNA,
76 * #GNUNET_SYSERR if the label is not valid for DNS names
79 GNUNET_DNSPARSER_check_name (const char *name)
86 ldup = GNUNET_strdup (name);
87 for (tok = strtok (ldup, "."); NULL != tok; tok = strtok (NULL, "."))
88 if (GNUNET_OK != GNUNET_DNSPARSER_check_label (tok))
94 if (IDNA_SUCCESS != idna_to_ascii_8z (name, &output, IDNA_ALLOW_UNASSIGNED))
96 slen = strlen (output);
98 return (slen > 253) ? GNUNET_SYSERR : GNUNET_OK;
103 * Free SOA information record.
105 * @param soa record to free
108 GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
112 GNUNET_free_non_null (soa->mname);
113 GNUNET_free_non_null (soa->rname);
119 * Free CERT information record.
121 * @param cert record to free
124 GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert)
128 GNUNET_free_non_null (cert->certificate_data);
134 * Free SRV information record.
136 * @param srv record to free
139 GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
143 GNUNET_free_non_null (srv->target);
149 * Free MX information record.
151 * @param mx record to free
154 GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
158 GNUNET_free_non_null (mx->mxhost);
164 * Free the given DNS record.
166 * @param r record to free
169 GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
171 GNUNET_free_non_null (r->name);
174 case GNUNET_DNSPARSER_TYPE_MX:
175 GNUNET_DNSPARSER_free_mx (r->data.mx);
178 case GNUNET_DNSPARSER_TYPE_SOA:
179 GNUNET_DNSPARSER_free_soa (r->data.soa);
182 case GNUNET_DNSPARSER_TYPE_SRV:
183 GNUNET_DNSPARSER_free_srv (r->data.srv);
186 case GNUNET_DNSPARSER_TYPE_CERT:
187 GNUNET_DNSPARSER_free_cert (r->data.cert);
190 case GNUNET_DNSPARSER_TYPE_NS:
191 case GNUNET_DNSPARSER_TYPE_CNAME:
192 case GNUNET_DNSPARSER_TYPE_PTR:
193 GNUNET_free_non_null (r->data.hostname);
197 GNUNET_free_non_null (r->data.raw.data);
204 * Parse name inside of a DNS query or record.
206 * @param udp_payload entire UDP payload
207 * @param udp_payload_length length of @a udp_payload
208 * @param off pointer to the offset of the name to parse in the udp_payload (to be
209 * incremented by the size of the name)
210 * @param depth current depth of our recursion (to prevent stack overflow)
211 * @return name as 0-terminated C string on success, NULL if the payload is malformed
214 parse_name (const char *udp_payload,
215 size_t udp_payload_length,
219 const uint8_t *input = (const uint8_t *) udp_payload;
228 ret = GNUNET_strdup ("");
231 if (*off >= udp_payload_length)
244 if (*off + 1 + len > udp_payload_length)
249 GNUNET_asprintf (&tmp, "%.*s", (int) len, &udp_payload[*off + 1]);
251 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
254 _ ("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
258 GNUNET_asprintf (&tmp,
262 &udp_payload[*off + 1]);
267 GNUNET_asprintf (&tmp, "%s%s.", ret, utf8);
274 else if ((64 | 128) == (len & (64 | 128)))
279 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
281 /* pointer to string */
282 if (*off + 1 > udp_payload_length)
287 xoff = ((len - (64 | 128)) << 8) + input[*off + 1];
288 xstr = parse_name (udp_payload, udp_payload_length, &xoff, depth + 1);
294 GNUNET_asprintf (&tmp, "%s%s.", ret, xstr);
298 if (strlen (ret) > udp_payload_length)
301 goto error; /* we are looping (building an infinite string) */
304 /* pointers always terminate names */
309 /* neither pointer nor inline string, not supported... */
314 if (0 < strlen (ret))
315 ret[strlen (ret) - 1] = '\0'; /* eat tailing '.' */
325 * Parse name inside of a DNS query or record.
327 * @param udp_payload entire UDP payload
328 * @param udp_payload_length length of @a udp_payload
329 * @param off pointer to the offset of the name to parse in the udp_payload (to be
330 * incremented by the size of the name)
331 * @return name as 0-terminated C string on success, NULL if the payload is malformed
334 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
335 size_t udp_payload_length,
338 return parse_name (udp_payload, udp_payload_length, off, 0);
343 * Parse a DNS query entry.
345 * @param udp_payload entire UDP payload
346 * @param udp_payload_length length of @a udp_payload
347 * @param off pointer to the offset of the query to parse in the udp_payload (to be
348 * incremented by the size of the query)
349 * @param q where to write the query information
350 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
353 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
354 size_t udp_payload_length,
356 struct GNUNET_DNSPARSER_Query *q)
359 struct GNUNET_TUN_DnsQueryLine ql;
361 name = GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
365 return GNUNET_SYSERR;
368 if (*off + sizeof(struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
371 return GNUNET_SYSERR;
373 GNUNET_memcpy (&ql, &udp_payload[*off], sizeof(ql));
375 q->type = ntohs (ql.type);
376 q->dns_traffic_class = ntohs (ql.dns_traffic_class);
382 * Parse a DNS SOA record.
384 * @param udp_payload reference to UDP packet
385 * @param udp_payload_length length of @a udp_payload
386 * @param off pointer to the offset of the query to parse in the SOA record (to be
387 * incremented by the size of the record), unchanged on error
388 * @return the parsed SOA record, NULL on error
390 struct GNUNET_DNSPARSER_SoaRecord *
391 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
392 size_t udp_payload_length,
395 struct GNUNET_DNSPARSER_SoaRecord *soa;
396 struct GNUNET_TUN_DnsSoaRecord soa_bin;
400 soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
402 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
404 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
405 if ((NULL == soa->mname) || (NULL == soa->rname) ||
406 (*off + sizeof(struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length))
409 GNUNET_DNSPARSER_free_soa (soa);
413 GNUNET_memcpy (&soa_bin,
415 sizeof(struct GNUNET_TUN_DnsSoaRecord));
416 soa->serial = ntohl (soa_bin.serial);
417 soa->refresh = ntohl (soa_bin.refresh);
418 soa->retry = ntohl (soa_bin.retry);
419 soa->expire = ntohl (soa_bin.expire);
420 soa->minimum_ttl = ntohl (soa_bin.minimum);
421 (*off) += sizeof(struct GNUNET_TUN_DnsSoaRecord);
427 * Parse a DNS MX record.
429 * @param udp_payload reference to UDP packet
430 * @param udp_payload_length length of @a udp_payload
431 * @param off pointer to the offset of the query to parse in the MX record (to be
432 * incremented by the size of the record), unchanged on error
433 * @return the parsed MX record, NULL on error
435 struct GNUNET_DNSPARSER_MxRecord *
436 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
437 size_t udp_payload_length,
440 struct GNUNET_DNSPARSER_MxRecord *mx;
445 if (*off + sizeof(uint16_t) > udp_payload_length)
450 GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof(uint16_t));
451 (*off) += sizeof(uint16_t);
452 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
453 mx->preference = ntohs (mxpref);
455 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
456 if (NULL == mx->mxhost)
459 GNUNET_DNSPARSER_free_mx (mx);
468 * Parse a DNS SRV record.
470 * @param udp_payload reference to UDP packet
471 * @param udp_payload_length length of @a udp_payload
472 * @param off pointer to the offset of the query to parse in the SRV record (to be
473 * incremented by the size of the record), unchanged on error
474 * @return the parsed SRV record, NULL on error
476 struct GNUNET_DNSPARSER_SrvRecord *
477 GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
478 size_t udp_payload_length,
481 struct GNUNET_DNSPARSER_SrvRecord *srv;
482 struct GNUNET_TUN_DnsSrvRecord srv_bin;
486 if (*off + sizeof(struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
488 GNUNET_memcpy (&srv_bin,
490 sizeof(struct GNUNET_TUN_DnsSrvRecord));
491 (*off) += sizeof(struct GNUNET_TUN_DnsSrvRecord);
492 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
493 srv->priority = ntohs (srv_bin.prio);
494 srv->weight = ntohs (srv_bin.weight);
495 srv->port = ntohs (srv_bin.port);
497 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
498 if (NULL == srv->target)
500 GNUNET_DNSPARSER_free_srv (srv);
509 * Parse a DNS CERT record.
511 * @param udp_payload reference to UDP packet
512 * @param udp_payload_length length of @a udp_payload
513 * @param off pointer to the offset of the query to parse in the CERT record (to be
514 * incremented by the size of the record), unchanged on error
515 * @return the parsed CERT record, NULL on error
517 struct GNUNET_DNSPARSER_CertRecord *
518 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
519 size_t udp_payload_length,
522 struct GNUNET_DNSPARSER_CertRecord *cert;
523 struct GNUNET_TUN_DnsCertRecord dcert;
525 if (*off + sizeof(struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
530 GNUNET_memcpy (&dcert,
532 sizeof(struct GNUNET_TUN_DnsCertRecord));
533 (*off) += sizeof(struct GNUNET_TUN_DnsCertRecord);
534 cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
535 cert->cert_type = ntohs (dcert.cert_type);
536 cert->cert_tag = ntohs (dcert.cert_tag);
537 cert->algorithm = dcert.algorithm;
538 cert->certificate_size = udp_payload_length - (*off);
539 cert->certificate_data = GNUNET_malloc (cert->certificate_size);
540 GNUNET_memcpy (cert->certificate_data,
542 cert->certificate_size);
543 (*off) += cert->certificate_size;
549 * Parse a DNS record entry.
551 * @param udp_payload entire UDP payload
552 * @param udp_payload_length length of @a udp_payload
553 * @param off pointer to the offset of the record to parse in the udp_payload (to be
554 * incremented by the size of the record)
555 * @param r where to write the record information
556 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
559 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
560 size_t udp_payload_length,
562 struct GNUNET_DNSPARSER_Record *r)
565 struct GNUNET_TUN_DnsRecordLine rl;
569 name = GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
573 return GNUNET_SYSERR;
576 if (*off + sizeof(struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
579 return GNUNET_SYSERR;
581 GNUNET_memcpy (&rl, &udp_payload[*off], sizeof(rl));
582 (*off) += sizeof(rl);
583 r->type = ntohs (rl.type);
584 r->dns_traffic_class = ntohs (rl.dns_traffic_class);
585 r->expiration_time = GNUNET_TIME_relative_to_absolute (
586 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, ntohl (rl.ttl)));
587 data_len = ntohs (rl.data_len);
588 if (*off + data_len > udp_payload_length)
591 return GNUNET_SYSERR;
596 case GNUNET_DNSPARSER_TYPE_NS:
597 case GNUNET_DNSPARSER_TYPE_CNAME:
598 case GNUNET_DNSPARSER_TYPE_DNAME:
599 case GNUNET_DNSPARSER_TYPE_PTR:
601 GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
602 if ((NULL == r->data.hostname) || (old_off + data_len != *off))
603 return GNUNET_SYSERR;
606 case GNUNET_DNSPARSER_TYPE_SOA:
608 GNUNET_DNSPARSER_parse_soa (udp_payload, udp_payload_length, off);
609 if ((NULL == r->data.soa) || (old_off + data_len != *off))
612 return GNUNET_SYSERR;
616 case GNUNET_DNSPARSER_TYPE_MX:
618 GNUNET_DNSPARSER_parse_mx (udp_payload, udp_payload_length, off);
619 if ((NULL == r->data.mx) || (old_off + data_len != *off))
622 return GNUNET_SYSERR;
626 case GNUNET_DNSPARSER_TYPE_SRV:
628 GNUNET_DNSPARSER_parse_srv (udp_payload, udp_payload_length, off);
629 if ((NULL == r->data.srv) || (old_off + data_len != *off))
632 return GNUNET_SYSERR;
637 r->data.raw.data = GNUNET_malloc (data_len);
638 r->data.raw.data_len = data_len;
639 GNUNET_memcpy (r->data.raw.data, &udp_payload[*off], data_len);
648 * Parse a UDP payload of a DNS packet in to a nice struct for further
649 * processing and manipulation.
651 * @param udp_payload wire-format of the DNS packet
652 * @param udp_payload_length number of bytes in @a udp_payload
653 * @return NULL on error, otherwise the parsed packet
655 struct GNUNET_DNSPARSER_Packet *
656 GNUNET_DNSPARSER_parse (const char *udp_payload, size_t udp_payload_length)
658 struct GNUNET_DNSPARSER_Packet *p;
659 const struct GNUNET_TUN_DnsHeader *dns;
663 if (udp_payload_length < sizeof(struct GNUNET_TUN_DnsHeader))
665 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
666 off = sizeof(struct GNUNET_TUN_DnsHeader);
667 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
668 p->flags = dns->flags;
670 n = ntohs (dns->query_count);
673 p->queries = GNUNET_new_array (n, struct GNUNET_DNSPARSER_Query);
675 for (unsigned int i = 0; i < n; i++)
676 if (GNUNET_OK != GNUNET_DNSPARSER_parse_query (udp_payload,
682 n = ntohs (dns->answer_rcount);
685 p->answers = GNUNET_new_array (n, struct GNUNET_DNSPARSER_Record);
687 for (unsigned int i = 0; i < n; i++)
688 if (GNUNET_OK != GNUNET_DNSPARSER_parse_record (udp_payload,
694 n = ntohs (dns->authority_rcount);
697 p->authority_records = GNUNET_new_array (n, struct GNUNET_DNSPARSER_Record);
698 p->num_authority_records = n;
699 for (unsigned int i = 0; i < n; i++)
700 if (GNUNET_OK != GNUNET_DNSPARSER_parse_record (udp_payload,
703 &p->authority_records[i]))
706 n = ntohs (dns->additional_rcount);
709 p->additional_records =
710 GNUNET_new_array (n, struct GNUNET_DNSPARSER_Record);
711 p->num_additional_records = n;
712 for (unsigned int i = 0; i < n; i++)
715 GNUNET_DNSPARSER_parse_record (udp_payload,
718 &p->additional_records[i]))
725 GNUNET_DNSPARSER_free_packet (p);
731 * Duplicate (deep-copy) the given DNS record
733 * @param r the record
734 * @return the newly allocated record
736 struct GNUNET_DNSPARSER_Record *
737 GNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r)
739 struct GNUNET_DNSPARSER_Record *dup = GNUNET_memdup (r, sizeof(*r));
741 dup->name = GNUNET_strdup (r->name);
744 case GNUNET_DNSPARSER_TYPE_NS:
745 case GNUNET_DNSPARSER_TYPE_CNAME:
746 case GNUNET_DNSPARSER_TYPE_PTR: {
747 dup->data.hostname = GNUNET_strdup (r->data.hostname);
751 case GNUNET_DNSPARSER_TYPE_SOA: {
752 dup->data.soa = GNUNET_DNSPARSER_duplicate_soa_record (r->data.soa);
756 case GNUNET_DNSPARSER_TYPE_CERT: {
757 dup->data.cert = GNUNET_DNSPARSER_duplicate_cert_record (r->data.cert);
761 case GNUNET_DNSPARSER_TYPE_MX: {
762 dup->data.mx = GNUNET_DNSPARSER_duplicate_mx_record (r->data.mx);
766 case GNUNET_DNSPARSER_TYPE_SRV: {
767 dup->data.srv = GNUNET_DNSPARSER_duplicate_srv_record (r->data.srv);
772 dup->data.raw.data = GNUNET_memdup (r->data.raw.data,
773 r->data.raw.data_len);
781 * Duplicate (deep-copy) the given DNS record
783 * @param r the record
784 * @return the newly allocated record
786 struct GNUNET_DNSPARSER_SoaRecord *
787 GNUNET_DNSPARSER_duplicate_soa_record (
788 const struct GNUNET_DNSPARSER_SoaRecord *r)
790 struct GNUNET_DNSPARSER_SoaRecord *dup = GNUNET_memdup (r, sizeof(*r));
792 dup->mname = GNUNET_strdup (r->mname);
793 dup->rname = GNUNET_strdup (r->rname);
799 * Duplicate (deep-copy) the given DNS record
801 * @param r the record
802 * @return the newly allocated record
804 struct GNUNET_DNSPARSER_CertRecord *
805 GNUNET_DNSPARSER_duplicate_cert_record (
806 const struct GNUNET_DNSPARSER_CertRecord *r)
808 struct GNUNET_DNSPARSER_CertRecord *dup = GNUNET_memdup (r, sizeof(*r));
810 dup->certificate_data = GNUNET_strdup (r->certificate_data);
816 * Duplicate (deep-copy) the given DNS record
818 * @param r the record
819 * @return the newly allocated record
821 struct GNUNET_DNSPARSER_MxRecord *
822 GNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r)
824 struct GNUNET_DNSPARSER_MxRecord *dup = GNUNET_memdup (r, sizeof(*r));
826 dup->mxhost = GNUNET_strdup (r->mxhost);
832 * Duplicate (deep-copy) the given DNS record
834 * @param r the record
835 * @return the newly allocated record
837 struct GNUNET_DNSPARSER_SrvRecord *
838 GNUNET_DNSPARSER_duplicate_srv_record (
839 const struct GNUNET_DNSPARSER_SrvRecord *r)
841 struct GNUNET_DNSPARSER_SrvRecord *dup = GNUNET_memdup (r, sizeof(*r));
843 dup->target = GNUNET_strdup (r->target);
849 * Free memory taken by a packet.
851 * @param p packet to free
854 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
856 for (unsigned int i = 0; i < p->num_queries; i++)
857 GNUNET_free_non_null (p->queries[i].name);
858 GNUNET_free_non_null (p->queries);
859 for (unsigned int i = 0; i < p->num_answers; i++)
860 GNUNET_DNSPARSER_free_record (&p->answers[i]);
861 GNUNET_free_non_null (p->answers);
862 for (unsigned int i = 0; i < p->num_authority_records; i++)
863 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
864 GNUNET_free_non_null (p->authority_records);
865 for (unsigned int i = 0; i < p->num_additional_records; i++)
866 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
867 GNUNET_free_non_null (p->additional_records);
872 /* ********************** DNS packet assembly code **************** */
876 * Add a DNS name to the UDP packet at the given location, converting
877 * the name to IDNA notation as necessary.
879 * @param dst where to write the name (UDP packet)
880 * @param dst_len number of bytes in @a dst
881 * @param off pointer to offset where to write the name (increment by bytes used)
882 * must not be changed if there is an error
883 * @param name name to write
884 * @return #GNUNET_SYSERR if @a name is invalid
885 * #GNUNET_NO if @a name did not fit
886 * #GNUNET_OK if @a name was added to @a dst
889 GNUNET_DNSPARSER_builder_add_name (char *dst,
895 const char *idna_name;
903 return GNUNET_SYSERR;
906 (rc = idna_to_ascii_8z (name, &idna_start, IDNA_ALLOW_UNASSIGNED)))
908 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
910 "Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
915 idna_name = idna_start;
917 if (start + strlen (idna_name) + 2 > dst_len)
922 dot = strchr (idna_name, '.');
924 len = strlen (idna_name);
926 len = dot - idna_name;
927 if ((len >= 64) || (0 == len))
929 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
930 "Invalid DNS name `%s': label with %u characters encountered\n",
933 goto fail; /* label too long or empty */
935 dst[pos++] = (char) (uint8_t) len;
936 GNUNET_memcpy (&dst[pos], idna_name, len);
938 idna_name += len + 1; /* also skip dot */
941 dst[pos++] = '\0'; /* terminator */
952 * Add a DNS query to the UDP packet at the given location.
954 * @param dst where to write the query
955 * @param dst_len number of bytes in @a dst
956 * @param off pointer to offset where to write the query (increment by bytes used)
957 * must not be changed if there is an error
958 * @param query query to write
959 * @return #GNUNET_SYSERR if @a query is invalid
960 * #GNUNET_NO if @a query did not fit
961 * #GNUNET_OK if @a query was added to @a dst
964 GNUNET_DNSPARSER_builder_add_query (char *dst,
967 const struct GNUNET_DNSPARSER_Query *query)
970 struct GNUNET_TUN_DnsQueryLine ql;
972 ret = GNUNET_DNSPARSER_builder_add_name (dst,
975 struct GNUNET_TUN_DnsQueryLine),
978 if (ret != GNUNET_OK)
980 ql.type = htons (query->type);
981 ql.dns_traffic_class = htons (query->dns_traffic_class);
982 GNUNET_memcpy (&dst[*off], &ql, sizeof(ql));
983 (*off) += sizeof(ql);
989 * Add an MX record to the UDP packet at the given location.
991 * @param dst where to write the mx record
992 * @param dst_len number of bytes in @a dst
993 * @param off pointer to offset where to write the mx information (increment by bytes used);
994 * can also change if there was an error
995 * @param mx mx information to write
996 * @return #GNUNET_SYSERR if @a mx is invalid
997 * #GNUNET_NO if @a mx did not fit
998 * #GNUNET_OK if @a mx was added to @a dst
1001 GNUNET_DNSPARSER_builder_add_mx (char *dst,
1004 const struct GNUNET_DNSPARSER_MxRecord *mx)
1008 if (*off + sizeof(uint16_t) > dst_len)
1010 mxpref = htons (mx->preference);
1011 GNUNET_memcpy (&dst[*off], &mxpref, sizeof(mxpref));
1012 (*off) += sizeof(mxpref);
1013 return GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, mx->mxhost);
1018 * Add a CERT record to the UDP packet at the given location.
1020 * @param dst where to write the CERT record
1021 * @param dst_len number of bytes in @a dst
1022 * @param off pointer to offset where to write the CERT information (increment by bytes used);
1023 * can also change if there was an error
1024 * @param cert CERT information to write
1025 * @return #GNUNET_SYSERR if @a cert is invalid
1026 * #GNUNET_NO if @a cert did not fit
1027 * #GNUNET_OK if @a cert was added to @a dst
1030 GNUNET_DNSPARSER_builder_add_cert (
1034 const struct GNUNET_DNSPARSER_CertRecord *cert)
1036 struct GNUNET_TUN_DnsCertRecord dcert;
1039 #pragma clang diagnostic push
1040 #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
1042 if ((cert->cert_type > UINT16_MAX) || (cert->algorithm > UINT8_MAX))
1045 return GNUNET_SYSERR;
1048 #pragma clang diagnostic pop
1050 if (*off + sizeof(struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size >
1053 dcert.cert_type = htons ((uint16_t) cert->cert_type);
1054 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
1055 dcert.algorithm = (uint8_t) cert->algorithm;
1056 GNUNET_memcpy (&dst[*off], &dcert, sizeof(dcert));
1057 (*off) += sizeof(dcert);
1058 GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
1059 (*off) += cert->certificate_size;
1065 * Add an SOA record to the UDP packet at the given location.
1067 * @param dst where to write the SOA record
1068 * @param dst_len number of bytes in @a dst
1069 * @param off pointer to offset where to write the SOA information (increment by bytes used)
1070 * can also change if there was an error
1071 * @param soa SOA information to write
1072 * @return #GNUNET_SYSERR if @a soa is invalid
1073 * #GNUNET_NO if @a soa did not fit
1074 * #GNUNET_OK if @a soa was added to @a dst
1077 GNUNET_DNSPARSER_builder_add_soa (char *dst,
1080 const struct GNUNET_DNSPARSER_SoaRecord *soa)
1082 struct GNUNET_TUN_DnsSoaRecord sd;
1087 GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, soa->mname))) ||
1090 GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, soa->rname))))
1092 if (*off + sizeof(struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1094 sd.serial = htonl (soa->serial);
1095 sd.refresh = htonl (soa->refresh);
1096 sd.retry = htonl (soa->retry);
1097 sd.expire = htonl (soa->expire);
1098 sd.minimum = htonl (soa->minimum_ttl);
1099 GNUNET_memcpy (&dst[*off], &sd, sizeof(sd));
1100 (*off) += sizeof(sd);
1106 * Add an SRV record to the UDP packet at the given location.
1108 * @param dst where to write the SRV record
1109 * @param dst_len number of bytes in @a dst
1110 * @param off pointer to offset where to write the SRV information (increment by bytes used)
1111 * can also change if there was an error
1112 * @param srv SRV information to write
1113 * @return #GNUNET_SYSERR if @a srv is invalid
1114 * #GNUNET_NO if @a srv did not fit
1115 * #GNUNET_OK if @a srv was added to @a dst
1118 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1121 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1123 struct GNUNET_TUN_DnsSrvRecord sd;
1126 if (*off + sizeof(struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1128 sd.prio = htons (srv->priority);
1129 sd.weight = htons (srv->weight);
1130 sd.port = htons (srv->port);
1131 GNUNET_memcpy (&dst[*off], &sd, sizeof(sd));
1132 (*off) += sizeof(sd);
1135 GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, srv->target)))
1142 * Add a DNS record to the UDP packet at the given location.
1144 * @param dst where to write the query
1145 * @param dst_len number of bytes in @a dst
1146 * @param off pointer to offset where to write the query (increment by bytes used)
1147 * must not be changed if there is an error
1148 * @param record record to write
1149 * @return #GNUNET_SYSERR if @a record is invalid
1150 * #GNUNET_NO if @a record did not fit
1151 * #GNUNET_OK if @a record was added to @a dst
1154 add_record (char *dst,
1157 const struct GNUNET_DNSPARSER_Record *record)
1162 struct GNUNET_TUN_DnsRecordLine rl;
1165 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1168 struct GNUNET_TUN_DnsRecordLine),
1171 if (GNUNET_OK != ret)
1173 /* '*off' is now the position where we will need to write the record line */
1175 pos = *off + sizeof(struct GNUNET_TUN_DnsRecordLine);
1176 switch (record->type)
1178 case GNUNET_DNSPARSER_TYPE_MX:
1179 ret = GNUNET_DNSPARSER_builder_add_mx (dst, dst_len, &pos, record->data.mx);
1182 case GNUNET_DNSPARSER_TYPE_CERT:
1184 GNUNET_DNSPARSER_builder_add_cert (dst, dst_len, &pos, record->data.cert);
1187 case GNUNET_DNSPARSER_TYPE_SOA:
1189 GNUNET_DNSPARSER_builder_add_soa (dst, dst_len, &pos, record->data.soa);
1192 case GNUNET_DNSPARSER_TYPE_NS:
1193 case GNUNET_DNSPARSER_TYPE_CNAME:
1194 case GNUNET_DNSPARSER_TYPE_PTR:
1195 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1198 record->data.hostname);
1201 case GNUNET_DNSPARSER_TYPE_SRV:
1203 GNUNET_DNSPARSER_builder_add_srv (dst, dst_len, &pos, record->data.srv);
1207 if (pos + record->data.raw.data_len > dst_len)
1212 GNUNET_memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
1213 pos += record->data.raw.data_len;
1217 if (GNUNET_OK != ret)
1223 if (pos - (*off + sizeof(struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1225 /* record data too long */
1229 rl.type = htons (record->type);
1230 rl.dns_traffic_class = htons (record->dns_traffic_class);
1232 GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us
1233 / 1000LL / 1000LL); /* in seconds */
1234 rl.data_len = htons (
1235 (uint16_t) (pos - (*off + sizeof(struct GNUNET_TUN_DnsRecordLine))));
1236 GNUNET_memcpy (&dst[*off], &rl, sizeof(struct GNUNET_TUN_DnsRecordLine));
1243 * Given a DNS packet @a p, generate the corresponding UDP payload.
1244 * Note that we do not attempt to pack the strings with pointers
1245 * as this would complicate the code and this is about being
1246 * simple and secure, not fast, fancy and broken like bind.
1248 * @param p packet to pack
1249 * @param max maximum allowed size for the resulting UDP payload
1250 * @param buf set to a buffer with the packed message
1251 * @param buf_length set to the length of @a buf
1252 * @return #GNUNET_SYSERR if @a p is invalid
1253 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1254 * #GNUNET_OK if @a p was packed completely into @a buf
1257 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1262 struct GNUNET_TUN_DnsHeader dns;
1268 if ((p->num_queries > UINT16_MAX) || (p->num_answers > UINT16_MAX) ||
1269 (p->num_authority_records > UINT16_MAX) ||
1270 (p->num_additional_records > UINT16_MAX))
1271 return GNUNET_SYSERR;
1273 dns.flags = p->flags;
1274 dns.query_count = htons (p->num_queries);
1275 dns.answer_rcount = htons (p->num_answers);
1276 dns.authority_rcount = htons (p->num_authority_records);
1277 dns.additional_rcount = htons (p->num_additional_records);
1279 off = sizeof(struct GNUNET_TUN_DnsHeader);
1281 for (unsigned int i = 0; i < p->num_queries; i++)
1283 ret = GNUNET_DNSPARSER_builder_add_query (tmp,
1287 if (GNUNET_SYSERR == ret)
1288 return GNUNET_SYSERR;
1289 if (GNUNET_NO == ret)
1291 dns.query_count = htons ((uint16_t) (i - 1));
1296 for (unsigned int i = 0; i < p->num_answers; i++)
1298 ret = add_record (tmp, sizeof(tmp), &off, &p->answers[i]);
1299 if (GNUNET_SYSERR == ret)
1300 return GNUNET_SYSERR;
1301 if (GNUNET_NO == ret)
1303 dns.answer_rcount = htons ((uint16_t) (i - 1));
1308 for (unsigned int i = 0; i < p->num_authority_records; i++)
1310 ret = add_record (tmp, sizeof(tmp), &off, &p->authority_records[i]);
1311 if (GNUNET_SYSERR == ret)
1312 return GNUNET_SYSERR;
1313 if (GNUNET_NO == ret)
1315 dns.authority_rcount = htons ((uint16_t) (i - 1));
1320 for (unsigned int i = 0; i < p->num_additional_records; i++)
1322 ret = add_record (tmp, sizeof(tmp), &off, &p->additional_records[i]);
1323 if (GNUNET_SYSERR == ret)
1324 return GNUNET_SYSERR;
1325 if (GNUNET_NO == ret)
1327 dns.additional_rcount = htons (i - 1);
1333 if (GNUNET_YES == trc)
1334 dns.flags.message_truncated = 1;
1335 GNUNET_memcpy (tmp, &dns, sizeof(struct GNUNET_TUN_DnsHeader));
1337 *buf = GNUNET_malloc (off);
1339 GNUNET_memcpy (*buf, tmp, off);
1340 if (GNUNET_YES == trc)
1347 * Convert a block of binary data to HEX.
1349 * @param data binary data to convert
1350 * @param data_size number of bytes in @a data
1351 * @return HEX string (lower case)
1354 GNUNET_DNSPARSER_bin_to_hex (const void *data, size_t data_size)
1358 const uint8_t *idata;
1361 ret = GNUNET_malloc (data_size * 2 + 1);
1362 for (off = 0; off < data_size; off++)
1363 sprintf (&ret[off * 2], "%02x", idata[off]);
1369 * Convert a HEX string to block of binary data.
1371 * @param hex HEX string to convert (may contain mixed case)
1372 * @param data where to write result, must be
1373 * at least `strlen(hex)/2` bytes long
1374 * @return number of bytes written to data
1377 GNUNET_DNSPARSER_hex_to_bin (const char *hex, void *data)
1385 data_size = strlen (hex) / 2;
1388 for (off = 0; off < data_size; off++)
1390 in[0] = tolower ((unsigned char) hex[off * 2]);
1391 in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
1392 if (1 != sscanf (in, "%x", &h))
1394 idata[off] = (uint8_t) h;
1400 /* end of dnsparser.c */