aboutsummaryrefslogtreecommitdiff
path: root/src/block
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-09-13 03:18:26 +0000
committerChristian Grothoff <christian@grothoff.org>2010-09-13 03:18:26 +0000
commit2e9791b5b713120238ea95113537fa55ff49b95d (patch)
tree98c0c6e0171109e7e4403fc50b6cae03a5c26c15 /src/block
parenta42b4e0ed22b65631caea4bb456f3d8fc21f11b1 (diff)
downloadgnunet-2e9791b5b713120238ea95113537fa55ff49b95d.tar.gz
gnunet-2e9791b5b713120238ea95113537fa55ff49b95d.zip
new block lib
Diffstat (limited to 'src/block')
-rw-r--r--src/block/block.c236
-rw-r--r--src/block/plugin_block_fs.c7
-rw-r--r--src/block/test_block.c61
3 files changed, 101 insertions, 203 deletions
diff --git a/src/block/block.c b/src/block/block.c
index cb6837499..8d8018b50 100644
--- a/src/block/block.c
+++ b/src/block/block.c
@@ -29,205 +29,6 @@
29#include "gnunet_block_lib.h" 29#include "gnunet_block_lib.h"
30#include "plugin_block.h" 30#include "plugin_block.h"
31 31
32/**
33 * Check if the given KBlock is well-formed.
34 *
35 * @param kb the kblock data (or at least "dsize" bytes claiming to be one)
36 * @param dsize size of "kb" in bytes; check for < sizeof(struct KBlock)!
37 * @param query where to store the query that this block answers
38 * @return GNUNET_OK if this is actually a well-formed KBlock
39 */
40static int
41check_kblock (const struct KBlock *kb,
42 size_t dsize,
43 GNUNET_HashCode *query)
44{
45 if (dsize < sizeof (struct KBlock))
46 {
47 GNUNET_break_op (0);
48 return GNUNET_SYSERR;
49 }
50 if (dsize - sizeof (struct KBlock) !=
51 ntohl (kb->purpose.size)
52 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
53 - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
54 {
55 GNUNET_break_op (0);
56 return GNUNET_SYSERR;
57 }
58 if (GNUNET_OK !=
59 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
60 &kb->purpose,
61 &kb->signature,
62 &kb->keyspace))
63 {
64 GNUNET_break_op (0);
65 return GNUNET_SYSERR;
66 }
67 if (query != NULL)
68 GNUNET_CRYPTO_hash (&kb->keyspace,
69 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
70 query);
71 return GNUNET_OK;
72}
73
74
75/**
76 * Check if the given NBlock is well-formed.
77 *
78 * @param nb the nblock data (or at least "dsize" bytes claiming to be one)
79 * @param dsize size of "nb" in bytes; check for < sizeof(struct NBlock)!
80 * @param query where to store the query that this block answers
81 * @return GNUNET_OK if this is actually a well-formed NBlock
82 */
83static int
84check_nblock (const struct NBlock *nb,
85 size_t dsize,
86 GNUNET_HashCode *query)
87{
88 if (dsize < sizeof (struct NBlock))
89 {
90 GNUNET_break_op (0);
91 return GNUNET_SYSERR;
92 }
93 if (dsize - sizeof (struct NBlock) !=
94 ntohl (nb->ns_purpose.size)
95 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
96 - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
97 {
98 GNUNET_break_op (0);
99 return GNUNET_SYSERR;
100 }
101 if (dsize !=
102 ntohl (nb->ksk_purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
103 {
104 GNUNET_break_op (0);
105 return GNUNET_SYSERR;
106 }
107 if (GNUNET_OK !=
108 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG,
109 &nb->ksk_purpose,
110 &nb->ksk_signature,
111 &nb->keyspace))
112 {
113 GNUNET_break_op (0);
114 return GNUNET_SYSERR;
115 }
116 if (GNUNET_OK !=
117 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK,
118 &nb->ns_purpose,
119 &nb->ns_signature,
120 &nb->subspace))
121 {
122 GNUNET_break_op (0);
123 return GNUNET_SYSERR;
124 }
125 if (query != NULL)
126 GNUNET_CRYPTO_hash (&nb->keyspace,
127 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
128 query);
129 return GNUNET_OK;
130}
131
132
133/**
134 * Check if the given SBlock is well-formed.
135 *
136 * @param sb the sblock data (or at least "dsize" bytes claiming to be one)
137 * @param dsize size of "kb" in bytes; check for < sizeof(struct SBlock)!
138 * @param query where to store the query that this block answers
139 * @return GNUNET_OK if this is actually a well-formed SBlock
140 */
141static int
142check_sblock (const struct SBlock *sb,
143 size_t dsize,
144 GNUNET_HashCode *query)
145{
146 if (dsize < sizeof (struct SBlock))
147 {
148 GNUNET_break_op (0);
149 return GNUNET_SYSERR;
150 }
151 if (dsize !=
152 ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
153 {
154 GNUNET_break_op (0);
155 return GNUNET_SYSERR;
156 }
157 if (GNUNET_OK !=
158 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK,
159 &sb->purpose,
160 &sb->signature,
161 &sb->subspace))
162 {
163 GNUNET_break_op (0);
164 return GNUNET_SYSERR;
165 }
166 if (query != NULL)
167 *query = sb->identifier;
168 return GNUNET_OK;
169}
170
171
172/**
173 * Check if the given block is well-formed (and of the given type).
174 *
175 * @param type type of the block
176 * @param block the block data (or at least "size" bytes claiming to be one)
177 * @param size size of "kb" in bytes; check that it is large enough
178 * @param query where to store the query that this block answers
179 * @return GNUNET_OK if this is actually a well-formed block,
180 * GNUNET_NO if we could not determine the query,
181 * GNUNET_SYSERR if the block is malformed
182 */
183int
184GNUNET_BLOCK_check_block (enum GNUNET_BLOCK_Type type,
185 const void *block,
186 size_t size,
187 GNUNET_HashCode *query)
188{
189 /* first, validate! */
190 switch (type)
191 {
192 case GNUNET_BLOCK_TYPE_DBLOCK:
193 case GNUNET_BLOCK_TYPE_IBLOCK:
194 GNUNET_CRYPTO_hash (block, size, query);
195 break;
196 case GNUNET_BLOCK_TYPE_KBLOCK:
197 if (GNUNET_OK !=
198 check_kblock (block,
199 size,
200 query))
201 return GNUNET_SYSERR;
202 break;
203 case GNUNET_BLOCK_TYPE_SBLOCK:
204 if (GNUNET_OK !=
205 check_sblock (block,
206 size,
207 query))
208 return GNUNET_SYSERR;
209 break;
210 case GNUNET_BLOCK_TYPE_NBLOCK:
211 if (GNUNET_OK !=
212 check_nblock (block,
213 size,
214 query))
215 return GNUNET_SYSERR;
216 return GNUNET_OK;
217 case GNUNET_BLOCK_TYPE_ONDEMAND:
218 if (size != sizeof (struct OnDemandBlock))
219 return GNUNET_SYSERR;
220 memset (query, 0, sizeof (GNUNET_HashCode));
221 return GNUNET_NO;
222 default:
223 /* unknown block type */
224 GNUNET_break_op (0);
225 return GNUNET_SYSERR;
226 }
227 return GNUNET_OK;
228}
229
230/* ***************** NEW API ******************* */
231 32
232/** 33/**
233 * Handle for a plugin. 34 * Handle for a plugin.
@@ -272,12 +73,47 @@ struct GNUNET_BLOCK_Context *
272GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg) 73GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
273{ 74{
274 struct GNUNET_BLOCK_Context *ctx; 75 struct GNUNET_BLOCK_Context *ctx;
76 struct GNUNET_BLOCK_PluginFunctions *api;
77 struct Plugin *plugin;
275 unsigned int num_plugins; 78 unsigned int num_plugins;
79 char *plugs;
80 char *pos;
81 char *libname;
276 82
277 ctx = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_Context)); 83 ctx = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_Context));
278 ctx->cfg = cfg; 84 ctx->cfg = cfg;
279 num_plugins = 0; 85 num_plugins = 0;
280 /* FIXME: actually load plugins... */ 86 if (GNUNET_OK ==
87 GNUNET_CONFIGURATION_get_value_string (cfg,
88 "block", "PLUGINS", &plugs))
89 {
90 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
91 _("Loading block plugins `%s'\n"), plugs);
92 pos = strtok (plugs, " ");
93 while (pos != NULL)
94 {
95 GNUNET_asprintf (&libname, "libgnunet_plugin_block_%s", pos);
96 api = GNUNET_PLUGIN_load (libname, NULL);
97 if (api == NULL)
98 {
99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
100 _("Failed to load block plugin `%s'\n"),
101 pos);
102 GNUNET_free (libname);
103 }
104 else
105 {
106 plugin = GNUNET_malloc (sizeof (struct Plugin));
107 plugin->api = api;
108 plugin->library_name = libname;
109 GNUNET_array_append (ctx->plugins,
110 num_plugins,
111 plugin);
112 }
113 pos = strtok (NULL, " ");
114 }
115 GNUNET_free (plugs);
116 }
281 GNUNET_array_append (ctx->plugins, 117 GNUNET_array_append (ctx->plugins,
282 num_plugins, 118 num_plugins,
283 NULL); 119 NULL);
diff --git a/src/block/plugin_block_fs.c b/src/block/plugin_block_fs.c
index 362932991..0626e458c 100644
--- a/src/block/plugin_block_fs.c
+++ b/src/block/plugin_block_fs.c
@@ -26,6 +26,7 @@
26 26
27#include "platform.h" 27#include "platform.h"
28#include "plugin_block.h" 28#include "plugin_block.h"
29#include "block_fs.h"
29#include "gnunet_signatures.h" 30#include "gnunet_signatures.h"
30 31
31#define DEBUG_FS_BLOCK GNUNET_NO 32#define DEBUG_FS_BLOCK GNUNET_NO
@@ -97,6 +98,8 @@ block_plugin_fs_evaluate (void *cls,
97 GNUNET_break_op (0); 98 GNUNET_break_op (0);
98 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; 99 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
99 } 100 }
101 if (reply_block == NULL)
102 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
100 return GNUNET_BLOCK_EVALUATION_OK_LAST; 103 return GNUNET_BLOCK_EVALUATION_OK_LAST;
101 case GNUNET_BLOCK_TYPE_KBLOCK: 104 case GNUNET_BLOCK_TYPE_KBLOCK:
102 case GNUNET_BLOCK_TYPE_NBLOCK: 105 case GNUNET_BLOCK_TYPE_NBLOCK:
@@ -310,7 +313,7 @@ block_plugin_fs_get_key (void *cls,
310 * Entry point for the plugin. 313 * Entry point for the plugin.
311 */ 314 */
312void * 315void *
313gnunet_plugin_block_fs_init (void *cls) 316libgnunet_plugin_block_fs_init (void *cls)
314{ 317{
315 static enum GNUNET_BLOCK_Type types[] = 318 static enum GNUNET_BLOCK_Type types[] =
316 { 319 {
@@ -335,7 +338,7 @@ gnunet_plugin_block_fs_init (void *cls)
335 * Exit point from the plugin. 338 * Exit point from the plugin.
336 */ 339 */
337void * 340void *
338gnunet_plugin_block_fs_done (void *cls) 341libgnunet_plugin_block_fs_done (void *cls)
339{ 342{
340 struct GNUNET_TRANSPORT_PluginFunctions *api = cls; 343 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
341 344
diff --git a/src/block/test_block.c b/src/block/test_block.c
index bdb806e5b..02719e5aa 100644
--- a/src/block/test_block.c
+++ b/src/block/test_block.c
@@ -29,8 +29,67 @@
29 29
30#define VERBOSE GNUNET_NO 30#define VERBOSE GNUNET_NO
31 31
32static int
33test_fs (struct GNUNET_BLOCK_Context *ctx)
34{
35 GNUNET_HashCode key;
36 char block[4];
37
38 memset (block, 1, sizeof (block));
39 if (GNUNET_OK !=
40 GNUNET_BLOCK_get_key (ctx,
41 GNUNET_BLOCK_TYPE_DBLOCK,
42 block,
43 sizeof (block),
44 &key))
45 return 1;
46 if (GNUNET_BLOCK_EVALUATION_OK_LAST !=
47 GNUNET_BLOCK_evaluate (ctx,
48 GNUNET_BLOCK_TYPE_DBLOCK,
49 &key,
50 NULL, 0,
51 NULL, 0,
52 block, sizeof (block)))
53 return 2;
54 if (GNUNET_BLOCK_EVALUATION_REQUEST_VALID !=
55 GNUNET_BLOCK_evaluate (ctx,
56 GNUNET_BLOCK_TYPE_DBLOCK,
57 &key,
58 NULL, 0,
59 NULL, 0,
60 NULL, 0))
61 return 4;
62 GNUNET_log_skip (1, GNUNET_NO);
63 if (GNUNET_BLOCK_EVALUATION_REQUEST_INVALID !=
64 GNUNET_BLOCK_evaluate (ctx,
65 GNUNET_BLOCK_TYPE_DBLOCK,
66 &key,
67 NULL, 0,
68 "bogus", 5,
69 NULL, 0))
70 return 8;
71 GNUNET_log_skip (0, GNUNET_YES);
72 return 0;
73}
74
32int 75int
33main (int argc, char *argv[]) 76main (int argc, char *argv[])
34{ 77{
35 return 0; /* testcase passed */ 78 int ret;
79 struct GNUNET_BLOCK_Context *ctx;
80 struct GNUNET_CONFIGURATION_Handle *cfg;
81
82 GNUNET_log_setup ("test-block", "WARNING", NULL);
83 cfg = GNUNET_CONFIGURATION_create ();
84 GNUNET_CONFIGURATION_set_value_string (cfg,
85 "block",
86 "PLUGINS",
87 "fs");
88 ctx = GNUNET_BLOCK_context_create (cfg);
89 ret = test_fs (ctx);
90 GNUNET_BLOCK_context_destroy (ctx);
91 GNUNET_CONFIGURATION_destroy (cfg);
92 if (ret != 0)
93 fprintf (stderr, "Tests failed: %d\n", ret);
94 return ret;
36} 95}