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, &udp_payload[*off], sizeof (struct GNUNET_TUN_DnsCertRecord));
549 (*off) += sizeof (struct GNUNET_TUN_DnsCertRecord);
550 cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
551 cert->cert_type = ntohs (dcert.cert_type);
552 cert->cert_tag = ntohs (dcert.cert_tag);
553 cert->algorithm = dcert.algorithm;
554 cert->certificate_size = udp_payload_length - (*off);
555 cert->certificate_data = GNUNET_malloc (cert->certificate_size);
556 GNUNET_memcpy (cert->certificate_data,
558 cert->certificate_size);
559 (*off) += cert->certificate_size;
565 * Parse a DNS record entry.
567 * @param udp_payload entire UDP payload
568 * @param udp_payload_length length of @a udp_payload
569 * @param off pointer to the offset of the record to parse in the udp_payload (to be
570 * incremented by the size of the record)
571 * @param r where to write the record information
572 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
575 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
576 size_t udp_payload_length,
578 struct GNUNET_DNSPARSER_Record *r)
581 struct GNUNET_TUN_DnsRecordLine rl;
585 name = GNUNET_DNSPARSER_parse_name (udp_payload,
591 return GNUNET_SYSERR;
594 if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
597 return GNUNET_SYSERR;
599 GNUNET_memcpy (&rl, &udp_payload[*off], sizeof (rl));
600 (*off) += sizeof (rl);
601 r->type = ntohs (rl.type);
602 r->dns_traffic_class = ntohs (rl.dns_traffic_class);
603 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
605 data_len = ntohs (rl.data_len);
606 if (*off + data_len > udp_payload_length)
609 return GNUNET_SYSERR;
614 case GNUNET_DNSPARSER_TYPE_NS:
615 case GNUNET_DNSPARSER_TYPE_CNAME:
616 case GNUNET_DNSPARSER_TYPE_DNAME:
617 case GNUNET_DNSPARSER_TYPE_PTR:
618 r->data.hostname = GNUNET_DNSPARSER_parse_name (udp_payload,
621 if ( (NULL == r->data.hostname) ||
622 (old_off + data_len != *off) )
623 return GNUNET_SYSERR;
625 case GNUNET_DNSPARSER_TYPE_SOA:
626 r->data.soa = GNUNET_DNSPARSER_parse_soa (udp_payload,
629 if ( (NULL == r->data.soa) ||
630 (old_off + data_len != *off) )
633 return GNUNET_SYSERR;
636 case GNUNET_DNSPARSER_TYPE_MX:
637 r->data.mx = GNUNET_DNSPARSER_parse_mx (udp_payload,
640 if ( (NULL == r->data.mx) ||
641 (old_off + data_len != *off) )
644 return GNUNET_SYSERR;
647 case GNUNET_DNSPARSER_TYPE_SRV:
648 r->data.srv = GNUNET_DNSPARSER_parse_srv (udp_payload,
651 if ( (NULL == r->data.srv) ||
652 (old_off + data_len != *off) )
655 return GNUNET_SYSERR;
659 r->data.raw.data = GNUNET_malloc (data_len);
660 r->data.raw.data_len = data_len;
661 GNUNET_memcpy (r->data.raw.data,
672 * Parse a UDP payload of a DNS packet in to a nice struct for further
673 * processing and manipulation.
675 * @param udp_payload wire-format of the DNS packet
676 * @param udp_payload_length number of bytes in @a udp_payload
677 * @return NULL on error, otherwise the parsed packet
679 struct GNUNET_DNSPARSER_Packet *
680 GNUNET_DNSPARSER_parse (const char *udp_payload,
681 size_t udp_payload_length)
683 struct GNUNET_DNSPARSER_Packet *p;
684 const struct GNUNET_TUN_DnsHeader *dns;
689 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
691 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
692 off = sizeof (struct GNUNET_TUN_DnsHeader);
693 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
694 p->flags = dns->flags;
696 n = ntohs (dns->query_count);
699 p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
703 GNUNET_DNSPARSER_parse_query (udp_payload,
709 n = ntohs (dns->answer_rcount);
712 p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
716 GNUNET_DNSPARSER_parse_record (udp_payload,
722 n = ntohs (dns->authority_rcount);
725 p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
726 p->num_authority_records = n;
729 GNUNET_DNSPARSER_parse_record (udp_payload,
732 &p->authority_records[i]))
735 n = ntohs (dns->additional_rcount);
738 p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
739 p->num_additional_records = n;
742 GNUNET_DNSPARSER_parse_record (udp_payload,
745 &p->additional_records[i]))
751 GNUNET_DNSPARSER_free_packet (p);
757 * Free memory taken by a packet.
759 * @param p packet to free
762 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
766 for (i=0;i<p->num_queries;i++)
767 GNUNET_free_non_null (p->queries[i].name);
768 GNUNET_free_non_null (p->queries);
769 for (i=0;i<p->num_answers;i++)
770 GNUNET_DNSPARSER_free_record (&p->answers[i]);
771 GNUNET_free_non_null (p->answers);
772 for (i=0;i<p->num_authority_records;i++)
773 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
774 GNUNET_free_non_null (p->authority_records);
775 for (i=0;i<p->num_additional_records;i++)
776 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
777 GNUNET_free_non_null (p->additional_records);
782 /* ********************** DNS packet assembly code **************** */
786 * Add a DNS name to the UDP packet at the given location, converting
787 * the name to IDNA notation as necessary.
789 * @param dst where to write the name (UDP packet)
790 * @param dst_len number of bytes in @a dst
791 * @param off pointer to offset where to write the name (increment by bytes used)
792 * must not be changed if there is an error
793 * @param name name to write
794 * @return #GNUNET_SYSERR if @a name is invalid
795 * #GNUNET_NO if @a name did not fit
796 * #GNUNET_OK if @a name was added to @a dst
799 GNUNET_DNSPARSER_builder_add_name (char *dst,
805 const char *idna_name;
813 return GNUNET_SYSERR;
816 (rc = idna_to_ascii_8z (name,
818 IDNA_ALLOW_UNASSIGNED)))
820 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
821 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
826 idna_name = idna_start;
828 if (start + strlen (idna_name) + 2 > dst_len)
833 dot = strchr (idna_name, '.');
835 len = strlen (idna_name);
837 len = dot - idna_name;
838 if ( (len >= 64) || (0 == len) )
841 goto fail; /* segment too long or empty */
843 dst[pos++] = (char) (uint8_t) len;
844 GNUNET_memcpy (&dst[pos],
848 idna_name += len + 1; /* also skip dot */
851 dst[pos++] = '\0'; /* terminator */
854 idn_free (idna_start);
861 idn_free (idna_start);
870 * Add a DNS query to the UDP packet at the given location.
872 * @param dst where to write the query
873 * @param dst_len number of bytes in @a dst
874 * @param off pointer to offset where to write the query (increment by bytes used)
875 * must not be changed if there is an error
876 * @param query query to write
877 * @return #GNUNET_SYSERR if @a query is invalid
878 * #GNUNET_NO if @a query did not fit
879 * #GNUNET_OK if @a query was added to @a dst
882 GNUNET_DNSPARSER_builder_add_query (char *dst,
885 const struct GNUNET_DNSPARSER_Query *query)
888 struct GNUNET_TUN_DnsQueryLine ql;
890 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
891 if (ret != GNUNET_OK)
893 ql.type = htons (query->type);
894 ql.dns_traffic_class = htons (query->dns_traffic_class);
895 GNUNET_memcpy (&dst[*off], &ql, sizeof (ql));
896 (*off) += sizeof (ql);
902 * Add an MX record to the UDP packet at the given location.
904 * @param dst where to write the mx record
905 * @param dst_len number of bytes in @a dst
906 * @param off pointer to offset where to write the mx information (increment by bytes used);
907 * can also change if there was an error
908 * @param mx mx information to write
909 * @return #GNUNET_SYSERR if @a mx is invalid
910 * #GNUNET_NO if @a mx did not fit
911 * #GNUNET_OK if @a mx was added to @a dst
914 GNUNET_DNSPARSER_builder_add_mx (char *dst,
917 const struct GNUNET_DNSPARSER_MxRecord *mx)
921 if (*off + sizeof (uint16_t) > dst_len)
923 mxpref = htons (mx->preference);
924 GNUNET_memcpy (&dst[*off],
927 (*off) += sizeof (mxpref);
928 return GNUNET_DNSPARSER_builder_add_name (dst,
936 * Add a CERT record to the UDP packet at the given location.
938 * @param dst where to write the CERT record
939 * @param dst_len number of bytes in @a dst
940 * @param off pointer to offset where to write the CERT information (increment by bytes used);
941 * can also change if there was an error
942 * @param cert CERT information to write
943 * @return #GNUNET_SYSERR if @a cert is invalid
944 * #GNUNET_NO if @a cert did not fit
945 * #GNUNET_OK if @a cert was added to @a dst
948 GNUNET_DNSPARSER_builder_add_cert (char *dst,
951 const struct GNUNET_DNSPARSER_CertRecord *cert)
953 struct GNUNET_TUN_DnsCertRecord dcert;
955 if ( (cert->cert_type > UINT16_MAX) ||
956 (cert->cert_tag > UINT16_MAX) ||
957 (cert->algorithm > UINT8_MAX) )
960 return GNUNET_SYSERR;
962 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size > dst_len)
964 dcert.cert_type = htons ((uint16_t) cert->cert_type);
965 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
966 dcert.algorithm = (uint8_t) cert->algorithm;
967 GNUNET_memcpy (&dst[*off], &dcert, sizeof (dcert));
968 (*off) += sizeof (dcert);
969 GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
970 (*off) += cert->certificate_size;
976 * Add an SOA record to the UDP packet at the given location.
978 * @param dst where to write the SOA record
979 * @param dst_len number of bytes in @a dst
980 * @param off pointer to offset where to write the SOA information (increment by bytes used)
981 * can also change if there was an error
982 * @param soa SOA information to write
983 * @return #GNUNET_SYSERR if @a soa is invalid
984 * #GNUNET_NO if @a soa did not fit
985 * #GNUNET_OK if @a soa was added to @a dst
988 GNUNET_DNSPARSER_builder_add_soa (char *dst,
991 const struct GNUNET_DNSPARSER_SoaRecord *soa)
993 struct GNUNET_TUN_DnsSoaRecord sd;
996 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1000 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1005 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1007 sd.serial = htonl (soa->serial);
1008 sd.refresh = htonl (soa->refresh);
1009 sd.retry = htonl (soa->retry);
1010 sd.expire = htonl (soa->expire);
1011 sd.minimum = htonl (soa->minimum_ttl);
1012 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1013 (*off) += sizeof (sd);
1019 * Add an SRV record to the UDP packet at the given location.
1021 * @param dst where to write the SRV record
1022 * @param dst_len number of bytes in @a dst
1023 * @param off pointer to offset where to write the SRV information (increment by bytes used)
1024 * can also change if there was an error
1025 * @param srv SRV information to write
1026 * @return #GNUNET_SYSERR if @a srv is invalid
1027 * #GNUNET_NO if @a srv did not fit
1028 * #GNUNET_OK if @a srv was added to @a dst
1031 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1034 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1036 struct GNUNET_TUN_DnsSrvRecord sd;
1039 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1041 sd.prio = htons (srv->priority);
1042 sd.weight = htons (srv->weight);
1043 sd.port = htons (srv->port);
1044 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1045 (*off) += sizeof (sd);
1046 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1056 * Add a DNS record to the UDP packet at the given location.
1058 * @param dst where to write the query
1059 * @param dst_len number of bytes in @a dst
1060 * @param off pointer to offset where to write the query (increment by bytes used)
1061 * must not be changed if there is an error
1062 * @param record record to write
1063 * @return #GNUNET_SYSERR if @a record is invalid
1064 * #GNUNET_NO if @a record did not fit
1065 * #GNUNET_OK if @a record was added to @a dst
1068 add_record (char *dst,
1071 const struct GNUNET_DNSPARSER_Record *record)
1076 struct GNUNET_TUN_DnsRecordLine rl;
1079 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1080 dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine),
1083 if (GNUNET_OK != ret)
1085 /* '*off' is now the position where we will need to write the record line */
1087 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1088 switch (record->type)
1090 case GNUNET_DNSPARSER_TYPE_MX:
1091 ret = GNUNET_DNSPARSER_builder_add_mx (dst,
1096 case GNUNET_DNSPARSER_TYPE_CERT:
1097 ret = GNUNET_DNSPARSER_builder_add_cert (dst,
1102 case GNUNET_DNSPARSER_TYPE_SOA:
1103 ret = GNUNET_DNSPARSER_builder_add_soa (dst,
1108 case GNUNET_DNSPARSER_TYPE_NS:
1109 case GNUNET_DNSPARSER_TYPE_CNAME:
1110 case GNUNET_DNSPARSER_TYPE_PTR:
1111 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1114 record->data.hostname);
1116 case GNUNET_DNSPARSER_TYPE_SRV:
1117 ret = GNUNET_DNSPARSER_builder_add_srv (dst,
1123 if (pos + record->data.raw.data_len > dst_len)
1128 GNUNET_memcpy (&dst[pos],
1129 record->data.raw.data,
1130 record->data.raw.data_len);
1131 pos += record->data.raw.data_len;
1135 if (GNUNET_OK != ret)
1141 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1143 /* record data too long */
1147 rl.type = htons (record->type);
1148 rl.dns_traffic_class = htons (record->dns_traffic_class);
1149 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1150 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1151 GNUNET_memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
1158 * Given a DNS packet @a p, generate the corresponding UDP payload.
1159 * Note that we do not attempt to pack the strings with pointers
1160 * as this would complicate the code and this is about being
1161 * simple and secure, not fast, fancy and broken like bind.
1163 * @param p packet to pack
1164 * @param max maximum allowed size for the resulting UDP payload
1165 * @param buf set to a buffer with the packed message
1166 * @param buf_length set to the length of @a buf
1167 * @return #GNUNET_SYSERR if @a p is invalid
1168 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1169 * #GNUNET_OK if @a p was packed completely into @a buf
1172 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1177 struct GNUNET_TUN_DnsHeader dns;
1184 if ( (p->num_queries > UINT16_MAX) ||
1185 (p->num_answers > UINT16_MAX) ||
1186 (p->num_authority_records > UINT16_MAX) ||
1187 (p->num_additional_records > UINT16_MAX) )
1188 return GNUNET_SYSERR;
1190 dns.flags = p->flags;
1191 dns.query_count = htons (p->num_queries);
1192 dns.answer_rcount = htons (p->num_answers);
1193 dns.authority_rcount = htons (p->num_authority_records);
1194 dns.additional_rcount = htons (p->num_additional_records);
1196 off = sizeof (struct GNUNET_TUN_DnsHeader);
1198 for (i=0;i<p->num_queries;i++)
1200 ret = GNUNET_DNSPARSER_builder_add_query (tmp,
1204 if (GNUNET_SYSERR == ret)
1205 return GNUNET_SYSERR;
1206 if (GNUNET_NO == ret)
1208 dns.query_count = htons ((uint16_t) (i-1));
1213 for (i=0;i<p->num_answers;i++)
1215 ret = add_record (tmp,
1219 if (GNUNET_SYSERR == ret)
1220 return GNUNET_SYSERR;
1221 if (GNUNET_NO == ret)
1223 dns.answer_rcount = htons ((uint16_t) (i-1));
1228 for (i=0;i<p->num_authority_records;i++)
1230 ret = add_record (tmp,
1233 &p->authority_records[i]);
1234 if (GNUNET_SYSERR == ret)
1235 return GNUNET_SYSERR;
1236 if (GNUNET_NO == ret)
1238 dns.authority_rcount = htons ((uint16_t) (i-1));
1243 for (i=0;i<p->num_additional_records;i++)
1245 ret = add_record (tmp,
1248 &p->additional_records[i]);
1249 if (GNUNET_SYSERR == ret)
1250 return GNUNET_SYSERR;
1251 if (GNUNET_NO == ret)
1253 dns.additional_rcount = htons (i-1);
1259 if (GNUNET_YES == trc)
1260 dns.flags.message_truncated = 1;
1263 sizeof (struct GNUNET_TUN_DnsHeader));
1265 *buf = GNUNET_malloc (off);
1267 GNUNET_memcpy (*buf,
1270 if (GNUNET_YES == trc)
1277 * Convert a block of binary data to HEX.
1279 * @param data binary data to convert
1280 * @param data_size number of bytes in @a data
1281 * @return HEX string (lower case)
1284 GNUNET_DNSPARSER_bin_to_hex (const void *data,
1289 const uint8_t *idata;
1292 ret = GNUNET_malloc (data_size * 2 + 1);
1293 for (off = 0; off < data_size; off++)
1294 sprintf (&ret[off * 2],
1302 * Convert a HEX string to block of binary data.
1304 * @param hex HEX string to convert (may contain mixed case)
1305 * @param data where to write result, must be
1306 * at least `strlen(hex)/2` bytes long
1307 * @return number of bytes written to data
1310 GNUNET_DNSPARSER_hex_to_bin (const char *hex,
1319 data_size = strlen (hex) / 2;
1322 for (off = 0; off < data_size; off++)
1324 in[0] = tolower ((unsigned char) hex[off * 2]);
1325 in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
1326 if (1 != sscanf (in, "%x", &h))
1328 idata[off] = (uint8_t) h;
1334 /* end of dnsparser.c */