GNUnet  0.16.x
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 void
96  uint32_t mingle_number,
97  struct GNUNET_HashCode *hc)
98 {
99  struct MinglePacker mp = {
100  .in = *in,
101  .mingle = mingle_number
102  };
103 
104  GNUNET_CRYPTO_hash (&mp,
105  sizeof(mp),
106  hc);
107 }
108 
109 
117 static void
118 add_plugin (void *cls,
119  const char *library_name,
120  void *lib_ret)
121 {
122  struct GNUNET_BLOCK_Context *ctx = cls;
123  struct GNUNET_BLOCK_PluginFunctions *api = lib_ret;
124  struct Plugin *plugin;
125 
127  "Loading block plugin `%s'\n",
128  library_name);
129  plugin = GNUNET_new (struct Plugin);
130  plugin->api = api;
132  GNUNET_array_append (ctx->plugins,
133  ctx->num_plugins,
134  plugin);
135 }
136 
137 
138 struct GNUNET_BLOCK_Context *
140 {
141  struct GNUNET_BLOCK_Context *ctx;
142 
144  ctx->cfg = cfg;
146  "libgnunet_plugin_block_",
147  (void *) cfg,
148  &add_plugin,
149  ctx);
150  return ctx;
151 }
152 
153 
154 void
156 {
157  struct Plugin *plugin;
158 
159  for (unsigned int i = 0; i < ctx->num_plugins; i++)
160  {
161  plugin = ctx->plugins[i];
162  GNUNET_break (NULL ==
164  plugin->api));
167  }
168  GNUNET_free (ctx->plugins);
169  GNUNET_free (ctx);
170 }
171 
172 
175  uint32_t *nonce,
176  void **raw_data,
177  size_t *raw_data_size)
178 {
179  *nonce = 0;
180  *raw_data = NULL;
181  *raw_data_size = 0;
182  if (NULL == bg)
183  return GNUNET_NO;
184  if (NULL == bg->serialize_cb)
185  return GNUNET_NO;
186  return bg->serialize_cb (bg,
187  nonce,
188  raw_data,
189  raw_data_size);
190 }
191 
192 
193 void
195 {
196  if (NULL == bg)
197  return;
198  bg->destroy_cb (bg);
199 }
200 
201 
204  struct GNUNET_BLOCK_Group *bg2)
205 {
207 
208  if (NULL == bg2)
209  return GNUNET_OK;
210  if (NULL == bg1)
211  {
212  bg2->destroy_cb (bg2);
213  return GNUNET_OK;
214  }
215  if (NULL == bg1->merge_cb)
216  return GNUNET_SYSERR;
217  GNUNET_assert (bg1->merge_cb == bg1->merge_cb);
218  ret = bg1->merge_cb (bg1,
219  bg2);
220  bg2->destroy_cb (bg2);
221  return ret;
222 }
223 
224 
232 static struct GNUNET_BLOCK_PluginFunctions *
234  enum GNUNET_BLOCK_Type type)
235 {
236  for (unsigned i = 0; i < ctx->num_plugins; i++)
237  {
238  struct Plugin *plugin = ctx->plugins[i];
239 
240  for (unsigned int j = 0; 0 != plugin->api->types[j]; j++)
241  if (type == plugin->api->types[j])
242  return plugin->api;
243  }
244  return NULL;
245 }
246 
247 
248 struct GNUNET_BLOCK_Group *
250  enum GNUNET_BLOCK_Type type,
251  uint32_t nonce,
252  const void *raw_data,
253  size_t raw_data_size,
254  ...)
255 {
257  struct GNUNET_BLOCK_Group *bg;
258  va_list ap;
259 
261  type);
262  if (NULL == plugin)
263  return NULL;
264  if (NULL == plugin->create_group)
265  return NULL;
266  va_start (ap,
267  raw_data_size);
268  bg = plugin->create_group (plugin->cls,
269  type,
270  nonce,
271  raw_data,
272  raw_data_size,
273  ap);
274  va_end (ap);
275  return bg;
276 }
277 
278 
281  enum GNUNET_BLOCK_Type type,
282  const void *block,
283  size_t block_size,
284  struct GNUNET_HashCode *key)
285 {
287  type);
288 
289  if (NULL == plugin)
290  return GNUNET_SYSERR;
291  return plugin->get_key (plugin->cls,
292  type,
293  block,
294  block_size,
295  key);
296 }
297 
298 
301  enum GNUNET_BLOCK_Type type,
302  const struct GNUNET_HashCode *query,
303  const void *xquery,
304  size_t xquery_size)
305 {
307  type);
308 
309  if (NULL == plugin)
310  return GNUNET_SYSERR;
311  return plugin->check_query (plugin->cls,
312  type,
313  query,
314  xquery,
315  xquery_size);
316 }
317 
318 
321  enum GNUNET_BLOCK_Type type,
322  const void *block,
323  size_t block_size)
324 {
326  type);
327 
328  if (NULL == plugin)
329  return GNUNET_SYSERR;
330  return plugin->check_block (plugin->cls,
331  type,
332  block,
333  block_size);
334 }
335 
336 
339  enum GNUNET_BLOCK_Type type,
340  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->check_reply (plugin->cls,
353  type,
354  group,
355  query,
356  xquery,
357  xquery_size,
358  reply_block,
359  reply_block_size);
360 }
361 
362 
365  const struct GNUNET_HashCode *seen_results,
366  unsigned int seen_results_count)
367 {
368  if (NULL == bg)
369  return GNUNET_OK;
370  if (NULL == bg->mark_seen_cb)
371  return GNUNET_SYSERR;
372  bg->mark_seen_cb (bg,
373  seen_results,
374  seen_results_count);
375  return GNUNET_OK;
376 }
377 
378 
379 /* 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:118
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:233
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 Plugin * 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.
#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.
Definition: gnunet_common.h:92
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_NO
Definition: gnunet_common.h:94
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
#define GNUNET_PACKED
gcc-ism to get packed structs.
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
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:364
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:338
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:320
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:300
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:155
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:203
void GNUNET_BLOCK_group_destroy(struct GNUNET_BLOCK_Group *bg)
Destroy resources used by a block group.
Definition: block.c:194
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:249
struct GNUNET_BLOCK_Context * GNUNET_BLOCK_context_create(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a block context.
Definition: block.c:139
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:95
enum GNUNET_GenericReturnValue 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:174
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:280
@ 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_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:269
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:443
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...
enum GNUNET_BLOCK_Type * types
0-terminated array of block types supported by this plugin.
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
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model