2 This file is part of GNUnet.
3 Copyright (C) 2014, 2015 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 peerstore/gnunet-service-peerstore.c
23 * @brief peerstore service implementation
24 * @author Omar Tarabai
27 #include "gnunet_util_lib.h"
28 #include "peerstore.h"
29 #include "gnunet_peerstore_plugin.h"
30 #include "peerstore_common.h"
33 * Connected client entry
40 struct ClientEntry *next;
45 struct ClientEntry *prev;
48 * Corresponding server handle.
50 struct GNUNET_SERVER_Client *client;
54 * Interval for expired records cleanup (in seconds)
56 #define EXPIRED_RECORDS_CLEANUP_INTERVAL 300 /* 5mins */
61 static const struct GNUNET_CONFIGURATION_Handle *cfg;
64 * Database plugin library name
66 static char *db_lib_name;
71 static struct GNUNET_PEERSTORE_PluginFunctions *db;
74 * Hashmap with all watch requests
76 static struct GNUNET_CONTAINER_MultiHashMap *watchers;
79 * Our notification context.
81 static struct GNUNET_SERVER_NotificationContext *nc;
84 * Head of linked list of connected clients
86 static struct ClientEntry *client_head;
89 * Tail of linked list of connected clients
91 static struct ClientEntry *client_tail;
94 * Task run to clean up expired records.
96 static struct GNUNET_SCHEDULER_Task *expire_task;
99 * Are we in the process of shutting down the service? #GNUNET_YES / #GNUNET_NO
101 static int in_shutdown;
104 * Perform the actual shutdown operations
109 if (NULL != db_lib_name)
111 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
112 GNUNET_free (db_lib_name);
117 GNUNET_SERVER_notification_context_destroy (nc);
120 if (NULL != watchers)
122 GNUNET_CONTAINER_multihashmap_destroy (watchers);
125 if (NULL != expire_task)
127 GNUNET_SCHEDULER_cancel (expire_task);
130 GNUNET_SCHEDULER_shutdown ();
135 * Task run during shutdown.
140 shutdown_task (void *cls)
142 in_shutdown = GNUNET_YES;
143 if (NULL == client_head) /* Only when no connected clients. */
148 /* Forward declaration */
150 expire_records_continuation (void *cls, int success);
154 * Deletes any expired records from storage
157 cleanup_expired_records (void *cls)
162 GNUNET_assert (NULL != db);
163 ret = db->expire_records (db->cls, GNUNET_TIME_absolute_get (),
164 &expire_records_continuation, NULL);
165 if (GNUNET_OK != ret)
167 GNUNET_assert (NULL == expire_task);
168 expire_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
169 (GNUNET_TIME_UNIT_SECONDS,
170 EXPIRED_RECORDS_CLEANUP_INTERVAL),
171 &cleanup_expired_records, NULL);
177 * Continuation to expire_records called by the peerstore plugin
180 * @param success count of records deleted or #GNUNET_SYSERR
183 expire_records_continuation (void *cls,
187 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
188 "%d records expired.\n",
190 GNUNET_assert (NULL == expire_task);
191 expire_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
192 (GNUNET_TIME_UNIT_SECONDS,
193 EXPIRED_RECORDS_CLEANUP_INTERVAL),
194 &cleanup_expired_records, NULL);
199 * Search for a disconnected client and remove it
201 * @param cls closuer, a 'struct GNUNET_PEERSTORE_Record *'
202 * @param key hash of record key
203 * @param value the watcher client, a 'struct GNUNET_SERVER_Client *'
204 * @return #GNUNET_OK to continue iterating
207 client_disconnect_it (void *cls, const struct GNUNET_HashCode *key, void *value)
210 GNUNET_CONTAINER_multihashmap_remove (watchers, key, value);
216 * A client disconnected. Remove all of its data structure entries.
218 * @param cls closure, NULL
219 * @param client identification of the client
222 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
224 struct ClientEntry *ce;
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A client disconnected, cleaning up.\n");
227 if (NULL != watchers)
228 GNUNET_CONTAINER_multihashmap_iterate (watchers, &client_disconnect_it,
233 if (ce->client == client)
235 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
241 if (NULL == client_head && in_shutdown)
247 * Function called by for each matching record.
250 * @param record peerstore record found
251 * @param emsg error message or NULL if no errors
252 * @return #GNUNET_YES to continue iteration
255 record_iterator (void *cls, const struct GNUNET_PEERSTORE_Record *record,
258 struct GNUNET_PEERSTORE_Record *cls_record = cls;
259 struct StoreRecordMessage *srm;
263 /* No more records */
264 struct GNUNET_MessageHeader endmsg;
266 endmsg.size = htons (sizeof (struct GNUNET_MessageHeader));
267 endmsg.type = htons (GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
268 GNUNET_SERVER_notification_context_unicast (nc, cls_record->client, &endmsg,
270 GNUNET_SERVER_receive_done (cls_record->client,
271 NULL == emsg ? GNUNET_OK : GNUNET_SYSERR);
272 PEERSTORE_destroy_record (cls_record);
277 PEERSTORE_create_record_message (record->sub_system, record->peer,
278 record->key, record->value,
279 record->value_size, record->expiry,
280 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
281 GNUNET_SERVER_notification_context_unicast (nc, cls_record->client,
282 (struct GNUNET_MessageHeader *)
290 * Iterator over all watcher clients
291 * to notify them of a new record
293 * @param cls closuer, a 'struct GNUNET_PEERSTORE_Record *'
294 * @param key hash of record key
295 * @param value the watcher client, a 'struct GNUNET_SERVER_Client *'
296 * @return #GNUNET_YES to continue iterating
299 watch_notifier_it (void *cls, const struct GNUNET_HashCode *key, void *value)
301 struct GNUNET_PEERSTORE_Record *record = cls;
302 struct GNUNET_SERVER_Client *client = value;
303 struct StoreRecordMessage *srm;
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found a watcher to update.\n");
307 PEERSTORE_create_record_message (record->sub_system, record->peer,
308 record->key, record->value,
309 record->value_size, record->expiry,
310 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD);
311 GNUNET_SERVER_notification_context_unicast (nc, client,
312 (const struct GNUNET_MessageHeader
320 * Given a new record, notifies watchers
322 * @param record changed record to update watchers with
325 watch_notifier (struct GNUNET_PEERSTORE_Record *record)
327 struct GNUNET_HashCode keyhash;
329 PEERSTORE_hash_key (record->sub_system, record->peer, record->key, &keyhash);
330 GNUNET_CONTAINER_multihashmap_get_multiple (watchers, &keyhash,
331 &watch_notifier_it, record);
336 * Handle a watch cancel request from client
339 * @param client identification of the client
340 * @param message the actual message
343 handle_watch_cancel (void *cls, struct GNUNET_SERVER_Client *client,
344 const struct GNUNET_MessageHeader *message)
346 struct StoreKeyHashMessage *hm;
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a watch cancel request.\n");
349 hm = (struct StoreKeyHashMessage *) message;
350 GNUNET_CONTAINER_multihashmap_remove (watchers, &hm->keyhash, client);
351 GNUNET_SERVER_receive_done (client, GNUNET_OK);
356 * Handle a watch request from client
359 * @param client identification of the client
360 * @param message the actual message
363 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
364 const struct GNUNET_MessageHeader *message)
366 struct StoreKeyHashMessage *hm;
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a watch request.\n");
369 hm = (struct StoreKeyHashMessage *) message;
370 GNUNET_SERVER_client_mark_monitor (client);
371 GNUNET_SERVER_notification_context_add (nc, client);
372 GNUNET_CONTAINER_multihashmap_put (watchers, &hm->keyhash, client,
373 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
374 GNUNET_SERVER_receive_done (client, GNUNET_OK);
379 * Handle an iterate request from client
382 * @param client identification of the client
383 * @param message the actual message
386 handle_iterate (void *cls, struct GNUNET_SERVER_Client *client,
387 const struct GNUNET_MessageHeader *message)
389 struct GNUNET_PEERSTORE_Record *record;
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request.\n");
392 record = PEERSTORE_parse_record_message (message);
395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Malformed iterate request.\n"));
396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
399 if (NULL == record->sub_system)
401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
402 _("Sub system not supplied in client iterate request.\n"));
403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
404 PEERSTORE_destroy_record (record);
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Iterate request: ss `%s', peer `%s', key `%s'\n",
410 (NULL == record->peer) ? "NULL" : GNUNET_i2s (record->peer),
411 (NULL == record->key) ? "NULL" : record->key);
412 GNUNET_SERVER_notification_context_add (nc, client);
413 record->client = client;
415 db->iterate_records (db->cls, record->sub_system, record->peer,
416 record->key, &record_iterator, record))
418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
419 PEERSTORE_destroy_record (record);
425 * Continuation of store_record called by the peerstore plugin
428 * @param success result
431 store_record_continuation (void *cls, int success)
433 struct GNUNET_PEERSTORE_Record *record = cls;
435 GNUNET_SERVER_receive_done (record->client, success);
436 if (GNUNET_OK == success)
438 watch_notifier (record);
440 PEERSTORE_destroy_record (record);
445 * Handle a store request from client
448 * @param client identification of the client
449 * @param message the actual message
452 handle_store (void *cls, struct GNUNET_SERVER_Client *client,
453 const struct GNUNET_MessageHeader *message)
455 struct GNUNET_PEERSTORE_Record *record;
456 struct StoreRecordMessage *srm;
458 record = PEERSTORE_parse_record_message (message);
461 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
462 _("Malformed store request from client\n"));
463 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
466 srm = (struct StoreRecordMessage *) message;
467 if (NULL == record->sub_system || NULL == record->peer || NULL == record->key)
469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
470 _("Full key not supplied in client store request\n"));
471 PEERSTORE_destroy_record (record);
472 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
475 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
476 "Received a store request (size: %lu).\n" " Sub system `%s'\n"
477 " Peer `%s'\n" " Key `%s'\n" " Value size %lu\n"
478 " Options: %d.\n", record->value_size, record->sub_system,
479 GNUNET_i2s (record->peer), record->key, record->value_size,
480 ntohl (srm->options));
481 record->client = client;
483 db->store_record (db->cls, record->sub_system, record->peer, record->key,
484 record->value, record->value_size, *record->expiry,
485 ntohl (srm->options), store_record_continuation,
488 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
489 _("Failed to store requested value, database error."));
490 PEERSTORE_destroy_record (record);
491 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
498 * Creates an entry for a new client or returns it if it already exists.
500 * @param client Client handle
501 * @return Client entry struct
503 static struct ClientEntry *
504 make_client_entry (struct GNUNET_SERVER_Client *client)
506 struct ClientEntry *ce;
511 if (ce->client == client)
515 if (GNUNET_YES == in_shutdown)
517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
520 ce = GNUNET_new (struct ClientEntry);
522 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
528 * Callback on a new client connection
530 * @param cls closure (unused)
531 * @param client identification of the client
534 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New client connection created.\n");
537 make_client_entry (client);
542 * Peerstore service runner.
545 * @param server the initialized server
546 * @param c configuration to use
549 run (void *cls, struct GNUNET_SERVER_Handle *server,
550 const struct GNUNET_CONFIGURATION_Handle *c)
552 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
553 {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
554 {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
555 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH,
556 sizeof (struct StoreKeyHashMessage)},
557 {&handle_watch_cancel, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL,
558 sizeof (struct StoreKeyHashMessage)},
563 in_shutdown = GNUNET_NO;
566 GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
568 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No database backend configured\n"));
572 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
573 db = GNUNET_PLUGIN_load (db_lib_name, (void *) cfg);
574 GNUNET_free (database);
578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
579 _("Could not load database backend `%s'\n"),
581 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
584 nc = GNUNET_SERVER_notification_context_create (server, 16);
585 watchers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
586 expire_task = GNUNET_SCHEDULER_add_now (&cleanup_expired_records,
588 GNUNET_SERVER_add_handlers (server, handlers);
589 GNUNET_SERVER_connect_notify (server, &handle_client_connect, NULL);
590 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
591 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
597 * The main function for the peerstore service.
599 * @param argc number of arguments from the command line
600 * @param argv command line arguments
601 * @return 0 ok, 1 on error
604 main (int argc, char *const *argv)
607 GNUNET_SERVICE_run (argc, argv, "peerstore",
608 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, &run,
612 /* end of gnunet-service-peerstore.c */