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_MX:
145 struct GNUNET_DNSPARSER_MxRecord *mx;
149 mx = GNUNET_DNSPARSER_parse_mx (data,
156 GNUNET_free_non_null (mx);
159 GNUNET_asprintf (&result,
163 GNUNET_DNSPARSER_free_mx (mx);
166 case GNUNET_DNSPARSER_TYPE_TXT:
167 return GNUNET_strndup (data, data_size);
168 case GNUNET_DNSPARSER_TYPE_AAAA:
169 if (data_size != sizeof (struct in6_addr))
171 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
173 return GNUNET_strdup (tmp);
174 case GNUNET_DNSPARSER_TYPE_SRV:
176 struct GNUNET_DNSPARSER_SrvRecord *srv;
180 srv = GNUNET_DNSPARSER_parse_srv ("+", /* FIXME: is this OK? */
184 if ( (NULL == srv) ||
189 GNUNET_DNSPARSER_free_srv (srv);
192 GNUNET_asprintf (&result,
193 "%d %d %d _%s._%s.%s",
200 GNUNET_DNSPARSER_free_srv (srv);
203 case GNUNET_DNSPARSER_TYPE_TLSA:
205 const struct GNUNET_TUN_DnsTlsaRecord *tlsa;
209 if ( (data_size <= sizeof (struct GNUNET_TUN_DnsTlsaRecord)) ||
210 ('\0' != cdata[data_size - 1]) )
211 return NULL; /* malformed */
213 if (0 == GNUNET_asprintf (&tlsa_str,
218 (const char *) &tlsa[1]))
220 GNUNET_free (tlsa_str);
232 * Convert human-readable version of a 'value' of a record to the binary
235 * @param cls closure, unused
236 * @param type type of the record
237 * @param s human-readable string
238 * @param data set to value in binary encoding (will be allocated)
239 * @param data_size set to number of bytes in @a data
240 * @return #GNUNET_OK on success
243 dns_string_to_value (void *cls,
249 struct in_addr value_a;
250 struct in6_addr value_aaaa;
251 struct GNUNET_TUN_DnsTlsaRecord *tlsa;
254 return GNUNET_SYSERR;
257 case GNUNET_DNSPARSER_TYPE_A:
258 if (1 != inet_pton (AF_INET, s, &value_a))
260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
261 _("Unable to parse IPv4 address `%s'\n"),
263 return GNUNET_SYSERR;
265 *data = GNUNET_malloc (sizeof (struct in_addr));
266 memcpy (*data, &value_a, sizeof (value_a));
267 *data_size = sizeof (value_a);
269 case GNUNET_DNSPARSER_TYPE_NS:
276 GNUNET_DNSPARSER_builder_add_name (nsbuf,
281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282 _("Failed to serialize NS record with value `%s'\n"),
284 return GNUNET_SYSERR;
287 *data = GNUNET_malloc (off);
288 memcpy (*data, nsbuf, off);
291 case GNUNET_DNSPARSER_TYPE_CNAME:
298 GNUNET_DNSPARSER_builder_add_name (cnamebuf,
303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
304 _("Failed to serialize CNAME record with value `%s'\n"),
306 return GNUNET_SYSERR;
309 *data = GNUNET_malloc (off);
310 memcpy (*data, cnamebuf, off);
313 case GNUNET_DNSPARSER_TYPE_SOA:
315 struct GNUNET_DNSPARSER_SoaRecord soa;
317 char soa_rname[253 + 1];
318 char soa_mname[253 + 1];
319 unsigned int soa_serial;
320 unsigned int soa_refresh;
321 unsigned int soa_retry;
322 unsigned int soa_expire;
323 unsigned int soa_min;
327 "rname=%253s mname=%253s %u,%u,%u,%u,%u",
328 soa_rname, soa_mname,
329 &soa_serial, &soa_refresh, &soa_retry, &soa_expire, &soa_min))
331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
332 _("Unable to parse SOA record `%s'\n"),
334 return GNUNET_SYSERR;
336 soa.mname = soa_mname;
337 soa.rname = soa_rname;
338 soa.serial = (uint32_t) soa_serial;
339 soa.refresh =(uint32_t) soa_refresh;
340 soa.retry = (uint32_t) soa_retry;
341 soa.expire = (uint32_t) soa_expire;
342 soa.minimum_ttl = (uint32_t) soa_min;
345 GNUNET_DNSPARSER_builder_add_soa (soabuf,
350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
351 _("Failed to serialize SOA record with mname `%s' and rname `%s'\n"),
354 return GNUNET_SYSERR;
357 *data = GNUNET_malloc (off);
358 memcpy (*data, soabuf, off);
361 case GNUNET_DNSPARSER_TYPE_PTR:
368 GNUNET_DNSPARSER_builder_add_name (ptrbuf,
373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
374 _("Failed to serialize PTR record with value `%s'\n"),
376 return GNUNET_SYSERR;
379 *data = GNUNET_malloc (off);
380 memcpy (*data, ptrbuf, off);
383 case GNUNET_DNSPARSER_TYPE_MX:
385 struct GNUNET_DNSPARSER_MxRecord mx;
387 char mxhost[253 + 1];
391 if (2 != SSCANF(s, "%hu,%253s", &mx_pref, mxhost))
393 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
394 _("Unable to parse MX record `%s'\n"),
396 return GNUNET_SYSERR;
398 mx.preference = mx_pref;
403 GNUNET_DNSPARSER_builder_add_mx (mxbuf,
408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
409 _("Failed to serialize MX record with hostname `%s'\n"),
411 return GNUNET_SYSERR;
414 *data = GNUNET_malloc (off);
415 memcpy (*data, mxbuf, off);
418 case GNUNET_DNSPARSER_TYPE_SRV:
419 GNUNET_break (0); // FIXME: not implemented!
420 return GNUNET_SYSERR;
421 case GNUNET_DNSPARSER_TYPE_TXT:
422 *data = GNUNET_strdup (s);
423 *data_size = strlen (s);
425 case GNUNET_DNSPARSER_TYPE_AAAA:
426 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
429 _("Unable to parse IPv6 address `%s'\n"),
431 return GNUNET_SYSERR;
433 *data = GNUNET_malloc (sizeof (struct in6_addr));
434 *data_size = sizeof (struct in6_addr);
435 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
437 case GNUNET_DNSPARSER_TYPE_TLSA:
438 *data_size = sizeof (struct GNUNET_TUN_DnsTlsaRecord) + strlen (s) - 6;
439 *data = tlsa = GNUNET_malloc (*data_size);
440 if (4 != SSCANF (s, "%c %c %c %s",
443 &tlsa->matching_type,
446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
447 _("Unable to parse TLSA record string `%s'\n"),
451 return GNUNET_SYSERR;
455 return GNUNET_SYSERR;
461 * Mapping of record type numbers to human-readable
468 { "A", GNUNET_DNSPARSER_TYPE_A },
469 { "NS", GNUNET_DNSPARSER_TYPE_NS },
470 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
471 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
472 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
473 { "MX", GNUNET_DNSPARSER_TYPE_MX },
474 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
475 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
476 { "TLSA", GNUNET_DNSPARSER_TYPE_TLSA },
482 * Convert a type name (i.e. "AAAA") to the corresponding number.
484 * @param cls closure, unused
485 * @param dns_typename name to convert
486 * @return corresponding number, UINT32_MAX on error
489 dns_typename_to_number (void *cls,
490 const char *dns_typename)
495 while ( (name_map[i].name != NULL) &&
496 (0 != strcasecmp (dns_typename, name_map[i].name)) )
498 return name_map[i].number;
503 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
505 * @param cls closure, unused
506 * @param type number of a type to convert
507 * @return corresponding typestring, NULL on error
510 dns_number_to_typename (void *cls,
516 while ( (name_map[i].name != NULL) &&
517 (type != name_map[i].number) )
519 return name_map[i].name;
524 * Entry point for the plugin.
527 * @return the exported block API
530 libgnunet_plugin_gnsrecord_dns_init (void *cls)
532 struct GNUNET_GNSRECORD_PluginFunctions *api;
534 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
535 api->value_to_string = &dns_value_to_string;
536 api->string_to_value = &dns_string_to_value;
537 api->typename_to_number = &dns_typename_to_number;
538 api->number_to_typename = &dns_number_to_typename;
544 * Exit point from the plugin.
546 * @param cls the return value from #libgnunet_plugin_block_test_init
550 libgnunet_plugin_gnsrecord_dns_done (void *cls)
552 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
558 /* end of plugin_gnsrecord_dns.c */