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 * Group membership policies.
61 enum GNUNET_MULTICAST_JoinPolicy
64 * Anyone can join the group, without announcing his presence;
65 * all messages are always public and can be distributed freely.
66 * Joins may be announced, but this is not required.
68 GNUNET_MULTICAST_JP_ANONYMOUS = 0,
71 * Origin must approve membership to the group, messages must only be
72 * distributed to current group members. This includes the group
73 * state as well as transient messages.
75 GNUNET_MULTICAST_JP_PRIVATE = 1,
79 * Anyone can freely join the group (no approval required); however,
80 * transient messages must only be distributed to current group
81 * members, so the origin must still acknowledge that the member
82 * joined before transient messages are delivered. As approval is
83 * guaranteed, the presistent group state can be synchronized freely
84 * immediately, prior to origin confirmation.
86 GNUNET_MULTICAST_JP_OPEN = 2,
89 * Origin must approve membership to the group, but past messages can be
90 * freely distributed to members.
92 GNUNET_MULTICAST_JP_CLOSED = 3,
97 enum GNUNET_MULTICAST_MessageFlags
100 * First fragment of a message.
102 GNUNET_MULTICAST_MESSAGE_FIRST_FRAGMENT = 1 << 0,
105 * Last fragment of a message.
107 GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT = 1 << 1,
110 * OR'ed flags if message is not fragmented.
112 GNUNET_MULTICAST_MESSAGE_NOT_FRAGMENTED
113 = GNUNET_MULTICAST_MESSAGE_FIRST_FRAGMENT
114 | GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT
118 GNUNET_NETWORK_STRUCT_BEGIN
121 * Header of a multicast message fragment.
123 * This format is public as the replay mechanism must replay message fragments using the
124 * same format. This is needed as we want to integrity-check message fragments within
125 * the multicast layer to avoid multicasting mal-formed messages.
127 struct GNUNET_MULTICAST_MessageHeader
131 * Header for all multicast message fragments from the origin.
133 struct GNUNET_MessageHeader header;
136 * Number of hops this message fragment has taken since the origin.
138 * Helpful to determine shortest paths to the origin for responses among
139 * honest peers; updated at each hop and thus not signed and not secure.
141 uint32_t hop_counter GNUNET_PACKED;
144 * ECC signature of the message fragment.
146 * Signature must match the public key of the multicast group.
148 struct GNUNET_CRYPTO_EccSignature signature;
151 * Signature of the multicast message fragment.
153 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
156 * Number of the message fragment, monotonically increasing.
158 uint64_t fragment_id GNUNET_PACKED;
161 * Number of the message this fragment belongs to.
163 uint64_t message_id GNUNET_PACKED;
166 * Byte offset of this @e fragment of the @e message.
170 uint64_t fragment_offset GNUNET_PACKED;
173 * Counter that monotonically increases whenever a member parts the group.
175 * It has significance in case of replay requests: when a member has missed
176 * messages and gets a replay request: in this case if the @a group_generation
177 * is still the same before and after the missed messages, it means that no
178 * @e join or @e part operations happened during the missed messages.
180 uint64_t group_generation GNUNET_PACKED;
183 * Difference between the current @a fragment_id and the @a fragment_id of the
184 * preceeding non-transient message.
186 * Zero for transient messages, @c UINT64_MAX for the first message, or any
187 * other message creating a full state reset by the origin. By subtracting
188 * @a state_delta from @a fragment_id, it is possible to calculate the message
189 * ID of the preceeding non-transient message and thus quickly traverse all
190 * state changes up to the last full state reset by the origin. This is
191 * useful as it allows joining clients to quickly reassemble the state while
192 * skipping over transient messages (and doing so without having to trust
193 * intermediaries to do it right, as the indices in the chain are signed). If
194 * the state chain is getting too long, the origin can choose to originate a
195 * state message with a state_delta of UINT64_MAX, thereby starting a new
196 * chain. The origin will then have to re-create the full state with state
197 * update messages following the state reset message.
199 * Open question: needed in multicast, or just have this in PSYC; still might
200 * be useful for selective fetching of messages. Still, that again should
201 * that not be done by PSYC?
203 uint64_t state_delta GNUNET_PACKED;
206 * Flags for this message fragment.
208 enum GNUNET_MULTICAST_MessageFlags flags GNUNET_PACKED;
211 * Header for the message body.
213 * Three message types are specifically understood by multicast, namely "peer
214 * join", "peer part", and "group terminated". Multicast will use those
215 * messages to update its list of candidates for content distribution. All
216 * other message types are application-specific.
218 struct GNUNET_MessageHeader body;
220 /* Followed by message body. */
225 * Header of a request from a member to the origin.
227 * FIXME: this is going to be internal.
229 struct GNUNET_MULTICAST_RequestHeader
232 * Header for all requests from a member to the origin.
234 struct GNUNET_MessageHeader header;
237 * Public key of the group.
239 struct GNUNET_CRYPTO_ECCPublicKey pub_key;
242 * Flags for this request.
244 enum GNUNET_MULTICAST_MessageFlags flags GNUNET_PACKED;
247 * Header for the request body.
249 * Two request types are specifically understood by multicast, namely "peer
250 * join", "peer part". Multicast will use those messages to update its list
251 * of candidates for content distribution. All other message types are
252 * application-specific.
254 struct GNUNET_MessageHeader body;
256 /* Followed by request body. */
259 GNUNET_NETWORK_STRUCT_END
263 * Handle that identifies a join request.
265 * Used to match calls to #GNUNET_MULTICAST_JoinCallback to the
266 * corresponding calls to GNUNET_MULTICAST_join_decision().
268 struct GNUNET_MULTICAST_JoinHandle;
272 * Function to call with the decision made for a join request.
274 * Must be called once and only once in response to an invocation of the
275 * #GNUNET_MULTICAST_JoinCallback.
277 * @param jh Join request handle.
278 * @param join_response Message to send in response to the joining peer;
279 * can also be used to redirect the peer to a different group at the
280 * application layer; this response is to be transmitted to the
281 * peer that issued the request even if admission is denied.
282 * @param is_admitted #GNUNET_YES if joining is approved,
283 * #GNUNET_NO if it is disapproved
284 * @param relay_count Number of relays given.
285 * @param relays Array of suggested peers that might be useful relays to use
286 * when joining the multicast group (essentially a list of peers that
287 * are already part of the multicast group and might thus be willing
288 * to help with routing). If empty, only this local peer (which must
289 * be the multicast origin) is a good candidate for building the
290 * multicast tree. Note that it is unnecessary to specify our own
291 * peer identity in this array.
294 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
295 const struct GNUNET_MessageHeader *join_response,
297 unsigned int relay_count,
298 const struct GNUNET_PeerIdentity *relays);
302 * Method called whenever another peer wants to join the multicast group.
304 * Implementations of this function must call GNUNET_MULTICAST_join_decision()
307 * @param cls Closure.
308 * @param peer Identity of the peer that wants to join.
309 * @param msg Application-dependent join message from the new user
310 * (might, for example, contain a user,
311 * bind user identity/pseudonym to peer identity, application-level
312 * message to origin, etc.).
313 * @param jh Join handle to pass to GNUNET_MULTICAST_join_decison().
315 typedef void (*GNUNET_MULTICAST_JoinCallback)(void *cls,
316 const struct GNUNET_PeerIdentity *peer,
317 const struct GNUNET_MessageHeader *msg,
318 struct GNUNET_MULTICAST_JoinHandle *jh);
322 * Handle to pass back for the answer of a membership test.
324 struct GNUNET_MULTICAST_MembershipTestHandle;
328 * Call informing multicast about the decision taken for membership test.
330 * @param mth Handle that was given for the query.
331 * @param decision #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
332 * #GNUNET_SYSERR if we cannot answer the membership test.
335 GNUNET_MULTICAST_membership_test_answer (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
340 * Method called to test if a member was in the group at a particular time.
342 * It is called when a replay request is received to determine if the requested
343 * message can be replayed.
345 * @param cls Closure.
346 * @param peer Identity of the peer that we want to test.
347 * @param fragment_id Message fragment ID for which we want to do the test.
348 * @param mth Handle to give to GNUNET_MULTICAST_membership_test_answer().
350 typedef void (*GNUNET_MULTICAST_MembershipTestCallback)(void *cls,
351 const struct GNUNET_PeerIdentity *peer,
352 uint64_t fragment_id,
353 struct GNUNET_MULTICAST_MembershipTestHandle *mth);
357 * Function called whenever a group member has transmitted a request
358 * to the origin (other than joining or leaving).
360 * @param cls Closure (set from GNUNET_MULTICAST_origin_start).
361 * @param sender Identity of the sender.
362 * @param req Request to the origin.
363 * @param flags Flags for the request.
365 typedef void (*GNUNET_MULTICAST_RequestCallback) (void *cls,
366 const struct GNUNET_PeerIdentity *sender,
367 const struct GNUNET_MessageHeader *req,
368 enum GNUNET_MULTICAST_MessageFlags flags);
372 * Function called whenever a group member is receiving a message fragment from
375 * If admission to the group is denied, this function is called once with the
376 * response of the @e origin (as given to GNUNET_MULTICAST_join_decision()) and
377 * then a second time with NULL to indicate that the connection failed for good.
379 * @param cls Closure (set from GNUNET_MULTICAST_member_join())
380 * @param msg Message from the origin, NULL if the origin shut down
381 * (or we were kicked out, and we should thus call
382 * GNUNET_MULTICAST_member_part() next)
384 typedef void (*GNUNET_MULTICAST_MessageCallback) (void *cls,
385 const struct GNUNET_MULTICAST_MessageHeader *msg);
389 * Opaque handle to a replay request from the multicast service.
391 struct GNUNET_MULTICAST_ReplayHandle;
395 * Functions with this signature are called whenever the multicast service needs
396 * a message to be replayed.
398 * Implementations of this function MUST call GNUNET_MULTICAST_replay() ONCE
399 * (with a message or an error); however, if the origin is destroyed or the
400 * group is left, the replay handle must no longer be used.
402 * @param cls Closure (set from GNUNET_MULTICAST_origin_start()
403 * or GNUNET_MULTICAST_member_join()).
404 * @param fragment_id Which message fragment should be replayed.
405 * @param rh Handle to pass to message transmit function.
407 typedef void (*GNUNET_MULTICAST_ReplayCallback) (void *cls,
408 uint64_t fragment_id,
409 struct GNUNET_MULTICAST_ReplayHandle *rh);
413 * Possible error codes during replay.
415 enum GNUNET_MULTICAST_ReplayErrorCode
419 * Everything is fine.
421 GNUNET_MULTICAST_REC_OK = 0,
424 * Message fragment has been discarded (likely transient message that was too old).
426 GNUNET_MULTICAST_REC_TRANSIENT_LOST = 1,
429 * Fragment ID counter was larger than the highest counter this
430 * replay function has ever encountered; thus it is likely the
431 * origin never sent it and we're at the HEAD of the multicast
432 * stream as far as this node is concerned.
434 GNUNET_MULTICAST_REC_PAST_HEAD = 2,
437 * Internal error (i.e. database error). Try some other peer.
439 GNUNET_MULTICAST_REC_INTERNAL_ERROR = 3
445 * Replay a message from the multicast group.
447 * @param rh Replay handle identifying which replay operation was requested.
448 * @param msg Replayed message fragment, NULL if unknown/error.
449 * @param ec Error code.
452 GNUNET_MULTICAST_replay (struct GNUNET_MULTICAST_ReplayHandle *rh,
453 const struct GNUNET_MULTICAST_MessageHeader *msg,
454 enum GNUNET_MULTICAST_ReplayErrorCode ec);
458 * Start a multicast group.
460 * Will advertise the origin in the P2P overlay network under the respective
461 * public key so that other peer can find this peer to join it. Peers that
462 * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
463 * either an existing group member (if the @a join_policy is permissive) or to
464 * the origin. If the joining is approved, the member is cleared for @e replay
465 * and will begin to receive messages transmitted to the group. If joining is
466 * disapproved, the failed candidate will be given a response. Members in the
467 * group can send messages to the origin (one at a time).
469 * @param cfg Configuration to use.
470 * @param cls Closure for the various callbacks that follow.
471 * @param priv_key ECC key that will be used to sign messages for this
472 * multicast session; public key is used to identify the
473 * multicast group; FIXME: we'll likely want to use
474 * NOT the p521 curve here, but a cheaper one in the future.
475 * @param join_policy What is the membership policy of the group?
476 * @param replay_cb Function that can be called to replay a message.
477 * @param test_cb Function multicast can use to test group membership.
478 * @param join_cb Function called to approve / disapprove joining of a peer.
479 * @param request_cb Function called with messages from group members.
480 * @return Handle for the origin, NULL on error.
482 struct GNUNET_MULTICAST_Origin *
483 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
485 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
486 enum GNUNET_MULTICAST_JoinPolicy join_policy,
487 GNUNET_MULITCAST_ReplayCallback replay_cb,
488 GNUNET_MULITCAST_MembershipTestCallback test_cb,
489 GNUNET_MULTICAST_JoinCallback join_cb,
490 GNUNET_MULTICAST_RequestCallback request_cb);
494 * Handle for a request to send a message to all multicast group members
497 struct GNUNET_MULTICAST_OriginMessageHandle;
501 * Send a message to the multicast group.
503 * @param origin Handle to the multicast group.
504 * @param size Number of bytes to transmit.
505 * @param cb Function to call to get the message.
506 * @param cb_cls Closure for @a cb.
507 * @return NULL on error (i.e. request already pending).
509 struct GNUNET_MULTICAST_OriginMessageHandle *
510 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
512 GNUNET_CONNECTION_TransmitReadyNotify cb,
517 * Cancel request for message transmission to multicast group.
519 * @param mh Request to cancel.
522 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginMessageHandle *mh);
526 * Stop a multicast group.
528 * @param origin Multicast group to stop.
531 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *origin);
535 * Join a multicast group.
537 * The entity joining is always the local peer. Further information about the
538 * candidate can be provided in the @a join_req message. If the join fails, the
539 * @a message_cb is invoked with a (failure) response and then with NULL. If
540 * the join succeeds, outstanding (state) messages and ongoing multicast
541 * messages will be given to the @a message_cb until the member decides to part
542 * the group. The @a test_cb and @a replay_cb functions may be called at
543 * anytime by the multicast service to support relaying messages to other
544 * members of the group.
546 * @param cfg Configuration to use.
547 * @param pub_key ECC key that identifies the group.
548 * @param origin Peer identity of the origin.
549 * @param max_known_fragment_id Largest known message fragment ID to the replay
550 * service; all messages with IDs larger than this ID will be replayed if
551 * possible (lower IDs will be considered known and thus only
552 * be replayed upon explicit request).
553 * @param max_known_state_fragment_id Largest known message fragment ID with a
554 * non-zero value for the @e state_delta; state messages with
555 * larger IDs than this value will be replayed with high priority
556 * (lower IDs will be considered known and thus only
557 * be replayed upon explicit request).
558 * @param replay_cb Function that can be called to replay messages
559 * this peer already knows from this group; NULL if this
560 * client is unable to support replay.
561 * @param test_cb Function multicast can use to test group membership.
562 * @param message_cb Function to be called for all message fragments we
563 * receive from the group, excluding those our @a replay_cb
565 * @param cls Closure for callbacks.
566 * @param join_req Application-dependent join message to be passed to origin
567 * (might, for example, contain a user
568 * bind user identity/pseudonym to peer identity, application-level
569 * message to origin, etc.).
570 * @return Handle for the member, NULL on error.
572 struct GNUNET_MULTICAST_Member *
573 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
574 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
575 const struct GNUNET_PeerIdentity *origin,
576 uint64_t max_known_fragment_id,
577 uint64_t max_known_state_fragment_id,
578 GNUNET_MULTICAST_ReplayCallback replay_cb,
579 GNUNET_MULITCAST_MembershipTestCallback test_cb,
580 GNUNET_MULTICAST_MessageCallback message_cb,
582 const struct GNUNET_MessageHeader *join_req);
586 * Handle for a replay request.
588 struct GNUNET_MULTICAST_MemberReplayHandle;
592 * Request a message to be replayed.
594 * Useful if messages below the @e max_known_*_id's given when joining are
595 * needed and not known to the client.
597 * @param member Membership handle.
598 * @param fragment_id ID of a message fragment that this client would like to see replayed.
599 * @param message_cb Function to be called for the replayed message.
600 * @param message_cb_cls Closure for @a message_cb.
601 * @return Replay request handle, NULL on error.
603 struct GNUNET_MULTICAST_MemberReplayHandle *
604 GNUNET_MULTICAST_member_request_replay (struct GNUNET_MULTICAST_Member *member,
605 uint64_t fragment_id,
606 GNUNET_MULTICAST_MessageCallback message_cb,
607 void *message_cb_cls);
611 * Cancel a replay request.
613 * @param rh Request to cancel.
616 GNUNET_MULTICAST_member_request_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh);
620 * Part a multicast group.
622 * Disconnects from all group members and invalidates the @a member handle.
624 * An application-dependent part message can be transmitted beforehand using
625 * GNUNET_MULTICAST_member_to_origin())
627 * @param member Membership handle.
630 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member);
634 * Handle for a message to be delivered from a member to the origin.
636 struct GNUNET_MULTICAST_MemberRequestHandle;
640 * Send a message to the origin of the multicast group.
642 * @param member Membership handle.
643 * @param size Number of bytes we want to send to origin.
644 * @param cb Callback to call to get the message.
645 * @param cb_cls Closure for @a cb.
646 * @return Handle to cancel request, NULL on error (i.e. request already pending).
648 struct GNUNET_MULTICAST_MemberRequestHandle *
649 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
651 GNUNET_CONNECTION_TransmitReadyNotify cb,
656 * Cancel request for message transmission to origin.
658 * @param rh Request to cancel.
661 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberRequestHandle *rh);
665 #if 0 /* keep Emacsens' auto-indent happy */
672 /* ifndef GNUNET_MULTICAST_SERVICE_H */
674 /* end of gnunet_multicast_service.h */