-finishing SRV implementation (#2268)
[oweals/gnunet.git] / src / dns / dnsparser.c
1 /*
2       This file is part of GNUnet
3       (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
4
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 2, or (at your
8       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       General Public License for more details.
14
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.
19  */
20
21 /**
22  * @file dns/dnsparser.c
23  * @brief helper library to parse DNS packets. 
24  * @author Philipp Toelke
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dnsparser_lib.h"
30
31
32 GNUNET_NETWORK_STRUCT_BEGIN
33
34 /* FIXME: replace this one with the one from tcpip_tun.h!? */
35 /**
36  * Head of a any DNS message.
37  */
38 struct GNUNET_TUN_DnsHeader
39 {
40   /**
41    * Request/response ID. (NBO)
42    */
43   uint16_t id GNUNET_PACKED;
44
45   /**
46    * Flags for the operation.
47    */
48   struct GNUNET_DNSPARSER_Flags flags; 
49
50   /**
51    * number of questions (NBO)
52    */
53   uint16_t query_count GNUNET_PACKED;
54
55   /**
56    * number of answers (NBO)
57    */
58   uint16_t answer_rcount GNUNET_PACKED;
59
60   /**
61    * number of authority-records (NBO)
62    */
63   uint16_t authority_rcount GNUNET_PACKED;
64
65   /**
66    * number of additional records (NBO)
67    */
68   uint16_t additional_rcount GNUNET_PACKED;
69 };
70
71
72 /**
73  * DNS query prefix.
74  */
75 struct query_line
76 {
77   /**
78    * Desired type (GNUNET_DNSPARSER_TYPE_XXX). (NBO)
79    */
80   uint16_t type GNUNET_PACKED;
81
82   /**
83    * Desired class (usually GNUNET_DNSPARSER_CLASS_INTERNET). (NBO)
84    */
85   uint16_t class GNUNET_PACKED;
86 };
87
88
89 /**
90  * General DNS record prefix.
91  */
92 struct record_line
93 {
94   /**
95    * Record type (GNUNET_DNSPARSER_TYPE_XXX). (NBO)
96    */
97   uint16_t type GNUNET_PACKED;
98
99   /**
100    * Record class (usually GNUNET_DNSPARSER_CLASS_INTERNET). (NBO)
101    */
102   uint16_t class GNUNET_PACKED;
103
104   /**
105    * Expiration for the record (in seconds). (NBO)
106    */
107   uint32_t ttl GNUNET_PACKED;
108
109   /**
110    * Number of bytes of data that follow. (NBO)
111    */
112   uint16_t data_len GNUNET_PACKED;
113 };
114
115
116 /**
117  * Payload of DNS SOA record (header).
118  */
119 struct soa_data
120 {
121   /**
122    * The version number of the original copy of the zone.   (NBO)
123    */
124   uint32_t serial GNUNET_PACKED;
125   
126   /**
127    * Time interval before the zone should be refreshed. (NBO)
128    */
129   uint32_t refresh GNUNET_PACKED;
130   
131   /**
132    * Time interval that should elapse before a failed refresh should
133    * be retried. (NBO)
134    */
135   uint32_t retry GNUNET_PACKED;
136  
137   /**
138    * Time value that specifies the upper limit on the time interval
139    * that can elapse before the zone is no longer authoritative. (NBO)
140    */
141   uint32_t expire GNUNET_PACKED;
142
143   /**
144    * The bit minimum TTL field that should be exported with any RR
145    * from this zone. (NBO)
146    */
147   uint32_t minimum GNUNET_PACKED;
148 };
149
150
151 /**
152  * Payload of DNS SRV record (header).
153  */
154 struct srv_data
155 {
156
157   /**
158    * Preference for this entry (lower value is higher preference).  Clients
159    * will contact hosts from the lowest-priority group first and fall back
160    * to higher priorities if the low-priority entries are unavailable. (NBO)
161    */
162   uint16_t prio GNUNET_PACKED;
163
164   /**
165    * Relative weight for records with the same priority.  Clients will use
166    * the hosts of the same (lowest) priority with a probability proportional
167    * to the weight given. (NBO)
168    */
169   uint16_t weight GNUNET_PACKED;
170
171   /**
172    * TCP or UDP port of the service. (NBO)
173    */
174   uint16_t port GNUNET_PACKED;
175
176   /* followed by 'target' name */
177 };
178
179 GNUNET_NETWORK_STRUCT_END
180
181
182 /**
183  * Parse name inside of a DNS query or record.
184  *
185  * @param udp_payload entire UDP payload
186  * @param udp_payload_length length of udp_payload
187  * @param off pointer to the offset of the name to parse in the udp_payload (to be
188  *                    incremented by the size of the name)
189  * @param depth current depth of our recursion (to prevent stack overflow)
190  * @return name as 0-terminated C string on success, NULL if the payload is malformed
191  */
192 static char *
193 parse_name (const char *udp_payload,
194             size_t udp_payload_length,
195             size_t *off,
196             unsigned int depth)
197 {
198   const uint8_t *input = (const uint8_t *) udp_payload;
199   char *ret;
200   char *tmp;
201   char *xstr;
202   uint8_t len;
203   size_t xoff;
204   
205   ret = GNUNET_strdup ("");
206   while (1)
207   {
208     if (*off >= udp_payload_length)
209       goto error;
210     len = input[*off];
211     if (0 == len)
212     {
213       (*off)++;
214       break;
215     }
216     if (len < 64)
217     {
218       if (*off + 1 + len > udp_payload_length)
219         goto error;
220       GNUNET_asprintf (&tmp,
221                        "%s%.*s.",
222                        ret,
223                        (int) len,
224                        &udp_payload[*off + 1]);
225       GNUNET_free (ret);
226       ret = tmp;
227       *off += 1 + len;
228     }
229     else if ((64 | 128) == (len & (64 | 128)) )
230     {
231       if (depth > 32)
232         goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
233       /* pointer to string */
234       if (*off + 1 > udp_payload_length)
235         goto error;
236       xoff = ((len - (64 | 128)) << 8) + input[*off+1];
237       xstr = parse_name (udp_payload,
238                          udp_payload_length,
239                          &xoff,
240                          depth + 1);
241       if (NULL == xstr)
242         goto error;
243       GNUNET_asprintf (&tmp,
244                        "%s%s.",
245                        ret,
246                        xstr);
247       GNUNET_free (ret);
248       GNUNET_free (xstr);
249       ret = tmp;
250       if (strlen (ret) > udp_payload_length)
251         goto error; /* we are looping (building an infinite string) */
252       *off += 2;
253       /* pointers always terminate names */
254       break;
255     } 
256     else
257     {
258       /* neither pointer nor inline string, not supported... */
259       goto error;
260     }
261   }
262   if (0 < strlen(ret))
263     ret[strlen(ret)-1] = '\0'; /* eat tailing '.' */
264   return ret;
265  error:  
266   GNUNET_free (ret);
267   return NULL;
268 }
269
270
271 /**
272  * Parse a DNS query entry.
273  *
274  * @param udp_payload entire UDP payload
275  * @param udp_payload_length length of udp_payload
276  * @param off pointer to the offset of the query to parse in the udp_payload (to be
277  *                    incremented by the size of the query)
278  * @param q where to write the query information
279  * @return GNUNET_OK on success, GNUNET_SYSERR if the query is malformed
280  */
281 static int
282 parse_query (const char *udp_payload,
283              size_t udp_payload_length,
284              size_t *off,
285              struct GNUNET_DNSPARSER_Query *q)
286 {
287   char *name;
288   struct query_line ql;
289
290   name = parse_name (udp_payload, 
291                      udp_payload_length,
292                      off, 0);
293   if (NULL == name)
294     return GNUNET_SYSERR;
295   q->name = name;
296   if (*off + sizeof (struct query_line) > udp_payload_length)
297     return GNUNET_SYSERR;
298   memcpy (&ql, &udp_payload[*off], sizeof (ql));
299   *off += sizeof (ql);
300   q->type = ntohs (ql.type);
301   q->class = ntohs (ql.class);
302   return GNUNET_OK;
303 }
304
305
306 /**
307  * Parse a DNS record entry.
308  *
309  * @param udp_payload entire UDP payload
310  * @param udp_payload_length length of udp_payload
311  * @param off pointer to the offset of the record to parse in the udp_payload (to be
312  *                    incremented by the size of the record)
313  * @param r where to write the record information
314  * @return GNUNET_OK on success, GNUNET_SYSERR if the record is malformed
315  */
316 static int
317 parse_record (const char *udp_payload,
318               size_t udp_payload_length,
319               size_t *off,
320               struct GNUNET_DNSPARSER_Record *r)
321 {
322   char *name;
323   struct record_line rl;
324   size_t old_off;
325   struct soa_data soa;
326   uint16_t mxpref;
327   uint16_t data_len;
328   struct srv_data srv;
329   char *ndup;
330   char *tok;
331
332   name = parse_name (udp_payload, 
333                      udp_payload_length,
334                      off, 0);
335   if (NULL == name)
336     return GNUNET_SYSERR;
337   r->name = name;
338   if (*off + sizeof (struct record_line) > udp_payload_length)
339     return GNUNET_SYSERR;
340   memcpy (&rl, &udp_payload[*off], sizeof (rl));
341   (*off) += sizeof (rl);
342   r->type = ntohs (rl.type);
343   r->class = ntohs (rl.class);
344   r->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
345                                                                                         ntohl (rl.ttl)));
346   data_len = ntohs (rl.data_len);
347   if (*off + data_len > udp_payload_length)
348     return GNUNET_SYSERR;
349   switch (r->type)
350   {
351   case GNUNET_DNSPARSER_TYPE_NS:
352   case GNUNET_DNSPARSER_TYPE_CNAME:
353   case GNUNET_DNSPARSER_TYPE_PTR:
354     old_off = *off;
355     r->data.hostname = parse_name (udp_payload,
356                                    udp_payload_length,
357                                    off, 0);    
358     if ( (NULL == r->data.hostname) ||
359          (old_off + data_len != *off) )
360       return GNUNET_SYSERR;
361     return GNUNET_OK;
362   case GNUNET_DNSPARSER_TYPE_SOA:
363     old_off = *off;
364     r->data.soa = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_SoaRecord));
365     r->data.soa->mname = parse_name (udp_payload,
366                                      udp_payload_length,
367                                      off, 0);
368     r->data.soa->rname = parse_name (udp_payload,
369                                      udp_payload_length,
370                                      off, 0);
371     if ( (NULL == r->data.soa->mname) ||
372          (NULL == r->data.soa->rname) ||
373          (*off + sizeof (struct soa_data) > udp_payload_length) )
374       return GNUNET_SYSERR;
375     memcpy (&soa, &udp_payload[*off], sizeof (struct soa_data));
376     r->data.soa->serial = ntohl (soa.serial);
377     r->data.soa->refresh = ntohl (soa.refresh);
378     r->data.soa->retry = ntohl (soa.retry);
379     r->data.soa->expire = ntohl (soa.expire);
380     r->data.soa->minimum_ttl = ntohl (soa.minimum);
381     (*off) += sizeof (struct soa_data);
382     if (old_off + data_len != *off) 
383       return GNUNET_SYSERR;
384     return GNUNET_OK;
385   case GNUNET_DNSPARSER_TYPE_MX:
386     old_off = *off;
387     if (*off + sizeof (uint16_t) > udp_payload_length)
388       return GNUNET_SYSERR;
389     memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));    
390     (*off) += sizeof (uint16_t);
391     r->data.mx = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_MxRecord));
392     r->data.mx->preference = ntohs (mxpref);
393     r->data.mx->mxhost = parse_name (udp_payload,
394                                      udp_payload_length,
395                                      off, 0);
396     if (old_off + data_len != *off) 
397       return GNUNET_SYSERR;
398     return GNUNET_OK;
399   case GNUNET_DNSPARSER_TYPE_SRV:
400     if ('_' != *r->name)
401       return GNUNET_SYSERR; /* all valid srv names must start with "_" */
402     if (NULL == strstr (r->name, "._"))
403       return GNUNET_SYSERR; /* necessary string from "._$PROTO" not present */
404     old_off = *off;
405     if (*off + sizeof (struct srv_data) > udp_payload_length)
406       return GNUNET_SYSERR;
407     memcpy (&srv, &udp_payload[*off], sizeof (struct srv_data));    
408     (*off) += sizeof (struct srv_data);
409     r->data.srv = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_SrvRecord));
410     r->data.srv->priority = ntohs (srv.prio);
411     r->data.srv->weight = ntohs (srv.weight);
412     r->data.srv->port = ntohs (srv.port);
413     /* parse 'data.hostname' into components, which are
414        "_$SERVICE._$PROTO.$DOMAIN_NAME" */
415     ndup = GNUNET_strdup (r->name);
416     tok = strtok (ndup, ".");
417     GNUNET_assert ('_' == *tok);
418     r->data.srv->service = GNUNET_strdup (&tok[1]);
419     tok = strtok (NULL, ".");
420     if ('_' != *tok)
421     {
422       GNUNET_free (r->data.srv);
423       GNUNET_free (ndup);
424       return GNUNET_SYSERR;
425     }
426     r->data.srv->proto = GNUNET_strdup (&tok[1]);
427     tok = strtok (NULL, ".");
428     if (NULL == tok)
429     {
430       GNUNET_free (r->data.srv);
431       GNUNET_free (ndup);
432       return GNUNET_SYSERR;
433     }
434     r->data.srv->domain_name = GNUNET_strdup (tok);
435     GNUNET_free (ndup);
436     r->data.srv->target = parse_name (udp_payload,
437                                       udp_payload_length,
438                                       off, 0);
439     if (old_off + data_len != *off) 
440       return GNUNET_SYSERR;
441     return GNUNET_OK;
442   default:
443     r->data.raw.data = GNUNET_malloc (data_len);
444     r->data.raw.data_len = data_len;
445     memcpy (r->data.raw.data, &udp_payload[*off], data_len);
446     break;
447   }
448   (*off) += data_len;
449   return GNUNET_OK;  
450 }
451
452
453 /**
454  * Parse a UDP payload of a DNS packet in to a nice struct for further
455  * processing and manipulation.
456  *
457  * @param udp_payload wire-format of the DNS packet
458  * @param udp_payload_length number of bytes in udp_payload 
459  * @return NULL on error, otherwise the parsed packet
460  */
461 struct GNUNET_DNSPARSER_Packet *
462 GNUNET_DNSPARSER_parse (const char *udp_payload,
463                         size_t udp_payload_length)
464 {
465   struct GNUNET_DNSPARSER_Packet *p;
466   const struct GNUNET_TUN_DnsHeader *dns;
467   size_t off;
468   unsigned int n;  
469   unsigned int i;
470
471   if (udp_payload_length < sizeof (struct GNUNET_TUN_DnsHeader))
472     return NULL;
473   dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
474   off = sizeof (struct GNUNET_TUN_DnsHeader);
475   p = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_Packet));
476   p->flags = dns->flags;
477   p->id = dns->id;
478   n = ntohs (dns->query_count);
479   if (n > 0)
480   {
481     p->queries = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Query));
482     p->num_queries = n;
483     for (i=0;i<n;i++)
484       if (GNUNET_OK !=
485           parse_query (udp_payload,
486                        udp_payload_length,
487                        &off,
488                        &p->queries[i]))
489         goto error;
490   }
491   n = ntohs (dns->answer_rcount);
492   if (n > 0)
493   {
494     p->answers = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
495     p->num_answers = n;
496     for (i=0;i<n;i++)
497       if (GNUNET_OK !=
498           parse_record (udp_payload,
499                         udp_payload_length,
500                         &off,
501                         &p->answers[i]))
502         goto error;
503   }
504   n = ntohs (dns->authority_rcount);
505   if (n > 0)
506   {
507     p->authority_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
508     p->num_authority_records = n;
509     for (i=0;i<n;i++)
510       if (GNUNET_OK !=
511           parse_record (udp_payload,
512                         udp_payload_length,
513                         &off,
514                         &p->authority_records[i]))
515         goto error;  
516   }
517   n = ntohs (dns->additional_rcount);
518   if (n > 0)
519   {
520     p->additional_records = GNUNET_malloc (n * sizeof (struct GNUNET_DNSPARSER_Record));
521     p->num_additional_records = n;
522     for (i=0;i<n;i++)
523       if (GNUNET_OK !=
524           parse_record (udp_payload,
525                         udp_payload_length,
526                         &off,
527                         &p->additional_records[i]))
528         goto error;   
529   }
530   return p;
531  error:
532   GNUNET_DNSPARSER_free_packet (p);
533   return NULL;
534 }
535
536
537 /**
538  * Free SOA information record.
539  *
540  * @param soa record to free
541  */
542 static void
543 free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
544 {
545   if (NULL == soa)
546     return;
547   GNUNET_free_non_null (soa->mname);
548   GNUNET_free_non_null (soa->rname);
549   GNUNET_free (soa);      
550 }
551
552
553 /**
554  * Free SRV information record.
555  *
556  * @param srv record to free
557  */
558 static void
559 free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
560 {
561   if (NULL == srv)
562     return;
563   GNUNET_free_non_null (srv->target);
564   GNUNET_free_non_null (srv->domain_name);
565   GNUNET_free_non_null (srv->proto);
566   GNUNET_free_non_null (srv->service);
567   GNUNET_free (srv);      
568 }
569
570
571 /**
572  * Free MX information record.
573  *
574  * @param mx record to free
575  */
576 static void
577 free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
578 {
579   if (NULL == mx)
580     return;
581   GNUNET_free_non_null (mx->mxhost);
582   GNUNET_free (mx);      
583 }
584
585
586 static void
587 free_record (struct GNUNET_DNSPARSER_Record *r)
588 {
589   GNUNET_free_non_null (r->name);
590   switch (r->type)
591   {
592   case GNUNET_DNSPARSER_TYPE_MX:
593     free_mx (r->data.mx);
594     break;
595   case GNUNET_DNSPARSER_TYPE_SOA:
596     free_soa (r->data.soa);
597     break;
598   case GNUNET_DNSPARSER_TYPE_SRV:
599     free_srv (r->data.srv);
600     break;
601   case GNUNET_DNSPARSER_TYPE_NS:
602   case GNUNET_DNSPARSER_TYPE_CNAME:
603   case GNUNET_DNSPARSER_TYPE_PTR:
604     GNUNET_free_non_null (r->data.hostname);
605     break;
606   default:
607     GNUNET_free_non_null (r->data.raw.data);
608     break;
609   }
610 }
611
612
613 /**
614  * Free memory taken by a packet.
615  *
616  * @param p packet to free
617  */
618 void
619 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
620 {
621   unsigned int i;
622
623   for (i=0;i<p->num_queries;i++)
624     GNUNET_free_non_null (p->queries[i].name);
625   GNUNET_free_non_null (p->queries);
626   for (i=0;i<p->num_answers;i++)
627     free_record (&p->answers[i]);
628   GNUNET_free_non_null (p->answers);
629   for (i=0;i<p->num_authority_records;i++)
630     free_record (&p->authority_records[i]);
631   GNUNET_free_non_null (p->authority_records);
632   for (i=0;i<p->num_additional_records;i++)
633     free_record (&p->additional_records[i]);
634   GNUNET_free_non_null (p->additional_records);
635   GNUNET_free (p);
636 }
637
638
639 /* ********************** DNS packet assembly code **************** */
640
641
642 /**
643  * Add a DNS name to the UDP packet at the given location.
644  *
645  * @param dst where to write the name
646  * @param dst_len number of bytes in dst
647  * @param off pointer to offset where to write the name (increment by bytes used)
648  *            must not be changed if there is an error
649  * @param name name to write
650  * @return GNUNET_SYSERR if 'name' is invalid
651  *         GNUNET_NO if 'name' did not fit
652  *         GNUNET_OK if 'name' was added to 'dst'
653  */
654 static int
655 add_name (char *dst,
656           size_t dst_len,
657           size_t *off,
658           const char *name)
659 {
660   const char *dot;
661   size_t start;
662   size_t pos;
663   size_t len;
664
665   if (NULL == name)
666     return GNUNET_SYSERR;
667   start = *off;
668   if (start + strlen (name) + 2 > dst_len)
669     return GNUNET_NO;
670   pos = start;
671   do
672   {
673     dot = strchr (name, '.');
674     if (NULL == dot)
675       len = strlen (name);
676     else
677       len = dot - name;
678     if ( (len >= 64) || (len == 0) )
679       return GNUNET_NO; /* segment too long or empty */
680     dst[pos++] = (char) (uint8_t) len;
681     memcpy (&dst[pos], name, len);
682     pos += len;
683     name += len + 1; /* also skip dot */
684   }
685   while (NULL != dot);
686   dst[pos++] = '\0'; /* terminator */
687   *off = pos;
688   return GNUNET_OK;
689 }
690
691
692 /**
693  * Add a DNS query to the UDP packet at the given location.
694  *
695  * @param dst where to write the query
696  * @param dst_len number of bytes in dst
697  * @param off pointer to offset where to write the query (increment by bytes used)
698  *            must not be changed if there is an error
699  * @param query query to write
700  * @return GNUNET_SYSERR if 'query' is invalid
701  *         GNUNET_NO if 'query' did not fit
702  *         GNUNET_OK if 'query' was added to 'dst'
703  */
704 static int
705 add_query (char *dst,
706            size_t dst_len,
707            size_t *off,
708            const struct GNUNET_DNSPARSER_Query *query)
709 {
710   int ret;
711   struct query_line ql;
712
713   ret = add_name (dst, dst_len - sizeof (struct query_line), off, query->name);
714   if (ret != GNUNET_OK)
715     return ret;
716   ql.type = htons (query->type);
717   ql.class = htons (query->class);
718   memcpy (&dst[*off], &ql, sizeof (ql));
719   (*off) += sizeof (ql);
720   return GNUNET_OK;
721 }
722
723
724 /**
725  * Add an MX record to the UDP packet at the given location.
726  *
727  * @param dst where to write the mx record
728  * @param dst_len number of bytes in dst
729  * @param off pointer to offset where to write the mx information (increment by bytes used);
730  *            can also change if there was an error
731  * @param mx mx information to write
732  * @return GNUNET_SYSERR if 'mx' is invalid
733  *         GNUNET_NO if 'mx' did not fit
734  *         GNUNET_OK if 'mx' was added to 'dst'
735  */
736 static int
737 add_mx (char *dst,
738         size_t dst_len,
739         size_t *off,
740         const struct GNUNET_DNSPARSER_MxRecord *mx)
741 {
742   uint16_t mxpref;
743
744   if (*off + sizeof (uint16_t) > dst_len)
745     return GNUNET_NO;
746   mxpref = htons (mx->preference);
747   memcpy (&dst[*off], &mxpref, sizeof (mxpref));
748   (*off) += sizeof (mxpref);
749   return add_name (dst, dst_len, off, mx->mxhost);
750 }
751
752
753 /**
754  * Add an SOA record to the UDP packet at the given location.
755  *
756  * @param dst where to write the SOA record
757  * @param dst_len number of bytes in dst
758  * @param off pointer to offset where to write the SOA information (increment by bytes used)
759  *            can also change if there was an error
760  * @param soa SOA information to write
761  * @return GNUNET_SYSERR if 'soa' is invalid
762  *         GNUNET_NO if 'soa' did not fit
763  *         GNUNET_OK if 'soa' was added to 'dst'
764  */
765 static int
766 add_soa (char *dst,
767          size_t dst_len,
768          size_t *off,
769          const struct GNUNET_DNSPARSER_SoaRecord *soa)
770 {
771   struct soa_data sd;
772   int ret;
773
774   if ( (GNUNET_OK != (ret = add_name (dst,
775                                       dst_len,
776                                       off,
777                                       soa->mname))) ||
778        (GNUNET_OK != (ret = add_name (dst,
779                                       dst_len,
780                                       off,
781                                       soa->rname)) ) )
782     return ret;
783   if (*off + sizeof (struct soa_data) > dst_len)
784     return GNUNET_NO;
785   sd.serial = htonl (soa->serial);
786   sd.refresh = htonl (soa->refresh);
787   sd.retry = htonl (soa->retry);
788   sd.expire = htonl (soa->expire);
789   sd.minimum = htonl (soa->minimum_ttl);
790   memcpy (&dst[*off], &sd, sizeof (sd));
791   (*off) += sizeof (sd);
792   return GNUNET_OK;
793 }
794
795
796 /**
797  * Add an SRV record to the UDP packet at the given location.
798  *
799  * @param dst where to write the SRV record
800  * @param dst_len number of bytes in dst
801  * @param off pointer to offset where to write the SRV information (increment by bytes used)
802  *            can also change if there was an error
803  * @param srv SRV information to write
804  * @return GNUNET_SYSERR if 'srv' is invalid
805  *         GNUNET_NO if 'srv' did not fit
806  *         GNUNET_OK if 'srv' was added to 'dst'
807  */
808 static int
809 add_srv (char *dst,
810          size_t dst_len,
811          size_t *off,
812          const struct GNUNET_DNSPARSER_SrvRecord *srv)
813 {
814   struct srv_data sd;
815   int ret;
816
817   if (*off + sizeof (struct srv_data) > dst_len)
818     return GNUNET_NO;
819   sd.prio = htons (srv->priority);
820   sd.weight = htons (srv->weight);
821   sd.port = htons (srv->port);
822   memcpy (&dst[*off], &sd, sizeof (sd));
823   (*off) += sizeof (sd);
824   if (GNUNET_OK != (ret = add_name (dst,
825                                     dst_len,
826                                     off,
827                                     srv->target)))
828     return ret;
829   return GNUNET_OK;
830 }
831
832
833 /**
834  * Add a DNS record to the UDP packet at the given location.
835  *
836  * @param dst where to write the query
837  * @param dst_len number of bytes in dst
838  * @param off pointer to offset where to write the query (increment by bytes used)
839  *            must not be changed if there is an error
840  * @param record record to write
841  * @return GNUNET_SYSERR if 'record' is invalid
842  *         GNUNET_NO if 'record' did not fit
843  *         GNUNET_OK if 'record' was added to 'dst'
844  */
845 static int
846 add_record (char *dst,
847             size_t dst_len,
848             size_t *off,
849             const struct GNUNET_DNSPARSER_Record *record)
850 {
851   int ret;
852   size_t start;
853   size_t pos;
854   struct record_line rl;
855   char *name;
856   
857   start = *off;
858   /* for SRV records, we can create the name from the details
859      of the record if needed */
860   name = record->name;
861   if  ( (GNUNET_DNSPARSER_TYPE_SRV == record->type) &&
862         (NULL == name) )
863     GNUNET_asprintf (&name,
864                      "_%s._%s.%s",
865                      record->data.srv->service,
866                      record->data.srv->proto,
867                      record->data.srv->domain_name);
868   ret = add_name (dst, dst_len - sizeof (struct record_line), off, name);
869   if (name != record->name)
870     GNUNET_free (name);
871   if (GNUNET_OK != ret)
872     return ret;
873   /* '*off' is now the position where we will need to write the record line */
874
875   pos = *off + sizeof (struct record_line);
876   switch (record->type)
877   { 
878   case GNUNET_DNSPARSER_TYPE_MX:
879     ret = add_mx (dst, dst_len, &pos, record->data.mx);    
880     break;
881   case GNUNET_DNSPARSER_TYPE_SOA:
882     ret = add_soa (dst, dst_len, &pos, record->data.soa);
883     break;
884   case GNUNET_DNSPARSER_TYPE_NS:
885   case GNUNET_DNSPARSER_TYPE_CNAME:
886   case GNUNET_DNSPARSER_TYPE_PTR:
887     ret = add_name (dst, dst_len, &pos, record->data.hostname);
888     break;
889   case GNUNET_DNSPARSER_TYPE_SRV:
890     ret = add_srv (dst, dst_len, &pos, record->data.srv);
891     break;
892   default:
893     if (pos + record->data.raw.data_len > dst_len)
894     {
895       ret = GNUNET_NO;
896       break;
897     }
898     memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
899     pos += record->data.raw.data_len;
900     ret = GNUNET_OK;
901     break;
902   }
903   if (GNUNET_OK != ret)
904   {
905     *off = start;
906     return GNUNET_NO;
907   }
908
909   if (pos - (*off + sizeof (struct record_line)) > UINT16_MAX)
910   {
911     /* record data too long */
912     *off = start;
913     return GNUNET_NO;
914   }
915   rl.type = htons (record->type);
916   rl.class = htons (record->class);
917   rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value / 1000); /* in seconds */
918   rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct record_line))));
919   memcpy (&dst[*off], &rl, sizeof (struct record_line));
920   *off = pos;
921   return GNUNET_OK;  
922 }
923
924
925 /**
926  * Given a DNS packet, generate the corresponding UDP payload.
927  * Note that we do not attempt to pack the strings with pointers
928  * as this would complicate the code and this is about being 
929  * simple and secure, not fast, fancy and broken like bind.
930  *
931  * @param p packet to pack
932  * @param max maximum allowed size for the resulting UDP payload
933  * @param buf set to a buffer with the packed message
934  * @param buf_length set to the length of buf
935  * @return GNUNET_SYSERR if 'p' is invalid
936  *         GNUNET_NO if 'p' was truncated (but there is still a result in 'buf')
937  *         GNUNET_OK if 'p' was packed completely into '*buf'
938  */
939 int
940 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
941                        uint16_t max,
942                        char **buf,
943                        size_t *buf_length)
944 {  
945   struct GNUNET_TUN_DnsHeader dns;
946   size_t off;
947   char tmp[max];
948   unsigned int i;
949   int ret;
950   int trc;
951   
952   if ( (p->num_queries > UINT16_MAX) ||
953        (p->num_answers > UINT16_MAX) ||
954        (p->num_authority_records > UINT16_MAX) ||
955        (p->num_additional_records > UINT16_MAX) )
956     return GNUNET_SYSERR;
957   dns.id = p->id;
958   dns.flags = p->flags;
959   dns.query_count = htons (p->num_queries);
960   dns.answer_rcount = htons (p->num_answers);
961   dns.authority_rcount = htons (p->num_authority_records);
962   dns.additional_rcount = htons (p->num_additional_records);
963
964   off = sizeof (struct GNUNET_TUN_DnsHeader);
965   trc = GNUNET_NO;
966   for (i=0;i<p->num_queries;i++)
967   {
968     ret = add_query (tmp, sizeof (tmp), &off, &p->queries[i]);  
969     if (GNUNET_SYSERR == ret)
970       return GNUNET_SYSERR;
971     if (GNUNET_NO == ret)
972     {
973       dns.query_count = htons ((uint16_t) (i-1));
974       trc = GNUNET_YES;      
975       break;
976     }
977   }
978   for (i=0;i<p->num_answers;i++)
979   {
980     ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);  
981     if (GNUNET_SYSERR == ret)
982       return GNUNET_SYSERR;
983     if (GNUNET_NO == ret)
984     {
985       dns.answer_rcount = htons ((uint16_t) (i-1));
986       trc = GNUNET_YES;      
987       break;
988     }
989   }
990   for (i=0;i<p->num_authority_records;i++)
991   {
992     ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);  
993     if (GNUNET_SYSERR == ret)
994       return GNUNET_SYSERR;
995     if (GNUNET_NO == ret)
996     {
997       dns.authority_rcount = htons ((uint16_t) (i-1));
998       trc = GNUNET_YES;      
999       break;
1000     }
1001   }
1002   for (i=0;i<p->num_additional_records;i++)
1003   {
1004     ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);  
1005     if (GNUNET_SYSERR == ret)
1006       return GNUNET_SYSERR;
1007     if (GNUNET_NO == ret)
1008     {
1009       dns.additional_rcount = htons (i-1);
1010       trc = GNUNET_YES;      
1011       break;
1012     }
1013   }
1014
1015   if (GNUNET_YES == trc)
1016     dns.flags.message_truncated = 1;    
1017   memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
1018
1019   *buf = GNUNET_malloc (off);
1020   *buf_length = off;
1021   memcpy (*buf, tmp, off);
1022   if (GNUNET_YES == trc)
1023     return GNUNET_NO;
1024   return GNUNET_OK;
1025 }
1026
1027 /* end of dnsparser.c */