fixing dv plugin
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_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 mesh/gnunet-service-mesh_tunnel.h
23  * @brief mesh service; dealing with tunnels and crypto
24  * @author Bartlomiej Polot
25  *
26  * All functions in this file should use the prefix GMT (Gnunet Mesh Tunnel)
27  */
28
29 #ifndef GNUNET_SERVICE_MESH_TUNNEL_H
30 #define GNUNET_SERVICE_MESH_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 MeshTunnel3CState
47 {
48     /**
49      * Uninitialized status, should never appear in operation.
50      */
51   MESH_TUNNEL3_NEW,
52
53     /**
54      * Path to the peer not known yet.
55      */
56   MESH_TUNNEL3_SEARCHING,
57
58     /**
59      * Request sent, not yet answered.
60      */
61   MESH_TUNNEL3_WAITING,
62
63     /**
64      * Peer connected and ready to accept data.
65      */
66   MESH_TUNNEL3_READY,
67 };
68
69
70 /**
71  * All the encryption states a tunnel can be in.
72  */
73 enum MeshTunnel3EState
74 {
75   /**
76    * Uninitialized status, should never appear in operation.
77    */
78   MESH_TUNNEL3_KEY_UNINITIALIZED,
79
80   /**
81    * Ephemeral key sent, waiting for peer's key.
82    */
83   MESH_TUNNEL3_KEY_SENT,
84
85   /**
86    * New ephemeral key and ping sent, waiting for pong.
87    * This means that we DO have the peer's ephemeral key, otherwise the
88    * state would be KEY_SENT.
89    */
90   MESH_TUNNEL3_KEY_PING,
91
92   /**
93    * Handshake completed: session key available.
94    */
95   MESH_TUNNEL3_KEY_OK,
96 };
97
98 /**
99  * Struct containing all information regarding a given peer
100  */
101 struct MeshTunnel3;
102
103
104 #include "gnunet-service-mesh_channel.h"
105 #include "gnunet-service-mesh_connection.h"
106 #include "gnunet-service-mesh_peer.h"
107
108 /**
109  * Handle for messages queued but not yet sent.
110  */
111 struct MeshTunnel3Queue;
112
113 /**
114  * Callback called when a queued message is sent.
115  *
116  * @param cls Closure.
117  * @param t Tunnel this message was on.
118  * @param type Type of message sent.
119  * @param size Size of the message.
120  */
121 typedef void (*GMT_sent) (void *cls,
122                           struct MeshTunnel3 *t,
123                           struct MeshTunnel3Queue *q,
124                           uint16_t type, size_t size);
125
126 typedef void (*GMT_conn_iter) (void *cls, struct MeshConnection *c);
127 typedef void (*GMT_chan_iter) (void *cls, struct MeshChannel *ch);
128
129
130 /******************************************************************************/
131 /********************************    API    ***********************************/
132 /******************************************************************************/
133
134 /**
135  * Initialize tunnel subsystem.
136  *
137  * @param c Configuration handle.
138  * @param key ECC private key, to derive all other keys and do crypto.
139  */
140 void
141 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
142           const struct GNUNET_CRYPTO_EddsaPrivateKey *key);
143
144 /**
145  * Shut down the tunnel subsystem.
146  */
147 void
148 GMT_shutdown (void);
149
150 /**
151  * Create a tunnel.
152  *
153  * @param destination Peer this tunnel is towards.
154  */
155 struct MeshTunnel3 *
156 GMT_new (struct MeshPeer *destination);
157
158 /**
159  * Tunnel is empty: destroy it.
160  *
161  * Notifies all connections about the destruction.
162  *
163  * @param t Tunnel to destroy.
164  */
165 void
166 GMT_destroy_empty (struct MeshTunnel3 *t);
167
168 /**
169  * Destroy tunnel if empty (no more channels).
170  *
171  * @param t Tunnel to destroy if empty.
172  */
173 void
174 GMT_destroy_if_empty (struct MeshTunnel3 *t);
175
176 /**
177  * Destroy the tunnel.
178  *
179  * This function does not generate any warning traffic to clients or peers.
180  *
181  * Tasks:
182  * Cancel messages belonging to this tunnel queued to neighbors.
183  * Free any allocated resources linked to the tunnel.
184  *
185  * @param t The tunnel to destroy.
186  */
187 void
188 GMT_destroy (struct MeshTunnel3 *t);
189
190
191 /**
192  * Change the tunnel's connection state.
193  *
194  * @param t Tunnel whose connection state to change.
195  * @param cstate New connection state.
196  */
197 void
198 GMT_change_cstate (struct MeshTunnel3* t, enum MeshTunnel3CState cstate);
199
200
201 /**
202  * Change the tunnel encryption state.
203  *
204  * @param t Tunnel whose encryption state to change.
205  * @param state New encryption state.
206  */
207 void
208 GMT_change_estate (struct MeshTunnel3* t, enum MeshTunnel3EState state);
209
210 /**
211  * Add a connection to a tunnel.
212  *
213  * @param t Tunnel.
214  * @param c Connection.
215  */
216 void
217 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c);
218
219 /**
220  * Mark a path as no longer valid for this tunnel: has been tried and failed.
221  *
222  * @param t Tunnel to update.
223  * @param path Invalid path to remove. Is destroyed after removal.
224  */
225 void
226 GMT_remove_path (struct MeshTunnel3 *t, struct MeshPeerPath *path);
227
228 /**
229  * Remove a connection from a tunnel.
230  *
231  * @param t Tunnel.
232  * @param c Connection.
233  */
234 void
235 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c);
236
237 /**
238  * Add a channel to a tunnel.
239  *
240  * @param t Tunnel.
241  * @param ch Channel.
242  */
243 void
244 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch);
245
246 /**
247  * Remove a channel from a tunnel.
248  *
249  * @param t Tunnel.
250  * @param ch Channel.
251  */
252 void
253 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch);
254
255 /**
256  * Search for a channel by global ID.
257  *
258  * @param t Tunnel containing the channel.
259  * @param chid Public channel number.
260  *
261  * @return channel handler, NULL if doesn't exist
262  */
263 struct MeshChannel *
264 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid);
265
266 /**
267  * Decrypt and demultiplex by message type. Call appropriate handler
268  * for a message
269  * towards a channel of a local tunnel.
270  *
271  * @param t Tunnel this message came on.
272  * @param msg Message header.
273  */
274 void
275 GMT_handle_encrypted (struct MeshTunnel3 *t,
276                       const struct GNUNET_MESH_Encrypted *msg);
277
278 /**
279  * Demultiplex an encapsulated KX message by message type.
280  *
281  * @param t Tunnel on which the message came.
282  * @param message KX message itself.
283  */
284 void
285 GMT_handle_kx (struct MeshTunnel3 *t,
286                const struct GNUNET_MessageHeader *message);
287
288 /**
289  * @brief Use the given path for the tunnel.
290  * Update the next and prev hops (and RCs).
291  * (Re)start the path refresh in case the tunnel is locally owned.
292  *
293  * @param t Tunnel to update.
294  * @param p Path to use.
295  *
296  * @return Connection created.
297  */
298 struct MeshConnection *
299 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p);
300
301 /**
302  * Count established (ready) connections of a tunnel.
303  *
304  * @param t Tunnel on which to count.
305  *
306  * @return Number of connections.
307  */
308 unsigned int
309 GMT_count_connections (struct MeshTunnel3 *t);
310
311 /**
312  * Count channels of a tunnel.
313  *
314  * @param t Tunnel on which to count.
315  *
316  * @return Number of channels.
317  */
318 unsigned int
319 GMT_count_channels (struct MeshTunnel3 *t);
320
321 /**
322  * Get the connectivity state of a tunnel.
323  *
324  * @param t Tunnel.
325  *
326  * @return Tunnel's connectivity state.
327  */
328 enum MeshTunnel3CState
329 GMT_get_cstate (struct MeshTunnel3 *t);
330
331 /**
332  * Get the encryption state of a tunnel.
333  *
334  * @param t Tunnel.
335  *
336  * @return Tunnel's encryption state.
337  */
338 enum MeshTunnel3EState
339 GMT_get_estate (struct MeshTunnel3 *t);
340
341 /**
342  * Get the maximum buffer space for a tunnel towards a local client.
343  *
344  * @param t Tunnel.
345  *
346  * @return Biggest buffer space offered by any channel in the tunnel.
347  */
348 unsigned int
349 GMT_get_channels_buffer (struct MeshTunnel3 *t);
350
351 /**
352  * Get the total buffer space for a tunnel for P2P traffic.
353  *
354  * @param t Tunnel.
355  *
356  * @return Buffer space offered by all connections in the tunnel.
357  */
358 unsigned int
359 GMT_get_connections_buffer (struct MeshTunnel3 *t);
360
361 /**
362  * Get the tunnel's destination.
363  *
364  * @param t Tunnel.
365  *
366  * @return ID of the destination peer.
367  */
368 const struct GNUNET_PeerIdentity *
369 GMT_get_destination (struct MeshTunnel3 *t);
370
371 /**
372  * Get the tunnel's next free Channel ID.
373  *
374  * @param t Tunnel.
375  *
376  * @return ID of a channel free to use.
377  */
378 MESH_ChannelNumber
379 GMT_get_next_chid (struct MeshTunnel3 *t);
380
381 /**
382  * Send ACK on one or more channels due to buffer in connections.
383  *
384  * @param t Channel which has some free buffer space.
385  */
386 void
387 GMT_unchoke_channels (struct MeshTunnel3 *t);
388
389 /**
390  * Send ACK on one or more connections due to buffer space to the client.
391  *
392  * Iterates all connections of the tunnel and sends ACKs appropriately.
393  *
394  * @param t Tunnel which has some free buffer space.
395  */
396 void
397 GMT_send_connection_acks (struct MeshTunnel3 *t);
398
399 /**
400  * Cancel a previously sent message while it's in the queue.
401  *
402  * ONLY can be called before the continuation given to the send function
403  * is called. Once the continuation is called, the message is no longer in the
404  * queue.
405  *
406  * @param q Handle to the queue.
407  */
408 void
409 GMT_cancel (struct MeshTunnel3Queue *q);
410
411 /**
412  * Sends an already built message on a tunnel, encrypting it and
413  * choosing the best connection.
414  *
415  * @param message Message to send. Function modifies it.
416  * @param t Tunnel on which this message is transmitted.
417  * @param force Force the tunnel to take the message (buffer overfill).
418  * @param cont Continuation to call once message is really sent.
419  * @param cont_cls Closure for @c cont.
420  *
421  * @return Handle to cancel message. NULL if @c cont is NULL.
422  */
423 struct MeshTunnel3Queue *
424 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
425                            struct MeshTunnel3 *t, int force,
426                            GMT_sent cont, void *cont_cls);
427
428 /**
429  * Is the tunnel directed towards the local peer?
430  *
431  * @param t Tunnel.
432  *
433  * @return #GNUNET_YES if it is loopback.
434  */
435 int
436 GMT_is_loopback (const struct MeshTunnel3 *t);
437
438 /**
439  * Is the tunnel using this path already?
440  *
441  * @param t Tunnel.
442  * @param p Path.
443  *
444  * @return #GNUNET_YES a connection uses this path.
445  */
446 int
447 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p);
448
449 /**
450  * Get a cost of a path for a tunnel considering existing connections.
451  *
452  * @param t Tunnel.
453  * @param path Candidate path.
454  *
455  * @return Cost of the path (path length + number of overlapping nodes)
456  */
457 unsigned int
458 GMT_get_path_cost (const struct MeshTunnel3 *t,
459                    const struct MeshPeerPath *path);
460
461 /**
462  * Get the static string for the peer this tunnel is directed.
463  *
464  * @param t Tunnel.
465  *
466  * @return Static string the destination peer's ID.
467  */
468 const char *
469 GMT_2s (const struct MeshTunnel3 *t);
470
471 /**
472  * Log all possible info about the tunnel state.
473  *
474  * @param t Tunnel to debug.
475  */
476 void
477 GMT_debug (const struct MeshTunnel3 *t);
478
479 void
480 GMT_iterate_all (void *cls, GNUNET_CONTAINER_PeerMapIterator iter);
481
482 unsigned int
483 GMT_count_all (void);
484
485 void
486 GMT_iterate_connections (struct MeshTunnel3 *t, GMT_conn_iter iter, void *cls);
487
488 void
489 GMT_iterate_channels (struct MeshTunnel3 *t, GMT_chan_iter iter, void *cls);
490
491 #if 0                           /* keep Emacsens' auto-indent happy */
492 {
493 #endif
494 #ifdef __cplusplus
495 }
496 #endif
497
498 /* ifndef GNUNET_MESH_SERVICE_TUNNEL_H */
499 #endif
500 /* end of gnunet-mesh-service_tunnel.h */