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