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? */
64 idna_to_ascii_8z (label, &output, IDNA_ALLOW_UNASSIGNED))
66 slen = strlen (output);
72 return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK;
77 * Check if a label in UTF-8 format can be coded into valid IDNA.
78 * This can fail if the ASCII-conversion becomes longer than 253 characters.
80 * @param name name to check (UTF-8 string)
81 * @return #GNUNET_OK if the label can be converted to IDNA,
82 * #GNUNET_SYSERR if the label is not valid for DNS names
85 GNUNET_DNSPARSER_check_name (const char *name)
92 ldup = GNUNET_strdup (name);
93 for (tok = strtok (ldup, "."); NULL != tok; tok = strtok (NULL, "."))
95 GNUNET_DNSPARSER_check_label (tok))
102 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,
259 &udp_payload[*off + 1]);
261 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
263 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
264 _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
268 GNUNET_asprintf (&tmp,
272 &udp_payload[*off + 1]);
277 GNUNET_asprintf (&tmp,
291 else if ((64 | 128) == (len & (64 | 128)) )
296 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
298 /* pointer to string */
299 if (*off + 1 > udp_payload_length)
304 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
305 xstr = parse_name (udp_payload,
314 GNUNET_asprintf (&tmp,
321 if (strlen (ret) > udp_payload_length)
324 goto error; /* we are looping (building an infinite string) */
327 /* pointers always terminate names */
332 /* neither pointer nor inline string, not supported... */
338 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
348 * Parse name inside of a DNS query or record.
350 * @param udp_payload entire UDP payload
351 * @param udp_payload_length length of @a udp_payload
352 * @param off pointer to the offset of the name to parse in the udp_payload (to be
353 * incremented by the size of the name)
354 * @return name as 0-terminated C string on success, NULL if the payload is malformed
357 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
358 size_t udp_payload_length,
361 return parse_name (udp_payload, udp_payload_length, off, 0);
366 * Parse a DNS query entry.
368 * @param udp_payload entire UDP payload
369 * @param udp_payload_length length of @a udp_payload
370 * @param off pointer to the offset of the query to parse in the udp_payload (to be
371 * incremented by the size of the query)
372 * @param q where to write the query information
373 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
376 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
377 size_t udp_payload_length,
379 struct GNUNET_DNSPARSER_Query *q)
382 struct GNUNET_TUN_DnsQueryLine ql;
384 name = GNUNET_DNSPARSER_parse_name (udp_payload,
390 return GNUNET_SYSERR;
393 if (*off + sizeof (struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
396 return GNUNET_SYSERR;
398 GNUNET_memcpy (&ql, &udp_payload[*off], sizeof (ql));
400 q->type = ntohs (ql.type);
401 q->dns_traffic_class = ntohs (ql.dns_traffic_class);
407 * Parse a DNS SOA record.
409 * @param udp_payload reference to UDP packet
410 * @param udp_payload_length length of @a udp_payload
411 * @param off pointer to the offset of the query to parse in the SOA record (to be
412 * incremented by the size of the record), unchanged on error
413 * @return the parsed SOA record, NULL on error
415 struct GNUNET_DNSPARSER_SoaRecord *
416 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
417 size_t udp_payload_length,
420 struct GNUNET_DNSPARSER_SoaRecord *soa;
421 struct GNUNET_TUN_DnsSoaRecord soa_bin;
425 soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
426 soa->mname = GNUNET_DNSPARSER_parse_name (udp_payload,
429 soa->rname = GNUNET_DNSPARSER_parse_name (udp_payload,
432 if ( (NULL == soa->mname) ||
433 (NULL == soa->rname) ||
434 (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length) )
437 GNUNET_DNSPARSER_free_soa (soa);
441 GNUNET_memcpy (&soa_bin,
443 sizeof (struct GNUNET_TUN_DnsSoaRecord));
444 soa->serial = ntohl (soa_bin.serial);
445 soa->refresh = ntohl (soa_bin.refresh);
446 soa->retry = ntohl (soa_bin.retry);
447 soa->expire = ntohl (soa_bin.expire);
448 soa->minimum_ttl = ntohl (soa_bin.minimum);
449 (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
455 * Parse a DNS MX record.
457 * @param udp_payload reference to UDP packet
458 * @param udp_payload_length length of @a udp_payload
459 * @param off pointer to the offset of the query to parse in the MX record (to be
460 * incremented by the size of the record), unchanged on error
461 * @return the parsed MX record, NULL on error
463 struct GNUNET_DNSPARSER_MxRecord *
464 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
465 size_t udp_payload_length,
468 struct GNUNET_DNSPARSER_MxRecord *mx;
473 if (*off + sizeof (uint16_t) > udp_payload_length)
478 GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
479 (*off) += sizeof (uint16_t);
480 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
481 mx->preference = ntohs (mxpref);
482 mx->mxhost = GNUNET_DNSPARSER_parse_name (udp_payload,
485 if (NULL == mx->mxhost)
488 GNUNET_DNSPARSER_free_mx (mx);
497 * Parse a DNS SRV record.
499 * @param udp_payload reference to UDP packet
500 * @param udp_payload_length length of @a udp_payload
501 * @param off pointer to the offset of the query to parse in the SRV record (to be
502 * incremented by the size of the record), unchanged on error
503 * @return the parsed SRV record, NULL on error
505 struct GNUNET_DNSPARSER_SrvRecord *
506 GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
507 size_t udp_payload_length,
510 struct GNUNET_DNSPARSER_SrvRecord *srv;
511 struct GNUNET_TUN_DnsSrvRecord srv_bin;
515 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
517 GNUNET_memcpy (&srv_bin,
519 sizeof (struct GNUNET_TUN_DnsSrvRecord));
520 (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
521 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
522 srv->priority = ntohs (srv_bin.prio);
523 srv->weight = ntohs (srv_bin.weight);
524 srv->port = ntohs (srv_bin.port);
525 srv->target = GNUNET_DNSPARSER_parse_name (udp_payload,
528 if (NULL == srv->target)
530 GNUNET_DNSPARSER_free_srv (srv);
539 * Parse a DNS CERT record.
541 * @param udp_payload reference to UDP packet
542 * @param udp_payload_length length of @a udp_payload
543 * @param off pointer to the offset of the query to parse in the CERT record (to be
544 * incremented by the size of the record), unchanged on error
545 * @return the parsed CERT record, NULL on error
547 struct GNUNET_DNSPARSER_CertRecord *
548 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
549 size_t udp_payload_length,
552 struct GNUNET_DNSPARSER_CertRecord *cert;
553 struct GNUNET_TUN_DnsCertRecord dcert;
555 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
560 GNUNET_memcpy (&dcert,
562 sizeof (struct GNUNET_TUN_DnsCertRecord));
563 (*off) += sizeof (struct GNUNET_TUN_DnsCertRecord);
564 cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
565 cert->cert_type = ntohs (dcert.cert_type);
566 cert->cert_tag = ntohs (dcert.cert_tag);
567 cert->algorithm = dcert.algorithm;
568 cert->certificate_size = udp_payload_length - (*off);
569 cert->certificate_data = GNUNET_malloc (cert->certificate_size);
570 GNUNET_memcpy (cert->certificate_data,
572 cert->certificate_size);
573 (*off) += cert->certificate_size;
579 * Parse a DNS record entry.
581 * @param udp_payload entire UDP payload
582 * @param udp_payload_length length of @a udp_payload
583 * @param off pointer to the offset of the record to parse in the udp_payload (to be
584 * incremented by the size of the record)
585 * @param r where to write the record information
586 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
589 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
590 size_t udp_payload_length,
592 struct GNUNET_DNSPARSER_Record *r)
595 struct GNUNET_TUN_DnsRecordLine rl;
599 name = GNUNET_DNSPARSER_parse_name (udp_payload,
605 return GNUNET_SYSERR;
608 if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
611 return GNUNET_SYSERR;
613 GNUNET_memcpy (&rl, &udp_payload[*off], sizeof (rl));
614 (*off) += sizeof (rl);
615 r->type = ntohs (rl.type);
616 r->dns_traffic_class = ntohs (rl.dns_traffic_class);
617 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
619 data_len = ntohs (rl.data_len);
620 if (*off + data_len > udp_payload_length)
623 return GNUNET_SYSERR;
628 case GNUNET_DNSPARSER_TYPE_NS:
629 case GNUNET_DNSPARSER_TYPE_CNAME:
630 case GNUNET_DNSPARSER_TYPE_DNAME:
631 case GNUNET_DNSPARSER_TYPE_PTR:
632 r->data.hostname = GNUNET_DNSPARSER_parse_name (udp_payload,
635 if ( (NULL == r->data.hostname) ||
636 (old_off + data_len != *off) )
637 return GNUNET_SYSERR;
639 case GNUNET_DNSPARSER_TYPE_SOA:
640 r->data.soa = GNUNET_DNSPARSER_parse_soa (udp_payload,
643 if ( (NULL == r->data.soa) ||
644 (old_off + data_len != *off) )
647 return GNUNET_SYSERR;
650 case GNUNET_DNSPARSER_TYPE_MX:
651 r->data.mx = GNUNET_DNSPARSER_parse_mx (udp_payload,
654 if ( (NULL == r->data.mx) ||
655 (old_off + data_len != *off) )
658 return GNUNET_SYSERR;
661 case GNUNET_DNSPARSER_TYPE_SRV:
662 r->data.srv = GNUNET_DNSPARSER_parse_srv (udp_payload,
665 if ( (NULL == r->data.srv) ||
666 (old_off + data_len != *off) )
669 return GNUNET_SYSERR;
673 r->data.raw.data = GNUNET_malloc (data_len);
674 r->data.raw.data_len = data_len;
675 GNUNET_memcpy (r->data.raw.data,
686 * Parse a UDP payload of a DNS packet in to a nice struct for further
687 * processing and manipulation.
689 * @param udp_payload wire-format of the DNS packet
690 * @param udp_payload_length number of bytes in @a udp_payload
691 * @return NULL on error, otherwise the parsed packet
693 struct GNUNET_DNSPARSER_Packet *
694 GNUNET_DNSPARSER_parse (const char *udp_payload,
695 size_t udp_payload_length)
697 struct GNUNET_DNSPARSER_Packet *p;
698 const struct GNUNET_TUN_DnsHeader *dns;
702 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
704 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
705 off = sizeof (struct GNUNET_TUN_DnsHeader);
706 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
707 p->flags = dns->flags;
709 n = ntohs (dns->query_count);
712 p->queries = GNUNET_new_array (n,
713 struct GNUNET_DNSPARSER_Query);
715 for (unsigned int i=0;i<n;i++)
717 GNUNET_DNSPARSER_parse_query (udp_payload,
723 n = ntohs (dns->answer_rcount);
726 p->answers = GNUNET_new_array (n,
727 struct GNUNET_DNSPARSER_Record);
729 for (unsigned int i=0;i<n;i++)
731 GNUNET_DNSPARSER_parse_record (udp_payload,
737 n = ntohs (dns->authority_rcount);
740 p->authority_records = GNUNET_new_array (n,
741 struct GNUNET_DNSPARSER_Record);
742 p->num_authority_records = n;
743 for (unsigned int i=0;i<n;i++)
745 GNUNET_DNSPARSER_parse_record (udp_payload,
748 &p->authority_records[i]))
751 n = ntohs (dns->additional_rcount);
754 p->additional_records = GNUNET_new_array (n,
755 struct GNUNET_DNSPARSER_Record);
756 p->num_additional_records = n;
757 for (unsigned int i=0;i<n;i++)
760 GNUNET_DNSPARSER_parse_record (udp_payload,
763 &p->additional_records[i]))
770 GNUNET_DNSPARSER_free_packet (p);
776 * Duplicate (deep-copy) the given DNS record
778 * @param r the record
779 * @return the newly allocated record
781 struct GNUNET_DNSPARSER_Record *
782 GNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r)
784 struct GNUNET_DNSPARSER_Record *dup = GNUNET_memdup (r, sizeof (*r));
786 dup->name = GNUNET_strdup (r->name);
789 case GNUNET_DNSPARSER_TYPE_NS:
790 case GNUNET_DNSPARSER_TYPE_CNAME:
791 case GNUNET_DNSPARSER_TYPE_PTR:
793 dup->data.hostname = GNUNET_strdup (r->data.hostname);
796 case GNUNET_DNSPARSER_TYPE_SOA:
798 dup->data.soa = GNUNET_DNSPARSER_duplicate_soa_record (r->data.soa);
801 case GNUNET_DNSPARSER_TYPE_CERT:
803 dup->data.cert = GNUNET_DNSPARSER_duplicate_cert_record (r->data.cert);
806 case GNUNET_DNSPARSER_TYPE_MX:
808 dup->data.mx = GNUNET_DNSPARSER_duplicate_mx_record (r->data.mx);
811 case GNUNET_DNSPARSER_TYPE_SRV:
813 dup->data.srv = GNUNET_DNSPARSER_duplicate_srv_record (r->data.srv);
818 dup->data.raw.data = GNUNET_memdup (r->data.raw.data,
819 r->data.raw.data_len);
827 * Duplicate (deep-copy) the given DNS record
829 * @param r the record
830 * @return the newly allocated record
832 struct GNUNET_DNSPARSER_SoaRecord *
833 GNUNET_DNSPARSER_duplicate_soa_record (const struct GNUNET_DNSPARSER_SoaRecord *r)
835 struct GNUNET_DNSPARSER_SoaRecord *dup = GNUNET_memdup (r, sizeof (*r));
837 dup->mname = GNUNET_strdup (r->mname);
838 dup->rname = GNUNET_strdup (r->rname);
844 * Duplicate (deep-copy) the given DNS record
846 * @param r the record
847 * @return the newly allocated record
849 struct GNUNET_DNSPARSER_CertRecord *
850 GNUNET_DNSPARSER_duplicate_cert_record (const struct GNUNET_DNSPARSER_CertRecord *r)
852 struct GNUNET_DNSPARSER_CertRecord *dup = GNUNET_memdup (r, sizeof (*r));
854 dup->certificate_data = GNUNET_strdup (r->certificate_data);
860 * Duplicate (deep-copy) the given DNS record
862 * @param r the record
863 * @return the newly allocated record
865 struct GNUNET_DNSPARSER_MxRecord *
866 GNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r)
868 struct GNUNET_DNSPARSER_MxRecord *dup = GNUNET_memdup (r, sizeof (*r));
870 dup->mxhost = GNUNET_strdup (r->mxhost);
876 * Duplicate (deep-copy) the given DNS record
878 * @param r the record
879 * @return the newly allocated record
881 struct GNUNET_DNSPARSER_SrvRecord *
882 GNUNET_DNSPARSER_duplicate_srv_record (const struct GNUNET_DNSPARSER_SrvRecord *r)
884 struct GNUNET_DNSPARSER_SrvRecord *dup = GNUNET_memdup (r, sizeof (*r));
886 dup->target = GNUNET_strdup (r->target);
892 * Free memory taken by a packet.
894 * @param p packet to free
897 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
899 for (unsigned int i=0;i<p->num_queries;i++)
900 GNUNET_free_non_null (p->queries[i].name);
901 GNUNET_free_non_null (p->queries);
902 for (unsigned int i=0;i<p->num_answers;i++)
903 GNUNET_DNSPARSER_free_record (&p->answers[i]);
904 GNUNET_free_non_null (p->answers);
905 for (unsigned int i=0;i<p->num_authority_records;i++)
906 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
907 GNUNET_free_non_null (p->authority_records);
908 for (unsigned int i=0;i<p->num_additional_records;i++)
909 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
910 GNUNET_free_non_null (p->additional_records);
915 /* ********************** DNS packet assembly code **************** */
919 * Add a DNS name to the UDP packet at the given location, converting
920 * the name to IDNA notation as necessary.
922 * @param dst where to write the name (UDP packet)
923 * @param dst_len number of bytes in @a dst
924 * @param off pointer to offset where to write the name (increment by bytes used)
925 * must not be changed if there is an error
926 * @param name name to write
927 * @return #GNUNET_SYSERR if @a name is invalid
928 * #GNUNET_NO if @a name did not fit
929 * #GNUNET_OK if @a name was added to @a dst
932 GNUNET_DNSPARSER_builder_add_name (char *dst,
938 const char *idna_name;
946 return GNUNET_SYSERR;
949 (rc = idna_to_ascii_8z (name,
951 IDNA_ALLOW_UNASSIGNED)))
953 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
954 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
959 idna_name = idna_start;
961 if (start + strlen (idna_name) + 2 > dst_len)
966 dot = strchr (idna_name, '.');
968 len = strlen (idna_name);
970 len = dot - idna_name;
971 if ( (len >= 64) || (0 == len) )
973 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
974 "Invalid DNS name `%s': label with %u characters encountered\n",
977 goto fail; /* label too long or empty */
979 dst[pos++] = (char) (uint8_t) len;
980 GNUNET_memcpy (&dst[pos],
984 idna_name += len + 1; /* also skip dot */
987 dst[pos++] = '\0'; /* terminator */
990 idn_free (idna_start);
997 idn_free (idna_start);
1006 * Add a DNS query to the UDP packet at the given location.
1008 * @param dst where to write the query
1009 * @param dst_len number of bytes in @a dst
1010 * @param off pointer to offset where to write the query (increment by bytes used)
1011 * must not be changed if there is an error
1012 * @param query query to write
1013 * @return #GNUNET_SYSERR if @a query is invalid
1014 * #GNUNET_NO if @a query did not fit
1015 * #GNUNET_OK if @a query was added to @a dst
1018 GNUNET_DNSPARSER_builder_add_query (char *dst,
1021 const struct GNUNET_DNSPARSER_Query *query)
1024 struct GNUNET_TUN_DnsQueryLine ql;
1026 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
1027 if (ret != GNUNET_OK)
1029 ql.type = htons (query->type);
1030 ql.dns_traffic_class = htons (query->dns_traffic_class);
1031 GNUNET_memcpy (&dst[*off], &ql, sizeof (ql));
1032 (*off) += sizeof (ql);
1038 * Add an MX record to the UDP packet at the given location.
1040 * @param dst where to write the mx record
1041 * @param dst_len number of bytes in @a dst
1042 * @param off pointer to offset where to write the mx information (increment by bytes used);
1043 * can also change if there was an error
1044 * @param mx mx information to write
1045 * @return #GNUNET_SYSERR if @a mx is invalid
1046 * #GNUNET_NO if @a mx did not fit
1047 * #GNUNET_OK if @a mx was added to @a dst
1050 GNUNET_DNSPARSER_builder_add_mx (char *dst,
1053 const struct GNUNET_DNSPARSER_MxRecord *mx)
1057 if (*off + sizeof (uint16_t) > dst_len)
1059 mxpref = htons (mx->preference);
1060 GNUNET_memcpy (&dst[*off],
1063 (*off) += sizeof (mxpref);
1064 return GNUNET_DNSPARSER_builder_add_name (dst,
1072 * Add a CERT record to the UDP packet at the given location.
1074 * @param dst where to write the CERT record
1075 * @param dst_len number of bytes in @a dst
1076 * @param off pointer to offset where to write the CERT information (increment by bytes used);
1077 * can also change if there was an error
1078 * @param cert CERT information to write
1079 * @return #GNUNET_SYSERR if @a cert is invalid
1080 * #GNUNET_NO if @a cert did not fit
1081 * #GNUNET_OK if @a cert was added to @a dst
1084 GNUNET_DNSPARSER_builder_add_cert (char *dst,
1087 const struct GNUNET_DNSPARSER_CertRecord *cert)
1089 struct GNUNET_TUN_DnsCertRecord dcert;
1092 #pragma clang diagnostic push
1093 #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
1095 if ( (cert->cert_type > UINT16_MAX) ||
1096 (cert->algorithm > UINT8_MAX) )
1099 return GNUNET_SYSERR;
1102 #pragma clang diagnostic pop
1104 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size > dst_len)
1106 dcert.cert_type = htons ((uint16_t) cert->cert_type);
1107 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
1108 dcert.algorithm = (uint8_t) cert->algorithm;
1109 GNUNET_memcpy (&dst[*off], &dcert, sizeof (dcert));
1110 (*off) += sizeof (dcert);
1111 GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
1112 (*off) += cert->certificate_size;
1118 * Add an SOA record to the UDP packet at the given location.
1120 * @param dst where to write the SOA record
1121 * @param dst_len number of bytes in @a dst
1122 * @param off pointer to offset where to write the SOA information (increment by bytes used)
1123 * can also change if there was an error
1124 * @param soa SOA information to write
1125 * @return #GNUNET_SYSERR if @a soa is invalid
1126 * #GNUNET_NO if @a soa did not fit
1127 * #GNUNET_OK if @a soa was added to @a dst
1130 GNUNET_DNSPARSER_builder_add_soa (char *dst,
1133 const struct GNUNET_DNSPARSER_SoaRecord *soa)
1135 struct GNUNET_TUN_DnsSoaRecord sd;
1138 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1142 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1147 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1149 sd.serial = htonl (soa->serial);
1150 sd.refresh = htonl (soa->refresh);
1151 sd.retry = htonl (soa->retry);
1152 sd.expire = htonl (soa->expire);
1153 sd.minimum = htonl (soa->minimum_ttl);
1154 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1155 (*off) += sizeof (sd);
1161 * Add an SRV record to the UDP packet at the given location.
1163 * @param dst where to write the SRV record
1164 * @param dst_len number of bytes in @a dst
1165 * @param off pointer to offset where to write the SRV information (increment by bytes used)
1166 * can also change if there was an error
1167 * @param srv SRV information to write
1168 * @return #GNUNET_SYSERR if @a srv is invalid
1169 * #GNUNET_NO if @a srv did not fit
1170 * #GNUNET_OK if @a srv was added to @a dst
1173 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1176 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1178 struct GNUNET_TUN_DnsSrvRecord sd;
1181 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1183 sd.prio = htons (srv->priority);
1184 sd.weight = htons (srv->weight);
1185 sd.port = htons (srv->port);
1186 GNUNET_memcpy (&dst[*off],
1189 (*off) += sizeof (sd);
1190 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1200 * Add a DNS record to the UDP packet at the given location.
1202 * @param dst where to write the query
1203 * @param dst_len number of bytes in @a dst
1204 * @param off pointer to offset where to write the query (increment by bytes used)
1205 * must not be changed if there is an error
1206 * @param record record to write
1207 * @return #GNUNET_SYSERR if @a record is invalid
1208 * #GNUNET_NO if @a record did not fit
1209 * #GNUNET_OK if @a record was added to @a dst
1212 add_record (char *dst,
1215 const struct GNUNET_DNSPARSER_Record *record)
1220 struct GNUNET_TUN_DnsRecordLine rl;
1223 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1224 dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine),
1227 if (GNUNET_OK != ret)
1229 /* '*off' is now the position where we will need to write the record line */
1231 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1232 switch (record->type)
1234 case GNUNET_DNSPARSER_TYPE_MX:
1235 ret = GNUNET_DNSPARSER_builder_add_mx (dst,
1240 case GNUNET_DNSPARSER_TYPE_CERT:
1241 ret = GNUNET_DNSPARSER_builder_add_cert (dst,
1246 case GNUNET_DNSPARSER_TYPE_SOA:
1247 ret = GNUNET_DNSPARSER_builder_add_soa (dst,
1252 case GNUNET_DNSPARSER_TYPE_NS:
1253 case GNUNET_DNSPARSER_TYPE_CNAME:
1254 case GNUNET_DNSPARSER_TYPE_PTR:
1255 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1258 record->data.hostname);
1260 case GNUNET_DNSPARSER_TYPE_SRV:
1261 ret = GNUNET_DNSPARSER_builder_add_srv (dst,
1267 if (pos + record->data.raw.data_len > dst_len)
1272 GNUNET_memcpy (&dst[pos],
1273 record->data.raw.data,
1274 record->data.raw.data_len);
1275 pos += record->data.raw.data_len;
1279 if (GNUNET_OK != ret)
1285 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1287 /* record data too long */
1291 rl.type = htons (record->type);
1292 rl.dns_traffic_class = htons (record->dns_traffic_class);
1293 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1294 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1295 GNUNET_memcpy (&dst[*off],
1297 sizeof (struct GNUNET_TUN_DnsRecordLine));
1304 * Given a DNS packet @a p, generate the corresponding UDP payload.
1305 * Note that we do not attempt to pack the strings with pointers
1306 * as this would complicate the code and this is about being
1307 * simple and secure, not fast, fancy and broken like bind.
1309 * @param p packet to pack
1310 * @param max maximum allowed size for the resulting UDP payload
1311 * @param buf set to a buffer with the packed message
1312 * @param buf_length set to the length of @a buf
1313 * @return #GNUNET_SYSERR if @a p is invalid
1314 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1315 * #GNUNET_OK if @a p was packed completely into @a buf
1318 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1323 struct GNUNET_TUN_DnsHeader dns;
1329 if ( (p->num_queries > UINT16_MAX) ||
1330 (p->num_answers > UINT16_MAX) ||
1331 (p->num_authority_records > UINT16_MAX) ||
1332 (p->num_additional_records > UINT16_MAX) )
1333 return GNUNET_SYSERR;
1335 dns.flags = p->flags;
1336 dns.query_count = htons (p->num_queries);
1337 dns.answer_rcount = htons (p->num_answers);
1338 dns.authority_rcount = htons (p->num_authority_records);
1339 dns.additional_rcount = htons (p->num_additional_records);
1341 off = sizeof (struct GNUNET_TUN_DnsHeader);
1343 for (unsigned int i=0;i<p->num_queries;i++)
1345 ret = GNUNET_DNSPARSER_builder_add_query (tmp,
1349 if (GNUNET_SYSERR == ret)
1350 return GNUNET_SYSERR;
1351 if (GNUNET_NO == ret)
1353 dns.query_count = htons ((uint16_t) (i-1));
1358 for (unsigned int i=0;i<p->num_answers;i++)
1360 ret = add_record (tmp,
1364 if (GNUNET_SYSERR == ret)
1365 return GNUNET_SYSERR;
1366 if (GNUNET_NO == ret)
1368 dns.answer_rcount = htons ((uint16_t) (i-1));
1373 for (unsigned int i=0;i<p->num_authority_records;i++)
1375 ret = add_record (tmp,
1378 &p->authority_records[i]);
1379 if (GNUNET_SYSERR == ret)
1380 return GNUNET_SYSERR;
1381 if (GNUNET_NO == ret)
1383 dns.authority_rcount = htons ((uint16_t) (i-1));
1388 for (unsigned int i=0;i<p->num_additional_records;i++)
1390 ret = add_record (tmp,
1393 &p->additional_records[i]);
1394 if (GNUNET_SYSERR == ret)
1395 return GNUNET_SYSERR;
1396 if (GNUNET_NO == ret)
1398 dns.additional_rcount = htons (i-1);
1404 if (GNUNET_YES == trc)
1405 dns.flags.message_truncated = 1;
1408 sizeof (struct GNUNET_TUN_DnsHeader));
1410 *buf = GNUNET_malloc (off);
1412 GNUNET_memcpy (*buf,
1415 if (GNUNET_YES == trc)
1422 * Convert a block of binary data to HEX.
1424 * @param data binary data to convert
1425 * @param data_size number of bytes in @a data
1426 * @return HEX string (lower case)
1429 GNUNET_DNSPARSER_bin_to_hex (const void *data,
1434 const uint8_t *idata;
1437 ret = GNUNET_malloc (data_size * 2 + 1);
1438 for (off = 0; off < data_size; off++)
1439 sprintf (&ret[off * 2],
1447 * Convert a HEX string to block of binary data.
1449 * @param hex HEX string to convert (may contain mixed case)
1450 * @param data where to write result, must be
1451 * at least `strlen(hex)/2` bytes long
1452 * @return number of bytes written to data
1455 GNUNET_DNSPARSER_hex_to_bin (const char *hex,
1464 data_size = strlen (hex) / 2;
1467 for (off = 0; off < data_size; off++)
1469 in[0] = tolower ((unsigned char) hex[off * 2]);
1470 in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
1471 if (1 != sscanf (in, "%x", &h))
1473 idata[off] = (uint8_t) h;
1479 /* end of dnsparser.c */