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 last packet sent towards the peer.
81 uint32_t last_pid_sent;
84 * ID of the last packet received from the peer.
86 uint32_t last_pid_recv;
89 * Bitmap of past 32 messages received:
90 * - LSB being @c last_pid_recv.
91 * - MSB being @c last_pid_recv - 31 (mod UINTMAX).
96 * Last ACK sent to the peer (peer can't send more than this PID).
98 uint32_t last_ack_sent;
101 * Last ACK sent towards the origin (for traffic towards leaf node).
103 uint32_t last_ack_recv;
106 * Task to poll the peer in case of a lost ACK causes stall.
108 struct GNUNET_SCHEDULER_Task *poll_task;
111 * How frequently to poll for ACKs.
113 struct GNUNET_TIME_Relative poll_time;
116 * Queued poll message, to cancel if not necessary anymore (got ACK).
118 struct CadetConnectionQueue *poll_msg;
121 * Queued poll message, to cancel if not necessary anymore (got ACK).
123 struct CadetConnectionQueue *ack_msg;
127 * Keep a record of the last messages sent on this connection.
129 struct CadetConnectionPerformance
132 * Circular buffer for storing measurements.
134 double usecsperbyte[AVG_MSGS];
137 * Running average of @c usecsperbyte.
142 * How many values of @c usecsperbyte are valid.
147 * Index of the next "free" position in @c usecsperbyte.
154 * Struct containing all information regarding a connection to a peer.
156 struct CadetConnection
159 * Tunnel this connection is part of.
161 struct CadetTunnel *t;
164 * Flow control information for traffic fwd.
166 struct CadetFlowControl fwd_fc;
169 * Flow control information for traffic bck.
171 struct CadetFlowControl bck_fc;
174 * Measure connection performance on the endpoint.
176 struct CadetConnectionPerformance *perf;
179 * ID of the connection.
181 struct GNUNET_CADET_Hash id;
184 * Path being used for the tunnel. At the origin of the connection
185 * it's a pointer to the destination's path pool, otherwise just a copy.
187 struct CadetPeerPath *path;
190 * Task to keep the used paths alive at the owner,
191 * time tunnel out on all the other peers.
193 struct GNUNET_SCHEDULER_Task *fwd_maintenance_task;
196 * Task to keep the used paths alive at the destination,
197 * time tunnel out on all the other peers.
199 struct GNUNET_SCHEDULER_Task *bck_maintenance_task;
202 * Queue handle for maintainance traffic. One handle for FWD and BCK since
203 * one peer never needs to maintain both directions (no loopback connections).
205 struct CadetPeerQueue *maintenance_q;
208 * Should equal #get_next_hop(), or NULL if that peer disconnected.
210 struct CadetPeer *next_peer;
213 * Should equal #get_prev_hop(), or NULL if that peer disconnected.
215 struct CadetPeer *prev_peer;
218 * State of the connection.
220 enum CadetConnectionState state;
223 * Position of the local peer in the path.
225 unsigned int own_pos;
228 * Pending message count.
230 unsigned int pending_messages;
234 * - if 0, connection in use.
235 * - if 1, destroy on last message.
236 * - if 2, connection is being destroyed don't re-enter.
241 * In-connection-map flag. Sometimes, when @e destroy is set but
242 * actual destruction is delayed to enable us to finish processing
243 * queues (i.e. in the direction that is still working), we remove
244 * the connection from the map to prevent it from still being
245 * found (and used) by accident. This flag is set to #GNUNET_YES
246 * for a connection that is not in the #connections map. Should
247 * only be #GNUNET_YES if #destroy is also non-zero.
252 * Counter to do exponential backoff when creating a connection (max 64).
254 unsigned short create_retry;
257 * Task to check if connection has duplicates.
259 struct GNUNET_SCHEDULER_Task *check_duplicates_task;
264 * Handle for messages queued but not yet sent.
266 struct CadetConnectionQueue
269 * Peer queue handle, to cancel if necessary.
271 struct CadetPeerQueue *q;
274 * Continuation to call once sent.
279 * Closure for @e cont.
284 * Was this a forced message? (Do not account for it)
289 /******************************************************************************/
290 /******************************* GLOBALS ***********************************/
291 /******************************************************************************/
294 * Global handle to the statistics service.
296 extern struct GNUNET_STATISTICS_Handle *stats;
299 * Local peer own ID (memory efficient handle).
301 extern GNUNET_PEER_Id myid;
304 * Local peer own ID (full value).
306 extern struct GNUNET_PeerIdentity my_full_id;
309 * Connections known, indexed by cid (CadetConnection).
311 static struct GNUNET_CONTAINER_MultiHashMap *connections;
314 * How many connections are we willing to maintain.
315 * Local connections are always allowed, even if there are more connections than max.
317 static unsigned long long max_connections;
320 * How many messages *in total* are we willing to queue, divide by number of
321 * connections to get connection queue size.
323 static unsigned long long max_msgs_queue;
326 * How often to send path keepalives. Paths timeout after 4 missed.
328 static struct GNUNET_TIME_Relative refresh_connection_time;
331 * How often to send path create / ACKs.
333 static struct GNUNET_TIME_Relative create_connection_time;
336 /******************************************************************************/
337 /******************************** STATIC ***********************************/
338 /******************************************************************************/
342 #if 0 // avoid compiler warning for unused static function
344 fc_debug (struct CadetFlowControl *fc)
346 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
347 fc->last_pid_recv, fc->last_ack_sent);
348 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
349 fc->last_pid_sent, fc->last_ack_recv);
350 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
351 fc->queue_n, fc->queue_max);
355 connection_debug (struct CadetConnection *c)
359 LOG (GNUNET_ERROR_TYPE_INFO, "DEBUG NULL CONNECTION\n");
362 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
363 peer2s (c->t->peer), GCC_2s (c));
364 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
365 c->state, c->pending_messages);
366 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
367 fc_debug (&c->fwd_fc);
368 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
369 fc_debug (&c->bck_fc);
375 * Schedule next keepalive task, taking in consideration
376 * the connection state and number of retries.
378 * @param c Connection for which to schedule the next keepalive.
379 * @param fwd Direction for the next keepalive.
382 schedule_next_keepalive (struct CadetConnection *c, int fwd);
386 * Resets the connection timeout task, some other message has done the
388 * - For the first peer on the direction this means to send
389 * a keepalive or a path confirmation message (either create or ACK).
390 * - For all other peers, this means to destroy the connection,
391 * due to lack of activity.
392 * Starts the timeout if no timeout was running (connection just created).
394 * @param c Connection whose timeout to reset.
395 * @param fwd Is this forward?
398 connection_reset_timeout (struct CadetConnection *c, int fwd);
402 * Get string description for tunnel state. Reentrant.
404 * @param s Tunnel state.
406 * @return String representation.
409 GCC_state2s (enum CadetConnectionState s)
413 case CADET_CONNECTION_NEW:
414 return "CADET_CONNECTION_NEW";
415 case CADET_CONNECTION_SENT:
416 return "CADET_CONNECTION_SENT";
417 case CADET_CONNECTION_ACK:
418 return "CADET_CONNECTION_ACK";
419 case CADET_CONNECTION_READY:
420 return "CADET_CONNECTION_READY";
421 case CADET_CONNECTION_DESTROYED:
422 return "CADET_CONNECTION_DESTROYED";
423 case CADET_CONNECTION_BROKEN:
424 return "CADET_CONNECTION_BROKEN";
427 LOG (GNUNET_ERROR_TYPE_ERROR, " conn state %u unknown!\n", s);
428 return "CADET_CONNECTION_STATE_ERROR";
434 * Initialize a Flow Control structure to the initial state.
436 * @param fc Flow Control structure to initialize.
439 fc_init (struct CadetFlowControl *fc)
441 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
442 fc->last_pid_recv = (uint32_t) -1;
443 fc->last_ack_sent = (uint32_t) 0;
444 fc->last_ack_recv = (uint32_t) 0;
445 fc->poll_task = NULL;
446 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
448 fc->queue_max = (max_msgs_queue / max_connections) + 1;
455 * @param cid Connection ID.
457 * @return conntection with the given ID @cid or NULL if not found.
459 static struct CadetConnection *
460 connection_get (const struct GNUNET_CADET_Hash *cid)
462 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
467 * Change the connection state. Cannot change a connection marked as destroyed.
469 * @param c Connection to change.
470 * @param state New state to set.
473 connection_change_state (struct CadetConnection* c,
474 enum CadetConnectionState state)
476 LOG (GNUNET_ERROR_TYPE_DEBUG,
477 "Connection %s state %s -> %s\n",
478 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
479 if (CADET_CONNECTION_DESTROYED <= c->state) /* Destroyed or broken. */
481 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
485 if (CADET_CONNECTION_READY == state)
491 * Mark a connection as "destroyed", to send all pending traffic and freeing
492 * all associated resources, without accepting new status changes on it.
494 * @param c Connection to mark as destroyed.
497 mark_destroyed (struct CadetConnection *c)
499 c->destroy = GNUNET_YES;
500 connection_change_state (c, CADET_CONNECTION_DESTROYED);
505 * Callback called when a queued ACK message is sent.
507 * @param cls Closure (FC).
508 * @param c Connection this message was on.
509 * @param q Queue handler this call invalidates.
510 * @param type Type of message sent.
511 * @param fwd Was this a FWD going message?
512 * @param size Size of the message.
516 struct CadetConnection *c,
517 struct CadetConnectionQueue *q,
518 uint16_t type, int fwd, size_t size)
520 struct CadetFlowControl *fc = cls;
527 * Send an ACK on the connection, informing the predecessor about
528 * the available buffer space. Should not be called in case the peer
529 * is origin (no predecessor) in the @c fwd direction.
531 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
532 * the ACK itself goes "back" (dest->root).
534 * @param c Connection on which to send the ACK.
535 * @param buffer How much space free to advertise?
536 * @param fwd Is this FWD ACK? (Going dest -> root)
537 * @param force Don't optimize out.
540 send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
542 struct CadetFlowControl *next_fc;
543 struct CadetFlowControl *prev_fc;
544 struct GNUNET_CADET_ACK msg;
548 /* If origin, there is no connection to send ACKs. Wrong function! */
549 GCC_check_connections ();
550 if (GCC_is_origin (c, fwd))
552 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
553 GCC_2s (c), GC_f2s (fwd));
558 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
559 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
561 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
562 GC_f2s (fwd), GCC_2s (c));
564 /* Check if we need to transmit the ACK. */
565 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
566 if (3 < delta && buffer < delta && GNUNET_NO == force)
568 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
569 LOG (GNUNET_ERROR_TYPE_DEBUG,
570 " last pid recv: %u, last ack sent: %u\n",
571 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
572 GCC_check_connections ();
576 /* Ok, ACK might be necessary, what PID to ACK? */
577 ack = prev_fc->last_pid_recv + buffer;
578 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 " last pid %u, last ack %u, qmax %u, q %u\n",
581 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
582 next_fc->queue_max, next_fc->queue_n);
583 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
585 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
586 GCC_check_connections ();
590 /* Check if message is already in queue */
591 if (NULL != prev_fc->ack_msg)
593 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
595 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
596 GCC_cancel (prev_fc->ack_msg);
597 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
601 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
602 GCC_check_connections ();
607 prev_fc->last_ack_sent = ack;
609 /* Build ACK message and send on conn */
610 msg.header.size = htons (sizeof (msg));
611 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
612 msg.ack = htonl (ack);
615 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, UINT16_MAX, ack,
618 GNUNET_assert (NULL != prev_fc->ack_msg);
619 GCC_check_connections ();
624 * Callback called when a connection queued message is sent.
626 * Calculates the average time and connection packet tracking.
628 * @param cls Closure (ConnectionQueue Handle).
629 * @param c Connection this message was on.
630 * @param sent Was it really sent? (Could have been canceled)
631 * @param type Type of message sent.
632 * @param pid Packet ID, or 0 if not applicable (create, destroy, etc).
633 * @param fwd Was this a FWD going message?
634 * @param size Size of the message.
635 * @param wait Time spent waiting for core (only the time for THIS message)
636 * @return #GNUNET_YES if connection was destroyed, #GNUNET_NO otherwise.
639 conn_message_sent (void *cls,
640 struct CadetConnection *c, int sent,
641 uint16_t type, uint32_t pid, int fwd, size_t size,
642 struct GNUNET_TIME_Relative wait)
644 struct CadetConnectionPerformance *p;
645 struct CadetFlowControl *fc;
646 struct CadetConnectionQueue *q = cls;
650 GCC_check_connections ();
651 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
653 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
655 fc = fwd ? &c->fwd_fc : &c->bck_fc;
656 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
657 sent ? "" : "not ", GC_f2s (fwd), GC_m2s (type), pid);
663 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
664 q->cont (q->cont_cls, c, q, type, fwd, size);
668 else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED
669 || type == GNUNET_MESSAGE_TYPE_CADET_AX)
671 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
680 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
681 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
683 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
686 GCC_check_connections ();
689 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
690 c->pending_messages--;
691 if ( (GNUNET_YES == c->destroy) &&
692 (0 == c->pending_messages) )
694 LOG (GNUNET_ERROR_TYPE_DEBUG,
695 "! destroying connection!\n");
697 GCC_check_connections ();
700 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
703 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
704 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
705 c->maintenance_q = NULL;
706 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
707 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
708 schedule_next_keepalive (c, fwd);
711 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
712 case GNUNET_MESSAGE_TYPE_CADET_AX:
713 if (GNUNET_YES == sent)
715 GNUNET_assert (NULL != q);
716 fc->last_pid_sent = pid;
717 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
718 GCC_start_poll (c, fwd);
719 GCC_send_ack (c, fwd, GNUNET_NO);
720 connection_reset_timeout (c, fwd);
723 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
724 if (GNUNET_NO == forced)
727 LOG (GNUNET_ERROR_TYPE_DEBUG,
728 "! accounting pid %u\n",
733 LOG (GNUNET_ERROR_TYPE_DEBUG,
734 "! forced, Q_N not accounting pid %u\n",
739 case GNUNET_MESSAGE_TYPE_CADET_KX:
740 if (GNUNET_YES == sent)
741 connection_reset_timeout (c, fwd);
744 case GNUNET_MESSAGE_TYPE_CADET_POLL:
748 case GNUNET_MESSAGE_TYPE_CADET_ACK:
752 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
753 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
757 LOG (GNUNET_ERROR_TYPE_ERROR, "%s unknown\n", GC_m2s (type));
761 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
764 return GNUNET_NO; /* Only endpoints are interested in timing. */
767 usecsperbyte = ((double) wait.rel_value_us) / size;
768 if (p->size == AVG_MSGS)
770 /* Array is full. Substract oldest value, add new one and store. */
771 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
772 p->usecsperbyte[p->idx] = usecsperbyte;
773 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
777 /* Array not yet full. Add current value to avg and store. */
778 p->usecsperbyte[p->idx] = usecsperbyte;
780 p->avg += p->usecsperbyte[p->idx];
784 p->idx = (p->idx + 1) % AVG_MSGS;
785 GCC_check_connections ();
791 * Get the previous hop in a connection
793 * @param c Connection.
795 * @return Previous peer in the connection.
797 static struct CadetPeer *
798 get_prev_hop (const struct CadetConnection *c)
804 LOG (GNUNET_ERROR_TYPE_DEBUG,
805 " get prev hop %s [%u/%u]\n",
806 GCC_2s (c), c->own_pos, c->path->length);
807 if (0 == c->own_pos || c->path->length < 2)
808 id = c->path->peers[0];
810 id = c->path->peers[c->own_pos - 1];
812 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
813 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
815 return GCP_get_short (id, GNUNET_YES);
820 * Get the next hop in a connection
822 * @param c Connection.
824 * @return Next peer in the connection.
826 static struct CadetPeer *
827 get_next_hop (const struct CadetConnection *c)
834 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
835 GCC_2s (c), c->own_pos, c->path->length);
836 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
837 id = c->path->peers[c->path->length - 1];
839 id = c->path->peers[c->own_pos + 1];
841 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
842 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
844 return GCP_get_short (id, GNUNET_YES);
849 * Check that the direct neighbours (previous and next hop)
850 * are properly associated with this connection.
852 * @param c connection to check
855 check_neighbours (const struct CadetConnection *c)
858 return; /* nothing to check */
859 GCP_check_connection (get_next_hop (c), c);
860 GCP_check_connection (get_prev_hop (c), c);
865 * Helper for #GCC_check_connections(). Calls #check_neighbours().
869 * @param value the `struct CadetConnection` to check
870 * @return #GNUNET_OK (continue to iterate)
873 check_connection (void *cls,
874 const struct GNUNET_HashCode *key,
877 struct CadetConnection *c = value;
879 check_neighbours (c);
885 * Check invariants for all connections using #check_neighbours().
888 GCC_check_connections ()
890 if (0 == CHECK_INVARIANTS)
892 if (NULL == connections)
894 GNUNET_CONTAINER_multihashmap_iterate (connections,
901 * Get the hop in a connection.
903 * @param c Connection.
904 * @param fwd Next in the FWD direction?
906 * @return Next peer in the connection.
908 static struct CadetPeer *
909 get_hop (struct CadetConnection *c, int fwd)
911 return (fwd) ? get_next_hop (c) : get_prev_hop (c);
916 * Get a bit mask for a message received out-of-order.
918 * @param last_pid_recv Last PID we received prior to the out-of-order.
919 * @param ooo_pid PID of the out-of-order message.
922 get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
924 return 1 << (last_pid_recv - ooo_pid);
929 * Check is an out-of-order message is ok:
930 * - at most 31 messages behind.
933 * @param last_pid_recv Last in-order PID received.
936 is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
940 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
943 mask = get_recv_bitmask (last_pid_recv, ooo_pid);
944 if (0 != (ooo_bitmap & mask))
952 * Is traffic coming from this sender 'FWD' traffic?
954 * @param c Connection to check.
955 * @param sender Peer identity of neighbor.
957 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
958 * the traffic is 'FWD'.
959 * #GNUNET_NO for BCK.
960 * #GNUNET_SYSERR for errors.
963 is_fwd (const struct CadetConnection *c,
964 const struct GNUNET_PeerIdentity *sender)
968 id = GNUNET_PEER_search (sender);
969 if (GCP_get_short_id (get_prev_hop (c)) == id)
972 if (GCP_get_short_id (get_next_hop (c)) == id)
976 return GNUNET_SYSERR;
981 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
982 * or a first CONNECTION_ACK directed to us.
984 * @param connection Connection to confirm.
985 * @param fwd Should we send it FWD? (root->dest)
986 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
989 send_connection_ack (struct CadetConnection *connection, int fwd)
991 struct CadetTunnel *t;
992 size_t size = sizeof (struct GNUNET_CADET_ConnectionACK);
994 GCC_check_connections ();
996 LOG (GNUNET_ERROR_TYPE_INFO,
997 "==> { C %s ACK} %19s on conn %s (%p) %s [%5u]\n",
998 GC_f2s (!fwd), "", GCC_2s (connection), connection, GC_f2s (fwd), size);
999 GCP_queue_add (get_hop (connection, fwd), NULL,
1000 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, UINT16_MAX, 0,
1001 size, connection, fwd, &conn_message_sent, NULL);
1002 connection->pending_messages++;
1003 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
1004 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
1005 if (CADET_CONNECTION_READY != connection->state)
1006 connection_change_state (connection, CADET_CONNECTION_SENT);
1007 GCC_check_connections ();
1012 * Send a notification that a connection is broken.
1014 * @param c Connection that is broken.
1015 * @param id1 Peer that has disconnected.
1016 * @param id2 Peer that has disconnected.
1017 * @param fwd Direction towards which to send it.
1020 send_broken (struct CadetConnection *c,
1021 const struct GNUNET_PeerIdentity *id1,
1022 const struct GNUNET_PeerIdentity *id2,
1025 struct GNUNET_CADET_ConnectionBroken msg;
1027 GCC_check_connections ();
1028 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1029 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1033 GNUNET_assert (NULL ==
1034 GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c, fwd,
1035 GNUNET_YES, NULL, NULL));
1036 GCC_check_connections ();
1041 * Send a notification that a connection is broken, when a connection
1042 * isn't even known to the local peer or soon to be destroyed.
1044 * @param connection_id Connection ID.
1045 * @param id1 Peer that has disconnected, probably local peer.
1046 * @param id2 Peer that has disconnected can be NULL if unknown.
1047 * @param peer Peer to notify (neighbor who sent the connection).
1050 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
1051 const struct GNUNET_PeerIdentity *id1,
1052 const struct GNUNET_PeerIdentity *id2,
1053 const struct GNUNET_PeerIdentity *peer_id)
1055 struct GNUNET_CADET_ConnectionBroken *msg;
1056 struct CadetPeerQueue *q;
1057 struct CadetPeer *neighbor;
1059 GCC_check_connections ();
1060 LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n",
1061 GNUNET_h2s (GC_h2hc (connection_id)));
1063 msg = GNUNET_new (struct GNUNET_CADET_ConnectionBroken);
1064 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1065 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1066 msg->cid = *connection_id;
1071 memset (&msg->peer2, 0, sizeof (msg->peer2));
1072 neighbor = GCP_get (peer_id, GNUNET_NO); /* We MUST know neighbor. */
1073 GNUNET_assert (NULL != neighbor);
1074 q = GCP_queue_add (neighbor, msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
1076 sizeof (struct GNUNET_CADET_ConnectionBroken),
1077 NULL, GNUNET_SYSERR, /* connection, fwd */
1078 NULL, NULL); /* continuation */
1079 GNUNET_assert (NULL != q);
1080 GCC_check_connections ();
1085 * Send keepalive packets for a connection.
1087 * @param c Connection to keep alive..
1088 * @param fwd Is this a FWD keepalive? (owner -> dest).
1091 send_connection_keepalive (struct CadetConnection *c, int fwd)
1093 struct GNUNET_MessageHeader msg;
1094 struct CadetFlowControl *fc;
1096 GCC_check_connections ();
1097 LOG (GNUNET_ERROR_TYPE_INFO,
1098 "keepalive %s for connection %s\n",
1099 GC_f2s (fwd), GCC_2s (c));
1101 GNUNET_assert (NULL != c->t);
1102 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1103 if (0 < fc->queue_n || GNUNET_YES == GCT_has_queued_traffic (c->t))
1105 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1109 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1111 GNUNET_assert (NULL != c->t);
1112 msg.size = htons (sizeof (msg));
1113 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
1115 GNUNET_assert (NULL ==
1116 GCT_send_prebuilt_message (&msg, c->t, c,
1117 GNUNET_NO, NULL, NULL));
1118 GCC_check_connections ();
1123 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1125 * @param c Connection for which to send the message.
1126 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1129 connection_recreate (struct CadetConnection *c, int fwd)
1131 LOG (GNUNET_ERROR_TYPE_DEBUG,
1132 "sending connection recreate\n");
1134 GCC_send_create (c);
1136 send_connection_ack (c, GNUNET_NO);
1141 * Generic connection timer management.
1142 * Depending on the role of the peer in the connection will send the
1143 * appropriate message (build or keepalive)
1145 * @param c Conncetion to maintain.
1146 * @param fwd Is FWD?
1149 connection_maintain (struct CadetConnection *c, int fwd)
1151 if (GNUNET_NO != c->destroy)
1153 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1160 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1164 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1166 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1168 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1169 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1170 schedule_next_keepalive (c, fwd);
1175 case CADET_CONNECTION_NEW:
1178 case CADET_CONNECTION_SENT:
1179 connection_recreate (c, fwd);
1181 case CADET_CONNECTION_READY:
1182 send_connection_keepalive (c, fwd);
1191 * Keep the connection alive.
1193 * @param c Connection to keep alive.
1194 * @param fwd Direction.
1195 * @param shutdown Are we shutting down? (Don't send traffic)
1196 * Non-zero value for true, not necessarily GNUNET_YES.
1199 connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
1201 GCC_check_connections ();
1202 LOG (GNUNET_ERROR_TYPE_DEBUG,
1203 "%s keepalive for %s\n",
1204 GC_f2s (fwd), GCC_2s (c));
1207 c->fwd_maintenance_task = NULL;
1209 c->bck_maintenance_task = NULL;
1211 if (GNUNET_NO != shutdown)
1214 connection_maintain (c, fwd);
1215 GCC_check_connections ();
1216 /* Next execution will be scheduled by message_sent or _maintain*/
1221 * Keep the connection alive in the FWD direction.
1223 * @param cls Closure (connection to keepalive).
1224 * @param tc TaskContext.
1227 connection_fwd_keepalive (void *cls,
1228 const struct GNUNET_SCHEDULER_TaskContext *tc)
1230 GCC_check_connections ();
1231 connection_keepalive ((struct CadetConnection *) cls,
1233 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1234 GCC_check_connections ();
1239 * Keep the connection alive in the BCK direction.
1241 * @param cls Closure (connection to keepalive).
1242 * @param tc TaskContext.
1245 connection_bck_keepalive (void *cls,
1246 const struct GNUNET_SCHEDULER_TaskContext *tc)
1248 GCC_check_connections ();
1249 connection_keepalive ((struct CadetConnection *) cls,
1251 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1252 GCC_check_connections ();
1257 * Schedule next keepalive task, taking in consideration
1258 * the connection state and number of retries.
1260 * If the peer is not the origin, do nothing.
1262 * @param c Connection for which to schedule the next keepalive.
1263 * @param fwd Direction for the next keepalive.
1266 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1268 struct GNUNET_TIME_Relative delay;
1269 struct GNUNET_SCHEDULER_Task * *task_id;
1270 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1272 GCC_check_connections ();
1273 if (GNUNET_NO == GCC_is_origin (c, fwd))
1276 /* Calculate delay to use, depending on the state of the connection */
1277 if (CADET_CONNECTION_READY == c->state)
1279 delay = refresh_connection_time;
1283 if (1 > c->create_retry)
1284 c->create_retry = 1;
1285 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1287 if (c->create_retry < 64)
1288 c->create_retry *= 2;
1291 /* Select direction-dependent parameters */
1292 if (GNUNET_YES == fwd)
1294 task_id = &c->fwd_maintenance_task;
1295 keepalive_task = &connection_fwd_keepalive;
1299 task_id = &c->bck_maintenance_task;
1300 keepalive_task = &connection_bck_keepalive;
1303 /* Check that no one scheduled it before us */
1304 if (NULL != *task_id)
1306 /* No need for a _break. It can happen for instance when sending a SYNACK
1307 * for a duplicate SYN: the first SYNACK scheduled the task. */
1308 GNUNET_SCHEDULER_cancel (*task_id);
1311 /* Schedule the task */
1312 *task_id = GNUNET_SCHEDULER_add_delayed (delay,
1315 LOG (GNUNET_ERROR_TYPE_DEBUG,
1316 "next keepalive in %s\n",
1317 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1318 GCC_check_connections ();
1323 * @brief Re-initiate traffic on this connection if necessary.
1325 * Check if there is traffic queued towards this peer
1326 * and the core transmit handle is NULL (traffic was stalled).
1327 * If so, call core tmt rdy.
1329 * @param c Connection on which initiate traffic.
1330 * @param fwd Is this about fwd traffic?
1333 connection_unlock_queue (struct CadetConnection *c, int fwd)
1335 struct CadetPeer *peer;
1337 GCC_check_connections ();
1338 LOG (GNUNET_ERROR_TYPE_DEBUG,
1339 "connection_unlock_queue %s on %s\n",
1340 GC_f2s (fwd), GCC_2s (c));
1342 if (GCC_is_terminal (c, fwd))
1344 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal, can unlock!\n");
1348 peer = get_hop (c, fwd);
1349 GCP_queue_unlock (peer, c);
1350 GCC_check_connections ();
1355 * Cancel all transmissions that belong to a certain connection.
1357 * If the connection is scheduled for destruction and no more messages are left,
1358 * the connection will be destroyed by the continuation call.
1360 * @param c Connection which to cancel. Might be destroyed during this call.
1361 * @param fwd Cancel fwd traffic?
1364 connection_cancel_queues (struct CadetConnection *c,
1367 struct CadetFlowControl *fc;
1368 struct CadetPeer *peer;
1370 GCC_check_connections ();
1371 LOG (GNUNET_ERROR_TYPE_DEBUG,
1372 "Cancel %s queues for connection %s\n",
1373 GC_f2s (fwd), GCC_2s (c));
1380 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1381 if (NULL != fc->poll_task)
1383 GNUNET_SCHEDULER_cancel (fc->poll_task);
1384 fc->poll_task = NULL;
1385 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL task for fc %p\n", fc);
1387 if (NULL != fc->poll_msg)
1389 GCC_cancel (fc->poll_msg);
1390 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL msg for fc %p\n", fc);
1392 peer = get_hop (c, fwd);
1393 GCP_queue_cancel (peer, c);
1394 GCC_check_connections ();
1399 * Function called if a connection has been stalled for a while,
1400 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1402 * @param cls Closure (poll ctx).
1403 * @param tc TaskContext.
1406 connection_poll (void *cls,
1407 const struct GNUNET_SCHEDULER_TaskContext *tc);
1411 * Callback called when a queued POLL message is sent.
1413 * @param cls Closure (flow control context).
1414 * @param c Connection this message was on.
1415 * @param q Queue handler this call invalidates.
1416 * @param type Type of message sent.
1417 * @param fwd Was this a FWD going message?
1418 * @param size Size of the message.
1421 poll_sent (void *cls,
1422 struct CadetConnection *c,
1423 struct CadetConnectionQueue *q,
1424 uint16_t type, int fwd, size_t size)
1426 struct CadetFlowControl *fc = cls;
1428 GNUNET_assert (fc->poll_msg == q);
1429 fc->poll_msg = NULL;
1430 if (2 == c->destroy)
1432 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1435 if (0 == fc->queue_max)
1437 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL cancelled: neighbor disconnected\n");
1440 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1442 GNUNET_assert (NULL == fc->poll_task);
1443 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1444 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1447 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1452 * Function called if a connection has been stalled for a while,
1453 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1455 * @param cls Closure (poll ctx).
1456 * @param tc TaskContext.
1459 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1461 struct CadetFlowControl *fc = cls;
1462 struct GNUNET_CADET_Poll msg;
1463 struct CadetConnection *c;
1466 fc->poll_task = NULL;
1467 GCC_check_connections ();
1468 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1474 fwd = fc == &c->fwd_fc;
1475 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1476 GCC_2s (c), GC_f2s (fwd));
1478 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1479 msg.header.size = htons (sizeof (msg));
1480 msg.pid = htonl (fc->last_pid_sent);
1481 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1483 GCC_send_prebuilt_message (&msg.header, UINT16_MAX, fc->last_pid_sent, c,
1484 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1485 GNUNET_assert (NULL != fc->poll_msg);
1486 GCC_check_connections ();
1491 * Resend all queued messages for a connection on other connections of the
1492 * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
1494 * @param c Connection whose messages to resend.
1495 * @param fwd Resend fwd messages?
1498 resend_messages_and_destroy (struct CadetConnection *c, int fwd)
1500 struct GNUNET_MessageHeader *out_msg;
1501 struct CadetTunnel *t = c->t;
1502 struct CadetPeer *neighbor;
1503 unsigned int pending;
1506 GCC_check_connections ();
1509 destroyed = GNUNET_NO;
1510 neighbor = get_hop (c, fwd);
1511 pending = c->pending_messages;
1513 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1516 GCT_resend_message (out_msg, t);
1517 GNUNET_free (out_msg);
1520 /* All pending messages should have been popped,
1521 * and the connection destroyed by the continuation.
1523 if (GNUNET_YES != destroyed)
1528 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1529 if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1533 GCC_check_connections ();
1538 * Generic connection timeout implementation.
1540 * Timeout function due to lack of keepalive/traffic from an endpoint.
1541 * Destroys connection if called.
1543 * @param c Connection to destroy.
1544 * @param fwd Was the timeout from the origin? (FWD timeout)
1547 connection_timeout (struct CadetConnection *c, int fwd)
1549 struct CadetFlowControl *reverse_fc;
1551 GCC_check_connections ();
1552 reverse_fc = fwd ? &c->bck_fc : &c->fwd_fc;
1554 LOG (GNUNET_ERROR_TYPE_INFO,
1555 "Connection %s %s timed out. Destroying.\n",
1558 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1560 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1566 /* If dest, salvage queued traffic. */
1567 if (GCC_is_origin (c, !fwd))
1569 const struct GNUNET_PeerIdentity *next_hop;
1571 next_hop = GCP_get_id (fwd ? get_prev_hop (c) : get_next_hop (c));
1572 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1573 if (0 < reverse_fc->queue_n)
1574 resend_messages_and_destroy (c, !fwd);
1575 GCC_check_connections ();
1580 GCC_check_connections ();
1585 * Timeout function due to lack of keepalive/traffic from the owner.
1586 * Destroys connection if called.
1588 * @param cls Closure (connection to destroy).
1589 * @param tc TaskContext.
1592 connection_fwd_timeout (void *cls,
1593 const struct GNUNET_SCHEDULER_TaskContext *tc)
1595 struct CadetConnection *c = cls;
1597 c->fwd_maintenance_task = NULL;
1598 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1600 GCC_check_connections ();
1601 connection_timeout (c, GNUNET_YES);
1602 GCC_check_connections ();
1607 * Timeout function due to lack of keepalive/traffic from the destination.
1608 * Destroys connection if called.
1610 * @param cls Closure (connection to destroy).
1611 * @param tc TaskContext
1614 connection_bck_timeout (void *cls,
1615 const struct GNUNET_SCHEDULER_TaskContext *tc)
1617 struct CadetConnection *c = cls;
1619 c->bck_maintenance_task = NULL;
1620 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1622 GCC_check_connections ();
1623 connection_timeout (c, GNUNET_NO);
1624 GCC_check_connections ();
1629 * Resets the connection timeout task, some other message has done the
1631 * - For the first peer on the direction this means to send
1632 * a keepalive or a path confirmation message (either create or ACK).
1633 * - For all other peers, this means to destroy the connection,
1634 * due to lack of activity.
1635 * Starts the timeout if no timeout was running (connection just created).
1637 * @param c Connection whose timeout to reset.
1638 * @param fwd Is this forward?
1640 * TODO use heap to improve efficiency of scheduler.
1643 connection_reset_timeout (struct CadetConnection *c, int fwd)
1645 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1646 if (GCC_is_origin (c, fwd)) /* Startpoint */
1648 schedule_next_keepalive (c, fwd);
1650 else /* Relay, endpoint. */
1652 struct GNUNET_TIME_Relative delay;
1653 struct GNUNET_SCHEDULER_Task * *ti;
1654 GNUNET_SCHEDULER_TaskCallback f;
1656 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1659 GNUNET_SCHEDULER_cancel (*ti);
1660 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1661 LOG (GNUNET_ERROR_TYPE_DEBUG,
1662 " timing out in %s\n",
1663 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1664 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1665 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1671 * Iterator to compare each connection's path with the path of a new connection.
1673 * If the connection coincides, the c member of path is set to the connection
1674 * and the destroy flag of the connection is set.
1676 * @param cls Closure (new path).
1677 * @param c Connection in the tunnel to check.
1680 check_path (void *cls, struct CadetConnection *c)
1682 struct CadetConnection *new_conn = cls;
1683 struct CadetPeerPath *path = new_conn->path;
1685 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1686 GCC_2s (c), c, c->path->length);
1689 && GNUNET_NO == c->destroy
1690 && CADET_CONNECTION_BROKEN != c->state
1691 && CADET_CONNECTION_DESTROYED != c->state
1692 && path_equivalent (path, c->path))
1694 new_conn->destroy = GNUNET_YES; /* Do not mark_destroyed, */
1695 new_conn->path->c = c; /* this is only a flag for the Iterator. */
1696 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1702 * Finds out if this path is already being used by an existing connection.
1704 * Checks the tunnel towards the destination to see if it contains
1705 * any connection with the same path.
1707 * If the existing connection is ready, it is kept.
1708 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1709 * the peer will eventually reject our attempt).
1711 * @param path Path to check.
1712 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1713 * #GNUNET_NO otherwise.
1716 does_connection_exist (struct CadetConnection *conn)
1718 struct CadetPeer *p;
1719 struct CadetTunnel *t;
1720 struct CadetConnection *c;
1722 p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
1725 t = GCP_get_tunnel (p);
1729 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1731 GCT_iterate_connections (t, &check_path, conn);
1733 if (GNUNET_YES == conn->destroy)
1736 conn->destroy = GNUNET_NO;
1737 conn->path->c = conn;
1738 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1739 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1740 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1741 if (CADET_CONNECTION_READY == c->state)
1743 /* The other peer confirmed a live connection with this path,
1744 * why are they trying to duplicate it? */
1745 GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
1748 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not valid, connection unique\n");
1753 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1760 * @brief Check if the tunnel this connection belongs to has any other
1761 * connection with the same path, and destroy one if so.
1763 * @param cls Closure (connection to check).
1764 * @param tc Task context.
1767 check_duplicates (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1769 struct CadetConnection *c = cls;
1771 c->check_duplicates_task = NULL;
1772 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1775 if (GNUNET_YES == does_connection_exist (c))
1777 GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1778 send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
1786 * Wait for enough time to let any dead connections time out and check for
1787 * any remaining duplicates.
1789 * @param c Connection that is a potential duplicate.
1792 schedule_check_duplicates (struct CadetConnection *c)
1794 struct GNUNET_TIME_Relative delay;
1796 if (NULL != c->check_duplicates_task)
1799 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 5);
1800 c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
1808 * Add the connection to the list of both neighbors.
1810 * @param c Connection.
1812 * @return #GNUNET_OK if everything went fine
1813 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1816 register_neighbors (struct CadetConnection *c)
1818 c->next_peer = get_next_hop (c);
1819 c->prev_peer = get_prev_hop (c);
1820 GNUNET_assert (c->next_peer != c->prev_peer);
1821 LOG (GNUNET_ERROR_TYPE_DEBUG,
1822 "register neighbors for connection %s\n",
1824 path_debug (c->path);
1825 LOG (GNUNET_ERROR_TYPE_DEBUG,
1826 "own pos %u\n", c->own_pos);
1827 LOG (GNUNET_ERROR_TYPE_DEBUG,
1828 "putting connection %s to next peer %p\n",
1831 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1833 GCP_2s (c->next_peer));
1834 LOG (GNUNET_ERROR_TYPE_DEBUG,
1835 "putting connection %s to prev peer %p\n",
1838 LOG (GNUNET_ERROR_TYPE_DEBUG,
1839 "prev peer %p %s\n",
1841 GCP_2s (c->prev_peer));
1843 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1844 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1846 if (GCC_is_origin (c, GNUNET_YES))
1847 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1848 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1850 LOG (GNUNET_ERROR_TYPE_DEBUG,
1851 " register neighbors failed\n");
1852 LOG (GNUNET_ERROR_TYPE_DEBUG,
1853 " prev: %s, neighbor?: %d\n",
1854 GCP_2s (c->prev_peer),
1855 GCP_is_neighbor (c->prev_peer));
1856 LOG (GNUNET_ERROR_TYPE_DEBUG,
1857 " next: %s, neighbor?: %d\n",
1858 GCP_2s (c->next_peer),
1859 GCP_is_neighbor (c->next_peer));
1860 return GNUNET_SYSERR;
1862 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1863 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1870 * Remove the connection from the list of both neighbors.
1872 * @param c Connection.
1875 unregister_neighbors (struct CadetConnection *c)
1877 // struct CadetPeer *peer; FIXME dont use next_peer, prev_peer
1878 /* Either already unregistered or never got registered, it's ok either way. */
1879 if (NULL == c->path)
1881 if (NULL != c->next_peer)
1883 GCP_remove_connection (c->next_peer, c);
1884 c->next_peer = NULL;
1886 if (NULL != c->prev_peer)
1888 GCP_remove_connection (c->prev_peer, c);
1889 c->prev_peer = NULL;
1895 * Invalidates all paths towards all peers that comprise the connection which
1896 * rely on the disconnected peer.
1898 * ~O(n^3) (peers in connection * paths/peer * links/path)
1900 * @param c Connection whose peers' paths to clean.
1901 * @param disconnected Peer that disconnected.
1904 invalidate_paths (struct CadetConnection *c, struct CadetPeer *disconnected)
1906 struct CadetPeer *peer;
1909 for (i = 0; i < c->path->length; i++)
1911 peer = GCP_get_short (c->path->peers[i], GNUNET_NO);
1913 GCP_notify_broken_link (peer, &my_full_id, GCP_get_id (disconnected));
1919 * Bind the connection to the peer and the tunnel to that peer.
1921 * If the peer has no tunnel, create one. Update tunnel and connection
1922 * data structres to reflect new status.
1924 * @param c Connection.
1928 add_to_peer (struct CadetConnection *c,
1929 struct CadetPeer *peer)
1931 GCP_add_tunnel (peer);
1932 c->t = GCP_get_tunnel (peer);
1933 GCT_add_connection (c->t, c);
1938 * Log receipt of message on stderr (INFO level).
1940 * @param message Message received.
1941 * @param peer Peer who sent the message.
1942 * @param hash Connection ID.
1945 log_message (const struct GNUNET_MessageHeader *message,
1946 const struct GNUNET_PeerIdentity *peer,
1947 const struct GNUNET_CADET_Hash *hash)
1953 size = ntohs (message->size);
1954 type = ntohs (message->type);
1957 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1958 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1959 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1960 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1966 LOG (GNUNET_ERROR_TYPE_INFO, "<%s %s on conn %s from %s, %6u bytes\n",
1967 arrow, GC_m2s (type), GNUNET_h2s (GC_h2hc (hash)),
1968 GNUNET_i2s (peer), (unsigned int) size);
1971 /******************************************************************************/
1972 /******************************** API ***********************************/
1973 /******************************************************************************/
1976 * Core handler for connection creation.
1978 * @param cls Closure (unused).
1979 * @param peer Sender (neighbor).
1980 * @param message Message.
1981 * @return #GNUNET_OK to keep the connection open,
1982 * #GNUNET_SYSERR to close it (signal serious error)
1985 GCC_handle_create (void *cls,
1986 const struct GNUNET_PeerIdentity *peer,
1987 const struct GNUNET_MessageHeader *message)
1989 struct GNUNET_CADET_ConnectionCreate *msg;
1990 struct GNUNET_PeerIdentity *id;
1991 struct GNUNET_CADET_Hash *cid;
1992 struct CadetPeerPath *path;
1993 struct CadetPeer *dest_peer;
1994 struct CadetPeer *orig_peer;
1995 struct CadetConnection *c;
1996 unsigned int own_pos;
1999 GCC_check_connections ();
2001 size = ntohs (message->size);
2002 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
2004 GNUNET_break_op (0);
2008 /* Calculate hops */
2009 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
2010 if (size % sizeof (struct GNUNET_PeerIdentity))
2012 GNUNET_break_op (0);
2015 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
2017 GNUNET_break_op (0);
2020 size /= sizeof (struct GNUNET_PeerIdentity);
2023 GNUNET_break_op (0);
2026 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2028 /* Get parameters */
2029 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
2031 log_message (message, peer, cid);
2032 id = (struct GNUNET_PeerIdentity *) &msg[1];
2033 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
2035 /* Create connection */
2036 c = connection_get (cid);
2039 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
2040 size, myid, &own_pos);
2043 /* Path was malformed, probably our own ID was not in it. */
2044 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
2045 GNUNET_break_op (0);
2051 /* We received this request from a neighbor, we cannot be origin */
2052 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
2053 GNUNET_break_op (0);
2054 path_destroy (path);
2058 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2059 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
2060 c = GCC_new (cid, NULL, path, own_pos);
2063 if (path->length - 1 == own_pos)
2065 /* If we are destination, why did the creation fail? */
2067 path_destroy (path);
2068 GCC_check_connections ();
2071 send_broken_unknown (cid, &my_full_id,
2072 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
2074 path_destroy (path);
2075 GCC_check_connections ();
2078 GCP_add_path_to_all (path, GNUNET_NO);
2079 connection_reset_timeout (c, GNUNET_YES);
2083 path = path_duplicate (c->path);
2085 if (CADET_CONNECTION_NEW == c->state)
2086 connection_change_state (c, CADET_CONNECTION_SENT);
2088 /* Remember peers */
2089 dest_peer = GCP_get (&id[size - 1], GNUNET_YES);
2090 orig_peer = GCP_get (&id[0], GNUNET_YES);
2092 /* Is it a connection to us? */
2093 if (c->own_pos == path->length - 1)
2095 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2096 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
2098 add_to_peer (c, orig_peer);
2099 if (GNUNET_YES == does_connection_exist (c))
2101 /* Peer created a connection equal to one we think exists
2103 * Solution: Keep both and postpone disambiguation. In the meantime
2104 * the connection will time out or peer will inform us it is broken.
2107 * - Use explicit duplicate.
2108 * - Accept new conn and destroy the old. (interruption in higher level)
2109 * - Keep the one with higher ID / created by peer with higher ID. */
2110 schedule_check_duplicates (c);
2113 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
2114 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
2116 send_connection_ack (c, GNUNET_NO);
2117 if (CADET_CONNECTION_SENT == c->state)
2118 connection_change_state (c, CADET_CONNECTION_ACK);
2122 /* It's for somebody else! Retransmit. */
2123 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2124 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2125 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2126 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
2127 GNUNET_YES, GNUNET_YES,
2130 path_destroy (path);
2131 GCC_check_connections ();
2137 * Core handler for path confirmations.
2139 * @param cls closure
2140 * @param message message
2141 * @param peer peer identity this notification is about
2142 * @return #GNUNET_OK to keep the connection open,
2143 * #GNUNET_SYSERR to close it (signal serious error)
2146 GCC_handle_confirm (void *cls,
2147 const struct GNUNET_PeerIdentity *peer,
2148 const struct GNUNET_MessageHeader *message)
2150 struct GNUNET_CADET_ConnectionACK *msg;
2151 struct CadetConnection *c;
2152 struct CadetPeerPath *p;
2153 struct CadetPeer *pi;
2154 enum CadetConnectionState oldstate;
2157 GCC_check_connections ();
2158 msg = (struct GNUNET_CADET_ConnectionACK *) message;
2159 log_message (message, peer, &msg->cid);
2160 c = connection_get (&msg->cid);
2163 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2165 LOG (GNUNET_ERROR_TYPE_DEBUG,
2166 " don't know the connection!\n");
2167 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2168 GCC_check_connections ();
2172 if (GNUNET_NO != c->destroy)
2174 GNUNET_assert (CADET_CONNECTION_DESTROYED == c->state);
2175 LOG (GNUNET_ERROR_TYPE_DEBUG,
2176 "connection %s being destroyed, ignoring confirm\n",
2178 GCC_check_connections ();
2182 oldstate = c->state;
2183 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
2184 pi = GCP_get (peer, GNUNET_YES);
2185 if (get_next_hop (c) == pi)
2187 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2189 if (CADET_CONNECTION_SENT == oldstate)
2190 connection_change_state (c, CADET_CONNECTION_ACK);
2192 else if (get_prev_hop (c) == pi)
2194 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2196 connection_change_state (c, CADET_CONNECTION_READY);
2200 GNUNET_break_op (0);
2204 connection_reset_timeout (c, fwd);
2206 /* Add path to peers? */
2210 GCP_add_path_to_all (p, GNUNET_YES);
2217 /* Message for us as creator? */
2218 if (GCC_is_origin (c, GNUNET_YES))
2220 if (GNUNET_NO != fwd)
2222 GNUNET_break_op (0);
2225 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2227 /* If just created, cancel the short timeout and start a long one */
2228 if (CADET_CONNECTION_SENT == oldstate)
2229 connection_reset_timeout (c, GNUNET_YES);
2231 /* Change connection state */
2232 connection_change_state (c, CADET_CONNECTION_READY);
2233 send_connection_ack (c, GNUNET_YES);
2235 /* Change tunnel state, trigger KX */
2236 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2237 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2238 GCC_check_connections ();
2242 /* Message for us as destination? */
2243 if (GCC_is_terminal (c, GNUNET_YES))
2245 if (GNUNET_YES != fwd)
2247 GNUNET_break_op (0);
2250 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2252 /* If just created, cancel the short timeout and start a long one */
2253 if (CADET_CONNECTION_ACK == oldstate)
2254 connection_reset_timeout (c, GNUNET_NO);
2256 /* Change tunnel state */
2257 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2258 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2259 GCC_check_connections ();
2263 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2264 GNUNET_assert (NULL ==
2265 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2266 GNUNET_YES, NULL, NULL));
2267 GCC_check_connections ();
2273 * Core handler for notifications of broken connections.
2275 * @param cls Closure (unused).
2276 * @param id Peer identity of sending neighbor.
2277 * @param message Message.
2278 * @return #GNUNET_OK to keep the connection open,
2279 * #GNUNET_SYSERR to close it (signal serious error)
2282 GCC_handle_broken (void* cls,
2283 const struct GNUNET_PeerIdentity* id,
2284 const struct GNUNET_MessageHeader* message)
2286 struct GNUNET_CADET_ConnectionBroken *msg;
2287 struct CadetConnection *c;
2288 struct CadetTunnel *t;
2292 GCC_check_connections ();
2293 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
2294 log_message (message, id, &msg->cid);
2295 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2296 GNUNET_i2s (&msg->peer1));
2297 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2298 GNUNET_i2s (&msg->peer2));
2299 c = connection_get (&msg->cid);
2302 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2303 GCC_check_connections ();
2309 fwd = is_fwd (c, id);
2311 if (GCC_is_terminal (c, fwd))
2313 struct CadetPeer *endpoint;
2317 /* A terminal connection should not have 't' set to NULL. */
2319 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2322 endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
2323 if (2 < c->path->length)
2324 path_invalidate (c->path);
2325 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2327 connection_change_state (c, CADET_CONNECTION_BROKEN);
2328 GCT_remove_connection (t, c);
2331 pending = c->pending_messages;
2333 resend_messages_and_destroy (c, !fwd);
2339 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2340 GNUNET_YES, NULL, NULL));
2341 connection_cancel_queues (c, !fwd);
2343 GCC_check_connections ();
2349 * Core handler for tunnel destruction
2351 * @param cls Closure (unused).
2352 * @param peer Peer identity of sending neighbor.
2353 * @param message Message.
2354 * @return #GNUNET_OK to keep the connection open,
2355 * #GNUNET_SYSERR to close it (signal serious error)
2358 GCC_handle_destroy (void *cls,
2359 const struct GNUNET_PeerIdentity *peer,
2360 const struct GNUNET_MessageHeader *message)
2362 const struct GNUNET_CADET_ConnectionDestroy *msg;
2363 struct CadetConnection *c;
2366 GCC_check_connections ();
2367 msg = (const struct GNUNET_CADET_ConnectionDestroy *) message;
2368 log_message (message, peer, &msg->cid);
2369 c = connection_get (&msg->cid);
2372 /* Probably already got the message from another path,
2373 * destroyed the tunnel and retransmitted to children.
2376 GNUNET_STATISTICS_update (stats,
2377 "# control on unknown connection",
2379 LOG (GNUNET_ERROR_TYPE_DEBUG,
2380 " connection unknown: already destroyed?\n");
2381 GCC_check_connections ();
2384 fwd = is_fwd (c, peer);
2385 if (GNUNET_SYSERR == fwd)
2387 GNUNET_break_op (0); /* FIXME */
2390 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2392 GNUNET_assert (NULL ==
2393 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2394 GNUNET_YES, NULL, NULL));
2396 else if (0 == c->pending_messages)
2398 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
2400 GCC_check_connections ();
2406 GCT_remove_connection (c->t, c);
2409 GCC_check_connections ();
2415 * Check the message against internal state and test if it goes FWD or BCK.
2417 * Updates the PID, state and timeout values for the connection.
2419 * @param message Message to check. It must belong to an existing connection.
2420 * @param minimum_size The message cannot be smaller than this value.
2421 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2422 * @param c Connection this message should belong. If NULL, check fails.
2423 * @param neighbor Neighbor that sent the message.
2426 check_message (const struct GNUNET_MessageHeader *message,
2427 size_t minimum_size,
2428 const struct GNUNET_CADET_Hash* cid,
2429 struct CadetConnection *c,
2430 const struct GNUNET_PeerIdentity *neighbor,
2433 GNUNET_PEER_Id neighbor_id;
2434 struct CadetFlowControl *fc;
2435 struct CadetPeer *hop;
2440 if (ntohs (message->size) < minimum_size)
2442 GNUNET_break_op (0);
2443 LOG (GNUNET_ERROR_TYPE_WARNING, "Size %u < %u\n",
2444 ntohs (message->size), minimum_size);
2445 return GNUNET_SYSERR;
2448 /* Check connection */
2451 GNUNET_STATISTICS_update (stats,
2452 "# unknown connection",
2454 LOG (GNUNET_ERROR_TYPE_DEBUG,
2455 "%s on unknown connection %s\n",
2456 GC_m2s (ntohs (message->type)),
2457 GNUNET_h2s (GC_h2hc (cid)));
2458 send_broken_unknown (cid,
2462 return GNUNET_SYSERR;
2465 /* Check if origin is as expected */
2466 neighbor_id = GNUNET_PEER_search (neighbor);
2467 hop = get_prev_hop (c);
2468 if (neighbor_id == GCP_get_short_id (hop))
2474 hop = get_next_hop (c);
2475 GNUNET_break (hop == c->next_peer);
2476 if (neighbor_id == GCP_get_short_id (hop))
2482 /* Unexpected peer sending traffic on a connection. */
2483 GNUNET_break_op (0);
2484 return GNUNET_SYSERR;
2488 /* Check PID for payload messages */
2489 type = ntohs (message->type);
2490 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
2491 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
2493 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2494 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2495 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2496 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2498 GNUNET_break_op (0);
2499 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2500 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2501 pid, fc->last_pid_recv, fc->last_ack_sent);
2502 return GNUNET_SYSERR;
2504 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2508 delta = pid - fc->last_pid_recv;
2509 fc->last_pid_recv = pid;
2510 fc->recv_bitmap <<= delta;
2511 fc->recv_bitmap |= 1;
2515 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2516 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2518 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2519 pid, fc->last_pid_recv - 31);
2520 return GNUNET_SYSERR;
2522 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2526 /* Count as connection confirmation. */
2527 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2529 connection_change_state (c, CADET_CONNECTION_READY);
2532 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2533 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2536 connection_reset_timeout (c, fwd);
2543 * Generic handler for cadet network encrypted traffic.
2545 * @param peer Peer identity this notification is about.
2546 * @param msg Encrypted message.
2547 * @return #GNUNET_OK to keep the connection open,
2548 * #GNUNET_SYSERR to close it (signal serious error)
2551 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
2552 const struct GNUNET_MessageHeader *message)
2554 const struct GNUNET_CADET_Encrypted *otr_msg;
2555 const struct GNUNET_CADET_AX *ax_msg;
2556 const struct GNUNET_CADET_Hash* cid;
2557 struct CadetConnection *c;
2558 size_t minimum_size;
2564 GCC_check_connections ();
2565 if (GNUNET_MESSAGE_TYPE_CADET_AX == ntohs (message->type))
2567 overhead = sizeof (struct GNUNET_CADET_AX);
2568 ax_msg = (const struct GNUNET_CADET_AX *) message;
2570 pid = ntohl (ax_msg->pid);
2575 overhead = sizeof (struct GNUNET_CADET_Encrypted);
2576 otr_msg = (const struct GNUNET_CADET_Encrypted *) message;
2577 cid = &otr_msg->cid;
2578 pid = ntohl (otr_msg->pid);
2581 log_message (message, peer, cid);
2583 minimum_size = sizeof (struct GNUNET_MessageHeader) + overhead;
2584 c = connection_get (cid);
2585 fwd = check_message (message,
2592 /* If something went wrong, discard message. */
2593 if (GNUNET_SYSERR == fwd)
2595 GCC_check_connections ();
2599 /* Is this message for us? */
2600 if (GCC_is_terminal (c, fwd))
2602 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2606 GNUNET_break (GNUNET_NO != c->destroy);
2609 GCT_handle_encrypted (c->t, message);
2610 GCC_send_ack (c, fwd, GNUNET_NO);
2611 GCC_check_connections ();
2615 /* Message not for us: forward to next hop */
2616 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2617 if (NULL != otr_msg) /* only otr has ttl */
2619 ttl = ntohl (otr_msg->ttl);
2620 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2623 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2624 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2625 GCC_send_ack (c, fwd, GNUNET_NO);
2626 GCC_check_connections ();
2631 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2632 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2633 GNUNET_NO, NULL, NULL));
2634 GCC_check_connections ();
2640 * Generic handler for cadet network encrypted traffic.
2642 * @param peer Peer identity this notification is about.
2643 * @param msg Encrypted message.
2644 * @return #GNUNET_OK to keep the connection open,
2645 * #GNUNET_SYSERR to close it (signal serious error)
2648 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2649 const struct GNUNET_CADET_KX *msg)
2651 const struct GNUNET_CADET_Hash* cid;
2652 struct CadetConnection *c;
2653 size_t expected_size;
2656 GCC_check_connections ();
2658 log_message (&msg->header, peer, cid);
2660 expected_size = sizeof (struct GNUNET_CADET_KX)
2661 + sizeof (struct GNUNET_MessageHeader);
2662 c = connection_get (cid);
2663 fwd = check_message (&msg->header,
2670 /* If something went wrong, discard message. */
2671 if (GNUNET_SYSERR == fwd)
2674 /* Is this message for us? */
2675 if (GCC_is_terminal (c, fwd))
2677 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2678 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2684 GCT_handle_kx (c->t, &msg[1].header);
2685 GCC_check_connections ();
2689 /* Message not for us: forward to next hop */
2690 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2691 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2692 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2693 GNUNET_NO, NULL, NULL));
2694 GCC_check_connections ();
2700 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2702 * @param cls Closure (unused).
2703 * @param message Message received.
2704 * @param peer Peer who sent the message.
2705 * @return #GNUNET_OK to keep the connection open,
2706 * #GNUNET_SYSERR to close it (signal serious error)
2709 GCC_handle_kx (void *cls,
2710 const struct GNUNET_PeerIdentity *peer,
2711 const struct GNUNET_MessageHeader *message)
2713 GCC_check_connections ();
2714 return handle_cadet_kx (peer, (struct GNUNET_CADET_KX *) message);
2719 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2721 * @param cls Closure (unused).
2722 * @param message Message received.
2723 * @param peer Peer who sent the message.
2724 * @return #GNUNET_OK to keep the connection open,
2725 * #GNUNET_SYSERR to close it (signal serious error)
2728 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2729 const struct GNUNET_MessageHeader *message)
2731 GCC_check_connections ();
2732 return handle_cadet_encrypted (peer, message);
2737 * Core handler for cadet network traffic point-to-point acks.
2739 * @param cls closure
2740 * @param message message
2741 * @param peer peer identity this notification is about
2742 * @return #GNUNET_OK to keep the connection open,
2743 * #GNUNET_SYSERR to close it (signal serious error)
2746 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2747 const struct GNUNET_MessageHeader *message)
2749 struct GNUNET_CADET_ACK *msg;
2750 struct CadetConnection *c;
2751 struct CadetFlowControl *fc;
2756 GCC_check_connections ();
2757 msg = (struct GNUNET_CADET_ACK *) message;
2758 log_message (message, peer, &msg->cid);
2759 c = connection_get (&msg->cid);
2762 GNUNET_STATISTICS_update (stats,
2763 "# ack on unknown connection",
2766 send_broken_unknown (&msg->cid,
2770 GCC_check_connections ();
2774 /* Is this a forward or backward ACK? */
2775 id = GNUNET_PEER_search (peer);
2776 if (GCP_get_short_id (get_next_hop (c)) == id)
2781 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2788 GNUNET_break_op (0);
2792 ack = ntohl (msg->ack);
2793 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2794 GC_f2s (fwd), ack, fc->last_ack_recv);
2795 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2796 fc->last_ack_recv = ack;
2798 /* Cancel polling if the ACK is big enough. */
2799 if (NULL != fc->poll_task &&
2800 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2802 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2803 GNUNET_SCHEDULER_cancel (fc->poll_task);
2804 fc->poll_task = NULL;
2805 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2808 connection_unlock_queue (c, fwd);
2809 GCC_check_connections ();
2815 * Core handler for cadet network traffic point-to-point ack polls.
2817 * @param cls closure
2818 * @param message message
2819 * @param peer peer identity this notification is about
2820 * @return #GNUNET_OK to keep the connection open,
2821 * #GNUNET_SYSERR to close it (signal serious error)
2824 GCC_handle_poll (void *cls,
2825 const struct GNUNET_PeerIdentity *peer,
2826 const struct GNUNET_MessageHeader *message)
2828 struct GNUNET_CADET_Poll *msg;
2829 struct CadetConnection *c;
2830 struct CadetFlowControl *fc;
2835 GCC_check_connections ();
2836 msg = (struct GNUNET_CADET_Poll *) message;
2837 log_message (message, peer, &msg->cid);
2838 c = connection_get (&msg->cid);
2841 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2843 LOG (GNUNET_ERROR_TYPE_DEBUG,
2844 "POLL message on unknown connection %s!\n",
2845 GNUNET_h2s (GC_h2hc (&msg->cid)));
2846 send_broken_unknown (&msg->cid,
2850 GCC_check_connections ();
2854 /* Is this a forward or backward ACK?
2855 * Note: a poll should never be needed in a loopback case,
2856 * since there is no possiblility of packet loss there, so
2857 * this way of discerining FWD/BCK should not be a problem.
2859 id = GNUNET_PEER_search (peer);
2860 if (GCP_get_short_id (get_next_hop (c)) == id)
2862 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2865 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2867 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2872 GNUNET_break_op (0);
2876 pid = ntohl (msg->pid);
2877 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2878 fc->last_pid_recv = pid;
2879 fwd = fc == &c->bck_fc;
2880 GCC_send_ack (c, fwd, GNUNET_YES);
2881 GCC_check_connections ();
2888 * Send an ACK on the appropriate connection/channel, depending on
2889 * the direction and the position of the peer.
2891 * @param c Which connection to send the hop-by-hop ACK.
2892 * @param fwd Is this a fwd ACK? (will go dest->root).
2893 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2896 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2898 unsigned int buffer;
2900 GCC_check_connections ();
2901 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
2902 GC_f2s (fwd), GCC_2s (c));
2910 if (GNUNET_NO != c->destroy)
2912 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2913 GCC_check_connections ();
2917 /* Get available buffer space */
2918 if (GCC_is_terminal (c, fwd))
2920 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2921 buffer = GCT_get_channels_buffer (c->t);
2925 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2926 buffer = GCC_get_buffer (c, fwd);
2928 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2929 if (0 == buffer && GNUNET_NO == force)
2931 GCC_check_connections ();
2935 /* Send available buffer space */
2936 if (GCC_is_origin (c, fwd))
2938 GNUNET_assert (NULL != c->t);
2939 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2940 GCT_unchoke_channels (c->t);
2944 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2945 send_ack (c, buffer, fwd, force);
2947 GCC_check_connections ();
2952 * Initialize the connections subsystem
2954 * @param c Configuration handle.
2957 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2959 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2961 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2964 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2965 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2966 GNUNET_SCHEDULER_shutdown ();
2971 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2974 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2975 "CADET", "MAX_CONNECTIONS", "MISSING");
2976 GNUNET_SCHEDULER_shutdown ();
2981 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2982 &refresh_connection_time))
2984 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2985 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2986 GNUNET_SCHEDULER_shutdown ();
2989 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2990 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2995 * Destroy each connection on shutdown.
2997 * @param cls Closure (unused).
2998 * @param key Current key code (CID, unused).
2999 * @param value Value in the hash map (`struct CadetConnection`)
3001 * @return #GNUNET_YES, because we should continue to iterate
3004 shutdown_iterator (void *cls,
3005 const struct GNUNET_HashCode *key,
3008 struct CadetConnection *c = value;
3010 c->state = CADET_CONNECTION_DESTROYED;
3017 * Shut down the connections subsystem.
3022 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n");
3023 GCC_check_connections ();
3024 GNUNET_CONTAINER_multihashmap_iterate (connections,
3027 GNUNET_CONTAINER_multihashmap_destroy (connections);
3033 * Create a connection.
3035 * @param cid Connection ID (either created locally or imposed remotely).
3036 * @param t Tunnel this connection belongs to (or NULL);
3037 * @param path Path this connection has to use (copy is made).
3038 * @param own_pos Own position in the @c path path.
3040 * @return Newly created connection, NULL in case of error (own id not in path).
3042 struct CadetConnection *
3043 GCC_new (const struct GNUNET_CADET_Hash *cid,
3044 struct CadetTunnel *t,
3045 struct CadetPeerPath *path,
3046 unsigned int own_pos)
3048 struct CadetConnection *c;
3049 struct CadetPeerPath *cpath;
3051 GCC_check_connections ();
3052 cpath = path_duplicate (path);
3053 GNUNET_assert (NULL != cpath);
3054 c = GNUNET_new (struct CadetConnection);
3056 GNUNET_assert (GNUNET_OK ==
3057 GNUNET_CONTAINER_multihashmap_put (connections,
3059 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3060 fc_init (&c->fwd_fc);
3061 fc_init (&c->bck_fc);
3066 GNUNET_assert (own_pos <= cpath->length - 1);
3067 c->own_pos = own_pos;
3070 if (GNUNET_OK != register_neighbors (c))
3074 /* We were the origin of this request, this means we have invalid
3075 * info about the paths to reach the destination. We must invalidate
3076 * the *original* path to avoid trying it again in the next minute.
3078 if (2 < path->length)
3079 path_invalidate (path);
3083 GCT_debug(t, GNUNET_ERROR_TYPE_WARNING);
3087 path_destroy (c->path);
3092 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
3093 GCC_check_connections ();
3099 GCC_destroy (struct CadetConnection *c)
3101 GCC_check_connections ();
3108 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
3109 return; /* -> message_sent -> GCC_destroy. Don't loop. */
3112 LOG (GNUNET_ERROR_TYPE_DEBUG,
3113 "destroying connection %s\n",
3115 LOG (GNUNET_ERROR_TYPE_DEBUG,
3116 " fc's f: %p, b: %p\n",
3117 &c->fwd_fc, &c->bck_fc);
3118 LOG (GNUNET_ERROR_TYPE_DEBUG,
3119 " fc tasks f: %u, b: %u\n",
3120 c->fwd_fc.poll_task,
3121 c->bck_fc.poll_task);
3123 /* Cancel all traffic */
3124 if (NULL != c->path)
3126 connection_cancel_queues (c, GNUNET_YES);
3127 connection_cancel_queues (c, GNUNET_NO);
3129 unregister_neighbors (c);
3130 path_destroy (c->path);
3133 /* Delete from tunnel */
3135 GCT_remove_connection (c->t, c);
3137 if (NULL != c->check_duplicates_task)
3138 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
3139 if (NULL != c->fwd_maintenance_task)
3140 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
3141 if (NULL != c->bck_maintenance_task)
3142 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
3144 if (GNUNET_NO == c->was_removed)
3146 GNUNET_break (GNUNET_YES ==
3147 GNUNET_CONTAINER_multihashmap_remove (connections,
3151 GNUNET_STATISTICS_update (stats,
3156 GCC_check_connections ();
3161 * Get the connection ID.
3163 * @param c Connection to get the ID from.
3165 * @return ID of the connection.
3167 const struct GNUNET_CADET_Hash *
3168 GCC_get_id (const struct CadetConnection *c)
3175 * Get the connection ID.
3177 * @param c Connection to get the ID from.
3179 * @return ID of the connection.
3181 const struct GNUNET_HashCode *
3182 GCC_get_h (const struct CadetConnection *c)
3184 return GC_h2hc (&c->id);
3189 * Get the connection path.
3191 * @param c Connection to get the path from.
3193 * @return path used by the connection.
3195 const struct CadetPeerPath *
3196 GCC_get_path (const struct CadetConnection *c)
3198 if (GNUNET_NO == c->destroy)
3205 * Get the connection state.
3207 * @param c Connection to get the state from.
3209 * @return state of the connection.
3211 enum CadetConnectionState
3212 GCC_get_state (const struct CadetConnection *c)
3218 * Get the connection tunnel.
3220 * @param c Connection to get the tunnel from.
3222 * @return tunnel of the connection.
3224 struct CadetTunnel *
3225 GCC_get_tunnel (const struct CadetConnection *c)
3232 * Get free buffer space in a connection.
3234 * @param c Connection.
3235 * @param fwd Is query about FWD traffic?
3237 * @return Free buffer space [0 - max_msgs_queue/max_connections]
3240 GCC_get_buffer (struct CadetConnection *c, int fwd)
3242 struct CadetFlowControl *fc;
3244 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3246 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
3247 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
3248 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
3250 return (fc->queue_max - fc->queue_n);
3255 * Get how many messages have we allowed to send to us from a direction.
3257 * @param c Connection.
3258 * @param fwd Are we asking about traffic from FWD (BCK messages)?
3260 * @return last_ack_sent - last_pid_recv
3263 GCC_get_allowed (struct CadetConnection *c, int fwd)
3265 struct CadetFlowControl *fc;
3267 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3268 if (CADET_CONNECTION_READY != c->state
3269 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
3273 return (fc->last_ack_sent - fc->last_pid_recv);
3278 * Get messages queued in a connection.
3280 * @param c Connection.
3281 * @param fwd Is query about FWD traffic?
3283 * @return Number of messages queued.
3286 GCC_get_qn (struct CadetConnection *c, int fwd)
3288 struct CadetFlowControl *fc;
3290 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3297 * Get next PID to use.
3299 * @param c Connection.
3300 * @param fwd Is query about FWD traffic?
3302 * @return Last PID used + 1.
3305 GCC_get_pid (struct CadetConnection *c, int fwd)
3307 struct CadetFlowControl *fc;
3309 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3311 return fc->last_pid_sent + 1;
3316 * Allow the connection to advertise a buffer of the given size.
3318 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3319 * allowing up to last_pid_recv + buffer.
3321 * @param c Connection.
3322 * @param buffer How many more messages the connection can accept.
3323 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3326 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3328 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3329 GCC_2s (c), buffer, GC_f2s (fwd));
3330 send_ack (c, buffer, fwd, GNUNET_NO);
3335 * Notify other peers on a connection of a broken link. Mark connections
3336 * to destroy after all traffic has been sent.
3338 * @param c Connection on which there has been a disconnection.
3339 * @param peer Peer that disconnected.
3342 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
3344 struct CadetFlowControl *fc;
3345 struct CadetPeer *hop;
3349 GCC_check_connections ();
3350 strncpy (peer_name, GCP_2s (peer), 16);
3351 peer_name[15] = '\0';
3352 LOG (GNUNET_ERROR_TYPE_DEBUG,
3353 "shutting down %s, %s disconnected\n",
3354 GCC_2s (c), peer_name);
3356 invalidate_paths (c, peer);
3358 hop = get_prev_hop (c);
3361 /* Path was NULL, we should have deleted the connection. */
3365 fwd = (peer == hop);
3366 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3367 (GNUNET_NO != c->destroy) )
3369 /* Local shutdown, or other peer already down (hence 'c->destroy');
3370 so there is no one to notify about this, just clean up. */
3372 GCC_check_connections ();
3375 /* Mark FlowControl towards the peer as unavaliable. */
3376 fc = fwd ? &c->bck_fc : &c->fwd_fc;
3379 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3381 /* Connection will have at least one pending message
3382 * (the one we just scheduled), so delay destruction
3383 * and remove from map so we don't use accidentally. */
3385 GNUNET_assert (GNUNET_NO == c->was_removed);
3386 c->was_removed = GNUNET_YES;
3387 GNUNET_break (GNUNET_YES ==
3388 GNUNET_CONTAINER_multihashmap_remove (connections,
3391 /* Cancel queue in the direction that just died. */
3392 connection_cancel_queues (c, ! fwd);
3393 GCC_stop_poll (c, ! fwd);
3394 unregister_neighbors (c);
3395 GCC_check_connections ();
3400 * Is this peer the first one on the connection?
3402 * @param c Connection.
3403 * @param fwd Is this about fwd traffic?
3405 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3408 GCC_is_origin (struct CadetConnection *c, int fwd)
3410 if (!fwd && c->path->length - 1 == c->own_pos )
3412 if (fwd && 0 == c->own_pos)
3419 * Is this peer the last one on the connection?
3421 * @param c Connection.
3422 * @param fwd Is this about fwd traffic?
3423 * Note that the ROOT is the terminal for BCK traffic!
3425 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3428 GCC_is_terminal (struct CadetConnection *c, int fwd)
3430 return GCC_is_origin (c, ! fwd);
3435 * See if we are allowed to send by the next hop in the given direction.
3437 * @param c Connection.
3438 * @param fwd Is this about fwd traffic?
3440 * @return #GNUNET_YES in case it's OK to send.
3443 GCC_is_sendable (struct CadetConnection *c, int fwd)
3445 struct CadetFlowControl *fc;
3447 LOG (GNUNET_ERROR_TYPE_DEBUG,
3448 " checking sendability of %s traffic on %s\n",
3449 GC_f2s (fwd), GCC_2s (c));
3455 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3456 LOG (GNUNET_ERROR_TYPE_DEBUG,
3457 " last ack recv: %u, last pid sent: %u\n",
3458 fc->last_ack_recv, fc->last_pid_sent);
3459 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3461 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3464 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3470 * Check if this connection is a direct one (never trim a direct connection).
3472 * @param c Connection.
3474 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3477 GCC_is_direct (struct CadetConnection *c)
3479 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3484 * Sends an already built message on a connection, properly registering
3485 * all used resources.
3487 * @param message Message to send. Function makes a copy of it.
3488 * If message is not hop-by-hop, decrements TTL of copy.
3489 * @param payload_type Type of payload, in case the message is encrypted.
3490 * @param c Connection on which this message is transmitted.
3491 * @param fwd Is this a fwd message?
3492 * @param force Force the connection to accept the message (buffer overfill).
3493 * @param cont Continuation called once message is sent. Can be NULL.
3494 * @param cont_cls Closure for @c cont.
3496 * @return Handle to cancel the message before it's sent.
3497 * NULL on error or if @c cont is NULL.
3498 * Invalid on @c cont call.
3500 struct CadetConnectionQueue *
3501 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3502 uint16_t payload_type, uint32_t payload_id,
3503 struct CadetConnection *c, int fwd, int force,
3504 GCC_sent cont, void *cont_cls)
3506 struct CadetFlowControl *fc;
3507 struct CadetConnectionQueue *q;
3513 GCC_check_connections ();
3514 size = ntohs (message->size);
3515 data = GNUNET_malloc (size);
3516 memcpy (data, message, size);
3517 type = ntohs (message->type);
3518 LOG (GNUNET_ERROR_TYPE_INFO,
3519 "--> %s (%s %4u) on conn %s (%p) %s [%5u]\n",
3520 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), c,
3523 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3524 if (0 == fc->queue_max)
3529 droppable = GNUNET_NO == force;
3532 struct GNUNET_CADET_AX *axmsg;
3533 struct GNUNET_CADET_Encrypted *emsg;
3534 struct GNUNET_CADET_KX *kmsg;
3535 struct GNUNET_CADET_ACK *amsg;
3536 struct GNUNET_CADET_Poll *pmsg;
3537 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3538 struct GNUNET_CADET_ConnectionBroken *bmsg;
3541 case GNUNET_MESSAGE_TYPE_CADET_AX:
3542 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3543 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3545 emsg = (struct GNUNET_CADET_Encrypted *) data;
3546 ttl = ntohl (emsg->ttl);
3549 GNUNET_break_op (0);
3554 emsg->ttl = htonl (ttl - 1);
3558 axmsg = (struct GNUNET_CADET_AX *) data;
3561 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3562 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3563 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3564 if (GNUNET_YES == droppable)
3570 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3574 case GNUNET_MESSAGE_TYPE_CADET_KX:
3575 kmsg = (struct GNUNET_CADET_KX *) data;
3579 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3580 amsg = (struct GNUNET_CADET_ACK *) data;
3582 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3583 droppable = GNUNET_NO;
3586 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3587 pmsg = (struct GNUNET_CADET_Poll *) data;
3589 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
3590 droppable = GNUNET_NO;
3593 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3594 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
3598 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3599 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
3603 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3604 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3613 if (fc->queue_n > fc->queue_max && droppable)
3615 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3618 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3619 fc->queue_n, fc->queue_max);
3620 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
3621 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
3626 return NULL; /* Drop this message */
3629 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3630 GCC_2s (c), c->pending_messages);
3631 c->pending_messages++;
3633 q = GNUNET_new (struct CadetConnectionQueue);
3634 q->forced = !droppable;
3635 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3636 size, c, fwd, &conn_message_sent, q);
3639 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3642 GCC_check_connections ();
3646 q->cont_cls = cont_cls;
3647 GCC_check_connections ();
3648 return (NULL == cont) ? NULL : q;
3653 * Cancel a previously sent message while it's in the queue.
3655 * ONLY can be called before the continuation given to the send function
3656 * is called. Once the continuation is called, the message is no longer in the
3659 * @param q Handle to the queue.
3662 GCC_cancel (struct CadetConnectionQueue *q)
3664 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3666 /* queue destroy calls message_sent, which calls q->cont and frees q */
3667 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3668 GCC_check_connections ();
3673 * Sends a CREATE CONNECTION message for a path to a peer.
3674 * Changes the connection and tunnel states if necessary.
3676 * @param connection Connection to create.
3679 GCC_send_create (struct CadetConnection *connection)
3681 enum CadetTunnelCState state;
3684 GCC_check_connections ();
3685 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3686 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3688 LOG (GNUNET_ERROR_TYPE_INFO, "==> %s %19s on conn %s (%p) FWD [%5u]\n",
3689 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE), "",
3690 GCC_2s (connection), connection, size);
3691 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3692 connection, connection->pending_messages);
3693 connection->pending_messages++;
3695 connection->maintenance_q =
3696 GCP_queue_add (get_next_hop (connection), NULL,
3697 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, UINT16_MAX, 0,
3698 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3700 state = GCT_get_cstate (connection->t);
3701 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3702 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3703 if (CADET_CONNECTION_NEW == connection->state)
3704 connection_change_state (connection, CADET_CONNECTION_SENT);
3705 GCC_check_connections ();
3710 * Send a message to all peers in this connection that the connection
3711 * is no longer valid.
3713 * If some peer should not receive the message, it should be zero'ed out
3714 * before calling this function.
3716 * @param c The connection whose peers to notify.
3719 GCC_send_destroy (struct CadetConnection *c)
3721 struct GNUNET_CADET_ConnectionDestroy msg;
3723 if (GNUNET_YES == c->destroy)
3725 GCC_check_connections ();
3726 msg.header.size = htons (sizeof (msg));
3727 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3729 LOG (GNUNET_ERROR_TYPE_DEBUG,
3730 " sending connection destroy for connection %s\n",
3733 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3734 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, UINT16_MAX,
3736 GNUNET_YES, NULL, NULL));
3737 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3738 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, UINT16_MAX,
3740 GNUNET_YES, NULL, NULL));
3742 GCC_check_connections ();
3747 * @brief Start a polling timer for the connection.
3749 * When a neighbor does not accept more traffic on the connection it could be
3750 * caused by a simple congestion or by a lost ACK. Polling enables to check
3751 * for the lastest ACK status for a connection.
3753 * @param c Connection.
3754 * @param fwd Should we poll in the FWD direction?
3757 GCC_start_poll (struct CadetConnection *c, int fwd)
3759 struct CadetFlowControl *fc;
3761 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3762 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3764 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3766 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL already in progress (t: %p, m: %p)\n",
3767 fc->poll_task, fc->poll_msg);
3770 if (0 == fc->queue_max)
3772 /* Should not be needed, traffic should've been cancelled. */
3774 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not possible, peer disconnected\n");
3777 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3778 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3785 * @brief Stop polling a connection for ACKs.
3787 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3789 * @param c Connection.
3790 * @param fwd Should we stop the poll in the FWD direction?
3793 GCC_stop_poll (struct CadetConnection *c, int fwd)
3795 struct CadetFlowControl *fc;
3797 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3798 if (NULL != fc->poll_task)
3800 GNUNET_SCHEDULER_cancel (fc->poll_task);
3801 fc->poll_task = NULL;
3803 if (NULL != fc->poll_msg)
3805 GCC_cancel (fc->poll_msg);
3806 fc->poll_msg = NULL;
3812 * Get a (static) string for a connection.
3814 * @param c Connection.
3817 GCC_2s (const struct CadetConnection *c)
3824 static char buf[128];
3826 SPRINTF (buf, "%s (->%s)",
3827 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3830 return GNUNET_h2s (GC_h2hc (&c->id));
3835 * Log all possible info about the connection state.
3837 * @param c Connection to debug.
3838 * @param level Debug level to use.
3841 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3846 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3848 __FILE__, __FUNCTION__, __LINE__);
3854 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3858 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3859 s = path_2s (c->path);
3860 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3862 LOG2 (level, "CCC state: %s, destroy: %u\n",
3863 GCC_state2s (c->state), c->destroy);
3864 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3865 if (NULL != c->perf)
3866 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3868 LOG2 (level, "CCC FWD flow control:\n");
3869 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3870 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3871 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3872 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3873 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3874 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3875 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3876 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3878 LOG2 (level, "CCC BCK flow control:\n");
3879 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3880 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3881 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3882 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3883 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3884 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3885 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3886 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3888 LOG2 (level, "CCC DEBUG CONNECTION END\n");