2
0

gnsrecord_serialization.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file gnsrecord/gnsrecord_serialization.c
  18. * @brief API to serialize and deserialize GNS records
  19. * @author Martin Schanzenbach
  20. * @author Matthias Wachs
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_constants.h"
  26. #include "gnunet_signatures.h"
  27. #include "gnunet_arm_service.h"
  28. #include "gnunet_gnsrecord_lib.h"
  29. #include "gnunet_dnsparser_lib.h"
  30. #include "gnunet_tun_lib.h"
  31. #define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__)
  32. /**
  33. * Set to 1 to check that all records are well-formed (can be converted
  34. * to string) during serialization/deserialization.
  35. */
  36. #define DEBUG_GNSRECORDS 0
  37. GNUNET_NETWORK_STRUCT_BEGIN
  38. /**
  39. * Internal format of a record in the serialized form.
  40. */
  41. struct NetworkRecord
  42. {
  43. /**
  44. * Expiration time for the DNS record; relative or absolute depends
  45. * on @e flags, network byte order.
  46. */
  47. uint64_t expiration_time GNUNET_PACKED;
  48. /**
  49. * Number of bytes in 'data', network byte order.
  50. */
  51. uint32_t data_size GNUNET_PACKED;
  52. /**
  53. * Type of the GNS/DNS record, network byte order.
  54. */
  55. uint32_t record_type GNUNET_PACKED;
  56. /**
  57. * Flags for the record, network byte order.
  58. */
  59. uint32_t flags GNUNET_PACKED;
  60. };
  61. GNUNET_NETWORK_STRUCT_END
  62. /**
  63. * Calculate how many bytes we will need to serialize the given
  64. * records.
  65. *
  66. * @param rd_count number of records in the rd array
  67. * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
  68. * @return the required size to serialize, -1 on error
  69. */
  70. ssize_t
  71. GNUNET_GNSRECORD_records_get_size (unsigned int rd_count,
  72. const struct GNUNET_GNSRECORD_Data *rd)
  73. {
  74. size_t ret;
  75. if (0 == rd_count)
  76. return 0;
  77. ret = sizeof(struct NetworkRecord) * rd_count;
  78. for (unsigned int i = 0; i < rd_count; i++)
  79. {
  80. if ((ret + rd[i].data_size) < ret)
  81. {
  82. GNUNET_break (0);
  83. return -1;
  84. }
  85. ret += rd[i].data_size;
  86. #if DEBUG_GNSRECORDS
  87. {
  88. char *str;
  89. str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
  90. rd[i].data,
  91. rd[i].data_size);
  92. if (NULL == str)
  93. {
  94. GNUNET_break_op (0);
  95. return -1;
  96. }
  97. GNUNET_free (str);
  98. }
  99. #endif
  100. }
  101. if (ret > SSIZE_MAX)
  102. {
  103. GNUNET_break (0);
  104. return -1;
  105. }
  106. // Do not pad PKEY
  107. if (GNUNET_GNSRECORD_TYPE_PKEY == rd->record_type)
  108. return ret;
  109. /**
  110. * Efficiently round up to the next
  111. * power of 2 for padding
  112. * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  113. */ret--;
  114. ret |= ret >> 1;
  115. ret |= ret >> 2;
  116. ret |= ret >> 4;
  117. ret |= ret >> 8;
  118. ret |= ret >> 16;
  119. ret++;
  120. return (ssize_t) ret;
  121. }
  122. /**
  123. * Serialize the given records to the given destination buffer.
  124. *
  125. * @param rd_count number of records in the rd array
  126. * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
  127. * @param dest_size size of the destination array
  128. * @param dest where to write the result
  129. * @return the size of serialized records, -1 if records do not fit
  130. */
  131. ssize_t
  132. GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
  133. const struct GNUNET_GNSRECORD_Data *rd,
  134. size_t dest_size,
  135. char *dest)
  136. {
  137. struct NetworkRecord rec;
  138. size_t off;
  139. off = 0;
  140. for (unsigned int i = 0; i < rd_count; i++)
  141. {
  142. LOG (GNUNET_ERROR_TYPE_DEBUG,
  143. "Serializing record %u with flags %d and expiration time %llu\n",
  144. i,
  145. rd[i].flags,
  146. (unsigned long long) rd[i].expiration_time);
  147. rec.expiration_time = GNUNET_htonll (rd[i].expiration_time);
  148. rec.data_size = htonl ((uint32_t) rd[i].data_size);
  149. rec.record_type = htonl (rd[i].record_type);
  150. rec.flags = htonl (rd[i].flags);
  151. if ((off + sizeof(rec) > dest_size) ||
  152. (off + sizeof(rec) < off))
  153. {
  154. GNUNET_break (0);
  155. return -1;
  156. }
  157. GNUNET_memcpy (&dest[off],
  158. &rec,
  159. sizeof(rec));
  160. off += sizeof(rec);
  161. if ((off + rd[i].data_size > dest_size) ||
  162. (off + rd[i].data_size < off))
  163. {
  164. GNUNET_break (0);
  165. return -1;
  166. }
  167. GNUNET_memcpy (&dest[off],
  168. rd[i].data,
  169. rd[i].data_size);
  170. off += rd[i].data_size;
  171. #if DEBUG_GNSRECORDS
  172. {
  173. char *str;
  174. str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
  175. rd[i].data,
  176. rd[i].data_size);
  177. if (NULL == str)
  178. {
  179. GNUNET_break_op (0);
  180. return -1;
  181. }
  182. GNUNET_free (str);
  183. }
  184. #endif
  185. }
  186. memset (&dest[off],
  187. 0,
  188. dest_size - off);
  189. return dest_size;
  190. }
  191. /**
  192. * Deserialize the given records to the given destination.
  193. *
  194. * @param len size of the serialized record data
  195. * @param src the serialized record data
  196. * @param rd_count number of records in the rd array
  197. * @param dest where to put the data
  198. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  199. */
  200. int
  201. GNUNET_GNSRECORD_records_deserialize (size_t len,
  202. const char *src,
  203. unsigned int rd_count,
  204. struct GNUNET_GNSRECORD_Data *dest)
  205. {
  206. struct NetworkRecord rec;
  207. size_t off;
  208. off = 0;
  209. for (unsigned int i = 0; i < rd_count; i++)
  210. {
  211. if ((off + sizeof(rec) > len) ||
  212. (off + sizeof(rec) < off))
  213. {
  214. GNUNET_break_op (0);
  215. return GNUNET_SYSERR;
  216. }
  217. GNUNET_memcpy (&rec,
  218. &src[off],
  219. sizeof(rec));
  220. dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time);
  221. dest[i].data_size = ntohl ((uint32_t) rec.data_size);
  222. dest[i].record_type = ntohl (rec.record_type);
  223. dest[i].flags = ntohl (rec.flags);
  224. off += sizeof(rec);
  225. if ((off + dest[i].data_size > len) ||
  226. (off + dest[i].data_size < off))
  227. {
  228. GNUNET_break_op (0);
  229. return GNUNET_SYSERR;
  230. }
  231. dest[i].data = &src[off];
  232. off += dest[i].data_size;
  233. #if GNUNET_EXTRA_LOGGING
  234. {
  235. char *str;
  236. str = GNUNET_GNSRECORD_value_to_string (dest[i].record_type,
  237. dest[i].data,
  238. dest[i].data_size);
  239. if (NULL == str)
  240. {
  241. GNUNET_break_op (0);
  242. return GNUNET_SYSERR;
  243. }
  244. GNUNET_free (str);
  245. }
  246. #endif
  247. LOG (GNUNET_ERROR_TYPE_DEBUG,
  248. "Deserialized record %u with flags %d and expiration time %llu\n",
  249. i,
  250. dest[i].flags,
  251. (unsigned long long) dest[i].expiration_time);
  252. }
  253. return GNUNET_OK;
  254. }
  255. /* end of gnsrecord_serialization.c */