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