2 This file is part of GNUnet.
3 Copyright (C) 2012, 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 multicast/multicast_api.c
23 * @brief Multicast service; implements multicast groups using CADET connections.
24 * @author Christian Grothoff
25 * @author Gabor X Toth
29 #include "gnunet_util_lib.h"
30 #include "gnunet_multicast_service.h"
31 #include "multicast.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "multicast-api",__VA_ARGS__)
37 * Handle for a request to send a message to all multicast group members
40 struct GNUNET_MULTICAST_OriginTransmitHandle
42 GNUNET_MULTICAST_OriginTransmitNotify notify;
44 struct GNUNET_MULTICAST_Origin *origin;
47 uint64_t group_generation;
48 uint64_t fragment_offset;
53 * Handle for a message to be delivered from a member to the origin.
55 struct GNUNET_MULTICAST_MemberTransmitHandle
57 GNUNET_MULTICAST_MemberTransmitNotify notify;
59 struct GNUNET_MULTICAST_Member *member;
62 uint64_t fragment_offset;
66 struct GNUNET_MULTICAST_Group
69 * Configuration to use.
71 const struct GNUNET_CONFIGURATION_Handle *cfg;
74 * Client connection to the service.
76 struct GNUNET_MQ_Handle *mq;
79 * Time to wait until we try to reconnect on failure.
81 struct GNUNET_TIME_Relative reconnect_delay;
84 * Task for reconnecting when the listener fails.
86 struct GNUNET_SCHEDULER_Task *reconnect_task;
89 * Message to send on connect.
91 struct GNUNET_MQ_Envelope *connect_env;
93 GNUNET_MULTICAST_JoinRequestCallback join_req_cb;
94 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb;
95 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb;
96 GNUNET_MULTICAST_MessageCallback message_cb;
100 * Function called after disconnected from the service.
102 GNUNET_ContinuationCallback disconnect_cb;
105 * Closure for @a disconnect_cb.
107 void *disconnect_cls;
110 * Are we currently transmitting a message?
115 * Number of MULTICAST_FRAGMENT_ACK messages we are still waiting for.
117 uint8_t acks_pending;
120 * Is this the origin or a member?
125 * Is this channel in the process of disconnecting from the service?
126 * #GNUNET_YES or #GNUNET_NO
128 uint8_t is_disconnecting;
133 * Handle for the origin of a multicast group.
135 struct GNUNET_MULTICAST_Origin
137 struct GNUNET_MULTICAST_Group grp;
138 struct GNUNET_MULTICAST_OriginTransmitHandle tmit;
140 GNUNET_MULTICAST_RequestCallback request_cb;
145 * Handle for a multicast group member.
147 struct GNUNET_MULTICAST_Member
149 struct GNUNET_MULTICAST_Group grp;
150 struct GNUNET_MULTICAST_MemberTransmitHandle tmit;
152 GNUNET_MULTICAST_JoinDecisionCallback join_dcsn_cb;
155 * Replay fragment -> struct GNUNET_MULTICAST_MemberReplayHandle *
157 struct GNUNET_CONTAINER_MultiHashMap *replay_reqs;
159 uint64_t next_fragment_id;
164 * Handle that identifies a join request.
166 * Used to match calls to #GNUNET_MULTICAST_JoinRequestCallback to the
167 * corresponding calls to #GNUNET_MULTICAST_join_decision().
169 struct GNUNET_MULTICAST_JoinHandle
171 struct GNUNET_MULTICAST_Group *group;
174 * Public key of the member requesting join.
176 struct GNUNET_CRYPTO_EcdsaPublicKey member_pub_key;
179 * Peer identity of the member requesting join.
181 struct GNUNET_PeerIdentity peer;
186 * Opaque handle to a replay request from the multicast service.
188 struct GNUNET_MULTICAST_ReplayHandle
190 struct GNUNET_MULTICAST_Group *grp;
191 struct MulticastReplayRequestMessage req;
196 * Handle for a replay request.
198 struct GNUNET_MULTICAST_MemberReplayHandle
204 origin_to_all (struct GNUNET_MULTICAST_Origin *orig);
207 member_to_origin (struct GNUNET_MULTICAST_Member *mem);
211 * Check join request message.
214 check_group_join_request (void *cls,
215 const struct MulticastJoinRequestMessage *jreq)
217 uint16_t size = ntohs (jreq->header.size);
219 if (sizeof (*jreq) == size)
222 if (sizeof (*jreq) + sizeof (struct GNUNET_MessageHeader) <= size)
225 return GNUNET_SYSERR;
230 * Receive join request from service.
233 handle_group_join_request (void *cls,
234 const struct MulticastJoinRequestMessage *jreq)
236 struct GNUNET_MULTICAST_Group *grp = cls;
237 struct GNUNET_MULTICAST_JoinHandle *jh;
238 const struct GNUNET_MessageHeader *jmsg = NULL;
245 if (NULL == grp->join_req_cb)
248 if (sizeof (*jreq) + sizeof (*jmsg) <= ntohs (jreq->header.size))
249 jmsg = (const struct GNUNET_MessageHeader *) &jreq[1];
251 jh = GNUNET_malloc (sizeof (*jh));
253 jh->member_pub_key = jreq->member_pub_key;
254 jh->peer = jreq->peer;
255 grp->join_req_cb (grp->cb_cls, &jreq->member_pub_key, jmsg, jh);
257 grp->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
262 * Check multicast message.
265 check_group_message (void *cls,
266 const struct GNUNET_MULTICAST_MessageHeader *mmsg)
273 * Receive multicast message from service.
276 handle_group_message (void *cls,
277 const struct GNUNET_MULTICAST_MessageHeader *mmsg)
279 struct GNUNET_MULTICAST_Group *grp = cls;
281 if (GNUNET_YES == grp->is_disconnecting)
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Calling message callback with a message of size %u.\n",
286 ntohs (mmsg->header.size));
288 if (NULL != grp->message_cb)
289 grp->message_cb (grp->cb_cls, mmsg);
291 grp->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
296 * Receive message/request fragment acknowledgement from service.
299 handle_group_fragment_ack (void *cls,
300 const struct GNUNET_MessageHeader *msg)
302 struct GNUNET_MULTICAST_Group *grp = cls;
304 LOG (GNUNET_ERROR_TYPE_DEBUG,
305 "%p Got fragment ACK. in_transmit=%u, acks_pending=%u\n",
306 grp, grp->in_transmit, grp->acks_pending);
308 if (0 == grp->acks_pending)
310 LOG (GNUNET_ERROR_TYPE_DEBUG,
311 "%p Ignoring extraneous fragment ACK.\n", grp);
316 if (GNUNET_YES != grp->in_transmit)
319 if (GNUNET_YES == grp->is_origin)
320 origin_to_all ((struct GNUNET_MULTICAST_Origin *) grp);
322 member_to_origin ((struct GNUNET_MULTICAST_Member *) grp);
324 grp->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
329 * Check unicast request.
332 check_origin_request (void *cls,
333 const struct GNUNET_MULTICAST_RequestHeader *req)
340 * Origin receives unicast request from a member.
343 handle_origin_request (void *cls,
344 const struct GNUNET_MULTICAST_RequestHeader *req)
346 struct GNUNET_MULTICAST_Group *grp;
347 struct GNUNET_MULTICAST_Origin *orig = cls;
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351 "Calling request callback with a request of size %u.\n",
352 ntohs (req->header.size));
354 if (NULL != orig->request_cb)
355 orig->request_cb (grp->cb_cls, req);
357 grp->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
362 * Receive multicast replay request from service.
365 handle_group_replay_request (void *cls,
366 const struct MulticastReplayRequestMessage *rep)
369 struct GNUNET_MULTICAST_Group *grp = cls;
371 if (GNUNET_YES == grp->is_disconnecting)
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got replay request.\n");
376 if (0 != rep->fragment_id)
378 if (NULL != grp->replay_frag_cb)
380 struct GNUNET_MULTICAST_ReplayHandle * rh = GNUNET_malloc (sizeof (*rh));
383 grp->replay_frag_cb (grp->cb_cls, &rep->member_pub_key,
384 GNUNET_ntohll (rep->fragment_id),
385 GNUNET_ntohll (rep->flags), rh);
388 else if (0 != rep->message_id)
390 if (NULL != grp->replay_msg_cb)
392 struct GNUNET_MULTICAST_ReplayHandle * rh = GNUNET_malloc (sizeof (*rh));
395 grp->replay_msg_cb (grp->cb_cls, &rep->member_pub_key,
396 GNUNET_ntohll (rep->message_id),
397 GNUNET_ntohll (rep->fragment_offset),
398 GNUNET_ntohll (rep->flags), rh);
402 grp->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
407 * Check replay response.
410 check_member_replay_response (void *cls,
411 const struct MulticastReplayResponseMessage *res)
413 uint16_t size = ntohs (res->header.size);
415 if (sizeof (*res) == size)
418 if (sizeof (*res) + sizeof (struct GNUNET_MULTICAST_MessageHeader) <= size)
421 return GNUNET_SYSERR;
426 * Receive replay response from service.
429 handle_member_replay_response (void *cls,
430 const struct MulticastReplayResponseMessage *res)
432 struct GNUNET_MULTICAST_Group *grp;
433 struct GNUNET_MULTICAST_Member *mem = cls;
436 if (GNUNET_YES == grp->is_disconnecting)
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got replay response.\n");
441 // FIXME: return result
446 * Check join decision.
449 check_member_join_decision (void *cls,
450 const struct MulticastJoinDecisionMessageHeader *hdcsn)
452 return GNUNET_OK; // checked in handle below
457 * Member receives join decision.
460 handle_member_join_decision (void *cls,
461 const struct MulticastJoinDecisionMessageHeader *hdcsn)
463 struct GNUNET_MULTICAST_Group *grp;
464 struct GNUNET_MULTICAST_Member *mem = cls;
467 const struct MulticastJoinDecisionMessage *
468 dcsn = (const struct MulticastJoinDecisionMessage *) &hdcsn[1];
470 uint16_t dcsn_size = ntohs (dcsn->header.size);
471 int is_admitted = ntohl (dcsn->is_admitted);
473 LOG (GNUNET_ERROR_TYPE_DEBUG,
474 "%p Member got join decision from multicast: %d\n",
477 const struct GNUNET_MessageHeader *join_resp = NULL;
478 uint16_t join_resp_size = 0;
480 uint16_t relay_count = ntohl (dcsn->relay_count);
481 const struct GNUNET_PeerIdentity *relays = NULL;
482 uint16_t relay_size = relay_count * sizeof (*relays);
485 if (dcsn_size < sizeof (*dcsn) + relay_size)
488 is_admitted = GNUNET_SYSERR;
492 relays = (struct GNUNET_PeerIdentity *) &dcsn[1];
496 if (sizeof (*dcsn) + relay_size + sizeof (*join_resp) <= dcsn_size)
498 join_resp = (const struct GNUNET_MessageHeader *) ((char *) &dcsn[1] + relay_size);
499 join_resp_size = ntohs (join_resp->size);
501 if (dcsn_size < sizeof (*dcsn) + relay_size + join_resp_size)
503 LOG (GNUNET_ERROR_TYPE_DEBUG,
504 "Received invalid join decision message from multicast: %u < %u + %u + %u\n",
505 dcsn_size , sizeof (*dcsn), relay_size, join_resp_size);
507 is_admitted = GNUNET_SYSERR;
510 if (NULL != mem->join_dcsn_cb)
511 mem->join_dcsn_cb (grp->cb_cls, is_admitted, &hdcsn->peer,
512 relay_count, relays, join_resp);
515 //if (GNUNET_YES != is_admitted)
516 // GNUNET_MULTICAST_member_part (mem);
518 grp->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
523 group_cleanup (struct GNUNET_MULTICAST_Group *grp)
525 GNUNET_free (grp->connect_env);
526 if (NULL != grp->disconnect_cb)
527 grp->disconnect_cb (grp->disconnect_cls);
532 origin_cleanup (void *cls)
534 struct GNUNET_MULTICAST_Origin *orig = cls;
535 group_cleanup (&orig->grp);
541 member_cleanup (void *cls)
543 struct GNUNET_MULTICAST_Member *mem = cls;
544 group_cleanup (&mem->grp);
550 * Function to call with the decision made for a join request.
552 * Must be called once and only once in response to an invocation of the
553 * #GNUNET_MULTICAST_JoinRequestCallback.
556 * Join request handle.
558 * #GNUNET_YES if the join is approved,
559 * #GNUNET_NO if it is disapproved,
560 * #GNUNET_SYSERR if we cannot answer the request.
562 * Number of relays given.
564 * Array of suggested peers that might be useful relays to use
565 * when joining the multicast group (essentially a list of peers that
566 * are already part of the multicast group and might thus be willing
567 * to help with routing). If empty, only this local peer (which must
568 * be the multicast origin) is a good candidate for building the
569 * multicast tree. Note that it is unnecessary to specify our own
570 * peer identity in this array.
572 * Message to send in response to the joining peer;
573 * can also be used to redirect the peer to a different group at the
574 * application layer; this response is to be transmitted to the
575 * peer that issued the request even if admission is denied.
577 struct GNUNET_MULTICAST_ReplayHandle *
578 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *join,
580 uint16_t relay_count,
581 const struct GNUNET_PeerIdentity *relays,
582 const struct GNUNET_MessageHeader *join_resp)
584 struct GNUNET_MULTICAST_Group *grp = join->group;
585 uint16_t join_resp_size = (NULL != join_resp) ? ntohs (join_resp->size) : 0;
586 uint16_t relay_size = relay_count * sizeof (*relays);
588 struct MulticastJoinDecisionMessageHeader *hdcsn;
589 struct MulticastJoinDecisionMessage *dcsn;
590 struct GNUNET_MQ_Envelope *
591 env = GNUNET_MQ_msg_extra (hdcsn, sizeof (*dcsn) + relay_size + join_resp_size,
592 GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION);
593 hdcsn->member_pub_key = join->member_pub_key;
594 hdcsn->peer = join->peer;
596 dcsn = (struct MulticastJoinDecisionMessage *) &hdcsn[1];
597 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION);
598 dcsn->header.size = htons (sizeof (*dcsn) + relay_size + join_resp_size);
599 dcsn->is_admitted = htonl (is_admitted);
600 dcsn->relay_count = htonl (relay_count);
602 GNUNET_memcpy (&dcsn[1], relays, relay_size);
603 if (0 < join_resp_size)
604 GNUNET_memcpy (((char *) &dcsn[1]) + relay_size, join_resp, join_resp_size);
606 GNUNET_MQ_send (grp->mq, env);
613 * Replay a message fragment for the multicast group.
616 * Replay handle identifying which replay operation was requested.
618 * Replayed message fragment, NULL if not found / an error occurred.
620 * Error code. See enum GNUNET_MULTICAST_ReplayErrorCode
621 * If not #GNUNET_MULTICAST_REC_OK, the replay handle is invalidated.
624 GNUNET_MULTICAST_replay_response (struct GNUNET_MULTICAST_ReplayHandle *rh,
625 const struct GNUNET_MessageHeader *msg,
626 enum GNUNET_MULTICAST_ReplayErrorCode ec)
628 uint8_t msg_size = (NULL != msg) ? ntohs (msg->size) : 0;
629 struct MulticastReplayResponseMessage *res;
630 struct GNUNET_MQ_Envelope *
631 env = GNUNET_MQ_msg_extra (res, msg_size,
632 GNUNET_MESSAGE_TYPE_MULTICAST_REPLAY_RESPONSE);
633 res->fragment_id = rh->req.fragment_id;
634 res->message_id = rh->req.message_id;
635 res->fragment_offset = rh->req.fragment_offset;
636 res->flags = rh->req.flags;
637 res->error_code = htonl (ec);
639 if (GNUNET_MULTICAST_REC_OK == ec)
641 GNUNET_assert (NULL != msg);
642 GNUNET_memcpy (&res[1], msg, msg_size);
645 GNUNET_MQ_send (rh->grp->mq, env);
647 if (GNUNET_MULTICAST_REC_OK != ec)
653 * Indicate the end of the replay session.
655 * Invalidates the replay handle.
658 * Replay session to end.
661 GNUNET_MULTICAST_replay_response_end (struct GNUNET_MULTICAST_ReplayHandle *rh)
663 struct MulticastReplayResponseMessage *end;
664 struct GNUNET_MQ_Envelope *
665 env = GNUNET_MQ_msg (end, GNUNET_MESSAGE_TYPE_MULTICAST_REPLAY_RESPONSE_END);
667 end->fragment_id = rh->req.fragment_id;
668 end->message_id = rh->req.message_id;
669 end->fragment_offset = rh->req.fragment_offset;
670 end->flags = rh->req.flags;
672 GNUNET_MQ_send (rh->grp->mq, env);
678 * Replay a message for the multicast group.
681 * Replay handle identifying which replay operation was requested.
683 * Function to call to get the message.
685 * Closure for @a notify.
688 GNUNET_MULTICAST_replay_response2 (struct GNUNET_MULTICAST_ReplayHandle *rh,
689 GNUNET_MULTICAST_ReplayTransmitNotify notify,
696 origin_connect (struct GNUNET_MULTICAST_Origin *orig);
700 origin_reconnect (void *cls)
702 origin_connect (cls);
707 * Origin client disconnected from service.
709 * Reconnect after backoff period.
712 origin_disconnected (void *cls, enum GNUNET_MQ_Error error)
714 struct GNUNET_MULTICAST_Origin *orig = cls;
715 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 "Origin client disconnected (%d), re-connecting\n",
722 GNUNET_MQ_destroy (grp->mq);
726 grp->reconnect_task = GNUNET_SCHEDULER_add_delayed (grp->reconnect_delay,
729 grp->reconnect_delay = GNUNET_TIME_STD_BACKOFF (grp->reconnect_delay);
734 * Connect to service as origin.
737 origin_connect (struct GNUNET_MULTICAST_Origin *orig)
739 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
741 GNUNET_MQ_hd_var_size (group_message,
742 GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE,
743 struct GNUNET_MULTICAST_MessageHeader);
745 GNUNET_MQ_hd_var_size (origin_request,
746 GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST,
747 struct GNUNET_MULTICAST_RequestHeader);
749 GNUNET_MQ_hd_fixed_size (group_fragment_ack,
750 GNUNET_MESSAGE_TYPE_MULTICAST_FRAGMENT_ACK,
751 struct GNUNET_MessageHeader);
753 GNUNET_MQ_hd_var_size (group_join_request,
754 GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST,
755 struct MulticastJoinRequestMessage);
757 GNUNET_MQ_hd_fixed_size (group_replay_request,
758 GNUNET_MESSAGE_TYPE_MULTICAST_REPLAY_REQUEST,
759 struct MulticastReplayRequestMessage);
761 struct GNUNET_MQ_MessageHandler handlers[] = {
762 make_group_message_handler (grp),
763 make_origin_request_handler (orig),
764 make_group_fragment_ack_handler (grp),
765 make_group_join_request_handler (grp),
766 make_group_replay_request_handler (grp),
767 GNUNET_MQ_handler_end ()
770 grp->mq = GNUNET_CLIENT_connecT (grp->cfg, "multicast",
771 handlers, origin_disconnected, orig);
772 GNUNET_assert (NULL != grp->mq);
773 GNUNET_MQ_send_copy (grp->mq, grp->connect_env);
778 * Start a multicast group.
780 * Will advertise the origin in the P2P overlay network under the respective
781 * public key so that other peer can find this peer to join it. Peers that
782 * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
783 * either an existing group member or to the origin. If the joining is
784 * approved, the member is cleared for @e replay and will begin to receive
785 * messages transmitted to the group. If joining is disapproved, the failed
786 * candidate will be given a response. Members in the group can send messages
787 * to the origin (one at a time).
790 * Configuration to use.
792 * ECC key that will be used to sign messages for this
793 * multicast session; public key is used to identify the multicast group;
794 * @param max_fragment_id
795 * Maximum fragment ID already sent to the group.
797 * @param join_request_cb
798 * Function called to approve / disapprove joining of a peer.
799 * @param replay_frag_cb
800 * Function that can be called to replay a message fragment.
801 * @param replay_msg_cb
802 * Function that can be called to replay a message.
804 * Function called with message fragments from group members.
806 * Function called with the message fragments sent to the
807 * network by GNUNET_MULTICAST_origin_to_all(). These message fragments
808 * should be stored for answering replay requests later.
810 * Closure for the various callbacks that follow.
812 * @return Handle for the origin, NULL on error.
814 struct GNUNET_MULTICAST_Origin *
815 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
816 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key,
817 uint64_t max_fragment_id,
818 GNUNET_MULTICAST_JoinRequestCallback join_request_cb,
819 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
820 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
821 GNUNET_MULTICAST_RequestCallback request_cb,
822 GNUNET_MULTICAST_MessageCallback message_cb,
825 struct GNUNET_MULTICAST_Origin *orig = GNUNET_malloc (sizeof (*orig));
826 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
828 struct MulticastOriginStartMessage *start;
829 grp->connect_env = GNUNET_MQ_msg (start,
830 GNUNET_MESSAGE_TYPE_MULTICAST_ORIGIN_START);
831 start->max_fragment_id = max_fragment_id;
832 GNUNET_memcpy (&start->group_key, priv_key, sizeof (*priv_key));
834 grp->is_origin = GNUNET_YES;
838 grp->join_req_cb = join_request_cb;
839 grp->replay_frag_cb = replay_frag_cb;
840 grp->replay_msg_cb = replay_msg_cb;
841 grp->message_cb = message_cb;
843 orig->request_cb = request_cb;
845 origin_connect (orig);
851 * Stop a multicast group.
854 * Multicast group to stop.
857 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *orig,
858 GNUNET_ContinuationCallback stop_cb,
861 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
863 grp->is_disconnecting = GNUNET_YES;
864 grp->disconnect_cb = stop_cb;
865 grp->disconnect_cls = stop_cls;
867 // FIXME: wait till queued messages are sent
870 GNUNET_MQ_destroy (grp->mq);
873 origin_cleanup (orig);
878 origin_to_all (struct GNUNET_MULTICAST_Origin *orig)
880 LOG (GNUNET_ERROR_TYPE_DEBUG, "%p origin_to_all()\n", orig);
881 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
882 struct GNUNET_MULTICAST_OriginTransmitHandle *tmit = &orig->tmit;
883 GNUNET_assert (GNUNET_YES == grp->in_transmit);
885 size_t buf_size = GNUNET_MULTICAST_FRAGMENT_MAX_SIZE;
886 struct GNUNET_MULTICAST_MessageHeader *msg;
887 struct GNUNET_MQ_Envelope *
888 env = GNUNET_MQ_msg_extra (msg, buf_size - sizeof(*msg),
889 GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
891 int ret = tmit->notify (tmit->notify_cls, &buf_size, &msg[1]);
893 if (! (GNUNET_YES == ret || GNUNET_NO == ret)
894 || GNUNET_MULTICAST_FRAGMENT_MAX_SIZE < buf_size)
896 LOG (GNUNET_ERROR_TYPE_ERROR,
897 "%p OriginTransmitNotify() returned error or invalid message size.\n",
899 /* FIXME: handle error */
900 GNUNET_MQ_discard (env);
904 if (GNUNET_NO == ret && 0 == buf_size)
906 LOG (GNUNET_ERROR_TYPE_DEBUG,
907 "%p OriginTransmitNotify() - transmission paused.\n", orig);
908 GNUNET_MQ_discard (env);
909 return; /* Transmission paused. */
912 msg->header.size = htons (sizeof (*msg) + buf_size);
913 msg->message_id = GNUNET_htonll (tmit->message_id);
914 msg->group_generation = tmit->group_generation;
915 msg->fragment_offset = GNUNET_htonll (tmit->fragment_offset);
916 tmit->fragment_offset += sizeof (*msg) + buf_size;
919 GNUNET_MQ_send (grp->mq, env);
921 if (GNUNET_YES == ret)
922 grp->in_transmit = GNUNET_NO;
927 * Send a message to the multicast group.
930 * Handle to the multicast group.
932 * Application layer ID for the message. Opaque to multicast.
933 * @param group_generation
934 * Group generation of the message.
935 * Documented in struct GNUNET_MULTICAST_MessageHeader.
937 * Function to call to get the message.
939 * Closure for @a notify.
941 * @return Message handle on success,
942 * NULL on error (i.e. another request is already pending).
944 struct GNUNET_MULTICAST_OriginTransmitHandle *
945 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *orig,
947 uint64_t group_generation,
948 GNUNET_MULTICAST_OriginTransmitNotify notify,
951 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
952 if (GNUNET_YES == grp->in_transmit)
954 grp->in_transmit = GNUNET_YES;
956 struct GNUNET_MULTICAST_OriginTransmitHandle *tmit = &orig->tmit;
958 tmit->message_id = message_id;
959 tmit->fragment_offset = 0;
960 tmit->group_generation = group_generation;
961 tmit->notify = notify;
962 tmit->notify_cls = notify_cls;
964 origin_to_all (orig);
970 * Resume message transmission to multicast group.
973 * Transmission to cancel.
976 GNUNET_MULTICAST_origin_to_all_resume (struct GNUNET_MULTICAST_OriginTransmitHandle *th)
978 struct GNUNET_MULTICAST_Group *grp = &th->origin->grp;
979 if (0 != grp->acks_pending || GNUNET_YES != grp->in_transmit)
981 origin_to_all (th->origin);
986 * Cancel request for message transmission to multicast group.
989 * Transmission to cancel.
992 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginTransmitHandle *th)
994 th->origin->grp.in_transmit = GNUNET_NO;
999 member_connect (struct GNUNET_MULTICAST_Member *mem);
1003 member_reconnect (void *cls)
1005 member_connect (cls);
1010 * Member client disconnected from service.
1012 * Reconnect after backoff period.
1015 member_disconnected (void *cls, enum GNUNET_MQ_Error error)
1017 struct GNUNET_MULTICAST_Member *mem = cls;
1018 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
1020 LOG (GNUNET_ERROR_TYPE_DEBUG,
1021 "Member client disconnected (%d), re-connecting\n",
1023 GNUNET_MQ_destroy (grp->mq);
1026 grp->reconnect_task = GNUNET_SCHEDULER_add_delayed (grp->reconnect_delay,
1029 grp->reconnect_delay = GNUNET_TIME_STD_BACKOFF (grp->reconnect_delay);
1034 * Connect to service as member.
1037 member_connect (struct GNUNET_MULTICAST_Member *mem)
1039 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
1041 GNUNET_MQ_hd_var_size (group_message,
1042 GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE,
1043 struct GNUNET_MULTICAST_MessageHeader);
1045 GNUNET_MQ_hd_fixed_size (group_fragment_ack,
1046 GNUNET_MESSAGE_TYPE_MULTICAST_FRAGMENT_ACK,
1047 struct GNUNET_MessageHeader);
1049 GNUNET_MQ_hd_var_size (group_join_request,
1050 GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST,
1051 struct MulticastJoinRequestMessage);
1053 GNUNET_MQ_hd_var_size (member_join_decision,
1054 GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION,
1055 struct MulticastJoinDecisionMessageHeader);
1057 GNUNET_MQ_hd_fixed_size (group_replay_request,
1058 GNUNET_MESSAGE_TYPE_MULTICAST_REPLAY_REQUEST,
1059 struct MulticastReplayRequestMessage);
1061 GNUNET_MQ_hd_var_size (member_replay_response,
1062 GNUNET_MESSAGE_TYPE_MULTICAST_REPLAY_RESPONSE,
1063 struct MulticastReplayResponseMessage);
1065 struct GNUNET_MQ_MessageHandler handlers[] = {
1066 make_group_message_handler (grp),
1067 make_group_fragment_ack_handler (grp),
1068 make_group_join_request_handler (grp),
1069 make_member_join_decision_handler (mem),
1070 make_group_replay_request_handler (grp),
1071 make_member_replay_response_handler (mem),
1072 GNUNET_MQ_handler_end ()
1075 grp->mq = GNUNET_CLIENT_connecT (grp->cfg, "multicast",
1076 handlers, member_disconnected, mem);
1077 GNUNET_assert (NULL != grp->mq);
1078 GNUNET_MQ_send_copy (grp->mq, grp->connect_env);
1083 * Join a multicast group.
1085 * The entity joining is always the local peer. Further information about the
1086 * candidate can be provided in the @a join_request message. If the join fails, the
1087 * @a message_cb is invoked with a (failure) response and then with NULL. If
1088 * the join succeeds, outstanding (state) messages and ongoing multicast
1089 * messages will be given to the @a message_cb until the member decides to part
1090 * the group. The @a replay_cb function may be called at any time by the
1091 * multicast service to support relaying messages to other members of the group.
1094 * Configuration to use.
1096 * ECC public key that identifies the group to join.
1098 * ECC key that identifies the member
1099 * and used to sign requests sent to the origin.
1101 * Peer ID of the origin to send unicast requsets to. If NULL,
1102 * unicast requests are sent back via multiple hops on the reverse path
1103 * of multicast messages.
1104 * @param relay_count
1105 * Number of peers in the @a relays array.
1107 * Peer identities of members of the group, which serve as relays
1108 * and can be used to join the group at. and send the @a join_request to.
1109 * If empty, the @a join_request is sent directly to the @a origin.
1111 * Application-dependent join message to be passed to the peer @a origin.
1112 * @param join_request_cb
1113 * Function called to approve / disapprove joining of a peer.
1114 * @param join_decision_cb
1115 * Function called to inform about the join decision.
1116 * @param replay_frag_cb
1117 * Function that can be called to replay message fragments
1118 * this peer already knows from this group. NULL if this
1119 * client is unable to support replay.
1120 * @param replay_msg_cb
1121 * Function that can be called to replay message fragments
1122 * this peer already knows from this group. NULL if this
1123 * client is unable to support replay.
1125 * Function to be called for all message fragments we
1126 * receive from the group, excluding those our @a replay_cb
1129 * Closure for callbacks.
1131 * @return Handle for the member, NULL on error.
1133 struct GNUNET_MULTICAST_Member *
1134 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
1135 const struct GNUNET_CRYPTO_EddsaPublicKey *group_pub_key,
1136 const struct GNUNET_CRYPTO_EcdsaPrivateKey *member_key,
1137 const struct GNUNET_PeerIdentity *origin,
1138 uint16_t relay_count,
1139 const struct GNUNET_PeerIdentity *relays,
1140 const struct GNUNET_MessageHeader *join_msg,
1141 GNUNET_MULTICAST_JoinRequestCallback join_request_cb,
1142 GNUNET_MULTICAST_JoinDecisionCallback join_decision_cb,
1143 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
1144 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
1145 GNUNET_MULTICAST_MessageCallback message_cb,
1148 struct GNUNET_MULTICAST_Member *mem = GNUNET_malloc (sizeof (*mem));
1149 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
1151 uint16_t relay_size = relay_count * sizeof (*relays);
1152 uint16_t join_msg_size = (NULL != join_msg) ? ntohs (join_msg->size) : 0;
1153 struct MulticastMemberJoinMessage *join;
1154 grp->connect_env = GNUNET_MQ_msg_extra (join, relay_size + join_msg_size,
1155 GNUNET_MESSAGE_TYPE_MULTICAST_MEMBER_JOIN);
1156 join->group_pub_key = *group_pub_key;
1157 join->member_key = *member_key;
1158 join->origin = *origin;
1159 join->relay_count = ntohl (relay_count);
1161 GNUNET_memcpy (&join[1], relays, relay_size);
1162 if (0 < join_msg_size)
1163 GNUNET_memcpy (((char *) &join[1]) + relay_size, join_msg, join_msg_size);
1165 grp->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1166 grp->is_origin = GNUNET_NO;
1169 mem->join_dcsn_cb = join_decision_cb;
1170 grp->join_req_cb = join_request_cb;
1171 grp->replay_frag_cb = replay_frag_cb;
1172 grp->replay_msg_cb = replay_msg_cb;
1173 grp->message_cb = message_cb;
1176 member_connect (mem);
1182 * Part a multicast group.
1184 * Disconnects from all group members and invalidates the @a member handle.
1186 * An application-dependent part message can be transmitted beforehand using
1187 * #GNUNET_MULTICAST_member_to_origin())
1190 * Membership handle.
1193 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *mem,
1194 GNUNET_ContinuationCallback part_cb,
1197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%p Member parting.\n", mem);
1198 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
1200 grp->is_disconnecting = GNUNET_YES;
1201 grp->disconnect_cb = part_cb;
1202 grp->disconnect_cls = part_cls;
1204 mem->join_dcsn_cb = NULL;
1205 grp->join_req_cb = NULL;
1206 grp->message_cb = NULL;
1207 grp->replay_msg_cb = NULL;
1208 grp->replay_frag_cb = NULL;
1210 // FIXME: wait till queued messages are sent
1211 if (NULL != grp->mq)
1213 GNUNET_MQ_destroy (grp->mq);
1216 member_cleanup (mem);
1221 member_replay_request (struct GNUNET_MULTICAST_Member *mem,
1222 uint64_t fragment_id,
1223 uint64_t message_id,
1224 uint64_t fragment_offset,
1227 struct MulticastReplayRequestMessage *rep;
1228 struct GNUNET_MQ_Envelope *
1229 env = GNUNET_MQ_msg (rep, GNUNET_MESSAGE_TYPE_MULTICAST_REPLAY_REQUEST);
1231 rep->fragment_id = GNUNET_htonll (fragment_id);
1232 rep->message_id = GNUNET_htonll (message_id);
1233 rep->fragment_offset = GNUNET_htonll (fragment_offset);
1234 rep->flags = GNUNET_htonll (flags);
1236 GNUNET_MQ_send (mem->grp.mq, env);
1241 * Request a fragment to be replayed by fragment ID.
1243 * Useful if messages below the @e max_known_fragment_id given when joining are
1244 * needed and not known to the client.
1247 * Membership handle.
1248 * @param fragment_id
1249 * ID of a message fragment that this client would like to see replayed.
1251 * Additional flags for the replay request.
1252 * It is used and defined by GNUNET_MULTICAST_ReplayFragmentCallback
1254 * @return Replay request handle.
1256 struct GNUNET_MULTICAST_MemberReplayHandle *
1257 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *mem,
1258 uint64_t fragment_id,
1261 member_replay_request (mem, fragment_id, 0, 0, flags);
1262 // FIXME: return something useful
1268 * Request a message fragment to be replayed.
1270 * Useful if messages below the @e max_known_fragment_id given when joining are
1271 * needed and not known to the client.
1274 * Membership handle.
1276 * ID of the message this client would like to see replayed.
1277 * @param fragment_offset
1278 * Offset of the fragment within the message to replay.
1280 * Additional flags for the replay request.
1281 * It is used & defined by GNUNET_MULTICAST_ReplayMessageCallback
1283 * @return Replay request handle, NULL on error.
1285 struct GNUNET_MULTICAST_MemberReplayHandle *
1286 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *mem,
1287 uint64_t message_id,
1288 uint64_t fragment_offset,
1291 member_replay_request (mem, 0, message_id, fragment_offset, flags);
1292 // FIXME: return something useful
1298 member_to_origin (struct GNUNET_MULTICAST_Member *mem)
1300 LOG (GNUNET_ERROR_TYPE_DEBUG, "member_to_origin()\n");
1301 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
1302 struct GNUNET_MULTICAST_MemberTransmitHandle *tmit = &mem->tmit;
1303 GNUNET_assert (GNUNET_YES == grp->in_transmit);
1305 size_t buf_size = GNUNET_MULTICAST_FRAGMENT_MAX_SIZE;
1306 struct GNUNET_MULTICAST_RequestHeader *req;
1307 struct GNUNET_MQ_Envelope *
1308 env = GNUNET_MQ_msg_extra (req, buf_size - sizeof(*req),
1309 GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST);
1311 int ret = tmit->notify (tmit->notify_cls, &buf_size, &req[1]);
1313 if (! (GNUNET_YES == ret || GNUNET_NO == ret)
1314 || GNUNET_MULTICAST_FRAGMENT_MAX_SIZE < buf_size)
1316 LOG (GNUNET_ERROR_TYPE_ERROR,
1317 "MemberTransmitNotify() returned error or invalid message size. "
1318 "ret=%d, buf_size=%u\n", ret, buf_size);
1319 /* FIXME: handle error */
1320 GNUNET_MQ_discard (env);
1324 if (GNUNET_NO == ret && 0 == buf_size)
1326 /* Transmission paused. */
1327 GNUNET_MQ_discard (env);
1331 req->header.size = htons (sizeof (*req) + buf_size);
1332 req->request_id = GNUNET_htonll (tmit->request_id);
1333 req->fragment_offset = GNUNET_ntohll (tmit->fragment_offset);
1334 tmit->fragment_offset += sizeof (*req) + buf_size;
1336 GNUNET_MQ_send (grp->mq, env);
1338 if (GNUNET_YES == ret)
1339 grp->in_transmit = GNUNET_NO;
1344 * Send a message to the origin of the multicast group.
1347 * Membership handle.
1349 * Application layer ID for the request. Opaque to multicast.
1351 * Callback to call to get the message.
1353 * Closure for @a notify.
1355 * @return Handle to cancel request, NULL on error (i.e. request already pending).
1357 struct GNUNET_MULTICAST_MemberTransmitHandle *
1358 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *mem,
1359 uint64_t request_id,
1360 GNUNET_MULTICAST_MemberTransmitNotify notify,
1363 if (GNUNET_YES == mem->grp.in_transmit)
1365 mem->grp.in_transmit = GNUNET_YES;
1367 struct GNUNET_MULTICAST_MemberTransmitHandle *tmit = &mem->tmit;
1369 tmit->request_id = request_id;
1370 tmit->fragment_offset = 0;
1371 tmit->notify = notify;
1372 tmit->notify_cls = notify_cls;
1374 member_to_origin (mem);
1380 * Resume message transmission to origin.
1383 * Transmission to cancel.
1386 GNUNET_MULTICAST_member_to_origin_resume (struct GNUNET_MULTICAST_MemberTransmitHandle *th)
1388 struct GNUNET_MULTICAST_Group *grp = &th->member->grp;
1389 if (0 != grp->acks_pending || GNUNET_YES != grp->in_transmit)
1391 member_to_origin (th->member);
1396 * Cancel request for message transmission to origin.
1399 * Transmission to cancel.
1402 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberTransmitHandle *th)
1404 th->member->grp.in_transmit = GNUNET_NO;
1408 /* end of multicast_api.c */