2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file namecache/gnunet-service-namecache.c
23 * @brief namecache for the GNUnet naming system
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dnsparser_lib.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_namecache_service.h"
32 #include "gnunet_namecache_plugin.h"
33 #include "gnunet_signatures.h"
34 #include "namecache.h"
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
42 struct NamecacheClient
48 struct GNUNET_SERVICE_Client *client;
51 * The message queue to talk to @e client.
53 struct GNUNET_MQ_Handle *mq;
59 * Configuration handle.
61 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
64 * Handle to the statistics service
66 static struct GNUNET_STATISTICS_Handle *statistics;
71 static struct GNUNET_NAMECACHE_PluginFunctions *GSN_database;
74 * Name of the database plugin
76 static char *db_lib_name;
80 * Task run during shutdown.
85 cleanup_task (void *cls)
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88 "Stopping namecache service\n");
90 GNUNET_PLUGIN_unload (db_lib_name,
92 GNUNET_free (db_lib_name);
94 if (NULL != statistics)
96 GNUNET_STATISTICS_destroy (statistics,
104 * Called whenever a client is disconnected.
105 * Frees our resources associated with that client.
108 * @param client identification of the client
109 * @param app_ctx the `struct NamecacheClient` for this @a client
112 client_disconnect_cb (void *cls,
113 struct GNUNET_SERVICE_Client *client,
116 struct NamecacheClient *nc = app_ctx;
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119 "Client %p disconnected\n",
126 * Add a client to our list of active clients.
129 * @param client client to add
130 * @param mq queue to talk to @a client
131 * @return internal namecache client structure for this client
134 client_connect_cb (void *cls,
135 struct GNUNET_SERVICE_Client *client,
136 struct GNUNET_MQ_Handle *mq)
138 struct NamecacheClient *nc;
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141 "Client %p connected\n",
143 nc = GNUNET_new (struct NamecacheClient);
151 * Context for name lookups passed from #handle_lookup_block to
152 * #handle_lookup_block_it as closure
154 struct LookupBlockContext
157 * The client to send the response to
159 struct NamecacheClient *nc;
162 * Operation id for the name lookup
174 * A #GNUNET_NAMECACHE_BlockCallback for name lookups in #handle_lookup_block
176 * @param cls a `struct LookupNameContext *` with information about the request
177 * @param block the block
180 handle_lookup_block_it (void *cls,
181 const struct GNUNET_GNSRECORD_Block *block)
183 struct LookupBlockContext *lnc = cls;
184 struct GNUNET_MQ_Envelope *env;
185 struct LookupBlockResponseMessage *r;
189 bsize = ntohl (block->purpose.size);
191 (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + sizeof (struct GNUNET_TIME_AbsoluteNBO)))
193 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
195 lnc->status = GNUNET_SYSERR;
198 esize = ntohl (block->purpose.size)
199 - sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)
200 - sizeof (struct GNUNET_TIME_AbsoluteNBO);
201 env = GNUNET_MQ_msg_extra (r,
203 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE);
204 r->gns_header.r_id = htonl (lnc->request_id);
205 r->expire = block->expiration_time;
206 r->signature = block->signature;
207 r->derived_key = block->derived_key;
208 GNUNET_memcpy (&r[1],
211 GNUNET_STATISTICS_update (statistics,
212 "blocks found in cache",
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Sending NAMECACHE_LOOKUP_BLOCK_RESPONSE message with expiration time %s\n",
217 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (r->expire)));
218 GNUNET_MQ_send (lnc->nc->mq,
224 * Handles a #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK message
226 * @param cls a `struct NamecacheClient *`
227 * @param the inbound message
230 handle_lookup_block (void *cls,
231 const struct LookupBlockMessage *ln_msg)
233 struct NamecacheClient *nc = cls;
234 struct GNUNET_MQ_Envelope *env;
235 struct LookupBlockContext lnc;
236 struct LookupBlockResponseMessage *zir_end;
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "Received NAMECACHE_LOOKUP_BLOCK message\n");
241 GNUNET_STATISTICS_update (statistics,
245 lnc.request_id = ntohl (ln_msg->gns_header.r_id);
247 lnc.status = GNUNET_OK;
249 (ret = GSN_database->lookup_block (GSN_database->cls,
251 &handle_lookup_block_it,
254 /* internal error (in database plugin); might be best to just hang up on
255 plugin rather than to signal that there are 'no' results, which
256 might also be false... */
258 GNUNET_SERVICE_client_drop (nc->client);
261 if ((0 == ret) || (GNUNET_SYSERR == lnc.status))
263 /* no records match at all, generate empty response */
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Sending empty NAMECACHE_LOOKUP_BLOCK_RESPONSE message\n");
266 env = GNUNET_MQ_msg (zir_end,
267 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE);
268 zir_end->gns_header.r_id = ln_msg->gns_header.r_id;
269 GNUNET_MQ_send (nc->mq,
272 GNUNET_SERVICE_client_continue (nc->client);
277 * Check a #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE message
279 * @param cls our `struct NamecacheClient`
280 * @param rp_msg message to process
281 * @return #GNUNET_OK (always fine)
284 check_block_cache (void *cls,
285 const struct BlockCacheMessage *rp_msg)
292 * Handles a #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE message
294 * @param cls our `struct NamecacheClient`
295 * @param rp_msg message to process
298 handle_block_cache (void *cls,
299 const struct BlockCacheMessage *rp_msg)
301 struct NamecacheClient *nc = cls;
302 struct GNUNET_MQ_Envelope *env;
303 struct BlockCacheResponseMessage *rpr_msg;
304 struct GNUNET_GNSRECORD_Block *block;
308 GNUNET_STATISTICS_update (statistics,
312 esize = ntohs (rp_msg->gns_header.header.size) - sizeof (struct BlockCacheMessage);
313 block = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Block) + esize);
314 block->signature = rp_msg->signature;
315 block->derived_key = rp_msg->derived_key;
316 block->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
317 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
319 block->expiration_time = rp_msg->expire;
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Received NAMECACHE_BLOCK_CACHE message with expiration time %s\n",
322 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (block->expiration_time)));
323 GNUNET_memcpy (&block[1],
326 res = GSN_database->cache_block (GSN_database->cls,
329 env = GNUNET_MQ_msg (rpr_msg,
330 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE);
331 rpr_msg->gns_header.r_id = rp_msg->gns_header.r_id;
332 rpr_msg->op_result = htonl (res);
333 GNUNET_MQ_send (nc->mq,
335 GNUNET_SERVICE_client_continue (nc->client);
340 * Process namecache requests.
343 * @param cfg configuration to use
344 * @param service the initialized service
348 const struct GNUNET_CONFIGURATION_Handle *cfg,
349 struct GNUNET_SERVICE_Handle *service)
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "Starting namecache service\n");
357 /* Loading database plugin */
359 GNUNET_CONFIGURATION_get_value_string (cfg,
363 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
364 "No database backend configured\n");
366 GNUNET_asprintf (&db_lib_name,
367 "libgnunet_plugin_namecache_%s",
369 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
371 GNUNET_free (database);
372 if (NULL == GSN_database)
374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
375 "Could not load database backend `%s'\n",
377 GNUNET_SCHEDULER_add_now (&cleanup_task,
381 statistics = GNUNET_STATISTICS_create ("namecache",
384 /* Configuring server handles */
385 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
391 * Define "main" method using service macro.
395 GNUNET_SERVICE_OPTION_NONE,
398 &client_disconnect_cb,
400 GNUNET_MQ_hd_fixed_size (lookup_block,
401 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK,
402 struct LookupBlockMessage,
404 GNUNET_MQ_hd_var_size (block_cache,
405 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE,
406 struct BlockCacheMessage,
408 GNUNET_MQ_handler_end ());
411 /* end of gnunet-service-namecache.c */