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