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 psyc/gnunet-service-psyc.c
24 * @author Gabor X Toth
30 #include "gnunet_util_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_multicast_service.h"
35 #include "gnunet_psycstore_service.h"
36 #include "gnunet_psyc_service.h"
37 #include "gnunet_psyc_util_lib.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;
57 * Handle to the PSYCstore.
59 static struct GNUNET_PSYCSTORE_Handle *store;
62 * All connected masters.
63 * Channel's pub_key_hash -> struct Master
65 static struct GNUNET_CONTAINER_MultiHashMap *masters;
68 * All connected slaves.
69 * Channel's pub_key_hash -> struct Slave
71 static struct GNUNET_CONTAINER_MultiHashMap *slaves;
74 * Connected slaves per channel.
75 * Channel's pub_key_hash -> Slave's pub_key -> struct Slave
77 static struct GNUNET_CONTAINER_MultiHashMap *channel_slaves;
81 * Message in the transmission queue.
83 struct TransmitMessage
85 struct TransmitMessage *prev;
86 struct TransmitMessage *next;
88 struct GNUNET_SERVER_Client *client;
91 * ID assigned to the message.
101 * Type of first message part.
103 uint16_t first_ptype;
106 * Type of last message part.
110 /* Followed by message */
115 * Cache for received message fragments.
116 * Message fragments are only sent to clients after all modifiers arrived.
118 * chan_key -> MultiHashMap chan_msgs
120 static struct GNUNET_CONTAINER_MultiHashMap *recv_cache;
124 * Entry in the chan_msgs hashmap of @a recv_cache:
125 * fragment_id -> RecvCacheEntry
127 struct RecvCacheEntry
129 struct GNUNET_MULTICAST_MessageHeader *mmsg;
135 * Entry in the @a recv_frags hash map of a @a Channel.
136 * message_id -> FragmentQueue
141 * Fragment IDs stored in @a recv_cache.
143 struct GNUNET_CONTAINER_Heap *fragments;
146 * Total size of received fragments.
151 * Total size of received header fragments (METHOD & MODIFIERs)
153 uint64_t header_size;
156 * The @a state_delta field from struct GNUNET_PSYC_MessageMethod.
158 uint64_t state_delta;
161 * The @a flags field from struct GNUNET_PSYC_MessageMethod.
166 * Receive state of message.
168 * @see MessageFragmentState
173 * Whether the state is already modified in PSYCstore.
175 uint8_t state_is_modified;
178 * Is the message queued for delivery to the client?
179 * i.e. added to the recv_msgs queue
186 * List of connected clients.
193 struct GNUNET_SERVER_Client *client;
199 struct Operation *prev;
200 struct Operation *next;
202 struct GNUNET_SERVER_Client *client;
210 * Common part of the client context for both a channel master and slave.
214 struct Client *clients_head;
215 struct Client *clients_tail;
217 struct Operation *op_head;
218 struct Operation *op_tail;
220 struct TransmitMessage *tmit_head;
221 struct TransmitMessage *tmit_tail;
224 * Current PSYCstore operation.
226 struct GNUNET_PSYCSTORE_OperationHandle *store_op;
229 * Received fragments not yet sent to the client.
230 * message_id -> FragmentQueue
232 struct GNUNET_CONTAINER_MultiHashMap *recv_frags;
235 * Received message IDs not yet sent to the client.
237 struct GNUNET_CONTAINER_Heap *recv_msgs;
240 * Public key of the channel.
242 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
245 * Hash of @a pub_key.
247 struct GNUNET_HashCode pub_key_hash;
250 * Last message ID sent to the client.
251 * 0 if there is no such message.
253 uint64_t max_message_id;
256 * ID of the last stateful message, where the state operations has been
257 * processed and saved to PSYCstore and which has been sent to the client.
258 * 0 if there is no such message.
260 uint64_t max_state_message_id;
263 * Expected value size for the modifier being received from the PSYC service.
265 uint32_t tmit_mod_value_size_expected;
268 * Actual value size for the modifier being received from the PSYC service.
270 uint32_t tmit_mod_value_size;
273 * Is this a channel master (#GNUNET_YES), or slave (#GNUNET_NO)?
278 * Is this channel ready to receive messages from client?
279 * #GNUNET_YES or #GNUNET_NO
284 * Is the client disconnected?
285 * #GNUNET_YES or #GNUNET_NO
287 uint8_t is_disconnected;
292 * Client context for a channel master.
297 * Channel struct common for Master and Slave
302 * Private key of the channel.
304 struct GNUNET_CRYPTO_EddsaPrivateKey priv_key;
307 * Handle for the multicast origin.
309 struct GNUNET_MULTICAST_Origin *origin;
312 * Transmit handle for multicast.
314 struct GNUNET_MULTICAST_OriginTransmitHandle *tmit_handle;
317 * Incoming join requests from multicast.
318 * member_pub_key -> struct GNUNET_MULTICAST_JoinHandle *
320 struct GNUNET_CONTAINER_MultiHashMap *join_reqs;
323 * Last message ID transmitted to this channel.
325 * Incremented before sending a message, thus the message_id in messages sent
328 uint64_t max_message_id;
331 * ID of the last message with state operations transmitted to the channel.
332 * 0 if there is no such message.
334 uint64_t max_state_message_id;
337 * Maximum group generation transmitted to the channel.
339 uint64_t max_group_generation;
342 * @see enum GNUNET_PSYC_Policy
344 enum GNUNET_PSYC_Policy policy;
349 * Client context for a channel slave.
354 * Channel struct common for Master and Slave
359 * Private key of the slave.
361 struct GNUNET_CRYPTO_EcdsaPrivateKey priv_key;
364 * Public key of the slave.
366 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
369 * Hash of @a pub_key.
371 struct GNUNET_HashCode pub_key_hash;
374 * Handle for the multicast member.
376 struct GNUNET_MULTICAST_Member *member;
379 * Transmit handle for multicast.
381 struct GNUNET_MULTICAST_MemberTransmitHandle *tmit_handle;
384 * Peer identity of the origin.
386 struct GNUNET_PeerIdentity origin;
389 * Number of items in @a relays.
391 uint32_t relay_count;
394 * Relays that multicast can use to connect.
396 struct GNUNET_PeerIdentity *relays;
399 * Join request to be transmitted to the master on join.
401 struct GNUNET_PSYC_Message *join_msg;
404 * Join decision received from multicast.
406 struct GNUNET_PSYC_JoinDecisionMessage *join_dcsn;
409 * Maximum request ID for this channel.
411 uint64_t max_request_id;
416 enum GNUNET_PSYC_SlaveJoinFlags join_flags;
421 transmit_message (struct Channel *chn);
424 message_queue_run (struct Channel *chn);
427 message_queue_drop (struct Channel *chn);
431 schedule_transmit_message (void *cls,
432 const struct GNUNET_SCHEDULER_TaskContext *tc)
434 struct Channel *chn = cls;
435 transmit_message (chn);
440 * Task run during shutdown.
446 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
450 GNUNET_SERVER_notification_context_destroy (nc);
455 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
461 static struct Operation *
462 op_add (struct Channel *chn, struct GNUNET_SERVER_Client *client,
463 uint64_t op_id, uint32_t flags)
465 struct Operation *op = GNUNET_malloc (sizeof (*op));
470 GNUNET_CONTAINER_DLL_insert (chn->op_head, chn->op_tail, op);
476 op_remove (struct Operation *op)
478 GNUNET_CONTAINER_DLL_remove (op->chn->op_head, op->chn->op_tail, op);
484 * Clean up master data structures after a client disconnected.
487 cleanup_master (struct Master *mst)
489 struct Channel *chn = &mst->chn;
491 if (NULL != mst->origin)
492 GNUNET_MULTICAST_origin_stop (mst->origin, NULL, NULL); // FIXME
493 GNUNET_CONTAINER_multihashmap_destroy (mst->join_reqs);
494 GNUNET_CONTAINER_multihashmap_remove (masters, &chn->pub_key_hash, mst);
499 * Clean up slave data structures after a client disconnected.
502 cleanup_slave (struct Slave *slv)
504 struct Channel *chn = &slv->chn;
505 struct GNUNET_CONTAINER_MultiHashMap *
506 chn_slv = GNUNET_CONTAINER_multihashmap_get (channel_slaves,
508 GNUNET_assert (NULL != chn_slv);
509 GNUNET_CONTAINER_multihashmap_remove (chn_slv, &slv->pub_key_hash, slv);
511 if (0 == GNUNET_CONTAINER_multihashmap_size (chn_slv))
513 GNUNET_CONTAINER_multihashmap_remove (channel_slaves, &chn->pub_key_hash,
515 GNUNET_CONTAINER_multihashmap_destroy (chn_slv);
517 GNUNET_CONTAINER_multihashmap_remove (slaves, &chn->pub_key_hash, slv);
519 if (NULL != slv->join_msg)
521 GNUNET_free (slv->join_msg);
522 slv->join_msg = NULL;
524 if (NULL != slv->relays)
526 GNUNET_free (slv->relays);
529 if (NULL != slv->member)
531 GNUNET_MULTICAST_member_part (slv->member, NULL, NULL); // FIXME
534 GNUNET_CONTAINER_multihashmap_remove (slaves, &chn->pub_key_hash, slv);
539 * Clean up channel data structures after a client disconnected.
542 cleanup_channel (struct Channel *chn)
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "%p Cleaning up channel %s. master? %u\n",
546 chn, GNUNET_h2s (&chn->pub_key_hash), chn->is_master);
547 message_queue_drop (chn);
548 GNUNET_CONTAINER_multihashmap_destroy (chn->recv_frags);
549 chn->recv_frags = NULL;
551 if (NULL != chn->store_op)
553 GNUNET_PSYCSTORE_operation_cancel (chn->store_op);
554 chn->store_op = NULL;
557 (GNUNET_YES == chn->is_master)
558 ? cleanup_master ((struct Master *) chn)
559 : cleanup_slave ((struct Slave *) chn);
565 * Called whenever a client is disconnected.
566 * Frees our resources associated with that client.
568 * @param cls Closure.
569 * @param client Identification of the client.
572 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
578 chn = GNUNET_SERVER_client_get_user_context (client, struct Channel);
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
583 "%p User context is NULL in client_disconnect()\n", chn);
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "%p Client (%s) disconnected from channel %s\n",
590 chn, (GNUNET_YES == chn->is_master) ? "master" : "slave",
591 GNUNET_h2s (&chn->pub_key_hash));
593 struct Client *cli = chn->clients_head;
596 if (cli->client == client)
598 GNUNET_CONTAINER_DLL_remove (chn->clients_head, chn->clients_tail, cli);
605 struct Operation *op = chn->op_head;
608 if (op->client == client)
616 if (NULL == chn->clients_head)
617 { /* Last client disconnected. */
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "%p Last client (%s) disconnected from channel %s\n",
620 chn, (GNUNET_YES == chn->is_master) ? "master" : "slave",
621 GNUNET_h2s (&chn->pub_key_hash));
622 chn->is_disconnected = GNUNET_YES;
623 if (NULL != chn->tmit_head)
624 { /* Send pending messages to multicast before cleanup. */
625 transmit_message (chn);
629 cleanup_channel (chn);
636 * Send message to all clients connected to the channel.
639 client_send_msg (const struct Channel *chn,
640 const struct GNUNET_MessageHeader *msg)
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
643 "%p Sending message to clients.\n", chn);
645 struct Client *cli = chn->clients_head;
648 GNUNET_SERVER_notification_context_add (nc, cli->client);
649 GNUNET_SERVER_notification_context_unicast (nc, cli->client, msg, GNUNET_NO);
656 * Send a result code back to the client.
659 * Client that should receive the result code.
663 * Operation ID in network byte order.
665 * Data payload or NULL.
670 client_send_result (struct GNUNET_SERVER_Client *client, uint64_t op_id,
671 int64_t result_code, const void *data, uint16_t data_size)
673 struct GNUNET_OperationResultMessage *res;
675 res = GNUNET_malloc (sizeof (*res) + data_size);
676 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE);
677 res->header.size = htons (sizeof (*res) + data_size);
678 res->result_code = GNUNET_htonll (result_code);
681 memcpy (&res[1], data, data_size);
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684 "%p Sending result to client for operation #%" PRIu64 ": "
685 "%" PRId64 " (size: %u)\n",
686 client, GNUNET_ntohll (op_id), result_code, data_size);
688 GNUNET_SERVER_notification_context_add (nc, client);
689 GNUNET_SERVER_notification_context_unicast (nc, client, &res->header,
696 * Closure for join_mem_test_cb()
698 struct JoinMemTestClosure
700 struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
702 struct GNUNET_MULTICAST_JoinHandle *jh;
703 struct GNUNET_PSYC_JoinRequestMessage *join_msg;
708 * Membership test result callback used for join requests.
711 join_mem_test_cb (void *cls, int64_t result,
712 const char *err_msg, uint16_t err_msg_size)
714 struct JoinMemTestClosure *jcls = cls;
716 if (GNUNET_NO == result && GNUNET_YES == jcls->chn->is_master)
717 { /* Pass on join request to client if this is a master channel */
718 struct Master *mst = (struct Master *) jcls->chn;
719 struct GNUNET_HashCode slave_pub_hash;
720 GNUNET_CRYPTO_hash (&jcls->slave_pub_key, sizeof (jcls->slave_pub_key),
722 GNUNET_CONTAINER_multihashmap_put (mst->join_reqs, &slave_pub_hash, jcls->jh,
723 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
724 client_send_msg (jcls->chn, &jcls->join_msg->header);
728 if (GNUNET_SYSERR == result)
730 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
731 "Could not perform membership test (%.*s)\n",
732 err_msg_size, err_msg);
735 GNUNET_MULTICAST_join_decision (jcls->jh, result, 0, NULL, NULL);
737 GNUNET_free (jcls->join_msg);
743 * Incoming join request from multicast.
746 mcast_recv_join_request (void *cls,
747 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_pub_key,
748 const struct GNUNET_MessageHeader *join_msg,
749 struct GNUNET_MULTICAST_JoinHandle *jh)
751 struct Channel *chn = cls;
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%p Got join request.\n", chn);
754 uint16_t join_msg_size = 0;
755 if (NULL != join_msg)
757 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE == ntohs (join_msg->type))
759 join_msg_size = ntohs (join_msg->size);
763 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
764 "%p Got join message with invalid type %u.\n",
765 chn, ntohs (join_msg->type));
769 struct GNUNET_PSYC_JoinRequestMessage *
770 req = GNUNET_malloc (sizeof (*req) + join_msg_size);
771 req->header.size = htons (sizeof (*req) + join_msg_size);
772 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST);
773 req->slave_pub_key = *slave_pub_key;
774 if (0 < join_msg_size)
775 memcpy (&req[1], join_msg, join_msg_size);
777 struct JoinMemTestClosure *jcls = GNUNET_malloc (sizeof (*jcls));
778 jcls->slave_pub_key = *slave_pub_key;
781 jcls->join_msg = req;
783 GNUNET_PSYCSTORE_membership_test (store, &chn->pub_key, slave_pub_key,
784 chn->max_message_id, 0,
785 &join_mem_test_cb, jcls);
790 * Join decision received from multicast.
793 mcast_recv_join_decision (void *cls, int is_admitted,
794 const struct GNUNET_PeerIdentity *peer,
795 uint16_t relay_count,
796 const struct GNUNET_PeerIdentity *relays,
797 const struct GNUNET_MessageHeader *join_resp)
799 struct Slave *slv = cls;
800 struct Channel *chn = &slv->chn;
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "%p Got join decision: %d\n", slv, is_admitted);
803 if (GNUNET_YES == chn->is_ready)
805 /* Already admitted */
809 uint16_t join_resp_size = (NULL != join_resp) ? ntohs (join_resp->size) : 0;
810 struct GNUNET_PSYC_JoinDecisionMessage *
811 dcsn = slv->join_dcsn = GNUNET_malloc (sizeof (*dcsn) + join_resp_size);
812 dcsn->header.size = htons (sizeof (*dcsn) + join_resp_size);
813 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
814 dcsn->is_admitted = htonl (is_admitted);
815 if (0 < join_resp_size)
816 memcpy (&dcsn[1], join_resp, join_resp_size);
818 client_send_msg (chn, &dcsn->header);
820 if (GNUNET_YES == is_admitted
821 && ! (GNUNET_PSYC_SLAVE_JOIN_LOCAL & slv->join_flags))
823 chn->is_ready = GNUNET_YES;
829 store_recv_fragment_replay (void *cls,
830 struct GNUNET_MULTICAST_MessageHeader *msg,
831 enum GNUNET_PSYCSTORE_MessageFlags flags)
833 struct GNUNET_MULTICAST_ReplayHandle *rh = cls;
835 GNUNET_MULTICAST_replay_response (rh, &msg->header, GNUNET_MULTICAST_REC_OK);
841 * Received result of GNUNET_PSYCSTORE_fragment_get() for multicast replay.
844 store_recv_fragment_replay_result (void *cls, int64_t result,
845 const char *err_msg, uint16_t err_msg_size)
847 struct GNUNET_MULTICAST_ReplayHandle *rh = cls;
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "%p Fragment replay: PSYCSTORE returned %" PRId64 " (%.*s)\n",
850 rh, result, err_msg_size, err_msg);
858 GNUNET_MULTICAST_replay_response (rh, NULL,
859 GNUNET_MULTICAST_REC_NOT_FOUND);
862 case GNUNET_PSYCSTORE_MEMBERSHIP_TEST_FAILED:
863 GNUNET_MULTICAST_replay_response (rh, NULL,
864 GNUNET_MULTICAST_REC_ACCESS_DENIED);
868 GNUNET_MULTICAST_replay_response (rh, NULL,
869 GNUNET_MULTICAST_REC_INTERNAL_ERROR);
872 GNUNET_MULTICAST_replay_response_end (rh);
877 * Incoming fragment replay request from multicast.
880 mcast_recv_replay_fragment (void *cls,
881 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_pub_key,
882 uint64_t fragment_id, uint64_t flags,
883 struct GNUNET_MULTICAST_ReplayHandle *rh)
886 struct Channel *chn = cls;
887 GNUNET_PSYCSTORE_fragment_get (store, &chn->pub_key, slave_pub_key,
888 fragment_id, fragment_id,
889 &store_recv_fragment_replay,
890 &store_recv_fragment_replay_result, rh);
895 * Incoming message replay request from multicast.
898 mcast_recv_replay_message (void *cls,
899 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_pub_key,
901 uint64_t fragment_offset,
903 struct GNUNET_MULTICAST_ReplayHandle *rh)
905 struct Channel *chn = cls;
906 GNUNET_PSYCSTORE_message_get (store, &chn->pub_key, slave_pub_key,
907 message_id, message_id, 1, NULL,
908 &store_recv_fragment_replay,
909 &store_recv_fragment_replay_result, rh);
914 * Convert an uint64_t in network byte order to a HashCode
915 * that can be used as key in a MultiHashMap
918 hash_key_from_nll (struct GNUNET_HashCode *key, uint64_t n)
920 /* use little-endian order, as idx_of MultiHashMap casts key to unsigned int */
921 /* TODO: use built-in byte swap functions if available */
923 n = ((n << 8) & 0xFF00FF00FF00FF00ULL) | ((n >> 8) & 0x00FF00FF00FF00FFULL);
924 n = ((n << 16) & 0xFFFF0000FFFF0000ULL) | ((n >> 16) & 0x0000FFFF0000FFFFULL);
926 *key = (struct GNUNET_HashCode) {};
928 = (n << 32) | (n >> 32);
933 * Convert an uint64_t in host byte order to a HashCode
934 * that can be used as key in a MultiHashMap
937 hash_key_from_hll (struct GNUNET_HashCode *key, uint64_t n)
939 #if __BYTE_ORDER == __BIG_ENDIAN
940 hash_key_from_nll (key, n);
941 #elif __BYTE_ORDER == __LITTLE_ENDIAN
942 *key = (struct GNUNET_HashCode) {};
943 *((uint64_t *) key) = n;
945 #error byteorder undefined
951 * Initialize PSYC message header.
954 psyc_msg_init (struct GNUNET_PSYC_MessageHeader *pmsg,
955 const struct GNUNET_MULTICAST_MessageHeader *mmsg, uint32_t flags)
957 uint16_t size = ntohs (mmsg->header.size);
958 uint16_t psize = sizeof (*pmsg) + size - sizeof (*mmsg);
960 pmsg->header.size = htons (psize);
961 pmsg->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
962 pmsg->message_id = mmsg->message_id;
963 pmsg->fragment_offset = mmsg->fragment_offset;
964 pmsg->flags = htonl (flags);
966 memcpy (&pmsg[1], &mmsg[1], size - sizeof (*mmsg));
971 * Create a new PSYC message from a multicast message for sending it to clients.
973 static inline struct GNUNET_PSYC_MessageHeader *
974 psyc_msg_new (const struct GNUNET_MULTICAST_MessageHeader *mmsg, uint32_t flags)
976 struct GNUNET_PSYC_MessageHeader *pmsg;
977 uint16_t size = ntohs (mmsg->header.size);
978 uint16_t psize = sizeof (*pmsg) + size - sizeof (*mmsg);
980 pmsg = GNUNET_malloc (psize);
981 psyc_msg_init (pmsg, mmsg, flags);
987 * Send multicast message to all clients connected to the channel.
990 client_send_mcast_msg (struct Channel *chn,
991 const struct GNUNET_MULTICAST_MessageHeader *mmsg,
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "%p Sending multicast message to client. "
996 "fragment_id: %" PRIu64 ", message_id: %" PRIu64 "\n",
997 chn, GNUNET_ntohll (mmsg->fragment_id),
998 GNUNET_ntohll (mmsg->message_id));
1000 struct GNUNET_PSYC_MessageHeader *
1001 pmsg = GNUNET_PSYC_message_header_create (mmsg, flags);
1002 client_send_msg (chn, &pmsg->header);
1008 * Send multicast request to all clients connected to the channel.
1011 client_send_mcast_req (struct Master *mst,
1012 const struct GNUNET_MULTICAST_RequestHeader *req)
1014 struct Channel *chn = &mst->chn;
1016 struct GNUNET_PSYC_MessageHeader *pmsg;
1017 uint16_t size = ntohs (req->header.size);
1018 uint16_t psize = sizeof (*pmsg) + size - sizeof (*req);
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021 "%p Sending multicast request to client. "
1022 "fragment_id: %" PRIu64 ", message_id: %" PRIu64 "\n",
1023 chn, GNUNET_ntohll (req->fragment_id),
1024 GNUNET_ntohll (req->request_id));
1026 pmsg = GNUNET_malloc (psize);
1027 pmsg->header.size = htons (psize);
1028 pmsg->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
1029 pmsg->message_id = req->request_id;
1030 pmsg->fragment_offset = req->fragment_offset;
1031 pmsg->flags = htonl (GNUNET_PSYC_MESSAGE_REQUEST);
1032 pmsg->slave_pub_key = req->member_pub_key;
1033 memcpy (&pmsg[1], &req[1], size - sizeof (*req));
1035 client_send_msg (chn, &pmsg->header);
1037 /* FIXME: save req to PSYCstore so that it can be resent later to clients */
1044 * Insert a multicast message fragment into the queue belonging to the message.
1046 * @param chn Channel.
1047 * @param mmsg Multicast message fragment.
1048 * @param msg_id_hash Message ID of @a mmsg in a struct GNUNET_HashCode.
1049 * @param first_ptype First PSYC message part type in @a mmsg.
1050 * @param last_ptype Last PSYC message part type in @a mmsg.
1053 fragment_queue_insert (struct Channel *chn,
1054 const struct GNUNET_MULTICAST_MessageHeader *mmsg,
1055 uint16_t first_ptype, uint16_t last_ptype)
1057 const uint16_t size = ntohs (mmsg->header.size);
1058 const uint64_t frag_offset = GNUNET_ntohll (mmsg->fragment_offset);
1059 struct GNUNET_CONTAINER_MultiHashMap
1060 *chan_msgs = GNUNET_CONTAINER_multihashmap_get (recv_cache,
1061 &chn->pub_key_hash);
1063 struct GNUNET_HashCode msg_id_hash;
1064 hash_key_from_nll (&msg_id_hash, mmsg->message_id);
1066 struct FragmentQueue
1067 *fragq = GNUNET_CONTAINER_multihashmap_get (chn->recv_frags, &msg_id_hash);
1071 fragq = GNUNET_new (struct FragmentQueue);
1072 fragq->state = MSG_FRAG_STATE_HEADER;
1074 = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1076 GNUNET_CONTAINER_multihashmap_put (chn->recv_frags, &msg_id_hash, fragq,
1077 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1079 if (NULL == chan_msgs)
1081 chan_msgs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1082 GNUNET_CONTAINER_multihashmap_put (recv_cache, &chn->pub_key_hash, chan_msgs,
1083 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1087 struct GNUNET_HashCode frag_id_hash;
1088 hash_key_from_nll (&frag_id_hash, mmsg->fragment_id);
1089 struct RecvCacheEntry
1090 *cache_entry = GNUNET_CONTAINER_multihashmap_get (chan_msgs, &frag_id_hash);
1091 if (NULL == cache_entry)
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 "%p Adding message fragment to cache. "
1095 "message_id: %" PRIu64 ", fragment_id: %" PRIu64 "\n",
1096 chn, GNUNET_ntohll (mmsg->message_id),
1097 GNUNET_ntohll (mmsg->fragment_id));
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1099 "%p header_size: %" PRIu64 " + %u\n",
1100 chn, fragq->header_size, size);
1101 cache_entry = GNUNET_new (struct RecvCacheEntry);
1102 cache_entry->ref_count = 1;
1103 cache_entry->mmsg = GNUNET_malloc (size);
1104 memcpy (cache_entry->mmsg, mmsg, size);
1105 GNUNET_CONTAINER_multihashmap_put (chan_msgs, &frag_id_hash, cache_entry,
1106 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1110 cache_entry->ref_count++;
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 "%p Message fragment is already in cache. "
1113 "message_id: %" PRIu64 ", fragment_id: %" PRIu64
1114 ", ref_count: %u\n",
1115 chn, GNUNET_ntohll (mmsg->message_id),
1116 GNUNET_ntohll (mmsg->fragment_id), cache_entry->ref_count);
1119 if (MSG_FRAG_STATE_HEADER == fragq->state)
1121 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == first_ptype)
1123 struct GNUNET_PSYC_MessageMethod *
1124 pmeth = (struct GNUNET_PSYC_MessageMethod *) &mmsg[1];
1125 fragq->state_delta = GNUNET_ntohll (pmeth->state_delta);
1126 fragq->flags = ntohl (pmeth->flags);
1129 if (last_ptype < GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA)
1131 fragq->header_size += size;
1133 else if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == first_ptype
1134 || frag_offset == fragq->header_size)
1135 { /* header is now complete */
1136 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1137 "%p Header of message %" PRIu64 " is complete.\n",
1138 chn, GNUNET_ntohll (mmsg->message_id));
1140 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1141 "%p Adding message %" PRIu64 " to queue.\n",
1142 chn, GNUNET_ntohll (mmsg->message_id));
1143 fragq->state = MSG_FRAG_STATE_DATA;
1147 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1148 "%p Header of message %" PRIu64 " is NOT complete yet: "
1149 "%" PRIu64 " != %" PRIu64 "\n",
1150 chn, GNUNET_ntohll (mmsg->message_id),
1151 frag_offset, fragq->header_size);
1157 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
1158 if (frag_offset == fragq->size)
1159 fragq->state = MSG_FRAG_STATE_END;
1161 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1162 "%p Message %" PRIu64 " is NOT complete yet: "
1163 "%" PRIu64 " != %" PRIu64 "\n",
1164 chn, GNUNET_ntohll (mmsg->message_id),
1165 frag_offset, fragq->size);
1168 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
1169 /* Drop message without delivering to client if it's a single fragment */
1171 (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == first_ptype)
1172 ? MSG_FRAG_STATE_DROP
1173 : MSG_FRAG_STATE_CANCEL;
1176 switch (fragq->state)
1178 case MSG_FRAG_STATE_DATA:
1179 case MSG_FRAG_STATE_END:
1180 case MSG_FRAG_STATE_CANCEL:
1181 if (GNUNET_NO == fragq->is_queued)
1183 GNUNET_CONTAINER_heap_insert (chn->recv_msgs, NULL,
1184 GNUNET_ntohll (mmsg->message_id));
1185 fragq->is_queued = GNUNET_YES;
1189 fragq->size += size;
1190 GNUNET_CONTAINER_heap_insert (fragq->fragments, NULL,
1191 GNUNET_ntohll (mmsg->fragment_id));
1196 * Run fragment queue of a message.
1198 * Send fragments of a message in order to client, after all modifiers arrived
1204 * ID of the message @a fragq belongs to.
1206 * Fragment queue of the message.
1208 * Drop message without delivering to client?
1209 * #GNUNET_YES or #GNUNET_NO.
1212 fragment_queue_run (struct Channel *chn, uint64_t msg_id,
1213 struct FragmentQueue *fragq, uint8_t drop)
1215 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1216 "%p Running message fragment queue for message %" PRIu64
1218 chn, msg_id, fragq->state);
1220 struct GNUNET_CONTAINER_MultiHashMap
1221 *chan_msgs = GNUNET_CONTAINER_multihashmap_get (recv_cache,
1222 &chn->pub_key_hash);
1223 GNUNET_assert (NULL != chan_msgs);
1226 while (GNUNET_YES == GNUNET_CONTAINER_heap_peek2 (fragq->fragments, NULL,
1229 struct GNUNET_HashCode frag_id_hash;
1230 hash_key_from_hll (&frag_id_hash, frag_id);
1231 struct RecvCacheEntry *cache_entry
1232 = GNUNET_CONTAINER_multihashmap_get (chan_msgs, &frag_id_hash);
1233 if (cache_entry != NULL)
1235 if (GNUNET_NO == drop)
1237 client_send_mcast_msg (chn, cache_entry->mmsg, 0);
1239 if (cache_entry->ref_count <= 1)
1241 GNUNET_CONTAINER_multihashmap_remove (chan_msgs, &frag_id_hash,
1243 GNUNET_free (cache_entry->mmsg);
1244 GNUNET_free (cache_entry);
1248 cache_entry->ref_count--;
1251 #if CACHE_AGING_IMPLEMENTED
1252 else if (GNUNET_NO == drop)
1254 /* TODO: fragment not in cache anymore, retrieve it from PSYCstore */
1258 GNUNET_CONTAINER_heap_remove_root (fragq->fragments);
1261 if (MSG_FRAG_STATE_END <= fragq->state)
1263 struct GNUNET_HashCode msg_id_hash;
1264 hash_key_from_hll (&msg_id_hash, msg_id);
1266 GNUNET_CONTAINER_multihashmap_remove (chn->recv_frags, &msg_id_hash, fragq);
1267 GNUNET_CONTAINER_heap_destroy (fragq->fragments);
1268 GNUNET_free (fragq);
1272 fragq->is_queued = GNUNET_NO;
1277 struct StateModifyClosure
1279 struct Channel *chn;
1281 struct GNUNET_HashCode msg_id_hash;
1286 store_recv_state_modify_result (void *cls, int64_t result,
1287 const char *err_msg, uint16_t err_msg_size)
1289 struct StateModifyClosure *mcls = cls;
1290 struct Channel *chn = mcls->chn;
1291 uint64_t msg_id = mcls->msg_id;
1293 struct FragmentQueue *
1294 fragq = GNUNET_CONTAINER_multihashmap_get (chn->recv_frags, &mcls->msg_id_hash);
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 "%p GNUNET_PSYCSTORE_state_modify() returned %" PRId64 " (%.*s)\n",
1298 chn, result, err_msg_size, err_msg);
1305 fragq->state_is_modified = GNUNET_YES;
1306 if (chn->max_state_message_id < msg_id)
1307 chn->max_state_message_id = msg_id;
1308 if (chn->max_message_id < msg_id)
1309 chn->max_message_id = msg_id;
1312 fragment_queue_run (chn, msg_id, fragq, MSG_FRAG_STATE_DROP == fragq->state);
1313 GNUNET_CONTAINER_heap_remove_root (chn->recv_msgs);
1314 message_queue_run (chn);
1318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1319 "%p GNUNET_PSYCSTORE_state_modify() failed with error %" PRId64 " (%.*s)\n",
1320 chn, result, err_msg_size, err_msg);
1321 /** @todo FIXME: handle state_modify error */
1327 * Run message queue.
1329 * Send messages in queue to client in order after a message has arrived from
1330 * multicast, according to the following:
1331 * - A message is only sent if all of its modifiers arrived.
1332 * - A stateful message is only sent if the previous stateful message
1333 * has already been delivered to the client.
1335 * @param chn Channel.
1337 * @return Number of messages removed from queue and sent to client.
1340 message_queue_run (struct Channel *chn)
1342 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1343 "%p Running message queue.\n", chn);
1347 while (GNUNET_YES == GNUNET_CONTAINER_heap_peek2 (chn->recv_msgs, NULL,
1350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1351 "%p Processing message %" PRIu64 " in queue.\n", chn, msg_id);
1352 struct GNUNET_HashCode msg_id_hash;
1353 hash_key_from_hll (&msg_id_hash, msg_id);
1355 struct FragmentQueue *
1356 fragq = GNUNET_CONTAINER_multihashmap_get (chn->recv_frags, &msg_id_hash);
1358 if (NULL == fragq || fragq->state <= MSG_FRAG_STATE_HEADER)
1360 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1361 "%p No fragq (%p) or header not complete.\n",
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367 "%p Fragment queue entry: state: %u, state delta: "
1368 "%" PRIu64 " - %" PRIu64 " ?= %" PRIu64 "\n",
1369 chn, fragq->state, msg_id, fragq->state_delta, chn->max_state_message_id);
1371 if (MSG_FRAG_STATE_DATA <= fragq->state)
1373 /* Check if there's a missing message before the current one */
1374 if (GNUNET_PSYC_STATE_NOT_MODIFIED == fragq->state_delta)
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%p state NOT modified\n", chn);
1378 if (!(fragq->flags & GNUNET_PSYC_MESSAGE_ORDER_ANY)
1379 && (chn->max_message_id != msg_id - 1
1380 && chn->max_message_id != msg_id))
1382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1383 "%p Out of order message. "
1384 "(%" PRIu64 " != %" PRIu64 " - 1)\n",
1385 chn, chn->max_message_id, msg_id);
1387 // FIXME: keep track of messages processed in this queue run,
1388 // and only stop after reaching the end
1393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%p state modified\n", chn);
1394 if (GNUNET_YES != fragq->state_is_modified)
1396 if (msg_id - fragq->state_delta != chn->max_state_message_id)
1398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1399 "%p Out of order stateful message. "
1400 "(%" PRIu64 " - %" PRIu64 " != %" PRIu64 ")\n",
1401 chn, msg_id, fragq->state_delta, chn->max_state_message_id);
1403 // FIXME: keep track of messages processed in this queue run,
1404 // and only stop after reaching the end
1407 struct StateModifyClosure *mcls = GNUNET_malloc (sizeof (*mcls));
1409 mcls->msg_id = msg_id;
1410 mcls->msg_id_hash = msg_id_hash;
1412 /* Apply modifiers to state in PSYCstore */
1413 GNUNET_PSYCSTORE_state_modify (store, &chn->pub_key, msg_id,
1415 store_recv_state_modify_result, mcls);
1416 break; // continue after asynchronous state modify result
1419 chn->max_message_id = msg_id;
1421 fragment_queue_run (chn, msg_id, fragq, MSG_FRAG_STATE_DROP == fragq->state);
1422 GNUNET_CONTAINER_heap_remove_root (chn->recv_msgs);
1426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1427 "%p Removed %" PRIu64 " messages from queue.\n", chn, n);
1433 * Drop message queue of a channel.
1435 * Remove all messages in queue without sending it to clients.
1437 * @param chn Channel.
1439 * @return Number of messages removed from queue.
1442 message_queue_drop (struct Channel *chn)
1444 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1445 "%p Dropping message queue.\n", chn);
1448 while (GNUNET_YES == GNUNET_CONTAINER_heap_peek2 (chn->recv_msgs, NULL,
1451 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1452 "%p Dropping message %" PRIu64 " from queue.\n", chn, msg_id);
1453 struct GNUNET_HashCode msg_id_hash;
1454 hash_key_from_hll (&msg_id_hash, msg_id);
1456 struct FragmentQueue *
1457 fragq = GNUNET_CONTAINER_multihashmap_get (chn->recv_frags, &msg_id_hash);
1458 GNUNET_assert (NULL != fragq);
1459 fragment_queue_run (chn, msg_id, fragq, GNUNET_YES);
1460 GNUNET_CONTAINER_heap_remove_root (chn->recv_msgs);
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 "%p Removed %" PRIu64 " messages from queue.\n", chn, n);
1470 * Received result of GNUNET_PSYCSTORE_fragment_store().
1473 store_recv_fragment_store_result (void *cls, int64_t result,
1474 const char *err_msg, uint16_t err_msg_size)
1476 struct Channel *chn = cls;
1477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1478 "%p GNUNET_PSYCSTORE_fragment_store() returned %" PRId64 " (%.*s)\n",
1479 chn, result, err_msg_size, err_msg);
1484 * Handle incoming message fragment from multicast.
1486 * Store it using PSYCstore and send it to the clients of the channel in order.
1489 mcast_recv_message (void *cls, const struct GNUNET_MULTICAST_MessageHeader *mmsg)
1491 struct Channel *chn = cls;
1492 uint16_t size = ntohs (mmsg->header.size);
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1495 "%p Received multicast message of size %u. "
1496 "fragment_id=%" PRIu64 ", message_id=%" PRIu64
1497 ", fragment_offset=%" PRIu64 ", flags=%" PRIu64 "\n",
1499 GNUNET_ntohll (mmsg->fragment_id),
1500 GNUNET_ntohll (mmsg->message_id),
1501 GNUNET_ntohll (mmsg->fragment_offset),
1502 GNUNET_ntohll (mmsg->flags));
1504 GNUNET_PSYCSTORE_fragment_store (store, &chn->pub_key, mmsg, 0,
1505 &store_recv_fragment_store_result, chn);
1507 uint16_t first_ptype = 0, last_ptype = 0;
1508 int check = GNUNET_PSYC_receive_check_parts (size - sizeof (*mmsg),
1509 (const char *) &mmsg[1],
1510 &first_ptype, &last_ptype);
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "%p Message check result %d, first part type %u, last part type %u\n",
1513 chn, check, first_ptype, last_ptype);
1514 if (GNUNET_SYSERR == check)
1516 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1517 "%p Dropping incoming multicast message with invalid parts.\n",
1519 GNUNET_break_op (0);
1523 fragment_queue_insert (chn, mmsg, first_ptype, last_ptype);
1524 message_queue_run (chn);
1529 * Incoming request fragment from multicast for a master.
1531 * @param cls Master.
1532 * @param req The request.
1535 mcast_recv_request (void *cls,
1536 const struct GNUNET_MULTICAST_RequestHeader *req)
1538 struct Master *mst = cls;
1539 uint16_t size = ntohs (req->header.size);
1541 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->member_pub_key);
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "%p Received multicast request of size %u from %s.\n",
1547 uint16_t first_ptype = 0, last_ptype = 0;
1549 == GNUNET_PSYC_receive_check_parts (size - sizeof (*req),
1550 (const char *) &req[1],
1551 &first_ptype, &last_ptype))
1553 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1554 "%p Dropping incoming multicast request with invalid parts.\n",
1556 GNUNET_break_op (0);
1560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1561 "Message parts: first: type %u, last: type %u\n",
1562 first_ptype, last_ptype);
1564 /* FIXME: in-order delivery */
1565 client_send_mcast_req (mst, req);
1570 * Response from PSYCstore with the current counter values for a channel master.
1573 store_recv_master_counters (void *cls, int result, uint64_t max_fragment_id,
1574 uint64_t max_message_id, uint64_t max_group_generation,
1575 uint64_t max_state_message_id)
1577 struct Master *mst = cls;
1578 struct Channel *chn = &mst->chn;
1579 chn->store_op = NULL;
1581 struct GNUNET_PSYC_CountersResultMessage res;
1582 res.header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MASTER_START_ACK);
1583 res.header.size = htons (sizeof (res));
1584 res.result_code = htonl (result);
1585 res.max_message_id = GNUNET_htonll (max_message_id);
1587 if (GNUNET_OK == result || GNUNET_NO == result)
1589 mst->max_message_id = max_message_id;
1590 chn->max_message_id = max_message_id;
1591 chn->max_state_message_id = max_state_message_id;
1592 mst->max_group_generation = max_group_generation;
1594 = GNUNET_MULTICAST_origin_start (cfg, &mst->priv_key, max_fragment_id,
1595 mcast_recv_join_request,
1596 mcast_recv_replay_fragment,
1597 mcast_recv_replay_message,
1599 mcast_recv_message, chn);
1600 chn->is_ready = GNUNET_YES;
1604 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1605 "%p GNUNET_PSYCSTORE_counters_get() "
1606 "returned %d for channel %s.\n",
1607 chn, result, GNUNET_h2s (&chn->pub_key_hash));
1610 client_send_msg (chn, &res.header);
1615 * Response from PSYCstore with the current counter values for a channel slave.
1618 store_recv_slave_counters (void *cls, int result, uint64_t max_fragment_id,
1619 uint64_t max_message_id, uint64_t max_group_generation,
1620 uint64_t max_state_message_id)
1622 struct Slave *slv = cls;
1623 struct Channel *chn = &slv->chn;
1624 chn->store_op = NULL;
1626 struct GNUNET_PSYC_CountersResultMessage res;
1627 res.header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN_ACK);
1628 res.header.size = htons (sizeof (res));
1629 res.result_code = htonl (result);
1630 res.max_message_id = GNUNET_htonll (max_message_id);
1632 if (GNUNET_OK == result || GNUNET_NO == result)
1634 chn->max_message_id = max_message_id;
1635 chn->max_state_message_id = max_state_message_id;
1637 = GNUNET_MULTICAST_member_join (cfg, &chn->pub_key, &slv->priv_key,
1639 slv->relay_count, slv->relays,
1640 &slv->join_msg->header,
1641 mcast_recv_join_request,
1642 mcast_recv_join_decision,
1643 mcast_recv_replay_fragment,
1644 mcast_recv_replay_message,
1645 mcast_recv_message, chn);
1646 if (NULL != slv->join_msg)
1648 GNUNET_free (slv->join_msg);
1649 slv->join_msg = NULL;
1654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1655 "%p GNUNET_PSYCSTORE_counters_get() "
1656 "returned %d for channel %s.\n",
1657 chn, result, GNUNET_h2s (&chn->pub_key_hash));
1660 client_send_msg (chn, &res.header);
1665 channel_init (struct Channel *chn)
1668 = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1669 chn->recv_frags = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1674 * Handle a connecting client starting a channel master.
1677 client_recv_master_start (void *cls, struct GNUNET_SERVER_Client *client,
1678 const struct GNUNET_MessageHeader *msg)
1680 const struct MasterStartRequest *req
1681 = (const struct MasterStartRequest *) msg;
1683 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
1684 struct GNUNET_HashCode pub_key_hash;
1686 GNUNET_CRYPTO_eddsa_key_get_public (&req->channel_key, &pub_key);
1687 GNUNET_CRYPTO_hash (&pub_key, sizeof (pub_key), &pub_key_hash);
1690 mst = GNUNET_CONTAINER_multihashmap_get (masters, &pub_key_hash);
1691 struct Channel *chn;
1695 mst = GNUNET_new (struct Master);
1696 mst->policy = ntohl (req->policy);
1697 mst->priv_key = req->channel_key;
1698 mst->join_reqs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1701 chn->is_master = GNUNET_YES;
1702 chn->pub_key = pub_key;
1703 chn->pub_key_hash = pub_key_hash;
1706 GNUNET_CONTAINER_multihashmap_put (masters, &chn->pub_key_hash, chn,
1707 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1708 chn->store_op = GNUNET_PSYCSTORE_counters_get (store, &chn->pub_key,
1709 store_recv_master_counters, mst);
1715 struct GNUNET_PSYC_CountersResultMessage res;
1716 res.header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MASTER_START_ACK);
1717 res.header.size = htons (sizeof (res));
1718 res.result_code = htonl (GNUNET_OK);
1719 res.max_message_id = GNUNET_htonll (mst->max_message_id);
1721 GNUNET_SERVER_notification_context_add (nc, client);
1722 GNUNET_SERVER_notification_context_unicast (nc, client, &res.header,
1726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1727 "%p Client connected as master to channel %s.\n",
1728 mst, GNUNET_h2s (&chn->pub_key_hash));
1730 struct Client *cli = GNUNET_new (struct Client);
1731 cli->client = client;
1732 GNUNET_CONTAINER_DLL_insert (chn->clients_head, chn->clients_tail, cli);
1734 GNUNET_SERVER_client_set_user_context (client, chn);
1735 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1740 * Handle a connecting client joining as a channel slave.
1743 client_recv_slave_join (void *cls, struct GNUNET_SERVER_Client *client,
1744 const struct GNUNET_MessageHeader *msg)
1746 const struct SlaveJoinRequest *req
1747 = (const struct SlaveJoinRequest *) msg;
1748 uint16_t req_size = ntohs (req->header.size);
1750 struct GNUNET_CRYPTO_EcdsaPublicKey slv_pub_key;
1751 struct GNUNET_HashCode pub_key_hash, slv_pub_hash;
1753 GNUNET_CRYPTO_ecdsa_key_get_public (&req->slave_key, &slv_pub_key);
1754 GNUNET_CRYPTO_hash (&slv_pub_key, sizeof (slv_pub_key), &slv_pub_hash);
1755 GNUNET_CRYPTO_hash (&req->channel_pub_key, sizeof (req->channel_pub_key), &pub_key_hash);
1757 struct GNUNET_CONTAINER_MultiHashMap *
1758 chn_slv = GNUNET_CONTAINER_multihashmap_get (channel_slaves, &pub_key_hash);
1759 struct Slave *slv = NULL;
1760 struct Channel *chn;
1762 if (NULL != chn_slv)
1764 slv = GNUNET_CONTAINER_multihashmap_get (chn_slv, &slv_pub_hash);
1768 slv = GNUNET_new (struct Slave);
1769 slv->priv_key = req->slave_key;
1770 slv->pub_key = slv_pub_key;
1771 slv->pub_key_hash = slv_pub_hash;
1772 slv->origin = req->origin;
1773 slv->relay_count = ntohl (req->relay_count);
1774 slv->join_flags = ntohl (req->flags);
1776 const struct GNUNET_PeerIdentity *
1777 relays = (const struct GNUNET_PeerIdentity *) &req[1];
1778 uint16_t relay_size = slv->relay_count * sizeof (*relays);
1779 uint16_t join_msg_size = 0;
1781 if (sizeof (*req) + relay_size + sizeof (struct GNUNET_MessageHeader)
1784 struct GNUNET_PSYC_Message *
1785 join_msg = (struct GNUNET_PSYC_Message *) (((char *) &req[1]) + relay_size);
1786 join_msg_size = ntohs (join_msg->header.size);
1787 slv->join_msg = GNUNET_malloc (join_msg_size);
1788 memcpy (slv->join_msg, join_msg, join_msg_size);
1790 if (sizeof (*req) + relay_size + join_msg_size != req_size)
1792 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1793 "%u + %u + %u != %u\n",
1794 sizeof (*req), relay_size, join_msg_size, req_size);
1796 GNUNET_SERVER_client_disconnect (client);
1800 if (0 < slv->relay_count)
1802 slv->relays = GNUNET_malloc (relay_size);
1803 memcpy (slv->relays, &req[1], relay_size);
1807 chn->is_master = GNUNET_NO;
1808 chn->pub_key = req->channel_pub_key;
1809 chn->pub_key_hash = pub_key_hash;
1812 if (NULL == chn_slv)
1814 chn_slv = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1815 GNUNET_CONTAINER_multihashmap_put (channel_slaves, &chn->pub_key_hash, chn_slv,
1816 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1818 GNUNET_CONTAINER_multihashmap_put (chn_slv, &slv->pub_key_hash, chn,
1819 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1820 GNUNET_CONTAINER_multihashmap_put (slaves, &chn->pub_key_hash, chn,
1821 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1822 chn->store_op = GNUNET_PSYCSTORE_counters_get (store, &chn->pub_key,
1823 &store_recv_slave_counters, slv);
1829 struct GNUNET_PSYC_CountersResultMessage res;
1830 res.header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN_ACK);
1831 res.header.size = htons (sizeof (res));
1832 res.result_code = htonl (GNUNET_OK);
1833 res.max_message_id = GNUNET_htonll (chn->max_message_id);
1835 GNUNET_SERVER_notification_context_add (nc, client);
1836 GNUNET_SERVER_notification_context_unicast (nc, client, &res.header,
1839 if (GNUNET_PSYC_SLAVE_JOIN_LOCAL & slv->join_flags)
1841 mcast_recv_join_decision (slv, GNUNET_YES,
1842 NULL, 0, NULL, NULL);
1844 else if (NULL == slv->member)
1847 = GNUNET_MULTICAST_member_join (cfg, &chn->pub_key, &slv->priv_key,
1849 slv->relay_count, slv->relays,
1850 &slv->join_msg->header,
1851 &mcast_recv_join_request,
1852 &mcast_recv_join_decision,
1853 &mcast_recv_replay_fragment,
1854 &mcast_recv_replay_message,
1855 &mcast_recv_message, chn);
1856 if (NULL != slv->join_msg)
1858 GNUNET_free (slv->join_msg);
1859 slv->join_msg = NULL;
1862 else if (NULL != slv->join_dcsn)
1864 GNUNET_SERVER_notification_context_add (nc, client);
1865 GNUNET_SERVER_notification_context_unicast (nc, client,
1866 &slv->join_dcsn->header,
1871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1872 "%p Client connected as slave to channel %s.\n",
1873 slv, GNUNET_h2s (&chn->pub_key_hash));
1875 struct Client *cli = GNUNET_new (struct Client);
1876 cli->client = client;
1877 GNUNET_CONTAINER_DLL_insert (chn->clients_head, chn->clients_tail, cli);
1879 GNUNET_SERVER_client_set_user_context (client, chn);
1880 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1884 struct JoinDecisionClosure
1886 int32_t is_admitted;
1887 struct GNUNET_MessageHeader *msg;
1892 * Iterator callback for sending join decisions to multicast.
1895 mcast_send_join_decision (void *cls, const struct GNUNET_HashCode *pub_key_hash,
1898 struct JoinDecisionClosure *jcls = cls;
1899 struct GNUNET_MULTICAST_JoinHandle *jh = value;
1900 // FIXME: add relays
1901 GNUNET_MULTICAST_join_decision (jh, jcls->is_admitted, 0, NULL, jcls->msg);
1907 * Join decision from client.
1910 client_recv_join_decision (void *cls, struct GNUNET_SERVER_Client *client,
1911 const struct GNUNET_MessageHeader *msg)
1913 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn
1914 = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
1915 struct Channel *chn;
1917 struct JoinDecisionClosure jcls;
1919 chn = GNUNET_SERVER_client_get_user_context (client, struct Channel);
1923 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1926 GNUNET_assert (GNUNET_YES == chn->is_master);
1927 mst = (struct Master *) chn;
1928 jcls.is_admitted = ntohl (dcsn->is_admitted);
1930 = (sizeof (*dcsn) + sizeof (*jcls.msg) <= ntohs (msg->size))
1931 ? (struct GNUNET_MessageHeader *) &dcsn[1]
1934 struct GNUNET_HashCode slave_pub_hash;
1935 GNUNET_CRYPTO_hash (&dcsn->slave_pub_key, sizeof (dcsn->slave_pub_key),
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939 "%p Got join decision (%d) from client for channel %s..\n",
1940 mst, jcls.is_admitted, GNUNET_h2s (&chn->pub_key_hash));
1941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1942 "%p ..and slave %s.\n",
1943 mst, GNUNET_h2s (&slave_pub_hash));
1945 GNUNET_CONTAINER_multihashmap_get_multiple (mst->join_reqs, &slave_pub_hash,
1946 &mcast_send_join_decision, &jcls);
1947 GNUNET_CONTAINER_multihashmap_remove_all (mst->join_reqs, &slave_pub_hash);
1948 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1953 * Send acknowledgement to a client.
1955 * Sent after a message fragment has been passed on to multicast.
1957 * @param chn The channel struct for the client.
1960 send_message_ack (struct Channel *chn, struct GNUNET_SERVER_Client *client)
1962 struct GNUNET_MessageHeader res;
1963 res.size = htons (sizeof (res));
1964 res.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK);
1967 GNUNET_SERVER_notification_context_add (nc, client);
1968 GNUNET_SERVER_notification_context_unicast (nc, client, &res, GNUNET_NO);
1973 * Callback for the transmit functions of multicast.
1976 transmit_notify (void *cls, size_t *data_size, void *data)
1978 struct Channel *chn = cls;
1979 struct TransmitMessage *tmit_msg = chn->tmit_head;
1981 if (NULL == tmit_msg || *data_size < tmit_msg->size)
1983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1984 "%p transmit_notify: nothing to send.\n", chn);
1985 if (NULL != tmit_msg && *data_size < tmit_msg->size)
1991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1992 "%p transmit_notify: sending %u bytes.\n", chn, tmit_msg->size);
1994 *data_size = tmit_msg->size;
1995 memcpy (data, &tmit_msg[1], *data_size);
1998 = (tmit_msg->last_ptype < GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END)
2002 /* FIXME: handle disconnecting clients */
2003 if (NULL != tmit_msg->client)
2004 send_message_ack (chn, tmit_msg->client);
2006 GNUNET_CONTAINER_DLL_remove (chn->tmit_head, chn->tmit_tail, tmit_msg);
2007 GNUNET_free (tmit_msg);
2009 if (NULL != chn->tmit_head)
2011 GNUNET_SCHEDULER_add_now (schedule_transmit_message, chn);
2013 else if (GNUNET_YES == chn->is_disconnected
2014 && tmit_msg->last_ptype < GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END)
2016 /* FIXME: handle partial message (when still in_transmit) */
2017 return GNUNET_SYSERR;
2024 * Callback for the transmit functions of multicast.
2027 master_transmit_notify (void *cls, size_t *data_size, void *data)
2029 int ret = transmit_notify (cls, data_size, data);
2031 if (GNUNET_YES == ret)
2033 struct Master *mst = cls;
2034 mst->tmit_handle = NULL;
2041 * Callback for the transmit functions of multicast.
2044 slave_transmit_notify (void *cls, size_t *data_size, void *data)
2046 int ret = transmit_notify (cls, data_size, data);
2048 if (GNUNET_YES == ret)
2050 struct Slave *slv = cls;
2051 slv->tmit_handle = NULL;
2058 * Transmit a message from a channel master to the multicast group.
2061 master_transmit_message (struct Master *mst)
2063 struct Channel *chn = &mst->chn;
2064 struct TransmitMessage *tmit_msg = chn->tmit_head;
2065 if (NULL == tmit_msg)
2067 if (NULL == mst->tmit_handle)
2070 = GNUNET_MULTICAST_origin_to_all (mst->origin, tmit_msg->id,
2071 mst->max_group_generation,
2072 master_transmit_notify, mst);
2076 GNUNET_MULTICAST_origin_to_all_resume (mst->tmit_handle);
2082 * Transmit a message from a channel slave to the multicast group.
2085 slave_transmit_message (struct Slave *slv)
2087 if (NULL == slv->chn.tmit_head)
2089 if (NULL == slv->tmit_handle)
2092 = GNUNET_MULTICAST_member_to_origin (slv->member, slv->chn.tmit_head->id,
2093 slave_transmit_notify, slv);
2097 GNUNET_MULTICAST_member_to_origin_resume (slv->tmit_handle);
2103 transmit_message (struct Channel *chn)
2106 ? master_transmit_message ((struct Master *) chn)
2107 : slave_transmit_message ((struct Slave *) chn);
2112 * Queue a message from a channel master for sending to the multicast group.
2115 master_queue_message (struct Master *mst, struct TransmitMessage *tmit_msg)
2117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%p master_queue_message()\n", mst);
2119 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == tmit_msg->first_ptype)
2121 tmit_msg->id = ++mst->max_message_id;
2122 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2123 "%p master_queue_message: message_id=%" PRIu64 "\n",
2125 struct GNUNET_PSYC_MessageMethod *pmeth
2126 = (struct GNUNET_PSYC_MessageMethod *) &tmit_msg[1];
2128 if (pmeth->flags & GNUNET_PSYC_MASTER_TRANSMIT_STATE_RESET)
2130 pmeth->state_delta = GNUNET_htonll (GNUNET_PSYC_STATE_RESET);
2132 else if (pmeth->flags & GNUNET_PSYC_MASTER_TRANSMIT_STATE_MODIFY)
2134 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2135 "%p master_queue_message: state_delta=%" PRIu64 "\n",
2136 mst, tmit_msg->id - mst->max_state_message_id);
2137 pmeth->state_delta = GNUNET_htonll (tmit_msg->id
2138 - mst->max_state_message_id);
2139 mst->max_state_message_id = tmit_msg->id;
2143 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2144 "%p master_queue_message: state not modified\n", mst);
2145 pmeth->state_delta = GNUNET_htonll (GNUNET_PSYC_STATE_NOT_MODIFIED);
2148 if (pmeth->flags & GNUNET_PSYC_MASTER_TRANSMIT_STATE_HASH)
2150 /// @todo add state_hash to PSYC header
2157 * Queue a message from a channel slave for sending to the multicast group.
2160 slave_queue_message (struct Slave *slv, struct TransmitMessage *tmit_msg)
2162 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == tmit_msg->first_ptype)
2164 struct GNUNET_PSYC_MessageMethod *pmeth
2165 = (struct GNUNET_PSYC_MessageMethod *) &tmit_msg[1];
2166 pmeth->state_delta = GNUNET_htonll (GNUNET_PSYC_STATE_NOT_MODIFIED);
2167 tmit_msg->id = ++slv->max_request_id;
2173 * Queue PSYC message parts for sending to multicast.
2176 * Channel to send to.
2178 * Client the message originates from.
2182 * Concatenated message parts.
2183 * @param first_ptype
2184 * First message part type in @a data.
2186 * Last message part type in @a data.
2188 static struct TransmitMessage *
2189 queue_message (struct Channel *chn,
2190 struct GNUNET_SERVER_Client *client,
2193 uint16_t first_ptype, uint16_t last_ptype)
2195 struct TransmitMessage *
2196 tmit_msg = GNUNET_malloc (sizeof (*tmit_msg) + data_size);
2197 memcpy (&tmit_msg[1], data, data_size);
2198 tmit_msg->client = client;
2199 tmit_msg->size = data_size;
2200 tmit_msg->first_ptype = first_ptype;
2201 tmit_msg->last_ptype = last_ptype;
2203 /* FIXME: separate queue per message ID */
2205 GNUNET_CONTAINER_DLL_insert_tail (chn->tmit_head, chn->tmit_tail, tmit_msg);
2208 ? master_queue_message ((struct Master *) chn, tmit_msg)
2209 : slave_queue_message ((struct Slave *) chn, tmit_msg);
2215 * Cancel transmission of current message.
2217 * @param chn Channel to send to.
2218 * @param client Client the message originates from.
2221 transmit_cancel (struct Channel *chn, struct GNUNET_SERVER_Client *client)
2223 uint16_t type = GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL;
2225 struct GNUNET_MessageHeader msg;
2226 msg.size = htons (sizeof (msg));
2227 msg.type = htons (type);
2229 queue_message (chn, client, sizeof (msg), &msg, type, type);
2230 transmit_message (chn);
2232 /* FIXME: cleanup */
2237 * Incoming message from a master or slave client.
2240 client_recv_psyc_message (void *cls, struct GNUNET_SERVER_Client *client,
2241 const struct GNUNET_MessageHeader *msg)
2244 chn = GNUNET_SERVER_client_get_user_context (client, struct Channel);
2245 GNUNET_assert (NULL != chn);
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2248 "%p Received message from client.\n", chn);
2249 GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, msg);
2251 if (GNUNET_YES != chn->is_ready)
2253 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2254 "%p Channel is not ready yet, disconnecting client.\n", chn);
2256 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2260 uint16_t size = ntohs (msg->size);
2261 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < size - sizeof (*msg))
2263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2264 "%p Message payload too large: %u < %u.\n",
2265 chn, GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD, size - sizeof (*msg));
2267 transmit_cancel (chn, client);
2268 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2272 uint16_t first_ptype = 0, last_ptype = 0;
2274 == GNUNET_PSYC_receive_check_parts (size - sizeof (*msg),
2275 (const char *) &msg[1],
2276 &first_ptype, &last_ptype))
2278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2279 "%p Received invalid message part from client.\n", chn);
2281 transmit_cancel (chn, client);
2282 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2286 "%p Received message with first part type %u and last part type %u.\n",
2287 chn, first_ptype, last_ptype);
2289 queue_message (chn, client, size - sizeof (*msg), &msg[1],
2290 first_ptype, last_ptype);
2291 transmit_message (chn);
2292 /* FIXME: send a few ACKs even before transmit_notify is called */
2294 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2299 * Received result of GNUNET_PSYCSTORE_membership_store()
2302 store_recv_membership_store_result (void *cls, int64_t result,
2303 const char *err_msg, uint16_t err_msg_size)
2305 struct Operation *op = cls;
2306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2307 "%p GNUNET_PSYCSTORE_membership_store() returned %" PRId64 " (%.s)\n",
2308 op->chn, result, err_msg_size, err_msg);
2310 if (NULL != op->client)
2311 client_send_result (op->client, op->op_id, result, err_msg, err_msg_size);
2317 * Client requests to add/remove a slave in the membership database.
2320 client_recv_membership_store (void *cls, struct GNUNET_SERVER_Client *client,
2321 const struct GNUNET_MessageHeader *msg)
2324 chn = GNUNET_SERVER_client_get_user_context (client, struct Channel);
2325 GNUNET_assert (NULL != chn);
2327 const struct ChannelMembershipStoreRequest *
2328 req = (const struct ChannelMembershipStoreRequest *) msg;
2330 struct Operation *op = op_add (chn, client, req->op_id, 0);
2332 uint64_t announced_at = GNUNET_ntohll (req->announced_at);
2333 uint64_t effective_since = GNUNET_ntohll (req->effective_since);
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2335 "%p Received membership store request from client.\n", chn);
2336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2337 "%p did_join: %u, announced_at: %" PRIu64 ", effective_since: %" PRIu64 "\n",
2338 chn, req->did_join, announced_at, effective_since);
2340 GNUNET_PSYCSTORE_membership_store (store, &chn->pub_key, &req->slave_pub_key,
2341 req->did_join, announced_at, effective_since,
2342 0, /* FIXME: group_generation */
2343 &store_recv_membership_store_result, op);
2344 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2349 * Received a fragment for GNUNET_PSYCSTORE_fragment_get(),
2350 * in response to a history request from a client.
2353 store_recv_fragment_history (void *cls,
2354 struct GNUNET_MULTICAST_MessageHeader *mmsg,
2355 enum GNUNET_PSYCSTORE_MessageFlags flags)
2357 struct Operation *op = cls;
2358 if (NULL == op->client)
2359 { /* Requesting client already disconnected. */
2362 struct Channel *chn = op->chn;
2364 struct GNUNET_PSYC_MessageHeader *pmsg;
2365 uint16_t msize = ntohs (mmsg->header.size);
2366 uint16_t psize = sizeof (*pmsg) + msize - sizeof (*mmsg);
2368 struct GNUNET_OperationResultMessage *
2369 res = GNUNET_malloc (sizeof (*res) + psize);
2370 res->header.size = htons (sizeof (*res) + psize);
2371 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT);
2372 res->op_id = op->op_id;
2373 res->result_code = GNUNET_htonll (GNUNET_OK);
2375 pmsg = (struct GNUNET_PSYC_MessageHeader *) &res[1];
2376 GNUNET_PSYC_message_header_init (pmsg, mmsg, flags | GNUNET_PSYC_MESSAGE_HISTORIC);
2377 memcpy (&res[1], pmsg, psize);
2379 /** @todo FIXME: send only to requesting client */
2380 client_send_msg (chn, &res->header);
2386 * Received the result of GNUNET_PSYCSTORE_fragment_get(),
2387 * in response to a history request from a client.
2390 store_recv_fragment_history_result (void *cls, int64_t result,
2391 const char *err_msg, uint16_t err_msg_size)
2393 struct Operation *op = cls;
2394 if (NULL == op->client)
2395 { /* Requesting client already disconnected. */
2399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2400 "%p History replay #%" PRIu64 ": "
2401 "PSYCSTORE returned %" PRId64 " (%.*s)\n",
2402 op->chn, GNUNET_ntohll (op->op_id), result, err_msg_size, err_msg);
2404 if (op->flags & GNUNET_PSYC_HISTORY_REPLAY_REMOTE)
2406 /** @todo Multicast replay request for messages not found locally. */
2409 client_send_result (op->client, op->op_id, result, err_msg, err_msg_size);
2415 * Client requests channel history.
2418 client_recv_history_replay (void *cls, struct GNUNET_SERVER_Client *client,
2419 const struct GNUNET_MessageHeader *msg)
2422 chn = GNUNET_SERVER_client_get_user_context (client, struct Channel);
2423 GNUNET_assert (NULL != chn);
2425 const struct GNUNET_PSYC_HistoryRequestMessage *
2426 req = (const struct GNUNET_PSYC_HistoryRequestMessage *) msg;
2427 uint16_t size = ntohs (msg->size);
2428 const char *method_prefix = (const char *) &req[1];
2430 if (size < sizeof (*req) + 1
2431 || '\0' != method_prefix[size - sizeof (*req) - 1])
2433 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2434 "%p History replay #%" PRIu64 ": "
2435 "invalid method prefix. size: %u < %u?\n",
2436 chn, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
2438 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2442 struct Operation *op = op_add (chn, client, req->op_id, ntohl (req->flags));
2444 if (0 == req->message_limit)
2445 GNUNET_PSYCSTORE_message_get (store, &chn->pub_key, NULL,
2446 GNUNET_ntohll (req->start_message_id),
2447 GNUNET_ntohll (req->end_message_id),
2449 &store_recv_fragment_history,
2450 &store_recv_fragment_history_result, op);
2452 GNUNET_PSYCSTORE_message_get_latest (store, &chn->pub_key, NULL,
2453 GNUNET_ntohll (req->message_limit),
2455 &store_recv_fragment_history,
2456 &store_recv_fragment_history_result,
2459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2464 * Received state var from PSYCstore, send it to client.
2467 store_recv_state_var (void *cls, const char *name,
2468 const void *value, uint32_t value_size)
2470 struct Operation *op = cls;
2471 struct GNUNET_OperationResultMessage *res;
2473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2474 "%p state_get #%" PRIu64 " - received var from PSYCstore: %s\n",
2475 op->chn, GNUNET_ntohll (op->op_id), name);
2477 if (NULL != name) /* First part */
2479 uint16_t name_size = strnlen (name, GNUNET_PSYC_MODIFIER_MAX_PAYLOAD) + 1;
2480 struct GNUNET_PSYC_MessageModifier *mod;
2481 res = GNUNET_malloc (sizeof (*res) + sizeof (*mod) + name_size + value_size);
2482 res->header.size = htons (sizeof (*res) + sizeof (*mod) + name_size + value_size);
2483 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT);
2484 res->op_id = op->op_id;
2486 mod = (struct GNUNET_PSYC_MessageModifier *) &res[1];
2487 mod->header.size = htons (sizeof (*mod) + name_size + value_size);
2488 mod->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER);
2489 mod->name_size = htons (name_size);
2490 mod->value_size = htonl (value_size);
2491 mod->oper = htons (GNUNET_PSYC_OP_ASSIGN);
2492 memcpy (&mod[1], name, name_size);
2493 memcpy (((char *) &mod[1]) + name_size, value, value_size);
2495 else /* Continuation */
2497 struct GNUNET_MessageHeader *mod;
2498 res = GNUNET_malloc (sizeof (*res) + sizeof (*mod) + value_size);
2499 res->header.size = htons (sizeof (*res) + sizeof (*mod) + value_size);
2500 res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT);
2501 res->op_id = op->op_id;
2503 mod = (struct GNUNET_MessageHeader *) &res[1];
2504 mod->size = htons (sizeof (*mod) + value_size);
2505 mod->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT);
2506 memcpy (&mod[1], value, value_size);
2509 // FIXME: client might have been disconnected
2510 GNUNET_SERVER_notification_context_add (nc, op->client);
2511 GNUNET_SERVER_notification_context_unicast (nc, op->client, &res->header,
2518 * Received result of GNUNET_PSYCSTORE_state_get()
2519 * or GNUNET_PSYCSTORE_state_get_prefix()
2522 store_recv_state_result (void *cls, int64_t result,
2523 const char *err_msg, uint16_t err_msg_size)
2525 struct Operation *op = cls;
2526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2527 "%p state_get #%" PRIu64 ": "
2528 "PSYCSTORE returned %" PRId64 " (%.*s)\n",
2529 op->chn, GNUNET_ntohll (op->op_id), result, err_msg_size, err_msg);
2531 // FIXME: client might have been disconnected
2532 client_send_result (op->client, op->op_id, result, err_msg, err_msg_size);
2538 * Client requests best matching state variable from PSYCstore.
2541 client_recv_state_get (void *cls, struct GNUNET_SERVER_Client *client,
2542 const struct GNUNET_MessageHeader *msg)
2545 chn = GNUNET_SERVER_client_get_user_context (client, struct Channel);
2546 GNUNET_assert (NULL != chn);
2548 const struct StateRequest *
2549 req = (const struct StateRequest *) msg;
2551 uint16_t name_size = ntohs (req->header.size) - sizeof (*req);
2552 const char *name = (const char *) &req[1];
2553 if (0 == name_size || '\0' != name[name_size - 1])
2556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2560 struct Operation *op = op_add (chn, client, req->op_id, 0);
2561 GNUNET_PSYCSTORE_state_get (store, &chn->pub_key, name,
2562 &store_recv_state_var,
2563 &store_recv_state_result, op);
2564 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2569 * Client requests state variables with a given prefix from PSYCstore.
2572 client_recv_state_get_prefix (void *cls, struct GNUNET_SERVER_Client *client,
2573 const struct GNUNET_MessageHeader *msg)
2576 chn = GNUNET_SERVER_client_get_user_context (client, struct Channel);
2577 GNUNET_assert (NULL != chn);
2579 const struct StateRequest *
2580 req = (const struct StateRequest *) msg;
2582 uint16_t name_size = ntohs (req->header.size) - sizeof (*req);
2583 const char *name = (const char *) &req[1];
2584 if (0 == name_size || '\0' != name[name_size - 1])
2587 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2591 struct Operation *op = op_add (chn, client, req->op_id, 0);
2592 GNUNET_PSYCSTORE_state_get_prefix (store, &chn->pub_key, name,
2593 &store_recv_state_var,
2594 &store_recv_state_result, op);
2595 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2599 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
2600 { &client_recv_master_start, NULL,
2601 GNUNET_MESSAGE_TYPE_PSYC_MASTER_START, 0 },
2603 { &client_recv_slave_join, NULL,
2604 GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN, 0 },
2606 { &client_recv_join_decision, NULL,
2607 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION, 0 },
2609 { &client_recv_psyc_message, NULL,
2610 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE, 0 },
2612 { &client_recv_membership_store, NULL,
2613 GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_MEMBERSHIP_STORE, 0 },
2615 { &client_recv_history_replay, NULL,
2616 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY, 0 },
2618 { &client_recv_state_get, NULL,
2619 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET, 0 },
2621 { &client_recv_state_get_prefix, NULL,
2622 GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX, 0 },
2624 { NULL, NULL, 0, 0 }
2629 * Initialize the PSYC service.
2631 * @param cls Closure.
2632 * @param server The initialized server.
2633 * @param c Configuration to use.
2636 run (void *cls, struct GNUNET_SERVER_Handle *server,
2637 const struct GNUNET_CONFIGURATION_Handle *c)
2640 store = GNUNET_PSYCSTORE_connect (cfg);
2641 stats = GNUNET_STATISTICS_create ("psyc", cfg);
2642 masters = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
2643 slaves = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
2644 channel_slaves = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2645 recv_cache = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2646 nc = GNUNET_SERVER_notification_context_create (server, 1);
2647 GNUNET_SERVER_add_handlers (server, server_handlers);
2648 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
2649 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2650 &shutdown_task, NULL);
2655 * The main function for the service.
2657 * @param argc number of arguments from the command line
2658 * @param argv command line arguments
2659 * @return 0 ok, 1 on error
2662 main (int argc, char *const *argv)
2664 return (GNUNET_OK ==
2665 GNUNET_SERVICE_run (argc, argv, "psyc",
2666 GNUNET_SERVICE_OPTION_NONE,
2667 &run, NULL)) ? 0 : 1;
2670 /* end of gnunet-service-psyc.c */