fix
[oweals/gnunet.git] / src / block / block.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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    * NULL-terminated array of our plugins.
58    */
59   struct Plugin **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 GNUNET_HashCode * in, uint32_t mingle_number,
77                           GNUNET_HashCode * hc)
78 {
79   GNUNET_HashCode m;
80
81   GNUNET_CRYPTO_hash (&mingle_number, sizeof (uint32_t), &m);
82   GNUNET_CRYPTO_hash_xor (&m, in, hc);
83 }
84
85
86 /**
87  * Create a block context.  Loads the block plugins.
88  *
89  * @param cfg configuration to use
90  * @return NULL on error
91  */
92 struct GNUNET_BLOCK_Context *
93 GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
94 {
95   struct GNUNET_BLOCK_Context *ctx;
96   struct GNUNET_BLOCK_PluginFunctions *api;
97   struct Plugin *plugin;
98   unsigned int num_plugins;
99   char *plugs;
100   char *pos;
101   char *libname;
102
103   ctx = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_Context));
104   ctx->cfg = cfg;
105   num_plugins = 0;
106   if (GNUNET_OK ==
107       GNUNET_CONFIGURATION_get_value_string (cfg, "block", "PLUGINS", &plugs))
108   {
109     GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading block plugins `%s'\n"),
110                 plugs);
111     pos = strtok (plugs, " ");
112     while (pos != NULL)
113     {
114       GNUNET_asprintf (&libname, "libgnunet_plugin_block_%s", pos);
115       api = GNUNET_PLUGIN_load (libname, NULL);
116       if (api == NULL)
117       {
118         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119                     _("Failed to load block plugin `%s'\n"), pos);
120         GNUNET_free (libname);
121       }
122       else
123       {
124         plugin = GNUNET_malloc (sizeof (struct Plugin));
125         plugin->api = api;
126         plugin->library_name = libname;
127         GNUNET_array_append (ctx->plugins, num_plugins, plugin);
128       }
129       pos = strtok (NULL, " ");
130     }
131     GNUNET_free (plugs);
132   }
133   GNUNET_array_append (ctx->plugins, num_plugins, NULL);
134   return ctx;
135 }
136
137
138 /**
139  * Destroy the block context.
140  *
141  * @param ctx context to destroy
142  */
143 void
144 GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
145 {
146   unsigned int i;
147   struct Plugin *plugin;
148
149   i = 0;
150   while (NULL != (plugin = ctx->plugins[i]))
151   {
152     GNUNET_break (NULL ==
153                   GNUNET_PLUGIN_unload (plugin->library_name, plugin->api));
154     GNUNET_free (plugin->library_name);
155     GNUNET_free (plugin);
156     i++;
157   }
158   GNUNET_free (ctx->plugins);
159   GNUNET_free (ctx);
160 }
161
162
163 /**
164  * Find a plugin for the given type.
165  *
166  * @param ctx context to search
167  * @param type type to look for
168  * @return NULL if no matching plugin exists
169  */
170 static struct GNUNET_BLOCK_PluginFunctions *
171 find_plugin (struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type)
172 {
173   struct Plugin *plugin;
174   unsigned int i;
175   unsigned int j;
176
177   i = 0;
178   while (NULL != (plugin = ctx->plugins[i]))
179   {
180     j = 0;
181     while (0 != (plugin->api->types[j]))
182     {
183       if (type == plugin->api->types[j])
184         return plugin->api;
185       j++;
186     }
187     i++;
188   }
189   return NULL;
190 }
191
192
193 /**
194  * Function called to validate a reply or a request.  For
195  * request evaluation, simply pass "NULL" for the reply_block.
196  * Note that it is assumed that the reply has already been
197  * matched to the key (and signatures checked) as it would
198  * be done with the "get_key" function.
199  *
200  * @param ctx block contxt
201  * @param type block type
202  * @param query original query (hash)
203  * @param bf pointer to bloom filter associated with query; possibly updated (!)
204  * @param bf_mutator mutation value for bf
205  * @param xquery extended query data (can be NULL, depending on type)
206  * @param xquery_size number of bytes in xquery
207  * @param reply_block response to validate
208  * @param reply_block_size number of bytes in reply block
209  * @return characterization of result
210  */
211 enum GNUNET_BLOCK_EvaluationResult
212 GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
213                        enum GNUNET_BLOCK_Type type,
214                        const GNUNET_HashCode * query,
215                        struct GNUNET_CONTAINER_BloomFilter **bf,
216                        int32_t bf_mutator, const void *xquery,
217                        size_t xquery_size, const void *reply_block,
218                        size_t reply_block_size)
219 {
220   struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
221
222   if (plugin == NULL)
223     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
224   return plugin->evaluate (plugin->cls, type, query, bf, bf_mutator, xquery,
225                            xquery_size, reply_block, reply_block_size);
226 }
227
228
229 /**
230  * Function called to obtain the key for a block.
231  *
232  * @param ctx block context
233  * @param type block type
234  * @param block block to get the key for
235  * @param block_size number of bytes in block
236  * @param key set to the key (query) for the given block
237  * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
238  *         (or if extracting a key from a block of this type does not work)
239  */
240 int
241 GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
242                       enum GNUNET_BLOCK_Type type, const void *block,
243                       size_t block_size, GNUNET_HashCode * key)
244 {
245   struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
246
247   if (plugin == NULL)
248     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
249   return plugin->get_key (plugin->cls, type, block, block_size, key);
250 }
251
252
253 /**
254  * How many bytes should a bloomfilter be if we have already seen
255  * entry_count responses?  Note that GNUNET_CONSTANTS_BLOOMFILTER_K gives us the number
256  * of bits set per entry.  Furthermore, we should not re-size the
257  * filter too often (to keep it cheap).
258  *
259  * Since other peers will also add entries but not resize the filter,
260  * we should generally pick a slightly larger size than what the
261  * strict math would suggest.
262  *
263  * @return must be a power of two and smaller or equal to 2^15.
264  */
265 static size_t
266 compute_bloomfilter_size (unsigned int entry_count)
267 {
268   size_t size;
269   unsigned int ideal = (entry_count * GNUNET_CONSTANTS_BLOOMFILTER_K) / 4;
270   uint16_t max = 1 << 15;
271
272   if (entry_count > max)
273     return max;
274   size = 8;
275   while ((size < max) && (size < ideal))
276     size *= 2;
277   if (size > max)
278     return max;
279   return size;
280 }
281
282
283 /**
284  * Construct a bloom filter that would filter out the given
285  * results.
286  *
287  * @param bf_mutator mutation value to use
288  * @param seen_results results already seen
289  * @param seen_results_count number of entries in 'seen_results'
290  * @return NULL if seen_results_count is 0, otherwise a BF
291  *         that would match the given results.
292  */
293 struct GNUNET_CONTAINER_BloomFilter *
294 GNUNET_BLOCK_construct_bloomfilter (int32_t bf_mutator,
295                                     const GNUNET_HashCode *seen_results,
296                                     unsigned int seen_results_count)
297 {
298   struct GNUNET_CONTAINER_BloomFilter *bf;
299   GNUNET_HashCode mhash;
300   unsigned int i;
301   size_t nsize;
302
303   nsize = compute_bloomfilter_size (seen_results_count);
304   bf = GNUNET_CONTAINER_bloomfilter_init (NULL, nsize, GNUNET_CONSTANTS_BLOOMFILTER_K);
305   for (i = 0; i < seen_results_count; i++)
306   {
307     GNUNET_BLOCK_mingle_hash (&seen_results[i], bf_mutator, &mhash);
308     GNUNET_CONTAINER_bloomfilter_add (bf, &mhash);
309   }
310   return bf;
311 }
312
313
314 /* end of block.c */