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 unsigned 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 pid %u\n",
586 sent ? "" : "not ", GC_f2s (fwd), GC_m2s (type), pid);
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 or soon to be destroyed.
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");
916 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
918 GNUNET_assert (NULL != c->t);
919 msg.size = htons (sizeof (msg));
920 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
922 GNUNET_assert (NULL ==
923 GCT_send_prebuilt_message (&msg, c->t, c,
924 GNUNET_NO, NULL, NULL));
929 * Send CONNECTION_{CREATE/ACK} packets for a connection.
931 * @param c Connection for which to send the message.
932 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
935 connection_recreate (struct CadetConnection *c, int fwd)
937 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
941 send_connection_ack (c, GNUNET_NO);
946 * Generic connection timer management.
947 * Depending on the role of the peer in the connection will send the
948 * appropriate message (build or keepalive)
950 * @param c Conncetion to maintain.
954 connection_maintain (struct CadetConnection *c, int fwd)
956 if (GNUNET_NO != c->destroy)
958 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
965 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
969 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
971 /* If status is SEARCHING, why is there a connection? Should be WAITING */
973 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
974 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
975 schedule_next_keepalive (c, fwd);
980 case CADET_CONNECTION_NEW:
983 case CADET_CONNECTION_SENT:
984 connection_recreate (c, fwd);
986 case CADET_CONNECTION_READY:
987 send_connection_keepalive (c, fwd);
997 * Keep the connection alive.
999 * @param c Connection to keep alive.
1000 * @param fwd Direction.
1001 * @param shutdown Are we shutting down? (Don't send traffic)
1002 * Non-zero value for true, not necessarily GNUNET_YES.
1005 connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
1007 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s keepalive for %s\n",
1008 GC_f2s (fwd), GCC_2s (c));
1011 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1013 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1015 if (GNUNET_NO != shutdown)
1018 connection_maintain (c, fwd);
1020 /* Next execution will be scheduled by message_sent or _maintain*/
1025 * Keep the connection alive in the FWD direction.
1027 * @param cls Closure (connection to keepalive).
1028 * @param tc TaskContext.
1031 connection_fwd_keepalive (void *cls,
1032 const struct GNUNET_SCHEDULER_TaskContext *tc)
1034 connection_keepalive ((struct CadetConnection *) cls,
1036 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1041 * Keep the connection alive in the BCK direction.
1043 * @param cls Closure (connection to keepalive).
1044 * @param tc TaskContext.
1047 connection_bck_keepalive (void *cls,
1048 const struct GNUNET_SCHEDULER_TaskContext *tc)
1050 connection_keepalive ((struct CadetConnection *) cls,
1052 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1057 * Schedule next keepalive task, taking in consideration
1058 * the connection state and number of retries.
1060 * If the peer is not the origin, do nothing.
1062 * @param c Connection for which to schedule the next keepalive.
1063 * @param fwd Direction for the next keepalive.
1066 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1068 struct GNUNET_TIME_Relative delay;
1069 GNUNET_SCHEDULER_TaskIdentifier *task_id;
1070 GNUNET_SCHEDULER_Task keepalive_task;
1072 if (GNUNET_NO == GCC_is_origin (c, fwd))
1075 /* Calculate delay to use, depending on the state of the connection */
1076 if (CADET_CONNECTION_READY == c->state)
1078 delay = refresh_connection_time;
1082 if (1 > c->create_retry)
1083 c->create_retry = 1;
1084 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1086 if (c->create_retry < 64)
1087 c->create_retry *= 2;
1090 /* Select direction-dependent parameters */
1091 if (GNUNET_YES == fwd)
1093 task_id = &c->fwd_maintenance_task;
1094 keepalive_task = &connection_fwd_keepalive;
1098 task_id = &c->bck_maintenance_task;
1099 keepalive_task = &connection_bck_keepalive;
1102 /* Check that no one scheduled it before us */
1103 if (GNUNET_SCHEDULER_NO_TASK != *task_id)
1105 /* No need for a _break. It can happen for instance when sending a SYNACK
1106 * for a duplicate SYN: the first SYNACK scheduled the task. */
1107 GNUNET_SCHEDULER_cancel (*task_id);
1110 /* Schedule the task */
1111 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1112 LOG (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1113 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1118 * @brief Re-initiate traffic on this connection if necessary.
1120 * Check if there is traffic queued towards this peer
1121 * and the core transmit handle is NULL (traffic was stalled).
1122 * If so, call core tmt rdy.
1124 * @param c Connection on which initiate traffic.
1125 * @param fwd Is this about fwd traffic?
1128 connection_unlock_queue (struct CadetConnection *c, int fwd)
1130 struct CadetPeer *peer;
1132 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_unlock_queue %s on %s\n",
1133 GC_f2s (fwd), GCC_2s (c));
1135 if (GCC_is_terminal (c, fwd))
1137 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal, can unlock!\n");
1141 peer = get_hop (c, fwd);
1142 GCP_queue_unlock (peer, c);
1147 * Cancel all transmissions that belong to a certain connection.
1149 * If the connection is scheduled for destruction and no more messages are left,
1150 * the connection will be destroyed by the continuation call.
1152 * @param c Connection which to cancel. Might be destroyed during this call.
1153 * @param fwd Cancel fwd traffic?
1156 connection_cancel_queues (struct CadetConnection *c, int fwd)
1158 struct CadetFlowControl *fc;
1159 struct CadetPeer *peer;
1161 LOG (GNUNET_ERROR_TYPE_DEBUG,
1162 " *** Cancel %s queues for connection %s\n",
1163 GC_f2s (fwd), GCC_2s (c));
1170 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1171 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1173 GNUNET_SCHEDULER_cancel (fc->poll_task);
1174 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1175 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1177 peer = get_hop (c, fwd);
1178 GCP_queue_cancel (peer, c);
1183 * Function called if a connection has been stalled for a while,
1184 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1186 * @param cls Closure (poll ctx).
1187 * @param tc TaskContext.
1190 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1194 * Callback called when a queued POLL message is sent.
1196 * @param cls Closure (FC).
1197 * @param c Connection this message was on.
1198 * @param q Queue handler this call invalidates.
1199 * @param type Type of message sent.
1200 * @param fwd Was this a FWD going message?
1201 * @param size Size of the message.
1204 poll_sent (void *cls,
1205 struct CadetConnection *c,
1206 struct CadetConnectionQueue *q,
1207 uint16_t type, int fwd, size_t size)
1209 struct CadetFlowControl *fc = cls;
1211 if (2 == c->destroy)
1213 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1216 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL sent for , scheduling new one!\n");
1217 fc->poll_msg = NULL;
1218 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1219 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1220 &connection_poll, fc);
1221 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1226 * Function called if a connection has been stalled for a while,
1227 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1229 * @param cls Closure (poll ctx).
1230 * @param tc TaskContext.
1233 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1235 struct CadetFlowControl *fc = cls;
1236 struct GNUNET_CADET_Poll msg;
1237 struct CadetConnection *c;
1239 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1240 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1246 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling connection %s %s\n",
1247 GCC_2s (c), fc == &c->fwd_fc ? "FWD" : "BCK");
1249 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1250 msg.header.size = htons (sizeof (msg));
1251 msg.pid = htonl (fc->last_pid_sent);
1252 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1254 GCC_send_prebuilt_message (&msg.header, 0, fc->last_pid_sent, c,
1255 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1256 GNUNET_assert (NULL != fc->poll_msg);
1261 * Resend all queued messages for a connection on other connections of the
1262 * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
1264 * @param c Connection whose messages to resend.
1265 * @param fwd Resend fwd messages?
1268 resend_messages_and_destroy (struct CadetConnection *c, int fwd)
1270 struct GNUNET_MessageHeader *out_msg;
1271 struct CadetTunnel *t = c->t;
1272 struct CadetPeer *neighbor;
1273 unsigned int pending;
1276 c->state = CADET_CONNECTION_DESTROYED;
1277 c->destroy = GNUNET_YES;
1279 destroyed = GNUNET_NO;
1280 neighbor = get_hop (c, fwd);
1281 pending = c->pending_messages;
1283 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1286 GCT_resend_message (out_msg, t);
1287 GNUNET_free (out_msg);
1290 /* All pending messages should have been popped,
1291 * and the connection destroyed by the continuation.
1293 if (GNUNET_YES != destroyed)
1298 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1299 if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1307 * Timeout function due to lack of keepalive/traffic from the owner.
1308 * Destroys connection if called.
1310 * @param cls Closure (connection to destroy).
1311 * @param tc TaskContext.
1314 connection_fwd_timeout (void *cls,
1315 const struct GNUNET_SCHEDULER_TaskContext *tc)
1317 struct CadetConnection *c = cls;
1319 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1320 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1323 LOG (GNUNET_ERROR_TYPE_INFO, "Connection %s FWD timed out. Destroying.\n",
1325 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1327 if (GCC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1333 /* If dest, salvage queued traffic. */
1334 if (GCC_is_origin (c, GNUNET_NO) && 0 < c->bck_fc.queue_n)
1336 send_broken_unknown (&c->id, &my_full_id, NULL,
1337 GCP_get_id( get_prev_hop (c)));
1338 resend_messages_and_destroy (c, GNUNET_NO);
1347 * Timeout function due to lack of keepalive/traffic from the destination.
1348 * Destroys connection if called.
1350 * FIXME refactor and merge with connection_fwd_timeout.
1352 * @param cls Closure (connection to destroy).
1353 * @param tc TaskContext
1356 connection_bck_timeout (void *cls,
1357 const struct GNUNET_SCHEDULER_TaskContext *tc)
1359 struct CadetConnection *c = cls;
1361 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
1362 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1365 LOG (GNUNET_ERROR_TYPE_INFO, "Connection %s BCK timed out. Destroying.\n",
1368 if (GCC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1374 /* If dest, salvage queued traffic. */
1375 if (GCC_is_origin (c, GNUNET_YES) && 0 < c->fwd_fc.queue_n)
1377 send_broken_unknown (&c->id, &my_full_id, NULL,
1378 GCP_get_id( get_next_hop (c)));
1379 resend_messages_and_destroy (c, GNUNET_YES);
1388 * Resets the connection timeout task, some other message has done the
1390 * - For the first peer on the direction this means to send
1391 * a keepalive or a path confirmation message (either create or ACK).
1392 * - For all other peers, this means to destroy the connection,
1393 * due to lack of activity.
1394 * Starts the timeout if no timeout was running (connection just created).
1396 * @param c Connection whose timeout to reset.
1397 * @param fwd Is this forward?
1399 * TODO use heap to improve efficiency of scheduler.
1402 connection_reset_timeout (struct CadetConnection *c, int fwd)
1404 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1406 if (GCC_is_origin (c, fwd)) /* Startpoint */
1408 schedule_next_keepalive (c, fwd);
1410 else /* Relay, endpoint. */
1412 struct GNUNET_TIME_Relative delay;
1413 GNUNET_SCHEDULER_TaskIdentifier *ti;
1414 GNUNET_SCHEDULER_Task f;
1416 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1418 if (GNUNET_SCHEDULER_NO_TASK != *ti)
1419 GNUNET_SCHEDULER_cancel (*ti);
1420 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1421 LOG (GNUNET_ERROR_TYPE_DEBUG, " timing out in %s\n",
1422 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1423 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1424 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1430 * Add the connection to the list of both neighbors.
1432 * @param c Connection.
1434 * @return #GNUNET_OK if everything went fine
1435 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1438 register_neighbors (struct CadetConnection *c)
1440 struct CadetPeer *next_peer;
1441 struct CadetPeer *prev_peer;
1443 next_peer = get_next_hop (c);
1444 prev_peer = get_prev_hop (c);
1446 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1448 path_debug (c->path);
1449 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1450 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1451 GCC_2s (c), next_peer);
1452 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GCP_2s (next_peer));
1453 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1454 GCC_2s (c), prev_peer);
1455 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GCP_2s (prev_peer));
1457 if (GNUNET_NO == GCP_is_neighbor (next_peer)
1458 || GNUNET_NO == GCP_is_neighbor (prev_peer))
1460 if (GCC_is_origin (c, GNUNET_YES))
1461 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1462 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1464 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1465 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1466 GCP_2s (prev_peer), GCP_is_neighbor (prev_peer));
1467 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1468 GCP_2s (next_peer), GCP_is_neighbor (next_peer));
1469 return GNUNET_SYSERR;
1472 GCP_add_connection (next_peer, c);
1473 GCP_add_connection (prev_peer, c);
1480 * Remove the connection from the list of both neighbors.
1482 * @param c Connection.
1485 unregister_neighbors (struct CadetConnection *c)
1487 struct CadetPeer *peer;
1489 peer = get_next_hop (c);
1490 if (GNUNET_OK != GCP_remove_connection (peer, c))
1492 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1493 if (NULL != c->t) GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1496 peer = get_prev_hop (c);
1497 if (GNUNET_OK != GCP_remove_connection (peer, c))
1499 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1500 if (NULL != c->t) GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1506 * Bind the connection to the peer and the tunnel to that peer.
1508 * If the peer has no tunnel, create one. Update tunnel and connection
1509 * data structres to reflect new status.
1511 * @param c Connection.
1515 add_to_peer (struct CadetConnection *c, struct CadetPeer *peer)
1517 GCP_add_tunnel (peer);
1518 c->t = GCP_get_tunnel (peer);
1519 GCT_add_connection (c->t, c);
1525 * Iterator to compare each connection's path with the path of a new connection.
1527 * If the connection conincides, the c member of path is set to the connection
1528 * and the destroy flag of the connection is set.
1530 * @param cls Closure (new path).
1531 * @param c Connection in the tunnel to check.
1534 check_path (void *cls, struct CadetConnection *c)
1536 struct CadetConnection *new_conn = cls;
1537 struct CadetPeerPath *path = new_conn->path;
1539 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s, length %u\n",
1540 GCC_2s (c), c->path->length);
1543 && c->destroy == GNUNET_NO
1544 && c->state != CADET_CONNECTION_BROKEN
1545 && c->state != CADET_CONNECTION_DESTROYED
1546 && c->path->length == path->length
1547 && 0 == memcmp (c->path->peers, path->peers,
1548 sizeof (path->peers[0]) * path->length))
1550 new_conn->destroy = GNUNET_YES;
1551 new_conn->path->c = c;
1552 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1557 * Finds out if this path is already being used by and existing connection.
1559 * Checks the tunnel towards the first peer in the path to see if it contains
1560 * any connection with the same path.
1562 * If the existing connection is ready, it is kept.
1563 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1564 * the peer will eventually reject our attempt).
1566 * @param path Path to check.
1568 * @return GNUNET_YES if the tunnel has a connection with the same path,
1569 * GNUNET_NO otherwise.
1572 does_connection_exist (struct CadetConnection *conn)
1574 struct CadetPeer *p;
1575 struct CadetTunnel *t;
1576 struct CadetConnection *c;
1578 p = GCP_get_short (conn->path->peers[0]);
1579 t = GCP_get_tunnel (p);
1583 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1585 GCT_iterate_connections (t, &check_path, conn);
1587 if (GNUNET_YES == conn->destroy)
1590 conn->destroy = GNUNET_NO;
1591 conn->path->c = conn;
1592 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1593 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1594 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1595 if (CADET_CONNECTION_READY == c->state)
1597 /* The other peer confirmed this connection,
1598 * they should not try to duplicate it. */
1599 GNUNET_break_op (0);
1603 if (GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GCP_get_id (p)) > 0)
1610 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1617 * Log receipt of message on stderr (INFO level).
1619 * @param message Message received.
1620 * @param peer Peer who sent the message.
1621 * @param hash Connection ID.
1624 log_message (const struct GNUNET_MessageHeader *message,
1625 const struct GNUNET_PeerIdentity *peer,
1626 const struct GNUNET_CADET_Hash *hash)
1628 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1629 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1630 LOG (GNUNET_ERROR_TYPE_INFO, "<-- %s on connection %s from %s\n",
1631 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (hash)),
1635 /******************************************************************************/
1636 /******************************** API ***********************************/
1637 /******************************************************************************/
1640 * Core handler for connection creation.
1642 * @param cls Closure (unused).
1643 * @param peer Sender (neighbor).
1644 * @param message Message.
1646 * @return GNUNET_OK to keep the connection open,
1647 * GNUNET_SYSERR to close it (signal serious error)
1650 GCC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1651 const struct GNUNET_MessageHeader *message)
1653 struct GNUNET_CADET_ConnectionCreate *msg;
1654 struct GNUNET_PeerIdentity *id;
1655 struct GNUNET_CADET_Hash *cid;
1656 struct CadetPeerPath *path;
1657 struct CadetPeer *dest_peer;
1658 struct CadetPeer *orig_peer;
1659 struct CadetConnection *c;
1660 unsigned int own_pos;
1664 size = ntohs (message->size);
1665 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1667 GNUNET_break_op (0);
1671 /* Calculate hops */
1672 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1673 if (size % sizeof (struct GNUNET_PeerIdentity))
1675 GNUNET_break_op (0);
1678 size /= sizeof (struct GNUNET_PeerIdentity);
1681 GNUNET_break_op (0);
1684 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1686 /* Get parameters */
1687 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
1689 log_message (message, peer, cid);
1690 id = (struct GNUNET_PeerIdentity *) &msg[1];
1691 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1693 /* Create connection */
1694 c = connection_get (cid);
1697 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1698 size, myid, &own_pos);
1704 GNUNET_break_op (0);
1705 path_destroy (path);
1709 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1710 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1711 c = GCC_new (cid, NULL, path, own_pos);
1714 if (path->length - 1 == own_pos)
1716 /* If we are destination, why did the creation fail? */
1718 path_destroy (path);
1721 send_broken_unknown (cid, &my_full_id,
1722 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1724 path_destroy (path);
1727 GCP_add_path_to_all (path, GNUNET_NO);
1728 connection_reset_timeout (c, GNUNET_YES);
1732 path = path_duplicate (c->path);
1734 if (CADET_CONNECTION_NEW == c->state)
1735 connection_change_state (c, CADET_CONNECTION_SENT);
1737 /* Remember peers */
1738 dest_peer = GCP_get (&id[size - 1]);
1739 orig_peer = GCP_get (&id[0]);
1741 /* Is it a connection to us? */
1742 if (c->own_pos == path->length - 1)
1744 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1745 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1747 add_to_peer (c, orig_peer);
1748 if (GNUNET_YES == does_connection_exist (c))
1750 path_destroy (path);
1752 // FIXME use explicit duplicate
1753 send_broken_unknown (cid, &my_full_id, NULL, peer);
1758 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
1759 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
1761 send_connection_ack (c, GNUNET_NO);
1762 if (CADET_CONNECTION_SENT == c->state)
1763 connection_change_state (c, CADET_CONNECTION_ACK);
1767 /* It's for somebody else! Retransmit. */
1768 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1769 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1770 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1771 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
1772 GNUNET_YES, GNUNET_YES,
1775 path_destroy (path);
1781 * Core handler for path confirmations.
1783 * @param cls closure
1784 * @param message message
1785 * @param peer peer identity this notification is about
1787 * @return GNUNET_OK to keep the connection open,
1788 * GNUNET_SYSERR to close it (signal serious error)
1791 GCC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1792 const struct GNUNET_MessageHeader *message)
1794 struct GNUNET_CADET_ConnectionACK *msg;
1795 struct CadetConnection *c;
1796 struct CadetPeerPath *p;
1797 struct CadetPeer *pi;
1798 enum CadetConnectionState oldstate;
1801 msg = (struct GNUNET_CADET_ConnectionACK *) message;
1802 log_message (message, peer, &msg->cid);
1803 c = connection_get (&msg->cid);
1806 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1808 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1809 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
1813 if (GNUNET_NO != c->destroy)
1815 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1819 oldstate = c->state;
1820 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1821 pi = GCP_get (peer);
1822 if (get_next_hop (c) == pi)
1824 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1826 if (CADET_CONNECTION_SENT == oldstate)
1827 connection_change_state (c, CADET_CONNECTION_ACK);
1829 else if (get_prev_hop (c) == pi)
1831 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
1833 connection_change_state (c, CADET_CONNECTION_READY);
1837 GNUNET_break_op (0);
1841 connection_reset_timeout (c, fwd);
1843 /* Add path to peers? */
1847 GCP_add_path_to_all (p, GNUNET_YES);
1854 /* Message for us as creator? */
1855 if (GCC_is_origin (c, GNUNET_YES))
1857 if (GNUNET_NO != fwd)
1859 GNUNET_break_op (0);
1862 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1864 /* If just created, cancel the short timeout and start a long one */
1865 if (CADET_CONNECTION_SENT == oldstate)
1866 connection_reset_timeout (c, GNUNET_YES);
1868 /* Change connection state */
1869 connection_change_state (c, CADET_CONNECTION_READY);
1870 send_connection_ack (c, GNUNET_YES);
1872 /* Change tunnel state, trigger KX */
1873 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
1874 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
1879 /* Message for us as destination? */
1880 if (GCC_is_terminal (c, GNUNET_YES))
1882 if (GNUNET_YES != fwd)
1884 GNUNET_break_op (0);
1887 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1889 /* If just created, cancel the short timeout and start a long one */
1890 if (CADET_CONNECTION_ACK == oldstate)
1891 connection_reset_timeout (c, GNUNET_NO);
1893 /* Change tunnel state */
1894 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
1895 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
1900 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1901 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
1902 GNUNET_YES, NULL, NULL));
1908 * Core handler for notifications of broken connections.
1910 * @param cls Closure (unused).
1911 * @param id Peer identity of sending neighbor.
1912 * @param message Message.
1914 * @return GNUNET_OK to keep the connection open,
1915 * GNUNET_SYSERR to close it (signal serious error)
1918 GCC_handle_broken (void* cls,
1919 const struct GNUNET_PeerIdentity* id,
1920 const struct GNUNET_MessageHeader* message)
1922 struct GNUNET_CADET_ConnectionBroken *msg;
1923 struct CadetConnection *c;
1924 struct CadetTunnel *t;
1928 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
1929 log_message (message, id, &msg->cid);
1930 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1931 GNUNET_i2s (&msg->peer1));
1932 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1933 GNUNET_i2s (&msg->peer2));
1934 c = connection_get (&msg->cid);
1937 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
1943 fwd = is_fwd (c, id);
1944 c->destroy = GNUNET_YES;
1945 if (GCC_is_terminal (c, fwd))
1947 struct CadetPeer *endpoint;
1951 /* A terminal connection should not have 't' set to NULL. */
1953 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1956 endpoint = GCP_get_short (c->path->peers[c->path->length - 1]);
1957 path_invalidate (c->path);
1958 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
1960 c->state = CADET_CONNECTION_BROKEN;
1961 GCT_remove_connection (t, c);
1964 pending = c->pending_messages;
1966 resend_messages_and_destroy (c, !fwd);
1972 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
1973 GNUNET_YES, NULL, NULL));
1974 connection_cancel_queues (c, !fwd);
1982 * Core handler for tunnel destruction
1984 * @param cls Closure (unused).
1985 * @param peer Peer identity of sending neighbor.
1986 * @param message Message.
1988 * @return GNUNET_OK to keep the connection open,
1989 * GNUNET_SYSERR to close it (signal serious error)
1992 GCC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1993 const struct GNUNET_MessageHeader *message)
1995 struct GNUNET_CADET_ConnectionDestroy *msg;
1996 struct CadetConnection *c;
1999 msg = (struct GNUNET_CADET_ConnectionDestroy *) message;
2000 log_message (message, peer, &msg->cid);
2001 c = connection_get (&msg->cid);
2004 /* Probably already got the message from another path,
2005 * destroyed the tunnel and retransmitted to children.
2008 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2010 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
2013 fwd = is_fwd (c, peer);
2014 if (GNUNET_SYSERR == fwd)
2016 GNUNET_break_op (0); /* FIXME */
2019 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2020 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2021 GNUNET_YES, NULL, NULL));
2022 else if (0 == c->pending_messages)
2024 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
2028 c->destroy = GNUNET_YES;
2029 c->state = CADET_CONNECTION_DESTROYED;
2032 GCT_remove_connection (c->t, c);
2040 * Generic handler for cadet network encrypted traffic.
2042 * @param peer Peer identity this notification is about.
2043 * @param msg Encrypted message.
2045 * @return GNUNET_OK to keep the connection open,
2046 * GNUNET_SYSERR to close it (signal serious error)
2049 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
2050 const struct GNUNET_CADET_Encrypted *msg)
2052 struct CadetConnection *c;
2053 struct CadetPeer *neighbor;
2054 struct CadetFlowControl *fc;
2055 GNUNET_PEER_Id peer_id;
2061 log_message (&msg->header, peer, &msg->cid);
2064 size = ntohs (msg->header.size);
2066 sizeof (struct GNUNET_CADET_Encrypted) +
2067 sizeof (struct GNUNET_MessageHeader))
2069 GNUNET_break_op (0);
2073 /* Check connection */
2074 c = connection_get (&msg->cid);
2077 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
2078 LOG (GNUNET_ERROR_TYPE_DEBUG, "enc on unknown connection %s\n",
2079 GNUNET_h2s (GC_h2hc (&msg->cid)));
2080 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2084 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s\n", GCC_2s (c));
2086 /* Check if origin is as expected */
2087 neighbor = get_prev_hop (c);
2088 peer_id = GNUNET_PEER_search (peer);
2089 if (peer_id == GCP_get_short_id (neighbor))
2095 neighbor = get_next_hop (c);
2096 if (peer_id == GCP_get_short_id (neighbor))
2102 /* Unexpected peer sending traffic on a connection. */
2103 GNUNET_break_op (0);
2109 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2110 pid = ntohl (msg->pid);
2111 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u+)\n",
2112 pid, fc->last_pid_recv + 1);
2113 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2115 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2116 GNUNET_break_op (0);
2117 LOG (GNUNET_ERROR_TYPE_WARNING,
2118 "Received PID %u, (prev %u), ACK %u\n",
2119 pid, fc->last_pid_recv, fc->last_ack_sent);
2122 if (GNUNET_NO == GC_is_pid_bigger (pid, fc->last_pid_recv))
2124 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
2125 LOG (GNUNET_ERROR_TYPE_WARNING,
2126 " PID %u not expected (%u+), dropping!\n",
2127 pid, fc->last_pid_recv + 1);
2130 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2131 connection_change_state (c, CADET_CONNECTION_READY);
2132 connection_reset_timeout (c, fwd);
2133 fc->last_pid_recv = pid;
2135 /* Is this message for us? */
2136 if (GCC_is_terminal (c, fwd))
2138 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2139 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2143 GNUNET_break (GNUNET_NO != c->destroy);
2146 fc->last_pid_recv = pid;
2147 GCT_handle_encrypted (c->t, msg);
2148 GCC_send_ack (c, fwd, GNUNET_NO);
2152 /* Message not for us: forward to next hop */
2153 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2154 ttl = ntohl (msg->ttl);
2155 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2158 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2159 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2160 GCC_send_ack (c, fwd, GNUNET_NO);
2164 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2165 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2166 GNUNET_NO, NULL, NULL));
2172 * Generic handler for cadet network encrypted traffic.
2174 * @param peer Peer identity this notification is about.
2175 * @param msg Encrypted message.
2177 * @return GNUNET_OK to keep the connection open,
2178 * GNUNET_SYSERR to close it (signal serious error)
2181 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2182 const struct GNUNET_CADET_KX *msg)
2184 struct CadetConnection *c;
2185 struct CadetPeer *neighbor;
2186 GNUNET_PEER_Id peer_id;
2190 log_message (&msg->header, peer, &msg->cid);
2193 size = ntohs (msg->header.size);
2195 sizeof (struct GNUNET_CADET_KX) +
2196 sizeof (struct GNUNET_MessageHeader))
2198 GNUNET_break_op (0);
2202 /* Check connection */
2203 c = connection_get (&msg->cid);
2206 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
2207 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx on unknown connection %s\n",
2208 GNUNET_h2s (GC_h2hc (&msg->cid)));
2209 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2212 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GCC_2s (c));
2214 /* Check if origin is as expected */
2215 neighbor = get_prev_hop (c);
2216 peer_id = GNUNET_PEER_search (peer);
2217 if (peer_id == GCP_get_short_id (neighbor))
2223 neighbor = get_next_hop (c);
2224 if (peer_id == GCP_get_short_id (neighbor))
2230 /* Unexpected peer sending traffic on a connection. */
2231 GNUNET_break_op (0);
2236 /* Count as connection confirmation. */
2237 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2239 connection_change_state (c, CADET_CONNECTION_READY);
2242 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2243 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2246 connection_reset_timeout (c, fwd);
2248 /* Is this message for us? */
2249 if (GCC_is_terminal (c, fwd))
2251 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2252 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2258 GCT_handle_kx (c->t, &msg[1].header);
2262 /* Message not for us: forward to next hop */
2263 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2264 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2265 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2266 GNUNET_NO, NULL, NULL));
2273 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2275 * @param cls Closure (unused).
2276 * @param message Message received.
2277 * @param peer Peer who sent the message.
2279 * @return GNUNET_OK to keep the connection open,
2280 * GNUNET_SYSERR to close it (signal serious error)
2283 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2284 const struct GNUNET_MessageHeader *message)
2286 return handle_cadet_encrypted (peer,
2287 (struct GNUNET_CADET_Encrypted *)message);
2292 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2294 * @param cls Closure (unused).
2295 * @param message Message received.
2296 * @param peer Peer who sent the message.
2298 * @return GNUNET_OK to keep the connection open,
2299 * GNUNET_SYSERR to close it (signal serious error)
2302 GCC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2303 const struct GNUNET_MessageHeader *message)
2305 return handle_cadet_kx (peer,
2306 (struct GNUNET_CADET_KX *) message);
2311 * Core handler for cadet network traffic point-to-point acks.
2313 * @param cls closure
2314 * @param message message
2315 * @param peer peer identity this notification is about
2317 * @return GNUNET_OK to keep the connection open,
2318 * GNUNET_SYSERR to close it (signal serious error)
2321 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2322 const struct GNUNET_MessageHeader *message)
2324 struct GNUNET_CADET_ACK *msg;
2325 struct CadetConnection *c;
2326 struct CadetFlowControl *fc;
2331 msg = (struct GNUNET_CADET_ACK *) message;
2332 log_message (message, peer, &msg->cid);
2333 c = connection_get (&msg->cid);
2336 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2338 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2342 /* Is this a forward or backward ACK? */
2343 id = GNUNET_PEER_search (peer);
2344 if (GCP_get_short_id (get_next_hop (c)) == id)
2346 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
2350 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2352 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
2358 GNUNET_break_op (0);
2362 ack = ntohl (msg->ack);
2363 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
2364 ack, fc->last_ack_recv);
2365 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2366 fc->last_ack_recv = ack;
2368 /* Cancel polling if the ACK is big enough. */
2369 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
2370 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2372 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2373 GNUNET_SCHEDULER_cancel (fc->poll_task);
2374 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2375 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2378 connection_unlock_queue (c, fwd);
2385 * Core handler for cadet network traffic point-to-point ack polls.
2387 * @param cls closure
2388 * @param message message
2389 * @param peer peer identity this notification is about
2391 * @return GNUNET_OK to keep the connection open,
2392 * GNUNET_SYSERR to close it (signal serious error)
2395 GCC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2396 const struct GNUNET_MessageHeader *message)
2398 struct GNUNET_CADET_Poll *msg;
2399 struct CadetConnection *c;
2400 struct CadetFlowControl *fc;
2405 msg = (struct GNUNET_CADET_Poll *) message;
2406 log_message (message, peer, &msg->cid);
2407 c = connection_get (&msg->cid);
2410 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2412 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL message on unknown connection %s!\n",
2413 GNUNET_h2s (GC_h2hc (&msg->cid)));
2414 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2418 /* Is this a forward or backward ACK?
2419 * Note: a poll should never be needed in a loopback case,
2420 * since there is no possiblility of packet loss there, so
2421 * this way of discerining FWD/BCK should not be a problem.
2423 id = GNUNET_PEER_search (peer);
2424 if (GCP_get_short_id (get_next_hop (c)) == id)
2426 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2429 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2431 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2436 GNUNET_break_op (0);
2440 pid = ntohl (msg->pid);
2441 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2442 fc->last_pid_recv = pid;
2443 fwd = fc == &c->bck_fc;
2444 GCC_send_ack (c, fwd, GNUNET_YES);
2451 * Send an ACK on the appropriate connection/channel, depending on
2452 * the direction and the position of the peer.
2454 * @param c Which connection to send the hop-by-hop ACK.
2455 * @param fwd Is this a fwd ACK? (will go dest->root).
2456 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2459 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2461 unsigned int buffer;
2463 LOG (GNUNET_ERROR_TYPE_DEBUG,
2464 "GMC send %s ACK on %s\n",
2465 GC_f2s (fwd), GCC_2s (c));
2473 if (GNUNET_NO != c->destroy)
2475 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2479 /* Get available buffer space */
2480 if (GCC_is_terminal (c, fwd))
2482 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2483 buffer = GCT_get_channels_buffer (c->t);
2487 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2488 buffer = GCC_get_buffer (c, fwd);
2490 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2491 if (0 == buffer && GNUNET_NO == force)
2494 /* Send available buffer space */
2495 if (GCC_is_origin (c, fwd))
2497 GNUNET_assert (NULL != c->t);
2498 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2499 GCT_unchoke_channels (c->t);
2503 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2504 send_ack (c, buffer, fwd, force);
2510 * Initialize the connections subsystem
2512 * @param c Configuration handle.
2515 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2517 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2519 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2522 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2523 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2524 GNUNET_SCHEDULER_shutdown ();
2529 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2532 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2533 "CADET", "MAX_CONNECTIONS", "MISSING");
2534 GNUNET_SCHEDULER_shutdown ();
2539 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2540 &refresh_connection_time))
2542 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2543 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2544 GNUNET_SCHEDULER_shutdown ();
2547 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2548 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2553 * Destroy each connection on shutdown.
2555 * @param cls Closure (unused).
2556 * @param key Current key code (CID, unused).
2557 * @param value Value in the hash map (connection)
2559 * @return #GNUNET_YES, because we should continue to iterate,
2562 shutdown_iterator (void *cls,
2563 const struct GNUNET_HashCode *key,
2566 struct CadetConnection *c = value;
2574 * Shut down the connections subsystem.
2579 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2580 GNUNET_CONTAINER_multihashmap_destroy (connections);
2586 * Create a connection.
2588 * @param cid Connection ID (either created locally or imposed remotely).
2589 * @param t Tunnel this connection belongs to (or NULL);
2590 * @param path Path this connection has to use (copy is made).
2591 * @param own_pos Own position in the @c path path.
2593 * @return Newly created connection, NULL in case of error (own id not in path).
2595 struct CadetConnection *
2596 GCC_new (const struct GNUNET_CADET_Hash *cid,
2597 struct CadetTunnel *t,
2598 struct CadetPeerPath *path,
2599 unsigned int own_pos)
2601 struct CadetConnection *c;
2602 struct CadetPeerPath *p;
2604 p = path_duplicate (path);
2605 c = GNUNET_new (struct CadetConnection);
2607 GNUNET_assert (GNUNET_OK ==
2608 GNUNET_CONTAINER_multihashmap_put (connections,
2610 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2611 fc_init (&c->fwd_fc);
2612 fc_init (&c->bck_fc);
2617 GNUNET_assert (own_pos <= p->length - 1);
2618 c->own_pos = own_pos;
2622 if (GNUNET_OK != register_neighbors (c))
2626 path_invalidate (c->path);
2633 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2639 GCC_destroy (struct CadetConnection *c)
2647 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2648 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2651 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GCC_2s (c));
2652 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2653 &c->fwd_fc, &c->bck_fc);
2654 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2655 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2657 /* Cancel all traffic */
2658 if (NULL != c->path)
2660 connection_cancel_queues (c, GNUNET_YES);
2661 connection_cancel_queues (c, GNUNET_NO);
2662 unregister_neighbors (c);
2665 /* Cancel maintainance task (keepalive/timeout) */
2666 if (NULL != c->fwd_fc.poll_msg)
2668 GCC_cancel (c->fwd_fc.poll_msg);
2669 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2671 if (NULL != c->bck_fc.poll_msg)
2673 GCC_cancel (c->bck_fc.poll_msg);
2674 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2677 /* Delete from tunnel */
2679 GCT_remove_connection (c->t, c);
2681 if (GNUNET_NO == GCC_is_origin (c, GNUNET_YES) && NULL != c->path)
2682 path_destroy (c->path);
2683 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
2684 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2685 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
2686 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2687 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
2689 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2690 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2692 if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
2694 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2695 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2698 GNUNET_break (GNUNET_YES ==
2699 GNUNET_CONTAINER_multihashmap_remove (connections,
2702 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2707 * Get the connection ID.
2709 * @param c Connection to get the ID from.
2711 * @return ID of the connection.
2713 const struct GNUNET_CADET_Hash *
2714 GCC_get_id (const struct CadetConnection *c)
2721 * Get the connection ID.
2723 * @param c Connection to get the ID from.
2725 * @return ID of the connection.
2727 const struct GNUNET_HashCode *
2728 GCC_get_h (const struct CadetConnection *c)
2730 return GC_h2hc (&c->id);
2735 * Get the connection path.
2737 * @param c Connection to get the path from.
2739 * @return path used by the connection.
2741 const struct CadetPeerPath *
2742 GCC_get_path (const struct CadetConnection *c)
2744 if (GNUNET_NO == c->destroy)
2751 * Get the connection state.
2753 * @param c Connection to get the state from.
2755 * @return state of the connection.
2757 enum CadetConnectionState
2758 GCC_get_state (const struct CadetConnection *c)
2764 * Get the connection tunnel.
2766 * @param c Connection to get the tunnel from.
2768 * @return tunnel of the connection.
2770 struct CadetTunnel *
2771 GCC_get_tunnel (const struct CadetConnection *c)
2778 * Get free buffer space in a connection.
2780 * @param c Connection.
2781 * @param fwd Is query about FWD traffic?
2783 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2786 GCC_get_buffer (struct CadetConnection *c, int fwd)
2788 struct CadetFlowControl *fc;
2790 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2792 return (fc->queue_max - fc->queue_n);
2796 * Get how many messages have we allowed to send to us from a direction.
2798 * @param c Connection.
2799 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2801 * @return last_ack_sent - last_pid_recv
2804 GCC_get_allowed (struct CadetConnection *c, int fwd)
2806 struct CadetFlowControl *fc;
2808 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2809 if (CADET_CONNECTION_READY != c->state
2810 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
2814 return (fc->last_ack_sent - fc->last_pid_recv);
2818 * Get messages queued in a connection.
2820 * @param c Connection.
2821 * @param fwd Is query about FWD traffic?
2823 * @return Number of messages queued.
2826 GCC_get_qn (struct CadetConnection *c, int fwd)
2828 struct CadetFlowControl *fc;
2830 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2837 * Get next PID to use.
2839 * @param c Connection.
2840 * @param fwd Is query about FWD traffic?
2842 * @return Last PID used + 1.
2845 GCC_get_pid (struct CadetConnection *c, int fwd)
2847 struct CadetFlowControl *fc;
2849 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2851 return fc->last_pid_sent + 1;
2856 * Allow the connection to advertise a buffer of the given size.
2858 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2859 * allowing up to last_pid_recv + buffer.
2861 * @param c Connection.
2862 * @param buffer How many more messages the connection can accept.
2863 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2866 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
2868 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2869 GCC_2s (c), buffer, GC_f2s (fwd));
2870 send_ack (c, buffer, fwd, GNUNET_NO);
2875 * Notify other peers on a connection of a broken link. Mark connections
2876 * to destroy after all traffic has been sent.
2878 * @param c Connection on which there has been a disconnection.
2879 * @param peer Peer that disconnected.
2882 GCC_notify_broken (struct CadetConnection *c,
2883 struct CadetPeer *peer)
2887 LOG (GNUNET_ERROR_TYPE_DEBUG,
2888 " notify broken on %s due to %s disconnect\n",
2889 GCC_2s (c), GCP_2s (peer));
2891 fwd = peer == get_prev_hop (c);
2893 if (GNUNET_YES == GCC_is_terminal (c, fwd))
2895 /* Local shutdown, no one to notify about this. */
2899 if (GNUNET_NO == c->destroy)
2900 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
2902 /* Connection will have at least one pending message
2903 * (the one we just scheduled), so no point in checking whether to
2904 * destroy immediately. */
2905 c->destroy = GNUNET_YES;
2906 c->state = CADET_CONNECTION_DESTROYED;
2909 * Cancel all queues, if no message is left, connection will be destroyed.
2911 connection_cancel_queues (c, !fwd);
2918 * Is this peer the first one on the connection?
2920 * @param c Connection.
2921 * @param fwd Is this about fwd traffic?
2923 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
2926 GCC_is_origin (struct CadetConnection *c, int fwd)
2928 if (!fwd && c->path->length - 1 == c->own_pos )
2930 if (fwd && 0 == c->own_pos)
2937 * Is this peer the last one on the connection?
2939 * @param c Connection.
2940 * @param fwd Is this about fwd traffic?
2941 * Note that the ROOT is the terminal for BCK traffic!
2943 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
2946 GCC_is_terminal (struct CadetConnection *c, int fwd)
2948 return GCC_is_origin (c, !fwd);
2953 * See if we are allowed to send by the next hop in the given direction.
2955 * @param c Connection.
2956 * @param fwd Is this about fwd traffic?
2958 * @return #GNUNET_YES in case it's OK to send.
2961 GCC_is_sendable (struct CadetConnection *c, int fwd)
2963 struct CadetFlowControl *fc;
2965 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking sendability of %s traffic on %s\n",
2966 GC_f2s (fwd), GCC_2s (c));
2972 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2973 LOG (GNUNET_ERROR_TYPE_DEBUG,
2974 " last ack recv: %u, last pid sent: %u\n",
2975 fc->last_ack_recv, fc->last_pid_sent);
2976 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2978 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
2981 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
2987 * Check if this connection is a direct one (never trim a direct connection).
2989 * @param c Connection.
2991 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
2994 GCC_is_direct (struct CadetConnection *c)
2996 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3000 * Sends an already built message on a connection, properly registering
3001 * all used resources.
3003 * @param message Message to send. Function makes a copy of it.
3004 * If message is not hop-by-hop, decrements TTL of copy.
3005 * @param payload_type Type of payload, in case the message is encrypted.
3006 * @param c Connection on which this message is transmitted.
3007 * @param fwd Is this a fwd message?
3008 * @param force Force the connection to accept the message (buffer overfill).
3009 * @param cont Continuation called once message is sent. Can be NULL.
3010 * @param cont_cls Closure for @c cont.
3012 * @return Handle to cancel the message before it's sent.
3013 * NULL on error or if @c cont is NULL.
3014 * Invalid on @c cont call.
3016 struct CadetConnectionQueue *
3017 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3018 uint16_t payload_type, uint32_t payload_id,
3019 struct CadetConnection *c, int fwd, int force,
3020 GCC_sent cont, void *cont_cls)
3022 struct CadetFlowControl *fc;
3023 struct CadetConnectionQueue *q;
3029 size = ntohs (message->size);
3030 data = GNUNET_malloc (size);
3031 memcpy (data, message, size);
3032 type = ntohs (message->type);
3033 LOG (GNUNET_ERROR_TYPE_INFO, "--> %s (%s %9u) on connection %s (%u bytes)\n",
3034 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), size);
3036 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3037 droppable = GNUNET_NO == force;
3040 struct GNUNET_CADET_Encrypted *emsg;
3041 struct GNUNET_CADET_KX *kmsg;
3042 struct GNUNET_CADET_ACK *amsg;
3043 struct GNUNET_CADET_Poll *pmsg;
3044 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3045 struct GNUNET_CADET_ConnectionBroken *bmsg;
3048 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3049 emsg = (struct GNUNET_CADET_Encrypted *) data;
3050 ttl = ntohl (emsg->ttl);
3053 GNUNET_break_op (0);
3058 emsg->ttl = htonl (ttl - 1);
3059 emsg->pid = htonl (0);
3060 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3061 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3062 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3063 if (GNUNET_YES == droppable)
3069 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3071 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
3073 GCC_start_poll (c, fwd);
3077 case GNUNET_MESSAGE_TYPE_CADET_KX:
3078 kmsg = (struct GNUNET_CADET_KX *) data;
3082 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3083 amsg = (struct GNUNET_CADET_ACK *) data;
3085 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3086 droppable = GNUNET_NO;
3089 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3090 pmsg = (struct GNUNET_CADET_Poll *) data;
3092 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
3093 droppable = GNUNET_NO;
3096 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3097 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
3101 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3102 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
3106 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3107 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3116 if (fc->queue_n > fc->queue_max && droppable)
3118 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3121 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3122 fc->queue_n, fc->queue_max);
3123 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3128 return NULL; /* Drop this message */
3131 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3132 GCC_2s (c), c->pending_messages);
3133 c->pending_messages++;
3135 q = GNUNET_new (struct CadetConnectionQueue);
3136 q->forced = !droppable;
3137 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3138 size, c, fwd, &conn_message_sent, q);
3141 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3147 q->cont_cls = cont_cls;
3148 return (NULL == cont) ? NULL : q;
3153 * Cancel a previously sent message while it's in the queue.
3155 * ONLY can be called before the continuation given to the send function
3156 * is called. Once the continuation is called, the message is no longer in the
3159 * @param q Handle to the queue.
3162 GCC_cancel (struct CadetConnectionQueue *q)
3164 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
3166 /* queue destroy calls message_sent, which calls q->cont and frees q */
3167 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3172 * Sends a CREATE CONNECTION message for a path to a peer.
3173 * Changes the connection and tunnel states if necessary.
3175 * @param connection Connection to create.
3178 GCC_send_create (struct CadetConnection *connection)
3180 enum CadetTunnelCState state;
3183 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3184 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3186 LOG (GNUNET_ERROR_TYPE_INFO, "---> %s on connection %s (%u bytes)\n",
3187 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE),
3188 GCC_2s (connection), size);
3189 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3190 connection, connection->pending_messages);
3191 connection->pending_messages++;
3193 connection->maintenance_q =
3194 GCP_queue_add (get_next_hop (connection), NULL,
3195 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 0,
3196 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3198 state = GCT_get_cstate (connection->t);
3199 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3200 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3201 if (CADET_CONNECTION_NEW == connection->state)
3202 connection_change_state (connection, CADET_CONNECTION_SENT);
3207 * Send a message to all peers in this connection that the connection
3208 * is no longer valid.
3210 * If some peer should not receive the message, it should be zero'ed out
3211 * before calling this function.
3213 * @param c The connection whose peers to notify.
3216 GCC_send_destroy (struct CadetConnection *c)
3218 struct GNUNET_CADET_ConnectionDestroy msg;
3220 if (GNUNET_YES == c->destroy)
3223 msg.header.size = htons (sizeof (msg));
3224 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3226 LOG (GNUNET_ERROR_TYPE_DEBUG,
3227 " sending connection destroy for connection %s\n",
3230 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3231 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3232 GNUNET_YES, GNUNET_YES,
3234 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3235 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3236 GNUNET_NO, GNUNET_YES,
3238 c->destroy = GNUNET_YES;
3239 c->state = CADET_CONNECTION_DESTROYED;
3244 * @brief Start a polling timer for the connection.
3246 * When a neighbor does not accept more traffic on the connection it could be
3247 * caused by a simple congestion or by a lost ACK. Polling enables to check
3248 * for the lastest ACK status for a connection.
3250 * @param c Connection.
3251 * @param fwd Should we poll in the FWD direction?
3254 GCC_start_poll (struct CadetConnection *c, int fwd)
3256 struct CadetFlowControl *fc;
3258 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3259 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
3261 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
3263 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
3264 fc->poll_task, fc->poll_msg);
3267 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
3268 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3275 * @brief Stop polling a connection for ACKs.
3277 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3279 * @param c Connection.
3280 * @param fwd Should we stop the poll in the FWD direction?
3283 GCC_stop_poll (struct CadetConnection *c, int fwd)
3285 struct CadetFlowControl *fc;
3287 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3288 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3290 GNUNET_SCHEDULER_cancel (fc->poll_task);
3291 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3296 * Get a (static) string for a connection.
3298 * @param c Connection.
3301 GCC_2s (const struct CadetConnection *c)
3308 static char buf[128];
3310 SPRINTF (buf, "%s (->%s)",
3311 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3314 return GNUNET_h2s (GC_h2hc (&c->id));
3319 * Log all possible info about the connection state.
3321 * @param c Connection to debug.
3322 * @param level Debug level to use.
3325 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3330 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3332 __FILE__, __FUNCTION__, __LINE__);
3338 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3342 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3343 s = path_2s (c->path);
3344 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3346 LOG2 (level, "CCC state: %s, destroy: %u\n",
3347 GCC_state2s (c->state), c->destroy);
3348 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3349 if (NULL != c->perf)
3350 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3352 LOG2 (level, "CCC FWD flow control:\n");
3353 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3354 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3355 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3356 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3357 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3358 LOG2 (level, "CCC POLL: task %d, msg %p, msg_ack %p)\n",
3359 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3361 LOG2 (level, "CCC BCK flow control:\n");
3362 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3363 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3364 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3365 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3366 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3367 LOG2 (level, "CCC POLL: task %d, msg %p, msg_ack %p)\n",
3368 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3370 LOG2 (level, "CCC DEBUG CONNECTION END\n");