68d4944dee753e8a738b9f4f21dfe564e779f936
[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 Gabor X Toth
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 enum GNUNET_MULTICAST_MessageFlags
98 {
99   /**
100    * First fragment of a message.
101    */
102   GNUNET_MULTICAST_MESSAGE_FIRST_FRAGMENT = 1 << 0,
103
104   /**
105    * Last fragment of a message.
106    */
107   GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT = 1 << 1,
108
109   /** 
110    * OR'ed flags if message is not fragmented.
111    */
112   GNUNET_MULTICAST_MESSAGE_NOT_FRAGMENTED
113     = GNUNET_MULTICAST_MESSAGE_FIRST_FRAGMENT
114     | GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT
115 };
116
117
118 GNUNET_NETWORK_STRUCT_BEGIN
119
120 /** 
121  * Header of a multicast message fragment.
122  *
123  * This format is public as the replay mechanism must replay message fragments using the
124  * same format.  This is needed as we want to integrity-check message fragments within
125  * the multicast layer to avoid multicasting mal-formed messages.
126  */
127 struct GNUNET_MULTICAST_MessageHeader
128 {
129
130   /** 
131    * Header for all multicast message fragments from the origin.
132    */
133   struct GNUNET_MessageHeader header;
134
135   /** 
136    * Number of hops this message fragment has taken since the origin.
137    *
138    * Helpful to determine shortest paths to the origin for responses among
139    * honest peers; updated at each hop and thus not signed and not secure.
140    */
141   uint32_t hop_counter GNUNET_PACKED;
142
143   /** 
144    * ECC signature of the message fragment.
145    *
146    * Signature must match the public key of the multicast group.
147    */
148   struct GNUNET_CRYPTO_EccSignature signature;
149
150   /** 
151    * Signature of the multicast message fragment.
152    */
153   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
154
155   /** 
156    * Number of the message fragment, monotonically increasing.
157    */
158   uint64_t fragment_id GNUNET_PACKED;
159
160   /** 
161    * Number of the message this fragment belongs to.
162    */
163   uint64_t message_id GNUNET_PACKED;
164
165   /** 
166    * Byte offset of this @e fragment of the @e message.
167    *
168    * FIXME: needed?
169    */
170   uint64_t fragment_offset GNUNET_PACKED;
171
172   /** 
173    * Counter that monotonically increases whenever a member parts the group.
174    *
175    * It has significance in case of replay requests: when a member has missed
176    * messages and gets a replay request: in this case if the @a group_generation
177    * is still the same before and after the missed messages, it means that no
178    * @e join or @e part operations happened during the missed messages.
179    */
180   uint64_t group_generation GNUNET_PACKED;
181
182   /** 
183    * Difference between the current @a fragment_id and the @a fragment_id of the
184    * preceeding non-transient message.
185    * 
186    * Zero for transient messages, @c UINT64_MAX for the first message, or any
187    * other message creating a full state reset by the origin.  By subtracting
188    * @a state_delta from @a fragment_id, it is possible to calculate the message
189    * ID of the preceeding non-transient message and thus quickly traverse all
190    * state changes up to the last full state reset by the origin.  This is
191    * useful as it allows joining clients to quickly reassemble the state while
192    * skipping over transient messages (and doing so without having to trust
193    * intermediaries to do it right, as the indices in the chain are signed).  If
194    * the state chain is getting too long, the origin can choose to originate a
195    * state message with a state_delta of UINT64_MAX, thereby starting a new
196    * chain.  The origin will then have to re-create the full state with state
197    * update messages following the state reset message.
198    *
199    * Open question: needed in multicast, or just have this in PSYC; still might
200    * be useful for selective fetching of messages.  Still, that again should
201    * that not be done by PSYC?
202    */
203   uint64_t state_delta GNUNET_PACKED;
204
205   /**
206    * Flags for this message fragment.
207    */
208   enum GNUNET_MULTICAST_MessageFlags flags GNUNET_PACKED;
209
210   /** 
211    * Header for the message body.
212    *
213    * Three message types are specifically understood by multicast, namely "peer
214    * join", "peer part", and "group terminated".  Multicast will use those
215    * messages to update its list of candidates for content distribution.  All
216    * other message types are application-specific.
217    */
218   struct GNUNET_MessageHeader body;
219
220   /* Followed by message body. */
221 };
222
223
224 /** 
225  * Header of a request from a member to the origin.
226  *
227  * FIXME: this is going to be internal.
228  */
229 struct GNUNET_MULTICAST_RequestHeader
230 {
231   /** 
232    * Header for all requests from a member to the origin.
233    */
234   struct GNUNET_MessageHeader header;
235
236   /**
237    * Public key of the group.
238    */
239   struct GNUNET_CRYPTO_ECCPublicKey pub_key;
240
241   /**
242    * Flags for this request.
243    */
244   enum GNUNET_MULTICAST_MessageFlags flags GNUNET_PACKED;
245
246   /** 
247    * Header for the request body.
248    *
249    * Two request types are specifically understood by multicast, namely "peer
250    * join", "peer part".  Multicast will use those messages to update its list
251    * of candidates for content distribution.  All other message types are
252    * application-specific.
253    */
254   struct GNUNET_MessageHeader body;
255
256   /* Followed by request body. */
257 };
258
259 GNUNET_NETWORK_STRUCT_END
260
261
262 /** 
263  * Handle that identifies a join request.
264  *
265  * Used to match calls to #GNUNET_MULTICAST_JoinCallback to the
266  * corresponding calls to GNUNET_MULTICAST_join_decision().
267  */
268 struct GNUNET_MULTICAST_JoinHandle;
269
270
271 /** 
272  * Function to call with the decision made for a join request.
273  *
274  * Must be called once and only once in response to an invocation of the
275  * #GNUNET_MULTICAST_JoinCallback.
276  *
277  * @param jh Join request handle.
278  * @param join_response Message to send in response to the joining peer;
279  *        can also be used to redirect the peer to a different group at the
280  *        application layer; this response is to be transmitted to the
281  *        peer that issued the request even if admission is denied.
282  * @param is_admitted #GNUNET_YES if joining is approved,
283  *        #GNUNET_NO if it is disapproved
284  * @param relay_count Number of relays given.
285  * @param relays Array of suggested peers that might be useful relays to use
286  *        when joining the multicast group (essentially a list of peers that
287  *        are already part of the multicast group and might thus be willing
288  *        to help with routing).  If empty, only this local peer (which must
289  *        be the multicast origin) is a good candidate for building the
290  *        multicast tree.  Note that it is unnecessary to specify our own
291  *        peer identity in this array.
292  */
293 void
294 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
295                                 const struct GNUNET_MessageHeader *join_response,
296                                 int is_admitted,
297                                 unsigned int relay_count,
298                                 const struct GNUNET_PeerIdentity *relays);
299
300
301 /** 
302  * Method called whenever another peer wants to join the multicast group.
303  *
304  * Implementations of this function must call GNUNET_MULTICAST_join_decision()
305  * with the decision.
306  *
307  * @param cls Closure.
308  * @param peer Identity of the peer that wants to join.
309  * @param msg Application-dependent join message from the new user
310  *        (might, for example, contain a user,
311  *        bind user identity/pseudonym to peer identity, application-level
312  *        message to origin, etc.).
313  * @param jh Join handle to pass to GNUNET_MULTICAST_join_decison().
314  */
315 typedef void (*GNUNET_MULTICAST_JoinCallback)(void *cls,
316                                               const struct GNUNET_PeerIdentity *peer,
317                                               const struct GNUNET_MessageHeader *msg,
318                                               struct GNUNET_MULTICAST_JoinHandle *jh);
319
320
321 /** 
322  * Handle to pass back for the answer of a membership test.
323  */
324 struct GNUNET_MULTICAST_MembershipTestHandle;
325
326
327 /** 
328  * Call informing multicast about the decision taken for membership test.
329  *
330  * @param mth Handle that was given for the query.
331  * @param decision #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
332  *         #GNUNET_SYSERR if we cannot answer the membership test.
333  */
334 void
335 GNUNET_MULTICAST_membership_test_answer (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
336                                          int decision);
337
338
339 /** 
340  * Method called to test if a member was in the group at a particular time.
341  *
342  * It is called when a replay request is received to determine if the requested
343  * message can be replayed.
344  *
345  * @param cls Closure.
346  * @param peer Identity of the peer that we want to test.
347  * @param fragment_id Message fragment ID for which we want to do the test.
348  * @param mth Handle to give to GNUNET_MULTICAST_membership_test_answer().
349  */
350 typedef void (*GNUNET_MULTICAST_MembershipTestCallback)(void *cls,
351                                                         const struct GNUNET_PeerIdentity *peer,
352                                                         uint64_t fragment_id,
353                                                         struct GNUNET_MULTICAST_MembershipTestHandle *mth);
354
355
356 /** 
357  * Function called whenever a group member has transmitted a request
358  * to the origin (other than joining or leaving).
359  *
360  * @param cls Closure (set from GNUNET_MULTICAST_origin_start).
361  * @param sender Identity of the sender.
362  * @param req Request to the origin.
363  * @param flags Flags for the request.
364  */
365 typedef void (*GNUNET_MULTICAST_RequestCallback) (void *cls,
366                                                   const struct GNUNET_PeerIdentity *sender,
367                                                   const struct GNUNET_MessageHeader *req,
368                                                   enum GNUNET_MULTICAST_MessageFlags flags);
369
370
371 /** 
372  * Function called whenever a group member is receiving a message fragment from
373  * the origin.
374  *
375  * If admission to the group is denied, this function is called once with the
376  * response of the @e origin (as given to GNUNET_MULTICAST_join_decision()) and
377  * then a second time with NULL to indicate that the connection failed for good.
378  *
379  * @param cls Closure (set from GNUNET_MULTICAST_member_join())
380  * @param msg Message from the origin, NULL if the origin shut down
381  *        (or we were kicked out, and we should thus call
382  *        GNUNET_MULTICAST_member_part() next)
383  */
384 typedef void (*GNUNET_MULTICAST_MessageCallback) (void *cls,
385                                                   const struct GNUNET_MULTICAST_MessageHeader *msg);
386
387
388 /** 
389  * Opaque handle to a replay request from the multicast service.
390  */
391 struct GNUNET_MULTICAST_ReplayHandle;
392
393
394 /** 
395  * Functions with this signature are called whenever the multicast service needs
396  * a message to be replayed.
397  *
398  * Implementations of this function MUST call GNUNET_MULTICAST_replay() ONCE
399  * (with a message or an error); however, if the origin is destroyed or the
400  * group is left, the replay handle must no longer be used.
401  *
402  * @param cls Closure (set from GNUNET_MULTICAST_origin_start()
403  *            or GNUNET_MULTICAST_member_join()).
404  * @param fragment_id Which message fragment should be replayed.
405  * @param rh Handle to pass to message transmit function.
406  */
407 typedef void (*GNUNET_MULTICAST_ReplayCallback) (void *cls,
408                                                  uint64_t fragment_id,
409                                                  struct GNUNET_MULTICAST_ReplayHandle *rh);
410
411
412 /** 
413  * Possible error codes during replay.
414  */
415 enum GNUNET_MULTICAST_ReplayErrorCode
416 {
417
418   /** 
419    * Everything is fine.
420    */
421   GNUNET_MULTICAST_REC_OK = 0,
422
423   /** 
424    * Message fragment has been discarded (likely transient message that was too old).
425    */
426   GNUNET_MULTICAST_REC_TRANSIENT_LOST = 1,
427
428   /** 
429    * Fragment ID counter was larger than the highest counter this
430    * replay function has ever encountered; thus it is likely the
431    * origin never sent it and we're at the HEAD of the multicast
432    * stream as far as this node is concerned.
433    */
434   GNUNET_MULTICAST_REC_PAST_HEAD = 2,
435
436   /** 
437    * Internal error (i.e. database error).  Try some other peer.
438    */
439   GNUNET_MULTICAST_REC_INTERNAL_ERROR = 3
440
441 };
442
443
444 /** 
445  * Replay a message from the multicast group.
446  *
447  * @param rh Replay handle identifying which replay operation was requested.
448  * @param msg Replayed message fragment, NULL if unknown/error.
449  * @param ec Error code.
450  */
451 void
452 GNUNET_MULTICAST_replay (struct GNUNET_MULTICAST_ReplayHandle *rh,
453                          const struct GNUNET_MULTICAST_MessageHeader *msg,
454                          enum GNUNET_MULTICAST_ReplayErrorCode ec);
455
456
457 /** 
458  * Start a multicast group.
459  *
460  * Will advertise the origin in the P2P overlay network under the respective
461  * public key so that other peer can find this peer to join it.  Peers that
462  * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
463  * either an existing group member (if the @a join_policy is permissive) or to
464  * the origin.  If the joining is approved, the member is cleared for @e replay
465  * and will begin to receive messages transmitted to the group.  If joining is
466  * disapproved, the failed candidate will be given a response.  Members in the
467  * group can send messages to the origin (one at a time).
468  *
469  * @param cfg Configuration to use.
470  * @param cls Closure for the various callbacks that follow.
471  * @param priv_key ECC key that will be used to sign messages for this
472  *                 multicast session; public key is used to identify the
473  *                 multicast group; FIXME: we'll likely want to use
474  *                 NOT the p521 curve here, but a cheaper one in the future.
475  * @param join_policy What is the membership policy of the group?
476  * @param replay_cb Function that can be called to replay a message.
477  * @param test_cb Function multicast can use to test group membership.
478  * @param join_cb Function called to approve / disapprove joining of a peer.
479  * @param request_cb Function called with messages from group members.
480  * @return Handle for the origin, NULL on error.
481  */
482 struct GNUNET_MULTICAST_Origin *
483 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
484                                void *cls,
485                                const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
486                                enum GNUNET_MULTICAST_JoinPolicy join_policy,
487                                GNUNET_MULITCAST_ReplayCallback replay_cb,
488                                GNUNET_MULITCAST_MembershipTestCallback test_cb,
489                                GNUNET_MULTICAST_JoinCallback join_cb,
490                                GNUNET_MULTICAST_RequestCallback request_cb);
491
492
493 /** 
494  * Handle for a request to send a message to all multicast group members
495  * (from the origin).
496  */
497 struct GNUNET_MULTICAST_OriginMessageHandle;
498
499
500 /** 
501  * Send a message to the multicast group.
502  *
503  * @param origin Handle to the multicast group.
504  * @param size Number of bytes to transmit.
505  * @param cb Function to call to get the message.
506  * @param cb_cls Closure for @a cb.
507  * @return NULL on error (i.e. request already pending).
508  */
509 struct GNUNET_MULTICAST_OriginMessageHandle *
510 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
511                                 size_t size,
512                                 GNUNET_CONNECTION_TransmitReadyNotify cb,
513                                 void *cb_cls);
514
515
516 /** 
517  * Cancel request for message transmission to multicast group.
518  *
519  * @param mh Request to cancel.
520  */
521 void
522 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginMessageHandle *mh);
523
524
525 /** 
526  * Stop a multicast group.
527  *
528  * @param origin Multicast group to stop.
529  */
530 void
531 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *origin);
532
533
534 /** 
535  * Join a multicast group.
536  *
537  * The entity joining is always the local peer.  Further information about the
538  * candidate can be provided in the @a join_req message.  If the join fails, the
539  * @a message_cb is invoked with a (failure) response and then with NULL.  If
540  * the join succeeds, outstanding (state) messages and ongoing multicast
541  * messages will be given to the @a message_cb until the member decides to part
542  * the group.  The @a test_cb and @a replay_cb functions may be called at
543  * anytime by the multicast service to support relaying messages to other
544  * members of the group.
545  *
546  * @param cfg Configuration to use.
547  * @param pub_key ECC key that identifies the group.
548  * @param origin Peer identity of the origin.
549  * @param max_known_fragment_id Largest known message fragment ID to the replay
550  *        service; all messages with IDs larger than this ID will be replayed if
551  *        possible (lower IDs will be considered known and thus only
552  *        be replayed upon explicit request).
553  * @param max_known_state_fragment_id Largest known message fragment ID with a
554  *        non-zero value for the @e state_delta; state messages with
555  *        larger IDs than this value will be replayed with high priority
556  *        (lower IDs will be considered known and thus only
557  *        be replayed upon explicit request).
558  * @param replay_cb Function that can be called to replay messages
559  *        this peer already knows from this group; NULL if this
560  *        client is unable to support replay.
561  * @param test_cb Function multicast can use to test group membership.
562  * @param message_cb Function to be called for all message fragments we
563  *        receive from the group, excluding those our @a replay_cb
564  *        already has.
565  * @param cls Closure for callbacks.
566  * @param join_req Application-dependent join message to be passed to origin
567  *        (might, for example, contain a user
568  *        bind user identity/pseudonym to peer identity, application-level
569  *        message to origin, etc.).
570  * @return Handle for the member, NULL on error.
571  */
572 struct GNUNET_MULTICAST_Member *
573 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
574                               const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
575                               const struct GNUNET_PeerIdentity *origin,
576                               uint64_t max_known_fragment_id,
577                               uint64_t max_known_state_fragment_id,
578                               GNUNET_MULTICAST_ReplayCallback replay_cb,
579                               GNUNET_MULITCAST_MembershipTestCallback test_cb,
580                               GNUNET_MULTICAST_MessageCallback message_cb,
581                               void *cls,
582                               const struct GNUNET_MessageHeader *join_req);
583
584
585 /** 
586  * Handle for a replay request.
587  */
588 struct GNUNET_MULTICAST_MemberReplayHandle;
589
590
591 /** 
592  * Request a message to be replayed.
593  *
594  * Useful if messages below the @e max_known_*_id's given when joining are
595  * needed and not known to the client.
596  *
597  * @param member Membership handle.
598  * @param fragment_id ID of a message fragment that this client would like to see replayed.
599  * @param message_cb Function to be called for the replayed message.
600  * @param message_cb_cls Closure for @a message_cb.
601  * @return Replay request handle, NULL on error.
602  */
603 struct GNUNET_MULTICAST_MemberReplayHandle *
604 GNUNET_MULTICAST_member_request_replay (struct GNUNET_MULTICAST_Member *member,
605                                         uint64_t fragment_id,
606                                         GNUNET_MULTICAST_MessageCallback message_cb,
607                                         void *message_cb_cls);
608
609
610 /** 
611  * Cancel a replay request.
612  *
613  * @param rh Request to cancel.
614  */
615 void
616 GNUNET_MULTICAST_member_request_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh);
617
618
619 /** 
620  * Part a multicast group.
621  *
622  * Disconnects from all group members and invalidates the @a member handle.
623  *
624  * An application-dependent part message can be transmitted beforehand using
625  * GNUNET_MULTICAST_member_to_origin())
626  *
627  * @param member Membership handle.
628  */
629 void
630 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member);
631
632
633 /** 
634  * Handle for a message to be delivered from a member to the origin.
635  */
636 struct GNUNET_MULTICAST_MemberRequestHandle;
637
638
639 /** 
640  * Send a message to the origin of the multicast group.
641  * 
642  * @param member Membership handle.
643  * @param size Number of bytes we want to send to origin.
644  * @param cb Callback to call to get the message.
645  * @param cb_cls Closure for @a cb.
646  * @return Handle to cancel request, NULL on error (i.e. request already pending).
647  */
648 struct GNUNET_MULTICAST_MemberRequestHandle *
649 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
650                                    size_t size,
651                                    GNUNET_CONNECTION_TransmitReadyNotify cb,
652                                    void *cb_cls);
653
654
655 /** 
656  * Cancel request for message transmission to origin.
657  *
658  * @param rh Request to cancel.
659  */
660 void
661 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberRequestHandle *rh);
662
663
664
665 #if 0                           /* keep Emacsens' auto-indent happy */
666 {
667 #endif
668 #ifdef __cplusplus
669 }
670 #endif
671
672 /* ifndef GNUNET_MULTICAST_SERVICE_H */
673 #endif
674 /* end of gnunet_multicast_service.h */