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