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() ?
33 //TODO: implement value lifetime
38 static const struct GNUNET_CONFIGURATION_Handle *cfg;
41 * Database plugin library name
48 static struct GNUNET_PEERSTORE_PluginFunctions *db;
51 * Task run during shutdown.
57 shutdown_task (void *cls,
58 const struct GNUNET_SCHEDULER_TaskContext *tc)
60 if(NULL != db_lib_name)
62 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
63 GNUNET_free (db_lib_name);
70 * A client disconnected. Remove all of its data structure entries.
72 * @param cls closure, NULL
73 * @param client identification of the client
76 handle_client_disconnect (void *cls,
77 struct GNUNET_SERVER_Client
83 * Function called by for each matching record.
86 * @param peer peer identity
87 * @param sub_system name of the GNUnet sub system responsible
88 * @param value stored value
89 * @param size size of stored value
91 int record_iterator(void *cls,
92 struct GNUNET_PEERSTORE_Record *record,
95 struct GNUNET_SERVER_TransmitContext *tc = cls;
96 struct StoreRecordMessage *srm;
98 srm = PEERSTORE_create_record_message(record->sub_system,
104 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
105 GNUNET_SERVER_transmit_context_append_message(tc, (const struct GNUNET_MessageHeader *)srm);
110 * Handle an iterate request from client
113 * @param client identification of the client
114 * @param message the actual message
116 void handle_iterate (void *cls,
117 struct GNUNET_SERVER_Client *client,
118 const struct GNUNET_MessageHeader *message)
120 struct GNUNET_PEERSTORE_Record *record;
121 struct GNUNET_SERVER_TransmitContext *tc;
123 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request from client.\n");
124 record = PEERSTORE_parse_record_message(message);
127 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed iterate request from client\n");
128 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
131 if(NULL == record->sub_system)
133 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sub system not supplied in client iterate request\n");
134 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
137 tc = GNUNET_SERVER_transmit_context_create (client);
138 if(GNUNET_OK == db->iterate_records(db->cls,
145 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
146 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
151 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
157 * Handle a store request from client
160 * @param client identification of the client
161 * @param message the actual message
163 void handle_store (void *cls,
164 struct GNUNET_SERVER_Client *client,
165 const struct GNUNET_MessageHeader *message)
167 struct GNUNET_PEERSTORE_Record *record;
168 uint16_t response_type;
169 struct GNUNET_SERVER_TransmitContext *tc;
171 record = PEERSTORE_parse_record_message(message);
174 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed store request from client\n");
175 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
178 if(NULL == record->sub_system
179 || NULL == record->peer
180 || NULL == record->key)
182 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Full key not supplied in client store request\n");
183 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
186 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
189 GNUNET_i2s (record->peer),
191 if(GNUNET_OK == db->store_record(db->cls,
199 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
203 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
204 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
207 tc = GNUNET_SERVER_transmit_context_create (client);
208 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
209 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
214 * Peerstore service runner.
217 * @param server the initialized server
218 * @param c configuration to use
222 struct GNUNET_SERVER_Handle *server,
223 const struct GNUNET_CONFIGURATION_Handle *c)
225 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
226 {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
227 {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
234 GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
240 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
241 db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
242 GNUNET_free(database);
245 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
248 GNUNET_SERVER_add_handlers (server, handlers);
249 GNUNET_SERVER_disconnect_notify (server,
250 &handle_client_disconnect,
253 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
260 * The main function for the peerstore service.
262 * @param argc number of arguments from the command line
263 * @param argv command line arguments
264 * @return 0 ok, 1 on error
267 main (int argc, char *const *argv)
270 GNUNET_SERVICE_run (argc,
273 GNUNET_SERVICE_OPTION_NONE,
274 &run, NULL)) ? 0 : 1;
277 /* end of gnunet-service-peerstore.c */