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"
36 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
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.
70 * Calculate how many bytes we will need to serialize the given
73 * @param rd_count number of records in the rd array
74 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
76 * @return the required size to serialize
80 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
81 const struct GNUNET_NAMESTORE_RecordData *rd)
86 ret = sizeof (struct NetworkRecord) * rd_count;
87 for (i=0;i<rd_count;i++)
89 GNUNET_assert ((ret + rd[i].data_size) >= ret);
90 ret += rd[i].data_size;
97 * Serialize the given records to the given destination buffer.
99 * @param rd_count number of records in the rd array
100 * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
101 * @param dest_size size of the destination array
102 * @param dest where to write the result
104 * @return the size of serialized records
107 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
108 const struct GNUNET_NAMESTORE_RecordData *rd,
112 struct NetworkRecord rec;
117 for (i=0;i<rd_count;i++)
119 rec.expiration = GNUNET_TIME_absolute_hton (rd[i].expiration);
120 rec.data_size = htonl ((uint32_t) rd[i].data_size);
121 rec.record_type = htonl (rd[i].record_type);
122 rec.flags = htonl (rd[i].flags);
123 if (off + sizeof (rec) > dest_size)
125 memcpy (&dest[off], &rec, sizeof (rec));
127 if (off + rd[i].data_size > dest_size)
129 memcpy (&dest[off], rd[i].data, rd[i].data_size);
130 off += rd[i].data_size;
136 * Compares if two records are equal
141 * @return GNUNET_YES or GNUNET_NO
144 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
145 const struct GNUNET_NAMESTORE_RecordData *b)
147 if ((a->record_type == b->record_type) &&
148 (a->expiration.abs_value == b->expiration.abs_value) &&
149 (a->data_size == b->data_size) &&
150 (0 == memcmp (a->data, b->data, a->data_size)))
158 * Deserialize the given records to the given destination.
160 * @param len size of the serialized record data
161 * @param src the serialized record data
162 * @param rd_count number of records in the rd array
163 * @param dest where to put the data
165 * @return GNUNET_OK on success, GNUNET_SYSERR on error
168 GNUNET_NAMESTORE_records_deserialize (size_t len,
170 unsigned int rd_count,
171 struct GNUNET_NAMESTORE_RecordData *dest)
173 struct NetworkRecord rec;
178 for (i=0;i<rd_count;i++)
180 if (off + sizeof (rec) > len)
181 return GNUNET_SYSERR;
182 memcpy (&rec, &src[off], sizeof (rec));
183 dest[i].expiration = GNUNET_TIME_absolute_ntoh (rec.expiration);
184 dest[i].data_size = ntohl ((uint32_t) rec.data_size);
185 dest[i].record_type = ntohl (rec.record_type);
186 dest[i].flags = ntohl (rec.flags);
189 if (off + dest[i].data_size > len)
190 return GNUNET_SYSERR;
191 dest[i].data = &src[off];
192 off += dest[i].data_size;
198 * Sign name and records
200 * @param key the private key
201 * @param expire block expiration
202 * @param name the name
203 * @param rd record data
204 * @param rd_count number of records
206 * @return the signature
208 struct GNUNET_CRYPTO_RsaSignature *
209 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
210 struct GNUNET_TIME_Absolute expire,
212 const struct GNUNET_NAMESTORE_RecordData *rd,
213 unsigned int rd_count)
215 struct GNUNET_CRYPTO_RsaSignature *sig = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignature));
216 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
217 struct GNUNET_TIME_AbsoluteNBO expire_nbo = GNUNET_TIME_absolute_hton(expire);
221 struct GNUNET_TIME_AbsoluteNBO *expire_tmp;
232 name_len = strlen (name) + 1;
234 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
235 char rd_ser[rd_ser_len];
236 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
238 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + sizeof (struct GNUNET_TIME_AbsoluteNBO) + rd_ser_len + name_len);
239 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
240 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
241 expire_tmp = (struct GNUNET_TIME_AbsoluteNBO *) &sig_purpose[1];
242 name_tmp = (char *) &expire_tmp[1];
243 rd_tmp = &name_tmp[name_len];
244 memcpy (expire_tmp, &expire_nbo, sizeof (struct GNUNET_TIME_AbsoluteNBO));
245 memcpy (name_tmp, name, name_len);
246 memcpy (rd_tmp, rd_ser, rd_ser_len);
248 res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
250 GNUNET_free (sig_purpose);
252 if (GNUNET_OK != res)
263 * Convert the 'value' of a record to a string.
265 * @param type type of the record
266 * @param data value in binary encoding
267 * @param data_size number of bytes in data
268 * @return NULL on error, otherwise human-readable representation of the value
271 GNUNET_NAMESTORE_value_to_string (uint32_t type,
275 char tmp[INET6_ADDRSTRLEN];
281 case GNUNET_DNSPARSER_TYPE_A:
282 if (data_size != sizeof (struct in_addr))
284 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
286 return GNUNET_strdup (tmp);
287 case GNUNET_DNSPARSER_TYPE_NS:
288 return GNUNET_strndup (data, data_size);
289 case GNUNET_DNSPARSER_TYPE_CNAME:
290 return GNUNET_strndup (data, data_size);
291 case GNUNET_DNSPARSER_TYPE_SOA:
295 case GNUNET_DNSPARSER_TYPE_PTR:
299 case GNUNET_DNSPARSER_TYPE_MX:
303 case GNUNET_DNSPARSER_TYPE_TXT:
304 return GNUNET_strndup (data, data_size);
305 case GNUNET_DNSPARSER_TYPE_AAAA:
306 if (data_size != sizeof (struct in6_addr))
308 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
310 return GNUNET_strdup (tmp);
311 case GNUNET_NAMESTORE_TYPE_PKEY:
312 if (data_size != sizeof (GNUNET_HashCode))
314 return GNUNET_strdup (GNUNET_h2s_full (data));
315 case GNUNET_NAMESTORE_TYPE_PSEU:
316 return GNUNET_strndup (data, data_size);
320 GNUNET_break (0); // not implemented
326 * Convert human-readable version of a 'value' of a record to the binary
329 * @param type type of the record
330 * @param s human-readable string
331 * @param data set to value in binary encoding (will be allocated)
332 * @param data_size set to number of bytes in data
333 * @return GNUNET_OK on success
336 GNUNET_NAMESTORE_string_to_value (uint32_t type,
341 struct in_addr value_a;
342 struct in6_addr value_aaaa;
343 GNUNET_HashCode pkey;
348 return GNUNET_SYSERR;
349 case GNUNET_DNSPARSER_TYPE_A:
350 if (1 != inet_pton (AF_INET, s, &value_a))
351 return GNUNET_SYSERR;
352 *data = GNUNET_malloc (sizeof (struct in_addr));
353 memcpy (*data, &value_a, sizeof (value_a));
354 *data_size = sizeof (value_a);
356 case GNUNET_DNSPARSER_TYPE_NS:
357 *data = GNUNET_strdup (s);
358 *data_size = strlen (s);
360 case GNUNET_DNSPARSER_TYPE_CNAME:
361 *data = GNUNET_strdup (s);
362 *data_size = strlen (s);
364 case GNUNET_DNSPARSER_TYPE_SOA:
367 return GNUNET_SYSERR;
368 case GNUNET_DNSPARSER_TYPE_PTR:
371 return GNUNET_SYSERR;
372 case GNUNET_DNSPARSER_TYPE_MX:
375 return GNUNET_SYSERR;
376 case GNUNET_DNSPARSER_TYPE_TXT:
377 *data = GNUNET_strdup (s);
378 *data_size = strlen (s);
380 case GNUNET_DNSPARSER_TYPE_AAAA:
381 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
382 return GNUNET_SYSERR;
383 *data = GNUNET_malloc (sizeof (struct in6_addr));
384 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
386 case GNUNET_NAMESTORE_TYPE_PKEY:
388 GNUNET_CRYPTO_hash_from_string (s, &pkey))
389 return GNUNET_SYSERR;
390 *data = GNUNET_malloc (sizeof (GNUNET_HashCode));
391 memcpy (*data, &pkey, sizeof (pkey));
392 *data_size = sizeof (GNUNET_HashCode);
394 case GNUNET_NAMESTORE_TYPE_PSEU:
395 *data = GNUNET_strdup (s);
396 *data_size = strlen (s);
401 return GNUNET_SYSERR;
409 { "A", GNUNET_DNSPARSER_TYPE_A },
410 { "NS", GNUNET_DNSPARSER_TYPE_NS },
411 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
412 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
413 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
414 { "MX", GNUNET_DNSPARSER_TYPE_MX },
415 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
416 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
417 { "PKEY", GNUNET_NAMESTORE_TYPE_PKEY },
418 { "PSEU", GNUNET_NAMESTORE_TYPE_PSEU },
424 * Convert a type name (i.e. "AAAA") to the corresponding number.
426 * @param typename name to convert
427 * @return corresponding number, UINT32_MAX on error
430 GNUNET_NAMESTORE_typename_to_number (const char *typename)
435 while ( (name_map[i].name != NULL) &&
436 (0 != strcasecmp (typename, name_map[i].name)) )
438 return name_map[i].number;
443 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
445 * @param type number of a type to convert
446 * @return corresponding typestring, NULL on error
449 GNUNET_NAMESTORE_number_to_typename (uint32_t type)
454 while ( (name_map[i].name != NULL) &&
455 (type != name_map[i].number) )
457 return name_map[i].name;
462 /* end of namestore_common.c */