2 This file is part of GNUnet.
3 (C) 2009, 2010, 2012 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_common.c
23 * @brief API to access the NAMESTORE service
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
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 "gns_protocol.h"
36 #include "namestore.h"
39 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
41 GNUNET_NETWORK_STRUCT_BEGIN
44 * Internal format of a record in the serialized form.
50 * Expiration time for the DNS record; relative or absolute depends
51 * on 'flags', network byte order.
53 uint64_t expiration_time GNUNET_PACKED;
56 * Number of bytes in 'data', network byte order.
58 uint32_t data_size GNUNET_PACKED;
61 * Type of the GNS/DNS record, network byte order.
63 uint32_t record_type GNUNET_PACKED;
66 * Flags for the record, network byte order.
68 uint32_t flags GNUNET_PACKED;
72 GNUNET_NETWORK_STRUCT_END
76 * Convert a short hash to a string (for printing debug messages).
77 * This is one of the very few calls in the entire API that is
80 * @param hc the short hash code
81 * @return string form; will be overwritten by next call to GNUNET_h2s.
84 GNUNET_short_h2s (const struct GNUNET_CRYPTO_ShortHashCode * hc)
86 static struct GNUNET_CRYPTO_ShortHashAsciiEncoded ret;
88 GNUNET_CRYPTO_short_hash_to_enc (hc, &ret);
89 return (const char *) &ret;
94 * Calculate how many bytes we will need to serialize the given
97 * @param rd_count number of records in the rd array
98 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
100 * @return the required size to serialize
104 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
105 const struct GNUNET_NAMESTORE_RecordData *rd)
110 ret = sizeof (struct NetworkRecord) * rd_count;
111 for (i=0;i<rd_count;i++)
113 GNUNET_assert ((ret + rd[i].data_size) >= ret);
114 ret += rd[i].data_size;
121 * Serialize the given records to the given destination buffer.
123 * @param rd_count number of records in the rd array
124 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
125 * @param dest_size size of the destination array
126 * @param dest where to write the result
128 * @return the size of serialized records, -1 if records do not fit
131 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
132 const struct GNUNET_NAMESTORE_RecordData *rd,
136 struct NetworkRecord rec;
141 for (i=0;i<rd_count;i++)
143 rec.expiration_time = GNUNET_htonll (rd[i].expiration_time);
144 rec.data_size = htonl ((uint32_t) rd[i].data_size);
145 rec.record_type = htonl (rd[i].record_type);
146 rec.flags = htonl (rd[i].flags);
147 if (off + sizeof (rec) > dest_size)
149 memcpy (&dest[off], &rec, sizeof (rec));
151 if (off + rd[i].data_size > dest_size)
153 memcpy (&dest[off], rd[i].data, rd[i].data_size);
154 off += rd[i].data_size;
161 * Compares if two records are equal (ignoring flags such
162 * as authority, private and pending, but not relative vs.
163 * absolute expiration time).
167 * @return GNUNET_YES if the records are equal or GNUNET_NO if they are not
170 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
171 const struct GNUNET_NAMESTORE_RecordData *b)
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174 "Comparing records\n");
175 if (a->record_type != b->record_type)
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "Record type %lu != %lu\n", a->record_type, b->record_type);
181 if ((a->expiration_time != b->expiration_time) &&
182 ((a->expiration_time != 0) && (b->expiration_time != 0)))
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 "Expiration time %llu != %llu\n", a->expiration_time, b->expiration_time);
188 if ((a->flags & GNUNET_NAMESTORE_RF_RCMP_FLAGS)
189 != (b->flags & GNUNET_NAMESTORE_RF_RCMP_FLAGS))
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "Flags %lu (%lu) != %lu (%lu)\n", a->flags,
193 a->flags & GNUNET_NAMESTORE_RF_RCMP_FLAGS, b->flags,
194 b->flags & GNUNET_NAMESTORE_RF_RCMP_FLAGS);
197 if (a->data_size != b->data_size)
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Data size %lu != %lu\n", a->data_size, b->data_size);
203 if (0 != memcmp (a->data, b->data, a->data_size))
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Data contents do not match\n");
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 "Records are equal\n");
216 * Deserialize the given records to the given destination.
218 * @param len size of the serialized record data
219 * @param src the serialized record data
220 * @param rd_count number of records in the rd array
221 * @param dest where to put the data
223 * @return GNUNET_OK on success, GNUNET_SYSERR on error
226 GNUNET_NAMESTORE_records_deserialize (size_t len,
228 unsigned int rd_count,
229 struct GNUNET_NAMESTORE_RecordData *dest)
231 struct NetworkRecord rec;
236 for (i=0;i<rd_count;i++)
238 if (off + sizeof (rec) > len)
239 return GNUNET_SYSERR;
240 memcpy (&rec, &src[off], sizeof (rec));
241 dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time);
242 dest[i].data_size = ntohl ((uint32_t) rec.data_size);
243 dest[i].record_type = ntohl (rec.record_type);
244 dest[i].flags = ntohl (rec.flags);
247 if (off + dest[i].data_size > len)
248 return GNUNET_SYSERR;
249 dest[i].data = &src[off];
250 off += dest[i].data_size;
257 * Sign name and records
259 * @param key the private key
260 * @param expire block expiration
261 * @param name the name
262 * @param rd record data
263 * @param rd_count number of records
265 * @return the signature
267 struct GNUNET_CRYPTO_RsaSignature *
268 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
269 struct GNUNET_TIME_Absolute expire,
271 const struct GNUNET_NAMESTORE_RecordData *rd,
272 unsigned int rd_count)
274 struct GNUNET_CRYPTO_RsaSignature *sig;
275 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
276 struct GNUNET_TIME_AbsoluteNBO expire_nbo;
279 struct GNUNET_TIME_AbsoluteNBO *expire_tmp;
290 sig = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaSignature));
291 name_len = strlen (name) + 1;
292 expire_nbo = GNUNET_TIME_absolute_hton (expire);
293 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
295 char rd_ser[rd_ser_len];
297 GNUNET_assert (rd_ser_len ==
298 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser));
299 sig_len = sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + sizeof (struct GNUNET_TIME_AbsoluteNBO) + rd_ser_len + name_len;
300 sig_purpose = GNUNET_malloc (sig_len);
301 sig_purpose->size = htonl (sig_len);
302 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
303 expire_tmp = (struct GNUNET_TIME_AbsoluteNBO *) &sig_purpose[1];
304 memcpy (expire_tmp, &expire_nbo, sizeof (struct GNUNET_TIME_AbsoluteNBO));
305 name_tmp = (char *) &expire_tmp[1];
306 memcpy (name_tmp, name, name_len);
307 rd_tmp = &name_tmp[name_len];
308 memcpy (rd_tmp, rd_ser, rd_ser_len);
309 res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
310 GNUNET_free (sig_purpose);
312 if (GNUNET_OK != res)
322 * Checks if a name is wellformed
324 * @param name the name to check
325 * @return GNUNET_OK on success, GNUNET_SYSERR on error
328 GNUNET_NAMESTORE_check_name (const char * name)
331 return GNUNET_SYSERR;
332 if (strlen (name) > 63)
333 return GNUNET_SYSERR;
339 * Convert the 'value' of a record to a string.
341 * @param type type of the record
342 * @param data value in binary encoding
343 * @param data_size number of bytes in data
344 * @return NULL on error, otherwise human-readable representation of the value
347 GNUNET_NAMESTORE_value_to_string (uint32_t type,
352 struct soa_data *soa;
353 struct vpn_data *vpn;
354 struct srv_data *srv;
355 struct tlsa_data *tlsa;
356 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
357 struct GNUNET_CRYPTO_HashAsciiEncoded s_peer;
364 char tmp[INET6_ADDRSTRLEN];
370 case GNUNET_DNSPARSER_TYPE_A:
371 if (data_size != sizeof (struct in_addr))
373 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
375 return GNUNET_strdup (tmp);
376 case GNUNET_DNSPARSER_TYPE_NS:
377 return GNUNET_strdup (data);
378 case GNUNET_DNSPARSER_TYPE_CNAME:
379 return GNUNET_strdup (data);
380 case GNUNET_DNSPARSER_TYPE_SOA:
381 soa = (struct soa_data*)data;
382 soa_rname = (char*)&soa[1];
383 soa_mname = (char*)&soa[1]+strlen(soa_rname)+1;
384 if (0 == GNUNET_asprintf(&result, "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
385 soa_rname, soa_mname,
386 ntohl (soa->serial), ntohl (soa->refresh),
387 ntohl (soa->retry), ntohl (soa->expire), ntohl (soa->minimum)))
389 GNUNET_free (result);
393 case GNUNET_DNSPARSER_TYPE_PTR:
394 return GNUNET_strndup (data, data_size);
395 case GNUNET_DNSPARSER_TYPE_MX:
396 mx_pref = ntohs(*((uint16_t*)data));
397 if (GNUNET_asprintf(&result, "%hu,%s", mx_pref, data+sizeof(uint16_t))
402 GNUNET_free (result);
405 case GNUNET_DNSPARSER_TYPE_TXT:
406 return GNUNET_strndup (data, data_size);
407 case GNUNET_DNSPARSER_TYPE_AAAA:
408 if (data_size != sizeof (struct in6_addr))
410 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
412 return GNUNET_strdup (tmp);
413 case GNUNET_NAMESTORE_TYPE_PKEY:
414 if (data_size != sizeof (struct GNUNET_CRYPTO_ShortHashCode))
416 GNUNET_CRYPTO_short_hash_to_enc (data,
418 return GNUNET_strdup ((const char*) enc.short_encoding);
419 case GNUNET_NAMESTORE_TYPE_PSEU:
420 return GNUNET_strndup (data, data_size);
421 case GNUNET_NAMESTORE_TYPE_LEHO:
422 return GNUNET_strndup (data, data_size);
423 case GNUNET_NAMESTORE_TYPE_VPN:
424 vpn = (struct vpn_data*)data;
426 GNUNET_CRYPTO_hash_to_enc (&vpn->peer, &s_peer);
427 if (0 == GNUNET_asprintf (&vpn_str, "%hu %s %s",
432 GNUNET_free (vpn_str);
436 case GNUNET_DNSPARSER_TYPE_SRV:
437 srv = (struct srv_data*)data;
439 if (0 == GNUNET_asprintf (&srv_str, "%d %d %d %s",
445 GNUNET_free (srv_str);
449 case GNUNET_DNSPARSER_TYPE_TLSA:
450 tlsa = (struct tlsa_data*)data;
452 if (0 == GNUNET_asprintf (&tlsa_str, "%c %c %c %s",
458 GNUNET_free (tlsa_str);
465 GNUNET_break (0); // not implemented
471 * Convert human-readable version of a 'value' of a record to the binary
474 * @param type type of the record
475 * @param s human-readable string
476 * @param data set to value in binary encoding (will be allocated)
477 * @param data_size set to number of bytes in data
478 * @return GNUNET_OK on success
481 GNUNET_NAMESTORE_string_to_value (uint32_t type,
486 struct in_addr value_a;
487 struct in6_addr value_aaaa;
488 struct GNUNET_CRYPTO_ShortHashCode pkey;
489 struct soa_data *soa;
490 struct vpn_data *vpn;
491 struct tlsa_data *tlsa;
498 uint32_t soa_refresh;
510 return GNUNET_SYSERR;
511 case GNUNET_DNSPARSER_TYPE_A:
512 if (1 != inet_pton (AF_INET, s, &value_a))
513 return GNUNET_SYSERR;
514 *data = GNUNET_malloc (sizeof (struct in_addr));
515 memcpy (*data, &value_a, sizeof (value_a));
516 *data_size = sizeof (value_a);
518 case GNUNET_DNSPARSER_TYPE_NS:
519 *data = GNUNET_strdup (s);
520 *data_size = strlen (s) + 1;
522 case GNUNET_DNSPARSER_TYPE_CNAME:
523 *data = GNUNET_strdup (s);
524 *data_size = strlen (s) + 1;
526 case GNUNET_DNSPARSER_TYPE_SOA:
528 if (SSCANF(s, "rname=%s mname=%s %u,%u,%u,%u,%u",
529 soa_rname, soa_mname,
530 &soa_serial, &soa_refresh, &soa_retry, &soa_expire, &soa_min)
532 return GNUNET_SYSERR;
534 *data_size = sizeof (struct soa_data)+strlen(soa_rname)+strlen(soa_mname)+2;
535 *data = GNUNET_malloc (*data_size);
536 soa = (struct soa_data*)*data;
537 soa->serial = htonl(soa_serial);
538 soa->refresh = htonl(soa_refresh);
539 soa->retry = htonl(soa_retry);
540 soa->expire = htonl(soa_expire);
541 soa->minimum = htonl(soa_min);
542 strcpy((char*)&soa[1], soa_rname);
543 strcpy((char*)&soa[1]+strlen(*data)+1, soa_mname);
546 case GNUNET_DNSPARSER_TYPE_PTR:
547 *data = GNUNET_strdup (s);
548 *data_size = strlen (s);
550 case GNUNET_DNSPARSER_TYPE_MX:
551 if (SSCANF(s, "%hu,%s", &mx_pref, result) != 2)
552 return GNUNET_SYSERR;
553 *data_size = sizeof (uint16_t)+strlen(result)+1;
554 *data = GNUNET_malloc (*data_size);
555 mx_pref_n = htons(mx_pref);
556 memcpy(*data, &mx_pref_n, sizeof (uint16_t));
557 strcpy((*data)+sizeof (uint16_t), result);
559 case GNUNET_DNSPARSER_TYPE_TXT:
560 *data = GNUNET_strdup (s);
561 *data_size = strlen (s);
563 case GNUNET_DNSPARSER_TYPE_AAAA:
564 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
565 return GNUNET_SYSERR;
566 *data = GNUNET_malloc (sizeof (struct in6_addr));
567 *data_size = sizeof (struct in6_addr);
568 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
570 case GNUNET_NAMESTORE_TYPE_PKEY:
572 GNUNET_CRYPTO_short_hash_from_string (s, &pkey))
573 return GNUNET_SYSERR;
574 *data = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
575 memcpy (*data, &pkey, sizeof (pkey));
576 *data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
578 case GNUNET_NAMESTORE_TYPE_PSEU:
579 *data = GNUNET_strdup (s);
580 *data_size = strlen (s);
582 case GNUNET_NAMESTORE_TYPE_LEHO:
583 *data = GNUNET_strdup (s);
584 *data_size = strlen (s);
586 case GNUNET_NAMESTORE_TYPE_VPN:
588 ret = SSCANF (s,"%hu %s %s",
589 &proto, s_peer, s_serv);
592 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
593 "Unable to parse VPN record string %s %d\n", s, ret);
594 return GNUNET_SYSERR;
596 *data_size = sizeof (struct vpn_data) + strlen (s_serv) + 1;
597 *data = GNUNET_malloc (*data_size);
599 vpn = (struct vpn_data*)*data;
601 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_peer, &vpn->peer))
604 return GNUNET_SYSERR;
607 vpn->proto = htons (proto);
608 strcpy ((char*)&vpn[1], s_serv);
610 case GNUNET_DNSPARSER_TYPE_TLSA:
611 *data_size = sizeof (struct tlsa_data) + strlen (s) - 6;
612 *data = GNUNET_malloc (*data_size);
613 tlsa = (struct tlsa_data*)*data;
614 ret = SSCANF (s, "%c %c %c %s",
617 &tlsa->matching_type,
622 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
623 "Unable to parse TLSA record string %s\n", s);
626 return GNUNET_SYSERR;
632 return GNUNET_SYSERR;
640 { "A", GNUNET_DNSPARSER_TYPE_A },
641 { "NS", GNUNET_DNSPARSER_TYPE_NS },
642 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
643 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
644 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
645 { "MX", GNUNET_DNSPARSER_TYPE_MX },
646 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
647 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
648 { "PKEY", GNUNET_NAMESTORE_TYPE_PKEY },
649 { "PSEU", GNUNET_NAMESTORE_TYPE_PSEU },
650 { "LEHO", GNUNET_NAMESTORE_TYPE_LEHO },
651 { "VPN", GNUNET_NAMESTORE_TYPE_VPN },
652 { "TLSA", GNUNET_DNSPARSER_TYPE_TLSA },
658 * Convert a type name (i.e. "AAAA") to the corresponding number.
660 * @param typename name to convert
661 * @return corresponding number, UINT32_MAX on error
664 GNUNET_NAMESTORE_typename_to_number (const char *typename)
669 while ( (name_map[i].name != NULL) &&
670 (0 != strcasecmp (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_NAMESTORE_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;
695 * Test if a given record is expired.
697 * @return GNUNET_YES if the record is expired,
701 GNUNET_NAMESTORE_is_expired (const struct GNUNET_NAMESTORE_RecordData *rd)
703 struct GNUNET_TIME_Absolute at;
705 if (0 != (rd->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
707 at.abs_value = rd->expiration_time;
708 return (0 == GNUNET_TIME_absolute_get_remaining (at).rel_value) ? GNUNET_YES : GNUNET_NO;
712 /* end of namestore_common.c */