- don't send KEEPALIVE if the tunnel has traffic queued
[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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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 /**
161  * Shut down the tunnel subsystem.
162  */
163 void
164 GCT_shutdown (void);
165
166
167 /**
168  * Create a tunnel.
169  *
170  * @param destination Peer this tunnel is towards.
171  */
172 struct CadetTunnel *
173 GCT_new (struct CadetPeer *destination);
174
175
176 /**
177  * Tunnel is empty: destroy it.
178  *
179  * Notifies all connections about the destruction.
180  *
181  * @param t Tunnel to destroy.
182  */
183 void
184 GCT_destroy_empty (struct CadetTunnel *t);
185
186
187 /**
188  * Destroy tunnel if empty (no more channels).
189  *
190  * @param t Tunnel to destroy if empty.
191  */
192 void
193 GCT_destroy_if_empty (struct CadetTunnel *t);
194
195
196 /**
197  * Destroy the tunnel.
198  *
199  * This function does not generate any warning traffic to clients or peers.
200  *
201  * Tasks:
202  * Cancel messages belonging to this tunnel queued to neighbors.
203  * Free any allocated resources linked to the tunnel.
204  *
205  * @param t The tunnel to destroy.
206  */
207 void
208 GCT_destroy (struct CadetTunnel *t);
209
210
211 /**
212  * Change the tunnel's connection state.
213  *
214  * @param t Tunnel whose connection state to change.
215  * @param cstate New connection state.
216  */
217 void
218 GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate);
219
220
221 /**
222  * Change the tunnel encryption state.
223  *
224  * @param t Tunnel whose encryption state to change.
225  * @param state New encryption state.
226  */
227 void
228 GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state);
229
230
231 /**
232  * Add a connection to a tunnel.
233  *
234  * @param t Tunnel.
235  * @param c Connection.
236  */
237 void
238 GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c);
239
240
241 /**
242  * Remove a connection from a tunnel.
243  *
244  * @param t Tunnel.
245  * @param c Connection.
246  */
247 void
248 GCT_remove_connection (struct CadetTunnel *t, struct CadetConnection *c);
249
250
251 /**
252  * Add a channel to a tunnel.
253  *
254  * @param t Tunnel.
255  * @param ch Channel.
256  */
257 void
258 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch);
259
260
261 /**
262  * Remove a channel from a tunnel.
263  *
264  * @param t Tunnel.
265  * @param ch Channel.
266  */
267 void
268 GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch);
269
270
271 /**
272  * Search for a channel by global ID.
273  *
274  * @param t Tunnel containing the channel.
275  * @param chid Public channel number.
276  *
277  * @return channel handler, NULL if doesn't exist
278  */
279 struct CadetChannel *
280 GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid);
281
282
283 /**
284  * Decrypt 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_encrypted (struct CadetTunnel *t,
292                       const struct GNUNET_MessageHeader *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 /**
307  * @brief Use the given path for the tunnel.
308  * Update the next and prev hops (and RCs).
309  * (Re)start the path refresh in case the tunnel is locally owned.
310  *
311  * @param t Tunnel to update.
312  * @param p Path to use.
313  *
314  * @return Connection created.
315  */
316 struct CadetConnection *
317 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p);
318
319
320 /**
321  * Count all created connections of a tunnel. Not necessarily ready connections!
322  *
323  * @param t Tunnel on which to count.
324  *
325  * @return Number of connections created, either being established or ready.
326  */
327 unsigned int
328 GCT_count_any_connections (struct CadetTunnel *t);
329
330
331 /**
332  * Count established (ready) connections of a tunnel.
333  *
334  * @param t Tunnel on which to count.
335  *
336  * @return Number of connections.
337  */
338 unsigned int
339 GCT_count_connections (struct CadetTunnel *t);
340
341
342 /**
343  * Count channels of a tunnel.
344  *
345  * @param t Tunnel on which to count.
346  *
347  * @return Number of channels.
348  */
349 unsigned int
350 GCT_count_channels (struct CadetTunnel *t);
351
352
353 /**
354  * Get the connectivity state of a tunnel.
355  *
356  * @param t Tunnel.
357  *
358  * @return Tunnel's connectivity state.
359  */
360 enum CadetTunnelCState
361 GCT_get_cstate (struct CadetTunnel *t);
362
363
364 /**
365  * Get the encryption state of a tunnel.
366  *
367  * @param t Tunnel.
368  *
369  * @return Tunnel's encryption state.
370  */
371 enum CadetTunnelEState
372 GCT_get_estate (struct CadetTunnel *t);
373
374
375 /**
376  * Get the maximum buffer space for a tunnel towards a local client.
377  *
378  * @param t Tunnel.
379  *
380  * @return Biggest buffer space offered by any channel in the tunnel.
381  */
382 unsigned int
383 GCT_get_channels_buffer (struct CadetTunnel *t);
384
385
386 /**
387  * Get the total buffer space for a tunnel for P2P traffic.
388  *
389  * @param t Tunnel.
390  *
391  * @return Buffer space offered by all connections in the tunnel.
392  */
393 unsigned int
394 GCT_get_connections_buffer (struct CadetTunnel *t);
395
396
397 /**
398  * Get the tunnel's destination.
399  *
400  * @param t Tunnel.
401  *
402  * @return ID of the destination peer.
403  */
404 const struct GNUNET_PeerIdentity *
405 GCT_get_destination (struct CadetTunnel *t);
406
407
408 /**
409  * Get the tunnel's next free Channel ID.
410  *
411  * @param t Tunnel.
412  *
413  * @return ID of a channel free to use.
414  */
415 CADET_ChannelNumber
416 GCT_get_next_chid (struct CadetTunnel *t);
417
418
419 /**
420  * Send ACK on one or more channels due to buffer in connections.
421  *
422  * @param t Channel which has some free buffer space.
423  */
424 void
425 GCT_unchoke_channels (struct CadetTunnel *t);
426
427
428 /**
429  * Send ACK on one or more connections due to buffer space to the client.
430  *
431  * Iterates all connections of the tunnel and sends ACKs appropriately.
432  *
433  * @param t Tunnel which has some free buffer space.
434  */
435 void
436 GCT_send_connection_acks (struct CadetTunnel *t);
437
438
439 /**
440  * Cancel a previously sent message while it's in the queue.
441  *
442  * ONLY can be called before the continuation given to the send function
443  * is called. Once the continuation is called, the message is no longer in the
444  * queue.
445  *
446  * @param q Handle to the queue.
447  */
448 void
449 GCT_cancel (struct CadetTunnelQueue *q);
450
451
452 /**
453  * Check if the tunnel has queued traffic.
454  *
455  * @param t Tunnel to check.
456  *
457  * @return #GNUNET_YES if there is queued traffic
458  *         #GNUNET_NO otherwise
459  */
460 int
461 GCT_has_queued_traffic (struct CadetTunnel *t);
462
463 /**
464  * Sends an already built message on a tunnel, encrypting it and
465  * choosing the best connection.
466  *
467  * @param message Message to send. Function modifies it.
468  * @param t Tunnel on which this message is transmitted.
469  * @param c Connection to use (autoselect if NULL).
470  * @param force Force the tunnel to take the message (buffer overfill).
471  * @param cont Continuation to call once message is really sent.
472  * @param cont_cls Closure for @c cont.
473  *
474  * @return Handle to cancel message. NULL if @c cont is NULL.
475  */
476 struct CadetTunnelQueue *
477 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
478                            struct CadetTunnel *t, struct CadetConnection *c,
479                            int force, GCT_sent cont, void *cont_cls);
480
481
482 /**
483  * Send an Axolotl KX message.
484  *
485  * @param t Tunnel on which to send it.
486  * @param force_reply Force the other peer to reply with a KX message.
487  */
488 void
489 GCT_send_ax_kx (struct CadetTunnel *t, int force_reply);
490
491
492 /**
493  * Sends an already built and encrypted message on a tunnel, choosing the best
494  * connection. Useful for re-queueing messages queued on a destroyed connection.
495  *
496  * @param message Message to send. Function modifies it.
497  * @param t Tunnel on which this message is transmitted.
498  */
499 void
500 GCT_resend_message (const struct GNUNET_MessageHeader *message,
501                     struct CadetTunnel *t);
502
503
504 /**
505  * Is the tunnel directed towards the local peer?
506  *
507  * @param t Tunnel.
508  *
509  * @return #GNUNET_YES if it is loopback.
510  */
511 int
512 GCT_is_loopback (const struct CadetTunnel *t);
513
514
515 /**
516  * Is the tunnel using this path already?
517  *
518  * @param t Tunnel.
519  * @param p Path.
520  *
521  * @return #GNUNET_YES a connection uses this path.
522  */
523 int
524 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p);
525
526
527 /**
528  * Get a cost of a path for a tunnel considering existing connections.
529  *
530  * @param t Tunnel.
531  * @param path Candidate path.
532  *
533  * @return Cost of the path (path length + number of overlapping nodes)
534  */
535 unsigned int
536 GCT_get_path_cost (const struct CadetTunnel *t,
537                    const struct CadetPeerPath *path);
538
539
540 /**
541  * Get the static string for the peer this tunnel is directed.
542  *
543  * @param t Tunnel.
544  *
545  * @return Static string the destination peer's ID.
546  */
547 const char *
548 GCT_2s (const struct CadetTunnel *t);
549
550
551 /**
552  * Log all possible info about the tunnel state.
553  *
554  * @param t Tunnel to debug.
555  * @param level Debug level to use.
556  */
557 void
558 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
559
560
561 /**
562  * Iterate all tunnels.
563  *
564  * @param iter Iterator.
565  * @param cls Closure for @c iter.
566  */
567 void
568 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
569
570
571 /**
572  * Count all tunnels.
573  *
574  * @return Number of tunnels to remote peers kept by this peer.
575  */
576 unsigned int
577 GCT_count_all (void);
578
579
580 /**
581  * Iterate all connections of a tunnel.
582  *
583  * @param t Tunnel whose connections to iterate.
584  * @param iter Iterator.
585  * @param cls Closure for @c iter.
586  */
587 void
588 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
589
590
591 /**
592  * Iterate all channels of a tunnel.
593  *
594  * @param t Tunnel whose channels to iterate.
595  * @param iter Iterator.
596  * @param cls Closure for @c iter.
597  */
598 void
599 GCT_iterate_channels (struct CadetTunnel *t,
600                       GCT_chan_iter iter,
601                       void *cls);
602
603
604 #if 0                           /* keep Emacsens' auto-indent happy */
605 {
606 #endif
607 #ifdef __cplusplus
608 }
609 #endif
610
611 /* ifndef GNUNET_CADET_SERVICE_TUNNEL_H */
612 #endif
613 /* end of gnunet-cadet-service_tunnel.h */