block.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010, 2017 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 block/block.c
  18. * @brief library for data block manipulation
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_constants.h"
  24. #include "gnunet_signatures.h"
  25. #include "gnunet_block_lib.h"
  26. #include "gnunet_block_plugin.h"
  27. /**
  28. * Handle for a plugin.
  29. */
  30. struct Plugin
  31. {
  32. /**
  33. * Name of the shared library.
  34. */
  35. char *library_name;
  36. /**
  37. * Plugin API.
  38. */
  39. struct GNUNET_BLOCK_PluginFunctions *api;
  40. };
  41. /**
  42. * Handle to an initialized block library.
  43. */
  44. struct GNUNET_BLOCK_Context
  45. {
  46. /**
  47. * Array of our plugins.
  48. */
  49. struct Plugin **plugins;
  50. /**
  51. * Size of the 'plugins' array.
  52. */
  53. unsigned int num_plugins;
  54. /**
  55. * Our configuration.
  56. */
  57. const struct GNUNET_CONFIGURATION_Handle *cfg;
  58. };
  59. /**
  60. * Mingle hash with the mingle_number to produce different bits.
  61. *
  62. * @param in original hash code
  63. * @param mingle_number number for hash permutation
  64. * @param hc where to store the result.
  65. */
  66. void
  67. GNUNET_BLOCK_mingle_hash (const struct GNUNET_HashCode *in,
  68. uint32_t mingle_number,
  69. struct GNUNET_HashCode *hc)
  70. {
  71. struct GNUNET_HashCode m;
  72. GNUNET_CRYPTO_hash (&mingle_number,
  73. sizeof(uint32_t),
  74. &m);
  75. GNUNET_CRYPTO_hash_xor (&m,
  76. in,
  77. hc);
  78. }
  79. /**
  80. * Add a plugin to the list managed by the block library.
  81. *
  82. * @param cls the block context
  83. * @param library_name name of the plugin
  84. * @param lib_ret the plugin API
  85. */
  86. static void
  87. add_plugin (void *cls,
  88. const char *library_name,
  89. void *lib_ret)
  90. {
  91. struct GNUNET_BLOCK_Context *ctx = cls;
  92. struct GNUNET_BLOCK_PluginFunctions *api = lib_ret;
  93. struct Plugin *plugin;
  94. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  95. "Loading block plugin `%s'\n",
  96. library_name);
  97. plugin = GNUNET_new (struct Plugin);
  98. plugin->api = api;
  99. plugin->library_name = GNUNET_strdup (library_name);
  100. GNUNET_array_append (ctx->plugins,
  101. ctx->num_plugins,
  102. plugin);
  103. }
  104. /**
  105. * Create a block context. Loads the block plugins.
  106. *
  107. * @param cfg configuration to use
  108. * @return NULL on error
  109. */
  110. struct GNUNET_BLOCK_Context *
  111. GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
  112. {
  113. struct GNUNET_BLOCK_Context *ctx;
  114. ctx = GNUNET_new (struct GNUNET_BLOCK_Context);
  115. ctx->cfg = cfg;
  116. GNUNET_PLUGIN_load_all ("libgnunet_plugin_block_",
  117. (void *) cfg,
  118. &add_plugin,
  119. ctx);
  120. return ctx;
  121. }
  122. /**
  123. * Destroy the block context.
  124. *
  125. * @param ctx context to destroy
  126. */
  127. void
  128. GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
  129. {
  130. struct Plugin *plugin;
  131. for (unsigned int i = 0; i < ctx->num_plugins; i++)
  132. {
  133. plugin = ctx->plugins[i];
  134. GNUNET_break (NULL ==
  135. GNUNET_PLUGIN_unload (plugin->library_name,
  136. plugin->api));
  137. GNUNET_free (plugin->library_name);
  138. GNUNET_free (plugin);
  139. }
  140. GNUNET_free (ctx->plugins);
  141. GNUNET_free (ctx);
  142. }
  143. /**
  144. * Serialize state of a block group.
  145. *
  146. * @param bg group to serialize
  147. * @param[out] nonce set to the nonce of the @a bg
  148. * @param[out] raw_data set to the serialized state
  149. * @param[out] raw_data_size set to the number of bytes in @a raw_data
  150. * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not
  151. * supported, #GNUNET_SYSERR on error
  152. */
  153. int
  154. GNUNET_BLOCK_group_serialize (struct GNUNET_BLOCK_Group *bg,
  155. uint32_t *nonce,
  156. void **raw_data,
  157. size_t *raw_data_size)
  158. {
  159. *nonce = 0;
  160. *raw_data = NULL;
  161. *raw_data_size = 0;
  162. if (NULL == bg)
  163. return GNUNET_NO;
  164. if (NULL == bg->serialize_cb)
  165. return GNUNET_NO;
  166. return bg->serialize_cb (bg,
  167. nonce,
  168. raw_data,
  169. raw_data_size);
  170. }
  171. /**
  172. * Destroy resources used by a block group.
  173. *
  174. * @param bg group to destroy, NULL is allowed
  175. */
  176. void
  177. GNUNET_BLOCK_group_destroy (struct GNUNET_BLOCK_Group *bg)
  178. {
  179. if (NULL == bg)
  180. return;
  181. bg->destroy_cb (bg);
  182. }
  183. /**
  184. * Try merging two block groups. Afterwards, @a bg1 should remain
  185. * valid and contain the rules from both @a bg1 and @bg2, and
  186. * @a bg2 should be destroyed (as part of this call). The latter
  187. * should happen even if merging is not supported.
  188. *
  189. * @param[in,out] bg1 first group to merge, is updated
  190. * @param bg2 second group to merge, is destroyed
  191. * @return #GNUNET_OK on success,
  192. * #GNUNET_NO if merge failed due to different nonce
  193. * #GNUNET_SYSERR if merging is not supported
  194. */
  195. int
  196. GNUNET_BLOCK_group_merge (struct GNUNET_BLOCK_Group *bg1,
  197. struct GNUNET_BLOCK_Group *bg2)
  198. {
  199. int ret;
  200. if (NULL == bg2)
  201. return GNUNET_OK;
  202. if (NULL == bg1)
  203. {
  204. bg2->destroy_cb (bg2);
  205. return GNUNET_OK;
  206. }
  207. if (NULL == bg1->merge_cb)
  208. return GNUNET_SYSERR;
  209. GNUNET_assert (bg1->merge_cb == bg1->merge_cb);
  210. ret = bg1->merge_cb (bg1,
  211. bg2);
  212. bg2->destroy_cb (bg2);
  213. return ret;
  214. }
  215. /**
  216. * Find a plugin for the given type.
  217. *
  218. * @param ctx context to search
  219. * @param type type to look for
  220. * @return NULL if no matching plugin exists
  221. */
  222. static struct GNUNET_BLOCK_PluginFunctions *
  223. find_plugin (struct GNUNET_BLOCK_Context *ctx,
  224. enum GNUNET_BLOCK_Type type)
  225. {
  226. struct Plugin *plugin;
  227. unsigned int j;
  228. for (unsigned i = 0; i < ctx->num_plugins; i++)
  229. {
  230. plugin = ctx->plugins[i];
  231. j = 0;
  232. while (0 != (plugin->api->types[j]))
  233. {
  234. if (type == plugin->api->types[j])
  235. return plugin->api;
  236. j++;
  237. }
  238. }
  239. return NULL;
  240. }
  241. /**
  242. * Create a new block group.
  243. *
  244. * @param ctx block context in which the block group is created
  245. * @param type type of the block for which we are creating the group
  246. * @param nonce random value used to seed the group creation
  247. * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
  248. * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
  249. * @return block group handle, NULL if block groups are not supported
  250. * by this @a type of block (this is not an error)
  251. */
  252. struct GNUNET_BLOCK_Group *
  253. GNUNET_BLOCK_group_create (struct GNUNET_BLOCK_Context *ctx,
  254. enum GNUNET_BLOCK_Type type,
  255. uint32_t nonce,
  256. const void *raw_data,
  257. size_t raw_data_size,
  258. ...)
  259. {
  260. struct GNUNET_BLOCK_PluginFunctions *plugin;
  261. struct GNUNET_BLOCK_Group *bg;
  262. va_list ap;
  263. plugin = find_plugin (ctx,
  264. type);
  265. if (NULL == plugin)
  266. return NULL;
  267. if (NULL == plugin->create_group)
  268. return NULL;
  269. va_start (ap,
  270. raw_data_size);
  271. bg = plugin->create_group (plugin->cls,
  272. type,
  273. nonce,
  274. raw_data,
  275. raw_data_size,
  276. ap);
  277. va_end (ap);
  278. return bg;
  279. }
  280. /**
  281. * Function called to validate a reply or a request. For
  282. * request evaluation, simply pass "NULL" for the reply_block.
  283. * Note that it is assumed that the reply has already been
  284. * matched to the key (and signatures checked) as it would
  285. * be done with the "get_key" function.
  286. *
  287. * @param ctx block contxt
  288. * @param type block type
  289. * @param block block group to use
  290. * @param eo control flags
  291. * @param query original query (hash)
  292. * @param xquery extended query data (can be NULL, depending on type)
  293. * @param xquery_size number of bytes in @a xquery
  294. * @param reply_block response to validate
  295. * @param reply_block_size number of bytes in @a reply_block
  296. * @return characterization of result
  297. */
  298. enum GNUNET_BLOCK_EvaluationResult
  299. GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
  300. enum GNUNET_BLOCK_Type type,
  301. struct GNUNET_BLOCK_Group *group,
  302. enum GNUNET_BLOCK_EvaluationOptions eo,
  303. const struct GNUNET_HashCode *query,
  304. const void *xquery,
  305. size_t xquery_size,
  306. const void *reply_block,
  307. size_t reply_block_size)
  308. {
  309. struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
  310. type);
  311. if (NULL == plugin)
  312. return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
  313. return plugin->evaluate (plugin->cls,
  314. ctx,
  315. type,
  316. group,
  317. eo,
  318. query,
  319. xquery,
  320. xquery_size,
  321. reply_block,
  322. reply_block_size);
  323. }
  324. /**
  325. * Function called to obtain the key for a block.
  326. *
  327. * @param ctx block context
  328. * @param type block type
  329. * @param block block to get the key for
  330. * @param block_size number of bytes in @a block
  331. * @param key set to the key (query) for the given block
  332. * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
  333. * (or if extracting a key from a block of this type does not work)
  334. */
  335. int
  336. GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
  337. enum GNUNET_BLOCK_Type type,
  338. const void *block,
  339. size_t block_size,
  340. struct GNUNET_HashCode *key)
  341. {
  342. struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
  343. type);
  344. if (plugin == NULL)
  345. return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
  346. return plugin->get_key (plugin->cls,
  347. type,
  348. block,
  349. block_size,
  350. key);
  351. }
  352. /**
  353. * Update block group to filter out the given results. Note that the
  354. * use of a hash for seen results implies that the caller magically
  355. * knows how the specific block engine hashes for filtering
  356. * duplicates, so this API may not always apply.
  357. *
  358. * @param bf_mutator mutation value to use
  359. * @param seen_results results already seen
  360. * @param seen_results_count number of entries in @a seen_results
  361. * @return #GNUNET_SYSERR if not supported, #GNUNET_OK on success
  362. */
  363. int
  364. GNUNET_BLOCK_group_set_seen (struct GNUNET_BLOCK_Group *bg,
  365. const struct GNUNET_HashCode *seen_results,
  366. unsigned int seen_results_count)
  367. {
  368. if (NULL == bg)
  369. return GNUNET_OK;
  370. if (NULL == bg->mark_seen_cb)
  371. return GNUNET_SYSERR;
  372. bg->mark_seen_cb (bg,
  373. seen_results,
  374. seen_results_count);
  375. return GNUNET_OK;
  376. }
  377. /* end of block.c */