382fcdf339f8f177d42081a12929b817e67d42c9
[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. 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 all created connections of a tunnel. Not necessarily ready connections!
316  *
317  * @param t Tunnel on which to count.
318  *
319  * @return Number of connections created, either being established or ready.
320  */
321 unsigned int
322 GCT_count_any_connections (struct CadetTunnel *t);
323
324 /**
325  * Count established (ready) connections of a tunnel.
326  *
327  * @param t Tunnel on which to count.
328  *
329  * @return Number of connections.
330  */
331 unsigned int
332 GCT_count_connections (struct CadetTunnel *t);
333
334 /**
335  * Count channels of a tunnel.
336  *
337  * @param t Tunnel on which to count.
338  *
339  * @return Number of channels.
340  */
341 unsigned int
342 GCT_count_channels (struct CadetTunnel *t);
343
344 /**
345  * Get the connectivity state of a tunnel.
346  *
347  * @param t Tunnel.
348  *
349  * @return Tunnel's connectivity state.
350  */
351 enum CadetTunnelCState
352 GCT_get_cstate (struct CadetTunnel *t);
353
354 /**
355  * Get the encryption state of a tunnel.
356  *
357  * @param t Tunnel.
358  *
359  * @return Tunnel's encryption state.
360  */
361 enum CadetTunnelEState
362 GCT_get_estate (struct CadetTunnel *t);
363
364 /**
365  * Get the maximum buffer space for a tunnel towards a local client.
366  *
367  * @param t Tunnel.
368  *
369  * @return Biggest buffer space offered by any channel in the tunnel.
370  */
371 unsigned int
372 GCT_get_channels_buffer (struct CadetTunnel *t);
373
374 /**
375  * Get the total buffer space for a tunnel for P2P traffic.
376  *
377  * @param t Tunnel.
378  *
379  * @return Buffer space offered by all connections in the tunnel.
380  */
381 unsigned int
382 GCT_get_connections_buffer (struct CadetTunnel *t);
383
384 /**
385  * Get the tunnel's destination.
386  *
387  * @param t Tunnel.
388  *
389  * @return ID of the destination peer.
390  */
391 const struct GNUNET_PeerIdentity *
392 GCT_get_destination (struct CadetTunnel *t);
393
394 /**
395  * Get the tunnel's next free Channel ID.
396  *
397  * @param t Tunnel.
398  *
399  * @return ID of a channel free to use.
400  */
401 CADET_ChannelNumber
402 GCT_get_next_chid (struct CadetTunnel *t);
403
404 /**
405  * Send ACK on one or more channels due to buffer in connections.
406  *
407  * @param t Channel which has some free buffer space.
408  */
409 void
410 GCT_unchoke_channels (struct CadetTunnel *t);
411
412 /**
413  * Send ACK on one or more connections due to buffer space to the client.
414  *
415  * Iterates all connections of the tunnel and sends ACKs appropriately.
416  *
417  * @param t Tunnel which has some free buffer space.
418  */
419 void
420 GCT_send_connection_acks (struct CadetTunnel *t);
421
422 /**
423  * Cancel a previously sent message while it's in the queue.
424  *
425  * ONLY can be called before the continuation given to the send function
426  * is called. Once the continuation is called, the message is no longer in the
427  * queue.
428  *
429  * @param q Handle to the queue.
430  */
431 void
432 GCT_cancel (struct CadetTunnelQueue *q);
433
434 /**
435  * Sends an already built message on a tunnel, encrypting it and
436  * choosing the best connection.
437  *
438  * @param message Message to send. Function modifies it.
439  * @param t Tunnel on which this message is transmitted.
440  * @param c Connection to use (autoselect if NULL).
441  * @param force Force the tunnel to take the message (buffer overfill).
442  * @param cont Continuation to call once message is really sent.
443  * @param cont_cls Closure for @c cont.
444  *
445  * @return Handle to cancel message. NULL if @c cont is NULL.
446  */
447 struct CadetTunnelQueue *
448 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
449                            struct CadetTunnel *t, struct CadetConnection *c,
450                            int force, GCT_sent cont, void *cont_cls);
451
452 /**
453  * Sends an already built and encrypted message on a tunnel, choosing the best
454  * connection. Useful for re-queueing messages queued on a destroyed connection.
455  *
456  * @param message Message to send. Function modifies it.
457  * @param t Tunnel on which this message is transmitted.
458  */
459 void
460 GCT_resend_message (const struct GNUNET_MessageHeader *message,
461                     struct CadetTunnel *t);
462
463 /**
464  * Is the tunnel directed towards the local peer?
465  *
466  * @param t Tunnel.
467  *
468  * @return #GNUNET_YES if it is loopback.
469  */
470 int
471 GCT_is_loopback (const struct CadetTunnel *t);
472
473 /**
474  * Is the tunnel using this path already?
475  *
476  * @param t Tunnel.
477  * @param p Path.
478  *
479  * @return #GNUNET_YES a connection uses this path.
480  */
481 int
482 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p);
483
484 /**
485  * Get a cost of a path for a tunnel considering existing connections.
486  *
487  * @param t Tunnel.
488  * @param path Candidate path.
489  *
490  * @return Cost of the path (path length + number of overlapping nodes)
491  */
492 unsigned int
493 GCT_get_path_cost (const struct CadetTunnel *t,
494                    const struct CadetPeerPath *path);
495
496 /**
497  * Get the static string for the peer this tunnel is directed.
498  *
499  * @param t Tunnel.
500  *
501  * @return Static string the destination peer's ID.
502  */
503 const char *
504 GCT_2s (const struct CadetTunnel *t);
505
506 /**
507  * Log all possible info about the tunnel state.
508  *
509  * @param t Tunnel to debug.
510  * @param level Debug level to use.
511  */
512 void
513 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
514
515 /**
516  * Iterate all tunnels.
517  *
518  * @param iter Iterator.
519  * @param cls Closure for @c iter.
520  */
521 void
522 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
523
524 /**
525  * Count all tunnels.
526  *
527  * @return Number of tunnels to remote peers kept by this peer.
528  */
529 unsigned int
530 GCT_count_all (void);
531
532 /**
533  * Iterate all connections of a tunnel.
534  *
535  * @param t Tunnel whose connections to iterate.
536  * @param iter Iterator.
537  * @param cls Closure for @c iter.
538  */
539 void
540 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
541
542 /**
543  * Iterate all channels of a tunnel.
544  *
545  * @param t Tunnel whose channels to iterate.
546  * @param iter Iterator.
547  * @param cls Closure for @c iter.
548  */
549 void
550 GCT_iterate_channels (struct CadetTunnel *t, GCT_chan_iter iter, void *cls);
551
552 #if 0                           /* keep Emacsens' auto-indent happy */
553 {
554 #endif
555 #ifdef __cplusplus
556 }
557 #endif
558
559 /* ifndef GNUNET_CADET_SERVICE_TUNNEL_H */
560 #endif
561 /* end of gnunet-cadet-service_tunnel.h */