2 This file is part of GNUnet.
3 Copyright (C) 2001-2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/gnunet-service-cadet_connection.c
22 * @brief GNUnet CADET service connection handling
23 * @author Bartlomiej Polot
26 #include "gnunet_util_lib.h"
27 #include "gnunet_statistics_service.h"
28 #include "cadet_path.h"
29 #include "cadet_protocol.h"
31 #include "gnunet-service-cadet_connection.h"
32 #include "gnunet-service-cadet_peer.h"
33 #include "gnunet-service-cadet_tunnel.h"
37 * Should we run somewhat expensive checks on our invariants?
39 #define CHECK_INVARIANTS 0
42 #define LOG(level, ...) GNUNET_log_from (level,"cadet-con",__VA_ARGS__)
43 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-con",__VA_ARGS__)
46 #define CADET_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
47 GNUNET_TIME_UNIT_MINUTES,\
52 /******************************************************************************/
53 /******************************** STRUCTS **********************************/
54 /******************************************************************************/
57 * Handle for messages queued but not yet sent.
59 struct CadetConnectionQueue
62 struct CadetConnectionQueue *next;
63 struct CadetConnectionQueue *prev;
66 * Peer queue handle, to cancel if necessary.
68 struct CadetPeerQueue *peer_q;
71 * Continuation to call once sent.
76 * Closure for @e cont.
81 * Was this a forced message? (Do not account for it)
88 * Struct to encapsulate all the Flow Control information to a peer to which
89 * we are directly connected (on a core level).
91 struct CadetFlowControl
94 * Connection this controls.
96 struct CadetConnection *c;
98 struct CadetConnectionQueue *q_head;
99 struct CadetConnectionQueue *q_tail;
102 * How many messages are in the queue on this connection.
104 unsigned int queue_n;
107 * How many messages do we accept in the queue.
108 * If 0, the connection is broken in this direction (next hop disconnected).
110 unsigned int queue_max;
113 * ID of the next packet to send.
118 * ID of the last packet sent towards the peer.
120 uint32_t last_pid_sent;
123 * ID of the last packet received from the peer.
125 uint32_t last_pid_recv;
128 * Bitmap of past 32 messages received:
129 * - LSB being @c last_pid_recv.
130 * - MSB being @c last_pid_recv - 31 (mod UINTMAX).
132 uint32_t recv_bitmap;
135 * Last ACK sent to the peer (peer can't send more than this PID).
137 uint32_t last_ack_sent;
140 * Last ACK sent towards the origin (for traffic towards leaf node).
142 uint32_t last_ack_recv;
145 * Task to poll the peer in case of a lost ACK causes stall.
147 struct GNUNET_SCHEDULER_Task *poll_task;
150 * How frequently to poll for ACKs.
152 struct GNUNET_TIME_Relative poll_time;
155 * Queued poll message, to cancel if not necessary anymore (got ACK).
157 struct CadetConnectionQueue *poll_msg;
160 * Queued poll message, to cancel if not necessary anymore (got ACK).
162 struct CadetConnectionQueue *ack_msg;
166 * Keep a record of the last messages sent on this connection.
168 struct CadetConnectionPerformance
171 * Circular buffer for storing measurements.
173 double usecsperbyte[AVG_MSGS];
176 * Running average of @c usecsperbyte.
181 * How many values of @c usecsperbyte are valid.
186 * Index of the next "free" position in @c usecsperbyte.
193 * Struct containing all information regarding a connection to a peer.
195 struct CadetConnection
198 * Tunnel this connection is part of.
200 struct CadetTunnel *t;
203 * Flow control information for traffic fwd.
205 struct CadetFlowControl fwd_fc;
208 * Flow control information for traffic bck.
210 struct CadetFlowControl bck_fc;
213 * Measure connection performance on the endpoint.
215 struct CadetConnectionPerformance *perf;
218 * ID of the connection.
220 struct GNUNET_CADET_Hash id;
223 * Path being used for the tunnel. At the origin of the connection
224 * it's a pointer to the destination's path pool, otherwise just a copy.
226 struct CadetPeerPath *path;
229 * Task to keep the used paths alive at the owner,
230 * time tunnel out on all the other peers.
232 struct GNUNET_SCHEDULER_Task *fwd_maintenance_task;
235 * Task to keep the used paths alive at the destination,
236 * time tunnel out on all the other peers.
238 struct GNUNET_SCHEDULER_Task *bck_maintenance_task;
241 * Queue handle for maintainance traffic. One handle for FWD and BCK since
242 * one peer never needs to maintain both directions (no loopback connections).
244 struct CadetPeerQueue *maintenance_q;
247 * Should equal #get_next_hop(), or NULL if that peer disconnected.
249 struct CadetPeer *next_peer;
252 * Should equal #get_prev_hop(), or NULL if that peer disconnected.
254 struct CadetPeer *prev_peer;
257 * State of the connection.
259 enum CadetConnectionState state;
262 * Position of the local peer in the path.
264 unsigned int own_pos;
267 * Pending message count.
269 unsigned int pending_messages;
273 * - if 0, connection in use.
274 * - if 1, destroy on last message.
275 * - if 2, connection is being destroyed don't re-enter.
280 * In-connection-map flag. Sometimes, when @e destroy is set but
281 * actual destruction is delayed to enable us to finish processing
282 * queues (i.e. in the direction that is still working), we remove
283 * the connection from the map to prevent it from still being
284 * found (and used) by accident. This flag is set to #GNUNET_YES
285 * for a connection that is not in the #connections map. Should
286 * only be #GNUNET_YES if #destroy is also non-zero.
291 * Counter to do exponential backoff when creating a connection (max 64).
293 unsigned short create_retry;
296 * Task to check if connection has duplicates.
298 struct GNUNET_SCHEDULER_Task *check_duplicates_task;
302 /******************************************************************************/
303 /******************************* GLOBALS ***********************************/
304 /******************************************************************************/
307 * Global handle to the statistics service.
309 extern struct GNUNET_STATISTICS_Handle *stats;
312 * Local peer own ID (memory efficient handle).
314 extern GNUNET_PEER_Id myid;
317 * Local peer own ID (full value).
319 extern struct GNUNET_PeerIdentity my_full_id;
322 * Connections known, indexed by cid (CadetConnection).
324 static struct GNUNET_CONTAINER_MultiHashMap *connections;
327 * How many connections are we willing to maintain.
328 * Local connections are always allowed,
329 * even if there are more connections than max.
331 static unsigned long long max_connections;
334 * How many messages *in total* are we willing to queue, divide by number of
335 * connections to get connection queue size.
337 static unsigned long long max_msgs_queue;
340 * How often to send path keepalives. Paths timeout after 4 missed.
342 static struct GNUNET_TIME_Relative refresh_connection_time;
345 * How often to send path create / ACKs.
347 static struct GNUNET_TIME_Relative create_connection_time;
350 /******************************************************************************/
351 /******************************** STATIC ***********************************/
352 /******************************************************************************/
356 #if 0 // avoid compiler warning for unused static function
358 fc_debug (struct CadetFlowControl *fc)
360 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
361 fc->last_pid_recv, fc->last_ack_sent);
362 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
363 fc->last_pid_sent, fc->last_ack_recv);
364 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
365 fc->queue_n, fc->queue_max);
369 connection_debug (struct CadetConnection *c)
373 LOG (GNUNET_ERROR_TYPE_INFO, "DEBUG NULL CONNECTION\n");
376 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
377 peer2s (c->t->peer), GCC_2s (c));
378 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
379 c->state, c->pending_messages);
380 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
381 fc_debug (&c->fwd_fc);
382 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
383 fc_debug (&c->bck_fc);
389 * Schedule next keepalive task, taking in consideration
390 * the connection state and number of retries.
392 * @param c Connection for which to schedule the next keepalive.
393 * @param fwd Direction for the next keepalive.
396 schedule_next_keepalive (struct CadetConnection *c, int fwd);
400 * Resets the connection timeout task, some other message has done the
402 * - For the first peer on the direction this means to send
403 * a keepalive or a path confirmation message (either create or ACK).
404 * - For all other peers, this means to destroy the connection,
405 * due to lack of activity.
406 * Starts the timeout if no timeout was running (connection just created).
408 * @param c Connection whose timeout to reset.
409 * @param fwd Is this forward?
412 connection_reset_timeout (struct CadetConnection *c, int fwd);
416 * Get string description for tunnel state. Reentrant.
418 * @param s Tunnel state.
420 * @return String representation.
423 GCC_state2s (enum CadetConnectionState s)
427 case CADET_CONNECTION_NEW:
428 return "CADET_CONNECTION_NEW";
429 case CADET_CONNECTION_SENT:
430 return "CADET_CONNECTION_SENT";
431 case CADET_CONNECTION_ACK:
432 return "CADET_CONNECTION_ACK";
433 case CADET_CONNECTION_READY:
434 return "CADET_CONNECTION_READY";
435 case CADET_CONNECTION_DESTROYED:
436 return "CADET_CONNECTION_DESTROYED";
437 case CADET_CONNECTION_BROKEN:
438 return "CADET_CONNECTION_BROKEN";
441 LOG (GNUNET_ERROR_TYPE_ERROR, " conn state %u unknown!\n", s);
442 return "CADET_CONNECTION_STATE_ERROR";
448 * Initialize a Flow Control structure to the initial state.
450 * @param fc Flow Control structure to initialize.
453 fc_init (struct CadetFlowControl *fc)
455 fc->next_pid = (uint32_t) 0;
456 fc->last_pid_sent = (uint32_t) -1;
457 fc->last_pid_recv = (uint32_t) -1;
458 fc->last_ack_sent = (uint32_t) 0;
459 fc->last_ack_recv = (uint32_t) 0;
460 fc->poll_task = NULL;
461 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
463 fc->queue_max = (max_msgs_queue / max_connections) + 1;
470 * @param cid Connection ID.
472 * @return conntection with the given ID @cid or NULL if not found.
474 static struct CadetConnection *
475 connection_get (const struct GNUNET_CADET_Hash *cid)
477 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
482 * Change the connection state. Cannot change a connection marked as destroyed.
484 * @param c Connection to change.
485 * @param state New state to set.
488 connection_change_state (struct CadetConnection* c,
489 enum CadetConnectionState state)
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 "Connection %s state %s -> %s\n",
493 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
494 if (CADET_CONNECTION_DESTROYED <= c->state) /* Destroyed or broken. */
496 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
500 if (CADET_CONNECTION_READY == state)
506 * Mark a connection as "destroyed", to send all pending traffic and freeing
507 * all associated resources, without accepting new status changes on it.
509 * @param c Connection to mark as destroyed.
512 mark_destroyed (struct CadetConnection *c)
514 c->destroy = GNUNET_YES;
515 connection_change_state (c, CADET_CONNECTION_DESTROYED);
520 * Function called if a connection has been stalled for a while,
521 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
523 * @param cls Closure (poll ctx).
526 send_poll (void *cls);
530 * Send an ACK on the connection, informing the predecessor about
531 * the available buffer space. Should not be called in case the peer
532 * is origin (no predecessor) in the @c fwd direction.
534 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
535 * the ACK itself goes "back" (dest->root).
537 * @param c Connection on which to send the ACK.
538 * @param buffer How much space free to advertise?
539 * @param fwd Is this FWD ACK? (Going dest -> root)
540 * @param force Don't optimize out.
543 send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
545 struct CadetFlowControl *next_fc;
546 struct CadetFlowControl *prev_fc;
547 struct GNUNET_CADET_ACK msg;
551 GCC_check_connections ();
552 GNUNET_assert (GNUNET_NO == GCC_is_origin (c, fwd));
554 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
555 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
557 LOG (GNUNET_ERROR_TYPE_DEBUG, "send %s ack on %s\n",
558 GC_f2s (fwd), GCC_2s (c));
560 /* Check if we need to transmit the ACK. */
561 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
562 if (3 < delta && buffer < delta && GNUNET_NO == force)
564 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, delta > 3\n");
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
566 " last pid recv: %u, last ack sent: %u\n",
567 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
568 GCC_check_connections ();
572 /* Ok, ACK might be necessary, what PID to ACK? */
573 ack = prev_fc->last_pid_recv + buffer;
574 LOG (GNUNET_ERROR_TYPE_DEBUG,
575 " ACK %u, last PID %u, last ACK %u, qmax %u, q %u\n",
576 ack, prev_fc->last_pid_recv, prev_fc->last_ack_sent,
577 next_fc->queue_max, next_fc->queue_n);
578 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
580 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
581 GCC_check_connections ();
585 /* Check if message is already in queue */
586 if (NULL != prev_fc->ack_msg)
588 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
590 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
591 GCC_cancel (prev_fc->ack_msg);
592 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
596 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
597 GCC_check_connections ();
602 prev_fc->last_ack_sent = ack;
604 /* Build ACK message and send on conn */
605 msg.header.size = htons (sizeof (msg));
606 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
607 msg.ack = htonl (ack);
610 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, UINT16_MAX, ack,
613 GNUNET_assert (NULL != prev_fc->ack_msg);
614 GCC_check_connections ();
619 * Update performance information if we are a connection's endpoint.
621 * @param c Connection to update.
622 * @param wait How much time did we wait to send the last message.
623 * @param size Size of the last message.
626 update_perf (struct CadetConnection *c,
627 struct GNUNET_TIME_Relative wait,
630 struct CadetConnectionPerformance *p;
634 return; /* Only endpoints are interested in timing. */
637 usecsperbyte = ((double) wait.rel_value_us) / size;
638 if (p->size == AVG_MSGS)
640 /* Array is full. Substract oldest value, add new one and store. */
641 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
642 p->usecsperbyte[p->idx] = usecsperbyte;
643 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
647 /* Array not yet full. Add current value to avg and store. */
648 p->usecsperbyte[p->idx] = usecsperbyte;
650 p->avg += p->usecsperbyte[p->idx];
654 p->idx = (p->idx + 1) % AVG_MSGS;
659 * Callback called when a connection queued message is sent.
661 * Calculates the average time and connection packet tracking.
663 * @param cls Closure (ConnectionQueue Handle), can be NULL.
664 * @param c Connection this message was on.
665 * @param fwd Was this a FWD going message?
666 * @param sent Was it really sent? (Could have been canceled)
667 * @param type Type of message sent.
668 * @param payload_type Type of payload, if applicable.
669 * @param pid Message ID, or 0 if not applicable (create, destroy, etc).
670 * @param size Size of the message.
671 * @param wait Time spent waiting for core (only the time for THIS message)
674 conn_message_sent (void *cls,
675 struct CadetConnection *c, int fwd, int sent,
676 uint16_t type, uint16_t payload_type, uint32_t pid,
678 struct GNUNET_TIME_Relative wait)
680 struct CadetConnectionQueue *q = cls;
681 struct CadetFlowControl *fc;
684 GCC_check_connections ();
685 LOG (GNUNET_ERROR_TYPE_INFO,
686 ">>> %s (%s %4u) on conn %s (%p) %s [%5u] in queue %s\n",
687 GC_m2s (type), GC_m2s (payload_type), pid, GCC_2s (c), c,
689 GNUNET_STRINGS_relative_time_to_string (wait, GNUNET_YES));
691 /* If c is NULL, nothing to update. */
694 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
695 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
697 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
700 GCC_check_connections ();
704 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
705 sent ? "" : "not ", GC_f2s (fwd),
706 GC_m2s (type), GC_m2s (payload_type), pid);
707 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
709 /* Update flow control info. */
710 fc = fwd ? &c->fwd_fc : &c->bck_fc;
714 GNUNET_CONTAINER_DLL_remove (fc->q_head, fc->q_tail, q);
718 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
719 q->cont (q->cont_cls, c, q, type, fwd, size);
723 else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED)
725 /* SHOULD NO LONGER HAPPEN FIXME: REMOVE CASE */
726 // If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt
728 GNUNET_assert (0); // FIXME
730 else /* CONN_CREATE or CONN_ACK */
735 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
736 c->pending_messages--;
737 if ( (GNUNET_YES == c->destroy) &&
738 (0 == c->pending_messages) )
740 LOG (GNUNET_ERROR_TYPE_DEBUG,
741 "! destroying connection!\n");
743 GCC_check_connections ();
747 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
750 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
751 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
752 c->maintenance_q = NULL;
753 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
754 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
755 schedule_next_keepalive (c, fwd);
758 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
759 if (GNUNET_YES == sent)
761 GNUNET_assert (NULL != q);
762 fc->last_pid_sent = pid;
763 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
764 GCC_start_poll (c, fwd);
765 GCC_send_ack (c, fwd, GNUNET_NO);
766 connection_reset_timeout (c, fwd);
769 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
770 if (GNUNET_NO == forced)
773 LOG (GNUNET_ERROR_TYPE_DEBUG,
774 "! accounting pid %u\n",
779 LOG (GNUNET_ERROR_TYPE_DEBUG,
780 "! forced, Q_N not accounting pid %u\n",
785 case GNUNET_MESSAGE_TYPE_CADET_KX:
786 if (GNUNET_YES == sent)
787 connection_reset_timeout (c, fwd);
790 case GNUNET_MESSAGE_TYPE_CADET_POLL:
794 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
797 if (0 == fc->queue_max)
799 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL cancelled: neighbor disconnected\n");
802 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
804 GNUNET_assert (NULL == fc->poll_task);
805 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
806 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
808 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
811 case GNUNET_MESSAGE_TYPE_CADET_ACK:
815 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
816 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
820 LOG (GNUNET_ERROR_TYPE_ERROR, "%s unknown\n", GC_m2s (type));
824 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
826 update_perf (c, wait, size);
827 GCC_check_connections ();
832 * Get the previous hop in a connection
834 * @param c Connection.
836 * @return Previous peer in the connection.
838 static struct CadetPeer *
839 get_prev_hop (const struct CadetConnection *c)
845 LOG (GNUNET_ERROR_TYPE_DEBUG,
846 " get prev hop %s [%u/%u]\n",
847 GCC_2s (c), c->own_pos, c->path->length);
848 if (0 == c->own_pos || c->path->length < 2)
849 id = c->path->peers[0];
851 id = c->path->peers[c->own_pos - 1];
853 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
854 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
856 return GCP_get_short (id, GNUNET_YES);
861 * Get the next hop in a connection
863 * @param c Connection.
865 * @return Next peer in the connection.
867 static struct CadetPeer *
868 get_next_hop (const struct CadetConnection *c)
875 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
876 GCC_2s (c), c->own_pos, c->path->length);
877 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
878 id = c->path->peers[c->path->length - 1];
880 id = c->path->peers[c->own_pos + 1];
882 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
883 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
885 return GCP_get_short (id, GNUNET_YES);
890 * Check that the direct neighbours (previous and next hop)
891 * are properly associated with this connection.
893 * @param c connection to check
896 check_neighbours (const struct CadetConnection *c)
899 return; /* nothing to check */
900 GCP_check_connection (get_next_hop (c), c);
901 GCP_check_connection (get_prev_hop (c), c);
906 * Helper for #GCC_check_connections(). Calls #check_neighbours().
910 * @param value the `struct CadetConnection` to check
911 * @return #GNUNET_OK (continue to iterate)
914 check_connection (void *cls,
915 const struct GNUNET_HashCode *key,
918 struct CadetConnection *c = value;
920 check_neighbours (c);
926 * Check invariants for all connections using #check_neighbours().
929 GCC_check_connections ()
931 if (0 == CHECK_INVARIANTS)
933 if (NULL == connections)
935 GNUNET_CONTAINER_multihashmap_iterate (connections,
942 * Get the hop in a connection.
944 * @param c Connection.
945 * @param fwd Next in the FWD direction?
947 * @return Next peer in the connection.
949 static struct CadetPeer *
950 get_hop (struct CadetConnection *c, int fwd)
952 return (fwd) ? get_next_hop (c) : get_prev_hop (c);
957 * Get a bit mask for a message received out-of-order.
959 * @param last_pid_recv Last PID we received prior to the out-of-order.
960 * @param ooo_pid PID of the out-of-order message.
963 get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
965 return 1 << (last_pid_recv - ooo_pid);
970 * Check is an out-of-order message is ok:
971 * - at most 31 messages behind.
974 * @param last_pid_recv Last in-order PID received.
977 is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
981 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
984 mask = get_recv_bitmask (last_pid_recv, ooo_pid);
985 if (0 != (ooo_bitmap & mask))
993 * Is traffic coming from this sender 'FWD' traffic?
995 * @param c Connection to check.
996 * @param sender Short peer identity of neighbor.
998 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
999 * the traffic is 'FWD'.
1000 * #GNUNET_NO for BCK.
1001 * #GNUNET_SYSERR for errors (sender isn't a hop in the connection).
1004 is_fwd (const struct CadetConnection *c,
1005 const struct CadetPeer *sender)
1009 id = GCP_get_short_id (sender);
1010 if (GCP_get_short_id (get_prev_hop (c)) == id)
1013 if (GCP_get_short_id (get_next_hop (c)) == id)
1016 return GNUNET_SYSERR;
1021 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1022 * or a first CONNECTION_ACK directed to us.
1024 * @param c Connection to confirm.
1025 * @param fwd Should we send it FWD? (root->dest)
1026 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
1029 send_connection_ack (struct CadetConnection *c, int fwd)
1031 struct GNUNET_CADET_ConnectionACK msg;
1032 struct CadetTunnel *t;
1033 const uint16_t size = sizeof (struct GNUNET_CADET_ConnectionACK);
1034 const uint16_t type = GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK;
1036 GCC_check_connections ();
1038 LOG (GNUNET_ERROR_TYPE_INFO,
1039 "==> %s ({ C %s ACK} 0) on conn %s (%p) %s [%5u]\n",
1040 GC_m2s (type), GC_f2s (!fwd), GCC_2s (c), c, GC_f2s (fwd), size);
1042 msg.header.size = htons (size);
1043 msg.header.type = htons (type);
1044 msg.reserved = htonl (0);
1047 GNUNET_assert (NULL == c->maintenance_q);
1048 c->maintenance_q = GCP_send (get_hop (c, fwd), &msg.header,
1049 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0,
1051 &conn_message_sent, NULL);
1052 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (conn`ACK)\n",
1053 c, c->pending_messages);
1054 c->pending_messages++;
1056 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
1057 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
1058 if (CADET_CONNECTION_READY != c->state)
1059 connection_change_state (c, CADET_CONNECTION_SENT);
1060 GCC_check_connections ();
1065 * Send a notification that a connection is broken.
1067 * @param c Connection that is broken.
1068 * @param id1 Peer that has disconnected.
1069 * @param id2 Peer that has disconnected.
1070 * @param fwd Direction towards which to send it.
1073 send_broken (struct CadetConnection *c,
1074 const struct GNUNET_PeerIdentity *id1,
1075 const struct GNUNET_PeerIdentity *id2,
1078 struct GNUNET_CADET_ConnectionBroken msg;
1080 GCC_check_connections ();
1081 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1082 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1084 msg.reserved = htonl (0);
1087 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c, fwd,
1088 GNUNET_YES, NULL, NULL);
1089 GCC_check_connections ();
1094 * Send a notification that a connection is broken, when a connection
1095 * isn't even known to the local peer or soon to be destroyed.
1097 * @param connection_id Connection ID.
1098 * @param id1 Peer that has disconnected, probably local peer.
1099 * @param id2 Peer that has disconnected can be NULL if unknown.
1100 * @param neighbor Peer to notify (neighbor who sent the connection).
1103 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
1104 const struct GNUNET_PeerIdentity *id1,
1105 const struct GNUNET_PeerIdentity *id2,
1106 struct CadetPeer *neighbor)
1108 struct GNUNET_CADET_ConnectionBroken msg;
1110 GCC_check_connections ();
1111 LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n",
1112 GNUNET_h2s (GC_h2hc (connection_id)));
1114 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1115 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1116 msg.cid = *connection_id;
1117 msg.reserved = htonl (0);
1122 memset (&msg.peer2, 0, sizeof (msg.peer2));
1123 GNUNET_assert (NULL != GCP_send (neighbor, &msg.header,
1125 NULL, GNUNET_SYSERR, /* connection, fwd */
1126 NULL, NULL)); /* continuation */
1127 GCC_check_connections ();
1132 * Send keepalive packets for a connection.
1134 * @param c Connection to keep alive..
1135 * @param fwd Is this a FWD keepalive? (owner -> dest).
1138 send_connection_keepalive (struct CadetConnection *c, int fwd)
1140 struct GNUNET_MessageHeader msg;
1141 struct CadetFlowControl *fc;
1144 GCC_check_connections ();
1145 LOG (GNUNET_ERROR_TYPE_INFO,
1146 "keepalive %s for connection %s\n",
1147 GC_f2s (fwd), GCC_2s (c));
1149 GNUNET_assert (NULL != c->t);
1150 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1151 tunnel_ready = GNUNET_YES == GCT_has_queued_traffic (c->t)
1152 && CADET_TUNNEL_KEY_OK <= GCT_get_estate (c->t);
1153 if (0 < fc->queue_n || tunnel_ready)
1155 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1159 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1161 GNUNET_assert (NULL != c->t);
1162 msg.size = htons (sizeof (msg));
1163 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
1165 GNUNET_assert (NULL ==
1166 GCT_send_prebuilt_message (&msg, c->t, c,
1167 GNUNET_NO, NULL, NULL));
1168 GCC_check_connections ();
1173 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1175 * @param c Connection for which to send the message.
1176 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1179 connection_recreate (struct CadetConnection *c, int fwd)
1181 LOG (GNUNET_ERROR_TYPE_DEBUG,
1182 "sending connection recreate\n");
1184 GCC_send_create (c);
1186 send_connection_ack (c, GNUNET_NO);
1191 * Generic connection timer management.
1192 * Depending on the role of the peer in the connection will send the
1193 * appropriate message (build or keepalive)
1195 * @param c Conncetion to maintain.
1196 * @param fwd Is FWD?
1199 connection_maintain (struct CadetConnection *c, int fwd)
1201 if (GNUNET_NO != c->destroy)
1203 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1210 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1214 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1216 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1218 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1219 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1220 schedule_next_keepalive (c, fwd);
1225 case CADET_CONNECTION_NEW:
1228 case CADET_CONNECTION_SENT:
1229 connection_recreate (c, fwd);
1231 case CADET_CONNECTION_READY:
1232 send_connection_keepalive (c, fwd);
1241 * Keep the connection alive.
1243 * @param c Connection to keep alive.
1244 * @param fwd Direction.
1247 connection_keepalive (struct CadetConnection *c,
1250 GCC_check_connections ();
1251 LOG (GNUNET_ERROR_TYPE_DEBUG,
1252 "%s keepalive for %s\n",
1253 GC_f2s (fwd), GCC_2s (c));
1256 c->fwd_maintenance_task = NULL;
1258 c->bck_maintenance_task = NULL;
1259 connection_maintain (c, fwd);
1260 GCC_check_connections ();
1261 /* Next execution will be scheduled by message_sent or _maintain*/
1266 * Keep the connection alive in the FWD direction.
1268 * @param cls Closure (connection to keepalive).
1271 connection_fwd_keepalive (void *cls)
1273 struct CadetConnection *c = cls;
1275 GCC_check_connections ();
1276 connection_keepalive (c,
1278 GCC_check_connections ();
1283 * Keep the connection alive in the BCK direction.
1285 * @param cls Closure (connection to keepalive).
1288 connection_bck_keepalive (void *cls)
1290 struct CadetConnection *c = cls;
1292 GCC_check_connections ();
1293 connection_keepalive (c,
1295 GCC_check_connections ();
1300 * Schedule next keepalive task, taking in consideration
1301 * the connection state and number of retries.
1303 * If the peer is not the origin, do nothing.
1305 * @param c Connection for which to schedule the next keepalive.
1306 * @param fwd Direction for the next keepalive.
1309 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1311 struct GNUNET_TIME_Relative delay;
1312 struct GNUNET_SCHEDULER_Task * *task_id;
1313 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1315 GCC_check_connections ();
1316 if (GNUNET_NO == GCC_is_origin (c, fwd))
1319 /* Calculate delay to use, depending on the state of the connection */
1320 if (CADET_CONNECTION_READY == c->state)
1322 delay = refresh_connection_time;
1326 if (1 > c->create_retry)
1327 c->create_retry = 1;
1328 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1330 if (c->create_retry < 64) // TODO make configurable
1331 c->create_retry *= 2;
1334 /* Select direction-dependent parameters */
1335 if (GNUNET_YES == fwd)
1337 task_id = &c->fwd_maintenance_task;
1338 keepalive_task = &connection_fwd_keepalive;
1342 task_id = &c->bck_maintenance_task;
1343 keepalive_task = &connection_bck_keepalive;
1346 /* Check that no one scheduled it before us */
1347 if (NULL != *task_id)
1349 /* No need for a _break. It can happen for instance when sending a SYNACK
1350 * for a duplicate SYN: the first SYNACK scheduled the task. */
1351 GNUNET_SCHEDULER_cancel (*task_id);
1354 /* Schedule the task */
1355 *task_id = GNUNET_SCHEDULER_add_delayed (delay,
1358 LOG (GNUNET_ERROR_TYPE_DEBUG,
1359 "next keepalive in %s\n",
1360 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1361 GCC_check_connections ();
1366 * Cancel all transmissions that belong to a certain connection.
1368 * If the connection is scheduled for destruction and no more messages are left,
1369 * the connection will be destroyed by the continuation call.
1371 * @param c Connection which to cancel. Might be destroyed during this call.
1372 * @param fwd Cancel fwd traffic?
1375 connection_cancel_queues (struct CadetConnection *c,
1378 struct CadetFlowControl *fc;
1380 GCC_check_connections ();
1381 LOG (GNUNET_ERROR_TYPE_DEBUG,
1382 "Cancel %s queues for connection %s\n",
1383 GC_f2s (fwd), GCC_2s (c));
1390 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1391 if (NULL != fc->poll_task)
1393 GNUNET_SCHEDULER_cancel (fc->poll_task);
1394 fc->poll_task = NULL;
1395 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL task for fc %p\n", fc);
1397 if (NULL != fc->poll_msg)
1399 GCC_cancel (fc->poll_msg);
1400 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL msg for fc %p\n", fc);
1403 while (NULL != fc->q_head)
1405 GCC_cancel (fc->q_head);
1407 GCC_check_connections ();
1412 * Function called if a connection has been stalled for a while,
1413 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1415 * @param cls Closure (poll ctx).
1418 send_poll (void *cls)
1420 struct CadetFlowControl *fc = cls;
1421 struct GNUNET_CADET_Poll msg;
1422 struct CadetConnection *c;
1425 fc->poll_task = NULL;
1426 GCC_check_connections ();
1428 fwd = fc == &c->fwd_fc;
1429 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1430 GCC_2s (c), GC_f2s (fwd));
1432 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1433 msg.header.size = htons (sizeof (msg));
1435 msg.pid = htonl (fc->last_pid_sent);
1436 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1438 GCC_send_prebuilt_message (&msg.header, UINT16_MAX, fc->last_pid_sent, c,
1439 fc == &c->fwd_fc, GNUNET_YES, NULL, NULL);
1440 GNUNET_assert (NULL != fc->poll_msg);
1441 GCC_check_connections ();
1446 * Generic connection timeout implementation.
1448 * Timeout function due to lack of keepalive/traffic from an endpoint.
1449 * Destroys connection if called.
1451 * @param c Connection to destroy.
1452 * @param fwd Was the timeout from the origin? (FWD timeout)
1455 connection_timeout (struct CadetConnection *c, int fwd)
1457 GCC_check_connections ();
1459 LOG (GNUNET_ERROR_TYPE_INFO,
1460 "Connection %s %s timed out. Destroying.\n",
1463 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1465 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1471 /* If dest, send "broken" notification. */
1472 if (GCC_is_terminal (c, fwd))
1474 struct CadetPeer *next_hop;
1476 next_hop = fwd ? get_prev_hop (c) : get_next_hop (c);
1477 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1481 GCC_check_connections ();
1486 * Timeout function due to lack of keepalive/traffic from the owner.
1487 * Destroys connection if called.
1489 * @param cls Closure (connection to destroy).
1492 connection_fwd_timeout (void *cls)
1494 struct CadetConnection *c = cls;
1496 c->fwd_maintenance_task = NULL;
1497 GCC_check_connections ();
1498 connection_timeout (c, GNUNET_YES);
1499 GCC_check_connections ();
1504 * Timeout function due to lack of keepalive/traffic from the destination.
1505 * Destroys connection if called.
1507 * @param cls Closure (connection to destroy).
1510 connection_bck_timeout (void *cls)
1512 struct CadetConnection *c = cls;
1514 c->bck_maintenance_task = NULL;
1515 GCC_check_connections ();
1516 connection_timeout (c, GNUNET_NO);
1517 GCC_check_connections ();
1522 * Resets the connection timeout task, some other message has done the
1524 * - For the first peer on the direction this means to send
1525 * a keepalive or a path confirmation message (either create or ACK).
1526 * - For all other peers, this means to destroy the connection,
1527 * due to lack of activity.
1528 * Starts the timeout if no timeout was running (connection just created).
1530 * @param c Connection whose timeout to reset.
1531 * @param fwd Is this forward?
1533 * TODO use heap to improve efficiency of scheduler.
1536 connection_reset_timeout (struct CadetConnection *c, int fwd)
1538 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1539 if (GCC_is_origin (c, fwd)) /* Startpoint */
1541 schedule_next_keepalive (c, fwd);
1542 if (NULL != c->maintenance_q)
1544 GCP_send_cancel (c->maintenance_q);
1545 c->maintenance_q = NULL; /* Is set to NULL by conn_message_sent anyway */
1548 else /* Relay, endpoint. */
1550 struct GNUNET_TIME_Relative delay;
1551 struct GNUNET_SCHEDULER_Task * *ti;
1552 GNUNET_SCHEDULER_TaskCallback f;
1554 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1557 GNUNET_SCHEDULER_cancel (*ti);
1558 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1559 LOG (GNUNET_ERROR_TYPE_DEBUG,
1560 " timing out in %s\n",
1561 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1562 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1563 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1569 * Iterator to compare each connection's path with the path of a new connection.
1571 * If the connection coincides, the c member of path is set to the connection
1572 * and the destroy flag of the connection is set.
1574 * @param cls Closure (new path).
1575 * @param c Connection in the tunnel to check.
1578 check_path (void *cls, struct CadetConnection *c)
1580 struct CadetConnection *new_conn = cls;
1581 struct CadetPeerPath *path = new_conn->path;
1583 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1584 GCC_2s (c), c, c->path->length);
1587 && GNUNET_NO == c->destroy
1588 && CADET_CONNECTION_BROKEN != c->state
1589 && CADET_CONNECTION_DESTROYED != c->state
1590 && path_equivalent (path, c->path))
1592 new_conn->destroy = GNUNET_YES; /* Do not mark_destroyed, */
1593 new_conn->path->c = c; /* this is only a flag for the Iterator. */
1594 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1600 * Finds out if this path is already being used by an existing connection.
1602 * Checks the tunnel towards the destination to see if it contains
1603 * any connection with the same path.
1605 * If the existing connection is ready, it is kept.
1606 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1607 * the peer will eventually reject our attempt).
1609 * @param path Path to check.
1610 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1611 * #GNUNET_NO otherwise.
1614 does_connection_exist (struct CadetConnection *conn)
1616 struct CadetPeer *p;
1617 struct CadetTunnel *t;
1618 struct CadetConnection *c;
1620 p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
1623 t = GCP_get_tunnel (p);
1627 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1629 GCT_iterate_connections (t, &check_path, conn);
1631 if (GNUNET_YES == conn->destroy)
1634 conn->destroy = GNUNET_NO;
1635 conn->path->c = conn;
1636 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1637 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1638 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1639 if (CADET_CONNECTION_READY == c->state)
1641 /* The other peer confirmed a live connection with this path,
1642 * why are they trying to duplicate it? */
1643 GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
1646 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not ready, connection unique\n");
1651 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1658 * @brief Check if the tunnel this connection belongs to has any other
1659 * connection with the same path, and destroy one if so.
1661 * @param cls Closure (connection to check).
1664 check_duplicates (void *cls)
1666 struct CadetConnection *c = cls;
1668 c->check_duplicates_task = NULL;
1669 if (GNUNET_YES == does_connection_exist (c))
1671 GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1672 send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
1679 * Wait for enough time to let any dead connections time out and check for
1680 * any remaining duplicates.
1682 * @param c Connection that is a potential duplicate.
1685 schedule_check_duplicates (struct CadetConnection *c)
1687 struct GNUNET_TIME_Relative delay;
1689 if (NULL != c->check_duplicates_task)
1691 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 5);
1692 c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
1699 * Add the connection to the list of both neighbors.
1701 * @param c Connection.
1703 * @return #GNUNET_OK if everything went fine
1704 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1707 register_neighbors (struct CadetConnection *c)
1709 c->next_peer = get_next_hop (c);
1710 c->prev_peer = get_prev_hop (c);
1711 GNUNET_assert (c->next_peer != c->prev_peer);
1712 LOG (GNUNET_ERROR_TYPE_DEBUG,
1713 "register neighbors for connection %s\n",
1715 path_debug (c->path);
1716 LOG (GNUNET_ERROR_TYPE_DEBUG,
1717 "own pos %u\n", c->own_pos);
1718 LOG (GNUNET_ERROR_TYPE_DEBUG,
1719 "putting connection %s to next peer %p\n",
1722 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1724 GCP_2s (c->next_peer));
1725 LOG (GNUNET_ERROR_TYPE_DEBUG,
1726 "putting connection %s to prev peer %p\n",
1729 LOG (GNUNET_ERROR_TYPE_DEBUG,
1730 "prev peer %p %s\n",
1732 GCP_2s (c->prev_peer));
1734 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1735 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1737 if (GCC_is_origin (c, GNUNET_YES))
1738 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1739 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1741 LOG (GNUNET_ERROR_TYPE_DEBUG,
1742 " register neighbors failed\n");
1743 LOG (GNUNET_ERROR_TYPE_DEBUG,
1744 " prev: %s, neighbor?: %d\n",
1745 GCP_2s (c->prev_peer),
1746 GCP_is_neighbor (c->prev_peer));
1747 LOG (GNUNET_ERROR_TYPE_DEBUG,
1748 " next: %s, neighbor?: %d\n",
1749 GCP_2s (c->next_peer),
1750 GCP_is_neighbor (c->next_peer));
1751 return GNUNET_SYSERR;
1753 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1754 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1761 * Remove the connection from the list of both neighbors.
1763 * @param c Connection.
1766 unregister_neighbors (struct CadetConnection *c)
1768 // struct CadetPeer *peer; FIXME dont use next_peer, prev_peer
1769 /* Either already unregistered or never got registered, it's ok either way. */
1770 if (NULL == c->path)
1772 if (NULL != c->next_peer)
1774 GCP_remove_connection (c->next_peer, c);
1775 c->next_peer = NULL;
1777 if (NULL != c->prev_peer)
1779 GCP_remove_connection (c->prev_peer, c);
1780 c->prev_peer = NULL;
1786 * Invalidates all paths towards all peers that comprise the connection which
1787 * rely on the disconnected peer.
1789 * ~O(n^3) (peers in connection * paths/peer * links/path)
1791 * @param c Connection whose peers' paths to clean.
1792 * @param disconnected Peer that disconnected.
1795 invalidate_paths (struct CadetConnection *c,
1796 struct CadetPeer *disconnected)
1798 struct CadetPeer *peer;
1801 for (i = 0; i < c->path->length; i++)
1803 peer = GCP_get_short (c->path->peers[i], GNUNET_NO);
1805 GCP_notify_broken_link (peer, &my_full_id, GCP_get_id (disconnected));
1811 * Bind the connection to the peer and the tunnel to that peer.
1813 * If the peer has no tunnel, create one. Update tunnel and connection
1814 * data structres to reflect new status.
1816 * @param c Connection.
1820 add_to_peer (struct CadetConnection *c,
1821 struct CadetPeer *peer)
1823 GCP_add_tunnel (peer);
1824 c->t = GCP_get_tunnel (peer);
1825 GCT_add_connection (c->t, c);
1830 * Log receipt of message on stderr (INFO level).
1832 * @param message Message received.
1833 * @param peer Peer who sent the message.
1834 * @param conn_id Connection ID of the message.
1837 log_message (const struct GNUNET_MessageHeader *message,
1838 const struct CadetPeer *peer,
1839 const struct GNUNET_CADET_Hash *conn_id)
1845 size = ntohs (message->size);
1846 type = ntohs (message->type);
1849 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1850 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1851 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1852 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1858 LOG (GNUNET_ERROR_TYPE_INFO, "<%s %s on conn %s from %s, %6u bytes\n",
1859 arrow, GC_m2s (type), GNUNET_h2s (GC_h2hc (conn_id)),
1860 GCP_2s(peer), (unsigned int) size);
1863 /******************************************************************************/
1864 /******************************** API ***********************************/
1865 /******************************************************************************/
1868 * Handler for connection creation.
1870 * @param peer Message sender (neighbor).
1871 * @param msg Message itself.
1874 GCC_handle_create (struct CadetPeer *peer,
1875 const struct GNUNET_CADET_ConnectionCreate *msg)
1877 const struct GNUNET_CADET_Hash *cid;
1878 struct GNUNET_PeerIdentity *id;
1879 struct CadetPeerPath *path;
1880 struct CadetPeer *dest_peer;
1881 struct CadetPeer *orig_peer;
1882 struct CadetConnection *c;
1883 unsigned int own_pos;
1886 GCC_check_connections ();
1887 size = ntohs (msg->header.size);
1889 /* Calculate hops */
1890 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1891 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1893 GNUNET_break_op (0);
1896 size /= sizeof (struct GNUNET_PeerIdentity);
1899 GNUNET_break_op (0);
1902 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1904 /* Get parameters */
1906 log_message (&msg->header, peer, cid);
1907 id = (struct GNUNET_PeerIdentity *) &msg[1];
1908 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1910 /* Create connection */
1911 c = connection_get (cid);
1914 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1915 size, myid, &own_pos);
1918 /* Path was malformed, probably our own ID was not in it. */
1919 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
1920 GNUNET_break_op (0);
1925 /* We received this request from a neighbor, we cannot be origin */
1926 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
1927 GNUNET_break_op (0);
1928 path_destroy (path);
1932 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1933 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1934 c = GCC_new (cid, NULL, path, own_pos);
1937 if (path->length - 1 == own_pos)
1939 /* If we are destination, why did the creation fail? */
1941 path_destroy (path);
1942 GCC_check_connections ();
1945 send_broken_unknown (cid, &my_full_id,
1946 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1948 path_destroy (path);
1949 GCC_check_connections ();
1952 GCP_add_path_to_all (path, GNUNET_NO);
1953 connection_reset_timeout (c, GNUNET_YES);
1957 path = path_duplicate (c->path);
1959 if (CADET_CONNECTION_NEW == c->state)
1960 connection_change_state (c, CADET_CONNECTION_SENT);
1962 /* Remember peers */
1963 dest_peer = GCP_get (&id[size - 1], GNUNET_YES);
1964 orig_peer = GCP_get (&id[0], GNUNET_YES);
1966 /* Is it a connection to us? */
1967 if (c->own_pos == path->length - 1)
1969 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1970 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1972 add_to_peer (c, orig_peer);
1973 if (GNUNET_YES == does_connection_exist (c))
1975 /* Peer created a connection equal to one we think exists
1977 * Solution: Keep both and postpone disambiguation. In the meantime
1978 * the connection will time out or peer will inform us it is broken.
1981 * - Use explicit duplicate.
1982 * - Accept new conn and destroy the old. (interruption in higher level)
1983 * - Keep the one with higher ID / created by peer with higher ID. */
1984 schedule_check_duplicates (c);
1987 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
1988 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
1989 if (NULL == c->maintenance_q)
1990 send_connection_ack (c, GNUNET_NO);
1991 if (CADET_CONNECTION_SENT == c->state)
1992 connection_change_state (c, CADET_CONNECTION_ACK);
1996 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1997 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1998 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1999 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c,
2000 GNUNET_YES, GNUNET_YES, NULL, NULL);
2002 path_destroy (path);
2003 GCC_check_connections ();
2008 * Handler for connection confirmations.
2010 * @param peer Message sender (neighbor).
2011 * @param msg Message itself.
2014 GCC_handle_confirm (struct CadetPeer *peer,
2015 const struct GNUNET_CADET_ConnectionACK *msg)
2017 struct CadetConnection *c;
2018 enum CadetConnectionState oldstate;
2021 GCC_check_connections ();
2022 log_message (&msg->header, peer, &msg->cid);
2023 c = connection_get (&msg->cid);
2026 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2028 LOG (GNUNET_ERROR_TYPE_DEBUG,
2029 " don't know the connection!\n");
2030 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2031 GCC_check_connections ();
2034 if (GNUNET_NO != c->destroy)
2036 GNUNET_assert (CADET_CONNECTION_DESTROYED == c->state);
2037 GNUNET_STATISTICS_update (stats, "# control on dying connection",
2039 LOG (GNUNET_ERROR_TYPE_DEBUG,
2040 "connection %s being destroyed, ignoring confirm\n",
2042 GCC_check_connections ();
2046 oldstate = c->state;
2047 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GCP_2s (peer));
2048 if (get_next_hop (c) == peer)
2050 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2052 if (CADET_CONNECTION_SENT == oldstate)
2053 connection_change_state (c, CADET_CONNECTION_ACK);
2055 else if (get_prev_hop (c) == peer)
2057 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2059 connection_change_state (c, CADET_CONNECTION_READY);
2063 GNUNET_STATISTICS_update (stats, "# control on connection from wrong peer",
2065 GNUNET_break_op (0);
2069 connection_reset_timeout (c, fwd);
2071 GNUNET_assert (NULL != c->path);
2072 GCP_add_path_to_all (c->path, GNUNET_YES);
2074 /* Message for us as creator? */
2075 if (GNUNET_YES == GCC_is_origin (c, GNUNET_YES))
2077 if (GNUNET_NO != fwd)
2082 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2084 /* If just created, cancel the short timeout and start a long one */
2085 if (CADET_CONNECTION_SENT == oldstate)
2087 c->create_retry = 1;
2088 connection_reset_timeout (c, GNUNET_YES);
2091 /* Change connection state, send ACK */
2092 connection_change_state (c, CADET_CONNECTION_READY);
2093 send_connection_ack (c, GNUNET_YES);
2095 /* Change tunnel state, trigger KX */
2096 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2097 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2098 GCC_check_connections ();
2102 /* Message for us as destination? */
2103 if (GCC_is_terminal (c, GNUNET_YES))
2105 if (GNUNET_YES != fwd)
2110 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2112 /* If just created, cancel the short timeout and start a long one */
2113 if (CADET_CONNECTION_ACK == oldstate)
2114 connection_reset_timeout (c, GNUNET_NO);
2116 /* Change tunnel state */
2117 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2118 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2119 GCC_check_connections ();
2123 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2124 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2125 GNUNET_YES, NULL, NULL);
2127 GCC_check_connections ();
2132 * Handler for notifications of broken connections.
2134 * @param peer Message sender (neighbor).
2135 * @param msg Message itself.
2138 GCC_handle_broken (struct CadetPeer *peer,
2139 const struct GNUNET_CADET_ConnectionBroken *msg)
2141 struct CadetConnection *c;
2142 struct CadetTunnel *t;
2145 GCC_check_connections ();
2146 log_message (&msg->header, peer, &msg->cid);
2147 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", GNUNET_i2s (&msg->peer1));
2148 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", GNUNET_i2s (&msg->peer2));
2149 c = connection_get (&msg->cid);
2152 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2153 GNUNET_STATISTICS_update (stats, "# duplicate CONNECTION_BROKEN",
2155 GCC_check_connections ();
2161 fwd = is_fwd (c, peer);
2162 if (GNUNET_SYSERR == fwd)
2164 GNUNET_break_op (0);
2165 GCC_check_connections ();
2169 if (GCC_is_terminal (c, fwd))
2171 struct CadetPeer *endpoint;
2175 /* A terminal connection should not have 't' set to NULL. */
2177 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2180 endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
2181 if (2 < c->path->length)
2182 path_invalidate (c->path);
2183 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2185 connection_change_state (c, CADET_CONNECTION_BROKEN);
2186 GCT_remove_connection (t, c);
2193 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2194 GNUNET_YES, NULL, NULL);
2195 connection_cancel_queues (c, !fwd);
2197 GCC_check_connections ();
2203 * Handler for notifications of destroyed connections.
2205 * @param peer Message sender (neighbor).
2206 * @param msg Message itself.
2209 GCC_handle_destroy (struct CadetPeer *peer,
2210 const struct GNUNET_CADET_ConnectionDestroy *msg)
2212 struct CadetConnection *c;
2215 GCC_check_connections ();
2216 log_message (&msg->header, peer, &msg->cid);
2217 c = connection_get (&msg->cid);
2220 /* Probably already got the message from another path,
2221 * destroyed the tunnel and retransmitted to children.
2224 GNUNET_STATISTICS_update (stats,
2225 "# control on unknown connection",
2227 LOG (GNUNET_ERROR_TYPE_DEBUG,
2228 " connection unknown destroyed: previously destroyed?\n");
2229 GCC_check_connections ();
2233 fwd = is_fwd (c, peer);
2234 if (GNUNET_SYSERR == fwd)
2236 GNUNET_break_op (0);
2237 GCC_check_connections ();
2241 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2243 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2244 GNUNET_YES, NULL, NULL);
2246 else if (0 == c->pending_messages)
2248 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
2250 GCC_check_connections ();
2256 GCT_remove_connection (c->t, c);
2259 GCC_check_connections ();
2265 * Handler for cadet network traffic hop-by-hop acks.
2267 * @param peer Message sender (neighbor).
2268 * @param msg Message itself.
2271 GCC_handle_ack (struct CadetPeer *peer,
2272 const struct GNUNET_CADET_ACK *msg)
2274 struct CadetConnection *c;
2275 struct CadetFlowControl *fc;
2279 GCC_check_connections ();
2280 log_message (&msg->header, peer, &msg->cid);
2281 c = connection_get (&msg->cid);
2284 GNUNET_STATISTICS_update (stats,
2285 "# ack on unknown connection",
2288 send_broken_unknown (&msg->cid,
2292 GCC_check_connections ();
2296 /* Is this a forward or backward ACK? */
2297 if (get_next_hop (c) == peer)
2302 else if (get_prev_hop (c) == peer)
2309 GNUNET_break_op (0);
2313 ack = ntohl (msg->ack);
2314 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2315 GC_f2s (fwd), ack, fc->last_ack_recv);
2316 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2317 fc->last_ack_recv = ack;
2319 /* Cancel polling if the ACK is big enough. */
2320 if (NULL != fc->poll_task &&
2321 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2323 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2324 GNUNET_SCHEDULER_cancel (fc->poll_task);
2325 fc->poll_task = NULL;
2326 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2329 GCC_check_connections ();
2334 * Handler for cadet network traffic hop-by-hop data counter polls.
2336 * @param peer Message sender (neighbor).
2337 * @param msg Message itself.
2340 GCC_handle_poll (struct CadetPeer *peer,
2341 const struct GNUNET_CADET_Poll *msg)
2343 struct CadetConnection *c;
2344 struct CadetFlowControl *fc;
2348 GCC_check_connections ();
2349 log_message (&msg->header, peer, &msg->cid);
2350 c = connection_get (&msg->cid);
2353 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2355 LOG (GNUNET_ERROR_TYPE_DEBUG,
2356 "POLL message on unknown connection %s!\n",
2357 GNUNET_h2s (GC_h2hc (&msg->cid)));
2358 send_broken_unknown (&msg->cid,
2362 GCC_check_connections ();
2366 /* Is this a forward or backward ACK?
2367 * Note: a poll should never be needed in a loopback case,
2368 * since there is no possiblility of packet loss there, so
2369 * this way of discerining FWD/BCK should not be a problem.
2371 if (get_next_hop (c) == peer)
2373 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2376 else if (get_prev_hop (c) == peer)
2378 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2383 GNUNET_break_op (0);
2387 pid = ntohl (msg->pid);
2388 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2389 fc->last_pid_recv = pid;
2390 fwd = fc == &c->bck_fc;
2391 GCC_send_ack (c, fwd, GNUNET_YES);
2392 GCC_check_connections ();
2397 * Check the message against internal state and test if it goes FWD or BCK.
2399 * Updates the PID, state and timeout values for the connection.
2401 * @param message Message to check. It must belong to an existing connection.
2402 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2403 * @param c Connection this message should belong. If NULL, check fails.
2404 * @param sender Neighbor that sent the message.
2406 * @return #GNUNET_YES if the message goes FWD.
2407 * #GNUNET_NO if it goes BCK.
2408 * #GNUNET_SYSERR if there is an error (unauthorized sender, ...).
2411 check_message (const struct GNUNET_MessageHeader *message,
2412 const struct GNUNET_CADET_Hash* cid,
2413 struct CadetConnection *c,
2414 struct CadetPeer *sender,
2417 struct CadetFlowControl *fc;
2418 struct CadetPeer *hop;
2422 /* Check connection */
2425 GNUNET_STATISTICS_update (stats,
2426 "# unknown connection",
2428 LOG (GNUNET_ERROR_TYPE_DEBUG,
2429 "%s on unknown connection %s\n",
2430 GC_m2s (ntohs (message->type)),
2431 GNUNET_h2s (GC_h2hc (cid)));
2432 send_broken_unknown (cid,
2436 return GNUNET_SYSERR;
2439 /* Check if origin is as expected */
2440 hop = get_prev_hop (c);
2447 hop = get_next_hop (c);
2448 GNUNET_break (hop == c->next_peer);
2455 /* Unexpected peer sending traffic on a connection. */
2456 GNUNET_break_op (0);
2457 return GNUNET_SYSERR;
2461 /* Check PID for payload messages */
2462 type = ntohs (message->type);
2463 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
2465 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2466 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2467 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2468 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2470 GNUNET_break_op (0);
2471 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2472 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2473 pid, fc->last_pid_recv, fc->last_ack_sent);
2474 return GNUNET_SYSERR;
2476 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2480 delta = pid - fc->last_pid_recv;
2481 fc->last_pid_recv = pid;
2482 fc->recv_bitmap <<= delta;
2483 fc->recv_bitmap |= 1;
2487 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2488 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2490 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2491 pid, fc->last_pid_recv - 31);
2492 return GNUNET_SYSERR;
2494 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2498 /* Count as connection confirmation. */
2499 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2501 connection_change_state (c, CADET_CONNECTION_READY);
2504 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2505 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2508 connection_reset_timeout (c, fwd);
2515 * Handler for key exchange traffic (Axolotl KX).
2517 * @param peer Message sender (neighbor).
2518 * @param msg Message itself.
2521 GCC_handle_kx (struct CadetPeer *peer,
2522 const struct GNUNET_CADET_KX *msg)
2524 const struct GNUNET_CADET_Hash* cid;
2525 struct CadetConnection *c;
2528 GCC_check_connections ();
2530 log_message (&msg->header, peer, cid);
2532 c = connection_get (cid);
2533 fwd = check_message (&msg->header,
2539 /* If something went wrong, discard message. */
2540 if (GNUNET_SYSERR == fwd)
2542 GNUNET_break_op (0);
2543 GCC_check_connections ();
2547 /* Is this message for us? */
2548 if (GCC_is_terminal (c, fwd))
2550 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2551 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2557 GCT_handle_kx (c->t, msg);
2558 GCC_check_connections ();
2562 /* Message not for us: forward to next hop */
2563 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2564 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2565 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2566 GNUNET_NO, NULL, NULL);
2567 GCC_check_connections ();
2572 * Handler for encrypted cadet network traffic (channel mgmt, data).
2574 * @param peer Message sender (neighbor).
2575 * @param msg Message itself.
2578 GCC_handle_encrypted (struct CadetPeer *peer,
2579 const struct GNUNET_CADET_Encrypted *msg)
2581 const struct GNUNET_CADET_Hash* cid;
2582 struct CadetConnection *c;
2586 GCC_check_connections ();
2588 pid = ntohl (msg->pid);
2589 log_message (&msg->header, peer, cid);
2591 c = connection_get (cid);
2592 fwd = check_message (&msg->header,
2598 /* If something went wrong, discard message. */
2599 if (GNUNET_SYSERR == fwd)
2601 GNUNET_break_op (0);
2602 GCC_check_connections ();
2606 /* Is this message for us? */
2607 if (GCC_is_terminal (c, fwd))
2609 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2613 GNUNET_break (GNUNET_NO != c->destroy);
2616 GCT_handle_encrypted (c->t, msg);
2617 GCC_send_ack (c, fwd, GNUNET_NO);
2618 GCC_check_connections ();
2622 /* Message not for us: forward to next hop */
2623 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2624 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2625 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2626 GNUNET_NO, NULL, NULL);
2627 GCC_check_connections ();
2632 * Initialize the connections subsystem
2634 * @param c Configuration handle.
2637 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2639 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2641 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2644 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2645 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2646 GNUNET_SCHEDULER_shutdown ();
2651 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2654 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2655 "CADET", "MAX_CONNECTIONS", "MISSING");
2656 GNUNET_SCHEDULER_shutdown ();
2661 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2662 &refresh_connection_time))
2664 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2665 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2666 GNUNET_SCHEDULER_shutdown ();
2669 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2670 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2675 * Destroy each connection on shutdown.
2677 * @param cls Closure (unused).
2678 * @param key Current key code (CID, unused).
2679 * @param value Value in the hash map (`struct CadetConnection`)
2681 * @return #GNUNET_YES, because we should continue to iterate
2684 shutdown_iterator (void *cls,
2685 const struct GNUNET_HashCode *key,
2688 struct CadetConnection *c = value;
2690 c->state = CADET_CONNECTION_DESTROYED;
2697 * Shut down the connections subsystem.
2702 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n");
2703 GCC_check_connections ();
2704 GNUNET_CONTAINER_multihashmap_iterate (connections,
2707 GNUNET_CONTAINER_multihashmap_destroy (connections);
2713 * Create a connection.
2715 * @param cid Connection ID (either created locally or imposed remotely).
2716 * @param t Tunnel this connection belongs to (or NULL for transit connections);
2717 * @param path Path this connection has to use (copy is made).
2718 * @param own_pos Own position in the @c path path.
2720 * @return Newly created connection.
2721 * NULL in case of error: own id not in path, wrong neighbors, ...
2723 struct CadetConnection *
2724 GCC_new (const struct GNUNET_CADET_Hash *cid,
2725 struct CadetTunnel *t,
2726 struct CadetPeerPath *path,
2727 unsigned int own_pos)
2729 struct CadetConnection *c;
2730 struct CadetPeerPath *cpath;
2732 GCC_check_connections ();
2733 cpath = path_duplicate (path);
2734 GNUNET_assert (NULL != cpath);
2735 c = GNUNET_new (struct CadetConnection);
2737 GNUNET_assert (GNUNET_OK ==
2738 GNUNET_CONTAINER_multihashmap_put (connections,
2740 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2741 fc_init (&c->fwd_fc);
2742 fc_init (&c->bck_fc);
2747 GNUNET_assert (own_pos <= cpath->length - 1);
2748 c->own_pos = own_pos;
2751 if (GNUNET_OK != register_neighbors (c))
2755 /* We were the origin of this request, this means we have invalid
2756 * info about the paths to reach the destination. We must invalidate
2757 * the *original* path to avoid trying it again in the next minute.
2759 if (2 < path->length)
2760 path_invalidate (path);
2764 GCT_debug(t, GNUNET_ERROR_TYPE_WARNING);
2768 path_destroy (c->path);
2773 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2774 GCC_check_connections ();
2780 * Connection is no longer needed: destroy it.
2782 * Cancels all pending traffic (including possible DESTROY messages), all
2783 * maintenance tasks and removes the connection from neighbor peers and tunnel.
2785 * @param c Connection to destroy.
2788 GCC_destroy (struct CadetConnection *c)
2790 GCC_check_connections ();
2797 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2798 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2801 LOG (GNUNET_ERROR_TYPE_DEBUG,
2802 "destroying connection %s\n",
2804 LOG (GNUNET_ERROR_TYPE_DEBUG,
2805 " fc's f: %p, b: %p\n",
2806 &c->fwd_fc, &c->bck_fc);
2807 LOG (GNUNET_ERROR_TYPE_DEBUG,
2808 " fc tasks f: %u, b: %u\n",
2809 c->fwd_fc.poll_task,
2810 c->bck_fc.poll_task);
2812 /* Cancel all traffic */
2813 if (NULL != c->path)
2815 connection_cancel_queues (c, GNUNET_YES);
2816 connection_cancel_queues (c, GNUNET_NO);
2817 if (NULL != c->maintenance_q)
2819 GCP_send_cancel (c->maintenance_q);
2820 c->maintenance_q = NULL;
2823 unregister_neighbors (c);
2824 path_destroy (c->path);
2827 /* Delete from tunnel */
2829 GCT_remove_connection (c->t, c);
2831 if (NULL != c->check_duplicates_task)
2832 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
2833 if (NULL != c->fwd_maintenance_task)
2834 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2835 if (NULL != c->bck_maintenance_task)
2836 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2838 if (GNUNET_NO == c->was_removed)
2840 GNUNET_break (GNUNET_YES ==
2841 GNUNET_CONTAINER_multihashmap_remove (connections,
2845 GNUNET_STATISTICS_update (stats,
2850 GCC_check_connections ();
2855 * Get the connection ID.
2857 * @param c Connection to get the ID from.
2859 * @return ID of the connection.
2861 const struct GNUNET_CADET_Hash *
2862 GCC_get_id (const struct CadetConnection *c)
2869 * Get the connection ID.
2871 * @param c Connection to get the ID from.
2873 * @return ID of the connection.
2875 const struct GNUNET_HashCode *
2876 GCC_get_h (const struct CadetConnection *c)
2878 return GC_h2hc (&c->id);
2883 * Get the connection path.
2885 * @param c Connection to get the path from.
2887 * @return path used by the connection.
2889 const struct CadetPeerPath *
2890 GCC_get_path (const struct CadetConnection *c)
2892 if (GNUNET_NO == c->destroy)
2899 * Get the connection state.
2901 * @param c Connection to get the state from.
2903 * @return state of the connection.
2905 enum CadetConnectionState
2906 GCC_get_state (const struct CadetConnection *c)
2912 * Get the connection tunnel.
2914 * @param c Connection to get the tunnel from.
2916 * @return tunnel of the connection.
2918 struct CadetTunnel *
2919 GCC_get_tunnel (const struct CadetConnection *c)
2926 * Get free buffer space in a connection.
2928 * @param c Connection.
2929 * @param fwd Is query about FWD traffic?
2931 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2934 GCC_get_buffer (struct CadetConnection *c, int fwd)
2936 struct CadetFlowControl *fc;
2938 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2940 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
2941 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
2942 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
2944 return (fc->queue_max - fc->queue_n);
2949 * Get how many messages have we allowed to send to us from a direction.
2951 * @param c Connection.
2952 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2954 * @return last_ack_sent - last_pid_recv
2957 GCC_get_allowed (struct CadetConnection *c, int fwd)
2959 struct CadetFlowControl *fc;
2961 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2962 if (CADET_CONNECTION_READY != c->state
2963 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
2967 return (fc->last_ack_sent - fc->last_pid_recv);
2972 * Get messages queued in a connection.
2974 * @param c Connection.
2975 * @param fwd Is query about FWD traffic?
2977 * @return Number of messages queued.
2980 GCC_get_qn (struct CadetConnection *c, int fwd)
2982 struct CadetFlowControl *fc;
2984 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2991 * Get next PID to use.
2993 * @param c Connection.
2994 * @param fwd Is query about FWD traffic?
2996 * @return Next PID to use.
2999 GCC_get_pid (struct CadetConnection *c, int fwd)
3001 struct CadetFlowControl *fc;
3004 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3012 * Allow the connection to advertise a buffer of the given size.
3014 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3015 * allowing up to last_pid_recv + buffer.
3017 * @param c Connection.
3018 * @param buffer How many more messages the connection can accept.
3019 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3022 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3024 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3025 GCC_2s (c), buffer, GC_f2s (fwd));
3026 send_ack (c, buffer, fwd, GNUNET_NO);
3031 * Notify other peers on a connection of a broken link. Mark connections
3032 * to destroy after all traffic has been sent.
3034 * @param c Connection on which there has been a disconnection.
3035 * @param peer Peer that disconnected.
3038 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
3040 struct CadetFlowControl *fc;
3044 GCC_check_connections ();
3045 strncpy (peer_name, GCP_2s (peer), 16);
3046 peer_name[15] = '\0';
3047 LOG (GNUNET_ERROR_TYPE_DEBUG,
3048 "shutting down %s, %s disconnected\n",
3049 GCC_2s (c), peer_name);
3051 invalidate_paths (c, peer);
3053 fwd = is_fwd (c, peer);
3054 if (GNUNET_SYSERR == fwd)
3059 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3060 (GNUNET_NO != c->destroy) )
3062 /* Local shutdown, or other peer already down (hence 'c->destroy');
3063 so there is no one to notify about this, just clean up. */
3065 GCC_check_connections ();
3068 /* Mark FlowControl towards the peer as unavaliable. */
3069 fc = fwd ? &c->bck_fc : &c->fwd_fc;
3072 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3074 /* Connection will have at least one pending message
3075 * (the one we just scheduled), so delay destruction
3076 * and remove from map so we don't use accidentally. */
3078 GNUNET_assert (GNUNET_NO == c->was_removed);
3079 c->was_removed = GNUNET_YES;
3080 GNUNET_break (GNUNET_YES ==
3081 GNUNET_CONTAINER_multihashmap_remove (connections,
3084 /* Cancel queue in the direction that just died. */
3085 connection_cancel_queues (c, ! fwd);
3086 GCC_stop_poll (c, ! fwd);
3087 unregister_neighbors (c);
3088 GCC_check_connections ();
3093 * Is this peer the first one on the connection?
3095 * @param c Connection.
3096 * @param fwd Is this about fwd traffic?
3098 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3101 GCC_is_origin (struct CadetConnection *c, int fwd)
3103 if (!fwd && c->path->length - 1 == c->own_pos )
3105 if (fwd && 0 == c->own_pos)
3112 * Is this peer the last one on the connection?
3114 * @param c Connection.
3115 * @param fwd Is this about fwd traffic?
3116 * Note that the ROOT is the terminal for BCK traffic!
3118 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3121 GCC_is_terminal (struct CadetConnection *c, int fwd)
3123 return GCC_is_origin (c, ! fwd);
3128 * See if we are allowed to send by the next hop in the given direction.
3130 * @param c Connection.
3131 * @param fwd Is this about fwd traffic?
3133 * @return #GNUNET_YES in case it's OK to send.
3136 GCC_is_sendable (struct CadetConnection *c, int fwd)
3138 struct CadetFlowControl *fc;
3140 LOG (GNUNET_ERROR_TYPE_DEBUG,
3141 " checking sendability of %s traffic on %s\n",
3142 GC_f2s (fwd), GCC_2s (c));
3148 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3149 LOG (GNUNET_ERROR_TYPE_DEBUG,
3150 " last ack recv: %u, last pid sent: %u\n",
3151 fc->last_ack_recv, fc->last_pid_sent);
3152 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3154 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3157 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3163 * Check if this connection is a direct one (never trim a direct connection).
3165 * @param c Connection.
3167 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3170 GCC_is_direct (struct CadetConnection *c)
3172 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3177 * Sends a completely built message on a connection, properly registering
3178 * all used resources.
3180 * @param message Message to send.
3181 * @param payload_type Type of payload, in case the message is encrypted.
3182 * 0 for restransmissions (when type is no longer known)
3183 * UINT16_MAX when not applicable.
3184 * @param payload_id ID of the payload (PID, ACK, ...).
3185 * @param c Connection on which this message is transmitted.
3186 * @param fwd Is this a fwd message?
3187 * @param force Force the connection to accept the message (buffer overfill).
3188 * @param cont Continuation called once message is sent. Can be NULL.
3189 * @param cont_cls Closure for @c cont.
3191 * @return Handle to cancel the message before it's sent.
3193 * Invalid on @c cont call.
3195 struct CadetConnectionQueue *
3196 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3197 uint16_t payload_type, uint32_t payload_id,
3198 struct CadetConnection *c, int fwd, int force,
3199 GCC_sent cont, void *cont_cls)
3201 struct CadetFlowControl *fc;
3202 struct CadetConnectionQueue *q;
3206 size = ntohs (message->size);
3207 type = ntohs (message->type);
3209 GCC_check_connections ();
3210 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3211 if (0 == fc->queue_max)
3217 LOG (GNUNET_ERROR_TYPE_INFO,
3218 "--> %s (%s %4u) on conn %s (%p) %s [%5u]\n",
3219 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), c,
3223 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3224 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, PIDsnt: %u, ACKrcv: %u\n",
3225 fc, fc->queue_n, fc->last_pid_sent, fc->last_ack_recv);
3226 if (GNUNET_NO == force)
3232 case GNUNET_MESSAGE_TYPE_CADET_KX:
3233 /* nothing to do here */
3236 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3237 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3238 /* Should've only be used for restransmissions. */
3239 GNUNET_break (0 == payload_type);
3242 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3243 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3244 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3245 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3246 GNUNET_assert (GNUNET_YES == force);
3254 if (fc->queue_n > fc->queue_max && GNUNET_NO == force)
3256 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3259 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3260 fc->queue_n, fc->queue_max);
3261 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3265 return NULL; /* Drop this message */
3268 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3269 GCC_2s (c), c->pending_messages);
3270 c->pending_messages++;
3272 q = GNUNET_new (struct CadetConnectionQueue);
3274 q->peer_q = GCP_send (get_hop (c, fwd), message,
3275 payload_type, payload_id,
3277 &conn_message_sent, q);
3278 if (NULL == q->peer_q)
3280 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3282 GCC_check_connections ();
3286 q->cont_cls = cont_cls;
3287 GNUNET_CONTAINER_DLL_insert (fc->q_head, fc->q_tail, q);
3288 GCC_check_connections ();
3294 * Cancel a previously sent message while it's in the queue.
3296 * ONLY can be called before the continuation given to the send function
3297 * is called. Once the continuation is called, the message is no longer in the
3300 * @param q Handle to the queue.
3303 GCC_cancel (struct CadetConnectionQueue *q)
3305 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3307 /* send_cancel calls message_sent, which calls q->cont and frees q */
3308 GCP_send_cancel (q->peer_q);
3309 GCC_check_connections ();
3314 * Sends a CREATE CONNECTION message for a path to a peer.
3315 * Changes the connection and tunnel states if necessary.
3317 * @param c Connection to create.
3320 GCC_send_create (struct CadetConnection *c)
3322 enum CadetTunnelCState state;
3325 GCC_check_connections ();
3326 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3327 size += c->path->length * sizeof (struct GNUNET_PeerIdentity);
3329 /* Allocate message on the stack */
3330 unsigned char cbuf[size];
3331 struct GNUNET_CADET_ConnectionCreate *msg;
3332 struct GNUNET_PeerIdentity *peers;
3334 msg = (struct GNUNET_CADET_ConnectionCreate *) cbuf;
3335 msg->header.size = htons (size);
3336 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
3337 msg->reserved = htonl (0);
3338 msg->cid = *GCC_get_id (c);
3339 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3340 for (int i = 0; i < c->path->length; i++)
3342 GNUNET_PEER_resolve (c->path->peers[i], peers++);
3344 GNUNET_assert (NULL == c->maintenance_q);
3345 c->maintenance_q = GCP_send (get_next_hop (c),
3347 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0,
3349 &conn_message_sent, NULL);
3352 LOG (GNUNET_ERROR_TYPE_INFO, "==> %s %19s on conn %s (%p) FWD [%5u]\n",
3353 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE), "",
3354 GCC_2s (c), c, size);
3355 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3356 c, c->pending_messages);
3357 c->pending_messages++;
3359 state = GCT_get_cstate (c->t);
3360 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3361 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
3362 if (CADET_CONNECTION_NEW == c->state)
3363 connection_change_state (c, CADET_CONNECTION_SENT);
3364 GCC_check_connections ();
3369 * Send an ACK on the appropriate connection/channel, depending on
3370 * the direction and the position of the peer.
3372 * @param c Which connection to send the hop-by-hop ACK.
3373 * @param fwd Is this a fwd ACK? (will go dest->root).
3374 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
3377 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
3379 unsigned int buffer;
3381 GCC_check_connections ();
3382 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
3383 GC_f2s (fwd), GCC_2s (c));
3391 if (GNUNET_NO != c->destroy)
3393 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
3394 GCC_check_connections ();
3398 /* Get available buffer space */
3399 if (GCC_is_terminal (c, fwd))
3401 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
3402 buffer = GCT_get_channels_buffer (c->t);
3406 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
3407 buffer = GCC_get_buffer (c, fwd);
3409 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
3410 if (0 == buffer && GNUNET_NO == force)
3412 GCC_check_connections ();
3416 /* Send available buffer space */
3417 if (GNUNET_YES == GCC_is_origin (c, fwd))
3419 GNUNET_assert (NULL != c->t);
3420 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
3421 GCT_unchoke_channels (c->t);
3425 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
3426 send_ack (c, buffer, fwd, force);
3428 GCC_check_connections ();
3433 * Send a message to all peers in this connection that the connection
3434 * is no longer valid.
3436 * If some peer should not receive the message, it should be zero'ed out
3437 * before calling this function.
3439 * @param c The connection whose peers to notify.
3442 GCC_send_destroy (struct CadetConnection *c)
3444 struct GNUNET_CADET_ConnectionDestroy msg;
3446 if (GNUNET_YES == c->destroy)
3448 GCC_check_connections ();
3449 msg.header.size = htons (sizeof (msg));
3450 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3452 msg.reserved = htonl (0);
3453 LOG (GNUNET_ERROR_TYPE_DEBUG,
3454 " sending connection destroy for connection %s\n",
3457 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3458 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c,
3459 GNUNET_YES, GNUNET_YES, NULL, NULL);
3460 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3461 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c,
3462 GNUNET_NO, GNUNET_YES, NULL, NULL);
3464 GCC_check_connections ();
3469 * @brief Start a polling timer for the connection.
3471 * When a neighbor does not accept more traffic on the connection it could be
3472 * caused by a simple congestion or by a lost ACK. Polling enables to check
3473 * for the lastest ACK status for a connection.
3475 * @param c Connection.
3476 * @param fwd Should we poll in the FWD direction?
3479 GCC_start_poll (struct CadetConnection *c, int fwd)
3481 struct CadetFlowControl *fc;
3483 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3484 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3486 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3488 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL already in progress (t: %p, m: %p)\n",
3489 fc->poll_task, fc->poll_msg);
3492 if (0 == fc->queue_max)
3494 /* Should not be needed, traffic should've been cancelled. */
3496 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not possible, peer disconnected\n");
3499 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3500 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time, &send_poll, fc);
3505 * @brief Stop polling a connection for ACKs.
3507 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3509 * @param c Connection.
3510 * @param fwd Should we stop the poll in the FWD direction?
3513 GCC_stop_poll (struct CadetConnection *c, int fwd)
3515 struct CadetFlowControl *fc;
3517 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3518 if (NULL != fc->poll_task)
3520 GNUNET_SCHEDULER_cancel (fc->poll_task);
3521 fc->poll_task = NULL;
3523 if (NULL != fc->poll_msg)
3525 GCC_cancel (fc->poll_msg);
3526 fc->poll_msg = NULL;
3532 * Get a (static) string for a connection.
3534 * @param c Connection.
3537 GCC_2s (const struct CadetConnection *c)
3544 static char buf[128];
3546 SPRINTF (buf, "%s (->%s)",
3547 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3550 return GNUNET_h2s (GC_h2hc (&c->id));
3555 * Log all possible info about the connection state.
3557 * @param c Connection to debug.
3558 * @param level Debug level to use.
3561 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3566 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3568 __FILE__, __FUNCTION__, __LINE__);
3574 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3578 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3579 s = path_2s (c->path);
3580 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3582 LOG2 (level, "CCC state: %s, destroy: %u\n",
3583 GCC_state2s (c->state), c->destroy);
3584 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3585 if (NULL != c->perf)
3586 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3588 LOG2 (level, "CCC FWD flow control:\n");
3589 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3590 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3591 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3592 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3593 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3594 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3595 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3596 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3598 LOG2 (level, "CCC BCK flow control:\n");
3599 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3600 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3601 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3602 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3603 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3604 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3605 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3606 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3608 LOG2 (level, "CCC DEBUG CONNECTION END\n");