GNUnet  0.11.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
38 {
42  char *library_name;
43 
48 };
49 
50 
55 {
59  struct Plugin **plugins;
60 
64  unsigned int num_plugins;
65 
70 };
71 
72 
80 void
82  uint32_t mingle_number,
83  struct GNUNET_HashCode *hc)
84 {
85  struct GNUNET_HashCode m;
86 
87  GNUNET_CRYPTO_hash (&mingle_number,
88  sizeof(uint32_t),
89  &m);
91  in,
92  hc);
93 }
94 
95 
103 static void
104 add_plugin (void *cls,
105  const char *library_name,
106  void *lib_ret)
107 {
108  struct GNUNET_BLOCK_Context *ctx = cls;
109  struct GNUNET_BLOCK_PluginFunctions *api = lib_ret;
110  struct Plugin *plugin;
111 
113  "Loading block plugin `%s'\n",
114  library_name);
115  plugin = GNUNET_new (struct Plugin);
116  plugin->api = api;
117  plugin->library_name = GNUNET_strdup (library_name);
119  ctx->num_plugins,
120  plugin);
121 }
122 
123 
130 struct GNUNET_BLOCK_Context *
132 {
133  struct GNUNET_BLOCK_Context *ctx;
134 
135  ctx = GNUNET_new (struct GNUNET_BLOCK_Context);
136  ctx->cfg = cfg;
137  GNUNET_PLUGIN_load_all ("libgnunet_plugin_block_",
138  (void *) cfg,
139  &add_plugin,
140  ctx);
141  return ctx;
142 }
143 
144 
150 void
152 {
153  struct Plugin *plugin;
154 
155  for (unsigned int i = 0; i < ctx->num_plugins; i++)
156  {
157  plugin = ctx->plugins[i];
158  GNUNET_break (NULL ==
160  plugin->api));
161  GNUNET_free (plugin->library_name);
162  GNUNET_free (plugin);
163  }
164  GNUNET_free (ctx->plugins);
165  GNUNET_free (ctx);
166 }
167 
168 
179 int
181  uint32_t *nonce,
182  void **raw_data,
183  size_t *raw_data_size)
184 {
185  *nonce = 0;
186  *raw_data = NULL;
187  *raw_data_size = 0;
188  if (NULL == bg)
189  return GNUNET_NO;
190  if (NULL == bg->serialize_cb)
191  return GNUNET_NO;
192  return bg->serialize_cb (bg,
193  nonce,
194  raw_data,
195  raw_data_size);
196 }
197 
198 
204 void
206 {
207  if (NULL == bg)
208  return;
209  bg->destroy_cb (bg);
210 }
211 
212 
225 int
227  struct GNUNET_BLOCK_Group *bg2)
228 {
229  int ret;
230 
231  if (NULL == bg2)
232  return GNUNET_OK;
233  if (NULL == bg1)
234  {
235  bg2->destroy_cb (bg2);
236  return GNUNET_OK;
237  }
238  if (NULL == bg1->merge_cb)
239  return GNUNET_SYSERR;
240  GNUNET_assert (bg1->merge_cb == bg1->merge_cb);
241  ret = bg1->merge_cb (bg1,
242  bg2);
243  bg2->destroy_cb (bg2);
244  return ret;
245 }
246 
247 
255 static struct GNUNET_BLOCK_PluginFunctions *
257  enum GNUNET_BLOCK_Type type)
258 {
259  struct Plugin *plugin;
260  unsigned int j;
261 
262  for (unsigned i = 0; i < ctx->num_plugins; i++)
263  {
264  plugin = ctx->plugins[i];
265  j = 0;
266  while (0 != (plugin->api->types[j]))
267  {
268  if (type == plugin->api->types[j])
269  return plugin->api;
270  j++;
271  }
272  }
273  return NULL;
274 }
275 
276 
288 struct GNUNET_BLOCK_Group *
290  enum GNUNET_BLOCK_Type type,
291  uint32_t nonce,
292  const void *raw_data,
293  size_t raw_data_size,
294  ...)
295 {
297  struct GNUNET_BLOCK_Group *bg;
298  va_list ap;
299 
300  plugin = find_plugin (ctx,
301  type);
302  if (NULL == plugin)
303  return NULL;
304  if (NULL == plugin->create_group)
305  return NULL;
306  va_start (ap,
307  raw_data_size);
308  bg = plugin->create_group (plugin->cls,
309  type,
310  nonce,
311  raw_data,
312  raw_data_size,
313  ap);
314  va_end (ap);
315  return bg;
316 }
317 
318 
339  enum GNUNET_BLOCK_Type type,
340  struct GNUNET_BLOCK_Group *group,
342  const struct GNUNET_HashCode *query,
343  const void *xquery,
344  size_t xquery_size,
345  const void *reply_block,
346  size_t reply_block_size)
347 {
349  type);
350 
351  if (NULL == plugin)
353  return plugin->evaluate (plugin->cls,
354  ctx,
355  type,
356  group,
357  eo,
358  query,
359  xquery,
360  xquery_size,
361  reply_block,
362  reply_block_size);
363 }
364 
365 
377 int
379  enum GNUNET_BLOCK_Type type,
380  const void *block,
381  size_t block_size,
382  struct GNUNET_HashCode *key)
383 {
385  type);
386 
387  if (plugin == NULL)
389  return plugin->get_key (plugin->cls,
390  type,
391  block,
392  block_size,
393  key);
394 }
395 
396 
408 int
410  const struct GNUNET_HashCode *seen_results,
411  unsigned int seen_results_count)
412 {
413  if (NULL == bg)
414  return GNUNET_OK;
415  if (NULL == bg->mark_seen_cb)
416  return GNUNET_SYSERR;
417  bg->mark_seen_cb (bg,
418  seen_results,
419  seen_results_count);
420  return GNUNET_OK;
421 }
422 
423 
424 /* 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:131
Handle to an initialized block library.
Definition: block.c:54
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.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#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:69
char * library_name
Name of the shared library.
Definition: block.c:42
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:104
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:47
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:378
int GNUNET_BLOCK_group_merge(struct GNUNET_BLOCK_Group *bg1, struct GNUNET_BLOCK_Group *bg2)
Try merging two block groups.
Definition: block.c:226
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:256
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:256
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
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:338
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:180
A 512-bit hashcode.
static char * plugin
Solver plugin name as string.
void GNUNET_BLOCK_context_destroy(struct GNUNET_BLOCK_Context *ctx)
Destroy the block context.
Definition: block.c:151
#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:372
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:289
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:64
#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:85
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:202
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:59
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:81
#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:205
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:409