optimize mqm_head scans by avoiding constantly scanning over definitively non-ready...
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet-new_tunnels.h
1
2 /*
3      This file is part of GNUnet.
4      Copyright (C) 2001-2017 GNUnet e.V.
5
6      GNUnet is free software; you can redistribute it and/or modify
7      it under the terms of the GNU General Public License as published
8      by the Free Software Foundation; either version 3, or (at your
9      option) any later version.
10
11      GNUnet is distributed in the hope that it will be useful, but
12      WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14      General Public License for more details.
15
16      You should have received a copy of the GNU General Public License
17      along with GNUnet; see the file COPYING.  If not, write to the
18      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19      Boston, MA 02110-1301, USA.
20 */
21
22 /**
23  * @file cadet/gnunet-service-cadet-new_tunnels.h
24  * @brief Information we track per tunnel.
25  * @author Bartlomiej Polot
26  * @author Christian Grothoff
27  */
28 #ifndef GNUNET_SERVICE_CADET_TUNNELS_H
29 #define GNUNET_SERVICE_CADET_TUNNELS_H
30
31 #include "gnunet-service-cadet-new.h"
32 #include "cadet_protocol.h"
33
34
35 /**
36  * How many connections would we like to have per tunnel?
37  */
38 #define DESIRED_CONNECTIONS_PER_TUNNEL 3
39
40
41 /**
42  * All the encryption states a tunnel can be in.
43  */
44 enum CadetTunnelEState
45 {
46   /**
47    * Uninitialized status, we need to send KX.  We will stay
48    * in this state until the first connection is up.
49    */
50   CADET_TUNNEL_KEY_UNINITIALIZED,
51
52   /**
53    * KX message sent, waiting for other peer's KX_AUTH.
54    */
55   CADET_TUNNEL_KEY_AX_SENT,
56
57   /**
58    * KX message received, trying to send back KX_AUTH.
59    */
60   CADET_TUNNEL_KEY_AX_RECV,
61
62   /**
63    * KX message sent and received, trying to send back KX_AUTH.
64    */
65   CADET_TUNNEL_KEY_AX_SENT_AND_RECV,
66
67   /**
68    * KX received and we sent KX_AUTH back, but we got no traffic yet,
69    * so we're waiting for either KX_AUTH or ENCRYPED traffic from
70    * the other peer.
71    *
72    * We will not yet send traffic, as this might have been a replay.
73    * The other (initiating) peer should send a CHANNEL_OPEN next
74    * anyway, and then we are in business!
75    */
76   CADET_TUNNEL_KEY_AX_AUTH_SENT,
77
78   /**
79    * Handshake completed: session key available.
80    */
81   CADET_TUNNEL_KEY_OK
82
83 };
84
85
86 /**
87  * Get the static string for the peer this tunnel is directed.
88  *
89  * @param t Tunnel.
90  *
91  * @return Static string the destination peer's ID.
92  */
93 const char *
94 GCT_2s (const struct CadetTunnel *t);
95
96
97 /**
98  * Create a tunnel to @a destionation.  Must only be called
99  * from within #GCP_get_tunnel().
100  *
101  * @param destination where to create the tunnel to
102  * @return new tunnel to @a destination
103  */
104 struct CadetTunnel *
105 GCT_create_tunnel (struct CadetPeer *destination);
106
107
108 /**
109  * Destroys the tunnel @a t now, without delay. Used during shutdown.
110  *
111  * @param t tunnel to destroy
112  */
113 void
114 GCT_destroy_tunnel_now (struct CadetTunnel *t);
115
116
117 /**
118  * Add a @a connection to the @a tunnel.
119  *
120  * @param t a tunnel
121  * @param cid connection identifer to use for the connection
122  * @param path path to use for the connection
123  * @return #GNUNET_OK on success,
124  *         #GNUNET_SYSERR on failure (duplicate connection)
125  */
126 int
127 GCT_add_inbound_connection (struct CadetTunnel *t,
128                             const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
129                             struct CadetPeerPath *path);
130
131
132 /**
133  * We lost a connection, remove it from our list and clean up
134  * the connection object itself.
135  *
136  * @param ct binding of connection to tunnel of the connection that was lost.
137  */
138 void
139 GCT_connection_lost (struct CadetTConnection *ct);
140
141
142 /**
143  * Return the peer to which this tunnel goes.
144  *
145  * @param t a tunnel
146  * @return the destination of the tunnel
147  */
148 struct CadetPeer *
149 GCT_get_destination (struct CadetTunnel *t);
150
151
152 /**
153  * Consider using the path @a p for the tunnel @a t.
154  * The tunnel destination is at offset @a off in path @a p.
155  *
156  * @param cls our tunnel
157  * @param path a path to our destination
158  * @param off offset of the destination on path @a path
159  */
160 void
161 GCT_consider_path (struct CadetTunnel *t,
162                    struct CadetPeerPath *p,
163                    unsigned int off);
164
165
166 /**
167  * Add a channel to a tunnel.
168  *
169  * @param t Tunnel.
170  * @param ch Channel
171  * @return unique number identifying @a ch within @a t
172  */
173 struct GNUNET_CADET_ChannelTunnelNumber
174 GCT_add_channel (struct CadetTunnel *t,
175                  struct CadetChannel *ch);
176
177
178 /**
179  * Remove a channel from a tunnel.
180  *
181  * @param t Tunnel.
182  * @param ch Channel
183  * @param ctn unique number identifying @a ch within @a t
184  */
185 void
186 GCT_remove_channel (struct CadetTunnel *t,
187                     struct CadetChannel *ch,
188                     struct GNUNET_CADET_ChannelTunnelNumber ctn);
189
190
191 /**
192  * Send a DESTROY message via the tunnel.
193  *
194  * @param t the tunnel to transmit over
195  * @param ctn ID of the channel to destroy
196  */
197 void
198 GCT_send_channel_destroy (struct CadetTunnel *t,
199                           struct GNUNET_CADET_ChannelTunnelNumber ctn);
200
201
202 /**
203  * Sends an already built message on a tunnel, encrypting it and
204  * choosing the best connection if not provided.
205  *
206  * @param message Message to send. Function modifies it.
207  * @param t Tunnel on which this message is transmitted.
208  * @param cont Continuation to call once message is really sent.
209  * @param cont_cls Closure for @c cont.
210  * @return Handle to cancel message.
211  */
212 struct CadetTunnelQueueEntry *
213 GCT_send (struct CadetTunnel *t,
214           const struct GNUNET_MessageHeader *message,
215           GNUNET_SCHEDULER_TaskCallback cont,
216           void *cont_cls);
217
218
219 /**
220  * Cancel a previously sent message while it's in the queue.
221  *
222  * ONLY can be called before the continuation given to the send
223  * function is called. Once the continuation is called, the message is
224  * no longer in the queue!
225  *
226  * @param q Handle to the queue entry to cancel.
227  */
228 void
229 GCT_send_cancel (struct CadetTunnelQueueEntry *q);
230
231
232 /**
233  * Return the number of channels using a tunnel.
234  *
235  * @param t tunnel to count obtain the number of channels for
236  * @return number of channels using the tunnel
237  */
238 unsigned int
239 GCT_count_channels (struct CadetTunnel *t);
240
241
242 /**
243  * Return the number of connections available for a tunnel.
244  *
245  * @param t tunnel to count obtain the number of connections for
246  * @return number of connections available for the tunnel
247  */
248 unsigned int
249 GCT_count_any_connections (const struct CadetTunnel *t);
250
251
252 /**
253  * Iterator over connections.
254  *
255  * @param cls closure
256  * @param ct one of the connections
257  */
258 typedef void
259 (*GCT_ConnectionIterator) (void *cls,
260                            struct CadetTConnection *ct);
261
262
263 /**
264  * Iterate over all connections of a tunnel.
265  *
266  * @param t Tunnel whose connections to iterate.
267  * @param iter Iterator.
268  * @param iter_cls Closure for @c iter.
269  */
270 void
271 GCT_iterate_connections (struct CadetTunnel *t,
272                          GCT_ConnectionIterator iter,
273                          void *iter_cls);
274
275
276 /**
277  * Iterator over channels.
278  *
279  * @param cls closure
280  * @param ch one of the channels
281  */
282 typedef void
283 (*GCT_ChannelIterator) (void *cls,
284                         struct CadetChannel *ch);
285
286
287 /**
288  * Iterate over all channels of a tunnel.
289  *
290  * @param t Tunnel whose channels to iterate.
291  * @param iter Iterator.
292  * @param iter_cls Closure for @c iter.
293  */
294 void
295 GCT_iterate_channels (struct CadetTunnel *t,
296                       GCT_ChannelIterator iter,
297                       void *iter_cls);
298
299
300 /**
301  * Get the encryption state of a tunnel.
302  *
303  * @param t Tunnel.
304  *
305  * @return Tunnel's encryption state.
306  */
307 enum CadetTunnelEState
308 GCT_get_estate (struct CadetTunnel *t);
309
310
311 /**
312  * Handle KX message.
313  *
314  * @param ct connection/tunnel combo that received encrypted message
315  * @param msg the key exchange message
316  */
317 void
318 GCT_handle_kx (struct CadetTConnection *ct,
319                const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg);
320
321
322 /**
323  * Handle KX_AUTH message.
324  *
325  * @param ct connection/tunnel combo that received encrypted message
326  * @param msg the key exchange message
327  */
328 void
329 GCT_handle_kx_auth (struct CadetTConnection *ct,
330                     const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg);
331
332
333 /**
334  * Handle encrypted message.
335  *
336  * @param ct connection/tunnel combo that received encrypted message
337  * @param msg the encrypted message to decrypt
338  */
339 void
340 GCT_handle_encrypted (struct CadetTConnection *ct,
341                       const struct GNUNET_CADET_TunnelEncryptedMessage *msg);
342
343
344 /**
345  * Log all possible info about the tunnel state.
346  *
347  * @param t Tunnel to debug.
348  * @param level Debug level to use.
349  */
350 void
351 GCT_debug (const struct CadetTunnel *t,
352            enum GNUNET_ErrorType level);
353
354
355 #endif