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.
115 struct CadetEncryptedMessageIdentifier next_pid;
118 * ID of the last packet sent towards the peer.
120 struct CadetEncryptedMessageIdentifier last_pid_sent;
123 * ID of the last packet received from the peer.
125 struct CadetEncryptedMessageIdentifier last_pid_recv;
128 * Bitmap of past 32 messages received:
129 * - LSB being @c last_pid_recv.
130 * - MSB being @c last_pid_recv - 31 (mod UINTMAX).
132 uint32_t recv_bitmap;
135 * Last ACK sent to the peer (peer is not allowed to send
136 * messages with PIDs higher than this value).
138 struct CadetEncryptedMessageIdentifier last_ack_sent;
141 * Last ACK sent towards the origin (for traffic towards leaf node).
143 struct CadetEncryptedMessageIdentifier last_ack_recv;
146 * Task to poll the peer in case of a lost ACK causes stall.
148 struct GNUNET_SCHEDULER_Task *poll_task;
151 * How frequently to poll for ACKs.
153 struct GNUNET_TIME_Relative poll_time;
156 * Queued poll message, to cancel if not necessary anymore (got ACK).
158 struct CadetConnectionQueue *poll_msg;
161 * Queued poll message, to cancel if not necessary anymore (got ACK).
163 struct CadetConnectionQueue *ack_msg;
167 * Keep a record of the last messages sent on this connection.
169 struct CadetConnectionPerformance
172 * Circular buffer for storing measurements.
174 double usecsperbyte[AVG_MSGS];
177 * Running average of @c usecsperbyte.
182 * How many values of @c usecsperbyte are valid.
187 * Index of the next "free" position in @c usecsperbyte.
194 * Struct containing all information regarding a connection to a peer.
196 struct CadetConnection
199 * Tunnel this connection is part of.
201 struct CadetTunnel *t;
204 * Flow control information for traffic fwd.
206 struct CadetFlowControl fwd_fc;
209 * Flow control information for traffic bck.
211 struct CadetFlowControl bck_fc;
214 * Measure connection performance on the endpoint.
216 struct CadetConnectionPerformance *perf;
219 * ID of the connection.
221 struct GNUNET_CADET_ConnectionTunnelIdentifier id;
224 * Path being used for the tunnel. At the origin of the connection
225 * it's a pointer to the destination's path pool, otherwise just a copy.
227 struct CadetPeerPath *path;
230 * Task to keep the used paths alive at the owner,
231 * time tunnel out on all the other peers.
233 struct GNUNET_SCHEDULER_Task *fwd_maintenance_task;
236 * Task to keep the used paths alive at the destination,
237 * time tunnel out on all the other peers.
239 struct GNUNET_SCHEDULER_Task *bck_maintenance_task;
242 * Queue handle for maintainance traffic. One handle for FWD and BCK since
243 * one peer never needs to maintain both directions (no loopback connections).
245 struct CadetPeerQueue *maintenance_q;
248 * Should equal #get_next_hop(), or NULL if that peer disconnected.
250 struct CadetPeer *next_peer;
253 * Should equal #get_prev_hop(), or NULL if that peer disconnected.
255 struct CadetPeer *prev_peer;
258 * State of the connection.
260 enum CadetConnectionState state;
263 * Position of the local peer in the path.
265 unsigned int own_pos;
268 * Pending message count.
270 unsigned int pending_messages;
274 * - if 0, connection in use.
275 * - if 1, destroy on last message.
276 * - if 2, connection is being destroyed don't re-enter.
281 * In-connection-map flag. Sometimes, when @e destroy is set but
282 * actual destruction is delayed to enable us to finish processing
283 * queues (i.e. in the direction that is still working), we remove
284 * the connection from the map to prevent it from still being
285 * found (and used) by accident. This flag is set to #GNUNET_YES
286 * for a connection that is not in the #connections map. Should
287 * only be #GNUNET_YES if #destroy is also non-zero.
292 * Counter to do exponential backoff when creating a connection (max 64).
294 unsigned short create_retry;
297 * Task to check if connection has duplicates.
299 struct GNUNET_SCHEDULER_Task *check_duplicates_task;
303 /******************************************************************************/
304 /******************************* GLOBALS ***********************************/
305 /******************************************************************************/
308 * Global handle to the statistics service.
310 extern struct GNUNET_STATISTICS_Handle *stats;
313 * Local peer own ID (memory efficient handle).
315 extern GNUNET_PEER_Id myid;
318 * Local peer own ID (full value).
320 extern struct GNUNET_PeerIdentity my_full_id;
323 * Connections known, indexed by cid (CadetConnection).
325 static struct GNUNET_CONTAINER_MultiShortmap *connections;
328 * How many connections are we willing to maintain.
329 * Local connections are always allowed,
330 * even if there are more connections than max.
332 static unsigned long long max_connections;
335 * How many messages *in total* are we willing to queue, divide by number of
336 * connections to get connection queue size.
338 static unsigned long long max_msgs_queue;
341 * How often to send path keepalives. Paths timeout after 4 missed.
343 static struct GNUNET_TIME_Relative refresh_connection_time;
346 * How often to send path create / ACKs.
348 static struct GNUNET_TIME_Relative create_connection_time;
351 /******************************************************************************/
352 /******************************** STATIC ***********************************/
353 /******************************************************************************/
357 #if 0 // avoid compiler warning for unused static function
359 fc_debug (struct CadetFlowControl *fc)
361 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
362 ntohl (fc->last_pid_recv.pid),
363 ntohl (fc->last_ack_sent.pid));
364 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
365 fc->last_pid_sent, fc->last_ack_recv);
366 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
367 fc->queue_n, fc->queue_max);
371 connection_debug (struct CadetConnection *c)
375 LOG (GNUNET_ERROR_TYPE_INFO, "DEBUG NULL CONNECTION\n");
378 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
379 peer2s (c->t->peer), GCC_2s (c));
380 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
381 c->state, c->pending_messages);
382 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
383 fc_debug (&c->fwd_fc);
384 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
385 fc_debug (&c->bck_fc);
391 * Schedule next keepalive task, taking in consideration
392 * the connection state and number of retries.
394 * @param c Connection for which to schedule the next keepalive.
395 * @param fwd Direction for the next keepalive.
398 schedule_next_keepalive (struct CadetConnection *c, int fwd);
402 * Resets the connection timeout task, some other message has done the
404 * - For the first peer on the direction this means to send
405 * a keepalive or a path confirmation message (either create or ACK).
406 * - For all other peers, this means to destroy the connection,
407 * due to lack of activity.
408 * Starts the timeout if no timeout was running (connection just created).
410 * @param c Connection whose timeout to reset.
411 * @param fwd Is this forward?
414 connection_reset_timeout (struct CadetConnection *c, int fwd);
418 * Get string description for tunnel state. Reentrant.
420 * @param s Tunnel state.
422 * @return String representation.
425 GCC_state2s (enum CadetConnectionState s)
429 case CADET_CONNECTION_NEW:
430 return "CADET_CONNECTION_NEW";
431 case CADET_CONNECTION_SENT:
432 return "CADET_CONNECTION_SENT";
433 case CADET_CONNECTION_ACK:
434 return "CADET_CONNECTION_ACK";
435 case CADET_CONNECTION_READY:
436 return "CADET_CONNECTION_READY";
437 case CADET_CONNECTION_DESTROYED:
438 return "CADET_CONNECTION_DESTROYED";
439 case CADET_CONNECTION_BROKEN:
440 return "CADET_CONNECTION_BROKEN";
443 LOG (GNUNET_ERROR_TYPE_ERROR, " conn state %u unknown!\n", s);
444 return "CADET_CONNECTION_STATE_ERROR";
450 * Initialize a Flow Control structure to the initial state.
452 * @param fc Flow Control structure to initialize.
455 fc_init (struct CadetFlowControl *fc)
457 fc->next_pid.pid = 0;
458 fc->last_pid_sent.pid = htonl (UINT32_MAX);
459 fc->last_pid_recv.pid = htonl (UINT32_MAX);
460 fc->last_ack_sent.pid = (uint32_t) 0;
461 fc->last_ack_recv.pid = (uint32_t) 0;
462 fc->poll_task = NULL;
463 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
465 fc->queue_max = (max_msgs_queue / max_connections) + 1;
472 * @param cid Connection ID.
474 * @return conntection with the given ID @cid or NULL if not found.
476 static struct CadetConnection *
477 connection_get (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
479 return GNUNET_CONTAINER_multishortmap_get (connections,
480 &cid->connection_of_tunnel);
485 * Change the connection state. Cannot change a connection marked as destroyed.
487 * @param c Connection to change.
488 * @param state New state to set.
491 connection_change_state (struct CadetConnection* c,
492 enum CadetConnectionState state)
494 LOG (GNUNET_ERROR_TYPE_DEBUG,
495 "Connection %s state %s -> %s\n",
496 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
497 if (CADET_CONNECTION_DESTROYED <= c->state) /* Destroyed or broken. */
499 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
503 if (CADET_CONNECTION_READY == state)
509 * Mark a connection as "destroyed", to send all pending traffic and freeing
510 * all associated resources, without accepting new status changes on it.
512 * @param c Connection to mark as destroyed.
515 mark_destroyed (struct CadetConnection *c)
517 c->destroy = GNUNET_YES;
518 connection_change_state (c, CADET_CONNECTION_DESTROYED);
523 * Function called if a connection has been stalled for a while,
524 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
526 * @param cls Closure (poll ctx).
529 send_poll (void *cls);
533 * Send an ACK on the connection, informing the predecessor about
534 * the available buffer space. Should not be called in case the peer
535 * is origin (no predecessor) in the @c fwd direction.
537 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
538 * the ACK itself goes "back" (dest->root).
540 * @param c Connection on which to send the ACK.
541 * @param buffer How much space free to advertise?
542 * @param fwd Is this FWD ACK? (Going dest -> root)
543 * @param force Don't optimize out.
546 send_ack (struct CadetConnection *c,
551 static struct CadetEncryptedMessageIdentifier zero;
552 struct CadetFlowControl *next_fc;
553 struct CadetFlowControl *prev_fc;
554 struct GNUNET_CADET_ConnectionEncryptedAckMessage msg;
555 struct CadetEncryptedMessageIdentifier ack_cemi;
558 GCC_check_connections ();
559 GNUNET_assert (GNUNET_NO == GCC_is_origin (c, fwd));
561 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
562 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
564 LOG (GNUNET_ERROR_TYPE_DEBUG, "send %s ack on %s\n",
565 GC_f2s (fwd), GCC_2s (c));
567 /* Check if we need to transmit the ACK. */
568 delta = ntohl (prev_fc->last_ack_sent.pid) - ntohl (prev_fc->last_pid_recv.pid);
569 if (3 < delta && buffer < delta && GNUNET_NO == force)
571 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, delta > 3\n");
572 LOG (GNUNET_ERROR_TYPE_DEBUG,
573 " last pid recv: %u, last ack sent: %u\n",
574 ntohl (prev_fc->last_pid_recv.pid),
575 ntohl (prev_fc->last_ack_sent.pid));
576 GCC_check_connections ();
580 /* Ok, ACK might be necessary, what PID to ACK? */
581 ack_cemi.pid = htonl (ntohl (prev_fc->last_pid_recv.pid) + buffer);
582 LOG (GNUNET_ERROR_TYPE_DEBUG,
583 " ACK %u, last PID %u, last ACK %u, qmax %u, q %u\n",
584 ntohl (ack_cemi.pid),
585 ntohl (prev_fc->last_pid_recv.pid),
586 ntohl (prev_fc->last_ack_sent.pid),
587 next_fc->queue_max, next_fc->queue_n);
588 if ( (ack_cemi.pid == prev_fc->last_ack_sent.pid) &&
589 (GNUNET_NO == force) )
591 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
592 GCC_check_connections ();
596 /* Check if message is already in queue */
597 if (NULL != prev_fc->ack_msg)
599 if (GC_is_pid_bigger (ntohl (ack_cemi.pid),
600 ntohl (prev_fc->last_ack_sent.pid)))
602 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
603 GCC_cancel (prev_fc->ack_msg);
604 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
608 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
609 GCC_check_connections ();
613 GNUNET_break (GC_is_pid_bigger (ntohl (ack_cemi.pid),
614 ntohl (prev_fc->last_ack_sent.pid)));
615 prev_fc->last_ack_sent = ack_cemi;
617 /* Build ACK message and send on conn */
618 msg.header.size = htons (sizeof (msg));
619 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK);
620 msg.cemi_max = ack_cemi;
623 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header,
630 GNUNET_assert (NULL != prev_fc->ack_msg);
631 GCC_check_connections ();
636 * Update performance information if we are a connection's endpoint.
638 * @param c Connection to update.
639 * @param wait How much time did we wait to send the last message.
640 * @param size Size of the last message.
643 update_perf (struct CadetConnection *c,
644 struct GNUNET_TIME_Relative wait,
647 struct CadetConnectionPerformance *p;
651 return; /* Only endpoints are interested in timing. */
654 usecsperbyte = ((double) wait.rel_value_us) / size;
655 if (p->size == AVG_MSGS)
657 /* Array is full. Substract oldest value, add new one and store. */
658 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
659 p->usecsperbyte[p->idx] = usecsperbyte;
660 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
664 /* Array not yet full. Add current value to avg and store. */
665 p->usecsperbyte[p->idx] = usecsperbyte;
667 p->avg += p->usecsperbyte[p->idx];
671 p->idx = (p->idx + 1) % AVG_MSGS;
676 * Callback called when a connection queued message is sent.
678 * Calculates the average time and connection packet tracking.
680 * @param cls Closure (ConnectionQueue Handle), can be NULL.
681 * @param c Connection this message was on.
682 * @param fwd Was this a FWD going message?
683 * @param sent Was it really sent? (Could have been canceled)
684 * @param type Type of message sent.
685 * @param payload_type Type of payload, if applicable.
686 * @param pid Message ID, or 0 if not applicable (create, destroy, etc).
687 * @param size Size of the message.
688 * @param wait Time spent waiting for core (only the time for THIS message)
691 conn_message_sent (void *cls,
692 struct CadetConnection *c,
696 uint16_t payload_type,
697 struct CadetEncryptedMessageIdentifier pid,
699 struct GNUNET_TIME_Relative wait)
701 struct CadetConnectionQueue *q = cls;
702 struct CadetFlowControl *fc;
705 GCC_check_connections ();
706 LOG (GNUNET_ERROR_TYPE_INFO,
707 ">>> %s (%s %4u) on conn %s (%p) %s [%5u] in queue %s\n",
708 GC_m2s (type), GC_m2s (payload_type),
713 GNUNET_STRINGS_relative_time_to_string (wait, GNUNET_YES));
715 /* If c is NULL, nothing to update. */
718 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
719 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
721 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
724 GCC_check_connections ();
728 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
729 sent ? "" : "not ", GC_f2s (fwd),
730 GC_m2s (type), GC_m2s (payload_type),
732 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
734 /* Update flow control info. */
735 fc = fwd ? &c->fwd_fc : &c->bck_fc;
739 GNUNET_CONTAINER_DLL_remove (fc->q_head, fc->q_tail, q);
743 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
744 q->cont (q->cont_cls, c, q, type, fwd, size);
748 else /* CONN_CREATE or CONN_ACK */
750 GNUNET_assert (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED != type);
754 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
755 c->pending_messages--;
756 if ( (GNUNET_YES == c->destroy) &&
757 (0 == c->pending_messages) )
759 LOG (GNUNET_ERROR_TYPE_DEBUG,
760 "! destroying connection!\n");
762 GCC_check_connections ();
766 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
769 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
770 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK:
771 c->maintenance_q = NULL;
772 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
773 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
774 schedule_next_keepalive (c, fwd);
777 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED:
778 if (GNUNET_YES == sent)
780 fc->last_pid_sent = pid;
781 if (GC_is_pid_bigger (ntohl (fc->last_pid_sent.pid) + 1,
782 ntohl (fc->last_ack_recv.pid)) )
783 GCC_start_poll (c, fwd);
784 GCC_send_ack (c, fwd, GNUNET_NO);
785 connection_reset_timeout (c, fwd);
788 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
789 if (GNUNET_NO == forced)
792 LOG (GNUNET_ERROR_TYPE_DEBUG,
793 "! accounting pid %u\n",
794 ntohl (fc->last_pid_sent.pid));
798 LOG (GNUNET_ERROR_TYPE_DEBUG,
799 "! forced, Q_N not accounting pid %u\n",
800 ntohl (fc->last_pid_sent.pid));
804 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX:
805 if (GNUNET_YES == sent)
806 connection_reset_timeout (c, fwd);
809 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL:
813 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
816 if (0 == fc->queue_max)
818 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL cancelled: neighbor disconnected\n");
821 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
823 GNUNET_assert (NULL == fc->poll_task);
824 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
825 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
827 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
830 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK:
834 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
835 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
839 LOG (GNUNET_ERROR_TYPE_ERROR, "%s unknown\n", GC_m2s (type));
843 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
845 update_perf (c, wait, size);
846 GCC_check_connections ();
851 * Get the previous hop in a connection
853 * @param c Connection.
855 * @return Previous peer in the connection.
857 static struct CadetPeer *
858 get_prev_hop (const struct CadetConnection *c)
864 LOG (GNUNET_ERROR_TYPE_DEBUG,
865 " get prev hop %s [%u/%u]\n",
866 GCC_2s (c), c->own_pos, c->path->length);
867 if (0 == c->own_pos || c->path->length < 2)
868 id = c->path->peers[0];
870 id = c->path->peers[c->own_pos - 1];
872 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
873 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
875 return GCP_get_short (id, GNUNET_YES);
880 * Get the next hop in a connection
882 * @param c Connection.
884 * @return Next peer in the connection.
886 static struct CadetPeer *
887 get_next_hop (const struct CadetConnection *c)
894 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
895 GCC_2s (c), c->own_pos, c->path->length);
896 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
897 id = c->path->peers[c->path->length - 1];
899 id = c->path->peers[c->own_pos + 1];
901 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
902 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
904 return GCP_get_short (id, GNUNET_YES);
909 * Check that the direct neighbours (previous and next hop)
910 * are properly associated with this connection.
912 * @param c connection to check
915 check_neighbours (const struct CadetConnection *c)
918 return; /* nothing to check */
919 GCP_check_connection (get_next_hop (c), c);
920 GCP_check_connection (get_prev_hop (c), c);
925 * Helper for #GCC_check_connections(). Calls #check_neighbours().
929 * @param value the `struct CadetConnection` to check
930 * @return #GNUNET_OK (continue to iterate)
933 check_connection (void *cls,
934 const struct GNUNET_ShortHashCode *key,
937 struct CadetConnection *c = value;
939 check_neighbours (c);
945 * Check invariants for all connections using #check_neighbours().
948 GCC_check_connections ()
950 if (0 == CHECK_INVARIANTS)
952 if (NULL == connections)
954 GNUNET_CONTAINER_multishortmap_iterate (connections,
961 * Get the hop in a connection.
963 * @param c Connection.
964 * @param fwd Next in the FWD direction?
966 * @return Next peer in the connection.
968 static struct CadetPeer *
969 get_hop (struct CadetConnection *c, int fwd)
971 return (fwd) ? get_next_hop (c) : get_prev_hop (c);
976 * Get a bit mask for a message received out-of-order.
978 * @param last_pid_recv Last PID we received prior to the out-of-order.
979 * @param ooo_pid PID of the out-of-order message.
982 get_recv_bitmask (struct CadetEncryptedMessageIdentifier last_pid_recv,
983 struct CadetEncryptedMessageIdentifier ooo_pid)
985 // FIXME: should assert that the delta is in range...
986 return 1 << (ntohl (last_pid_recv.pid) - ntohl (ooo_pid.pid));
991 * Check is an out-of-order message is ok:
992 * - at most 31 messages behind.
995 * @param last_pid_recv Last in-order PID received.
998 is_ooo_ok (struct CadetEncryptedMessageIdentifier last_pid_recv,
999 struct CadetEncryptedMessageIdentifier ooo_pid,
1000 uint32_t ooo_bitmap)
1004 if (GC_is_pid_bigger (ntohl (last_pid_recv.pid) - 31,
1005 ntohl (ooo_pid.pid)))
1008 mask = get_recv_bitmask (last_pid_recv,
1010 if (0 != (ooo_bitmap & mask))
1018 * Is traffic coming from this sender 'FWD' traffic?
1020 * @param c Connection to check.
1021 * @param sender Short peer identity of neighbor.
1023 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
1024 * the traffic is 'FWD'.
1025 * #GNUNET_NO for BCK.
1026 * #GNUNET_SYSERR for errors (sender isn't a hop in the connection).
1029 is_fwd (const struct CadetConnection *c,
1030 const struct CadetPeer *sender)
1034 id = GCP_get_short_id (sender);
1035 if (GCP_get_short_id (get_prev_hop (c)) == id)
1038 if (GCP_get_short_id (get_next_hop (c)) == id)
1041 return GNUNET_SYSERR;
1046 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1047 * or a first CONNECTION_ACK directed to us.
1049 * @param c Connection to confirm.
1050 * @param fwd Should we send it FWD? (root->dest)
1051 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
1054 send_connection_ack (struct CadetConnection *c, int fwd)
1056 static struct CadetEncryptedMessageIdentifier zero;
1057 struct GNUNET_CADET_ConnectionCreateAckMessage msg;
1058 struct CadetTunnel *t;
1059 const uint16_t size = sizeof (struct GNUNET_CADET_ConnectionCreateAckMessage);
1060 const uint16_t type = GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK;
1062 GCC_check_connections ();
1064 LOG (GNUNET_ERROR_TYPE_INFO,
1065 "==> %s ({ C %s ACK} 0) on conn %s (%p) %s [%5u]\n",
1066 GC_m2s (type), GC_f2s (!fwd), GCC_2s (c), c, GC_f2s (fwd), size);
1068 msg.header.size = htons (size);
1069 msg.header.type = htons (type);
1070 msg.reserved = htonl (0);
1073 GNUNET_assert (NULL == c->maintenance_q);
1074 c->maintenance_q = GCP_send (get_hop (c, fwd),
1076 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
1080 &conn_message_sent, NULL);
1081 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (conn`ACK)\n",
1082 c, c->pending_messages);
1083 c->pending_messages++;
1085 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
1086 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
1087 if (CADET_CONNECTION_READY != c->state)
1088 connection_change_state (c, CADET_CONNECTION_SENT);
1089 GCC_check_connections ();
1094 * Send a notification that a connection is broken.
1096 * @param c Connection that is broken.
1097 * @param id1 Peer that has disconnected.
1098 * @param id2 Peer that has disconnected.
1099 * @param fwd Direction towards which to send it.
1102 send_broken (struct CadetConnection *c,
1103 const struct GNUNET_PeerIdentity *id1,
1104 const struct GNUNET_PeerIdentity *id2,
1107 static struct CadetEncryptedMessageIdentifier zero;
1108 struct GNUNET_CADET_ConnectionBrokenMessage msg;
1110 GCC_check_connections ();
1111 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBrokenMessage));
1112 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1114 msg.reserved = htonl (0);
1117 (void) GCC_send_prebuilt_message (&msg.header,
1124 GCC_check_connections ();
1129 * Send a notification that a connection is broken, when a connection
1130 * isn't even known to the local peer or soon to be destroyed.
1132 * @param connection_id Connection ID.
1133 * @param id1 Peer that has disconnected, probably local peer.
1134 * @param id2 Peer that has disconnected can be NULL if unknown.
1135 * @param neighbor Peer to notify (neighbor who sent the connection).
1138 send_broken_unknown (const struct GNUNET_CADET_ConnectionTunnelIdentifier *connection_id,
1139 const struct GNUNET_PeerIdentity *id1,
1140 const struct GNUNET_PeerIdentity *id2,
1141 struct CadetPeer *neighbor)
1143 static struct CadetEncryptedMessageIdentifier zero;
1144 struct GNUNET_CADET_ConnectionBrokenMessage msg;
1146 GCC_check_connections ();
1147 LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n",
1148 GNUNET_sh2s (&connection_id->connection_of_tunnel));
1150 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBrokenMessage));
1151 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1152 msg.cid = *connection_id;
1153 msg.reserved = htonl (0);
1158 memset (&msg.peer2, 0, sizeof (msg.peer2));
1159 GNUNET_assert (NULL != GCP_send (neighbor,
1164 GNUNET_SYSERR, /* connection, fwd */
1165 NULL, NULL)); /* continuation */
1166 GCC_check_connections ();
1171 * Send keepalive packets for a connection.
1173 * @param c Connection to keep alive..
1174 * @param fwd Is this a FWD keepalive? (owner -> dest).
1177 send_connection_keepalive (struct CadetConnection *c, int fwd)
1179 struct GNUNET_MessageHeader msg;
1180 struct CadetFlowControl *fc;
1183 GCC_check_connections ();
1184 LOG (GNUNET_ERROR_TYPE_INFO,
1185 "keepalive %s for connection %s\n",
1186 GC_f2s (fwd), GCC_2s (c));
1188 GNUNET_assert (NULL != c->t);
1189 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1190 tunnel_ready = GNUNET_YES == GCT_has_queued_traffic (c->t)
1191 && CADET_TUNNEL_KEY_OK <= GCT_get_estate (c->t);
1192 if (0 < fc->queue_n || tunnel_ready)
1194 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1198 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1200 GNUNET_assert (NULL != c->t);
1201 msg.size = htons (sizeof (msg));
1202 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE);
1204 GNUNET_assert (NULL ==
1205 GCT_send_prebuilt_message (&msg, c->t, c,
1206 GNUNET_NO, NULL, NULL));
1207 GCC_check_connections ();
1212 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1214 * @param c Connection for which to send the message.
1215 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1218 connection_recreate (struct CadetConnection *c, int fwd)
1220 LOG (GNUNET_ERROR_TYPE_DEBUG,
1221 "sending connection recreate\n");
1223 GCC_send_create (c);
1225 send_connection_ack (c, GNUNET_NO);
1230 * Generic connection timer management.
1231 * Depending on the role of the peer in the connection will send the
1232 * appropriate message (build or keepalive)
1234 * @param c Conncetion to maintain.
1235 * @param fwd Is FWD?
1238 connection_maintain (struct CadetConnection *c, int fwd)
1240 if (GNUNET_NO != c->destroy)
1242 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1249 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1253 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1255 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1257 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1258 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1259 schedule_next_keepalive (c, fwd);
1264 case CADET_CONNECTION_NEW:
1267 case CADET_CONNECTION_SENT:
1268 connection_recreate (c, fwd);
1270 case CADET_CONNECTION_READY:
1271 send_connection_keepalive (c, fwd);
1280 * Keep the connection alive.
1282 * @param c Connection to keep alive.
1283 * @param fwd Direction.
1286 connection_keepalive (struct CadetConnection *c,
1289 GCC_check_connections ();
1290 LOG (GNUNET_ERROR_TYPE_DEBUG,
1291 "%s keepalive for %s\n",
1292 GC_f2s (fwd), GCC_2s (c));
1295 c->fwd_maintenance_task = NULL;
1297 c->bck_maintenance_task = NULL;
1298 connection_maintain (c, fwd);
1299 GCC_check_connections ();
1300 /* Next execution will be scheduled by message_sent or _maintain*/
1305 * Keep the connection alive in the FWD direction.
1307 * @param cls Closure (connection to keepalive).
1310 connection_fwd_keepalive (void *cls)
1312 struct CadetConnection *c = cls;
1314 GCC_check_connections ();
1315 connection_keepalive (c,
1317 GCC_check_connections ();
1322 * Keep the connection alive in the BCK direction.
1324 * @param cls Closure (connection to keepalive).
1327 connection_bck_keepalive (void *cls)
1329 struct CadetConnection *c = cls;
1331 GCC_check_connections ();
1332 connection_keepalive (c,
1334 GCC_check_connections ();
1339 * Schedule next keepalive task, taking in consideration
1340 * the connection state and number of retries.
1342 * If the peer is not the origin, do nothing.
1344 * @param c Connection for which to schedule the next keepalive.
1345 * @param fwd Direction for the next keepalive.
1348 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1350 struct GNUNET_TIME_Relative delay;
1351 struct GNUNET_SCHEDULER_Task * *task_id;
1352 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1354 GCC_check_connections ();
1355 if (GNUNET_NO == GCC_is_origin (c, fwd))
1358 /* Calculate delay to use, depending on the state of the connection */
1359 if (CADET_CONNECTION_READY == c->state)
1361 delay = refresh_connection_time;
1365 if (1 > c->create_retry)
1366 c->create_retry = 1;
1367 delay = GNUNET_TIME_relative_saturating_multiply (create_connection_time,
1369 if (c->create_retry < 64) // TODO make configurable
1370 c->create_retry *= 2;
1373 /* Select direction-dependent parameters */
1374 if (GNUNET_YES == fwd)
1376 task_id = &c->fwd_maintenance_task;
1377 keepalive_task = &connection_fwd_keepalive;
1381 task_id = &c->bck_maintenance_task;
1382 keepalive_task = &connection_bck_keepalive;
1385 /* Check that no one scheduled it before us */
1386 if (NULL != *task_id)
1388 /* No need for a _break. It can happen for instance when sending a SYNACK
1389 * for a duplicate SYN: the first SYNACK scheduled the task. */
1390 GNUNET_SCHEDULER_cancel (*task_id);
1393 /* Schedule the task */
1394 *task_id = GNUNET_SCHEDULER_add_delayed (delay,
1397 LOG (GNUNET_ERROR_TYPE_INFO,
1398 "next keepalive for %s in in %s\n",
1399 GCC_2s (c), GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1400 GCC_check_connections ();
1405 * Cancel all transmissions that belong to a certain connection.
1407 * If the connection is scheduled for destruction and no more messages are left,
1408 * the connection will be destroyed by the continuation call.
1410 * @param c Connection which to cancel. Might be destroyed during this call.
1411 * @param fwd Cancel fwd traffic?
1414 connection_cancel_queues (struct CadetConnection *c,
1417 struct CadetFlowControl *fc;
1419 GCC_check_connections ();
1420 LOG (GNUNET_ERROR_TYPE_DEBUG,
1421 "Cancel %s queues for connection %s\n",
1422 GC_f2s (fwd), GCC_2s (c));
1429 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1430 if (NULL != fc->poll_task)
1432 GNUNET_SCHEDULER_cancel (fc->poll_task);
1433 fc->poll_task = NULL;
1434 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL task for fc %p\n", fc);
1436 if (NULL != fc->poll_msg)
1438 GCC_cancel (fc->poll_msg);
1439 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL msg for fc %p\n", fc);
1442 while (NULL != fc->q_head)
1444 GCC_cancel (fc->q_head);
1446 GCC_check_connections ();
1451 * Function called if a connection has been stalled for a while,
1452 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1454 * @param cls Closure (poll ctx).
1457 send_poll (void *cls)
1459 static struct CadetEncryptedMessageIdentifier zero;
1460 struct CadetFlowControl *fc = cls;
1461 struct GNUNET_CADET_ConnectionHopByHopPollMessage msg;
1462 struct CadetConnection *c;
1465 fc->poll_task = NULL;
1466 GCC_check_connections ();
1468 fwd = fc == &c->fwd_fc;
1469 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1470 GCC_2s (c), GC_f2s (fwd));
1472 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL);
1473 msg.header.size = htons (sizeof (msg));
1475 msg.cemi = fc->last_pid_sent;
1476 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", ntohl (fc->last_pid_sent.pid));
1478 = GCC_send_prebuilt_message (&msg.header,
1486 GNUNET_assert (NULL != fc->poll_msg);
1487 GCC_check_connections ();
1492 * Generic connection timeout implementation.
1494 * Timeout function due to lack of keepalive/traffic from an endpoint.
1495 * Destroys connection if called.
1497 * @param c Connection to destroy.
1498 * @param fwd Was the timeout from the origin? (FWD timeout)
1501 connection_timeout (struct CadetConnection *c, int fwd)
1503 GCC_check_connections ();
1505 LOG (GNUNET_ERROR_TYPE_INFO,
1506 "Connection %s %s timed out. Destroying.\n",
1509 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1511 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1517 /* If dest, send "broken" notification. */
1518 if (GCC_is_terminal (c, fwd))
1520 struct CadetPeer *next_hop;
1522 next_hop = fwd ? get_prev_hop (c) : get_next_hop (c);
1523 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1527 GCC_check_connections ();
1532 * Timeout function due to lack of keepalive/traffic from the owner.
1533 * Destroys connection if called.
1535 * @param cls Closure (connection to destroy).
1538 connection_fwd_timeout (void *cls)
1540 struct CadetConnection *c = cls;
1542 c->fwd_maintenance_task = NULL;
1543 GCC_check_connections ();
1544 connection_timeout (c, GNUNET_YES);
1545 GCC_check_connections ();
1550 * Timeout function due to lack of keepalive/traffic from the destination.
1551 * Destroys connection if called.
1553 * @param cls Closure (connection to destroy).
1556 connection_bck_timeout (void *cls)
1558 struct CadetConnection *c = cls;
1560 c->bck_maintenance_task = NULL;
1561 GCC_check_connections ();
1562 connection_timeout (c, GNUNET_NO);
1563 GCC_check_connections ();
1568 * Resets the connection timeout task, some other message has done the
1570 * - For the first peer on the direction this means to send
1571 * a keepalive or a path confirmation message (either create or ACK).
1572 * - For all other peers, this means to destroy the connection,
1573 * due to lack of activity.
1574 * Starts the timeout if no timeout was running (connection just created).
1576 * @param c Connection whose timeout to reset.
1577 * @param fwd Is this forward?
1579 * TODO use heap to improve efficiency of scheduler.
1582 connection_reset_timeout (struct CadetConnection *c, int fwd)
1584 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1585 if (GCC_is_origin (c, fwd)) /* Startpoint */
1587 schedule_next_keepalive (c, fwd);
1588 if (NULL != c->maintenance_q)
1590 GCP_send_cancel (c->maintenance_q);
1591 c->maintenance_q = NULL; /* Is set to NULL by conn_message_sent anyway */
1594 else /* Relay, endpoint. */
1596 struct GNUNET_TIME_Relative delay;
1597 struct GNUNET_SCHEDULER_Task * *ti;
1598 GNUNET_SCHEDULER_TaskCallback f;
1600 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1603 GNUNET_SCHEDULER_cancel (*ti);
1604 delay = GNUNET_TIME_relative_saturating_multiply (refresh_connection_time, 4);
1605 LOG (GNUNET_ERROR_TYPE_DEBUG,
1606 " timing out in %s\n",
1607 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1608 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1609 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1615 * Iterator to compare each connection's path with the path of a new connection.
1617 * If the connection coincides, the c member of path is set to the connection
1618 * and the destroy flag of the connection is set.
1620 * @param cls Closure (new path).
1621 * @param c Connection in the tunnel to check.
1624 check_path (void *cls, struct CadetConnection *c)
1626 struct CadetConnection *new_conn = cls;
1627 struct CadetPeerPath *path = new_conn->path;
1629 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1630 GCC_2s (c), c, c->path->length);
1633 && GNUNET_NO == c->destroy
1634 && CADET_CONNECTION_BROKEN != c->state
1635 && CADET_CONNECTION_DESTROYED != c->state
1636 && path_equivalent (path, c->path))
1638 new_conn->destroy = GNUNET_YES; /* Do not mark_destroyed, */
1639 new_conn->path->c = c; /* this is only a flag for the Iterator. */
1640 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1646 * Finds out if this path is already being used by an existing connection.
1648 * Checks the tunnel towards the destination to see if it contains
1649 * any connection with the same path.
1651 * If the existing connection is ready, it is kept.
1652 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1653 * the peer will eventually reject our attempt).
1655 * @param path Path to check.
1656 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1657 * #GNUNET_NO otherwise.
1660 does_connection_exist (struct CadetConnection *conn)
1662 struct CadetPeer *p;
1663 struct CadetTunnel *t;
1664 struct CadetConnection *c;
1666 p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
1669 t = GCP_get_tunnel (p);
1673 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1675 GCT_iterate_connections (t, &check_path, conn);
1677 if (GNUNET_YES == conn->destroy)
1680 conn->destroy = GNUNET_NO;
1681 conn->path->c = conn;
1682 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1683 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1684 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1685 if (CADET_CONNECTION_READY == c->state)
1687 /* The other peer confirmed a live connection with this path,
1688 * why are they trying to duplicate it? */
1689 GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
1692 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not ready, connection unique\n");
1697 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1704 * @brief Check if the tunnel this connection belongs to has any other
1705 * connection with the same path, and destroy one if so.
1707 * @param cls Closure (connection to check).
1710 check_duplicates (void *cls)
1712 struct CadetConnection *c = cls;
1714 c->check_duplicates_task = NULL;
1715 if (GNUNET_YES == does_connection_exist (c))
1717 GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1718 send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
1725 * Wait for enough time to let any dead connections time out and check for
1726 * any remaining duplicates.
1728 * @param c Connection that is a potential duplicate.
1731 schedule_check_duplicates (struct CadetConnection *c)
1733 struct GNUNET_TIME_Relative delay;
1735 if (NULL != c->check_duplicates_task)
1737 delay = GNUNET_TIME_relative_saturating_multiply (refresh_connection_time, 5);
1738 c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
1745 * Add the connection to the list of both neighbors.
1747 * @param c Connection.
1749 * @return #GNUNET_OK if everything went fine
1750 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1753 register_neighbors (struct CadetConnection *c)
1755 c->next_peer = get_next_hop (c);
1756 c->prev_peer = get_prev_hop (c);
1757 GNUNET_assert (c->next_peer != c->prev_peer);
1758 LOG (GNUNET_ERROR_TYPE_DEBUG,
1759 "register neighbors for connection %s\n",
1761 path_debug (c->path);
1762 LOG (GNUNET_ERROR_TYPE_DEBUG,
1763 "own pos %u\n", c->own_pos);
1764 LOG (GNUNET_ERROR_TYPE_DEBUG,
1765 "putting connection %s to next peer %p\n",
1768 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1770 GCP_2s (c->next_peer));
1771 LOG (GNUNET_ERROR_TYPE_DEBUG,
1772 "putting connection %s to prev peer %p\n",
1775 LOG (GNUNET_ERROR_TYPE_DEBUG,
1776 "prev peer %p %s\n",
1778 GCP_2s (c->prev_peer));
1780 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1781 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1783 if (GCC_is_origin (c, GNUNET_YES))
1784 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1785 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1787 LOG (GNUNET_ERROR_TYPE_DEBUG,
1788 " register neighbors failed\n");
1789 LOG (GNUNET_ERROR_TYPE_DEBUG,
1790 " prev: %s, neighbor?: %d\n",
1791 GCP_2s (c->prev_peer),
1792 GCP_is_neighbor (c->prev_peer));
1793 LOG (GNUNET_ERROR_TYPE_DEBUG,
1794 " next: %s, neighbor?: %d\n",
1795 GCP_2s (c->next_peer),
1796 GCP_is_neighbor (c->next_peer));
1797 return GNUNET_SYSERR;
1799 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1800 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1807 * Remove the connection from the list of both neighbors.
1809 * @param c Connection.
1812 unregister_neighbors (struct CadetConnection *c)
1814 // struct CadetPeer *peer; FIXME dont use next_peer, prev_peer
1815 /* Either already unregistered or never got registered, it's ok either way. */
1816 if (NULL == c->path)
1818 if (NULL != c->next_peer)
1820 GCP_remove_connection (c->next_peer, c);
1821 c->next_peer = NULL;
1823 if (NULL != c->prev_peer)
1825 GCP_remove_connection (c->prev_peer, c);
1826 c->prev_peer = NULL;
1832 * Invalidates all paths towards all peers that comprise the connection which
1833 * rely on the disconnected peer.
1835 * ~O(n^3) (peers in connection * paths/peer * links/path)
1837 * @param c Connection whose peers' paths to clean.
1838 * @param disconnected Peer that disconnected.
1841 invalidate_paths (struct CadetConnection *c,
1842 struct CadetPeer *disconnected)
1844 struct CadetPeer *peer;
1847 for (i = 0; i < c->path->length; i++)
1849 peer = GCP_get_short (c->path->peers[i], GNUNET_NO);
1851 GCP_notify_broken_link (peer, &my_full_id, GCP_get_id (disconnected));
1857 * Bind the connection to the peer and the tunnel to that peer.
1859 * If the peer has no tunnel, create one. Update tunnel and connection
1860 * data structres to reflect new status.
1862 * @param c Connection.
1866 add_to_peer (struct CadetConnection *c,
1867 struct CadetPeer *peer)
1869 GCP_add_tunnel (peer);
1870 c->t = GCP_get_tunnel (peer);
1871 GCT_add_connection (c->t, c);
1876 * Log receipt of message on stderr (INFO level).
1878 * @param message Message received.
1879 * @param peer Peer who sent the message.
1880 * @param conn_id Connection ID of the message.
1883 log_message (const struct GNUNET_MessageHeader *message,
1884 const struct CadetPeer *peer,
1885 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conn_id)
1891 size = ntohs (message->size);
1892 type = ntohs (message->type);
1895 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1896 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK:
1897 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1898 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1904 LOG (GNUNET_ERROR_TYPE_INFO,
1905 "<%s %s on conn %s from %s, %6u bytes\n",
1908 GNUNET_sh2s (&conn_id->connection_of_tunnel),
1910 (unsigned int) size);
1913 /******************************************************************************/
1914 /******************************** API ***********************************/
1915 /******************************************************************************/
1918 * Handler for connection creation.
1920 * @param peer Message sender (neighbor).
1921 * @param msg Message itself.
1924 GCC_handle_create (struct CadetPeer *peer,
1925 const struct GNUNET_CADET_ConnectionCreateMessage *msg)
1927 static struct CadetEncryptedMessageIdentifier zero;
1928 const struct GNUNET_CADET_ConnectionTunnelIdentifier *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_ConnectionCreateMessage);
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 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2048 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2049 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2050 (void) GCC_send_prebuilt_message (&msg->header,
2054 GNUNET_YES, GNUNET_YES,
2057 path_destroy (path);
2058 GCC_check_connections ();
2063 * Handler for connection confirmations.
2065 * @param peer Message sender (neighbor).
2066 * @param msg Message itself.
2069 GCC_handle_confirm (struct CadetPeer *peer,
2070 const struct GNUNET_CADET_ConnectionCreateAckMessage *msg)
2072 static struct CadetEncryptedMessageIdentifier zero;
2073 struct CadetConnection *c;
2074 enum CadetConnectionState oldstate;
2077 GCC_check_connections ();
2078 log_message (&msg->header, peer, &msg->cid);
2079 c = connection_get (&msg->cid);
2082 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2084 LOG (GNUNET_ERROR_TYPE_DEBUG,
2085 " don't know the connection!\n");
2086 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2087 GCC_check_connections ();
2090 if (GNUNET_NO != c->destroy)
2092 GNUNET_assert (CADET_CONNECTION_DESTROYED == c->state);
2093 GNUNET_STATISTICS_update (stats, "# control on dying connection",
2095 LOG (GNUNET_ERROR_TYPE_DEBUG,
2096 "connection %s being destroyed, ignoring confirm\n",
2098 GCC_check_connections ();
2102 oldstate = c->state;
2103 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GCP_2s (peer));
2104 if (get_next_hop (c) == peer)
2106 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2108 if (CADET_CONNECTION_SENT == oldstate)
2109 connection_change_state (c, CADET_CONNECTION_ACK);
2111 else if (get_prev_hop (c) == peer)
2113 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2115 connection_change_state (c, CADET_CONNECTION_READY);
2119 GNUNET_STATISTICS_update (stats, "# control on connection from wrong peer",
2121 GNUNET_break_op (0);
2125 connection_reset_timeout (c, fwd);
2127 GNUNET_assert (NULL != c->path);
2128 GCP_add_path_to_all (c->path, GNUNET_YES);
2130 /* Message for us as creator? */
2131 if (GNUNET_YES == GCC_is_origin (c, GNUNET_YES))
2133 if (GNUNET_NO != fwd)
2138 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2140 /* If just created, cancel the short timeout and start a long one */
2141 if (CADET_CONNECTION_SENT == oldstate)
2143 c->create_retry = 1;
2144 connection_reset_timeout (c, GNUNET_YES);
2147 /* Change connection state, send ACK */
2148 connection_change_state (c, CADET_CONNECTION_READY);
2149 send_connection_ack (c, GNUNET_YES);
2151 /* Change tunnel state, trigger KX */
2152 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2153 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2154 GCC_check_connections ();
2158 /* Message for us as destination? */
2159 if (GCC_is_terminal (c, GNUNET_YES))
2161 if (GNUNET_YES != fwd)
2166 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2168 /* If just created, cancel the short timeout and start a long one */
2169 if (CADET_CONNECTION_ACK == oldstate)
2170 connection_reset_timeout (c, GNUNET_NO);
2172 /* Change tunnel state */
2173 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2174 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2175 GCC_check_connections ();
2179 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2180 (void) GCC_send_prebuilt_message (&msg->header, 0,
2184 GNUNET_YES, NULL, NULL);
2186 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_ConnectionBrokenMessage *msg)
2200 static struct CadetEncryptedMessageIdentifier zero;
2201 struct CadetConnection *c;
2202 struct CadetTunnel *t;
2205 GCC_check_connections ();
2206 log_message (&msg->header, peer, &msg->cid);
2207 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", GNUNET_i2s (&msg->peer1));
2208 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", GNUNET_i2s (&msg->peer2));
2209 c = connection_get (&msg->cid);
2212 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2213 GNUNET_STATISTICS_update (stats, "# duplicate CONNECTION_BROKEN",
2215 GCC_check_connections ();
2221 fwd = is_fwd (c, peer);
2222 if (GNUNET_SYSERR == fwd)
2224 GNUNET_break_op (0);
2225 GCC_check_connections ();
2229 if (GCC_is_terminal (c, fwd))
2231 struct CadetPeer *endpoint;
2235 /* A terminal connection should not have 't' set to NULL. */
2237 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2240 endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
2241 if (2 < c->path->length)
2242 path_invalidate (c->path);
2243 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2245 connection_change_state (c, CADET_CONNECTION_BROKEN);
2246 GCT_remove_connection (t, c);
2253 (void) GCC_send_prebuilt_message (&msg->header, 0,
2255 GNUNET_YES, NULL, NULL);
2256 connection_cancel_queues (c, !fwd);
2258 GCC_check_connections ();
2264 * Handler for notifications of destroyed connections.
2266 * @param peer Message sender (neighbor).
2267 * @param msg Message itself.
2270 GCC_handle_destroy (struct CadetPeer *peer,
2271 const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
2273 static struct CadetEncryptedMessageIdentifier zero;
2274 struct CadetConnection *c;
2277 GCC_check_connections ();
2278 log_message (&msg->header, peer, &msg->cid);
2279 c = connection_get (&msg->cid);
2282 /* Probably already got the message from another path,
2283 * destroyed the tunnel and retransmitted to children.
2286 GNUNET_STATISTICS_update (stats,
2287 "# control on unknown connection",
2289 LOG (GNUNET_ERROR_TYPE_DEBUG,
2290 " connection unknown destroyed: previously destroyed?\n");
2291 GCC_check_connections ();
2295 fwd = is_fwd (c, peer);
2296 if (GNUNET_SYSERR == fwd)
2298 GNUNET_break_op (0);
2299 GCC_check_connections ();
2303 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2305 (void) GCC_send_prebuilt_message (&msg->header, 0,
2307 GNUNET_YES, NULL, NULL);
2309 else if (0 == c->pending_messages)
2311 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
2313 GCC_check_connections ();
2319 GCT_remove_connection (c->t, c);
2322 GCC_check_connections ();
2328 * Handler for cadet network traffic hop-by-hop acks.
2330 * @param peer Message sender (neighbor).
2331 * @param msg Message itself.
2334 GCC_handle_ack (struct CadetPeer *peer,
2335 const struct GNUNET_CADET_ConnectionEncryptedAckMessage *msg)
2337 struct CadetConnection *c;
2338 struct CadetFlowControl *fc;
2339 struct CadetEncryptedMessageIdentifier ack;
2342 GCC_check_connections ();
2343 log_message (&msg->header, peer, &msg->cid);
2344 c = connection_get (&msg->cid);
2347 GNUNET_STATISTICS_update (stats,
2348 "# ack on unknown connection",
2351 send_broken_unknown (&msg->cid,
2355 GCC_check_connections ();
2359 /* Is this a forward or backward ACK? */
2360 if (get_next_hop (c) == peer)
2365 else if (get_prev_hop (c) == peer)
2372 GNUNET_break_op (0);
2376 ack = msg->cemi_max;
2377 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2380 ntohl (fc->last_ack_recv.pid));
2381 if (GC_is_pid_bigger (ntohl (ack.pid),
2382 ntohl (fc->last_ack_recv.pid)))
2383 fc->last_ack_recv = ack;
2385 /* Cancel polling if the ACK is big enough. */
2386 if ( (NULL != fc->poll_task) &
2387 GC_is_pid_bigger (ntohl (fc->last_ack_recv.pid),
2388 ntohl (fc->last_pid_sent.pid)))
2390 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2391 GNUNET_SCHEDULER_cancel (fc->poll_task);
2392 fc->poll_task = NULL;
2393 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2396 GCC_check_connections ();
2401 * Handler for cadet network traffic hop-by-hop data counter polls.
2403 * @param peer Message sender (neighbor).
2404 * @param msg Message itself.
2407 GCC_handle_poll (struct CadetPeer *peer,
2408 const struct GNUNET_CADET_ConnectionHopByHopPollMessage *msg)
2410 struct CadetConnection *c;
2411 struct CadetFlowControl *fc;
2412 struct CadetEncryptedMessageIdentifier pid;
2415 GCC_check_connections ();
2416 log_message (&msg->header, peer, &msg->cid);
2417 c = connection_get (&msg->cid);
2420 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2422 LOG (GNUNET_ERROR_TYPE_DEBUG,
2423 "POLL message on unknown connection %s!\n",
2424 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
2425 send_broken_unknown (&msg->cid,
2429 GCC_check_connections ();
2433 /* Is this a forward or backward ACK?
2434 * Note: a poll should never be needed in a loopback case,
2435 * since there is no possiblility of packet loss there, so
2436 * this way of discerining FWD/BCK should not be a problem.
2438 if (get_next_hop (c) == peer)
2440 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2443 else if (get_prev_hop (c) == peer)
2445 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2450 GNUNET_break_op (0);
2455 LOG (GNUNET_ERROR_TYPE_DEBUG,
2456 " PID %u, OLD %u\n",
2458 ntohl (fc->last_pid_recv.pid));
2459 fc->last_pid_recv = pid;
2460 fwd = fc == &c->bck_fc;
2461 GCC_send_ack (c, fwd, GNUNET_YES);
2462 GCC_check_connections ();
2467 * Check the message against internal state and test if it goes FWD or BCK.
2469 * Updates the PID, state and timeout values for the connection.
2471 * @param message Message to check. It must belong to an existing connection.
2472 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2473 * @param c Connection this message should belong. If NULL, check fails.
2474 * @param sender Neighbor that sent the message.
2476 * @return #GNUNET_YES if the message goes FWD.
2477 * #GNUNET_NO if it goes BCK.
2478 * #GNUNET_SYSERR if there is an error (unauthorized sender, ...).
2481 check_message (const struct GNUNET_MessageHeader *message,
2482 const struct GNUNET_CADET_ConnectionTunnelIdentifier* cid,
2483 struct CadetConnection *c,
2484 struct CadetPeer *sender,
2485 struct CadetEncryptedMessageIdentifier pid)
2487 struct CadetFlowControl *fc;
2488 struct CadetPeer *hop;
2492 /* Check connection */
2495 GNUNET_STATISTICS_update (stats,
2496 "# unknown connection",
2498 LOG (GNUNET_ERROR_TYPE_DEBUG,
2499 "%s on unknown connection %s\n",
2500 GC_m2s (ntohs (message->type)),
2501 GNUNET_sh2s (&cid->connection_of_tunnel));
2502 GNUNET_break_op (0);
2503 send_broken_unknown (cid,
2507 return GNUNET_SYSERR;
2510 /* Check if origin is as expected */
2511 hop = get_prev_hop (c);
2518 hop = get_next_hop (c);
2519 GNUNET_break (hop == c->next_peer);
2526 /* Unexpected peer sending traffic on a connection. */
2527 GNUNET_break_op (0);
2528 return GNUNET_SYSERR;
2532 /* Check PID for payload messages */
2533 type = ntohs (message->type);
2534 if (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED == type)
2536 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2537 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected in interval [%u,%u])\n",
2539 ntohl (fc->last_pid_recv.pid) + 1,
2540 ntohl (fc->last_ack_sent.pid));
2541 if (GC_is_pid_bigger (ntohl (pid.pid),
2542 ntohl (fc->last_ack_sent.pid)))
2544 GNUNET_STATISTICS_update (stats,
2545 "# unsolicited message",
2548 LOG (GNUNET_ERROR_TYPE_WARNING,
2549 "Received PID %u, (prev %u), ACK %u\n",
2550 pid, fc->last_pid_recv, fc->last_ack_sent);
2551 return GNUNET_SYSERR;
2553 if (GC_is_pid_bigger (ntohl (pid.pid),
2554 ntohl (fc->last_pid_recv.pid)))
2558 delta = ntohl (pid.pid) - ntohl (fc->last_pid_recv.pid);
2559 fc->last_pid_recv = pid;
2560 fc->recv_bitmap <<= delta;
2561 fc->recv_bitmap |= 1;
2565 GNUNET_STATISTICS_update (stats,
2566 "# out of order PID",
2569 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv,
2573 LOG (GNUNET_ERROR_TYPE_WARNING,
2574 "PID %u unexpected (%u+), dropping!\n",
2576 ntohl (fc->last_pid_recv.pid) - 31);
2577 return GNUNET_SYSERR;
2579 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv,
2584 /* Count as connection confirmation. */
2585 if ( (CADET_CONNECTION_SENT == c->state) ||
2586 (CADET_CONNECTION_ACK == c->state) )
2588 connection_change_state (c, CADET_CONNECTION_READY);
2591 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2592 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2595 connection_reset_timeout (c, fwd);
2602 * Handler for key exchange traffic (Axolotl KX).
2604 * @param peer Message sender (neighbor).
2605 * @param msg Message itself.
2608 GCC_handle_kx (struct CadetPeer *peer,
2609 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
2611 static struct CadetEncryptedMessageIdentifier zero;
2612 const struct GNUNET_CADET_ConnectionTunnelIdentifier* cid;
2613 struct CadetConnection *c;
2616 GCC_check_connections ();
2618 log_message (&msg->header, peer, cid);
2620 c = connection_get (cid);
2621 fwd = check_message (&msg->header,
2627 /* If something went wrong, discard message. */
2628 if (GNUNET_SYSERR == fwd)
2630 GNUNET_break_op (0);
2631 GCC_check_connections ();
2635 /* Is this message for us? */
2636 if (GCC_is_terminal (c, fwd))
2638 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2639 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2645 GCT_handle_kx (c->t, msg);
2646 GCC_check_connections ();
2650 /* Message not for us: forward to next hop */
2651 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2652 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2653 (void) GCC_send_prebuilt_message (&msg->header, 0,
2655 GNUNET_NO, NULL, NULL);
2656 GCC_check_connections ();
2661 * Handler for encrypted cadet network traffic (channel mgmt, data).
2663 * @param peer Message sender (neighbor).
2664 * @param msg Message itself.
2667 GCC_handle_encrypted (struct CadetPeer *peer,
2668 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
2670 static struct CadetEncryptedMessageIdentifier zero;
2671 const struct GNUNET_CADET_ConnectionTunnelIdentifier* cid;
2672 struct CadetConnection *c;
2673 struct CadetEncryptedMessageIdentifier pid;
2676 GCC_check_connections ();
2679 log_message (&msg->header, peer, cid);
2681 c = connection_get (cid);
2682 fwd = check_message (&msg->header,
2688 /* If something went wrong, discard message. */
2689 if (GNUNET_SYSERR == fwd)
2691 GCC_check_connections ();
2695 /* Is this message for us? */
2696 if (GCC_is_terminal (c, fwd))
2698 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2702 GNUNET_break (GNUNET_NO != c->destroy);
2705 GCT_handle_encrypted (c->t, msg);
2706 GCC_send_ack (c, fwd, GNUNET_NO);
2707 GCC_check_connections ();
2711 /* Message not for us: forward to next hop */
2712 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2713 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2714 (void) GCC_send_prebuilt_message (&msg->header, 0,
2716 GNUNET_NO, NULL, NULL);
2717 GCC_check_connections ();
2722 * Initialize the connections subsystem
2724 * @param c Configuration handle.
2727 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2729 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2731 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2734 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2735 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2736 GNUNET_SCHEDULER_shutdown ();
2741 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2744 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2745 "CADET", "MAX_CONNECTIONS", "MISSING");
2746 GNUNET_SCHEDULER_shutdown ();
2751 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2752 &refresh_connection_time))
2754 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2755 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2756 GNUNET_SCHEDULER_shutdown ();
2759 create_connection_time = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
2760 refresh_connection_time);
2761 connections = GNUNET_CONTAINER_multishortmap_create (1024,
2767 * Destroy each connection on shutdown.
2769 * @param cls Closure (unused).
2770 * @param key Current key code (CID, unused).
2771 * @param value Value in the hash map (`struct CadetConnection`)
2773 * @return #GNUNET_YES, because we should continue to iterate
2776 shutdown_iterator (void *cls,
2777 const struct GNUNET_ShortHashCode *key,
2780 struct CadetConnection *c = value;
2782 c->state = CADET_CONNECTION_DESTROYED;
2789 * Shut down the connections subsystem.
2794 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n");
2795 GCC_check_connections ();
2796 GNUNET_CONTAINER_multishortmap_iterate (connections,
2799 GNUNET_CONTAINER_multishortmap_destroy (connections);
2805 * Create a connection.
2807 * @param cid Connection ID (either created locally or imposed remotely).
2808 * @param t Tunnel this connection belongs to (or NULL for transit connections);
2809 * @param path Path this connection has to use (copy is made).
2810 * @param own_pos Own position in the @c path path.
2812 * @return Newly created connection.
2813 * NULL in case of error: own id not in path, wrong neighbors, ...
2815 struct CadetConnection *
2816 GCC_new (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
2817 struct CadetTunnel *t,
2818 struct CadetPeerPath *path,
2819 unsigned int own_pos)
2821 struct CadetConnection *c;
2822 struct CadetPeerPath *cpath;
2824 GCC_check_connections ();
2825 cpath = path_duplicate (path);
2826 GNUNET_assert (NULL != cpath);
2827 c = GNUNET_new (struct CadetConnection);
2829 GNUNET_assert (GNUNET_OK ==
2830 GNUNET_CONTAINER_multishortmap_put (connections,
2831 &c->id.connection_of_tunnel,
2833 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2834 fc_init (&c->fwd_fc);
2835 fc_init (&c->bck_fc);
2840 GNUNET_assert (own_pos <= cpath->length - 1);
2841 c->own_pos = own_pos;
2844 if (GNUNET_OK != register_neighbors (c))
2848 /* We were the origin of this request, this means we have invalid
2849 * info about the paths to reach the destination. We must invalidate
2850 * the *original* path to avoid trying it again in the next minute.
2852 if (2 < path->length)
2853 path_invalidate (path);
2857 GCT_debug(t, GNUNET_ERROR_TYPE_WARNING);
2861 path_destroy (c->path);
2866 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2867 GCC_check_connections ();
2873 * Connection is no longer needed: destroy it.
2875 * Cancels all pending traffic (including possible DESTROY messages), all
2876 * maintenance tasks and removes the connection from neighbor peers and tunnel.
2878 * @param c Connection to destroy.
2881 GCC_destroy (struct CadetConnection *c)
2883 GCC_check_connections ();
2890 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2891 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2894 LOG (GNUNET_ERROR_TYPE_DEBUG,
2895 "destroying connection %s\n",
2897 LOG (GNUNET_ERROR_TYPE_DEBUG,
2898 " fc's f: %p, b: %p\n",
2899 &c->fwd_fc, &c->bck_fc);
2900 LOG (GNUNET_ERROR_TYPE_DEBUG,
2901 " fc tasks f: %u, b: %u\n",
2902 c->fwd_fc.poll_task,
2903 c->bck_fc.poll_task);
2905 /* Cancel all traffic */
2906 if (NULL != c->path)
2908 connection_cancel_queues (c, GNUNET_YES);
2909 connection_cancel_queues (c, GNUNET_NO);
2910 if (NULL != c->maintenance_q)
2912 GCP_send_cancel (c->maintenance_q);
2913 c->maintenance_q = NULL;
2916 unregister_neighbors (c);
2917 path_destroy (c->path);
2920 /* Delete from tunnel */
2922 GCT_remove_connection (c->t, c);
2924 if (NULL != c->check_duplicates_task)
2925 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
2926 if (NULL != c->fwd_maintenance_task)
2927 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2928 if (NULL != c->bck_maintenance_task)
2929 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2931 if (GNUNET_NO == c->was_removed)
2933 GNUNET_break (GNUNET_YES ==
2934 GNUNET_CONTAINER_multishortmap_remove (connections,
2935 &c->id.connection_of_tunnel,
2938 GNUNET_STATISTICS_update (stats,
2943 GCC_check_connections ();
2948 * Get the connection ID.
2950 * @param c Connection to get the ID from.
2952 * @return ID of the connection.
2954 const struct GNUNET_CADET_ConnectionTunnelIdentifier *
2955 GCC_get_id (const struct CadetConnection *c)
2962 * Get the connection path.
2964 * @param c Connection to get the path from.
2966 * @return path used by the connection.
2968 const struct CadetPeerPath *
2969 GCC_get_path (const struct CadetConnection *c)
2971 if (GNUNET_NO == c->destroy)
2978 * Get the connection state.
2980 * @param c Connection to get the state from.
2982 * @return state of the connection.
2984 enum CadetConnectionState
2985 GCC_get_state (const struct CadetConnection *c)
2991 * Get the connection tunnel.
2993 * @param c Connection to get the tunnel from.
2995 * @return tunnel of the connection.
2997 struct CadetTunnel *
2998 GCC_get_tunnel (const struct CadetConnection *c)
3005 * Get free buffer space in a connection.
3007 * @param c Connection.
3008 * @param fwd Is query about FWD traffic?
3010 * @return Free buffer space [0 - max_msgs_queue/max_connections]
3013 GCC_get_buffer (struct CadetConnection *c, int fwd)
3015 struct CadetFlowControl *fc;
3017 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3019 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
3020 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
3021 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
3023 return (fc->queue_max - fc->queue_n);
3028 * Get how many messages have we allowed to send to us from a direction.
3030 * @param c Connection.
3031 * @param fwd Are we asking about traffic from FWD (BCK messages)?
3033 * @return last_ack_sent - last_pid_recv
3036 GCC_get_allowed (struct CadetConnection *c, int fwd)
3038 struct CadetFlowControl *fc;
3040 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3041 if ( (CADET_CONNECTION_READY != c->state) ||
3042 GC_is_pid_bigger (ntohl (fc->last_pid_recv.pid),
3043 ntohl (fc->last_ack_sent.pid)) )
3047 return (ntohl (fc->last_ack_sent.pid) - ntohl (fc->last_pid_recv.pid));
3052 * Get messages queued in a connection.
3054 * @param c Connection.
3055 * @param fwd Is query about FWD traffic?
3057 * @return Number of messages queued.
3060 GCC_get_qn (struct CadetConnection *c, int fwd)
3062 struct CadetFlowControl *fc;
3064 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3071 * Get next PID to use.
3073 * @param c Connection.
3074 * @param fwd Is query about FWD traffic?
3075 * @return Next PID to use.
3077 struct CadetEncryptedMessageIdentifier
3078 GCC_get_pid (struct CadetConnection *c, int fwd)
3080 struct CadetFlowControl *fc;
3081 struct CadetEncryptedMessageIdentifier pid;
3083 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3085 fc->next_pid.pid = htonl (1 + ntohl (pid.pid));
3091 * Allow the connection to advertise a buffer of the given size.
3093 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3094 * allowing up to last_pid_recv + buffer.
3096 * @param c Connection.
3097 * @param buffer How many more messages the connection can accept.
3098 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3101 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3103 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3104 GCC_2s (c), buffer, GC_f2s (fwd));
3105 send_ack (c, buffer, fwd, GNUNET_NO);
3110 * Notify other peers on a connection of a broken link. Mark connections
3111 * to destroy after all traffic has been sent.
3113 * @param c Connection on which there has been a disconnection.
3114 * @param peer Peer that disconnected.
3117 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
3119 struct CadetFlowControl *fc;
3123 GCC_check_connections ();
3124 strncpy (peer_name, GCP_2s (peer), 16);
3125 peer_name[15] = '\0';
3126 LOG (GNUNET_ERROR_TYPE_DEBUG,
3127 "shutting down %s, %s disconnected\n",
3128 GCC_2s (c), peer_name);
3130 invalidate_paths (c, peer);
3132 fwd = is_fwd (c, peer);
3133 if (GNUNET_SYSERR == fwd)
3138 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3139 (GNUNET_NO != c->destroy) )
3141 /* Local shutdown, or other peer already down (hence 'c->destroy');
3142 so there is no one to notify about this, just clean up. */
3144 GCC_check_connections ();
3147 /* Mark FlowControl towards the peer as unavaliable. */
3148 fc = fwd ? &c->bck_fc : &c->fwd_fc;
3151 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3153 /* Connection will have at least one pending message
3154 * (the one we just scheduled), so delay destruction
3155 * and remove from map so we don't use accidentally. */
3157 GNUNET_assert (GNUNET_NO == c->was_removed);
3158 c->was_removed = GNUNET_YES;
3159 GNUNET_break (GNUNET_YES ==
3160 GNUNET_CONTAINER_multishortmap_remove (connections,
3161 &c->id.connection_of_tunnel,
3163 /* Cancel queue in the direction that just died. */
3164 connection_cancel_queues (c, ! fwd);
3165 GCC_stop_poll (c, ! fwd);
3166 unregister_neighbors (c);
3167 GCC_check_connections ();
3172 * Is this peer the first one on the connection?
3174 * @param c Connection.
3175 * @param fwd Is this about fwd traffic?
3177 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3180 GCC_is_origin (struct CadetConnection *c, int fwd)
3182 if (!fwd && c->path->length - 1 == c->own_pos )
3184 if (fwd && 0 == c->own_pos)
3191 * Is this peer the last one on the connection?
3193 * @param c Connection.
3194 * @param fwd Is this about fwd traffic?
3195 * Note that the ROOT is the terminal for BCK traffic!
3197 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3200 GCC_is_terminal (struct CadetConnection *c, int fwd)
3202 return GCC_is_origin (c, ! fwd);
3207 * See if we are allowed to send by the next hop in the given direction.
3209 * @param c Connection.
3210 * @param fwd Is this about fwd traffic?
3212 * @return #GNUNET_YES in case it's OK to send.
3215 GCC_is_sendable (struct CadetConnection *c, int fwd)
3217 struct CadetFlowControl *fc;
3219 LOG (GNUNET_ERROR_TYPE_DEBUG,
3220 " checking sendability of %s traffic on %s\n",
3221 GC_f2s (fwd), GCC_2s (c));
3227 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3228 LOG (GNUNET_ERROR_TYPE_DEBUG,
3229 " last ack recv: %u, last pid sent: %u\n",
3230 ntohl (fc->last_ack_recv.pid),
3231 ntohl (fc->last_pid_sent.pid));
3232 if (GC_is_pid_bigger (ntohl (fc->last_ack_recv.pid),
3233 ntohl (fc->last_pid_sent.pid)))
3235 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3238 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3244 * Check if this connection is a direct one (never trim a direct connection).
3246 * @param c Connection.
3248 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3251 GCC_is_direct (struct CadetConnection *c)
3253 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3258 * Sends a completely built message on a connection, properly registering
3259 * all used resources.
3261 * @param message Message to send.
3262 * @param payload_type Type of payload, in case the message is encrypted.
3263 * 0 for restransmissions (when type is no longer known)
3264 * UINT16_MAX when not applicable.
3265 * @param payload_id ID of the payload (PID, ACK, ...).
3266 * @param c Connection on which this message is transmitted.
3267 * @param fwd Is this a fwd message?
3268 * @param force Force the connection to accept the message (buffer overfill).
3269 * @param cont Continuation called once message is sent. Can be NULL.
3270 * @param cont_cls Closure for @c cont.
3272 * @return Handle to cancel the message before it's sent.
3274 * Invalid on @c cont call.
3276 struct CadetConnectionQueue *
3277 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3278 uint16_t payload_type,
3279 struct CadetEncryptedMessageIdentifier payload_id,
3280 struct CadetConnection *c, int fwd, int force,
3281 GCC_sent cont, void *cont_cls)
3283 struct CadetFlowControl *fc;
3284 struct CadetConnectionQueue *q;
3288 size = ntohs (message->size);
3289 type = ntohs (message->type);
3291 GCC_check_connections ();
3292 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3293 if (0 == fc->queue_max)
3299 LOG (GNUNET_ERROR_TYPE_INFO,
3300 "--> %s (%s %4u) on conn %s (%p) %s [%5u]\n",
3301 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), c,
3305 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED:
3306 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, PIDsnt: %u, ACKrcv: %u\n",
3309 ntohl (fc->last_pid_sent.pid),
3310 ntohl (fc->last_ack_recv.pid));
3311 if (GNUNET_NO == force)
3317 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX:
3318 /* nothing to do here */
3321 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3322 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK:
3323 /* Should've only be used for restransmissions. */
3324 GNUNET_break (0 == payload_type);
3327 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK:
3328 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL:
3329 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3330 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3331 GNUNET_assert (GNUNET_YES == force);
3339 if (fc->queue_n > fc->queue_max && GNUNET_NO == force)
3341 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3344 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3345 fc->queue_n, fc->queue_max);
3346 if (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED == type)
3350 return NULL; /* Drop this message */
3353 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3354 GCC_2s (c), c->pending_messages);
3355 c->pending_messages++;
3357 q = GNUNET_new (struct CadetConnectionQueue);
3359 q->cont_cls = cont_cls;
3361 GNUNET_CONTAINER_DLL_insert (fc->q_head, fc->q_tail, q);
3362 q->peer_q = GCP_send (get_hop (c, fwd),
3368 &conn_message_sent, q);
3369 if (NULL == q->peer_q)
3371 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3372 GNUNET_CONTAINER_DLL_remove (fc->q_head, fc->q_tail, q);
3374 GCC_check_connections ();
3377 GCC_check_connections ();
3383 * Cancel a previously sent message while it's in the queue.
3385 * ONLY can be called before the continuation given to the send function
3386 * is called. Once the continuation is called, the message is no longer in the
3389 * @param q Handle to the queue.
3392 GCC_cancel (struct CadetConnectionQueue *q)
3394 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3396 /* send_cancel calls message_sent, which calls q->cont and frees q */
3397 GCP_send_cancel (q->peer_q);
3398 GCC_check_connections ();
3403 * Sends a CREATE CONNECTION message for a path to a peer.
3404 * Changes the connection and tunnel states if necessary.
3406 * @param c Connection to create.
3409 GCC_send_create (struct CadetConnection *c)
3411 static struct CadetEncryptedMessageIdentifier zero;
3412 enum CadetTunnelCState state;
3415 GCC_check_connections ();
3416 size = sizeof (struct GNUNET_CADET_ConnectionCreateMessage);
3417 size += c->path->length * sizeof (struct GNUNET_PeerIdentity);
3419 /* Allocate message on the stack */
3420 unsigned char cbuf[size];
3421 struct GNUNET_CADET_ConnectionCreateMessage *msg;
3422 struct GNUNET_PeerIdentity *peers;
3425 msg = (struct GNUNET_CADET_ConnectionCreateMessage *) cbuf;
3426 msg->header.size = htons (size);
3427 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
3428 msg->options = htonl (0);
3429 msg->cid = *GCC_get_id (c);
3430 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3431 for (int i = 0; i < c->path->length; i++)
3433 GNUNET_PEER_resolve (c->path->peers[i], peers++);
3435 GNUNET_assert (NULL == c->maintenance_q);
3436 c->maintenance_q = GCP_send (get_next_hop (c),
3438 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
3441 &conn_message_sent, NULL);
3444 LOG (GNUNET_ERROR_TYPE_INFO, "==> %s %19s on conn %s (%p) FWD [%5u]\n",
3445 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE), "",
3446 GCC_2s (c), c, size);
3447 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3448 c, c->pending_messages);
3449 c->pending_messages++;
3451 state = GCT_get_cstate (c->t);
3452 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3453 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
3454 if (CADET_CONNECTION_NEW == c->state)
3455 connection_change_state (c, CADET_CONNECTION_SENT);
3456 GCC_check_connections ();
3461 * Send an ACK on the appropriate connection/channel, depending on
3462 * the direction and the position of the peer.
3464 * @param c Which connection to send the hop-by-hop ACK.
3465 * @param fwd Is this a fwd ACK? (will go dest->root).
3466 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
3469 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
3471 unsigned int buffer;
3473 GCC_check_connections ();
3474 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
3475 GC_f2s (fwd), GCC_2s (c));
3483 if (GNUNET_NO != c->destroy)
3485 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
3486 GCC_check_connections ();
3490 /* Get available buffer space */
3491 if (GCC_is_terminal (c, fwd))
3493 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
3494 buffer = GCT_get_channels_buffer (c->t);
3498 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
3499 buffer = GCC_get_buffer (c, fwd);
3501 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
3502 if (0 == buffer && GNUNET_NO == force)
3504 GCC_check_connections ();
3508 /* Send available buffer space */
3509 if (GNUNET_YES == GCC_is_origin (c, fwd))
3511 GNUNET_assert (NULL != c->t);
3512 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
3513 GCT_unchoke_channels (c->t);
3517 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
3518 send_ack (c, buffer, fwd, force);
3520 GCC_check_connections ();
3525 * Send a message to all peers in this connection that the connection
3526 * is no longer valid.
3528 * If some peer should not receive the message, it should be zero'ed out
3529 * before calling this function.
3531 * @param c The connection whose peers to notify.
3534 GCC_send_destroy (struct CadetConnection *c)
3536 static struct CadetEncryptedMessageIdentifier zero;
3537 struct GNUNET_CADET_ConnectionDestroyMessage msg;
3539 if (GNUNET_YES == c->destroy)
3541 GCC_check_connections ();
3542 msg.header.size = htons (sizeof (msg));
3543 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3545 msg.reserved = htonl (0);
3546 LOG (GNUNET_ERROR_TYPE_DEBUG,
3547 " sending connection destroy for connection %s\n",
3550 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3551 (void) GCC_send_prebuilt_message (&msg.header,
3555 GNUNET_YES, GNUNET_YES, NULL, NULL);
3556 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3557 (void) GCC_send_prebuilt_message (&msg.header,
3561 GNUNET_NO, GNUNET_YES, NULL, NULL);
3563 GCC_check_connections ();
3568 * @brief Start a polling timer for the connection.
3570 * When a neighbor does not accept more traffic on the connection it could be
3571 * caused by a simple congestion or by a lost ACK. Polling enables to check
3572 * for the lastest ACK status for a connection.
3574 * @param c Connection.
3575 * @param fwd Should we poll in the FWD direction?
3578 GCC_start_poll (struct CadetConnection *c, int fwd)
3580 struct CadetFlowControl *fc;
3582 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3583 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3585 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3587 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL already in progress (t: %p, m: %p)\n",
3588 fc->poll_task, fc->poll_msg);
3591 if (0 == fc->queue_max)
3593 /* Should not be needed, traffic should've been cancelled. */
3595 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not possible, peer disconnected\n");
3598 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3599 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time, &send_poll, fc);
3604 * @brief Stop polling a connection for ACKs.
3606 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3608 * @param c Connection.
3609 * @param fwd Should we stop the poll in the FWD direction?
3612 GCC_stop_poll (struct CadetConnection *c, int fwd)
3614 struct CadetFlowControl *fc;
3616 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3617 if (NULL != fc->poll_task)
3619 GNUNET_SCHEDULER_cancel (fc->poll_task);
3620 fc->poll_task = NULL;
3622 if (NULL != fc->poll_msg)
3624 GCC_cancel (fc->poll_msg);
3625 fc->poll_msg = NULL;
3631 * Get a (static) string for a connection.
3633 * @param c Connection.
3636 GCC_2s (const struct CadetConnection *c)
3643 static char buf[128];
3645 SPRINTF (buf, "%s (->%s)",
3646 GNUNET_sh2s (&GCC_get_id (c)->connection_of_tunnel),
3650 return GNUNET_sh2s (&c->id.connection_of_tunnel);
3655 * Log all possible info about the connection state.
3657 * @param c Connection to debug.
3658 * @param level Debug level to use.
3661 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3666 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3668 __FILE__, __FUNCTION__, __LINE__);
3674 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3678 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3679 s = path_2s (c->path);
3680 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3682 LOG2 (level, "CCC state: %s, destroy: %u\n",
3683 GCC_state2s (c->state), c->destroy);
3684 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3685 if (NULL != c->perf)
3686 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3688 LOG2 (level, "CCC FWD flow control:\n");
3689 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3690 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3691 ntohl (c->fwd_fc.last_pid_sent.pid),
3692 ntohl (c->fwd_fc.last_pid_recv.pid));
3693 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3694 ntohl (c->fwd_fc.last_ack_sent.pid),
3695 ntohl (c->fwd_fc.last_ack_recv.pid));
3696 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3697 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3698 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3700 LOG2 (level, "CCC BCK flow control:\n");
3701 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3702 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3703 ntohl (c->bck_fc.last_pid_sent.pid),
3704 ntohl (c->bck_fc.last_pid_recv.pid));
3705 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3706 ntohl (c->bck_fc.last_ack_sent.pid),
3707 ntohl (c->bck_fc.last_ack_recv.pid));
3708 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3709 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3710 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3712 LOG2 (level, "CCC DEBUG CONNECTION END\n");