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 mesh/gnunet-service-mesh_tunnel.h
23 * @brief mesh service; dealing with tunnels and crypto
24 * @author Bartlomiej Polot
26 * All functions in this file should use the prefix GMT (Gnunet Mesh Tunnel)
29 #ifndef GNUNET_SERVICE_MESH_TUNNEL_H
30 #define GNUNET_SERVICE_MESH_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 MeshTunnel3CState
49 * Uninitialized status, should never appear in operation.
54 * Path to the peer not known yet.
56 MESH_TUNNEL3_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 MeshTunnel3EState
81 * Uninitialized status, should never appear in operation.
83 MESH_TUNNEL3_KEY_UNINITIALIZED,
86 * Ephemeral key sent, waiting for peer's key.
88 MESH_TUNNEL3_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.
95 MESH_TUNNEL3_KEY_PING,
98 * Handshake completed: session key available.
104 * Struct containing all information regarding a given peer
109 #include "gnunet-service-mesh_channel.h"
110 #include "gnunet-service-mesh_connection.h"
111 #include "gnunet-service-mesh_peer.h"
114 * Handle for messages queued but not yet sent.
116 struct MeshTunnel3Queue;
119 * Callback called when a queued message is sent.
121 * @param cls Closure.
122 * @param t Tunnel this message was on.
123 * @param type Type of message sent.
124 * @param size Size of the message.
126 typedef void (*GMT_sent) (void *cls,
127 struct MeshTunnel3 *t,
128 struct MeshTunnel3Queue *q,
129 uint16_t type, size_t size);
131 typedef void (*GMT_conn_iter) (void *cls, struct MeshConnection *c);
132 typedef void (*GMT_chan_iter) (void *cls, struct MeshChannel *ch);
135 /******************************************************************************/
136 /******************************** API ***********************************/
137 /******************************************************************************/
140 * Initialize tunnel subsystem.
142 * @param c Configuration handle.
143 * @param key ECC private key, to derive all other keys and do crypto.
146 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
147 const struct GNUNET_CRYPTO_EddsaPrivateKey *key);
150 * Shut down the tunnel subsystem.
158 * @param destination Peer this tunnel is towards.
161 GMT_new (struct MeshPeer *destination);
164 * Tunnel is empty: destroy it.
166 * Notifies all connections about the destruction.
168 * @param t Tunnel to destroy.
171 GMT_destroy_empty (struct MeshTunnel3 *t);
174 * Destroy tunnel if empty (no more channels).
176 * @param t Tunnel to destroy if empty.
179 GMT_destroy_if_empty (struct MeshTunnel3 *t);
182 * Destroy the tunnel.
184 * This function does not generate any warning traffic to clients or peers.
187 * Cancel messages belonging to this tunnel queued to neighbors.
188 * Free any allocated resources linked to the tunnel.
190 * @param t The tunnel to destroy.
193 GMT_destroy (struct MeshTunnel3 *t);
197 * Change the tunnel's connection state.
199 * @param t Tunnel whose connection state to change.
200 * @param cstate New connection state.
203 GMT_change_cstate (struct MeshTunnel3* t, enum MeshTunnel3CState cstate);
207 * Change the tunnel encryption state.
209 * @param t Tunnel whose encryption state to change.
210 * @param state New encryption state.
213 GMT_change_estate (struct MeshTunnel3* t, enum MeshTunnel3EState state);
216 * Add a connection to a tunnel.
219 * @param c Connection.
222 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c);
225 * Mark a path as no longer valid for this tunnel: has been tried and failed.
227 * @param t Tunnel to update.
228 * @param path Invalid path to remove. Is destroyed after removal.
231 GMT_remove_path (struct MeshTunnel3 *t, struct MeshPeerPath *path);
234 * Remove a connection from a tunnel.
237 * @param c Connection.
240 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c);
243 * Add a channel to a tunnel.
249 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch);
252 * Remove a channel from a tunnel.
258 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch);
261 * Search for a channel by global ID.
263 * @param t Tunnel containing the channel.
264 * @param chid Public channel number.
266 * @return channel handler, NULL if doesn't exist
269 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid);
272 * Decrypt and demultiplex by message type. Call appropriate handler
274 * towards a channel of a local tunnel.
276 * @param t Tunnel this message came on.
277 * @param msg Message header.
280 GMT_handle_encrypted (struct MeshTunnel3 *t,
281 const struct GNUNET_MESH_Encrypted *msg);
284 * Demultiplex an encapsulated KX message by message type.
286 * @param t Tunnel on which the message came.
287 * @param message KX message itself.
290 GMT_handle_kx (struct MeshTunnel3 *t,
291 const struct GNUNET_MessageHeader *message);
294 * @brief Use the given path for the tunnel.
295 * Update the next and prev hops (and RCs).
296 * (Re)start the path refresh in case the tunnel is locally owned.
298 * @param t Tunnel to update.
299 * @param p Path to use.
301 * @return Connection created.
303 struct MeshConnection *
304 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p);
307 * Count established (ready) connections of a tunnel.
309 * @param t Tunnel on which to count.
311 * @return Number of connections.
314 GMT_count_connections (struct MeshTunnel3 *t);
317 * Count channels of a tunnel.
319 * @param t Tunnel on which to count.
321 * @return Number of channels.
324 GMT_count_channels (struct MeshTunnel3 *t);
327 * Get the connectivity state of a tunnel.
331 * @return Tunnel's connectivity state.
333 enum MeshTunnel3CState
334 GMT_get_cstate (struct MeshTunnel3 *t);
337 * Get the encryption state of a tunnel.
341 * @return Tunnel's encryption state.
343 enum MeshTunnel3EState
344 GMT_get_estate (struct MeshTunnel3 *t);
347 * Get the maximum buffer space for a tunnel towards a local client.
351 * @return Biggest buffer space offered by any channel in the tunnel.
354 GMT_get_channels_buffer (struct MeshTunnel3 *t);
357 * Get the total buffer space for a tunnel for P2P traffic.
361 * @return Buffer space offered by all connections in the tunnel.
364 GMT_get_connections_buffer (struct MeshTunnel3 *t);
367 * Get the tunnel's destination.
371 * @return ID of the destination peer.
373 const struct GNUNET_PeerIdentity *
374 GMT_get_destination (struct MeshTunnel3 *t);
377 * Get the tunnel's next free Channel ID.
381 * @return ID of a channel free to use.
384 GMT_get_next_chid (struct MeshTunnel3 *t);
387 * Send ACK on one or more channels due to buffer in connections.
389 * @param t Channel which has some free buffer space.
392 GMT_unchoke_channels (struct MeshTunnel3 *t);
395 * Send ACK on one or more connections due to buffer space to the client.
397 * Iterates all connections of the tunnel and sends ACKs appropriately.
399 * @param t Tunnel which has some free buffer space.
402 GMT_send_connection_acks (struct MeshTunnel3 *t);
405 * Cancel a previously sent message while it's in the queue.
407 * ONLY can be called before the continuation given to the send function
408 * is called. Once the continuation is called, the message is no longer in the
411 * @param q Handle to the queue.
414 GMT_cancel (struct MeshTunnel3Queue *q);
417 * Sends an already built message on a tunnel, encrypting it and
418 * choosing the best connection.
420 * @param message Message to send. Function modifies it.
421 * @param t Tunnel on which this message is transmitted.
422 * @param c Connection to use (autoselect if NULL).
423 * @param force Force the tunnel to take the message (buffer overfill).
424 * @param cont Continuation to call once message is really sent.
425 * @param cont_cls Closure for @c cont.
427 * @return Handle to cancel message. NULL if @c cont is NULL.
429 struct MeshTunnel3Queue *
430 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
431 struct MeshTunnel3 *t, struct MeshConnection *c,
432 int force, GMT_sent cont, void *cont_cls);
435 * Is the tunnel directed towards the local peer?
439 * @return #GNUNET_YES if it is loopback.
442 GMT_is_loopback (const struct MeshTunnel3 *t);
445 * Is the tunnel using this path already?
450 * @return #GNUNET_YES a connection uses this path.
453 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p);
456 * Get a cost of a path for a tunnel considering existing connections.
459 * @param path Candidate path.
461 * @return Cost of the path (path length + number of overlapping nodes)
464 GMT_get_path_cost (const struct MeshTunnel3 *t,
465 const struct MeshPeerPath *path);
468 * Get the static string for the peer this tunnel is directed.
472 * @return Static string the destination peer's ID.
475 GMT_2s (const struct MeshTunnel3 *t);
478 * Log all possible info about the tunnel state.
480 * @param t Tunnel to debug.
483 GMT_debug (const struct MeshTunnel3 *t);
486 * Iterate all tunnels.
488 * @param iter Iterator.
489 * @param cls Closure for @c iter.
492 GMT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
497 * @return Number of tunnels to remote peers kept by this peer.
500 GMT_count_all (void);
503 * Iterate all connections of a tunnel.
505 * @param t Tunnel whose connections to iterate.
506 * @param iter Iterator.
507 * @param cls Closure for @c iter.
510 GMT_iterate_connections (struct MeshTunnel3 *t, GMT_conn_iter iter, void *cls);
513 * Iterate all channels of a tunnel.
515 * @param t Tunnel whose channels to iterate.
516 * @param iter Iterator.
517 * @param cls Closure for @c iter.
520 GMT_iterate_channels (struct MeshTunnel3 *t, GMT_chan_iter iter, void *cls);
522 #if 0 /* keep Emacsens' auto-indent happy */
529 /* ifndef GNUNET_MESH_SERVICE_TUNNEL_H */
531 /* end of gnunet-mesh-service_tunnel.h */