diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-10-05 15:09:28 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-10-05 15:09:28 +0200 |
commit | c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch) | |
tree | cac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/gnsrecord/gnsrecord_serialization.c | |
parent | fbb71d527c7d6babf269a8fefce1db291b9f7068 (diff) | |
download | gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip |
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/gnsrecord/gnsrecord_serialization.c')
-rw-r--r-- | src/gnsrecord/gnsrecord_serialization.c | 239 |
1 files changed, 120 insertions, 119 deletions
diff --git a/src/gnsrecord/gnsrecord_serialization.c b/src/gnsrecord/gnsrecord_serialization.c index 9d2c858ef..a2711538c 100644 --- a/src/gnsrecord/gnsrecord_serialization.c +++ b/src/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,7 +49,8 @@ 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 | { | ||
53 | /** | 54 | /** |
54 | * Expiration time for the DNS record; relative or absolute depends | 55 | * Expiration time for the DNS record; relative or absolute depends |
55 | * on @e flags, network byte order. | 56 | * on @e flags, network byte order. |
@@ -84,8 +85,8 @@ GNUNET_NETWORK_STRUCT_END | |||
84 | * @return the required size to serialize, -1 on error | 85 | * @return the required size to serialize, -1 on error |
85 | */ | 86 | */ |
86 | ssize_t | 87 | ssize_t |
87 | GNUNET_GNSRECORD_records_get_size(unsigned int rd_count, | 88 | GNUNET_GNSRECORD_records_get_size (unsigned int rd_count, |
88 | const struct GNUNET_GNSRECORD_Data *rd) | 89 | const struct GNUNET_GNSRECORD_Data *rd) |
89 | { | 90 | { |
90 | size_t ret; | 91 | size_t ret; |
91 | 92 | ||
@@ -94,35 +95,35 @@ GNUNET_GNSRECORD_records_get_size(unsigned int rd_count, | |||
94 | 95 | ||
95 | ret = sizeof(struct NetworkRecord) * rd_count; | 96 | ret = sizeof(struct NetworkRecord) * rd_count; |
96 | for (unsigned int i = 0; i < rd_count; i++) | 97 | for (unsigned int i = 0; i < rd_count; i++) |
98 | { | ||
99 | if ((ret + rd[i].data_size) < ret) | ||
97 | { | 100 | { |
98 | if ((ret + rd[i].data_size) < ret) | 101 | GNUNET_break (0); |
99 | { | 102 | return -1; |
100 | GNUNET_break(0); | 103 | } |
101 | return -1; | 104 | ret += rd[i].data_size; |
102 | } | ||
103 | ret += rd[i].data_size; | ||
104 | #if DEBUG_GNSRECORDS | 105 | #if DEBUG_GNSRECORDS |
105 | { | 106 | { |
106 | char *str; | 107 | char *str; |
107 | 108 | ||
108 | str = GNUNET_GNSRECORD_value_to_string(rd[i].record_type, | 109 | str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, |
109 | rd[i].data, | 110 | rd[i].data, |
110 | rd[i].data_size); | 111 | rd[i].data_size); |
111 | if (NULL == str) | 112 | if (NULL == str) |
112 | { | 113 | { |
113 | GNUNET_break_op(0); | 114 | GNUNET_break_op (0); |
114 | return -1; | 115 | return -1; |
115 | } | ||
116 | GNUNET_free(str); | ||
117 | } | 116 | } |
118 | #endif | 117 | GNUNET_free (str); |
119 | } | 118 | } |
119 | #endif | ||
120 | } | ||
120 | if (ret > SSIZE_MAX) | 121 | if (ret > SSIZE_MAX) |
121 | { | 122 | { |
122 | GNUNET_break(0); | 123 | GNUNET_break (0); |
123 | return -1; | 124 | return -1; |
124 | } | 125 | } |
125 | //Do not pad PKEY | 126 | // Do not pad PKEY |
126 | if (GNUNET_GNSRECORD_TYPE_PKEY == rd->record_type) | 127 | if (GNUNET_GNSRECORD_TYPE_PKEY == rd->record_type) |
127 | return ret; | 128 | return ret; |
128 | /** | 129 | /** |
@@ -137,7 +138,7 @@ GNUNET_GNSRECORD_records_get_size(unsigned int rd_count, | |||
137 | ret |= ret >> 8; | 138 | ret |= ret >> 8; |
138 | ret |= ret >> 16; | 139 | ret |= ret >> 16; |
139 | ret++; | 140 | ret++; |
140 | return (ssize_t)ret; | 141 | return (ssize_t) ret; |
141 | } | 142 | } |
142 | 143 | ||
143 | 144 | ||
@@ -151,65 +152,65 @@ GNUNET_GNSRECORD_records_get_size(unsigned int rd_count, | |||
151 | * @return the size of serialized records, -1 if records do not fit | 152 | * @return the size of serialized records, -1 if records do not fit |
152 | */ | 153 | */ |
153 | ssize_t | 154 | ssize_t |
154 | GNUNET_GNSRECORD_records_serialize(unsigned int rd_count, | 155 | GNUNET_GNSRECORD_records_serialize (unsigned int rd_count, |
155 | const struct GNUNET_GNSRECORD_Data *rd, | 156 | const struct GNUNET_GNSRECORD_Data *rd, |
156 | size_t dest_size, | 157 | size_t dest_size, |
157 | char *dest) | 158 | char *dest) |
158 | { | 159 | { |
159 | struct NetworkRecord rec; | 160 | struct NetworkRecord rec; |
160 | size_t off; | 161 | size_t off; |
161 | 162 | ||
162 | off = 0; | 163 | off = 0; |
163 | for (unsigned int i = 0; i < rd_count; i++) | 164 | for (unsigned int i = 0; i < rd_count; i++) |
165 | { | ||
166 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
167 | "Serializing record %u with flags %d and expiration time %llu\n", | ||
168 | i, | ||
169 | rd[i].flags, | ||
170 | (unsigned long long) rd[i].expiration_time); | ||
171 | rec.expiration_time = GNUNET_htonll (rd[i].expiration_time); | ||
172 | rec.data_size = htonl ((uint32_t) rd[i].data_size); | ||
173 | rec.record_type = htonl (rd[i].record_type); | ||
174 | rec.flags = htonl (rd[i].flags); | ||
175 | if ((off + sizeof(rec) > dest_size) || | ||
176 | (off + sizeof(rec) < off)) | ||
164 | { | 177 | { |
165 | LOG(GNUNET_ERROR_TYPE_DEBUG, | 178 | GNUNET_break (0); |
166 | "Serializing record %u with flags %d and expiration time %llu\n", | 179 | return -1; |
167 | i, | 180 | } |
168 | rd[i].flags, | 181 | GNUNET_memcpy (&dest[off], |
169 | (unsigned long long)rd[i].expiration_time); | 182 | &rec, |
170 | rec.expiration_time = GNUNET_htonll(rd[i].expiration_time); | 183 | sizeof(rec)); |
171 | rec.data_size = htonl((uint32_t)rd[i].data_size); | 184 | off += sizeof(rec); |
172 | rec.record_type = htonl(rd[i].record_type); | 185 | if ((off + rd[i].data_size > dest_size) || |
173 | rec.flags = htonl(rd[i].flags); | 186 | (off + rd[i].data_size < off)) |
174 | if ((off + sizeof(rec) > dest_size) || | 187 | { |
175 | (off + sizeof(rec) < off)) | 188 | GNUNET_break (0); |
176 | { | 189 | return -1; |
177 | GNUNET_break(0); | 190 | } |
178 | return -1; | 191 | GNUNET_memcpy (&dest[off], |
179 | } | 192 | rd[i].data, |
180 | GNUNET_memcpy(&dest[off], | 193 | rd[i].data_size); |
181 | &rec, | 194 | off += rd[i].data_size; |
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; | ||
194 | #if DEBUG_GNSRECORDS | 195 | #if DEBUG_GNSRECORDS |
195 | { | 196 | { |
196 | char *str; | 197 | char *str; |
197 | 198 | ||
198 | str = GNUNET_GNSRECORD_value_to_string(rd[i].record_type, | 199 | str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, |
199 | rd[i].data, | 200 | rd[i].data, |
200 | rd[i].data_size); | 201 | rd[i].data_size); |
201 | if (NULL == str) | 202 | if (NULL == str) |
202 | { | 203 | { |
203 | GNUNET_break_op(0); | 204 | GNUNET_break_op (0); |
204 | return -1; | 205 | return -1; |
205 | } | ||
206 | GNUNET_free(str); | ||
207 | } | 206 | } |
208 | #endif | 207 | GNUNET_free (str); |
209 | } | 208 | } |
210 | memset(&dest[off], | 209 | #endif |
211 | 0, | 210 | } |
212 | dest_size - off); | 211 | memset (&dest[off], |
212 | 0, | ||
213 | dest_size - off); | ||
213 | return dest_size; | 214 | return dest_size; |
214 | } | 215 | } |
215 | 216 | ||
@@ -224,60 +225,60 @@ GNUNET_GNSRECORD_records_serialize(unsigned int rd_count, | |||
224 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 225 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
225 | */ | 226 | */ |
226 | int | 227 | int |
227 | GNUNET_GNSRECORD_records_deserialize(size_t len, | 228 | GNUNET_GNSRECORD_records_deserialize (size_t len, |
228 | const char *src, | 229 | const char *src, |
229 | unsigned int rd_count, | 230 | unsigned int rd_count, |
230 | struct GNUNET_GNSRECORD_Data *dest) | 231 | struct GNUNET_GNSRECORD_Data *dest) |
231 | { | 232 | { |
232 | struct NetworkRecord rec; | 233 | struct NetworkRecord rec; |
233 | size_t off; | 234 | size_t off; |
234 | 235 | ||
235 | off = 0; | 236 | off = 0; |
236 | for (unsigned int i = 0; i < rd_count; i++) | 237 | for (unsigned int i = 0; i < rd_count; i++) |
238 | { | ||
239 | if ((off + sizeof(rec) > len) || | ||
240 | (off + sizeof(rec) < off)) | ||
241 | { | ||
242 | GNUNET_break_op (0); | ||
243 | return GNUNET_SYSERR; | ||
244 | } | ||
245 | GNUNET_memcpy (&rec, | ||
246 | &src[off], | ||
247 | sizeof(rec)); | ||
248 | dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time); | ||
249 | dest[i].data_size = ntohl ((uint32_t) rec.data_size); | ||
250 | dest[i].record_type = ntohl (rec.record_type); | ||
251 | dest[i].flags = ntohl (rec.flags); | ||
252 | off += sizeof(rec); | ||
253 | if ((off + dest[i].data_size > len) || | ||
254 | (off + dest[i].data_size < off)) | ||
237 | { | 255 | { |
238 | if ((off + sizeof(rec) > len) || | 256 | GNUNET_break_op (0); |
239 | (off + sizeof(rec) < off)) | 257 | return GNUNET_SYSERR; |
240 | { | 258 | } |
241 | GNUNET_break_op(0); | 259 | dest[i].data = &src[off]; |
242 | return GNUNET_SYSERR; | 260 | off += dest[i].data_size; |
243 | } | ||
244 | GNUNET_memcpy(&rec, | ||
245 | &src[off], | ||
246 | sizeof(rec)); | ||
247 | dest[i].expiration_time = GNUNET_ntohll(rec.expiration_time); | ||
248 | dest[i].data_size = ntohl((uint32_t)rec.data_size); | ||
249 | dest[i].record_type = ntohl(rec.record_type); | ||
250 | dest[i].flags = ntohl(rec.flags); | ||
251 | off += sizeof(rec); | ||
252 | if ((off + dest[i].data_size > len) || | ||
253 | (off + dest[i].data_size < off)) | ||
254 | { | ||
255 | GNUNET_break_op(0); | ||
256 | return GNUNET_SYSERR; | ||
257 | } | ||
258 | dest[i].data = &src[off]; | ||
259 | off += dest[i].data_size; | ||
260 | #if GNUNET_EXTRA_LOGGING | 261 | #if GNUNET_EXTRA_LOGGING |
261 | { | 262 | { |
262 | char *str; | 263 | char *str; |
263 | 264 | ||
264 | str = GNUNET_GNSRECORD_value_to_string(dest[i].record_type, | 265 | str = GNUNET_GNSRECORD_value_to_string (dest[i].record_type, |
265 | dest[i].data, | 266 | dest[i].data, |
266 | dest[i].data_size); | 267 | dest[i].data_size); |
267 | if (NULL == str) | 268 | if (NULL == str) |
268 | { | 269 | { |
269 | GNUNET_break_op(0); | 270 | GNUNET_break_op (0); |
270 | return GNUNET_SYSERR; | 271 | return GNUNET_SYSERR; |
271 | } | ||
272 | GNUNET_free(str); | ||
273 | } | 272 | } |
274 | #endif | 273 | GNUNET_free (str); |
275 | LOG(GNUNET_ERROR_TYPE_DEBUG, | ||
276 | "Deserialized record %u with flags %d and expiration time %llu\n", | ||
277 | i, | ||
278 | dest[i].flags, | ||
279 | (unsigned long long)dest[i].expiration_time); | ||
280 | } | 274 | } |
275 | #endif | ||
276 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
277 | "Deserialized record %u with flags %d and expiration time %llu\n", | ||
278 | i, | ||
279 | dest[i].flags, | ||
280 | (unsigned long long) dest[i].expiration_time); | ||
281 | } | ||
281 | return GNUNET_OK; | 282 | return GNUNET_OK; |
282 | } | 283 | } |
283 | 284 | ||