towards flow control in TNG
[oweals/gnunet.git] / src / include / gnunet_cadet_service.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2017 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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 0x00000005
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  * Opaque handle to a port.
67  */
68 struct GNUNET_CADET_Port;
69
70
71 /**
72  * Hash uniquely identifying a connection below a tunnel.
73  */
74 struct GNUNET_CADET_ConnectionTunnelIdentifier
75 {
76   struct GNUNET_ShortHashCode connection_of_tunnel;
77 };
78
79
80 /**
81  * Number identifying a CADET channel within a tunnel.
82  */
83 struct GNUNET_CADET_ChannelTunnelNumber
84 {
85   /**
86    * Which number does this channel have that uniquely identfies
87    * it within its tunnel, in network byte order.
88    *
89    * Given two peers, both may initiate channels over the same tunnel.
90    * The @e cn must be greater or equal to 0x80000000 (high-bit set)
91    * for tunnels initiated with the peer that has the larger peer
92    * identity as compared using #GNUNET_memcmp().
93    */
94   uint32_t cn GNUNET_PACKED;
95 };
96
97
98 /**
99  * Channel options.  Second line indicates filed in the
100  * CadetChannelInfo union carrying the answer.
101  */
102 enum GNUNET_CADET_ChannelOption
103 {
104   /**
105    * Default options: unreliable, default buffering, not out of order.
106    */
107   GNUNET_CADET_OPTION_DEFAULT    = 0x0,
108
109   /**
110    * Disable buffering on intermediate nodes (for minimum latency).
111    * Yes/No.
112    */
113   GNUNET_CADET_OPTION_NOBUFFER   = 0x1,
114
115   /**
116    * Enable channel reliability, lost messages will be retransmitted.
117    * Yes/No.
118    */
119   GNUNET_CADET_OPTION_RELIABLE   = 0x2,
120
121   /**
122    * Enable out of order delivery of messages.
123    * Set bit for out-of-order delivery.
124    */
125   GNUNET_CADET_OPTION_OUT_OF_ORDER = 0x4,
126
127   /**
128    * Who is the peer at the other end of the channel.
129    * Only for use in @c GNUNET_CADET_channel_get_info
130    * struct GNUNET_PeerIdentity *peer
131    */
132   GNUNET_CADET_OPTION_PEER       = 0x8
133
134 };
135
136
137 /**
138  * Method called whenever a peer connects to a port in MQ-based CADET.
139  *
140  * @param cls Closure from #GNUNET_CADET_open_port.
141  * @param channel New handle to the channel.
142  * @param source Peer that started this channel.
143  * @return Closure for the incoming @a channel. It's given to:
144  *         - The #GNUNET_CADET_DisconnectEventHandler (given to
145  *           #GNUNET_CADET_open_port) when the channel dies.
146  *         - Each the #GNUNET_MQ_MessageCallback handlers for each message
147  *           received on the @a channel.
148  */
149 typedef void *
150 (*GNUNET_CADET_ConnectEventHandler) (void *cls,
151                                      struct GNUNET_CADET_Channel *channel,
152                                      const struct GNUNET_PeerIdentity *source);
153
154
155 /**
156  * Function called whenever an MQ-channel is destroyed, unless the destruction
157  * was requested by #GNUNET_CADET_channel_destroy.
158  * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
159  *
160  * It should clean up any associated state, including cancelling any pending
161  * transmission on this channel.
162  *
163  * @param cls Channel closure.
164  * @param channel Connection to the other end (henceforth invalid).
165  */
166 typedef void
167 (*GNUNET_CADET_DisconnectEventHandler) (void *cls,
168                                         const struct GNUNET_CADET_Channel *channel);
169
170
171 /**
172  * Function called whenever an MQ-channel's transmission window size changes.
173  *
174  * The first callback in an outgoing channel will be with a non-zero value
175  * and will mean the channel is connected to the destination.
176  *
177  * For an incoming channel it will be called immediately after the
178  * #GNUNET_CADET_ConnectEventHandler, also with a non-zero value.
179  *
180  * @param cls Channel closure.
181  * @param channel Connection to the other end --- FIXME: drop?
182  * @param window_size New window size. If the is more messages than buffer size
183  *                    this value will be negative. -- FIXME: make unsigned, we never call negative?
184  */
185 typedef void
186 (*GNUNET_CADET_WindowSizeEventHandler) (void *cls,
187                                         const struct GNUNET_CADET_Channel *channel,
188                                         int window_size);
189
190
191 /**
192  * Connect to the MQ-based cadet service.
193  *
194  * @param cfg Configuration to use.
195  * @return Handle to the cadet service NULL on error.
196  */
197 struct GNUNET_CADET_Handle *
198 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
199
200
201 /**
202  * Disconnect from the cadet service. All channels will be destroyed. All channel
203  * disconnect callbacks will be called on any still connected peers, notifying
204  * about their disconnection. The registered inbound channel cleaner will be
205  * called should any inbound channels still exist.
206  *
207  * @param handle connection to cadet to disconnect
208  */
209 void
210 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle);
211
212
213 /**
214  * Open a port to receive incomming MQ-based channels.
215  *
216  * @param h CADET handle.
217  * @param port Hash identifying the port.
218  * @param connects Function called when an incoming channel is connected.
219  * @param connects_cls Closure for the @a connects handler.
220  * @param window_changes Function called when the transmit window size changes.
221  *                       Can be NULL.
222  * @param disconnects Function called when a channel is disconnected.
223  * @param handlers Callbacks for messages we care about, NULL-terminated.
224  * @return Port handle, NULL if port is in use
225  */
226 struct GNUNET_CADET_Port *
227 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
228                         const struct GNUNET_HashCode *port,
229                         GNUNET_CADET_ConnectEventHandler connects,
230                         void *connects_cls,
231                         GNUNET_CADET_WindowSizeEventHandler window_changes,
232                         GNUNET_CADET_DisconnectEventHandler disconnects,
233                         const struct GNUNET_MQ_MessageHandler *handlers);
234
235
236 /**
237  * Close a port opened with @a GNUNET_CADET_open_port.
238  * The @a new_channel callback will no longer be called.
239  *
240  * @param p Port handle.
241  */
242 void
243 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p);
244
245
246 /**
247  * Create a new channel towards a remote peer.
248  *
249  * If the destination port is not open by any peer or the destination peer
250  * does not accept the channel, @a disconnects will be called
251  * for this channel.
252  *
253  * @param h CADET handle.
254  * @param channel_cls Closure for the channel. It's given to:
255  *                    - The management handler @a window_changes.
256  *                    - The disconnect handler @a disconnects
257  *                    - Each message type callback in @a handlers
258  * @param destination Peer identity the channel should go to.
259  * @param port Identification of the destination port.
260  * @param options CadetOption flag field, with all desired option bits set to 1.
261  * @param window_changes Function called when the transmit window size changes.
262  *                       Can be NULL if this data is of no interest.
263  * TODO                  Not yet implemented.
264  * @param disconnects Function called when the channel is disconnected.
265  * @param handlers Callbacks for messages we care about, NULL-terminated.
266  * @return Handle to the channel.
267  */
268 struct GNUNET_CADET_Channel *
269 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
270                              void *channel_cls,
271                              const struct GNUNET_PeerIdentity *destination,
272                              const struct GNUNET_HashCode *port,
273                              enum GNUNET_CADET_ChannelOption options,
274                              GNUNET_CADET_WindowSizeEventHandler window_changes,
275                              GNUNET_CADET_DisconnectEventHandler disconnects,
276                              const struct GNUNET_MQ_MessageHandler *handlers);
277
278
279 /**
280  * Destroy an existing channel.
281  *
282  * The existing end callback for the channel will NOT be called.
283  * Any pending outgoing messages will be sent but no incoming messages will be
284  * accepted and no data callbacks will be called.
285  *
286  * @param channel Channel handle, becomes invalid after this call.
287  */
288 void
289 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel);
290
291
292 /**
293  * Obtain the message queue for a connected channel.
294  *
295  * @param channel The channel handle from which to get the MQ.
296  * @return The message queue of the channel.
297  */
298 struct GNUNET_MQ_Handle *
299 GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel);
300
301
302 /**
303  * Indicate readiness to receive the next message on a channel.
304  *
305  * Should only be called once per handler called.
306  *
307  * @param channel Channel that will be allowed to call another handler.
308  */
309 void
310 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel);
311
312
313 /**
314  * Transitional function to convert an unsigned int port to a hash value.
315  * WARNING: local static value returned, NOT reentrant!
316  * WARNING: do not use this function for new code!
317  *
318  * @param port Numerical port (unsigned int format).
319  *
320  * @return A GNUNET_HashCode usable for the new CADET API.
321  */
322 const struct GNUNET_HashCode *
323 GC_u2h (uint32_t port);
324
325
326
327 /**
328  * Union to retrieve info about a channel.
329  */
330 union GNUNET_CADET_ChannelInfo
331 {
332
333   /**
334    * #GNUNET_YES / #GNUNET_NO, for binary flags.
335    */
336   int yes_no;
337
338   /**
339    * Peer on the other side of the channel
340    */
341   const struct GNUNET_PeerIdentity peer;
342 };
343
344
345 /**
346  * Get information about a channel.
347  *
348  * @param channel Channel handle.
349  * @param option Query type GNUNET_CADET_OPTION_*
350  * @param ... dependant on option, currently not used
351  * @return Union with an answer to the query.
352  */
353 const union GNUNET_CADET_ChannelInfo *
354 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
355                                enum GNUNET_CADET_ChannelOption option,
356                                ...);
357
358
359 /******************************************************************************/
360 /********************       MONITORING /DEBUG API     *************************/
361 /******************************************************************************/
362 /* The following calls are not useful for normal CADET operation, but for      */
363 /* debug and monitoring of the cadet state. They can be safely ignored.        */
364 /* The API can change at any point without notice.                            */
365 /* Please contact the developer if you consider any of this calls useful for  */
366 /* normal cadet applications.                                                  */
367 /******************************************************************************/
368
369
370 /**
371  * Internal details about a channel.
372  */
373 struct GNUNET_CADET_ChannelInternals
374 {
375   /**
376    * Root of the channel
377    */
378   struct GNUNET_PeerIdentity root;
379
380   /**
381    * Destination of the channel
382    */
383   struct GNUNET_PeerIdentity dest;
384
385   // to be expanded!
386 };
387
388
389 /**
390  * Method called to retrieve information about a specific channel the cadet peer
391  * is aware of, including all transit nodes.
392  *
393  * @param cls Closure.
394  * @param info internal details, NULL for end of list
395  */
396 typedef void
397 (*GNUNET_CADET_ChannelCB) (void *cls,
398                            const struct GNUNET_CADET_ChannelInternals *info);
399
400
401 /**
402  * Operation handle.
403  */
404 struct GNUNET_CADET_ChannelMonitor;
405
406
407 /**
408  * Request information about channels to @a peer from the local peer.
409  *
410  * @param cfg configuration to use
411  * @param peer ID of the other end of the channel.
412  * @param callback Function to call with the requested data.
413  * @param callback_cls Closure for @c callback.
414  */
415 struct GNUNET_CADET_ChannelMonitor *
416 GNUNET_CADET_get_channel (const struct GNUNET_CONFIGURATION_Handle *cfg,
417                           struct GNUNET_PeerIdentity *peer,
418                           GNUNET_CADET_ChannelCB callback,
419                           void *callback_cls);
420
421
422 /**
423  * Cancel a channel monitor request. The callback will not be called (anymore).
424  *
425  * @param h Cadet handle.
426  * @return Closure that was given to #GNUNET_CADET_get_channel().
427  */
428 void *
429 GNUNET_CADET_get_channel_cancel (struct GNUNET_CADET_ChannelMonitor *cm);
430
431
432 /**
433  * Information we return per peer.
434  */
435 struct GNUNET_CADET_PeerListEntry
436 {
437   /**
438    * Which peer is the information about?
439    */
440   struct GNUNET_PeerIdentity peer;
441
442   /**
443    * Do we have a tunnel to this peer?
444    */
445   int have_tunnel;
446
447   /**
448    * Number of disjoint known paths to @e peer.
449    */
450   unsigned int n_paths;
451
452   /**
453    * Length of the shortest path (0 = unknown, 1 = ourselves, 2 = direct neighbour).
454    */
455   unsigned int best_path_length;
456 };
457
458
459 /**
460  * Method called to retrieve information about all peers in CADET, called
461  * once per peer.
462  *
463  * After last peer has been reported, an additional call with NULL is done.
464  *
465  * @param cls Closure.
466  * @param ple information about a peer, or NULL on "EOF".
467  */
468 typedef void
469 (*GNUNET_CADET_PeersCB) (void *cls,
470                          const struct GNUNET_CADET_PeerListEntry *ple);
471
472
473 /**
474  * Operation handle.
475  */
476 struct GNUNET_CADET_PeersLister;
477
478
479 /**
480  * Request information about peers known to the running cadet service.
481  * The callback will be called for every peer known to the service.
482  * Only one info request (of any kind) can be active at once.
483  *
484  * @param cfg configuration to use
485  * @param callback Function to call with the requested data.
486  * @param callback_cls Closure for @c callback.
487  * @return NULL on error
488  */
489 struct GNUNET_CADET_PeersLister *
490 GNUNET_CADET_list_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
491                          GNUNET_CADET_PeersCB callback,
492                          void *callback_cls);
493
494
495 /**
496  * Cancel a peer info request. The callback will not be called (anymore).
497  *
498  * @param pl operation handle
499  * @return Closure that was given to #GNUNET_CADET_list_peers().
500  */
501 void *
502 GNUNET_CADET_list_peers_cancel (struct GNUNET_CADET_PeersLister *pl);
503
504
505 /**
506  * Detailed information we return per peer.
507  */
508 struct GNUNET_CADET_PeerPathDetail
509 {
510   /**
511    * Peer this is about.
512    */
513   struct GNUNET_PeerIdentity peer;
514
515   /**
516    * Offset of the target peer on the @e path.
517    */
518   unsigned int target_offset;
519
520   /**
521    * Number of entries on the @e path.
522    */
523   unsigned int path_length;
524
525   /**
526    * Array of PEER_IDs representing all paths to reach the peer.  Each
527    * path starts with the first hop (local peer not included).  Each
528    * path ends with the destination peer (given in @e peer).
529    */
530   const struct GNUNET_PeerIdentity *path;
531
532 };
533
534
535 /**
536  * Method called to retrieve information about a specific path
537  * known to the service.
538  *
539  * @param cls Closure.
540  * @param ppd details about a path to the peer, NULL for end of information
541  */
542 typedef void
543 (*GNUNET_CADET_PathCB) (void *cls,
544                         const struct GNUNET_CADET_PeerPathDetail *ppd);
545
546
547 /**
548  * Handle to cancel #GNUNET_CADET_get_path() operation.
549  */
550 struct GNUNET_CADET_GetPath;
551
552
553 /**
554  * Request information about a peer known to the running cadet peer.
555  *
556  * @param cfg configuration to use
557  * @param id Peer whose paths we want to examine.
558  * @param callback Function to call with the requested data.
559  * @param callback_cls Closure for @c callback.
560  * @return NULL on error
561  */
562 struct GNUNET_CADET_GetPath *
563 GNUNET_CADET_get_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
564                        const struct GNUNET_PeerIdentity *id,
565                        GNUNET_CADET_PathCB callback,
566                        void *callback_cls);
567
568
569 /**
570  * Cancel @a gp operation.
571  *
572  * @param gp operation to cancel
573  * @return closure from #GNUNET_CADET_get_path().
574  */
575 void *
576 GNUNET_CADET_get_path_cancel (struct GNUNET_CADET_GetPath *gp);
577
578
579 /**
580  * Details about a tunnel managed by CADET.
581  */
582 struct GNUNET_CADET_TunnelDetails
583 {
584   /**
585    * Target of the tunnel.
586    */
587   struct GNUNET_PeerIdentity peer;
588
589   /**
590    * How many channels use the tunnel.
591    */
592   uint32_t channels;
593
594   /**
595    * How many connections support the tunnel.
596    */
597   uint32_t connections;
598
599   /**
600    * What is our encryption state?
601    */
602   uint16_t estate;
603
604   /**
605    * What is our connectivity state?
606    */
607   uint16_t cstate;
608 };
609
610
611 /**
612  * Method called to retrieve information about all tunnels in CADET, called
613  * once per tunnel.
614  *
615  * After last tunnel has been reported, an additional call with NULL is done.
616  *
617  * @param cls Closure.
618  * @param td tunnel details, NULL for end of list
619  */
620 typedef void
621 (*GNUNET_CADET_TunnelsCB) (void *cls,
622                            const struct GNUNET_CADET_TunnelDetails *td);
623
624
625 /**
626  * Operation handle.
627  */
628 struct GNUNET_CADET_ListTunnels;
629
630
631 /**
632  * Request information about tunnels of the running cadet peer.
633  * The callback will be called for every tunnel of the service.
634  * Only one info request (of any kind) can be active at once.
635  *
636  * @param cfg configuration to use
637  * @param callback Function to call with the requested data.
638  * @param callback_cls Closure for @c callback.
639  * @return NULL on error
640  */
641 struct GNUNET_CADET_ListTunnels *
642 GNUNET_CADET_list_tunnels (const struct GNUNET_CONFIGURATION_Handle *cfg,
643                            GNUNET_CADET_TunnelsCB callback,
644                            void *callback_cls);
645
646
647 /**
648  * Cancel a monitor request. The monitor callback will not be called.
649  *
650  * @param lt operation handle
651  * @return Closure given to #GNUNET_CADET_list_tunnels(), if any.
652  */
653 void *
654 GNUNET_CADET_list_tunnels_cancel (struct GNUNET_CADET_ListTunnels *lt);
655
656
657 #if 0                           /* keep Emacsens' auto-indent happy */
658 {
659 #endif
660 #ifdef __cplusplus
661 }
662 #endif
663
664 /* ifndef GNUNET_CADET_SERVICE_H */
665 #endif
666
667 /** @} */  /* end of group */
668
669 /* end of gnunet_cadet_service.h */