2 This file is part of GNUnet
3 Copyright (C) 2010-2014 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 dns/dnsparser.c
21 * @brief helper library to parse DNS packets.
22 * @author Philipp Toelke
23 * @author Christian Grothoff
30 #include "gnunet_util_lib.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_tun_lib.h"
36 * Check if a label in UTF-8 format can be coded into valid IDNA.
37 * This can fail if the ASCII-conversion becomes longer than 63 characters.
39 * @param label label to check (UTF-8 string)
40 * @return #GNUNET_OK if the label can be converted to IDNA,
41 * #GNUNET_SYSERR if the label is not valid for DNS names
44 GNUNET_DNSPARSER_check_label (const char *label)
49 if (NULL != strchr (label, '.'))
50 return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */
52 idna_to_ascii_8z (label, &output, IDNA_ALLOW_UNASSIGNED))
54 slen = strlen (output);
60 return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK;
65 * Check if a label in UTF-8 format can be coded into valid IDNA.
66 * This can fail if the ASCII-conversion becomes longer than 253 characters.
68 * @param name name to check (UTF-8 string)
69 * @return #GNUNET_OK if the label can be converted to IDNA,
70 * #GNUNET_SYSERR if the label is not valid for DNS names
73 GNUNET_DNSPARSER_check_name (const char *name)
80 ldup = GNUNET_strdup (name);
81 for (tok = strtok (ldup, "."); NULL != tok; tok = strtok (NULL, "."))
83 GNUNET_DNSPARSER_check_label (tok))
90 idna_to_ascii_8z (name, &output, IDNA_ALLOW_UNASSIGNED))
92 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);
177 case GNUNET_DNSPARSER_TYPE_SOA:
178 GNUNET_DNSPARSER_free_soa (r->data.soa);
180 case GNUNET_DNSPARSER_TYPE_SRV:
181 GNUNET_DNSPARSER_free_srv (r->data.srv);
183 case GNUNET_DNSPARSER_TYPE_CERT:
184 GNUNET_DNSPARSER_free_cert (r->data.cert);
186 case GNUNET_DNSPARSER_TYPE_NS:
187 case GNUNET_DNSPARSER_TYPE_CNAME:
188 case GNUNET_DNSPARSER_TYPE_PTR:
189 GNUNET_free_non_null (r->data.hostname);
192 GNUNET_free_non_null (r->data.raw.data);
199 * Parse name inside of a DNS query or record.
201 * @param udp_payload entire UDP payload
202 * @param udp_payload_length length of @a udp_payload
203 * @param off pointer to the offset of the name to parse in the udp_payload (to be
204 * incremented by the size of the name)
205 * @param depth current depth of our recursion (to prevent stack overflow)
206 * @return name as 0-terminated C string on success, NULL if the payload is malformed
209 parse_name (const char *udp_payload,
210 size_t udp_payload_length,
214 const uint8_t *input = (const uint8_t *) udp_payload;
223 ret = GNUNET_strdup ("");
226 if (*off >= udp_payload_length)
239 if (*off + 1 + len > udp_payload_length)
244 GNUNET_asprintf (&tmp,
247 &udp_payload[*off + 1]);
249 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
251 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
252 _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
256 GNUNET_asprintf (&tmp,
260 &udp_payload[*off + 1]);
265 GNUNET_asprintf (&tmp,
279 else if ((64 | 128) == (len & (64 | 128)) )
284 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
286 /* pointer to string */
287 if (*off + 1 > udp_payload_length)
292 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
293 xstr = parse_name (udp_payload,
302 GNUNET_asprintf (&tmp,
309 if (strlen (ret) > udp_payload_length)
312 goto error; /* we are looping (building an infinite string) */
315 /* pointers always terminate names */
320 /* neither pointer nor inline string, not supported... */
326 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
336 * Parse name inside of a DNS query or record.
338 * @param udp_payload entire UDP payload
339 * @param udp_payload_length length of @a udp_payload
340 * @param off pointer to the offset of the name to parse in the udp_payload (to be
341 * incremented by the size of the name)
342 * @return name as 0-terminated C string on success, NULL if the payload is malformed
345 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
346 size_t udp_payload_length,
349 return parse_name (udp_payload, udp_payload_length, off, 0);
354 * Parse a DNS query entry.
356 * @param udp_payload entire UDP payload
357 * @param udp_payload_length length of @a udp_payload
358 * @param off pointer to the offset of the query to parse in the udp_payload (to be
359 * incremented by the size of the query)
360 * @param q where to write the query information
361 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
364 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
365 size_t udp_payload_length,
367 struct GNUNET_DNSPARSER_Query *q)
370 struct GNUNET_TUN_DnsQueryLine ql;
372 name = GNUNET_DNSPARSER_parse_name (udp_payload,
378 return GNUNET_SYSERR;
381 if (*off + sizeof (struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
384 return GNUNET_SYSERR;
386 GNUNET_memcpy (&ql, &udp_payload[*off], sizeof (ql));
388 q->type = ntohs (ql.type);
389 q->dns_traffic_class = ntohs (ql.dns_traffic_class);
395 * Parse a DNS SOA record.
397 * @param udp_payload reference to UDP packet
398 * @param udp_payload_length length of @a udp_payload
399 * @param off pointer to the offset of the query to parse in the SOA record (to be
400 * incremented by the size of the record), unchanged on error
401 * @return the parsed SOA record, NULL on error
403 struct GNUNET_DNSPARSER_SoaRecord *
404 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
405 size_t udp_payload_length,
408 struct GNUNET_DNSPARSER_SoaRecord *soa;
409 struct GNUNET_TUN_DnsSoaRecord soa_bin;
413 soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
414 soa->mname = GNUNET_DNSPARSER_parse_name (udp_payload,
417 soa->rname = GNUNET_DNSPARSER_parse_name (udp_payload,
420 if ( (NULL == soa->mname) ||
421 (NULL == soa->rname) ||
422 (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length) )
425 GNUNET_DNSPARSER_free_soa (soa);
429 GNUNET_memcpy (&soa_bin,
431 sizeof (struct GNUNET_TUN_DnsSoaRecord));
432 soa->serial = ntohl (soa_bin.serial);
433 soa->refresh = ntohl (soa_bin.refresh);
434 soa->retry = ntohl (soa_bin.retry);
435 soa->expire = ntohl (soa_bin.expire);
436 soa->minimum_ttl = ntohl (soa_bin.minimum);
437 (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
443 * Parse a DNS MX record.
445 * @param udp_payload reference to UDP packet
446 * @param udp_payload_length length of @a udp_payload
447 * @param off pointer to the offset of the query to parse in the MX record (to be
448 * incremented by the size of the record), unchanged on error
449 * @return the parsed MX record, NULL on error
451 struct GNUNET_DNSPARSER_MxRecord *
452 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
453 size_t udp_payload_length,
456 struct GNUNET_DNSPARSER_MxRecord *mx;
461 if (*off + sizeof (uint16_t) > udp_payload_length)
466 GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
467 (*off) += sizeof (uint16_t);
468 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
469 mx->preference = ntohs (mxpref);
470 mx->mxhost = GNUNET_DNSPARSER_parse_name (udp_payload,
473 if (NULL == mx->mxhost)
476 GNUNET_DNSPARSER_free_mx (mx);
485 * Parse a DNS SRV record.
487 * @param udp_payload reference to UDP packet
488 * @param udp_payload_length length of @a udp_payload
489 * @param off pointer to the offset of the query to parse in the SRV record (to be
490 * incremented by the size of the record), unchanged on error
491 * @return the parsed SRV record, NULL on error
493 struct GNUNET_DNSPARSER_SrvRecord *
494 GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
495 size_t udp_payload_length,
498 struct GNUNET_DNSPARSER_SrvRecord *srv;
499 struct GNUNET_TUN_DnsSrvRecord srv_bin;
503 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
505 GNUNET_memcpy (&srv_bin,
507 sizeof (struct GNUNET_TUN_DnsSrvRecord));
508 (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
509 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
510 srv->priority = ntohs (srv_bin.prio);
511 srv->weight = ntohs (srv_bin.weight);
512 srv->port = ntohs (srv_bin.port);
513 srv->target = GNUNET_DNSPARSER_parse_name (udp_payload,
516 if (NULL == srv->target)
518 GNUNET_DNSPARSER_free_srv (srv);
527 * Parse a DNS CERT record.
529 * @param udp_payload reference to UDP packet
530 * @param udp_payload_length length of @a udp_payload
531 * @param off pointer to the offset of the query to parse in the CERT record (to be
532 * incremented by the size of the record), unchanged on error
533 * @return the parsed CERT record, NULL on error
535 struct GNUNET_DNSPARSER_CertRecord *
536 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
537 size_t udp_payload_length,
540 struct GNUNET_DNSPARSER_CertRecord *cert;
541 struct GNUNET_TUN_DnsCertRecord dcert;
543 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
548 GNUNET_memcpy (&dcert,
550 sizeof (struct GNUNET_TUN_DnsCertRecord));
551 (*off) += sizeof (struct GNUNET_TUN_DnsCertRecord);
552 cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
553 cert->cert_type = ntohs (dcert.cert_type);
554 cert->cert_tag = ntohs (dcert.cert_tag);
555 cert->algorithm = dcert.algorithm;
556 cert->certificate_size = udp_payload_length - (*off);
557 cert->certificate_data = GNUNET_malloc (cert->certificate_size);
558 GNUNET_memcpy (cert->certificate_data,
560 cert->certificate_size);
561 (*off) += cert->certificate_size;
567 * Parse a DNS record entry.
569 * @param udp_payload entire UDP payload
570 * @param udp_payload_length length of @a udp_payload
571 * @param off pointer to the offset of the record to parse in the udp_payload (to be
572 * incremented by the size of the record)
573 * @param r where to write the record information
574 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
577 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
578 size_t udp_payload_length,
580 struct GNUNET_DNSPARSER_Record *r)
583 struct GNUNET_TUN_DnsRecordLine rl;
587 name = GNUNET_DNSPARSER_parse_name (udp_payload,
593 return GNUNET_SYSERR;
596 if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
599 return GNUNET_SYSERR;
601 GNUNET_memcpy (&rl, &udp_payload[*off], sizeof (rl));
602 (*off) += sizeof (rl);
603 r->type = ntohs (rl.type);
604 r->dns_traffic_class = ntohs (rl.dns_traffic_class);
605 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
607 data_len = ntohs (rl.data_len);
608 if (*off + data_len > udp_payload_length)
611 return GNUNET_SYSERR;
616 case GNUNET_DNSPARSER_TYPE_NS:
617 case GNUNET_DNSPARSER_TYPE_CNAME:
618 case GNUNET_DNSPARSER_TYPE_DNAME:
619 case GNUNET_DNSPARSER_TYPE_PTR:
620 r->data.hostname = GNUNET_DNSPARSER_parse_name (udp_payload,
623 if ( (NULL == r->data.hostname) ||
624 (old_off + data_len != *off) )
625 return GNUNET_SYSERR;
627 case GNUNET_DNSPARSER_TYPE_SOA:
628 r->data.soa = GNUNET_DNSPARSER_parse_soa (udp_payload,
631 if ( (NULL == r->data.soa) ||
632 (old_off + data_len != *off) )
635 return GNUNET_SYSERR;
638 case GNUNET_DNSPARSER_TYPE_MX:
639 r->data.mx = GNUNET_DNSPARSER_parse_mx (udp_payload,
642 if ( (NULL == r->data.mx) ||
643 (old_off + data_len != *off) )
646 return GNUNET_SYSERR;
649 case GNUNET_DNSPARSER_TYPE_SRV:
650 r->data.srv = GNUNET_DNSPARSER_parse_srv (udp_payload,
653 if ( (NULL == r->data.srv) ||
654 (old_off + data_len != *off) )
657 return GNUNET_SYSERR;
661 r->data.raw.data = GNUNET_malloc (data_len);
662 r->data.raw.data_len = data_len;
663 GNUNET_memcpy (r->data.raw.data,
674 * Parse a UDP payload of a DNS packet in to a nice struct for further
675 * processing and manipulation.
677 * @param udp_payload wire-format of the DNS packet
678 * @param udp_payload_length number of bytes in @a udp_payload
679 * @return NULL on error, otherwise the parsed packet
681 struct GNUNET_DNSPARSER_Packet *
682 GNUNET_DNSPARSER_parse (const char *udp_payload,
683 size_t udp_payload_length)
685 struct GNUNET_DNSPARSER_Packet *p;
686 const struct GNUNET_TUN_DnsHeader *dns;
690 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
692 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
693 off = sizeof (struct GNUNET_TUN_DnsHeader);
694 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
695 p->flags = dns->flags;
697 n = ntohs (dns->query_count);
700 p->queries = GNUNET_new_array (n,
701 struct GNUNET_DNSPARSER_Query);
703 for (unsigned int i=0;i<n;i++)
705 GNUNET_DNSPARSER_parse_query (udp_payload,
711 n = ntohs (dns->answer_rcount);
714 p->answers = GNUNET_new_array (n,
715 struct GNUNET_DNSPARSER_Record);
717 for (unsigned int i=0;i<n;i++)
719 GNUNET_DNSPARSER_parse_record (udp_payload,
725 n = ntohs (dns->authority_rcount);
728 p->authority_records = GNUNET_new_array (n,
729 struct GNUNET_DNSPARSER_Record);
730 p->num_authority_records = n;
731 for (unsigned int i=0;i<n;i++)
733 GNUNET_DNSPARSER_parse_record (udp_payload,
736 &p->authority_records[i]))
739 n = ntohs (dns->additional_rcount);
742 p->additional_records = GNUNET_new_array (n,
743 struct GNUNET_DNSPARSER_Record);
744 p->num_additional_records = n;
745 for (unsigned int i=0;i<n;i++)
748 GNUNET_DNSPARSER_parse_record (udp_payload,
751 &p->additional_records[i]))
758 GNUNET_DNSPARSER_free_packet (p);
764 * Free memory taken by a packet.
766 * @param p packet to free
769 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
771 for (unsigned int i=0;i<p->num_queries;i++)
772 GNUNET_free_non_null (p->queries[i].name);
773 GNUNET_free_non_null (p->queries);
774 for (unsigned int i=0;i<p->num_answers;i++)
775 GNUNET_DNSPARSER_free_record (&p->answers[i]);
776 GNUNET_free_non_null (p->answers);
777 for (unsigned int i=0;i<p->num_authority_records;i++)
778 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
779 GNUNET_free_non_null (p->authority_records);
780 for (unsigned int i=0;i<p->num_additional_records;i++)
781 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
782 GNUNET_free_non_null (p->additional_records);
787 /* ********************** DNS packet assembly code **************** */
791 * Add a DNS name to the UDP packet at the given location, converting
792 * the name to IDNA notation as necessary.
794 * @param dst where to write the name (UDP packet)
795 * @param dst_len number of bytes in @a dst
796 * @param off pointer to offset where to write the name (increment by bytes used)
797 * must not be changed if there is an error
798 * @param name name to write
799 * @return #GNUNET_SYSERR if @a name is invalid
800 * #GNUNET_NO if @a name did not fit
801 * #GNUNET_OK if @a name was added to @a dst
804 GNUNET_DNSPARSER_builder_add_name (char *dst,
810 const char *idna_name;
818 return GNUNET_SYSERR;
821 (rc = idna_to_ascii_8z (name,
823 IDNA_ALLOW_UNASSIGNED)))
825 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
826 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
831 idna_name = idna_start;
833 if (start + strlen (idna_name) + 2 > dst_len)
838 dot = strchr (idna_name, '.');
840 len = strlen (idna_name);
842 len = dot - idna_name;
843 if ( (len >= 64) || (0 == len) )
846 goto fail; /* segment too long or empty */
848 dst[pos++] = (char) (uint8_t) len;
849 GNUNET_memcpy (&dst[pos],
853 idna_name += len + 1; /* also skip dot */
856 dst[pos++] = '\0'; /* terminator */
859 idn_free (idna_start);
866 idn_free (idna_start);
875 * Add a DNS query to the UDP packet at the given location.
877 * @param dst where to write the query
878 * @param dst_len number of bytes in @a dst
879 * @param off pointer to offset where to write the query (increment by bytes used)
880 * must not be changed if there is an error
881 * @param query query to write
882 * @return #GNUNET_SYSERR if @a query is invalid
883 * #GNUNET_NO if @a query did not fit
884 * #GNUNET_OK if @a query was added to @a dst
887 GNUNET_DNSPARSER_builder_add_query (char *dst,
890 const struct GNUNET_DNSPARSER_Query *query)
893 struct GNUNET_TUN_DnsQueryLine ql;
895 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
896 if (ret != GNUNET_OK)
898 ql.type = htons (query->type);
899 ql.dns_traffic_class = htons (query->dns_traffic_class);
900 GNUNET_memcpy (&dst[*off], &ql, sizeof (ql));
901 (*off) += sizeof (ql);
907 * Add an MX record to the UDP packet at the given location.
909 * @param dst where to write the mx record
910 * @param dst_len number of bytes in @a dst
911 * @param off pointer to offset where to write the mx information (increment by bytes used);
912 * can also change if there was an error
913 * @param mx mx information to write
914 * @return #GNUNET_SYSERR if @a mx is invalid
915 * #GNUNET_NO if @a mx did not fit
916 * #GNUNET_OK if @a mx was added to @a dst
919 GNUNET_DNSPARSER_builder_add_mx (char *dst,
922 const struct GNUNET_DNSPARSER_MxRecord *mx)
926 if (*off + sizeof (uint16_t) > dst_len)
928 mxpref = htons (mx->preference);
929 GNUNET_memcpy (&dst[*off],
932 (*off) += sizeof (mxpref);
933 return GNUNET_DNSPARSER_builder_add_name (dst,
941 * Add a CERT record to the UDP packet at the given location.
943 * @param dst where to write the CERT record
944 * @param dst_len number of bytes in @a dst
945 * @param off pointer to offset where to write the CERT information (increment by bytes used);
946 * can also change if there was an error
947 * @param cert CERT information to write
948 * @return #GNUNET_SYSERR if @a cert is invalid
949 * #GNUNET_NO if @a cert did not fit
950 * #GNUNET_OK if @a cert was added to @a dst
953 GNUNET_DNSPARSER_builder_add_cert (char *dst,
956 const struct GNUNET_DNSPARSER_CertRecord *cert)
958 struct GNUNET_TUN_DnsCertRecord dcert;
960 if ( (cert->cert_type > UINT16_MAX) ||
961 (cert->algorithm > UINT8_MAX) )
964 return GNUNET_SYSERR;
966 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size > dst_len)
968 dcert.cert_type = htons ((uint16_t) cert->cert_type);
969 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
970 dcert.algorithm = (uint8_t) cert->algorithm;
971 GNUNET_memcpy (&dst[*off], &dcert, sizeof (dcert));
972 (*off) += sizeof (dcert);
973 GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
974 (*off) += cert->certificate_size;
980 * Add an SOA record to the UDP packet at the given location.
982 * @param dst where to write the SOA record
983 * @param dst_len number of bytes in @a dst
984 * @param off pointer to offset where to write the SOA information (increment by bytes used)
985 * can also change if there was an error
986 * @param soa SOA information to write
987 * @return #GNUNET_SYSERR if @a soa is invalid
988 * #GNUNET_NO if @a soa did not fit
989 * #GNUNET_OK if @a soa was added to @a dst
992 GNUNET_DNSPARSER_builder_add_soa (char *dst,
995 const struct GNUNET_DNSPARSER_SoaRecord *soa)
997 struct GNUNET_TUN_DnsSoaRecord sd;
1000 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1004 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1009 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1011 sd.serial = htonl (soa->serial);
1012 sd.refresh = htonl (soa->refresh);
1013 sd.retry = htonl (soa->retry);
1014 sd.expire = htonl (soa->expire);
1015 sd.minimum = htonl (soa->minimum_ttl);
1016 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1017 (*off) += sizeof (sd);
1023 * Add an SRV record to the UDP packet at the given location.
1025 * @param dst where to write the SRV record
1026 * @param dst_len number of bytes in @a dst
1027 * @param off pointer to offset where to write the SRV information (increment by bytes used)
1028 * can also change if there was an error
1029 * @param srv SRV information to write
1030 * @return #GNUNET_SYSERR if @a srv is invalid
1031 * #GNUNET_NO if @a srv did not fit
1032 * #GNUNET_OK if @a srv was added to @a dst
1035 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1038 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1040 struct GNUNET_TUN_DnsSrvRecord sd;
1043 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1045 sd.prio = htons (srv->priority);
1046 sd.weight = htons (srv->weight);
1047 sd.port = htons (srv->port);
1048 GNUNET_memcpy (&dst[*off],
1051 (*off) += sizeof (sd);
1052 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1062 * Add a DNS record to the UDP packet at the given location.
1064 * @param dst where to write the query
1065 * @param dst_len number of bytes in @a dst
1066 * @param off pointer to offset where to write the query (increment by bytes used)
1067 * must not be changed if there is an error
1068 * @param record record to write
1069 * @return #GNUNET_SYSERR if @a record is invalid
1070 * #GNUNET_NO if @a record did not fit
1071 * #GNUNET_OK if @a record was added to @a dst
1074 add_record (char *dst,
1077 const struct GNUNET_DNSPARSER_Record *record)
1082 struct GNUNET_TUN_DnsRecordLine rl;
1085 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1086 dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine),
1089 if (GNUNET_OK != ret)
1091 /* '*off' is now the position where we will need to write the record line */
1093 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1094 switch (record->type)
1096 case GNUNET_DNSPARSER_TYPE_MX:
1097 ret = GNUNET_DNSPARSER_builder_add_mx (dst,
1102 case GNUNET_DNSPARSER_TYPE_CERT:
1103 ret = GNUNET_DNSPARSER_builder_add_cert (dst,
1108 case GNUNET_DNSPARSER_TYPE_SOA:
1109 ret = GNUNET_DNSPARSER_builder_add_soa (dst,
1114 case GNUNET_DNSPARSER_TYPE_NS:
1115 case GNUNET_DNSPARSER_TYPE_CNAME:
1116 case GNUNET_DNSPARSER_TYPE_PTR:
1117 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1120 record->data.hostname);
1122 case GNUNET_DNSPARSER_TYPE_SRV:
1123 ret = GNUNET_DNSPARSER_builder_add_srv (dst,
1129 if (pos + record->data.raw.data_len > dst_len)
1134 GNUNET_memcpy (&dst[pos],
1135 record->data.raw.data,
1136 record->data.raw.data_len);
1137 pos += record->data.raw.data_len;
1141 if (GNUNET_OK != ret)
1147 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1149 /* record data too long */
1153 rl.type = htons (record->type);
1154 rl.dns_traffic_class = htons (record->dns_traffic_class);
1155 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1156 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1157 GNUNET_memcpy (&dst[*off],
1159 sizeof (struct GNUNET_TUN_DnsRecordLine));
1166 * Given a DNS packet @a p, generate the corresponding UDP payload.
1167 * Note that we do not attempt to pack the strings with pointers
1168 * as this would complicate the code and this is about being
1169 * simple and secure, not fast, fancy and broken like bind.
1171 * @param p packet to pack
1172 * @param max maximum allowed size for the resulting UDP payload
1173 * @param buf set to a buffer with the packed message
1174 * @param buf_length set to the length of @a buf
1175 * @return #GNUNET_SYSERR if @a p is invalid
1176 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1177 * #GNUNET_OK if @a p was packed completely into @a buf
1180 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1185 struct GNUNET_TUN_DnsHeader dns;
1192 if ( (p->num_queries > UINT16_MAX) ||
1193 (p->num_answers > UINT16_MAX) ||
1194 (p->num_authority_records > UINT16_MAX) ||
1195 (p->num_additional_records > UINT16_MAX) )
1196 return GNUNET_SYSERR;
1198 dns.flags = p->flags;
1199 dns.query_count = htons (p->num_queries);
1200 dns.answer_rcount = htons (p->num_answers);
1201 dns.authority_rcount = htons (p->num_authority_records);
1202 dns.additional_rcount = htons (p->num_additional_records);
1204 off = sizeof (struct GNUNET_TUN_DnsHeader);
1206 for (i=0;i<p->num_queries;i++)
1208 ret = GNUNET_DNSPARSER_builder_add_query (tmp,
1212 if (GNUNET_SYSERR == ret)
1213 return GNUNET_SYSERR;
1214 if (GNUNET_NO == ret)
1216 dns.query_count = htons ((uint16_t) (i-1));
1221 for (i=0;i<p->num_answers;i++)
1223 ret = add_record (tmp,
1227 if (GNUNET_SYSERR == ret)
1228 return GNUNET_SYSERR;
1229 if (GNUNET_NO == ret)
1231 dns.answer_rcount = htons ((uint16_t) (i-1));
1236 for (i=0;i<p->num_authority_records;i++)
1238 ret = add_record (tmp,
1241 &p->authority_records[i]);
1242 if (GNUNET_SYSERR == ret)
1243 return GNUNET_SYSERR;
1244 if (GNUNET_NO == ret)
1246 dns.authority_rcount = htons ((uint16_t) (i-1));
1251 for (i=0;i<p->num_additional_records;i++)
1253 ret = add_record (tmp,
1256 &p->additional_records[i]);
1257 if (GNUNET_SYSERR == ret)
1258 return GNUNET_SYSERR;
1259 if (GNUNET_NO == ret)
1261 dns.additional_rcount = htons (i-1);
1267 if (GNUNET_YES == trc)
1268 dns.flags.message_truncated = 1;
1271 sizeof (struct GNUNET_TUN_DnsHeader));
1273 *buf = GNUNET_malloc (off);
1275 GNUNET_memcpy (*buf,
1278 if (GNUNET_YES == trc)
1285 * Convert a block of binary data to HEX.
1287 * @param data binary data to convert
1288 * @param data_size number of bytes in @a data
1289 * @return HEX string (lower case)
1292 GNUNET_DNSPARSER_bin_to_hex (const void *data,
1297 const uint8_t *idata;
1300 ret = GNUNET_malloc (data_size * 2 + 1);
1301 for (off = 0; off < data_size; off++)
1302 sprintf (&ret[off * 2],
1310 * Convert a HEX string to block of binary data.
1312 * @param hex HEX string to convert (may contain mixed case)
1313 * @param data where to write result, must be
1314 * at least `strlen(hex)/2` bytes long
1315 * @return number of bytes written to data
1318 GNUNET_DNSPARSER_hex_to_bin (const char *hex,
1327 data_size = strlen (hex) / 2;
1330 for (off = 0; off < data_size; off++)
1332 in[0] = tolower ((unsigned char) hex[off * 2]);
1333 in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
1334 if (1 != sscanf (in, "%x", &h))
1336 idata[off] = (uint8_t) h;
1342 /* end of dnsparser.c */