- create test
[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 "namestore.h"
35 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
36
37 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
38
39
40 /**
41  * Internal format of a record in the serialized form.
42  */
43 struct NetworkRecord
44 {
45
46   /**
47    * Expiration time for the DNS record.
48    */
49   struct GNUNET_TIME_AbsoluteNBO expiration;
50
51   /**
52    * Number of bytes in 'data', network byte order.
53    */
54   uint32_t data_size;
55
56   /**
57    * Type of the GNS/DNS record, network byte order.
58    */
59   uint32_t record_type;
60
61   /**
62    * Flags for the record, network byte order.
63    */
64   uint32_t flags;
65   
66 };
67
68 /**
69  * Calculate how many bytes we will need to serialize the given
70  * records.
71  *
72  * @param rd_count number of records in the rd array
73  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
74  *
75  * @return the required size to serialize
76  *
77  */
78 size_t
79 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
80                                    const struct GNUNET_NAMESTORE_RecordData *rd)
81 {
82   unsigned int i;
83   size_t ret;
84
85   ret = sizeof (struct NetworkRecord) * rd_count;
86   for (i=0;i<rd_count;i++)
87   {
88     GNUNET_assert (ret + rd[i].data_size >= ret);
89     ret += rd[i].data_size;
90   }
91   return ret;  
92 }
93
94
95 /**
96  * Serialize the given records to the given destination buffer.
97  *
98  * @param rd_cound number of records in the rd array
99  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
100  * @param dest_size size of the destination array
101  * @param dest where to write the result
102  *
103  * @return the size of serialized records
104  */
105 ssize_t
106 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
107                                     const struct GNUNET_NAMESTORE_RecordData *rd,
108                                     size_t dest_size,
109                                     char *dest)
110 {
111   struct NetworkRecord rec;
112   unsigned int i;
113   size_t off;
114   
115   off = 0;
116   for (i=0;i<rd_count;i++)
117   {
118     rec.expiration = GNUNET_TIME_absolute_hton (rd[i].expiration);
119     rec.data_size = htonl ((uint32_t) rd[i].data_size);
120     rec.record_type = htonl (rd[i].record_type);
121     rec.flags = htonl (rd[i].flags);
122     if (off + sizeof (rec) > dest_size)
123       return -1;
124     memcpy (&dest[off], &rec, sizeof (rec));
125     off += sizeof (rec);
126     if (off + rd[i].data_size > dest_size)
127       return -1;
128     memcpy (&dest[off], rd[i].data, rd[i].data_size);
129     off += rd[i].data_size;
130   }
131   return off;
132 }
133
134 /**
135  * Compares if two records are equal
136  *
137  * @param a record
138  * @param b record
139  *
140  * @return GNUNET_YES or GNUNET_NO
141  */
142 int
143 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
144                               const struct GNUNET_NAMESTORE_RecordData *b)
145 {
146   if ((a->record_type == b->record_type) &&
147       (a->expiration.abs_value == b->expiration.abs_value) &&
148       (a->data_size == b->data_size) &&
149       (0 == memcmp (a->data, b->data, a->data_size)))
150     return GNUNET_YES;
151   else
152     return GNUNET_NO;
153 }
154
155
156 /**
157  * Deserialize the given records to the given destination.
158  *
159  * @param len size of the serialized record data
160  * @param src the serialized record data
161  * @param rd_cound number of records in the rd array
162  * @param dest where to put the data
163  *
164  * @return GNUNET_OK on success, GNUNET_SYSERR on error
165  */
166 int
167 GNUNET_NAMESTORE_records_deserialize (size_t len,
168                                       const char *src,
169                                       unsigned int rd_count,
170                                       struct GNUNET_NAMESTORE_RecordData *dest)
171 {
172   struct NetworkRecord rec;
173   unsigned int i;
174   size_t off;
175   
176   off = 0;
177   for (i=0;i<rd_count;i++)
178   {
179     if (off + sizeof (rec) > len)
180       return GNUNET_SYSERR;
181     memcpy (&rec, &src[off], sizeof (rec));
182     dest[i].expiration = GNUNET_TIME_absolute_ntoh (rec.expiration);
183     dest[i].data_size = ntohl ((uint32_t) rec.data_size);
184     dest[i].record_type = ntohl (rec.record_type);
185     dest[i].flags = ntohl (rec.flags);
186     off += sizeof (rec);
187
188     if (off + sizeof (dest[i].data_size) > len)
189       return GNUNET_SYSERR;
190     dest[i].data = &src[off];
191     off += dest[i].data_size;
192   }
193   return GNUNET_OK; 
194 }
195
196 struct GNUNET_CRYPTO_RsaSignature *
197 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key, const char *name, struct GNUNET_NAMESTORE_RecordData *rd, unsigned int rd_count)
198 {
199   struct GNUNET_CRYPTO_RsaSignature *sig = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignature));
200   struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
201   size_t rd_ser_len;
202   size_t name_len;
203   char * name_tmp;
204   char * rd_tmp;
205   int res;
206
207   if (name == NULL)
208   {
209     GNUNET_break (0);
210     GNUNET_free (sig);
211     return NULL;
212   }
213   name_len = strlen (name) + 1;
214
215   rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
216   char rd_ser[rd_ser_len];
217   GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
218
219   sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
220
221   sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
222   sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
223   name_tmp = (char *) &sig_purpose[1];
224   rd_tmp = &name_tmp[name_len];
225   memcpy (name_tmp, name, name_len);
226   memcpy (rd_tmp, rd_ser, rd_ser_len);
227
228   res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
229
230   GNUNET_free (sig_purpose);
231
232   if (GNUNET_OK != res)
233   {
234     GNUNET_break (0);
235     GNUNET_free (sig);
236     return NULL;
237   }
238   return sig;
239 }
240
241 /* end of namestore_api.c */