multicast: ping/pong docs
[oweals/gnunet.git] / src / include / gnunet_multicast_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /** 
22  * @file include/gnunet_multicast_service.h
23  * @brief multicast service; establish tunnels to distant peers
24  * @author Christian Grothoff
25  * @author tg(x)
26  */
27
28 #ifndef GNUNET_MULTICAST_SERVICE_H
29 #define GNUNET_MULTICAST_SERVICE_H
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 #include "gnunet_util_lib.h"
40 #include "gnunet_transport_service.h"
41
42 /** 
43  * Version number of GNUnet-multicast API.
44  */
45 #define GNUNET_MULTICAST_VERSION 0x00000000
46
47
48 /** 
49  * Opaque handle for a multicast group member.
50  */
51 struct GNUNET_MULTICAST_Member;
52
53 /** 
54  * Handle for the origin of a multicast group.
55  */
56 struct GNUNET_MULTICAST_Origin;
57
58 /** 
59  * Group membership policies.
60  */
61 enum GNUNET_MULTICAST_JoinPolicy
62 {
63   /** 
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.
67    */
68   GNUNET_MULTICAST_JP_ANONYMOUS = 0,
69
70   /** 
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.
74    */
75   GNUNET_MULTICAST_JP_PRIVATE = 1,
76
77 #if IDEAS_FOR_FUTURE
78   /** 
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.
85    */
86   GNUNET_MULTICAST_JP_OPEN = 2,
87
88   /**
89    * Origin must approve membership to the group, but past messages can be
90    * freely distributed to members.
91    */
92   GNUNET_MULTICAST_JP_CLOSED = 3,
93 #endif
94
95 };
96
97
98 GNUNET_NETWORK_STRUCT_BEGIN
99
100 /** 
101  * Header of a multicast message.
102  *
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.
106  */
107 struct GNUNET_MULTICAST_MessageHeader
108 {
109
110   /** 
111    * Header for all multicast messages from the origin.
112    */
113   struct GNUNET_MessageHeader header;
114
115   /** 
116    * Number of hops this message has taken since the origin.
117    *
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.
120    */
121   uint32_t hop_counter GNUNET_PACKED;
122
123   /** 
124    * ECC signature of the message.
125    *
126    * Signature must match the public key of the multicast group.
127    */
128   struct GNUNET_CRYPTO_EccSignature signature;
129
130   /** 
131    * Signature of the multicast message.
132    */
133   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
134
135   /** 
136    * Number of the message, monotonically increasing.
137    */
138   uint64_t message_id GNUNET_PACKED;
139
140   /** 
141    * Counter that monotonically increases whenever a member leaves the group.
142    *
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.
147    */
148   uint64_t group_generation GNUNET_PACKED;
149
150   /** 
151    * Difference between the current @a message_id and the @a message_id of the
152    * preceeding non-transient message.
153    * 
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.
166    *
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?
170    */
171   uint64_t state_delta GNUNET_PACKED;
172
173   /** 
174    * Header for the message body.
175    *
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.
180    */
181   struct GNUNET_MessageHeader body;
182
183   /* followed by message body */
184 };
185
186 GNUNET_NETWORK_STRUCT_END
187
188
189 /** 
190  * Handle that identifies a join request.
191  *
192  * Used to match calls to #GNUNET_MULTICAST_JoinCallback to the
193  * corresponding calls to GNUNET_MULTICAST_join_decision().
194  */
195 struct GNUNET_MULTICAST_JoinHandle;
196
197
198 /** 
199  * Function to call with the decision made for a membership change request.
200  *
201  * Must be called once and only once in response to an invocation of the
202  * #GNUNET_MULTICAST_JoinCallback.
203  *
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.
220  */
221 void
222 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
223                                 const struct GNUNET_MessageHeader *join_response,
224                                 int is_admitted,
225                                 unsigned int relay_count,
226                                 const struct GNUNET_PeerIdentity *relays);
227
228
229 /** 
230  * Method called whenever another peer wants to join the multicast group.
231  *
232  * Implementations of this function must call GNUNET_MULTICAST_join_decision()
233  * with the decision.
234  *
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().
242  */
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);
247
248 /** 
249  * Method called whenever another peer wants to leave the multicast group.
250  *
251  * A leave request must be always be honoured.
252  *
253  * @param cls Closure.
254  * @param peer Identity of the peer that wants to leave.
255  * @param msg Application-dependent leave message from the leaving user.
256  * @param jh Join handle to pass to GNUNET_MULTICAST_join_decison().
257  */
258 typedef void (*GNUNET_MULTICAST_LeaveCallback)(void *cls,
259                                                const struct GNUNET_PeerIdentity *peer,
260                                                const struct GNUNET_MessageHeader *msg,
261                                                struct GNUNET_MULTICAST_JoinHandle *jh);
262
263
264 /** 
265  * Handle to pass back for the answer of a membership test.
266  */
267 struct GNUNET_MULTICAST_MembershipTestHandle;
268
269
270 /** 
271  * Call informing multicast about the decision taken for membership test.
272  *
273  * @param mth Handle that was given for the query.
274  * @param decision #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
275  *         #GNUNET_SYSERR if we cannot answer the membership test.
276  */
277 void
278 GNUNET_MULTICAST_membership_test_answer (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
279                                          int decision);
280
281
282 /** 
283  * Method called to test if a member was in the group at a particular time.
284  *
285  * @param cls Closure.
286  * @param peer Identity of the peer that we want to test.
287  * @param message_id Message ID for which we want to do the test.
288  * @param mth Handle to give to GNUNET_MULTICAST_membership_test_answer().
289  */
290 typedef void (*GNUNET_MULTICAST_MembershipTestCallback)(void *cls,
291                                                         const struct GNUNET_PeerIdentity *peer,
292                                                         uint64_t message_id,
293                                                         struct GNUNET_MULTICAST_MembershipTestHandle *mth);
294
295
296 /** 
297  * Function called whenever a group member has transmitted a message
298  * to the origin (other than joining or leaving).
299  *
300  * @param cls Closure (set from GNUNET_MULTICAST_origin_start).
301  * @param sender Identity of the sender.
302  * @param request_id Unique counter for the request from this sender to this origin.
303  * @param msg Message to the origin.
304  */
305 typedef void (*GNUNET_MULTICAST_RequestCallback) (void *cls,
306                                                   const struct GNUNET_PeerIdentity *sender,
307                                                   uint64_t request_id,
308                                                   const struct GNUNET_MessageHeader *msg);
309
310
311 /** 
312  * Function called whenever a group member is receiving a message from
313  * the origin.
314  *
315  * If admission to the group is denied, this function is called once with the
316  * response of the @e origin (as given to GNUNET_MULTICAST_join_decision()) and
317  * then a second time with NULL to indicate that the connection failed for good.
318  *
319  * @param cls Closure (set from GNUNET_MULTICAST_member_join())
320  * @param message_id Unique number of the message, 0 for response to join request,
321  *        normal message IDs in either direction start at 1.
322  * @param msg Message from the origin, NULL if the origin shut down
323  *        (or we were kicked out, and we should thus call
324  *        GNUNET_MULTICAST_member_leave() next)
325  */
326 typedef void (*GNUNET_MULTICAST_MulticastMessageCallback) (void *cls,
327                                                            uint64_t message_id,
328                                                            const struct GNUNET_MULTICAST_MessageHeader *msg);
329
330
331 /** 
332  * Opaque handle to a replay request from the multicast service.
333  */
334 struct GNUNET_MULTICAST_ReplayHandle;
335
336
337 /** 
338  * Functions with this signature are called whenever the multicast
339  * service needs a message to be replayed.  Implementations of this
340  * function MUST call GNUNET_MULTICAST_replay() ONCE (with a message
341  * or an error); however, if the origin is destroyed or the group is
342  * left, the replay handle must no longer be used.
343  *
344  * @param cls Closure (set from GNUNET_MULTICAST_origin_start()
345  *            or GNUNET_MULTICAST_member_join()).
346  * @param message_id Which message should be replayed.
347  * @param rh Handle to pass to message transmit function.
348  */
349 typedef void (*GNUNET_MULTICAST_ReplayCallback) (void *cls,
350                                                  uint64_t message_id,
351                                                  struct GNUNET_MULTICAST_ReplayHandle *rh);
352
353
354 /** 
355  * Possible error codes during replay.
356  */
357 enum GNUNET_MULTICAST_ReplayErrorCode
358 {
359
360   /** 
361    * Everything is fine.
362    */ 
363   GNUNET_MULTICAST_REC_OK = 0,
364
365   /** 
366    * Message has been discarded (likely transient message that was too old).
367    */ 
368   GNUNET_MULTICAST_REC_TRANSIENT_LOST = 1,
369
370   /** 
371    * Message ID counter was larger than the highest counter this
372    * replay function has ever encountered; thus it is likely the
373    * origin never sent it and we're at the HEAD of the multicast
374    * stream as far as this node is concerned.
375    */ 
376   GNUNET_MULTICAST_REC_PAST_HEAD = 2,
377
378   /** 
379    * Internal error (i.e. database error).  Try some other peer.
380    */ 
381   GNUNET_MULTICAST_REC_INTERNAL_ERROR = 3
382
383 };
384
385
386 /** 
387  * Replay a message from the multicast group.
388  *
389  * @param rh Replay handle identifying which replay operation was requested.
390  * @param msg Replayed message, NULL if unknown/error.
391  * @param ec Error code.
392  */
393 void
394 GNUNET_MULTICAST_replay (struct GNUNET_MULTICAST_ReplayHandle *rh,
395                          const struct GNUNET_MULTICAST_MessageHeader *msg,
396                          enum GNUNET_MULTICAST_ReplayErrorCode ec);
397
398
399 /** 
400  * Handle to pass back for the answer of a ping.
401  */
402 struct GNUNET_MULTICAST_PingHandle;
403
404
405 /** 
406  * A response to a @e ping.
407  *
408  * @param ph Handle that was given for the ping.
409  * @param message_id Latest message ID seen by this peer for this group.
410  */
411 void
412 GNUNET_MULTICAST_pong (struct GNUNET_MULTICAST_ReplayHandle *rh,
413                        uint64_t message_id);
414
415
416 /** 
417  * Method called whenever a @e ping is received from another member.
418  *
419  * A @e ping is sent after a period of inactivity to check if the member has not
420  * missed any messages.  A ping contains the latest message ID a member has
421  * seen, and must be answered with GNUNET_MULTICAST_pong() containing the latest
422  * message ID seen by this peer.  If the latest message ID on this peer is higher, the
423  * missing messages must be replayed to the requesting member using
424  * GNUNET_MULTICAST_replay().
425  *
426  * @param cls Closure.
427  * @param peer Identity of the peer who sent the ping.
428  * @param latest_message_id Latest message ID seen by the requesting member.
429  * @param rh Handle to pass back to GNUNET_MULTICAST_pong() or GNUNET_MULTICAST_replay().
430  */
431 typedef void (*GNUNET_MULTICAST_PingCallback)(void *cls,
432                                               const struct GNUNET_PeerIdentity *peer,
433                                               uint64_t latest_messaged_id
434                                               struct GNUNET_MULTICAST_ReplayHandle *rh);
435
436
437 /** 
438  * Start a multicast group.
439  *
440  * Will advertise the origin in the P2P overlay network under the respective
441  * public key so that other peer can find this peer to join it.  Peers that
442  * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
443  * either an existing group member (if the @a join_policy is permissive) or to
444  * the origin.  If the joining is approved, the member is cleared for @e replay
445  * and will begin to receive messages transmitted to the group.  If joining is
446  * disapproved, the failed candidate will be given a response.  Members in the
447  * group can send messages to the origin (one at a time).
448  *
449  * @param cfg Configuration to use.
450  * @param cls Closure for the various callbacks that follow.
451  * @param priv_key ECC key that will be used to sign messages for this
452  *                 multicast session; public key is used to identify the
453  *                 multicast group; FIXME: we'll likely want to use
454  *                 NOT the p521 curve here, but a cheaper one in the future.
455  * @param join_policy What is the membership policy of the group?
456  * @param replay_cb Function that can be called to replay a message.
457  * @param test_cb Function multicast can use to test group membership.
458  * @param ping_cb Function called to answer a ping.
459  * @param join_cb Function called to approve / disapprove joining of a peer.
460  * @param leave_cb Function called when a member wants to leave the group.
461  * @param request_cb Function called with messages from group members.
462  * @return Handle for the origin, NULL on error.
463  */
464 struct GNUNET_MULTICAST_Origin *
465 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 
466                                void *cls,
467                                const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
468                                enum GNUNET_MULTICAST_JoinPolicy join_policy,
469                                GNUNET_MULITCAST_ReplayCallback replay_cb,
470                                GNUNET_MULITCAST_MembershipTestCallback test_cb,
471                                GNUNET_MULITCAST_PingCallback ping_cb,
472                                GNUNET_MULTICAST_JoinCallback join_cb,
473                                GNUNET_MULTICAST_LeaveCallback leave_cb,
474                                GNUNET_MULTICAST_RequestCallback request_cb);
475
476
477 /** 
478  * Handle for a request to send a message to all multicast group members
479  * (from the origin).
480  */
481 struct GNUNET_MULTICAST_MulticastRequest;
482
483
484 /** 
485  * Send a message to the multicast group.
486  *
487  * @param origin Handle to the multicast group.
488  * @param size Number of bytes to transmit.
489  * @param cb Function to call to get the message.
490  * @param cb_cls Closure for @a cb.
491  * @return NULL on error (i.e. request already pending).
492  */
493 struct GNUNET_MULTICAST_MulticastRequest *
494 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
495                                 size_t size,
496                                 GNUNET_CONNECTION_TransmitReadyNotify cb,
497                                 void *cb_cls);
498
499
500 /** 
501  * Cancel request for message transmission to multicast group.
502  *
503  * @param mr Request to cancel.
504  */
505 void
506 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_MulticastRequest *mr);
507
508
509 /** 
510  * End a multicast group.
511  *
512  * @param origin multicast group to terminate
513  */
514 void
515 GNUNET_MULTICAST_origin_end (struct GNUNET_MULTICAST_Origin *origin);
516
517
518 /** 
519  * Join a multicast group.
520  *
521  * The entity joining is always the local peer.  Further information about the
522  * candidate can be provided in the @a join_req message.  If the join fails, the
523  * @a message_cb is invoked with a (failure) response and then with NULL.  If
524  * the join succeeds, outstanding (state) messages and ongoing multicast
525  * messages will be given to the @a message_cb until the member decides to leave
526  * the group.  The @a test_cb and @a replay_cb functions may be called at
527  * anytime by the multicast service to support relaying messages to other
528  * members of the group.
529  *
530  * @param cfg Configuration to use.
531  * @param cls Closure for callbacks.
532  * @param pub_key ECC key that identifies the group.
533  * @param max_known_message_id Largest known message ID to the replay service;
534  *        all messages with IDs larger than this ID will be replayed if
535  *        possible (lower IDs will be considered known and thus only
536  *        be replayed upon explicit request).
537  * @param max_known_state_message_id Largest known message ID with a non-zero
538  *        value for the @e state_delta; state messages with
539  *        larger IDs than this value will be replayed with high priority
540  *        (lower IDs will be considered known and thus only
541  *        be replayed upon explicit request).
542  * @param replay_cb Function that can be called to replay messages
543  *        this peer already knows from this group; NULL if this
544  *        client is unable to support replay.
545  * @param test_cb Function multicast can use to test group membership.
546  * @param message_cb Function to be called for all messages we 
547  *        receive from the group, excluding those our @a replay_cb
548  *        already has.
549  * @param join_req Application-dependent join message to be passed to origin
550  *        (might, for example, contain a user 
551  *        bind user identity/pseudonym to peer identity, application-level
552  *        message to origin, etc.).
553  * @return Handle for the member, NULL on error.
554  */
555 struct GNUNET_MULTICAST_Member *
556 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg, 
557                               void *cls,
558                               const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
559                               uint64_t max_known_message_id,
560                               uint64_t max_known_state_message_id,
561                               GNUNET_MULTICAST_ReplayCallback replay_cb,
562                               GNUNET_MULITCAST_MembershipTestCallback test_cb,
563                               GNUNET_MULTICAST_MulticastMessageCallback message_cb,
564                               const struct GNUNET_MessageHeader *join_req);
565
566
567 /** 
568  * Handle for a replay request.
569  */
570 struct GNUNET_MULTICAST_ReplayRequest;
571
572
573 /** 
574  * Request a message to be replayed.
575  *
576  * Useful if messages below the @e max_known_*_id's given when joining are
577  * needed and not known to the client.
578  *
579  * @param member Membership handle.
580  * @param message_id ID of a message that this client would like to see replayed.
581  * @param message_cb Function to be called for the replayed message.
582  * @param message_cb_cls Closure for @a message_cb.
583  * @return Replay request handle, NULL on error.
584  */
585 struct GNUNET_MULTICAST_ReplayRequest *
586 GNUNET_MULTICAST_member_request_replay (struct GNUNET_MULTICAST_Member *member,
587                                         uint64_t message_id,
588                                         GNUNET_MULTICAST_MulticastMessageCallback message_cb,
589                                         void *message_cb_cls);
590
591
592 /** 
593  * Cancel a replay request.
594  *
595  * @param rr Request to cancel.
596  */
597 void
598 GNUNET_MULTICAST_member_request_replay_cancel (struct GNUNET_MULTICAST_ReplayRequest *rr);
599
600
601 /** 
602  * Leave a multicast group.
603  *
604  * @param member Membership handle.
605  */
606 void
607 GNUNET_MULTICAST_member_leave (struct GNUNET_MULTICAST_Member *member);
608
609
610 /** 
611  * Handle for a message to be delivered to the origin.
612  */
613 struct GNUNET_MULTICAST_ResponseRequest;
614
615
616 /** 
617  * Send a message to the origin of the multicast group.  
618  * 
619  * @param member Membership handle.
620  * @param size Number of bytes we want to send to origin.
621  * @param cb Callback to call to get the message.
622  * @param cb_cls Closure for @a cb.
623  * @return Handle to cancel request, NULL on error (i.e. request already pending).
624  */
625 struct GNUNET_MULTICAST_ResponseRequest *
626 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
627                                    size_t size,
628                                    GNUNET_CONNECTION_TransmitReadyNotify cb,
629                                    void *cb_cls);
630
631
632 /** 
633  * Cancel request for message transmission to origin.
634  *
635  * @param rr Request to cancel.
636  */
637 void
638 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_ResponseRequest *rr);
639
640
641
642 #if 0                           /* keep Emacsens' auto-indent happy */
643 {
644 #endif
645 #ifdef __cplusplus
646 }
647 #endif
648
649 /* ifndef GNUNET_MULTICAST_SERVICE_H */
650 #endif
651 /* end of gnunet_multicast_service.h */