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,
49 char tmp[INET6_ADDRSTRLEN];
53 case GNUNET_DNSPARSER_TYPE_A:
54 if (data_size != sizeof (struct in_addr))
56 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
58 return GNUNET_strdup (tmp);
59 case GNUNET_DNSPARSER_TYPE_NS:
65 ns = GNUNET_DNSPARSER_parse_name (data,
72 GNUNET_free_non_null (ns);
77 case GNUNET_DNSPARSER_TYPE_CNAME:
83 cname = GNUNET_DNSPARSER_parse_name (data,
86 if ( (NULL == cname) ||
90 GNUNET_free_non_null (cname);
95 case GNUNET_DNSPARSER_TYPE_SOA:
97 struct GNUNET_DNSPARSER_SoaRecord *soa;
101 soa = GNUNET_DNSPARSER_parse_soa (data,
104 if ( (NULL == soa) ||
109 GNUNET_DNSPARSER_free_soa (soa);
112 GNUNET_asprintf (&result,
113 "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
121 GNUNET_DNSPARSER_free_soa (soa);
124 case GNUNET_DNSPARSER_TYPE_PTR:
130 ptr = GNUNET_DNSPARSER_parse_name (data,
133 if ( (NULL == ptr) ||
137 GNUNET_free_non_null (ptr);
142 case GNUNET_DNSPARSER_TYPE_CERT:
144 struct GNUNET_DNSPARSER_CertRecord *cert;
150 cert = GNUNET_DNSPARSER_parse_cert (data,
153 if ( (NULL == cert) ||
157 GNUNET_DNSPARSER_free_cert (cert);
160 len = GNUNET_STRINGS_base64_encode (cert->certificate_data,
161 cert->certificate_size,
163 GNUNET_asprintf (&result,
170 GNUNET_free (base64);
171 GNUNET_DNSPARSER_free_cert (cert);
174 case GNUNET_DNSPARSER_TYPE_MX:
176 struct GNUNET_DNSPARSER_MxRecord *mx;
180 mx = GNUNET_DNSPARSER_parse_mx (data,
187 GNUNET_DNSPARSER_free_mx (mx);
190 GNUNET_asprintf (&result,
192 (unsigned int) mx->preference,
194 GNUNET_DNSPARSER_free_mx (mx);
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))
202 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
204 return GNUNET_strdup (tmp);
205 case GNUNET_DNSPARSER_TYPE_SRV:
207 struct GNUNET_DNSPARSER_SrvRecord *srv;
211 srv = GNUNET_DNSPARSER_parse_srv (data,
214 if ( (NULL == srv) ||
219 GNUNET_DNSPARSER_free_srv (srv);
222 GNUNET_asprintf (&result,
228 GNUNET_DNSPARSER_free_srv (srv);
231 case GNUNET_DNSPARSER_TYPE_TLSA:
233 const struct GNUNET_TUN_DnsTlsaRecord *tlsa;
237 if ( (data_size <= sizeof (struct GNUNET_TUN_DnsTlsaRecord)) ||
238 ('\0' != cdata[data_size - 1]) )
239 return NULL; /* malformed */
241 if (0 == GNUNET_asprintf (&tlsa_str,
246 (const char *) &tlsa[1]))
248 GNUNET_free (tlsa_str);
260 * Convert human-readable version of a 'value' of a record to the binary
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
271 dns_string_to_value (void *cls,
277 struct in_addr value_a;
278 struct in6_addr value_aaaa;
279 struct GNUNET_TUN_DnsTlsaRecord *tlsa;
282 return GNUNET_SYSERR;
285 case GNUNET_DNSPARSER_TYPE_A:
286 if (1 != inet_pton (AF_INET, s, &value_a))
288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
289 _("Unable to parse IPv4 address `%s'\n"),
291 return GNUNET_SYSERR;
293 *data = GNUNET_new (struct in_addr);
294 memcpy (*data, &value_a, sizeof (value_a));
295 *data_size = sizeof (value_a);
297 case GNUNET_DNSPARSER_TYPE_NS:
304 GNUNET_DNSPARSER_builder_add_name (nsbuf,
309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310 _("Failed to serialize NS record with value `%s'\n"),
312 return GNUNET_SYSERR;
315 *data = GNUNET_malloc (off);
316 memcpy (*data, nsbuf, off);
319 case GNUNET_DNSPARSER_TYPE_CNAME:
326 GNUNET_DNSPARSER_builder_add_name (cnamebuf,
331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
332 _("Failed to serialize CNAME record with value `%s'\n"),
334 return GNUNET_SYSERR;
337 *data = GNUNET_malloc (off);
338 memcpy (*data, cnamebuf, off);
341 case GNUNET_DNSPARSER_TYPE_CERT:
353 struct GNUNET_DNSPARSER_CertRecord cert;
355 sdup = GNUNET_strdup (s);
356 typep = strtok (sdup, " ");
357 /* TODO: add typep mnemonic conversion according to RFC 4398 */
358 if ( (NULL == typep) ||
362 (type > UINT16_MAX) )
365 return GNUNET_SYSERR;
367 keyp = strtok (NULL, " ");
368 if ( (NULL == keyp) ||
375 return GNUNET_SYSERR;
377 algp = strtok (NULL, " ");
378 /* TODO: add algp mnemonic conversion according to RFC 4398/RFC 4034 */
379 if ( (NULL == algp) ||
386 return GNUNET_SYSERR;
388 certp = strtok (NULL, " ");
389 if ( (NULL == certp) ||
390 (0 == strlen (certp) ) )
393 return GNUNET_SYSERR;
395 cert_size = GNUNET_STRINGS_base64_decode (certp,
399 cert.cert_type = type;
401 cert.algorithm = alg;
402 cert.certificate_size = cert_size;
403 cert.certificate_data = cert_data;
405 char certbuf[cert_size + sizeof (struct GNUNET_TUN_DnsCertRecord)];
410 GNUNET_DNSPARSER_builder_add_cert (certbuf,
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;
421 GNUNET_free (cert_data);
423 *data = GNUNET_malloc (off);
424 memcpy (*data, certbuf, off);
426 GNUNET_free (cert_data);
429 case GNUNET_DNSPARSER_TYPE_SOA:
431 struct GNUNET_DNSPARSER_SoaRecord soa;
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;
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))
447 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
448 _("Unable to parse SOA record `%s'\n"),
450 return GNUNET_SYSERR;
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;
461 GNUNET_DNSPARSER_builder_add_soa (soabuf,
466 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
467 _("Failed to serialize SOA record with mname `%s' and rname `%s'\n"),
470 return GNUNET_SYSERR;
473 *data = GNUNET_malloc (off);
474 memcpy (*data, soabuf, off);
477 case GNUNET_DNSPARSER_TYPE_PTR:
484 GNUNET_DNSPARSER_builder_add_name (ptrbuf,
489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
490 _("Failed to serialize PTR record with value `%s'\n"),
492 return GNUNET_SYSERR;
495 *data = GNUNET_malloc (off);
496 memcpy (*data, ptrbuf, off);
499 case GNUNET_DNSPARSER_TYPE_MX:
501 struct GNUNET_DNSPARSER_MxRecord mx;
503 char mxhost[253 + 1];
504 unsigned int mx_pref;
512 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
513 _("Unable to parse MX record `%s'\n"),
515 return GNUNET_SYSERR;
517 mx.preference = (uint16_t) mx_pref;
522 GNUNET_DNSPARSER_builder_add_mx (mxbuf,
527 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
528 _("Failed to serialize MX record with hostname `%s'\n"),
530 return GNUNET_SYSERR;
533 *data = GNUNET_malloc (off);
534 memcpy (*data, mxbuf, off);
537 case GNUNET_DNSPARSER_TYPE_SRV:
539 struct GNUNET_DNSPARSER_SrvRecord srv;
541 char srvtarget[253 + 1];
542 unsigned int priority;
554 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
555 _("Unable to parse SRV record `%s'\n"),
557 return GNUNET_SYSERR;
559 srv.priority = (uint16_t) priority;
560 srv.weight = (uint16_t) weight;
561 srv.port = (uint16_t) port;
562 srv.target = srvtarget;
565 GNUNET_DNSPARSER_builder_add_srv (srvbuf,
570 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
571 _("Failed to serialize SRV record with target `%s'\n"),
573 return GNUNET_SYSERR;
576 *data = GNUNET_malloc (off);
577 memcpy (*data, srvbuf, off);
580 case GNUNET_DNSPARSER_TYPE_TXT:
581 *data = GNUNET_strdup (s);
582 *data_size = strlen (s);
584 case GNUNET_DNSPARSER_TYPE_AAAA:
585 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
587 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
588 _("Unable to parse IPv6 address `%s'\n"),
590 return GNUNET_SYSERR;
592 *data = GNUNET_new (struct in6_addr);
593 *data_size = sizeof (struct in6_addr);
594 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
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",
602 &tlsa->matching_type,
605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
606 _("Unable to parse TLSA record string `%s'\n"),
610 return GNUNET_SYSERR;
614 return GNUNET_SYSERR;
620 * Mapping of record type numbers to human-readable
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 },
641 * Convert a type name (i.e. "AAAA") to the corresponding number.
643 * @param cls closure, unused
644 * @param dns_typename name to convert
645 * @return corresponding number, UINT32_MAX on error
648 dns_typename_to_number (void *cls,
649 const char *dns_typename)
654 while ( (name_map[i].name != NULL) &&
655 (0 != strcasecmp (dns_typename, name_map[i].name)) )
657 return name_map[i].number;
662 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
664 * @param cls closure, unused
665 * @param type number of a type to convert
666 * @return corresponding typestring, NULL on error
669 dns_number_to_typename (void *cls,
675 while ( (name_map[i].name != NULL) &&
676 (type != name_map[i].number) )
678 return name_map[i].name;
683 * Entry point for the plugin.
686 * @return the exported block API
689 libgnunet_plugin_gnsrecord_dns_init (void *cls)
691 struct GNUNET_GNSRECORD_PluginFunctions *api;
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;
703 * Exit point from the plugin.
705 * @param cls the return value from #libgnunet_plugin_block_test_init
709 libgnunet_plugin_gnsrecord_dns_done (void *cls)
711 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
717 /* end of plugin_gnsrecord_dns.c */