libidn + libidn2 support, as proposed by WJ Liu / multiSnow in a github gist.
[oweals/gnunet.git] / src / util / dnsparser.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2010-2014 GNUnet e.V.
4
5       GNUnet is free software: you can redistribute it and/or modify it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your option) any later version.
9
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       Affero General Public License for more details.
14
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @file util/dnsparser.c
21  * @brief helper library to parse DNS packets.
22  * @author Philipp Toelke
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #if defined(HAVE_LIBIDN2)
27 #include <idn2.h>
28 #elif defined(HAVE_LIBIDN)
29 #include <idna.h>
30 #endif
31 #if WINDOWS
32 #include <idn-free.h>
33 #endif
34 #include "gnunet_util_lib.h"
35
36
37 /**
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.
40  *
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
44  */
45 int
46 GNUNET_DNSPARSER_check_label (const char *label)
47 {
48   char *output;
49   size_t slen;
50
51   if (NULL != strchr (label, '.'))
52     return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */
53   if (IDNA_SUCCESS !=
54       idna_to_ascii_8z (label, &output, IDNA_ALLOW_UNASSIGNED))
55     return GNUNET_SYSERR;
56   slen = strlen (output);
57 #if WINDOWS
58   idn_free (output);
59 #else
60   free (output);
61 #endif
62   return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK;
63 }
64
65
66 /**
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.
69  *
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
73  */
74 int
75 GNUNET_DNSPARSER_check_name (const char *name)
76 {
77   char *ldup;
78   char *output;
79   size_t slen;
80   char *tok;
81
82   ldup = GNUNET_strdup (name);
83   for (tok = strtok (ldup, "."); NULL != tok; tok = strtok (NULL, "."))
84     if (GNUNET_OK !=
85         GNUNET_DNSPARSER_check_label (tok))
86     {
87       GNUNET_free (ldup);
88       return GNUNET_SYSERR;
89     }
90   GNUNET_free (ldup);
91   if (IDNA_SUCCESS !=
92       idna_to_ascii_8z (name, &output, IDNA_ALLOW_UNASSIGNED))
93     return GNUNET_SYSERR;
94   slen = strlen (output);
95 #if WINDOWS
96   idn_free (output);
97 #else
98   free (output);
99 #endif
100   return (slen > 253) ? GNUNET_SYSERR : GNUNET_OK;
101 }
102
103
104 /**
105  * Free SOA information record.
106  *
107  * @param soa record to free
108  */
109 void
110 GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
111 {
112   if (NULL == soa)
113     return;
114   GNUNET_free_non_null (soa->mname);
115   GNUNET_free_non_null (soa->rname);
116   GNUNET_free (soa);
117 }
118
119
120 /**
121  * Free CERT information record.
122  *
123  * @param cert record to free
124  */
125 void
126 GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert)
127 {
128   if (NULL == cert)
129     return;
130   GNUNET_free_non_null (cert->certificate_data);
131   GNUNET_free (cert);
132 }
133
134
135 /**
136  * Free SRV information record.
137  *
138  * @param srv record to free
139  */
140 void
141 GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
142 {
143   if (NULL == srv)
144     return;
145   GNUNET_free_non_null (srv->target);
146   GNUNET_free (srv);
147 }
148
149
150 /**
151  * Free MX information record.
152  *
153  * @param mx record to free
154  */
155 void
156 GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
157 {
158   if (NULL == mx)
159     return;
160   GNUNET_free_non_null (mx->mxhost);
161   GNUNET_free (mx);
162 }
163
164
165 /**
166  * Free the given DNS record.
167  *
168  * @param r record to free
169  */
170 void
171 GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
172 {
173   GNUNET_free_non_null (r->name);
174   switch (r->type)
175   {
176   case GNUNET_DNSPARSER_TYPE_MX:
177     GNUNET_DNSPARSER_free_mx (r->data.mx);
178     break;
179   case GNUNET_DNSPARSER_TYPE_SOA:
180     GNUNET_DNSPARSER_free_soa (r->data.soa);
181     break;
182   case GNUNET_DNSPARSER_TYPE_SRV:
183     GNUNET_DNSPARSER_free_srv (r->data.srv);
184     break;
185   case GNUNET_DNSPARSER_TYPE_CERT:
186     GNUNET_DNSPARSER_free_cert (r->data.cert);
187     break;
188   case GNUNET_DNSPARSER_TYPE_NS:
189   case GNUNET_DNSPARSER_TYPE_CNAME:
190   case GNUNET_DNSPARSER_TYPE_PTR:
191     GNUNET_free_non_null (r->data.hostname);
192     break;
193   default:
194     GNUNET_free_non_null (r->data.raw.data);
195     break;
196   }
197 }
198
199
200 /**
201  * Parse name inside of a DNS query or record.
202  *
203  * @param udp_payload entire UDP payload
204  * @param udp_payload_length length of @a udp_payload
205  * @param off pointer to the offset of the name to parse in the udp_payload (to be
206  *                    incremented by the size of the name)
207  * @param depth current depth of our recursion (to prevent stack overflow)
208  * @return name as 0-terminated C string on success, NULL if the payload is malformed
209  */
210 static char *
211 parse_name (const char *udp_payload,
212             size_t udp_payload_length,
213             size_t *off,
214             unsigned int depth)
215 {
216   const uint8_t *input = (const uint8_t *) udp_payload;
217   char *ret;
218   char *tmp;
219   char *xstr;
220   uint8_t len;
221   size_t xoff;
222   char *utf8;
223   Idna_rc rc;
224
225   ret = GNUNET_strdup ("");
226   while (1)
227   {
228     if (*off >= udp_payload_length)
229     {
230       GNUNET_break_op (0);
231       goto error;
232     }
233     len = input[*off];
234     if (0 == len)
235     {
236       (*off)++;
237       break;
238     }
239     if (len < 64)
240     {
241       if (*off + 1 + len > udp_payload_length)
242       {
243         GNUNET_break_op (0);
244         goto error;
245       }
246       GNUNET_asprintf (&tmp,
247                        "%.*s",
248                        (int) len,
249                        &udp_payload[*off + 1]);
250       if (IDNA_SUCCESS !=
251           (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
252       {
253         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
254                     _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
255                     tmp,
256                     idna_strerror (rc));
257         GNUNET_free (tmp);
258         GNUNET_asprintf (&tmp,
259                          "%s%.*s.",
260                          ret,
261                          (int) len,
262                          &udp_payload[*off + 1]);
263       }
264       else
265       {
266         GNUNET_free (tmp);
267         GNUNET_asprintf (&tmp,
268                          "%s%s.",
269                          ret,
270                          utf8);
271 #if WINDOWS
272         idn_free (utf8);
273 #else
274         free (utf8);
275 #endif
276       }
277       GNUNET_free (ret);
278       ret = tmp;
279       *off += 1 + len;
280     }
281     else if ((64 | 128) == (len & (64 | 128)) )
282     {
283       if (depth > 32)
284       {
285         GNUNET_break_op (0);
286         goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
287       }
288       /* pointer to string */
289       if (*off + 1 > udp_payload_length)
290       {
291         GNUNET_break_op (0);
292         goto error;
293       }
294       xoff = ((len - (64 | 128)) << 8) + input[*off+1];
295       xstr = parse_name (udp_payload,
296                          udp_payload_length,
297                          &xoff,
298                          depth + 1);
299       if (NULL == xstr)
300       {
301         GNUNET_break_op (0);
302         goto error;
303       }
304       GNUNET_asprintf (&tmp,
305                        "%s%s.",
306                        ret,
307                        xstr);
308       GNUNET_free (ret);
309       GNUNET_free (xstr);
310       ret = tmp;
311       if (strlen (ret) > udp_payload_length)
312       {
313         GNUNET_break_op (0);
314         goto error; /* we are looping (building an infinite string) */
315       }
316       *off += 2;
317       /* pointers always terminate names */
318       break;
319     }
320     else
321     {
322       /* neither pointer nor inline string, not supported... */
323       GNUNET_break_op (0);
324       goto error;
325     }
326   }
327   if (0 < strlen(ret))
328     ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
329   return ret;
330  error:
331   GNUNET_break_op (0);
332   GNUNET_free (ret);
333   return NULL;
334 }
335
336
337 /**
338  * Parse name inside of a DNS query or record.
339  *
340  * @param udp_payload entire UDP payload
341  * @param udp_payload_length length of @a udp_payload
342  * @param off pointer to the offset of the name to parse in the udp_payload (to be
343  *                    incremented by the size of the name)
344  * @return name as 0-terminated C string on success, NULL if the payload is malformed
345  */
346 char *
347 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
348                              size_t udp_payload_length,
349                              size_t *off)
350 {
351   return parse_name (udp_payload, udp_payload_length, off, 0);
352 }
353
354
355 /**
356  * Parse a DNS query entry.
357  *
358  * @param udp_payload entire UDP payload
359  * @param udp_payload_length length of @a udp_payload
360  * @param off pointer to the offset of the query to parse in the udp_payload (to be
361  *                    incremented by the size of the query)
362  * @param q where to write the query information
363  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
364  */
365 int
366 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
367                               size_t udp_payload_length,
368                               size_t *off,
369                               struct GNUNET_DNSPARSER_Query *q)
370 {
371   char *name;
372   struct GNUNET_TUN_DnsQueryLine ql;
373
374   name = GNUNET_DNSPARSER_parse_name (udp_payload,
375                                       udp_payload_length,
376                                       off);
377   if (NULL == name)
378   {
379     GNUNET_break_op (0);
380     return GNUNET_SYSERR;
381   }
382   q->name = name;
383   if (*off + sizeof (struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
384   {
385     GNUNET_break_op (0);
386     return GNUNET_SYSERR;
387   }
388   GNUNET_memcpy (&ql, &udp_payload[*off], sizeof (ql));
389   *off += sizeof (ql);
390   q->type = ntohs (ql.type);
391   q->dns_traffic_class = ntohs (ql.dns_traffic_class);
392   return GNUNET_OK;
393 }
394
395
396 /**
397  * Parse a DNS SOA record.
398  *
399  * @param udp_payload reference to UDP packet
400  * @param udp_payload_length length of @a udp_payload
401  * @param off pointer to the offset of the query to parse in the SOA record (to be
402  *                    incremented by the size of the record), unchanged on error
403  * @return the parsed SOA record, NULL on error
404  */
405 struct GNUNET_DNSPARSER_SoaRecord *
406 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
407                             size_t udp_payload_length,
408                             size_t *off)
409 {
410   struct GNUNET_DNSPARSER_SoaRecord *soa;
411   struct GNUNET_TUN_DnsSoaRecord soa_bin;
412   size_t old_off;
413
414   old_off = *off;
415   soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
416   soa->mname = GNUNET_DNSPARSER_parse_name (udp_payload,
417                                             udp_payload_length,
418                                             off);
419   soa->rname = GNUNET_DNSPARSER_parse_name (udp_payload,
420                                             udp_payload_length,
421                                             off);
422   if ( (NULL == soa->mname) ||
423        (NULL == soa->rname) ||
424        (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length) )
425   {
426     GNUNET_break_op (0);
427     GNUNET_DNSPARSER_free_soa (soa);
428     *off = old_off;
429     return NULL;
430   }
431   GNUNET_memcpy (&soa_bin,
432           &udp_payload[*off],
433           sizeof (struct GNUNET_TUN_DnsSoaRecord));
434   soa->serial = ntohl (soa_bin.serial);
435   soa->refresh = ntohl (soa_bin.refresh);
436   soa->retry = ntohl (soa_bin.retry);
437   soa->expire = ntohl (soa_bin.expire);
438   soa->minimum_ttl = ntohl (soa_bin.minimum);
439   (*off) += sizeof (struct GNUNET_TUN_DnsSoaRecord);
440   return soa;
441 }
442
443
444 /**
445  * Parse a DNS MX record.
446  *
447  * @param udp_payload reference to UDP packet
448  * @param udp_payload_length length of @a udp_payload
449  * @param off pointer to the offset of the query to parse in the MX record (to be
450  *                    incremented by the size of the record), unchanged on error
451  * @return the parsed MX record, NULL on error
452  */
453 struct GNUNET_DNSPARSER_MxRecord *
454 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
455                            size_t udp_payload_length,
456                            size_t *off)
457 {
458   struct GNUNET_DNSPARSER_MxRecord *mx;
459   uint16_t mxpref;
460   size_t old_off;
461
462   old_off = *off;
463   if (*off + sizeof (uint16_t) > udp_payload_length)
464   {
465     GNUNET_break_op (0);
466     return NULL;
467   }
468   GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
469   (*off) += sizeof (uint16_t);
470   mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
471   mx->preference = ntohs (mxpref);
472   mx->mxhost = GNUNET_DNSPARSER_parse_name (udp_payload,
473                                             udp_payload_length,
474                                             off);
475   if (NULL == mx->mxhost)
476   {
477     GNUNET_break_op (0);
478     GNUNET_DNSPARSER_free_mx (mx);
479     *off = old_off;
480     return NULL;
481   }
482   return mx;
483 }
484
485
486 /**
487  * Parse a DNS SRV record.
488  *
489  * @param udp_payload reference to UDP packet
490  * @param udp_payload_length length of @a udp_payload
491  * @param off pointer to the offset of the query to parse in the SRV record (to be
492  *                    incremented by the size of the record), unchanged on error
493  * @return the parsed SRV record, NULL on error
494  */
495 struct GNUNET_DNSPARSER_SrvRecord *
496 GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
497                             size_t udp_payload_length,
498                             size_t *off)
499 {
500   struct GNUNET_DNSPARSER_SrvRecord *srv;
501   struct GNUNET_TUN_DnsSrvRecord srv_bin;
502   size_t old_off;
503
504   old_off = *off;
505   if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
506     return NULL;
507   GNUNET_memcpy (&srv_bin,
508           &udp_payload[*off],
509           sizeof (struct GNUNET_TUN_DnsSrvRecord));
510   (*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
511   srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
512   srv->priority = ntohs (srv_bin.prio);
513   srv->weight = ntohs (srv_bin.weight);
514   srv->port = ntohs (srv_bin.port);
515   srv->target = GNUNET_DNSPARSER_parse_name (udp_payload,
516                                              udp_payload_length,
517                                              off);
518   if (NULL == srv->target)
519   {
520     GNUNET_DNSPARSER_free_srv (srv);
521     *off = old_off;
522     return NULL;
523   }
524   return srv;
525 }
526
527
528 /**
529  * Parse a DNS CERT record.
530  *
531  * @param udp_payload reference to UDP packet
532  * @param udp_payload_length length of @a udp_payload
533  * @param off pointer to the offset of the query to parse in the CERT record (to be
534  *                    incremented by the size of the record), unchanged on error
535  * @return the parsed CERT record, NULL on error
536  */
537 struct GNUNET_DNSPARSER_CertRecord *
538 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
539                              size_t udp_payload_length,
540                              size_t *off)
541 {
542   struct GNUNET_DNSPARSER_CertRecord *cert;
543   struct GNUNET_TUN_DnsCertRecord dcert;
544
545   if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
546   {
547     GNUNET_break_op (0);
548     return NULL;
549   }
550   GNUNET_memcpy (&dcert,
551                  &udp_payload[*off],
552                  sizeof (struct GNUNET_TUN_DnsCertRecord));
553   (*off) += sizeof (struct GNUNET_TUN_DnsCertRecord);
554   cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
555   cert->cert_type = ntohs (dcert.cert_type);
556   cert->cert_tag = ntohs (dcert.cert_tag);
557   cert->algorithm = dcert.algorithm;
558   cert->certificate_size = udp_payload_length - (*off);
559   cert->certificate_data = GNUNET_malloc (cert->certificate_size);
560   GNUNET_memcpy (cert->certificate_data,
561                  &udp_payload[*off],
562                  cert->certificate_size);
563   (*off) += cert->certificate_size;
564   return cert;
565 }
566
567
568 /**
569  * Parse a DNS record entry.
570  *
571  * @param udp_payload entire UDP payload
572  * @param udp_payload_length length of @a udp_payload
573  * @param off pointer to the offset of the record to parse in the udp_payload (to be
574  *                    incremented by the size of the record)
575  * @param r where to write the record information
576  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
577  */
578 int
579 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
580                                size_t udp_payload_length,
581                                size_t *off,
582                                struct GNUNET_DNSPARSER_Record *r)
583 {
584   char *name;
585   struct GNUNET_TUN_DnsRecordLine rl;
586   size_t old_off;
587   uint16_t data_len;
588
589   name = GNUNET_DNSPARSER_parse_name (udp_payload,
590                                       udp_payload_length,
591                                       off);
592   if (NULL == name)
593   {
594     GNUNET_break_op (0);
595     return GNUNET_SYSERR;
596   }
597   r->name = name;
598   if (*off + sizeof (struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
599   {
600     GNUNET_break_op (0);
601     return GNUNET_SYSERR;
602   }
603   GNUNET_memcpy (&rl, &udp_payload[*off], sizeof (rl));
604   (*off) += sizeof (rl);
605   r->type = ntohs (rl.type);
606   r->dns_traffic_class = ntohs (rl.dns_traffic_class);
607   r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
608                                                                                         ntohl (rl.ttl)));
609   data_len = ntohs (rl.data_len);
610   if (*off + data_len > udp_payload_length)
611   {
612     GNUNET_break_op (0);
613     return GNUNET_SYSERR;
614   }
615   old_off = *off;
616   switch (r->type)
617   {
618   case GNUNET_DNSPARSER_TYPE_NS:
619   case GNUNET_DNSPARSER_TYPE_CNAME:
620   case GNUNET_DNSPARSER_TYPE_DNAME:
621   case GNUNET_DNSPARSER_TYPE_PTR:
622     r->data.hostname = GNUNET_DNSPARSER_parse_name (udp_payload,
623                                                     udp_payload_length,
624                                                     off);
625     if ( (NULL == r->data.hostname) ||
626          (old_off + data_len != *off) )
627       return GNUNET_SYSERR;
628     return GNUNET_OK;
629   case GNUNET_DNSPARSER_TYPE_SOA:
630     r->data.soa = GNUNET_DNSPARSER_parse_soa (udp_payload,
631                                               udp_payload_length,
632                                               off);
633     if ( (NULL == r->data.soa) ||
634          (old_off + data_len != *off) )
635     {
636       GNUNET_break_op (0);
637       return GNUNET_SYSERR;
638     }
639     return GNUNET_OK;
640   case GNUNET_DNSPARSER_TYPE_MX:
641     r->data.mx = GNUNET_DNSPARSER_parse_mx (udp_payload,
642                                             udp_payload_length,
643                                             off);
644     if ( (NULL == r->data.mx) ||
645          (old_off + data_len != *off) )
646     {
647       GNUNET_break_op (0);
648       return GNUNET_SYSERR;
649     }
650     return GNUNET_OK;
651   case GNUNET_DNSPARSER_TYPE_SRV:
652     r->data.srv = GNUNET_DNSPARSER_parse_srv (udp_payload,
653                                               udp_payload_length,
654                                               off);
655     if ( (NULL == r->data.srv) ||
656          (old_off + data_len != *off) )
657     {
658       GNUNET_break_op (0);
659       return GNUNET_SYSERR;
660     }
661     return GNUNET_OK;
662   default:
663     r->data.raw.data = GNUNET_malloc (data_len);
664     r->data.raw.data_len = data_len;
665     GNUNET_memcpy (r->data.raw.data,
666                    &udp_payload[*off],
667                    data_len);
668     break;
669   }
670   (*off) += data_len;
671   return GNUNET_OK;
672 }
673
674
675 /**
676  * Parse a UDP payload of a DNS packet in to a nice struct for further
677  * processing and manipulation.
678  *
679  * @param udp_payload wire-format of the DNS packet
680  * @param udp_payload_length number of bytes in @a udp_payload
681  * @return NULL on error, otherwise the parsed packet
682  */
683 struct GNUNET_DNSPARSER_Packet *
684 GNUNET_DNSPARSER_parse (const char *udp_payload,
685                         size_t udp_payload_length)
686 {
687   struct GNUNET_DNSPARSER_Packet *p;
688   const struct GNUNET_TUN_DnsHeader *dns;
689   size_t off;
690   unsigned int n;
691
692   if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
693     return NULL;
694   dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
695   off = sizeof (struct GNUNET_TUN_DnsHeader);
696   p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
697   p->flags = dns->flags;
698   p->id = dns->id;
699   n = ntohs (dns->query_count);
700   if (n > 0)
701   {
702     p->queries = GNUNET_new_array (n,
703                                    struct GNUNET_DNSPARSER_Query);
704     p->num_queries = n;
705     for (unsigned int i=0;i<n;i++)
706       if (GNUNET_OK !=
707           GNUNET_DNSPARSER_parse_query (udp_payload,
708                                         udp_payload_length,
709                                         &off,
710                                         &p->queries[i]))
711         goto error;
712   }
713   n = ntohs (dns->answer_rcount);
714   if (n > 0)
715   {
716     p->answers = GNUNET_new_array (n,
717                                    struct GNUNET_DNSPARSER_Record);
718     p->num_answers = n;
719     for (unsigned int i=0;i<n;i++)
720       if (GNUNET_OK !=
721           GNUNET_DNSPARSER_parse_record (udp_payload,
722                                          udp_payload_length,
723                                          &off,
724                                          &p->answers[i]))
725         goto error;
726   }
727   n = ntohs (dns->authority_rcount);
728   if (n > 0)
729   {
730     p->authority_records = GNUNET_new_array (n,
731                                              struct GNUNET_DNSPARSER_Record);
732     p->num_authority_records = n;
733     for (unsigned int i=0;i<n;i++)
734       if (GNUNET_OK !=
735           GNUNET_DNSPARSER_parse_record (udp_payload,
736                                          udp_payload_length,
737                                          &off,
738                                          &p->authority_records[i]))
739         goto error;
740   }
741   n = ntohs (dns->additional_rcount);
742   if (n > 0)
743   {
744     p->additional_records = GNUNET_new_array (n,
745                                               struct GNUNET_DNSPARSER_Record);
746     p->num_additional_records = n;
747     for (unsigned int i=0;i<n;i++)
748     {
749       if (GNUNET_OK !=
750           GNUNET_DNSPARSER_parse_record (udp_payload,
751                                          udp_payload_length,
752                                          &off,
753                                          &p->additional_records[i]))
754         goto error;
755     }
756   }
757   return p;
758  error:
759   GNUNET_break_op (0);
760   GNUNET_DNSPARSER_free_packet (p);
761   return NULL;
762 }
763
764
765 /**
766  * Duplicate (deep-copy) the given DNS record
767  *
768  * @param r the record
769  * @return the newly allocated record
770  */
771 struct GNUNET_DNSPARSER_Record *
772 GNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r)
773 {
774   struct GNUNET_DNSPARSER_Record *dup = GNUNET_memdup (r, sizeof (*r));
775
776   dup->name = GNUNET_strdup (r->name);
777   switch (r->type)
778   {
779     case GNUNET_DNSPARSER_TYPE_NS:
780     case GNUNET_DNSPARSER_TYPE_CNAME:
781     case GNUNET_DNSPARSER_TYPE_PTR:
782     {
783       dup->data.hostname = GNUNET_strdup (r->data.hostname);
784       break;
785     }
786     case GNUNET_DNSPARSER_TYPE_SOA:
787     {
788       dup->data.soa = GNUNET_DNSPARSER_duplicate_soa_record (r->data.soa);
789       break;
790     }
791     case GNUNET_DNSPARSER_TYPE_CERT:
792     {
793       dup->data.cert = GNUNET_DNSPARSER_duplicate_cert_record (r->data.cert);
794       break;
795     }
796     case GNUNET_DNSPARSER_TYPE_MX:
797     {
798       dup->data.mx = GNUNET_DNSPARSER_duplicate_mx_record (r->data.mx);
799       break;
800     }
801     case GNUNET_DNSPARSER_TYPE_SRV:
802     {
803       dup->data.srv = GNUNET_DNSPARSER_duplicate_srv_record (r->data.srv);
804       break;
805     }
806     default:
807     {
808       dup->data.raw.data = GNUNET_memdup (r->data.raw.data,
809                                           r->data.raw.data_len);
810     }
811   }
812   return dup;
813 }
814
815
816 /**
817  * Duplicate (deep-copy) the given DNS record
818  *
819  * @param r the record
820  * @return the newly allocated record
821  */
822 struct GNUNET_DNSPARSER_SoaRecord *
823 GNUNET_DNSPARSER_duplicate_soa_record (const struct GNUNET_DNSPARSER_SoaRecord *r)
824 {
825   struct GNUNET_DNSPARSER_SoaRecord *dup = GNUNET_memdup (r, sizeof (*r));
826
827   dup->mname = GNUNET_strdup (r->mname);
828   dup->rname = GNUNET_strdup (r->rname);
829   return dup;
830 }
831
832
833 /**
834  * Duplicate (deep-copy) the given DNS record
835  *
836  * @param r the record
837  * @return the newly allocated record
838  */
839 struct GNUNET_DNSPARSER_CertRecord *
840 GNUNET_DNSPARSER_duplicate_cert_record (const struct GNUNET_DNSPARSER_CertRecord *r)
841 {
842   struct GNUNET_DNSPARSER_CertRecord *dup = GNUNET_memdup (r, sizeof (*r));
843
844   dup->certificate_data = GNUNET_strdup (r->certificate_data);
845   return dup;
846 }
847
848
849 /**
850  * Duplicate (deep-copy) the given DNS record
851  *
852  * @param r the record
853  * @return the newly allocated record
854  */
855 struct GNUNET_DNSPARSER_MxRecord *
856 GNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r)
857 {
858   struct GNUNET_DNSPARSER_MxRecord *dup = GNUNET_memdup (r, sizeof (*r));
859
860   dup->mxhost = GNUNET_strdup (r->mxhost);
861   return dup;
862 }
863
864
865 /**
866  * Duplicate (deep-copy) the given DNS record
867  *
868  * @param r the record
869  * @return the newly allocated record
870  */
871 struct GNUNET_DNSPARSER_SrvRecord *
872 GNUNET_DNSPARSER_duplicate_srv_record (const struct GNUNET_DNSPARSER_SrvRecord *r)
873 {
874   struct GNUNET_DNSPARSER_SrvRecord *dup = GNUNET_memdup (r, sizeof (*r));
875
876   dup->target = GNUNET_strdup (r->target);
877   return dup;
878 }
879
880
881 /**
882  * Free memory taken by a packet.
883  *
884  * @param p packet to free
885  */
886 void
887 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
888 {
889   for (unsigned int i=0;i<p->num_queries;i++)
890     GNUNET_free_non_null (p->queries[i].name);
891   GNUNET_free_non_null (p->queries);
892   for (unsigned int i=0;i<p->num_answers;i++)
893     GNUNET_DNSPARSER_free_record (&p->answers[i]);
894   GNUNET_free_non_null (p->answers);
895   for (unsigned int i=0;i<p->num_authority_records;i++)
896     GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
897   GNUNET_free_non_null (p->authority_records);
898   for (unsigned int i=0;i<p->num_additional_records;i++)
899     GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
900   GNUNET_free_non_null (p->additional_records);
901   GNUNET_free (p);
902 }
903
904
905 /* ********************** DNS packet assembly code **************** */
906
907
908 /**
909  * Add a DNS name to the UDP packet at the given location, converting
910  * the name to IDNA notation as necessary.
911  *
912  * @param dst where to write the name (UDP packet)
913  * @param dst_len number of bytes in @a dst
914  * @param off pointer to offset where to write the name (increment by bytes used)
915  *            must not be changed if there is an error
916  * @param name name to write
917  * @return #GNUNET_SYSERR if @a name is invalid
918  *         #GNUNET_NO if @a name did not fit
919  *         #GNUNET_OK if @a name was added to @a dst
920  */
921 int
922 GNUNET_DNSPARSER_builder_add_name (char *dst,
923                                    size_t dst_len,
924                                    size_t *off,
925                                    const char *name)
926 {
927   const char *dot;
928   const char *idna_name;
929   char *idna_start;
930   size_t start;
931   size_t pos;
932   size_t len;
933   Idna_rc rc;
934
935   if (NULL == name)
936     return GNUNET_SYSERR;
937
938   if (IDNA_SUCCESS !=
939       (rc = idna_to_ascii_8z (name,
940                               &idna_start,
941                               IDNA_ALLOW_UNASSIGNED)))
942   {
943     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
944                 _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
945                 name,
946                 idna_strerror (rc));
947     return GNUNET_NO;
948   }
949   idna_name = idna_start;
950   start = *off;
951   if (start + strlen (idna_name) + 2 > dst_len)
952     goto fail;
953   pos = start;
954   do
955   {
956     dot = strchr (idna_name, '.');
957     if (NULL == dot)
958       len = strlen (idna_name);
959     else
960       len = dot - idna_name;
961     if ( (len >= 64) || (0 == len) )
962     {
963       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
964                   "Invalid DNS name `%s': label with %u characters encountered\n",
965                   name,
966                   (unsigned int) len);
967       goto fail; /* label too long or empty */
968     }
969     dst[pos++] = (char) (uint8_t) len;
970     GNUNET_memcpy (&dst[pos],
971                    idna_name,
972                    len);
973     pos += len;
974     idna_name += len + 1; /* also skip dot */
975   }
976   while (NULL != dot);
977   dst[pos++] = '\0'; /* terminator */
978   *off = pos;
979 #if WINDOWS
980   idn_free (idna_start);
981 #else
982   free (idna_start);
983 #endif
984   return GNUNET_OK;
985  fail:
986 #if WINDOWS
987   idn_free (idna_start);
988 #else
989   free (idna_start);
990 #endif
991   return GNUNET_NO;
992 }
993
994
995 /**
996  * Add a DNS query to the UDP packet at the given location.
997  *
998  * @param dst where to write the query
999  * @param dst_len number of bytes in @a dst
1000  * @param off pointer to offset where to write the query (increment by bytes used)
1001  *            must not be changed if there is an error
1002  * @param query query to write
1003  * @return #GNUNET_SYSERR if @a query is invalid
1004  *         #GNUNET_NO if @a query did not fit
1005  *         #GNUNET_OK if @a query was added to @a dst
1006  */
1007 int
1008 GNUNET_DNSPARSER_builder_add_query (char *dst,
1009                                     size_t dst_len,
1010                                     size_t *off,
1011                                     const struct GNUNET_DNSPARSER_Query *query)
1012 {
1013   int ret;
1014   struct GNUNET_TUN_DnsQueryLine ql;
1015
1016   ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsQueryLine), off, query->name);
1017   if (ret != GNUNET_OK)
1018     return ret;
1019   ql.type = htons (query->type);
1020   ql.dns_traffic_class = htons (query->dns_traffic_class);
1021   GNUNET_memcpy (&dst[*off], &ql, sizeof (ql));
1022   (*off) += sizeof (ql);
1023   return GNUNET_OK;
1024 }
1025
1026
1027 /**
1028  * Add an MX record to the UDP packet at the given location.
1029  *
1030  * @param dst where to write the mx record
1031  * @param dst_len number of bytes in @a dst
1032  * @param off pointer to offset where to write the mx information (increment by bytes used);
1033  *            can also change if there was an error
1034  * @param mx mx information to write
1035  * @return #GNUNET_SYSERR if @a mx is invalid
1036  *         #GNUNET_NO if @a mx did not fit
1037  *         #GNUNET_OK if @a mx was added to @a dst
1038  */
1039 int
1040 GNUNET_DNSPARSER_builder_add_mx (char *dst,
1041                                  size_t dst_len,
1042                                  size_t *off,
1043                                  const struct GNUNET_DNSPARSER_MxRecord *mx)
1044 {
1045   uint16_t mxpref;
1046
1047   if (*off + sizeof (uint16_t) > dst_len)
1048     return GNUNET_NO;
1049   mxpref = htons (mx->preference);
1050   GNUNET_memcpy (&dst[*off],
1051                  &mxpref,
1052                  sizeof (mxpref));
1053   (*off) += sizeof (mxpref);
1054   return GNUNET_DNSPARSER_builder_add_name (dst,
1055                                             dst_len,
1056                                             off,
1057                                             mx->mxhost);
1058 }
1059
1060
1061 /**
1062  * Add a CERT record to the UDP packet at the given location.
1063  *
1064  * @param dst where to write the CERT record
1065  * @param dst_len number of bytes in @a dst
1066  * @param off pointer to offset where to write the CERT information (increment by bytes used);
1067  *            can also change if there was an error
1068  * @param cert CERT information to write
1069  * @return #GNUNET_SYSERR if @a cert is invalid
1070  *         #GNUNET_NO if @a cert did not fit
1071  *         #GNUNET_OK if @a cert was added to @a dst
1072  */
1073 int
1074 GNUNET_DNSPARSER_builder_add_cert (char *dst,
1075                                    size_t dst_len,
1076                                    size_t *off,
1077                                    const struct GNUNET_DNSPARSER_CertRecord *cert)
1078 {
1079   struct GNUNET_TUN_DnsCertRecord dcert;
1080
1081   if ( (cert->cert_type > UINT16_MAX) ||
1082        (cert->algorithm > UINT8_MAX) )
1083   {
1084     GNUNET_break (0);
1085     return GNUNET_SYSERR;
1086   }
1087   if (*off + sizeof (struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size > dst_len)
1088     return GNUNET_NO;
1089   dcert.cert_type = htons ((uint16_t) cert->cert_type);
1090   dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
1091   dcert.algorithm = (uint8_t) cert->algorithm;
1092   GNUNET_memcpy (&dst[*off], &dcert, sizeof (dcert));
1093   (*off) += sizeof (dcert);
1094   GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
1095   (*off) += cert->certificate_size;
1096   return GNUNET_OK;
1097 }
1098
1099
1100 /**
1101  * Add an SOA record to the UDP packet at the given location.
1102  *
1103  * @param dst where to write the SOA record
1104  * @param dst_len number of bytes in @a dst
1105  * @param off pointer to offset where to write the SOA information (increment by bytes used)
1106  *            can also change if there was an error
1107  * @param soa SOA information to write
1108  * @return #GNUNET_SYSERR if @a soa is invalid
1109  *         #GNUNET_NO if @a soa did not fit
1110  *         #GNUNET_OK if @a soa was added to @a dst
1111  */
1112 int
1113 GNUNET_DNSPARSER_builder_add_soa (char *dst,
1114                                   size_t dst_len,
1115                                   size_t *off,
1116                                   const struct GNUNET_DNSPARSER_SoaRecord *soa)
1117 {
1118   struct GNUNET_TUN_DnsSoaRecord sd;
1119   int ret;
1120
1121   if ( (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1122                                                                dst_len,
1123                                                                off,
1124                                                                soa->mname))) ||
1125        (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1126                                                                dst_len,
1127                                                                off,
1128                                                                soa->rname)) ) )
1129     return ret;
1130   if (*off + sizeof (struct GNUNET_TUN_DnsSoaRecord) > dst_len)
1131     return GNUNET_NO;
1132   sd.serial = htonl (soa->serial);
1133   sd.refresh = htonl (soa->refresh);
1134   sd.retry = htonl (soa->retry);
1135   sd.expire = htonl (soa->expire);
1136   sd.minimum = htonl (soa->minimum_ttl);
1137   GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
1138   (*off) += sizeof (sd);
1139   return GNUNET_OK;
1140 }
1141
1142
1143 /**
1144  * Add an SRV record to the UDP packet at the given location.
1145  *
1146  * @param dst where to write the SRV record
1147  * @param dst_len number of bytes in @a dst
1148  * @param off pointer to offset where to write the SRV information (increment by bytes used)
1149  *            can also change if there was an error
1150  * @param srv SRV information to write
1151  * @return #GNUNET_SYSERR if @a srv is invalid
1152  *         #GNUNET_NO if @a srv did not fit
1153  *         #GNUNET_OK if @a srv was added to @a dst
1154  */
1155 int
1156 GNUNET_DNSPARSER_builder_add_srv (char *dst,
1157                                   size_t dst_len,
1158                                   size_t *off,
1159                                   const struct GNUNET_DNSPARSER_SrvRecord *srv)
1160 {
1161   struct GNUNET_TUN_DnsSrvRecord sd;
1162   int ret;
1163
1164   if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > dst_len)
1165     return GNUNET_NO;
1166   sd.prio = htons (srv->priority);
1167   sd.weight = htons (srv->weight);
1168   sd.port = htons (srv->port);
1169   GNUNET_memcpy (&dst[*off],
1170                  &sd,
1171                  sizeof (sd));
1172   (*off) += sizeof (sd);
1173   if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
1174                                                              dst_len,
1175                                                              off,
1176                                                              srv->target)))
1177     return ret;
1178   return GNUNET_OK;
1179 }
1180
1181
1182 /**
1183  * Add a DNS record to the UDP packet at the given location.
1184  *
1185  * @param dst where to write the query
1186  * @param dst_len number of bytes in @a dst
1187  * @param off pointer to offset where to write the query (increment by bytes used)
1188  *            must not be changed if there is an error
1189  * @param record record to write
1190  * @return #GNUNET_SYSERR if @a record is invalid
1191  *         #GNUNET_NO if @a record did not fit
1192  *         #GNUNET_OK if @a record was added to @a dst
1193  */
1194 static int
1195 add_record (char *dst,
1196             size_t dst_len,
1197             size_t *off,
1198             const struct GNUNET_DNSPARSER_Record *record)
1199 {
1200   int ret;
1201   size_t start;
1202   size_t pos;
1203   struct GNUNET_TUN_DnsRecordLine rl;
1204
1205   start = *off;
1206   ret = GNUNET_DNSPARSER_builder_add_name (dst,
1207                                            dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine),
1208                                            off,
1209                                            record->name);
1210   if (GNUNET_OK != ret)
1211     return ret;
1212   /* '*off' is now the position where we will need to write the record line */
1213
1214   pos = *off + sizeof (struct GNUNET_TUN_DnsRecordLine);
1215   switch (record->type)
1216   {
1217   case GNUNET_DNSPARSER_TYPE_MX:
1218     ret = GNUNET_DNSPARSER_builder_add_mx (dst,
1219                                            dst_len,
1220                                            &pos,
1221                                            record->data.mx);
1222     break;
1223   case GNUNET_DNSPARSER_TYPE_CERT:
1224     ret = GNUNET_DNSPARSER_builder_add_cert (dst,
1225                                              dst_len,
1226                                              &pos,
1227                                              record->data.cert);
1228     break;
1229   case GNUNET_DNSPARSER_TYPE_SOA:
1230     ret = GNUNET_DNSPARSER_builder_add_soa (dst,
1231                                             dst_len,
1232                                             &pos,
1233                                             record->data.soa);
1234     break;
1235   case GNUNET_DNSPARSER_TYPE_NS:
1236   case GNUNET_DNSPARSER_TYPE_CNAME:
1237   case GNUNET_DNSPARSER_TYPE_PTR:
1238     ret = GNUNET_DNSPARSER_builder_add_name (dst,
1239                                              dst_len,
1240                                              &pos,
1241                                              record->data.hostname);
1242     break;
1243   case GNUNET_DNSPARSER_TYPE_SRV:
1244     ret = GNUNET_DNSPARSER_builder_add_srv (dst,
1245                                             dst_len,
1246                                             &pos,
1247                                             record->data.srv);
1248     break;
1249   default:
1250     if (pos + record->data.raw.data_len > dst_len)
1251     {
1252       ret = GNUNET_NO;
1253       break;
1254     }
1255     GNUNET_memcpy (&dst[pos],
1256                    record->data.raw.data,
1257                    record->data.raw.data_len);
1258     pos += record->data.raw.data_len;
1259     ret = GNUNET_OK;
1260     break;
1261   }
1262   if (GNUNET_OK != ret)
1263   {
1264     *off = start;
1265     return GNUNET_NO;
1266   }
1267
1268   if (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1269   {
1270     /* record data too long */
1271     *off = start;
1272     return GNUNET_NO;
1273   }
1274   rl.type = htons (record->type);
1275   rl.dns_traffic_class = htons (record->dns_traffic_class);
1276   rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
1277   rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
1278   GNUNET_memcpy (&dst[*off],
1279                  &rl,
1280                  sizeof (struct GNUNET_TUN_DnsRecordLine));
1281   *off = pos;
1282   return GNUNET_OK;
1283 }
1284
1285
1286 /**
1287  * Given a DNS packet @a p, generate the corresponding UDP payload.
1288  * Note that we do not attempt to pack the strings with pointers
1289  * as this would complicate the code and this is about being
1290  * simple and secure, not fast, fancy and broken like bind.
1291  *
1292  * @param p packet to pack
1293  * @param max maximum allowed size for the resulting UDP payload
1294  * @param buf set to a buffer with the packed message
1295  * @param buf_length set to the length of @a buf
1296  * @return #GNUNET_SYSERR if @a p is invalid
1297  *         #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
1298  *         #GNUNET_OK if @a p was packed completely into @a buf
1299  */
1300 int
1301 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
1302                        uint16_t max,
1303                        char **buf,
1304                        size_t *buf_length)
1305 {
1306   struct GNUNET_TUN_DnsHeader dns;
1307   size_t off;
1308   char tmp[max];
1309   int ret;
1310   int trc;
1311
1312   if ( (p->num_queries > UINT16_MAX) ||
1313        (p->num_answers > UINT16_MAX) ||
1314        (p->num_authority_records > UINT16_MAX) ||
1315        (p->num_additional_records > UINT16_MAX) )
1316     return GNUNET_SYSERR;
1317   dns.id = p->id;
1318   dns.flags = p->flags;
1319   dns.query_count = htons (p->num_queries);
1320   dns.answer_rcount = htons (p->num_answers);
1321   dns.authority_rcount = htons (p->num_authority_records);
1322   dns.additional_rcount = htons (p->num_additional_records);
1323
1324   off = sizeof (struct GNUNET_TUN_DnsHeader);
1325   trc = GNUNET_NO;
1326   for (unsigned int i=0;i<p->num_queries;i++)
1327   {
1328     ret = GNUNET_DNSPARSER_builder_add_query (tmp,
1329                                               sizeof (tmp),
1330                                               &off,
1331                                               &p->queries[i]);
1332     if (GNUNET_SYSERR == ret)
1333       return GNUNET_SYSERR;
1334     if (GNUNET_NO == ret)
1335     {
1336       dns.query_count = htons ((uint16_t) (i-1));
1337       trc = GNUNET_YES;
1338       break;
1339     }
1340   }
1341   for (unsigned int i=0;i<p->num_answers;i++)
1342   {
1343     ret = add_record (tmp,
1344                       sizeof (tmp),
1345                       &off,
1346                       &p->answers[i]);
1347     if (GNUNET_SYSERR == ret)
1348       return GNUNET_SYSERR;
1349     if (GNUNET_NO == ret)
1350     {
1351       dns.answer_rcount = htons ((uint16_t) (i-1));
1352       trc = GNUNET_YES;
1353       break;
1354     }
1355   }
1356   for (unsigned int i=0;i<p->num_authority_records;i++)
1357   {
1358     ret = add_record (tmp,
1359                       sizeof (tmp),
1360                       &off,
1361                       &p->authority_records[i]);
1362     if (GNUNET_SYSERR == ret)
1363       return GNUNET_SYSERR;
1364     if (GNUNET_NO == ret)
1365     {
1366       dns.authority_rcount = htons ((uint16_t) (i-1));
1367       trc = GNUNET_YES;
1368       break;
1369     }
1370   }
1371   for (unsigned int i=0;i<p->num_additional_records;i++)
1372   {
1373     ret = add_record (tmp,
1374                       sizeof (tmp),
1375                       &off,
1376                       &p->additional_records[i]);
1377     if (GNUNET_SYSERR == ret)
1378       return GNUNET_SYSERR;
1379     if (GNUNET_NO == ret)
1380     {
1381       dns.additional_rcount = htons (i-1);
1382       trc = GNUNET_YES;
1383       break;
1384     }
1385   }
1386
1387   if (GNUNET_YES == trc)
1388     dns.flags.message_truncated = 1;
1389   GNUNET_memcpy (tmp,
1390                  &dns,
1391                  sizeof (struct GNUNET_TUN_DnsHeader));
1392
1393   *buf = GNUNET_malloc (off);
1394   *buf_length = off;
1395   GNUNET_memcpy (*buf,
1396                  tmp,
1397                  off);
1398   if (GNUNET_YES == trc)
1399     return GNUNET_NO;
1400   return GNUNET_OK;
1401 }
1402
1403
1404 /**
1405  * Convert a block of binary data to HEX.
1406  *
1407  * @param data binary data to convert
1408  * @param data_size number of bytes in @a data
1409  * @return HEX string (lower case)
1410  */
1411 char *
1412 GNUNET_DNSPARSER_bin_to_hex (const void *data,
1413                              size_t data_size)
1414 {
1415   char *ret;
1416   size_t off;
1417   const uint8_t *idata;
1418
1419   idata = data;
1420   ret = GNUNET_malloc (data_size * 2 + 1);
1421   for (off = 0; off < data_size; off++)
1422     sprintf (&ret[off * 2],
1423              "%02x",
1424              idata[off]);
1425   return ret;
1426 }
1427
1428
1429 /**
1430  * Convert a HEX string to block of binary data.
1431  *
1432  * @param hex HEX string to convert (may contain mixed case)
1433  * @param data where to write result, must be
1434  *             at least `strlen(hex)/2` bytes long
1435  * @return number of bytes written to data
1436  */
1437 size_t
1438 GNUNET_DNSPARSER_hex_to_bin (const char *hex,
1439                              void *data)
1440 {
1441   size_t data_size;
1442   size_t off;
1443   uint8_t *idata;
1444   unsigned int h;
1445   char in[3];
1446
1447   data_size = strlen (hex) / 2;
1448   idata = data;
1449   in[2] = '\0';
1450   for (off = 0; off < data_size; off++)
1451   {
1452     in[0] = tolower ((unsigned char) hex[off * 2]);
1453     in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
1454     if (1 != sscanf (in, "%x", &h))
1455       return off;
1456     idata[off] = (uint8_t) h;
1457   }
1458   return off;
1459 }
1460
1461
1462 /* end of dnsparser.c */