-only notify AFTER sending is really close to finished, not before
[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 states a tunnel can be in.
45  */
46 enum MeshTunnel3State
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      * Ephemeral key sent, waiting for peer's key.
65      */
66   MESH_TUNNEL3_KEY_SENT,
67
68     /**
69      * Peer connected and ready to accept data.
70      */
71   MESH_TUNNEL3_READY,
72
73     /**
74      * Peer connected previosly but not responding.
75      */
76   MESH_TUNNEL3_RECONNECTING,
77
78     /**
79      * New ephemeral key and ping sent, waiting for pong.
80      * This means that we DO have the peer's ephemeral key, otherwise the
81      * state would be KEY_SENT.
82      */
83   MESH_TUNNEL3_REKEY,
84 };
85
86 /**
87  * Struct containing all information regarding a given peer
88  */
89 struct MeshTunnel3;
90
91
92 #include "gnunet-service-mesh_channel.h"
93 #include "gnunet-service-mesh_connection.h"
94 #include "gnunet-service-mesh_peer.h"
95
96 /**
97  * Handle for messages queued but not yet sent.
98  */
99 struct MeshTunnel3Queue;
100
101 /**
102  * Callback called when a queued message is sent.
103  *
104  * @param cls Closure.
105  * @param t Tunnel this message was on.
106  * @param type Type of message sent.
107  * @param size Size of the message.
108  */
109 typedef void (*GMT_sent) (void *cls,
110                           struct MeshTunnel3 *t,
111                           struct MeshTunnel3Queue *q,
112                           uint16_t type, size_t size);
113
114
115 /******************************************************************************/
116 /********************************    API    ***********************************/
117 /******************************************************************************/
118
119 /**
120  * Initialize tunnel subsystem.
121  *
122  * @param c Configuration handle.
123  * @param key ECC private key, to derive all other keys and do crypto.
124  */
125 void
126 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
127           const struct GNUNET_CRYPTO_EddsaPrivateKey *key);
128
129 /**
130  * Shut down the tunnel subsystem.
131  */
132 void
133 GMT_shutdown (void);
134
135 /**
136  * Create a tunnel.
137  *
138  * @param destination Peer this tunnel is towards.
139  */
140 struct MeshTunnel3 *
141 GMT_new (struct MeshPeer *destination);
142
143 /**
144  * Tunnel is empty: destroy it.
145  *
146  * Notifies all connections about the destruction.
147  *
148  * @param t Tunnel to destroy.
149  */
150 void
151 GMT_destroy_empty (struct MeshTunnel3 *t);
152
153 /**
154  * Destroy tunnel if empty (no more channels).
155  *
156  * @param t Tunnel to destroy if empty.
157  */
158 void
159 GMT_destroy_if_empty (struct MeshTunnel3 *t);
160
161 /**
162  * Destroy the tunnel.
163  *
164  * This function does not generate any warning traffic to clients or peers.
165  *
166  * Tasks:
167  * Cancel messages belonging to this tunnel queued to neighbors.
168  * Free any allocated resources linked to the tunnel.
169  *
170  * @param t The tunnel to destroy.
171  */
172 void
173 GMT_destroy (struct MeshTunnel3 *t);
174
175 /**
176  * Change the tunnel state.
177  *
178  * @param t Tunnel whose state to change.
179  * @param state New state.
180  */
181 void
182 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnel3State state);
183
184 /**
185  * Add a connection to a tunnel.
186  *
187  * @param t Tunnel.
188  * @param c Connection.
189  */
190 void
191 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c);
192
193 /**
194  * Remove a connection from a tunnel.
195  *
196  * @param t Tunnel.
197  * @param c Connection.
198  */
199 void
200 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c);
201
202 /**
203  * Add a channel to a tunnel.
204  *
205  * @param t Tunnel.
206  * @param ch Channel.
207  */
208 void
209 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch);
210
211 /**
212  * Remove a channel from a tunnel.
213  *
214  * @param t Tunnel.
215  * @param ch Channel.
216  */
217 void
218 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch);
219
220 /**
221  * Search for a channel by global ID.
222  *
223  * @param t Tunnel containing the channel.
224  * @param chid Public channel number.
225  *
226  * @return channel handler, NULL if doesn't exist
227  */
228 struct MeshChannel *
229 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid);
230
231 /**
232  * Decrypt and demultiplex by message type. Call appropriate handler
233  * for a message
234  * towards a channel of a local tunnel.
235  *
236  * @param t Tunnel this message came on.
237  * @param msg Message header.
238  */
239 void
240 GMT_handle_encrypted (struct MeshTunnel3 *t,
241                       const struct GNUNET_MESH_Encrypted *msg);
242
243 /**
244  * Demultiplex an encapsulated KX message by message type.
245  *
246  * @param t Tunnel on which the message came.
247  * @param message KX message itself.
248  */
249 void
250 GMT_handle_kx (struct MeshTunnel3 *t,
251                const struct GNUNET_MessageHeader *message);
252
253 /**
254  * @brief Use the given path for the tunnel.
255  * Update the next and prev hops (and RCs).
256  * (Re)start the path refresh in case the tunnel is locally owned.
257  *
258  * @param t Tunnel to update.
259  * @param p Path to use.
260  *
261  * @return Connection created.
262  */
263 struct MeshConnection *
264 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p);
265
266 /**
267  * Count established (ready) connections of a tunnel.
268  *
269  * @param t Tunnel on which to count.
270  *
271  * @return Number of connections.
272  */
273 unsigned int
274 GMT_count_connections (struct MeshTunnel3 *t);
275
276 /**
277  * Count channels of a tunnel.
278  *
279  * @param t Tunnel on which to count.
280  *
281  * @return Number of channels.
282  */
283 unsigned int
284 GMT_count_channels (struct MeshTunnel3 *t);
285
286 /**
287  * Get the state of a tunnel.
288  *
289  * @param t Tunnel.
290  *
291  * @return Tunnel's state.
292  */
293 enum MeshTunnel3State
294 GMT_get_state (struct MeshTunnel3 *t);
295
296 /**
297  * Get the maximum buffer space for a tunnel towards a local client.
298  *
299  * @param t Tunnel.
300  *
301  * @return Biggest buffer space offered by any channel in the tunnel.
302  */
303 unsigned int
304 GMT_get_channels_buffer (struct MeshTunnel3 *t);
305
306 /**
307  * Get the total buffer space for a tunnel for P2P traffic.
308  *
309  * @param t Tunnel.
310  *
311  * @return Buffer space offered by all connections in the tunnel.
312  */
313 unsigned int
314 GMT_get_connections_buffer (struct MeshTunnel3 *t);
315
316 /**
317  * Get the tunnel's destination.
318  *
319  * @param t Tunnel.
320  *
321  * @return ID of the destination peer.
322  */
323 const struct GNUNET_PeerIdentity *
324 GMT_get_destination (struct MeshTunnel3 *t);
325
326 /**
327  * Get the tunnel's next free Channel ID.
328  *
329  * @param t Tunnel.
330  *
331  * @return ID of a channel free to use.
332  */
333 MESH_ChannelNumber
334 GMT_get_next_chid (struct MeshTunnel3 *t);
335
336 /**
337  * Send ACK on one or more channels due to buffer in connections.
338  *
339  * @param t Channel which has some free buffer space.
340  */
341 void
342 GMT_unchoke_channels (struct MeshTunnel3 *t);
343
344 /**
345  * Send ACK on one or more connections due to buffer space to the client.
346  *
347  * Iterates all connections of the tunnel and sends ACKs appropriately.
348  *
349  * @param t Tunnel which has some free buffer space.
350  */
351 void
352 GMT_send_connection_acks (struct MeshTunnel3 *t);
353
354 /**
355  * Cancel a previously sent message while it's in the queue.
356  *
357  * ONLY can be called before the continuation given to the send function
358  * is called. Once the continuation is called, the message is no longer in the
359  * queue.
360  *
361  * @param q Handle to the queue.
362  */
363 void
364 GMT_cancel (struct MeshTunnel3Queue *q);
365
366 /**
367  * Sends an already built message on a tunnel, encrypting it and
368  * choosing the best connection.
369  *
370  * @param message Message to send. Function modifies it.
371  * @param t Tunnel on which this message is transmitted.
372  * @param ch Channel on which this message is transmitted.
373  * @param fwd Is this a fwd message on @c ch?
374  * @param cont Continuation to call once message is really sent.
375  * @param cls Closure for @c cont.
376  *
377  * @return Handle to cancel message. NULL if @c cont is NULL.
378  */
379 struct MeshTunnel3Queue *
380 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
381                            struct MeshTunnel3 *t,
382                            struct MeshChannel *ch, int fwd,
383                            GMT_sent cont, void *cls);
384
385 /**
386  * Is the tunnel directed towards the local peer?
387  *
388  * @param t Tunnel.
389  *
390  * @return #GNUNET_YES if it is loopback.
391  */
392 int
393 GMT_is_loopback (const struct MeshTunnel3 *t);
394
395 /**
396  * Is the tunnel using this path already?
397  *
398  * @param t Tunnel.
399  * @param p Path.
400  *
401  * @return #GNUNET_YES a connection uses this path.
402  */
403 int
404 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p);
405
406 /**
407  * Get a cost of a path for a tunnel considering existing connections.
408  *
409  * @param t Tunnel.
410  * @param path Candidate path.
411  *
412  * @return Cost of the path (path length + number of overlapping nodes)
413  */
414 unsigned int
415 GMT_get_path_cost (const struct MeshTunnel3 *t,
416                    const struct MeshPeerPath *path);
417
418 /**
419  * Get the static string for the peer this tunnel is directed.
420  *
421  * @param t Tunnel.
422  *
423  * @return Static string the destination peer's ID.
424  */
425 const char *
426 GMT_2s (const struct MeshTunnel3 *t);
427
428 #if 0                           /* keep Emacsens' auto-indent happy */
429 {
430 #endif
431 #ifdef __cplusplus
432 }
433 #endif
434
435 /* ifndef GNUNET_MESH_SERVICE_TUNNEL_H */
436 #endif
437 /* end of gnunet-mesh-service_tunnel.h */