2 * This file is part of GNUnet
3 * Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @file psycstore/gnunet-service-psycstore.c
23 * @brief PSYCstore service
24 * @author Gabor X Toth
25 * @author Christian Grothoff
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_psyc_util_lib.h"
36 #include "gnunet_psycstore_service.h"
37 #include "gnunet_psycstore_plugin.h"
38 #include "psycstore.h"
42 * Handle to our current configuration.
44 static const struct GNUNET_CONFIGURATION_Handle *cfg;
47 * Handle to the statistics service.
49 static struct GNUNET_STATISTICS_Handle *stats;
52 * Notification context, simplifies client broadcasts.
54 static struct GNUNET_SERVER_NotificationContext *nc;
59 static struct GNUNET_PSYCSTORE_PluginFunctions *db;
62 * Name of the database plugin
64 static char *db_lib_name;
68 * Task run during shutdown.
74 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
78 GNUNET_SERVER_notification_context_destroy (nc);
83 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
86 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
87 GNUNET_free (db_lib_name);
93 * Send a result code back to the client.
96 * Client that should receive the result code.
100 * Operation ID in network byte order.
102 * Error message to include (or NULL for none).
105 send_result_code (struct GNUNET_SERVER_Client *client, uint64_t op_id,
106 int64_t result_code, const char *err_msg)
108 struct OperationResult *res;
112 err_size = strnlen (err_msg,
113 GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*res) - 1) + 1;
114 res = GNUNET_malloc (sizeof (struct OperationResult) + err_size);
115 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE);
116 res->header.size = htons (sizeof (struct OperationResult) + err_size);
117 res->result_code = GNUNET_htonll (result_code - INT64_MIN);
121 memcpy (&res[1], err_msg, err_size);
122 ((char *) &res[1])[err_size - 1] = '\0';
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125 "Sending result to client: %" PRId64 " (%s)\n",
126 result_code, err_msg);
127 GNUNET_SERVER_notification_context_add (nc, client);
128 GNUNET_SERVER_notification_context_unicast (nc, client, &res->header,
136 MEMBERSHIP_TEST_NOT_NEEDED = 0,
137 MEMBERSHIP_TEST_NEEDED = 1,
138 MEMBERSHIP_TEST_DONE = 2,
139 } MessageMembershipTest;
144 struct GNUNET_SERVER_Client *client;
147 * Channel's public key.
149 struct GNUNET_CRYPTO_EddsaPublicKey channel_key;
152 * Slave's public key.
154 struct GNUNET_CRYPTO_EcdsaPublicKey slave_key;
162 * Membership test result.
164 int membership_test_result;
167 * Do membership test with @a slave_key before returning fragment?
168 * @see enum MessageMembershipTest
170 uint8_t membership_test;
175 send_fragment (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg,
176 enum GNUNET_PSYCSTORE_MessageFlags flags)
178 struct SendClosure *sc = cls;
179 struct FragmentResult *res;
181 if (MEMBERSHIP_TEST_NEEDED == sc->membership_test)
183 sc->membership_test = MEMBERSHIP_TEST_DONE;
184 sc->membership_test_result
185 = db->membership_test (db->cls, &sc->channel_key, &sc->slave_key,
186 GNUNET_ntohll (msg->message_id));
187 switch (sc->membership_test_result)
198 size_t msg_size = ntohs (msg->header.size);
200 res = GNUNET_malloc (sizeof (struct FragmentResult) + msg_size);
201 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_FRAGMENT);
202 res->header.size = htons (sizeof (struct FragmentResult) + msg_size);
203 res->op_id = sc->op_id;
204 res->psycstore_flags = htonl (flags);
205 memcpy (&res[1], msg, msg_size);
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Sending fragment %ld to client\n",
208 GNUNET_ntohll (msg->fragment_id));
210 GNUNET_SERVER_notification_context_add (nc, sc->client);
211 GNUNET_SERVER_notification_context_unicast (nc, sc->client, &res->header,
219 send_state_var (void *cls, const char *name,
220 const void *value, uint32_t value_size)
222 struct SendClosure *sc = cls;
223 struct StateResult *res;
224 size_t name_size = strlen (name) + 1;
226 /** @todo FIXME: split up value into 64k chunks */
228 res = GNUNET_malloc (sizeof (struct StateResult) + name_size + value_size);
229 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_STATE);
230 res->header.size = htons (sizeof (struct StateResult) + name_size + value_size);
231 res->op_id = sc->op_id;
232 res->name_size = htons (name_size);
233 memcpy (&res[1], name, name_size);
234 memcpy ((char *) &res[1] + name_size, value, value_size);
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Sending state variable %s to client\n", name);
237 GNUNET_SERVER_notification_context_add (nc, sc->client);
238 GNUNET_SERVER_notification_context_unicast (nc, sc->client, &res->header,
246 handle_membership_store (void *cls,
247 struct GNUNET_SERVER_Client *client,
248 const struct GNUNET_MessageHeader *msg)
250 const struct MembershipStoreRequest *req =
251 (const struct MembershipStoreRequest *) msg;
253 int ret = db->membership_store (db->cls, &req->channel_key, &req->slave_key,
255 GNUNET_ntohll (req->announced_at),
256 GNUNET_ntohll (req->effective_since),
257 GNUNET_ntohll (req->group_generation));
259 if (ret != GNUNET_OK)
260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
261 _("Failed to store membership information!\n"));
263 send_result_code (client, req->op_id, ret, NULL);
264 GNUNET_SERVER_receive_done (client, GNUNET_OK);
269 handle_membership_test (void *cls,
270 struct GNUNET_SERVER_Client *client,
271 const struct GNUNET_MessageHeader *msg)
273 const struct MembershipTestRequest *req =
274 (const struct MembershipTestRequest *) msg;
276 int ret = db->membership_test (db->cls, &req->channel_key, &req->slave_key,
277 GNUNET_ntohll (req->message_id));
284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
285 _("Failed to test membership!\n"));
288 send_result_code (client, req->op_id, ret, NULL);
289 GNUNET_SERVER_receive_done (client, GNUNET_OK);
294 handle_fragment_store (void *cls,
295 struct GNUNET_SERVER_Client *client,
296 const struct GNUNET_MessageHeader *msg)
298 const struct FragmentStoreRequest *req =
299 (const struct FragmentStoreRequest *) msg;
301 int ret = db->fragment_store (db->cls, &req->channel_key,
302 (const struct GNUNET_MULTICAST_MessageHeader *)
303 &req[1], ntohl (req->psycstore_flags));
305 if (ret != GNUNET_OK)
306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307 _("Failed to store fragment!\n"));
309 send_result_code (client, req->op_id, ret, NULL);
310 GNUNET_SERVER_receive_done (client, GNUNET_OK);
315 handle_fragment_get (void *cls,
316 struct GNUNET_SERVER_Client *client,
317 const struct GNUNET_MessageHeader *msg)
319 const struct FragmentGetRequest *
320 req = (const struct FragmentGetRequest *) msg;
322 sc = { .op_id = req->op_id, .client = client,
323 .channel_key = req->channel_key, .slave_key = req->slave_key,
324 .membership_test = req->do_membership_test };
327 uint64_t ret_frags = 0;
328 uint64_t first_fragment_id = GNUNET_ntohll (req->first_fragment_id);
329 uint64_t last_fragment_id = GNUNET_ntohll (req->last_fragment_id);
330 uint64_t limit = GNUNET_ntohll (req->fragment_limit);
333 ret = db->fragment_get (db->cls, &req->channel_key,
334 first_fragment_id, last_fragment_id,
335 &ret_frags, &send_fragment, &sc);
337 ret = db->fragment_get_latest (db->cls, &req->channel_key, limit,
338 &ret_frags, &send_fragment, &sc);
344 if (MEMBERSHIP_TEST_DONE == sc.membership_test)
346 switch (sc.membership_test_result)
352 ret = GNUNET_PSYCSTORE_MEMBERSHIP_TEST_FAILED;
362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
363 _("Failed to get fragment!\n"));
365 send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
366 GNUNET_SERVER_receive_done (client, GNUNET_OK);
371 handle_message_get (void *cls,
372 struct GNUNET_SERVER_Client *client,
373 const struct GNUNET_MessageHeader *msg)
375 const struct MessageGetRequest *
376 req = (const struct MessageGetRequest *) msg;
377 uint16_t size = ntohs (msg->size);
378 const char *method_prefix = (const char *) &req[1];
380 if (size < sizeof (*req) + 1
381 || '\0' != method_prefix[size - sizeof (*req) - 1])
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
384 "Message get: invalid method prefix. size: %u < %u?\n",
385 size, sizeof (*req) + 1);
387 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
392 sc = { .op_id = req->op_id, .client = client,
393 .channel_key = req->channel_key, .slave_key = req->slave_key,
394 .membership_test = req->do_membership_test };
397 uint64_t ret_frags = 0;
398 uint64_t first_message_id = GNUNET_ntohll (req->first_message_id);
399 uint64_t last_message_id = GNUNET_ntohll (req->last_message_id);
400 uint64_t limit = GNUNET_ntohll (req->message_limit);
402 /** @todo method_prefix */
404 ret = db->message_get (db->cls, &req->channel_key,
405 first_message_id, last_message_id,
406 &ret_frags, &send_fragment, &sc);
408 ret = db->message_get_latest (db->cls, &req->channel_key, limit,
409 &ret_frags, &send_fragment, &sc);
417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
418 _("Failed to get message!\n"));
421 send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
422 GNUNET_SERVER_receive_done (client, GNUNET_OK);
427 handle_message_get_fragment (void *cls,
428 struct GNUNET_SERVER_Client *client,
429 const struct GNUNET_MessageHeader *msg)
431 const struct MessageGetFragmentRequest *
432 req = (const struct MessageGetFragmentRequest *) msg;
434 sc = { .op_id = req->op_id, .client = client,
435 .channel_key = req->channel_key, .slave_key = req->slave_key,
436 .membership_test = req->do_membership_test };
438 int ret = db->message_get_fragment (db->cls, &req->channel_key,
439 GNUNET_ntohll (req->message_id),
440 GNUNET_ntohll (req->fragment_offset),
441 &send_fragment, &sc);
448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
449 _("Failed to get message fragment!\n"));
452 send_result_code (client, req->op_id, ret, NULL);
453 GNUNET_SERVER_receive_done (client, GNUNET_OK);
458 handle_counters_get (void *cls,
459 struct GNUNET_SERVER_Client *client,
460 const struct GNUNET_MessageHeader *msg)
462 const struct OperationRequest *req = (const struct OperationRequest *) msg;
463 struct CountersResult res = { {0} };
465 int ret = db->counters_message_get (db->cls, &req->channel_key,
466 &res.max_fragment_id, &res.max_message_id,
467 &res.max_group_generation);
471 ret = db->counters_state_get (db->cls, &req->channel_key,
472 &res.max_state_message_id);
476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
477 _("Failed to get master counters!\n"));
480 res.header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS);
481 res.header.size = htons (sizeof (res));
482 res.result_code = htonl (ret - INT32_MIN);
483 res.op_id = req->op_id;
484 res.max_fragment_id = GNUNET_htonll (res.max_fragment_id);
485 res.max_message_id = GNUNET_htonll (res.max_message_id);
486 res.max_group_generation = GNUNET_htonll (res.max_group_generation);
487 res.max_state_message_id = GNUNET_htonll (res.max_state_message_id);
489 GNUNET_SERVER_notification_context_add (nc, client);
490 GNUNET_SERVER_notification_context_unicast (nc, client, &res.header,
493 GNUNET_SERVER_receive_done (client, GNUNET_OK);
497 struct StateModifyClosure
499 const struct GNUNET_CRYPTO_EddsaPublicKey channel_key;
500 struct GNUNET_PSYC_ReceiveHandle *recv;
501 enum GNUNET_PSYC_MessageState msg_state;
505 uint32_t mod_value_size;
506 uint32_t mod_value_remaining;
511 recv_state_message_part (void *cls, uint64_t message_id, uint64_t data_offset,
512 uint32_t flags, const struct GNUNET_MessageHeader *msg)
514 struct StateModifyClosure *scls = cls;
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "recv_state_message_part() message_id: %" PRIu64
519 ", data_offset: %" PRIu64 ", flags: %u\n",
520 message_id, data_offset, flags);
524 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
528 switch (ntohs (msg->type))
530 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
532 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_METHOD;
536 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
538 struct GNUNET_PSYC_MessageModifier *
539 pmod = (struct GNUNET_PSYC_MessageModifier *) msg;
540 psize = ntohs (pmod->header.size);
541 uint16_t name_size = ntohs (pmod->name_size);
542 uint32_t value_size = ntohl (pmod->value_size);
544 const char *name = (const char *) &pmod[1];
545 const void *value = name + name_size;
547 if (GNUNET_ENV_OP_SET != pmod->oper)
548 { // Apply non-transient operation.
549 if (psize == sizeof (*pmod) + name_size + value_size)
551 db->state_modify_op (db->cls, &scls->channel_key,
552 pmod->oper, name, value, value_size);
556 scls->mod_oper = pmod->oper;
557 scls->mod_name = GNUNET_malloc (name_size);
558 memcpy (scls->mod_name, name, name_size);
560 scls->mod_value_size = value_size;
561 scls->mod_value = GNUNET_malloc (scls->mod_value_size);
562 scls->mod_value_remaining
563 = scls->mod_value_size - (psize - sizeof (*pmod) - name_size);
564 memcpy (scls->mod_value, value, value_size - scls->mod_value_remaining);
567 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
571 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
572 if (GNUNET_ENV_OP_SET != scls->mod_oper)
574 if (scls->mod_value_remaining == 0)
577 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
579 psize = ntohs (msg->size);
580 memcpy (scls->mod_value + (scls->mod_value_size - scls->mod_value_remaining),
581 &msg[1], psize - sizeof (*msg));
582 scls->mod_value_remaining -= psize - sizeof (*msg);
583 if (0 == scls->mod_value_remaining)
585 db->state_modify_op (db->cls, &scls->channel_key,
586 scls->mod_oper, scls->mod_name,
587 scls->mod_value, scls->mod_value_size);
588 GNUNET_free (scls->mod_name);
589 GNUNET_free (scls->mod_value);
591 scls->mod_name = NULL;
592 scls->mod_value = NULL;
593 scls->mod_value_size = 0;
596 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_MOD_CONT;
599 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
600 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_DATA;
603 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
604 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_END;
608 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
614 recv_state_fragment (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg,
615 enum GNUNET_PSYCSTORE_MessageFlags flags)
617 struct StateModifyClosure *scls = cls;
619 if (NULL == scls->recv)
621 scls->recv = GNUNET_PSYC_receive_create (NULL, &recv_state_message_part,
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "recv_state_fragment: %" PRIu64 "\n", GNUNET_ntohll (msg->fragment_id));
628 struct GNUNET_PSYC_MessageHeader *
629 pmsg = GNUNET_PSYC_message_header_create (msg, flags);
630 GNUNET_PSYC_receive_message (scls->recv, pmsg);
638 handle_state_modify (void *cls,
639 struct GNUNET_SERVER_Client *client,
640 const struct GNUNET_MessageHeader *msg)
642 const struct StateModifyRequest *req
643 = (const struct StateModifyRequest *) msg;
645 uint64_t message_id = GNUNET_ntohll (req->message_id);
646 uint64_t state_delta = GNUNET_ntohll (req->state_delta);
647 uint64_t ret_frags = 0;
648 struct StateModifyClosure
649 scls = { .channel_key = req->channel_key };
651 int ret = db->state_modify_begin (db->cls, &req->channel_key,
652 message_id, state_delta);
654 if (GNUNET_OK != ret)
656 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
657 _("Failed to begin modifying state: %d\n"), ret);
661 ret = db->message_get (db->cls, &req->channel_key,
662 message_id, message_id,
663 &ret_frags, &recv_state_fragment, &scls);
664 if (GNUNET_OK != ret)
666 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
667 _("Failed to modify state: %d\n"), ret);
672 if (GNUNET_OK != db->state_modify_end (db->cls, &req->channel_key, message_id))
674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
675 _("Failed to end modifying state!\n"));
679 if (NULL != scls.recv)
681 GNUNET_PSYC_receive_destroy (scls.recv);
685 send_result_code (client, req->op_id, ret, NULL);
686 GNUNET_SERVER_receive_done (client, GNUNET_OK);
690 /** @todo FIXME: stop processing further state sync messages after an error */
692 handle_state_sync (void *cls,
693 struct GNUNET_SERVER_Client *client,
694 const struct GNUNET_MessageHeader *msg)
696 const struct StateSyncRequest *req
697 = (const struct StateSyncRequest *) msg;
699 int ret = GNUNET_SYSERR;
700 const char *name = (const char *) &req[1];
701 uint16_t name_size = ntohs (req->name_size);
703 if (name_size <= 2 || '\0' != name[name_size - 1])
705 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
706 _("Tried to set invalid state variable name!\n"));
713 if (req->flags & STATE_OP_FIRST)
715 ret = db->state_sync_begin (db->cls, &req->channel_key);
717 if (ret != GNUNET_OK)
719 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
720 _("Failed to begin synchronizing state!\n"));
724 ret = db->state_sync_assign (db->cls, &req->channel_key, name,
725 name + ntohs (req->name_size),
726 ntohs (req->header.size) - sizeof (*req)
727 - ntohs (req->name_size));
730 if (GNUNET_OK == ret && req->flags & STATE_OP_LAST)
732 ret = db->state_sync_end (db->cls, &req->channel_key,
733 GNUNET_ntohll (req->max_state_message_id),
734 GNUNET_ntohll (req->state_hash_message_id));
735 if (ret != GNUNET_OK)
736 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
737 _("Failed to end synchronizing state!\n"));
740 send_result_code (client, req->op_id, ret, NULL);
741 GNUNET_SERVER_receive_done (client, GNUNET_OK);
746 handle_state_reset (void *cls,
747 struct GNUNET_SERVER_Client *client,
748 const struct GNUNET_MessageHeader *msg)
750 const struct OperationRequest *req =
751 (const struct OperationRequest *) msg;
753 int ret = db->state_reset (db->cls, &req->channel_key);
755 if (ret != GNUNET_OK)
756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
757 _("Failed to reset state!\n"));
759 send_result_code (client, req->op_id, ret, NULL);
760 GNUNET_SERVER_receive_done (client, GNUNET_OK);
765 handle_state_hash_update (void *cls,
766 struct GNUNET_SERVER_Client *client,
767 const struct GNUNET_MessageHeader *msg)
769 const struct OperationRequest *req =
770 (const struct OperationRequest *) msg;
772 int ret = db->state_reset (db->cls, &req->channel_key);
774 if (ret != GNUNET_OK)
775 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
776 _("Failed to reset state!\n"));
778 send_result_code (client, req->op_id, ret, NULL);
779 GNUNET_SERVER_receive_done (client, GNUNET_OK);
784 handle_state_get (void *cls,
785 struct GNUNET_SERVER_Client *client,
786 const struct GNUNET_MessageHeader *msg)
788 const struct OperationRequest *req =
789 (const struct OperationRequest *) msg;
791 struct SendClosure sc = { .op_id = req->op_id, .client = client };
792 int64_t ret = GNUNET_SYSERR;
793 const char *name = (const char *) &req[1];
794 uint16_t name_size = ntohs (req->header.size) - sizeof (*req);
796 if (name_size <= 2 || '\0' != name[name_size - 1])
798 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
799 _("Tried to get invalid state variable name!\n"));
804 ret = db->state_get (db->cls, &req->channel_key, name,
805 &send_state_var, &sc);
806 if (GNUNET_NO == ret && name_size >= 5) /* min: _a_b\0 */
808 char *p, *n = GNUNET_malloc (name_size);
809 memcpy (n, name, name_size);
810 while (&n[1] < (p = strrchr (n, '_')) && GNUNET_NO == ret)
813 ret = db->state_get (db->cls, &req->channel_key, n,
814 &send_state_var, &sc);
825 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
826 _("Failed to get state variable!\n"));
829 send_result_code (client, req->op_id, ret, NULL);
830 GNUNET_SERVER_receive_done (client, GNUNET_OK);
835 handle_state_get_prefix (void *cls,
836 struct GNUNET_SERVER_Client *client,
837 const struct GNUNET_MessageHeader *msg)
839 const struct OperationRequest *req =
840 (const struct OperationRequest *) msg;
842 struct SendClosure sc = { .op_id = req->op_id, .client = client };
843 int64_t ret = GNUNET_SYSERR;
844 const char *name = (const char *) &req[1];
845 uint16_t name_size = ntohs (req->header.size) - sizeof (*req);
847 if (name_size <= 1 || '\0' != name[name_size - 1])
849 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
850 _("Tried to get invalid state variable name!\n"));
855 ret = db->state_get_prefix (db->cls, &req->channel_key, name,
856 &send_state_var, &sc);
864 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
865 _("Failed to get state variable!\n"));
868 send_result_code (client, req->op_id, ret, NULL);
869 GNUNET_SERVER_receive_done (client, GNUNET_OK);
874 * Initialize the PSYCstore service.
876 * @param cls Closure.
877 * @param server The initialized server.
878 * @param c Configuration to use.
881 run (void *cls, struct GNUNET_SERVER_Handle *server,
882 const struct GNUNET_CONFIGURATION_Handle *c)
884 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
885 { &handle_membership_store, NULL,
886 GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_STORE,
887 sizeof (struct MembershipStoreRequest) },
889 { &handle_membership_test, NULL,
890 GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_TEST,
891 sizeof (struct MembershipTestRequest) },
893 { &handle_fragment_store, NULL,
894 GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_STORE, 0, },
896 { &handle_fragment_get, NULL,
897 GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET,
898 sizeof (struct FragmentGetRequest) },
900 { &handle_message_get, NULL,
901 GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET, 0 },
903 { &handle_message_get_fragment, NULL,
904 GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET_FRAGMENT,
905 sizeof (struct MessageGetFragmentRequest) },
907 { &handle_counters_get, NULL,
908 GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET,
909 sizeof (struct OperationRequest) },
911 { &handle_state_modify, NULL,
912 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_MODIFY, 0 },
914 { &handle_state_sync, NULL,
915 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC, 0 },
917 { &handle_state_reset, NULL,
918 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_RESET,
919 sizeof (struct OperationRequest) },
921 { &handle_state_hash_update, NULL,
922 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_HASH_UPDATE,
923 sizeof (struct StateHashUpdateRequest) },
925 { &handle_state_get, NULL,
926 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET, 0 },
928 { &handle_state_get_prefix, NULL,
929 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET_PREFIX, 0 },
936 /* Loading database plugin */
939 GNUNET_CONFIGURATION_get_value_string (cfg, "psycstore", "database",
942 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
946 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_psycstore_%s", database);
947 db = GNUNET_PLUGIN_load (db_lib_name, (void *) cfg);
948 GNUNET_free (database);
952 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
953 "Could not load database backend `%s'\n",
955 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
959 stats = GNUNET_STATISTICS_create ("psycstore", cfg);
960 GNUNET_SERVER_add_handlers (server, handlers);
961 nc = GNUNET_SERVER_notification_context_create (server, 1);
962 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
968 * The main function for the service.
970 * @param argc number of arguments from the command line
971 * @param argv command line arguments
972 * @return 0 ok, 1 on error
975 main (int argc, char *const *argv)
978 GNUNET_SERVICE_run (argc, argv, "psycstore",
979 GNUNET_SERVICE_OPTION_NONE,
980 &run, NULL)) ? 0 : 1;
984 /* end of gnunet-service-psycstore.c */