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
30 #elif HAVE_IDN2_IDN2_H
31 #include <idn2/idn2.h>
40 #include "gnunet_util_lib.h"
44 * Check if a label in UTF-8 format can be coded into valid IDNA.
45 * This can fail if the ASCII-conversion becomes longer than 63 characters.
47 * @param label label to check (UTF-8 string)
48 * @return #GNUNET_OK if the label can be converted to IDNA,
49 * #GNUNET_SYSERR if the label is not valid for DNS names
52 GNUNET_DNSPARSER_check_label (const char *label)
57 if (NULL != strchr (label, '.'))
58 return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */
60 idna_to_ascii_8z (label, &output, IDNA_ALLOW_UNASSIGNED))
62 slen = strlen (output);
68 return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK;
73 * Check if a label in UTF-8 format can be coded into valid IDNA.
74 * This can fail if the ASCII-conversion becomes longer than 253 characters.
76 * @param name name to check (UTF-8 string)
77 * @return #GNUNET_OK if the label can be converted to IDNA,
78 * #GNUNET_SYSERR if the label is not valid for DNS names
81 GNUNET_DNSPARSER_check_name (const char *name)
88 ldup = GNUNET_strdup (name);
89 for (tok = strtok (ldup, "."); NULL != tok; tok = strtok (NULL, "."))
91 GNUNET_DNSPARSER_check_label (tok))
98 idna_to_ascii_8z (name, &output, IDNA_ALLOW_UNASSIGNED))
100 slen = strlen (output);
106 return (slen > 253) ? GNUNET_SYSERR : GNUNET_OK;
111 * Free SOA information record.
113 * @param soa record to free
116 GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
120 GNUNET_free_non_null (soa->mname);
121 GNUNET_free_non_null (soa->rname);
127 * Free CERT information record.
129 * @param cert record to free
132 GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert)
136 GNUNET_free_non_null (cert->certificate_data);
142 * Free SRV information record.
144 * @param srv record to free
147 GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
151 GNUNET_free_non_null (srv->target);
157 * Free MX information record.
159 * @param mx record to free
162 GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
166 GNUNET_free_non_null (mx->mxhost);
172 * Free the given DNS record.
174 * @param r record to free
177 GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
179 GNUNET_free_non_null (r->name);
182 case GNUNET_DNSPARSER_TYPE_MX:
183 GNUNET_DNSPARSER_free_mx (r->data.mx);
185 case GNUNET_DNSPARSER_TYPE_SOA:
186 GNUNET_DNSPARSER_free_soa (r->data.soa);
188 case GNUNET_DNSPARSER_TYPE_SRV:
189 GNUNET_DNSPARSER_free_srv (r->data.srv);
191 case GNUNET_DNSPARSER_TYPE_CERT:
192 GNUNET_DNSPARSER_free_cert (r->data.cert);
194 case GNUNET_DNSPARSER_TYPE_NS:
195 case GNUNET_DNSPARSER_TYPE_CNAME:
196 case GNUNET_DNSPARSER_TYPE_PTR:
197 GNUNET_free_non_null (r->data.hostname);
200 GNUNET_free_non_null (r->data.raw.data);
207 * Parse name inside of a DNS query or record.
209 * @param udp_payload entire UDP payload
210 * @param udp_payload_length length of @a udp_payload
211 * @param off pointer to the offset of the name to parse in the udp_payload (to be
212 * incremented by the size of the name)
213 * @param depth current depth of our recursion (to prevent stack overflow)
214 * @return name as 0-terminated C string on success, NULL if the payload is malformed
217 parse_name (const char *udp_payload,
218 size_t udp_payload_length,
222 const uint8_t *input = (const uint8_t *) udp_payload;
231 ret = GNUNET_strdup ("");
234 if (*off >= udp_payload_length)
247 if (*off + 1 + len > udp_payload_length)
252 GNUNET_asprintf (&tmp,
255 &udp_payload[*off + 1]);
257 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
259 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
260 _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
264 GNUNET_asprintf (&tmp,
268 &udp_payload[*off + 1]);
273 GNUNET_asprintf (&tmp,
287 else if ((64 | 128) == (len & (64 | 128)) )
292 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
294 /* pointer to string */
295 if (*off + 1 > udp_payload_length)
300 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
301 xstr = parse_name (udp_payload,
310 GNUNET_asprintf (&tmp,
317 if (strlen (ret) > udp_payload_length)
320 goto error; /* we are looping (building an infinite string) */
323 /* pointers always terminate names */
328 /* neither pointer nor inline string, not supported... */
334 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
344 * Parse name inside of a DNS query or record.
346 * @param udp_payload entire UDP payload
347 * @param udp_payload_length length of @a udp_payload
348 * @param off pointer to the offset of the name to parse in the udp_payload (to be
349 * incremented by the size of the name)
350 * @return name as 0-terminated C string on success, NULL if the payload is malformed
353 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
354 size_t udp_payload_length,
357 return parse_name (udp_payload, udp_payload_length, off, 0);
362 * Parse a DNS query entry.
364 * @param udp_payload entire UDP payload
365 * @param udp_payload_length length of @a udp_payload
366 * @param off pointer to the offset of the query to parse in the udp_payload (to be
367 * incremented by the size of the query)
368 * @param q where to write the query information
369 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
372 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
373 size_t udp_payload_length,
375 struct GNUNET_DNSPARSER_Query *q)
378 struct GNUNET_TUN_DnsQueryLine ql;
380 name = GNUNET_DNSPARSER_parse_name (udp_payload,
386 return GNUNET_SYSERR;
389 if (*off + sizeof (struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
392 return GNUNET_SYSERR;
394 GNUNET_memcpy (&ql, &udp_payload[*off], sizeof (ql));
396 q->type = ntohs (ql.type);
397 q->dns_traffic_class = ntohs (ql.dns_traffic_class);
403 * Parse a DNS SOA record.
405 * @param udp_payload reference to UDP packet
406 * @param udp_payload_length length of @a udp_payload
407 * @param off pointer to the offset of the query to parse in the SOA record (to be
408 * incremented by the size of the record), unchanged on error
409 * @return the parsed SOA record, NULL on error
411 struct GNUNET_DNSPARSER_SoaRecord *
412 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
413 size_t udp_payload_length,
416 struct GNUNET_DNSPARSER_SoaRecord *soa;
417 struct GNUNET_TUN_DnsSoaRecord soa_bin;
421 soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
422 soa->mname = GNUNET_DNSPARSER_parse_name (udp_payload,
425 soa->rname = GNUNET_DNSPARSER_parse_name (udp_payload,
428 if ( (NULL == soa->mname) ||
429 (NULL == soa->rname) ||
430 (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length) )
433 GNUNET_DNSPARSER_free_soa (soa);
437 GNUNET_memcpy (&soa_bin,
439 sizeof (struct GNUNET_TUN_DnsSoaRecord));
440 soa->serial = ntohl (soa_bin.serial);
441 soa->refresh = ntohl (soa_bin.refresh);
442 soa->retry = ntohl (soa_bin.retry);
443 soa->expire = ntohl (soa_bin.expire);
444 soa->minimum_ttl = ntohl (soa_bin.minimum);
445 (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
451 * Parse a DNS MX record.
453 * @param udp_payload reference to UDP packet
454 * @param udp_payload_length length of @a udp_payload
455 * @param off pointer to the offset of the query to parse in the MX record (to be
456 * incremented by the size of the record), unchanged on error
457 * @return the parsed MX record, NULL on error
459 struct GNUNET_DNSPARSER_MxRecord *
460 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
461 size_t udp_payload_length,
464 struct GNUNET_DNSPARSER_MxRecord *mx;
469 if (*off + sizeof (uint16_t) > udp_payload_length)
474 GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
475 (*off) += sizeof (uint16_t);
476 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
477 mx->preference = ntohs (mxpref);
478 mx->mxhost = GNUNET_DNSPARSER_parse_name (udp_payload,
481 if (NULL == mx->mxhost)
484 GNUNET_DNSPARSER_free_mx (mx);
493 * Parse a DNS SRV record.
495 * @param udp_payload reference to UDP packet
496 * @param udp_payload_length length of @a udp_payload
497 * @param off pointer to the offset of the query to parse in the SRV record (to be
498 * incremented by the size of the record), unchanged on error
499 * @return the parsed SRV record, NULL on error
501 struct GNUNET_DNSPARSER_SrvRecord *
502 GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
503 size_t udp_payload_length,
506 struct GNUNET_DNSPARSER_SrvRecord *srv;
507 struct GNUNET_TUN_DnsSrvRecord srv_bin;
511 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
513 GNUNET_memcpy (&srv_bin,
515 sizeof (struct GNUNET_TUN_DnsSrvRecord));
516 (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
517 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
518 srv->priority = ntohs (srv_bin.prio);
519 srv->weight = ntohs (srv_bin.weight);
520 srv->port = ntohs (srv_bin.port);
521 srv->target = GNUNET_DNSPARSER_parse_name (udp_payload,
524 if (NULL == srv->target)
526 GNUNET_DNSPARSER_free_srv (srv);
535 * Parse a DNS CERT record.
537 * @param udp_payload reference to UDP packet
538 * @param udp_payload_length length of @a udp_payload
539 * @param off pointer to the offset of the query to parse in the CERT record (to be
540 * incremented by the size of the record), unchanged on error
541 * @return the parsed CERT record, NULL on error
543 struct GNUNET_DNSPARSER_CertRecord *
544 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
545 size_t udp_payload_length,
548 struct GNUNET_DNSPARSER_CertRecord *cert;
549 struct GNUNET_TUN_DnsCertRecord dcert;
551 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
556 GNUNET_memcpy (&dcert,
558 sizeof (struct GNUNET_TUN_DnsCertRecord));
559 (*off) += sizeof (struct GNUNET_TUN_DnsCertRecord);
560 cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
561 cert->cert_type = ntohs (dcert.cert_type);
562 cert->cert_tag = ntohs (dcert.cert_tag);
563 cert->algorithm = dcert.algorithm;
564 cert->certificate_size = udp_payload_length - (*off);
565 cert->certificate_data = GNUNET_malloc (cert->certificate_size);
566 GNUNET_memcpy (cert->certificate_data,
568 cert->certificate_size);
569 (*off) += cert->certificate_size;
575 * Parse a DNS record entry.
577 * @param udp_payload entire UDP payload
578 * @param udp_payload_length length of @a udp_payload
579 * @param off pointer to the offset of the record to parse in the udp_payload (to be
580 * incremented by the size of the record)
581 * @param r where to write the record information
582 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
585 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
586 size_t udp_payload_length,
588 struct GNUNET_DNSPARSER_Record *r)
591 struct GNUNET_TUN_DnsRecordLine rl;
595 name = GNUNET_DNSPARSER_parse_name (udp_payload,
601 return GNUNET_SYSERR;
604 if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
607 return GNUNET_SYSERR;
609 GNUNET_memcpy (&rl, &udp_payload[*off], sizeof (rl));
610 (*off) += sizeof (rl);
611 r->type = ntohs (rl.type);
612 r->dns_traffic_class = ntohs (rl.dns_traffic_class);
613 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
615 data_len = ntohs (rl.data_len);
616 if (*off + data_len > udp_payload_length)
619 return GNUNET_SYSERR;
624 case GNUNET_DNSPARSER_TYPE_NS:
625 case GNUNET_DNSPARSER_TYPE_CNAME:
626 case GNUNET_DNSPARSER_TYPE_DNAME:
627 case GNUNET_DNSPARSER_TYPE_PTR:
628 r->data.hostname = GNUNET_DNSPARSER_parse_name (udp_payload,
631 if ( (NULL == r->data.hostname) ||
632 (old_off + data_len != *off) )
633 return GNUNET_SYSERR;
635 case GNUNET_DNSPARSER_TYPE_SOA:
636 r->data.soa = GNUNET_DNSPARSER_parse_soa (udp_payload,
639 if ( (NULL == r->data.soa) ||
640 (old_off + data_len != *off) )
643 return GNUNET_SYSERR;
646 case GNUNET_DNSPARSER_TYPE_MX:
647 r->data.mx = GNUNET_DNSPARSER_parse_mx (udp_payload,
650 if ( (NULL == r->data.mx) ||
651 (old_off + data_len != *off) )
654 return GNUNET_SYSERR;
657 case GNUNET_DNSPARSER_TYPE_SRV:
658 r->data.srv = GNUNET_DNSPARSER_parse_srv (udp_payload,
661 if ( (NULL == r->data.srv) ||
662 (old_off + data_len != *off) )
665 return GNUNET_SYSERR;
669 r->data.raw.data = GNUNET_malloc (data_len);
670 r->data.raw.data_len = data_len;
671 GNUNET_memcpy (r->data.raw.data,
682 * Parse a UDP payload of a DNS packet in to a nice struct for further
683 * processing and manipulation.
685 * @param udp_payload wire-format of the DNS packet
686 * @param udp_payload_length number of bytes in @a udp_payload
687 * @return NULL on error, otherwise the parsed packet
689 struct GNUNET_DNSPARSER_Packet *
690 GNUNET_DNSPARSER_parse (const char *udp_payload,
691 size_t udp_payload_length)
693 struct GNUNET_DNSPARSER_Packet *p;
694 const struct GNUNET_TUN_DnsHeader *dns;
698 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
700 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
701 off = sizeof (struct GNUNET_TUN_DnsHeader);
702 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
703 p->flags = dns->flags;
705 n = ntohs (dns->query_count);
708 p->queries = GNUNET_new_array (n,
709 struct GNUNET_DNSPARSER_Query);
711 for (unsigned int i=0;i<n;i++)
713 GNUNET_DNSPARSER_parse_query (udp_payload,
719 n = ntohs (dns->answer_rcount);
722 p->answers = GNUNET_new_array (n,
723 struct GNUNET_DNSPARSER_Record);
725 for (unsigned int i=0;i<n;i++)
727 GNUNET_DNSPARSER_parse_record (udp_payload,
733 n = ntohs (dns->authority_rcount);
736 p->authority_records = GNUNET_new_array (n,
737 struct GNUNET_DNSPARSER_Record);
738 p->num_authority_records = n;
739 for (unsigned int i=0;i<n;i++)
741 GNUNET_DNSPARSER_parse_record (udp_payload,
744 &p->authority_records[i]))
747 n = ntohs (dns->additional_rcount);
750 p->additional_records = GNUNET_new_array (n,
751 struct GNUNET_DNSPARSER_Record);
752 p->num_additional_records = n;
753 for (unsigned int i=0;i<n;i++)
756 GNUNET_DNSPARSER_parse_record (udp_payload,
759 &p->additional_records[i]))
766 GNUNET_DNSPARSER_free_packet (p);
772 * Duplicate (deep-copy) the given DNS record
774 * @param r the record
775 * @return the newly allocated record
777 struct GNUNET_DNSPARSER_Record *
778 GNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r)
780 struct GNUNET_DNSPARSER_Record *dup = GNUNET_memdup (r, sizeof (*r));
782 dup->name = GNUNET_strdup (r->name);
785 case GNUNET_DNSPARSER_TYPE_NS:
786 case GNUNET_DNSPARSER_TYPE_CNAME:
787 case GNUNET_DNSPARSER_TYPE_PTR:
789 dup->data.hostname = GNUNET_strdup (r->data.hostname);
792 case GNUNET_DNSPARSER_TYPE_SOA:
794 dup->data.soa = GNUNET_DNSPARSER_duplicate_soa_record (r->data.soa);
797 case GNUNET_DNSPARSER_TYPE_CERT:
799 dup->data.cert = GNUNET_DNSPARSER_duplicate_cert_record (r->data.cert);
802 case GNUNET_DNSPARSER_TYPE_MX:
804 dup->data.mx = GNUNET_DNSPARSER_duplicate_mx_record (r->data.mx);
807 case GNUNET_DNSPARSER_TYPE_SRV:
809 dup->data.srv = GNUNET_DNSPARSER_duplicate_srv_record (r->data.srv);
814 dup->data.raw.data = GNUNET_memdup (r->data.raw.data,
815 r->data.raw.data_len);
823 * Duplicate (deep-copy) the given DNS record
825 * @param r the record
826 * @return the newly allocated record
828 struct GNUNET_DNSPARSER_SoaRecord *
829 GNUNET_DNSPARSER_duplicate_soa_record (const struct GNUNET_DNSPARSER_SoaRecord *r)
831 struct GNUNET_DNSPARSER_SoaRecord *dup = GNUNET_memdup (r, sizeof (*r));
833 dup->mname = GNUNET_strdup (r->mname);
834 dup->rname = GNUNET_strdup (r->rname);
840 * Duplicate (deep-copy) the given DNS record
842 * @param r the record
843 * @return the newly allocated record
845 struct GNUNET_DNSPARSER_CertRecord *
846 GNUNET_DNSPARSER_duplicate_cert_record (const struct GNUNET_DNSPARSER_CertRecord *r)
848 struct GNUNET_DNSPARSER_CertRecord *dup = GNUNET_memdup (r, sizeof (*r));
850 dup->certificate_data = GNUNET_strdup (r->certificate_data);
856 * Duplicate (deep-copy) the given DNS record
858 * @param r the record
859 * @return the newly allocated record
861 struct GNUNET_DNSPARSER_MxRecord *
862 GNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r)
864 struct GNUNET_DNSPARSER_MxRecord *dup = GNUNET_memdup (r, sizeof (*r));
866 dup->mxhost = GNUNET_strdup (r->mxhost);
872 * Duplicate (deep-copy) the given DNS record
874 * @param r the record
875 * @return the newly allocated record
877 struct GNUNET_DNSPARSER_SrvRecord *
878 GNUNET_DNSPARSER_duplicate_srv_record (const struct GNUNET_DNSPARSER_SrvRecord *r)
880 struct GNUNET_DNSPARSER_SrvRecord *dup = GNUNET_memdup (r, sizeof (*r));
882 dup->target = GNUNET_strdup (r->target);
888 * Free memory taken by a packet.
890 * @param p packet to free
893 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
895 for (unsigned int i=0;i<p->num_queries;i++)
896 GNUNET_free_non_null (p->queries[i].name);
897 GNUNET_free_non_null (p->queries);
898 for (unsigned int i=0;i<p->num_answers;i++)
899 GNUNET_DNSPARSER_free_record (&p->answers[i]);
900 GNUNET_free_non_null (p->answers);
901 for (unsigned int i=0;i<p->num_authority_records;i++)
902 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
903 GNUNET_free_non_null (p->authority_records);
904 for (unsigned int i=0;i<p->num_additional_records;i++)
905 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
906 GNUNET_free_non_null (p->additional_records);
911 /* ********************** DNS packet assembly code **************** */
915 * Add a DNS name to the UDP packet at the given location, converting
916 * the name to IDNA notation as necessary.
918 * @param dst where to write the name (UDP packet)
919 * @param dst_len number of bytes in @a dst
920 * @param off pointer to offset where to write the name (increment by bytes used)
921 * must not be changed if there is an error
922 * @param name name to write
923 * @return #GNUNET_SYSERR if @a name is invalid
924 * #GNUNET_NO if @a name did not fit
925 * #GNUNET_OK if @a name was added to @a dst
928 GNUNET_DNSPARSER_builder_add_name (char *dst,
934 const char *idna_name;
942 return GNUNET_SYSERR;
945 (rc = idna_to_ascii_8z (name,
947 IDNA_ALLOW_UNASSIGNED)))
949 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
950 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
955 idna_name = idna_start;
957 if (start + strlen (idna_name) + 2 > dst_len)
962 dot = strchr (idna_name, '.');
964 len = strlen (idna_name);
966 len = dot - idna_name;
967 if ( (len >= 64) || (0 == len) )
969 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
970 "Invalid DNS name `%s': label with %u characters encountered\n",
973 goto fail; /* label too long or empty */
975 dst[pos++] = (char) (uint8_t) len;
976 GNUNET_memcpy (&dst[pos],
980 idna_name += len + 1; /* also skip dot */
983 dst[pos++] = '\0'; /* terminator */
986 idn_free (idna_start);
993 idn_free (idna_start);
1002 * Add a DNS query to the UDP packet at the given location.
1004 * @param dst where to write the query
1005 * @param dst_len number of bytes in @a dst
1006 * @param off pointer to offset where to write the query (increment by bytes used)
1007 * must not be changed if there is an error
1008 * @param query query to write
1009 * @return #GNUNET_SYSERR if @a query is invalid
1010 * #GNUNET_NO if @a query did not fit
1011 * #GNUNET_OK if @a query was added to @a dst
1014 GNUNET_DNSPARSER_builder_add_query (char *dst,
1017 const struct GNUNET_DNSPARSER_Query *query)
1020 struct GNUNET_TUN_DnsQueryLine ql;
1022 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
1023 if (ret != GNUNET_OK)
1025 ql.type = htons (query->type);
1026 ql.dns_traffic_class = htons (query->dns_traffic_class);
1027 GNUNET_memcpy (&dst[*off], &ql, sizeof (ql));
1028 (*off) += sizeof (ql);
1034 * Add an MX record to the UDP packet at the given location.
1036 * @param dst where to write the mx record
1037 * @param dst_len number of bytes in @a dst
1038 * @param off pointer to offset where to write the mx information (increment by bytes used);
1039 * can also change if there was an error
1040 * @param mx mx information to write
1041 * @return #GNUNET_SYSERR if @a mx is invalid
1042 * #GNUNET_NO if @a mx did not fit
1043 * #GNUNET_OK if @a mx was added to @a dst
1046 GNUNET_DNSPARSER_builder_add_mx (char *dst,
1049 const struct GNUNET_DNSPARSER_MxRecord *mx)
1053 if (*off + sizeof (uint16_t) > dst_len)
1055 mxpref = htons (mx->preference);
1056 GNUNET_memcpy (&dst[*off],
1059 (*off) += sizeof (mxpref);
1060 return GNUNET_DNSPARSER_builder_add_name (dst,
1068 * Add a CERT record to the UDP packet at the given location.
1070 * @param dst where to write the CERT record
1071 * @param dst_len number of bytes in @a dst
1072 * @param off pointer to offset where to write the CERT information (increment by bytes used);
1073 * can also change if there was an error
1074 * @param cert CERT information to write
1075 * @return #GNUNET_SYSERR if @a cert is invalid
1076 * #GNUNET_NO if @a cert did not fit
1077 * #GNUNET_OK if @a cert was added to @a dst
1080 GNUNET_DNSPARSER_builder_add_cert (char *dst,
1083 const struct GNUNET_DNSPARSER_CertRecord *cert)
1085 struct GNUNET_TUN_DnsCertRecord dcert;
1087 if ( (cert->cert_type > UINT16_MAX) ||
1088 (cert->algorithm > UINT8_MAX) )
1091 return GNUNET_SYSERR;
1093 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size > dst_len)
1095 dcert.cert_type = htons ((uint16_t) cert->cert_type);
1096 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
1097 dcert.algorithm = (uint8_t) cert->algorithm;
1098 GNUNET_memcpy (&dst[*off], &dcert, sizeof (dcert));
1099 (*off) += sizeof (dcert);
1100 GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
1101 (*off) += cert->certificate_size;
1107 * Add an SOA record to the UDP packet at the given location.
1109 * @param dst where to write the SOA record
1110 * @param dst_len number of bytes in @a dst
1111 * @param off pointer to offset where to write the SOA information (increment by bytes used)
1112 * can also change if there was an error
1113 * @param soa SOA information to write
1114 * @return #GNUNET_SYSERR if @a soa is invalid
1115 * #GNUNET_NO if @a soa did not fit
1116 * #GNUNET_OK if @a soa was added to @a dst
1119 GNUNET_DNSPARSER_builder_add_soa (char *dst,
1122 const struct GNUNET_DNSPARSER_SoaRecord *soa)
1124 struct GNUNET_TUN_DnsSoaRecord sd;
1127 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1131 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1136 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1138 sd.serial = htonl (soa->serial);
1139 sd.refresh = htonl (soa->refresh);
1140 sd.retry = htonl (soa->retry);
1141 sd.expire = htonl (soa->expire);
1142 sd.minimum = htonl (soa->minimum_ttl);
1143 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1144 (*off) += sizeof (sd);
1150 * Add an SRV record to the UDP packet at the given location.
1152 * @param dst where to write the SRV record
1153 * @param dst_len number of bytes in @a dst
1154 * @param off pointer to offset where to write the SRV information (increment by bytes used)
1155 * can also change if there was an error
1156 * @param srv SRV information to write
1157 * @return #GNUNET_SYSERR if @a srv is invalid
1158 * #GNUNET_NO if @a srv did not fit
1159 * #GNUNET_OK if @a srv was added to @a dst
1162 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1165 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1167 struct GNUNET_TUN_DnsSrvRecord sd;
1170 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1172 sd.prio = htons (srv->priority);
1173 sd.weight = htons (srv->weight);
1174 sd.port = htons (srv->port);
1175 GNUNET_memcpy (&dst[*off],
1178 (*off) += sizeof (sd);
1179 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1189 * Add a DNS record to the UDP packet at the given location.
1191 * @param dst where to write the query
1192 * @param dst_len number of bytes in @a dst
1193 * @param off pointer to offset where to write the query (increment by bytes used)
1194 * must not be changed if there is an error
1195 * @param record record to write
1196 * @return #GNUNET_SYSERR if @a record is invalid
1197 * #GNUNET_NO if @a record did not fit
1198 * #GNUNET_OK if @a record was added to @a dst
1201 add_record (char *dst,
1204 const struct GNUNET_DNSPARSER_Record *record)
1209 struct GNUNET_TUN_DnsRecordLine rl;
1212 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1213 dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine),
1216 if (GNUNET_OK != ret)
1218 /* '*off' is now the position where we will need to write the record line */
1220 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1221 switch (record->type)
1223 case GNUNET_DNSPARSER_TYPE_MX:
1224 ret = GNUNET_DNSPARSER_builder_add_mx (dst,
1229 case GNUNET_DNSPARSER_TYPE_CERT:
1230 ret = GNUNET_DNSPARSER_builder_add_cert (dst,
1235 case GNUNET_DNSPARSER_TYPE_SOA:
1236 ret = GNUNET_DNSPARSER_builder_add_soa (dst,
1241 case GNUNET_DNSPARSER_TYPE_NS:
1242 case GNUNET_DNSPARSER_TYPE_CNAME:
1243 case GNUNET_DNSPARSER_TYPE_PTR:
1244 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1247 record->data.hostname);
1249 case GNUNET_DNSPARSER_TYPE_SRV:
1250 ret = GNUNET_DNSPARSER_builder_add_srv (dst,
1256 if (pos + record->data.raw.data_len > dst_len)
1261 GNUNET_memcpy (&dst[pos],
1262 record->data.raw.data,
1263 record->data.raw.data_len);
1264 pos += record->data.raw.data_len;
1268 if (GNUNET_OK != ret)
1274 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1276 /* record data too long */
1280 rl.type = htons (record->type);
1281 rl.dns_traffic_class = htons (record->dns_traffic_class);
1282 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1283 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1284 GNUNET_memcpy (&dst[*off],
1286 sizeof (struct GNUNET_TUN_DnsRecordLine));
1293 * Given a DNS packet @a p, generate the corresponding UDP payload.
1294 * Note that we do not attempt to pack the strings with pointers
1295 * as this would complicate the code and this is about being
1296 * simple and secure, not fast, fancy and broken like bind.
1298 * @param p packet to pack
1299 * @param max maximum allowed size for the resulting UDP payload
1300 * @param buf set to a buffer with the packed message
1301 * @param buf_length set to the length of @a buf
1302 * @return #GNUNET_SYSERR if @a p is invalid
1303 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1304 * #GNUNET_OK if @a p was packed completely into @a buf
1307 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1312 struct GNUNET_TUN_DnsHeader dns;
1318 if ( (p->num_queries > UINT16_MAX) ||
1319 (p->num_answers > UINT16_MAX) ||
1320 (p->num_authority_records > UINT16_MAX) ||
1321 (p->num_additional_records > UINT16_MAX) )
1322 return GNUNET_SYSERR;
1324 dns.flags = p->flags;
1325 dns.query_count = htons (p->num_queries);
1326 dns.answer_rcount = htons (p->num_answers);
1327 dns.authority_rcount = htons (p->num_authority_records);
1328 dns.additional_rcount = htons (p->num_additional_records);
1330 off = sizeof (struct GNUNET_TUN_DnsHeader);
1332 for (unsigned int i=0;i<p->num_queries;i++)
1334 ret = GNUNET_DNSPARSER_builder_add_query (tmp,
1338 if (GNUNET_SYSERR == ret)
1339 return GNUNET_SYSERR;
1340 if (GNUNET_NO == ret)
1342 dns.query_count = htons ((uint16_t) (i-1));
1347 for (unsigned int i=0;i<p->num_answers;i++)
1349 ret = add_record (tmp,
1353 if (GNUNET_SYSERR == ret)
1354 return GNUNET_SYSERR;
1355 if (GNUNET_NO == ret)
1357 dns.answer_rcount = htons ((uint16_t) (i-1));
1362 for (unsigned int i=0;i<p->num_authority_records;i++)
1364 ret = add_record (tmp,
1367 &p->authority_records[i]);
1368 if (GNUNET_SYSERR == ret)
1369 return GNUNET_SYSERR;
1370 if (GNUNET_NO == ret)
1372 dns.authority_rcount = htons ((uint16_t) (i-1));
1377 for (unsigned int i=0;i<p->num_additional_records;i++)
1379 ret = add_record (tmp,
1382 &p->additional_records[i]);
1383 if (GNUNET_SYSERR == ret)
1384 return GNUNET_SYSERR;
1385 if (GNUNET_NO == ret)
1387 dns.additional_rcount = htons (i-1);
1393 if (GNUNET_YES == trc)
1394 dns.flags.message_truncated = 1;
1397 sizeof (struct GNUNET_TUN_DnsHeader));
1399 *buf = GNUNET_malloc (off);
1401 GNUNET_memcpy (*buf,
1404 if (GNUNET_YES == trc)
1411 * Convert a block of binary data to HEX.
1413 * @param data binary data to convert
1414 * @param data_size number of bytes in @a data
1415 * @return HEX string (lower case)
1418 GNUNET_DNSPARSER_bin_to_hex (const void *data,
1423 const uint8_t *idata;
1426 ret = GNUNET_malloc (data_size * 2 + 1);
1427 for (off = 0; off < data_size; off++)
1428 sprintf (&ret[off * 2],
1436 * Convert a HEX string to block of binary data.
1438 * @param hex HEX string to convert (may contain mixed case)
1439 * @param data where to write result, must be
1440 * at least `strlen(hex)/2` bytes long
1441 * @return number of bytes written to data
1444 GNUNET_DNSPARSER_hex_to_bin (const char *hex,
1453 data_size = strlen (hex) / 2;
1456 for (off = 0; off < data_size; off++)
1458 in[0] = tolower ((unsigned char) hex[off * 2]);
1459 in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
1460 if (1 != sscanf (in, "%x", &h))
1462 idata[off] = (uint8_t) h;
1468 /* end of dnsparser.c */