2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file cadet/gnunet-service-cadet_tunnel.h
23 * @brief cadet service; dealing with tunnels and crypto
24 * @author Bartlomiej Polot
26 * All functions in this file should use the prefix GMT (Gnunet Cadet Tunnel)
29 #ifndef GNUNET_SERVICE_CADET_TUNNEL_H
30 #define GNUNET_SERVICE_CADET_TUNNEL_H
35 #if 0 /* keep Emacsens' auto-indent happy */
41 #include "gnunet_util_lib.h"
44 * All the connectivity states a tunnel can be in.
46 enum CadetTunnelCState
49 * Uninitialized status, should never appear in operation.
54 * No path to the peer known yet.
56 CADET_TUNNEL_SEARCHING,
59 * Request sent, not yet answered.
64 * Peer connected and ready to accept data.
69 * Tunnel being shut down, don't try to keep it alive.
76 * All the encryption states a tunnel can be in.
78 enum CadetTunnelEState
81 * Uninitialized status, should never appear in operation.
83 CADET_TUNNEL_KEY_UNINITIALIZED,
86 * Ephemeral key sent, waiting for peer's key.
88 CADET_TUNNEL_KEY_SENT,
91 * New ephemeral key and ping sent, waiting for pong.
92 * This means that we DO have the peer's ephemeral key, otherwise the
93 * state would be KEY_SENT. We DO NOT have a valid session key (either no
94 * previous key or previous key expired).
96 CADET_TUNNEL_KEY_PING,
99 * Handshake completed: session key available.
104 * New ephemeral key and ping sent, waiting for pong. Unlike KEY_PING,
105 * we still have a valid session key and therefore we *can* still send
106 * traffic on the tunnel.
108 CADET_TUNNEL_KEY_REKEY,
112 * Struct containing all information regarding a given peer
117 #include "gnunet-service-cadet_channel.h"
118 #include "gnunet-service-cadet_connection.h"
119 #include "gnunet-service-cadet_peer.h"
122 * Handle for messages queued but not yet sent.
124 struct CadetTunnelQueue;
127 * Callback called when a queued message is sent.
129 * @param cls Closure.
130 * @param t Tunnel this message was on.
131 * @param type Type of message sent.
132 * @param size Size of the message.
134 typedef void (*GCT_sent) (void *cls,
135 struct CadetTunnel *t,
136 struct CadetTunnelQueue *q,
137 uint16_t type, size_t size);
139 typedef void (*GCT_conn_iter) (void *cls, struct CadetConnection *c);
140 typedef void (*GCT_chan_iter) (void *cls, struct CadetChannel *ch);
143 /******************************************************************************/
144 /******************************** API ***********************************/
145 /******************************************************************************/
148 * Initialize tunnel subsystem.
150 * @param c Configuration handle.
151 * @param key ECC private key, to derive all other keys and do crypto.
154 GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
155 const struct GNUNET_CRYPTO_EddsaPrivateKey *key);
158 * Shut down the tunnel subsystem.
166 * @param destination Peer this tunnel is towards.
169 GCT_new (struct CadetPeer *destination);
172 * Tunnel is empty: destroy it.
174 * Notifies all connections about the destruction.
176 * @param t Tunnel to destroy.
179 GCT_destroy_empty (struct CadetTunnel *t);
182 * Destroy tunnel if empty (no more channels).
184 * @param t Tunnel to destroy if empty.
187 GCT_destroy_if_empty (struct CadetTunnel *t);
190 * Destroy the tunnel.
192 * This function does not generate any warning traffic to clients or peers.
195 * Cancel messages belonging to this tunnel queued to neighbors.
196 * Free any allocated resources linked to the tunnel.
198 * @param t The tunnel to destroy.
201 GCT_destroy (struct CadetTunnel *t);
205 * Change the tunnel's connection state.
207 * @param t Tunnel whose connection state to change.
208 * @param cstate New connection state.
211 GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate);
215 * Change the tunnel encryption state.
217 * @param t Tunnel whose encryption state to change.
218 * @param state New encryption state.
221 GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state);
224 * Add a connection to a tunnel.
227 * @param c Connection.
230 GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c);
233 * Remove a connection from a tunnel.
236 * @param c Connection.
239 GCT_remove_connection (struct CadetTunnel *t, struct CadetConnection *c);
242 * Add a channel to a tunnel.
248 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch);
251 * Remove a channel from a tunnel.
257 GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch);
260 * Search for a channel by global ID.
262 * @param t Tunnel containing the channel.
263 * @param chid Public channel number.
265 * @return channel handler, NULL if doesn't exist
267 struct CadetChannel *
268 GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid);
271 * Decrypt and demultiplex by message type. Call appropriate handler
273 * towards a channel of a local tunnel.
275 * @param t Tunnel this message came on.
276 * @param msg Message header.
279 GCT_handle_encrypted (struct CadetTunnel *t,
280 const struct GNUNET_CADET_Encrypted *msg);
283 * Demultiplex an encapsulated KX message by message type.
285 * @param t Tunnel on which the message came.
286 * @param message KX message itself.
289 GCT_handle_kx (struct CadetTunnel *t,
290 const struct GNUNET_MessageHeader *message);
293 * @brief Use the given path for the tunnel.
294 * Update the next and prev hops (and RCs).
295 * (Re)start the path refresh in case the tunnel is locally owned.
297 * @param t Tunnel to update.
298 * @param p Path to use.
300 * @return Connection created.
302 struct CadetConnection *
303 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p);
306 * Count all created connections of a tunnel. Not necessarily ready connections!
308 * @param t Tunnel on which to count.
310 * @return Number of connections created, either being established or ready.
313 GCT_count_any_connections (struct CadetTunnel *t);
316 * Count established (ready) connections of a tunnel.
318 * @param t Tunnel on which to count.
320 * @return Number of connections.
323 GCT_count_connections (struct CadetTunnel *t);
326 * Count channels of a tunnel.
328 * @param t Tunnel on which to count.
330 * @return Number of channels.
333 GCT_count_channels (struct CadetTunnel *t);
336 * Get the connectivity state of a tunnel.
340 * @return Tunnel's connectivity state.
342 enum CadetTunnelCState
343 GCT_get_cstate (struct CadetTunnel *t);
346 * Get the encryption state of a tunnel.
350 * @return Tunnel's encryption state.
352 enum CadetTunnelEState
353 GCT_get_estate (struct CadetTunnel *t);
356 * Get the maximum buffer space for a tunnel towards a local client.
360 * @return Biggest buffer space offered by any channel in the tunnel.
363 GCT_get_channels_buffer (struct CadetTunnel *t);
366 * Get the total buffer space for a tunnel for P2P traffic.
370 * @return Buffer space offered by all connections in the tunnel.
373 GCT_get_connections_buffer (struct CadetTunnel *t);
376 * Get the tunnel's destination.
380 * @return ID of the destination peer.
382 const struct GNUNET_PeerIdentity *
383 GCT_get_destination (struct CadetTunnel *t);
386 * Get the tunnel's next free Channel ID.
390 * @return ID of a channel free to use.
393 GCT_get_next_chid (struct CadetTunnel *t);
396 * Send ACK on one or more channels due to buffer in connections.
398 * @param t Channel which has some free buffer space.
401 GCT_unchoke_channels (struct CadetTunnel *t);
404 * Send ACK on one or more connections due to buffer space to the client.
406 * Iterates all connections of the tunnel and sends ACKs appropriately.
408 * @param t Tunnel which has some free buffer space.
411 GCT_send_connection_acks (struct CadetTunnel *t);
414 * Cancel a previously sent message while it's in the queue.
416 * ONLY can be called before the continuation given to the send function
417 * is called. Once the continuation is called, the message is no longer in the
420 * @param q Handle to the queue.
423 GCT_cancel (struct CadetTunnelQueue *q);
426 * Sends an already built message on a tunnel, encrypting it and
427 * choosing the best connection.
429 * @param message Message to send. Function modifies it.
430 * @param t Tunnel on which this message is transmitted.
431 * @param c Connection to use (autoselect if NULL).
432 * @param force Force the tunnel to take the message (buffer overfill).
433 * @param cont Continuation to call once message is really sent.
434 * @param cont_cls Closure for @c cont.
436 * @return Handle to cancel message. NULL if @c cont is NULL.
438 struct CadetTunnelQueue *
439 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
440 struct CadetTunnel *t, struct CadetConnection *c,
441 int force, GCT_sent cont, void *cont_cls);
444 * Sends an already built and encrypted message on a tunnel, choosing the best
445 * connection. Useful for re-queueing messages queued on a destroyed connection.
447 * @param message Message to send. Function modifies it.
448 * @param t Tunnel on which this message is transmitted.
451 GCT_resend_message (const struct GNUNET_MessageHeader *message,
452 struct CadetTunnel *t);
455 * Is the tunnel directed towards the local peer?
459 * @return #GNUNET_YES if it is loopback.
462 GCT_is_loopback (const struct CadetTunnel *t);
465 * Is the tunnel using this path already?
470 * @return #GNUNET_YES a connection uses this path.
473 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p);
476 * Get a cost of a path for a tunnel considering existing connections.
479 * @param path Candidate path.
481 * @return Cost of the path (path length + number of overlapping nodes)
484 GCT_get_path_cost (const struct CadetTunnel *t,
485 const struct CadetPeerPath *path);
488 * Get the static string for the peer this tunnel is directed.
492 * @return Static string the destination peer's ID.
495 GCT_2s (const struct CadetTunnel *t);
498 * Log all possible info about the tunnel state.
500 * @param t Tunnel to debug.
501 * @param level Debug level to use.
504 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
507 * Iterate all tunnels.
509 * @param iter Iterator.
510 * @param cls Closure for @c iter.
513 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
518 * @return Number of tunnels to remote peers kept by this peer.
521 GCT_count_all (void);
524 * Iterate all connections of a tunnel.
526 * @param t Tunnel whose connections to iterate.
527 * @param iter Iterator.
528 * @param cls Closure for @c iter.
531 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
534 * Iterate all channels of a tunnel.
536 * @param t Tunnel whose channels to iterate.
537 * @param iter Iterator.
538 * @param cls Closure for @c iter.
541 GCT_iterate_channels (struct CadetTunnel *t, GCT_chan_iter iter, void *cls);
543 #if 0 /* keep Emacsens' auto-indent happy */
550 /* ifndef GNUNET_CADET_SERVICE_TUNNEL_H */
552 /* end of gnunet-cadet-service_tunnel.h */