- doxygen
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_tunnel.h
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file cadet/gnunet-service-cadet_tunnel.h
23  * @brief cadet service; dealing with tunnels and crypto
24  * @author Bartlomiej Polot
25  *
26  * All functions in this file should use the prefix GMT (Gnunet Cadet Tunnel)
27  */
28
29 #ifndef GNUNET_SERVICE_CADET_TUNNEL_H
30 #define GNUNET_SERVICE_CADET_TUNNEL_H
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #if 0                           /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40 #include "platform.h"
41 #include "gnunet_util_lib.h"
42
43 /**
44  * All the connectivity states a tunnel can be in.
45  */
46 enum CadetTunnelCState
47 {
48     /**
49      * Uninitialized status, should never appear in operation.
50      */
51   CADET_TUNNEL_NEW,
52
53     /**
54      * No path to the peer known yet.
55      */
56   CADET_TUNNEL_SEARCHING,
57
58     /**
59      * Request sent, not yet answered.
60      */
61   CADET_TUNNEL_WAITING,
62
63     /**
64      * Peer connected and ready to accept data.
65      */
66   CADET_TUNNEL_READY,
67
68   /**
69    * Tunnel being shut down, don't try to keep it alive.
70    */
71   CADET_TUNNEL_SHUTDOWN
72 };
73
74
75 /**
76  * All the encryption states a tunnel can be in.
77  */
78 enum CadetTunnelEState
79 {
80   /**
81    * Uninitialized status, should never appear in operation.
82    */
83   CADET_TUNNEL_KEY_UNINITIALIZED,
84
85   /**
86    * Ephemeral key sent, waiting for peer's key.
87    */
88   CADET_TUNNEL_KEY_SENT,
89
90   /**
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).
95    */
96   CADET_TUNNEL_KEY_PING,
97
98   /**
99    * Handshake completed: session key available.
100    */
101   CADET_TUNNEL_KEY_OK,
102
103   /**
104    * New ephemeral key and ping sent, waiting for pong. Unlike KEY_PING,
105    * we still have a valid session key and therefore we *can* still send
106    * traffic on the tunnel.
107    */
108   CADET_TUNNEL_KEY_REKEY,
109 };
110
111 /**
112  * Struct containing all information regarding a given peer
113  */
114 struct CadetTunnel;
115
116
117 #include "gnunet-service-cadet_channel.h"
118 #include "gnunet-service-cadet_connection.h"
119 #include "gnunet-service-cadet_peer.h"
120
121 /**
122  * Handle for messages queued but not yet sent.
123  */
124 struct CadetTunnelQueue;
125
126 /**
127  * Callback called when a queued message is sent.
128  *
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.
133  */
134 typedef void (*GCT_sent) (void *cls,
135                           struct CadetTunnel *t,
136                           struct CadetTunnelQueue *q,
137                           uint16_t type, size_t size);
138
139 typedef void (*GCT_conn_iter) (void *cls, struct CadetConnection *c);
140 typedef void (*GCT_chan_iter) (void *cls, struct CadetChannel *ch);
141
142
143 /******************************************************************************/
144 /********************************    API    ***********************************/
145 /******************************************************************************/
146
147 /**
148  * Initialize tunnel subsystem.
149  *
150  * @param c Configuration handle.
151  * @param key ECC private key, to derive all other keys and do crypto.
152  */
153 void
154 GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
155           const struct GNUNET_CRYPTO_EddsaPrivateKey *key);
156
157 /**
158  * Shut down the tunnel subsystem.
159  */
160 void
161 GCT_shutdown (void);
162
163 /**
164  * Create a tunnel.
165  *
166  * @param destination Peer this tunnel is towards.
167  */
168 struct CadetTunnel *
169 GCT_new (struct CadetPeer *destination);
170
171 /**
172  * Tunnel is empty: destroy it.
173  *
174  * Notifies all connections about the destruction.
175  *
176  * @param t Tunnel to destroy.
177  */
178 void
179 GCT_destroy_empty (struct CadetTunnel *t);
180
181 /**
182  * Destroy tunnel if empty (no more channels).
183  *
184  * @param t Tunnel to destroy if empty.
185  */
186 void
187 GCT_destroy_if_empty (struct CadetTunnel *t);
188
189 /**
190  * Destroy the tunnel.
191  *
192  * This function does not generate any warning traffic to clients or peers.
193  *
194  * Tasks:
195  * Cancel messages belonging to this tunnel queued to neighbors.
196  * Free any allocated resources linked to the tunnel.
197  *
198  * @param t The tunnel to destroy.
199  */
200 void
201 GCT_destroy (struct CadetTunnel *t);
202
203
204 /**
205  * Change the tunnel's connection state.
206  *
207  * @param t Tunnel whose connection state to change.
208  * @param cstate New connection state.
209  */
210 void
211 GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate);
212
213
214 /**
215  * Change the tunnel encryption state.
216  *
217  * @param t Tunnel whose encryption state to change.
218  * @param state New encryption state.
219  */
220 void
221 GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state);
222
223 /**
224  * Add a connection to a tunnel.
225  *
226  * @param t Tunnel.
227  * @param c Connection.
228  */
229 void
230 GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c);
231
232 /**
233  * Remove a connection from a tunnel.
234  *
235  * @param t Tunnel.
236  * @param c Connection.
237  */
238 void
239 GCT_remove_connection (struct CadetTunnel *t, struct CadetConnection *c);
240
241 /**
242  * Add a channel to a tunnel.
243  *
244  * @param t Tunnel.
245  * @param ch Channel.
246  */
247 void
248 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch);
249
250 /**
251  * Remove a channel from a tunnel.
252  *
253  * @param t Tunnel.
254  * @param ch Channel.
255  */
256 void
257 GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch);
258
259 /**
260  * Search for a channel by global ID.
261  *
262  * @param t Tunnel containing the channel.
263  * @param chid Public channel number.
264  *
265  * @return channel handler, NULL if doesn't exist
266  */
267 struct CadetChannel *
268 GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid);
269
270 /**
271  * Decrypt and demultiplex by message type. Call appropriate handler
272  * for a message
273  * towards a channel of a local tunnel.
274  *
275  * @param t Tunnel this message came on.
276  * @param msg Message header.
277  */
278 void
279 GCT_handle_encrypted (struct CadetTunnel *t,
280                       const struct GNUNET_CADET_Encrypted *msg);
281
282 /**
283  * Demultiplex an encapsulated KX message by message type.
284  *
285  * @param t Tunnel on which the message came.
286  * @param message KX message itself.
287  */
288 void
289 GCT_handle_kx (struct CadetTunnel *t,
290                const struct GNUNET_MessageHeader *message);
291
292 /**
293  * @brief Use the given path for the tunnel.
294  * Update the next and prev hops (and RCs).
295  * (Re)start the path refresh in case the tunnel is locally owned.
296  *
297  * @param t Tunnel to update.
298  * @param p Path to use.
299  *
300  * @return Connection created.
301  */
302 struct CadetConnection *
303 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p);
304
305 /**
306  * Count all created connections of a tunnel. Not necessarily ready connections!
307  *
308  * @param t Tunnel on which to count.
309  *
310  * @return Number of connections created, either being established or ready.
311  */
312 unsigned int
313 GCT_count_any_connections (struct CadetTunnel *t);
314
315 /**
316  * Count established (ready) connections of a tunnel.
317  *
318  * @param t Tunnel on which to count.
319  *
320  * @return Number of connections.
321  */
322 unsigned int
323 GCT_count_connections (struct CadetTunnel *t);
324
325 /**
326  * Count channels of a tunnel.
327  *
328  * @param t Tunnel on which to count.
329  *
330  * @return Number of channels.
331  */
332 unsigned int
333 GCT_count_channels (struct CadetTunnel *t);
334
335 /**
336  * Get the connectivity state of a tunnel.
337  *
338  * @param t Tunnel.
339  *
340  * @return Tunnel's connectivity state.
341  */
342 enum CadetTunnelCState
343 GCT_get_cstate (struct CadetTunnel *t);
344
345 /**
346  * Get the encryption state of a tunnel.
347  *
348  * @param t Tunnel.
349  *
350  * @return Tunnel's encryption state.
351  */
352 enum CadetTunnelEState
353 GCT_get_estate (struct CadetTunnel *t);
354
355 /**
356  * Get the maximum buffer space for a tunnel towards a local client.
357  *
358  * @param t Tunnel.
359  *
360  * @return Biggest buffer space offered by any channel in the tunnel.
361  */
362 unsigned int
363 GCT_get_channels_buffer (struct CadetTunnel *t);
364
365 /**
366  * Get the total buffer space for a tunnel for P2P traffic.
367  *
368  * @param t Tunnel.
369  *
370  * @return Buffer space offered by all connections in the tunnel.
371  */
372 unsigned int
373 GCT_get_connections_buffer (struct CadetTunnel *t);
374
375 /**
376  * Get the tunnel's destination.
377  *
378  * @param t Tunnel.
379  *
380  * @return ID of the destination peer.
381  */
382 const struct GNUNET_PeerIdentity *
383 GCT_get_destination (struct CadetTunnel *t);
384
385 /**
386  * Get the tunnel's next free Channel ID.
387  *
388  * @param t Tunnel.
389  *
390  * @return ID of a channel free to use.
391  */
392 CADET_ChannelNumber
393 GCT_get_next_chid (struct CadetTunnel *t);
394
395 /**
396  * Send ACK on one or more channels due to buffer in connections.
397  *
398  * @param t Channel which has some free buffer space.
399  */
400 void
401 GCT_unchoke_channels (struct CadetTunnel *t);
402
403 /**
404  * Send ACK on one or more connections due to buffer space to the client.
405  *
406  * Iterates all connections of the tunnel and sends ACKs appropriately.
407  *
408  * @param t Tunnel which has some free buffer space.
409  */
410 void
411 GCT_send_connection_acks (struct CadetTunnel *t);
412
413 /**
414  * Cancel a previously sent message while it's in the queue.
415  *
416  * ONLY can be called before the continuation given to the send function
417  * is called. Once the continuation is called, the message is no longer in the
418  * queue.
419  *
420  * @param q Handle to the queue.
421  */
422 void
423 GCT_cancel (struct CadetTunnelQueue *q);
424
425 /**
426  * Sends an already built message on a tunnel, encrypting it and
427  * choosing the best connection.
428  *
429  * @param message Message to send. Function modifies it.
430  * @param t Tunnel on which this message is transmitted.
431  * @param c Connection to use (autoselect if NULL).
432  * @param force Force the tunnel to take the message (buffer overfill).
433  * @param cont Continuation to call once message is really sent.
434  * @param cont_cls Closure for @c cont.
435  *
436  * @return Handle to cancel message. NULL if @c cont is NULL.
437  */
438 struct CadetTunnelQueue *
439 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
440                            struct CadetTunnel *t, struct CadetConnection *c,
441                            int force, GCT_sent cont, void *cont_cls);
442
443 /**
444  * Sends an already built and encrypted message on a tunnel, choosing the best
445  * connection. Useful for re-queueing messages queued on a destroyed connection.
446  *
447  * @param message Message to send. Function modifies it.
448  * @param t Tunnel on which this message is transmitted.
449  */
450 void
451 GCT_resend_message (const struct GNUNET_MessageHeader *message,
452                     struct CadetTunnel *t);
453
454 /**
455  * Is the tunnel directed towards the local peer?
456  *
457  * @param t Tunnel.
458  *
459  * @return #GNUNET_YES if it is loopback.
460  */
461 int
462 GCT_is_loopback (const struct CadetTunnel *t);
463
464 /**
465  * Is the tunnel using this path already?
466  *
467  * @param t Tunnel.
468  * @param p Path.
469  *
470  * @return #GNUNET_YES a connection uses this path.
471  */
472 int
473 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p);
474
475 /**
476  * Get a cost of a path for a tunnel considering existing connections.
477  *
478  * @param t Tunnel.
479  * @param path Candidate path.
480  *
481  * @return Cost of the path (path length + number of overlapping nodes)
482  */
483 unsigned int
484 GCT_get_path_cost (const struct CadetTunnel *t,
485                    const struct CadetPeerPath *path);
486
487 /**
488  * Get the static string for the peer this tunnel is directed.
489  *
490  * @param t Tunnel.
491  *
492  * @return Static string the destination peer's ID.
493  */
494 const char *
495 GCT_2s (const struct CadetTunnel *t);
496
497 /**
498  * Log all possible info about the tunnel state.
499  *
500  * @param t Tunnel to debug.
501  * @param level Debug level to use.
502  */
503 void
504 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
505
506 /**
507  * Iterate all tunnels.
508  *
509  * @param iter Iterator.
510  * @param cls Closure for @c iter.
511  */
512 void
513 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
514
515 /**
516  * Count all tunnels.
517  *
518  * @return Number of tunnels to remote peers kept by this peer.
519  */
520 unsigned int
521 GCT_count_all (void);
522
523 /**
524  * Iterate all connections of a tunnel.
525  *
526  * @param t Tunnel whose connections to iterate.
527  * @param iter Iterator.
528  * @param cls Closure for @c iter.
529  */
530 void
531 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
532
533 /**
534  * Iterate all channels of a tunnel.
535  *
536  * @param t Tunnel whose channels to iterate.
537  * @param iter Iterator.
538  * @param cls Closure for @c iter.
539  */
540 void
541 GCT_iterate_channels (struct CadetTunnel *t, GCT_chan_iter iter, void *cls);
542
543 #if 0                           /* keep Emacsens' auto-indent happy */
544 {
545 #endif
546 #ifdef __cplusplus
547 }
548 #endif
549
550 /* ifndef GNUNET_CADET_SERVICE_TUNNEL_H */
551 #endif
552 /* end of gnunet-cadet-service_tunnel.h */