2 This file is part of GNUnet.
3 Copyright (C) 2009-2017 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @author Christian Grothoff
25 * CADET service; establish channels to distant peers
27 * @defgroup cadet CADET service
28 * Confidential Ad-hoc Decentralized End-to-End Transport
30 * @see [Documentation](https://gnunet.org/cadet-subsystem)
31 * @see [Paper](https://gnunet.org/cadet)
35 #ifndef GNUNET_CADET_SERVICE_H
36 #define GNUNET_CADET_SERVICE_H
41 #if 0 /* keep Emacsens' auto-indent happy */
46 #include "gnunet_util_lib.h"
47 #include "gnunet_transport_service.h"
50 * Version number of GNUnet-cadet API.
52 #define GNUNET_CADET_VERSION 0x00000005
56 * Opaque handle to the service.
58 struct GNUNET_CADET_Handle;
61 * Opaque handle to a channel.
63 struct GNUNET_CADET_Channel;
66 * Opaque handle to a port.
68 struct GNUNET_CADET_Port;
72 * Hash uniquely identifying a connection below a tunnel.
74 struct GNUNET_CADET_ConnectionTunnelIdentifier
76 struct GNUNET_ShortHashCode connection_of_tunnel;
81 * Number identifying a CADET channel within a tunnel.
83 struct GNUNET_CADET_ChannelTunnelNumber
86 * Which number does this channel have that uniquely identfies
87 * it within its tunnel, in network byte order.
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_CRYPTO_cmp_peer_identity().
94 uint32_t cn GNUNET_PACKED;
99 * Channel options. Second line indicates filed in the
100 * CadetChannelInfo union carrying the answer.
102 enum GNUNET_CADET_ChannelOption
105 * Default options: unreliable, default buffering, not out of order.
107 GNUNET_CADET_OPTION_DEFAULT = 0x0,
110 * Disable buffering on intermediate nodes (for minimum latency).
113 GNUNET_CADET_OPTION_NOBUFFER = 0x1,
116 * Enable channel reliability, lost messages will be retransmitted.
119 GNUNET_CADET_OPTION_RELIABLE = 0x2,
122 * Enable out of order delivery of messages.
123 * Set bit for out-of-order delivery.
125 GNUNET_CADET_OPTION_OUT_OF_ORDER = 0x4,
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
132 GNUNET_CADET_OPTION_PEER = 0x8
138 * Method called whenever a peer connects to a port in MQ-based CADET.
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.
150 (*GNUNET_CADET_ConnectEventHandler) (void *cls,
151 struct GNUNET_CADET_Channel *channel,
152 const struct GNUNET_PeerIdentity *source);
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.
160 * It should clean up any associated state, including cancelling any pending
161 * transmission on this channel.
163 * @param cls Channel closure.
164 * @param channel Connection to the other end (henceforth invalid).
167 (*GNUNET_CADET_DisconnectEventHandler) (void *cls,
168 const struct GNUNET_CADET_Channel *channel);
172 * Function called whenever an MQ-channel's transmission window size changes.
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.
177 * For an incoming channel it will be called immediately after the
178 * #GNUNET_CADET_ConnectEventHandler, also with a non-zero value.
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?
186 (*GNUNET_CADET_WindowSizeEventHandler) (void *cls,
187 const struct GNUNET_CADET_Channel *channel,
192 * Connect to the MQ-based cadet service.
194 * @param cfg Configuration to use.
195 * @return Handle to the cadet service NULL on error.
197 struct GNUNET_CADET_Handle *
198 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
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.
207 * @param handle connection to cadet to disconnect
210 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle);
214 * Open a port to receive incomming MQ-based channels.
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.
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
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,
231 GNUNET_CADET_WindowSizeEventHandler window_changes,
232 GNUNET_CADET_DisconnectEventHandler disconnects,
233 const struct GNUNET_MQ_MessageHandler *handlers);
237 * Close a port opened with @a GNUNET_CADET_open_port.
238 * The @a new_channel callback will no longer be called.
240 * @param p Port handle.
243 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p);
247 * Create a new channel towards a remote peer.
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
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.
268 struct GNUNET_CADET_Channel *
269 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
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);
280 * Destroy an existing channel.
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.
286 * @param channel Channel handle, becomes invalid after this call.
289 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel);
293 * Obtain the message queue for a connected channel.
295 * @param channel The channel handle from which to get the MQ.
296 * @return The message queue of the channel.
298 struct GNUNET_MQ_Handle *
299 GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel);
303 * Indicate readiness to receive the next message on a channel.
305 * Should only be called once per handler called.
307 * @param channel Channel that will be allowed to call another handler.
310 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel);
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!
318 * @param port Numerical port (unsigned int format).
320 * @return A GNUNET_HashCode usable for the new CADET API.
322 const struct GNUNET_HashCode *
323 GC_u2h (uint32_t port);
328 * Struct to retrieve info about a channel.
330 union GNUNET_CADET_ChannelInfo
334 * #GNUNET_YES / #GNUNET_NO, for binary flags.
339 * Peer on the other side of the channel
341 const struct GNUNET_PeerIdentity peer;
346 * Get information about a channel.
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.
353 const union GNUNET_CADET_ChannelInfo *
354 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
355 enum GNUNET_CADET_ChannelOption option,
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 /******************************************************************************/
371 * Method called to retrieve information about a specific channel the cadet peer
372 * is aware of, including all transit nodes.
374 * @param cls Closure.
375 * @param root Root of the channel.
376 * @param dest Destination of the channel.
377 * @param port Destination port of the channel.
378 * @param root_channel_number Local number for root, if known.
379 * @param dest_channel_number Local number for dest, if known.
380 * @param public_channel_numbe Number for P2P, always known.
383 (*GNUNET_CADET_ChannelCB) (void *cls,
384 const struct GNUNET_PeerIdentity *root,
385 const struct GNUNET_PeerIdentity *dest,
386 uint32_t /* UGH */ port,
387 uint32_t /* ugh */ root_channel_number,
388 uint32_t /* ugh */ dest_channel_number,
389 uint32_t /* ugh */ public_channel_number);
395 struct GNUNET_CADET_ChannelMonitor;
399 * Request information about a specific channel of the running cadet peer.
401 * @param cfg configuration to use
402 * @param peer ID of the other end of the channel.
403 * @param channel_number Channel number.
404 * @param callback Function to call with the requested data.
405 * @param callback_cls Closure for @c callback.
407 struct GNUNET_CADET_ChannelMonitor *
408 GNUNET_CADET_get_channel (const struct GNUNET_CONFIGURATION_Handle *cfg,
409 struct GNUNET_PeerIdentity *peer,
410 uint32_t /* UGH */ channel_number,
411 GNUNET_CADET_ChannelCB callback,
416 * Cancel a channel monitor request. The callback will not be called (anymore).
418 * @param h Cadet handle.
419 * @return Closure that was given to #GNUNET_CADET_get_channel().
422 GNUNET_CADET_get_channel_cancel (struct GNUNET_CADET_ChannelMonitor *cm);
426 * Method called to retrieve information about all peers in CADET, called
429 * After last peer has been reported, an additional call with NULL is done.
431 * @param cls Closure.
432 * @param peer Peer, or NULL on "EOF".
433 * @param tunnel Do we have a tunnel towards this peer?
434 * @param n_paths Number of known paths towards this peer.
435 * @param best_path How long is the best path?
436 * (0 = unknown, 1 = ourselves, 2 = neighbor)
439 (*GNUNET_CADET_PeersCB) (void *cls,
440 const struct GNUNET_PeerIdentity *peer,
442 unsigned int n_paths,
443 unsigned int best_path);
449 struct GNUNET_CADET_PeersLister;
453 * Request information about peers known to the running cadet service.
454 * The callback will be called for every peer known to the service.
455 * Only one info request (of any kind) can be active at once.
457 * @param cfg configuration to use
458 * @param callback Function to call with the requested data.
459 * @param callback_cls Closure for @c callback.
460 * @return NULL on error
462 struct GNUNET_CADET_PeersLister *
463 GNUNET_CADET_list_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
464 GNUNET_CADET_PeersCB callback,
469 * Cancel a peer info request. The callback will not be called (anymore).
471 * @param pl operation handle
472 * @return Closure that was given to #GNUNET_CADET_list_peers().
475 GNUNET_CADET_list_peers_cancel (struct GNUNET_CADET_PeersLister *pl);
479 * Method called to retrieve information about a specific peer
480 * known to the service.
482 * @param cls Closure.
483 * @param peer Peer ID.
484 * @param tunnel Do we have a tunnel towards this peer? #GNUNET_YES/#GNUNET_NO
485 * @param neighbor Is this a direct neighbor? #GNUNET_YES/#GNUNET_NO
486 * @param n_paths Number of paths known towards peer.
487 * @param paths Array of PEER_IDs representing all paths to reach the peer.
488 * Each path starts with the first hop (local peer not included).
489 * Each path ends with the destination peer (given in @c peer).
492 (*GNUNET_CADET_PeerCB) (void *cls,
493 const struct GNUNET_PeerIdentity *peer,
496 unsigned int n_paths,
497 const struct GNUNET_PeerIdentity *paths,
499 int finished_with_paths);
503 * Handle to cancel #GNUNET_CADET_get_peer() operation.
505 struct GNUNET_CADET_GetPeer;
509 * Request information about a peer known to the running cadet peer.
510 * The callback will be called for the tunnel once.
511 * Only one info request (of any kind) can be active at once.
513 * @param cfg configuration to use
514 * @param id Peer whose tunnel to examine.
515 * @param callback Function to call with the requested data.
516 * @param callback_cls Closure for @c callback.
517 * @return NULL on error
519 struct GNUNET_CADET_GetPeer *
520 GNUNET_CADET_get_peer (const struct GNUNET_CONFIGURATION_Handle *cfg,
521 const struct GNUNET_PeerIdentity *id,
522 GNUNET_CADET_PeerCB callback,
527 * Cancel @a gp operation.
529 * @param gp operation to cancel
530 * @return closure from #GNUNET_CADET_get_peer().
533 GNUNET_CADET_get_peer_cancel (struct GNUNET_CADET_GetPeer *gp);
537 * Method called to retrieve information about all tunnels in CADET, called
540 * After last tunnel has been reported, an additional call with NULL is done.
542 * @param cls Closure.
543 * @param peer Destination peer, or NULL on "EOF".
544 * @param channels Number of channels.
545 * @param connections Number of connections.
546 * @param estate Encryption state.
547 * @param cstate Connectivity state.
550 (*GNUNET_CADET_TunnelsCB) (void *cls,
551 const struct GNUNET_PeerIdentity *peer,
552 unsigned int channels,
553 unsigned int connections,
561 struct GNUNET_CADET_ListTunnels;
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.
569 * @param cfg configuration to use
570 * @param callback Function to call with the requested data.
571 * @param callback_cls Closure for @c callback.
572 * @return NULL on error
574 struct GNUNET_CADET_ListTunnels *
575 GNUNET_CADET_list_tunnels (const struct GNUNET_CONFIGURATION_Handle *cfg,
576 GNUNET_CADET_TunnelsCB callback,
581 * Cancel a monitor request. The monitor callback will not be called.
583 * @param lt operation handle
584 * @return Closure given to #GNUNET_CADET_list_tunnels(), if any.
587 GNUNET_CADET_list_tunnels_cancel (struct GNUNET_CADET_ListTunnels *lt);
592 * Method called to retrieve information about a specific tunnel the cadet peer
593 * has established, o`r is trying to establish.
595 * @param cls Closure.
596 * @param peer Peer towards whom the tunnel is directed.
597 * @param n_channels Number of channels.
598 * @param n_connections Number of connections.
599 * @param channels Channels.
600 * @param connections Connections.
601 * @param estate Encryption state.
602 * @param cstate Connectivity state.
605 (*GNUNET_CADET_TunnelCB) (void *cls,
606 const struct GNUNET_PeerIdentity *peer,
607 unsigned int n_channels,
608 unsigned int n_connections,
609 const struct GNUNET_CADET_ChannelTunnelNumber *channels,
610 const struct GNUNET_CADET_ConnectionTunnelIdentifier *connections,
612 unsigned int cstate);
618 struct GNUNET_CADET_GetTunnel;
622 * Request information about a tunnel of the running cadet peer.
623 * The callback will be called for the tunnel once.
624 * Only one info request (of any kind) can be active at once.
626 * @param cfg configuration to use
627 * @param id Peer whose tunnel to examine.
628 * @param callback Function to call with the requested data.
629 * @param callback_cls Closure for @c callback.
630 * @return NULL on error
632 struct GNUNET_CADET_GetTunnel *
633 GNUNET_CADET_get_tunnel (const struct GNUNET_CONFIGURATION_Handle *cfg,
634 const struct GNUNET_PeerIdentity *id,
635 GNUNET_CADET_TunnelCB callback,
640 * Cancel a monitor request. The monitor callback will not be called.
642 * @param lt operation handle
643 * @return Closure given to #GNUNET_CADET_get_tunnel(), if any.
646 GNUNET_CADET_get_tunnel_cancel (struct GNUNET_CADET_GetTunnel *gt);
649 #if 0 /* keep Emacsens' auto-indent happy */
656 /* ifndef GNUNET_CADET_SERVICE_H */
659 /** @} */ /* end of group */
661 /* end of gnunet_cadet_service.h */