-avoid 'hu', as it is unsigned short, not uint16_t
[oweals/gnunet.git] / src / gnsrecord / plugin_gnsrecord_dns.c
1 /*
2      This file is part of GNUnet
3      (C) 2013, 2014 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 3, 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 gnsrecord/plugin_gnsrecord_dns.c
23  * @brief gnsrecord plugin to provide the API for basic DNS records
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_dnsparser_lib.h"
29 #include "gnunet_gnsrecord_plugin.h"
30
31
32 /**
33  * Convert the 'value' of a record to a string.
34  *
35  * @param cls closure, unused
36  * @param type type of the record
37  * @param data value in binary encoding
38  * @param data_size number of bytes in @a data
39  * @return NULL on error, otherwise human-readable representation of the value
40  */
41 static char *
42 dns_value_to_string (void *cls,
43                      uint32_t type,
44                      const void *data,
45                      size_t data_size)
46 {
47   const char *cdata;
48   char* result;
49   char tmp[INET6_ADDRSTRLEN];
50
51   switch (type)
52   {
53   case GNUNET_DNSPARSER_TYPE_A:
54     if (data_size != sizeof (struct in_addr))
55       return NULL;
56     if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
57       return NULL;
58     return GNUNET_strdup (tmp);
59   case GNUNET_DNSPARSER_TYPE_NS:
60     {
61       char *ns;
62       size_t off;
63
64       off = 0;
65       ns = GNUNET_DNSPARSER_parse_name (data,
66                                         data_size,
67                                         &off);
68       if ( (NULL == ns) ||
69            (off != data_size) )
70       {
71         GNUNET_break_op (0);
72         GNUNET_free_non_null (ns);
73         return NULL;
74       }
75       return ns;
76     }
77   case GNUNET_DNSPARSER_TYPE_CNAME:
78     {
79       char *cname;
80       size_t off;
81
82       off = 0;
83       cname = GNUNET_DNSPARSER_parse_name (data,
84                                            data_size,
85                                            &off);
86       if ( (NULL == cname) ||
87            (off != data_size) )
88       {
89         GNUNET_break_op (0);
90         GNUNET_free_non_null (cname);
91         return NULL;
92       }
93       return cname;
94     }
95   case GNUNET_DNSPARSER_TYPE_SOA:
96     {
97       struct GNUNET_DNSPARSER_SoaRecord *soa;
98       size_t off;
99
100       off = 0;
101       soa = GNUNET_DNSPARSER_parse_soa (data,
102                                         data_size,
103                                         &off);
104       if ( (NULL == soa) ||
105            (off != data_size) )
106       {
107         GNUNET_break_op (0);
108         if (NULL != soa)
109           GNUNET_DNSPARSER_free_soa (soa);
110         return NULL;
111       }
112       GNUNET_asprintf (&result,
113                        "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
114                        soa->rname,
115                        soa->mname,
116                        soa->serial,
117                        soa->refresh,
118                        soa->retry,
119                        soa->expire,
120                        soa->minimum_ttl);
121       GNUNET_DNSPARSER_free_soa (soa);
122       return result;
123     }
124   case GNUNET_DNSPARSER_TYPE_PTR:
125     {
126       char *ptr;
127       size_t off;
128
129       off = 0;
130       ptr = GNUNET_DNSPARSER_parse_name (data,
131                                            data_size,
132                                            &off);
133       if ( (NULL == ptr) ||
134            (off != data_size) )
135       {
136         GNUNET_break_op (0);
137         GNUNET_free_non_null (ptr);
138         return NULL;
139       }
140       return ptr;
141     }
142   case GNUNET_DNSPARSER_TYPE_CERT:
143     {
144       struct GNUNET_DNSPARSER_CertRecord *cert;
145       size_t off;
146       char *base64;
147       int len;
148
149       off = 0;
150       cert = GNUNET_DNSPARSER_parse_cert (data,
151                                           data_size,
152                                           &off);
153       if ( (NULL == cert) ||
154            (off != data_size) )
155       {
156         GNUNET_break_op (0);
157         GNUNET_DNSPARSER_free_cert (cert);
158         return NULL;
159       }
160       len = GNUNET_STRINGS_base64_encode (cert->certificate_data,
161                                           cert->certificate_size,
162                                           &base64);
163       GNUNET_asprintf (&result,
164                        "%u %u %u %.*s",
165                        cert->cert_type,
166                        cert->cert_tag,
167                        cert->algorithm,
168                        len,
169                        base64);
170       GNUNET_free (base64);
171       GNUNET_DNSPARSER_free_cert (cert);
172       return result;
173     }
174   case GNUNET_DNSPARSER_TYPE_MX:
175     {
176       struct GNUNET_DNSPARSER_MxRecord *mx;
177       size_t off;
178
179       off = 0;
180       mx = GNUNET_DNSPARSER_parse_mx (data,
181                                       data_size,
182                                       &off);
183       if ( (NULL == mx) ||
184            (off != data_size) )
185       {
186         GNUNET_break_op (0);
187         GNUNET_DNSPARSER_free_mx (mx);
188         return NULL;
189       }
190       GNUNET_asprintf (&result,
191                        "%u,%s",
192                        (unsigned int) mx->preference,
193                        mx->mxhost);
194       GNUNET_DNSPARSER_free_mx (mx);
195       return result;
196     }
197   case GNUNET_DNSPARSER_TYPE_TXT:
198     return GNUNET_strndup (data, data_size);
199   case GNUNET_DNSPARSER_TYPE_AAAA:
200     if (data_size != sizeof (struct in6_addr))
201       return NULL;
202     if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
203       return NULL;
204     return GNUNET_strdup (tmp);
205   case GNUNET_DNSPARSER_TYPE_SRV:
206     {
207       struct GNUNET_DNSPARSER_SrvRecord *srv;
208       size_t off;
209
210       off = 0;
211       srv = GNUNET_DNSPARSER_parse_srv (data,
212                                         data_size,
213                                         &off);
214       if ( (NULL == srv) ||
215            (off != data_size) )
216       {
217         GNUNET_break_op (0);
218         if (NULL != srv)
219           GNUNET_DNSPARSER_free_srv (srv);
220         return NULL;
221       }
222       GNUNET_asprintf (&result,
223                        "%d %d %d %s",
224                        srv->priority,
225                        srv->weight,
226                        srv->port,
227                        srv->target);
228       GNUNET_DNSPARSER_free_srv (srv);
229       return result;
230     }
231   case GNUNET_DNSPARSER_TYPE_TLSA:
232     {
233       const struct GNUNET_TUN_DnsTlsaRecord *tlsa;
234       char* tlsa_str;
235
236       cdata = data;
237       if ( (data_size <= sizeof (struct GNUNET_TUN_DnsTlsaRecord)) ||
238            ('\0' != cdata[data_size - 1]) )
239         return NULL; /* malformed */
240       tlsa = data;
241       if (0 == GNUNET_asprintf (&tlsa_str,
242                                 "%c %c %c %s",
243                                 tlsa->usage,
244                                 tlsa->selector,
245                                 tlsa->matching_type,
246                                 (const char *) &tlsa[1]))
247       {
248         GNUNET_free (tlsa_str);
249         return NULL;
250       }
251       return tlsa_str;
252     }
253   default:
254     return NULL;
255   }
256 }
257
258
259 /**
260  * Convert human-readable version of a 'value' of a record to the binary
261  * representation.
262  *
263  * @param cls closure, unused
264  * @param type type of the record
265  * @param s human-readable string
266  * @param data set to value in binary encoding (will be allocated)
267  * @param data_size set to number of bytes in @a data
268  * @return #GNUNET_OK on success
269  */
270 static int
271 dns_string_to_value (void *cls,
272                      uint32_t type,
273                      const char *s,
274                      void **data,
275                      size_t *data_size)
276 {
277   struct in_addr value_a;
278   struct in6_addr value_aaaa;
279   struct GNUNET_TUN_DnsTlsaRecord *tlsa;
280
281   if (NULL == s)
282     return GNUNET_SYSERR;
283   switch (type)
284   {
285   case GNUNET_DNSPARSER_TYPE_A:
286     if (1 != inet_pton (AF_INET, s, &value_a))
287     {
288       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
289                   _("Unable to parse IPv4 address `%s'\n"),
290                   s);
291       return GNUNET_SYSERR;
292     }
293     *data = GNUNET_new (struct in_addr);
294     memcpy (*data, &value_a, sizeof (value_a));
295     *data_size = sizeof (value_a);
296     return GNUNET_OK;
297   case GNUNET_DNSPARSER_TYPE_NS:
298     {
299       char nsbuf[256];
300       size_t off;
301
302       off = 0;
303       if (GNUNET_OK !=
304           GNUNET_DNSPARSER_builder_add_name (nsbuf,
305                                              sizeof (nsbuf),
306                                              &off,
307                                              s))
308       {
309         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310              _("Failed to serialize NS record with value `%s'\n"),
311              s);
312         return GNUNET_SYSERR;
313       }
314       *data_size = off;
315       *data = GNUNET_malloc (off);
316       memcpy (*data, nsbuf, off);
317       return GNUNET_OK;
318     }
319   case GNUNET_DNSPARSER_TYPE_CNAME:
320     {
321       char cnamebuf[256];
322       size_t off;
323
324       off = 0;
325       if (GNUNET_OK !=
326           GNUNET_DNSPARSER_builder_add_name (cnamebuf,
327                                              sizeof (cnamebuf),
328                                              &off,
329                                              s))
330       {
331         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
332              _("Failed to serialize CNAME record with value `%s'\n"),
333              s);
334         return GNUNET_SYSERR;
335       }
336       *data_size = off;
337       *data = GNUNET_malloc (off);
338       memcpy (*data, cnamebuf, off);
339       return GNUNET_OK;
340     }
341   case GNUNET_DNSPARSER_TYPE_CERT:
342     {
343       char *sdup;
344       const char *typep;
345       const char *keyp;
346       const char *algp;
347       const char *certp;
348       unsigned int type;
349       unsigned int key;
350       unsigned int alg;
351       size_t cert_size;
352       char *cert_data;
353       struct GNUNET_DNSPARSER_CertRecord cert;
354
355       sdup = GNUNET_strdup (s);
356       typep = strtok (sdup, " ");
357       /* TODO: add typep mnemonic conversion according to RFC 4398 */
358       if ( (NULL == typep) ||
359            (1 != sscanf (typep,
360                          "%u",
361                          &type)) ||
362            (type > UINT16_MAX) )
363       {
364         GNUNET_free (sdup);
365         return GNUNET_SYSERR;
366       }
367       keyp = strtok (NULL, " ");
368       if ( (NULL == keyp) ||
369            (1 != sscanf (keyp,
370                          "%u",
371                          &key)) ||
372            (key > UINT16_MAX) )
373       {
374         GNUNET_free (sdup);
375         return GNUNET_SYSERR;
376       }
377       algp = strtok (NULL, " ");
378       /* TODO: add algp mnemonic conversion according to RFC 4398/RFC 4034 */
379       if ( (NULL == algp) ||
380            (1 != sscanf (algp,
381                          "%u",
382                          &alg)) ||
383            (alg > UINT8_MAX) )
384       {
385         GNUNET_free (sdup);
386         return GNUNET_SYSERR;
387       }
388       certp = strtok (NULL, " ");
389       if ( (NULL == certp) ||
390            (0 == strlen (certp) ) )
391       {
392         GNUNET_free (sdup);
393         return GNUNET_SYSERR;
394       }
395       cert_size = GNUNET_STRINGS_base64_decode (certp,
396                                                 strlen (certp),
397                                                 &cert_data);
398       GNUNET_free (sdup);
399       cert.cert_type = type;
400       cert.cert_tag = key;
401       cert.algorithm = alg;
402       cert.certificate_size = cert_size;
403       cert.certificate_data = cert_data;
404       {
405         char certbuf[cert_size + sizeof (struct GNUNET_TUN_DnsCertRecord)];
406         size_t off;
407
408         off = 0;
409         if (GNUNET_OK !=
410             GNUNET_DNSPARSER_builder_add_cert (certbuf,
411                                                sizeof (certbuf),
412                                                &off,
413                                                &cert))
414         {
415           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
416                       _("Failed to serialize CERT record with %u bytes\n"),
417                       (unsigned int) cert_size);
418           GNUNET_free (cert_data);
419           return GNUNET_SYSERR;
420         }
421         GNUNET_free (cert_data);
422         *data_size = off;
423         *data = GNUNET_malloc (off);
424         memcpy (*data, certbuf, off);
425       }
426       GNUNET_free (cert_data);
427       return GNUNET_OK;
428     }
429   case GNUNET_DNSPARSER_TYPE_SOA:
430     {
431       struct GNUNET_DNSPARSER_SoaRecord soa;
432       char soabuf[540];
433       char soa_rname[253 + 1];
434       char soa_mname[253 + 1];
435       unsigned int soa_serial;
436       unsigned int soa_refresh;
437       unsigned int soa_retry;
438       unsigned int soa_expire;
439       unsigned int soa_min;
440       size_t off;
441
442       if (7 != SSCANF (s,
443                        "rname=%253s mname=%253s %u,%u,%u,%u,%u",
444                        soa_rname, soa_mname,
445                        &soa_serial, &soa_refresh, &soa_retry, &soa_expire, &soa_min))
446       {
447         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
448              _("Unable to parse SOA record `%s'\n"),
449              s);
450         return GNUNET_SYSERR;
451       }
452       soa.mname = soa_mname;
453       soa.rname = soa_rname;
454       soa.serial = (uint32_t) soa_serial;
455       soa.refresh =(uint32_t)  soa_refresh;
456       soa.retry = (uint32_t) soa_retry;
457       soa.expire = (uint32_t) soa_expire;
458       soa.minimum_ttl = (uint32_t) soa_min;
459       off = 0;
460       if (GNUNET_OK !=
461           GNUNET_DNSPARSER_builder_add_soa (soabuf,
462                                             sizeof (soabuf),
463                                             &off,
464                                             &soa))
465       {
466         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
467              _("Failed to serialize SOA record with mname `%s' and rname `%s'\n"),
468              soa_mname,
469              soa_rname);
470         return GNUNET_SYSERR;
471       }
472       *data_size = off;
473       *data = GNUNET_malloc (off);
474       memcpy (*data, soabuf, off);
475       return GNUNET_OK;
476     }
477   case GNUNET_DNSPARSER_TYPE_PTR:
478     {
479       char ptrbuf[256];
480       size_t off;
481
482       off = 0;
483       if (GNUNET_OK !=
484           GNUNET_DNSPARSER_builder_add_name (ptrbuf,
485                                              sizeof (ptrbuf),
486                                              &off,
487                                              s))
488       {
489         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
490              _("Failed to serialize PTR record with value `%s'\n"),
491              s);
492         return GNUNET_SYSERR;
493       }
494       *data_size = off;
495       *data = GNUNET_malloc (off);
496       memcpy (*data, ptrbuf, off);
497       return GNUNET_OK;
498     }
499   case GNUNET_DNSPARSER_TYPE_MX:
500     {
501       struct GNUNET_DNSPARSER_MxRecord mx;
502       char mxbuf[258];
503       char mxhost[253 + 1];
504       unsigned int mx_pref;
505       size_t off;
506
507       if (2 != SSCANF(s,
508                       "%u,%253s",
509                       &mx_pref,
510                       mxhost))
511       {
512         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
513              _("Unable to parse MX record `%s'\n"),
514              s);
515       return GNUNET_SYSERR;
516       }
517       mx.preference = (uint16_t) mx_pref;
518       mx.mxhost = mxhost;
519       off = 0;
520
521       if (GNUNET_OK !=
522           GNUNET_DNSPARSER_builder_add_mx (mxbuf,
523                                            sizeof (mxbuf),
524                                            &off,
525                                            &mx))
526       {
527         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
528              _("Failed to serialize MX record with hostname `%s'\n"),
529              mxhost);
530         return GNUNET_SYSERR;
531       }
532       *data_size = off;
533       *data = GNUNET_malloc (off);
534       memcpy (*data, mxbuf, off);
535       return GNUNET_OK;
536     }
537   case GNUNET_DNSPARSER_TYPE_SRV:
538     {
539       struct GNUNET_DNSPARSER_SrvRecord srv;
540       char srvbuf[270];
541       char srvtarget[253 + 1];
542       unsigned int priority;
543       unsigned int weight;
544       unsigned int port;
545       size_t off;
546
547       if (2 != SSCANF(s,
548                       "%u %u %u %253s",
549                       &priority,
550                       &weight,
551                       &port,
552                       srvtarget))
553       {
554         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
555              _("Unable to parse SRV record `%s'\n"),
556              s);
557         return GNUNET_SYSERR;
558       }
559       srv.priority = (uint16_t) priority;
560       srv.weight = (uint16_t) weight;
561       srv.port = (uint16_t) port;
562       srv.target = srvtarget;
563       off = 0;
564       if (GNUNET_OK !=
565           GNUNET_DNSPARSER_builder_add_srv (srvbuf,
566                                             sizeof (srvbuf),
567                                             &off,
568                                             &srv))
569       {
570         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
571                     _("Failed to serialize SRV record with target `%s'\n"),
572                     srvtarget);
573         return GNUNET_SYSERR;
574       }
575       *data_size = off;
576       *data = GNUNET_malloc (off);
577       memcpy (*data, srvbuf, off);
578       return GNUNET_OK;
579     }
580   case GNUNET_DNSPARSER_TYPE_TXT:
581     *data = GNUNET_strdup (s);
582     *data_size = strlen (s);
583     return GNUNET_OK;
584   case GNUNET_DNSPARSER_TYPE_AAAA:
585     if (1 != inet_pton (AF_INET6, s, &value_aaaa))
586     {
587       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
588            _("Unable to parse IPv6 address `%s'\n"),
589            s);
590       return GNUNET_SYSERR;
591     }
592     *data = GNUNET_new (struct in6_addr);
593     *data_size = sizeof (struct in6_addr);
594     memcpy (*data, &value_aaaa, sizeof (value_aaaa));
595     return GNUNET_OK;
596   case GNUNET_DNSPARSER_TYPE_TLSA:
597     *data_size = sizeof (struct GNUNET_TUN_DnsTlsaRecord) + strlen (s) - 6;
598     *data = tlsa = GNUNET_malloc (*data_size);
599     if (4 != SSCANF (s, "%c %c %c %s",
600                      &tlsa->usage,
601                      &tlsa->selector,
602                      &tlsa->matching_type,
603                      (char*)&tlsa[1]))
604     {
605       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
606                   _("Unable to parse TLSA record string `%s'\n"),
607                   s);
608       *data_size = 0;
609       GNUNET_free (tlsa);
610       return GNUNET_SYSERR;
611     }
612     return GNUNET_OK;
613   default:
614     return GNUNET_SYSERR;
615   }
616 }
617
618
619 /**
620  * Mapping of record type numbers to human-readable
621  * record type names.
622  */
623 static struct {
624   const char *name;
625   uint32_t number;
626 } name_map[] = {
627   { "A", GNUNET_DNSPARSER_TYPE_A },
628   { "NS", GNUNET_DNSPARSER_TYPE_NS },
629   { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
630   { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
631   { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
632   { "MX", GNUNET_DNSPARSER_TYPE_MX },
633   { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
634   { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
635   { "TLSA", GNUNET_DNSPARSER_TYPE_TLSA },
636   { NULL, UINT32_MAX }
637 };
638
639
640 /**
641  * Convert a type name (i.e. "AAAA") to the corresponding number.
642  *
643  * @param cls closure, unused
644  * @param dns_typename name to convert
645  * @return corresponding number, UINT32_MAX on error
646  */
647 static uint32_t
648 dns_typename_to_number (void *cls,
649                         const char *dns_typename)
650 {
651   unsigned int i;
652
653   i=0;
654   while ( (name_map[i].name != NULL) &&
655           (0 != strcasecmp (dns_typename, name_map[i].name)) )
656     i++;
657   return name_map[i].number;
658 }
659
660
661 /**
662  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
663  *
664  * @param cls closure, unused
665  * @param type number of a type to convert
666  * @return corresponding typestring, NULL on error
667  */
668 static const char *
669 dns_number_to_typename (void *cls,
670                         uint32_t type)
671 {
672   unsigned int i;
673
674   i=0;
675   while ( (name_map[i].name != NULL) &&
676           (type != name_map[i].number) )
677     i++;
678   return name_map[i].name;
679 }
680
681
682 /**
683  * Entry point for the plugin.
684  *
685  * @param cls NULL
686  * @return the exported block API
687  */
688 void *
689 libgnunet_plugin_gnsrecord_dns_init (void *cls)
690 {
691   struct GNUNET_GNSRECORD_PluginFunctions *api;
692
693   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
694   api->value_to_string = &dns_value_to_string;
695   api->string_to_value = &dns_string_to_value;
696   api->typename_to_number = &dns_typename_to_number;
697   api->number_to_typename = &dns_number_to_typename;
698   return api;
699 }
700
701
702 /**
703  * Exit point from the plugin.
704  *
705  * @param cls the return value from #libgnunet_plugin_block_test_init
706  * @return NULL
707  */
708 void *
709 libgnunet_plugin_gnsrecord_dns_done (void *cls)
710 {
711   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
712
713   GNUNET_free (api);
714   return NULL;
715 }
716
717 /* end of plugin_gnsrecord_dns.c */