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 namestore/namestore_api_common.c
23 * @brief API to access the NAMESTORE service
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_arm_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_dnsparser_lib.h"
35 #include "gnunet_tun_lib.h"
36 #include "namestore.h"
39 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-api",__VA_ARGS__)
41 GNUNET_NETWORK_STRUCT_BEGIN
45 * Internal format of a record in the serialized form.
51 * Expiration time for the DNS record; relative or absolute depends
52 * on 'flags', network byte order.
54 uint64_t expiration_time GNUNET_PACKED;
57 * Number of bytes in 'data', network byte order.
59 uint32_t data_size GNUNET_PACKED;
62 * Type of the GNS/DNS record, network byte order.
64 uint32_t record_type GNUNET_PACKED;
67 * Flags for the record, network byte order.
69 uint32_t flags GNUNET_PACKED;
73 GNUNET_NETWORK_STRUCT_END
76 * Convert a UTF-8 string to UTF-8 lowercase
77 * @param src source string
78 * @return converted result
81 GNUNET_NAMESTORE_normalize_string (const char *src)
83 GNUNET_assert (NULL != src);
84 char *res = strdup (src);
86 GNUNET_STRINGS_utf8_tolower(src, &res);
92 * Convert a zone key to a string (for printing debug messages).
93 * This is one of the very few calls in the entire API that is
96 * @param z the zone key
97 * @return string form; will be overwritten by next call to #GNUNET_NAMESTORE_z2s
100 GNUNET_NAMESTORE_z2s (const struct GNUNET_CRYPTO_EccPublicSignKey *z)
102 static char buf[sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) * 8];
105 end = GNUNET_STRINGS_data_to_string ((const unsigned char *) z,
106 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
119 * Calculate how many bytes we will need to serialize the given
122 * @param rd_count number of records in the rd array
123 * @param rd array of #GNUNET_NAMESTORE_RecordData with @a rd_count elements
124 * @return the required size to serialize
127 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
128 const struct GNUNET_NAMESTORE_RecordData *rd)
133 ret = sizeof (struct NetworkRecord) * rd_count;
134 for (i=0;i<rd_count;i++)
136 GNUNET_assert ((ret + rd[i].data_size) >= ret);
137 ret += rd[i].data_size;
144 * Serialize the given records to the given destination buffer.
146 * @param rd_count number of records in the rd array
147 * @param rd array of #GNUNET_NAMESTORE_RecordData with @a rd_count elements
148 * @param dest_size size of the destination array
149 * @param dest where to write the result
150 * @return the size of serialized records, -1 if records do not fit
153 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
154 const struct GNUNET_NAMESTORE_RecordData *rd,
158 struct NetworkRecord rec;
163 for (i=0;i<rd_count;i++)
165 LOG (GNUNET_ERROR_TYPE_DEBUG,
166 "Serializing record %u with flags %d and expiration time %llu\n",
169 (unsigned long long) rd[i].expiration_time);
170 rec.expiration_time = GNUNET_htonll (rd[i].expiration_time);
171 rec.data_size = htonl ((uint32_t) rd[i].data_size);
172 rec.record_type = htonl (rd[i].record_type);
173 rec.flags = htonl (rd[i].flags);
174 if (off + sizeof (rec) > dest_size)
176 memcpy (&dest[off], &rec, sizeof (rec));
178 if (off + rd[i].data_size > dest_size)
180 memcpy (&dest[off], rd[i].data, rd[i].data_size);
181 off += rd[i].data_size;
188 * Compares if two records are equal (ignoring flags such
189 * as authority, private and pending, but not relative vs.
190 * absolute expiration time).
194 * @return #GNUNET_YES if the records are equal or #GNUNET_NO if they are not
197 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
198 const struct GNUNET_NAMESTORE_RecordData *b)
200 LOG (GNUNET_ERROR_TYPE_DEBUG,
201 "Comparing records\n");
202 if (a->record_type != b->record_type)
204 LOG (GNUNET_ERROR_TYPE_DEBUG,
205 "Record type %lu != %lu\n", a->record_type, b->record_type);
208 if ((a->expiration_time != b->expiration_time) &&
209 ((a->expiration_time != 0) && (b->expiration_time != 0)))
211 LOG (GNUNET_ERROR_TYPE_DEBUG,
212 "Expiration time %llu != %llu\n",
217 if ((a->flags & GNUNET_NAMESTORE_RF_RCMP_FLAGS)
218 != (b->flags & GNUNET_NAMESTORE_RF_RCMP_FLAGS))
220 LOG (GNUNET_ERROR_TYPE_DEBUG,
221 "Flags %lu (%lu) != %lu (%lu)\n", a->flags,
222 a->flags & GNUNET_NAMESTORE_RF_RCMP_FLAGS, b->flags,
223 b->flags & GNUNET_NAMESTORE_RF_RCMP_FLAGS);
226 if (a->data_size != b->data_size)
228 LOG (GNUNET_ERROR_TYPE_DEBUG,
229 "Data size %lu != %lu\n",
234 if (0 != memcmp (a->data, b->data, a->data_size))
236 LOG (GNUNET_ERROR_TYPE_DEBUG,
237 "Data contents do not match\n");
240 LOG (GNUNET_ERROR_TYPE_DEBUG,
241 "Records are equal\n");
247 * Deserialize the given records to the given destination.
249 * @param len size of the serialized record data
250 * @param src the serialized record data
251 * @param rd_count number of records in the rd array
252 * @param dest where to put the data
253 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
256 GNUNET_NAMESTORE_records_deserialize (size_t len,
258 unsigned int rd_count,
259 struct GNUNET_NAMESTORE_RecordData *dest)
261 struct NetworkRecord rec;
266 for (i=0;i<rd_count;i++)
268 if (off + sizeof (rec) > len)
269 return GNUNET_SYSERR;
270 memcpy (&rec, &src[off], sizeof (rec));
271 dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time);
272 dest[i].data_size = ntohl ((uint32_t) rec.data_size);
273 dest[i].record_type = ntohl (rec.record_type);
274 dest[i].flags = ntohl (rec.flags);
276 if (off + dest[i].data_size > len)
277 return GNUNET_SYSERR;
278 dest[i].data = &src[off];
279 off += dest[i].data_size;
280 LOG (GNUNET_ERROR_TYPE_DEBUG,
281 "Deserialized record %u with flags %d and expiration time %llu\n",
284 (unsigned long long) dest[i].expiration_time);
291 * Returns the expiration time of the given block of records. The block
292 * expiration time is the expiration time of the record with smallest
295 * @param rd_count number of records given in @a rd
296 * @param rd array of records
297 * @return absolute expiration time
299 struct GNUNET_TIME_Absolute
300 GNUNET_NAMESTORE_record_get_expiration_time (unsigned int rd_count,
301 const struct GNUNET_NAMESTORE_RecordData *rd)
304 struct GNUNET_TIME_Absolute expire;
305 struct GNUNET_TIME_Absolute at;
306 struct GNUNET_TIME_Relative rt;
309 return GNUNET_TIME_UNIT_ZERO_ABS;
310 expire = GNUNET_TIME_UNIT_FOREVER_ABS;
311 for (c = 0; c < rd_count; c++)
313 if (0 != (rd[c].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
315 rt.rel_value_us = rd[c].expiration_time;
316 at = GNUNET_TIME_relative_to_absolute (rt);
320 at.abs_value_us = rd[c].expiration_time;
322 expire = GNUNET_TIME_absolute_min (at, expire);
324 LOG (GNUNET_ERROR_TYPE_DEBUG,
325 "Determined expiration time for block with %u records to be %s\n",
327 GNUNET_STRINGS_absolute_time_to_string (expire));
333 * Derive session key and iv from label and public key.
335 * @param iv initialization vector to initialize
336 * @param skey session key to initialize
337 * @param label label to use for KDF
338 * @param pub public key to use for KDF
341 derive_block_aes_key (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
342 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
344 const struct GNUNET_CRYPTO_EccPublicSignKey *pub)
346 static const char ctx_key[] = "gns-aes-ctx-key";
347 static const char ctx_iv[] = "gns-aes-ctx-iv";
349 GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
350 pub, sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
351 label, strlen (label),
352 ctx_key, strlen (ctx_key),
354 GNUNET_CRYPTO_kdf (iv, sizeof (struct GNUNET_CRYPTO_SymmetricInitializationVector),
355 pub, sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
356 label, strlen (label),
357 ctx_iv, strlen (ctx_iv),
363 * Sign name and records
365 * @param key the private key
366 * @param expire block expiration
367 * @param label the name for the records
368 * @param rd record data
369 * @param rd_count number of records
370 * @return NULL on error (block too large)
372 struct GNUNET_NAMESTORE_Block *
373 GNUNET_NAMESTORE_block_create (const struct GNUNET_CRYPTO_EccPrivateKey *key,
374 struct GNUNET_TIME_Absolute expire,
376 const struct GNUNET_NAMESTORE_RecordData *rd,
377 unsigned int rd_count)
379 size_t payload_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
380 char payload[sizeof (uint32_t) + payload_len];
381 struct GNUNET_NAMESTORE_Block *block;
382 struct GNUNET_CRYPTO_EccPublicSignKey pkey;
383 struct GNUNET_CRYPTO_EccPrivateKey *dkey;
384 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
385 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
386 uint32_t rd_count_nbo;
388 if (payload_len > GNUNET_NAMESTORE_MAX_VALUE_SIZE)
390 rd_count_nbo = htonl (rd_count);
391 memcpy (payload, &rd_count_nbo, sizeof (uint32_t));
392 GNUNET_assert (payload_len ==
393 GNUNET_NAMESTORE_records_serialize (rd_count, rd,
394 payload_len, &payload[sizeof (uint32_t)]));
395 block = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Block) +
396 sizeof (uint32_t) + payload_len);
397 block->purpose.size = htonl (sizeof (uint32_t) + payload_len +
398 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
399 sizeof (struct GNUNET_TIME_AbsoluteNBO));
400 block->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
401 block->expiration_time = GNUNET_TIME_absolute_hton (expire);
402 dkey = GNUNET_CRYPTO_ecc_key_derive (key,
405 GNUNET_CRYPTO_ecc_key_get_public_for_signature (dkey,
406 &block->derived_key);
407 GNUNET_CRYPTO_ecc_key_get_public_for_signature (key,
409 derive_block_aes_key (&iv, &skey, label, &pkey);
410 GNUNET_break (payload_len + sizeof (uint32_t) ==
411 GNUNET_CRYPTO_symmetric_encrypt (payload, payload_len + sizeof (uint32_t),
415 GNUNET_CRYPTO_ecc_sign (dkey,
430 * Check if a signature is valid. This API is used by the GNS Block
431 * to validate signatures received from the network.
433 * @param block block to verify
434 * @return #GNUNET_OK if the signature is valid
437 GNUNET_NAMESTORE_block_verify (const struct GNUNET_NAMESTORE_Block *block)
439 return GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN,
442 &block->derived_key);
449 * @param block block to decrypt
450 * @param zone_key public key of the zone
451 * @param label the name for the records
452 * @param proc function to call with the result
453 * @param proc_cls closure for proc
454 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was
458 GNUNET_NAMESTORE_block_decrypt (const struct GNUNET_NAMESTORE_Block *block,
459 const struct GNUNET_CRYPTO_EccPublicSignKey *zone_key,
461 GNUNET_NAMESTORE_RecordCallback proc,
464 size_t payload_len = ntohl (block->purpose.size) -
465 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) -
466 sizeof (struct GNUNET_TIME_AbsoluteNBO);
467 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
468 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
470 if (ntohl (block->purpose.size) <
471 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
472 sizeof (struct GNUNET_TIME_AbsoluteNBO))
475 return GNUNET_SYSERR;
477 derive_block_aes_key (&iv, &skey, label, zone_key);
479 char payload[payload_len];
482 GNUNET_break (payload_len ==
483 GNUNET_CRYPTO_symmetric_decrypt (&block[1], payload_len,
489 rd_count = ntohl (rd_count);
492 /* limit to sane value */
494 return GNUNET_SYSERR;
497 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
500 GNUNET_NAMESTORE_records_deserialize (payload_len - sizeof (uint32_t),
501 &payload[sizeof (uint32_t)],
506 return GNUNET_SYSERR;
509 proc (proc_cls, rd_count, (0 != rd_count) ? rd : NULL);
517 * Convert the 'value' of a record to a string.
519 * @param type type of the record
520 * @param data value in binary encoding
521 * @param data_size number of bytes in @a data
522 * @return NULL on error, otherwise human-readable representation of the value
525 GNUNET_NAMESTORE_value_to_string (uint32_t type,
531 char tmp[INET6_ADDRSTRLEN];
537 case GNUNET_DNSPARSER_TYPE_A:
538 if (data_size != sizeof (struct in_addr))
540 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
542 return GNUNET_strdup (tmp);
543 case GNUNET_DNSPARSER_TYPE_NS:
549 ns = GNUNET_DNSPARSER_parse_name (data,
560 case GNUNET_DNSPARSER_TYPE_CNAME:
566 cname = GNUNET_DNSPARSER_parse_name (data,
569 if ( (NULL == cname) ||
577 case GNUNET_DNSPARSER_TYPE_SOA:
579 struct GNUNET_DNSPARSER_SoaRecord *soa;
583 soa = GNUNET_DNSPARSER_parse_soa (data,
586 if ( (NULL == soa) ||
592 GNUNET_asprintf (&result,
593 "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
601 GNUNET_DNSPARSER_free_soa (soa);
604 case GNUNET_DNSPARSER_TYPE_PTR:
610 ptr = GNUNET_DNSPARSER_parse_name (data,
613 if ( (NULL == ptr) ||
621 case GNUNET_DNSPARSER_TYPE_MX:
623 struct GNUNET_DNSPARSER_MxRecord *mx;
627 mx = GNUNET_DNSPARSER_parse_mx (data,
636 GNUNET_asprintf (&result,
640 GNUNET_DNSPARSER_free_mx (mx);
643 case GNUNET_DNSPARSER_TYPE_TXT:
644 return GNUNET_strndup (data, data_size);
645 case GNUNET_DNSPARSER_TYPE_AAAA:
646 if (data_size != sizeof (struct in6_addr))
648 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
650 return GNUNET_strdup (tmp);
651 case GNUNET_NAMESTORE_TYPE_PKEY:
652 if (data_size != sizeof (struct GNUNET_CRYPTO_EccPublicSignKey))
654 return GNUNET_CRYPTO_ecc_public_sign_key_to_string (data);
655 case GNUNET_NAMESTORE_TYPE_PKEY:
657 const struct GNUNET_CONVERSATION_PhoneRecord *pr;
661 if (data_size != sizeof (struct GNUNET_CONVERSATION_PhoneRecord))
664 if (0 != ntohl (pr->version))
666 pkey = GNUNET_CRYPTO_ecc_public_sign_key_to_string (&pr->peer.public_key);
667 GNUNET_asprintf (&ret,
674 case GNUNET_NAMESTORE_TYPE_PSEU:
675 return GNUNET_strndup (data, data_size);
676 case GNUNET_NAMESTORE_TYPE_LEHO:
677 return GNUNET_strndup (data, data_size);
678 case GNUNET_NAMESTORE_TYPE_VPN:
680 const struct GNUNET_TUN_GnsVpnRecord *vpn;
684 if ( (data_size <= sizeof (struct GNUNET_TUN_GnsVpnRecord)) ||
685 ('\0' != cdata[data_size - 1]) )
686 return NULL; /* malformed */
688 if (0 == GNUNET_asprintf (&vpn_str, "%u %s %s",
689 (unsigned int) ntohs (vpn->proto),
690 (const char*) GNUNET_i2s_full (&vpn->peer),
691 (const char*) &vpn[1]))
693 GNUNET_free (vpn_str);
698 case GNUNET_NAMESTORE_TYPE_GNS2DNS:
704 ns = GNUNET_DNSPARSER_parse_name (data,
715 case GNUNET_DNSPARSER_TYPE_SRV:
717 struct GNUNET_DNSPARSER_SrvRecord *srv;
721 srv = GNUNET_DNSPARSER_parse_srv ("+", /* FIXME: is this OK? */
725 if ( (NULL == srv) ||
731 GNUNET_asprintf (&result,
732 "%d %d %d _%s._%s.%s",
739 GNUNET_DNSPARSER_free_srv (srv);
742 case GNUNET_DNSPARSER_TYPE_TLSA:
744 const struct GNUNET_TUN_DnsTlsaRecord *tlsa;
748 if ( (data_size <= sizeof (struct GNUNET_TUN_DnsTlsaRecord)) ||
749 ('\0' != cdata[data_size - 1]) )
750 return NULL; /* malformed */
752 if (0 == GNUNET_asprintf (&tlsa_str,
757 (const char *) &tlsa[1]))
759 GNUNET_free (tlsa_str);
767 GNUNET_break (0); // not implemented
773 * Convert human-readable version of a 'value' of a record to the binary
776 * @param type type of the record
777 * @param s human-readable string
778 * @param data set to value in binary encoding (will be allocated)
779 * @param data_size set to number of bytes in @a data
780 * @return #GNUNET_OK on success
783 GNUNET_NAMESTORE_string_to_value (uint32_t type,
788 struct in_addr value_a;
789 struct in6_addr value_aaaa;
790 struct GNUNET_CRYPTO_EccPublicSignKey pkey;
791 struct GNUNET_TUN_GnsVpnRecord *vpn;
792 struct GNUNET_TUN_DnsTlsaRecord *tlsa;
793 char s_peer[103 + 1];
794 char s_serv[253 + 1];
798 return GNUNET_SYSERR;
802 LOG (GNUNET_ERROR_TYPE_ERROR,
803 _("Unsupported record type %d\n"),
805 return GNUNET_SYSERR;
806 case GNUNET_DNSPARSER_TYPE_A:
807 if (1 != inet_pton (AF_INET, s, &value_a))
809 LOG (GNUNET_ERROR_TYPE_ERROR,
810 _("Unable to parse IPv4 address `%s'\n"),
812 return GNUNET_SYSERR;
814 *data = GNUNET_malloc (sizeof (struct in_addr));
815 memcpy (*data, &value_a, sizeof (value_a));
816 *data_size = sizeof (value_a);
818 case GNUNET_DNSPARSER_TYPE_NS:
825 GNUNET_DNSPARSER_builder_add_name (nsbuf,
830 LOG (GNUNET_ERROR_TYPE_ERROR,
831 _("Failed to serialize NS record with value `%s'\n"),
833 return GNUNET_SYSERR;
836 *data = GNUNET_malloc (off);
837 memcpy (*data, nsbuf, off);
840 case GNUNET_DNSPARSER_TYPE_CNAME:
847 GNUNET_DNSPARSER_builder_add_name (cnamebuf,
852 LOG (GNUNET_ERROR_TYPE_ERROR,
853 _("Failed to serialize CNAME record with value `%s'\n"),
855 return GNUNET_SYSERR;
858 *data = GNUNET_malloc (off);
859 memcpy (*data, cnamebuf, off);
862 case GNUNET_DNSPARSER_TYPE_SOA:
864 struct GNUNET_DNSPARSER_SoaRecord soa;
866 char soa_rname[253 + 1];
867 char soa_mname[253 + 1];
868 unsigned int soa_serial;
869 unsigned int soa_refresh;
870 unsigned int soa_retry;
871 unsigned int soa_expire;
872 unsigned int soa_min;
876 "rname=%253s mname=%253s %u,%u,%u,%u,%u",
877 soa_rname, soa_mname,
878 &soa_serial, &soa_refresh, &soa_retry, &soa_expire, &soa_min))
880 LOG (GNUNET_ERROR_TYPE_ERROR,
881 _("Unable to parse SOA record `%s'\n"),
883 return GNUNET_SYSERR;
885 soa.mname = soa_mname;
886 soa.rname = soa_rname;
887 soa.serial = (uint32_t) soa_serial;
888 soa.refresh =(uint32_t) soa_refresh;
889 soa.retry = (uint32_t) soa_retry;
890 soa.expire = (uint32_t) soa_expire;
891 soa.minimum_ttl = (uint32_t) soa_min;
894 GNUNET_DNSPARSER_builder_add_soa (soabuf,
899 LOG (GNUNET_ERROR_TYPE_ERROR,
900 _("Failed to serialize SOA record with mname `%s' and rname `%s'\n"),
903 return GNUNET_SYSERR;
906 *data = GNUNET_malloc (off);
907 memcpy (*data, soabuf, off);
910 case GNUNET_DNSPARSER_TYPE_PTR:
917 GNUNET_DNSPARSER_builder_add_name (ptrbuf,
922 LOG (GNUNET_ERROR_TYPE_ERROR,
923 _("Failed to serialize PTR record with value `%s'\n"),
925 return GNUNET_SYSERR;
928 *data = GNUNET_malloc (off);
929 memcpy (*data, ptrbuf, off);
932 case GNUNET_DNSPARSER_TYPE_MX:
934 struct GNUNET_DNSPARSER_MxRecord mx;
936 char mxhost[253 + 1];
940 if (2 != SSCANF(s, "%hu,%253s", &mx_pref, mxhost))
942 LOG (GNUNET_ERROR_TYPE_ERROR,
943 _("Unable to parse MX record `%s'\n"),
945 return GNUNET_SYSERR;
947 mx.preference = mx_pref;
952 GNUNET_DNSPARSER_builder_add_mx (mxbuf,
957 LOG (GNUNET_ERROR_TYPE_ERROR,
958 _("Failed to serialize MX record with hostname `%s'\n"),
960 return GNUNET_SYSERR;
963 *data = GNUNET_malloc (off);
964 memcpy (*data, mxbuf, off);
967 case GNUNET_DNSPARSER_TYPE_TXT:
968 *data = GNUNET_strdup (s);
969 *data_size = strlen (s);
971 case GNUNET_DNSPARSER_TYPE_AAAA:
972 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
974 LOG (GNUNET_ERROR_TYPE_ERROR,
975 _("Unable to parse IPv6 address `%s'\n"),
977 return GNUNET_SYSERR;
979 *data = GNUNET_malloc (sizeof (struct in6_addr));
980 *data_size = sizeof (struct in6_addr);
981 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
983 case GNUNET_NAMESTORE_TYPE_PKEY:
985 GNUNET_CRYPTO_ecc_public_sign_key_from_string (s, strlen (s), &pkey))
987 LOG (GNUNET_ERROR_TYPE_ERROR,
988 _("Unable to parse PKEY record `%s'\n"),
990 return GNUNET_SYSERR;
992 *data = GNUNET_new (struct GNUNET_CRYPTO_EccPublicSignKey);
993 memcpy (*data, &pkey, sizeof (pkey));
994 *data_size = sizeof (struct GNUNET_CRYPTO_EccPublicSignKey);
996 case GNUNET_NAMESTORE_TYPE_PHONE:
998 struct GNUNET_CONVERSATION_PhoneRecord *pr;
1001 struct GNUNET_PeerIdentity peer;
1003 if ( (NULL == (dash = strchr (s, "-"))) ||
1004 (1 != sscanf (s, "%u-", &line)) ||
1006 GNUNET_CRYPTO_ecc_public_sign_key_from_string (dash + 1,
1008 &peer.public_key)) )
1010 LOG (GNUNET_ERROR_TYPE_ERROR,
1011 _("Unable to parse PHONE record `%s'\n"),
1013 return GNUNET_SYSERR;
1015 pr = GNUNET_new (struct GNUNET_CONVERSATION_PhoneRecord);
1016 pr->version = htonl (0);
1017 pr->line = htonl ((uint32_t) line);
1020 *data_size = sizeof (struct GNUNET_CONVERSATION_PhoneRecord);
1023 case GNUNET_NAMESTORE_TYPE_PSEU:
1024 *data = GNUNET_strdup (s);
1025 *data_size = strlen (s);
1027 case GNUNET_NAMESTORE_TYPE_LEHO:
1028 *data = GNUNET_strdup (s);
1029 *data_size = strlen (s);
1031 case GNUNET_NAMESTORE_TYPE_VPN:
1032 if (3 != SSCANF (s,"%u %103s %253s",
1033 &proto, s_peer, s_serv))
1035 LOG (GNUNET_ERROR_TYPE_ERROR,
1036 _("Unable to parse VPN record string `%s'\n"),
1038 return GNUNET_SYSERR;
1040 *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
1041 *data = vpn = GNUNET_malloc (*data_size);
1042 if (GNUNET_OK != GNUNET_CRYPTO_ecc_public_sign_key_from_string ((char*) s_peer,
1044 &vpn->peer.public_key))
1048 return GNUNET_SYSERR;
1050 vpn->proto = htons ((uint16_t) proto);
1051 strcpy ((char*)&vpn[1], s_serv);
1053 case GNUNET_NAMESTORE_TYPE_GNS2DNS:
1060 GNUNET_DNSPARSER_builder_add_name (nsbuf,
1065 LOG (GNUNET_ERROR_TYPE_ERROR,
1066 _("Failed to serialize GNS2DNS record with value `%s'\n"),
1068 return GNUNET_SYSERR;
1071 *data = GNUNET_malloc (off);
1072 memcpy (*data, nsbuf, off);
1075 case GNUNET_DNSPARSER_TYPE_TLSA:
1076 *data_size = sizeof (struct GNUNET_TUN_DnsTlsaRecord) + strlen (s) - 6;
1077 *data = tlsa = GNUNET_malloc (*data_size);
1078 if (4 != SSCANF (s, "%c %c %c %s",
1081 &tlsa->matching_type,
1084 LOG (GNUNET_ERROR_TYPE_ERROR,
1085 _("Unable to parse TLSA record string `%s'\n"),
1089 return GNUNET_SYSERR;
1093 LOG (GNUNET_ERROR_TYPE_ERROR,
1094 _("Unsupported record type %d\n"),
1096 return GNUNET_SYSERR;
1102 * Mapping of record type numbers to human-readable
1103 * record type names.
1109 { "A", GNUNET_DNSPARSER_TYPE_A },
1110 { "NS", GNUNET_DNSPARSER_TYPE_NS },
1111 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
1112 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
1113 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
1114 { "MX", GNUNET_DNSPARSER_TYPE_MX },
1115 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
1116 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
1117 { "PKEY", GNUNET_NAMESTORE_TYPE_PKEY },
1118 { "PSEU", GNUNET_NAMESTORE_TYPE_PSEU },
1119 { "LEHO", GNUNET_NAMESTORE_TYPE_LEHO },
1120 { "VPN", GNUNET_NAMESTORE_TYPE_VPN },
1121 { "GNS2DNS", GNUNET_NAMESTORE_TYPE_GNS2DNS },
1122 { "PHONE", GNUNET_NAMESTORE_TYPE_PHONE },
1123 { "TLSA", GNUNET_DNSPARSER_TYPE_TLSA },
1124 { NULL, UINT32_MAX }
1129 * Convert a type name (i.e. "AAAA") to the corresponding number.
1131 * @param typename name to convert
1132 * @return corresponding number, UINT32_MAX on error
1135 GNUNET_NAMESTORE_typename_to_number (const char *dns_typename)
1140 while ( (name_map[i].name != NULL) &&
1141 (0 != strcasecmp (dns_typename, name_map[i].name)) )
1143 return name_map[i].number;
1148 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
1150 * @param type number of a type to convert
1151 * @return corresponding typestring, NULL on error
1154 GNUNET_NAMESTORE_number_to_typename (uint32_t type)
1159 while ( (name_map[i].name != NULL) &&
1160 (type != name_map[i].number) )
1162 return name_map[i].name;
1167 * Test if a given record is expired.
1169 * @return #GNUNET_YES if the record is expired,
1173 GNUNET_NAMESTORE_is_expired (const struct GNUNET_NAMESTORE_RecordData *rd)
1175 struct GNUNET_TIME_Absolute at;
1177 if (0 != (rd->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
1179 at.abs_value_us = rd->expiration_time;
1180 return (0 == GNUNET_TIME_absolute_get_remaining (at).rel_value_us) ? GNUNET_YES : GNUNET_NO;
1185 * Calculate the DHT query for a given @a label in a given @a zone.
1187 * @param zone private key of the zone
1188 * @param label label of the record
1189 * @param query hash to use for the query
1192 GNUNET_NAMESTORE_query_from_private_key (const struct GNUNET_CRYPTO_EccPrivateKey *zone,
1194 struct GNUNET_HashCode *query)
1196 struct GNUNET_CRYPTO_EccPublicSignKey pub;
1198 GNUNET_CRYPTO_ecc_key_get_public_for_signature (zone, &pub);
1199 GNUNET_NAMESTORE_query_from_public_key (&pub, label, query);
1204 * Calculate the DHT query for a given @a label in a given @a zone.
1206 * @param pub public key of the zone
1207 * @param label label of the record
1208 * @param query hash to use for the query
1211 GNUNET_NAMESTORE_query_from_public_key (const struct GNUNET_CRYPTO_EccPublicSignKey *pub,
1213 struct GNUNET_HashCode *query)
1215 struct GNUNET_CRYPTO_EccPublicSignKey pd;
1217 GNUNET_CRYPTO_ecc_public_key_derive (pub, label, "gns", &pd);
1218 GNUNET_CRYPTO_hash (&pd, sizeof (pd), query);
1223 * Convert public key to the respective absolute domain name in the
1225 * This is one of the very few calls in the entire API that is
1228 * @param pkey a public key with a point on the eliptic curve
1229 * @return string "X.zkey" where X is the public
1230 * key in an encoding suitable for DNS labels.
1233 GNUNET_NAMESTORE_pkey_to_zkey (const struct GNUNET_CRYPTO_EccPublicSignKey *pkey)
1235 static char ret[128];
1238 pkeys = GNUNET_CRYPTO_ecc_public_sign_key_to_string (pkey);
1239 GNUNET_snprintf (ret,
1243 GNUNET_free (pkeys);
1249 * Convert an absolute domain name in the ".zkey" pTLD to the
1250 * respective public key.
1252 * @param zkey string "X.zkey" where X is the coordinates of the public
1253 * key in an encoding suitable for DNS labels.
1254 * @param pkey set to a public key on the eliptic curve
1255 * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
1258 GNUNET_NAMESTORE_zkey_to_pkey (const char *zkey,
1259 struct GNUNET_CRYPTO_EccPublicSignKey *pkey)
1265 cpy = GNUNET_strdup (zkey);
1267 if (NULL == (dot = strchr (x, (int) '.')))
1270 if (0 != strcasecmp (dot + 1,
1275 GNUNET_CRYPTO_ecc_public_sign_key_from_string (x,
1283 return GNUNET_SYSERR;
1287 /* end of namestore_common.c */