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/>.
20 * @file util/dnsparser.c
21 * @brief helper library to parse DNS packets.
22 * @author Philipp Toelke
23 * @author Christian Grothoff
28 #elif HAVE_IDN2_IDN2_H
29 #include <idn2/idn2.h>
38 #include "gnunet_util_lib.h"
42 * Check if a label in UTF-8 format can be coded into valid IDNA.
43 * This can fail if the ASCII-conversion becomes longer than 63 characters.
45 * @param label label to check (UTF-8 string)
46 * @return #GNUNET_OK if the label can be converted to IDNA,
47 * #GNUNET_SYSERR if the label is not valid for DNS names
50 GNUNET_DNSPARSER_check_label (const char *label)
55 if (NULL != strchr (label, '.'))
56 return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */
58 idna_to_ascii_8z (label, &output, IDNA_ALLOW_UNASSIGNED))
60 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, "."))
89 GNUNET_DNSPARSER_check_label (tok))
96 idna_to_ascii_8z (name, &output, IDNA_ALLOW_UNASSIGNED))
98 slen = strlen (output);
104 return (slen > 253) ? GNUNET_SYSERR : GNUNET_OK;
109 * Free SOA information record.
111 * @param soa record to free
114 GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
118 GNUNET_free_non_null (soa->mname);
119 GNUNET_free_non_null (soa->rname);
125 * Free CERT information record.
127 * @param cert record to free
130 GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert)
134 GNUNET_free_non_null (cert->certificate_data);
140 * Free SRV information record.
142 * @param srv record to free
145 GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
149 GNUNET_free_non_null (srv->target);
155 * Free MX information record.
157 * @param mx record to free
160 GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
164 GNUNET_free_non_null (mx->mxhost);
170 * Free the given DNS record.
172 * @param r record to free
175 GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
177 GNUNET_free_non_null (r->name);
180 case GNUNET_DNSPARSER_TYPE_MX:
181 GNUNET_DNSPARSER_free_mx (r->data.mx);
183 case GNUNET_DNSPARSER_TYPE_SOA:
184 GNUNET_DNSPARSER_free_soa (r->data.soa);
186 case GNUNET_DNSPARSER_TYPE_SRV:
187 GNUNET_DNSPARSER_free_srv (r->data.srv);
189 case GNUNET_DNSPARSER_TYPE_CERT:
190 GNUNET_DNSPARSER_free_cert (r->data.cert);
192 case GNUNET_DNSPARSER_TYPE_NS:
193 case GNUNET_DNSPARSER_TYPE_CNAME:
194 case GNUNET_DNSPARSER_TYPE_PTR:
195 GNUNET_free_non_null (r->data.hostname);
198 GNUNET_free_non_null (r->data.raw.data);
205 * Parse name inside of a DNS query or record.
207 * @param udp_payload entire UDP payload
208 * @param udp_payload_length length of @a udp_payload
209 * @param off pointer to the offset of the name to parse in the udp_payload (to be
210 * incremented by the size of the name)
211 * @param depth current depth of our recursion (to prevent stack overflow)
212 * @return name as 0-terminated C string on success, NULL if the payload is malformed
215 parse_name (const char *udp_payload,
216 size_t udp_payload_length,
220 const uint8_t *input = (const uint8_t *) udp_payload;
229 ret = GNUNET_strdup ("");
232 if (*off >= udp_payload_length)
245 if (*off + 1 + len > udp_payload_length)
250 GNUNET_asprintf (&tmp,
253 &udp_payload[*off + 1]);
255 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
257 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
258 _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
262 GNUNET_asprintf (&tmp,
266 &udp_payload[*off + 1]);
271 GNUNET_asprintf (&tmp,
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,
308 GNUNET_asprintf (&tmp,
315 if (strlen (ret) > udp_payload_length)
318 goto error; /* we are looping (building an infinite string) */
321 /* pointers always terminate names */
326 /* neither pointer nor inline string, not supported... */
332 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
342 * Parse name inside of a DNS query or record.
344 * @param udp_payload entire UDP payload
345 * @param udp_payload_length length of @a udp_payload
346 * @param off pointer to the offset of the name to parse in the udp_payload (to be
347 * incremented by the size of the name)
348 * @return name as 0-terminated C string on success, NULL if the payload is malformed
351 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
352 size_t udp_payload_length,
355 return parse_name (udp_payload, udp_payload_length, off, 0);
360 * Parse a DNS query entry.
362 * @param udp_payload entire UDP payload
363 * @param udp_payload_length length of @a udp_payload
364 * @param off pointer to the offset of the query to parse in the udp_payload (to be
365 * incremented by the size of the query)
366 * @param q where to write the query information
367 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
370 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
371 size_t udp_payload_length,
373 struct GNUNET_DNSPARSER_Query *q)
376 struct GNUNET_TUN_DnsQueryLine ql;
378 name = GNUNET_DNSPARSER_parse_name (udp_payload,
384 return GNUNET_SYSERR;
387 if (*off + sizeof (struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
390 return GNUNET_SYSERR;
392 GNUNET_memcpy (&ql, &udp_payload[*off], sizeof (ql));
394 q->type = ntohs (ql.type);
395 q->dns_traffic_class = ntohs (ql.dns_traffic_class);
401 * Parse a DNS SOA record.
403 * @param udp_payload reference to UDP packet
404 * @param udp_payload_length length of @a udp_payload
405 * @param off pointer to the offset of the query to parse in the SOA record (to be
406 * incremented by the size of the record), unchanged on error
407 * @return the parsed SOA record, NULL on error
409 struct GNUNET_DNSPARSER_SoaRecord *
410 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
411 size_t udp_payload_length,
414 struct GNUNET_DNSPARSER_SoaRecord *soa;
415 struct GNUNET_TUN_DnsSoaRecord soa_bin;
419 soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
420 soa->mname = GNUNET_DNSPARSER_parse_name (udp_payload,
423 soa->rname = GNUNET_DNSPARSER_parse_name (udp_payload,
426 if ( (NULL == soa->mname) ||
427 (NULL == soa->rname) ||
428 (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length) )
431 GNUNET_DNSPARSER_free_soa (soa);
435 GNUNET_memcpy (&soa_bin,
437 sizeof (struct GNUNET_TUN_DnsSoaRecord));
438 soa->serial = ntohl (soa_bin.serial);
439 soa->refresh = ntohl (soa_bin.refresh);
440 soa->retry = ntohl (soa_bin.retry);
441 soa->expire = ntohl (soa_bin.expire);
442 soa->minimum_ttl = ntohl (soa_bin.minimum);
443 (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
449 * Parse a DNS MX record.
451 * @param udp_payload reference to UDP packet
452 * @param udp_payload_length length of @a udp_payload
453 * @param off pointer to the offset of the query to parse in the MX record (to be
454 * incremented by the size of the record), unchanged on error
455 * @return the parsed MX record, NULL on error
457 struct GNUNET_DNSPARSER_MxRecord *
458 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
459 size_t udp_payload_length,
462 struct GNUNET_DNSPARSER_MxRecord *mx;
467 if (*off + sizeof (uint16_t) > udp_payload_length)
472 GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
473 (*off) += sizeof (uint16_t);
474 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
475 mx->preference = ntohs (mxpref);
476 mx->mxhost = GNUNET_DNSPARSER_parse_name (udp_payload,
479 if (NULL == mx->mxhost)
482 GNUNET_DNSPARSER_free_mx (mx);
491 * Parse a DNS SRV record.
493 * @param udp_payload reference to UDP packet
494 * @param udp_payload_length length of @a udp_payload
495 * @param off pointer to the offset of the query to parse in the SRV record (to be
496 * incremented by the size of the record), unchanged on error
497 * @return the parsed SRV record, NULL on error
499 struct GNUNET_DNSPARSER_SrvRecord *
500 GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
501 size_t udp_payload_length,
504 struct GNUNET_DNSPARSER_SrvRecord *srv;
505 struct GNUNET_TUN_DnsSrvRecord srv_bin;
509 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
511 GNUNET_memcpy (&srv_bin,
513 sizeof (struct GNUNET_TUN_DnsSrvRecord));
514 (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
515 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
516 srv->priority = ntohs (srv_bin.prio);
517 srv->weight = ntohs (srv_bin.weight);
518 srv->port = ntohs (srv_bin.port);
519 srv->target = GNUNET_DNSPARSER_parse_name (udp_payload,
522 if (NULL == srv->target)
524 GNUNET_DNSPARSER_free_srv (srv);
533 * Parse a DNS CERT record.
535 * @param udp_payload reference to UDP packet
536 * @param udp_payload_length length of @a udp_payload
537 * @param off pointer to the offset of the query to parse in the CERT record (to be
538 * incremented by the size of the record), unchanged on error
539 * @return the parsed CERT record, NULL on error
541 struct GNUNET_DNSPARSER_CertRecord *
542 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
543 size_t udp_payload_length,
546 struct GNUNET_DNSPARSER_CertRecord *cert;
547 struct GNUNET_TUN_DnsCertRecord dcert;
549 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
554 GNUNET_memcpy (&dcert,
556 sizeof (struct GNUNET_TUN_DnsCertRecord));
557 (*off) += sizeof (struct GNUNET_TUN_DnsCertRecord);
558 cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
559 cert->cert_type = ntohs (dcert.cert_type);
560 cert->cert_tag = ntohs (dcert.cert_tag);
561 cert->algorithm = dcert.algorithm;
562 cert->certificate_size = udp_payload_length - (*off);
563 cert->certificate_data = GNUNET_malloc (cert->certificate_size);
564 GNUNET_memcpy (cert->certificate_data,
566 cert->certificate_size);
567 (*off) += cert->certificate_size;
573 * Parse a DNS record entry.
575 * @param udp_payload entire UDP payload
576 * @param udp_payload_length length of @a udp_payload
577 * @param off pointer to the offset of the record to parse in the udp_payload (to be
578 * incremented by the size of the record)
579 * @param r where to write the record information
580 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
583 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
584 size_t udp_payload_length,
586 struct GNUNET_DNSPARSER_Record *r)
589 struct GNUNET_TUN_DnsRecordLine rl;
593 name = GNUNET_DNSPARSER_parse_name (udp_payload,
599 return GNUNET_SYSERR;
602 if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
605 return GNUNET_SYSERR;
607 GNUNET_memcpy (&rl, &udp_payload[*off], sizeof (rl));
608 (*off) += sizeof (rl);
609 r->type = ntohs (rl.type);
610 r->dns_traffic_class = ntohs (rl.dns_traffic_class);
611 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
613 data_len = ntohs (rl.data_len);
614 if (*off + data_len > udp_payload_length)
617 return GNUNET_SYSERR;
622 case GNUNET_DNSPARSER_TYPE_NS:
623 case GNUNET_DNSPARSER_TYPE_CNAME:
624 case GNUNET_DNSPARSER_TYPE_DNAME:
625 case GNUNET_DNSPARSER_TYPE_PTR:
626 r->data.hostname = GNUNET_DNSPARSER_parse_name (udp_payload,
629 if ( (NULL == r->data.hostname) ||
630 (old_off + data_len != *off) )
631 return GNUNET_SYSERR;
633 case GNUNET_DNSPARSER_TYPE_SOA:
634 r->data.soa = GNUNET_DNSPARSER_parse_soa (udp_payload,
637 if ( (NULL == r->data.soa) ||
638 (old_off + data_len != *off) )
641 return GNUNET_SYSERR;
644 case GNUNET_DNSPARSER_TYPE_MX:
645 r->data.mx = GNUNET_DNSPARSER_parse_mx (udp_payload,
648 if ( (NULL == r->data.mx) ||
649 (old_off + data_len != *off) )
652 return GNUNET_SYSERR;
655 case GNUNET_DNSPARSER_TYPE_SRV:
656 r->data.srv = GNUNET_DNSPARSER_parse_srv (udp_payload,
659 if ( (NULL == r->data.srv) ||
660 (old_off + data_len != *off) )
663 return GNUNET_SYSERR;
667 r->data.raw.data = GNUNET_malloc (data_len);
668 r->data.raw.data_len = data_len;
669 GNUNET_memcpy (r->data.raw.data,
680 * Parse a UDP payload of a DNS packet in to a nice struct for further
681 * processing and manipulation.
683 * @param udp_payload wire-format of the DNS packet
684 * @param udp_payload_length number of bytes in @a udp_payload
685 * @return NULL on error, otherwise the parsed packet
687 struct GNUNET_DNSPARSER_Packet *
688 GNUNET_DNSPARSER_parse (const char *udp_payload,
689 size_t udp_payload_length)
691 struct GNUNET_DNSPARSER_Packet *p;
692 const struct GNUNET_TUN_DnsHeader *dns;
696 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
698 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
699 off = sizeof (struct GNUNET_TUN_DnsHeader);
700 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
701 p->flags = dns->flags;
703 n = ntohs (dns->query_count);
706 p->queries = GNUNET_new_array (n,
707 struct GNUNET_DNSPARSER_Query);
709 for (unsigned int i=0;i<n;i++)
711 GNUNET_DNSPARSER_parse_query (udp_payload,
717 n = ntohs (dns->answer_rcount);
720 p->answers = GNUNET_new_array (n,
721 struct GNUNET_DNSPARSER_Record);
723 for (unsigned int i=0;i<n;i++)
725 GNUNET_DNSPARSER_parse_record (udp_payload,
731 n = ntohs (dns->authority_rcount);
734 p->authority_records = GNUNET_new_array (n,
735 struct GNUNET_DNSPARSER_Record);
736 p->num_authority_records = n;
737 for (unsigned int i=0;i<n;i++)
739 GNUNET_DNSPARSER_parse_record (udp_payload,
742 &p->authority_records[i]))
745 n = ntohs (dns->additional_rcount);
748 p->additional_records = GNUNET_new_array (n,
749 struct GNUNET_DNSPARSER_Record);
750 p->num_additional_records = n;
751 for (unsigned int i=0;i<n;i++)
754 GNUNET_DNSPARSER_parse_record (udp_payload,
757 &p->additional_records[i]))
764 GNUNET_DNSPARSER_free_packet (p);
770 * Duplicate (deep-copy) the given DNS record
772 * @param r the record
773 * @return the newly allocated record
775 struct GNUNET_DNSPARSER_Record *
776 GNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r)
778 struct GNUNET_DNSPARSER_Record *dup = GNUNET_memdup (r, sizeof (*r));
780 dup->name = GNUNET_strdup (r->name);
783 case GNUNET_DNSPARSER_TYPE_NS:
784 case GNUNET_DNSPARSER_TYPE_CNAME:
785 case GNUNET_DNSPARSER_TYPE_PTR:
787 dup->data.hostname = GNUNET_strdup (r->data.hostname);
790 case GNUNET_DNSPARSER_TYPE_SOA:
792 dup->data.soa = GNUNET_DNSPARSER_duplicate_soa_record (r->data.soa);
795 case GNUNET_DNSPARSER_TYPE_CERT:
797 dup->data.cert = GNUNET_DNSPARSER_duplicate_cert_record (r->data.cert);
800 case GNUNET_DNSPARSER_TYPE_MX:
802 dup->data.mx = GNUNET_DNSPARSER_duplicate_mx_record (r->data.mx);
805 case GNUNET_DNSPARSER_TYPE_SRV:
807 dup->data.srv = GNUNET_DNSPARSER_duplicate_srv_record (r->data.srv);
812 dup->data.raw.data = GNUNET_memdup (r->data.raw.data,
813 r->data.raw.data_len);
821 * Duplicate (deep-copy) the given DNS record
823 * @param r the record
824 * @return the newly allocated record
826 struct GNUNET_DNSPARSER_SoaRecord *
827 GNUNET_DNSPARSER_duplicate_soa_record (const struct GNUNET_DNSPARSER_SoaRecord *r)
829 struct GNUNET_DNSPARSER_SoaRecord *dup = GNUNET_memdup (r, sizeof (*r));
831 dup->mname = GNUNET_strdup (r->mname);
832 dup->rname = GNUNET_strdup (r->rname);
838 * Duplicate (deep-copy) the given DNS record
840 * @param r the record
841 * @return the newly allocated record
843 struct GNUNET_DNSPARSER_CertRecord *
844 GNUNET_DNSPARSER_duplicate_cert_record (const struct GNUNET_DNSPARSER_CertRecord *r)
846 struct GNUNET_DNSPARSER_CertRecord *dup = GNUNET_memdup (r, sizeof (*r));
848 dup->certificate_data = GNUNET_strdup (r->certificate_data);
854 * Duplicate (deep-copy) the given DNS record
856 * @param r the record
857 * @return the newly allocated record
859 struct GNUNET_DNSPARSER_MxRecord *
860 GNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r)
862 struct GNUNET_DNSPARSER_MxRecord *dup = GNUNET_memdup (r, sizeof (*r));
864 dup->mxhost = GNUNET_strdup (r->mxhost);
870 * Duplicate (deep-copy) the given DNS record
872 * @param r the record
873 * @return the newly allocated record
875 struct GNUNET_DNSPARSER_SrvRecord *
876 GNUNET_DNSPARSER_duplicate_srv_record (const struct GNUNET_DNSPARSER_SrvRecord *r)
878 struct GNUNET_DNSPARSER_SrvRecord *dup = GNUNET_memdup (r, sizeof (*r));
880 dup->target = GNUNET_strdup (r->target);
886 * Free memory taken by a packet.
888 * @param p packet to free
891 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
893 for (unsigned int i=0;i<p->num_queries;i++)
894 GNUNET_free_non_null (p->queries[i].name);
895 GNUNET_free_non_null (p->queries);
896 for (unsigned int i=0;i<p->num_answers;i++)
897 GNUNET_DNSPARSER_free_record (&p->answers[i]);
898 GNUNET_free_non_null (p->answers);
899 for (unsigned int i=0;i<p->num_authority_records;i++)
900 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
901 GNUNET_free_non_null (p->authority_records);
902 for (unsigned int i=0;i<p->num_additional_records;i++)
903 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
904 GNUNET_free_non_null (p->additional_records);
909 /* ********************** DNS packet assembly code **************** */
913 * Add a DNS name to the UDP packet at the given location, converting
914 * the name to IDNA notation as necessary.
916 * @param dst where to write the name (UDP packet)
917 * @param dst_len number of bytes in @a dst
918 * @param off pointer to offset where to write the name (increment by bytes used)
919 * must not be changed if there is an error
920 * @param name name to write
921 * @return #GNUNET_SYSERR if @a name is invalid
922 * #GNUNET_NO if @a name did not fit
923 * #GNUNET_OK if @a name was added to @a dst
926 GNUNET_DNSPARSER_builder_add_name (char *dst,
932 const char *idna_name;
940 return GNUNET_SYSERR;
943 (rc = idna_to_ascii_8z (name,
945 IDNA_ALLOW_UNASSIGNED)))
947 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
948 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
953 idna_name = idna_start;
955 if (start + strlen (idna_name) + 2 > dst_len)
960 dot = strchr (idna_name, '.');
962 len = strlen (idna_name);
964 len = dot - idna_name;
965 if ( (len >= 64) || (0 == len) )
967 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
968 "Invalid DNS name `%s': label with %u characters encountered\n",
971 goto fail; /* label too long or empty */
973 dst[pos++] = (char) (uint8_t) len;
974 GNUNET_memcpy (&dst[pos],
978 idna_name += len + 1; /* also skip dot */
981 dst[pos++] = '\0'; /* terminator */
984 idn_free (idna_start);
991 idn_free (idna_start);
1000 * Add a DNS query to the UDP packet at the given location.
1002 * @param dst where to write the query
1003 * @param dst_len number of bytes in @a dst
1004 * @param off pointer to offset where to write the query (increment by bytes used)
1005 * must not be changed if there is an error
1006 * @param query query to write
1007 * @return #GNUNET_SYSERR if @a query is invalid
1008 * #GNUNET_NO if @a query did not fit
1009 * #GNUNET_OK if @a query was added to @a dst
1012 GNUNET_DNSPARSER_builder_add_query (char *dst,
1015 const struct GNUNET_DNSPARSER_Query *query)
1018 struct GNUNET_TUN_DnsQueryLine ql;
1020 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
1021 if (ret != GNUNET_OK)
1023 ql.type = htons (query->type);
1024 ql.dns_traffic_class = htons (query->dns_traffic_class);
1025 GNUNET_memcpy (&dst[*off], &ql, sizeof (ql));
1026 (*off) += sizeof (ql);
1032 * Add an MX record to the UDP packet at the given location.
1034 * @param dst where to write the mx record
1035 * @param dst_len number of bytes in @a dst
1036 * @param off pointer to offset where to write the mx information (increment by bytes used);
1037 * can also change if there was an error
1038 * @param mx mx information to write
1039 * @return #GNUNET_SYSERR if @a mx is invalid
1040 * #GNUNET_NO if @a mx did not fit
1041 * #GNUNET_OK if @a mx was added to @a dst
1044 GNUNET_DNSPARSER_builder_add_mx (char *dst,
1047 const struct GNUNET_DNSPARSER_MxRecord *mx)
1051 if (*off + sizeof (uint16_t) > dst_len)
1053 mxpref = htons (mx->preference);
1054 GNUNET_memcpy (&dst[*off],
1057 (*off) += sizeof (mxpref);
1058 return GNUNET_DNSPARSER_builder_add_name (dst,
1066 * Add a CERT record to the UDP packet at the given location.
1068 * @param dst where to write the CERT record
1069 * @param dst_len number of bytes in @a dst
1070 * @param off pointer to offset where to write the CERT information (increment by bytes used);
1071 * can also change if there was an error
1072 * @param cert CERT information to write
1073 * @return #GNUNET_SYSERR if @a cert is invalid
1074 * #GNUNET_NO if @a cert did not fit
1075 * #GNUNET_OK if @a cert was added to @a dst
1078 GNUNET_DNSPARSER_builder_add_cert (char *dst,
1081 const struct GNUNET_DNSPARSER_CertRecord *cert)
1083 struct GNUNET_TUN_DnsCertRecord dcert;
1085 if ( (cert->cert_type > UINT16_MAX) ||
1086 (cert->algorithm > UINT8_MAX) )
1089 return GNUNET_SYSERR;
1091 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size > dst_len)
1093 dcert.cert_type = htons ((uint16_t) cert->cert_type);
1094 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
1095 dcert.algorithm = (uint8_t) cert->algorithm;
1096 GNUNET_memcpy (&dst[*off], &dcert, sizeof (dcert));
1097 (*off) += sizeof (dcert);
1098 GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
1099 (*off) += cert->certificate_size;
1105 * Add an SOA record to the UDP packet at the given location.
1107 * @param dst where to write the SOA record
1108 * @param dst_len number of bytes in @a dst
1109 * @param off pointer to offset where to write the SOA information (increment by bytes used)
1110 * can also change if there was an error
1111 * @param soa SOA information to write
1112 * @return #GNUNET_SYSERR if @a soa is invalid
1113 * #GNUNET_NO if @a soa did not fit
1114 * #GNUNET_OK if @a soa was added to @a dst
1117 GNUNET_DNSPARSER_builder_add_soa (char *dst,
1120 const struct GNUNET_DNSPARSER_SoaRecord *soa)
1122 struct GNUNET_TUN_DnsSoaRecord sd;
1125 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1129 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1134 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1136 sd.serial = htonl (soa->serial);
1137 sd.refresh = htonl (soa->refresh);
1138 sd.retry = htonl (soa->retry);
1139 sd.expire = htonl (soa->expire);
1140 sd.minimum = htonl (soa->minimum_ttl);
1141 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1142 (*off) += sizeof (sd);
1148 * Add an SRV record to the UDP packet at the given location.
1150 * @param dst where to write the SRV record
1151 * @param dst_len number of bytes in @a dst
1152 * @param off pointer to offset where to write the SRV information (increment by bytes used)
1153 * can also change if there was an error
1154 * @param srv SRV information to write
1155 * @return #GNUNET_SYSERR if @a srv is invalid
1156 * #GNUNET_NO if @a srv did not fit
1157 * #GNUNET_OK if @a srv was added to @a dst
1160 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1163 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1165 struct GNUNET_TUN_DnsSrvRecord sd;
1168 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1170 sd.prio = htons (srv->priority);
1171 sd.weight = htons (srv->weight);
1172 sd.port = htons (srv->port);
1173 GNUNET_memcpy (&dst[*off],
1176 (*off) += sizeof (sd);
1177 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1187 * Add a DNS record to the UDP packet at the given location.
1189 * @param dst where to write the query
1190 * @param dst_len number of bytes in @a dst
1191 * @param off pointer to offset where to write the query (increment by bytes used)
1192 * must not be changed if there is an error
1193 * @param record record to write
1194 * @return #GNUNET_SYSERR if @a record is invalid
1195 * #GNUNET_NO if @a record did not fit
1196 * #GNUNET_OK if @a record was added to @a dst
1199 add_record (char *dst,
1202 const struct GNUNET_DNSPARSER_Record *record)
1207 struct GNUNET_TUN_DnsRecordLine rl;
1210 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1211 dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine),
1214 if (GNUNET_OK != ret)
1216 /* '*off' is now the position where we will need to write the record line */
1218 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1219 switch (record->type)
1221 case GNUNET_DNSPARSER_TYPE_MX:
1222 ret = GNUNET_DNSPARSER_builder_add_mx (dst,
1227 case GNUNET_DNSPARSER_TYPE_CERT:
1228 ret = GNUNET_DNSPARSER_builder_add_cert (dst,
1233 case GNUNET_DNSPARSER_TYPE_SOA:
1234 ret = GNUNET_DNSPARSER_builder_add_soa (dst,
1239 case GNUNET_DNSPARSER_TYPE_NS:
1240 case GNUNET_DNSPARSER_TYPE_CNAME:
1241 case GNUNET_DNSPARSER_TYPE_PTR:
1242 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1245 record->data.hostname);
1247 case GNUNET_DNSPARSER_TYPE_SRV:
1248 ret = GNUNET_DNSPARSER_builder_add_srv (dst,
1254 if (pos + record->data.raw.data_len > dst_len)
1259 GNUNET_memcpy (&dst[pos],
1260 record->data.raw.data,
1261 record->data.raw.data_len);
1262 pos += record->data.raw.data_len;
1266 if (GNUNET_OK != ret)
1272 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1274 /* record data too long */
1278 rl.type = htons (record->type);
1279 rl.dns_traffic_class = htons (record->dns_traffic_class);
1280 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1281 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1282 GNUNET_memcpy (&dst[*off],
1284 sizeof (struct GNUNET_TUN_DnsRecordLine));
1291 * Given a DNS packet @a p, generate the corresponding UDP payload.
1292 * Note that we do not attempt to pack the strings with pointers
1293 * as this would complicate the code and this is about being
1294 * simple and secure, not fast, fancy and broken like bind.
1296 * @param p packet to pack
1297 * @param max maximum allowed size for the resulting UDP payload
1298 * @param buf set to a buffer with the packed message
1299 * @param buf_length set to the length of @a buf
1300 * @return #GNUNET_SYSERR if @a p is invalid
1301 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1302 * #GNUNET_OK if @a p was packed completely into @a buf
1305 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1310 struct GNUNET_TUN_DnsHeader dns;
1316 if ( (p->num_queries > UINT16_MAX) ||
1317 (p->num_answers > UINT16_MAX) ||
1318 (p->num_authority_records > UINT16_MAX) ||
1319 (p->num_additional_records > UINT16_MAX) )
1320 return GNUNET_SYSERR;
1322 dns.flags = p->flags;
1323 dns.query_count = htons (p->num_queries);
1324 dns.answer_rcount = htons (p->num_answers);
1325 dns.authority_rcount = htons (p->num_authority_records);
1326 dns.additional_rcount = htons (p->num_additional_records);
1328 off = sizeof (struct GNUNET_TUN_DnsHeader);
1330 for (unsigned int i=0;i<p->num_queries;i++)
1332 ret = GNUNET_DNSPARSER_builder_add_query (tmp,
1336 if (GNUNET_SYSERR == ret)
1337 return GNUNET_SYSERR;
1338 if (GNUNET_NO == ret)
1340 dns.query_count = htons ((uint16_t) (i-1));
1345 for (unsigned int i=0;i<p->num_answers;i++)
1347 ret = add_record (tmp,
1351 if (GNUNET_SYSERR == ret)
1352 return GNUNET_SYSERR;
1353 if (GNUNET_NO == ret)
1355 dns.answer_rcount = htons ((uint16_t) (i-1));
1360 for (unsigned int i=0;i<p->num_authority_records;i++)
1362 ret = add_record (tmp,
1365 &p->authority_records[i]);
1366 if (GNUNET_SYSERR == ret)
1367 return GNUNET_SYSERR;
1368 if (GNUNET_NO == ret)
1370 dns.authority_rcount = htons ((uint16_t) (i-1));
1375 for (unsigned int i=0;i<p->num_additional_records;i++)
1377 ret = add_record (tmp,
1380 &p->additional_records[i]);
1381 if (GNUNET_SYSERR == ret)
1382 return GNUNET_SYSERR;
1383 if (GNUNET_NO == ret)
1385 dns.additional_rcount = htons (i-1);
1391 if (GNUNET_YES == trc)
1392 dns.flags.message_truncated = 1;
1395 sizeof (struct GNUNET_TUN_DnsHeader));
1397 *buf = GNUNET_malloc (off);
1399 GNUNET_memcpy (*buf,
1402 if (GNUNET_YES == trc)
1409 * Convert a block of binary data to HEX.
1411 * @param data binary data to convert
1412 * @param data_size number of bytes in @a data
1413 * @return HEX string (lower case)
1416 GNUNET_DNSPARSER_bin_to_hex (const void *data,
1421 const uint8_t *idata;
1424 ret = GNUNET_malloc (data_size * 2 + 1);
1425 for (off = 0; off < data_size; off++)
1426 sprintf (&ret[off * 2],
1434 * Convert a HEX string to block of binary data.
1436 * @param hex HEX string to convert (may contain mixed case)
1437 * @param data where to write result, must be
1438 * at least `strlen(hex)/2` bytes long
1439 * @return number of bytes written to data
1442 GNUNET_DNSPARSER_hex_to_bin (const char *hex,
1451 data_size = strlen (hex) / 2;
1454 for (off = 0; off < data_size; off++)
1456 in[0] = tolower ((unsigned char) hex[off * 2]);
1457 in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
1458 if (1 != sscanf (in, "%x", &h))
1460 idata[off] = (uint8_t) h;
1466 /* end of dnsparser.c */