2 This file is part of GNUnet.
3 Copyright (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 * Bitmap of past 32 messages received:
87 * - LSB being @c last_pid_recv.
88 * - MSB being @c last_pid_recv - 31 (mod UINTMAX).
93 * Last ACK sent to the peer (peer can't send more than this PID).
95 uint32_t last_ack_sent;
98 * Last ACK sent towards the origin (for traffic towards leaf node).
100 uint32_t last_ack_recv;
103 * Task to poll the peer in case of a lost ACK causes stall.
105 struct GNUNET_SCHEDULER_Task * poll_task;
108 * How frequently to poll for ACKs.
110 struct GNUNET_TIME_Relative poll_time;
113 * Queued poll message, to cancel if not necessary anymore (got ACK).
115 struct CadetConnectionQueue *poll_msg;
118 * Queued poll message, to cancel if not necessary anymore (got ACK).
120 struct CadetConnectionQueue *ack_msg;
124 * Keep a record of the last messages sent on this connection.
126 struct CadetConnectionPerformance
129 * Circular buffer for storing measurements.
131 double usecsperbyte[AVG_MSGS];
134 * Running average of @c usecsperbyte.
139 * How many values of @c usecsperbyte are valid.
144 * Index of the next "free" position in @c usecsperbyte.
151 * Struct containing all information regarding a connection to a peer.
153 struct CadetConnection
156 * Tunnel this connection is part of.
158 struct CadetTunnel *t;
161 * Flow control information for traffic fwd.
163 struct CadetFlowControl fwd_fc;
166 * Flow control information for traffic bck.
168 struct CadetFlowControl bck_fc;
171 * Measure connection performance on the endpoint.
173 struct CadetConnectionPerformance *perf;
176 * ID of the connection.
178 struct GNUNET_CADET_Hash id;
181 * State of the connection.
183 enum CadetConnectionState state;
186 * Path being used for the tunnel. At the origin of the connection
187 * it's a pointer to the destination's path pool, otherwise just a copy.
189 struct CadetPeerPath *path;
192 * Position of the local peer in the path.
194 unsigned int own_pos;
197 * Task to keep the used paths alive at the owner,
198 * time tunnel out on all the other peers.
200 struct GNUNET_SCHEDULER_Task * fwd_maintenance_task;
203 * Task to keep the used paths alive at the destination,
204 * time tunnel out on all the other peers.
206 struct GNUNET_SCHEDULER_Task * bck_maintenance_task;
209 * Queue handle for maintainance traffic. One handle for FWD and BCK since
210 * one peer never needs to maintain both directions (no loopback connections).
212 struct CadetPeerQueue *maintenance_q;
215 * Counter to do exponential backoff when creating a connection (max 64).
217 unsigned short create_retry;
220 * Pending message count.
222 unsigned int pending_messages;
225 * Destroy flag: if true, destroy on last message.
231 * Handle for messages queued but not yet sent.
233 struct CadetConnectionQueue
236 * Peer queue handle, to cancel if necessary.
238 struct CadetPeerQueue *q;
241 * Was this a forced message? (Do not account for it)
246 * Continuation to call once sent.
251 * Closure for @c cont.
256 /******************************************************************************/
257 /******************************* GLOBALS ***********************************/
258 /******************************************************************************/
261 * Global handle to the statistics service.
263 extern struct GNUNET_STATISTICS_Handle *stats;
266 * Local peer own ID (memory efficient handle).
268 extern GNUNET_PEER_Id myid;
271 * Local peer own ID (full value).
273 extern struct GNUNET_PeerIdentity my_full_id;
276 * Connections known, indexed by cid (CadetConnection).
278 static struct GNUNET_CONTAINER_MultiHashMap *connections;
281 * How many connections are we willing to maintain.
282 * Local connections are always allowed, even if there are more connections than max.
284 static unsigned long long max_connections;
287 * How many messages *in total* are we willing to queue, divide by number of
288 * connections to get connection queue size.
290 static unsigned long long max_msgs_queue;
293 * How often to send path keepalives. Paths timeout after 4 missed.
295 static struct GNUNET_TIME_Relative refresh_connection_time;
298 * How often to send path create / ACKs.
300 static struct GNUNET_TIME_Relative create_connection_time;
303 /******************************************************************************/
304 /******************************** STATIC ***********************************/
305 /******************************************************************************/
307 #if 0 // avoid compiler warning for unused static function
309 fc_debug (struct CadetFlowControl *fc)
311 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
312 fc->last_pid_recv, fc->last_ack_sent);
313 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
314 fc->last_pid_sent, fc->last_ack_recv);
315 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
316 fc->queue_n, fc->queue_max);
320 connection_debug (struct CadetConnection *c)
324 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
327 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
328 peer2s (c->t->peer), GCC_2s (c));
329 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
330 c->state, c->pending_messages);
331 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
332 fc_debug (&c->fwd_fc);
333 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
334 fc_debug (&c->bck_fc);
340 * Schedule next keepalive task, taking in consideration
341 * the connection state and number of retries.
343 * @param c Connection for which to schedule the next keepalive.
344 * @param fwd Direction for the next keepalive.
347 schedule_next_keepalive (struct CadetConnection *c, int fwd);
351 * Resets the connection timeout task, some other message has done the
353 * - For the first peer on the direction this means to send
354 * a keepalive or a path confirmation message (either create or ACK).
355 * - For all other peers, this means to destroy the connection,
356 * due to lack of activity.
357 * Starts the timeout if no timeout was running (connection just created).
359 * @param c Connection whose timeout to reset.
360 * @param fwd Is this forward?
363 connection_reset_timeout (struct CadetConnection *c, int fwd);
367 * Get string description for tunnel state. Reentrant.
369 * @param s Tunnel state.
371 * @return String representation.
374 GCC_state2s (enum CadetConnectionState s)
378 case CADET_CONNECTION_NEW:
379 return "CADET_CONNECTION_NEW";
380 case CADET_CONNECTION_SENT:
381 return "CADET_CONNECTION_SENT";
382 case CADET_CONNECTION_ACK:
383 return "CADET_CONNECTION_ACK";
384 case CADET_CONNECTION_READY:
385 return "CADET_CONNECTION_READY";
386 case CADET_CONNECTION_DESTROYED:
387 return "CADET_CONNECTION_DESTROYED";
388 case CADET_CONNECTION_BROKEN:
389 return "CADET_CONNECTION_BROKEN";
392 LOG (GNUNET_ERROR_TYPE_ERROR, " conn state %u unknown!\n", s);
393 return "CADET_CONNECTION_STATE_ERROR";
399 * Initialize a Flow Control structure to the initial state.
401 * @param fc Flow Control structure to initialize.
404 fc_init (struct CadetFlowControl *fc)
406 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
407 fc->last_pid_recv = (uint32_t) -1;
408 fc->last_ack_sent = (uint32_t) 0;
409 fc->last_ack_recv = (uint32_t) 0;
410 fc->poll_task = NULL;
411 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
413 fc->queue_max = (max_msgs_queue / max_connections) + 1;
420 * @param cid Connection ID.
422 static struct CadetConnection *
423 connection_get (const struct GNUNET_CADET_Hash *cid)
425 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
430 connection_change_state (struct CadetConnection* c,
431 enum CadetConnectionState state)
433 LOG (GNUNET_ERROR_TYPE_DEBUG,
434 "Connection %s state %s -> %s\n",
435 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
436 if (CADET_CONNECTION_DESTROYED <= c->state) /* Destroyed or broken. */
438 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
442 if (CADET_CONNECTION_READY == state)
448 * Callback called when a queued ACK message is sent.
450 * @param cls Closure (FC).
451 * @param c Connection this message was on.
452 * @param q Queue handler this call invalidates.
453 * @param type Type of message sent.
454 * @param fwd Was this a FWD going message?
455 * @param size Size of the message.
459 struct CadetConnection *c,
460 struct CadetConnectionQueue *q,
461 uint16_t type, int fwd, size_t size)
463 struct CadetFlowControl *fc = cls;
470 * Send an ACK on the connection, informing the predecessor about
471 * the available buffer space. Should not be called in case the peer
472 * is origin (no predecessor) in the @c fwd direction.
474 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
475 * the ACK itself goes "back" (dest->root).
477 * @param c Connection on which to send the ACK.
478 * @param buffer How much space free to advertise?
479 * @param fwd Is this FWD ACK? (Going dest -> root)
480 * @param force Don't optimize out.
483 send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
485 struct CadetFlowControl *next_fc;
486 struct CadetFlowControl *prev_fc;
487 struct GNUNET_CADET_ACK msg;
491 /* If origin, there is no connection to send ACKs. Wrong function! */
492 if (GCC_is_origin (c, fwd))
494 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
495 GCC_2s (c), GC_f2s (fwd));
500 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
501 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
503 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
504 GC_f2s (fwd), GCC_2s (c));
506 /* Check if we need to transmit the ACK. */
507 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
508 if (3 < delta && buffer < delta && GNUNET_NO == force)
510 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
511 LOG (GNUNET_ERROR_TYPE_DEBUG,
512 " last pid recv: %u, last ack sent: %u\n",
513 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
517 /* Ok, ACK might be necessary, what PID to ACK? */
518 ack = prev_fc->last_pid_recv + buffer;
519 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
520 LOG (GNUNET_ERROR_TYPE_DEBUG,
521 " last pid %u, last ack %u, qmax %u, q %u\n",
522 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
523 next_fc->queue_max, next_fc->queue_n);
524 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
526 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
530 /* Check if message is already in queue */
531 if (NULL != prev_fc->ack_msg)
533 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
535 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
536 GCC_cancel (prev_fc->ack_msg);
537 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
541 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
546 prev_fc->last_ack_sent = ack;
548 /* Build ACK message and send on connection */
549 msg.header.size = htons (sizeof (msg));
550 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
551 msg.ack = htonl (ack);
554 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, 0, ack, c,
557 GNUNET_assert (NULL != prev_fc->ack_msg);
562 * Callback called when a connection queued message is sent.
564 * Calculates the average time and connection packet tracking.
566 * @param cls Closure (ConnectionQueue Handle).
567 * @param c Connection this message was on.
568 * @param sent Was it really sent? (Could have been canceled)
569 * @param type Type of message sent.
570 * @param pid Packet ID, or 0 if not applicable (create, destroy, etc).
571 * @param fwd Was this a FWD going message?
572 * @param size Size of the message.
573 * @param wait Time spent waiting for core (only the time for THIS message)
575 * @return #GNUNET_YES if connection was destroyed, #GNUNET_NO otherwise.
578 conn_message_sent (void *cls,
579 struct CadetConnection *c, int sent,
580 uint16_t type, uint32_t pid, int fwd, size_t size,
581 struct GNUNET_TIME_Relative wait)
583 struct CadetConnectionPerformance *p;
584 struct CadetFlowControl *fc;
585 struct CadetConnectionQueue *q = cls;
589 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
591 fc = fwd ? &c->fwd_fc : &c->bck_fc;
592 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
593 sent ? "" : "not ", GC_f2s (fwd), GC_m2s (type), pid);
599 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
600 q->cont (q->cont_cls, c, q, type, fwd, size);
604 else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED)
606 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
615 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
616 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
618 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
623 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
624 c->pending_messages--;
625 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
627 LOG (GNUNET_ERROR_TYPE_DEBUG, "! destroying connection!\n");
631 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
634 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
635 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
636 c->maintenance_q = NULL;
637 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
638 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
639 schedule_next_keepalive (c, fwd);
642 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
643 if (GNUNET_YES == sent)
645 GNUNET_assert (NULL != q);
646 fc->last_pid_sent = pid; // FIXME
647 GCC_send_ack (c, fwd, GNUNET_NO);
648 connection_reset_timeout (c, fwd);
651 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
652 if (GNUNET_NO == forced)
655 LOG (GNUNET_ERROR_TYPE_DEBUG,
656 "! accounting pid %u\n",
661 LOG (GNUNET_ERROR_TYPE_DEBUG,
662 "! forced, Q_N not accounting pid %u\n",
667 case GNUNET_MESSAGE_TYPE_CADET_KX:
668 if (GNUNET_YES == sent)
669 connection_reset_timeout (c, fwd);
672 case GNUNET_MESSAGE_TYPE_CADET_POLL:
676 case GNUNET_MESSAGE_TYPE_CADET_ACK:
683 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
686 return GNUNET_NO; /* Only endpoints are interested in timing. */
689 usecsperbyte = ((double) wait.rel_value_us) / size;
690 if (p->size == AVG_MSGS)
692 /* Array is full. Substract oldest value, add new one and store. */
693 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
694 p->usecsperbyte[p->idx] = usecsperbyte;
695 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
699 /* Array not yet full. Add current value to avg and store. */
700 p->usecsperbyte[p->idx] = usecsperbyte;
702 p->avg += p->usecsperbyte[p->idx];
706 p->idx = (p->idx + 1) % AVG_MSGS;
712 * Get the previous hop in a connection
714 * @param c Connection.
716 * @return Previous peer in the connection.
718 static struct CadetPeer *
719 get_prev_hop (const struct CadetConnection *c)
726 LOG (GNUNET_ERROR_TYPE_DEBUG, " get prev hop %s [%u/%u]\n",
727 GCC_2s (c), c->own_pos, c->path->length);
728 if (0 == c->own_pos || c->path->length < 2)
729 id = c->path->peers[0];
731 id = c->path->peers[c->own_pos - 1];
733 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
734 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
736 return GCP_get_short (id);
741 * Get the next hop in a connection
743 * @param c Connection.
745 * @return Next peer in the connection.
747 static struct CadetPeer *
748 get_next_hop (const struct CadetConnection *c)
755 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
756 GCC_2s (c), c->own_pos, c->path->length);
757 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
758 id = c->path->peers[c->path->length - 1];
760 id = c->path->peers[c->own_pos + 1];
762 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
763 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
765 return GCP_get_short (id);
770 * Get the hop in a connection.
772 * @param c Connection.
773 * @param fwd Next in the FWD direction?
775 * @return Next peer in the connection.
777 static struct CadetPeer *
778 get_hop (struct CadetConnection *c, int fwd)
781 return get_next_hop (c);
782 return get_prev_hop (c);
787 * Get a bit mask for a message received out-of-order.
789 * @param last_pid_recv Last PID we received prior to the out-of-order.
790 * @param ooo_pid PID of the out-of-order message.
793 get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
795 return 1 << (last_pid_recv - ooo_pid);
800 * Check is an out-of-order message is ok:
801 * - at most 31 messages behind.
804 * @param last_pid_recv Last in-order PID received.
807 is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
811 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
814 mask = get_recv_bitmask (last_pid_recv, ooo_pid);
815 if (0 != (ooo_bitmap & mask))
823 * Is traffic coming from this sender 'FWD' traffic?
825 * @param c Connection to check.
826 * @param sender Peer identity of neighbor.
828 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
829 * the traffic is 'FWD'.
830 * #GNUNET_NO for BCK.
831 * #GNUNET_SYSERR for errors.
834 is_fwd (const struct CadetConnection *c,
835 const struct GNUNET_PeerIdentity *sender)
839 id = GNUNET_PEER_search (sender);
840 if (GCP_get_short_id (get_prev_hop (c)) == id)
843 if (GCP_get_short_id (get_next_hop (c)) == id)
847 return GNUNET_SYSERR;
852 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
853 * or a first CONNECTION_ACK directed to us.
855 * @param connection Connection to confirm.
856 * @param fwd Should we send it FWD? (root->dest)
857 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
860 send_connection_ack (struct CadetConnection *connection, int fwd)
862 struct CadetTunnel *t;
865 LOG (GNUNET_ERROR_TYPE_INFO, "---> {%14s ACK} on connection %s\n",
866 GC_f2s (!fwd), GCC_2s (connection));
867 GCP_queue_add (get_hop (connection, fwd), NULL,
868 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0, 0,
869 sizeof (struct GNUNET_CADET_ConnectionACK),
870 connection, fwd, &conn_message_sent, NULL);
871 connection->pending_messages++;
872 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
873 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
874 if (CADET_CONNECTION_READY != connection->state)
875 connection_change_state (connection, CADET_CONNECTION_SENT);
880 * Send a notification that a connection is broken.
882 * @param c Connection that is broken.
883 * @param id1 Peer that has disconnected.
884 * @param id2 Peer that has disconnected.
885 * @param fwd Direction towards which to send it.
888 send_broken (struct CadetConnection *c,
889 const struct GNUNET_PeerIdentity *id1,
890 const struct GNUNET_PeerIdentity *id2,
893 struct GNUNET_CADET_ConnectionBroken msg;
895 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
896 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
900 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c, fwd,
901 GNUNET_YES, NULL, NULL));
906 * Send a notification that a connection is broken, when a connection
907 * isn't even known to the local peer or soon to be destroyed.
909 * @param connection_id Connection ID.
910 * @param id1 Peer that has disconnected, probably local peer.
911 * @param id2 Peer that has disconnected can be NULL if unknown.
912 * @param peer Peer to notify (neighbor who sent the connection).
915 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
916 const struct GNUNET_PeerIdentity *id1,
917 const struct GNUNET_PeerIdentity *id2,
918 const struct GNUNET_PeerIdentity *peer_id)
920 struct GNUNET_CADET_ConnectionBroken *msg;
921 struct CadetPeer *neighbor;
923 LOG (GNUNET_ERROR_TYPE_INFO, "---> BROKEN on unknown connection %s\n",
924 GNUNET_h2s (GC_h2hc (connection_id)));
926 msg = GNUNET_new (struct GNUNET_CADET_ConnectionBroken);
927 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
928 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
929 msg->cid = *connection_id;
934 memset (&msg->peer2, 0, sizeof (msg->peer2));
935 neighbor = GCP_get (peer_id);
936 GCP_queue_add (neighbor, msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
937 0, 2, sizeof (struct GNUNET_CADET_ConnectionBroken),
938 NULL, GNUNET_SYSERR, /* connection, fwd */
939 NULL, NULL); /* continuation */
944 * Send keepalive packets for a connection.
946 * @param c Connection to keep alive..
947 * @param fwd Is this a FWD keepalive? (owner -> dest).
950 send_connection_keepalive (struct CadetConnection *c, int fwd)
952 struct GNUNET_MessageHeader msg;
953 struct CadetFlowControl *fc;
955 LOG (GNUNET_ERROR_TYPE_INFO, "keepalive %s for connection %s\n",
956 GC_f2s (fwd), GCC_2s (c));
958 fc = fwd ? &c->fwd_fc : &c->bck_fc;
961 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
965 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
967 GNUNET_assert (NULL != c->t);
968 msg.size = htons (sizeof (msg));
969 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
971 GNUNET_assert (NULL ==
972 GCT_send_prebuilt_message (&msg, c->t, c,
973 GNUNET_NO, NULL, NULL));
978 * Send CONNECTION_{CREATE/ACK} packets for a connection.
980 * @param c Connection for which to send the message.
981 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
984 connection_recreate (struct CadetConnection *c, int fwd)
986 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
990 send_connection_ack (c, GNUNET_NO);
995 * Generic connection timer management.
996 * Depending on the role of the peer in the connection will send the
997 * appropriate message (build or keepalive)
999 * @param c Conncetion to maintain.
1000 * @param fwd Is FWD?
1003 connection_maintain (struct CadetConnection *c, int fwd)
1005 if (GNUNET_NO != c->destroy)
1007 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1014 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1018 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1020 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1022 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1023 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1024 schedule_next_keepalive (c, fwd);
1029 case CADET_CONNECTION_NEW:
1032 case CADET_CONNECTION_SENT:
1033 connection_recreate (c, fwd);
1035 case CADET_CONNECTION_READY:
1036 send_connection_keepalive (c, fwd);
1046 * Keep the connection alive.
1048 * @param c Connection to keep alive.
1049 * @param fwd Direction.
1050 * @param shutdown Are we shutting down? (Don't send traffic)
1051 * Non-zero value for true, not necessarily GNUNET_YES.
1054 connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
1056 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s keepalive for %s\n",
1057 GC_f2s (fwd), GCC_2s (c));
1060 c->fwd_maintenance_task = NULL;
1062 c->bck_maintenance_task = NULL;
1064 if (GNUNET_NO != shutdown)
1067 connection_maintain (c, fwd);
1069 /* Next execution will be scheduled by message_sent or _maintain*/
1074 * Keep the connection alive in the FWD direction.
1076 * @param cls Closure (connection to keepalive).
1077 * @param tc TaskContext.
1080 connection_fwd_keepalive (void *cls,
1081 const struct GNUNET_SCHEDULER_TaskContext *tc)
1083 connection_keepalive ((struct CadetConnection *) cls,
1085 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1090 * Keep the connection alive in the BCK direction.
1092 * @param cls Closure (connection to keepalive).
1093 * @param tc TaskContext.
1096 connection_bck_keepalive (void *cls,
1097 const struct GNUNET_SCHEDULER_TaskContext *tc)
1099 connection_keepalive ((struct CadetConnection *) cls,
1101 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1106 * Schedule next keepalive task, taking in consideration
1107 * the connection state and number of retries.
1109 * If the peer is not the origin, do nothing.
1111 * @param c Connection for which to schedule the next keepalive.
1112 * @param fwd Direction for the next keepalive.
1115 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1117 struct GNUNET_TIME_Relative delay;
1118 struct GNUNET_SCHEDULER_Task * *task_id;
1119 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1121 if (GNUNET_NO == GCC_is_origin (c, fwd))
1124 /* Calculate delay to use, depending on the state of the connection */
1125 if (CADET_CONNECTION_READY == c->state)
1127 delay = refresh_connection_time;
1131 if (1 > c->create_retry)
1132 c->create_retry = 1;
1133 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1135 if (c->create_retry < 64)
1136 c->create_retry *= 2;
1139 /* Select direction-dependent parameters */
1140 if (GNUNET_YES == fwd)
1142 task_id = &c->fwd_maintenance_task;
1143 keepalive_task = &connection_fwd_keepalive;
1147 task_id = &c->bck_maintenance_task;
1148 keepalive_task = &connection_bck_keepalive;
1151 /* Check that no one scheduled it before us */
1152 if (NULL != *task_id)
1154 /* No need for a _break. It can happen for instance when sending a SYNACK
1155 * for a duplicate SYN: the first SYNACK scheduled the task. */
1156 GNUNET_SCHEDULER_cancel (*task_id);
1159 /* Schedule the task */
1160 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1161 LOG (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1162 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1167 * @brief Re-initiate traffic on this connection if necessary.
1169 * Check if there is traffic queued towards this peer
1170 * and the core transmit handle is NULL (traffic was stalled).
1171 * If so, call core tmt rdy.
1173 * @param c Connection on which initiate traffic.
1174 * @param fwd Is this about fwd traffic?
1177 connection_unlock_queue (struct CadetConnection *c, int fwd)
1179 struct CadetPeer *peer;
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_unlock_queue %s on %s\n",
1182 GC_f2s (fwd), GCC_2s (c));
1184 if (GCC_is_terminal (c, fwd))
1186 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal, can unlock!\n");
1190 peer = get_hop (c, fwd);
1191 GCP_queue_unlock (peer, c);
1196 * Cancel all transmissions that belong to a certain connection.
1198 * If the connection is scheduled for destruction and no more messages are left,
1199 * the connection will be destroyed by the continuation call.
1201 * @param c Connection which to cancel. Might be destroyed during this call.
1202 * @param fwd Cancel fwd traffic?
1205 connection_cancel_queues (struct CadetConnection *c, int fwd)
1207 struct CadetFlowControl *fc;
1208 struct CadetPeer *peer;
1210 LOG (GNUNET_ERROR_TYPE_DEBUG,
1211 " *** Cancel %s queues for connection %s\n",
1212 GC_f2s (fwd), GCC_2s (c));
1219 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1220 if (NULL != fc->poll_task)
1222 GNUNET_SCHEDULER_cancel (fc->poll_task);
1223 fc->poll_task = NULL;
1224 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Cancel POLL in ccq for fc %p\n", fc);
1226 peer = get_hop (c, fwd);
1227 GCP_queue_cancel (peer, c);
1232 * Function called if a connection has been stalled for a while,
1233 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1235 * @param cls Closure (poll ctx).
1236 * @param tc TaskContext.
1239 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1243 * Callback called when a queued POLL message is sent.
1245 * @param cls Closure (FC).
1246 * @param c Connection this message was on.
1247 * @param q Queue handler this call invalidates.
1248 * @param type Type of message sent.
1249 * @param fwd Was this a FWD going message?
1250 * @param size Size of the message.
1253 poll_sent (void *cls,
1254 struct CadetConnection *c,
1255 struct CadetConnectionQueue *q,
1256 uint16_t type, int fwd, size_t size)
1258 struct CadetFlowControl *fc = cls;
1260 if (2 == c->destroy)
1262 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL canceled on shutdown\n");
1265 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL sent for , scheduling new one!\n");
1266 fc->poll_msg = NULL;
1267 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1268 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1269 &connection_poll, fc);
1270 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1275 * Function called if a connection has been stalled for a while,
1276 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1278 * @param cls Closure (poll ctx).
1279 * @param tc TaskContext.
1282 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1284 struct CadetFlowControl *fc = cls;
1285 struct GNUNET_CADET_Poll msg;
1286 struct CadetConnection *c;
1288 fc->poll_task = NULL;
1289 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1295 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling connection %s %s\n",
1296 GCC_2s (c), fc == &c->fwd_fc ? "FWD" : "BCK");
1298 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1299 msg.header.size = htons (sizeof (msg));
1300 msg.pid = htonl (fc->last_pid_sent);
1301 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
1303 GCC_send_prebuilt_message (&msg.header, 0, fc->last_pid_sent, c,
1304 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1305 GNUNET_assert (NULL != fc->poll_msg);
1310 * Resend all queued messages for a connection on other connections of the
1311 * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
1313 * @param c Connection whose messages to resend.
1314 * @param fwd Resend fwd messages?
1317 resend_messages_and_destroy (struct CadetConnection *c, int fwd)
1319 struct GNUNET_MessageHeader *out_msg;
1320 struct CadetTunnel *t = c->t;
1321 struct CadetPeer *neighbor;
1322 unsigned int pending;
1325 c->state = CADET_CONNECTION_DESTROYED;
1326 c->destroy = GNUNET_YES;
1328 destroyed = GNUNET_NO;
1329 neighbor = get_hop (c, fwd);
1330 pending = c->pending_messages;
1332 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1335 GCT_resend_message (out_msg, t);
1336 GNUNET_free (out_msg);
1339 /* All pending messages should have been popped,
1340 * and the connection destroyed by the continuation.
1342 if (GNUNET_YES != destroyed)
1347 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1348 if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1356 * Timeout function due to lack of keepalive/traffic from the owner.
1357 * Destroys connection if called.
1359 * @param cls Closure (connection to destroy).
1360 * @param tc TaskContext.
1363 connection_fwd_timeout (void *cls,
1364 const struct GNUNET_SCHEDULER_TaskContext *tc)
1366 struct CadetConnection *c = cls;
1368 c->fwd_maintenance_task = NULL;
1369 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1372 LOG (GNUNET_ERROR_TYPE_INFO, "Connection %s FWD timed out. Destroying.\n",
1374 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1376 if (GCC_is_origin (c, GNUNET_YES)) /* If local, leave. */
1382 /* If dest, salvage queued traffic. */
1383 if (GCC_is_origin (c, GNUNET_NO) && 0 < c->bck_fc.queue_n)
1385 send_broken_unknown (&c->id, &my_full_id, NULL,
1386 GCP_get_id( get_prev_hop (c)));
1387 resend_messages_and_destroy (c, GNUNET_NO);
1396 * Timeout function due to lack of keepalive/traffic from the destination.
1397 * Destroys connection if called.
1399 * FIXME refactor and merge with connection_fwd_timeout.
1401 * @param cls Closure (connection to destroy).
1402 * @param tc TaskContext
1405 connection_bck_timeout (void *cls,
1406 const struct GNUNET_SCHEDULER_TaskContext *tc)
1408 struct CadetConnection *c = cls;
1410 c->bck_maintenance_task = NULL;
1411 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1414 LOG (GNUNET_ERROR_TYPE_INFO, "Connection %s BCK timed out. Destroying.\n",
1417 if (GCC_is_origin (c, GNUNET_NO)) /* If local, leave. */
1423 /* If dest, salvage queued traffic. */
1424 if (GCC_is_origin (c, GNUNET_YES) && 0 < c->fwd_fc.queue_n)
1426 send_broken_unknown (&c->id, &my_full_id, NULL,
1427 GCP_get_id( get_next_hop (c)));
1428 resend_messages_and_destroy (c, GNUNET_YES);
1437 * Resets the connection timeout task, some other message has done the
1439 * - For the first peer on the direction this means to send
1440 * a keepalive or a path confirmation message (either create or ACK).
1441 * - For all other peers, this means to destroy the connection,
1442 * due to lack of activity.
1443 * Starts the timeout if no timeout was running (connection just created).
1445 * @param c Connection whose timeout to reset.
1446 * @param fwd Is this forward?
1448 * TODO use heap to improve efficiency of scheduler.
1451 connection_reset_timeout (struct CadetConnection *c, int fwd)
1453 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1455 if (GCC_is_origin (c, fwd)) /* Startpoint */
1457 schedule_next_keepalive (c, fwd);
1459 else /* Relay, endpoint. */
1461 struct GNUNET_TIME_Relative delay;
1462 struct GNUNET_SCHEDULER_Task * *ti;
1463 GNUNET_SCHEDULER_TaskCallback f;
1465 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1468 GNUNET_SCHEDULER_cancel (*ti);
1469 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1470 LOG (GNUNET_ERROR_TYPE_DEBUG, " timing out in %s\n",
1471 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1472 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1473 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1479 * Add the connection to the list of both neighbors.
1481 * @param c Connection.
1483 * @return #GNUNET_OK if everything went fine
1484 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1487 register_neighbors (struct CadetConnection *c)
1489 struct CadetPeer *next_peer;
1490 struct CadetPeer *prev_peer;
1492 next_peer = get_next_hop (c);
1493 prev_peer = get_prev_hop (c);
1495 LOG (GNUNET_ERROR_TYPE_DEBUG, "register neighbors for connection %s\n",
1497 path_debug (c->path);
1498 LOG (GNUNET_ERROR_TYPE_DEBUG, "own pos %u\n", c->own_pos);
1499 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to next peer %p\n",
1500 GCC_2s (c), next_peer);
1501 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n", next_peer, GCP_2s (next_peer));
1502 LOG (GNUNET_ERROR_TYPE_DEBUG, "putting connection %s to prev peer %p\n",
1503 GCC_2s (c), prev_peer);
1504 LOG (GNUNET_ERROR_TYPE_DEBUG, "prev peer %p %s\n", prev_peer, GCP_2s (prev_peer));
1506 if (GNUNET_NO == GCP_is_neighbor (next_peer)
1507 || GNUNET_NO == GCP_is_neighbor (prev_peer))
1509 if (GCC_is_origin (c, GNUNET_YES))
1510 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1511 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1513 LOG (GNUNET_ERROR_TYPE_DEBUG, " register neighbors failed\n");
1514 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev: %s, neighbor?: %d\n",
1515 GCP_2s (prev_peer), GCP_is_neighbor (prev_peer));
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, " next: %s, neighbor?: %d\n",
1517 GCP_2s (next_peer), GCP_is_neighbor (next_peer));
1518 return GNUNET_SYSERR;
1521 GCP_add_connection (next_peer, c);
1522 GCP_add_connection (prev_peer, c);
1529 * Remove the connection from the list of both neighbors.
1531 * @param c Connection.
1534 unregister_neighbors (struct CadetConnection *c)
1536 struct CadetPeer *peer;
1538 peer = get_next_hop (c);
1539 if (GNUNET_OK != GCP_remove_connection (peer, c))
1541 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1542 if (NULL != c->t) GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1545 peer = get_prev_hop (c);
1546 if (GNUNET_OK != GCP_remove_connection (peer, c))
1548 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate: %u\n", c->state);
1549 if (NULL != c->t) GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1555 * Bind the connection to the peer and the tunnel to that peer.
1557 * If the peer has no tunnel, create one. Update tunnel and connection
1558 * data structres to reflect new status.
1560 * @param c Connection.
1564 add_to_peer (struct CadetConnection *c, struct CadetPeer *peer)
1566 GCP_add_tunnel (peer);
1567 c->t = GCP_get_tunnel (peer);
1568 GCT_add_connection (c->t, c);
1574 * Iterator to compare each connection's path with the path of a new connection.
1576 * If the connection conincides, the c member of path is set to the connection
1577 * and the destroy flag of the connection is set.
1579 * @param cls Closure (new path).
1580 * @param c Connection in the tunnel to check.
1583 check_path (void *cls, struct CadetConnection *c)
1585 struct CadetConnection *new_conn = cls;
1586 struct CadetPeerPath *path = new_conn->path;
1588 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1589 GCC_2s (c), c, c->path->length);
1592 && c->destroy == GNUNET_NO
1593 && c->state != CADET_CONNECTION_BROKEN
1594 && c->state != CADET_CONNECTION_DESTROYED
1595 && path_equivalent (path, c->path))
1597 new_conn->destroy = GNUNET_YES;
1598 new_conn->path->c = c;
1599 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1604 * Finds out if this path is already being used by and existing connection.
1606 * Checks the tunnel towards the first peer in the path to see if it contains
1607 * any connection with the same path.
1609 * If the existing connection is ready, it is kept.
1610 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1611 * the peer will eventually reject our attempt).
1613 * @param path Path to check.
1615 * @return GNUNET_YES if the tunnel has a connection with the same path,
1616 * GNUNET_NO otherwise.
1619 does_connection_exist (struct CadetConnection *conn)
1621 struct CadetPeer *p;
1622 struct CadetTunnel *t;
1623 struct CadetConnection *c;
1625 p = GCP_get_short (conn->path->peers[0]);
1626 t = GCP_get_tunnel (p);
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1632 GCT_iterate_connections (t, &check_path, conn);
1634 if (GNUNET_YES == conn->destroy)
1637 conn->destroy = GNUNET_NO;
1638 conn->path->c = conn;
1639 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1640 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1641 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1642 if (CADET_CONNECTION_READY == c->state)
1644 /* The other peer confirmed this connection,
1645 * they should not try to duplicate it. */
1646 GNUNET_break_op (0);
1650 if (GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GCP_get_id (p)) > 0)
1652 struct CadetPeer *neighbor;
1654 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate allowed (killing old)\n");
1655 if (GCC_is_origin (c, GNUNET_YES))
1656 neighbor = get_next_hop (c);
1658 neighbor = get_prev_hop (c);
1659 send_broken_unknown (&c->id, &my_full_id, NULL,
1660 GCP_get_id (neighbor));
1669 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1676 * Log receipt of message on stderr (INFO level).
1678 * @param message Message received.
1679 * @param peer Peer who sent the message.
1680 * @param hash Connection ID.
1683 log_message (const struct GNUNET_MessageHeader *message,
1684 const struct GNUNET_PeerIdentity *peer,
1685 const struct GNUNET_CADET_Hash *hash)
1689 size = ntohs (message->size);
1690 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1691 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1692 LOG (GNUNET_ERROR_TYPE_INFO, "<-- %s on connection %s from %s, %6u bytes\n",
1693 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (hash)),
1694 GNUNET_i2s (peer), (unsigned int) size);
1697 /******************************************************************************/
1698 /******************************** API ***********************************/
1699 /******************************************************************************/
1702 * Core handler for connection creation.
1704 * @param cls Closure (unused).
1705 * @param peer Sender (neighbor).
1706 * @param message Message.
1708 * @return GNUNET_OK to keep the connection open,
1709 * GNUNET_SYSERR to close it (signal serious error)
1712 GCC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1713 const struct GNUNET_MessageHeader *message)
1715 struct GNUNET_CADET_ConnectionCreate *msg;
1716 struct GNUNET_PeerIdentity *id;
1717 struct GNUNET_CADET_Hash *cid;
1718 struct CadetPeerPath *path;
1719 struct CadetPeer *dest_peer;
1720 struct CadetPeer *orig_peer;
1721 struct CadetConnection *c;
1722 unsigned int own_pos;
1726 size = ntohs (message->size);
1727 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1729 GNUNET_break_op (0);
1733 /* Calculate hops */
1734 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1735 if (size % sizeof (struct GNUNET_PeerIdentity))
1737 GNUNET_break_op (0);
1740 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1742 GNUNET_break_op (0);
1745 size /= sizeof (struct GNUNET_PeerIdentity);
1748 GNUNET_break_op (0);
1751 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1753 /* Get parameters */
1754 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
1756 log_message (message, peer, cid);
1757 id = (struct GNUNET_PeerIdentity *) &msg[1];
1758 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1760 /* Create connection */
1761 c = connection_get (cid);
1764 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1765 size, myid, &own_pos);
1771 GNUNET_break_op (0);
1772 path_destroy (path);
1776 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1777 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1778 c = GCC_new (cid, NULL, path, own_pos);
1781 if (path->length - 1 == own_pos)
1783 /* If we are destination, why did the creation fail? */
1785 path_destroy (path);
1788 send_broken_unknown (cid, &my_full_id,
1789 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1791 path_destroy (path);
1794 GCP_add_path_to_all (path, GNUNET_NO);
1795 connection_reset_timeout (c, GNUNET_YES);
1799 path = path_duplicate (c->path);
1801 if (CADET_CONNECTION_NEW == c->state)
1802 connection_change_state (c, CADET_CONNECTION_SENT);
1804 /* Remember peers */
1805 dest_peer = GCP_get (&id[size - 1]);
1806 orig_peer = GCP_get (&id[0]);
1808 /* Is it a connection to us? */
1809 if (c->own_pos == path->length - 1)
1811 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1812 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1814 add_to_peer (c, orig_peer);
1815 if (GNUNET_YES == does_connection_exist (c))
1817 path_destroy (path);
1819 // FIXME use explicit duplicate
1820 send_broken_unknown (cid, &my_full_id, NULL, peer);
1825 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
1826 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
1828 send_connection_ack (c, GNUNET_NO);
1829 if (CADET_CONNECTION_SENT == c->state)
1830 connection_change_state (c, CADET_CONNECTION_ACK);
1834 /* It's for somebody else! Retransmit. */
1835 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1836 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1837 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1838 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
1839 GNUNET_YES, GNUNET_YES,
1842 path_destroy (path);
1848 * Core handler for path confirmations.
1850 * @param cls closure
1851 * @param message message
1852 * @param peer peer identity this notification is about
1854 * @return GNUNET_OK to keep the connection open,
1855 * GNUNET_SYSERR to close it (signal serious error)
1858 GCC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1859 const struct GNUNET_MessageHeader *message)
1861 struct GNUNET_CADET_ConnectionACK *msg;
1862 struct CadetConnection *c;
1863 struct CadetPeerPath *p;
1864 struct CadetPeer *pi;
1865 enum CadetConnectionState oldstate;
1868 msg = (struct GNUNET_CADET_ConnectionACK *) message;
1869 log_message (message, peer, &msg->cid);
1870 c = connection_get (&msg->cid);
1873 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1875 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1876 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
1880 if (GNUNET_NO != c->destroy)
1882 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1886 oldstate = c->state;
1887 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1888 pi = GCP_get (peer);
1889 if (get_next_hop (c) == pi)
1891 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1893 if (CADET_CONNECTION_SENT == oldstate)
1894 connection_change_state (c, CADET_CONNECTION_ACK);
1896 else if (get_prev_hop (c) == pi)
1898 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
1900 connection_change_state (c, CADET_CONNECTION_READY);
1904 GNUNET_break_op (0);
1908 connection_reset_timeout (c, fwd);
1910 /* Add path to peers? */
1914 GCP_add_path_to_all (p, GNUNET_YES);
1921 /* Message for us as creator? */
1922 if (GCC_is_origin (c, GNUNET_YES))
1924 if (GNUNET_NO != fwd)
1926 GNUNET_break_op (0);
1929 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1931 /* If just created, cancel the short timeout and start a long one */
1932 if (CADET_CONNECTION_SENT == oldstate)
1933 connection_reset_timeout (c, GNUNET_YES);
1935 /* Change connection state */
1936 connection_change_state (c, CADET_CONNECTION_READY);
1937 send_connection_ack (c, GNUNET_YES);
1939 /* Change tunnel state, trigger KX */
1940 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
1941 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
1946 /* Message for us as destination? */
1947 if (GCC_is_terminal (c, GNUNET_YES))
1949 if (GNUNET_YES != fwd)
1951 GNUNET_break_op (0);
1954 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1956 /* If just created, cancel the short timeout and start a long one */
1957 if (CADET_CONNECTION_ACK == oldstate)
1958 connection_reset_timeout (c, GNUNET_NO);
1960 /* Change tunnel state */
1961 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
1962 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
1967 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1968 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
1969 GNUNET_YES, NULL, NULL));
1975 * Core handler for notifications of broken connections.
1977 * @param cls Closure (unused).
1978 * @param id Peer identity of sending neighbor.
1979 * @param message Message.
1981 * @return GNUNET_OK to keep the connection open,
1982 * GNUNET_SYSERR to close it (signal serious error)
1985 GCC_handle_broken (void* cls,
1986 const struct GNUNET_PeerIdentity* id,
1987 const struct GNUNET_MessageHeader* message)
1989 struct GNUNET_CADET_ConnectionBroken *msg;
1990 struct CadetConnection *c;
1991 struct CadetTunnel *t;
1995 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
1996 log_message (message, id, &msg->cid);
1997 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
1998 GNUNET_i2s (&msg->peer1));
1999 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2000 GNUNET_i2s (&msg->peer2));
2001 c = connection_get (&msg->cid);
2004 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2010 fwd = is_fwd (c, id);
2011 c->destroy = GNUNET_YES;
2012 if (GCC_is_terminal (c, fwd))
2014 struct CadetPeer *endpoint;
2018 /* A terminal connection should not have 't' set to NULL. */
2020 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2023 endpoint = GCP_get_short (c->path->peers[c->path->length - 1]);
2024 path_invalidate (c->path);
2025 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2027 c->state = CADET_CONNECTION_BROKEN;
2028 GCT_remove_connection (t, c);
2031 pending = c->pending_messages;
2033 resend_messages_and_destroy (c, !fwd);
2039 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2040 GNUNET_YES, NULL, NULL));
2041 connection_cancel_queues (c, !fwd);
2049 * Core handler for tunnel destruction
2051 * @param cls Closure (unused).
2052 * @param peer Peer identity of sending neighbor.
2053 * @param message Message.
2055 * @return GNUNET_OK to keep the connection open,
2056 * GNUNET_SYSERR to close it (signal serious error)
2059 GCC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2060 const struct GNUNET_MessageHeader *message)
2062 struct GNUNET_CADET_ConnectionDestroy *msg;
2063 struct CadetConnection *c;
2066 msg = (struct GNUNET_CADET_ConnectionDestroy *) message;
2067 log_message (message, peer, &msg->cid);
2068 c = connection_get (&msg->cid);
2071 /* Probably already got the message from another path,
2072 * destroyed the tunnel and retransmitted to children.
2075 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2077 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection unknown: already destroyed?\n");
2080 fwd = is_fwd (c, peer);
2081 if (GNUNET_SYSERR == fwd)
2083 GNUNET_break_op (0); /* FIXME */
2086 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2087 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2088 GNUNET_YES, NULL, NULL));
2089 else if (0 == c->pending_messages)
2091 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
2095 c->destroy = GNUNET_YES;
2096 c->state = CADET_CONNECTION_DESTROYED;
2099 GCT_remove_connection (c->t, c);
2108 * Check the message against internal state and test if it goes FWD or BCK.
2110 * Updates the PID, state and timeout values for the connection.
2112 * @param message Message to check. It must belong to an existing connection.
2113 * @param minimum_size The message cannot be smaller than this value.
2114 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2115 * @param c Connection this message should belong. If NULL, check fails.
2116 * @param neighbor Neighbor that sent the message.
2119 check_message (const struct GNUNET_MessageHeader *message,
2120 size_t minimum_size,
2121 const struct GNUNET_CADET_Hash* cid,
2122 struct CadetConnection *c,
2123 const struct GNUNET_PeerIdentity *neighbor,
2126 GNUNET_PEER_Id neighbor_id;
2127 struct CadetFlowControl *fc;
2128 struct CadetPeer *hop;
2132 if (ntohs (message->size) < minimum_size)
2134 GNUNET_break_op (0);
2135 return GNUNET_SYSERR;
2138 /* Check connection */
2141 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
2142 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s on unknown connection %s\n",
2143 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (cid)));
2144 send_broken_unknown (cid, &my_full_id, NULL, neighbor);
2145 return GNUNET_SYSERR;
2148 /* Check if origin is as expected */
2149 neighbor_id = GNUNET_PEER_search (neighbor);
2150 hop = get_prev_hop (c);
2151 if (neighbor_id == GCP_get_short_id (hop))
2157 hop = get_next_hop (c);
2158 if (neighbor_id == GCP_get_short_id (hop))
2164 /* Unexpected peer sending traffic on a connection. */
2165 GNUNET_break_op (0);
2166 return GNUNET_SYSERR;
2170 /* Check PID for payload messages */
2171 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == ntohs (message->type))
2173 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2174 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2175 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2176 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2178 GNUNET_break_op (0);
2179 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2180 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2181 pid, fc->last_pid_recv, fc->last_ack_sent);
2182 return GNUNET_SYSERR;
2184 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2188 delta = pid - fc->last_pid_recv;
2189 fc->last_pid_recv = pid;
2190 fc->recv_bitmap <<= delta;
2191 fc->recv_bitmap |= 1;
2195 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2196 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2198 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2199 pid, fc->last_pid_recv - 31);
2200 return GNUNET_SYSERR;
2202 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2206 /* Count as connection confirmation. */
2207 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2209 connection_change_state (c, CADET_CONNECTION_READY);
2212 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2213 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2216 connection_reset_timeout (c, fwd);
2223 * Generic handler for cadet network encrypted traffic.
2225 * @param peer Peer identity this notification is about.
2226 * @param msg Encrypted message.
2228 * @return GNUNET_OK to keep the connection open,
2229 * GNUNET_SYSERR to close it (signal serious error)
2232 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
2233 const struct GNUNET_MessageHeader *message)
2235 const struct GNUNET_CADET_Encrypted *msg;
2236 const struct GNUNET_CADET_Hash* cid;
2237 struct CadetConnection *c;
2238 size_t expected_size;
2243 msg = (struct GNUNET_CADET_Encrypted *) message;
2245 log_message (message, peer, cid);
2247 expected_size = sizeof (struct GNUNET_CADET_Encrypted)
2248 + sizeof (struct GNUNET_MessageHeader);
2249 c = connection_get (cid);
2250 pid = ntohl (msg->pid);
2251 fwd = check_message (message, expected_size, cid, c, peer, pid);
2253 /* If something went wrong, discard message. */
2254 if (GNUNET_SYSERR == fwd)
2257 /* Is this message for us? */
2258 if (GCC_is_terminal (c, fwd))
2260 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2264 GNUNET_break (GNUNET_NO != c->destroy);
2267 GCT_handle_encrypted (c->t, message);
2268 GCC_send_ack (c, fwd, GNUNET_NO);
2272 /* Message not for us: forward to next hop */
2273 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2274 ttl = ntohl (msg->ttl);
2275 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2278 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2279 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2280 GCC_send_ack (c, fwd, GNUNET_NO);
2284 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2285 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2286 GNUNET_NO, NULL, NULL));
2292 * Generic handler for cadet network encrypted traffic.
2294 * @param peer Peer identity this notification is about.
2295 * @param msg Encrypted message.
2297 * @return GNUNET_OK to keep the connection open,
2298 * GNUNET_SYSERR to close it (signal serious error)
2301 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2302 const struct GNUNET_CADET_KX *msg)
2304 const struct GNUNET_CADET_Hash* cid;
2305 struct CadetConnection *c;
2306 size_t expected_size;
2310 log_message (&msg->header, peer, cid);
2312 expected_size = sizeof (struct GNUNET_CADET_KX)
2313 + sizeof (struct GNUNET_MessageHeader);
2314 c = connection_get (cid);
2315 fwd = check_message (&msg->header, expected_size, cid, c, peer, 0);
2317 /* If something went wrong, discard message. */
2318 if (GNUNET_SYSERR == fwd)
2321 /* Is this message for us? */
2322 if (GCC_is_terminal (c, fwd))
2324 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2325 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2331 GCT_handle_kx (c->t, &msg[1].header);
2335 /* Message not for us: forward to next hop */
2336 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2337 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2338 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2339 GNUNET_NO, NULL, NULL));
2346 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2348 * @param cls Closure (unused).
2349 * @param message Message received.
2350 * @param peer Peer who sent the message.
2352 * @return GNUNET_OK to keep the connection open,
2353 * GNUNET_SYSERR to close it (signal serious error)
2356 GCC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2357 const struct GNUNET_MessageHeader *message)
2359 return handle_cadet_kx (peer, (struct GNUNET_CADET_KX *) message);
2364 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2366 * @param cls Closure (unused).
2367 * @param message Message received.
2368 * @param peer Peer who sent the message.
2370 * @return GNUNET_OK to keep the connection open,
2371 * GNUNET_SYSERR to close it (signal serious error)
2374 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2375 const struct GNUNET_MessageHeader *message)
2377 return handle_cadet_encrypted (peer, message);
2382 * Core handler for cadet network traffic point-to-point acks.
2384 * @param cls closure
2385 * @param message message
2386 * @param peer peer identity this notification is about
2388 * @return GNUNET_OK to keep the connection open,
2389 * GNUNET_SYSERR to close it (signal serious error)
2392 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2393 const struct GNUNET_MessageHeader *message)
2395 struct GNUNET_CADET_ACK *msg;
2396 struct CadetConnection *c;
2397 struct CadetFlowControl *fc;
2402 msg = (struct GNUNET_CADET_ACK *) message;
2403 log_message (message, peer, &msg->cid);
2404 c = connection_get (&msg->cid);
2407 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2409 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2413 /* Is this a forward or backward ACK? */
2414 id = GNUNET_PEER_search (peer);
2415 if (GCP_get_short_id (get_next_hop (c)) == id)
2420 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2427 GNUNET_break_op (0);
2431 ack = ntohl (msg->ack);
2432 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2433 GC_f2s (fwd), ack, fc->last_ack_recv);
2434 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2435 fc->last_ack_recv = ack;
2437 /* Cancel polling if the ACK is big enough. */
2438 if (NULL != fc->poll_task &&
2439 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2441 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2442 GNUNET_SCHEDULER_cancel (fc->poll_task);
2443 fc->poll_task = NULL;
2444 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2447 connection_unlock_queue (c, fwd);
2454 * Core handler for cadet network traffic point-to-point ack polls.
2456 * @param cls closure
2457 * @param message message
2458 * @param peer peer identity this notification is about
2460 * @return GNUNET_OK to keep the connection open,
2461 * GNUNET_SYSERR to close it (signal serious error)
2464 GCC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2465 const struct GNUNET_MessageHeader *message)
2467 struct GNUNET_CADET_Poll *msg;
2468 struct CadetConnection *c;
2469 struct CadetFlowControl *fc;
2474 msg = (struct GNUNET_CADET_Poll *) message;
2475 log_message (message, peer, &msg->cid);
2476 c = connection_get (&msg->cid);
2479 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2481 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL message on unknown connection %s!\n",
2482 GNUNET_h2s (GC_h2hc (&msg->cid)));
2483 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2487 /* Is this a forward or backward ACK?
2488 * Note: a poll should never be needed in a loopback case,
2489 * since there is no possiblility of packet loss there, so
2490 * this way of discerining FWD/BCK should not be a problem.
2492 id = GNUNET_PEER_search (peer);
2493 if (GCP_get_short_id (get_next_hop (c)) == id)
2495 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2498 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2500 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2505 GNUNET_break_op (0);
2509 pid = ntohl (msg->pid);
2510 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2511 fc->last_pid_recv = pid;
2512 fwd = fc == &c->bck_fc;
2513 GCC_send_ack (c, fwd, GNUNET_YES);
2520 * Send an ACK on the appropriate connection/channel, depending on
2521 * the direction and the position of the peer.
2523 * @param c Which connection to send the hop-by-hop ACK.
2524 * @param fwd Is this a fwd ACK? (will go dest->root).
2525 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2528 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2530 unsigned int buffer;
2532 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMC send %s ACK on %s\n",
2533 GC_f2s (fwd), GCC_2s (c));
2541 if (GNUNET_NO != c->destroy)
2543 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2547 /* Get available buffer space */
2548 if (GCC_is_terminal (c, fwd))
2550 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2551 buffer = GCT_get_channels_buffer (c->t);
2555 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2556 buffer = GCC_get_buffer (c, fwd);
2558 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2559 if (0 == buffer && GNUNET_NO == force)
2562 /* Send available buffer space */
2563 if (GCC_is_origin (c, fwd))
2565 GNUNET_assert (NULL != c->t);
2566 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2567 GCT_unchoke_channels (c->t);
2571 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2572 send_ack (c, buffer, fwd, force);
2578 * Initialize the connections subsystem
2580 * @param c Configuration handle.
2583 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2585 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2587 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2590 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2591 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2592 GNUNET_SCHEDULER_shutdown ();
2597 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2600 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2601 "CADET", "MAX_CONNECTIONS", "MISSING");
2602 GNUNET_SCHEDULER_shutdown ();
2607 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2608 &refresh_connection_time))
2610 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2611 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2612 GNUNET_SCHEDULER_shutdown ();
2615 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2616 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2621 * Destroy each connection on shutdown.
2623 * @param cls Closure (unused).
2624 * @param key Current key code (CID, unused).
2625 * @param value Value in the hash map (connection)
2627 * @return #GNUNET_YES, because we should continue to iterate,
2630 shutdown_iterator (void *cls,
2631 const struct GNUNET_HashCode *key,
2634 struct CadetConnection *c = value;
2642 * Shut down the connections subsystem.
2647 GNUNET_CONTAINER_multihashmap_iterate (connections, &shutdown_iterator, NULL);
2648 GNUNET_CONTAINER_multihashmap_destroy (connections);
2654 * Create a connection.
2656 * @param cid Connection ID (either created locally or imposed remotely).
2657 * @param t Tunnel this connection belongs to (or NULL);
2658 * @param path Path this connection has to use (copy is made).
2659 * @param own_pos Own position in the @c path path.
2661 * @return Newly created connection, NULL in case of error (own id not in path).
2663 struct CadetConnection *
2664 GCC_new (const struct GNUNET_CADET_Hash *cid,
2665 struct CadetTunnel *t,
2666 struct CadetPeerPath *path,
2667 unsigned int own_pos)
2669 struct CadetConnection *c;
2670 struct CadetPeerPath *p;
2672 p = path_duplicate (path);
2673 c = GNUNET_new (struct CadetConnection);
2675 GNUNET_assert (GNUNET_OK ==
2676 GNUNET_CONTAINER_multihashmap_put (connections,
2678 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2679 fc_init (&c->fwd_fc);
2680 fc_init (&c->bck_fc);
2685 GNUNET_assert (own_pos <= p->length - 1);
2686 c->own_pos = own_pos;
2690 if (GNUNET_OK != register_neighbors (c))
2694 path_invalidate (c->path);
2701 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2707 GCC_destroy (struct CadetConnection *c)
2715 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2716 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2719 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n", GCC_2s (c));
2720 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc's f: %p, b: %p\n",
2721 &c->fwd_fc, &c->bck_fc);
2722 LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
2723 c->fwd_fc.poll_task, c->bck_fc.poll_task);
2725 /* Cancel all traffic */
2726 if (NULL != c->path)
2728 connection_cancel_queues (c, GNUNET_YES);
2729 connection_cancel_queues (c, GNUNET_NO);
2730 unregister_neighbors (c);
2733 /* Cancel maintainance task (keepalive/timeout) */
2734 if (NULL != c->fwd_fc.poll_msg)
2736 GCC_cancel (c->fwd_fc.poll_msg);
2737 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg FWD canceled\n");
2739 if (NULL != c->bck_fc.poll_msg)
2741 GCC_cancel (c->bck_fc.poll_msg);
2742 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
2745 /* Delete from tunnel */
2747 GCT_remove_connection (c->t, c);
2749 if (GNUNET_NO == GCC_is_origin (c, GNUNET_YES) && NULL != c->path)
2750 path_destroy (c->path);
2751 if (NULL != c->fwd_maintenance_task)
2752 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2753 if (NULL != c->bck_maintenance_task)
2754 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2755 if (NULL != c->fwd_fc.poll_task)
2757 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2758 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
2760 if (NULL != c->bck_fc.poll_task)
2762 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2763 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
2766 GNUNET_break (GNUNET_YES ==
2767 GNUNET_CONTAINER_multihashmap_remove (connections,
2770 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
2775 * Get the connection ID.
2777 * @param c Connection to get the ID from.
2779 * @return ID of the connection.
2781 const struct GNUNET_CADET_Hash *
2782 GCC_get_id (const struct CadetConnection *c)
2789 * Get the connection ID.
2791 * @param c Connection to get the ID from.
2793 * @return ID of the connection.
2795 const struct GNUNET_HashCode *
2796 GCC_get_h (const struct CadetConnection *c)
2798 return GC_h2hc (&c->id);
2803 * Get the connection path.
2805 * @param c Connection to get the path from.
2807 * @return path used by the connection.
2809 const struct CadetPeerPath *
2810 GCC_get_path (const struct CadetConnection *c)
2812 if (GNUNET_NO == c->destroy)
2819 * Get the connection state.
2821 * @param c Connection to get the state from.
2823 * @return state of the connection.
2825 enum CadetConnectionState
2826 GCC_get_state (const struct CadetConnection *c)
2832 * Get the connection tunnel.
2834 * @param c Connection to get the tunnel from.
2836 * @return tunnel of the connection.
2838 struct CadetTunnel *
2839 GCC_get_tunnel (const struct CadetConnection *c)
2846 * Get free buffer space in a connection.
2848 * @param c Connection.
2849 * @param fwd Is query about FWD traffic?
2851 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2854 GCC_get_buffer (struct CadetConnection *c, int fwd)
2856 struct CadetFlowControl *fc;
2858 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2860 return (fc->queue_max - fc->queue_n);
2864 * Get how many messages have we allowed to send to us from a direction.
2866 * @param c Connection.
2867 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2869 * @return last_ack_sent - last_pid_recv
2872 GCC_get_allowed (struct CadetConnection *c, int fwd)
2874 struct CadetFlowControl *fc;
2876 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2877 if (CADET_CONNECTION_READY != c->state
2878 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
2882 return (fc->last_ack_sent - fc->last_pid_recv);
2886 * Get messages queued in a connection.
2888 * @param c Connection.
2889 * @param fwd Is query about FWD traffic?
2891 * @return Number of messages queued.
2894 GCC_get_qn (struct CadetConnection *c, int fwd)
2896 struct CadetFlowControl *fc;
2898 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2905 * Get next PID to use.
2907 * @param c Connection.
2908 * @param fwd Is query about FWD traffic?
2910 * @return Last PID used + 1.
2913 GCC_get_pid (struct CadetConnection *c, int fwd)
2915 struct CadetFlowControl *fc;
2917 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2919 return fc->last_pid_sent + 1;
2924 * Allow the connection to advertise a buffer of the given size.
2926 * The connection will send an @c fwd ACK message (so: in direction !fwd)
2927 * allowing up to last_pid_recv + buffer.
2929 * @param c Connection.
2930 * @param buffer How many more messages the connection can accept.
2931 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
2934 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
2936 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
2937 GCC_2s (c), buffer, GC_f2s (fwd));
2938 send_ack (c, buffer, fwd, GNUNET_NO);
2943 * Notify other peers on a connection of a broken link. Mark connections
2944 * to destroy after all traffic has been sent.
2946 * @param c Connection on which there has been a disconnection.
2947 * @param peer Peer that disconnected.
2950 GCC_notify_broken (struct CadetConnection *c,
2951 struct CadetPeer *peer)
2953 struct CadetPeer *hop;
2956 LOG (GNUNET_ERROR_TYPE_DEBUG,
2957 " notify broken on %s due to %s disconnect\n",
2958 GCC_2s (c), GCP_2s (peer));
2960 hop = get_prev_hop (c);
2963 /* Path was NULL, we should have deleted the connection. */
2968 fwd = (peer == hop);
2969 if (GNUNET_YES == GCC_is_terminal (c, fwd))
2971 /* Local shutdown, no one to notify about this. */
2975 if (GNUNET_NO == c->destroy)
2976 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
2978 /* Connection will have at least one pending message
2979 * (the one we just scheduled), so no point in checking whether to
2980 * destroy immediately. */
2981 c->destroy = GNUNET_YES;
2982 c->state = CADET_CONNECTION_DESTROYED;
2985 * Cancel all queues, if no message is left, connection will be destroyed.
2987 connection_cancel_queues (c, !fwd);
2994 * Is this peer the first one on the connection?
2996 * @param c Connection.
2997 * @param fwd Is this about fwd traffic?
2999 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3002 GCC_is_origin (struct CadetConnection *c, int fwd)
3004 if (!fwd && c->path->length - 1 == c->own_pos )
3006 if (fwd && 0 == c->own_pos)
3013 * Is this peer the last one on the connection?
3015 * @param c Connection.
3016 * @param fwd Is this about fwd traffic?
3017 * Note that the ROOT is the terminal for BCK traffic!
3019 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3022 GCC_is_terminal (struct CadetConnection *c, int fwd)
3024 return GCC_is_origin (c, !fwd);
3029 * See if we are allowed to send by the next hop in the given direction.
3031 * @param c Connection.
3032 * @param fwd Is this about fwd traffic?
3034 * @return #GNUNET_YES in case it's OK to send.
3037 GCC_is_sendable (struct CadetConnection *c, int fwd)
3039 struct CadetFlowControl *fc;
3041 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking sendability of %s traffic on %s\n",
3042 GC_f2s (fwd), GCC_2s (c));
3048 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3049 LOG (GNUNET_ERROR_TYPE_DEBUG,
3050 " last ack recv: %u, last pid sent: %u\n",
3051 fc->last_ack_recv, fc->last_pid_sent);
3052 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3054 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3057 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3063 * Check if this connection is a direct one (never trim a direct connection).
3065 * @param c Connection.
3067 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3070 GCC_is_direct (struct CadetConnection *c)
3072 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3076 * Sends an already built message on a connection, properly registering
3077 * all used resources.
3079 * @param message Message to send. Function makes a copy of it.
3080 * If message is not hop-by-hop, decrements TTL of copy.
3081 * @param payload_type Type of payload, in case the message is encrypted.
3082 * @param c Connection on which this message is transmitted.
3083 * @param fwd Is this a fwd message?
3084 * @param force Force the connection to accept the message (buffer overfill).
3085 * @param cont Continuation called once message is sent. Can be NULL.
3086 * @param cont_cls Closure for @c cont.
3088 * @return Handle to cancel the message before it's sent.
3089 * NULL on error or if @c cont is NULL.
3090 * Invalid on @c cont call.
3092 struct CadetConnectionQueue *
3093 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3094 uint16_t payload_type, uint32_t payload_id,
3095 struct CadetConnection *c, int fwd, int force,
3096 GCC_sent cont, void *cont_cls)
3098 struct CadetFlowControl *fc;
3099 struct CadetConnectionQueue *q;
3105 size = ntohs (message->size);
3106 data = GNUNET_malloc (size);
3107 memcpy (data, message, size);
3108 type = ntohs (message->type);
3109 LOG (GNUNET_ERROR_TYPE_INFO, "--> %s (%s %4u) on connection %s (%u bytes)\n",
3110 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), size);
3112 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3113 droppable = GNUNET_NO == force;
3116 struct GNUNET_CADET_AX *axmsg;
3117 struct GNUNET_CADET_Encrypted *emsg;
3118 struct GNUNET_CADET_KX *kmsg;
3119 struct GNUNET_CADET_ACK *amsg;
3120 struct GNUNET_CADET_Poll *pmsg;
3121 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3122 struct GNUNET_CADET_ConnectionBroken *bmsg;
3125 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3126 emsg = (struct GNUNET_CADET_Encrypted *) data;
3127 ttl = ntohl (emsg->ttl);
3130 GNUNET_break_op (0);
3135 emsg->ttl = htonl (ttl - 1);
3136 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3137 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3138 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3139 if (GNUNET_YES == droppable)
3145 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3147 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
3149 GCC_start_poll (c, fwd);
3152 case GNUNET_MESSAGE_TYPE_CADET_AX:
3153 axmsg = (struct GNUNET_CADET_AX *) data;
3155 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3156 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3157 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3160 case GNUNET_MESSAGE_TYPE_CADET_KX:
3161 kmsg = (struct GNUNET_CADET_KX *) data;
3165 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3166 amsg = (struct GNUNET_CADET_ACK *) data;
3168 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3169 droppable = GNUNET_NO;
3172 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3173 pmsg = (struct GNUNET_CADET_Poll *) data;
3175 LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
3176 droppable = GNUNET_NO;
3179 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3180 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
3184 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3185 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
3189 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3190 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3199 if (fc->queue_n > fc->queue_max && droppable)
3201 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3204 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3205 fc->queue_n, fc->queue_max);
3206 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3211 return NULL; /* Drop this message */
3214 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3215 GCC_2s (c), c->pending_messages);
3216 c->pending_messages++;
3218 q = GNUNET_new (struct CadetConnectionQueue);
3219 q->forced = !droppable;
3220 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3221 size, c, fwd, &conn_message_sent, q);
3224 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3230 q->cont_cls = cont_cls;
3231 return (NULL == cont) ? NULL : q;
3236 * Cancel a previously sent message while it's in the queue.
3238 * ONLY can be called before the continuation given to the send function
3239 * is called. Once the continuation is called, the message is no longer in the
3242 * @param q Handle to the queue.
3245 GCC_cancel (struct CadetConnectionQueue *q)
3247 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GMC cancel message\n");
3249 /* queue destroy calls message_sent, which calls q->cont and frees q */
3250 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3255 * Sends a CREATE CONNECTION message for a path to a peer.
3256 * Changes the connection and tunnel states if necessary.
3258 * @param connection Connection to create.
3261 GCC_send_create (struct CadetConnection *connection)
3263 enum CadetTunnelCState state;
3266 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3267 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3269 LOG (GNUNET_ERROR_TYPE_INFO, "---> %s on connection %s (%u bytes)\n",
3270 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE),
3271 GCC_2s (connection), size);
3272 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3273 connection, connection->pending_messages);
3274 connection->pending_messages++;
3276 connection->maintenance_q =
3277 GCP_queue_add (get_next_hop (connection), NULL,
3278 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 0,
3279 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3281 state = GCT_get_cstate (connection->t);
3282 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3283 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3284 if (CADET_CONNECTION_NEW == connection->state)
3285 connection_change_state (connection, CADET_CONNECTION_SENT);
3290 * Send a message to all peers in this connection that the connection
3291 * is no longer valid.
3293 * If some peer should not receive the message, it should be zero'ed out
3294 * before calling this function.
3296 * @param c The connection whose peers to notify.
3299 GCC_send_destroy (struct CadetConnection *c)
3301 struct GNUNET_CADET_ConnectionDestroy msg;
3303 if (GNUNET_YES == c->destroy)
3306 msg.header.size = htons (sizeof (msg));
3307 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3309 LOG (GNUNET_ERROR_TYPE_DEBUG,
3310 " sending connection destroy for connection %s\n",
3313 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3314 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3315 GNUNET_YES, GNUNET_YES,
3317 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3318 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3319 GNUNET_NO, GNUNET_YES,
3321 c->destroy = GNUNET_YES;
3322 c->state = CADET_CONNECTION_DESTROYED;
3327 * @brief Start a polling timer for the connection.
3329 * When a neighbor does not accept more traffic on the connection it could be
3330 * caused by a simple congestion or by a lost ACK. Polling enables to check
3331 * for the lastest ACK status for a connection.
3333 * @param c Connection.
3334 * @param fwd Should we poll in the FWD direction?
3337 GCC_start_poll (struct CadetConnection *c, int fwd)
3339 struct CadetFlowControl *fc;
3341 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3342 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
3344 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3346 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** not needed (%u, %p)\n",
3347 fc->poll_task, fc->poll_msg);
3350 LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL started on request\n");
3351 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3358 * @brief Stop polling a connection for ACKs.
3360 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3362 * @param c Connection.
3363 * @param fwd Should we stop the poll in the FWD direction?
3366 GCC_stop_poll (struct CadetConnection *c, int fwd)
3368 struct CadetFlowControl *fc;
3370 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3371 if (NULL != fc->poll_task)
3373 GNUNET_SCHEDULER_cancel (fc->poll_task);
3374 fc->poll_task = NULL;
3379 * Get a (static) string for a connection.
3381 * @param c Connection.
3384 GCC_2s (const struct CadetConnection *c)
3391 static char buf[128];
3393 SPRINTF (buf, "%s (->%s)",
3394 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3397 return GNUNET_h2s (GC_h2hc (&c->id));
3402 * Log all possible info about the connection state.
3404 * @param c Connection to debug.
3405 * @param level Debug level to use.
3408 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3413 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3415 __FILE__, __FUNCTION__, __LINE__);
3421 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3425 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3426 s = path_2s (c->path);
3427 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3429 LOG2 (level, "CCC state: %s, destroy: %u\n",
3430 GCC_state2s (c->state), c->destroy);
3431 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3432 if (NULL != c->perf)
3433 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3435 LOG2 (level, "CCC FWD flow control:\n");
3436 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3437 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3438 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3439 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3440 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3441 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3442 LOG2 (level, "CCC POLL: task %d, msg %p, msg_ack %p)\n",
3443 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3445 LOG2 (level, "CCC BCK flow control:\n");
3446 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3447 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3448 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3449 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3450 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3451 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3452 LOG2 (level, "CCC POLL: task %d, msg %p, msg_ack %p)\n",
3453 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3455 LOG2 (level, "CCC DEBUG CONNECTION END\n");