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
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 
124 
131 struct GNUNET_BLOCK_Context *
133 {
134  struct GNUNET_BLOCK_Context *ctx;
135 
136  ctx = GNUNET_new (struct GNUNET_BLOCK_Context);
137  ctx->cfg = cfg;
138  GNUNET_PLUGIN_load_all ("libgnunet_plugin_block_",
139  (void *) cfg,
140  &add_plugin,
141  ctx);
142  return ctx;
143 }
144 
145 
151 void
153 {
154  struct Plugin *plugin;
155 
156  for (unsigned int i = 0; i < ctx->num_plugins; i++)
157  {
158  plugin = ctx->plugins[i];
159  GNUNET_break (NULL ==
161  plugin->api));
162  GNUNET_free (plugin->library_name);
163  GNUNET_free (plugin);
164  }
165  GNUNET_free (ctx->plugins);
166  GNUNET_free (ctx);
167 }
168 
169 
180 int
182  uint32_t *nonce,
183  void **raw_data,
184  size_t *raw_data_size)
185 {
186  *nonce = 0;
187  *raw_data = NULL;
188  *raw_data_size = 0;
189  if (NULL == bg)
190  return GNUNET_NO;
191  if (NULL == bg->serialize_cb)
192  return GNUNET_NO;
193  return bg->serialize_cb (bg,
194  nonce,
195  raw_data,
196  raw_data_size);
197 }
198 
199 
205 void
207 {
208  if (NULL == bg)
209  return;
210  bg->destroy_cb (bg);
211 }
212 
213 
226 int
228  struct GNUNET_BLOCK_Group *bg2)
229 {
230  int ret;
231 
232  if (NULL == bg2)
233  return GNUNET_OK;
234  if (NULL == bg1)
235  {
236  bg2->destroy_cb (bg2);
237  return GNUNET_OK;
238  }
239  if (NULL == bg1->merge_cb)
240  return GNUNET_SYSERR;
241  GNUNET_assert (bg1->merge_cb == bg1->merge_cb);
242  ret = bg1->merge_cb (bg1,
243  bg2);
244  bg2->destroy_cb (bg2);
245  return ret;
246 }
247 
248 
256 static struct GNUNET_BLOCK_PluginFunctions *
258  enum GNUNET_BLOCK_Type type)
259 {
260  struct Plugin *plugin;
261  unsigned int j;
262 
263  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  {
269  if (type == plugin->api->types[j])
270  return plugin->api;
271  j++;
272  }
273  }
274  return NULL;
275 }
276 
277 
289 struct GNUNET_BLOCK_Group *
291  enum GNUNET_BLOCK_Type type,
292  uint32_t nonce,
293  const void *raw_data,
294  size_t raw_data_size,
295  ...)
296 {
298  struct GNUNET_BLOCK_Group *bg;
299  va_list ap;
300 
301  plugin = find_plugin (ctx,
302  type);
303  if (NULL == plugin)
304  return NULL;
305  if (NULL == plugin->create_group)
306  return NULL;
307  va_start (ap,
308  raw_data_size);
309  bg = plugin->create_group (plugin->cls,
310  type,
311  nonce,
312  raw_data,
313  raw_data_size,
314  ap);
315  va_end (ap);
316  return bg;
317 }
318 
319 
340  enum GNUNET_BLOCK_Type type,
341  struct GNUNET_BLOCK_Group *group,
343  const struct GNUNET_HashCode *query,
344  const void *xquery,
345  size_t xquery_size,
346  const void *reply_block,
347  size_t reply_block_size)
348 {
350  type);
351 
352  if (NULL == plugin)
354  return plugin->evaluate (plugin->cls,
355  ctx,
356  type,
357  group,
358  eo,
359  query,
360  xquery,
361  xquery_size,
362  reply_block,
363  reply_block_size);
364 }
365 
366 
378 int
380  enum GNUNET_BLOCK_Type type,
381  const void *block,
382  size_t block_size,
383  struct GNUNET_HashCode *key)
384 {
386  type);
387 
388  if (plugin == NULL)
390  return plugin->get_key (plugin->cls,
391  type,
392  block,
393  block_size,
394  key);
395 }
396 
397 
409 int
411  const struct GNUNET_HashCode *seen_results,
412  unsigned int seen_results_count)
413 {
414  if (NULL == bg)
415  return GNUNET_OK;
416  if (NULL == bg->mark_seen_cb)
417  return GNUNET_SYSERR;
418  bg->mark_seen_cb (bg,
419  seen_results,
420  seen_results_count);
421  return GNUNET_OK;
422 }
423 
424 
425 /* 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:132
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.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#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
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: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:379
int GNUNET_BLOCK_group_merge(struct GNUNET_BLOCK_Group *bg1, struct GNUNET_BLOCK_Group *bg2)
Try merging two block groups.
Definition: block.c:227
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:258
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:257
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:339
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:181
A 512-bit hashcode.
void GNUNET_BLOCK_context_destroy(struct GNUNET_BLOCK_Context *ctx)
Destroy the block context.
Definition: block.c:152
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
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:375
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:290
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: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: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:206
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:410