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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file dns/dnsparser.c
23 * @brief helper library to parse DNS packets.
24 * @author Philipp Toelke
25 * @author Christian Grothoff
32 #include "gnunet_util_lib.h"
33 #include "gnunet_dnsparser_lib.h"
34 #include "gnunet_tun_lib.h"
38 * Check if a label in UTF-8 format can be coded into valid IDNA.
39 * This can fail if the ASCII-conversion becomes longer than 63 characters.
41 * @param label label to check (UTF-8 string)
42 * @return #GNUNET_OK if the label can be converted to IDNA,
43 * #GNUNET_SYSERR if the label is not valid for DNS names
46 GNUNET_DNSPARSER_check_label (const char *label)
51 if (NULL != strchr (label, '.'))
52 return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */
54 idna_to_ascii_8z (label, &output, IDNA_ALLOW_UNASSIGNED))
56 slen = strlen (output);
62 return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK;
67 * Check if a label in UTF-8 format can be coded into valid IDNA.
68 * This can fail if the ASCII-conversion becomes longer than 253 characters.
70 * @param name name to check (UTF-8 string)
71 * @return #GNUNET_OK if the label can be converted to IDNA,
72 * #GNUNET_SYSERR if the label is not valid for DNS names
75 GNUNET_DNSPARSER_check_name (const char *name)
82 ldup = GNUNET_strdup (name);
83 for (tok = strtok (ldup, "."); NULL != tok; tok = strtok (NULL, "."))
85 GNUNET_DNSPARSER_check_label (tok))
92 idna_to_ascii_8z (name, &output, IDNA_ALLOW_UNASSIGNED))
94 slen = strlen (output);
100 return (slen > 253) ? GNUNET_SYSERR : GNUNET_OK;
105 * Free SOA information record.
107 * @param soa record to free
110 GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
114 GNUNET_free_non_null (soa->mname);
115 GNUNET_free_non_null (soa->rname);
121 * Free CERT information record.
123 * @param cert record to free
126 GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert)
130 GNUNET_free_non_null (cert->certificate_data);
136 * Free SRV information record.
138 * @param srv record to free
141 GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
145 GNUNET_free_non_null (srv->target);
151 * Free MX information record.
153 * @param mx record to free
156 GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
160 GNUNET_free_non_null (mx->mxhost);
166 * Free the given DNS record.
168 * @param r record to free
171 GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
173 GNUNET_free_non_null (r->name);
176 case GNUNET_DNSPARSER_TYPE_MX:
177 GNUNET_DNSPARSER_free_mx (r->data.mx);
179 case GNUNET_DNSPARSER_TYPE_SOA:
180 GNUNET_DNSPARSER_free_soa (r->data.soa);
182 case GNUNET_DNSPARSER_TYPE_SRV:
183 GNUNET_DNSPARSER_free_srv (r->data.srv);
185 case GNUNET_DNSPARSER_TYPE_CERT:
186 GNUNET_DNSPARSER_free_cert (r->data.cert);
188 case GNUNET_DNSPARSER_TYPE_NS:
189 case GNUNET_DNSPARSER_TYPE_CNAME:
190 case GNUNET_DNSPARSER_TYPE_PTR:
191 GNUNET_free_non_null (r->data.hostname);
194 GNUNET_free_non_null (r->data.raw.data);
201 * Parse name inside of a DNS query or record.
203 * @param udp_payload entire UDP payload
204 * @param udp_payload_length length of @a udp_payload
205 * @param off pointer to the offset of the name to parse in the udp_payload (to be
206 * incremented by the size of the name)
207 * @param depth current depth of our recursion (to prevent stack overflow)
208 * @return name as 0-terminated C string on success, NULL if the payload is malformed
211 parse_name (const char *udp_payload,
212 size_t udp_payload_length,
216 const uint8_t *input = (const uint8_t *) udp_payload;
225 ret = GNUNET_strdup ("");
228 if (*off >= udp_payload_length)
241 if (*off + 1 + len > udp_payload_length)
246 GNUNET_asprintf (&tmp,
249 &udp_payload[*off + 1]);
251 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
254 _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
258 GNUNET_asprintf (&tmp,
262 &udp_payload[*off + 1]);
267 GNUNET_asprintf (&tmp,
281 else if ((64 | 128) == (len & (64 | 128)) )
286 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
288 /* pointer to string */
289 if (*off + 1 > udp_payload_length)
294 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
295 xstr = parse_name (udp_payload,
304 GNUNET_asprintf (&tmp,
311 if (strlen (ret) > udp_payload_length)
314 goto error; /* we are looping (building an infinite string) */
317 /* pointers always terminate names */
322 /* neither pointer nor inline string, not supported... */
328 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
338 * Parse name inside of a DNS query or record.
340 * @param udp_payload entire UDP payload
341 * @param udp_payload_length length of @a udp_payload
342 * @param off pointer to the offset of the name to parse in the udp_payload (to be
343 * incremented by the size of the name)
344 * @return name as 0-terminated C string on success, NULL if the payload is malformed
347 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
348 size_t udp_payload_length,
351 return parse_name (udp_payload, udp_payload_length, off, 0);
356 * Parse a DNS query entry.
358 * @param udp_payload entire UDP payload
359 * @param udp_payload_length length of @a udp_payload
360 * @param off pointer to the offset of the query to parse in the udp_payload (to be
361 * incremented by the size of the query)
362 * @param q where to write the query information
363 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
366 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
367 size_t udp_payload_length,
369 struct GNUNET_DNSPARSER_Query *q)
372 struct GNUNET_TUN_DnsQueryLine ql;
374 name = GNUNET_DNSPARSER_parse_name (udp_payload,
380 return GNUNET_SYSERR;
383 if (*off + sizeof (struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
386 return GNUNET_SYSERR;
388 GNUNET_memcpy (&ql, &udp_payload[*off], sizeof (ql));
390 q->type = ntohs (ql.type);
391 q->dns_traffic_class = ntohs (ql.dns_traffic_class);
397 * Parse a DNS SOA record.
399 * @param udp_payload reference to UDP packet
400 * @param udp_payload_length length of @a udp_payload
401 * @param off pointer to the offset of the query to parse in the SOA record (to be
402 * incremented by the size of the record), unchanged on error
403 * @return the parsed SOA record, NULL on error
405 struct GNUNET_DNSPARSER_SoaRecord *
406 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
407 size_t udp_payload_length,
410 struct GNUNET_DNSPARSER_SoaRecord *soa;
411 struct GNUNET_TUN_DnsSoaRecord soa_bin;
415 soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
416 soa->mname = GNUNET_DNSPARSER_parse_name (udp_payload,
419 soa->rname = GNUNET_DNSPARSER_parse_name (udp_payload,
422 if ( (NULL == soa->mname) ||
423 (NULL == soa->rname) ||
424 (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length) )
427 GNUNET_DNSPARSER_free_soa (soa);
431 GNUNET_memcpy (&soa_bin,
433 sizeof (struct GNUNET_TUN_DnsSoaRecord));
434 soa->serial = ntohl (soa_bin.serial);
435 soa->refresh = ntohl (soa_bin.refresh);
436 soa->retry = ntohl (soa_bin.retry);
437 soa->expire = ntohl (soa_bin.expire);
438 soa->minimum_ttl = ntohl (soa_bin.minimum);
439 (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
445 * Parse a DNS MX record.
447 * @param udp_payload reference to UDP packet
448 * @param udp_payload_length length of @a udp_payload
449 * @param off pointer to the offset of the query to parse in the MX record (to be
450 * incremented by the size of the record), unchanged on error
451 * @return the parsed MX record, NULL on error
453 struct GNUNET_DNSPARSER_MxRecord *
454 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
455 size_t udp_payload_length,
458 struct GNUNET_DNSPARSER_MxRecord *mx;
463 if (*off + sizeof (uint16_t) > udp_payload_length)
468 GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
469 (*off) += sizeof (uint16_t);
470 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
471 mx->preference = ntohs (mxpref);
472 mx->mxhost = GNUNET_DNSPARSER_parse_name (udp_payload,
475 if (NULL == mx->mxhost)
478 GNUNET_DNSPARSER_free_mx (mx);
487 * Parse a DNS SRV record.
489 * @param udp_payload reference to UDP packet
490 * @param udp_payload_length length of @a udp_payload
491 * @param off pointer to the offset of the query to parse in the SRV record (to be
492 * incremented by the size of the record), unchanged on error
493 * @return the parsed SRV record, NULL on error
495 struct GNUNET_DNSPARSER_SrvRecord *
496 GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
497 size_t udp_payload_length,
500 struct GNUNET_DNSPARSER_SrvRecord *srv;
501 struct GNUNET_TUN_DnsSrvRecord srv_bin;
505 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
507 GNUNET_memcpy (&srv_bin,
509 sizeof (struct GNUNET_TUN_DnsSrvRecord));
510 (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
511 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
512 srv->priority = ntohs (srv_bin.prio);
513 srv->weight = ntohs (srv_bin.weight);
514 srv->port = ntohs (srv_bin.port);
515 srv->target = GNUNET_DNSPARSER_parse_name (udp_payload,
518 if (NULL == srv->target)
520 GNUNET_DNSPARSER_free_srv (srv);
529 * Parse a DNS CERT record.
531 * @param udp_payload reference to UDP packet
532 * @param udp_payload_length length of @a udp_payload
533 * @param off pointer to the offset of the query to parse in the CERT record (to be
534 * incremented by the size of the record), unchanged on error
535 * @return the parsed CERT record, NULL on error
537 struct GNUNET_DNSPARSER_CertRecord *
538 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
539 size_t udp_payload_length,
542 struct GNUNET_DNSPARSER_CertRecord *cert;
543 struct GNUNET_TUN_DnsCertRecord dcert;
545 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
550 GNUNET_memcpy (&dcert, &udp_payload[*off], 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;
691 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
693 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
694 off = sizeof (struct GNUNET_TUN_DnsHeader);
695 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
696 p->flags = dns->flags;
698 n = ntohs (dns->query_count);
701 p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
705 GNUNET_DNSPARSER_parse_query (udp_payload,
711 n = ntohs (dns->answer_rcount);
714 p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
718 GNUNET_DNSPARSER_parse_record (udp_payload,
724 n = ntohs (dns->authority_rcount);
727 p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
728 p->num_authority_records = n;
731 GNUNET_DNSPARSER_parse_record (udp_payload,
734 &p->authority_records[i]))
737 n = ntohs (dns->additional_rcount);
740 p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
741 p->num_additional_records = n;
744 GNUNET_DNSPARSER_parse_record (udp_payload,
747 &p->additional_records[i]))
753 GNUNET_DNSPARSER_free_packet (p);
759 * Free memory taken by a packet.
761 * @param p packet to free
764 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
768 for (i=0;i<p->num_queries;i++)
769 GNUNET_free_non_null (p->queries[i].name);
770 GNUNET_free_non_null (p->queries);
771 for (i=0;i<p->num_answers;i++)
772 GNUNET_DNSPARSER_free_record (&p->answers[i]);
773 GNUNET_free_non_null (p->answers);
774 for (i=0;i<p->num_authority_records;i++)
775 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
776 GNUNET_free_non_null (p->authority_records);
777 for (i=0;i<p->num_additional_records;i++)
778 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
779 GNUNET_free_non_null (p->additional_records);
784 /* ********************** DNS packet assembly code **************** */
788 * Add a DNS name to the UDP packet at the given location, converting
789 * the name to IDNA notation as necessary.
791 * @param dst where to write the name (UDP packet)
792 * @param dst_len number of bytes in @a dst
793 * @param off pointer to offset where to write the name (increment by bytes used)
794 * must not be changed if there is an error
795 * @param name name to write
796 * @return #GNUNET_SYSERR if @a name is invalid
797 * #GNUNET_NO if @a name did not fit
798 * #GNUNET_OK if @a name was added to @a dst
801 GNUNET_DNSPARSER_builder_add_name (char *dst,
807 const char *idna_name;
815 return GNUNET_SYSERR;
818 (rc = idna_to_ascii_8z (name, &idna_start, 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], idna_name, len);
846 idna_name += len + 1; /* also skip dot */
849 dst[pos++] = '\0'; /* terminator */
852 idn_free (idna_start);
859 idn_free (idna_start);
868 * Add a DNS query to the UDP packet at the given location.
870 * @param dst where to write the query
871 * @param dst_len number of bytes in @a dst
872 * @param off pointer to offset where to write the query (increment by bytes used)
873 * must not be changed if there is an error
874 * @param query query to write
875 * @return #GNUNET_SYSERR if @a query is invalid
876 * #GNUNET_NO if @a query did not fit
877 * #GNUNET_OK if @a query was added to @a dst
880 GNUNET_DNSPARSER_builder_add_query (char *dst,
883 const struct GNUNET_DNSPARSER_Query *query)
886 struct GNUNET_TUN_DnsQueryLine ql;
888 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
889 if (ret != GNUNET_OK)
891 ql.type = htons (query->type);
892 ql.dns_traffic_class = htons (query->dns_traffic_class);
893 GNUNET_memcpy (&dst[*off], &ql, sizeof (ql));
894 (*off) += sizeof (ql);
900 * Add an MX record to the UDP packet at the given location.
902 * @param dst where to write the mx record
903 * @param dst_len number of bytes in @a dst
904 * @param off pointer to offset where to write the mx information (increment by bytes used);
905 * can also change if there was an error
906 * @param mx mx information to write
907 * @return #GNUNET_SYSERR if @a mx is invalid
908 * #GNUNET_NO if @a mx did not fit
909 * #GNUNET_OK if @a mx was added to @a dst
912 GNUNET_DNSPARSER_builder_add_mx (char *dst,
915 const struct GNUNET_DNSPARSER_MxRecord *mx)
919 if (*off + sizeof (uint16_t) > dst_len)
921 mxpref = htons (mx->preference);
922 GNUNET_memcpy (&dst[*off],
925 (*off) += sizeof (mxpref);
926 return GNUNET_DNSPARSER_builder_add_name (dst,
934 * Add a CERT record to the UDP packet at the given location.
936 * @param dst where to write the CERT record
937 * @param dst_len number of bytes in @a dst
938 * @param off pointer to offset where to write the CERT information (increment by bytes used);
939 * can also change if there was an error
940 * @param cert CERT information to write
941 * @return #GNUNET_SYSERR if @a cert is invalid
942 * #GNUNET_NO if @a cert did not fit
943 * #GNUNET_OK if @a cert was added to @a dst
946 GNUNET_DNSPARSER_builder_add_cert (char *dst,
949 const struct GNUNET_DNSPARSER_CertRecord *cert)
951 struct GNUNET_TUN_DnsCertRecord dcert;
953 if ( (cert->cert_type > UINT16_MAX) ||
954 (cert->cert_tag > UINT16_MAX) ||
955 (cert->algorithm > UINT8_MAX) )
958 return GNUNET_SYSERR;
960 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size > dst_len)
962 dcert.cert_type = htons ((uint16_t) cert->cert_type);
963 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
964 dcert.algorithm = (uint8_t) cert->algorithm;
965 GNUNET_memcpy (&dst[*off], &dcert, sizeof (dcert));
966 (*off) += sizeof (dcert);
967 GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
968 (*off) += cert->certificate_size;
974 * Add an SOA record to the UDP packet at the given location.
976 * @param dst where to write the SOA record
977 * @param dst_len number of bytes in @a dst
978 * @param off pointer to offset where to write the SOA information (increment by bytes used)
979 * can also change if there was an error
980 * @param soa SOA information to write
981 * @return #GNUNET_SYSERR if @a soa is invalid
982 * #GNUNET_NO if @a soa did not fit
983 * #GNUNET_OK if @a soa was added to @a dst
986 GNUNET_DNSPARSER_builder_add_soa (char *dst,
989 const struct GNUNET_DNSPARSER_SoaRecord *soa)
991 struct GNUNET_TUN_DnsSoaRecord sd;
994 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
998 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1003 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1005 sd.serial = htonl (soa->serial);
1006 sd.refresh = htonl (soa->refresh);
1007 sd.retry = htonl (soa->retry);
1008 sd.expire = htonl (soa->expire);
1009 sd.minimum = htonl (soa->minimum_ttl);
1010 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1011 (*off) += sizeof (sd);
1017 * Add an SRV record to the UDP packet at the given location.
1019 * @param dst where to write the SRV record
1020 * @param dst_len number of bytes in @a dst
1021 * @param off pointer to offset where to write the SRV information (increment by bytes used)
1022 * can also change if there was an error
1023 * @param srv SRV information to write
1024 * @return #GNUNET_SYSERR if @a srv is invalid
1025 * #GNUNET_NO if @a srv did not fit
1026 * #GNUNET_OK if @a srv was added to @a dst
1029 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1032 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1034 struct GNUNET_TUN_DnsSrvRecord sd;
1037 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1039 sd.prio = htons (srv->priority);
1040 sd.weight = htons (srv->weight);
1041 sd.port = htons (srv->port);
1042 GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1043 (*off) += sizeof (sd);
1044 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1054 * Add a DNS record to the UDP packet at the given location.
1056 * @param dst where to write the query
1057 * @param dst_len number of bytes in @a dst
1058 * @param off pointer to offset where to write the query (increment by bytes used)
1059 * must not be changed if there is an error
1060 * @param record record to write
1061 * @return #GNUNET_SYSERR if @a record is invalid
1062 * #GNUNET_NO if @a record did not fit
1063 * #GNUNET_OK if @a record was added to @a dst
1066 add_record (char *dst,
1069 const struct GNUNET_DNSPARSER_Record *record)
1074 struct GNUNET_TUN_DnsRecordLine rl;
1077 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1078 dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine),
1081 if (GNUNET_OK != ret)
1083 /* '*off' is now the position where we will need to write the record line */
1085 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1086 switch (record->type)
1088 case GNUNET_DNSPARSER_TYPE_MX:
1089 ret = GNUNET_DNSPARSER_builder_add_mx (dst,
1094 case GNUNET_DNSPARSER_TYPE_CERT:
1095 ret = GNUNET_DNSPARSER_builder_add_cert (dst,
1100 case GNUNET_DNSPARSER_TYPE_SOA:
1101 ret = GNUNET_DNSPARSER_builder_add_soa (dst,
1106 case GNUNET_DNSPARSER_TYPE_NS:
1107 case GNUNET_DNSPARSER_TYPE_CNAME:
1108 case GNUNET_DNSPARSER_TYPE_PTR:
1109 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1112 record->data.hostname);
1114 case GNUNET_DNSPARSER_TYPE_SRV:
1115 ret = GNUNET_DNSPARSER_builder_add_srv (dst,
1121 if (pos + record->data.raw.data_len > dst_len)
1126 GNUNET_memcpy (&dst[pos],
1127 record->data.raw.data,
1128 record->data.raw.data_len);
1129 pos += record->data.raw.data_len;
1133 if (GNUNET_OK != ret)
1139 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1141 /* record data too long */
1145 rl.type = htons (record->type);
1146 rl.dns_traffic_class = htons (record->dns_traffic_class);
1147 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1148 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1149 GNUNET_memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
1156 * Given a DNS packet @a p, generate the corresponding UDP payload.
1157 * Note that we do not attempt to pack the strings with pointers
1158 * as this would complicate the code and this is about being
1159 * simple and secure, not fast, fancy and broken like bind.
1161 * @param p packet to pack
1162 * @param max maximum allowed size for the resulting UDP payload
1163 * @param buf set to a buffer with the packed message
1164 * @param buf_length set to the length of @a buf
1165 * @return #GNUNET_SYSERR if @a p is invalid
1166 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1167 * #GNUNET_OK if @a p was packed completely into @a buf
1170 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1175 struct GNUNET_TUN_DnsHeader dns;
1182 if ( (p->num_queries > UINT16_MAX) ||
1183 (p->num_answers > UINT16_MAX) ||
1184 (p->num_authority_records > UINT16_MAX) ||
1185 (p->num_additional_records > UINT16_MAX) )
1186 return GNUNET_SYSERR;
1188 dns.flags = p->flags;
1189 dns.query_count = htons (p->num_queries);
1190 dns.answer_rcount = htons (p->num_answers);
1191 dns.authority_rcount = htons (p->num_authority_records);
1192 dns.additional_rcount = htons (p->num_additional_records);
1194 off = sizeof (struct GNUNET_TUN_DnsHeader);
1196 for (i=0;i<p->num_queries;i++)
1198 ret = GNUNET_DNSPARSER_builder_add_query (tmp,
1202 if (GNUNET_SYSERR == ret)
1203 return GNUNET_SYSERR;
1204 if (GNUNET_NO == ret)
1206 dns.query_count = htons ((uint16_t) (i-1));
1211 for (i=0;i<p->num_answers;i++)
1213 ret = add_record (tmp,
1217 if (GNUNET_SYSERR == ret)
1218 return GNUNET_SYSERR;
1219 if (GNUNET_NO == ret)
1221 dns.answer_rcount = htons ((uint16_t) (i-1));
1226 for (i=0;i<p->num_authority_records;i++)
1228 ret = add_record (tmp,
1231 &p->authority_records[i]);
1232 if (GNUNET_SYSERR == ret)
1233 return GNUNET_SYSERR;
1234 if (GNUNET_NO == ret)
1236 dns.authority_rcount = htons ((uint16_t) (i-1));
1241 for (i=0;i<p->num_additional_records;i++)
1243 ret = add_record (tmp,
1246 &p->additional_records[i]);
1247 if (GNUNET_SYSERR == ret)
1248 return GNUNET_SYSERR;
1249 if (GNUNET_NO == ret)
1251 dns.additional_rcount = htons (i-1);
1257 if (GNUNET_YES == trc)
1258 dns.flags.message_truncated = 1;
1261 sizeof (struct GNUNET_TUN_DnsHeader));
1263 *buf = GNUNET_malloc (off);
1265 GNUNET_memcpy (*buf,
1268 if (GNUNET_YES == trc)
1275 * Convert a block of binary data to HEX.
1277 * @param data binary data to convert
1278 * @param data_size number of bytes in @a data
1279 * @return HEX string (lower case)
1282 GNUNET_DNSPARSER_bin_to_hex (const void *data,
1287 const uint8_t *idata;
1290 ret = GNUNET_malloc (data_size * 2 + 1);
1291 for (off = 0; off < data_size; off++)
1292 sprintf (&ret[off * 2],
1300 * Convert a HEX string to block of binary data.
1302 * @param hex HEX string to convert (may contain mixed case)
1303 * @param data where to write result, must be
1304 * at least `strlen(hex)/2` bytes long
1305 * @return number of bytes written to data
1308 GNUNET_DNSPARSER_hex_to_bin (const char *hex,
1317 data_size = strlen (hex) / 2;
1320 for (off = 0; off < data_size; off++)
1322 in[0] = tolower ((unsigned char) hex[off * 2]);
1323 in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
1324 if (1 != sscanf (in, "%x", &h))
1326 idata[off] = (uint8_t) h;
1332 /* end of dnsparser.c */