diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 13:37:38 +0200 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 13:37:38 +0200 |
commit | 9ef4abad615bea12d13be542b8ae5fbeb2dfee32 (patch) | |
tree | 8875a687e004d331c9ea6a1d511a328c72b88113 /src/block | |
parent | e95236b3ed78cd597c15f34b89385295702b627f (diff) | |
download | gnunet-9ef4abad615bea12d13be542b8ae5fbeb2dfee32.tar.gz gnunet-9ef4abad615bea12d13be542b8ae5fbeb2dfee32.zip |
NEWS: Refactoring components under src/ into lib/, plugin/, cli/ and service/
This also includes a necessary API refactoring of crypto from IDENTITY
to UTIL.
Diffstat (limited to 'src/block')
-rw-r--r-- | src/block/Makefile.am | 59 | ||||
-rw-r--r-- | src/block/bg_bf.c | 286 | ||||
-rw-r--r-- | src/block/block.c | 378 | ||||
-rw-r--r-- | src/block/meson.build | 47 | ||||
-rw-r--r-- | src/block/plugin_block_template.c | 222 | ||||
-rw-r--r-- | src/block/plugin_block_test.c | 270 |
6 files changed, 0 insertions, 1262 deletions
diff --git a/src/block/Makefile.am b/src/block/Makefile.am deleted file mode 100644 index ea796bf8f..000000000 --- a/src/block/Makefile.am +++ /dev/null | |||
@@ -1,59 +0,0 @@ | |||
1 | # This Makefile.am is in the public domain | ||
2 | AM_CPPFLAGS = -I$(top_srcdir)/src/include | ||
3 | |||
4 | plugindir = $(libdir)/gnunet | ||
5 | |||
6 | if USE_COVERAGE | ||
7 | AM_CFLAGS = --coverage | ||
8 | endif | ||
9 | |||
10 | lib_LTLIBRARIES = \ | ||
11 | libgnunetblock.la \ | ||
12 | libgnunetblockgroup.la | ||
13 | |||
14 | plugin_LTLIBRARIES = \ | ||
15 | libgnunet_plugin_block_test.la | ||
16 | |||
17 | # Real plugins should of course go into | ||
18 | # plugin_LTLIBRARIES | ||
19 | noinst_LTLIBRARIES = \ | ||
20 | libgnunet_plugin_block_template.la | ||
21 | |||
22 | libgnunet_plugin_block_template_la_SOURCES = \ | ||
23 | plugin_block_template.c | ||
24 | libgnunet_plugin_block_template_la_LIBADD = \ | ||
25 | libgnunetblockgroup.la \ | ||
26 | libgnunetblock.la \ | ||
27 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
28 | $(LTLIBINTL) | ||
29 | libgnunet_plugin_block_template_la_LDFLAGS = \ | ||
30 | $(GN_PLUGIN_LDFLAGS) | ||
31 | |||
32 | libgnunet_plugin_block_test_la_SOURCES = \ | ||
33 | plugin_block_test.c | ||
34 | libgnunet_plugin_block_test_la_LIBADD = \ | ||
35 | libgnunetblockgroup.la \ | ||
36 | libgnunetblock.la \ | ||
37 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
38 | $(LTLIBINTL) | ||
39 | libgnunet_plugin_block_test_la_LDFLAGS = \ | ||
40 | $(GN_PLUGIN_LDFLAGS) | ||
41 | |||
42 | libgnunetblock_la_SOURCES = \ | ||
43 | block.c | ||
44 | libgnunetblock_la_LIBADD = \ | ||
45 | $(top_builddir)/src/util/libgnunetutil.la | ||
46 | libgnunetblock_la_LDFLAGS = \ | ||
47 | $(GN_LIB_LDFLAGS) \ | ||
48 | $(GN_LIBINTL) \ | ||
49 | -version-info 0:0:0 | ||
50 | |||
51 | libgnunetblockgroup_la_SOURCES = \ | ||
52 | bg_bf.c | ||
53 | libgnunetblockgroup_la_LIBADD = \ | ||
54 | libgnunetblock.la \ | ||
55 | $(top_builddir)/src/util/libgnunetutil.la | ||
56 | libgnunetblockgroup_la_LDFLAGS = \ | ||
57 | $(GN_LIB_LDFLAGS) \ | ||
58 | $(GN_LIBINTL) \ | ||
59 | -version-info 0:0:0 | ||
diff --git a/src/block/bg_bf.c b/src/block/bg_bf.c deleted file mode 100644 index c8269498e..000000000 --- a/src/block/bg_bf.c +++ /dev/null | |||
@@ -1,286 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 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 | /** | ||
21 | * @file block/bg_bf.c | ||
22 | * @brief implementation of a block group using a Bloom filter | ||
23 | * to drop duplicate blocks | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_block_group_lib.h" | ||
29 | #include "gnunet_block_plugin.h" | ||
30 | |||
31 | |||
32 | /** | ||
33 | * Internal data structure for a block group. | ||
34 | */ | ||
35 | struct BfGroupInternals | ||
36 | { | ||
37 | /** | ||
38 | * A Bloom filter to weed out duplicate replies probabilistically. | ||
39 | */ | ||
40 | struct GNUNET_CONTAINER_BloomFilter *bf; | ||
41 | |||
42 | /** | ||
43 | * Set from the nonce to mingle the hashes before going into the @e bf. | ||
44 | */ | ||
45 | uint32_t bf_mutator; | ||
46 | |||
47 | /** | ||
48 | * Size of @a bf. | ||
49 | */ | ||
50 | uint32_t bf_size; | ||
51 | }; | ||
52 | |||
53 | |||
54 | /** | ||
55 | * Serialize state of a block group. | ||
56 | * | ||
57 | * @param bg group to serialize | ||
58 | * @param[out] raw_data set to the serialized state | ||
59 | * @param[out] raw_data_size set to the number of bytes in @a raw_data | ||
60 | * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not | ||
61 | * supported, #GNUNET_SYSERR on error | ||
62 | */ | ||
63 | static enum GNUNET_GenericReturnValue | ||
64 | bf_group_serialize_cb (struct GNUNET_BLOCK_Group *bg, | ||
65 | void **raw_data, | ||
66 | size_t *raw_data_size) | ||
67 | { | ||
68 | struct BfGroupInternals *gi = bg->internal_cls; | ||
69 | void *raw; | ||
70 | |||
71 | raw = GNUNET_malloc (gi->bf_size + sizeof (uint32_t)); | ||
72 | if (GNUNET_OK != | ||
73 | GNUNET_CONTAINER_bloomfilter_get_raw_data (gi->bf, | ||
74 | raw + sizeof (uint32_t), | ||
75 | gi->bf_size)) | ||
76 | { | ||
77 | GNUNET_free (raw); | ||
78 | GNUNET_break (0); | ||
79 | return GNUNET_SYSERR; | ||
80 | } | ||
81 | memcpy (raw, | ||
82 | &gi->bf_mutator, | ||
83 | sizeof (uint32_t)); | ||
84 | *raw_data = raw; | ||
85 | *raw_data_size = gi->bf_size + sizeof (uint32_t); | ||
86 | return GNUNET_OK; | ||
87 | } | ||
88 | |||
89 | |||
90 | /** | ||
91 | * Mark elements as "seen" using a hash of the element. Not supported | ||
92 | * by all block plugins. | ||
93 | * | ||
94 | * @param bg group to update | ||
95 | * @param seen_results results already seen | ||
96 | * @param seen_results_count number of entries in @a seen_results | ||
97 | */ | ||
98 | static void | ||
99 | bf_group_mark_seen_cb (struct GNUNET_BLOCK_Group *bg, | ||
100 | const struct GNUNET_HashCode *seen_results, | ||
101 | unsigned int seen_results_count) | ||
102 | { | ||
103 | struct BfGroupInternals *gi = bg->internal_cls; | ||
104 | |||
105 | for (unsigned int i = 0; i < seen_results_count; i++) | ||
106 | { | ||
107 | struct GNUNET_HashCode mhash; | ||
108 | |||
109 | GNUNET_BLOCK_mingle_hash (&seen_results[i], | ||
110 | gi->bf_mutator, | ||
111 | &mhash); | ||
112 | GNUNET_CONTAINER_bloomfilter_add (gi->bf, | ||
113 | &mhash); | ||
114 | } | ||
115 | } | ||
116 | |||
117 | |||
118 | /** | ||
119 | * Merge two groups, if possible. Not supported by all block plugins, | ||
120 | * can also fail if the nonces were different. | ||
121 | * | ||
122 | * @param bg1 group to update | ||
123 | * @param bg2 group to merge into @a bg1 | ||
124 | * @return #GNUNET_OK on success, #GNUNET_NO if the nonces were different and thus | ||
125 | * we failed. | ||
126 | */ | ||
127 | static enum GNUNET_GenericReturnValue | ||
128 | bf_group_merge_cb (struct GNUNET_BLOCK_Group *bg1, | ||
129 | const struct GNUNET_BLOCK_Group *bg2) | ||
130 | { | ||
131 | struct BfGroupInternals *gi1 = bg1->internal_cls; | ||
132 | struct BfGroupInternals *gi2 = bg2->internal_cls; | ||
133 | |||
134 | if (gi1->bf_mutator != gi2->bf_mutator) | ||
135 | return GNUNET_NO; | ||
136 | if (gi1->bf_size != gi2->bf_size) | ||
137 | return GNUNET_NO; | ||
138 | GNUNET_CONTAINER_bloomfilter_or2 (gi1->bf, | ||
139 | gi2->bf); | ||
140 | return GNUNET_OK; | ||
141 | } | ||
142 | |||
143 | |||
144 | /** | ||
145 | * Destroy resources used by a block group. | ||
146 | * | ||
147 | * @param bg group to destroy, NULL is allowed | ||
148 | */ | ||
149 | static void | ||
150 | bf_group_destroy_cb (struct GNUNET_BLOCK_Group *bg) | ||
151 | { | ||
152 | struct BfGroupInternals *gi = bg->internal_cls; | ||
153 | |||
154 | GNUNET_CONTAINER_bloomfilter_free (gi->bf); | ||
155 | GNUNET_free (gi); | ||
156 | GNUNET_free (bg); | ||
157 | } | ||
158 | |||
159 | |||
160 | /** | ||
161 | * Create a new block group that filters duplicates using a Bloom filter. | ||
162 | * | ||
163 | * @param ctx block context in which the block group is created | ||
164 | * @param bf_size size of the Bloom filter | ||
165 | * @param bf_k K-value for the Bloom filter | ||
166 | * @param type block type | ||
167 | * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh | ||
168 | * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh | ||
169 | * @return block group handle, NULL if block groups are not supported | ||
170 | * by this @a type of block (this is not an error) | ||
171 | */ | ||
172 | struct GNUNET_BLOCK_Group * | ||
173 | GNUNET_BLOCK_GROUP_bf_create (void *cls, | ||
174 | size_t bf_size, | ||
175 | unsigned int bf_k, | ||
176 | enum GNUNET_BLOCK_Type type, | ||
177 | const void *raw_data, | ||
178 | size_t raw_data_size) | ||
179 | { | ||
180 | struct BfGroupInternals *gi; | ||
181 | struct GNUNET_BLOCK_Group *bg; | ||
182 | uint32_t nonce; | ||
183 | |||
184 | if ( (NULL != raw_data) && | ||
185 | (raw_data_size < sizeof (nonce)) ) | ||
186 | { | ||
187 | GNUNET_break_op (0); | ||
188 | return NULL; | ||
189 | } | ||
190 | if (NULL != raw_data) | ||
191 | { | ||
192 | memcpy (&nonce, | ||
193 | raw_data, | ||
194 | sizeof (nonce)); | ||
195 | raw_data += sizeof (nonce); | ||
196 | raw_data_size -= sizeof (nonce); | ||
197 | } | ||
198 | else | ||
199 | { | ||
200 | nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, | ||
201 | UINT32_MAX); | ||
202 | } | ||
203 | gi = GNUNET_new (struct BfGroupInternals); | ||
204 | gi->bf = GNUNET_CONTAINER_bloomfilter_init ((bf_size != raw_data_size) ? | ||
205 | NULL : raw_data, | ||
206 | bf_size, | ||
207 | bf_k); | ||
208 | gi->bf_mutator = nonce; | ||
209 | gi->bf_size = bf_size; | ||
210 | bg = GNUNET_new (struct GNUNET_BLOCK_Group); | ||
211 | bg->type = type; | ||
212 | bg->serialize_cb = &bf_group_serialize_cb; | ||
213 | bg->mark_seen_cb = &bf_group_mark_seen_cb; | ||
214 | bg->merge_cb = &bf_group_merge_cb; | ||
215 | bg->destroy_cb = &bf_group_destroy_cb; | ||
216 | bg->internal_cls = gi; | ||
217 | return bg; | ||
218 | } | ||
219 | |||
220 | |||
221 | /** | ||
222 | * Test if @a hc is contained in the Bloom filter of @a bg. If so, | ||
223 | * return #GNUNET_YES. If not, add @a hc to the Bloom filter and | ||
224 | * return #GNUNET_NO. | ||
225 | * | ||
226 | * @param bg block group to use for testing | ||
227 | * @param hc hash of element to evaluate | ||
228 | * @return #GNUNET_YES if @a hc is (likely) a duplicate | ||
229 | * #GNUNET_NO if @a hc was definitively not in @a bg (but now is) | ||
230 | */ | ||
231 | enum GNUNET_GenericReturnValue | ||
232 | GNUNET_BLOCK_GROUP_bf_test_and_set (struct GNUNET_BLOCK_Group *bg, | ||
233 | const struct GNUNET_HashCode *hc) | ||
234 | { | ||
235 | struct BfGroupInternals *gi; | ||
236 | struct GNUNET_HashCode mhash; | ||
237 | |||
238 | if (NULL == bg) | ||
239 | return GNUNET_NO; | ||
240 | gi = bg->internal_cls; | ||
241 | GNUNET_BLOCK_mingle_hash (hc, | ||
242 | gi->bf_mutator, | ||
243 | &mhash); | ||
244 | if (GNUNET_YES == | ||
245 | GNUNET_CONTAINER_bloomfilter_test (gi->bf, | ||
246 | &mhash)) | ||
247 | return GNUNET_YES; | ||
248 | GNUNET_CONTAINER_bloomfilter_add (gi->bf, | ||
249 | &mhash); | ||
250 | return GNUNET_NO; | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | ||
255 | * How many bytes should a bloomfilter be if we have already seen | ||
256 | * entry_count responses? Sized so that do not have to | ||
257 | * re-size the filter too often (to keep it cheap). | ||
258 | * | ||
259 | * Since other peers will also add entries but not resize the filter, | ||
260 | * we should generally pick a slightly larger size than what the | ||
261 | * strict math would suggest. | ||
262 | * | ||
263 | * @param entry_count expected number of entries in the Bloom filter | ||
264 | * @param k number of bits set per entry | ||
265 | * @return must be a power of two and smaller or equal to 2^15. | ||
266 | */ | ||
267 | size_t | ||
268 | GNUNET_BLOCK_GROUP_compute_bloomfilter_size (unsigned int entry_count, | ||
269 | unsigned int k) | ||
270 | { | ||
271 | size_t size; | ||
272 | unsigned int ideal = (entry_count * k) / 4; | ||
273 | uint16_t max = 1 << 15; | ||
274 | |||
275 | if (entry_count > max) | ||
276 | return max; | ||
277 | size = 8; | ||
278 | while ((size < max) && (size < ideal)) | ||
279 | size *= 2; | ||
280 | if (size > max) | ||
281 | return max; | ||
282 | return size; | ||
283 | } | ||
284 | |||
285 | |||
286 | /* end of bg_bf.c */ | ||
diff --git a/src/block/block.c b/src/block/block.c deleted file mode 100644 index 9edc7ce5e..000000000 --- a/src/block/block.c +++ /dev/null | |||
@@ -1,378 +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; | ||
303 | |||
304 | if (GNUNET_BLOCK_TYPE_ANY == type) | ||
305 | return GNUNET_SYSERR; /* no checks */ | ||
306 | plugin = find_plugin (ctx, | ||
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 | |||
318 | enum GNUNET_GenericReturnValue | ||
319 | GNUNET_BLOCK_check_block (struct GNUNET_BLOCK_Context *ctx, | ||
320 | enum GNUNET_BLOCK_Type type, | ||
321 | const void *block, | ||
322 | size_t block_size) | ||
323 | { | ||
324 | struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, | ||
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 | |||
336 | enum GNUNET_BLOCK_ReplyEvaluationResult | ||
337 | GNUNET_BLOCK_check_reply (struct GNUNET_BLOCK_Context *ctx, | ||
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 | { | ||
346 | struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, | ||
347 | type); | ||
348 | |||
349 | if (NULL == plugin) | ||
350 | return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED; | ||
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 | |||
362 | enum GNUNET_GenericReturnValue | ||
363 | GNUNET_BLOCK_group_set_seen (struct GNUNET_BLOCK_Group *bg, | ||
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 */ | ||
diff --git a/src/block/meson.build b/src/block/meson.build deleted file mode 100644 index b9bde48f6..000000000 --- a/src/block/meson.build +++ /dev/null | |||
@@ -1,47 +0,0 @@ | |||
1 | libgnunetblock_src = ['block.c'] | ||
2 | libgnunetblockgroup_src = ['bg_bf.c'] | ||
3 | |||
4 | if get_option('monolith') | ||
5 | foreach p : libgnunetblock_src + libgnunetblockgroup_src | ||
6 | gnunet_src += 'block/' + p | ||
7 | endforeach | ||
8 | subdir_done() | ||
9 | endif | ||
10 | |||
11 | libgnunetblock = library('gnunetblock', | ||
12 | libgnunetblock_src, | ||
13 | dependencies: libgnunetutil_dep, | ||
14 | include_directories: [incdir, configuration_inc], | ||
15 | install: true, | ||
16 | version: '0.0.0', | ||
17 | soversion: '0', | ||
18 | install_dir: get_option('libdir')) | ||
19 | libgnunetblock_dep = declare_dependency(link_with : libgnunetblock) | ||
20 | pkg.generate(libgnunetblock, url: 'https://www.gnunet.org', | ||
21 | description : 'Library for data block manipulation') | ||
22 | |||
23 | libgnunetblockgroup = library('gnunetblockgroup', | ||
24 | libgnunetblockgroup_src, | ||
25 | dependencies: [libgnunetutil_dep, libgnunetblock_dep], | ||
26 | include_directories: [incdir, configuration_inc], | ||
27 | install: true, | ||
28 | version: '0.0.0', | ||
29 | soversion: '0', | ||
30 | install_dir: get_option('libdir')) | ||
31 | libgnunetblockgroup_dep = declare_dependency(link_with : libgnunetblockgroup) | ||
32 | |||
33 | plugin_dep = [libgnunetutil_dep, libgnunetblock_dep, libgnunetblockgroup_dep] | ||
34 | shared_module('gnunet_plugin_block_test', | ||
35 | ['plugin_block_test.c'], | ||
36 | dependencies: plugin_dep, | ||
37 | include_directories: [incdir, configuration_inc], | ||
38 | install: true, | ||
39 | install_dir: get_option('libdir')/'gnunet') | ||
40 | |||
41 | shared_module('gnunet_plugin_block_template', | ||
42 | ['plugin_block_template.c'], | ||
43 | dependencies: plugin_dep, | ||
44 | include_directories: [incdir, configuration_inc], | ||
45 | install: false, | ||
46 | install_dir: get_option('libdir')/'gnunet') | ||
47 | |||
diff --git a/src/block/plugin_block_template.c b/src/block/plugin_block_template.c deleted file mode 100644 index 4a271fa42..000000000 --- a/src/block/plugin_block_template.c +++ /dev/null | |||
@@ -1,222 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010, 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/plugin_block_template.c | ||
23 | * @brief template for a block plugin | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_block_plugin.h" | ||
29 | #include "gnunet_block_group_lib.h" | ||
30 | |||
31 | #define DEBUG_TEMPLATE GNUNET_EXTRA_LOGGING | ||
32 | |||
33 | /** | ||
34 | * Number of bits we set per entry in the bloomfilter. | ||
35 | * Do not change! | ||
36 | */ | ||
37 | #define BLOOMFILTER_K 16 | ||
38 | |||
39 | |||
40 | /** | ||
41 | * How big is the BF we use for DHT blocks? | ||
42 | */ | ||
43 | #define TEMPLATE_BF_SIZE 8 | ||
44 | |||
45 | |||
46 | /** | ||
47 | * Create a new block group. | ||
48 | * | ||
49 | * @param ctx block context in which the block group is created | ||
50 | * @param type type of the block for which we are creating the group | ||
51 | * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh | ||
52 | * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh | ||
53 | * @param va variable arguments specific to @a type | ||
54 | * @return block group handle, NULL if block groups are not supported | ||
55 | * by this @a type of block (this is not an error) | ||
56 | */ | ||
57 | static struct GNUNET_BLOCK_Group * | ||
58 | block_plugin_template_create_group (void *ctx, | ||
59 | enum GNUNET_BLOCK_Type type, | ||
60 | const void *raw_data, | ||
61 | size_t raw_data_size, | ||
62 | va_list va) | ||
63 | { | ||
64 | unsigned int bf_size; | ||
65 | const char *guard; | ||
66 | |||
67 | guard = va_arg (va, const char *); | ||
68 | if (0 == strcmp (guard, | ||
69 | "seen-set-size")) | ||
70 | bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned | ||
71 | int), | ||
72 | BLOOMFILTER_K); | ||
73 | else if (0 == strcmp (guard, | ||
74 | "filter-size")) | ||
75 | bf_size = va_arg (va, unsigned int); | ||
76 | else | ||
77 | { | ||
78 | GNUNET_break (0); | ||
79 | bf_size = TEMPLATE_BF_SIZE; | ||
80 | } | ||
81 | GNUNET_break (NULL == va_arg (va, const char *)); | ||
82 | return GNUNET_BLOCK_GROUP_bf_create (ctx, | ||
83 | bf_size, | ||
84 | BLOOMFILTER_K, | ||
85 | type, | ||
86 | raw_data, | ||
87 | raw_data_size); | ||
88 | } | ||
89 | |||
90 | |||
91 | /** | ||
92 | * Function called to validate a query. | ||
93 | * | ||
94 | * @param cls closure | ||
95 | * @param type block type | ||
96 | * @param query original query (hash) | ||
97 | * @param xquery extended query data (can be NULL, depending on type) | ||
98 | * @param xquery_size number of bytes in @a xquery | ||
99 | * @return #GNUNET_OK if the query is fine, #GNUNET_NO if not, #GNUNET_SYSERR if not supported | ||
100 | */ | ||
101 | static enum GNUNET_GenericReturnValue | ||
102 | block_plugin_template_check_query (void *cls, | ||
103 | enum GNUNET_BLOCK_Type type, | ||
104 | const struct GNUNET_HashCode *query, | ||
105 | const void *xquery, | ||
106 | size_t xquery_size) | ||
107 | { | ||
108 | return GNUNET_SYSERR; | ||
109 | } | ||
110 | |||
111 | |||
112 | /** | ||
113 | * Function called to validate a block for storage. | ||
114 | * | ||
115 | * @param cls closure | ||
116 | * @param type block type | ||
117 | * @param block block data to validate | ||
118 | * @param block_size number of bytes in @a block | ||
119 | * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not, #GNUNET_SYSERR if not supported | ||
120 | */ | ||
121 | static enum GNUNET_GenericReturnValue | ||
122 | block_plugin_template_check_block (void *cls, | ||
123 | enum GNUNET_BLOCK_Type type, | ||
124 | const void *block, | ||
125 | size_t block_size) | ||
126 | { | ||
127 | return GNUNET_SYSERR; | ||
128 | } | ||
129 | |||
130 | |||
131 | /** | ||
132 | * Function called to validate a reply to a request. Note that it is assumed | ||
133 | * that the reply has already been matched to the key (and signatures checked) | ||
134 | * as it would be done with the GetKeyFunction and the | ||
135 | * BlockEvaluationFunction. | ||
136 | * | ||
137 | * @param cls closure | ||
138 | * @param type block type | ||
139 | * @param group which block group to use for evaluation | ||
140 | * @param query original query (hash) | ||
141 | * @param xquery extrended query data (can be NULL, depending on type) | ||
142 | * @param xquery_size number of bytes in @a xquery | ||
143 | * @param reply_block response to validate | ||
144 | * @param reply_block_size number of bytes in @a reply_block | ||
145 | * @return characterization of result | ||
146 | */ | ||
147 | static enum GNUNET_BLOCK_ReplyEvaluationResult | ||
148 | block_plugin_template_check_reply ( | ||
149 | void *cls, | ||
150 | enum GNUNET_BLOCK_Type type, | ||
151 | struct GNUNET_BLOCK_Group *group, | ||
152 | const struct GNUNET_HashCode *query, | ||
153 | const void *xquery, | ||
154 | size_t xquery_size, | ||
155 | const void *reply_block, | ||
156 | size_t reply_block_size) | ||
157 | { | ||
158 | return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED; | ||
159 | } | ||
160 | |||
161 | |||
162 | /** | ||
163 | * Function called to obtain the key for a block. | ||
164 | * | ||
165 | * @param cls closure | ||
166 | * @param type block type | ||
167 | * @param block block to get the key for | ||
168 | * @param block_size number of bytes in block | ||
169 | * @param key set to the key (query) for the given block | ||
170 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported | ||
171 | * (or if extracting a key from a block of this type does not work) | ||
172 | */ | ||
173 | static enum GNUNET_GenericReturnValue | ||
174 | block_plugin_template_get_key (void *cls, | ||
175 | enum GNUNET_BLOCK_Type type, | ||
176 | const void *block, | ||
177 | size_t block_size, | ||
178 | struct GNUNET_HashCode *key) | ||
179 | { | ||
180 | return GNUNET_SYSERR; | ||
181 | } | ||
182 | |||
183 | |||
184 | /** | ||
185 | * Entry point for the plugin. | ||
186 | * | ||
187 | * @param cls a `const struct GNUNET_CONFIGURATION_Handle` | ||
188 | */ | ||
189 | void * | ||
190 | libgnunet_plugin_block_template_init (void *cls) | ||
191 | { | ||
192 | static const enum GNUNET_BLOCK_Type types[] = { | ||
193 | /* NOTE: insert supported block types here */ | ||
194 | GNUNET_BLOCK_TYPE_ANY /* end of list */ | ||
195 | }; | ||
196 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
197 | |||
198 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); | ||
199 | api->get_key = &block_plugin_template_get_key; | ||
200 | api->check_query = &block_plugin_template_check_query; | ||
201 | api->check_block = &block_plugin_template_check_block; | ||
202 | api->check_reply = &block_plugin_template_check_reply; | ||
203 | api->create_group = &block_plugin_template_create_group; | ||
204 | api->types = types; | ||
205 | return api; | ||
206 | } | ||
207 | |||
208 | |||
209 | /** | ||
210 | * Exit point from the plugin. | ||
211 | */ | ||
212 | void * | ||
213 | libgnunet_plugin_block_template_done (void *cls) | ||
214 | { | ||
215 | struct GNUNET_BLOCK_PluginFunctions *api = cls; | ||
216 | |||
217 | GNUNET_free (api); | ||
218 | return NULL; | ||
219 | } | ||
220 | |||
221 | |||
222 | /* end of plugin_block_template.c */ | ||
diff --git a/src/block/plugin_block_test.c b/src/block/plugin_block_test.c deleted file mode 100644 index 5afac9205..000000000 --- a/src/block/plugin_block_test.c +++ /dev/null | |||
@@ -1,270 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010, 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/plugin_block_test.c | ||
23 | * @brief block plugin to test the DHT as a simple key-value store; | ||
24 | * this plugin simply accepts any (new) response for any key | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | |||
28 | #include "platform.h" | ||
29 | #include "gnunet_block_plugin.h" | ||
30 | #include "gnunet_block_group_lib.h" | ||
31 | |||
32 | /** | ||
33 | * Number of bits we set per entry in the bloomfilter. | ||
34 | * Do not change! | ||
35 | */ | ||
36 | #define BLOOMFILTER_K 16 | ||
37 | |||
38 | /** | ||
39 | * How big is the BF we use for DHT blocks? | ||
40 | */ | ||
41 | #define TEST_BF_SIZE 8 | ||
42 | |||
43 | |||
44 | /** | ||
45 | * Create a new block group. | ||
46 | * | ||
47 | * @param ctx block context in which the block group is created | ||
48 | * @param type type of the block for which we are creating the group | ||
49 | * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh | ||
50 | * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh | ||
51 | * @param va variable arguments specific to @a type | ||
52 | * @return block group handle, NULL if block groups are not supported | ||
53 | * by this @a type of block (this is not an error) | ||
54 | */ | ||
55 | static struct GNUNET_BLOCK_Group * | ||
56 | block_plugin_test_create_group (void *ctx, | ||
57 | enum GNUNET_BLOCK_Type type, | ||
58 | const void *raw_data, | ||
59 | size_t raw_data_size, | ||
60 | va_list va) | ||
61 | { | ||
62 | unsigned int bf_size; | ||
63 | const char *guard; | ||
64 | |||
65 | guard = va_arg (va, const char *); | ||
66 | if (0 == strcmp (guard, | ||
67 | "seen-set-size")) | ||
68 | bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned | ||
69 | int), | ||
70 | BLOOMFILTER_K); | ||
71 | else if (0 == strcmp (guard, | ||
72 | "filter-size")) | ||
73 | bf_size = va_arg (va, unsigned int); | ||
74 | else | ||
75 | { | ||
76 | GNUNET_break (0); | ||
77 | bf_size = TEST_BF_SIZE; | ||
78 | } | ||
79 | GNUNET_break (NULL == va_arg (va, const char *)); | ||
80 | return GNUNET_BLOCK_GROUP_bf_create (ctx, | ||
81 | bf_size, | ||
82 | BLOOMFILTER_K, | ||
83 | type, | ||
84 | raw_data, | ||
85 | raw_data_size); | ||
86 | } | ||
87 | |||
88 | |||
89 | /** | ||
90 | * Function called to validate a query. | ||
91 | * | ||
92 | * @param cls closure | ||
93 | * @param type block type | ||
94 | * @param query original query (hash) | ||
95 | * @param xquery extended query data (can be NULL, depending on type) | ||
96 | * @param xquery_size number of bytes in @a xquery | ||
97 | * @return #GNUNET_OK if the query is fine, #GNUNET_NO if not, #GNUNET_SYSERR if @a type is not supported | ||
98 | */ | ||
99 | static enum GNUNET_GenericReturnValue | ||
100 | block_plugin_test_check_query (void *cls, | ||
101 | enum GNUNET_BLOCK_Type type, | ||
102 | const struct GNUNET_HashCode *query, | ||
103 | const void *xquery, | ||
104 | size_t xquery_size) | ||
105 | { | ||
106 | (void) cls; | ||
107 | (void) query; | ||
108 | (void) xquery; | ||
109 | if (GNUNET_BLOCK_TYPE_TEST != type) | ||
110 | { | ||
111 | GNUNET_break (0); | ||
112 | return GNUNET_SYSERR; | ||
113 | } | ||
114 | if (0 != xquery_size) | ||
115 | { | ||
116 | GNUNET_break_op (0); | ||
117 | return GNUNET_NO; | ||
118 | } | ||
119 | return GNUNET_OK; | ||
120 | } | ||
121 | |||
122 | |||
123 | /** | ||
124 | * Function called to validate a block for storage. | ||
125 | * | ||
126 | * @param cls closure | ||
127 | * @param type block type | ||
128 | * @param block block data to validate | ||
129 | * @param block_size number of bytes in @a block | ||
130 | * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not, #GNUNET_SYSERR if @a type is not supported | ||
131 | */ | ||
132 | static enum GNUNET_GenericReturnValue | ||
133 | block_plugin_test_check_block (void *cls, | ||
134 | enum GNUNET_BLOCK_Type type, | ||
135 | const void *block, | ||
136 | size_t block_size) | ||
137 | { | ||
138 | (void) cls; | ||
139 | (void) block; | ||
140 | (void) block_size; | ||
141 | if (GNUNET_BLOCK_TYPE_TEST != type) | ||
142 | { | ||
143 | GNUNET_break (0); | ||
144 | return GNUNET_SYSERR; | ||
145 | } | ||
146 | return GNUNET_OK; | ||
147 | } | ||
148 | |||
149 | |||
150 | /** | ||
151 | * Function called to validate a reply to a request. Note that it is assumed | ||
152 | * that the reply has already been matched to the key (and signatures checked) | ||
153 | * as it would be done with the GetKeyFunction and the | ||
154 | * BlockEvaluationFunction. | ||
155 | * | ||
156 | * @param cls closure | ||
157 | * @param type block type | ||
158 | * @param group which block group to use for evaluation | ||
159 | * @param query original query (hash) | ||
160 | * @param xquery extrended query data (can be NULL, depending on type) | ||
161 | * @param xquery_size number of bytes in @a xquery | ||
162 | * @param reply_block response to validate | ||
163 | * @param reply_block_size number of bytes in @a reply_block | ||
164 | * @return characterization of result | ||
165 | */ | ||
166 | static enum GNUNET_BLOCK_ReplyEvaluationResult | ||
167 | block_plugin_test_check_reply (void *cls, | ||
168 | enum GNUNET_BLOCK_Type type, | ||
169 | struct GNUNET_BLOCK_Group *group, | ||
170 | const struct GNUNET_HashCode *query, | ||
171 | const void *xquery, | ||
172 | size_t xquery_size, | ||
173 | const void *reply_block, | ||
174 | size_t reply_block_size) | ||
175 | { | ||
176 | struct GNUNET_HashCode chash; | ||
177 | |||
178 | (void) cls; | ||
179 | (void) query; | ||
180 | (void) xquery; | ||
181 | (void) xquery_size; | ||
182 | if (GNUNET_BLOCK_TYPE_TEST != type) | ||
183 | { | ||
184 | GNUNET_break (0); | ||
185 | return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED; | ||
186 | } | ||
187 | GNUNET_CRYPTO_hash (reply_block, | ||
188 | reply_block_size, | ||
189 | &chash); | ||
190 | if (GNUNET_YES == | ||
191 | GNUNET_BLOCK_GROUP_bf_test_and_set (group, | ||
192 | &chash)) | ||
193 | return GNUNET_BLOCK_REPLY_OK_DUPLICATE; | ||
194 | return GNUNET_BLOCK_REPLY_OK_MORE; | ||
195 | } | ||
196 | |||
197 | |||
198 | /** | ||
199 | * Function called to obtain the key for a block. | ||
200 | * | ||
201 | * @param cls closure | ||
202 | * @param type block type | ||
203 | * @param block block to get the key for | ||
204 | * @param block_size number of bytes in @a block | ||
205 | * @param key set to the key (query) for the given block | ||
206 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported, #GNUNET_NO if extracting a key from a block of this type does not work | ||
207 | */ | ||
208 | static enum GNUNET_GenericReturnValue | ||
209 | block_plugin_test_get_key (void *cls, | ||
210 | enum GNUNET_BLOCK_Type type, | ||
211 | const void *block, | ||
212 | size_t block_size, | ||
213 | struct GNUNET_HashCode *key) | ||
214 | { | ||
215 | (void) cls; | ||
216 | (void) block; | ||
217 | (void) block_size; | ||
218 | (void) key; | ||
219 | if (GNUNET_BLOCK_TYPE_TEST != type) | ||
220 | { | ||
221 | GNUNET_break (0); | ||
222 | return GNUNET_SYSERR; | ||
223 | } | ||
224 | return GNUNET_NO; | ||
225 | } | ||
226 | |||
227 | |||
228 | /** | ||
229 | * Entry point for the plugin. | ||
230 | * | ||
231 | * @param cls NULL | ||
232 | * @return the exported block API | ||
233 | */ | ||
234 | void * | ||
235 | libgnunet_plugin_block_test_init (void *cls) | ||
236 | { | ||
237 | static const enum GNUNET_BLOCK_Type types[] = { | ||
238 | GNUNET_BLOCK_TYPE_TEST, | ||
239 | GNUNET_BLOCK_TYPE_ANY /* end of list */ | ||
240 | }; | ||
241 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
242 | |||
243 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); | ||
244 | api->get_key = &block_plugin_test_get_key; | ||
245 | api->check_query = &block_plugin_test_check_query; | ||
246 | api->check_block = &block_plugin_test_check_block; | ||
247 | api->check_reply = &block_plugin_test_check_reply; | ||
248 | api->create_group = &block_plugin_test_create_group; | ||
249 | api->types = types; | ||
250 | return api; | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | ||
255 | * Exit point from the plugin. | ||
256 | * | ||
257 | * @param cls the return value from #libgnunet_plugin_block_test_init | ||
258 | * @return NULL | ||
259 | */ | ||
260 | void * | ||
261 | libgnunet_plugin_block_test_done (void *cls) | ||
262 | { | ||
263 | struct GNUNET_BLOCK_PluginFunctions *api = cls; | ||
264 | |||
265 | GNUNET_free (api); | ||
266 | return NULL; | ||
267 | } | ||
268 | |||
269 | |||
270 | /* end of plugin_block_test.c */ | ||