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