2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
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.
73 shutdown_task (void *cls)
77 GNUNET_SERVER_notification_context_destroy (nc);
82 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
85 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
86 GNUNET_free (db_lib_name);
92 * Send a result code back to the client.
95 * Client that should receive the result code.
99 * Operation ID in network byte order.
101 * Error message to include (or NULL for none).
104 send_result_code (struct GNUNET_SERVER_Client *client, uint64_t op_id,
105 int64_t result_code, const char *err_msg)
107 struct OperationResult *res;
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);
120 memcpy (&res[1], err_msg, err_size);
121 ((char *) &res[1])[err_size - 1] = '\0';
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,
135 MEMBERSHIP_TEST_NOT_NEEDED = 0,
136 MEMBERSHIP_TEST_NEEDED = 1,
137 MEMBERSHIP_TEST_DONE = 2,
138 } MessageMembershipTest;
143 struct GNUNET_SERVER_Client *client;
146 * Channel's public key.
148 struct GNUNET_CRYPTO_EddsaPublicKey channel_key;
151 * Slave's public key.
153 struct GNUNET_CRYPTO_EcdsaPublicKey slave_key;
161 * Membership test result.
163 int membership_test_result;
166 * Do membership test with @a slave_key before returning fragment?
167 * @see enum MessageMembershipTest
169 uint8_t membership_test;
174 send_fragment (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg,
175 enum GNUNET_PSYCSTORE_MessageFlags flags)
177 struct SendClosure *sc = cls;
178 struct FragmentResult *res;
180 if (MEMBERSHIP_TEST_NEEDED == sc->membership_test)
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)
197 size_t msg_size = ntohs (msg->header.size);
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));
209 GNUNET_SERVER_notification_context_add (nc, sc->client);
210 GNUNET_SERVER_notification_context_unicast (nc, sc->client, &res->header,
218 send_state_var (void *cls, const char *name,
219 const void *value, uint32_t value_size)
221 struct SendClosure *sc = cls;
222 struct StateResult *res;
223 size_t name_size = strlen (name) + 1;
225 /** @todo FIXME: split up value into 64k chunks */
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,
245 handle_membership_store (void *cls,
246 struct GNUNET_SERVER_Client *client,
247 const struct GNUNET_MessageHeader *msg)
249 const struct MembershipStoreRequest *req =
250 (const struct MembershipStoreRequest *) msg;
252 int ret = db->membership_store (db->cls, &req->channel_key, &req->slave_key,
254 GNUNET_ntohll (req->announced_at),
255 GNUNET_ntohll (req->effective_since),
256 GNUNET_ntohll (req->group_generation));
258 if (ret != GNUNET_OK)
259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260 _("Failed to store membership information!\n"));
262 send_result_code (client, req->op_id, ret, NULL);
263 GNUNET_SERVER_receive_done (client, GNUNET_OK);
268 handle_membership_test (void *cls,
269 struct GNUNET_SERVER_Client *client,
270 const struct GNUNET_MessageHeader *msg)
272 const struct MembershipTestRequest *req =
273 (const struct MembershipTestRequest *) msg;
275 int ret = db->membership_test (db->cls, &req->channel_key, &req->slave_key,
276 GNUNET_ntohll (req->message_id));
283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
284 _("Failed to test membership!\n"));
287 send_result_code (client, req->op_id, ret, NULL);
288 GNUNET_SERVER_receive_done (client, GNUNET_OK);
293 handle_fragment_store (void *cls,
294 struct GNUNET_SERVER_Client *client,
295 const struct GNUNET_MessageHeader *msg)
297 const struct FragmentStoreRequest *req =
298 (const struct FragmentStoreRequest *) msg;
300 int ret = db->fragment_store (db->cls, &req->channel_key,
301 (const struct GNUNET_MULTICAST_MessageHeader *)
302 &req[1], ntohl (req->psycstore_flags));
304 if (ret != GNUNET_OK)
305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
306 _("Failed to store fragment!\n"));
308 send_result_code (client, req->op_id, ret, NULL);
309 GNUNET_SERVER_receive_done (client, GNUNET_OK);
314 handle_fragment_get (void *cls,
315 struct GNUNET_SERVER_Client *client,
316 const struct GNUNET_MessageHeader *msg)
318 const struct FragmentGetRequest *
319 req = (const struct FragmentGetRequest *) msg;
321 sc = { .op_id = req->op_id,
323 .channel_key = req->channel_key,
324 .slave_key = req->slave_key,
325 .membership_test = req->do_membership_test };
328 uint64_t ret_frags = 0;
329 uint64_t first_fragment_id = GNUNET_ntohll (req->first_fragment_id);
330 uint64_t last_fragment_id = GNUNET_ntohll (req->last_fragment_id);
331 uint64_t limit = GNUNET_ntohll (req->fragment_limit);
334 ret = db->fragment_get (db->cls, &req->channel_key,
335 first_fragment_id, last_fragment_id,
336 &ret_frags, send_fragment, &sc);
338 ret = db->fragment_get_latest (db->cls, &req->channel_key, limit,
339 &ret_frags, send_fragment, &sc);
345 if (MEMBERSHIP_TEST_DONE == sc.membership_test)
347 switch (sc.membership_test_result)
353 ret = GNUNET_PSYCSTORE_MEMBERSHIP_TEST_FAILED;
363 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
364 _("Failed to get fragment!\n"));
366 send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
367 GNUNET_SERVER_receive_done (client, GNUNET_OK);
372 handle_message_get (void *cls,
373 struct GNUNET_SERVER_Client *client,
374 const struct GNUNET_MessageHeader *msg)
376 const struct MessageGetRequest *
377 req = (const struct MessageGetRequest *) msg;
378 uint16_t size = ntohs (msg->size);
379 const char *method_prefix = (const char *) &req[1];
381 if (size < sizeof (*req) + 1
382 || '\0' != method_prefix[size - sizeof (*req) - 1])
384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
385 "Message get: invalid method prefix. size: %u < %u?\n",
386 size, sizeof (*req) + 1);
388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
393 sc = { .op_id = req->op_id,
395 .channel_key = req->channel_key,
396 .slave_key = req->slave_key,
397 .membership_test = req->do_membership_test };
400 uint64_t ret_frags = 0;
401 uint64_t first_message_id = GNUNET_ntohll (req->first_message_id);
402 uint64_t last_message_id = GNUNET_ntohll (req->last_message_id);
403 uint64_t msg_limit = GNUNET_ntohll (req->message_limit);
404 uint64_t frag_limit = GNUNET_ntohll (req->fragment_limit);
406 /** @todo method_prefix */
408 ret = db->message_get (db->cls, &req->channel_key,
409 first_message_id, last_message_id, frag_limit,
410 &ret_frags, send_fragment, &sc);
412 ret = db->message_get_latest (db->cls, &req->channel_key, msg_limit,
413 &ret_frags, send_fragment, &sc);
421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
422 _("Failed to get message!\n"));
425 send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
426 GNUNET_SERVER_receive_done (client, GNUNET_OK);
431 handle_message_get_fragment (void *cls,
432 struct GNUNET_SERVER_Client *client,
433 const struct GNUNET_MessageHeader *msg)
435 const struct MessageGetFragmentRequest *
436 req = (const struct MessageGetFragmentRequest *) msg;
438 sc = { .op_id = req->op_id, .client = client,
439 .channel_key = req->channel_key, .slave_key = req->slave_key,
440 .membership_test = req->do_membership_test };
442 int ret = db->message_get_fragment (db->cls, &req->channel_key,
443 GNUNET_ntohll (req->message_id),
444 GNUNET_ntohll (req->fragment_offset),
445 &send_fragment, &sc);
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
453 _("Failed to get message fragment!\n"));
456 send_result_code (client, req->op_id, ret, NULL);
457 GNUNET_SERVER_receive_done (client, GNUNET_OK);
462 handle_counters_get (void *cls,
463 struct GNUNET_SERVER_Client *client,
464 const struct GNUNET_MessageHeader *msg)
466 const struct OperationRequest *req = (const struct OperationRequest *) msg;
467 struct CountersResult res = { {0} };
469 int ret = db->counters_message_get (db->cls, &req->channel_key,
470 &res.max_fragment_id, &res.max_message_id,
471 &res.max_group_generation);
475 ret = db->counters_state_get (db->cls, &req->channel_key,
476 &res.max_state_message_id);
480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
481 _("Failed to get master counters!\n"));
484 res.header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS);
485 res.header.size = htons (sizeof (res));
486 res.result_code = htonl (ret);
487 res.op_id = req->op_id;
488 res.max_fragment_id = GNUNET_htonll (res.max_fragment_id);
489 res.max_message_id = GNUNET_htonll (res.max_message_id);
490 res.max_group_generation = GNUNET_htonll (res.max_group_generation);
491 res.max_state_message_id = GNUNET_htonll (res.max_state_message_id);
493 GNUNET_SERVER_notification_context_add (nc, client);
494 GNUNET_SERVER_notification_context_unicast (nc, client, &res.header,
497 GNUNET_SERVER_receive_done (client, GNUNET_OK);
501 struct StateModifyClosure
503 const struct GNUNET_CRYPTO_EddsaPublicKey channel_key;
504 struct GNUNET_PSYC_ReceiveHandle *recv;
505 enum GNUNET_PSYC_MessageState msg_state;
509 uint32_t mod_value_size;
510 uint32_t mod_value_remaining;
515 recv_state_message_part (void *cls,
516 const struct GNUNET_PSYC_MessageHeader *msg,
517 const struct GNUNET_MessageHeader *pmsg)
519 struct StateModifyClosure *scls = cls;
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "recv_state_message_part() message_id: %" PRIu64
524 ", fragment_offset: %" PRIu64 ", flags: %u\n",
525 GNUNET_ntohll (msg->message_id),
526 GNUNET_ntohll (msg->fragment_offset),
531 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
535 switch (ntohs (pmsg->type))
537 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
539 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_METHOD;
543 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
545 struct GNUNET_PSYC_MessageModifier *
546 pmod = (struct GNUNET_PSYC_MessageModifier *) pmsg;
547 psize = ntohs (pmod->header.size);
548 uint16_t name_size = ntohs (pmod->name_size);
549 uint32_t value_size = ntohl (pmod->value_size);
551 const char *name = (const char *) &pmod[1];
552 const void *value = name + name_size;
554 if (GNUNET_PSYC_OP_SET != pmod->oper)
555 { // Apply non-transient operation.
556 if (psize == sizeof (*pmod) + name_size + value_size)
558 db->state_modify_op (db->cls, &scls->channel_key,
559 pmod->oper, name, value, value_size);
563 scls->mod_oper = pmod->oper;
564 scls->mod_name = GNUNET_malloc (name_size);
565 memcpy (scls->mod_name, name, name_size);
567 scls->mod_value_size = value_size;
568 scls->mod_value = GNUNET_malloc (scls->mod_value_size);
569 scls->mod_value_remaining
570 = scls->mod_value_size - (psize - sizeof (*pmod) - name_size);
571 memcpy (scls->mod_value, value, value_size - scls->mod_value_remaining);
574 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
578 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
579 if (GNUNET_PSYC_OP_SET != scls->mod_oper)
581 if (scls->mod_value_remaining == 0)
584 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
586 psize = ntohs (pmsg->size);
587 memcpy (scls->mod_value + (scls->mod_value_size - scls->mod_value_remaining),
588 &pmsg[1], psize - sizeof (*pmsg));
589 scls->mod_value_remaining -= psize - sizeof (*pmsg);
590 if (0 == scls->mod_value_remaining)
592 db->state_modify_op (db->cls, &scls->channel_key,
593 scls->mod_oper, scls->mod_name,
594 scls->mod_value, scls->mod_value_size);
595 GNUNET_free (scls->mod_name);
596 GNUNET_free (scls->mod_value);
598 scls->mod_name = NULL;
599 scls->mod_value = NULL;
600 scls->mod_value_size = 0;
603 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_MOD_CONT;
606 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
607 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_DATA;
610 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
611 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_END;
615 scls->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
621 recv_state_fragment (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg,
622 enum GNUNET_PSYCSTORE_MessageFlags flags)
624 struct StateModifyClosure *scls = cls;
626 if (NULL == scls->recv)
628 scls->recv = GNUNET_PSYC_receive_create (NULL, recv_state_message_part,
632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633 "recv_state_fragment: %" PRIu64 "\n", GNUNET_ntohll (msg->fragment_id));
635 struct GNUNET_PSYC_MessageHeader *
636 pmsg = GNUNET_PSYC_message_header_create (msg, flags);
637 GNUNET_PSYC_receive_message (scls->recv, pmsg);
645 handle_state_modify (void *cls,
646 struct GNUNET_SERVER_Client *client,
647 const struct GNUNET_MessageHeader *msg)
649 const struct StateModifyRequest *req
650 = (const struct StateModifyRequest *) msg;
652 uint64_t message_id = GNUNET_ntohll (req->message_id);
653 uint64_t state_delta = GNUNET_ntohll (req->state_delta);
654 uint64_t ret_frags = 0;
655 struct StateModifyClosure
656 scls = { .channel_key = req->channel_key };
658 int ret = db->state_modify_begin (db->cls, &req->channel_key,
659 message_id, state_delta);
661 if (GNUNET_OK != ret)
663 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
664 _("Failed to begin modifying state: %d\n"), ret);
668 ret = db->message_get (db->cls, &req->channel_key,
669 message_id, message_id, 0,
670 &ret_frags, recv_state_fragment, &scls);
671 if (GNUNET_OK != ret)
673 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
674 _("Failed to modify state: %d\n"), ret);
679 if (GNUNET_OK != db->state_modify_end (db->cls, &req->channel_key, message_id))
681 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
682 _("Failed to end modifying state!\n"));
686 if (NULL != scls.recv)
688 GNUNET_PSYC_receive_destroy (scls.recv);
692 send_result_code (client, req->op_id, ret, NULL);
693 GNUNET_SERVER_receive_done (client, GNUNET_OK);
697 /** @todo FIXME: stop processing further state sync messages after an error */
699 handle_state_sync (void *cls,
700 struct GNUNET_SERVER_Client *client,
701 const struct GNUNET_MessageHeader *msg)
703 const struct StateSyncRequest *req
704 = (const struct StateSyncRequest *) msg;
706 int ret = GNUNET_SYSERR;
707 const char *name = (const char *) &req[1];
708 uint16_t name_size = ntohs (req->name_size);
710 if (name_size <= 2 || '\0' != name[name_size - 1])
712 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
713 _("Tried to set invalid state variable name!\n"));
720 if (req->flags & STATE_OP_FIRST)
722 ret = db->state_sync_begin (db->cls, &req->channel_key);
724 if (ret != GNUNET_OK)
726 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
727 _("Failed to begin synchronizing state!\n"));
731 ret = db->state_sync_assign (db->cls, &req->channel_key, name,
732 name + ntohs (req->name_size),
733 ntohs (req->header.size) - sizeof (*req)
734 - ntohs (req->name_size));
737 if (GNUNET_OK == ret && req->flags & STATE_OP_LAST)
739 ret = db->state_sync_end (db->cls, &req->channel_key,
740 GNUNET_ntohll (req->max_state_message_id),
741 GNUNET_ntohll (req->state_hash_message_id));
742 if (ret != GNUNET_OK)
743 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
744 _("Failed to end synchronizing state!\n"));
747 send_result_code (client, req->op_id, ret, NULL);
748 GNUNET_SERVER_receive_done (client, GNUNET_OK);
753 handle_state_reset (void *cls,
754 struct GNUNET_SERVER_Client *client,
755 const struct GNUNET_MessageHeader *msg)
757 const struct OperationRequest *req =
758 (const struct OperationRequest *) msg;
760 int ret = db->state_reset (db->cls, &req->channel_key);
762 if (ret != GNUNET_OK)
763 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
764 _("Failed to reset state!\n"));
766 send_result_code (client, req->op_id, ret, NULL);
767 GNUNET_SERVER_receive_done (client, GNUNET_OK);
772 handle_state_hash_update (void *cls,
773 struct GNUNET_SERVER_Client *client,
774 const struct GNUNET_MessageHeader *msg)
776 const struct OperationRequest *req =
777 (const struct OperationRequest *) msg;
779 int ret = db->state_reset (db->cls, &req->channel_key);
781 if (ret != GNUNET_OK)
782 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
783 _("Failed to reset state!\n"));
785 send_result_code (client, req->op_id, ret, NULL);
786 GNUNET_SERVER_receive_done (client, GNUNET_OK);
791 handle_state_get (void *cls,
792 struct GNUNET_SERVER_Client *client,
793 const struct GNUNET_MessageHeader *msg)
795 const struct OperationRequest *req =
796 (const struct OperationRequest *) msg;
798 struct SendClosure sc = { .op_id = req->op_id, .client = client };
799 int64_t ret = GNUNET_SYSERR;
800 const char *name = (const char *) &req[1];
801 uint16_t name_size = ntohs (req->header.size) - sizeof (*req);
803 if (name_size <= 2 || '\0' != name[name_size - 1])
805 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
806 _("Tried to get invalid state variable name!\n"));
811 ret = db->state_get (db->cls, &req->channel_key, name,
812 &send_state_var, &sc);
813 if (GNUNET_NO == ret && name_size >= 5) /* min: _a_b\0 */
815 char *p, *n = GNUNET_malloc (name_size);
816 memcpy (n, name, name_size);
817 while (&n[1] < (p = strrchr (n, '_')) && GNUNET_NO == ret)
820 ret = db->state_get (db->cls, &req->channel_key, n,
821 &send_state_var, &sc);
832 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
833 _("Failed to get state variable!\n"));
836 send_result_code (client, req->op_id, ret, NULL);
837 GNUNET_SERVER_receive_done (client, GNUNET_OK);
842 handle_state_get_prefix (void *cls,
843 struct GNUNET_SERVER_Client *client,
844 const struct GNUNET_MessageHeader *msg)
846 const struct OperationRequest *req =
847 (const struct OperationRequest *) msg;
849 struct SendClosure sc = { .op_id = req->op_id, .client = client };
850 int64_t ret = GNUNET_SYSERR;
851 const char *name = (const char *) &req[1];
852 uint16_t name_size = ntohs (req->header.size) - sizeof (*req);
854 if (name_size <= 1 || '\0' != name[name_size - 1])
856 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
857 _("Tried to get invalid state variable name!\n"));
862 ret = db->state_get_prefix (db->cls, &req->channel_key, name,
863 &send_state_var, &sc);
871 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
872 _("Failed to get state variable!\n"));
875 send_result_code (client, req->op_id, ret, NULL);
876 GNUNET_SERVER_receive_done (client, GNUNET_OK);
881 * Initialize the PSYCstore service.
883 * @param cls Closure.
884 * @param server The initialized server.
885 * @param c Configuration to use.
888 run (void *cls, struct GNUNET_SERVER_Handle *server,
889 const struct GNUNET_CONFIGURATION_Handle *c)
891 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
892 { &handle_membership_store, NULL,
893 GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_STORE,
894 sizeof (struct MembershipStoreRequest) },
896 { &handle_membership_test, NULL,
897 GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_TEST,
898 sizeof (struct MembershipTestRequest) },
900 { &handle_fragment_store, NULL,
901 GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_STORE, 0, },
903 { &handle_fragment_get, NULL,
904 GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET,
905 sizeof (struct FragmentGetRequest) },
907 { &handle_message_get, NULL,
908 GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET, 0 },
910 { &handle_message_get_fragment, NULL,
911 GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET_FRAGMENT,
912 sizeof (struct MessageGetFragmentRequest) },
914 { &handle_counters_get, NULL,
915 GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET,
916 sizeof (struct OperationRequest) },
918 { &handle_state_modify, NULL,
919 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_MODIFY, 0 },
921 { &handle_state_sync, NULL,
922 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC, 0 },
924 { &handle_state_reset, NULL,
925 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_RESET,
926 sizeof (struct OperationRequest) },
928 { &handle_state_hash_update, NULL,
929 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_HASH_UPDATE,
930 sizeof (struct StateHashUpdateRequest) },
932 { &handle_state_get, NULL,
933 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET, 0 },
935 { &handle_state_get_prefix, NULL,
936 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET_PREFIX, 0 },
943 /* Loading database plugin */
946 GNUNET_CONFIGURATION_get_value_string (cfg, "psycstore", "database",
949 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
955 GNUNET_asprintf (&db_lib_name,
956 "libgnunet_plugin_psycstore_%s",
958 db = GNUNET_PLUGIN_load (db_lib_name, (void *) cfg);
959 GNUNET_free (database);
963 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
964 "Could not load database backend `%s'\n",
966 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
970 stats = GNUNET_STATISTICS_create ("psycstore", cfg);
971 GNUNET_SERVER_add_handlers (server, handlers);
972 nc = GNUNET_SERVER_notification_context_create (server, 1);
973 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
979 * The main function for the service.
981 * @param argc number of arguments from the command line
982 * @param argv command line arguments
983 * @return 0 ok, 1 on error
986 main (int argc, char *const *argv)
989 GNUNET_SERVICE_run (argc, argv, "psycstore",
990 GNUNET_SERVICE_OPTION_NONE,
991 &run, NULL)) ? 0 : 1;
995 /* end of gnunet-service-psycstore.c */