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_WARNING,
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) )
820 goto fail; /* segment too long or empty */
822 dst[pos++] = (char) (uint8_t) len;
823 memcpy (&dst[pos], idna_name, len);
825 idna_name += len + 1; /* also skip dot */
828 dst[pos++] = '\0'; /* terminator */
831 idn_free (idna_start);
838 idn_free (idna_start);
847 * Add a DNS query to the UDP packet at the given location.
849 * @param dst where to write the query
850 * @param dst_len number of bytes in @a dst
851 * @param off pointer to offset where to write the query (increment by bytes used)
852 * must not be changed if there is an error
853 * @param query query to write
854 * @return #GNUNET_SYSERR if @a query is invalid
855 * #GNUNET_NO if @a query did not fit
856 * #GNUNET_OK if @a query was added to @a dst
859 GNUNET_DNSPARSER_builder_add_query (char *dst,
862 const struct GNUNET_DNSPARSER_Query *query)
865 struct GNUNET_TUN_DnsQueryLine ql;
867 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
868 if (ret != GNUNET_OK)
870 ql.type = htons (query->type);
871 ql.dns_traffic_class = htons (query->dns_traffic_class);
872 memcpy (&dst[*off], &ql, sizeof (ql));
873 (*off) += sizeof (ql);
879 * Add an MX record to the UDP packet at the given location.
881 * @param dst where to write the mx record
882 * @param dst_len number of bytes in @a dst
883 * @param off pointer to offset where to write the mx information (increment by bytes used);
884 * can also change if there was an error
885 * @param mx mx information to write
886 * @return #GNUNET_SYSERR if @a mx is invalid
887 * #GNUNET_NO if @a mx did not fit
888 * #GNUNET_OK if @a mx was added to @a dst
891 GNUNET_DNSPARSER_builder_add_mx (char *dst,
894 const struct GNUNET_DNSPARSER_MxRecord *mx)
898 if (*off + sizeof (uint16_t) > dst_len)
900 mxpref = htons (mx->preference);
901 memcpy (&dst[*off], &mxpref, sizeof (mxpref));
902 (*off) += sizeof (mxpref);
903 return GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, mx->mxhost);
908 * Add an SOA record to the UDP packet at the given location.
910 * @param dst where to write the SOA record
911 * @param dst_len number of bytes in @a dst
912 * @param off pointer to offset where to write the SOA information (increment by bytes used)
913 * can also change if there was an error
914 * @param soa SOA information to write
915 * @return #GNUNET_SYSERR if @a soa is invalid
916 * #GNUNET_NO if @a soa did not fit
917 * #GNUNET_OK if @a soa was added to @a dst
920 GNUNET_DNSPARSER_builder_add_soa (char *dst,
923 const struct GNUNET_DNSPARSER_SoaRecord *soa)
925 struct GNUNET_TUN_DnsSoaRecord sd;
928 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
932 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
937 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
939 sd.serial = htonl (soa->serial);
940 sd.refresh = htonl (soa->refresh);
941 sd.retry = htonl (soa->retry);
942 sd.expire = htonl (soa->expire);
943 sd.minimum = htonl (soa->minimum_ttl);
944 memcpy (&dst[*off], &sd, sizeof (sd));
945 (*off) += sizeof (sd);
951 * Add an SRV record to the UDP packet at the given location.
953 * @param dst where to write the SRV record
954 * @param dst_len number of bytes in @a dst
955 * @param off pointer to offset where to write the SRV information (increment by bytes used)
956 * can also change if there was an error
957 * @param srv SRV information to write
958 * @return #GNUNET_SYSERR if @a srv is invalid
959 * #GNUNET_NO if @a srv did not fit
960 * #GNUNET_OK if @a srv was added to @a dst
963 GNUNET_DNSPARSER_builder_add_srv (char *dst,
966 const struct GNUNET_DNSPARSER_SrvRecord *srv)
968 struct GNUNET_TUN_DnsSrvRecord sd;
971 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
973 sd.prio = htons (srv->priority);
974 sd.weight = htons (srv->weight);
975 sd.port = htons (srv->port);
976 memcpy (&dst[*off], &sd, sizeof (sd));
977 (*off) += sizeof (sd);
978 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
988 * Add a DNS record to the UDP packet at the given location.
990 * @param dst where to write the query
991 * @param dst_len number of bytes in @a dst
992 * @param off pointer to offset where to write the query (increment by bytes used)
993 * must not be changed if there is an error
994 * @param record record to write
995 * @return #GNUNET_SYSERR if @a record is invalid
996 * #GNUNET_NO if @a record did not fit
997 * #GNUNET_OK if @a record was added to @a dst
1000 add_record (char *dst,
1003 const struct GNUNET_DNSPARSER_Record *record)
1008 struct GNUNET_TUN_DnsRecordLine rl;
1012 /* for SRV records, we can create the name from the details
1013 of the record if needed */
1014 name = record->name;
1015 if ( (GNUNET_DNSPARSER_TYPE_SRV == record->type) &&
1017 GNUNET_asprintf (&name,
1019 record->data.srv->service,
1020 record->data.srv->proto,
1021 record->data.srv->domain_name);
1022 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine), off, name);
1023 if (name != record->name)
1025 if (GNUNET_OK != ret)
1027 /* '*off' is now the position where we will need to write the record line */
1029 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1030 switch (record->type)
1032 case GNUNET_DNSPARSER_TYPE_MX:
1033 ret = GNUNET_DNSPARSER_builder_add_mx (dst, dst_len, &pos, record->data.mx);
1035 case GNUNET_DNSPARSER_TYPE_SOA:
1036 ret = GNUNET_DNSPARSER_builder_add_soa (dst, dst_len, &pos, record->data.soa);
1038 case GNUNET_DNSPARSER_TYPE_NS:
1039 case GNUNET_DNSPARSER_TYPE_CNAME:
1040 case GNUNET_DNSPARSER_TYPE_PTR:
1041 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len, &pos, record->data.hostname);
1043 case GNUNET_DNSPARSER_TYPE_SRV:
1044 ret = GNUNET_DNSPARSER_builder_add_srv (dst, dst_len, &pos, record->data.srv);
1047 if (pos + record->data.raw.data_len > dst_len)
1052 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
1053 pos += record->data.raw.data_len;
1057 if (GNUNET_OK != ret)
1063 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1065 /* record data too long */
1069 rl.type = htons (record->type);
1070 rl.dns_traffic_class = htons (record->dns_traffic_class);
1071 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1072 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1073 memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
1080 * Given a DNS packet @a p, generate the corresponding UDP payload.
1081 * Note that we do not attempt to pack the strings with pointers
1082 * as this would complicate the code and this is about being
1083 * simple and secure, not fast, fancy and broken like bind.
1085 * @param p packet to pack
1086 * @param max maximum allowed size for the resulting UDP payload
1087 * @param buf set to a buffer with the packed message
1088 * @param buf_length set to the length of @a buf
1089 * @return #GNUNET_SYSERR if @a p is invalid
1090 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1091 * #GNUNET_OK if @a p was packed completely into @a buf
1094 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1099 struct GNUNET_TUN_DnsHeader dns;
1106 if ( (p->num_queries > UINT16_MAX) ||
1107 (p->num_answers > UINT16_MAX) ||
1108 (p->num_authority_records > UINT16_MAX) ||
1109 (p->num_additional_records > UINT16_MAX) )
1110 return GNUNET_SYSERR;
1112 dns.flags = p->flags;
1113 dns.query_count = htons (p->num_queries);
1114 dns.answer_rcount = htons (p->num_answers);
1115 dns.authority_rcount = htons (p->num_authority_records);
1116 dns.additional_rcount = htons (p->num_additional_records);
1118 off = sizeof (struct GNUNET_TUN_DnsHeader);
1120 for (i=0;i<p->num_queries;i++)
1122 ret = GNUNET_DNSPARSER_builder_add_query (tmp, sizeof (tmp), &off, &p->queries[i]);
1123 if (GNUNET_SYSERR == ret)
1124 return GNUNET_SYSERR;
1125 if (GNUNET_NO == ret)
1127 dns.query_count = htons ((uint16_t) (i-1));
1132 for (i=0;i<p->num_answers;i++)
1134 ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
1135 if (GNUNET_SYSERR == ret)
1136 return GNUNET_SYSERR;
1137 if (GNUNET_NO == ret)
1139 dns.answer_rcount = htons ((uint16_t) (i-1));
1144 for (i=0;i<p->num_authority_records;i++)
1146 ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
1147 if (GNUNET_SYSERR == ret)
1148 return GNUNET_SYSERR;
1149 if (GNUNET_NO == ret)
1151 dns.authority_rcount = htons ((uint16_t) (i-1));
1156 for (i=0;i<p->num_additional_records;i++)
1158 ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
1159 if (GNUNET_SYSERR == ret)
1160 return GNUNET_SYSERR;
1161 if (GNUNET_NO == ret)
1163 dns.additional_rcount = htons (i-1);
1169 if (GNUNET_YES == trc)
1170 dns.flags.message_truncated = 1;
1171 memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
1173 *buf = GNUNET_malloc (off);
1175 memcpy (*buf, tmp, off);
1176 if (GNUNET_YES == trc)
1181 /* end of dnsparser.c */