-more datacache integration work
[oweals/gnunet.git] / src / include / gnunet_multicast_service.h
1 /*
2      This file is part of GNUnet.
3      Copyright (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  * Opaque handle for a multicast group member.
49  */
50 struct GNUNET_MULTICAST_Member;
51
52 /**
53  * Handle for the origin of a multicast group.
54  */
55 struct GNUNET_MULTICAST_Origin;
56
57
58 enum GNUNET_MULTICAST_MessageFlags
59 {
60   /**
61    * First fragment of a message.
62    */
63   GNUNET_MULTICAST_MESSAGE_FIRST_FRAGMENT = 1 << 0,
64
65   /**
66    * Last fragment of a message.
67    */
68   GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT = 1 << 1,
69
70   /**
71    * OR'ed flags if message is not fragmented.
72    */
73   GNUNET_MULTICAST_MESSAGE_NOT_FRAGMENTED
74     = GNUNET_MULTICAST_MESSAGE_FIRST_FRAGMENT
75   | GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT,
76
77   /**
78    * Historic message, used only locally when replaying messages from local
79    * storage.
80    */
81   GNUNET_MULTICAST_MESSAGE_HISTORIC = 1 << 30
82
83 };
84
85
86 GNUNET_NETWORK_STRUCT_BEGIN
87
88 /**
89  * Header of a multicast message fragment.
90  *
91  * This format is public as the replay mechanism must replay message fragments using the
92  * same format.  This is needed as we want to integrity-check message fragments within
93  * the multicast layer to avoid multicasting mal-formed messages.
94  */
95 struct GNUNET_MULTICAST_MessageHeader
96 {
97
98   /**
99    * Header for all multicast message fragments from the origin.
100    */
101   struct GNUNET_MessageHeader header;
102
103   /**
104    * Number of hops this message fragment has taken since the origin.
105    *
106    * Helpful to determine shortest paths to the origin among honest peers for
107    * unicast requests from members.  Updated at each hop and thus not signed and
108    * not secure.
109    */
110   uint32_t hop_counter GNUNET_PACKED;
111
112   /**
113    * ECC signature of the message fragment.
114    *
115    * Signature must match the public key of the multicast group.
116    */
117   struct GNUNET_CRYPTO_EddsaSignature signature;
118
119   /**
120    * Purpose for the signature and size of the signed data.
121    */
122   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
123
124   /**
125    * Number of the message fragment, monotonically increasing starting from 1.
126    */
127   uint64_t fragment_id GNUNET_PACKED;
128
129   /**
130    * Byte offset of this @e fragment of the @e message.
131    */
132   uint64_t fragment_offset GNUNET_PACKED;
133
134   /**
135    * Number of the message this fragment belongs to.
136    *
137    * Set in GNUNET_MULTICAST_origin_to_all().
138    */
139   uint64_t message_id GNUNET_PACKED;
140
141   /**
142    * Counter that monotonically increases whenever a member parts the group.
143    *
144    * Set in GNUNET_MULTICAST_origin_to_all().
145    *
146    * It has significance in case of replay requests: when a member has missed
147    * messages and gets a replay request: in this case if the @a group_generation
148    * is still the same before and after the missed messages, it means that no
149    * @e join or @e part operations happened during the missed messages.
150    */
151   uint64_t group_generation GNUNET_PACKED;
152
153   /**
154    * Flags for this message fragment.
155    *
156    * @see enum GNUNET_MULTICAST_MessageFlags
157    */
158   uint32_t flags GNUNET_PACKED;
159
160   /* Followed by message body. */
161 };
162
163
164 /**
165  * Header of a request from a member to the origin.
166  */
167 struct GNUNET_MULTICAST_RequestHeader
168 {
169   /**
170    * Header for all requests from a member to the origin.
171    */
172   struct GNUNET_MessageHeader header;
173
174   /**
175    * Public key of the sending member.
176    */
177   struct GNUNET_CRYPTO_EcdsaPublicKey member_key;
178
179   /**
180    * ECC signature of the request fragment.
181    *
182    * Signature must match the public key of the multicast group.
183    */
184   struct GNUNET_CRYPTO_EcdsaSignature signature;
185
186   /**
187    * Purpose for the signature and size of the signed data.
188    */
189   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
190
191   /**
192    * Number of the request fragment.
193    * Monotonically increasing from 1.
194    */
195   uint64_t fragment_id GNUNET_PACKED;
196
197   /**
198    * Byte offset of this @e fragment of the @e request.
199    */
200   uint64_t fragment_offset GNUNET_PACKED;
201
202   /**
203    * Number of the request this fragment belongs to.
204    *
205    * Set in GNUNET_MULTICAST_origin_to_all().
206    */
207   uint64_t request_id GNUNET_PACKED;
208
209   /**
210    * Flags for this request.
211    */
212   enum GNUNET_MULTICAST_MessageFlags flags GNUNET_PACKED;
213
214   /* Followed by request body. */
215 };
216
217 GNUNET_NETWORK_STRUCT_END
218
219
220 /**
221  * Maximum size of a multicast message fragment.
222  */
223 #define GNUNET_MULTICAST_FRAGMENT_MAX_SIZE 63 * 1024
224
225 #define GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD           \
226   GNUNET_MULTICAST_FRAGMENT_MAX_SIZE                    \
227   - sizeof (struct GNUNET_MULTICAST_MessageHeader)
228
229
230 /**
231  * Handle that identifies a join request.
232  *
233  * Used to match calls to #GNUNET_MULTICAST_JoinRequestCallback to the
234  * corresponding calls to #GNUNET_MULTICAST_join_decision().
235  */
236 struct GNUNET_MULTICAST_JoinHandle;
237
238
239 /**
240  * Function to call with the decision made for a join request.
241  *
242  * Must be called once and only once in response to an invocation of the
243  * #GNUNET_MULTICAST_JoinRequestCallback.
244  *
245  * @param jh Join request handle.
246  * @param is_admitted  #GNUNET_YES    if the join is approved,
247  *                     #GNUNET_NO     if it is disapproved,
248  *                     #GNUNET_SYSERR if we cannot answer the request.
249  * @param relay_count Number of relays given.
250  * @param relays Array of suggested peers that might be useful relays to use
251  *        when joining the multicast group (essentially a list of peers that
252  *        are already part of the multicast group and might thus be willing
253  *        to help with routing).  If empty, only this local peer (which must
254  *        be the multicast origin) is a good candidate for building the
255  *        multicast tree.  Note that it is unnecessary to specify our own
256  *        peer identity in this array.
257  * @param join_resp  Message to send in response to the joining peer;
258  *        can also be used to redirect the peer to a different group at the
259  *        application layer; this response is to be transmitted to the
260  *        peer that issued the request even if admission is denied.
261  */
262 struct GNUNET_MULTICAST_ReplayHandle *
263 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
264                                 int is_admitted,
265                                 uint16_t relay_count,
266                                 const struct GNUNET_PeerIdentity *relays,
267                                 const struct GNUNET_MessageHeader *join_resp);
268
269
270 /**
271  * Method called whenever another peer wants to join the multicast group.
272  *
273  * Implementations of this function must call GNUNET_MULTICAST_join_decision()
274  * with the decision.
275  *
276  * @param cls  Closure.
277  * @param member_key  Public key of the member requesting join.
278  * @param join_msg  Application-dependent join message from the new member.
279  * @param jh  Join handle to pass to GNUNET_MULTICAST_join_decison().
280  */
281 typedef void
282 (*GNUNET_MULTICAST_JoinRequestCallback) (void *cls,
283                                          const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
284                                          const struct GNUNET_MessageHeader *join_msg,
285                                          struct GNUNET_MULTICAST_JoinHandle *jh);
286
287
288 /**
289  * Method called to inform about the decision in response to a join request.
290  *
291  * If @a is_admitted is not #GNUNET_YES, then the multicast service disconnects
292  * the client and the multicast member handle returned by
293  * GNUNET_MULTICAST_member_join() is invalidated.
294  *
295  * @param cls  Closure.
296  * @param is_admitted  #GNUNET_YES or #GNUNET_NO or #GNUNET_SYSERR
297  * @param peer  The peer we are connected to and the join decision is from.
298  * @param relay_count  Number of peers in the @a relays array.
299  * @param relays  Peer identities of members of the group, which serve as relays
300  *        and can be used to join the group at.  If empty, only the origin can
301  *        be used to connect to the group.
302  * @param join_msg  Application-dependent join message from the origin.
303  */
304 typedef void
305 (*GNUNET_MULTICAST_JoinDecisionCallback) (void *cls,
306                                           int is_admitted,
307                                           const struct GNUNET_PeerIdentity *peer,
308                                           uint16_t relay_count,
309                                           const struct GNUNET_PeerIdentity *relays,
310                                           const struct GNUNET_MessageHeader *join_msg);
311
312
313 /**
314  * Handle to pass back for the answer of a membership test.
315  */
316 struct GNUNET_MULTICAST_MembershipTestHandle;
317
318
319 /**
320  * Call informing multicast about the decision taken for a membership test.
321  *
322  * @param mth Handle that was given for the query.
323  * @param result #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
324  *        #GNUNET_SYSERR if we cannot answer the membership test.
325  */
326 void
327 GNUNET_MULTICAST_membership_test_result (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
328                                          int result);
329
330
331 /**
332  * Method called to test if a member was in the group at a particular time.
333  *
334  * It is called when a replay request is received to determine if the requested
335  * message can be replayed.
336  *
337  * @param cls Closure.
338  * @param member_key Identity of the member that we want to test.
339  * @param message_id Message ID for which to perform the test.
340  * @param group_generation Group generation of the message. It has relevance if
341  *        the message consists of multiple fragments with different group
342  *        generations.
343  * @param mth Handle to give to GNUNET_MULTICAST_membership_test_answer().
344  */
345 typedef void
346 (*GNUNET_MULTICAST_MembershipTestCallback) (void *cls,
347                                             const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
348                                             uint64_t message_id,
349                                             uint64_t group_generation,
350                                             struct GNUNET_MULTICAST_MembershipTestHandle *mth);
351
352
353 /**
354  * Function called whenever a group member has transmitted a request
355  * to the origin (other than joining or leaving).
356  *
357  * FIXME: need to distinguish between origin cancelling a message (some fragments
358  * were sent, then the rest 'discarded') and the case where we got disconnected;
359  * right now, both would mean 'msg' is NULL, but they could be quite different...
360  * So the semantics from the receiver side of
361  * GNUNET_MULTICAST_member_to_origin_cancel() are not clear here.   Maybe we
362  * should do something with the flags in this case?
363  *
364  * @param cls Closure (set from GNUNET_MULTICAST_origin_start).
365  * @param sender Identity of the sender.
366  * @param req Request to the origin.
367  * @param flags Flags for the request.
368  */
369 typedef void
370 (*GNUNET_MULTICAST_RequestCallback) (void *cls,
371                                      const struct GNUNET_MULTICAST_RequestHeader *req);
372
373
374 /**
375  * Function called whenever a group member is receiving a message fragment from
376  * the origin.
377  *
378  * If admission to the group is denied, this function is called once with the
379  * response of the @e origin (as given to GNUNET_MULTICAST_join_decision()) and
380  * then a second time with NULL to indicate that the connection failed for good.
381  *
382  * FIXME: need to distinguish between origin cancelling a message (some fragments
383  * were sent, then the rest 'discarded') and the case where we got disconnected;
384  * right now, both would mean 'msg' is NULL, but they could be quite different...
385  * So the semantics from the receiver side of
386  * GNUNET_MULTICAST_origin_to_all_cancel() are not clear here.
387  *
388  * @param cls Closure (set from GNUNET_MULTICAST_member_join())
389  * @param msg Message from the origin, NULL if the origin shut down
390  *        (or we were kicked out, and we should thus call
391  *        GNUNET_MULTICAST_member_part() next)
392  */
393 typedef void
394 (*GNUNET_MULTICAST_MessageCallback) (void *cls,
395                                      const struct GNUNET_MULTICAST_MessageHeader *msg);
396
397
398 /**
399  * Function called with the result of an asynchronous operation.
400  *
401  * @see GNUNET_MULTICAST_replay_fragment()
402  *
403  * @param result Result of the operation.
404  */
405 typedef void
406 (*GNUNET_MULTICAST_ResultCallback) (void *cls,
407                                     int result);
408
409
410 /**
411  * Opaque handle to a replay request from the multicast service.
412  */
413 struct GNUNET_MULTICAST_ReplayHandle;
414
415
416 /**
417  * Functions with this signature are called whenever the multicast service needs
418  * a message fragment to be replayed by fragment_id.
419  *
420  * Implementations of this function MUST call GNUNET_MULTICAST_replay() ONCE
421  * (with a message or an error); however, if the origin is destroyed or the
422  * group is left, the replay handle must no longer be used.
423  *
424  * @param cls Closure (set from GNUNET_MULTICAST_origin_start()
425  *        or GNUNET_MULTICAST_member_join()).
426  * @param member_key The member requesting replay.
427  * @param fragment_id Which message fragment should be replayed.
428  * @param flags Flags for the replay.
429  * @param rh Handle to pass to message transmit function.
430  */
431 typedef void
432 (*GNUNET_MULTICAST_ReplayFragmentCallback) (void *cls,
433                                             const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
434                                             uint64_t fragment_id,
435                                             uint64_t flags,
436                                             struct GNUNET_MULTICAST_ReplayHandle *rh);
437
438 /**
439  * Functions with this signature are called whenever the multicast service needs
440  * a message fragment to be replayed by message_id and fragment_offset.
441  *
442  * Implementations of this function MUST call GNUNET_MULTICAST_replay() ONCE
443  * (with a message or an error); however, if the origin is destroyed or the
444  * group is left, the replay handle must no longer be used.
445  *
446  * @param cls Closure (set from GNUNET_MULTICAST_origin_start()
447  *        or GNUNET_MULTICAST_member_join()).
448  * @param member_key The member requesting replay.
449  * @param message_id Which message should be replayed.
450  * @param fragment_offset Offset of the fragment within of @a message_id to be replayed.
451  * @param flags Flags for the replay.
452  * @param rh Handle to pass to message transmit function.
453  */
454 typedef void
455 (*GNUNET_MULTICAST_ReplayMessageCallback) (void *cls,
456                                            const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
457                                            uint64_t message_id,
458                                            uint64_t fragment_offset,
459                                            uint64_t flags,
460                                            struct GNUNET_MULTICAST_ReplayHandle *rh);
461
462
463 /**
464  * Possible error codes during replay.
465  */
466 enum GNUNET_MULTICAST_ReplayErrorCode
467 {
468
469   /**
470    * Everything is fine.
471    */
472   GNUNET_MULTICAST_REC_OK = 0,
473
474   /**
475    * Message fragment not found in the message store.
476    *
477    * Either discarded if it is too old, or not arrived yet if this member has
478    * missed some messages.
479    */
480   GNUNET_MULTICAST_REC_NOT_FOUND = 1,
481
482   /**
483    * Fragment ID counter was larger than the highest counter this
484    * replay function has ever encountered; thus it is likely the
485    * origin never sent it and we're at the HEAD of the multicast
486    * stream as far as this node is concerned.
487    *
488    * FIXME: needed?
489    */
490   GNUNET_MULTICAST_REC_PAST_HEAD = 2,
491
492   /**
493    * Access is denied to the requested fragment, membership test did not pass.
494    */
495   GNUNET_MULTICAST_REC_ACCESS_DENIED = 3,
496
497   /**
498    * Internal error (i.e. database error).  Try some other peer.
499    */
500   GNUNET_MULTICAST_REC_INTERNAL_ERROR = 4
501
502 };
503
504
505 /**
506  * Replay a message fragment for the multicast group.
507  *
508  * @param rh Replay handle identifying which replay operation was requested.
509  * @param msg Replayed message fragment, NULL if unknown/error.
510  * @param ec Error code.
511  */
512 void
513 GNUNET_MULTICAST_replay_response (struct GNUNET_MULTICAST_ReplayHandle *rh,
514                                   const struct GNUNET_MessageHeader *msg,
515                                   enum GNUNET_MULTICAST_ReplayErrorCode ec);
516
517
518 /**
519  * Indicate the end of the replay session.
520  *
521  * Invalidates the replay handle.
522  *
523  * @param rh Replay session to end.
524  */
525 void
526 GNUNET_MULTICAST_replay_response_end (struct GNUNET_MULTICAST_ReplayHandle *rh);
527
528
529 /**
530  * Function called to provide data for a transmission for a replay.
531  *
532  * @see GNUNET_MULTICAST_replay2()
533  */
534 typedef int
535 (*GNUNET_MULTICAST_ReplayTransmitNotify) (void *cls,
536                                           size_t *data_size,
537                                           void *data);
538
539
540 /**
541  * Replay a message for the multicast group.
542  *
543  * @param rh Replay handle identifying which replay operation was requested.
544  * @param notify Function to call to get the message.
545  * @param notify_cls Closure for @a notify.
546  */
547 void
548 GNUNET_MULTICAST_replay_response2 (struct GNUNET_MULTICAST_ReplayHandle *rh,
549                                    GNUNET_MULTICAST_ReplayTransmitNotify notify,
550                                    void *notify_cls);
551
552
553 /**
554  * Start a multicast group.
555  *
556  * Will advertise the origin in the P2P overlay network under the respective
557  * public key so that other peer can find this peer to join it.  Peers that
558  * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
559  * either an existing group member or to the origin.  If the joining is
560  * approved, the member is cleared for @e replay and will begin to receive
561  * messages transmitted to the group.  If joining is disapproved, the failed
562  * candidate will be given a response.  Members in the group can send messages
563  * to the origin (one at a time).
564  *
565  * @param cfg Configuration to use.
566  * @param priv_key ECC key that will be used to sign messages for this
567  *        multicast session; public key is used to identify the multicast group;
568  * @param max_fragment_id  Maximum fragment ID already sent to the group.
569  *        0 for a new group.
570  * @param join_request_cb Function called to approve / disapprove joining of a peer.
571  * @param member_test_cb Function multicast can use to test group membership.
572  * @param replay_frag_cb Function that can be called to replay a message fragment.
573  * @param replay_msg_cb Function that can be called to replay a message.
574  * @param request_cb Function called with message fragments from group members.
575  * @param message_cb Function called with the message fragments sent to the
576  *        network by GNUNET_MULTICAST_origin_to_all().  These message fragments
577  *        should be stored for answering replay requests later.
578  * @param cls Closure for the various callbacks that follow.
579  * @return Handle for the origin, NULL on error.
580  */
581 struct GNUNET_MULTICAST_Origin *
582 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
583                                const struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key,
584                                uint64_t max_fragment_id,
585                                GNUNET_MULTICAST_JoinRequestCallback join_request_cb,
586                                GNUNET_MULTICAST_MembershipTestCallback member_test_cb,
587                                GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
588                                GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
589                                GNUNET_MULTICAST_RequestCallback request_cb,
590                                GNUNET_MULTICAST_MessageCallback message_cb,
591                                void *cls);
592
593 /**
594  * Function called to provide data for a transmission from the origin to all
595  * members.
596  *
597  * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
598  * invalidates the respective transmission handle.
599  *
600  * @param cls Closure.
601  * @param[in,out] data_size Initially set to the number of bytes available in
602  *        @a data, should be set to the number of bytes written to data.
603  * @param[out] data Where to write the body of the message to give to the
604  *         method. The function must copy at most @a data_size bytes to @a data.
605  * @return #GNUNET_SYSERR on error (fatal, aborts transmission)
606  *         #GNUNET_NO on success, if more data is to be transmitted later.
607  *         Should be used if @a data_size was not big enough to take all the
608  *         data.  If 0 is returned in @a data_size the transmission is paused,
609  *         and can be resumed with GNUNET_MULTICAST_origin_to_all_resume().
610  *         #GNUNET_YES if this completes the transmission (all data supplied)
611  */
612 typedef int
613 (*GNUNET_MULTICAST_OriginTransmitNotify) (void *cls,
614                                           size_t *data_size,
615                                           void *data);
616
617
618 /**
619  * Handle for a request to send a message to all multicast group members
620  * (from the origin).
621  */
622 struct GNUNET_MULTICAST_OriginTransmitHandle;
623
624
625 /**
626  * Send a message to the multicast group.
627  *
628  * @param origin Handle to the multicast group.
629  * @param message_id Application layer ID for the message.  Opaque to multicast.
630  * @param group_generation Group generation of the message.  Documented in
631  *             GNUNET_MULTICAST_MessageHeader.
632  * @param notify Function to call to get the message.
633  * @param notify_cls Closure for @a notify.
634  * @return NULL on error (i.e. request already pending).
635  */
636 struct GNUNET_MULTICAST_OriginTransmitHandle *
637 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
638                                 uint64_t message_id,
639                                 uint64_t group_generation,
640                                 GNUNET_MULTICAST_OriginTransmitNotify notify,
641                                 void *notify_cls);
642
643
644
645 /**
646  * Resume message transmission to multicast group.
647  *
648  * @param th  Transmission to cancel.
649  */
650 void
651 GNUNET_MULTICAST_origin_to_all_resume (struct GNUNET_MULTICAST_OriginTransmitHandle *th);
652
653
654 /**
655  * Cancel request for message transmission to multicast group.
656  *
657  * @param th  Transmission to cancel.
658  */
659 void
660 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginTransmitHandle *th);
661
662
663 /**
664  * Stop a multicast group.
665  *
666  * @param origin Multicast group to stop.
667  */
668 void
669 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *origin,
670                               GNUNET_ContinuationCallback stop_cb,
671                               void *stop_cls);
672
673
674 /**
675  * Join a multicast group.
676  *
677  * The entity joining is always the local peer.  Further information about the
678  * candidate can be provided in @a join_msg.  If the join fails, the
679  * @a message_cb is invoked with a (failure) response and then with NULL.  If
680  * the join succeeds, outstanding (state) messages and ongoing multicast
681  * messages will be given to the @a message_cb until the member decides to part
682  * the group.  The @a mem_test_cb and @a replay_cb functions may be called at
683  * anytime by the multicast service to support relaying messages to other
684  * members of the group.
685  *
686  * @param cfg Configuration to use.
687  * @param group_key ECC public key that identifies the group to join.
688  * @param member_key ECC key that identifies the member and used to sign
689  *        requests sent to the origin.
690  * @param origin Peer ID of the origin to send unicast requsets to.  If NULL,
691  *        unicast requests are sent back via multiple hops on the reverse path
692  *        of multicast messages.
693  * @param relay_count Number of peers in the @a relays array.
694  * @param relays Peer identities of members of the group, which serve as relays
695  *        and can be used to join the group at and send the @a join_request to.
696  *        If empty, the @a join_request is sent directly to the @a origin.
697  * @param join_msg  Application-dependent join message to be passed to the
698  *        @a origin.
699  * @param join_request_cb Function called to approve / disapprove joining of a peer.
700  * @param join_decision_cb Function called to inform about the join decision.
701  * @param mem_test_cb Function multicast can use to test group membership.
702  * @param replay_frag_cb Function that can be called to replay message fragments
703  *        this peer already knows from this group. NULL if this
704  *        client is unable to support replay.
705  * @param replay_msg_cb Function that can be called to replay message fragments
706  *        this peer already knows from this group. NULL if this
707  *        client is unable to support replay.
708  * @param message_cb Function to be called for all message fragments we
709  *        receive from the group, excluding those our @a replay_cb
710  *        already has.
711  * @param cls Closure for callbacks.
712  * @return Handle for the member, NULL on error.
713  */
714 struct GNUNET_MULTICAST_Member *
715 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
716                               const struct GNUNET_CRYPTO_EddsaPublicKey *group_key,
717                               const struct GNUNET_CRYPTO_EcdsaPrivateKey *member_key,
718                               const struct GNUNET_PeerIdentity *origin,
719                               uint16_t relay_count,
720                               const struct GNUNET_PeerIdentity *relays,
721                               const struct GNUNET_MessageHeader *join_request,
722                               GNUNET_MULTICAST_JoinRequestCallback join_request_cb,
723                               GNUNET_MULTICAST_JoinDecisionCallback join_decision_cb,
724                               GNUNET_MULTICAST_MembershipTestCallback mem_test_cb,
725                               GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
726                               GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
727                               GNUNET_MULTICAST_MessageCallback message_cb,
728                               void *cls);
729
730 /**
731  * Handle for a replay request.
732  */
733 struct GNUNET_MULTICAST_MemberReplayHandle;
734
735
736 /**
737  * Request a fragment to be replayed by fragment ID.
738  *
739  * Useful if messages below the @e max_known_fragment_id given when joining are
740  * needed and not known to the client.
741  *
742  * @param member Membership handle.
743  * @param fragment_id ID of a message fragment that this client would like to
744           see replayed.
745  * @param flags Additional flags for the replay request.  It is used and defined
746  *        by the replay callback.  FIXME: which replay callback? FIXME: use enum?
747  *        FIXME: why not pass reply cb here?
748  * @return Replay request handle, NULL on error.
749  */
750 struct GNUNET_MULTICAST_MemberReplayHandle *
751 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *member,
752                                          uint64_t fragment_id,
753                                          uint64_t flags);
754
755
756 /**
757  * Request a message fr to be replayed.
758  *
759  * Useful if messages below the @e max_known_fragment_id given when joining are
760  * needed and not known to the client.
761  *
762  * @param member Membership handle.
763  * @param message_id ID of the message this client would like to see replayed.
764  * @param fragment_offset Offset of the fragment within the message to replay.
765  * @param flags Additional flags for the replay request.  It is used & defined
766  *        by the replay callback.
767  * @param result_cb Function to be called for the replayed message.
768  * @param result_cb_cls Closure for @a result_cb.
769  * @return Replay request handle, NULL on error.
770  */
771 struct GNUNET_MULTICAST_MemberReplayHandle *
772 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *member,
773                                         uint64_t message_id,
774                                         uint64_t fragment_offset,
775                                         uint64_t flags,
776                                         GNUNET_MULTICAST_ResultCallback result_cb,
777                                         void *result_cb_cls);
778
779
780 /**
781  * Cancel a replay request.
782  *
783  * @param rh Request to cancel.
784  */
785 void
786 GNUNET_MULTICAST_member_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh);
787
788
789 /**
790  * Part a multicast group.
791  *
792  * Disconnects from all group members and invalidates the @a member handle.
793  *
794  * An application-dependent part message can be transmitted beforehand using
795  * #GNUNET_MULTICAST_member_to_origin())
796  *
797  * @param member Membership handle.
798  */
799 void
800 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member,
801                               GNUNET_ContinuationCallback part_cb,
802                               void *part_cls);
803
804
805 /**
806  * Function called to provide data for a transmission from a member to the origin.
807  *
808  * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
809  * invalidates the respective transmission handle.
810  *
811  * @param cls Closure.
812  * @param[in,out] data_size Initially set to the number of bytes available in
813  *        @a data, should be set to the number of bytes written to data.
814  * @param[out] data Where to write the body of the message to give to the
815  *         method. The function must copy at most @a data_size bytes to @a data.
816  *
817  * @return #GNUNET_SYSERR on error (fatal, aborts transmission)
818  *         #GNUNET_NO on success, if more data is to be transmitted later.
819  *         Should be used if @a data_size was not big enough to take all the
820  *         data.  If 0 is returned in @a data_size the transmission is paused,
821  *         and can be resumed with GNUNET_MULTICAST_member_to_origin_resume().
822  *         #GNUNET_YES if this completes the transmission (all data supplied)
823  */
824 typedef int
825 (*GNUNET_MULTICAST_MemberTransmitNotify) (void *cls,
826                                           size_t *data_size,
827                                           void *data);
828
829
830 /**
831  * Handle for a message to be delivered from a member to the origin.
832  */
833 struct GNUNET_MULTICAST_MemberTransmitHandle;
834
835
836 /**
837  * Send a message to the origin of the multicast group.
838  *
839  * @param member Membership handle.
840  * @param request_id Application layer ID for the request.  Opaque to multicast.
841  * @param notify Callback to call to get the message.
842  * @param notify_cls Closure for @a notify.
843  *
844  * @return Handle to cancel request, NULL on error (i.e. request already pending).
845  */
846 struct GNUNET_MULTICAST_MemberTransmitHandle *
847 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
848                                    uint64_t request_id,
849                                    GNUNET_MULTICAST_MemberTransmitNotify notify,
850                                    void *notify_cls);
851
852
853 /**
854  * Resume message transmission to origin.
855  *
856  * @param th  Transmission to cancel.
857  */
858 void
859 GNUNET_MULTICAST_member_to_origin_resume (struct GNUNET_MULTICAST_MemberTransmitHandle *th);
860
861
862 /**
863  * Cancel request for message transmission to origin.
864  *
865  * @param th  Transmission to cancel.
866  */
867 void
868 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberTransmitHandle *th);
869
870
871 #if 0                           /* keep Emacsens' auto-indent happy */
872 {
873 #endif
874 #ifdef __cplusplus
875 }
876 #endif
877
878 /* ifndef GNUNET_MULTICAST_SERVICE_H */
879 #endif
880 /* end of gnunet_multicast_service.h */