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"
32 //TODO: GNUNET_SERVER_receive_done() ?
35 * Interval for expired records cleanup (in seconds)
37 #define CLEANUP_INTERVAL 300 /* 5mins */
42 static const struct GNUNET_CONFIGURATION_Handle *cfg;
45 * Database plugin library name
52 static struct GNUNET_PEERSTORE_PluginFunctions *db;
55 * Task run during shutdown.
61 shutdown_task (void *cls,
62 const struct GNUNET_SCHEDULER_TaskContext *tc)
64 if(NULL != db_lib_name)
66 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
67 GNUNET_free (db_lib_name);
71 GNUNET_SCHEDULER_shutdown();
75 * Deletes any expired records from storage
78 cleanup_expired_records(void *cls,
79 const struct GNUNET_SCHEDULER_TaskContext *tc)
83 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
85 GNUNET_assert(NULL != db);
86 deleted = db->expire_records(db->cls, GNUNET_TIME_absolute_get());
87 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", deleted);
88 GNUNET_SCHEDULER_add_delayed(
89 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, CLEANUP_INTERVAL),
90 &cleanup_expired_records, NULL);
95 * A client disconnected. Remove all of its data structure entries.
97 * @param cls closure, NULL
98 * @param client identification of the client
101 handle_client_disconnect (void *cls,
102 struct GNUNET_SERVER_Client
108 * Function called by for each matching record.
111 * @param peer peer identity
112 * @param sub_system name of the GNUnet sub system responsible
113 * @param value stored value
114 * @param size size of stored value
116 int record_iterator(void *cls,
117 struct GNUNET_PEERSTORE_Record *record,
120 struct GNUNET_SERVER_TransmitContext *tc = cls;
121 struct StoreRecordMessage *srm;
123 srm = PEERSTORE_create_record_message(record->sub_system,
129 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
130 GNUNET_SERVER_transmit_context_append_message(tc, (const struct GNUNET_MessageHeader *)srm);
135 * Handle an iterate request from client
138 * @param client identification of the client
139 * @param message the actual message
141 void handle_iterate (void *cls,
142 struct GNUNET_SERVER_Client *client,
143 const struct GNUNET_MessageHeader *message)
145 struct GNUNET_PEERSTORE_Record *record;
146 struct GNUNET_SERVER_TransmitContext *tc;
148 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request from client.\n");
149 record = PEERSTORE_parse_record_message(message);
152 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed iterate request from client\n");
153 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
156 if(NULL == record->sub_system)
158 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sub system not supplied in client iterate request\n");
159 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
162 tc = GNUNET_SERVER_transmit_context_create (client);
163 if(GNUNET_OK == db->iterate_records(db->cls,
170 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
171 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
176 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
182 * Handle a store request from client
185 * @param client identification of the client
186 * @param message the actual message
188 void handle_store (void *cls,
189 struct GNUNET_SERVER_Client *client,
190 const struct GNUNET_MessageHeader *message)
192 struct GNUNET_PEERSTORE_Record *record;
193 uint16_t response_type;
194 struct GNUNET_SERVER_TransmitContext *tc;
196 record = PEERSTORE_parse_record_message(message);
199 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed store request from client\n");
200 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
203 if(NULL == record->sub_system
204 || NULL == record->peer
205 || NULL == record->key)
207 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Full key not supplied in client store request\n");
208 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
211 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
214 GNUNET_i2s (record->peer),
216 if(GNUNET_OK == db->store_record(db->cls,
224 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
228 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
229 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
232 tc = GNUNET_SERVER_transmit_context_create (client);
233 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
234 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
239 * Peerstore service runner.
242 * @param server the initialized server
243 * @param c configuration to use
247 struct GNUNET_SERVER_Handle *server,
248 const struct GNUNET_CONFIGURATION_Handle *c)
250 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
251 {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
252 {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
259 GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
265 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
266 db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
267 GNUNET_free(database);
270 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
273 GNUNET_SCHEDULER_add_now(&cleanup_expired_records, NULL);
274 GNUNET_SERVER_add_handlers (server, handlers);
275 GNUNET_SERVER_disconnect_notify (server,
276 &handle_client_disconnect,
279 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
286 * The main function for the peerstore service.
288 * @param argc number of arguments from the command line
289 * @param argv command line arguments
290 * @return 0 ok, 1 on error
293 main (int argc, char *const *argv)
296 GNUNET_SERVICE_run (argc,
299 GNUNET_SERVICE_OPTION_NONE,
300 &run, NULL)) ? 0 : 1;
303 /* end of gnunet-service-peerstore.c */