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, struct GNUNET_CADET_ChannelNumber chid);
298 * Decrypt and process an encrypted message.
300 * Calls the appropriate handler for a message in a channel of a local tunnel.
302 * @param t Tunnel this message came on.
303 * @param msg Message header.
306 GCT_handle_encrypted (struct CadetTunnel *t,
307 const struct GNUNET_CADET_Encrypted *msg);
311 * Handle a Key eXchange message.
313 * @param t Tunnel on which the message came.
314 * @param msg KX message itself.
317 GCT_handle_kx (struct CadetTunnel *t,
318 const struct GNUNET_CADET_KX *msg);
322 * @brief Use the given path for the tunnel.
323 * Update the next and prev hops (and RCs).
324 * (Re)start the path refresh in case the tunnel is locally owned.
326 * @param t Tunnel to update.
327 * @param p Path to use.
329 * @return Connection created.
331 struct CadetConnection *
332 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p);
336 * Count all created connections of a tunnel. Not necessarily ready connections!
338 * @param t Tunnel on which to count.
340 * @return Number of connections created, either being established or ready.
343 GCT_count_any_connections (struct CadetTunnel *t);
347 * Count established (ready) connections of a tunnel.
349 * @param t Tunnel on which to count.
351 * @return Number of connections.
354 GCT_count_connections (struct CadetTunnel *t);
358 * Count channels of a tunnel.
360 * @param t Tunnel on which to count.
362 * @return Number of channels.
365 GCT_count_channels (struct CadetTunnel *t);
369 * Get the connectivity state of a tunnel.
373 * @return Tunnel's connectivity state.
375 enum CadetTunnelCState
376 GCT_get_cstate (struct CadetTunnel *t);
380 * Get the encryption state of a tunnel.
384 * @return Tunnel's encryption state.
386 enum CadetTunnelEState
387 GCT_get_estate (struct CadetTunnel *t);
391 * Get the maximum buffer space for a tunnel towards a local client.
395 * @return Biggest buffer space offered by any channel in the tunnel.
398 GCT_get_channels_buffer (struct CadetTunnel *t);
402 * Get the total buffer space for a tunnel for P2P traffic.
406 * @return Buffer space offered by all connections in the tunnel.
409 GCT_get_connections_buffer (struct CadetTunnel *t);
413 * Get the tunnel's destination.
417 * @return ID of the destination peer.
419 const struct GNUNET_PeerIdentity *
420 GCT_get_destination (struct CadetTunnel *t);
424 * Get the tunnel's next free Channel ID.
428 * @return ID of a channel free to use.
430 struct GNUNET_CADET_ChannelNumber
431 GCT_get_next_chid (struct CadetTunnel *t);
435 * Send ACK on one or more channels due to buffer in connections.
437 * @param t Channel which has some free buffer space.
440 GCT_unchoke_channels (struct CadetTunnel *t);
444 * Send ACK on one or more connections due to buffer space to the client.
446 * Iterates all connections of the tunnel and sends ACKs appropriately.
448 * @param t Tunnel which has some free buffer space.
451 GCT_send_connection_acks (struct CadetTunnel *t);
455 * Cancel a previously sent message while it's in the queue.
457 * ONLY can be called before the continuation given to the send function
458 * is called. Once the continuation is called, the message is no longer in the
461 * @param q Handle to the queue.
464 GCT_cancel (struct CadetTunnelQueue *q);
468 * Check if the tunnel has queued traffic.
470 * @param t Tunnel to check.
472 * @return #GNUNET_YES if there is queued traffic
473 * #GNUNET_NO otherwise
476 GCT_has_queued_traffic (struct CadetTunnel *t);
479 * Sends an already built message on a tunnel, encrypting it and
480 * choosing the best connection.
482 * @param message Message to send. Function modifies it.
483 * @param t Tunnel on which this message is transmitted.
484 * @param c Connection to use (autoselect if NULL).
485 * @param force Force the tunnel to take the message (buffer overfill).
486 * @param cont Continuation to call once message is really sent.
487 * @param cont_cls Closure for @c cont.
489 * @return Handle to cancel message. NULL if @c cont is NULL.
491 struct CadetTunnelQueue *
492 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
493 struct CadetTunnel *t, struct CadetConnection *c,
494 int force, GCT_sent cont, void *cont_cls);
500 * @param t Tunnel on which to send it.
501 * @param force_reply Force the other peer to reply with a KX message.
504 GCT_send_kx (struct CadetTunnel *t, int force_reply);
508 * Is the tunnel directed towards the local peer?
512 * @return #GNUNET_YES if it is loopback.
515 GCT_is_loopback (const struct CadetTunnel *t);
519 * Is the tunnel using this path already?
524 * @return #GNUNET_YES a connection uses this path.
527 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p);
531 * Get a cost of a path for a tunnel considering existing connections.
534 * @param path Candidate path.
536 * @return Cost of the path (path length + number of overlapping nodes)
539 GCT_get_path_cost (const struct CadetTunnel *t,
540 const struct CadetPeerPath *path);
544 * Get the static string for the peer this tunnel is directed.
548 * @return Static string the destination peer's ID.
551 GCT_2s (const struct CadetTunnel *t);
555 * Log all possible info about the tunnel state.
557 * @param t Tunnel to debug.
558 * @param level Debug level to use.
561 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
565 * Iterate all tunnels.
567 * @param iter Iterator.
568 * @param cls Closure for @c iter.
571 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
577 * @return Number of tunnels to remote peers kept by this peer.
580 GCT_count_all (void);
584 * Iterate all connections of a tunnel.
586 * @param t Tunnel whose connections to iterate.
587 * @param iter Iterator.
588 * @param cls Closure for @c iter.
591 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
595 * Iterate all channels of a tunnel.
597 * @param t Tunnel whose channels to iterate.
598 * @param iter Iterator.
599 * @param cls Closure for @c iter.
602 GCT_iterate_channels (struct CadetTunnel *t,
607 #if 0 /* keep Emacsens' auto-indent happy */
614 /* ifndef GNUNET_CADET_SERVICE_TUNNEL_H */
616 /* end of gnunet-cadet-service_tunnel.h */