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
27 #ifndef GNUNET_MULTICAST_SERVICE_H
28 #define GNUNET_MULTICAST_SERVICE_H
33 #if 0 /* keep Emacsens' auto-indent happy */
38 #include "gnunet_util_lib.h"
39 #include "gnunet_transport_service.h"
42 * Version number of GNUnet-multicast API.
44 #define GNUNET_MULTICAST_VERSION 0x00000000
48 * Opaque handle for a multicast group member.
50 struct GNUNET_MULTICAST_Member;
53 * Handle for the origin of a multicast group.
55 struct GNUNET_MULTICAST_Origin;
58 * Group membership policies.
60 enum GNUNET_MULTICAST_JoinPolicy
63 * Anyone can join the group, without announcing his presence;
64 * all messages are always public and can be distributed freely.
65 * Joins may be announced, but this is not required.
67 GNUNET_MULTICAST_JP_ANONYMOUS = 0,
70 * Origin must approve membership to the group, messages must only be
71 * distributed to current group members. This includes the group
72 * state as well as transient messages.
74 GNUNET_MULTICAST_JP_PRIVATE = 1
78 * Anyone can freely join the group (no approval required); however,
79 * transient messages must only be distributed to current group
80 * members, so the origin must still acknowledge that the member
81 * joined before transient messages are delivered. As approval is
82 * guaranteed, the presistent group state can be synchronized freely
83 * immediately, prior to origin confirmation.
85 GNUNET_MULTICAST_JP_OPEN = 2
92 * Opaque handle to a replay request from the multicast service.
94 struct GNUNET_MULTICAST_ReplayHandle;
98 * Functions with this signature are called whenever the multicast
99 * service needs a message to be replayed. Implementations of this
100 * function MUST call GNUNET_MULTICAST_replay() ONCE (with a message
101 * or an error); however, if the origin is destroyed or the group is
102 * left, the replay handle must no longer be used.
104 * @param cls Closure (set from GNUNET_MULTICAST_origin_start()
105 * or GNUNET_MULTICAST_member_join()).
106 * @param message_id Which message should be replayed.
107 * @param rh Handle to pass to message transmit function.
109 typedef void (*GNUNET_MULTICAST_ReplayCallback) (void *cls,
111 struct GNUNET_MULTICAST_ReplayHandle *rh);
115 * Possible error codes during replay.
117 enum GNUNET_MULTICAST_ReplayErrorCode
121 * Everything is fine.
123 GNUNET_MULTICAST_REC_OK = 0,
126 * Message has been discarded (likely transient message that was too old).
128 GNUNET_MULTICAST_REC_TRANSIENT_LOST = 1,
131 * Message ID counter was larger than the highest counter this
132 * replay function has ever encountered; thus it is likely the
133 * origin never sent it and we're at the HEAD of the multicast
134 * stream as far as this node is concerned.
136 GNUNET_MULTICAST_REC_PAST_HEAD = 2,
139 * Internal error (i.e. database error). Try some other peer.
141 GNUNET_MULTICAST_REC_INTERNAL_ERROR = 3
146 GNUNET_NETWORK_STRUCT_BEGIN
149 * Header of a multicast message.
151 * This format is public as the replay mechanism must replay messages using the
152 * same format. This is needed as we want to integrity-check messages within
153 * the multicast layer to avoid multicasting mal-formed messages.
155 struct GNUNET_MULTICAST_MessageHeader
159 * Header for all multicast messages from the origin.
161 struct GNUNET_MessageHeader header;
164 * Number of hops this message has taken since the origin.
166 * Helpful to determine shortest paths to the origin for responses among
167 * honest peers; updated at each hop and thus not signed and not secure.
169 uint32_t hop_counter GNUNET_PACKED;
172 * ECC signature of the message.
174 * Signature must match the public key of the multicast group.
176 struct GNUNET_CRYPTO_EccSignature signature;
179 * Signature of the multicast message.
181 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
184 * Number of the message, monotonically increasing.
186 uint64_t message_id GNUNET_PACKED;
189 * Counter that monotonically increases whenever a member leaves the group.
191 uint64_t group_generation GNUNET_PACKED;
194 * Difference between the current @a message_id and the @a message_id of the
195 * preceeding non-transient message.
197 * Zero for transient messages, @c UINT64_MAX for the first message, or any
198 * other message creating a full state reset by the origin. By subtracting
199 * @a state_delta from @a message_id, it is possible to calculate the message
200 * ID of the preceeding non-transient message and thus quickly traverse all
201 * state changes up to the last full state reset by the origin. This is
202 * useful as it allows joining clients to quickly reassemble the state while
203 * skipping over transient messages (and doing so without having to trust
204 * intermediaries to do it right, as the indices in the chain are signed). If
205 * the state chain is getting too long, the origin can choose to originate a
206 * state message with a state_delta of UINT64_MAX, thereby starting a new
207 * chain. The origin will then have to re-create the full state with state
208 * update messages following the state reset message.
210 * Open question: needed in multicast, or just have this in PSYC; still might
211 * be useful for selective fetching of messages. Still, that again should
212 * that not be done by PSYC?
214 uint64_t state_delta GNUNET_PACKED;
217 * Header for the message body.
219 * Three message types are specifically understood by multicast, namely "peer
220 * join", "peer leave", and "group terminated". Multicast will use those
221 * messages to update its list of candidates for content distribution. All
222 * other message types are application-specific.
224 struct GNUNET_MessageHeader body;
226 /* followed by message body */
229 GNUNET_NETWORK_STRUCT_END
233 * Replay a message from the multicast group.
235 * @param rh Replay handle identifying which replay operation was requested.
236 * @param msg Replayed message, NULL if unknown/error.
237 * @param ec Error code.
240 GNUNET_MULTICAST_replay (struct GNUNET_MULTICAST_ReplayHandle *rh,
241 const struct GNUNET_MULTICAST_MessageHeader *msg,
242 enum GNUNET_MULTICAST_ReplayErrorCode ec);
246 * Handle that identifies a join request.
248 * Used to match calls to #GNUNET_MULTICAST_MembershipChangeCallback to the
249 * corresponding calls to GNUNET_MULTICAST_join_decision().
251 struct GNUNET_MULTICAST_JoinHande;
255 * Function to call with the decision made for a membership change request.
257 * Must be called once and only once in response to an invocation of the
258 * #GNUNET_MULTICAST_MembershipChangeCallback.
260 * @param jh Join request handle.
261 * @param join_response Message to send in response to the joining peer;
262 * can also be used to redirect the peer to a different group at the
263 * application layer; this response is to be transmitted to the
264 * peer that issued the request even if admission is denied.
265 * @param is_admitted #GNUNET_OK if joining is approved,
266 * #GNUNET_SYSERR if it is disapproved;
267 * #GNUNET_NO for peers leaving.
268 * @param relay_count Number of relays given.
269 * @param relays Array of suggested peers that might be useful relays to use
270 * when joining the multicast group (essentially a list of peers that
271 * are already part of the multicast group and might thus be willing
272 * to help with routing). If empty, only this local peer (which must
273 * be the multicast origin) is a good candidate for building the
274 * multicast tree. Note that it is unnecessary to specify our own
275 * peer identity in this array.
278 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
279 const struct GNUNET_MessageHeader *join_response,
281 unsigned int relay_count,
282 const struct GNUNET_PeerIdentity *relays);
286 * Method called whenever another peer wants to join or has left a multicast
289 * Implementations of this function must call GNUNET_MULTICAST_join_decision()
292 * @param cls Closure.
293 * @param peer Identity of the peer that wants to join or leave.
294 * @param join_req Application-dependent join message from the new user
295 * (might, for example, contain a user,
296 * bind user identity/pseudonym to peer identity, application-level
297 * message to origin, etc.).
298 * @param is_joining #GNUNET_YES if the peer wants to join, #GNUNET_NO if the peer left.
299 * @param jh Join handle to pass to GNUNET_MULTICAST_join_decison().
301 typedef void (*GNUNET_MULTICAST_MembershipChangeCallback)(void *cls,
302 const struct GNUNET_PeerIdentity *peer,
303 const struct GNUNET_MessageHeader *join_req,
305 struct GNUNET_MULTICAST_JoinHandle *jh);
309 * Handle to pass back for the answer of a membership test.
311 struct GNUNET_MULTICAST_MembershipTestHandle;
315 * Call informing multicast about the decision taken for membership test.
317 * @param mth Handle that was given for the query.
318 * @param decision #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
319 * #GNUNET_SYSERR if we cannot answer the membership test.
322 GNUNET_MULTICAST_membership_test_answer (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
327 * Method called to test if a member was in the group at a particular time.
329 * @param cls Closure.
330 * @param peer Identity of the peer that we want to test.
331 * @param message_id Message ID for which we want to do the test.
332 * @param mth Handle to give to GNUNET_MULTICAST_membership_test_answer().
334 typedef void (*GNUNET_MULTICAST_MembershipTestCallback)(void *cls,
335 const struct GNUNET_PeerIdentity *peer,
337 struct GNUNET_MULTICAST_MembershipTestHandle *mth);
341 * Function called whenever a group member has transmitted a message
342 * to the origin (other than joining or leaving).
344 * @param cls Closure (set from GNUNET_MULTICAST_origin_start).
345 * @param sender Identity of the sender.
346 * @param response_id Unique counter for the response from this sender to this origin.
347 * @param msg Message to the origin.
349 typedef void (*GNUNET_MULTICAST_ResponseCallback) (void *cls,
350 const struct GNUNET_PeerIdentity *sender,
351 uint64_t response_id,
352 const struct GNUNET_MessageHeader *msg);
356 * Function called whenever a group member is receiving a message from
357 * the origin. If admission to the group is denied, this function is
358 * called once with the response of the @e origin (as given to
359 * GNUNET_MULTICAST_join_decision()) and then a second time with NULL
360 * to indicate that the connection failed for good.
362 * @param cls Closure (set from GNUNET_MULTICAST_member_join())
363 * @param message_id Unique number of the message, 0 for response to join request,
364 * normal message IDs in either direction start at 1.
365 * @param msg Message from the origin, NULL if the origin shut down
366 * (or we were kicked out, and we should thus call
367 * GNUNET_MULTICAST_member_leave() next)
369 typedef void (*GNUNET_MULTICAST_MulticastMessageCallback) (void *cls,
371 const struct GNUNET_MULTICAST_MessageHeader *msg);
375 * Start a multicast group.
377 * Will advertise the origin in the P2P overlay network under the respective
378 * public key so that other peer can find this peer to join it. Peers that
379 * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
380 * either an existing group member (if the @a join_policy is permissive) or to
381 * the origin. If the joining is approved, the member is cleared for @e replay
382 * and will begin to receive messages transmitted to the group. If joining is
383 * disapproved, the failed candidate will be given a response. Members in the
384 * group can send messages to the origin (one at a time).
386 * @param cfg Configuration to use.
387 * @param cls Closure for the various callbacks that follow.
388 * @param priv_key ECC key that will be used to sign messages for this
389 * multicast session; public key is used to identify the
390 * multicast group; FIXME: we'll likely want to use
391 * NOT the p521 curve here, but a cheaper one in the future.
392 * @param join_policy What is the membership policy of the group?
393 * @param replay_cb Function that can be called to replay a message.
394 * @param test_cb Function multicast can use to test group membership.
395 * @param join_cb Function called to approve / disapprove joining of a peer,
396 * and to inform about a leaving member.
397 * @param leave_cb Function called to inform about a leaving member.
398 * @param response_cb Function called with messages from group members.
399 * @return Handle for the origin, NULL on error.
401 struct GNUNET_MULTICAST_Origin *
402 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
404 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
405 enum GNUNET_MULTICAST_JoinPolicy join_policy,
406 GNUNET_MULITCAST_ReplayCallback replay_cb,
407 GNUNET_MULITCAST_MembershipTestCallback test_cb,
408 GNUNET_MULTICAST_MembershipChangeCallback join_cb,
409 GNUNET_MULTICAST_ResponseCallback response_cb);
413 * Handle for a request to send a message to all multicast group members
416 struct GNUNET_MULTICAST_MulticastRequest;
420 * Send a message to the multicast group.
422 * @param origin Handle to the multicast group.
423 * @param size Number of bytes to transmit.
424 * @param cb Function to call to get the message.
425 * @param cb_cls Closure for @a cb.
426 * @return NULL on error (i.e. request already pending).
428 struct GNUNET_MULTICAST_MulticastRequest *
429 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
431 GNUNET_CONNECTION_TransmitReadyNotify cb,
436 * Cancel request for message transmission to multicast group.
438 * @param mr Request to cancel.
441 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_MulticastRequest *mr);
445 * End a multicast group.
447 * @param origin multicast group to terminate
450 GNUNET_MULTICAST_origin_end (struct GNUNET_MULTICAST_Origin *origin);
454 * Join a multicast group.
456 * The entity joining is always the local peer. Further information about the
457 * candidate can be provided in the @a join_req message. If the join fails, the
458 * @a message_cb is invoked with a (failure) response and then with NULL. If
459 * the join succeeds, outstanding (state) messages and ongoing multicast
460 * messages will be given to the @a message_cb until the member decides to leave
461 * the group. The @a test_cb and @a replay_cb functions may be called at
462 * anytime by the multicast service to support relaying messages to other
463 * members of the group.
465 * @param cfg Configuration to use.
466 * @param cls Closure for callbacks.
467 * @param pub_key ECC key that identifies the group.
468 * @param max_known_message_id Largest known message ID to the replay service;
469 * all messages with IDs larger than this ID will be replayed if
470 * possible (lower IDs will be considered known and thus only
471 * be replayed upon explicit request).
472 * @param max_known_state_message_id Largest known message ID with a non-zero
473 * value for the @e state_delta; state messages with
474 * larger IDs than this value will be replayed with high priority
475 * (lower IDs will be considered known and thus only
476 * be replayed upon explicit request).
477 * @param replay_cb Function that can be called to replay messages
478 * this peer already knows from this group; NULL if this
479 * client is unable to support replay.
480 * @param test_cb Function multicast can use to test group membership.
481 * @param message_cb Function to be called for all messages we
482 * receive from the group, excluding those our @a replay_cb
484 * @param join_req Application-dependent join message to be passed to origin
485 * (might, for example, contain a user
486 * bind user identity/pseudonym to peer identity, application-level
487 * message to origin, etc.).
488 * @return Handle for the member, NULL on error.
490 struct GNUNET_MULTICAST_Member *
491 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
493 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
494 uint64_t max_known_message_id,
495 uint64_t max_known_state_message_id,
496 GNUNET_MULTICAST_ReplayCallback replay_cb,
497 GNUNET_MULITCAST_MembershipTestCallback test_cb,
498 GNUNET_MULTICAST_MulticastMessageCallback message_cb,
499 const struct GNUNET_MessageHeader *join_req);
503 * Handle for a replay request.
505 struct GNUNET_MULTICAST_ReplayRequest;
509 * Request a message to be replayed.
511 * Useful if messages below the @e max_known_*_id's given when joining are
512 * needed and not known to the client.
514 * @param member Membership handle.
515 * @param message_id ID of a message that this client would like to see replayed.
516 * @param message_cb Function to be called for the replayed message.
517 * @param message_cb_cls Closure for @a message_cb.
518 * @return Replay request handle, NULL on error.
520 struct GNUNET_MULTICAST_ReplayRequest *
521 GNUNET_MULTICAST_member_request_replay (struct GNUNET_MULTICAST_Member *member,
523 GNUNET_MULTICAST_MulticastMessageCallback message_cb,
524 void *message_cb_cls);
528 * Cancel a replay request.
530 * @param rr Request to cancel.
533 GNUNET_MULTICAST_member_request_replay_cancel (struct GNUNET_MULTICAST_ReplayRequest *rr);
537 * Leave a multicast group.
539 * @param member Membership handle.
542 GNUNET_MULTICAST_member_leave (struct GNUNET_MULTICAST_Member *member);
546 * Handle for a message to be delivered to the origin.
548 struct GNUNET_MULTICAST_ResponseRequest;
552 * Send a message to the origin of the multicast group.
554 * @param member Membership handle.
555 * @param size Number of bytes we want to send to origin.
556 * @param cb Callback to call to get the message.
557 * @param cb_cls Closure for @a cb.
558 * @return Handle to cancel request, NULL on error (i.e. request already pending).
560 struct GNUNET_MULTICAST_ResponseRequest *
561 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
563 GNUNET_CONNECTION_TransmitReadyNotify cb,
568 * Cancel request for message transmission to origin.
570 * @param rr Request to cancel.
573 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_ResponseRequest *rr);
577 #if 0 /* keep Emacsens' auto-indent happy */
584 /* ifndef GNUNET_MULTICAST_SERVICE_H */
586 /* end of gnunet_multicast_service.h */