Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_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 it
7      under the terms of the GNU Affero General Public License as published
8      by the Free Software Foundation, either version 3 of the License,
9      or (at your 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      Affero General Public License for more details.
15     
16      You should have received a copy of the GNU Affero General Public License
17      along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /**
21  * @file cadet/gnunet-service-cadet_tunnels.h
22  * @brief Information we track per tunnel.
23  * @author Bartlomiej Polot
24  * @author Christian Grothoff
25  */
26 #ifndef GNUNET_SERVICE_CADET_TUNNELS_H
27 #define GNUNET_SERVICE_CADET_TUNNELS_H
28
29 #include "gnunet-service-cadet.h"
30 #include "cadet_protocol.h"
31
32
33 /**
34  * How many connections would we like to have per tunnel?
35  */
36 #define DESIRED_CONNECTIONS_PER_TUNNEL 3
37
38
39 /**
40  * All the encryption states a tunnel can be in.
41  */
42 enum CadetTunnelEState
43 {
44   /**
45    * Uninitialized status, we need to send KX.  We will stay
46    * in this state until the first connection is up.
47    */
48   CADET_TUNNEL_KEY_UNINITIALIZED,
49
50   /**
51    * KX message sent, waiting for other peer's KX_AUTH.
52    */
53   CADET_TUNNEL_KEY_AX_SENT,
54
55   /**
56    * KX message received, trying to send back KX_AUTH.
57    */
58   CADET_TUNNEL_KEY_AX_RECV,
59
60   /**
61    * KX message sent and received, trying to send back KX_AUTH.
62    */
63   CADET_TUNNEL_KEY_AX_SENT_AND_RECV,
64
65   /**
66    * KX received and we sent KX_AUTH back, but we got no traffic yet,
67    * so we're waiting for either KX_AUTH or ENCRYPED traffic from
68    * the other peer.
69    *
70    * We will not yet send traffic, as this might have been a replay.
71    * The other (initiating) peer should send a CHANNEL_OPEN next
72    * anyway, and then we are in business!
73    */
74   CADET_TUNNEL_KEY_AX_AUTH_SENT,
75
76   /**
77    * Handshake completed: session key available.
78    */
79   CADET_TUNNEL_KEY_OK
80
81 };
82
83
84 /**
85  * Get the static string for the peer this tunnel is directed.
86  *
87  * @param t Tunnel.
88  *
89  * @return Static string the destination peer's ID.
90  */
91 const char *
92 GCT_2s (const struct CadetTunnel *t);
93
94
95 /**
96  * Create a tunnel to @a destionation.  Must only be called
97  * from within #GCP_get_tunnel().
98  *
99  * @param destination where to create the tunnel to
100  * @return new tunnel to @a destination
101  */
102 struct CadetTunnel *
103 GCT_create_tunnel (struct CadetPeer *destination);
104
105
106 /**
107  * Destroys the tunnel @a t now, without delay. Used during shutdown.
108  *
109  * @param t tunnel to destroy
110  */
111 void
112 GCT_destroy_tunnel_now (struct CadetTunnel *t);
113
114
115 /**
116  * Add a @a connection to the @a tunnel.
117  *
118  * @param t a tunnel
119  * @param cid connection identifer to use for the connection
120  * @param options options for the connection
121  * @param path path to use for the connection
122  * @return #GNUNET_OK on success,
123  *         #GNUNET_SYSERR on failure (duplicate connection)
124  */
125 int
126 GCT_add_inbound_connection (struct CadetTunnel *t,
127                             const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
128                             enum GNUNET_CADET_ChannelOption options,
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  * Function called when a transmission requested using #GCT_send is done.
204  *
205  * @param cls closure
206  * @param ctn identifier of the connection used for transmission, NULL if
207  *            the transmission failed (to be used to match ACKs to the
208  *            respective connection for connection performance evaluation)
209  */
210 typedef void
211 (*GCT_SendContinuation)(void *cls,
212                         const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
213
214
215 /**
216  * Sends an already built message on a tunnel, encrypting it and
217  * choosing the best connection if not provided.
218  *
219  * @param message Message to send. Function modifies it.
220  * @param t Tunnel on which this message is transmitted.
221  * @param cont Continuation to call once message is really sent.
222  * @param cont_cls Closure for @c cont.
223  * @return Handle to cancel message.
224  */
225 struct CadetTunnelQueueEntry *
226 GCT_send (struct CadetTunnel *t,
227           const struct GNUNET_MessageHeader *message,
228           GCT_SendContinuation cont,
229           void *cont_cls);
230
231
232 /**
233  * Cancel a previously sent message while it's in the queue.
234  *
235  * ONLY can be called before the continuation given to the send
236  * function is called. Once the continuation is called, the message is
237  * no longer in the queue!
238  *
239  * @param q Handle to the queue entry to cancel.
240  */
241 void
242 GCT_send_cancel (struct CadetTunnelQueueEntry *q);
243
244
245 /**
246  * Return the number of channels using a tunnel.
247  *
248  * @param t tunnel to count obtain the number of channels for
249  * @return number of channels using the tunnel
250  */
251 unsigned int
252 GCT_count_channels (struct CadetTunnel *t);
253
254
255 /**
256  * Return the number of connections available for a tunnel.
257  *
258  * @param t tunnel to count obtain the number of connections for
259  * @return number of connections available for the tunnel
260  */
261 unsigned int
262 GCT_count_any_connections (const struct CadetTunnel *t);
263
264
265 /**
266  * Iterator over connections.
267  *
268  * @param cls closure
269  * @param ct one of the connections
270  */
271 typedef void
272 (*GCT_ConnectionIterator) (void *cls,
273                            struct CadetTConnection *ct);
274
275
276 /**
277  * Iterate over all connections of a tunnel.
278  *
279  * @param t Tunnel whose connections to iterate.
280  * @param iter Iterator.
281  * @param iter_cls Closure for @c iter.
282  */
283 void
284 GCT_iterate_connections (struct CadetTunnel *t,
285                          GCT_ConnectionIterator iter,
286                          void *iter_cls);
287
288
289 /**
290  * Iterator over channels.
291  *
292  * @param cls closure
293  * @param ch one of the channels
294  */
295 typedef void
296 (*GCT_ChannelIterator) (void *cls,
297                         struct CadetChannel *ch);
298
299
300 /**
301  * Iterate over all channels of a tunnel.
302  *
303  * @param t Tunnel whose channels to iterate.
304  * @param iter Iterator.
305  * @param iter_cls Closure for @c iter.
306  */
307 void
308 GCT_iterate_channels (struct CadetTunnel *t,
309                       GCT_ChannelIterator iter,
310                       void *iter_cls);
311
312
313 /**
314  * Get the encryption state of a tunnel.
315  *
316  * @param t Tunnel.
317  *
318  * @return Tunnel's encryption state.
319  */
320 enum CadetTunnelEState
321 GCT_get_estate (struct CadetTunnel *t);
322
323
324 /**
325  * Handle KX message.
326  *
327  * @param ct connection/tunnel combo that received encrypted message
328  * @param msg the key exchange message
329  */
330 void
331 GCT_handle_kx (struct CadetTConnection *ct,
332                const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg);
333
334
335 /**
336  * Handle KX_AUTH message.
337  *
338  * @param ct connection/tunnel combo that received encrypted message
339  * @param msg the key exchange message
340  */
341 void
342 GCT_handle_kx_auth (struct CadetTConnection *ct,
343                     const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg);
344
345
346 /**
347  * Handle encrypted message.
348  *
349  * @param ct connection/tunnel combo that received encrypted message
350  * @param msg the encrypted message to decrypt
351  */
352 void
353 GCT_handle_encrypted (struct CadetTConnection *ct,
354                       const struct GNUNET_CADET_TunnelEncryptedMessage *msg);
355
356
357 /**
358  * Log all possible info about the tunnel state.
359  *
360  * @param t Tunnel to debug.
361  * @param level Debug level to use.
362  */
363 void
364 GCT_debug (const struct CadetTunnel *t,
365            enum GNUNET_ErrorType level);
366
367
368 #endif