diff options
Diffstat (limited to 'src/lib/gnsrecord/gnsrecord_serialization.c')
-rw-r--r-- | src/lib/gnsrecord/gnsrecord_serialization.c | 302 |
1 files changed, 302 insertions, 0 deletions
diff --git a/src/lib/gnsrecord/gnsrecord_serialization.c b/src/lib/gnsrecord/gnsrecord_serialization.c new file mode 100644 index 000000000..053edfd33 --- /dev/null +++ b/src/lib/gnsrecord/gnsrecord_serialization.c | |||
@@ -0,0 +1,302 @@ | |||
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 | |||
35 | #define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__) | ||
36 | |||
37 | /** | ||
38 | * Set to 1 to check that all records are well-formed (can be converted | ||
39 | * to string) during serialization/deserialization. | ||
40 | */ | ||
41 | #define DEBUG_GNSRECORDS 0 | ||
42 | |||
43 | GNUNET_NETWORK_STRUCT_BEGIN | ||
44 | |||
45 | |||
46 | /** | ||
47 | * Internal format of a record in the serialized form. | ||
48 | */ | ||
49 | struct NetworkRecord | ||
50 | { | ||
51 | /** | ||
52 | * Expiration time for the DNS record; relative or absolute depends | ||
53 | * on @e flags, network byte order. | ||
54 | */ | ||
55 | uint64_t expiration_time GNUNET_PACKED; | ||
56 | |||
57 | /** | ||
58 | * Number of bytes in 'data', network byte order. | ||
59 | */ | ||
60 | uint16_t data_size GNUNET_PACKED; | ||
61 | |||
62 | /** | ||
63 | * Flags for the record, network byte order. | ||
64 | */ | ||
65 | uint16_t flags GNUNET_PACKED; | ||
66 | |||
67 | /** | ||
68 | * Type of the GNS/DNS record, network byte order. | ||
69 | */ | ||
70 | uint32_t record_type GNUNET_PACKED; | ||
71 | |||
72 | }; | ||
73 | |||
74 | GNUNET_NETWORK_STRUCT_END | ||
75 | |||
76 | |||
77 | ssize_t | ||
78 | GNUNET_GNSRECORD_records_get_size (unsigned int rd_count, | ||
79 | const struct GNUNET_GNSRECORD_Data *rd) | ||
80 | { | ||
81 | size_t ret; | ||
82 | |||
83 | if (0 == rd_count) | ||
84 | return 0; | ||
85 | |||
86 | ret = sizeof(struct NetworkRecord) * rd_count; | ||
87 | for (unsigned int i = 0; i < rd_count; i++) | ||
88 | { | ||
89 | if ((ret + rd[i].data_size) < ret) | ||
90 | { | ||
91 | GNUNET_break (0); | ||
92 | return -1; | ||
93 | } | ||
94 | ret += rd[i].data_size; | ||
95 | #if DEBUG_GNSRECORDS | ||
96 | { | ||
97 | char *str; | ||
98 | |||
99 | str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, | ||
100 | rd[i].data, | ||
101 | rd[i].data_size); | ||
102 | if (NULL == str) | ||
103 | { | ||
104 | GNUNET_break_op (0); | ||
105 | return -1; | ||
106 | } | ||
107 | GNUNET_free (str); | ||
108 | } | ||
109 | #endif | ||
110 | } | ||
111 | if (ret > SSIZE_MAX) | ||
112 | { | ||
113 | GNUNET_break (0); | ||
114 | return -1; | ||
115 | } | ||
116 | // Do not pad PKEY | ||
117 | if ((GNUNET_GNSRECORD_TYPE_PKEY == rd->record_type) || | ||
118 | (GNUNET_GNSRECORD_TYPE_EDKEY == rd->record_type)) | ||
119 | return ret; | ||
120 | /** | ||
121 | * Efficiently round up to the next | ||
122 | * power of 2 for padding | ||
123 | * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 | ||
124 | */ret--; | ||
125 | ret |= ret >> 1; | ||
126 | ret |= ret >> 2; | ||
127 | ret |= ret >> 4; | ||
128 | ret |= ret >> 8; | ||
129 | ret |= ret >> 16; | ||
130 | ret++; | ||
131 | return (ssize_t) ret; | ||
132 | } | ||
133 | |||
134 | |||
135 | ssize_t | ||
136 | GNUNET_GNSRECORD_records_serialize (unsigned int rd_count, | ||
137 | const struct GNUNET_GNSRECORD_Data *rd, | ||
138 | size_t dest_size, | ||
139 | char *dest) | ||
140 | { | ||
141 | struct NetworkRecord rec; | ||
142 | size_t off; | ||
143 | |||
144 | off = 0; | ||
145 | for (unsigned int i = 0; i < rd_count; i++) | ||
146 | { | ||
147 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
148 | "Serializing record %u with flags %d and expiration time %llu\n", | ||
149 | i, | ||
150 | rd[i].flags, | ||
151 | (unsigned long long) rd[i].expiration_time); | ||
152 | rec.expiration_time = GNUNET_htonll (rd[i].expiration_time); | ||
153 | rec.data_size = htons ((uint16_t) rd[i].data_size); | ||
154 | rec.record_type = htonl (rd[i].record_type); | ||
155 | rec.flags = htons (rd[i].flags); | ||
156 | if ((off + sizeof(rec) > dest_size) || | ||
157 | (off + sizeof(rec) < off)) | ||
158 | { | ||
159 | GNUNET_break (0); | ||
160 | return -1; | ||
161 | } | ||
162 | GNUNET_memcpy (&dest[off], | ||
163 | &rec, | ||
164 | sizeof(rec)); | ||
165 | off += sizeof(rec); | ||
166 | if ((off + rd[i].data_size > dest_size) || | ||
167 | (off + rd[i].data_size < off)) | ||
168 | { | ||
169 | GNUNET_break (0); | ||
170 | return -1; | ||
171 | } | ||
172 | GNUNET_memcpy (&dest[off], | ||
173 | rd[i].data, | ||
174 | rd[i].data_size); | ||
175 | off += rd[i].data_size; | ||
176 | #if DEBUG_GNSRECORDS | ||
177 | { | ||
178 | char *str; | ||
179 | |||
180 | str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, | ||
181 | rd[i].data, | ||
182 | rd[i].data_size); | ||
183 | if (NULL == str) | ||
184 | { | ||
185 | GNUNET_break_op (0); | ||
186 | return -1; | ||
187 | } | ||
188 | GNUNET_free (str); | ||
189 | } | ||
190 | #endif | ||
191 | } | ||
192 | memset (&dest[off], | ||
193 | 0, | ||
194 | dest_size - off); | ||
195 | return dest_size; | ||
196 | } | ||
197 | |||
198 | unsigned int | ||
199 | GNUNET_GNSRECORD_records_deserialize_get_size (size_t len, | ||
200 | const char *src) | ||
201 | { | ||
202 | struct NetworkRecord rec; | ||
203 | struct NetworkRecord rec_zero; | ||
204 | size_t off; | ||
205 | unsigned int rd_count = 0; | ||
206 | |||
207 | memset (&rec_zero, 0, sizeof (rec_zero)); | ||
208 | |||
209 | off = 0; | ||
210 | for (off = 0; (off + sizeof(rec) <= len) && (off + sizeof(rec) >= off);) | ||
211 | { | ||
212 | GNUNET_memcpy (&rec, | ||
213 | &src[off], | ||
214 | sizeof(rec)); | ||
215 | /* | ||
216 | * If we have found a byte string of zeroes, we have reached | ||
217 | * the padding | ||
218 | */ | ||
219 | if (0 == GNUNET_memcmp (&rec, &rec_zero)) | ||
220 | break; | ||
221 | off += sizeof(rec); | ||
222 | if ((off + ntohs ((uint16_t) rec.data_size) > len) || | ||
223 | (off + ntohs ((uint16_t) rec.data_size) < off)) | ||
224 | { | ||
225 | GNUNET_break_op (0); | ||
226 | return 0; | ||
227 | } | ||
228 | off += ntohs ((uint16_t) rec.data_size); | ||
229 | rd_count++; | ||
230 | } | ||
231 | return rd_count; | ||
232 | } | ||
233 | |||
234 | /** | ||
235 | * Deserialize the given records to the given destination. | ||
236 | * | ||
237 | * @param len size of the serialized record data | ||
238 | * @param src the serialized record data | ||
239 | * @param rd_count number of records parsed | ||
240 | * @param dest where to put the data | ||
241 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | ||
242 | */ | ||
243 | int | ||
244 | GNUNET_GNSRECORD_records_deserialize (size_t len, | ||
245 | const char *src, | ||
246 | unsigned int rd_count, | ||
247 | struct GNUNET_GNSRECORD_Data *dest) | ||
248 | { | ||
249 | struct NetworkRecord rec; | ||
250 | size_t off; | ||
251 | |||
252 | off = 0; | ||
253 | for (unsigned int i = 0; i < rd_count; i++) | ||
254 | { | ||
255 | if ((off + sizeof(rec) > len) || | ||
256 | (off + sizeof(rec) < off)) | ||
257 | { | ||
258 | GNUNET_break_op (0); | ||
259 | return GNUNET_SYSERR; | ||
260 | } | ||
261 | GNUNET_memcpy (&rec, | ||
262 | &src[off], | ||
263 | sizeof(rec)); | ||
264 | dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time); | ||
265 | dest[i].data_size = ntohs ((uint16_t) rec.data_size); | ||
266 | dest[i].record_type = ntohl (rec.record_type); | ||
267 | dest[i].flags = ntohs (rec.flags); | ||
268 | off += sizeof(rec); | ||
269 | if ((off + dest[i].data_size > len) || | ||
270 | (off + dest[i].data_size < off)) | ||
271 | { | ||
272 | GNUNET_break_op (0); | ||
273 | return GNUNET_SYSERR; | ||
274 | } | ||
275 | dest[i].data = &src[off]; | ||
276 | off += dest[i].data_size; | ||
277 | #if GNUNET_EXTRA_LOGGING | ||
278 | { | ||
279 | char *str; | ||
280 | |||
281 | str = GNUNET_GNSRECORD_value_to_string (dest[i].record_type, | ||
282 | dest[i].data, | ||
283 | dest[i].data_size); | ||
284 | if (NULL == str) | ||
285 | { | ||
286 | GNUNET_break_op (0); | ||
287 | return GNUNET_SYSERR; | ||
288 | } | ||
289 | GNUNET_free (str); | ||
290 | } | ||
291 | #endif | ||
292 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
293 | "Deserialized record %u with flags %d and expiration time %llu\n", | ||
294 | i, | ||
295 | dest[i].flags, | ||
296 | (unsigned long long) dest[i].expiration_time); | ||
297 | } | ||
298 | return GNUNET_OK; | ||
299 | } | ||
300 | |||
301 | |||
302 | /* end of gnsrecord_serialization.c */ | ||