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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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_namecache_service.h"
31 #include "gnunet_namecache_plugin.h"
32 #include "gnunet_signatures.h"
33 #include "namecache.h"
35 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
41 struct NamecacheClient
44 * Next element in the DLL
46 struct NamecacheClient *next;
49 * Previous element in the DLL
51 struct NamecacheClient *prev;
56 struct GNUNET_SERVER_Client *client;
62 * Configuration handle.
64 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
69 static struct GNUNET_NAMECACHE_PluginFunctions *GSN_database;
72 * Name of the database plugin
74 static char *db_lib_name;
77 * Our notification context.
79 static struct GNUNET_SERVER_NotificationContext *snc;
82 * Head of the Client DLL
84 static struct NamecacheClient *client_head;
87 * Tail of the Client DLL
89 static struct NamecacheClient *client_tail;
92 * Notification context shared by all monitors.
94 static struct GNUNET_SERVER_NotificationContext *monitor_nc;
99 * Task run during shutdown.
104 cleanup_task (void *cls)
106 struct NamecacheClient *nc;
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109 "Stopping namecache service\n");
112 GNUNET_SERVER_notification_context_destroy (snc);
115 while (NULL != (nc = client_head))
117 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
118 GNUNET_SERVER_client_set_user_context (nc->client, NULL);
121 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
122 GNUNET_free (db_lib_name);
124 if (NULL != monitor_nc)
126 GNUNET_SERVER_notification_context_destroy (monitor_nc);
133 * Called whenever a client is disconnected.
134 * Frees our resources associated with that client.
137 * @param client identification of the client
140 client_disconnect_notification (void *cls,
141 struct GNUNET_SERVER_Client *client)
143 struct NamecacheClient *nc;
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Client %p disconnected\n",
150 if (NULL == (nc = GNUNET_SERVER_client_get_user_context (client, struct NamecacheClient)))
152 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
158 * Add a client to our list of active clients, if it is not yet
161 * @param client client to add
162 * @return internal namecache client structure for this client
164 static struct NamecacheClient *
165 client_lookup (struct GNUNET_SERVER_Client *client)
167 struct NamecacheClient *nc;
169 nc = GNUNET_SERVER_client_get_user_context (client, struct NamecacheClient);
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Client %p connected\n",
175 nc = GNUNET_new (struct NamecacheClient);
177 GNUNET_SERVER_notification_context_add (snc, client);
178 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, nc);
179 GNUNET_SERVER_client_set_user_context (client, nc);
185 * Context for name lookups passed from #handle_lookup_block to
186 * #handle_lookup_block_it as closure
188 struct LookupBlockContext
191 * The client to send the response to
193 struct NamecacheClient *nc;
196 * Operation id for the name lookup
204 * A #GNUNET_NAMECACHE_BlockCallback for name lookups in #handle_lookup_block
206 * @param cls a `struct LookupNameContext *` with information about the request
207 * @param block the block
210 handle_lookup_block_it (void *cls,
211 const struct GNUNET_GNSRECORD_Block *block)
213 struct LookupBlockContext *lnc = cls;
214 struct LookupBlockResponseMessage *r;
217 esize = ntohl (block->purpose.size)
218 - sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)
219 - sizeof (struct GNUNET_TIME_AbsoluteNBO);
220 r = GNUNET_malloc (sizeof (struct LookupBlockResponseMessage) + esize);
221 r->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE);
222 r->gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage) + esize);
223 r->gns_header.r_id = htonl (lnc->request_id);
224 r->expire = block->expiration_time;
225 r->signature = block->signature;
226 r->derived_key = block->derived_key;
227 memcpy (&r[1], &block[1], esize);
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229 "Sending `%s' message with expiration time %s\n",
230 "NAMECACHE_LOOKUP_BLOCK_RESPONSE",
231 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (r->expire)));
232 GNUNET_SERVER_notification_context_unicast (snc,
234 &r->gns_header.header,
241 * Handles a #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK message
244 * @param client client sending the message
245 * @param message message of type 'struct LookupNameMessage'
248 handle_lookup_block (void *cls,
249 struct GNUNET_SERVER_Client *client,
250 const struct GNUNET_MessageHeader *message)
252 const struct LookupBlockMessage *ln_msg;
253 struct LookupBlockContext lnc;
254 struct NamecacheClient *nc;
255 struct LookupBlockResponseMessage zir_end;
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Received `%s' message\n",
260 "NAMECACHE_LOOKUP_BLOCK");
261 nc = client_lookup(client);
262 ln_msg = (const struct LookupBlockMessage *) message;
263 lnc.request_id = ntohl (ln_msg->gns_header.r_id);
266 (ret = GSN_database->lookup_block (GSN_database->cls,
268 &handle_lookup_block_it, &lnc)))
270 /* internal error (in database plugin); might be best to just hang up on
271 plugin rather than to signal that there are 'no' results, which
272 might also be false... */
274 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
279 /* no records match at all, generate empty response */
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Sending empty `%s' message\n",
282 "NAMECACHE_LOOKUP_BLOCK_RESPONSE");
283 memset (&zir_end, 0, sizeof (zir_end));
284 zir_end.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE);
285 zir_end.gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage));
286 zir_end.gns_header.r_id = ln_msg->gns_header.r_id;
287 GNUNET_SERVER_notification_context_unicast (snc,
289 &zir_end.gns_header.header,
293 GNUNET_SERVER_receive_done (client, GNUNET_OK);
298 * Handles a #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE message
301 * @param client client sending the message
302 * @param message message of type 'struct BlockCacheMessage'
305 handle_block_cache (void *cls,
306 struct GNUNET_SERVER_Client *client,
307 const struct GNUNET_MessageHeader *message)
309 struct NamecacheClient *nc;
310 const struct BlockCacheMessage *rp_msg;
311 struct BlockCacheResponseMessage rpr_msg;
312 struct GNUNET_GNSRECORD_Block *block;
316 nc = client_lookup (client);
317 if (ntohs (message->size) < sizeof (struct BlockCacheMessage))
320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
323 rp_msg = (const struct BlockCacheMessage *) message;
324 esize = ntohs (rp_msg->gns_header.header.size) - sizeof (struct BlockCacheMessage);
325 block = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Block) + esize);
326 block->signature = rp_msg->signature;
327 block->derived_key = rp_msg->derived_key;
328 block->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
329 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
331 block->expiration_time = rp_msg->expire;
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Received `%s' message with expiration time %s\n",
334 "NAMECACHE_BLOCK_CACHE",
335 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (block->expiration_time)));
336 memcpy (&block[1], &rp_msg[1], esize);
337 res = GSN_database->cache_block (GSN_database->cls,
341 rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE);
342 rpr_msg.gns_header.header.size = htons (sizeof (struct BlockCacheResponseMessage));
343 rpr_msg.gns_header.r_id = rp_msg->gns_header.r_id;
344 rpr_msg.op_result = htonl (res);
345 GNUNET_SERVER_notification_context_unicast (snc,
347 &rpr_msg.gns_header.header,
349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
354 * Process namecache requests.
357 * @param server the initialized server
358 * @param cfg configuration to use
361 run (void *cls, struct GNUNET_SERVER_Handle *server,
362 const struct GNUNET_CONFIGURATION_Handle *cfg)
364 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
365 {&handle_lookup_block, NULL,
366 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK, sizeof (struct LookupBlockMessage)},
367 {&handle_block_cache, NULL,
368 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE, 0},
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namecache service\n");
375 monitor_nc = GNUNET_SERVER_notification_context_create (server, 1);
377 /* Loading database plugin */
379 GNUNET_CONFIGURATION_get_value_string (cfg, "namecache", "database",
381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
383 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namecache_%s", database);
384 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
385 GNUNET_free (database);
386 if (NULL == GSN_database)
388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
389 "Could not load database backend `%s'\n",
391 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
395 /* Configuring server handles */
396 GNUNET_SERVER_add_handlers (server, handlers);
397 snc = GNUNET_SERVER_notification_context_create (server, 16);
398 GNUNET_SERVER_disconnect_notify (server,
399 &client_disconnect_notification,
401 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
407 * The main function for the template service.
409 * @param argc number of arguments from the command line
410 * @param argv command line arguments
411 * @return 0 ok, 1 on error
414 main (int argc, char *const *argv)
417 GNUNET_SERVICE_run (argc, argv, "namecache",
418 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
421 /* end of gnunet-service-namecache.c */