3 This file is part of GNUnet.
4 Copyright (C) 2001-2017 GNUnet e.V.
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.
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.
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.
23 * @file cadet/gnunet-service-cadet-new_connection.c
24 * @brief management of CORE-level end-to-end connections; establishes
25 * end-to-end routes and transmits messages along the route
26 * @author Bartlomiej Polot
27 * @author Christian Grothoff
30 * - Optimization: keepalive messages / timeout (timeout to be done @ peer level!)
31 * - Optimization: keep performance metrics (?)
34 #include "gnunet-service-cadet-new_channel.h"
35 #include "gnunet-service-cadet-new_connection.h"
36 #include "gnunet-service-cadet-new_paths.h"
37 #include "gnunet-service-cadet-new_peer.h"
38 #include "gnunet-service-cadet-new_tunnels.h"
39 #include "gnunet_cadet_service.h"
40 #include "cadet_protocol.h"
44 * All the states a connection can be in.
46 enum CadetConnectionState
49 * Uninitialized status, we have not yet even gotten the message queue.
54 * Connection create message in queue, awaiting transmission by CORE.
56 CADET_CONNECTION_SENDING_CREATE,
59 * Connection create message sent, waiting for ACK.
61 CADET_CONNECTION_SENT,
64 * We are an inbound connection, and received a CREATE. Need to
65 * send an CREATE_ACK back.
67 CADET_CONNECTION_CREATE_RECEIVED,
70 * Connection confirmed, ready to carry traffic.
72 CADET_CONNECTION_READY
78 * Low-level connection to a destination.
80 struct CadetConnection
84 * ID of the connection.
86 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
89 * To which peer does this connection go?
91 struct CadetPeer *destination;
94 * Which tunnel is using this connection?
96 struct CadetTConnection *ct;
99 * Path we are using to our destination.
101 struct CadetPeerPath *path;
104 * Pending message, NULL if we are ready to transmit.
106 struct GNUNET_MQ_Envelope *env;
109 * Handle for calling #GCP_request_mq_cancel() once we are finished.
111 struct GCP_MessageQueueManager *mq_man;
114 * Task for connection maintenance.
116 struct GNUNET_SCHEDULER_Task *task;
119 * Function to call once we are ready to transmit.
121 GCC_ReadyCallback ready_cb;
124 * Closure for @e ready_cb.
129 * How long do we wait before we try again with a CREATE message?
131 struct GNUNET_TIME_Relative retry_delay;
134 * State of the connection.
136 enum CadetConnectionState state;
139 * Offset of our @e destination in @e path.
144 * Are we ready to transmit via @e mq_man right now?
152 * Destroy a connection.
154 * @param cc connection to destroy
157 GCC_destroy (struct CadetConnection *cc)
159 struct GNUNET_MQ_Envelope *env = NULL;
161 if (CADET_CONNECTION_SENDING_CREATE != cc->state)
163 struct GNUNET_CADET_ConnectionDestroyMessage *destroy_msg;
165 /* Need to notify next hop that we are down. */
166 env = GNUNET_MQ_msg (destroy_msg,
167 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
168 destroy_msg->cid = cc->cid;
170 GCP_request_mq_cancel (cc->mq_man,
173 GCPP_del_connection (cc->path,
176 GNUNET_assert (GNUNET_YES ==
177 GNUNET_CONTAINER_multishortmap_remove (connections,
178 &GCC_get_id (cc)->connection_of_tunnel,
185 * Return the tunnel associated with this connection.
187 * @param cc connection to query
188 * @return corresponding entry in the tunnel's connection list
190 struct CadetTConnection *
191 GCC_get_ct (struct CadetConnection *cc)
198 * A connection ACK was received for this connection, implying
199 * that the end-to-end connection is up. Process it.
201 * @param cc the connection that got the ACK.
204 GCC_handle_connection_ack (struct CadetConnection *cc)
206 if (NULL != cc->task)
208 GNUNET_SCHEDULER_cancel (cc->task);
212 cc->task = GNUNET_SCHEDULER_add_delayed (cc->keepalive_period,
216 cc->state = CADET_CONNECTION_READY;
217 if (GNUNET_YES == cc->mqm_ready)
218 cc->ready_cb (cc->ready_cb_cls,
226 * @param cc connection that received encrypted message
227 * @param msg the key exchange message
230 GCC_handle_kx (struct CadetConnection *cc,
231 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
233 if (CADET_CONNECTION_SENT == cc->state)
235 /* We didn't get the CREATE_ACK, but instead got payload. That's fine,
236 clearly something is working, so pretend we got an ACK. */
237 GCC_handle_connection_ack (cc);
239 GCT_handle_kx (cc->ct,
245 * Handle encrypted message.
247 * @param cc connection that received encrypted message
248 * @param msg the encrypted message to decrypt
251 GCC_handle_encrypted (struct CadetConnection *cc,
252 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
254 if (CADET_CONNECTION_SENT == cc->state)
256 /* We didn't get the CREATE_ACK, but instead got payload. That's fine,
257 clearly something is working, so pretend we got an ACK. */
258 GCC_handle_connection_ack (cc);
260 GCT_handle_encrypted (cc->ct,
266 * Send a CREATE message to the first hop.
268 * @param cls the `struct CadetConnection` to initiate
271 send_create (void *cls)
273 struct CadetConnection *cc = cls;
274 struct GNUNET_CADET_ConnectionCreateMessage *create_msg;
275 struct GNUNET_PeerIdentity *pids;
276 struct GNUNET_MQ_Envelope *env;
277 unsigned int path_length;
280 GNUNET_assert (GNUNET_YES == cc->mqm_ready);
281 path_length = GCPP_get_length (cc->path);
282 env = GNUNET_MQ_msg_extra (create_msg,
283 path_length * sizeof (struct GNUNET_PeerIdentity),
284 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
285 create_msg->cid = cc->cid;
286 pids = (struct GNUNET_PeerIdentity *) &create_msg[1];
287 for (unsigned int i=0;i<path_length;i++)
288 pids[i] = *GCP_get_id (GCPP_get_peer_at_offset (cc->path,
291 cc->mqm_ready = GNUNET_NO;
292 cc->state = CADET_CONNECTION_SENT;
293 GCP_send (cc->mq_man,
299 * Send a CREATE_ACK message towards the origin.
301 * @param cls the `struct CadetConnection` to initiate
304 send_create_ack (void *cls)
306 struct CadetConnection *cc = cls;
307 struct GNUNET_CADET_ConnectionCreateMessage *create_msg;
308 struct GNUNET_PeerIdentity *pids;
309 struct GNUNET_MQ_Envelope *env;
310 unsigned int path_length;
313 GNUNET_assert (GNUNET_YES == cc->mqm_ready);
314 path_length = GCPP_get_length (cc->path);
315 env = GNUNET_MQ_msg_extra (create_msg,
316 path_length * sizeof (struct GNUNET_PeerIdentity),
317 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
318 create_msg->cid = cc->cid;
319 pids = (struct GNUNET_PeerIdentity *) &create_msg[1];
320 for (unsigned int i=0;i<path_length;i++)
321 pids[i] = *GCP_get_id (GCPP_get_peer_at_offset (cc->path,
324 cc->mqm_ready = GNUNET_NO;
325 cc->state = CADET_CONNECTION_READY;
326 GCP_send (cc->mq_man,
332 * We got a #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE for a
333 * connection that we already have. Either our ACK got lost
334 * or something is fishy. Consider retransmitting the ACK.
336 * @param cc connection that got the duplicate CREATE
339 GCC_handle_duplicate_create (struct CadetConnection *cc)
341 if (GNUNET_YES == cc->mqm_ready)
343 /* Tell tunnel that we are not ready for transmission anymore
344 (until CREATE_ACK is done) */
345 cc->ready_cb (cc->ready_cb_cls,
348 /* Revert back to the state of having only received the 'CREATE',
349 and immediately proceed to send the CREATE_ACK. */
350 cc->state = CADET_CONNECTION_CREATE_RECEIVED;
351 cc->task = GNUNET_SCHEDULER_add_now (&send_create_ack,
356 /* We are currently sending something else back, which
357 can only be an ACK or payload, either of which would
358 do. So actually no need to do anything. */
364 * There has been a change in the message queue existence for our
365 * peer at the first hop. Adjust accordingly.
367 * @param cls the `struct CadetConnection`
368 * @param available #GNUNET_YES if sending is now possible,
369 * #GNUNET_NO if sending is no longer possible
370 * #GNUNET_SYSERR if sending is no longer possible
371 * and the last envelope was discarded
374 manage_first_hop_mq (void *cls,
377 struct CadetConnection *cc = cls;
379 if (GNUNET_YES != available)
381 /* Connection is down, for now... */
382 cc->mqm_ready = GNUNET_NO;
383 cc->state = CADET_CONNECTION_NEW;
384 cc->retry_delay = GNUNET_TIME_UNIT_ZERO;
385 if (NULL != cc->task)
387 GNUNET_SCHEDULER_cancel (cc->task);
390 cc->ready_cb (cc->ready_cb_cls,
395 cc->mqm_ready = GNUNET_YES;
398 case CADET_CONNECTION_NEW:
399 /* Transmit immediately */
400 cc->task = GNUNET_SCHEDULER_add_now (&send_create,
403 case CADET_CONNECTION_SENDING_CREATE:
404 /* Should not be possible to be called in this state. */
407 case CADET_CONNECTION_SENT:
408 /* Retry a bit later... */
409 cc->retry_delay = GNUNET_TIME_STD_BACKOFF (cc->retry_delay);
410 cc->task = GNUNET_SCHEDULER_add_delayed (cc->retry_delay,
414 case CADET_CONNECTION_CREATE_RECEIVED:
415 /* We got the 'CREATE' (incoming connection), should send the CREATE_ACK */
416 cc->task = GNUNET_SCHEDULER_add_now (&send_create_ack,
419 case CADET_CONNECTION_READY:
420 cc->ready_cb (cc->ready_cb_cls,
428 * Create a connection to @a destination via @a path and notify @a cb
429 * whenever we are ready for more data. Shared logic independent of
430 * who is initiating the connection.
432 * @param destination where to go
433 * @param path which path to take (may not be the full path)
434 * @param ct which tunnel uses this connection
435 * @param init_state initial state for the connection
436 * @param ready_cb function to call when ready to transmit
437 * @param ready_cb_cls closure for @a cb
438 * @return handle to the connection
440 static struct CadetConnection *
441 connection_create (struct CadetPeer *destination,
442 struct CadetPeerPath *path,
443 struct CadetTConnection *ct,
444 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
445 enum CadetConnectionState init_state,
446 GCC_ReadyCallback ready_cb,
449 struct CadetConnection *cc;
450 struct CadetPeer *first_hop;
453 off = GCPP_find_peer (path,
455 GNUNET_assert (UINT_MAX > off);
456 cc = GNUNET_new (struct CadetConnection);
457 cc->state = init_state;
460 GNUNET_assert (GNUNET_OK ==
461 GNUNET_CONTAINER_multishortmap_put (connections,
462 &GCC_get_id (cc)->connection_of_tunnel,
464 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
465 cc->ready_cb = ready_cb;
466 cc->ready_cb_cls = ready_cb_cls;
469 GCPP_add_connection (path,
472 for (unsigned int i=0;i<off;i++)
473 GCP_add_connection (GCPP_get_peer_at_offset (path,
477 first_hop = GCPP_get_peer_at_offset (path,
479 cc->mq_man = GCP_request_mq (first_hop,
480 &manage_first_hop_mq,
487 * Create a connection to @a destination via @a path and
488 * notify @a cb whenever we are ready for more data. This
489 * is an inbound tunnel, so we must use the existing @a cid
491 * @param destination where to go
492 * @param path which path to take (may not be the full path)
493 * @param ct which tunnel uses this connection
494 * @param ready_cb function to call when ready to transmit
495 * @param ready_cb_cls closure for @a cb
496 * @return handle to the connection
498 struct CadetConnection *
499 GCC_create_inbound (struct CadetPeer *destination,
500 struct CadetPeerPath *path,
501 struct CadetTConnection *ct,
502 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
503 GCC_ReadyCallback ready_cb,
506 return connection_create (destination,
510 CADET_CONNECTION_CREATE_RECEIVED,
517 * Create a connection to @a destination via @a path and
518 * notify @a cb whenever we are ready for more data.
520 * @param destination where to go
521 * @param path which path to take (may not be the full path)
522 * @param ct tunnel that uses the connection
523 * @param ready_cb function to call when ready to transmit
524 * @param ready_cb_cls closure for @a cb
525 * @return handle to the connection
527 struct CadetConnection *
528 GCC_create (struct CadetPeer *destination,
529 struct CadetPeerPath *path,
530 struct CadetTConnection *ct,
531 GCC_ReadyCallback ready_cb,
534 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
536 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
539 return connection_create (destination,
543 CADET_CONNECTION_NEW,
550 * Transmit message @a msg via connection @a cc. Must only be called
551 * (once) after the connection has signalled that it is ready via the
552 * `ready_cb`. Clients can also use #GCC_is_ready() to check if the
553 * connection is right now ready for transmission.
555 * @param cc connection identification
556 * @param env envelope with message to transmit; must NOT
557 * yet have a #GNUNET_MQ_notify_sent() callback attached to it
560 GCC_transmit (struct CadetConnection *cc,
561 struct GNUNET_MQ_Envelope *env)
563 GNUNET_assert (GNUNET_YES == cc->mqm_ready);
564 GNUNET_assert (CADET_CONNECTION_READY == cc->state);
565 cc->mqm_ready = GNUNET_NO;
566 GCP_send (cc->mq_man,
572 * Obtain the path used by this connection.
574 * @param cc connection
575 * @return path to @a cc
577 struct CadetPeerPath *
578 GCC_get_path (struct CadetConnection *cc)
585 * Obtain unique ID for the connection.
587 * @param cc connection.
588 * @return unique number of the connection
590 const struct GNUNET_CADET_ConnectionTunnelIdentifier *
591 GCC_get_id (struct CadetConnection *cc)
598 * Get a (static) string for a connection.
600 * @param cc Connection.
603 GCC_2s (const struct CadetConnection *cc)
605 static char buf[128];
608 return "Connection(NULL)";
612 GNUNET_snprintf (buf,
614 "Connection(%s(Tunnel(%s)))",
615 GNUNET_sh2s (&cc->cid.connection_of_tunnel),
619 GNUNET_snprintf (buf,
621 "Connection(%s(Tunnel(NULL)))",
622 GNUNET_sh2s (&cc->cid.connection_of_tunnel));
627 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-con",__VA_ARGS__)
631 * Log connection info.
633 * @param cc connection
634 * @param level Debug level to use.
637 GCC_debug (struct CadetConnection *cc,
638 enum GNUNET_ErrorType level)
643 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
645 __FILE__, __FUNCTION__, __LINE__);
651 "Connection (NULL)\n");
654 s = GCPP_2s (cc->path);
656 "Connection %s to %s via path %s in state %d is %s\n",
658 GCP_2s (cc->destination),
661 (GNUNET_YES == cc->mqm_ready) ? "ready" : "busy");
665 /* end of gnunet-service-cadet-new_connection.c */