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