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 is not allowed to send
136 * messages with PIDs higher than this value).
138 uint32_t last_ack_sent;
141 * Last ACK sent towards the origin (for traffic towards leaf node).
143 uint32_t last_ack_recv;
146 * Task to poll the peer in case of a lost ACK causes stall.
148 struct GNUNET_SCHEDULER_Task *poll_task;
151 * How frequently to poll for ACKs.
153 struct GNUNET_TIME_Relative poll_time;
156 * Queued poll message, to cancel if not necessary anymore (got ACK).
158 struct CadetConnectionQueue *poll_msg;
161 * Queued poll message, to cancel if not necessary anymore (got ACK).
163 struct CadetConnectionQueue *ack_msg;
167 * Keep a record of the last messages sent on this connection.
169 struct CadetConnectionPerformance
172 * Circular buffer for storing measurements.
174 double usecsperbyte[AVG_MSGS];
177 * Running average of @c usecsperbyte.
182 * How many values of @c usecsperbyte are valid.
187 * Index of the next "free" position in @c usecsperbyte.
194 * Struct containing all information regarding a connection to a peer.
196 struct CadetConnection
199 * Tunnel this connection is part of.
201 struct CadetTunnel *t;
204 * Flow control information for traffic fwd.
206 struct CadetFlowControl fwd_fc;
209 * Flow control information for traffic bck.
211 struct CadetFlowControl bck_fc;
214 * Measure connection performance on the endpoint.
216 struct CadetConnectionPerformance *perf;
219 * ID of the connection.
221 struct GNUNET_CADET_ConnectionTunnelIdentifier id;
224 * Path being used for the tunnel. At the origin of the connection
225 * it's a pointer to the destination's path pool, otherwise just a copy.
227 struct CadetPeerPath *path;
230 * Task to keep the used paths alive at the owner,
231 * time tunnel out on all the other peers.
233 struct GNUNET_SCHEDULER_Task *fwd_maintenance_task;
236 * Task to keep the used paths alive at the destination,
237 * time tunnel out on all the other peers.
239 struct GNUNET_SCHEDULER_Task *bck_maintenance_task;
242 * Queue handle for maintainance traffic. One handle for FWD and BCK since
243 * one peer never needs to maintain both directions (no loopback connections).
245 struct CadetPeerQueue *maintenance_q;
248 * Should equal #get_next_hop(), or NULL if that peer disconnected.
250 struct CadetPeer *next_peer;
253 * Should equal #get_prev_hop(), or NULL if that peer disconnected.
255 struct CadetPeer *prev_peer;
258 * State of the connection.
260 enum CadetConnectionState state;
263 * Position of the local peer in the path.
265 unsigned int own_pos;
268 * Pending message count.
270 unsigned int pending_messages;
274 * - if 0, connection in use.
275 * - if 1, destroy on last message.
276 * - if 2, connection is being destroyed don't re-enter.
281 * In-connection-map flag. Sometimes, when @e destroy is set but
282 * actual destruction is delayed to enable us to finish processing
283 * queues (i.e. in the direction that is still working), we remove
284 * the connection from the map to prevent it from still being
285 * found (and used) by accident. This flag is set to #GNUNET_YES
286 * for a connection that is not in the #connections map. Should
287 * only be #GNUNET_YES if #destroy is also non-zero.
292 * Counter to do exponential backoff when creating a connection (max 64).
294 unsigned short create_retry;
297 * Task to check if connection has duplicates.
299 struct GNUNET_SCHEDULER_Task *check_duplicates_task;
303 /******************************************************************************/
304 /******************************* GLOBALS ***********************************/
305 /******************************************************************************/
308 * Global handle to the statistics service.
310 extern struct GNUNET_STATISTICS_Handle *stats;
313 * Local peer own ID (memory efficient handle).
315 extern GNUNET_PEER_Id myid;
318 * Local peer own ID (full value).
320 extern struct GNUNET_PeerIdentity my_full_id;
323 * Connections known, indexed by cid (CadetConnection).
325 static struct GNUNET_CONTAINER_MultiHashMap *connections;
328 * How many connections are we willing to maintain.
329 * Local connections are always allowed,
330 * even if there are more connections than max.
332 static unsigned long long max_connections;
335 * How many messages *in total* are we willing to queue, divide by number of
336 * connections to get connection queue size.
338 static unsigned long long max_msgs_queue;
341 * How often to send path keepalives. Paths timeout after 4 missed.
343 static struct GNUNET_TIME_Relative refresh_connection_time;
346 * How often to send path create / ACKs.
348 static struct GNUNET_TIME_Relative create_connection_time;
351 /******************************************************************************/
352 /******************************** STATIC ***********************************/
353 /******************************************************************************/
357 #if 0 // avoid compiler warning for unused static function
359 fc_debug (struct CadetFlowControl *fc)
361 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
362 fc->last_pid_recv, fc->last_ack_sent);
363 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
364 fc->last_pid_sent, fc->last_ack_recv);
365 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
366 fc->queue_n, fc->queue_max);
370 connection_debug (struct CadetConnection *c)
374 LOG (GNUNET_ERROR_TYPE_INFO, "DEBUG NULL CONNECTION\n");
377 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
378 peer2s (c->t->peer), GCC_2s (c));
379 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
380 c->state, c->pending_messages);
381 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
382 fc_debug (&c->fwd_fc);
383 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
384 fc_debug (&c->bck_fc);
390 * Schedule next keepalive task, taking in consideration
391 * the connection state and number of retries.
393 * @param c Connection for which to schedule the next keepalive.
394 * @param fwd Direction for the next keepalive.
397 schedule_next_keepalive (struct CadetConnection *c, int fwd);
401 * Resets the connection timeout task, some other message has done the
403 * - For the first peer on the direction this means to send
404 * a keepalive or a path confirmation message (either create or ACK).
405 * - For all other peers, this means to destroy the connection,
406 * due to lack of activity.
407 * Starts the timeout if no timeout was running (connection just created).
409 * @param c Connection whose timeout to reset.
410 * @param fwd Is this forward?
413 connection_reset_timeout (struct CadetConnection *c, int fwd);
417 * Get string description for tunnel state. Reentrant.
419 * @param s Tunnel state.
421 * @return String representation.
424 GCC_state2s (enum CadetConnectionState s)
428 case CADET_CONNECTION_NEW:
429 return "CADET_CONNECTION_NEW";
430 case CADET_CONNECTION_SENT:
431 return "CADET_CONNECTION_SENT";
432 case CADET_CONNECTION_ACK:
433 return "CADET_CONNECTION_ACK";
434 case CADET_CONNECTION_READY:
435 return "CADET_CONNECTION_READY";
436 case CADET_CONNECTION_DESTROYED:
437 return "CADET_CONNECTION_DESTROYED";
438 case CADET_CONNECTION_BROKEN:
439 return "CADET_CONNECTION_BROKEN";
442 LOG (GNUNET_ERROR_TYPE_ERROR, " conn state %u unknown!\n", s);
443 return "CADET_CONNECTION_STATE_ERROR";
449 * Initialize a Flow Control structure to the initial state.
451 * @param fc Flow Control structure to initialize.
454 fc_init (struct CadetFlowControl *fc)
456 fc->next_pid = (uint32_t) 0;
457 fc->last_pid_sent = (uint32_t) -1;
458 fc->last_pid_recv = (uint32_t) -1;
459 fc->last_ack_sent = (uint32_t) 0;
460 fc->last_ack_recv = (uint32_t) 0;
461 fc->poll_task = NULL;
462 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
464 fc->queue_max = (max_msgs_queue / max_connections) + 1;
471 * @param cid Connection ID.
473 * @return conntection with the given ID @cid or NULL if not found.
475 static struct CadetConnection *
476 connection_get (const struct GNUNET_CADET_Hash *cid)
478 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
483 * Change the connection state. Cannot change a connection marked as destroyed.
485 * @param c Connection to change.
486 * @param state New state to set.
489 connection_change_state (struct CadetConnection* c,
490 enum CadetConnectionState state)
492 LOG (GNUNET_ERROR_TYPE_DEBUG,
493 "Connection %s state %s -> %s\n",
494 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
495 if (CADET_CONNECTION_DESTROYED <= c->state) /* Destroyed or broken. */
497 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
501 if (CADET_CONNECTION_READY == state)
507 * Mark a connection as "destroyed", to send all pending traffic and freeing
508 * all associated resources, without accepting new status changes on it.
510 * @param c Connection to mark as destroyed.
513 mark_destroyed (struct CadetConnection *c)
515 c->destroy = GNUNET_YES;
516 connection_change_state (c, CADET_CONNECTION_DESTROYED);
521 * Function called if a connection has been stalled for a while,
522 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
524 * @param cls Closure (poll ctx).
527 send_poll (void *cls);
531 * Send an ACK on the connection, informing the predecessor about
532 * the available buffer space. Should not be called in case the peer
533 * is origin (no predecessor) in the @c fwd direction.
535 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
536 * the ACK itself goes "back" (dest->root).
538 * @param c Connection on which to send the ACK.
539 * @param buffer How much space free to advertise?
540 * @param fwd Is this FWD ACK? (Going dest -> root)
541 * @param force Don't optimize out.
544 send_ack (struct CadetConnection *c,
549 struct CadetFlowControl *next_fc;
550 struct CadetFlowControl *prev_fc;
551 struct GNUNET_CADET_ACK msg;
555 GCC_check_connections ();
556 GNUNET_assert (GNUNET_NO == GCC_is_origin (c, fwd));
558 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
559 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
561 LOG (GNUNET_ERROR_TYPE_DEBUG, "send %s ack on %s\n",
562 GC_f2s (fwd), GCC_2s (c));
564 /* Check if we need to transmit the ACK. */
565 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
566 if (3 < delta && buffer < delta && GNUNET_NO == force)
568 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, delta > 3\n");
569 LOG (GNUNET_ERROR_TYPE_DEBUG,
570 " last pid recv: %u, last ack sent: %u\n",
571 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
572 GCC_check_connections ();
576 /* Ok, ACK might be necessary, what PID to ACK? */
577 ack = prev_fc->last_pid_recv + buffer;
578 LOG (GNUNET_ERROR_TYPE_DEBUG,
579 " ACK %u, last PID %u, last ACK %u, qmax %u, q %u\n",
580 ack, prev_fc->last_pid_recv, prev_fc->last_ack_sent,
581 next_fc->queue_max, next_fc->queue_n);
582 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
584 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
585 GCC_check_connections ();
589 /* Check if message is already in queue */
590 if (NULL != prev_fc->ack_msg)
592 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
594 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
595 GCC_cancel (prev_fc->ack_msg);
596 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
600 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
601 GCC_check_connections ();
605 GNUNET_break (GC_is_pid_bigger (ack,
606 prev_fc->last_ack_sent));
607 prev_fc->last_ack_sent = ack;
609 /* Build ACK message and send on conn */
610 msg.header.size = htons (sizeof (msg));
611 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
612 msg.ack = htonl (ack);
615 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header,
620 GNUNET_assert (NULL != prev_fc->ack_msg);
621 GCC_check_connections ();
626 * Update performance information if we are a connection's endpoint.
628 * @param c Connection to update.
629 * @param wait How much time did we wait to send the last message.
630 * @param size Size of the last message.
633 update_perf (struct CadetConnection *c,
634 struct GNUNET_TIME_Relative wait,
637 struct CadetConnectionPerformance *p;
641 return; /* Only endpoints are interested in timing. */
644 usecsperbyte = ((double) wait.rel_value_us) / size;
645 if (p->size == AVG_MSGS)
647 /* Array is full. Substract oldest value, add new one and store. */
648 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
649 p->usecsperbyte[p->idx] = usecsperbyte;
650 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
654 /* Array not yet full. Add current value to avg and store. */
655 p->usecsperbyte[p->idx] = usecsperbyte;
657 p->avg += p->usecsperbyte[p->idx];
661 p->idx = (p->idx + 1) % AVG_MSGS;
666 * Callback called when a connection queued message is sent.
668 * Calculates the average time and connection packet tracking.
670 * @param cls Closure (ConnectionQueue Handle), can be NULL.
671 * @param c Connection this message was on.
672 * @param fwd Was this a FWD going message?
673 * @param sent Was it really sent? (Could have been canceled)
674 * @param type Type of message sent.
675 * @param payload_type Type of payload, if applicable.
676 * @param pid Message ID, or 0 if not applicable (create, destroy, etc).
677 * @param size Size of the message.
678 * @param wait Time spent waiting for core (only the time for THIS message)
681 conn_message_sent (void *cls,
682 struct CadetConnection *c, int fwd, int sent,
683 uint16_t type, uint16_t payload_type, uint32_t pid,
685 struct GNUNET_TIME_Relative wait)
687 struct CadetConnectionQueue *q = cls;
688 struct CadetFlowControl *fc;
691 GCC_check_connections ();
692 LOG (GNUNET_ERROR_TYPE_INFO,
693 ">>> %s (%s %4u) on conn %s (%p) %s [%5u] in queue %s\n",
694 GC_m2s (type), GC_m2s (payload_type), pid, GCC_2s (c), c,
696 GNUNET_STRINGS_relative_time_to_string (wait, GNUNET_YES));
698 /* If c is NULL, nothing to update. */
701 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
702 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
704 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
707 GCC_check_connections ();
711 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
712 sent ? "" : "not ", GC_f2s (fwd),
713 GC_m2s (type), GC_m2s (payload_type), pid);
714 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
716 /* Update flow control info. */
717 fc = fwd ? &c->fwd_fc : &c->bck_fc;
721 GNUNET_CONTAINER_DLL_remove (fc->q_head, fc->q_tail, q);
725 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
726 q->cont (q->cont_cls, c, q, type, fwd, size);
730 else /* CONN_CREATE or CONN_ACK */
732 GNUNET_assert (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED != type);
736 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
737 c->pending_messages--;
738 if ( (GNUNET_YES == c->destroy) &&
739 (0 == c->pending_messages) )
741 LOG (GNUNET_ERROR_TYPE_DEBUG,
742 "! destroying connection!\n");
744 GCC_check_connections ();
748 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
751 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
752 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
753 c->maintenance_q = NULL;
754 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
755 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
756 schedule_next_keepalive (c, fwd);
759 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
760 if (GNUNET_YES == sent)
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_ConnectionTunnelIdentifier *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_saturating_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_INFO,
1359 "next keepalive for %s in in %s\n",
1360 GCC_2s (c), 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_saturating_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_saturating_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_ConnectionTunnelIdentifier *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_ConnectionTunnelIdentifier *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_ConnectionTunnelIdentifier* 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 GNUNET_break_op (0);
2433 send_broken_unknown (cid,
2437 return GNUNET_SYSERR;
2440 /* Check if origin is as expected */
2441 hop = get_prev_hop (c);
2448 hop = get_next_hop (c);
2449 GNUNET_break (hop == c->next_peer);
2456 /* Unexpected peer sending traffic on a connection. */
2457 GNUNET_break_op (0);
2458 return GNUNET_SYSERR;
2462 /* Check PID for payload messages */
2463 type = ntohs (message->type);
2464 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
2466 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2467 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected in interval [%u,%u])\n",
2469 fc->last_pid_recv + 1,
2471 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2473 GNUNET_STATISTICS_update (stats,
2474 "# unsolicited message",
2477 LOG (GNUNET_ERROR_TYPE_WARNING,
2478 "Received PID %u, (prev %u), ACK %u\n",
2479 pid, fc->last_pid_recv, fc->last_ack_sent);
2480 return GNUNET_SYSERR;
2482 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2486 delta = pid - fc->last_pid_recv;
2487 fc->last_pid_recv = pid;
2488 fc->recv_bitmap <<= delta;
2489 fc->recv_bitmap |= 1;
2493 GNUNET_STATISTICS_update (stats,
2494 "# out of order PID",
2497 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv,
2501 LOG (GNUNET_ERROR_TYPE_WARNING,
2502 "PID %u unexpected (%u+), dropping!\n",
2503 pid, fc->last_pid_recv - 31);
2504 return GNUNET_SYSERR;
2506 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2510 /* Count as connection confirmation. */
2511 if ( (CADET_CONNECTION_SENT == c->state) ||
2512 (CADET_CONNECTION_ACK == c->state) )
2514 connection_change_state (c, CADET_CONNECTION_READY);
2517 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2518 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2521 connection_reset_timeout (c, fwd);
2528 * Handler for key exchange traffic (Axolotl KX).
2530 * @param peer Message sender (neighbor).
2531 * @param msg Message itself.
2534 GCC_handle_kx (struct CadetPeer *peer,
2535 const struct GNUNET_CADET_KX *msg)
2537 const struct GNUNET_CADET_ConnectionTunnelIdentifier* cid;
2538 struct CadetConnection *c;
2541 GCC_check_connections ();
2543 log_message (&msg->header, peer, cid);
2545 c = connection_get (cid);
2546 fwd = check_message (&msg->header,
2552 /* If something went wrong, discard message. */
2553 if (GNUNET_SYSERR == fwd)
2555 GNUNET_break_op (0);
2556 GCC_check_connections ();
2560 /* Is this message for us? */
2561 if (GCC_is_terminal (c, fwd))
2563 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2564 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2570 GCT_handle_kx (c->t, msg);
2571 GCC_check_connections ();
2575 /* Message not for us: forward to next hop */
2576 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2577 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2578 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2579 GNUNET_NO, NULL, NULL);
2580 GCC_check_connections ();
2585 * Handler for encrypted cadet network traffic (channel mgmt, data).
2587 * @param peer Message sender (neighbor).
2588 * @param msg Message itself.
2591 GCC_handle_encrypted (struct CadetPeer *peer,
2592 const struct GNUNET_CADET_Encrypted *msg)
2594 const struct GNUNET_CADET_ConnectionTunnelIdentifier* cid;
2595 struct CadetConnection *c;
2599 GCC_check_connections ();
2601 pid = ntohl (msg->pid);
2602 log_message (&msg->header, peer, cid);
2604 c = connection_get (cid);
2605 fwd = check_message (&msg->header,
2611 /* If something went wrong, discard message. */
2612 if (GNUNET_SYSERR == fwd)
2614 GCC_check_connections ();
2618 /* Is this message for us? */
2619 if (GCC_is_terminal (c, fwd))
2621 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2625 GNUNET_break (GNUNET_NO != c->destroy);
2628 GCT_handle_encrypted (c->t, msg);
2629 GCC_send_ack (c, fwd, GNUNET_NO);
2630 GCC_check_connections ();
2634 /* Message not for us: forward to next hop */
2635 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2636 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2637 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2638 GNUNET_NO, NULL, NULL);
2639 GCC_check_connections ();
2644 * Initialize the connections subsystem
2646 * @param c Configuration handle.
2649 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2651 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2653 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2656 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2657 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2658 GNUNET_SCHEDULER_shutdown ();
2663 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2666 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2667 "CADET", "MAX_CONNECTIONS", "MISSING");
2668 GNUNET_SCHEDULER_shutdown ();
2673 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2674 &refresh_connection_time))
2676 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2677 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2678 GNUNET_SCHEDULER_shutdown ();
2681 create_connection_time = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
2682 refresh_connection_time);
2683 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2688 * Destroy each connection on shutdown.
2690 * @param cls Closure (unused).
2691 * @param key Current key code (CID, unused).
2692 * @param value Value in the hash map (`struct CadetConnection`)
2694 * @return #GNUNET_YES, because we should continue to iterate
2697 shutdown_iterator (void *cls,
2698 const struct GNUNET_HashCode *key,
2701 struct CadetConnection *c = value;
2703 c->state = CADET_CONNECTION_DESTROYED;
2710 * Shut down the connections subsystem.
2715 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n");
2716 GCC_check_connections ();
2717 GNUNET_CONTAINER_multihashmap_iterate (connections,
2720 GNUNET_CONTAINER_multihashmap_destroy (connections);
2726 * Create a connection.
2728 * @param cid Connection ID (either created locally or imposed remotely).
2729 * @param t Tunnel this connection belongs to (or NULL for transit connections);
2730 * @param path Path this connection has to use (copy is made).
2731 * @param own_pos Own position in the @c path path.
2733 * @return Newly created connection.
2734 * NULL in case of error: own id not in path, wrong neighbors, ...
2736 struct CadetConnection *
2737 GCC_new (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
2738 struct CadetTunnel *t,
2739 struct CadetPeerPath *path,
2740 unsigned int own_pos)
2742 struct CadetConnection *c;
2743 struct CadetPeerPath *cpath;
2745 GCC_check_connections ();
2746 cpath = path_duplicate (path);
2747 GNUNET_assert (NULL != cpath);
2748 c = GNUNET_new (struct CadetConnection);
2750 GNUNET_assert (GNUNET_OK ==
2751 GNUNET_CONTAINER_multihashmap_put (connections,
2753 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2754 fc_init (&c->fwd_fc);
2755 fc_init (&c->bck_fc);
2760 GNUNET_assert (own_pos <= cpath->length - 1);
2761 c->own_pos = own_pos;
2764 if (GNUNET_OK != register_neighbors (c))
2768 /* We were the origin of this request, this means we have invalid
2769 * info about the paths to reach the destination. We must invalidate
2770 * the *original* path to avoid trying it again in the next minute.
2772 if (2 < path->length)
2773 path_invalidate (path);
2777 GCT_debug(t, GNUNET_ERROR_TYPE_WARNING);
2781 path_destroy (c->path);
2786 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2787 GCC_check_connections ();
2793 * Connection is no longer needed: destroy it.
2795 * Cancels all pending traffic (including possible DESTROY messages), all
2796 * maintenance tasks and removes the connection from neighbor peers and tunnel.
2798 * @param c Connection to destroy.
2801 GCC_destroy (struct CadetConnection *c)
2803 GCC_check_connections ();
2810 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2811 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2814 LOG (GNUNET_ERROR_TYPE_DEBUG,
2815 "destroying connection %s\n",
2817 LOG (GNUNET_ERROR_TYPE_DEBUG,
2818 " fc's f: %p, b: %p\n",
2819 &c->fwd_fc, &c->bck_fc);
2820 LOG (GNUNET_ERROR_TYPE_DEBUG,
2821 " fc tasks f: %u, b: %u\n",
2822 c->fwd_fc.poll_task,
2823 c->bck_fc.poll_task);
2825 /* Cancel all traffic */
2826 if (NULL != c->path)
2828 connection_cancel_queues (c, GNUNET_YES);
2829 connection_cancel_queues (c, GNUNET_NO);
2830 if (NULL != c->maintenance_q)
2832 GCP_send_cancel (c->maintenance_q);
2833 c->maintenance_q = NULL;
2836 unregister_neighbors (c);
2837 path_destroy (c->path);
2840 /* Delete from tunnel */
2842 GCT_remove_connection (c->t, c);
2844 if (NULL != c->check_duplicates_task)
2845 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
2846 if (NULL != c->fwd_maintenance_task)
2847 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2848 if (NULL != c->bck_maintenance_task)
2849 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2851 if (GNUNET_NO == c->was_removed)
2853 GNUNET_break (GNUNET_YES ==
2854 GNUNET_CONTAINER_multihashmap_remove (connections,
2858 GNUNET_STATISTICS_update (stats,
2863 GCC_check_connections ();
2868 * Get the connection ID.
2870 * @param c Connection to get the ID from.
2872 * @return ID of the connection.
2874 const struct GNUNET_CADET_ConnectionTunnelIdentifier *
2875 GCC_get_id (const struct CadetConnection *c)
2882 * Get the connection ID.
2884 * @param c Connection to get the ID from.
2886 * @return ID of the connection.
2888 const struct GNUNET_HashCode *
2889 GCC_get_h (const struct CadetConnection *c)
2891 return GC_h2hc (&c->id);
2896 * Get the connection path.
2898 * @param c Connection to get the path from.
2900 * @return path used by the connection.
2902 const struct CadetPeerPath *
2903 GCC_get_path (const struct CadetConnection *c)
2905 if (GNUNET_NO == c->destroy)
2912 * Get the connection state.
2914 * @param c Connection to get the state from.
2916 * @return state of the connection.
2918 enum CadetConnectionState
2919 GCC_get_state (const struct CadetConnection *c)
2925 * Get the connection tunnel.
2927 * @param c Connection to get the tunnel from.
2929 * @return tunnel of the connection.
2931 struct CadetTunnel *
2932 GCC_get_tunnel (const struct CadetConnection *c)
2939 * Get free buffer space in a connection.
2941 * @param c Connection.
2942 * @param fwd Is query about FWD traffic?
2944 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2947 GCC_get_buffer (struct CadetConnection *c, int fwd)
2949 struct CadetFlowControl *fc;
2951 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2953 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
2954 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
2955 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
2957 return (fc->queue_max - fc->queue_n);
2962 * Get how many messages have we allowed to send to us from a direction.
2964 * @param c Connection.
2965 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2967 * @return last_ack_sent - last_pid_recv
2970 GCC_get_allowed (struct CadetConnection *c, int fwd)
2972 struct CadetFlowControl *fc;
2974 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2975 if ( (CADET_CONNECTION_READY != c->state) ||
2976 GC_is_pid_bigger (fc->last_pid_recv,
2977 fc->last_ack_sent) )
2981 return (fc->last_ack_sent - fc->last_pid_recv);
2986 * Get messages queued in a connection.
2988 * @param c Connection.
2989 * @param fwd Is query about FWD traffic?
2991 * @return Number of messages queued.
2994 GCC_get_qn (struct CadetConnection *c, int fwd)
2996 struct CadetFlowControl *fc;
2998 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3005 * Get next PID to use.
3007 * @param c Connection.
3008 * @param fwd Is query about FWD traffic?
3010 * @return Next PID to use.
3013 GCC_get_pid (struct CadetConnection *c, int fwd)
3015 struct CadetFlowControl *fc;
3018 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3026 * Allow the connection to advertise a buffer of the given size.
3028 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3029 * allowing up to last_pid_recv + buffer.
3031 * @param c Connection.
3032 * @param buffer How many more messages the connection can accept.
3033 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3036 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3038 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3039 GCC_2s (c), buffer, GC_f2s (fwd));
3040 send_ack (c, buffer, fwd, GNUNET_NO);
3045 * Notify other peers on a connection of a broken link. Mark connections
3046 * to destroy after all traffic has been sent.
3048 * @param c Connection on which there has been a disconnection.
3049 * @param peer Peer that disconnected.
3052 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
3054 struct CadetFlowControl *fc;
3058 GCC_check_connections ();
3059 strncpy (peer_name, GCP_2s (peer), 16);
3060 peer_name[15] = '\0';
3061 LOG (GNUNET_ERROR_TYPE_DEBUG,
3062 "shutting down %s, %s disconnected\n",
3063 GCC_2s (c), peer_name);
3065 invalidate_paths (c, peer);
3067 fwd = is_fwd (c, peer);
3068 if (GNUNET_SYSERR == fwd)
3073 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3074 (GNUNET_NO != c->destroy) )
3076 /* Local shutdown, or other peer already down (hence 'c->destroy');
3077 so there is no one to notify about this, just clean up. */
3079 GCC_check_connections ();
3082 /* Mark FlowControl towards the peer as unavaliable. */
3083 fc = fwd ? &c->bck_fc : &c->fwd_fc;
3086 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3088 /* Connection will have at least one pending message
3089 * (the one we just scheduled), so delay destruction
3090 * and remove from map so we don't use accidentally. */
3092 GNUNET_assert (GNUNET_NO == c->was_removed);
3093 c->was_removed = GNUNET_YES;
3094 GNUNET_break (GNUNET_YES ==
3095 GNUNET_CONTAINER_multihashmap_remove (connections,
3098 /* Cancel queue in the direction that just died. */
3099 connection_cancel_queues (c, ! fwd);
3100 GCC_stop_poll (c, ! fwd);
3101 unregister_neighbors (c);
3102 GCC_check_connections ();
3107 * Is this peer the first one on the connection?
3109 * @param c Connection.
3110 * @param fwd Is this about fwd traffic?
3112 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3115 GCC_is_origin (struct CadetConnection *c, int fwd)
3117 if (!fwd && c->path->length - 1 == c->own_pos )
3119 if (fwd && 0 == c->own_pos)
3126 * Is this peer the last one on the connection?
3128 * @param c Connection.
3129 * @param fwd Is this about fwd traffic?
3130 * Note that the ROOT is the terminal for BCK traffic!
3132 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3135 GCC_is_terminal (struct CadetConnection *c, int fwd)
3137 return GCC_is_origin (c, ! fwd);
3142 * See if we are allowed to send by the next hop in the given direction.
3144 * @param c Connection.
3145 * @param fwd Is this about fwd traffic?
3147 * @return #GNUNET_YES in case it's OK to send.
3150 GCC_is_sendable (struct CadetConnection *c, int fwd)
3152 struct CadetFlowControl *fc;
3154 LOG (GNUNET_ERROR_TYPE_DEBUG,
3155 " checking sendability of %s traffic on %s\n",
3156 GC_f2s (fwd), GCC_2s (c));
3162 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3163 LOG (GNUNET_ERROR_TYPE_DEBUG,
3164 " last ack recv: %u, last pid sent: %u\n",
3165 fc->last_ack_recv, fc->last_pid_sent);
3166 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3168 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3171 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3177 * Check if this connection is a direct one (never trim a direct connection).
3179 * @param c Connection.
3181 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3184 GCC_is_direct (struct CadetConnection *c)
3186 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3191 * Sends a completely built message on a connection, properly registering
3192 * all used resources.
3194 * @param message Message to send.
3195 * @param payload_type Type of payload, in case the message is encrypted.
3196 * 0 for restransmissions (when type is no longer known)
3197 * UINT16_MAX when not applicable.
3198 * @param payload_id ID of the payload (PID, ACK, ...).
3199 * @param c Connection on which this message is transmitted.
3200 * @param fwd Is this a fwd message?
3201 * @param force Force the connection to accept the message (buffer overfill).
3202 * @param cont Continuation called once message is sent. Can be NULL.
3203 * @param cont_cls Closure for @c cont.
3205 * @return Handle to cancel the message before it's sent.
3207 * Invalid on @c cont call.
3209 struct CadetConnectionQueue *
3210 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3211 uint16_t payload_type, uint32_t payload_id,
3212 struct CadetConnection *c, int fwd, int force,
3213 GCC_sent cont, void *cont_cls)
3215 struct CadetFlowControl *fc;
3216 struct CadetConnectionQueue *q;
3220 size = ntohs (message->size);
3221 type = ntohs (message->type);
3223 GCC_check_connections ();
3224 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3225 if (0 == fc->queue_max)
3231 LOG (GNUNET_ERROR_TYPE_INFO,
3232 "--> %s (%s %4u) on conn %s (%p) %s [%5u]\n",
3233 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), c,
3237 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3238 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, PIDsnt: %u, ACKrcv: %u\n",
3239 fc, fc->queue_n, fc->last_pid_sent, fc->last_ack_recv);
3240 if (GNUNET_NO == force)
3246 case GNUNET_MESSAGE_TYPE_CADET_KX:
3247 /* nothing to do here */
3250 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3251 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3252 /* Should've only be used for restransmissions. */
3253 GNUNET_break (0 == payload_type);
3256 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3257 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3258 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3259 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3260 GNUNET_assert (GNUNET_YES == force);
3268 if (fc->queue_n > fc->queue_max && GNUNET_NO == force)
3270 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3273 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3274 fc->queue_n, fc->queue_max);
3275 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3279 return NULL; /* Drop this message */
3282 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3283 GCC_2s (c), c->pending_messages);
3284 c->pending_messages++;
3286 q = GNUNET_new (struct CadetConnectionQueue);
3288 q->cont_cls = cont_cls;
3290 GNUNET_CONTAINER_DLL_insert (fc->q_head, fc->q_tail, q);
3291 q->peer_q = GCP_send (get_hop (c, fwd), message,
3292 payload_type, payload_id,
3294 &conn_message_sent, q);
3295 if (NULL == q->peer_q)
3297 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3298 GNUNET_CONTAINER_DLL_remove (fc->q_head, fc->q_tail, q);
3300 GCC_check_connections ();
3303 GCC_check_connections ();
3309 * Cancel a previously sent message while it's in the queue.
3311 * ONLY can be called before the continuation given to the send function
3312 * is called. Once the continuation is called, the message is no longer in the
3315 * @param q Handle to the queue.
3318 GCC_cancel (struct CadetConnectionQueue *q)
3320 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3322 /* send_cancel calls message_sent, which calls q->cont and frees q */
3323 GCP_send_cancel (q->peer_q);
3324 GCC_check_connections ();
3329 * Sends a CREATE CONNECTION message for a path to a peer.
3330 * Changes the connection and tunnel states if necessary.
3332 * @param c Connection to create.
3335 GCC_send_create (struct CadetConnection *c)
3337 enum CadetTunnelCState state;
3340 GCC_check_connections ();
3341 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3342 size += c->path->length * sizeof (struct GNUNET_PeerIdentity);
3344 /* Allocate message on the stack */
3345 unsigned char cbuf[size];
3346 struct GNUNET_CADET_ConnectionCreate *msg;
3347 struct GNUNET_PeerIdentity *peers;
3349 msg = (struct GNUNET_CADET_ConnectionCreate *) cbuf;
3350 msg->header.size = htons (size);
3351 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
3352 msg->reserved = htonl (0);
3353 msg->cid = *GCC_get_id (c);
3354 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3355 for (int i = 0; i < c->path->length; i++)
3357 GNUNET_PEER_resolve (c->path->peers[i], peers++);
3359 GNUNET_assert (NULL == c->maintenance_q);
3360 c->maintenance_q = GCP_send (get_next_hop (c),
3362 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0,
3364 &conn_message_sent, NULL);
3367 LOG (GNUNET_ERROR_TYPE_INFO, "==> %s %19s on conn %s (%p) FWD [%5u]\n",
3368 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE), "",
3369 GCC_2s (c), c, size);
3370 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3371 c, c->pending_messages);
3372 c->pending_messages++;
3374 state = GCT_get_cstate (c->t);
3375 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3376 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
3377 if (CADET_CONNECTION_NEW == c->state)
3378 connection_change_state (c, CADET_CONNECTION_SENT);
3379 GCC_check_connections ();
3384 * Send an ACK on the appropriate connection/channel, depending on
3385 * the direction and the position of the peer.
3387 * @param c Which connection to send the hop-by-hop ACK.
3388 * @param fwd Is this a fwd ACK? (will go dest->root).
3389 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
3392 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
3394 unsigned int buffer;
3396 GCC_check_connections ();
3397 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
3398 GC_f2s (fwd), GCC_2s (c));
3406 if (GNUNET_NO != c->destroy)
3408 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
3409 GCC_check_connections ();
3413 /* Get available buffer space */
3414 if (GCC_is_terminal (c, fwd))
3416 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
3417 buffer = GCT_get_channels_buffer (c->t);
3421 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
3422 buffer = GCC_get_buffer (c, fwd);
3424 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
3425 if (0 == buffer && GNUNET_NO == force)
3427 GCC_check_connections ();
3431 /* Send available buffer space */
3432 if (GNUNET_YES == GCC_is_origin (c, fwd))
3434 GNUNET_assert (NULL != c->t);
3435 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
3436 GCT_unchoke_channels (c->t);
3440 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
3441 send_ack (c, buffer, fwd, force);
3443 GCC_check_connections ();
3448 * Send a message to all peers in this connection that the connection
3449 * is no longer valid.
3451 * If some peer should not receive the message, it should be zero'ed out
3452 * before calling this function.
3454 * @param c The connection whose peers to notify.
3457 GCC_send_destroy (struct CadetConnection *c)
3459 struct GNUNET_CADET_ConnectionDestroy msg;
3461 if (GNUNET_YES == c->destroy)
3463 GCC_check_connections ();
3464 msg.header.size = htons (sizeof (msg));
3465 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3467 msg.reserved = htonl (0);
3468 LOG (GNUNET_ERROR_TYPE_DEBUG,
3469 " sending connection destroy for connection %s\n",
3472 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3473 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c,
3474 GNUNET_YES, GNUNET_YES, NULL, NULL);
3475 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3476 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c,
3477 GNUNET_NO, GNUNET_YES, NULL, NULL);
3479 GCC_check_connections ();
3484 * @brief Start a polling timer for the connection.
3486 * When a neighbor does not accept more traffic on the connection it could be
3487 * caused by a simple congestion or by a lost ACK. Polling enables to check
3488 * for the lastest ACK status for a connection.
3490 * @param c Connection.
3491 * @param fwd Should we poll in the FWD direction?
3494 GCC_start_poll (struct CadetConnection *c, int fwd)
3496 struct CadetFlowControl *fc;
3498 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3499 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3501 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3503 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL already in progress (t: %p, m: %p)\n",
3504 fc->poll_task, fc->poll_msg);
3507 if (0 == fc->queue_max)
3509 /* Should not be needed, traffic should've been cancelled. */
3511 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not possible, peer disconnected\n");
3514 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3515 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time, &send_poll, fc);
3520 * @brief Stop polling a connection for ACKs.
3522 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3524 * @param c Connection.
3525 * @param fwd Should we stop the poll in the FWD direction?
3528 GCC_stop_poll (struct CadetConnection *c, int fwd)
3530 struct CadetFlowControl *fc;
3532 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3533 if (NULL != fc->poll_task)
3535 GNUNET_SCHEDULER_cancel (fc->poll_task);
3536 fc->poll_task = NULL;
3538 if (NULL != fc->poll_msg)
3540 GCC_cancel (fc->poll_msg);
3541 fc->poll_msg = NULL;
3547 * Get a (static) string for a connection.
3549 * @param c Connection.
3552 GCC_2s (const struct CadetConnection *c)
3559 static char buf[128];
3561 SPRINTF (buf, "%s (->%s)",
3562 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3565 return GNUNET_h2s (GC_h2hc (&c->id));
3570 * Log all possible info about the connection state.
3572 * @param c Connection to debug.
3573 * @param level Debug level to use.
3576 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3581 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3583 __FILE__, __FUNCTION__, __LINE__);
3589 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3593 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3594 s = path_2s (c->path);
3595 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3597 LOG2 (level, "CCC state: %s, destroy: %u\n",
3598 GCC_state2s (c->state), c->destroy);
3599 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3600 if (NULL != c->perf)
3601 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3603 LOG2 (level, "CCC FWD flow control:\n");
3604 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3605 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3606 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3607 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3608 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3609 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3610 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3611 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3613 LOG2 (level, "CCC BCK flow control:\n");
3614 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3615 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3616 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3617 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3618 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3619 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3620 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3621 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3623 LOG2 (level, "CCC DEBUG CONNECTION END\n");