gnunet-service-core_typemap.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011-2014 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 core/gnunet-service-core_typemap.c
  18. * @brief management of map that specifies which message types this peer supports
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_transport_service.h"
  24. #include "gnunet-service-core.h"
  25. #include "gnunet-service-core_sessions.h"
  26. #include "gnunet-service-core_typemap.h"
  27. #include <zlib.h>
  28. /**
  29. * A type map describing which messages a given neighbour is able
  30. * to process.
  31. */
  32. struct GSC_TypeMap
  33. {
  34. uint32_t bits[(UINT16_MAX + 1) / 32];
  35. };
  36. /**
  37. * Bitmap of message types this peer is able to handle.
  38. */
  39. static struct GSC_TypeMap my_type_map;
  40. /**
  41. * Counters for message types this peer is able to handle.
  42. */
  43. static uint8_t map_counters[UINT16_MAX + 1];
  44. /**
  45. * Current hash of our (uncompressed) type map.
  46. * Lazily computed when needed.
  47. */
  48. static struct GNUNET_HashCode my_tm_hash;
  49. /**
  50. * Is #my_tm_hash() current with respect to our type map?
  51. */
  52. static int hash_current;
  53. /**
  54. * Our type map changed, recompute its hash.
  55. */
  56. static void
  57. rehash_typemap ()
  58. {
  59. hash_current = GNUNET_NO;
  60. }
  61. /**
  62. * Hash the contents of a type map.
  63. *
  64. * @param tm map to hash
  65. * @param hc where to store the hash code
  66. */
  67. void
  68. GSC_TYPEMAP_hash (const struct GSC_TypeMap *tm, struct GNUNET_HashCode *hc)
  69. {
  70. GNUNET_CRYPTO_hash (tm, sizeof(struct GSC_TypeMap), hc);
  71. }
  72. /**
  73. * Check if the given hash matches our current type map.
  74. *
  75. * @param hc hash code to check if it matches our type map
  76. * @return #GNUNET_YES if the hash matches, #GNUNET_NO if not
  77. */
  78. int
  79. GSC_TYPEMAP_check_hash (const struct GNUNET_HashCode *hc)
  80. {
  81. if (GNUNET_NO == hash_current)
  82. {
  83. GSC_TYPEMAP_hash (&my_type_map, &my_tm_hash);
  84. hash_current = GNUNET_YES;
  85. }
  86. return (0 == memcmp (hc, &my_tm_hash, sizeof(struct GNUNET_HashCode)))
  87. ? GNUNET_YES
  88. : GNUNET_NO;
  89. }
  90. /**
  91. * Compute a type map message for this peer.
  92. *
  93. * @return this peers current type map message.
  94. */
  95. struct GNUNET_MessageHeader *
  96. GSC_TYPEMAP_compute_type_map_message ()
  97. {
  98. char *tmp;
  99. uLongf dlen;
  100. struct GNUNET_MessageHeader *hdr;
  101. #ifdef compressBound
  102. dlen = compressBound (sizeof(my_type_map));
  103. #else
  104. dlen = sizeof(my_type_map) + (sizeof(my_type_map) / 100) + 20;
  105. /* documentation says 100.1% oldSize + 12 bytes, but we
  106. * should be able to overshoot by more to be safe */
  107. #endif
  108. hdr = GNUNET_malloc (dlen + sizeof(struct GNUNET_MessageHeader));
  109. tmp = (char *) &hdr[1];
  110. if ((Z_OK != compress2 ((Bytef *) tmp,
  111. &dlen,
  112. (const Bytef *) &my_type_map,
  113. sizeof(my_type_map),
  114. 9)) ||
  115. (dlen >= sizeof(my_type_map)))
  116. {
  117. /* compression failed, use uncompressed map */
  118. dlen = sizeof(my_type_map);
  119. GNUNET_memcpy (tmp, &my_type_map, sizeof(my_type_map));
  120. hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
  121. }
  122. else
  123. {
  124. /* compression worked, use compressed map */
  125. hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
  126. }
  127. hdr->size = htons ((uint16_t) dlen + sizeof(struct GNUNET_MessageHeader));
  128. return hdr;
  129. }
  130. /**
  131. * Extract a type map from a TYPE_MAP message.
  132. *
  133. * @param msg a type map message
  134. * @return NULL on error
  135. */
  136. struct GSC_TypeMap *
  137. GSC_TYPEMAP_get_from_message (const struct GNUNET_MessageHeader *msg)
  138. {
  139. struct GSC_TypeMap *ret;
  140. uint16_t size;
  141. uLongf dlen;
  142. size = ntohs (msg->size);
  143. switch (ntohs (msg->type))
  144. {
  145. case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP:
  146. GNUNET_STATISTICS_update (GSC_stats,
  147. gettext_noop ("# type maps received"),
  148. 1,
  149. GNUNET_NO);
  150. if (size != sizeof(struct GSC_TypeMap))
  151. {
  152. GNUNET_break_op (0);
  153. return NULL;
  154. }
  155. ret = GNUNET_new (struct GSC_TypeMap);
  156. GNUNET_memcpy (ret, &msg[1], sizeof(struct GSC_TypeMap));
  157. return ret;
  158. case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
  159. GNUNET_STATISTICS_update (GSC_stats,
  160. gettext_noop ("# type maps received"),
  161. 1,
  162. GNUNET_NO);
  163. ret = GNUNET_new (struct GSC_TypeMap);
  164. dlen = sizeof(struct GSC_TypeMap);
  165. if ((Z_OK != uncompress ((Bytef *) ret,
  166. &dlen,
  167. (const Bytef *) &msg[1],
  168. (uLong) size)) ||
  169. (dlen != sizeof(struct GSC_TypeMap)))
  170. {
  171. GNUNET_break_op (0);
  172. GNUNET_free (ret);
  173. return NULL;
  174. }
  175. return ret;
  176. default:
  177. GNUNET_break (0);
  178. return NULL;
  179. }
  180. }
  181. /**
  182. * Send my type map to all connected peers (it got changed).
  183. */
  184. static void
  185. broadcast_my_type_map ()
  186. {
  187. struct GNUNET_MessageHeader *hdr;
  188. hdr = GSC_TYPEMAP_compute_type_map_message ();
  189. GNUNET_STATISTICS_update (GSC_stats,
  190. gettext_noop ("# updates to my type map"),
  191. 1,
  192. GNUNET_NO);
  193. GSC_SESSIONS_broadcast_typemap (hdr);
  194. GNUNET_free (hdr);
  195. }
  196. /**
  197. * Add a set of types to our type map.
  198. *
  199. * @param types array of message types supported by this peer
  200. * @param tlen number of entries in @a types
  201. */
  202. void
  203. GSC_TYPEMAP_add (const uint16_t *types, unsigned int tlen)
  204. {
  205. unsigned int i;
  206. int changed;
  207. changed = GNUNET_NO;
  208. for (i = 0; i < tlen; i++)
  209. {
  210. if (0 == map_counters[types[i]]++)
  211. {
  212. my_type_map.bits[types[i] / 32] |= (1 << (types[i] % 32));
  213. changed = GNUNET_YES;
  214. }
  215. }
  216. if (GNUNET_YES == changed)
  217. {
  218. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Typemap changed, broadcasting!\n");
  219. rehash_typemap ();
  220. broadcast_my_type_map ();
  221. }
  222. }
  223. /**
  224. * Remove a set of types from our type map.
  225. *
  226. * @param types array of types to remove
  227. * @param tlen length of the @a types array
  228. */
  229. void
  230. GSC_TYPEMAP_remove (const uint16_t *types, unsigned int tlen)
  231. {
  232. int changed;
  233. changed = GNUNET_NO;
  234. for (unsigned int i = 0; i < tlen; i++)
  235. {
  236. if (0 == --map_counters[types[i]])
  237. {
  238. my_type_map.bits[types[i] / 32] &= ~(1 << (types[i] % 32));
  239. changed = GNUNET_YES;
  240. }
  241. }
  242. if (GNUNET_YES == changed)
  243. {
  244. rehash_typemap ();
  245. broadcast_my_type_map ();
  246. }
  247. }
  248. /**
  249. * Test if any of the types from the types array is in the
  250. * given type map.
  251. *
  252. * @param tmap map to test
  253. * @param types array of types
  254. * @param tcnt number of entries in @a types
  255. * @return #GNUNET_YES if a type is in the map, #GNUNET_NO if not
  256. */
  257. int
  258. GSC_TYPEMAP_test_match (const struct GSC_TypeMap *tmap,
  259. const uint16_t *types,
  260. unsigned int tcnt)
  261. {
  262. if (NULL == tmap)
  263. return GNUNET_NO;
  264. if (0 == tcnt)
  265. return GNUNET_YES; /* matches all */
  266. for (unsigned int i = 0; i < tcnt; i++)
  267. if (0 != (tmap->bits[types[i] / 32] & (1 << (types[i] % 32))))
  268. return GNUNET_YES;
  269. return GNUNET_NO;
  270. }
  271. /**
  272. * Add additional types to a given typemap.
  273. *
  274. * @param tmap map to extend (not changed)
  275. * @param types array of types to add
  276. * @param tcnt number of entries in @a types
  277. * @return updated type map (fresh copy)
  278. */
  279. struct GSC_TypeMap *
  280. GSC_TYPEMAP_extend (const struct GSC_TypeMap *tmap,
  281. const uint16_t *types,
  282. unsigned int tcnt)
  283. {
  284. struct GSC_TypeMap *ret;
  285. ret = GNUNET_new (struct GSC_TypeMap);
  286. if (NULL != tmap)
  287. GNUNET_memcpy (ret, tmap, sizeof(struct GSC_TypeMap));
  288. for (unsigned int i = 0; i < tcnt; i++)
  289. ret->bits[types[i] / 32] |= (1 << (types[i] % 32));
  290. return ret;
  291. }
  292. /**
  293. * Create an empty type map.
  294. *
  295. * @return an empty type map
  296. */
  297. struct GSC_TypeMap *
  298. GSC_TYPEMAP_create ()
  299. {
  300. return GNUNET_new (struct GSC_TypeMap);
  301. }
  302. /**
  303. * Free the given type map.
  304. *
  305. * @param tmap a type map
  306. */
  307. void
  308. GSC_TYPEMAP_destroy (struct GSC_TypeMap *tmap)
  309. {
  310. GNUNET_free (tmap);
  311. }
  312. /**
  313. * Initialize typemap subsystem.
  314. */
  315. void
  316. GSC_TYPEMAP_init ()
  317. {
  318. /* nothing to do */
  319. }
  320. /**
  321. * Shutdown typemap subsystem.
  322. */
  323. void
  324. GSC_TYPEMAP_done ()
  325. {
  326. /* nothing to do */
  327. }
  328. /* end of gnunet-service-core_typemap.c */