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.
75 unsigned int queue_max;
78 * ID of the last packet sent towards the peer.
80 uint32_t last_pid_sent;
83 * ID of the last packet received from the peer.
85 uint32_t last_pid_recv;
88 * Bitmap of past 32 messages received:
89 * - LSB being @c last_pid_recv.
90 * - MSB being @c last_pid_recv - 31 (mod UINTMAX).
95 * Last ACK sent to the peer (peer can't send more than this PID).
97 uint32_t last_ack_sent;
100 * Last ACK sent towards the origin (for traffic towards leaf node).
102 uint32_t last_ack_recv;
105 * Task to poll the peer in case of a lost ACK causes stall.
107 struct GNUNET_SCHEDULER_Task *poll_task;
110 * How frequently to poll for ACKs.
112 struct GNUNET_TIME_Relative poll_time;
115 * Queued poll message, to cancel if not necessary anymore (got ACK).
117 struct CadetConnectionQueue *poll_msg;
120 * Queued poll message, to cancel if not necessary anymore (got ACK).
122 struct CadetConnectionQueue *ack_msg;
126 * Keep a record of the last messages sent on this connection.
128 struct CadetConnectionPerformance
131 * Circular buffer for storing measurements.
133 double usecsperbyte[AVG_MSGS];
136 * Running average of @c usecsperbyte.
141 * How many values of @c usecsperbyte are valid.
146 * Index of the next "free" position in @c usecsperbyte.
153 * Struct containing all information regarding a connection to a peer.
155 struct CadetConnection
158 * Tunnel this connection is part of.
160 struct CadetTunnel *t;
163 * Flow control information for traffic fwd.
165 struct CadetFlowControl fwd_fc;
168 * Flow control information for traffic bck.
170 struct CadetFlowControl bck_fc;
173 * Measure connection performance on the endpoint.
175 struct CadetConnectionPerformance *perf;
178 * ID of the connection.
180 struct GNUNET_CADET_Hash id;
183 * Path being used for the tunnel. At the origin of the connection
184 * it's a pointer to the destination's path pool, otherwise just a copy.
186 struct CadetPeerPath *path;
189 * Task to keep the used paths alive at the owner,
190 * time tunnel out on all the other peers.
192 struct GNUNET_SCHEDULER_Task *fwd_maintenance_task;
195 * Task to keep the used paths alive at the destination,
196 * time tunnel out on all the other peers.
198 struct GNUNET_SCHEDULER_Task *bck_maintenance_task;
201 * Queue handle for maintainance traffic. One handle for FWD and BCK since
202 * one peer never needs to maintain both directions (no loopback connections).
204 struct CadetPeerQueue *maintenance_q;
207 * Should equal #get_next_hop(), or NULL if that peer disconnected.
209 struct CadetPeer *next_peer;
212 * Should equal #get_prev_hop(), or NULL if that peer disconnected.
214 struct CadetPeer *prev_peer;
217 * State of the connection.
219 enum CadetConnectionState state;
222 * Position of the local peer in the path.
224 unsigned int own_pos;
227 * Pending message count.
229 unsigned int pending_messages;
233 * - if 0, connection in use.
234 * - if 1, destroy on last message.
235 * - if 2, connection is being destroyed don't re-enter.
240 * In-connection-map flag. Sometimes, when @e destroy is set but
241 * actual destruction is delayed to enable us to finish processing
242 * queues (i.e. in the direction that is still working), we remove
243 * the connection from the map to prevent it from still being
244 * found (and used) by accident. This flag is set to #GNUNET_YES
245 * for a connection that is not in the #connections map. Should
246 * only be #GNUNET_YES if #destroy is also non-zero.
251 * Counter to do exponential backoff when creating a connection (max 64).
253 unsigned short create_retry;
256 * Task to check if connection has duplicates.
258 struct GNUNET_SCHEDULER_Task *check_duplicates_task;
263 * Handle for messages queued but not yet sent.
265 struct CadetConnectionQueue
268 * Peer queue handle, to cancel if necessary.
270 struct CadetPeerQueue *q;
273 * Continuation to call once sent.
278 * Closure for @e cont.
283 * Was this a forced message? (Do not account for it)
288 /******************************************************************************/
289 /******************************* GLOBALS ***********************************/
290 /******************************************************************************/
293 * Global handle to the statistics service.
295 extern struct GNUNET_STATISTICS_Handle *stats;
298 * Local peer own ID (memory efficient handle).
300 extern GNUNET_PEER_Id myid;
303 * Local peer own ID (full value).
305 extern struct GNUNET_PeerIdentity my_full_id;
308 * Connections known, indexed by cid (CadetConnection).
310 static struct GNUNET_CONTAINER_MultiHashMap *connections;
313 * How many connections are we willing to maintain.
314 * Local connections are always allowed, even if there are more connections than max.
316 static unsigned long long max_connections;
319 * How many messages *in total* are we willing to queue, divide by number of
320 * connections to get connection queue size.
322 static unsigned long long max_msgs_queue;
325 * How often to send path keepalives. Paths timeout after 4 missed.
327 static struct GNUNET_TIME_Relative refresh_connection_time;
330 * How often to send path create / ACKs.
332 static struct GNUNET_TIME_Relative create_connection_time;
335 /******************************************************************************/
336 /******************************** STATIC ***********************************/
337 /******************************************************************************/
341 #if 0 // avoid compiler warning for unused static function
343 fc_debug (struct CadetFlowControl *fc)
345 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
346 fc->last_pid_recv, fc->last_ack_sent);
347 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
348 fc->last_pid_sent, fc->last_ack_recv);
349 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
350 fc->queue_n, fc->queue_max);
354 connection_debug (struct CadetConnection *c)
358 LOG (GNUNET_ERROR_TYPE_INFO, "DEBUG NULL CONNECTION\n");
361 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
362 peer2s (c->t->peer), GCC_2s (c));
363 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
364 c->state, c->pending_messages);
365 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
366 fc_debug (&c->fwd_fc);
367 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
368 fc_debug (&c->bck_fc);
374 * Schedule next keepalive task, taking in consideration
375 * the connection state and number of retries.
377 * @param c Connection for which to schedule the next keepalive.
378 * @param fwd Direction for the next keepalive.
381 schedule_next_keepalive (struct CadetConnection *c, int fwd);
385 * Resets the connection timeout task, some other message has done the
387 * - For the first peer on the direction this means to send
388 * a keepalive or a path confirmation message (either create or ACK).
389 * - For all other peers, this means to destroy the connection,
390 * due to lack of activity.
391 * Starts the timeout if no timeout was running (connection just created).
393 * @param c Connection whose timeout to reset.
394 * @param fwd Is this forward?
397 connection_reset_timeout (struct CadetConnection *c, int fwd);
401 * Get string description for tunnel state. Reentrant.
403 * @param s Tunnel state.
405 * @return String representation.
408 GCC_state2s (enum CadetConnectionState s)
412 case CADET_CONNECTION_NEW:
413 return "CADET_CONNECTION_NEW";
414 case CADET_CONNECTION_SENT:
415 return "CADET_CONNECTION_SENT";
416 case CADET_CONNECTION_ACK:
417 return "CADET_CONNECTION_ACK";
418 case CADET_CONNECTION_READY:
419 return "CADET_CONNECTION_READY";
420 case CADET_CONNECTION_DESTROYED:
421 return "CADET_CONNECTION_DESTROYED";
422 case CADET_CONNECTION_BROKEN:
423 return "CADET_CONNECTION_BROKEN";
426 LOG (GNUNET_ERROR_TYPE_ERROR, " conn state %u unknown!\n", s);
427 return "CADET_CONNECTION_STATE_ERROR";
433 * Initialize a Flow Control structure to the initial state.
435 * @param fc Flow Control structure to initialize.
438 fc_init (struct CadetFlowControl *fc)
440 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
441 fc->last_pid_recv = (uint32_t) -1;
442 fc->last_ack_sent = (uint32_t) 0;
443 fc->last_ack_recv = (uint32_t) 0;
444 fc->poll_task = NULL;
445 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
447 fc->queue_max = (max_msgs_queue / max_connections) + 1;
454 * @param cid Connection ID.
456 static struct CadetConnection *
457 connection_get (const struct GNUNET_CADET_Hash *cid)
459 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
464 connection_change_state (struct CadetConnection* c,
465 enum CadetConnectionState state)
467 LOG (GNUNET_ERROR_TYPE_DEBUG,
468 "Connection %s state %s -> %s\n",
469 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
470 if (CADET_CONNECTION_DESTROYED <= c->state) /* Destroyed or broken. */
472 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
476 if (CADET_CONNECTION_READY == state)
482 * Callback called when a queued ACK message is sent.
484 * @param cls Closure (FC).
485 * @param c Connection this message was on.
486 * @param q Queue handler this call invalidates.
487 * @param type Type of message sent.
488 * @param fwd Was this a FWD going message?
489 * @param size Size of the message.
493 struct CadetConnection *c,
494 struct CadetConnectionQueue *q,
495 uint16_t type, int fwd, size_t size)
497 struct CadetFlowControl *fc = cls;
504 * Send an ACK on the connection, informing the predecessor about
505 * the available buffer space. Should not be called in case the peer
506 * is origin (no predecessor) in the @c fwd direction.
508 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
509 * the ACK itself goes "back" (dest->root).
511 * @param c Connection on which to send the ACK.
512 * @param buffer How much space free to advertise?
513 * @param fwd Is this FWD ACK? (Going dest -> root)
514 * @param force Don't optimize out.
517 send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
519 struct CadetFlowControl *next_fc;
520 struct CadetFlowControl *prev_fc;
521 struct GNUNET_CADET_ACK msg;
525 /* If origin, there is no connection to send ACKs. Wrong function! */
526 GCC_check_connections ();
527 if (GCC_is_origin (c, fwd))
529 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
530 GCC_2s (c), GC_f2s (fwd));
535 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
536 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
538 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
539 GC_f2s (fwd), GCC_2s (c));
541 /* Check if we need to transmit the ACK. */
542 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
543 if (3 < delta && buffer < delta && GNUNET_NO == force)
545 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
546 LOG (GNUNET_ERROR_TYPE_DEBUG,
547 " last pid recv: %u, last ack sent: %u\n",
548 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
549 GCC_check_connections ();
553 /* Ok, ACK might be necessary, what PID to ACK? */
554 ack = prev_fc->last_pid_recv + buffer;
555 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
556 LOG (GNUNET_ERROR_TYPE_DEBUG,
557 " last pid %u, last ack %u, qmax %u, q %u\n",
558 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
559 next_fc->queue_max, next_fc->queue_n);
560 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
562 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
563 GCC_check_connections ();
567 /* Check if message is already in queue */
568 if (NULL != prev_fc->ack_msg)
570 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
572 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
573 GCC_cancel (prev_fc->ack_msg);
574 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
578 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
579 GCC_check_connections ();
584 prev_fc->last_ack_sent = ack;
586 /* Build ACK message and send on connection */
587 msg.header.size = htons (sizeof (msg));
588 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
589 msg.ack = htonl (ack);
592 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, 0, ack, c,
595 GNUNET_assert (NULL != prev_fc->ack_msg);
596 GCC_check_connections ();
601 * Callback called when a connection queued message is sent.
603 * Calculates the average time and connection packet tracking.
605 * @param cls Closure (ConnectionQueue Handle).
606 * @param c Connection this message was on.
607 * @param sent Was it really sent? (Could have been canceled)
608 * @param type Type of message sent.
609 * @param pid Packet ID, or 0 if not applicable (create, destroy, etc).
610 * @param fwd Was this a FWD going message?
611 * @param size Size of the message.
612 * @param wait Time spent waiting for core (only the time for THIS message)
613 * @return #GNUNET_YES if connection was destroyed, #GNUNET_NO otherwise.
616 conn_message_sent (void *cls,
617 struct CadetConnection *c, int sent,
618 uint16_t type, uint32_t pid, int fwd, size_t size,
619 struct GNUNET_TIME_Relative wait)
621 struct CadetConnectionPerformance *p;
622 struct CadetFlowControl *fc;
623 struct CadetConnectionQueue *q = cls;
627 GCC_check_connections ();
628 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
630 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
632 fc = fwd ? &c->fwd_fc : &c->bck_fc;
633 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
634 sent ? "" : "not ", GC_f2s (fwd), GC_m2s (type), pid);
640 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
641 q->cont (q->cont_cls, c, q, type, fwd, size);
645 else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED
646 || type == GNUNET_MESSAGE_TYPE_CADET_AX)
648 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
657 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
658 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
660 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
663 GCC_check_connections ();
666 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
667 c->pending_messages--;
668 if ( (GNUNET_YES == c->destroy) &&
669 (0 == c->pending_messages) )
671 LOG (GNUNET_ERROR_TYPE_DEBUG,
672 "! destroying connection!\n");
674 GCC_check_connections ();
677 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
680 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
681 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
682 c->maintenance_q = NULL;
683 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
684 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
685 schedule_next_keepalive (c, fwd);
688 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
689 case GNUNET_MESSAGE_TYPE_CADET_AX:
690 if (GNUNET_YES == sent)
692 GNUNET_assert (NULL != q);
693 fc->last_pid_sent = pid;
694 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
695 GCC_start_poll (c, fwd);
696 GCC_send_ack (c, fwd, GNUNET_NO);
697 connection_reset_timeout (c, fwd);
700 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
701 if (GNUNET_NO == forced)
704 LOG (GNUNET_ERROR_TYPE_DEBUG,
705 "! accounting pid %u\n",
710 LOG (GNUNET_ERROR_TYPE_DEBUG,
711 "! forced, Q_N not accounting pid %u\n",
716 case GNUNET_MESSAGE_TYPE_CADET_KX:
717 if (GNUNET_YES == sent)
718 connection_reset_timeout (c, fwd);
721 case GNUNET_MESSAGE_TYPE_CADET_POLL:
725 case GNUNET_MESSAGE_TYPE_CADET_ACK:
729 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
730 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
734 LOG (GNUNET_ERROR_TYPE_ERROR, "%s unknown\n", GC_m2s (type));
738 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
741 return GNUNET_NO; /* Only endpoints are interested in timing. */
744 usecsperbyte = ((double) wait.rel_value_us) / size;
745 if (p->size == AVG_MSGS)
747 /* Array is full. Substract oldest value, add new one and store. */
748 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
749 p->usecsperbyte[p->idx] = usecsperbyte;
750 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
754 /* Array not yet full. Add current value to avg and store. */
755 p->usecsperbyte[p->idx] = usecsperbyte;
757 p->avg += p->usecsperbyte[p->idx];
761 p->idx = (p->idx + 1) % AVG_MSGS;
762 GCC_check_connections ();
768 * Get the previous hop in a connection
770 * @param c Connection.
772 * @return Previous peer in the connection.
774 static struct CadetPeer *
775 get_prev_hop (const struct CadetConnection *c)
781 LOG (GNUNET_ERROR_TYPE_DEBUG,
782 " get prev hop %s [%u/%u]\n",
783 GCC_2s (c), c->own_pos, c->path->length);
784 if (0 == c->own_pos || c->path->length < 2)
785 id = c->path->peers[0];
787 id = c->path->peers[c->own_pos - 1];
789 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
790 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
792 return GCP_get_short (id, GNUNET_YES);
797 * Get the next hop in a connection
799 * @param c Connection.
801 * @return Next peer in the connection.
803 static struct CadetPeer *
804 get_next_hop (const struct CadetConnection *c)
811 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
812 GCC_2s (c), c->own_pos, c->path->length);
813 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
814 id = c->path->peers[c->path->length - 1];
816 id = c->path->peers[c->own_pos + 1];
818 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
819 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
821 return GCP_get_short (id, GNUNET_YES);
826 * Check that the direct neighbours (previous and next hop)
827 * are properly associated with this connection.
829 * @param c connection to check
832 check_neighbours (const struct CadetConnection *c)
835 return; /* nothing to check */
836 GCP_check_connection (get_next_hop (c), c);
837 GCP_check_connection (get_prev_hop (c), c);
842 * Helper for #check_connections(). Calls #check_neighbours().
846 * @param value the `struct CadetConnection` to check
847 * @return #GNUNET_OK (continue to iterate)
850 check_connection (void *cls,
851 const struct GNUNET_HashCode *key,
854 struct CadetConnection *c = value;
856 check_neighbours (c);
862 * Check invariants for all connections using #check_neighbours().
865 GCC_check_connections ()
867 if (0 == CHECK_INVARIANTS)
869 if (NULL == connections)
871 GNUNET_CONTAINER_multihashmap_iterate (connections,
878 * Get the hop in a connection.
880 * @param c Connection.
881 * @param fwd Next in the FWD direction?
883 * @return Next peer in the connection.
885 static struct CadetPeer *
886 get_hop (struct CadetConnection *c, int fwd)
888 return (fwd) ? get_next_hop (c) : get_prev_hop (c);
893 * Get a bit mask for a message received out-of-order.
895 * @param last_pid_recv Last PID we received prior to the out-of-order.
896 * @param ooo_pid PID of the out-of-order message.
899 get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
901 return 1 << (last_pid_recv - ooo_pid);
906 * Check is an out-of-order message is ok:
907 * - at most 31 messages behind.
910 * @param last_pid_recv Last in-order PID received.
913 is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
917 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
920 mask = get_recv_bitmask (last_pid_recv, ooo_pid);
921 if (0 != (ooo_bitmap & mask))
929 * Is traffic coming from this sender 'FWD' traffic?
931 * @param c Connection to check.
932 * @param sender Peer identity of neighbor.
934 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
935 * the traffic is 'FWD'.
936 * #GNUNET_NO for BCK.
937 * #GNUNET_SYSERR for errors.
940 is_fwd (const struct CadetConnection *c,
941 const struct GNUNET_PeerIdentity *sender)
945 id = GNUNET_PEER_search (sender);
946 if (GCP_get_short_id (get_prev_hop (c)) == id)
949 if (GCP_get_short_id (get_next_hop (c)) == id)
953 return GNUNET_SYSERR;
958 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
959 * or a first CONNECTION_ACK directed to us.
961 * @param connection Connection to confirm.
962 * @param fwd Should we send it FWD? (root->dest)
963 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
966 send_connection_ack (struct CadetConnection *connection, int fwd)
968 struct CadetTunnel *t;
970 GCC_check_connections ();
972 LOG (GNUNET_ERROR_TYPE_INFO, "---> {%14s ACK} on connection %s\n",
973 GC_f2s (!fwd), GCC_2s (connection));
974 GCP_queue_add (get_hop (connection, fwd), NULL,
975 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0, 0,
976 sizeof (struct GNUNET_CADET_ConnectionACK),
977 connection, fwd, &conn_message_sent, NULL);
978 connection->pending_messages++;
979 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
980 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
981 if (CADET_CONNECTION_READY != connection->state)
982 connection_change_state (connection, CADET_CONNECTION_SENT);
983 GCC_check_connections ();
988 * Send a notification that a connection is broken.
990 * @param c Connection that is broken.
991 * @param id1 Peer that has disconnected.
992 * @param id2 Peer that has disconnected.
993 * @param fwd Direction towards which to send it.
996 send_broken (struct CadetConnection *c,
997 const struct GNUNET_PeerIdentity *id1,
998 const struct GNUNET_PeerIdentity *id2,
1001 struct GNUNET_CADET_ConnectionBroken msg;
1003 GCC_check_connections ();
1004 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1005 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1009 GNUNET_assert (NULL ==
1010 GCC_send_prebuilt_message (&msg.header, 0, 0, c, fwd,
1011 GNUNET_YES, NULL, NULL));
1012 GCC_check_connections ();
1017 * Send a notification that a connection is broken, when a connection
1018 * isn't even known to the local peer or soon to be destroyed.
1020 * @param connection_id Connection ID.
1021 * @param id1 Peer that has disconnected, probably local peer.
1022 * @param id2 Peer that has disconnected can be NULL if unknown.
1023 * @param peer Peer to notify (neighbor who sent the connection).
1026 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
1027 const struct GNUNET_PeerIdentity *id1,
1028 const struct GNUNET_PeerIdentity *id2,
1029 const struct GNUNET_PeerIdentity *peer_id)
1031 struct GNUNET_CADET_ConnectionBroken *msg;
1032 struct CadetPeer *neighbor;
1034 GCC_check_connections ();
1035 LOG (GNUNET_ERROR_TYPE_INFO, "---> BROKEN on unknown connection %s\n",
1036 GNUNET_h2s (GC_h2hc (connection_id)));
1038 msg = GNUNET_new (struct GNUNET_CADET_ConnectionBroken);
1039 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1040 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1041 msg->cid = *connection_id;
1046 memset (&msg->peer2, 0, sizeof (msg->peer2));
1047 neighbor = GCP_get (peer_id, GNUNET_NO); /* We MUST know neighbor. */
1048 GNUNET_assert (NULL != neighbor);
1049 GCP_queue_add (neighbor, msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
1050 0, 2, sizeof (struct GNUNET_CADET_ConnectionBroken),
1051 NULL, GNUNET_SYSERR, /* connection, fwd */
1052 NULL, NULL); /* continuation */
1053 GCC_check_connections ();
1058 * Send keepalive packets for a connection.
1060 * @param c Connection to keep alive..
1061 * @param fwd Is this a FWD keepalive? (owner -> dest).
1064 send_connection_keepalive (struct CadetConnection *c, int fwd)
1066 struct GNUNET_MessageHeader msg;
1067 struct CadetFlowControl *fc;
1069 GCC_check_connections ();
1070 LOG (GNUNET_ERROR_TYPE_INFO,
1071 "keepalive %s for connection %s\n",
1072 GC_f2s (fwd), GCC_2s (c));
1074 GNUNET_assert (NULL != c->t);
1075 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1076 if (0 < fc->queue_n || GNUNET_YES == GCT_has_queued_traffic (c->t))
1078 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1082 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1084 GNUNET_assert (NULL != c->t);
1085 msg.size = htons (sizeof (msg));
1086 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
1088 GNUNET_assert (NULL ==
1089 GCT_send_prebuilt_message (&msg, c->t, c,
1090 GNUNET_NO, NULL, NULL));
1091 GCC_check_connections ();
1096 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1098 * @param c Connection for which to send the message.
1099 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1102 connection_recreate (struct CadetConnection *c, int fwd)
1104 LOG (GNUNET_ERROR_TYPE_DEBUG,
1105 "sending connection recreate\n");
1107 GCC_send_create (c);
1109 send_connection_ack (c, GNUNET_NO);
1114 * Generic connection timer management.
1115 * Depending on the role of the peer in the connection will send the
1116 * appropriate message (build or keepalive)
1118 * @param c Conncetion to maintain.
1119 * @param fwd Is FWD?
1122 connection_maintain (struct CadetConnection *c, int fwd)
1124 if (GNUNET_NO != c->destroy)
1126 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1133 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1137 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1139 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1141 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1142 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1143 schedule_next_keepalive (c, fwd);
1148 case CADET_CONNECTION_NEW:
1151 case CADET_CONNECTION_SENT:
1152 connection_recreate (c, fwd);
1154 case CADET_CONNECTION_READY:
1155 send_connection_keepalive (c, fwd);
1164 * Keep the connection alive.
1166 * @param c Connection to keep alive.
1167 * @param fwd Direction.
1168 * @param shutdown Are we shutting down? (Don't send traffic)
1169 * Non-zero value for true, not necessarily GNUNET_YES.
1172 connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
1174 GCC_check_connections ();
1175 LOG (GNUNET_ERROR_TYPE_DEBUG,
1176 "%s keepalive for %s\n",
1177 GC_f2s (fwd), GCC_2s (c));
1180 c->fwd_maintenance_task = NULL;
1182 c->bck_maintenance_task = NULL;
1184 if (GNUNET_NO != shutdown)
1187 connection_maintain (c, fwd);
1188 GCC_check_connections ();
1189 /* Next execution will be scheduled by message_sent or _maintain*/
1194 * Keep the connection alive in the FWD direction.
1196 * @param cls Closure (connection to keepalive).
1197 * @param tc TaskContext.
1200 connection_fwd_keepalive (void *cls,
1201 const struct GNUNET_SCHEDULER_TaskContext *tc)
1203 GCC_check_connections ();
1204 connection_keepalive ((struct CadetConnection *) cls,
1206 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1207 GCC_check_connections ();
1212 * Keep the connection alive in the BCK direction.
1214 * @param cls Closure (connection to keepalive).
1215 * @param tc TaskContext.
1218 connection_bck_keepalive (void *cls,
1219 const struct GNUNET_SCHEDULER_TaskContext *tc)
1221 GCC_check_connections ();
1222 connection_keepalive ((struct CadetConnection *) cls,
1224 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1225 GCC_check_connections ();
1230 * Schedule next keepalive task, taking in consideration
1231 * the connection state and number of retries.
1233 * If the peer is not the origin, do nothing.
1235 * @param c Connection for which to schedule the next keepalive.
1236 * @param fwd Direction for the next keepalive.
1239 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1241 struct GNUNET_TIME_Relative delay;
1242 struct GNUNET_SCHEDULER_Task * *task_id;
1243 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1245 GCC_check_connections ();
1246 if (GNUNET_NO == GCC_is_origin (c, fwd))
1249 /* Calculate delay to use, depending on the state of the connection */
1250 if (CADET_CONNECTION_READY == c->state)
1252 delay = refresh_connection_time;
1256 if (1 > c->create_retry)
1257 c->create_retry = 1;
1258 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1260 if (c->create_retry < 64)
1261 c->create_retry *= 2;
1264 /* Select direction-dependent parameters */
1265 if (GNUNET_YES == fwd)
1267 task_id = &c->fwd_maintenance_task;
1268 keepalive_task = &connection_fwd_keepalive;
1272 task_id = &c->bck_maintenance_task;
1273 keepalive_task = &connection_bck_keepalive;
1276 /* Check that no one scheduled it before us */
1277 if (NULL != *task_id)
1279 /* No need for a _break. It can happen for instance when sending a SYNACK
1280 * for a duplicate SYN: the first SYNACK scheduled the task. */
1281 GNUNET_SCHEDULER_cancel (*task_id);
1284 /* Schedule the task */
1285 *task_id = GNUNET_SCHEDULER_add_delayed (delay,
1288 LOG (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1289 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1290 GCC_check_connections ();
1295 * @brief Re-initiate traffic on this connection if necessary.
1297 * Check if there is traffic queued towards this peer
1298 * and the core transmit handle is NULL (traffic was stalled).
1299 * If so, call core tmt rdy.
1301 * @param c Connection on which initiate traffic.
1302 * @param fwd Is this about fwd traffic?
1305 connection_unlock_queue (struct CadetConnection *c, int fwd)
1307 struct CadetPeer *peer;
1309 GCC_check_connections ();
1310 LOG (GNUNET_ERROR_TYPE_DEBUG,
1311 "connection_unlock_queue %s on %s\n",
1312 GC_f2s (fwd), GCC_2s (c));
1314 if (GCC_is_terminal (c, fwd))
1316 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal, can unlock!\n");
1320 peer = get_hop (c, fwd);
1321 GCP_queue_unlock (peer, c);
1322 GCC_check_connections ();
1327 * Cancel all transmissions that belong to a certain connection.
1329 * If the connection is scheduled for destruction and no more messages are left,
1330 * the connection will be destroyed by the continuation call.
1332 * @param c Connection which to cancel. Might be destroyed during this call.
1333 * @param fwd Cancel fwd traffic?
1336 connection_cancel_queues (struct CadetConnection *c,
1339 struct CadetFlowControl *fc;
1340 struct CadetPeer *peer;
1342 GCC_check_connections ();
1343 LOG (GNUNET_ERROR_TYPE_DEBUG,
1344 "Cancel %s queues for connection %s\n",
1345 GC_f2s (fwd), GCC_2s (c));
1352 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1353 if (NULL != fc->poll_task)
1355 GNUNET_SCHEDULER_cancel (fc->poll_task);
1356 fc->poll_task = NULL;
1357 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cancel POLL in ccq for fc %p\n", fc);
1359 peer = get_hop (c, fwd);
1360 GCP_queue_cancel (peer, c);
1361 GCC_check_connections ();
1366 * Function called if a connection has been stalled for a while,
1367 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1369 * @param cls Closure (poll ctx).
1370 * @param tc TaskContext.
1373 connection_poll (void *cls,
1374 const struct GNUNET_SCHEDULER_TaskContext *tc);
1378 * Callback called when a queued POLL message is sent.
1380 * @param cls Closure (FC).
1381 * @param c Connection this message was on.
1382 * @param q Queue handler this call invalidates.
1383 * @param type Type of message sent.
1384 * @param fwd Was this a FWD going message?
1385 * @param size Size of the message.
1388 poll_sent (void *cls,
1389 struct CadetConnection *c,
1390 struct CadetConnectionQueue *q,
1391 uint16_t type, int fwd, size_t size)
1393 struct CadetFlowControl *fc = cls;
1395 if (2 == c->destroy)
1397 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1400 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1402 fc->poll_msg = NULL;
1403 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1404 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1407 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1412 * Function called if a connection has been stalled for a while,
1413 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1415 * @param cls Closure (poll ctx).
1416 * @param tc TaskContext.
1419 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1421 struct CadetFlowControl *fc = cls;
1422 struct GNUNET_CADET_Poll msg;
1423 struct CadetConnection *c;
1426 fc->poll_task = NULL;
1427 GCC_check_connections ();
1428 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1434 fwd = fc == &c->fwd_fc;
1435 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1436 GCC_2s (c), GC_f2s (fwd));
1438 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1439 msg.header.size = htons (sizeof (msg));
1440 msg.pid = htonl (fc->last_pid_sent);
1441 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1443 GCC_send_prebuilt_message (&msg.header, 0, fc->last_pid_sent, c,
1444 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1445 GNUNET_assert (NULL != fc->poll_msg);
1446 GCC_check_connections ();
1451 * Resend all queued messages for a connection on other connections of the
1452 * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
1454 * @param c Connection whose messages to resend.
1455 * @param fwd Resend fwd messages?
1458 resend_messages_and_destroy (struct CadetConnection *c, int fwd)
1460 struct GNUNET_MessageHeader *out_msg;
1461 struct CadetTunnel *t = c->t;
1462 struct CadetPeer *neighbor;
1463 unsigned int pending;
1466 GCC_check_connections ();
1467 c->state = CADET_CONNECTION_DESTROYED;
1468 c->destroy = GNUNET_YES;
1470 destroyed = GNUNET_NO;
1471 neighbor = get_hop (c, fwd);
1472 pending = c->pending_messages;
1474 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1477 GCT_resend_message (out_msg, t);
1478 GNUNET_free (out_msg);
1481 /* All pending messages should have been popped,
1482 * and the connection destroyed by the continuation.
1484 if (GNUNET_YES != destroyed)
1489 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1490 if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1494 GCC_check_connections ();
1499 * Generic connection timeout implementation.
1501 * Timeout function due to lack of keepalive/traffic from an endpoint.
1502 * Destroys connection if called.
1504 * @param c Connection to destroy.
1505 * @param fwd Was the timeout from the origin? (FWD timeout)
1508 connection_timeout (struct CadetConnection *c, int fwd)
1510 struct CadetFlowControl *reverse_fc;
1512 GCC_check_connections ();
1513 reverse_fc = fwd ? &c->bck_fc : &c->fwd_fc;
1515 LOG (GNUNET_ERROR_TYPE_INFO,
1516 "Connection %s %s timed out. Destroying.\n",
1519 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1521 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1527 /* If dest, salvage queued traffic. */
1528 if (GCC_is_origin (c, !fwd))
1530 const struct GNUNET_PeerIdentity *next_hop;
1532 next_hop = GCP_get_id (fwd ? get_prev_hop (c) : get_next_hop (c));
1533 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1534 if (0 < reverse_fc->queue_n)
1535 resend_messages_and_destroy (c, !fwd);
1536 GCC_check_connections ();
1541 GCC_check_connections ();
1546 * Timeout function due to lack of keepalive/traffic from the owner.
1547 * Destroys connection if called.
1549 * @param cls Closure (connection to destroy).
1550 * @param tc TaskContext.
1553 connection_fwd_timeout (void *cls,
1554 const struct GNUNET_SCHEDULER_TaskContext *tc)
1556 struct CadetConnection *c = cls;
1558 c->fwd_maintenance_task = NULL;
1559 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1561 GCC_check_connections ();
1562 connection_timeout (c, GNUNET_YES);
1563 GCC_check_connections ();
1568 * Timeout function due to lack of keepalive/traffic from the destination.
1569 * Destroys connection if called.
1571 * @param cls Closure (connection to destroy).
1572 * @param tc TaskContext
1575 connection_bck_timeout (void *cls,
1576 const struct GNUNET_SCHEDULER_TaskContext *tc)
1578 struct CadetConnection *c = cls;
1580 c->bck_maintenance_task = NULL;
1581 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1583 GCC_check_connections ();
1584 connection_timeout (c, GNUNET_NO);
1585 GCC_check_connections ();
1590 * Resets the connection timeout task, some other message has done the
1592 * - For the first peer on the direction this means to send
1593 * a keepalive or a path confirmation message (either create or ACK).
1594 * - For all other peers, this means to destroy the connection,
1595 * due to lack of activity.
1596 * Starts the timeout if no timeout was running (connection just created).
1598 * @param c Connection whose timeout to reset.
1599 * @param fwd Is this forward?
1601 * TODO use heap to improve efficiency of scheduler.
1604 connection_reset_timeout (struct CadetConnection *c, int fwd)
1606 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1607 if (GCC_is_origin (c, fwd)) /* Startpoint */
1609 schedule_next_keepalive (c, fwd);
1611 else /* Relay, endpoint. */
1613 struct GNUNET_TIME_Relative delay;
1614 struct GNUNET_SCHEDULER_Task * *ti;
1615 GNUNET_SCHEDULER_TaskCallback f;
1617 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1620 GNUNET_SCHEDULER_cancel (*ti);
1621 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1622 LOG (GNUNET_ERROR_TYPE_DEBUG, " timing out in %s\n",
1623 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1624 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1625 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1631 * Iterator to compare each connection's path with the path of a new connection.
1633 * If the connection conincides, the c member of path is set to the connection
1634 * and the destroy flag of the connection is set.
1636 * @param cls Closure (new path).
1637 * @param c Connection in the tunnel to check.
1640 check_path (void *cls, struct CadetConnection *c)
1642 struct CadetConnection *new_conn = cls;
1643 struct CadetPeerPath *path = new_conn->path;
1645 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1646 GCC_2s (c), c, c->path->length);
1649 && c->destroy == GNUNET_NO
1650 && c->state != CADET_CONNECTION_BROKEN
1651 && c->state != CADET_CONNECTION_DESTROYED
1652 && path_equivalent (path, c->path))
1654 new_conn->destroy = GNUNET_YES;
1655 new_conn->path->c = c;
1656 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1662 * Finds out if this path is already being used by an existing connection.
1664 * Checks the tunnel towards the destination to see if it contains
1665 * any connection with the same path.
1667 * If the existing connection is ready, it is kept.
1668 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1669 * the peer will eventually reject our attempt).
1671 * @param path Path to check.
1672 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1673 * #GNUNET_NO otherwise.
1676 does_connection_exist (struct CadetConnection *conn)
1678 struct CadetPeer *p;
1679 struct CadetTunnel *t;
1680 struct CadetConnection *c;
1682 p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
1685 t = GCP_get_tunnel (p);
1689 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1691 GCT_iterate_connections (t, &check_path, conn);
1693 if (GNUNET_YES == conn->destroy)
1696 conn->destroy = GNUNET_NO;
1697 conn->path->c = conn;
1698 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1699 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1700 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1701 if (CADET_CONNECTION_READY == c->state)
1703 /* The other peer confirmed a live connection with this path,
1704 * why is it trying to duplicate it. */
1705 GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
1708 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not valid, connection unique\n");
1713 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1720 * @brief Check if the tunnel this connection belongs to has any other
1721 * connection with the same path, and destroy one if so.
1723 * @param cls Closure (connection to check).
1724 * @param tc Task context.
1727 check_duplicates (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1729 struct CadetConnection *c = cls;
1731 c->check_duplicates_task = NULL;
1732 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1735 if (GNUNET_YES == does_connection_exist (c))
1737 GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1738 send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
1746 * Wait for enough time to let any dead connections time out and check for
1747 * any remaining duplicates.
1749 * @param c Connection that is a potential duplicate.
1752 schedule_check_duplicates (struct CadetConnection *c)
1754 struct GNUNET_TIME_Relative delay;
1756 if (NULL != c->check_duplicates_task)
1759 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 5);
1760 c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
1768 * Add the connection to the list of both neighbors.
1770 * @param c Connection.
1772 * @return #GNUNET_OK if everything went fine
1773 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1776 register_neighbors (struct CadetConnection *c)
1778 c->next_peer = get_next_hop (c);
1779 c->prev_peer = get_prev_hop (c);
1780 GNUNET_assert (c->next_peer != c->prev_peer);
1781 LOG (GNUNET_ERROR_TYPE_DEBUG,
1782 "register neighbors for connection %s\n",
1784 path_debug (c->path);
1785 LOG (GNUNET_ERROR_TYPE_DEBUG,
1786 "own pos %u\n", c->own_pos);
1787 LOG (GNUNET_ERROR_TYPE_DEBUG,
1788 "putting connection %s to next peer %p\n",
1791 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1793 GCP_2s (c->next_peer));
1794 LOG (GNUNET_ERROR_TYPE_DEBUG,
1795 "putting connection %s to prev peer %p\n",
1798 LOG (GNUNET_ERROR_TYPE_DEBUG,
1799 "prev peer %p %s\n",
1801 GCP_2s (c->prev_peer));
1803 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1804 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1806 if (GCC_is_origin (c, GNUNET_YES))
1807 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1808 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1810 LOG (GNUNET_ERROR_TYPE_DEBUG,
1811 " register neighbors failed\n");
1812 LOG (GNUNET_ERROR_TYPE_DEBUG,
1813 " prev: %s, neighbor?: %d\n",
1814 GCP_2s (c->prev_peer),
1815 GCP_is_neighbor (c->prev_peer));
1816 LOG (GNUNET_ERROR_TYPE_DEBUG,
1817 " next: %s, neighbor?: %d\n",
1818 GCP_2s (c->next_peer),
1819 GCP_is_neighbor (c->next_peer));
1820 return GNUNET_SYSERR;
1822 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1823 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1830 * Remove the connection from the list of both neighbors.
1832 * @param c Connection.
1835 unregister_neighbors (struct CadetConnection *c)
1837 /* Either already unregistered or never got registered, it's ok either way. */
1838 if (NULL == c->path)
1840 if (NULL != c->next_peer)
1842 GCP_remove_connection (c->next_peer, c);
1843 c->next_peer = NULL;
1845 if (NULL != c->prev_peer)
1847 GCP_remove_connection (c->prev_peer, c);
1848 c->prev_peer = NULL;
1854 * Bind the connection to the peer and the tunnel to that peer.
1856 * If the peer has no tunnel, create one. Update tunnel and connection
1857 * data structres to reflect new status.
1859 * @param c Connection.
1863 add_to_peer (struct CadetConnection *c,
1864 struct CadetPeer *peer)
1866 GCP_add_tunnel (peer);
1867 c->t = GCP_get_tunnel (peer);
1868 GCT_add_connection (c->t, c);
1873 * Log receipt of message on stderr (INFO level).
1875 * @param message Message received.
1876 * @param peer Peer who sent the message.
1877 * @param hash Connection ID.
1880 log_message (const struct GNUNET_MessageHeader *message,
1881 const struct GNUNET_PeerIdentity *peer,
1882 const struct GNUNET_CADET_Hash *hash)
1886 size = ntohs (message->size);
1887 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1888 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1889 LOG (GNUNET_ERROR_TYPE_INFO, "<-- %s on connection %s from %s, %6u bytes\n",
1890 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (hash)),
1891 GNUNET_i2s (peer), (unsigned int) size);
1894 /******************************************************************************/
1895 /******************************** API ***********************************/
1896 /******************************************************************************/
1899 * Core handler for connection creation.
1901 * @param cls Closure (unused).
1902 * @param peer Sender (neighbor).
1903 * @param message Message.
1904 * @return #GNUNET_OK to keep the connection open,
1905 * #GNUNET_SYSERR to close it (signal serious error)
1908 GCC_handle_create (void *cls,
1909 const struct GNUNET_PeerIdentity *peer,
1910 const struct GNUNET_MessageHeader *message)
1912 struct GNUNET_CADET_ConnectionCreate *msg;
1913 struct GNUNET_PeerIdentity *id;
1914 struct GNUNET_CADET_Hash *cid;
1915 struct CadetPeerPath *path;
1916 struct CadetPeer *dest_peer;
1917 struct CadetPeer *orig_peer;
1918 struct CadetConnection *c;
1919 unsigned int own_pos;
1922 GCC_check_connections ();
1924 size = ntohs (message->size);
1925 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1927 GNUNET_break_op (0);
1931 /* Calculate hops */
1932 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1933 if (size % sizeof (struct GNUNET_PeerIdentity))
1935 GNUNET_break_op (0);
1938 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1940 GNUNET_break_op (0);
1943 size /= sizeof (struct GNUNET_PeerIdentity);
1946 GNUNET_break_op (0);
1949 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1951 /* Get parameters */
1952 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
1954 log_message (message, peer, cid);
1955 id = (struct GNUNET_PeerIdentity *) &msg[1];
1956 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1958 /* Create connection */
1959 c = connection_get (cid);
1962 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1963 size, myid, &own_pos);
1966 /* Path was malformed, probably our own ID was not in it. */
1967 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
1968 GNUNET_break_op (0);
1974 /* We received this request from a neighbor, we cannot be origin */
1975 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
1976 GNUNET_break_op (0);
1977 path_destroy (path);
1981 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1982 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1983 c = GCC_new (cid, NULL, path, own_pos);
1986 if (path->length - 1 == own_pos)
1988 /* If we are destination, why did the creation fail? */
1990 path_destroy (path);
1991 GCC_check_connections ();
1994 send_broken_unknown (cid, &my_full_id,
1995 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1997 path_destroy (path);
1998 GCC_check_connections ();
2001 GCP_add_path_to_all (path, GNUNET_NO);
2002 connection_reset_timeout (c, GNUNET_YES);
2006 path = path_duplicate (c->path);
2008 if (CADET_CONNECTION_NEW == c->state)
2009 connection_change_state (c, CADET_CONNECTION_SENT);
2011 /* Remember peers */
2012 dest_peer = GCP_get (&id[size - 1], GNUNET_YES);
2013 orig_peer = GCP_get (&id[0], GNUNET_YES);
2015 /* Is it a connection to us? */
2016 if (c->own_pos == path->length - 1)
2018 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2019 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
2021 add_to_peer (c, orig_peer);
2022 if (GNUNET_YES == does_connection_exist (c))
2024 /* Peer created a connection equal to one we think exists
2026 * Solution: Keep both and postpone disambiguation. In the meantime
2027 * the connection will time out or peer will inform us it is broken.
2030 * - Use explicit duplicate.
2031 * - Accept new conn and destroy the old. (interruption in higher level)
2032 * - Keep the one with higher ID / created by peer with higher ID. */
2033 schedule_check_duplicates (c);
2036 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
2037 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
2039 send_connection_ack (c, GNUNET_NO);
2040 if (CADET_CONNECTION_SENT == c->state)
2041 connection_change_state (c, CADET_CONNECTION_ACK);
2045 /* It's for somebody else! Retransmit. */
2046 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2047 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2048 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2049 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
2050 GNUNET_YES, GNUNET_YES,
2053 path_destroy (path);
2054 GCC_check_connections ();
2060 * Core handler for path confirmations.
2062 * @param cls closure
2063 * @param message message
2064 * @param peer peer identity this notification is about
2065 * @return #GNUNET_OK to keep the connection open,
2066 * #GNUNET_SYSERR to close it (signal serious error)
2069 GCC_handle_confirm (void *cls,
2070 const struct GNUNET_PeerIdentity *peer,
2071 const struct GNUNET_MessageHeader *message)
2073 struct GNUNET_CADET_ConnectionACK *msg;
2074 struct CadetConnection *c;
2075 struct CadetPeerPath *p;
2076 struct CadetPeer *pi;
2077 enum CadetConnectionState oldstate;
2080 GCC_check_connections ();
2081 msg = (struct GNUNET_CADET_ConnectionACK *) message;
2082 log_message (message, peer, &msg->cid);
2083 c = connection_get (&msg->cid);
2086 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2088 LOG (GNUNET_ERROR_TYPE_DEBUG,
2089 " don't know the connection!\n");
2090 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2091 GCC_check_connections ();
2095 oldstate = c->state;
2096 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
2097 pi = GCP_get (peer, GNUNET_YES);
2098 if (get_next_hop (c) == pi)
2100 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2102 if (CADET_CONNECTION_SENT == oldstate)
2103 connection_change_state (c, CADET_CONNECTION_ACK);
2105 else if (get_prev_hop (c) == pi)
2107 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2109 connection_change_state (c, CADET_CONNECTION_READY);
2113 GNUNET_break_op (0);
2117 connection_reset_timeout (c, fwd);
2119 /* Add path to peers? */
2123 GCP_add_path_to_all (p, GNUNET_YES);
2130 /* Message for us as creator? */
2131 if (GCC_is_origin (c, GNUNET_YES))
2133 if (GNUNET_NO != fwd)
2135 GNUNET_break_op (0);
2138 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2140 /* If just created, cancel the short timeout and start a long one */
2141 if (CADET_CONNECTION_SENT == oldstate)
2142 connection_reset_timeout (c, GNUNET_YES);
2144 /* Change connection state */
2145 connection_change_state (c, CADET_CONNECTION_READY);
2146 send_connection_ack (c, GNUNET_YES);
2148 /* Change tunnel state, trigger KX */
2149 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2150 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2151 GCC_check_connections ();
2155 /* Message for us as destination? */
2156 if (GCC_is_terminal (c, GNUNET_YES))
2158 if (GNUNET_YES != fwd)
2160 GNUNET_break_op (0);
2163 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2165 /* If just created, cancel the short timeout and start a long one */
2166 if (CADET_CONNECTION_ACK == oldstate)
2167 connection_reset_timeout (c, GNUNET_NO);
2169 /* Change tunnel state */
2170 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2171 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2172 GCC_check_connections ();
2176 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2177 GNUNET_assert (NULL ==
2178 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2179 GNUNET_YES, NULL, NULL));
2180 GCC_check_connections ();
2186 * Core handler for notifications of broken connections.
2188 * @param cls Closure (unused).
2189 * @param id Peer identity of sending neighbor.
2190 * @param message Message.
2191 * @return #GNUNET_OK to keep the connection open,
2192 * #GNUNET_SYSERR to close it (signal serious error)
2195 GCC_handle_broken (void* cls,
2196 const struct GNUNET_PeerIdentity* id,
2197 const struct GNUNET_MessageHeader* message)
2199 struct GNUNET_CADET_ConnectionBroken *msg;
2200 struct CadetConnection *c;
2201 struct CadetTunnel *t;
2205 GCC_check_connections ();
2206 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
2207 log_message (message, id, &msg->cid);
2208 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2209 GNUNET_i2s (&msg->peer1));
2210 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2211 GNUNET_i2s (&msg->peer2));
2212 c = connection_get (&msg->cid);
2215 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2216 GCC_check_connections ();
2222 fwd = is_fwd (c, id);
2223 c->destroy = GNUNET_YES;
2224 if (GCC_is_terminal (c, fwd))
2226 struct CadetPeer *endpoint;
2230 /* A terminal connection should not have 't' set to NULL. */
2232 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2235 endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
2236 if (2 < c->path->length)
2237 path_invalidate (c->path);
2238 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2240 c->state = CADET_CONNECTION_BROKEN;
2241 GCT_remove_connection (t, c);
2244 pending = c->pending_messages;
2246 resend_messages_and_destroy (c, !fwd);
2252 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2253 GNUNET_YES, NULL, NULL));
2254 connection_cancel_queues (c, !fwd);
2256 GCC_check_connections ();
2262 * Core handler for tunnel destruction
2264 * @param cls Closure (unused).
2265 * @param peer Peer identity of sending neighbor.
2266 * @param message Message.
2267 * @return #GNUNET_OK to keep the connection open,
2268 * #GNUNET_SYSERR to close it (signal serious error)
2271 GCC_handle_destroy (void *cls,
2272 const struct GNUNET_PeerIdentity *peer,
2273 const struct GNUNET_MessageHeader *message)
2275 const struct GNUNET_CADET_ConnectionDestroy *msg;
2276 struct CadetConnection *c;
2279 GCC_check_connections ();
2280 msg = (const struct GNUNET_CADET_ConnectionDestroy *) message;
2281 log_message (message, peer, &msg->cid);
2282 c = connection_get (&msg->cid);
2285 /* Probably already got the message from another path,
2286 * destroyed the tunnel and retransmitted to children.
2289 GNUNET_STATISTICS_update (stats,
2290 "# control on unknown connection",
2292 LOG (GNUNET_ERROR_TYPE_DEBUG,
2293 " connection unknown: already destroyed?\n");
2294 GCC_check_connections ();
2297 fwd = is_fwd (c, peer);
2298 if (GNUNET_SYSERR == fwd)
2300 GNUNET_break_op (0); /* FIXME */
2303 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2305 GNUNET_assert (NULL ==
2306 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2307 GNUNET_YES, NULL, NULL));
2309 else if (0 == c->pending_messages)
2311 LOG (GNUNET_ERROR_TYPE_DEBUG,
2312 " directly destroying connection!\n");
2314 GCC_check_connections ();
2317 c->destroy = GNUNET_YES;
2318 c->state = CADET_CONNECTION_DESTROYED;
2321 GCT_remove_connection (c->t, c);
2324 GCC_check_connections ();
2330 * Check the message against internal state and test if it goes FWD or BCK.
2332 * Updates the PID, state and timeout values for the connection.
2334 * @param message Message to check. It must belong to an existing connection.
2335 * @param minimum_size The message cannot be smaller than this value.
2336 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2337 * @param c Connection this message should belong. If NULL, check fails.
2338 * @param neighbor Neighbor that sent the message.
2341 check_message (const struct GNUNET_MessageHeader *message,
2342 size_t minimum_size,
2343 const struct GNUNET_CADET_Hash* cid,
2344 struct CadetConnection *c,
2345 const struct GNUNET_PeerIdentity *neighbor,
2348 GNUNET_PEER_Id neighbor_id;
2349 struct CadetFlowControl *fc;
2350 struct CadetPeer *hop;
2355 if (ntohs (message->size) < minimum_size)
2357 GNUNET_break_op (0);
2358 LOG (GNUNET_ERROR_TYPE_WARNING, "Size %u < %u\n",
2359 ntohs (message->size), minimum_size);
2360 return GNUNET_SYSERR;
2363 /* Check connection */
2366 GNUNET_STATISTICS_update (stats,
2367 "# unknown connection",
2369 LOG (GNUNET_ERROR_TYPE_DEBUG,
2370 "%s on unknown connection %s\n",
2371 GC_m2s (ntohs (message->type)),
2372 GNUNET_h2s (GC_h2hc (cid)));
2373 send_broken_unknown (cid,
2377 return GNUNET_SYSERR;
2380 /* Check if origin is as expected */
2381 neighbor_id = GNUNET_PEER_search (neighbor);
2382 hop = get_prev_hop (c);
2383 if (neighbor_id == GCP_get_short_id (hop))
2389 hop = get_next_hop (c);
2390 GNUNET_break (hop == c->next_peer);
2391 if (neighbor_id == GCP_get_short_id (hop))
2397 /* Unexpected peer sending traffic on a connection. */
2398 GNUNET_break_op (0);
2399 return GNUNET_SYSERR;
2403 /* Check PID for payload messages */
2404 type = ntohs (message->type);
2405 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
2406 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
2408 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2409 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2410 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2411 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2413 GNUNET_break_op (0);
2414 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2415 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2416 pid, fc->last_pid_recv, fc->last_ack_sent);
2417 return GNUNET_SYSERR;
2419 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2423 delta = pid - fc->last_pid_recv;
2424 fc->last_pid_recv = pid;
2425 fc->recv_bitmap <<= delta;
2426 fc->recv_bitmap |= 1;
2430 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2431 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2433 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2434 pid, fc->last_pid_recv - 31);
2435 return GNUNET_SYSERR;
2437 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2441 /* Count as connection confirmation. */
2442 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2444 connection_change_state (c, CADET_CONNECTION_READY);
2447 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2448 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2451 connection_reset_timeout (c, fwd);
2458 * Generic handler for cadet network encrypted traffic.
2460 * @param peer Peer identity this notification is about.
2461 * @param msg Encrypted message.
2462 * @return #GNUNET_OK to keep the connection open,
2463 * #GNUNET_SYSERR to close it (signal serious error)
2466 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
2467 const struct GNUNET_MessageHeader *message)
2469 const struct GNUNET_CADET_Encrypted *otr_msg;
2470 const struct GNUNET_CADET_AX *ax_msg;
2471 const struct GNUNET_CADET_Hash* cid;
2472 struct CadetConnection *c;
2473 size_t minumum_size;
2479 GCC_check_connections ();
2480 if (GNUNET_MESSAGE_TYPE_CADET_AX == ntohs (message->type))
2482 overhead = sizeof (struct GNUNET_CADET_AX);
2483 ax_msg = (const struct GNUNET_CADET_AX *) message;
2485 pid = ntohl (ax_msg->pid);
2490 overhead = sizeof (struct GNUNET_CADET_Encrypted);
2491 otr_msg = (const struct GNUNET_CADET_Encrypted *) message;
2492 cid = &otr_msg->cid;
2493 pid = ntohl (otr_msg->pid);
2496 log_message (message, peer, cid);
2498 minumum_size = sizeof (struct GNUNET_MessageHeader) + overhead;
2499 c = connection_get (cid);
2500 fwd = check_message (message,
2507 /* If something went wrong, discard message. */
2508 if (GNUNET_SYSERR == fwd)
2510 GCC_check_connections ();
2514 /* Is this message for us? */
2515 if (GCC_is_terminal (c, fwd))
2517 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2521 GNUNET_break (GNUNET_NO != c->destroy);
2524 GCT_handle_encrypted (c->t, message);
2525 GCC_send_ack (c, fwd, GNUNET_NO);
2526 GCC_check_connections ();
2530 /* Message not for us: forward to next hop */
2531 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2532 if (NULL != otr_msg) /* only otr has ttl */
2534 ttl = ntohl (otr_msg->ttl);
2535 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2538 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2539 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2540 GCC_send_ack (c, fwd, GNUNET_NO);
2541 GCC_check_connections ();
2546 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2547 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2548 GNUNET_NO, NULL, NULL));
2549 GCC_check_connections ();
2555 * Generic handler for cadet network encrypted traffic.
2557 * @param peer Peer identity this notification is about.
2558 * @param msg Encrypted message.
2559 * @return #GNUNET_OK to keep the connection open,
2560 * #GNUNET_SYSERR to close it (signal serious error)
2563 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2564 const struct GNUNET_CADET_KX *msg)
2566 const struct GNUNET_CADET_Hash* cid;
2567 struct CadetConnection *c;
2568 size_t expected_size;
2571 GCC_check_connections ();
2573 log_message (&msg->header, peer, cid);
2575 expected_size = sizeof (struct GNUNET_CADET_KX)
2576 + sizeof (struct GNUNET_MessageHeader);
2577 c = connection_get (cid);
2578 fwd = check_message (&msg->header,
2585 /* If something went wrong, discard message. */
2586 if (GNUNET_SYSERR == fwd)
2589 /* Is this message for us? */
2590 if (GCC_is_terminal (c, fwd))
2592 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2593 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2599 GCT_handle_kx (c->t, &msg[1].header);
2600 GCC_check_connections ();
2604 /* Message not for us: forward to next hop */
2605 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2606 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2607 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2608 GNUNET_NO, NULL, NULL));
2609 GCC_check_connections ();
2615 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2617 * @param cls Closure (unused).
2618 * @param message Message received.
2619 * @param peer Peer who sent the message.
2620 * @return #GNUNET_OK to keep the connection open,
2621 * #GNUNET_SYSERR to close it (signal serious error)
2624 GCC_handle_kx (void *cls,
2625 const struct GNUNET_PeerIdentity *peer,
2626 const struct GNUNET_MessageHeader *message)
2628 GCC_check_connections ();
2629 return handle_cadet_kx (peer, (struct GNUNET_CADET_KX *) message);
2634 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2636 * @param cls Closure (unused).
2637 * @param message Message received.
2638 * @param peer Peer who sent the message.
2639 * @return #GNUNET_OK to keep the connection open,
2640 * #GNUNET_SYSERR to close it (signal serious error)
2643 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2644 const struct GNUNET_MessageHeader *message)
2646 GCC_check_connections ();
2647 return handle_cadet_encrypted (peer, message);
2652 * Core handler for cadet network traffic point-to-point acks.
2654 * @param cls closure
2655 * @param message message
2656 * @param peer peer identity this notification is about
2657 * @return #GNUNET_OK to keep the connection open,
2658 * #GNUNET_SYSERR to close it (signal serious error)
2661 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2662 const struct GNUNET_MessageHeader *message)
2664 struct GNUNET_CADET_ACK *msg;
2665 struct CadetConnection *c;
2666 struct CadetFlowControl *fc;
2671 GCC_check_connections ();
2672 msg = (struct GNUNET_CADET_ACK *) message;
2673 log_message (message, peer, &msg->cid);
2674 c = connection_get (&msg->cid);
2677 GNUNET_STATISTICS_update (stats,
2678 "# ack on unknown connection",
2681 send_broken_unknown (&msg->cid,
2685 GCC_check_connections ();
2689 /* Is this a forward or backward ACK? */
2690 id = GNUNET_PEER_search (peer);
2691 if (GCP_get_short_id (get_next_hop (c)) == id)
2696 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2703 GNUNET_break_op (0);
2707 ack = ntohl (msg->ack);
2708 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2709 GC_f2s (fwd), ack, fc->last_ack_recv);
2710 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2711 fc->last_ack_recv = ack;
2713 /* Cancel polling if the ACK is big enough. */
2714 if (NULL != fc->poll_task &&
2715 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2717 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2718 GNUNET_SCHEDULER_cancel (fc->poll_task);
2719 fc->poll_task = NULL;
2720 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2723 connection_unlock_queue (c, fwd);
2724 GCC_check_connections ();
2730 * Core handler for cadet network traffic point-to-point ack polls.
2732 * @param cls closure
2733 * @param message message
2734 * @param peer peer identity this notification is about
2735 * @return #GNUNET_OK to keep the connection open,
2736 * #GNUNET_SYSERR to close it (signal serious error)
2739 GCC_handle_poll (void *cls,
2740 const struct GNUNET_PeerIdentity *peer,
2741 const struct GNUNET_MessageHeader *message)
2743 struct GNUNET_CADET_Poll *msg;
2744 struct CadetConnection *c;
2745 struct CadetFlowControl *fc;
2750 GCC_check_connections ();
2751 msg = (struct GNUNET_CADET_Poll *) message;
2752 log_message (message, peer, &msg->cid);
2753 c = connection_get (&msg->cid);
2756 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2758 LOG (GNUNET_ERROR_TYPE_DEBUG,
2759 "POLL message on unknown connection %s!\n",
2760 GNUNET_h2s (GC_h2hc (&msg->cid)));
2761 send_broken_unknown (&msg->cid,
2765 GCC_check_connections ();
2769 /* Is this a forward or backward ACK?
2770 * Note: a poll should never be needed in a loopback case,
2771 * since there is no possiblility of packet loss there, so
2772 * this way of discerining FWD/BCK should not be a problem.
2774 id = GNUNET_PEER_search (peer);
2775 if (GCP_get_short_id (get_next_hop (c)) == id)
2777 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2780 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2782 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2787 GNUNET_break_op (0);
2791 pid = ntohl (msg->pid);
2792 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2793 fc->last_pid_recv = pid;
2794 fwd = fc == &c->bck_fc;
2795 GCC_send_ack (c, fwd, GNUNET_YES);
2796 GCC_check_connections ();
2803 * Send an ACK on the appropriate connection/channel, depending on
2804 * the direction and the position of the peer.
2806 * @param c Which connection to send the hop-by-hop ACK.
2807 * @param fwd Is this a fwd ACK? (will go dest->root).
2808 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2811 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2813 unsigned int buffer;
2815 GCC_check_connections ();
2816 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
2817 GC_f2s (fwd), GCC_2s (c));
2825 if (GNUNET_NO != c->destroy)
2827 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2828 GCC_check_connections ();
2832 /* Get available buffer space */
2833 if (GCC_is_terminal (c, fwd))
2835 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2836 buffer = GCT_get_channels_buffer (c->t);
2840 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2841 buffer = GCC_get_buffer (c, fwd);
2843 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2844 if (0 == buffer && GNUNET_NO == force)
2846 GCC_check_connections ();
2850 /* Send available buffer space */
2851 if (GCC_is_origin (c, fwd))
2853 GNUNET_assert (NULL != c->t);
2854 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2855 GCT_unchoke_channels (c->t);
2859 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2860 send_ack (c, buffer, fwd, force);
2862 GCC_check_connections ();
2867 * Initialize the connections subsystem
2869 * @param c Configuration handle.
2872 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2874 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2876 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2879 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2880 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2881 GNUNET_SCHEDULER_shutdown ();
2886 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2889 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2890 "CADET", "MAX_CONNECTIONS", "MISSING");
2891 GNUNET_SCHEDULER_shutdown ();
2896 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2897 &refresh_connection_time))
2899 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2900 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2901 GNUNET_SCHEDULER_shutdown ();
2904 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2905 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2910 * Destroy each connection on shutdown.
2912 * @param cls Closure (unused).
2913 * @param key Current key code (CID, unused).
2914 * @param value Value in the hash map (`struct CadetConnection`)
2916 * @return #GNUNET_YES, because we should continue to iterate
2919 shutdown_iterator (void *cls,
2920 const struct GNUNET_HashCode *key,
2923 struct CadetConnection *c = value;
2925 c->state = CADET_CONNECTION_DESTROYED;
2932 * Shut down the connections subsystem.
2937 GCC_check_connections ();
2938 GNUNET_CONTAINER_multihashmap_iterate (connections,
2941 GNUNET_CONTAINER_multihashmap_destroy (connections);
2947 * Create a connection.
2949 * @param cid Connection ID (either created locally or imposed remotely).
2950 * @param t Tunnel this connection belongs to (or NULL);
2951 * @param path Path this connection has to use (copy is made).
2952 * @param own_pos Own position in the @c path path.
2954 * @return Newly created connection, NULL in case of error (own id not in path).
2956 struct CadetConnection *
2957 GCC_new (const struct GNUNET_CADET_Hash *cid,
2958 struct CadetTunnel *t,
2959 struct CadetPeerPath *path,
2960 unsigned int own_pos)
2962 struct CadetConnection *c;
2963 struct CadetPeerPath *p;
2965 GCC_check_connections ();
2966 p = path_duplicate (path);
2967 c = GNUNET_new (struct CadetConnection);
2969 GNUNET_assert (GNUNET_OK ==
2970 GNUNET_CONTAINER_multihashmap_put (connections,
2972 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2973 fc_init (&c->fwd_fc);
2974 fc_init (&c->bck_fc);
2979 GNUNET_assert (own_pos <= p->length - 1);
2980 c->own_pos = own_pos;
2983 GNUNET_assert (NULL != p);
2984 if (GNUNET_OK != register_neighbors (c))
2988 /* We were the origin of this request, this means we have invalid
2989 * info about the paths to reach the destination. We must invalidate
2990 * the *original* path to avoid trying it again in the next minute.
2992 path_invalidate (path);
2995 path_destroy (c->path);
3000 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
3001 GCC_check_connections ();
3007 GCC_destroy (struct CadetConnection *c)
3009 GCC_check_connections ();
3016 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
3017 return; /* -> message_sent -> GCC_destroy. Don't loop. */
3020 LOG (GNUNET_ERROR_TYPE_DEBUG,
3021 "destroying connection %s\n",
3023 LOG (GNUNET_ERROR_TYPE_DEBUG,
3024 " fc's f: %p, b: %p\n",
3025 &c->fwd_fc, &c->bck_fc);
3026 LOG (GNUNET_ERROR_TYPE_DEBUG,
3027 " fc tasks f: %u, b: %u\n",
3028 c->fwd_fc.poll_task,
3029 c->bck_fc.poll_task);
3031 /* Cancel all traffic */
3032 if (NULL != c->path)
3034 connection_cancel_queues (c, GNUNET_YES);
3035 connection_cancel_queues (c, GNUNET_NO);
3037 unregister_neighbors (c);
3038 path_destroy (c->path);
3041 /* Cancel maintainance task (keepalive/timeout) */
3042 if (NULL != c->fwd_fc.poll_msg)
3044 GCC_cancel (c->fwd_fc.poll_msg);
3045 LOG (GNUNET_ERROR_TYPE_DEBUG,
3046 " POLL msg FWD canceled\n");
3048 if (NULL != c->bck_fc.poll_msg)
3050 GCC_cancel (c->bck_fc.poll_msg);
3051 LOG (GNUNET_ERROR_TYPE_DEBUG,
3052 " POLL msg BCK canceled\n");
3055 /* Delete from tunnel */
3057 GCT_remove_connection (c->t, c);
3059 if (NULL != c->check_duplicates_task)
3060 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
3061 if (NULL != c->fwd_maintenance_task)
3062 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
3063 if (NULL != c->bck_maintenance_task)
3064 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
3065 if (NULL != c->fwd_fc.poll_task)
3067 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
3068 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task FWD canceled\n");
3070 if (NULL != c->bck_fc.poll_task)
3072 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
3073 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task BCK canceled\n");
3075 if (GNUNET_NO == c->was_removed)
3077 GNUNET_break (GNUNET_YES ==
3078 GNUNET_CONTAINER_multihashmap_remove (connections,
3082 GNUNET_STATISTICS_update (stats,
3087 GCC_check_connections ();
3092 * Get the connection ID.
3094 * @param c Connection to get the ID from.
3096 * @return ID of the connection.
3098 const struct GNUNET_CADET_Hash *
3099 GCC_get_id (const struct CadetConnection *c)
3106 * Get the connection ID.
3108 * @param c Connection to get the ID from.
3110 * @return ID of the connection.
3112 const struct GNUNET_HashCode *
3113 GCC_get_h (const struct CadetConnection *c)
3115 return GC_h2hc (&c->id);
3120 * Get the connection path.
3122 * @param c Connection to get the path from.
3124 * @return path used by the connection.
3126 const struct CadetPeerPath *
3127 GCC_get_path (const struct CadetConnection *c)
3129 if (GNUNET_NO == c->destroy)
3136 * Get the connection state.
3138 * @param c Connection to get the state from.
3140 * @return state of the connection.
3142 enum CadetConnectionState
3143 GCC_get_state (const struct CadetConnection *c)
3149 * Get the connection tunnel.
3151 * @param c Connection to get the tunnel from.
3153 * @return tunnel of the connection.
3155 struct CadetTunnel *
3156 GCC_get_tunnel (const struct CadetConnection *c)
3163 * Get free buffer space in a connection.
3165 * @param c Connection.
3166 * @param fwd Is query about FWD traffic?
3168 * @return Free buffer space [0 - max_msgs_queue/max_connections]
3171 GCC_get_buffer (struct CadetConnection *c, int fwd)
3173 struct CadetFlowControl *fc;
3175 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3177 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
3178 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
3179 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
3181 return (fc->queue_max - fc->queue_n);
3186 * Get how many messages have we allowed to send to us from a direction.
3188 * @param c Connection.
3189 * @param fwd Are we asking about traffic from FWD (BCK messages)?
3191 * @return last_ack_sent - last_pid_recv
3194 GCC_get_allowed (struct CadetConnection *c, int fwd)
3196 struct CadetFlowControl *fc;
3198 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3199 if (CADET_CONNECTION_READY != c->state
3200 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
3204 return (fc->last_ack_sent - fc->last_pid_recv);
3209 * Get messages queued in a connection.
3211 * @param c Connection.
3212 * @param fwd Is query about FWD traffic?
3214 * @return Number of messages queued.
3217 GCC_get_qn (struct CadetConnection *c, int fwd)
3219 struct CadetFlowControl *fc;
3221 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3228 * Get next PID to use.
3230 * @param c Connection.
3231 * @param fwd Is query about FWD traffic?
3233 * @return Last PID used + 1.
3236 GCC_get_pid (struct CadetConnection *c, int fwd)
3238 struct CadetFlowControl *fc;
3240 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3242 return fc->last_pid_sent + 1;
3247 * Allow the connection to advertise a buffer of the given size.
3249 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3250 * allowing up to last_pid_recv + buffer.
3252 * @param c Connection.
3253 * @param buffer How many more messages the connection can accept.
3254 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3257 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3259 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3260 GCC_2s (c), buffer, GC_f2s (fwd));
3261 send_ack (c, buffer, fwd, GNUNET_NO);
3266 * Notify other peers on a connection of a broken link. Mark connections
3267 * to destroy after all traffic has been sent.
3269 * @param c Connection on which there has been a disconnection.
3270 * @param peer Peer that disconnected.
3273 GCC_notify_broken (struct CadetConnection *c,
3274 struct CadetPeer *peer)
3276 struct CadetPeer *hop;
3279 GCC_check_connections ();
3280 LOG (GNUNET_ERROR_TYPE_DEBUG,
3281 "Notify broken on %s due to %s disconnect\n",
3284 hop = get_prev_hop (c);
3287 /* Path was NULL, we should have deleted the connection. */
3291 fwd = (peer == hop);
3292 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3293 (GNUNET_YES == c->destroy) )
3295 /* Local shutdown, or other peer already down (hence 'c->destroy');
3296 so there is no one to notify about this, just clean up. */
3298 GCC_check_connections ();
3301 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3302 /* Connection will have at least one pending message
3303 * (the one we just scheduled), so delay destruction
3304 * and remove from map so we don't use accidentally. */
3305 c->destroy = GNUNET_YES;
3306 c->state = CADET_CONNECTION_DESTROYED;
3307 GNUNET_assert (GNUNET_NO == c->was_removed);
3308 c->was_removed = GNUNET_YES;
3309 GNUNET_break (GNUNET_YES ==
3310 GNUNET_CONTAINER_multihashmap_remove (connections,
3313 /* Cancel queue in the direction that just died. */
3314 connection_cancel_queues (c, ! fwd);
3315 unregister_neighbors (c);
3316 GCC_check_connections ();
3321 * Is this peer the first one on the connection?
3323 * @param c Connection.
3324 * @param fwd Is this about fwd traffic?
3326 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3329 GCC_is_origin (struct CadetConnection *c, int fwd)
3331 if (!fwd && c->path->length - 1 == c->own_pos )
3333 if (fwd && 0 == c->own_pos)
3340 * Is this peer the last one on the connection?
3342 * @param c Connection.
3343 * @param fwd Is this about fwd traffic?
3344 * Note that the ROOT is the terminal for BCK traffic!
3346 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3349 GCC_is_terminal (struct CadetConnection *c, int fwd)
3351 return GCC_is_origin (c, ! fwd);
3356 * See if we are allowed to send by the next hop in the given direction.
3358 * @param c Connection.
3359 * @param fwd Is this about fwd traffic?
3361 * @return #GNUNET_YES in case it's OK to send.
3364 GCC_is_sendable (struct CadetConnection *c, int fwd)
3366 struct CadetFlowControl *fc;
3368 LOG (GNUNET_ERROR_TYPE_DEBUG,
3369 " checking sendability of %s traffic on %s\n",
3370 GC_f2s (fwd), GCC_2s (c));
3376 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3377 LOG (GNUNET_ERROR_TYPE_DEBUG,
3378 " last ack recv: %u, last pid sent: %u\n",
3379 fc->last_ack_recv, fc->last_pid_sent);
3380 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3382 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3385 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3391 * Check if this connection is a direct one (never trim a direct connection).
3393 * @param c Connection.
3395 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3398 GCC_is_direct (struct CadetConnection *c)
3400 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3405 * Sends an already built message on a connection, properly registering
3406 * all used resources.
3408 * @param message Message to send. Function makes a copy of it.
3409 * If message is not hop-by-hop, decrements TTL of copy.
3410 * @param payload_type Type of payload, in case the message is encrypted.
3411 * @param c Connection on which this message is transmitted.
3412 * @param fwd Is this a fwd message?
3413 * @param force Force the connection to accept the message (buffer overfill).
3414 * @param cont Continuation called once message is sent. Can be NULL.
3415 * @param cont_cls Closure for @c cont.
3417 * @return Handle to cancel the message before it's sent.
3418 * NULL on error or if @c cont is NULL.
3419 * Invalid on @c cont call.
3421 struct CadetConnectionQueue *
3422 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3423 uint16_t payload_type, uint32_t payload_id,
3424 struct CadetConnection *c, int fwd, int force,
3425 GCC_sent cont, void *cont_cls)
3427 struct CadetFlowControl *fc;
3428 struct CadetConnectionQueue *q;
3434 GCC_check_connections ();
3435 size = ntohs (message->size);
3436 data = GNUNET_malloc (size);
3437 memcpy (data, message, size);
3438 type = ntohs (message->type);
3439 LOG (GNUNET_ERROR_TYPE_INFO, "--> %s (%s %4u) on connection %s (%u bytes)\n",
3440 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), size);
3442 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3443 droppable = GNUNET_NO == force;
3446 struct GNUNET_CADET_AX *axmsg;
3447 struct GNUNET_CADET_Encrypted *emsg;
3448 struct GNUNET_CADET_KX *kmsg;
3449 struct GNUNET_CADET_ACK *amsg;
3450 struct GNUNET_CADET_Poll *pmsg;
3451 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3452 struct GNUNET_CADET_ConnectionBroken *bmsg;
3455 case GNUNET_MESSAGE_TYPE_CADET_AX:
3456 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3457 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3459 emsg = (struct GNUNET_CADET_Encrypted *) data;
3460 ttl = ntohl (emsg->ttl);
3463 GNUNET_break_op (0);
3468 emsg->ttl = htonl (ttl - 1);
3472 axmsg = (struct GNUNET_CADET_AX *) data;
3475 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3476 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3477 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3478 if (GNUNET_YES == droppable)
3484 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3488 case GNUNET_MESSAGE_TYPE_CADET_KX:
3489 kmsg = (struct GNUNET_CADET_KX *) data;
3493 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3494 amsg = (struct GNUNET_CADET_ACK *) data;
3496 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3497 droppable = GNUNET_NO;
3500 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3501 pmsg = (struct GNUNET_CADET_Poll *) data;
3503 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
3504 droppable = GNUNET_NO;
3507 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3508 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
3512 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3513 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
3517 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3518 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3527 if (fc->queue_n > fc->queue_max && droppable)
3529 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3532 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3533 fc->queue_n, fc->queue_max);
3534 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
3535 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
3540 return NULL; /* Drop this message */
3543 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3544 GCC_2s (c), c->pending_messages);
3545 c->pending_messages++;
3547 q = GNUNET_new (struct CadetConnectionQueue);
3548 q->forced = !droppable;
3549 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3550 size, c, fwd, &conn_message_sent, q);
3553 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3556 GCC_check_connections ();
3560 q->cont_cls = cont_cls;
3561 GCC_check_connections ();
3562 return (NULL == cont) ? NULL : q;
3567 * Cancel a previously sent message while it's in the queue.
3569 * ONLY can be called before the continuation given to the send function
3570 * is called. Once the continuation is called, the message is no longer in the
3573 * @param q Handle to the queue.
3576 GCC_cancel (struct CadetConnectionQueue *q)
3578 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3580 /* queue destroy calls message_sent, which calls q->cont and frees q */
3581 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3582 GCC_check_connections ();
3587 * Sends a CREATE CONNECTION message for a path to a peer.
3588 * Changes the connection and tunnel states if necessary.
3590 * @param connection Connection to create.
3593 GCC_send_create (struct CadetConnection *connection)
3595 enum CadetTunnelCState state;
3598 GCC_check_connections ();
3599 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3600 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3602 LOG (GNUNET_ERROR_TYPE_INFO, "---> %s on connection %s (%u bytes)\n",
3603 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE),
3604 GCC_2s (connection), size);
3605 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3606 connection, connection->pending_messages);
3607 connection->pending_messages++;
3609 connection->maintenance_q =
3610 GCP_queue_add (get_next_hop (connection), NULL,
3611 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 0,
3612 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3614 state = GCT_get_cstate (connection->t);
3615 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3616 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3617 if (CADET_CONNECTION_NEW == connection->state)
3618 connection_change_state (connection, CADET_CONNECTION_SENT);
3619 GCC_check_connections ();
3624 * Send a message to all peers in this connection that the connection
3625 * is no longer valid.
3627 * If some peer should not receive the message, it should be zero'ed out
3628 * before calling this function.
3630 * @param c The connection whose peers to notify.
3633 GCC_send_destroy (struct CadetConnection *c)
3635 struct GNUNET_CADET_ConnectionDestroy msg;
3637 if (GNUNET_YES == c->destroy)
3639 GCC_check_connections ();
3640 msg.header.size = htons (sizeof (msg));
3641 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3643 LOG (GNUNET_ERROR_TYPE_DEBUG,
3644 " sending connection destroy for connection %s\n",
3647 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3648 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3649 GNUNET_YES, GNUNET_YES,
3651 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3652 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3653 GNUNET_NO, GNUNET_YES,
3655 c->destroy = GNUNET_YES;
3656 c->state = CADET_CONNECTION_DESTROYED;
3657 GCC_check_connections ();
3662 * @brief Start a polling timer for the connection.
3664 * When a neighbor does not accept more traffic on the connection it could be
3665 * caused by a simple congestion or by a lost ACK. Polling enables to check
3666 * for the lastest ACK status for a connection.
3668 * @param c Connection.
3669 * @param fwd Should we poll in the FWD direction?
3672 GCC_start_poll (struct CadetConnection *c, int fwd)
3674 struct CadetFlowControl *fc;
3676 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3677 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3679 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3681 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not needed (%p, %p)\n",
3682 fc->poll_task, fc->poll_msg);
3685 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3686 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3693 * @brief Stop polling a connection for ACKs.
3695 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3697 * @param c Connection.
3698 * @param fwd Should we stop the poll in the FWD direction?
3701 GCC_stop_poll (struct CadetConnection *c, int fwd)
3703 struct CadetFlowControl *fc;
3705 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3706 if (NULL != fc->poll_task)
3708 GNUNET_SCHEDULER_cancel (fc->poll_task);
3709 fc->poll_task = NULL;
3715 * Get a (static) string for a connection.
3717 * @param c Connection.
3720 GCC_2s (const struct CadetConnection *c)
3727 static char buf[128];
3729 SPRINTF (buf, "%s (->%s)",
3730 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3733 return GNUNET_h2s (GC_h2hc (&c->id));
3738 * Log all possible info about the connection state.
3740 * @param c Connection to debug.
3741 * @param level Debug level to use.
3744 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3749 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3751 __FILE__, __FUNCTION__, __LINE__);
3757 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3761 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3762 s = path_2s (c->path);
3763 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3765 LOG2 (level, "CCC state: %s, destroy: %u\n",
3766 GCC_state2s (c->state), c->destroy);
3767 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3768 if (NULL != c->perf)
3769 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3771 LOG2 (level, "CCC FWD flow control:\n");
3772 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3773 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3774 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3775 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3776 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3777 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3778 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3779 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3781 LOG2 (level, "CCC BCK flow control:\n");
3782 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3783 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3784 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3785 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3786 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3787 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3788 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3789 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3791 LOG2 (level, "CCC DEBUG CONNECTION END\n");