2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file namestore/namestore_common.c
23 * @brief API to access the NAMESTORE service
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
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
36 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
40 * Internal format of a record in the serialized form.
46 * Expiration time for the DNS record.
48 struct GNUNET_TIME_AbsoluteNBO expiration;
51 * Number of bytes in 'data', network byte order.
56 * Type of the GNS/DNS record, network byte order.
61 * Flags for the record, network byte order.
68 * Calculate how many bytes we will need to serialize the given
72 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
73 const struct GNUNET_NAMESTORE_RecordData *rd)
78 ret = sizeof (struct NetworkRecord) * rd_count;
79 for (i=0;i<rd_count;i++)
81 GNUNET_assert (ret + rd[i].data_size >= ret);
82 ret += rd[i].data_size;
89 * Serialize the given records to the given destination buffer.
92 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
93 const struct GNUNET_NAMESTORE_RecordData *rd,
97 struct NetworkRecord rec;
102 for (i=0;i<rd_count;i++)
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)
110 memcpy (&dest[off], &rec, sizeof (rec));
112 if (off + rd[i].data_size > dest_size)
114 memcpy (&dest[off], rd[i].data, rd[i].data_size);
115 off += rd[i].data_size;
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
129 GNUNET_NAMESTORE_records_deserialize (size_t len,
131 unsigned int rd_count,
132 struct GNUNET_NAMESTORE_RecordData *dest)
134 struct NetworkRecord rec;
139 for (i=0;i<rd_count;i++)
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);
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;
163 * Serialize an array of GNUNET_NAMESTORE_RecordData *rd to transmit over the
166 * @param dest where to write the serialized data
167 * @param rd_count number of elements in array
170 * @return number of bytes written to destination dest
173 GNUNET_NAMESTORE_records_serialize (char ** dest,
174 unsigned int rd_count,
175 const struct GNUNET_NAMESTORE_RecordData *rd)
178 struct GNUNET_NAMESTORE_NetworkRecord nr;
183 GNUNET_assert (rd != NULL);
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);
188 /* figure out total len required */
189 for (c = 0; c < rd_count; c ++)
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;
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serializing %i records with total length of %llu\n", rd_count, total_len);
197 (*dest) = GNUNET_malloc (total_len);
203 for (c = 0; c < rd_count; c ++)
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serialized record [%i]: data_size %i\n", c,rd[c].data_size);
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);
216 memcpy (&d[offset], rd[c].data, rd[c].data_size);
217 offset += rd[c].data_size;
220 GNUNET_assert (offset == total_len);
225 GNUNET_NAMESTORE_records_free (unsigned int rd_count, struct GNUNET_NAMESTORE_RecordData *rd)
228 if ((rd == NULL) || (rd_count == 0))
231 for (c = 0; c < rd_count; c++)
232 GNUNET_free_non_null ((void *) rd[c].data);
238 * Deserialize an array of GNUNET_NAMESTORE_RecordData *rd after transmission
241 * @param source where to read the data to deserialize
242 * @param rd_count number of elements in array
245 * @return number of elements deserialized
248 GNUNET_NAMESTORE_records_deserialize ( struct GNUNET_NAMESTORE_RecordData **dest, char *src, size_t len)
250 struct GNUNET_NAMESTORE_NetworkRecord * nr;
251 struct GNUNET_NAMESTORE_RecordData *d = (*dest);
267 nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &src[offset];
268 offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
270 data_size = ntohl (nr->data_size);
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Datasize record[%i]: %u\n", elements, data_size);
283 GNUNET_assert (len == offset);
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deserializing %i records with total length of %u\n", elements, len);
286 (*dest) = GNUNET_malloc (elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
290 for (c = 0; c < elements; c++)
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);
302 offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
303 memcpy((char *) d[c].data, &src[offset], d[c].data_size);
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);
308 GNUNET_assert(offset == len);
315 /* end of namestore_api.c */