-additional namestore conversion APIs added
[oweals/gnunet.git] / src / namestore / namestore_common.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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   GNUNET_break (0); // not implemented
270   return NULL;
271 }
272
273
274 /**
275  * Convert human-readable version of a 'value' of a record to the binary
276  * representation.
277  *
278  * @param type type of the record
279  * @param s human-readable string
280  * @param data set to value in binary encoding (will be allocated)
281  * @param data_size set to number of bytes in data
282  * @return GNUNET_OK on success
283  */
284 int
285 GNUNET_NAMESTORE_string_to_value (uint32_t type,
286                                   const char *s,
287                                   void **data,
288                                   size_t *data_size)
289 {
290   struct in_addr value_a;
291   struct in6_addr value_aaaa;
292
293   switch (type)
294   {
295   case 0:
296     return GNUNET_SYSERR;
297     break;
298   case GNUNET_DNSPARSER_TYPE_A:
299     if (1 != inet_pton (AF_INET, s, &value_a))
300       return GNUNET_SYSERR;
301     *data = GNUNET_malloc (sizeof (struct in_addr));
302     memcpy (*data, &value_a, sizeof (value_a));
303     *data_size = sizeof (value_a);
304     break;
305   case GNUNET_DNSPARSER_TYPE_NS:
306     *data = GNUNET_strdup (s);
307     *data_size = strlen (s);
308     break;
309   case GNUNET_DNSPARSER_TYPE_CNAME:
310     *data = GNUNET_strdup (s);
311     *data_size = strlen (s);
312     break;
313   case GNUNET_DNSPARSER_TYPE_SOA:
314     GNUNET_break (0);
315     // FIXME
316     return GNUNET_SYSERR;
317   case GNUNET_DNSPARSER_TYPE_PTR:
318     GNUNET_break (0);
319     // FIXME
320     return GNUNET_SYSERR;
321   case GNUNET_DNSPARSER_TYPE_MX:
322     GNUNET_break (0);
323     // FIXME
324     return GNUNET_SYSERR;
325   case GNUNET_DNSPARSER_TYPE_TXT:
326     *data = GNUNET_strdup (s);
327     *data_size = strlen (s);
328     break;
329   case GNUNET_DNSPARSER_TYPE_AAAA:
330     if (1 != inet_pton (AF_INET6, s, &value_aaaa))    
331       return GNUNET_SYSERR;    
332     *data = GNUNET_malloc (sizeof (struct in6_addr));
333     memcpy (*data, &value_aaaa, sizeof (value_aaaa));
334     break;
335   case GNUNET_NAMESTORE_TYPE_PKEY:
336     GNUNET_break (0);
337     // FIXME
338     return GNUNET_SYSERR;
339   case GNUNET_NAMESTORE_TYPE_PSEU:
340     *data = GNUNET_strdup (s);
341     *data_size = strlen (s);
342     break;
343   default:
344     GNUNET_break (0);
345   }
346   return GNUNET_SYSERR;
347 }
348
349
350 static struct { 
351   const char *name; 
352   uint32_t number; 
353 } name_map[] = {
354   { "A", GNUNET_DNSPARSER_TYPE_A },
355   { "NS", GNUNET_DNSPARSER_TYPE_NS },
356   { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
357   { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
358   { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
359   { "MX", GNUNET_DNSPARSER_TYPE_MX },
360   { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
361   { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
362   { "PKEY",  GNUNET_NAMESTORE_TYPE_PKEY },
363   { "PSEU",  GNUNET_NAMESTORE_TYPE_PSEU },
364   { NULL, UINT32_MAX }
365 };
366
367
368 /**
369  * Convert a type name (i.e. "AAAA") to the corresponding number.
370  *
371  * @param typename name to convert
372  * @return corresponding number, UINT32_MAX on error
373  */
374 uint32_t
375 GNUNET_NAMESTORE_typename_to_number (const char *typename)
376 {
377   unsigned int i;
378
379   i=0;
380   while ( (name_map[i].name != NULL) &&
381           (0 != strcasecmp (typename, name_map[i].name)) )
382     i++;
383   return name_map[i].number;  
384 }
385
386
387 /**
388  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
389  *
390  * @param type number of a type to convert
391  * @return corresponding typestring, NULL on error
392  */
393 const char *
394 GNUNET_NAMESTORE_number_to_typename (uint32_t type)
395 {
396   unsigned int i;
397
398   i=0;
399   while ( (name_map[i].name != NULL) &&
400           (type != name_map[i].number) )
401     i++;
402   return name_map[i].name;  
403 }
404
405
406
407 /* end of namestore_common.c */