aboutsummaryrefslogtreecommitdiff
path: root/src/gnsrecord/gnsrecord_serialization.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gnsrecord/gnsrecord_serialization.c')
-rw-r--r--src/gnsrecord/gnsrecord_serialization.c322
1 files changed, 0 insertions, 322 deletions
diff --git a/src/gnsrecord/gnsrecord_serialization.c b/src/gnsrecord/gnsrecord_serialization.c
deleted file mode 100644
index 97c488c2b..000000000
--- a/src/gnsrecord/gnsrecord_serialization.c
+++ /dev/null
@@ -1,322 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file gnsrecord/gnsrecord_serialization.c
23 * @brief API to serialize and deserialize GNS records
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
26 * @author Christian Grothoff
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_gnsrecord_lib.h"
34#include "gnunet_dnsparser_lib.h"
35#include "gnunet_tun_lib.h"
36
37
38#define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__)
39
40/**
41 * Set to 1 to check that all records are well-formed (can be converted
42 * to string) during serialization/deserialization.
43 */
44#define DEBUG_GNSRECORDS 0
45
46GNUNET_NETWORK_STRUCT_BEGIN
47
48
49/**
50 * Internal format of a record in the serialized form.
51 */
52struct NetworkRecord
53{
54 /**
55 * Expiration time for the DNS record; relative or absolute depends
56 * on @e flags, network byte order.
57 */
58 uint64_t expiration_time GNUNET_PACKED;
59
60 /**
61 * Number of bytes in 'data', network byte order.
62 */
63 uint16_t data_size GNUNET_PACKED;
64
65 /**
66 * Flags for the record, network byte order.
67 */
68 uint16_t flags GNUNET_PACKED;
69
70 /**
71 * Type of the GNS/DNS record, network byte order.
72 */
73 uint32_t record_type GNUNET_PACKED;
74
75};
76
77GNUNET_NETWORK_STRUCT_END
78
79
80/**
81 * Calculate how many bytes we will need to serialize the given
82 * records.
83 *
84 * @param rd_count number of records in the rd array
85 * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
86 * @return the required size to serialize, -1 on error
87 */
88ssize_t
89GNUNET_GNSRECORD_records_get_size (unsigned int rd_count,
90 const struct GNUNET_GNSRECORD_Data *rd)
91{
92 size_t ret;
93
94 if (0 == rd_count)
95 return 0;
96
97 ret = sizeof(struct NetworkRecord) * rd_count;
98 for (unsigned int i = 0; i < rd_count; i++)
99 {
100 if ((ret + rd[i].data_size) < ret)
101 {
102 GNUNET_break (0);
103 return -1;
104 }
105 ret += rd[i].data_size;
106#if DEBUG_GNSRECORDS
107 {
108 char *str;
109
110 str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
111 rd[i].data,
112 rd[i].data_size);
113 if (NULL == str)
114 {
115 GNUNET_break_op (0);
116 return -1;
117 }
118 GNUNET_free (str);
119 }
120#endif
121 }
122 if (ret > SSIZE_MAX)
123 {
124 GNUNET_break (0);
125 return -1;
126 }
127 // Do not pad PKEY
128 if ((GNUNET_GNSRECORD_TYPE_PKEY == rd->record_type) ||
129 (GNUNET_GNSRECORD_TYPE_EDKEY == rd->record_type))
130 return ret;
131 /**
132 * Efficiently round up to the next
133 * power of 2 for padding
134 * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
135 */ret--;
136 ret |= ret >> 1;
137 ret |= ret >> 2;
138 ret |= ret >> 4;
139 ret |= ret >> 8;
140 ret |= ret >> 16;
141 ret++;
142 return (ssize_t) ret;
143}
144
145
146/**
147 * Serialize the given records to the given destination buffer.
148 *
149 * @param rd_count number of records in the rd array
150 * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
151 * @param dest_size size of the destination array
152 * @param dest where to write the result
153 * @return the size of serialized records, -1 if records do not fit
154 */
155ssize_t
156GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
157 const struct GNUNET_GNSRECORD_Data *rd,
158 size_t dest_size,
159 char *dest)
160{
161 struct NetworkRecord rec;
162 size_t off;
163
164 off = 0;
165 for (unsigned int i = 0; i < rd_count; i++)
166 {
167 LOG (GNUNET_ERROR_TYPE_DEBUG,
168 "Serializing record %u with flags %d and expiration time %llu\n",
169 i,
170 rd[i].flags,
171 (unsigned long long) rd[i].expiration_time);
172 rec.expiration_time = GNUNET_htonll (rd[i].expiration_time);
173 rec.data_size = htons ((uint16_t) rd[i].data_size);
174 rec.record_type = htonl (rd[i].record_type);
175 rec.flags = htons (rd[i].flags);
176 if ((off + sizeof(rec) > dest_size) ||
177 (off + sizeof(rec) < off))
178 {
179 GNUNET_break (0);
180 return -1;
181 }
182 GNUNET_memcpy (&dest[off],
183 &rec,
184 sizeof(rec));
185 off += sizeof(rec);
186 if ((off + rd[i].data_size > dest_size) ||
187 (off + rd[i].data_size < off))
188 {
189 GNUNET_break (0);
190 return -1;
191 }
192 GNUNET_memcpy (&dest[off],
193 rd[i].data,
194 rd[i].data_size);
195 off += rd[i].data_size;
196#if DEBUG_GNSRECORDS
197 {
198 char *str;
199
200 str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
201 rd[i].data,
202 rd[i].data_size);
203 if (NULL == str)
204 {
205 GNUNET_break_op (0);
206 return -1;
207 }
208 GNUNET_free (str);
209 }
210#endif
211 }
212 memset (&dest[off],
213 0,
214 dest_size - off);
215 return dest_size;
216}
217
218unsigned int
219GNUNET_GNSRECORD_records_deserialize_get_size (size_t len,
220 const char *src)
221{
222 struct NetworkRecord rec;
223 struct NetworkRecord rec_zero;
224 size_t off;
225 unsigned int rd_count = 0;
226
227 memset (&rec_zero, 0, sizeof (rec_zero));
228
229 off = 0;
230 for (off = 0; (off + sizeof(rec) <= len) && (off + sizeof(rec) >= off);)
231 {
232 GNUNET_memcpy (&rec,
233 &src[off],
234 sizeof(rec));
235 /*
236 * If we have found a byte string of zeroes, we have reached
237 * the padding
238 */
239 if (0 == GNUNET_memcmp (&rec, &rec_zero))
240 break;
241 off += sizeof(rec);
242 if ((off + ntohs ((uint16_t) rec.data_size) > len) ||
243 (off + ntohs ((uint16_t) rec.data_size) < off))
244 {
245 GNUNET_break_op (0);
246 return 0;
247 }
248 off += ntohs ((uint16_t) rec.data_size);
249 rd_count++;
250 }
251 return rd_count;
252}
253
254/**
255 * Deserialize the given records to the given destination.
256 *
257 * @param len size of the serialized record data
258 * @param src the serialized record data
259 * @param rd_count number of records parsed
260 * @param dest where to put the data
261 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
262 */
263int
264GNUNET_GNSRECORD_records_deserialize (size_t len,
265 const char *src,
266 unsigned int rd_count,
267 struct GNUNET_GNSRECORD_Data *dest)
268{
269 struct NetworkRecord rec;
270 size_t off;
271
272 off = 0;
273 for (unsigned int i = 0; i < rd_count; i++)
274 {
275 if ((off + sizeof(rec) > len) ||
276 (off + sizeof(rec) < off))
277 {
278 GNUNET_break_op (0);
279 return GNUNET_SYSERR;
280 }
281 GNUNET_memcpy (&rec,
282 &src[off],
283 sizeof(rec));
284 dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time);
285 dest[i].data_size = ntohs ((uint16_t) rec.data_size);
286 dest[i].record_type = ntohl (rec.record_type);
287 dest[i].flags = ntohs (rec.flags);
288 off += sizeof(rec);
289 if ((off + dest[i].data_size > len) ||
290 (off + dest[i].data_size < off))
291 {
292 GNUNET_break_op (0);
293 return GNUNET_SYSERR;
294 }
295 dest[i].data = &src[off];
296 off += dest[i].data_size;
297#if GNUNET_EXTRA_LOGGING
298 {
299 char *str;
300
301 str = GNUNET_GNSRECORD_value_to_string (dest[i].record_type,
302 dest[i].data,
303 dest[i].data_size);
304 if (NULL == str)
305 {
306 GNUNET_break_op (0);
307 return GNUNET_SYSERR;
308 }
309 GNUNET_free (str);
310 }
311#endif
312 LOG (GNUNET_ERROR_TYPE_DEBUG,
313 "Deserialized record %u with flags %d and expiration time %llu\n",
314 i,
315 dest[i].flags,
316 (unsigned long long) dest[i].expiration_time);
317 }
318 return GNUNET_OK;
319}
320
321
322/* end of gnsrecord_serialization.c */