- fix encryption/decryption visisbility
[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 msgh 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  * Cache a message to be sent once tunnel is online.
217  *
218  * @param t Tunnel to hold the message.
219  * @param ch Channel the message is about.
220  * @param msg Message itself (copy will be made).
221  * @param fwd Is this fwd?
222  */
223 void
224 GMT_queue_data (struct MeshTunnel3 *t,
225                 struct MeshChannel *ch,
226                 struct GNUNET_MessageHeader *msg,
227                 int fwd);
228
229 /**
230  * Send all cached messages that we can, tunnel is online.
231  *
232  * @param t Tunnel that holds the messages.
233  * @param fwd Is this fwd?
234  */
235 void
236 GMT_send_queued_data (struct MeshTunnel3 *t, int fwd);
237
238 /**
239  * @brief Use the given path for the tunnel.
240  * Update the next and prev hops (and RCs).
241  * (Re)start the path refresh in case the tunnel is locally owned.
242  *
243  * @param t Tunnel to update.
244  * @param p Path to use.
245  *
246  * @return Connection created.
247  */
248 struct MeshConnection *
249 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p);
250
251 /**
252  * Count established (ready) connections of a tunnel.
253  *
254  * @param t Tunnel on which to count.
255  *
256  * @return Number of connections.
257  */
258 unsigned int
259 GMT_count_connections (struct MeshTunnel3 *t);
260
261 /**
262  * Count channels of a tunnel.
263  *
264  * @param t Tunnel on which to count.
265  *
266  * @return Number of channels.
267  */
268 unsigned int
269 GMT_count_channels (struct MeshTunnel3 *t);
270
271 /**
272  * Get the state of a tunnel.
273  *
274  * @param t Tunnel.
275  *
276  * @return Tunnel's state.
277  */
278 enum MeshTunnel3State
279 GMT_get_state (struct MeshTunnel3 *t);
280
281 /**
282  * Get the total buffer space for a tunnel.
283  *
284  * @param t Tunnel.
285  * @param fwd Is this for FWD traffic?
286  *
287  * @return Buffer space offered by all connections in the tunnel.
288  */
289 unsigned int
290 GMT_get_buffer (struct MeshTunnel3 *t, int fwd);
291
292 /**
293  * Get the tunnel's destination.
294  *
295  * @param t Tunnel.
296  *
297  * @return ID of the destination peer.
298  */
299 const struct GNUNET_PeerIdentity *
300 GMT_get_destination (struct MeshTunnel3 *t);
301
302 /**
303  * Get the tunnel's next free Channel ID.
304  *
305  * @param t Tunnel.
306  *
307  * @return ID of a channel free to use.
308  */
309 MESH_ChannelNumber
310 GMT_get_next_chid (struct MeshTunnel3 *t);
311
312 /**
313  * Sends an already built message on a tunnel, encrypting it and
314  * choosing the best connection.
315  *
316  * @param message Message to send. Function modifies it.
317  * @param t Tunnel on which this message is transmitted.
318  * @param ch Channel on which this message is transmitted.
319  * @param fwd Is this a fwd message?
320  */
321 void
322 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
323                            struct MeshTunnel3 *t,
324                            struct MeshChannel *ch,
325                            int fwd);
326
327 /**
328  * Is the tunnel directed towards the local peer?
329  *
330  * @param t Tunnel.
331  *
332  * @return GNUNET_YES if it is loopback.
333  */
334 int
335 GMT_is_loopback (const struct MeshTunnel3 *t);
336
337 /**
338  * Is the tunnel using this path already?
339  *
340  * @param t Tunnel.
341  * @param p Path.
342  *
343  * @return GNUNET_YES a connection uses this path.
344  */
345 int
346 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p);
347
348 /**
349  * Get a cost of a path for a tunnel considering existing connections.
350  *
351  * @param t Tunnel.
352  * @param path Candidate path.
353  *
354  * @return Cost of the path (path length + number of overlapping nodes)
355  */
356 unsigned int
357 GMT_get_path_cost (const struct MeshTunnel3 *t,
358                    const struct MeshPeerPath *path);
359
360 /**
361  * Get the static string for the peer this tunnel is directed.
362  *
363  * @param t Tunnel.
364  *
365  * @return Static string the destination peer's ID.
366  */
367 const char *
368 GMT_2s (const struct MeshTunnel3 *t);
369
370 #if 0                           /* keep Emacsens' auto-indent happy */
371 {
372 #endif
373 #ifdef __cplusplus
374 }
375 #endif
376
377 /* ifndef GNUNET_MESH_SERVICE_TUNNEL_H */
378 #endif
379 /* end of gnunet-mesh-service_tunnel.h */