psyc/social: request history & state from psycstore; more documentation, tests, cleanup
[oweals/gnunet.git] / src / psycstore / gnunet-service-psycstore.c
1 /*
2  * This file is part of GNUnet
3  * Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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 psycstore/gnunet-service-psycstore.c
23  * @brief PSYCstore service
24  * @author Gabor X Toth
25  * @author Christian Grothoff
26  */
27
28 #include <inttypes.h>
29
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_statistics_service.h"
35 #include "gnunet_psycstore_service.h"
36 #include "gnunet_psycstore_plugin.h"
37 #include "psycstore.h"
38
39
40 /**
41  * Handle to our current configuration.
42  */
43 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44
45 /**
46  * Handle to the statistics service.
47  */
48 static struct GNUNET_STATISTICS_Handle *stats;
49
50 /**
51  * Notification context, simplifies client broadcasts.
52  */
53 static struct GNUNET_SERVER_NotificationContext *nc;
54
55 /**
56  * Database handle
57  */
58 static struct GNUNET_PSYCSTORE_PluginFunctions *db;
59
60 /**
61  * Name of the database plugin
62  */
63 static char *db_lib_name;
64
65
66 /**
67  * Task run during shutdown.
68  *
69  * @param cls unused
70  * @param tc unused
71  */
72 static void
73 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
74 {
75   if (NULL != nc)
76   {
77     GNUNET_SERVER_notification_context_destroy (nc);
78     nc = NULL;
79   }
80   if (NULL != stats)
81   {
82     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
83     stats = NULL;
84   }
85   GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
86   GNUNET_free (db_lib_name);
87   db_lib_name = NULL;
88 }
89
90
91 /**
92  * Send a result code back to the client.
93  *
94  * @param client
95  *        Client that should receive the result code.
96  * @param result_code
97  *        Code to transmit.
98  * @param op_id
99  *        Operation ID in network byte order.
100  * @param err_msg
101  *        Error message to include (or NULL for none).
102  */
103 static void
104 send_result_code (struct GNUNET_SERVER_Client *client, uint64_t op_id,
105                   int64_t result_code, const char *err_msg)
106 {
107   struct OperationResult *res;
108   size_t err_size = 0;
109
110   if (NULL != err_msg)
111     err_size = strnlen (err_msg,
112                         GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*res) - 1) + 1;
113   res = GNUNET_malloc (sizeof (struct OperationResult) + err_size);
114   res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE);
115   res->header.size = htons (sizeof (struct OperationResult) + err_size);
116   res->result_code = GNUNET_htonll (result_code - INT64_MIN);
117   res->op_id = op_id;
118   if (0 < err_size)
119   {
120     memcpy (&res[1], err_msg, err_size);
121     ((char *) &res[1])[err_size - 1] = '\0';
122   }
123   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
124               "Sending result to client: %" PRId64 " (%s)\n",
125               result_code, err_msg);
126   GNUNET_SERVER_notification_context_add (nc, client);
127   GNUNET_SERVER_notification_context_unicast (nc, client, &res->header,
128                                               GNUNET_NO);
129   GNUNET_free (res);
130 }
131
132
133 enum
134 {
135   MEMBERSHIP_TEST_NOT_NEEDED = 0,
136   MEMBERSHIP_TEST_NEEDED = 1,
137   MEMBERSHIP_TEST_DONE = 2,
138 } MessageMembershipTest;
139
140
141 struct SendClosure
142 {
143   struct GNUNET_SERVER_Client *client;
144
145   /**
146    * Channel's public key.
147    */
148   struct GNUNET_CRYPTO_EddsaPublicKey channel_key;
149
150   /**
151    * Slave's public key.
152    */
153   struct GNUNET_CRYPTO_EcdsaPublicKey slave_key;
154
155   /**
156    * Operation ID.
157    */
158   uint64_t op_id;
159
160   /**
161    * Membership test result.
162    */
163   int membership_test_result;
164
165   /**
166    * Do membership test with @a slave_key before returning fragment?
167    * @see enum MessageMembershipTest
168    */
169   uint8_t membership_test;
170 };
171
172
173 static int
174 send_fragment (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg,
175                enum GNUNET_PSYCSTORE_MessageFlags flags)
176 {
177   struct SendClosure *sc = cls;
178   struct FragmentResult *res;
179
180   if (MEMBERSHIP_TEST_NEEDED == sc->membership_test)
181   {
182     sc->membership_test = MEMBERSHIP_TEST_DONE;
183     sc->membership_test_result
184       = db->membership_test (db->cls, &sc->channel_key, &sc->slave_key,
185                              GNUNET_ntohll (msg->message_id));
186     switch (sc->membership_test_result)
187     {
188     case GNUNET_YES:
189       break;
190
191     case GNUNET_NO:
192     case GNUNET_SYSERR:
193       return GNUNET_NO;
194     }
195   }
196
197   size_t msg_size = ntohs (msg->header.size);
198
199   res = GNUNET_malloc (sizeof (struct FragmentResult) + msg_size);
200   res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_FRAGMENT);
201   res->header.size = htons (sizeof (struct FragmentResult) + msg_size);
202   res->op_id = sc->op_id;
203   res->psycstore_flags = htonl (flags);
204   memcpy (&res[1], msg, msg_size);
205   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206               "Sending fragment %ld to client\n",
207               GNUNET_ntohll (msg->fragment_id));
208   GNUNET_free (msg);
209   GNUNET_SERVER_notification_context_add (nc, sc->client);
210   GNUNET_SERVER_notification_context_unicast (nc, sc->client, &res->header,
211                                               GNUNET_NO);
212   GNUNET_free (res);
213   return GNUNET_YES;
214 }
215
216
217 static int
218 send_state_var (void *cls, const char *name,
219                 const void *value, size_t value_size)
220 {
221   struct SendClosure *sc = cls;
222   struct StateResult *res;
223   size_t name_size = strlen (name) + 1;
224
225   /** @todo FIXME: split up value into 64k chunks */
226
227   res = GNUNET_malloc (sizeof (struct StateResult) + name_size + value_size);
228   res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_STATE);
229   res->header.size = htons (sizeof (struct StateResult) + name_size + value_size);
230   res->op_id = sc->op_id;
231   res->name_size = htons (name_size);
232   memcpy (&res[1], name, name_size);
233   memcpy ((char *) &res[1] + name_size, value, value_size);
234   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235               "Sending state variable %s to client\n", name);
236   GNUNET_SERVER_notification_context_add (nc, sc->client);
237   GNUNET_SERVER_notification_context_unicast (nc, sc->client, &res->header,
238                                               GNUNET_NO);
239   GNUNET_free (res);
240   return GNUNET_OK;
241 }
242
243
244 static void
245 handle_membership_store (void *cls,
246                          struct GNUNET_SERVER_Client *client,
247                          const struct GNUNET_MessageHeader *msg)
248 {
249   const struct MembershipStoreRequest *req =
250     (const struct MembershipStoreRequest *) msg;
251
252   int ret = db->membership_store (db->cls, &req->channel_key, &req->slave_key,
253                                   req->did_join,
254                                   GNUNET_ntohll (req->announced_at),
255                                   GNUNET_ntohll (req->effective_since),
256                                   GNUNET_ntohll (req->group_generation));
257
258   if (ret != GNUNET_OK)
259     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260                 _("Failed to store membership information!\n"));
261
262   send_result_code (client, req->op_id, ret, NULL);
263   GNUNET_SERVER_receive_done (client, GNUNET_OK);
264 }
265
266
267 static void
268 handle_membership_test (void *cls,
269                         struct GNUNET_SERVER_Client *client,
270                         const struct GNUNET_MessageHeader *msg)
271 {
272   const struct MembershipTestRequest *req =
273     (const struct MembershipTestRequest *) msg;
274
275   int ret = db->membership_test (db->cls, &req->channel_key, &req->slave_key,
276                                  GNUNET_ntohll (req->message_id));
277   switch (ret)
278   {
279   case GNUNET_YES:
280   case GNUNET_NO:
281     break;
282   default:
283     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
284                 _("Failed to test membership!\n"));
285   }
286
287   send_result_code (client, req->op_id, ret, NULL);
288   GNUNET_SERVER_receive_done (client, GNUNET_OK);
289 }
290
291
292 static void
293 handle_fragment_store (void *cls,
294                        struct GNUNET_SERVER_Client *client,
295                        const struct GNUNET_MessageHeader *msg)
296 {
297   const struct FragmentStoreRequest *req =
298     (const struct FragmentStoreRequest *) msg;
299
300   int ret = db->fragment_store (db->cls, &req->channel_key,
301                                 (const struct GNUNET_MULTICAST_MessageHeader *)
302                                 &req[1], ntohl (req->psycstore_flags));
303
304   if (ret != GNUNET_OK)
305     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
306                 _("Failed to store fragment!\n"));
307
308   send_result_code (client, req->op_id, ret, NULL);
309   GNUNET_SERVER_receive_done (client, GNUNET_OK);
310 }
311
312
313 static void
314 handle_fragment_get (void *cls,
315                      struct GNUNET_SERVER_Client *client,
316                      const struct GNUNET_MessageHeader *msg)
317 {
318   const struct FragmentGetRequest *
319     req = (const struct FragmentGetRequest *) msg;
320   struct SendClosure
321     sc = { .op_id = req->op_id, .client = client,
322            .channel_key = req->channel_key, .slave_key = req->slave_key,
323            .membership_test = req->do_membership_test };
324
325   int64_t ret;
326   uint64_t ret_frags = 0;
327   uint64_t first_fragment_id = GNUNET_ntohll (req->first_fragment_id);
328   uint64_t last_fragment_id = GNUNET_ntohll (req->last_fragment_id);
329   uint64_t limit = GNUNET_ntohll (req->fragment_limit);
330
331   if (0 == limit)
332     ret = db->fragment_get (db->cls, &req->channel_key,
333                             first_fragment_id, last_fragment_id,
334                             &ret_frags, &send_fragment, &sc);
335   else
336     ret = db->fragment_get_latest (db->cls, &req->channel_key, limit,
337                                    &ret_frags, &send_fragment, &sc);
338
339   switch (ret)
340   {
341   case GNUNET_YES:
342   case GNUNET_NO:
343     if (MEMBERSHIP_TEST_DONE == sc.membership_test)
344     {
345       switch (sc.membership_test_result)
346       {
347       case GNUNET_YES:
348         break;
349
350       case GNUNET_NO:
351         ret = GNUNET_PSYCSTORE_MEMBERSHIP_TEST_FAILED;
352         break;
353
354       case GNUNET_SYSERR:
355         ret = GNUNET_SYSERR;
356         break;
357       }
358     }
359     break;
360   default:
361     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
362                 _("Failed to get fragment!\n"));
363   }
364   send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
365   GNUNET_SERVER_receive_done (client, GNUNET_OK);
366 }
367
368
369 static void
370 handle_message_get (void *cls,
371                     struct GNUNET_SERVER_Client *client,
372                     const struct GNUNET_MessageHeader *msg)
373 {
374   const struct MessageGetRequest *
375     req = (const struct MessageGetRequest *) msg;
376   uint16_t size = ntohs (msg->size);
377   const char *method_prefix = (const char *) &req[1];
378
379   if (size < sizeof (*req) + 1
380       || '\0' != method_prefix[size - sizeof (*req) - 1])
381   {
382     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
383                 "Message get: invalid method prefix. size: %u < %u?\n",
384                 size, sizeof (*req) + 1);
385     GNUNET_break (0);
386     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
387     return;
388   }
389
390   struct SendClosure
391     sc = { .op_id = req->op_id, .client = client,
392            .channel_key = req->channel_key, .slave_key = req->slave_key,
393            .membership_test = req->do_membership_test };
394
395   int64_t ret;
396   uint64_t ret_frags = 0;
397   uint64_t first_message_id = GNUNET_ntohll (req->first_message_id);
398   uint64_t last_message_id = GNUNET_ntohll (req->last_message_id);
399   uint64_t limit = GNUNET_ntohll (req->message_limit);
400
401   /** @todo method_prefix */
402   if (0 == limit)
403     ret = db->message_get (db->cls, &req->channel_key,
404                            first_message_id, last_message_id,
405                            &ret_frags, &send_fragment, &sc);
406   else
407     ret = db->message_get_latest (db->cls, &req->channel_key, limit,
408                                   &ret_frags, &send_fragment, &sc);
409
410   switch (ret)
411   {
412   case GNUNET_YES:
413   case GNUNET_NO:
414     break;
415   default:
416     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
417                 _("Failed to get message!\n"));
418   }
419
420   send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
421   GNUNET_SERVER_receive_done (client, GNUNET_OK);
422 }
423
424
425 static void
426 handle_message_get_fragment (void *cls,
427                              struct GNUNET_SERVER_Client *client,
428                              const struct GNUNET_MessageHeader *msg)
429 {
430   const struct MessageGetFragmentRequest *
431     req = (const struct MessageGetFragmentRequest *) msg;
432   struct SendClosure
433     sc = { .op_id = req->op_id, .client = client,
434            .channel_key = req->channel_key, .slave_key = req->slave_key,
435            .membership_test = req->do_membership_test };
436
437   int ret = db->message_get_fragment (db->cls, &req->channel_key,
438                                       GNUNET_ntohll (req->message_id),
439                                       GNUNET_ntohll (req->fragment_offset),
440                                       &send_fragment, &sc);
441   switch (ret)
442   {
443   case GNUNET_YES:
444   case GNUNET_NO:
445     break;
446   default:
447     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
448                 _("Failed to get message fragment!\n"));
449   }
450
451   send_result_code (client, req->op_id, ret, NULL);
452   GNUNET_SERVER_receive_done (client, GNUNET_OK);
453 }
454
455
456 static void
457 handle_counters_get (void *cls,
458                      struct GNUNET_SERVER_Client *client,
459                      const struct GNUNET_MessageHeader *msg)
460 {
461   const struct OperationRequest *req = (const struct OperationRequest *) msg;
462   struct CountersResult res = { {0} };
463
464   int ret = db->counters_message_get (db->cls, &req->channel_key,
465                                       &res.max_fragment_id, &res.max_message_id,
466                                       &res.max_group_generation);
467   switch (ret)
468   {
469   case GNUNET_OK:
470     ret = db->counters_state_get (db->cls, &req->channel_key,
471                                   &res.max_state_message_id);
472   case GNUNET_NO:
473     break;
474   default:
475     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
476                 _("Failed to get master counters!\n"));
477   }
478
479   res.header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS);
480   res.header.size = htons (sizeof (res));
481   res.result_code = htonl (ret - INT32_MIN);
482   res.op_id = req->op_id;
483   res.max_fragment_id = GNUNET_htonll (res.max_fragment_id);
484   res.max_message_id = GNUNET_htonll (res.max_message_id);
485   res.max_group_generation = GNUNET_htonll (res.max_group_generation);
486   res.max_state_message_id = GNUNET_htonll (res.max_state_message_id);
487
488   GNUNET_SERVER_notification_context_add (nc, client);
489   GNUNET_SERVER_notification_context_unicast (nc, client, &res.header,
490                                               GNUNET_NO);
491
492   GNUNET_SERVER_receive_done (client, GNUNET_OK);
493 }
494
495
496 /** @todo FIXME: stop processing further state modify messages after an error */
497 static void
498 handle_state_modify (void *cls,
499                      struct GNUNET_SERVER_Client *client,
500                      const struct GNUNET_MessageHeader *msg)
501 {
502   const struct StateModifyRequest *req
503     = (const struct StateModifyRequest *) msg;
504
505   int ret = GNUNET_SYSERR;
506   const char *name = (const char *) &req[1];
507   uint16_t name_size = ntohs (req->name_size);
508
509   if (name_size <= 2 || '\0' != name[name_size - 1])
510   {
511     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
512                 _("Tried to set invalid state variable name!\n"));
513     GNUNET_break_op (0);
514   }
515   else
516   {
517     ret = GNUNET_OK;
518
519     if (req->flags & STATE_OP_FIRST)
520     {
521       ret = db->state_modify_begin (db->cls, &req->channel_key,
522                                     GNUNET_ntohll (req->message_id),
523                                     GNUNET_ntohll (req->state_delta));
524     }
525     if (ret != GNUNET_OK)
526     {
527       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
528                   _("Failed to begin modifying state!\n"));
529     }
530     else
531     {
532       switch (req->oper)
533       {
534       case GNUNET_ENV_OP_ASSIGN:
535         ret = db->state_modify_set (db->cls, &req->channel_key,
536                                     (const char *) &req[1],
537                                     name + ntohs (req->name_size),
538                                     ntohs (req->header.size) - sizeof (*req)
539                                     - ntohs (req->name_size));
540         break;
541       default:
542 #if TODO
543         ret = GNUNET_ENV_operation ((const char *) &req[1],
544                                     current_value, current_value_size,
545                                     req->oper, name + ntohs (req->name_size),
546                                     ntohs (req->header.size) - sizeof (*req)
547                                     - ntohs (req->name_size), &value, &value_size);
548 #endif
549         ret = GNUNET_SYSERR;
550         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
551                     _("Unknown operator: %c\n"), req->oper);
552       }
553     }
554
555     if (GNUNET_OK == ret && req->flags & STATE_OP_LAST)
556     {
557       ret = db->state_modify_end (db->cls, &req->channel_key,
558                                   GNUNET_ntohll (req->message_id));
559       if (ret != GNUNET_OK)
560         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
561                     _("Failed to end modifying state!\n"));
562     }
563   }
564   send_result_code (client, req->op_id, ret, NULL);
565   GNUNET_SERVER_receive_done (client, GNUNET_OK);
566 }
567
568
569 /** @todo FIXME: stop processing further state sync messages after an error */
570 static void
571 handle_state_sync (void *cls,
572                    struct GNUNET_SERVER_Client *client,
573                    const struct GNUNET_MessageHeader *msg)
574 {
575   const struct StateSyncRequest *req
576     = (const struct StateSyncRequest *) msg;
577
578   int ret = GNUNET_SYSERR;
579   const char *name = (const char *) &req[1];
580   uint16_t name_size = ntohs (req->name_size);
581
582   if (name_size <= 2 || '\0' != name[name_size - 1])
583   {
584     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
585                 _("Tried to set invalid state variable name!\n"));
586     GNUNET_break_op (0);
587   }
588   else
589   {
590     ret = GNUNET_OK;
591
592     if (req->flags & STATE_OP_FIRST)
593     {
594       ret = db->state_sync_begin (db->cls, &req->channel_key);
595     }
596     if (ret != GNUNET_OK)
597     {
598       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
599                   _("Failed to begin synchronizing state!\n"));
600     }
601     else
602     {
603       ret = db->state_sync_set (db->cls, &req->channel_key, name,
604                                 name + ntohs (req->name_size),
605                                 ntohs (req->header.size) - sizeof (*req)
606                                 - ntohs (req->name_size));
607     }
608
609     if (GNUNET_OK == ret && req->flags & STATE_OP_LAST)
610     {
611       ret = db->state_sync_end (db->cls, &req->channel_key,
612                                 GNUNET_ntohll (req->message_id));
613       if (ret != GNUNET_OK)
614         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
615                     _("Failed to end synchronizing state!\n"));
616     }
617   }
618   send_result_code (client, req->op_id, ret, NULL);
619   GNUNET_SERVER_receive_done (client, GNUNET_OK);
620 }
621
622
623 static void
624 handle_state_reset (void *cls,
625                     struct GNUNET_SERVER_Client *client,
626                     const struct GNUNET_MessageHeader *msg)
627 {
628   const struct OperationRequest *req =
629     (const struct OperationRequest *) msg;
630
631   int ret = db->state_reset (db->cls, &req->channel_key);
632
633   if (ret != GNUNET_OK)
634     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
635                 _("Failed to reset state!\n"));
636
637   send_result_code (client, req->op_id, ret, NULL);
638   GNUNET_SERVER_receive_done (client, GNUNET_OK);
639 }
640
641
642 static void
643 handle_state_hash_update (void *cls,
644                           struct GNUNET_SERVER_Client *client,
645                           const struct GNUNET_MessageHeader *msg)
646 {
647   const struct OperationRequest *req =
648     (const struct OperationRequest *) msg;
649
650   int ret = db->state_reset (db->cls, &req->channel_key);
651
652   if (ret != GNUNET_OK)
653     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
654                 _("Failed to reset state!\n"));
655
656   send_result_code (client, req->op_id, ret, NULL);
657   GNUNET_SERVER_receive_done (client, GNUNET_OK);
658 }
659
660
661 static void
662 handle_state_get (void *cls,
663                   struct GNUNET_SERVER_Client *client,
664                   const struct GNUNET_MessageHeader *msg)
665 {
666   const struct OperationRequest *req =
667     (const struct OperationRequest *) msg;
668
669   struct SendClosure sc = { .op_id = req->op_id, .client = client };
670   int64_t ret = GNUNET_SYSERR;
671   const char *name = (const char *) &req[1];
672   uint16_t name_size = ntohs (req->header.size) - sizeof (*req);
673
674   if (name_size <= 2 || '\0' != name[name_size - 1])
675   {
676     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
677                 _("Tried to get invalid state variable name!\n"));
678     GNUNET_break (0);
679   }
680   else
681   {
682     ret = db->state_get (db->cls, &req->channel_key, name,
683                          &send_state_var, &sc);
684     if (GNUNET_NO == ret && name_size >= 5) /* min: _a_b\0 */
685     {
686       char *p, *n = GNUNET_malloc (name_size);
687       memcpy (n, name, name_size);
688       while (&n[1] < (p = strrchr (n, '_')) && GNUNET_NO == ret)
689       {
690         *p = '\0';
691         ret = db->state_get (db->cls, &req->channel_key, n,
692                              &send_state_var, &sc);
693       }
694       GNUNET_free (n);
695     }
696   }
697   switch (ret)
698   {
699   case GNUNET_OK:
700   case GNUNET_NO:
701     break;
702   default:
703     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
704                 _("Failed to get state variable!\n"));
705   }
706
707   send_result_code (client, req->op_id, ret, NULL);
708   GNUNET_SERVER_receive_done (client, GNUNET_OK);
709 }
710
711
712 static void
713 handle_state_get_prefix (void *cls,
714                          struct GNUNET_SERVER_Client *client,
715                          const struct GNUNET_MessageHeader *msg)
716 {
717   const struct OperationRequest *req =
718     (const struct OperationRequest *) msg;
719
720   struct SendClosure sc = { .op_id = req->op_id, .client = client };
721   int64_t ret = GNUNET_SYSERR;
722   const char *name = (const char *) &req[1];
723   uint16_t name_size = ntohs (req->header.size) - sizeof (*req);
724
725   if (name_size <= 1 || '\0' != name[name_size - 1])
726   {
727     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
728                 _("Tried to get invalid state variable name!\n"));
729     GNUNET_break (0);
730   }
731   else
732   {
733     ret = db->state_get_prefix (db->cls, &req->channel_key, name,
734                                 &send_state_var, &sc);
735   }
736   switch (ret)
737   {
738   case GNUNET_OK:
739   case GNUNET_NO:
740     break;
741   default:
742     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
743                 _("Failed to get state variable!\n"));
744   }
745
746   send_result_code (client, req->op_id, ret, NULL);
747   GNUNET_SERVER_receive_done (client, GNUNET_OK);
748 }
749
750
751 /**
752  * Initialize the PSYCstore service.
753  *
754  * @param cls Closure.
755  * @param server The initialized server.
756  * @param c Configuration to use.
757  */
758 static void
759 run (void *cls, struct GNUNET_SERVER_Handle *server,
760      const struct GNUNET_CONFIGURATION_Handle *c)
761 {
762   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
763     { &handle_membership_store, NULL,
764       GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_STORE,
765       sizeof (struct MembershipStoreRequest) },
766
767     { &handle_membership_test, NULL,
768       GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_TEST,
769       sizeof (struct MembershipTestRequest) },
770
771     { &handle_fragment_store, NULL,
772       GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_STORE, 0, },
773
774     { &handle_fragment_get, NULL,
775       GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET,
776       sizeof (struct FragmentGetRequest) },
777
778     { &handle_message_get, NULL,
779       GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET, 0 },
780
781     { &handle_message_get_fragment, NULL,
782       GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET_FRAGMENT,
783       sizeof (struct MessageGetFragmentRequest) },
784
785     { &handle_counters_get, NULL,
786       GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET,
787       sizeof (struct OperationRequest) },
788
789     { &handle_state_modify, NULL,
790       GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_MODIFY, 0 },
791
792     { &handle_state_sync, NULL,
793       GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC, 0 },
794
795     { &handle_state_reset, NULL,
796       GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_RESET,
797       sizeof (struct OperationRequest) },
798
799     { &handle_state_hash_update, NULL,
800       GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_HASH_UPDATE,
801       sizeof (struct StateHashUpdateRequest) },
802
803     { &handle_state_get, NULL,
804       GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET, 0 },
805
806     { &handle_state_get_prefix, NULL,
807       GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET_PREFIX, 0 },
808
809     { NULL, NULL, 0, 0 }
810   };
811
812   cfg = c;
813
814   /* Loading database plugin */
815   char *database;
816   if (GNUNET_OK !=
817       GNUNET_CONFIGURATION_get_value_string (cfg, "psycstore", "database",
818                                              &database))
819   {
820     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
821   }
822   else
823   {
824     GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_psycstore_%s", database);
825     db = GNUNET_PLUGIN_load (db_lib_name, (void *) cfg);
826     GNUNET_free (database);
827   }
828   if (NULL == db)
829   {
830     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
831                 "Could not load database backend `%s'\n",
832                 db_lib_name);
833     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
834     return;
835   }
836
837   stats = GNUNET_STATISTICS_create ("psycstore", cfg);
838   GNUNET_SERVER_add_handlers (server, handlers);
839   nc = GNUNET_SERVER_notification_context_create (server, 1);
840   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
841                                 NULL);
842 }
843
844
845 /**
846  * The main function for the service.
847  *
848  * @param argc number of arguments from the command line
849  * @param argv command line arguments
850  * @return 0 ok, 1 on error
851  */
852 int
853 main (int argc, char *const *argv)
854 {
855   return (GNUNET_OK ==
856           GNUNET_SERVICE_run (argc, argv, "psycstore",
857                               GNUNET_SERVICE_OPTION_NONE,
858                               &run, NULL)) ? 0 : 1;
859 }
860
861
862 /* end of gnunet-service-psycstore.c */