2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
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.
146 (*GCT_sent) (void *cls,
147 struct CadetTunnel *t,
148 struct CadetTunnelQueue *q,
149 uint16_t type, size_t size);
152 (*GCT_conn_iter) (void *cls, struct CadetConnection *c);
156 (*GCT_chan_iter) (void *cls, struct CadetChannel *ch);
159 /******************************************************************************/
160 /******************************** API ***********************************/
161 /******************************************************************************/
164 * Initialize tunnel subsystem.
166 * @param c Configuration handle.
167 * @param key ECC private key, to derive all other keys and do crypto.
170 GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
171 const struct GNUNET_CRYPTO_EddsaPrivateKey *key);
175 * Shut down the tunnel subsystem.
184 * @param destination Peer this tunnel is towards.
187 GCT_new (struct CadetPeer *destination);
191 * Tunnel is empty: destroy it.
193 * Notifies all connections about the destruction.
195 * @param t Tunnel to destroy.
198 GCT_destroy_empty (struct CadetTunnel *t);
202 * Destroy tunnel if empty (no more channels).
204 * @param t Tunnel to destroy if empty.
207 GCT_destroy_if_empty (struct CadetTunnel *t);
211 * Destroy the tunnel.
213 * This function does not generate any warning traffic to clients or peers.
216 * Cancel messages belonging to this tunnel queued to neighbors.
217 * Free any allocated resources linked to the tunnel.
219 * @param t The tunnel to destroy.
222 GCT_destroy (struct CadetTunnel *t);
226 * Change the tunnel's connection state.
228 * @param t Tunnel whose connection state to change.
229 * @param cstate New connection state.
232 GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate);
236 * Change the tunnel encryption state.
238 * @param t Tunnel whose encryption state to change.
239 * @param state New encryption state.
242 GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state);
246 * Add a connection to a tunnel.
249 * @param c Connection.
252 GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c);
256 * Remove a connection from a tunnel.
259 * @param c Connection.
262 GCT_remove_connection (struct CadetTunnel *t, struct CadetConnection *c);
266 * Add a channel to a tunnel.
272 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch);
276 * Remove a channel from a tunnel.
282 GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch);
286 * Search for a channel by global ID.
288 * @param t Tunnel containing the channel.
289 * @param chid Public channel number.
291 * @return channel handler, NULL if doesn't exist
293 struct CadetChannel *
294 GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid);
298 * Decrypt and demultiplex by message type. Call appropriate handler
299 * for a message towards a channel of a local tunnel.
301 * @param t Tunnel this message came on.
302 * @param msg Message header.
305 GCT_handle_encrypted (struct CadetTunnel *t,
306 const struct GNUNET_MessageHeader *msg);
310 * Demultiplex an encapsulated KX message by message type.
312 * @param t Tunnel on which the message came.
313 * @param message KX message itself.
316 GCT_handle_kx (struct CadetTunnel *t,
317 const struct GNUNET_MessageHeader *message);
321 * @brief Use the given path for the tunnel.
322 * Update the next and prev hops (and RCs).
323 * (Re)start the path refresh in case the tunnel is locally owned.
325 * @param t Tunnel to update.
326 * @param p Path to use.
328 * @return Connection created.
330 struct CadetConnection *
331 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p);
335 * Count all created connections of a tunnel. Not necessarily ready connections!
337 * @param t Tunnel on which to count.
339 * @return Number of connections created, either being established or ready.
342 GCT_count_any_connections (struct CadetTunnel *t);
346 * Count established (ready) connections of a tunnel.
348 * @param t Tunnel on which to count.
350 * @return Number of connections.
353 GCT_count_connections (struct CadetTunnel *t);
357 * Count channels of a tunnel.
359 * @param t Tunnel on which to count.
361 * @return Number of channels.
364 GCT_count_channels (struct CadetTunnel *t);
368 * Get the connectivity state of a tunnel.
372 * @return Tunnel's connectivity state.
374 enum CadetTunnelCState
375 GCT_get_cstate (struct CadetTunnel *t);
379 * Get the encryption state of a tunnel.
383 * @return Tunnel's encryption state.
385 enum CadetTunnelEState
386 GCT_get_estate (struct CadetTunnel *t);
390 * Get the maximum buffer space for a tunnel towards a local client.
394 * @return Biggest buffer space offered by any channel in the tunnel.
397 GCT_get_channels_buffer (struct CadetTunnel *t);
401 * Get the total buffer space for a tunnel for P2P traffic.
405 * @return Buffer space offered by all connections in the tunnel.
408 GCT_get_connections_buffer (struct CadetTunnel *t);
412 * Get the tunnel's destination.
416 * @return ID of the destination peer.
418 const struct GNUNET_PeerIdentity *
419 GCT_get_destination (struct CadetTunnel *t);
423 * Get the tunnel's next free Channel ID.
427 * @return ID of a channel free to use.
430 GCT_get_next_chid (struct CadetTunnel *t);
434 * Send ACK on one or more channels due to buffer in connections.
436 * @param t Channel which has some free buffer space.
439 GCT_unchoke_channels (struct CadetTunnel *t);
443 * Send ACK on one or more connections due to buffer space to the client.
445 * Iterates all connections of the tunnel and sends ACKs appropriately.
447 * @param t Tunnel which has some free buffer space.
450 GCT_send_connection_acks (struct CadetTunnel *t);
454 * Cancel a previously sent message while it's in the queue.
456 * ONLY can be called before the continuation given to the send function
457 * is called. Once the continuation is called, the message is no longer in the
460 * @param q Handle to the queue.
463 GCT_cancel (struct CadetTunnelQueue *q);
467 * Check if the tunnel has queued traffic.
469 * @param t Tunnel to check.
471 * @return #GNUNET_YES if there is queued traffic
472 * #GNUNET_NO otherwise
475 GCT_has_queued_traffic (struct CadetTunnel *t);
478 * Sends an already built message on a tunnel, encrypting it and
479 * choosing the best connection.
481 * @param message Message to send. Function modifies it.
482 * @param t Tunnel on which this message is transmitted.
483 * @param c Connection to use (autoselect if NULL).
484 * @param force Force the tunnel to take the message (buffer overfill).
485 * @param cont Continuation to call once message is really sent.
486 * @param cont_cls Closure for @c cont.
488 * @return Handle to cancel message. NULL if @c cont is NULL.
490 struct CadetTunnelQueue *
491 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
492 struct CadetTunnel *t, struct CadetConnection *c,
493 int force, GCT_sent cont, void *cont_cls);
497 * Send an Axolotl KX message.
499 * @param t Tunnel on which to send it.
500 * @param force_reply Force the other peer to reply with a KX message.
503 GCT_send_ax_kx (struct CadetTunnel *t, int force_reply);
507 * Is the tunnel directed towards the local peer?
511 * @return #GNUNET_YES if it is loopback.
514 GCT_is_loopback (const struct CadetTunnel *t);
518 * Is the tunnel using this path already?
523 * @return #GNUNET_YES a connection uses this path.
526 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p);
530 * Get a cost of a path for a tunnel considering existing connections.
533 * @param path Candidate path.
535 * @return Cost of the path (path length + number of overlapping nodes)
538 GCT_get_path_cost (const struct CadetTunnel *t,
539 const struct CadetPeerPath *path);
543 * Get the static string for the peer this tunnel is directed.
547 * @return Static string the destination peer's ID.
550 GCT_2s (const struct CadetTunnel *t);
554 * Log all possible info about the tunnel state.
556 * @param t Tunnel to debug.
557 * @param level Debug level to use.
560 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
564 * Iterate all tunnels.
566 * @param iter Iterator.
567 * @param cls Closure for @c iter.
570 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
576 * @return Number of tunnels to remote peers kept by this peer.
579 GCT_count_all (void);
583 * Iterate all connections of a tunnel.
585 * @param t Tunnel whose connections to iterate.
586 * @param iter Iterator.
587 * @param cls Closure for @c iter.
590 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
594 * Iterate all channels of a tunnel.
596 * @param t Tunnel whose channels to iterate.
597 * @param iter Iterator.
598 * @param cls Closure for @c iter.
601 GCT_iterate_channels (struct CadetTunnel *t,
606 #if 0 /* keep Emacsens' auto-indent happy */
613 /* ifndef GNUNET_CADET_SERVICE_TUNNEL_H */
615 /* end of gnunet-cadet-service_tunnel.h */