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.
17 * @file dns/dnsparser.c
18 * @brief helper library to parse DNS packets.
19 * @author Philipp Toelke
20 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_dnsparser_lib.h"
29 #include "gnunet_tun_lib.h"
33 * Check if a label in UTF-8 format can be coded into valid IDNA.
34 * This can fail if the ASCII-conversion becomes longer than 63 characters.
36 * @param label label to check (UTF-8 string)
37 * @return #GNUNET_OK if the label can be converted to IDNA,
38 * #GNUNET_SYSERR if the label is not valid for DNS names
41 GNUNET_DNSPARSER_check_label (const char *label)
46 if (NULL != strchr (label, '.'))
47 return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */
49 idna_to_ascii_8z (label, &output, IDNA_ALLOW_UNASSIGNED))
51 slen = strlen (output);
57 return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK;
62 * Check if a label in UTF-8 format can be coded into valid IDNA.
63 * This can fail if the ASCII-conversion becomes longer than 253 characters.
65 * @param name name to check (UTF-8 string)
66 * @return #GNUNET_OK if the label can be converted to IDNA,
67 * #GNUNET_SYSERR if the label is not valid for DNS names
70 GNUNET_DNSPARSER_check_name (const char *name)
77 ldup = GNUNET_strdup (name);
78 for (tok = strtok (ldup, "."); NULL != tok; tok = strtok (NULL, "."))
80 GNUNET_DNSPARSER_check_label (tok))
87 idna_to_ascii_8z (name, &output, IDNA_ALLOW_UNASSIGNED))
89 slen = strlen (output);
95 return (slen > 253) ? GNUNET_SYSERR : GNUNET_OK;
100 * Free SOA information record.
102 * @param soa record to free
105 GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
109 GNUNET_free_non_null (soa->mname);
110 GNUNET_free_non_null (soa->rname);
116 * Free CERT information record.
118 * @param cert record to free
121 GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert)
125 GNUNET_free_non_null (cert->certificate_data);
131 * Free SRV information record.
133 * @param srv record to free
136 GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
140 GNUNET_free_non_null (srv->target);
146 * Free MX information record.
148 * @param mx record to free
151 GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
155 GNUNET_free_non_null (mx->mxhost);
161 * Free the given DNS record.
163 * @param r record to free
166 GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
168 GNUNET_free_non_null (r->name);
171 case GNUNET_DNSPARSER_TYPE_MX:
172 GNUNET_DNSPARSER_free_mx (r->data.mx);
174 case GNUNET_DNSPARSER_TYPE_SOA:
175 GNUNET_DNSPARSER_free_soa (r->data.soa);
177 case GNUNET_DNSPARSER_TYPE_SRV:
178 GNUNET_DNSPARSER_free_srv (r->data.srv);
180 case GNUNET_DNSPARSER_TYPE_CERT:
181 GNUNET_DNSPARSER_free_cert (r->data.cert);
183 case GNUNET_DNSPARSER_TYPE_NS:
184 case GNUNET_DNSPARSER_TYPE_CNAME:
185 case GNUNET_DNSPARSER_TYPE_PTR:
186 GNUNET_free_non_null (r->data.hostname);
189 GNUNET_free_non_null (r->data.raw.data);
196 * Parse name inside of a DNS query or record.
198 * @param udp_payload entire UDP payload
199 * @param udp_payload_length length of @a udp_payload
200 * @param off pointer to the offset of the name to parse in the udp_payload (to be
201 * incremented by the size of the name)
202 * @param depth current depth of our recursion (to prevent stack overflow)
203 * @return name as 0-terminated C string on success, NULL if the payload is malformed
206 parse_name (const char *udp_payload,
207 size_t udp_payload_length,
211 const uint8_t *input = (const uint8_t *) udp_payload;
220 ret = GNUNET_strdup ("");
223 if (*off >= udp_payload_length)
236 if (*off + 1 + len > udp_payload_length)
241 GNUNET_asprintf (&tmp,
244 &udp_payload[*off + 1]);
246 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
248 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
249 _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
253 GNUNET_asprintf (&tmp,
257 &udp_payload[*off + 1]);
262 GNUNET_asprintf (&tmp,
276 else if ((64 | 128) == (len & (64 | 128)) )
281 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
283 /* pointer to string */
284 if (*off + 1 > udp_payload_length)
289 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
290 xstr = parse_name (udp_payload,
299 GNUNET_asprintf (&tmp,
306 if (strlen (ret) > udp_payload_length)
309 goto error; /* we are looping (building an infinite string) */
312 /* pointers always terminate names */
317 /* neither pointer nor inline string, not supported... */
323 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
333 * Parse name inside of a DNS query or record.
335 * @param udp_payload entire UDP payload
336 * @param udp_payload_length length of @a udp_payload
337 * @param off pointer to the offset of the name to parse in the udp_payload (to be
338 * incremented by the size of the name)
339 * @return name as 0-terminated C string on success, NULL if the payload is malformed
342 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
343 size_t udp_payload_length,
346 return parse_name (udp_payload, udp_payload_length, off, 0);
351 * Parse a DNS query entry.
353 * @param udp_payload entire UDP payload
354 * @param udp_payload_length length of @a udp_payload
355 * @param off pointer to the offset of the query to parse in the udp_payload (to be
356 * incremented by the size of the query)
357 * @param q where to write the query information
358 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
361 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
362 size_t udp_payload_length,
364 struct GNUNET_DNSPARSER_Query *q)
367 struct GNUNET_TUN_DnsQueryLine ql;
369 name = GNUNET_DNSPARSER_parse_name (udp_payload,
375 return GNUNET_SYSERR;
378 if (*off + sizeof (struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
381 return GNUNET_SYSERR;
383 GNUNET_memcpy (&ql, &udp_payload[*off], sizeof (ql));
385 q->type = ntohs (ql.type);
386 q->dns_traffic_class = ntohs (ql.dns_traffic_class);
392 * Parse a DNS SOA record.
394 * @param udp_payload reference to UDP packet
395 * @param udp_payload_length length of @a udp_payload
396 * @param off pointer to the offset of the query to parse in the SOA record (to be
397 * incremented by the size of the record), unchanged on error
398 * @return the parsed SOA record, NULL on error
400 struct GNUNET_DNSPARSER_SoaRecord *
401 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
402 size_t udp_payload_length,
405 struct GNUNET_DNSPARSER_SoaRecord *soa;
406 struct GNUNET_TUN_DnsSoaRecord soa_bin;
410 soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
411 soa->mname = GNUNET_DNSPARSER_parse_name (udp_payload,
414 soa->rname = GNUNET_DNSPARSER_parse_name (udp_payload,
417 if ( (NULL == soa->mname) ||
418 (NULL == soa->rname) ||
419 (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length) )
422 GNUNET_DNSPARSER_free_soa (soa);
426 GNUNET_memcpy (&soa_bin,
428 sizeof (struct GNUNET_TUN_DnsSoaRecord));
429 soa->serial = ntohl (soa_bin.serial);
430 soa->refresh = ntohl (soa_bin.refresh);
431 soa->retry = ntohl (soa_bin.retry);
432 soa->expire = ntohl (soa_bin.expire);
433 soa->minimum_ttl = ntohl (soa_bin.minimum);
434 (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
440 * Parse a DNS MX record.
442 * @param udp_payload reference to UDP packet
443 * @param udp_payload_length length of @a udp_payload
444 * @param off pointer to the offset of the query to parse in the MX record (to be
445 * incremented by the size of the record), unchanged on error
446 * @return the parsed MX record, NULL on error
448 struct GNUNET_DNSPARSER_MxRecord *
449 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
450 size_t udp_payload_length,
453 struct GNUNET_DNSPARSER_MxRecord *mx;
458 if (*off + sizeof (uint16_t) > udp_payload_length)
463 GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
464 (*off) += sizeof (uint16_t);
465 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
466 mx->preference = ntohs (mxpref);
467 mx->mxhost = GNUNET_DNSPARSER_parse_name (udp_payload,
470 if (NULL == mx->mxhost)
473 GNUNET_DNSPARSER_free_mx (mx);
482 * Parse a DNS SRV record.
484 * @param udp_payload reference to UDP packet
485 * @param udp_payload_length length of @a udp_payload
486 * @param off pointer to the offset of the query to parse in the SRV record (to be
487 * incremented by the size of the record), unchanged on error
488 * @return the parsed SRV record, NULL on error
490 struct GNUNET_DNSPARSER_SrvRecord *
491 GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
492 size_t udp_payload_length,
495 struct GNUNET_DNSPARSER_SrvRecord *srv;
496 struct GNUNET_TUN_DnsSrvRecord srv_bin;
500 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
502 GNUNET_memcpy (&srv_bin,
504 sizeof (struct GNUNET_TUN_DnsSrvRecord));
505 (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
506 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
507 srv->priority = ntohs (srv_bin.prio);
508 srv->weight = ntohs (srv_bin.weight);
509 srv->port = ntohs (srv_bin.port);
510 srv->target = GNUNET_DNSPARSER_parse_name (udp_payload,
513 if (NULL == srv->target)
515 GNUNET_DNSPARSER_free_srv (srv);
524 * Parse a DNS CERT record.
526 * @param udp_payload reference to UDP packet
527 * @param udp_payload_length length of @a udp_payload
528 * @param off pointer to the offset of the query to parse in the CERT record (to be
529 * incremented by the size of the record), unchanged on error
530 * @return the parsed CERT record, NULL on error
532 struct GNUNET_DNSPARSER_CertRecord *
533 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
534 size_t udp_payload_length,
537 struct GNUNET_DNSPARSER_CertRecord *cert;
538 struct GNUNET_TUN_DnsCertRecord dcert;
540 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
545 GNUNET_memcpy (&dcert, &udp_payload[*off], sizeof (struct GNUNET_TUN_DnsCertRecord));
546 (*off) += sizeof (struct GNUNET_TUN_DnsCertRecord);
547 cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
548 cert->cert_type = ntohs (dcert.cert_type);
549 cert->cert_tag = ntohs (dcert.cert_tag);
550 cert->algorithm = dcert.algorithm;
551 cert->certificate_size = udp_payload_length - (*off);
552 cert->certificate_data = GNUNET_malloc (cert->certificate_size);
553 GNUNET_memcpy (cert->certificate_data,
555 cert->certificate_size);
556 (*off) += cert->certificate_size;
562 * Parse a DNS record entry.
564 * @param udp_payload entire UDP payload
565 * @param udp_payload_length length of @a udp_payload
566 * @param off pointer to the offset of the record to parse in the udp_payload (to be
567 * incremented by the size of the record)
568 * @param r where to write the record information
569 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
572 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
573 size_t udp_payload_length,
575 struct GNUNET_DNSPARSER_Record *r)
578 struct GNUNET_TUN_DnsRecordLine rl;
582 name = GNUNET_DNSPARSER_parse_name (udp_payload,
588 return GNUNET_SYSERR;
591 if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
594 return GNUNET_SYSERR;
596 GNUNET_memcpy (&rl, &udp_payload[*off], sizeof (rl));
597 (*off) += sizeof (rl);
598 r->type = ntohs (rl.type);
599 r->dns_traffic_class = ntohs (rl.dns_traffic_class);
600 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
602 data_len = ntohs (rl.data_len);
603 if (*off + data_len > udp_payload_length)
606 return GNUNET_SYSERR;
611 case GNUNET_DNSPARSER_TYPE_NS:
612 case GNUNET_DNSPARSER_TYPE_CNAME:
613 case GNUNET_DNSPARSER_TYPE_DNAME:
614 case GNUNET_DNSPARSER_TYPE_PTR:
615 r->data.hostname = GNUNET_DNSPARSER_parse_name (udp_payload,
618 if ( (NULL == r->data.hostname) ||
619 (old_off + data_len != *off) )
620 return GNUNET_SYSERR;
622 case GNUNET_DNSPARSER_TYPE_SOA:
623 r->data.soa = GNUNET_DNSPARSER_parse_soa (udp_payload,
626 if ( (NULL == r->data.soa) ||
627 (old_off + data_len != *off) )
630 return GNUNET_SYSERR;
633 case GNUNET_DNSPARSER_TYPE_MX:
634 r->data.mx = GNUNET_DNSPARSER_parse_mx (udp_payload,
637 if ( (NULL == r->data.mx) ||
638 (old_off + data_len != *off) )
641 return GNUNET_SYSERR;
644 case GNUNET_DNSPARSER_TYPE_SRV:
645 r->data.srv = GNUNET_DNSPARSER_parse_srv (udp_payload,
648 if ( (NULL == r->data.srv) ||
649 (old_off + data_len != *off) )
652 return GNUNET_SYSERR;
656 r->data.raw.data = GNUNET_malloc (data_len);
657 r->data.raw.data_len = data_len;
658 GNUNET_memcpy (r->data.raw.data,
669 * Parse a UDP payload of a DNS packet in to a nice struct for further
670 * processing and manipulation.
672 * @param udp_payload wire-format of the DNS packet
673 * @param udp_payload_length number of bytes in @a udp_payload
674 * @return NULL on error, otherwise the parsed packet
676 struct GNUNET_DNSPARSER_Packet *
677 GNUNET_DNSPARSER_parse (const char *udp_payload,
678 size_t udp_payload_length)
680 struct GNUNET_DNSPARSER_Packet *p;
681 const struct GNUNET_TUN_DnsHeader *dns;
686 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
688 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
689 off = sizeof (struct GNUNET_TUN_DnsHeader);
690 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
691 p->flags = dns->flags;
693 n = ntohs (dns->query_count);
696 p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
700 GNUNET_DNSPARSER_parse_query (udp_payload,
706 n = ntohs (dns->answer_rcount);
709 p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
713 GNUNET_DNSPARSER_parse_record (udp_payload,
719 n = ntohs (dns->authority_rcount);
722 p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
723 p->num_authority_records = n;
726 GNUNET_DNSPARSER_parse_record (udp_payload,
729 &p->authority_records[i]))
732 n = ntohs (dns->additional_rcount);
735 p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
736 p->num_additional_records = n;
739 GNUNET_DNSPARSER_parse_record (udp_payload,
742 &p->additional_records[i]))
748 GNUNET_DNSPARSER_free_packet (p);
754 * Free memory taken by a packet.
756 * @param p packet to free
759 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
763 for (i=0;i<p->num_queries;i++)
764 GNUNET_free_non_null (p->queries[i].name);
765 GNUNET_free_non_null (p->queries);
766 for (i=0;i<p->num_answers;i++)
767 GNUNET_DNSPARSER_free_record (&p->answers[i]);
768 GNUNET_free_non_null (p->answers);
769 for (i=0;i<p->num_authority_records;i++)
770 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
771 GNUNET_free_non_null (p->authority_records);
772 for (i=0;i<p->num_additional_records;i++)
773 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
774 GNUNET_free_non_null (p->additional_records);
779 /* ********************** DNS packet assembly code **************** */
783 * Add a DNS name to the UDP packet at the given location, converting
784 * the name to IDNA notation as necessary.
786 * @param dst where to write the name (UDP packet)
787 * @param dst_len number of bytes in @a dst
788 * @param off pointer to offset where to write the name (increment by bytes used)
789 * must not be changed if there is an error
790 * @param name name to write
791 * @return #GNUNET_SYSERR if @a name is invalid
792 * #GNUNET_NO if @a name did not fit
793 * #GNUNET_OK if @a name was added to @a dst
796 GNUNET_DNSPARSER_builder_add_name (char *dst,
802 const char *idna_name;
810 return GNUNET_SYSERR;
813 (rc = idna_to_ascii_8z (name,
815 IDNA_ALLOW_UNASSIGNED)))
817 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
818 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
823 idna_name = idna_start;
825 if (start + strlen (idna_name) + 2 > dst_len)
830 dot = strchr (idna_name, '.');
832 len = strlen (idna_name);
834 len = dot - idna_name;
835 if ( (len >= 64) || (0 == len) )
838 goto fail; /* segment too long or empty */
840 dst[pos++] = (char) (uint8_t) len;
841 GNUNET_memcpy (&dst[pos],
845 idna_name += len + 1; /* also skip dot */
848 dst[pos++] = '\0'; /* terminator */
851 idn_free (idna_start);
858 idn_free (idna_start);
867 * Add a DNS query to the UDP packet at the given location.
869 * @param dst where to write the query
870 * @param dst_len number of bytes in @a dst
871 * @param off pointer to offset where to write the query (increment by bytes used)
872 * must not be changed if there is an error
873 * @param query query to write
874 * @return #GNUNET_SYSERR if @a query is invalid
875 * #GNUNET_NO if @a query did not fit
876 * #GNUNET_OK if @a query was added to @a dst
879 GNUNET_DNSPARSER_builder_add_query (char *dst,
882 const struct GNUNET_DNSPARSER_Query *query)
885 struct GNUNET_TUN_DnsQueryLine ql;
887 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
888 if (ret != GNUNET_OK)
890 ql.type = htons (query->type);
891 ql.dns_traffic_class = htons (query->dns_traffic_class);
892 GNUNET_memcpy (&dst[*off], &ql, sizeof (ql));
893 (*off) += sizeof (ql);
899 * Add an MX record to the UDP packet at the given location.
901 * @param dst where to write the mx record
902 * @param dst_len number of bytes in @a dst
903 * @param off pointer to offset where to write the mx information (increment by bytes used);
904 * can also change if there was an error
905 * @param mx mx information to write
906 * @return #GNUNET_SYSERR if @a mx is invalid
907 * #GNUNET_NO if @a mx did not fit
908 * #GNUNET_OK if @a mx was added to @a dst
911 GNUNET_DNSPARSER_builder_add_mx (char *dst,
914 const struct GNUNET_DNSPARSER_MxRecord *mx)
918 if (*off + sizeof (uint16_t) > dst_len)
920 mxpref = htons (mx->preference);
921 GNUNET_memcpy (&dst[*off],
924 (*off) += sizeof (mxpref);
925 return GNUNET_DNSPARSER_builder_add_name (dst,
933 * Add a CERT record to the UDP packet at the given location.
935 * @param dst where to write the CERT record
936 * @param dst_len number of bytes in @a dst
937 * @param off pointer to offset where to write the CERT information (increment by bytes used);
938 * can also change if there was an error
939 * @param cert CERT information to write
940 * @return #GNUNET_SYSERR if @a cert is invalid
941 * #GNUNET_NO if @a cert did not fit
942 * #GNUNET_OK if @a cert was added to @a dst
945 GNUNET_DNSPARSER_builder_add_cert (char *dst,
948 const struct GNUNET_DNSPARSER_CertRecord *cert)
950 struct GNUNET_TUN_DnsCertRecord dcert;
952 if ( (cert->cert_type > UINT16_MAX) ||
953 (cert->cert_tag > UINT16_MAX) ||
954 (cert->algorithm > UINT8_MAX) )
957 return GNUNET_SYSERR;
959 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size > dst_len)
961 dcert.cert_type = htons ((uint16_t) cert->cert_type);
962 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
963 dcert.algorithm = (uint8_t) cert->algorithm;
964 GNUNET_memcpy (&dst[*off], &dcert, sizeof (dcert));
965 (*off) += sizeof (dcert);
966 GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
967 (*off) += cert->certificate_size;
973 * Add an SOA record to the UDP packet at the given location.
975 * @param dst where to write the SOA record
976 * @param dst_len number of bytes in @a dst
977 * @param off pointer to offset where to write the SOA information (increment by bytes used)
978 * can also change if there was an error
979 * @param soa SOA information to write
980 * @return #GNUNET_SYSERR if @a soa is invalid
981 * #GNUNET_NO if @a soa did not fit
982 * #GNUNET_OK if @a soa was added to @a dst
985 GNUNET_DNSPARSER_builder_add_soa (char *dst,
988 const struct GNUNET_DNSPARSER_SoaRecord *soa)
990 struct GNUNET_TUN_DnsSoaRecord sd;
993 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
997 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1002 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1004 sd.serial = htonl (soa->serial);
1005 sd.refresh = htonl (soa->refresh);
1006 sd.retry = htonl (soa->retry);
1007 sd.expire = htonl (soa->expire);
1008 sd.minimum = htonl (soa->minimum_ttl);
1009 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1010 (*off) += sizeof (sd);
1016 * Add an SRV record to the UDP packet at the given location.
1018 * @param dst where to write the SRV record
1019 * @param dst_len number of bytes in @a dst
1020 * @param off pointer to offset where to write the SRV information (increment by bytes used)
1021 * can also change if there was an error
1022 * @param srv SRV information to write
1023 * @return #GNUNET_SYSERR if @a srv is invalid
1024 * #GNUNET_NO if @a srv did not fit
1025 * #GNUNET_OK if @a srv was added to @a dst
1028 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1031 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1033 struct GNUNET_TUN_DnsSrvRecord sd;
1036 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1038 sd.prio = htons (srv->priority);
1039 sd.weight = htons (srv->weight);
1040 sd.port = htons (srv->port);
1041 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1042 (*off) += sizeof (sd);
1043 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1053 * Add a DNS record to the UDP packet at the given location.
1055 * @param dst where to write the query
1056 * @param dst_len number of bytes in @a dst
1057 * @param off pointer to offset where to write the query (increment by bytes used)
1058 * must not be changed if there is an error
1059 * @param record record to write
1060 * @return #GNUNET_SYSERR if @a record is invalid
1061 * #GNUNET_NO if @a record did not fit
1062 * #GNUNET_OK if @a record was added to @a dst
1065 add_record (char *dst,
1068 const struct GNUNET_DNSPARSER_Record *record)
1073 struct GNUNET_TUN_DnsRecordLine rl;
1076 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1077 dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine),
1080 if (GNUNET_OK != ret)
1082 /* '*off' is now the position where we will need to write the record line */
1084 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1085 switch (record->type)
1087 case GNUNET_DNSPARSER_TYPE_MX:
1088 ret = GNUNET_DNSPARSER_builder_add_mx (dst,
1093 case GNUNET_DNSPARSER_TYPE_CERT:
1094 ret = GNUNET_DNSPARSER_builder_add_cert (dst,
1099 case GNUNET_DNSPARSER_TYPE_SOA:
1100 ret = GNUNET_DNSPARSER_builder_add_soa (dst,
1105 case GNUNET_DNSPARSER_TYPE_NS:
1106 case GNUNET_DNSPARSER_TYPE_CNAME:
1107 case GNUNET_DNSPARSER_TYPE_PTR:
1108 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1111 record->data.hostname);
1113 case GNUNET_DNSPARSER_TYPE_SRV:
1114 ret = GNUNET_DNSPARSER_builder_add_srv (dst,
1120 if (pos + record->data.raw.data_len > dst_len)
1125 GNUNET_memcpy (&dst[pos],
1126 record->data.raw.data,
1127 record->data.raw.data_len);
1128 pos += record->data.raw.data_len;
1132 if (GNUNET_OK != ret)
1138 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1140 /* record data too long */
1144 rl.type = htons (record->type);
1145 rl.dns_traffic_class = htons (record->dns_traffic_class);
1146 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1147 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1148 GNUNET_memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
1155 * Given a DNS packet @a p, generate the corresponding UDP payload.
1156 * Note that we do not attempt to pack the strings with pointers
1157 * as this would complicate the code and this is about being
1158 * simple and secure, not fast, fancy and broken like bind.
1160 * @param p packet to pack
1161 * @param max maximum allowed size for the resulting UDP payload
1162 * @param buf set to a buffer with the packed message
1163 * @param buf_length set to the length of @a buf
1164 * @return #GNUNET_SYSERR if @a p is invalid
1165 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1166 * #GNUNET_OK if @a p was packed completely into @a buf
1169 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1174 struct GNUNET_TUN_DnsHeader dns;
1181 if ( (p->num_queries > UINT16_MAX) ||
1182 (p->num_answers > UINT16_MAX) ||
1183 (p->num_authority_records > UINT16_MAX) ||
1184 (p->num_additional_records > UINT16_MAX) )
1185 return GNUNET_SYSERR;
1187 dns.flags = p->flags;
1188 dns.query_count = htons (p->num_queries);
1189 dns.answer_rcount = htons (p->num_answers);
1190 dns.authority_rcount = htons (p->num_authority_records);
1191 dns.additional_rcount = htons (p->num_additional_records);
1193 off = sizeof (struct GNUNET_TUN_DnsHeader);
1195 for (i=0;i<p->num_queries;i++)
1197 ret = GNUNET_DNSPARSER_builder_add_query (tmp,
1201 if (GNUNET_SYSERR == ret)
1202 return GNUNET_SYSERR;
1203 if (GNUNET_NO == ret)
1205 dns.query_count = htons ((uint16_t) (i-1));
1210 for (i=0;i<p->num_answers;i++)
1212 ret = add_record (tmp,
1216 if (GNUNET_SYSERR == ret)
1217 return GNUNET_SYSERR;
1218 if (GNUNET_NO == ret)
1220 dns.answer_rcount = htons ((uint16_t) (i-1));
1225 for (i=0;i<p->num_authority_records;i++)
1227 ret = add_record (tmp,
1230 &p->authority_records[i]);
1231 if (GNUNET_SYSERR == ret)
1232 return GNUNET_SYSERR;
1233 if (GNUNET_NO == ret)
1235 dns.authority_rcount = htons ((uint16_t) (i-1));
1240 for (i=0;i<p->num_additional_records;i++)
1242 ret = add_record (tmp,
1245 &p->additional_records[i]);
1246 if (GNUNET_SYSERR == ret)
1247 return GNUNET_SYSERR;
1248 if (GNUNET_NO == ret)
1250 dns.additional_rcount = htons (i-1);
1256 if (GNUNET_YES == trc)
1257 dns.flags.message_truncated = 1;
1260 sizeof (struct GNUNET_TUN_DnsHeader));
1262 *buf = GNUNET_malloc (off);
1264 GNUNET_memcpy (*buf,
1267 if (GNUNET_YES == trc)
1274 * Convert a block of binary data to HEX.
1276 * @param data binary data to convert
1277 * @param data_size number of bytes in @a data
1278 * @return HEX string (lower case)
1281 GNUNET_DNSPARSER_bin_to_hex (const void *data,
1286 const uint8_t *idata;
1289 ret = GNUNET_malloc (data_size * 2 + 1);
1290 for (off = 0; off < data_size; off++)
1291 sprintf (&ret[off * 2],
1299 * Convert a HEX string to block of binary data.
1301 * @param hex HEX string to convert (may contain mixed case)
1302 * @param data where to write result, must be
1303 * at least `strlen(hex)/2` bytes long
1304 * @return number of bytes written to data
1307 GNUNET_DNSPARSER_hex_to_bin (const char *hex,
1316 data_size = strlen (hex) / 2;
1319 for (off = 0; off < data_size; off++)
1321 in[0] = tolower ((unsigned char) hex[off * 2]);
1322 in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
1323 if (1 != sscanf (in, "%x", &h))
1325 idata[off] = (uint8_t) h;
1331 /* end of dnsparser.c */