aboutsummaryrefslogtreecommitdiff
path: root/src/service
diff options
context:
space:
mode:
Diffstat (limited to 'src/service')
-rw-r--r--src/service/dns/Makefile.am14
-rw-r--r--src/service/dns/meson.build10
-rw-r--r--src/service/dns/plugin_block_dns.c290
-rw-r--r--src/service/meson.build1
-rw-r--r--src/service/regex/Makefile.am40
-rw-r--r--src/service/regex/meson.build24
-rw-r--r--src/service/regex/plugin_block_regex.c380
-rw-r--r--src/service/regex/regex_block_lib.c431
-rw-r--r--src/service/regex/regex_block_lib.h193
-rw-r--r--src/service/regex/regex_internal_dht.c1
-rw-r--r--src/service/regex/regex_internal_lib.h2
11 files changed, 11 insertions, 1375 deletions
diff --git a/src/service/dns/Makefile.am b/src/service/dns/Makefile.am
index 38ee78c3a..0d07b9e5e 100644
--- a/src/service/dns/Makefile.am
+++ b/src/service/dns/Makefile.am
@@ -29,9 +29,6 @@ noinst_PROGRAMS = \
29 gnunet-dns-redirector \ 29 gnunet-dns-redirector \
30 gnunet-zonewalk 30 gnunet-zonewalk
31 31
32plugin_LTLIBRARIES = \
33 libgnunet_plugin_block_dns.la
34
35if LINUX 32if LINUX
36check_SCRIPTS = \ 33check_SCRIPTS = \
37 test_gnunet_dns.sh 34 test_gnunet_dns.sh
@@ -77,17 +74,6 @@ libgnunetdns_la_LDFLAGS = \
77 $(GN_LIB_LDFLAGS) \ 74 $(GN_LIB_LDFLAGS) \
78 -version-info 0:0:0 75 -version-info 0:0:0
79 76
80libgnunet_plugin_block_dns_la_SOURCES = \
81 plugin_block_dns.c
82libgnunet_plugin_block_dns_la_LIBADD = \
83 $(top_builddir)/src/lib/block/libgnunetblockgroup.la \
84 $(top_builddir)/src/lib/block/libgnunetblock.la \
85 $(top_builddir)/src/lib/util/libgnunetutil.la
86libgnunet_plugin_block_dns_la_LDFLAGS = \
87 $(GN_LIBINTL) \
88 $(top_builddir)/src/block/$(GN_PLUGIN_LDFLAGS)
89
90
91if ENABLE_TEST_RUN 77if ENABLE_TEST_RUN
92AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; 78AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME;
93TESTS = $(check_PROGRAMS) $(check_SCRIPTS) 79TESTS = $(check_PROGRAMS) $(check_SCRIPTS)
diff --git a/src/service/dns/meson.build b/src/service/dns/meson.build
index fe0f6e954..e1b7f7670 100644
--- a/src/service/dns/meson.build
+++ b/src/service/dns/meson.build
@@ -1,5 +1,4 @@
1libgnunetdns_src = ['dns_api.c'] 1libgnunetdns_src = ['dns_api.c']
2libgnunetpluginblockdns_src = ['plugin_block_dns.c']
3 2
4gnunetservicedns_src = ['gnunet-service-dns.c'] 3gnunetservicedns_src = ['gnunet-service-dns.c']
5gnunetdnsredirector_src = ['gnunet-dns-monitor.c'] 4gnunetdnsredirector_src = ['gnunet-dns-monitor.c']
@@ -12,7 +11,7 @@ configure_file(input : 'dns.conf.in',
12 install_dir: pkgcfgdir) 11 install_dir: pkgcfgdir)
13 12
14if get_option('monolith') 13if get_option('monolith')
15 foreach p : libgnunetdns_src + gnunetservicedns_src + libgnunetpluginblockdns_src 14 foreach p : libgnunetdns_src + gnunetservicedns_src
16 gnunet_src += 'dns/' + p 15 gnunet_src += 'dns/' + p
17 endforeach 16 endforeach
18endif 17endif
@@ -29,13 +28,6 @@ libgnunetdns_dep = declare_dependency(link_with : libgnunetdns)
29pkg.generate(libgnunetdns, url: 'https://www.gnunet.org', 28pkg.generate(libgnunetdns, url: 'https://www.gnunet.org',
30 description : 'Provides API to access GNUnet\'s DNS service (to intercept and manipulate DNS queries)') 29 description : 'Provides API to access GNUnet\'s DNS service (to intercept and manipulate DNS queries)')
31 30
32shared_module('gnunet_plugin_block_dns',
33 libgnunetpluginblockdns_src,
34 dependencies: [libgnunetutil_dep, libgnunetblockgroup_dep],
35 include_directories: [incdir, configuration_inc],
36 install: true,
37 install_dir: get_option('libdir')/'gnunet')
38
39executable ('gnunet-service-dns', 31executable ('gnunet-service-dns',
40 gnunetservicedns_src, 32 gnunetservicedns_src,
41 dependencies: [libgnunetdns_dep, libgnunetutil_dep, 33 dependencies: [libgnunetdns_dep, libgnunetutil_dep,
diff --git a/src/service/dns/plugin_block_dns.c b/src/service/dns/plugin_block_dns.c
deleted file mode 100644
index 1bbd7f750..000000000
--- a/src/service/dns/plugin_block_dns.c
+++ /dev/null
@@ -1,290 +0,0 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2013, 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/**
22 * @file dns/plugin_block_dns.c
23 * @brief block plugin for advertising a DNS exit service
24 * @author Christian Grothoff
25 *
26 * Note that this plugin might more belong with EXIT and PT
27 * as those two are using this type of block. Still, this
28 * might be a natural enough place for people to find the code...
29 */
30#include "platform.h"
31#include "gnunet_block_plugin.h"
32#include "block_dns.h"
33#include "gnunet_signatures.h"
34#include "gnunet_block_group_lib.h"
35
36
37/**
38 * Number of bits we set per entry in the bloomfilter.
39 * Do not change!
40 */
41#define BLOOMFILTER_K 16
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_dns_create_group (void *cls,
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 = 8;
78 }
79 GNUNET_break (NULL == va_arg (va, const char *));
80 return GNUNET_BLOCK_GROUP_bf_create (cls,
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 extrended 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
98 */
99static enum GNUNET_GenericReturnValue
100block_plugin_dns_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 switch (type)
107 {
108 case GNUNET_BLOCK_TYPE_DNS:
109 if (0 != xquery_size)
110 {
111 GNUNET_break_op (0);
112 return GNUNET_NO;
113 }
114 return GNUNET_OK;
115 default:
116 GNUNET_break (0);
117 return GNUNET_SYSERR;
118 }
119}
120
121
122/**
123 * Function called to validate a block for storage.
124 *
125 * @param cls closure
126 * @param type block type
127 * @param block block data to validate
128 * @param block_size number of bytes in @a block
129 * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not
130 */
131static enum GNUNET_GenericReturnValue
132block_plugin_dns_check_block (void *cls,
133 enum GNUNET_BLOCK_Type type,
134 const void *block,
135 size_t block_size)
136{
137 const struct GNUNET_DNS_Advertisement *ad;
138
139 switch (type)
140 {
141 case GNUNET_BLOCK_TYPE_DNS:
142 if (sizeof(struct GNUNET_DNS_Advertisement) != block_size)
143 {
144 GNUNET_break_op (0);
145 return GNUNET_NO;
146 }
147 ad = block;
148
149 if (ntohl (ad->purpose.size) !=
150 sizeof(struct GNUNET_DNS_Advertisement)
151 - sizeof(struct GNUNET_CRYPTO_EddsaSignature))
152 {
153 GNUNET_break_op (0);
154 return GNUNET_NO;
155 }
156 if (GNUNET_TIME_absolute_is_past (
157 GNUNET_TIME_absolute_ntoh (ad->expiration_time)))
158 {
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "DNS advertisement has expired\n");
161 return GNUNET_NO;
162 }
163 if (GNUNET_OK !=
164 GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_DNS_RECORD,
165 &ad->purpose,
166 &ad->signature,
167 &ad->peer.public_key))
168 {
169 GNUNET_break_op (0);
170 return GNUNET_NO;
171 }
172 return GNUNET_OK;
173 default:
174 GNUNET_break (0);
175 return GNUNET_SYSERR;
176 }
177}
178
179
180/**
181 * Function called to validate a reply to a request. Note that it is assumed
182 * that the reply has already been matched to the key (and signatures checked)
183 * as it would be done with the GetKeyFunction and the
184 * BlockEvaluationFunction.
185 *
186 * @param cls closure
187 * @param type block type
188 * @param group which block group to use for evaluation
189 * @param query original query (hash)
190 * @param xquery extrended query data (can be NULL, depending on type)
191 * @param xquery_size number of bytes in @a xquery
192 * @param reply_block response to validate
193 * @param reply_block_size number of bytes in @a reply_block
194 * @return characterization of result
195 */
196static enum GNUNET_BLOCK_ReplyEvaluationResult
197block_plugin_dns_check_reply (
198 void *cls,
199 enum GNUNET_BLOCK_Type type,
200 struct GNUNET_BLOCK_Group *group,
201 const struct GNUNET_HashCode *query,
202 const void *xquery,
203 size_t xquery_size,
204 const void *reply_block,
205 size_t reply_block_size)
206{
207 struct GNUNET_HashCode phash;
208
209 switch (type)
210 {
211 case GNUNET_BLOCK_TYPE_DNS:
212 GNUNET_CRYPTO_hash (reply_block,
213 reply_block_size,
214 &phash);
215 if (GNUNET_YES ==
216 GNUNET_BLOCK_GROUP_bf_test_and_set (group,
217 &phash))
218 return GNUNET_BLOCK_REPLY_OK_DUPLICATE;
219 return GNUNET_BLOCK_REPLY_OK_MORE;
220 default:
221 GNUNET_break (0);
222 return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED;
223 }
224}
225
226
227/**
228 * Function called to obtain the key for a block.
229 *
230 * @param cls closure
231 * @param type block type
232 * @param block block to get the key for
233 * @param block_size number of bytes in @a block
234 * @param key set to the key (query) for the given block
235 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
236 * (or if extracting a key from a block of this type does not work)
237 */
238static enum GNUNET_GenericReturnValue
239block_plugin_dns_get_key (void *cls,
240 enum GNUNET_BLOCK_Type type,
241 const void *block,
242 size_t block_size,
243 struct GNUNET_HashCode *key)
244{
245 if (GNUNET_BLOCK_TYPE_DNS != type)
246 {
247 GNUNET_break (0);
248 return GNUNET_SYSERR;
249 }
250 return GNUNET_NO;
251}
252
253
254/**
255 * Entry point for the plugin.
256 */
257void *
258libgnunet_plugin_block_dns_init (void *cls)
259{
260 static enum GNUNET_BLOCK_Type types[] = {
261 GNUNET_BLOCK_TYPE_DNS,
262 GNUNET_BLOCK_TYPE_ANY /* end of list */
263 };
264 struct GNUNET_BLOCK_PluginFunctions *api;
265
266 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
267 api->get_key = &block_plugin_dns_get_key;
268 api->check_query = &block_plugin_dns_check_query;
269 api->check_block = &block_plugin_dns_check_block;
270 api->check_reply = &block_plugin_dns_check_reply;
271 api->create_group = &block_plugin_dns_create_group;
272 api->types = types;
273 return api;
274}
275
276
277/**
278 * Exit point from the plugin.
279 */
280void *
281libgnunet_plugin_block_dns_done (void *cls)
282{
283 struct GNUNET_BLOCK_PluginFunctions *api = cls;
284
285 GNUNET_free (api);
286 return NULL;
287}
288
289
290/* end of plugin_block_dns.c */
diff --git a/src/service/meson.build b/src/service/meson.build
index 34dd997e2..b089c9794 100644
--- a/src/service/meson.build
+++ b/src/service/meson.build
@@ -43,6 +43,7 @@ libgnunet = library('gnunet',
43 libgnunetjson_dep, 43 libgnunetjson_dep,
44 libgnunetblock_dep, 44 libgnunetblock_dep,
45 libgnunetblockgroup_dep, 45 libgnunetblockgroup_dep,
46 libgnunetregexblock_dep,
46 libgnunetgnsrecord_dep, 47 libgnunetgnsrecord_dep,
47 libgnunetgnsrecordjson_dep, 48 libgnunetgnsrecordjson_dep,
48 jose_dep, 49 jose_dep,
diff --git a/src/service/regex/Makefile.am b/src/service/regex/Makefile.am
index 14cdb054b..255988889 100644
--- a/src/service/regex/Makefile.am
+++ b/src/service/regex/Makefile.am
@@ -31,27 +31,15 @@ REGEX_INTERNAL_TEST = \
31gnunet_service_regex_SOURCES = \ 31gnunet_service_regex_SOURCES = \
32 $(REGEX_INTERNAL) gnunet-service-regex.c 32 $(REGEX_INTERNAL) gnunet-service-regex.c
33gnunet_service_regex_LDADD = -lm \ 33gnunet_service_regex_LDADD = -lm \
34 libgnunetregexblock.la \ 34 $(top_builddir)/src/plugin/regex/libgnunetregexblock.la \
35 $(top_builddir)/src/service/dht/libgnunetdht.la \ 35 $(top_builddir)/src/service/dht/libgnunetdht.la \
36 $(top_builddir)/src/service/statistics/libgnunetstatistics.la \ 36 $(top_builddir)/src/service/statistics/libgnunetstatistics.la \
37 $(top_builddir)/src/lib/util/libgnunetutil.la \ 37 $(top_builddir)/src/lib/util/libgnunetutil.la \
38 $(GN_LIBINTL) 38 $(GN_LIBINTL)
39 39
40lib_LTLIBRARIES = \ 40lib_LTLIBRARIES = \
41 libgnunetregexblock.la \
42 libgnunetregex.la 41 libgnunetregex.la
43 42
44libgnunetregexblock_la_SOURCES = \
45 regex_block_lib.c regex_block_lib.h
46libgnunetregexblock_la_LIBADD = \
47 $(top_builddir)/src/lib/util/libgnunetutil.la \
48 $(XLIB) \
49 $(LTLIBINTL)
50libgnunetregexblock_la_LDFLAGS = \
51 $(GN_LIB_LDFLAGS) \
52 -version-info 1:0:0
53
54
55libgnunetregex_la_SOURCES = \ 43libgnunetregex_la_SOURCES = \
56 regex_api_announce.c \ 44 regex_api_announce.c \
57 regex_api_search.c \ 45 regex_api_search.c \
@@ -64,20 +52,6 @@ libgnunetregex_la_LDFLAGS = \
64 -version-info 3:1:0 52 -version-info 3:1:0
65 53
66 54
67plugin_LTLIBRARIES = \
68 libgnunet_plugin_block_regex.la
69
70libgnunet_plugin_block_regex_la_SOURCES = \
71 plugin_block_regex.c
72libgnunet_plugin_block_regex_la_LIBADD = \
73 libgnunetregexblock.la \
74 $(top_builddir)/src/lib/block/libgnunetblock.la \
75 $(top_builddir)/src/lib/block/libgnunetblockgroup.la \
76 $(top_builddir)/src/lib/util/libgnunetutil.la
77libgnunet_plugin_block_regex_la_LDFLAGS = \
78 $(GN_LIBINTL) \
79 $(GN_PLUGIN_LDFLAGS)
80
81# FIXME we phased out mysql. If we want to keep, needs rewrite 55# FIXME we phased out mysql. If we want to keep, needs rewrite
82#if HAVE_MYSQL 56#if HAVE_MYSQL
83#noinst_mysql_progs = \ 57#noinst_mysql_progs = \
@@ -100,7 +74,7 @@ perf_regex_SOURCES = \
100 $(REGEX_INTERNAL_TEST) perf-regex.c 74 $(REGEX_INTERNAL_TEST) perf-regex.c
101perf_regex_LDADD = -lm \ 75perf_regex_LDADD = -lm \
102 $(top_builddir)/src/service/dht/libgnunetdht.la \ 76 $(top_builddir)/src/service/dht/libgnunetdht.la \
103 libgnunetregexblock.la \ 77 $(top_builddir)/src/plugin/regex/libgnunetregexblock.la \
104 $(top_builddir)/src/lib/util/libgnunetutil.la \ 78 $(top_builddir)/src/lib/util/libgnunetutil.la \
105 $(top_builddir)/src/service/statistics/libgnunetstatistics.la 79 $(top_builddir)/src/service/statistics/libgnunetstatistics.la
106perf_regex_LDFLAGS = \ 80perf_regex_LDFLAGS = \
@@ -110,7 +84,7 @@ gnunet_daemon_regexprofiler_SOURCES = \
110 $(REGEX_INTERNAL_TEST) gnunet-daemon-regexprofiler.c 84 $(REGEX_INTERNAL_TEST) gnunet-daemon-regexprofiler.c
111gnunet_daemon_regexprofiler_LDADD = -lm \ 85gnunet_daemon_regexprofiler_LDADD = -lm \
112 $(top_builddir)/src/service/dht/libgnunetdht.la \ 86 $(top_builddir)/src/service/dht/libgnunetdht.la \
113 libgnunetregexblock.la \ 87 $(top_builddir)/src/plugin/regex/libgnunetregexblock.la \
114 $(top_builddir)/src/service/statistics/libgnunetstatistics.la \ 88 $(top_builddir)/src/service/statistics/libgnunetstatistics.la \
115 $(top_builddir)/src/lib/util/libgnunetutil.la 89 $(top_builddir)/src/lib/util/libgnunetutil.la
116gnunet_daemon_regexprofiler_LDFLAGS = \ 90gnunet_daemon_regexprofiler_LDFLAGS = \
@@ -133,7 +107,7 @@ test_regex_eval_api_SOURCES = \
133 $(REGEX_INTERNAL_TEST) test_regex_eval_api.c 107 $(REGEX_INTERNAL_TEST) test_regex_eval_api.c
134test_regex_eval_api_LDADD = -lm \ 108test_regex_eval_api_LDADD = -lm \
135 $(top_builddir)/src/service/dht/libgnunetdht.la \ 109 $(top_builddir)/src/service/dht/libgnunetdht.la \
136 libgnunetregexblock.la \ 110 $(top_builddir)/src/plugin/regex/libgnunetregexblock.la \
137 $(top_builddir)/src/service/statistics/libgnunetstatistics.la \ 111 $(top_builddir)/src/service/statistics/libgnunetstatistics.la \
138 $(top_builddir)/src/lib/util/libgnunetutil.la 112 $(top_builddir)/src/lib/util/libgnunetutil.la
139 113
@@ -154,7 +128,7 @@ test_regex_api_LDADD = -lm \
154test_regex_iterate_api_SOURCES = \ 128test_regex_iterate_api_SOURCES = \
155 $(REGEX_INTERNAL) test_regex_iterate_api.c 129 $(REGEX_INTERNAL) test_regex_iterate_api.c
156test_regex_iterate_api_LDADD = -lm \ 130test_regex_iterate_api_LDADD = -lm \
157 libgnunetregexblock.la \ 131 $(top_builddir)/src/plugin/regex/libgnunetregexblock.la \
158 $(top_builddir)/src/service/dht/libgnunetdht.la \ 132 $(top_builddir)/src/service/dht/libgnunetdht.la \
159 $(top_builddir)/src/service/statistics/libgnunetstatistics.la \ 133 $(top_builddir)/src/service/statistics/libgnunetstatistics.la \
160 $(top_builddir)/src/lib/util/libgnunetutil.la 134 $(top_builddir)/src/lib/util/libgnunetutil.la
@@ -163,7 +137,7 @@ test_regex_proofs_SOURCES = \
163 $(REGEX_INTERNAL_TEST) test_regex_proofs.c 137 $(REGEX_INTERNAL_TEST) test_regex_proofs.c
164test_regex_proofs_LDADD = -lm \ 138test_regex_proofs_LDADD = -lm \
165 $(top_builddir)/src/service/dht/libgnunetdht.la \ 139 $(top_builddir)/src/service/dht/libgnunetdht.la \
166 libgnunetregexblock.la \ 140 $(top_builddir)/src/plugin/regex/libgnunetregexblock.la \
167 $(top_builddir)/src/service/statistics/libgnunetstatistics.la \ 141 $(top_builddir)/src/service/statistics/libgnunetstatistics.la \
168 $(top_builddir)/src/lib/util/libgnunetutil.la 142 $(top_builddir)/src/lib/util/libgnunetutil.la
169 143
@@ -171,7 +145,7 @@ test_regex_graph_api_SOURCES = \
171 $(REGEX_INTERNAL_TEST) test_regex_graph_api.c 145 $(REGEX_INTERNAL_TEST) test_regex_graph_api.c
172test_regex_graph_api_LDADD = -lm \ 146test_regex_graph_api_LDADD = -lm \
173 $(top_builddir)/src/service/dht/libgnunetdht.la \ 147 $(top_builddir)/src/service/dht/libgnunetdht.la \
174 libgnunetregexblock.la \ 148 $(top_builddir)/src/plugin/regex/libgnunetregexblock.la \
175 $(top_builddir)/src/service/statistics/libgnunetstatistics.la \ 149 $(top_builddir)/src/service/statistics/libgnunetstatistics.la \
176 $(top_builddir)/src/lib/util/libgnunetutil.la 150 $(top_builddir)/src/lib/util/libgnunetutil.la
177 151
diff --git a/src/service/regex/meson.build b/src/service/regex/meson.build
index da607ca47..3c25a5148 100644
--- a/src/service/regex/meson.build
+++ b/src/service/regex/meson.build
@@ -1,7 +1,5 @@
1libgnunetregex_src = ['regex_api_announce.c', 1libgnunetregex_src = ['regex_api_announce.c',
2 'regex_api_search.c'] 2 'regex_api_search.c']
3libgnunetregexblock_src = ['regex_block_lib.c']
4
5gnunetserviceregex_src = ['gnunet-service-regex.c', 3gnunetserviceregex_src = ['gnunet-service-regex.c',
6 'regex_internal_dht.c', 4 'regex_internal_dht.c',
7 'regex_internal.c'] 5 'regex_internal.c']
@@ -14,7 +12,7 @@ configure_file(input : 'regex.conf.in',
14 12
15 13
16if get_option('monolith') 14if get_option('monolith')
17 foreach p : libgnunetregex_src + libgnunetregexblock_src + gnunetserviceregex_src 15 foreach p : libgnunetregex_src + gnunetserviceregex_src
18 gnunet_src += 'regex/' + p 16 gnunet_src += 'regex/' + p
19 endforeach 17 endforeach
20endif 18endif
@@ -30,26 +28,6 @@ libgnunetregex = library('gnunetregex',
30pkg.generate(libgnunetregex, url: 'https://www.gnunet.org', 28pkg.generate(libgnunetregex, url: 'https://www.gnunet.org',
31 description : 'Provides API for accessing the regex service') 29 description : 'Provides API for accessing the regex service')
32libgnunetregex_dep = declare_dependency(link_with : libgnunetregex) 30libgnunetregex_dep = declare_dependency(link_with : libgnunetregex)
33libgnunetregexblock = library('gnunetregexblock',
34 libgnunetregexblock_src,
35 soversion: '1',
36 version: '1.0.0',
37 dependencies: libgnunetutil_dep,
38 include_directories: [incdir, configuration_inc],
39 install: true,
40 install_dir: get_option('libdir'))
41libgnunetregexblock_dep = declare_dependency(link_with : libgnunetregexblock)
42
43shared_module('gnunet_plugin_block_regex',
44 ['plugin_block_regex.c'],
45 dependencies: [libgnunetutil_dep,
46 libgnunetregexblock_dep,
47 libgnunetblock_dep,
48 libgnunetblockgroup_dep],
49 include_directories: [incdir, configuration_inc],
50 install:true,
51 install_dir: get_option('libdir')/'gnunet')
52
53 31
54executable ('gnunet-service-regex', 32executable ('gnunet-service-regex',
55 gnunetserviceregex_src, 33 gnunetserviceregex_src,
diff --git a/src/service/regex/plugin_block_regex.c b/src/service/regex/plugin_block_regex.c
deleted file mode 100644
index 5f23a32df..000000000
--- a/src/service/regex/plugin_block_regex.c
+++ /dev/null
@@ -1,380 +0,0 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2013 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 regex/plugin_block_regex.c
23 * @brief blocks used for regex storage and search
24 * @author Bartlomiej Polot
25 */
26#include "platform.h"
27#include "gnunet_block_plugin.h"
28#include "gnunet_block_group_lib.h"
29#include "block_regex.h"
30#include "regex_block_lib.h"
31#include "gnunet_signatures.h"
32
33
34/**
35 * Number of bits we set per entry in the bloomfilter.
36 * Do not change!
37 */
38#define BLOOMFILTER_K 16
39
40
41/**
42 * How big is the BF we use for REGEX blocks?
43 */
44#define REGEX_BF_SIZE 8
45
46
47/**
48 * Create a new block group.
49 *
50 * @param ctx block context in which the block group is created
51 * @param type type of the block for which we are creating the group
52 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
53 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
54 * @param va variable arguments specific to @a type
55 * @return block group handle, NULL if block groups are not supported
56 * by this @a type of block (this is not an error)
57 */
58static struct GNUNET_BLOCK_Group *
59block_plugin_regex_create_group (void *cls,
60 enum GNUNET_BLOCK_Type type,
61 const void *raw_data,
62 size_t raw_data_size,
63 va_list va)
64{
65 unsigned int bf_size;
66 const char *guard;
67
68 guard = va_arg (va, const char *);
69 if (0 == strcmp (guard,
70 "seen-set-size"))
71 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned
72 int),
73 BLOOMFILTER_K);
74 else if (0 == strcmp (guard,
75 "filter-size"))
76 bf_size = va_arg (va, unsigned int);
77 else
78 {
79 GNUNET_break (0);
80 bf_size = REGEX_BF_SIZE;
81 }
82 GNUNET_break (NULL == va_arg (va, const char *));
83 return GNUNET_BLOCK_GROUP_bf_create (cls,
84 bf_size,
85 BLOOMFILTER_K,
86 type,
87 raw_data,
88 raw_data_size);
89}
90
91
92/**
93 * Function called to validate a query.
94 *
95 * @param cls closure
96 * @param ctx block context
97 * @param type block type
98 * @param query original query (hash)
99 * @param xquery extrended query data (can be NULL, depending on type)
100 * @param xquery_size number of bytes in @a xquery
101 * @return #GNUNET_OK if the query is fine, #GNUNET_NO if not
102 */
103static enum GNUNET_GenericReturnValue
104block_plugin_regex_check_query (void *cls,
105 enum GNUNET_BLOCK_Type type,
106 const struct GNUNET_HashCode *query,
107 const void *xquery,
108 size_t xquery_size)
109{
110 switch (type)
111 {
112 case GNUNET_BLOCK_TYPE_REGEX:
113 if (0 != xquery_size)
114 {
115 const char *s;
116
117 s = (const char *) xquery;
118 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
119 {
120 GNUNET_break_op (0);
121 return GNUNET_NO;
122 }
123 }
124 return GNUNET_OK;
125 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
126 if (0 != xquery_size)
127 {
128 GNUNET_break_op (0);
129 return GNUNET_NO;
130 }
131 return GNUNET_OK;
132 default:
133 GNUNET_break (0);
134 return GNUNET_SYSERR;
135 }
136}
137
138
139/**
140 * Function called to validate a block for storage.
141 *
142 * @param cls closure
143 * @param type block type
144 * @param block block data to validate
145 * @param block_size number of bytes in @a block
146 * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not
147 */
148static enum GNUNET_GenericReturnValue
149block_plugin_regex_check_block (void *cls,
150 enum GNUNET_BLOCK_Type type,
151 const void *block,
152 size_t block_size)
153{
154 switch (type)
155 {
156 case GNUNET_BLOCK_TYPE_REGEX:
157 if (GNUNET_SYSERR ==
158 REGEX_BLOCK_check (block,
159 block_size,
160 NULL,
161 NULL))
162 return GNUNET_NO;
163 return GNUNET_OK;
164 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
165 {
166 const struct RegexAcceptBlock *rba;
167
168 if (sizeof(struct RegexAcceptBlock) != block_size)
169 {
170 GNUNET_break_op (0);
171 return GNUNET_NO;
172 }
173 rba = block;
174 if (ntohl (rba->purpose.size) !=
175 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
176 + sizeof(struct GNUNET_TIME_AbsoluteNBO)
177 + sizeof(struct GNUNET_HashCode))
178 {
179 GNUNET_break_op (0);
180 return GNUNET_NO;
181 }
182 if (GNUNET_TIME_absolute_is_past (GNUNET_TIME_absolute_ntoh (
183 rba->expiration_time)))
184 {
185 return GNUNET_NO;
186 }
187 if (GNUNET_OK !=
188 GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
189 &rba->purpose,
190 &rba->signature,
191 &rba->peer.public_key))
192 {
193 GNUNET_break_op (0);
194 return GNUNET_NO;
195 }
196 return GNUNET_OK;
197 }
198 default:
199 GNUNET_break (0);
200 return GNUNET_SYSERR;
201 }
202}
203
204
205/**
206 * Function called to validate a reply to a request. Note that it is assumed
207 * that the reply has already been matched to the key (and signatures checked)
208 * as it would be done with the GetKeyFunction and the
209 * BlockEvaluationFunction.
210 *
211 * @param cls closure
212 * @param type block type
213 * @param group which block group to use for evaluation
214 * @param query original query (hash)
215 * @param xquery extrended query data (can be NULL, depending on type)
216 * @param xquery_size number of bytes in @a xquery
217 * @param reply_block response to validate
218 * @param reply_block_size number of bytes in @a reply_block
219 * @return characterization of result
220 */
221static enum GNUNET_BLOCK_ReplyEvaluationResult
222block_plugin_regex_check_reply (
223 void *cls,
224 enum GNUNET_BLOCK_Type type,
225 struct GNUNET_BLOCK_Group *group,
226 const struct GNUNET_HashCode *query,
227 const void *xquery,
228 size_t xquery_size,
229 const void *reply_block,
230 size_t reply_block_size)
231{
232 struct GNUNET_HashCode chash;
233
234 switch (type)
235 {
236 case GNUNET_BLOCK_TYPE_REGEX:
237 if (0 != xquery_size)
238 {
239 const char *s;
240
241 s = (const char *) xquery;
242 GNUNET_assert ('\0' == s[xquery_size - 1]);
243 }
244 switch (REGEX_BLOCK_check (reply_block,
245 reply_block_size,
246 query,
247 xquery))
248 {
249 case GNUNET_SYSERR:
250 GNUNET_assert (0);
251 case GNUNET_NO:
252 /* xquery mismatch, can happen */
253 return GNUNET_BLOCK_REPLY_IRRELEVANT;
254 default:
255 break;
256 }
257 GNUNET_CRYPTO_hash (reply_block,
258 reply_block_size,
259 &chash);
260 if (GNUNET_YES ==
261 GNUNET_BLOCK_GROUP_bf_test_and_set (group,
262 &chash))
263 return GNUNET_BLOCK_REPLY_OK_DUPLICATE;
264 return GNUNET_BLOCK_REPLY_OK_MORE;
265 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
266 {
267 const struct RegexAcceptBlock *rba;
268
269 GNUNET_assert (sizeof(struct RegexAcceptBlock) == reply_block_size);
270 rba = reply_block;
271 GNUNET_assert (ntohl (rba->purpose.size) ==
272 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
273 + sizeof(struct GNUNET_TIME_AbsoluteNBO)
274 + sizeof(struct GNUNET_HashCode));
275 GNUNET_CRYPTO_hash (reply_block,
276 reply_block_size,
277 &chash);
278 if (GNUNET_YES ==
279 GNUNET_BLOCK_GROUP_bf_test_and_set (group,
280 &chash))
281 return GNUNET_BLOCK_REPLY_OK_DUPLICATE;
282 return GNUNET_BLOCK_REPLY_OK_MORE;
283 }
284 default:
285 GNUNET_break (0);
286 return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED;
287 }
288 return GNUNET_BLOCK_REPLY_OK_MORE;
289}
290
291
292/**
293 * Function called to obtain the key for a block.
294 *
295 * @param cls closure
296 * @param type block type
297 * @param block block to get the key for
298 * @param block_size number of bytes in @a block
299 * @param key set to the key (query) for the given block
300 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported,
301 * #GNUNET_NO if extracting a key from a block of this type does not work
302 */
303static enum GNUNET_GenericReturnValue
304block_plugin_regex_get_key (void *cls,
305 enum GNUNET_BLOCK_Type type,
306 const void *block,
307 size_t block_size,
308 struct GNUNET_HashCode *key)
309{
310 switch (type)
311 {
312 case GNUNET_BLOCK_TYPE_REGEX:
313 if (GNUNET_OK !=
314 REGEX_BLOCK_get_key (block,
315 block_size,
316 key))
317 {
318 GNUNET_break_op (0);
319 memset (key,
320 0,
321 sizeof (*key));
322 return GNUNET_OK;
323 }
324 return GNUNET_OK;
325 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
326 if (sizeof(struct RegexAcceptBlock) != block_size)
327 {
328 GNUNET_break_op (0);
329 memset (key,
330 0,
331 sizeof (*key));
332 return GNUNET_OK;
333 }
334 *key = ((struct RegexAcceptBlock *) block)->key;
335 return GNUNET_OK;
336 default:
337 GNUNET_break (0);
338 return GNUNET_SYSERR;
339 }
340}
341
342
343/**
344 * Entry point for the plugin.
345 */
346void *
347libgnunet_plugin_block_regex_init (void *cls)
348{
349 static const enum GNUNET_BLOCK_Type types[] = {
350 GNUNET_BLOCK_TYPE_REGEX,
351 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
352 GNUNET_BLOCK_TYPE_ANY /* end of list */
353 };
354 struct GNUNET_BLOCK_PluginFunctions *api;
355
356 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
357 api->get_key = &block_plugin_regex_get_key;
358 api->check_query = &block_plugin_regex_check_query;
359 api->check_block = &block_plugin_regex_check_block;
360 api->check_reply = &block_plugin_regex_check_reply;
361 api->create_group = &block_plugin_regex_create_group;
362 api->types = types;
363 return api;
364}
365
366
367/**
368 * Exit point from the plugin.
369 */
370void *
371libgnunet_plugin_block_regex_done (void *cls)
372{
373 struct GNUNET_BLOCK_PluginFunctions *api = cls;
374
375 GNUNET_free (api);
376 return NULL;
377}
378
379
380/* end of plugin_block_regex.c */
diff --git a/src/service/regex/regex_block_lib.c b/src/service/regex/regex_block_lib.c
deleted file mode 100644
index 048d6d743..000000000
--- a/src/service/regex/regex_block_lib.c
+++ /dev/null
@@ -1,431 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012,2013 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 * @author Bartlomiej Polot
22 * @file regex/regex_block_lib.c
23 * @brief functions for manipulating non-accept blocks stored for
24 * regex in the DHT
25 */
26#include "platform.h"
27#include "regex_block_lib.h"
28#include "gnunet_constants.h"
29
30#define LOG(kind, ...) GNUNET_log_from (kind, "regex-bck", __VA_ARGS__)
31
32GNUNET_NETWORK_STRUCT_BEGIN
33
34/**
35 * Information for each edge.
36 */
37struct EdgeInfo
38{
39 /**
40 * Index of the destination of this edge in the
41 * unique destinations array.
42 */
43 uint16_t destination_index GNUNET_PACKED;
44
45 /**
46 * Number of bytes the token for this edge takes in the
47 * token area.
48 */
49 uint16_t token_length GNUNET_PACKED;
50};
51
52
53/**
54 * @brief Block to announce a regex state.
55 */
56struct RegexBlock
57{
58 /**
59 * Length of the proof regex string.
60 */
61 uint16_t proof_len GNUNET_PACKED;
62
63 /**
64 * Is this state an accepting state?
65 */
66 int16_t is_accepting GNUNET_PACKED;
67
68 /**
69 * Number of edges parting from this state.
70 */
71 uint16_t num_edges GNUNET_PACKED;
72
73 /**
74 * Number of unique destinations reachable from this state.
75 */
76 uint16_t num_destinations GNUNET_PACKED;
77
78 /* followed by 'struct GNUNET_HashCode[num_destinations]' */
79
80 /* followed by 'struct EdgeInfo[edge_destination_indices]' */
81
82 /* followed by 'char proof[n_proof]', NOT 0-terminated */
83
84 /* followed by 'char tokens[num_edges][edge_info[k].token_length]';
85 essentially all of the tokens one after the other in the
86 order of the edges; tokens are NOT 0-terminated */
87};
88
89
90GNUNET_NETWORK_STRUCT_END
91
92
93/**
94 * Test if this block is marked as being an accept state.
95 *
96 * @param block block to test
97 * @param size number of bytes in block
98 * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not
99 */
100int
101GNUNET_BLOCK_is_accepting (const struct RegexBlock *block,
102 size_t size)
103{
104 if (size < sizeof(struct RegexBlock))
105 {
106 GNUNET_break_op (0);
107 return GNUNET_SYSERR;
108 }
109 return ntohs (block->is_accepting);
110}
111
112
113int
114REGEX_BLOCK_check_proof (const char *proof,
115 size_t proof_len,
116 const struct GNUNET_HashCode *key)
117{
118 struct GNUNET_HashCode key_check;
119
120 if ((NULL == proof) || (NULL == key))
121 {
122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
123 return GNUNET_NO;
124 }
125 GNUNET_CRYPTO_hash (proof, proof_len, &key_check);
126 return (0 ==
127 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
128}
129
130
131/**
132 * Struct to keep track of the xquery while iterating all the edges in a block.
133 */
134struct CheckEdgeContext
135{
136 /**
137 * Xquery: string we are looking for.
138 */
139 const char *xquery;
140
141 /**
142 * Has any edge matched the xquery so far? (GNUNET_OK / GNUNET_NO)
143 */
144 int found;
145};
146
147
148/**
149 * Iterator over all edges in a block, checking for a presence of a given query.
150 *
151 * @param cls Closure, (xquery context).
152 * @param token Token that follows to next state.
153 * @param len Length of token.
154 * @param key Hash of next state.
155 *
156 * @return #GNUNET_YES, to keep iterating
157 */
158static int
159check_edge (void *cls,
160 const char *token,
161 size_t len,
162 const struct GNUNET_HashCode *key)
163{
164 struct CheckEdgeContext *ctx = cls;
165
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167 "edge %.*s [%u]: %s\n",
168 (int) len,
169 token,
170 (unsigned int) len,
171 GNUNET_h2s (key));
172 if (NULL == ctx->xquery)
173 return GNUNET_YES;
174 if (strlen (ctx->xquery) < len)
175 return GNUNET_YES; /* too long */
176 if (0 == strncmp (ctx->xquery, token, len))
177 ctx->found = GNUNET_OK;
178 return GNUNET_YES; /* keep checking for malformed data! */
179}
180
181
182int
183REGEX_BLOCK_check (const struct RegexBlock *block,
184 size_t size,
185 const struct GNUNET_HashCode *query,
186 const char *xquery)
187{
188 struct GNUNET_HashCode key;
189 struct CheckEdgeContext ctx;
190 int res;
191
192 LOG (GNUNET_ERROR_TYPE_DEBUG,
193 "Block check\n");
194 if (GNUNET_OK !=
195 REGEX_BLOCK_get_key (block, size,
196 &key))
197 {
198 GNUNET_break_op (0);
199 return GNUNET_SYSERR;
200 }
201 if ((NULL != query) &&
202 (0 != GNUNET_memcmp (&key,
203 query)) )
204 {
205 GNUNET_break_op (0);
206 return GNUNET_SYSERR;
207 }
208 if ((GNUNET_YES == ntohs (block->is_accepting)) &&
209 ((NULL == xquery) || ('\0' == xquery[0])))
210 {
211 LOG (GNUNET_ERROR_TYPE_DEBUG,
212 " out! Is accepting: %u, xquery %p\n",
213 ntohs (block->is_accepting),
214 xquery);
215 return GNUNET_OK;
216 }
217 ctx.xquery = xquery;
218 ctx.found = GNUNET_NO;
219 res = REGEX_BLOCK_iterate (block, size, &check_edge, &ctx);
220 if (GNUNET_SYSERR == res)
221 return GNUNET_SYSERR;
222 if (NULL == xquery)
223 return GNUNET_YES;
224 LOG (GNUNET_ERROR_TYPE_DEBUG, "Result %d\n", ctx.found);
225 return ctx.found;
226}
227
228
229int
230REGEX_BLOCK_get_key (const struct RegexBlock *block,
231 size_t block_len,
232 struct GNUNET_HashCode *key)
233{
234 uint16_t len;
235 const struct GNUNET_HashCode *destinations;
236 const struct EdgeInfo *edges;
237 uint16_t num_destinations;
238 uint16_t num_edges;
239 size_t total;
240
241 if (block_len < sizeof(struct RegexBlock))
242 {
243 GNUNET_break_op (0);
244 return GNUNET_SYSERR;
245 }
246 num_destinations = ntohs (block->num_destinations);
247 num_edges = ntohs (block->num_edges);
248 len = ntohs (block->proof_len);
249 destinations = (const struct GNUNET_HashCode *) &block[1];
250 edges = (const struct EdgeInfo *) &destinations[num_destinations];
251 total = sizeof(struct RegexBlock) + num_destinations * sizeof(struct
252 GNUNET_HashCode)
253 + num_edges * sizeof(struct EdgeInfo) + len;
254 if (block_len < total)
255 {
256 GNUNET_break_op (0);
257 return GNUNET_SYSERR;
258 }
259 GNUNET_CRYPTO_hash (&edges[num_edges], len, key);
260 return GNUNET_OK;
261}
262
263
264int
265REGEX_BLOCK_iterate (const struct RegexBlock *block,
266 size_t size,
267 REGEX_INTERNAL_EgdeIterator iterator,
268 void *iter_cls)
269{
270 uint16_t len;
271 const struct GNUNET_HashCode *destinations;
272 const struct EdgeInfo *edges;
273 const char *aux;
274 uint16_t num_destinations;
275 uint16_t num_edges;
276 size_t total;
277 unsigned int n;
278 size_t off;
279
280 LOG (GNUNET_ERROR_TYPE_DEBUG, "Block iterate\n");
281 if (size < sizeof(struct RegexBlock))
282 {
283 GNUNET_break_op (0);
284 return GNUNET_SYSERR;
285 }
286 num_destinations = ntohs (block->num_destinations);
287 num_edges = ntohs (block->num_edges);
288 len = ntohs (block->proof_len);
289 destinations = (const struct GNUNET_HashCode *) &block[1];
290 edges = (const struct EdgeInfo *) &destinations[num_destinations];
291 aux = (const char *) &edges[num_edges];
292 total = sizeof(struct RegexBlock) + num_destinations * sizeof(struct
293 GNUNET_HashCode)
294 + num_edges * sizeof(struct EdgeInfo) + len;
295 if (size < total)
296 {
297 GNUNET_break_op (0);
298 return GNUNET_SYSERR;
299 }
300 for (n = 0; n < num_edges; n++)
301 total += ntohs (edges[n].token_length);
302 if (size != total)
303 {
304 fprintf (stderr, "Expected %u, got %u\n",
305 (unsigned int) size,
306 (unsigned int) total);
307 GNUNET_break_op (0);
308 return GNUNET_SYSERR;
309 }
310 off = len;
311 LOG (GNUNET_ERROR_TYPE_DEBUG,
312 "Start iterating block of size %lu, proof %u, off %lu edges %u\n",
313 (unsigned long) size, len, (unsigned long) off, n);
314 /* &aux[off] always points to our token */
315 for (n = 0; n < num_edges; n++)
316 {
317 LOG (GNUNET_ERROR_TYPE_DEBUG,
318 "Edge %u/%u, off %lu tokenlen %u (%.*s)\n",
319 n + 1, num_edges, (unsigned long) off,
320 ntohs (edges[n].token_length), ntohs (edges[n].token_length),
321 &aux[off]);
322 if (NULL != iterator)
323 if (GNUNET_NO == iterator (iter_cls,
324 &aux[off],
325 ntohs (edges[n].token_length),
326 &destinations[ntohs (
327 edges[n].destination_index)]))
328 return GNUNET_OK;
329 off += ntohs (edges[n].token_length);
330 }
331 return GNUNET_OK;
332}
333
334
335/**
336 * Construct a regex block to be stored in the DHT.
337 *
338 * @param proof proof string for the block
339 * @param num_edges number of edges in the block
340 * @param edges the edges of the block
341 * @param accepting is this an accepting state
342 * @param rsize set to the size of the returned block (OUT-only)
343 * @return the regex block, NULL on error
344 */
345struct RegexBlock *
346REGEX_BLOCK_create (const char *proof,
347 unsigned int num_edges,
348 const struct REGEX_BLOCK_Edge *edges,
349 int accepting,
350 size_t *rsize)
351{
352 struct RegexBlock *block;
353 struct GNUNET_HashCode destinations[1024]; /* 1024 = 64k/64 bytes/key == absolute MAX */
354 uint16_t destination_indices[num_edges];
355 struct GNUNET_HashCode *dests;
356 struct EdgeInfo *edgeinfos;
357 size_t off;
358 size_t len;
359 size_t total;
360 size_t slen;
361 unsigned int unique_destinations;
362 unsigned int j;
363 unsigned int i;
364 char *aux;
365
366 len = strlen (proof);
367 if (len > UINT16_MAX)
368 {
369 GNUNET_break (0);
370 return NULL;
371 }
372 unique_destinations = 0;
373 total = sizeof(struct RegexBlock) + len;
374 for (i = 0; i < num_edges; i++)
375 {
376 slen = strlen (edges[i].label);
377 if (slen > UINT16_MAX)
378 {
379 GNUNET_break (0);
380 return NULL;
381 }
382 total += slen;
383 for (j = 0; j < unique_destinations; j++)
384 if (0 == memcmp (&destinations[j],
385 &edges[i].destination,
386 sizeof(struct GNUNET_HashCode)))
387 break;
388 if (j >= 1024)
389 {
390 GNUNET_break (0);
391 return NULL;
392 }
393 destination_indices[i] = j;
394 if (j == unique_destinations)
395 destinations[unique_destinations++] = edges[i].destination;
396 }
397 total += num_edges * sizeof(struct EdgeInfo) + unique_destinations
398 * sizeof(struct GNUNET_HashCode);
399 if (total >= GNUNET_CONSTANTS_MAX_BLOCK_SIZE)
400 {
401 GNUNET_break (0);
402 return NULL;
403 }
404 block = GNUNET_malloc (total);
405 block->proof_len = htons (len);
406 block->is_accepting = htons (accepting);
407 block->num_edges = htons (num_edges);
408 block->num_destinations = htons (unique_destinations);
409 dests = (struct GNUNET_HashCode *) &block[1];
410 GNUNET_memcpy (dests, destinations, sizeof(struct GNUNET_HashCode)
411 * unique_destinations);
412 edgeinfos = (struct EdgeInfo *) &dests[unique_destinations];
413 aux = (char *) &edgeinfos[num_edges];
414 off = len;
415 GNUNET_memcpy (aux, proof, len);
416 for (i = 0; i < num_edges; i++)
417 {
418 slen = strlen (edges[i].label);
419 edgeinfos[i].token_length = htons ((uint16_t) slen);
420 edgeinfos[i].destination_index = htons (destination_indices[i]);
421 GNUNET_memcpy (&aux[off],
422 edges[i].label,
423 slen);
424 off += slen;
425 }
426 *rsize = total;
427 return block;
428}
429
430
431/* end of regex_block_lib.c */
diff --git a/src/service/regex/regex_block_lib.h b/src/service/regex/regex_block_lib.h
deleted file mode 100644
index 11029b9af..000000000
--- a/src/service/regex/regex_block_lib.h
+++ /dev/null
@@ -1,193 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012,2013 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 * @author Bartlomiej Polot
23 * @file regex/regex_block_lib.h
24 * @brief common function to manipulate blocks stored by regex in the DHT
25 */
26
27#ifndef REGEX_BLOCK_LIB_H_
28#define REGEX_BLOCK_LIB_H_
29
30#ifdef __cplusplus
31extern "C"
32{
33#if 0
34/* keep Emacsens' auto-indent happy */
35}
36#endif
37#endif
38
39#include "platform.h"
40#include "block_regex.h"
41
42
43/**
44 * Representation of a Regex node (and edges) in the DHT.
45 */
46struct RegexBlock;
47
48
49/**
50 * Edge representation.
51 */
52struct REGEX_BLOCK_Edge
53{
54 /**
55 * Label of the edge. FIXME: might want to not consume exactly
56 * multiples of 8 bits, need length!
57 */
58 const char *label;
59
60 /**
61 * Destination of the edge.
62 */
63 struct GNUNET_HashCode destination;
64};
65
66
67/**
68 * Check if the given 'proof' matches the given 'key'.
69 *
70 * @param proof partial regex of a state
71 * @param proof_len number of bytes in @a proof
72 * @param key hash of a state.
73 * @return #GNUNET_OK if the proof is valid for the given key.
74 */
75int
76REGEX_BLOCK_check_proof (const char *proof,
77 size_t proof_len,
78 const struct GNUNET_HashCode *key);
79
80
81/**
82 * Check if the regex block is well formed, including all edges.
83 *
84 * @param block The start of the block.
85 * @param size The size of the @a block.
86 * @param query the query for the @a block
87 * @param xquery String describing the edge we are looking for.
88 * Can be NULL in case this is a put block.
89 * @return #GNUNET_OK in case it's fine.
90 * #GNUNET_NO in case the xquery exists and is not found (IRRELEVANT).
91 * #GNUNET_SYSERR if the block is invalid.
92 */
93int
94REGEX_BLOCK_check (const struct RegexBlock *block,
95 size_t size,
96 const struct GNUNET_HashCode *query,
97 const char *xquery);
98
99
100/* FIXME: might want to use 'struct REGEX_BLOCK_Edge' here instead of 3 arguments! */
101
102/**
103 * Iterator over edges in a block.
104 *
105 * @param cls Closure.
106 * @param token Token that follows to next state.
107 * @param len Length of token.
108 * @param key Hash of next state.
109 * @return #GNUNET_YES if should keep iterating, #GNUNET_NO otherwise.
110 */
111typedef int
112(*REGEX_INTERNAL_EgdeIterator)(void *cls,
113 const char *token,
114 size_t len,
115 const struct GNUNET_HashCode *key);
116
117
118/**
119 * Iterate over all edges of a block of a regex state.
120 *
121 * @param block Block to iterate over.
122 * @param size Size of block.
123 * @param iterator Function to call on each edge in the block.
124 * @param iter_cls Closure for the @a iterator.
125 * @return #GNUNET_SYSERR if an error has been encountered.
126 * #GNUNET_OK if no error has been encountered.
127 * Note that if the iterator stops the iteration by returning
128 * #GNUNET_NO, the block will no longer be checked for further errors.
129 * The return value will be #GNUNET_OK meaning that no errors were
130 * found until the edge last notified to the iterator, but there might
131 * be errors in further edges.
132 */
133int
134REGEX_BLOCK_iterate (const struct RegexBlock *block,
135 size_t size,
136 REGEX_INTERNAL_EgdeIterator iterator,
137 void *iter_cls);
138
139
140/**
141 * Obtain the key that a particular block is to be stored under.
142 *
143 * @param block block to get the key from
144 * @param block_len number of bytes in @a block
145 * @param key where to store the key
146 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block is malformed
147 */
148int
149REGEX_BLOCK_get_key (const struct RegexBlock *block,
150 size_t block_len,
151 struct GNUNET_HashCode *key);
152
153
154/**
155 * Test if this block is marked as being an accept state.
156 *
157 * @param block block to test
158 * @param size number of bytes in block
159 * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not
160 */
161int
162GNUNET_BLOCK_is_accepting (const struct RegexBlock *block,
163 size_t block_len);
164
165
166/**
167 * Construct a regex block to be stored in the DHT.
168 *
169 * @param proof proof string for the block
170 * @param num_edges number of edges in the block
171 * @param edges the edges of the block
172 * @param accepting is this an accepting state
173 * @param rsize set to the size of the returned block (OUT-only)
174 * @return the regex block, NULL on error
175 */
176struct RegexBlock *
177REGEX_BLOCK_create (const char *proof,
178 unsigned int num_edges,
179 const struct REGEX_BLOCK_Edge *edges,
180 int accepting,
181 size_t *rsize);
182
183
184#if 0 /* keep Emacsens' auto-indent happy */
185{
186#endif
187#ifdef __cplusplus
188}
189#endif
190
191/* ifndef REGEX_BLOCK_LIB_H */
192#endif
193/* end of regex_block_lib.h */
diff --git a/src/service/regex/regex_internal_dht.c b/src/service/regex/regex_internal_dht.c
index bda979edb..9850f106f 100644
--- a/src/service/regex/regex_internal_dht.c
+++ b/src/service/regex/regex_internal_dht.c
@@ -25,7 +25,6 @@
25 */ 25 */
26#include "platform.h" 26#include "platform.h"
27#include "regex_internal_lib.h" 27#include "regex_internal_lib.h"
28#include "regex_block_lib.h"
29#include "gnunet_dht_service.h" 28#include "gnunet_dht_service.h"
30#include "gnunet_statistics_service.h" 29#include "gnunet_statistics_service.h"
31#include "gnunet_constants.h" 30#include "gnunet_constants.h"
diff --git a/src/service/regex/regex_internal_lib.h b/src/service/regex/regex_internal_lib.h
index 9baf84995..bfa3fc97d 100644
--- a/src/service/regex/regex_internal_lib.h
+++ b/src/service/regex/regex_internal_lib.h
@@ -29,7 +29,7 @@
29#include "gnunet_util_lib.h" 29#include "gnunet_util_lib.h"
30#include "gnunet_dht_service.h" 30#include "gnunet_dht_service.h"
31#include "gnunet_statistics_service.h" 31#include "gnunet_statistics_service.h"
32#include "regex_block_lib.h" 32#include "../../plugin/regex/regex_block_lib.h"
33 33
34#ifdef __cplusplus 34#ifdef __cplusplus
35extern "C" 35extern "C"