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__)
42 * Internal format of a record in the serialized form.
48 * Expiration time for the DNS record.
50 struct GNUNET_TIME_AbsoluteNBO expiration;
53 * Number of bytes in 'data', network byte order.
58 * Type of the GNS/DNS record, network byte order.
63 * Flags for the record, network byte order.
71 * Convert a short hash to a string (for printing debug messages).
72 * This is one of the very few calls in the entire API that is
75 * @param hc the short hash code
76 * @return string form; will be overwritten by next call to GNUNET_h2s.
79 GNUNET_short_h2s (const struct GNUNET_CRYPTO_ShortHashCode * hc)
81 static struct GNUNET_CRYPTO_ShortHashAsciiEncoded ret;
83 GNUNET_CRYPTO_short_hash_to_enc (hc, &ret);
84 return (const char *) &ret;
89 * Calculate how many bytes we will need to serialize the given
92 * @param rd_count number of records in the rd array
93 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
95 * @return the required size to serialize
99 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
100 const struct GNUNET_NAMESTORE_RecordData *rd)
105 ret = sizeof (struct NetworkRecord) * rd_count;
106 for (i=0;i<rd_count;i++)
108 GNUNET_assert ((ret + rd[i].data_size) >= ret);
109 ret += rd[i].data_size;
116 * Serialize the given records to the given destination buffer.
118 * @param rd_count number of records in the rd array
119 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
120 * @param dest_size size of the destination array
121 * @param dest where to write the result
123 * @return the size of serialized records
126 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
127 const struct GNUNET_NAMESTORE_RecordData *rd,
131 struct NetworkRecord rec;
136 for (i=0;i<rd_count;i++)
138 rec.expiration = GNUNET_TIME_absolute_hton (rd[i].expiration);
139 rec.data_size = htonl ((uint32_t) rd[i].data_size);
140 rec.record_type = htonl (rd[i].record_type);
141 rec.flags = htonl (rd[i].flags);
142 if (off + sizeof (rec) > dest_size)
144 memcpy (&dest[off], &rec, sizeof (rec));
146 if (off + rd[i].data_size > dest_size)
148 memcpy (&dest[off], rd[i].data, rd[i].data_size);
149 off += rd[i].data_size;
155 * Compares if two records are equal
160 * @return GNUNET_YES or GNUNET_NO
163 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
164 const struct GNUNET_NAMESTORE_RecordData *b)
166 if ((a->record_type == b->record_type) &&
167 (a->expiration.abs_value == b->expiration.abs_value) &&
168 (a->data_size == b->data_size) &&
169 (0 == memcmp (a->data, b->data, a->data_size)))
177 * Deserialize the given records to the given destination.
179 * @param len size of the serialized record data
180 * @param src the serialized record data
181 * @param rd_count number of records in the rd array
182 * @param dest where to put the data
184 * @return GNUNET_OK on success, GNUNET_SYSERR on error
187 GNUNET_NAMESTORE_records_deserialize (size_t len,
189 unsigned int rd_count,
190 struct GNUNET_NAMESTORE_RecordData *dest)
192 struct NetworkRecord rec;
197 for (i=0;i<rd_count;i++)
199 if (off + sizeof (rec) > len)
200 return GNUNET_SYSERR;
201 memcpy (&rec, &src[off], sizeof (rec));
202 dest[i].expiration = GNUNET_TIME_absolute_ntoh (rec.expiration);
203 dest[i].data_size = ntohl ((uint32_t) rec.data_size);
204 dest[i].record_type = ntohl (rec.record_type);
205 dest[i].flags = ntohl (rec.flags);
208 if (off + dest[i].data_size > len)
209 return GNUNET_SYSERR;
210 dest[i].data = &src[off];
211 off += dest[i].data_size;
217 * Sign name and records
219 * @param key the private key
220 * @param expire block expiration
221 * @param name the name
222 * @param rd record data
223 * @param rd_count number of records
225 * @return the signature
227 struct GNUNET_CRYPTO_RsaSignature *
228 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
229 struct GNUNET_TIME_Absolute expire,
231 const struct GNUNET_NAMESTORE_RecordData *rd,
232 unsigned int rd_count)
234 struct GNUNET_CRYPTO_RsaSignature *sig = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignature));
235 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
236 struct GNUNET_TIME_AbsoluteNBO expire_nbo = GNUNET_TIME_absolute_hton(expire);
240 struct GNUNET_TIME_AbsoluteNBO *expire_tmp;
251 name_len = strlen (name) + 1;
253 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
254 char rd_ser[rd_ser_len];
255 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
257 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + sizeof (struct GNUNET_TIME_AbsoluteNBO) + rd_ser_len + name_len);
258 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
259 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
260 expire_tmp = (struct GNUNET_TIME_AbsoluteNBO *) &sig_purpose[1];
261 name_tmp = (char *) &expire_tmp[1];
262 rd_tmp = &name_tmp[name_len];
263 memcpy (expire_tmp, &expire_nbo, sizeof (struct GNUNET_TIME_AbsoluteNBO));
264 memcpy (name_tmp, name, name_len);
265 memcpy (rd_tmp, rd_ser, rd_ser_len);
267 res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
269 GNUNET_free (sig_purpose);
271 if (GNUNET_OK != res)
281 * Checks if a name is wellformed
283 * @param name the name to check
284 * @return GNUNET_OK on success, GNUNET_SYSERR on error
287 GNUNET_NAMESTORE_check_name (const char * name)
290 return GNUNET_SYSERR;
291 if (strlen (name) > 63)
292 return GNUNET_SYSERR;
298 * Convert the 'value' of a record to a string.
300 * @param type type of the record
301 * @param data value in binary encoding
302 * @param data_size number of bytes in data
303 * @return NULL on error, otherwise human-readable representation of the value
306 GNUNET_NAMESTORE_value_to_string (uint32_t type,
310 char tmp[INET6_ADDRSTRLEN];
311 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
318 uint32_t soa_refresh;
327 case GNUNET_DNSPARSER_TYPE_A:
328 if (data_size != sizeof (struct in_addr))
330 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
332 return GNUNET_strdup (tmp);
333 case GNUNET_DNSPARSER_TYPE_NS:
334 return GNUNET_strndup (data, data_size);
335 case GNUNET_DNSPARSER_TYPE_CNAME:
336 return GNUNET_strndup (data, data_size);
337 case GNUNET_DNSPARSER_TYPE_SOA:
338 soa_rname = (char*)data;
339 soa_mname = (char*)data+strlen(soa_rname)+1;
340 soa_data = (uint32_t*)(soa_mname+strlen(soa_mname)+1);
341 soa_serial = ntohl(soa_data[0]);
342 soa_refresh = ntohl(soa_data[1]);
343 soa_retry = ntohl(soa_data[2]);
344 soa_expire = ntohl(soa_data[3]);
345 soa_min = ntohl(soa_data[4]);
346 if (GNUNET_asprintf(&result, "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
347 soa_rname, soa_mname,
348 soa_serial, soa_refresh, soa_retry, soa_expire, soa_min))
352 case GNUNET_DNSPARSER_TYPE_PTR:
353 return GNUNET_strndup (data, data_size);
354 case GNUNET_DNSPARSER_TYPE_MX:
355 mx_pref = ntohs(*((uint16_t*)data));
356 if (GNUNET_asprintf(&result, "%hu,%s", mx_pref, data+sizeof(uint16_t))
361 case GNUNET_DNSPARSER_TYPE_TXT:
362 return GNUNET_strndup (data, data_size);
363 case GNUNET_DNSPARSER_TYPE_AAAA:
364 if (data_size != sizeof (struct in6_addr))
366 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
368 return GNUNET_strdup (tmp);
369 case GNUNET_NAMESTORE_TYPE_PKEY:
370 if (data_size != sizeof (struct GNUNET_CRYPTO_ShortHashCode))
372 GNUNET_CRYPTO_short_hash_to_enc (data,
374 return GNUNET_strdup ((const char*) enc.short_encoding);
375 case GNUNET_NAMESTORE_TYPE_PSEU:
376 return GNUNET_strndup (data, data_size);
377 case GNUNET_NAMESTORE_TYPE_LEHO:
378 return GNUNET_strndup (data, data_size);
379 case GNUNET_NAMESTORE_TYPE_VPN:
380 return GNUNET_strndup (data, data_size);
384 GNUNET_break (0); // not implemented
390 * Convert human-readable version of a 'value' of a record to the binary
393 * @param type type of the record
394 * @param s human-readable string
395 * @param data set to value in binary encoding (will be allocated)
396 * @param data_size set to number of bytes in data
397 * @return GNUNET_OK on success
400 GNUNET_NAMESTORE_string_to_value (uint32_t type,
405 struct in_addr value_a;
406 struct in6_addr value_aaaa;
407 struct GNUNET_CRYPTO_ShortHashCode pkey;
410 uint32_t soa_data[5];
415 uint32_t soa_refresh;
419 struct GNUNET_HashCode hash;
420 struct GNUNET_CRYPTO_HashAsciiEncoded s_peer;
421 struct GNUNET_CRYPTO_HashAsciiEncoded s_serv;
428 return GNUNET_SYSERR;
429 case GNUNET_DNSPARSER_TYPE_A:
430 if (1 != inet_pton (AF_INET, s, &value_a))
431 return GNUNET_SYSERR;
432 *data = GNUNET_malloc (sizeof (struct in_addr));
433 memcpy (*data, &value_a, sizeof (value_a));
434 *data_size = sizeof (value_a);
436 case GNUNET_DNSPARSER_TYPE_NS:
437 *data = GNUNET_strdup (s);
438 *data_size = strlen (s);
440 case GNUNET_DNSPARSER_TYPE_CNAME:
441 *data = GNUNET_strdup (s);
442 *data_size = strlen (s);
444 case GNUNET_DNSPARSER_TYPE_SOA:
446 if (SSCANF(s, "rname=%s mname=%s %u,%u,%u,%u,%u",
447 soa_rname, soa_mname,
448 &soa_serial, &soa_refresh, &soa_retry, &soa_expire, &soa_min)
450 return GNUNET_SYSERR;
452 *data_size = sizeof (soa_data)+strlen(soa_rname)+strlen(soa_mname)+2;
453 *data = GNUNET_malloc (*data_size);
454 soa_data[0] = htonl(soa_serial);
455 soa_data[1] = htonl(soa_refresh);
456 soa_data[2] = htonl(soa_retry);
457 soa_data[3] = htonl(soa_expire);
458 soa_data[4] = htonl(soa_min);
459 strcpy(*data, soa_rname);
460 strcpy(*data+strlen(*data)+1, soa_mname);
461 memcpy(*data+strlen(*data)+1+strlen(soa_mname)+1,
462 soa_data, sizeof(soa_data));
465 case GNUNET_DNSPARSER_TYPE_PTR:
466 *data = GNUNET_strdup (s);
467 *data_size = strlen (s);
469 case GNUNET_DNSPARSER_TYPE_MX:
470 if (SSCANF(s, "%hu,%s", &mx_pref, result) != 2)
471 return GNUNET_SYSERR;
472 *data_size = sizeof (uint16_t)+strlen(result)+1;
473 *data = GNUNET_malloc (*data_size);
474 mx_pref_n = htons(mx_pref);
475 memcpy(*data, &mx_pref_n, sizeof (uint16_t));
476 strcpy((*data)+sizeof (uint16_t), result);
478 case GNUNET_DNSPARSER_TYPE_TXT:
479 *data = GNUNET_strdup (s);
480 *data_size = strlen (s);
482 case GNUNET_DNSPARSER_TYPE_AAAA:
483 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
484 return GNUNET_SYSERR;
485 *data = GNUNET_malloc (sizeof (struct in6_addr));
486 *data_size = sizeof (struct in6_addr);
487 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
489 case GNUNET_NAMESTORE_TYPE_PKEY:
491 GNUNET_CRYPTO_short_hash_from_string (s, &pkey))
492 return GNUNET_SYSERR;
493 *data = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
494 memcpy (*data, &pkey, sizeof (pkey));
495 *data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
497 case GNUNET_NAMESTORE_TYPE_PSEU:
498 *data = GNUNET_strdup (s);
499 *data_size = strlen (s);
501 case GNUNET_NAMESTORE_TYPE_LEHO:
502 *data = GNUNET_strdup (s);
503 *data_size = strlen (s);
505 case GNUNET_NAMESTORE_TYPE_VPN:
506 if (4 != SSCANF (s,"%d:%d:%s:%s",
507 &af, &proto, (char*)&s_peer, (char*)&s_serv))
509 return GNUNET_SYSERR;
511 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_peer, &hash)) ||
512 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_serv, &hash)))
514 return GNUNET_SYSERR;
516 *data = GNUNET_strdup (s);
517 *data_size = strlen (s);
522 return GNUNET_SYSERR;
530 { "A", GNUNET_DNSPARSER_TYPE_A },
531 { "NS", GNUNET_DNSPARSER_TYPE_NS },
532 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
533 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
534 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
535 { "MX", GNUNET_DNSPARSER_TYPE_MX },
536 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
537 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
538 { "PKEY", GNUNET_NAMESTORE_TYPE_PKEY },
539 { "PSEU", GNUNET_NAMESTORE_TYPE_PSEU },
540 { "LEHO", GNUNET_NAMESTORE_TYPE_LEHO },
546 * Convert a type name (i.e. "AAAA") to the corresponding number.
548 * @param typename name to convert
549 * @return corresponding number, UINT32_MAX on error
552 GNUNET_NAMESTORE_typename_to_number (const char *typename)
557 while ( (name_map[i].name != NULL) &&
558 (0 != strcasecmp (typename, name_map[i].name)) )
560 return name_map[i].number;
565 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
567 * @param type number of a type to convert
568 * @return corresponding typestring, NULL on error
571 GNUNET_NAMESTORE_number_to_typename (uint32_t type)
576 while ( (name_map[i].name != NULL) &&
577 (type != name_map[i].number) )
579 return name_map[i].name;
584 /* end of namestore_common.c */