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 GCC_check_connections ();
564 GNUNET_assert (GNUNET_NO == GCC_is_origin (c, fwd));
566 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
567 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
570 GC_f2s (fwd), GCC_2s (c));
572 /* Check if we need to transmit the ACK. */
573 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
574 if (3 < delta && buffer < delta && GNUNET_NO == force)
576 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 " last pid recv: %u, last ack sent: %u\n",
579 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
580 GCC_check_connections ();
584 /* Ok, ACK might be necessary, what PID to ACK? */
585 ack = prev_fc->last_pid_recv + buffer;
586 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
587 LOG (GNUNET_ERROR_TYPE_DEBUG,
588 " last pid %u, last ack %u, qmax %u, q %u\n",
589 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
590 next_fc->queue_max, next_fc->queue_n);
591 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
593 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
594 GCC_check_connections ();
598 /* Check if message is already in queue */
599 if (NULL != prev_fc->ack_msg)
601 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
603 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
604 GCC_cancel (prev_fc->ack_msg);
605 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
609 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
610 GCC_check_connections ();
615 prev_fc->last_ack_sent = ack;
617 /* Build ACK message and send on conn */
618 msg.header.size = htons (sizeof (msg));
619 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
620 msg.ack = htonl (ack);
623 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, UINT16_MAX, ack,
626 GNUNET_assert (NULL != prev_fc->ack_msg);
627 GCC_check_connections ();
632 * Update performance information if we are a connection's endpoint.
634 * @param c Connection to update.
635 * @param wait How much time did we wait to send the last message.
636 * @param size Size of the last message.
639 update_perf (struct CadetConnection *c,
640 struct GNUNET_TIME_Relative wait,
643 struct CadetConnectionPerformance *p;
647 return; /* Only endpoints are interested in timing. */
650 usecsperbyte = ((double) wait.rel_value_us) / size;
651 if (p->size == AVG_MSGS)
653 /* Array is full. Substract oldest value, add new one and store. */
654 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
655 p->usecsperbyte[p->idx] = usecsperbyte;
656 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
660 /* Array not yet full. Add current value to avg and store. */
661 p->usecsperbyte[p->idx] = usecsperbyte;
663 p->avg += p->usecsperbyte[p->idx];
667 p->idx = (p->idx + 1) % AVG_MSGS;
672 * Callback called when a connection queued message is sent.
674 * Calculates the average time and connection packet tracking.
676 * @param cls Closure (ConnectionQueue Handle), can be NULL.
677 * @param c Connection this message was on.
678 * @param fwd Was this a FWD going message?
679 * @param sent Was it really sent? (Could have been canceled)
680 * @param type Type of message sent.
681 * @param payload_type Type of payload, if applicable.
682 * @param pid Message ID, or 0 if not applicable (create, destroy, etc).
683 * @param size Size of the message.
684 * @param wait Time spent waiting for core (only the time for THIS message)
687 conn_message_sent (void *cls,
688 struct CadetConnection *c, int fwd, int sent,
689 uint16_t type, uint16_t payload_type, uint32_t pid,
691 struct GNUNET_TIME_Relative wait)
693 struct CadetConnectionQueue *q = cls;
694 struct CadetFlowControl *fc;
697 GCC_check_connections ();
698 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
700 /* If c is NULL, nothing to update. */
703 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
704 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
706 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
709 GCC_check_connections ();
713 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
714 sent ? "" : "not ", GC_f2s (fwd),
715 GC_m2s (type), GC_m2s (payload_type), pid);
716 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
718 /* Update flow control info. */
719 fc = fwd ? &c->fwd_fc : &c->bck_fc;
723 GNUNET_CONTAINER_DLL_remove (fc->q_head, fc->q_tail, q);
727 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
728 q->cont (q->cont_cls, c, q, type, fwd, size);
732 else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED)
734 /* SHOULD NO LONGER HAPPEN FIXME: REMOVE CASE */
735 // If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt
737 GNUNET_assert (0); // FIXME
739 else /* CONN_CREATE or CONN_ACK */
744 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
745 c->pending_messages--;
746 if ( (GNUNET_YES == c->destroy) &&
747 (0 == c->pending_messages) )
749 LOG (GNUNET_ERROR_TYPE_DEBUG,
750 "! destroying connection!\n");
752 GCC_check_connections ();
756 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
759 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
760 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
761 c->maintenance_q = NULL;
762 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
763 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
764 schedule_next_keepalive (c, fwd);
767 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
768 if (GNUNET_YES == sent)
770 GNUNET_assert (NULL != q);
771 fc->last_pid_sent = pid;
772 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
773 GCC_start_poll (c, fwd);
774 GCC_send_ack (c, fwd, GNUNET_NO);
775 connection_reset_timeout (c, fwd);
778 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
779 if (GNUNET_NO == forced)
782 LOG (GNUNET_ERROR_TYPE_DEBUG,
783 "! accounting pid %u\n",
788 LOG (GNUNET_ERROR_TYPE_DEBUG,
789 "! forced, Q_N not accounting pid %u\n",
794 case GNUNET_MESSAGE_TYPE_CADET_KX:
795 if (GNUNET_YES == sent)
796 connection_reset_timeout (c, fwd);
799 case GNUNET_MESSAGE_TYPE_CADET_POLL:
803 case GNUNET_MESSAGE_TYPE_CADET_ACK:
807 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
808 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
812 LOG (GNUNET_ERROR_TYPE_ERROR, "%s unknown\n", GC_m2s (type));
816 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
818 update_perf (c, wait, size);
819 GCC_check_connections ();
824 * Get the previous hop in a connection
826 * @param c Connection.
828 * @return Previous peer in the connection.
830 static struct CadetPeer *
831 get_prev_hop (const struct CadetConnection *c)
837 LOG (GNUNET_ERROR_TYPE_DEBUG,
838 " get prev hop %s [%u/%u]\n",
839 GCC_2s (c), c->own_pos, c->path->length);
840 if (0 == c->own_pos || c->path->length < 2)
841 id = c->path->peers[0];
843 id = c->path->peers[c->own_pos - 1];
845 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
846 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
848 return GCP_get_short (id, GNUNET_YES);
853 * Get the next hop in a connection
855 * @param c Connection.
857 * @return Next peer in the connection.
859 static struct CadetPeer *
860 get_next_hop (const struct CadetConnection *c)
867 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
868 GCC_2s (c), c->own_pos, c->path->length);
869 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
870 id = c->path->peers[c->path->length - 1];
872 id = c->path->peers[c->own_pos + 1];
874 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
875 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
877 return GCP_get_short (id, GNUNET_YES);
882 * Check that the direct neighbours (previous and next hop)
883 * are properly associated with this connection.
885 * @param c connection to check
888 check_neighbours (const struct CadetConnection *c)
891 return; /* nothing to check */
892 GCP_check_connection (get_next_hop (c), c);
893 GCP_check_connection (get_prev_hop (c), c);
898 * Helper for #GCC_check_connections(). Calls #check_neighbours().
902 * @param value the `struct CadetConnection` to check
903 * @return #GNUNET_OK (continue to iterate)
906 check_connection (void *cls,
907 const struct GNUNET_HashCode *key,
910 struct CadetConnection *c = value;
912 check_neighbours (c);
918 * Check invariants for all connections using #check_neighbours().
921 GCC_check_connections ()
923 if (0 == CHECK_INVARIANTS)
925 if (NULL == connections)
927 GNUNET_CONTAINER_multihashmap_iterate (connections,
934 * Get the hop in a connection.
936 * @param c Connection.
937 * @param fwd Next in the FWD direction?
939 * @return Next peer in the connection.
941 static struct CadetPeer *
942 get_hop (struct CadetConnection *c, int fwd)
944 return (fwd) ? get_next_hop (c) : get_prev_hop (c);
949 * Get a bit mask for a message received out-of-order.
951 * @param last_pid_recv Last PID we received prior to the out-of-order.
952 * @param ooo_pid PID of the out-of-order message.
955 get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
957 return 1 << (last_pid_recv - ooo_pid);
962 * Check is an out-of-order message is ok:
963 * - at most 31 messages behind.
966 * @param last_pid_recv Last in-order PID received.
969 is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
973 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
976 mask = get_recv_bitmask (last_pid_recv, ooo_pid);
977 if (0 != (ooo_bitmap & mask))
985 * Is traffic coming from this sender 'FWD' traffic?
987 * @param c Connection to check.
988 * @param sender Short peer identity of neighbor.
990 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
991 * the traffic is 'FWD'.
992 * #GNUNET_NO for BCK.
993 * #GNUNET_SYSERR for errors (sender isn't a hop in the connection).
996 is_fwd (const struct CadetConnection *c,
997 const struct CadetPeer *sender)
1001 id = GCP_get_short_id (sender);
1002 if (GCP_get_short_id (get_prev_hop (c)) == id)
1005 if (GCP_get_short_id (get_next_hop (c)) == id)
1008 return GNUNET_SYSERR;
1013 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1014 * or a first CONNECTION_ACK directed to us.
1016 * @param c Connection to confirm.
1017 * @param fwd Should we send it FWD? (root->dest)
1018 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
1021 send_connection_ack (struct CadetConnection *c, int fwd)
1023 struct GNUNET_CADET_ConnectionACK msg;
1024 struct CadetTunnel *t;
1025 const uint16_t size = sizeof (struct GNUNET_CADET_ConnectionACK);
1026 const uint16_t type = GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK;
1028 GCC_check_connections ();
1030 LOG (GNUNET_ERROR_TYPE_INFO,
1031 "==> %s ({ C %s ACK} 0) on conn %s (%p) %s [%5u]\n",
1032 GC_m2s (type), GC_f2s (!fwd), GCC_2s (c), c, GC_f2s (fwd), size);
1034 msg.header.size = htons (size);
1035 msg.header.type = htons (type);
1036 msg.reserved = htonl (0);
1039 GNUNET_assert (NULL == c->maintenance_q);
1040 c->maintenance_q = GCP_send (get_hop (c, fwd), &msg.header,
1041 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0,
1043 &conn_message_sent, NULL);
1044 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (conn`ACK)\n",
1045 c, c->pending_messages);
1046 c->pending_messages++;
1048 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
1049 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
1050 if (CADET_CONNECTION_READY != c->state)
1051 connection_change_state (c, CADET_CONNECTION_SENT);
1052 GCC_check_connections ();
1057 * Send a notification that a connection is broken.
1059 * @param c Connection that is broken.
1060 * @param id1 Peer that has disconnected.
1061 * @param id2 Peer that has disconnected.
1062 * @param fwd Direction towards which to send it.
1065 send_broken (struct CadetConnection *c,
1066 const struct GNUNET_PeerIdentity *id1,
1067 const struct GNUNET_PeerIdentity *id2,
1070 struct GNUNET_CADET_ConnectionBroken msg;
1072 GCC_check_connections ();
1073 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1074 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1076 msg.reserved = htonl (0);
1079 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c, fwd,
1080 GNUNET_YES, NULL, NULL);
1081 GCC_check_connections ();
1086 * Send a notification that a connection is broken, when a connection
1087 * isn't even known to the local peer or soon to be destroyed.
1089 * @param connection_id Connection ID.
1090 * @param id1 Peer that has disconnected, probably local peer.
1091 * @param id2 Peer that has disconnected can be NULL if unknown.
1092 * @param neighbor Peer to notify (neighbor who sent the connection).
1095 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
1096 const struct GNUNET_PeerIdentity *id1,
1097 const struct GNUNET_PeerIdentity *id2,
1098 struct CadetPeer *neighbor)
1100 struct GNUNET_CADET_ConnectionBroken msg;
1102 GCC_check_connections ();
1103 LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n",
1104 GNUNET_h2s (GC_h2hc (connection_id)));
1106 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1107 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1108 msg.cid = *connection_id;
1109 msg.reserved = htonl (0);
1114 memset (&msg.peer2, 0, sizeof (msg.peer2));
1115 GNUNET_assert (NULL != GCP_send (neighbor, &msg.header,
1117 NULL, GNUNET_SYSERR, /* connection, fwd */
1118 NULL, NULL)); /* continuation */
1119 GCC_check_connections ();
1124 * Send keepalive packets for a connection.
1126 * @param c Connection to keep alive..
1127 * @param fwd Is this a FWD keepalive? (owner -> dest).
1130 send_connection_keepalive (struct CadetConnection *c, int fwd)
1132 struct GNUNET_MessageHeader msg;
1133 struct CadetFlowControl *fc;
1136 GCC_check_connections ();
1137 LOG (GNUNET_ERROR_TYPE_INFO,
1138 "keepalive %s for connection %s\n",
1139 GC_f2s (fwd), GCC_2s (c));
1141 GNUNET_assert (NULL != c->t);
1142 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1143 tunnel_ready = GNUNET_YES == GCT_has_queued_traffic (c->t)
1144 && CADET_TUNNEL_KEY_OK <= GCT_get_estate (c->t);
1145 if (0 < fc->queue_n || tunnel_ready)
1147 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1151 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1153 GNUNET_assert (NULL != c->t);
1154 msg.size = htons (sizeof (msg));
1155 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
1157 GNUNET_assert (NULL ==
1158 GCT_send_prebuilt_message (&msg, c->t, c,
1159 GNUNET_NO, NULL, NULL));
1160 GCC_check_connections ();
1165 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1167 * @param c Connection for which to send the message.
1168 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1171 connection_recreate (struct CadetConnection *c, int fwd)
1173 LOG (GNUNET_ERROR_TYPE_DEBUG,
1174 "sending connection recreate\n");
1176 GCC_send_create (c);
1178 send_connection_ack (c, GNUNET_NO);
1183 * Generic connection timer management.
1184 * Depending on the role of the peer in the connection will send the
1185 * appropriate message (build or keepalive)
1187 * @param c Conncetion to maintain.
1188 * @param fwd Is FWD?
1191 connection_maintain (struct CadetConnection *c, int fwd)
1193 if (GNUNET_NO != c->destroy)
1195 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1202 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1206 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1208 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1210 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1211 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1212 schedule_next_keepalive (c, fwd);
1217 case CADET_CONNECTION_NEW:
1220 case CADET_CONNECTION_SENT:
1221 connection_recreate (c, fwd);
1223 case CADET_CONNECTION_READY:
1224 send_connection_keepalive (c, fwd);
1233 * Keep the connection alive.
1235 * @param c Connection to keep alive.
1236 * @param fwd Direction.
1239 connection_keepalive (struct CadetConnection *c,
1242 GCC_check_connections ();
1243 LOG (GNUNET_ERROR_TYPE_DEBUG,
1244 "%s keepalive for %s\n",
1245 GC_f2s (fwd), GCC_2s (c));
1248 c->fwd_maintenance_task = NULL;
1250 c->bck_maintenance_task = NULL;
1251 connection_maintain (c, fwd);
1252 GCC_check_connections ();
1253 /* Next execution will be scheduled by message_sent or _maintain*/
1258 * Keep the connection alive in the FWD direction.
1260 * @param cls Closure (connection to keepalive).
1263 connection_fwd_keepalive (void *cls)
1265 struct CadetConnection *c = cls;
1267 GCC_check_connections ();
1268 connection_keepalive (c,
1270 GCC_check_connections ();
1275 * Keep the connection alive in the BCK direction.
1277 * @param cls Closure (connection to keepalive).
1280 connection_bck_keepalive (void *cls)
1282 struct CadetConnection *c = cls;
1284 GCC_check_connections ();
1285 connection_keepalive (c,
1287 GCC_check_connections ();
1292 * Schedule next keepalive task, taking in consideration
1293 * the connection state and number of retries.
1295 * If the peer is not the origin, do nothing.
1297 * @param c Connection for which to schedule the next keepalive.
1298 * @param fwd Direction for the next keepalive.
1301 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1303 struct GNUNET_TIME_Relative delay;
1304 struct GNUNET_SCHEDULER_Task * *task_id;
1305 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1307 GCC_check_connections ();
1308 if (GNUNET_NO == GCC_is_origin (c, fwd))
1311 /* Calculate delay to use, depending on the state of the connection */
1312 if (CADET_CONNECTION_READY == c->state)
1314 delay = refresh_connection_time;
1318 if (1 > c->create_retry)
1319 c->create_retry = 1;
1320 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1322 if (c->create_retry < 64) // TODO make configurable
1323 c->create_retry *= 2;
1326 /* Select direction-dependent parameters */
1327 if (GNUNET_YES == fwd)
1329 task_id = &c->fwd_maintenance_task;
1330 keepalive_task = &connection_fwd_keepalive;
1334 task_id = &c->bck_maintenance_task;
1335 keepalive_task = &connection_bck_keepalive;
1338 /* Check that no one scheduled it before us */
1339 if (NULL != *task_id)
1341 /* No need for a _break. It can happen for instance when sending a SYNACK
1342 * for a duplicate SYN: the first SYNACK scheduled the task. */
1343 GNUNET_SCHEDULER_cancel (*task_id);
1346 /* Schedule the task */
1347 *task_id = GNUNET_SCHEDULER_add_delayed (delay,
1350 LOG (GNUNET_ERROR_TYPE_DEBUG,
1351 "next keepalive in %s\n",
1352 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1353 GCC_check_connections ();
1358 * Cancel all transmissions that belong to a certain connection.
1360 * If the connection is scheduled for destruction and no more messages are left,
1361 * the connection will be destroyed by the continuation call.
1363 * @param c Connection which to cancel. Might be destroyed during this call.
1364 * @param fwd Cancel fwd traffic?
1367 connection_cancel_queues (struct CadetConnection *c,
1370 struct CadetFlowControl *fc;
1372 GCC_check_connections ();
1373 LOG (GNUNET_ERROR_TYPE_DEBUG,
1374 "Cancel %s queues for connection %s\n",
1375 GC_f2s (fwd), GCC_2s (c));
1382 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1383 if (NULL != fc->poll_task)
1385 GNUNET_SCHEDULER_cancel (fc->poll_task);
1386 fc->poll_task = NULL;
1387 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL task for fc %p\n", fc);
1389 if (NULL != fc->poll_msg)
1391 GCC_cancel (fc->poll_msg);
1392 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL msg for fc %p\n", fc);
1395 while (NULL != fc->q_head)
1397 GCC_cancel (fc->q_head);
1399 GCC_check_connections ();
1404 * Function called if a connection has been stalled for a while,
1405 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1407 * @param cls Closure (poll ctx).
1410 send_connection_poll (void *cls);
1414 * Callback called when a queued POLL message is sent.
1416 * @param cls Closure (flow control context).
1417 * @param c Connection this message was on.
1418 * @param q Queue handler this call invalidates.
1419 * @param type Type of message sent.
1420 * @param fwd Was this a FWD going message?
1421 * @param size Size of the message.
1424 poll_sent (void *cls,
1425 struct CadetConnection *c,
1426 struct CadetConnectionQueue *q,
1427 uint16_t type, int fwd, size_t size)
1429 struct CadetFlowControl *fc = cls;
1431 GNUNET_assert (fc->poll_msg == q);
1432 fc->poll_msg = NULL;
1433 if (2 == c->destroy)
1435 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1438 if (0 == fc->queue_max)
1440 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL cancelled: neighbor disconnected\n");
1443 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1445 GNUNET_assert (NULL == fc->poll_task);
1446 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1447 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1448 &send_connection_poll,
1450 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1455 * Function called if a connection has been stalled for a while,
1456 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1458 * @param cls Closure (poll ctx).
1461 send_connection_poll (void *cls)
1463 struct CadetFlowControl *fc = cls;
1464 struct GNUNET_CADET_Poll msg;
1465 struct CadetConnection *c;
1468 fc->poll_task = NULL;
1469 GCC_check_connections ();
1471 fwd = fc == &c->fwd_fc;
1472 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1473 GCC_2s (c), GC_f2s (fwd));
1475 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1476 msg.header.size = htons (sizeof (msg));
1478 msg.pid = htonl (fc->last_pid_sent);
1479 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1481 GCC_send_prebuilt_message (&msg.header, UINT16_MAX, fc->last_pid_sent, c,
1482 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1483 GNUNET_assert (NULL != fc->poll_msg);
1484 GCC_check_connections ();
1489 * Generic connection timeout implementation.
1491 * Timeout function due to lack of keepalive/traffic from an endpoint.
1492 * Destroys connection if called.
1494 * @param c Connection to destroy.
1495 * @param fwd Was the timeout from the origin? (FWD timeout)
1498 connection_timeout (struct CadetConnection *c, int fwd)
1500 GCC_check_connections ();
1502 LOG (GNUNET_ERROR_TYPE_INFO,
1503 "Connection %s %s timed out. Destroying.\n",
1506 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1508 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1514 /* If dest, send "broken" notification. */
1515 if (GCC_is_terminal (c, fwd))
1517 struct CadetPeer *next_hop;
1519 next_hop = fwd ? get_prev_hop (c) : get_next_hop (c);
1520 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1524 GCC_check_connections ();
1529 * Timeout function due to lack of keepalive/traffic from the owner.
1530 * Destroys connection if called.
1532 * @param cls Closure (connection to destroy).
1535 connection_fwd_timeout (void *cls)
1537 struct CadetConnection *c = cls;
1539 c->fwd_maintenance_task = NULL;
1540 GCC_check_connections ();
1541 connection_timeout (c, GNUNET_YES);
1542 GCC_check_connections ();
1547 * Timeout function due to lack of keepalive/traffic from the destination.
1548 * Destroys connection if called.
1550 * @param cls Closure (connection to destroy).
1553 connection_bck_timeout (void *cls)
1555 struct CadetConnection *c = cls;
1557 c->bck_maintenance_task = NULL;
1558 GCC_check_connections ();
1559 connection_timeout (c, GNUNET_NO);
1560 GCC_check_connections ();
1565 * Resets the connection timeout task, some other message has done the
1567 * - For the first peer on the direction this means to send
1568 * a keepalive or a path confirmation message (either create or ACK).
1569 * - For all other peers, this means to destroy the connection,
1570 * due to lack of activity.
1571 * Starts the timeout if no timeout was running (connection just created).
1573 * @param c Connection whose timeout to reset.
1574 * @param fwd Is this forward?
1576 * TODO use heap to improve efficiency of scheduler.
1579 connection_reset_timeout (struct CadetConnection *c, int fwd)
1581 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1582 if (GCC_is_origin (c, fwd)) /* Startpoint */
1584 schedule_next_keepalive (c, fwd);
1585 if (NULL != c->maintenance_q)
1587 GCP_send_cancel (c->maintenance_q);
1588 c->maintenance_q = NULL; /* Is set to NULL by conn_message_sent anyway */
1591 else /* Relay, endpoint. */
1593 struct GNUNET_TIME_Relative delay;
1594 struct GNUNET_SCHEDULER_Task * *ti;
1595 GNUNET_SCHEDULER_TaskCallback f;
1597 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1600 GNUNET_SCHEDULER_cancel (*ti);
1601 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1602 LOG (GNUNET_ERROR_TYPE_DEBUG,
1603 " timing out in %s\n",
1604 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1605 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1606 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1612 * Iterator to compare each connection's path with the path of a new connection.
1614 * If the connection coincides, the c member of path is set to the connection
1615 * and the destroy flag of the connection is set.
1617 * @param cls Closure (new path).
1618 * @param c Connection in the tunnel to check.
1621 check_path (void *cls, struct CadetConnection *c)
1623 struct CadetConnection *new_conn = cls;
1624 struct CadetPeerPath *path = new_conn->path;
1626 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1627 GCC_2s (c), c, c->path->length);
1630 && GNUNET_NO == c->destroy
1631 && CADET_CONNECTION_BROKEN != c->state
1632 && CADET_CONNECTION_DESTROYED != c->state
1633 && path_equivalent (path, c->path))
1635 new_conn->destroy = GNUNET_YES; /* Do not mark_destroyed, */
1636 new_conn->path->c = c; /* this is only a flag for the Iterator. */
1637 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1643 * Finds out if this path is already being used by an existing connection.
1645 * Checks the tunnel towards the destination to see if it contains
1646 * any connection with the same path.
1648 * If the existing connection is ready, it is kept.
1649 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1650 * the peer will eventually reject our attempt).
1652 * @param path Path to check.
1653 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1654 * #GNUNET_NO otherwise.
1657 does_connection_exist (struct CadetConnection *conn)
1659 struct CadetPeer *p;
1660 struct CadetTunnel *t;
1661 struct CadetConnection *c;
1663 p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
1666 t = GCP_get_tunnel (p);
1670 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1672 GCT_iterate_connections (t, &check_path, conn);
1674 if (GNUNET_YES == conn->destroy)
1677 conn->destroy = GNUNET_NO;
1678 conn->path->c = conn;
1679 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1680 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1681 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1682 if (CADET_CONNECTION_READY == c->state)
1684 /* The other peer confirmed a live connection with this path,
1685 * why are they trying to duplicate it? */
1686 GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
1689 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not ready, connection unique\n");
1694 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1701 * @brief Check if the tunnel this connection belongs to has any other
1702 * connection with the same path, and destroy one if so.
1704 * @param cls Closure (connection to check).
1707 check_duplicates (void *cls)
1709 struct CadetConnection *c = cls;
1711 c->check_duplicates_task = NULL;
1712 if (GNUNET_YES == does_connection_exist (c))
1714 GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1715 send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
1722 * Wait for enough time to let any dead connections time out and check for
1723 * any remaining duplicates.
1725 * @param c Connection that is a potential duplicate.
1728 schedule_check_duplicates (struct CadetConnection *c)
1730 struct GNUNET_TIME_Relative delay;
1732 if (NULL != c->check_duplicates_task)
1734 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 5);
1735 c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
1742 * Add the connection to the list of both neighbors.
1744 * @param c Connection.
1746 * @return #GNUNET_OK if everything went fine
1747 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1750 register_neighbors (struct CadetConnection *c)
1752 c->next_peer = get_next_hop (c);
1753 c->prev_peer = get_prev_hop (c);
1754 GNUNET_assert (c->next_peer != c->prev_peer);
1755 LOG (GNUNET_ERROR_TYPE_DEBUG,
1756 "register neighbors for connection %s\n",
1758 path_debug (c->path);
1759 LOG (GNUNET_ERROR_TYPE_DEBUG,
1760 "own pos %u\n", c->own_pos);
1761 LOG (GNUNET_ERROR_TYPE_DEBUG,
1762 "putting connection %s to next peer %p\n",
1765 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1767 GCP_2s (c->next_peer));
1768 LOG (GNUNET_ERROR_TYPE_DEBUG,
1769 "putting connection %s to prev peer %p\n",
1772 LOG (GNUNET_ERROR_TYPE_DEBUG,
1773 "prev peer %p %s\n",
1775 GCP_2s (c->prev_peer));
1777 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1778 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1780 if (GCC_is_origin (c, GNUNET_YES))
1781 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1782 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1784 LOG (GNUNET_ERROR_TYPE_DEBUG,
1785 " register neighbors failed\n");
1786 LOG (GNUNET_ERROR_TYPE_DEBUG,
1787 " prev: %s, neighbor?: %d\n",
1788 GCP_2s (c->prev_peer),
1789 GCP_is_neighbor (c->prev_peer));
1790 LOG (GNUNET_ERROR_TYPE_DEBUG,
1791 " next: %s, neighbor?: %d\n",
1792 GCP_2s (c->next_peer),
1793 GCP_is_neighbor (c->next_peer));
1794 return GNUNET_SYSERR;
1796 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1797 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1804 * Remove the connection from the list of both neighbors.
1806 * @param c Connection.
1809 unregister_neighbors (struct CadetConnection *c)
1811 // struct CadetPeer *peer; FIXME dont use next_peer, prev_peer
1812 /* Either already unregistered or never got registered, it's ok either way. */
1813 if (NULL == c->path)
1815 if (NULL != c->next_peer)
1817 GCP_remove_connection (c->next_peer, c);
1818 c->next_peer = NULL;
1820 if (NULL != c->prev_peer)
1822 GCP_remove_connection (c->prev_peer, c);
1823 c->prev_peer = NULL;
1829 * Invalidates all paths towards all peers that comprise the connection which
1830 * rely on the disconnected peer.
1832 * ~O(n^3) (peers in connection * paths/peer * links/path)
1834 * @param c Connection whose peers' paths to clean.
1835 * @param disconnected Peer that disconnected.
1838 invalidate_paths (struct CadetConnection *c,
1839 struct CadetPeer *disconnected)
1841 struct CadetPeer *peer;
1844 for (i = 0; i < c->path->length; i++)
1846 peer = GCP_get_short (c->path->peers[i], GNUNET_NO);
1848 GCP_notify_broken_link (peer, &my_full_id, GCP_get_id (disconnected));
1854 * Bind the connection to the peer and the tunnel to that peer.
1856 * If the peer has no tunnel, create one. Update tunnel and connection
1857 * data structres to reflect new status.
1859 * @param c Connection.
1863 add_to_peer (struct CadetConnection *c,
1864 struct CadetPeer *peer)
1866 GCP_add_tunnel (peer);
1867 c->t = GCP_get_tunnel (peer);
1868 GCT_add_connection (c->t, c);
1873 * Log receipt of message on stderr (INFO level).
1875 * @param message Message received.
1876 * @param peer Peer who sent the message.
1877 * @param conn_id Connection ID of the message.
1880 log_message (const struct GNUNET_MessageHeader *message,
1881 const struct CadetPeer *peer,
1882 const struct GNUNET_CADET_Hash *conn_id)
1888 size = ntohs (message->size);
1889 type = ntohs (message->type);
1892 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1893 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1894 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1895 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1901 LOG (GNUNET_ERROR_TYPE_INFO, "<%s %s on conn %s from %s, %6u bytes\n",
1902 arrow, GC_m2s (type), GNUNET_h2s (GC_h2hc (conn_id)),
1903 GCP_2s(peer), (unsigned int) size);
1906 /******************************************************************************/
1907 /******************************** API ***********************************/
1908 /******************************************************************************/
1911 * Handler for connection creation.
1913 * @param peer Message sender (neighbor).
1914 * @param msg Message itself.
1917 GCC_handle_create (struct CadetPeer *peer,
1918 const struct GNUNET_CADET_ConnectionCreate *msg)
1920 const struct GNUNET_CADET_Hash *cid;
1921 struct GNUNET_PeerIdentity *id;
1922 struct CadetPeerPath *path;
1923 struct CadetPeer *dest_peer;
1924 struct CadetPeer *orig_peer;
1925 struct CadetConnection *c;
1926 unsigned int own_pos;
1929 GCC_check_connections ();
1930 size = ntohs (msg->header.size);
1932 /* Calculate hops */
1933 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1934 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1936 GNUNET_break_op (0);
1939 size /= sizeof (struct GNUNET_PeerIdentity);
1942 GNUNET_break_op (0);
1945 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1947 /* Get parameters */
1949 log_message (&msg->header, peer, cid);
1950 id = (struct GNUNET_PeerIdentity *) &msg[1];
1951 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1953 /* Create connection */
1954 c = connection_get (cid);
1957 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1958 size, myid, &own_pos);
1961 /* Path was malformed, probably our own ID was not in it. */
1962 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
1963 GNUNET_break_op (0);
1968 /* We received this request from a neighbor, we cannot be origin */
1969 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
1970 GNUNET_break_op (0);
1971 path_destroy (path);
1975 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1976 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1977 c = GCC_new (cid, NULL, path, own_pos);
1980 if (path->length - 1 == own_pos)
1982 /* If we are destination, why did the creation fail? */
1984 path_destroy (path);
1985 GCC_check_connections ();
1988 send_broken_unknown (cid, &my_full_id,
1989 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1991 path_destroy (path);
1992 GCC_check_connections ();
1995 GCP_add_path_to_all (path, GNUNET_NO);
1996 connection_reset_timeout (c, GNUNET_YES);
2000 path = path_duplicate (c->path);
2002 if (CADET_CONNECTION_NEW == c->state)
2003 connection_change_state (c, CADET_CONNECTION_SENT);
2005 /* Remember peers */
2006 dest_peer = GCP_get (&id[size - 1], GNUNET_YES);
2007 orig_peer = GCP_get (&id[0], GNUNET_YES);
2009 /* Is it a connection to us? */
2010 if (c->own_pos == path->length - 1)
2012 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2013 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
2015 add_to_peer (c, orig_peer);
2016 if (GNUNET_YES == does_connection_exist (c))
2018 /* Peer created a connection equal to one we think exists
2020 * Solution: Keep both and postpone disambiguation. In the meantime
2021 * the connection will time out or peer will inform us it is broken.
2024 * - Use explicit duplicate.
2025 * - Accept new conn and destroy the old. (interruption in higher level)
2026 * - Keep the one with higher ID / created by peer with higher ID. */
2027 schedule_check_duplicates (c);
2030 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
2031 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
2032 if (NULL == c->maintenance_q)
2033 send_connection_ack (c, GNUNET_NO);
2034 if (CADET_CONNECTION_SENT == c->state)
2035 connection_change_state (c, CADET_CONNECTION_ACK);
2039 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2040 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2041 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2042 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c,
2043 GNUNET_YES, GNUNET_YES, NULL, NULL);
2045 path_destroy (path);
2046 GCC_check_connections ();
2051 * Handler for connection confirmations.
2053 * @param peer Message sender (neighbor).
2054 * @param msg Message itself.
2057 GCC_handle_confirm (struct CadetPeer *peer,
2058 const struct GNUNET_CADET_ConnectionACK *msg)
2060 struct CadetConnection *c;
2061 enum CadetConnectionState oldstate;
2064 GCC_check_connections ();
2065 log_message (&msg->header, peer, &msg->cid);
2066 c = connection_get (&msg->cid);
2069 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2071 LOG (GNUNET_ERROR_TYPE_DEBUG,
2072 " don't know the connection!\n");
2073 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2074 GCC_check_connections ();
2077 if (GNUNET_NO != c->destroy)
2079 GNUNET_assert (CADET_CONNECTION_DESTROYED == c->state);
2080 GNUNET_STATISTICS_update (stats, "# control on dying connection",
2082 LOG (GNUNET_ERROR_TYPE_DEBUG,
2083 "connection %s being destroyed, ignoring confirm\n",
2085 GCC_check_connections ();
2089 oldstate = c->state;
2090 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GCP_2s (peer));
2091 if (get_next_hop (c) == peer)
2093 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2095 if (CADET_CONNECTION_SENT == oldstate)
2096 connection_change_state (c, CADET_CONNECTION_ACK);
2098 else if (get_prev_hop (c) == peer)
2100 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2102 connection_change_state (c, CADET_CONNECTION_READY);
2106 GNUNET_STATISTICS_update (stats, "# control on connection from wrong peer",
2108 GNUNET_break_op (0);
2112 connection_reset_timeout (c, fwd);
2114 GNUNET_assert (NULL != c->path);
2115 GCP_add_path_to_all (c->path, GNUNET_YES);
2117 /* Message for us as creator? */
2118 if (GNUNET_YES == GCC_is_origin (c, GNUNET_YES))
2120 if (GNUNET_NO != fwd)
2125 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2127 /* If just created, cancel the short timeout and start a long one */
2128 if (CADET_CONNECTION_SENT == oldstate)
2130 c->create_retry = 1;
2131 connection_reset_timeout (c, GNUNET_YES);
2134 /* Change connection state, send ACK */
2135 connection_change_state (c, CADET_CONNECTION_READY);
2136 send_connection_ack (c, GNUNET_YES);
2138 /* Change tunnel state, trigger KX */
2139 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2140 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2141 GCC_check_connections ();
2145 /* Message for us as destination? */
2146 if (GCC_is_terminal (c, GNUNET_YES))
2148 if (GNUNET_YES != fwd)
2153 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2155 /* If just created, cancel the short timeout and start a long one */
2156 if (CADET_CONNECTION_ACK == oldstate)
2157 connection_reset_timeout (c, GNUNET_NO);
2159 /* Change tunnel state */
2160 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2161 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2162 GCC_check_connections ();
2166 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2167 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2168 GNUNET_YES, NULL, NULL);
2170 GCC_check_connections ();
2175 * Handler for notifications of broken connections.
2177 * @param peer Message sender (neighbor).
2178 * @param msg Message itself.
2181 GCC_handle_broken (struct CadetPeer *peer,
2182 const struct GNUNET_CADET_ConnectionBroken *msg)
2184 struct CadetConnection *c;
2185 struct CadetTunnel *t;
2188 GCC_check_connections ();
2189 log_message (&msg->header, peer, &msg->cid);
2190 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", GNUNET_i2s (&msg->peer1));
2191 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", GNUNET_i2s (&msg->peer2));
2192 c = connection_get (&msg->cid);
2195 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2196 GNUNET_STATISTICS_update (stats, "# duplicate CONNECTION_BROKEN",
2198 GCC_check_connections ();
2204 fwd = is_fwd (c, peer);
2205 if (GNUNET_SYSERR == fwd)
2207 GNUNET_break_op (0);
2208 GCC_check_connections ();
2212 if (GCC_is_terminal (c, fwd))
2214 struct CadetPeer *endpoint;
2218 /* A terminal connection should not have 't' set to NULL. */
2220 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2223 endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
2224 if (2 < c->path->length)
2225 path_invalidate (c->path);
2226 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2228 connection_change_state (c, CADET_CONNECTION_BROKEN);
2229 GCT_remove_connection (t, c);
2236 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2237 GNUNET_YES, NULL, NULL);
2238 connection_cancel_queues (c, !fwd);
2240 GCC_check_connections ();
2246 * Handler for notifications of destroyed connections.
2248 * @param peer Message sender (neighbor).
2249 * @param msg Message itself.
2252 GCC_handle_destroy (struct CadetPeer *peer,
2253 const struct GNUNET_CADET_ConnectionDestroy *msg)
2255 struct CadetConnection *c;
2258 GCC_check_connections ();
2259 log_message (&msg->header, peer, &msg->cid);
2260 c = connection_get (&msg->cid);
2263 /* Probably already got the message from another path,
2264 * destroyed the tunnel and retransmitted to children.
2267 GNUNET_STATISTICS_update (stats,
2268 "# control on unknown connection",
2270 LOG (GNUNET_ERROR_TYPE_DEBUG,
2271 " connection unknown destroyed: previously destroyed?\n");
2272 GCC_check_connections ();
2276 fwd = is_fwd (c, peer);
2277 if (GNUNET_SYSERR == fwd)
2279 GNUNET_break_op (0);
2280 GCC_check_connections ();
2284 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2286 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2287 GNUNET_YES, NULL, NULL);
2289 else if (0 == c->pending_messages)
2291 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
2293 GCC_check_connections ();
2299 GCT_remove_connection (c->t, c);
2302 GCC_check_connections ();
2308 * Handler for cadet network traffic hop-by-hop acks.
2310 * @param peer Message sender (neighbor).
2311 * @param msg Message itself.
2314 GCC_handle_ack (struct CadetPeer *peer,
2315 const struct GNUNET_CADET_ACK *msg)
2317 struct CadetConnection *c;
2318 struct CadetFlowControl *fc;
2322 GCC_check_connections ();
2323 log_message (&msg->header, peer, &msg->cid);
2324 c = connection_get (&msg->cid);
2327 GNUNET_STATISTICS_update (stats,
2328 "# ack on unknown connection",
2331 send_broken_unknown (&msg->cid,
2335 GCC_check_connections ();
2339 /* Is this a forward or backward ACK? */
2340 if (get_next_hop (c) == peer)
2345 else if (get_prev_hop (c) == peer)
2352 GNUNET_break_op (0);
2356 ack = ntohl (msg->ack);
2357 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2358 GC_f2s (fwd), ack, fc->last_ack_recv);
2359 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2360 fc->last_ack_recv = ack;
2362 /* Cancel polling if the ACK is big enough. */
2363 if (NULL != fc->poll_task &&
2364 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2366 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2367 GNUNET_SCHEDULER_cancel (fc->poll_task);
2368 fc->poll_task = NULL;
2369 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2372 GCC_check_connections ();
2377 * Handler for cadet network traffic hop-by-hop data counter polls.
2379 * @param peer Message sender (neighbor).
2380 * @param msg Message itself.
2383 GCC_handle_poll (struct CadetPeer *peer,
2384 const struct GNUNET_CADET_Poll *msg)
2386 struct CadetConnection *c;
2387 struct CadetFlowControl *fc;
2391 GCC_check_connections ();
2392 log_message (&msg->header, peer, &msg->cid);
2393 c = connection_get (&msg->cid);
2396 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2398 LOG (GNUNET_ERROR_TYPE_DEBUG,
2399 "POLL message on unknown connection %s!\n",
2400 GNUNET_h2s (GC_h2hc (&msg->cid)));
2401 send_broken_unknown (&msg->cid,
2405 GCC_check_connections ();
2409 /* Is this a forward or backward ACK?
2410 * Note: a poll should never be needed in a loopback case,
2411 * since there is no possiblility of packet loss there, so
2412 * this way of discerining FWD/BCK should not be a problem.
2414 if (get_next_hop (c) == peer)
2416 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2419 else if (get_prev_hop (c) == peer)
2421 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2426 GNUNET_break_op (0);
2430 pid = ntohl (msg->pid);
2431 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2432 fc->last_pid_recv = pid;
2433 fwd = fc == &c->bck_fc;
2434 GCC_send_ack (c, fwd, GNUNET_YES);
2435 GCC_check_connections ();
2440 * Check the message against internal state and test if it goes FWD or BCK.
2442 * Updates the PID, state and timeout values for the connection.
2444 * @param message Message to check. It must belong to an existing connection.
2445 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2446 * @param c Connection this message should belong. If NULL, check fails.
2447 * @param sender Neighbor that sent the message.
2449 * @return #GNUNET_YES if the message goes FWD.
2450 * #GNUNET_NO if it goes BCK.
2451 * #GNUNET_SYSERR if there is an error (unauthorized sender, ...).
2454 check_message (const struct GNUNET_MessageHeader *message,
2455 const struct GNUNET_CADET_Hash* cid,
2456 struct CadetConnection *c,
2457 struct CadetPeer *sender,
2460 struct CadetFlowControl *fc;
2461 struct CadetPeer *hop;
2465 /* Check connection */
2468 GNUNET_STATISTICS_update (stats,
2469 "# unknown connection",
2471 LOG (GNUNET_ERROR_TYPE_DEBUG,
2472 "%s on unknown connection %s\n",
2473 GC_m2s (ntohs (message->type)),
2474 GNUNET_h2s (GC_h2hc (cid)));
2475 send_broken_unknown (cid,
2479 return GNUNET_SYSERR;
2482 /* Check if origin is as expected */
2483 hop = get_prev_hop (c);
2490 hop = get_next_hop (c);
2491 GNUNET_break (hop == c->next_peer);
2498 /* Unexpected peer sending traffic on a connection. */
2499 GNUNET_break_op (0);
2500 return GNUNET_SYSERR;
2504 /* Check PID for payload messages */
2505 type = ntohs (message->type);
2506 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
2508 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2509 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2510 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2511 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2513 GNUNET_break_op (0);
2514 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2515 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2516 pid, fc->last_pid_recv, fc->last_ack_sent);
2517 return GNUNET_SYSERR;
2519 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2523 delta = pid - fc->last_pid_recv;
2524 fc->last_pid_recv = pid;
2525 fc->recv_bitmap <<= delta;
2526 fc->recv_bitmap |= 1;
2530 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2531 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2533 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2534 pid, fc->last_pid_recv - 31);
2535 return GNUNET_SYSERR;
2537 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2541 /* Count as connection confirmation. */
2542 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2544 connection_change_state (c, CADET_CONNECTION_READY);
2547 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2548 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2551 connection_reset_timeout (c, fwd);
2558 * Handler for key exchange traffic (Axolotl KX).
2560 * @param peer Message sender (neighbor).
2561 * @param msg Message itself.
2564 GCC_handle_kx (struct CadetPeer *peer,
2565 const struct GNUNET_CADET_KX *msg)
2567 const struct GNUNET_CADET_Hash* cid;
2568 struct CadetConnection *c;
2571 GCC_check_connections ();
2573 log_message (&msg->header, peer, cid);
2575 c = connection_get (cid);
2576 fwd = check_message (&msg->header,
2582 /* If something went wrong, discard message. */
2583 if (GNUNET_SYSERR == fwd)
2585 GNUNET_break_op (0);
2586 GCC_check_connections ();
2590 /* Is this message for us? */
2591 if (GCC_is_terminal (c, fwd))
2593 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2594 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2600 GCT_handle_kx (c->t, msg);
2601 GCC_check_connections ();
2605 /* Message not for us: forward to next hop */
2606 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2607 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2608 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2609 GNUNET_NO, NULL, NULL);
2610 GCC_check_connections ();
2615 * Handler for encrypted cadet network traffic (channel mgmt, data).
2617 * @param peer Message sender (neighbor).
2618 * @param msg Message itself.
2621 GCC_handle_encrypted (struct CadetPeer *peer,
2622 const struct GNUNET_CADET_Encrypted *msg)
2624 const struct GNUNET_CADET_Hash* cid;
2625 struct CadetConnection *c;
2629 GCC_check_connections ();
2631 pid = ntohl (msg->pid);
2632 log_message (&msg->header, peer, cid);
2634 c = connection_get (cid);
2635 fwd = check_message (&msg->header,
2641 /* If something went wrong, discard message. */
2642 if (GNUNET_SYSERR == fwd)
2644 GNUNET_break_op (0);
2645 GCC_check_connections ();
2649 /* Is this message for us? */
2650 if (GCC_is_terminal (c, fwd))
2652 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2656 GNUNET_break (GNUNET_NO != c->destroy);
2659 GCT_handle_encrypted (c->t, msg);
2660 GCC_send_ack (c, fwd, GNUNET_NO);
2661 GCC_check_connections ();
2665 /* Message not for us: forward to next hop */
2666 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2667 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2668 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2669 GNUNET_NO, NULL, NULL);
2670 GCC_check_connections ();
2675 * Initialize the connections subsystem
2677 * @param c Configuration handle.
2680 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2682 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2684 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2687 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2688 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2689 GNUNET_SCHEDULER_shutdown ();
2694 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2697 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2698 "CADET", "MAX_CONNECTIONS", "MISSING");
2699 GNUNET_SCHEDULER_shutdown ();
2704 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2705 &refresh_connection_time))
2707 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2708 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2709 GNUNET_SCHEDULER_shutdown ();
2712 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2713 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2718 * Destroy each connection on shutdown.
2720 * @param cls Closure (unused).
2721 * @param key Current key code (CID, unused).
2722 * @param value Value in the hash map (`struct CadetConnection`)
2724 * @return #GNUNET_YES, because we should continue to iterate
2727 shutdown_iterator (void *cls,
2728 const struct GNUNET_HashCode *key,
2731 struct CadetConnection *c = value;
2733 c->state = CADET_CONNECTION_DESTROYED;
2740 * Shut down the connections subsystem.
2745 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n");
2746 GCC_check_connections ();
2747 GNUNET_CONTAINER_multihashmap_iterate (connections,
2750 GNUNET_CONTAINER_multihashmap_destroy (connections);
2756 * Create a connection.
2758 * @param cid Connection ID (either created locally or imposed remotely).
2759 * @param t Tunnel this connection belongs to (or NULL for transit connections);
2760 * @param path Path this connection has to use (copy is made).
2761 * @param own_pos Own position in the @c path path.
2763 * @return Newly created connection.
2764 * NULL in case of error: own id not in path, wrong neighbors, ...
2766 struct CadetConnection *
2767 GCC_new (const struct GNUNET_CADET_Hash *cid,
2768 struct CadetTunnel *t,
2769 struct CadetPeerPath *path,
2770 unsigned int own_pos)
2772 struct CadetConnection *c;
2773 struct CadetPeerPath *cpath;
2775 GCC_check_connections ();
2776 cpath = path_duplicate (path);
2777 GNUNET_assert (NULL != cpath);
2778 c = GNUNET_new (struct CadetConnection);
2780 GNUNET_assert (GNUNET_OK ==
2781 GNUNET_CONTAINER_multihashmap_put (connections,
2783 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2784 fc_init (&c->fwd_fc);
2785 fc_init (&c->bck_fc);
2790 GNUNET_assert (own_pos <= cpath->length - 1);
2791 c->own_pos = own_pos;
2794 if (GNUNET_OK != register_neighbors (c))
2798 /* We were the origin of this request, this means we have invalid
2799 * info about the paths to reach the destination. We must invalidate
2800 * the *original* path to avoid trying it again in the next minute.
2802 if (2 < path->length)
2803 path_invalidate (path);
2807 GCT_debug(t, GNUNET_ERROR_TYPE_WARNING);
2811 path_destroy (c->path);
2816 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2817 GCC_check_connections ();
2823 * Connection is no longer needed: destroy it.
2825 * Cancels all pending traffic (including possible DESTROY messages), all
2826 * maintenance tasks and removes the connection from neighbor peers and tunnel.
2828 * @param c Connection to destroy.
2831 GCC_destroy (struct CadetConnection *c)
2833 GCC_check_connections ();
2840 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2841 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2844 LOG (GNUNET_ERROR_TYPE_DEBUG,
2845 "destroying connection %s\n",
2847 LOG (GNUNET_ERROR_TYPE_DEBUG,
2848 " fc's f: %p, b: %p\n",
2849 &c->fwd_fc, &c->bck_fc);
2850 LOG (GNUNET_ERROR_TYPE_DEBUG,
2851 " fc tasks f: %u, b: %u\n",
2852 c->fwd_fc.poll_task,
2853 c->bck_fc.poll_task);
2855 /* Cancel all traffic */
2856 if (NULL != c->path)
2858 connection_cancel_queues (c, GNUNET_YES);
2859 connection_cancel_queues (c, GNUNET_NO);
2860 if (NULL != c->maintenance_q)
2862 GCP_send_cancel (c->maintenance_q);
2863 c->maintenance_q = NULL;
2866 unregister_neighbors (c);
2867 path_destroy (c->path);
2870 /* Delete from tunnel */
2872 GCT_remove_connection (c->t, c);
2874 if (NULL != c->check_duplicates_task)
2875 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
2876 if (NULL != c->fwd_maintenance_task)
2877 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2878 if (NULL != c->bck_maintenance_task)
2879 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2881 if (GNUNET_NO == c->was_removed)
2883 GNUNET_break (GNUNET_YES ==
2884 GNUNET_CONTAINER_multihashmap_remove (connections,
2888 GNUNET_STATISTICS_update (stats,
2893 GCC_check_connections ();
2898 * Get the connection ID.
2900 * @param c Connection to get the ID from.
2902 * @return ID of the connection.
2904 const struct GNUNET_CADET_Hash *
2905 GCC_get_id (const struct CadetConnection *c)
2912 * Get the connection ID.
2914 * @param c Connection to get the ID from.
2916 * @return ID of the connection.
2918 const struct GNUNET_HashCode *
2919 GCC_get_h (const struct CadetConnection *c)
2921 return GC_h2hc (&c->id);
2926 * Get the connection path.
2928 * @param c Connection to get the path from.
2930 * @return path used by the connection.
2932 const struct CadetPeerPath *
2933 GCC_get_path (const struct CadetConnection *c)
2935 if (GNUNET_NO == c->destroy)
2942 * Get the connection state.
2944 * @param c Connection to get the state from.
2946 * @return state of the connection.
2948 enum CadetConnectionState
2949 GCC_get_state (const struct CadetConnection *c)
2955 * Get the connection tunnel.
2957 * @param c Connection to get the tunnel from.
2959 * @return tunnel of the connection.
2961 struct CadetTunnel *
2962 GCC_get_tunnel (const struct CadetConnection *c)
2969 * Get free buffer space in a connection.
2971 * @param c Connection.
2972 * @param fwd Is query about FWD traffic?
2974 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2977 GCC_get_buffer (struct CadetConnection *c, int fwd)
2979 struct CadetFlowControl *fc;
2981 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2983 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
2984 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
2985 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
2987 return (fc->queue_max - fc->queue_n);
2992 * Get how many messages have we allowed to send to us from a direction.
2994 * @param c Connection.
2995 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2997 * @return last_ack_sent - last_pid_recv
3000 GCC_get_allowed (struct CadetConnection *c, int fwd)
3002 struct CadetFlowControl *fc;
3004 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3005 if (CADET_CONNECTION_READY != c->state
3006 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
3010 return (fc->last_ack_sent - fc->last_pid_recv);
3015 * Get messages queued in a connection.
3017 * @param c Connection.
3018 * @param fwd Is query about FWD traffic?
3020 * @return Number of messages queued.
3023 GCC_get_qn (struct CadetConnection *c, int fwd)
3025 struct CadetFlowControl *fc;
3027 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3034 * Get next PID to use.
3036 * @param c Connection.
3037 * @param fwd Is query about FWD traffic?
3039 * @return Next PID to use.
3042 GCC_get_pid (struct CadetConnection *c, int fwd)
3044 struct CadetFlowControl *fc;
3047 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3055 * Allow the connection to advertise a buffer of the given size.
3057 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3058 * allowing up to last_pid_recv + buffer.
3060 * @param c Connection.
3061 * @param buffer How many more messages the connection can accept.
3062 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3065 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3067 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3068 GCC_2s (c), buffer, GC_f2s (fwd));
3069 send_ack (c, buffer, fwd, GNUNET_NO);
3074 * Notify other peers on a connection of a broken link. Mark connections
3075 * to destroy after all traffic has been sent.
3077 * @param c Connection on which there has been a disconnection.
3078 * @param peer Peer that disconnected.
3081 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
3083 struct CadetFlowControl *fc;
3087 GCC_check_connections ();
3088 strncpy (peer_name, GCP_2s (peer), 16);
3089 peer_name[15] = '\0';
3090 LOG (GNUNET_ERROR_TYPE_DEBUG,
3091 "shutting down %s, %s disconnected\n",
3092 GCC_2s (c), peer_name);
3094 invalidate_paths (c, peer);
3096 fwd = is_fwd (c, peer);
3097 if (GNUNET_SYSERR == fwd)
3102 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3103 (GNUNET_NO != c->destroy) )
3105 /* Local shutdown, or other peer already down (hence 'c->destroy');
3106 so there is no one to notify about this, just clean up. */
3108 GCC_check_connections ();
3111 /* Mark FlowControl towards the peer as unavaliable. */
3112 fc = fwd ? &c->bck_fc : &c->fwd_fc;
3115 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3117 /* Connection will have at least one pending message
3118 * (the one we just scheduled), so delay destruction
3119 * and remove from map so we don't use accidentally. */
3121 GNUNET_assert (GNUNET_NO == c->was_removed);
3122 c->was_removed = GNUNET_YES;
3123 GNUNET_break (GNUNET_YES ==
3124 GNUNET_CONTAINER_multihashmap_remove (connections,
3127 /* Cancel queue in the direction that just died. */
3128 connection_cancel_queues (c, ! fwd);
3129 GCC_stop_poll (c, ! fwd);
3130 unregister_neighbors (c);
3131 GCC_check_connections ();
3136 * Is this peer the first one on the connection?
3138 * @param c Connection.
3139 * @param fwd Is this about fwd traffic?
3141 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3144 GCC_is_origin (struct CadetConnection *c, int fwd)
3146 if (!fwd && c->path->length - 1 == c->own_pos )
3148 if (fwd && 0 == c->own_pos)
3155 * Is this peer the last one on the connection?
3157 * @param c Connection.
3158 * @param fwd Is this about fwd traffic?
3159 * Note that the ROOT is the terminal for BCK traffic!
3161 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3164 GCC_is_terminal (struct CadetConnection *c, int fwd)
3166 return GCC_is_origin (c, ! fwd);
3171 * See if we are allowed to send by the next hop in the given direction.
3173 * @param c Connection.
3174 * @param fwd Is this about fwd traffic?
3176 * @return #GNUNET_YES in case it's OK to send.
3179 GCC_is_sendable (struct CadetConnection *c, int fwd)
3181 struct CadetFlowControl *fc;
3183 LOG (GNUNET_ERROR_TYPE_DEBUG,
3184 " checking sendability of %s traffic on %s\n",
3185 GC_f2s (fwd), GCC_2s (c));
3191 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3192 LOG (GNUNET_ERROR_TYPE_DEBUG,
3193 " last ack recv: %u, last pid sent: %u\n",
3194 fc->last_ack_recv, fc->last_pid_sent);
3195 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3197 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3200 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3206 * Check if this connection is a direct one (never trim a direct connection).
3208 * @param c Connection.
3210 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3213 GCC_is_direct (struct CadetConnection *c)
3215 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3220 * Sends a completely built message on a connection, properly registering
3221 * all used resources.
3223 * @param message Message to send.
3224 * @param payload_type Type of payload, in case the message is encrypted.
3225 * 0 for restransmissions (when type is no longer known)
3226 * UINT16_MAX when not applicable.
3227 * @param payload_id ID of the payload (PID, ACK, ...).
3228 * @param c Connection on which this message is transmitted.
3229 * @param fwd Is this a fwd message?
3230 * @param force Force the connection to accept the message (buffer overfill).
3231 * @param cont Continuation called once message is sent. Can be NULL.
3232 * @param cont_cls Closure for @c cont.
3234 * @return Handle to cancel the message before it's sent.
3235 * NULL on error or if @c cont is NULL.
3236 * Invalid on @c cont call.
3238 struct CadetConnectionQueue *
3239 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3240 uint16_t payload_type, uint32_t payload_id,
3241 struct CadetConnection *c, int fwd, int force,
3242 GCC_sent cont, void *cont_cls)
3244 struct CadetFlowControl *fc;
3245 struct CadetConnectionQueue *q;
3249 size = ntohs (message->size);
3250 type = ntohs (message->type);
3252 GCC_check_connections ();
3253 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3254 if (0 == fc->queue_max)
3260 LOG (GNUNET_ERROR_TYPE_INFO,
3261 "--> %s (%s %4u) on conn %s (%p) %s [%5u]\n",
3262 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), c,
3266 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3267 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3268 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3269 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3270 if (GNUNET_NO == force)
3276 case GNUNET_MESSAGE_TYPE_CADET_KX:
3279 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3280 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3281 /* Should've only be used for restransmissions. */
3282 GNUNET_break (0 == payload_type);
3285 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3286 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3287 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3288 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3289 GNUNET_assert (GNUNET_YES == force);
3297 if (fc->queue_n > fc->queue_max && GNUNET_NO == force)
3299 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3302 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3303 fc->queue_n, fc->queue_max);
3304 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3308 return NULL; /* Drop this message */
3311 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3312 GCC_2s (c), c->pending_messages);
3313 c->pending_messages++;
3315 q = GNUNET_new (struct CadetConnectionQueue);
3317 q->peer_q = GCP_send (get_hop (c, fwd), message,
3318 payload_type, payload_id,
3320 &conn_message_sent, q);
3321 if (NULL == q->peer_q)
3323 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3325 GCC_check_connections ();
3329 q->cont_cls = cont_cls;
3330 GNUNET_CONTAINER_DLL_insert (fc->q_head, fc->q_tail, q);
3331 GCC_check_connections ();
3337 * Cancel a previously sent message while it's in the queue.
3339 * ONLY can be called before the continuation given to the send function
3340 * is called. Once the continuation is called, the message is no longer in the
3343 * @param q Handle to the queue.
3346 GCC_cancel (struct CadetConnectionQueue *q)
3348 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3350 /* send_cancel calls message_sent, which calls q->cont and frees q */
3351 GCP_send_cancel (q->peer_q);
3352 GCC_check_connections ();
3357 * Sends a CREATE CONNECTION message for a path to a peer.
3358 * Changes the connection and tunnel states if necessary.
3360 * @param c Connection to create.
3363 GCC_send_create (struct CadetConnection *c)
3365 enum CadetTunnelCState state;
3368 GCC_check_connections ();
3369 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3370 size += c->path->length * sizeof (struct GNUNET_PeerIdentity);
3372 /* Allocate message on the stack */
3373 unsigned char cbuf[size];
3374 struct GNUNET_CADET_ConnectionCreate *msg;
3375 struct GNUNET_PeerIdentity *peers;
3377 msg = (struct GNUNET_CADET_ConnectionCreate *) cbuf;
3378 msg->header.size = htons (size);
3379 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
3380 msg->reserved = htonl (0);
3381 msg->cid = *GCC_get_id (c);
3382 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3383 for (int i = 0; i < c->path->length; i++)
3385 GNUNET_PEER_resolve (c->path->peers[i], peers++);
3387 GNUNET_assert (NULL == c->maintenance_q);
3388 c->maintenance_q = GCP_send (get_next_hop (c),
3390 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0,
3392 &conn_message_sent, NULL);
3395 LOG (GNUNET_ERROR_TYPE_INFO, "==> %s %19s on conn %s (%p) FWD [%5u]\n",
3396 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE), "",
3397 GCC_2s (c), c, size);
3398 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3399 c, c->pending_messages);
3400 c->pending_messages++;
3402 state = GCT_get_cstate (c->t);
3403 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3404 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
3405 if (CADET_CONNECTION_NEW == c->state)
3406 connection_change_state (c, CADET_CONNECTION_SENT);
3407 GCC_check_connections ();
3412 * Send an ACK on the appropriate connection/channel, depending on
3413 * the direction and the position of the peer.
3415 * @param c Which connection to send the hop-by-hop ACK.
3416 * @param fwd Is this a fwd ACK? (will go dest->root).
3417 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
3420 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
3422 unsigned int buffer;
3424 GCC_check_connections ();
3425 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
3426 GC_f2s (fwd), GCC_2s (c));
3434 if (GNUNET_NO != c->destroy)
3436 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
3437 GCC_check_connections ();
3441 /* Get available buffer space */
3442 if (GCC_is_terminal (c, fwd))
3444 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
3445 buffer = GCT_get_channels_buffer (c->t);
3449 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
3450 buffer = GCC_get_buffer (c, fwd);
3452 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
3453 if (0 == buffer && GNUNET_NO == force)
3455 GCC_check_connections ();
3459 /* Send available buffer space */
3460 if (GNUNET_YES == GCC_is_origin (c, fwd))
3462 GNUNET_assert (NULL != c->t);
3463 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
3464 GCT_unchoke_channels (c->t);
3468 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
3469 send_ack (c, buffer, fwd, force);
3471 GCC_check_connections ();
3476 * Send a message to all peers in this connection that the connection
3477 * is no longer valid.
3479 * If some peer should not receive the message, it should be zero'ed out
3480 * before calling this function.
3482 * @param c The connection whose peers to notify.
3485 GCC_send_destroy (struct CadetConnection *c)
3487 struct GNUNET_CADET_ConnectionDestroy msg;
3489 if (GNUNET_YES == c->destroy)
3491 GCC_check_connections ();
3492 msg.header.size = htons (sizeof (msg));
3493 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3495 msg.reserved = htonl (0);
3496 LOG (GNUNET_ERROR_TYPE_DEBUG,
3497 " sending connection destroy for connection %s\n",
3500 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3501 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c,
3502 GNUNET_YES, GNUNET_YES, NULL, NULL);
3503 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3504 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c,
3505 GNUNET_NO, GNUNET_YES, NULL, NULL);
3507 GCC_check_connections ();
3512 * @brief Start a polling timer for the connection.
3514 * When a neighbor does not accept more traffic on the connection it could be
3515 * caused by a simple congestion or by a lost ACK. Polling enables to check
3516 * for the lastest ACK status for a connection.
3518 * @param c Connection.
3519 * @param fwd Should we poll in the FWD direction?
3522 GCC_start_poll (struct CadetConnection *c, int fwd)
3524 struct CadetFlowControl *fc;
3526 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3527 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3529 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3531 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL already in progress (t: %p, m: %p)\n",
3532 fc->poll_task, fc->poll_msg);
3535 if (0 == fc->queue_max)
3537 /* Should not be needed, traffic should've been cancelled. */
3539 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not possible, peer disconnected\n");
3542 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3543 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3544 &send_connection_poll,
3550 * @brief Stop polling a connection for ACKs.
3552 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3554 * @param c Connection.
3555 * @param fwd Should we stop the poll in the FWD direction?
3558 GCC_stop_poll (struct CadetConnection *c, int fwd)
3560 struct CadetFlowControl *fc;
3562 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3563 if (NULL != fc->poll_task)
3565 GNUNET_SCHEDULER_cancel (fc->poll_task);
3566 fc->poll_task = NULL;
3568 if (NULL != fc->poll_msg)
3570 GCC_cancel (fc->poll_msg);
3571 fc->poll_msg = NULL;
3577 * Get a (static) string for a connection.
3579 * @param c Connection.
3582 GCC_2s (const struct CadetConnection *c)
3589 static char buf[128];
3591 SPRINTF (buf, "%s (->%s)",
3592 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3595 return GNUNET_h2s (GC_h2hc (&c->id));
3600 * Log all possible info about the connection state.
3602 * @param c Connection to debug.
3603 * @param level Debug level to use.
3606 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3611 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3613 __FILE__, __FUNCTION__, __LINE__);
3619 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3623 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3624 s = path_2s (c->path);
3625 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3627 LOG2 (level, "CCC state: %s, destroy: %u\n",
3628 GCC_state2s (c->state), c->destroy);
3629 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3630 if (NULL != c->perf)
3631 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3633 LOG2 (level, "CCC FWD flow control:\n");
3634 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3635 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3636 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3637 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3638 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3639 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3640 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3641 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3643 LOG2 (level, "CCC BCK flow control:\n");
3644 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3645 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3646 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3647 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3648 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3649 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3650 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3651 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3653 LOG2 (level, "CCC DEBUG CONNECTION END\n");