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/psycstore_api.c
23 * @brief API to interact with the 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_psycstore_service.h"
35 #include "gnunet_multicast_service.h"
36 #include "psycstore.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "psycstore-api",__VA_ARGS__)
41 * Handle for an operation with the PSYCstore service.
43 struct GNUNET_PSYCSTORE_OperationHandle
47 * Main PSYCstore handle.
49 struct GNUNET_PSYCSTORE_Handle *h;
55 GNUNET_PSYCSTORE_FragmentCallback fragment_cb;
56 GNUNET_PSYCSTORE_CountersCallback counters_cb;
57 GNUNET_PSYCSTORE_StateCallback state_cb;
61 * Closure for callbacks.
68 struct GNUNET_MQ_Envelope *env;
78 * Handle for the service.
80 struct GNUNET_PSYCSTORE_Handle
83 * Configuration to use.
85 const struct GNUNET_CONFIGURATION_Handle *cfg;
90 struct GNUNET_MQ_Handle *mq;
95 struct GNUNET_OP_Handle *op;
98 * Task doing exponential back-off trying to reconnect.
100 struct GNUNET_SCHEDULER_Task *reconnect_task;
103 * Delay for next connect retry.
105 struct GNUNET_TIME_Relative reconnect_delay;
108 GNUNET_PSYCSTORE_FragmentCallback *fragment_cb;
110 GNUNET_PSYCSTORE_CountersCallback *counters_cb;
112 GNUNET_PSYCSTORE_StateCallback *state_cb;
114 * Closure for callbacks.
121 check_result_code (void *cls, const struct OperationResult *opres)
123 uint16_t size = ntohs (opres->header.size);
124 const char *str = (const char *) &opres[1];
125 if ( (sizeof (*opres) < size) &&
126 ('\0' != str[size - sizeof (*opres) - 1]) )
129 return GNUNET_SYSERR;
137 handle_result_code (void *cls, const struct OperationResult *opres)
139 struct GNUNET_PSYCSTORE_Handle *h = cls;
140 struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
141 uint16_t size = ntohs (opres->header.size);
144 str = (sizeof (*opres) < size) ? (const char *) &opres[1] : "";
146 if (GNUNET_YES == GNUNET_OP_result (h->op, GNUNET_ntohll (opres->op_id),
147 GNUNET_ntohll (opres->result_code) + INT64_MIN,
148 str, size - sizeof (*opres), (void **) &op))
150 LOG (GNUNET_ERROR_TYPE_DEBUG,
151 "handle_result_code: Received result message with OP ID: %" PRIu64 "\n",
152 GNUNET_ntohll (opres->op_id));
157 LOG (GNUNET_ERROR_TYPE_DEBUG,
158 "handle_result_code: No callback registered for OP ID %" PRIu64 ".\n",
159 GNUNET_ntohll (opres->op_id));
161 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
166 handle_result_counters (void *cls, const struct CountersResult *cres)
168 struct GNUNET_PSYCSTORE_Handle *h = cls;
169 struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
171 if (GNUNET_YES == GNUNET_OP_get (h->op, GNUNET_ntohll (cres->op_id),
172 NULL, NULL, (void **) &op))
174 GNUNET_assert (NULL != op);
175 if (NULL != op->counters_cb)
177 op->counters_cb (op->cls,
178 ntohl (cres->result_code),
179 GNUNET_ntohll (cres->max_fragment_id),
180 GNUNET_ntohll (cres->max_message_id),
181 GNUNET_ntohll (cres->max_group_generation),
182 GNUNET_ntohll (cres->max_state_message_id));
184 GNUNET_OP_remove (h->op, GNUNET_ntohll (cres->op_id));
189 LOG (GNUNET_ERROR_TYPE_DEBUG,
190 "handle_result_counters: No callback registered for OP ID %" PRIu64 ".\n",
191 GNUNET_ntohll (cres->op_id));
193 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
198 check_result_fragment (void *cls, const struct FragmentResult *fres)
200 uint16_t size = ntohs (fres->header.size);
201 struct GNUNET_MULTICAST_MessageHeader *mmsg =
202 (struct GNUNET_MULTICAST_MessageHeader *) &fres[1];
203 if (sizeof (*fres) + sizeof (*mmsg) < size
204 && sizeof (*fres) + ntohs (mmsg->header.size) != size)
206 LOG (GNUNET_ERROR_TYPE_ERROR,
207 "check_result_fragment: Received message with invalid length %lu bytes.\n",
208 size, sizeof (*fres));
210 return GNUNET_SYSERR;
217 handle_result_fragment (void *cls, const struct FragmentResult *fres)
219 struct GNUNET_PSYCSTORE_Handle *h = cls;
220 struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
222 if (GNUNET_YES == GNUNET_OP_get (h->op, GNUNET_ntohll (fres->op_id),
223 NULL, NULL, (void **) &op))
225 GNUNET_assert (NULL != op);
226 if (NULL != op->fragment_cb)
227 op->fragment_cb (op->cls,
228 (struct GNUNET_MULTICAST_MessageHeader *) &fres[1],
229 ntohl (fres->psycstore_flags));
230 //GNUNET_OP_remove (h->op, GNUNET_ntohll (fres->op_id));
235 LOG (GNUNET_ERROR_TYPE_DEBUG,
236 "handle_result_fragment: No callback registered for OP ID %" PRIu64 ".\n",
237 GNUNET_ntohll (fres->op_id));
239 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
244 check_result_state (void *cls, const struct StateResult *sres)
246 const char *name = (const char *) &sres[1];
247 uint16_t size = ntohs (sres->header.size);
248 uint16_t name_size = ntohs (sres->name_size);
251 || size - sizeof (*sres) < name_size
252 || '\0' != name[name_size - 1])
254 LOG (GNUNET_ERROR_TYPE_ERROR,
255 "check_result_state: Received state result message with invalid name.\n");
257 return GNUNET_SYSERR;
264 handle_result_state (void *cls, const struct StateResult *sres)
266 struct GNUNET_PSYCSTORE_Handle *h = cls;
267 struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
269 const char *name = (const char *) &sres[1];
270 uint16_t name_size = ntohs (sres->name_size);
272 if (GNUNET_YES == GNUNET_OP_get (h->op, GNUNET_ntohll (sres->op_id),
273 NULL, NULL, (void **) &op))
275 GNUNET_assert (NULL != op);
276 if (NULL != op->state_cb)
277 op->state_cb (op->cls, name, (char *) &sres[1] + name_size,
278 ntohs (sres->header.size) - sizeof (*sres) - name_size);
279 //GNUNET_OP_remove (h->op, GNUNET_ntohll (sres->op_id));
284 LOG (GNUNET_ERROR_TYPE_DEBUG,
285 "handle_result_state: No callback registered for OP ID %" PRIu64 ".\n",
286 GNUNET_ntohll (sres->op_id));
288 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
293 reconnect (void *cls);
297 * Client disconnected from service.
299 * Reconnect after backoff period.=
302 disconnected (void *cls, enum GNUNET_MQ_Error error)
304 struct GNUNET_PSYCSTORE_Handle *h = cls;
306 LOG (GNUNET_ERROR_TYPE_DEBUG,
307 "Origin client disconnected (%d), re-connecting\n",
311 GNUNET_MQ_destroy (h->mq);
312 GNUNET_OP_destroy (h->op);
317 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
319 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
324 do_connect (struct GNUNET_PSYCSTORE_Handle *h)
326 LOG (GNUNET_ERROR_TYPE_DEBUG,
327 "Connecting to PSYCstore service.\n");
329 struct GNUNET_MQ_MessageHandler handlers[] = {
330 GNUNET_MQ_hd_var_size (result_code,
331 GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE,
332 struct OperationResult,
334 GNUNET_MQ_hd_fixed_size (result_counters,
335 GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS,
336 struct CountersResult,
338 GNUNET_MQ_hd_var_size (result_fragment,
339 GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_FRAGMENT,
340 struct FragmentResult,
342 GNUNET_MQ_hd_var_size (result_state,
343 GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_STATE,
346 GNUNET_MQ_handler_end ()
349 h->op = GNUNET_OP_create ();
350 GNUNET_assert (NULL == h->mq);
351 h->mq = GNUNET_CLIENT_connect (h->cfg, "psycstore",
352 handlers, disconnected, h);
353 GNUNET_assert (NULL != h->mq);
358 * Try again to connect to the PSYCstore service.
360 * @param cls Handle to the PSYCstore service.
363 reconnect (void *cls)
365 struct GNUNET_PSYCSTORE_Handle *h = cls;
367 h->reconnect_task = NULL;
373 * Connect to the PSYCstore service.
375 * @param cfg The configuration to use
376 * @return Handle to use
378 struct GNUNET_PSYCSTORE_Handle *
379 GNUNET_PSYCSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
381 struct GNUNET_PSYCSTORE_Handle *h
382 = GNUNET_new (struct GNUNET_PSYCSTORE_Handle);
384 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
391 * Disconnect from PSYCstore service
393 * @param h Handle to destroy
396 GNUNET_PSYCSTORE_disconnect (struct GNUNET_PSYCSTORE_Handle *h)
398 GNUNET_assert (NULL != h);
399 if (h->reconnect_task != NULL)
401 GNUNET_SCHEDULER_cancel (h->reconnect_task);
402 h->reconnect_task = NULL;
406 // FIXME: free data structures for pending operations
407 GNUNET_MQ_destroy (h->mq);
415 * Message sent notification.
417 * Remove invalidated envelope pointer.
420 message_sent (void *cls)
422 struct GNUNET_PSYCSTORE_OperationHandle *op = cls;
428 * Create a new operation.
430 static struct GNUNET_PSYCSTORE_OperationHandle *
431 op_create (struct GNUNET_PSYCSTORE_Handle *h,
432 struct GNUNET_OP_Handle *hop,
433 GNUNET_PSYCSTORE_ResultCallback result_cb,
436 struct GNUNET_PSYCSTORE_OperationHandle *
437 op = GNUNET_malloc (sizeof (*op));
439 op->op_id = GNUNET_OP_add (hop,
440 (GNUNET_ResultCallback) result_cb,
447 * Send a message associated with an operation.
454 * Message envelope to send.
456 * Operation ID to write in network byte order. NULL if not needed.
458 * @return Operation handle.
461 static struct GNUNET_PSYCSTORE_OperationHandle *
462 op_send (struct GNUNET_PSYCSTORE_Handle *h,
463 struct GNUNET_PSYCSTORE_OperationHandle *op,
464 struct GNUNET_MQ_Envelope *env,
469 *op_id = GNUNET_htonll (op->op_id);
471 GNUNET_MQ_notify_sent (env, message_sent, op);
472 GNUNET_MQ_send (h->mq, env);
478 * Cancel a PSYCstore operation. Note that the operation MAY still
479 * be executed; this merely cancels the continuation; if the request
480 * was already transmitted, the service may still choose to complete
483 * @param op Operation to cancel.
485 * @return #GNUNET_YES if message was not sent yet and got discarded,
486 * #GNUNET_NO if it was already sent, and only the callbacks got cancelled.
489 GNUNET_PSYCSTORE_operation_cancel (struct GNUNET_PSYCSTORE_OperationHandle *op)
491 struct GNUNET_PSYCSTORE_Handle *h = op->h;
496 GNUNET_MQ_send_cancel (op->env);
500 GNUNET_OP_remove (h->op, op->op_id);
508 * Store join/leave events for a PSYC channel in order to be able to answer
509 * membership test queries later.
512 * Handle for the PSYCstore.
514 * The channel where the event happened.
516 * Public key of joining/leaving slave.
518 * #GNUNET_YES on join, #GNUNET_NO on part.
519 * @param announced_at
520 * ID of the message that announced the membership change.
521 * @param effective_since
522 * Message ID this membership change is in effect since.
523 * For joins it is <= announced_at, for parts it is always 0.
524 * @param group_generation
525 * In case of a part, the last group generation the slave has access to.
526 * It has relevance when a larger message have fragments with different
529 * Callback to call with the result of the storage operation.
531 * Closure for the callback.
533 * @return Operation handle that can be used to cancel the operation.
535 struct GNUNET_PSYCSTORE_OperationHandle *
536 GNUNET_PSYCSTORE_membership_store (struct GNUNET_PSYCSTORE_Handle *h,
537 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
538 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
540 uint64_t announced_at,
541 uint64_t effective_since,
542 uint64_t group_generation,
543 GNUNET_PSYCSTORE_ResultCallback result_cb,
546 GNUNET_assert (NULL != h);
547 GNUNET_assert (NULL != channel_key);
548 GNUNET_assert (NULL != slave_key);
549 GNUNET_assert (GNUNET_YES == did_join || GNUNET_NO == did_join);
550 GNUNET_assert (did_join
551 ? effective_since <= announced_at
552 : effective_since == 0);
554 struct MembershipStoreRequest *req;
555 struct GNUNET_MQ_Envelope *
556 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_STORE);
557 req->channel_key = *channel_key;
558 req->slave_key = *slave_key;
559 req->did_join = did_join;
560 req->announced_at = GNUNET_htonll (announced_at);
561 req->effective_since = GNUNET_htonll (effective_since);
562 req->group_generation = GNUNET_htonll (group_generation);
565 op_send (h, op_create (h, h->op, result_cb, cls),
571 * Test if a member was admitted to the channel at the given message ID.
573 * This is useful when relaying and replaying messages to check if a particular
574 * slave has access to the message fragment with a given group generation. It
575 * is also used when handling join requests to determine whether the slave is
576 * currently admitted to the channel.
579 * Handle for the PSYCstore.
581 * The channel we are interested in.
583 * Public key of slave whose membership to check.
585 * Message ID for which to do the membership test.
586 * @param group_generation
587 * Group generation of the fragment of the message to test.
588 * It has relevance if the message consists of multiple fragments with
589 * different group generations.
591 * Callback to call with the test result.
593 * Closure for the callback.
595 * @return Operation handle that can be used to cancel the operation.
597 struct GNUNET_PSYCSTORE_OperationHandle *
598 GNUNET_PSYCSTORE_membership_test (struct GNUNET_PSYCSTORE_Handle *h,
599 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
600 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
602 uint64_t group_generation,
603 GNUNET_PSYCSTORE_ResultCallback result_cb,
606 struct MembershipTestRequest *req;
607 struct GNUNET_MQ_Envelope *
608 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_TEST);
609 req->channel_key = *channel_key;
610 req->slave_key = *slave_key;
611 req->message_id = GNUNET_htonll (message_id);
612 req->group_generation = GNUNET_htonll (group_generation);
615 op_send (h, op_create (h, h->op, result_cb, cls),
621 * Store a message fragment sent to a channel.
623 * @param h Handle for the PSYCstore.
624 * @param channel_key The channel the message belongs to.
625 * @param message Message to store.
626 * @param psycstore_flags Flags indicating whether the PSYC message contains
628 * @param result_cb Callback to call with the result of the operation.
629 * @param cls Closure for the callback.
631 * @return Handle that can be used to cancel the operation.
633 struct GNUNET_PSYCSTORE_OperationHandle *
634 GNUNET_PSYCSTORE_fragment_store (struct GNUNET_PSYCSTORE_Handle *h,
635 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
636 const struct GNUNET_MULTICAST_MessageHeader *msg,
637 enum GNUNET_PSYCSTORE_MessageFlags psycstore_flags,
638 GNUNET_PSYCSTORE_ResultCallback result_cb,
641 uint16_t size = ntohs (msg->header.size);
642 struct FragmentStoreRequest *req;
643 struct GNUNET_MQ_Envelope *
644 env = GNUNET_MQ_msg_extra (req, size,
645 GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_STORE);
646 req->channel_key = *channel_key;
647 req->psycstore_flags = htonl (psycstore_flags);
648 GNUNET_memcpy (&req[1], msg, size);
651 op_send (h, op_create (h, h->op, result_cb, cls),
657 * Retrieve message fragments by fragment ID range.
660 * Handle for the PSYCstore.
662 * The channel we are interested in.
664 * The slave requesting the fragment. If not NULL, a membership test is
665 * performed first and the fragment is only returned if the slave has
667 * @param first_fragment_id
668 * First fragment ID to retrieve.
669 * Use 0 to get the latest message fragment.
670 * @param last_fragment_id
671 * Last consecutive fragment ID to retrieve.
672 * Use 0 to get the latest message fragment.
673 * @param fragment_limit
674 * Maximum number of fragments to retrieve.
676 * Callback to call with the retrieved fragments.
678 * Callback to call with the result of the operation.
680 * Closure for the callbacks.
682 * @return Handle that can be used to cancel the operation.
684 struct GNUNET_PSYCSTORE_OperationHandle *
685 GNUNET_PSYCSTORE_fragment_get (struct GNUNET_PSYCSTORE_Handle *h,
686 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
687 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
688 uint64_t first_fragment_id,
689 uint64_t last_fragment_id,
690 GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
691 GNUNET_PSYCSTORE_ResultCallback result_cb,
694 struct FragmentGetRequest *req;
695 struct GNUNET_MQ_Envelope *
696 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET);
697 req->channel_key = *channel_key;
698 req->first_fragment_id = GNUNET_htonll (first_fragment_id);
699 req->last_fragment_id = GNUNET_htonll (last_fragment_id);
700 if (NULL != slave_key)
702 req->slave_key = *slave_key;
703 req->do_membership_test = GNUNET_YES;
706 struct GNUNET_PSYCSTORE_OperationHandle *
707 op = op_create (h, h->op, result_cb, cls);
708 op->fragment_cb = fragment_cb;
710 return op_send (h, op, env, &req->op_id);
715 * Retrieve latest message fragments.
718 * Handle for the PSYCstore.
720 * The channel we are interested in.
722 * The slave requesting the fragment. If not NULL, a membership test is
723 * performed first and the fragment is only returned if the slave has
725 * @param first_fragment_id
726 * First fragment ID to retrieve.
727 * Use 0 to get the latest message fragment.
728 * @param last_fragment_id
729 * Last consecutive fragment ID to retrieve.
730 * Use 0 to get the latest message fragment.
731 * @param fragment_limit
732 * Maximum number of fragments to retrieve.
734 * Callback to call with the retrieved fragments.
736 * Callback to call with the result of the operation.
738 * Closure for the callbacks.
740 * @return Handle that can be used to cancel the operation.
742 struct GNUNET_PSYCSTORE_OperationHandle *
743 GNUNET_PSYCSTORE_fragment_get_latest (struct GNUNET_PSYCSTORE_Handle *h,
744 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
745 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
746 uint64_t fragment_limit,
747 GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
748 GNUNET_PSYCSTORE_ResultCallback result_cb,
751 struct FragmentGetRequest *req;
752 struct GNUNET_MQ_Envelope *
753 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_FRAGMENT_GET);
754 req->channel_key = *channel_key;
755 req->fragment_limit = GNUNET_ntohll (fragment_limit);
756 if (NULL != slave_key)
758 req->slave_key = *slave_key;
759 req->do_membership_test = GNUNET_YES;
762 struct GNUNET_PSYCSTORE_OperationHandle *
763 op = op_create (h, h->op, result_cb, cls);
764 op->fragment_cb = fragment_cb;
766 return op_send (h, op, env, &req->op_id);
771 * Retrieve all fragments of messages in a message ID range.
774 * Handle for the PSYCstore.
776 * The channel we are interested in.
778 * The slave requesting the message.
779 * If not NULL, a membership test is performed first
780 * and the message is only returned if the slave has access to it.
781 * @param first_message_id
782 * First message ID to retrieve.
783 * @param last_message_id
784 * Last consecutive message ID to retrieve.
785 * @param fragment_limit
786 * Maximum number of fragments to retrieve.
787 * @param method_prefix
788 * Retrieve only messages with a matching method prefix.
789 * @todo Implement method_prefix query.
791 * Callback to call with the retrieved fragments.
793 * Callback to call with the result of the operation.
795 * Closure for the callbacks.
797 * @return Handle that can be used to cancel the operation.
799 struct GNUNET_PSYCSTORE_OperationHandle *
800 GNUNET_PSYCSTORE_message_get (struct GNUNET_PSYCSTORE_Handle *h,
801 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
802 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
803 uint64_t first_message_id,
804 uint64_t last_message_id,
805 uint64_t fragment_limit,
806 const char *method_prefix,
807 GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
808 GNUNET_PSYCSTORE_ResultCallback result_cb,
811 struct MessageGetRequest *req;
812 if (NULL == method_prefix)
814 uint16_t method_size = strnlen (method_prefix,
815 GNUNET_MAX_MESSAGE_SIZE
816 - sizeof (*req)) + 1;
818 struct GNUNET_MQ_Envelope *
819 env = GNUNET_MQ_msg_extra (req, method_size,
820 GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET);
821 req->channel_key = *channel_key;
822 req->first_message_id = GNUNET_htonll (first_message_id);
823 req->last_message_id = GNUNET_htonll (last_message_id);
824 req->fragment_limit = GNUNET_htonll (fragment_limit);
825 if (NULL != slave_key)
827 req->slave_key = *slave_key;
828 req->do_membership_test = GNUNET_YES;
830 GNUNET_memcpy (&req[1], method_prefix, method_size);
831 ((char *) &req[1])[method_size - 1] = '\0';
833 struct GNUNET_PSYCSTORE_OperationHandle *
834 op = op_create (h, h->op, result_cb, cls);
835 op->fragment_cb = fragment_cb;
837 return op_send (h, op, env, &req->op_id);
842 * Retrieve all fragments of the latest messages.
845 * Handle for the PSYCstore.
847 * The channel we are interested in.
849 * The slave requesting the message.
850 * If not NULL, a membership test is performed first
851 * and the message is only returned if the slave has access to it.
852 * @param message_limit
853 * Maximum number of messages to retrieve.
854 * @param method_prefix
855 * Retrieve only messages with a matching method prefix.
856 * @todo Implement method_prefix query.
858 * Callback to call with the retrieved fragments.
860 * Callback to call with the result of the operation.
862 * Closure for the callbacks.
864 * @return Handle that can be used to cancel the operation.
866 struct GNUNET_PSYCSTORE_OperationHandle *
867 GNUNET_PSYCSTORE_message_get_latest (struct GNUNET_PSYCSTORE_Handle *h,
868 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
869 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
870 uint64_t message_limit,
871 const char *method_prefix,
872 GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
873 GNUNET_PSYCSTORE_ResultCallback result_cb,
876 struct MessageGetRequest *req;
878 if (NULL == method_prefix)
880 uint16_t method_size = strnlen (method_prefix,
881 GNUNET_MAX_MESSAGE_SIZE
882 - sizeof (*req)) + 1;
883 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
885 struct GNUNET_MQ_Envelope *
886 env = GNUNET_MQ_msg_extra (req, method_size,
887 GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET);
888 req->channel_key = *channel_key;
889 req->message_limit = GNUNET_ntohll (message_limit);
890 if (NULL != slave_key)
892 req->slave_key = *slave_key;
893 req->do_membership_test = GNUNET_YES;
895 GNUNET_memcpy (&req[1], method_prefix, method_size);
897 struct GNUNET_PSYCSTORE_OperationHandle *
898 op = op_create (h, h->op, result_cb, cls);
899 op->fragment_cb = fragment_cb;
901 return op_send (h, op, env, &req->op_id);
906 * Retrieve a fragment of message specified by its message ID and fragment
910 * Handle for the PSYCstore.
912 * The channel we are interested in.
914 * The slave requesting the message fragment. If not NULL, a membership
915 * test is performed first and the message fragment is only returned
916 * if the slave has access to it.
918 * Message ID to retrieve. Use 0 to get the latest message.
919 * @param fragment_offset
920 * Offset of the fragment to retrieve.
922 * Callback to call with the retrieved fragments.
924 * Callback to call with the result of the operation.
926 * Closure for the callbacks.
928 * @return Handle that can be used to cancel the operation.
930 struct GNUNET_PSYCSTORE_OperationHandle *
931 GNUNET_PSYCSTORE_message_get_fragment (struct GNUNET_PSYCSTORE_Handle *h,
932 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
933 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
935 uint64_t fragment_offset,
936 GNUNET_PSYCSTORE_FragmentCallback fragment_cb,
937 GNUNET_PSYCSTORE_ResultCallback result_cb,
940 struct MessageGetFragmentRequest *req;
941 struct GNUNET_MQ_Envelope *
942 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET_FRAGMENT);
944 req->channel_key = *channel_key;
945 req->message_id = GNUNET_htonll (message_id);
946 req->fragment_offset = GNUNET_htonll (fragment_offset);
947 if (NULL != slave_key)
949 req->slave_key = *slave_key;
950 req->do_membership_test = GNUNET_YES;
953 struct GNUNET_PSYCSTORE_OperationHandle *
954 op = op_create (h, h->op, result_cb, cls);
955 op->fragment_cb = fragment_cb;
957 return op_send (h, op, env, &req->op_id);
962 * Retrieve latest values of counters for a channel master.
964 * The current value of counters are needed when a channel master is restarted,
965 * so that it can continue incrementing the counters from their last value.
968 * Handle for the PSYCstore.
970 * Public key that identifies the channel.
972 * Callback to call with the result.
974 * Closure for the @a ccb callback.
976 * @return Handle that can be used to cancel the operation.
978 struct GNUNET_PSYCSTORE_OperationHandle *
979 GNUNET_PSYCSTORE_counters_get (struct GNUNET_PSYCSTORE_Handle *h,
980 struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
981 GNUNET_PSYCSTORE_CountersCallback counters_cb,
984 struct OperationRequest *req;
985 struct GNUNET_MQ_Envelope *
986 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET);
987 req->channel_key = *channel_key;
989 struct GNUNET_PSYCSTORE_OperationHandle *
990 op = op_create (h, h->op, NULL, NULL);
991 op->counters_cb = counters_cb;
993 return op_send (h, op, env, &req->op_id);
998 * Apply modifiers of a message to the current channel state.
1000 * An error is returned if there are missing messages containing state
1001 * operations before the current one.
1004 * Handle for the PSYCstore.
1005 * @param channel_key
1006 * The channel we are interested in.
1008 * ID of the message that contains the @a modifiers.
1009 * @param state_delta
1010 * Value of the _state_delta PSYC header variable of the message.
1012 * Callback to call with the result of the operation.
1014 * Closure for @a result_cb.
1016 * @return Handle that can be used to cancel the operation.
1018 struct GNUNET_PSYCSTORE_OperationHandle *
1019 GNUNET_PSYCSTORE_state_modify (struct GNUNET_PSYCSTORE_Handle *h,
1020 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1021 uint64_t message_id,
1022 uint64_t state_delta,
1023 GNUNET_PSYCSTORE_ResultCallback result_cb,
1026 struct StateModifyRequest *req;
1027 struct GNUNET_MQ_Envelope *
1028 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_MODIFY);
1029 req->channel_key = *channel_key;
1030 req->message_id = GNUNET_htonll (message_id);
1031 req->state_delta = GNUNET_htonll (state_delta);
1033 return op_send (h, op_create (h, h->op, result_cb, cls),
1038 struct StateSyncClosure
1040 GNUNET_PSYCSTORE_ResultCallback result_cb;
1047 state_sync_result (void *cls, int64_t result,
1048 const char *err_msg, uint16_t err_msg_size)
1050 struct StateSyncClosure *ssc = cls;
1051 if (GNUNET_OK != result || ssc->last)
1052 ssc->result_cb (ssc->cls, result, err_msg, err_msg_size);
1058 * Store synchronized state.
1061 * Handle for the PSYCstore.
1062 * @param channel_key
1063 * The channel we are interested in.
1064 * @param max_state_message_id
1065 * ID of the last stateful message before @a state_hash_message_id.
1066 * @param state_hash_message_id
1067 * ID of the message that contains the state_hash PSYC header variable.
1068 * @param modifier_count
1069 * Number of elements in the @a modifiers array.
1071 * Full state to store.
1073 * Callback to call with the result of the operation.
1075 * Closure for the callback.
1077 * @return Handle that can be used to cancel the operation.
1079 struct GNUNET_PSYCSTORE_OperationHandle *
1080 GNUNET_PSYCSTORE_state_sync (struct GNUNET_PSYCSTORE_Handle *h,
1081 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1082 uint64_t max_state_message_id,
1083 uint64_t state_hash_message_id,
1084 size_t modifier_count,
1085 const struct GNUNET_PSYC_Modifier *modifiers,
1086 GNUNET_PSYCSTORE_ResultCallback result_cb,
1089 struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
1092 for (i = 0; i < modifier_count; i++) {
1093 struct StateSyncRequest *req;
1094 uint16_t name_size = strlen (modifiers[i].name) + 1;
1096 struct GNUNET_MQ_Envelope *
1097 env = GNUNET_MQ_msg_extra (req,
1098 sizeof (*req) + name_size + modifiers[i].value_size,
1099 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC);
1101 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC);
1102 req->header.size = htons (sizeof (*req) + name_size
1103 + modifiers[i].value_size);
1104 req->channel_key = *channel_key;
1105 req->max_state_message_id = GNUNET_htonll (max_state_message_id);
1106 req->state_hash_message_id = GNUNET_htonll (state_hash_message_id);
1107 req->name_size = htons (name_size);
1111 : (modifier_count - 1 == i)
1115 GNUNET_memcpy (&req[1], modifiers[i].name, name_size);
1116 GNUNET_memcpy ((char *) &req[1] + name_size, modifiers[i].value, modifiers[i].value_size);
1118 struct StateSyncClosure *ssc = GNUNET_malloc (sizeof (*ssc));
1119 ssc->last = (req->flags & STATE_OP_LAST);
1120 ssc->result_cb = result_cb;
1123 op_send (h, op_create (h, h->op, state_sync_result, ssc),
1126 // FIXME: only one operation is returned,
1127 // add pointers to other operations and make all cancellable.
1133 * Reset the state of a channel.
1135 * Delete all state variables stored for the given channel.
1138 * Handle for the PSYCstore.
1139 * @param channel_key
1140 * The channel we are interested in.
1142 * Callback to call with the result of the operation.
1144 * Closure for the callback.
1146 * @return Handle that can be used to cancel the operation.
1148 struct GNUNET_PSYCSTORE_OperationHandle *
1149 GNUNET_PSYCSTORE_state_reset (struct GNUNET_PSYCSTORE_Handle *h,
1150 const struct GNUNET_CRYPTO_EddsaPublicKey
1152 GNUNET_PSYCSTORE_ResultCallback result_cb,
1155 struct OperationRequest *req;
1156 struct GNUNET_MQ_Envelope *
1157 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_RESET);
1158 req->channel_key = *channel_key;
1161 op_send (h, op_create (h, h->op, result_cb, cls),
1167 * Update signed values of state variables in the state store.
1170 * Handle for the PSYCstore.
1171 * @param channel_key
1172 * The channel we are interested in.
1174 * Message ID that contained the state @a hash.
1176 * Hash of the serialized full state.
1178 * Callback to call with the result of the operation.
1180 * Closure for the callback.
1182 struct GNUNET_PSYCSTORE_OperationHandle *
1183 GNUNET_PSYCSTORE_state_hash_update (struct GNUNET_PSYCSTORE_Handle *h,
1184 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1185 uint64_t message_id,
1186 const struct GNUNET_HashCode *hash,
1187 GNUNET_PSYCSTORE_ResultCallback result_cb,
1190 struct StateHashUpdateRequest *req;
1191 struct GNUNET_MQ_Envelope *
1192 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_HASH_UPDATE);
1193 req->channel_key = *channel_key;
1197 op_send (h, op_create (h, h->op, result_cb, cls),
1203 * Retrieve the best matching state variable.
1206 * Handle for the PSYCstore.
1207 * @param channel_key
1208 * The channel we are interested in.
1210 * Name of variable to match, the returned variable might be less specific.
1212 * Callback to return the matching state variable.
1214 * Callback to call with the result of the operation.
1216 * Closure for the callbacks.
1218 * @return Handle that can be used to cancel the operation.
1220 struct GNUNET_PSYCSTORE_OperationHandle *
1221 GNUNET_PSYCSTORE_state_get (struct GNUNET_PSYCSTORE_Handle *h,
1222 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1224 GNUNET_PSYCSTORE_StateCallback state_cb,
1225 GNUNET_PSYCSTORE_ResultCallback result_cb,
1228 size_t name_size = strlen (name) + 1;
1229 struct OperationRequest *req;
1230 struct GNUNET_MQ_Envelope *
1231 env = GNUNET_MQ_msg_extra (req, name_size,
1232 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET);
1233 req->channel_key = *channel_key;
1234 GNUNET_memcpy (&req[1], name, name_size);
1236 struct GNUNET_PSYCSTORE_OperationHandle *
1237 op = op_create (h, h->op, result_cb, cls);
1238 op->state_cb = state_cb;
1240 return op_send (h, op, env, &req->op_id);
1245 * Retrieve all state variables for a channel with the given prefix.
1248 * Handle for the PSYCstore.
1249 * @param channel_key
1250 * The channel we are interested in.
1251 * @param name_prefix
1252 * Prefix of state variable names to match.
1254 * Callback to return matching state variables.
1256 * Callback to call with the result of the operation.
1258 * Closure for the callbacks.
1260 * @return Handle that can be used to cancel the operation.
1262 struct GNUNET_PSYCSTORE_OperationHandle *
1263 GNUNET_PSYCSTORE_state_get_prefix (struct GNUNET_PSYCSTORE_Handle *h,
1264 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1265 const char *name_prefix,
1266 GNUNET_PSYCSTORE_StateCallback state_cb,
1267 GNUNET_PSYCSTORE_ResultCallback result_cb,
1270 size_t name_size = strlen (name_prefix) + 1;
1271 struct OperationRequest *req;
1272 struct GNUNET_MQ_Envelope *
1273 env = GNUNET_MQ_msg_extra (req, name_size,
1274 GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET_PREFIX);
1275 req->channel_key = *channel_key;
1276 GNUNET_memcpy (&req[1], name_prefix, name_size);
1278 struct GNUNET_PSYCSTORE_OperationHandle *
1279 op = op_create (h, h->op, result_cb, cls);
1280 op->state_cb = state_cb;
1282 return op_send (h, op, env, &req->op_id);
1285 /* end of psycstore_api.c */