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 name the name
202 * @param rd record data
203 * @param rd_count number of records
205 * @return the signature
207 struct GNUNET_CRYPTO_RsaSignature *
208 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
210 const struct GNUNET_NAMESTORE_RecordData *rd,
211 unsigned int rd_count)
213 struct GNUNET_CRYPTO_RsaSignature *sig = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignature));
214 struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
227 name_len = strlen (name) + 1;
229 rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
230 char rd_ser[rd_ser_len];
231 GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
233 sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
235 sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
236 sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
237 name_tmp = (char *) &sig_purpose[1];
238 rd_tmp = &name_tmp[name_len];
239 memcpy (name_tmp, name, name_len);
240 memcpy (rd_tmp, rd_ser, rd_ser_len);
242 res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
244 GNUNET_free (sig_purpose);
246 if (GNUNET_OK != res)
257 * Convert the 'value' of a record to a string.
259 * @param type type of the record
260 * @param data value in binary encoding
261 * @param data_size number of bytes in data
262 * @return NULL on error, otherwise human-readable representation of the value
265 GNUNET_NAMESTORE_value_to_string (uint32_t type,
269 char tmp[INET6_ADDRSTRLEN];
275 case GNUNET_DNSPARSER_TYPE_A:
276 if (data_size != sizeof (struct in_addr))
278 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
280 return GNUNET_strdup (tmp);
281 case GNUNET_DNSPARSER_TYPE_NS:
282 return GNUNET_strndup (data, data_size);
283 case GNUNET_DNSPARSER_TYPE_CNAME:
284 return GNUNET_strndup (data, data_size);
285 case GNUNET_DNSPARSER_TYPE_SOA:
289 case GNUNET_DNSPARSER_TYPE_PTR:
293 case GNUNET_DNSPARSER_TYPE_MX:
297 case GNUNET_DNSPARSER_TYPE_TXT:
298 return GNUNET_strndup (data, data_size);
299 case GNUNET_DNSPARSER_TYPE_AAAA:
300 if (data_size != sizeof (struct in6_addr))
302 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
304 return GNUNET_strdup (tmp);
305 case GNUNET_NAMESTORE_TYPE_PKEY:
306 if (data_size != sizeof (GNUNET_HashCode))
308 return GNUNET_strdup (GNUNET_h2s_full (data));
309 case GNUNET_NAMESTORE_TYPE_PSEU:
310 return GNUNET_strndup (data, data_size);
314 GNUNET_break (0); // not implemented
320 * Convert human-readable version of a 'value' of a record to the binary
323 * @param type type of the record
324 * @param s human-readable string
325 * @param data set to value in binary encoding (will be allocated)
326 * @param data_size set to number of bytes in data
327 * @return GNUNET_OK on success
330 GNUNET_NAMESTORE_string_to_value (uint32_t type,
335 struct in_addr value_a;
336 struct in6_addr value_aaaa;
337 GNUNET_HashCode pkey;
342 return GNUNET_SYSERR;
343 case GNUNET_DNSPARSER_TYPE_A:
344 if (1 != inet_pton (AF_INET, s, &value_a))
345 return GNUNET_SYSERR;
346 *data = GNUNET_malloc (sizeof (struct in_addr));
347 memcpy (*data, &value_a, sizeof (value_a));
348 *data_size = sizeof (value_a);
350 case GNUNET_DNSPARSER_TYPE_NS:
351 *data = GNUNET_strdup (s);
352 *data_size = strlen (s);
354 case GNUNET_DNSPARSER_TYPE_CNAME:
355 *data = GNUNET_strdup (s);
356 *data_size = strlen (s);
358 case GNUNET_DNSPARSER_TYPE_SOA:
361 return GNUNET_SYSERR;
362 case GNUNET_DNSPARSER_TYPE_PTR:
365 return GNUNET_SYSERR;
366 case GNUNET_DNSPARSER_TYPE_MX:
369 return GNUNET_SYSERR;
370 case GNUNET_DNSPARSER_TYPE_TXT:
371 *data = GNUNET_strdup (s);
372 *data_size = strlen (s);
374 case GNUNET_DNSPARSER_TYPE_AAAA:
375 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
376 return GNUNET_SYSERR;
377 *data = GNUNET_malloc (sizeof (struct in6_addr));
378 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
380 case GNUNET_NAMESTORE_TYPE_PKEY:
382 GNUNET_CRYPTO_hash_from_string (s, &pkey))
383 return GNUNET_SYSERR;
384 *data = GNUNET_malloc (sizeof (GNUNET_HashCode));
385 memcpy (*data, &pkey, sizeof (pkey));
387 case GNUNET_NAMESTORE_TYPE_PSEU:
388 *data = GNUNET_strdup (s);
389 *data_size = strlen (s);
394 return GNUNET_SYSERR;
402 { "A", GNUNET_DNSPARSER_TYPE_A },
403 { "NS", GNUNET_DNSPARSER_TYPE_NS },
404 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
405 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
406 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
407 { "MX", GNUNET_DNSPARSER_TYPE_MX },
408 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
409 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
410 { "PKEY", GNUNET_NAMESTORE_TYPE_PKEY },
411 { "PSEU", GNUNET_NAMESTORE_TYPE_PSEU },
417 * Convert a type name (i.e. "AAAA") to the corresponding number.
419 * @param typename name to convert
420 * @return corresponding number, UINT32_MAX on error
423 GNUNET_NAMESTORE_typename_to_number (const char *typename)
428 while ( (name_map[i].name != NULL) &&
429 (0 != strcasecmp (typename, name_map[i].name)) )
431 return name_map[i].number;
436 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
438 * @param type number of a type to convert
439 * @return corresponding typestring, NULL on error
442 GNUNET_NAMESTORE_number_to_typename (uint32_t type)
447 while ( (name_map[i].name != NULL) &&
448 (type != name_map[i].number) )
450 return name_map[i].name;
455 /* end of namestore_common.c */