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 SRV information record.
123 * @param srv record to free
126 GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
130 GNUNET_free_non_null (srv->target);
131 GNUNET_free_non_null (srv->domain_name);
132 GNUNET_free_non_null (srv->proto);
133 GNUNET_free_non_null (srv->service);
139 * Free MX information record.
141 * @param mx record to free
144 GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
148 GNUNET_free_non_null (mx->mxhost);
154 * Free the given DNS record.
156 * @param r record to free
159 GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
161 GNUNET_free_non_null (r->name);
164 case GNUNET_DNSPARSER_TYPE_MX:
165 GNUNET_DNSPARSER_free_mx (r->data.mx);
167 case GNUNET_DNSPARSER_TYPE_SOA:
168 GNUNET_DNSPARSER_free_soa (r->data.soa);
170 case GNUNET_DNSPARSER_TYPE_SRV:
171 GNUNET_DNSPARSER_free_srv (r->data.srv);
173 case GNUNET_DNSPARSER_TYPE_NS:
174 case GNUNET_DNSPARSER_TYPE_CNAME:
175 case GNUNET_DNSPARSER_TYPE_PTR:
176 GNUNET_free_non_null (r->data.hostname);
179 GNUNET_free_non_null (r->data.raw.data);
186 * Parse name inside of a DNS query or record.
188 * @param udp_payload entire UDP payload
189 * @param udp_payload_length length of @a udp_payload
190 * @param off pointer to the offset of the name to parse in the udp_payload (to be
191 * incremented by the size of the name)
192 * @param depth current depth of our recursion (to prevent stack overflow)
193 * @return name as 0-terminated C string on success, NULL if the payload is malformed
196 parse_name (const char *udp_payload,
197 size_t udp_payload_length,
201 const uint8_t *input = (const uint8_t *) udp_payload;
210 ret = GNUNET_strdup ("");
213 if (*off >= udp_payload_length)
226 if (*off + 1 + len > udp_payload_length)
231 GNUNET_asprintf (&tmp,
234 &udp_payload[*off + 1]);
236 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
238 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
239 _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
243 GNUNET_asprintf (&tmp,
247 &udp_payload[*off + 1]);
252 GNUNET_asprintf (&tmp,
266 else if ((64 | 128) == (len & (64 | 128)) )
271 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
273 /* pointer to string */
274 if (*off + 1 > udp_payload_length)
279 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
280 xstr = parse_name (udp_payload,
289 GNUNET_asprintf (&tmp,
296 if (strlen (ret) > udp_payload_length)
299 goto error; /* we are looping (building an infinite string) */
302 /* pointers always terminate names */
307 /* neither pointer nor inline string, not supported... */
313 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
323 * Parse name inside of a DNS query or record.
325 * @param udp_payload entire UDP payload
326 * @param udp_payload_length length of @a udp_payload
327 * @param off pointer to the offset of the name to parse in the udp_payload (to be
328 * incremented by the size of the name)
329 * @return name as 0-terminated C string on success, NULL if the payload is malformed
332 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
333 size_t udp_payload_length,
336 return parse_name (udp_payload, udp_payload_length, off, 0);
341 * Parse a DNS query entry.
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 query to parse in the udp_payload (to be
346 * incremented by the size of the query)
347 * @param q where to write the query information
348 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
351 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
352 size_t udp_payload_length,
354 struct GNUNET_DNSPARSER_Query *q)
357 struct GNUNET_TUN_DnsQueryLine ql;
359 name = GNUNET_DNSPARSER_parse_name (udp_payload,
365 return GNUNET_SYSERR;
368 if (*off + sizeof (struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
371 return GNUNET_SYSERR;
373 memcpy (&ql, &udp_payload[*off], sizeof (ql));
375 q->type = ntohs (ql.type);
376 q->dns_traffic_class = ntohs (ql.dns_traffic_class);
382 * Parse a DNS SOA record.
384 * @param udp_payload reference to UDP packet
385 * @param udp_payload_length length of @a udp_payload
386 * @param off pointer to the offset of the query to parse in the SOA record (to be
387 * incremented by the size of the record), unchanged on error
388 * @return the parsed SOA record, NULL on error
390 struct GNUNET_DNSPARSER_SoaRecord *
391 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
392 size_t udp_payload_length,
395 struct GNUNET_DNSPARSER_SoaRecord *soa;
396 struct GNUNET_TUN_DnsSoaRecord soa_bin;
400 soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
401 soa->mname = GNUNET_DNSPARSER_parse_name (udp_payload,
404 soa->rname = GNUNET_DNSPARSER_parse_name (udp_payload,
407 if ( (NULL == soa->mname) ||
408 (NULL == soa->rname) ||
409 (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length) )
412 GNUNET_DNSPARSER_free_soa (soa);
418 sizeof (struct GNUNET_TUN_DnsSoaRecord));
419 soa->serial = ntohl (soa_bin.serial);
420 soa->refresh = ntohl (soa_bin.refresh);
421 soa->retry = ntohl (soa_bin.retry);
422 soa->expire = ntohl (soa_bin.expire);
423 soa->minimum_ttl = ntohl (soa_bin.minimum);
424 (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
430 * Parse a DNS MX record.
432 * @param udp_payload reference to UDP packet
433 * @param udp_payload_length length of @a udp_payload
434 * @param off pointer to the offset of the query to parse in the MX record (to be
435 * incremented by the size of the record), unchanged on error
436 * @return the parsed MX record, NULL on error
438 struct GNUNET_DNSPARSER_MxRecord *
439 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
440 size_t udp_payload_length,
443 struct GNUNET_DNSPARSER_MxRecord *mx;
448 if (*off + sizeof (uint16_t) > udp_payload_length)
453 memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
454 (*off) += sizeof (uint16_t);
455 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
456 mx->preference = ntohs (mxpref);
457 mx->mxhost = GNUNET_DNSPARSER_parse_name (udp_payload,
460 if (NULL == mx->mxhost)
463 GNUNET_DNSPARSER_free_mx (mx);
472 * Parse a DNS SRV record.
474 * @param r_name name of the SRV record
475 * @param udp_payload reference to UDP packet
476 * @param udp_payload_length length of @a udp_payload
477 * @param off pointer to the offset of the query to parse in the SRV record (to be
478 * incremented by the size of the record), unchanged on error
479 * @return the parsed SRV record, NULL on error
481 struct GNUNET_DNSPARSER_SrvRecord *
482 GNUNET_DNSPARSER_parse_srv (const char *r_name,
483 const char *udp_payload,
484 size_t udp_payload_length,
487 struct GNUNET_DNSPARSER_SrvRecord *srv;
488 struct GNUNET_TUN_DnsSrvRecord srv_bin;
494 return NULL; /* all valid srv names must start with "_" */
495 if (NULL == strstr (r_name, "._"))
496 return NULL; /* necessary string from "._$PROTO" not present */
498 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
502 sizeof (struct GNUNET_TUN_DnsSrvRecord));
503 (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
504 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
505 srv->priority = ntohs (srv_bin.prio);
506 srv->weight = ntohs (srv_bin.weight);
507 srv->port = ntohs (srv_bin.port);
508 /* parse 'data.hostname' into components, which are
509 "_$SERVICE._$PROTO.$DOMAIN_NAME" */
510 ndup = GNUNET_strdup (r_name);
511 tok = strtok (ndup, ".");
512 GNUNET_assert (NULL != tok);
513 GNUNET_assert ('_' == *tok);
514 srv->service = GNUNET_strdup (&tok[1]);
515 tok = strtok (NULL, ".");
516 if ( (NULL == tok) || ('_' != *tok) )
518 GNUNET_DNSPARSER_free_srv (srv);
523 srv->proto = GNUNET_strdup (&tok[1]);
524 tok = strtok (NULL, ".");
527 GNUNET_DNSPARSER_free_srv (srv);
532 srv->domain_name = GNUNET_strdup (tok);
534 srv->target = GNUNET_DNSPARSER_parse_name (udp_payload,
537 if (NULL == srv->target)
539 GNUNET_DNSPARSER_free_srv (srv);
548 * Parse a DNS record entry.
550 * @param udp_payload entire UDP payload
551 * @param udp_payload_length length of @a udp_payload
552 * @param off pointer to the offset of the record to parse in the udp_payload (to be
553 * incremented by the size of the record)
554 * @param r where to write the record information
555 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
558 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
559 size_t udp_payload_length,
561 struct GNUNET_DNSPARSER_Record *r)
564 struct GNUNET_TUN_DnsRecordLine rl;
568 name = GNUNET_DNSPARSER_parse_name (udp_payload,
574 return GNUNET_SYSERR;
577 if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
580 return GNUNET_SYSERR;
582 memcpy (&rl, &udp_payload[*off], sizeof (rl));
583 (*off) += sizeof (rl);
584 r->type = ntohs (rl.type);
585 r->dns_traffic_class = ntohs (rl.dns_traffic_class);
586 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
588 data_len = ntohs (rl.data_len);
589 if (*off + data_len > udp_payload_length)
592 return GNUNET_SYSERR;
597 case GNUNET_DNSPARSER_TYPE_NS:
598 case GNUNET_DNSPARSER_TYPE_CNAME:
599 case GNUNET_DNSPARSER_TYPE_PTR:
600 r->data.hostname = GNUNET_DNSPARSER_parse_name (udp_payload,
603 if ( (NULL == r->data.hostname) ||
604 (old_off + data_len != *off) )
605 return GNUNET_SYSERR;
607 case GNUNET_DNSPARSER_TYPE_SOA:
608 r->data.soa = GNUNET_DNSPARSER_parse_soa (udp_payload,
611 if ( (NULL == r->data.soa) ||
612 (old_off + data_len != *off) )
615 return GNUNET_SYSERR;
618 case GNUNET_DNSPARSER_TYPE_MX:
619 r->data.mx = GNUNET_DNSPARSER_parse_mx (udp_payload,
622 if ( (NULL == r->data.mx) ||
623 (old_off + data_len != *off) )
626 return GNUNET_SYSERR;
629 case GNUNET_DNSPARSER_TYPE_SRV:
630 r->data.srv = GNUNET_DNSPARSER_parse_srv (r->name,
634 if ( (NULL == r->data.srv) ||
635 (old_off + data_len != *off) )
638 return GNUNET_SYSERR;
642 r->data.raw.data = GNUNET_malloc (data_len);
643 r->data.raw.data_len = data_len;
644 memcpy (r->data.raw.data, &udp_payload[*off], data_len);
653 * Parse a UDP payload of a DNS packet in to a nice struct for further
654 * processing and manipulation.
656 * @param udp_payload wire-format of the DNS packet
657 * @param udp_payload_length number of bytes in @a udp_payload
658 * @return NULL on error, otherwise the parsed packet
660 struct GNUNET_DNSPARSER_Packet *
661 GNUNET_DNSPARSER_parse (const char *udp_payload,
662 size_t udp_payload_length)
664 struct GNUNET_DNSPARSER_Packet *p;
665 const struct GNUNET_TUN_DnsHeader *dns;
670 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
672 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
673 off = sizeof (struct GNUNET_TUN_DnsHeader);
674 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
675 p->flags = dns->flags;
677 n = ntohs (dns->query_count);
680 p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
684 GNUNET_DNSPARSER_parse_query (udp_payload,
690 n = ntohs (dns->answer_rcount);
693 p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
697 GNUNET_DNSPARSER_parse_record (udp_payload,
703 n = ntohs (dns->authority_rcount);
706 p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
707 p->num_authority_records = n;
710 GNUNET_DNSPARSER_parse_record (udp_payload,
713 &p->authority_records[i]))
716 n = ntohs (dns->additional_rcount);
719 p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
720 p->num_additional_records = n;
723 GNUNET_DNSPARSER_parse_record (udp_payload,
726 &p->additional_records[i]))
732 GNUNET_DNSPARSER_free_packet (p);
738 * Free memory taken by a packet.
740 * @param p packet to free
743 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
747 for (i=0;i<p->num_queries;i++)
748 GNUNET_free_non_null (p->queries[i].name);
749 GNUNET_free_non_null (p->queries);
750 for (i=0;i<p->num_answers;i++)
751 GNUNET_DNSPARSER_free_record (&p->answers[i]);
752 GNUNET_free_non_null (p->answers);
753 for (i=0;i<p->num_authority_records;i++)
754 GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
755 GNUNET_free_non_null (p->authority_records);
756 for (i=0;i<p->num_additional_records;i++)
757 GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
758 GNUNET_free_non_null (p->additional_records);
763 /* ********************** DNS packet assembly code **************** */
767 * Add a DNS name to the UDP packet at the given location, converting
768 * the name to IDNA notation as necessary.
770 * @param dst where to write the name (UDP packet)
771 * @param dst_len number of bytes in @a dst
772 * @param off pointer to offset where to write the name (increment by bytes used)
773 * must not be changed if there is an error
774 * @param name name to write
775 * @return #GNUNET_SYSERR if @a name is invalid
776 * #GNUNET_NO if @a name did not fit
777 * #GNUNET_OK if @a name was added to @a dst
780 GNUNET_DNSPARSER_builder_add_name (char *dst,
786 const char *idna_name;
794 return GNUNET_SYSERR;
797 (rc = idna_to_ascii_8z (name, &idna_start, IDNA_ALLOW_UNASSIGNED)))
799 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
800 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
805 idna_name = idna_start;
807 if (start + strlen (idna_name) + 2 > dst_len)
812 dot = strchr (idna_name, '.');
814 len = strlen (idna_name);
816 len = dot - idna_name;
817 if ( (len >= 64) || (0 == len) )
818 goto fail; /* segment too long or empty */
819 dst[pos++] = (char) (uint8_t) len;
820 memcpy (&dst[pos], idna_name, len);
822 idna_name += len + 1; /* also skip dot */
825 dst[pos++] = '\0'; /* terminator */
828 idn_free (idna_start);
835 idn_free (idna_start);
844 * Add a DNS query to the UDP packet at the given location.
846 * @param dst where to write the query
847 * @param dst_len number of bytes in @a dst
848 * @param off pointer to offset where to write the query (increment by bytes used)
849 * must not be changed if there is an error
850 * @param query query to write
851 * @return #GNUNET_SYSERR if @a query is invalid
852 * #GNUNET_NO if @a query did not fit
853 * #GNUNET_OK if @a query was added to @a dst
856 GNUNET_DNSPARSER_builder_add_query (char *dst,
859 const struct GNUNET_DNSPARSER_Query *query)
862 struct GNUNET_TUN_DnsQueryLine ql;
864 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
865 if (ret != GNUNET_OK)
867 ql.type = htons (query->type);
868 ql.dns_traffic_class = htons (query->dns_traffic_class);
869 memcpy (&dst[*off], &ql, sizeof (ql));
870 (*off) += sizeof (ql);
876 * Add an MX record to the UDP packet at the given location.
878 * @param dst where to write the mx record
879 * @param dst_len number of bytes in @a dst
880 * @param off pointer to offset where to write the mx information (increment by bytes used);
881 * can also change if there was an error
882 * @param mx mx information to write
883 * @return #GNUNET_SYSERR if @a mx is invalid
884 * #GNUNET_NO if @a mx did not fit
885 * #GNUNET_OK if @a mx was added to @a dst
888 GNUNET_DNSPARSER_builder_add_mx (char *dst,
891 const struct GNUNET_DNSPARSER_MxRecord *mx)
895 if (*off + sizeof (uint16_t) > dst_len)
897 mxpref = htons (mx->preference);
898 memcpy (&dst[*off], &mxpref, sizeof (mxpref));
899 (*off) += sizeof (mxpref);
900 return GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, mx->mxhost);
905 * Add an SOA record to the UDP packet at the given location.
907 * @param dst where to write the SOA record
908 * @param dst_len number of bytes in @a dst
909 * @param off pointer to offset where to write the SOA information (increment by bytes used)
910 * can also change if there was an error
911 * @param soa SOA information to write
912 * @return #GNUNET_SYSERR if @a soa is invalid
913 * #GNUNET_NO if @a soa did not fit
914 * #GNUNET_OK if @a soa was added to @a dst
917 GNUNET_DNSPARSER_builder_add_soa (char *dst,
920 const struct GNUNET_DNSPARSER_SoaRecord *soa)
922 struct GNUNET_TUN_DnsSoaRecord sd;
925 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
929 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
934 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
936 sd.serial = htonl (soa->serial);
937 sd.refresh = htonl (soa->refresh);
938 sd.retry = htonl (soa->retry);
939 sd.expire = htonl (soa->expire);
940 sd.minimum = htonl (soa->minimum_ttl);
941 memcpy (&dst[*off], &sd, sizeof (sd));
942 (*off) += sizeof (sd);
948 * Add an SRV record to the UDP packet at the given location.
950 * @param dst where to write the SRV record
951 * @param dst_len number of bytes in @a dst
952 * @param off pointer to offset where to write the SRV information (increment by bytes used)
953 * can also change if there was an error
954 * @param srv SRV information to write
955 * @return #GNUNET_SYSERR if @a srv is invalid
956 * #GNUNET_NO if @a srv did not fit
957 * #GNUNET_OK if @a srv was added to @a dst
960 GNUNET_DNSPARSER_builder_add_srv (char *dst,
963 const struct GNUNET_DNSPARSER_SrvRecord *srv)
965 struct GNUNET_TUN_DnsSrvRecord sd;
968 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
970 sd.prio = htons (srv->priority);
971 sd.weight = htons (srv->weight);
972 sd.port = htons (srv->port);
973 memcpy (&dst[*off], &sd, sizeof (sd));
974 (*off) += sizeof (sd);
975 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
985 * Add a DNS record to the UDP packet at the given location.
987 * @param dst where to write the query
988 * @param dst_len number of bytes in @a dst
989 * @param off pointer to offset where to write the query (increment by bytes used)
990 * must not be changed if there is an error
991 * @param record record to write
992 * @return #GNUNET_SYSERR if @a record is invalid
993 * #GNUNET_NO if @a record did not fit
994 * #GNUNET_OK if @a record was added to @a dst
997 add_record (char *dst,
1000 const struct GNUNET_DNSPARSER_Record *record)
1005 struct GNUNET_TUN_DnsRecordLine rl;
1009 /* for SRV records, we can create the name from the details
1010 of the record if needed */
1011 name = record->name;
1012 if ( (GNUNET_DNSPARSER_TYPE_SRV == record->type) &&
1014 GNUNET_asprintf (&name,
1016 record->data.srv->service,
1017 record->data.srv->proto,
1018 record->data.srv->domain_name);
1019 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine), off, name);
1020 if (name != record->name)
1022 if (GNUNET_OK != ret)
1024 /* '*off' is now the position where we will need to write the record line */
1026 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1027 switch (record->type)
1029 case GNUNET_DNSPARSER_TYPE_MX:
1030 ret = GNUNET_DNSPARSER_builder_add_mx (dst, dst_len, &pos, record->data.mx);
1032 case GNUNET_DNSPARSER_TYPE_SOA:
1033 ret = GNUNET_DNSPARSER_builder_add_soa (dst, dst_len, &pos, record->data.soa);
1035 case GNUNET_DNSPARSER_TYPE_NS:
1036 case GNUNET_DNSPARSER_TYPE_CNAME:
1037 case GNUNET_DNSPARSER_TYPE_PTR:
1038 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len, &pos, record->data.hostname);
1040 case GNUNET_DNSPARSER_TYPE_SRV:
1041 ret = GNUNET_DNSPARSER_builder_add_srv (dst, dst_len, &pos, record->data.srv);
1044 if (pos + record->data.raw.data_len > dst_len)
1049 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
1050 pos += record->data.raw.data_len;
1054 if (GNUNET_OK != ret)
1060 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1062 /* record data too long */
1066 rl.type = htons (record->type);
1067 rl.dns_traffic_class = htons (record->dns_traffic_class);
1068 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1069 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1070 memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
1077 * Given a DNS packet @a p, generate the corresponding UDP payload.
1078 * Note that we do not attempt to pack the strings with pointers
1079 * as this would complicate the code and this is about being
1080 * simple and secure, not fast, fancy and broken like bind.
1082 * @param p packet to pack
1083 * @param max maximum allowed size for the resulting UDP payload
1084 * @param buf set to a buffer with the packed message
1085 * @param buf_length set to the length of @a buf
1086 * @return #GNUNET_SYSERR if @a p is invalid
1087 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1088 * #GNUNET_OK if @a p was packed completely into @a buf
1091 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1096 struct GNUNET_TUN_DnsHeader dns;
1103 if ( (p->num_queries > UINT16_MAX) ||
1104 (p->num_answers > UINT16_MAX) ||
1105 (p->num_authority_records > UINT16_MAX) ||
1106 (p->num_additional_records > UINT16_MAX) )
1107 return GNUNET_SYSERR;
1109 dns.flags = p->flags;
1110 dns.query_count = htons (p->num_queries);
1111 dns.answer_rcount = htons (p->num_answers);
1112 dns.authority_rcount = htons (p->num_authority_records);
1113 dns.additional_rcount = htons (p->num_additional_records);
1115 off = sizeof (struct GNUNET_TUN_DnsHeader);
1117 for (i=0;i<p->num_queries;i++)
1119 ret = GNUNET_DNSPARSER_builder_add_query (tmp, sizeof (tmp), &off, &p->queries[i]);
1120 if (GNUNET_SYSERR == ret)
1121 return GNUNET_SYSERR;
1122 if (GNUNET_NO == ret)
1124 dns.query_count = htons ((uint16_t) (i-1));
1129 for (i=0;i<p->num_answers;i++)
1131 ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
1132 if (GNUNET_SYSERR == ret)
1133 return GNUNET_SYSERR;
1134 if (GNUNET_NO == ret)
1136 dns.answer_rcount = htons ((uint16_t) (i-1));
1141 for (i=0;i<p->num_authority_records;i++)
1143 ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
1144 if (GNUNET_SYSERR == ret)
1145 return GNUNET_SYSERR;
1146 if (GNUNET_NO == ret)
1148 dns.authority_rcount = htons ((uint16_t) (i-1));
1153 for (i=0;i<p->num_additional_records;i++)
1155 ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
1156 if (GNUNET_SYSERR == ret)
1157 return GNUNET_SYSERR;
1158 if (GNUNET_NO == ret)
1160 dns.additional_rcount = htons (i-1);
1166 if (GNUNET_YES == trc)
1167 dns.flags.message_truncated = 1;
1168 memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
1170 *buf = GNUNET_malloc (off);
1172 memcpy (*buf, tmp, off);
1173 if (GNUNET_YES == trc)
1178 /* end of dnsparser.c */