2 This file is part of GNUnet.
3 Copyright (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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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"
43 #define CONNECTIONS_PER_TUNNEL 3
46 * All the connectivity states a tunnel can be in.
48 enum CadetTunnelCState
51 * Uninitialized status, should never appear in operation.
56 * No path to the peer known yet.
58 CADET_TUNNEL_SEARCHING,
61 * Request sent, not yet answered.
66 * Peer connected and ready to accept data.
71 * Tunnel being shut down, don't try to keep it alive.
78 * All the encryption states a tunnel can be in.
80 enum CadetTunnelEState
83 * Uninitialized status, should never appear in operation.
85 CADET_TUNNEL_KEY_UNINITIALIZED,
88 * Ephemeral key sent, waiting for peer's key.
90 CADET_TUNNEL_KEY_SENT,
93 * New ephemeral key and ping sent, waiting for pong.
94 * This means that we DO have the peer's ephemeral key, otherwise the
95 * state would be KEY_SENT. We DO NOT have a valid session key (either no
96 * previous key or previous key expired).
98 CADET_TUNNEL_KEY_PING,
101 * Handshake completed: session key available.
106 * New ephemeral key and ping sent, waiting for pong. Unlike KEY_PING,
107 * we still have a valid session key and therefore we *can* still send
108 * traffic on the tunnel.
110 CADET_TUNNEL_KEY_REKEY
114 * Struct containing all information regarding a given peer
119 #include "gnunet-service-cadet_channel.h"
120 #include "gnunet-service-cadet_connection.h"
121 #include "gnunet-service-cadet_peer.h"
124 * Handle for messages queued but not yet sent.
126 struct CadetTunnelQueue;
129 * Callback called when a queued message is sent.
131 * @param cls Closure.
132 * @param t Tunnel this message was on.
133 * @param type Type of message sent.
134 * @param size Size of the message.
136 typedef void (*GCT_sent) (void *cls,
137 struct CadetTunnel *t,
138 struct CadetTunnelQueue *q,
139 uint16_t type, size_t size);
141 typedef void (*GCT_conn_iter) (void *cls, struct CadetConnection *c);
142 typedef void (*GCT_chan_iter) (void *cls, struct CadetChannel *ch);
145 /******************************************************************************/
146 /******************************** API ***********************************/
147 /******************************************************************************/
150 * Initialize tunnel subsystem.
152 * @param c Configuration handle.
153 * @param key ECC private key, to derive all other keys and do crypto.
156 GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
157 const struct GNUNET_CRYPTO_EddsaPrivateKey *key);
161 * Shut down the tunnel subsystem.
170 * @param destination Peer this tunnel is towards.
173 GCT_new (struct CadetPeer *destination);
177 * Tunnel is empty: destroy it.
179 * Notifies all connections about the destruction.
181 * @param t Tunnel to destroy.
184 GCT_destroy_empty (struct CadetTunnel *t);
188 * Destroy tunnel if empty (no more channels).
190 * @param t Tunnel to destroy if empty.
193 GCT_destroy_if_empty (struct CadetTunnel *t);
197 * Destroy the tunnel.
199 * This function does not generate any warning traffic to clients or peers.
202 * Cancel messages belonging to this tunnel queued to neighbors.
203 * Free any allocated resources linked to the tunnel.
205 * @param t The tunnel to destroy.
208 GCT_destroy (struct CadetTunnel *t);
212 * Change the tunnel's connection state.
214 * @param t Tunnel whose connection state to change.
215 * @param cstate New connection state.
218 GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate);
222 * Change the tunnel encryption state.
224 * @param t Tunnel whose encryption state to change.
225 * @param state New encryption state.
228 GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state);
232 * Add a connection to a tunnel.
235 * @param c Connection.
238 GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c);
242 * Remove a connection from a tunnel.
245 * @param c Connection.
248 GCT_remove_connection (struct CadetTunnel *t, struct CadetConnection *c);
252 * Add a channel to a tunnel.
258 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch);
262 * Remove a channel from a tunnel.
268 GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch);
272 * Search for a channel by global ID.
274 * @param t Tunnel containing the channel.
275 * @param chid Public channel number.
277 * @return channel handler, NULL if doesn't exist
279 struct CadetChannel *
280 GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid);
284 * Decrypt and demultiplex by message type. Call appropriate handler
285 * for a message towards a channel of a local tunnel.
287 * @param t Tunnel this message came on.
288 * @param msg Message header.
291 GCT_handle_encrypted (struct CadetTunnel *t,
292 const struct GNUNET_MessageHeader *msg);
296 * Demultiplex an encapsulated KX message by message type.
298 * @param t Tunnel on which the message came.
299 * @param message KX message itself.
302 GCT_handle_kx (struct CadetTunnel *t,
303 const struct GNUNET_MessageHeader *message);
307 * @brief Use the given path for the tunnel.
308 * Update the next and prev hops (and RCs).
309 * (Re)start the path refresh in case the tunnel is locally owned.
311 * @param t Tunnel to update.
312 * @param p Path to use.
314 * @return Connection created.
316 struct CadetConnection *
317 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p);
321 * Count all created connections of a tunnel. Not necessarily ready connections!
323 * @param t Tunnel on which to count.
325 * @return Number of connections created, either being established or ready.
328 GCT_count_any_connections (struct CadetTunnel *t);
332 * Count established (ready) connections of a tunnel.
334 * @param t Tunnel on which to count.
336 * @return Number of connections.
339 GCT_count_connections (struct CadetTunnel *t);
343 * Count channels of a tunnel.
345 * @param t Tunnel on which to count.
347 * @return Number of channels.
350 GCT_count_channels (struct CadetTunnel *t);
354 * Get the connectivity state of a tunnel.
358 * @return Tunnel's connectivity state.
360 enum CadetTunnelCState
361 GCT_get_cstate (struct CadetTunnel *t);
365 * Get the encryption state of a tunnel.
369 * @return Tunnel's encryption state.
371 enum CadetTunnelEState
372 GCT_get_estate (struct CadetTunnel *t);
376 * Get the maximum buffer space for a tunnel towards a local client.
380 * @return Biggest buffer space offered by any channel in the tunnel.
383 GCT_get_channels_buffer (struct CadetTunnel *t);
387 * Get the total buffer space for a tunnel for P2P traffic.
391 * @return Buffer space offered by all connections in the tunnel.
394 GCT_get_connections_buffer (struct CadetTunnel *t);
398 * Get the tunnel's destination.
402 * @return ID of the destination peer.
404 const struct GNUNET_PeerIdentity *
405 GCT_get_destination (struct CadetTunnel *t);
409 * Get the tunnel's next free Channel ID.
413 * @return ID of a channel free to use.
416 GCT_get_next_chid (struct CadetTunnel *t);
420 * Send ACK on one or more channels due to buffer in connections.
422 * @param t Channel which has some free buffer space.
425 GCT_unchoke_channels (struct CadetTunnel *t);
429 * Send ACK on one or more connections due to buffer space to the client.
431 * Iterates all connections of the tunnel and sends ACKs appropriately.
433 * @param t Tunnel which has some free buffer space.
436 GCT_send_connection_acks (struct CadetTunnel *t);
440 * Cancel a previously sent message while it's in the queue.
442 * ONLY can be called before the continuation given to the send function
443 * is called. Once the continuation is called, the message is no longer in the
446 * @param q Handle to the queue.
449 GCT_cancel (struct CadetTunnelQueue *q);
453 * Sends an already built message on a tunnel, encrypting it and
454 * choosing the best connection.
456 * @param message Message to send. Function modifies it.
457 * @param t Tunnel on which this message is transmitted.
458 * @param c Connection to use (autoselect if NULL).
459 * @param force Force the tunnel to take the message (buffer overfill).
460 * @param cont Continuation to call once message is really sent.
461 * @param cont_cls Closure for @c cont.
463 * @return Handle to cancel message. NULL if @c cont is NULL.
465 struct CadetTunnelQueue *
466 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
467 struct CadetTunnel *t, struct CadetConnection *c,
468 int force, GCT_sent cont, void *cont_cls);
472 * Send an Axolotl KX message.
474 * @param t Tunnel on which to send it.
475 * @param force_reply Force the other peer to reply with a KX message.
478 GCT_send_ax_kx (struct CadetTunnel *t, int force_reply);
482 * Sends an already built and encrypted message on a tunnel, choosing the best
483 * connection. Useful for re-queueing messages queued on a destroyed connection.
485 * @param message Message to send. Function modifies it.
486 * @param t Tunnel on which this message is transmitted.
489 GCT_resend_message (const struct GNUNET_MessageHeader *message,
490 struct CadetTunnel *t);
494 * Is the tunnel directed towards the local peer?
498 * @return #GNUNET_YES if it is loopback.
501 GCT_is_loopback (const struct CadetTunnel *t);
505 * Is the tunnel using this path already?
510 * @return #GNUNET_YES a connection uses this path.
513 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p);
517 * Get a cost of a path for a tunnel considering existing connections.
520 * @param path Candidate path.
522 * @return Cost of the path (path length + number of overlapping nodes)
525 GCT_get_path_cost (const struct CadetTunnel *t,
526 const struct CadetPeerPath *path);
530 * Get the static string for the peer this tunnel is directed.
534 * @return Static string the destination peer's ID.
537 GCT_2s (const struct CadetTunnel *t);
541 * Log all possible info about the tunnel state.
543 * @param t Tunnel to debug.
544 * @param level Debug level to use.
547 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
551 * Iterate all tunnels.
553 * @param iter Iterator.
554 * @param cls Closure for @c iter.
557 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
563 * @return Number of tunnels to remote peers kept by this peer.
566 GCT_count_all (void);
570 * Iterate all connections of a tunnel.
572 * @param t Tunnel whose connections to iterate.
573 * @param iter Iterator.
574 * @param cls Closure for @c iter.
577 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
581 * Iterate all channels of a tunnel.
583 * @param t Tunnel whose channels to iterate.
584 * @param iter Iterator.
585 * @param cls Closure for @c iter.
588 GCT_iterate_channels (struct CadetTunnel *t,
593 #if 0 /* keep Emacsens' auto-indent happy */
600 /* ifndef GNUNET_CADET_SERVICE_TUNNEL_H */
602 /* end of gnunet-cadet-service_tunnel.h */