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;
94 uint16_t sub_system_size;
98 struct GNUNET_SERVER_TransmitContext *tc;
99 struct StoreResponseMessage *res;
101 size_t emsg_size = 0;
104 msg_size = ntohs(message->size);
105 if(msg_size < sizeof(struct StoreRequestMessage))
108 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
111 req = (struct StoreRequestMessage *)message;
112 sub_system_size = ntohs(req->sub_system_size);
113 value_size = ntohs(req->value_size);
114 if(sub_system_size + value_size + sizeof(struct StoreRequestMessage)
118 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
121 sub_system = (char *)&req[1];
122 value = sub_system + sub_system_size;
123 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s' and peer `%s'\n",
126 GNUNET_i2s (&req->peer));
127 if(GNUNET_OK != db->store_record(db->cls,
133 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
134 emsg = _("Failed to store requested value, sqlite database error.");
135 emsg_size = strlen(emsg) + 1;
137 res = GNUNET_malloc(sizeof(struct StoreResponseMessage) + emsg_size);
138 res->emsg_size = htons(emsg_size);
139 res->header.size = htons(sizeof(struct StoreResponseMessage) + emsg_size);
140 res->header.type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT);
143 res->success = htons(GNUNET_NO);
144 emsg_dest = (char *)&res[1];
145 memcpy(emsg_dest, emsg, emsg_size);
148 res->success = htons(GNUNET_YES);
149 tc = GNUNET_SERVER_transmit_context_create (client);
150 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)res);
152 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
157 * Peerstore service runner.
160 * @param server the initialized server
161 * @param c configuration to use
165 struct GNUNET_SERVER_Handle *server,
166 const struct GNUNET_CONFIGURATION_Handle *c)
168 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
169 {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
176 GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
182 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
183 db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
184 GNUNET_free(database);
187 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
190 GNUNET_SERVER_add_handlers (server, handlers);
191 GNUNET_SERVER_disconnect_notify (server,
192 &handle_client_disconnect,
195 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
202 * The main function for the peerstore service.
204 * @param argc number of arguments from the command line
205 * @param argv command line arguments
206 * @return 0 ok, 1 on error
209 main (int argc, char *const *argv)
212 GNUNET_SERVICE_run (argc,
215 GNUNET_SERVICE_OPTION_NONE,
216 &run, NULL)) ? 0 : 1;
219 /* end of gnunet-service-peerstore.c */