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; 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_MembershipTestCallback member_test_cb;
85 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb;
86 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb;
87 GNUNET_MULTICAST_MessageCallback message_cb;
91 * Function called after disconnected from the service.
93 GNUNET_ContinuationCallback disconnect_cb;
96 * Closure for @a disconnect_cb.
101 * Are we currently transmitting a message?
106 * Is this the origin or a member?
111 * Is this channel in the process of disconnecting from the service?
112 * #GNUNET_YES or #GNUNET_NO
114 uint8_t is_disconnecting;
119 * Handle for the origin of a multicast group.
121 struct GNUNET_MULTICAST_Origin
123 struct GNUNET_MULTICAST_Group grp;
124 struct GNUNET_MULTICAST_OriginTransmitHandle tmit;
126 GNUNET_MULTICAST_RequestCallback request_cb;
131 * Handle for a multicast group member.
133 struct GNUNET_MULTICAST_Member
135 struct GNUNET_MULTICAST_Group grp;
136 struct GNUNET_MULTICAST_MemberTransmitHandle tmit;
138 GNUNET_MULTICAST_JoinDecisionCallback join_dcsn_cb;
140 uint64_t next_fragment_id;
145 * Handle that identifies a join request.
147 * Used to match calls to #GNUNET_MULTICAST_JoinCallback to the
148 * corresponding calls to #GNUNET_MULTICAST_join_decision().
150 struct GNUNET_MULTICAST_JoinHandle
152 struct GNUNET_MULTICAST_Group *group;
155 * Public key of the member requesting join.
157 struct GNUNET_CRYPTO_EcdsaPublicKey member_key;
160 * Peer identity of the member requesting join.
162 struct GNUNET_PeerIdentity peer;
167 * Handle to pass back for the answer of a membership test.
169 struct GNUNET_MULTICAST_MembershipTestHandle
175 * Opaque handle to a replay request from the multicast service.
177 struct GNUNET_MULTICAST_ReplayHandle
183 * Handle for a replay request.
185 struct GNUNET_MULTICAST_MemberReplayHandle
191 * Send first message to the service after connecting.
194 group_send_connect_msg (struct GNUNET_MULTICAST_Group *grp)
196 uint16_t cmsg_size = ntohs (grp->connect_msg->size);
197 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
198 memcpy (cmsg, grp->connect_msg, cmsg_size);
199 GNUNET_CLIENT_MANAGER_transmit_now (grp->client, cmsg);
204 * Got disconnected from service. Reconnect.
207 group_recv_disconnect (void *cls,
208 struct GNUNET_CLIENT_MANAGER_Connection *client,
209 const struct GNUNET_MessageHeader *msg)
211 struct GNUNET_MULTICAST_Group *
212 grp = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
213 GNUNET_CLIENT_MANAGER_reconnect (client);
214 group_send_connect_msg (grp);
219 * Receive join request from service.
222 group_recv_join_request (void *cls,
223 struct GNUNET_CLIENT_MANAGER_Connection *client,
224 const struct GNUNET_MessageHeader *msg)
226 struct GNUNET_MULTICAST_Group *grp;
227 const struct MulticastJoinRequestMessage *jreq;
228 struct GNUNET_MULTICAST_JoinHandle *jh;
229 const struct GNUNET_MessageHeader *jmsg;
231 grp = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
237 if (NULL == grp->join_req_cb)
239 /* FIXME: this fails to check that 'msg' is well-formed! */
240 jreq = (const struct MulticastJoinRequestMessage *) msg;
241 if (sizeof (*jreq) + sizeof (*jmsg) <= ntohs (jreq->header.size))
242 jmsg = (const struct GNUNET_MessageHeader *) &jreq[1];
245 jh = GNUNET_malloc (sizeof (*jh));
247 jh->member_key = jreq->member_key;
248 jh->peer = jreq->peer;
249 grp->join_req_cb (grp->cb_cls, &jreq->member_key, jmsg, jh);
254 * Receive multicast message from service.
257 group_recv_message (void *cls,
258 struct GNUNET_CLIENT_MANAGER_Connection *client,
259 const struct GNUNET_MessageHeader *msg)
261 struct GNUNET_MULTICAST_Group *
262 grp = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
263 struct GNUNET_MULTICAST_MessageHeader *
264 mmsg = (struct GNUNET_MULTICAST_MessageHeader *) msg;
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267 "Calling message callback with a message of size %u.\n",
268 ntohs (mmsg->header.size));
270 if (NULL != grp->message_cb)
271 grp->message_cb (grp->cb_cls, mmsg);
276 * Origin receives uniquest request from a member.
279 origin_recv_request (void *cls,
280 struct GNUNET_CLIENT_MANAGER_Connection *client,
281 const struct GNUNET_MessageHeader *msg)
283 struct GNUNET_MULTICAST_Group *grp;
284 struct GNUNET_MULTICAST_Origin *
285 orig = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
287 struct GNUNET_MULTICAST_RequestHeader *
288 req = (struct GNUNET_MULTICAST_RequestHeader *) msg;
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Calling request callback with a request of size %u.\n",
292 ntohs (req->header.size));
294 if (NULL != orig->request_cb)
295 orig->request_cb (grp->cb_cls, req);
300 * Member receives join decision.
303 member_recv_join_decision (void *cls,
304 struct GNUNET_CLIENT_MANAGER_Connection *client,
305 const struct GNUNET_MessageHeader *msg)
307 struct GNUNET_MULTICAST_Group *grp;
308 struct GNUNET_MULTICAST_Member *
309 mem = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
312 const struct MulticastJoinDecisionMessageHeader *
313 hdcsn = (const struct MulticastJoinDecisionMessageHeader *) msg;
314 const struct MulticastJoinDecisionMessage *
315 dcsn = (const struct MulticastJoinDecisionMessage *) &hdcsn[1];
317 uint16_t dcsn_size = ntohs (dcsn->header.size);
318 int is_admitted = ntohl (dcsn->is_admitted);
320 LOG (GNUNET_ERROR_TYPE_DEBUG,
321 "%p Member got join decision from multicast: %d\n",
324 const struct GNUNET_MessageHeader *join_resp = NULL;
325 uint16_t join_resp_size = 0;
327 uint16_t relay_count = ntohl (dcsn->relay_count);
328 const struct GNUNET_PeerIdentity *relays = NULL;
329 uint16_t relay_size = relay_count * sizeof (*relays);
330 if (0 < relay_count && dcsn_size < sizeof (*dcsn) + relay_size)
331 relays = (struct GNUNET_PeerIdentity *) &dcsn[1];
333 if (sizeof (*dcsn) + relay_size + sizeof (*join_resp) <= dcsn_size)
335 join_resp = (const struct GNUNET_MessageHeader *) &dcsn[1];
336 join_resp_size = ntohs (join_resp->size);
338 if (dcsn_size < sizeof (*dcsn) + relay_size + join_resp_size)
340 LOG (GNUNET_ERROR_TYPE_DEBUG,
341 "Received invalid join decision message from multicast.\n");
343 is_admitted = GNUNET_SYSERR;
346 if (NULL != mem->join_dcsn_cb)
347 mem->join_dcsn_cb (grp->cb_cls, is_admitted, &hdcsn->peer,
348 relay_count, relays, join_resp);
351 //if (GNUNET_YES != is_admitted)
352 // GNUNET_MULTICAST_member_part (mem);
357 * Message handlers for an origin.
359 static struct GNUNET_CLIENT_MANAGER_MessageHandler origin_handlers[] =
361 { &group_recv_disconnect, NULL, 0, 0, GNUNET_NO },
363 { &group_recv_message, NULL,
364 GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE,
365 sizeof (struct GNUNET_MULTICAST_MessageHeader), GNUNET_YES },
367 { &origin_recv_request, NULL,
368 GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST,
369 sizeof (struct GNUNET_MULTICAST_RequestHeader), GNUNET_YES },
371 { &group_recv_join_request, NULL,
372 GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST,
373 sizeof (struct MulticastJoinRequestMessage), GNUNET_YES },
375 { NULL, NULL, 0, 0, GNUNET_NO }
380 * Message handlers for a member.
382 static struct GNUNET_CLIENT_MANAGER_MessageHandler member_handlers[] =
384 { &group_recv_disconnect, NULL, 0, 0, GNUNET_NO },
386 { &group_recv_message, NULL,
387 GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE,
388 sizeof (struct GNUNET_MULTICAST_MessageHeader), GNUNET_YES },
390 { &group_recv_join_request, NULL,
391 GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST,
392 sizeof (struct MulticastJoinRequestMessage), GNUNET_YES },
394 { &member_recv_join_decision, NULL,
395 GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION,
396 sizeof (struct MulticastJoinDecisionMessage), GNUNET_YES },
398 { NULL, NULL, 0, 0, GNUNET_NO }
403 group_cleanup (struct GNUNET_MULTICAST_Group *grp)
405 GNUNET_free (grp->connect_msg);
406 if (NULL != grp->disconnect_cb)
407 grp->disconnect_cb (grp->disconnect_cls);
412 origin_cleanup (void *cls)
414 struct GNUNET_MULTICAST_Origin *orig = cls;
415 group_cleanup (&orig->grp);
421 member_cleanup (void *cls)
423 struct GNUNET_MULTICAST_Member *mem = cls;
424 group_cleanup (&mem->grp);
430 * Function to call with the decision made for a join request.
432 * Must be called once and only once in response to an invocation of the
433 * #GNUNET_MULTICAST_JoinRequestCallback.
435 * @param join Join request handle.
436 * @param is_admitted #GNUNET_YES if the join is approved,
437 * #GNUNET_NO if it is disapproved,
438 * #GNUNET_SYSERR if we cannot answer the request.
439 * @param relay_count Number of relays given.
440 * @param relays Array of suggested peers that might be useful relays to use
441 * when joining the multicast group (essentially a list of peers that
442 * are already part of the multicast group and might thus be willing
443 * to help with routing). If empty, only this local peer (which must
444 * be the multicast origin) is a good candidate for building the
445 * multicast tree. Note that it is unnecessary to specify our own
446 * peer identity in this array.
447 * @param join_resp Message to send in response to the joining peer;
448 * can also be used to redirect the peer to a different group at the
449 * application layer; this response is to be transmitted to the
450 * peer that issued the request even if admission is denied.
452 struct GNUNET_MULTICAST_ReplayHandle *
453 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *join,
455 uint16_t relay_count,
456 const struct GNUNET_PeerIdentity *relays,
457 const struct GNUNET_MessageHeader *join_resp)
459 struct GNUNET_MULTICAST_Group *grp = join->group;
460 uint16_t join_resp_size = (NULL != join_resp) ? ntohs (join_resp->size) : 0;
461 uint16_t relay_size = relay_count * sizeof (*relays);
463 struct MulticastJoinDecisionMessageHeader * hdcsn;
464 struct MulticastJoinDecisionMessage *dcsn;
465 hdcsn = GNUNET_malloc (sizeof (*hdcsn) + sizeof (*dcsn)
466 + relay_size + join_resp_size);
467 hdcsn->header.size = htons (sizeof (*hdcsn) + sizeof (*dcsn)
468 + relay_size + join_resp_size);
469 hdcsn->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION);
470 hdcsn->member_key = join->member_key;
471 hdcsn->peer = join->peer;
473 dcsn = (struct MulticastJoinDecisionMessage *) &hdcsn[1];
474 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION);
475 dcsn->header.size = htons (sizeof (*dcsn) + relay_size + join_resp_size);
476 dcsn->is_admitted = htonl (is_admitted);
477 dcsn->relay_count = htonl (relay_count);
479 memcpy (&dcsn[1], relays, relay_size);
480 if (0 < join_resp_size)
481 memcpy (((char *) &dcsn[1]) + relay_size, join_resp, join_resp_size);
483 GNUNET_CLIENT_MANAGER_transmit (grp->client, &hdcsn->header);
490 * Call informing multicast about the decision taken for a membership test.
492 * @param mth Handle that was given for the query.
493 * @param result #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
494 * #GNUNET_SYSERR if we cannot answer the membership test.
497 GNUNET_MULTICAST_membership_test_result (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
504 * Replay a message fragment for the multicast group.
506 * @param rh Replay handle identifying which replay operation was requested.
507 * @param msg Replayed message fragment, NULL if unknown/error.
508 * @param ec Error code.
511 GNUNET_MULTICAST_replay_response (struct GNUNET_MULTICAST_ReplayHandle *rh,
512 const struct GNUNET_MessageHeader *msg,
513 enum GNUNET_MULTICAST_ReplayErrorCode ec)
519 * Indicate the end of the replay session.
521 * Invalidates the replay handle.
523 * @param rh Replay session to end.
526 GNUNET_MULTICAST_replay_response_end (struct GNUNET_MULTICAST_ReplayHandle *rh)
532 * Replay a message for the multicast group.
534 * @param rh Replay handle identifying which replay operation was requested.
535 * @param notify Function to call to get the message.
536 * @param notify_cls Closure for @a notify.
539 GNUNET_MULTICAST_replay_response2 (struct GNUNET_MULTICAST_ReplayHandle *rh,
540 GNUNET_MULTICAST_ReplayTransmitNotify notify,
547 * Start a multicast group.
549 * Will advertise the origin in the P2P overlay network under the respective
550 * public key so that other peer can find this peer to join it. Peers that
551 * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
552 * either an existing group member or to the origin. If the joining is
553 * approved, the member is cleared for @e replay and will begin to receive
554 * messages transmitted to the group. If joining is disapproved, the failed
555 * candidate will be given a response. Members in the group can send messages
556 * to the origin (one at a time).
558 * @param cfg Configuration to use.
559 * @param priv_key ECC key that will be used to sign messages for this
560 * multicast session; public key is used to identify the multicast group;
561 * @param max_fragment_id Maximum fragment ID already sent to the group.
563 * @param join_request_cb Function called to approve / disapprove joining of a peer.
564 * @param member_test_cb Function multicast can use to test group membership.
565 * @param replay_frag_cb Function that can be called to replay a message fragment.
566 * @param replay_msg_cb Function that can be called to replay a message.
567 * @param request_cb Function called with message fragments from group members.
568 * @param message_cb Function called with the message fragments sent to the
569 * network by GNUNET_MULTICAST_origin_to_all(). These message fragments
570 * should be stored for answering replay requests later.
571 * @param cls Closure for the various callbacks that follow.
573 * @return Handle for the origin, NULL on error.
575 struct GNUNET_MULTICAST_Origin *
576 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
577 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key,
578 uint64_t max_fragment_id,
579 GNUNET_MULTICAST_JoinRequestCallback join_request_cb,
580 GNUNET_MULTICAST_MembershipTestCallback member_test_cb,
581 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
582 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
583 GNUNET_MULTICAST_RequestCallback request_cb,
584 GNUNET_MULTICAST_MessageCallback message_cb,
587 struct GNUNET_MULTICAST_Origin *orig = GNUNET_malloc (sizeof (*orig));
588 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
589 struct MulticastOriginStartMessage *start = GNUNET_malloc (sizeof (*start));
591 start->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_ORIGIN_START);
592 start->header.size = htons (sizeof (*start));
593 start->max_fragment_id = max_fragment_id;
594 memcpy (&start->group_key, priv_key, sizeof (*priv_key));
596 grp->connect_msg = (struct GNUNET_MessageHeader *) start;
597 grp->is_origin = GNUNET_YES;
601 grp->join_req_cb = join_request_cb;
602 grp->member_test_cb = member_test_cb;
603 grp->replay_frag_cb = replay_frag_cb;
604 grp->replay_msg_cb = replay_msg_cb;
605 grp->message_cb = message_cb;
607 orig->request_cb = request_cb;
609 grp->client = GNUNET_CLIENT_MANAGER_connect (cfg, "multicast", origin_handlers);
610 GNUNET_CLIENT_MANAGER_set_user_context_ (grp->client, orig, sizeof (*grp));
611 group_send_connect_msg (grp);
618 * Stop a multicast group.
620 * @param origin Multicast group to stop.
623 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *orig,
624 GNUNET_ContinuationCallback stop_cb,
627 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
629 grp->is_disconnecting = GNUNET_YES;
630 grp->disconnect_cb = stop_cb;
631 grp->disconnect_cls = stop_cls;
633 GNUNET_CLIENT_MANAGER_disconnect (orig->grp.client, GNUNET_YES,
634 &origin_cleanup, orig);
639 origin_to_all (struct GNUNET_MULTICAST_Origin *orig)
641 LOG (GNUNET_ERROR_TYPE_DEBUG, "origin_to_all()\n");
642 struct GNUNET_MULTICAST_Group *grp = &orig->grp;
643 struct GNUNET_MULTICAST_OriginTransmitHandle *tmit = &orig->tmit;
645 size_t buf_size = GNUNET_MULTICAST_FRAGMENT_MAX_SIZE;
646 struct GNUNET_MULTICAST_MessageHeader *msg = GNUNET_malloc (buf_size);
647 int ret = tmit->notify (tmit->notify_cls, &buf_size, &msg[1]);
649 if (! (GNUNET_YES == ret || GNUNET_NO == ret)
650 || GNUNET_MULTICAST_FRAGMENT_MAX_SIZE < buf_size)
652 LOG (GNUNET_ERROR_TYPE_ERROR,
653 "OriginTransmitNotify() returned error or invalid message size.\n");
654 /* FIXME: handle error */
659 if (GNUNET_NO == ret && 0 == buf_size)
662 return; /* Transmission paused. */
665 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
666 msg->header.size = htons (sizeof (*msg) + buf_size);
667 msg->message_id = GNUNET_htonll (tmit->message_id);
668 msg->group_generation = tmit->group_generation;
669 msg->fragment_offset = GNUNET_htonll (tmit->fragment_offset);
670 tmit->fragment_offset += sizeof (*msg) + buf_size;
672 GNUNET_CLIENT_MANAGER_transmit (grp->client, &msg->header);
677 * Send a message to the multicast group.
679 * @param orig Handle to the multicast group.
680 * @param message_id Application layer ID for the message. Opaque to multicast.
681 * @param group_generation Group generation of the message.
682 * Documented in struct GNUNET_MULTICAST_MessageHeader.
683 * @param notify Function to call to get the message.
684 * @param notify_cls Closure for @a notify.
686 * @return Message handle on success,
687 * NULL on error (i.e. another request is already pending).
689 struct GNUNET_MULTICAST_OriginTransmitHandle *
690 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *orig,
692 uint64_t group_generation,
693 GNUNET_MULTICAST_OriginTransmitNotify notify,
697 if (GNUNET_YES == orig->grp.in_transmit)
699 orig->grp.in_transmit = GNUNET_YES;
702 struct GNUNET_MULTICAST_OriginTransmitHandle *tmit = &orig->tmit;
704 tmit->message_id = message_id;
705 tmit->group_generation = group_generation;
706 tmit->notify = notify;
707 tmit->notify_cls = notify_cls;
709 origin_to_all (orig);
715 * Resume message transmission to multicast group.
717 * @param th Transmission to cancel.
720 GNUNET_MULTICAST_origin_to_all_resume (struct GNUNET_MULTICAST_OriginTransmitHandle *th)
722 origin_to_all (th->origin);
727 * Cancel request for message transmission to multicast group.
729 * @param th Transmission to cancel.
732 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginTransmitHandle *th)
738 * Join a multicast group.
740 * The entity joining is always the local peer. Further information about the
741 * candidate can be provided in the @a join_request message. If the join fails, the
742 * @a message_cb is invoked with a (failure) response and then with NULL. If
743 * the join succeeds, outstanding (state) messages and ongoing multicast
744 * messages will be given to the @a message_cb until the member decides to part
745 * the group. The @a test_cb and @a replay_cb functions may be called at
746 * anytime by the multicast service to support relaying messages to other
747 * members of the group.
749 * @param cfg Configuration to use.
750 * @param group_key ECC public key that identifies the group to join.
751 * @param member_key ECC key that identifies the member and used to sign
752 * requests sent to the origin.
753 * @param origin Peer ID of the origin to send unicast requsets to. If NULL,
754 * unicast requests are sent back via multiple hops on the reverse path
755 * of multicast messages.
756 * @param relay_count Number of peers in the @a relays array.
757 * @param relays Peer identities of members of the group, which serve as relays
758 * and can be used to join the group at. and send the @a join_request to.
759 * If empty, the @a join_request is sent directly to the @a origin.
760 * @param join_msg Application-dependent join message to be passed to the peer
762 * @param join_request_cb Function called to approve / disapprove joining of a peer.
763 * @param join_decision_cb Function called to inform about the join decision.
764 * @param member_test_cb Function multicast can use to test group membership.
765 * @param replay_frag_cb Function that can be called to replay message fragments
766 * this peer already knows from this group. NULL if this
767 * client is unable to support replay.
768 * @param replay_msg_cb Function that can be called to replay message fragments
769 * this peer already knows from this group. NULL if this
770 * client is unable to support replay.
771 * @param message_cb Function to be called for all message fragments we
772 * receive from the group, excluding those our @a replay_cb
774 * @param cls Closure for callbacks.
775 * @return Handle for the member, NULL on error.
777 struct GNUNET_MULTICAST_Member *
778 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
779 const struct GNUNET_CRYPTO_EddsaPublicKey *group_key,
780 const struct GNUNET_CRYPTO_EcdsaPrivateKey *member_key,
781 const struct GNUNET_PeerIdentity *origin,
782 uint16_t relay_count,
783 const struct GNUNET_PeerIdentity *relays,
784 const struct GNUNET_MessageHeader *join_msg,
785 GNUNET_MULTICAST_JoinRequestCallback join_request_cb,
786 GNUNET_MULTICAST_JoinDecisionCallback join_decision_cb,
787 GNUNET_MULTICAST_MembershipTestCallback member_test_cb,
788 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
789 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
790 GNUNET_MULTICAST_MessageCallback message_cb,
793 struct GNUNET_MULTICAST_Member *mem = GNUNET_malloc (sizeof (*mem));
794 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
796 uint16_t relay_size = relay_count * sizeof (*relays);
797 uint16_t join_msg_size = (NULL != join_msg) ? ntohs (join_msg->size) : 0;
798 struct MulticastMemberJoinMessage *
799 join = GNUNET_malloc (sizeof (*join) + relay_size + join_msg_size);
800 join->header.size = htons (sizeof (*join) + relay_size + join_msg_size);
801 join->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MEMBER_JOIN);
802 join->group_key = *group_key;
803 join->member_key = *member_key;
804 join->origin = *origin;
806 memcpy (&join[1], relays, relay_size);
807 if (0 < join_msg_size)
808 memcpy (((char *) &join[1]) + relay_size, join_msg, join_msg_size);
810 grp->connect_msg = (struct GNUNET_MessageHeader *) join;
811 grp->is_origin = GNUNET_NO;
814 mem->join_dcsn_cb = join_decision_cb;
815 grp->join_req_cb = join_request_cb;
816 grp->member_test_cb = member_test_cb;
817 grp->replay_frag_cb = replay_frag_cb;
818 grp->message_cb = message_cb;
821 grp->client = GNUNET_CLIENT_MANAGER_connect (cfg, "multicast", member_handlers);
822 GNUNET_CLIENT_MANAGER_set_user_context_ (grp->client, mem, sizeof (*grp));
823 group_send_connect_msg (grp);
830 * Part a multicast group.
832 * Disconnects from all group members and invalidates the @a member handle.
834 * An application-dependent part message can be transmitted beforehand using
835 * #GNUNET_MULTICAST_member_to_origin())
837 * @param member Membership handle.
840 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *mem,
841 GNUNET_ContinuationCallback part_cb,
844 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
846 grp->is_disconnecting = GNUNET_YES;
847 grp->disconnect_cb = part_cb;
848 grp->disconnect_cls = part_cls;
850 GNUNET_CLIENT_MANAGER_disconnect (mem->grp.client, GNUNET_YES,
851 &member_cleanup, mem);
856 * Request a fragment to be replayed by fragment ID.
858 * Useful if messages below the @e max_known_fragment_id given when joining are
859 * needed and not known to the client.
861 * @param member Membership handle.
862 * @param fragment_id ID of a message fragment that this client would like to
864 * @param flags Additional flags for the replay request. It is used and defined
865 * by the replay callback. FIXME: which replay callback? FIXME: use enum?
866 * FIXME: why not pass reply cb here?
867 * @return Replay request handle, NULL on error.
869 struct GNUNET_MULTICAST_MemberReplayHandle *
870 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *member,
871 uint64_t fragment_id,
879 * Request a message fragment to be replayed.
881 * Useful if messages below the @e max_known_fragment_id given when joining are
882 * needed and not known to the client.
884 * @param member Membership handle.
885 * @param message_id ID of the message this client would like to see replayed.
886 * @param fragment_offset Offset of the fragment within the message to replay.
887 * @param flags Additional flags for the replay request. It is used & defined
888 * by the replay callback.
889 * @param result_cb Function to be called for the replayed message.
890 * @param result_cb_cls Closure for @a result_cb.
891 * @return Replay request handle, NULL on error.
893 struct GNUNET_MULTICAST_MemberReplayHandle *
894 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *member,
896 uint64_t fragment_offset,
898 GNUNET_MULTICAST_ResultCallback result_cb,
906 * Cancel a replay request.
908 * @param rh Request to cancel.
911 GNUNET_MULTICAST_member_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh)
917 member_to_origin (struct GNUNET_MULTICAST_Member *mem)
919 LOG (GNUNET_ERROR_TYPE_DEBUG, "member_to_origin()\n");
920 struct GNUNET_MULTICAST_Group *grp = &mem->grp;
921 struct GNUNET_MULTICAST_MemberTransmitHandle *tmit = &mem->tmit;
923 size_t buf_size = GNUNET_MULTICAST_FRAGMENT_MAX_SIZE;
924 struct GNUNET_MULTICAST_RequestHeader *req = GNUNET_malloc (buf_size);
925 int ret = tmit->notify (tmit->notify_cls, &buf_size, &req[1]);
927 if (! (GNUNET_YES == ret || GNUNET_NO == ret)
928 || GNUNET_MULTICAST_FRAGMENT_MAX_SIZE < buf_size)
930 LOG (GNUNET_ERROR_TYPE_ERROR,
931 "MemberTransmitNotify() returned error or invalid message size.\n");
932 /* FIXME: handle error */
937 if (GNUNET_NO == ret && 0 == buf_size)
939 /* Transmission paused. */
944 req->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST);
945 req->header.size = htons (sizeof (*req) + buf_size);
946 req->request_id = GNUNET_htonll (tmit->request_id);
947 req->fragment_offset = GNUNET_ntohll (tmit->fragment_offset);
948 tmit->fragment_offset += sizeof (*req) + buf_size;
950 GNUNET_CLIENT_MANAGER_transmit (grp->client, &req->header);
955 * Send a message to the origin of the multicast group.
957 * @param mem Membership handle.
958 * @param request_id Application layer ID for the request. Opaque to multicast.
959 * @param notify Callback to call to get the message.
960 * @param notify_cls Closure for @a notify.
961 * @return Handle to cancel request, NULL on error (i.e. request already pending).
963 struct GNUNET_MULTICAST_MemberTransmitHandle *
964 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *mem,
966 GNUNET_MULTICAST_MemberTransmitNotify notify,
970 if (GNUNET_YES == mem->grp.in_transmit)
972 mem->grp.in_transmit = GNUNET_YES;
975 struct GNUNET_MULTICAST_MemberTransmitHandle *tmit = &mem->tmit;
977 tmit->request_id = request_id;
978 tmit->notify = notify;
979 tmit->notify_cls = notify_cls;
981 member_to_origin (mem);
987 * Resume message transmission to origin.
989 * @param th Transmission to cancel.
992 GNUNET_MULTICAST_member_to_origin_resume (struct GNUNET_MULTICAST_MemberTransmitHandle *th)
994 member_to_origin (th->member);
999 * Cancel request for message transmission to origin.
1001 * @param th Transmission to cancel.
1004 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberTransmitHandle *th)
1009 /* end of multicast_api.c */