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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @brief library for data block manipulation
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_constants.h"
27 #include "gnunet_signatures.h"
28 #include "gnunet_block_lib.h"
29 #include "gnunet_block_plugin.h"
33 * Handle for a plugin.
38 * Name of the shared library.
45 struct GNUNET_BLOCK_PluginFunctions *api;
50 * Handle to an initialized block library.
52 struct GNUNET_BLOCK_Context
55 * Array of our plugins.
57 struct Plugin **plugins;
60 * Size of the 'plugins' array.
62 unsigned int num_plugins;
67 const struct GNUNET_CONFIGURATION_Handle *cfg;
72 * Mingle hash with the mingle_number to produce different bits.
74 * @param in original hash code
75 * @param mingle_number number for hash permutation
76 * @param hc where to store the result.
79 GNUNET_BLOCK_mingle_hash (const struct GNUNET_HashCode *in,
80 uint32_t mingle_number,
81 struct GNUNET_HashCode *hc)
83 struct GNUNET_HashCode m;
85 GNUNET_CRYPTO_hash (&mingle_number,
88 GNUNET_CRYPTO_hash_xor (&m,
95 * Add a plugin to the list managed by the block library.
97 * @param cls the block context
98 * @param library_name name of the plugin
99 * @param lib_ret the plugin API
102 add_plugin (void *cls,
103 const char *library_name,
106 struct GNUNET_BLOCK_Context *ctx = cls;
107 struct GNUNET_BLOCK_PluginFunctions *api = lib_ret;
108 struct Plugin *plugin;
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111 "Loading block plugin `%s'\n",
113 plugin = GNUNET_new (struct Plugin);
115 plugin->library_name = GNUNET_strdup (library_name);
116 GNUNET_array_append (ctx->plugins,
124 * Create a block context. Loads the block plugins.
126 * @param cfg configuration to use
127 * @return NULL on error
129 struct GNUNET_BLOCK_Context *
130 GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
132 struct GNUNET_BLOCK_Context *ctx;
134 ctx = GNUNET_new (struct GNUNET_BLOCK_Context);
136 GNUNET_PLUGIN_load_all ("libgnunet_plugin_block_",
145 * Destroy the block context.
147 * @param ctx context to destroy
150 GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
152 struct Plugin *plugin;
154 for (unsigned int i = 0; i < ctx->num_plugins; i++)
156 plugin = ctx->plugins[i];
157 GNUNET_break (NULL ==
158 GNUNET_PLUGIN_unload (plugin->library_name,
160 GNUNET_free (plugin->library_name);
161 GNUNET_free (plugin);
163 GNUNET_free (ctx->plugins);
169 * Serialize state of a block group.
171 * @param bg group to serialize
172 * @param[out] nonce set to the nonce of the @a bg
173 * @param[out] raw_data set to the serialized state
174 * @param[out] raw_data_size set to the number of bytes in @a raw_data
175 * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not
176 * supported, #GNUNET_SYSERR on error
179 GNUNET_BLOCK_group_serialize (struct GNUNET_BLOCK_Group *bg,
182 size_t *raw_data_size)
189 if (NULL == bg->serialize_cb)
191 return bg->serialize_cb (bg,
199 * Destroy resources used by a block group.
201 * @param bg group to destroy, NULL is allowed
204 GNUNET_BLOCK_group_destroy (struct GNUNET_BLOCK_Group *bg)
213 * Try merging two block groups. Afterwards, @a bg1 should remain
214 * valid and contain the rules from both @a bg1 and @bg2, and
215 * @a bg2 should be destroyed (as part of this call). The latter
216 * should happen even if merging is not supported.
218 * @param[in,out] bg1 first group to merge, is updated
219 * @param bg2 second group to merge, is destroyed
220 * @return #GNUNET_OK on success,
221 * #GNUNET_NO if merge failed due to different nonce
222 * #GNUNET_SYSERR if merging is not supported
225 GNUNET_BLOCK_group_merge (struct GNUNET_BLOCK_Group *bg1,
226 struct GNUNET_BLOCK_Group *bg2)
234 bg2->destroy_cb (bg2);
237 if (NULL == bg1->merge_cb)
238 return GNUNET_SYSERR;
239 GNUNET_assert (bg1->merge_cb == bg1->merge_cb);
240 ret = bg1->merge_cb (bg1,
242 bg2->destroy_cb (bg2);
248 * Find a plugin for the given type.
250 * @param ctx context to search
251 * @param type type to look for
252 * @return NULL if no matching plugin exists
254 static struct GNUNET_BLOCK_PluginFunctions *
255 find_plugin (struct GNUNET_BLOCK_Context *ctx,
256 enum GNUNET_BLOCK_Type type)
258 struct Plugin *plugin;
261 for (unsigned i = 0; i < ctx->num_plugins; i++)
263 plugin = ctx->plugins[i];
265 while (0 != (plugin->api->types[j]))
267 if (type == plugin->api->types[j])
277 * Create a new block group.
279 * @param ctx block context in which the block group is created
280 * @param type type of the block for which we are creating the group
281 * @param nonce random value used to seed the group creation
282 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
283 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
284 * @return block group handle, NULL if block groups are not supported
285 * by this @a type of block (this is not an error)
287 struct GNUNET_BLOCK_Group *
288 GNUNET_BLOCK_group_create (struct GNUNET_BLOCK_Context *ctx,
289 enum GNUNET_BLOCK_Type type,
291 const void *raw_data,
292 size_t raw_data_size,
295 struct GNUNET_BLOCK_PluginFunctions *plugin;
296 struct GNUNET_BLOCK_Group *bg;
299 plugin = find_plugin (ctx,
303 if (NULL == plugin->create_group)
307 bg = plugin->create_group (plugin->cls,
319 * Function called to validate a reply or a request. For
320 * request evaluation, simply pass "NULL" for the reply_block.
321 * Note that it is assumed that the reply has already been
322 * matched to the key (and signatures checked) as it would
323 * be done with the "get_key" function.
325 * @param ctx block contxt
326 * @param type block type
327 * @param block block group to use
328 * @param eo control flags
329 * @param query original query (hash)
330 * @param xquery extended query data (can be NULL, depending on type)
331 * @param xquery_size number of bytes in @a xquery
332 * @param reply_block response to validate
333 * @param reply_block_size number of bytes in @a reply_block
334 * @return characterization of result
336 enum GNUNET_BLOCK_EvaluationResult
337 GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
338 enum GNUNET_BLOCK_Type type,
339 struct GNUNET_BLOCK_Group *group,
340 enum GNUNET_BLOCK_EvaluationOptions eo,
341 const struct GNUNET_HashCode *query,
344 const void *reply_block,
345 size_t reply_block_size)
347 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
351 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
352 return plugin->evaluate (plugin->cls,
366 * Function called to obtain the key for a block.
368 * @param ctx block context
369 * @param type block type
370 * @param block block to get the key for
371 * @param block_size number of bytes in @a block
372 * @param key set to the key (query) for the given block
373 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
374 * (or if extracting a key from a block of this type does not work)
377 GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
378 enum GNUNET_BLOCK_Type type,
381 struct GNUNET_HashCode *key)
383 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
387 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
388 return plugin->get_key (plugin->cls,
397 * Update block group to filter out the given results. Note that the
398 * use of a hash for seen results implies that the caller magically
399 * knows how the specific block engine hashes for filtering
400 * duplicates, so this API may not always apply.
402 * @param bf_mutator mutation value to use
403 * @param seen_results results already seen
404 * @param seen_results_count number of entries in @a seen_results
405 * @return #GNUNET_SYSERR if not supported, #GNUNET_OK on success
408 GNUNET_BLOCK_group_set_seen (struct GNUNET_BLOCK_Group *bg,
409 const struct GNUNET_HashCode *seen_results,
410 unsigned int seen_results_count)
414 if (NULL == bg->mark_seen_cb)
415 return GNUNET_SYSERR;
416 bg->mark_seen_cb (bg,