2 This file is part of GNUnet
3 (C) 2013, 2014 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
27 #include "gnunet_util_lib.h"
28 #include "gnunet_dnsparser_lib.h"
29 #include "gnunet_gnsrecord_plugin.h"
33 * Convert the 'value' of a record to a string.
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
42 dns_value_to_string (void *cls,
48 char tmp[INET6_ADDRSTRLEN];
52 case GNUNET_DNSPARSER_TYPE_A:
53 if (data_size != sizeof (struct in_addr))
55 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
57 return GNUNET_strdup (tmp);
58 case GNUNET_DNSPARSER_TYPE_NS:
64 ns = GNUNET_DNSPARSER_parse_name (data,
71 GNUNET_free_non_null (ns);
76 case GNUNET_DNSPARSER_TYPE_CNAME:
82 cname = GNUNET_DNSPARSER_parse_name (data,
85 if ( (NULL == cname) ||
89 GNUNET_free_non_null (cname);
94 case GNUNET_DNSPARSER_TYPE_SOA:
96 struct GNUNET_DNSPARSER_SoaRecord *soa;
100 soa = GNUNET_DNSPARSER_parse_soa (data,
103 if ( (NULL == soa) ||
108 GNUNET_DNSPARSER_free_soa (soa);
111 GNUNET_asprintf (&result,
112 "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
120 GNUNET_DNSPARSER_free_soa (soa);
123 case GNUNET_DNSPARSER_TYPE_PTR:
129 ptr = GNUNET_DNSPARSER_parse_name (data,
132 if ( (NULL == ptr) ||
136 GNUNET_free_non_null (ptr);
141 case GNUNET_DNSPARSER_TYPE_CERT:
143 struct GNUNET_DNSPARSER_CertRecord *cert;
149 cert = GNUNET_DNSPARSER_parse_cert (data,
152 if ( (NULL == cert) ||
156 GNUNET_DNSPARSER_free_cert (cert);
159 len = GNUNET_STRINGS_base64_encode (cert->certificate_data,
160 cert->certificate_size,
162 GNUNET_asprintf (&result,
169 GNUNET_free (base64);
170 GNUNET_DNSPARSER_free_cert (cert);
173 case GNUNET_DNSPARSER_TYPE_MX:
175 struct GNUNET_DNSPARSER_MxRecord *mx;
179 mx = GNUNET_DNSPARSER_parse_mx (data,
186 GNUNET_DNSPARSER_free_mx (mx);
189 GNUNET_asprintf (&result,
191 (unsigned int) mx->preference,
193 GNUNET_DNSPARSER_free_mx (mx);
196 case GNUNET_DNSPARSER_TYPE_TXT:
197 return GNUNET_strndup (data, data_size);
198 case GNUNET_DNSPARSER_TYPE_AAAA:
199 if (data_size != sizeof (struct in6_addr))
201 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
203 return GNUNET_strdup (tmp);
204 case GNUNET_DNSPARSER_TYPE_SRV:
206 struct GNUNET_DNSPARSER_SrvRecord *srv;
210 srv = GNUNET_DNSPARSER_parse_srv (data,
213 if ( (NULL == srv) ||
218 GNUNET_DNSPARSER_free_srv (srv);
221 GNUNET_asprintf (&result,
227 GNUNET_DNSPARSER_free_srv (srv);
230 case GNUNET_DNSPARSER_TYPE_TLSA:
232 const struct GNUNET_TUN_DnsTlsaRecord *tlsa;
236 if (data_size < sizeof (struct GNUNET_TUN_DnsTlsaRecord))
237 return NULL; /* malformed */
239 hex = GNUNET_DNSPARSER_bin_to_hex (&tlsa[1],
240 data_size - sizeof (struct GNUNET_TUN_DnsTlsaRecord));
241 if (0 == GNUNET_asprintf (&tlsa_str,
243 (unsigned int) tlsa->usage,
244 (unsigned int) tlsa->selector,
245 (unsigned int) tlsa->matching_type,
249 GNUNET_free (tlsa_str);
262 * Convert human-readable version of a 'value' of a record to the binary
265 * @param cls closure, unused
266 * @param type type of the record
267 * @param s human-readable string
268 * @param data set to value in binary encoding (will be allocated)
269 * @param data_size set to number of bytes in @a data
270 * @return #GNUNET_OK on success
273 dns_string_to_value (void *cls,
279 struct in_addr value_a;
280 struct in6_addr value_aaaa;
281 struct GNUNET_TUN_DnsTlsaRecord *tlsa;
284 return GNUNET_SYSERR;
287 case GNUNET_DNSPARSER_TYPE_A:
288 if (1 != inet_pton (AF_INET, s, &value_a))
290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
291 _("Unable to parse IPv4 address `%s'\n"),
293 return GNUNET_SYSERR;
295 *data = GNUNET_new (struct in_addr);
296 memcpy (*data, &value_a, sizeof (value_a));
297 *data_size = sizeof (value_a);
299 case GNUNET_DNSPARSER_TYPE_NS:
306 GNUNET_DNSPARSER_builder_add_name (nsbuf,
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
312 _("Failed to serialize NS record with value `%s'\n"),
314 return GNUNET_SYSERR;
317 *data = GNUNET_malloc (off);
318 memcpy (*data, nsbuf, off);
321 case GNUNET_DNSPARSER_TYPE_CNAME:
328 GNUNET_DNSPARSER_builder_add_name (cnamebuf,
333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
334 _("Failed to serialize CNAME record with value `%s'\n"),
336 return GNUNET_SYSERR;
339 *data = GNUNET_malloc (off);
340 memcpy (*data, cnamebuf, off);
343 case GNUNET_DNSPARSER_TYPE_CERT:
355 struct GNUNET_DNSPARSER_CertRecord cert;
357 sdup = GNUNET_strdup (s);
358 typep = strtok (sdup, " ");
359 /* TODO: add typep mnemonic conversion according to RFC 4398 */
360 if ( (NULL == typep) ||
364 (type > UINT16_MAX) )
367 return GNUNET_SYSERR;
369 keyp = strtok (NULL, " ");
370 if ( (NULL == keyp) ||
377 return GNUNET_SYSERR;
379 algp = strtok (NULL, " ");
380 /* TODO: add algp mnemonic conversion according to RFC 4398/RFC 4034 */
381 if ( (NULL == algp) ||
388 return GNUNET_SYSERR;
390 certp = strtok (NULL, " ");
391 if ( (NULL == certp) ||
392 (0 == strlen (certp) ) )
395 return GNUNET_SYSERR;
397 cert_size = GNUNET_STRINGS_base64_decode (certp,
401 cert.cert_type = type;
403 cert.algorithm = alg;
404 cert.certificate_size = cert_size;
405 cert.certificate_data = cert_data;
407 char certbuf[cert_size + sizeof (struct GNUNET_TUN_DnsCertRecord)];
412 GNUNET_DNSPARSER_builder_add_cert (certbuf,
417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
418 _("Failed to serialize CERT record with %u bytes\n"),
419 (unsigned int) cert_size);
420 GNUNET_free (cert_data);
421 return GNUNET_SYSERR;
423 GNUNET_free (cert_data);
425 *data = GNUNET_malloc (off);
426 memcpy (*data, certbuf, off);
428 GNUNET_free (cert_data);
431 case GNUNET_DNSPARSER_TYPE_SOA:
433 struct GNUNET_DNSPARSER_SoaRecord soa;
435 char soa_rname[253 + 1];
436 char soa_mname[253 + 1];
437 unsigned int soa_serial;
438 unsigned int soa_refresh;
439 unsigned int soa_retry;
440 unsigned int soa_expire;
441 unsigned int soa_min;
445 "rname=%253s mname=%253s %u,%u,%u,%u,%u",
454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
455 _("Unable to parse SOA record `%s'\n"),
457 return GNUNET_SYSERR;
459 soa.mname = soa_mname;
460 soa.rname = soa_rname;
461 soa.serial = (uint32_t) soa_serial;
462 soa.refresh =(uint32_t) soa_refresh;
463 soa.retry = (uint32_t) soa_retry;
464 soa.expire = (uint32_t) soa_expire;
465 soa.minimum_ttl = (uint32_t) soa_min;
468 GNUNET_DNSPARSER_builder_add_soa (soabuf,
473 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
474 _("Failed to serialize SOA record with mname `%s' and rname `%s'\n"),
477 return GNUNET_SYSERR;
480 *data = GNUNET_malloc (off);
481 memcpy (*data, soabuf, off);
484 case GNUNET_DNSPARSER_TYPE_PTR:
491 GNUNET_DNSPARSER_builder_add_name (ptrbuf,
496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
497 _("Failed to serialize PTR record with value `%s'\n"),
499 return GNUNET_SYSERR;
502 *data = GNUNET_malloc (off);
503 memcpy (*data, ptrbuf, off);
506 case GNUNET_DNSPARSER_TYPE_MX:
508 struct GNUNET_DNSPARSER_MxRecord mx;
510 char mxhost[253 + 1];
511 unsigned int mx_pref;
519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
520 _("Unable to parse MX record `%s'\n"),
522 return GNUNET_SYSERR;
524 mx.preference = (uint16_t) mx_pref;
529 GNUNET_DNSPARSER_builder_add_mx (mxbuf,
534 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
535 _("Failed to serialize MX record with hostname `%s'\n"),
537 return GNUNET_SYSERR;
540 *data = GNUNET_malloc (off);
541 memcpy (*data, mxbuf, off);
544 case GNUNET_DNSPARSER_TYPE_SRV:
546 struct GNUNET_DNSPARSER_SrvRecord srv;
548 char srvtarget[253 + 1];
549 unsigned int priority;
561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
562 _("Unable to parse SRV record `%s'\n"),
564 return GNUNET_SYSERR;
566 srv.priority = (uint16_t) priority;
567 srv.weight = (uint16_t) weight;
568 srv.port = (uint16_t) port;
569 srv.target = srvtarget;
572 GNUNET_DNSPARSER_builder_add_srv (srvbuf,
577 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
578 _("Failed to serialize SRV record with target `%s'\n"),
580 return GNUNET_SYSERR;
583 *data = GNUNET_malloc (off);
584 memcpy (*data, srvbuf, off);
587 case GNUNET_DNSPARSER_TYPE_TXT:
588 *data = GNUNET_strdup (s);
589 *data_size = strlen (s);
591 case GNUNET_DNSPARSER_TYPE_AAAA:
592 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
595 _("Unable to parse IPv6 address `%s'\n"),
597 return GNUNET_SYSERR;
599 *data = GNUNET_new (struct in6_addr);
600 *data_size = sizeof (struct in6_addr);
601 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
603 case GNUNET_DNSPARSER_TYPE_TLSA:
606 unsigned int selector;
607 unsigned int matching_type;
608 size_t slen = strlen (s) + 1;
618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
619 _("Unable to parse TLSA record string `%s'\n"),
622 return GNUNET_SYSERR;
625 *data_size = sizeof (struct GNUNET_TUN_DnsTlsaRecord) + strlen (hex) / 2;
626 *data = tlsa = GNUNET_malloc (*data_size);
627 tlsa->usage = (uint8_t) usage;
628 tlsa->selector = (uint8_t) selector;
629 tlsa->matching_type = (uint8_t) matching_type;
630 if (strlen (hex) / 2 !=
631 GNUNET_DNSPARSER_hex_to_bin (hex,
634 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
635 _("Unable to parse TLSA record string `%s'\n"),
640 return GNUNET_SYSERR;
645 return GNUNET_SYSERR;
651 * Mapping of record type numbers to human-readable
658 { "A", GNUNET_DNSPARSER_TYPE_A },
659 { "NS", GNUNET_DNSPARSER_TYPE_NS },
660 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
661 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
662 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
663 { "MX", GNUNET_DNSPARSER_TYPE_MX },
664 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
665 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
666 { "SRV", GNUNET_DNSPARSER_TYPE_SRV },
667 { "TLSA", GNUNET_DNSPARSER_TYPE_TLSA },
673 * Convert a type name (i.e. "AAAA") to the corresponding number.
675 * @param cls closure, unused
676 * @param dns_typename name to convert
677 * @return corresponding number, UINT32_MAX on error
680 dns_typename_to_number (void *cls,
681 const char *dns_typename)
686 while ( (NULL != name_map[i].name) &&
687 (0 != strcasecmp (dns_typename, name_map[i].name)) )
689 return name_map[i].number;
694 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
696 * @param cls closure, unused
697 * @param type number of a type to convert
698 * @return corresponding typestring, NULL on error
701 dns_number_to_typename (void *cls,
707 while ( (NULL != name_map[i].name) &&
708 (type != name_map[i].number) )
710 return name_map[i].name;
715 * Entry point for the plugin.
718 * @return the exported block API
721 libgnunet_plugin_gnsrecord_dns_init (void *cls)
723 struct GNUNET_GNSRECORD_PluginFunctions *api;
725 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
726 api->value_to_string = &dns_value_to_string;
727 api->string_to_value = &dns_string_to_value;
728 api->typename_to_number = &dns_typename_to_number;
729 api->number_to_typename = &dns_number_to_typename;
735 * Exit point from the plugin.
737 * @param cls the return value from #libgnunet_plugin_block_test_init
741 libgnunet_plugin_gnsrecord_dns_done (void *cls)
743 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
749 /* end of plugin_gnsrecord_dns.c */