- add send kx for axolotl
[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;
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   /* 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   struct SendClosure
377     sc = { .op_id = req->op_id, .client = client,
378            .channel_key = req->channel_key, .slave_key = req->slave_key,
379            .membership_test = req->do_membership_test };
380
381   int64_t ret;
382   uint64_t ret_frags = 0;
383   uint64_t first_message_id = GNUNET_ntohll (req->first_message_id);
384   uint64_t last_message_id = GNUNET_ntohll (req->last_message_id);
385   uint64_t limit = GNUNET_ntohll (req->message_limit);
386
387   if (0 == limit)
388     ret = db->message_get (db->cls, &req->channel_key,
389                            first_message_id, last_message_id,
390                            &ret_frags, &send_fragment, &sc);
391   else
392     ret = db->message_get_latest (db->cls, &req->channel_key, limit,
393                                   &ret_frags, &send_fragment, &sc);
394
395   switch (ret)
396   {
397   case GNUNET_YES:
398   case GNUNET_NO:
399     break;
400   default:
401     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
402                 _("Failed to get message!\n"));
403   }
404
405   send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
406   GNUNET_SERVER_receive_done (client, GNUNET_OK);
407 }
408
409
410 static void
411 handle_message_get_fragment (void *cls,
412                              struct GNUNET_SERVER_Client *client,
413                              const struct GNUNET_MessageHeader *msg)
414 {
415   const struct MessageGetFragmentRequest *
416     req = (const struct MessageGetFragmentRequest *) msg;
417   struct SendClosure
418     sc = { .op_id = req->op_id, .client = client,
419            .channel_key = req->channel_key, .slave_key = req->slave_key,
420            .membership_test = req->do_membership_test };
421
422   int ret = db->message_get_fragment (db->cls, &req->channel_key,
423                                       GNUNET_ntohll (req->message_id),
424                                       GNUNET_ntohll (req->fragment_offset),
425                                       &send_fragment, &sc);
426   switch (ret)
427   {
428   case GNUNET_YES:
429   case GNUNET_NO:
430     break;
431   default:
432     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
433                 _("Failed to get message fragment!\n"));
434   }
435
436   send_result_code (client, req->op_id, ret, NULL);
437   GNUNET_SERVER_receive_done (client, GNUNET_OK);
438 }
439
440
441 static void
442 handle_counters_get (void *cls,
443                      struct GNUNET_SERVER_Client *client,
444                      const struct GNUNET_MessageHeader *msg)
445 {
446   const struct OperationRequest *req = (const struct OperationRequest *) msg;
447   struct CountersResult res = { {0} };
448
449   int ret = db->counters_message_get (db->cls, &req->channel_key,
450                                       &res.max_fragment_id, &res.max_message_id,
451                                       &res.max_group_generation);
452   switch (ret)
453   {
454   case GNUNET_OK:
455     ret = db->counters_state_get (db->cls, &req->channel_key,
456                                   &res.max_state_message_id);
457   case GNUNET_NO:
458     break;
459   default:
460     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
461                 _("Failed to get master counters!\n"));
462   }
463
464   res.header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS);
465   res.header.size = htons (sizeof (res));
466   res.result_code = htonl (ret - INT32_MIN);
467   res.op_id = req->op_id;
468   res.max_fragment_id = GNUNET_htonll (res.max_fragment_id);
469   res.max_message_id = GNUNET_htonll (res.max_message_id);
470   res.max_group_generation = GNUNET_htonll (res.max_group_generation);
471   res.max_state_message_id = GNUNET_htonll (res.max_state_message_id);
472
473   GNUNET_SERVER_notification_context_add (nc, client);
474   GNUNET_SERVER_notification_context_unicast (nc, client, &res.header,
475                                               GNUNET_NO);
476
477   GNUNET_SERVER_receive_done (client, GNUNET_OK);
478 }
479
480
481 /* FIXME: stop processing further state modify messages after an error */
482 static void
483 handle_state_modify (void *cls,
484                      struct GNUNET_SERVER_Client *client,
485                      const struct GNUNET_MessageHeader *msg)
486 {
487   const struct StateModifyRequest *req
488     = (const struct StateModifyRequest *) msg;
489
490   int ret = GNUNET_SYSERR;
491   const char *name = (const char *) &req[1];
492   uint16_t name_size = ntohs (req->name_size);
493
494   if (name_size <= 2 || '\0' != name[name_size - 1])
495   {
496     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
497                 _("Tried to set invalid state variable name!\n"));
498     GNUNET_break_op (0);
499   }
500   else
501   {
502     ret = GNUNET_OK;
503
504     if (req->flags & STATE_OP_FIRST)
505     {
506       ret = db->state_modify_begin (db->cls, &req->channel_key,
507                                     GNUNET_ntohll (req->message_id),
508                                     GNUNET_ntohll (req->state_delta));
509     }
510     if (ret != GNUNET_OK)
511     {
512       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
513                   _("Failed to begin modifying state!\n"));
514     }
515     else
516     {
517       switch (req->oper)
518       {
519       case GNUNET_ENV_OP_ASSIGN:
520         ret = db->state_modify_set (db->cls, &req->channel_key,
521                                     (const char *) &req[1],
522                                     name + ntohs (req->name_size),
523                                     ntohs (req->header.size) - sizeof (*req)
524                                     - ntohs (req->name_size));
525         break;
526       default:
527 #if TODO
528         ret = GNUNET_ENV_operation ((const char *) &req[1],
529                                     current_value, current_value_size,
530                                     req->oper, name + ntohs (req->name_size),
531                                     ntohs (req->header.size) - sizeof (*req)
532                                     - ntohs (req->name_size), &value, &value_size);
533 #endif
534         ret = GNUNET_SYSERR;
535         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
536                     _("Unknown operator: %c\n"), req->oper);
537       }
538     }
539
540     if (GNUNET_OK == ret && req->flags & STATE_OP_LAST)
541     {
542       ret = db->state_modify_end (db->cls, &req->channel_key,
543                                   GNUNET_ntohll (req->message_id));
544       if (ret != GNUNET_OK)
545         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
546                     _("Failed to end modifying state!\n"));
547     }
548   }
549   send_result_code (client, req->op_id, ret, NULL);
550   GNUNET_SERVER_receive_done (client, GNUNET_OK);
551 }
552
553
554 /* FIXME: stop processing further state sync messages after an error */
555 static void
556 handle_state_sync (void *cls,
557                    struct GNUNET_SERVER_Client *client,
558                    const struct GNUNET_MessageHeader *msg)
559 {
560   const struct StateSyncRequest *req
561     = (const struct StateSyncRequest *) msg;
562
563   int ret = GNUNET_SYSERR;
564   const char *name = (const char *) &req[1];
565   uint16_t name_size = ntohs (req->name_size);
566
567   if (name_size <= 2 || '\0' != name[name_size - 1])
568   {
569     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
570                 _("Tried to set invalid state variable name!\n"));
571     GNUNET_break_op (0);
572   }
573   else
574   {
575     ret = GNUNET_OK;
576
577     if (req->flags & STATE_OP_FIRST)
578     {
579       ret = db->state_sync_begin (db->cls, &req->channel_key);
580     }
581     if (ret != GNUNET_OK)
582     {
583       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
584                   _("Failed to begin synchronizing state!\n"));
585     }
586     else
587     {
588       ret = db->state_sync_set (db->cls, &req->channel_key, name,
589                                 name + ntohs (req->name_size),
590                                 ntohs (req->header.size) - sizeof (*req)
591                                 - ntohs (req->name_size));
592     }
593
594     if (GNUNET_OK == ret && req->flags & STATE_OP_LAST)
595     {
596       ret = db->state_sync_end (db->cls, &req->channel_key,
597                                 GNUNET_ntohll (req->message_id));
598       if (ret != GNUNET_OK)
599         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
600                     _("Failed to end synchronizing state!\n"));
601     }
602   }
603   send_result_code (client, req->op_id, ret, NULL);
604   GNUNET_SERVER_receive_done (client, GNUNET_OK);
605 }
606
607
608 static void
609 handle_state_reset (void *cls,
610                     struct GNUNET_SERVER_Client *client,
611                     const struct GNUNET_MessageHeader *msg)
612 {
613   const struct OperationRequest *req =
614     (const struct OperationRequest *) msg;
615
616   int ret = db->state_reset (db->cls, &req->channel_key);
617
618   if (ret != GNUNET_OK)
619     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
620                 _("Failed to reset state!\n"));
621
622   send_result_code (client, req->op_id, ret, NULL);
623   GNUNET_SERVER_receive_done (client, GNUNET_OK);
624 }
625
626
627 static void
628 handle_state_hash_update (void *cls,
629                           struct GNUNET_SERVER_Client *client,
630                           const struct GNUNET_MessageHeader *msg)
631 {
632   const struct OperationRequest *req =
633     (const struct OperationRequest *) msg;
634
635   int ret = db->state_reset (db->cls, &req->channel_key);
636
637   if (ret != GNUNET_OK)
638     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
639                 _("Failed to reset state!\n"));
640
641   send_result_code (client, req->op_id, ret, NULL);
642   GNUNET_SERVER_receive_done (client, GNUNET_OK);
643 }
644
645
646 static void
647 handle_state_get (void *cls,
648                   struct GNUNET_SERVER_Client *client,
649                   const struct GNUNET_MessageHeader *msg)
650 {
651   const struct OperationRequest *req =
652     (const struct OperationRequest *) msg;
653
654   struct SendClosure sc = { .op_id = req->op_id, .client = client };
655   int64_t ret = GNUNET_SYSERR;
656   const char *name = (const char *) &req[1];
657   uint16_t name_size = ntohs (req->header.size) - sizeof (*req);
658
659   if (name_size <= 2 || '\0' != name[name_size - 1])
660   {
661     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
662                 _("Tried to get invalid state variable name!\n"));
663     GNUNET_break (0);
664   }
665   else
666   {
667     ret = db->state_get (db->cls, &req->channel_key, name,
668                          &send_state_var, &sc);
669     if (GNUNET_NO == ret && name_size >= 5) /* min: _a_b\0 */
670     {
671       char *p, *n = GNUNET_malloc (name_size);
672       memcpy (n, name, name_size);
673       while (&n[1] < (p = strrchr (n, '_')) && GNUNET_NO == ret)
674       {
675         *p = '\0';
676         ret = db->state_get (db->cls, &req->channel_key, n,
677                              &send_state_var, &sc);
678       }
679       GNUNET_free (n);
680     }
681   }
682   switch (ret)
683   {
684   case GNUNET_OK:
685   case GNUNET_NO:
686     break;
687   default:
688     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
689                 _("Failed to get state variable!\n"));
690   }
691
692   send_result_code (client, req->op_id, ret, NULL);
693   GNUNET_SERVER_receive_done (client, GNUNET_OK);
694 }
695
696
697 static void
698 handle_state_get_prefix (void *cls,
699                          struct GNUNET_SERVER_Client *client,
700                          const struct GNUNET_MessageHeader *msg)
701 {
702   const struct OperationRequest *req =
703     (const struct OperationRequest *) msg;
704
705   struct SendClosure sc = { .op_id = req->op_id, .client = client };
706   int64_t ret = GNUNET_SYSERR;
707   const char *name = (const char *) &req[1];
708   uint16_t name_size = ntohs (req->header.size) - sizeof (*req);
709
710   if (name_size <= 1 || '\0' != name[name_size - 1])
711   {
712     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
713                 _("Tried to get invalid state variable name!\n"));
714     GNUNET_break (0);
715   }
716   else
717   {
718     ret = db->state_get_prefix (db->cls, &req->channel_key, name,
719                                 &send_state_var, &sc);
720   }
721   switch (ret)
722   {
723   case GNUNET_OK:
724   case GNUNET_NO:
725     break;
726   default:
727     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
728                 _("Failed to get state variable!\n"));
729   }
730
731   send_result_code (client, req->op_id, ret, NULL);
732   GNUNET_SERVER_receive_done (client, GNUNET_OK);
733 }
734
735
736 /**
737  * Initialize the PSYCstore service.
738  *
739  * @param cls Closure.
740  * @param server The initialized server.
741  * @param c Configuration to use.
742  */
743 static void
744 run (void *cls, struct GNUNET_SERVER_Handle *server,
745      const struct GNUNET_CONFIGURATION_Handle *c)
746 {
747   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
748     { &handle_membership_store, NULL,
749       GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_STORE,
750       sizeof (struct MembershipStoreRequest) },
751
752     { &handle_membership_test, NULL,
753       GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_TEST,
754       sizeof (struct MembershipTestRequest) },
755
756     { &handle_fragment_store, NULL,
757       GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_STORE, 0, },
758
759     { &handle_fragment_get, NULL,
760       GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET,
761       sizeof (struct FragmentGetRequest) },
762
763     { &handle_message_get, NULL,
764       GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET,
765       sizeof (struct MessageGetRequest) },
766
767     { &handle_message_get_fragment, NULL,
768       GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET_FRAGMENT,
769       sizeof (struct MessageGetFragmentRequest) },
770
771     { &handle_counters_get, NULL,
772       GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET,
773       sizeof (struct OperationRequest) },
774
775     { &handle_state_modify, NULL,
776       GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_MODIFY, 0 },
777
778     { &handle_state_sync, NULL,
779       GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC, 0 },
780
781     { &handle_state_reset, NULL,
782       GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_RESET,
783       sizeof (struct OperationRequest) },
784
785     { &handle_state_hash_update, NULL,
786       GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_HASH_UPDATE,
787       sizeof (struct StateHashUpdateRequest) },
788
789     { &handle_state_get, NULL,
790       GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET, 0 },
791
792     { &handle_state_get_prefix, NULL,
793       GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET_PREFIX, 0 },
794
795     { NULL, NULL, 0, 0 }
796   };
797
798   cfg = c;
799
800   /* Loading database plugin */
801   char *database;
802   if (GNUNET_OK !=
803       GNUNET_CONFIGURATION_get_value_string (cfg, "psycstore", "database",
804                                              &database))
805   {
806     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
807   }
808   else
809   {
810     GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_psycstore_%s", database);
811     db = GNUNET_PLUGIN_load (db_lib_name, (void *) cfg);
812     GNUNET_free (database);
813   }
814   if (NULL == db)
815   {
816     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
817                 "Could not load database backend `%s'\n",
818                 db_lib_name);
819     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
820     return;
821   }
822
823   stats = GNUNET_STATISTICS_create ("psycstore", cfg);
824   GNUNET_SERVER_add_handlers (server, handlers);
825   nc = GNUNET_SERVER_notification_context_create (server, 1);
826   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
827                                 NULL);
828 }
829
830
831 /**
832  * The main function for the service.
833  *
834  * @param argc number of arguments from the command line
835  * @param argv command line arguments
836  * @return 0 ok, 1 on error
837  */
838 int
839 main (int argc, char *const *argv)
840 {
841   return (GNUNET_OK ==
842           GNUNET_SERVICE_run (argc, argv, "psycstore",
843                               GNUNET_SERVICE_OPTION_NONE,
844                               &run, NULL)) ? 0 : 1;
845 }
846
847
848 /* end of gnunet-service-psycstore.c */