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_new (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_new (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_new (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 the given DNS record.
540 * @param r record to free
543 free_record (struct GNUNET_DNSPARSER_Record *r)
545 GNUNET_free_non_null (r->name);
548 case GNUNET_DNSPARSER_TYPE_MX:
549 free_mx (r->data.mx);
551 case GNUNET_DNSPARSER_TYPE_SOA:
552 free_soa (r->data.soa);
554 case GNUNET_DNSPARSER_TYPE_SRV:
555 free_srv (r->data.srv);
557 case GNUNET_DNSPARSER_TYPE_NS:
558 case GNUNET_DNSPARSER_TYPE_CNAME:
559 case GNUNET_DNSPARSER_TYPE_PTR:
560 GNUNET_free_non_null (r->data.hostname);
563 GNUNET_free_non_null (r->data.raw.data);
570 * Free memory taken by a packet.
572 * @param p packet to free
575 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
579 for (i=0;i<p->num_queries;i++)
580 GNUNET_free_non_null (p->queries[i].name);
581 GNUNET_free_non_null (p->queries);
582 for (i=0;i<p->num_answers;i++)
583 free_record (&p->answers[i]);
584 GNUNET_free_non_null (p->answers);
585 for (i=0;i<p->num_authority_records;i++)
586 free_record (&p->authority_records[i]);
587 GNUNET_free_non_null (p->authority_records);
588 for (i=0;i<p->num_additional_records;i++)
589 free_record (&p->additional_records[i]);
590 GNUNET_free_non_null (p->additional_records);
595 /* ********************** DNS packet assembly code **************** */
599 * Add a DNS name to the UDP packet at the given location, converting
600 * the name to IDNA notation as necessary.
602 * @param dst where to write the name (UDP packet)
603 * @param dst_len number of bytes in @a dst
604 * @param off pointer to offset where to write the name (increment by bytes used)
605 * must not be changed if there is an error
606 * @param name name to write
607 * @return #GNUNET_SYSERR if @a name is invalid
608 * #GNUNET_NO if @a name did not fit
609 * #GNUNET_OK if @a name was added to @a dst
612 GNUNET_DNSPARSER_builder_add_name (char *dst,
618 const char *idna_name;
626 return GNUNET_SYSERR;
629 (rc = idna_to_ascii_8z (name, &idna_start, IDNA_ALLOW_UNASSIGNED)))
631 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
632 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
637 idna_name = idna_start;
639 if (start + strlen (idna_name) + 2 > dst_len)
644 dot = strchr (idna_name, '.');
646 len = strlen (idna_name);
648 len = dot - idna_name;
649 if ( (len >= 64) || (len == 0) )
650 goto fail; /* segment too long or empty */
651 dst[pos++] = (char) (uint8_t) len;
652 memcpy (&dst[pos], idna_name, len);
654 idna_name += len + 1; /* also skip dot */
657 dst[pos++] = '\0'; /* terminator */
660 idn_free (idna_start);
667 idn_free (idna_start);
676 * Add a DNS query to the UDP packet at the given location.
678 * @param dst where to write the query
679 * @param dst_len number of bytes in @a dst
680 * @param off pointer to offset where to write the query (increment by bytes used)
681 * must not be changed if there is an error
682 * @param query query to write
683 * @return #GNUNET_SYSERR if @a query is invalid
684 * #GNUNET_NO if @a query did not fit
685 * #GNUNET_OK if @a query was added to @a dst
688 GNUNET_DNSPARSER_builder_add_query (char *dst,
691 const struct GNUNET_DNSPARSER_Query *query)
694 struct GNUNET_TUN_DnsQueryLine ql;
696 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
697 if (ret != GNUNET_OK)
699 ql.type = htons (query->type);
700 ql.class = htons (query->class);
701 memcpy (&dst[*off], &ql, sizeof (ql));
702 (*off) += sizeof (ql);
708 * Add an MX record to the UDP packet at the given location.
710 * @param dst where to write the mx record
711 * @param dst_len number of bytes in @a dst
712 * @param off pointer to offset where to write the mx information (increment by bytes used);
713 * can also change if there was an error
714 * @param mx mx information to write
715 * @return #GNUNET_SYSERR if @a mx is invalid
716 * #GNUNET_NO if @a mx did not fit
717 * #GNUNET_OK if @a mx was added to @a dst
720 GNUNET_DNSPARSER_builder_add_mx (char *dst,
723 const struct GNUNET_DNSPARSER_MxRecord *mx)
727 if (*off + sizeof (uint16_t) > dst_len)
729 mxpref = htons (mx->preference);
730 memcpy (&dst[*off], &mxpref, sizeof (mxpref));
731 (*off) += sizeof (mxpref);
732 return GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, mx->mxhost);
737 * Add an SOA record to the UDP packet at the given location.
739 * @param dst where to write the SOA record
740 * @param dst_len number of bytes in @a dst
741 * @param off pointer to offset where to write the SOA information (increment by bytes used)
742 * can also change if there was an error
743 * @param soa SOA information to write
744 * @return #GNUNET_SYSERR if @a soa is invalid
745 * #GNUNET_NO if @a soa did not fit
746 * #GNUNET_OK if @a soa was added to @a dst
749 GNUNET_DNSPARSER_builder_add_soa (char *dst,
752 const struct GNUNET_DNSPARSER_SoaRecord *soa)
754 struct GNUNET_TUN_DnsSoaRecord sd;
757 if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
761 (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
766 if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
768 sd.serial = htonl (soa->serial);
769 sd.refresh = htonl (soa->refresh);
770 sd.retry = htonl (soa->retry);
771 sd.expire = htonl (soa->expire);
772 sd.minimum = htonl (soa->minimum_ttl);
773 memcpy (&dst[*off], &sd, sizeof (sd));
774 (*off) += sizeof (sd);
780 * Add an SRV record to the UDP packet at the given location.
782 * @param dst where to write the SRV record
783 * @param dst_len number of bytes in @a dst
784 * @param off pointer to offset where to write the SRV information (increment by bytes used)
785 * can also change if there was an error
786 * @param srv SRV information to write
787 * @return #GNUNET_SYSERR if @a srv is invalid
788 * #GNUNET_NO if @a srv did not fit
789 * #GNUNET_OK if @a srv was added to @a dst
792 GNUNET_DNSPARSER_builder_add_srv (char *dst,
795 const struct GNUNET_DNSPARSER_SrvRecord *srv)
797 struct GNUNET_TUN_DnsSrvRecord sd;
800 if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
802 sd.prio = htons (srv->priority);
803 sd.weight = htons (srv->weight);
804 sd.port = htons (srv->port);
805 memcpy (&dst[*off], &sd, sizeof (sd));
806 (*off) += sizeof (sd);
807 if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
817 * Add a DNS record to the UDP packet at the given location.
819 * @param dst where to write the query
820 * @param dst_len number of bytes in @a dst
821 * @param off pointer to offset where to write the query (increment by bytes used)
822 * must not be changed if there is an error
823 * @param record record to write
824 * @return #GNUNET_SYSERR if @a record is invalid
825 * #GNUNET_NO if @a record did not fit
826 * #GNUNET_OK if @a record was added to @a dst
829 add_record (char *dst,
832 const struct GNUNET_DNSPARSER_Record *record)
837 struct GNUNET_TUN_DnsRecordLine rl;
841 /* for SRV records, we can create the name from the details
842 of the record if needed */
844 if ( (GNUNET_DNSPARSER_TYPE_SRV == record->type) &&
846 GNUNET_asprintf (&name,
848 record->data.srv->service,
849 record->data.srv->proto,
850 record->data.srv->domain_name);
851 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine), off, name);
852 if (name != record->name)
854 if (GNUNET_OK != ret)
856 /* '*off' is now the position where we will need to write the record line */
858 pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
859 switch (record->type)
861 case GNUNET_DNSPARSER_TYPE_MX:
862 ret = GNUNET_DNSPARSER_builder_add_mx (dst, dst_len, &pos, record->data.mx);
864 case GNUNET_DNSPARSER_TYPE_SOA:
865 ret = GNUNET_DNSPARSER_builder_add_soa (dst, dst_len, &pos, record->data.soa);
867 case GNUNET_DNSPARSER_TYPE_NS:
868 case GNUNET_DNSPARSER_TYPE_CNAME:
869 case GNUNET_DNSPARSER_TYPE_PTR:
870 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len, &pos, record->data.hostname);
872 case GNUNET_DNSPARSER_TYPE_SRV:
873 ret = GNUNET_DNSPARSER_builder_add_srv (dst, dst_len, &pos, record->data.srv);
876 if (pos + record->data.raw.data_len > dst_len)
881 memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
882 pos += record->data.raw.data_len;
886 if (GNUNET_OK != ret)
892 if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
894 /* record data too long */
898 rl.type = htons (record->type);
899 rl.class = htons (record->class);
900 rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
901 rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
902 memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
909 * Given a DNS packet @a p, generate the corresponding UDP payload.
910 * Note that we do not attempt to pack the strings with pointers
911 * as this would complicate the code and this is about being
912 * simple and secure, not fast, fancy and broken like bind.
914 * @param p packet to pack
915 * @param max maximum allowed size for the resulting UDP payload
916 * @param buf set to a buffer with the packed message
917 * @param buf_length set to the length of @a buf
918 * @return #GNUNET_SYSERR if @a p is invalid
919 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
920 * #GNUNET_OK if @a p was packed completely into @a buf
923 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
928 struct GNUNET_TUN_DnsHeader dns;
935 if ( (p->num_queries > UINT16_MAX) ||
936 (p->num_answers > UINT16_MAX) ||
937 (p->num_authority_records > UINT16_MAX) ||
938 (p->num_additional_records > UINT16_MAX) )
939 return GNUNET_SYSERR;
941 dns.flags = p->flags;
942 dns.query_count = htons (p->num_queries);
943 dns.answer_rcount = htons (p->num_answers);
944 dns.authority_rcount = htons (p->num_authority_records);
945 dns.additional_rcount = htons (p->num_additional_records);
947 off = sizeof (struct GNUNET_TUN_DnsHeader);
949 for (i=0;i<p->num_queries;i++)
951 ret = GNUNET_DNSPARSER_builder_add_query (tmp, sizeof (tmp), &off, &p->queries[i]);
952 if (GNUNET_SYSERR == ret)
953 return GNUNET_SYSERR;
954 if (GNUNET_NO == ret)
956 dns.query_count = htons ((uint16_t) (i-1));
961 for (i=0;i<p->num_answers;i++)
963 ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
964 if (GNUNET_SYSERR == ret)
965 return GNUNET_SYSERR;
966 if (GNUNET_NO == ret)
968 dns.answer_rcount = htons ((uint16_t) (i-1));
973 for (i=0;i<p->num_authority_records;i++)
975 ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
976 if (GNUNET_SYSERR == ret)
977 return GNUNET_SYSERR;
978 if (GNUNET_NO == ret)
980 dns.authority_rcount = htons ((uint16_t) (i-1));
985 for (i=0;i<p->num_additional_records;i++)
987 ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
988 if (GNUNET_SYSERR == ret)
989 return GNUNET_SYSERR;
990 if (GNUNET_NO == ret)
992 dns.additional_rcount = htons (i-1);
998 if (GNUNET_YES == trc)
999 dns.flags.message_truncated = 1;
1000 memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
1002 *buf = GNUNET_malloc (off);
1004 memcpy (*buf, tmp, off);
1005 if (GNUNET_YES == trc)
1010 /* end of dnsparser.c */