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 * Callback called when a queued ACK message is sent.
522 * @param cls Closure (FC).
523 * @param c Connection this message was on.
524 * @param q Queue handler this call invalidates.
525 * @param type Type of message sent.
526 * @param fwd Was this a FWD going message?
527 * @param size Size of the message.
531 struct CadetConnection *c,
532 struct CadetConnectionQueue *q,
533 uint16_t type, int fwd, size_t size)
535 struct CadetFlowControl *fc = cls;
542 * Send an ACK on the connection, informing the predecessor about
543 * the available buffer space. Should not be called in case the peer
544 * is origin (no predecessor) in the @c fwd direction.
546 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
547 * the ACK itself goes "back" (dest->root).
549 * @param c Connection on which to send the ACK.
550 * @param buffer How much space free to advertise?
551 * @param fwd Is this FWD ACK? (Going dest -> root)
552 * @param force Don't optimize out.
555 send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
557 struct CadetFlowControl *next_fc;
558 struct CadetFlowControl *prev_fc;
559 struct GNUNET_CADET_ACK msg;
563 /* If origin, there is no connection to send ACKs. Wrong function! */
564 GCC_check_connections ();
565 if (GCC_is_origin (c, fwd))
567 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
568 GCC_2s (c), GC_f2s (fwd));
573 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
574 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
576 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
577 GC_f2s (fwd), GCC_2s (c));
579 /* Check if we need to transmit the ACK. */
580 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
581 if (3 < delta && buffer < delta && GNUNET_NO == force)
583 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
584 LOG (GNUNET_ERROR_TYPE_DEBUG,
585 " last pid recv: %u, last ack sent: %u\n",
586 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
587 GCC_check_connections ();
591 /* Ok, ACK might be necessary, what PID to ACK? */
592 ack = prev_fc->last_pid_recv + buffer;
593 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
594 LOG (GNUNET_ERROR_TYPE_DEBUG,
595 " last pid %u, last ack %u, qmax %u, q %u\n",
596 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
597 next_fc->queue_max, next_fc->queue_n);
598 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
600 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
601 GCC_check_connections ();
605 /* Check if message is already in queue */
606 if (NULL != prev_fc->ack_msg)
608 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
610 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
611 GCC_cancel (prev_fc->ack_msg);
612 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
616 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
617 GCC_check_connections ();
622 prev_fc->last_ack_sent = ack;
624 /* Build ACK message and send on conn */
625 msg.header.size = htons (sizeof (msg));
626 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
627 msg.ack = htonl (ack);
630 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, UINT16_MAX, ack,
633 GNUNET_assert (NULL != prev_fc->ack_msg);
634 GCC_check_connections ();
639 * Update performance information if we are a connection's endpoint.
641 * @param c Connection to update.
642 * @param wait How much time did we wait to send the last message.
643 * @param size Size of the last message.
646 update_perf (struct CadetConnection *c,
647 struct GNUNET_TIME_Relative wait,
650 struct CadetConnectionPerformance *p;
654 return; /* Only endpoints are interested in timing. */
657 usecsperbyte = ((double) wait.rel_value_us) / size;
658 if (p->size == AVG_MSGS)
660 /* Array is full. Substract oldest value, add new one and store. */
661 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
662 p->usecsperbyte[p->idx] = usecsperbyte;
663 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
667 /* Array not yet full. Add current value to avg and store. */
668 p->usecsperbyte[p->idx] = usecsperbyte;
670 p->avg += p->usecsperbyte[p->idx];
674 p->idx = (p->idx + 1) % AVG_MSGS;
679 * Callback called when a connection queued message is sent.
681 * Calculates the average time and connection packet tracking.
683 * @param cls Closure (ConnectionQueue Handle), can be NULL.
684 * @param c Connection this message was on.
685 * @param fwd Was this a FWD going message?
686 * @param sent Was it really sent? (Could have been canceled)
687 * @param type Type of message sent.
688 * @param payload_type Type of payload, if applicable.
689 * @param pid Message ID, or 0 if not applicable (create, destroy, etc).
690 * @param size Size of the message.
691 * @param wait Time spent waiting for core (only the time for THIS message)
694 conn_message_sent (void *cls,
695 struct CadetConnection *c, int fwd, int sent,
696 uint16_t type, uint16_t payload_type, uint32_t pid,
698 struct GNUNET_TIME_Relative wait)
700 struct CadetConnectionQueue *q = cls;
701 struct CadetFlowControl *fc;
704 GCC_check_connections ();
705 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
707 /* If c is NULL, nothing to update. */
710 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
711 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
713 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
716 GCC_check_connections ();
720 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
721 sent ? "" : "not ", GC_f2s (fwd),
722 GC_m2s (type), GC_m2s (payload_type), pid);
723 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
725 /* Update flow control info. */
726 fc = fwd ? &c->fwd_fc : &c->bck_fc;
730 GNUNET_CONTAINER_DLL_remove (fc->q_head, fc->q_tail, q);
734 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
735 q->cont (q->cont_cls, c, q, type, fwd, size);
739 else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED)
741 /* SHOULD NO LONGER HAPPEN FIXME: REMOVE CASE */
742 // If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt
744 GNUNET_assert (0); // FIXME
746 else /* CONN_CREATE or CONN_ACK */
751 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
752 c->pending_messages--;
753 if ( (GNUNET_YES == c->destroy) &&
754 (0 == c->pending_messages) )
756 LOG (GNUNET_ERROR_TYPE_DEBUG,
757 "! destroying connection!\n");
759 GCC_check_connections ();
763 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
766 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
767 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
768 c->maintenance_q = NULL;
769 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
770 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
771 schedule_next_keepalive (c, fwd);
774 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
775 if (GNUNET_YES == sent)
777 GNUNET_assert (NULL != q);
778 fc->last_pid_sent = pid;
779 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
780 GCC_start_poll (c, fwd);
781 GCC_send_ack (c, fwd, GNUNET_NO);
782 connection_reset_timeout (c, fwd);
785 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
786 if (GNUNET_NO == forced)
789 LOG (GNUNET_ERROR_TYPE_DEBUG,
790 "! accounting pid %u\n",
795 LOG (GNUNET_ERROR_TYPE_DEBUG,
796 "! forced, Q_N not accounting pid %u\n",
801 case GNUNET_MESSAGE_TYPE_CADET_KX:
802 if (GNUNET_YES == sent)
803 connection_reset_timeout (c, fwd);
806 case GNUNET_MESSAGE_TYPE_CADET_POLL:
810 case GNUNET_MESSAGE_TYPE_CADET_ACK:
814 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
815 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
819 LOG (GNUNET_ERROR_TYPE_ERROR, "%s unknown\n", GC_m2s (type));
823 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
825 update_perf (c, wait, size);
826 GCC_check_connections ();
831 * Get the previous hop in a connection
833 * @param c Connection.
835 * @return Previous peer in the connection.
837 static struct CadetPeer *
838 get_prev_hop (const struct CadetConnection *c)
844 LOG (GNUNET_ERROR_TYPE_DEBUG,
845 " get prev hop %s [%u/%u]\n",
846 GCC_2s (c), c->own_pos, c->path->length);
847 if (0 == c->own_pos || c->path->length < 2)
848 id = c->path->peers[0];
850 id = c->path->peers[c->own_pos - 1];
852 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
853 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
855 return GCP_get_short (id, GNUNET_YES);
860 * Get the next hop in a connection
862 * @param c Connection.
864 * @return Next peer in the connection.
866 static struct CadetPeer *
867 get_next_hop (const struct CadetConnection *c)
874 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
875 GCC_2s (c), c->own_pos, c->path->length);
876 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
877 id = c->path->peers[c->path->length - 1];
879 id = c->path->peers[c->own_pos + 1];
881 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
882 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
884 return GCP_get_short (id, GNUNET_YES);
889 * Check that the direct neighbours (previous and next hop)
890 * are properly associated with this connection.
892 * @param c connection to check
895 check_neighbours (const struct CadetConnection *c)
898 return; /* nothing to check */
899 GCP_check_connection (get_next_hop (c), c);
900 GCP_check_connection (get_prev_hop (c), c);
905 * Helper for #GCC_check_connections(). Calls #check_neighbours().
909 * @param value the `struct CadetConnection` to check
910 * @return #GNUNET_OK (continue to iterate)
913 check_connection (void *cls,
914 const struct GNUNET_HashCode *key,
917 struct CadetConnection *c = value;
919 check_neighbours (c);
925 * Check invariants for all connections using #check_neighbours().
928 GCC_check_connections ()
930 if (0 == CHECK_INVARIANTS)
932 if (NULL == connections)
934 GNUNET_CONTAINER_multihashmap_iterate (connections,
941 * Get the hop in a connection.
943 * @param c Connection.
944 * @param fwd Next in the FWD direction?
946 * @return Next peer in the connection.
948 static struct CadetPeer *
949 get_hop (struct CadetConnection *c, int fwd)
951 return (fwd) ? get_next_hop (c) : get_prev_hop (c);
956 * Get a bit mask for a message received out-of-order.
958 * @param last_pid_recv Last PID we received prior to the out-of-order.
959 * @param ooo_pid PID of the out-of-order message.
962 get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
964 return 1 << (last_pid_recv - ooo_pid);
969 * Check is an out-of-order message is ok:
970 * - at most 31 messages behind.
973 * @param last_pid_recv Last in-order PID received.
976 is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
980 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
983 mask = get_recv_bitmask (last_pid_recv, ooo_pid);
984 if (0 != (ooo_bitmap & mask))
992 * Is traffic coming from this sender 'FWD' traffic?
994 * @param c Connection to check.
995 * @param sender Short peer identity of neighbor.
997 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
998 * the traffic is 'FWD'.
999 * #GNUNET_NO for BCK.
1000 * #GNUNET_SYSERR for errors (sender isn't a hop in the connection).
1003 is_fwd (const struct CadetConnection *c,
1004 const struct CadetPeer *sender)
1008 id = GCP_get_short_id (sender);
1009 if (GCP_get_short_id (get_prev_hop (c)) == id)
1012 if (GCP_get_short_id (get_next_hop (c)) == id)
1015 return GNUNET_SYSERR;
1020 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1021 * or a first CONNECTION_ACK directed to us.
1023 * @param c Connection to confirm.
1024 * @param fwd Should we send it FWD? (root->dest)
1025 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
1028 send_connection_ack (struct CadetConnection *c, int fwd)
1030 struct GNUNET_CADET_ConnectionACK msg;
1031 struct CadetTunnel *t;
1032 const uint16_t size = sizeof (struct GNUNET_CADET_ConnectionACK);
1033 const uint16_t type = GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK;
1035 GCC_check_connections ();
1037 LOG (GNUNET_ERROR_TYPE_INFO,
1038 "==> %s ({ C %s ACK} 0) on conn %s (%p) %s [%5u]\n",
1039 GC_m2s (type), GC_f2s (!fwd), GCC_2s (c), c, GC_f2s (fwd), size);
1041 msg.header.size = htons (size);
1042 msg.header.type = htons (type);
1043 msg.reserved = htonl (0);
1046 GNUNET_assert (NULL == c->maintenance_q);
1047 c->maintenance_q = GCP_send (get_hop (c, fwd), &msg.header,
1048 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0,
1050 &conn_message_sent, NULL);
1051 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (conn`ACK)\n",
1052 c, c->pending_messages);
1053 c->pending_messages++;
1055 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
1056 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
1057 if (CADET_CONNECTION_READY != c->state)
1058 connection_change_state (c, CADET_CONNECTION_SENT);
1059 GCC_check_connections ();
1064 * Send a notification that a connection is broken.
1066 * @param c Connection that is broken.
1067 * @param id1 Peer that has disconnected.
1068 * @param id2 Peer that has disconnected.
1069 * @param fwd Direction towards which to send it.
1072 send_broken (struct CadetConnection *c,
1073 const struct GNUNET_PeerIdentity *id1,
1074 const struct GNUNET_PeerIdentity *id2,
1077 struct GNUNET_CADET_ConnectionBroken msg;
1079 GCC_check_connections ();
1080 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1081 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1083 msg.reserved = htonl (0);
1086 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c, fwd,
1087 GNUNET_YES, NULL, NULL);
1088 GCC_check_connections ();
1093 * Send a notification that a connection is broken, when a connection
1094 * isn't even known to the local peer or soon to be destroyed.
1096 * @param connection_id Connection ID.
1097 * @param id1 Peer that has disconnected, probably local peer.
1098 * @param id2 Peer that has disconnected can be NULL if unknown.
1099 * @param neighbor Peer to notify (neighbor who sent the connection).
1102 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
1103 const struct GNUNET_PeerIdentity *id1,
1104 const struct GNUNET_PeerIdentity *id2,
1105 struct CadetPeer *neighbor)
1107 struct GNUNET_CADET_ConnectionBroken msg;
1109 GCC_check_connections ();
1110 LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n",
1111 GNUNET_h2s (GC_h2hc (connection_id)));
1113 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1114 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1115 msg.cid = *connection_id;
1116 msg.reserved = htonl (0);
1121 memset (&msg.peer2, 0, sizeof (msg.peer2));
1122 GNUNET_assert (NULL != GCP_send (neighbor, &msg.header,
1124 NULL, GNUNET_SYSERR, /* connection, fwd */
1125 NULL, NULL)); /* continuation */
1126 GCC_check_connections ();
1131 * Send keepalive packets for a connection.
1133 * @param c Connection to keep alive..
1134 * @param fwd Is this a FWD keepalive? (owner -> dest).
1137 send_connection_keepalive (struct CadetConnection *c, int fwd)
1139 struct GNUNET_MessageHeader msg;
1140 struct CadetFlowControl *fc;
1143 GCC_check_connections ();
1144 LOG (GNUNET_ERROR_TYPE_INFO,
1145 "keepalive %s for connection %s\n",
1146 GC_f2s (fwd), GCC_2s (c));
1148 GNUNET_assert (NULL != c->t);
1149 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1150 tunnel_ready = GNUNET_YES == GCT_has_queued_traffic (c->t)
1151 && CADET_TUNNEL_KEY_OK <= GCT_get_estate (c->t);
1152 if (0 < fc->queue_n || tunnel_ready)
1154 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1158 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1160 GNUNET_assert (NULL != c->t);
1161 msg.size = htons (sizeof (msg));
1162 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
1164 GNUNET_assert (NULL ==
1165 GCT_send_prebuilt_message (&msg, c->t, c,
1166 GNUNET_NO, NULL, NULL));
1167 GCC_check_connections ();
1172 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1174 * @param c Connection for which to send the message.
1175 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1178 connection_recreate (struct CadetConnection *c, int fwd)
1180 LOG (GNUNET_ERROR_TYPE_DEBUG,
1181 "sending connection recreate\n");
1183 GCC_send_create (c);
1185 send_connection_ack (c, GNUNET_NO);
1190 * Generic connection timer management.
1191 * Depending on the role of the peer in the connection will send the
1192 * appropriate message (build or keepalive)
1194 * @param c Conncetion to maintain.
1195 * @param fwd Is FWD?
1198 connection_maintain (struct CadetConnection *c, int fwd)
1200 if (GNUNET_NO != c->destroy)
1202 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1209 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1213 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1215 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1217 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1218 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1219 schedule_next_keepalive (c, fwd);
1224 case CADET_CONNECTION_NEW:
1227 case CADET_CONNECTION_SENT:
1228 connection_recreate (c, fwd);
1230 case CADET_CONNECTION_READY:
1231 send_connection_keepalive (c, fwd);
1240 * Keep the connection alive.
1242 * @param c Connection to keep alive.
1243 * @param fwd Direction.
1246 connection_keepalive (struct CadetConnection *c,
1249 GCC_check_connections ();
1250 LOG (GNUNET_ERROR_TYPE_DEBUG,
1251 "%s keepalive for %s\n",
1252 GC_f2s (fwd), GCC_2s (c));
1255 c->fwd_maintenance_task = NULL;
1257 c->bck_maintenance_task = NULL;
1258 connection_maintain (c, fwd);
1259 GCC_check_connections ();
1260 /* Next execution will be scheduled by message_sent or _maintain*/
1265 * Keep the connection alive in the FWD direction.
1267 * @param cls Closure (connection to keepalive).
1270 connection_fwd_keepalive (void *cls)
1272 struct CadetConnection *c = cls;
1274 GCC_check_connections ();
1275 connection_keepalive (c,
1277 GCC_check_connections ();
1282 * Keep the connection alive in the BCK direction.
1284 * @param cls Closure (connection to keepalive).
1287 connection_bck_keepalive (void *cls)
1289 struct CadetConnection *c = cls;
1291 GCC_check_connections ();
1292 connection_keepalive (c,
1294 GCC_check_connections ();
1299 * Schedule next keepalive task, taking in consideration
1300 * the connection state and number of retries.
1302 * If the peer is not the origin, do nothing.
1304 * @param c Connection for which to schedule the next keepalive.
1305 * @param fwd Direction for the next keepalive.
1308 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1310 struct GNUNET_TIME_Relative delay;
1311 struct GNUNET_SCHEDULER_Task * *task_id;
1312 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1314 GCC_check_connections ();
1315 if (GNUNET_NO == GCC_is_origin (c, fwd))
1318 /* Calculate delay to use, depending on the state of the connection */
1319 if (CADET_CONNECTION_READY == c->state)
1321 delay = refresh_connection_time;
1325 if (1 > c->create_retry)
1326 c->create_retry = 1;
1327 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1329 if (c->create_retry < 64) // TODO make configurable
1330 c->create_retry *= 2;
1333 /* Select direction-dependent parameters */
1334 if (GNUNET_YES == fwd)
1336 task_id = &c->fwd_maintenance_task;
1337 keepalive_task = &connection_fwd_keepalive;
1341 task_id = &c->bck_maintenance_task;
1342 keepalive_task = &connection_bck_keepalive;
1345 /* Check that no one scheduled it before us */
1346 if (NULL != *task_id)
1348 /* No need for a _break. It can happen for instance when sending a SYNACK
1349 * for a duplicate SYN: the first SYNACK scheduled the task. */
1350 GNUNET_SCHEDULER_cancel (*task_id);
1353 /* Schedule the task */
1354 *task_id = GNUNET_SCHEDULER_add_delayed (delay,
1357 LOG (GNUNET_ERROR_TYPE_DEBUG,
1358 "next keepalive in %s\n",
1359 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1360 GCC_check_connections ();
1365 * Cancel all transmissions that belong to a certain connection.
1367 * If the connection is scheduled for destruction and no more messages are left,
1368 * the connection will be destroyed by the continuation call.
1370 * @param c Connection which to cancel. Might be destroyed during this call.
1371 * @param fwd Cancel fwd traffic?
1374 connection_cancel_queues (struct CadetConnection *c,
1377 struct CadetFlowControl *fc;
1379 GCC_check_connections ();
1380 LOG (GNUNET_ERROR_TYPE_DEBUG,
1381 "Cancel %s queues for connection %s\n",
1382 GC_f2s (fwd), GCC_2s (c));
1389 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1390 if (NULL != fc->poll_task)
1392 GNUNET_SCHEDULER_cancel (fc->poll_task);
1393 fc->poll_task = NULL;
1394 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL task for fc %p\n", fc);
1396 if (NULL != fc->poll_msg)
1398 GCC_cancel (fc->poll_msg);
1399 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL msg for fc %p\n", fc);
1402 while (NULL != fc->q_head)
1404 GCC_cancel (fc->q_head);
1406 GCC_check_connections ();
1411 * Function called if a connection has been stalled for a while,
1412 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1414 * @param cls Closure (poll ctx).
1417 send_connection_poll (void *cls);
1421 * Callback called when a queued POLL message is sent.
1423 * @param cls Closure (flow control context).
1424 * @param c Connection this message was on.
1425 * @param q Queue handler this call invalidates.
1426 * @param type Type of message sent.
1427 * @param fwd Was this a FWD going message?
1428 * @param size Size of the message.
1431 poll_sent (void *cls,
1432 struct CadetConnection *c,
1433 struct CadetConnectionQueue *q,
1434 uint16_t type, int fwd, size_t size)
1436 struct CadetFlowControl *fc = cls;
1438 GNUNET_assert (fc->poll_msg == q);
1439 fc->poll_msg = NULL;
1440 if (2 == c->destroy)
1442 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1445 if (0 == fc->queue_max)
1447 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL cancelled: neighbor disconnected\n");
1450 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1452 GNUNET_assert (NULL == fc->poll_task);
1453 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1454 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1455 &send_connection_poll,
1457 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1462 * Function called if a connection has been stalled for a while,
1463 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1465 * @param cls Closure (poll ctx).
1468 send_connection_poll (void *cls)
1470 struct CadetFlowControl *fc = cls;
1471 struct GNUNET_CADET_Poll msg;
1472 struct CadetConnection *c;
1475 fc->poll_task = NULL;
1476 GCC_check_connections ();
1478 fwd = fc == &c->fwd_fc;
1479 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1480 GCC_2s (c), GC_f2s (fwd));
1482 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1483 msg.header.size = htons (sizeof (msg));
1485 msg.pid = htonl (fc->last_pid_sent);
1486 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1488 GCC_send_prebuilt_message (&msg.header, UINT16_MAX, fc->last_pid_sent, c,
1489 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1490 GNUNET_assert (NULL != fc->poll_msg);
1491 GCC_check_connections ();
1496 * Generic connection timeout implementation.
1498 * Timeout function due to lack of keepalive/traffic from an endpoint.
1499 * Destroys connection if called.
1501 * @param c Connection to destroy.
1502 * @param fwd Was the timeout from the origin? (FWD timeout)
1505 connection_timeout (struct CadetConnection *c, int fwd)
1507 GCC_check_connections ();
1509 LOG (GNUNET_ERROR_TYPE_INFO,
1510 "Connection %s %s timed out. Destroying.\n",
1513 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1515 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1521 /* If dest, send "broken" notification. */
1522 if (GCC_is_terminal (c, fwd))
1524 struct CadetPeer *next_hop;
1526 next_hop = fwd ? get_prev_hop (c) : get_next_hop (c);
1527 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1531 GCC_check_connections ();
1536 * Timeout function due to lack of keepalive/traffic from the owner.
1537 * Destroys connection if called.
1539 * @param cls Closure (connection to destroy).
1542 connection_fwd_timeout (void *cls)
1544 struct CadetConnection *c = cls;
1546 c->fwd_maintenance_task = NULL;
1547 GCC_check_connections ();
1548 connection_timeout (c, GNUNET_YES);
1549 GCC_check_connections ();
1554 * Timeout function due to lack of keepalive/traffic from the destination.
1555 * Destroys connection if called.
1557 * @param cls Closure (connection to destroy).
1560 connection_bck_timeout (void *cls)
1562 struct CadetConnection *c = cls;
1564 c->bck_maintenance_task = NULL;
1565 GCC_check_connections ();
1566 connection_timeout (c, GNUNET_NO);
1567 GCC_check_connections ();
1572 * Resets the connection timeout task, some other message has done the
1574 * - For the first peer on the direction this means to send
1575 * a keepalive or a path confirmation message (either create or ACK).
1576 * - For all other peers, this means to destroy the connection,
1577 * due to lack of activity.
1578 * Starts the timeout if no timeout was running (connection just created).
1580 * @param c Connection whose timeout to reset.
1581 * @param fwd Is this forward?
1583 * TODO use heap to improve efficiency of scheduler.
1586 connection_reset_timeout (struct CadetConnection *c, int fwd)
1588 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1589 if (GCC_is_origin (c, fwd)) /* Startpoint */
1591 schedule_next_keepalive (c, fwd);
1592 if (NULL != c->maintenance_q)
1594 GCP_send_cancel (c->maintenance_q);
1595 c->maintenance_q = NULL; /* Is set to NULL by conn_message_sent anyway */
1598 else /* Relay, endpoint. */
1600 struct GNUNET_TIME_Relative delay;
1601 struct GNUNET_SCHEDULER_Task * *ti;
1602 GNUNET_SCHEDULER_TaskCallback f;
1604 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1607 GNUNET_SCHEDULER_cancel (*ti);
1608 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1609 LOG (GNUNET_ERROR_TYPE_DEBUG,
1610 " timing out in %s\n",
1611 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1612 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1613 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1619 * Iterator to compare each connection's path with the path of a new connection.
1621 * If the connection coincides, the c member of path is set to the connection
1622 * and the destroy flag of the connection is set.
1624 * @param cls Closure (new path).
1625 * @param c Connection in the tunnel to check.
1628 check_path (void *cls, struct CadetConnection *c)
1630 struct CadetConnection *new_conn = cls;
1631 struct CadetPeerPath *path = new_conn->path;
1633 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1634 GCC_2s (c), c, c->path->length);
1637 && GNUNET_NO == c->destroy
1638 && CADET_CONNECTION_BROKEN != c->state
1639 && CADET_CONNECTION_DESTROYED != c->state
1640 && path_equivalent (path, c->path))
1642 new_conn->destroy = GNUNET_YES; /* Do not mark_destroyed, */
1643 new_conn->path->c = c; /* this is only a flag for the Iterator. */
1644 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1650 * Finds out if this path is already being used by an existing connection.
1652 * Checks the tunnel towards the destination to see if it contains
1653 * any connection with the same path.
1655 * If the existing connection is ready, it is kept.
1656 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1657 * the peer will eventually reject our attempt).
1659 * @param path Path to check.
1660 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1661 * #GNUNET_NO otherwise.
1664 does_connection_exist (struct CadetConnection *conn)
1666 struct CadetPeer *p;
1667 struct CadetTunnel *t;
1668 struct CadetConnection *c;
1670 p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
1673 t = GCP_get_tunnel (p);
1677 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1679 GCT_iterate_connections (t, &check_path, conn);
1681 if (GNUNET_YES == conn->destroy)
1684 conn->destroy = GNUNET_NO;
1685 conn->path->c = conn;
1686 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1687 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1688 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1689 if (CADET_CONNECTION_READY == c->state)
1691 /* The other peer confirmed a live connection with this path,
1692 * why are they trying to duplicate it? */
1693 GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
1696 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not ready, connection unique\n");
1701 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1708 * @brief Check if the tunnel this connection belongs to has any other
1709 * connection with the same path, and destroy one if so.
1711 * @param cls Closure (connection to check).
1714 check_duplicates (void *cls)
1716 struct CadetConnection *c = cls;
1718 c->check_duplicates_task = NULL;
1719 if (GNUNET_YES == does_connection_exist (c))
1721 GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1722 send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
1729 * Wait for enough time to let any dead connections time out and check for
1730 * any remaining duplicates.
1732 * @param c Connection that is a potential duplicate.
1735 schedule_check_duplicates (struct CadetConnection *c)
1737 struct GNUNET_TIME_Relative delay;
1739 if (NULL != c->check_duplicates_task)
1741 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 5);
1742 c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
1749 * Add the connection to the list of both neighbors.
1751 * @param c Connection.
1753 * @return #GNUNET_OK if everything went fine
1754 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1757 register_neighbors (struct CadetConnection *c)
1759 c->next_peer = get_next_hop (c);
1760 c->prev_peer = get_prev_hop (c);
1761 GNUNET_assert (c->next_peer != c->prev_peer);
1762 LOG (GNUNET_ERROR_TYPE_DEBUG,
1763 "register neighbors for connection %s\n",
1765 path_debug (c->path);
1766 LOG (GNUNET_ERROR_TYPE_DEBUG,
1767 "own pos %u\n", c->own_pos);
1768 LOG (GNUNET_ERROR_TYPE_DEBUG,
1769 "putting connection %s to next peer %p\n",
1772 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1774 GCP_2s (c->next_peer));
1775 LOG (GNUNET_ERROR_TYPE_DEBUG,
1776 "putting connection %s to prev peer %p\n",
1779 LOG (GNUNET_ERROR_TYPE_DEBUG,
1780 "prev peer %p %s\n",
1782 GCP_2s (c->prev_peer));
1784 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1785 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1787 if (GCC_is_origin (c, GNUNET_YES))
1788 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1789 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1791 LOG (GNUNET_ERROR_TYPE_DEBUG,
1792 " register neighbors failed\n");
1793 LOG (GNUNET_ERROR_TYPE_DEBUG,
1794 " prev: %s, neighbor?: %d\n",
1795 GCP_2s (c->prev_peer),
1796 GCP_is_neighbor (c->prev_peer));
1797 LOG (GNUNET_ERROR_TYPE_DEBUG,
1798 " next: %s, neighbor?: %d\n",
1799 GCP_2s (c->next_peer),
1800 GCP_is_neighbor (c->next_peer));
1801 return GNUNET_SYSERR;
1803 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1804 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1811 * Remove the connection from the list of both neighbors.
1813 * @param c Connection.
1816 unregister_neighbors (struct CadetConnection *c)
1818 // struct CadetPeer *peer; FIXME dont use next_peer, prev_peer
1819 /* Either already unregistered or never got registered, it's ok either way. */
1820 if (NULL == c->path)
1822 if (NULL != c->next_peer)
1824 GCP_remove_connection (c->next_peer, c);
1825 c->next_peer = NULL;
1827 if (NULL != c->prev_peer)
1829 GCP_remove_connection (c->prev_peer, c);
1830 c->prev_peer = NULL;
1836 * Invalidates all paths towards all peers that comprise the connection which
1837 * rely on the disconnected peer.
1839 * ~O(n^3) (peers in connection * paths/peer * links/path)
1841 * @param c Connection whose peers' paths to clean.
1842 * @param disconnected Peer that disconnected.
1845 invalidate_paths (struct CadetConnection *c,
1846 struct CadetPeer *disconnected)
1848 struct CadetPeer *peer;
1851 for (i = 0; i < c->path->length; i++)
1853 peer = GCP_get_short (c->path->peers[i], GNUNET_NO);
1855 GCP_notify_broken_link (peer, &my_full_id, GCP_get_id (disconnected));
1861 * Bind the connection to the peer and the tunnel to that peer.
1863 * If the peer has no tunnel, create one. Update tunnel and connection
1864 * data structres to reflect new status.
1866 * @param c Connection.
1870 add_to_peer (struct CadetConnection *c,
1871 struct CadetPeer *peer)
1873 GCP_add_tunnel (peer);
1874 c->t = GCP_get_tunnel (peer);
1875 GCT_add_connection (c->t, c);
1880 * Log receipt of message on stderr (INFO level).
1882 * @param message Message received.
1883 * @param peer Peer who sent the message.
1884 * @param conn_id Connection ID of the message.
1887 log_message (const struct GNUNET_MessageHeader *message,
1888 const struct CadetPeer *peer,
1889 const struct GNUNET_CADET_Hash *conn_id)
1895 size = ntohs (message->size);
1896 type = ntohs (message->type);
1899 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1900 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1901 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1902 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1908 LOG (GNUNET_ERROR_TYPE_INFO, "<%s %s on conn %s from %s, %6u bytes\n",
1909 arrow, GC_m2s (type), GNUNET_h2s (GC_h2hc (conn_id)),
1910 GCP_2s(peer), (unsigned int) size);
1913 /******************************************************************************/
1914 /******************************** API ***********************************/
1915 /******************************************************************************/
1918 * Handler for connection creation.
1920 * @param peer Message sender (neighbor).
1921 * @param msg Message itself.
1924 GCC_handle_create (struct CadetPeer *peer,
1925 const struct GNUNET_CADET_ConnectionCreate *msg)
1927 const struct GNUNET_CADET_Hash *cid;
1928 struct GNUNET_PeerIdentity *id;
1929 struct CadetPeerPath *path;
1930 struct CadetPeer *dest_peer;
1931 struct CadetPeer *orig_peer;
1932 struct CadetConnection *c;
1933 unsigned int own_pos;
1936 GCC_check_connections ();
1937 size = ntohs (msg->header.size);
1939 /* Calculate hops */
1940 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1941 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1943 GNUNET_break_op (0);
1946 size /= sizeof (struct GNUNET_PeerIdentity);
1949 GNUNET_break_op (0);
1952 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1954 /* Get parameters */
1956 log_message (&msg->header, peer, cid);
1957 id = (struct GNUNET_PeerIdentity *) &msg[1];
1958 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1960 /* Create connection */
1961 c = connection_get (cid);
1964 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1965 size, myid, &own_pos);
1968 /* Path was malformed, probably our own ID was not in it. */
1969 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
1970 GNUNET_break_op (0);
1975 /* We received this request from a neighbor, we cannot be origin */
1976 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
1977 GNUNET_break_op (0);
1978 path_destroy (path);
1982 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1983 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1984 c = GCC_new (cid, NULL, path, own_pos);
1987 if (path->length - 1 == own_pos)
1989 /* If we are destination, why did the creation fail? */
1991 path_destroy (path);
1992 GCC_check_connections ();
1995 send_broken_unknown (cid, &my_full_id,
1996 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1998 path_destroy (path);
1999 GCC_check_connections ();
2002 GCP_add_path_to_all (path, GNUNET_NO);
2003 connection_reset_timeout (c, GNUNET_YES);
2007 path = path_duplicate (c->path);
2009 if (CADET_CONNECTION_NEW == c->state)
2010 connection_change_state (c, CADET_CONNECTION_SENT);
2012 /* Remember peers */
2013 dest_peer = GCP_get (&id[size - 1], GNUNET_YES);
2014 orig_peer = GCP_get (&id[0], GNUNET_YES);
2016 /* Is it a connection to us? */
2017 if (c->own_pos == path->length - 1)
2019 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2020 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
2022 add_to_peer (c, orig_peer);
2023 if (GNUNET_YES == does_connection_exist (c))
2025 /* Peer created a connection equal to one we think exists
2027 * Solution: Keep both and postpone disambiguation. In the meantime
2028 * the connection will time out or peer will inform us it is broken.
2031 * - Use explicit duplicate.
2032 * - Accept new conn and destroy the old. (interruption in higher level)
2033 * - Keep the one with higher ID / created by peer with higher ID. */
2034 schedule_check_duplicates (c);
2037 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
2038 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
2039 if (NULL == c->maintenance_q)
2040 send_connection_ack (c, GNUNET_NO);
2041 if (CADET_CONNECTION_SENT == c->state)
2042 connection_change_state (c, CADET_CONNECTION_ACK);
2046 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2047 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2048 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2049 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c,
2050 GNUNET_YES, GNUNET_YES, NULL, NULL);
2052 path_destroy (path);
2053 GCC_check_connections ();
2058 * Handler for connection confirmations.
2060 * @param peer Message sender (neighbor).
2061 * @param msg Message itself.
2064 GCC_handle_confirm (struct CadetPeer *peer,
2065 const struct GNUNET_CADET_ConnectionACK *msg)
2067 struct CadetConnection *c;
2068 enum CadetConnectionState oldstate;
2071 GCC_check_connections ();
2072 log_message (&msg->header, peer, &msg->cid);
2073 c = connection_get (&msg->cid);
2076 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2078 LOG (GNUNET_ERROR_TYPE_DEBUG,
2079 " don't know the connection!\n");
2080 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2081 GCC_check_connections ();
2084 if (GNUNET_NO != c->destroy)
2086 GNUNET_assert (CADET_CONNECTION_DESTROYED == c->state);
2087 GNUNET_STATISTICS_update (stats, "# control on dying connection",
2089 LOG (GNUNET_ERROR_TYPE_DEBUG,
2090 "connection %s being destroyed, ignoring confirm\n",
2092 GCC_check_connections ();
2096 oldstate = c->state;
2097 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GCP_2s (peer));
2098 if (get_next_hop (c) == peer)
2100 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2102 if (CADET_CONNECTION_SENT == oldstate)
2103 connection_change_state (c, CADET_CONNECTION_ACK);
2105 else if (get_prev_hop (c) == peer)
2107 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2109 connection_change_state (c, CADET_CONNECTION_READY);
2113 GNUNET_STATISTICS_update (stats, "# control on connection from wrong peer",
2115 GNUNET_break_op (0);
2119 connection_reset_timeout (c, fwd);
2121 GNUNET_assert (NULL != c->path);
2122 GCP_add_path_to_all (c->path, GNUNET_YES);
2124 /* Message for us as creator? */
2125 if (GNUNET_YES == GCC_is_origin (c, GNUNET_YES))
2127 if (GNUNET_NO != fwd)
2132 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2134 /* If just created, cancel the short timeout and start a long one */
2135 if (CADET_CONNECTION_SENT == oldstate)
2137 c->create_retry = 1;
2138 connection_reset_timeout (c, GNUNET_YES);
2141 /* Change connection state, send ACK */
2142 connection_change_state (c, CADET_CONNECTION_READY);
2143 send_connection_ack (c, GNUNET_YES);
2145 /* Change tunnel state, trigger KX */
2146 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2147 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2148 GCC_check_connections ();
2152 /* Message for us as destination? */
2153 if (GCC_is_terminal (c, GNUNET_YES))
2155 if (GNUNET_YES != fwd)
2160 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2162 /* If just created, cancel the short timeout and start a long one */
2163 if (CADET_CONNECTION_ACK == oldstate)
2164 connection_reset_timeout (c, GNUNET_NO);
2166 /* Change tunnel state */
2167 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2168 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2169 GCC_check_connections ();
2173 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2174 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2175 GNUNET_YES, NULL, NULL);
2177 GCC_check_connections ();
2182 * Handler for notifications of broken connections.
2184 * @param peer Message sender (neighbor).
2185 * @param msg Message itself.
2188 GCC_handle_broken (struct CadetPeer *peer,
2189 const struct GNUNET_CADET_ConnectionBroken *msg)
2191 struct CadetConnection *c;
2192 struct CadetTunnel *t;
2195 GCC_check_connections ();
2196 log_message (&msg->header, peer, &msg->cid);
2197 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", GNUNET_i2s (&msg->peer1));
2198 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", GNUNET_i2s (&msg->peer2));
2199 c = connection_get (&msg->cid);
2202 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2203 GNUNET_STATISTICS_update (stats, "# duplicate CONNECTION_BROKEN",
2205 GCC_check_connections ();
2211 fwd = is_fwd (c, peer);
2212 if (GNUNET_SYSERR == fwd)
2214 GNUNET_break_op (0);
2215 GCC_check_connections ();
2219 if (GCC_is_terminal (c, fwd))
2221 struct CadetPeer *endpoint;
2225 /* A terminal connection should not have 't' set to NULL. */
2227 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2230 endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
2231 if (2 < c->path->length)
2232 path_invalidate (c->path);
2233 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2235 connection_change_state (c, CADET_CONNECTION_BROKEN);
2236 GCT_remove_connection (t, c);
2243 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2244 GNUNET_YES, NULL, NULL);
2245 connection_cancel_queues (c, !fwd);
2247 GCC_check_connections ();
2253 * Handler for notifications of destroyed connections.
2255 * @param peer Message sender (neighbor).
2256 * @param msg Message itself.
2259 GCC_handle_destroy (struct CadetPeer *peer,
2260 const struct GNUNET_CADET_ConnectionDestroy *msg)
2262 struct CadetConnection *c;
2265 GCC_check_connections ();
2266 log_message (&msg->header, peer, &msg->cid);
2267 c = connection_get (&msg->cid);
2270 /* Probably already got the message from another path,
2271 * destroyed the tunnel and retransmitted to children.
2274 GNUNET_STATISTICS_update (stats,
2275 "# control on unknown connection",
2277 LOG (GNUNET_ERROR_TYPE_DEBUG,
2278 " connection unknown destroyed: previously destroyed?\n");
2279 GCC_check_connections ();
2283 fwd = is_fwd (c, peer);
2284 if (GNUNET_SYSERR == fwd)
2286 GNUNET_break_op (0);
2287 GCC_check_connections ();
2291 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2293 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2294 GNUNET_YES, NULL, NULL);
2296 else if (0 == c->pending_messages)
2298 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
2300 GCC_check_connections ();
2306 GCT_remove_connection (c->t, c);
2309 GCC_check_connections ();
2315 * Handler for cadet network traffic hop-by-hop acks.
2317 * @param peer Message sender (neighbor).
2318 * @param msg Message itself.
2321 GCC_handle_ack (struct CadetPeer *peer,
2322 const struct GNUNET_CADET_ACK *msg)
2324 struct CadetConnection *c;
2325 struct CadetFlowControl *fc;
2329 GCC_check_connections ();
2330 log_message (&msg->header, peer, &msg->cid);
2331 c = connection_get (&msg->cid);
2334 GNUNET_STATISTICS_update (stats,
2335 "# ack on unknown connection",
2338 send_broken_unknown (&msg->cid,
2342 GCC_check_connections ();
2346 /* Is this a forward or backward ACK? */
2347 if (get_next_hop (c) == peer)
2352 else if (get_prev_hop (c) == peer)
2359 GNUNET_break_op (0);
2363 ack = ntohl (msg->ack);
2364 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2365 GC_f2s (fwd), ack, fc->last_ack_recv);
2366 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2367 fc->last_ack_recv = ack;
2369 /* Cancel polling if the ACK is big enough. */
2370 if (NULL != fc->poll_task &&
2371 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2373 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2374 GNUNET_SCHEDULER_cancel (fc->poll_task);
2375 fc->poll_task = NULL;
2376 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2379 GCC_check_connections ();
2384 * Handler for cadet network traffic hop-by-hop data counter polls.
2386 * @param peer Message sender (neighbor).
2387 * @param msg Message itself.
2390 GCC_handle_poll (struct CadetPeer *peer,
2391 const struct GNUNET_CADET_Poll *msg)
2393 struct CadetConnection *c;
2394 struct CadetFlowControl *fc;
2398 GCC_check_connections ();
2399 log_message (&msg->header, peer, &msg->cid);
2400 c = connection_get (&msg->cid);
2403 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2405 LOG (GNUNET_ERROR_TYPE_DEBUG,
2406 "POLL message on unknown connection %s!\n",
2407 GNUNET_h2s (GC_h2hc (&msg->cid)));
2408 send_broken_unknown (&msg->cid,
2412 GCC_check_connections ();
2416 /* Is this a forward or backward ACK?
2417 * Note: a poll should never be needed in a loopback case,
2418 * since there is no possiblility of packet loss there, so
2419 * this way of discerining FWD/BCK should not be a problem.
2421 if (get_next_hop (c) == peer)
2423 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2426 else if (get_prev_hop (c) == peer)
2428 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2433 GNUNET_break_op (0);
2437 pid = ntohl (msg->pid);
2438 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2439 fc->last_pid_recv = pid;
2440 fwd = fc == &c->bck_fc;
2441 GCC_send_ack (c, fwd, GNUNET_YES);
2442 GCC_check_connections ();
2447 * Check the message against internal state and test if it goes FWD or BCK.
2449 * Updates the PID, state and timeout values for the connection.
2451 * @param message Message to check. It must belong to an existing connection.
2452 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2453 * @param c Connection this message should belong. If NULL, check fails.
2454 * @param sender Neighbor that sent the message.
2456 * @return #GNUNET_YES if the message goes FWD.
2457 * #GNUNET_NO if it goes BCK.
2458 * #GNUNET_SYSERR if there is an error (unauthorized sender, ...).
2461 check_message (const struct GNUNET_MessageHeader *message,
2462 const struct GNUNET_CADET_Hash* cid,
2463 struct CadetConnection *c,
2464 struct CadetPeer *sender,
2467 struct CadetFlowControl *fc;
2468 struct CadetPeer *hop;
2472 /* Check connection */
2475 GNUNET_STATISTICS_update (stats,
2476 "# unknown connection",
2478 LOG (GNUNET_ERROR_TYPE_DEBUG,
2479 "%s on unknown connection %s\n",
2480 GC_m2s (ntohs (message->type)),
2481 GNUNET_h2s (GC_h2hc (cid)));
2482 send_broken_unknown (cid,
2486 return GNUNET_SYSERR;
2489 /* Check if origin is as expected */
2490 hop = get_prev_hop (c);
2497 hop = get_next_hop (c);
2498 GNUNET_break (hop == c->next_peer);
2505 /* Unexpected peer sending traffic on a connection. */
2506 GNUNET_break_op (0);
2507 return GNUNET_SYSERR;
2511 /* Check PID for payload messages */
2512 type = ntohs (message->type);
2513 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
2515 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2516 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2517 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2518 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2520 GNUNET_break_op (0);
2521 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2522 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2523 pid, fc->last_pid_recv, fc->last_ack_sent);
2524 return GNUNET_SYSERR;
2526 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2530 delta = pid - fc->last_pid_recv;
2531 fc->last_pid_recv = pid;
2532 fc->recv_bitmap <<= delta;
2533 fc->recv_bitmap |= 1;
2537 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2538 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2540 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2541 pid, fc->last_pid_recv - 31);
2542 return GNUNET_SYSERR;
2544 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2548 /* Count as connection confirmation. */
2549 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2551 connection_change_state (c, CADET_CONNECTION_READY);
2554 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2555 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2558 connection_reset_timeout (c, fwd);
2565 * Handler for key exchange traffic (Axolotl KX).
2567 * @param peer Message sender (neighbor).
2568 * @param msg Message itself.
2571 GCC_handle_kx (struct CadetPeer *peer,
2572 const struct GNUNET_CADET_KX *msg)
2574 const struct GNUNET_CADET_Hash* cid;
2575 struct CadetConnection *c;
2578 GCC_check_connections ();
2580 log_message (&msg->header, peer, cid);
2582 c = connection_get (cid);
2583 fwd = check_message (&msg->header,
2589 /* If something went wrong, discard message. */
2590 if (GNUNET_SYSERR == fwd)
2592 GNUNET_break_op (0);
2593 GCC_check_connections ();
2597 /* Is this message for us? */
2598 if (GCC_is_terminal (c, fwd))
2600 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2601 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2607 GCT_handle_kx (c->t, msg);
2608 GCC_check_connections ();
2612 /* Message not for us: forward to next hop */
2613 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2614 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2615 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2616 GNUNET_NO, NULL, NULL);
2617 GCC_check_connections ();
2622 * Handler for encrypted cadet network traffic (channel mgmt, data).
2624 * @param peer Message sender (neighbor).
2625 * @param msg Message itself.
2628 GCC_handle_encrypted (struct CadetPeer *peer,
2629 const struct GNUNET_CADET_Encrypted *msg)
2631 const struct GNUNET_CADET_Hash* cid;
2632 struct CadetConnection *c;
2636 GCC_check_connections ();
2638 pid = ntohl (msg->pid);
2639 log_message (&msg->header, peer, cid);
2641 c = connection_get (cid);
2642 fwd = check_message (&msg->header,
2648 /* If something went wrong, discard message. */
2649 if (GNUNET_SYSERR == fwd)
2651 GNUNET_break_op (0);
2652 GCC_check_connections ();
2656 /* Is this message for us? */
2657 if (GCC_is_terminal (c, fwd))
2659 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2663 GNUNET_break (GNUNET_NO != c->destroy);
2666 GCT_handle_encrypted (c->t, msg);
2667 GCC_send_ack (c, fwd, GNUNET_NO);
2668 GCC_check_connections ();
2672 /* Message not for us: forward to next hop */
2673 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2674 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2675 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2676 GNUNET_NO, NULL, NULL);
2677 GCC_check_connections ();
2682 * Initialize the connections subsystem
2684 * @param c Configuration handle.
2687 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2689 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2691 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2694 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2695 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2696 GNUNET_SCHEDULER_shutdown ();
2701 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2704 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2705 "CADET", "MAX_CONNECTIONS", "MISSING");
2706 GNUNET_SCHEDULER_shutdown ();
2711 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2712 &refresh_connection_time))
2714 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2715 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2716 GNUNET_SCHEDULER_shutdown ();
2719 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2720 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2725 * Destroy each connection on shutdown.
2727 * @param cls Closure (unused).
2728 * @param key Current key code (CID, unused).
2729 * @param value Value in the hash map (`struct CadetConnection`)
2731 * @return #GNUNET_YES, because we should continue to iterate
2734 shutdown_iterator (void *cls,
2735 const struct GNUNET_HashCode *key,
2738 struct CadetConnection *c = value;
2740 c->state = CADET_CONNECTION_DESTROYED;
2747 * Shut down the connections subsystem.
2752 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n");
2753 GCC_check_connections ();
2754 GNUNET_CONTAINER_multihashmap_iterate (connections,
2757 GNUNET_CONTAINER_multihashmap_destroy (connections);
2763 * Create a connection.
2765 * @param cid Connection ID (either created locally or imposed remotely).
2766 * @param t Tunnel this connection belongs to (or NULL for transit connections);
2767 * @param path Path this connection has to use (copy is made).
2768 * @param own_pos Own position in the @c path path.
2770 * @return Newly created connection.
2771 * NULL in case of error: own id not in path, wrong neighbors, ...
2773 struct CadetConnection *
2774 GCC_new (const struct GNUNET_CADET_Hash *cid,
2775 struct CadetTunnel *t,
2776 struct CadetPeerPath *path,
2777 unsigned int own_pos)
2779 struct CadetConnection *c;
2780 struct CadetPeerPath *cpath;
2782 GCC_check_connections ();
2783 cpath = path_duplicate (path);
2784 GNUNET_assert (NULL != cpath);
2785 c = GNUNET_new (struct CadetConnection);
2787 GNUNET_assert (GNUNET_OK ==
2788 GNUNET_CONTAINER_multihashmap_put (connections,
2790 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2791 fc_init (&c->fwd_fc);
2792 fc_init (&c->bck_fc);
2797 GNUNET_assert (own_pos <= cpath->length - 1);
2798 c->own_pos = own_pos;
2801 if (GNUNET_OK != register_neighbors (c))
2805 /* We were the origin of this request, this means we have invalid
2806 * info about the paths to reach the destination. We must invalidate
2807 * the *original* path to avoid trying it again in the next minute.
2809 if (2 < path->length)
2810 path_invalidate (path);
2814 GCT_debug(t, GNUNET_ERROR_TYPE_WARNING);
2818 path_destroy (c->path);
2823 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2824 GCC_check_connections ();
2830 * Connection is no longer needed: destroy it.
2832 * Cancels all pending traffic (including possible DESTROY messages), all
2833 * maintenance tasks and removes the connection from neighbor peers and tunnel.
2835 * @param c Connection to destroy.
2838 GCC_destroy (struct CadetConnection *c)
2840 GCC_check_connections ();
2847 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2848 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2851 LOG (GNUNET_ERROR_TYPE_DEBUG,
2852 "destroying connection %s\n",
2854 LOG (GNUNET_ERROR_TYPE_DEBUG,
2855 " fc's f: %p, b: %p\n",
2856 &c->fwd_fc, &c->bck_fc);
2857 LOG (GNUNET_ERROR_TYPE_DEBUG,
2858 " fc tasks f: %u, b: %u\n",
2859 c->fwd_fc.poll_task,
2860 c->bck_fc.poll_task);
2862 /* Cancel all traffic */
2863 if (NULL != c->path)
2865 connection_cancel_queues (c, GNUNET_YES);
2866 connection_cancel_queues (c, GNUNET_NO);
2867 if (NULL != c->maintenance_q)
2869 GCP_send_cancel (c->maintenance_q);
2870 c->maintenance_q = NULL;
2873 unregister_neighbors (c);
2874 path_destroy (c->path);
2877 /* Delete from tunnel */
2879 GCT_remove_connection (c->t, c);
2881 if (NULL != c->check_duplicates_task)
2882 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
2883 if (NULL != c->fwd_maintenance_task)
2884 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2885 if (NULL != c->bck_maintenance_task)
2886 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2888 if (GNUNET_NO == c->was_removed)
2890 GNUNET_break (GNUNET_YES ==
2891 GNUNET_CONTAINER_multihashmap_remove (connections,
2895 GNUNET_STATISTICS_update (stats,
2900 GCC_check_connections ();
2905 * Get the connection ID.
2907 * @param c Connection to get the ID from.
2909 * @return ID of the connection.
2911 const struct GNUNET_CADET_Hash *
2912 GCC_get_id (const struct CadetConnection *c)
2919 * Get the connection ID.
2921 * @param c Connection to get the ID from.
2923 * @return ID of the connection.
2925 const struct GNUNET_HashCode *
2926 GCC_get_h (const struct CadetConnection *c)
2928 return GC_h2hc (&c->id);
2933 * Get the connection path.
2935 * @param c Connection to get the path from.
2937 * @return path used by the connection.
2939 const struct CadetPeerPath *
2940 GCC_get_path (const struct CadetConnection *c)
2942 if (GNUNET_NO == c->destroy)
2949 * Get the connection state.
2951 * @param c Connection to get the state from.
2953 * @return state of the connection.
2955 enum CadetConnectionState
2956 GCC_get_state (const struct CadetConnection *c)
2962 * Get the connection tunnel.
2964 * @param c Connection to get the tunnel from.
2966 * @return tunnel of the connection.
2968 struct CadetTunnel *
2969 GCC_get_tunnel (const struct CadetConnection *c)
2976 * Get free buffer space in a connection.
2978 * @param c Connection.
2979 * @param fwd Is query about FWD traffic?
2981 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2984 GCC_get_buffer (struct CadetConnection *c, int fwd)
2986 struct CadetFlowControl *fc;
2988 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2990 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
2991 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
2992 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
2994 return (fc->queue_max - fc->queue_n);
2999 * Get how many messages have we allowed to send to us from a direction.
3001 * @param c Connection.
3002 * @param fwd Are we asking about traffic from FWD (BCK messages)?
3004 * @return last_ack_sent - last_pid_recv
3007 GCC_get_allowed (struct CadetConnection *c, int fwd)
3009 struct CadetFlowControl *fc;
3011 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3012 if (CADET_CONNECTION_READY != c->state
3013 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
3017 return (fc->last_ack_sent - fc->last_pid_recv);
3022 * Get messages queued in a connection.
3024 * @param c Connection.
3025 * @param fwd Is query about FWD traffic?
3027 * @return Number of messages queued.
3030 GCC_get_qn (struct CadetConnection *c, int fwd)
3032 struct CadetFlowControl *fc;
3034 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3041 * Get next PID to use.
3043 * @param c Connection.
3044 * @param fwd Is query about FWD traffic?
3046 * @return Next PID to use.
3049 GCC_get_pid (struct CadetConnection *c, int fwd)
3051 struct CadetFlowControl *fc;
3054 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3062 * Allow the connection to advertise a buffer of the given size.
3064 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3065 * allowing up to last_pid_recv + buffer.
3067 * @param c Connection.
3068 * @param buffer How many more messages the connection can accept.
3069 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3072 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3074 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3075 GCC_2s (c), buffer, GC_f2s (fwd));
3076 send_ack (c, buffer, fwd, GNUNET_NO);
3081 * Notify other peers on a connection of a broken link. Mark connections
3082 * to destroy after all traffic has been sent.
3084 * @param c Connection on which there has been a disconnection.
3085 * @param peer Peer that disconnected.
3088 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
3090 struct CadetFlowControl *fc;
3094 GCC_check_connections ();
3095 strncpy (peer_name, GCP_2s (peer), 16);
3096 peer_name[15] = '\0';
3097 LOG (GNUNET_ERROR_TYPE_DEBUG,
3098 "shutting down %s, %s disconnected\n",
3099 GCC_2s (c), peer_name);
3101 invalidate_paths (c, peer);
3103 fwd = is_fwd (c, peer);
3104 if (GNUNET_SYSERR == fwd)
3109 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3110 (GNUNET_NO != c->destroy) )
3112 /* Local shutdown, or other peer already down (hence 'c->destroy');
3113 so there is no one to notify about this, just clean up. */
3115 GCC_check_connections ();
3118 /* Mark FlowControl towards the peer as unavaliable. */
3119 fc = fwd ? &c->bck_fc : &c->fwd_fc;
3122 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3124 /* Connection will have at least one pending message
3125 * (the one we just scheduled), so delay destruction
3126 * and remove from map so we don't use accidentally. */
3128 GNUNET_assert (GNUNET_NO == c->was_removed);
3129 c->was_removed = GNUNET_YES;
3130 GNUNET_break (GNUNET_YES ==
3131 GNUNET_CONTAINER_multihashmap_remove (connections,
3134 /* Cancel queue in the direction that just died. */
3135 connection_cancel_queues (c, ! fwd);
3136 GCC_stop_poll (c, ! fwd);
3137 unregister_neighbors (c);
3138 GCC_check_connections ();
3143 * Is this peer the first one on the connection?
3145 * @param c Connection.
3146 * @param fwd Is this about fwd traffic?
3148 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3151 GCC_is_origin (struct CadetConnection *c, int fwd)
3153 if (!fwd && c->path->length - 1 == c->own_pos )
3155 if (fwd && 0 == c->own_pos)
3162 * Is this peer the last one on the connection?
3164 * @param c Connection.
3165 * @param fwd Is this about fwd traffic?
3166 * Note that the ROOT is the terminal for BCK traffic!
3168 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3171 GCC_is_terminal (struct CadetConnection *c, int fwd)
3173 return GCC_is_origin (c, ! fwd);
3178 * See if we are allowed to send by the next hop in the given direction.
3180 * @param c Connection.
3181 * @param fwd Is this about fwd traffic?
3183 * @return #GNUNET_YES in case it's OK to send.
3186 GCC_is_sendable (struct CadetConnection *c, int fwd)
3188 struct CadetFlowControl *fc;
3190 LOG (GNUNET_ERROR_TYPE_DEBUG,
3191 " checking sendability of %s traffic on %s\n",
3192 GC_f2s (fwd), GCC_2s (c));
3198 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3199 LOG (GNUNET_ERROR_TYPE_DEBUG,
3200 " last ack recv: %u, last pid sent: %u\n",
3201 fc->last_ack_recv, fc->last_pid_sent);
3202 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3204 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3207 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3213 * Check if this connection is a direct one (never trim a direct connection).
3215 * @param c Connection.
3217 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3220 GCC_is_direct (struct CadetConnection *c)
3222 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3227 * Sends a completely built message on a connection, properly registering
3228 * all used resources.
3230 * @param message Message to send.
3231 * @param payload_type Type of payload, in case the message is encrypted.
3232 * 0 for restransmissions (when type is no longer known)
3233 * UINT16_MAX when not applicable.
3234 * @param payload_id ID of the payload (PID, ACK, ...).
3235 * @param c Connection on which this message is transmitted.
3236 * @param fwd Is this a fwd message?
3237 * @param force Force the connection to accept the message (buffer overfill).
3238 * @param cont Continuation called once message is sent. Can be NULL.
3239 * @param cont_cls Closure for @c cont.
3241 * @return Handle to cancel the message before it's sent.
3242 * NULL on error or if @c cont is NULL.
3243 * Invalid on @c cont call.
3245 struct CadetConnectionQueue *
3246 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3247 uint16_t payload_type, uint32_t payload_id,
3248 struct CadetConnection *c, int fwd, int force,
3249 GCC_sent cont, void *cont_cls)
3251 struct CadetFlowControl *fc;
3252 struct CadetConnectionQueue *q;
3256 size = ntohs (message->size);
3257 type = ntohs (message->type);
3259 GCC_check_connections ();
3260 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3261 if (0 == fc->queue_max)
3267 LOG (GNUNET_ERROR_TYPE_INFO,
3268 "--> %s (%s %4u) on conn %s (%p) %s [%5u]\n",
3269 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), c,
3273 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3274 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3275 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3276 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3277 if (GNUNET_NO == force)
3283 case GNUNET_MESSAGE_TYPE_CADET_KX:
3286 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3287 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3288 /* Should've only be used for restransmissions. */
3289 GNUNET_break (0 == payload_type);
3292 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3293 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3294 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3295 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3296 GNUNET_assert (GNUNET_YES == force);
3304 if (fc->queue_n > fc->queue_max && GNUNET_NO == force)
3306 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3309 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3310 fc->queue_n, fc->queue_max);
3311 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3315 return NULL; /* Drop this message */
3318 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3319 GCC_2s (c), c->pending_messages);
3320 c->pending_messages++;
3322 q = GNUNET_new (struct CadetConnectionQueue);
3324 q->peer_q = GCP_send (get_hop (c, fwd), message,
3325 payload_type, payload_id,
3327 &conn_message_sent, q);
3328 if (NULL == q->peer_q)
3330 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3332 GCC_check_connections ();
3336 q->cont_cls = cont_cls;
3337 GNUNET_CONTAINER_DLL_insert (fc->q_head, fc->q_tail, q);
3338 GCC_check_connections ();
3344 * Cancel a previously sent message while it's in the queue.
3346 * ONLY can be called before the continuation given to the send function
3347 * is called. Once the continuation is called, the message is no longer in the
3350 * @param q Handle to the queue.
3353 GCC_cancel (struct CadetConnectionQueue *q)
3355 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3357 /* send_cancel calls message_sent, which calls q->cont and frees q */
3358 GCP_send_cancel (q->peer_q);
3359 GCC_check_connections ();
3364 * Sends a CREATE CONNECTION message for a path to a peer.
3365 * Changes the connection and tunnel states if necessary.
3367 * @param c Connection to create.
3370 GCC_send_create (struct CadetConnection *c)
3372 enum CadetTunnelCState state;
3375 GCC_check_connections ();
3376 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3377 size += c->path->length * sizeof (struct GNUNET_PeerIdentity);
3379 /* Allocate message on the stack */
3380 unsigned char cbuf[size];
3381 struct GNUNET_CADET_ConnectionCreate *msg;
3382 struct GNUNET_PeerIdentity *peers;
3384 msg = (struct GNUNET_CADET_ConnectionCreate *) cbuf;
3385 msg->header.size = htons (size);
3386 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
3387 msg->reserved = htonl (0);
3388 msg->cid = *GCC_get_id (c);
3389 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3390 for (int i = 0; i < c->path->length; i++)
3392 GNUNET_PEER_resolve (c->path->peers[i], peers++);
3394 GNUNET_assert (NULL == c->maintenance_q);
3395 c->maintenance_q = GCP_send (get_next_hop (c),
3397 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0,
3399 &conn_message_sent, NULL);
3402 LOG (GNUNET_ERROR_TYPE_INFO, "==> %s %19s on conn %s (%p) FWD [%5u]\n",
3403 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE), "",
3404 GCC_2s (c), c, size);
3405 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3406 c, c->pending_messages);
3407 c->pending_messages++;
3409 state = GCT_get_cstate (c->t);
3410 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3411 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
3412 if (CADET_CONNECTION_NEW == c->state)
3413 connection_change_state (c, CADET_CONNECTION_SENT);
3414 GCC_check_connections ();
3419 * Send an ACK on the appropriate connection/channel, depending on
3420 * the direction and the position of the peer.
3422 * @param c Which connection to send the hop-by-hop ACK.
3423 * @param fwd Is this a fwd ACK? (will go dest->root).
3424 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
3427 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
3429 unsigned int buffer;
3431 GCC_check_connections ();
3432 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
3433 GC_f2s (fwd), GCC_2s (c));
3441 if (GNUNET_NO != c->destroy)
3443 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
3444 GCC_check_connections ();
3448 /* Get available buffer space */
3449 if (GCC_is_terminal (c, fwd))
3451 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
3452 buffer = GCT_get_channels_buffer (c->t);
3456 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
3457 buffer = GCC_get_buffer (c, fwd);
3459 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
3460 if (0 == buffer && GNUNET_NO == force)
3462 GCC_check_connections ();
3466 /* Send available buffer space */
3467 if (GNUNET_YES == GCC_is_origin (c, fwd))
3469 GNUNET_assert (NULL != c->t);
3470 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
3471 GCT_unchoke_channels (c->t);
3475 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
3476 send_ack (c, buffer, fwd, force);
3478 GCC_check_connections ();
3483 * Send a message to all peers in this connection that the connection
3484 * is no longer valid.
3486 * If some peer should not receive the message, it should be zero'ed out
3487 * before calling this function.
3489 * @param c The connection whose peers to notify.
3492 GCC_send_destroy (struct CadetConnection *c)
3494 struct GNUNET_CADET_ConnectionDestroy msg;
3496 if (GNUNET_YES == c->destroy)
3498 GCC_check_connections ();
3499 msg.header.size = htons (sizeof (msg));
3500 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3502 msg.reserved = htonl (0);
3503 LOG (GNUNET_ERROR_TYPE_DEBUG,
3504 " sending connection destroy for connection %s\n",
3507 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3508 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c,
3509 GNUNET_YES, GNUNET_YES, NULL, NULL);
3510 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3511 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c,
3512 GNUNET_NO, GNUNET_YES, NULL, NULL);
3514 GCC_check_connections ();
3519 * @brief Start a polling timer for the connection.
3521 * When a neighbor does not accept more traffic on the connection it could be
3522 * caused by a simple congestion or by a lost ACK. Polling enables to check
3523 * for the lastest ACK status for a connection.
3525 * @param c Connection.
3526 * @param fwd Should we poll in the FWD direction?
3529 GCC_start_poll (struct CadetConnection *c, int fwd)
3531 struct CadetFlowControl *fc;
3533 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3534 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3536 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3538 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL already in progress (t: %p, m: %p)\n",
3539 fc->poll_task, fc->poll_msg);
3542 if (0 == fc->queue_max)
3544 /* Should not be needed, traffic should've been cancelled. */
3546 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not possible, peer disconnected\n");
3549 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3550 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3551 &send_connection_poll,
3557 * @brief Stop polling a connection for ACKs.
3559 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3561 * @param c Connection.
3562 * @param fwd Should we stop the poll in the FWD direction?
3565 GCC_stop_poll (struct CadetConnection *c, int fwd)
3567 struct CadetFlowControl *fc;
3569 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3570 if (NULL != fc->poll_task)
3572 GNUNET_SCHEDULER_cancel (fc->poll_task);
3573 fc->poll_task = NULL;
3575 if (NULL != fc->poll_msg)
3577 GCC_cancel (fc->poll_msg);
3578 fc->poll_msg = NULL;
3584 * Get a (static) string for a connection.
3586 * @param c Connection.
3589 GCC_2s (const struct CadetConnection *c)
3596 static char buf[128];
3598 SPRINTF (buf, "%s (->%s)",
3599 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3602 return GNUNET_h2s (GC_h2hc (&c->id));
3607 * Log all possible info about the connection state.
3609 * @param c Connection to debug.
3610 * @param level Debug level to use.
3613 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3618 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3620 __FILE__, __FUNCTION__, __LINE__);
3626 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3630 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3631 s = path_2s (c->path);
3632 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3634 LOG2 (level, "CCC state: %s, destroy: %u\n",
3635 GCC_state2s (c->state), c->destroy);
3636 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3637 if (NULL != c->perf)
3638 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3640 LOG2 (level, "CCC FWD flow control:\n");
3641 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3642 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3643 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3644 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3645 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3646 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3647 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3648 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3650 LOG2 (level, "CCC BCK flow control:\n");
3651 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3652 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3653 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3654 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3655 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3656 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3657 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3658 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3660 LOG2 (level, "CCC DEBUG CONNECTION END\n");