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