add function conv param string
[oweals/gnunet.git] / src / include / gnunet_cadet_service.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2014 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @author Christian Grothoff
22  * @author Bart Polot
23  *
24  * @file
25  * CADET service; establish channels to distant peers
26  *
27  * @defgroup cadet  CADET service
28  * Confidential Ad-hoc Decentralized End-to-End Transport
29  *
30  * @see [Documentation](https://gnunet.org/cadet-subsystem)
31  * @see [Paper](https://gnunet.org/cadet)
32  *
33  * @{
34  */
35 #ifndef GNUNET_CADET_SERVICE_H
36 #define GNUNET_CADET_SERVICE_H
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #if 0                           /* keep Emacsens' auto-indent happy */
42 }
43 #endif
44 #endif
45
46 #include "gnunet_util_lib.h"
47 #include "gnunet_transport_service.h"
48
49 /**
50  * Version number of GNUnet-cadet API.
51  */
52 #define GNUNET_CADET_VERSION 0x00000003
53
54
55 /**
56  * Opaque handle to the service.
57  */
58 struct GNUNET_CADET_Handle;
59
60 /**
61  * Opaque handle to a channel.
62  */
63 struct GNUNET_CADET_Channel;
64
65 /**
66  * Hash to be used in Cadet communication. Only 256 bits needed,
67  * instead of the 512 from `struct GNUNET_HashCode`.
68  */
69 struct GNUNET_CADET_Hash
70 {
71   unsigned char bits[256 / 8];
72 };
73
74
75 /**
76  * Channel options.  Second line indicates filed in the
77  * CadetChannelInfo union carrying the answer.
78  */
79 enum GNUNET_CADET_ChannelOption
80 {
81   /**
82    * Default options: unreliable, default buffering, not out of order.
83    */
84   GNUNET_CADET_OPTION_DEFAULT    = 0x0,
85
86   /**
87    * Disable buffering on intermediate nodes (for minimum latency).
88    * Yes/No.
89    */
90   GNUNET_CADET_OPTION_NOBUFFER   = 0x1,
91
92   /**
93    * Enable channel reliability, lost messages will be retransmitted.
94    * Yes/No.
95    */
96   GNUNET_CADET_OPTION_RELIABLE   = 0x2,
97
98   /**
99    * Enable out of order delivery of messages.
100    * Yes/No.
101    */
102   GNUNET_CADET_OPTION_OOORDER    = 0x4,
103
104   /**
105    * Who is the peer at the other end of the channel.
106    * Only for use in @c GNUNET_CADET_channel_get_info
107    * struct GNUNET_PeerIdentity *peer
108    */
109   GNUNET_CADET_OPTION_PEER       = 0x8
110
111 };
112
113
114 /**
115  * Functions with this signature are called whenever a message is
116  * received.
117  *
118  * Each time the function must call #GNUNET_CADET_receive_done on the channel
119  * in order to receive the next message. This doesn't need to be immediate:
120  * can be delayed if some processing is done on the message.
121  *
122  * @param cls Closure (set from #GNUNET_CADET_connect).
123  * @param channel Connection to the other end.
124  * @param channel_ctx Place to store local state associated with the channel.
125  * @param message The actual message.
126  * @return #GNUNET_OK to keep the channel open,
127  *         #GNUNET_SYSERR to close it (signal serious error).
128  */
129 typedef int
130 (*GNUNET_CADET_MessageCallback) (void *cls,
131                                  struct GNUNET_CADET_Channel *channel,
132                                  void **channel_ctx,
133                                  const struct GNUNET_MessageHeader *message);
134
135
136 /**
137  * Message handler.  Each struct specifies how to handle on particular
138  * type of message received.
139  */
140 struct GNUNET_CADET_MessageHandler
141 {
142   /**
143    * Function to call for messages of type @e type.
144    */
145   GNUNET_CADET_MessageCallback callback;
146
147   /**
148    * Type of the message this handler covers.
149    */
150   uint16_t type;
151
152   /**
153    * Expected size of messages of this type.  Use 0 for variable-size.
154    * If non-zero, messages of the given type will be discarded if they
155    * do not have the right size.
156    */
157   uint16_t expected_size;
158 };
159
160
161 /**
162  * Method called whenever another peer has added us to a channel
163  * the other peer initiated.
164  * Only called (once) upon reception of data with a message type which was
165  * subscribed to in #GNUNET_CADET_connect.
166  *
167  * A call to #GNUNET_CADET_channel_destroy causes te channel to be ignored. In
168  * this case the handler MUST return NULL.
169  *
170  * @param cls closure
171  * @param channel new handle to the channel
172  * @param initiator peer that started the channel
173  * @param port Port this channel is for.
174  * @param options CadetOption flag field, with all active option bits set to 1.
175  *
176  * @return initial channel context for the channel
177  *         (can be NULL -- that's not an error)
178  */
179 typedef void *
180 (GNUNET_CADET_InboundChannelNotificationHandler) (void *cls,
181                                                   struct GNUNET_CADET_Channel *channel,
182                                                   const struct GNUNET_PeerIdentity *initiator,
183                                                   uint32_t port,
184                                                   enum GNUNET_CADET_ChannelOption options);
185
186
187 /**
188  * Function called whenever a channel is destroyed.  Should clean up
189  * any associated state, including cancelling any pending transmission on this
190  * channel.
191  *
192  * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
193  *
194  * @param cls closure (set from #GNUNET_CADET_connect)
195  * @param channel connection to the other end (henceforth invalid)
196  * @param channel_ctx place where local state associated
197  *                   with the channel is stored
198  */
199 typedef void
200 (GNUNET_CADET_ChannelEndHandler) (void *cls,
201                                   const struct GNUNET_CADET_Channel *channel,
202                                   void *channel_ctx);
203
204
205 /**
206  * Connect to the cadet service.
207  *
208  * @param cfg Configuration to use.
209  * @param cls Closure for the various callbacks that follow (including
210  *            handlers in the handlers array).
211  * @param new_channel Function called when an *incoming* channel is created.
212  *                    Can be NULL if no inbound channels are desired.
213  *                    See @a ports.
214  * @param cleaner Function called when a channel is destroyed.
215  *                It is called immediately if #GNUNET_CADET_channel_destroy
216  *                is called on the channel.
217  * @param handlers Callbacks for messages we care about, NULL-terminated. Each
218  *                 one must call #GNUNET_CADET_receive_done on the channel to
219  *                 receive the next message.  Messages of a type that is not
220  *                 in the handlers array are ignored if received.
221  * @param ports NULL or 0-terminated array of port numbers for incoming channels.
222  *              See @a new_channel.
223  *
224  * @return handle to the cadet service NULL on error
225  *         (in this case, init is never called)
226  */
227 struct GNUNET_CADET_Handle *
228 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
229                       void *cls,
230                       GNUNET_CADET_InboundChannelNotificationHandler new_channel,
231                       GNUNET_CADET_ChannelEndHandler cleaner,
232                       const struct GNUNET_CADET_MessageHandler *handlers,
233                       const uint32_t *ports);
234
235
236 /**
237  * Disconnect from the cadet service. All channels will be destroyed. All channel
238  * disconnect callbacks will be called on any still connected peers, notifying
239  * about their disconnection. The registered inbound channel cleaner will be
240  * called should any inbound channels still exist.
241  *
242  * @param handle connection to cadet to disconnect
243  */
244 void
245 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle);
246
247
248 /**
249  * Create a new channel towards a remote peer.
250  *
251  * If the destination port is not open by any peer or the destination peer
252  * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
253  * for this channel.
254  *
255  * @param h cadet handle
256  * @param channel_ctx client's channel context to associate with the channel
257  * @param peer peer identity the channel should go to
258  * @param port Port number.
259  * @param options CadetOption flag field, with all desired option bits set to 1.
260  *
261  * @return handle to the channel
262  */
263 struct GNUNET_CADET_Channel *
264 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
265                              void *channel_ctx,
266                              const struct GNUNET_PeerIdentity *peer,
267                              uint32_t port,
268                              enum GNUNET_CADET_ChannelOption options);
269
270
271 /**
272  * Destroy an existing channel.
273  *
274  * The existing end callback for the channel will be called immediately.
275  * Any pending outgoing messages will be sent but no incoming messages will be
276  * accepted and no data callbacks will be called.
277  *
278  * @param channel Channel handle, becomes invalid after this call.
279  */
280 void
281 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel);
282
283
284 /**
285  * Struct to retrieve info about a channel.
286  */
287 union GNUNET_CADET_ChannelInfo
288 {
289
290   /**
291    * #GNUNET_YES / #GNUNET_NO, for binary flags.
292    */
293   int yes_no;
294
295   /**
296    * Peer on the other side of the channel
297    */
298   const struct GNUNET_PeerIdentity peer;
299 };
300
301
302 /**
303  * Get information about a channel.
304  *
305  * @param channel Channel handle.
306  * @param option Query type GNUNET_CADET_OPTION_*
307  * @param ... dependant on option, currently not used
308  * @return Union with an answer to the query.
309  */
310 const union GNUNET_CADET_ChannelInfo *
311 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
312                               enum GNUNET_CADET_ChannelOption option, ...);
313
314
315 /**
316  * Handle for a transmission request.
317  */
318 struct GNUNET_CADET_TransmitHandle;
319
320
321 /**
322  * Ask the cadet to call @a notify once it is ready to transmit the
323  * given number of bytes to the specified channel.
324  * Only one call can be active at any time, to issue another request,
325  * wait for the callback or cancel the current request.
326  *
327  * @param channel channel to use for transmission
328  * @param cork is corking allowed for this transmission?
329  * @param maxdelay how long can the message wait?
330  * @param notify_size how many bytes of buffer space does notify want?
331  * @param notify function to call when buffer space is available;
332  *        will be called with NULL on timeout or if the overall queue
333  *        for this peer is larger than queue_size and this is currently
334  *        the message with the lowest priority
335  * @param notify_cls closure for @a notify
336  * @return non-NULL if the notify callback was queued,
337  *         NULL if we can not even queue the request (insufficient
338  *         memory); if NULL is returned, @a notify will NOT be called.
339  */
340 struct GNUNET_CADET_TransmitHandle *
341 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
342                                    int cork,
343                                    struct GNUNET_TIME_Relative maxdelay,
344                                    size_t notify_size,
345                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
346                                    void *notify_cls);
347
348
349 /**
350  * Cancel the specified transmission-ready notification.
351  *
352  * @param th handle that was returned by "notify_transmit_ready".
353  */
354 void
355 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th);
356
357
358 /**
359  * Indicate readiness to receive the next message on a channel.
360  *
361  * Should only be called once per handler called.
362  *
363  * @param channel Channel that will be allowed to call another handler.
364  */
365 void
366 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel);
367
368
369
370 /******************************************************************************/
371 /********************       MONITORING /DEBUG API     *************************/
372 /******************************************************************************/
373 /* The following calls are not useful for normal CADET operation, but for      */
374 /* debug and monitoring of the cadet state. They can be safely ignored.        */
375 /* The API can change at any point without notice.                            */
376 /* Please contact the developer if you consider any of this calls useful for  */
377 /* normal cadet applications.                                                  */
378 /******************************************************************************/
379
380
381 /**
382  * Method called to retrieve information about a specific channel the cadet peer
383  * is aware of, including all transit nodes.
384  *
385  * @param cls Closure.
386  * @param root Root of the channel.
387  * @param dest Destination of the channel.
388  * @param port Destination port of the channel.
389  * @param root_channel_number Local number for root, if known.
390  * @param dest_channel_number Local number for dest, if known.
391  * @param public_channel_numbe Number for P2P, always known.
392  */
393 typedef void
394 (*GNUNET_CADET_ChannelCB) (void *cls,
395                            const struct GNUNET_PeerIdentity *root,
396                            const struct GNUNET_PeerIdentity *dest,
397                            uint32_t port,
398                            uint32_t root_channel_number,
399                            uint32_t dest_channel_number,
400                            uint32_t public_channel_number);
401
402 /**
403  * Method called to retrieve information about all peers in CADET, called
404  * once per peer.
405  *
406  * After last peer has been reported, an additional call with NULL is done.
407  *
408  * @param cls Closure.
409  * @param peer Peer, or NULL on "EOF".
410  * @param tunnel Do we have a tunnel towards this peer?
411  * @param n_paths Number of known paths towards this peer.
412  * @param best_path How long is the best path?
413  *                  (0 = unknown, 1 = ourselves, 2 = neighbor)
414  */
415 typedef void
416 (*GNUNET_CADET_PeersCB) (void *cls,
417                          const struct GNUNET_PeerIdentity *peer,
418                          int tunnel,
419                          unsigned int n_paths,
420                          unsigned int best_path);
421
422 /**
423  * Method called to retrieve information about a specific peer
424  * known to the service.
425  *
426  * @param cls Closure.
427  * @param peer Peer ID.
428  * @param tunnel Do we have a tunnel towards this peer? #GNUNET_YES/#GNUNET_NO
429  * @param neighbor Is this a direct neighbor? #GNUNET_YES/#GNUNET_NO
430  * @param n_paths Number of paths known towards peer.
431  * @param paths Array of PEER_IDs representing all paths to reach the peer.
432  *              Each path starts with the first hop (local peer not included).
433  *              Each path ends with the destination peer (given in @c peer).
434  */
435 typedef void
436 (*GNUNET_CADET_PeerCB) (void *cls,
437                         const struct GNUNET_PeerIdentity *peer,
438                         int tunnel,
439                         int neighbor,
440                         unsigned int n_paths,
441                         struct GNUNET_PeerIdentity *paths);
442
443
444 /**
445  * Method called to retrieve information about all tunnels in CADET, called
446  * once per tunnel.
447  *
448  * After last tunnel has been reported, an additional call with NULL is done.
449  *
450  * @param cls Closure.
451  * @param peer Destination peer, or NULL on "EOF".
452  * @param channels Number of channels.
453  * @param connections Number of connections.
454  * @param estate Encryption state.
455  * @param cstate Connectivity state.
456  */
457 typedef void
458 (*GNUNET_CADET_TunnelsCB) (void *cls,
459                            const struct GNUNET_PeerIdentity *peer,
460                            unsigned int channels,
461                            unsigned int connections,
462                            uint16_t estate,
463                            uint16_t cstate);
464
465
466 /**
467  * Method called to retrieve information about a specific tunnel the cadet peer
468  * has established, o`r is trying to establish.
469  *
470  * @param cls Closure.
471  * @param peer Peer towards whom the tunnel is directed.
472  * @param n_channels Number of channels.
473  * @param n_connections Number of connections.
474  * @param channels Channels.
475  * @param connections Connections.
476  * @param estate Encryption state.
477  * @param cstate Connectivity state.
478  */
479 typedef void
480 (*GNUNET_CADET_TunnelCB) (void *cls,
481                           const struct GNUNET_PeerIdentity *peer,
482                           unsigned int n_channels,
483                           unsigned int n_connections,
484                           uint32_t *channels,
485                           struct GNUNET_CADET_Hash *connections,
486                           unsigned int estate,
487                           unsigned int cstate);
488
489
490 /**
491  * Request information about a specific channel of the running cadet peer.
492  *
493  * WARNING: unstable API, likely to change in the future!
494  *
495  * @param h Handle to the cadet peer.
496  * @param peer ID of the other end of the channel.
497  * @param channel_number Channel number.
498  * @param callback Function to call with the requested data.
499  * @param callback_cls Closure for @c callback.
500  */
501 void
502 GNUNET_CADET_get_channel (struct GNUNET_CADET_Handle *h,
503                           struct GNUNET_PeerIdentity *peer,
504                           uint32_t channel_number,
505                           GNUNET_CADET_ChannelCB callback,
506                           void *callback_cls);
507
508
509 /**
510  * Request a debug dump on the service's STDERR.
511  *
512  * WARNING: unstable API, likely to change in the future!
513  *
514  * @param h cadet handle
515  */
516 void
517 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h);
518
519
520 /**
521  * Request information about peers known to the running cadet service.
522  * The callback will be called for every peer known to the service.
523  * Only one info request (of any kind) can be active at once.
524  *
525  *
526  * WARNING: unstable API, likely to change in the future!
527  *
528  * @param h Handle to the cadet peer.
529  * @param callback Function to call with the requested data.
530  * @param callback_cls Closure for @c callback.
531  *
532  * @return #GNUNET_OK / #GNUNET_SYSERR
533  */
534 int
535 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
536                         GNUNET_CADET_PeersCB callback,
537                         void *callback_cls);
538
539
540 /**
541  * Cancel a peer info request. The callback will not be called (anymore).
542  *
543  * WARNING: unstable API, likely to change in the future!
544  *
545  * @param h Cadet handle.
546  *
547  * @return Closure that was given to #GNUNET_CADET_get_peers().
548  */
549 void *
550 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h);
551
552
553 /**
554  * Request information about a peer known to the running cadet peer.
555  * The callback will be called for the tunnel once.
556  * Only one info request (of any kind) can be active at once.
557  *
558  * WARNING: unstable API, likely to change in the future!
559  *
560  * @param h Handle to the cadet peer.
561  * @param id Peer whose tunnel to examine.
562  * @param callback Function to call with the requested data.
563  * @param callback_cls Closure for @c callback.
564  *
565  * @return #GNUNET_OK / #GNUNET_SYSERR
566  */
567 int
568 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
569                       const struct GNUNET_PeerIdentity *id,
570                       GNUNET_CADET_PeerCB callback,
571                       void *callback_cls);
572
573
574 /**
575  * Request information about tunnels of the running cadet peer.
576  * The callback will be called for every tunnel of the service.
577  * Only one info request (of any kind) can be active at once.
578  *
579  * WARNING: unstable API, likely to change in the future!
580  *
581  * @param h Handle to the cadet peer.
582  * @param callback Function to call with the requested data.
583  * @param callback_cls Closure for @c callback.
584  *
585  * @return #GNUNET_OK / #GNUNET_SYSERR
586  */
587 int
588 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
589                           GNUNET_CADET_TunnelsCB callback,
590                           void *callback_cls);
591
592
593 /**
594  * Cancel a monitor request. The monitor callback will not be called.
595  *
596  * @param h Cadet handle.
597  *
598  * @return Closure given to #GNUNET_CADET_get_tunnels(), if any.
599  */
600 void *
601 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h);
602
603
604 /**
605  * Request information about a tunnel of the running cadet peer.
606  * The callback will be called for the tunnel once.
607  * Only one info request (of any kind) can be active at once.
608  *
609  * WARNING: unstable API, likely to change in the future!
610  *
611  * @param h Handle to the cadet peer.
612  * @param id Peer whose tunnel to examine.
613  * @param callback Function to call with the requested data.
614  * @param callback_cls Closure for @c callback.
615  *
616  * @return #GNUNET_OK / #GNUNET_SYSERR
617  */
618 int
619 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
620                          const struct GNUNET_PeerIdentity *id,
621                          GNUNET_CADET_TunnelCB callback,
622                          void *callback_cls);
623
624
625 /**
626  * Create a message queue for a cadet channel.
627  * The message queue can only be used to transmit messages,
628  * not to receive them.
629  *
630  * @param channel the channel to create the message qeue for
631  * @return a message queue to messages over the channel
632  */
633 struct GNUNET_MQ_Handle *
634 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel);
635
636
637 #if 0                           /* keep Emacsens' auto-indent happy */
638 {
639 #endif
640 #ifdef __cplusplus
641 }
642 #endif
643
644 /* ifndef GNUNET_CADET_SERVICE_H */
645 #endif
646
647 /** @} */  /* end of group */
648
649 /* end of gnunet_cadet_service.h */