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 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);
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 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)
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 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 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 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_PTR:
619 r->data.hostname = GNUNET_DNSPARSER_parse_name (udp_payload,
622 if ( (NULL == r->data.hostname) ||
623 (old_off + data_len != *off) )
624 return GNUNET_SYSERR;
626 case GNUNET_DNSPARSER_TYPE_SOA:
627 r->data.soa = GNUNET_DNSPARSER_parse_soa (udp_payload,
630 if ( (NULL == r->data.soa) ||
631 (old_off + data_len != *off) )
634 return GNUNET_SYSERR;
637 case GNUNET_DNSPARSER_TYPE_MX:
638 r->data.mx = GNUNET_DNSPARSER_parse_mx (udp_payload,
641 if ( (NULL == r->data.mx) ||
642 (old_off + data_len != *off) )
645 return GNUNET_SYSERR;
648 case GNUNET_DNSPARSER_TYPE_SRV:
649 r->data.srv = GNUNET_DNSPARSER_parse_srv (udp_payload,
652 if ( (NULL == r->data.srv) ||
653 (old_off + data_len != *off) )
656 return GNUNET_SYSERR;
660 r->data.raw.data = GNUNET_malloc (data_len);
661 r->data.raw.data_len = data_len;
662 memcpy (r->data.raw.data, &udp_payload[*off], data_len);
671 * Parse a UDP payload of a DNS packet in to a nice struct for further
672 * processing and manipulation.
674 * @param udp_payload wire-format of the DNS packet
675 * @param udp_payload_length number of bytes in @a udp_payload
676 * @return NULL on error, otherwise the parsed packet
678 struct GNUNET_DNSPARSER_Packet *
679 GNUNET_DNSPARSER_parse (const char *udp_payload,
680 size_t udp_payload_length)
682 struct GNUNET_DNSPARSER_Packet *p;
683 const struct GNUNET_TUN_DnsHeader *dns;
688 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
690 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
691 off = sizeof (struct GNUNET_TUN_DnsHeader);
692 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
693 p->flags = dns->flags;
695 n = ntohs (dns->query_count);
698 p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
702 GNUNET_DNSPARSER_parse_query (udp_payload,
708 n = ntohs (dns->answer_rcount);
711 p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
715 GNUNET_DNSPARSER_parse_record (udp_payload,
721 n = ntohs (dns->authority_rcount);
724 p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
725 p->num_authority_records = n;
728 GNUNET_DNSPARSER_parse_record (udp_payload,
731 &p->authority_records[i]))
734 n = ntohs (dns->additional_rcount);
737 p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
738 p->num_additional_records = n;
741 GNUNET_DNSPARSER_parse_record (udp_payload,
744 &p->additional_records[i]))
750 GNUNET_DNSPARSER_free_packet (p);
756 * Free memory taken by a packet.
758 * @param p packet to free
761 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
765 for (i=0;i<p->num_queries;i++)
766 GNUNET_free_non_null (p->queries[i].name);
767 GNUNET_free_non_null (p->queries);
768 for (i=0;i<p->num_answers;i++)
769 GNUNET_DNSPARSER_free_record (&p->answers[i]);
770 GNUNET_free_non_null (p->answers);
771 for (i=0;i<p->num_authority_records;i++)
772 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
773 GNUNET_free_non_null (p->authority_records);
774 for (i=0;i<p->num_additional_records;i++)
775 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
776 GNUNET_free_non_null (p->additional_records);
781 /* ********************** DNS packet assembly code **************** */
785 * Add a DNS name to the UDP packet at the given location, converting
786 * the name to IDNA notation as necessary.
788 * @param dst where to write the name (UDP packet)
789 * @param dst_len number of bytes in @a dst
790 * @param off pointer to offset where to write the name (increment by bytes used)
791 * must not be changed if there is an error
792 * @param name name to write
793 * @return #GNUNET_SYSERR if @a name is invalid
794 * #GNUNET_NO if @a name did not fit
795 * #GNUNET_OK if @a name was added to @a dst
798 GNUNET_DNSPARSER_builder_add_name (char *dst,
804 const char *idna_name;
812 return GNUNET_SYSERR;
815 (rc = idna_to_ascii_8z (name, &idna_start, 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 memcpy (&dst[pos], idna_name, len);
843 idna_name += len + 1; /* also skip dot */
846 dst[pos++] = '\0'; /* terminator */
849 idn_free (idna_start);
856 idn_free (idna_start);
865 * Add a DNS query to the UDP packet at the given location.
867 * @param dst where to write the query
868 * @param dst_len number of bytes in @a dst
869 * @param off pointer to offset where to write the query (increment by bytes used)
870 * must not be changed if there is an error
871 * @param query query to write
872 * @return #GNUNET_SYSERR if @a query is invalid
873 * #GNUNET_NO if @a query did not fit
874 * #GNUNET_OK if @a query was added to @a dst
877 GNUNET_DNSPARSER_builder_add_query (char *dst,
880 const struct GNUNET_DNSPARSER_Query *query)
883 struct GNUNET_TUN_DnsQueryLine ql;
885 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
886 if (ret != GNUNET_OK)
888 ql.type = htons (query->type);
889 ql.dns_traffic_class = htons (query->dns_traffic_class);
890 memcpy (&dst[*off], &ql, sizeof (ql));
891 (*off) += sizeof (ql);
897 * Add an MX record to the UDP packet at the given location.
899 * @param dst where to write the mx record
900 * @param dst_len number of bytes in @a dst
901 * @param off pointer to offset where to write the mx information (increment by bytes used);
902 * can also change if there was an error
903 * @param mx mx information to write
904 * @return #GNUNET_SYSERR if @a mx is invalid
905 * #GNUNET_NO if @a mx did not fit
906 * #GNUNET_OK if @a mx was added to @a dst
909 GNUNET_DNSPARSER_builder_add_mx (char *dst,
912 const struct GNUNET_DNSPARSER_MxRecord *mx)
916 if (*off + sizeof (uint16_t) > dst_len)
918 mxpref = htons (mx->preference);
919 memcpy (&dst[*off], &mxpref, sizeof (mxpref));
920 (*off) += sizeof (mxpref);
921 return GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, mx->mxhost);
926 * Add a CERT record to the UDP packet at the given location.
928 * @param dst where to write the CERT record
929 * @param dst_len number of bytes in @a dst
930 * @param off pointer to offset where to write the CERT information (increment by bytes used);
931 * can also change if there was an error
932 * @param cert CERT information to write
933 * @return #GNUNET_SYSERR if @a cert is invalid
934 * #GNUNET_NO if @a cert did not fit
935 * #GNUNET_OK if @a cert was added to @a dst
938 GNUNET_DNSPARSER_builder_add_cert (char *dst,
941 const struct GNUNET_DNSPARSER_CertRecord *cert)
943 struct GNUNET_TUN_DnsCertRecord dcert;
945 if ( (cert->cert_type > UINT16_MAX) ||
946 (cert->cert_tag > UINT16_MAX) ||
947 (cert->algorithm > UINT8_MAX) )
950 return GNUNET_SYSERR;
952 if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size > dst_len)
954 dcert.cert_type = htons ((uint16_t) cert->cert_type);
955 dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
956 dcert.algorithm = (uint8_t) cert->algorithm;
957 memcpy (&dst[*off], &dcert, sizeof (dcert));
958 (*off) += sizeof (dcert);
959 memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
960 (*off) += cert->certificate_size;
966 * Add an SOA record to the UDP packet at the given location.
968 * @param dst where to write the SOA record
969 * @param dst_len number of bytes in @a dst
970 * @param off pointer to offset where to write the SOA information (increment by bytes used)
971 * can also change if there was an error
972 * @param soa SOA information to write
973 * @return #GNUNET_SYSERR if @a soa is invalid
974 * #GNUNET_NO if @a soa did not fit
975 * #GNUNET_OK if @a soa was added to @a dst
978 GNUNET_DNSPARSER_builder_add_soa (char *dst,
981 const struct GNUNET_DNSPARSER_SoaRecord *soa)
983 struct GNUNET_TUN_DnsSoaRecord sd;
986 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
990 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
995 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
997 sd.serial = htonl (soa->serial);
998 sd.refresh = htonl (soa->refresh);
999 sd.retry = htonl (soa->retry);
1000 sd.expire = htonl (soa->expire);
1001 sd.minimum = htonl (soa->minimum_ttl);
1002 memcpy (&dst[*off], &sd, sizeof (sd));
1003 (*off) += sizeof (sd);
1009 * Add an SRV record to the UDP packet at the given location.
1011 * @param dst where to write the SRV record
1012 * @param dst_len number of bytes in @a dst
1013 * @param off pointer to offset where to write the SRV information (increment by bytes used)
1014 * can also change if there was an error
1015 * @param srv SRV information to write
1016 * @return #GNUNET_SYSERR if @a srv is invalid
1017 * #GNUNET_NO if @a srv did not fit
1018 * #GNUNET_OK if @a srv was added to @a dst
1021 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1024 const struct GNUNET_DNSPARSER_SrvRecord *srv)
1026 struct GNUNET_TUN_DnsSrvRecord sd;
1029 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1031 sd.prio = htons (srv->priority);
1032 sd.weight = htons (srv->weight);
1033 sd.port = htons (srv->port);
1034 memcpy (&dst[*off], &sd, sizeof (sd));
1035 (*off) += sizeof (sd);
1036 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1046 * Add a DNS record to the UDP packet at the given location.
1048 * @param dst where to write the query
1049 * @param dst_len number of bytes in @a dst
1050 * @param off pointer to offset where to write the query (increment by bytes used)
1051 * must not be changed if there is an error
1052 * @param record record to write
1053 * @return #GNUNET_SYSERR if @a record is invalid
1054 * #GNUNET_NO if @a record did not fit
1055 * #GNUNET_OK if @a record was added to @a dst
1058 add_record (char *dst,
1061 const struct GNUNET_DNSPARSER_Record *record)
1066 struct GNUNET_TUN_DnsRecordLine rl;
1069 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1070 dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine),
1073 if (GNUNET_OK != ret)
1075 /* '*off' is now the position where we will need to write the record line */
1077 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1078 switch (record->type)
1080 case GNUNET_DNSPARSER_TYPE_MX:
1081 ret = GNUNET_DNSPARSER_builder_add_mx (dst, dst_len, &pos, record->data.mx);
1083 case GNUNET_DNSPARSER_TYPE_CERT:
1084 ret = GNUNET_DNSPARSER_builder_add_cert (dst, dst_len, &pos, record->data.cert);
1086 case GNUNET_DNSPARSER_TYPE_SOA:
1087 ret = GNUNET_DNSPARSER_builder_add_soa (dst, dst_len, &pos, record->data.soa);
1089 case GNUNET_DNSPARSER_TYPE_NS:
1090 case GNUNET_DNSPARSER_TYPE_CNAME:
1091 case GNUNET_DNSPARSER_TYPE_PTR:
1092 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len, &pos, record->data.hostname);
1094 case GNUNET_DNSPARSER_TYPE_SRV:
1095 ret = GNUNET_DNSPARSER_builder_add_srv (dst, dst_len, &pos, record->data.srv);
1098 if (pos + record->data.raw.data_len > dst_len)
1103 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
1104 pos += record->data.raw.data_len;
1108 if (GNUNET_OK != ret)
1114 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1116 /* record data too long */
1120 rl.type = htons (record->type);
1121 rl.dns_traffic_class = htons (record->dns_traffic_class);
1122 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1123 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1124 memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
1131 * Given a DNS packet @a p, generate the corresponding UDP payload.
1132 * Note that we do not attempt to pack the strings with pointers
1133 * as this would complicate the code and this is about being
1134 * simple and secure, not fast, fancy and broken like bind.
1136 * @param p packet to pack
1137 * @param max maximum allowed size for the resulting UDP payload
1138 * @param buf set to a buffer with the packed message
1139 * @param buf_length set to the length of @a buf
1140 * @return #GNUNET_SYSERR if @a p is invalid
1141 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1142 * #GNUNET_OK if @a p was packed completely into @a buf
1145 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1150 struct GNUNET_TUN_DnsHeader dns;
1157 if ( (p->num_queries > UINT16_MAX) ||
1158 (p->num_answers > UINT16_MAX) ||
1159 (p->num_authority_records > UINT16_MAX) ||
1160 (p->num_additional_records > UINT16_MAX) )
1161 return GNUNET_SYSERR;
1163 dns.flags = p->flags;
1164 dns.query_count = htons (p->num_queries);
1165 dns.answer_rcount = htons (p->num_answers);
1166 dns.authority_rcount = htons (p->num_authority_records);
1167 dns.additional_rcount = htons (p->num_additional_records);
1169 off = sizeof (struct GNUNET_TUN_DnsHeader);
1171 for (i=0;i<p->num_queries;i++)
1173 ret = GNUNET_DNSPARSER_builder_add_query (tmp, sizeof (tmp), &off, &p->queries[i]);
1174 if (GNUNET_SYSERR == ret)
1175 return GNUNET_SYSERR;
1176 if (GNUNET_NO == ret)
1178 dns.query_count = htons ((uint16_t) (i-1));
1183 for (i=0;i<p->num_answers;i++)
1185 ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
1186 if (GNUNET_SYSERR == ret)
1187 return GNUNET_SYSERR;
1188 if (GNUNET_NO == ret)
1190 dns.answer_rcount = htons ((uint16_t) (i-1));
1195 for (i=0;i<p->num_authority_records;i++)
1197 ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
1198 if (GNUNET_SYSERR == ret)
1199 return GNUNET_SYSERR;
1200 if (GNUNET_NO == ret)
1202 dns.authority_rcount = htons ((uint16_t) (i-1));
1207 for (i=0;i<p->num_additional_records;i++)
1209 ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
1210 if (GNUNET_SYSERR == ret)
1211 return GNUNET_SYSERR;
1212 if (GNUNET_NO == ret)
1214 dns.additional_rcount = htons (i-1);
1220 if (GNUNET_YES == trc)
1221 dns.flags.message_truncated = 1;
1222 memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
1224 *buf = GNUNET_malloc (off);
1226 memcpy (*buf, tmp, off);
1227 if (GNUNET_YES == trc)
1234 * Convert a block of binary data to HEX.
1236 * @param data binary data to convert
1237 * @param data_size number of bytes in @a data
1238 * @return HEX string (lower case)
1241 GNUNET_DNSPARSER_bin_to_hex (const void *data,
1246 const uint8_t *idata;
1249 ret = GNUNET_malloc (data_size * 2 + 1);
1250 for (off = 0; off < data_size; off++)
1251 sprintf (&ret[off * 2],
1259 * Convert a HEX string to block of binary data.
1261 * @param hex HEX string to convert (may contain mixed case)
1262 * @param data where to write result, must be
1263 * at least `strlen(hex)/2` bytes long
1264 * @return number of bytes written to data
1267 GNUNET_DNSPARSER_hex_to_bin (const char *hex,
1276 data_size = strlen (hex) / 2;
1279 for (off = 0; off < data_size; off++)
1281 in[0] = tolower ((int) hex[off * 2]);
1282 in[1] = tolower ((int) hex[off * 2 + 1]);
1283 if (1 != sscanf (in, "%x", &h))
1285 idata[off] = (uint8_t) h;
1291 /* end of dnsparser.c */