- not required anymore
[oweals/gnunet.git] / src / namestore / namestore_common.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file namestore/namestore_common.c
23  * @brief API to access the NAMESTORE service
24  * @author Martin Schanzenbach
25  * @author Matthias Wachs
26  */
27
28 #include "platform.h"
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
37
38 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
39
40
41 /**
42  * Internal format of a record in the serialized form.
43  */
44 struct NetworkRecord
45 {
46
47   /**
48    * Expiration time for the DNS record.
49    */
50   struct GNUNET_TIME_AbsoluteNBO expiration;
51
52   /**
53    * Number of bytes in 'data', network byte order.
54    */
55   uint32_t data_size;
56
57   /**
58    * Type of the GNS/DNS record, network byte order.
59    */
60   uint32_t record_type;
61
62   /**
63    * Flags for the record, network byte order.
64    */
65   uint32_t flags;
66   
67 };
68
69 /**
70  * Calculate how many bytes we will need to serialize the given
71  * records.
72  *
73  * @param rd_count number of records in the rd array
74  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
75  *
76  * @return the required size to serialize
77  *
78  */
79 size_t
80 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
81                                    const struct GNUNET_NAMESTORE_RecordData *rd)
82 {
83   unsigned int i;
84   size_t ret;
85
86   ret = sizeof (struct NetworkRecord) * rd_count;
87   for (i=0;i<rd_count;i++)
88   {
89     GNUNET_assert (ret + rd[i].data_size >= ret);
90     ret += rd[i].data_size;
91   }
92   return ret;  
93 }
94
95
96 /**
97  * Serialize the given records to the given destination buffer.
98  *
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
103  *
104  * @return the size of serialized records
105  */
106 ssize_t
107 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
108                                     const struct GNUNET_NAMESTORE_RecordData *rd,
109                                     size_t dest_size,
110                                     char *dest)
111 {
112   struct NetworkRecord rec;
113   unsigned int i;
114   size_t off;
115   
116   off = 0;
117   for (i=0;i<rd_count;i++)
118   {
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)
124       return -1;
125     memcpy (&dest[off], &rec, sizeof (rec));
126     off += sizeof (rec);
127     if (off + rd[i].data_size > dest_size)
128       return -1;
129     memcpy (&dest[off], rd[i].data, rd[i].data_size);
130     off += rd[i].data_size;
131   }
132   return off;
133 }
134
135 /**
136  * Compares if two records are equal
137  *
138  * @param a record
139  * @param b record
140  *
141  * @return GNUNET_YES or GNUNET_NO
142  */
143 int
144 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
145                               const struct GNUNET_NAMESTORE_RecordData *b)
146 {
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)))
151     return GNUNET_YES;
152   else
153     return GNUNET_NO;
154 }
155
156
157 /**
158  * Deserialize the given records to the given destination.
159  *
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
164  *
165  * @return GNUNET_OK on success, GNUNET_SYSERR on error
166  */
167 int
168 GNUNET_NAMESTORE_records_deserialize (size_t len,
169                                       const char *src,
170                                       unsigned int rd_count,
171                                       struct GNUNET_NAMESTORE_RecordData *dest)
172 {
173   struct NetworkRecord rec;
174   unsigned int i;
175   size_t off;
176   
177   off = 0;
178   for (i=0;i<rd_count;i++)
179   {
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);
187     off += sizeof (rec);
188
189     if (off + dest[i].data_size > len)
190       return GNUNET_SYSERR;
191     dest[i].data = &src[off];
192     off += dest[i].data_size;
193   }
194   return GNUNET_OK; 
195 }
196
197 /**
198  * Sign name and records
199  *
200  * @param key the private key
201  * @param name the name
202  * @param rd record data
203  * @param rd_count number of records
204  *
205  * @return the signature
206  */
207 struct GNUNET_CRYPTO_RsaSignature *
208 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
209     const char *name,
210     const struct GNUNET_NAMESTORE_RecordData *rd,
211     unsigned int rd_count)
212 {
213   struct GNUNET_CRYPTO_RsaSignature *sig = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignature));
214   struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
215   size_t rd_ser_len;
216   size_t name_len;
217   char * name_tmp;
218   char * rd_tmp;
219   int res;
220
221   if (name == NULL)
222   {
223     GNUNET_break (0);
224     GNUNET_free (sig);
225     return NULL;
226   }
227   name_len = strlen (name) + 1;
228
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);
232
233   sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
234
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);
241
242   res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
243
244   GNUNET_free (sig_purpose);
245
246   if (GNUNET_OK != res)
247   {
248     GNUNET_break (0);
249     GNUNET_free (sig);
250     return NULL;
251   }
252   return sig;
253 }
254
255
256 /**
257  * Convert the 'value' of a record to a string.
258  *
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
263  */
264 char *
265 GNUNET_NAMESTORE_value_to_string (uint32_t type,
266                                   const void *data,
267                                   size_t data_size)
268 {
269   char tmp[INET6_ADDRSTRLEN];
270
271   switch (type)
272   {
273   case 0:
274     return NULL;
275   case GNUNET_DNSPARSER_TYPE_A:
276     if (data_size != sizeof (struct in_addr))
277       return NULL;
278     if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
279       return NULL;
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:
286     GNUNET_break (0);
287     // FIXME
288     return NULL;
289   case GNUNET_DNSPARSER_TYPE_PTR:
290     GNUNET_break (0);
291     // FIXME
292     return NULL;
293   case GNUNET_DNSPARSER_TYPE_MX:
294     GNUNET_break (0);
295     // FIXME
296     return NULL;
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))
301       return NULL;
302     if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
303       return NULL;
304     return GNUNET_strdup (tmp);
305   case GNUNET_NAMESTORE_TYPE_PKEY:
306     if (data_size != sizeof (GNUNET_HashCode))
307       return NULL;
308     return GNUNET_strdup (GNUNET_h2s_full (data));
309   case GNUNET_NAMESTORE_TYPE_PSEU:
310     return GNUNET_strndup (data, data_size);
311   default:
312     GNUNET_break (0);
313   }
314   GNUNET_break (0); // not implemented
315   return NULL;
316 }
317
318
319 /**
320  * Convert human-readable version of a 'value' of a record to the binary
321  * representation.
322  *
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
328  */
329 int
330 GNUNET_NAMESTORE_string_to_value (uint32_t type,
331                                   const char *s,
332                                   void **data,
333                                   size_t *data_size)
334 {
335   struct in_addr value_a;
336   struct in6_addr value_aaaa;
337   GNUNET_HashCode pkey;
338
339   switch (type)
340   {
341   case 0:
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);
349     return GNUNET_OK;
350   case GNUNET_DNSPARSER_TYPE_NS:
351     *data = GNUNET_strdup (s);
352     *data_size = strlen (s);
353     return GNUNET_OK;
354   case GNUNET_DNSPARSER_TYPE_CNAME:
355     *data = GNUNET_strdup (s);
356     *data_size = strlen (s);
357     return GNUNET_OK;
358   case GNUNET_DNSPARSER_TYPE_SOA:
359     GNUNET_break (0);
360     // FIXME
361     return GNUNET_SYSERR;
362   case GNUNET_DNSPARSER_TYPE_PTR:
363     GNUNET_break (0);
364     // FIXME
365     return GNUNET_SYSERR;
366   case GNUNET_DNSPARSER_TYPE_MX:
367     GNUNET_break (0);
368     // FIXME
369     return GNUNET_SYSERR;
370   case GNUNET_DNSPARSER_TYPE_TXT:
371     *data = GNUNET_strdup (s);
372     *data_size = strlen (s);
373     return GNUNET_OK;
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));
379     return GNUNET_OK;
380   case GNUNET_NAMESTORE_TYPE_PKEY:
381     if (GNUNET_OK !=
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));
386     return GNUNET_OK;
387   case GNUNET_NAMESTORE_TYPE_PSEU:
388     *data = GNUNET_strdup (s);
389     *data_size = strlen (s);
390     return GNUNET_OK;
391   default:
392     GNUNET_break (0);
393   }
394   return GNUNET_SYSERR;
395 }
396
397
398 static struct { 
399   const char *name; 
400   uint32_t number; 
401 } name_map[] = {
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 },
412   { NULL, UINT32_MAX }
413 };
414
415
416 /**
417  * Convert a type name (i.e. "AAAA") to the corresponding number.
418  *
419  * @param typename name to convert
420  * @return corresponding number, UINT32_MAX on error
421  */
422 uint32_t
423 GNUNET_NAMESTORE_typename_to_number (const char *typename)
424 {
425   unsigned int i;
426
427   i=0;
428   while ( (name_map[i].name != NULL) &&
429           (0 != strcasecmp (typename, name_map[i].name)) )
430     i++;
431   return name_map[i].number;  
432 }
433
434
435 /**
436  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
437  *
438  * @param type number of a type to convert
439  * @return corresponding typestring, NULL on error
440  */
441 const char *
442 GNUNET_NAMESTORE_number_to_typename (uint32_t type)
443 {
444   unsigned int i;
445
446   i=0;
447   while ( (name_map[i].name != NULL) &&
448           (type != name_map[i].number) )
449     i++;
450   return name_map[i].name;  
451 }
452
453
454
455 /* end of namestore_common.c */