76a6bcf32ae9ea064bf83a36dcc10a114cffbbbe
[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 /** 
250  * Method called whenever another peer wants to part the multicast group.
251  *
252  * A leave request must be always be honoured.
253  *
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().
258  */
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);
263
264
265 /** 
266  * Handle to pass back for the answer of a membership test.
267  */
268 struct GNUNET_MULTICAST_MembershipTestHandle;
269
270
271 /** 
272  * Call informing multicast about the decision taken for membership test.
273  *
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.
277  */
278 void
279 GNUNET_MULTICAST_membership_test_answer (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
280                                          int decision);
281
282
283 /** 
284  * Method called to test if a member was in the group at a particular time.
285  *
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().
290  */
291 typedef void (*GNUNET_MULTICAST_MembershipTestCallback)(void *cls,
292                                                         const struct GNUNET_PeerIdentity *peer,
293                                                         uint64_t message_id,
294                                                         struct GNUNET_MULTICAST_MembershipTestHandle *mth);
295
296
297 /** 
298  * Function called whenever a group member has transmitted a message
299  * to the origin (other than joining or leaving).
300  *
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.
305  */
306 typedef void (*GNUNET_MULTICAST_RequestCallback) (void *cls,
307                                                   const struct GNUNET_PeerIdentity *sender,
308                                                   uint64_t request_id,
309                                                   const struct GNUNET_MessageHeader *msg);
310
311
312 /** 
313  * Function called whenever a group member is receiving a message from
314  * the origin.
315  *
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.
319  *
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)
326  */
327 typedef void (*GNUNET_MULTICAST_MulticastMessageCallback) (void *cls,
328                                                            uint64_t message_id,
329                                                            const struct GNUNET_MULTICAST_MessageHeader *msg);
330
331
332 /** 
333  * Opaque handle to a replay request from the multicast service.
334  */
335 struct GNUNET_MULTICAST_ReplayHandle;
336
337
338 /** 
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.
344  *
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.
349  */
350 typedef void (*GNUNET_MULTICAST_ReplayCallback) (void *cls,
351                                                  uint64_t message_id,
352                                                  struct GNUNET_MULTICAST_ReplayHandle *rh);
353
354
355 /** 
356  * Possible error codes during replay.
357  */
358 enum GNUNET_MULTICAST_ReplayErrorCode
359 {
360
361   /** 
362    * Everything is fine.
363    */ 
364   GNUNET_MULTICAST_REC_OK = 0,
365
366   /** 
367    * Message has been discarded (likely transient message that was too old).
368    */ 
369   GNUNET_MULTICAST_REC_TRANSIENT_LOST = 1,
370
371   /** 
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.
376    */ 
377   GNUNET_MULTICAST_REC_PAST_HEAD = 2,
378
379   /** 
380    * Internal error (i.e. database error).  Try some other peer.
381    */ 
382   GNUNET_MULTICAST_REC_INTERNAL_ERROR = 3
383
384 };
385
386
387 /** 
388  * Replay a message from the multicast group.
389  *
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.
393  */
394 void
395 GNUNET_MULTICAST_replay (struct GNUNET_MULTICAST_ReplayHandle *rh,
396                          const struct GNUNET_MULTICAST_MessageHeader *msg,
397                          enum GNUNET_MULTICAST_ReplayErrorCode ec);
398
399
400 /** 
401  * Handle to pass back for the answer of a ping.
402  */
403 struct GNUNET_MULTICAST_PingHandle;
404
405
406 /** 
407  * A response to a @e ping.
408  *
409  * @param rh Handle that was given for the ping.
410  * @param message_id Latest message ID seen by this peer for this group.
411  *
412  * @deprecated needed???
413  */
414 void
415 GNUNET_MULTICAST_pong (struct GNUNET_MULTICAST_ReplayHandle *rh,
416                        uint64_t message_id);
417
418
419 /** 
420  * Method called whenever a @e ping is received from another member.
421  *
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().
428  *
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().
433  *
434  * @deprecated needed???
435  */
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);
440
441
442 /** 
443  * Start a multicast group.
444  *
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).
453  *
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.
468  */
469 struct GNUNET_MULTICAST_Origin *
470 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 
471                                void *cls,
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);
480
481
482 /** 
483  * Handle for a request to send a message to all multicast group members
484  * (from the origin).
485  */
486 struct GNUNET_MULTICAST_MulticastRequest;
487
488
489 /** 
490  * Send a message to the multicast group.
491  *
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).
497  */
498 struct GNUNET_MULTICAST_MulticastRequest *
499 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
500                                 size_t size,
501                                 GNUNET_CONNECTION_TransmitReadyNotify cb,
502                                 void *cb_cls);
503
504
505 /** 
506  * Cancel request for message transmission to multicast group.
507  *
508  * @param mr Request to cancel.
509  */
510 void
511 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_MulticastRequest *mr);
512
513
514 /** 
515  * End a multicast group.
516  *
517  * @param origin multicast group to terminate
518  */
519 void
520 GNUNET_MULTICAST_origin_end (struct GNUNET_MULTICAST_Origin *origin);
521
522
523 /** 
524  * Join a multicast group.
525  *
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.
534  *
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
553  *        already has.
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.
559  */
560 struct GNUNET_MULTICAST_Member *
561 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg, 
562                               void *cls,
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);
570
571
572 /** 
573  * Handle for a replay request.
574  */
575 struct GNUNET_MULTICAST_ReplayRequest;
576
577
578 /** 
579  * Request a message to be replayed.
580  *
581  * Useful if messages below the @e max_known_*_id's given when joining are
582  * needed and not known to the client.
583  *
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.
589  */
590 struct GNUNET_MULTICAST_ReplayRequest *
591 GNUNET_MULTICAST_member_request_replay (struct GNUNET_MULTICAST_Member *member,
592                                         uint64_t message_id,
593                                         GNUNET_MULTICAST_MulticastMessageCallback message_cb,
594                                         void *message_cb_cls);
595
596
597 /** 
598  * Cancel a replay request.
599  *
600  * @param rr Request to cancel.
601  */
602 void
603 GNUNET_MULTICAST_member_request_replay_cancel (struct GNUNET_MULTICAST_ReplayRequest *rr);
604
605
606 /** 
607  * Part a multicast group.
608  *
609  * @param member Membership handle.
610  */
611 void
612 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member);
613
614
615 /** 
616  * Handle for a message to be delivered to the origin.
617  */
618 struct GNUNET_MULTICAST_ResponseRequest;
619
620
621 /** 
622  * Send a message to the origin of the multicast group.  
623  * 
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).
629  */
630 struct GNUNET_MULTICAST_ResponseRequest *
631 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
632                                    size_t size,
633                                    GNUNET_CONNECTION_TransmitReadyNotify cb,
634                                    void *cb_cls);
635
636
637 /** 
638  * Cancel request for message transmission to origin.
639  *
640  * @param rr Request to cancel.
641  */
642 void
643 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_ResponseRequest *rr);
644
645
646
647 #if 0                           /* keep Emacsens' auto-indent happy */
648 {
649 #endif
650 #ifdef __cplusplus
651 }
652 #endif
653
654 /* ifndef GNUNET_MULTICAST_SERVICE_H */
655 #endif
656 /* end of gnunet_multicast_service.h */