- begin work on enhanced multipart receiving
[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  * Mark a path as no longer valid for this tunnel: has been tried and failed.
218  *
219  * @param t Tunnel to update.
220  * @param path Invalid path to remove. Is destroyed after removal.
221  */
222 void
223 GMT_remove_path (struct MeshTunnel3 *t, struct MeshPeerPath *path);
224
225 /**
226  * Remove a connection from a tunnel.
227  *
228  * @param t Tunnel.
229  * @param c Connection.
230  */
231 void
232 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c);
233
234 /**
235  * Add a channel to a tunnel.
236  *
237  * @param t Tunnel.
238  * @param ch Channel.
239  */
240 void
241 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch);
242
243 /**
244  * Remove a channel from a tunnel.
245  *
246  * @param t Tunnel.
247  * @param ch Channel.
248  */
249 void
250 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch);
251
252 /**
253  * Search for a channel by global ID.
254  *
255  * @param t Tunnel containing the channel.
256  * @param chid Public channel number.
257  *
258  * @return channel handler, NULL if doesn't exist
259  */
260 struct MeshChannel *
261 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid);
262
263 /**
264  * Decrypt and demultiplex by message type. Call appropriate handler
265  * for a message
266  * towards a channel of a local tunnel.
267  *
268  * @param t Tunnel this message came on.
269  * @param msg Message header.
270  */
271 void
272 GMT_handle_encrypted (struct MeshTunnel3 *t,
273                       const struct GNUNET_MESH_Encrypted *msg);
274
275 /**
276  * Demultiplex an encapsulated KX message by message type.
277  *
278  * @param t Tunnel on which the message came.
279  * @param message KX message itself.
280  */
281 void
282 GMT_handle_kx (struct MeshTunnel3 *t,
283                const struct GNUNET_MessageHeader *message);
284
285 /**
286  * @brief Use the given path for the tunnel.
287  * Update the next and prev hops (and RCs).
288  * (Re)start the path refresh in case the tunnel is locally owned.
289  *
290  * @param t Tunnel to update.
291  * @param p Path to use.
292  *
293  * @return Connection created.
294  */
295 struct MeshConnection *
296 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p);
297
298 /**
299  * Count established (ready) connections of a tunnel.
300  *
301  * @param t Tunnel on which to count.
302  *
303  * @return Number of connections.
304  */
305 unsigned int
306 GMT_count_connections (struct MeshTunnel3 *t);
307
308 /**
309  * Count channels of a tunnel.
310  *
311  * @param t Tunnel on which to count.
312  *
313  * @return Number of channels.
314  */
315 unsigned int
316 GMT_count_channels (struct MeshTunnel3 *t);
317
318 /**
319  * Get the connectivity state of a tunnel.
320  *
321  * @param t Tunnel.
322  *
323  * @return Tunnel's connectivity state.
324  */
325 enum MeshTunnel3CState
326 GMT_get_cstate (struct MeshTunnel3 *t);
327
328 /**
329  * Get the maximum buffer space for a tunnel towards a local client.
330  *
331  * @param t Tunnel.
332  *
333  * @return Biggest buffer space offered by any channel in the tunnel.
334  */
335 unsigned int
336 GMT_get_channels_buffer (struct MeshTunnel3 *t);
337
338 /**
339  * Get the total buffer space for a tunnel for P2P traffic.
340  *
341  * @param t Tunnel.
342  *
343  * @return Buffer space offered by all connections in the tunnel.
344  */
345 unsigned int
346 GMT_get_connections_buffer (struct MeshTunnel3 *t);
347
348 /**
349  * Get the tunnel's destination.
350  *
351  * @param t Tunnel.
352  *
353  * @return ID of the destination peer.
354  */
355 const struct GNUNET_PeerIdentity *
356 GMT_get_destination (struct MeshTunnel3 *t);
357
358 /**
359  * Get the tunnel's next free Channel ID.
360  *
361  * @param t Tunnel.
362  *
363  * @return ID of a channel free to use.
364  */
365 MESH_ChannelNumber
366 GMT_get_next_chid (struct MeshTunnel3 *t);
367
368 /**
369  * Send ACK on one or more channels due to buffer in connections.
370  *
371  * @param t Channel which has some free buffer space.
372  */
373 void
374 GMT_unchoke_channels (struct MeshTunnel3 *t);
375
376 /**
377  * Send ACK on one or more connections due to buffer space to the client.
378  *
379  * Iterates all connections of the tunnel and sends ACKs appropriately.
380  *
381  * @param t Tunnel which has some free buffer space.
382  */
383 void
384 GMT_send_connection_acks (struct MeshTunnel3 *t);
385
386 /**
387  * Cancel a previously sent message while it's in the queue.
388  *
389  * ONLY can be called before the continuation given to the send function
390  * is called. Once the continuation is called, the message is no longer in the
391  * queue.
392  *
393  * @param q Handle to the queue.
394  */
395 void
396 GMT_cancel (struct MeshTunnel3Queue *q);
397
398 /**
399  * Sends an already built message on a tunnel, encrypting it and
400  * choosing the best connection.
401  *
402  * @param message Message to send. Function modifies it.
403  * @param t Tunnel on which this message is transmitted.
404  * @param force Force the tunnel to take the message (buffer overfill).
405  * @param cont Continuation to call once message is really sent.
406  * @param cont_cls Closure for @c cont.
407  *
408  * @return Handle to cancel message. NULL if @c cont is NULL.
409  */
410 struct MeshTunnel3Queue *
411 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
412                            struct MeshTunnel3 *t, int force,
413                            GMT_sent cont, void *cont_cls);
414
415 /**
416  * Is the tunnel directed towards the local peer?
417  *
418  * @param t Tunnel.
419  *
420  * @return #GNUNET_YES if it is loopback.
421  */
422 int
423 GMT_is_loopback (const struct MeshTunnel3 *t);
424
425 /**
426  * Is the tunnel using this path already?
427  *
428  * @param t Tunnel.
429  * @param p Path.
430  *
431  * @return #GNUNET_YES a connection uses this path.
432  */
433 int
434 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p);
435
436 /**
437  * Get a cost of a path for a tunnel considering existing connections.
438  *
439  * @param t Tunnel.
440  * @param path Candidate path.
441  *
442  * @return Cost of the path (path length + number of overlapping nodes)
443  */
444 unsigned int
445 GMT_get_path_cost (const struct MeshTunnel3 *t,
446                    const struct MeshPeerPath *path);
447
448 /**
449  * Get the static string for the peer this tunnel is directed.
450  *
451  * @param t Tunnel.
452  *
453  * @return Static string the destination peer's ID.
454  */
455 const char *
456 GMT_2s (const struct MeshTunnel3 *t);
457
458 /**
459  * Log all possible info about the tunnel state.
460  *
461  * @param t Tunnel to debug.
462  */
463 void
464 GMT_debug (const struct MeshTunnel3 *t);
465
466 #if 0                           /* keep Emacsens' auto-indent happy */
467 {
468 #endif
469 #ifdef __cplusplus
470 }
471 #endif
472
473 /* ifndef GNUNET_MESH_SERVICE_TUNNEL_H */
474 #endif
475 /* end of gnunet-mesh-service_tunnel.h */