2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file cadet/gnunet-service-cadet_connection.c
23 * @brief GNUnet CADET service connection handling
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
32 #include "cadet_path.h"
33 #include "cadet_protocol.h"
35 #include "gnunet-service-cadet_connection.h"
36 #include "gnunet-service-cadet_peer.h"
37 #include "gnunet-service-cadet_tunnel.h"
40 #define LOG(level, ...) GNUNET_log_from (level,"cadet-con",__VA_ARGS__)
41 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-con",__VA_ARGS__)
44 #define CADET_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
45 GNUNET_TIME_UNIT_MINUTES,\
50 /******************************************************************************/
51 /******************************** STRUCTS **********************************/
52 /******************************************************************************/
55 * Struct to encapsulate all the Flow Control information to a peer to which
56 * we are directly connected (on a core level).
58 struct CadetFlowControl
61 * Connection this controls.
63 struct CadetConnection *c;
66 * How many messages are in the queue on this connection.
71 * How many messages do we accept in the queue.
73 unsigned int queue_max;
76 * ID of the last packet sent towards the peer.
78 uint32_t last_pid_sent;
81 * ID of the last packet received from the peer.
83 uint32_t last_pid_recv;
86 * Last ACK sent to the peer (peer can't send more than this PID).
88 uint32_t last_ack_sent;
91 * Last ACK sent towards the origin (for traffic towards leaf node).
93 uint32_t last_ack_recv;
96 * Task to poll the peer in case of a lost ACK causes stall.
98 GNUNET_SCHEDULER_TaskIdentifier poll_task;
101 * How frequently to poll for ACKs.
103 struct GNUNET_TIME_Relative poll_time;
106 * Queued poll message, to cancel if not necessary anymore (got ACK).
108 struct CadetConnectionQueue *poll_msg;
111 * Queued poll message, to cancel if not necessary anymore (got ACK).
113 struct CadetConnectionQueue *ack_msg;
117 * Keep a record of the last messages sent on this connection.
119 struct CadetConnectionPerformance
122 * Circular buffer for storing measurements.
124 double usecsperbyte[AVG_MSGS];
127 * Running average of @c usecsperbyte.
132 * How many values of @c usecsperbyte are valid.
137 * Index of the next "free" position in @c usecsperbyte.
144 * Struct containing all information regarding a connection to a peer.
146 struct CadetConnection
149 * Tunnel this connection is part of.
151 struct CadetTunnel *t;
154 * Flow control information for traffic fwd.
156 struct CadetFlowControl fwd_fc;
159 * Flow control information for traffic bck.
161 struct CadetFlowControl bck_fc;
164 * Measure connection performance on the endpoint.
166 struct CadetConnectionPerformance *perf;
169 * ID of the connection.
171 struct GNUNET_CADET_Hash id;
174 * State of the connection.
176 enum CadetConnectionState state;
179 * Path being used for the tunnel. At the origin of the connection
180 * it's a pointer to the destination's path pool, otherwise just a copy.
182 struct CadetPeerPath *path;
185 * Position of the local peer in the path.
187 unsigned int own_pos;
190 * Task to keep the used paths alive at the owner,
191 * time tunnel out on all the other peers.
193 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
196 * Task to keep the used paths alive at the destination,
197 * time tunnel out on all the other peers.
199 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
202 * Queue handle for maintainance traffic. One handle for FWD and BCK since
203 * one peer never needs to maintain both directions (no loopback connections).
205 struct CadetPeerQueue *maintenance_q;
208 * Counter to do exponential backoff when creating a connection (max 64).
210 unsigned short create_retry;
213 * Pending message count.
215 int pending_messages;
218 * Destroy flag: if true, destroy on last message.
224 * Handle for messages queued but not yet sent.
226 struct CadetConnectionQueue
229 * Peer queue handle, to cancel if necessary.
231 struct CadetPeerQueue *q;
234 * Was this a forced message? (Do not account for it)
239 * Continuation to call once sent.
244 * Closure for @c cont.
249 /******************************************************************************/
250 /******************************* GLOBALS ***********************************/
251 /******************************************************************************/
254 * Global handle to the statistics service.
256 extern struct GNUNET_STATISTICS_Handle *stats;
259 * Local peer own ID (memory efficient handle).
261 extern GNUNET_PEER_Id myid;
264 * Local peer own ID (full value).
266 extern struct GNUNET_PeerIdentity my_full_id;
269 * Connections known, indexed by cid (CadetConnection).
271 static struct GNUNET_CONTAINER_MultiHashMap *connections;
274 * How many connections are we willing to maintain.
275 * Local connections are always allowed, even if there are more connections than max.
277 static unsigned long long max_connections;
280 * How many messages *in total* are we willing to queue, divide by number of
281 * connections to get connection queue size.
283 static unsigned long long max_msgs_queue;
286 * How often to send path keepalives. Paths timeout after 4 missed.
288 static struct GNUNET_TIME_Relative refresh_connection_time;
291 * How often to send path create / ACKs.
293 static struct GNUNET_TIME_Relative create_connection_time;
296 /******************************************************************************/
297 /******************************** STATIC ***********************************/
298 /******************************************************************************/
300 #if 0 // avoid compiler warning for unused static function
302 fc_debug (struct CadetFlowControl *fc)
304 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
305 fc->last_pid_recv, fc->last_ack_sent);
306 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
307 fc->last_pid_sent, fc->last_ack_recv);
308 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
309 fc->queue_n, fc->queue_max);
313 connection_debug (struct CadetConnection *c)
317 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
320 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
321 peer2s (c->t->peer), GCC_2s (c));
322 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
323 c->state, c->pending_messages);
324 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
325 fc_debug (&c->fwd_fc);
326 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
327 fc_debug (&c->bck_fc);
333 * Schedule next keepalive task, taking in consideration
334 * the connection state and number of retries.
336 * @param c Connection for which to schedule the next keepalive.
337 * @param fwd Direction for the next keepalive.
340 schedule_next_keepalive (struct CadetConnection *c, int fwd);
344 * Resets the connection timeout task, some other message has done the
346 * - For the first peer on the direction this means to send
347 * a keepalive or a path confirmation message (either create or ACK).
348 * - For all other peers, this means to destroy the connection,
349 * due to lack of activity.
350 * Starts the timeout if no timeout was running (connection just created).
352 * @param c Connection whose timeout to reset.
353 * @param fwd Is this forward?
356 connection_reset_timeout (struct CadetConnection *c, int fwd);
360 * Get string description for tunnel state. Reentrant.
362 * @param s Tunnel state.
364 * @return String representation.
367 GCC_state2s (enum CadetConnectionState s)
371 case CADET_CONNECTION_NEW:
372 return "CADET_CONNECTION_NEW";
373 case CADET_CONNECTION_SENT:
374 return "CADET_CONNECTION_SENT";
375 case CADET_CONNECTION_ACK:
376 return "CADET_CONNECTION_ACK";
377 case CADET_CONNECTION_READY:
378 return "CADET_CONNECTION_READY";
379 case CADET_CONNECTION_DESTROYED:
380 return "CADET_CONNECTION_DESTROYED";
381 case CADET_CONNECTION_BROKEN:
382 return "CADET_CONNECTION_BROKEN";
385 LOG (GNUNET_ERROR_TYPE_ERROR, " conn state %u unknown!\n", s);
386 return "CADET_CONNECTION_STATE_ERROR";
392 * Initialize a Flow Control structure to the initial state.
394 * @param fc Flow Control structure to initialize.
397 fc_init (struct CadetFlowControl *fc)
399 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
400 fc->last_pid_recv = (uint32_t) -1;
401 fc->last_ack_sent = (uint32_t) 0;
402 fc->last_ack_recv = (uint32_t) 0;
403 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
404 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
406 fc->queue_max = (max_msgs_queue / max_connections) + 1;
413 * @param cid Connection ID.
415 static struct CadetConnection *
416 connection_get (const struct GNUNET_CADET_Hash *cid)
418 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
423 connection_change_state (struct CadetConnection* c,
424 enum CadetConnectionState state)
426 LOG (GNUNET_ERROR_TYPE_DEBUG,
427 "Connection %s state %s -> %s\n",
428 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
429 if (CADET_CONNECTION_DESTROYED <= c->state) /* Destroyed or broken. */
431 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
435 if (CADET_CONNECTION_READY == state)
441 * Callback called when a queued ACK message is sent.
443 * @param cls Closure (FC).
444 * @param c Connection this message was on.
445 * @param q Queue handler this call invalidates.
446 * @param type Type of message sent.
447 * @param fwd Was this a FWD going message?
448 * @param size Size of the message.
452 struct CadetConnection *c,
453 struct CadetConnectionQueue *q,
454 uint16_t type, int fwd, size_t size)
456 struct CadetFlowControl *fc = cls;
463 * Send an ACK on the connection, informing the predecessor about
464 * the available buffer space. Should not be called in case the peer
465 * is origin (no predecessor) in the @c fwd direction.
467 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
468 * the ACK itself goes "back" (dest->root).
470 * @param c Connection on which to send the ACK.
471 * @param buffer How much space free to advertise?
472 * @param fwd Is this FWD ACK? (Going dest -> root)
473 * @param force Don't optimize out.
476 send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
478 struct CadetFlowControl *next_fc;
479 struct CadetFlowControl *prev_fc;
480 struct GNUNET_CADET_ACK msg;
484 /* If origin, there is no connection to send ACKs. Wrong function! */
485 if (GCC_is_origin (c, fwd))
487 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
488 GCC_2s (c), GC_f2s (fwd));
493 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
494 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
496 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
497 GC_f2s (fwd), GCC_2s (c));
499 /* Check if we need to transmit the ACK. */
500 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
501 if (3 < delta && buffer < delta && GNUNET_NO == force)
503 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
504 LOG (GNUNET_ERROR_TYPE_DEBUG,
505 " last pid recv: %u, last ack sent: %u\n",
506 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
510 /* Ok, ACK might be necessary, what PID to ACK? */
511 ack = prev_fc->last_pid_recv + buffer;
512 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
513 LOG (GNUNET_ERROR_TYPE_DEBUG,
514 " last pid %u, last ack %u, qmax %u, q %u\n",
515 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
516 next_fc->queue_max, next_fc->queue_n);
517 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
519 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
523 /* Check if message is already in queue */
524 if (NULL != prev_fc->ack_msg)
526 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
528 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
529 GCC_cancel (prev_fc->ack_msg);
530 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
534 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
539 prev_fc->last_ack_sent = ack;
541 /* Build ACK message and send on connection */
542 msg.header.size = htons (sizeof (msg));
543 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
544 msg.ack = htonl (ack);
547 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, 0, ack, c,
550 GNUNET_assert (NULL != prev_fc->ack_msg);
555 * Callback called when a connection queued message is sent.
557 * Calculates the average time and connection packet tracking.
559 * @param cls Closure (ConnectionQueue Handle).
560 * @param c Connection this message was on.
561 * @param sent Was it really sent? (Could have been canceled)
562 * @param type Type of message sent.
563 * @param pid Packet ID, or 0 if not applicable (create, destroy, etc).
564 * @param fwd Was this a FWD going message?
565 * @param size Size of the message.
566 * @param wait Time spent waiting for core (only the time for THIS message)
568 * @return #GNUNET_YES if connection was destroyed, #GNUNET_NO otherwise.
571 conn_message_sent (void *cls,
572 struct CadetConnection *c, int sent,
573 uint16_t type, uint32_t pid, int fwd, size_t size,
574 struct GNUNET_TIME_Relative wait)
576 struct CadetConnectionPerformance *p;
577 struct CadetFlowControl *fc;
578 struct CadetConnectionQueue *q = cls;
582 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
584 fc = fwd ? &c->fwd_fc : &c->bck_fc;
585 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s\n",
586 sent ? "" : "not ", GC_f2s (fwd), GC_m2s (type));
592 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
593 q->cont (q->cont_cls, c, q, type, fwd, size);
597 else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED)
599 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
608 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
609 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
611 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
616 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
617 c->pending_messages--;
618 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
620 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
624 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
627 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
628 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
629 c->maintenance_q = NULL;
630 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
631 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
632 schedule_next_keepalive (c, fwd);
635 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
636 if (GNUNET_YES == sent)
638 GNUNET_assert (NULL != q);
639 fc->last_pid_sent = pid; // FIXME
640 GCC_send_ack (c, fwd, GNUNET_NO);
641 connection_reset_timeout (c, fwd);
644 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
645 if (GNUNET_NO == forced)
648 LOG (GNUNET_ERROR_TYPE_DEBUG,
649 "! accounting pid %u\n",
654 LOG (GNUNET_ERROR_TYPE_DEBUG,
655 "! forced, Q_N not accounting pid %u\n",
660 case GNUNET_MESSAGE_TYPE_CADET_KX:
661 if (GNUNET_YES == sent)
662 connection_reset_timeout (c, fwd);
665 case GNUNET_MESSAGE_TYPE_CADET_POLL:
669 case GNUNET_MESSAGE_TYPE_CADET_ACK:
676 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
679 return GNUNET_NO; /* Only endpoints are interested in timing. */
682 usecsperbyte = ((double) wait.rel_value_us) / size;
683 if (p->size == AVG_MSGS)
685 /* Array is full. Substract oldest value, add new one and store. */
686 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
687 p->usecsperbyte[p->idx] = usecsperbyte;
688 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
692 /* Array not yet full. Add current value to avg and store. */
693 p->usecsperbyte[p->idx] = usecsperbyte;
695 p->avg += p->usecsperbyte[p->idx];
699 p->idx = (p->idx + 1) % AVG_MSGS;
705 * Get the previous hop in a connection
707 * @param c Connection.
709 * @return Previous peer in the connection.
711 static struct CadetPeer *
712 get_prev_hop (const struct CadetConnection *c)
716 LOG (GNUNET_ERROR_TYPE_DEBUG, " get prev hop %s [%u/%u]\n",
717 GCC_2s (c), c->own_pos, c->path->length);
718 if (0 == c->own_pos || c->path->length < 2)
719 id = c->path->peers[0];
721 id = c->path->peers[c->own_pos - 1];
723 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
724 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
726 return GCP_get_short (id);
731 * Get the next hop in a connection
733 * @param c Connection.
735 * @return Next peer in the connection.
737 static struct CadetPeer *
738 get_next_hop (const struct CadetConnection *c)
742 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
743 GCC_2s (c), c->own_pos, c->path->length);
744 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
745 id = c->path->peers[c->path->length - 1];
747 id = c->path->peers[c->own_pos + 1];
749 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
750 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
752 return GCP_get_short (id);
757 * Get the hop in a connection.
759 * @param c Connection.
760 * @param fwd Next hop?
762 * @return Next peer in the connection.
764 static struct CadetPeer *
765 get_hop (struct CadetConnection *c, int fwd)
768 return get_next_hop (c);
769 return get_prev_hop (c);
774 * Is traffic coming from this sender 'FWD' traffic?
776 * @param c Connection to check.
777 * @param sender Peer identity of neighbor.
779 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
780 * the traffic is 'FWD'.
781 * #GNUNET_NO for BCK.
782 * #GNUNET_SYSERR for errors.
785 is_fwd (const struct CadetConnection *c,
786 const struct GNUNET_PeerIdentity *sender)
790 id = GNUNET_PEER_search (sender);
791 if (GCP_get_short_id (get_prev_hop (c)) == id)
794 if (GCP_get_short_id (get_next_hop (c)) == id)
798 return GNUNET_SYSERR;
803 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
804 * or a first CONNECTION_ACK directed to us.
806 * @param connection Connection to confirm.
807 * @param fwd Should we send it FWD? (root->dest)
808 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
811 send_connection_ack (struct CadetConnection *connection, int fwd)
813 struct CadetTunnel *t;
816 LOG (GNUNET_ERROR_TYPE_INFO, "===> {%14s ACK} on connection %s\n",
817 GC_f2s (!fwd), GCC_2s (connection));
818 GCP_queue_add (get_hop (connection, fwd), NULL,
819 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0, 0,
820 sizeof (struct GNUNET_CADET_ConnectionACK),
821 connection, fwd, &conn_message_sent, NULL);
822 connection->pending_messages++;
823 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
824 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
825 if (CADET_CONNECTION_READY != connection->state)
826 connection_change_state (connection, CADET_CONNECTION_SENT);
831 * Send a notification that a connection is broken.
833 * @param c Connection that is broken.
834 * @param id1 Peer that has disconnected.
835 * @param id2 Peer that has disconnected.
836 * @param fwd Direction towards which to send it.
839 send_broken (struct CadetConnection *c,
840 const struct GNUNET_PeerIdentity *id1,
841 const struct GNUNET_PeerIdentity *id2,
844 struct GNUNET_CADET_ConnectionBroken msg;
846 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
847 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
851 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c, fwd,
852 GNUNET_YES, NULL, NULL));
857 * Send a notification that a connection is broken, when a connection
858 * isn't even known to the local peer.
860 * @param connection_id Connection ID.
861 * @param id1 Peer that has disconnected, probably local peer.
862 * @param id2 Peer that has disconnected can be NULL if unknown.
863 * @param peer Peer to notify (neighbor who sent the connection).
866 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
867 const struct GNUNET_PeerIdentity *id1,
868 const struct GNUNET_PeerIdentity *id2,
869 const struct GNUNET_PeerIdentity *peer_id)
871 struct GNUNET_CADET_ConnectionBroken *msg;
872 struct CadetPeer *neighbor;
874 LOG (GNUNET_ERROR_TYPE_INFO, "===> BROKEN on unknown connection %s\n",
875 GNUNET_h2s (GC_h2hc (connection_id)));
877 msg = GNUNET_new (struct GNUNET_CADET_ConnectionBroken);
878 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
879 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
880 msg->cid = *connection_id;
885 memset (&msg->peer2, 0, sizeof (msg->peer2));
886 neighbor = GCP_get (peer_id);
887 GCP_queue_add (neighbor, msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
888 0, 2, sizeof (struct GNUNET_CADET_ConnectionBroken),
889 NULL, GNUNET_SYSERR, /* connection, fwd */
890 NULL, NULL); /* continuation */
895 * Send keepalive packets for a connection.
897 * @param c Connection to keep alive..
898 * @param fwd Is this a FWD keepalive? (owner -> dest).
901 send_connection_keepalive (struct CadetConnection *c, int fwd)
903 struct GNUNET_MessageHeader msg;
904 struct CadetFlowControl *fc;
906 LOG (GNUNET_ERROR_TYPE_INFO, "keepalive %s for connection %s\n",
907 GC_f2s (fwd), GCC_2s (c));
909 fc = fwd ? &c->fwd_fc : &c->bck_fc;
912 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
915 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
917 GNUNET_assert (NULL != c->t);
918 msg.size = htons (sizeof (msg));
919 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
921 GNUNET_assert (NULL ==
922 GCT_send_prebuilt_message (&msg, c->t, c,
923 GNUNET_NO, NULL, NULL));
928 * Send CONNECTION_{CREATE/ACK} packets for a connection.
930 * @param c Connection for which to send the message.
931 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
934 connection_recreate (struct CadetConnection *c, int fwd)
936 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
940 send_connection_ack (c, GNUNET_NO);
945 * Generic connection timer management.
946 * Depending on the role of the peer in the connection will send the
947 * appropriate message (build or keepalive)
949 * @param c Conncetion to maintain.
953 connection_maintain (struct CadetConnection *c, int fwd)
955 if (GNUNET_NO != c->destroy)
957 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
964 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
968 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
970 /* If status is SEARCHING, why is there a connection? Should be WAITING */
972 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
973 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
974 schedule_next_keepalive (c, fwd);
979 case CADET_CONNECTION_NEW:
982 case CADET_CONNECTION_SENT:
983 connection_recreate (c, fwd);
985 case CADET_CONNECTION_READY:
986 send_connection_keepalive (c, fwd);
996 * Keep the connection alive.
998 * @param c Connection to keep alive.
999 * @param fwd Direction.
1000 * @param shutdown Are we shutting down? (Don't send traffic)
1001 * Non-zero value for true, not necessarily GNUNET_YES.
1004 connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
1006 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s keepalive for %s\n",
1007 GC_f2s (fwd), GCC_2s (c));
1010 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1012 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1014 if (GNUNET_NO != shutdown)
1017 connection_maintain (c, fwd);
1019 /* Next execution will be scheduled by message_sent or _maintain*/
1024 * Keep the connection alive in the FWD direction.
1026 * @param cls Closure (connection to keepalive).
1027 * @param tc TaskContext.
1030 connection_fwd_keepalive (void *cls,
1031 const struct GNUNET_SCHEDULER_TaskContext *tc)
1033 connection_keepalive ((struct CadetConnection *) cls,
1035 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1040 * Keep the connection alive in the BCK direction.
1042 * @param cls Closure (connection to keepalive).
1043 * @param tc TaskContext.
1046 connection_bck_keepalive (void *cls,
1047 const struct GNUNET_SCHEDULER_TaskContext *tc)
1049 connection_keepalive ((struct CadetConnection *) cls,
1051 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1056 * Schedule next keepalive task, taking in consideration
1057 * the connection state and number of retries.
1059 * If the peer is not the origin, do nothing.
1061 * @param c Connection for which to schedule the next keepalive.
1062 * @param fwd Direction for the next keepalive.
1065 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1067 struct GNUNET_TIME_Relative delay;
1068 GNUNET_SCHEDULER_TaskIdentifier *task_id;
1069 GNUNET_SCHEDULER_Task keepalive_task;
1071 if (GNUNET_NO == GCC_is_origin (c, fwd))
1074 /* Calculate delay to use, depending on the state of the connection */
1075 if (CADET_CONNECTION_READY == c->state)
1077 delay = refresh_connection_time;
1081 if (1 > c->create_retry)
1082 c->create_retry = 1;
1083 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1085 if (c->create_retry < 64)
1086 c->create_retry *= 2;
1089 /* Select direction-dependent parameters */
1090 if (GNUNET_YES == fwd)
1092 task_id = &c->fwd_maintenance_task;
1093 keepalive_task = &connection_fwd_keepalive;
1097 task_id = &c->bck_maintenance_task;
1098 keepalive_task = &connection_bck_keepalive;
1101 /* Check that no one scheduled it before us */
1102 if (GNUNET_SCHEDULER_NO_TASK != *task_id)
1104 /* No need for a _break. It can happen for instance when sending a SYNACK
1105 * for a duplicate SYN: the first SYNACK scheduled the task. */
1106 GNUNET_SCHEDULER_cancel (*task_id);
1109 /* Schedule the task */
1110 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1111 LOG (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1112 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1117 * @brief Re-initiate traffic on this connection if necessary.
1119 * Check if there is traffic queued towards this peer
1120 * and the core transmit handle is NULL (traffic was stalled).
1121 * If so, call core tmt rdy.
1123 * @param c Connection on which initiate traffic.
1124 * @param fwd Is this about fwd traffic?
1127 connection_unlock_queue (struct CadetConnection *c, int fwd)
1129 struct CadetPeer *peer;
1131 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_unlock_queue %s on %s\n",
1132 GC_f2s (fwd), GCC_2s (c));
1134 if (GCC_is_terminal (c, fwd))
1136 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal, can unlock!\n");
1140 peer = get_hop (c, fwd);
1141 GCP_queue_unlock (peer, c);
1146 * Cancel all transmissions that belong to a certain connection.
1148 * If the connection is scheduled for destruction and no more messages are left,
1149 * the connection will be destroyed by the continuation call.
1151 * @param c Connection which to cancel. Might be destroyed during this call.
1152 * @param fwd Cancel fwd traffic?
1155 connection_cancel_queues (struct CadetConnection *c, int fwd)
1157 struct CadetFlowControl *fc;
1158 struct CadetPeer *peer;
1160 LOG (GNUNET_ERROR_TYPE_DEBUG,
1161 " *** Cancel %s queues for connection %s\n",
1162 GC_f2s (fwd), GCC_2s (c));
1169 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1170 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1172 GNUNET_SCHEDULER_cancel (fc->poll_task);
1173 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1174 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1176 peer = get_hop (c, fwd);
1177 GCP_queue_cancel (peer, c);
1182 * Function called if a connection has been stalled for a while,
1183 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1185 * @param cls Closure (poll ctx).
1186 * @param tc TaskContext.
1189 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1193 * Callback called when a queued POLL message is sent.
1195 * @param cls Closure (FC).
1196 * @param c Connection this message was on.
1197 * @param q Queue handler this call invalidates.
1198 * @param type Type of message sent.
1199 * @param fwd Was this a FWD going message?
1200 * @param size Size of the message.
1203 poll_sent (void *cls,
1204 struct CadetConnection *c,
1205 struct CadetConnectionQueue *q,
1206 uint16_t type, int fwd, size_t size)
1208 struct CadetFlowControl *fc = cls;
1210 if (2 == c->destroy)
1212 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1215 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL sent for , scheduling new one!\n");
1216 fc->poll_msg = NULL;
1217 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1218 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1219 &connection_poll, fc);
1220 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1225 * Function called if a connection has been stalled for a while,
1226 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1228 * @param cls Closure (poll ctx).
1229 * @param tc TaskContext.
1232 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1234 struct CadetFlowControl *fc = cls;
1235 struct GNUNET_CADET_Poll msg;
1236 struct CadetConnection *c;
1238 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1239 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1245 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling connection %s %s\n",
1246 GCC_2s (c), fc == &c->fwd_fc ? "FWD" : "BCK");
1248 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1249 msg.header.size = htons (sizeof (msg));
1250 msg.pid = htonl (fc->last_pid_sent);
1251 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1253 GCC_send_prebuilt_message (&msg.header, 0, fc->last_pid_sent, c,
1254 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1255 GNUNET_assert (NULL != fc->poll_msg);
1260 * Timeout function due to lack of keepalive/traffic from the owner.
1261 * Destroys connection if called.
1263 * @param cls Closure (connection to destroy).
1264 * @param tc TaskContext.
1267 connection_fwd_timeout (void *cls,
1268 const struct GNUNET_SCHEDULER_TaskContext *tc)
1270 struct CadetConnection *c = cls;
1272 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1273 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1276 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1278 if (GCC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1289 * Timeout function due to lack of keepalive/traffic from the destination.
1290 * Destroys connection if called.
1292 * @param cls Closure (connection to destroy).
1293 * @param tc TaskContext
1296 connection_bck_timeout (void *cls,
1297 const struct GNUNET_SCHEDULER_TaskContext *tc)
1299 struct CadetConnection *c = cls;
1301 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1302 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1305 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1308 if (GCC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1319 * Resets the connection timeout task, some other message has done the
1321 * - For the first peer on the direction this means to send
1322 * a keepalive or a path confirmation message (either create or ACK).
1323 * - For all other peers, this means to destroy the connection,
1324 * due to lack of activity.
1325 * Starts the timeout if no timeout was running (connection just created).
1327 * @param c Connection whose timeout to reset.
1328 * @param fwd Is this forward?
1330 * TODO use heap to improve efficiency of scheduler.
1333 connection_reset_timeout (struct CadetConnection *c, int fwd)
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1337 if (GCC_is_origin (c, fwd)) /* Startpoint */
1339 schedule_next_keepalive (c, fwd);
1341 else /* Relay, endpoint. */
1343 struct GNUNET_TIME_Relative delay;
1344 GNUNET_SCHEDULER_TaskIdentifier *ti;
1345 GNUNET_SCHEDULER_Task f;
1347 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1349 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1350 GNUNET_SCHEDULER_cancel (*ti);
1351 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1352 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1353 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1359 * Add the connection to the list of both neighbors.
1361 * @param c Connection.
1363 * @return #GNUNET_OK if everything went fine
1364 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1367 register_neighbors (struct CadetConnection *c)
1369 struct CadetPeer *next_peer;
1370 struct CadetPeer *prev_peer;
1372 next_peer = get_next_hop (c);
1373 prev_peer = get_prev_hop (c);
1375 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1377 path_debug (c->path);
1378 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1379 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1380 GCC_2s (c), next_peer);
1381 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GCP_2s (next_peer));
1382 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1383 GCC_2s (c), prev_peer);
1384 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GCP_2s (prev_peer));
1386 if (GNUNET_NO == GCP_is_neighbor (next_peer)
1387 || GNUNET_NO == GCP_is_neighbor (prev_peer))
1389 if (GCC_is_origin (c, GNUNET_YES))
1390 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1391 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1393 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1394 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1395 GCP_2s (prev_peer), GCP_is_neighbor (prev_peer));
1396 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1397 GCP_2s (next_peer), GCP_is_neighbor (next_peer));
1398 return GNUNET_SYSERR;
1401 GCP_add_connection (next_peer, c);
1402 GCP_add_connection (prev_peer, c);
1409 * Remove the connection from the list of both neighbors.
1411 * @param c Connection.
1414 unregister_neighbors (struct CadetConnection *c)
1416 struct CadetPeer *peer;
1418 peer = get_next_hop (c);
1419 if (GNUNET_OK != GCP_remove_connection (peer, c))
1421 GNUNET_assert (CADET_CONNECTION_NEW == c->state
1422 || CADET_CONNECTION_DESTROYED <= c->state);
1423 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1424 if (NULL != c->t) GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1427 peer = get_prev_hop (c);
1428 if (GNUNET_OK != GCP_remove_connection (peer, c))
1430 GNUNET_assert (CADET_CONNECTION_NEW == c->state
1431 || CADET_CONNECTION_DESTROYED <= c->state);
1432 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1433 if (NULL != c->t) GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1439 * Bind the connection to the peer and the tunnel to that peer.
1441 * If the peer has no tunnel, create one. Update tunnel and connection
1442 * data structres to reflect new status.
1444 * @param c Connection.
1448 add_to_peer (struct CadetConnection *c, struct CadetPeer *peer)
1450 GCP_add_tunnel (peer);
1451 c->t = GCP_get_tunnel (peer);
1452 GCT_add_connection (c->t, c);
1457 * Builds a path from a PeerIdentity array.
1459 * @param peers PeerIdentity array.
1460 * @param size Size of the @c peers array.
1461 * @param own_pos Output parameter: own position in the path.
1463 * @return Fixed and shortened path.
1465 static struct CadetPeerPath *
1466 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1468 unsigned int *own_pos)
1470 struct CadetPeerPath *path;
1471 GNUNET_PEER_Id shortid;
1474 unsigned int offset;
1477 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1478 path = path_new (size);
1481 for (i = 0; i < size; i++)
1483 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1484 i, GNUNET_i2s (&peers[i]));
1485 shortid = GNUNET_PEER_intern (&peers[i]);
1487 /* Check for loops / duplicates */
1488 for (j = 0; j < i - offset; j++)
1490 if (path->peers[j] == shortid)
1492 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now %u\n", offset);
1495 GNUNET_PEER_change_rc (shortid, -1);
1498 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1499 path->peers[i - offset] = shortid;
1500 if (path->peers[i - offset] == myid)
1501 *own_pos = i - offset;
1503 path->length -= offset;
1505 if (path->peers[*own_pos] != myid)
1507 /* create path: self not found in path through self */
1508 GNUNET_break_op (0);
1509 path_destroy (path);
1518 * Log receipt of message on stderr (INFO level).
1520 * @param message Message received.
1521 * @param peer Peer who sent the message.
1522 * @param hash Connection ID.
1525 log_message (const struct GNUNET_MessageHeader *message,
1526 const struct GNUNET_PeerIdentity *peer,
1527 const struct GNUNET_CADET_Hash *hash)
1529 LOG (GNUNET_ERROR_TYPE_INFO, "<-- %s on connection %s from %s\n",
1530 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (hash)),
1534 /******************************************************************************/
1535 /******************************** API ***********************************/
1536 /******************************************************************************/
1539 * Core handler for connection creation.
1541 * @param cls Closure (unused).
1542 * @param peer Sender (neighbor).
1543 * @param message Message.
1545 * @return GNUNET_OK to keep the connection open,
1546 * GNUNET_SYSERR to close it (signal serious error)
1549 GCC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1550 const struct GNUNET_MessageHeader *message)
1552 struct GNUNET_CADET_ConnectionCreate *msg;
1553 struct GNUNET_PeerIdentity *id;
1554 struct GNUNET_CADET_Hash *cid;
1555 struct CadetPeerPath *path;
1556 struct CadetPeer *dest_peer;
1557 struct CadetPeer *orig_peer;
1558 struct CadetConnection *c;
1559 unsigned int own_pos;
1563 size = ntohs (message->size);
1564 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1566 GNUNET_break_op (0);
1570 /* Calculate hops */
1571 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1572 if (size % sizeof (struct GNUNET_PeerIdentity))
1574 GNUNET_break_op (0);
1577 size /= sizeof (struct GNUNET_PeerIdentity);
1580 GNUNET_break_op (0);
1583 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1585 /* Get parameters */
1586 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
1588 log_message (message, peer, cid);
1589 id = (struct GNUNET_PeerIdentity *) &msg[1];
1590 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1592 /* Create connection */
1593 c = connection_get (cid);
1596 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1602 GNUNET_break_op (0);
1603 path_destroy (path);
1606 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1608 c = GCC_new (cid, NULL, path_duplicate (path), own_pos);
1611 if (path->length - 1 == own_pos)
1613 /* If we are destination, why did the creation fail? */
1617 send_broken_unknown (cid, &my_full_id,
1618 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1620 path_destroy (path);
1623 GCP_add_path_to_all (path, GNUNET_NO);
1624 connection_reset_timeout (c, GNUNET_YES);
1628 path = path_duplicate (c->path);
1630 if (CADET_CONNECTION_NEW == c->state)
1631 connection_change_state (c, CADET_CONNECTION_SENT);
1633 /* Remember peers */
1634 dest_peer = GCP_get (&id[size - 1]);
1635 orig_peer = GCP_get (&id[0]);
1637 /* Is it a connection to us? */
1638 if (c->own_pos == path->length - 1)
1640 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1641 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1643 add_to_peer (c, orig_peer);
1644 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
1645 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
1647 send_connection_ack (c, GNUNET_NO);
1648 if (CADET_CONNECTION_SENT == c->state)
1649 connection_change_state (c, CADET_CONNECTION_ACK);
1653 /* It's for somebody else! Retransmit. */
1654 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1655 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1656 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1657 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
1658 GNUNET_YES, GNUNET_YES,
1661 path_destroy (path);
1667 * Core handler for path confirmations.
1669 * @param cls closure
1670 * @param message message
1671 * @param peer peer identity this notification is about
1673 * @return GNUNET_OK to keep the connection open,
1674 * GNUNET_SYSERR to close it (signal serious error)
1677 GCC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1678 const struct GNUNET_MessageHeader *message)
1680 struct GNUNET_CADET_ConnectionACK *msg;
1681 struct CadetConnection *c;
1682 struct CadetPeerPath *p;
1683 struct CadetPeer *pi;
1684 enum CadetConnectionState oldstate;
1687 msg = (struct GNUNET_CADET_ConnectionACK *) message;
1688 log_message (message, peer, &msg->cid);
1689 c = connection_get (&msg->cid);
1692 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1694 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1695 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
1699 if (GNUNET_NO != c->destroy)
1701 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1705 oldstate = c->state;
1706 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1707 pi = GCP_get (peer);
1708 if (get_next_hop (c) == pi)
1710 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1712 if (CADET_CONNECTION_SENT == oldstate)
1713 connection_change_state (c, CADET_CONNECTION_ACK);
1715 else if (get_prev_hop (c) == pi)
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
1719 connection_change_state (c, CADET_CONNECTION_READY);
1723 GNUNET_break_op (0);
1727 connection_reset_timeout (c, fwd);
1729 /* Add path to peers? */
1733 GCP_add_path_to_all (p, GNUNET_YES);
1740 /* Message for us as creator? */
1741 if (GCC_is_origin (c, GNUNET_YES))
1743 if (GNUNET_NO != fwd)
1745 GNUNET_break_op (0);
1748 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1750 /* If just created, cancel the short timeout and start a long one */
1751 if (CADET_CONNECTION_SENT == oldstate)
1752 connection_reset_timeout (c, GNUNET_YES);
1754 /* Change connection state */
1755 connection_change_state (c, CADET_CONNECTION_READY);
1756 send_connection_ack (c, GNUNET_YES);
1758 /* Change tunnel state, trigger KX */
1759 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
1760 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
1765 /* Message for us as destination? */
1766 if (GCC_is_terminal (c, GNUNET_YES))
1768 if (GNUNET_YES != fwd)
1770 GNUNET_break_op (0);
1773 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1775 /* If just created, cancel the short timeout and start a long one */
1776 if (CADET_CONNECTION_ACK == oldstate)
1777 connection_reset_timeout (c, GNUNET_NO);
1779 /* Change tunnel state */
1780 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
1781 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
1786 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1787 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
1788 GNUNET_YES, NULL, NULL));
1794 * Core handler for notifications of broken connections.
1796 * @param cls Closure (unused).
1797 * @param id Peer identity of sending neighbor.
1798 * @param message Message.
1800 * @return GNUNET_OK to keep the connection open,
1801 * GNUNET_SYSERR to close it (signal serious error)
1804 GCC_handle_broken (void* cls,
1805 const struct GNUNET_PeerIdentity* id,
1806 const struct GNUNET_MessageHeader* message)
1808 struct GNUNET_CADET_ConnectionBroken *msg;
1809 struct CadetConnection *c;
1810 struct CadetTunnel *t;
1814 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
1815 log_message (message, id, &msg->cid);
1816 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1817 GNUNET_i2s (&msg->peer1));
1818 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1819 GNUNET_i2s (&msg->peer2));
1820 c = connection_get (&msg->cid);
1823 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
1828 fwd = is_fwd (c, id);
1829 if (GCC_is_terminal (c, fwd))
1831 struct GNUNET_MessageHeader *out_msg;
1832 struct CadetPeer *neighbor;
1833 struct CadetPeer *endpoint;
1837 /* A terminal connection should not have 't' set to NULL. */
1839 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1842 neighbor = get_hop (c, !fwd);
1843 endpoint = GCP_get_short (c->path->peers[c->path->length - 1]);
1844 path_invalidate (c->path);
1845 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
1846 c->state = CADET_CONNECTION_BROKEN;
1847 GCT_remove_connection (t, c);
1849 destroyed = GNUNET_NO;
1851 /* GCP_connection_pop could destroy the connection! */
1852 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1854 GCT_resend_message (out_msg, t);
1856 /* All pending messages should have been popped,
1857 * and the connection destroyed by the continuation.
1859 if (GNUNET_YES != destroyed)
1867 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
1868 GNUNET_YES, NULL, NULL));
1869 c->destroy = GNUNET_YES;
1870 connection_cancel_queues (c, !fwd);
1878 * Core handler for tunnel destruction
1880 * @param cls Closure (unused).
1881 * @param peer Peer identity of sending neighbor.
1882 * @param message Message.
1884 * @return GNUNET_OK to keep the connection open,
1885 * GNUNET_SYSERR to close it (signal serious error)
1888 GCC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1889 const struct GNUNET_MessageHeader *message)
1891 struct GNUNET_CADET_ConnectionDestroy *msg;
1892 struct CadetConnection *c;
1895 msg = (struct GNUNET_CADET_ConnectionDestroy *) message;
1896 log_message (message, peer, &msg->cid);
1897 c = connection_get (&msg->cid);
1900 /* Probably already got the message from another path,
1901 * destroyed the tunnel and retransmitted to children.
1904 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1906 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1909 fwd = is_fwd (c, peer);
1910 if (GNUNET_SYSERR == fwd)
1912 GNUNET_break_op (0); /* FIXME */
1915 if (GNUNET_NO == GCC_is_terminal (c, fwd))
1916 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
1917 GNUNET_YES, NULL, NULL));
1918 else if (0 == c->pending_messages)
1920 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
1924 c->destroy = GNUNET_YES;
1925 c->state = CADET_CONNECTION_DESTROYED;
1928 GCT_remove_connection (c->t, c);
1936 * Generic handler for cadet network encrypted traffic.
1938 * @param peer Peer identity this notification is about.
1939 * @param msg Encrypted message.
1941 * @return GNUNET_OK to keep the connection open,
1942 * GNUNET_SYSERR to close it (signal serious error)
1945 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
1946 const struct GNUNET_CADET_Encrypted *msg)
1948 struct CadetConnection *c;
1949 struct CadetPeer *neighbor;
1950 struct CadetFlowControl *fc;
1951 GNUNET_PEER_Id peer_id;
1957 log_message (&msg->header, peer, &msg->cid);
1960 size = ntohs (msg->header.size);
1962 sizeof (struct GNUNET_CADET_Encrypted) +
1963 sizeof (struct GNUNET_MessageHeader))
1965 GNUNET_break_op (0);
1969 /* Check connection */
1970 c = connection_get (&msg->cid);
1973 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1974 LOG (GNUNET_ERROR_TYPE_DEBUG, "enc on unknown connection %s\n",
1975 GNUNET_h2s (GC_h2hc (&msg->cid)));
1976 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
1980 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s\n", GCC_2s (c));
1982 /* Check if origin is as expected */
1983 neighbor = get_prev_hop (c);
1984 peer_id = GNUNET_PEER_search (peer);
1985 if (peer_id == GCP_get_short_id (neighbor))
1991 neighbor = get_next_hop (c);
1992 if (peer_id == GCP_get_short_id (neighbor))
1998 /* Unexpected peer sending traffic on a connection. */
1999 GNUNET_break_op (0);
2005 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2006 pid = ntohl (msg->pid);
2007 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u+)\n",
2008 pid, fc->last_pid_recv + 1);
2009 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2011 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2012 GNUNET_break_op (0);
2013 LOG (GNUNET_ERROR_TYPE_WARNING,
2014 "Received PID %u, (prev %u), ACK %u\n",
2015 pid, fc->last_pid_recv, fc->last_ack_sent);
2018 if (GNUNET_NO == GC_is_pid_bigger (pid, fc->last_pid_recv))
2020 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
2021 LOG (GNUNET_ERROR_TYPE_DEBUG,
2022 " PID %u not expected (%u+), dropping!\n",
2023 pid, fc->last_pid_recv + 1);
2026 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2027 connection_change_state (c, CADET_CONNECTION_READY);
2028 connection_reset_timeout (c, fwd);
2029 fc->last_pid_recv = pid;
2031 /* Is this message for us? */
2032 if (GCC_is_terminal (c, fwd))
2034 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2035 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2039 GNUNET_break (GNUNET_NO != c->destroy);
2042 fc->last_pid_recv = pid;
2043 GCT_handle_encrypted (c->t, msg);
2044 GCC_send_ack (c, fwd, GNUNET_NO);
2048 /* Message not for us: forward to next hop */
2049 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2050 ttl = ntohl (msg->ttl);
2051 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2054 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2055 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2056 GCC_send_ack (c, fwd, GNUNET_NO);
2060 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2061 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2062 GNUNET_NO, NULL, NULL));
2068 * Generic handler for cadet network encrypted traffic.
2070 * @param peer Peer identity this notification is about.
2071 * @param msg Encrypted message.
2073 * @return GNUNET_OK to keep the connection open,
2074 * GNUNET_SYSERR to close it (signal serious error)
2077 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2078 const struct GNUNET_CADET_KX *msg)
2080 struct CadetConnection *c;
2081 struct CadetPeer *neighbor;
2082 GNUNET_PEER_Id peer_id;
2086 log_message (&msg->header, peer, &msg->cid);
2089 size = ntohs (msg->header.size);
2091 sizeof (struct GNUNET_CADET_KX) +
2092 sizeof (struct GNUNET_MessageHeader))
2094 GNUNET_break_op (0);
2098 /* Check connection */
2099 c = connection_get (&msg->cid);
2102 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
2103 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx on unknown connection %s\n",
2104 GNUNET_h2s (GC_h2hc (&msg->cid)));
2105 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2108 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GCC_2s (c));
2110 /* Check if origin is as expected */
2111 neighbor = get_prev_hop (c);
2112 peer_id = GNUNET_PEER_search (peer);
2113 if (peer_id == GCP_get_short_id (neighbor))
2119 neighbor = get_next_hop (c);
2120 if (peer_id == GCP_get_short_id (neighbor))
2126 /* Unexpected peer sending traffic on a connection. */
2127 GNUNET_break_op (0);
2132 /* Count as connection confirmation. */
2133 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2135 connection_change_state (c, CADET_CONNECTION_READY);
2138 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2139 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2142 connection_reset_timeout (c, fwd);
2144 /* Is this message for us? */
2145 if (GCC_is_terminal (c, fwd))
2147 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2148 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2154 GCT_handle_kx (c->t, &msg[1].header);
2158 /* Message not for us: forward to next hop */
2159 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2160 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2161 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2162 GNUNET_NO, NULL, NULL));
2169 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2171 * @param cls Closure (unused).
2172 * @param message Message received.
2173 * @param peer Peer who sent the message.
2175 * @return GNUNET_OK to keep the connection open,
2176 * GNUNET_SYSERR to close it (signal serious error)
2179 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2180 const struct GNUNET_MessageHeader *message)
2182 return handle_cadet_encrypted (peer,
2183 (struct GNUNET_CADET_Encrypted *)message);
2188 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2190 * @param cls Closure (unused).
2191 * @param message Message received.
2192 * @param peer Peer who sent the message.
2194 * @return GNUNET_OK to keep the connection open,
2195 * GNUNET_SYSERR to close it (signal serious error)
2198 GCC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2199 const struct GNUNET_MessageHeader *message)
2201 return handle_cadet_kx (peer,
2202 (struct GNUNET_CADET_KX *) message);
2207 * Core handler for cadet network traffic point-to-point acks.
2209 * @param cls closure
2210 * @param message message
2211 * @param peer peer identity this notification is about
2213 * @return GNUNET_OK to keep the connection open,
2214 * GNUNET_SYSERR to close it (signal serious error)
2217 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2218 const struct GNUNET_MessageHeader *message)
2220 struct GNUNET_CADET_ACK *msg;
2221 struct CadetConnection *c;
2222 struct CadetFlowControl *fc;
2227 msg = (struct GNUNET_CADET_ACK *) message;
2228 log_message (message, peer, &msg->cid);
2229 c = connection_get (&msg->cid);
2232 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2234 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2238 /* Is this a forward or backward ACK? */
2239 id = GNUNET_PEER_search (peer);
2240 if (GCP_get_short_id (get_next_hop (c)) == id)
2242 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2246 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2248 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2254 GNUNET_break_op (0);
2258 ack = ntohl (msg->ack);
2259 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2260 ack, fc->last_ack_recv);
2261 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2262 fc->last_ack_recv = ack;
2264 /* Cancel polling if the ACK is big enough. */
2265 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2266 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2268 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2269 GNUNET_SCHEDULER_cancel (fc->poll_task);
2270 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2271 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2274 connection_unlock_queue (c, fwd);
2281 * Core handler for cadet network traffic point-to-point ack polls.
2283 * @param cls closure
2284 * @param message message
2285 * @param peer peer identity this notification is about
2287 * @return GNUNET_OK to keep the connection open,
2288 * GNUNET_SYSERR to close it (signal serious error)
2291 GCC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2292 const struct GNUNET_MessageHeader *message)
2294 struct GNUNET_CADET_Poll *msg;
2295 struct CadetConnection *c;
2296 struct CadetFlowControl *fc;
2301 msg = (struct GNUNET_CADET_Poll *) message;
2302 log_message (message, peer, &msg->cid);
2303 c = connection_get (&msg->cid);
2306 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2308 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL message on unknown connection %s!\n",
2309 GNUNET_h2s (GC_h2hc (&msg->cid)));
2310 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2314 /* Is this a forward or backward ACK?
2315 * Note: a poll should never be needed in a loopback case,
2316 * since there is no possiblility of packet loss there, so
2317 * this way of discerining FWD/BCK should not be a problem.
2319 id = GNUNET_PEER_search (peer);
2320 if (GCP_get_short_id (get_next_hop (c)) == id)
2322 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2325 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2327 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2332 GNUNET_break_op (0);
2336 pid = ntohl (msg->pid);
2337 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2338 fc->last_pid_recv = pid;
2339 fwd = fc == &c->bck_fc;
2340 GCC_send_ack (c, fwd, GNUNET_YES);
2347 * Send an ACK on the appropriate connection/channel, depending on
2348 * the direction and the position of the peer.
2350 * @param c Which connection to send the hop-by-hop ACK.
2351 * @param fwd Is this a fwd ACK? (will go dest->root).
2352 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2355 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2357 unsigned int buffer;
2359 LOG (GNUNET_ERROR_TYPE_DEBUG,
2360 "GMC send %s ACK on %s\n",
2361 GC_f2s (fwd), GCC_2s (c));
2369 if (GNUNET_NO != c->destroy)
2371 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2375 /* Get available buffer space */
2376 if (GCC_is_terminal (c, fwd))
2378 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2379 buffer = GCT_get_channels_buffer (c->t);
2383 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2384 buffer = GCC_get_buffer (c, fwd);
2386 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2387 if (0 == buffer && GNUNET_NO == force)
2390 /* Send available buffer space */
2391 if (GCC_is_origin (c, fwd))
2393 GNUNET_assert (NULL != c->t);
2394 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2395 GCT_unchoke_channels (c->t);
2399 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2400 send_ack (c, buffer, fwd, force);
2406 * Initialize the connections subsystem
2408 * @param c Configuration handle.
2411 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2413 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2415 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2418 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2419 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2420 GNUNET_SCHEDULER_shutdown ();
2425 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2428 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2429 "CADET", "MAX_CONNECTIONS", "MISSING");
2430 GNUNET_SCHEDULER_shutdown ();
2435 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2436 &refresh_connection_time))
2438 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2439 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2440 GNUNET_SCHEDULER_shutdown ();
2443 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2444 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2449 * Destroy each connection on shutdown.
2451 * @param cls Closure (unused).
2452 * @param key Current key code (CID, unused).
2453 * @param value Value in the hash map (connection)
2455 * @return #GNUNET_YES, because we should continue to iterate,
2458 shutdown_iterator (void *cls,
2459 const struct GNUNET_HashCode *key,
2462 struct CadetConnection *c = value;
2470 * Shut down the connections subsystem.
2475 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2476 GNUNET_CONTAINER_multihashmap_destroy (connections);
2481 struct CadetConnection *
2482 GCC_new (const struct GNUNET_CADET_Hash *cid,
2483 struct CadetTunnel *t,
2484 struct CadetPeerPath *p,
2485 unsigned int own_pos)
2487 struct CadetConnection *c;
2489 c = GNUNET_new (struct CadetConnection);
2491 GNUNET_assert (GNUNET_OK ==
2492 GNUNET_CONTAINER_multihashmap_put (connections,
2494 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2495 fc_init (&c->fwd_fc);
2496 fc_init (&c->bck_fc);
2501 GNUNET_assert (own_pos <= p->length - 1);
2502 c->own_pos = own_pos;
2506 if (GNUNET_OK != register_neighbors (c))
2510 path_invalidate (c->path);
2523 GCC_destroy (struct CadetConnection *c)
2531 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2532 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2535 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GCC_2s (c));
2536 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2537 &c->fwd_fc, &c->bck_fc);
2538 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2539 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2541 /* Cancel all traffic */
2542 if (NULL != c->path)
2544 connection_cancel_queues (c, GNUNET_YES);
2545 connection_cancel_queues (c, GNUNET_NO);
2546 unregister_neighbors (c);
2549 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2550 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2552 /* Cancel maintainance task (keepalive/timeout) */
2553 if (NULL != c->fwd_fc.poll_msg)
2555 GCC_cancel (c->fwd_fc.poll_msg);
2556 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2558 if (NULL != c->bck_fc.poll_msg)
2560 GCC_cancel (c->bck_fc.poll_msg);
2561 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2564 /* Delete from tunnel */
2566 GCT_remove_connection (c->t, c);
2568 if (GNUNET_NO == GCC_is_origin (c, GNUNET_YES) && NULL != c->path)
2569 path_destroy (c->path);
2570 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2571 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2572 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2573 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2574 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2576 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2577 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2579 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2581 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2582 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2585 GNUNET_break (GNUNET_YES ==
2586 GNUNET_CONTAINER_multihashmap_remove (connections,
2589 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2594 * Get the connection ID.
2596 * @param c Connection to get the ID from.
2598 * @return ID of the connection.
2600 const struct GNUNET_CADET_Hash *
2601 GCC_get_id (const struct CadetConnection *c)
2608 * Get the connection ID.
2610 * @param c Connection to get the ID from.
2612 * @return ID of the connection.
2614 const struct GNUNET_HashCode *
2615 GCC_get_h (const struct CadetConnection *c)
2617 return GC_h2hc (&c->id);
2622 * Get the connection path.
2624 * @param c Connection to get the path from.
2626 * @return path used by the connection.
2628 const struct CadetPeerPath *
2629 GCC_get_path (const struct CadetConnection *c)
2631 if (GNUNET_NO == c->destroy)
2638 * Get the connection state.
2640 * @param c Connection to get the state from.
2642 * @return state of the connection.
2644 enum CadetConnectionState
2645 GCC_get_state (const struct CadetConnection *c)
2651 * Get the connection tunnel.
2653 * @param c Connection to get the tunnel from.
2655 * @return tunnel of the connection.
2657 struct CadetTunnel *
2658 GCC_get_tunnel (const struct CadetConnection *c)
2665 * Get free buffer space in a connection.
2667 * @param c Connection.
2668 * @param fwd Is query about FWD traffic?
2670 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2673 GCC_get_buffer (struct CadetConnection *c, int fwd)
2675 struct CadetFlowControl *fc;
2677 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2679 return (fc->queue_max - fc->queue_n);
2683 * Get how many messages have we allowed to send to us from a direction.
2685 * @param c Connection.
2686 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2688 * @return last_ack_sent - last_pid_recv
2691 GCC_get_allowed (struct CadetConnection *c, int fwd)
2693 struct CadetFlowControl *fc;
2695 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2696 if (GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
2700 return (fc->last_ack_sent - fc->last_pid_recv);
2704 * Get messages queued in a connection.
2706 * @param c Connection.
2707 * @param fwd Is query about FWD traffic?
2709 * @return Number of messages queued.
2712 GCC_get_qn (struct CadetConnection *c, int fwd)
2714 struct CadetFlowControl *fc;
2716 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2723 * Get next PID to use.
2725 * @param c Connection.
2726 * @param fwd Is query about FWD traffic?
2728 * @return Last PID used + 1.
2731 GCC_get_pid (struct CadetConnection *c, int fwd)
2733 struct CadetFlowControl *fc;
2735 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2737 return fc->last_pid_sent + 1;
2742 * Allow the connection to advertise a buffer of the given size.
2744 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2745 * allowing up to last_pid_recv + buffer.
2747 * @param c Connection.
2748 * @param buffer How many more messages the connection can accept.
2749 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2752 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
2754 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2755 GCC_2s (c), buffer, GC_f2s (fwd));
2756 send_ack (c, buffer, fwd, GNUNET_NO);
2761 * Notify other peers on a connection of a broken link. Mark connections
2762 * to destroy after all traffic has been sent.
2764 * @param c Connection on which there has been a disconnection.
2765 * @param peer Peer that disconnected.
2768 GCC_notify_broken (struct CadetConnection *c,
2769 struct CadetPeer *peer)
2773 LOG (GNUNET_ERROR_TYPE_DEBUG,
2774 " notify broken on %s due to %s disconnect\n",
2775 GCC_2s (c), GCP_2s (peer));
2777 fwd = peer == get_prev_hop (c);
2779 if (GNUNET_YES == GCC_is_terminal (c, fwd))
2781 /* Local shutdown, no one to notify about this. */
2785 if (GNUNET_NO == c->destroy)
2786 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
2788 /* Connection will have at least one pending message
2789 * (the one we just scheduled), so no point in checking whether to
2790 * destroy immediately. */
2791 c->destroy = GNUNET_YES;
2792 c->state = CADET_CONNECTION_DESTROYED;
2795 * Cancel all queues, if no message is left, connection will be destroyed.
2797 connection_cancel_queues (c, !fwd);
2804 * Is this peer the first one on the connection?
2806 * @param c Connection.
2807 * @param fwd Is this about fwd traffic?
2809 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2812 GCC_is_origin (struct CadetConnection *c, int fwd)
2814 if (!fwd && c->path->length - 1 == c->own_pos )
2816 if (fwd && 0 == c->own_pos)
2823 * Is this peer the last one on the connection?
2825 * @param c Connection.
2826 * @param fwd Is this about fwd traffic?
2827 * Note that the ROOT is the terminal for BCK traffic!
2829 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2832 GCC_is_terminal (struct CadetConnection *c, int fwd)
2834 return GCC_is_origin (c, !fwd);
2839 * See if we are allowed to send by the next hop in the given direction.
2841 * @param c Connection.
2842 * @param fwd Is this about fwd traffic?
2844 * @return #GNUNET_YES in case it's OK to send.
2847 GCC_is_sendable (struct CadetConnection *c, int fwd)
2849 struct CadetFlowControl *fc;
2851 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking sendability of %s traffic on %s\n",
2852 GC_f2s (fwd), GCC_2s (c));
2858 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2859 LOG (GNUNET_ERROR_TYPE_DEBUG,
2860 " last ack recv: %u, last pid sent: %u\n",
2861 fc->last_ack_recv, fc->last_pid_sent);
2862 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2864 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
2867 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
2873 * Check if this connection is a direct one (never trim a direct connection).
2875 * @param c Connection.
2877 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
2880 GCC_is_direct (struct CadetConnection *c)
2882 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
2886 * Sends an already built message on a connection, properly registering
2887 * all used resources.
2889 * @param message Message to send. Function makes a copy of it.
2890 * If message is not hop-by-hop, decrements TTL of copy.
2891 * @param payload_type Type of payload, in case the message is encrypted.
2892 * @param c Connection on which this message is transmitted.
2893 * @param fwd Is this a fwd message?
2894 * @param force Force the connection to accept the message (buffer overfill).
2895 * @param cont Continuation called once message is sent. Can be NULL.
2896 * @param cont_cls Closure for @c cont.
2898 * @return Handle to cancel the message before it's sent.
2899 * NULL on error or if @c cont is NULL.
2900 * Invalid on @c cont call.
2902 struct CadetConnectionQueue *
2903 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2904 uint16_t payload_type, uint32_t payload_id,
2905 struct CadetConnection *c, int fwd, int force,
2906 GCC_sent cont, void *cont_cls)
2908 struct CadetFlowControl *fc;
2909 struct CadetConnectionQueue *q;
2915 size = ntohs (message->size);
2916 data = GNUNET_malloc (size);
2917 memcpy (data, message, size);
2918 type = ntohs (message->type);
2919 LOG (GNUNET_ERROR_TYPE_INFO, "--> %s (%s %u) on connection %s (%u bytes)\n",
2920 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), size);
2922 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2923 droppable = GNUNET_NO == force;
2926 struct GNUNET_CADET_Encrypted *emsg;
2927 struct GNUNET_CADET_KX *kmsg;
2928 struct GNUNET_CADET_ACK *amsg;
2929 struct GNUNET_CADET_Poll *pmsg;
2930 struct GNUNET_CADET_ConnectionDestroy *dmsg;
2931 struct GNUNET_CADET_ConnectionBroken *bmsg;
2934 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
2935 emsg = (struct GNUNET_CADET_Encrypted *) data;
2936 ttl = ntohl (emsg->ttl);
2939 GNUNET_break_op (0);
2944 emsg->ttl = htonl (ttl - 1);
2945 emsg->pid = htonl (0);
2946 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2947 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2948 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2949 if (GNUNET_YES == droppable)
2955 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2957 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2959 GCC_start_poll (c, fwd);
2963 case GNUNET_MESSAGE_TYPE_CADET_KX:
2964 kmsg = (struct GNUNET_CADET_KX *) data;
2968 case GNUNET_MESSAGE_TYPE_CADET_ACK:
2969 amsg = (struct GNUNET_CADET_ACK *) data;
2971 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2972 droppable = GNUNET_NO;
2975 case GNUNET_MESSAGE_TYPE_CADET_POLL:
2976 pmsg = (struct GNUNET_CADET_Poll *) data;
2978 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2979 droppable = GNUNET_NO;
2982 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
2983 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
2987 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
2988 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
2992 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
2995 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
2996 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3005 if (fc->queue_n > fc->queue_max && droppable)
3007 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3010 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3011 fc->queue_n, fc->queue_max);
3012 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3017 return NULL; /* Drop this message */
3020 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3021 GCC_2s (c), c->pending_messages);
3022 c->pending_messages++;
3024 q = GNUNET_new (struct CadetConnectionQueue);
3025 q->forced = !droppable;
3026 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3027 size, c, fwd, &conn_message_sent, q);
3030 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3036 q->cont_cls = cont_cls;
3037 return (NULL == cont) ? NULL : q;
3042 * Cancel a previously sent message while it's in the queue.
3044 * ONLY can be called before the continuation given to the send function
3045 * is called. Once the continuation is called, the message is no longer in the
3048 * @param q Handle to the queue.
3051 GCC_cancel (struct CadetConnectionQueue *q)
3053 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
3055 /* queue destroy calls message_sent, which calls q->cont and frees q */
3056 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3061 * Sends a CREATE CONNECTION message for a path to a peer.
3062 * Changes the connection and tunnel states if necessary.
3064 * @param connection Connection to create.
3067 GCC_send_create (struct CadetConnection *connection)
3069 enum CadetTunnelCState state;
3072 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3073 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3075 LOG (GNUNET_ERROR_TYPE_INFO, "===> %s on connection %s (%u bytes)\n",
3076 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE),
3077 GCC_2s (connection), size);
3078 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3079 connection, connection->pending_messages);
3080 connection->pending_messages++;
3082 connection->maintenance_q =
3083 GCP_queue_add (get_next_hop (connection), NULL,
3084 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 0,
3085 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3087 state = GCT_get_cstate (connection->t);
3088 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3089 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3090 if (CADET_CONNECTION_NEW == connection->state)
3091 connection_change_state (connection, CADET_CONNECTION_SENT);
3096 * Send a message to all peers in this connection that the connection
3097 * is no longer valid.
3099 * If some peer should not receive the message, it should be zero'ed out
3100 * before calling this function.
3102 * @param c The connection whose peers to notify.
3105 GCC_send_destroy (struct CadetConnection *c)
3107 struct GNUNET_CADET_ConnectionDestroy msg;
3109 if (GNUNET_YES == c->destroy)
3112 msg.header.size = htons (sizeof (msg));
3113 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3115 LOG (GNUNET_ERROR_TYPE_DEBUG,
3116 " sending connection destroy for connection %s\n",
3119 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3120 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3121 GNUNET_YES, GNUNET_YES,
3123 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3124 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3125 GNUNET_NO, GNUNET_YES,
3127 c->destroy = GNUNET_YES;
3128 c->state = CADET_CONNECTION_DESTROYED;
3133 * @brief Start a polling timer for the connection.
3135 * When a neighbor does not accept more traffic on the connection it could be
3136 * caused by a simple congestion or by a lost ACK. Polling enables to check
3137 * for the lastest ACK status for a connection.
3139 * @param c Connection.
3140 * @param fwd Should we poll in the FWD direction?
3143 GCC_start_poll (struct CadetConnection *c, int fwd)
3145 struct CadetFlowControl *fc;
3147 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3148 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
3150 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
3152 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
3153 fc->poll_task, fc->poll_msg);
3156 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
3157 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3164 * @brief Stop polling a connection for ACKs.
3166 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3168 * @param c Connection.
3169 * @param fwd Should we stop the poll in the FWD direction?
3172 GCC_stop_poll (struct CadetConnection *c, int fwd)
3174 struct CadetFlowControl *fc;
3176 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3177 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3179 GNUNET_SCHEDULER_cancel (fc->poll_task);
3180 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3185 * Get a (static) string for a connection.
3187 * @param c Connection.
3190 GCC_2s (const struct CadetConnection *c)
3197 static char buf[128];
3199 sprintf (buf, "%s (->%s)",
3200 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3203 return GNUNET_h2s (GC_h2hc (&c->id));
3208 * Log all possible info about the connection state.
3210 * @param c Connection to debug.
3211 * @param level Debug level to use.
3214 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3219 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3221 __FILE__, __FUNCTION__, __LINE__);
3227 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3231 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3232 s = path_2s (c->path);
3233 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3235 LOG2 (level, "CCC state: %s, destroy: %u\n",
3236 GCC_state2s (c->state), c->destroy);
3237 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3238 if (NULL != c->perf)
3239 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3241 LOG2 (level, "CCC FWD flow control:\n");
3242 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3243 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3244 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3245 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3246 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3247 LOG2 (level, "CCC POLL: task %d, msg %p, msg_ack %p)\n",
3248 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3250 LOG2 (level, "CCC BCK flow control:\n");
3251 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3252 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3253 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3254 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3255 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3256 LOG2 (level, "CCC POLL: task %d, msg %p, msg_ack %p)\n",
3257 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3259 LOG2 (level, "CCC DEBUG CONNECTION END\n");