- expose appropriate functions in API
[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
83
84 /******************************************************************************/
85 /********************************    API    ***********************************/
86 /******************************************************************************/
87
88 /**
89  * Initialize tunnel subsystem.
90  *
91  * @param c Configuration handle.
92  * @param key ECC private key, to derive all other keys and do crypto.
93  */
94 void
95 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
96           const struct GNUNET_CRYPTO_EddsaPrivateKey *key);
97
98 /**
99  * Shut down the tunnel subsystem.
100  */
101 void
102 GMT_shutdown (void);
103
104 /**
105  * Create a tunnel.
106  *
107  * @param destination Peer this tunnel is towards.
108  */
109 struct MeshTunnel3 *
110 GMT_new (struct MeshPeer *destination);
111
112 /**
113  * Tunnel is empty: destroy it.
114  *
115  * Notifies all connections about the destruction.
116  *
117  * @param t Tunnel to destroy.
118  */
119 void
120 GMT_destroy_empty (struct MeshTunnel3 *t);
121
122 /**
123  * Destroy tunnel if empty (no more channels).
124  *
125  * @param t Tunnel to destroy if empty.
126  */
127 void
128 GMT_destroy_if_empty (struct MeshTunnel3 *t);
129
130 /**
131  * Destroy the tunnel.
132  *
133  * This function does not generate any warning traffic to clients or peers.
134  *
135  * Tasks:
136  * Cancel messages belonging to this tunnel queued to neighbors.
137  * Free any allocated resources linked to the tunnel.
138  *
139  * @param t The tunnel to destroy.
140  */
141 void
142 GMT_destroy (struct MeshTunnel3 *t);
143
144 /**
145  * Change the tunnel state.
146  *
147  * @param t Tunnel whose state to change.
148  * @param state New state.
149  */
150 void
151 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnel3State state);
152
153 /**
154  * Add a connection to a tunnel.
155  *
156  * @param t Tunnel.
157  * @param c Connection.
158  */
159 void
160 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c);
161
162 /**
163  * Remove a connection from a tunnel.
164  *
165  * @param t Tunnel.
166  * @param c Connection.
167  */
168 void
169 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c);
170
171 /**
172  * Add a channel to a tunnel.
173  *
174  * @param t Tunnel.
175  * @param ch Channel.
176  */
177 void
178 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch);
179
180 /**
181  * Remove a channel from a tunnel.
182  *
183  * @param t Tunnel.
184  * @param ch Channel.
185  */
186 void
187 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch);
188
189 /**
190  * Search for a channel by global ID.
191  *
192  * @param t Tunnel containing the channel.
193  * @param chid Public channel number.
194  *
195  * @return channel handler, NULL if doesn't exist
196  */
197 struct MeshChannel *
198 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid);
199
200
201 /**
202  * Cache a message to be sent once tunnel is online.
203  *
204  * @param t Tunnel to hold the message.
205  * @param ch Channel the message is about.
206  * @param msg Message itself (copy will be made).
207  * @param fwd Is this fwd?
208  */
209 void
210 GMT_queue_data (struct MeshTunnel3 *t,
211                 struct MeshChannel *ch,
212                 struct GNUNET_MessageHeader *msg,
213                 int fwd);
214
215 /**
216  * Send all cached messages that we can, tunnel is online.
217  *
218  * @param t Tunnel that holds the messages.
219  * @param fwd Is this fwd?
220  */
221 void
222 GMT_send_queued_data (struct MeshTunnel3 *t, int fwd);
223
224 /**
225  * @brief Use the given path for the tunnel.
226  * Update the next and prev hops (and RCs).
227  * (Re)start the path refresh in case the tunnel is locally owned.
228  *
229  * @param t Tunnel to update.
230  * @param p Path to use.
231  *
232  * @return Connection created.
233  */
234 struct MeshConnection *
235 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p);
236
237 /**
238  * Count established (ready) connections of a tunnel.
239  *
240  * @param t Tunnel on which to count.
241  *
242  * @return Number of connections.
243  */
244 unsigned int
245 GMT_count_connections (struct MeshTunnel3 *t);
246
247 /**
248  * Count channels of a tunnel.
249  *
250  * @param t Tunnel on which to count.
251  *
252  * @return Number of channels.
253  */
254 unsigned int
255 GMT_count_channels (struct MeshTunnel3 *t);
256
257 /**
258  * Get the state of a tunnel.
259  *
260  * @param t Tunnel.
261  *
262  * @return Tunnel's state.
263  */
264 enum MeshTunnel3State
265 GMT_get_state (struct MeshTunnel3 *t);
266
267 /**
268  * Get the total buffer space for a tunnel.
269  *
270  * @param t Tunnel.
271  * @param fwd Is this for FWD traffic?
272  *
273  * @return Buffer space offered by all connections in the tunnel.
274  */
275 unsigned int
276 GMT_get_buffer (struct MeshTunnel3 *t, int fwd);
277
278 /**
279  * Get the tunnel's destination.
280  *
281  * @param t Tunnel.
282  *
283  * @return ID of the destination peer.
284  */
285 const struct GNUNET_PeerIdentity *
286 GMT_get_destination (struct MeshTunnel3 *t);
287
288 /**
289  * Get the tunnel's next free Channel ID.
290  *
291  * @param t Tunnel.
292  *
293  * @return ID of a channel free to use.
294  */
295 MESH_ChannelNumber
296 GMT_get_next_chid (struct MeshTunnel3 *t);
297
298 /**
299  * Sends an already built message on a tunnel, encrypting it and
300  * choosing the best connection.
301  *
302  * @param message Message to send. Function modifies it.
303  * @param t Tunnel on which this message is transmitted.
304  * @param ch Channel on which this message is transmitted.
305  * @param fwd Is this a fwd message?
306  */
307 void
308 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
309                            struct MeshTunnel3 *t,
310                            struct MeshChannel *ch,
311                            int fwd);
312
313 /**
314  * Is the tunnel directed towards the local peer?
315  *
316  * @param t Tunnel.
317  *
318  * @return GNUNET_YES if it is loopback.
319  */
320 int
321 GMT_is_loopback (const struct MeshTunnel3 *t);
322
323 /**
324  * Get the static string for the peer this tunnel is directed.
325  *
326  * @param t Tunnel.
327  *
328  * @return Static string the destination peer's ID.
329  */
330 const char *
331 GMT_2s (const struct MeshTunnel3 *t);
332
333 #if 0                           /* keep Emacsens' auto-indent happy */
334 {
335 #endif
336 #ifdef __cplusplus
337 }
338 #endif
339
340 /* ifndef GNUNET_MESH_SERVICE_TUNNEL_H */
341 #endif
342 /* end of gnunet-mesh-service_tunnel.h */