2 This file is part of GNUnet.
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 * Are we in the process of shutting down the service? #GNUNET_YES / #GNUNET_NO
96 static int in_shutdown;
99 * Perform the actual shutdown operations
104 if (NULL != db_lib_name)
106 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
107 GNUNET_free (db_lib_name);
112 GNUNET_SERVER_notification_context_destroy (nc);
115 if (NULL != watchers)
117 GNUNET_CONTAINER_multihashmap_destroy (watchers);
120 GNUNET_SCHEDULER_shutdown ();
125 * Task run during shutdown.
131 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
133 in_shutdown = GNUNET_YES;
134 if (NULL == client_head) /* Only when no connected clients. */
139 /* Forward declaration */
140 static void expire_records_continuation (void *cls, int success);
144 * Deletes any expired records from storage
147 cleanup_expired_records (void *cls,
148 const struct GNUNET_SCHEDULER_TaskContext *tc)
152 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
154 GNUNET_assert (NULL != db);
155 ret = db->expire_records (db->cls, GNUNET_TIME_absolute_get (),
156 expire_records_continuation, NULL);
157 if (GNUNET_OK != ret)
159 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
160 (GNUNET_TIME_UNIT_SECONDS,
161 EXPIRED_RECORDS_CLEANUP_INTERVAL),
162 &cleanup_expired_records, NULL);
168 * Continuation to expire_records called by the peerstore plugin
171 * @param success count of records deleted or #GNUNET_SYSERR
174 expire_records_continuation(void *cls, int success)
177 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", success);
178 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
179 (GNUNET_TIME_UNIT_SECONDS,
180 EXPIRED_RECORDS_CLEANUP_INTERVAL),
181 &cleanup_expired_records, NULL);
186 * Search for a disconnected client and remove it
188 * @param cls closuer, a 'struct GNUNET_PEERSTORE_Record *'
189 * @param key hash of record key
190 * @param value the watcher client, a 'struct GNUNET_SERVER_Client *'
191 * @return #GNUNET_OK to continue iterating
194 client_disconnect_it (void *cls, const struct GNUNET_HashCode *key, void *value)
197 GNUNET_CONTAINER_multihashmap_remove (watchers, key, value);
203 * A client disconnected. Remove all of its data structure entries.
205 * @param cls closure, NULL
206 * @param client identification of the client
209 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
211 struct ClientEntry *ce;
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A client disconnected, cleaning up.\n");
214 if (NULL != watchers)
215 GNUNET_CONTAINER_multihashmap_iterate (watchers, &client_disconnect_it,
220 if (ce->client == client)
222 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
228 if (NULL == client_head && in_shutdown)
234 * Function called by for each matching record.
237 * @param record peerstore record found
238 * @param emsg error message or NULL if no errors
239 * @return #GNUNET_YES to continue iteration
242 record_iterator (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg)
244 struct GNUNET_PEERSTORE_Record *cls_record = cls;
245 struct StoreRecordMessage *srm;
249 /* No more records */
250 struct GNUNET_MessageHeader *endmsg;
252 endmsg = GNUNET_new (struct GNUNET_MessageHeader);
253 endmsg->size = htons (sizeof (struct GNUNET_MessageHeader));
254 endmsg->type = htons (GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
255 GNUNET_SERVER_notification_context_unicast (nc, cls_record->client, endmsg,
257 GNUNET_free (endmsg);
258 GNUNET_SERVER_receive_done (cls_record->client,
259 NULL == emsg ? GNUNET_OK : GNUNET_SYSERR);
260 PEERSTORE_destroy_record (cls_record);
265 PEERSTORE_create_record_message (record->sub_system, record->peer,
266 record->key, record->value,
267 record->value_size, record->expiry,
268 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
269 GNUNET_SERVER_notification_context_unicast (nc, cls_record->client,
270 (struct GNUNET_MessageHeader *)
278 * Iterator over all watcher clients
279 * to notify them of a new record
281 * @param cls closuer, a 'struct GNUNET_PEERSTORE_Record *'
282 * @param key hash of record key
283 * @param value the watcher client, a 'struct GNUNET_SERVER_Client *'
284 * @return #GNUNET_YES to continue iterating
287 watch_notifier_it (void *cls, const struct GNUNET_HashCode *key, void *value)
289 struct GNUNET_PEERSTORE_Record *record = cls;
290 struct GNUNET_SERVER_Client *client = value;
291 struct StoreRecordMessage *srm;
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found a watcher to update.\n");
295 PEERSTORE_create_record_message (record->sub_system, record->peer,
296 record->key, record->value,
297 record->value_size, record->expiry,
298 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD);
299 GNUNET_SERVER_notification_context_unicast (nc, client,
300 (const struct GNUNET_MessageHeader
308 * Given a new record, notifies watchers
310 * @param record changed record to update watchers with
313 watch_notifier (struct GNUNET_PEERSTORE_Record *record)
315 struct GNUNET_HashCode keyhash;
317 PEERSTORE_hash_key (record->sub_system, record->peer, record->key, &keyhash);
318 GNUNET_CONTAINER_multihashmap_get_multiple (watchers, &keyhash,
319 &watch_notifier_it, record);
324 * Handle a watch cancel request from client
327 * @param client identification of the client
328 * @param message the actual message
331 handle_watch_cancel (void *cls, struct GNUNET_SERVER_Client *client,
332 const struct GNUNET_MessageHeader *message)
334 struct StoreKeyHashMessage *hm;
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a watch cancel request.\n");
337 hm = (struct StoreKeyHashMessage *) message;
338 GNUNET_CONTAINER_multihashmap_remove (watchers, &hm->keyhash, client);
339 GNUNET_SERVER_receive_done (client, GNUNET_OK);
344 * Handle a watch request from client
347 * @param client identification of the client
348 * @param message the actual message
351 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
352 const struct GNUNET_MessageHeader *message)
354 struct StoreKeyHashMessage *hm;
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a watch request.\n");
357 hm = (struct StoreKeyHashMessage *) message;
358 GNUNET_SERVER_client_mark_monitor (client);
359 GNUNET_SERVER_notification_context_add (nc, client);
360 GNUNET_CONTAINER_multihashmap_put (watchers, &hm->keyhash, client,
361 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
362 GNUNET_SERVER_receive_done (client, GNUNET_OK);
367 * Handle an iterate request from client
370 * @param client identification of the client
371 * @param message the actual message
374 handle_iterate (void *cls, struct GNUNET_SERVER_Client *client,
375 const struct GNUNET_MessageHeader *message)
377 struct GNUNET_PEERSTORE_Record *record;
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request.\n");
380 record = PEERSTORE_parse_record_message (message);
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Malformed iterate request.\n"));
384 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
387 if (NULL == record->sub_system)
389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
390 _("Sub system not supplied in client iterate request.\n"));
391 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
392 PEERSTORE_destroy_record (record);
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Iterate request: ss `%s', peer `%s', key `%s'\n",
398 (NULL == record->peer) ? "NULL" : GNUNET_i2s (record->peer),
399 (NULL == record->key) ? "NULL" : record->key);
400 GNUNET_SERVER_notification_context_add (nc, client);
401 record->client = client;
403 db->iterate_records (db->cls, record->sub_system, record->peer,
404 record->key, &record_iterator, record))
406 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
407 PEERSTORE_destroy_record (record);
413 * Continuation of store_record called by the peerstore plugin
416 * @param success result
419 store_record_continuation (void *cls, int success)
421 struct GNUNET_PEERSTORE_Record *record = cls;
423 GNUNET_SERVER_receive_done (record->client, success);
424 if (GNUNET_OK == success)
426 watch_notifier (record);
428 PEERSTORE_destroy_record (record);
433 * Handle a store request from client
436 * @param client identification of the client
437 * @param message the actual message
440 handle_store (void *cls, struct GNUNET_SERVER_Client *client,
441 const struct GNUNET_MessageHeader *message)
443 struct GNUNET_PEERSTORE_Record *record;
444 struct StoreRecordMessage *srm;
446 record = PEERSTORE_parse_record_message (message);
449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
450 _("Malformed store request from client\n"));
451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
454 srm = (struct StoreRecordMessage *) message;
455 if (NULL == record->sub_system || NULL == record->peer || NULL == record->key)
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
458 _("Full key not supplied in client store request\n"));
459 PEERSTORE_destroy_record (record);
460 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
463 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
464 "Received a store request (size: %lu).\n"
470 record->value_size, record->sub_system, GNUNET_i2s (record->peer),
471 record->key, record->value_size, ntohl (srm->options));
472 record->client = client;
474 db->store_record (db->cls, record->sub_system, record->peer, record->key,
475 record->value, record->value_size, *record->expiry,
476 ntohl (srm->options), store_record_continuation,
479 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
480 _("Failed to store requested value, database error."));
481 PEERSTORE_destroy_record (record);
482 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
489 * Creates an entry for a new client or returns it if it already exists.
491 * @param client Client handle
492 * @return Client entry struct
494 static struct ClientEntry *
495 make_client_entry (struct GNUNET_SERVER_Client *client)
497 struct ClientEntry *ce;
502 if (ce->client == client)
506 if (GNUNET_YES == in_shutdown)
508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
511 ce = GNUNET_new (struct ClientEntry);
513 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
519 * Callback on a new client connection
521 * @param cls closure (unused)
522 * @param client identification of the client
525 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New client connection created.\n");
528 make_client_entry (client);
533 * Peerstore service runner.
536 * @param server the initialized server
537 * @param c configuration to use
540 run (void *cls, struct GNUNET_SERVER_Handle *server,
541 const struct GNUNET_CONFIGURATION_Handle *c)
543 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
544 {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
545 {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
546 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH,
547 sizeof (struct StoreKeyHashMessage)},
548 {&handle_watch_cancel, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL,
549 sizeof (struct StoreKeyHashMessage)},
554 in_shutdown = GNUNET_NO;
557 GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No database backend configured\n"));
563 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
564 db = GNUNET_PLUGIN_load (db_lib_name, (void *) cfg);
565 GNUNET_free (database);
569 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
570 _("Could not load database backend `%s'\n"), db_lib_name);
571 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
574 nc = GNUNET_SERVER_notification_context_create (server, 16);
575 watchers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
576 GNUNET_SCHEDULER_add_now (&cleanup_expired_records, NULL);
577 GNUNET_SERVER_add_handlers (server, handlers);
578 GNUNET_SERVER_connect_notify (server, &handle_client_connect, NULL);
579 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
580 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
586 * The main function for the peerstore service.
588 * @param argc number of arguments from the command line
589 * @param argv command line arguments
590 * @return 0 ok, 1 on error
593 main (int argc, char *const *argv)
596 GNUNET_SERVICE_run (argc, argv, "peerstore",
597 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, &run,
601 /* end of gnunet-service-peerstore.c */