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,
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) ||
110 GNUNET_asprintf (&result,
111 "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
119 GNUNET_DNSPARSER_free_soa (soa);
122 case GNUNET_DNSPARSER_TYPE_PTR:
128 ptr = GNUNET_DNSPARSER_parse_name (data,
131 if ( (NULL == ptr) ||
135 GNUNET_free_non_null (ptr);
140 case GNUNET_DNSPARSER_TYPE_MX:
142 struct GNUNET_DNSPARSER_MxRecord *mx;
146 mx = GNUNET_DNSPARSER_parse_mx (data,
153 GNUNET_free_non_null (mx);
156 GNUNET_asprintf (&result,
160 GNUNET_DNSPARSER_free_mx (mx);
163 case GNUNET_DNSPARSER_TYPE_TXT:
164 return GNUNET_strndup (data, data_size);
165 case GNUNET_DNSPARSER_TYPE_AAAA:
166 if (data_size != sizeof (struct in6_addr))
168 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
170 return GNUNET_strdup (tmp);
171 case GNUNET_DNSPARSER_TYPE_SRV:
173 struct GNUNET_DNSPARSER_SrvRecord *srv;
177 srv = GNUNET_DNSPARSER_parse_srv ("+", /* FIXME: is this OK? */
181 if ( (NULL == srv) ||
187 GNUNET_asprintf (&result,
188 "%d %d %d _%s._%s.%s",
195 GNUNET_DNSPARSER_free_srv (srv);
198 case GNUNET_DNSPARSER_TYPE_TLSA:
200 const struct GNUNET_TUN_DnsTlsaRecord *tlsa;
204 if ( (data_size <= sizeof (struct GNUNET_TUN_DnsTlsaRecord)) ||
205 ('\0' != cdata[data_size - 1]) )
206 return NULL; /* malformed */
208 if (0 == GNUNET_asprintf (&tlsa_str,
213 (const char *) &tlsa[1]))
215 GNUNET_free (tlsa_str);
227 * Convert human-readable version of a 'value' of a record to the binary
230 * @param cls closure, unused
231 * @param type type of the record
232 * @param s human-readable string
233 * @param data set to value in binary encoding (will be allocated)
234 * @param data_size set to number of bytes in @a data
235 * @return #GNUNET_OK on success
238 dns_string_to_value (void *cls,
244 struct in_addr value_a;
245 struct in6_addr value_aaaa;
246 struct GNUNET_TUN_DnsTlsaRecord *tlsa;
249 return GNUNET_SYSERR;
252 case GNUNET_DNSPARSER_TYPE_A:
253 if (1 != inet_pton (AF_INET, s, &value_a))
255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256 _("Unable to parse IPv4 address `%s'\n"),
258 return GNUNET_SYSERR;
260 *data = GNUNET_malloc (sizeof (struct in_addr));
261 memcpy (*data, &value_a, sizeof (value_a));
262 *data_size = sizeof (value_a);
264 case GNUNET_DNSPARSER_TYPE_NS:
271 GNUNET_DNSPARSER_builder_add_name (nsbuf,
276 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
277 _("Failed to serialize NS record with value `%s'\n"),
279 return GNUNET_SYSERR;
282 *data = GNUNET_malloc (off);
283 memcpy (*data, nsbuf, off);
286 case GNUNET_DNSPARSER_TYPE_CNAME:
293 GNUNET_DNSPARSER_builder_add_name (cnamebuf,
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299 _("Failed to serialize CNAME record with value `%s'\n"),
301 return GNUNET_SYSERR;
304 *data = GNUNET_malloc (off);
305 memcpy (*data, cnamebuf, off);
308 case GNUNET_DNSPARSER_TYPE_SOA:
310 struct GNUNET_DNSPARSER_SoaRecord soa;
312 char soa_rname[253 + 1];
313 char soa_mname[253 + 1];
314 unsigned int soa_serial;
315 unsigned int soa_refresh;
316 unsigned int soa_retry;
317 unsigned int soa_expire;
318 unsigned int soa_min;
322 "rname=%253s mname=%253s %u,%u,%u,%u,%u",
323 soa_rname, soa_mname,
324 &soa_serial, &soa_refresh, &soa_retry, &soa_expire, &soa_min))
326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
327 _("Unable to parse SOA record `%s'\n"),
329 return GNUNET_SYSERR;
331 soa.mname = soa_mname;
332 soa.rname = soa_rname;
333 soa.serial = (uint32_t) soa_serial;
334 soa.refresh =(uint32_t) soa_refresh;
335 soa.retry = (uint32_t) soa_retry;
336 soa.expire = (uint32_t) soa_expire;
337 soa.minimum_ttl = (uint32_t) soa_min;
340 GNUNET_DNSPARSER_builder_add_soa (soabuf,
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346 _("Failed to serialize SOA record with mname `%s' and rname `%s'\n"),
349 return GNUNET_SYSERR;
352 *data = GNUNET_malloc (off);
353 memcpy (*data, soabuf, off);
356 case GNUNET_DNSPARSER_TYPE_PTR:
363 GNUNET_DNSPARSER_builder_add_name (ptrbuf,
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369 _("Failed to serialize PTR record with value `%s'\n"),
371 return GNUNET_SYSERR;
374 *data = GNUNET_malloc (off);
375 memcpy (*data, ptrbuf, off);
378 case GNUNET_DNSPARSER_TYPE_MX:
380 struct GNUNET_DNSPARSER_MxRecord mx;
382 char mxhost[253 + 1];
386 if (2 != SSCANF(s, "%hu,%253s", &mx_pref, mxhost))
388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
389 _("Unable to parse MX record `%s'\n"),
391 return GNUNET_SYSERR;
393 mx.preference = mx_pref;
398 GNUNET_DNSPARSER_builder_add_mx (mxbuf,
403 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
404 _("Failed to serialize MX record with hostname `%s'\n"),
406 return GNUNET_SYSERR;
409 *data = GNUNET_malloc (off);
410 memcpy (*data, mxbuf, off);
413 case GNUNET_DNSPARSER_TYPE_SRV:
414 GNUNET_break (0); // FIXME: not implemented!
415 return GNUNET_SYSERR;
416 case GNUNET_DNSPARSER_TYPE_TXT:
417 *data = GNUNET_strdup (s);
418 *data_size = strlen (s);
420 case GNUNET_DNSPARSER_TYPE_AAAA:
421 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
424 _("Unable to parse IPv6 address `%s'\n"),
426 return GNUNET_SYSERR;
428 *data = GNUNET_malloc (sizeof (struct in6_addr));
429 *data_size = sizeof (struct in6_addr);
430 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
432 case GNUNET_DNSPARSER_TYPE_TLSA:
433 *data_size = sizeof (struct GNUNET_TUN_DnsTlsaRecord) + strlen (s) - 6;
434 *data = tlsa = GNUNET_malloc (*data_size);
435 if (4 != SSCANF (s, "%c %c %c %s",
438 &tlsa->matching_type,
441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
442 _("Unable to parse TLSA record string `%s'\n"),
446 return GNUNET_SYSERR;
450 return GNUNET_SYSERR;
456 * Mapping of record type numbers to human-readable
463 { "A", GNUNET_DNSPARSER_TYPE_A },
464 { "NS", GNUNET_DNSPARSER_TYPE_NS },
465 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
466 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
467 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
468 { "MX", GNUNET_DNSPARSER_TYPE_MX },
469 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
470 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
471 { "TLSA", GNUNET_DNSPARSER_TYPE_TLSA },
477 * Convert a type name (i.e. "AAAA") to the corresponding number.
479 * @param cls closure, unused
480 * @param dns_typename name to convert
481 * @return corresponding number, UINT32_MAX on error
484 dns_typename_to_number (void *cls,
485 const char *dns_typename)
490 while ( (name_map[i].name != NULL) &&
491 (0 != strcasecmp (dns_typename, name_map[i].name)) )
493 return name_map[i].number;
498 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
500 * @param cls closure, unused
501 * @param type number of a type to convert
502 * @return corresponding typestring, NULL on error
505 dns_number_to_typename (void *cls,
511 while ( (name_map[i].name != NULL) &&
512 (type != name_map[i].number) )
514 return name_map[i].name;
519 * Entry point for the plugin.
522 * @return the exported block API
525 libgnunet_plugin_gnsrecord_dns_init (void *cls)
527 struct GNUNET_GNSRECORD_PluginFunctions *api;
529 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
530 api->value_to_string = &dns_value_to_string;
531 api->string_to_value = &dns_string_to_value;
532 api->typename_to_number = &dns_typename_to_number;
533 api->number_to_typename = &dns_number_to_typename;
539 * Exit point from the plugin.
541 * @param cls the return value from #libgnunet_plugin_block_test_init
545 libgnunet_plugin_gnsrecord_dns_done (void *cls)
547 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
553 /* end of plugin_gnsrecord_dns.c */