2 This file is part of GNUnet
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file gnsrecord/plugin_gnsrecord_dns.c
23 * @brief gnsrecord plugin to provide the API for basic DNS records
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dnsparser_lib.h"
30 #include "gnunet_gnsrecord_plugin.h"
34 * Convert the 'value' of a record to a string.
36 * @param cls closure, unused
37 * @param type type of the record
38 * @param data value in binary encoding
39 * @param data_size number of bytes in @a data
40 * @return NULL on error, otherwise human-readable representation of the value
43 dns_value_to_string (void *cls,
50 char tmp[INET6_ADDRSTRLEN];
54 case GNUNET_DNSPARSER_TYPE_A:
55 if (data_size != sizeof (struct in_addr))
57 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
59 return GNUNET_strdup (tmp);
60 case GNUNET_DNSPARSER_TYPE_NS:
66 ns = GNUNET_DNSPARSER_parse_name (data,
73 GNUNET_free_non_null (ns);
78 case GNUNET_DNSPARSER_TYPE_CNAME:
84 cname = GNUNET_DNSPARSER_parse_name (data,
87 if ( (NULL == cname) ||
91 GNUNET_free_non_null (cname);
96 case GNUNET_DNSPARSER_TYPE_SOA:
98 struct GNUNET_DNSPARSER_SoaRecord *soa;
102 soa = GNUNET_DNSPARSER_parse_soa (data,
105 if ( (NULL == soa) ||
110 GNUNET_DNSPARSER_free_soa (soa);
113 GNUNET_asprintf (&result,
114 "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
122 GNUNET_DNSPARSER_free_soa (soa);
125 case GNUNET_DNSPARSER_TYPE_PTR:
131 ptr = GNUNET_DNSPARSER_parse_name (data,
134 if ( (NULL == ptr) ||
138 GNUNET_free_non_null (ptr);
143 case GNUNET_DNSPARSER_TYPE_CERT:
145 struct GNUNET_DNSPARSER_CertRecord *cert;
151 cert = GNUNET_DNSPARSER_parse_cert (data,
154 if ( (NULL == cert) ||
158 GNUNET_DNSPARSER_free_cert (cert);
161 len = GNUNET_STRINGS_base64_encode (cert->certificate_data,
162 cert->certificate_size,
164 GNUNET_asprintf (&result,
171 GNUNET_free (base64);
172 GNUNET_DNSPARSER_free_cert (cert);
175 case GNUNET_DNSPARSER_TYPE_MX:
177 struct GNUNET_DNSPARSER_MxRecord *mx;
181 mx = GNUNET_DNSPARSER_parse_mx (data,
188 GNUNET_DNSPARSER_free_mx (mx);
191 GNUNET_asprintf (&result,
195 GNUNET_DNSPARSER_free_mx (mx);
198 case GNUNET_DNSPARSER_TYPE_TXT:
199 return GNUNET_strndup (data, data_size);
200 case GNUNET_DNSPARSER_TYPE_AAAA:
201 if (data_size != sizeof (struct in6_addr))
203 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
205 return GNUNET_strdup (tmp);
206 case GNUNET_DNSPARSER_TYPE_SRV:
208 struct GNUNET_DNSPARSER_SrvRecord *srv;
212 srv = GNUNET_DNSPARSER_parse_srv ("+", /* FIXME: is this OK? */
216 if ( (NULL == srv) ||
221 GNUNET_DNSPARSER_free_srv (srv);
224 GNUNET_asprintf (&result,
225 "%d %d %d _%s._%s.%s",
232 GNUNET_DNSPARSER_free_srv (srv);
235 case GNUNET_DNSPARSER_TYPE_TLSA:
237 const struct GNUNET_TUN_DnsTlsaRecord *tlsa;
241 if ( (data_size <= sizeof (struct GNUNET_TUN_DnsTlsaRecord)) ||
242 ('\0' != cdata[data_size - 1]) )
243 return NULL; /* malformed */
245 if (0 == GNUNET_asprintf (&tlsa_str,
250 (const char *) &tlsa[1]))
252 GNUNET_free (tlsa_str);
264 * Convert human-readable version of a 'value' of a record to the binary
267 * @param cls closure, unused
268 * @param type type of the record
269 * @param s human-readable string
270 * @param data set to value in binary encoding (will be allocated)
271 * @param data_size set to number of bytes in @a data
272 * @return #GNUNET_OK on success
275 dns_string_to_value (void *cls,
281 struct in_addr value_a;
282 struct in6_addr value_aaaa;
283 struct GNUNET_TUN_DnsTlsaRecord *tlsa;
286 return GNUNET_SYSERR;
289 case GNUNET_DNSPARSER_TYPE_A:
290 if (1 != inet_pton (AF_INET, s, &value_a))
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
293 _("Unable to parse IPv4 address `%s'\n"),
295 return GNUNET_SYSERR;
297 *data = GNUNET_new (struct in_addr);
298 memcpy (*data, &value_a, sizeof (value_a));
299 *data_size = sizeof (value_a);
301 case GNUNET_DNSPARSER_TYPE_NS:
308 GNUNET_DNSPARSER_builder_add_name (nsbuf,
313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
314 _("Failed to serialize NS record with value `%s'\n"),
316 return GNUNET_SYSERR;
319 *data = GNUNET_malloc (off);
320 memcpy (*data, nsbuf, off);
323 case GNUNET_DNSPARSER_TYPE_CNAME:
330 GNUNET_DNSPARSER_builder_add_name (cnamebuf,
335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
336 _("Failed to serialize CNAME record with value `%s'\n"),
338 return GNUNET_SYSERR;
341 *data = GNUNET_malloc (off);
342 memcpy (*data, cnamebuf, off);
345 case GNUNET_DNSPARSER_TYPE_CERT:
357 struct GNUNET_DNSPARSER_CertRecord cert;
359 sdup = GNUNET_strdup (s);
360 typep = strtok (sdup, " ");
361 /* TODO: add typep mnemonic conversion according to RFC 4398 */
362 if ( (NULL == typep) ||
366 (type > UINT16_MAX) )
369 return GNUNET_SYSERR;
371 keyp = strtok (NULL, " ");
372 if ( (NULL == keyp) ||
379 return GNUNET_SYSERR;
381 algp = strtok (NULL, " ");
382 /* TODO: add algp mnemonic conversion according to RFC 4398/RFC 4034 */
383 if ( (NULL == algp) ||
390 return GNUNET_SYSERR;
392 certp = strtok (NULL, " ");
393 if ( (NULL == certp) ||
394 (0 == strlen (certp) ) )
397 return GNUNET_SYSERR;
399 cert_size = GNUNET_STRINGS_base64_decode (certp,
403 cert.cert_type = type;
405 cert.algorithm = alg;
406 cert.certificate_size = cert_size;
407 cert.certificate_data = cert_data;
409 char certbuf[cert_size + sizeof (struct GNUNET_TUN_DnsCertRecord)];
414 GNUNET_DNSPARSER_builder_add_cert (certbuf,
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420 _("Failed to serialize CERT record with %u bytes\n"),
421 (unsigned int) cert_size);
422 GNUNET_free (cert_data);
423 return GNUNET_SYSERR;
425 GNUNET_free (cert_data);
427 *data = GNUNET_malloc (off);
428 memcpy (*data, certbuf, off);
430 GNUNET_free (cert_data);
433 case GNUNET_DNSPARSER_TYPE_SOA:
435 struct GNUNET_DNSPARSER_SoaRecord soa;
437 char soa_rname[253 + 1];
438 char soa_mname[253 + 1];
439 unsigned int soa_serial;
440 unsigned int soa_refresh;
441 unsigned int soa_retry;
442 unsigned int soa_expire;
443 unsigned int soa_min;
447 "rname=%253s mname=%253s %u,%u,%u,%u,%u",
448 soa_rname, soa_mname,
449 &soa_serial, &soa_refresh, &soa_retry, &soa_expire, &soa_min))
451 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
452 _("Unable to parse SOA record `%s'\n"),
454 return GNUNET_SYSERR;
456 soa.mname = soa_mname;
457 soa.rname = soa_rname;
458 soa.serial = (uint32_t) soa_serial;
459 soa.refresh =(uint32_t) soa_refresh;
460 soa.retry = (uint32_t) soa_retry;
461 soa.expire = (uint32_t) soa_expire;
462 soa.minimum_ttl = (uint32_t) soa_min;
465 GNUNET_DNSPARSER_builder_add_soa (soabuf,
470 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
471 _("Failed to serialize SOA record with mname `%s' and rname `%s'\n"),
474 return GNUNET_SYSERR;
477 *data = GNUNET_malloc (off);
478 memcpy (*data, soabuf, off);
481 case GNUNET_DNSPARSER_TYPE_PTR:
488 GNUNET_DNSPARSER_builder_add_name (ptrbuf,
493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
494 _("Failed to serialize PTR record with value `%s'\n"),
496 return GNUNET_SYSERR;
499 *data = GNUNET_malloc (off);
500 memcpy (*data, ptrbuf, off);
503 case GNUNET_DNSPARSER_TYPE_MX:
505 struct GNUNET_DNSPARSER_MxRecord mx;
507 char mxhost[253 + 1];
511 if (2 != SSCANF(s, "%hu,%253s", &mx_pref, mxhost))
513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
514 _("Unable to parse MX record `%s'\n"),
516 return GNUNET_SYSERR;
518 mx.preference = mx_pref;
523 GNUNET_DNSPARSER_builder_add_mx (mxbuf,
528 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
529 _("Failed to serialize MX record with hostname `%s'\n"),
531 return GNUNET_SYSERR;
534 *data = GNUNET_malloc (off);
535 memcpy (*data, mxbuf, off);
538 case GNUNET_DNSPARSER_TYPE_SRV:
539 GNUNET_break (0); // FIXME: not implemented!
540 return GNUNET_SYSERR;
541 case GNUNET_DNSPARSER_TYPE_TXT:
542 *data = GNUNET_strdup (s);
543 *data_size = strlen (s);
545 case GNUNET_DNSPARSER_TYPE_AAAA:
546 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
548 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
549 _("Unable to parse IPv6 address `%s'\n"),
551 return GNUNET_SYSERR;
553 *data = GNUNET_new (struct in6_addr);
554 *data_size = sizeof (struct in6_addr);
555 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
557 case GNUNET_DNSPARSER_TYPE_TLSA:
558 *data_size = sizeof (struct GNUNET_TUN_DnsTlsaRecord) + strlen (s) - 6;
559 *data = tlsa = GNUNET_malloc (*data_size);
560 if (4 != SSCANF (s, "%c %c %c %s",
563 &tlsa->matching_type,
566 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
567 _("Unable to parse TLSA record string `%s'\n"),
571 return GNUNET_SYSERR;
575 return GNUNET_SYSERR;
581 * Mapping of record type numbers to human-readable
588 { "A", GNUNET_DNSPARSER_TYPE_A },
589 { "NS", GNUNET_DNSPARSER_TYPE_NS },
590 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
591 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
592 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
593 { "MX", GNUNET_DNSPARSER_TYPE_MX },
594 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
595 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
596 { "TLSA", GNUNET_DNSPARSER_TYPE_TLSA },
602 * Convert a type name (i.e. "AAAA") to the corresponding number.
604 * @param cls closure, unused
605 * @param dns_typename name to convert
606 * @return corresponding number, UINT32_MAX on error
609 dns_typename_to_number (void *cls,
610 const char *dns_typename)
615 while ( (name_map[i].name != NULL) &&
616 (0 != strcasecmp (dns_typename, name_map[i].name)) )
618 return name_map[i].number;
623 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
625 * @param cls closure, unused
626 * @param type number of a type to convert
627 * @return corresponding typestring, NULL on error
630 dns_number_to_typename (void *cls,
636 while ( (name_map[i].name != NULL) &&
637 (type != name_map[i].number) )
639 return name_map[i].name;
644 * Entry point for the plugin.
647 * @return the exported block API
650 libgnunet_plugin_gnsrecord_dns_init (void *cls)
652 struct GNUNET_GNSRECORD_PluginFunctions *api;
654 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
655 api->value_to_string = &dns_value_to_string;
656 api->string_to_value = &dns_string_to_value;
657 api->typename_to_number = &dns_typename_to_number;
658 api->number_to_typename = &dns_number_to_typename;
664 * Exit point from the plugin.
666 * @param cls the return value from #libgnunet_plugin_block_test_init
670 libgnunet_plugin_gnsrecord_dns_done (void *cls)
672 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
678 /* end of plugin_gnsrecord_dns.c */