2 This file is part of GNUnet.
3 (C) 2009-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/gnsrecord.c
23 * @brief API to access GNS record data
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
26 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_conversation_service.h"
33 #include "gnunet_dnsparser_lib.h"
34 #include "gnunet_gnsrecord_lib.h"
35 #include "gnunet_tun_lib.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__)
42 * Convert the 'value' of a record to a string.
44 * @param type type of the record
45 * @param data value in binary encoding
46 * @param data_size number of bytes in @a data
47 * @return NULL on error, otherwise human-readable representation of the value
50 GNUNET_GNSRECORD_value_to_string (uint32_t type,
56 char tmp[INET6_ADDRSTRLEN];
62 case GNUNET_DNSPARSER_TYPE_A:
63 if (data_size != sizeof (struct in_addr))
65 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
67 return GNUNET_strdup (tmp);
68 case GNUNET_DNSPARSER_TYPE_NS:
74 ns = GNUNET_DNSPARSER_parse_name (data,
85 case GNUNET_DNSPARSER_TYPE_CNAME:
91 cname = GNUNET_DNSPARSER_parse_name (data,
94 if ( (NULL == cname) ||
98 GNUNET_free_non_null (cname);
103 case GNUNET_DNSPARSER_TYPE_SOA:
105 struct GNUNET_DNSPARSER_SoaRecord *soa;
109 soa = GNUNET_DNSPARSER_parse_soa (data,
112 if ( (NULL == soa) ||
118 GNUNET_asprintf (&result,
119 "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
127 GNUNET_DNSPARSER_free_soa (soa);
130 case GNUNET_DNSPARSER_TYPE_PTR:
136 ptr = GNUNET_DNSPARSER_parse_name (data,
139 if ( (NULL == ptr) ||
143 GNUNET_free_non_null (ptr);
148 case GNUNET_DNSPARSER_TYPE_MX:
150 struct GNUNET_DNSPARSER_MxRecord *mx;
154 mx = GNUNET_DNSPARSER_parse_mx (data,
161 GNUNET_free_non_null (mx);
164 GNUNET_asprintf (&result,
168 GNUNET_DNSPARSER_free_mx (mx);
171 case GNUNET_DNSPARSER_TYPE_TXT:
172 return GNUNET_strndup (data, data_size);
173 case GNUNET_DNSPARSER_TYPE_AAAA:
174 if (data_size != sizeof (struct in6_addr))
176 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
178 return GNUNET_strdup (tmp);
179 case GNUNET_GNSRECORD_TYPE_PKEY:
180 if (data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
182 return GNUNET_CRYPTO_ecdsa_public_key_to_string (data);
183 case GNUNET_GNSRECORD_TYPE_PHONE:
185 const struct GNUNET_CONVERSATION_PhoneRecord *pr;
189 if (data_size != sizeof (struct GNUNET_CONVERSATION_PhoneRecord))
192 if (0 != ntohl (pr->version))
194 pkey = GNUNET_CRYPTO_eddsa_public_key_to_string (&pr->peer.public_key);
195 GNUNET_asprintf (&ret,
202 case GNUNET_GNSRECORD_TYPE_PSEU:
203 return GNUNET_strndup (data, data_size);
204 case GNUNET_GNSRECORD_TYPE_LEHO:
205 return GNUNET_strndup (data, data_size);
206 case GNUNET_GNSRECORD_TYPE_VPN:
208 const struct GNUNET_TUN_GnsVpnRecord *vpn;
212 if ( (data_size <= sizeof (struct GNUNET_TUN_GnsVpnRecord)) ||
213 ('\0' != cdata[data_size - 1]) )
214 return NULL; /* malformed */
216 if (0 == GNUNET_asprintf (&vpn_str, "%u %s %s",
217 (unsigned int) ntohs (vpn->proto),
218 (const char*) GNUNET_i2s_full (&vpn->peer),
219 (const char*) &vpn[1]))
221 GNUNET_free (vpn_str);
226 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
232 ns = GNUNET_DNSPARSER_parse_name (data,
239 GNUNET_free_non_null (ns);
244 case GNUNET_DNSPARSER_TYPE_SRV:
246 struct GNUNET_DNSPARSER_SrvRecord *srv;
250 srv = GNUNET_DNSPARSER_parse_srv ("+", /* FIXME: is this OK? */
254 if ( (NULL == srv) ||
260 GNUNET_asprintf (&result,
261 "%d %d %d _%s._%s.%s",
268 GNUNET_DNSPARSER_free_srv (srv);
271 case GNUNET_DNSPARSER_TYPE_TLSA:
273 const struct GNUNET_TUN_DnsTlsaRecord *tlsa;
277 if ( (data_size <= sizeof (struct GNUNET_TUN_DnsTlsaRecord)) ||
278 ('\0' != cdata[data_size - 1]) )
279 return NULL; /* malformed */
281 if (0 == GNUNET_asprintf (&tlsa_str,
286 (const char *) &tlsa[1]))
288 GNUNET_free (tlsa_str);
296 GNUNET_break (0); // not implemented
302 * Convert human-readable version of a 'value' of a record to the binary
305 * @param type type of the record
306 * @param s human-readable string
307 * @param data set to value in binary encoding (will be allocated)
308 * @param data_size set to number of bytes in @a data
309 * @return #GNUNET_OK on success
312 GNUNET_GNSRECORD_string_to_value (uint32_t type,
317 struct in_addr value_a;
318 struct in6_addr value_aaaa;
319 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
320 struct GNUNET_TUN_GnsVpnRecord *vpn;
321 struct GNUNET_TUN_DnsTlsaRecord *tlsa;
322 char s_peer[103 + 1];
323 char s_serv[253 + 1];
327 return GNUNET_SYSERR;
331 LOG (GNUNET_ERROR_TYPE_ERROR,
332 _("Unsupported record type %d\n"),
334 return GNUNET_SYSERR;
335 case GNUNET_DNSPARSER_TYPE_A:
336 if (1 != inet_pton (AF_INET, s, &value_a))
338 LOG (GNUNET_ERROR_TYPE_ERROR,
339 _("Unable to parse IPv4 address `%s'\n"),
341 return GNUNET_SYSERR;
343 *data = GNUNET_malloc (sizeof (struct in_addr));
344 memcpy (*data, &value_a, sizeof (value_a));
345 *data_size = sizeof (value_a);
347 case GNUNET_DNSPARSER_TYPE_NS:
354 GNUNET_DNSPARSER_builder_add_name (nsbuf,
359 LOG (GNUNET_ERROR_TYPE_ERROR,
360 _("Failed to serialize NS record with value `%s'\n"),
362 return GNUNET_SYSERR;
365 *data = GNUNET_malloc (off);
366 memcpy (*data, nsbuf, off);
369 case GNUNET_DNSPARSER_TYPE_CNAME:
376 GNUNET_DNSPARSER_builder_add_name (cnamebuf,
381 LOG (GNUNET_ERROR_TYPE_ERROR,
382 _("Failed to serialize CNAME record with value `%s'\n"),
384 return GNUNET_SYSERR;
387 *data = GNUNET_malloc (off);
388 memcpy (*data, cnamebuf, off);
391 case GNUNET_DNSPARSER_TYPE_SOA:
393 struct GNUNET_DNSPARSER_SoaRecord soa;
395 char soa_rname[253 + 1];
396 char soa_mname[253 + 1];
397 unsigned int soa_serial;
398 unsigned int soa_refresh;
399 unsigned int soa_retry;
400 unsigned int soa_expire;
401 unsigned int soa_min;
405 "rname=%253s mname=%253s %u,%u,%u,%u,%u",
406 soa_rname, soa_mname,
407 &soa_serial, &soa_refresh, &soa_retry, &soa_expire, &soa_min))
409 LOG (GNUNET_ERROR_TYPE_ERROR,
410 _("Unable to parse SOA record `%s'\n"),
412 return GNUNET_SYSERR;
414 soa.mname = soa_mname;
415 soa.rname = soa_rname;
416 soa.serial = (uint32_t) soa_serial;
417 soa.refresh =(uint32_t) soa_refresh;
418 soa.retry = (uint32_t) soa_retry;
419 soa.expire = (uint32_t) soa_expire;
420 soa.minimum_ttl = (uint32_t) soa_min;
423 GNUNET_DNSPARSER_builder_add_soa (soabuf,
428 LOG (GNUNET_ERROR_TYPE_ERROR,
429 _("Failed to serialize SOA record with mname `%s' and rname `%s'\n"),
432 return GNUNET_SYSERR;
435 *data = GNUNET_malloc (off);
436 memcpy (*data, soabuf, off);
439 case GNUNET_DNSPARSER_TYPE_PTR:
446 GNUNET_DNSPARSER_builder_add_name (ptrbuf,
451 LOG (GNUNET_ERROR_TYPE_ERROR,
452 _("Failed to serialize PTR record with value `%s'\n"),
454 return GNUNET_SYSERR;
457 *data = GNUNET_malloc (off);
458 memcpy (*data, ptrbuf, off);
461 case GNUNET_DNSPARSER_TYPE_MX:
463 struct GNUNET_DNSPARSER_MxRecord mx;
465 char mxhost[253 + 1];
469 if (2 != SSCANF(s, "%hu,%253s", &mx_pref, mxhost))
471 LOG (GNUNET_ERROR_TYPE_ERROR,
472 _("Unable to parse MX record `%s'\n"),
474 return GNUNET_SYSERR;
476 mx.preference = mx_pref;
481 GNUNET_DNSPARSER_builder_add_mx (mxbuf,
486 LOG (GNUNET_ERROR_TYPE_ERROR,
487 _("Failed to serialize MX record with hostname `%s'\n"),
489 return GNUNET_SYSERR;
492 *data = GNUNET_malloc (off);
493 memcpy (*data, mxbuf, off);
496 case GNUNET_DNSPARSER_TYPE_TXT:
497 *data = GNUNET_strdup (s);
498 *data_size = strlen (s);
500 case GNUNET_DNSPARSER_TYPE_AAAA:
501 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
503 LOG (GNUNET_ERROR_TYPE_ERROR,
504 _("Unable to parse IPv6 address `%s'\n"),
506 return GNUNET_SYSERR;
508 *data = GNUNET_malloc (sizeof (struct in6_addr));
509 *data_size = sizeof (struct in6_addr);
510 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
512 case GNUNET_GNSRECORD_TYPE_PKEY:
514 GNUNET_CRYPTO_ecdsa_public_key_from_string (s, strlen (s), &pkey))
516 LOG (GNUNET_ERROR_TYPE_ERROR,
517 _("Unable to parse PKEY record `%s'\n"),
519 return GNUNET_SYSERR;
521 *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
522 memcpy (*data, &pkey, sizeof (pkey));
523 *data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
525 case GNUNET_GNSRECORD_TYPE_PHONE:
527 struct GNUNET_CONVERSATION_PhoneRecord *pr;
530 struct GNUNET_PeerIdentity peer;
532 if ( (NULL == (dash = strchr (s, '-'))) ||
533 (1 != sscanf (s, "%u-", &line)) ||
535 GNUNET_CRYPTO_eddsa_public_key_from_string (dash + 1,
539 LOG (GNUNET_ERROR_TYPE_ERROR,
540 _("Unable to parse PHONE record `%s'\n"),
542 return GNUNET_SYSERR;
544 pr = GNUNET_new (struct GNUNET_CONVERSATION_PhoneRecord);
545 pr->version = htonl (0);
546 pr->line = htonl ((uint32_t) line);
549 *data_size = sizeof (struct GNUNET_CONVERSATION_PhoneRecord);
552 case GNUNET_GNSRECORD_TYPE_PSEU:
553 *data = GNUNET_strdup (s);
554 *data_size = strlen (s);
556 case GNUNET_GNSRECORD_TYPE_LEHO:
557 *data = GNUNET_strdup (s);
558 *data_size = strlen (s);
560 case GNUNET_GNSRECORD_TYPE_VPN:
561 if (3 != SSCANF (s,"%u %103s %253s",
562 &proto, s_peer, s_serv))
564 LOG (GNUNET_ERROR_TYPE_ERROR,
565 _("Unable to parse VPN record string `%s'\n"),
567 return GNUNET_SYSERR;
569 *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
570 *data = vpn = GNUNET_malloc (*data_size);
571 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string ((char*) s_peer,
573 &vpn->peer.public_key))
577 return GNUNET_SYSERR;
579 vpn->proto = htons ((uint16_t) proto);
580 strcpy ((char*)&vpn[1], s_serv);
582 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
589 GNUNET_DNSPARSER_builder_add_name (nsbuf,
594 LOG (GNUNET_ERROR_TYPE_ERROR,
595 _("Failed to serialize GNS2DNS record with value `%s'\n"),
597 return GNUNET_SYSERR;
600 *data = GNUNET_malloc (off);
601 memcpy (*data, nsbuf, off);
604 case GNUNET_DNSPARSER_TYPE_TLSA:
605 *data_size = sizeof (struct GNUNET_TUN_DnsTlsaRecord) + strlen (s) - 6;
606 *data = tlsa = GNUNET_malloc (*data_size);
607 if (4 != SSCANF (s, "%c %c %c %s",
610 &tlsa->matching_type,
613 LOG (GNUNET_ERROR_TYPE_ERROR,
614 _("Unable to parse TLSA record string `%s'\n"),
618 return GNUNET_SYSERR;
622 LOG (GNUNET_ERROR_TYPE_ERROR,
623 _("Unsupported record type %d\n"),
625 return GNUNET_SYSERR;
631 * Mapping of record type numbers to human-readable
638 { "A", GNUNET_DNSPARSER_TYPE_A },
639 { "NS", GNUNET_DNSPARSER_TYPE_NS },
640 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
641 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
642 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
643 { "MX", GNUNET_DNSPARSER_TYPE_MX },
644 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
645 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
646 { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
647 { "PSEU", GNUNET_GNSRECORD_TYPE_PSEU },
648 { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO },
649 { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
650 { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
651 { "PHONE", GNUNET_GNSRECORD_TYPE_PHONE },
652 { "TLSA", GNUNET_DNSPARSER_TYPE_TLSA },
658 * Convert a type name (i.e. "AAAA") to the corresponding number.
660 * @param dns_typename name to convert
661 * @return corresponding number, UINT32_MAX on error
664 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename)
669 while ( (name_map[i].name != NULL) &&
670 (0 != strcasecmp (dns_typename, name_map[i].name)) )
672 return name_map[i].number;
677 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
679 * @param type number of a type to convert
680 * @return corresponding typestring, NULL on error
683 GNUNET_GNSRECORD_number_to_typename (uint32_t type)
688 while ( (name_map[i].name != NULL) &&
689 (type != name_map[i].number) )
691 return name_map[i].name;
696 /* end of namestore_common.c */