- file extension
[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 expire block expiration
202  * @param name the name
203  * @param rd record data
204  * @param rd_count number of records
205  *
206  * @return the signature
207  */
208 struct GNUNET_CRYPTO_RsaSignature *
209 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
210     struct GNUNET_TIME_Absolute expire,
211     const char *name,
212     const struct GNUNET_NAMESTORE_RecordData *rd,
213     unsigned int rd_count)
214 {
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);
218   size_t rd_ser_len;
219   size_t name_len;
220
221   struct GNUNET_TIME_AbsoluteNBO *expire_tmp;
222   char * name_tmp;
223   char * rd_tmp;
224   int res;
225
226   if (name == NULL)
227   {
228     GNUNET_break (0);
229     GNUNET_free (sig);
230     return NULL;
231   }
232   name_len = strlen (name) + 1;
233
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);
237
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);
247
248   res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
249
250   GNUNET_free (sig_purpose);
251
252   if (GNUNET_OK != res)
253   {
254     GNUNET_break (0);
255     GNUNET_free (sig);
256     return NULL;
257   }
258   return sig;
259 }
260
261
262 /**
263  * Convert the 'value' of a record to a string.
264  *
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
269  */
270 char *
271 GNUNET_NAMESTORE_value_to_string (uint32_t type,
272                                   const void *data,
273                                   size_t data_size)
274 {
275   char tmp[INET6_ADDRSTRLEN];
276
277   switch (type)
278   {
279   case 0:
280     return NULL;
281   case GNUNET_DNSPARSER_TYPE_A:
282     if (data_size != sizeof (struct in_addr))
283       return NULL;
284     if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
285       return NULL;
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:
292     GNUNET_break (0);
293     // FIXME
294     return NULL;
295   case GNUNET_DNSPARSER_TYPE_PTR:
296     GNUNET_break (0);
297     // FIXME
298     return NULL;
299   case GNUNET_DNSPARSER_TYPE_MX:
300     GNUNET_break (0);
301     // FIXME
302     return NULL;
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))
307       return NULL;
308     if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
309       return NULL;
310     return GNUNET_strdup (tmp);
311   case GNUNET_NAMESTORE_TYPE_PKEY:
312     if (data_size != sizeof (GNUNET_HashCode))
313       return NULL;
314     return GNUNET_strdup (GNUNET_h2s_full (data));
315   case GNUNET_NAMESTORE_TYPE_PSEU:
316     return GNUNET_strndup (data, data_size);
317   default:
318     GNUNET_break (0);
319   }
320   GNUNET_break (0); // not implemented
321   return NULL;
322 }
323
324
325 /**
326  * Convert human-readable version of a 'value' of a record to the binary
327  * representation.
328  *
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
334  */
335 int
336 GNUNET_NAMESTORE_string_to_value (uint32_t type,
337                                   const char *s,
338                                   void **data,
339                                   size_t *data_size)
340 {
341   struct in_addr value_a;
342   struct in6_addr value_aaaa;
343   GNUNET_HashCode pkey;
344
345   switch (type)
346   {
347   case 0:
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);
355     return GNUNET_OK;
356   case GNUNET_DNSPARSER_TYPE_NS:
357     *data = GNUNET_strdup (s);
358     *data_size = strlen (s);
359     return GNUNET_OK;
360   case GNUNET_DNSPARSER_TYPE_CNAME:
361     *data = GNUNET_strdup (s);
362     *data_size = strlen (s);
363     return GNUNET_OK;
364   case GNUNET_DNSPARSER_TYPE_SOA:
365     GNUNET_break (0);
366     // FIXME
367     return GNUNET_SYSERR;
368   case GNUNET_DNSPARSER_TYPE_PTR:
369     GNUNET_break (0);
370     // FIXME
371     return GNUNET_SYSERR;
372   case GNUNET_DNSPARSER_TYPE_MX:
373     GNUNET_break (0);
374     // FIXME
375     return GNUNET_SYSERR;
376   case GNUNET_DNSPARSER_TYPE_TXT:
377     *data = GNUNET_strdup (s);
378     *data_size = strlen (s);
379     return GNUNET_OK;
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));
385     return GNUNET_OK;
386   case GNUNET_NAMESTORE_TYPE_PKEY:
387     if (GNUNET_OK !=
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     return GNUNET_OK;
393   case GNUNET_NAMESTORE_TYPE_PSEU:
394     *data = GNUNET_strdup (s);
395     *data_size = strlen (s);
396     return GNUNET_OK;
397   default:
398     GNUNET_break (0);
399   }
400   return GNUNET_SYSERR;
401 }
402
403
404 static struct { 
405   const char *name; 
406   uint32_t number; 
407 } name_map[] = {
408   { "A", GNUNET_DNSPARSER_TYPE_A },
409   { "NS", GNUNET_DNSPARSER_TYPE_NS },
410   { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
411   { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
412   { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
413   { "MX", GNUNET_DNSPARSER_TYPE_MX },
414   { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
415   { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
416   { "PKEY",  GNUNET_NAMESTORE_TYPE_PKEY },
417   { "PSEU",  GNUNET_NAMESTORE_TYPE_PSEU },
418   { NULL, UINT32_MAX }
419 };
420
421
422 /**
423  * Convert a type name (i.e. "AAAA") to the corresponding number.
424  *
425  * @param typename name to convert
426  * @return corresponding number, UINT32_MAX on error
427  */
428 uint32_t
429 GNUNET_NAMESTORE_typename_to_number (const char *typename)
430 {
431   unsigned int i;
432
433   i=0;
434   while ( (name_map[i].name != NULL) &&
435           (0 != strcasecmp (typename, name_map[i].name)) )
436     i++;
437   return name_map[i].number;  
438 }
439
440
441 /**
442  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
443  *
444  * @param type number of a type to convert
445  * @return corresponding typestring, NULL on error
446  */
447 const char *
448 GNUNET_NAMESTORE_number_to_typename (uint32_t type)
449 {
450   unsigned int i;
451
452   i=0;
453   while ( (name_map[i].name != NULL) &&
454           (type != name_map[i].number) )
455     i++;
456   return name_map[i].name;  
457 }
458
459
460
461 /* end of namestore_common.c */