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. Opposite to 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 * Mark a path as no longer valid for this tunnel: has been tried and failed.
235 * @param t Tunnel to update.
236 * @param path Invalid path to remove. Is destroyed after removal.
239 GCT_remove_path (struct CadetTunnel *t, struct CadetPeerPath *path);
242 * Remove a connection from a tunnel.
245 * @param c Connection.
248 GCT_remove_connection (struct CadetTunnel *t, struct CadetConnection *c);
251 * Add a channel to a tunnel.
257 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch);
260 * Remove a channel from a tunnel.
266 GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch);
269 * Search for a channel by global ID.
271 * @param t Tunnel containing the channel.
272 * @param chid Public channel number.
274 * @return channel handler, NULL if doesn't exist
276 struct CadetChannel *
277 GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid);
280 * Decrypt and demultiplex by message type. Call appropriate handler
282 * towards a channel of a local tunnel.
284 * @param t Tunnel this message came on.
285 * @param msg Message header.
288 GCT_handle_encrypted (struct CadetTunnel *t,
289 const struct GNUNET_CADET_Encrypted *msg);
292 * Demultiplex an encapsulated KX message by message type.
294 * @param t Tunnel on which the message came.
295 * @param message KX message itself.
298 GCT_handle_kx (struct CadetTunnel *t,
299 const struct GNUNET_MessageHeader *message);
302 * @brief Use the given path for the tunnel.
303 * Update the next and prev hops (and RCs).
304 * (Re)start the path refresh in case the tunnel is locally owned.
306 * @param t Tunnel to update.
307 * @param p Path to use.
309 * @return Connection created.
311 struct CadetConnection *
312 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p);
315 * Count all created connections of a tunnel. Not necessarily ready connections!
317 * @param t Tunnel on which to count.
319 * @return Number of connections created, either being established or ready.
322 GCT_count_any_connections (struct CadetTunnel *t);
325 * Count established (ready) connections of a tunnel.
327 * @param t Tunnel on which to count.
329 * @return Number of connections.
332 GCT_count_connections (struct CadetTunnel *t);
335 * Count channels of a tunnel.
337 * @param t Tunnel on which to count.
339 * @return Number of channels.
342 GCT_count_channels (struct CadetTunnel *t);
345 * Get the connectivity state of a tunnel.
349 * @return Tunnel's connectivity state.
351 enum CadetTunnelCState
352 GCT_get_cstate (struct CadetTunnel *t);
355 * Get the encryption state of a tunnel.
359 * @return Tunnel's encryption state.
361 enum CadetTunnelEState
362 GCT_get_estate (struct CadetTunnel *t);
365 * Get the maximum buffer space for a tunnel towards a local client.
369 * @return Biggest buffer space offered by any channel in the tunnel.
372 GCT_get_channels_buffer (struct CadetTunnel *t);
375 * Get the total buffer space for a tunnel for P2P traffic.
379 * @return Buffer space offered by all connections in the tunnel.
382 GCT_get_connections_buffer (struct CadetTunnel *t);
385 * Get the tunnel's destination.
389 * @return ID of the destination peer.
391 const struct GNUNET_PeerIdentity *
392 GCT_get_destination (struct CadetTunnel *t);
395 * Get the tunnel's next free Channel ID.
399 * @return ID of a channel free to use.
402 GCT_get_next_chid (struct CadetTunnel *t);
405 * Send ACK on one or more channels due to buffer in connections.
407 * @param t Channel which has some free buffer space.
410 GCT_unchoke_channels (struct CadetTunnel *t);
413 * Send ACK on one or more connections due to buffer space to the client.
415 * Iterates all connections of the tunnel and sends ACKs appropriately.
417 * @param t Tunnel which has some free buffer space.
420 GCT_send_connection_acks (struct CadetTunnel *t);
423 * Cancel a previously sent message while it's in the queue.
425 * ONLY can be called before the continuation given to the send function
426 * is called. Once the continuation is called, the message is no longer in the
429 * @param q Handle to the queue.
432 GCT_cancel (struct CadetTunnelQueue *q);
435 * Sends an already built message on a tunnel, encrypting it and
436 * choosing the best connection.
438 * @param message Message to send. Function modifies it.
439 * @param t Tunnel on which this message is transmitted.
440 * @param c Connection to use (autoselect if NULL).
441 * @param force Force the tunnel to take the message (buffer overfill).
442 * @param cont Continuation to call once message is really sent.
443 * @param cont_cls Closure for @c cont.
445 * @return Handle to cancel message. NULL if @c cont is NULL.
447 struct CadetTunnelQueue *
448 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
449 struct CadetTunnel *t, struct CadetConnection *c,
450 int force, GCT_sent cont, void *cont_cls);
453 * Sends an already built and encrypted message on a tunnel, choosing the best
454 * connection. Useful for re-queueing messages queued on a destroyed connection.
456 * @param message Message to send. Function modifies it.
457 * @param t Tunnel on which this message is transmitted.
460 GCT_resend_message (const struct GNUNET_MessageHeader *message,
461 struct CadetTunnel *t);
464 * Is the tunnel directed towards the local peer?
468 * @return #GNUNET_YES if it is loopback.
471 GCT_is_loopback (const struct CadetTunnel *t);
474 * Is the tunnel using this path already?
479 * @return #GNUNET_YES a connection uses this path.
482 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p);
485 * Get a cost of a path for a tunnel considering existing connections.
488 * @param path Candidate path.
490 * @return Cost of the path (path length + number of overlapping nodes)
493 GCT_get_path_cost (const struct CadetTunnel *t,
494 const struct CadetPeerPath *path);
497 * Get the static string for the peer this tunnel is directed.
501 * @return Static string the destination peer's ID.
504 GCT_2s (const struct CadetTunnel *t);
507 * Log all possible info about the tunnel state.
509 * @param t Tunnel to debug.
510 * @param level Debug level to use.
513 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
516 * Iterate all tunnels.
518 * @param iter Iterator.
519 * @param cls Closure for @c iter.
522 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
527 * @return Number of tunnels to remote peers kept by this peer.
530 GCT_count_all (void);
533 * Iterate all connections of a tunnel.
535 * @param t Tunnel whose connections to iterate.
536 * @param iter Iterator.
537 * @param cls Closure for @c iter.
540 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
543 * Iterate all channels of a tunnel.
545 * @param t Tunnel whose channels to iterate.
546 * @param iter Iterator.
547 * @param cls Closure for @c iter.
550 GCT_iterate_channels (struct CadetTunnel *t, GCT_chan_iter iter, void *cls);
552 #if 0 /* keep Emacsens' auto-indent happy */
559 /* ifndef GNUNET_CADET_SERVICE_TUNNEL_H */
561 /* end of gnunet-cadet-service_tunnel.h */