2 This file is part of GNUnet
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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);
146 GNUNET_free_non_null (srv->domain_name);
147 GNUNET_free_non_null (srv->proto);
148 GNUNET_free_non_null (srv->service);
154 * Free MX information record.
156 * @param mx record to free
159 GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
163 GNUNET_free_non_null (mx->mxhost);
169 * Free the given DNS record.
171 * @param r record to free
174 GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
176 GNUNET_free_non_null (r->name);
179 case GNUNET_DNSPARSER_TYPE_MX:
180 GNUNET_DNSPARSER_free_mx (r->data.mx);
182 case GNUNET_DNSPARSER_TYPE_SOA:
183 GNUNET_DNSPARSER_free_soa (r->data.soa);
185 case GNUNET_DNSPARSER_TYPE_SRV:
186 GNUNET_DNSPARSER_free_srv (r->data.srv);
188 case GNUNET_DNSPARSER_TYPE_CERT:
189 GNUNET_DNSPARSER_free_cert (r->data.cert);
191 case GNUNET_DNSPARSER_TYPE_NS:
192 case GNUNET_DNSPARSER_TYPE_CNAME:
193 case GNUNET_DNSPARSER_TYPE_PTR:
194 GNUNET_free_non_null (r->data.hostname);
197 GNUNET_free_non_null (r->data.raw.data);
204 * Parse name inside of a DNS query or record.
206 * @param udp_payload entire UDP payload
207 * @param udp_payload_length length of @a udp_payload
208 * @param off pointer to the offset of the name to parse in the udp_payload (to be
209 * incremented by the size of the name)
210 * @param depth current depth of our recursion (to prevent stack overflow)
211 * @return name as 0-terminated C string on success, NULL if the payload is malformed
214 parse_name (const char *udp_payload,
215 size_t udp_payload_length,
219 const uint8_t *input = (const uint8_t *) udp_payload;
228 ret = GNUNET_strdup ("");
231 if (*off >= udp_payload_length)
244 if (*off + 1 + len > udp_payload_length)
249 GNUNET_asprintf (&tmp,
252 &udp_payload[*off + 1]);
254 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
256 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
257 _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
261 GNUNET_asprintf (&tmp,
265 &udp_payload[*off + 1]);
270 GNUNET_asprintf (&tmp,
284 else if ((64 | 128) == (len & (64 | 128)) )
289 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
291 /* pointer to string */
292 if (*off + 1 > udp_payload_length)
297 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
298 xstr = parse_name (udp_payload,
307 GNUNET_asprintf (&tmp,
314 if (strlen (ret) > udp_payload_length)
317 goto error; /* we are looping (building an infinite string) */
320 /* pointers always terminate names */
325 /* neither pointer nor inline string, not supported... */
331 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
341 * Parse name inside of a DNS query or record.
343 * @param udp_payload entire UDP payload
344 * @param udp_payload_length length of @a udp_payload
345 * @param off pointer to the offset of the name to parse in the udp_payload (to be
346 * incremented by the size of the name)
347 * @return name as 0-terminated C string on success, NULL if the payload is malformed
350 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
351 size_t udp_payload_length,
354 return parse_name (udp_payload, udp_payload_length, off, 0);
359 * Parse a DNS query entry.
361 * @param udp_payload entire UDP payload
362 * @param udp_payload_length length of @a udp_payload
363 * @param off pointer to the offset of the query to parse in the udp_payload (to be
364 * incremented by the size of the query)
365 * @param q where to write the query information
366 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
369 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
370 size_t udp_payload_length,
372 struct GNUNET_DNSPARSER_Query *q)
375 struct GNUNET_TUN_DnsQueryLine ql;
377 name = GNUNET_DNSPARSER_parse_name (udp_payload,
383 return GNUNET_SYSERR;
386 if (*off + sizeof (struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
389 return GNUNET_SYSERR;
391 memcpy (&ql, &udp_payload[*off], sizeof (ql));
393 q->type = ntohs (ql.type);
394 q->dns_traffic_class = ntohs (ql.dns_traffic_class);
400 * Parse a DNS SOA record.
402 * @param udp_payload reference to UDP packet
403 * @param udp_payload_length length of @a udp_payload
404 * @param off pointer to the offset of the query to parse in the SOA record (to be
405 * incremented by the size of the record), unchanged on error
406 * @return the parsed SOA record, NULL on error
408 struct GNUNET_DNSPARSER_SoaRecord *
409 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
410 size_t udp_payload_length,
413 struct GNUNET_DNSPARSER_SoaRecord *soa;
414 struct GNUNET_TUN_DnsSoaRecord soa_bin;
418 soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
419 soa->mname = GNUNET_DNSPARSER_parse_name (udp_payload,
422 soa->rname = GNUNET_DNSPARSER_parse_name (udp_payload,
425 if ( (NULL == soa->mname) ||
426 (NULL == soa->rname) ||
427 (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length) )
430 GNUNET_DNSPARSER_free_soa (soa);
436 sizeof (struct GNUNET_TUN_DnsSoaRecord));
437 soa->serial = ntohl (soa_bin.serial);
438 soa->refresh = ntohl (soa_bin.refresh);
439 soa->retry = ntohl (soa_bin.retry);
440 soa->expire = ntohl (soa_bin.expire);
441 soa->minimum_ttl = ntohl (soa_bin.minimum);
442 (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
448 * Parse a DNS MX record.
450 * @param udp_payload reference to UDP packet
451 * @param udp_payload_length length of @a udp_payload
452 * @param off pointer to the offset of the query to parse in the MX record (to be
453 * incremented by the size of the record), unchanged on error
454 * @return the parsed MX record, NULL on error
456 struct GNUNET_DNSPARSER_MxRecord *
457 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
458 size_t udp_payload_length,
461 struct GNUNET_DNSPARSER_MxRecord *mx;
466 if (*off + sizeof (uint16_t) > udp_payload_length)
471 memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
472 (*off) += sizeof (uint16_t);
473 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
474 mx->preference = ntohs (mxpref);
475 mx->mxhost = GNUNET_DNSPARSER_parse_name (udp_payload,
478 if (NULL == mx->mxhost)
481 GNUNET_DNSPARSER_free_mx (mx);
490 * Parse a DNS SRV record.
492 * @param r_name name of the SRV record
493 * @param udp_payload reference to UDP packet
494 * @param udp_payload_length length of @a udp_payload
495 * @param off pointer to the offset of the query to parse in the SRV record (to be
496 * incremented by the size of the record), unchanged on error
497 * @return the parsed SRV record, NULL on error
499 struct GNUNET_DNSPARSER_SrvRecord *
500 GNUNET_DNSPARSER_parse_srv (const char *r_name,
501 const char *udp_payload,
502 size_t udp_payload_length,
505 struct GNUNET_DNSPARSER_SrvRecord *srv;
506 struct GNUNET_TUN_DnsSrvRecord srv_bin;
512 return NULL; /* all valid srv names must start with "_" */
513 if (NULL == strstr (r_name, "._"))
514 return NULL; /* necessary string from "._$PROTO" not present */
516 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
520 sizeof (struct GNUNET_TUN_DnsSrvRecord));
521 (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
522 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
523 srv->priority = ntohs (srv_bin.prio);
524 srv->weight = ntohs (srv_bin.weight);
525 srv->port = ntohs (srv_bin.port);
526 /* parse 'data.hostname' into components, which are
527 "_$SERVICE._$PROTO.$DOMAIN_NAME" */
528 ndup = GNUNET_strdup (r_name);
529 tok = strtok (ndup, ".");
530 GNUNET_assert (NULL != tok);
531 GNUNET_assert ('_' == *tok);
532 srv->service = GNUNET_strdup (&tok[1]);
533 tok = strtok (NULL, ".");
534 if ( (NULL == tok) || ('_' != *tok) )
536 GNUNET_DNSPARSER_free_srv (srv);
541 srv->proto = GNUNET_strdup (&tok[1]);
542 tok = strtok (NULL, ".");
545 GNUNET_DNSPARSER_free_srv (srv);
550 srv->domain_name = GNUNET_strdup (tok);
552 srv->target = GNUNET_DNSPARSER_parse_name (udp_payload,
555 if (NULL == srv->target)
557 GNUNET_DNSPARSER_free_srv (srv);
566 * Parse a DNS CERT record.
568 * @param udp_payload reference to UDP packet
569 * @param udp_payload_length length of @a udp_payload
570 * @param off pointer to the offset of the query to parse in the CERT record (to be
571 * incremented by the size of the record), unchanged on error
572 * @return the parsed CERT record, NULL on error
574 struct GNUNET_DNSPARSER_CertRecord *
575 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
576 size_t udp_payload_length,
579 struct GNUNET_DNSPARSER_CertRecord *cert;
580 struct GNUNET_TUN_DnsCertRecord dcert;
582 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
587 memcpy (&dcert, &udp_payload[*off], sizeof (struct GNUNET_TUN_DnsCertRecord));
588 (*off) += sizeof (sizeof (struct GNUNET_TUN_DnsCertRecord));
589 cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
590 cert->cert_type = ntohs (dcert.cert_type);
591 cert->cert_tag = ntohs (dcert.cert_tag);
592 cert->algorithm = dcert.algorithm;
593 cert->certificate_size = udp_payload_length - (*off);
594 cert->certificate_data = GNUNET_malloc (cert->certificate_size);
595 memcpy (cert->certificate_data,
597 cert->certificate_size);
598 (*off) += cert->certificate_size;
604 * Parse a DNS record entry.
606 * @param udp_payload entire UDP payload
607 * @param udp_payload_length length of @a udp_payload
608 * @param off pointer to the offset of the record to parse in the udp_payload (to be
609 * incremented by the size of the record)
610 * @param r where to write the record information
611 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
614 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
615 size_t udp_payload_length,
617 struct GNUNET_DNSPARSER_Record *r)
620 struct GNUNET_TUN_DnsRecordLine rl;
624 name = GNUNET_DNSPARSER_parse_name (udp_payload,
630 return GNUNET_SYSERR;
633 if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
636 return GNUNET_SYSERR;
638 memcpy (&rl, &udp_payload[*off], sizeof (rl));
639 (*off) += sizeof (rl);
640 r->type = ntohs (rl.type);
641 r->dns_traffic_class = ntohs (rl.dns_traffic_class);
642 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
644 data_len = ntohs (rl.data_len);
645 if (*off + data_len > udp_payload_length)
648 return GNUNET_SYSERR;
653 case GNUNET_DNSPARSER_TYPE_NS:
654 case GNUNET_DNSPARSER_TYPE_CNAME:
655 case GNUNET_DNSPARSER_TYPE_PTR:
656 r->data.hostname = GNUNET_DNSPARSER_parse_name (udp_payload,
659 if ( (NULL == r->data.hostname) ||
660 (old_off + data_len != *off) )
661 return GNUNET_SYSERR;
663 case GNUNET_DNSPARSER_TYPE_SOA:
664 r->data.soa = GNUNET_DNSPARSER_parse_soa (udp_payload,
667 if ( (NULL == r->data.soa) ||
668 (old_off + data_len != *off) )
671 return GNUNET_SYSERR;
674 case GNUNET_DNSPARSER_TYPE_MX:
675 r->data.mx = GNUNET_DNSPARSER_parse_mx (udp_payload,
678 if ( (NULL == r->data.mx) ||
679 (old_off + data_len != *off) )
682 return GNUNET_SYSERR;
685 case GNUNET_DNSPARSER_TYPE_SRV:
686 r->data.srv = GNUNET_DNSPARSER_parse_srv (r->name,
690 if ( (NULL == r->data.srv) ||
691 (old_off + data_len != *off) )
694 return GNUNET_SYSERR;
698 r->data.raw.data = GNUNET_malloc (data_len);
699 r->data.raw.data_len = data_len;
700 memcpy (r->data.raw.data, &udp_payload[*off], data_len);
709 * Parse a UDP payload of a DNS packet in to a nice struct for further
710 * processing and manipulation.
712 * @param udp_payload wire-format of the DNS packet
713 * @param udp_payload_length number of bytes in @a udp_payload
714 * @return NULL on error, otherwise the parsed packet
716 struct GNUNET_DNSPARSER_Packet *
717 GNUNET_DNSPARSER_parse (const char *udp_payload,
718 size_t udp_payload_length)
720 struct GNUNET_DNSPARSER_Packet *p;
721 const struct GNUNET_TUN_DnsHeader *dns;
726 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
728 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
729 off = sizeof (struct GNUNET_TUN_DnsHeader);
730 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
731 p->flags = dns->flags;
733 n = ntohs (dns->query_count);
736 p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
740 GNUNET_DNSPARSER_parse_query (udp_payload,
746 n = ntohs (dns->answer_rcount);
749 p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
753 GNUNET_DNSPARSER_parse_record (udp_payload,
759 n = ntohs (dns->authority_rcount);
762 p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
763 p->num_authority_records = n;
766 GNUNET_DNSPARSER_parse_record (udp_payload,
769 &p->authority_records[i]))
772 n = ntohs (dns->additional_rcount);
775 p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
776 p->num_additional_records = n;
779 GNUNET_DNSPARSER_parse_record (udp_payload,
782 &p->additional_records[i]))
788 GNUNET_DNSPARSER_free_packet (p);
794 * Free memory taken by a packet.
796 * @param p packet to free
799 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
803 for (i=0;i<p->num_queries;i++)
804 GNUNET_free_non_null (p->queries[i].name);
805 GNUNET_free_non_null (p->queries);
806 for (i=0;i<p->num_answers;i++)
807 GNUNET_DNSPARSER_free_record (&p->answers[i]);
808 GNUNET_free_non_null (p->answers);
809 for (i=0;i<p->num_authority_records;i++)
810 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
811 GNUNET_free_non_null (p->authority_records);
812 for (i=0;i<p->num_additional_records;i++)
813 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
814 GNUNET_free_non_null (p->additional_records);
819 /* ********************** DNS packet assembly code **************** */
823 * Add a DNS name to the UDP packet at the given location, converting
824 * the name to IDNA notation as necessary.
826 * @param dst where to write the name (UDP packet)
827 * @param dst_len number of bytes in @a dst
828 * @param off pointer to offset where to write the name (increment by bytes used)
829 * must not be changed if there is an error
830 * @param name name to write
831 * @return #GNUNET_SYSERR if @a name is invalid
832 * #GNUNET_NO if @a name did not fit
833 * #GNUNET_OK if @a name was added to @a dst
836 GNUNET_DNSPARSER_builder_add_name (char *dst,
842 const char *idna_name;
850 return GNUNET_SYSERR;
853 (rc = idna_to_ascii_8z (name, &idna_start, IDNA_ALLOW_UNASSIGNED)))
855 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
856 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
861 idna_name = idna_start;
863 if (start + strlen (idna_name) + 2 > dst_len)
868 dot = strchr (idna_name, '.');
870 len = strlen (idna_name);
872 len = dot - idna_name;
873 if ( (len >= 64) || (0 == len) )
876 goto fail; /* segment too long or empty */
878 dst[pos++] = (char) (uint8_t) len;
879 memcpy (&dst[pos], idna_name, len);
881 idna_name += len + 1; /* also skip dot */
884 dst[pos++] = '\0'; /* terminator */
887 idn_free (idna_start);
894 idn_free (idna_start);
903 * Add a DNS query to the UDP packet at the given location.
905 * @param dst where to write the query
906 * @param dst_len number of bytes in @a dst
907 * @param off pointer to offset where to write the query (increment by bytes used)
908 * must not be changed if there is an error
909 * @param query query to write
910 * @return #GNUNET_SYSERR if @a query is invalid
911 * #GNUNET_NO if @a query did not fit
912 * #GNUNET_OK if @a query was added to @a dst
915 GNUNET_DNSPARSER_builder_add_query (char *dst,
918 const struct GNUNET_DNSPARSER_Query *query)
921 struct GNUNET_TUN_DnsQueryLine ql;
923 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
924 if (ret != GNUNET_OK)
926 ql.type = htons (query->type);
927 ql.dns_traffic_class = htons (query->dns_traffic_class);
928 memcpy (&dst[*off], &ql, sizeof (ql));
929 (*off) += sizeof (ql);
935 * Add an MX record to the UDP packet at the given location.
937 * @param dst where to write the mx record
938 * @param dst_len number of bytes in @a dst
939 * @param off pointer to offset where to write the mx information (increment by bytes used);
940 * can also change if there was an error
941 * @param mx mx information to write
942 * @return #GNUNET_SYSERR if @a mx is invalid
943 * #GNUNET_NO if @a mx did not fit
944 * #GNUNET_OK if @a mx was added to @a dst
947 GNUNET_DNSPARSER_builder_add_mx (char *dst,
950 const struct GNUNET_DNSPARSER_MxRecord *mx)
954 if (*off + sizeof (uint16_t) > dst_len)
956 mxpref = htons (mx->preference);
957 memcpy (&dst[*off], &mxpref, sizeof (mxpref));
958 (*off) += sizeof (mxpref);
959 return GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, mx->mxhost);
964 * Add a CERT record to the UDP packet at the given location.
966 * @param dst where to write the CERT record
967 * @param dst_len number of bytes in @a dst
968 * @param off pointer to offset where to write the CERT information (increment by bytes used);
969 * can also change if there was an error
970 * @param cert CERT information to write
971 * @return #GNUNET_SYSERR if @a cert is invalid
972 * #GNUNET_NO if @a cert did not fit
973 * #GNUNET_OK if @a cert was added to @a dst
976 GNUNET_DNSPARSER_builder_add_cert (char *dst,
979 const struct GNUNET_DNSPARSER_CertRecord *cert)
981 struct GNUNET_TUN_DnsCertRecord dcert;
983 if ( (cert->cert_type > UINT16_MAX) ||
984 (cert->cert_tag > UINT16_MAX) ||
985 (cert->algorithm > UINT8_MAX) )
988 return GNUNET_SYSERR;
990 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size > dst_len)
992 dcert.cert_type = htons ((uint16_t) cert->cert_type);
993 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
994 dcert.algorithm = (uint8_t) cert->algorithm;
995 memcpy (&dst[*off], &dcert, sizeof (dcert));
996 (*off) += sizeof (dcert);
997 memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
998 (*off) += cert->certificate_size;
1004 * Add an SOA record to the UDP packet at the given location.
1006 * @param dst where to write the SOA record
1007 * @param dst_len number of bytes in @a dst
1008 * @param off pointer to offset where to write the SOA information (increment by bytes used)
1009 * can also change if there was an error
1010 * @param soa SOA information to write
1011 * @return #GNUNET_SYSERR if @a soa is invalid
1012 * #GNUNET_NO if @a soa did not fit
1013 * #GNUNET_OK if @a soa was added to @a dst
1016 GNUNET_DNSPARSER_builder_add_soa (char *dst,
1019 const struct GNUNET_DNSPARSER_SoaRecord *soa)
1021 struct GNUNET_TUN_DnsSoaRecord sd;
1024 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1028 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1033 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1035 sd.serial = htonl (soa->serial);
1036 sd.refresh = htonl (soa->refresh);
1037 sd.retry = htonl (soa->retry);
1038 sd.expire = htonl (soa->expire);
1039 sd.minimum = htonl (soa->minimum_ttl);
1040 memcpy (&dst[*off], &sd, sizeof (sd));
1041 (*off) += sizeof (sd);
1047 * Add an SRV record to the UDP packet at the given location.
1049 * @param dst where to write the SRV record
1050 * @param dst_len number of bytes in @a dst
1051 * @param off pointer to offset where to write the SRV information (increment by bytes used)
1052 * can also change if there was an error
1053 * @param srv SRV information to write
1054 * @return #GNUNET_SYSERR if @a srv is invalid
1055 * #GNUNET_NO if @a srv did not fit
1056 * #GNUNET_OK if @a srv was added to @a dst
1059 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1062 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1064 struct GNUNET_TUN_DnsSrvRecord sd;
1067 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1069 sd.prio = htons (srv->priority);
1070 sd.weight = htons (srv->weight);
1071 sd.port = htons (srv->port);
1072 memcpy (&dst[*off], &sd, sizeof (sd));
1073 (*off) += sizeof (sd);
1074 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1084 * Add a DNS record to the UDP packet at the given location.
1086 * @param dst where to write the query
1087 * @param dst_len number of bytes in @a dst
1088 * @param off pointer to offset where to write the query (increment by bytes used)
1089 * must not be changed if there is an error
1090 * @param record record to write
1091 * @return #GNUNET_SYSERR if @a record is invalid
1092 * #GNUNET_NO if @a record did not fit
1093 * #GNUNET_OK if @a record was added to @a dst
1096 add_record (char *dst,
1099 const struct GNUNET_DNSPARSER_Record *record)
1104 struct GNUNET_TUN_DnsRecordLine rl;
1108 /* for SRV records, we can create the name from the details
1109 of the record if needed */
1110 name = record->name;
1111 if ( (GNUNET_DNSPARSER_TYPE_SRV == record->type) &&
1113 GNUNET_asprintf (&name,
1115 record->data.srv->service,
1116 record->data.srv->proto,
1117 record->data.srv->domain_name);
1118 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine), off, name);
1119 if (name != record->name)
1121 if (GNUNET_OK != ret)
1123 /* '*off' is now the position where we will need to write the record line */
1125 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1126 switch (record->type)
1128 case GNUNET_DNSPARSER_TYPE_MX:
1129 ret = GNUNET_DNSPARSER_builder_add_mx (dst, dst_len, &pos, record->data.mx);
1131 case GNUNET_DNSPARSER_TYPE_CERT:
1132 ret = GNUNET_DNSPARSER_builder_add_cert (dst, dst_len, &pos, record->data.cert);
1134 case GNUNET_DNSPARSER_TYPE_SOA:
1135 ret = GNUNET_DNSPARSER_builder_add_soa (dst, dst_len, &pos, record->data.soa);
1137 case GNUNET_DNSPARSER_TYPE_NS:
1138 case GNUNET_DNSPARSER_TYPE_CNAME:
1139 case GNUNET_DNSPARSER_TYPE_PTR:
1140 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len, &pos, record->data.hostname);
1142 case GNUNET_DNSPARSER_TYPE_SRV:
1143 ret = GNUNET_DNSPARSER_builder_add_srv (dst, dst_len, &pos, record->data.srv);
1146 if (pos + record->data.raw.data_len > dst_len)
1151 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
1152 pos += record->data.raw.data_len;
1156 if (GNUNET_OK != ret)
1162 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1164 /* record data too long */
1168 rl.type = htons (record->type);
1169 rl.dns_traffic_class = htons (record->dns_traffic_class);
1170 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1171 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1172 memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
1179 * Given a DNS packet @a p, generate the corresponding UDP payload.
1180 * Note that we do not attempt to pack the strings with pointers
1181 * as this would complicate the code and this is about being
1182 * simple and secure, not fast, fancy and broken like bind.
1184 * @param p packet to pack
1185 * @param max maximum allowed size for the resulting UDP payload
1186 * @param buf set to a buffer with the packed message
1187 * @param buf_length set to the length of @a buf
1188 * @return #GNUNET_SYSERR if @a p is invalid
1189 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1190 * #GNUNET_OK if @a p was packed completely into @a buf
1193 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1198 struct GNUNET_TUN_DnsHeader dns;
1205 if ( (p->num_queries > UINT16_MAX) ||
1206 (p->num_answers > UINT16_MAX) ||
1207 (p->num_authority_records > UINT16_MAX) ||
1208 (p->num_additional_records > UINT16_MAX) )
1209 return GNUNET_SYSERR;
1211 dns.flags = p->flags;
1212 dns.query_count = htons (p->num_queries);
1213 dns.answer_rcount = htons (p->num_answers);
1214 dns.authority_rcount = htons (p->num_authority_records);
1215 dns.additional_rcount = htons (p->num_additional_records);
1217 off = sizeof (struct GNUNET_TUN_DnsHeader);
1219 for (i=0;i<p->num_queries;i++)
1221 ret = GNUNET_DNSPARSER_builder_add_query (tmp, sizeof (tmp), &off, &p->queries[i]);
1222 if (GNUNET_SYSERR == ret)
1223 return GNUNET_SYSERR;
1224 if (GNUNET_NO == ret)
1226 dns.query_count = htons ((uint16_t) (i-1));
1231 for (i=0;i<p->num_answers;i++)
1233 ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
1234 if (GNUNET_SYSERR == ret)
1235 return GNUNET_SYSERR;
1236 if (GNUNET_NO == ret)
1238 dns.answer_rcount = htons ((uint16_t) (i-1));
1243 for (i=0;i<p->num_authority_records;i++)
1245 ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
1246 if (GNUNET_SYSERR == ret)
1247 return GNUNET_SYSERR;
1248 if (GNUNET_NO == ret)
1250 dns.authority_rcount = htons ((uint16_t) (i-1));
1255 for (i=0;i<p->num_additional_records;i++)
1257 ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
1258 if (GNUNET_SYSERR == ret)
1259 return GNUNET_SYSERR;
1260 if (GNUNET_NO == ret)
1262 dns.additional_rcount = htons (i-1);
1268 if (GNUNET_YES == trc)
1269 dns.flags.message_truncated = 1;
1270 memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
1272 *buf = GNUNET_malloc (off);
1274 memcpy (*buf, tmp, off);
1275 if (GNUNET_YES == trc)
1280 /* end of dnsparser.c */