aboutsummaryrefslogtreecommitdiff
path: root/src/block/block.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-12-29 19:12:28 +0100
committerChristian Grothoff <christian@grothoff.org>2021-12-29 19:12:28 +0100
commitdaa9284567865177279a692eb722e257ed06923e (patch)
tree1954840c92a91027d2caee63668f9c7b2b79f04e /src/block/block.c
parent2536cd090b7e467e604cf5cfe5d1d93b32431876 (diff)
downloadgnunet-daa9284567865177279a692eb722e257ed06923e.tar.gz
gnunet-daa9284567865177279a692eb722e257ed06923e.zip
implement revised block API
Diffstat (limited to 'src/block/block.c')
-rw-r--r--src/block/block.c190
1 files changed, 77 insertions, 113 deletions
diff --git a/src/block/block.c b/src/block/block.c
index 975c0f747..5824946f7 100644
--- a/src/block/block.c
+++ b/src/block/block.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2010, 2017 GNUnet e.V. 3 Copyright (C) 2010, 2017, 2021 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
@@ -70,13 +70,6 @@ struct GNUNET_BLOCK_Context
70}; 70};
71 71
72 72
73/**
74 * Mingle hash with the mingle_number to produce different bits.
75 *
76 * @param in original hash code
77 * @param mingle_number number for hash permutation
78 * @param hc where to store the result.
79 */
80void 73void
81GNUNET_BLOCK_mingle_hash (const struct GNUNET_HashCode *in, 74GNUNET_BLOCK_mingle_hash (const struct GNUNET_HashCode *in,
82 uint32_t mingle_number, 75 uint32_t mingle_number,
@@ -121,12 +114,6 @@ add_plugin (void *cls,
121} 114}
122 115
123 116
124/**
125 * Create a block context. Loads the block plugins.
126 *
127 * @param cfg configuration to use
128 * @return NULL on error
129 */
130struct GNUNET_BLOCK_Context * 117struct GNUNET_BLOCK_Context *
131GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg) 118GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
132{ 119{
@@ -143,11 +130,6 @@ GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
143} 130}
144 131
145 132
146/**
147 * Destroy the block context.
148 *
149 * @param ctx context to destroy
150 */
151void 133void
152GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx) 134GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
153{ 135{
@@ -167,17 +149,7 @@ GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
167} 149}
168 150
169 151
170/** 152enum GNUNET_GenericReturnValue
171 * Serialize state of a block group.
172 *
173 * @param bg group to serialize
174 * @param[out] nonce set to the nonce of the @a bg
175 * @param[out] raw_data set to the serialized state
176 * @param[out] raw_data_size set to the number of bytes in @a raw_data
177 * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not
178 * supported, #GNUNET_SYSERR on error
179 */
180int
181GNUNET_BLOCK_group_serialize (struct GNUNET_BLOCK_Group *bg, 153GNUNET_BLOCK_group_serialize (struct GNUNET_BLOCK_Group *bg,
182 uint32_t *nonce, 154 uint32_t *nonce,
183 void **raw_data, 155 void **raw_data,
@@ -197,11 +169,6 @@ GNUNET_BLOCK_group_serialize (struct GNUNET_BLOCK_Group *bg,
197} 169}
198 170
199 171
200/**
201 * Destroy resources used by a block group.
202 *
203 * @param bg group to destroy, NULL is allowed
204 */
205void 172void
206GNUNET_BLOCK_group_destroy (struct GNUNET_BLOCK_Group *bg) 173GNUNET_BLOCK_group_destroy (struct GNUNET_BLOCK_Group *bg)
207{ 174{
@@ -211,23 +178,11 @@ GNUNET_BLOCK_group_destroy (struct GNUNET_BLOCK_Group *bg)
211} 178}
212 179
213 180
214/** 181enum GNUNET_GenericReturnValue
215 * Try merging two block groups. Afterwards, @a bg1 should remain
216 * valid and contain the rules from both @a bg1 and @bg2, and
217 * @a bg2 should be destroyed (as part of this call). The latter
218 * should happen even if merging is not supported.
219 *
220 * @param[in,out] bg1 first group to merge, is updated
221 * @param bg2 second group to merge, is destroyed
222 * @return #GNUNET_OK on success,
223 * #GNUNET_NO if merge failed due to different nonce
224 * #GNUNET_SYSERR if merging is not supported
225 */
226int
227GNUNET_BLOCK_group_merge (struct GNUNET_BLOCK_Group *bg1, 182GNUNET_BLOCK_group_merge (struct GNUNET_BLOCK_Group *bg1,
228 struct GNUNET_BLOCK_Group *bg2) 183 struct GNUNET_BLOCK_Group *bg2)
229{ 184{
230 int ret; 185 enum GNUNET_GenericReturnValue ret;
231 186
232 if (NULL == bg2) 187 if (NULL == bg2)
233 return GNUNET_OK; 188 return GNUNET_OK;
@@ -257,35 +212,18 @@ static struct GNUNET_BLOCK_PluginFunctions *
257find_plugin (struct GNUNET_BLOCK_Context *ctx, 212find_plugin (struct GNUNET_BLOCK_Context *ctx,
258 enum GNUNET_BLOCK_Type type) 213 enum GNUNET_BLOCK_Type type)
259{ 214{
260 struct Plugin *plugin;
261 unsigned int j;
262
263 for (unsigned i = 0; i < ctx->num_plugins; i++) 215 for (unsigned i = 0; i < ctx->num_plugins; i++)
264 { 216 {
265 plugin = ctx->plugins[i]; 217 struct Plugin *plugin = ctx->plugins[i];
266 j = 0; 218
267 while (0 != (plugin->api->types[j])) 219 for (unsigned int j = 0; 0 != plugin->api->types[j]; j++)
268 {
269 if (type == plugin->api->types[j]) 220 if (type == plugin->api->types[j])
270 return plugin->api; 221 return plugin->api;
271 j++;
272 }
273 } 222 }
274 return NULL; 223 return NULL;
275} 224}
276 225
277 226
278/**
279 * Create a new block group.
280 *
281 * @param ctx block context in which the block group is created
282 * @param type type of the block for which we are creating the group
283 * @param nonce random value used to seed the group creation
284 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
285 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
286 * @return block group handle, NULL if block groups are not supported
287 * by this @a type of block (this is not an error)
288 */
289struct GNUNET_BLOCK_Group * 227struct GNUNET_BLOCK_Group *
290GNUNET_BLOCK_group_create (struct GNUNET_BLOCK_Context *ctx, 228GNUNET_BLOCK_group_create (struct GNUNET_BLOCK_Context *ctx,
291 enum GNUNET_BLOCK_Type type, 229 enum GNUNET_BLOCK_Type type,
@@ -317,24 +255,6 @@ GNUNET_BLOCK_group_create (struct GNUNET_BLOCK_Context *ctx,
317} 255}
318 256
319 257
320/**
321 * Function called to validate a reply or a request. For
322 * request evaluation, simply pass "NULL" for the reply_block.
323 * Note that it is assumed that the reply has already been
324 * matched to the key (and signatures checked) as it would
325 * be done with the "get_key" function.
326 *
327 * @param ctx block contxt
328 * @param type block type
329 * @param block block group to use
330 * @param eo control flags
331 * @param query original query (hash)
332 * @param xquery extended query data (can be NULL, depending on type)
333 * @param xquery_size number of bytes in @a xquery
334 * @param reply_block response to validate
335 * @param reply_block_size number of bytes in @a reply_block
336 * @return characterization of result
337 */
338enum GNUNET_BLOCK_EvaluationResult 258enum GNUNET_BLOCK_EvaluationResult
339GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx, 259GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
340 enum GNUNET_BLOCK_Type type, 260 enum GNUNET_BLOCK_Type type,
@@ -364,18 +284,7 @@ GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
364} 284}
365 285
366 286
367/** 287enum GNUNET_GenericReturnValue
368 * Function called to obtain the key for a block.
369 *
370 * @param ctx block context
371 * @param type block type
372 * @param block block to get the key for
373 * @param block_size number of bytes in @a block
374 * @param key set to the key (query) for the given block
375 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
376 * (or if extracting a key from a block of this type does not work)
377 */
378int
379GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx, 288GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
380 enum GNUNET_BLOCK_Type type, 289 enum GNUNET_BLOCK_Type type,
381 const void *block, 290 const void *block,
@@ -385,8 +294,8 @@ GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
385 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, 294 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
386 type); 295 type);
387 296
388 if (plugin == NULL) 297 if (NULL == plugin)
389 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; 298 return GNUNET_SYSERR;
390 return plugin->get_key (plugin->cls, 299 return plugin->get_key (plugin->cls,
391 type, 300 type,
392 block, 301 block,
@@ -395,18 +304,73 @@ GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
395} 304}
396 305
397 306
398/** 307enum GNUNET_GenericReturnValue
399 * Update block group to filter out the given results. Note that the 308GNUNET_BLOCK_check_query (struct GNUNET_BLOCK_Context *ctx,
400 * use of a hash for seen results implies that the caller magically 309 enum GNUNET_BLOCK_Type type,
401 * knows how the specific block engine hashes for filtering 310 const struct GNUNET_HashCode *query,
402 * duplicates, so this API may not always apply. 311 const void *xquery,
403 * 312 size_t xquery_size)
404 * @param bf_mutator mutation value to use 313{
405 * @param seen_results results already seen 314 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
406 * @param seen_results_count number of entries in @a seen_results 315 type);
407 * @return #GNUNET_SYSERR if not supported, #GNUNET_OK on success 316
408 */ 317 if (NULL == plugin)
409int 318 return GNUNET_SYSERR;
319 return plugin->check_query (plugin->cls,
320 type,
321 query,
322 xquery,
323 xquery_size);
324}
325
326
327enum GNUNET_GenericReturnValue
328GNUNET_BLOCK_check_block (struct GNUNET_BLOCK_Context *ctx,
329 enum GNUNET_BLOCK_Type type,
330 const struct GNUNET_HashCode *query,
331 const void *block,
332 size_t block_size)
333{
334 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
335 type);
336
337 if (NULL == plugin)
338 return GNUNET_SYSERR;
339 return plugin->check_block (plugin->cls,
340 type,
341 query,
342 block,
343 block_size);
344}
345
346
347enum GNUNET_BLOCK_ReplyEvaluationResult
348GNUNET_BLOCK_check_reply (struct GNUNET_BLOCK_Context *ctx,
349 enum GNUNET_BLOCK_Type type,
350 struct GNUNET_BLOCK_Group *group,
351 const struct GNUNET_HashCode *query,
352 const void *xquery,
353 size_t xquery_size,
354 const void *reply_block,
355 size_t reply_block_size)
356{
357 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
358 type);
359
360 if (NULL == plugin)
361 return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED;
362 return plugin->check_reply (plugin->cls,
363 type,
364 group,
365 query,
366 xquery,
367 xquery_size,
368 reply_block,
369 reply_block_size);
370}
371
372
373enum GNUNET_GenericReturnValue
410GNUNET_BLOCK_group_set_seen (struct GNUNET_BLOCK_Group *bg, 374GNUNET_BLOCK_group_set_seen (struct GNUNET_BLOCK_Group *bg,
411 const struct GNUNET_HashCode *seen_results, 375 const struct GNUNET_HashCode *seen_results,
412 unsigned int seen_results_count) 376 unsigned int seen_results_count)