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,
99 * Opaque handle to a replay request from the multicast service.
101 struct GNUNET_MULTICAST_ReplayHandle;
105 * Functions with this signature are called whenever the multicast
106 * service needs a message to be replayed. Implementations of this
107 * function MUST call GNUNET_MULTICAST_replay() ONCE (with a message
108 * or an error); however, if the origin is destroyed or the group is
109 * left, the replay handle must no longer be used.
111 * @param cls Closure (set from GNUNET_MULTICAST_origin_start()
112 * or GNUNET_MULTICAST_member_join()).
113 * @param message_id Which message should be replayed.
114 * @param rh Handle to pass to message transmit function.
116 typedef void (*GNUNET_MULTICAST_ReplayCallback) (void *cls,
118 struct GNUNET_MULTICAST_ReplayHandle *rh);
122 * Possible error codes during replay.
124 enum GNUNET_MULTICAST_ReplayErrorCode
128 * Everything is fine.
130 GNUNET_MULTICAST_REC_OK = 0,
133 * Message has been discarded (likely transient message that was too old).
135 GNUNET_MULTICAST_REC_TRANSIENT_LOST = 1,
138 * Message ID counter was larger than the highest counter this
139 * replay function has ever encountered; thus it is likely the
140 * origin never sent it and we're at the HEAD of the multicast
141 * stream as far as this node is concerned.
143 GNUNET_MULTICAST_REC_PAST_HEAD = 2,
146 * Internal error (i.e. database error). Try some other peer.
148 GNUNET_MULTICAST_REC_INTERNAL_ERROR = 3
153 GNUNET_NETWORK_STRUCT_BEGIN
156 * Header of a multicast message.
158 * This format is public as the replay mechanism must replay messages using the
159 * same format. This is needed as we want to integrity-check messages within
160 * the multicast layer to avoid multicasting mal-formed messages.
162 struct GNUNET_MULTICAST_MessageHeader
166 * Header for all multicast messages from the origin.
168 struct GNUNET_MessageHeader header;
171 * Number of hops this message has taken since the origin.
173 * Helpful to determine shortest paths to the origin for responses among
174 * honest peers; updated at each hop and thus not signed and not secure.
176 uint32_t hop_counter GNUNET_PACKED;
179 * ECC signature of the message.
181 * Signature must match the public key of the multicast group.
183 struct GNUNET_CRYPTO_EccSignature signature;
186 * Signature of the multicast message.
188 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
191 * Number of the message, monotonically increasing.
193 uint64_t message_id GNUNET_PACKED;
196 * Counter that monotonically increases whenever a member leaves the group.
198 * It has significance in case of replay requests: when a member has missed
199 * messages and gets a replay request: in this case if the @a group_generation
200 * is still the same before and after the missed messages, it means that no
201 * @e join or @a leave operations happened during the missed messages.
203 uint64_t group_generation GNUNET_PACKED;
206 * Difference between the current @a message_id and the @a message_id of the
207 * preceeding non-transient message.
209 * Zero for transient messages, @c UINT64_MAX for the first message, or any
210 * other message creating a full state reset by the origin. By subtracting
211 * @a state_delta from @a message_id, it is possible to calculate the message
212 * ID of the preceeding non-transient message and thus quickly traverse all
213 * state changes up to the last full state reset by the origin. This is
214 * useful as it allows joining clients to quickly reassemble the state while
215 * skipping over transient messages (and doing so without having to trust
216 * intermediaries to do it right, as the indices in the chain are signed). If
217 * the state chain is getting too long, the origin can choose to originate a
218 * state message with a state_delta of UINT64_MAX, thereby starting a new
219 * chain. The origin will then have to re-create the full state with state
220 * update messages following the state reset message.
222 * Open question: needed in multicast, or just have this in PSYC; still might
223 * be useful for selective fetching of messages. Still, that again should
224 * that not be done by PSYC?
226 uint64_t state_delta GNUNET_PACKED;
229 * Header for the message body.
231 * Three message types are specifically understood by multicast, namely "peer
232 * join", "peer leave", and "group terminated". Multicast will use those
233 * messages to update its list of candidates for content distribution. All
234 * other message types are application-specific.
236 struct GNUNET_MessageHeader body;
238 /* followed by message body */
241 GNUNET_NETWORK_STRUCT_END
245 * Replay a message from the multicast group.
247 * @param rh Replay handle identifying which replay operation was requested.
248 * @param msg Replayed message, NULL if unknown/error.
249 * @param ec Error code.
252 GNUNET_MULTICAST_replay (struct GNUNET_MULTICAST_ReplayHandle *rh,
253 const struct GNUNET_MULTICAST_MessageHeader *msg,
254 enum GNUNET_MULTICAST_ReplayErrorCode ec);
258 * Handle that identifies a join request.
260 * Used to match calls to #GNUNET_MULTICAST_MembershipChangeCallback to the
261 * corresponding calls to GNUNET_MULTICAST_join_decision().
263 struct GNUNET_MULTICAST_JoinHande;
267 * Function to call with the decision made for a membership change request.
269 * Must be called once and only once in response to an invocation of the
270 * #GNUNET_MULTICAST_MembershipChangeCallback.
272 * @param jh Join request handle.
273 * @param join_response Message to send in response to the joining peer;
274 * can also be used to redirect the peer to a different group at the
275 * application layer; this response is to be transmitted to the
276 * peer that issued the request even if admission is denied.
277 * @param is_admitted #GNUNET_OK if joining is approved,
278 * #GNUNET_SYSERR if it is disapproved;
279 * #GNUNET_NO for peers leaving.
280 * @param relay_count Number of relays given.
281 * @param relays Array of suggested peers that might be useful relays to use
282 * when joining the multicast group (essentially a list of peers that
283 * are already part of the multicast group and might thus be willing
284 * to help with routing). If empty, only this local peer (which must
285 * be the multicast origin) is a good candidate for building the
286 * multicast tree. Note that it is unnecessary to specify our own
287 * peer identity in this array.
290 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
291 const struct GNUNET_MessageHeader *join_response,
293 unsigned int relay_count,
294 const struct GNUNET_PeerIdentity *relays);
298 * Method called whenever another peer wants to join or has left a multicast
301 * Implementations of this function must call GNUNET_MULTICAST_join_decision()
304 * @param cls Closure.
305 * @param peer Identity of the peer that wants to join or leave.
306 * @param join_req Application-dependent join message from the new user
307 * (might, for example, contain a user,
308 * bind user identity/pseudonym to peer identity, application-level
309 * message to origin, etc.).
310 * @param is_joining #GNUNET_YES if the peer wants to join, #GNUNET_NO if the peer left.
311 * @param jh Join handle to pass to GNUNET_MULTICAST_join_decison().
313 typedef void (*GNUNET_MULTICAST_MembershipChangeCallback)(void *cls,
314 const struct GNUNET_PeerIdentity *peer,
315 const struct GNUNET_MessageHeader *join_req,
317 struct GNUNET_MULTICAST_JoinHandle *jh);
321 * Handle to pass back for the answer of a membership test.
323 struct GNUNET_MULTICAST_MembershipTestHandle;
327 * Call informing multicast about the decision taken for membership test.
329 * @param mth Handle that was given for the query.
330 * @param decision #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
331 * #GNUNET_SYSERR if we cannot answer the membership test.
334 GNUNET_MULTICAST_membership_test_answer (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
339 * Method called to test if a member was in the group at a particular time.
341 * @param cls Closure.
342 * @param peer Identity of the peer that we want to test.
343 * @param message_id Message ID for which we want to do the test.
344 * @param mth Handle to give to GNUNET_MULTICAST_membership_test_answer().
346 typedef void (*GNUNET_MULTICAST_MembershipTestCallback)(void *cls,
347 const struct GNUNET_PeerIdentity *peer,
349 struct GNUNET_MULTICAST_MembershipTestHandle *mth);
353 * Function called whenever a group member has transmitted a message
354 * to the origin (other than joining or leaving).
356 * @param cls Closure (set from GNUNET_MULTICAST_origin_start).
357 * @param sender Identity of the sender.
358 * @param response_id Unique counter for the response from this sender to this origin.
359 * @param msg Message to the origin.
361 typedef void (*GNUNET_MULTICAST_ResponseCallback) (void *cls,
362 const struct GNUNET_PeerIdentity *sender,
363 uint64_t response_id,
364 const struct GNUNET_MessageHeader *msg);
368 * Function called whenever a group member is receiving a message from
369 * the origin. If admission to the group is denied, this function is
370 * called once with the response of the @e origin (as given to
371 * GNUNET_MULTICAST_join_decision()) and then a second time with NULL
372 * to indicate that the connection failed for good.
374 * @param cls Closure (set from GNUNET_MULTICAST_member_join())
375 * @param message_id Unique number of the message, 0 for response to join request,
376 * normal message IDs in either direction start at 1.
377 * @param msg Message from the origin, NULL if the origin shut down
378 * (or we were kicked out, and we should thus call
379 * GNUNET_MULTICAST_member_leave() next)
381 typedef void (*GNUNET_MULTICAST_MulticastMessageCallback) (void *cls,
383 const struct GNUNET_MULTICAST_MessageHeader *msg);
387 * Start a multicast group.
389 * Will advertise the origin in the P2P overlay network under the respective
390 * public key so that other peer can find this peer to join it. Peers that
391 * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
392 * either an existing group member (if the @a join_policy is permissive) or to
393 * the origin. If the joining is approved, the member is cleared for @e replay
394 * and will begin to receive messages transmitted to the group. If joining is
395 * disapproved, the failed candidate will be given a response. Members in the
396 * group can send messages to the origin (one at a time).
398 * @param cfg Configuration to use.
399 * @param cls Closure for the various callbacks that follow.
400 * @param priv_key ECC key that will be used to sign messages for this
401 * multicast session; public key is used to identify the
402 * multicast group; FIXME: we'll likely want to use
403 * NOT the p521 curve here, but a cheaper one in the future.
404 * @param join_policy What is the membership policy of the group?
405 * @param replay_cb Function that can be called to replay a message.
406 * @param test_cb Function multicast can use to test group membership.
407 * @param join_cb Function called to approve / disapprove joining of a peer,
408 * and to inform about a leaving member.
409 * @param leave_cb Function called to inform about a leaving member.
410 * @param response_cb Function called with messages from group members.
411 * @return Handle for the origin, NULL on error.
413 struct GNUNET_MULTICAST_Origin *
414 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
416 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
417 enum GNUNET_MULTICAST_JoinPolicy join_policy,
418 GNUNET_MULITCAST_ReplayCallback replay_cb,
419 GNUNET_MULITCAST_MembershipTestCallback test_cb,
420 GNUNET_MULTICAST_MembershipChangeCallback join_cb,
421 GNUNET_MULTICAST_ResponseCallback response_cb);
425 * Handle for a request to send a message to all multicast group members
428 struct GNUNET_MULTICAST_MulticastRequest;
432 * Send a message to the multicast group.
434 * @param origin Handle to the multicast group.
435 * @param size Number of bytes to transmit.
436 * @param cb Function to call to get the message.
437 * @param cb_cls Closure for @a cb.
438 * @return NULL on error (i.e. request already pending).
440 struct GNUNET_MULTICAST_MulticastRequest *
441 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
443 GNUNET_CONNECTION_TransmitReadyNotify cb,
448 * Cancel request for message transmission to multicast group.
450 * @param mr Request to cancel.
453 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_MulticastRequest *mr);
457 * End a multicast group.
459 * @param origin multicast group to terminate
462 GNUNET_MULTICAST_origin_end (struct GNUNET_MULTICAST_Origin *origin);
466 * Join a multicast group.
468 * The entity joining is always the local peer. Further information about the
469 * candidate can be provided in the @a join_req message. If the join fails, the
470 * @a message_cb is invoked with a (failure) response and then with NULL. If
471 * the join succeeds, outstanding (state) messages and ongoing multicast
472 * messages will be given to the @a message_cb until the member decides to leave
473 * the group. The @a test_cb and @a replay_cb functions may be called at
474 * anytime by the multicast service to support relaying messages to other
475 * members of the group.
477 * @param cfg Configuration to use.
478 * @param cls Closure for callbacks.
479 * @param pub_key ECC key that identifies the group.
480 * @param max_known_message_id Largest known message ID to the replay service;
481 * all messages with IDs larger than this ID will be replayed if
482 * possible (lower IDs will be considered known and thus only
483 * be replayed upon explicit request).
484 * @param max_known_state_message_id Largest known message ID with a non-zero
485 * value for the @e state_delta; state messages with
486 * larger IDs than this value will be replayed with high priority
487 * (lower IDs will be considered known and thus only
488 * be replayed upon explicit request).
489 * @param replay_cb Function that can be called to replay messages
490 * this peer already knows from this group; NULL if this
491 * client is unable to support replay.
492 * @param test_cb Function multicast can use to test group membership.
493 * @param message_cb Function to be called for all messages we
494 * receive from the group, excluding those our @a replay_cb
496 * @param join_req Application-dependent join message to be passed to origin
497 * (might, for example, contain a user
498 * bind user identity/pseudonym to peer identity, application-level
499 * message to origin, etc.).
500 * @return Handle for the member, NULL on error.
502 struct GNUNET_MULTICAST_Member *
503 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
505 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
506 uint64_t max_known_message_id,
507 uint64_t max_known_state_message_id,
508 GNUNET_MULTICAST_ReplayCallback replay_cb,
509 GNUNET_MULITCAST_MembershipTestCallback test_cb,
510 GNUNET_MULTICAST_MulticastMessageCallback message_cb,
511 const struct GNUNET_MessageHeader *join_req);
515 * Handle for a replay request.
517 struct GNUNET_MULTICAST_ReplayRequest;
521 * Request a message to be replayed.
523 * Useful if messages below the @e max_known_*_id's given when joining are
524 * needed and not known to the client.
526 * @param member Membership handle.
527 * @param message_id ID of a message that this client would like to see replayed.
528 * @param message_cb Function to be called for the replayed message.
529 * @param message_cb_cls Closure for @a message_cb.
530 * @return Replay request handle, NULL on error.
532 struct GNUNET_MULTICAST_ReplayRequest *
533 GNUNET_MULTICAST_member_request_replay (struct GNUNET_MULTICAST_Member *member,
535 GNUNET_MULTICAST_MulticastMessageCallback message_cb,
536 void *message_cb_cls);
540 * Cancel a replay request.
542 * @param rr Request to cancel.
545 GNUNET_MULTICAST_member_request_replay_cancel (struct GNUNET_MULTICAST_ReplayRequest *rr);
549 * Leave a multicast group.
551 * @param member Membership handle.
554 GNUNET_MULTICAST_member_leave (struct GNUNET_MULTICAST_Member *member);
558 * Handle for a message to be delivered to the origin.
560 struct GNUNET_MULTICAST_ResponseRequest;
564 * Send a message to the origin of the multicast group.
566 * @param member Membership handle.
567 * @param size Number of bytes we want to send to origin.
568 * @param cb Callback to call to get the message.
569 * @param cb_cls Closure for @a cb.
570 * @return Handle to cancel request, NULL on error (i.e. request already pending).
572 struct GNUNET_MULTICAST_ResponseRequest *
573 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
575 GNUNET_CONNECTION_TransmitReadyNotify cb,
580 * Cancel request for message transmission to origin.
582 * @param rr Request to cancel.
585 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_ResponseRequest *rr);
589 #if 0 /* keep Emacsens' auto-indent happy */
596 /* ifndef GNUNET_MULTICAST_SERVICE_H */
598 /* end of gnunet_multicast_service.h */