2 This file is part of GNUnet.
3 Copyright (C) 2001-2015 Christian Grothoff (and other contributing authors)
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 static struct CadetConnection *
458 connection_get (const struct GNUNET_CADET_Hash *cid)
460 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
465 * Change the connection state. Cannot change a connection marked as destroyed.
467 * @param c Connection to change.
468 * @param state New state to set.
471 connection_change_state (struct CadetConnection* c,
472 enum CadetConnectionState state)
474 LOG (GNUNET_ERROR_TYPE_DEBUG,
475 "Connection %s state %s -> %s\n",
476 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
477 if (CADET_CONNECTION_DESTROYED <= c->state) /* Destroyed or broken. */
479 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
483 if (CADET_CONNECTION_READY == state)
489 * Callback called when a queued ACK message is sent.
491 * @param cls Closure (FC).
492 * @param c Connection this message was on.
493 * @param q Queue handler this call invalidates.
494 * @param type Type of message sent.
495 * @param fwd Was this a FWD going message?
496 * @param size Size of the message.
500 struct CadetConnection *c,
501 struct CadetConnectionQueue *q,
502 uint16_t type, int fwd, size_t size)
504 struct CadetFlowControl *fc = cls;
511 * Send an ACK on the connection, informing the predecessor about
512 * the available buffer space. Should not be called in case the peer
513 * is origin (no predecessor) in the @c fwd direction.
515 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
516 * the ACK itself goes "back" (dest->root).
518 * @param c Connection on which to send the ACK.
519 * @param buffer How much space free to advertise?
520 * @param fwd Is this FWD ACK? (Going dest -> root)
521 * @param force Don't optimize out.
524 send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
526 struct CadetFlowControl *next_fc;
527 struct CadetFlowControl *prev_fc;
528 struct GNUNET_CADET_ACK msg;
532 /* If origin, there is no connection to send ACKs. Wrong function! */
533 GCC_check_connections ();
534 if (GCC_is_origin (c, fwd))
536 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
537 GCC_2s (c), GC_f2s (fwd));
542 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
543 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
545 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
546 GC_f2s (fwd), GCC_2s (c));
548 /* Check if we need to transmit the ACK. */
549 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
550 if (3 < delta && buffer < delta && GNUNET_NO == force)
552 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
553 LOG (GNUNET_ERROR_TYPE_DEBUG,
554 " last pid recv: %u, last ack sent: %u\n",
555 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
556 GCC_check_connections ();
560 /* Ok, ACK might be necessary, what PID to ACK? */
561 ack = prev_fc->last_pid_recv + buffer;
562 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
563 LOG (GNUNET_ERROR_TYPE_DEBUG,
564 " last pid %u, last ack %u, qmax %u, q %u\n",
565 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
566 next_fc->queue_max, next_fc->queue_n);
567 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
570 GCC_check_connections ();
574 /* Check if message is already in queue */
575 if (NULL != prev_fc->ack_msg)
577 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
579 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
580 GCC_cancel (prev_fc->ack_msg);
581 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
585 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
586 GCC_check_connections ();
591 prev_fc->last_ack_sent = ack;
593 /* Build ACK message and send on conn */
594 msg.header.size = htons (sizeof (msg));
595 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
596 msg.ack = htonl (ack);
599 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, UINT16_MAX, ack,
602 GNUNET_assert (NULL != prev_fc->ack_msg);
603 GCC_check_connections ();
608 * Callback called when a connection queued message is sent.
610 * Calculates the average time and connection packet tracking.
612 * @param cls Closure (ConnectionQueue Handle).
613 * @param c Connection this message was on.
614 * @param sent Was it really sent? (Could have been canceled)
615 * @param type Type of message sent.
616 * @param pid Packet ID, or 0 if not applicable (create, destroy, etc).
617 * @param fwd Was this a FWD going message?
618 * @param size Size of the message.
619 * @param wait Time spent waiting for core (only the time for THIS message)
620 * @return #GNUNET_YES if connection was destroyed, #GNUNET_NO otherwise.
623 conn_message_sent (void *cls,
624 struct CadetConnection *c, int sent,
625 uint16_t type, uint32_t pid, int fwd, size_t size,
626 struct GNUNET_TIME_Relative wait)
628 struct CadetConnectionPerformance *p;
629 struct CadetFlowControl *fc;
630 struct CadetConnectionQueue *q = cls;
634 GCC_check_connections ();
635 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
637 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
639 fc = fwd ? &c->fwd_fc : &c->bck_fc;
640 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
641 sent ? "" : "not ", GC_f2s (fwd), GC_m2s (type), pid);
647 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
648 q->cont (q->cont_cls, c, q, type, fwd, size);
652 else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED
653 || type == GNUNET_MESSAGE_TYPE_CADET_AX)
655 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
664 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
665 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
667 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
670 GCC_check_connections ();
673 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
674 c->pending_messages--;
675 if ( (GNUNET_YES == c->destroy) &&
676 (0 == c->pending_messages) )
678 LOG (GNUNET_ERROR_TYPE_DEBUG,
679 "! destroying connection!\n");
681 GCC_check_connections ();
684 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
687 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
688 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
689 c->maintenance_q = NULL;
690 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
691 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
692 schedule_next_keepalive (c, fwd);
695 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
696 case GNUNET_MESSAGE_TYPE_CADET_AX:
697 if (GNUNET_YES == sent)
699 GNUNET_assert (NULL != q);
700 fc->last_pid_sent = pid;
701 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
702 GCC_start_poll (c, fwd);
703 GCC_send_ack (c, fwd, GNUNET_NO);
704 connection_reset_timeout (c, fwd);
707 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
708 if (GNUNET_NO == forced)
711 LOG (GNUNET_ERROR_TYPE_DEBUG,
712 "! accounting pid %u\n",
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 "! forced, Q_N not accounting pid %u\n",
723 case GNUNET_MESSAGE_TYPE_CADET_KX:
724 if (GNUNET_YES == sent)
725 connection_reset_timeout (c, fwd);
728 case GNUNET_MESSAGE_TYPE_CADET_POLL:
732 case GNUNET_MESSAGE_TYPE_CADET_ACK:
736 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
737 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
741 LOG (GNUNET_ERROR_TYPE_ERROR, "%s unknown\n", GC_m2s (type));
745 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
748 return GNUNET_NO; /* Only endpoints are interested in timing. */
751 usecsperbyte = ((double) wait.rel_value_us) / size;
752 if (p->size == AVG_MSGS)
754 /* Array is full. Substract oldest value, add new one and store. */
755 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
756 p->usecsperbyte[p->idx] = usecsperbyte;
757 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
761 /* Array not yet full. Add current value to avg and store. */
762 p->usecsperbyte[p->idx] = usecsperbyte;
764 p->avg += p->usecsperbyte[p->idx];
768 p->idx = (p->idx + 1) % AVG_MSGS;
769 GCC_check_connections ();
775 * Get the previous hop in a connection
777 * @param c Connection.
779 * @return Previous peer in the connection.
781 static struct CadetPeer *
782 get_prev_hop (const struct CadetConnection *c)
788 LOG (GNUNET_ERROR_TYPE_DEBUG,
789 " get prev hop %s [%u/%u]\n",
790 GCC_2s (c), c->own_pos, c->path->length);
791 if (0 == c->own_pos || c->path->length < 2)
792 id = c->path->peers[0];
794 id = c->path->peers[c->own_pos - 1];
796 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
797 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
799 return GCP_get_short (id, GNUNET_YES);
804 * Get the next hop in a connection
806 * @param c Connection.
808 * @return Next peer in the connection.
810 static struct CadetPeer *
811 get_next_hop (const struct CadetConnection *c)
818 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
819 GCC_2s (c), c->own_pos, c->path->length);
820 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
821 id = c->path->peers[c->path->length - 1];
823 id = c->path->peers[c->own_pos + 1];
825 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
826 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
828 return GCP_get_short (id, GNUNET_YES);
833 * Check that the direct neighbours (previous and next hop)
834 * are properly associated with this connection.
836 * @param c connection to check
839 check_neighbours (const struct CadetConnection *c)
842 return; /* nothing to check */
843 GCP_check_connection (get_next_hop (c), c);
844 GCP_check_connection (get_prev_hop (c), c);
849 * Helper for #GCC_check_connections(). Calls #check_neighbours().
853 * @param value the `struct CadetConnection` to check
854 * @return #GNUNET_OK (continue to iterate)
857 check_connection (void *cls,
858 const struct GNUNET_HashCode *key,
861 struct CadetConnection *c = value;
863 check_neighbours (c);
869 * Check invariants for all connections using #check_neighbours().
872 GCC_check_connections ()
874 if (0 == CHECK_INVARIANTS)
876 if (NULL == connections)
878 GNUNET_CONTAINER_multihashmap_iterate (connections,
885 * Get the hop in a connection.
887 * @param c Connection.
888 * @param fwd Next in the FWD direction?
890 * @return Next peer in the connection.
892 static struct CadetPeer *
893 get_hop (struct CadetConnection *c, int fwd)
895 return (fwd) ? get_next_hop (c) : get_prev_hop (c);
900 * Get a bit mask for a message received out-of-order.
902 * @param last_pid_recv Last PID we received prior to the out-of-order.
903 * @param ooo_pid PID of the out-of-order message.
906 get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
908 return 1 << (last_pid_recv - ooo_pid);
913 * Check is an out-of-order message is ok:
914 * - at most 31 messages behind.
917 * @param last_pid_recv Last in-order PID received.
920 is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
924 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
927 mask = get_recv_bitmask (last_pid_recv, ooo_pid);
928 if (0 != (ooo_bitmap & mask))
936 * Is traffic coming from this sender 'FWD' traffic?
938 * @param c Connection to check.
939 * @param sender Peer identity of neighbor.
941 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
942 * the traffic is 'FWD'.
943 * #GNUNET_NO for BCK.
944 * #GNUNET_SYSERR for errors.
947 is_fwd (const struct CadetConnection *c,
948 const struct GNUNET_PeerIdentity *sender)
952 id = GNUNET_PEER_search (sender);
953 if (GCP_get_short_id (get_prev_hop (c)) == id)
956 if (GCP_get_short_id (get_next_hop (c)) == id)
960 return GNUNET_SYSERR;
965 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
966 * or a first CONNECTION_ACK directed to us.
968 * @param connection Connection to confirm.
969 * @param fwd Should we send it FWD? (root->dest)
970 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
973 send_connection_ack (struct CadetConnection *connection, int fwd)
975 struct CadetTunnel *t;
976 size_t size = sizeof (struct GNUNET_CADET_ConnectionACK);
978 GCC_check_connections ();
980 LOG (GNUNET_ERROR_TYPE_INFO,
981 "==> { C %s ACK} %19s on conn %s (%p) %s [%5u]\n",
982 GC_f2s (!fwd), "", GCC_2s (connection), connection, GC_f2s (fwd), size);
983 GCP_queue_add (get_hop (connection, fwd), NULL,
984 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, UINT16_MAX, 0,
985 size, connection, fwd, &conn_message_sent, NULL);
986 connection->pending_messages++;
987 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
988 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
989 if (CADET_CONNECTION_READY != connection->state)
990 connection_change_state (connection, CADET_CONNECTION_SENT);
991 GCC_check_connections ();
996 * Send a notification that a connection is broken.
998 * @param c Connection that is broken.
999 * @param id1 Peer that has disconnected.
1000 * @param id2 Peer that has disconnected.
1001 * @param fwd Direction towards which to send it.
1004 send_broken (struct CadetConnection *c,
1005 const struct GNUNET_PeerIdentity *id1,
1006 const struct GNUNET_PeerIdentity *id2,
1009 struct GNUNET_CADET_ConnectionBroken msg;
1011 GCC_check_connections ();
1012 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1013 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1017 GNUNET_assert (NULL ==
1018 GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c, fwd,
1019 GNUNET_YES, NULL, NULL));
1020 GCC_check_connections ();
1025 * Send a notification that a connection is broken, when a connection
1026 * isn't even known to the local peer or soon to be destroyed.
1028 * @param connection_id Connection ID.
1029 * @param id1 Peer that has disconnected, probably local peer.
1030 * @param id2 Peer that has disconnected can be NULL if unknown.
1031 * @param peer Peer to notify (neighbor who sent the connection).
1034 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
1035 const struct GNUNET_PeerIdentity *id1,
1036 const struct GNUNET_PeerIdentity *id2,
1037 const struct GNUNET_PeerIdentity *peer_id)
1039 struct GNUNET_CADET_ConnectionBroken *msg;
1040 struct CadetPeer *neighbor;
1042 GCC_check_connections ();
1043 LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n",
1044 GNUNET_h2s (GC_h2hc (connection_id)));
1046 msg = GNUNET_new (struct GNUNET_CADET_ConnectionBroken);
1047 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1048 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1049 msg->cid = *connection_id;
1054 memset (&msg->peer2, 0, sizeof (msg->peer2));
1055 neighbor = GCP_get (peer_id, GNUNET_NO); /* We MUST know neighbor. */
1056 GNUNET_assert (NULL != neighbor);
1057 GCP_queue_add (neighbor, msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
1058 UINT16_MAX, 2, sizeof (struct GNUNET_CADET_ConnectionBroken),
1059 NULL, GNUNET_SYSERR, /* connection, fwd */
1060 NULL, NULL); /* continuation */
1061 GCC_check_connections ();
1066 * Send keepalive packets for a connection.
1068 * @param c Connection to keep alive..
1069 * @param fwd Is this a FWD keepalive? (owner -> dest).
1072 send_connection_keepalive (struct CadetConnection *c, int fwd)
1074 struct GNUNET_MessageHeader msg;
1075 struct CadetFlowControl *fc;
1077 GCC_check_connections ();
1078 LOG (GNUNET_ERROR_TYPE_INFO,
1079 "keepalive %s for connection %s\n",
1080 GC_f2s (fwd), GCC_2s (c));
1082 GNUNET_assert (NULL != c->t);
1083 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1084 if (0 < fc->queue_n || GNUNET_YES == GCT_has_queued_traffic (c->t))
1086 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1090 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1092 GNUNET_assert (NULL != c->t);
1093 msg.size = htons (sizeof (msg));
1094 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
1096 GNUNET_assert (NULL ==
1097 GCT_send_prebuilt_message (&msg, c->t, c,
1098 GNUNET_NO, NULL, NULL));
1099 GCC_check_connections ();
1104 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1106 * @param c Connection for which to send the message.
1107 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1110 connection_recreate (struct CadetConnection *c, int fwd)
1112 LOG (GNUNET_ERROR_TYPE_DEBUG,
1113 "sending connection recreate\n");
1115 GCC_send_create (c);
1117 send_connection_ack (c, GNUNET_NO);
1122 * Generic connection timer management.
1123 * Depending on the role of the peer in the connection will send the
1124 * appropriate message (build or keepalive)
1126 * @param c Conncetion to maintain.
1127 * @param fwd Is FWD?
1130 connection_maintain (struct CadetConnection *c, int fwd)
1132 if (GNUNET_NO != c->destroy)
1134 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1141 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1145 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1147 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1149 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1150 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1151 schedule_next_keepalive (c, fwd);
1156 case CADET_CONNECTION_NEW:
1159 case CADET_CONNECTION_SENT:
1160 connection_recreate (c, fwd);
1162 case CADET_CONNECTION_READY:
1163 send_connection_keepalive (c, fwd);
1172 * Keep the connection alive.
1174 * @param c Connection to keep alive.
1175 * @param fwd Direction.
1176 * @param shutdown Are we shutting down? (Don't send traffic)
1177 * Non-zero value for true, not necessarily GNUNET_YES.
1180 connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
1182 GCC_check_connections ();
1183 LOG (GNUNET_ERROR_TYPE_DEBUG,
1184 "%s keepalive for %s\n",
1185 GC_f2s (fwd), GCC_2s (c));
1188 c->fwd_maintenance_task = NULL;
1190 c->bck_maintenance_task = NULL;
1192 if (GNUNET_NO != shutdown)
1195 connection_maintain (c, fwd);
1196 GCC_check_connections ();
1197 /* Next execution will be scheduled by message_sent or _maintain*/
1202 * Keep the connection alive in the FWD direction.
1204 * @param cls Closure (connection to keepalive).
1205 * @param tc TaskContext.
1208 connection_fwd_keepalive (void *cls,
1209 const struct GNUNET_SCHEDULER_TaskContext *tc)
1211 GCC_check_connections ();
1212 connection_keepalive ((struct CadetConnection *) cls,
1214 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1215 GCC_check_connections ();
1220 * Keep the connection alive in the BCK direction.
1222 * @param cls Closure (connection to keepalive).
1223 * @param tc TaskContext.
1226 connection_bck_keepalive (void *cls,
1227 const struct GNUNET_SCHEDULER_TaskContext *tc)
1229 GCC_check_connections ();
1230 connection_keepalive ((struct CadetConnection *) cls,
1232 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1233 GCC_check_connections ();
1238 * Schedule next keepalive task, taking in consideration
1239 * the connection state and number of retries.
1241 * If the peer is not the origin, do nothing.
1243 * @param c Connection for which to schedule the next keepalive.
1244 * @param fwd Direction for the next keepalive.
1247 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1249 struct GNUNET_TIME_Relative delay;
1250 struct GNUNET_SCHEDULER_Task * *task_id;
1251 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1253 GCC_check_connections ();
1254 if (GNUNET_NO == GCC_is_origin (c, fwd))
1257 /* Calculate delay to use, depending on the state of the connection */
1258 if (CADET_CONNECTION_READY == c->state)
1260 delay = refresh_connection_time;
1264 if (1 > c->create_retry)
1265 c->create_retry = 1;
1266 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1268 if (c->create_retry < 64)
1269 c->create_retry *= 2;
1272 /* Select direction-dependent parameters */
1273 if (GNUNET_YES == fwd)
1275 task_id = &c->fwd_maintenance_task;
1276 keepalive_task = &connection_fwd_keepalive;
1280 task_id = &c->bck_maintenance_task;
1281 keepalive_task = &connection_bck_keepalive;
1284 /* Check that no one scheduled it before us */
1285 if (NULL != *task_id)
1287 /* No need for a _break. It can happen for instance when sending a SYNACK
1288 * for a duplicate SYN: the first SYNACK scheduled the task. */
1289 GNUNET_SCHEDULER_cancel (*task_id);
1292 /* Schedule the task */
1293 *task_id = GNUNET_SCHEDULER_add_delayed (delay,
1296 LOG (GNUNET_ERROR_TYPE_DEBUG,
1297 "next keepalive in %s\n",
1298 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1299 GCC_check_connections ();
1304 * @brief Re-initiate traffic on this connection if necessary.
1306 * Check if there is traffic queued towards this peer
1307 * and the core transmit handle is NULL (traffic was stalled).
1308 * If so, call core tmt rdy.
1310 * @param c Connection on which initiate traffic.
1311 * @param fwd Is this about fwd traffic?
1314 connection_unlock_queue (struct CadetConnection *c, int fwd)
1316 struct CadetPeer *peer;
1318 GCC_check_connections ();
1319 LOG (GNUNET_ERROR_TYPE_DEBUG,
1320 "connection_unlock_queue %s on %s\n",
1321 GC_f2s (fwd), GCC_2s (c));
1323 if (GCC_is_terminal (c, fwd))
1325 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal, can unlock!\n");
1329 peer = get_hop (c, fwd);
1330 GCP_queue_unlock (peer, c);
1331 GCC_check_connections ();
1336 * Cancel all transmissions that belong to a certain connection.
1338 * If the connection is scheduled for destruction and no more messages are left,
1339 * the connection will be destroyed by the continuation call.
1341 * @param c Connection which to cancel. Might be destroyed during this call.
1342 * @param fwd Cancel fwd traffic?
1345 connection_cancel_queues (struct CadetConnection *c,
1348 struct CadetFlowControl *fc;
1349 struct CadetPeer *peer;
1351 GCC_check_connections ();
1352 LOG (GNUNET_ERROR_TYPE_DEBUG,
1353 "Cancel %s queues for connection %s\n",
1354 GC_f2s (fwd), GCC_2s (c));
1361 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1362 if (NULL != fc->poll_task)
1364 GNUNET_SCHEDULER_cancel (fc->poll_task);
1365 fc->poll_task = NULL;
1366 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL task for fc %p\n", fc);
1368 if (NULL != fc->poll_msg)
1370 GCC_cancel (fc->poll_msg);
1371 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL msg for fc %p\n", fc);
1373 peer = get_hop (c, fwd);
1374 GCP_queue_cancel (peer, c);
1375 GCC_check_connections ();
1380 * Function called if a connection has been stalled for a while,
1381 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1383 * @param cls Closure (poll ctx).
1384 * @param tc TaskContext.
1387 connection_poll (void *cls,
1388 const struct GNUNET_SCHEDULER_TaskContext *tc);
1392 * Callback called when a queued POLL message is sent.
1394 * @param cls Closure (flow control context).
1395 * @param c Connection this message was on.
1396 * @param q Queue handler this call invalidates.
1397 * @param type Type of message sent.
1398 * @param fwd Was this a FWD going message?
1399 * @param size Size of the message.
1402 poll_sent (void *cls,
1403 struct CadetConnection *c,
1404 struct CadetConnectionQueue *q,
1405 uint16_t type, int fwd, size_t size)
1407 struct CadetFlowControl *fc = cls;
1409 GNUNET_assert (fc->poll_msg == q);
1410 fc->poll_msg = NULL;
1411 if (2 == c->destroy)
1413 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1416 if (0 == fc->queue_max)
1418 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL cancelled: neighbor disconnected\n");
1421 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1423 GNUNET_assert (NULL == fc->poll_task);
1424 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1425 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1433 * Function called if a connection has been stalled for a while,
1434 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1436 * @param cls Closure (poll ctx).
1437 * @param tc TaskContext.
1440 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1442 struct CadetFlowControl *fc = cls;
1443 struct GNUNET_CADET_Poll msg;
1444 struct CadetConnection *c;
1447 fc->poll_task = NULL;
1448 GCC_check_connections ();
1449 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1455 fwd = fc == &c->fwd_fc;
1456 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1457 GCC_2s (c), GC_f2s (fwd));
1459 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1460 msg.header.size = htons (sizeof (msg));
1461 msg.pid = htonl (fc->last_pid_sent);
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1464 GCC_send_prebuilt_message (&msg.header, UINT16_MAX, fc->last_pid_sent, c,
1465 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1466 GNUNET_assert (NULL != fc->poll_msg);
1467 GCC_check_connections ();
1472 * Resend all queued messages for a connection on other connections of the
1473 * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
1475 * @param c Connection whose messages to resend.
1476 * @param fwd Resend fwd messages?
1479 resend_messages_and_destroy (struct CadetConnection *c, int fwd)
1481 struct GNUNET_MessageHeader *out_msg;
1482 struct CadetTunnel *t = c->t;
1483 struct CadetPeer *neighbor;
1484 unsigned int pending;
1487 GCC_check_connections ();
1488 c->state = CADET_CONNECTION_DESTROYED;
1489 c->destroy = GNUNET_YES;
1491 destroyed = GNUNET_NO;
1492 neighbor = get_hop (c, fwd);
1493 pending = c->pending_messages;
1495 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1498 GCT_resend_message (out_msg, t);
1499 GNUNET_free (out_msg);
1502 /* All pending messages should have been popped,
1503 * and the connection destroyed by the continuation.
1505 if (GNUNET_YES != destroyed)
1510 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1511 if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1515 GCC_check_connections ();
1520 * Generic connection timeout implementation.
1522 * Timeout function due to lack of keepalive/traffic from an endpoint.
1523 * Destroys connection if called.
1525 * @param c Connection to destroy.
1526 * @param fwd Was the timeout from the origin? (FWD timeout)
1529 connection_timeout (struct CadetConnection *c, int fwd)
1531 struct CadetFlowControl *reverse_fc;
1533 GCC_check_connections ();
1534 reverse_fc = fwd ? &c->bck_fc : &c->fwd_fc;
1536 LOG (GNUNET_ERROR_TYPE_INFO,
1537 "Connection %s %s timed out. Destroying.\n",
1540 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1542 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1548 /* If dest, salvage queued traffic. */
1549 if (GCC_is_origin (c, !fwd))
1551 const struct GNUNET_PeerIdentity *next_hop;
1553 next_hop = GCP_get_id (fwd ? get_prev_hop (c) : get_next_hop (c));
1554 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1555 if (0 < reverse_fc->queue_n)
1556 resend_messages_and_destroy (c, !fwd);
1557 GCC_check_connections ();
1562 GCC_check_connections ();
1567 * Timeout function due to lack of keepalive/traffic from the owner.
1568 * Destroys connection if called.
1570 * @param cls Closure (connection to destroy).
1571 * @param tc TaskContext.
1574 connection_fwd_timeout (void *cls,
1575 const struct GNUNET_SCHEDULER_TaskContext *tc)
1577 struct CadetConnection *c = cls;
1579 c->fwd_maintenance_task = NULL;
1580 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1582 GCC_check_connections ();
1583 connection_timeout (c, GNUNET_YES);
1584 GCC_check_connections ();
1589 * Timeout function due to lack of keepalive/traffic from the destination.
1590 * Destroys connection if called.
1592 * @param cls Closure (connection to destroy).
1593 * @param tc TaskContext
1596 connection_bck_timeout (void *cls,
1597 const struct GNUNET_SCHEDULER_TaskContext *tc)
1599 struct CadetConnection *c = cls;
1601 c->bck_maintenance_task = NULL;
1602 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1604 GCC_check_connections ();
1605 connection_timeout (c, GNUNET_NO);
1606 GCC_check_connections ();
1611 * Resets the connection timeout task, some other message has done the
1613 * - For the first peer on the direction this means to send
1614 * a keepalive or a path confirmation message (either create or ACK).
1615 * - For all other peers, this means to destroy the connection,
1616 * due to lack of activity.
1617 * Starts the timeout if no timeout was running (connection just created).
1619 * @param c Connection whose timeout to reset.
1620 * @param fwd Is this forward?
1622 * TODO use heap to improve efficiency of scheduler.
1625 connection_reset_timeout (struct CadetConnection *c, int fwd)
1627 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1628 if (GCC_is_origin (c, fwd)) /* Startpoint */
1630 schedule_next_keepalive (c, fwd);
1632 else /* Relay, endpoint. */
1634 struct GNUNET_TIME_Relative delay;
1635 struct GNUNET_SCHEDULER_Task * *ti;
1636 GNUNET_SCHEDULER_TaskCallback f;
1638 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1641 GNUNET_SCHEDULER_cancel (*ti);
1642 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1643 LOG (GNUNET_ERROR_TYPE_DEBUG,
1644 " timing out in %s\n",
1645 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1646 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1647 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1653 * Iterator to compare each connection's path with the path of a new connection.
1655 * If the connection conincides, the c member of path is set to the connection
1656 * and the destroy flag of the connection is set.
1658 * @param cls Closure (new path).
1659 * @param c Connection in the tunnel to check.
1662 check_path (void *cls, struct CadetConnection *c)
1664 struct CadetConnection *new_conn = cls;
1665 struct CadetPeerPath *path = new_conn->path;
1667 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1668 GCC_2s (c), c, c->path->length);
1671 && c->destroy == GNUNET_NO
1672 && c->state != CADET_CONNECTION_BROKEN
1673 && c->state != CADET_CONNECTION_DESTROYED
1674 && path_equivalent (path, c->path))
1676 new_conn->destroy = GNUNET_YES;
1677 new_conn->path->c = c;
1678 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1684 * Finds out if this path is already being used by an existing connection.
1686 * Checks the tunnel towards the destination to see if it contains
1687 * any connection with the same path.
1689 * If the existing connection is ready, it is kept.
1690 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1691 * the peer will eventually reject our attempt).
1693 * @param path Path to check.
1694 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1695 * #GNUNET_NO otherwise.
1698 does_connection_exist (struct CadetConnection *conn)
1700 struct CadetPeer *p;
1701 struct CadetTunnel *t;
1702 struct CadetConnection *c;
1704 p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
1707 t = GCP_get_tunnel (p);
1711 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1713 GCT_iterate_connections (t, &check_path, conn);
1715 if (GNUNET_YES == conn->destroy)
1718 conn->destroy = GNUNET_NO;
1719 conn->path->c = conn;
1720 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1721 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1722 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1723 if (CADET_CONNECTION_READY == c->state)
1725 /* The other peer confirmed a live connection with this path,
1726 * why is it trying to duplicate it. */
1727 GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
1730 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not valid, connection unique\n");
1735 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1742 * @brief Check if the tunnel this connection belongs to has any other
1743 * connection with the same path, and destroy one if so.
1745 * @param cls Closure (connection to check).
1746 * @param tc Task context.
1749 check_duplicates (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1751 struct CadetConnection *c = cls;
1753 c->check_duplicates_task = NULL;
1754 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1757 if (GNUNET_YES == does_connection_exist (c))
1759 GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1760 send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
1768 * Wait for enough time to let any dead connections time out and check for
1769 * any remaining duplicates.
1771 * @param c Connection that is a potential duplicate.
1774 schedule_check_duplicates (struct CadetConnection *c)
1776 struct GNUNET_TIME_Relative delay;
1778 if (NULL != c->check_duplicates_task)
1781 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 5);
1782 c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
1790 * Add the connection to the list of both neighbors.
1792 * @param c Connection.
1794 * @return #GNUNET_OK if everything went fine
1795 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1798 register_neighbors (struct CadetConnection *c)
1800 c->next_peer = get_next_hop (c);
1801 c->prev_peer = get_prev_hop (c);
1802 GNUNET_assert (c->next_peer != c->prev_peer);
1803 LOG (GNUNET_ERROR_TYPE_DEBUG,
1804 "register neighbors for connection %s\n",
1806 path_debug (c->path);
1807 LOG (GNUNET_ERROR_TYPE_DEBUG,
1808 "own pos %u\n", c->own_pos);
1809 LOG (GNUNET_ERROR_TYPE_DEBUG,
1810 "putting connection %s to next peer %p\n",
1813 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1815 GCP_2s (c->next_peer));
1816 LOG (GNUNET_ERROR_TYPE_DEBUG,
1817 "putting connection %s to prev peer %p\n",
1820 LOG (GNUNET_ERROR_TYPE_DEBUG,
1821 "prev peer %p %s\n",
1823 GCP_2s (c->prev_peer));
1825 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1826 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1828 if (GCC_is_origin (c, GNUNET_YES))
1829 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1830 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1832 LOG (GNUNET_ERROR_TYPE_DEBUG,
1833 " register neighbors failed\n");
1834 LOG (GNUNET_ERROR_TYPE_DEBUG,
1835 " prev: %s, neighbor?: %d\n",
1836 GCP_2s (c->prev_peer),
1837 GCP_is_neighbor (c->prev_peer));
1838 LOG (GNUNET_ERROR_TYPE_DEBUG,
1839 " next: %s, neighbor?: %d\n",
1840 GCP_2s (c->next_peer),
1841 GCP_is_neighbor (c->next_peer));
1842 return GNUNET_SYSERR;
1844 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1845 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1852 * Remove the connection from the list of both neighbors.
1854 * @param c Connection.
1857 unregister_neighbors (struct CadetConnection *c)
1859 // struct CadetPeer *peer; FIXME dont use next_peer, prev_peer
1860 /* Either already unregistered or never got registered, it's ok either way. */
1861 if (NULL == c->path)
1863 if (NULL != c->next_peer)
1865 GCP_remove_connection (c->next_peer, c);
1866 c->next_peer = NULL;
1868 if (NULL != c->prev_peer)
1870 GCP_remove_connection (c->prev_peer, c);
1871 c->prev_peer = NULL;
1877 * Invalidates all paths towards all peers that comprise the connection which
1878 * rely on the disconnected peer.
1880 * ~O(n^3) (peers in connection * paths/peer * links/path)
1882 * @param c Connection whose peers' paths to clean.
1883 * @param disconnected Peer that disconnected.
1886 invalidate_paths (struct CadetConnection *c, struct CadetPeer *disconnected)
1888 struct CadetPeer *peer;
1891 for (i = 0; i < c->path->length; i++)
1893 peer = GCP_get_short (c->path->peers[i], GNUNET_NO);
1895 GCP_notify_broken_link (peer, &my_full_id, GCP_get_id (disconnected));
1901 * Bind the connection to the peer and the tunnel to that peer.
1903 * If the peer has no tunnel, create one. Update tunnel and connection
1904 * data structres to reflect new status.
1906 * @param c Connection.
1910 add_to_peer (struct CadetConnection *c,
1911 struct CadetPeer *peer)
1913 GCP_add_tunnel (peer);
1914 c->t = GCP_get_tunnel (peer);
1915 GCT_add_connection (c->t, c);
1920 * Log receipt of message on stderr (INFO level).
1922 * @param message Message received.
1923 * @param peer Peer who sent the message.
1924 * @param hash Connection ID.
1927 log_message (const struct GNUNET_MessageHeader *message,
1928 const struct GNUNET_PeerIdentity *peer,
1929 const struct GNUNET_CADET_Hash *hash)
1935 size = ntohs (message->size);
1936 type = ntohs (message->type);
1939 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1940 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1941 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1942 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1948 LOG (GNUNET_ERROR_TYPE_INFO, "<%s %s on conn %s from %s, %6u bytes\n",
1949 arrow, GC_m2s (type), GNUNET_h2s (GC_h2hc (hash)),
1950 GNUNET_i2s (peer), (unsigned int) size);
1953 /******************************************************************************/
1954 /******************************** API ***********************************/
1955 /******************************************************************************/
1958 * Core handler for connection creation.
1960 * @param cls Closure (unused).
1961 * @param peer Sender (neighbor).
1962 * @param message Message.
1963 * @return #GNUNET_OK to keep the connection open,
1964 * #GNUNET_SYSERR to close it (signal serious error)
1967 GCC_handle_create (void *cls,
1968 const struct GNUNET_PeerIdentity *peer,
1969 const struct GNUNET_MessageHeader *message)
1971 struct GNUNET_CADET_ConnectionCreate *msg;
1972 struct GNUNET_PeerIdentity *id;
1973 struct GNUNET_CADET_Hash *cid;
1974 struct CadetPeerPath *path;
1975 struct CadetPeer *dest_peer;
1976 struct CadetPeer *orig_peer;
1977 struct CadetConnection *c;
1978 unsigned int own_pos;
1981 GCC_check_connections ();
1983 size = ntohs (message->size);
1984 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1986 GNUNET_break_op (0);
1990 /* Calculate hops */
1991 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1992 if (size % sizeof (struct GNUNET_PeerIdentity))
1994 GNUNET_break_op (0);
1997 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1999 GNUNET_break_op (0);
2002 size /= sizeof (struct GNUNET_PeerIdentity);
2005 GNUNET_break_op (0);
2008 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2010 /* Get parameters */
2011 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
2013 log_message (message, peer, cid);
2014 id = (struct GNUNET_PeerIdentity *) &msg[1];
2015 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
2017 /* Create connection */
2018 c = connection_get (cid);
2021 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
2022 size, myid, &own_pos);
2025 /* Path was malformed, probably our own ID was not in it. */
2026 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
2027 GNUNET_break_op (0);
2033 /* We received this request from a neighbor, we cannot be origin */
2034 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
2035 GNUNET_break_op (0);
2036 path_destroy (path);
2040 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2041 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
2042 c = GCC_new (cid, NULL, path, own_pos);
2045 if (path->length - 1 == own_pos)
2047 /* If we are destination, why did the creation fail? */
2049 path_destroy (path);
2050 GCC_check_connections ();
2053 send_broken_unknown (cid, &my_full_id,
2054 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
2056 path_destroy (path);
2057 GCC_check_connections ();
2060 GCP_add_path_to_all (path, GNUNET_NO);
2061 connection_reset_timeout (c, GNUNET_YES);
2065 path = path_duplicate (c->path);
2067 if (CADET_CONNECTION_NEW == c->state)
2068 connection_change_state (c, CADET_CONNECTION_SENT);
2070 /* Remember peers */
2071 dest_peer = GCP_get (&id[size - 1], GNUNET_YES);
2072 orig_peer = GCP_get (&id[0], GNUNET_YES);
2074 /* Is it a connection to us? */
2075 if (c->own_pos == path->length - 1)
2077 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2078 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
2080 add_to_peer (c, orig_peer);
2081 if (GNUNET_YES == does_connection_exist (c))
2083 /* Peer created a connection equal to one we think exists
2085 * Solution: Keep both and postpone disambiguation. In the meantime
2086 * the connection will time out or peer will inform us it is broken.
2089 * - Use explicit duplicate.
2090 * - Accept new conn and destroy the old. (interruption in higher level)
2091 * - Keep the one with higher ID / created by peer with higher ID. */
2092 schedule_check_duplicates (c);
2095 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
2096 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
2098 send_connection_ack (c, GNUNET_NO);
2099 if (CADET_CONNECTION_SENT == c->state)
2100 connection_change_state (c, CADET_CONNECTION_ACK);
2104 /* It's for somebody else! Retransmit. */
2105 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2106 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2107 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2108 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
2109 GNUNET_YES, GNUNET_YES,
2112 path_destroy (path);
2113 GCC_check_connections ();
2119 * Core handler for path confirmations.
2121 * @param cls closure
2122 * @param message message
2123 * @param peer peer identity this notification is about
2124 * @return #GNUNET_OK to keep the connection open,
2125 * #GNUNET_SYSERR to close it (signal serious error)
2128 GCC_handle_confirm (void *cls,
2129 const struct GNUNET_PeerIdentity *peer,
2130 const struct GNUNET_MessageHeader *message)
2132 struct GNUNET_CADET_ConnectionACK *msg;
2133 struct CadetConnection *c;
2134 struct CadetPeerPath *p;
2135 struct CadetPeer *pi;
2136 enum CadetConnectionState oldstate;
2139 GCC_check_connections ();
2140 msg = (struct GNUNET_CADET_ConnectionACK *) message;
2141 log_message (message, peer, &msg->cid);
2142 c = connection_get (&msg->cid);
2145 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2147 LOG (GNUNET_ERROR_TYPE_DEBUG,
2148 " don't know the connection!\n");
2149 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2150 GCC_check_connections ();
2154 if (GNUNET_NO != c->destroy)
2156 GNUNET_assert (CADET_CONNECTION_DESTROYED == c->state);
2157 LOG (GNUNET_ERROR_TYPE_DEBUG,
2158 "connection %s being destroyed, ignoring confirm\n",
2160 GCC_check_connections ();
2164 oldstate = c->state;
2165 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
2166 pi = GCP_get (peer, GNUNET_YES);
2167 if (get_next_hop (c) == pi)
2169 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2171 if (CADET_CONNECTION_SENT == oldstate)
2172 connection_change_state (c, CADET_CONNECTION_ACK);
2174 else if (get_prev_hop (c) == pi)
2176 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2178 connection_change_state (c, CADET_CONNECTION_READY);
2182 GNUNET_break_op (0);
2186 connection_reset_timeout (c, fwd);
2188 /* Add path to peers? */
2192 GCP_add_path_to_all (p, GNUNET_YES);
2199 /* Message for us as creator? */
2200 if (GCC_is_origin (c, GNUNET_YES))
2202 if (GNUNET_NO != fwd)
2204 GNUNET_break_op (0);
2207 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2209 /* If just created, cancel the short timeout and start a long one */
2210 if (CADET_CONNECTION_SENT == oldstate)
2211 connection_reset_timeout (c, GNUNET_YES);
2213 /* Change connection state */
2214 connection_change_state (c, CADET_CONNECTION_READY);
2215 send_connection_ack (c, GNUNET_YES);
2217 /* Change tunnel state, trigger KX */
2218 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2219 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2220 GCC_check_connections ();
2224 /* Message for us as destination? */
2225 if (GCC_is_terminal (c, GNUNET_YES))
2227 if (GNUNET_YES != fwd)
2229 GNUNET_break_op (0);
2232 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2234 /* If just created, cancel the short timeout and start a long one */
2235 if (CADET_CONNECTION_ACK == oldstate)
2236 connection_reset_timeout (c, GNUNET_NO);
2238 /* Change tunnel state */
2239 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2240 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2241 GCC_check_connections ();
2245 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2246 GNUNET_assert (NULL ==
2247 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2248 GNUNET_YES, NULL, NULL));
2249 GCC_check_connections ();
2255 * Core handler for notifications of broken connections.
2257 * @param cls Closure (unused).
2258 * @param id Peer identity of sending neighbor.
2259 * @param message Message.
2260 * @return #GNUNET_OK to keep the connection open,
2261 * #GNUNET_SYSERR to close it (signal serious error)
2264 GCC_handle_broken (void* cls,
2265 const struct GNUNET_PeerIdentity* id,
2266 const struct GNUNET_MessageHeader* message)
2268 struct GNUNET_CADET_ConnectionBroken *msg;
2269 struct CadetConnection *c;
2270 struct CadetTunnel *t;
2274 GCC_check_connections ();
2275 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
2276 log_message (message, id, &msg->cid);
2277 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2278 GNUNET_i2s (&msg->peer1));
2279 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2280 GNUNET_i2s (&msg->peer2));
2281 c = connection_get (&msg->cid);
2284 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2285 GCC_check_connections ();
2291 fwd = is_fwd (c, id);
2292 c->destroy = GNUNET_YES;
2293 if (GCC_is_terminal (c, fwd))
2295 struct CadetPeer *endpoint;
2299 /* A terminal connection should not have 't' set to NULL. */
2301 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2304 endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
2305 if (2 < c->path->length)
2306 path_invalidate (c->path);
2307 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2309 c->state = CADET_CONNECTION_BROKEN;
2310 GCT_remove_connection (t, c);
2313 pending = c->pending_messages;
2315 resend_messages_and_destroy (c, !fwd);
2321 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2322 GNUNET_YES, NULL, NULL));
2323 connection_cancel_queues (c, !fwd);
2325 GCC_check_connections ();
2331 * Core handler for tunnel destruction
2333 * @param cls Closure (unused).
2334 * @param peer Peer identity of sending neighbor.
2335 * @param message Message.
2336 * @return #GNUNET_OK to keep the connection open,
2337 * #GNUNET_SYSERR to close it (signal serious error)
2340 GCC_handle_destroy (void *cls,
2341 const struct GNUNET_PeerIdentity *peer,
2342 const struct GNUNET_MessageHeader *message)
2344 const struct GNUNET_CADET_ConnectionDestroy *msg;
2345 struct CadetConnection *c;
2348 GCC_check_connections ();
2349 msg = (const struct GNUNET_CADET_ConnectionDestroy *) message;
2350 log_message (message, peer, &msg->cid);
2351 c = connection_get (&msg->cid);
2354 /* Probably already got the message from another path,
2355 * destroyed the tunnel and retransmitted to children.
2358 GNUNET_STATISTICS_update (stats,
2359 "# control on unknown connection",
2361 LOG (GNUNET_ERROR_TYPE_DEBUG,
2362 " connection unknown: already destroyed?\n");
2363 GCC_check_connections ();
2366 fwd = is_fwd (c, peer);
2367 if (GNUNET_SYSERR == fwd)
2369 GNUNET_break_op (0); /* FIXME */
2372 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2374 GNUNET_assert (NULL ==
2375 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2376 GNUNET_YES, NULL, NULL));
2378 else if (0 == c->pending_messages)
2380 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
2382 GCC_check_connections ();
2385 c->destroy = GNUNET_YES;
2386 c->state = CADET_CONNECTION_DESTROYED;
2389 GCT_remove_connection (c->t, c);
2392 GCC_check_connections ();
2398 * Check the message against internal state and test if it goes FWD or BCK.
2400 * Updates the PID, state and timeout values for the connection.
2402 * @param message Message to check. It must belong to an existing connection.
2403 * @param minimum_size The message cannot be smaller than this value.
2404 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2405 * @param c Connection this message should belong. If NULL, check fails.
2406 * @param neighbor Neighbor that sent the message.
2409 check_message (const struct GNUNET_MessageHeader *message,
2410 size_t minimum_size,
2411 const struct GNUNET_CADET_Hash* cid,
2412 struct CadetConnection *c,
2413 const struct GNUNET_PeerIdentity *neighbor,
2416 GNUNET_PEER_Id neighbor_id;
2417 struct CadetFlowControl *fc;
2418 struct CadetPeer *hop;
2423 if (ntohs (message->size) < minimum_size)
2425 GNUNET_break_op (0);
2426 LOG (GNUNET_ERROR_TYPE_WARNING, "Size %u < %u\n",
2427 ntohs (message->size), minimum_size);
2428 return GNUNET_SYSERR;
2431 /* Check connection */
2434 GNUNET_STATISTICS_update (stats,
2435 "# unknown connection",
2437 LOG (GNUNET_ERROR_TYPE_DEBUG,
2438 "%s on unknown connection %s\n",
2439 GC_m2s (ntohs (message->type)),
2440 GNUNET_h2s (GC_h2hc (cid)));
2441 send_broken_unknown (cid,
2445 return GNUNET_SYSERR;
2448 /* Check if origin is as expected */
2449 neighbor_id = GNUNET_PEER_search (neighbor);
2450 hop = get_prev_hop (c);
2451 if (neighbor_id == GCP_get_short_id (hop))
2457 hop = get_next_hop (c);
2458 GNUNET_break (hop == c->next_peer);
2459 if (neighbor_id == GCP_get_short_id (hop))
2465 /* Unexpected peer sending traffic on a connection. */
2466 GNUNET_break_op (0);
2467 return GNUNET_SYSERR;
2471 /* Check PID for payload messages */
2472 type = ntohs (message->type);
2473 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
2474 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
2476 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2477 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2478 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2479 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2481 GNUNET_break_op (0);
2482 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2483 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2484 pid, fc->last_pid_recv, fc->last_ack_sent);
2485 return GNUNET_SYSERR;
2487 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2491 delta = pid - fc->last_pid_recv;
2492 fc->last_pid_recv = pid;
2493 fc->recv_bitmap <<= delta;
2494 fc->recv_bitmap |= 1;
2498 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2499 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2501 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2502 pid, fc->last_pid_recv - 31);
2503 return GNUNET_SYSERR;
2505 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2509 /* Count as connection confirmation. */
2510 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2512 connection_change_state (c, CADET_CONNECTION_READY);
2515 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2516 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2519 connection_reset_timeout (c, fwd);
2526 * Generic handler for cadet network encrypted traffic.
2528 * @param peer Peer identity this notification is about.
2529 * @param msg Encrypted message.
2530 * @return #GNUNET_OK to keep the connection open,
2531 * #GNUNET_SYSERR to close it (signal serious error)
2534 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
2535 const struct GNUNET_MessageHeader *message)
2537 const struct GNUNET_CADET_Encrypted *otr_msg;
2538 const struct GNUNET_CADET_AX *ax_msg;
2539 const struct GNUNET_CADET_Hash* cid;
2540 struct CadetConnection *c;
2541 size_t minimum_size;
2547 GCC_check_connections ();
2548 if (GNUNET_MESSAGE_TYPE_CADET_AX == ntohs (message->type))
2550 overhead = sizeof (struct GNUNET_CADET_AX);
2551 ax_msg = (const struct GNUNET_CADET_AX *) message;
2553 pid = ntohl (ax_msg->pid);
2558 overhead = sizeof (struct GNUNET_CADET_Encrypted);
2559 otr_msg = (const struct GNUNET_CADET_Encrypted *) message;
2560 cid = &otr_msg->cid;
2561 pid = ntohl (otr_msg->pid);
2564 log_message (message, peer, cid);
2566 minimum_size = sizeof (struct GNUNET_MessageHeader) + overhead;
2567 c = connection_get (cid);
2568 fwd = check_message (message,
2575 /* If something went wrong, discard message. */
2576 if (GNUNET_SYSERR == fwd)
2578 GCC_check_connections ();
2582 /* Is this message for us? */
2583 if (GCC_is_terminal (c, fwd))
2585 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2589 GNUNET_break (GNUNET_NO != c->destroy);
2592 GCT_handle_encrypted (c->t, message);
2593 GCC_send_ack (c, fwd, GNUNET_NO);
2594 GCC_check_connections ();
2598 /* Message not for us: forward to next hop */
2599 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2600 if (NULL != otr_msg) /* only otr has ttl */
2602 ttl = ntohl (otr_msg->ttl);
2603 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2606 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2607 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2608 GCC_send_ack (c, fwd, GNUNET_NO);
2609 GCC_check_connections ();
2614 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2615 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2616 GNUNET_NO, NULL, NULL));
2617 GCC_check_connections ();
2623 * Generic handler for cadet network encrypted traffic.
2625 * @param peer Peer identity this notification is about.
2626 * @param msg Encrypted message.
2627 * @return #GNUNET_OK to keep the connection open,
2628 * #GNUNET_SYSERR to close it (signal serious error)
2631 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2632 const struct GNUNET_CADET_KX *msg)
2634 const struct GNUNET_CADET_Hash* cid;
2635 struct CadetConnection *c;
2636 size_t expected_size;
2639 GCC_check_connections ();
2641 log_message (&msg->header, peer, cid);
2643 expected_size = sizeof (struct GNUNET_CADET_KX)
2644 + sizeof (struct GNUNET_MessageHeader);
2645 c = connection_get (cid);
2646 fwd = check_message (&msg->header,
2653 /* If something went wrong, discard message. */
2654 if (GNUNET_SYSERR == fwd)
2657 /* Is this message for us? */
2658 if (GCC_is_terminal (c, fwd))
2660 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2661 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2667 GCT_handle_kx (c->t, &msg[1].header);
2668 GCC_check_connections ();
2672 /* Message not for us: forward to next hop */
2673 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2674 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2675 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2676 GNUNET_NO, NULL, NULL));
2677 GCC_check_connections ();
2683 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2685 * @param cls Closure (unused).
2686 * @param message Message received.
2687 * @param peer Peer who sent the message.
2688 * @return #GNUNET_OK to keep the connection open,
2689 * #GNUNET_SYSERR to close it (signal serious error)
2692 GCC_handle_kx (void *cls,
2693 const struct GNUNET_PeerIdentity *peer,
2694 const struct GNUNET_MessageHeader *message)
2696 GCC_check_connections ();
2697 return handle_cadet_kx (peer, (struct GNUNET_CADET_KX *) message);
2702 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2704 * @param cls Closure (unused).
2705 * @param message Message received.
2706 * @param peer Peer who sent the message.
2707 * @return #GNUNET_OK to keep the connection open,
2708 * #GNUNET_SYSERR to close it (signal serious error)
2711 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2712 const struct GNUNET_MessageHeader *message)
2714 GCC_check_connections ();
2715 return handle_cadet_encrypted (peer, message);
2720 * Core handler for cadet network traffic point-to-point acks.
2722 * @param cls closure
2723 * @param message message
2724 * @param peer peer identity this notification is about
2725 * @return #GNUNET_OK to keep the connection open,
2726 * #GNUNET_SYSERR to close it (signal serious error)
2729 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2730 const struct GNUNET_MessageHeader *message)
2732 struct GNUNET_CADET_ACK *msg;
2733 struct CadetConnection *c;
2734 struct CadetFlowControl *fc;
2739 GCC_check_connections ();
2740 msg = (struct GNUNET_CADET_ACK *) message;
2741 log_message (message, peer, &msg->cid);
2742 c = connection_get (&msg->cid);
2745 GNUNET_STATISTICS_update (stats,
2746 "# ack on unknown connection",
2749 send_broken_unknown (&msg->cid,
2753 GCC_check_connections ();
2757 /* Is this a forward or backward ACK? */
2758 id = GNUNET_PEER_search (peer);
2759 if (GCP_get_short_id (get_next_hop (c)) == id)
2764 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2771 GNUNET_break_op (0);
2775 ack = ntohl (msg->ack);
2776 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2777 GC_f2s (fwd), ack, fc->last_ack_recv);
2778 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2779 fc->last_ack_recv = ack;
2781 /* Cancel polling if the ACK is big enough. */
2782 if (NULL != fc->poll_task &&
2783 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2785 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2786 GNUNET_SCHEDULER_cancel (fc->poll_task);
2787 fc->poll_task = NULL;
2788 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2791 connection_unlock_queue (c, fwd);
2792 GCC_check_connections ();
2798 * Core handler for cadet network traffic point-to-point ack polls.
2800 * @param cls closure
2801 * @param message message
2802 * @param peer peer identity this notification is about
2803 * @return #GNUNET_OK to keep the connection open,
2804 * #GNUNET_SYSERR to close it (signal serious error)
2807 GCC_handle_poll (void *cls,
2808 const struct GNUNET_PeerIdentity *peer,
2809 const struct GNUNET_MessageHeader *message)
2811 struct GNUNET_CADET_Poll *msg;
2812 struct CadetConnection *c;
2813 struct CadetFlowControl *fc;
2818 GCC_check_connections ();
2819 msg = (struct GNUNET_CADET_Poll *) message;
2820 log_message (message, peer, &msg->cid);
2821 c = connection_get (&msg->cid);
2824 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2826 LOG (GNUNET_ERROR_TYPE_DEBUG,
2827 "POLL message on unknown connection %s!\n",
2828 GNUNET_h2s (GC_h2hc (&msg->cid)));
2829 send_broken_unknown (&msg->cid,
2833 GCC_check_connections ();
2837 /* Is this a forward or backward ACK?
2838 * Note: a poll should never be needed in a loopback case,
2839 * since there is no possiblility of packet loss there, so
2840 * this way of discerining FWD/BCK should not be a problem.
2842 id = GNUNET_PEER_search (peer);
2843 if (GCP_get_short_id (get_next_hop (c)) == id)
2845 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2848 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2850 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2855 GNUNET_break_op (0);
2859 pid = ntohl (msg->pid);
2860 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2861 fc->last_pid_recv = pid;
2862 fwd = fc == &c->bck_fc;
2863 GCC_send_ack (c, fwd, GNUNET_YES);
2864 GCC_check_connections ();
2871 * Send an ACK on the appropriate connection/channel, depending on
2872 * the direction and the position of the peer.
2874 * @param c Which connection to send the hop-by-hop ACK.
2875 * @param fwd Is this a fwd ACK? (will go dest->root).
2876 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2879 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2881 unsigned int buffer;
2883 GCC_check_connections ();
2884 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
2885 GC_f2s (fwd), GCC_2s (c));
2893 if (GNUNET_NO != c->destroy)
2895 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2896 GCC_check_connections ();
2900 /* Get available buffer space */
2901 if (GCC_is_terminal (c, fwd))
2903 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2904 buffer = GCT_get_channels_buffer (c->t);
2908 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2909 buffer = GCC_get_buffer (c, fwd);
2911 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2912 if (0 == buffer && GNUNET_NO == force)
2914 GCC_check_connections ();
2918 /* Send available buffer space */
2919 if (GCC_is_origin (c, fwd))
2921 GNUNET_assert (NULL != c->t);
2922 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2923 GCT_unchoke_channels (c->t);
2927 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2928 send_ack (c, buffer, fwd, force);
2930 GCC_check_connections ();
2935 * Initialize the connections subsystem
2937 * @param c Configuration handle.
2940 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2942 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2944 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2947 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2948 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2949 GNUNET_SCHEDULER_shutdown ();
2954 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2957 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2958 "CADET", "MAX_CONNECTIONS", "MISSING");
2959 GNUNET_SCHEDULER_shutdown ();
2964 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2965 &refresh_connection_time))
2967 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2968 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2969 GNUNET_SCHEDULER_shutdown ();
2972 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2973 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2978 * Destroy each connection on shutdown.
2980 * @param cls Closure (unused).
2981 * @param key Current key code (CID, unused).
2982 * @param value Value in the hash map (`struct CadetConnection`)
2984 * @return #GNUNET_YES, because we should continue to iterate
2987 shutdown_iterator (void *cls,
2988 const struct GNUNET_HashCode *key,
2991 struct CadetConnection *c = value;
2993 c->state = CADET_CONNECTION_DESTROYED;
3000 * Shut down the connections subsystem.
3005 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n");
3006 GCC_check_connections ();
3007 GNUNET_CONTAINER_multihashmap_iterate (connections,
3010 GNUNET_CONTAINER_multihashmap_destroy (connections);
3016 * Create a connection.
3018 * @param cid Connection ID (either created locally or imposed remotely).
3019 * @param t Tunnel this connection belongs to (or NULL);
3020 * @param path Path this connection has to use (copy is made).
3021 * @param own_pos Own position in the @c path path.
3023 * @return Newly created connection, NULL in case of error (own id not in path).
3025 struct CadetConnection *
3026 GCC_new (const struct GNUNET_CADET_Hash *cid,
3027 struct CadetTunnel *t,
3028 struct CadetPeerPath *path,
3029 unsigned int own_pos)
3031 struct CadetConnection *c;
3032 struct CadetPeerPath *cpath;
3034 GCC_check_connections ();
3035 cpath = path_duplicate (path);
3036 GNUNET_assert (NULL != cpath);
3037 c = GNUNET_new (struct CadetConnection);
3039 GNUNET_assert (GNUNET_OK ==
3040 GNUNET_CONTAINER_multihashmap_put (connections,
3042 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3043 fc_init (&c->fwd_fc);
3044 fc_init (&c->bck_fc);
3049 GNUNET_assert (own_pos <= cpath->length - 1);
3050 c->own_pos = own_pos;
3053 if (GNUNET_OK != register_neighbors (c))
3057 /* We were the origin of this request, this means we have invalid
3058 * info about the paths to reach the destination. We must invalidate
3059 * the *original* path to avoid trying it again in the next minute.
3061 if (2 < path->length)
3062 path_invalidate (path);
3066 GCT_debug(t, GNUNET_ERROR_TYPE_WARNING);
3070 path_destroy (c->path);
3075 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
3076 GCC_check_connections ();
3082 GCC_destroy (struct CadetConnection *c)
3084 GCC_check_connections ();
3091 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
3092 return; /* -> message_sent -> GCC_destroy. Don't loop. */
3095 LOG (GNUNET_ERROR_TYPE_DEBUG,
3096 "destroying connection %s\n",
3098 LOG (GNUNET_ERROR_TYPE_DEBUG,
3099 " fc's f: %p, b: %p\n",
3100 &c->fwd_fc, &c->bck_fc);
3101 LOG (GNUNET_ERROR_TYPE_DEBUG,
3102 " fc tasks f: %u, b: %u\n",
3103 c->fwd_fc.poll_task,
3104 c->bck_fc.poll_task);
3106 /* Cancel all traffic */
3107 if (NULL != c->path)
3109 connection_cancel_queues (c, GNUNET_YES);
3110 connection_cancel_queues (c, GNUNET_NO);
3112 unregister_neighbors (c);
3113 path_destroy (c->path);
3116 /* Delete from tunnel */
3118 GCT_remove_connection (c->t, c);
3120 if (NULL != c->check_duplicates_task)
3121 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
3122 if (NULL != c->fwd_maintenance_task)
3123 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
3124 if (NULL != c->bck_maintenance_task)
3125 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
3127 if (GNUNET_NO == c->was_removed)
3129 GNUNET_break (GNUNET_YES ==
3130 GNUNET_CONTAINER_multihashmap_remove (connections,
3134 GNUNET_STATISTICS_update (stats,
3139 GCC_check_connections ();
3144 * Get the connection ID.
3146 * @param c Connection to get the ID from.
3148 * @return ID of the connection.
3150 const struct GNUNET_CADET_Hash *
3151 GCC_get_id (const struct CadetConnection *c)
3158 * Get the connection ID.
3160 * @param c Connection to get the ID from.
3162 * @return ID of the connection.
3164 const struct GNUNET_HashCode *
3165 GCC_get_h (const struct CadetConnection *c)
3167 return GC_h2hc (&c->id);
3172 * Get the connection path.
3174 * @param c Connection to get the path from.
3176 * @return path used by the connection.
3178 const struct CadetPeerPath *
3179 GCC_get_path (const struct CadetConnection *c)
3181 if (GNUNET_NO == c->destroy)
3188 * Get the connection state.
3190 * @param c Connection to get the state from.
3192 * @return state of the connection.
3194 enum CadetConnectionState
3195 GCC_get_state (const struct CadetConnection *c)
3201 * Get the connection tunnel.
3203 * @param c Connection to get the tunnel from.
3205 * @return tunnel of the connection.
3207 struct CadetTunnel *
3208 GCC_get_tunnel (const struct CadetConnection *c)
3215 * Get free buffer space in a connection.
3217 * @param c Connection.
3218 * @param fwd Is query about FWD traffic?
3220 * @return Free buffer space [0 - max_msgs_queue/max_connections]
3223 GCC_get_buffer (struct CadetConnection *c, int fwd)
3225 struct CadetFlowControl *fc;
3227 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3229 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
3230 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
3231 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
3233 return (fc->queue_max - fc->queue_n);
3238 * Get how many messages have we allowed to send to us from a direction.
3240 * @param c Connection.
3241 * @param fwd Are we asking about traffic from FWD (BCK messages)?
3243 * @return last_ack_sent - last_pid_recv
3246 GCC_get_allowed (struct CadetConnection *c, int fwd)
3248 struct CadetFlowControl *fc;
3250 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3251 if (CADET_CONNECTION_READY != c->state
3252 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
3256 return (fc->last_ack_sent - fc->last_pid_recv);
3261 * Get messages queued in a connection.
3263 * @param c Connection.
3264 * @param fwd Is query about FWD traffic?
3266 * @return Number of messages queued.
3269 GCC_get_qn (struct CadetConnection *c, int fwd)
3271 struct CadetFlowControl *fc;
3273 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3280 * Get next PID to use.
3282 * @param c Connection.
3283 * @param fwd Is query about FWD traffic?
3285 * @return Last PID used + 1.
3288 GCC_get_pid (struct CadetConnection *c, int fwd)
3290 struct CadetFlowControl *fc;
3292 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3294 return fc->last_pid_sent + 1;
3299 * Allow the connection to advertise a buffer of the given size.
3301 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3302 * allowing up to last_pid_recv + buffer.
3304 * @param c Connection.
3305 * @param buffer How many more messages the connection can accept.
3306 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3309 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3311 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3312 GCC_2s (c), buffer, GC_f2s (fwd));
3313 send_ack (c, buffer, fwd, GNUNET_NO);
3318 * Notify other peers on a connection of a broken link. Mark connections
3319 * to destroy after all traffic has been sent.
3321 * @param c Connection on which there has been a disconnection.
3322 * @param peer Peer that disconnected.
3325 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
3327 struct CadetFlowControl *fc;
3328 struct CadetPeer *hop;
3332 GCC_check_connections ();
3333 strncpy (peer_name, GCP_2s (peer), 16);
3334 peer_name[15] = '\0';
3335 LOG (GNUNET_ERROR_TYPE_DEBUG,
3336 "shutting down %s, %s disconnected\n",
3337 GCC_2s (c), peer_name);
3339 invalidate_paths (c, peer);
3341 hop = get_prev_hop (c);
3344 /* Path was NULL, we should have deleted the connection. */
3348 fwd = (peer == hop);
3349 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3350 (GNUNET_NO != c->destroy) )
3352 /* Local shutdown, or other peer already down (hence 'c->destroy');
3353 so there is no one to notify about this, just clean up. */
3355 GCC_check_connections ();
3358 /* Mark FlowControl towards the peer as unavaliable. */
3359 fc = fwd ? &c->bck_fc : &c->fwd_fc;
3362 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3364 /* Connection will have at least one pending message
3365 * (the one we just scheduled), so delay destruction
3366 * and remove from map so we don't use accidentally. */
3367 c->destroy = GNUNET_YES;
3368 c->state = CADET_CONNECTION_DESTROYED;
3369 GNUNET_assert (GNUNET_NO == c->was_removed);
3370 c->was_removed = GNUNET_YES;
3371 GNUNET_break (GNUNET_YES ==
3372 GNUNET_CONTAINER_multihashmap_remove (connections,
3375 /* Cancel queue in the direction that just died. */
3376 connection_cancel_queues (c, ! fwd);
3377 GCC_stop_poll (c, ! fwd);
3378 unregister_neighbors (c);
3379 GCC_check_connections ();
3384 * Is this peer the first one on the connection?
3386 * @param c Connection.
3387 * @param fwd Is this about fwd traffic?
3389 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3392 GCC_is_origin (struct CadetConnection *c, int fwd)
3394 if (!fwd && c->path->length - 1 == c->own_pos )
3396 if (fwd && 0 == c->own_pos)
3403 * Is this peer the last one on the connection?
3405 * @param c Connection.
3406 * @param fwd Is this about fwd traffic?
3407 * Note that the ROOT is the terminal for BCK traffic!
3409 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3412 GCC_is_terminal (struct CadetConnection *c, int fwd)
3414 return GCC_is_origin (c, ! fwd);
3419 * See if we are allowed to send by the next hop in the given direction.
3421 * @param c Connection.
3422 * @param fwd Is this about fwd traffic?
3424 * @return #GNUNET_YES in case it's OK to send.
3427 GCC_is_sendable (struct CadetConnection *c, int fwd)
3429 struct CadetFlowControl *fc;
3431 LOG (GNUNET_ERROR_TYPE_DEBUG,
3432 " checking sendability of %s traffic on %s\n",
3433 GC_f2s (fwd), GCC_2s (c));
3439 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3440 LOG (GNUNET_ERROR_TYPE_DEBUG,
3441 " last ack recv: %u, last pid sent: %u\n",
3442 fc->last_ack_recv, fc->last_pid_sent);
3443 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3445 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3448 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3454 * Check if this connection is a direct one (never trim a direct connection).
3456 * @param c Connection.
3458 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3461 GCC_is_direct (struct CadetConnection *c)
3463 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3468 * Sends an already built message on a connection, properly registering
3469 * all used resources.
3471 * @param message Message to send. Function makes a copy of it.
3472 * If message is not hop-by-hop, decrements TTL of copy.
3473 * @param payload_type Type of payload, in case the message is encrypted.
3474 * @param c Connection on which this message is transmitted.
3475 * @param fwd Is this a fwd message?
3476 * @param force Force the connection to accept the message (buffer overfill).
3477 * @param cont Continuation called once message is sent. Can be NULL.
3478 * @param cont_cls Closure for @c cont.
3480 * @return Handle to cancel the message before it's sent.
3481 * NULL on error or if @c cont is NULL.
3482 * Invalid on @c cont call.
3484 struct CadetConnectionQueue *
3485 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3486 uint16_t payload_type, uint32_t payload_id,
3487 struct CadetConnection *c, int fwd, int force,
3488 GCC_sent cont, void *cont_cls)
3490 struct CadetFlowControl *fc;
3491 struct CadetConnectionQueue *q;
3497 GCC_check_connections ();
3498 size = ntohs (message->size);
3499 data = GNUNET_malloc (size);
3500 memcpy (data, message, size);
3501 type = ntohs (message->type);
3502 LOG (GNUNET_ERROR_TYPE_INFO,
3503 "--> %s (%s %4u) on conn %s (%p) %s [%5u]\n",
3504 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), c,
3507 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3508 droppable = GNUNET_NO == force;
3511 struct GNUNET_CADET_AX *axmsg;
3512 struct GNUNET_CADET_Encrypted *emsg;
3513 struct GNUNET_CADET_KX *kmsg;
3514 struct GNUNET_CADET_ACK *amsg;
3515 struct GNUNET_CADET_Poll *pmsg;
3516 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3517 struct GNUNET_CADET_ConnectionBroken *bmsg;
3520 case GNUNET_MESSAGE_TYPE_CADET_AX:
3521 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3522 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3524 emsg = (struct GNUNET_CADET_Encrypted *) data;
3525 ttl = ntohl (emsg->ttl);
3528 GNUNET_break_op (0);
3533 emsg->ttl = htonl (ttl - 1);
3537 axmsg = (struct GNUNET_CADET_AX *) data;
3540 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3541 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3542 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3543 if (GNUNET_YES == droppable)
3549 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3553 case GNUNET_MESSAGE_TYPE_CADET_KX:
3554 kmsg = (struct GNUNET_CADET_KX *) data;
3558 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3559 amsg = (struct GNUNET_CADET_ACK *) data;
3561 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3562 droppable = GNUNET_NO;
3565 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3566 pmsg = (struct GNUNET_CADET_Poll *) data;
3568 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
3569 droppable = GNUNET_NO;
3572 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3573 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
3577 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3578 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
3582 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3583 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3592 if (fc->queue_n > fc->queue_max && droppable)
3594 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3597 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3598 fc->queue_n, fc->queue_max);
3599 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
3600 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
3605 return NULL; /* Drop this message */
3608 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3609 GCC_2s (c), c->pending_messages);
3610 c->pending_messages++;
3612 q = GNUNET_new (struct CadetConnectionQueue);
3613 q->forced = !droppable;
3614 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3615 size, c, fwd, &conn_message_sent, q);
3618 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3621 GCC_check_connections ();
3625 q->cont_cls = cont_cls;
3626 GCC_check_connections ();
3627 return (NULL == cont) ? NULL : q;
3632 * Cancel a previously sent message while it's in the queue.
3634 * ONLY can be called before the continuation given to the send function
3635 * is called. Once the continuation is called, the message is no longer in the
3638 * @param q Handle to the queue.
3641 GCC_cancel (struct CadetConnectionQueue *q)
3643 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3645 /* queue destroy calls message_sent, which calls q->cont and frees q */
3646 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3647 GCC_check_connections ();
3652 * Sends a CREATE CONNECTION message for a path to a peer.
3653 * Changes the connection and tunnel states if necessary.
3655 * @param connection Connection to create.
3658 GCC_send_create (struct CadetConnection *connection)
3660 enum CadetTunnelCState state;
3663 GCC_check_connections ();
3664 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3665 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3667 LOG (GNUNET_ERROR_TYPE_INFO, "==> %s %19s on conn %s (%p) FWD [%5u]\n",
3668 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE), "",
3669 GCC_2s (connection), connection, size);
3670 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3671 connection, connection->pending_messages);
3672 connection->pending_messages++;
3674 connection->maintenance_q =
3675 GCP_queue_add (get_next_hop (connection), NULL,
3676 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, UINT16_MAX, 0,
3677 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3679 state = GCT_get_cstate (connection->t);
3680 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3681 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3682 if (CADET_CONNECTION_NEW == connection->state)
3683 connection_change_state (connection, CADET_CONNECTION_SENT);
3684 GCC_check_connections ();
3689 * Send a message to all peers in this connection that the connection
3690 * is no longer valid.
3692 * If some peer should not receive the message, it should be zero'ed out
3693 * before calling this function.
3695 * @param c The connection whose peers to notify.
3698 GCC_send_destroy (struct CadetConnection *c)
3700 struct GNUNET_CADET_ConnectionDestroy msg;
3702 if (GNUNET_YES == c->destroy)
3704 GCC_check_connections ();
3705 msg.header.size = htons (sizeof (msg));
3706 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3708 LOG (GNUNET_ERROR_TYPE_DEBUG,
3709 " sending connection destroy for connection %s\n",
3712 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3713 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, UINT16_MAX,
3715 GNUNET_YES, NULL, NULL));
3716 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3717 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, UINT16_MAX,
3719 GNUNET_YES, NULL, NULL));
3720 c->destroy = GNUNET_YES;
3721 c->state = CADET_CONNECTION_DESTROYED;
3722 GCC_check_connections ();
3727 * @brief Start a polling timer for the connection.
3729 * When a neighbor does not accept more traffic on the connection it could be
3730 * caused by a simple congestion or by a lost ACK. Polling enables to check
3731 * for the lastest ACK status for a connection.
3733 * @param c Connection.
3734 * @param fwd Should we poll in the FWD direction?
3737 GCC_start_poll (struct CadetConnection *c, int fwd)
3739 struct CadetFlowControl *fc;
3741 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3742 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3744 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3746 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL already in progress (t: %p, m: %p)\n",
3747 fc->poll_task, fc->poll_msg);
3750 if (0 == fc->queue_max)
3752 /* Should not be needed, traffic should've been cancelled. */
3754 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not possible, peer disconnected\n");
3757 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3758 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3765 * @brief Stop polling a connection for ACKs.
3767 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3769 * @param c Connection.
3770 * @param fwd Should we stop the poll in the FWD direction?
3773 GCC_stop_poll (struct CadetConnection *c, int fwd)
3775 struct CadetFlowControl *fc;
3777 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3778 if (NULL != fc->poll_task)
3780 GNUNET_SCHEDULER_cancel (fc->poll_task);
3781 fc->poll_task = NULL;
3783 if (NULL != fc->poll_msg)
3785 GCC_cancel (fc->poll_msg);
3786 fc->poll_msg = NULL;
3792 * Get a (static) string for a connection.
3794 * @param c Connection.
3797 GCC_2s (const struct CadetConnection *c)
3804 static char buf[128];
3806 SPRINTF (buf, "%s (->%s)",
3807 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3810 return GNUNET_h2s (GC_h2hc (&c->id));
3815 * Log all possible info about the connection state.
3817 * @param c Connection to debug.
3818 * @param level Debug level to use.
3821 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3826 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3828 __FILE__, __FUNCTION__, __LINE__);
3834 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3838 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3839 s = path_2s (c->path);
3840 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3842 LOG2 (level, "CCC state: %s, destroy: %u\n",
3843 GCC_state2s (c->state), c->destroy);
3844 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3845 if (NULL != c->perf)
3846 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3848 LOG2 (level, "CCC FWD flow control:\n");
3849 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3850 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3851 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3852 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3853 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3854 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3855 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3856 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3858 LOG2 (level, "CCC BCK flow control:\n");
3859 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3860 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3861 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3862 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3863 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3864 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3865 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3866 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3868 LOG2 (level, "CCC DEBUG CONNECTION END\n");