GNUnet  0.19.5
block.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010, 2017, 2021, 2022 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 
74 
75 
80 {
84  struct GNUNET_HashCode in;
85 
90 };
91 
93 
94 
95 void
97  uint32_t mingle_number,
98  struct GNUNET_HashCode *hc)
99 {
100  struct MinglePacker mp = {
101  .in = *in,
102  .mingle = mingle_number
103  };
104 
105  GNUNET_CRYPTO_hash (&mp,
106  sizeof(mp),
107  hc);
108 }
109 
110 
118 static void
119 add_plugin (void *cls,
120  const char *library_name,
121  void *lib_ret)
122 {
123  struct GNUNET_BLOCK_Context *ctx = cls;
124  struct GNUNET_BLOCK_PluginFunctions *api = lib_ret;
125  struct Plugin *plugin;
126 
128  "Loading block plugin `%s'\n",
129  library_name);
130  plugin = GNUNET_new (struct Plugin);
131  plugin->api = api;
133  GNUNET_array_append (ctx->plugins,
134  ctx->num_plugins,
135  plugin);
136 }
137 
138 
139 struct GNUNET_BLOCK_Context *
141 {
142  struct GNUNET_BLOCK_Context *ctx;
143 
145  ctx->cfg = cfg;
147  "libgnunet_plugin_block_",
148  (void *) cfg,
149  &add_plugin,
150  ctx);
151  return ctx;
152 }
153 
154 
155 void
157 {
158  struct Plugin *plugin;
159 
160  for (unsigned int i = 0; i < ctx->num_plugins; i++)
161  {
162  plugin = ctx->plugins[i];
163  GNUNET_break (NULL ==
165  plugin->api));
168  }
169  GNUNET_free (ctx->plugins);
170  GNUNET_free (ctx);
171 }
172 
173 
176  void **raw_data,
177  size_t *raw_data_size)
178 {
179  *raw_data = NULL;
180  *raw_data_size = 0;
181  if (NULL == bg)
182  return GNUNET_NO;
183  if (NULL == bg->serialize_cb)
184  return GNUNET_NO;
185  return bg->serialize_cb (bg,
186  raw_data,
187  raw_data_size);
188 }
189 
190 
191 void
193 {
194  if (NULL == bg)
195  return;
196  bg->destroy_cb (bg);
197 }
198 
199 
202  struct GNUNET_BLOCK_Group *bg2)
203 {
205 
206  if (NULL == bg2)
207  return GNUNET_OK;
208  if (NULL == bg1)
209  {
210  bg2->destroy_cb (bg2);
211  return GNUNET_OK;
212  }
213  if (NULL == bg1->merge_cb)
214  return GNUNET_SYSERR;
215  GNUNET_assert (bg1->merge_cb == bg1->merge_cb);
216  ret = bg1->merge_cb (bg1,
217  bg2);
218  bg2->destroy_cb (bg2);
219  return ret;
220 }
221 
222 
230 static struct GNUNET_BLOCK_PluginFunctions *
232  enum GNUNET_BLOCK_Type type)
233 {
234  for (unsigned i = 0; i < ctx->num_plugins; i++)
235  {
236  struct Plugin *plugin = ctx->plugins[i];
237 
238  for (unsigned int j = 0; 0 != plugin->api->types[j]; j++)
239  if (type == plugin->api->types[j])
240  return plugin->api;
241  }
242  return NULL;
243 }
244 
245 
246 struct GNUNET_BLOCK_Group *
248  enum GNUNET_BLOCK_Type type,
249  const void *raw_data,
250  size_t raw_data_size,
251  ...)
252 {
254  struct GNUNET_BLOCK_Group *bg;
255  va_list ap;
256 
258  type);
259  if (NULL == plugin)
260  return NULL;
261  if (NULL == plugin->create_group)
262  return NULL;
263  va_start (ap,
264  raw_data_size);
265  bg = plugin->create_group (plugin->cls,
266  type,
267  raw_data,
268  raw_data_size,
269  ap);
270  va_end (ap);
271  return bg;
272 }
273 
274 
277  enum GNUNET_BLOCK_Type type,
278  const void *block,
279  size_t block_size,
280  struct GNUNET_HashCode *key)
281 {
283  type);
284 
285  if (NULL == plugin)
286  return GNUNET_SYSERR;
287  return plugin->get_key (plugin->cls,
288  type,
289  block,
290  block_size,
291  key);
292 }
293 
294 
297  enum GNUNET_BLOCK_Type type,
298  const struct GNUNET_HashCode *query,
299  const void *xquery,
300  size_t xquery_size)
301 {
303 
305  return GNUNET_SYSERR; /* no checks */
307  type);
308  if (NULL == plugin)
309  return GNUNET_SYSERR;
310  return plugin->check_query (plugin->cls,
311  type,
312  query,
313  xquery,
314  xquery_size);
315 }
316 
317 
320  enum GNUNET_BLOCK_Type type,
321  const void *block,
322  size_t block_size)
323 {
325  type);
326 
327  if (NULL == plugin)
328  return GNUNET_SYSERR;
329  return plugin->check_block (plugin->cls,
330  type,
331  block,
332  block_size);
333 }
334 
335 
338  enum GNUNET_BLOCK_Type type,
339  struct GNUNET_BLOCK_Group *group,
340  const struct GNUNET_HashCode *query,
341  const void *xquery,
342  size_t xquery_size,
343  const void *reply_block,
344  size_t reply_block_size)
345 {
347  type);
348 
349  if (NULL == plugin)
351  return plugin->check_reply (plugin->cls,
352  type,
353  group,
354  query,
355  xquery,
356  xquery_size,
357  reply_block,
358  reply_block_size);
359 }
360 
361 
364  const struct GNUNET_HashCode *seen_results,
365  unsigned int seen_results_count)
366 {
367  if (NULL == bg)
368  return GNUNET_OK;
369  if (NULL == bg->mark_seen_cb)
370  return GNUNET_SYSERR;
371  bg->mark_seen_cb (bg,
372  seen_results,
373  seen_results_count);
374  return GNUNET_OK;
375 }
376 
377 
378 /* end of block.c */
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:119
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:231
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
@ GNUNET_BLOCK_TYPE_ANY
Identifier for any block.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
struct TestcasePlugin * plugin
The process handle to the testbed service.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
Library for data block manipulation.
API for block plugins.
enum GNUNET_GenericReturnValue 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:363
enum GNUNET_BLOCK_ReplyEvaluationResult GNUNET_BLOCK_check_reply(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, struct GNUNET_BLOCK_Group *group, 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 if a reply is good for a particular query.
Definition: block.c:337
enum GNUNET_GenericReturnValue GNUNET_BLOCK_check_block(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, const void *block, size_t block_size)
Function called to validate a block.
Definition: block.c:319
enum GNUNET_GenericReturnValue GNUNET_BLOCK_check_query(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, const struct GNUNET_HashCode *query, const void *xquery, size_t xquery_size)
Function called to validate a request.
Definition: block.c:296
GNUNET_BLOCK_ReplyEvaluationResult
Possible ways for how a block may relate to a query.
void GNUNET_BLOCK_context_destroy(struct GNUNET_BLOCK_Context *ctx)
Destroy the block context.
Definition: block.c:156
enum GNUNET_GenericReturnValue GNUNET_BLOCK_group_merge(struct GNUNET_BLOCK_Group *bg1, struct GNUNET_BLOCK_Group *bg2)
Try merging two block groups.
Definition: block.c:201
void GNUNET_BLOCK_group_destroy(struct GNUNET_BLOCK_Group *bg)
Destroy resources used by a block group.
Definition: block.c:192
struct GNUNET_BLOCK_Group * GNUNET_BLOCK_group_create(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, const void *raw_data, size_t raw_data_size,...)
Create a new block group.
Definition: block.c:247
struct GNUNET_BLOCK_Context * GNUNET_BLOCK_context_create(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a block context.
Definition: block.c:140
GNUNET_NETWORK_STRUCT_END 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:96
enum GNUNET_GenericReturnValue GNUNET_BLOCK_group_serialize(struct GNUNET_BLOCK_Group *bg, void **raw_data, size_t *raw_data_size)
Serialize state of a block group.
Definition: block.c:175
enum GNUNET_GenericReturnValue 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:276
@ GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED
Specified block type not supported by any plugin.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32.
#define GNUNET_log(kind,...)
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;.
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_PACKED
gcc-ism to get packed structs.
@ GNUNET_OK
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
#define GNUNET_free(ptr)
Wrapper around free.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_default(void)
Return default project data used by 'libgnunetutil' for GNUnet.
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:242
void GNUNET_PLUGIN_load_all_in_context(const struct GNUNET_OS_ProjectData *ctx, const char *basename, void *arg, GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
Load all compatible plugins with the given base name while inside the given context (i....
Definition: plugin.c:386
Handle to an initialized block library.
Definition: block.c:55
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: block.c:69
struct Plugin ** plugins
Array of our plugins.
Definition: block.c:59
unsigned int num_plugins
Size of the 'plugins' array.
Definition: block.c:64
Block group data.
GNUNET_BLOCK_GroupMergeFunction merge_cb
Function to call to merge two groups.
GNUNET_BLOCK_GroupDestroyFunction destroy_cb
Function to call to destroy the block group.
GNUNET_BLOCK_GroupMarkSeenFunction mark_seen_cb
Function to call to mark elements as seen in the group.
GNUNET_BLOCK_GroupSerializeFunction serialize_cb
Serialize the block group data, can be NULL if not supported.
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
A 512-bit hashcode.
Serialization to use in GNUNET_BLOCK_mingle_hash.
Definition: block.c:80
struct GNUNET_HashCode in
Original hash.
Definition: block.c:84
uint32_t mingle
Mingle value.
Definition: block.c:89
Handle for a plugin.
Definition: block.c:38
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
char * library_name
Name of the shared library.
Definition: block.c:42
char * library_name
Name of the shared library.
Definition: testing.h:98
struct GNUNET_TESTING_PluginFunctions * api
Plugin API.
Definition: testing.h:103
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model