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 "namestore.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
40 GNUNET_NETWORK_STRUCT_BEGIN
43 * Internal format of a record in the serialized form.
49 * Expiration time for the DNS record; relative or absolute depends
50 * on 'flags', network byte order.
52 uint64_t expiration_time GNUNET_PACKED;
55 * Number of bytes in 'data', network byte order.
57 uint32_t data_size GNUNET_PACKED;
60 * Type of the GNS/DNS record, network byte order.
62 uint32_t record_type GNUNET_PACKED;
65 * Flags for the record, network byte order.
67 uint32_t flags GNUNET_PACKED;
71 GNUNET_NETWORK_STRUCT_END
75 * Convert a short hash to a string (for printing debug messages).
76 * This is one of the very few calls in the entire API that is
79 * @param hc the short hash code
80 * @return string form; will be overwritten by next call to GNUNET_h2s.
83 GNUNET_short_h2s (const struct GNUNET_CRYPTO_ShortHashCode * hc)
85 static struct GNUNET_CRYPTO_ShortHashAsciiEncoded ret;
87 GNUNET_CRYPTO_short_hash_to_enc (hc, &ret);
88 return (const char *) &ret;
93 * Calculate how many bytes we will need to serialize the given
96 * @param rd_count number of records in the rd array
97 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
99 * @return the required size to serialize
103 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
104 const struct GNUNET_NAMESTORE_RecordData *rd)
109 ret = sizeof (struct NetworkRecord) * rd_count;
110 for (i=0;i<rd_count;i++)
112 GNUNET_assert ((ret + rd[i].data_size) >= ret);
113 ret += rd[i].data_size;
120 * Serialize the given records to the given destination buffer.
122 * @param rd_count number of records in the rd array
123 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
124 * @param dest_size size of the destination array
125 * @param dest where to write the result
127 * @return the size of serialized records
130 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
131 const struct GNUNET_NAMESTORE_RecordData *rd,
135 struct NetworkRecord rec;
140 for (i=0;i<rd_count;i++)
142 rec.expiration_time = GNUNET_htonll (rd[i].expiration_time);
143 rec.data_size = htonl ((uint32_t) rd[i].data_size);
144 rec.record_type = htonl (rd[i].record_type);
145 rec.flags = htonl (rd[i].flags);
146 if (off + sizeof (rec) > dest_size)
148 memcpy (&dest[off], &rec, sizeof (rec));
150 if (off + rd[i].data_size > dest_size)
152 memcpy (&dest[off], rd[i].data, rd[i].data_size);
153 off += rd[i].data_size;
160 * Compares if two records are equal (ignoring flags such
161 * as authority, private and pending, but not relative vs.
162 * absolute expiration time).
166 * @return GNUNET_YES if the records are equal or GNUNET_NO if they are not
169 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
170 const struct GNUNET_NAMESTORE_RecordData *b)
172 if ((a->record_type == b->record_type) &&
173 (a->expiration_time == b->expiration_time) &&
174 ((a->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)
175 == (b->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION) ) &&
176 (a->data_size == b->data_size) &&
177 (0 == memcmp (a->data, b->data, a->data_size)))
184 * Deserialize the given records to the given destination.
186 * @param len size of the serialized record data
187 * @param src the serialized record data
188 * @param rd_count number of records in the rd array
189 * @param dest where to put the data
191 * @return GNUNET_OK on success, GNUNET_SYSERR on error
194 GNUNET_NAMESTORE_records_deserialize (size_t len,
196 unsigned int rd_count,
197 struct GNUNET_NAMESTORE_RecordData *dest)
199 struct NetworkRecord rec;
204 for (i=0;i<rd_count;i++)
206 if (off + sizeof (rec) > len)
207 return GNUNET_SYSERR;
208 memcpy (&rec, &src[off], sizeof (rec));
209 dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time);
210 dest[i].data_size = ntohl ((uint32_t) rec.data_size);
211 dest[i].record_type = ntohl (rec.record_type);
212 dest[i].flags = ntohl (rec.flags);
215 if (off + dest[i].data_size > len)
216 return GNUNET_SYSERR;
217 dest[i].data = &src[off];
218 off += dest[i].data_size;
225 * Sign name and records
227 * @param key the private key
228 * @param expire block expiration
229 * @param name the name
230 * @param rd record data
231 * @param rd_count number of records
233 * @return the signature
235 struct GNUNET_CRYPTO_RsaSignature *
236 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
237 struct GNUNET_TIME_Absolute expire,
239 const struct GNUNET_NAMESTORE_RecordData *rd,
240 unsigned int rd_count)
242 struct GNUNET_CRYPTO_RsaSignature *sig;
243 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
244 struct GNUNET_TIME_AbsoluteNBO expire_nbo;
247 struct GNUNET_TIME_AbsoluteNBO *expire_tmp;
258 sig = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaSignature));
259 name_len = strlen (name) + 1;
260 expire_nbo = GNUNET_TIME_absolute_hton (expire);
261 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
263 char rd_ser[rd_ser_len];
265 GNUNET_assert (rd_ser_len ==
266 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser));
267 sig_len = sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + sizeof (struct GNUNET_TIME_AbsoluteNBO) + rd_ser_len + name_len;
268 sig_purpose = GNUNET_malloc (sig_len);
269 sig_purpose->size = htonl (sig_len);
270 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
271 expire_tmp = (struct GNUNET_TIME_AbsoluteNBO *) &sig_purpose[1];
272 memcpy (expire_tmp, &expire_nbo, sizeof (struct GNUNET_TIME_AbsoluteNBO));
273 name_tmp = (char *) &expire_tmp[1];
274 memcpy (name_tmp, name, name_len);
275 rd_tmp = &name_tmp[name_len];
276 memcpy (rd_tmp, rd_ser, rd_ser_len);
277 res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
278 GNUNET_free (sig_purpose);
280 if (GNUNET_OK != res)
290 * Checks if a name is wellformed
292 * @param name the name to check
293 * @return GNUNET_OK on success, GNUNET_SYSERR on error
296 GNUNET_NAMESTORE_check_name (const char * name)
299 return GNUNET_SYSERR;
300 if (strlen (name) > 63)
301 return GNUNET_SYSERR;
307 * Convert the 'value' of a record to a string.
309 * @param type type of the record
310 * @param data value in binary encoding
311 * @param data_size number of bytes in data
312 * @return NULL on error, otherwise human-readable representation of the value
315 GNUNET_NAMESTORE_value_to_string (uint32_t type,
319 char tmp[INET6_ADDRSTRLEN];
320 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
327 uint32_t soa_refresh;
334 struct GNUNET_HashCode *h_peer;
335 struct GNUNET_HashCode *h_serv;
336 struct GNUNET_CRYPTO_HashAsciiEncoded s_peer;
337 struct GNUNET_CRYPTO_HashAsciiEncoded s_serv;
343 case GNUNET_DNSPARSER_TYPE_A:
344 if (data_size != sizeof (struct in_addr))
346 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
348 return GNUNET_strdup (tmp);
349 case GNUNET_DNSPARSER_TYPE_NS:
350 return GNUNET_strndup (data, data_size);
351 case GNUNET_DNSPARSER_TYPE_CNAME:
352 return GNUNET_strndup (data, data_size);
353 case GNUNET_DNSPARSER_TYPE_SOA:
354 soa_rname = (char*)data;
355 soa_mname = (char*)data+strlen(soa_rname)+1;
356 soa_data = (uint32_t*)(soa_mname+strlen(soa_mname)+1);
357 soa_serial = ntohl(soa_data[0]);
358 soa_refresh = ntohl(soa_data[1]);
359 soa_retry = ntohl(soa_data[2]);
360 soa_expire = ntohl(soa_data[3]);
361 soa_min = ntohl(soa_data[4]);
362 if (GNUNET_asprintf(&result, "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
363 soa_rname, soa_mname,
364 soa_serial, soa_refresh, soa_retry, soa_expire, soa_min))
368 case GNUNET_DNSPARSER_TYPE_PTR:
369 return GNUNET_strndup (data, data_size);
370 case GNUNET_DNSPARSER_TYPE_MX:
371 mx_pref = ntohs(*((uint16_t*)data));
372 if (GNUNET_asprintf(&result, "%hu,%s", mx_pref, data+sizeof(uint16_t))
377 case GNUNET_DNSPARSER_TYPE_TXT:
378 return GNUNET_strndup (data, data_size);
379 case GNUNET_DNSPARSER_TYPE_AAAA:
380 if (data_size != sizeof (struct in6_addr))
382 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
384 return GNUNET_strdup (tmp);
385 case GNUNET_NAMESTORE_TYPE_PKEY:
386 if (data_size != sizeof (struct GNUNET_CRYPTO_ShortHashCode))
388 GNUNET_CRYPTO_short_hash_to_enc (data,
390 return GNUNET_strdup ((const char*) enc.short_encoding);
391 case GNUNET_NAMESTORE_TYPE_PSEU:
392 return GNUNET_strndup (data, data_size);
393 case GNUNET_NAMESTORE_TYPE_LEHO:
394 return GNUNET_strndup (data, data_size);
395 case GNUNET_NAMESTORE_TYPE_VPN:
396 af = (uint32_t*)data;
397 proto = (uint32_t*)((char*)af + sizeof (uint32_t));
398 h_peer = (struct GNUNET_HashCode*)((char*)proto + sizeof (struct GNUNET_HashCode));
399 h_serv = (struct GNUNET_HashCode*)((char*)h_peer + sizeof (struct GNUNET_HashCode));
401 GNUNET_CRYPTO_hash_to_enc (h_peer, &s_peer);
402 GNUNET_CRYPTO_hash_to_enc (h_serv, &s_serv);
403 if (GNUNET_OK != GNUNET_asprintf (&vpn_str, "%d:%d:%s:%s", af, proto, (char*)&s_peer, (char*)&s_serv))
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 uint32_t soa_data[5];
440 uint32_t soa_refresh;
444 struct GNUNET_HashCode *h_peer;
445 struct GNUNET_HashCode *h_serv;
446 struct GNUNET_CRYPTO_HashAsciiEncoded s_peer;
447 struct GNUNET_CRYPTO_HashAsciiEncoded s_serv;
454 return GNUNET_SYSERR;
455 case GNUNET_DNSPARSER_TYPE_A:
456 if (1 != inet_pton (AF_INET, s, &value_a))
457 return GNUNET_SYSERR;
458 *data = GNUNET_malloc (sizeof (struct in_addr));
459 memcpy (*data, &value_a, sizeof (value_a));
460 *data_size = sizeof (value_a);
462 case GNUNET_DNSPARSER_TYPE_NS:
463 *data = GNUNET_strdup (s);
464 *data_size = strlen (s);
466 case GNUNET_DNSPARSER_TYPE_CNAME:
467 *data = GNUNET_strdup (s);
468 *data_size = strlen (s);
470 case GNUNET_DNSPARSER_TYPE_SOA:
472 if (SSCANF(s, "rname=%s mname=%s %u,%u,%u,%u,%u",
473 soa_rname, soa_mname,
474 &soa_serial, &soa_refresh, &soa_retry, &soa_expire, &soa_min)
476 return GNUNET_SYSERR;
478 *data_size = sizeof (soa_data)+strlen(soa_rname)+strlen(soa_mname)+2;
479 *data = GNUNET_malloc (*data_size);
480 soa_data[0] = htonl(soa_serial);
481 soa_data[1] = htonl(soa_refresh);
482 soa_data[2] = htonl(soa_retry);
483 soa_data[3] = htonl(soa_expire);
484 soa_data[4] = htonl(soa_min);
485 strcpy(*data, soa_rname);
486 strcpy(*data+strlen(*data)+1, soa_mname);
487 memcpy(*data+strlen(*data)+1+strlen(soa_mname)+1,
488 soa_data, sizeof(soa_data));
491 case GNUNET_DNSPARSER_TYPE_PTR:
492 *data = GNUNET_strdup (s);
493 *data_size = strlen (s);
495 case GNUNET_DNSPARSER_TYPE_MX:
496 if (SSCANF(s, "%hu,%s", &mx_pref, result) != 2)
497 return GNUNET_SYSERR;
498 *data_size = sizeof (uint16_t)+strlen(result)+1;
499 *data = GNUNET_malloc (*data_size);
500 mx_pref_n = htons(mx_pref);
501 memcpy(*data, &mx_pref_n, sizeof (uint16_t));
502 strcpy((*data)+sizeof (uint16_t), result);
504 case GNUNET_DNSPARSER_TYPE_TXT:
505 *data = GNUNET_strdup (s);
506 *data_size = strlen (s);
508 case GNUNET_DNSPARSER_TYPE_AAAA:
509 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
510 return GNUNET_SYSERR;
511 *data = GNUNET_malloc (sizeof (struct in6_addr));
512 *data_size = sizeof (struct in6_addr);
513 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
515 case GNUNET_NAMESTORE_TYPE_PKEY:
517 GNUNET_CRYPTO_short_hash_from_string (s, &pkey))
518 return GNUNET_SYSERR;
519 *data = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
520 memcpy (*data, &pkey, sizeof (pkey));
521 *data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
523 case GNUNET_NAMESTORE_TYPE_PSEU:
524 *data = GNUNET_strdup (s);
525 *data_size = strlen (s);
527 case GNUNET_NAMESTORE_TYPE_LEHO:
528 *data = GNUNET_strdup (s);
529 *data_size = strlen (s);
531 case GNUNET_NAMESTORE_TYPE_VPN:
533 *data_size = sizeof (uint32_t) * 2;
534 *data_size += sizeof (struct GNUNET_HashCode) * 2;
535 *data = GNUNET_malloc (*data_size);
536 af = (uint32_t*)(*data);
537 proto = (uint32_t*) ((char*)af + sizeof (uint32_t));
538 h_peer = (struct GNUNET_HashCode*)((char*)proto + sizeof (uint32_t));
539 h_serv = (struct GNUNET_HashCode*)((char*)h_peer + sizeof (struct GNUNET_HashCode));
541 if (4 != SSCANF (s,"%d:%d:%s:%s",
542 af, proto, (char*)&s_peer, (char*)&s_serv))
544 return GNUNET_SYSERR;
546 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_peer, h_peer)) ||
547 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_serv, h_serv)))
550 return GNUNET_SYSERR;
556 return GNUNET_SYSERR;
564 { "A", GNUNET_DNSPARSER_TYPE_A },
565 { "NS", GNUNET_DNSPARSER_TYPE_NS },
566 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
567 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
568 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
569 { "MX", GNUNET_DNSPARSER_TYPE_MX },
570 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
571 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
572 { "PKEY", GNUNET_NAMESTORE_TYPE_PKEY },
573 { "PSEU", GNUNET_NAMESTORE_TYPE_PSEU },
574 { "LEHO", GNUNET_NAMESTORE_TYPE_LEHO },
580 * Convert a type name (i.e. "AAAA") to the corresponding number.
582 * @param typename name to convert
583 * @return corresponding number, UINT32_MAX on error
586 GNUNET_NAMESTORE_typename_to_number (const char *typename)
591 while ( (name_map[i].name != NULL) &&
592 (0 != strcasecmp (typename, name_map[i].name)) )
594 return name_map[i].number;
599 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
601 * @param type number of a type to convert
602 * @return corresponding typestring, NULL on error
605 GNUNET_NAMESTORE_number_to_typename (uint32_t type)
610 while ( (name_map[i].name != NULL) &&
611 (type != name_map[i].number) )
613 return name_map[i].name;
618 /* end of namestore_common.c */