diff options
Diffstat (limited to 'src/block/block.c')
-rw-r--r-- | src/block/block.c | 375 |
1 files changed, 0 insertions, 375 deletions
diff --git a/src/block/block.c b/src/block/block.c deleted file mode 100644 index 4b2d8a960..000000000 --- a/src/block/block.c +++ /dev/null | |||
@@ -1,375 +0,0 @@ | |||
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 | |||
21 | /** | ||
22 | * @file block/block.c | ||
23 | * @brief library for data block manipulation | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
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 | |||
34 | /** | ||
35 | * Handle for a plugin. | ||
36 | */ | ||
37 | struct Plugin | ||
38 | { | ||
39 | /** | ||
40 | * Name of the shared library. | ||
41 | */ | ||
42 | char *library_name; | ||
43 | |||
44 | /** | ||
45 | * Plugin API. | ||
46 | */ | ||
47 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
48 | }; | ||
49 | |||
50 | |||
51 | /** | ||
52 | * Handle to an initialized block library. | ||
53 | */ | ||
54 | struct GNUNET_BLOCK_Context | ||
55 | { | ||
56 | /** | ||
57 | * Array of our plugins. | ||
58 | */ | ||
59 | struct Plugin **plugins; | ||
60 | |||
61 | /** | ||
62 | * Size of the 'plugins' array. | ||
63 | */ | ||
64 | unsigned int num_plugins; | ||
65 | |||
66 | /** | ||
67 | * Our configuration. | ||
68 | */ | ||
69 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
70 | }; | ||
71 | |||
72 | |||
73 | GNUNET_NETWORK_STRUCT_BEGIN | ||
74 | |||
75 | |||
76 | /** | ||
77 | * Serialization to use in #GNUNET_BLOCK_mingle_hash. | ||
78 | */ | ||
79 | struct MinglePacker | ||
80 | { | ||
81 | /** | ||
82 | * Original hash. | ||
83 | */ | ||
84 | struct GNUNET_HashCode in; | ||
85 | |||
86 | /** | ||
87 | * Mingle value. | ||
88 | */ | ||
89 | uint32_t mingle GNUNET_PACKED; | ||
90 | }; | ||
91 | |||
92 | GNUNET_NETWORK_STRUCT_END | ||
93 | |||
94 | |||
95 | void | ||
96 | GNUNET_BLOCK_mingle_hash (const struct GNUNET_HashCode *in, | ||
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 | |||
111 | /** | ||
112 | * Add a plugin to the list managed by the block library. | ||
113 | * | ||
114 | * @param cls the block context | ||
115 | * @param library_name name of the plugin | ||
116 | * @param lib_ret the plugin API | ||
117 | */ | ||
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 | |||
127 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
128 | "Loading block plugin `%s'\n", | ||
129 | library_name); | ||
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 | |||
139 | struct GNUNET_BLOCK_Context * | ||
140 | GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
141 | { | ||
142 | struct GNUNET_BLOCK_Context *ctx; | ||
143 | |||
144 | ctx = GNUNET_new (struct GNUNET_BLOCK_Context); | ||
145 | ctx->cfg = cfg; | ||
146 | GNUNET_PLUGIN_load_all_in_context (GNUNET_OS_project_data_default (), | ||
147 | "libgnunet_plugin_block_", | ||
148 | (void *) cfg, | ||
149 | &add_plugin, | ||
150 | ctx); | ||
151 | return ctx; | ||
152 | } | ||
153 | |||
154 | |||
155 | void | ||
156 | GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx) | ||
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 == | ||
164 | GNUNET_PLUGIN_unload (plugin->library_name, | ||
165 | plugin->api)); | ||
166 | GNUNET_free (plugin->library_name); | ||
167 | GNUNET_free (plugin); | ||
168 | } | ||
169 | GNUNET_free (ctx->plugins); | ||
170 | GNUNET_free (ctx); | ||
171 | } | ||
172 | |||
173 | |||
174 | enum GNUNET_GenericReturnValue | ||
175 | GNUNET_BLOCK_group_serialize (struct GNUNET_BLOCK_Group *bg, | ||
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 | ||
192 | GNUNET_BLOCK_group_destroy (struct GNUNET_BLOCK_Group *bg) | ||
193 | { | ||
194 | if (NULL == bg) | ||
195 | return; | ||
196 | bg->destroy_cb (bg); | ||
197 | } | ||
198 | |||
199 | |||
200 | enum GNUNET_GenericReturnValue | ||
201 | GNUNET_BLOCK_group_merge (struct GNUNET_BLOCK_Group *bg1, | ||
202 | struct GNUNET_BLOCK_Group *bg2) | ||
203 | { | ||
204 | enum GNUNET_GenericReturnValue ret; | ||
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 | |||
223 | /** | ||
224 | * Find a plugin for the given type. | ||
225 | * | ||
226 | * @param ctx context to search | ||
227 | * @param type type to look for | ||
228 | * @return NULL if no matching plugin exists | ||
229 | */ | ||
230 | static struct GNUNET_BLOCK_PluginFunctions * | ||
231 | find_plugin (struct GNUNET_BLOCK_Context *ctx, | ||
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 * | ||
247 | GNUNET_BLOCK_group_create (struct GNUNET_BLOCK_Context *ctx, | ||
248 | enum GNUNET_BLOCK_Type type, | ||
249 | const void *raw_data, | ||
250 | size_t raw_data_size, | ||
251 | ...) | ||
252 | { | ||
253 | struct GNUNET_BLOCK_PluginFunctions *plugin; | ||
254 | struct GNUNET_BLOCK_Group *bg; | ||
255 | va_list ap; | ||
256 | |||
257 | plugin = find_plugin (ctx, | ||
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 | |||
275 | enum GNUNET_GenericReturnValue | ||
276 | GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx, | ||
277 | enum GNUNET_BLOCK_Type type, | ||
278 | const void *block, | ||
279 | size_t block_size, | ||
280 | struct GNUNET_HashCode *key) | ||
281 | { | ||
282 | struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, | ||
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 | |||
295 | enum GNUNET_GenericReturnValue | ||
296 | GNUNET_BLOCK_check_query (struct GNUNET_BLOCK_Context *ctx, | ||
297 | enum GNUNET_BLOCK_Type type, | ||
298 | const struct GNUNET_HashCode *query, | ||
299 | const void *xquery, | ||
300 | size_t xquery_size) | ||
301 | { | ||
302 | struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, | ||
303 | type); | ||
304 | |||
305 | if (NULL == plugin) | ||
306 | return GNUNET_SYSERR; | ||
307 | return plugin->check_query (plugin->cls, | ||
308 | type, | ||
309 | query, | ||
310 | xquery, | ||
311 | xquery_size); | ||
312 | } | ||
313 | |||
314 | |||
315 | enum GNUNET_GenericReturnValue | ||
316 | GNUNET_BLOCK_check_block (struct GNUNET_BLOCK_Context *ctx, | ||
317 | enum GNUNET_BLOCK_Type type, | ||
318 | const void *block, | ||
319 | size_t block_size) | ||
320 | { | ||
321 | struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, | ||
322 | type); | ||
323 | |||
324 | if (NULL == plugin) | ||
325 | return GNUNET_SYSERR; | ||
326 | return plugin->check_block (plugin->cls, | ||
327 | type, | ||
328 | block, | ||
329 | block_size); | ||
330 | } | ||
331 | |||
332 | |||
333 | enum GNUNET_BLOCK_ReplyEvaluationResult | ||
334 | GNUNET_BLOCK_check_reply (struct GNUNET_BLOCK_Context *ctx, | ||
335 | enum GNUNET_BLOCK_Type type, | ||
336 | struct GNUNET_BLOCK_Group *group, | ||
337 | const struct GNUNET_HashCode *query, | ||
338 | const void *xquery, | ||
339 | size_t xquery_size, | ||
340 | const void *reply_block, | ||
341 | size_t reply_block_size) | ||
342 | { | ||
343 | struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, | ||
344 | type); | ||
345 | |||
346 | if (NULL == plugin) | ||
347 | return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED; | ||
348 | return plugin->check_reply (plugin->cls, | ||
349 | type, | ||
350 | group, | ||
351 | query, | ||
352 | xquery, | ||
353 | xquery_size, | ||
354 | reply_block, | ||
355 | reply_block_size); | ||
356 | } | ||
357 | |||
358 | |||
359 | enum GNUNET_GenericReturnValue | ||
360 | GNUNET_BLOCK_group_set_seen (struct GNUNET_BLOCK_Group *bg, | ||
361 | const struct GNUNET_HashCode *seen_results, | ||
362 | unsigned int seen_results_count) | ||
363 | { | ||
364 | if (NULL == bg) | ||
365 | return GNUNET_OK; | ||
366 | if (NULL == bg->mark_seen_cb) | ||
367 | return GNUNET_SYSERR; | ||
368 | bg->mark_seen_cb (bg, | ||
369 | seen_results, | ||
370 | seen_results_count); | ||
371 | return GNUNET_OK; | ||
372 | } | ||
373 | |||
374 | |||
375 | /* end of block.c */ | ||