2 This file is part of GNUnet.
3 Copyright (C) 2010, 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
18 * @brief library for data block manipulation
19 * @author Christian Grothoff
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"
30 * Handle for a plugin.
35 * Name of the shared library.
42 struct GNUNET_BLOCK_PluginFunctions *api;
47 * Handle to an initialized block library.
49 struct GNUNET_BLOCK_Context
52 * Array of our plugins.
54 struct Plugin **plugins;
57 * Size of the 'plugins' array.
59 unsigned int num_plugins;
64 const struct GNUNET_CONFIGURATION_Handle *cfg;
69 * Mingle hash with the mingle_number to produce different bits.
71 * @param in original hash code
72 * @param mingle_number number for hash permutation
73 * @param hc where to store the result.
76 GNUNET_BLOCK_mingle_hash (const struct GNUNET_HashCode *in,
77 uint32_t mingle_number,
78 struct GNUNET_HashCode *hc)
80 struct GNUNET_HashCode m;
82 GNUNET_CRYPTO_hash (&mingle_number,
85 GNUNET_CRYPTO_hash_xor (&m,
92 * Add a plugin to the list managed by the block library.
94 * @param cls the block context
95 * @param library_name name of the plugin
96 * @param lib_ret the plugin API
99 add_plugin (void *cls,
100 const char *library_name,
103 struct GNUNET_BLOCK_Context *ctx = cls;
104 struct GNUNET_BLOCK_PluginFunctions *api = lib_ret;
105 struct Plugin *plugin;
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108 "Loading block plugin `%s'\n",
110 plugin = GNUNET_new (struct Plugin);
112 plugin->library_name = GNUNET_strdup (library_name);
113 GNUNET_array_append (ctx->plugins,
121 * Create a block context. Loads the block plugins.
123 * @param cfg configuration to use
124 * @return NULL on error
126 struct GNUNET_BLOCK_Context *
127 GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
129 struct GNUNET_BLOCK_Context *ctx;
131 ctx = GNUNET_new (struct GNUNET_BLOCK_Context);
133 GNUNET_PLUGIN_load_all ("libgnunet_plugin_block_",
142 * Destroy the block context.
144 * @param ctx context to destroy
147 GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
149 struct Plugin *plugin;
151 for (unsigned int i = 0; i < ctx->num_plugins; i++)
153 plugin = ctx->plugins[i];
154 GNUNET_break (NULL ==
155 GNUNET_PLUGIN_unload (plugin->library_name,
157 GNUNET_free (plugin->library_name);
158 GNUNET_free (plugin);
160 GNUNET_free (ctx->plugins);
166 * Serialize state of a block group.
168 * @param bg group to serialize
169 * @param[out] nonce set to the nonce of the @a bg
170 * @param[out] raw_data set to the serialized state
171 * @param[out] raw_data_size set to the number of bytes in @a raw_data
172 * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not
173 * supported, #GNUNET_SYSERR on error
176 GNUNET_BLOCK_group_serialize (struct GNUNET_BLOCK_Group *bg,
179 size_t *raw_data_size)
186 if (NULL == bg->serialize_cb)
188 return bg->serialize_cb (bg,
196 * Destroy resources used by a block group.
198 * @param bg group to destroy, NULL is allowed
201 GNUNET_BLOCK_group_destroy (struct GNUNET_BLOCK_Group *bg)
210 * Try merging two block groups. Afterwards, @a bg1 should remain
211 * valid and contain the rules from both @a bg1 and @bg2, and
212 * @a bg2 should be destroyed (as part of this call). The latter
213 * should happen even if merging is not supported.
215 * @param[in,out] bg1 first group to merge, is updated
216 * @param bg2 second group to merge, is destroyed
217 * @return #GNUNET_OK on success,
218 * #GNUNET_NO if merge failed due to different nonce
219 * #GNUNET_SYSERR if merging is not supported
222 GNUNET_BLOCK_group_merge (struct GNUNET_BLOCK_Group *bg1,
223 struct GNUNET_BLOCK_Group *bg2)
231 bg2->destroy_cb (bg2);
234 if (NULL == bg1->merge_cb)
235 return GNUNET_SYSERR;
236 GNUNET_assert (bg1->merge_cb == bg1->merge_cb);
237 ret = bg1->merge_cb (bg1,
239 bg2->destroy_cb (bg2);
245 * Find a plugin for the given type.
247 * @param ctx context to search
248 * @param type type to look for
249 * @return NULL if no matching plugin exists
251 static struct GNUNET_BLOCK_PluginFunctions *
252 find_plugin (struct GNUNET_BLOCK_Context *ctx,
253 enum GNUNET_BLOCK_Type type)
255 struct Plugin *plugin;
258 for (unsigned i = 0; i < ctx->num_plugins; i++)
260 plugin = ctx->plugins[i];
262 while (0 != (plugin->api->types[j]))
264 if (type == plugin->api->types[j])
274 * Create a new block group.
276 * @param ctx block context in which the block group is created
277 * @param type type of the block for which we are creating the group
278 * @param nonce random value used to seed the group creation
279 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
280 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
281 * @return block group handle, NULL if block groups are not supported
282 * by this @a type of block (this is not an error)
284 struct GNUNET_BLOCK_Group *
285 GNUNET_BLOCK_group_create (struct GNUNET_BLOCK_Context *ctx,
286 enum GNUNET_BLOCK_Type type,
288 const void *raw_data,
289 size_t raw_data_size,
292 struct GNUNET_BLOCK_PluginFunctions *plugin;
293 struct GNUNET_BLOCK_Group *bg;
296 plugin = find_plugin (ctx,
300 if (NULL == plugin->create_group)
304 bg = plugin->create_group (plugin->cls,
316 * Function called to validate a reply or a request. For
317 * request evaluation, simply pass "NULL" for the reply_block.
318 * Note that it is assumed that the reply has already been
319 * matched to the key (and signatures checked) as it would
320 * be done with the "get_key" function.
322 * @param ctx block contxt
323 * @param type block type
324 * @param block block group to use
325 * @param eo control flags
326 * @param query original query (hash)
327 * @param xquery extended query data (can be NULL, depending on type)
328 * @param xquery_size number of bytes in @a xquery
329 * @param reply_block response to validate
330 * @param reply_block_size number of bytes in @a reply_block
331 * @return characterization of result
333 enum GNUNET_BLOCK_EvaluationResult
334 GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
335 enum GNUNET_BLOCK_Type type,
336 struct GNUNET_BLOCK_Group *group,
337 enum GNUNET_BLOCK_EvaluationOptions eo,
338 const struct GNUNET_HashCode *query,
341 const void *reply_block,
342 size_t reply_block_size)
344 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
348 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
349 return plugin->evaluate (plugin->cls,
363 * Function called to obtain the key for a block.
365 * @param ctx block context
366 * @param type block type
367 * @param block block to get the key for
368 * @param block_size number of bytes in @a block
369 * @param key set to the key (query) for the given block
370 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
371 * (or if extracting a key from a block of this type does not work)
374 GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
375 enum GNUNET_BLOCK_Type type,
378 struct GNUNET_HashCode *key)
380 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
384 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
385 return plugin->get_key (plugin->cls,
394 * Update block group to filter out the given results. Note that the
395 * use of a hash for seen results implies that the caller magically
396 * knows how the specific block engine hashes for filtering
397 * duplicates, so this API may not always apply.
399 * @param bf_mutator mutation value to use
400 * @param seen_results results already seen
401 * @param seen_results_count number of entries in @a seen_results
402 * @return #GNUNET_SYSERR if not supported, #GNUNET_OK on success
405 GNUNET_BLOCK_group_set_seen (struct GNUNET_BLOCK_Group *bg,
406 const struct GNUNET_HashCode *seen_results,
407 unsigned int seen_results_count)
411 if (NULL == bg->mark_seen_cb)
412 return GNUNET_SYSERR;
413 bg->mark_seen_cb (bg,