diff options
Diffstat (limited to 'src/service/namecache')
-rw-r--r-- | src/service/namecache/.gitignore | 6 | ||||
-rw-r--r-- | src/service/namecache/Makefile.am | 76 | ||||
-rw-r--r-- | src/service/namecache/gnunet-service-namecache.c | 390 | ||||
-rw-r--r-- | src/service/namecache/meson.build | 41 | ||||
-rw-r--r-- | src/service/namecache/namecache.conf.in | 29 | ||||
-rw-r--r-- | src/service/namecache/namecache.h | 131 | ||||
-rw-r--r-- | src/service/namecache/namecache_api.c | 564 | ||||
-rw-r--r-- | src/service/namecache/test_namecache_api.conf | 13 | ||||
-rw-r--r-- | src/service/namecache/test_namecache_api_cache_block.c | 244 |
9 files changed, 1494 insertions, 0 deletions
diff --git a/src/service/namecache/.gitignore b/src/service/namecache/.gitignore new file mode 100644 index 000000000..280bf9f39 --- /dev/null +++ b/src/service/namecache/.gitignore | |||
@@ -0,0 +1,6 @@ | |||
1 | gnunet-service-namecache | ||
2 | test_namecache_api_cache_block | ||
3 | test_plugin_namecache_postgres | ||
4 | test_plugin_namecache_sqlite | ||
5 | zonefiles | ||
6 | test_plugin_namecache_flat | ||
diff --git a/src/service/namecache/Makefile.am b/src/service/namecache/Makefile.am new file mode 100644 index 000000000..7a8305491 --- /dev/null +++ b/src/service/namecache/Makefile.am | |||
@@ -0,0 +1,76 @@ | |||
1 | # This Makefile.am is in the public domain | ||
2 | AM_CPPFLAGS = -I$(top_srcdir)/src/include $(POSTGRESQL_CPPFLAGS) | ||
3 | |||
4 | plugindir = $(libdir)/gnunet | ||
5 | |||
6 | pkgcfgdir= $(pkgdatadir)/config.d/ | ||
7 | |||
8 | libexecdir= $(pkglibdir)/libexec/ | ||
9 | |||
10 | pkgcfg_DATA = \ | ||
11 | namecache.conf | ||
12 | |||
13 | if USE_COVERAGE | ||
14 | AM_CFLAGS = --coverage -O0 | ||
15 | XLIBS = -lgcov | ||
16 | endif | ||
17 | |||
18 | # testcases do not even build yet; thus: experimental! | ||
19 | TESTING_TESTS = \ | ||
20 | test_namecache_api_cache_block | ||
21 | |||
22 | if HAVE_SQLITE | ||
23 | #check_PROGRAMS = \ | ||
24 | # $(TESTING_TESTS) | ||
25 | endif | ||
26 | |||
27 | if ENABLE_TEST_RUN | ||
28 | AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; | ||
29 | TESTS = \ | ||
30 | $(check_PROGRAMS) | ||
31 | endif | ||
32 | |||
33 | lib_LTLIBRARIES = \ | ||
34 | libgnunetnamecache.la | ||
35 | |||
36 | |||
37 | libgnunetnamecache_la_SOURCES = \ | ||
38 | namecache_api.c \ | ||
39 | namecache.h | ||
40 | libgnunetnamecache_la_LIBADD = \ | ||
41 | $(top_builddir)/src/lib/gnsrecord/libgnunetgnsrecord.la \ | ||
42 | $(top_builddir)/src/service/identity/libgnunetidentity.la \ | ||
43 | $(top_builddir)/src/lib/util/libgnunetutil.la \ | ||
44 | $(GN_LIBINTL) | ||
45 | libgnunetnamecache_la_LDFLAGS = \ | ||
46 | $(GN_LIB_LDFLAGS) \ | ||
47 | -version-info 0:0:0 | ||
48 | |||
49 | |||
50 | libexec_PROGRAMS = \ | ||
51 | gnunet-service-namecache | ||
52 | |||
53 | gnunet_service_namecache_SOURCES = \ | ||
54 | gnunet-service-namecache.c | ||
55 | |||
56 | gnunet_service_namecache_LDADD = \ | ||
57 | $(top_builddir)/src/lib/gnsrecord/libgnunetgnsrecord.la \ | ||
58 | $(top_builddir)/src/service/identity/libgnunetidentity.la \ | ||
59 | $(top_builddir)/src/service/statistics/libgnunetstatistics.la \ | ||
60 | $(top_builddir)/src/lib/util/libgnunetutil.la \ | ||
61 | libgnunetnamecache.la \ | ||
62 | $(GN_LIBINTL) | ||
63 | |||
64 | |||
65 | test_namecache_api_cache_block_SOURCES = \ | ||
66 | test_namecache_api_cache_block.c | ||
67 | test_namecache_api_cache_block_LDADD = \ | ||
68 | $(top_builddir)/src/lib/gnsrecord/libgnunetgnsrecord.la \ | ||
69 | $(top_builddir)/src/service/identity/libgnunetidentity.la \ | ||
70 | libgnunetnamecache.la \ | ||
71 | $(top_builddir)/src/service/testing/libgnunettesting.la \ | ||
72 | $(top_builddir)/src/lib/util/libgnunetutil.la | ||
73 | |||
74 | |||
75 | EXTRA_DIST = \ | ||
76 | test_namecache_api.conf | ||
diff --git a/src/service/namecache/gnunet-service-namecache.c b/src/service/namecache/gnunet-service-namecache.c new file mode 100644 index 000000000..dce843801 --- /dev/null +++ b/src/service/namecache/gnunet-service-namecache.c | |||
@@ -0,0 +1,390 @@ | |||
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 | * @file namecache/gnunet-service-namecache.c | ||
23 | * @brief namecache for the GNUnet naming system | ||
24 | * @author Matthias Wachs | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_statistics_service.h" | ||
30 | #include "gnunet_namecache_service.h" | ||
31 | #include "gnunet_namecache_plugin.h" | ||
32 | #include "gnunet_signatures.h" | ||
33 | #include "namecache.h" | ||
34 | |||
35 | #define LOG_STRERROR_FILE(kind, syscall, \ | ||
36 | filename) GNUNET_log_from_strerror_file (kind, "util", \ | ||
37 | syscall, \ | ||
38 | filename) | ||
39 | |||
40 | |||
41 | /** | ||
42 | * A namecache client | ||
43 | */ | ||
44 | struct NamecacheClient | ||
45 | { | ||
46 | /** | ||
47 | * The client | ||
48 | */ | ||
49 | struct GNUNET_SERVICE_Client *client; | ||
50 | |||
51 | /** | ||
52 | * The message queue to talk to @e client. | ||
53 | */ | ||
54 | struct GNUNET_MQ_Handle *mq; | ||
55 | }; | ||
56 | |||
57 | |||
58 | /** | ||
59 | * Configuration handle. | ||
60 | */ | ||
61 | static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg; | ||
62 | |||
63 | /** | ||
64 | * Handle to the statistics service | ||
65 | */ | ||
66 | static struct GNUNET_STATISTICS_Handle *statistics; | ||
67 | |||
68 | /** | ||
69 | * Database handle | ||
70 | */ | ||
71 | static struct GNUNET_NAMECACHE_PluginFunctions *GSN_database; | ||
72 | |||
73 | /** | ||
74 | * Name of the database plugin | ||
75 | */ | ||
76 | static char *db_lib_name; | ||
77 | |||
78 | |||
79 | /** | ||
80 | * Task run during shutdown. | ||
81 | * | ||
82 | * @param cls unused | ||
83 | */ | ||
84 | static void | ||
85 | cleanup_task (void *cls) | ||
86 | { | ||
87 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
88 | "Stopping namecache service\n"); | ||
89 | GNUNET_break (NULL == | ||
90 | GNUNET_PLUGIN_unload (db_lib_name, | ||
91 | GSN_database)); | ||
92 | GNUNET_free (db_lib_name); | ||
93 | db_lib_name = NULL; | ||
94 | if (NULL != statistics) | ||
95 | { | ||
96 | GNUNET_STATISTICS_destroy (statistics, | ||
97 | GNUNET_NO); | ||
98 | statistics = NULL; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | |||
103 | /** | ||
104 | * Called whenever a client is disconnected. | ||
105 | * Frees our resources associated with that client. | ||
106 | * | ||
107 | * @param cls closure | ||
108 | * @param client identification of the client | ||
109 | * @param app_ctx the `struct NamecacheClient` for this @a client | ||
110 | */ | ||
111 | static void | ||
112 | client_disconnect_cb (void *cls, | ||
113 | struct GNUNET_SERVICE_Client *client, | ||
114 | void *app_ctx) | ||
115 | { | ||
116 | struct NamecacheClient *nc = app_ctx; | ||
117 | |||
118 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
119 | "Client %p disconnected\n", | ||
120 | client); | ||
121 | GNUNET_free (nc); | ||
122 | } | ||
123 | |||
124 | |||
125 | /** | ||
126 | * Add a client to our list of active clients. | ||
127 | * | ||
128 | * @param cls NULL | ||
129 | * @param client client to add | ||
130 | * @param mq queue to talk to @a client | ||
131 | * @return internal namecache client structure for this client | ||
132 | */ | ||
133 | static void * | ||
134 | client_connect_cb (void *cls, | ||
135 | struct GNUNET_SERVICE_Client *client, | ||
136 | struct GNUNET_MQ_Handle *mq) | ||
137 | { | ||
138 | struct NamecacheClient *nc; | ||
139 | |||
140 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
141 | "Client %p connected\n", | ||
142 | client); | ||
143 | nc = GNUNET_new (struct NamecacheClient); | ||
144 | nc->client = client; | ||
145 | nc->mq = mq; | ||
146 | return nc; | ||
147 | } | ||
148 | |||
149 | |||
150 | /** | ||
151 | * Context for name lookups passed from #handle_lookup_block to | ||
152 | * #handle_lookup_block_it as closure | ||
153 | */ | ||
154 | struct LookupBlockContext | ||
155 | { | ||
156 | /** | ||
157 | * The client to send the response to | ||
158 | */ | ||
159 | struct NamecacheClient *nc; | ||
160 | |||
161 | /** | ||
162 | * Operation id for the name lookup | ||
163 | */ | ||
164 | uint32_t request_id; | ||
165 | |||
166 | /** | ||
167 | * Lookup status | ||
168 | */ | ||
169 | int status; | ||
170 | }; | ||
171 | |||
172 | |||
173 | /** | ||
174 | * A #GNUNET_NAMECACHE_BlockCallback for name lookups in #handle_lookup_block | ||
175 | * | ||
176 | * @param cls a `struct LookupNameContext *` with information about the request | ||
177 | * @param block the block | ||
178 | */ | ||
179 | static void | ||
180 | handle_lookup_block_it (void *cls, | ||
181 | const struct GNUNET_GNSRECORD_Block *block) | ||
182 | { | ||
183 | struct LookupBlockContext *lnc = cls; | ||
184 | struct GNUNET_MQ_Envelope *env; | ||
185 | struct LookupBlockResponseMessage *r; | ||
186 | size_t bsize; | ||
187 | |||
188 | bsize = GNUNET_GNSRECORD_block_get_size (block); | ||
189 | env = GNUNET_MQ_msg_extra (r, | ||
190 | bsize, | ||
191 | GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE); | ||
192 | r->gns_header.r_id = htonl (lnc->request_id); | ||
193 | GNUNET_memcpy (&r[1], | ||
194 | block, | ||
195 | bsize); | ||
196 | GNUNET_STATISTICS_update (statistics, | ||
197 | "blocks found in cache", | ||
198 | 1, | ||
199 | GNUNET_NO); | ||
200 | r->expire = GNUNET_TIME_absolute_hton ( | ||
201 | GNUNET_GNSRECORD_block_get_expiration (block)); | ||
202 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
203 | "Sending NAMECACHE_LOOKUP_BLOCK_RESPONSE message\n"); | ||
204 | GNUNET_MQ_send (lnc->nc->mq, | ||
205 | env); | ||
206 | } | ||
207 | |||
208 | |||
209 | /** | ||
210 | * Handles a #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK message | ||
211 | * | ||
212 | * @param cls a `struct NamecacheClient *` | ||
213 | * @param ln_msg the inbound message | ||
214 | */ | ||
215 | static void | ||
216 | handle_lookup_block (void *cls, | ||
217 | const struct LookupBlockMessage *ln_msg) | ||
218 | { | ||
219 | struct NamecacheClient *nc = cls; | ||
220 | struct GNUNET_MQ_Envelope *env; | ||
221 | struct LookupBlockContext lnc; | ||
222 | struct LookupBlockResponseMessage *zir_end; | ||
223 | int ret; | ||
224 | |||
225 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
226 | "Received NAMECACHE_LOOKUP_BLOCK message\n"); | ||
227 | GNUNET_STATISTICS_update (statistics, | ||
228 | "blocks looked up", | ||
229 | 1, | ||
230 | GNUNET_NO); | ||
231 | lnc.request_id = ntohl (ln_msg->gns_header.r_id); | ||
232 | lnc.nc = nc; | ||
233 | lnc.status = GNUNET_OK; | ||
234 | if (GNUNET_SYSERR == | ||
235 | (ret = GSN_database->lookup_block (GSN_database->cls, | ||
236 | &ln_msg->query, | ||
237 | &handle_lookup_block_it, | ||
238 | &lnc))) | ||
239 | { | ||
240 | /* internal error (in database plugin); might be best to just hang up on | ||
241 | plugin rather than to signal that there are 'no' results, which | ||
242 | might also be false... */ | ||
243 | GNUNET_break (0); | ||
244 | GNUNET_SERVICE_client_drop (nc->client); | ||
245 | return; | ||
246 | } | ||
247 | if ((0 == ret) || (GNUNET_SYSERR == lnc.status)) | ||
248 | { | ||
249 | /* no records match at all, generate empty response */ | ||
250 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
251 | "Sending empty NAMECACHE_LOOKUP_BLOCK_RESPONSE message\n"); | ||
252 | env = GNUNET_MQ_msg (zir_end, | ||
253 | GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE); | ||
254 | zir_end->gns_header.r_id = ln_msg->gns_header.r_id; | ||
255 | GNUNET_MQ_send (nc->mq, | ||
256 | env); | ||
257 | } | ||
258 | GNUNET_SERVICE_client_continue (nc->client); | ||
259 | } | ||
260 | |||
261 | |||
262 | /** | ||
263 | * Check a #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE message | ||
264 | * | ||
265 | * @param cls our `struct NamecacheClient` | ||
266 | * @param rp_msg message to process | ||
267 | * @return #GNUNET_OK (always fine) | ||
268 | */ | ||
269 | static int | ||
270 | check_block_cache (void *cls, | ||
271 | const struct BlockCacheMessage *rp_msg) | ||
272 | { | ||
273 | return GNUNET_OK; | ||
274 | } | ||
275 | |||
276 | |||
277 | /** | ||
278 | * Handles a #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE message | ||
279 | * | ||
280 | * @param cls our `struct NamecacheClient` | ||
281 | * @param rp_msg message to process | ||
282 | */ | ||
283 | static void | ||
284 | handle_block_cache (void *cls, | ||
285 | const struct BlockCacheMessage *rp_msg) | ||
286 | { | ||
287 | struct NamecacheClient *nc = cls; | ||
288 | struct GNUNET_MQ_Envelope *env; | ||
289 | struct BlockCacheResponseMessage *rpr_msg; | ||
290 | struct GNUNET_GNSRECORD_Block *block; | ||
291 | size_t esize; | ||
292 | int res; | ||
293 | |||
294 | GNUNET_STATISTICS_update (statistics, | ||
295 | "blocks cached", | ||
296 | 1, | ||
297 | GNUNET_NO); | ||
298 | esize = ntohs (rp_msg->gns_header.header.size) - sizeof(struct | ||
299 | BlockCacheMessage); | ||
300 | block = GNUNET_malloc (esize); | ||
301 | memcpy (block, &rp_msg[1], esize); | ||
302 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
303 | "Received NAMECACHE_BLOCK_CACHE message with type %u\n", | ||
304 | htonl (block->type)); | ||
305 | res = GSN_database->cache_block (GSN_database->cls, | ||
306 | block); | ||
307 | GNUNET_free (block); | ||
308 | env = GNUNET_MQ_msg (rpr_msg, | ||
309 | GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE); | ||
310 | rpr_msg->gns_header.r_id = rp_msg->gns_header.r_id; | ||
311 | rpr_msg->op_result = htonl (res); | ||
312 | GNUNET_MQ_send (nc->mq, | ||
313 | env); | ||
314 | GNUNET_SERVICE_client_continue (nc->client); | ||
315 | } | ||
316 | |||
317 | |||
318 | /** | ||
319 | * Process namecache requests. | ||
320 | * | ||
321 | * @param cls closure | ||
322 | * @param cfg configuration to use | ||
323 | * @param service the initialized service | ||
324 | */ | ||
325 | static void | ||
326 | run (void *cls, | ||
327 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
328 | struct GNUNET_SERVICE_Handle *service) | ||
329 | { | ||
330 | char *database; | ||
331 | |||
332 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
333 | "Starting namecache service\n"); | ||
334 | GSN_cfg = cfg; | ||
335 | |||
336 | /* Loading database plugin */ | ||
337 | if (GNUNET_OK != | ||
338 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
339 | "namecache", | ||
340 | "database", | ||
341 | &database)) | ||
342 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
343 | "No database backend configured\n"); | ||
344 | |||
345 | GNUNET_asprintf (&db_lib_name, | ||
346 | "libgnunet_plugin_namecache_%s", | ||
347 | database); | ||
348 | GSN_database = GNUNET_PLUGIN_load (db_lib_name, | ||
349 | (void *) GSN_cfg); | ||
350 | GNUNET_free (database); | ||
351 | if (NULL == GSN_database) | ||
352 | { | ||
353 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
354 | "Could not load database backend `%s'\n", | ||
355 | db_lib_name); | ||
356 | GNUNET_SCHEDULER_add_now (&cleanup_task, | ||
357 | NULL); | ||
358 | return; | ||
359 | } | ||
360 | statistics = GNUNET_STATISTICS_create ("namecache", | ||
361 | cfg); | ||
362 | |||
363 | /* Configuring server handles */ | ||
364 | GNUNET_SCHEDULER_add_shutdown (&cleanup_task, | ||
365 | NULL); | ||
366 | } | ||
367 | |||
368 | |||
369 | /** | ||
370 | * Define "main" method using service macro. | ||
371 | */ | ||
372 | GNUNET_SERVICE_MAIN | ||
373 | ("namecache", | ||
374 | GNUNET_SERVICE_OPTION_NONE, | ||
375 | &run, | ||
376 | &client_connect_cb, | ||
377 | &client_disconnect_cb, | ||
378 | NULL, | ||
379 | GNUNET_MQ_hd_fixed_size (lookup_block, | ||
380 | GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK, | ||
381 | struct LookupBlockMessage, | ||
382 | NULL), | ||
383 | GNUNET_MQ_hd_var_size (block_cache, | ||
384 | GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE, | ||
385 | struct BlockCacheMessage, | ||
386 | NULL), | ||
387 | GNUNET_MQ_handler_end ()); | ||
388 | |||
389 | |||
390 | /* end of gnunet-service-namecache.c */ | ||
diff --git a/src/service/namecache/meson.build b/src/service/namecache/meson.build new file mode 100644 index 000000000..72dafcd9d --- /dev/null +++ b/src/service/namecache/meson.build | |||
@@ -0,0 +1,41 @@ | |||
1 | libgnunetnamecache_src = ['namecache_api.c'] | ||
2 | |||
3 | gnunetnamecache_src = ['gnunet-namecache.c'] | ||
4 | gnunetservicenamecache_src = ['gnunet-service-namecache.c'] | ||
5 | |||
6 | configure_file(input : 'namecache.conf.in', | ||
7 | output : 'namecache.conf', | ||
8 | configuration : cdata, | ||
9 | install: true, | ||
10 | install_dir: pkgcfgdir) | ||
11 | |||
12 | |||
13 | if get_option('monolith') | ||
14 | foreach p : libgnunetnamecache_src + gnunetservicenamecache_src | ||
15 | gnunet_src += 'namecache/' + p | ||
16 | endforeach | ||
17 | endif | ||
18 | |||
19 | libgnunetnamecache = library('gnunetnamecache', | ||
20 | libgnunetnamecache_src, | ||
21 | soversion: '0', | ||
22 | version: '0.0.0', | ||
23 | dependencies: [libgnunetutil_dep, | ||
24 | libgnunetgnsrecord_dep], | ||
25 | include_directories: [incdir, configuration_inc], | ||
26 | install: true, | ||
27 | install_dir: get_option('libdir')) | ||
28 | libgnunetnamecache_dep = declare_dependency(link_with : libgnunetnamecache) | ||
29 | pkg.generate(libgnunetnamecache, url: 'https://www.gnunet.org', | ||
30 | description : 'Provides API for storing GNS records to a cache') | ||
31 | |||
32 | executable ('gnunet-service-namecache', | ||
33 | gnunetservicenamecache_src, | ||
34 | dependencies: [libgnunetnamecache_dep, | ||
35 | libgnunetutil_dep, | ||
36 | libgnunetgnsrecord_dep, | ||
37 | libgnunetstatistics_dep], | ||
38 | include_directories: [incdir, configuration_inc], | ||
39 | install: true, | ||
40 | install_dir: get_option('libdir')/'gnunet'/'libexec') | ||
41 | |||
diff --git a/src/service/namecache/namecache.conf.in b/src/service/namecache/namecache.conf.in new file mode 100644 index 000000000..6bf75454b --- /dev/null +++ b/src/service/namecache/namecache.conf.in | |||
@@ -0,0 +1,29 @@ | |||
1 | [namecache] | ||
2 | START_ON_DEMAND = @START_ON_DEMAND@ | ||
3 | RUN_PER_USER = NO | ||
4 | UNIXPATH = $GNUNET_RUNTIME_DIR/gnunet-service-namecache.sock | ||
5 | UNIX_MATCH_UID = NO | ||
6 | UNIX_MATCH_GID = YES | ||
7 | @UNIXONLY@ PORT = 2113 | ||
8 | HOSTNAME = localhost | ||
9 | BINARY = gnunet-service-namecache | ||
10 | ACCEPT_FROM = 127.0.0.1; | ||
11 | ACCEPT_FROM6 = ::1; | ||
12 | DATABASE = sqlite | ||
13 | |||
14 | # Disables use of caching by GNS. Useful for systems that | ||
15 | # publish very large zones and are CPU bound, if they do not | ||
16 | # also do a large number of lookups. | ||
17 | DISABLE = NO | ||
18 | |||
19 | [namecache-sqlite] | ||
20 | FILENAME = $GNUNET_DATA_HOME/namecache/sqlite.db | ||
21 | |||
22 | [namecache-flat] | ||
23 | FILENAME = $GNUNET_DATA_HOME/namecache/flat.db | ||
24 | |||
25 | [namecache-postgres] | ||
26 | CONFIG = postgres:///gnunet | ||
27 | SQL_DIR = ${DATADIR}/sql/ | ||
28 | |||
29 | |||
diff --git a/src/service/namecache/namecache.h b/src/service/namecache/namecache.h new file mode 100644 index 000000000..cb4d1bcdd --- /dev/null +++ b/src/service/namecache/namecache.h | |||
@@ -0,0 +1,131 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011-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 namecache/namecache.h | ||
23 | * @brief common internal definitions for namecache service | ||
24 | * @author Matthias Wachs | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | #ifndef NAMECACHE_H | ||
28 | #define NAMECACHE_H | ||
29 | |||
30 | /** | ||
31 | * Maximum length of any name, including 0-termination. | ||
32 | */ | ||
33 | #define MAX_NAME_LEN 256 | ||
34 | |||
35 | GNUNET_NETWORK_STRUCT_BEGIN | ||
36 | |||
37 | /** | ||
38 | * Generic namecache message with op id | ||
39 | */ | ||
40 | struct GNUNET_NAMECACHE_Header | ||
41 | { | ||
42 | /** | ||
43 | * header.type will be GNUNET_MESSAGE_TYPE_NAMECACHE_* | ||
44 | * header.size will be message size | ||
45 | */ | ||
46 | struct GNUNET_MessageHeader header; | ||
47 | |||
48 | /** | ||
49 | * Request ID in NBO | ||
50 | */ | ||
51 | uint32_t r_id GNUNET_PACKED; | ||
52 | }; | ||
53 | |||
54 | |||
55 | /** | ||
56 | * Lookup a block in the namecache | ||
57 | */ | ||
58 | struct LookupBlockMessage | ||
59 | { | ||
60 | /** | ||
61 | * Type will be #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK | ||
62 | */ | ||
63 | struct GNUNET_NAMECACHE_Header gns_header; | ||
64 | |||
65 | /** | ||
66 | * The query. | ||
67 | */ | ||
68 | struct GNUNET_HashCode query GNUNET_PACKED; | ||
69 | }; | ||
70 | |||
71 | |||
72 | /** | ||
73 | * Lookup response | ||
74 | */ | ||
75 | struct LookupBlockResponseMessage | ||
76 | { | ||
77 | /** | ||
78 | * Type will be #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE | ||
79 | */ | ||
80 | struct GNUNET_NAMECACHE_Header gns_header; | ||
81 | |||
82 | /** | ||
83 | * Expiration time | ||
84 | */ | ||
85 | struct GNUNET_TIME_AbsoluteNBO expire; | ||
86 | |||
87 | /* followed by encrypted block data */ | ||
88 | }; | ||
89 | |||
90 | |||
91 | /** | ||
92 | * Cache a record in the namecache. | ||
93 | */ | ||
94 | struct BlockCacheMessage | ||
95 | { | ||
96 | /** | ||
97 | * Type will be #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE | ||
98 | */ | ||
99 | struct GNUNET_NAMECACHE_Header gns_header; | ||
100 | |||
101 | /** | ||
102 | * Expiration time | ||
103 | */ | ||
104 | struct GNUNET_TIME_AbsoluteNBO expire; | ||
105 | |||
106 | /* followed by encrypted block data */ | ||
107 | }; | ||
108 | |||
109 | |||
110 | /** | ||
111 | * Response to a request to cache a block. | ||
112 | */ | ||
113 | struct BlockCacheResponseMessage | ||
114 | { | ||
115 | /** | ||
116 | * Type will be #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE | ||
117 | */ | ||
118 | struct GNUNET_NAMECACHE_Header gns_header; | ||
119 | |||
120 | /** | ||
121 | * #GNUNET_OK on success, #GNUNET_SYSERR error | ||
122 | */ | ||
123 | int32_t op_result GNUNET_PACKED; | ||
124 | }; | ||
125 | |||
126 | |||
127 | GNUNET_NETWORK_STRUCT_END | ||
128 | |||
129 | |||
130 | /* end of namecache.h */ | ||
131 | #endif | ||
diff --git a/src/service/namecache/namecache_api.c b/src/service/namecache/namecache_api.c new file mode 100644 index 000000000..fb4b0e304 --- /dev/null +++ b/src/service/namecache/namecache_api.c | |||
@@ -0,0 +1,564 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2010-2013, 2016 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 namecache/namecache_api.c | ||
23 | * @brief API to access the NAMECACHE service | ||
24 | * @author Martin Schanzenbach | ||
25 | * @author Matthias Wachs | ||
26 | * @author Christian Grothoff | ||
27 | */ | ||
28 | |||
29 | #include "platform.h" | ||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_constants.h" | ||
32 | #include "gnunet_gnsrecord_lib.h" | ||
33 | #include "gnunet_signatures.h" | ||
34 | #include "gnunet_namecache_service.h" | ||
35 | #include "namecache.h" | ||
36 | |||
37 | |||
38 | #define LOG(kind, ...) GNUNET_log_from (kind, "namecache-api", __VA_ARGS__) | ||
39 | |||
40 | |||
41 | /** | ||
42 | * An QueueEntry used to store information for a pending | ||
43 | * NAMECACHE record operation | ||
44 | */ | ||
45 | struct GNUNET_NAMECACHE_QueueEntry | ||
46 | { | ||
47 | /** | ||
48 | * Kept in a DLL. | ||
49 | */ | ||
50 | struct GNUNET_NAMECACHE_QueueEntry *next; | ||
51 | |||
52 | /** | ||
53 | * Kept in a DLL. | ||
54 | */ | ||
55 | struct GNUNET_NAMECACHE_QueueEntry *prev; | ||
56 | |||
57 | /** | ||
58 | * Main handle to access the namecache. | ||
59 | */ | ||
60 | struct GNUNET_NAMECACHE_Handle *nsh; | ||
61 | |||
62 | /** | ||
63 | * Continuation to call | ||
64 | */ | ||
65 | GNUNET_NAMECACHE_ContinuationWithStatus cont; | ||
66 | |||
67 | /** | ||
68 | * Closure for @e cont. | ||
69 | */ | ||
70 | void *cont_cls; | ||
71 | |||
72 | /** | ||
73 | * Function to call with the blocks we get back; or NULL. | ||
74 | */ | ||
75 | GNUNET_NAMECACHE_BlockProcessor block_proc; | ||
76 | |||
77 | /** | ||
78 | * Closure for @e block_proc. | ||
79 | */ | ||
80 | void *block_proc_cls; | ||
81 | |||
82 | /** | ||
83 | * The operation id this zone iteration operation has | ||
84 | */ | ||
85 | uint32_t op_id; | ||
86 | }; | ||
87 | |||
88 | |||
89 | /** | ||
90 | * Connection to the NAMECACHE service. | ||
91 | */ | ||
92 | struct GNUNET_NAMECACHE_Handle | ||
93 | { | ||
94 | /** | ||
95 | * Configuration to use. | ||
96 | */ | ||
97 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
98 | |||
99 | /** | ||
100 | * Message queue to service. | ||
101 | */ | ||
102 | struct GNUNET_MQ_Handle *mq; | ||
103 | |||
104 | /** | ||
105 | * Currently pending transmission request (or NULL). | ||
106 | */ | ||
107 | struct GNUNET_CLIENT_TransmitHandle *th; | ||
108 | |||
109 | /** | ||
110 | * Head of pending namecache queue entries | ||
111 | */ | ||
112 | struct GNUNET_NAMECACHE_QueueEntry *op_head; | ||
113 | |||
114 | /** | ||
115 | * Tail of pending namecache queue entries | ||
116 | */ | ||
117 | struct GNUNET_NAMECACHE_QueueEntry *op_tail; | ||
118 | |||
119 | /** | ||
120 | * Reconnect task | ||
121 | */ | ||
122 | struct GNUNET_SCHEDULER_Task *reconnect_task; | ||
123 | |||
124 | /** | ||
125 | * Delay introduced before we reconnect. | ||
126 | */ | ||
127 | struct GNUNET_TIME_Relative reconnect_delay; | ||
128 | |||
129 | /** | ||
130 | * Should we reconnect to service due to some serious error? | ||
131 | */ | ||
132 | int reconnect; | ||
133 | |||
134 | /** | ||
135 | * The last operation id used for a NAMECACHE operation | ||
136 | */ | ||
137 | uint32_t last_op_id_used; | ||
138 | }; | ||
139 | |||
140 | |||
141 | /** | ||
142 | * Disconnect from service and then reconnect. | ||
143 | * | ||
144 | * @param h our handle | ||
145 | */ | ||
146 | static void | ||
147 | force_reconnect (struct GNUNET_NAMECACHE_Handle *h); | ||
148 | |||
149 | |||
150 | /** | ||
151 | * Find queue entry for the given @a rid. | ||
152 | * | ||
153 | * @param h handle to search | ||
154 | * @param rid request ID to look for | ||
155 | * @return NULL if not found, otherwise the queue entry (removed from the queue) | ||
156 | */ | ||
157 | static struct GNUNET_NAMECACHE_QueueEntry * | ||
158 | find_qe (struct GNUNET_NAMECACHE_Handle *h, | ||
159 | uint32_t rid) | ||
160 | { | ||
161 | struct GNUNET_NAMECACHE_QueueEntry *qe; | ||
162 | |||
163 | for (qe = h->op_head; qe != NULL; qe = qe->next) | ||
164 | { | ||
165 | if (qe->op_id == rid) | ||
166 | { | ||
167 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
168 | h->op_tail, | ||
169 | qe); | ||
170 | return qe; | ||
171 | } | ||
172 | } | ||
173 | return NULL; | ||
174 | } | ||
175 | |||
176 | |||
177 | /** | ||
178 | * Handle an incoming message of type | ||
179 | * #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE. | ||
180 | * | ||
181 | * @param cls the `struct GNUNET_NAMECACHE_Handle` | ||
182 | * @param msg the message we received | ||
183 | */ | ||
184 | static int | ||
185 | check_lookup_block_response (void *cls, | ||
186 | const struct LookupBlockResponseMessage *msg) | ||
187 | { | ||
188 | /* any length will do, format validation is in handler */ | ||
189 | return GNUNET_OK; | ||
190 | } | ||
191 | |||
192 | |||
193 | /** | ||
194 | * Handle an incoming message of type | ||
195 | * #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE. | ||
196 | * | ||
197 | * @param cls the `struct GNUNET_NAMECACHE_Handle` | ||
198 | * @param msg the message we received | ||
199 | */ | ||
200 | static void | ||
201 | handle_lookup_block_response (void *cls, | ||
202 | const struct LookupBlockResponseMessage *msg) | ||
203 | { | ||
204 | struct GNUNET_NAMECACHE_Handle *h = cls; | ||
205 | size_t size; | ||
206 | struct GNUNET_NAMECACHE_QueueEntry *qe; | ||
207 | |||
208 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
209 | "Received LOOKUP_BLOCK_RESPONSE\n"); | ||
210 | qe = find_qe (h, | ||
211 | ntohl (msg->gns_header.r_id)); | ||
212 | if (NULL == qe) | ||
213 | return; | ||
214 | if (0 == GNUNET_TIME_absolute_ntoh (msg->expire).abs_value_us) | ||
215 | { | ||
216 | /* no match found */ | ||
217 | if (NULL != qe->block_proc) | ||
218 | qe->block_proc (qe->block_proc_cls, | ||
219 | NULL); | ||
220 | GNUNET_free (qe); | ||
221 | return; | ||
222 | } | ||
223 | size = ntohs (msg->gns_header.header.size) | ||
224 | - sizeof(struct LookupBlockResponseMessage); | ||
225 | { | ||
226 | char buf[size] GNUNET_ALIGN; | ||
227 | struct GNUNET_GNSRECORD_Block *block; | ||
228 | |||
229 | memset (buf, 0, size); | ||
230 | block = (struct GNUNET_GNSRECORD_Block *) buf; | ||
231 | GNUNET_memcpy (block, | ||
232 | &msg[1], | ||
233 | size); | ||
234 | if (GNUNET_OK != | ||
235 | GNUNET_GNSRECORD_block_verify (block)) | ||
236 | { | ||
237 | GNUNET_break (0); | ||
238 | if (NULL != qe->block_proc) | ||
239 | qe->block_proc (qe->block_proc_cls, | ||
240 | NULL); | ||
241 | force_reconnect (h); | ||
242 | } | ||
243 | else | ||
244 | { | ||
245 | if (NULL != qe->block_proc) | ||
246 | qe->block_proc (qe->block_proc_cls, | ||
247 | block); | ||
248 | } | ||
249 | } | ||
250 | GNUNET_free (qe); | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | ||
255 | * Handle an incoming message of type | ||
256 | * #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE | ||
257 | * | ||
258 | * @param cls the `struct GNUNET_NAMECACHE_Handle` | ||
259 | * @param msg the message we received | ||
260 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client | ||
261 | */ | ||
262 | static void | ||
263 | handle_block_cache_response (void *cls, | ||
264 | const struct BlockCacheResponseMessage *msg) | ||
265 | { | ||
266 | struct GNUNET_NAMECACHE_Handle *h = cls; | ||
267 | struct GNUNET_NAMECACHE_QueueEntry *qe; | ||
268 | int res; | ||
269 | |||
270 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
271 | "Received BLOCK_CACHE_RESPONSE\n"); | ||
272 | qe = find_qe (h, | ||
273 | ntohl (msg->gns_header.r_id)); | ||
274 | if (NULL == qe) | ||
275 | return; | ||
276 | res = ntohl (msg->op_result); | ||
277 | /* TODO: add actual error message from namecache to response... */ | ||
278 | if (NULL != qe->cont) | ||
279 | qe->cont (qe->cont_cls, | ||
280 | res, | ||
281 | (GNUNET_OK == res) | ||
282 | ? NULL | ||
283 | : _ ("Namecache failed to cache block")); | ||
284 | GNUNET_free (qe); | ||
285 | } | ||
286 | |||
287 | |||
288 | /** | ||
289 | * Generic error handler, called with the appropriate error code and | ||
290 | * the same closure specified at the creation of the message queue. | ||
291 | * Not every message queue implementation supports an error handler. | ||
292 | * | ||
293 | * @param cls closure with the `struct GNUNET_NAMECACHE_Handle *` | ||
294 | * @param error error code | ||
295 | */ | ||
296 | static void | ||
297 | mq_error_handler (void *cls, | ||
298 | enum GNUNET_MQ_Error error) | ||
299 | { | ||
300 | struct GNUNET_NAMECACHE_Handle *h = cls; | ||
301 | |||
302 | force_reconnect (h); | ||
303 | } | ||
304 | |||
305 | |||
306 | /** | ||
307 | * Reconnect to namecache service. | ||
308 | * | ||
309 | * @param h the handle to the NAMECACHE service | ||
310 | */ | ||
311 | static void | ||
312 | reconnect (struct GNUNET_NAMECACHE_Handle *h) | ||
313 | { | ||
314 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
315 | GNUNET_MQ_hd_var_size (lookup_block_response, | ||
316 | GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE, | ||
317 | struct LookupBlockResponseMessage, | ||
318 | h), | ||
319 | GNUNET_MQ_hd_fixed_size (block_cache_response, | ||
320 | GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE, | ||
321 | struct BlockCacheResponseMessage, | ||
322 | h), | ||
323 | GNUNET_MQ_handler_end () | ||
324 | }; | ||
325 | |||
326 | GNUNET_assert (NULL == h->mq); | ||
327 | h->mq = GNUNET_CLIENT_connect (h->cfg, | ||
328 | "namecache", | ||
329 | handlers, | ||
330 | &mq_error_handler, | ||
331 | h); | ||
332 | } | ||
333 | |||
334 | |||
335 | /** | ||
336 | * Re-establish the connection to the service. | ||
337 | * | ||
338 | * @param cls handle to use to re-connect. | ||
339 | */ | ||
340 | static void | ||
341 | reconnect_task (void *cls) | ||
342 | { | ||
343 | struct GNUNET_NAMECACHE_Handle *h = cls; | ||
344 | |||
345 | h->reconnect_task = NULL; | ||
346 | reconnect (h); | ||
347 | } | ||
348 | |||
349 | |||
350 | /** | ||
351 | * Disconnect from service and then reconnect. | ||
352 | * | ||
353 | * @param h our handle | ||
354 | */ | ||
355 | static void | ||
356 | force_reconnect (struct GNUNET_NAMECACHE_Handle *h) | ||
357 | { | ||
358 | struct GNUNET_NAMECACHE_QueueEntry *qe; | ||
359 | |||
360 | h->reconnect = GNUNET_NO; | ||
361 | GNUNET_MQ_destroy (h->mq); | ||
362 | h->mq = NULL; | ||
363 | while (NULL != (qe = h->op_head)) | ||
364 | { | ||
365 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
366 | h->op_tail, | ||
367 | qe); | ||
368 | if (NULL != qe->cont) | ||
369 | qe->cont (qe->cont_cls, | ||
370 | GNUNET_SYSERR, | ||
371 | _ ("Error communicating with namecache service")); | ||
372 | GNUNET_free (qe); | ||
373 | } | ||
374 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
375 | "Reconnecting to namecache\n"); | ||
376 | h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay); | ||
377 | h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, | ||
378 | &reconnect_task, | ||
379 | h); | ||
380 | } | ||
381 | |||
382 | |||
383 | /** | ||
384 | * Get a fresh operation id to distinguish between namecache requests | ||
385 | * | ||
386 | * @param h the namecache handle | ||
387 | * @return next operation id to use | ||
388 | */ | ||
389 | static uint32_t | ||
390 | get_op_id (struct GNUNET_NAMECACHE_Handle *h) | ||
391 | { | ||
392 | return h->last_op_id_used++; | ||
393 | } | ||
394 | |||
395 | |||
396 | /** | ||
397 | * Initialize the connection with the NAMECACHE service. | ||
398 | * | ||
399 | * @param cfg configuration to use | ||
400 | * @return handle to the GNS service, or NULL on error | ||
401 | */ | ||
402 | struct GNUNET_NAMECACHE_Handle * | ||
403 | GNUNET_NAMECACHE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
404 | { | ||
405 | struct GNUNET_NAMECACHE_Handle *h; | ||
406 | |||
407 | h = GNUNET_new (struct GNUNET_NAMECACHE_Handle); | ||
408 | h->cfg = cfg; | ||
409 | reconnect (h); | ||
410 | if (NULL == h->mq) | ||
411 | { | ||
412 | GNUNET_free (h); | ||
413 | return NULL; | ||
414 | } | ||
415 | return h; | ||
416 | } | ||
417 | |||
418 | |||
419 | /** | ||
420 | * Disconnect from the namecache service (and free associated | ||
421 | * resources). | ||
422 | * | ||
423 | * @param h handle to the namecache | ||
424 | */ | ||
425 | void | ||
426 | GNUNET_NAMECACHE_disconnect (struct GNUNET_NAMECACHE_Handle *h) | ||
427 | { | ||
428 | struct GNUNET_NAMECACHE_QueueEntry *q; | ||
429 | |||
430 | GNUNET_break (NULL == h->op_head); | ||
431 | while (NULL != (q = h->op_head)) | ||
432 | { | ||
433 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
434 | h->op_tail, | ||
435 | q); | ||
436 | GNUNET_free (q); | ||
437 | } | ||
438 | if (NULL != h->mq) | ||
439 | { | ||
440 | GNUNET_MQ_destroy (h->mq); | ||
441 | h->mq = NULL; | ||
442 | } | ||
443 | if (NULL != h->reconnect_task) | ||
444 | { | ||
445 | GNUNET_SCHEDULER_cancel (h->reconnect_task); | ||
446 | h->reconnect_task = NULL; | ||
447 | } | ||
448 | GNUNET_free (h); | ||
449 | } | ||
450 | |||
451 | |||
452 | /** | ||
453 | * Store an item in the namecache. If the item is already present, | ||
454 | * it is replaced with the new record. | ||
455 | * | ||
456 | * @param h handle to the namecache | ||
457 | * @param block block to store | ||
458 | * @param cont continuation to call when done | ||
459 | * @param cont_cls closure for @a cont | ||
460 | * @return handle to abort the request, NULL on error | ||
461 | */ | ||
462 | struct GNUNET_NAMECACHE_QueueEntry * | ||
463 | GNUNET_NAMECACHE_block_cache (struct GNUNET_NAMECACHE_Handle *h, | ||
464 | const struct GNUNET_GNSRECORD_Block *block, | ||
465 | GNUNET_NAMECACHE_ContinuationWithStatus cont, | ||
466 | void *cont_cls) | ||
467 | { | ||
468 | struct GNUNET_NAMECACHE_QueueEntry *qe; | ||
469 | struct BlockCacheMessage *msg; | ||
470 | struct GNUNET_MQ_Envelope *env; | ||
471 | uint32_t rid; | ||
472 | size_t blen; | ||
473 | |||
474 | if (NULL == h->mq) | ||
475 | return NULL; | ||
476 | blen = GNUNET_GNSRECORD_block_get_size (block); | ||
477 | rid = get_op_id (h); | ||
478 | qe = GNUNET_new (struct GNUNET_NAMECACHE_QueueEntry); | ||
479 | qe->nsh = h; | ||
480 | qe->cont = cont; | ||
481 | qe->cont_cls = cont_cls; | ||
482 | qe->op_id = rid; | ||
483 | GNUNET_CONTAINER_DLL_insert_tail (h->op_head, | ||
484 | h->op_tail, | ||
485 | qe); | ||
486 | /* send msg */ | ||
487 | env = GNUNET_MQ_msg_extra (msg, | ||
488 | blen, | ||
489 | GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE); | ||
490 | msg->gns_header.r_id = htonl (rid); | ||
491 | GNUNET_memcpy (&msg[1], | ||
492 | block, | ||
493 | blen); | ||
494 | GNUNET_MQ_send (h->mq, | ||
495 | env); | ||
496 | return qe; | ||
497 | } | ||
498 | |||
499 | |||
500 | /** | ||
501 | * Get a result for a particular key from the namecache. The processor | ||
502 | * will only be called once. | ||
503 | * | ||
504 | * @param h handle to the namecache | ||
505 | * @param derived_hash hash of zone key combined with name to lookup | ||
506 | * @param proc function to call on the matching block, or with | ||
507 | * NULL if there is no matching block | ||
508 | * @param proc_cls closure for @a proc | ||
509 | * @return a handle that can be used to cancel, NULL on error | ||
510 | */ | ||
511 | struct GNUNET_NAMECACHE_QueueEntry * | ||
512 | GNUNET_NAMECACHE_lookup_block (struct GNUNET_NAMECACHE_Handle *h, | ||
513 | const struct GNUNET_HashCode *derived_hash, | ||
514 | GNUNET_NAMECACHE_BlockProcessor proc, | ||
515 | void *proc_cls) | ||
516 | { | ||
517 | struct GNUNET_NAMECACHE_QueueEntry *qe; | ||
518 | struct LookupBlockMessage *msg; | ||
519 | struct GNUNET_MQ_Envelope *env; | ||
520 | uint32_t rid; | ||
521 | |||
522 | if (NULL == h->mq) | ||
523 | return NULL; | ||
524 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
525 | "Looking for block under %s\n", | ||
526 | GNUNET_h2s (derived_hash)); | ||
527 | rid = get_op_id (h); | ||
528 | qe = GNUNET_new (struct GNUNET_NAMECACHE_QueueEntry); | ||
529 | qe->nsh = h; | ||
530 | qe->block_proc = proc; | ||
531 | qe->block_proc_cls = proc_cls; | ||
532 | qe->op_id = rid; | ||
533 | GNUNET_CONTAINER_DLL_insert_tail (h->op_head, | ||
534 | h->op_tail, | ||
535 | qe); | ||
536 | env = GNUNET_MQ_msg (msg, | ||
537 | GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK); | ||
538 | msg->gns_header.r_id = htonl (rid); | ||
539 | msg->query = *derived_hash; | ||
540 | GNUNET_MQ_send (h->mq, | ||
541 | env); | ||
542 | return qe; | ||
543 | } | ||
544 | |||
545 | |||
546 | /** | ||
547 | * Cancel a namecache operation. The final callback from the | ||
548 | * operation must not have been done yet. | ||
549 | * | ||
550 | * @param qe operation to cancel | ||
551 | */ | ||
552 | void | ||
553 | GNUNET_NAMECACHE_cancel (struct GNUNET_NAMECACHE_QueueEntry *qe) | ||
554 | { | ||
555 | struct GNUNET_NAMECACHE_Handle *h = qe->nsh; | ||
556 | |||
557 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
558 | h->op_tail, | ||
559 | qe); | ||
560 | GNUNET_free (qe); | ||
561 | } | ||
562 | |||
563 | |||
564 | /* end of namecache_api.c */ | ||
diff --git a/src/service/namecache/test_namecache_api.conf b/src/service/namecache/test_namecache_api.conf new file mode 100644 index 000000000..b1ae31f14 --- /dev/null +++ b/src/service/namecache/test_namecache_api.conf | |||
@@ -0,0 +1,13 @@ | |||
1 | [PATHS] | ||
2 | GNUNET_TEST_HOME = $GNUNET_TMP/test-gnunet-namecache/ | ||
3 | |||
4 | [namecache] | ||
5 | DATABASE = sqlite | ||
6 | |||
7 | [namecache-sqlite] | ||
8 | FILENAME = $GNUNET_TEST_HOME/namecache/sqlite_test.db | ||
9 | |||
10 | [namecache-postgres] | ||
11 | CONFIG = connect_timeout=10; dbname=gnunetcheck | ||
12 | TEMPORARY_TABLE = YES | ||
13 | |||
diff --git a/src/service/namecache/test_namecache_api_cache_block.c b/src/service/namecache/test_namecache_api_cache_block.c new file mode 100644 index 000000000..3a18a72e1 --- /dev/null +++ b/src/service/namecache/test_namecache_api_cache_block.c | |||
@@ -0,0 +1,244 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012 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 namecache/test_namecache_api.c | ||
22 | * @brief testcase for namecache_api.c: store a record and perform a lookup | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_namecache_service.h" | ||
26 | #include "gnunet_testing_lib.h" | ||
27 | |||
28 | #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT | ||
29 | |||
30 | #define TEST_RECORD_DATALEN 123 | ||
31 | |||
32 | #define TEST_RECORD_DATA 'a' | ||
33 | |||
34 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100) | ||
35 | |||
36 | |||
37 | static struct GNUNET_NAMECACHE_Handle *nsh; | ||
38 | |||
39 | static struct GNUNET_SCHEDULER_Task *endbadly_task; | ||
40 | |||
41 | static struct GNUNET_CRYPTO_PrivateKey privkey; | ||
42 | |||
43 | static struct GNUNET_CRYPTO_PublicKey pubkey; | ||
44 | |||
45 | static int res; | ||
46 | |||
47 | static struct GNUNET_NAMECACHE_QueueEntry *nsqe; | ||
48 | |||
49 | |||
50 | static void | ||
51 | cleanup () | ||
52 | { | ||
53 | if (NULL != nsh) | ||
54 | { | ||
55 | GNUNET_NAMECACHE_disconnect (nsh); | ||
56 | nsh = NULL; | ||
57 | } | ||
58 | GNUNET_SCHEDULER_shutdown (); | ||
59 | } | ||
60 | |||
61 | |||
62 | /** | ||
63 | * Re-establish the connection to the service. | ||
64 | * | ||
65 | * @param cls handle to use to re-connect. | ||
66 | */ | ||
67 | static void | ||
68 | endbadly (void *cls) | ||
69 | { | ||
70 | if (NULL != nsqe) | ||
71 | { | ||
72 | GNUNET_NAMECACHE_cancel (nsqe); | ||
73 | nsqe = NULL; | ||
74 | } | ||
75 | cleanup (); | ||
76 | res = 1; | ||
77 | } | ||
78 | |||
79 | |||
80 | static void | ||
81 | end (void *cls) | ||
82 | { | ||
83 | cleanup (); | ||
84 | res = 0; | ||
85 | } | ||
86 | |||
87 | |||
88 | static void | ||
89 | rd_decrypt_cb (void *cls, | ||
90 | unsigned int rd_count, | ||
91 | const struct GNUNET_GNSRECORD_Data *rd) | ||
92 | { | ||
93 | char rd_cmp_data[TEST_RECORD_DATALEN]; | ||
94 | |||
95 | GNUNET_assert (1 == rd_count); | ||
96 | GNUNET_assert (NULL != rd); | ||
97 | |||
98 | memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN); | ||
99 | |||
100 | GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type); | ||
101 | GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size); | ||
102 | GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN)); | ||
103 | |||
104 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
105 | "Block was decrypted successfully \n"); | ||
106 | |||
107 | GNUNET_SCHEDULER_add_now (&end, NULL); | ||
108 | } | ||
109 | |||
110 | |||
111 | static void | ||
112 | name_lookup_proc (void *cls, | ||
113 | const struct GNUNET_GNSRECORD_Block *block) | ||
114 | { | ||
115 | const char *name = cls; | ||
116 | |||
117 | nsqe = NULL; | ||
118 | |||
119 | GNUNET_assert (NULL != cls); | ||
120 | |||
121 | if (endbadly_task != NULL) | ||
122 | { | ||
123 | GNUNET_SCHEDULER_cancel (endbadly_task); | ||
124 | endbadly_task = NULL; | ||
125 | } | ||
126 | |||
127 | if (NULL == block) | ||
128 | { | ||
129 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
130 | _ ("Namecache returned no block\n")); | ||
131 | if (NULL != endbadly_task) | ||
132 | GNUNET_SCHEDULER_cancel (endbadly_task); | ||
133 | endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL); | ||
134 | return; | ||
135 | } | ||
136 | |||
137 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
138 | "Namecache returned block, decrypting \n"); | ||
139 | GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt (block, | ||
140 | &pubkey, name, | ||
141 | &rd_decrypt_cb, | ||
142 | (void *) name)); | ||
143 | } | ||
144 | |||
145 | |||
146 | static void | ||
147 | cache_cont (void *cls, int32_t success, const char *emsg) | ||
148 | { | ||
149 | const char *name = cls; | ||
150 | struct GNUNET_HashCode derived_hash; | ||
151 | |||
152 | GNUNET_assert (NULL != cls); | ||
153 | |||
154 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
155 | "Name store cached record for `%s': %s\n", | ||
156 | name, | ||
157 | (success == GNUNET_OK) ? "SUCCESS" : "FAIL"); | ||
158 | |||
159 | /* Create derived hash */ | ||
160 | GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &derived_hash); | ||
161 | |||
162 | nsqe = GNUNET_NAMECACHE_lookup_block (nsh, &derived_hash, | ||
163 | &name_lookup_proc, (void *) name); | ||
164 | } | ||
165 | |||
166 | |||
167 | static void | ||
168 | run (void *cls, | ||
169 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
170 | struct GNUNET_TESTING_Peer *peer) | ||
171 | { | ||
172 | struct GNUNET_GNSRECORD_Data rd; | ||
173 | struct GNUNET_GNSRECORD_Block *block; | ||
174 | const char *name = "dummy.dummy.gnunet"; | ||
175 | |||
176 | endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, | ||
177 | &endbadly, NULL); | ||
178 | privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); | ||
179 | GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key); | ||
180 | GNUNET_CRYPTO_key_get_public (&privkey, &pubkey); | ||
181 | |||
182 | |||
183 | rd.expiration_time = GNUNET_TIME_absolute_get ().abs_value_us + 10000000000; | ||
184 | rd.record_type = TEST_RECORD_TYPE; | ||
185 | rd.data_size = TEST_RECORD_DATALEN; | ||
186 | rd.data = GNUNET_malloc (TEST_RECORD_DATALEN); | ||
187 | rd.flags = 0; | ||
188 | memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN); | ||
189 | GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_create (&privkey, | ||
190 | GNUNET_TIME_UNIT_FOREVER_ABS, | ||
191 | name, &rd, 1, | ||
192 | &block)); | ||
193 | if (NULL == block) | ||
194 | { | ||
195 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
196 | "Namecache cannot cache no block!\n"); | ||
197 | GNUNET_SCHEDULER_shutdown (); | ||
198 | GNUNET_free (block); | ||
199 | return; | ||
200 | } | ||
201 | |||
202 | nsh = GNUNET_NAMECACHE_connect (cfg); | ||
203 | if (NULL == nsh) | ||
204 | { | ||
205 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
206 | _ ("Namecache cannot connect to namecache\n")); | ||
207 | GNUNET_SCHEDULER_shutdown (); | ||
208 | GNUNET_free (block); | ||
209 | return; | ||
210 | } | ||
211 | GNUNET_break (NULL != nsh); | ||
212 | |||
213 | nsqe = GNUNET_NAMECACHE_block_cache (nsh, | ||
214 | block, | ||
215 | &cache_cont, (void *) name); | ||
216 | if (NULL == nsqe) | ||
217 | { | ||
218 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
219 | _ ("Namecache cannot cache no block\n")); | ||
220 | } | ||
221 | GNUNET_free (block); | ||
222 | GNUNET_free_nz ((void *) rd.data); | ||
223 | } | ||
224 | |||
225 | |||
226 | int | ||
227 | main (int argc, char *argv[]) | ||
228 | { | ||
229 | GNUNET_DISK_purge_cfg_dir | ||
230 | ("test_namecache_api.conf", | ||
231 | "GNUNET_TEST_HOME"); | ||
232 | res = 1; | ||
233 | if (0 != | ||
234 | GNUNET_TESTING_service_run ("test-namecache-api", | ||
235 | "namecache", | ||
236 | "test_namecache_api.conf", | ||
237 | &run, | ||
238 | NULL)) | ||
239 | return 1; | ||
240 | return res; | ||
241 | } | ||
242 | |||
243 | |||
244 | /* end of test_namecache_api_cache_block.c */ | ||