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 enum GNUNET_MULTICAST_MessageFlags
62 * First fragment of a message.
64 GNUNET_MULTICAST_MESSAGE_FIRST_FRAGMENT = 1 << 0,
67 * Last fragment of a message.
69 GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT = 1 << 1,
72 * OR'ed flags if message is not fragmented.
74 GNUNET_MULTICAST_MESSAGE_NOT_FRAGMENTED
75 = GNUNET_MULTICAST_MESSAGE_FIRST_FRAGMENT
76 | GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT
80 GNUNET_NETWORK_STRUCT_BEGIN
83 * Header of a multicast message fragment.
85 * This format is public as the replay mechanism must replay message fragments using the
86 * same format. This is needed as we want to integrity-check message fragments within
87 * the multicast layer to avoid multicasting mal-formed messages.
89 struct GNUNET_MULTICAST_MessageHeader
93 * Header for all multicast message fragments from the origin.
95 struct GNUNET_MessageHeader header;
98 * Number of hops this message fragment has taken since the origin.
100 * Helpful to determine shortest paths to the origin among honest peers for
101 * unicast requests from members. Updated at each hop and thus not signed and
104 uint32_t hop_counter GNUNET_PACKED;
107 * ECC signature of the message fragment.
109 * Signature must match the public key of the multicast group.
111 struct GNUNET_CRYPTO_EccSignature signature;
114 * Purpose for the signature and size of the signed data.
116 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
119 * Number of the message fragment, monotonically increasing.
121 uint64_t fragment_id GNUNET_PACKED;
124 * Byte offset of this @e fragment of the @e message.
126 uint64_t fragment_offset GNUNET_PACKED;
129 * Number of the message this fragment belongs to.
131 * Set in GNUNET_MULTICAST_origin_to_all().
133 uint64_t message_id GNUNET_PACKED;
136 * Counter that monotonically increases whenever a member parts the group.
138 * Set in GNUNET_MULTICAST_origin_to_all().
140 * It has significance in case of replay requests: when a member has missed
141 * messages and gets a replay request: in this case if the @a group_generation
142 * is still the same before and after the missed messages, it means that no
143 * @e join or @e part operations happened during the missed messages.
145 uint64_t group_generation GNUNET_PACKED;
148 * Flags for this message fragment.
150 uint32_t flags GNUNET_PACKED;
152 /* Followed by message body. */
155 GNUNET_NETWORK_STRUCT_END
159 * Handle that identifies a join request.
161 * Used to match calls to #GNUNET_MULTICAST_JoinCallback to the
162 * corresponding calls to #GNUNET_MULTICAST_join_decision().
164 struct GNUNET_MULTICAST_JoinHandle;
168 * Function to call with the decision made for a join request.
170 * Must be called once and only once in response to an invocation of the
171 * #GNUNET_MULTICAST_JoinCallback.
173 * @param jh Join request handle.
174 * @param is_admitted #GNUNET_YES if joining is approved,
175 * #GNUNET_NO if it is disapproved
176 * @param relay_count Number of relays given.
177 * @param relays Array of suggested peers that might be useful relays to use
178 * when joining the multicast group (essentially a list of peers that
179 * are already part of the multicast group and might thus be willing
180 * to help with routing). If empty, only this local peer (which must
181 * be the multicast origin) is a good candidate for building the
182 * multicast tree. Note that it is unnecessary to specify our own
183 * peer identity in this array.
184 * @param join_response Message to send in response to the joining peer;
185 * can also be used to redirect the peer to a different group at the
186 * application layer; this response is to be transmitted to the
187 * peer that issued the request even if admission is denied.
189 struct GNUNET_MULTICAST_ReplayHandle *
190 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
192 unsigned int relay_count,
193 const struct GNUNET_PeerIdentity *relays,
194 const struct GNUNET_MessageHeader *join_response);
198 * Method called whenever another peer wants to join the multicast group.
200 * Implementations of this function must call GNUNET_MULTICAST_join_decision()
203 * @param cls Closure.
204 * @param peer Identity of the member that wants to join.
205 * @param join_req Application-dependent join message from the new member
206 * (might, for example, contain a user,
207 * bind user identity/pseudonym to peer identity, application-level
208 * message to origin, etc.).
209 * @param jh Join handle to pass to GNUNET_MULTICAST_join_decison().
212 (*GNUNET_MULTICAST_JoinCallback) (void *cls,
213 const struct GNUNET_CRYPTO_EccPublicSignKey *member_key,
214 const struct GNUNET_MessageHeader *join_req,
215 struct GNUNET_MULTICAST_JoinHandle *jh);
219 * Handle to pass back for the answer of a membership test.
221 struct GNUNET_MULTICAST_MembershipTestHandle;
225 * Call informing multicast about the decision taken for a membership test.
227 * @param mth Handle that was given for the query.
228 * @param result #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
229 * #GNUNET_SYSERR if we cannot answer the membership test.
232 GNUNET_MULTICAST_membership_test_result (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
237 * Method called to test if a member was in the group at a particular time.
239 * It is called when a replay request is received to determine if the requested
240 * message can be replayed.
242 * @param cls Closure.
243 * @param member_key Identity of the member that we want to test.
244 * @param message_id Message ID for which to perform the test.
245 * @param group_generation Group generation of the message. It has relevance if
246 * the message consists of multiple fragments with different group
248 * @param mth Handle to give to GNUNET_MULTICAST_membership_test_answer().
251 (*GNUNET_MULTICAST_MembershipTestCallback) (void *cls,
252 const struct GNUNET_CRYPTO_EccPublicSignKey *member_key,
254 uint64_t group_generation,
255 struct GNUNET_MULTICAST_MembershipTestHandle *mth);
259 * Function called whenever a group member has transmitted a request
260 * to the origin (other than joining or leaving).
262 * @param cls Closure (set from GNUNET_MULTICAST_origin_start).
263 * @param sender Identity of the sender.
264 * @param req Request to the origin.
265 * @param flags Flags for the request.
268 (*GNUNET_MULTICAST_RequestCallback) (void *cls,
269 const struct GNUNET_CRYPTO_EccPublicSignKey *member_key,
270 const struct GNUNET_MessageHeader *req,
271 enum GNUNET_MULTICAST_MessageFlags flags);
275 * Function called whenever a group member is receiving a message fragment from
278 * If admission to the group is denied, this function is called once with the
279 * response of the @e origin (as given to GNUNET_MULTICAST_join_decision()) and
280 * then a second time with NULL to indicate that the connection failed for good.
282 * @param cls Closure (set from GNUNET_MULTICAST_member_join())
283 * @param msg Message from the origin, NULL if the origin shut down
284 * (or we were kicked out, and we should thus call
285 * GNUNET_MULTICAST_member_part() next)
288 (*GNUNET_MULTICAST_MessageCallback) (void *cls,
289 const struct GNUNET_MessageHeader *msg);
293 * Function called with the result of an asynchronous operation.
295 * @see GNUNET_MULTICAST_replay_fragment()
297 * @param result Result of the operation.
300 (*GNUNET_MULTICAST_ResultCallback) (void *cls,
305 * Opaque handle to a replay request from the multicast service.
307 struct GNUNET_MULTICAST_ReplayHandle;
311 * Functions with this signature are called whenever the multicast service needs
312 * a message fragment to be replayed by fragment_id.
314 * Implementations of this function MUST call GNUNET_MULTICAST_replay() ONCE
315 * (with a message or an error); however, if the origin is destroyed or the
316 * group is left, the replay handle must no longer be used.
318 * @param cls Closure (set from GNUNET_MULTICAST_origin_start()
319 * or GNUNET_MULTICAST_member_join()).
320 * @param member_key The member requesting replay.
321 * @param fragment_id Which message fragment should be replayed.
322 * @param flags Flags for the replay.
323 * @param rh Handle to pass to message transmit function.
326 (*GNUNET_MULTICAST_ReplayFragmentCallback) (void *cls,
327 const struct GNUNET_CRYPTO_EccPublicSignKey *member_key,
328 uint64_t fragment_id,
330 struct GNUNET_MULTICAST_ReplayHandle *rh);
333 * Functions with this signature are called whenever the multicast service needs
334 * a message fragment to be replayed by message_id and fragment_offset.
336 * Implementations of this function MUST call GNUNET_MULTICAST_replay() ONCE
337 * (with a message or an error); however, if the origin is destroyed or the
338 * group is left, the replay handle must no longer be used.
340 * @param cls Closure (set from GNUNET_MULTICAST_origin_start()
341 * or GNUNET_MULTICAST_member_join()).
342 * @param member_key The member requesting replay.
343 * @param message_id Which message should be replayed.
344 * @param fragment_offset Offset of the fragment within of @a message_id to be replayed.
345 * @param flags Flags for the replay.
346 * @param rh Handle to pass to message transmit function.
349 (*GNUNET_MULTICAST_ReplayMessageCallback) (void *cls,
350 const struct GNUNET_CRYPTO_EccPublicSignKey *member_key,
352 uint64_t fragment_offset,
354 struct GNUNET_MULTICAST_ReplayHandle *rh);
358 * Possible error codes during replay.
360 enum GNUNET_MULTICAST_ReplayErrorCode
364 * Everything is fine.
366 GNUNET_MULTICAST_REC_OK = 0,
369 * Message fragment not found in the message store.
371 * Either discarded if it is too old, or not arrived yet if this member has
372 * missed some messages.
374 GNUNET_MULTICAST_REC_NOT_FOUND = 1,
377 * Fragment ID counter was larger than the highest counter this
378 * replay function has ever encountered; thus it is likely the
379 * origin never sent it and we're at the HEAD of the multicast
380 * stream as far as this node is concerned.
384 GNUNET_MULTICAST_REC_PAST_HEAD = 2,
387 * Access is denied to the requested fragment, membership test did not pass.
389 GNUNET_MULTICAST_REC_ACCESS_DENIED = 3,
392 * Internal error (i.e. database error). Try some other peer.
394 GNUNET_MULTICAST_REC_INTERNAL_ERROR = 4
400 * Replay a message fragment for the multicast group.
402 * @param rh Replay handle identifying which replay operation was requested.
403 * @param msg Replayed message fragment, NULL if unknown/error.
404 * @param ec Error code.
407 GNUNET_MULTICAST_replay_response (struct GNUNET_MULTICAST_ReplayHandle *rh,
408 const struct GNUNET_MessageHeader *msg,
409 enum GNUNET_MULTICAST_ReplayErrorCode ec);
413 * Indicate the end of the replay session.
415 * Invalidates the replay handle.
417 * @param rh Replay session to end.
420 GNUNET_MULTICAST_replay_response_end (struct GNUNET_MULTICAST_ReplayHandle *rh);
424 * Function called to provide data for a transmission for a replay.
426 * @see GNUNET_MULTICAST_replay2()
429 (*GNUNET_MULTICAST_ReplayTransmitNotify) (void *cls,
435 * Replay a message for the multicast group.
437 * @param rh Replay handle identifying which replay operation was requested.
438 * @param notify Function to call to get the message.
439 * @param notify_cls Closure for @a notify.
442 GNUNET_MULTICAST_replay_response2 (struct GNUNET_MULTICAST_ReplayHandle *rh,
443 GNUNET_MULTICAST_ReplayTransmitNotify notify,
448 * Start a multicast group.
450 * Will advertise the origin in the P2P overlay network under the respective
451 * public key so that other peer can find this peer to join it. Peers that
452 * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
453 * either an existing group member or to the origin. If the joining is
454 * approved, the member is cleared for @e replay and will begin to receive
455 * messages transmitted to the group. If joining is disapproved, the failed
456 * candidate will be given a response. Members in the group can send messages
457 * to the origin (one at a time).
459 * @param cfg Configuration to use.
460 * @param priv_key ECC key that will be used to sign messages for this
461 * multicast session; public key is used to identify the multicast group;
462 * @param last_fragment_id Last fragment ID to continue counting fragments from
463 * when restarting the origin. 0 for a new group.
464 * @param join_cb Function called to approve / disapprove joining of a peer.
465 * @param test_cb Function multicast can use to test group membership.
466 * @param replay_frag_cb Function that can be called to replay a message fragment.
467 * @param replay_msg_cb Function that can be called to replay a message.
468 * @param request_cb Function called with message fragments from group members.
469 * @param message_cb Function called with the message fragments sent to the
470 * network by GNUNET_MULTICAST_origin_to_all(). These message fragments
471 * should be stored for answering replay requests later.
472 * @param cls Closure for the various callbacks that follow.
473 * @return Handle for the origin, NULL on error.
475 struct GNUNET_MULTICAST_Origin *
476 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
477 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
478 uint64_t last_fragment_id,
479 GNUNET_MULTICAST_JoinCallback join_cb,
480 GNUNET_MULTICAST_MembershipTestCallback test_cb,
481 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
482 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
483 GNUNET_MULTICAST_RequestCallback request_cb,
484 GNUNET_MULTICAST_MessageCallback message_cb,
488 * Function called to provide data for a transmission from the origin to all
490 * FIXME: what if origin needs to pause transmission for a while?
493 * @param data_size number of bytes available in @a data
494 * @param data where to copy the message
495 * @return number of bytes copied to @a data? (FIXME: size_t?), or status code?
498 (*GNUNET_MULTICAST_OriginTransmitNotify) (void *cls,
504 * Handle for a request to send a message to all multicast group members
507 struct GNUNET_MULTICAST_OriginMessageHandle;
511 * Send a message to the multicast group.
513 * @param origin Handle to the multicast group.
514 * @param message_id Application layer ID for the message. Opaque to multicast.
515 * @param group_generation Group generation of the message. Documented in
516 * GNUNET_MULTICAST_MessageHeader.
517 * @param size Number of bytes to transmit.
518 * FIXME: Needed? The end of the message can be flagged with a last fragment flag.
519 * FIXME: what last fragment flag? OriginTransmitNotify is not that well documented...
520 * FIXME: size_t? If this is a total size, uint64_t might be better!
521 * FIXME: do we reserve "MAX" to indicate 'unknown'?
522 * @param notify Function to call to get the message.
523 * @param notify_cls Closure for @a notify.
524 * @return NULL on error (i.e. request already pending).
526 struct GNUNET_MULTICAST_OriginMessageHandle *
527 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
529 uint64_t group_generation,
531 GNUNET_MULTICAST_OriginTransmitNotify notify,
536 * Cancel request for message transmission to multicast group.
538 * @param mh Request to cancel.
541 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginMessageHandle *mh);
545 * Stop a multicast group.
547 * @param origin Multicast group to stop.
550 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *origin);
554 * Join a multicast group.
556 * The entity joining is always the local peer. Further information about the
557 * candidate can be provided in the @a join_request message. If the join fails, the
558 * @a message_cb is invoked with a (failure) response and then with NULL. If
559 * the join succeeds, outstanding (state) messages and ongoing multicast
560 * messages will be given to the @a message_cb until the member decides to part
561 * the group. The @a test_cb and @a replay_cb functions may be called at
562 * anytime by the multicast service to support relaying messages to other
563 * members of the group.
565 * @param cfg Configuration to use.
566 * @param group_key ECC public key that identifies the group to join.
567 * @param member_key ECC key that identifies the member and used to sign
568 * requests sent to the origin.
569 * @param origin Peer ID of the origin to send unicast requsets to. If NULL,
570 * unicast requests are sent back via multiple hops on the reverse path
571 * of multicast messages.
572 * @param relay_count Number of peers in the @a relays array.
573 * @param relays Peer identities of members of the group, which serve as relays
574 * and can be used to join the group at. and send the @a join_request to.
575 * If empty, the @a join_request is sent directly to the @a origin.
576 * @param join_request Application-dependent join request to be passed to the peer
577 * @a relay (might, for example, contain a user, bind user
578 * identity/pseudonym to peer identity, application-level message to
580 * @param join_cb Function called to approve / disapprove joining of a peer.
581 * @param test_cb Function multicast can use to test group membership.
582 * @param replay_frag_cb Function that can be called to replay message fragments
583 * this peer already knows from this group. NULL if this
584 * client is unable to support replay.
585 * @param replay_msg_cb Function that can be called to replay message fragments
586 * this peer already knows from this group. NULL if this
587 * client is unable to support replay.
588 * @param message_cb Function to be called for all message fragments we
589 * receive from the group, excluding those our @a replay_cb
591 * @param cls Closure for callbacks.
592 * @return Handle for the member, NULL on error.
594 struct GNUNET_MULTICAST_Member *
595 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
596 const struct GNUNET_CRYPTO_EccPublicSignKey *group_key,
597 const struct GNUNET_CRYPTO_EccPrivateKey *member_key,
598 const struct GNUNET_PeerIdentity *origin,
600 const struct GNUNET_PeerIdentity *relays,
601 const struct GNUNET_MessageHeader *join_request,
602 GNUNET_MULTICAST_JoinCallback join_cb,
603 GNUNET_MULTICAST_MembershipTestCallback test_cb,
604 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
605 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
606 GNUNET_MULTICAST_MessageCallback message_cb,
610 * Handle for a replay request.
612 struct GNUNET_MULTICAST_MemberReplayHandle;
616 * Request a fragment to be replayed by fragment ID.
618 * Useful if messages below the @e max_known_fragment_id given when joining are
619 * needed and not known to the client.
621 * @param member Membership handle.
622 * @param fragment_id ID of a message fragment that this client would like to
624 * @param flags Additional flags for the replay request. It is used and defined
625 * by the replay callback. FIXME: which replay callback? FIXME: use enum?
626 * FIXME: why not pass reply cb here?
627 * @return Replay request handle, NULL on error.
629 struct GNUNET_MULTICAST_MemberReplayHandle *
630 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *member,
631 uint64_t fragment_id,
636 * Request a message fr to be replayed.
638 * Useful if messages below the @e max_known_fragment_id given when joining are
639 * needed and not known to the client.
641 * @param member Membership handle.
642 * @param message_id ID of the message this client would like to see replayed.
643 * @param fragment_offset Offset of the fragment within the message to replay.
644 * @param flags Additional flags for the replay request. It is used & defined
645 * by the replay callback.
646 * @param result_cb Function to be called for the replayed message.
647 * @param result_cb_cls Closure for @a result_cb.
648 * @return Replay request handle, NULL on error.
650 struct GNUNET_MULTICAST_MemberReplayHandle *
651 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *member,
653 uint64_t fragment_offset,
655 GNUNET_MULTICAST_ResultCallback result_cb,
656 void *result_cb_cls);
660 * Cancel a replay request.
662 * @param rh Request to cancel.
665 GNUNET_MULTICAST_member_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh);
669 * Part a multicast group.
671 * Disconnects from all group members and invalidates the @a member handle.
673 * An application-dependent part message can be transmitted beforehand using
674 * #GNUNET_MULTICAST_member_to_origin())
676 * @param member Membership handle.
679 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member);
683 * Function called to provide data for a transmission from a member to the origin.
686 * @param data_size number of bytes available in @a data
687 * @param data where to copy data for transmission
688 * @return number of bytes copied to data
691 (*GNUNET_MULTICAST_MemberTransmitNotify) (void *cls,
697 * Handle for a message to be delivered from a member to the origin.
699 struct GNUNET_MULTICAST_MemberRequestHandle;
703 * Send a message to the origin of the multicast group.
705 * @param member Membership handle.
706 * @param message_id Application layer ID for the message. Opaque to multicast.
707 * @param size Number of bytes we want to send to origin.
708 * @param notify Callback to call to get the message.
709 * @param notify_cls Closure for @a notify.
710 * @return Handle to cancel request, NULL on error (i.e. request already pending).
712 struct GNUNET_MULTICAST_MemberRequestHandle *
713 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
716 GNUNET_MULTICAST_MemberTransmitNotify notify,
721 * Cancel request for message transmission to origin.
723 * @param rh Request to cancel.
726 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberRequestHandle *rh);
729 #if 0 /* keep Emacsens' auto-indent happy */
736 /* ifndef GNUNET_MULTICAST_SERVICE_H */
738 /* end of gnunet_multicast_service.h */