minor fixes including string internationalization
[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 /**
33  * Interval for expired records cleanup (in seconds)
34  */
35 #define EXPIRED_RECORDS_CLEANUP_INTERVAL 300 /* 5mins */
36
37 /**
38  * Our configuration.
39  */
40 static const struct GNUNET_CONFIGURATION_Handle *cfg;
41
42 /**
43  * Database plugin library name
44  */
45 static char *db_lib_name;
46
47 /**
48  * Database handle
49  */
50 static struct GNUNET_PEERSTORE_PluginFunctions *db;
51
52 /**
53  * Hashmap with all watch requests
54  */
55 static struct GNUNET_CONTAINER_MultiHashMap *watchers;
56
57 /**
58  * Our notification context.
59  */
60 static struct GNUNET_SERVER_NotificationContext *nc;
61
62 /**
63  * Task run during shutdown.
64  *
65  * @param cls unused
66  * @param tc unused
67  */
68 static void
69 shutdown_task (void *cls,
70                const struct GNUNET_SCHEDULER_TaskContext *tc)
71 {
72   if(NULL != db_lib_name)
73   {
74     GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
75     GNUNET_free (db_lib_name);
76     db_lib_name = NULL;
77   }
78   GNUNET_SERVER_notification_context_destroy(nc);
79   GNUNET_CONTAINER_multihashmap_destroy(watchers);
80   watchers = NULL;
81   GNUNET_SCHEDULER_shutdown();
82 }
83
84 /**
85  * Deletes any expired records from storage
86  */
87 static void
88 cleanup_expired_records(void *cls,
89     const struct GNUNET_SCHEDULER_TaskContext *tc)
90 {
91   int deleted;
92
93   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
94     return;
95   GNUNET_assert(NULL != db);
96   deleted = db->expire_records(db->cls, GNUNET_TIME_absolute_get());
97   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", deleted);
98   GNUNET_SCHEDULER_add_delayed(
99       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, EXPIRED_RECORDS_CLEANUP_INTERVAL),
100       &cleanup_expired_records, NULL);
101 }
102
103 /**
104  * Search for a disconnected client and remove it
105  *
106  * @param cls closuer, a 'struct GNUNET_PEERSTORE_Record *'
107  * @param key hash of record key
108  * @param value the watcher client, a 'struct GNUNET_SERVER_Client *'
109  * @return #GNUNET_OK to continue iterating
110  */
111 int client_disconnect_it(void *cls,
112     const struct GNUNET_HashCode *key,
113     void *value)
114 {
115   if(cls == value)
116     GNUNET_CONTAINER_multihashmap_remove(watchers, key, value);
117   return GNUNET_OK;
118 }
119
120 /**
121  * A client disconnected.  Remove all of its data structure entries.
122  *
123  * @param cls closure, NULL
124  * @param client identification of the client
125  */
126 static void
127 handle_client_disconnect (void *cls,
128                           struct GNUNET_SERVER_Client
129                           * client)
130 {
131   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "A client was disconnected, cleaning up.\n");
132   if(NULL != watchers)
133     GNUNET_CONTAINER_multihashmap_iterate(watchers,
134         &client_disconnect_it, client);
135 }
136
137 /**
138  * Function called by for each matching record.
139  *
140  * @param cls closure
141  * @param peer peer identity
142  * @param sub_system name of the GNUnet sub system responsible
143  * @param value stored value
144  * @param size size of stored value
145  */
146 int record_iterator(void *cls,
147     struct GNUNET_PEERSTORE_Record *record,
148     char *emsg)
149 {
150   struct GNUNET_SERVER_Client *client = cls;
151   struct StoreRecordMessage *srm;
152
153   srm = PEERSTORE_create_record_message(record->sub_system,
154       record->peer,
155       record->key,
156       record->value,
157       record->value_size,
158       record->expiry,
159       GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD);
160   GNUNET_SERVER_notification_context_unicast(nc, client, (struct GNUNET_MessageHeader *)srm, GNUNET_NO);
161   GNUNET_free(srm);
162   return GNUNET_YES;
163 }
164
165 /**
166  * Iterator over all watcher clients
167  * to notify them of a new record
168  *
169  * @param cls closuer, a 'struct GNUNET_PEERSTORE_Record *'
170  * @param key hash of record key
171  * @param value the watcher client, a 'struct GNUNET_SERVER_Client *'
172  * @return #GNUNET_YES to continue iterating
173  */
174 int watch_notifier_it(void *cls,
175     const struct GNUNET_HashCode *key,
176     void *value)
177 {
178   struct GNUNET_PEERSTORE_Record *record = cls;
179   struct GNUNET_SERVER_Client *client = value;
180   struct StoreRecordMessage *srm;
181
182   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found a watcher to update.\n");
183   srm = PEERSTORE_create_record_message(record->sub_system,
184       record->peer,
185       record->key,
186       record->value,
187       record->value_size,
188       record->expiry,
189       GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD);
190   GNUNET_SERVER_notification_context_unicast(nc, client,
191       (const struct GNUNET_MessageHeader *)srm, GNUNET_NO);
192   GNUNET_free(srm);
193   return GNUNET_YES;
194 }
195
196 /**
197  * Given a new record, notifies watchers
198  *
199  * @param record changed record to update watchers with
200  */
201 void watch_notifier (struct GNUNET_PEERSTORE_Record *record)
202 {
203   struct GNUNET_HashCode keyhash;
204
205   PEERSTORE_hash_key(record->sub_system,
206       record->peer,
207       record->key,
208       &keyhash);
209   GNUNET_CONTAINER_multihashmap_get_multiple(watchers, &keyhash, &watch_notifier_it, record);
210 }
211
212 /**
213  * Handle a watch cancel request from client
214  *
215  * @param cls unused
216  * @param client identification of the client
217  * @param message the actual message
218  */
219 void handle_watch_cancel (void *cls,
220     struct GNUNET_SERVER_Client *client,
221     const struct GNUNET_MessageHeader *message)
222 {
223   struct StoreKeyHashMessage *hm;
224
225   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch cancel request from client.\n");
226   hm = (struct StoreKeyHashMessage *) message;
227   GNUNET_CONTAINER_multihashmap_remove(watchers, &hm->keyhash, client);
228   GNUNET_SERVER_receive_done(client, GNUNET_OK);
229 }
230
231 /**
232  * Handle a watch request from client
233  *
234  * @param cls unused
235  * @param client identification of the client
236  * @param message the actual message
237  */
238 void handle_watch (void *cls,
239     struct GNUNET_SERVER_Client *client,
240     const struct GNUNET_MessageHeader *message)
241 {
242   struct StoreKeyHashMessage *hm;
243
244   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch request from client.\n");
245   hm = (struct StoreKeyHashMessage *) message;
246   GNUNET_SERVER_client_mark_monitor(client);
247   GNUNET_SERVER_notification_context_add(nc, client);
248   GNUNET_CONTAINER_multihashmap_put(watchers, &hm->keyhash,
249      client, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
250   GNUNET_SERVER_receive_done(client, GNUNET_OK);
251 }
252
253 /**
254  * Handle an iterate request from client
255  *
256  * @param cls unused
257  * @param client identification of the client
258  * @param message the actual message
259  */
260 void handle_iterate (void *cls,
261     struct GNUNET_SERVER_Client *client,
262     const struct GNUNET_MessageHeader *message)
263 {
264   struct GNUNET_PEERSTORE_Record *record;
265   struct GNUNET_MessageHeader *endmsg;
266
267   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received an iterate request from client.\n");
268   record = PEERSTORE_parse_record_message(message);
269   if(NULL == record)
270   {
271     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Malformed iterate request from client\n"));
272     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
273     return;
274   }
275   if(NULL == record->sub_system)
276   {
277     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Sub system not supplied in client iterate request\n"));
278     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
279     return;
280   }
281   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Iterate request: ss `%s', peer `%s', key `%s'\n",
282       record->sub_system,
283       (NULL == record->peer) ? "NULL" : GNUNET_i2s(record->peer),
284       (NULL == record->key) ? "NULL" : record->key);
285   GNUNET_SERVER_notification_context_add(nc, client);
286   if(GNUNET_OK == db->iterate_records(db->cls,
287       record->sub_system,
288       record->peer,
289       record->key,
290       &record_iterator,
291       client))
292   {
293     endmsg = GNUNET_new(struct GNUNET_MessageHeader);
294     endmsg->size = htons(sizeof(struct GNUNET_MessageHeader));
295     endmsg->type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END);
296     GNUNET_SERVER_notification_context_unicast(nc, client, endmsg, GNUNET_NO);
297     GNUNET_free(endmsg);
298     GNUNET_SERVER_receive_done(client, GNUNET_OK);
299   }
300   else
301   {
302     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
303   }
304   PEERSTORE_destroy_record(record);
305 }
306
307 /**
308  * Handle a store request from client
309  *
310  * @param cls unused
311  * @param client identification of the client
312  * @param message the actual message
313  */
314 void handle_store (void *cls,
315     struct GNUNET_SERVER_Client *client,
316     const struct GNUNET_MessageHeader *message)
317 {
318   struct GNUNET_PEERSTORE_Record *record;
319   struct StoreRecordMessage *srm;
320
321   record = PEERSTORE_parse_record_message(message);
322   if(NULL == record)
323   {
324     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Malformed store request from client\n"));
325     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
326     return;
327   }
328   srm = (struct StoreRecordMessage *)message;
329   if(NULL == record->sub_system
330       || NULL == record->peer
331       || NULL == record->key)
332   {
333     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Full key not supplied in client store request\n"));
334     PEERSTORE_destroy_record(record);
335     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
336     return;
337   }
338   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
339       record->value_size,
340       record->sub_system,
341       GNUNET_i2s (record->peer),
342       record->key);
343   if(GNUNET_OK != db->store_record(db->cls,
344       record->sub_system,
345       record->peer,
346       record->key,
347       record->value,
348       record->value_size,
349       *record->expiry,
350       srm->options))
351   {
352     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to store requested value, sqlite database error."));
353     PEERSTORE_destroy_record(record);
354     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
355     return;
356   }
357   GNUNET_SERVER_receive_done(client, GNUNET_OK);
358   watch_notifier(record);
359   PEERSTORE_destroy_record(record);
360 }
361
362 /**
363  * Peerstore service runner.
364  *
365  * @param cls closure
366  * @param server the initialized server
367  * @param c configuration to use
368  */
369 static void
370 run (void *cls,
371      struct GNUNET_SERVER_Handle *server,
372      const struct GNUNET_CONFIGURATION_Handle *c)
373 {
374   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
375       {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
376       {&handle_iterate, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, 0},
377       {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH, sizeof(struct StoreKeyHashMessage)},
378       {&handle_watch_cancel, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL, sizeof(struct StoreKeyHashMessage)},
379       {NULL, NULL, 0, 0}
380   };
381   char *database;
382
383   cfg = c;
384   if (GNUNET_OK !=
385         GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
386                                                &database))
387     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
388
389   else
390   {
391     GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
392     db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
393     GNUNET_free(database);
394   }
395   if(NULL == db)
396           GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
397   else
398   {
399     nc = GNUNET_SERVER_notification_context_create (server, 16);
400     watchers = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
401     GNUNET_SCHEDULER_add_now(&cleanup_expired_records, NULL);
402     GNUNET_SERVER_add_handlers (server, handlers);
403     GNUNET_SERVER_disconnect_notify (server,
404              &handle_client_disconnect,
405              NULL);
406   }
407   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
408                                 &shutdown_task,
409                                 NULL);
410 }
411
412
413 /**
414  * The main function for the peerstore service.
415  *
416  * @param argc number of arguments from the command line
417  * @param argv command line arguments
418  * @return 0 ok, 1 on error
419  */
420 int
421 main (int argc, char *const *argv)
422 {
423   return (GNUNET_OK ==
424           GNUNET_SERVICE_run (argc,
425                               argv,
426                               "peerstore",
427                               GNUNET_SERVICE_OPTION_NONE,
428                               &run, NULL)) ? 0 : 1;
429 }
430
431 /* end of gnunet-service-peerstore.c */