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 * Interval for expired records cleanup (in seconds)
35 #define CLEANUP_INTERVAL 300 /* 5mins */
40 static const struct GNUNET_CONFIGURATION_Handle *cfg;
43 * Database plugin library name
50 static struct GNUNET_PEERSTORE_PluginFunctions *db;
53 * Hashmap with all watch requests
55 static struct GNUNET_CONTAINER_MultiHashMap *watchers;
58 * Our notification context.
60 static struct GNUNET_SERVER_NotificationContext *nc;
63 * Task run during shutdown.
69 shutdown_task (void *cls,
70 const struct GNUNET_SCHEDULER_TaskContext *tc)
72 if(NULL != db_lib_name)
74 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
75 GNUNET_free (db_lib_name);
78 GNUNET_SERVER_notification_context_destroy(nc);
79 GNUNET_CONTAINER_multihashmap_destroy(watchers);
81 GNUNET_SCHEDULER_shutdown();
85 * Deletes any expired records from storage
88 cleanup_expired_records(void *cls,
89 const struct GNUNET_SCHEDULER_TaskContext *tc)
93 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
95 GNUNET_assert(NULL != db);
96 deleted = db->expire_records(db->cls, GNUNET_TIME_absolute_get());
97 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", deleted);
98 GNUNET_SCHEDULER_add_delayed(
99 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, CLEANUP_INTERVAL),
100 &cleanup_expired_records, NULL);
104 * Search for a disconnected client and remove it
106 * @param cls closuer, a 'struct GNUNET_PEERSTORE_Record *'
107 * @param key hash of record key
108 * @param value the watcher client, a 'struct GNUNET_SERVER_Client *'
109 * @return #GNUNET_OK to continue iterating
111 int client_disconnect_it(void *cls,
112 const struct GNUNET_HashCode *key,
116 GNUNET_CONTAINER_multihashmap_remove(watchers, key, value);
121 * A client disconnected. Remove all of its data structure entries.
123 * @param cls closure, NULL
124 * @param client identification of the client
127 handle_client_disconnect (void *cls,
128 struct GNUNET_SERVER_Client
131 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "A client was disconnected, cleaning up.\n");
133 GNUNET_CONTAINER_multihashmap_iterate(watchers,
134 &client_disconnect_it, client);
138 * Function called by for each matching record.
141 * @param peer peer identity
142 * @param sub_system name of the GNUnet sub system responsible
143 * @param value stored value
144 * @param size size of stored value
146 int record_iterator(void *cls,
147 struct GNUNET_PEERSTORE_Record *record,
150 struct GNUNET_SERVER_Client *client = cls;
151 struct StoreRecordMessage *srm;
153 srm = PEERSTORE_create_record_message(record->sub_system,
159 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
160 GNUNET_SERVER_notification_context_unicast(nc, client, (struct GNUNET_MessageHeader *)srm, GNUNET_NO);
166 * Iterator over all watcher clients
167 * to notify them of a new record
169 * @param cls closuer, a 'struct GNUNET_PEERSTORE_Record *'
170 * @param key hash of record key
171 * @param value the watcher client, a 'struct GNUNET_SERVER_Client *'
172 * @return #GNUNET_YES to continue iterating
174 int watch_notifier_it(void *cls,
175 const struct GNUNET_HashCode *key,
178 struct GNUNET_PEERSTORE_Record *record = cls;
179 struct GNUNET_SERVER_Client *client = value;
180 struct StoreRecordMessage *srm;
182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found a watcher to update.\n");
183 srm = PEERSTORE_create_record_message(record->sub_system,
189 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD);
190 GNUNET_SERVER_notification_context_unicast(nc, client,
191 (const struct GNUNET_MessageHeader *)srm, GNUNET_NO);
197 * Given a new record, notifies watchers
199 * @param record changed record to update watchers with
201 void watch_notifier (struct GNUNET_PEERSTORE_Record *record)
203 struct GNUNET_HashCode keyhash;
205 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending update to any watchers.\n");
206 PEERSTORE_hash_key(record->sub_system,
210 GNUNET_CONTAINER_multihashmap_get_multiple(watchers, &keyhash, &watch_notifier_it, record);
214 * Handle a watch cancel request from client
217 * @param client identification of the client
218 * @param message the actual message
220 void handle_watch_cancel (void *cls,
221 struct GNUNET_SERVER_Client *client,
222 const struct GNUNET_MessageHeader *message)
224 struct StoreKeyHashMessage *hm;
226 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch cancel request from client.\n");
227 hm = (struct StoreKeyHashMessage *) message;
228 GNUNET_CONTAINER_multihashmap_remove(watchers, &hm->keyhash, client);
229 GNUNET_SERVER_receive_done(client, GNUNET_OK);
233 * Handle a watch request from client
236 * @param client identification of the client
237 * @param message the actual message
239 void handle_watch (void *cls,
240 struct GNUNET_SERVER_Client *client,
241 const struct GNUNET_MessageHeader *message)
243 struct StoreKeyHashMessage *hm;
245 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch request from client.\n");
246 hm = (struct StoreKeyHashMessage *) message;
247 GNUNET_SERVER_client_mark_monitor(client);
248 GNUNET_SERVER_notification_context_add(nc, client);
249 GNUNET_CONTAINER_multihashmap_put(watchers, &hm->keyhash,
250 client, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
251 GNUNET_SERVER_receive_done(client, GNUNET_OK);
255 * Handle an iterate request from client
258 * @param client identification of the client
259 * @param message the actual message
261 void handle_iterate (void *cls,
262 struct GNUNET_SERVER_Client *client,
263 const struct GNUNET_MessageHeader *message)
265 struct GNUNET_PEERSTORE_Record *record;
266 struct GNUNET_MessageHeader *endmsg;
268 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request from client.\n");
269 record = PEERSTORE_parse_record_message(message);
272 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed iterate request from client\n");
273 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
276 if(NULL == record->sub_system)
278 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sub system not supplied in client iterate request\n");
279 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
282 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Iterate request: ss `%s', peer `%s', key `%s'\n",
284 (NULL == record->peer) ? "NULL" : GNUNET_i2s(record->peer),
285 (NULL == record->key) ? "NULL" : record->key);
286 GNUNET_SERVER_notification_context_add(nc, client);
287 if(GNUNET_OK == db->iterate_records(db->cls,
294 endmsg = GNUNET_new(struct GNUNET_MessageHeader);
295 endmsg->size = htons(sizeof(struct GNUNET_MessageHeader));
296 endmsg->type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
297 GNUNET_SERVER_notification_context_unicast(nc, client, endmsg, GNUNET_NO);
299 GNUNET_SERVER_receive_done(client, GNUNET_OK);
303 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
305 PEERSTORE_destroy_record(record);
309 * Handle a store request from client
312 * @param client identification of the client
313 * @param message the actual message
315 void handle_store (void *cls,
316 struct GNUNET_SERVER_Client *client,
317 const struct GNUNET_MessageHeader *message)
319 struct GNUNET_PEERSTORE_Record *record;
320 struct StoreRecordMessage *srm;
322 record = PEERSTORE_parse_record_message(message);
325 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed store request from client\n");
326 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
329 srm = (struct StoreRecordMessage *)message;
330 if(NULL == record->sub_system
331 || NULL == record->peer
332 || NULL == record->key)
334 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Full key not supplied in client store request\n");
335 PEERSTORE_destroy_record(record);
336 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
339 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
342 GNUNET_i2s (record->peer),
344 if(GNUNET_OK != db->store_record(db->cls,
353 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
354 PEERSTORE_destroy_record(record);
355 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
358 GNUNET_SERVER_receive_done(client, GNUNET_OK);
359 watch_notifier(record);
360 PEERSTORE_destroy_record(record);
364 * Peerstore service runner.
367 * @param server the initialized server
368 * @param c configuration to use
372 struct GNUNET_SERVER_Handle *server,
373 const struct GNUNET_CONFIGURATION_Handle *c)
375 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
376 {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
377 {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
378 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH, sizeof(struct StoreKeyHashMessage)},
379 {&handle_watch_cancel, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL, sizeof(struct StoreKeyHashMessage)},
386 GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
392 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
393 db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
394 GNUNET_free(database);
397 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
400 nc = GNUNET_SERVER_notification_context_create (server, 16);
401 watchers = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
402 GNUNET_SCHEDULER_add_now(&cleanup_expired_records, NULL);
403 GNUNET_SERVER_add_handlers (server, handlers);
404 GNUNET_SERVER_disconnect_notify (server,
405 &handle_client_disconnect,
408 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
415 * The main function for the peerstore service.
417 * @param argc number of arguments from the command line
418 * @param argv command line arguments
419 * @return 0 ok, 1 on error
422 main (int argc, char *const *argv)
425 GNUNET_SERVICE_run (argc,
428 GNUNET_SERVICE_OPTION_NONE,
429 &run, NULL)) ? 0 : 1;
432 /* end of gnunet-service-peerstore.c */