GNUnet 0.22.2
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
37struct Plugin
38{
43
48};
49
50
55{
59 struct Plugin **plugins;
60
64 unsigned int num_plugins;
65
70};
71
72
74
75
80{
85
90};
91
93
94
95void
97 uint32_t mingle_number,
98 struct GNUNET_HashCode *hc)
99{
100 struct MinglePacker mp = {
101 .in = *in,
102 .mingle = mingle_number
103 };
104
106 sizeof(mp),
107 hc);
108}
109
110
118static void
119add_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",
130 plugin = GNUNET_new (struct Plugin);
131 plugin->api = api;
132 plugin->library_name = GNUNET_strdup (library_name);
133 GNUNET_array_append (ctx->plugins,
134 ctx->num_plugins,
135 plugin);
136}
137
138
141{
143 const struct GNUNET_OS_ProjectData *pd;
144
146 ctx->cfg = cfg;
149 "libgnunet_plugin_block_",
150 (void *) cfg,
151 &add_plugin,
152 ctx);
153 return ctx;
154}
155
156
157void
159{
160 struct Plugin *plugin;
161
162 for (unsigned int i = 0; i < ctx->num_plugins; i++)
163 {
164 plugin = ctx->plugins[i];
165 GNUNET_break (NULL ==
166 GNUNET_PLUGIN_unload (plugin->library_name,
167 plugin->api));
168 GNUNET_free (plugin->library_name);
170 }
171 GNUNET_free (ctx->plugins);
173}
174
175
178 void **raw_data,
179 size_t *raw_data_size)
180{
181 *raw_data = NULL;
182 *raw_data_size = 0;
183 if (NULL == bg)
184 return GNUNET_NO;
185 if (NULL == bg->serialize_cb)
186 return GNUNET_NO;
187 return bg->serialize_cb (bg,
188 raw_data,
189 raw_data_size);
190}
191
192
193void
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
232static struct GNUNET_BLOCK_PluginFunctions *
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
248struct GNUNET_BLOCK_Group *
251 const void *raw_data,
252 size_t raw_data_size,
253 ...)
254{
256 struct GNUNET_BLOCK_Group *bg;
257 va_list ap;
258
260 type);
261 if (NULL == plugin)
262 return NULL;
263 if (NULL == plugin->create_group)
264 return NULL;
265 va_start (ap,
266 raw_data_size);
267 bg = plugin->create_group (plugin->cls,
268 type,
269 raw_data,
270 raw_data_size,
271 ap);
272 va_end (ap);
273 return bg;
274}
275
276
280 const void *block,
281 size_t block_size,
282 struct GNUNET_HashCode *key)
283{
285 type);
286
287 if (NULL == plugin)
288 return GNUNET_SYSERR;
289 return plugin->get_key (plugin->cls,
290 type,
291 block,
292 block_size,
293 key);
294}
295
296
300 const struct GNUNET_HashCode *query,
301 const void *xquery,
302 size_t xquery_size)
303{
305
307 return GNUNET_SYSERR; /* no checks */
309 type);
310 if (NULL == plugin)
311 return GNUNET_SYSERR;
312 return plugin->check_query (plugin->cls,
313 type,
314 query,
315 xquery,
316 xquery_size);
317}
318
319
323 const void *block,
324 size_t block_size)
325{
327 type);
328
329 if (NULL == plugin)
330 return GNUNET_SYSERR;
331 return plugin->check_block (plugin->cls,
332 type,
333 block,
334 block_size);
335}
336
337
341 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->check_reply (plugin->cls,
354 type,
355 group,
356 query,
357 xquery,
358 xquery_size,
359 reply_block,
360 reply_block_size);
361}
362
363
366 const struct GNUNET_HashCode *seen_results,
367 unsigned int seen_results_count)
368{
369 if (NULL == bg)
370 return GNUNET_OK;
371 if (NULL == bg->mark_seen_cb)
372 return GNUNET_SYSERR;
373 bg->mark_seen_cb (bg,
374 seen_results,
375 seen_results_count);
376 return GNUNET_OK;
377}
378
379
380/* 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:233
static int ret
Final status code.
Definition: gnunet-arm.c:93
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
static struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_FS_Handle * ctx
static uint32_t type
Type string converted to DNS type value.
Library for data block manipulation.
API for block plugins.
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.
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:365
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:339
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:321
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:298
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:158
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
struct GNUNET_BLOCK_Context * GNUNET_BLOCK_context_create(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a block context.
Definition: block.c:140
void GNUNET_BLOCK_group_destroy(struct GNUNET_BLOCK_Group *bg)
Destroy resources used by a block group.
Definition: block.c:194
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:177
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:249
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:278
@ GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED
Specified block type not supported by any plugin.
const struct GNUNET_OS_ProjectData * GNUNET_CONFIGURATION_get_project_data(const struct GNUNET_CONFIGURATION_Handle *cfg)
Return the project data associated with this configuration.
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.
void GNUNET_PLUGIN_load_all(const struct GNUNET_OS_ProjectData *pd, const char *basename, void *arg, GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
Load all compatible plugins with the given base name.
Definition: plugin.c:401
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:277
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...
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: fs_api.h:1074
A 512-bit hashcode.
Project-specific data used to help the OS subsystem find installation paths.
void * cls
Closure to pass to start_testcase.
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