-stuff
[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 size_t
72 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
73                                    const struct GNUNET_NAMESTORE_RecordData *rd)
74 {
75   unsigned int i;
76   size_t ret;
77
78   ret = sizeof (struct NetworkRecord) * rd_count;
79   for (i=0;i<rd_count;i++)
80   {
81     GNUNET_assert (ret + rd[i].data_size >= ret);
82     ret += rd[i].data_size;
83   }
84   return ret;  
85 }
86
87
88 /**
89  * Serialize the given records to the given destination buffer.
90  */
91 ssize_t
92 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
93                                     const struct GNUNET_NAMESTORE_RecordData *rd,
94                                     size_t dest_size,
95                                     char *dest)
96 {
97   struct NetworkRecord rec;
98   unsigned int i;
99   size_t off;
100   
101   off = 0;
102   for (i=0;i<rd_count;i++)
103   {
104     rec.expiration = GNUNET_TIME_absolute_hton (rd[i].expiration);
105     rec.data_size = htonl ((uint32_t) rd[i].data_size);
106     rec.record_type = htonl (rd[i].record_type);
107     rec.flags = htonl (rd[i].flags);
108     if (off + sizeof (rec) > dest_size)
109       return -1;
110     memcpy (&dest[off], &rec, sizeof (rec));
111     off += sizeof (rec);
112     if (off + rd[i].data_size > dest_size)
113       return -1;
114     memcpy (&dest[off], rd[i].data, rd[i].data_size);
115     off += rd[i].data_size;
116   }
117   return off;
118 }
119
120
121 /**
122  * @param rd_count expected number of records in 'src'
123  * @param dest array of 'rd_count' entries for storing record data;
124  *         'data' values in 'dest' will point into 'src' and will thus
125  *         become invalid if 'src' is modified
126  * @return GNUNET_OK on success, GNUNET_SYSERR on error
127  */
128 int
129 GNUNET_NAMESTORE_records_deserialize (size_t len,
130                                       const char *src,
131                                       unsigned int rd_count,
132                                       struct GNUNET_NAMESTORE_RecordData *dest)
133 {
134   struct NetworkRecord rec;
135   unsigned int i;
136   size_t off;
137   
138   off = 0;
139   for (i=0;i<rd_count;i++)
140   {
141     if (off + sizeof (rec) > len)
142       return GNUNET_SYSERR;
143     memcpy (&rec, &src[off], sizeof (rec));
144     dest[i].expiration = GNUNET_TIME_absolute_ntoh (rec.expiration);
145     dest[i].data_size = ntohl ((uint32_t) rec.data_size);
146     dest[i].record_type = ntohl (rec.record_type);
147     dest[i].flags = ntohl (rec.flags);
148     off += sizeof (rec);
149
150     if (off + sizeof (dest[i].data_size) > len)
151       return GNUNET_SYSERR;
152     dest[i].data = &src[off];
153     off += dest[i].data_size;
154   }
155   return GNUNET_OK; 
156 }
157
158
159
160 #if 0
161
162 /**
163  * Serialize an array of GNUNET_NAMESTORE_RecordData *rd to transmit over the
164  * network
165  *
166  * @param dest where to write the serialized data
167  * @param rd_count number of elements in array
168  * @param rd array
169  *
170  * @return number of bytes written to destination dest
171  */
172 size_t
173 GNUNET_NAMESTORE_records_serialize (char ** dest,
174                              unsigned int rd_count,
175                              const struct GNUNET_NAMESTORE_RecordData *rd)
176 {
177   //size_t len = 0;
178   struct GNUNET_NAMESTORE_NetworkRecord nr;
179   char * d = (*dest);
180   int c = 0;
181   int offset;
182
183   GNUNET_assert (rd != NULL);
184
185   size_t total_len = rd_count * sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
186   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Struct size: %u\n", total_len);
187
188   /* figure out total len required */
189   for (c = 0; c < rd_count; c ++)
190   {
191     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data size record[%i] : %u\n", c, rd[c].data_size);
192     total_len += rd[c].data_size;
193   }
194
195   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serializing %i records with total length of %llu\n", rd_count, total_len);
196
197   (*dest) = GNUNET_malloc (total_len);
198   d = (*dest);
199
200   /* copy records */
201   offset = 0;
202
203   for (c = 0; c < rd_count; c ++)
204   {
205     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serialized record [%i]: data_size %i\n", c,rd[c].data_size);
206
207     // nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &d[offset];
208     nr.data_size = htonl (rd[c].data_size);
209     nr.flags = htonl (rd[c].flags);
210     nr.record_type = htonl (rd[c].record_type);
211     nr.expiration = GNUNET_TIME_absolute_hton(rd[c].expiration);
212     memcpy (&d[offset], &nr, sizeof (nr));
213     offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
214
215     /*put data here */
216     memcpy (&d[offset], rd[c].data, rd[c].data_size);
217     offset += rd[c].data_size;
218   }
219
220   GNUNET_assert (offset == total_len);
221   return total_len;
222 }
223
224 void
225 GNUNET_NAMESTORE_records_free (unsigned int rd_count, struct GNUNET_NAMESTORE_RecordData *rd)
226 {
227   int c;
228   if ((rd == NULL) || (rd_count == 0))
229     return;
230
231   for (c = 0; c < rd_count; c++)
232     GNUNET_free_non_null ((void *) rd[c].data);
233   GNUNET_free (rd);
234 }
235
236
237 /**
238  * Deserialize an array of GNUNET_NAMESTORE_RecordData *rd after transmission
239  * over the network
240  *
241  * @param source where to read the data to deserialize
242  * @param rd_count number of elements in array
243  * @param rd array
244  *
245  * @return number of elements deserialized
246  */
247 int
248 GNUNET_NAMESTORE_records_deserialize ( struct GNUNET_NAMESTORE_RecordData **dest, char *src, size_t len)
249 {
250   struct GNUNET_NAMESTORE_NetworkRecord * nr;
251   struct GNUNET_NAMESTORE_RecordData *d = (*dest);
252   int elements;
253   size_t offset;
254   uint32_t data_size;
255   int c;
256
257   if (len == 0)
258   {
259     (*dest) = NULL;
260     return 0;
261   }
262
263   offset = 0;
264   elements = 0;
265   while (offset < len)
266   {
267     nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &src[offset];
268     offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
269
270     data_size = ntohl (nr->data_size);
271     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Datasize record[%i]: %u\n", elements, data_size);
272     offset += data_size;
273     elements ++;
274   }
275
276   if (elements == 0)
277   {
278     (*dest) = NULL;
279     return 0;
280   }
281
282
283   GNUNET_assert (len == offset);
284   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deserializing %i records with total length of %u\n", elements, len);
285
286   (*dest) = GNUNET_malloc (elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
287   d = (*dest);
288
289   offset = 0;
290   for (c = 0; c < elements; c++)
291   {
292     nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &src[offset];
293     d[c].expiration = GNUNET_TIME_absolute_ntoh(nr->expiration);
294     d[c].record_type = ntohl (nr->record_type);
295     d[c].flags = ntohl (nr->flags);
296     d[c].data_size = ntohl (nr->data_size);
297     if (d[c].data_size > 0)
298       d[c].data = GNUNET_malloc (d[c].data_size);
299     else
300       d[c].data = NULL;
301
302     offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
303     memcpy((char *) d[c].data, &src[offset], d[c].data_size);
304
305     offset += d[c].data_size;
306     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deserialized record[%i] /w data_size %i\n", c, d[c].data_size);
307   }
308   GNUNET_assert(offset == len);
309
310   return elements;
311 }
312
313 #endif
314
315 /* end of namestore_api.c */