ea6268bf126f1555f1d4973aec9ae1e22e27c56e
[oweals/gnunet.git] / src / peerstore / gnunet-service-peerstore.c
1 /*
2      This file is part of GNUnet.
3      (C) 
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file peerstore/gnunet-service-peerstore.c
23  * @brief peerstore service implementation
24  * @author Omar Tarabai
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "peerstore.h"
29 #include "gnunet_peerstore_plugin.h"
30 #include "peerstore_common.h"
31
32 //TODO: GNUNET_SERVER_receive_done() ?
33 //TODO: implement value lifetime
34
35 /**
36  * Our configuration.
37  */
38 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39
40 /**
41  * Database plugin library name
42  */
43 char *db_lib_name;
44
45 /**
46  * Database handle
47  */
48 static struct GNUNET_PEERSTORE_PluginFunctions *db;
49
50 /**
51  * Task run during shutdown.
52  *
53  * @param cls unused
54  * @param tc unused
55  */
56 static void
57 shutdown_task (void *cls,
58                const struct GNUNET_SCHEDULER_TaskContext *tc)
59 {
60   if(NULL != db_lib_name)
61   {
62     GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
63     GNUNET_free (db_lib_name);
64     db_lib_name = NULL;
65   }
66 }
67
68
69 /**
70  * A client disconnected.  Remove all of its data structure entries.
71  *
72  * @param cls closure, NULL
73  * @param client identification of the client
74  */
75 static void
76 handle_client_disconnect (void *cls,
77                           struct GNUNET_SERVER_Client
78                           * client)
79 {
80 }
81
82 /**
83  * Function called by for each matching record.
84  *
85  * @param cls closure
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
90  */
91 int record_iterator(void *cls,
92     struct GNUNET_PEERSTORE_Record *record,
93     char *emsg)
94 {
95   struct GNUNET_SERVER_TransmitContext *tc = cls;
96   struct StoreRecordMessage *srm;
97
98   srm = PEERSTORE_create_record_message(record->sub_system,
99       record->peer,
100       record->key,
101       record->value,
102       record->value_size,
103       record->expiry,
104       GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
105   GNUNET_SERVER_transmit_context_append_message(tc, (const struct GNUNET_MessageHeader *)srm);
106   return GNUNET_YES;
107 }
108
109 /**
110  * Handle an iterate request from client
111  *
112  * @param cls unused
113  * @param client identification of the client
114  * @param message the actual message
115  */
116 void handle_iterate (void *cls,
117     struct GNUNET_SERVER_Client *client,
118     const struct GNUNET_MessageHeader *message)
119 {
120   struct GNUNET_PEERSTORE_Record *record;
121   struct GNUNET_SERVER_TransmitContext *tc;
122
123   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request from client.\n");
124   record = PEERSTORE_parse_record_message(message);
125   if(NULL == record)
126   {
127     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed iterate request from client\n");
128     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
129     return;
130   }
131   if(NULL == record->sub_system)
132   {
133     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sub system not supplied in client iterate request\n");
134     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
135     return;
136   }
137   tc = GNUNET_SERVER_transmit_context_create (client);
138   if(GNUNET_OK == db->iterate_records(db->cls,
139       record->sub_system,
140       record->peer,
141       record->key,
142       &record_iterator,
143       tc))
144   {
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);
147   }
148   else
149   {
150     GNUNET_free(tc);
151     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
152   }
153   GNUNET_free(record);
154 }
155
156 /**
157  * Handle a store request from client
158  *
159  * @param cls unused
160  * @param client identification of the client
161  * @param message the actual message
162  */
163 void handle_store (void *cls,
164     struct GNUNET_SERVER_Client *client,
165     const struct GNUNET_MessageHeader *message)
166 {
167   struct GNUNET_PEERSTORE_Record *record;
168   uint16_t response_type;
169   struct GNUNET_SERVER_TransmitContext *tc;
170
171   record = PEERSTORE_parse_record_message(message);
172   if(NULL == record)
173   {
174     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed store request from client\n");
175     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
176     return;
177   }
178   if(NULL == record->sub_system
179       || NULL == record->peer
180       || NULL == record->key)
181   {
182     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Full key not supplied in client store request\n");
183     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
184     return;
185   }
186   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
187       record->value_size,
188       record->sub_system,
189       GNUNET_i2s (record->peer),
190       record->key);
191   if(GNUNET_OK == db->store_record(db->cls,
192       record->sub_system,
193       record->peer,
194       record->key,
195       record->value,
196       record->value_size,
197       *record->expiry))
198   {
199     response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
200   }
201   else
202   {
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;
205   }
206
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);
210
211 }
212
213 /**
214  * Peerstore service runner.
215  *
216  * @param cls closure
217  * @param server the initialized server
218  * @param c configuration to use
219  */
220 static void
221 run (void *cls,
222      struct GNUNET_SERVER_Handle *server,
223      const struct GNUNET_CONFIGURATION_Handle *c)
224 {
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},
228       {NULL, NULL, 0, 0}
229   };
230   char *database;
231
232   cfg = c;
233   if (GNUNET_OK !=
234         GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
235                                                &database))
236     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
237
238   else
239   {
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);
243   }
244   if(NULL == db)
245           GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
246   else
247   {
248     GNUNET_SERVER_add_handlers (server, handlers);
249     GNUNET_SERVER_disconnect_notify (server,
250              &handle_client_disconnect,
251              NULL);
252   }
253   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
254                                 &shutdown_task,
255                                 NULL);
256 }
257
258
259 /**
260  * The main function for the peerstore service.
261  *
262  * @param argc number of arguments from the command line
263  * @param argv command line arguments
264  * @return 0 ok, 1 on error
265  */
266 int
267 main (int argc, char *const *argv)
268 {
269   return (GNUNET_OK ==
270           GNUNET_SERVICE_run (argc,
271                               argv,
272                               "peerstore",
273                               GNUNET_SERVICE_OPTION_NONE,
274                               &run, NULL)) ? 0 : 1;
275 }
276
277 /* end of gnunet-service-peerstore.c */