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