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 * Parse name inside of a DNS query or record.
107 * @param udp_payload entire UDP payload
108 * @param udp_payload_length length of @a udp_payload
109 * @param off pointer to the offset of the name to parse in the udp_payload (to be
110 * incremented by the size of the name)
111 * @param depth current depth of our recursion (to prevent stack overflow)
112 * @return name as 0-terminated C string on success, NULL if the payload is malformed
115 parse_name (const char *udp_payload,
116 size_t udp_payload_length,
120 const uint8_t *input = (const uint8_t *) udp_payload;
129 ret = GNUNET_strdup ("");
132 if (*off >= udp_payload_length)
142 if (*off + 1 + len > udp_payload_length)
144 GNUNET_asprintf (&tmp,
147 &udp_payload[*off + 1]);
149 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
151 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
152 _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
156 GNUNET_asprintf (&tmp,
160 &udp_payload[*off + 1]);
165 GNUNET_asprintf (&tmp,
179 else if ((64 | 128) == (len & (64 | 128)) )
182 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
183 /* pointer to string */
184 if (*off + 1 > udp_payload_length)
186 xoff = ((len - (64 | 128)) << 8) + input[*off+1];
187 xstr = parse_name (udp_payload,
193 GNUNET_asprintf (&tmp,
200 if (strlen (ret) > udp_payload_length)
201 goto error; /* we are looping (building an infinite string) */
203 /* pointers always terminate names */
208 /* neither pointer nor inline string, not supported... */
213 ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
222 * Parse a DNS query entry.
224 * @param udp_payload entire UDP payload
225 * @param udp_payload_length length of @a udp_payload
226 * @param off pointer to the offset of the query to parse in the udp_payload (to be
227 * incremented by the size of the query)
228 * @param q where to write the query information
229 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
232 parse_query (const char *udp_payload,
233 size_t udp_payload_length,
235 struct GNUNET_DNSPARSER_Query *q)
238 struct GNUNET_TUN_DnsQueryLine ql;
240 name = parse_name (udp_payload,
244 return GNUNET_SYSERR;
246 if (*off + sizeof (struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
247 return GNUNET_SYSERR;
248 memcpy (&ql, &udp_payload[*off], sizeof (ql));
250 q->type = ntohs (ql.type);
251 q->class = ntohs (ql.class);
257 * Parse a DNS record entry.
259 * @param udp_payload entire UDP payload
260 * @param udp_payload_length length of @a udp_payload
261 * @param off pointer to the offset of the record to parse in the udp_payload (to be
262 * incremented by the size of the record)
263 * @param r where to write the record information
264 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
267 parse_record (const char *udp_payload,
268 size_t udp_payload_length,
270 struct GNUNET_DNSPARSER_Record *r)
273 struct GNUNET_TUN_DnsRecordLine rl;
275 struct GNUNET_TUN_DnsSoaRecord soa;
278 struct GNUNET_TUN_DnsSrvRecord srv;
282 name = parse_name (udp_payload,
286 return GNUNET_SYSERR;
288 if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
289 return GNUNET_SYSERR;
290 memcpy (&rl, &udp_payload[*off], sizeof (rl));
291 (*off) += sizeof (rl);
292 r->type = ntohs (rl.type);
293 r->class = ntohs (rl.class);
294 r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
296 data_len = ntohs (rl.data_len);
297 if (*off + data_len > udp_payload_length)
298 return GNUNET_SYSERR;
301 case GNUNET_DNSPARSER_TYPE_NS:
302 case GNUNET_DNSPARSER_TYPE_CNAME:
303 case GNUNET_DNSPARSER_TYPE_PTR:
305 r->data.hostname = parse_name (udp_payload,
308 if ( (NULL == r->data.hostname) ||
309 (old_off + data_len != *off) )
310 return GNUNET_SYSERR;
312 case GNUNET_DNSPARSER_TYPE_SOA:
314 r->data.soa = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_SoaRecord));
315 r->data.soa->mname = parse_name (udp_payload,
318 r->data.soa->rname = parse_name (udp_payload,
321 if ( (NULL == r->data.soa->mname) ||
322 (NULL == r->data.soa->rname) ||
323 (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length) )
324 return GNUNET_SYSERR;
325 memcpy (&soa, &udp_payload[*off], sizeof (struct GNUNET_TUN_DnsSoaRecord));
326 r->data.soa->serial = ntohl (soa.serial);
327 r->data.soa->refresh = ntohl (soa.refresh);
328 r->data.soa->retry = ntohl (soa.retry);
329 r->data.soa->expire = ntohl (soa.expire);
330 r->data.soa->minimum_ttl = ntohl (soa.minimum);
331 (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
332 if (old_off + data_len != *off)
333 return GNUNET_SYSERR;
335 case GNUNET_DNSPARSER_TYPE_MX:
337 if (*off + sizeof (uint16_t) > udp_payload_length)
338 return GNUNET_SYSERR;
339 memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
340 (*off) += sizeof (uint16_t);
341 r->data.mx = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_MxRecord));
342 r->data.mx->preference = ntohs (mxpref);
343 r->data.mx->mxhost = parse_name (udp_payload,
346 if (old_off + data_len != *off)
347 return GNUNET_SYSERR;
349 case GNUNET_DNSPARSER_TYPE_SRV:
351 return GNUNET_SYSERR; /* all valid srv names must start with "_" */
352 if (NULL == strstr (r->name, "._"))
353 return GNUNET_SYSERR; /* necessary string from "._$PROTO" not present */
355 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
356 return GNUNET_SYSERR;
357 memcpy (&srv, &udp_payload[*off], sizeof (struct GNUNET_TUN_DnsSrvRecord));
358 (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
359 r->data.srv = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_SrvRecord));
360 r->data.srv->priority = ntohs (srv.prio);
361 r->data.srv->weight = ntohs (srv.weight);
362 r->data.srv->port = ntohs (srv.port);
363 /* parse 'data.hostname' into components, which are
364 "_$SERVICE._$PROTO.$DOMAIN_NAME" */
365 ndup = GNUNET_strdup (r->name);
366 tok = strtok (ndup, ".");
367 GNUNET_assert (NULL != tok);
368 GNUNET_assert ('_' == *tok);
369 r->data.srv->service = GNUNET_strdup (&tok[1]);
370 tok = strtok (NULL, ".");
371 if ( (NULL == tok) || ('_' != *tok) )
373 GNUNET_free (r->data.srv);
375 return GNUNET_SYSERR;
377 r->data.srv->proto = GNUNET_strdup (&tok[1]);
378 tok = strtok (NULL, ".");
381 GNUNET_free (r->data.srv);
383 return GNUNET_SYSERR;
385 r->data.srv->domain_name = GNUNET_strdup (tok);
387 r->data.srv->target = parse_name (udp_payload,
390 if (old_off + data_len != *off)
391 return GNUNET_SYSERR;
394 r->data.raw.data = GNUNET_malloc (data_len);
395 r->data.raw.data_len = data_len;
396 memcpy (r->data.raw.data, &udp_payload[*off], data_len);
405 * Parse a UDP payload of a DNS packet in to a nice struct for further
406 * processing and manipulation.
408 * @param udp_payload wire-format of the DNS packet
409 * @param udp_payload_length number of bytes in @a udp_payload
410 * @return NULL on error, otherwise the parsed packet
412 struct GNUNET_DNSPARSER_Packet *
413 GNUNET_DNSPARSER_parse (const char *udp_payload,
414 size_t udp_payload_length)
416 struct GNUNET_DNSPARSER_Packet *p;
417 const struct GNUNET_TUN_DnsHeader *dns;
422 if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
424 dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
425 off = sizeof (struct GNUNET_TUN_DnsHeader);
426 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
427 p->flags = dns->flags;
429 n = ntohs (dns->query_count);
432 p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
436 parse_query (udp_payload,
442 n = ntohs (dns->answer_rcount);
445 p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
449 parse_record (udp_payload,
455 n = ntohs (dns->authority_rcount);
458 p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
459 p->num_authority_records = n;
462 parse_record (udp_payload,
465 &p->authority_records[i]))
468 n = ntohs (dns->additional_rcount);
471 p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
472 p->num_additional_records = n;
475 parse_record (udp_payload,
478 &p->additional_records[i]))
483 GNUNET_DNSPARSER_free_packet (p);
489 * Free SOA information record.
491 * @param soa record to free
494 free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
498 GNUNET_free_non_null (soa->mname);
499 GNUNET_free_non_null (soa->rname);
505 * Free SRV information record.
507 * @param srv record to free
510 free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
514 GNUNET_free_non_null (srv->target);
515 GNUNET_free_non_null (srv->domain_name);
516 GNUNET_free_non_null (srv->proto);
517 GNUNET_free_non_null (srv->service);
523 * Free MX information record.
525 * @param mx record to free
528 free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
532 GNUNET_free_non_null (mx->mxhost);
538 free_record (struct GNUNET_DNSPARSER_Record *r)
540 GNUNET_free_non_null (r->name);
543 case GNUNET_DNSPARSER_TYPE_MX:
544 free_mx (r->data.mx);
546 case GNUNET_DNSPARSER_TYPE_SOA:
547 free_soa (r->data.soa);
549 case GNUNET_DNSPARSER_TYPE_SRV:
550 free_srv (r->data.srv);
552 case GNUNET_DNSPARSER_TYPE_NS:
553 case GNUNET_DNSPARSER_TYPE_CNAME:
554 case GNUNET_DNSPARSER_TYPE_PTR:
555 GNUNET_free_non_null (r->data.hostname);
558 GNUNET_free_non_null (r->data.raw.data);
565 * Free memory taken by a packet.
567 * @param p packet to free
570 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
574 for (i=0;i<p->num_queries;i++)
575 GNUNET_free_non_null (p->queries[i].name);
576 GNUNET_free_non_null (p->queries);
577 for (i=0;i<p->num_answers;i++)
578 free_record (&p->answers[i]);
579 GNUNET_free_non_null (p->answers);
580 for (i=0;i<p->num_authority_records;i++)
581 free_record (&p->authority_records[i]);
582 GNUNET_free_non_null (p->authority_records);
583 for (i=0;i<p->num_additional_records;i++)
584 free_record (&p->additional_records[i]);
585 GNUNET_free_non_null (p->additional_records);
590 /* ********************** DNS packet assembly code **************** */
594 * Add a DNS name to the UDP packet at the given location.
596 * @param dst where to write the name
597 * @param dst_len number of bytes in @a dst
598 * @param off pointer to offset where to write the name (increment by bytes used)
599 * must not be changed if there is an error
600 * @param name name to write
601 * @return #GNUNET_SYSERR if @a name is invalid
602 * #GNUNET_NO if @a name did not fit
603 * #GNUNET_OK if @a name was added to 'dst'
612 const char *idna_name;
620 return GNUNET_SYSERR;
623 (rc = idna_to_ascii_8z (name, &idna_start, IDNA_ALLOW_UNASSIGNED)))
625 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
626 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
631 idna_name = idna_start;
633 if (start + strlen (idna_name) + 2 > dst_len)
638 dot = strchr (idna_name, '.');
640 len = strlen (idna_name);
642 len = dot - idna_name;
643 if ( (len >= 64) || (len == 0) )
644 goto fail; /* segment too long or empty */
645 dst[pos++] = (char) (uint8_t) len;
646 memcpy (&dst[pos], idna_name, len);
648 idna_name += len + 1; /* also skip dot */
651 dst[pos++] = '\0'; /* terminator */
654 idn_free (idna_start);
661 idn_free (idna_start);
670 * Add a DNS query to the UDP packet at the given location.
672 * @param dst where to write the query
673 * @param dst_len number of bytes in dst
674 * @param off pointer to offset where to write the query (increment by bytes used)
675 * must not be changed if there is an error
676 * @param query query to write
677 * @return #GNUNET_SYSERR if 'query' is invalid
678 * #GNUNET_NO if 'query' did not fit
679 * #GNUNET_OK if 'query' was added to @a dst
682 add_query (char *dst,
685 const struct GNUNET_DNSPARSER_Query *query)
688 struct GNUNET_TUN_DnsQueryLine ql;
690 ret = add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
691 if (ret != GNUNET_OK)
693 ql.type = htons (query->type);
694 ql.class = htons (query->class);
695 memcpy (&dst[*off], &ql, sizeof (ql));
696 (*off) += sizeof (ql);
702 * Add an MX record to the UDP packet at the given location.
704 * @param dst where to write the mx record
705 * @param dst_len number of bytes in dst
706 * @param off pointer to offset where to write the mx information (increment by bytes used);
707 * can also change if there was an error
708 * @param mx mx information to write
709 * @return #GNUNET_SYSERR if @a mx is invalid
710 * #GNUNET_NO if @a mx did not fit
711 * #GNUNET_OK if @a mx was added to @a dst
717 const struct GNUNET_DNSPARSER_MxRecord *mx)
721 if (*off + sizeof (uint16_t) > dst_len)
723 mxpref = htons (mx->preference);
724 memcpy (&dst[*off], &mxpref, sizeof (mxpref));
725 (*off) += sizeof (mxpref);
726 return add_name (dst, dst_len, off, mx->mxhost);
731 * Add an SOA record to the UDP packet at the given location.
733 * @param dst where to write the SOA record
734 * @param dst_len number of bytes in dst
735 * @param off pointer to offset where to write the SOA information (increment by bytes used)
736 * can also change if there was an error
737 * @param soa SOA information to write
738 * @return #GNUNET_SYSERR if @a soa is invalid
739 * #GNUNET_NO if @a soa did not fit
740 * #GNUNET_OK if @a soa was added to 'dst'
746 const struct GNUNET_DNSPARSER_SoaRecord *soa)
748 struct GNUNET_TUN_DnsSoaRecord sd;
751 if ( (GNUNET_OK != (ret = add_name (dst,
755 (GNUNET_OK != (ret = add_name (dst,
760 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
762 sd.serial = htonl (soa->serial);
763 sd.refresh = htonl (soa->refresh);
764 sd.retry = htonl (soa->retry);
765 sd.expire = htonl (soa->expire);
766 sd.minimum = htonl (soa->minimum_ttl);
767 memcpy (&dst[*off], &sd, sizeof (sd));
768 (*off) += sizeof (sd);
774 * Add an SRV record to the UDP packet at the given location.
776 * @param dst where to write the SRV record
777 * @param dst_len number of bytes in dst
778 * @param off pointer to offset where to write the SRV information (increment by bytes used)
779 * can also change if there was an error
780 * @param srv SRV information to write
781 * @return #GNUNET_SYSERR if @a srv is invalid
782 * #GNUNET_NO if @a srv did not fit
783 * #GNUNET_OK if @a srv was added to 'dst'
789 const struct GNUNET_DNSPARSER_SrvRecord *srv)
791 struct GNUNET_TUN_DnsSrvRecord sd;
794 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
796 sd.prio = htons (srv->priority);
797 sd.weight = htons (srv->weight);
798 sd.port = htons (srv->port);
799 memcpy (&dst[*off], &sd, sizeof (sd));
800 (*off) += sizeof (sd);
801 if (GNUNET_OK != (ret = add_name (dst,
811 * Add a DNS record to the UDP packet at the given location.
813 * @param dst where to write the query
814 * @param dst_len number of bytes in dst
815 * @param off pointer to offset where to write the query (increment by bytes used)
816 * must not be changed if there is an error
817 * @param record record to write
818 * @return #GNUNET_SYSERR if 'record' is invalid
819 * #GNUNET_NO if 'record' did not fit
820 * #GNUNET_OK if 'record' was added to @a dst
823 add_record (char *dst,
826 const struct GNUNET_DNSPARSER_Record *record)
831 struct GNUNET_TUN_DnsRecordLine rl;
835 /* for SRV records, we can create the name from the details
836 of the record if needed */
838 if ( (GNUNET_DNSPARSER_TYPE_SRV == record->type) &&
840 GNUNET_asprintf (&name,
842 record->data.srv->service,
843 record->data.srv->proto,
844 record->data.srv->domain_name);
845 ret = add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine), off, name);
846 if (name != record->name)
848 if (GNUNET_OK != ret)
850 /* '*off' is now the position where we will need to write the record line */
852 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
853 switch (record->type)
855 case GNUNET_DNSPARSER_TYPE_MX:
856 ret = add_mx (dst, dst_len, &pos, record->data.mx);
858 case GNUNET_DNSPARSER_TYPE_SOA:
859 ret = add_soa (dst, dst_len, &pos, record->data.soa);
861 case GNUNET_DNSPARSER_TYPE_NS:
862 case GNUNET_DNSPARSER_TYPE_CNAME:
863 case GNUNET_DNSPARSER_TYPE_PTR:
864 ret = add_name (dst, dst_len, &pos, record->data.hostname);
866 case GNUNET_DNSPARSER_TYPE_SRV:
867 ret = add_srv (dst, dst_len, &pos, record->data.srv);
870 if (pos + record->data.raw.data_len > dst_len)
875 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
876 pos += record->data.raw.data_len;
880 if (GNUNET_OK != ret)
886 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
888 /* record data too long */
892 rl.type = htons (record->type);
893 rl.class = htons (record->class);
894 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
895 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
896 memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
903 * Given a DNS packet, generate the corresponding UDP payload.
904 * Note that we do not attempt to pack the strings with pointers
905 * as this would complicate the code and this is about being
906 * simple and secure, not fast, fancy and broken like bind.
908 * @param p packet to pack
909 * @param max maximum allowed size for the resulting UDP payload
910 * @param buf set to a buffer with the packed message
911 * @param buf_length set to the length of @a buf
912 * @return #GNUNET_SYSERR if @a p is invalid
913 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
914 * #GNUNET_OK if @a p was packed completely into @a buf
917 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
922 struct GNUNET_TUN_DnsHeader dns;
929 if ( (p->num_queries > UINT16_MAX) ||
930 (p->num_answers > UINT16_MAX) ||
931 (p->num_authority_records > UINT16_MAX) ||
932 (p->num_additional_records > UINT16_MAX) )
933 return GNUNET_SYSERR;
935 dns.flags = p->flags;
936 dns.query_count = htons (p->num_queries);
937 dns.answer_rcount = htons (p->num_answers);
938 dns.authority_rcount = htons (p->num_authority_records);
939 dns.additional_rcount = htons (p->num_additional_records);
941 off = sizeof (struct GNUNET_TUN_DnsHeader);
943 for (i=0;i<p->num_queries;i++)
945 ret = add_query (tmp, sizeof (tmp), &off, &p->queries[i]);
946 if (GNUNET_SYSERR == ret)
947 return GNUNET_SYSERR;
948 if (GNUNET_NO == ret)
950 dns.query_count = htons ((uint16_t) (i-1));
955 for (i=0;i<p->num_answers;i++)
957 ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
958 if (GNUNET_SYSERR == ret)
959 return GNUNET_SYSERR;
960 if (GNUNET_NO == ret)
962 dns.answer_rcount = htons ((uint16_t) (i-1));
967 for (i=0;i<p->num_authority_records;i++)
969 ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
970 if (GNUNET_SYSERR == ret)
971 return GNUNET_SYSERR;
972 if (GNUNET_NO == ret)
974 dns.authority_rcount = htons ((uint16_t) (i-1));
979 for (i=0;i<p->num_additional_records;i++)
981 ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
982 if (GNUNET_SYSERR == ret)
983 return GNUNET_SYSERR;
984 if (GNUNET_NO == ret)
986 dns.additional_rcount = htons (i-1);
992 if (GNUNET_YES == trc)
993 dns.flags.message_truncated = 1;
994 memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
996 *buf = GNUNET_malloc (off);
998 memcpy (*buf, tmp, off);
999 if (GNUNET_YES == trc)
1004 /* end of dnsparser.c */