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);
73 * Deletes any expired records from storage
76 cleanup_expired_records(void *cls,
77 const struct GNUNET_SCHEDULER_TaskContext *tc)
81 GNUNET_assert(NULL != db);
82 deleted = db->expire_records(db->cls, GNUNET_TIME_absolute_get());
83 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", deleted);
84 GNUNET_SCHEDULER_add_delayed(
85 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, CLEANUP_INTERVAL),
86 &cleanup_expired_records, NULL);
91 * A client disconnected. Remove all of its data structure entries.
93 * @param cls closure, NULL
94 * @param client identification of the client
97 handle_client_disconnect (void *cls,
98 struct GNUNET_SERVER_Client
104 * Function called by for each matching record.
107 * @param peer peer identity
108 * @param sub_system name of the GNUnet sub system responsible
109 * @param value stored value
110 * @param size size of stored value
112 int record_iterator(void *cls,
113 struct GNUNET_PEERSTORE_Record *record,
116 struct GNUNET_SERVER_TransmitContext *tc = cls;
117 struct StoreRecordMessage *srm;
119 srm = PEERSTORE_create_record_message(record->sub_system,
125 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
126 GNUNET_SERVER_transmit_context_append_message(tc, (const struct GNUNET_MessageHeader *)srm);
131 * Handle an iterate request from client
134 * @param client identification of the client
135 * @param message the actual message
137 void handle_iterate (void *cls,
138 struct GNUNET_SERVER_Client *client,
139 const struct GNUNET_MessageHeader *message)
141 struct GNUNET_PEERSTORE_Record *record;
142 struct GNUNET_SERVER_TransmitContext *tc;
144 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request from client.\n");
145 record = PEERSTORE_parse_record_message(message);
148 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed iterate request from client\n");
149 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
152 if(NULL == record->sub_system)
154 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sub system not supplied in client iterate request\n");
155 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
158 tc = GNUNET_SERVER_transmit_context_create (client);
159 if(GNUNET_OK == db->iterate_records(db->cls,
166 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
167 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
172 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
178 * Handle a store request from client
181 * @param client identification of the client
182 * @param message the actual message
184 void handle_store (void *cls,
185 struct GNUNET_SERVER_Client *client,
186 const struct GNUNET_MessageHeader *message)
188 struct GNUNET_PEERSTORE_Record *record;
189 uint16_t response_type;
190 struct GNUNET_SERVER_TransmitContext *tc;
192 record = PEERSTORE_parse_record_message(message);
195 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed store request from client\n");
196 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
199 if(NULL == record->sub_system
200 || NULL == record->peer
201 || NULL == record->key)
203 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Full key not supplied in client store request\n");
204 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
207 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
210 GNUNET_i2s (record->peer),
212 if(GNUNET_OK == db->store_record(db->cls,
220 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
224 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
225 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
228 tc = GNUNET_SERVER_transmit_context_create (client);
229 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
230 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
235 * Peerstore service runner.
238 * @param server the initialized server
239 * @param c configuration to use
243 struct GNUNET_SERVER_Handle *server,
244 const struct GNUNET_CONFIGURATION_Handle *c)
246 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
247 {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
248 {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
255 GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
261 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
262 db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
263 GNUNET_free(database);
266 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
269 cleanup_expired_records(NULL, NULL);
270 GNUNET_SERVER_add_handlers (server, handlers);
271 GNUNET_SERVER_disconnect_notify (server,
272 &handle_client_disconnect,
275 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
282 * The main function for the peerstore service.
284 * @param argc number of arguments from the command line
285 * @param argv command line arguments
286 * @return 0 ok, 1 on error
289 main (int argc, char *const *argv)
292 GNUNET_SERVICE_run (argc,
295 GNUNET_SERVICE_OPTION_NONE,
296 &run, NULL)) ? 0 : 1;
299 /* end of gnunet-service-peerstore.c */