b7827692242733db2641eb1d30ffcfe459fe6131
[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
34 /**
35  * Interval for expired records cleanup (in seconds)
36  */
37 #define CLEANUP_INTERVAL 300 /* 5mins */
38
39 /**
40  * Our configuration.
41  */
42 static const struct GNUNET_CONFIGURATION_Handle *cfg;
43
44 /**
45  * Database plugin library name
46  */
47 char *db_lib_name;
48
49 /**
50  * Database handle
51  */
52 static struct GNUNET_PEERSTORE_PluginFunctions *db;
53
54 /**
55  * Task run during shutdown.
56  *
57  * @param cls unused
58  * @param tc unused
59  */
60 static void
61 shutdown_task (void *cls,
62                const struct GNUNET_SCHEDULER_TaskContext *tc)
63 {
64   if(NULL != db_lib_name)
65   {
66     GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
67     GNUNET_free (db_lib_name);
68     db_lib_name = NULL;
69   }
70 }
71
72 /**
73  * Deletes any expired records from storage
74  */
75 static void
76 cleanup_expired_records(void *cls,
77     const struct GNUNET_SCHEDULER_TaskContext *tc)
78 {
79   int deleted;
80
81   GNUNET_assert(NULL != db);
82   deleted = db->expire_records(db->cls, GNUNET_TIME_absolute_get());
83   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", deleted);
84   GNUNET_SCHEDULER_add_delayed(
85       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, CLEANUP_INTERVAL),
86       &cleanup_expired_records, NULL);
87 }
88
89
90 /**
91  * A client disconnected.  Remove all of its data structure entries.
92  *
93  * @param cls closure, NULL
94  * @param client identification of the client
95  */
96 static void
97 handle_client_disconnect (void *cls,
98                           struct GNUNET_SERVER_Client
99                           * client)
100 {
101 }
102
103 /**
104  * Function called by for each matching record.
105  *
106  * @param cls closure
107  * @param peer peer identity
108  * @param sub_system name of the GNUnet sub system responsible
109  * @param value stored value
110  * @param size size of stored value
111  */
112 int record_iterator(void *cls,
113     struct GNUNET_PEERSTORE_Record *record,
114     char *emsg)
115 {
116   struct GNUNET_SERVER_TransmitContext *tc = cls;
117   struct StoreRecordMessage *srm;
118
119   srm = PEERSTORE_create_record_message(record->sub_system,
120       record->peer,
121       record->key,
122       record->value,
123       record->value_size,
124       record->expiry,
125       GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
126   GNUNET_SERVER_transmit_context_append_message(tc, (const struct GNUNET_MessageHeader *)srm);
127   return GNUNET_YES;
128 }
129
130 /**
131  * Handle an iterate request from client
132  *
133  * @param cls unused
134  * @param client identification of the client
135  * @param message the actual message
136  */
137 void handle_iterate (void *cls,
138     struct GNUNET_SERVER_Client *client,
139     const struct GNUNET_MessageHeader *message)
140 {
141   struct GNUNET_PEERSTORE_Record *record;
142   struct GNUNET_SERVER_TransmitContext *tc;
143
144   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request from client.\n");
145   record = PEERSTORE_parse_record_message(message);
146   if(NULL == record)
147   {
148     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed iterate request from client\n");
149     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
150     return;
151   }
152   if(NULL == record->sub_system)
153   {
154     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sub system not supplied in client iterate request\n");
155     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
156     return;
157   }
158   tc = GNUNET_SERVER_transmit_context_create (client);
159   if(GNUNET_OK == db->iterate_records(db->cls,
160       record->sub_system,
161       record->peer,
162       record->key,
163       &record_iterator,
164       tc))
165   {
166     GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
167     GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
168   }
169   else
170   {
171     GNUNET_free(tc);
172     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
173   }
174   GNUNET_free(record);
175 }
176
177 /**
178  * Handle a store request from client
179  *
180  * @param cls unused
181  * @param client identification of the client
182  * @param message the actual message
183  */
184 void handle_store (void *cls,
185     struct GNUNET_SERVER_Client *client,
186     const struct GNUNET_MessageHeader *message)
187 {
188   struct GNUNET_PEERSTORE_Record *record;
189   uint16_t response_type;
190   struct GNUNET_SERVER_TransmitContext *tc;
191
192   record = PEERSTORE_parse_record_message(message);
193   if(NULL == record)
194   {
195     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed store request from client\n");
196     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
197     return;
198   }
199   if(NULL == record->sub_system
200       || NULL == record->peer
201       || NULL == record->key)
202   {
203     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Full key not supplied in client store request\n");
204     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
205     return;
206   }
207   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
208       record->value_size,
209       record->sub_system,
210       GNUNET_i2s (record->peer),
211       record->key);
212   if(GNUNET_OK == db->store_record(db->cls,
213       record->sub_system,
214       record->peer,
215       record->key,
216       record->value,
217       record->value_size,
218       *record->expiry))
219   {
220     response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
221   }
222   else
223   {
224     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
225     response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
226   }
227
228   tc = GNUNET_SERVER_transmit_context_create (client);
229   GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
230   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
231
232 }
233
234 /**
235  * Peerstore service runner.
236  *
237  * @param cls closure
238  * @param server the initialized server
239  * @param c configuration to use
240  */
241 static void
242 run (void *cls,
243      struct GNUNET_SERVER_Handle *server,
244      const struct GNUNET_CONFIGURATION_Handle *c)
245 {
246   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
247       {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
248       {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
249       {NULL, NULL, 0, 0}
250   };
251   char *database;
252
253   cfg = c;
254   if (GNUNET_OK !=
255         GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
256                                                &database))
257     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
258
259   else
260   {
261     GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
262     db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
263     GNUNET_free(database);
264   }
265   if(NULL == db)
266           GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
267   else
268   {
269     cleanup_expired_records(NULL, NULL);
270     GNUNET_SERVER_add_handlers (server, handlers);
271     GNUNET_SERVER_disconnect_notify (server,
272              &handle_client_disconnect,
273              NULL);
274   }
275   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
276                                 &shutdown_task,
277                                 NULL);
278 }
279
280
281 /**
282  * The main function for the peerstore service.
283  *
284  * @param argc number of arguments from the command line
285  * @param argv command line arguments
286  * @return 0 ok, 1 on error
287  */
288 int
289 main (int argc, char *const *argv)
290 {
291   return (GNUNET_OK ==
292           GNUNET_SERVICE_run (argc,
293                               argv,
294                               "peerstore",
295                               GNUNET_SERVICE_OPTION_NONE,
296                               &run, NULL)) ? 0 : 1;
297 }
298
299 /* end of gnunet-service-peerstore.c */