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 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 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 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 free_record (struct GNUNET_DNSPARSER_Record *r)
161 GNUNET_free_non_null (r->name);
164 case GNUNET_DNSPARSER_TYPE_MX:
165 free_mx (r->data.mx);
167 case GNUNET_DNSPARSER_TYPE_SOA:
168 free_soa (r->data.soa);
170 case GNUNET_DNSPARSER_TYPE_SRV:
171 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)
223 if (*off + 1 + len > udp_payload_length)
225 GNUNET_asprintf (&tmp,
228 &udp_payload[*off + 1]);
230 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
232 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
233 _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
237 GNUNET_asprintf (&tmp,
241 &udp_payload[*off + 1]);
246 GNUNET_asprintf (&tmp,
260 else if ((64 | 128) == (len & (64 | 128)) )
263 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
264 /* pointer to string */
265 if (*off + 1 > udp_payload_length)
267 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
268 xstr = parse_name (udp_payload,
274 GNUNET_asprintf (&tmp,
281 if (strlen (ret) > udp_payload_length)
282 goto error; /* we are looping (building an infinite string) */
284 /* pointers always terminate names */
289 /* neither pointer nor inline string, not supported... */
294 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
303 * Parse name inside of a DNS query or record.
305 * @param udp_payload entire UDP payload
306 * @param udp_payload_length length of @a udp_payload
307 * @param off pointer to the offset of the name to parse in the udp_payload (to be
308 * incremented by the size of the name)
309 * @return name as 0-terminated C string on success, NULL if the payload is malformed
312 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
313 size_t udp_payload_length,
316 return parse_name (udp_payload, udp_payload_length, off, 0);
321 * Parse a DNS query entry.
323 * @param udp_payload entire UDP payload
324 * @param udp_payload_length length of @a udp_payload
325 * @param off pointer to the offset of the query to parse in the udp_payload (to be
326 * incremented by the size of the query)
327 * @param q where to write the query information
328 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
331 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
332 size_t udp_payload_length,
334 struct GNUNET_DNSPARSER_Query *q)
337 struct GNUNET_TUN_DnsQueryLine ql;
339 name = GNUNET_DNSPARSER_parse_name (udp_payload,
343 return GNUNET_SYSERR;
345 if (*off + sizeof (struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
346 return GNUNET_SYSERR;
347 memcpy (&ql, &udp_payload[*off], sizeof (ql));
349 q->type = ntohs (ql.type);
350 q->class = ntohs (ql.class);
356 * Parse a DNS SOA record.
358 * @param udp_payload reference to UDP packet
359 * @param udp_payload_length length of @a udp_payload
360 * @param off pointer to the offset of the query to parse in the SOA record (to be
361 * incremented by the size of the record), unchanged on error
362 * @return the parsed SOA record, NULL on error
364 struct GNUNET_DNSPARSER_SoaRecord *
365 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
366 size_t udp_payload_length,
369 struct GNUNET_DNSPARSER_SoaRecord *soa;
370 struct GNUNET_TUN_DnsSoaRecord soa_bin;
374 soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
375 soa->mname = GNUNET_DNSPARSER_parse_name (udp_payload,
378 soa->rname = GNUNET_DNSPARSER_parse_name (udp_payload,
381 if ( (NULL == soa->mname) ||
382 (NULL == soa->rname) ||
383 (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length) )
391 sizeof (struct GNUNET_TUN_DnsSoaRecord));
392 soa->serial = ntohl (soa_bin.serial);
393 soa->refresh = ntohl (soa_bin.refresh);
394 soa->retry = ntohl (soa_bin.retry);
395 soa->expire = ntohl (soa_bin.expire);
396 soa->minimum_ttl = ntohl (soa_bin.minimum);
397 (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
403 * Parse a DNS MX record.
405 * @param udp_payload reference to UDP packet
406 * @param udp_payload_length length of @a udp_payload
407 * @param off pointer to the offset of the query to parse in the MX record (to be
408 * incremented by the size of the record), unchanged on error
409 * @return the parsed MX record, NULL on error
411 struct GNUNET_DNSPARSER_MxRecord *
412 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
413 size_t udp_payload_length,
416 struct GNUNET_DNSPARSER_MxRecord *mx;
421 if (*off + sizeof (uint16_t) > udp_payload_length)
423 memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
424 (*off) += sizeof (uint16_t);
425 mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
426 mx->preference = ntohs (mxpref);
427 mx->mxhost = GNUNET_DNSPARSER_parse_name (udp_payload,
430 if (NULL == mx->mxhost)
442 * Parse a DNS SRV record.
444 * @param r_name name of the SRV record
445 * @param udp_payload reference to UDP packet
446 * @param udp_payload_length length of @a udp_payload
447 * @param off pointer to the offset of the query to parse in the SRV record (to be
448 * incremented by the size of the record), unchanged on error
449 * @return the parsed SRV record, NULL on error
451 struct GNUNET_DNSPARSER_SrvRecord *
452 GNUNET_DNSPARSER_parse_srv (const char *r_name,
453 const char *udp_payload,
454 size_t udp_payload_length,
457 struct GNUNET_DNSPARSER_SrvRecord *srv;
458 struct GNUNET_TUN_DnsSrvRecord srv_bin;
464 return NULL; /* all valid srv names must start with "_" */
465 if (NULL == strstr (r_name, "._"))
466 return NULL; /* necessary string from "._$PROTO" not present */
468 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
472 sizeof (struct GNUNET_TUN_DnsSrvRecord));
473 (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
474 srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
475 srv->priority = ntohs (srv_bin.prio);
476 srv->weight = ntohs (srv_bin.weight);
477 srv->port = ntohs (srv_bin.port);
478 /* parse 'data.hostname' into components, which are
479 "_$SERVICE._$PROTO.$DOMAIN_NAME" */
480 ndup = GNUNET_strdup (r_name);
481 tok = strtok (ndup, ".");
482 GNUNET_assert (NULL != tok);
483 GNUNET_assert ('_' == *tok);
484 srv->service = GNUNET_strdup (&tok[1]);
485 tok = strtok (NULL, ".");
486 if ( (NULL == tok) || ('_' != *tok) )
493 srv->proto = GNUNET_strdup (&tok[1]);
494 tok = strtok (NULL, ".");
502 srv->domain_name = GNUNET_strdup (tok);
504 srv->target = GNUNET_DNSPARSER_parse_name (udp_payload,
507 if (NULL == srv->target)
518 * Parse a DNS record entry.
520 * @param udp_payload entire UDP payload
521 * @param udp_payload_length length of @a udp_payload
522 * @param off pointer to the offset of the record to parse in the udp_payload (to be
523 * incremented by the size of the record)
524 * @param r where to write the record information
525 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
528 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
529 size_t udp_payload_length,
531 struct GNUNET_DNSPARSER_Record *r)
534 struct GNUNET_TUN_DnsRecordLine rl;
538 name = GNUNET_DNSPARSER_parse_name (udp_payload,
542 return GNUNET_SYSERR;
544 if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
545 return GNUNET_SYSERR;
546 memcpy (&rl, &udp_payload[*off], sizeof (rl));
547 (*off) += sizeof (rl);
548 r->type = ntohs (rl.type);
549 r->class = ntohs (rl.class);
550 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
552 data_len = ntohs (rl.data_len);
553 if (*off + data_len > udp_payload_length)
554 return GNUNET_SYSERR;
558 case GNUNET_DNSPARSER_TYPE_NS:
559 case GNUNET_DNSPARSER_TYPE_CNAME:
560 case GNUNET_DNSPARSER_TYPE_PTR:
561 r->data.hostname = GNUNET_DNSPARSER_parse_name (udp_payload,
564 if ( (NULL == r->data.hostname) ||
565 (old_off + data_len != *off) )
566 return GNUNET_SYSERR;
568 case GNUNET_DNSPARSER_TYPE_SOA:
569 r->data.soa = GNUNET_DNSPARSER_parse_soa (udp_payload,
572 if ( (NULL == r->data.soa) ||
573 (old_off + data_len != *off) )
574 return GNUNET_SYSERR;
576 case GNUNET_DNSPARSER_TYPE_MX:
577 r->data.mx = GNUNET_DNSPARSER_parse_mx (udp_payload,
580 if ( (NULL == r->data.mx) ||
581 (old_off + data_len != *off) )
582 return GNUNET_SYSERR;
584 case GNUNET_DNSPARSER_TYPE_SRV:
585 r->data.srv = GNUNET_DNSPARSER_parse_srv (r->name,
589 if ( (NULL == r->data.srv) ||
590 (old_off + data_len != *off) )
591 return GNUNET_SYSERR;
594 r->data.raw.data = GNUNET_malloc (data_len);
595 r->data.raw.data_len = data_len;
596 memcpy (r->data.raw.data, &udp_payload[*off], data_len);
605 * Parse a UDP payload of a DNS packet in to a nice struct for further
606 * processing and manipulation.
608 * @param udp_payload wire-format of the DNS packet
609 * @param udp_payload_length number of bytes in @a udp_payload
610 * @return NULL on error, otherwise the parsed packet
612 struct GNUNET_DNSPARSER_Packet *
613 GNUNET_DNSPARSER_parse (const char *udp_payload,
614 size_t udp_payload_length)
616 struct GNUNET_DNSPARSER_Packet *p;
617 const struct GNUNET_TUN_DnsHeader *dns;
622 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
624 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
625 off = sizeof (struct GNUNET_TUN_DnsHeader);
626 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
627 p->flags = dns->flags;
629 n = ntohs (dns->query_count);
632 p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
636 GNUNET_DNSPARSER_parse_query (udp_payload,
642 n = ntohs (dns->answer_rcount);
645 p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
649 GNUNET_DNSPARSER_parse_record (udp_payload,
655 n = ntohs (dns->authority_rcount);
658 p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
659 p->num_authority_records = n;
662 GNUNET_DNSPARSER_parse_record (udp_payload,
665 &p->authority_records[i]))
668 n = ntohs (dns->additional_rcount);
671 p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
672 p->num_additional_records = n;
675 GNUNET_DNSPARSER_parse_record (udp_payload,
678 &p->additional_records[i]))
683 GNUNET_DNSPARSER_free_packet (p);
689 * Free memory taken by a packet.
691 * @param p packet to free
694 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
698 for (i=0;i<p->num_queries;i++)
699 GNUNET_free_non_null (p->queries[i].name);
700 GNUNET_free_non_null (p->queries);
701 for (i=0;i<p->num_answers;i++)
702 free_record (&p->answers[i]);
703 GNUNET_free_non_null (p->answers);
704 for (i=0;i<p->num_authority_records;i++)
705 free_record (&p->authority_records[i]);
706 GNUNET_free_non_null (p->authority_records);
707 for (i=0;i<p->num_additional_records;i++)
708 free_record (&p->additional_records[i]);
709 GNUNET_free_non_null (p->additional_records);
714 /* ********************** DNS packet assembly code **************** */
718 * Add a DNS name to the UDP packet at the given location, converting
719 * the name to IDNA notation as necessary.
721 * @param dst where to write the name (UDP packet)
722 * @param dst_len number of bytes in @a dst
723 * @param off pointer to offset where to write the name (increment by bytes used)
724 * must not be changed if there is an error
725 * @param name name to write
726 * @return #GNUNET_SYSERR if @a name is invalid
727 * #GNUNET_NO if @a name did not fit
728 * #GNUNET_OK if @a name was added to @a dst
731 GNUNET_DNSPARSER_builder_add_name (char *dst,
737 const char *idna_name;
745 return GNUNET_SYSERR;
748 (rc = idna_to_ascii_8z (name, &idna_start, IDNA_ALLOW_UNASSIGNED)))
750 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
751 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
756 idna_name = idna_start;
758 if (start + strlen (idna_name) + 2 > dst_len)
763 dot = strchr (idna_name, '.');
765 len = strlen (idna_name);
767 len = dot - idna_name;
768 if ( (len >= 64) || (len == 0) )
769 goto fail; /* segment too long or empty */
770 dst[pos++] = (char) (uint8_t) len;
771 memcpy (&dst[pos], idna_name, len);
773 idna_name += len + 1; /* also skip dot */
776 dst[pos++] = '\0'; /* terminator */
779 idn_free (idna_start);
786 idn_free (idna_start);
795 * Add a DNS query to the UDP packet at the given location.
797 * @param dst where to write the query
798 * @param dst_len number of bytes in @a dst
799 * @param off pointer to offset where to write the query (increment by bytes used)
800 * must not be changed if there is an error
801 * @param query query to write
802 * @return #GNUNET_SYSERR if @a query is invalid
803 * #GNUNET_NO if @a query did not fit
804 * #GNUNET_OK if @a query was added to @a dst
807 GNUNET_DNSPARSER_builder_add_query (char *dst,
810 const struct GNUNET_DNSPARSER_Query *query)
813 struct GNUNET_TUN_DnsQueryLine ql;
815 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
816 if (ret != GNUNET_OK)
818 ql.type = htons (query->type);
819 ql.class = htons (query->class);
820 memcpy (&dst[*off], &ql, sizeof (ql));
821 (*off) += sizeof (ql);
827 * Add an MX record to the UDP packet at the given location.
829 * @param dst where to write the mx record
830 * @param dst_len number of bytes in @a dst
831 * @param off pointer to offset where to write the mx information (increment by bytes used);
832 * can also change if there was an error
833 * @param mx mx information to write
834 * @return #GNUNET_SYSERR if @a mx is invalid
835 * #GNUNET_NO if @a mx did not fit
836 * #GNUNET_OK if @a mx was added to @a dst
839 GNUNET_DNSPARSER_builder_add_mx (char *dst,
842 const struct GNUNET_DNSPARSER_MxRecord *mx)
846 if (*off + sizeof (uint16_t) > dst_len)
848 mxpref = htons (mx->preference);
849 memcpy (&dst[*off], &mxpref, sizeof (mxpref));
850 (*off) += sizeof (mxpref);
851 return GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, mx->mxhost);
856 * Add an SOA record to the UDP packet at the given location.
858 * @param dst where to write the SOA record
859 * @param dst_len number of bytes in @a dst
860 * @param off pointer to offset where to write the SOA information (increment by bytes used)
861 * can also change if there was an error
862 * @param soa SOA information to write
863 * @return #GNUNET_SYSERR if @a soa is invalid
864 * #GNUNET_NO if @a soa did not fit
865 * #GNUNET_OK if @a soa was added to @a dst
868 GNUNET_DNSPARSER_builder_add_soa (char *dst,
871 const struct GNUNET_DNSPARSER_SoaRecord *soa)
873 struct GNUNET_TUN_DnsSoaRecord sd;
876 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
880 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
885 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
887 sd.serial = htonl (soa->serial);
888 sd.refresh = htonl (soa->refresh);
889 sd.retry = htonl (soa->retry);
890 sd.expire = htonl (soa->expire);
891 sd.minimum = htonl (soa->minimum_ttl);
892 memcpy (&dst[*off], &sd, sizeof (sd));
893 (*off) += sizeof (sd);
899 * Add an SRV record to the UDP packet at the given location.
901 * @param dst where to write the SRV record
902 * @param dst_len number of bytes in @a dst
903 * @param off pointer to offset where to write the SRV information (increment by bytes used)
904 * can also change if there was an error
905 * @param srv SRV information to write
906 * @return #GNUNET_SYSERR if @a srv is invalid
907 * #GNUNET_NO if @a srv did not fit
908 * #GNUNET_OK if @a srv was added to @a dst
911 GNUNET_DNSPARSER_builder_add_srv (char *dst,
914 const struct GNUNET_DNSPARSER_SrvRecord *srv)
916 struct GNUNET_TUN_DnsSrvRecord sd;
919 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
921 sd.prio = htons (srv->priority);
922 sd.weight = htons (srv->weight);
923 sd.port = htons (srv->port);
924 memcpy (&dst[*off], &sd, sizeof (sd));
925 (*off) += sizeof (sd);
926 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
936 * Add a DNS record to the UDP packet at the given location.
938 * @param dst where to write the query
939 * @param dst_len number of bytes in @a dst
940 * @param off pointer to offset where to write the query (increment by bytes used)
941 * must not be changed if there is an error
942 * @param record record to write
943 * @return #GNUNET_SYSERR if @a record is invalid
944 * #GNUNET_NO if @a record did not fit
945 * #GNUNET_OK if @a record was added to @a dst
948 add_record (char *dst,
951 const struct GNUNET_DNSPARSER_Record *record)
956 struct GNUNET_TUN_DnsRecordLine rl;
960 /* for SRV records, we can create the name from the details
961 of the record if needed */
963 if ( (GNUNET_DNSPARSER_TYPE_SRV == record->type) &&
965 GNUNET_asprintf (&name,
967 record->data.srv->service,
968 record->data.srv->proto,
969 record->data.srv->domain_name);
970 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine), off, name);
971 if (name != record->name)
973 if (GNUNET_OK != ret)
975 /* '*off' is now the position where we will need to write the record line */
977 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
978 switch (record->type)
980 case GNUNET_DNSPARSER_TYPE_MX:
981 ret = GNUNET_DNSPARSER_builder_add_mx (dst, dst_len, &pos, record->data.mx);
983 case GNUNET_DNSPARSER_TYPE_SOA:
984 ret = GNUNET_DNSPARSER_builder_add_soa (dst, dst_len, &pos, record->data.soa);
986 case GNUNET_DNSPARSER_TYPE_NS:
987 case GNUNET_DNSPARSER_TYPE_CNAME:
988 case GNUNET_DNSPARSER_TYPE_PTR:
989 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len, &pos, record->data.hostname);
991 case GNUNET_DNSPARSER_TYPE_SRV:
992 ret = GNUNET_DNSPARSER_builder_add_srv (dst, dst_len, &pos, record->data.srv);
995 if (pos + record->data.raw.data_len > dst_len)
1000 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
1001 pos += record->data.raw.data_len;
1005 if (GNUNET_OK != ret)
1011 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1013 /* record data too long */
1017 rl.type = htons (record->type);
1018 rl.class = htons (record->class);
1019 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1020 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1021 memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
1028 * Given a DNS packet @a p, generate the corresponding UDP payload.
1029 * Note that we do not attempt to pack the strings with pointers
1030 * as this would complicate the code and this is about being
1031 * simple and secure, not fast, fancy and broken like bind.
1033 * @param p packet to pack
1034 * @param max maximum allowed size for the resulting UDP payload
1035 * @param buf set to a buffer with the packed message
1036 * @param buf_length set to the length of @a buf
1037 * @return #GNUNET_SYSERR if @a p is invalid
1038 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1039 * #GNUNET_OK if @a p was packed completely into @a buf
1042 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1047 struct GNUNET_TUN_DnsHeader dns;
1054 if ( (p->num_queries > UINT16_MAX) ||
1055 (p->num_answers > UINT16_MAX) ||
1056 (p->num_authority_records > UINT16_MAX) ||
1057 (p->num_additional_records > UINT16_MAX) )
1058 return GNUNET_SYSERR;
1060 dns.flags = p->flags;
1061 dns.query_count = htons (p->num_queries);
1062 dns.answer_rcount = htons (p->num_answers);
1063 dns.authority_rcount = htons (p->num_authority_records);
1064 dns.additional_rcount = htons (p->num_additional_records);
1066 off = sizeof (struct GNUNET_TUN_DnsHeader);
1068 for (i=0;i<p->num_queries;i++)
1070 ret = GNUNET_DNSPARSER_builder_add_query (tmp, sizeof (tmp), &off, &p->queries[i]);
1071 if (GNUNET_SYSERR == ret)
1072 return GNUNET_SYSERR;
1073 if (GNUNET_NO == ret)
1075 dns.query_count = htons ((uint16_t) (i-1));
1080 for (i=0;i<p->num_answers;i++)
1082 ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
1083 if (GNUNET_SYSERR == ret)
1084 return GNUNET_SYSERR;
1085 if (GNUNET_NO == ret)
1087 dns.answer_rcount = htons ((uint16_t) (i-1));
1092 for (i=0;i<p->num_authority_records;i++)
1094 ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
1095 if (GNUNET_SYSERR == ret)
1096 return GNUNET_SYSERR;
1097 if (GNUNET_NO == ret)
1099 dns.authority_rcount = htons ((uint16_t) (i-1));
1104 for (i=0;i<p->num_additional_records;i++)
1106 ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
1107 if (GNUNET_SYSERR == ret)
1108 return GNUNET_SYSERR;
1109 if (GNUNET_NO == ret)
1111 dns.additional_rcount = htons (i-1);
1117 if (GNUNET_YES == trc)
1118 dns.flags.message_truncated = 1;
1119 memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
1121 *buf = GNUNET_malloc (off);
1123 memcpy (*buf, tmp, off);
1124 if (GNUNET_YES == trc)
1129 /* end of dnsparser.c */