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_AX)
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_AX:
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 GNUNET_assert (NULL ==
1087 GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c, fwd,
1088 GNUNET_YES, NULL, NULL));
1089 GCC_check_connections ();
1094 * Send a notification that a connection is broken, when a connection
1095 * isn't even known to the local peer or soon to be destroyed.
1097 * @param connection_id Connection ID.
1098 * @param id1 Peer that has disconnected, probably local peer.
1099 * @param id2 Peer that has disconnected can be NULL if unknown.
1100 * @param neighbor Peer to notify (neighbor who sent the connection).
1103 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
1104 const struct GNUNET_PeerIdentity *id1,
1105 const struct GNUNET_PeerIdentity *id2,
1106 struct CadetPeer *neighbor)
1108 struct GNUNET_CADET_ConnectionBroken msg;
1110 GCC_check_connections ();
1111 LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n",
1112 GNUNET_h2s (GC_h2hc (connection_id)));
1114 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1115 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1116 msg.cid = *connection_id;
1117 msg.reserved = htonl (0);
1122 memset (&msg.peer2, 0, sizeof (msg.peer2));
1123 GNUNET_assert (NULL != GCP_send (neighbor, &msg.header,
1125 NULL, GNUNET_SYSERR, /* connection, fwd */
1126 NULL, NULL)); /* continuation */
1127 GCC_check_connections ();
1132 * Send keepalive packets for a connection.
1134 * @param c Connection to keep alive..
1135 * @param fwd Is this a FWD keepalive? (owner -> dest).
1138 send_connection_keepalive (struct CadetConnection *c, int fwd)
1140 struct GNUNET_MessageHeader msg;
1141 struct CadetFlowControl *fc;
1144 GCC_check_connections ();
1145 LOG (GNUNET_ERROR_TYPE_INFO,
1146 "keepalive %s for connection %s\n",
1147 GC_f2s (fwd), GCC_2s (c));
1149 GNUNET_assert (NULL != c->t);
1150 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1151 tunnel_ready = GNUNET_YES == GCT_has_queued_traffic (c->t)
1152 && CADET_TUNNEL_KEY_OK <= GCT_get_estate (c->t);
1153 if (0 < fc->queue_n || tunnel_ready)
1155 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1159 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1161 GNUNET_assert (NULL != c->t);
1162 msg.size = htons (sizeof (msg));
1163 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
1165 GNUNET_assert (NULL ==
1166 GCT_send_prebuilt_message (&msg, c->t, c,
1167 GNUNET_NO, NULL, NULL));
1168 GCC_check_connections ();
1173 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1175 * @param c Connection for which to send the message.
1176 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1179 connection_recreate (struct CadetConnection *c, int fwd)
1181 LOG (GNUNET_ERROR_TYPE_DEBUG,
1182 "sending connection recreate\n");
1184 GCC_send_create (c);
1186 send_connection_ack (c, GNUNET_NO);
1191 * Generic connection timer management.
1192 * Depending on the role of the peer in the connection will send the
1193 * appropriate message (build or keepalive)
1195 * @param c Conncetion to maintain.
1196 * @param fwd Is FWD?
1199 connection_maintain (struct CadetConnection *c, int fwd)
1201 if (GNUNET_NO != c->destroy)
1203 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1210 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1214 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1216 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1218 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1219 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1220 schedule_next_keepalive (c, fwd);
1225 case CADET_CONNECTION_NEW:
1228 case CADET_CONNECTION_SENT:
1229 connection_recreate (c, fwd);
1231 case CADET_CONNECTION_READY:
1232 send_connection_keepalive (c, fwd);
1241 * Keep the connection alive.
1243 * @param c Connection to keep alive.
1244 * @param fwd Direction.
1247 connection_keepalive (struct CadetConnection *c,
1250 GCC_check_connections ();
1251 LOG (GNUNET_ERROR_TYPE_DEBUG,
1252 "%s keepalive for %s\n",
1253 GC_f2s (fwd), GCC_2s (c));
1256 c->fwd_maintenance_task = NULL;
1258 c->bck_maintenance_task = NULL;
1259 connection_maintain (c, fwd);
1260 GCC_check_connections ();
1261 /* Next execution will be scheduled by message_sent or _maintain*/
1266 * Keep the connection alive in the FWD direction.
1268 * @param cls Closure (connection to keepalive).
1271 connection_fwd_keepalive (void *cls)
1273 struct CadetConnection *c = cls;
1275 GCC_check_connections ();
1276 connection_keepalive (c,
1278 GCC_check_connections ();
1283 * Keep the connection alive in the BCK direction.
1285 * @param cls Closure (connection to keepalive).
1288 connection_bck_keepalive (void *cls)
1290 struct CadetConnection *c = cls;
1292 GCC_check_connections ();
1293 connection_keepalive (c,
1295 GCC_check_connections ();
1300 * Schedule next keepalive task, taking in consideration
1301 * the connection state and number of retries.
1303 * If the peer is not the origin, do nothing.
1305 * @param c Connection for which to schedule the next keepalive.
1306 * @param fwd Direction for the next keepalive.
1309 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1311 struct GNUNET_TIME_Relative delay;
1312 struct GNUNET_SCHEDULER_Task * *task_id;
1313 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1315 GCC_check_connections ();
1316 if (GNUNET_NO == GCC_is_origin (c, fwd))
1319 /* Calculate delay to use, depending on the state of the connection */
1320 if (CADET_CONNECTION_READY == c->state)
1322 delay = refresh_connection_time;
1326 if (1 > c->create_retry)
1327 c->create_retry = 1;
1328 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1330 if (c->create_retry < 64) // TODO make configurable
1331 c->create_retry *= 2;
1334 /* Select direction-dependent parameters */
1335 if (GNUNET_YES == fwd)
1337 task_id = &c->fwd_maintenance_task;
1338 keepalive_task = &connection_fwd_keepalive;
1342 task_id = &c->bck_maintenance_task;
1343 keepalive_task = &connection_bck_keepalive;
1346 /* Check that no one scheduled it before us */
1347 if (NULL != *task_id)
1349 /* No need for a _break. It can happen for instance when sending a SYNACK
1350 * for a duplicate SYN: the first SYNACK scheduled the task. */
1351 GNUNET_SCHEDULER_cancel (*task_id);
1354 /* Schedule the task */
1355 *task_id = GNUNET_SCHEDULER_add_delayed (delay,
1358 LOG (GNUNET_ERROR_TYPE_DEBUG,
1359 "next keepalive in %s\n",
1360 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1361 GCC_check_connections ();
1366 * Cancel all transmissions that belong to a certain connection.
1368 * If the connection is scheduled for destruction and no more messages are left,
1369 * the connection will be destroyed by the continuation call.
1371 * @param c Connection which to cancel. Might be destroyed during this call.
1372 * @param fwd Cancel fwd traffic?
1375 connection_cancel_queues (struct CadetConnection *c,
1378 struct CadetFlowControl *fc;
1380 GCC_check_connections ();
1381 LOG (GNUNET_ERROR_TYPE_DEBUG,
1382 "Cancel %s queues for connection %s\n",
1383 GC_f2s (fwd), GCC_2s (c));
1390 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1391 if (NULL != fc->poll_task)
1393 GNUNET_SCHEDULER_cancel (fc->poll_task);
1394 fc->poll_task = NULL;
1395 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL task for fc %p\n", fc);
1397 if (NULL != fc->poll_msg)
1399 GCC_cancel (fc->poll_msg);
1400 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL msg for fc %p\n", fc);
1403 while (NULL != fc->q_head)
1405 GCC_cancel (fc->q_head);
1407 GCC_check_connections ();
1412 * Function called if a connection has been stalled for a while,
1413 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1415 * @param cls Closure (poll ctx).
1418 send_connection_poll (void *cls);
1422 * Callback called when a queued POLL message is sent.
1424 * @param cls Closure (flow control context).
1425 * @param c Connection this message was on.
1426 * @param q Queue handler this call invalidates.
1427 * @param type Type of message sent.
1428 * @param fwd Was this a FWD going message?
1429 * @param size Size of the message.
1432 poll_sent (void *cls,
1433 struct CadetConnection *c,
1434 struct CadetConnectionQueue *q,
1435 uint16_t type, int fwd, size_t size)
1437 struct CadetFlowControl *fc = cls;
1439 GNUNET_assert (fc->poll_msg == q);
1440 fc->poll_msg = NULL;
1441 if (2 == c->destroy)
1443 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1446 if (0 == fc->queue_max)
1448 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL cancelled: neighbor disconnected\n");
1451 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1453 GNUNET_assert (NULL == fc->poll_task);
1454 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1455 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1456 &send_connection_poll,
1458 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1463 * Function called if a connection has been stalled for a while,
1464 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1466 * @param cls Closure (poll ctx).
1469 send_connection_poll (void *cls)
1471 struct CadetFlowControl *fc = cls;
1472 struct GNUNET_CADET_Poll msg;
1473 struct CadetConnection *c;
1476 fc->poll_task = NULL;
1477 GCC_check_connections ();
1479 fwd = fc == &c->fwd_fc;
1480 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1481 GCC_2s (c), GC_f2s (fwd));
1483 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1484 msg.header.size = htons (sizeof (msg));
1486 msg.pid = htonl (fc->last_pid_sent);
1487 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1489 GCC_send_prebuilt_message (&msg.header, UINT16_MAX, fc->last_pid_sent, c,
1490 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1491 GNUNET_assert (NULL != fc->poll_msg);
1492 GCC_check_connections ();
1497 * Generic connection timeout implementation.
1499 * Timeout function due to lack of keepalive/traffic from an endpoint.
1500 * Destroys connection if called.
1502 * @param c Connection to destroy.
1503 * @param fwd Was the timeout from the origin? (FWD timeout)
1506 connection_timeout (struct CadetConnection *c, int fwd)
1508 GCC_check_connections ();
1510 LOG (GNUNET_ERROR_TYPE_INFO,
1511 "Connection %s %s timed out. Destroying.\n",
1514 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1516 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1522 /* If dest, send "broken" notification. */
1523 if (GCC_is_terminal (c, fwd))
1525 struct CadetPeer *next_hop;
1527 next_hop = fwd ? get_prev_hop (c) : get_next_hop (c);
1528 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1532 GCC_check_connections ();
1537 * Timeout function due to lack of keepalive/traffic from the owner.
1538 * Destroys connection if called.
1540 * @param cls Closure (connection to destroy).
1543 connection_fwd_timeout (void *cls)
1545 struct CadetConnection *c = cls;
1547 c->fwd_maintenance_task = NULL;
1548 GCC_check_connections ();
1549 connection_timeout (c, GNUNET_YES);
1550 GCC_check_connections ();
1555 * Timeout function due to lack of keepalive/traffic from the destination.
1556 * Destroys connection if called.
1558 * @param cls Closure (connection to destroy).
1561 connection_bck_timeout (void *cls)
1563 struct CadetConnection *c = cls;
1565 c->bck_maintenance_task = NULL;
1566 GCC_check_connections ();
1567 connection_timeout (c, GNUNET_NO);
1568 GCC_check_connections ();
1573 * Resets the connection timeout task, some other message has done the
1575 * - For the first peer on the direction this means to send
1576 * a keepalive or a path confirmation message (either create or ACK).
1577 * - For all other peers, this means to destroy the connection,
1578 * due to lack of activity.
1579 * Starts the timeout if no timeout was running (connection just created).
1581 * @param c Connection whose timeout to reset.
1582 * @param fwd Is this forward?
1584 * TODO use heap to improve efficiency of scheduler.
1587 connection_reset_timeout (struct CadetConnection *c, int fwd)
1589 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1590 if (GCC_is_origin (c, fwd)) /* Startpoint */
1592 schedule_next_keepalive (c, fwd);
1593 if (NULL != c->maintenance_q)
1595 GCP_send_cancel (c->maintenance_q);
1596 c->maintenance_q = NULL; /* Is set to NULL by conn_message_sent anyway */
1599 else /* Relay, endpoint. */
1601 struct GNUNET_TIME_Relative delay;
1602 struct GNUNET_SCHEDULER_Task * *ti;
1603 GNUNET_SCHEDULER_TaskCallback f;
1605 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1608 GNUNET_SCHEDULER_cancel (*ti);
1609 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1610 LOG (GNUNET_ERROR_TYPE_DEBUG,
1611 " timing out in %s\n",
1612 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1613 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1614 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1620 * Iterator to compare each connection's path with the path of a new connection.
1622 * If the connection coincides, the c member of path is set to the connection
1623 * and the destroy flag of the connection is set.
1625 * @param cls Closure (new path).
1626 * @param c Connection in the tunnel to check.
1629 check_path (void *cls, struct CadetConnection *c)
1631 struct CadetConnection *new_conn = cls;
1632 struct CadetPeerPath *path = new_conn->path;
1634 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1635 GCC_2s (c), c, c->path->length);
1638 && GNUNET_NO == c->destroy
1639 && CADET_CONNECTION_BROKEN != c->state
1640 && CADET_CONNECTION_DESTROYED != c->state
1641 && path_equivalent (path, c->path))
1643 new_conn->destroy = GNUNET_YES; /* Do not mark_destroyed, */
1644 new_conn->path->c = c; /* this is only a flag for the Iterator. */
1645 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1651 * Finds out if this path is already being used by an existing connection.
1653 * Checks the tunnel towards the destination to see if it contains
1654 * any connection with the same path.
1656 * If the existing connection is ready, it is kept.
1657 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1658 * the peer will eventually reject our attempt).
1660 * @param path Path to check.
1661 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1662 * #GNUNET_NO otherwise.
1665 does_connection_exist (struct CadetConnection *conn)
1667 struct CadetPeer *p;
1668 struct CadetTunnel *t;
1669 struct CadetConnection *c;
1671 p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
1674 t = GCP_get_tunnel (p);
1678 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1680 GCT_iterate_connections (t, &check_path, conn);
1682 if (GNUNET_YES == conn->destroy)
1685 conn->destroy = GNUNET_NO;
1686 conn->path->c = conn;
1687 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1688 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1689 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1690 if (CADET_CONNECTION_READY == c->state)
1692 /* The other peer confirmed a live connection with this path,
1693 * why are they trying to duplicate it? */
1694 GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
1697 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not ready, connection unique\n");
1702 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1709 * @brief Check if the tunnel this connection belongs to has any other
1710 * connection with the same path, and destroy one if so.
1712 * @param cls Closure (connection to check).
1715 check_duplicates (void *cls)
1717 struct CadetConnection *c = cls;
1719 c->check_duplicates_task = NULL;
1720 if (GNUNET_YES == does_connection_exist (c))
1722 GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1723 send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
1730 * Wait for enough time to let any dead connections time out and check for
1731 * any remaining duplicates.
1733 * @param c Connection that is a potential duplicate.
1736 schedule_check_duplicates (struct CadetConnection *c)
1738 struct GNUNET_TIME_Relative delay;
1740 if (NULL != c->check_duplicates_task)
1742 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 5);
1743 c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
1750 * Add the connection to the list of both neighbors.
1752 * @param c Connection.
1754 * @return #GNUNET_OK if everything went fine
1755 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1758 register_neighbors (struct CadetConnection *c)
1760 c->next_peer = get_next_hop (c);
1761 c->prev_peer = get_prev_hop (c);
1762 GNUNET_assert (c->next_peer != c->prev_peer);
1763 LOG (GNUNET_ERROR_TYPE_DEBUG,
1764 "register neighbors for connection %s\n",
1766 path_debug (c->path);
1767 LOG (GNUNET_ERROR_TYPE_DEBUG,
1768 "own pos %u\n", c->own_pos);
1769 LOG (GNUNET_ERROR_TYPE_DEBUG,
1770 "putting connection %s to next peer %p\n",
1773 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1775 GCP_2s (c->next_peer));
1776 LOG (GNUNET_ERROR_TYPE_DEBUG,
1777 "putting connection %s to prev peer %p\n",
1780 LOG (GNUNET_ERROR_TYPE_DEBUG,
1781 "prev peer %p %s\n",
1783 GCP_2s (c->prev_peer));
1785 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1786 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1788 if (GCC_is_origin (c, GNUNET_YES))
1789 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1790 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1792 LOG (GNUNET_ERROR_TYPE_DEBUG,
1793 " register neighbors failed\n");
1794 LOG (GNUNET_ERROR_TYPE_DEBUG,
1795 " prev: %s, neighbor?: %d\n",
1796 GCP_2s (c->prev_peer),
1797 GCP_is_neighbor (c->prev_peer));
1798 LOG (GNUNET_ERROR_TYPE_DEBUG,
1799 " next: %s, neighbor?: %d\n",
1800 GCP_2s (c->next_peer),
1801 GCP_is_neighbor (c->next_peer));
1802 return GNUNET_SYSERR;
1804 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1805 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1812 * Remove the connection from the list of both neighbors.
1814 * @param c Connection.
1817 unregister_neighbors (struct CadetConnection *c)
1819 // struct CadetPeer *peer; FIXME dont use next_peer, prev_peer
1820 /* Either already unregistered or never got registered, it's ok either way. */
1821 if (NULL == c->path)
1823 if (NULL != c->next_peer)
1825 GCP_remove_connection (c->next_peer, c);
1826 c->next_peer = NULL;
1828 if (NULL != c->prev_peer)
1830 GCP_remove_connection (c->prev_peer, c);
1831 c->prev_peer = NULL;
1837 * Invalidates all paths towards all peers that comprise the connection which
1838 * rely on the disconnected peer.
1840 * ~O(n^3) (peers in connection * paths/peer * links/path)
1842 * @param c Connection whose peers' paths to clean.
1843 * @param disconnected Peer that disconnected.
1846 invalidate_paths (struct CadetConnection *c,
1847 struct CadetPeer *disconnected)
1849 struct CadetPeer *peer;
1852 for (i = 0; i < c->path->length; i++)
1854 peer = GCP_get_short (c->path->peers[i], GNUNET_NO);
1856 GCP_notify_broken_link (peer, &my_full_id, GCP_get_id (disconnected));
1862 * Bind the connection to the peer and the tunnel to that peer.
1864 * If the peer has no tunnel, create one. Update tunnel and connection
1865 * data structres to reflect new status.
1867 * @param c Connection.
1871 add_to_peer (struct CadetConnection *c,
1872 struct CadetPeer *peer)
1874 GCP_add_tunnel (peer);
1875 c->t = GCP_get_tunnel (peer);
1876 GCT_add_connection (c->t, c);
1881 * Log receipt of message on stderr (INFO level).
1883 * @param message Message received.
1884 * @param peer Peer who sent the message.
1885 * @param conn_id Connection ID of the message.
1888 log_message (const struct GNUNET_MessageHeader *message,
1889 const struct CadetPeer *peer,
1890 const struct GNUNET_CADET_Hash *conn_id)
1896 size = ntohs (message->size);
1897 type = ntohs (message->type);
1900 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1901 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1902 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1903 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1909 LOG (GNUNET_ERROR_TYPE_INFO, "<%s %s on conn %s from %s, %6u bytes\n",
1910 arrow, GC_m2s (type), GNUNET_h2s (GC_h2hc (conn_id)),
1911 GCP_2s(peer), (unsigned int) size);
1914 /******************************************************************************/
1915 /******************************** API ***********************************/
1916 /******************************************************************************/
1919 * Handler for connection creation.
1921 * @param peer Message sender (neighbor).
1922 * @param msg Message itself.
1925 GCC_handle_create (struct CadetPeer *peer,
1926 const struct GNUNET_CADET_ConnectionCreate *msg)
1928 const struct GNUNET_CADET_Hash *cid;
1929 struct GNUNET_PeerIdentity *id;
1930 struct CadetPeerPath *path;
1931 struct CadetPeer *dest_peer;
1932 struct CadetPeer *orig_peer;
1933 struct CadetConnection *c;
1934 unsigned int own_pos;
1937 GCC_check_connections ();
1938 size = ntohs (msg->header.size);
1940 /* Calculate hops */
1941 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1942 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1944 GNUNET_break_op (0);
1947 size /= sizeof (struct GNUNET_PeerIdentity);
1950 GNUNET_break_op (0);
1953 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1955 /* Get parameters */
1957 log_message (&msg->header, peer, cid);
1958 id = (struct GNUNET_PeerIdentity *) &msg[1];
1959 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1961 /* Create connection */
1962 c = connection_get (cid);
1965 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1966 size, myid, &own_pos);
1969 /* Path was malformed, probably our own ID was not in it. */
1970 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
1971 GNUNET_break_op (0);
1976 /* We received this request from a neighbor, we cannot be origin */
1977 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
1978 GNUNET_break_op (0);
1979 path_destroy (path);
1983 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1984 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1985 c = GCC_new (cid, NULL, path, own_pos);
1988 if (path->length - 1 == own_pos)
1990 /* If we are destination, why did the creation fail? */
1992 path_destroy (path);
1993 GCC_check_connections ();
1996 send_broken_unknown (cid, &my_full_id,
1997 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1999 path_destroy (path);
2000 GCC_check_connections ();
2003 GCP_add_path_to_all (path, GNUNET_NO);
2004 connection_reset_timeout (c, GNUNET_YES);
2008 path = path_duplicate (c->path);
2010 if (CADET_CONNECTION_NEW == c->state)
2011 connection_change_state (c, CADET_CONNECTION_SENT);
2013 /* Remember peers */
2014 dest_peer = GCP_get (&id[size - 1], GNUNET_YES);
2015 orig_peer = GCP_get (&id[0], GNUNET_YES);
2017 /* Is it a connection to us? */
2018 if (c->own_pos == path->length - 1)
2020 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2021 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
2023 add_to_peer (c, orig_peer);
2024 if (GNUNET_YES == does_connection_exist (c))
2026 /* Peer created a connection equal to one we think exists
2028 * Solution: Keep both and postpone disambiguation. In the meantime
2029 * the connection will time out or peer will inform us it is broken.
2032 * - Use explicit duplicate.
2033 * - Accept new conn and destroy the old. (interruption in higher level)
2034 * - Keep the one with higher ID / created by peer with higher ID. */
2035 schedule_check_duplicates (c);
2038 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
2039 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
2040 if (NULL == c->maintenance_q)
2041 send_connection_ack (c, GNUNET_NO);
2042 if (CADET_CONNECTION_SENT == c->state)
2043 connection_change_state (c, CADET_CONNECTION_ACK);
2047 /* It's for somebody else! Retransmit. */
2048 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2049 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2050 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2051 GNUNET_assert (NULL ==
2052 GCC_send_prebuilt_message (&msg->header, 0, 0, c,
2053 GNUNET_YES, GNUNET_YES,
2056 path_destroy (path);
2057 GCC_check_connections ();
2062 * Handler for connection confirmations.
2064 * @param peer Message sender (neighbor).
2065 * @param msg Message itself.
2068 GCC_handle_confirm (struct CadetPeer *peer,
2069 const struct GNUNET_CADET_ConnectionACK *msg)
2071 struct CadetConnection *c;
2072 enum CadetConnectionState oldstate;
2075 GCC_check_connections ();
2076 log_message (&msg->header, peer, &msg->cid);
2077 c = connection_get (&msg->cid);
2080 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2082 LOG (GNUNET_ERROR_TYPE_DEBUG,
2083 " don't know the connection!\n");
2084 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2085 GCC_check_connections ();
2088 if (GNUNET_NO != c->destroy)
2090 GNUNET_assert (CADET_CONNECTION_DESTROYED == c->state);
2091 GNUNET_STATISTICS_update (stats, "# control on dying connection",
2093 LOG (GNUNET_ERROR_TYPE_DEBUG,
2094 "connection %s being destroyed, ignoring confirm\n",
2096 GCC_check_connections ();
2100 oldstate = c->state;
2101 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GCP_2s (peer));
2102 if (get_next_hop (c) == peer)
2104 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2106 if (CADET_CONNECTION_SENT == oldstate)
2107 connection_change_state (c, CADET_CONNECTION_ACK);
2109 else if (get_prev_hop (c) == peer)
2111 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2113 connection_change_state (c, CADET_CONNECTION_READY);
2117 GNUNET_STATISTICS_update (stats, "# control on connection from wrong peer",
2119 GNUNET_break_op (0);
2123 connection_reset_timeout (c, fwd);
2125 /* Add path to peers? */
2126 if (NULL != c->path)
2128 GCP_add_path_to_all (c->path, GNUNET_YES);
2135 /* Message for us as creator? */
2136 if (GNUNET_YES == GCC_is_origin (c, GNUNET_YES))
2138 if (GNUNET_NO != fwd)
2143 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2145 /* If just created, cancel the short timeout and start a long one */
2146 if (CADET_CONNECTION_SENT == oldstate)
2147 connection_reset_timeout (c, GNUNET_YES);
2149 /* Change connection state, send ACK */
2150 connection_change_state (c, CADET_CONNECTION_READY);
2151 send_connection_ack (c, GNUNET_YES);
2153 /* Change tunnel state, trigger KX */
2154 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2155 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2156 GCC_check_connections ();
2160 /* Message for us as destination? */
2161 if (GCC_is_terminal (c, GNUNET_YES))
2163 if (GNUNET_YES != fwd)
2168 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2170 /* If just created, cancel the short timeout and start a long one */
2171 if (CADET_CONNECTION_ACK == oldstate)
2172 connection_reset_timeout (c, GNUNET_NO);
2174 /* Change tunnel state */
2175 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2176 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2177 GCC_check_connections ();
2181 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2182 GNUNET_assert (NULL ==
2183 GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2184 GNUNET_YES, NULL, NULL));
2185 GCC_check_connections ();
2191 * Handler for notifications of broken connections.
2193 * @param peer Message sender (neighbor).
2194 * @param msg Message itself.
2197 GCC_handle_broken (struct CadetPeer *peer,
2198 const struct GNUNET_CADET_ConnectionBroken *msg)
2200 struct CadetConnection *c;
2201 struct CadetTunnel *t;
2204 GCC_check_connections ();
2205 log_message (&msg->header, peer, &msg->cid);
2206 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", GNUNET_i2s (&msg->peer1));
2207 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", GNUNET_i2s (&msg->peer2));
2208 c = connection_get (&msg->cid);
2211 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2212 GNUNET_STATISTICS_update (stats, "# duplicate CONNECTION_BROKEN",
2214 GCC_check_connections ();
2220 fwd = is_fwd (c, peer);
2221 if (GNUNET_SYSERR == fwd)
2223 GNUNET_break_op (0);
2224 GCC_check_connections ();
2228 if (GCC_is_terminal (c, fwd))
2230 struct CadetPeer *endpoint;
2234 /* A terminal connection should not have 't' set to NULL. */
2236 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2239 endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
2240 if (2 < c->path->length)
2241 path_invalidate (c->path);
2242 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2244 connection_change_state (c, CADET_CONNECTION_BROKEN);
2245 GCT_remove_connection (t, c);
2252 GNUNET_assert (NULL ==
2253 GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2254 GNUNET_YES, NULL, NULL));
2255 connection_cancel_queues (c, !fwd);
2257 GCC_check_connections ();
2263 * Handler for notifications of destroyed connections.
2265 * @param peer Message sender (neighbor).
2266 * @param msg Message itself.
2269 GCC_handle_destroy (struct CadetPeer *peer,
2270 const struct GNUNET_CADET_ConnectionDestroy *msg)
2272 struct CadetConnection *c;
2275 GCC_check_connections ();
2276 log_message (&msg->header, peer, &msg->cid);
2277 c = connection_get (&msg->cid);
2280 /* Probably already got the message from another path,
2281 * destroyed the tunnel and retransmitted to children.
2284 GNUNET_STATISTICS_update (stats,
2285 "# control on unknown connection",
2287 LOG (GNUNET_ERROR_TYPE_DEBUG,
2288 " connection unknown destroyed: previously destroyed?\n");
2289 GCC_check_connections ();
2293 fwd = is_fwd (c, peer);
2294 if (GNUNET_SYSERR == fwd)
2296 GNUNET_break_op (0);
2297 GCC_check_connections ();
2301 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2303 GNUNET_assert (NULL ==
2304 GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2305 GNUNET_YES, NULL, NULL));
2307 else if (0 == c->pending_messages)
2309 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
2311 GCC_check_connections ();
2317 GCT_remove_connection (c->t, c);
2320 GCC_check_connections ();
2326 * Handler for cadet network traffic hop-by-hop acks.
2328 * @param peer Message sender (neighbor).
2329 * @param msg Message itself.
2332 GCC_handle_ack (struct CadetPeer *peer,
2333 const struct GNUNET_CADET_ACK *msg)
2335 struct CadetConnection *c;
2336 struct CadetFlowControl *fc;
2340 GCC_check_connections ();
2341 log_message (&msg->header, peer, &msg->cid);
2342 c = connection_get (&msg->cid);
2345 GNUNET_STATISTICS_update (stats,
2346 "# ack on unknown connection",
2349 send_broken_unknown (&msg->cid,
2353 GCC_check_connections ();
2357 /* Is this a forward or backward ACK? */
2358 if (get_next_hop (c) == peer)
2363 else if (get_prev_hop (c) == peer)
2370 GNUNET_break_op (0);
2374 ack = ntohl (msg->ack);
2375 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2376 GC_f2s (fwd), ack, fc->last_ack_recv);
2377 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2378 fc->last_ack_recv = ack;
2380 /* Cancel polling if the ACK is big enough. */
2381 if (NULL != fc->poll_task &&
2382 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2384 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2385 GNUNET_SCHEDULER_cancel (fc->poll_task);
2386 fc->poll_task = NULL;
2387 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2390 GCC_check_connections ();
2395 * Handler for cadet network traffic hop-by-hop data counter polls.
2397 * @param peer Message sender (neighbor).
2398 * @param msg Message itself.
2401 GCC_handle_poll (struct CadetPeer *peer,
2402 const struct GNUNET_CADET_Poll *msg)
2404 struct CadetConnection *c;
2405 struct CadetFlowControl *fc;
2409 GCC_check_connections ();
2410 log_message (&msg->header, peer, &msg->cid);
2411 c = connection_get (&msg->cid);
2414 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2416 LOG (GNUNET_ERROR_TYPE_DEBUG,
2417 "POLL message on unknown connection %s!\n",
2418 GNUNET_h2s (GC_h2hc (&msg->cid)));
2419 send_broken_unknown (&msg->cid,
2423 GCC_check_connections ();
2427 /* Is this a forward or backward ACK?
2428 * Note: a poll should never be needed in a loopback case,
2429 * since there is no possiblility of packet loss there, so
2430 * this way of discerining FWD/BCK should not be a problem.
2432 if (get_next_hop (c) == peer)
2434 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2437 else if (get_prev_hop (c) == peer)
2439 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2444 GNUNET_break_op (0);
2448 pid = ntohl (msg->pid);
2449 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2450 fc->last_pid_recv = pid;
2451 fwd = fc == &c->bck_fc;
2452 GCC_send_ack (c, fwd, GNUNET_YES);
2453 GCC_check_connections ();
2458 * Check the message against internal state and test if it goes FWD or BCK.
2460 * Updates the PID, state and timeout values for the connection.
2462 * @param message Message to check. It must belong to an existing connection.
2463 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2464 * @param c Connection this message should belong. If NULL, check fails.
2465 * @param sender Neighbor that sent the message.
2467 * @return #GNUNET_YES if the message goes FWD.
2468 * #GNUNET_NO if it goes BCK.
2469 * #GNUNET_SYSERR if there is an error (unauthorized sender, ...).
2472 check_message (const struct GNUNET_MessageHeader *message,
2473 const struct GNUNET_CADET_Hash* cid,
2474 struct CadetConnection *c,
2475 struct CadetPeer *sender,
2478 struct CadetFlowControl *fc;
2479 struct CadetPeer *hop;
2483 /* Check connection */
2486 GNUNET_STATISTICS_update (stats,
2487 "# unknown connection",
2489 LOG (GNUNET_ERROR_TYPE_DEBUG,
2490 "%s on unknown connection %s\n",
2491 GC_m2s (ntohs (message->type)),
2492 GNUNET_h2s (GC_h2hc (cid)));
2493 send_broken_unknown (cid,
2497 return GNUNET_SYSERR;
2500 /* Check if origin is as expected */
2501 hop = get_prev_hop (c);
2508 hop = get_next_hop (c);
2509 GNUNET_break (hop == c->next_peer);
2516 /* Unexpected peer sending traffic on a connection. */
2517 GNUNET_break_op (0);
2518 return GNUNET_SYSERR;
2522 /* Check PID for payload messages */
2523 type = ntohs (message->type);
2524 if (GNUNET_MESSAGE_TYPE_CADET_AX == type)
2526 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2527 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2528 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2529 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2531 GNUNET_break_op (0);
2532 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2533 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2534 pid, fc->last_pid_recv, fc->last_ack_sent);
2535 return GNUNET_SYSERR;
2537 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2541 delta = pid - fc->last_pid_recv;
2542 fc->last_pid_recv = pid;
2543 fc->recv_bitmap <<= delta;
2544 fc->recv_bitmap |= 1;
2548 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2549 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2551 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2552 pid, fc->last_pid_recv - 31);
2553 return GNUNET_SYSERR;
2555 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2559 /* Count as connection confirmation. */
2560 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2562 connection_change_state (c, CADET_CONNECTION_READY);
2565 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2566 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2569 connection_reset_timeout (c, fwd);
2576 * Handler for key exchange traffic (Axolotl KX).
2578 * @param peer Message sender (neighbor).
2579 * @param msg Message itself.
2582 GCC_handle_kx (struct CadetPeer *peer,
2583 const struct GNUNET_CADET_KX *msg)
2585 const struct GNUNET_CADET_Hash* cid;
2586 struct CadetConnection *c;
2589 GCC_check_connections ();
2591 log_message (&msg->header, peer, cid);
2593 c = connection_get (cid);
2594 fwd = check_message (&msg->header,
2600 /* If something went wrong, discard message. */
2601 if (GNUNET_SYSERR == fwd)
2603 GNUNET_break_op (0);
2604 GCC_check_connections ();
2608 /* Is this message for us? */
2609 if (GCC_is_terminal (c, fwd))
2611 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2612 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2618 GCT_handle_kx (c->t, &msg[1].header);
2619 GCC_check_connections ();
2623 /* Message not for us: forward to next hop */
2624 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2625 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2626 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2627 GNUNET_NO, NULL, NULL));
2628 GCC_check_connections ();
2633 * Handler for encrypted cadet network traffic (channel mgmt, data).
2635 * @param peer Message sender (neighbor).
2636 * @param msg Message itself.
2639 GCC_handle_encrypted (struct CadetPeer *peer,
2640 const struct GNUNET_CADET_AX *msg)
2642 const struct GNUNET_CADET_Hash* cid;
2643 struct CadetConnection *c;
2647 GCC_check_connections ();
2649 pid = ntohl (msg->pid);
2650 log_message (&msg->header, peer, cid);
2652 c = connection_get (cid);
2653 fwd = check_message (&msg->header,
2659 /* If something went wrong, discard message. */
2660 if (GNUNET_SYSERR == fwd)
2662 GNUNET_break_op (0);
2663 GCC_check_connections ();
2667 /* Is this message for us? */
2668 if (GCC_is_terminal (c, fwd))
2670 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2674 GNUNET_break (GNUNET_NO != c->destroy);
2677 GCT_handle_encrypted (c->t, &msg->header);
2678 GCC_send_ack (c, fwd, GNUNET_NO);
2679 GCC_check_connections ();
2683 /* Message not for us: forward to next hop */
2684 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2685 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2686 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2687 GNUNET_NO, NULL, NULL));
2688 GCC_check_connections ();
2693 * Initialize the connections subsystem
2695 * @param c Configuration handle.
2698 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2700 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2702 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2705 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2706 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2707 GNUNET_SCHEDULER_shutdown ();
2712 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2715 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2716 "CADET", "MAX_CONNECTIONS", "MISSING");
2717 GNUNET_SCHEDULER_shutdown ();
2722 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2723 &refresh_connection_time))
2725 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2726 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2727 GNUNET_SCHEDULER_shutdown ();
2730 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2731 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2736 * Destroy each connection on shutdown.
2738 * @param cls Closure (unused).
2739 * @param key Current key code (CID, unused).
2740 * @param value Value in the hash map (`struct CadetConnection`)
2742 * @return #GNUNET_YES, because we should continue to iterate
2745 shutdown_iterator (void *cls,
2746 const struct GNUNET_HashCode *key,
2749 struct CadetConnection *c = value;
2751 c->state = CADET_CONNECTION_DESTROYED;
2758 * Shut down the connections subsystem.
2763 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n");
2764 GCC_check_connections ();
2765 GNUNET_CONTAINER_multihashmap_iterate (connections,
2768 GNUNET_CONTAINER_multihashmap_destroy (connections);
2774 * Create a connection.
2776 * @param cid Connection ID (either created locally or imposed remotely).
2777 * @param t Tunnel this connection belongs to (or NULL for transit connections);
2778 * @param path Path this connection has to use (copy is made).
2779 * @param own_pos Own position in the @c path path.
2781 * @return Newly created connection.
2782 * NULL in case of error: own id not in path, wrong neighbors, ...
2784 struct CadetConnection *
2785 GCC_new (const struct GNUNET_CADET_Hash *cid,
2786 struct CadetTunnel *t,
2787 struct CadetPeerPath *path,
2788 unsigned int own_pos)
2790 struct CadetConnection *c;
2791 struct CadetPeerPath *cpath;
2793 GCC_check_connections ();
2794 cpath = path_duplicate (path);
2795 GNUNET_assert (NULL != cpath);
2796 c = GNUNET_new (struct CadetConnection);
2798 GNUNET_assert (GNUNET_OK ==
2799 GNUNET_CONTAINER_multihashmap_put (connections,
2801 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2802 fc_init (&c->fwd_fc);
2803 fc_init (&c->bck_fc);
2808 GNUNET_assert (own_pos <= cpath->length - 1);
2809 c->own_pos = own_pos;
2812 if (GNUNET_OK != register_neighbors (c))
2816 /* We were the origin of this request, this means we have invalid
2817 * info about the paths to reach the destination. We must invalidate
2818 * the *original* path to avoid trying it again in the next minute.
2820 if (2 < path->length)
2821 path_invalidate (path);
2825 GCT_debug(t, GNUNET_ERROR_TYPE_WARNING);
2829 path_destroy (c->path);
2834 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2835 GCC_check_connections ();
2841 * Connection is no longer needed: destroy it.
2843 * Cancels all pending traffic (including possible DESTROY messages), all
2844 * maintenance tasks and removes the connection from neighbor peers and tunnel.
2846 * @param c Connection to destroy.
2849 GCC_destroy (struct CadetConnection *c)
2851 GCC_check_connections ();
2858 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2859 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2862 LOG (GNUNET_ERROR_TYPE_DEBUG,
2863 "destroying connection %s\n",
2865 LOG (GNUNET_ERROR_TYPE_DEBUG,
2866 " fc's f: %p, b: %p\n",
2867 &c->fwd_fc, &c->bck_fc);
2868 LOG (GNUNET_ERROR_TYPE_DEBUG,
2869 " fc tasks f: %u, b: %u\n",
2870 c->fwd_fc.poll_task,
2871 c->bck_fc.poll_task);
2873 /* Cancel all traffic */
2874 if (NULL != c->path)
2876 connection_cancel_queues (c, GNUNET_YES);
2877 connection_cancel_queues (c, GNUNET_NO);
2878 if (NULL != c->maintenance_q)
2880 GCP_send_cancel (c->maintenance_q);
2881 c->maintenance_q = NULL;
2884 unregister_neighbors (c);
2885 path_destroy (c->path);
2888 /* Delete from tunnel */
2890 GCT_remove_connection (c->t, c);
2892 if (NULL != c->check_duplicates_task)
2893 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
2894 if (NULL != c->fwd_maintenance_task)
2895 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2896 if (NULL != c->bck_maintenance_task)
2897 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2899 if (GNUNET_NO == c->was_removed)
2901 GNUNET_break (GNUNET_YES ==
2902 GNUNET_CONTAINER_multihashmap_remove (connections,
2906 GNUNET_STATISTICS_update (stats,
2911 GCC_check_connections ();
2916 * Get the connection ID.
2918 * @param c Connection to get the ID from.
2920 * @return ID of the connection.
2922 const struct GNUNET_CADET_Hash *
2923 GCC_get_id (const struct CadetConnection *c)
2930 * Get the connection ID.
2932 * @param c Connection to get the ID from.
2934 * @return ID of the connection.
2936 const struct GNUNET_HashCode *
2937 GCC_get_h (const struct CadetConnection *c)
2939 return GC_h2hc (&c->id);
2944 * Get the connection path.
2946 * @param c Connection to get the path from.
2948 * @return path used by the connection.
2950 const struct CadetPeerPath *
2951 GCC_get_path (const struct CadetConnection *c)
2953 if (GNUNET_NO == c->destroy)
2960 * Get the connection state.
2962 * @param c Connection to get the state from.
2964 * @return state of the connection.
2966 enum CadetConnectionState
2967 GCC_get_state (const struct CadetConnection *c)
2973 * Get the connection tunnel.
2975 * @param c Connection to get the tunnel from.
2977 * @return tunnel of the connection.
2979 struct CadetTunnel *
2980 GCC_get_tunnel (const struct CadetConnection *c)
2987 * Get free buffer space in a connection.
2989 * @param c Connection.
2990 * @param fwd Is query about FWD traffic?
2992 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2995 GCC_get_buffer (struct CadetConnection *c, int fwd)
2997 struct CadetFlowControl *fc;
2999 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3001 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
3002 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
3003 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
3005 return (fc->queue_max - fc->queue_n);
3010 * Get how many messages have we allowed to send to us from a direction.
3012 * @param c Connection.
3013 * @param fwd Are we asking about traffic from FWD (BCK messages)?
3015 * @return last_ack_sent - last_pid_recv
3018 GCC_get_allowed (struct CadetConnection *c, int fwd)
3020 struct CadetFlowControl *fc;
3022 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3023 if (CADET_CONNECTION_READY != c->state
3024 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
3028 return (fc->last_ack_sent - fc->last_pid_recv);
3033 * Get messages queued in a connection.
3035 * @param c Connection.
3036 * @param fwd Is query about FWD traffic?
3038 * @return Number of messages queued.
3041 GCC_get_qn (struct CadetConnection *c, int fwd)
3043 struct CadetFlowControl *fc;
3045 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3052 * Get next PID to use.
3054 * @param c Connection.
3055 * @param fwd Is query about FWD traffic?
3057 * @return Next PID to use.
3060 GCC_get_pid (struct CadetConnection *c, int fwd)
3062 struct CadetFlowControl *fc;
3065 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3073 * Allow the connection to advertise a buffer of the given size.
3075 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3076 * allowing up to last_pid_recv + buffer.
3078 * @param c Connection.
3079 * @param buffer How many more messages the connection can accept.
3080 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3083 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3085 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3086 GCC_2s (c), buffer, GC_f2s (fwd));
3087 send_ack (c, buffer, fwd, GNUNET_NO);
3092 * Notify other peers on a connection of a broken link. Mark connections
3093 * to destroy after all traffic has been sent.
3095 * @param c Connection on which there has been a disconnection.
3096 * @param peer Peer that disconnected.
3099 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
3101 struct CadetFlowControl *fc;
3105 GCC_check_connections ();
3106 strncpy (peer_name, GCP_2s (peer), 16);
3107 peer_name[15] = '\0';
3108 LOG (GNUNET_ERROR_TYPE_DEBUG,
3109 "shutting down %s, %s disconnected\n",
3110 GCC_2s (c), peer_name);
3112 invalidate_paths (c, peer);
3114 fwd = is_fwd (c, peer);
3115 if (GNUNET_SYSERR == fwd)
3120 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3121 (GNUNET_NO != c->destroy) )
3123 /* Local shutdown, or other peer already down (hence 'c->destroy');
3124 so there is no one to notify about this, just clean up. */
3126 GCC_check_connections ();
3129 /* Mark FlowControl towards the peer as unavaliable. */
3130 fc = fwd ? &c->bck_fc : &c->fwd_fc;
3133 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3135 /* Connection will have at least one pending message
3136 * (the one we just scheduled), so delay destruction
3137 * and remove from map so we don't use accidentally. */
3139 GNUNET_assert (GNUNET_NO == c->was_removed);
3140 c->was_removed = GNUNET_YES;
3141 GNUNET_break (GNUNET_YES ==
3142 GNUNET_CONTAINER_multihashmap_remove (connections,
3145 /* Cancel queue in the direction that just died. */
3146 connection_cancel_queues (c, ! fwd);
3147 GCC_stop_poll (c, ! fwd);
3148 unregister_neighbors (c);
3149 GCC_check_connections ();
3154 * Is this peer the first one on the connection?
3156 * @param c Connection.
3157 * @param fwd Is this about fwd traffic?
3159 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3162 GCC_is_origin (struct CadetConnection *c, int fwd)
3164 if (!fwd && c->path->length - 1 == c->own_pos )
3166 if (fwd && 0 == c->own_pos)
3173 * Is this peer the last one on the connection?
3175 * @param c Connection.
3176 * @param fwd Is this about fwd traffic?
3177 * Note that the ROOT is the terminal for BCK traffic!
3179 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3182 GCC_is_terminal (struct CadetConnection *c, int fwd)
3184 return GCC_is_origin (c, ! fwd);
3189 * See if we are allowed to send by the next hop in the given direction.
3191 * @param c Connection.
3192 * @param fwd Is this about fwd traffic?
3194 * @return #GNUNET_YES in case it's OK to send.
3197 GCC_is_sendable (struct CadetConnection *c, int fwd)
3199 struct CadetFlowControl *fc;
3201 LOG (GNUNET_ERROR_TYPE_DEBUG,
3202 " checking sendability of %s traffic on %s\n",
3203 GC_f2s (fwd), GCC_2s (c));
3209 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3210 LOG (GNUNET_ERROR_TYPE_DEBUG,
3211 " last ack recv: %u, last pid sent: %u\n",
3212 fc->last_ack_recv, fc->last_pid_sent);
3213 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3215 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3218 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3224 * Check if this connection is a direct one (never trim a direct connection).
3226 * @param c Connection.
3228 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3231 GCC_is_direct (struct CadetConnection *c)
3233 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3238 * Internal implementation of the send function.
3240 * Sends an completely built message on a connection, properly registering
3241 * all used resources.
3243 * @param message Message to send.
3244 * @param payload_type Type of payload, in case the message is encrypted.
3245 * @param payload_id ID of the payload (PID, ACK, ...).
3246 * @param c Connection on which this message is transmitted.
3247 * @param fwd Is this a fwd message?
3248 * @param force Force the connection to accept the message (buffer overfill).
3249 * @param cont Continuation called once message is sent. Can be NULL.
3250 * @param cont_cls Closure for @c cont.
3252 * @return Handle to cancel the message before it's sent.
3253 * NULL on error or if @c cont is NULL.
3254 * Invalid on @c cont call.
3256 static struct CadetConnectionQueue *
3257 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3258 uint16_t payload_type, uint32_t payload_id,
3259 struct CadetConnection *c, int fwd, int force,
3260 GCC_sent cont, void *cont_cls)
3262 struct CadetFlowControl *fc;
3263 struct CadetConnectionQueue *q;
3267 GCC_check_connections ();
3268 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3269 if (0 == fc->queue_max)
3275 size = ntohs (message->size);
3276 type = ntohs (message->type);
3277 LOG (GNUNET_ERROR_TYPE_INFO,
3278 "--> %s (%s %4u) on conn %s (%p) %s [%5u]\n",
3279 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), c,
3283 case GNUNET_MESSAGE_TYPE_CADET_AX:
3284 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3285 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3286 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3287 if (GNUNET_NO == force)
3293 case GNUNET_MESSAGE_TYPE_CADET_KX:
3296 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3297 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3298 GNUNET_break (0); /* Should've used specific functions. */
3301 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3302 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3303 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3304 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3305 GNUNET_assert (GNUNET_YES == force);
3313 if (fc->queue_n > fc->queue_max && GNUNET_NO == force)
3315 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3318 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3319 fc->queue_n, fc->queue_max);
3320 if (GNUNET_MESSAGE_TYPE_CADET_AX == type)
3324 return NULL; /* Drop this message */
3327 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3328 GCC_2s (c), c->pending_messages);
3329 c->pending_messages++;
3331 q = GNUNET_new (struct CadetConnectionQueue);
3333 q->peer_q = GCP_send (get_hop (c, fwd), message,
3334 payload_type, payload_id,
3336 &conn_message_sent, q);
3337 if (NULL == q->peer_q)
3339 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3341 GCC_check_connections ();
3345 q->cont_cls = cont_cls;
3346 GNUNET_CONTAINER_DLL_insert (fc->q_head, fc->q_tail, q);
3347 GCC_check_connections ();
3348 return (NULL == cont) ? NULL : q;
3353 * Sends an already built message on a connection, properly registering
3354 * all used resources.
3356 * @param message Message to send.
3357 * @param payload_type Type of payload, in case the message is encrypted.
3358 * @param payload_id ID of the payload (PID, ACK, ...).
3359 * @param c Connection on which this message is transmitted.
3360 * @param fwd Is this a fwd message?
3361 * @param force Force the connection to accept the message (buffer overfill).
3362 * @param cont Continuation called once message is sent. Can be NULL.
3363 * @param cont_cls Closure for @c cont.
3365 * @return Handle to cancel the message before it's sent.
3366 * NULL on error or if @c cont is NULL.
3367 * Invalid on @c cont call.
3369 struct CadetConnectionQueue *
3370 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3371 uint16_t payload_type, uint32_t payload_id,
3372 struct CadetConnection *c, int fwd, int force,
3373 GCC_sent cont, void *cont_cls)
3378 size = ntohs (message->size);
3379 type = ntohs (message->type);
3381 /* Allocate a copy of the message on the stack, so we can modify it as needed,
3382 * adding the Connection ID, PID, and other data the Tunnel layer doesn't
3385 if (GNUNET_MESSAGE_TYPE_CADET_AX == type
3386 || GNUNET_MESSAGE_TYPE_CADET_KX == type)
3388 struct GNUNET_MessageHeader *copy;
3389 unsigned char cbuf[size];
3391 copy = (struct GNUNET_MessageHeader *) cbuf;
3392 GNUNET_memcpy (copy, message, size);
3393 if (GNUNET_MESSAGE_TYPE_CADET_AX == type)
3395 struct GNUNET_CADET_AX *axmsg;
3397 axmsg = (struct GNUNET_CADET_AX *) copy;
3399 axmsg->pid = htonl (GCC_get_pid (c, fwd));
3401 else /* case GNUNET_MESSAGE_TYPE_CADET_KX */
3403 struct GNUNET_CADET_KX *kmsg;
3405 GNUNET_assert (GNUNET_MESSAGE_TYPE_CADET_KX == type);
3406 kmsg = (struct GNUNET_CADET_KX *) copy;
3407 kmsg->reserved = htonl (0);
3410 return send_prebuilt_message (copy, payload_type, payload_id,
3414 return send_prebuilt_message (message, payload_type, payload_id,
3422 * Cancel a previously sent message while it's in the queue.
3424 * ONLY can be called before the continuation given to the send function
3425 * is called. Once the continuation is called, the message is no longer in the
3428 * @param q Handle to the queue.
3431 GCC_cancel (struct CadetConnectionQueue *q)
3433 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3435 /* send_cancel calls message_sent, which calls q->cont and frees q */
3436 GCP_send_cancel (q->peer_q);
3437 GCC_check_connections ();
3442 * Sends a CREATE CONNECTION message for a path to a peer.
3443 * Changes the connection and tunnel states if necessary.
3445 * @param c Connection to create.
3448 GCC_send_create (struct CadetConnection *c)
3450 enum CadetTunnelCState state;
3453 GCC_check_connections ();
3454 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3455 size += c->path->length * sizeof (struct GNUNET_PeerIdentity);
3457 /* Allocate message on the stack */
3458 unsigned char cbuf[size];
3459 struct GNUNET_CADET_ConnectionCreate *msg;
3460 struct GNUNET_PeerIdentity *peers;
3462 msg = (struct GNUNET_CADET_ConnectionCreate *) cbuf;
3463 msg->header.size = htons (size);
3464 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
3465 msg->reserved = htonl (0);
3466 msg->cid = *GCC_get_id (c);
3467 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3468 for (int i = 0; i < c->path->length; i++)
3470 GNUNET_PEER_resolve (c->path->peers[i], peers++);
3472 GNUNET_assert (NULL == c->maintenance_q);
3473 c->maintenance_q = GCP_send (get_next_hop (c),
3475 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0,
3477 &conn_message_sent, NULL);
3480 LOG (GNUNET_ERROR_TYPE_INFO, "==> %s %19s on conn %s (%p) FWD [%5u]\n",
3481 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE), "",
3482 GCC_2s (c), c, size);
3483 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3484 c, c->pending_messages);
3485 c->pending_messages++;
3487 state = GCT_get_cstate (c->t);
3488 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3489 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
3490 if (CADET_CONNECTION_NEW == c->state)
3491 connection_change_state (c, CADET_CONNECTION_SENT);
3492 GCC_check_connections ();
3497 * Send an ACK on the appropriate connection/channel, depending on
3498 * the direction and the position of the peer.
3500 * @param c Which connection to send the hop-by-hop ACK.
3501 * @param fwd Is this a fwd ACK? (will go dest->root).
3502 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
3505 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
3507 unsigned int buffer;
3509 GCC_check_connections ();
3510 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
3511 GC_f2s (fwd), GCC_2s (c));
3519 if (GNUNET_NO != c->destroy)
3521 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
3522 GCC_check_connections ();
3526 /* Get available buffer space */
3527 if (GCC_is_terminal (c, fwd))
3529 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
3530 buffer = GCT_get_channels_buffer (c->t);
3534 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
3535 buffer = GCC_get_buffer (c, fwd);
3537 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
3538 if (0 == buffer && GNUNET_NO == force)
3540 GCC_check_connections ();
3544 /* Send available buffer space */
3545 if (GNUNET_YES == GCC_is_origin (c, fwd))
3547 GNUNET_assert (NULL != c->t);
3548 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
3549 GCT_unchoke_channels (c->t);
3553 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
3554 send_ack (c, buffer, fwd, force);
3556 GCC_check_connections ();
3561 * Send a message to all peers in this connection that the connection
3562 * is no longer valid.
3564 * If some peer should not receive the message, it should be zero'ed out
3565 * before calling this function.
3567 * @param c The connection whose peers to notify.
3570 GCC_send_destroy (struct CadetConnection *c)
3572 struct GNUNET_CADET_ConnectionDestroy msg;
3574 if (GNUNET_YES == c->destroy)
3576 GCC_check_connections ();
3577 msg.header.size = htons (sizeof (msg));
3578 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3580 msg.reserved = htonl (0);
3581 LOG (GNUNET_ERROR_TYPE_DEBUG,
3582 " sending connection destroy for connection %s\n",
3585 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3586 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, UINT16_MAX,
3588 GNUNET_YES, NULL, NULL));
3589 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3590 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, UINT16_MAX,
3592 GNUNET_YES, NULL, NULL));
3594 GCC_check_connections ();
3599 * @brief Start a polling timer for the connection.
3601 * When a neighbor does not accept more traffic on the connection it could be
3602 * caused by a simple congestion or by a lost ACK. Polling enables to check
3603 * for the lastest ACK status for a connection.
3605 * @param c Connection.
3606 * @param fwd Should we poll in the FWD direction?
3609 GCC_start_poll (struct CadetConnection *c, int fwd)
3611 struct CadetFlowControl *fc;
3613 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3614 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3616 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3618 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL already in progress (t: %p, m: %p)\n",
3619 fc->poll_task, fc->poll_msg);
3622 if (0 == fc->queue_max)
3624 /* Should not be needed, traffic should've been cancelled. */
3626 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not possible, peer disconnected\n");
3629 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3630 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3631 &send_connection_poll,
3637 * @brief Stop polling a connection for ACKs.
3639 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3641 * @param c Connection.
3642 * @param fwd Should we stop the poll in the FWD direction?
3645 GCC_stop_poll (struct CadetConnection *c, int fwd)
3647 struct CadetFlowControl *fc;
3649 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3650 if (NULL != fc->poll_task)
3652 GNUNET_SCHEDULER_cancel (fc->poll_task);
3653 fc->poll_task = NULL;
3655 if (NULL != fc->poll_msg)
3657 GCC_cancel (fc->poll_msg);
3658 fc->poll_msg = NULL;
3664 * Get a (static) string for a connection.
3666 * @param c Connection.
3669 GCC_2s (const struct CadetConnection *c)
3676 static char buf[128];
3678 SPRINTF (buf, "%s (->%s)",
3679 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3682 return GNUNET_h2s (GC_h2hc (&c->id));
3687 * Log all possible info about the connection state.
3689 * @param c Connection to debug.
3690 * @param level Debug level to use.
3693 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3698 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3700 __FILE__, __FUNCTION__, __LINE__);
3706 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3710 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3711 s = path_2s (c->path);
3712 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3714 LOG2 (level, "CCC state: %s, destroy: %u\n",
3715 GCC_state2s (c->state), c->destroy);
3716 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3717 if (NULL != c->perf)
3718 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3720 LOG2 (level, "CCC FWD flow control:\n");
3721 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3722 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3723 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3724 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3725 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3726 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3727 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3728 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3730 LOG2 (level, "CCC BCK flow control:\n");
3731 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3732 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3733 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3734 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3735 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3736 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3737 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3738 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3740 LOG2 (level, "CCC DEBUG CONNECTION END\n");