-fixing sparc fun
[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  * Serialize an array of GNUNET_NAMESTORE_RecordData *rd to transmit over the
39  * network
40  *
41  * @param dest where to write the serialized data
42  * @param rd_count number of elements in array
43  * @param rd array
44  *
45  * @return number of bytes written to destination dest
46  */
47 size_t
48 GNUNET_NAMESTORE_records_serialize (char ** dest,
49                              unsigned int rd_count,
50                              const struct GNUNET_NAMESTORE_RecordData *rd)
51 {
52   //size_t len = 0;
53   struct GNUNET_NAMESTORE_NetworkRecord nr;
54   char * d = (*dest);
55   int c = 0;
56   int offset;
57
58   GNUNET_assert (rd != NULL);
59
60   size_t total_len = rd_count * sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
61   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Struct size: %u\n", total_len);
62
63   /* figure out total len required */
64   for (c = 0; c < rd_count; c ++)
65   {
66     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data size record[%i] : %u\n", c, rd[c].data_size);
67     total_len += rd[c].data_size;
68   }
69
70   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serializing %i records with total length of %llu\n", rd_count, total_len);
71
72   (*dest) = GNUNET_malloc (total_len);
73   d = (*dest);
74
75   /* copy records */
76   offset = 0;
77
78   for (c = 0; c < rd_count; c ++)
79   {
80     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serialized record [%i]: data_size %i\n", c,rd[c].data_size);
81
82     // nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &d[offset];
83     nr.data_size = htonl (rd[c].data_size);
84     nr.flags = htonl (rd[c].flags);
85     nr.record_type = htonl (rd[c].record_type);
86     nr.expiration = GNUNET_TIME_absolute_hton(rd[c].expiration);
87     memcpy (&d[offset], &nr, sizeof (nr));
88     offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
89
90     /*put data here */
91     memcpy (&d[offset], rd[c].data, rd[c].data_size);
92     offset += rd[c].data_size;
93   }
94
95   GNUNET_assert (offset == total_len);
96   return total_len;
97 }
98
99 void
100 GNUNET_NAMESTORE_records_free (unsigned int rd_count, struct GNUNET_NAMESTORE_RecordData *rd)
101 {
102   int c;
103   if ((rd == NULL) || (rd_count == 0))
104     return;
105
106   for (c = 0; c < rd_count; c++)
107     GNUNET_free_non_null ((void *) rd[c].data);
108   GNUNET_free (rd);
109 }
110
111
112 /**
113  * Deserialize an array of GNUNET_NAMESTORE_RecordData *rd after transmission
114  * over the network
115  *
116  * @param source where to read the data to deserialize
117  * @param rd_count number of elements in array
118  * @param rd array
119  *
120  * @return number of elements deserialized
121  */
122 int
123 GNUNET_NAMESTORE_records_deserialize ( struct GNUNET_NAMESTORE_RecordData **dest, char *src, size_t len)
124 {
125   struct GNUNET_NAMESTORE_NetworkRecord * nr;
126   struct GNUNET_NAMESTORE_RecordData *d = (*dest);
127   int elements;
128   size_t offset;
129   uint32_t data_size;
130   int c;
131
132   if (len == 0)
133   {
134     (*dest) = NULL;
135     return 0;
136   }
137
138   offset = 0;
139   elements = 0;
140   while (offset < len)
141   {
142     nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &src[offset];
143     offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
144
145     data_size = ntohl (nr->data_size);
146     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Datasize record[%i]: %u\n", elements, data_size);
147     offset += data_size;
148     elements ++;
149   }
150
151   if (elements == 0)
152   {
153     (*dest) = NULL;
154     return 0;
155   }
156
157
158   GNUNET_assert (len == offset);
159   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deserializing %i records with total length of %u\n", elements, len);
160
161   (*dest) = GNUNET_malloc (elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
162   d = (*dest);
163
164   offset = 0;
165   for (c = 0; c < elements; c++)
166   {
167     nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &src[offset];
168     d[c].expiration = GNUNET_TIME_absolute_ntoh(nr->expiration);
169     d[c].record_type = ntohl (nr->record_type);
170     d[c].flags = ntohl (nr->flags);
171     d[c].data_size = ntohl (nr->data_size);
172     if (d[c].data_size > 0)
173       d[c].data = GNUNET_malloc (d[c].data_size);
174     else
175       d[c].data = NULL;
176
177     offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord);
178     memcpy((char *) d[c].data, &src[offset], d[c].data_size);
179
180     offset += d[c].data_size;
181     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deserialized record[%i] /w data_size %i\n", c, d[c].data_size);
182   }
183   GNUNET_assert(offset == len);
184
185   return elements;
186 }
187
188 /* end of namestore_api.c */