glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / block / block.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010, 2017 GNUnet e.V.
4
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.
9
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.
14 */
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
29 /**
30  * Handle for a plugin.
31  */
32 struct Plugin
33 {
34   /**
35    * Name of the shared library.
36    */
37   char *library_name;
38
39   /**
40    * Plugin API.
41    */
42   struct GNUNET_BLOCK_PluginFunctions *api;
43 };
44
45
46 /**
47  * Handle to an initialized block library.
48  */
49 struct GNUNET_BLOCK_Context
50 {
51   /**
52    * Array of our plugins.
53    */
54   struct Plugin **plugins;
55
56   /**
57    * Size of the 'plugins' array.
58    */
59   unsigned int num_plugins;
60
61   /**
62    * Our configuration.
63    */
64   const struct GNUNET_CONFIGURATION_Handle *cfg;
65 };
66
67
68 /**
69  * Mingle hash with the mingle_number to produce different bits.
70  *
71  * @param in original hash code
72  * @param mingle_number number for hash permutation
73  * @param hc where to store the result.
74  */
75 void
76 GNUNET_BLOCK_mingle_hash (const struct GNUNET_HashCode *in,
77                           uint32_t mingle_number,
78                           struct GNUNET_HashCode *hc)
79 {
80   struct GNUNET_HashCode m;
81
82   GNUNET_CRYPTO_hash (&mingle_number,
83                       sizeof (uint32_t),
84                       &m);
85   GNUNET_CRYPTO_hash_xor (&m,
86                           in,
87                           hc);
88 }
89
90
91 /**
92  * Add a plugin to the list managed by the block library.
93  *
94  * @param cls the block context
95  * @param library_name name of the plugin
96  * @param lib_ret the plugin API
97  */
98 static void
99 add_plugin (void *cls,
100             const char *library_name,
101             void *lib_ret)
102 {
103   struct GNUNET_BLOCK_Context *ctx = cls;
104   struct GNUNET_BLOCK_PluginFunctions *api = lib_ret;
105   struct Plugin *plugin;
106
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108               "Loading block plugin `%s'\n",
109               library_name);
110   plugin = GNUNET_new (struct Plugin);
111   plugin->api = api;
112   plugin->library_name = GNUNET_strdup (library_name);
113   GNUNET_array_append (ctx->plugins,
114                        ctx->num_plugins,
115                        plugin);
116 }
117
118
119
120 /**
121  * Create a block context.  Loads the block plugins.
122  *
123  * @param cfg configuration to use
124  * @return NULL on error
125  */
126 struct GNUNET_BLOCK_Context *
127 GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
128 {
129   struct GNUNET_BLOCK_Context *ctx;
130
131   ctx = GNUNET_new (struct GNUNET_BLOCK_Context);
132   ctx->cfg = cfg;
133   GNUNET_PLUGIN_load_all ("libgnunet_plugin_block_",
134                           (void *) cfg,
135                           &add_plugin,
136                           ctx);
137   return ctx;
138 }
139
140
141 /**
142  * Destroy the block context.
143  *
144  * @param ctx context to destroy
145  */
146 void
147 GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
148 {
149   struct Plugin *plugin;
150
151   for (unsigned int i = 0; i < ctx->num_plugins; i++)
152   {
153     plugin = ctx->plugins[i];
154     GNUNET_break (NULL ==
155                   GNUNET_PLUGIN_unload (plugin->library_name,
156                                         plugin->api));
157     GNUNET_free (plugin->library_name);
158     GNUNET_free (plugin);
159   }
160   GNUNET_free (ctx->plugins);
161   GNUNET_free (ctx);
162 }
163
164
165 /**
166  * Serialize state of a block group.
167  *
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
174  */
175 int
176 GNUNET_BLOCK_group_serialize (struct GNUNET_BLOCK_Group *bg,
177                               uint32_t *nonce,
178                               void **raw_data,
179                               size_t *raw_data_size)
180 {
181   *nonce = 0;
182   *raw_data = NULL;
183   *raw_data_size = 0;
184   if (NULL == bg)
185     return GNUNET_NO;
186   if (NULL == bg->serialize_cb)
187     return GNUNET_NO;
188   return bg->serialize_cb (bg,
189                            nonce,
190                            raw_data,
191                            raw_data_size);
192 }
193
194
195 /**
196  * Destroy resources used by a block group.
197  *
198  * @param bg group to destroy, NULL is allowed
199  */
200 void
201 GNUNET_BLOCK_group_destroy (struct GNUNET_BLOCK_Group *bg)
202 {
203   if (NULL == bg)
204     return;
205   bg->destroy_cb (bg);
206 }
207
208
209 /**
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.
214  *
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
220  */
221 int
222 GNUNET_BLOCK_group_merge (struct GNUNET_BLOCK_Group *bg1,
223                           struct GNUNET_BLOCK_Group *bg2)
224 {
225   int ret;
226
227   if (NULL == bg2)
228     return GNUNET_OK;
229   if (NULL == bg1)
230   {
231     bg2->destroy_cb (bg2);
232     return GNUNET_OK;
233   }
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,
238                        bg2);
239   bg2->destroy_cb (bg2);
240   return ret;
241 }
242
243
244 /**
245  * Find a plugin for the given type.
246  *
247  * @param ctx context to search
248  * @param type type to look for
249  * @return NULL if no matching plugin exists
250  */
251 static struct GNUNET_BLOCK_PluginFunctions *
252 find_plugin (struct GNUNET_BLOCK_Context *ctx,
253              enum GNUNET_BLOCK_Type type)
254 {
255   struct Plugin *plugin;
256   unsigned int j;
257
258   for (unsigned i = 0; i < ctx->num_plugins; i++)
259   {
260     plugin = ctx->plugins[i];
261     j = 0;
262     while (0 != (plugin->api->types[j]))
263     {
264       if (type == plugin->api->types[j])
265         return plugin->api;
266       j++;
267     }
268   }
269   return NULL;
270 }
271
272
273 /**
274  * Create a new block group.
275  *
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)
283  */
284 struct GNUNET_BLOCK_Group *
285 GNUNET_BLOCK_group_create (struct GNUNET_BLOCK_Context *ctx,
286                            enum GNUNET_BLOCK_Type type,
287                            uint32_t nonce,
288                            const void *raw_data,
289                            size_t raw_data_size,
290                            ...)
291 {
292   struct GNUNET_BLOCK_PluginFunctions *plugin;
293   struct GNUNET_BLOCK_Group *bg;
294   va_list ap;
295
296   plugin = find_plugin (ctx,
297                         type);
298   if (NULL == plugin)
299     return NULL;
300   if (NULL == plugin->create_group)
301     return NULL;
302   va_start (ap,
303             raw_data_size);
304   bg = plugin->create_group (plugin->cls,
305                              type,
306                              nonce,
307                              raw_data,
308                              raw_data_size,
309                              ap);
310   va_end (ap);
311   return bg;
312 }
313
314
315 /**
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.
321  *
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
332  */
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,
339                        const void *xquery,
340                        size_t xquery_size,
341                        const void *reply_block,
342                        size_t reply_block_size)
343 {
344   struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
345                                                              type);
346
347   if (NULL == plugin)
348     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
349   return plugin->evaluate (plugin->cls,
350                            ctx,
351                            type,
352                            group,
353                            eo,
354                            query,
355                            xquery,
356                            xquery_size,
357                            reply_block,
358                            reply_block_size);
359 }
360
361
362 /**
363  * Function called to obtain the key for a block.
364  *
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)
372  */
373 int
374 GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
375                       enum GNUNET_BLOCK_Type type,
376                       const void *block,
377                       size_t block_size,
378                       struct GNUNET_HashCode *key)
379 {
380   struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
381                                                              type);
382
383   if (plugin == NULL)
384     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
385   return plugin->get_key (plugin->cls,
386                           type,
387                           block,
388                           block_size,
389                           key);
390 }
391
392
393 /**
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.
398  *
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
403  */
404 int
405 GNUNET_BLOCK_group_set_seen (struct GNUNET_BLOCK_Group *bg,
406                              const struct GNUNET_HashCode *seen_results,
407                              unsigned int seen_results_count)
408 {
409   if (NULL == bg)
410     return GNUNET_OK;
411   if (NULL == bg->mark_seen_cb)
412     return GNUNET_SYSERR;
413   bg->mark_seen_cb (bg,
414                     seen_results,
415                     seen_results_count);
416   return GNUNET_OK;
417 }
418
419
420 /* end of block.c */