plugin datastore mysql
[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  * Find a plugin for the given type.
163  *
164  * @param ctx context to search
165  * @param type type to look for
166  * @return NULL if no matching plugin exists
167  */
168 static struct GNUNET_BLOCK_PluginFunctions *
169 find_plugin (struct GNUNET_BLOCK_Context *ctx,
170              enum GNUNET_BLOCK_Type type)
171 {
172   struct Plugin *plugin;
173   unsigned int i;
174   unsigned int j;
175
176   for (i = 0; i < ctx->num_plugins; i++)
177   {
178     plugin = ctx->plugins[i];
179     j = 0;
180     while (0 != (plugin->api->types[j]))
181     {
182       if (type == plugin->api->types[j])
183         return plugin->api;
184       j++;
185     }
186   }
187   return NULL;
188 }
189
190
191 /**
192  * Function called to validate a reply or a request.  For
193  * request evaluation, simply pass "NULL" for the reply_block.
194  * Note that it is assumed that the reply has already been
195  * matched to the key (and signatures checked) as it would
196  * be done with the "get_key" function.
197  *
198  * @param ctx block contxt
199  * @param type block type
200  * @param eo control flags
201  * @param query original query (hash)
202  * @param bf pointer to bloom filter associated with query; possibly updated (!)
203  * @param bf_mutator mutation value for @a bf
204  * @param xquery extended query data (can be NULL, depending on type)
205  * @param xquery_size number of bytes in @a xquery
206  * @param reply_block response to validate
207  * @param reply_block_size number of bytes in @a reply_block
208  * @return characterization of result
209  */
210 enum GNUNET_BLOCK_EvaluationResult
211 GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
212                        enum GNUNET_BLOCK_Type type,
213                        enum GNUNET_BLOCK_EvaluationOptions eo,
214                        const struct GNUNET_HashCode *query,
215                        struct GNUNET_CONTAINER_BloomFilter **bf,
216                        int32_t bf_mutator,
217                        const void *xquery,
218                        size_t xquery_size,
219                        const void *reply_block,
220                        size_t reply_block_size)
221 {
222   struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
223
224   if (plugin == NULL)
225     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
226   return plugin->evaluate (plugin->cls,
227                            type,
228                            eo,
229                            query,
230                            bf,
231                            bf_mutator,
232                            xquery,
233                            xquery_size,
234                            reply_block,
235                            reply_block_size);
236 }
237
238
239 /**
240  * Function called to obtain the key for a block.
241  *
242  * @param ctx block context
243  * @param type block type
244  * @param block block to get the key for
245  * @param block_size number of bytes in @a block
246  * @param key set to the key (query) for the given block
247  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
248  *         (or if extracting a key from a block of this type does not work)
249  */
250 int
251 GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
252                       enum GNUNET_BLOCK_Type type,
253                       const void *block,
254                       size_t block_size,
255                       struct GNUNET_HashCode *key)
256 {
257   struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
258
259   if (plugin == NULL)
260     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
261   return plugin->get_key (plugin->cls, type, block, block_size, key);
262 }
263
264
265 /**
266  * How many bytes should a bloomfilter be if we have already seen
267  * entry_count responses?  Note that #GNUNET_CONSTANTS_BLOOMFILTER_K
268  * gives us the number of bits set per entry.  Furthermore, we should
269  * not re-size the filter too often (to keep it cheap).
270  *
271  * Since other peers will also add entries but not resize the filter,
272  * we should generally pick a slightly larger size than what the
273  * strict math would suggest.
274  *
275  * @param entry_count expected number of entries in the Bloom filter
276  * @return must be a power of two and smaller or equal to 2^15.
277  */
278 static size_t
279 compute_bloomfilter_size (unsigned int entry_count)
280 {
281   size_t size;
282   unsigned int ideal = (entry_count * GNUNET_CONSTANTS_BLOOMFILTER_K) / 4;
283   uint16_t max = 1 << 15;
284
285   if (entry_count > max)
286     return max;
287   size = 8;
288   while ((size < max) && (size < ideal))
289     size *= 2;
290   if (size > max)
291     return max;
292   return size;
293 }
294
295
296 /**
297  * Construct a bloom filter that would filter out the given
298  * results.
299  *
300  * @param bf_mutator mutation value to use
301  * @param seen_results results already seen
302  * @param seen_results_count number of entries in @a seen_results
303  * @return NULL if seen_results_count is 0, otherwise a BF
304  *         that would match the given results.
305  */
306 struct GNUNET_CONTAINER_BloomFilter *
307 GNUNET_BLOCK_construct_bloomfilter (int32_t bf_mutator,
308                                     const struct GNUNET_HashCode *seen_results,
309                                     unsigned int seen_results_count)
310 {
311   struct GNUNET_CONTAINER_BloomFilter *bf;
312   struct GNUNET_HashCode mhash;
313   unsigned int i;
314   size_t nsize;
315
316   nsize = compute_bloomfilter_size (seen_results_count);
317   bf = GNUNET_CONTAINER_bloomfilter_init (NULL, nsize,
318                                           GNUNET_CONSTANTS_BLOOMFILTER_K);
319   for (i = 0; i < seen_results_count; i++)
320   {
321     GNUNET_BLOCK_mingle_hash (&seen_results[i], bf_mutator, &mhash);
322     GNUNET_CONTAINER_bloomfilter_add (bf, &mhash);
323   }
324   return bf;
325 }
326
327
328 /* end of block.c */