2 This file is part of GNUnet.
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file multicast/multicast_api.c
23 * @brief multicast service; establish tunnels to distant peers
24 * @author Christian Grothoff
25 * @author Gabor X Toth
28 #include "gnunet_util_lib.h"
29 #include "gnunet_multicast_service.h"
30 #include "multicast.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "multicast-api",__VA_ARGS__)
37 * Group's pub_key_hash -> struct GNUNET_MULTICAST_Origin
39 static struct GNUNET_CONTAINER_MultiHashMap *origins;
43 * group_key_hash -> struct GNUNET_MULTICAST_Member
45 static struct GNUNET_CONTAINER_MultiHashMap *members;
50 struct MessageQueue *prev;
51 struct MessageQueue *next;
56 * Handle for a request to send a message to all multicast group members
59 struct GNUNET_MULTICAST_OriginTransmitHandle
61 GNUNET_MULTICAST_OriginTransmitNotify notify;
63 struct GNUNET_MULTICAST_Origin *origin;
66 uint64_t group_generation;
67 uint64_t fragment_offset;
72 * Handle for a message to be delivered from a member to the origin.
74 struct GNUNET_MULTICAST_MemberTransmitHandle
76 GNUNET_MULTICAST_MemberTransmitNotify notify;
78 struct GNUNET_MULTICAST_Member *member;
81 uint64_t fragment_offset;
85 struct GNUNET_MULTICAST_Group
88 * Configuration to use.
90 const struct GNUNET_CONFIGURATION_Handle *cfg;
93 * Socket (if available).
95 struct GNUNET_CLIENT_Connection *client;
98 * Currently pending transmission request, or NULL for none.
100 struct GNUNET_CLIENT_TransmitHandle *th;
103 * Head of operations to transmit.
105 struct MessageQueue *tmit_head;
108 * Tail of operations to transmit.
110 struct MessageQueue *tmit_tail;
113 * Message being transmitted to the Multicast service.
115 struct MessageQueue *tmit_msg;
118 * Message to send on reconnect.
120 struct GNUNET_MessageHeader *reconnect_msg;
123 * Task doing exponential back-off trying to reconnect.
125 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
128 * Time for next connect retry.
130 struct GNUNET_TIME_Relative reconnect_delay;
132 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
133 struct GNUNET_HashCode pub_key_hash;
135 GNUNET_MULTICAST_JoinCallback join_cb;
136 GNUNET_MULTICAST_MembershipTestCallback member_test_cb;
137 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb;
138 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb;
139 GNUNET_MULTICAST_MessageCallback message_cb;
143 * Are we polling for incoming messages right now?
148 * Are we currently transmitting a message?
153 * Is this the origin or a member?
160 * Handle for the origin of a multicast group.
162 struct GNUNET_MULTICAST_Origin
164 struct GNUNET_MULTICAST_Group grp;
165 struct GNUNET_MULTICAST_OriginTransmitHandle tmit;
166 struct GNUNET_CRYPTO_EddsaPrivateKey priv_key;
168 GNUNET_MULTICAST_RequestCallback request_cb;
173 * Handle for a multicast group member.
175 struct GNUNET_MULTICAST_Member
177 struct GNUNET_MULTICAST_Group grp;
178 struct GNUNET_MULTICAST_MemberTransmitHandle tmit;
180 struct GNUNET_CRYPTO_EddsaPrivateKey priv_key;
181 struct GNUNET_PeerIdentity origin;
182 struct GNUNET_PeerIdentity relays;
183 uint32_t relay_count;
185 uint64_t next_fragment_id;
190 * Handle that identifies a join request.
192 * Used to match calls to #GNUNET_MULTICAST_JoinCallback to the
193 * corresponding calls to #GNUNET_MULTICAST_join_decision().
195 struct GNUNET_MULTICAST_JoinHandle
197 struct GNUNET_MULTICAST_Group *group;
200 * Public key of the joining member.
202 struct GNUNET_CRYPTO_EddsaPublicKey member_key;
205 * Peer identity of the joining member.
207 struct GNUNET_PeerIdentity member_peer;
212 * Handle to pass back for the answer of a membership test.
214 struct GNUNET_MULTICAST_MembershipTestHandle
220 * Opaque handle to a replay request from the multicast service.
222 struct GNUNET_MULTICAST_ReplayHandle
228 * Handle for a replay request.
230 struct GNUNET_MULTICAST_MemberReplayHandle
236 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
240 reschedule_connect (struct GNUNET_MULTICAST_Group *grp);
244 * Schedule transmission of the next message from our queue.
246 * @param grp PSYC channel handle
249 transmit_next (struct GNUNET_MULTICAST_Group *grp);
253 message_handler (void *cls, const struct GNUNET_MessageHeader *msg);
257 * Reschedule a connect attempt to the service.
259 * @param c channel to reconnect
262 reschedule_connect (struct GNUNET_MULTICAST_Group *grp)
264 GNUNET_assert (grp->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
268 GNUNET_CLIENT_notify_transmit_ready_cancel (grp->th);
271 if (NULL != grp->client)
273 GNUNET_CLIENT_disconnect (grp->client);
276 grp->in_receive = GNUNET_NO;
277 LOG (GNUNET_ERROR_TYPE_DEBUG,
278 "Scheduling task to reconnect to Multicast service in %s.\n",
279 GNUNET_STRINGS_relative_time_to_string (grp->reconnect_delay, GNUNET_YES));
280 grp->reconnect_task =
281 GNUNET_SCHEDULER_add_delayed (grp->reconnect_delay, &reconnect, grp);
282 grp->reconnect_delay = GNUNET_TIME_STD_BACKOFF (grp->reconnect_delay);
287 * Reset stored data related to the last received message.
290 recv_reset (struct GNUNET_MULTICAST_Group *grp)
296 recv_error (struct GNUNET_MULTICAST_Group *grp)
298 if (NULL != grp->message_cb)
299 grp->message_cb (grp->cb_cls, NULL);
306 * Transmit next message to service.
308 * @param cls The struct GNUNET_MULTICAST_Group.
309 * @param size Number of bytes available in @a buf.
310 * @param buf Where to copy the message.
312 * @return Number of bytes copied to @a buf.
315 send_next_message (void *cls, size_t size, void *buf)
317 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_next_message()\n");
318 struct GNUNET_MULTICAST_Group *grp = cls;
319 struct MessageQueue *mq = grp->tmit_head;
322 struct GNUNET_MessageHeader *qmsg = (struct GNUNET_MessageHeader *) &mq[1];
323 size_t ret = ntohs (qmsg->size);
327 reschedule_connect (grp);
330 memcpy (buf, qmsg, ret);
332 GNUNET_CONTAINER_DLL_remove (grp->tmit_head, grp->tmit_tail, mq);
335 if (NULL != grp->tmit_head)
338 if (GNUNET_NO == grp->in_receive)
340 grp->in_receive = GNUNET_YES;
341 GNUNET_CLIENT_receive (grp->client, &message_handler, grp,
342 GNUNET_TIME_UNIT_FOREVER_REL);
349 * Schedule transmission of the next message from our queue.
351 * @param grp Multicast group handle.
354 transmit_next (struct GNUNET_MULTICAST_Group *grp)
356 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_next()\n");
357 if (NULL != grp->th || NULL == grp->client)
360 struct MessageQueue *mq = grp->tmit_head;
363 struct GNUNET_MessageHeader *qmsg = (struct GNUNET_MessageHeader *) &mq[1];
365 grp->th = GNUNET_CLIENT_notify_transmit_ready (grp->client,
367 GNUNET_TIME_UNIT_FOREVER_REL,
375 * Try again to connect to the Multicast service.
377 * @param cls Channel handle.
378 * @param tc Scheduler context.
381 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
383 struct GNUNET_MULTICAST_Group *grp = cls;
386 grp->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
387 LOG (GNUNET_ERROR_TYPE_DEBUG,
388 "Connecting to Multicast service.\n");
389 GNUNET_assert (NULL == grp->client);
390 grp->client = GNUNET_CLIENT_connect ("multicast", grp->cfg);
391 GNUNET_assert (NULL != grp->client);
392 uint16_t reconn_size = ntohs (grp->reconnect_msg->size);
394 if (NULL == grp->tmit_head ||
395 0 != memcmp (&grp->tmit_head[1], grp->reconnect_msg, reconn_size))
397 struct MessageQueue *mq = GNUNET_malloc (sizeof (*mq) + reconn_size);
398 memcpy (&mq[1], grp->reconnect_msg, reconn_size);
399 GNUNET_CONTAINER_DLL_insert (grp->tmit_head, grp->tmit_tail, mq);
406 * Disconnect from the Multicast service.
408 * @param g Group handle to disconnect.
413 struct GNUNET_MULTICAST_Group *grp = g;
415 GNUNET_assert (NULL != grp);
416 if (grp->tmit_head != grp->tmit_tail)
418 LOG (GNUNET_ERROR_TYPE_ERROR,
419 "Disconnecting while there are still outstanding messages!\n");
422 if (grp->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
424 GNUNET_SCHEDULER_cancel (grp->reconnect_task);
425 grp->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
429 GNUNET_CLIENT_notify_transmit_ready_cancel (grp->th);
432 if (NULL != grp->client)
434 GNUNET_CLIENT_disconnect (grp->client);
437 if (NULL != grp->reconnect_msg)
439 GNUNET_free (grp->reconnect_msg);
440 grp->reconnect_msg = NULL;
446 * Iterator callback for calling message callbacks for all groups.
449 message_cb (void *cls, const struct GNUNET_HashCode *pub_key_hash, void *group)
451 const struct GNUNET_MessageHeader *msg = cls;
452 struct GNUNET_MULTICAST_Group *grp = group;
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Calling message callback with a message "
456 "of type %u and size %u.\n",
457 ntohs (msg->type), ntohs (msg->size));
459 if (NULL != grp->message_cb)
460 grp->message_cb (grp->cb_cls, msg);
467 * Iterator callback for calling request callbacks of origins.
470 request_cb (void *cls, const struct GNUNET_HashCode *pub_key_hash, void *origin)
472 const struct GNUNET_MULTICAST_RequestHeader *req = cls;
473 struct GNUNET_MULTICAST_Origin *orig = origin;
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Calling request callback for a request of type %u and size %u.\n",
477 ntohs (req->header.type), ntohs (req->header.size));
479 orig->request_cb (orig->grp.cb_cls, &req->member_key,
480 (const struct GNUNET_MessageHeader *) req, 0);
486 * Iterator callback for calling join request callbacks of origins.
489 join_request_cb (void *cls, const struct GNUNET_HashCode *pub_key_hash,
492 const struct MulticastJoinRequestMessage *req = cls;
493 struct GNUNET_MULTICAST_Group *grp = group;
495 struct GNUNET_MULTICAST_JoinHandle *jh = GNUNET_malloc (sizeof (*jh));
497 jh->member_key = req->member_key;
498 jh->member_peer = req->member_peer;
500 const struct GNUNET_MessageHeader *msg = NULL;
501 if (sizeof (*req) + sizeof (*msg) <= ntohs (req->header.size))
502 msg = (const struct GNUNET_MessageHeader *) &req[1];
504 grp->join_cb (grp->cb_cls, &req->member_key, msg, jh);
510 * Iterator callback for calling join decision callbacks of members.
513 join_decision_cb (void *cls, const struct GNUNET_HashCode *pub_key_hash,
516 const struct MulticastJoinDecisionMessage *dcsn = cls;
517 struct GNUNET_MULTICAST_Member *mem = member;
518 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
520 const struct GNUNET_MessageHeader *msg = NULL;
521 if (sizeof (*dcsn) + sizeof (*msg) <= ntohs (dcsn->header.size))
522 msg = (const struct GNUNET_MessageHeader *) &dcsn[1];
524 // FIXME: grp->join_decision_cb (grp->cb_cls, msg);
529 * Function called when we receive a message from the service.
531 * @param cls struct GNUNET_MULTICAST_Group
532 * @param msg Message received, NULL on timeout or fatal error.
535 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
537 struct GNUNET_MULTICAST_Group *grp = cls;
541 // timeout / disconnected from service, reconnect
542 reschedule_connect (grp);
546 uint16_t size_eq = 0;
547 uint16_t size_min = 0;
548 uint16_t size = ntohs (msg->size);
549 uint16_t type = ntohs (msg->type);
551 LOG (GNUNET_ERROR_TYPE_DEBUG,
552 "Received message of type %d and size %u from Multicast service\n",
557 case GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE:
558 size_min = sizeof (struct GNUNET_MULTICAST_MessageHeader);
561 case GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST:
562 size_min = sizeof (struct GNUNET_MULTICAST_RequestHeader);
565 case GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST:
566 size_min = sizeof (struct MulticastJoinRequestMessage);
569 case GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION:
570 size_min = sizeof (struct MulticastJoinDecisionMessage);
578 if (! ((0 < size_eq && size == size_eq)
579 || (0 < size_min && size_min <= size)))
587 case GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE:
589 GNUNET_CONTAINER_multihashmap_get_multiple (origins, &grp->pub_key_hash,
590 message_cb, (void *) msg);
592 GNUNET_CONTAINER_multihashmap_get_multiple (members, &grp->pub_key_hash,
593 message_cb, (void *) msg);
596 case GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST:
597 if (GNUNET_YES != grp->is_origin)
604 GNUNET_CONTAINER_multihashmap_get_multiple (origins, &grp->pub_key_hash,
605 request_cb, (void *) msg);
608 case GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST:
610 GNUNET_CONTAINER_multihashmap_get_multiple (origins, &grp->pub_key_hash,
611 join_request_cb, (void *) msg);
613 GNUNET_CONTAINER_multihashmap_get_multiple (members, &grp->pub_key_hash,
614 join_request_cb, (void *) msg);
617 case GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION:
619 GNUNET_CONTAINER_multihashmap_get_multiple (origins, &grp->pub_key_hash,
620 join_decision_cb, (void *) msg);
622 GNUNET_CONTAINER_multihashmap_get_multiple (members, &grp->pub_key_hash,
623 join_decision_cb, (void *) msg);
627 if (NULL != grp->client)
629 GNUNET_CLIENT_receive (grp->client, &message_handler, grp,
630 GNUNET_TIME_UNIT_FOREVER_REL);
636 * Function to call with the decision made for a join request.
638 * Must be called once and only once in response to an invocation of the
639 * #GNUNET_MULTICAST_JoinCallback.
641 * @param jh Join request handle.
642 * @param is_admitted #GNUNET_YES if joining is approved,
643 * #GNUNET_NO if it is disapproved
644 * @param relay_count Number of relays given.
645 * @param relays Array of suggested peers that might be useful relays to use
646 * when joining the multicast group (essentially a list of peers that
647 * are already part of the multicast group and might thus be willing
648 * to help with routing). If empty, only this local peer (which must
649 * be the multicast origin) is a good candidate for building the
650 * multicast tree. Note that it is unnecessary to specify our own
651 * peer identity in this array.
652 * @param join_resp Message to send in response to the joining peer;
653 * can also be used to redirect the peer to a different group at the
654 * application layer; this response is to be transmitted to the
655 * peer that issued the request even if admission is denied.
657 struct GNUNET_MULTICAST_ReplayHandle *
658 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
660 unsigned int relay_count,
661 const struct GNUNET_PeerIdentity *relays,
662 const struct GNUNET_MessageHeader *join_resp)
664 struct GNUNET_MULTICAST_Group *grp = jh->group;
665 uint16_t join_resp_size = (NULL != join_resp) ? ntohs (join_resp->size) : 0;
666 uint16_t relay_size = relay_count * sizeof (*relays);
667 struct MulticastClientJoinDecisionMessage * dcsn;
668 struct MessageQueue *
669 mq = GNUNET_malloc (sizeof (*mq) + sizeof (*dcsn)
670 + relay_size + join_resp_size);
672 dcsn = (struct MulticastClientJoinDecisionMessage *) &mq[1];
673 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION);
674 dcsn->header.size = htons (sizeof (*dcsn) + relay_size + join_resp_size);
675 dcsn->member_key = jh->member_key;
676 dcsn->member_peer = jh->member_peer;
677 dcsn->is_admitted = is_admitted;
678 dcsn->relay_count = relay_count;
680 memcpy (&dcsn[1], relays, relay_size);
681 if (0 < join_resp_size)
682 memcpy (((char *) &dcsn[1]) + relay_size, join_resp, join_resp_size);
684 GNUNET_CONTAINER_DLL_insert_tail (grp->tmit_head, grp->tmit_tail, mq);
693 * Call informing multicast about the decision taken for a membership test.
695 * @param mth Handle that was given for the query.
696 * @param result #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
697 * #GNUNET_SYSERR if we cannot answer the membership test.
700 GNUNET_MULTICAST_membership_test_result (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
707 * Replay a message fragment for the multicast group.
709 * @param rh Replay handle identifying which replay operation was requested.
710 * @param msg Replayed message fragment, NULL if unknown/error.
711 * @param ec Error code.
714 GNUNET_MULTICAST_replay_response (struct GNUNET_MULTICAST_ReplayHandle *rh,
715 const struct GNUNET_MessageHeader *msg,
716 enum GNUNET_MULTICAST_ReplayErrorCode ec)
722 * Indicate the end of the replay session.
724 * Invalidates the replay handle.
726 * @param rh Replay session to end.
729 GNUNET_MULTICAST_replay_response_end (struct GNUNET_MULTICAST_ReplayHandle *rh)
735 * Replay a message for the multicast group.
737 * @param rh Replay handle identifying which replay operation was requested.
738 * @param notify Function to call to get the message.
739 * @param notify_cls Closure for @a notify.
742 GNUNET_MULTICAST_replay_response2 (struct GNUNET_MULTICAST_ReplayHandle *rh,
743 GNUNET_MULTICAST_ReplayTransmitNotify notify,
750 * Start a multicast group.
752 * Will advertise the origin in the P2P overlay network under the respective
753 * public key so that other peer can find this peer to join it. Peers that
754 * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
755 * either an existing group member or to the origin. If the joining is
756 * approved, the member is cleared for @e replay and will begin to receive
757 * messages transmitted to the group. If joining is disapproved, the failed
758 * candidate will be given a response. Members in the group can send messages
759 * to the origin (one at a time).
761 * @param cfg Configuration to use.
762 * @param priv_key ECC key that will be used to sign messages for this
763 * multicast session; public key is used to identify the multicast group;
764 * @param max_fragment_id Maximum fragment ID already sent to the group.
766 * @param join_cb Function called to approve / disapprove joining of a peer.
767 * @param member_test_cb Function multicast can use to test group membership.
768 * @param replay_frag_cb Function that can be called to replay a message fragment.
769 * @param replay_msg_cb Function that can be called to replay a message.
770 * @param request_cb Function called with message fragments from group members.
771 * @param message_cb Function called with the message fragments sent to the
772 * network by GNUNET_MULTICAST_origin_to_all(). These message fragments
773 * should be stored for answering replay requests later.
774 * @param cls Closure for the various callbacks that follow.
776 * @return Handle for the origin, NULL on error.
778 struct GNUNET_MULTICAST_Origin *
779 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
780 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key,
781 uint64_t max_fragment_id,
782 GNUNET_MULTICAST_JoinCallback join_cb,
783 GNUNET_MULTICAST_MembershipTestCallback member_test_cb,
784 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
785 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
786 GNUNET_MULTICAST_RequestCallback request_cb,
787 GNUNET_MULTICAST_MessageCallback message_cb,
790 struct GNUNET_MULTICAST_Origin *orig = GNUNET_malloc (sizeof (*orig));
791 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
792 struct MulticastOriginStartMessage *start = GNUNET_malloc (sizeof (*start));
794 start->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_ORIGIN_START);
795 start->header.size = htons (sizeof (*start));
796 start->max_fragment_id = max_fragment_id;
797 memcpy (&start->group_key, priv_key, sizeof (*priv_key));
799 grp->reconnect_msg = (struct GNUNET_MessageHeader *) start;
800 grp->is_origin = GNUNET_YES;
804 grp->join_cb = join_cb;
805 grp->member_test_cb = member_test_cb;
806 grp->replay_frag_cb = replay_frag_cb;
807 grp->replay_msg_cb = replay_msg_cb;
808 grp->message_cb = message_cb;
810 orig->request_cb = request_cb;
811 orig->priv_key = *priv_key;
813 GNUNET_CRYPTO_eddsa_key_get_public (&orig->priv_key, &grp->pub_key);
814 GNUNET_CRYPTO_hash (&grp->pub_key, sizeof (grp->pub_key),
818 origins = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
820 GNUNET_CONTAINER_multihashmap_put (origins, &grp->pub_key_hash, orig,
821 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
823 grp->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
824 grp->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, grp);
831 * Stop a multicast group.
833 * @param origin Multicast group to stop.
836 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *orig)
838 disconnect (&orig->grp);
839 GNUNET_CONTAINER_multihashmap_remove (origins, &orig->grp.pub_key_hash, orig);
845 origin_to_all (struct GNUNET_MULTICAST_Origin *orig)
847 LOG (GNUNET_ERROR_TYPE_DEBUG, "origin_to_all()\n");
848 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
849 struct GNUNET_MULTICAST_OriginTransmitHandle *tmit = &orig->tmit;
851 size_t buf_size = GNUNET_MULTICAST_FRAGMENT_MAX_SIZE;
852 struct MessageQueue *mq = GNUNET_malloc (sizeof (*mq) + buf_size);
853 GNUNET_CONTAINER_DLL_insert_tail (grp->tmit_head, grp->tmit_tail, mq);
855 struct GNUNET_MULTICAST_MessageHeader *
856 msg = (struct GNUNET_MULTICAST_MessageHeader *) &mq[1];
857 int ret = tmit->notify (tmit->notify_cls, &buf_size, &msg[1]);
859 if (! (GNUNET_YES == ret || GNUNET_NO == ret)
860 || GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < buf_size)
862 LOG (GNUNET_ERROR_TYPE_ERROR,
863 "OriginTransmitNotify() returned error or invalid message size.\n");
864 /* FIXME: handle error */
869 if (GNUNET_NO == ret && 0 == buf_size)
872 return; /* Transmission paused. */
875 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
876 msg->header.size = htons (sizeof (*msg) + buf_size);
877 msg->message_id = GNUNET_htonll (tmit->message_id);
878 msg->group_generation = tmit->group_generation;
879 msg->fragment_offset = GNUNET_htonll (tmit->fragment_offset);
880 tmit->fragment_offset += sizeof (*msg) + buf_size;
887 * Send a message to the multicast group.
889 * @param orig Handle to the multicast group.
890 * @param message_id Application layer ID for the message. Opaque to multicast.
891 * @param group_generation Group generation of the message.
892 * Documented in struct GNUNET_MULTICAST_MessageHeader.
893 * @param notify Function to call to get the message.
894 * @param notify_cls Closure for @a notify.
896 * @return Message handle on success,
897 * NULL on error (i.e. another request is already pending).
899 struct GNUNET_MULTICAST_OriginTransmitHandle *
900 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *orig,
902 uint64_t group_generation,
903 GNUNET_MULTICAST_OriginTransmitNotify notify,
906 struct GNUNET_MULTICAST_OriginTransmitHandle *tmit = &orig->tmit;
908 tmit->message_id = message_id;
909 tmit->group_generation = group_generation;
910 tmit->notify = notify;
911 tmit->notify_cls = notify_cls;
913 origin_to_all (orig);
919 * Resume message transmission to multicast group.
921 * @param th Transmission to cancel.
924 GNUNET_MULTICAST_origin_to_all_resume (struct GNUNET_MULTICAST_OriginTransmitHandle *th)
926 origin_to_all (th->origin);
931 * Cancel request for message transmission to multicast group.
933 * @param th Transmission to cancel.
936 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginTransmitHandle *th)
942 * Join a multicast group.
944 * The entity joining is always the local peer. Further information about the
945 * candidate can be provided in the @a join_request message. If the join fails, the
946 * @a message_cb is invoked with a (failure) response and then with NULL. If
947 * the join succeeds, outstanding (state) messages and ongoing multicast
948 * messages will be given to the @a message_cb until the member decides to part
949 * the group. The @a test_cb and @a replay_cb functions may be called at
950 * anytime by the multicast service to support relaying messages to other
951 * members of the group.
953 * @param cfg Configuration to use.
954 * @param group_key ECC public key that identifies the group to join.
955 * @param member_key ECC key that identifies the member and used to sign
956 * requests sent to the origin.
957 * @param origin Peer ID of the origin to send unicast requsets to. If NULL,
958 * unicast requests are sent back via multiple hops on the reverse path
959 * of multicast messages.
960 * @param relay_count Number of peers in the @a relays array.
961 * @param relays Peer identities of members of the group, which serve as relays
962 * and can be used to join the group at. and send the @a join_request to.
963 * If empty, the @a join_request is sent directly to the @a origin.
964 * @param join_msg Application-dependent join message to be passed to the peer
966 * @param join_cb Function called to approve / disapprove joining of a peer.
967 * @param member_test_cb Function multicast can use to test group membership.
968 * @param replay_frag_cb Function that can be called to replay message fragments
969 * this peer already knows from this group. NULL if this
970 * client is unable to support replay.
971 * @param replay_msg_cb Function that can be called to replay message fragments
972 * this peer already knows from this group. NULL if this
973 * client is unable to support replay.
974 * @param message_cb Function to be called for all message fragments we
975 * receive from the group, excluding those our @a replay_cb
977 * @param cls Closure for callbacks.
978 * @return Handle for the member, NULL on error.
980 struct GNUNET_MULTICAST_Member *
981 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
982 const struct GNUNET_CRYPTO_EddsaPublicKey *group_key,
983 const struct GNUNET_CRYPTO_EddsaPrivateKey *member_key,
984 const struct GNUNET_PeerIdentity *origin,
985 uint32_t relay_count,
986 const struct GNUNET_PeerIdentity *relays,
987 const struct GNUNET_MessageHeader *join_msg,
988 GNUNET_MULTICAST_JoinCallback join_cb,
989 GNUNET_MULTICAST_MembershipTestCallback member_test_cb,
990 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
991 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
992 GNUNET_MULTICAST_MessageCallback message_cb,
995 struct GNUNET_MULTICAST_Member *mem = GNUNET_malloc (sizeof (*mem));
996 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
998 uint16_t relay_size = relay_count * sizeof (*relays);
999 uint16_t join_msg_size = (NULL != join_msg) ? ntohs (join_msg->size) : 0;
1000 struct MulticastMemberJoinMessage *
1001 join = GNUNET_malloc (sizeof (*join) + relay_size + join_msg_size);
1002 join->header.size = htons (sizeof (*join) + relay_size + join_msg_size);
1003 join->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MEMBER_JOIN);
1004 join->group_key = *group_key;
1005 join->member_key = *member_key;
1006 join->origin = *origin;
1008 memcpy (&join[1], relays, relay_size);
1009 if (0 < join_msg_size)
1010 memcpy (((char *) &join[1]) + relay_size, join_msg, join_msg_size);
1012 grp->reconnect_msg = (struct GNUNET_MessageHeader *) join;
1013 grp->is_origin = GNUNET_NO;
1015 grp->pub_key = *group_key;
1017 grp->join_cb = join_cb;
1018 grp->member_test_cb = member_test_cb;
1019 grp->replay_frag_cb = replay_frag_cb;
1020 grp->message_cb = message_cb;
1023 mem->origin = *origin;
1024 mem->relay_count = relay_count;
1025 mem->relays = *relays;
1026 mem->priv_key = *member_key;
1028 GNUNET_CRYPTO_eddsa_key_get_public (&mem->priv_key, &grp->pub_key);
1029 GNUNET_CRYPTO_hash (&grp->pub_key, sizeof (grp->pub_key), &grp->pub_key_hash);
1031 if (NULL == members)
1032 members = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1034 GNUNET_CONTAINER_multihashmap_put (members, &grp->pub_key_hash, mem,
1035 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1037 grp->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1038 grp->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, grp);
1045 * Part a multicast group.
1047 * Disconnects from all group members and invalidates the @a member handle.
1049 * An application-dependent part message can be transmitted beforehand using
1050 * #GNUNET_MULTICAST_member_to_origin())
1052 * @param member Membership handle.
1055 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *mem)
1057 disconnect (&mem->grp);
1058 GNUNET_CONTAINER_multihashmap_remove (members, &mem->grp.pub_key_hash, mem);
1064 * Request a fragment to be replayed by fragment ID.
1066 * Useful if messages below the @e max_known_fragment_id given when joining are
1067 * needed and not known to the client.
1069 * @param member Membership handle.
1070 * @param fragment_id ID of a message fragment that this client would like to
1072 * @param flags Additional flags for the replay request. It is used and defined
1073 * by the replay callback. FIXME: which replay callback? FIXME: use enum?
1074 * FIXME: why not pass reply cb here?
1075 * @return Replay request handle, NULL on error.
1077 struct GNUNET_MULTICAST_MemberReplayHandle *
1078 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *member,
1079 uint64_t fragment_id,
1087 * Request a message fragment to be replayed.
1089 * Useful if messages below the @e max_known_fragment_id given when joining are
1090 * needed and not known to the client.
1092 * @param member Membership handle.
1093 * @param message_id ID of the message this client would like to see replayed.
1094 * @param fragment_offset Offset of the fragment within the message to replay.
1095 * @param flags Additional flags for the replay request. It is used & defined
1096 * by the replay callback.
1097 * @param result_cb Function to be called for the replayed message.
1098 * @param result_cb_cls Closure for @a result_cb.
1099 * @return Replay request handle, NULL on error.
1101 struct GNUNET_MULTICAST_MemberReplayHandle *
1102 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *member,
1103 uint64_t message_id,
1104 uint64_t fragment_offset,
1106 GNUNET_MULTICAST_ResultCallback result_cb,
1107 void *result_cb_cls)
1114 * Cancel a replay request.
1116 * @param rh Request to cancel.
1119 GNUNET_MULTICAST_member_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh)
1125 member_to_origin (struct GNUNET_MULTICAST_Member *mem)
1127 LOG (GNUNET_ERROR_TYPE_DEBUG, "member_to_origin()\n");
1128 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
1129 struct GNUNET_MULTICAST_MemberTransmitHandle *tmit = &mem->tmit;
1131 size_t buf_size = GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD;
1132 struct MessageQueue *mq = GNUNET_malloc (sizeof (*mq) + buf_size);
1133 GNUNET_CONTAINER_DLL_insert_tail (grp->tmit_head, grp->tmit_tail, mq);
1135 struct GNUNET_MULTICAST_RequestHeader *
1136 req = (struct GNUNET_MULTICAST_RequestHeader *) &mq[1];
1137 int ret = tmit->notify (tmit->notify_cls, &buf_size, &req[1]);
1139 if (! (GNUNET_YES == ret || GNUNET_NO == ret)
1140 || GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < buf_size)
1142 LOG (GNUNET_ERROR_TYPE_ERROR,
1143 "MemberTransmitNotify() returned error or invalid message size.\n");
1144 /* FIXME: handle error */
1148 if (GNUNET_NO == ret && 0 == buf_size)
1149 return; /* Transmission paused. */
1151 req->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST);
1152 req->header.size = htons (sizeof (*req) + buf_size);
1153 req->request_id = GNUNET_htonll (tmit->request_id);
1154 req->fragment_offset = GNUNET_ntohll (tmit->fragment_offset);
1155 tmit->fragment_offset += sizeof (*req) + buf_size;
1157 transmit_next (grp);
1162 * Send a message to the origin of the multicast group.
1164 * @param mem Membership handle.
1165 * @param request_id Application layer ID for the request. Opaque to multicast.
1166 * @param notify Callback to call to get the message.
1167 * @param notify_cls Closure for @a notify.
1168 * @return Handle to cancel request, NULL on error (i.e. request already pending).
1170 struct GNUNET_MULTICAST_MemberTransmitHandle *
1171 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *mem,
1172 uint64_t request_id,
1173 GNUNET_MULTICAST_MemberTransmitNotify notify,
1176 struct GNUNET_MULTICAST_MemberTransmitHandle *tmit = &mem->tmit;
1178 tmit->request_id = request_id;
1179 tmit->notify = notify;
1180 tmit->notify_cls = notify_cls;
1182 member_to_origin (mem);
1188 * Resume message transmission to origin.
1190 * @param th Transmission to cancel.
1193 GNUNET_MULTICAST_member_to_origin_resume (struct GNUNET_MULTICAST_MemberTransmitHandle *th)
1200 * Cancel request for message transmission to origin.
1202 * @param th Transmission to cancel.
1205 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberTransmitHandle *th)
1210 /* end of multicast_api.c */