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_CLIENT_MANAGER_Connection *client;
79 * Message to send on reconnect.
81 struct GNUNET_MessageHeader *connect_msg;
83 GNUNET_MULTICAST_JoinRequestCallback join_req_cb;
84 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb;
85 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb;
86 GNUNET_MULTICAST_MessageCallback message_cb;
90 * Function called after disconnected from the service.
92 GNUNET_ContinuationCallback disconnect_cb;
95 * Closure for @a disconnect_cb.
100 * Are we currently transmitting a message?
105 * Number of MULTICAST_FRAGMENT_ACK messages we are still waiting for.
107 uint8_t acks_pending;
110 * Is this the origin or a member?
115 * Is this channel in the process of disconnecting from the service?
116 * #GNUNET_YES or #GNUNET_NO
118 uint8_t is_disconnecting;
123 * Handle for the origin of a multicast group.
125 struct GNUNET_MULTICAST_Origin
127 struct GNUNET_MULTICAST_Group grp;
128 struct GNUNET_MULTICAST_OriginTransmitHandle tmit;
130 GNUNET_MULTICAST_RequestCallback request_cb;
135 * Handle for a multicast group member.
137 struct GNUNET_MULTICAST_Member
139 struct GNUNET_MULTICAST_Group grp;
140 struct GNUNET_MULTICAST_MemberTransmitHandle tmit;
142 GNUNET_MULTICAST_JoinDecisionCallback join_dcsn_cb;
145 * Replay fragment -> struct GNUNET_MULTICAST_MemberReplayHandle *
147 struct GNUNET_CONTAINER_MultiHashMap *replay_reqs;
149 uint64_t next_fragment_id;
154 * Handle that identifies a join request.
156 * Used to match calls to #GNUNET_MULTICAST_JoinRequestCallback to the
157 * corresponding calls to #GNUNET_MULTICAST_join_decision().
159 struct GNUNET_MULTICAST_JoinHandle
161 struct GNUNET_MULTICAST_Group *group;
164 * Public key of the member requesting join.
166 struct GNUNET_CRYPTO_EcdsaPublicKey member_pub_key;
169 * Peer identity of the member requesting join.
171 struct GNUNET_PeerIdentity peer;
176 * Opaque handle to a replay request from the multicast service.
178 struct GNUNET_MULTICAST_ReplayHandle
180 struct GNUNET_MULTICAST_Group *grp;
181 struct MulticastReplayRequestMessage req;
186 * Handle for a replay request.
188 struct GNUNET_MULTICAST_MemberReplayHandle
194 origin_to_all (struct GNUNET_MULTICAST_Origin *orig);
197 member_to_origin (struct GNUNET_MULTICAST_Member *mem);
201 * Send first message to the service after connecting.
204 group_send_connect_msg (struct GNUNET_MULTICAST_Group *grp)
206 uint16_t cmsg_size = ntohs (grp->connect_msg->size);
207 struct GNUNET_MessageHeader *cmsg = GNUNET_malloc (cmsg_size);
208 memcpy (cmsg, grp->connect_msg, cmsg_size);
209 GNUNET_CLIENT_MANAGER_transmit_now (grp->client, cmsg);
215 * Got disconnected from service. Reconnect.
218 group_recv_disconnect (void *cls,
219 struct GNUNET_CLIENT_MANAGER_Connection *client,
220 const struct GNUNET_MessageHeader *msg)
222 struct GNUNET_MULTICAST_Group *
223 grp = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
224 GNUNET_CLIENT_MANAGER_reconnect (client);
225 group_send_connect_msg (grp);
230 * Receive join request from service.
233 group_recv_join_request (void *cls,
234 struct GNUNET_CLIENT_MANAGER_Connection *client,
235 const struct GNUNET_MessageHeader *msg)
237 struct GNUNET_MULTICAST_Group *grp;
238 const struct MulticastJoinRequestMessage *jreq;
239 struct GNUNET_MULTICAST_JoinHandle *jh;
240 const struct GNUNET_MessageHeader *jmsg;
242 grp = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
248 if (NULL == grp->join_req_cb)
250 /* FIXME: this fails to check that 'msg' is well-formed! */
251 jreq = (const struct MulticastJoinRequestMessage *) msg;
252 if (sizeof (*jreq) + sizeof (*jmsg) <= ntohs (jreq->header.size))
253 jmsg = (const struct GNUNET_MessageHeader *) &jreq[1];
256 jh = GNUNET_malloc (sizeof (*jh));
258 jh->member_pub_key = jreq->member_pub_key;
259 jh->peer = jreq->peer;
260 grp->join_req_cb (grp->cb_cls, &jreq->member_pub_key, jmsg, jh);
265 * Receive multicast message from service.
268 group_recv_message (void *cls,
269 struct GNUNET_CLIENT_MANAGER_Connection *client,
270 const struct GNUNET_MessageHeader *msg)
272 struct GNUNET_MULTICAST_Group *
273 grp = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
274 struct GNUNET_MULTICAST_MessageHeader *
275 mmsg = (struct GNUNET_MULTICAST_MessageHeader *) msg;
277 if (GNUNET_YES == grp->is_disconnecting)
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Calling message callback with a message of size %u.\n",
282 ntohs (mmsg->header.size));
284 if (NULL != grp->message_cb)
285 grp->message_cb (grp->cb_cls, mmsg);
290 * Receive message/request fragment acknowledgement from service.
293 group_recv_fragment_ack (void *cls,
294 struct GNUNET_CLIENT_MANAGER_Connection *client,
295 const struct GNUNET_MessageHeader *msg)
297 struct GNUNET_MULTICAST_Group *
298 grp = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
300 LOG (GNUNET_ERROR_TYPE_DEBUG,
301 "%p Got fragment ACK. in_transmit=%u, acks_pending=%u\n",
302 grp, grp->in_transmit, grp->acks_pending);
304 if (0 == grp->acks_pending)
306 LOG (GNUNET_ERROR_TYPE_DEBUG,
307 "%p Ignoring extraneous fragment ACK.\n", grp);
312 if (GNUNET_YES != grp->in_transmit)
315 if (GNUNET_YES == grp->is_origin)
316 origin_to_all ((struct GNUNET_MULTICAST_Origin *) grp);
318 member_to_origin ((struct GNUNET_MULTICAST_Member *) grp);
322 * Origin receives uniquest request from a member.
325 origin_recv_request (void *cls,
326 struct GNUNET_CLIENT_MANAGER_Connection *client,
327 const struct GNUNET_MessageHeader *msg)
329 struct GNUNET_MULTICAST_Group *grp;
330 struct GNUNET_MULTICAST_Origin *
331 orig = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
333 struct GNUNET_MULTICAST_RequestHeader *
334 req = (struct GNUNET_MULTICAST_RequestHeader *) msg;
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "Calling request callback with a request of size %u.\n",
338 ntohs (req->header.size));
340 if (NULL != orig->request_cb)
341 orig->request_cb (grp->cb_cls, req);
346 * Receive multicast replay request from service.
349 group_recv_replay_request (void *cls,
350 struct GNUNET_CLIENT_MANAGER_Connection *client,
351 const struct GNUNET_MessageHeader *msg)
353 struct GNUNET_MULTICAST_Group *
354 grp = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
355 struct MulticastReplayRequestMessage *
356 rep = (struct MulticastReplayRequestMessage *) msg;
358 if (GNUNET_YES == grp->is_disconnecting)
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got replay request.\n");
363 if (0 != rep->fragment_id)
365 if (NULL != grp->replay_frag_cb)
367 struct GNUNET_MULTICAST_ReplayHandle * rh = GNUNET_malloc (sizeof (*rh));
370 grp->replay_frag_cb (grp->cb_cls, &rep->member_pub_key,
371 GNUNET_ntohll (rep->fragment_id),
372 GNUNET_ntohll (rep->flags), rh);
375 else if (0 != rep->message_id)
377 if (NULL != grp->replay_msg_cb)
379 struct GNUNET_MULTICAST_ReplayHandle * rh = GNUNET_malloc (sizeof (*rh));
382 grp->replay_msg_cb (grp->cb_cls, &rep->member_pub_key,
383 GNUNET_ntohll (rep->message_id),
384 GNUNET_ntohll (rep->fragment_offset),
385 GNUNET_ntohll (rep->flags), rh);
392 * Receive multicast replay request from service.
395 member_recv_replay_response (void *cls,
396 struct GNUNET_CLIENT_MANAGER_Connection *client,
397 const struct GNUNET_MessageHeader *msg)
399 struct GNUNET_MULTICAST_Group *grp;
400 struct GNUNET_MULTICAST_Member *
401 mem = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
403 struct MulticastReplayResponseMessage *
404 res = (struct MulticastReplayResponseMessage *) msg;
406 if (GNUNET_YES == grp->is_disconnecting)
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got replay response.\n");
413 * Member receives join decision.
416 member_recv_join_decision (void *cls,
417 struct GNUNET_CLIENT_MANAGER_Connection *client,
418 const struct GNUNET_MessageHeader *msg)
420 struct GNUNET_MULTICAST_Group *grp;
421 struct GNUNET_MULTICAST_Member *
422 mem = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
425 const struct MulticastJoinDecisionMessageHeader *
426 hdcsn = (const struct MulticastJoinDecisionMessageHeader *) msg;
427 const struct MulticastJoinDecisionMessage *
428 dcsn = (const struct MulticastJoinDecisionMessage *) &hdcsn[1];
430 uint16_t dcsn_size = ntohs (dcsn->header.size);
431 int is_admitted = ntohl (dcsn->is_admitted);
433 LOG (GNUNET_ERROR_TYPE_DEBUG,
434 "%p Member got join decision from multicast: %d\n",
437 const struct GNUNET_MessageHeader *join_resp = NULL;
438 uint16_t join_resp_size = 0;
440 uint16_t relay_count = ntohl (dcsn->relay_count);
441 const struct GNUNET_PeerIdentity *relays = NULL;
442 uint16_t relay_size = relay_count * sizeof (*relays);
445 if (dcsn_size < sizeof (*dcsn) + relay_size)
448 is_admitted = GNUNET_SYSERR;
452 relays = (struct GNUNET_PeerIdentity *) &dcsn[1];
456 if (sizeof (*dcsn) + relay_size + sizeof (*join_resp) <= dcsn_size)
458 join_resp = (const struct GNUNET_MessageHeader *) ((char *) &dcsn[1] + relay_size);
459 join_resp_size = ntohs (join_resp->size);
461 if (dcsn_size < sizeof (*dcsn) + relay_size + join_resp_size)
463 LOG (GNUNET_ERROR_TYPE_DEBUG,
464 "Received invalid join decision message from multicast: %u < %u + %u + %u\n",
465 dcsn_size , sizeof (*dcsn), relay_size, join_resp_size);
467 is_admitted = GNUNET_SYSERR;
470 if (NULL != mem->join_dcsn_cb)
471 mem->join_dcsn_cb (grp->cb_cls, is_admitted, &hdcsn->peer,
472 relay_count, relays, join_resp);
475 //if (GNUNET_YES != is_admitted)
476 // GNUNET_MULTICAST_member_part (mem);
481 * Message handlers for an origin.
483 static struct GNUNET_CLIENT_MANAGER_MessageHandler origin_handlers[] =
485 { group_recv_disconnect, NULL, 0, 0, GNUNET_NO },
487 { group_recv_message, NULL,
488 GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE,
489 sizeof (struct GNUNET_MULTICAST_MessageHeader), GNUNET_YES },
491 { origin_recv_request, NULL,
492 GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST,
493 sizeof (struct GNUNET_MULTICAST_RequestHeader), GNUNET_YES },
495 { group_recv_fragment_ack, NULL,
496 GNUNET_MESSAGE_TYPE_MULTICAST_FRAGMENT_ACK,
497 sizeof (struct GNUNET_MessageHeader), GNUNET_YES },
499 { group_recv_join_request, NULL,
500 GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST,
501 sizeof (struct MulticastJoinRequestMessage), GNUNET_YES },
503 { group_recv_replay_request, NULL,
504 GNUNET_MESSAGE_TYPE_MULTICAST_REPLAY_REQUEST,
505 sizeof (struct MulticastReplayRequestMessage), GNUNET_NO },
507 { NULL, NULL, 0, 0, GNUNET_NO }
512 * Message handlers for a member.
514 static struct GNUNET_CLIENT_MANAGER_MessageHandler member_handlers[] =
516 { group_recv_disconnect, NULL, 0, 0, GNUNET_NO },
518 { group_recv_message, NULL,
519 GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE,
520 sizeof (struct GNUNET_MULTICAST_MessageHeader), GNUNET_YES },
522 { group_recv_fragment_ack, NULL,
523 GNUNET_MESSAGE_TYPE_MULTICAST_FRAGMENT_ACK,
524 sizeof (struct GNUNET_MessageHeader), GNUNET_YES },
526 { group_recv_join_request, NULL,
527 GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST,
528 sizeof (struct MulticastJoinRequestMessage), GNUNET_YES },
530 { member_recv_join_decision, NULL,
531 GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION,
532 sizeof (struct MulticastJoinDecisionMessage), GNUNET_YES },
534 { group_recv_replay_request, NULL,
535 GNUNET_MESSAGE_TYPE_MULTICAST_REPLAY_REQUEST,
536 sizeof (struct MulticastReplayRequestMessage), GNUNET_NO },
538 { member_recv_replay_response, NULL,
539 GNUNET_MESSAGE_TYPE_MULTICAST_REPLAY_RESPONSE,
540 sizeof (struct MulticastReplayRequestMessage), GNUNET_NO },
542 { NULL, NULL, 0, 0, GNUNET_NO }
547 group_cleanup (struct GNUNET_MULTICAST_Group *grp)
549 GNUNET_free (grp->connect_msg);
550 if (NULL != grp->disconnect_cb)
551 grp->disconnect_cb (grp->disconnect_cls);
556 origin_cleanup (void *cls)
558 struct GNUNET_MULTICAST_Origin *orig = cls;
559 group_cleanup (&orig->grp);
565 member_cleanup (void *cls)
567 struct GNUNET_MULTICAST_Member *mem = cls;
568 group_cleanup (&mem->grp);
574 * Function to call with the decision made for a join request.
576 * Must be called once and only once in response to an invocation of the
577 * #GNUNET_MULTICAST_JoinRequestCallback.
580 * Join request handle.
582 * #GNUNET_YES if the join is approved,
583 * #GNUNET_NO if it is disapproved,
584 * #GNUNET_SYSERR if we cannot answer the request.
586 * Number of relays given.
588 * Array of suggested peers that might be useful relays to use
589 * when joining the multicast group (essentially a list of peers that
590 * are already part of the multicast group and might thus be willing
591 * to help with routing). If empty, only this local peer (which must
592 * be the multicast origin) is a good candidate for building the
593 * multicast tree. Note that it is unnecessary to specify our own
594 * peer identity in this array.
596 * Message to send in response to the joining peer;
597 * can also be used to redirect the peer to a different group at the
598 * application layer; this response is to be transmitted to the
599 * peer that issued the request even if admission is denied.
601 struct GNUNET_MULTICAST_ReplayHandle *
602 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *join,
604 uint16_t relay_count,
605 const struct GNUNET_PeerIdentity *relays,
606 const struct GNUNET_MessageHeader *join_resp)
608 struct GNUNET_MULTICAST_Group *grp = join->group;
609 uint16_t join_resp_size = (NULL != join_resp) ? ntohs (join_resp->size) : 0;
610 uint16_t relay_size = relay_count * sizeof (*relays);
612 struct MulticastJoinDecisionMessageHeader * hdcsn;
613 struct MulticastJoinDecisionMessage *dcsn;
614 hdcsn = GNUNET_malloc (sizeof (*hdcsn) + sizeof (*dcsn)
615 + relay_size + join_resp_size);
616 hdcsn->header.size = htons (sizeof (*hdcsn) + sizeof (*dcsn)
617 + relay_size + join_resp_size);
618 hdcsn->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION);
619 hdcsn->member_pub_key = join->member_pub_key;
620 hdcsn->peer = join->peer;
622 dcsn = (struct MulticastJoinDecisionMessage *) &hdcsn[1];
623 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION);
624 dcsn->header.size = htons (sizeof (*dcsn) + relay_size + join_resp_size);
625 dcsn->is_admitted = htonl (is_admitted);
626 dcsn->relay_count = htonl (relay_count);
628 memcpy (&dcsn[1], relays, relay_size);
629 if (0 < join_resp_size)
630 memcpy (((char *) &dcsn[1]) + relay_size, join_resp, join_resp_size);
632 GNUNET_CLIENT_MANAGER_transmit (grp->client, &hdcsn->header);
640 * Replay a message fragment for the multicast group.
643 * Replay handle identifying which replay operation was requested.
645 * Replayed message fragment, NULL if not found / an error occurred.
647 * Error code. See enum GNUNET_MULTICAST_ReplayErrorCode
648 * If not #GNUNET_MULTICAST_REC_OK, the replay handle is invalidated.
651 GNUNET_MULTICAST_replay_response (struct GNUNET_MULTICAST_ReplayHandle *rh,
652 const struct GNUNET_MessageHeader *msg,
653 enum GNUNET_MULTICAST_ReplayErrorCode ec)
655 uint8_t msg_size = (NULL != msg) ? ntohs (msg->size) : 0;
656 struct MulticastReplayResponseMessage *
657 res = GNUNET_malloc (sizeof (*res) + msg_size);
658 *res = (struct MulticastReplayResponseMessage) {
660 .type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_REPLAY_RESPONSE),
661 .size = htons (sizeof (*res) + msg_size),
663 .fragment_id = rh->req.fragment_id,
664 .message_id = rh->req.message_id,
665 .fragment_offset = rh->req.fragment_offset,
666 .flags = rh->req.flags,
667 .error_code = htonl (ec),
670 if (GNUNET_MULTICAST_REC_OK == ec)
672 GNUNET_assert (NULL != msg);
673 memcpy (&res[1], msg, msg_size);
676 GNUNET_CLIENT_MANAGER_transmit (rh->grp->client, &res->header);
679 if (GNUNET_MULTICAST_REC_OK != ec)
685 * Indicate the end of the replay session.
687 * Invalidates the replay handle.
690 * Replay session to end.
693 GNUNET_MULTICAST_replay_response_end (struct GNUNET_MULTICAST_ReplayHandle *rh)
695 struct MulticastReplayResponseMessage end = {
697 .type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_REPLAY_RESPONSE_END),
698 .size = htons (sizeof (end)),
700 .fragment_id = rh->req.fragment_id,
701 .message_id = rh->req.message_id,
702 .fragment_offset = rh->req.fragment_offset,
703 .flags = rh->req.flags,
706 GNUNET_CLIENT_MANAGER_transmit (rh->grp->client, &end.header);
712 * Replay a message for the multicast group.
715 * Replay handle identifying which replay operation was requested.
717 * Function to call to get the message.
719 * Closure for @a notify.
722 GNUNET_MULTICAST_replay_response2 (struct GNUNET_MULTICAST_ReplayHandle *rh,
723 GNUNET_MULTICAST_ReplayTransmitNotify notify,
730 * Start a multicast group.
732 * Will advertise the origin in the P2P overlay network under the respective
733 * public key so that other peer can find this peer to join it. Peers that
734 * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
735 * either an existing group member or to the origin. If the joining is
736 * approved, the member is cleared for @e replay and will begin to receive
737 * messages transmitted to the group. If joining is disapproved, the failed
738 * candidate will be given a response. Members in the group can send messages
739 * to the origin (one at a time).
742 * Configuration to use.
744 * ECC key that will be used to sign messages for this
745 * multicast session; public key is used to identify the multicast group;
746 * @param max_fragment_id
747 * Maximum fragment ID already sent to the group.
749 * @param join_request_cb
750 * Function called to approve / disapprove joining of a peer.
751 * @param replay_frag_cb
752 * Function that can be called to replay a message fragment.
753 * @param replay_msg_cb
754 * Function that can be called to replay a message.
756 * Function called with message fragments from group members.
758 * Function called with the message fragments sent to the
759 * network by GNUNET_MULTICAST_origin_to_all(). These message fragments
760 * should be stored for answering replay requests later.
762 * Closure for the various callbacks that follow.
764 * @return Handle for the origin, NULL on error.
766 struct GNUNET_MULTICAST_Origin *
767 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
768 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key,
769 uint64_t max_fragment_id,
770 GNUNET_MULTICAST_JoinRequestCallback join_request_cb,
771 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
772 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
773 GNUNET_MULTICAST_RequestCallback request_cb,
774 GNUNET_MULTICAST_MessageCallback message_cb,
777 struct GNUNET_MULTICAST_Origin *orig = GNUNET_malloc (sizeof (*orig));
778 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
779 struct MulticastOriginStartMessage *start = GNUNET_malloc (sizeof (*start));
781 start->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_ORIGIN_START);
782 start->header.size = htons (sizeof (*start));
783 start->max_fragment_id = max_fragment_id;
784 memcpy (&start->group_key, priv_key, sizeof (*priv_key));
786 grp->connect_msg = (struct GNUNET_MessageHeader *) start;
787 grp->is_origin = GNUNET_YES;
791 grp->join_req_cb = join_request_cb;
792 grp->replay_frag_cb = replay_frag_cb;
793 grp->replay_msg_cb = replay_msg_cb;
794 grp->message_cb = message_cb;
796 orig->request_cb = request_cb;
798 grp->client = GNUNET_CLIENT_MANAGER_connect (cfg, "multicast", origin_handlers);
799 GNUNET_CLIENT_MANAGER_set_user_context_ (grp->client, orig, sizeof (*grp));
800 group_send_connect_msg (grp);
807 * Stop a multicast group.
810 * Multicast group to stop.
813 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *orig,
814 GNUNET_ContinuationCallback stop_cb,
817 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
819 grp->is_disconnecting = GNUNET_YES;
820 grp->disconnect_cb = stop_cb;
821 grp->disconnect_cls = stop_cls;
823 GNUNET_CLIENT_MANAGER_disconnect (orig->grp.client, GNUNET_YES,
824 &origin_cleanup, orig);
829 origin_to_all (struct GNUNET_MULTICAST_Origin *orig)
831 LOG (GNUNET_ERROR_TYPE_DEBUG, "%p origin_to_all()\n", orig);
832 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
833 struct GNUNET_MULTICAST_OriginTransmitHandle *tmit = &orig->tmit;
834 GNUNET_assert (GNUNET_YES == grp->in_transmit);
836 size_t buf_size = GNUNET_MULTICAST_FRAGMENT_MAX_SIZE;
837 struct GNUNET_MULTICAST_MessageHeader *msg = GNUNET_malloc (buf_size);
838 int ret = tmit->notify (tmit->notify_cls, &buf_size, &msg[1]);
840 if (! (GNUNET_YES == ret || GNUNET_NO == ret)
841 || GNUNET_MULTICAST_FRAGMENT_MAX_SIZE < buf_size)
843 LOG (GNUNET_ERROR_TYPE_ERROR,
844 "%p OriginTransmitNotify() returned error or invalid message size.\n",
846 /* FIXME: handle error */
851 if (GNUNET_NO == ret && 0 == buf_size)
853 LOG (GNUNET_ERROR_TYPE_DEBUG,
854 "%p OriginTransmitNotify() - transmission paused.\n", orig);
856 return; /* Transmission paused. */
859 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
860 msg->header.size = htons (sizeof (*msg) + buf_size);
861 msg->message_id = GNUNET_htonll (tmit->message_id);
862 msg->group_generation = tmit->group_generation;
863 msg->fragment_offset = GNUNET_htonll (tmit->fragment_offset);
864 tmit->fragment_offset += sizeof (*msg) + buf_size;
867 GNUNET_CLIENT_MANAGER_transmit (grp->client, &msg->header);
870 if (GNUNET_YES == ret)
871 grp->in_transmit = GNUNET_NO;
876 * Send a message to the multicast group.
879 * Handle to the multicast group.
881 * Application layer ID for the message. Opaque to multicast.
882 * @param group_generation
883 * Group generation of the message.
884 * Documented in struct GNUNET_MULTICAST_MessageHeader.
886 * Function to call to get the message.
888 * Closure for @a notify.
890 * @return Message handle on success,
891 * NULL on error (i.e. another request is already pending).
893 struct GNUNET_MULTICAST_OriginTransmitHandle *
894 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *orig,
896 uint64_t group_generation,
897 GNUNET_MULTICAST_OriginTransmitNotify notify,
900 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
901 if (GNUNET_YES == grp->in_transmit)
903 grp->in_transmit = GNUNET_YES;
905 struct GNUNET_MULTICAST_OriginTransmitHandle *tmit = &orig->tmit;
907 tmit->message_id = message_id;
908 tmit->fragment_offset = 0;
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.
922 * Transmission to cancel.
925 GNUNET_MULTICAST_origin_to_all_resume (struct GNUNET_MULTICAST_OriginTransmitHandle *th)
927 struct GNUNET_MULTICAST_Group *grp = &th->origin->grp;
928 if (0 != grp->acks_pending || GNUNET_YES != grp->in_transmit)
930 origin_to_all (th->origin);
935 * Cancel request for message transmission to multicast group.
938 * Transmission to cancel.
941 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginTransmitHandle *th)
943 th->origin->grp.in_transmit = GNUNET_NO;
948 * Join a multicast group.
950 * The entity joining is always the local peer. Further information about the
951 * candidate can be provided in the @a join_request message. If the join fails, the
952 * @a message_cb is invoked with a (failure) response and then with NULL. If
953 * the join succeeds, outstanding (state) messages and ongoing multicast
954 * messages will be given to the @a message_cb until the member decides to part
955 * the group. The @a replay_cb function may be called at any time by the
956 * multicast service to support relaying messages to other members of the group.
959 * Configuration to use.
961 * ECC public key that identifies the group to join.
963 * ECC key that identifies the member
964 * and used to sign requests sent to the origin.
966 * Peer ID of the origin to send unicast requsets to. If NULL,
967 * unicast requests are sent back via multiple hops on the reverse path
968 * of multicast messages.
970 * Number of peers in the @a relays array.
972 * Peer identities of members of the group, which serve as relays
973 * and can be used to join the group at. and send the @a join_request to.
974 * If empty, the @a join_request is sent directly to the @a origin.
976 * Application-dependent join message to be passed to the peer @a origin.
977 * @param join_request_cb
978 * Function called to approve / disapprove joining of a peer.
979 * @param join_decision_cb
980 * Function called to inform about the join decision.
981 * @param replay_frag_cb
982 * Function that can be called to replay message fragments
983 * this peer already knows from this group. NULL if this
984 * client is unable to support replay.
985 * @param replay_msg_cb
986 * Function that can be called to replay message fragments
987 * this peer already knows from this group. NULL if this
988 * client is unable to support replay.
990 * Function to be called for all message fragments we
991 * receive from the group, excluding those our @a replay_cb
994 * Closure for callbacks.
996 * @return Handle for the member, NULL on error.
998 struct GNUNET_MULTICAST_Member *
999 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
1000 const struct GNUNET_CRYPTO_EddsaPublicKey *group_pub_key,
1001 const struct GNUNET_CRYPTO_EcdsaPrivateKey *member_key,
1002 const struct GNUNET_PeerIdentity *origin,
1003 uint16_t relay_count,
1004 const struct GNUNET_PeerIdentity *relays,
1005 const struct GNUNET_MessageHeader *join_msg,
1006 GNUNET_MULTICAST_JoinRequestCallback join_request_cb,
1007 GNUNET_MULTICAST_JoinDecisionCallback join_decision_cb,
1008 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
1009 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
1010 GNUNET_MULTICAST_MessageCallback message_cb,
1013 struct GNUNET_MULTICAST_Member *mem = GNUNET_malloc (sizeof (*mem));
1014 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
1016 uint16_t relay_size = relay_count * sizeof (*relays);
1017 uint16_t join_msg_size = (NULL != join_msg) ? ntohs (join_msg->size) : 0;
1018 struct MulticastMemberJoinMessage *
1019 join = GNUNET_malloc (sizeof (*join) + relay_size + join_msg_size);
1020 join->header.size = htons (sizeof (*join) + relay_size + join_msg_size);
1021 join->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MEMBER_JOIN);
1022 join->group_pub_key = *group_pub_key;
1023 join->member_key = *member_key;
1024 join->origin = *origin;
1025 join->relay_count = ntohl (relay_count);
1027 memcpy (&join[1], relays, relay_size);
1028 if (0 < join_msg_size)
1029 memcpy (((char *) &join[1]) + relay_size, join_msg, join_msg_size);
1031 grp->connect_msg = (struct GNUNET_MessageHeader *) join;
1032 grp->is_origin = GNUNET_NO;
1035 mem->join_dcsn_cb = join_decision_cb;
1036 grp->join_req_cb = join_request_cb;
1037 grp->replay_frag_cb = replay_frag_cb;
1038 grp->replay_msg_cb = replay_msg_cb;
1039 grp->message_cb = message_cb;
1042 grp->client = GNUNET_CLIENT_MANAGER_connect (cfg, "multicast", member_handlers);
1043 GNUNET_CLIENT_MANAGER_set_user_context_ (grp->client, mem, sizeof (*grp));
1044 group_send_connect_msg (grp);
1051 * Part a multicast group.
1053 * Disconnects from all group members and invalidates the @a member handle.
1055 * An application-dependent part message can be transmitted beforehand using
1056 * #GNUNET_MULTICAST_member_to_origin())
1059 * Membership handle.
1062 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *mem,
1063 GNUNET_ContinuationCallback part_cb,
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%p Member parting.\n", mem);
1067 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
1069 grp->is_disconnecting = GNUNET_YES;
1070 grp->disconnect_cb = part_cb;
1071 grp->disconnect_cls = part_cls;
1073 mem->join_dcsn_cb = NULL;
1074 grp->join_req_cb = NULL;
1075 grp->message_cb = NULL;
1076 grp->replay_msg_cb = NULL;
1077 grp->replay_frag_cb = NULL;
1079 GNUNET_CLIENT_MANAGER_disconnect (mem->grp.client, GNUNET_YES,
1080 member_cleanup, mem);
1085 member_replay_request (struct GNUNET_MULTICAST_Member *mem,
1086 uint64_t fragment_id,
1087 uint64_t message_id,
1088 uint64_t fragment_offset,
1091 struct MulticastReplayRequestMessage rep = {
1093 .type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_REPLAY_REQUEST),
1094 .size = htons (sizeof (rep)),
1096 .fragment_id = GNUNET_htonll (fragment_id),
1097 .message_id = GNUNET_htonll (message_id),
1098 .fragment_offset = GNUNET_htonll (fragment_offset),
1099 .flags = GNUNET_htonll (flags),
1101 GNUNET_CLIENT_MANAGER_transmit (mem->grp.client, &rep.header);
1106 * Request a fragment to be replayed by fragment ID.
1108 * Useful if messages below the @e max_known_fragment_id given when joining are
1109 * needed and not known to the client.
1112 * Membership handle.
1113 * @param fragment_id
1114 * ID of a message fragment that this client would like to see replayed.
1116 * Additional flags for the replay request.
1117 * It is used and defined by GNUNET_MULTICAST_ReplayFragmentCallback
1119 * @return Replay request handle.
1121 struct GNUNET_MULTICAST_MemberReplayHandle *
1122 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *mem,
1123 uint64_t fragment_id,
1126 member_replay_request (mem, fragment_id, 0, 0, flags);
1132 * Request a message fragment to be replayed.
1134 * Useful if messages below the @e max_known_fragment_id given when joining are
1135 * needed and not known to the client.
1138 * Membership handle.
1140 * ID of the message this client would like to see replayed.
1141 * @param fragment_offset
1142 * Offset of the fragment within the message to replay.
1144 * Additional flags for the replay request.
1145 * It is used & defined by GNUNET_MULTICAST_ReplayMessageCallback
1147 * @return Replay request handle, NULL on error.
1149 struct GNUNET_MULTICAST_MemberReplayHandle *
1150 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *mem,
1151 uint64_t message_id,
1152 uint64_t fragment_offset,
1155 member_replay_request (mem, 0, message_id, fragment_offset, flags);
1161 member_to_origin (struct GNUNET_MULTICAST_Member *mem)
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, "member_to_origin()\n");
1164 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
1165 struct GNUNET_MULTICAST_MemberTransmitHandle *tmit = &mem->tmit;
1166 GNUNET_assert (GNUNET_YES == grp->in_transmit);
1168 size_t buf_size = GNUNET_MULTICAST_FRAGMENT_MAX_SIZE;
1169 struct GNUNET_MULTICAST_RequestHeader *req = GNUNET_malloc (buf_size);
1170 int ret = tmit->notify (tmit->notify_cls, &buf_size, &req[1]);
1172 if (! (GNUNET_YES == ret || GNUNET_NO == ret)
1173 || GNUNET_MULTICAST_FRAGMENT_MAX_SIZE < buf_size)
1175 LOG (GNUNET_ERROR_TYPE_ERROR,
1176 "MemberTransmitNotify() returned error or invalid message size. "
1177 "ret=%d, buf_size=%u\n", ret, buf_size);
1178 /* FIXME: handle error */
1183 if (GNUNET_NO == ret && 0 == buf_size)
1185 /* Transmission paused. */
1190 req->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST);
1191 req->header.size = htons (sizeof (*req) + buf_size);
1192 req->request_id = GNUNET_htonll (tmit->request_id);
1193 req->fragment_offset = GNUNET_ntohll (tmit->fragment_offset);
1194 tmit->fragment_offset += sizeof (*req) + buf_size;
1196 GNUNET_CLIENT_MANAGER_transmit (grp->client, &req->header);
1199 if (GNUNET_YES == ret)
1200 grp->in_transmit = GNUNET_NO;
1205 * Send a message to the origin of the multicast group.
1208 * Membership handle.
1210 * Application layer ID for the request. Opaque to multicast.
1212 * Callback to call to get the message.
1214 * Closure for @a notify.
1216 * @return Handle to cancel request, NULL on error (i.e. request already pending).
1218 struct GNUNET_MULTICAST_MemberTransmitHandle *
1219 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *mem,
1220 uint64_t request_id,
1221 GNUNET_MULTICAST_MemberTransmitNotify notify,
1224 if (GNUNET_YES == mem->grp.in_transmit)
1226 mem->grp.in_transmit = GNUNET_YES;
1228 struct GNUNET_MULTICAST_MemberTransmitHandle *tmit = &mem->tmit;
1230 tmit->request_id = request_id;
1231 tmit->fragment_offset = 0;
1232 tmit->notify = notify;
1233 tmit->notify_cls = notify_cls;
1235 member_to_origin (mem);
1241 * Resume message transmission to origin.
1244 * Transmission to cancel.
1247 GNUNET_MULTICAST_member_to_origin_resume (struct GNUNET_MULTICAST_MemberTransmitHandle *th)
1249 struct GNUNET_MULTICAST_Group *grp = &th->member->grp;
1250 if (0 != grp->acks_pending || GNUNET_YES != grp->in_transmit)
1252 member_to_origin (th->member);
1257 * Cancel request for message transmission to origin.
1260 * Transmission to cancel.
1263 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberTransmitHandle *th)
1265 th->member->grp.in_transmit = GNUNET_NO;
1269 /* end of multicast_api.c */