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";
382 return "CADET_CONNECTION_STATE_ERROR";
388 * Initialize a Flow Control structure to the initial state.
390 * @param fc Flow Control structure to initialize.
393 fc_init (struct CadetFlowControl *fc)
395 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
396 fc->last_pid_recv = (uint32_t) -1;
397 fc->last_ack_sent = (uint32_t) 0;
398 fc->last_ack_recv = (uint32_t) 0;
399 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
400 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
402 fc->queue_max = (max_msgs_queue / max_connections) + 1;
409 * @param cid Connection ID.
411 static struct CadetConnection *
412 connection_get (const struct GNUNET_CADET_Hash *cid)
414 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
419 connection_change_state (struct CadetConnection* c,
420 enum CadetConnectionState state)
422 LOG (GNUNET_ERROR_TYPE_DEBUG,
423 "Connection %s state %s -> %s\n",
424 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
425 if (CADET_CONNECTION_DESTROYED == c->state)
427 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
431 if (CADET_CONNECTION_READY == state)
437 * Callback called when a queued ACK message is sent.
439 * @param cls Closure (FC).
440 * @param c Connection this message was on.
441 * @param q Queue handler this call invalidates.
442 * @param type Type of message sent.
443 * @param fwd Was this a FWD going message?
444 * @param size Size of the message.
448 struct CadetConnection *c,
449 struct CadetConnectionQueue *q,
450 uint16_t type, int fwd, size_t size)
452 struct CadetFlowControl *fc = cls;
459 * Send an ACK on the connection, informing the predecessor about
460 * the available buffer space. Should not be called in case the peer
461 * is origin (no predecessor) in the @c fwd direction.
463 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
464 * the ACK itself goes "back" (dest->root).
466 * @param c Connection on which to send the ACK.
467 * @param buffer How much space free to advertise?
468 * @param fwd Is this FWD ACK? (Going dest -> root)
469 * @param force Don't optimize out.
472 send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
474 struct CadetFlowControl *next_fc;
475 struct CadetFlowControl *prev_fc;
476 struct GNUNET_CADET_ACK msg;
480 /* If origin, there is no connection to send ACKs. Wrong function! */
481 if (GCC_is_origin (c, fwd))
483 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
484 GCC_2s (c), GC_f2s (fwd));
489 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
490 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
492 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
493 GC_f2s (fwd), GCC_2s (c));
495 /* Check if we need to transmit the ACK. */
496 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
497 if (3 < delta && buffer < delta && GNUNET_NO == force)
499 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
500 LOG (GNUNET_ERROR_TYPE_DEBUG,
501 " last pid recv: %u, last ack sent: %u\n",
502 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
506 /* Ok, ACK might be necessary, what PID to ACK? */
507 ack = prev_fc->last_pid_recv + buffer;
508 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
509 LOG (GNUNET_ERROR_TYPE_DEBUG,
510 " last pid %u, last ack %u, qmax %u, q %u\n",
511 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
512 next_fc->queue_max, next_fc->queue_n);
513 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
515 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
519 /* Check if message is already in queue */
520 if (NULL != prev_fc->ack_msg)
522 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
524 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
525 GCC_cancel (prev_fc->ack_msg);
526 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
530 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
535 prev_fc->last_ack_sent = ack;
537 /* Build ACK message and send on connection */
538 msg.header.size = htons (sizeof (msg));
539 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
540 msg.ack = htonl (ack);
543 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, 0, ack, c,
546 GNUNET_assert (NULL != prev_fc->ack_msg);
551 * Callback called when a connection queued message is sent.
553 * Calculates the average time and connection packet tracking.
555 * @param cls Closure (ConnectionQueue Handle).
556 * @param c Connection this message was on.
557 * @param sent Was it really sent? (Could have been canceled)
558 * @param type Type of message sent.
559 * @param pid Packet ID, or 0 if not applicable (create, destroy, etc).
560 * @param fwd Was this a FWD going message?
561 * @param size Size of the message.
562 * @param wait Time spent waiting for core (only the time for THIS message)
565 conn_message_sent (void *cls,
566 struct CadetConnection *c, int sent,
567 uint16_t type, uint32_t pid, int fwd, size_t size,
568 struct GNUNET_TIME_Relative wait)
570 struct CadetConnectionPerformance *p;
571 struct CadetFlowControl *fc;
572 struct CadetConnectionQueue *q = cls;
576 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
578 fc = fwd ? &c->fwd_fc : &c->bck_fc;
579 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s\n",
580 sent ? "" : "not ", GC_f2s (fwd), GC_m2s (type));
586 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
587 q->cont (q->cont_cls, c, q, type, fwd, size);
591 else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED)
593 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
602 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
603 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
605 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
610 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
611 c->pending_messages--;
612 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
614 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
618 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
621 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
622 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
623 c->maintenance_q = NULL;
624 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
625 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
626 schedule_next_keepalive (c, fwd);
629 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
630 if (GNUNET_YES == sent)
632 GNUNET_assert (NULL != q);
633 fc->last_pid_sent = pid; // FIXME
634 GCC_send_ack (c, fwd, GNUNET_NO);
635 connection_reset_timeout (c, fwd);
638 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
639 if (GNUNET_NO == forced)
642 LOG (GNUNET_ERROR_TYPE_DEBUG,
643 "! accounting pid %u\n",
648 LOG (GNUNET_ERROR_TYPE_DEBUG,
649 "! forced, Q_N not accounting pid %u\n",
654 case GNUNET_MESSAGE_TYPE_CADET_KX:
655 if (GNUNET_YES == sent)
656 connection_reset_timeout (c, fwd);
659 case GNUNET_MESSAGE_TYPE_CADET_POLL:
663 case GNUNET_MESSAGE_TYPE_CADET_ACK:
670 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
673 return; /* Only endpoints are interested in timing. */
676 usecsperbyte = ((double) wait.rel_value_us) / size;
677 if (p->size == AVG_MSGS)
679 /* Array is full. Substract oldest value, add new one and store. */
680 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
681 p->usecsperbyte[p->idx] = usecsperbyte;
682 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
686 /* Array not yet full. Add current value to avg and store. */
687 p->usecsperbyte[p->idx] = usecsperbyte;
689 p->avg += p->usecsperbyte[p->idx];
693 p->idx = (p->idx + 1) % AVG_MSGS;
698 * Get the previous hop in a connection
700 * @param c Connection.
702 * @return Previous peer in the connection.
704 static struct CadetPeer *
705 get_prev_hop (const struct CadetConnection *c)
709 LOG (GNUNET_ERROR_TYPE_DEBUG, " get prev hop %s [%u/%u]\n",
710 GCC_2s (c), c->own_pos, c->path->length);
711 if (0 == c->own_pos || c->path->length < 2)
712 id = c->path->peers[0];
714 id = c->path->peers[c->own_pos - 1];
716 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
717 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
719 return GCP_get_short (id);
724 * Get the next hop in a connection
726 * @param c Connection.
728 * @return Next peer in the connection.
730 static struct CadetPeer *
731 get_next_hop (const struct CadetConnection *c)
735 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
736 GCC_2s (c), c->own_pos, c->path->length);
737 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
738 id = c->path->peers[c->path->length - 1];
740 id = c->path->peers[c->own_pos + 1];
742 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
743 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
745 return GCP_get_short (id);
750 * Get the hop in a connection.
752 * @param c Connection.
753 * @param fwd Next hop?
755 * @return Next peer in the connection.
757 static struct CadetPeer *
758 get_hop (struct CadetConnection *c, int fwd)
761 return get_next_hop (c);
762 return get_prev_hop (c);
767 * Is traffic coming from this sender 'FWD' traffic?
769 * @param c Connection to check.
770 * @param sender Peer identity of neighbor.
772 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
773 * the traffic is 'FWD'.
774 * #GNUNET_NO for BCK.
775 * #GNUNET_SYSERR for errors.
778 is_fwd (const struct CadetConnection *c,
779 const struct GNUNET_PeerIdentity *sender)
783 id = GNUNET_PEER_search (sender);
784 if (GCP_get_short_id (get_prev_hop (c)) == id)
787 if (GCP_get_short_id (get_next_hop (c)) == id)
791 return GNUNET_SYSERR;
796 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
797 * or a first CONNECTION_ACK directed to us.
799 * @param connection Connection to confirm.
800 * @param fwd Should we send it FWD? (root->dest)
801 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
804 send_connection_ack (struct CadetConnection *connection, int fwd)
806 struct CadetTunnel *t;
809 LOG (GNUNET_ERROR_TYPE_INFO, "===> {%14s ACK} on connection %s\n",
810 GC_f2s (!fwd), GCC_2s (connection));
811 GCP_queue_add (get_hop (connection, fwd), NULL,
812 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0, 0,
813 sizeof (struct GNUNET_CADET_ConnectionACK),
814 connection, fwd, &conn_message_sent, NULL);
815 connection->pending_messages++;
816 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
817 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
818 if (CADET_CONNECTION_READY != connection->state)
819 connection_change_state (connection, CADET_CONNECTION_SENT);
824 * Send a notification that a connection is broken.
826 * @param c Connection that is broken.
827 * @param id1 Peer that has disconnected.
828 * @param id2 Peer that has disconnected.
829 * @param fwd Direction towards which to send it.
832 send_broken (struct CadetConnection *c,
833 const struct GNUNET_PeerIdentity *id1,
834 const struct GNUNET_PeerIdentity *id2,
837 struct GNUNET_CADET_ConnectionBroken msg;
839 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
840 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
844 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c, fwd,
845 GNUNET_YES, NULL, NULL));
850 * Send a notification that a connection is broken, when a connection
851 * isn't even known to the local peer.
853 * @param connection_id Connection ID.
854 * @param id1 Peer that has disconnected, probably local peer.
855 * @param id2 Peer that has disconnected can be NULL if unknown.
856 * @param peer Peer to notify (neighbor who sent the connection).
859 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
860 const struct GNUNET_PeerIdentity *id1,
861 const struct GNUNET_PeerIdentity *id2,
862 const struct GNUNET_PeerIdentity *peer_id)
864 struct GNUNET_CADET_ConnectionBroken *msg;
865 struct CadetPeer *neighbor;
867 LOG (GNUNET_ERROR_TYPE_INFO, "===> BROKEN on unknown connection %s\n",
868 GNUNET_h2s (GC_h2hc (connection_id)));
870 msg = GNUNET_new (struct GNUNET_CADET_ConnectionBroken);
871 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
872 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
873 msg->cid = *connection_id;
878 memset (&msg->peer2, 0, sizeof (msg->peer2));
879 neighbor = GCP_get (peer_id);
880 GCP_queue_add (neighbor, msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
881 0, 2, sizeof (struct GNUNET_CADET_ConnectionBroken),
882 NULL, GNUNET_SYSERR, /* connection, fwd */
883 NULL, NULL); /* continuation */
888 * Send keepalive packets for a connection.
890 * @param c Connection to keep alive..
891 * @param fwd Is this a FWD keepalive? (owner -> dest).
894 send_connection_keepalive (struct CadetConnection *c, int fwd)
896 struct GNUNET_MessageHeader msg;
897 struct CadetFlowControl *fc;
899 LOG (GNUNET_ERROR_TYPE_INFO,
900 "keepalive %s for connection %s\n",
901 GC_f2s (fwd), GCC_2s (c));
903 fc = fwd ? &c->fwd_fc : &c->bck_fc;
906 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
909 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
911 GNUNET_assert (NULL != c->t);
912 msg.size = htons (sizeof (msg));
913 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
915 GNUNET_assert (NULL ==
916 GCT_send_prebuilt_message (&msg, c->t, c,
917 GNUNET_NO, NULL, NULL));
922 * Send CONNECTION_{CREATE/ACK} packets for a connection.
924 * @param c Connection for which to send the message.
925 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
928 connection_recreate (struct CadetConnection *c, int fwd)
930 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
934 send_connection_ack (c, GNUNET_NO);
939 * Generic connection timer management.
940 * Depending on the role of the peer in the connection will send the
941 * appropriate message (build or keepalive)
943 * @param c Conncetion to maintain.
947 connection_maintain (struct CadetConnection *c, int fwd)
949 if (GNUNET_NO != c->destroy)
952 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
954 /* TODO DHT GET with RO_BART */
959 case CADET_CONNECTION_NEW:
962 case CADET_CONNECTION_SENT:
963 connection_recreate (c, fwd);
965 case CADET_CONNECTION_READY:
966 send_connection_keepalive (c, fwd);
976 * Keep the connection alive.
978 * @param c Connection to keep alive.
979 * @param fwd Direction.
980 * @param shutdown Are we shutting down? (Don't send traffic)
981 * Non-zero value for true, not necessarily GNUNET_YES.
984 connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
986 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s keepalive for %s\n",
987 GC_f2s (fwd), GCC_2s (c));
990 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
992 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
994 if (GNUNET_NO != shutdown)
997 connection_maintain (c, fwd);
999 /* Next execution will be scheduled by message_sent */
1004 * Keep the connection alive in the FWD direction.
1006 * @param cls Closure (connection to keepalive).
1007 * @param tc TaskContext.
1010 connection_fwd_keepalive (void *cls,
1011 const struct GNUNET_SCHEDULER_TaskContext *tc)
1013 connection_keepalive ((struct CadetConnection *) cls,
1015 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1020 * Keep the connection alive in the BCK direction.
1022 * @param cls Closure (connection to keepalive).
1023 * @param tc TaskContext.
1026 connection_bck_keepalive (void *cls,
1027 const struct GNUNET_SCHEDULER_TaskContext *tc)
1029 connection_keepalive ((struct CadetConnection *) cls,
1031 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1036 * Schedule next keepalive task, taking in consideration
1037 * the connection state and number of retries.
1039 * If the peer is not the origin, do nothing.
1041 * @param c Connection for which to schedule the next keepalive.
1042 * @param fwd Direction for the next keepalive.
1045 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1047 struct GNUNET_TIME_Relative delay;
1048 GNUNET_SCHEDULER_TaskIdentifier *task_id;
1049 GNUNET_SCHEDULER_Task keepalive_task;
1051 if (GNUNET_NO == GCC_is_origin (c, fwd))
1054 /* Calculate delay to use, depending on the state of the connection */
1055 if (CADET_CONNECTION_READY == c->state)
1057 delay = refresh_connection_time;
1061 if (1 > c->create_retry)
1062 c->create_retry = 1;
1063 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1065 if (c->create_retry < 64)
1066 c->create_retry *= 2;
1069 /* Select direction-dependent parameters */
1070 if (GNUNET_YES == fwd)
1072 task_id = &c->fwd_maintenance_task;
1073 keepalive_task = &connection_fwd_keepalive;
1077 task_id = &c->bck_maintenance_task;
1078 keepalive_task = &connection_bck_keepalive;
1081 /* Check that no one scheduled it before us */
1082 if (GNUNET_SCHEDULER_NO_TASK != *task_id)
1084 /* No need for a _break. It can happen for instance when sending a SYNACK
1085 * for a duplicate SYN: the first SYNACK scheduled the task. */
1086 GNUNET_SCHEDULER_cancel (*task_id);
1089 /* Schedule the task */
1090 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1091 LOG (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1092 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1097 * @brief Re-initiate traffic on this connection if necessary.
1099 * Check if there is traffic queued towards this peer
1100 * and the core transmit handle is NULL (traffic was stalled).
1101 * If so, call core tmt rdy.
1103 * @param c Connection on which initiate traffic.
1104 * @param fwd Is this about fwd traffic?
1107 connection_unlock_queue (struct CadetConnection *c, int fwd)
1109 struct CadetPeer *peer;
1111 LOG (GNUNET_ERROR_TYPE_DEBUG,
1112 "connection_unlock_queue %s on %s\n",
1113 GC_f2s (fwd), GCC_2s (c));
1115 if (GCC_is_terminal (c, fwd))
1117 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
1121 peer = get_hop (c, fwd);
1122 GCP_queue_unlock (peer, c);
1127 * Cancel all transmissions that belong to a certain connection.
1129 * If the connection is scheduled for destruction and no more messages are left,
1130 * the connection will be destroyed by the continuation call.
1132 * @param c Connection which to cancel. Might be destroyed during this call.
1133 * @param fwd Cancel fwd traffic?
1136 connection_cancel_queues (struct CadetConnection *c, int fwd)
1138 struct CadetFlowControl *fc;
1139 struct CadetPeer *peer;
1141 LOG (GNUNET_ERROR_TYPE_DEBUG,
1142 " *** Cancel %s queues for connection %s\n",
1143 GC_f2s (fwd), GCC_2s (c));
1150 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1151 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1153 GNUNET_SCHEDULER_cancel (fc->poll_task);
1154 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1155 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1157 peer = get_hop (c, fwd);
1158 GCP_queue_cancel (peer, c);
1163 * Function called if a connection has been stalled for a while,
1164 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1166 * @param cls Closure (poll ctx).
1167 * @param tc TaskContext.
1170 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1174 * Callback called when a queued POLL message is sent.
1176 * @param cls Closure (FC).
1177 * @param c Connection this message was on.
1178 * @param q Queue handler this call invalidates.
1179 * @param type Type of message sent.
1180 * @param fwd Was this a FWD going message?
1181 * @param size Size of the message.
1184 poll_sent (void *cls,
1185 struct CadetConnection *c,
1186 struct CadetConnectionQueue *q,
1187 uint16_t type, int fwd, size_t size)
1189 struct CadetFlowControl *fc = cls;
1191 if (2 == c->destroy)
1193 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1196 LOG (GNUNET_ERROR_TYPE_DEBUG,
1197 " *** POLL sent for , scheduling new one!\n");
1198 fc->poll_msg = NULL;
1199 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1200 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1201 &connection_poll, fc);
1202 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1207 * Function called if a connection has been stalled for a while,
1208 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1210 * @param cls Closure (poll ctx).
1211 * @param tc TaskContext.
1214 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1216 struct CadetFlowControl *fc = cls;
1217 struct GNUNET_CADET_Poll msg;
1218 struct CadetConnection *c;
1220 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1221 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1227 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling connection %s %s\n",
1228 GCC_2s (c), fc == &c->fwd_fc ? "FWD" : "BCK");
1230 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1231 msg.header.size = htons (sizeof (msg));
1232 msg.pid = htonl (fc->last_pid_sent);
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1235 GCC_send_prebuilt_message (&msg.header, 0, fc->last_pid_sent, c,
1236 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1237 GNUNET_assert (NULL != fc->poll_msg);
1242 * Timeout function due to lack of keepalive/traffic from the owner.
1243 * Destroys connection if called.
1245 * @param cls Closure (connection to destroy).
1246 * @param tc TaskContext.
1249 connection_fwd_timeout (void *cls,
1250 const struct GNUNET_SCHEDULER_TaskContext *tc)
1252 struct CadetConnection *c = cls;
1254 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1255 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1258 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s FWD timed out. Destroying.\n",
1260 if (GCC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1271 * Timeout function due to lack of keepalive/traffic from the destination.
1272 * Destroys connection if called.
1274 * @param cls Closure (connection to destroy).
1275 * @param tc TaskContext
1278 connection_bck_timeout (void *cls,
1279 const struct GNUNET_SCHEDULER_TaskContext *tc)
1281 struct CadetConnection *c = cls;
1283 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1284 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1287 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s BCK timed out. Destroying.\n",
1290 if (GCC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1301 * Resets the connection timeout task, some other message has done the
1303 * - For the first peer on the direction this means to send
1304 * a keepalive or a path confirmation message (either create or ACK).
1305 * - For all other peers, this means to destroy the connection,
1306 * due to lack of activity.
1307 * Starts the timeout if no timeout was running (connection just created).
1309 * @param c Connection whose timeout to reset.
1310 * @param fwd Is this forward?
1312 * TODO use heap to improve efficiency of scheduler.
1315 connection_reset_timeout (struct CadetConnection *c, int fwd)
1317 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1319 if (GCC_is_origin (c, fwd)) /* Startpoint */
1321 schedule_next_keepalive (c, fwd);
1323 else /* Relay, endpoint. */
1325 struct GNUNET_TIME_Relative delay;
1326 GNUNET_SCHEDULER_TaskIdentifier *ti;
1327 GNUNET_SCHEDULER_Task f;
1329 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1331 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1332 GNUNET_SCHEDULER_cancel (*ti);
1333 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1334 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1335 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1341 * Add the connection to the list of both neighbors.
1343 * @param c Connection.
1345 * @return #GNUNET_OK if everything went fine
1346 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1349 register_neighbors (struct CadetConnection *c)
1351 struct CadetPeer *next_peer;
1352 struct CadetPeer *prev_peer;
1354 next_peer = get_next_hop (c);
1355 prev_peer = get_prev_hop (c);
1357 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1359 path_debug (c->path);
1360 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1361 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1362 GCC_2s (c), next_peer);
1363 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GCP_2s (next_peer));
1364 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1365 GCC_2s (c), prev_peer);
1366 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GCP_2s (prev_peer));
1368 if (GNUNET_NO == GCP_is_neighbor (next_peer)
1369 || GNUNET_NO == GCP_is_neighbor (prev_peer))
1371 if (GCC_is_origin (c, GNUNET_YES))
1372 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1373 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1375 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1376 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1377 GCP_2s (prev_peer), GCP_is_neighbor (prev_peer));
1378 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1379 GCP_2s (next_peer), GCP_is_neighbor (next_peer));
1380 return GNUNET_SYSERR;
1383 GCP_add_connection (next_peer, c);
1384 GCP_add_connection (prev_peer, c);
1391 * Remove the connection from the list of both neighbors.
1393 * @param c Connection.
1396 unregister_neighbors (struct CadetConnection *c)
1398 struct CadetPeer *peer;
1400 peer = get_next_hop (c);
1401 if (GNUNET_OK != GCP_remove_connection (peer, c))
1403 GNUNET_assert (CADET_CONNECTION_NEW == c->state
1404 || CADET_CONNECTION_DESTROYED == c->state);
1405 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1406 if (NULL != c->t) GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1409 peer = get_prev_hop (c);
1410 if (GNUNET_OK != GCP_remove_connection (peer, c))
1412 GNUNET_assert (CADET_CONNECTION_NEW == c->state
1413 || CADET_CONNECTION_DESTROYED == c->state);
1414 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1415 if (NULL != c->t) GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1421 * Bind the connection to the peer and the tunnel to that peer.
1423 * If the peer has no tunnel, create one. Update tunnel and connection
1424 * data structres to reflect new status.
1426 * @param c Connection.
1430 add_to_peer (struct CadetConnection *c, struct CadetPeer *peer)
1432 GCP_add_tunnel (peer);
1433 c->t = GCP_get_tunnel (peer);
1434 GCT_add_connection (c->t, c);
1439 * Builds a path from a PeerIdentity array.
1441 * @param peers PeerIdentity array.
1442 * @param size Size of the @c peers array.
1443 * @param own_pos Output parameter: own position in the path.
1445 * @return Fixed and shortened path.
1447 static struct CadetPeerPath *
1448 build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
1450 unsigned int *own_pos)
1452 struct CadetPeerPath *path;
1453 GNUNET_PEER_Id shortid;
1456 unsigned int offset;
1459 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
1460 path = path_new (size);
1463 for (i = 0; i < size; i++)
1465 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
1466 i, GNUNET_i2s (&peers[i]));
1467 shortid = GNUNET_PEER_intern (&peers[i]);
1469 /* Check for loops / duplicates */
1470 for (j = 0; j < i - offset; j++)
1472 if (path->peers[j] == shortid)
1474 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
1476 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now %u\n", offset);
1477 GNUNET_PEER_change_rc (shortid, -1);
1480 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
1481 path->peers[i - offset] = shortid;
1482 if (path->peers[i - offset] == myid)
1483 *own_pos = i - offset;
1485 path->length -= offset;
1487 if (path->peers[*own_pos] != myid)
1489 /* create path: self not found in path through self */
1490 GNUNET_break_op (0);
1491 path_destroy (path);
1500 * Log receipt of message on stderr (INFO level).
1502 * @param message Message received.
1503 * @param peer Peer who sent the message.
1504 * @param hash Connection ID.
1507 log_message (const struct GNUNET_MessageHeader *message,
1508 const struct GNUNET_PeerIdentity *peer,
1509 const struct GNUNET_CADET_Hash *hash)
1511 LOG (GNUNET_ERROR_TYPE_INFO, "<-- %s on connection %s from %s\n",
1512 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (hash)),
1516 /******************************************************************************/
1517 /******************************** API ***********************************/
1518 /******************************************************************************/
1521 * Core handler for connection creation.
1523 * @param cls Closure (unused).
1524 * @param peer Sender (neighbor).
1525 * @param message Message.
1527 * @return GNUNET_OK to keep the connection open,
1528 * GNUNET_SYSERR to close it (signal serious error)
1531 GCC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1532 const struct GNUNET_MessageHeader *message)
1534 struct GNUNET_CADET_ConnectionCreate *msg;
1535 struct GNUNET_PeerIdentity *id;
1536 struct GNUNET_CADET_Hash *cid;
1537 struct CadetPeerPath *path;
1538 struct CadetPeer *dest_peer;
1539 struct CadetPeer *orig_peer;
1540 struct CadetConnection *c;
1541 unsigned int own_pos;
1545 size = ntohs (message->size);
1546 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1548 GNUNET_break_op (0);
1552 /* Calculate hops */
1553 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1554 if (size % sizeof (struct GNUNET_PeerIdentity))
1556 GNUNET_break_op (0);
1559 size /= sizeof (struct GNUNET_PeerIdentity);
1562 GNUNET_break_op (0);
1565 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1567 /* Get parameters */
1568 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
1570 log_message (message, peer, cid);
1571 id = (struct GNUNET_PeerIdentity *) &msg[1];
1572 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1574 /* Create connection */
1575 c = connection_get (cid);
1578 path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1584 GNUNET_break_op (0);
1585 path_destroy (path);
1588 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1589 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1590 c = GCC_new (cid, NULL, path_duplicate (path), own_pos);
1593 if (path->length - 1 == own_pos)
1595 /* If we are destination, why did the creation fail? */
1599 send_broken_unknown (cid, &my_full_id,
1600 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1602 path_destroy (path);
1605 GCP_add_path_to_all (path, GNUNET_NO);
1606 connection_reset_timeout (c, GNUNET_YES);
1610 path = path_duplicate (c->path);
1612 if (CADET_CONNECTION_NEW == c->state)
1613 connection_change_state (c, CADET_CONNECTION_SENT);
1615 /* Remember peers */
1616 dest_peer = GCP_get (&id[size - 1]);
1617 orig_peer = GCP_get (&id[0]);
1619 /* Is it a connection to us? */
1620 if (c->own_pos == path->length - 1)
1622 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1623 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1625 add_to_peer (c, orig_peer);
1626 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
1627 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
1629 send_connection_ack (c, GNUNET_NO);
1630 if (CADET_CONNECTION_SENT == c->state)
1631 connection_change_state (c, CADET_CONNECTION_ACK);
1635 /* It's for somebody else! Retransmit. */
1636 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1637 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1638 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1639 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
1640 GNUNET_YES, GNUNET_YES,
1643 path_destroy (path);
1649 * Core handler for path confirmations.
1651 * @param cls closure
1652 * @param message message
1653 * @param peer peer identity this notification is about
1655 * @return GNUNET_OK to keep the connection open,
1656 * GNUNET_SYSERR to close it (signal serious error)
1659 GCC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1660 const struct GNUNET_MessageHeader *message)
1662 struct GNUNET_CADET_ConnectionACK *msg;
1663 struct CadetConnection *c;
1664 struct CadetPeerPath *p;
1665 struct CadetPeer *pi;
1666 enum CadetConnectionState oldstate;
1669 msg = (struct GNUNET_CADET_ConnectionACK *) message;
1670 log_message (message, peer, &msg->cid);
1671 c = connection_get (&msg->cid);
1674 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1676 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1677 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
1681 if (GNUNET_NO != c->destroy)
1683 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1687 oldstate = c->state;
1688 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1689 pi = GCP_get (peer);
1690 if (get_next_hop (c) == pi)
1692 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1694 if (CADET_CONNECTION_SENT == oldstate)
1695 connection_change_state (c, CADET_CONNECTION_ACK);
1697 else if (get_prev_hop (c) == pi)
1699 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
1701 connection_change_state (c, CADET_CONNECTION_READY);
1705 GNUNET_break_op (0);
1709 connection_reset_timeout (c, fwd);
1711 /* Add path to peers? */
1715 GCP_add_path_to_all (p, GNUNET_YES);
1722 /* Message for us as creator? */
1723 if (GCC_is_origin (c, GNUNET_YES))
1725 if (GNUNET_NO != fwd)
1727 GNUNET_break_op (0);
1730 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1732 /* If just created, cancel the short timeout and start a long one */
1733 if (CADET_CONNECTION_SENT == oldstate)
1734 connection_reset_timeout (c, GNUNET_YES);
1736 /* Change connection state */
1737 connection_change_state (c, CADET_CONNECTION_READY);
1738 send_connection_ack (c, GNUNET_YES);
1740 /* Change tunnel state, trigger KX */
1741 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
1742 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
1747 /* Message for us as destination? */
1748 if (GCC_is_terminal (c, GNUNET_YES))
1750 if (GNUNET_YES != fwd)
1752 GNUNET_break_op (0);
1755 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1757 /* If just created, cancel the short timeout and start a long one */
1758 if (CADET_CONNECTION_ACK == oldstate)
1759 connection_reset_timeout (c, GNUNET_NO);
1761 /* Change tunnel state */
1762 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
1763 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
1768 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1769 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
1770 GNUNET_YES, NULL, NULL));
1776 * Core handler for notifications of broken connections.
1778 * @param cls Closure (unused).
1779 * @param id Peer identity of sending neighbor.
1780 * @param message Message.
1782 * @return GNUNET_OK to keep the connection open,
1783 * GNUNET_SYSERR to close it (signal serious error)
1786 GCC_handle_broken (void* cls,
1787 const struct GNUNET_PeerIdentity* id,
1788 const struct GNUNET_MessageHeader* message)
1790 struct GNUNET_CADET_ConnectionBroken *msg;
1791 struct CadetConnection *c;
1792 struct CadetTunnel *t;
1795 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
1796 log_message (message, id, &msg->cid);
1797 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1798 GNUNET_i2s (&msg->peer1));
1799 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1800 GNUNET_i2s (&msg->peer2));
1801 c = connection_get (&msg->cid);
1804 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
1809 fwd = is_fwd (c, id);
1810 if (GCC_is_terminal (c, fwd))
1812 struct GNUNET_MessageHeader *out_msg;
1813 struct CadetPeer *neighbor;
1814 struct CadetPeer *endpoint;
1819 /* A terminal connection should not have 't' set to NULL. */
1823 neighbor = get_hop (c, !fwd);
1824 endpoint = GCP_get_short (c->path->peers[c->path->length - 1]);
1825 path_invalidate (c->path);
1826 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
1827 c->state = CADET_CONNECTION_DESTROYED;
1828 GCT_remove_connection (t, c);
1830 pending_msgs = c->pending_messages;
1832 /* GCP_connection_pop will destroy the connection when the last message
1833 * is popped! Do not use 'c' after the call. */
1834 while (NULL != (out_msg = GCP_connection_pop (neighbor, c)))
1836 GNUNET_assert (NULL ==
1837 GCT_send_prebuilt_message (out_msg, t, NULL, GNUNET_YES,
1842 /* All pending messages should have been popped and the connection
1843 * destroyed. If not, destroy the connection anyway! */
1844 if (0 < pending_msgs)
1850 GNUNET_break (0 == pending_msgs); /* Counter error! */
1854 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
1855 GNUNET_YES, NULL, NULL));
1856 c->destroy = GNUNET_YES;
1857 connection_cancel_queues (c, !fwd);
1865 * Core handler for tunnel destruction
1867 * @param cls Closure (unused).
1868 * @param peer Peer identity of sending neighbor.
1869 * @param message Message.
1871 * @return GNUNET_OK to keep the connection open,
1872 * GNUNET_SYSERR to close it (signal serious error)
1875 GCC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1876 const struct GNUNET_MessageHeader *message)
1878 struct GNUNET_CADET_ConnectionDestroy *msg;
1879 struct CadetConnection *c;
1882 msg = (struct GNUNET_CADET_ConnectionDestroy *) message;
1883 log_message (message, peer, &msg->cid);
1884 c = connection_get (&msg->cid);
1887 /* Probably already got the message from another path,
1888 * destroyed the tunnel and retransmitted to children.
1891 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1893 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
1896 fwd = is_fwd (c, peer);
1897 if (GNUNET_SYSERR == fwd)
1899 GNUNET_break_op (0); /* FIXME */
1902 if (GNUNET_NO == GCC_is_terminal (c, fwd))
1903 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
1904 GNUNET_YES, NULL, NULL));
1905 else if (0 == c->pending_messages)
1907 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
1911 c->destroy = GNUNET_YES;
1912 c->state = CADET_CONNECTION_DESTROYED;
1915 GCT_remove_connection (c->t, c);
1923 * Generic handler for cadet network encrypted traffic.
1925 * @param peer Peer identity this notification is about.
1926 * @param msg Encrypted message.
1928 * @return GNUNET_OK to keep the connection open,
1929 * GNUNET_SYSERR to close it (signal serious error)
1932 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
1933 const struct GNUNET_CADET_Encrypted *msg)
1935 struct CadetConnection *c;
1936 struct CadetPeer *neighbor;
1937 struct CadetFlowControl *fc;
1938 GNUNET_PEER_Id peer_id;
1944 log_message (&msg->header, peer, &msg->cid);
1947 size = ntohs (msg->header.size);
1949 sizeof (struct GNUNET_CADET_Encrypted) +
1950 sizeof (struct GNUNET_MessageHeader))
1952 GNUNET_break_op (0);
1956 /* Check connection */
1957 c = connection_get (&msg->cid);
1960 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1961 LOG (GNUNET_ERROR_TYPE_DEBUG, "enc on unknown connection %s\n",
1962 GNUNET_h2s (GC_h2hc (&msg->cid)));
1963 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
1967 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s\n", GCC_2s (c));
1969 /* Check if origin is as expected */
1970 neighbor = get_prev_hop (c);
1971 peer_id = GNUNET_PEER_search (peer);
1972 if (peer_id == GCP_get_short_id (neighbor))
1978 neighbor = get_next_hop (c);
1979 if (peer_id == GCP_get_short_id (neighbor))
1985 /* Unexpected peer sending traffic on a connection. */
1986 GNUNET_break_op (0);
1992 fc = fwd ? &c->bck_fc : &c->fwd_fc;
1993 pid = ntohl (msg->pid);
1994 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u+)\n",
1995 pid, fc->last_pid_recv + 1);
1996 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
1998 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1999 GNUNET_break_op (0);
2000 LOG (GNUNET_ERROR_TYPE_WARNING,
2001 "Received PID %u, (prev %u), ACK %u\n",
2002 pid, fc->last_pid_recv, fc->last_ack_sent);
2005 if (GNUNET_NO == GC_is_pid_bigger (pid, fc->last_pid_recv))
2007 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
2008 LOG (GNUNET_ERROR_TYPE_DEBUG,
2009 " PID %u not expected (%u+), dropping!\n",
2010 pid, fc->last_pid_recv + 1);
2013 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2014 connection_change_state (c, CADET_CONNECTION_READY);
2015 connection_reset_timeout (c, fwd);
2016 fc->last_pid_recv = pid;
2018 /* Is this message for us? */
2019 if (GCC_is_terminal (c, fwd))
2021 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2022 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2026 GNUNET_break (GNUNET_NO != c->destroy);
2029 fc->last_pid_recv = pid;
2030 GCT_handle_encrypted (c->t, msg);
2031 GCC_send_ack (c, fwd, GNUNET_NO);
2035 /* Message not for us: forward to next hop */
2036 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2037 ttl = ntohl (msg->ttl);
2038 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2041 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2042 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2043 GCC_send_ack (c, fwd, GNUNET_NO);
2047 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2048 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2049 GNUNET_NO, NULL, NULL));
2055 * Generic handler for cadet network encrypted traffic.
2057 * @param peer Peer identity this notification is about.
2058 * @param msg Encrypted message.
2060 * @return GNUNET_OK to keep the connection open,
2061 * GNUNET_SYSERR to close it (signal serious error)
2064 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2065 const struct GNUNET_CADET_KX *msg)
2067 struct CadetConnection *c;
2068 struct CadetPeer *neighbor;
2069 GNUNET_PEER_Id peer_id;
2073 log_message (&msg->header, peer, &msg->cid);
2076 size = ntohs (msg->header.size);
2078 sizeof (struct GNUNET_CADET_KX) +
2079 sizeof (struct GNUNET_MessageHeader))
2081 GNUNET_break_op (0);
2085 /* Check connection */
2086 c = connection_get (&msg->cid);
2089 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
2090 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx on unknown connection %s\n",
2091 GNUNET_h2s (GC_h2hc (&msg->cid)));
2092 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2095 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GCC_2s (c));
2097 /* Check if origin is as expected */
2098 neighbor = get_prev_hop (c);
2099 peer_id = GNUNET_PEER_search (peer);
2100 if (peer_id == GCP_get_short_id (neighbor))
2106 neighbor = get_next_hop (c);
2107 if (peer_id == GCP_get_short_id (neighbor))
2113 /* Unexpected peer sending traffic on a connection. */
2114 GNUNET_break_op (0);
2119 /* Count as connection confirmation. */
2120 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2122 connection_change_state (c, CADET_CONNECTION_READY);
2125 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2126 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2129 connection_reset_timeout (c, fwd);
2131 /* Is this message for us? */
2132 if (GCC_is_terminal (c, fwd))
2134 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2135 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2141 GCT_handle_kx (c->t, &msg[1].header);
2145 /* Message not for us: forward to next hop */
2146 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2147 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2148 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2149 GNUNET_NO, NULL, NULL));
2156 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2158 * @param cls Closure (unused).
2159 * @param message Message received.
2160 * @param peer Peer who sent the message.
2162 * @return GNUNET_OK to keep the connection open,
2163 * GNUNET_SYSERR to close it (signal serious error)
2166 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2167 const struct GNUNET_MessageHeader *message)
2169 return handle_cadet_encrypted (peer,
2170 (struct GNUNET_CADET_Encrypted *)message);
2175 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2177 * @param cls Closure (unused).
2178 * @param message Message received.
2179 * @param peer Peer who sent the message.
2181 * @return GNUNET_OK to keep the connection open,
2182 * GNUNET_SYSERR to close it (signal serious error)
2185 GCC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2186 const struct GNUNET_MessageHeader *message)
2188 return handle_cadet_kx (peer,
2189 (struct GNUNET_CADET_KX *) message);
2194 * Core handler for cadet network traffic point-to-point acks.
2196 * @param cls closure
2197 * @param message message
2198 * @param peer peer identity this notification is about
2200 * @return GNUNET_OK to keep the connection open,
2201 * GNUNET_SYSERR to close it (signal serious error)
2204 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2205 const struct GNUNET_MessageHeader *message)
2207 struct GNUNET_CADET_ACK *msg;
2208 struct CadetConnection *c;
2209 struct CadetFlowControl *fc;
2214 msg = (struct GNUNET_CADET_ACK *) message;
2215 log_message (message, peer, &msg->cid);
2216 c = connection_get (&msg->cid);
2219 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2221 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2225 /* Is this a forward or backward ACK? */
2226 id = GNUNET_PEER_search (peer);
2227 if (GCP_get_short_id (get_next_hop (c)) == id)
2229 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2233 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2235 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2241 GNUNET_break_op (0);
2245 ack = ntohl (msg->ack);
2246 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2247 ack, fc->last_ack_recv);
2248 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2249 fc->last_ack_recv = ack;
2251 /* Cancel polling if the ACK is big enough. */
2252 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2253 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2255 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2256 GNUNET_SCHEDULER_cancel (fc->poll_task);
2257 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2258 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2261 connection_unlock_queue (c, fwd);
2268 * Core handler for cadet network traffic point-to-point ack polls.
2270 * @param cls closure
2271 * @param message message
2272 * @param peer peer identity this notification is about
2274 * @return GNUNET_OK to keep the connection open,
2275 * GNUNET_SYSERR to close it (signal serious error)
2278 GCC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2279 const struct GNUNET_MessageHeader *message)
2281 struct GNUNET_CADET_Poll *msg;
2282 struct CadetConnection *c;
2283 struct CadetFlowControl *fc;
2288 msg = (struct GNUNET_CADET_Poll *) message;
2289 log_message (message, peer, &msg->cid);
2290 c = connection_get (&msg->cid);
2293 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2295 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL message on unknown connection %s!\n",
2296 GNUNET_h2s (GC_h2hc (&msg->cid)));
2297 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2301 /* Is this a forward or backward ACK?
2302 * Note: a poll should never be needed in a loopback case,
2303 * since there is no possiblility of packet loss there, so
2304 * this way of discerining FWD/BCK should not be a problem.
2306 id = GNUNET_PEER_search (peer);
2307 if (GCP_get_short_id (get_next_hop (c)) == id)
2309 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2312 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2314 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2319 GNUNET_break_op (0);
2323 pid = ntohl (msg->pid);
2324 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2325 fc->last_pid_recv = pid;
2326 fwd = fc == &c->bck_fc;
2327 GCC_send_ack (c, fwd, GNUNET_YES);
2334 * Send an ACK on the appropriate connection/channel, depending on
2335 * the direction and the position of the peer.
2337 * @param c Which connection to send the hop-by-hop ACK.
2338 * @param fwd Is this a fwd ACK? (will go dest->root).
2339 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2342 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2344 unsigned int buffer;
2346 LOG (GNUNET_ERROR_TYPE_DEBUG,
2347 "GMC send %s ACK on %s\n",
2348 GC_f2s (fwd), GCC_2s (c));
2356 if (GNUNET_NO != c->destroy)
2358 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2362 /* Get available buffer space */
2363 if (GCC_is_terminal (c, fwd))
2365 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2366 buffer = GCT_get_channels_buffer (c->t);
2370 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2371 buffer = GCC_get_buffer (c, fwd);
2373 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2374 if (0 == buffer && GNUNET_NO == force)
2377 /* Send available buffer space */
2378 if (GCC_is_origin (c, fwd))
2380 GNUNET_assert (NULL != c->t);
2381 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2382 GCT_unchoke_channels (c->t);
2386 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2387 send_ack (c, buffer, fwd, force);
2393 * Initialize the connections subsystem
2395 * @param c Configuration handle.
2398 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2400 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2402 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2405 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2406 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2407 GNUNET_SCHEDULER_shutdown ();
2412 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2415 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2416 "CADET", "MAX_CONNECTIONS", "MISSING");
2417 GNUNET_SCHEDULER_shutdown ();
2422 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2423 &refresh_connection_time))
2425 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2426 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2427 GNUNET_SCHEDULER_shutdown ();
2430 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2431 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2436 * Destroy each connection on shutdown.
2438 * @param cls Closure (unused).
2439 * @param key Current key code (CID, unused).
2440 * @param value Value in the hash map (connection)
2442 * @return #GNUNET_YES, because we should continue to iterate,
2445 shutdown_iterator (void *cls,
2446 const struct GNUNET_HashCode *key,
2449 struct CadetConnection *c = value;
2457 * Shut down the connections subsystem.
2462 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2463 GNUNET_CONTAINER_multihashmap_destroy (connections);
2468 struct CadetConnection *
2469 GCC_new (const struct GNUNET_CADET_Hash *cid,
2470 struct CadetTunnel *t,
2471 struct CadetPeerPath *p,
2472 unsigned int own_pos)
2474 struct CadetConnection *c;
2476 c = GNUNET_new (struct CadetConnection);
2478 GNUNET_assert (GNUNET_OK ==
2479 GNUNET_CONTAINER_multihashmap_put (connections,
2481 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2482 fc_init (&c->fwd_fc);
2483 fc_init (&c->bck_fc);
2488 GNUNET_assert (own_pos <= p->length - 1);
2489 c->own_pos = own_pos;
2493 if (GNUNET_OK != register_neighbors (c))
2497 path_invalidate (c->path);
2510 GCC_destroy (struct CadetConnection *c)
2518 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2519 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2522 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GCC_2s (c));
2523 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2524 &c->fwd_fc, &c->bck_fc);
2525 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2526 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2528 /* Cancel all traffic */
2529 if (NULL != c->path)
2531 connection_cancel_queues (c, GNUNET_YES);
2532 connection_cancel_queues (c, GNUNET_NO);
2533 unregister_neighbors (c);
2536 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2537 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2539 /* Cancel maintainance task (keepalive/timeout) */
2540 if (NULL != c->fwd_fc.poll_msg)
2542 GCC_cancel (c->fwd_fc.poll_msg);
2543 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2545 if (NULL != c->bck_fc.poll_msg)
2547 GCC_cancel (c->bck_fc.poll_msg);
2548 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2551 /* Delete from tunnel */
2553 GCT_remove_connection (c->t, c);
2555 if (GNUNET_NO == GCC_is_origin (c, GNUNET_YES) && NULL != c->path)
2556 path_destroy (c->path);
2557 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2558 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2559 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2560 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2561 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2563 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2564 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2566 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2568 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2569 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2572 GNUNET_break (GNUNET_YES ==
2573 GNUNET_CONTAINER_multihashmap_remove (connections,
2576 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2581 * Get the connection ID.
2583 * @param c Connection to get the ID from.
2585 * @return ID of the connection.
2587 const struct GNUNET_CADET_Hash *
2588 GCC_get_id (const struct CadetConnection *c)
2595 * Get the connection ID.
2597 * @param c Connection to get the ID from.
2599 * @return ID of the connection.
2601 const struct GNUNET_HashCode *
2602 GCC_get_h (const struct CadetConnection *c)
2604 return GC_h2hc (&c->id);
2609 * Get the connection path.
2611 * @param c Connection to get the path from.
2613 * @return path used by the connection.
2615 const struct CadetPeerPath *
2616 GCC_get_path (const struct CadetConnection *c)
2618 if (GNUNET_NO == c->destroy)
2625 * Get the connection state.
2627 * @param c Connection to get the state from.
2629 * @return state of the connection.
2631 enum CadetConnectionState
2632 GCC_get_state (const struct CadetConnection *c)
2638 * Get the connection tunnel.
2640 * @param c Connection to get the tunnel from.
2642 * @return tunnel of the connection.
2644 struct CadetTunnel *
2645 GCC_get_tunnel (const struct CadetConnection *c)
2652 * Get free buffer space in a connection.
2654 * @param c Connection.
2655 * @param fwd Is query about FWD traffic?
2657 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2660 GCC_get_buffer (struct CadetConnection *c, int fwd)
2662 struct CadetFlowControl *fc;
2664 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2666 return (fc->queue_max - fc->queue_n);
2670 * Get how many messages have we allowed to send to us from a direction.
2672 * @param c Connection.
2673 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2675 * @return last_ack_sent - last_pid_recv
2678 GCC_get_allowed (struct CadetConnection *c, int fwd)
2680 struct CadetFlowControl *fc;
2682 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2683 if (GC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
2687 return (fc->last_ack_sent - fc->last_pid_recv);
2691 * Get messages queued in a connection.
2693 * @param c Connection.
2694 * @param fwd Is query about FWD traffic?
2696 * @return Number of messages queued.
2699 GCC_get_qn (struct CadetConnection *c, int fwd)
2701 struct CadetFlowControl *fc;
2703 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2710 * Get next PID to use.
2712 * @param c Connection.
2713 * @param fwd Is query about FWD traffic?
2715 * @return Last PID used + 1.
2718 GCC_get_pid (struct CadetConnection *c, int fwd)
2720 struct CadetFlowControl *fc;
2722 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2724 return fc->last_pid_sent + 1;
2729 * Allow the connection to advertise a buffer of the given size.
2731 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2732 * allowing up to last_pid_recv + buffer.
2734 * @param c Connection.
2735 * @param buffer How many more messages the connection can accept.
2736 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2739 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
2741 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2742 GCC_2s (c), buffer, GC_f2s (fwd));
2743 send_ack (c, buffer, fwd, GNUNET_NO);
2748 * Notify other peers on a connection of a broken link. Mark connections
2749 * to destroy after all traffic has been sent.
2751 * @param c Connection on which there has been a disconnection.
2752 * @param peer Peer that disconnected.
2755 GCC_notify_broken (struct CadetConnection *c,
2756 struct CadetPeer *peer)
2760 LOG (GNUNET_ERROR_TYPE_DEBUG,
2761 " notify broken on %s due to %s disconnect\n",
2762 GCC_2s (c), GCP_2s (peer));
2764 fwd = peer == get_prev_hop (c);
2766 if (GNUNET_YES == GCC_is_terminal (c, fwd))
2768 /* Local shutdown, no one to notify about this. */
2772 if (GNUNET_NO == c->destroy)
2773 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
2775 /* Connection will have at least one pending message
2776 * (the one we just scheduled), so no point in checking whether to
2777 * destroy immediately. */
2778 c->destroy = GNUNET_YES;
2779 c->state = CADET_CONNECTION_DESTROYED;
2782 * Cancel all queues, if no message is left, connection will be destroyed.
2784 connection_cancel_queues (c, !fwd);
2791 * Is this peer the first one on the connection?
2793 * @param c Connection.
2794 * @param fwd Is this about fwd traffic?
2796 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2799 GCC_is_origin (struct CadetConnection *c, int fwd)
2801 if (!fwd && c->path->length - 1 == c->own_pos )
2803 if (fwd && 0 == c->own_pos)
2810 * Is this peer the last one on the connection?
2812 * @param c Connection.
2813 * @param fwd Is this about fwd traffic?
2814 * Note that the ROOT is the terminal for BCK traffic!
2816 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2819 GCC_is_terminal (struct CadetConnection *c, int fwd)
2821 return GCC_is_origin (c, !fwd);
2826 * See if we are allowed to send by the next hop in the given direction.
2828 * @param c Connection.
2829 * @param fwd Is this about fwd traffic?
2831 * @return #GNUNET_YES in case it's OK to send.
2834 GCC_is_sendable (struct CadetConnection *c, int fwd)
2836 struct CadetFlowControl *fc;
2838 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking sendability of %s traffic on %s\n",
2839 GC_f2s (fwd), GCC_2s (c));
2845 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2846 LOG (GNUNET_ERROR_TYPE_DEBUG,
2847 " last ack recv: %u, last pid sent: %u\n",
2848 fc->last_ack_recv, fc->last_pid_sent);
2849 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2851 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
2854 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
2860 * Check if this connection is a direct one (never trim a direct connection).
2862 * @param c Connection.
2864 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
2867 GCC_is_direct (struct CadetConnection *c)
2869 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
2873 * Sends an already built message on a connection, properly registering
2874 * all used resources.
2876 * @param message Message to send. Function makes a copy of it.
2877 * If message is not hop-by-hop, decrements TTL of copy.
2878 * @param payload_type Type of payload, in case the message is encrypted.
2879 * @param c Connection on which this message is transmitted.
2880 * @param fwd Is this a fwd message?
2881 * @param force Force the connection to accept the message (buffer overfill).
2882 * @param cont Continuation called once message is sent. Can be NULL.
2883 * @param cont_cls Closure for @c cont.
2885 * @return Handle to cancel the message before it's sent.
2886 * NULL on error or if @c cont is NULL.
2887 * Invalid on @c cont call.
2889 struct CadetConnectionQueue *
2890 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2891 uint16_t payload_type, uint32_t payload_id,
2892 struct CadetConnection *c, int fwd, int force,
2893 GCC_sent cont, void *cont_cls)
2895 struct CadetFlowControl *fc;
2896 struct CadetConnectionQueue *q;
2902 size = ntohs (message->size);
2903 data = GNUNET_malloc (size);
2904 memcpy (data, message, size);
2905 type = ntohs (message->type);
2906 LOG (GNUNET_ERROR_TYPE_INFO, "--> %s (%s %u) on connection %s (%u bytes)\n",
2907 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), size);
2909 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2910 droppable = GNUNET_NO == force;
2913 struct GNUNET_CADET_Encrypted *emsg;
2914 struct GNUNET_CADET_KX *kmsg;
2915 struct GNUNET_CADET_ACK *amsg;
2916 struct GNUNET_CADET_Poll *pmsg;
2917 struct GNUNET_CADET_ConnectionDestroy *dmsg;
2918 struct GNUNET_CADET_ConnectionBroken *bmsg;
2921 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
2922 emsg = (struct GNUNET_CADET_Encrypted *) data;
2923 ttl = ntohl (emsg->ttl);
2926 GNUNET_break_op (0);
2931 emsg->ttl = htonl (ttl - 1);
2932 emsg->pid = htonl (0);
2933 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
2934 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
2935 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
2936 if (GNUNET_YES == droppable)
2942 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
2944 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
2946 GCC_start_poll (c, fwd);
2950 case GNUNET_MESSAGE_TYPE_CADET_KX:
2951 kmsg = (struct GNUNET_CADET_KX *) data;
2955 case GNUNET_MESSAGE_TYPE_CADET_ACK:
2956 amsg = (struct GNUNET_CADET_ACK *) data;
2958 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2959 droppable = GNUNET_NO;
2962 case GNUNET_MESSAGE_TYPE_CADET_POLL:
2963 pmsg = (struct GNUNET_CADET_Poll *) data;
2965 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2966 droppable = GNUNET_NO;
2969 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
2970 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
2974 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
2975 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
2979 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
2982 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
2983 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
2992 if (fc->queue_n > fc->queue_max && droppable)
2994 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
2997 LOG (GNUNET_ERROR_TYPE_DEBUG,
2998 "queue full: %u/%u\n",
2999 fc->queue_n, fc->queue_max);
3000 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3005 return NULL; /* Drop this message */
3008 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u\n", c, c->pending_messages);
3009 c->pending_messages++;
3011 q = GNUNET_new (struct CadetConnectionQueue);
3012 q->forced = !droppable;
3013 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3014 size, c, fwd, &conn_message_sent, q);
3017 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING dropping msg on %s\n", GCC_2s (c));
3023 q->cont_cls = cont_cls;
3024 return (NULL == cont) ? NULL : q;
3029 * Cancel a previously sent message while it's in the queue.
3031 * ONLY can be called before the continuation given to the send function
3032 * is called. Once the continuation is called, the message is no longer in the
3035 * @param q Handle to the queue.
3038 GCC_cancel (struct CadetConnectionQueue *q)
3040 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
3042 /* queue destroy calls message_sent, which calls q->cont and frees q */
3043 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3048 * Sends a CREATE CONNECTION message for a path to a peer.
3049 * Changes the connection and tunnel states if necessary.
3051 * @param connection Connection to create.
3054 GCC_send_create (struct CadetConnection *connection)
3056 enum CadetTunnelCState state;
3059 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3060 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3062 LOG (GNUNET_ERROR_TYPE_INFO, "===> %s on connection %s (%u bytes)\n",
3063 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE),
3064 GCC_2s (connection), size);
3065 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3066 connection, connection->pending_messages);
3067 connection->pending_messages++;
3069 connection->maintenance_q =
3070 GCP_queue_add (get_next_hop (connection), NULL,
3071 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 0,
3072 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3074 state = GCT_get_cstate (connection->t);
3075 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3076 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3077 if (CADET_CONNECTION_NEW == connection->state)
3078 connection_change_state (connection, CADET_CONNECTION_SENT);
3083 * Send a message to all peers in this connection that the connection
3084 * is no longer valid.
3086 * If some peer should not receive the message, it should be zero'ed out
3087 * before calling this function.
3089 * @param c The connection whose peers to notify.
3092 GCC_send_destroy (struct CadetConnection *c)
3094 struct GNUNET_CADET_ConnectionDestroy msg;
3096 if (GNUNET_YES == c->destroy)
3099 msg.header.size = htons (sizeof (msg));
3100 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3102 LOG (GNUNET_ERROR_TYPE_DEBUG,
3103 " sending connection destroy for connection %s\n",
3106 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3107 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3108 GNUNET_YES, GNUNET_YES,
3110 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3111 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3112 GNUNET_NO, GNUNET_YES,
3114 c->destroy = GNUNET_YES;
3115 c->state = CADET_CONNECTION_DESTROYED;
3120 * @brief Start a polling timer for the connection.
3122 * When a neighbor does not accept more traffic on the connection it could be
3123 * caused by a simple congestion or by a lost ACK. Polling enables to check
3124 * for the lastest ACK status for a connection.
3126 * @param c Connection.
3127 * @param fwd Should we poll in the FWD direction?
3130 GCC_start_poll (struct CadetConnection *c, int fwd)
3132 struct CadetFlowControl *fc;
3134 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3135 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
3137 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
3139 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
3140 fc->poll_task, fc->poll_msg);
3143 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
3144 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3151 * @brief Stop polling a connection for ACKs.
3153 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3155 * @param c Connection.
3156 * @param fwd Should we stop the poll in the FWD direction?
3159 GCC_stop_poll (struct CadetConnection *c, int fwd)
3161 struct CadetFlowControl *fc;
3163 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3164 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3166 GNUNET_SCHEDULER_cancel (fc->poll_task);
3167 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3172 * Get a (static) string for a connection.
3174 * @param c Connection.
3177 GCC_2s (const struct CadetConnection *c)
3184 static char buf[128];
3186 sprintf (buf, "%s (->%s)",
3187 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3190 return GNUNET_h2s (GC_h2hc (&c->id));
3195 * Log all possible info about the connection state.
3197 * @param c Connection to debug.
3198 * @param level Debug level to use.
3201 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3206 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3208 __FILE__, __FUNCTION__, __LINE__);
3214 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3218 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3219 s = path_2s (c->path);
3220 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3222 LOG2 (level, "CCC state: %s, destroy: %u\n",
3223 GCC_state2s (c->state), c->destroy);
3224 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3225 if (NULL != c->perf)
3226 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3228 LOG2 (level, "CCC FWD flow control:\n");
3229 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3230 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3231 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3232 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3233 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3234 LOG2 (level, "CCC POLL: task %d, msg %p, msg_ack %p)\n",
3235 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3237 LOG2 (level, "CCC BCK flow control:\n");
3238 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3239 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3240 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3241 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3242 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3243 LOG2 (level, "CCC POLL: task %d, msg %p, msg_ack %p)\n",
3244 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3246 LOG2 (level, "CCC DEBUG CONNECTION END\n");