diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-09-12 13:08:54 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-09-12 13:08:54 +0000 |
commit | a42b4e0ed22b65631caea4bb456f3d8fc21f11b1 (patch) | |
tree | a3a1e30a7f8e7f8b30c75c96923249831a7e8b2e /src/block | |
parent | dd253b7f7591f0869f8ea14ee111b7d3b3e480b6 (diff) | |
download | gnunet-a42b4e0ed22b65631caea4bb456f3d8fc21f11b1.tar.gz gnunet-a42b4e0ed22b65631caea4bb456f3d8fc21f11b1.zip |
towards pluggable block library
Diffstat (limited to 'src/block')
-rw-r--r-- | src/block/Makefile.am | 23 | ||||
-rw-r--r-- | src/block/block.c | 181 | ||||
-rw-r--r-- | src/block/plugin_block.h | 117 | ||||
-rw-r--r-- | src/block/plugin_block_fs.c | 346 | ||||
-rw-r--r-- | src/block/plugin_block_template.c | 118 |
5 files changed, 784 insertions, 1 deletions
diff --git a/src/block/Makefile.am b/src/block/Makefile.am index b9585c260..238c48af3 100644 --- a/src/block/Makefile.am +++ b/src/block/Makefile.am | |||
@@ -1,5 +1,7 @@ | |||
1 | INCLUDES = -I$(top_srcdir)/src/include | 1 | INCLUDES = -I$(top_srcdir)/src/include |
2 | 2 | ||
3 | plugindir = $(libdir)/gnunet | ||
4 | |||
3 | if MINGW | 5 | if MINGW |
4 | WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols | 6 | WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols |
5 | endif | 7 | endif |
@@ -10,8 +12,27 @@ endif | |||
10 | 12 | ||
11 | lib_LTLIBRARIES = libgnunetblock.la | 13 | lib_LTLIBRARIES = libgnunetblock.la |
12 | 14 | ||
15 | plugin_LTLIBRARIES = \ | ||
16 | libgnunet_plugin_block_fs.la \ | ||
17 | libgnunet_plugin_block_template.la | ||
18 | |||
19 | libgnunet_plugin_block_fs_la_SOURCES = \ | ||
20 | plugin_block_fs.c | ||
21 | libgnunet_plugin_block_fs_la_LIBADD = \ | ||
22 | $(top_builddir)/src/util/libgnunetutil.la | ||
23 | libgnunet_plugin_block_fs_la_LDFLAGS = \ | ||
24 | $(GN_PLUGIN_LDFLAGS) | ||
25 | |||
26 | libgnunet_plugin_block_template_la_SOURCES = \ | ||
27 | plugin_block_template.c | ||
28 | libgnunet_plugin_block_template_la_LIBADD = \ | ||
29 | $(top_builddir)/src/util/libgnunetutil.la | ||
30 | libgnunet_plugin_block_template_la_LDFLAGS = \ | ||
31 | $(GN_PLUGIN_LDFLAGS) | ||
32 | |||
33 | |||
13 | libgnunetblock_la_SOURCES = \ | 34 | libgnunetblock_la_SOURCES = \ |
14 | block.c | 35 | block.c plugin_block.h |
15 | libgnunetblock_la_LIBADD = \ | 36 | libgnunetblock_la_LIBADD = \ |
16 | $(top_builddir)/src/util/libgnunetutil.la | 37 | $(top_builddir)/src/util/libgnunetutil.la |
17 | 38 | ||
diff --git a/src/block/block.c b/src/block/block.c index 72dedd7be..cb6837499 100644 --- a/src/block/block.c +++ b/src/block/block.c | |||
@@ -27,6 +27,7 @@ | |||
27 | #include "gnunet_util_lib.h" | 27 | #include "gnunet_util_lib.h" |
28 | #include "gnunet_signatures.h" | 28 | #include "gnunet_signatures.h" |
29 | #include "gnunet_block_lib.h" | 29 | #include "gnunet_block_lib.h" |
30 | #include "plugin_block.h" | ||
30 | 31 | ||
31 | /** | 32 | /** |
32 | * Check if the given KBlock is well-formed. | 33 | * Check if the given KBlock is well-formed. |
@@ -226,5 +227,185 @@ GNUNET_BLOCK_check_block (enum GNUNET_BLOCK_Type type, | |||
226 | return GNUNET_OK; | 227 | return GNUNET_OK; |
227 | } | 228 | } |
228 | 229 | ||
230 | /* ***************** NEW API ******************* */ | ||
231 | |||
232 | /** | ||
233 | * Handle for a plugin. | ||
234 | */ | ||
235 | struct Plugin | ||
236 | { | ||
237 | /** | ||
238 | * Name of the shared library. | ||
239 | */ | ||
240 | char *library_name; | ||
241 | |||
242 | /** | ||
243 | * Plugin API. | ||
244 | */ | ||
245 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
246 | }; | ||
247 | |||
248 | /** | ||
249 | * Handle to an initialized block library. | ||
250 | */ | ||
251 | struct GNUNET_BLOCK_Context | ||
252 | { | ||
253 | /** | ||
254 | * NULL-terminated array of our plugins. | ||
255 | */ | ||
256 | struct Plugin **plugins; | ||
257 | |||
258 | /** | ||
259 | * Our configuration. | ||
260 | */ | ||
261 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
262 | }; | ||
263 | |||
264 | |||
265 | /** | ||
266 | * Create a block context. Loads the block plugins. | ||
267 | * | ||
268 | * @param cfg configuration to use | ||
269 | * @return NULL on error | ||
270 | */ | ||
271 | struct GNUNET_BLOCK_Context * | ||
272 | GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
273 | { | ||
274 | struct GNUNET_BLOCK_Context *ctx; | ||
275 | unsigned int num_plugins; | ||
276 | |||
277 | ctx = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_Context)); | ||
278 | ctx->cfg = cfg; | ||
279 | num_plugins = 0; | ||
280 | /* FIXME: actually load plugins... */ | ||
281 | GNUNET_array_append (ctx->plugins, | ||
282 | num_plugins, | ||
283 | NULL); | ||
284 | return ctx; | ||
285 | } | ||
286 | |||
287 | |||
288 | /** | ||
289 | * Destroy the block context. | ||
290 | * | ||
291 | * @param ctx context to destroy | ||
292 | */ | ||
293 | void | ||
294 | GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx) | ||
295 | { | ||
296 | unsigned int i; | ||
297 | struct Plugin *plugin; | ||
298 | |||
299 | i = 0; | ||
300 | while (NULL != (plugin = ctx->plugins[i])) | ||
301 | { | ||
302 | GNUNET_break (NULL == | ||
303 | GNUNET_PLUGIN_unload (plugin->library_name, | ||
304 | plugin->api)); | ||
305 | GNUNET_free (plugin->library_name); | ||
306 | GNUNET_free (plugin); | ||
307 | i++; | ||
308 | } | ||
309 | GNUNET_free (ctx->plugins); | ||
310 | GNUNET_free (ctx); | ||
311 | } | ||
312 | |||
313 | |||
314 | /** | ||
315 | * Find a plugin for the given type. | ||
316 | * | ||
317 | * @param ctx context to search | ||
318 | * @param type type to look for | ||
319 | * @return NULL if no matching plugin exists | ||
320 | */ | ||
321 | static struct GNUNET_BLOCK_PluginFunctions * | ||
322 | find_plugin (struct GNUNET_BLOCK_Context *ctx, | ||
323 | enum GNUNET_BLOCK_Type type) | ||
324 | { | ||
325 | struct Plugin *plugin; | ||
326 | unsigned int i; | ||
327 | unsigned int j; | ||
328 | |||
329 | i = 0; | ||
330 | while (NULL != (plugin = ctx->plugins[i])) | ||
331 | { | ||
332 | j = 0; | ||
333 | while (0 != (plugin->api->types[j])) | ||
334 | { | ||
335 | if (type == plugin->api->types[j]) | ||
336 | return plugin->api; | ||
337 | j++; | ||
338 | } | ||
339 | i++; | ||
340 | } | ||
341 | return NULL; | ||
342 | } | ||
343 | |||
344 | |||
345 | /** | ||
346 | * Function called to validate a reply or a request. For | ||
347 | * request evaluation, simply pass "NULL" for the reply_block. | ||
348 | * Note that it is assumed that the reply has already been | ||
349 | * matched to the key (and signatures checked) as it would | ||
350 | * be done with the "get_key" function. | ||
351 | * | ||
352 | * @param ctx block contxt | ||
353 | * @param type block type | ||
354 | * @param query original query (hash) | ||
355 | * @param bf pointer to bloom filter associated with query; possibly updated (!) | ||
356 | * @param bf_mutator mutation value for bf | ||
357 | * @param xquery extrended query data (can be NULL, depending on type) | ||
358 | * @param xquery_size number of bytes in xquery | ||
359 | * @param reply_block response to validate | ||
360 | * @param reply_block_size number of bytes in reply block | ||
361 | * @return characterization of result | ||
362 | */ | ||
363 | enum GNUNET_BLOCK_EvaluationResult | ||
364 | GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx, | ||
365 | enum GNUNET_BLOCK_Type type, | ||
366 | const GNUNET_HashCode *query, | ||
367 | struct GNUNET_CONTAINER_BloomFilter **bf, | ||
368 | int32_t bf_mutator, | ||
369 | const void *xquery, | ||
370 | size_t xquery_size, | ||
371 | const void *reply_block, | ||
372 | size_t reply_block_size) | ||
373 | { | ||
374 | struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type); | ||
375 | |||
376 | if (plugin == NULL) | ||
377 | return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; | ||
378 | return plugin->evaluate (plugin->cls, | ||
379 | type, query, bf, bf_mutator, | ||
380 | xquery, xquery_size, reply_block, reply_block_size); | ||
381 | } | ||
382 | |||
383 | |||
384 | /** | ||
385 | * Function called to obtain the key for a block. | ||
386 | * | ||
387 | * @param ctx block context | ||
388 | * @param type block type | ||
389 | * @param block block to get the key for | ||
390 | * @param block_size number of bytes in block | ||
391 | * @param key set to the key (query) for the given block | ||
392 | * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported | ||
393 | * (or if extracting a key from a block of this type does not work) | ||
394 | */ | ||
395 | int | ||
396 | GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx, | ||
397 | enum GNUNET_BLOCK_Type type, | ||
398 | const void *block, | ||
399 | size_t block_size, | ||
400 | GNUNET_HashCode *key) | ||
401 | { | ||
402 | struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type); | ||
403 | |||
404 | if (plugin == NULL) | ||
405 | return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; | ||
406 | return plugin->get_key (plugin->cls, | ||
407 | type, block, block_size, key); | ||
408 | } | ||
409 | |||
229 | 410 | ||
230 | /* end of block.c */ | 411 | /* end of block.c */ |
diff --git a/src/block/plugin_block.h b/src/block/plugin_block.h new file mode 100644 index 000000000..671f83aad --- /dev/null +++ b/src/block/plugin_block.h | |||
@@ -0,0 +1,117 @@ | |||
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/plugin_block.h | ||
23 | * @brief API for block plugins. Each block plugin must conform to | ||
24 | * the API specified by this header. | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | #ifndef PLUGIN_BLOCK_H | ||
28 | #define PLUGIN_BLOCK_H | ||
29 | |||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_container_lib.h" | ||
32 | #include "gnunet_block_lib.h" | ||
33 | |||
34 | |||
35 | |||
36 | /** | ||
37 | * Function called to validate a reply or a request. For | ||
38 | * request evaluation, simply pass "NULL" for the reply_block. | ||
39 | * Note that it is assumed that the reply has already been | ||
40 | * matched to the key (and signatures checked) as it would | ||
41 | * be done with the "get_key" function. | ||
42 | * | ||
43 | * @param cls closure | ||
44 | * @param type block type | ||
45 | * @param query original query (hash) | ||
46 | * @param bf pointer to bloom filter associated with query; possibly updated (!) | ||
47 | * @param bf_mutator mutation value for bf | ||
48 | * @param xquery extrended query data (can be NULL, depending on type) | ||
49 | * @param xquery_size number of bytes in xquery | ||
50 | * @param reply_block response to validate | ||
51 | * @param reply_block_size number of bytes in reply block | ||
52 | * @return characterization of result | ||
53 | */ | ||
54 | typedef enum GNUNET_BLOCK_EvaluationResult | ||
55 | (*GNUNET_BLOCK_EvaluationFunction) (void *cls, | ||
56 | enum GNUNET_BLOCK_Type type, | ||
57 | const GNUNET_HashCode *query, | ||
58 | struct GNUNET_CONTAINER_BloomFilter **bf, | ||
59 | int32_t bf_mutator, | ||
60 | const void *xquery, | ||
61 | size_t xquery_size, | ||
62 | const void *reply_block, | ||
63 | size_t reply_block_size); | ||
64 | |||
65 | |||
66 | /** | ||
67 | * Function called to obtain the key for a block. | ||
68 | * | ||
69 | * @param cls closure | ||
70 | * @param type block type | ||
71 | * @param block block to get the key for | ||
72 | * @param block_size number of bytes in block | ||
73 | * @param key set to the key (query) for the given block | ||
74 | * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported | ||
75 | * (or if extracting a key from a block of this type does not work) | ||
76 | */ | ||
77 | typedef int | ||
78 | (*GNUNET_BLOCK_GetKeyFunction) (void *cls, | ||
79 | enum GNUNET_BLOCK_Type type, | ||
80 | const void *block, | ||
81 | size_t block_size, | ||
82 | GNUNET_HashCode *key); | ||
83 | |||
84 | |||
85 | |||
86 | /** | ||
87 | * Each plugin is required to return a pointer to a struct of this | ||
88 | * type as the return value from its entry point. | ||
89 | */ | ||
90 | struct GNUNET_BLOCK_PluginFunctions | ||
91 | { | ||
92 | |||
93 | /** | ||
94 | * Closure for all of the callbacks. | ||
95 | */ | ||
96 | void *cls; | ||
97 | |||
98 | /** | ||
99 | * 0-terminated array of block types supported by this plugin. | ||
100 | */ | ||
101 | const enum GNUNET_BLOCK_Type *types; | ||
102 | |||
103 | /** | ||
104 | * Main function of a block plugin. Allows us to check if a | ||
105 | * block matches a query. | ||
106 | */ | ||
107 | GNUNET_BLOCK_EvaluationFunction evaluate; | ||
108 | |||
109 | /** | ||
110 | * Obtain the key for a given block (if possible). | ||
111 | */ | ||
112 | GNUNET_BLOCK_GetKeyFunction get_key; | ||
113 | |||
114 | }; | ||
115 | |||
116 | |||
117 | #endif | ||
diff --git a/src/block/plugin_block_fs.c b/src/block/plugin_block_fs.c new file mode 100644 index 000000000..362932991 --- /dev/null +++ b/src/block/plugin_block_fs.c | |||
@@ -0,0 +1,346 @@ | |||
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/plugin_block_fs.c | ||
23 | * @brief blocks used for file-sharing | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "plugin_block.h" | ||
29 | #include "gnunet_signatures.h" | ||
30 | |||
31 | #define DEBUG_FS_BLOCK GNUNET_NO | ||
32 | |||
33 | /** | ||
34 | * Number of bits we set per entry in the bloomfilter. | ||
35 | * Do not change! | ||
36 | */ | ||
37 | #define BLOOMFILTER_K 16 | ||
38 | |||
39 | /** | ||
40 | * Mingle hash with the mingle_number to produce different bits. | ||
41 | */ | ||
42 | static void | ||
43 | mingle_hash (const GNUNET_HashCode * in, | ||
44 | int32_t mingle_number, | ||
45 | GNUNET_HashCode * hc) | ||
46 | { | ||
47 | GNUNET_HashCode m; | ||
48 | |||
49 | GNUNET_CRYPTO_hash (&mingle_number, | ||
50 | sizeof (int32_t), | ||
51 | &m); | ||
52 | GNUNET_CRYPTO_hash_xor (&m, in, hc); | ||
53 | } | ||
54 | |||
55 | |||
56 | /** | ||
57 | * Function called to validate a reply or a request. For | ||
58 | * request evaluation, simply pass "NULL" for the reply_block. | ||
59 | * Note that it is assumed that the reply has already been | ||
60 | * matched to the key (and signatures checked) as it would | ||
61 | * be done with the "get_key" function. | ||
62 | * | ||
63 | * @param cls closure | ||
64 | * @param type block type | ||
65 | * @param query original query (hash) | ||
66 | * @param bf pointer to bloom filter associated with query; possibly updated (!) | ||
67 | * @param bf_mutator mutation value for bf | ||
68 | * @param xquery extrended query data (can be NULL, depending on type) | ||
69 | * @param xquery_size number of bytes in xquery | ||
70 | * @param reply_block response to validate | ||
71 | * @param reply_block_size number of bytes in reply block | ||
72 | * @return characterization of result | ||
73 | */ | ||
74 | static enum GNUNET_BLOCK_EvaluationResult | ||
75 | block_plugin_fs_evaluate (void *cls, | ||
76 | enum GNUNET_BLOCK_Type type, | ||
77 | const GNUNET_HashCode *query, | ||
78 | struct GNUNET_CONTAINER_BloomFilter **bf, | ||
79 | int32_t bf_mutator, | ||
80 | const void *xquery, | ||
81 | size_t xquery_size, | ||
82 | const void *reply_block, | ||
83 | size_t reply_block_size) | ||
84 | { | ||
85 | const struct SBlock *sb; | ||
86 | GNUNET_HashCode chash; | ||
87 | GNUNET_HashCode mhash; | ||
88 | const GNUNET_HashCode *nsid; | ||
89 | GNUNET_HashCode sh; | ||
90 | |||
91 | switch (type) | ||
92 | { | ||
93 | case GNUNET_BLOCK_TYPE_DBLOCK: | ||
94 | case GNUNET_BLOCK_TYPE_IBLOCK: | ||
95 | if (xquery_size != 0) | ||
96 | { | ||
97 | GNUNET_break_op (0); | ||
98 | return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; | ||
99 | } | ||
100 | return GNUNET_BLOCK_EVALUATION_OK_LAST; | ||
101 | case GNUNET_BLOCK_TYPE_KBLOCK: | ||
102 | case GNUNET_BLOCK_TYPE_NBLOCK: | ||
103 | if (xquery_size != 0) | ||
104 | { | ||
105 | GNUNET_break_op (0); | ||
106 | return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; | ||
107 | } | ||
108 | if (reply_block == NULL) | ||
109 | return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; | ||
110 | GNUNET_CRYPTO_hash (reply_block, | ||
111 | reply_block_size, | ||
112 | &chash); | ||
113 | mingle_hash (&chash, bf_mutator, &mhash); | ||
114 | if (NULL != *bf) | ||
115 | { | ||
116 | if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, | ||
117 | &mhash)) | ||
118 | return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; | ||
119 | } | ||
120 | else | ||
121 | { | ||
122 | *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, | ||
123 | 8, | ||
124 | BLOOMFILTER_K); | ||
125 | } | ||
126 | GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash); | ||
127 | return GNUNET_BLOCK_EVALUATION_OK_MORE; | ||
128 | case GNUNET_BLOCK_TYPE_SBLOCK: | ||
129 | if (xquery_size != sizeof (GNUNET_HashCode)) | ||
130 | { | ||
131 | GNUNET_break_op (0); | ||
132 | return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; | ||
133 | } | ||
134 | if (reply_block == NULL) | ||
135 | return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; | ||
136 | nsid = xquery; | ||
137 | if (reply_block_size < sizeof (struct NBlock)) | ||
138 | return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; | ||
139 | sb = reply_block; | ||
140 | GNUNET_CRYPTO_hash (&sb->subspace, | ||
141 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | ||
142 | &sh); | ||
143 | if (0 != memcmp (nsid, | ||
144 | &sh, | ||
145 | sizeof (GNUNET_HashCode))) | ||
146 | { | ||
147 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
148 | _("Reply mismatched in terms of namespace. Discarded.\n")); | ||
149 | return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; | ||
150 | } | ||
151 | GNUNET_CRYPTO_hash (reply_block, | ||
152 | reply_block_size, | ||
153 | &chash); | ||
154 | mingle_hash (&chash, bf_mutator, &mhash); | ||
155 | if (NULL != *bf) | ||
156 | { | ||
157 | if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, | ||
158 | &mhash)) | ||
159 | return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; | ||
160 | } | ||
161 | else | ||
162 | { | ||
163 | *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, | ||
164 | 8, | ||
165 | BLOOMFILTER_K); | ||
166 | } | ||
167 | GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash); | ||
168 | return GNUNET_BLOCK_EVALUATION_OK_MORE; | ||
169 | default: | ||
170 | return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; | ||
171 | } | ||
172 | } | ||
173 | |||
174 | |||
175 | /** | ||
176 | * Function called to obtain the key for a block. | ||
177 | * | ||
178 | * @param cls closure | ||
179 | * @param type block type | ||
180 | * @param block block to get the key for | ||
181 | * @param block_size number of bytes in block | ||
182 | * @param key set to the key (query) for the given block | ||
183 | * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported | ||
184 | * (or if extracting a key from a block of this type does not work) | ||
185 | */ | ||
186 | static int | ||
187 | block_plugin_fs_get_key (void *cls, | ||
188 | enum GNUNET_BLOCK_Type type, | ||
189 | const void *block, | ||
190 | size_t block_size, | ||
191 | GNUNET_HashCode *key) | ||
192 | { | ||
193 | const struct KBlock *kb; | ||
194 | const struct SBlock *sb; | ||
195 | const struct NBlock *nb; | ||
196 | |||
197 | switch (type) | ||
198 | { | ||
199 | case GNUNET_BLOCK_TYPE_DBLOCK: | ||
200 | case GNUNET_BLOCK_TYPE_IBLOCK: | ||
201 | GNUNET_CRYPTO_hash (block, block_size, key); | ||
202 | return GNUNET_OK; | ||
203 | case GNUNET_BLOCK_TYPE_KBLOCK: | ||
204 | if (block_size < sizeof (struct KBlock)) | ||
205 | { | ||
206 | GNUNET_break_op (0); | ||
207 | return GNUNET_SYSERR; | ||
208 | } | ||
209 | kb = block; | ||
210 | if (block_size - sizeof (struct KBlock) != | ||
211 | ntohl (kb->purpose.size) | ||
212 | - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) | ||
213 | - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) ) | ||
214 | { | ||
215 | GNUNET_break_op (0); | ||
216 | return GNUNET_SYSERR; | ||
217 | } | ||
218 | if (GNUNET_OK != | ||
219 | GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK, | ||
220 | &kb->purpose, | ||
221 | &kb->signature, | ||
222 | &kb->keyspace)) | ||
223 | { | ||
224 | GNUNET_break_op (0); | ||
225 | return GNUNET_SYSERR; | ||
226 | } | ||
227 | if (key != NULL) | ||
228 | GNUNET_CRYPTO_hash (&kb->keyspace, | ||
229 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | ||
230 | key); | ||
231 | return GNUNET_OK; | ||
232 | case GNUNET_BLOCK_TYPE_SBLOCK: | ||
233 | if (block_size < sizeof (struct SBlock)) | ||
234 | { | ||
235 | GNUNET_break_op (0); | ||
236 | return GNUNET_SYSERR; | ||
237 | } | ||
238 | sb = block; | ||
239 | if (block_size != | ||
240 | ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature)) | ||
241 | { | ||
242 | GNUNET_break_op (0); | ||
243 | return GNUNET_SYSERR; | ||
244 | } | ||
245 | if (GNUNET_OK != | ||
246 | GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK, | ||
247 | &sb->purpose, | ||
248 | &sb->signature, | ||
249 | &sb->subspace)) | ||
250 | { | ||
251 | GNUNET_break_op (0); | ||
252 | return GNUNET_SYSERR; | ||
253 | } | ||
254 | if (key != NULL) | ||
255 | *key = sb->identifier; | ||
256 | return GNUNET_OK; | ||
257 | case GNUNET_BLOCK_TYPE_NBLOCK: | ||
258 | if (block_size < sizeof (struct NBlock)) | ||
259 | { | ||
260 | GNUNET_break_op (0); | ||
261 | return GNUNET_SYSERR; | ||
262 | } | ||
263 | nb = block; | ||
264 | if (block_size - sizeof (struct NBlock) != | ||
265 | ntohl (nb->ns_purpose.size) | ||
266 | - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) | ||
267 | - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) ) | ||
268 | { | ||
269 | GNUNET_break_op (0); | ||
270 | return GNUNET_SYSERR; | ||
271 | } | ||
272 | if (block_size != | ||
273 | ntohl (nb->ksk_purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature)) | ||
274 | { | ||
275 | GNUNET_break_op (0); | ||
276 | return GNUNET_SYSERR; | ||
277 | } | ||
278 | if (GNUNET_OK != | ||
279 | GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG, | ||
280 | &nb->ksk_purpose, | ||
281 | &nb->ksk_signature, | ||
282 | &nb->keyspace)) | ||
283 | { | ||
284 | GNUNET_break_op (0); | ||
285 | return GNUNET_SYSERR; | ||
286 | } | ||
287 | if (GNUNET_OK != | ||
288 | GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK, | ||
289 | &nb->ns_purpose, | ||
290 | &nb->ns_signature, | ||
291 | &nb->subspace)) | ||
292 | { | ||
293 | GNUNET_break_op (0); | ||
294 | return GNUNET_SYSERR; | ||
295 | } | ||
296 | /* FIXME: we used to xor ID with NSID, | ||
297 | why not here? */ | ||
298 | if (key != NULL) | ||
299 | GNUNET_CRYPTO_hash (&nb->keyspace, | ||
300 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | ||
301 | key); | ||
302 | return GNUNET_OK; | ||
303 | default: | ||
304 | return GNUNET_SYSERR; | ||
305 | } | ||
306 | } | ||
307 | |||
308 | |||
309 | /** | ||
310 | * Entry point for the plugin. | ||
311 | */ | ||
312 | void * | ||
313 | gnunet_plugin_block_fs_init (void *cls) | ||
314 | { | ||
315 | static enum GNUNET_BLOCK_Type types[] = | ||
316 | { | ||
317 | GNUNET_BLOCK_TYPE_DBLOCK, | ||
318 | GNUNET_BLOCK_TYPE_IBLOCK, | ||
319 | GNUNET_BLOCK_TYPE_KBLOCK, | ||
320 | GNUNET_BLOCK_TYPE_SBLOCK, | ||
321 | GNUNET_BLOCK_TYPE_NBLOCK, | ||
322 | GNUNET_BLOCK_TYPE_ANY /* end of list */ | ||
323 | }; | ||
324 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
325 | |||
326 | api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions)); | ||
327 | api->evaluate = &block_plugin_fs_evaluate; | ||
328 | api->get_key = &block_plugin_fs_get_key; | ||
329 | api->types = types; | ||
330 | return api; | ||
331 | } | ||
332 | |||
333 | |||
334 | /** | ||
335 | * Exit point from the plugin. | ||
336 | */ | ||
337 | void * | ||
338 | gnunet_plugin_block_fs_done (void *cls) | ||
339 | { | ||
340 | struct GNUNET_TRANSPORT_PluginFunctions *api = cls; | ||
341 | |||
342 | GNUNET_free (api); | ||
343 | return NULL; | ||
344 | } | ||
345 | |||
346 | /* end of plugin_block_fs.c */ | ||
diff --git a/src/block/plugin_block_template.c b/src/block/plugin_block_template.c new file mode 100644 index 000000000..016a3fc8f --- /dev/null +++ b/src/block/plugin_block_template.c | |||
@@ -0,0 +1,118 @@ | |||
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/plugin_block_template.c | ||
23 | * @brief template for a block plugin | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "plugin_block.h" | ||
29 | |||
30 | #define DEBUG_TEMPLATE GNUNET_NO | ||
31 | |||
32 | |||
33 | /** | ||
34 | * Function called to validate a reply or a request. For | ||
35 | * request evaluation, simply pass "NULL" for the reply_block. | ||
36 | * | ||
37 | * @param cls closure | ||
38 | * @param type block type | ||
39 | * @param query original query (hash) | ||
40 | * @param bf pointer to bloom filter associated with query; possibly updated (!) | ||
41 | * @param bf_mutator mutation value for bf | ||
42 | * @param xquery extrended query data (can be NULL, depending on type) | ||
43 | * @param xquery_size number of bytes in xquery | ||
44 | * @param reply_block response to validate | ||
45 | * @param reply_block_size number of bytes in reply block | ||
46 | * @return characterization of result | ||
47 | */ | ||
48 | static enum GNUNET_BLOCK_EvaluationResult | ||
49 | block_plugin_template_evaluate (void *cls, | ||
50 | enum GNUNET_BLOCK_Type type, | ||
51 | const GNUNET_HashCode *query, | ||
52 | struct GNUNET_CONTAINER_BloomFilter **bf, | ||
53 | int32_t bf_mutator, | ||
54 | const void *xquery, | ||
55 | size_t xquery_size, | ||
56 | const void *reply_block, | ||
57 | size_t reply_block_size) | ||
58 | { | ||
59 | return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; | ||
60 | } | ||
61 | |||
62 | |||
63 | /** | ||
64 | * Function called to obtain the key for a block. | ||
65 | * | ||
66 | * @param cls closure | ||
67 | * @param type block type | ||
68 | * @param block block to get the key for | ||
69 | * @param block_size number of bytes in block | ||
70 | * @param key set to the key (query) for the given block | ||
71 | * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported | ||
72 | * (or if extracting a key from a block of this type does not work) | ||
73 | */ | ||
74 | static int | ||
75 | block_plugin_template_get_key (void *cls, | ||
76 | enum GNUNET_BLOCK_Type type, | ||
77 | const void *block, | ||
78 | size_t block_size, | ||
79 | GNUNET_HashCode *key) | ||
80 | { | ||
81 | return GNUNET_SYSERR; | ||
82 | } | ||
83 | |||
84 | |||
85 | /** | ||
86 | * Entry point for the plugin. | ||
87 | */ | ||
88 | void * | ||
89 | gnunet_plugin_block_template_init (void *cls) | ||
90 | { | ||
91 | static enum GNUNET_BLOCK_Type types[] = | ||
92 | { | ||
93 | /* FIXME: insert supported block types here */ | ||
94 | GNUNET_BLOCK_TYPE_ANY /* end of list */ | ||
95 | }; | ||
96 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
97 | |||
98 | api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions)); | ||
99 | api->evaluate = &block_plugin_template_evaluate; | ||
100 | api->get_key = &block_plugin_template_get_key; | ||
101 | api->types = types; | ||
102 | return api; | ||
103 | } | ||
104 | |||
105 | |||
106 | /** | ||
107 | * Exit point from the plugin. | ||
108 | */ | ||
109 | void * | ||
110 | gnunet_plugin_block_template_done (void *cls) | ||
111 | { | ||
112 | struct GNUNET_TRANSPORT_PluginFunctions *api = cls; | ||
113 | |||
114 | GNUNET_free (api); | ||
115 | return NULL; | ||
116 | } | ||
117 | |||
118 | /* end of plugin_block_template.c */ | ||