- hunting 3214
[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
127 /******************************************************************************/
128 /********************************    API    ***********************************/
129 /******************************************************************************/
130
131 /**
132  * Initialize tunnel subsystem.
133  *
134  * @param c Configuration handle.
135  * @param key ECC private key, to derive all other keys and do crypto.
136  */
137 void
138 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
139           const struct GNUNET_CRYPTO_EddsaPrivateKey *key);
140
141 /**
142  * Shut down the tunnel subsystem.
143  */
144 void
145 GMT_shutdown (void);
146
147 /**
148  * Create a tunnel.
149  *
150  * @param destination Peer this tunnel is towards.
151  */
152 struct MeshTunnel3 *
153 GMT_new (struct MeshPeer *destination);
154
155 /**
156  * Tunnel is empty: destroy it.
157  *
158  * Notifies all connections about the destruction.
159  *
160  * @param t Tunnel to destroy.
161  */
162 void
163 GMT_destroy_empty (struct MeshTunnel3 *t);
164
165 /**
166  * Destroy tunnel if empty (no more channels).
167  *
168  * @param t Tunnel to destroy if empty.
169  */
170 void
171 GMT_destroy_if_empty (struct MeshTunnel3 *t);
172
173 /**
174  * Destroy the tunnel.
175  *
176  * This function does not generate any warning traffic to clients or peers.
177  *
178  * Tasks:
179  * Cancel messages belonging to this tunnel queued to neighbors.
180  * Free any allocated resources linked to the tunnel.
181  *
182  * @param t The tunnel to destroy.
183  */
184 void
185 GMT_destroy (struct MeshTunnel3 *t);
186
187
188 /**
189  * Change the tunnel's connection state.
190  *
191  * @param t Tunnel whose connection state to change.
192  * @param cstate New connection state.
193  */
194 void
195 GMT_change_cstate (struct MeshTunnel3* t, enum MeshTunnel3CState cstate);
196
197
198 /**
199  * Change the tunnel encryption state.
200  *
201  * @param t Tunnel whose encryption state to change.
202  * @param state New encryption state.
203  */
204 void
205 GMT_change_estate (struct MeshTunnel3* t, enum MeshTunnel3EState state);
206
207 /**
208  * Add a connection to a tunnel.
209  *
210  * @param t Tunnel.
211  * @param c Connection.
212  */
213 void
214 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c);
215
216 /**
217  * Remove a connection from a tunnel.
218  *
219  * @param t Tunnel.
220  * @param c Connection.
221  */
222 void
223 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c);
224
225 /**
226  * Add a channel to a tunnel.
227  *
228  * @param t Tunnel.
229  * @param ch Channel.
230  */
231 void
232 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch);
233
234 /**
235  * Remove a channel from a tunnel.
236  *
237  * @param t Tunnel.
238  * @param ch Channel.
239  */
240 void
241 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch);
242
243 /**
244  * Search for a channel by global ID.
245  *
246  * @param t Tunnel containing the channel.
247  * @param chid Public channel number.
248  *
249  * @return channel handler, NULL if doesn't exist
250  */
251 struct MeshChannel *
252 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid);
253
254 /**
255  * Decrypt and demultiplex by message type. Call appropriate handler
256  * for a message
257  * towards a channel of a local tunnel.
258  *
259  * @param t Tunnel this message came on.
260  * @param msg Message header.
261  */
262 void
263 GMT_handle_encrypted (struct MeshTunnel3 *t,
264                       const struct GNUNET_MESH_Encrypted *msg);
265
266 /**
267  * Demultiplex an encapsulated KX message by message type.
268  *
269  * @param t Tunnel on which the message came.
270  * @param message KX message itself.
271  */
272 void
273 GMT_handle_kx (struct MeshTunnel3 *t,
274                const struct GNUNET_MessageHeader *message);
275
276 /**
277  * @brief Use the given path for the tunnel.
278  * Update the next and prev hops (and RCs).
279  * (Re)start the path refresh in case the tunnel is locally owned.
280  *
281  * @param t Tunnel to update.
282  * @param p Path to use.
283  *
284  * @return Connection created.
285  */
286 struct MeshConnection *
287 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p);
288
289 /**
290  * Count established (ready) connections of a tunnel.
291  *
292  * @param t Tunnel on which to count.
293  *
294  * @return Number of connections.
295  */
296 unsigned int
297 GMT_count_connections (struct MeshTunnel3 *t);
298
299 /**
300  * Count channels of a tunnel.
301  *
302  * @param t Tunnel on which to count.
303  *
304  * @return Number of channels.
305  */
306 unsigned int
307 GMT_count_channels (struct MeshTunnel3 *t);
308
309 /**
310  * Get the connectivity state of a tunnel.
311  *
312  * @param t Tunnel.
313  *
314  * @return Tunnel's connectivity state.
315  */
316 enum MeshTunnel3CState
317 GMT_get_cstate (struct MeshTunnel3 *t);
318
319 /**
320  * Get the maximum buffer space for a tunnel towards a local client.
321  *
322  * @param t Tunnel.
323  *
324  * @return Biggest buffer space offered by any channel in the tunnel.
325  */
326 unsigned int
327 GMT_get_channels_buffer (struct MeshTunnel3 *t);
328
329 /**
330  * Get the total buffer space for a tunnel for P2P traffic.
331  *
332  * @param t Tunnel.
333  *
334  * @return Buffer space offered by all connections in the tunnel.
335  */
336 unsigned int
337 GMT_get_connections_buffer (struct MeshTunnel3 *t);
338
339 /**
340  * Get the tunnel's destination.
341  *
342  * @param t Tunnel.
343  *
344  * @return ID of the destination peer.
345  */
346 const struct GNUNET_PeerIdentity *
347 GMT_get_destination (struct MeshTunnel3 *t);
348
349 /**
350  * Get the tunnel's next free Channel ID.
351  *
352  * @param t Tunnel.
353  *
354  * @return ID of a channel free to use.
355  */
356 MESH_ChannelNumber
357 GMT_get_next_chid (struct MeshTunnel3 *t);
358
359 /**
360  * Send ACK on one or more channels due to buffer in connections.
361  *
362  * @param t Channel which has some free buffer space.
363  */
364 void
365 GMT_unchoke_channels (struct MeshTunnel3 *t);
366
367 /**
368  * Send ACK on one or more connections due to buffer space to the client.
369  *
370  * Iterates all connections of the tunnel and sends ACKs appropriately.
371  *
372  * @param t Tunnel which has some free buffer space.
373  */
374 void
375 GMT_send_connection_acks (struct MeshTunnel3 *t);
376
377 /**
378  * Cancel a previously sent message while it's in the queue.
379  *
380  * ONLY can be called before the continuation given to the send function
381  * is called. Once the continuation is called, the message is no longer in the
382  * queue.
383  *
384  * @param q Handle to the queue.
385  */
386 void
387 GMT_cancel (struct MeshTunnel3Queue *q);
388
389 /**
390  * Sends an already built message on a tunnel, encrypting it and
391  * choosing the best connection.
392  *
393  * @param message Message to send. Function modifies it.
394  * @param t Tunnel on which this message is transmitted.
395  * @param force Force the tunnel to take the message (buffer overfill).
396  * @param cont Continuation to call once message is really sent.
397  * @param cont_cls Closure for @c cont.
398  *
399  * @return Handle to cancel message. NULL if @c cont is NULL.
400  */
401 struct MeshTunnel3Queue *
402 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
403                            struct MeshTunnel3 *t, int force,
404                            GMT_sent cont, void *cont_cls);
405
406 /**
407  * Is the tunnel directed towards the local peer?
408  *
409  * @param t Tunnel.
410  *
411  * @return #GNUNET_YES if it is loopback.
412  */
413 int
414 GMT_is_loopback (const struct MeshTunnel3 *t);
415
416 /**
417  * Is the tunnel using this path already?
418  *
419  * @param t Tunnel.
420  * @param p Path.
421  *
422  * @return #GNUNET_YES a connection uses this path.
423  */
424 int
425 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p);
426
427 /**
428  * Get a cost of a path for a tunnel considering existing connections.
429  *
430  * @param t Tunnel.
431  * @param path Candidate path.
432  *
433  * @return Cost of the path (path length + number of overlapping nodes)
434  */
435 unsigned int
436 GMT_get_path_cost (const struct MeshTunnel3 *t,
437                    const struct MeshPeerPath *path);
438
439 /**
440  * Get the static string for the peer this tunnel is directed.
441  *
442  * @param t Tunnel.
443  *
444  * @return Static string the destination peer's ID.
445  */
446 const char *
447 GMT_2s (const struct MeshTunnel3 *t);
448
449 #if 0                           /* keep Emacsens' auto-indent happy */
450 {
451 #endif
452 #ifdef __cplusplus
453 }
454 #endif
455
456 /* ifndef GNUNET_MESH_SERVICE_TUNNEL_H */
457 #endif
458 /* end of gnunet-mesh-service_tunnel.h */