- don't re-encrypt data queued in a broken connection
[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      * Path to the peer not 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. Opposite to 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  * Mark a path as no longer valid for this tunnel: has been tried and failed.
234  *
235  * @param t Tunnel to update.
236  * @param path Invalid path to remove. Is destroyed after removal.
237  */
238 void
239 GCT_remove_path (struct CadetTunnel *t, struct CadetPeerPath *path);
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  * Add a channel to a tunnel.
252  *
253  * @param t Tunnel.
254  * @param ch Channel.
255  */
256 void
257 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch);
258
259 /**
260  * Remove a channel from a tunnel.
261  *
262  * @param t Tunnel.
263  * @param ch Channel.
264  */
265 void
266 GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch);
267
268 /**
269  * Search for a channel by global ID.
270  *
271  * @param t Tunnel containing the channel.
272  * @param chid Public channel number.
273  *
274  * @return channel handler, NULL if doesn't exist
275  */
276 struct CadetChannel *
277 GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid);
278
279 /**
280  * Decrypt and demultiplex by message type. Call appropriate handler
281  * for a message
282  * towards a channel of a local tunnel.
283  *
284  * @param t Tunnel this message came on.
285  * @param msg Message header.
286  */
287 void
288 GCT_handle_encrypted (struct CadetTunnel *t,
289                       const struct GNUNET_CADET_Encrypted *msg);
290
291 /**
292  * Demultiplex an encapsulated KX message by message type.
293  *
294  * @param t Tunnel on which the message came.
295  * @param message KX message itself.
296  */
297 void
298 GCT_handle_kx (struct CadetTunnel *t,
299                const struct GNUNET_MessageHeader *message);
300
301 /**
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.
305  *
306  * @param t Tunnel to update.
307  * @param p Path to use.
308  *
309  * @return Connection created.
310  */
311 struct CadetConnection *
312 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p);
313
314 /**
315  * Count established (ready) connections of a tunnel.
316  *
317  * @param t Tunnel on which to count.
318  *
319  * @return Number of connections.
320  */
321 unsigned int
322 GCT_count_connections (struct CadetTunnel *t);
323
324 /**
325  * Count channels of a tunnel.
326  *
327  * @param t Tunnel on which to count.
328  *
329  * @return Number of channels.
330  */
331 unsigned int
332 GCT_count_channels (struct CadetTunnel *t);
333
334 /**
335  * Get the connectivity state of a tunnel.
336  *
337  * @param t Tunnel.
338  *
339  * @return Tunnel's connectivity state.
340  */
341 enum CadetTunnelCState
342 GCT_get_cstate (struct CadetTunnel *t);
343
344 /**
345  * Get the encryption state of a tunnel.
346  *
347  * @param t Tunnel.
348  *
349  * @return Tunnel's encryption state.
350  */
351 enum CadetTunnelEState
352 GCT_get_estate (struct CadetTunnel *t);
353
354 /**
355  * Get the maximum buffer space for a tunnel towards a local client.
356  *
357  * @param t Tunnel.
358  *
359  * @return Biggest buffer space offered by any channel in the tunnel.
360  */
361 unsigned int
362 GCT_get_channels_buffer (struct CadetTunnel *t);
363
364 /**
365  * Get the total buffer space for a tunnel for P2P traffic.
366  *
367  * @param t Tunnel.
368  *
369  * @return Buffer space offered by all connections in the tunnel.
370  */
371 unsigned int
372 GCT_get_connections_buffer (struct CadetTunnel *t);
373
374 /**
375  * Get the tunnel's destination.
376  *
377  * @param t Tunnel.
378  *
379  * @return ID of the destination peer.
380  */
381 const struct GNUNET_PeerIdentity *
382 GCT_get_destination (struct CadetTunnel *t);
383
384 /**
385  * Get the tunnel's next free Channel ID.
386  *
387  * @param t Tunnel.
388  *
389  * @return ID of a channel free to use.
390  */
391 CADET_ChannelNumber
392 GCT_get_next_chid (struct CadetTunnel *t);
393
394 /**
395  * Send ACK on one or more channels due to buffer in connections.
396  *
397  * @param t Channel which has some free buffer space.
398  */
399 void
400 GCT_unchoke_channels (struct CadetTunnel *t);
401
402 /**
403  * Send ACK on one or more connections due to buffer space to the client.
404  *
405  * Iterates all connections of the tunnel and sends ACKs appropriately.
406  *
407  * @param t Tunnel which has some free buffer space.
408  */
409 void
410 GCT_send_connection_acks (struct CadetTunnel *t);
411
412 /**
413  * Cancel a previously sent message while it's in the queue.
414  *
415  * ONLY can be called before the continuation given to the send function
416  * is called. Once the continuation is called, the message is no longer in the
417  * queue.
418  *
419  * @param q Handle to the queue.
420  */
421 void
422 GCT_cancel (struct CadetTunnelQueue *q);
423
424 /**
425  * Sends an already built message on a tunnel, encrypting it and
426  * choosing the best connection.
427  *
428  * @param message Message to send. Function modifies it.
429  * @param t Tunnel on which this message is transmitted.
430  * @param c Connection to use (autoselect if NULL).
431  * @param force Force the tunnel to take the message (buffer overfill).
432  * @param cont Continuation to call once message is really sent.
433  * @param cont_cls Closure for @c cont.
434  *
435  * @return Handle to cancel message. NULL if @c cont is NULL.
436  */
437 struct CadetTunnelQueue *
438 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
439                            struct CadetTunnel *t, struct CadetConnection *c,
440                            int force, GCT_sent cont, void *cont_cls);
441
442 /**
443  * Sends an already built and encrypted message on a tunnel, choosing the best
444  * connection. Useful for re-queueing messages queued on a destroyed connection.
445  *
446  * @param message Message to send. Function modifies it.
447  * @param t Tunnel on which this message is transmitted.
448  */
449 void
450 GCT_resend_message (const struct GNUNET_MessageHeader *message,
451                     struct CadetTunnel *t);
452
453 /**
454  * Is the tunnel directed towards the local peer?
455  *
456  * @param t Tunnel.
457  *
458  * @return #GNUNET_YES if it is loopback.
459  */
460 int
461 GCT_is_loopback (const struct CadetTunnel *t);
462
463 /**
464  * Is the tunnel using this path already?
465  *
466  * @param t Tunnel.
467  * @param p Path.
468  *
469  * @return #GNUNET_YES a connection uses this path.
470  */
471 int
472 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p);
473
474 /**
475  * Get a cost of a path for a tunnel considering existing connections.
476  *
477  * @param t Tunnel.
478  * @param path Candidate path.
479  *
480  * @return Cost of the path (path length + number of overlapping nodes)
481  */
482 unsigned int
483 GCT_get_path_cost (const struct CadetTunnel *t,
484                    const struct CadetPeerPath *path);
485
486 /**
487  * Get the static string for the peer this tunnel is directed.
488  *
489  * @param t Tunnel.
490  *
491  * @return Static string the destination peer's ID.
492  */
493 const char *
494 GCT_2s (const struct CadetTunnel *t);
495
496 /**
497  * Log all possible info about the tunnel state.
498  *
499  * @param t Tunnel to debug.
500  * @param level Debug level to use.
501  */
502 void
503 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
504
505 /**
506  * Iterate all tunnels.
507  *
508  * @param iter Iterator.
509  * @param cls Closure for @c iter.
510  */
511 void
512 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
513
514 /**
515  * Count all tunnels.
516  *
517  * @return Number of tunnels to remote peers kept by this peer.
518  */
519 unsigned int
520 GCT_count_all (void);
521
522 /**
523  * Iterate all connections of a tunnel.
524  *
525  * @param t Tunnel whose connections to iterate.
526  * @param iter Iterator.
527  * @param cls Closure for @c iter.
528  */
529 void
530 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
531
532 /**
533  * Iterate all channels of a tunnel.
534  *
535  * @param t Tunnel whose channels to iterate.
536  * @param iter Iterator.
537  * @param cls Closure for @c iter.
538  */
539 void
540 GCT_iterate_channels (struct CadetTunnel *t, GCT_chan_iter iter, void *cls);
541
542 #if 0                           /* keep Emacsens' auto-indent happy */
543 {
544 #endif
545 #ifdef __cplusplus
546 }
547 #endif
548
549 /* ifndef GNUNET_CADET_SERVICE_TUNNEL_H */
550 #endif
551 /* end of gnunet-cadet-service_tunnel.h */