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 * Context of a PEERSTORE watch
39 * Hash of key of watched record
41 struct GNUNET_HashCode keyhash;
44 * Client requested the watch
46 struct GNUNET_SERVER_Client *client;
51 * Interval for expired records cleanup (in seconds)
53 #define CLEANUP_INTERVAL 300 /* 5mins */
58 static const struct GNUNET_CONFIGURATION_Handle *cfg;
61 * Database plugin library name
68 static struct GNUNET_PEERSTORE_PluginFunctions *db;
71 * Hashmap with all watch requests
73 static struct GNUNET_CONTAINER_MultiHashMap *watchers;
76 * Task run during shutdown.
82 shutdown_task (void *cls,
83 const struct GNUNET_SCHEDULER_TaskContext *tc)
85 if(NULL != db_lib_name)
87 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
88 GNUNET_free (db_lib_name);
92 GNUNET_CONTAINER_multihashmap_destroy(watchers);
93 GNUNET_SCHEDULER_shutdown();
97 * Deletes any expired records from storage
100 cleanup_expired_records(void *cls,
101 const struct GNUNET_SCHEDULER_TaskContext *tc)
105 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
107 GNUNET_assert(NULL != db);
108 deleted = db->expire_records(db->cls, GNUNET_TIME_absolute_get());
109 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", deleted);
110 GNUNET_SCHEDULER_add_delayed(
111 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, CLEANUP_INTERVAL),
112 &cleanup_expired_records, NULL);
117 * A client disconnected. Remove all of its data structure entries.
119 * @param cls closure, NULL
120 * @param client identification of the client
123 handle_client_disconnect (void *cls,
124 struct GNUNET_SERVER_Client
130 * Function called by for each matching record.
133 * @param peer peer identity
134 * @param sub_system name of the GNUnet sub system responsible
135 * @param value stored value
136 * @param size size of stored value
138 int record_iterator(void *cls,
139 struct GNUNET_PEERSTORE_Record *record,
142 struct GNUNET_SERVER_TransmitContext *tc = cls;
143 struct StoreRecordMessage *srm;
145 srm = PEERSTORE_create_record_message(record->sub_system,
151 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
152 GNUNET_SERVER_transmit_context_append_message(tc, (const struct GNUNET_MessageHeader *)srm);
157 * Handle a watch cancel request from client
160 * @param client identification of the client
161 * @param message the actual message
163 void handle_watch_cancel (void *cls,
164 struct GNUNET_SERVER_Client *client,
165 const struct GNUNET_MessageHeader *message)
167 struct StoreKeyHashMessage *hm;
169 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch cancel request from client.\n");
172 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
173 "Received a watch cancel request when we don't have any watchers.\n");
174 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
177 hm = (struct StoreKeyHashMessage *) message;
178 GNUNET_CONTAINER_multihashmap_remove(watchers, &hm->keyhash, client);
179 GNUNET_SERVER_receive_done(client, GNUNET_OK);
183 * Handle a watch request from client
186 * @param client identification of the client
187 * @param message the actual message
189 void handle_watch (void *cls,
190 struct GNUNET_SERVER_Client *client,
191 const struct GNUNET_MessageHeader *message)
193 struct StoreKeyHashMessage *hm;
195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch request from client.\n");
196 hm = (struct StoreKeyHashMessage *) message;
197 GNUNET_SERVER_client_mark_monitor(client);
199 watchers = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
200 GNUNET_CONTAINER_multihashmap_put(watchers, &hm->keyhash,
201 client, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
202 GNUNET_SERVER_receive_done(client, GNUNET_OK);
206 * Handle an iterate request from client
209 * @param client identification of the client
210 * @param message the actual message
212 void handle_iterate (void *cls,
213 struct GNUNET_SERVER_Client *client,
214 const struct GNUNET_MessageHeader *message)
216 struct GNUNET_PEERSTORE_Record *record;
217 struct GNUNET_SERVER_TransmitContext *tc;
219 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request from client.\n");
220 record = PEERSTORE_parse_record_message(message);
223 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed iterate request from client\n");
224 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
227 if(NULL == record->sub_system)
229 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sub system not supplied in client iterate request\n");
230 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
233 tc = GNUNET_SERVER_transmit_context_create (client);
234 if(GNUNET_OK == db->iterate_records(db->cls,
241 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
242 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
247 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
253 * Handle a store request from client
256 * @param client identification of the client
257 * @param message the actual message
259 void handle_store (void *cls,
260 struct GNUNET_SERVER_Client *client,
261 const struct GNUNET_MessageHeader *message)
263 struct GNUNET_PEERSTORE_Record *record;
264 uint16_t response_type;
265 struct GNUNET_SERVER_TransmitContext *tc;
267 record = PEERSTORE_parse_record_message(message);
270 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed store request from client\n");
271 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
274 if(NULL == record->sub_system
275 || NULL == record->peer
276 || NULL == record->key)
278 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Full key not supplied in client store request\n");
279 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
282 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
285 GNUNET_i2s (record->peer),
287 if(GNUNET_OK == db->store_record(db->cls,
295 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
299 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
300 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
303 tc = GNUNET_SERVER_transmit_context_create (client);
304 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
305 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
306 //TODO: notify watchers, if a client is disconnected, remove its watch entry
310 * Peerstore service runner.
313 * @param server the initialized server
314 * @param c configuration to use
318 struct GNUNET_SERVER_Handle *server,
319 const struct GNUNET_CONFIGURATION_Handle *c)
321 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
322 {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
323 {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
324 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH, sizeof(struct StoreKeyHashMessage)},
325 {&handle_watch_cancel, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL, sizeof(struct StoreKeyHashMessage)},
332 GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
338 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
339 db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
340 GNUNET_free(database);
343 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
346 GNUNET_SCHEDULER_add_now(&cleanup_expired_records, NULL);
347 GNUNET_SERVER_add_handlers (server, handlers);
348 GNUNET_SERVER_disconnect_notify (server,
349 &handle_client_disconnect,
352 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
359 * The main function for the peerstore service.
361 * @param argc number of arguments from the command line
362 * @param argv command line arguments
363 * @return 0 ok, 1 on error
366 main (int argc, char *const *argv)
369 GNUNET_SERVICE_run (argc,
372 GNUNET_SERVICE_OPTION_NONE,
373 &run, NULL)) ? 0 : 1;
376 /* end of gnunet-service-peerstore.c */