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
99 * Perform the actual shutdown operations
103 if(NULL != db_lib_name)
105 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
106 GNUNET_free (db_lib_name);
111 GNUNET_SERVER_notification_context_destroy(nc);
116 GNUNET_CONTAINER_multihashmap_destroy(watchers);
119 GNUNET_SCHEDULER_shutdown();
123 * Task run during shutdown.
129 shutdown_task (void *cls,
130 const struct GNUNET_SCHEDULER_TaskContext *tc)
132 in_shutdown = GNUNET_YES;
133 if (NULL == client_head) /* Only when no connected clients. */
138 * Deletes any expired records from storage
141 cleanup_expired_records(void *cls,
142 const struct GNUNET_SCHEDULER_TaskContext *tc)
146 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
148 GNUNET_assert(NULL != db);
149 deleted = db->expire_records(db->cls, GNUNET_TIME_absolute_get());
151 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", deleted);
152 GNUNET_SCHEDULER_add_delayed(
153 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, EXPIRED_RECORDS_CLEANUP_INTERVAL),
154 &cleanup_expired_records, NULL);
158 * Search for a disconnected client and remove it
160 * @param cls closuer, a 'struct GNUNET_PEERSTORE_Record *'
161 * @param key hash of record key
162 * @param value the watcher client, a 'struct GNUNET_SERVER_Client *'
163 * @return #GNUNET_OK to continue iterating
165 static int client_disconnect_it(void *cls,
166 const struct GNUNET_HashCode *key,
170 GNUNET_CONTAINER_multihashmap_remove(watchers, key, value);
175 * A client disconnected. Remove all of its data structure entries.
177 * @param cls closure, NULL
178 * @param client identification of the client
181 handle_client_disconnect (void *cls,
182 struct GNUNET_SERVER_Client
185 struct ClientEntry *ce;
187 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "A client disconnected, cleaning up.\n");
189 GNUNET_CONTAINER_multihashmap_iterate(watchers,
190 &client_disconnect_it, client);
194 if (ce->client == client)
196 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
202 if (NULL == client_head && in_shutdown)
207 * Function called by for each matching record.
210 * @param record peerstore record found
211 * @param emsg error message or NULL if no errors
212 * @return #GNUNET_YES to continue iteration
215 record_iterator (void *cls,
216 struct GNUNET_PEERSTORE_Record *record,
219 struct GNUNET_SERVER_Client *client = cls;
220 struct StoreRecordMessage *srm;
223 PEERSTORE_create_record_message(record->sub_system,
229 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
230 GNUNET_SERVER_notification_context_unicast (nc, client,
231 (struct GNUNET_MessageHeader *)srm,
238 * Iterator over all watcher clients
239 * to notify them of a new record
241 * @param cls closuer, a 'struct GNUNET_PEERSTORE_Record *'
242 * @param key hash of record key
243 * @param value the watcher client, a 'struct GNUNET_SERVER_Client *'
244 * @return #GNUNET_YES to continue iterating
246 static int watch_notifier_it(void *cls,
247 const struct GNUNET_HashCode *key,
250 struct GNUNET_PEERSTORE_Record *record = cls;
251 struct GNUNET_SERVER_Client *client = value;
252 struct StoreRecordMessage *srm;
254 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found a watcher to update.\n");
255 srm = PEERSTORE_create_record_message(record->sub_system,
261 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD);
262 GNUNET_SERVER_notification_context_unicast(nc, client,
263 (const struct GNUNET_MessageHeader *)srm, GNUNET_NO);
269 * Given a new record, notifies watchers
271 * @param record changed record to update watchers with
273 static void watch_notifier (struct GNUNET_PEERSTORE_Record *record)
275 struct GNUNET_HashCode keyhash;
277 PEERSTORE_hash_key(record->sub_system,
281 GNUNET_CONTAINER_multihashmap_get_multiple(watchers, &keyhash, &watch_notifier_it, record);
285 * Handle a watch cancel request from client
288 * @param client identification of the client
289 * @param message the actual message
291 static void handle_watch_cancel (void *cls,
292 struct GNUNET_SERVER_Client *client,
293 const struct GNUNET_MessageHeader *message)
295 struct StoreKeyHashMessage *hm;
297 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch cancel request.\n");
298 hm = (struct StoreKeyHashMessage *) message;
299 GNUNET_CONTAINER_multihashmap_remove(watchers, &hm->keyhash, client);
300 GNUNET_SERVER_receive_done(client, GNUNET_OK);
304 * Handle a watch request from client
307 * @param client identification of the client
308 * @param message the actual message
310 static void handle_watch (void *cls,
311 struct GNUNET_SERVER_Client *client,
312 const struct GNUNET_MessageHeader *message)
314 struct StoreKeyHashMessage *hm;
316 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch request.\n");
317 hm = (struct StoreKeyHashMessage *) message;
318 GNUNET_SERVER_client_mark_monitor(client);
319 GNUNET_SERVER_notification_context_add(nc, client);
320 GNUNET_CONTAINER_multihashmap_put(watchers, &hm->keyhash,
321 client, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
322 GNUNET_SERVER_receive_done(client, GNUNET_OK);
326 * Handle an iterate request from client
329 * @param client identification of the client
330 * @param message the actual message
332 static void handle_iterate (void *cls,
333 struct GNUNET_SERVER_Client *client,
334 const struct GNUNET_MessageHeader *message)
336 struct GNUNET_PEERSTORE_Record *record;
337 struct GNUNET_MessageHeader *endmsg;
339 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request.\n");
340 record = PEERSTORE_parse_record_message(message);
343 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Malformed iterate request.\n"));
344 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
347 if(NULL == record->sub_system)
349 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
350 _("Sub system not supplied in client iterate request.\n"));
351 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
354 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
355 "Iterate request: ss `%s', peer `%s', key `%s'\n",
357 (NULL == record->peer) ? "NULL" : GNUNET_i2s(record->peer),
358 (NULL == record->key) ? "NULL" : record->key);
359 GNUNET_SERVER_notification_context_add(nc, client);
360 if(GNUNET_OK == db->iterate_records(db->cls,
367 endmsg = GNUNET_new(struct GNUNET_MessageHeader);
368 endmsg->size = htons(sizeof(struct GNUNET_MessageHeader));
369 endmsg->type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
370 GNUNET_SERVER_notification_context_unicast(nc, client, endmsg, GNUNET_NO);
372 GNUNET_SERVER_receive_done(client, GNUNET_OK);
376 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
378 PEERSTORE_destroy_record(record);
382 * Handle a store request from client
385 * @param client identification of the client
386 * @param message the actual message
388 static void handle_store (void *cls,
389 struct GNUNET_SERVER_Client *client,
390 const struct GNUNET_MessageHeader *message)
392 struct GNUNET_PEERSTORE_Record *record;
393 struct StoreRecordMessage *srm;
395 record = PEERSTORE_parse_record_message(message);
398 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Malformed store request from client\n"));
399 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
402 srm = (struct StoreRecordMessage *)message;
403 if(NULL == record->sub_system
404 || NULL == record->peer
405 || NULL == record->key)
407 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Full key not supplied in client store request\n"));
408 PEERSTORE_destroy_record(record);
409 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
412 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
415 GNUNET_i2s (record->peer),
417 if(GNUNET_OK != db->store_record(db->cls,
426 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to store requested value, sqlite database error."));
427 PEERSTORE_destroy_record(record);
428 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
431 GNUNET_SERVER_receive_done(client, GNUNET_OK);
432 watch_notifier(record);
433 PEERSTORE_destroy_record(record);
437 * Creates an entry for a new client or returns it if it already exists.
439 * @param client Client handle
440 * @return Client entry struct
442 static struct ClientEntry *
443 make_client_entry (struct GNUNET_SERVER_Client *client)
445 struct ClientEntry *ce;
450 if (ce->client == client)
454 if (GNUNET_YES == in_shutdown)
456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
459 ce = GNUNET_new (struct ClientEntry);
461 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
466 * Callback on a new client connection
468 * @param cls closure (unused)
469 * @param client identification of the client
472 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New client connection created.\n");
475 make_client_entry (client);
479 * Peerstore service runner.
482 * @param server the initialized server
483 * @param c configuration to use
487 struct GNUNET_SERVER_Handle *server,
488 const struct GNUNET_CONFIGURATION_Handle *c)
490 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
491 {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
492 {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
493 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH, sizeof(struct StoreKeyHashMessage)},
494 {&handle_watch_cancel, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL, sizeof(struct StoreKeyHashMessage)},
499 in_shutdown = GNUNET_NO;
502 GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
504 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No database backend configured\n"));
508 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
509 db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
510 GNUNET_free(database);
514 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Could not load database backend `%s'\n"), db_lib_name);
515 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
518 nc = GNUNET_SERVER_notification_context_create (server, 16);
519 watchers = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
520 GNUNET_SCHEDULER_add_now(&cleanup_expired_records, NULL);
521 GNUNET_SERVER_add_handlers (server, handlers);
522 GNUNET_SERVER_connect_notify (server, &handle_client_connect, NULL);
523 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
524 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
531 * The main function for the peerstore service.
533 * @param argc number of arguments from the command line
534 * @param argv command line arguments
535 * @return 0 ok, 1 on error
538 main (int argc, char *const *argv)
541 GNUNET_SERVICE_run (argc,
544 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN,
545 &run, NULL)) ? 0 : 1;
548 /* end of gnunet-service-peerstore.c */