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 "../dns/dnsparser.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
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 if ((a->record_type == b->record_type) &&
174 (a->expiration_time == b->expiration_time) &&
175 ((a->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)
176 == (b->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION) ) &&
177 (a->data_size == b->data_size) &&
178 (0 == memcmp (a->data, b->data, a->data_size)))
185 * Deserialize the given records to the given destination.
187 * @param len size of the serialized record data
188 * @param src the serialized record data
189 * @param rd_count number of records in the rd array
190 * @param dest where to put the data
192 * @return GNUNET_OK on success, GNUNET_SYSERR on error
195 GNUNET_NAMESTORE_records_deserialize (size_t len,
197 unsigned int rd_count,
198 struct GNUNET_NAMESTORE_RecordData *dest)
200 struct NetworkRecord rec;
205 for (i=0;i<rd_count;i++)
207 if (off + sizeof (rec) > len)
208 return GNUNET_SYSERR;
209 memcpy (&rec, &src[off], sizeof (rec));
210 dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time);
211 dest[i].data_size = ntohl ((uint32_t) rec.data_size);
212 dest[i].record_type = ntohl (rec.record_type);
213 dest[i].flags = ntohl (rec.flags);
216 if (off + dest[i].data_size > len)
217 return GNUNET_SYSERR;
218 dest[i].data = &src[off];
219 off += dest[i].data_size;
226 * Sign name and records
228 * @param key the private key
229 * @param expire block expiration
230 * @param name the name
231 * @param rd record data
232 * @param rd_count number of records
234 * @return the signature
236 struct GNUNET_CRYPTO_RsaSignature *
237 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
238 struct GNUNET_TIME_Absolute expire,
240 const struct GNUNET_NAMESTORE_RecordData *rd,
241 unsigned int rd_count)
243 struct GNUNET_CRYPTO_RsaSignature *sig;
244 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
245 struct GNUNET_TIME_AbsoluteNBO expire_nbo;
248 struct GNUNET_TIME_AbsoluteNBO *expire_tmp;
259 sig = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaSignature));
260 name_len = strlen (name) + 1;
261 expire_nbo = GNUNET_TIME_absolute_hton (expire);
262 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
264 char rd_ser[rd_ser_len];
266 GNUNET_assert (rd_ser_len ==
267 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser));
268 sig_len = sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + sizeof (struct GNUNET_TIME_AbsoluteNBO) + rd_ser_len + name_len;
269 sig_purpose = GNUNET_malloc (sig_len);
270 sig_purpose->size = htonl (sig_len);
271 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
272 expire_tmp = (struct GNUNET_TIME_AbsoluteNBO *) &sig_purpose[1];
273 memcpy (expire_tmp, &expire_nbo, sizeof (struct GNUNET_TIME_AbsoluteNBO));
274 name_tmp = (char *) &expire_tmp[1];
275 memcpy (name_tmp, name, name_len);
276 rd_tmp = &name_tmp[name_len];
277 memcpy (rd_tmp, rd_ser, rd_ser_len);
278 res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
279 GNUNET_free (sig_purpose);
281 if (GNUNET_OK != res)
291 * Checks if a name is wellformed
293 * @param name the name to check
294 * @return GNUNET_OK on success, GNUNET_SYSERR on error
297 GNUNET_NAMESTORE_check_name (const char * name)
300 return GNUNET_SYSERR;
301 if (strlen (name) > 63)
302 return GNUNET_SYSERR;
308 * Convert the 'value' of a record to a string.
310 * @param type type of the record
311 * @param data value in binary encoding
312 * @param data_size number of bytes in data
313 * @return NULL on error, otherwise human-readable representation of the value
316 GNUNET_NAMESTORE_value_to_string (uint32_t type,
320 char tmp[INET6_ADDRSTRLEN];
321 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
326 struct soa_data *soa;
328 struct vpn_data *vpn;
331 struct GNUNET_CRYPTO_HashAsciiEncoded s_peer;
332 struct srv_data *srv;
338 case GNUNET_DNSPARSER_TYPE_A:
339 if (data_size != sizeof (struct in_addr))
341 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
343 return GNUNET_strdup (tmp);
344 case GNUNET_DNSPARSER_TYPE_NS:
345 return GNUNET_strndup (data, data_size);
346 case GNUNET_DNSPARSER_TYPE_CNAME:
347 return GNUNET_strndup (data, data_size);
348 case GNUNET_DNSPARSER_TYPE_SOA:
349 soa = (struct soa_data*)data;
350 soa_rname = (char*)&soa[1];
351 soa_mname = (char*)&soa[1]+strlen(soa_rname)+1;
352 if (GNUNET_asprintf(&result, "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
353 soa_rname, soa_mname,
354 ntohl (soa->serial), ntohl (soa->refresh),
355 ntohl (soa->retry), ntohl (soa->expire), ntohl (soa->minimum)))
359 case GNUNET_DNSPARSER_TYPE_PTR:
360 return GNUNET_strndup (data, data_size);
361 case GNUNET_DNSPARSER_TYPE_MX:
362 mx_pref = ntohs(*((uint16_t*)data));
363 if (GNUNET_asprintf(&result, "%hu,%s", mx_pref, data+sizeof(uint16_t))
368 case GNUNET_DNSPARSER_TYPE_TXT:
369 return GNUNET_strndup (data, data_size);
370 case GNUNET_DNSPARSER_TYPE_AAAA:
371 if (data_size != sizeof (struct in6_addr))
373 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
375 return GNUNET_strdup (tmp);
376 case GNUNET_NAMESTORE_TYPE_PKEY:
377 if (data_size != sizeof (struct GNUNET_CRYPTO_ShortHashCode))
379 GNUNET_CRYPTO_short_hash_to_enc (data,
381 return GNUNET_strdup ((const char*) enc.short_encoding);
382 case GNUNET_NAMESTORE_TYPE_PSEU:
383 return GNUNET_strndup (data, data_size);
384 case GNUNET_NAMESTORE_TYPE_LEHO:
385 return GNUNET_strndup (data, data_size);
386 case GNUNET_NAMESTORE_TYPE_VPN:
387 vpn = (struct vpn_data*)data;
389 GNUNET_CRYPTO_hash_to_enc (&vpn->peer, &s_peer);
390 if (GNUNET_OK != GNUNET_asprintf (&vpn_str, "%hu %s %s",
396 case GNUNET_DNSPARSER_TYPE_SRV:
397 srv = (struct srv_data*)data;
399 if (GNUNET_OK != GNUNET_asprintf (&srv_str, "%d %d %d %s",
409 GNUNET_break (0); // not implemented
415 * Convert human-readable version of a 'value' of a record to the binary
418 * @param type type of the record
419 * @param s human-readable string
420 * @param data set to value in binary encoding (will be allocated)
421 * @param data_size set to number of bytes in data
422 * @return GNUNET_OK on success
425 GNUNET_NAMESTORE_string_to_value (uint32_t type,
430 struct in_addr value_a;
431 struct in6_addr value_aaaa;
432 struct GNUNET_CRYPTO_ShortHashCode pkey;
435 struct soa_data *soa;
440 uint32_t soa_refresh;
446 struct vpn_data* vpn;
453 return GNUNET_SYSERR;
454 case GNUNET_DNSPARSER_TYPE_A:
455 if (1 != inet_pton (AF_INET, s, &value_a))
456 return GNUNET_SYSERR;
457 *data = GNUNET_malloc (sizeof (struct in_addr));
458 memcpy (*data, &value_a, sizeof (value_a));
459 *data_size = sizeof (value_a);
461 case GNUNET_DNSPARSER_TYPE_NS:
462 *data = GNUNET_strdup (s);
463 *data_size = strlen (s);
465 case GNUNET_DNSPARSER_TYPE_CNAME:
466 *data = GNUNET_strdup (s);
467 *data_size = strlen (s);
469 case GNUNET_DNSPARSER_TYPE_SOA:
471 if (SSCANF(s, "rname=%s mname=%s %u,%u,%u,%u,%u",
472 soa_rname, soa_mname,
473 &soa_serial, &soa_refresh, &soa_retry, &soa_expire, &soa_min)
475 return GNUNET_SYSERR;
477 *data_size = sizeof (struct soa_data)+strlen(soa_rname)+strlen(soa_mname)+2;
478 *data = GNUNET_malloc (*data_size);
479 soa = (struct soa_data*)*data;
480 soa->serial = htonl(soa_serial);
481 soa->refresh = htonl(soa_refresh);
482 soa->retry = htonl(soa_retry);
483 soa->expire = htonl(soa_expire);
484 soa->minimum = htonl(soa_min);
485 strcpy((char*)&soa[1], soa_rname);
486 strcpy((char*)&soa[1]+strlen(*data)+1, soa_mname);
489 case GNUNET_DNSPARSER_TYPE_PTR:
490 *data = GNUNET_strdup (s);
491 *data_size = strlen (s);
493 case GNUNET_DNSPARSER_TYPE_MX:
494 if (SSCANF(s, "%hu,%s", &mx_pref, result) != 2)
495 return GNUNET_SYSERR;
496 *data_size = sizeof (uint16_t)+strlen(result)+1;
497 *data = GNUNET_malloc (*data_size);
498 mx_pref_n = htons(mx_pref);
499 memcpy(*data, &mx_pref_n, sizeof (uint16_t));
500 strcpy((*data)+sizeof (uint16_t), result);
502 case GNUNET_DNSPARSER_TYPE_TXT:
503 *data = GNUNET_strdup (s);
504 *data_size = strlen (s);
506 case GNUNET_DNSPARSER_TYPE_AAAA:
507 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
508 return GNUNET_SYSERR;
509 *data = GNUNET_malloc (sizeof (struct in6_addr));
510 *data_size = sizeof (struct in6_addr);
511 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
513 case GNUNET_NAMESTORE_TYPE_PKEY:
515 GNUNET_CRYPTO_short_hash_from_string (s, &pkey))
516 return GNUNET_SYSERR;
517 *data = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
518 memcpy (*data, &pkey, sizeof (pkey));
519 *data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
521 case GNUNET_NAMESTORE_TYPE_PSEU:
522 *data = GNUNET_strdup (s);
523 *data_size = strlen (s);
525 case GNUNET_NAMESTORE_TYPE_LEHO:
526 *data = GNUNET_strdup (s);
527 *data_size = strlen (s);
529 case GNUNET_NAMESTORE_TYPE_VPN:
531 ret = SSCANF (s,"%hu %s %s",
532 &proto, s_peer, s_serv);
535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
536 "Unable to parse VPN record string %s %d\n", s, ret);
537 return GNUNET_SYSERR;
539 *data_size = sizeof (struct vpn_data) + strlen (s_serv) + 1;
541 *data = GNUNET_malloc (*data_size);
543 vpn = (struct vpn_data*)*data;
545 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_peer, &vpn->peer))
548 return GNUNET_SYSERR;
551 vpn->proto = htons (proto);
552 strcpy ((char*)&vpn[1], s_serv);
557 return GNUNET_SYSERR;
565 { "A", GNUNET_DNSPARSER_TYPE_A },
566 { "NS", GNUNET_DNSPARSER_TYPE_NS },
567 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
568 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
569 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
570 { "MX", GNUNET_DNSPARSER_TYPE_MX },
571 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
572 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
573 { "PKEY", GNUNET_NAMESTORE_TYPE_PKEY },
574 { "PSEU", GNUNET_NAMESTORE_TYPE_PSEU },
575 { "LEHO", GNUNET_NAMESTORE_TYPE_LEHO },
581 * Convert a type name (i.e. "AAAA") to the corresponding number.
583 * @param typename name to convert
584 * @return corresponding number, UINT32_MAX on error
587 GNUNET_NAMESTORE_typename_to_number (const char *typename)
592 while ( (name_map[i].name != NULL) &&
593 (0 != strcasecmp (typename, name_map[i].name)) )
595 return name_map[i].number;
600 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
602 * @param type number of a type to convert
603 * @return corresponding typestring, NULL on error
606 GNUNET_NAMESTORE_number_to_typename (uint32_t type)
611 while ( (name_map[i].name != NULL) &&
612 (type != name_map[i].number) )
614 return name_map[i].name;
619 /* end of namestore_common.c */