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 * In OTR: New ephemeral key and ping sent, waiting for pong.
95 * This means that we DO have the peer's ephemeral key, otherwise the
96 * state would be KEY_SENT. We DO NOT have a valid session key (either no
97 * previous key or previous key expired).
100 * In Axolotl: Key sent and received but no deciphered traffic yet.
102 * This means that we can send traffic (otherwise we would never complete
103 * the handshake), but we don't have complete confirmation. Since the first
104 * traffic MUST be a complete channel creation 3-way handshake, no payload
105 * will be sent before confirmation.
107 CADET_TUNNEL_KEY_PING,
110 * Handshake completed: session key available.
115 * New ephemeral key and ping sent, waiting for pong. Unlike KEY_PING,
116 * we still have a valid session key and therefore we *can* still send
117 * traffic on the tunnel.
119 CADET_TUNNEL_KEY_REKEY
123 * Struct containing all information regarding a given peer
128 #include "gnunet-service-cadet_channel.h"
129 #include "gnunet-service-cadet_connection.h"
130 #include "gnunet-service-cadet_peer.h"
133 * Handle for messages queued but not yet sent.
135 struct CadetTunnelQueue;
138 * Callback called when a queued message is sent.
140 * @param cls Closure.
141 * @param t Tunnel this message was on.
142 * @param type Type of message sent.
143 * @param size Size of the message.
145 typedef void (*GCT_sent) (void *cls,
146 struct CadetTunnel *t,
147 struct CadetTunnelQueue *q,
148 uint16_t type, size_t size);
150 typedef void (*GCT_conn_iter) (void *cls, struct CadetConnection *c);
151 typedef void (*GCT_chan_iter) (void *cls, struct CadetChannel *ch);
154 /******************************************************************************/
155 /******************************** API ***********************************/
156 /******************************************************************************/
159 * Initialize tunnel subsystem.
161 * @param c Configuration handle.
162 * @param key ECC private key, to derive all other keys and do crypto.
165 GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
166 const struct GNUNET_CRYPTO_EddsaPrivateKey *key);
170 * Shut down the tunnel subsystem.
179 * @param destination Peer this tunnel is towards.
182 GCT_new (struct CadetPeer *destination);
186 * Tunnel is empty: destroy it.
188 * Notifies all connections about the destruction.
190 * @param t Tunnel to destroy.
193 GCT_destroy_empty (struct CadetTunnel *t);
197 * Destroy tunnel if empty (no more channels).
199 * @param t Tunnel to destroy if empty.
202 GCT_destroy_if_empty (struct CadetTunnel *t);
206 * Destroy the tunnel.
208 * This function does not generate any warning traffic to clients or peers.
211 * Cancel messages belonging to this tunnel queued to neighbors.
212 * Free any allocated resources linked to the tunnel.
214 * @param t The tunnel to destroy.
217 GCT_destroy (struct CadetTunnel *t);
221 * Change the tunnel's connection state.
223 * @param t Tunnel whose connection state to change.
224 * @param cstate New connection state.
227 GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate);
231 * Change the tunnel encryption state.
233 * @param t Tunnel whose encryption state to change.
234 * @param state New encryption state.
237 GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state);
241 * Add a connection to a tunnel.
244 * @param c Connection.
247 GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c);
251 * Remove a connection from a tunnel.
254 * @param c Connection.
257 GCT_remove_connection (struct CadetTunnel *t, struct CadetConnection *c);
261 * Add a channel to a tunnel.
267 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch);
271 * Remove a channel from a tunnel.
277 GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch);
281 * Search for a channel by global ID.
283 * @param t Tunnel containing the channel.
284 * @param chid Public channel number.
286 * @return channel handler, NULL if doesn't exist
288 struct CadetChannel *
289 GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid);
293 * Decrypt and demultiplex by message type. Call appropriate handler
294 * for a message towards a channel of a local tunnel.
296 * @param t Tunnel this message came on.
297 * @param msg Message header.
300 GCT_handle_encrypted (struct CadetTunnel *t,
301 const struct GNUNET_MessageHeader *msg);
305 * Demultiplex an encapsulated KX message by message type.
307 * @param t Tunnel on which the message came.
308 * @param message KX message itself.
311 GCT_handle_kx (struct CadetTunnel *t,
312 const struct GNUNET_MessageHeader *message);
316 * @brief Use the given path for the tunnel.
317 * Update the next and prev hops (and RCs).
318 * (Re)start the path refresh in case the tunnel is locally owned.
320 * @param t Tunnel to update.
321 * @param p Path to use.
323 * @return Connection created.
325 struct CadetConnection *
326 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p);
330 * Count all created connections of a tunnel. Not necessarily ready connections!
332 * @param t Tunnel on which to count.
334 * @return Number of connections created, either being established or ready.
337 GCT_count_any_connections (struct CadetTunnel *t);
341 * Count established (ready) connections of a tunnel.
343 * @param t Tunnel on which to count.
345 * @return Number of connections.
348 GCT_count_connections (struct CadetTunnel *t);
352 * Count channels of a tunnel.
354 * @param t Tunnel on which to count.
356 * @return Number of channels.
359 GCT_count_channels (struct CadetTunnel *t);
363 * Get the connectivity state of a tunnel.
367 * @return Tunnel's connectivity state.
369 enum CadetTunnelCState
370 GCT_get_cstate (struct CadetTunnel *t);
374 * Get the encryption state of a tunnel.
378 * @return Tunnel's encryption state.
380 enum CadetTunnelEState
381 GCT_get_estate (struct CadetTunnel *t);
385 * Get the maximum buffer space for a tunnel towards a local client.
389 * @return Biggest buffer space offered by any channel in the tunnel.
392 GCT_get_channels_buffer (struct CadetTunnel *t);
396 * Get the total buffer space for a tunnel for P2P traffic.
400 * @return Buffer space offered by all connections in the tunnel.
403 GCT_get_connections_buffer (struct CadetTunnel *t);
407 * Get the tunnel's destination.
411 * @return ID of the destination peer.
413 const struct GNUNET_PeerIdentity *
414 GCT_get_destination (struct CadetTunnel *t);
418 * Get the tunnel's next free Channel ID.
422 * @return ID of a channel free to use.
425 GCT_get_next_chid (struct CadetTunnel *t);
429 * Send ACK on one or more channels due to buffer in connections.
431 * @param t Channel which has some free buffer space.
434 GCT_unchoke_channels (struct CadetTunnel *t);
438 * Send ACK on one or more connections due to buffer space to the client.
440 * Iterates all connections of the tunnel and sends ACKs appropriately.
442 * @param t Tunnel which has some free buffer space.
445 GCT_send_connection_acks (struct CadetTunnel *t);
449 * Cancel a previously sent message while it's in the queue.
451 * ONLY can be called before the continuation given to the send function
452 * is called. Once the continuation is called, the message is no longer in the
455 * @param q Handle to the queue.
458 GCT_cancel (struct CadetTunnelQueue *q);
462 * Check if the tunnel has queued traffic.
464 * @param t Tunnel to check.
466 * @return #GNUNET_YES if there is queued traffic
467 * #GNUNET_NO otherwise
470 GCT_has_queued_traffic (struct CadetTunnel *t);
473 * Sends an already built message on a tunnel, encrypting it and
474 * choosing the best connection.
476 * @param message Message to send. Function modifies it.
477 * @param t Tunnel on which this message is transmitted.
478 * @param c Connection to use (autoselect if NULL).
479 * @param force Force the tunnel to take the message (buffer overfill).
480 * @param cont Continuation to call once message is really sent.
481 * @param cont_cls Closure for @c cont.
483 * @return Handle to cancel message. NULL if @c cont is NULL.
485 struct CadetTunnelQueue *
486 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
487 struct CadetTunnel *t, struct CadetConnection *c,
488 int force, GCT_sent cont, void *cont_cls);
492 * Send an Axolotl KX message.
494 * @param t Tunnel on which to send it.
495 * @param force_reply Force the other peer to reply with a KX message.
498 GCT_send_ax_kx (struct CadetTunnel *t, int force_reply);
502 * Sends an already built and encrypted message on a tunnel, choosing the best
503 * connection. Useful for re-queueing messages queued on a destroyed connection.
505 * @param message Message to send. Function modifies it.
506 * @param t Tunnel on which this message is transmitted.
509 GCT_resend_message (const struct GNUNET_MessageHeader *message,
510 struct CadetTunnel *t);
514 * Is the tunnel directed towards the local peer?
518 * @return #GNUNET_YES if it is loopback.
521 GCT_is_loopback (const struct CadetTunnel *t);
525 * Is the tunnel using this path already?
530 * @return #GNUNET_YES a connection uses this path.
533 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p);
537 * Get a cost of a path for a tunnel considering existing connections.
540 * @param path Candidate path.
542 * @return Cost of the path (path length + number of overlapping nodes)
545 GCT_get_path_cost (const struct CadetTunnel *t,
546 const struct CadetPeerPath *path);
550 * Get the static string for the peer this tunnel is directed.
554 * @return Static string the destination peer's ID.
557 GCT_2s (const struct CadetTunnel *t);
561 * Log all possible info about the tunnel state.
563 * @param t Tunnel to debug.
564 * @param level Debug level to use.
567 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
571 * Iterate all tunnels.
573 * @param iter Iterator.
574 * @param cls Closure for @c iter.
577 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
583 * @return Number of tunnels to remote peers kept by this peer.
586 GCT_count_all (void);
590 * Iterate all connections of a tunnel.
592 * @param t Tunnel whose connections to iterate.
593 * @param iter Iterator.
594 * @param cls Closure for @c iter.
597 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
601 * Iterate all channels of a tunnel.
603 * @param t Tunnel whose channels to iterate.
604 * @param iter Iterator.
605 * @param cls Closure for @c iter.
608 GCT_iterate_channels (struct CadetTunnel *t,
613 #if 0 /* keep Emacsens' auto-indent happy */
620 /* ifndef GNUNET_CADET_SERVICE_TUNNEL_H */
622 /* end of gnunet-cadet-service_tunnel.h */