-proper data layout vpn record
[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
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 /**
71  * Convert a short hash to a string (for printing debug messages).
72  * This is one of the very few calls in the entire API that is
73  * NOT reentrant!
74  *
75  * @param hc the short hash code
76  * @return string form; will be overwritten by next call to GNUNET_h2s.
77  */
78 const char *
79 GNUNET_short_h2s (const struct GNUNET_CRYPTO_ShortHashCode * hc)
80 {
81   static struct GNUNET_CRYPTO_ShortHashAsciiEncoded ret;
82
83   GNUNET_CRYPTO_short_hash_to_enc (hc, &ret);
84   return (const char *) &ret;
85 }
86
87
88 /**
89  * Calculate how many bytes we will need to serialize the given
90  * records.
91  *
92  * @param rd_count number of records in the rd array
93  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
94  *
95  * @return the required size to serialize
96  *
97  */
98 size_t
99 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
100                                    const struct GNUNET_NAMESTORE_RecordData *rd)
101 {
102   unsigned int i;
103   size_t ret;
104
105   ret = sizeof (struct NetworkRecord) * rd_count;
106   for (i=0;i<rd_count;i++)
107   {
108     GNUNET_assert ((ret + rd[i].data_size) >= ret);
109     ret += rd[i].data_size;
110   }
111   return ret;  
112 }
113
114
115 /**
116  * Serialize the given records to the given destination buffer.
117  *
118  * @param rd_count number of records in the rd array
119  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
120  * @param dest_size size of the destination array
121  * @param dest where to write the result
122  *
123  * @return the size of serialized records
124  */
125 ssize_t
126 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
127                                     const struct GNUNET_NAMESTORE_RecordData *rd,
128                                     size_t dest_size,
129                                     char *dest)
130 {
131   struct NetworkRecord rec;
132   unsigned int i;
133   size_t off;
134   
135   off = 0;
136   for (i=0;i<rd_count;i++)
137   {
138     rec.expiration = GNUNET_TIME_absolute_hton (rd[i].expiration);
139     rec.data_size = htonl ((uint32_t) rd[i].data_size);
140     rec.record_type = htonl (rd[i].record_type);
141     rec.flags = htonl (rd[i].flags);
142     if (off + sizeof (rec) > dest_size)
143       return -1;
144     memcpy (&dest[off], &rec, sizeof (rec));
145     off += sizeof (rec);
146     if (off + rd[i].data_size > dest_size)
147       return -1;
148     memcpy (&dest[off], rd[i].data, rd[i].data_size);
149     off += rd[i].data_size;
150   }
151   return off;
152 }
153
154 /**
155  * Compares if two records are equal
156  *
157  * @param a record
158  * @param b record
159  *
160  * @return GNUNET_YES or GNUNET_NO
161  */
162 int
163 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
164                               const struct GNUNET_NAMESTORE_RecordData *b)
165 {
166   if ((a->record_type == b->record_type) &&
167       (a->expiration.abs_value == b->expiration.abs_value) &&
168       (a->data_size == b->data_size) &&
169       (0 == memcmp (a->data, b->data, a->data_size)))
170     return GNUNET_YES;
171   else
172     return GNUNET_NO;
173 }
174
175
176 /**
177  * Deserialize the given records to the given destination.
178  *
179  * @param len size of the serialized record data
180  * @param src the serialized record data
181  * @param rd_count number of records in the rd array
182  * @param dest where to put the data
183  *
184  * @return GNUNET_OK on success, GNUNET_SYSERR on error
185  */
186 int
187 GNUNET_NAMESTORE_records_deserialize (size_t len,
188                                       const char *src,
189                                       unsigned int rd_count,
190                                       struct GNUNET_NAMESTORE_RecordData *dest)
191 {
192   struct NetworkRecord rec;
193   unsigned int i;
194   size_t off;
195   
196   off = 0;
197   for (i=0;i<rd_count;i++)
198   {
199     if (off + sizeof (rec) > len)
200       return GNUNET_SYSERR;
201     memcpy (&rec, &src[off], sizeof (rec));
202     dest[i].expiration = GNUNET_TIME_absolute_ntoh (rec.expiration);
203     dest[i].data_size = ntohl ((uint32_t) rec.data_size);
204     dest[i].record_type = ntohl (rec.record_type);
205     dest[i].flags = ntohl (rec.flags);
206     off += sizeof (rec);
207
208     if (off + dest[i].data_size > len)
209       return GNUNET_SYSERR;
210     dest[i].data = &src[off];
211     off += dest[i].data_size;
212   }
213   return GNUNET_OK; 
214 }
215
216 /**
217  * Sign name and records
218  *
219  * @param key the private key
220  * @param expire block expiration
221  * @param name the name
222  * @param rd record data
223  * @param rd_count number of records
224  *
225  * @return the signature
226  */
227 struct GNUNET_CRYPTO_RsaSignature *
228 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
229     struct GNUNET_TIME_Absolute expire,
230     const char *name,
231     const struct GNUNET_NAMESTORE_RecordData *rd,
232     unsigned int rd_count)
233 {
234   struct GNUNET_CRYPTO_RsaSignature *sig = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignature));
235   struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
236   struct GNUNET_TIME_AbsoluteNBO expire_nbo = GNUNET_TIME_absolute_hton(expire);
237   size_t rd_ser_len;
238   size_t name_len;
239
240   struct GNUNET_TIME_AbsoluteNBO *expire_tmp;
241   char * name_tmp;
242   char * rd_tmp;
243   int res;
244
245   if (name == NULL)
246   {
247     GNUNET_break (0);
248     GNUNET_free (sig);
249     return NULL;
250   }
251   name_len = strlen (name) + 1;
252
253   rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
254   char rd_ser[rd_ser_len];
255   GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
256
257   sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + sizeof (struct GNUNET_TIME_AbsoluteNBO) + rd_ser_len + name_len);
258   sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
259   sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
260   expire_tmp = (struct GNUNET_TIME_AbsoluteNBO *) &sig_purpose[1];
261   name_tmp = (char *) &expire_tmp[1];
262   rd_tmp = &name_tmp[name_len];
263   memcpy (expire_tmp, &expire_nbo, sizeof (struct GNUNET_TIME_AbsoluteNBO));
264   memcpy (name_tmp, name, name_len);
265   memcpy (rd_tmp, rd_ser, rd_ser_len);
266
267   res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
268
269   GNUNET_free (sig_purpose);
270
271   if (GNUNET_OK != res)
272   {
273     GNUNET_break (0);
274     GNUNET_free (sig);
275     return NULL;
276   }
277   return sig;
278 }
279
280 /**
281  * Checks if a name is wellformed
282  *
283  * @param name the name to check
284  * @return GNUNET_OK on success, GNUNET_SYSERR on error
285  */
286 int
287 GNUNET_NAMESTORE_check_name (const char * name)
288 {
289   if (name == NULL)
290     return GNUNET_SYSERR;
291   if (strlen (name) > 63)
292     return GNUNET_SYSERR;
293   return GNUNET_OK;
294 }
295
296
297 /**
298  * Convert the 'value' of a record to a string.
299  *
300  * @param type type of the record
301  * @param data value in binary encoding
302  * @param data_size number of bytes in data
303  * @return NULL on error, otherwise human-readable representation of the value
304  */
305 char *
306 GNUNET_NAMESTORE_value_to_string (uint32_t type,
307                                   const void *data,
308                                   size_t data_size)
309 {
310   char tmp[INET6_ADDRSTRLEN];
311   struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
312   uint16_t mx_pref;
313   char* result;
314   char* soa_rname;
315   char* soa_mname;
316   uint32_t* soa_data;
317   uint32_t soa_serial;
318   uint32_t soa_refresh;
319   uint32_t soa_retry;
320   uint32_t soa_expire;
321   uint32_t soa_min;
322   uint32_t *af;
323   uint32_t *proto;
324   char* vpn_str;
325   struct GNUNET_HashCode *h_peer;
326   struct GNUNET_HashCode *h_serv;
327   struct GNUNET_CRYPTO_HashAsciiEncoded s_peer;
328   struct GNUNET_CRYPTO_HashAsciiEncoded s_serv;
329
330   switch (type)
331   {
332   case 0:
333     return NULL;
334   case GNUNET_DNSPARSER_TYPE_A:
335     if (data_size != sizeof (struct in_addr))
336       return NULL;
337     if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
338       return NULL;
339     return GNUNET_strdup (tmp);
340   case GNUNET_DNSPARSER_TYPE_NS:
341     return GNUNET_strndup (data, data_size);
342   case GNUNET_DNSPARSER_TYPE_CNAME:
343     return GNUNET_strndup (data, data_size);
344   case GNUNET_DNSPARSER_TYPE_SOA:
345     soa_rname = (char*)data;
346     soa_mname = (char*)data+strlen(soa_rname)+1;
347     soa_data = (uint32_t*)(soa_mname+strlen(soa_mname)+1);
348     soa_serial = ntohl(soa_data[0]);
349     soa_refresh = ntohl(soa_data[1]);
350     soa_retry = ntohl(soa_data[2]);
351     soa_expire = ntohl(soa_data[3]);
352     soa_min = ntohl(soa_data[4]);
353     if (GNUNET_asprintf(&result, "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu", 
354                      soa_rname, soa_mname,
355                      soa_serial, soa_refresh, soa_retry, soa_expire, soa_min))
356       return result;
357     else
358       return NULL;
359   case GNUNET_DNSPARSER_TYPE_PTR:
360     return GNUNET_strndup (data, data_size);
361   case GNUNET_DNSPARSER_TYPE_MX:
362     mx_pref = ntohs(*((uint16_t*)data));
363     if (GNUNET_asprintf(&result, "%hu,%s", mx_pref, data+sizeof(uint16_t))
364         != 0)
365       return result;
366     else
367       return NULL;
368   case GNUNET_DNSPARSER_TYPE_TXT:
369     return GNUNET_strndup (data, data_size);
370   case GNUNET_DNSPARSER_TYPE_AAAA:
371     if (data_size != sizeof (struct in6_addr))
372       return NULL;
373     if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
374       return NULL;
375     return GNUNET_strdup (tmp);
376   case GNUNET_NAMESTORE_TYPE_PKEY:
377     if (data_size != sizeof (struct GNUNET_CRYPTO_ShortHashCode))
378       return NULL;
379     GNUNET_CRYPTO_short_hash_to_enc (data,
380                                      &enc);
381     return GNUNET_strdup ((const char*) enc.short_encoding);
382   case GNUNET_NAMESTORE_TYPE_PSEU:
383     return GNUNET_strndup (data, data_size);
384   case GNUNET_NAMESTORE_TYPE_LEHO:
385     return GNUNET_strndup (data, data_size);
386   case GNUNET_NAMESTORE_TYPE_VPN:
387     af = (uint32_t*)data;
388     proto = (uint32_t*)((char*)af + sizeof (uint32_t));
389     h_peer = (struct GNUNET_HashCode*)((char*)proto + sizeof (struct GNUNET_HashCode));
390     h_serv = (struct GNUNET_HashCode*)((char*)h_peer + sizeof (struct GNUNET_HashCode));
391
392     GNUNET_CRYPTO_hash_to_enc (h_peer, &s_peer);
393     GNUNET_CRYPTO_hash_to_enc (h_serv, &s_serv);
394     if (GNUNET_OK != GNUNET_asprintf (&vpn_str, "%d:%d:%s:%s", af, proto, (char*)&s_peer, (char*)&s_serv))
395       return NULL;
396     return vpn_str;
397   default:
398     GNUNET_break (0);
399   }
400   GNUNET_break (0); // not implemented
401   return NULL;
402 }
403
404
405 /**
406  * Convert human-readable version of a 'value' of a record to the binary
407  * representation.
408  *
409  * @param type type of the record
410  * @param s human-readable string
411  * @param data set to value in binary encoding (will be allocated)
412  * @param data_size set to number of bytes in data
413  * @return GNUNET_OK on success
414  */
415 int
416 GNUNET_NAMESTORE_string_to_value (uint32_t type,
417                                   const char *s,
418                                   void **data,
419                                   size_t *data_size)
420 {
421   struct in_addr value_a;
422   struct in6_addr value_aaaa;
423   struct GNUNET_CRYPTO_ShortHashCode pkey;
424   uint16_t mx_pref;
425   uint16_t mx_pref_n;
426   uint32_t soa_data[5];
427   char result[253];
428   char soa_rname[63];
429   char soa_mname[63];
430   uint32_t soa_serial;
431   uint32_t soa_refresh;
432   uint32_t soa_retry;
433   uint32_t soa_expire;
434   uint32_t soa_min;
435   struct GNUNET_HashCode *h_peer;
436   struct GNUNET_HashCode *h_serv;
437   struct GNUNET_CRYPTO_HashAsciiEncoded s_peer;
438   struct GNUNET_CRYPTO_HashAsciiEncoded s_serv;
439   uint32_t* af;
440   uint32_t* proto;
441   
442   switch (type)
443   {
444   case 0:
445     return GNUNET_SYSERR;
446   case GNUNET_DNSPARSER_TYPE_A:
447     if (1 != inet_pton (AF_INET, s, &value_a))
448       return GNUNET_SYSERR;
449     *data = GNUNET_malloc (sizeof (struct in_addr));
450     memcpy (*data, &value_a, sizeof (value_a));
451     *data_size = sizeof (value_a);
452     return GNUNET_OK;
453   case GNUNET_DNSPARSER_TYPE_NS:
454     *data = GNUNET_strdup (s);
455     *data_size = strlen (s);
456     return GNUNET_OK;
457   case GNUNET_DNSPARSER_TYPE_CNAME:
458     *data = GNUNET_strdup (s);
459     *data_size = strlen (s);
460     return GNUNET_OK;
461   case GNUNET_DNSPARSER_TYPE_SOA:
462     
463     if (SSCANF(s, "rname=%s mname=%s %u,%u,%u,%u,%u",
464                soa_rname, soa_mname,
465                &soa_serial, &soa_refresh, &soa_retry, &soa_expire, &soa_min) 
466         != 7)
467       return GNUNET_SYSERR;
468     
469     *data_size = sizeof (soa_data)+strlen(soa_rname)+strlen(soa_mname)+2;
470     *data = GNUNET_malloc (*data_size);
471     soa_data[0] = htonl(soa_serial);
472     soa_data[1] = htonl(soa_refresh);
473     soa_data[2] = htonl(soa_retry);
474     soa_data[3] = htonl(soa_expire);
475     soa_data[4] = htonl(soa_min);
476     strcpy(*data, soa_rname);
477     strcpy(*data+strlen(*data)+1, soa_mname);
478     memcpy(*data+strlen(*data)+1+strlen(soa_mname)+1,
479            soa_data, sizeof(soa_data));
480     return GNUNET_OK;
481
482   case GNUNET_DNSPARSER_TYPE_PTR:
483     *data = GNUNET_strdup (s);
484     *data_size = strlen (s);
485     return GNUNET_OK;
486   case GNUNET_DNSPARSER_TYPE_MX:
487     if (SSCANF(s, "%hu,%s", &mx_pref, result) != 2)
488       return GNUNET_SYSERR;
489     *data_size = sizeof (uint16_t)+strlen(result)+1;
490     *data = GNUNET_malloc (*data_size);
491     mx_pref_n = htons(mx_pref);
492     memcpy(*data, &mx_pref_n, sizeof (uint16_t));
493     strcpy((*data)+sizeof (uint16_t), result);
494     return GNUNET_OK;
495   case GNUNET_DNSPARSER_TYPE_TXT:
496     *data = GNUNET_strdup (s);
497     *data_size = strlen (s);
498     return GNUNET_OK;
499   case GNUNET_DNSPARSER_TYPE_AAAA:
500     if (1 != inet_pton (AF_INET6, s, &value_aaaa))    
501       return GNUNET_SYSERR;    
502     *data = GNUNET_malloc (sizeof (struct in6_addr));
503     *data_size = sizeof (struct in6_addr);
504     memcpy (*data, &value_aaaa, sizeof (value_aaaa));
505     return GNUNET_OK;
506   case GNUNET_NAMESTORE_TYPE_PKEY:
507     if (GNUNET_OK !=
508         GNUNET_CRYPTO_short_hash_from_string (s, &pkey))
509       return GNUNET_SYSERR;
510     *data = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
511     memcpy (*data, &pkey, sizeof (pkey));
512     *data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
513     return GNUNET_OK;
514   case GNUNET_NAMESTORE_TYPE_PSEU:
515     *data = GNUNET_strdup (s);
516     *data_size = strlen (s);
517     return GNUNET_OK;
518   case GNUNET_NAMESTORE_TYPE_LEHO:
519     *data = GNUNET_strdup (s);
520     *data_size = strlen (s);
521     return GNUNET_OK;
522   case GNUNET_NAMESTORE_TYPE_VPN:
523     
524     *data_size = sizeof (uint32_t) * 2;
525     *data_size += sizeof (struct GNUNET_HashCode) * 2;
526     *data = GNUNET_malloc (*data_size);
527     af = (uint32_t*)(*data);
528     proto = (uint32_t*) ((char*)af + sizeof (uint32_t));
529     h_peer = (struct GNUNET_HashCode*)((char*)proto + sizeof (uint32_t));
530     h_serv = (struct GNUNET_HashCode*)((char*)h_peer + sizeof (struct GNUNET_HashCode));
531     
532     if (4 != SSCANF (s,"%d:%d:%s:%s",
533                      af, proto, (char*)&s_peer, (char*)&s_serv))
534     {
535       return GNUNET_SYSERR;
536     }
537     if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_peer, h_peer)) ||
538         (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_serv, h_serv)))
539     {
540       GNUNET_free (*data);
541       return GNUNET_SYSERR;
542     }
543     return GNUNET_OK;
544   default:
545     GNUNET_break (0);
546   }
547   return GNUNET_SYSERR;
548 }
549
550
551 static struct { 
552   const char *name; 
553   uint32_t number; 
554 } name_map[] = {
555   { "A", GNUNET_DNSPARSER_TYPE_A },
556   { "NS", GNUNET_DNSPARSER_TYPE_NS },
557   { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
558   { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
559   { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
560   { "MX", GNUNET_DNSPARSER_TYPE_MX },
561   { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
562   { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
563   { "PKEY",  GNUNET_NAMESTORE_TYPE_PKEY },
564   { "PSEU",  GNUNET_NAMESTORE_TYPE_PSEU },
565   { "LEHO",  GNUNET_NAMESTORE_TYPE_LEHO },
566   { NULL, UINT32_MAX }
567 };
568
569
570 /**
571  * Convert a type name (i.e. "AAAA") to the corresponding number.
572  *
573  * @param typename name to convert
574  * @return corresponding number, UINT32_MAX on error
575  */
576 uint32_t
577 GNUNET_NAMESTORE_typename_to_number (const char *typename)
578 {
579   unsigned int i;
580
581   i=0;
582   while ( (name_map[i].name != NULL) &&
583           (0 != strcasecmp (typename, name_map[i].name)) )
584     i++;
585   return name_map[i].number;  
586 }
587
588
589 /**
590  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
591  *
592  * @param type number of a type to convert
593  * @return corresponding typestring, NULL on error
594  */
595 const char *
596 GNUNET_NAMESTORE_number_to_typename (uint32_t type)
597 {
598   unsigned int i;
599
600   i=0;
601   while ( (name_map[i].name != NULL) &&
602           (type != name_map[i].number) )
603     i++;
604   return name_map[i].name;  
605 }
606
607
608
609 /* end of namestore_common.c */