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"
31 //TODO: GNUNET_SERVER_receive_done() ?
32 //TODO: implement value lifetime
37 static const struct GNUNET_CONFIGURATION_Handle *cfg;
40 * Database plugin library name
47 static struct GNUNET_PEERSTORE_PluginFunctions *db;
50 * Task run during shutdown.
56 shutdown_task (void *cls,
57 const struct GNUNET_SCHEDULER_TaskContext *tc)
59 if(NULL != db_lib_name)
61 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
62 GNUNET_free (db_lib_name);
69 * A client disconnected. Remove all of its data structure entries.
71 * @param cls closure, NULL
72 * @param client identification of the client
75 handle_client_disconnect (void *cls,
76 struct GNUNET_SERVER_Client
82 * Handle a store request from client
85 * @param client identification of the client
86 * @param message the actual message
88 void handle_store (void *cls,
89 struct GNUNET_SERVER_Client *client,
90 const struct GNUNET_MessageHeader *message)
92 struct StoreRequestMessage *req;
100 uint16_t response_type;
101 struct GNUNET_SERVER_TransmitContext *tc;
103 req_size = ntohs(message->size);
104 if(req_size < sizeof(struct StoreRequestMessage))
107 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
110 req = (struct StoreRequestMessage *)message;
111 ss_size = ntohs(req->sub_system_size);
112 key_size = ntohs(req->key_size);
113 value_size = ntohs(req->value_size);
114 if(ss_size + key_size + value_size + sizeof(struct StoreRequestMessage)
118 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
121 sub_system = (char *)&req[1];
122 key = sub_system + ss_size;
123 value = key + key_size;
124 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
127 GNUNET_i2s (&req->peer),
129 if(GNUNET_OK == db->store_record(db->cls,
136 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
140 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
141 response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
144 tc = GNUNET_SERVER_transmit_context_create (client);
145 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
146 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
151 * Peerstore service runner.
154 * @param server the initialized server
155 * @param c configuration to use
159 struct GNUNET_SERVER_Handle *server,
160 const struct GNUNET_CONFIGURATION_Handle *c)
162 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
163 {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
170 GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
176 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
177 db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
178 GNUNET_free(database);
181 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
184 GNUNET_SERVER_add_handlers (server, handlers);
185 GNUNET_SERVER_disconnect_notify (server,
186 &handle_client_disconnect,
189 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
196 * The main function for the peerstore service.
198 * @param argc number of arguments from the command line
199 * @param argv command line arguments
200 * @return 0 ok, 1 on error
203 main (int argc, char *const *argv)
206 GNUNET_SERVICE_run (argc,
209 GNUNET_SERVICE_OPTION_NONE,
210 &run, NULL)) ? 0 : 1;
213 /* end of gnunet-service-peerstore.c */