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.c56
1 files changed, 46 insertions, 10 deletions
diff --git a/src/gnsrecord/gnsrecord_serialization.c b/src/gnsrecord/gnsrecord_serialization.c
index cb6957605..eaa3a9ab2 100644
--- a/src/gnsrecord/gnsrecord_serialization.c
+++ b/src/gnsrecord/gnsrecord_serialization.c
@@ -60,17 +60,18 @@ struct NetworkRecord
60 /** 60 /**
61 * Number of bytes in 'data', network byte order. 61 * Number of bytes in 'data', network byte order.
62 */ 62 */
63 uint32_t data_size GNUNET_PACKED; 63 uint16_t data_size GNUNET_PACKED;
64 64
65 /** 65 /**
66 * Type of the GNS/DNS record, network byte order. 66 * Flags for the record, network byte order.
67 */ 67 */
68 uint32_t record_type GNUNET_PACKED; 68 uint16_t flags GNUNET_PACKED;
69 69
70 /** 70 /**
71 * Flags for the record, network byte order. 71 * Type of the GNS/DNS record, network byte order.
72 */ 72 */
73 uint32_t flags GNUNET_PACKED; 73 uint32_t record_type GNUNET_PACKED;
74
74}; 75};
75 76
76GNUNET_NETWORK_STRUCT_END 77GNUNET_NETWORK_STRUCT_END
@@ -169,9 +170,9 @@ GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
169 rd[i].flags, 170 rd[i].flags,
170 (unsigned long long) rd[i].expiration_time); 171 (unsigned long long) rd[i].expiration_time);
171 rec.expiration_time = GNUNET_htonll (rd[i].expiration_time); 172 rec.expiration_time = GNUNET_htonll (rd[i].expiration_time);
172 rec.data_size = htonl ((uint32_t) rd[i].data_size); 173 rec.data_size = htons ((uint16_t) rd[i].data_size);
173 rec.record_type = htonl (rd[i].record_type); 174 rec.record_type = htonl (rd[i].record_type);
174 rec.flags = htonl (rd[i].flags); 175 rec.flags = htons (rd[i].flags);
175 if ((off + sizeof(rec) > dest_size) || 176 if ((off + sizeof(rec) > dest_size) ||
176 (off + sizeof(rec) < off)) 177 (off + sizeof(rec) < off))
177 { 178 {
@@ -214,13 +215,48 @@ GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
214 return dest_size; 215 return dest_size;
215} 216}
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 /*
233 * If we have found a byte string of zeroes, we have reached
234 * the padding
235 */
236 if (0 == GNUNET_memcmp (&rec, &rec_zero))
237 break;
238 GNUNET_memcpy (&rec,
239 &src[off],
240 sizeof(rec));
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}
217 253
218/** 254/**
219 * Deserialize the given records to the given destination. 255 * Deserialize the given records to the given destination.
220 * 256 *
221 * @param len size of the serialized record data 257 * @param len size of the serialized record data
222 * @param src the serialized record data 258 * @param src the serialized record data
223 * @param rd_count number of records in the rd array 259 * @param rd_count number of records parsed
224 * @param dest where to put the data 260 * @param dest where to put the data
225 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 261 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
226 */ 262 */
@@ -246,9 +282,9 @@ GNUNET_GNSRECORD_records_deserialize (size_t len,
246 &src[off], 282 &src[off],
247 sizeof(rec)); 283 sizeof(rec));
248 dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time); 284 dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time);
249 dest[i].data_size = ntohl ((uint32_t) rec.data_size); 285 dest[i].data_size = ntohs ((uint16_t) rec.data_size);
250 dest[i].record_type = ntohl (rec.record_type); 286 dest[i].record_type = ntohl (rec.record_type);
251 dest[i].flags = ntohl (rec.flags); 287 dest[i].flags = ntohs (rec.flags);
252 off += sizeof(rec); 288 off += sizeof(rec);
253 if ((off + dest[i].data_size > len) || 289 if ((off + dest[i].data_size > len) ||
254 (off + dest[i].data_size < off)) 290 (off + dest[i].data_size < off))