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 include/gnunet_multicast_service.h
23 * @brief multicast service; establish tunnels to distant peers
24 * @author Christian Grothoff
25 * @author Gabor X Toth
28 #ifndef GNUNET_MULTICAST_SERVICE_H
29 #define GNUNET_MULTICAST_SERVICE_H
34 #if 0 /* keep Emacsens' auto-indent happy */
39 #include "gnunet_util_lib.h"
40 #include "gnunet_transport_service.h"
43 * Version number of GNUnet-multicast API.
45 #define GNUNET_MULTICAST_VERSION 0x00000000
49 * Opaque handle for a multicast group member.
51 struct GNUNET_MULTICAST_Member;
54 * Handle for the origin of a multicast group.
56 struct GNUNET_MULTICAST_Origin;
59 * Policy flags for the group.
61 enum GNUNET_MULTICAST_GroupFlags
64 * Admission must be confirmed by the origin.
66 GNUNET_MULTICAST_GROUP_ADMISSION_CONTROL = 1 << 0,
69 * Past messages are only available to peers who were a member at the time
70 * they were sent to the group.
72 GNUNET_MULTICAST_GROUP_RESTRICTED_HISTORY = 1 << 1,
76 * Group membership policies.
78 enum GNUNET_MULTICAST_GroupPolicy
81 * Anyone can join the group, without announcing his presence;
82 * all messages are always public and can be distributed freely.
83 * Joins may be announced, but this is not required.
85 GNUNET_MULTICAST_GROUP_ANONYMOUS = 0,
88 * Origin must approve membership to the group, messages must only be
89 * distributed to current group members. This includes the group
90 * state as well as transient messages.
92 GNUNET_MULTICAST_GROUP_PRIVATE
93 = GNUNET_MULTICAST_GROUP_ADMISSION_CONTROL
94 | GNUNET_MULTICAST_GROUP_RESTRICTED_HISTORY,
98 * Anyone can freely join the group (no approval required);
99 * however, messages must only be distributed to current group
100 * members, so the origin must still acknowledge that the member
101 * joined before transient messages are delivered. As approval is
102 * guaranteed, the presistent group state can be synchronized freely
103 * immediately, prior to origin confirmation.
105 GNUNET_MULTICAST_GROUP_OPEN
106 = GNUNET_MULTICAST_GROUP_RESTRICTED_HISTORY,
109 * Origin must approve membership to the group, but past messages can be
110 * freely distributed to members.
112 GNUNET_MULTICAST_GROUP_CLOSED
113 = GNUNET_MULTICAST_GROUP_ADMISSION_CONTROL,
119 enum GNUNET_MULTICAST_MessageFlags
122 * First fragment of a message.
124 GNUNET_MULTICAST_MESSAGE_FIRST_FRAGMENT = 1 << 0,
127 * Last fragment of a message.
129 GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT = 1 << 1,
132 * OR'ed flags if message is not fragmented.
134 GNUNET_MULTICAST_MESSAGE_NOT_FRAGMENTED
135 = GNUNET_MULTICAST_MESSAGE_FIRST_FRAGMENT
136 | GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT
140 GNUNET_NETWORK_STRUCT_BEGIN
143 * Header of a multicast message fragment.
145 * This format is public as the replay mechanism must replay message fragments using the
146 * same format. This is needed as we want to integrity-check message fragments within
147 * the multicast layer to avoid multicasting mal-formed messages.
149 struct GNUNET_MULTICAST_MessageHeader
153 * Header for all multicast message fragments from the origin.
155 struct GNUNET_MessageHeader header;
158 * Number of hops this message fragment has taken since the origin.
160 * Helpful to determine shortest paths to the origin for responses among
161 * honest peers; updated at each hop and thus not signed and not secure.
163 uint32_t hop_counter GNUNET_PACKED;
166 * ECC signature of the message fragment.
168 * Signature must match the public key of the multicast group.
170 struct GNUNET_CRYPTO_EccSignature signature;
173 * Signature of the multicast message fragment.
175 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
178 * Number of the message fragment, monotonically increasing.
180 uint64_t fragment_id GNUNET_PACKED;
183 * Number of the message this fragment belongs to.
185 uint64_t message_id GNUNET_PACKED;
188 * Number of fragments before the current one that has the same @a message_id.
190 * 0 for the first fragment of a message.
191 * This allows replaying a message with all of its fragments.
193 uint64_t fragment_delta GNUNET_PACKED;
196 * Byte offset of this @e fragment of the @e message.
198 uint64_t fragment_offset GNUNET_PACKED;
201 * Counter that monotonically increases whenever a member parts the group.
203 * It has significance in case of replay requests: when a member has missed
204 * messages and gets a replay request: in this case if the @a group_generation
205 * is still the same before and after the missed messages, it means that no
206 * @e join or @e part operations happened during the missed messages.
208 uint64_t group_generation GNUNET_PACKED;
211 * Difference between the current @a fragment_id and the @a fragment_id of the
212 * preceeding non-transient message.
214 * Zero for transient messages, @c UINT64_MAX for the first message, or any
215 * other message creating a full state reset by the origin. By subtracting
216 * @a state_delta from @a fragment_id, it is possible to calculate the message
217 * ID of the preceeding non-transient message and thus quickly traverse all
218 * state changes up to the last full state reset by the origin. This is
219 * useful as it allows joining clients to quickly reassemble the state while
220 * skipping over transient messages (and doing so without having to trust
221 * intermediaries to do it right, as the indices in the chain are signed). If
222 * the state chain is getting too long, the origin can choose to originate a
223 * state message with a state_delta of UINT64_MAX, thereby starting a new
224 * chain. The origin will then have to re-create the full state with state
225 * update messages following the state reset message.
227 * Open question: needed in multicast, or just have this in PSYC; still might
228 * be useful for selective fetching of messages. Still, that again should
229 * that not be done by PSYC?
231 uint64_t state_delta GNUNET_PACKED;
234 * Flags for this message fragment.
236 enum GNUNET_MULTICAST_MessageFlags flags GNUNET_PACKED;
239 * Header for the message body.
241 * Three message types are specifically understood by multicast, namely "peer
242 * join", "peer part", and "group terminated". Multicast will use those
243 * messages to update its list of candidates for content distribution. All
244 * other message types are application-specific.
246 struct GNUNET_MessageHeader body;
248 /* Followed by message body. */
253 * Header of a request from a member to the origin.
255 * FIXME: this is going to be internal.
257 struct GNUNET_MULTICAST_RequestHeader
260 * Header for all requests from a member to the origin.
262 struct GNUNET_MessageHeader header;
265 * Public key of the group.
267 struct GNUNET_CRYPTO_ECCPublicKey pub_key;
270 * Flags for this request.
272 enum GNUNET_MULTICAST_MessageFlags flags GNUNET_PACKED;
275 * Header for the request body.
277 * Two request types are specifically understood by multicast, namely "peer
278 * join", "peer part". Multicast will use those messages to update its list
279 * of candidates for content distribution. All other message types are
280 * application-specific.
282 struct GNUNET_MessageHeader body;
284 /* Followed by request body. */
287 GNUNET_NETWORK_STRUCT_END
291 * Handle that identifies a join request.
293 * Used to match calls to #GNUNET_MULTICAST_JoinCallback to the
294 * corresponding calls to GNUNET_MULTICAST_join_decision().
296 struct GNUNET_MULTICAST_JoinHandle;
300 * Function to call with the decision made for a join request.
302 * Must be called once and only once in response to an invocation of the
303 * #GNUNET_MULTICAST_JoinCallback.
305 * @param jh Join request handle.
306 * @param is_admitted #GNUNET_YES if joining is approved,
307 * #GNUNET_NO if it is disapproved
308 * @param relay_count Number of relays given.
309 * @param relays Array of suggested peers that might be useful relays to use
310 * when joining the multicast group (essentially a list of peers that
311 * are already part of the multicast group and might thus be willing
312 * to help with routing). If empty, only this local peer (which must
313 * be the multicast origin) is a good candidate for building the
314 * multicast tree. Note that it is unnecessary to specify our own
315 * peer identity in this array.
316 * @param join_response Message to send in response to the joining peer;
317 * can also be used to redirect the peer to a different group at the
318 * application layer; this response is to be transmitted to the
319 * peer that issued the request even if admission is denied.
322 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
324 unsigned int relay_count,
325 const struct GNUNET_PeerIdentity *relays,
326 const struct GNUNET_MessageHeader *join_response);
330 * Method called whenever another peer wants to join the multicast group.
332 * Implementations of this function must call GNUNET_MULTICAST_join_decision()
335 * @param cls Closure.
336 * @param peer Identity of the peer that wants to join.
337 * @param join_req Application-dependent join message from the new user
338 * (might, for example, contain a user,
339 * bind user identity/pseudonym to peer identity, application-level
340 * message to origin, etc.).
341 * @param jh Join handle to pass to GNUNET_MULTICAST_join_decison().
343 typedef void (*GNUNET_MULTICAST_JoinCallback)(void *cls,
344 const struct GNUNET_PeerIdentity *peer,
345 const struct GNUNET_MessageHeader *join_req,
346 struct GNUNET_MULTICAST_JoinHandle *jh);
350 * Handle to pass back for the answer of a membership test.
352 struct GNUNET_MULTICAST_MembershipTestHandle;
356 * Call informing multicast about the decision taken for membership test.
358 * @param mth Handle that was given for the query.
359 * @param decision #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
360 * #GNUNET_SYSERR if we cannot answer the membership test.
363 GNUNET_MULTICAST_membership_test_answer (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
368 * Method called to test if a member was in the group at a particular time.
370 * It is called when a replay request is received to determine if the requested
371 * message can be replayed.
373 * @param cls Closure.
374 * @param peer Identity of the peer that we want to test.
375 * @param fragment_id Message fragment ID for which we want to do the test.
376 * @param mth Handle to give to GNUNET_MULTICAST_membership_test_answer().
378 typedef void (*GNUNET_MULTICAST_MembershipTestCallback)(void *cls,
379 const struct GNUNET_PeerIdentity *peer,
380 uint64_t fragment_id,
381 struct GNUNET_MULTICAST_MembershipTestHandle *mth);
385 * Function called whenever a group member has transmitted a request
386 * to the origin (other than joining or leaving).
388 * @param cls Closure (set from GNUNET_MULTICAST_origin_start).
389 * @param sender Identity of the sender.
390 * @param req Request to the origin.
391 * @param flags Flags for the request.
393 typedef void (*GNUNET_MULTICAST_RequestCallback) (void *cls,
394 const struct GNUNET_PeerIdentity *sender,
395 const struct GNUNET_MessageHeader *req,
396 enum GNUNET_MULTICAST_MessageFlags flags);
400 * Function called whenever a group member is receiving a message fragment from
403 * If admission to the group is denied, this function is called once with the
404 * response of the @e origin (as given to GNUNET_MULTICAST_join_decision()) and
405 * then a second time with NULL to indicate that the connection failed for good.
407 * @param cls Closure (set from GNUNET_MULTICAST_member_join())
408 * @param msg Message from the origin, NULL if the origin shut down
409 * (or we were kicked out, and we should thus call
410 * GNUNET_MULTICAST_member_part() next)
412 typedef void (*GNUNET_MULTICAST_MessageCallback) (void *cls,
413 const struct GNUNET_MULTICAST_MessageHeader *msg);
417 * Opaque handle to a replay request from the multicast service.
419 struct GNUNET_MULTICAST_ReplayHandle;
423 * Functions with this signature are called whenever the multicast service needs
424 * a message to be replayed.
426 * Implementations of this function MUST call GNUNET_MULTICAST_replay() ONCE
427 * (with a message or an error); however, if the origin is destroyed or the
428 * group is left, the replay handle must no longer be used.
430 * @param cls Closure (set from GNUNET_MULTICAST_origin_start()
431 * or GNUNET_MULTICAST_member_join()).
432 * @param fragment_id Which message fragment should be replayed.
433 * @param rh Handle to pass to message transmit function.
435 typedef void (*GNUNET_MULTICAST_ReplayCallback) (void *cls,
436 uint64_t fragment_id,
437 struct GNUNET_MULTICAST_ReplayHandle *rh);
441 * Possible error codes during replay.
443 enum GNUNET_MULTICAST_ReplayErrorCode
447 * Everything is fine.
449 GNUNET_MULTICAST_REC_OK = 0,
452 * Message fragment has been discarded (likely transient message that was too old).
454 GNUNET_MULTICAST_REC_TRANSIENT_LOST = 1,
457 * Fragment ID counter was larger than the highest counter this
458 * replay function has ever encountered; thus it is likely the
459 * origin never sent it and we're at the HEAD of the multicast
460 * stream as far as this node is concerned.
462 GNUNET_MULTICAST_REC_PAST_HEAD = 2,
465 * Internal error (i.e. database error). Try some other peer.
467 GNUNET_MULTICAST_REC_INTERNAL_ERROR = 3
473 * Replay a message from the multicast group.
475 * @param rh Replay handle identifying which replay operation was requested.
476 * @param msg Replayed message fragment, NULL if unknown/error.
477 * @param ec Error code.
480 GNUNET_MULTICAST_replay (struct GNUNET_MULTICAST_ReplayHandle *rh,
481 const struct GNUNET_MULTICAST_MessageHeader *msg,
482 enum GNUNET_MULTICAST_ReplayErrorCode ec);
486 * Start a multicast group.
488 * Will advertise the origin in the P2P overlay network under the respective
489 * public key so that other peer can find this peer to join it. Peers that
490 * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
491 * either an existing group member (if the @a policy is permissive) or to
492 * the origin. If the joining is approved, the member is cleared for @e replay
493 * and will begin to receive messages transmitted to the group. If joining is
494 * disapproved, the failed candidate will be given a response. Members in the
495 * group can send messages to the origin (one at a time).
497 * @param cfg Configuration to use.
498 * @param priv_key ECC key that will be used to sign messages for this
499 * multicast session; public key is used to identify the multicast group;
500 * FIXME: we'll likely want to use NOT the p521 curve here, but a cheaper
502 * @param policy Group policy specifying join and history restrictions.
503 * @param last_fragment_id Last fragment ID to continue counting fragments from
504 * when restarting the origin. 0 for a new group.
505 * @param join_cb Function called to approve / disapprove joining of a peer.
506 * @param test_cb Function multicast can use to test group membership.
507 * @param replay_cb Function that can be called to replay a message.
508 * @param request_cb Function called with message fragments from group members.
509 * @param message_cb Function called with the message fragments sent to the
510 * network by GNUNET_MULTICAST_origin_to_all(). These message fragments
511 * should be stored for answering replay requests later.
512 * @param cls Closure for the various callbacks that follow.
513 * @return Handle for the origin, NULL on error.
515 struct GNUNET_MULTICAST_Origin *
516 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
517 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
518 enum GNUNET_MULTICAST_GroupPolicy policy,
519 uint64_t last_fragment_id,
520 GNUNET_MULTICAST_JoinCallback join_cb,
521 GNUNET_MULITCAST_MembershipTestCallback test_cb,
522 GNUNET_MULITCAST_ReplayCallback replay_cb,
523 GNUNET_MULTICAST_RequestCallback request_cb,
524 GNUNET_MULTICAST_MessageCallback message_cb,
529 * Handle for a request to send a message to all multicast group members
532 struct GNUNET_MULTICAST_OriginMessageHandle;
536 * Send a message to the multicast group.
538 * @param origin Handle to the multicast group.
539 * @param message_id Application layer ID for the message.
540 * @param group_generation Group generation of the message. See GNUNET_MULTICAST_MessageHeader.
541 * @param state_delta State delta of the message. See GNUNET_MULTICAST_MessageHeader.
542 * @param size Number of bytes to transmit.
543 * @param cb Function to call to get the message.
544 * @param cb_cls Closure for @a cb.
545 * @return NULL on error (i.e. request already pending).
547 struct GNUNET_MULTICAST_OriginMessageHandle *
548 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
550 uint64_t group_generation,
551 uint64_t state_delta,
553 GNUNET_CONNECTION_TransmitReadyNotify cb,
558 * Cancel request for message transmission to multicast group.
560 * @param mh Request to cancel.
563 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginMessageHandle *mh);
567 * Stop a multicast group.
569 * @param origin Multicast group to stop.
572 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *origin);
576 * Join a multicast group.
578 * The entity joining is always the local peer. Further information about the
579 * candidate can be provided in the @a join_request message. If the join fails, the
580 * @a message_cb is invoked with a (failure) response and then with NULL. If
581 * the join succeeds, outstanding (state) messages and ongoing multicast
582 * messages will be given to the @a message_cb until the member decides to part
583 * the group. The @a test_cb and @a replay_cb functions may be called at
584 * anytime by the multicast service to support relaying messages to other
585 * members of the group.
587 * @param cfg Configuration to use.
588 * @param pub_key ECC key that identifies the group.
589 * @param origin Peer identity of the origin, to send unicast requests to.
590 * @param relay_count Number of peers in the @a relays array.
591 * @param relays Peer identities of members of the group, which serve as relays
592 * and can be used to join the group at. and send the @a join_request to.
593 * If empty, the @a join_request is sent directly to the @a origin.
594 * @param join_request Application-dependent join request to be passed to the peer
595 * @a relay (might, for example, contain a user, bind user
596 * identity/pseudonym to peer identity, application-level message to
598 * @param max_known_fragment_id Largest known message fragment ID to the replay
599 * service; all messages with IDs larger than this ID will be replayed if
600 * possible (lower IDs will be considered known and thus only
601 * be replayed upon explicit request).
602 * FIXME: needed? can be optional or moved to a separate function.
603 * @param replay_cb Function that can be called to replay messages
604 * this peer already knows from this group; NULL if this
605 * client is unable to support replay.
606 * @param test_cb Function multicast can use to test group membership.
607 * @param message_cb Function to be called for all message fragments we
608 * receive from the group, excluding those our @a replay_cb
610 * @param cls Closure for callbacks.
611 * @return Handle for the member, NULL on error.
613 struct GNUNET_MULTICAST_Member *
614 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
615 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
616 const struct GNUNET_PeerIdentity *origin,
618 const struct GNUNET_PeerIdentity *members,
619 const struct GNUNET_MessageHeader *join_request,
620 uint64_t max_known_fragment_id,
621 GNUNET_MULTICAST_ReplayCallback replay_cb,
622 GNUNET_MULITCAST_MembershipTestCallback test_cb,
623 GNUNET_MULTICAST_MessageCallback message_cb,
628 * Handle for a replay request.
630 struct GNUNET_MULTICAST_MemberReplayHandle;
634 * Request a message to be replayed.
636 * Useful if messages below the @e max_known_*_id's given when joining are
637 * needed and not known to the client.
639 * @param member Membership handle.
640 * @param message_id ID of a message that this client would like to see replayed.
641 * @param message_cb Function to be called for the replayed message.
642 * @param message_cb_cls Closure for @a message_cb.
643 * @return Replay request handle, NULL on error.
645 struct GNUNET_MULTICAST_MemberReplayHandle *
646 GNUNET_MULTICAST_member_request_replay (struct GNUNET_MULTICAST_Member *member,
648 GNUNET_MULTICAST_MessageCallback message_cb,
649 void *message_cb_cls);
653 * Cancel a replay request.
655 * @param rh Request to cancel.
658 GNUNET_MULTICAST_member_request_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh);
662 * Part a multicast group.
664 * Disconnects from all group members and invalidates the @a member handle.
666 * An application-dependent part message can be transmitted beforehand using
667 * GNUNET_MULTICAST_member_to_origin())
669 * @param member Membership handle.
672 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member);
676 * Handle for a message to be delivered from a member to the origin.
678 struct GNUNET_MULTICAST_MemberRequestHandle;
682 * Send a message to the origin of the multicast group.
684 * @param member Membership handle.
685 * @param size Number of bytes we want to send to origin.
686 * @param cb Callback to call to get the message.
687 * @param cb_cls Closure for @a cb.
688 * @return Handle to cancel request, NULL on error (i.e. request already pending).
690 struct GNUNET_MULTICAST_MemberRequestHandle *
691 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
693 GNUNET_CONNECTION_TransmitReadyNotify cb,
698 * Cancel request for message transmission to origin.
700 * @param rh Request to cancel.
703 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberRequestHandle *rh);
707 * Handle to access multicast group operations for both the origin and members.
709 struct GNUNET_MULTICAST_Group;
713 * Convert a group @a origin to a @e group handle to access the @e group APIs.
715 * @param origin Group origin handle.
716 * @return Group handle, valid for as long as @a origin is valid.
718 struct GNUNET_MULTICAST_Group *
719 GNUNET_MULTICAST_origin_get_group (struct GNUNET_MULTICAST_Origin *origin);
723 * Convert @a member to a @e group handle to access the @e group APIs.
725 * @param member Member handle.
726 * @return Group handle, valid for as long as @a member is valid.
728 struct GNUNET_MULTICAST_Group *
729 GNUNET_MULTICAST_member_get_group (struct GNUNET_MULTICAST_Member *member);
733 * Remove a peer from the group.
735 * After a message was received notifying about a leaving member, remove the
736 * member from the multicast group. Fragments with a greater @a message_id than
737 * the specified one won't be transmitted to the member anymore, but the
738 * transmission of lower or equal ones will still be performed.
741 GNUNET_MULTICAST_group_member_remove (struct GNUNET_MULTICAST_Group *group,
742 const struct GNUNET_PeerIdentity *peer,
743 uint64_t message_id);
746 #if 0 /* keep Emacsens' auto-indent happy */
753 /* ifndef GNUNET_MULTICAST_SERVICE_H */
755 /* end of gnunet_multicast_service.h */