GNUnet  0.10.x
block.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010, 2017 GNUnet e.V.
4 
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
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your 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  Affero General Public License for more details.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_block_lib.h"
31 #include "gnunet_block_plugin.h"
32 
33 
37 struct Plugin {
41  char *library_name;
42 
47 };
48 
49 
57  struct Plugin **plugins;
58 
62  unsigned int num_plugins;
63 
68 };
69 
70 
78 void
80  uint32_t mingle_number,
81  struct GNUNET_HashCode *hc)
82 {
83  struct GNUNET_HashCode m;
84 
85  GNUNET_CRYPTO_hash(&mingle_number,
86  sizeof(uint32_t),
87  &m);
89  in,
90  hc);
91 }
92 
93 
101 static void
102 add_plugin(void *cls,
103  const char *library_name,
104  void *lib_ret)
105 {
106  struct GNUNET_BLOCK_Context *ctx = cls;
107  struct GNUNET_BLOCK_PluginFunctions *api = lib_ret;
108  struct Plugin *plugin;
109 
111  "Loading block plugin `%s'\n",
112  library_name);
113  plugin = GNUNET_new(struct Plugin);
114  plugin->api = api;
115  plugin->library_name = GNUNET_strdup(library_name);
117  ctx->num_plugins,
118  plugin);
119 }
120 
121 
122 
129 struct GNUNET_BLOCK_Context *
131 {
132  struct GNUNET_BLOCK_Context *ctx;
133 
134  ctx = GNUNET_new(struct GNUNET_BLOCK_Context);
135  ctx->cfg = cfg;
136  GNUNET_PLUGIN_load_all("libgnunet_plugin_block_",
137  (void *)cfg,
138  &add_plugin,
139  ctx);
140  return ctx;
141 }
142 
143 
149 void
151 {
152  struct Plugin *plugin;
153 
154  for (unsigned int i = 0; i < ctx->num_plugins; i++)
155  {
156  plugin = ctx->plugins[i];
157  GNUNET_break(NULL ==
159  plugin->api));
160  GNUNET_free(plugin->library_name);
161  GNUNET_free(plugin);
162  }
163  GNUNET_free(ctx->plugins);
164  GNUNET_free(ctx);
165 }
166 
167 
178 int
180  uint32_t *nonce,
181  void **raw_data,
182  size_t *raw_data_size)
183 {
184  *nonce = 0;
185  *raw_data = NULL;
186  *raw_data_size = 0;
187  if (NULL == bg)
188  return GNUNET_NO;
189  if (NULL == bg->serialize_cb)
190  return GNUNET_NO;
191  return bg->serialize_cb(bg,
192  nonce,
193  raw_data,
194  raw_data_size);
195 }
196 
197 
203 void
205 {
206  if (NULL == bg)
207  return;
208  bg->destroy_cb(bg);
209 }
210 
211 
224 int
226  struct GNUNET_BLOCK_Group *bg2)
227 {
228  int ret;
229 
230  if (NULL == bg2)
231  return GNUNET_OK;
232  if (NULL == bg1)
233  {
234  bg2->destroy_cb(bg2);
235  return GNUNET_OK;
236  }
237  if (NULL == bg1->merge_cb)
238  return GNUNET_SYSERR;
239  GNUNET_assert(bg1->merge_cb == bg1->merge_cb);
240  ret = bg1->merge_cb(bg1,
241  bg2);
242  bg2->destroy_cb(bg2);
243  return ret;
244 }
245 
246 
254 static struct GNUNET_BLOCK_PluginFunctions *
256  enum GNUNET_BLOCK_Type type)
257 {
258  struct Plugin *plugin;
259  unsigned int j;
260 
261  for (unsigned i = 0; i < ctx->num_plugins; i++)
262  {
263  plugin = ctx->plugins[i];
264  j = 0;
265  while (0 != (plugin->api->types[j]))
266  {
267  if (type == plugin->api->types[j])
268  return plugin->api;
269  j++;
270  }
271  }
272  return NULL;
273 }
274 
275 
287 struct GNUNET_BLOCK_Group *
289  enum GNUNET_BLOCK_Type type,
290  uint32_t nonce,
291  const void *raw_data,
292  size_t raw_data_size,
293  ...)
294 {
296  struct GNUNET_BLOCK_Group *bg;
297  va_list ap;
298 
299  plugin = find_plugin(ctx,
300  type);
301  if (NULL == plugin)
302  return NULL;
303  if (NULL == plugin->create_group)
304  return NULL;
305  va_start(ap,
306  raw_data_size);
307  bg = plugin->create_group(plugin->cls,
308  type,
309  nonce,
310  raw_data,
311  raw_data_size,
312  ap);
313  va_end(ap);
314  return bg;
315 }
316 
317 
338  enum GNUNET_BLOCK_Type type,
339  struct GNUNET_BLOCK_Group *group,
341  const struct GNUNET_HashCode *query,
342  const void *xquery,
343  size_t xquery_size,
344  const void *reply_block,
345  size_t reply_block_size)
346 {
348  type);
349 
350  if (NULL == plugin)
352  return plugin->evaluate(plugin->cls,
353  ctx,
354  type,
355  group,
356  eo,
357  query,
358  xquery,
359  xquery_size,
360  reply_block,
361  reply_block_size);
362 }
363 
364 
376 int
378  enum GNUNET_BLOCK_Type type,
379  const void *block,
380  size_t block_size,
381  struct GNUNET_HashCode *key)
382 {
384  type);
385 
386  if (plugin == NULL)
388  return plugin->get_key(plugin->cls,
389  type,
390  block,
391  block_size,
392  key);
393 }
394 
395 
407 int
409  const struct GNUNET_HashCode *seen_results,
410  unsigned int seen_results_count)
411 {
412  if (NULL == bg)
413  return GNUNET_OK;
414  if (NULL == bg->mark_seen_cb)
415  return GNUNET_SYSERR;
416  bg->mark_seen_cb(bg,
417  seen_results,
418  seen_results_count);
419  return GNUNET_OK;
420 }
421 
422 
423 /* end of block.c */
struct GNUNET_BLOCK_Context * GNUNET_BLOCK_context_create(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a block context.
Definition: block.c:130
Handle to an initialized block library.
Definition: block.c:53
enum GNUNET_BLOCK_Type * types
0-terminated array of block types supported by this plugin.
GNUNET_BLOCK_GroupSerializeFunction serialize_cb
Serialize the block group data, can be NULL if not supported.
GNUNET_BLOCK_EvaluationFunction evaluate
Main function of a block plugin.
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
char * key
TLS key.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: block.c:67
char * library_name
Name of the shared library.
Definition: block.c:41
static void add_plugin(void *cls, const char *library_name, void *lib_ret)
Add a plugin to the list managed by the block library.
Definition: block.c:102
static int ret
Final status code.
Definition: gnunet-arm.c:89
GNUNET_BLOCK_GroupDestroyFunction destroy_cb
Function to call to destroy the block group.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:46
int GNUNET_BLOCK_get_key(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, const void *block, size_t block_size, struct GNUNET_HashCode *key)
Function called to obtain the key for a block.
Definition: block.c:377
int GNUNET_BLOCK_group_merge(struct GNUNET_BLOCK_Group *bg1, struct GNUNET_BLOCK_Group *bg2)
Try merging two block groups.
Definition: block.c:225
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:255
static struct GNUNET_BLOCK_PluginFunctions * find_plugin(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type)
Find a plugin for the given type.
Definition: block.c:255
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
enum GNUNET_BLOCK_EvaluationResult GNUNET_BLOCK_evaluate(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, struct GNUNET_BLOCK_Group *group, enum GNUNET_BLOCK_EvaluationOptions eo, const struct GNUNET_HashCode *query, const void *xquery, size_t xquery_size, const void *reply_block, size_t reply_block_size)
Function called to validate a reply or a request.
Definition: block.c:337
int GNUNET_BLOCK_group_serialize(struct GNUNET_BLOCK_Group *bg, uint32_t *nonce, void **raw_data, size_t *raw_data_size)
Serialize state of a block group.
Definition: block.c:179
A 512-bit hashcode.
void GNUNET_BLOCK_context_destroy(struct GNUNET_BLOCK_Context *ctx)
Destroy the block context.
Definition: block.c:150
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
GNUNET_BLOCK_GroupCreateFunction create_group
Create a block group to process a bunch of blocks in a shared context (i.e.
void GNUNET_PLUGIN_load_all(const char *basename, void *arg, GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
Load all compatible plugins with the given base name.
Definition: plugin.c:370
GNUNET_BLOCK_EvaluationResult
Possible ways for how a block may relate to a query.
struct GNUNET_BLOCK_Group * GNUNET_BLOCK_group_create(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, uint32_t nonce, const void *raw_data, size_t raw_data_size,...)
Create a new block group.
Definition: block.c:288
Specified block type not supported by this plugin.
GNUNET_BLOCK_GetKeyFunction get_key
Obtain the key for a given block (if possible).
unsigned int num_plugins
Size of the &#39;plugins&#39; array.
Definition: block.c:62
#define GNUNET_array_append(arr, size, element)
Append an element to a list (growing the list by one).
GNUNET_BLOCK_GroupMergeFunction merge_cb
Function to call to merge two groups.
configuration data
Definition: configuration.c:83
Handle for a plugin.
Definition: block.c:37
#define GNUNET_log(kind,...)
Block group data.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
void GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
compute result = a ^ b
Definition: crypto_hash.c:193
void * cls
Closure for all of the callbacks.
GNUNET_BLOCK_EvaluationOptions
Flags that can be set to control the evaluation.
struct Plugin ** plugins
Array of our plugins.
Definition: block.c:57
void GNUNET_BLOCK_mingle_hash(const struct GNUNET_HashCode *in, uint32_t mingle_number, struct GNUNET_HashCode *hc)
Mingle hash with the mingle_number to produce different bits.
Definition: block.c:79
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_BLOCK_GroupMarkSeenFunction mark_seen_cb
Function to call to mark elements as seen in the group.
void GNUNET_BLOCK_group_destroy(struct GNUNET_BLOCK_Group *bg)
Destroy resources used by a block group.
Definition: block.c:204
int GNUNET_BLOCK_group_set_seen(struct GNUNET_BLOCK_Group *bg, const struct GNUNET_HashCode *seen_results, unsigned int seen_results_count)
Update block group to filter out the given results.
Definition: block.c:408