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