diff options
Diffstat (limited to 'src/gnsrecord/gnsrecord_serialization.c')
-rw-r--r-- | src/gnsrecord/gnsrecord_serialization.c | 253 |
1 files changed, 125 insertions, 128 deletions
diff --git a/src/gnsrecord/gnsrecord_serialization.c b/src/gnsrecord/gnsrecord_serialization.c index be1dc1889..9d2c858ef 100644 --- a/src/gnsrecord/gnsrecord_serialization.c +++ b/src/gnsrecord/gnsrecord_serialization.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 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/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file gnsrecord/gnsrecord_serialization.c | 22 | * @file gnsrecord/gnsrecord_serialization.c |
@@ -35,7 +35,7 @@ | |||
35 | #include "gnunet_tun_lib.h" | 35 | #include "gnunet_tun_lib.h" |
36 | 36 | ||
37 | 37 | ||
38 | #define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__) | 38 | #define LOG(kind, ...) GNUNET_log_from(kind, "gnsrecord", __VA_ARGS__) |
39 | 39 | ||
40 | /** | 40 | /** |
41 | * Set to 1 to check that all records are well-formed (can be converted | 41 | * Set to 1 to check that all records are well-formed (can be converted |
@@ -49,9 +49,7 @@ GNUNET_NETWORK_STRUCT_BEGIN | |||
49 | /** | 49 | /** |
50 | * Internal format of a record in the serialized form. | 50 | * Internal format of a record in the serialized form. |
51 | */ | 51 | */ |
52 | struct NetworkRecord | 52 | struct NetworkRecord { |
53 | { | ||
54 | |||
55 | /** | 53 | /** |
56 | * Expiration time for the DNS record; relative or absolute depends | 54 | * Expiration time for the DNS record; relative or absolute depends |
57 | * on @e flags, network byte order. | 55 | * on @e flags, network byte order. |
@@ -72,7 +70,6 @@ struct NetworkRecord | |||
72 | * Flags for the record, network byte order. | 70 | * Flags for the record, network byte order. |
73 | */ | 71 | */ |
74 | uint32_t flags GNUNET_PACKED; | 72 | uint32_t flags GNUNET_PACKED; |
75 | |||
76 | }; | 73 | }; |
77 | 74 | ||
78 | GNUNET_NETWORK_STRUCT_END | 75 | GNUNET_NETWORK_STRUCT_END |
@@ -87,44 +84,44 @@ GNUNET_NETWORK_STRUCT_END | |||
87 | * @return the required size to serialize, -1 on error | 84 | * @return the required size to serialize, -1 on error |
88 | */ | 85 | */ |
89 | ssize_t | 86 | ssize_t |
90 | GNUNET_GNSRECORD_records_get_size (unsigned int rd_count, | 87 | GNUNET_GNSRECORD_records_get_size(unsigned int rd_count, |
91 | const struct GNUNET_GNSRECORD_Data *rd) | 88 | const struct GNUNET_GNSRECORD_Data *rd) |
92 | { | 89 | { |
93 | size_t ret; | 90 | size_t ret; |
94 | 91 | ||
95 | if (0 == rd_count) | 92 | if (0 == rd_count) |
96 | return 0; | 93 | return 0; |
97 | 94 | ||
98 | ret = sizeof (struct NetworkRecord) * rd_count; | 95 | ret = sizeof(struct NetworkRecord) * rd_count; |
99 | for (unsigned int i=0;i<rd_count;i++) | 96 | for (unsigned int i = 0; i < rd_count; i++) |
100 | { | ||
101 | if ((ret + rd[i].data_size) < ret) | ||
102 | { | 97 | { |
103 | GNUNET_break (0); | 98 | if ((ret + rd[i].data_size) < ret) |
104 | return -1; | 99 | { |
105 | } | 100 | GNUNET_break(0); |
106 | ret += rd[i].data_size; | 101 | return -1; |
102 | } | ||
103 | ret += rd[i].data_size; | ||
107 | #if DEBUG_GNSRECORDS | 104 | #if DEBUG_GNSRECORDS |
108 | { | ||
109 | char *str; | ||
110 | |||
111 | str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, | ||
112 | rd[i].data, | ||
113 | rd[i].data_size); | ||
114 | if (NULL == str) | ||
115 | { | 105 | { |
116 | GNUNET_break_op (0); | 106 | char *str; |
117 | return -1; | 107 | |
108 | str = GNUNET_GNSRECORD_value_to_string(rd[i].record_type, | ||
109 | rd[i].data, | ||
110 | rd[i].data_size); | ||
111 | if (NULL == str) | ||
112 | { | ||
113 | GNUNET_break_op(0); | ||
114 | return -1; | ||
115 | } | ||
116 | GNUNET_free(str); | ||
118 | } | 117 | } |
119 | GNUNET_free (str); | ||
120 | } | ||
121 | #endif | 118 | #endif |
122 | } | 119 | } |
123 | if (ret > SSIZE_MAX) | 120 | if (ret > SSIZE_MAX) |
124 | { | 121 | { |
125 | GNUNET_break (0); | 122 | GNUNET_break(0); |
126 | return -1; | 123 | return -1; |
127 | } | 124 | } |
128 | //Do not pad PKEY | 125 | //Do not pad PKEY |
129 | if (GNUNET_GNSRECORD_TYPE_PKEY == rd->record_type) | 126 | if (GNUNET_GNSRECORD_TYPE_PKEY == rd->record_type) |
130 | return ret; | 127 | return ret; |
@@ -140,7 +137,7 @@ GNUNET_GNSRECORD_records_get_size (unsigned int rd_count, | |||
140 | ret |= ret >> 8; | 137 | ret |= ret >> 8; |
141 | ret |= ret >> 16; | 138 | ret |= ret >> 16; |
142 | ret++; | 139 | ret++; |
143 | return (ssize_t) ret; | 140 | return (ssize_t)ret; |
144 | } | 141 | } |
145 | 142 | ||
146 | 143 | ||
@@ -154,65 +151,65 @@ GNUNET_GNSRECORD_records_get_size (unsigned int rd_count, | |||
154 | * @return the size of serialized records, -1 if records do not fit | 151 | * @return the size of serialized records, -1 if records do not fit |
155 | */ | 152 | */ |
156 | ssize_t | 153 | ssize_t |
157 | GNUNET_GNSRECORD_records_serialize (unsigned int rd_count, | 154 | GNUNET_GNSRECORD_records_serialize(unsigned int rd_count, |
158 | const struct GNUNET_GNSRECORD_Data *rd, | 155 | const struct GNUNET_GNSRECORD_Data *rd, |
159 | size_t dest_size, | 156 | size_t dest_size, |
160 | char *dest) | 157 | char *dest) |
161 | { | 158 | { |
162 | struct NetworkRecord rec; | 159 | struct NetworkRecord rec; |
163 | size_t off; | 160 | size_t off; |
164 | 161 | ||
165 | off = 0; | 162 | off = 0; |
166 | for (unsigned int i=0;i<rd_count;i++) | 163 | for (unsigned int i = 0; i < rd_count; i++) |
167 | { | ||
168 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
169 | "Serializing record %u with flags %d and expiration time %llu\n", | ||
170 | i, | ||
171 | rd[i].flags, | ||
172 | (unsigned long long) rd[i].expiration_time); | ||
173 | rec.expiration_time = GNUNET_htonll (rd[i].expiration_time); | ||
174 | rec.data_size = htonl ((uint32_t) rd[i].data_size); | ||
175 | rec.record_type = htonl (rd[i].record_type); | ||
176 | rec.flags = htonl (rd[i].flags); | ||
177 | if ( (off + sizeof (rec) > dest_size) || | ||
178 | (off + sizeof (rec) < off) ) | ||
179 | { | ||
180 | GNUNET_break (0); | ||
181 | return -1; | ||
182 | } | ||
183 | GNUNET_memcpy (&dest[off], | ||
184 | &rec, | ||
185 | sizeof (rec)); | ||
186 | off += sizeof (rec); | ||
187 | if ( (off + rd[i].data_size > dest_size) || | ||
188 | (off + rd[i].data_size < off) ) | ||
189 | { | 164 | { |
190 | GNUNET_break (0); | 165 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
191 | return -1; | 166 | "Serializing record %u with flags %d and expiration time %llu\n", |
192 | } | 167 | i, |
193 | GNUNET_memcpy (&dest[off], | 168 | rd[i].flags, |
194 | rd[i].data, | 169 | (unsigned long long)rd[i].expiration_time); |
195 | rd[i].data_size); | 170 | rec.expiration_time = GNUNET_htonll(rd[i].expiration_time); |
196 | off += rd[i].data_size; | 171 | rec.data_size = htonl((uint32_t)rd[i].data_size); |
172 | rec.record_type = htonl(rd[i].record_type); | ||
173 | rec.flags = htonl(rd[i].flags); | ||
174 | if ((off + sizeof(rec) > dest_size) || | ||
175 | (off + sizeof(rec) < off)) | ||
176 | { | ||
177 | GNUNET_break(0); | ||
178 | return -1; | ||
179 | } | ||
180 | GNUNET_memcpy(&dest[off], | ||
181 | &rec, | ||
182 | sizeof(rec)); | ||
183 | off += sizeof(rec); | ||
184 | if ((off + rd[i].data_size > dest_size) || | ||
185 | (off + rd[i].data_size < off)) | ||
186 | { | ||
187 | GNUNET_break(0); | ||
188 | return -1; | ||
189 | } | ||
190 | GNUNET_memcpy(&dest[off], | ||
191 | rd[i].data, | ||
192 | rd[i].data_size); | ||
193 | off += rd[i].data_size; | ||
197 | #if DEBUG_GNSRECORDS | 194 | #if DEBUG_GNSRECORDS |
198 | { | ||
199 | char *str; | ||
200 | |||
201 | str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, | ||
202 | rd[i].data, | ||
203 | rd[i].data_size); | ||
204 | if (NULL == str) | ||
205 | { | 195 | { |
206 | GNUNET_break_op (0); | 196 | char *str; |
207 | return -1; | 197 | |
198 | str = GNUNET_GNSRECORD_value_to_string(rd[i].record_type, | ||
199 | rd[i].data, | ||
200 | rd[i].data_size); | ||
201 | if (NULL == str) | ||
202 | { | ||
203 | GNUNET_break_op(0); | ||
204 | return -1; | ||
205 | } | ||
206 | GNUNET_free(str); | ||
208 | } | 207 | } |
209 | GNUNET_free (str); | ||
210 | } | ||
211 | #endif | 208 | #endif |
212 | } | 209 | } |
213 | memset (&dest[off], | 210 | memset(&dest[off], |
214 | 0, | 211 | 0, |
215 | dest_size-off); | 212 | dest_size - off); |
216 | return dest_size; | 213 | return dest_size; |
217 | } | 214 | } |
218 | 215 | ||
@@ -227,60 +224,60 @@ GNUNET_GNSRECORD_records_serialize (unsigned int rd_count, | |||
227 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 224 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
228 | */ | 225 | */ |
229 | int | 226 | int |
230 | GNUNET_GNSRECORD_records_deserialize (size_t len, | 227 | GNUNET_GNSRECORD_records_deserialize(size_t len, |
231 | const char *src, | 228 | const char *src, |
232 | unsigned int rd_count, | 229 | unsigned int rd_count, |
233 | struct GNUNET_GNSRECORD_Data *dest) | 230 | struct GNUNET_GNSRECORD_Data *dest) |
234 | { | 231 | { |
235 | struct NetworkRecord rec; | 232 | struct NetworkRecord rec; |
236 | size_t off; | 233 | size_t off; |
237 | 234 | ||
238 | off = 0; | 235 | off = 0; |
239 | for (unsigned int i=0;i<rd_count;i++) | 236 | for (unsigned int i = 0; i < rd_count; i++) |
240 | { | ||
241 | if ( (off + sizeof (rec) > len) || | ||
242 | (off + sizeof (rec) < off) ) | ||
243 | { | 237 | { |
244 | GNUNET_break_op (0); | 238 | if ((off + sizeof(rec) > len) || |
245 | return GNUNET_SYSERR; | 239 | (off + sizeof(rec) < off)) |
246 | } | 240 | { |
247 | GNUNET_memcpy (&rec, | 241 | GNUNET_break_op(0); |
248 | &src[off], | 242 | return GNUNET_SYSERR; |
249 | sizeof (rec)); | 243 | } |
250 | dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time); | 244 | GNUNET_memcpy(&rec, |
251 | dest[i].data_size = ntohl ((uint32_t) rec.data_size); | 245 | &src[off], |
252 | dest[i].record_type = ntohl (rec.record_type); | 246 | sizeof(rec)); |
253 | dest[i].flags = ntohl (rec.flags); | 247 | dest[i].expiration_time = GNUNET_ntohll(rec.expiration_time); |
254 | off += sizeof (rec); | 248 | dest[i].data_size = ntohl((uint32_t)rec.data_size); |
255 | if ( (off + dest[i].data_size > len) || | 249 | dest[i].record_type = ntohl(rec.record_type); |
256 | (off + dest[i].data_size < off) ) | 250 | dest[i].flags = ntohl(rec.flags); |
257 | { | 251 | off += sizeof(rec); |
258 | GNUNET_break_op (0); | 252 | if ((off + dest[i].data_size > len) || |
259 | return GNUNET_SYSERR; | 253 | (off + dest[i].data_size < off)) |
260 | } | 254 | { |
261 | dest[i].data = &src[off]; | 255 | GNUNET_break_op(0); |
262 | off += dest[i].data_size; | 256 | return GNUNET_SYSERR; |
257 | } | ||
258 | dest[i].data = &src[off]; | ||
259 | off += dest[i].data_size; | ||
263 | #if GNUNET_EXTRA_LOGGING | 260 | #if GNUNET_EXTRA_LOGGING |
264 | { | ||
265 | char *str; | ||
266 | |||
267 | str = GNUNET_GNSRECORD_value_to_string (dest[i].record_type, | ||
268 | dest[i].data, | ||
269 | dest[i].data_size); | ||
270 | if (NULL == str) | ||
271 | { | 261 | { |
272 | GNUNET_break_op (0); | 262 | char *str; |
273 | return GNUNET_SYSERR; | 263 | |
264 | str = GNUNET_GNSRECORD_value_to_string(dest[i].record_type, | ||
265 | dest[i].data, | ||
266 | dest[i].data_size); | ||
267 | if (NULL == str) | ||
268 | { | ||
269 | GNUNET_break_op(0); | ||
270 | return GNUNET_SYSERR; | ||
271 | } | ||
272 | GNUNET_free(str); | ||
274 | } | 273 | } |
275 | GNUNET_free (str); | ||
276 | } | ||
277 | #endif | 274 | #endif |
278 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 275 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
279 | "Deserialized record %u with flags %d and expiration time %llu\n", | 276 | "Deserialized record %u with flags %d and expiration time %llu\n", |
280 | i, | 277 | i, |
281 | dest[i].flags, | 278 | dest[i].flags, |
282 | (unsigned long long) dest[i].expiration_time); | 279 | (unsigned long long)dest[i].expiration_time); |
283 | } | 280 | } |
284 | return GNUNET_OK; | 281 | return GNUNET_OK; |
285 | } | 282 | } |
286 | 283 | ||