aboutsummaryrefslogtreecommitdiff
path: root/src/block/block.c
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/block.c
parenta42b4e0ed22b65631caea4bb456f3d8fc21f11b1 (diff)
downloadgnunet-2e9791b5b713120238ea95113537fa55ff49b95d.tar.gz
gnunet-2e9791b5b713120238ea95113537fa55ff49b95d.zip
new block lib
Diffstat (limited to 'src/block/block.c')
-rw-r--r--src/block/block.c236
1 files changed, 36 insertions, 200 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);