- comments
[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_arm_service.h"
32 #include "gnunet_namestore_service.h"
33 #include "namestore.h"
34 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
35
36 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
37
38
39 /**
40  * Internal format of a record in the serialized form.
41  */
42 struct NetworkRecord
43 {
44
45   /**
46    * Expiration time for the DNS record.
47    */
48   struct GNUNET_TIME_AbsoluteNBO expiration;
49
50   /**
51    * Number of bytes in 'data', network byte order.
52    */
53   uint32_t data_size;
54
55   /**
56    * Type of the GNS/DNS record, network byte order.
57    */
58   uint32_t record_type;
59
60   /**
61    * Flags for the record, network byte order.
62    */
63   uint32_t flags;
64   
65 };
66
67 /**
68  * Calculate how many bytes we will need to serialize the given
69  * records.
70  *
71  * @param rd_count number of records in the rd array
72  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
73  *
74  * @return the required size to serialize
75  *
76  */
77 size_t
78 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
79                                    const struct GNUNET_NAMESTORE_RecordData *rd)
80 {
81   unsigned int i;
82   size_t ret;
83
84   ret = sizeof (struct NetworkRecord) * rd_count;
85   for (i=0;i<rd_count;i++)
86   {
87     GNUNET_assert (ret + rd[i].data_size >= ret);
88     ret += rd[i].data_size;
89   }
90   return ret;  
91 }
92
93
94 /**
95  * Serialize the given records to the given destination buffer.
96  *
97  * @param rd_cound number of records in the rd array
98  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
99  * @param dest_size size of the destination array
100  * @param dest where to write the result
101  *
102  * @return the size of serialized records
103  */
104 ssize_t
105 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
106                                     const struct GNUNET_NAMESTORE_RecordData *rd,
107                                     size_t dest_size,
108                                     char *dest)
109 {
110   struct NetworkRecord rec;
111   unsigned int i;
112   size_t off;
113   
114   off = 0;
115   for (i=0;i<rd_count;i++)
116   {
117     rec.expiration = GNUNET_TIME_absolute_hton (rd[i].expiration);
118     rec.data_size = htonl ((uint32_t) rd[i].data_size);
119     rec.record_type = htonl (rd[i].record_type);
120     rec.flags = htonl (rd[i].flags);
121     if (off + sizeof (rec) > dest_size)
122       return -1;
123     memcpy (&dest[off], &rec, sizeof (rec));
124     off += sizeof (rec);
125     if (off + rd[i].data_size > dest_size)
126       return -1;
127     memcpy (&dest[off], rd[i].data, rd[i].data_size);
128     off += rd[i].data_size;
129   }
130   return off;
131 }
132
133
134 /**
135  * Deserialize the given records to the given destination.
136  *
137  * @param len size of the serialized record data
138  * @param src the serialized record data
139  * @param rd_cound number of records in the rd array
140  * @param dest where to put the data
141  *
142  * @return GNUNET_OK on success, GNUNET_SYSERR on error
143  */
144 int
145 GNUNET_NAMESTORE_records_deserialize (size_t len,
146                                       const char *src,
147                                       unsigned int rd_count,
148                                       struct GNUNET_NAMESTORE_RecordData *dest)
149 {
150   struct NetworkRecord rec;
151   unsigned int i;
152   size_t off;
153   
154   off = 0;
155   for (i=0;i<rd_count;i++)
156   {
157     if (off + sizeof (rec) > len)
158       return GNUNET_SYSERR;
159     memcpy (&rec, &src[off], sizeof (rec));
160     dest[i].expiration = GNUNET_TIME_absolute_ntoh (rec.expiration);
161     dest[i].data_size = ntohl ((uint32_t) rec.data_size);
162     dest[i].record_type = ntohl (rec.record_type);
163     dest[i].flags = ntohl (rec.flags);
164     off += sizeof (rec);
165
166     if (off + sizeof (dest[i].data_size) > len)
167       return GNUNET_SYSERR;
168     dest[i].data = &src[off];
169     off += dest[i].data_size;
170   }
171   return GNUNET_OK; 
172 }
173
174
175
176 #if 0
177
178 /**
179  * Serialize an array of GNUNET_NAMESTORE_RecordData *rd to transmit over the
180  * network
181  *
182  * @param dest where to write the serialized data
183  * @param rd_count number of elements in array
184  * @param rd array
185  *
186  * @return number of bytes written to destination dest
187  */
188 size_t
189 GNUNET_NAMESTORE_records_serialize (char ** dest,
190                              unsigned int rd_count,
191                              const struct GNUNET_NAMESTORE_RecordData *rd)
192 {
193   //size_t len = 0;
194   struct GNUNET_NAMESTORE_NetworkRecord nr;
195   char * d = (*dest);
196   int c = 0;
197   int offset;
198
199   GNUNET_assert (rd != NULL);
200
201   size_t total_len = rd_count * sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
202   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Struct size: %u\n", total_len);
203
204   /* figure out total len required */
205   for (c = 0; c < rd_count; c ++)
206   {
207     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data size record[%i] : %u\n", c, rd[c].data_size);
208     total_len += rd[c].data_size;
209   }
210
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serializing %i records with total length of %llu\n", rd_count, total_len);
212
213   (*dest) = GNUNET_malloc (total_len);
214   d = (*dest);
215
216   /* copy records */
217   offset = 0;
218
219   for (c = 0; c < rd_count; c ++)
220   {
221     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serialized record [%i]: data_size %i\n", c,rd[c].data_size);
222
223     // nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &d[offset];
224     nr.data_size = htonl (rd[c].data_size);
225     nr.flags = htonl (rd[c].flags);
226     nr.record_type = htonl (rd[c].record_type);
227     nr.expiration = GNUNET_TIME_absolute_hton(rd[c].expiration);
228     memcpy (&d[offset], &nr, sizeof (nr));
229     offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
230
231     /*put data here */
232     memcpy (&d[offset], rd[c].data, rd[c].data_size);
233     offset += rd[c].data_size;
234   }
235
236   GNUNET_assert (offset == total_len);
237   return total_len;
238 }
239
240 void
241 GNUNET_NAMESTORE_records_free (unsigned int rd_count, struct GNUNET_NAMESTORE_RecordData *rd)
242 {
243   int c;
244   if ((rd == NULL) || (rd_count == 0))
245     return;
246
247   for (c = 0; c < rd_count; c++)
248     GNUNET_free_non_null ((void *) rd[c].data);
249   GNUNET_free (rd);
250 }
251
252
253 /**
254  * Deserialize an array of GNUNET_NAMESTORE_RecordData *rd after transmission
255  * over the network
256  *
257  * @param source where to read the data to deserialize
258  * @param rd_count number of elements in array
259  * @param rd array
260  *
261  * @return number of elements deserialized
262  */
263 int
264 GNUNET_NAMESTORE_records_deserialize ( struct GNUNET_NAMESTORE_RecordData **dest, char *src, size_t len)
265 {
266   struct GNUNET_NAMESTORE_NetworkRecord * nr;
267   struct GNUNET_NAMESTORE_RecordData *d = (*dest);
268   int elements;
269   size_t offset;
270   uint32_t data_size;
271   int c;
272
273   if (len == 0)
274   {
275     (*dest) = NULL;
276     return 0;
277   }
278
279   offset = 0;
280   elements = 0;
281   while (offset < len)
282   {
283     nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &src[offset];
284     offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
285
286     data_size = ntohl (nr->data_size);
287     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Datasize record[%i]: %u\n", elements, data_size);
288     offset += data_size;
289     elements ++;
290   }
291
292   if (elements == 0)
293   {
294     (*dest) = NULL;
295     return 0;
296   }
297
298
299   GNUNET_assert (len == offset);
300   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deserializing %i records with total length of %u\n", elements, len);
301
302   (*dest) = GNUNET_malloc (elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
303   d = (*dest);
304
305   offset = 0;
306   for (c = 0; c < elements; c++)
307   {
308     nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &src[offset];
309     d[c].expiration = GNUNET_TIME_absolute_ntoh(nr->expiration);
310     d[c].record_type = ntohl (nr->record_type);
311     d[c].flags = ntohl (nr->flags);
312     d[c].data_size = ntohl (nr->data_size);
313     if (d[c].data_size > 0)
314       d[c].data = GNUNET_malloc (d[c].data_size);
315     else
316       d[c].data = NULL;
317
318     offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
319     memcpy((char *) d[c].data, &src[offset], d[c].data_size);
320
321     offset += d[c].data_size;
322     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deserialized record[%i] /w data_size %i\n", c, d[c].data_size);
323   }
324   GNUNET_assert(offset == len);
325
326   return elements;
327 }
328
329 #endif
330
331 /* end of namestore_api.c */