aboutsummaryrefslogtreecommitdiff
path: root/src/plugin/block
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugin/block')
-rw-r--r--src/plugin/block/Makefile.am37
-rw-r--r--src/plugin/block/meson.build15
-rw-r--r--src/plugin/block/plugin_block_template.c222
-rw-r--r--src/plugin/block/plugin_block_test.c270
4 files changed, 544 insertions, 0 deletions
diff --git a/src/plugin/block/Makefile.am b/src/plugin/block/Makefile.am
new file mode 100644
index 000000000..9b72e4e3c
--- /dev/null
+++ b/src/plugin/block/Makefile.am
@@ -0,0 +1,37 @@
1# This Makefile.am is in the public domain
2AM_CPPFLAGS = -I$(top_srcdir)/src/include
3
4plugindir = $(libdir)/gnunet
5
6if USE_COVERAGE
7 AM_CFLAGS = --coverage
8endif
9
10plugin_LTLIBRARIES = \
11 libgnunet_plugin_block_test.la
12
13# Real plugins should of course go into
14# plugin_LTLIBRARIES
15noinst_LTLIBRARIES = \
16 libgnunet_plugin_block_template.la
17
18libgnunet_plugin_block_template_la_SOURCES = \
19 plugin_block_template.c
20libgnunet_plugin_block_template_la_LIBADD = \
21 $(top_builddir)/src/lib/block/libgnunetblockgroup.la \
22 $(top_builddir)/src/lib/block/libgnunetblock.la \
23 $(top_builddir)/src/lib/util/libgnunetutil.la \
24 $(LTLIBINTL)
25libgnunet_plugin_block_template_la_LDFLAGS = \
26 $(GN_PLUGIN_LDFLAGS)
27
28libgnunet_plugin_block_test_la_SOURCES = \
29 plugin_block_test.c
30libgnunet_plugin_block_test_la_LIBADD = \
31 $(top_builddir)/src/lib/block/libgnunetblockgroup.la \
32 $(top_builddir)/src/lib/block/libgnunetblock.la \
33 $(top_builddir)/src/lib/util/libgnunetutil.la \
34 $(LTLIBINTL)
35libgnunet_plugin_block_test_la_LDFLAGS = \
36 $(GN_PLUGIN_LDFLAGS)
37
diff --git a/src/plugin/block/meson.build b/src/plugin/block/meson.build
new file mode 100644
index 000000000..480a9f4ca
--- /dev/null
+++ b/src/plugin/block/meson.build
@@ -0,0 +1,15 @@
1plugin_dep = [libgnunetutil_dep, libgnunetblock_dep, libgnunetblockgroup_dep]
2shared_module('gnunet_plugin_block_test',
3 ['plugin_block_test.c'],
4 dependencies: plugin_dep,
5 include_directories: [incdir, configuration_inc],
6 install: true,
7 install_dir: get_option('libdir')/'gnunet')
8
9shared_module('gnunet_plugin_block_template',
10 ['plugin_block_template.c'],
11 dependencies: plugin_dep,
12 include_directories: [incdir, configuration_inc],
13 install: false,
14 install_dir: get_option('libdir')/'gnunet')
15
diff --git a/src/plugin/block/plugin_block_template.c b/src/plugin/block/plugin_block_template.c
new file mode 100644
index 000000000..4a271fa42
--- /dev/null
+++ b/src/plugin/block/plugin_block_template.c
@@ -0,0 +1,222 @@
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 */
57static struct GNUNET_BLOCK_Group *
58block_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 */
101static enum GNUNET_GenericReturnValue
102block_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 */
121static enum GNUNET_GenericReturnValue
122block_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 */
147static enum GNUNET_BLOCK_ReplyEvaluationResult
148block_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 */
173static enum GNUNET_GenericReturnValue
174block_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 */
189void *
190libgnunet_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 */
212void *
213libgnunet_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/plugin/block/plugin_block_test.c b/src/plugin/block/plugin_block_test.c
new file mode 100644
index 000000000..5afac9205
--- /dev/null
+++ b/src/plugin/block/plugin_block_test.c
@@ -0,0 +1,270 @@
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 */
55static struct GNUNET_BLOCK_Group *
56block_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 */
99static enum GNUNET_GenericReturnValue
100block_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 */
132static enum GNUNET_GenericReturnValue
133block_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 */
166static enum GNUNET_BLOCK_ReplyEvaluationResult
167block_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 */
208static enum GNUNET_GenericReturnValue
209block_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 */
234void *
235libgnunet_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 */
260void *
261libgnunet_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 */