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 * Struct to encapsulate all the Flow Control information to a peer to which
58 * we are directly connected (on a core level).
60 struct CadetFlowControl
63 * Connection this controls.
65 struct CadetConnection *c;
68 * How many messages are in the queue on this connection.
73 * How many messages do we accept in the queue.
74 * If 0, the connection is broken in this direction (next hop disconnected).
76 unsigned int queue_max;
79 * ID of the next packet to send.
84 * ID of the last packet sent towards the peer.
86 uint32_t last_pid_sent;
89 * ID of the last packet received from the peer.
91 uint32_t last_pid_recv;
94 * Bitmap of past 32 messages received:
95 * - LSB being @c last_pid_recv.
96 * - MSB being @c last_pid_recv - 31 (mod UINTMAX).
101 * Last ACK sent to the peer (peer can't send more than this PID).
103 uint32_t last_ack_sent;
106 * Last ACK sent towards the origin (for traffic towards leaf node).
108 uint32_t last_ack_recv;
111 * Task to poll the peer in case of a lost ACK causes stall.
113 struct GNUNET_SCHEDULER_Task *poll_task;
116 * How frequently to poll for ACKs.
118 struct GNUNET_TIME_Relative poll_time;
121 * Queued poll message, to cancel if not necessary anymore (got ACK).
123 struct CadetConnectionQueue *poll_msg;
126 * Queued poll message, to cancel if not necessary anymore (got ACK).
128 struct CadetConnectionQueue *ack_msg;
132 * Keep a record of the last messages sent on this connection.
134 struct CadetConnectionPerformance
137 * Circular buffer for storing measurements.
139 double usecsperbyte[AVG_MSGS];
142 * Running average of @c usecsperbyte.
147 * How many values of @c usecsperbyte are valid.
152 * Index of the next "free" position in @c usecsperbyte.
159 * Struct containing all information regarding a connection to a peer.
161 struct CadetConnection
164 * Tunnel this connection is part of.
166 struct CadetTunnel *t;
169 * Flow control information for traffic fwd.
171 struct CadetFlowControl fwd_fc;
174 * Flow control information for traffic bck.
176 struct CadetFlowControl bck_fc;
179 * Measure connection performance on the endpoint.
181 struct CadetConnectionPerformance *perf;
184 * ID of the connection.
186 struct GNUNET_CADET_Hash id;
189 * Path being used for the tunnel. At the origin of the connection
190 * it's a pointer to the destination's path pool, otherwise just a copy.
192 struct CadetPeerPath *path;
195 * Task to keep the used paths alive at the owner,
196 * time tunnel out on all the other peers.
198 struct GNUNET_SCHEDULER_Task *fwd_maintenance_task;
201 * Task to keep the used paths alive at the destination,
202 * time tunnel out on all the other peers.
204 struct GNUNET_SCHEDULER_Task *bck_maintenance_task;
207 * Queue handle for maintainance traffic. One handle for FWD and BCK since
208 * one peer never needs to maintain both directions (no loopback connections).
210 struct CadetPeerQueue *maintenance_q;
213 * Should equal #get_next_hop(), or NULL if that peer disconnected.
215 struct CadetPeer *next_peer;
218 * Should equal #get_prev_hop(), or NULL if that peer disconnected.
220 struct CadetPeer *prev_peer;
223 * State of the connection.
225 enum CadetConnectionState state;
228 * Position of the local peer in the path.
230 unsigned int own_pos;
233 * Pending message count.
235 unsigned int pending_messages;
239 * - if 0, connection in use.
240 * - if 1, destroy on last message.
241 * - if 2, connection is being destroyed don't re-enter.
246 * In-connection-map flag. Sometimes, when @e destroy is set but
247 * actual destruction is delayed to enable us to finish processing
248 * queues (i.e. in the direction that is still working), we remove
249 * the connection from the map to prevent it from still being
250 * found (and used) by accident. This flag is set to #GNUNET_YES
251 * for a connection that is not in the #connections map. Should
252 * only be #GNUNET_YES if #destroy is also non-zero.
257 * Counter to do exponential backoff when creating a connection (max 64).
259 unsigned short create_retry;
262 * Task to check if connection has duplicates.
264 struct GNUNET_SCHEDULER_Task *check_duplicates_task;
269 * Handle for messages queued but not yet sent.
271 struct CadetConnectionQueue
274 * Peer queue handle, to cancel if necessary.
276 struct CadetPeerQueue *peer_q;
279 * Continuation to call once sent.
284 * Closure for @e cont.
289 * Was this a forced message? (Do not account for it)
294 /******************************************************************************/
295 /******************************* GLOBALS ***********************************/
296 /******************************************************************************/
299 * Global handle to the statistics service.
301 extern struct GNUNET_STATISTICS_Handle *stats;
304 * Local peer own ID (memory efficient handle).
306 extern GNUNET_PEER_Id myid;
309 * Local peer own ID (full value).
311 extern struct GNUNET_PeerIdentity my_full_id;
314 * Connections known, indexed by cid (CadetConnection).
316 static struct GNUNET_CONTAINER_MultiHashMap *connections;
319 * How many connections are we willing to maintain.
320 * Local connections are always allowed,
321 * even if there are more connections than max.
323 static unsigned long long max_connections;
326 * How many messages *in total* are we willing to queue, divide by number of
327 * connections to get connection queue size.
329 static unsigned long long max_msgs_queue;
332 * How often to send path keepalives. Paths timeout after 4 missed.
334 static struct GNUNET_TIME_Relative refresh_connection_time;
337 * How often to send path create / ACKs.
339 static struct GNUNET_TIME_Relative create_connection_time;
342 /******************************************************************************/
343 /******************************** STATIC ***********************************/
344 /******************************************************************************/
348 #if 0 // avoid compiler warning for unused static function
350 fc_debug (struct CadetFlowControl *fc)
352 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
353 fc->last_pid_recv, fc->last_ack_sent);
354 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
355 fc->last_pid_sent, fc->last_ack_recv);
356 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
357 fc->queue_n, fc->queue_max);
361 connection_debug (struct CadetConnection *c)
365 LOG (GNUNET_ERROR_TYPE_INFO, "DEBUG NULL CONNECTION\n");
368 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
369 peer2s (c->t->peer), GCC_2s (c));
370 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
371 c->state, c->pending_messages);
372 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
373 fc_debug (&c->fwd_fc);
374 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
375 fc_debug (&c->bck_fc);
381 * Schedule next keepalive task, taking in consideration
382 * the connection state and number of retries.
384 * @param c Connection for which to schedule the next keepalive.
385 * @param fwd Direction for the next keepalive.
388 schedule_next_keepalive (struct CadetConnection *c, int fwd);
392 * Resets the connection timeout task, some other message has done the
394 * - For the first peer on the direction this means to send
395 * a keepalive or a path confirmation message (either create or ACK).
396 * - For all other peers, this means to destroy the connection,
397 * due to lack of activity.
398 * Starts the timeout if no timeout was running (connection just created).
400 * @param c Connection whose timeout to reset.
401 * @param fwd Is this forward?
404 connection_reset_timeout (struct CadetConnection *c, int fwd);
408 * Get string description for tunnel state. Reentrant.
410 * @param s Tunnel state.
412 * @return String representation.
415 GCC_state2s (enum CadetConnectionState s)
419 case CADET_CONNECTION_NEW:
420 return "CADET_CONNECTION_NEW";
421 case CADET_CONNECTION_SENT:
422 return "CADET_CONNECTION_SENT";
423 case CADET_CONNECTION_ACK:
424 return "CADET_CONNECTION_ACK";
425 case CADET_CONNECTION_READY:
426 return "CADET_CONNECTION_READY";
427 case CADET_CONNECTION_DESTROYED:
428 return "CADET_CONNECTION_DESTROYED";
429 case CADET_CONNECTION_BROKEN:
430 return "CADET_CONNECTION_BROKEN";
433 LOG (GNUNET_ERROR_TYPE_ERROR, " conn state %u unknown!\n", s);
434 return "CADET_CONNECTION_STATE_ERROR";
440 * Initialize a Flow Control structure to the initial state.
442 * @param fc Flow Control structure to initialize.
445 fc_init (struct CadetFlowControl *fc)
447 fc->next_pid = (uint32_t) 0;
448 fc->last_pid_sent = (uint32_t) -1;
449 fc->last_pid_recv = (uint32_t) -1;
450 fc->last_ack_sent = (uint32_t) 0;
451 fc->last_ack_recv = (uint32_t) 0;
452 fc->poll_task = NULL;
453 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
455 fc->queue_max = (max_msgs_queue / max_connections) + 1;
462 * @param cid Connection ID.
464 * @return conntection with the given ID @cid or NULL if not found.
466 static struct CadetConnection *
467 connection_get (const struct GNUNET_CADET_Hash *cid)
469 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
474 * Change the connection state. Cannot change a connection marked as destroyed.
476 * @param c Connection to change.
477 * @param state New state to set.
480 connection_change_state (struct CadetConnection* c,
481 enum CadetConnectionState state)
483 LOG (GNUNET_ERROR_TYPE_DEBUG,
484 "Connection %s state %s -> %s\n",
485 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
486 if (CADET_CONNECTION_DESTROYED <= c->state) /* Destroyed or broken. */
488 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
492 if (CADET_CONNECTION_READY == state)
498 * Mark a connection as "destroyed", to send all pending traffic and freeing
499 * all associated resources, without accepting new status changes on it.
501 * @param c Connection to mark as destroyed.
504 mark_destroyed (struct CadetConnection *c)
506 c->destroy = GNUNET_YES;
507 connection_change_state (c, CADET_CONNECTION_DESTROYED);
512 * Callback called when a queued ACK message is sent.
514 * @param cls Closure (FC).
515 * @param c Connection this message was on.
516 * @param q Queue handler this call invalidates.
517 * @param type Type of message sent.
518 * @param fwd Was this a FWD going message?
519 * @param size Size of the message.
523 struct CadetConnection *c,
524 struct CadetConnectionQueue *q,
525 uint16_t type, int fwd, size_t size)
527 struct CadetFlowControl *fc = cls;
534 * Send an ACK on the connection, informing the predecessor about
535 * the available buffer space. Should not be called in case the peer
536 * is origin (no predecessor) in the @c fwd direction.
538 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
539 * the ACK itself goes "back" (dest->root).
541 * @param c Connection on which to send the ACK.
542 * @param buffer How much space free to advertise?
543 * @param fwd Is this FWD ACK? (Going dest -> root)
544 * @param force Don't optimize out.
547 send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
549 struct CadetFlowControl *next_fc;
550 struct CadetFlowControl *prev_fc;
551 struct GNUNET_CADET_ACK msg;
555 /* If origin, there is no connection to send ACKs. Wrong function! */
556 GCC_check_connections ();
557 if (GCC_is_origin (c, fwd))
559 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
560 GCC_2s (c), GC_f2s (fwd));
565 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
566 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
568 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
569 GC_f2s (fwd), GCC_2s (c));
571 /* Check if we need to transmit the ACK. */
572 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
573 if (3 < delta && buffer < delta && GNUNET_NO == force)
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
576 LOG (GNUNET_ERROR_TYPE_DEBUG,
577 " last pid recv: %u, last ack sent: %u\n",
578 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
579 GCC_check_connections ();
583 /* Ok, ACK might be necessary, what PID to ACK? */
584 ack = prev_fc->last_pid_recv + buffer;
585 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
586 LOG (GNUNET_ERROR_TYPE_DEBUG,
587 " last pid %u, last ack %u, qmax %u, q %u\n",
588 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
589 next_fc->queue_max, next_fc->queue_n);
590 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
592 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
593 GCC_check_connections ();
597 /* Check if message is already in queue */
598 if (NULL != prev_fc->ack_msg)
600 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
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 ();
614 prev_fc->last_ack_sent = ack;
616 /* Build ACK message and send on conn */
617 msg.header.size = htons (sizeof (msg));
618 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
619 msg.ack = htonl (ack);
622 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, UINT16_MAX, ack,
625 GNUNET_assert (NULL != prev_fc->ack_msg);
626 GCC_check_connections ();
631 * Update performance information if we are a connection's endpoint.
633 * @param c Connection to update.
634 * @param wait How much time did we wait to send the last message.
635 * @param size Size of the last message.
638 update_perf (struct CadetConnection *c,
639 struct GNUNET_TIME_Relative wait,
642 struct CadetConnectionPerformance *p;
646 return; /* Only endpoints are interested in timing. */
649 usecsperbyte = ((double) wait.rel_value_us) / size;
650 if (p->size == AVG_MSGS)
652 /* Array is full. Substract oldest value, add new one and store. */
653 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
654 p->usecsperbyte[p->idx] = usecsperbyte;
655 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
659 /* Array not yet full. Add current value to avg and store. */
660 p->usecsperbyte[p->idx] = usecsperbyte;
662 p->avg += p->usecsperbyte[p->idx];
666 p->idx = (p->idx + 1) % AVG_MSGS;
671 * Callback called when a connection queued message is sent.
673 * Calculates the average time and connection packet tracking.
675 * @param cls Closure (ConnectionQueue Handle), can be NULL.
676 * @param c Connection this message was on.
677 * @param fwd Was this a FWD going message?
678 * @param sent Was it really sent? (Could have been canceled)
679 * @param type Type of message sent.
680 * @param payload_type Type of payload, if applicable.
681 * @param pid Message ID, or 0 if not applicable (create, destroy, etc).
682 * @param size Size of the message.
683 * @param wait Time spent waiting for core (only the time for THIS message)
686 conn_message_sent (void *cls,
687 struct CadetConnection *c, int fwd, int sent,
688 uint16_t type, uint16_t payload_type, uint32_t pid,
690 struct GNUNET_TIME_Relative wait)
692 struct CadetConnectionQueue *q = cls;
693 struct CadetFlowControl *fc;
696 GCC_check_connections ();
698 /* If c is NULL, nothing to update. */
701 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
702 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
704 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
707 GCC_check_connections ();
711 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
712 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
714 /* Update flow control info. */
715 fc = fwd ? &c->fwd_fc : &c->bck_fc;
716 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
717 sent ? "" : "not ", GC_f2s (fwd),
718 GC_m2s (type), GC_m2s (payload_type), pid);
724 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
725 q->cont (q->cont_cls, c, q, type, fwd, size);
729 else if (type == GNUNET_MESSAGE_TYPE_CADET_AX)
731 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
739 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
740 c->pending_messages--;
741 if ( (GNUNET_YES == c->destroy) &&
742 (0 == c->pending_messages) )
744 LOG (GNUNET_ERROR_TYPE_DEBUG,
745 "! destroying connection!\n");
747 GCC_check_connections ();
751 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
754 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
755 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
756 c->maintenance_q = NULL;
757 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
758 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
759 schedule_next_keepalive (c, fwd);
762 case GNUNET_MESSAGE_TYPE_CADET_AX:
763 if (GNUNET_YES == sent)
765 GNUNET_assert (NULL != q);
766 fc->last_pid_sent = pid;
767 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
768 GCC_start_poll (c, fwd);
769 GCC_send_ack (c, fwd, GNUNET_NO);
770 connection_reset_timeout (c, fwd);
773 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
774 if (GNUNET_NO == forced)
777 LOG (GNUNET_ERROR_TYPE_DEBUG,
778 "! accounting pid %u\n",
783 LOG (GNUNET_ERROR_TYPE_DEBUG,
784 "! forced, Q_N not accounting pid %u\n",
789 case GNUNET_MESSAGE_TYPE_CADET_KX:
790 if (GNUNET_YES == sent)
791 connection_reset_timeout (c, fwd);
794 case GNUNET_MESSAGE_TYPE_CADET_POLL:
798 case GNUNET_MESSAGE_TYPE_CADET_ACK:
802 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
803 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
807 LOG (GNUNET_ERROR_TYPE_ERROR, "%s unknown\n", GC_m2s (type));
811 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
813 update_perf (c, wait, size);
814 GCC_check_connections ();
819 * Get the previous hop in a connection
821 * @param c Connection.
823 * @return Previous peer in the connection.
825 static struct CadetPeer *
826 get_prev_hop (const struct CadetConnection *c)
832 LOG (GNUNET_ERROR_TYPE_DEBUG,
833 " get prev hop %s [%u/%u]\n",
834 GCC_2s (c), c->own_pos, c->path->length);
835 if (0 == c->own_pos || c->path->length < 2)
836 id = c->path->peers[0];
838 id = c->path->peers[c->own_pos - 1];
840 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
841 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
843 return GCP_get_short (id, GNUNET_YES);
848 * Get the next hop in a connection
850 * @param c Connection.
852 * @return Next peer in the connection.
854 static struct CadetPeer *
855 get_next_hop (const struct CadetConnection *c)
862 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
863 GCC_2s (c), c->own_pos, c->path->length);
864 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
865 id = c->path->peers[c->path->length - 1];
867 id = c->path->peers[c->own_pos + 1];
869 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
870 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
872 return GCP_get_short (id, GNUNET_YES);
877 * Check that the direct neighbours (previous and next hop)
878 * are properly associated with this connection.
880 * @param c connection to check
883 check_neighbours (const struct CadetConnection *c)
886 return; /* nothing to check */
887 GCP_check_connection (get_next_hop (c), c);
888 GCP_check_connection (get_prev_hop (c), c);
893 * Helper for #GCC_check_connections(). Calls #check_neighbours().
897 * @param value the `struct CadetConnection` to check
898 * @return #GNUNET_OK (continue to iterate)
901 check_connection (void *cls,
902 const struct GNUNET_HashCode *key,
905 struct CadetConnection *c = value;
907 check_neighbours (c);
913 * Check invariants for all connections using #check_neighbours().
916 GCC_check_connections ()
918 if (0 == CHECK_INVARIANTS)
920 if (NULL == connections)
922 GNUNET_CONTAINER_multihashmap_iterate (connections,
929 * Get the hop in a connection.
931 * @param c Connection.
932 * @param fwd Next in the FWD direction?
934 * @return Next peer in the connection.
936 static struct CadetPeer *
937 get_hop (struct CadetConnection *c, int fwd)
939 return (fwd) ? get_next_hop (c) : get_prev_hop (c);
944 * Get a bit mask for a message received out-of-order.
946 * @param last_pid_recv Last PID we received prior to the out-of-order.
947 * @param ooo_pid PID of the out-of-order message.
950 get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
952 return 1 << (last_pid_recv - ooo_pid);
957 * Check is an out-of-order message is ok:
958 * - at most 31 messages behind.
961 * @param last_pid_recv Last in-order PID received.
964 is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
968 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
971 mask = get_recv_bitmask (last_pid_recv, ooo_pid);
972 if (0 != (ooo_bitmap & mask))
980 * Is traffic coming from this sender 'FWD' traffic?
982 * @param c Connection to check.
983 * @param sender Short peer identity of neighbor.
985 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
986 * the traffic is 'FWD'.
987 * #GNUNET_NO for BCK.
988 * #GNUNET_SYSERR for errors (sender isn't a hop in the connection).
991 is_fwd (const struct CadetConnection *c,
992 const struct CadetPeer *sender)
996 id = GCP_get_short_id (sender);
997 if (GCP_get_short_id (get_prev_hop (c)) == id)
1000 if (GCP_get_short_id (get_next_hop (c)) == id)
1003 return GNUNET_SYSERR;
1008 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1009 * or a first CONNECTION_ACK directed to us.
1011 * @param c Connection to confirm.
1012 * @param fwd Should we send it FWD? (root->dest)
1013 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
1016 send_connection_ack (struct CadetConnection *c, int fwd)
1018 struct GNUNET_CADET_ConnectionACK msg;
1019 struct CadetTunnel *t;
1020 size_t size = sizeof (struct GNUNET_CADET_ConnectionACK);
1022 GCC_check_connections ();
1024 LOG (GNUNET_ERROR_TYPE_INFO,
1025 "==> { C %s ACK} %19s on conn %s (%p) %s [%5u]\n",
1026 GC_f2s (!fwd), "", GCC_2s (c), c, GC_f2s (fwd), size);
1028 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
1029 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
1032 GNUNET_assert (NULL == c->maintenance_q);
1033 c->maintenance_q = GCP_send (get_hop (c, fwd), &msg.header,
1034 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0,
1036 &conn_message_sent, NULL);
1037 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (conn`ACK)\n",
1038 c, c->pending_messages);
1039 c->pending_messages++;
1041 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
1042 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
1043 if (CADET_CONNECTION_READY != c->state)
1044 connection_change_state (c, CADET_CONNECTION_SENT);
1045 GCC_check_connections ();
1050 * Send a notification that a connection is broken.
1052 * @param c Connection that is broken.
1053 * @param id1 Peer that has disconnected.
1054 * @param id2 Peer that has disconnected.
1055 * @param fwd Direction towards which to send it.
1058 send_broken (struct CadetConnection *c,
1059 const struct GNUNET_PeerIdentity *id1,
1060 const struct GNUNET_PeerIdentity *id2,
1063 struct GNUNET_CADET_ConnectionBroken msg;
1065 GCC_check_connections ();
1066 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1067 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1071 GNUNET_assert (NULL ==
1072 GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c, fwd,
1073 GNUNET_YES, NULL, NULL));
1074 GCC_check_connections ();
1079 * Send a notification that a connection is broken, when a connection
1080 * isn't even known to the local peer or soon to be destroyed.
1082 * @param connection_id Connection ID.
1083 * @param id1 Peer that has disconnected, probably local peer.
1084 * @param id2 Peer that has disconnected can be NULL if unknown.
1085 * @param neighbor Peer to notify (neighbor who sent the connection).
1088 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
1089 const struct GNUNET_PeerIdentity *id1,
1090 const struct GNUNET_PeerIdentity *id2,
1091 struct CadetPeer *neighbor)
1093 struct GNUNET_CADET_ConnectionBroken *msg;
1095 GCC_check_connections ();
1096 LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n",
1097 GNUNET_h2s (GC_h2hc (connection_id)));
1099 msg = GNUNET_new (struct GNUNET_CADET_ConnectionBroken);
1100 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1101 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1102 msg->cid = *connection_id;
1107 memset (&msg->peer2, 0, sizeof (msg->peer2));
1108 GNUNET_assert (NULL != GCP_send (neighbor, &msg->header,
1110 NULL, GNUNET_SYSERR, /* connection, fwd */
1111 NULL, NULL)); /* continuation */
1112 GCC_check_connections ();
1117 * Send keepalive packets for a connection.
1119 * @param c Connection to keep alive..
1120 * @param fwd Is this a FWD keepalive? (owner -> dest).
1123 send_connection_keepalive (struct CadetConnection *c, int fwd)
1125 struct GNUNET_MessageHeader msg;
1126 struct CadetFlowControl *fc;
1128 GCC_check_connections ();
1129 LOG (GNUNET_ERROR_TYPE_INFO,
1130 "keepalive %s for connection %s\n",
1131 GC_f2s (fwd), GCC_2s (c));
1133 GNUNET_assert (NULL != c->t);
1134 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1135 if (0 < fc->queue_n || GNUNET_YES == GCT_has_queued_traffic (c->t))
1137 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1141 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1143 GNUNET_assert (NULL != c->t);
1144 msg.size = htons (sizeof (msg));
1145 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
1147 GNUNET_assert (NULL ==
1148 GCT_send_prebuilt_message (&msg, c->t, c,
1149 GNUNET_NO, NULL, NULL));
1150 GCC_check_connections ();
1155 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1157 * @param c Connection for which to send the message.
1158 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1161 connection_recreate (struct CadetConnection *c, int fwd)
1163 LOG (GNUNET_ERROR_TYPE_DEBUG,
1164 "sending connection recreate\n");
1166 GCC_send_create (c);
1168 send_connection_ack (c, GNUNET_NO);
1173 * Generic connection timer management.
1174 * Depending on the role of the peer in the connection will send the
1175 * appropriate message (build or keepalive)
1177 * @param c Conncetion to maintain.
1178 * @param fwd Is FWD?
1181 connection_maintain (struct CadetConnection *c, int fwd)
1183 if (GNUNET_NO != c->destroy)
1185 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1192 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1196 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1198 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1200 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1201 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1202 schedule_next_keepalive (c, fwd);
1207 case CADET_CONNECTION_NEW:
1210 case CADET_CONNECTION_SENT:
1211 connection_recreate (c, fwd);
1213 case CADET_CONNECTION_READY:
1214 send_connection_keepalive (c, fwd);
1223 * Keep the connection alive.
1225 * @param c Connection to keep alive.
1226 * @param fwd Direction.
1229 connection_keepalive (struct CadetConnection *c,
1232 GCC_check_connections ();
1233 LOG (GNUNET_ERROR_TYPE_DEBUG,
1234 "%s keepalive for %s\n",
1235 GC_f2s (fwd), GCC_2s (c));
1238 c->fwd_maintenance_task = NULL;
1240 c->bck_maintenance_task = NULL;
1241 connection_maintain (c, fwd);
1242 GCC_check_connections ();
1243 /* Next execution will be scheduled by message_sent or _maintain*/
1248 * Keep the connection alive in the FWD direction.
1250 * @param cls Closure (connection to keepalive).
1253 connection_fwd_keepalive (void *cls)
1255 struct CadetConnection *c = cls;
1257 GCC_check_connections ();
1258 connection_keepalive (c,
1260 GCC_check_connections ();
1265 * Keep the connection alive in the BCK direction.
1267 * @param cls Closure (connection to keepalive).
1270 connection_bck_keepalive (void *cls)
1272 struct CadetConnection *c = cls;
1274 GCC_check_connections ();
1275 connection_keepalive (c,
1277 GCC_check_connections ();
1282 * Schedule next keepalive task, taking in consideration
1283 * the connection state and number of retries.
1285 * If the peer is not the origin, do nothing.
1287 * @param c Connection for which to schedule the next keepalive.
1288 * @param fwd Direction for the next keepalive.
1291 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1293 struct GNUNET_TIME_Relative delay;
1294 struct GNUNET_SCHEDULER_Task * *task_id;
1295 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1297 GCC_check_connections ();
1298 if (GNUNET_NO == GCC_is_origin (c, fwd))
1301 /* Calculate delay to use, depending on the state of the connection */
1302 if (CADET_CONNECTION_READY == c->state)
1304 delay = refresh_connection_time;
1308 if (1 > c->create_retry)
1309 c->create_retry = 1;
1310 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1312 if (c->create_retry < 64)
1313 c->create_retry *= 2;
1316 /* Select direction-dependent parameters */
1317 if (GNUNET_YES == fwd)
1319 task_id = &c->fwd_maintenance_task;
1320 keepalive_task = &connection_fwd_keepalive;
1324 task_id = &c->bck_maintenance_task;
1325 keepalive_task = &connection_bck_keepalive;
1328 /* Check that no one scheduled it before us */
1329 if (NULL != *task_id)
1331 /* No need for a _break. It can happen for instance when sending a SYNACK
1332 * for a duplicate SYN: the first SYNACK scheduled the task. */
1333 GNUNET_SCHEDULER_cancel (*task_id);
1336 /* Schedule the task */
1337 *task_id = GNUNET_SCHEDULER_add_delayed (delay,
1340 LOG (GNUNET_ERROR_TYPE_DEBUG,
1341 "next keepalive in %s\n",
1342 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1343 GCC_check_connections ();
1348 * Cancel all transmissions that belong to a certain connection.
1350 * If the connection is scheduled for destruction and no more messages are left,
1351 * the connection will be destroyed by the continuation call.
1353 * @param c Connection which to cancel. Might be destroyed during this call.
1354 * @param fwd Cancel fwd traffic?
1357 connection_cancel_queues (struct CadetConnection *c,
1360 struct CadetFlowControl *fc;
1362 GCC_check_connections ();
1363 LOG (GNUNET_ERROR_TYPE_DEBUG,
1364 "Cancel %s queues for connection %s\n",
1365 GC_f2s (fwd), GCC_2s (c));
1372 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1373 if (NULL != fc->poll_task)
1375 GNUNET_SCHEDULER_cancel (fc->poll_task);
1376 fc->poll_task = NULL;
1377 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL task for fc %p\n", fc);
1379 if (NULL != fc->poll_msg)
1381 GCC_cancel (fc->poll_msg);
1382 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL msg for fc %p\n", fc);
1384 GCC_check_connections ();
1389 * Function called if a connection has been stalled for a while,
1390 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1392 * @param cls Closure (poll ctx).
1395 connection_poll (void *cls);
1399 * Callback called when a queued POLL message is sent.
1401 * @param cls Closure (flow control context).
1402 * @param c Connection this message was on.
1403 * @param q Queue handler this call invalidates.
1404 * @param type Type of message sent.
1405 * @param fwd Was this a FWD going message?
1406 * @param size Size of the message.
1409 poll_sent (void *cls,
1410 struct CadetConnection *c,
1411 struct CadetConnectionQueue *q,
1412 uint16_t type, int fwd, size_t size)
1414 struct CadetFlowControl *fc = cls;
1416 GNUNET_assert (fc->poll_msg == q);
1417 fc->poll_msg = NULL;
1418 if (2 == c->destroy)
1420 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1423 if (0 == fc->queue_max)
1425 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL cancelled: neighbor disconnected\n");
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1430 GNUNET_assert (NULL == fc->poll_task);
1431 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1432 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1435 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1440 * Function called if a connection has been stalled for a while,
1441 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1443 * @param cls Closure (poll ctx).
1446 connection_poll (void *cls)
1448 struct CadetFlowControl *fc = cls;
1449 struct GNUNET_CADET_Poll msg;
1450 struct CadetConnection *c;
1453 fc->poll_task = NULL;
1454 GCC_check_connections ();
1456 fwd = fc == &c->fwd_fc;
1457 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1458 GCC_2s (c), GC_f2s (fwd));
1460 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1461 msg.header.size = htons (sizeof (msg));
1462 msg.pid = htonl (fc->last_pid_sent);
1463 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1465 GCC_send_prebuilt_message (&msg.header, UINT16_MAX, fc->last_pid_sent, c,
1466 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1467 GNUNET_assert (NULL != fc->poll_msg);
1468 GCC_check_connections ();
1473 * Generic connection timeout implementation.
1475 * Timeout function due to lack of keepalive/traffic from an endpoint.
1476 * Destroys connection if called.
1478 * @param c Connection to destroy.
1479 * @param fwd Was the timeout from the origin? (FWD timeout)
1482 connection_timeout (struct CadetConnection *c, int fwd)
1484 GCC_check_connections ();
1486 LOG (GNUNET_ERROR_TYPE_INFO,
1487 "Connection %s %s timed out. Destroying.\n",
1490 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1492 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1498 /* If dest, send "broken" notification. */
1499 if (GCC_is_terminal (c, fwd))
1501 struct CadetPeer *next_hop;
1503 next_hop = fwd ? get_prev_hop (c) : get_next_hop (c);
1504 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1508 GCC_check_connections ();
1513 * Timeout function due to lack of keepalive/traffic from the owner.
1514 * Destroys connection if called.
1516 * @param cls Closure (connection to destroy).
1519 connection_fwd_timeout (void *cls)
1521 struct CadetConnection *c = cls;
1523 c->fwd_maintenance_task = NULL;
1524 GCC_check_connections ();
1525 connection_timeout (c, GNUNET_YES);
1526 GCC_check_connections ();
1531 * Timeout function due to lack of keepalive/traffic from the destination.
1532 * Destroys connection if called.
1534 * @param cls Closure (connection to destroy).
1537 connection_bck_timeout (void *cls)
1539 struct CadetConnection *c = cls;
1541 c->bck_maintenance_task = NULL;
1542 GCC_check_connections ();
1543 connection_timeout (c, GNUNET_NO);
1544 GCC_check_connections ();
1549 * Resets the connection timeout task, some other message has done the
1551 * - For the first peer on the direction this means to send
1552 * a keepalive or a path confirmation message (either create or ACK).
1553 * - For all other peers, this means to destroy the connection,
1554 * due to lack of activity.
1555 * Starts the timeout if no timeout was running (connection just created).
1557 * @param c Connection whose timeout to reset.
1558 * @param fwd Is this forward?
1560 * TODO use heap to improve efficiency of scheduler.
1563 connection_reset_timeout (struct CadetConnection *c, int fwd)
1565 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1566 if (GCC_is_origin (c, fwd)) /* Startpoint */
1568 schedule_next_keepalive (c, fwd);
1570 else /* Relay, endpoint. */
1572 struct GNUNET_TIME_Relative delay;
1573 struct GNUNET_SCHEDULER_Task * *ti;
1574 GNUNET_SCHEDULER_TaskCallback f;
1576 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1579 GNUNET_SCHEDULER_cancel (*ti);
1580 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1581 LOG (GNUNET_ERROR_TYPE_DEBUG,
1582 " timing out in %s\n",
1583 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1584 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1585 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1591 * Iterator to compare each connection's path with the path of a new connection.
1593 * If the connection coincides, the c member of path is set to the connection
1594 * and the destroy flag of the connection is set.
1596 * @param cls Closure (new path).
1597 * @param c Connection in the tunnel to check.
1600 check_path (void *cls, struct CadetConnection *c)
1602 struct CadetConnection *new_conn = cls;
1603 struct CadetPeerPath *path = new_conn->path;
1605 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1606 GCC_2s (c), c, c->path->length);
1609 && GNUNET_NO == c->destroy
1610 && CADET_CONNECTION_BROKEN != c->state
1611 && CADET_CONNECTION_DESTROYED != c->state
1612 && path_equivalent (path, c->path))
1614 new_conn->destroy = GNUNET_YES; /* Do not mark_destroyed, */
1615 new_conn->path->c = c; /* this is only a flag for the Iterator. */
1616 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1622 * Finds out if this path is already being used by an existing connection.
1624 * Checks the tunnel towards the destination to see if it contains
1625 * any connection with the same path.
1627 * If the existing connection is ready, it is kept.
1628 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1629 * the peer will eventually reject our attempt).
1631 * @param path Path to check.
1632 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1633 * #GNUNET_NO otherwise.
1636 does_connection_exist (struct CadetConnection *conn)
1638 struct CadetPeer *p;
1639 struct CadetTunnel *t;
1640 struct CadetConnection *c;
1642 p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
1645 t = GCP_get_tunnel (p);
1649 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1651 GCT_iterate_connections (t, &check_path, conn);
1653 if (GNUNET_YES == conn->destroy)
1656 conn->destroy = GNUNET_NO;
1657 conn->path->c = conn;
1658 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1659 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1660 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1661 if (CADET_CONNECTION_READY == c->state)
1663 /* The other peer confirmed a live connection with this path,
1664 * why are they trying to duplicate it? */
1665 GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not valid, connection unique\n");
1673 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1680 * @brief Check if the tunnel this connection belongs to has any other
1681 * connection with the same path, and destroy one if so.
1683 * @param cls Closure (connection to check).
1686 check_duplicates (void *cls)
1688 struct CadetConnection *c = cls;
1690 c->check_duplicates_task = NULL;
1691 if (GNUNET_YES == does_connection_exist (c))
1693 GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1694 send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
1701 * Wait for enough time to let any dead connections time out and check for
1702 * any remaining duplicates.
1704 * @param c Connection that is a potential duplicate.
1707 schedule_check_duplicates (struct CadetConnection *c)
1709 struct GNUNET_TIME_Relative delay;
1711 if (NULL != c->check_duplicates_task)
1713 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 5);
1714 c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
1721 * Add the connection to the list of both neighbors.
1723 * @param c Connection.
1725 * @return #GNUNET_OK if everything went fine
1726 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1729 register_neighbors (struct CadetConnection *c)
1731 c->next_peer = get_next_hop (c);
1732 c->prev_peer = get_prev_hop (c);
1733 GNUNET_assert (c->next_peer != c->prev_peer);
1734 LOG (GNUNET_ERROR_TYPE_DEBUG,
1735 "register neighbors for connection %s\n",
1737 path_debug (c->path);
1738 LOG (GNUNET_ERROR_TYPE_DEBUG,
1739 "own pos %u\n", c->own_pos);
1740 LOG (GNUNET_ERROR_TYPE_DEBUG,
1741 "putting connection %s to next peer %p\n",
1744 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1746 GCP_2s (c->next_peer));
1747 LOG (GNUNET_ERROR_TYPE_DEBUG,
1748 "putting connection %s to prev peer %p\n",
1751 LOG (GNUNET_ERROR_TYPE_DEBUG,
1752 "prev peer %p %s\n",
1754 GCP_2s (c->prev_peer));
1756 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1757 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1759 if (GCC_is_origin (c, GNUNET_YES))
1760 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1761 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1763 LOG (GNUNET_ERROR_TYPE_DEBUG,
1764 " register neighbors failed\n");
1765 LOG (GNUNET_ERROR_TYPE_DEBUG,
1766 " prev: %s, neighbor?: %d\n",
1767 GCP_2s (c->prev_peer),
1768 GCP_is_neighbor (c->prev_peer));
1769 LOG (GNUNET_ERROR_TYPE_DEBUG,
1770 " next: %s, neighbor?: %d\n",
1771 GCP_2s (c->next_peer),
1772 GCP_is_neighbor (c->next_peer));
1773 return GNUNET_SYSERR;
1775 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1776 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1783 * Remove the connection from the list of both neighbors.
1785 * @param c Connection.
1788 unregister_neighbors (struct CadetConnection *c)
1790 // struct CadetPeer *peer; FIXME dont use next_peer, prev_peer
1791 /* Either already unregistered or never got registered, it's ok either way. */
1792 if (NULL == c->path)
1794 if (NULL != c->next_peer)
1796 GCP_remove_connection (c->next_peer, c);
1797 c->next_peer = NULL;
1799 if (NULL != c->prev_peer)
1801 GCP_remove_connection (c->prev_peer, c);
1802 c->prev_peer = NULL;
1808 * Invalidates all paths towards all peers that comprise the connection which
1809 * rely on the disconnected peer.
1811 * ~O(n^3) (peers in connection * paths/peer * links/path)
1813 * @param c Connection whose peers' paths to clean.
1814 * @param disconnected Peer that disconnected.
1817 invalidate_paths (struct CadetConnection *c,
1818 struct CadetPeer *disconnected)
1820 struct CadetPeer *peer;
1823 for (i = 0; i < c->path->length; i++)
1825 peer = GCP_get_short (c->path->peers[i], GNUNET_NO);
1827 GCP_notify_broken_link (peer, &my_full_id, GCP_get_id (disconnected));
1833 * Bind the connection to the peer and the tunnel to that peer.
1835 * If the peer has no tunnel, create one. Update tunnel and connection
1836 * data structres to reflect new status.
1838 * @param c Connection.
1842 add_to_peer (struct CadetConnection *c,
1843 struct CadetPeer *peer)
1845 GCP_add_tunnel (peer);
1846 c->t = GCP_get_tunnel (peer);
1847 GCT_add_connection (c->t, c);
1852 * Log receipt of message on stderr (INFO level).
1854 * @param message Message received.
1855 * @param peer Peer who sent the message.
1856 * @param conn_id Connection ID of the message.
1859 log_message (const struct GNUNET_MessageHeader *message,
1860 const struct CadetPeer *peer,
1861 const struct GNUNET_CADET_Hash *conn_id)
1867 size = ntohs (message->size);
1868 type = ntohs (message->type);
1871 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1872 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1873 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1874 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1880 LOG (GNUNET_ERROR_TYPE_INFO, "<%s %s on conn %s from %s, %6u bytes\n",
1881 arrow, GC_m2s (type), GNUNET_h2s (GC_h2hc (conn_id)),
1882 GCP_2s(peer), (unsigned int) size);
1885 /******************************************************************************/
1886 /******************************** API ***********************************/
1887 /******************************************************************************/
1890 * Handler for connection creation.
1892 * @param peer Message sender (neighbor).
1893 * @param msg Message itself.
1896 GCC_handle_create (struct CadetPeer *peer,
1897 const struct GNUNET_CADET_ConnectionCreate *msg)
1899 const struct GNUNET_CADET_Hash *cid;
1900 struct GNUNET_PeerIdentity *id;
1901 struct CadetPeerPath *path;
1902 struct CadetPeer *dest_peer;
1903 struct CadetPeer *orig_peer;
1904 struct CadetConnection *c;
1905 unsigned int own_pos;
1908 GCC_check_connections ();
1909 size = ntohs (msg->header.size);
1911 /* Calculate hops */
1912 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1913 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1915 GNUNET_break_op (0);
1918 size /= sizeof (struct GNUNET_PeerIdentity);
1921 GNUNET_break_op (0);
1924 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1926 /* Get parameters */
1928 log_message (&msg->header, peer, cid);
1929 id = (struct GNUNET_PeerIdentity *) &msg[1];
1930 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1932 /* Create connection */
1933 c = connection_get (cid);
1936 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1937 size, myid, &own_pos);
1940 /* Path was malformed, probably our own ID was not in it. */
1941 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
1942 GNUNET_break_op (0);
1947 /* We received this request from a neighbor, we cannot be origin */
1948 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
1949 GNUNET_break_op (0);
1950 path_destroy (path);
1954 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1955 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1956 c = GCC_new (cid, NULL, path, own_pos);
1959 if (path->length - 1 == own_pos)
1961 /* If we are destination, why did the creation fail? */
1963 path_destroy (path);
1964 GCC_check_connections ();
1967 send_broken_unknown (cid, &my_full_id,
1968 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1970 path_destroy (path);
1971 GCC_check_connections ();
1974 GCP_add_path_to_all (path, GNUNET_NO);
1975 connection_reset_timeout (c, GNUNET_YES);
1979 path = path_duplicate (c->path);
1981 if (CADET_CONNECTION_NEW == c->state)
1982 connection_change_state (c, CADET_CONNECTION_SENT);
1984 /* Remember peers */
1985 dest_peer = GCP_get (&id[size - 1], GNUNET_YES);
1986 orig_peer = GCP_get (&id[0], GNUNET_YES);
1988 /* Is it a connection to us? */
1989 if (c->own_pos == path->length - 1)
1991 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1992 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1994 add_to_peer (c, orig_peer);
1995 if (GNUNET_YES == does_connection_exist (c))
1997 /* Peer created a connection equal to one we think exists
1999 * Solution: Keep both and postpone disambiguation. In the meantime
2000 * the connection will time out or peer will inform us it is broken.
2003 * - Use explicit duplicate.
2004 * - Accept new conn and destroy the old. (interruption in higher level)
2005 * - Keep the one with higher ID / created by peer with higher ID. */
2006 schedule_check_duplicates (c);
2009 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
2010 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
2012 send_connection_ack (c, GNUNET_NO);
2013 if (CADET_CONNECTION_SENT == c->state)
2014 connection_change_state (c, CADET_CONNECTION_ACK);
2018 /* It's for somebody else! Retransmit. */
2019 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2020 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2021 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2022 GNUNET_assert (NULL ==
2023 GCC_send_prebuilt_message (&msg->header, 0, 0, c,
2024 GNUNET_YES, GNUNET_YES,
2027 path_destroy (path);
2028 GCC_check_connections ();
2033 * Handler for connection confirmations.
2035 * @param peer Message sender (neighbor).
2036 * @param msg Message itself.
2039 GCC_handle_confirm (struct CadetPeer *peer,
2040 const struct GNUNET_CADET_ConnectionACK *msg)
2042 struct CadetConnection *c;
2043 enum CadetConnectionState oldstate;
2046 GCC_check_connections ();
2047 log_message (&msg->header, peer, &msg->cid);
2048 c = connection_get (&msg->cid);
2051 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2053 LOG (GNUNET_ERROR_TYPE_DEBUG,
2054 " don't know the connection!\n");
2055 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2056 GCC_check_connections ();
2059 if (GNUNET_NO != c->destroy)
2061 GNUNET_assert (CADET_CONNECTION_DESTROYED == c->state);
2062 GNUNET_STATISTICS_update (stats, "# control on dying connection",
2064 LOG (GNUNET_ERROR_TYPE_DEBUG,
2065 "connection %s being destroyed, ignoring confirm\n",
2067 GCC_check_connections ();
2071 oldstate = c->state;
2072 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GCP_2s (peer));
2073 if (get_next_hop (c) == peer)
2075 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2077 if (CADET_CONNECTION_SENT == oldstate)
2078 connection_change_state (c, CADET_CONNECTION_ACK);
2080 else if (get_prev_hop (c) == peer)
2082 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2084 connection_change_state (c, CADET_CONNECTION_READY);
2088 GNUNET_STATISTICS_update (stats, "# control on connection from wrong peer",
2090 GNUNET_break_op (0);
2094 connection_reset_timeout (c, fwd);
2096 /* Add path to peers? */
2097 if (NULL != c->path)
2099 GCP_add_path_to_all (c->path, GNUNET_YES);
2106 /* Message for us as creator? */
2107 if (GNUNET_YES == GCC_is_origin (c, GNUNET_YES))
2109 if (GNUNET_NO != fwd)
2114 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2116 /* If just created, cancel the short timeout and start a long one */
2117 if (CADET_CONNECTION_SENT == oldstate)
2118 connection_reset_timeout (c, GNUNET_YES);
2120 /* Change connection state, send ACK */
2121 connection_change_state (c, CADET_CONNECTION_READY);
2122 send_connection_ack (c, GNUNET_YES);
2124 /* Change tunnel state, trigger KX */
2125 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2126 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2127 GCC_check_connections ();
2131 /* Message for us as destination? */
2132 if (GCC_is_terminal (c, GNUNET_YES))
2134 if (GNUNET_YES != fwd)
2139 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2141 /* If just created, cancel the short timeout and start a long one */
2142 if (CADET_CONNECTION_ACK == oldstate)
2143 connection_reset_timeout (c, GNUNET_NO);
2145 /* Change tunnel state */
2146 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2147 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2148 GCC_check_connections ();
2152 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2153 GNUNET_assert (NULL ==
2154 GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2155 GNUNET_YES, NULL, NULL));
2156 GCC_check_connections ();
2162 * Handler for notifications of broken connections.
2164 * @param peer Message sender (neighbor).
2165 * @param msg Message itself.
2168 GCC_handle_broken (struct CadetPeer *peer,
2169 const struct GNUNET_CADET_ConnectionBroken *msg)
2171 struct CadetConnection *c;
2172 struct CadetTunnel *t;
2175 GCC_check_connections ();
2176 log_message (&msg->header, peer, &msg->cid);
2177 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2178 GNUNET_i2s (&msg->peer1));
2179 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2180 GNUNET_i2s (&msg->peer2));
2181 c = connection_get (&msg->cid);
2184 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2185 GNUNET_STATISTICS_update (stats, "# duplicate CONNECTION_BROKEN",
2187 GCC_check_connections ();
2193 fwd = is_fwd (c, peer);
2194 if (GNUNET_SYSERR == fwd)
2196 GNUNET_break_op (0);
2197 GCC_check_connections ();
2201 if (GCC_is_terminal (c, fwd))
2203 struct CadetPeer *endpoint;
2207 /* A terminal connection should not have 't' set to NULL. */
2209 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2212 endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
2213 if (2 < c->path->length)
2214 path_invalidate (c->path);
2215 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2217 connection_change_state (c, CADET_CONNECTION_BROKEN);
2218 GCT_remove_connection (t, c);
2225 GNUNET_assert (NULL ==
2226 GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2227 GNUNET_YES, NULL, NULL));
2228 connection_cancel_queues (c, !fwd);
2230 GCC_check_connections ();
2236 * Handler for notifications of destroyed connections.
2238 * @param peer Message sender (neighbor).
2239 * @param msg Message itself.
2242 GCC_handle_destroy (struct CadetPeer *peer,
2243 const struct GNUNET_CADET_ConnectionDestroy *msg)
2245 struct CadetConnection *c;
2248 GCC_check_connections ();
2249 log_message (&msg->header, peer, &msg->cid);
2250 c = connection_get (&msg->cid);
2253 /* Probably already got the message from another path,
2254 * destroyed the tunnel and retransmitted to children.
2257 GNUNET_STATISTICS_update (stats,
2258 "# control on unknown connection",
2260 LOG (GNUNET_ERROR_TYPE_DEBUG,
2261 " connection unknown destroyed: previously destroyed?\n");
2262 GCC_check_connections ();
2266 fwd = is_fwd (c, peer);
2267 if (GNUNET_SYSERR == fwd)
2269 GNUNET_break_op (0);
2270 GCC_check_connections ();
2274 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2276 GNUNET_assert (NULL ==
2277 GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2278 GNUNET_YES, NULL, NULL));
2280 else if (0 == c->pending_messages)
2282 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
2284 GCC_check_connections ();
2290 GCT_remove_connection (c->t, c);
2293 GCC_check_connections ();
2299 * Handler for cadet network traffic hop-by-hop acks.
2301 * @param peer Message sender (neighbor).
2302 * @param msg Message itself.
2305 GCC_handle_ack (struct CadetPeer *peer,
2306 const struct GNUNET_CADET_ACK *msg)
2308 struct CadetConnection *c;
2309 struct CadetFlowControl *fc;
2313 GCC_check_connections ();
2314 log_message (&msg->header, peer, &msg->cid);
2315 c = connection_get (&msg->cid);
2318 GNUNET_STATISTICS_update (stats,
2319 "# ack on unknown connection",
2322 send_broken_unknown (&msg->cid,
2326 GCC_check_connections ();
2330 /* Is this a forward or backward ACK? */
2331 if (get_next_hop (c) == peer)
2336 else if (get_prev_hop (c) == peer)
2343 GNUNET_break_op (0);
2347 ack = ntohl (msg->ack);
2348 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2349 GC_f2s (fwd), ack, fc->last_ack_recv);
2350 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2351 fc->last_ack_recv = ack;
2353 /* Cancel polling if the ACK is big enough. */
2354 if (NULL != fc->poll_task &&
2355 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2357 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2358 GNUNET_SCHEDULER_cancel (fc->poll_task);
2359 fc->poll_task = NULL;
2360 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2363 GCC_check_connections ();
2368 * Handler for cadet network traffic hop-by-hop data counter polls.
2370 * @param peer Message sender (neighbor).
2371 * @param msg Message itself.
2374 GCC_handle_poll (struct CadetPeer *peer,
2375 const struct GNUNET_CADET_Poll *msg)
2377 struct CadetConnection *c;
2378 struct CadetFlowControl *fc;
2382 GCC_check_connections ();
2383 log_message (&msg->header, peer, &msg->cid);
2384 c = connection_get (&msg->cid);
2387 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2389 LOG (GNUNET_ERROR_TYPE_DEBUG,
2390 "POLL message on unknown connection %s!\n",
2391 GNUNET_h2s (GC_h2hc (&msg->cid)));
2392 send_broken_unknown (&msg->cid,
2396 GCC_check_connections ();
2400 /* Is this a forward or backward ACK?
2401 * Note: a poll should never be needed in a loopback case,
2402 * since there is no possiblility of packet loss there, so
2403 * this way of discerining FWD/BCK should not be a problem.
2405 if (get_next_hop (c) == peer)
2407 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2410 else if (get_prev_hop (c) == peer)
2412 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2417 GNUNET_break_op (0);
2421 pid = ntohl (msg->pid);
2422 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2423 fc->last_pid_recv = pid;
2424 fwd = fc == &c->bck_fc;
2425 GCC_send_ack (c, fwd, GNUNET_YES);
2426 GCC_check_connections ();
2431 * Check the message against internal state and test if it goes FWD or BCK.
2433 * Updates the PID, state and timeout values for the connection.
2435 * @param message Message to check. It must belong to an existing connection.
2436 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2437 * @param c Connection this message should belong. If NULL, check fails.
2438 * @param sender Neighbor that sent the message.
2440 * @return #GNUNET_YES if the message goes FWD.
2441 * #GNUNET_NO if it goes BCK.
2442 * #GNUNET_SYSERR if there is an error (unauthorized sender, ...).
2445 check_message (const struct GNUNET_MessageHeader *message,
2446 const struct GNUNET_CADET_Hash* cid,
2447 struct CadetConnection *c,
2448 struct CadetPeer *sender,
2451 struct CadetFlowControl *fc;
2452 struct CadetPeer *hop;
2456 /* Check connection */
2459 GNUNET_STATISTICS_update (stats,
2460 "# unknown connection",
2462 LOG (GNUNET_ERROR_TYPE_DEBUG,
2463 "%s on unknown connection %s\n",
2464 GC_m2s (ntohs (message->type)),
2465 GNUNET_h2s (GC_h2hc (cid)));
2466 send_broken_unknown (cid,
2470 return GNUNET_SYSERR;
2473 /* Check if origin is as expected */
2474 hop = get_prev_hop (c);
2481 hop = get_next_hop (c);
2482 GNUNET_break (hop == c->next_peer);
2489 /* Unexpected peer sending traffic on a connection. */
2490 GNUNET_break_op (0);
2491 return GNUNET_SYSERR;
2495 /* Check PID for payload messages */
2496 type = ntohs (message->type);
2497 if (GNUNET_MESSAGE_TYPE_CADET_AX == type)
2499 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2500 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2501 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2502 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2504 GNUNET_break_op (0);
2505 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2506 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2507 pid, fc->last_pid_recv, fc->last_ack_sent);
2508 return GNUNET_SYSERR;
2510 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2514 delta = pid - fc->last_pid_recv;
2515 fc->last_pid_recv = pid;
2516 fc->recv_bitmap <<= delta;
2517 fc->recv_bitmap |= 1;
2521 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2522 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2524 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2525 pid, fc->last_pid_recv - 31);
2526 return GNUNET_SYSERR;
2528 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2532 /* Count as connection confirmation. */
2533 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2535 connection_change_state (c, CADET_CONNECTION_READY);
2538 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2539 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2542 connection_reset_timeout (c, fwd);
2549 * Handler for key exchange traffic (Axolotl KX).
2551 * @param peer Message sender (neighbor).
2552 * @param msg Message itself.
2555 GCC_handle_kx (struct CadetPeer *peer,
2556 const struct GNUNET_CADET_KX *msg)
2558 const struct GNUNET_CADET_Hash* cid;
2559 struct CadetConnection *c;
2562 GCC_check_connections ();
2564 log_message (&msg->header, peer, cid);
2566 c = connection_get (cid);
2567 fwd = check_message (&msg->header,
2573 /* If something went wrong, discard message. */
2574 if (GNUNET_SYSERR == fwd)
2576 GNUNET_break_op (0);
2577 GCC_check_connections ();
2581 /* Is this message for us? */
2582 if (GCC_is_terminal (c, fwd))
2584 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2585 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2591 GCT_handle_kx (c->t, &msg[1].header);
2592 GCC_check_connections ();
2596 /* Message not for us: forward to next hop */
2597 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2598 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2599 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2600 GNUNET_NO, NULL, NULL));
2601 GCC_check_connections ();
2606 * Handler for encrypted cadet network traffic (channel mgmt, data).
2608 * @param peer Message sender (neighbor).
2609 * @param msg Message itself.
2612 GCC_handle_encrypted (struct CadetPeer *peer,
2613 const struct GNUNET_CADET_AX *msg)
2615 const struct GNUNET_CADET_Hash* cid;
2616 struct CadetConnection *c;
2620 GCC_check_connections ();
2622 pid = ntohl (msg->pid);
2623 log_message (&msg->header, peer, cid);
2625 c = connection_get (cid);
2626 fwd = check_message (&msg->header,
2632 /* If something went wrong, discard message. */
2633 if (GNUNET_SYSERR == fwd)
2635 GNUNET_break_op (0);
2636 GCC_check_connections ();
2640 /* Is this message for us? */
2641 if (GCC_is_terminal (c, fwd))
2643 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2647 GNUNET_break (GNUNET_NO != c->destroy);
2650 GCT_handle_encrypted (c->t, &msg->header);
2651 GCC_send_ack (c, fwd, GNUNET_NO);
2652 GCC_check_connections ();
2656 /* Message not for us: forward to next hop */
2657 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2658 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2659 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2660 GNUNET_NO, NULL, NULL));
2661 GCC_check_connections ();
2666 * Initialize the connections subsystem
2668 * @param c Configuration handle.
2671 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2673 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2675 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2678 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2679 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2680 GNUNET_SCHEDULER_shutdown ();
2685 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2688 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2689 "CADET", "MAX_CONNECTIONS", "MISSING");
2690 GNUNET_SCHEDULER_shutdown ();
2695 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2696 &refresh_connection_time))
2698 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2699 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2700 GNUNET_SCHEDULER_shutdown ();
2703 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2704 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2709 * Destroy each connection on shutdown.
2711 * @param cls Closure (unused).
2712 * @param key Current key code (CID, unused).
2713 * @param value Value in the hash map (`struct CadetConnection`)
2715 * @return #GNUNET_YES, because we should continue to iterate
2718 shutdown_iterator (void *cls,
2719 const struct GNUNET_HashCode *key,
2722 struct CadetConnection *c = value;
2724 c->state = CADET_CONNECTION_DESTROYED;
2731 * Shut down the connections subsystem.
2736 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n");
2737 GCC_check_connections ();
2738 GNUNET_CONTAINER_multihashmap_iterate (connections,
2741 GNUNET_CONTAINER_multihashmap_destroy (connections);
2747 * Create a connection.
2749 * @param cid Connection ID (either created locally or imposed remotely).
2750 * @param t Tunnel this connection belongs to (or NULL for transit connections);
2751 * @param path Path this connection has to use (copy is made).
2752 * @param own_pos Own position in the @c path path.
2754 * @return Newly created connection.
2755 * NULL in case of error: own id not in path, wrong neighbors, ...
2757 struct CadetConnection *
2758 GCC_new (const struct GNUNET_CADET_Hash *cid,
2759 struct CadetTunnel *t,
2760 struct CadetPeerPath *path,
2761 unsigned int own_pos)
2763 struct CadetConnection *c;
2764 struct CadetPeerPath *cpath;
2766 GCC_check_connections ();
2767 cpath = path_duplicate (path);
2768 GNUNET_assert (NULL != cpath);
2769 c = GNUNET_new (struct CadetConnection);
2771 GNUNET_assert (GNUNET_OK ==
2772 GNUNET_CONTAINER_multihashmap_put (connections,
2774 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2775 fc_init (&c->fwd_fc);
2776 fc_init (&c->bck_fc);
2781 GNUNET_assert (own_pos <= cpath->length - 1);
2782 c->own_pos = own_pos;
2785 if (GNUNET_OK != register_neighbors (c))
2789 /* We were the origin of this request, this means we have invalid
2790 * info about the paths to reach the destination. We must invalidate
2791 * the *original* path to avoid trying it again in the next minute.
2793 if (2 < path->length)
2794 path_invalidate (path);
2798 GCT_debug(t, GNUNET_ERROR_TYPE_WARNING);
2802 path_destroy (c->path);
2807 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2808 GCC_check_connections ();
2814 * Connection is no longer needed: destroy it.
2816 * Cancels all pending traffic (including possible DESTROY messages), all
2817 * maintenance tasks and removes the connection from neighbor peers and tunnel.
2819 * @param c Connection to destroy.
2822 GCC_destroy (struct CadetConnection *c)
2824 GCC_check_connections ();
2831 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2832 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2835 LOG (GNUNET_ERROR_TYPE_DEBUG,
2836 "destroying connection %s\n",
2838 LOG (GNUNET_ERROR_TYPE_DEBUG,
2839 " fc's f: %p, b: %p\n",
2840 &c->fwd_fc, &c->bck_fc);
2841 LOG (GNUNET_ERROR_TYPE_DEBUG,
2842 " fc tasks f: %u, b: %u\n",
2843 c->fwd_fc.poll_task,
2844 c->bck_fc.poll_task);
2846 /* Cancel all traffic */
2847 if (NULL != c->path)
2849 connection_cancel_queues (c, GNUNET_YES);
2850 connection_cancel_queues (c, GNUNET_NO);
2852 unregister_neighbors (c);
2853 path_destroy (c->path);
2856 /* Delete from tunnel */
2858 GCT_remove_connection (c->t, c);
2860 if (NULL != c->check_duplicates_task)
2861 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
2862 if (NULL != c->fwd_maintenance_task)
2863 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2864 if (NULL != c->bck_maintenance_task)
2865 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2867 if (GNUNET_NO == c->was_removed)
2869 GNUNET_break (GNUNET_YES ==
2870 GNUNET_CONTAINER_multihashmap_remove (connections,
2874 GNUNET_STATISTICS_update (stats,
2879 GCC_check_connections ();
2884 * Get the connection ID.
2886 * @param c Connection to get the ID from.
2888 * @return ID of the connection.
2890 const struct GNUNET_CADET_Hash *
2891 GCC_get_id (const struct CadetConnection *c)
2898 * Get the connection ID.
2900 * @param c Connection to get the ID from.
2902 * @return ID of the connection.
2904 const struct GNUNET_HashCode *
2905 GCC_get_h (const struct CadetConnection *c)
2907 return GC_h2hc (&c->id);
2912 * Get the connection path.
2914 * @param c Connection to get the path from.
2916 * @return path used by the connection.
2918 const struct CadetPeerPath *
2919 GCC_get_path (const struct CadetConnection *c)
2921 if (GNUNET_NO == c->destroy)
2928 * Get the connection state.
2930 * @param c Connection to get the state from.
2932 * @return state of the connection.
2934 enum CadetConnectionState
2935 GCC_get_state (const struct CadetConnection *c)
2941 * Get the connection tunnel.
2943 * @param c Connection to get the tunnel from.
2945 * @return tunnel of the connection.
2947 struct CadetTunnel *
2948 GCC_get_tunnel (const struct CadetConnection *c)
2955 * Get free buffer space in a connection.
2957 * @param c Connection.
2958 * @param fwd Is query about FWD traffic?
2960 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2963 GCC_get_buffer (struct CadetConnection *c, int fwd)
2965 struct CadetFlowControl *fc;
2967 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2969 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
2970 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
2971 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
2973 return (fc->queue_max - fc->queue_n);
2978 * Get how many messages have we allowed to send to us from a direction.
2980 * @param c Connection.
2981 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2983 * @return last_ack_sent - last_pid_recv
2986 GCC_get_allowed (struct CadetConnection *c, int fwd)
2988 struct CadetFlowControl *fc;
2990 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2991 if (CADET_CONNECTION_READY != c->state
2992 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
2996 return (fc->last_ack_sent - fc->last_pid_recv);
3001 * Get messages queued in a connection.
3003 * @param c Connection.
3004 * @param fwd Is query about FWD traffic?
3006 * @return Number of messages queued.
3009 GCC_get_qn (struct CadetConnection *c, int fwd)
3011 struct CadetFlowControl *fc;
3013 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3020 * Get next PID to use.
3022 * @param c Connection.
3023 * @param fwd Is query about FWD traffic?
3025 * @return Next PID to use.
3028 GCC_get_pid (struct CadetConnection *c, int fwd)
3030 struct CadetFlowControl *fc;
3033 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3041 * Allow the connection to advertise a buffer of the given size.
3043 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3044 * allowing up to last_pid_recv + buffer.
3046 * @param c Connection.
3047 * @param buffer How many more messages the connection can accept.
3048 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3051 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3053 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3054 GCC_2s (c), buffer, GC_f2s (fwd));
3055 send_ack (c, buffer, fwd, GNUNET_NO);
3060 * Notify other peers on a connection of a broken link. Mark connections
3061 * to destroy after all traffic has been sent.
3063 * @param c Connection on which there has been a disconnection.
3064 * @param peer Peer that disconnected.
3067 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
3069 struct CadetFlowControl *fc;
3073 GCC_check_connections ();
3074 strncpy (peer_name, GCP_2s (peer), 16);
3075 peer_name[15] = '\0';
3076 LOG (GNUNET_ERROR_TYPE_DEBUG,
3077 "shutting down %s, %s disconnected\n",
3078 GCC_2s (c), peer_name);
3080 invalidate_paths (c, peer);
3082 fwd = is_fwd (c, peer);
3083 if (GNUNET_SYSERR == fwd)
3088 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3089 (GNUNET_NO != c->destroy) )
3091 /* Local shutdown, or other peer already down (hence 'c->destroy');
3092 so there is no one to notify about this, just clean up. */
3094 GCC_check_connections ();
3097 /* Mark FlowControl towards the peer as unavaliable. */
3098 fc = fwd ? &c->bck_fc : &c->fwd_fc;
3101 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3103 /* Connection will have at least one pending message
3104 * (the one we just scheduled), so delay destruction
3105 * and remove from map so we don't use accidentally. */
3107 GNUNET_assert (GNUNET_NO == c->was_removed);
3108 c->was_removed = GNUNET_YES;
3109 GNUNET_break (GNUNET_YES ==
3110 GNUNET_CONTAINER_multihashmap_remove (connections,
3113 /* Cancel queue in the direction that just died. */
3114 connection_cancel_queues (c, ! fwd);
3115 GCC_stop_poll (c, ! fwd);
3116 unregister_neighbors (c);
3117 GCC_check_connections ();
3122 * Is this peer the first one on the connection?
3124 * @param c Connection.
3125 * @param fwd Is this about fwd traffic?
3127 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3130 GCC_is_origin (struct CadetConnection *c, int fwd)
3132 if (!fwd && c->path->length - 1 == c->own_pos )
3134 if (fwd && 0 == c->own_pos)
3141 * Is this peer the last one on the connection?
3143 * @param c Connection.
3144 * @param fwd Is this about fwd traffic?
3145 * Note that the ROOT is the terminal for BCK traffic!
3147 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3150 GCC_is_terminal (struct CadetConnection *c, int fwd)
3152 return GCC_is_origin (c, ! fwd);
3157 * See if we are allowed to send by the next hop in the given direction.
3159 * @param c Connection.
3160 * @param fwd Is this about fwd traffic?
3162 * @return #GNUNET_YES in case it's OK to send.
3165 GCC_is_sendable (struct CadetConnection *c, int fwd)
3167 struct CadetFlowControl *fc;
3169 LOG (GNUNET_ERROR_TYPE_DEBUG,
3170 " checking sendability of %s traffic on %s\n",
3171 GC_f2s (fwd), GCC_2s (c));
3177 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3178 LOG (GNUNET_ERROR_TYPE_DEBUG,
3179 " last ack recv: %u, last pid sent: %u\n",
3180 fc->last_ack_recv, fc->last_pid_sent);
3181 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3183 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3186 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3192 * Check if this connection is a direct one (never trim a direct connection).
3194 * @param c Connection.
3196 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3199 GCC_is_direct (struct CadetConnection *c)
3201 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3206 * Sends an already built message on a connection, properly registering
3207 * all used resources.
3209 * @param message Message to send. Function makes a copy of it.
3210 * If message is not hop-by-hop, decrements TTL of copy.
3211 * @param payload_type Type of payload, in case the message is encrypted.
3212 * @param payload_id ID of the payload (PID, ACK, ...).
3213 * @param c Connection on which this message is transmitted.
3214 * @param fwd Is this a fwd message?
3215 * @param force Force the connection to accept the message (buffer overfill).
3216 * @param cont Continuation called once message is sent. Can be NULL.
3217 * @param cont_cls Closure for @c cont.
3219 * @return Handle to cancel the message before it's sent.
3220 * NULL on error or if @c cont is NULL.
3221 * Invalid on @c cont call.
3223 struct CadetConnectionQueue *
3224 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3225 uint16_t payload_type, uint32_t payload_id,
3226 struct CadetConnection *c, int fwd, int force,
3227 GCC_sent cont, void *cont_cls)
3229 struct GNUNET_CADET_AX *axmsg;
3230 struct GNUNET_CADET_KX *kmsg;
3231 struct GNUNET_CADET_ACK *amsg;
3232 struct GNUNET_CADET_Poll *pmsg;
3233 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3234 struct GNUNET_CADET_ConnectionBroken *bmsg;
3235 struct CadetFlowControl *fc;
3236 struct CadetConnectionQueue *q;
3237 struct GNUNET_MessageHeader *copy;
3242 GCC_check_connections ();
3243 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3244 if (0 == fc->queue_max)
3250 size = ntohs (message->size);
3251 copy = GNUNET_malloc (size);
3252 GNUNET_memcpy (copy, message, size);
3253 type = ntohs (message->type);
3254 LOG (GNUNET_ERROR_TYPE_INFO,
3255 "--> %s (%s %4u) on conn %s (%p) %s [%5u]\n",
3256 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), c,
3258 droppable = (GNUNET_NO == force);
3261 case GNUNET_MESSAGE_TYPE_CADET_AX:
3262 axmsg = (struct GNUNET_CADET_AX *) copy;
3264 axmsg->pid = htonl (GCC_get_pid (c, fwd));
3265 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3266 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3267 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3268 if (GNUNET_YES == droppable)
3274 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3278 case GNUNET_MESSAGE_TYPE_CADET_KX:
3279 kmsg = (struct GNUNET_CADET_KX *) copy;
3283 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3284 amsg = (struct GNUNET_CADET_ACK *) copy;
3286 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3287 droppable = GNUNET_NO;
3290 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3291 pmsg = (struct GNUNET_CADET_Poll *) copy;
3293 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
3294 droppable = GNUNET_NO;
3297 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3298 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) copy;
3302 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3303 bmsg = (struct GNUNET_CADET_ConnectionBroken *) copy;
3307 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3308 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3309 GNUNET_break (0); /* Should've used specific functions. */
3318 if (fc->queue_n > fc->queue_max && droppable)
3320 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3323 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3324 fc->queue_n, fc->queue_max);
3325 if (GNUNET_MESSAGE_TYPE_CADET_AX == type)
3330 return NULL; /* Drop this message */
3333 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3334 GCC_2s (c), c->pending_messages);
3335 c->pending_messages++;
3337 q = GNUNET_new (struct CadetConnectionQueue);
3338 q->forced = !droppable;
3339 q->peer_q = GCP_send (get_hop (c, fwd), copy,
3340 payload_type, payload_id,
3342 &conn_message_sent, q);
3343 if (NULL == q->peer_q)
3345 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3348 GCC_check_connections ();
3352 q->cont_cls = cont_cls;
3353 GCC_check_connections ();
3354 return (NULL == cont) ? NULL : q;
3359 * Cancel a previously sent message while it's in the queue.
3361 * ONLY can be called before the continuation given to the send function
3362 * is called. Once the continuation is called, the message is no longer in the
3365 * @param q Handle to the queue.
3368 GCC_cancel (struct CadetConnectionQueue *q)
3370 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3372 /* send_cancel calls message_sent, which calls q->cont and frees q */
3373 GCP_send_cancel (q->peer_q);
3374 GCC_check_connections ();
3379 * Sends a CREATE CONNECTION message for a path to a peer.
3380 * Changes the connection and tunnel states if necessary.
3382 * @param c Connection to create.
3385 GCC_send_create (struct CadetConnection *c)
3387 enum CadetTunnelCState state;
3390 GCC_check_connections ();
3391 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3392 size += c->path->length * sizeof (struct GNUNET_PeerIdentity);
3394 /* Allocate message on the stack */
3395 unsigned char cbuf[size];
3396 struct GNUNET_CADET_ConnectionCreate *msg;
3397 struct GNUNET_PeerIdentity *peers;
3399 msg = (struct GNUNET_CADET_ConnectionCreate *) cbuf;
3400 msg->header.size = htons (size);
3401 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
3402 msg->cid = *GCC_get_id (c);
3403 peers = (struct GNUNET_PeerIdentity *) &msg[1];
3404 for (int i = 0; i < c->path->length; i++)
3406 GNUNET_PEER_resolve (c->path->peers[i], peers++);
3408 GNUNET_assert (NULL == c->maintenance_q);
3409 c->maintenance_q = GCP_send (get_next_hop (c),
3411 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0,
3413 &conn_message_sent, NULL);
3416 LOG (GNUNET_ERROR_TYPE_INFO, "==> %s %19s on conn %s (%p) FWD [%5u]\n",
3417 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE), "",
3418 GCC_2s (c), c, size);
3419 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3420 c, c->pending_messages);
3421 c->pending_messages++;
3423 state = GCT_get_cstate (c->t);
3424 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3425 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
3426 if (CADET_CONNECTION_NEW == c->state)
3427 connection_change_state (c, CADET_CONNECTION_SENT);
3428 GCC_check_connections ();
3433 * Send an ACK on the appropriate connection/channel, depending on
3434 * the direction and the position of the peer.
3436 * @param c Which connection to send the hop-by-hop ACK.
3437 * @param fwd Is this a fwd ACK? (will go dest->root).
3438 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
3441 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
3443 unsigned int buffer;
3445 GCC_check_connections ();
3446 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
3447 GC_f2s (fwd), GCC_2s (c));
3455 if (GNUNET_NO != c->destroy)
3457 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
3458 GCC_check_connections ();
3462 /* Get available buffer space */
3463 if (GCC_is_terminal (c, fwd))
3465 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
3466 buffer = GCT_get_channels_buffer (c->t);
3470 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
3471 buffer = GCC_get_buffer (c, fwd);
3473 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
3474 if (0 == buffer && GNUNET_NO == force)
3476 GCC_check_connections ();
3480 /* Send available buffer space */
3481 if (GNUNET_YES == GCC_is_origin (c, fwd))
3483 GNUNET_assert (NULL != c->t);
3484 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
3485 GCT_unchoke_channels (c->t);
3489 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
3490 send_ack (c, buffer, fwd, force);
3492 GCC_check_connections ();
3497 * Send a message to all peers in this connection that the connection
3498 * is no longer valid.
3500 * If some peer should not receive the message, it should be zero'ed out
3501 * before calling this function.
3503 * @param c The connection whose peers to notify.
3506 GCC_send_destroy (struct CadetConnection *c)
3508 struct GNUNET_CADET_ConnectionDestroy msg;
3510 if (GNUNET_YES == c->destroy)
3512 GCC_check_connections ();
3513 msg.header.size = htons (sizeof (msg));
3514 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3516 LOG (GNUNET_ERROR_TYPE_DEBUG,
3517 " sending connection destroy for connection %s\n",
3520 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3521 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, UINT16_MAX,
3523 GNUNET_YES, NULL, NULL));
3524 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3525 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, UINT16_MAX,
3527 GNUNET_YES, NULL, NULL));
3529 GCC_check_connections ();
3534 * @brief Start a polling timer for the connection.
3536 * When a neighbor does not accept more traffic on the connection it could be
3537 * caused by a simple congestion or by a lost ACK. Polling enables to check
3538 * for the lastest ACK status for a connection.
3540 * @param c Connection.
3541 * @param fwd Should we poll in the FWD direction?
3544 GCC_start_poll (struct CadetConnection *c, int fwd)
3546 struct CadetFlowControl *fc;
3548 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3549 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3551 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3553 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL already in progress (t: %p, m: %p)\n",
3554 fc->poll_task, fc->poll_msg);
3557 if (0 == fc->queue_max)
3559 /* Should not be needed, traffic should've been cancelled. */
3561 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not possible, peer disconnected\n");
3564 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3565 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3572 * @brief Stop polling a connection for ACKs.
3574 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3576 * @param c Connection.
3577 * @param fwd Should we stop the poll in the FWD direction?
3580 GCC_stop_poll (struct CadetConnection *c, int fwd)
3582 struct CadetFlowControl *fc;
3584 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3585 if (NULL != fc->poll_task)
3587 GNUNET_SCHEDULER_cancel (fc->poll_task);
3588 fc->poll_task = NULL;
3590 if (NULL != fc->poll_msg)
3592 GCC_cancel (fc->poll_msg);
3593 fc->poll_msg = NULL;
3599 * Get a (static) string for a connection.
3601 * @param c Connection.
3604 GCC_2s (const struct CadetConnection *c)
3611 static char buf[128];
3613 SPRINTF (buf, "%s (->%s)",
3614 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3617 return GNUNET_h2s (GC_h2hc (&c->id));
3622 * Log all possible info about the connection state.
3624 * @param c Connection to debug.
3625 * @param level Debug level to use.
3628 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3633 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3635 __FILE__, __FUNCTION__, __LINE__);
3641 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3645 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3646 s = path_2s (c->path);
3647 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3649 LOG2 (level, "CCC state: %s, destroy: %u\n",
3650 GCC_state2s (c->state), c->destroy);
3651 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3652 if (NULL != c->perf)
3653 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3655 LOG2 (level, "CCC FWD flow control:\n");
3656 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3657 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3658 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3659 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3660 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3661 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3662 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3663 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3665 LOG2 (level, "CCC BCK flow control:\n");
3666 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3667 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3668 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3669 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3670 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3671 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3672 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3673 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3675 LOG2 (level, "CCC DEBUG CONNECTION END\n");