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 const char *sub_system,
93 const struct GNUNET_PeerIdentity *peer,
97 struct GNUNET_TIME_Absolute expiry)
99 struct GNUNET_SERVER_TransmitContext *tc = cls;
100 struct StoreRecordMessage *srm;
102 srm = PEERSTORE_create_record_message(sub_system,
108 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
109 GNUNET_SERVER_transmit_context_append_message(tc, (const struct GNUNET_MessageHeader *)srm);
114 * Handle an iterate request from client
117 * @param client identification of the client
118 * @param message the actual message
120 void handle_iterate (void *cls,
121 struct GNUNET_SERVER_Client *client,
122 const struct GNUNET_MessageHeader *message)
124 struct GNUNET_PEERSTORE_Record *record;
125 struct GNUNET_SERVER_TransmitContext *tc;
127 record = PEERSTORE_parse_record_message(message);
130 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed iterate request from client\n");
131 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
134 if(NULL == record->sub_system)
136 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sub system not supplied in client iterate request\n");
137 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
140 tc = GNUNET_SERVER_transmit_context_create (client);
141 if(GNUNET_OK == db->iterate_records(db->cls,
153 * Handle a store request from client
156 * @param client identification of the client
157 * @param message the actual message
159 void handle_store (void *cls,
160 struct GNUNET_SERVER_Client *client,
161 const struct GNUNET_MessageHeader *message)
163 struct GNUNET_PEERSTORE_Record *record;
164 uint16_t response_type;
165 struct GNUNET_SERVER_TransmitContext *tc;
167 record = PEERSTORE_parse_record_message(message);
170 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed store request from client\n");
171 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
174 if(NULL == record->sub_system
175 || NULL == record->peer
176 || NULL == record->key)
178 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Full key not supplied in client store request\n");
179 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
182 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
185 GNUNET_i2s (record->peer),
187 if(GNUNET_OK == db->store_record(db->cls,
195 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
199 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
200 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
203 tc = GNUNET_SERVER_transmit_context_create (client);
204 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
205 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
210 * Peerstore service runner.
213 * @param server the initialized server
214 * @param c configuration to use
218 struct GNUNET_SERVER_Handle *server,
219 const struct GNUNET_CONFIGURATION_Handle *c)
221 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
222 {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
223 // {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
230 GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
236 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
237 db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
238 GNUNET_free(database);
241 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
244 GNUNET_SERVER_add_handlers (server, handlers);
245 GNUNET_SERVER_disconnect_notify (server,
246 &handle_client_disconnect,
249 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
256 * The main function for the peerstore service.
258 * @param argc number of arguments from the command line
259 * @param argv command line arguments
260 * @return 0 ok, 1 on error
263 main (int argc, char *const *argv)
266 GNUNET_SERVICE_run (argc,
269 GNUNET_SERVICE_OPTION_NONE,
270 &run, NULL)) ? 0 : 1;
273 /* end of gnunet-service-peerstore.c */