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
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,
98 GNUNET_NETWORK_STRUCT_BEGIN
101 * Header of a multicast message.
103 * This format is public as the replay mechanism must replay messages using the
104 * same format. This is needed as we want to integrity-check messages within
105 * the multicast layer to avoid multicasting mal-formed messages.
107 struct GNUNET_MULTICAST_MessageHeader
111 * Header for all multicast messages from the origin.
113 struct GNUNET_MessageHeader header;
116 * Number of hops this message has taken since the origin.
118 * Helpful to determine shortest paths to the origin for responses among
119 * honest peers; updated at each hop and thus not signed and not secure.
121 uint32_t hop_counter GNUNET_PACKED;
124 * ECC signature of the message.
126 * Signature must match the public key of the multicast group.
128 struct GNUNET_CRYPTO_EccSignature signature;
131 * Signature of the multicast message.
133 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
136 * Number of the message, monotonically increasing.
138 uint64_t message_id GNUNET_PACKED;
141 * Counter that monotonically increases whenever a member leaves the group.
143 * It has significance in case of replay requests: when a member has missed
144 * messages and gets a replay request: in this case if the @a group_generation
145 * is still the same before and after the missed messages, it means that no
146 * @e join or @a leave operations happened during the missed messages.
148 uint64_t group_generation GNUNET_PACKED;
151 * Difference between the current @a message_id and the @a message_id of the
152 * preceeding non-transient message.
154 * Zero for transient messages, @c UINT64_MAX for the first message, or any
155 * other message creating a full state reset by the origin. By subtracting
156 * @a state_delta from @a message_id, it is possible to calculate the message
157 * ID of the preceeding non-transient message and thus quickly traverse all
158 * state changes up to the last full state reset by the origin. This is
159 * useful as it allows joining clients to quickly reassemble the state while
160 * skipping over transient messages (and doing so without having to trust
161 * intermediaries to do it right, as the indices in the chain are signed). If
162 * the state chain is getting too long, the origin can choose to originate a
163 * state message with a state_delta of UINT64_MAX, thereby starting a new
164 * chain. The origin will then have to re-create the full state with state
165 * update messages following the state reset message.
167 * Open question: needed in multicast, or just have this in PSYC; still might
168 * be useful for selective fetching of messages. Still, that again should
169 * that not be done by PSYC?
171 uint64_t state_delta GNUNET_PACKED;
174 * Header for the message body.
176 * Three message types are specifically understood by multicast, namely "peer
177 * join", "peer leave", and "group terminated". Multicast will use those
178 * messages to update its list of candidates for content distribution. All
179 * other message types are application-specific.
181 struct GNUNET_MessageHeader body;
183 /* followed by message body */
186 GNUNET_NETWORK_STRUCT_END
190 * Handle that identifies a join request.
192 * Used to match calls to #GNUNET_MULTICAST_JoinCallback to the
193 * corresponding calls to GNUNET_MULTICAST_join_decision().
195 struct GNUNET_MULTICAST_JoinHandle;
199 * Function to call with the decision made for a membership change request.
201 * Must be called once and only once in response to an invocation of the
202 * #GNUNET_MULTICAST_JoinCallback.
204 * @param jh Join request handle.
205 * @param join_response Message to send in response to the joining peer;
206 * can also be used to redirect the peer to a different group at the
207 * application layer; this response is to be transmitted to the
208 * peer that issued the request even if admission is denied.
209 * @param is_admitted #GNUNET_OK if joining is approved,
210 * #GNUNET_SYSERR if it is disapproved;
211 * #GNUNET_NO for peers leaving.
212 * @param relay_count Number of relays given.
213 * @param relays Array of suggested peers that might be useful relays to use
214 * when joining the multicast group (essentially a list of peers that
215 * are already part of the multicast group and might thus be willing
216 * to help with routing). If empty, only this local peer (which must
217 * be the multicast origin) is a good candidate for building the
218 * multicast tree. Note that it is unnecessary to specify our own
219 * peer identity in this array.
222 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
223 const struct GNUNET_MessageHeader *join_response,
225 unsigned int relay_count,
226 const struct GNUNET_PeerIdentity *relays);
230 * Method called whenever another peer wants to join the multicast group.
232 * Implementations of this function must call GNUNET_MULTICAST_join_decision()
235 * @param cls Closure.
236 * @param peer Identity of the peer that wants to join.
237 * @param msg Application-dependent join message from the new user
238 * (might, for example, contain a user,
239 * bind user identity/pseudonym to peer identity, application-level
240 * message to origin, etc.).
241 * @param jh Join handle to pass to GNUNET_MULTICAST_join_decison().
243 typedef void (*GNUNET_MULTICAST_JoinCallback)(void *cls,
244 const struct GNUNET_PeerIdentity *peer,
245 const struct GNUNET_MessageHeader *msg,
246 struct GNUNET_MULTICAST_JoinHandle *jh);
250 * Method called whenever another peer wants to part the multicast group.
252 * A leave request must be always be honoured.
254 * @param cls Closure.
255 * @param peer Identity of the peer that wants to leave.
256 * @param msg Application-dependent leave message from the leaving user.
257 * @param jh Join handle to pass to GNUNET_MULTICAST_join_decison().
259 typedef void (*GNUNET_MULTICAST_PartCallback)(void *cls,
260 const struct GNUNET_PeerIdentity *peer,
261 const struct GNUNET_MessageHeader *msg,
262 struct GNUNET_MULTICAST_JoinHandle *jh);
266 * Handle to pass back for the answer of a membership test.
268 struct GNUNET_MULTICAST_MembershipTestHandle;
272 * Call informing multicast about the decision taken for membership test.
274 * @param mth Handle that was given for the query.
275 * @param decision #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
276 * #GNUNET_SYSERR if we cannot answer the membership test.
279 GNUNET_MULTICAST_membership_test_answer (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
284 * Method called to test if a member was in the group at a particular time.
286 * @param cls Closure.
287 * @param peer Identity of the peer that we want to test.
288 * @param message_id Message ID for which we want to do the test.
289 * @param mth Handle to give to GNUNET_MULTICAST_membership_test_answer().
291 typedef void (*GNUNET_MULTICAST_MembershipTestCallback)(void *cls,
292 const struct GNUNET_PeerIdentity *peer,
294 struct GNUNET_MULTICAST_MembershipTestHandle *mth);
298 * Function called whenever a group member has transmitted a message
299 * to the origin (other than joining or leaving).
301 * @param cls Closure (set from GNUNET_MULTICAST_origin_start).
302 * @param sender Identity of the sender.
303 * @param request_id Unique counter for the request from this sender to this origin.
304 * @param msg Message to the origin.
306 typedef void (*GNUNET_MULTICAST_RequestCallback) (void *cls,
307 const struct GNUNET_PeerIdentity *sender,
309 const struct GNUNET_MessageHeader *msg);
313 * Function called whenever a group member is receiving a message from
316 * If admission to the group is denied, this function is called once with the
317 * response of the @e origin (as given to GNUNET_MULTICAST_join_decision()) and
318 * then a second time with NULL to indicate that the connection failed for good.
320 * @param cls Closure (set from GNUNET_MULTICAST_member_join())
321 * @param message_id Unique number of the message, 0 for response to join request,
322 * normal message IDs in either direction start at 1.
323 * @param msg Message from the origin, NULL if the origin shut down
324 * (or we were kicked out, and we should thus call
325 * GNUNET_MULTICAST_member_leave() next)
327 typedef void (*GNUNET_MULTICAST_MulticastMessageCallback) (void *cls,
329 const struct GNUNET_MULTICAST_MessageHeader *msg);
333 * Opaque handle to a replay request from the multicast service.
335 struct GNUNET_MULTICAST_ReplayHandle;
339 * Functions with this signature are called whenever the multicast
340 * service needs a message to be replayed. Implementations of this
341 * function MUST call GNUNET_MULTICAST_replay() ONCE (with a message
342 * or an error); however, if the origin is destroyed or the group is
343 * left, the replay handle must no longer be used.
345 * @param cls Closure (set from GNUNET_MULTICAST_origin_start()
346 * or GNUNET_MULTICAST_member_join()).
347 * @param message_id Which message should be replayed.
348 * @param rh Handle to pass to message transmit function.
350 typedef void (*GNUNET_MULTICAST_ReplayCallback) (void *cls,
352 struct GNUNET_MULTICAST_ReplayHandle *rh);
356 * Possible error codes during replay.
358 enum GNUNET_MULTICAST_ReplayErrorCode
362 * Everything is fine.
364 GNUNET_MULTICAST_REC_OK = 0,
367 * Message has been discarded (likely transient message that was too old).
369 GNUNET_MULTICAST_REC_TRANSIENT_LOST = 1,
372 * Message ID counter was larger than the highest counter this
373 * replay function has ever encountered; thus it is likely the
374 * origin never sent it and we're at the HEAD of the multicast
375 * stream as far as this node is concerned.
377 GNUNET_MULTICAST_REC_PAST_HEAD = 2,
380 * Internal error (i.e. database error). Try some other peer.
382 GNUNET_MULTICAST_REC_INTERNAL_ERROR = 3
388 * Replay a message from the multicast group.
390 * @param rh Replay handle identifying which replay operation was requested.
391 * @param msg Replayed message, NULL if unknown/error.
392 * @param ec Error code.
395 GNUNET_MULTICAST_replay (struct GNUNET_MULTICAST_ReplayHandle *rh,
396 const struct GNUNET_MULTICAST_MessageHeader *msg,
397 enum GNUNET_MULTICAST_ReplayErrorCode ec);
401 * Handle to pass back for the answer of a ping.
403 struct GNUNET_MULTICAST_PingHandle;
407 * A response to a @e ping.
409 * @param rh Handle that was given for the ping.
410 * @param message_id Latest message ID seen by this peer for this group.
412 * @deprecated needed???
415 GNUNET_MULTICAST_pong (struct GNUNET_MULTICAST_ReplayHandle *rh,
416 uint64_t message_id);
420 * Method called whenever a @e ping is received from another member.
422 * A @e ping is sent after a period of inactivity to check if the member has not
423 * missed any messages. A ping contains the latest message ID a member has
424 * seen, and must be answered with GNUNET_MULTICAST_pong() containing the latest
425 * message ID seen by this peer. If the latest message ID on this peer is higher, the
426 * missing messages must be replayed to the requesting member using
427 * GNUNET_MULTICAST_replay().
429 * @param cls Closure.
430 * @param peer Identity of the peer who sent the ping.
431 * @param latest_message_id Latest message ID seen by the requesting member.
432 * @param rh Handle to pass back to GNUNET_MULTICAST_pong() or GNUNET_MULTICAST_replay().
434 * @deprecated needed???
436 typedef void (*GNUNET_MULTICAST_PingCallback)(void *cls,
437 const struct GNUNET_PeerIdentity *peer,
438 uint64_t latest_messaged_id
439 struct GNUNET_MULTICAST_ReplayHandle *rh);
443 * Start a multicast group.
445 * Will advertise the origin in the P2P overlay network under the respective
446 * public key so that other peer can find this peer to join it. Peers that
447 * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
448 * either an existing group member (if the @a join_policy is permissive) or to
449 * the origin. If the joining is approved, the member is cleared for @e replay
450 * and will begin to receive messages transmitted to the group. If joining is
451 * disapproved, the failed candidate will be given a response. Members in the
452 * group can send messages to the origin (one at a time).
454 * @param cfg Configuration to use.
455 * @param cls Closure for the various callbacks that follow.
456 * @param priv_key ECC key that will be used to sign messages for this
457 * multicast session; public key is used to identify the
458 * multicast group; FIXME: we'll likely want to use
459 * NOT the p521 curve here, but a cheaper one in the future.
460 * @param join_policy What is the membership policy of the group?
461 * @param replay_cb Function that can be called to replay a message.
462 * @param test_cb Function multicast can use to test group membership.
463 * @param ping_cb Function called to answer a ping. -- Gabor: remove this one!? deprecated?
464 * @param join_cb Function called to approve / disapprove joining of a peer.
465 * @param part_cb Function called when a member wants to part the group.
466 * @param request_cb Function called with messages from group members.
467 * @return Handle for the origin, NULL on error.
469 struct GNUNET_MULTICAST_Origin *
470 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
472 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
473 enum GNUNET_MULTICAST_JoinPolicy join_policy,
474 GNUNET_MULITCAST_ReplayCallback replay_cb,
475 GNUNET_MULITCAST_MembershipTestCallback test_cb,
476 GNUNET_MULITCAST_PingCallback ping_cb,
477 GNUNET_MULTICAST_JoinCallback join_cb,
478 GNUNET_MULTICAST_PartCallback part_cb,
479 GNUNET_MULTICAST_RequestCallback request_cb);
483 * Handle for a request to send a message to all multicast group members
486 struct GNUNET_MULTICAST_MulticastRequest;
490 * Send a message to the multicast group.
492 * @param origin Handle to the multicast group.
493 * @param size Number of bytes to transmit.
494 * @param cb Function to call to get the message.
495 * @param cb_cls Closure for @a cb.
496 * @return NULL on error (i.e. request already pending).
498 struct GNUNET_MULTICAST_MulticastRequest *
499 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
501 GNUNET_CONNECTION_TransmitReadyNotify cb,
506 * Cancel request for message transmission to multicast group.
508 * @param mr Request to cancel.
511 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_MulticastRequest *mr);
515 * End a multicast group.
517 * @param origin multicast group to terminate
520 GNUNET_MULTICAST_origin_end (struct GNUNET_MULTICAST_Origin *origin);
524 * Join a multicast group.
526 * The entity joining is always the local peer. Further information about the
527 * candidate can be provided in the @a join_req message. If the join fails, the
528 * @a message_cb is invoked with a (failure) response and then with NULL. If
529 * the join succeeds, outstanding (state) messages and ongoing multicast
530 * messages will be given to the @a message_cb until the member decides to leave
531 * the group. The @a test_cb and @a replay_cb functions may be called at
532 * anytime by the multicast service to support relaying messages to other
533 * members of the group.
535 * @param cfg Configuration to use.
536 * @param cls Closure for callbacks.
537 * @param pub_key ECC key that identifies the group.
538 * @param max_known_message_id Largest known message ID to the replay service;
539 * all messages with IDs larger than this ID will be replayed if
540 * possible (lower IDs will be considered known and thus only
541 * be replayed upon explicit request).
542 * @param max_known_state_message_id Largest known message ID with a non-zero
543 * value for the @e state_delta; state messages with
544 * larger IDs than this value will be replayed with high priority
545 * (lower IDs will be considered known and thus only
546 * be replayed upon explicit request).
547 * @param replay_cb Function that can be called to replay messages
548 * this peer already knows from this group; NULL if this
549 * client is unable to support replay.
550 * @param test_cb Function multicast can use to test group membership.
551 * @param message_cb Function to be called for all messages we
552 * receive from the group, excluding those our @a replay_cb
554 * @param join_req Application-dependent join message to be passed to origin
555 * (might, for example, contain a user
556 * bind user identity/pseudonym to peer identity, application-level
557 * message to origin, etc.).
558 * @return Handle for the member, NULL on error.
560 struct GNUNET_MULTICAST_Member *
561 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
563 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
564 uint64_t max_known_message_id,
565 uint64_t max_known_state_message_id,
566 GNUNET_MULTICAST_ReplayCallback replay_cb,
567 GNUNET_MULITCAST_MembershipTestCallback test_cb,
568 GNUNET_MULTICAST_MulticastMessageCallback message_cb,
569 const struct GNUNET_MessageHeader *join_req);
573 * Handle for a replay request.
575 struct GNUNET_MULTICAST_ReplayRequest;
579 * Request a message to be replayed.
581 * Useful if messages below the @e max_known_*_id's given when joining are
582 * needed and not known to the client.
584 * @param member Membership handle.
585 * @param message_id ID of a message that this client would like to see replayed.
586 * @param message_cb Function to be called for the replayed message.
587 * @param message_cb_cls Closure for @a message_cb.
588 * @return Replay request handle, NULL on error.
590 struct GNUNET_MULTICAST_ReplayRequest *
591 GNUNET_MULTICAST_member_request_replay (struct GNUNET_MULTICAST_Member *member,
593 GNUNET_MULTICAST_MulticastMessageCallback message_cb,
594 void *message_cb_cls);
598 * Cancel a replay request.
600 * @param rr Request to cancel.
603 GNUNET_MULTICAST_member_request_replay_cancel (struct GNUNET_MULTICAST_ReplayRequest *rr);
607 * Part a multicast group.
609 * @param member Membership handle.
612 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member);
616 * Handle for a message to be delivered to the origin.
618 struct GNUNET_MULTICAST_ResponseRequest;
622 * Send a message to the origin of the multicast group.
624 * @param member Membership handle.
625 * @param size Number of bytes we want to send to origin.
626 * @param cb Callback to call to get the message.
627 * @param cb_cls Closure for @a cb.
628 * @return Handle to cancel request, NULL on error (i.e. request already pending).
630 struct GNUNET_MULTICAST_ResponseRequest *
631 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
633 GNUNET_CONNECTION_TransmitReadyNotify cb,
638 * Cancel request for message transmission to origin.
640 * @param rr Request to cancel.
643 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_ResponseRequest *rr);
647 #if 0 /* keep Emacsens' auto-indent happy */
654 /* ifndef GNUNET_MULTICAST_SERVICE_H */
656 /* end of gnunet_multicast_service.h */