summaryrefslogtreecommitdiff
path: root/src/block/block.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/block/block.c')
-rw-r--r--src/block/block.c254
1 files changed, 126 insertions, 128 deletions
diff --git a/src/block/block.c b/src/block/block.c
index ecc98f88d..78e9bc2dd 100644
--- a/src/block/block.c
+++ b/src/block/block.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file block/block.c 22 * @file block/block.c
@@ -34,8 +34,7 @@
34/** 34/**
35 * Handle for a plugin. 35 * Handle for a plugin.
36 */ 36 */
37struct Plugin 37struct Plugin {
38{
39 /** 38 /**
40 * Name of the shared library. 39 * Name of the shared library.
41 */ 40 */
@@ -51,8 +50,7 @@ struct Plugin
51/** 50/**
52 * Handle to an initialized block library. 51 * Handle to an initialized block library.
53 */ 52 */
54struct GNUNET_BLOCK_Context 53struct GNUNET_BLOCK_Context {
55{
56 /** 54 /**
57 * Array of our plugins. 55 * Array of our plugins.
58 */ 56 */
@@ -78,18 +76,18 @@ struct GNUNET_BLOCK_Context
78 * @param hc where to store the result. 76 * @param hc where to store the result.
79 */ 77 */
80void 78void
81GNUNET_BLOCK_mingle_hash (const struct GNUNET_HashCode *in, 79GNUNET_BLOCK_mingle_hash(const struct GNUNET_HashCode *in,
82 uint32_t mingle_number, 80 uint32_t mingle_number,
83 struct GNUNET_HashCode *hc) 81 struct GNUNET_HashCode *hc)
84{ 82{
85 struct GNUNET_HashCode m; 83 struct GNUNET_HashCode m;
86 84
87 GNUNET_CRYPTO_hash (&mingle_number, 85 GNUNET_CRYPTO_hash(&mingle_number,
88 sizeof (uint32_t), 86 sizeof(uint32_t),
89 &m); 87 &m);
90 GNUNET_CRYPTO_hash_xor (&m, 88 GNUNET_CRYPTO_hash_xor(&m,
91 in, 89 in,
92 hc); 90 hc);
93} 91}
94 92
95 93
@@ -101,23 +99,23 @@ GNUNET_BLOCK_mingle_hash (const struct GNUNET_HashCode *in,
101 * @param lib_ret the plugin API 99 * @param lib_ret the plugin API
102 */ 100 */
103static void 101static void
104add_plugin (void *cls, 102add_plugin(void *cls,
105 const char *library_name, 103 const char *library_name,
106 void *lib_ret) 104 void *lib_ret)
107{ 105{
108 struct GNUNET_BLOCK_Context *ctx = cls; 106 struct GNUNET_BLOCK_Context *ctx = cls;
109 struct GNUNET_BLOCK_PluginFunctions *api = lib_ret; 107 struct GNUNET_BLOCK_PluginFunctions *api = lib_ret;
110 struct Plugin *plugin; 108 struct Plugin *plugin;
111 109
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 110 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
113 "Loading block plugin `%s'\n", 111 "Loading block plugin `%s'\n",
114 library_name); 112 library_name);
115 plugin = GNUNET_new (struct Plugin); 113 plugin = GNUNET_new(struct Plugin);
116 plugin->api = api; 114 plugin->api = api;
117 plugin->library_name = GNUNET_strdup (library_name); 115 plugin->library_name = GNUNET_strdup(library_name);
118 GNUNET_array_append (ctx->plugins, 116 GNUNET_array_append(ctx->plugins,
119 ctx->num_plugins, 117 ctx->num_plugins,
120 plugin); 118 plugin);
121} 119}
122 120
123 121
@@ -129,16 +127,16 @@ add_plugin (void *cls,
129 * @return NULL on error 127 * @return NULL on error
130 */ 128 */
131struct GNUNET_BLOCK_Context * 129struct GNUNET_BLOCK_Context *
132GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg) 130GNUNET_BLOCK_context_create(const struct GNUNET_CONFIGURATION_Handle *cfg)
133{ 131{
134 struct GNUNET_BLOCK_Context *ctx; 132 struct GNUNET_BLOCK_Context *ctx;
135 133
136 ctx = GNUNET_new (struct GNUNET_BLOCK_Context); 134 ctx = GNUNET_new(struct GNUNET_BLOCK_Context);
137 ctx->cfg = cfg; 135 ctx->cfg = cfg;
138 GNUNET_PLUGIN_load_all ("libgnunet_plugin_block_", 136 GNUNET_PLUGIN_load_all("libgnunet_plugin_block_",
139 (void *) cfg, 137 (void *)cfg,
140 &add_plugin, 138 &add_plugin,
141 ctx); 139 ctx);
142 return ctx; 140 return ctx;
143} 141}
144 142
@@ -149,21 +147,21 @@ GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
149 * @param ctx context to destroy 147 * @param ctx context to destroy
150 */ 148 */
151void 149void
152GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx) 150GNUNET_BLOCK_context_destroy(struct GNUNET_BLOCK_Context *ctx)
153{ 151{
154 struct Plugin *plugin; 152 struct Plugin *plugin;
155 153
156 for (unsigned int i = 0; i < ctx->num_plugins; i++) 154 for (unsigned int i = 0; i < ctx->num_plugins; i++)
157 { 155 {
158 plugin = ctx->plugins[i]; 156 plugin = ctx->plugins[i];
159 GNUNET_break (NULL == 157 GNUNET_break(NULL ==
160 GNUNET_PLUGIN_unload (plugin->library_name, 158 GNUNET_PLUGIN_unload(plugin->library_name,
161 plugin->api)); 159 plugin->api));
162 GNUNET_free (plugin->library_name); 160 GNUNET_free(plugin->library_name);
163 GNUNET_free (plugin); 161 GNUNET_free(plugin);
164 } 162 }
165 GNUNET_free (ctx->plugins); 163 GNUNET_free(ctx->plugins);
166 GNUNET_free (ctx); 164 GNUNET_free(ctx);
167} 165}
168 166
169 167
@@ -178,10 +176,10 @@ GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
178 * supported, #GNUNET_SYSERR on error 176 * supported, #GNUNET_SYSERR on error
179 */ 177 */
180int 178int
181GNUNET_BLOCK_group_serialize (struct GNUNET_BLOCK_Group *bg, 179GNUNET_BLOCK_group_serialize(struct GNUNET_BLOCK_Group *bg,
182 uint32_t *nonce, 180 uint32_t *nonce,
183 void **raw_data, 181 void **raw_data,
184 size_t *raw_data_size) 182 size_t *raw_data_size)
185{ 183{
186 *nonce = 0; 184 *nonce = 0;
187 *raw_data = NULL; 185 *raw_data = NULL;
@@ -190,10 +188,10 @@ GNUNET_BLOCK_group_serialize (struct GNUNET_BLOCK_Group *bg,
190 return GNUNET_NO; 188 return GNUNET_NO;
191 if (NULL == bg->serialize_cb) 189 if (NULL == bg->serialize_cb)
192 return GNUNET_NO; 190 return GNUNET_NO;
193 return bg->serialize_cb (bg, 191 return bg->serialize_cb(bg,
194 nonce, 192 nonce,
195 raw_data, 193 raw_data,
196 raw_data_size); 194 raw_data_size);
197} 195}
198 196
199 197
@@ -203,11 +201,11 @@ GNUNET_BLOCK_group_serialize (struct GNUNET_BLOCK_Group *bg,
203 * @param bg group to destroy, NULL is allowed 201 * @param bg group to destroy, NULL is allowed
204 */ 202 */
205void 203void
206GNUNET_BLOCK_group_destroy (struct GNUNET_BLOCK_Group *bg) 204GNUNET_BLOCK_group_destroy(struct GNUNET_BLOCK_Group *bg)
207{ 205{
208 if (NULL == bg) 206 if (NULL == bg)
209 return; 207 return;
210 bg->destroy_cb (bg); 208 bg->destroy_cb(bg);
211} 209}
212 210
213 211
@@ -224,24 +222,24 @@ GNUNET_BLOCK_group_destroy (struct GNUNET_BLOCK_Group *bg)
224 * #GNUNET_SYSERR if merging is not supported 222 * #GNUNET_SYSERR if merging is not supported
225 */ 223 */
226int 224int
227GNUNET_BLOCK_group_merge (struct GNUNET_BLOCK_Group *bg1, 225GNUNET_BLOCK_group_merge(struct GNUNET_BLOCK_Group *bg1,
228 struct GNUNET_BLOCK_Group *bg2) 226 struct GNUNET_BLOCK_Group *bg2)
229{ 227{
230 int ret; 228 int ret;
231 229
232 if (NULL == bg2) 230 if (NULL == bg2)
233 return GNUNET_OK; 231 return GNUNET_OK;
234 if (NULL == bg1) 232 if (NULL == bg1)
235 { 233 {
236 bg2->destroy_cb (bg2); 234 bg2->destroy_cb(bg2);
237 return GNUNET_OK; 235 return GNUNET_OK;
238 } 236 }
239 if (NULL == bg1->merge_cb) 237 if (NULL == bg1->merge_cb)
240 return GNUNET_SYSERR; 238 return GNUNET_SYSERR;
241 GNUNET_assert (bg1->merge_cb == bg1->merge_cb); 239 GNUNET_assert(bg1->merge_cb == bg1->merge_cb);
242 ret = bg1->merge_cb (bg1, 240 ret = bg1->merge_cb(bg1,
243 bg2); 241 bg2);
244 bg2->destroy_cb (bg2); 242 bg2->destroy_cb(bg2);
245 return ret; 243 return ret;
246} 244}
247 245
@@ -254,23 +252,23 @@ GNUNET_BLOCK_group_merge (struct GNUNET_BLOCK_Group *bg1,
254 * @return NULL if no matching plugin exists 252 * @return NULL if no matching plugin exists
255 */ 253 */
256static struct GNUNET_BLOCK_PluginFunctions * 254static struct GNUNET_BLOCK_PluginFunctions *
257find_plugin (struct GNUNET_BLOCK_Context *ctx, 255find_plugin(struct GNUNET_BLOCK_Context *ctx,
258 enum GNUNET_BLOCK_Type type) 256 enum GNUNET_BLOCK_Type type)
259{ 257{
260 struct Plugin *plugin; 258 struct Plugin *plugin;
261 unsigned int j; 259 unsigned int j;
262 260
263 for (unsigned i = 0; i < ctx->num_plugins; i++) 261 for (unsigned i = 0; i < ctx->num_plugins; i++)
264 {
265 plugin = ctx->plugins[i];
266 j = 0;
267 while (0 != (plugin->api->types[j]))
268 { 262 {
269 if (type == plugin->api->types[j]) 263 plugin = ctx->plugins[i];
270 return plugin->api; 264 j = 0;
271 j++; 265 while (0 != (plugin->api->types[j]))
266 {
267 if (type == plugin->api->types[j])
268 return plugin->api;
269 j++;
270 }
272 } 271 }
273 }
274 return NULL; 272 return NULL;
275} 273}
276 274
@@ -287,32 +285,32 @@ find_plugin (struct GNUNET_BLOCK_Context *ctx,
287 * by this @a type of block (this is not an error) 285 * by this @a type of block (this is not an error)
288 */ 286 */
289struct GNUNET_BLOCK_Group * 287struct GNUNET_BLOCK_Group *
290GNUNET_BLOCK_group_create (struct GNUNET_BLOCK_Context *ctx, 288GNUNET_BLOCK_group_create(struct GNUNET_BLOCK_Context *ctx,
291 enum GNUNET_BLOCK_Type type, 289 enum GNUNET_BLOCK_Type type,
292 uint32_t nonce, 290 uint32_t nonce,
293 const void *raw_data, 291 const void *raw_data,
294 size_t raw_data_size, 292 size_t raw_data_size,
295 ...) 293 ...)
296{ 294{
297 struct GNUNET_BLOCK_PluginFunctions *plugin; 295 struct GNUNET_BLOCK_PluginFunctions *plugin;
298 struct GNUNET_BLOCK_Group *bg; 296 struct GNUNET_BLOCK_Group *bg;
299 va_list ap; 297 va_list ap;
300 298
301 plugin = find_plugin (ctx, 299 plugin = find_plugin(ctx,
302 type); 300 type);
303 if (NULL == plugin) 301 if (NULL == plugin)
304 return NULL; 302 return NULL;
305 if (NULL == plugin->create_group) 303 if (NULL == plugin->create_group)
306 return NULL; 304 return NULL;
307 va_start (ap, 305 va_start(ap,
308 raw_data_size); 306 raw_data_size);
309 bg = plugin->create_group (plugin->cls, 307 bg = plugin->create_group(plugin->cls,
310 type, 308 type,
311 nonce, 309 nonce,
312 raw_data, 310 raw_data,
313 raw_data_size, 311 raw_data_size,
314 ap); 312 ap);
315 va_end (ap); 313 va_end(ap);
316 return bg; 314 return bg;
317} 315}
318 316
@@ -336,31 +334,31 @@ GNUNET_BLOCK_group_create (struct GNUNET_BLOCK_Context *ctx,
336 * @return characterization of result 334 * @return characterization of result
337 */ 335 */
338enum GNUNET_BLOCK_EvaluationResult 336enum GNUNET_BLOCK_EvaluationResult
339GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx, 337GNUNET_BLOCK_evaluate(struct GNUNET_BLOCK_Context *ctx,
340 enum GNUNET_BLOCK_Type type, 338 enum GNUNET_BLOCK_Type type,
341 struct GNUNET_BLOCK_Group *group, 339 struct GNUNET_BLOCK_Group *group,
342 enum GNUNET_BLOCK_EvaluationOptions eo, 340 enum GNUNET_BLOCK_EvaluationOptions eo,
343 const struct GNUNET_HashCode *query, 341 const struct GNUNET_HashCode *query,
344 const void *xquery, 342 const void *xquery,
345 size_t xquery_size, 343 size_t xquery_size,
346 const void *reply_block, 344 const void *reply_block,
347 size_t reply_block_size) 345 size_t reply_block_size)
348{ 346{
349 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, 347 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin(ctx,
350 type); 348 type);
351 349
352 if (NULL == plugin) 350 if (NULL == plugin)
353 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; 351 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
354 return plugin->evaluate (plugin->cls, 352 return plugin->evaluate(plugin->cls,
355 ctx, 353 ctx,
356 type, 354 type,
357 group, 355 group,
358 eo, 356 eo,
359 query, 357 query,
360 xquery, 358 xquery,
361 xquery_size, 359 xquery_size,
362 reply_block, 360 reply_block,
363 reply_block_size); 361 reply_block_size);
364} 362}
365 363
366 364
@@ -376,22 +374,22 @@ GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
376 * (or if extracting a key from a block of this type does not work) 374 * (or if extracting a key from a block of this type does not work)
377 */ 375 */
378int 376int
379GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx, 377GNUNET_BLOCK_get_key(struct GNUNET_BLOCK_Context *ctx,
380 enum GNUNET_BLOCK_Type type, 378 enum GNUNET_BLOCK_Type type,
381 const void *block, 379 const void *block,
382 size_t block_size, 380 size_t block_size,
383 struct GNUNET_HashCode *key) 381 struct GNUNET_HashCode *key)
384{ 382{
385 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, 383 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin(ctx,
386 type); 384 type);
387 385
388 if (plugin == NULL) 386 if (plugin == NULL)
389 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; 387 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
390 return plugin->get_key (plugin->cls, 388 return plugin->get_key(plugin->cls,
391 type, 389 type,
392 block, 390 block,
393 block_size, 391 block_size,
394 key); 392 key);
395} 393}
396 394
397 395
@@ -407,17 +405,17 @@ GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
407 * @return #GNUNET_SYSERR if not supported, #GNUNET_OK on success 405 * @return #GNUNET_SYSERR if not supported, #GNUNET_OK on success
408 */ 406 */
409int 407int
410GNUNET_BLOCK_group_set_seen (struct GNUNET_BLOCK_Group *bg, 408GNUNET_BLOCK_group_set_seen(struct GNUNET_BLOCK_Group *bg,
411 const struct GNUNET_HashCode *seen_results, 409 const struct GNUNET_HashCode *seen_results,
412 unsigned int seen_results_count) 410 unsigned int seen_results_count)
413{ 411{
414 if (NULL == bg) 412 if (NULL == bg)
415 return GNUNET_OK; 413 return GNUNET_OK;
416 if (NULL == bg->mark_seen_cb) 414 if (NULL == bg->mark_seen_cb)
417 return GNUNET_SYSERR; 415 return GNUNET_SYSERR;
418 bg->mark_seen_cb (bg, 416 bg->mark_seen_cb(bg,
419 seen_results, 417 seen_results,
420 seen_results_count); 418 seen_results_count);
421 return GNUNET_OK; 419 return GNUNET_OK;
422} 420}
423 421