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:
733 LOG (GNUNET_ERROR_TYPE_ERROR, "%s unknown\n", GC_m2s (type));
737 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
740 return GNUNET_NO; /* Only endpoints are interested in timing. */
743 usecsperbyte = ((double) wait.rel_value_us) / size;
744 if (p->size == AVG_MSGS)
746 /* Array is full. Substract oldest value, add new one and store. */
747 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
748 p->usecsperbyte[p->idx] = usecsperbyte;
749 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
753 /* Array not yet full. Add current value to avg and store. */
754 p->usecsperbyte[p->idx] = usecsperbyte;
756 p->avg += p->usecsperbyte[p->idx];
760 p->idx = (p->idx + 1) % AVG_MSGS;
761 GCC_check_connections ();
767 * Get the previous hop in a connection
769 * @param c Connection.
771 * @return Previous peer in the connection.
773 static struct CadetPeer *
774 get_prev_hop (const struct CadetConnection *c)
780 LOG (GNUNET_ERROR_TYPE_DEBUG,
781 " get prev hop %s [%u/%u]\n",
782 GCC_2s (c), c->own_pos, c->path->length);
783 if (0 == c->own_pos || c->path->length < 2)
784 id = c->path->peers[0];
786 id = c->path->peers[c->own_pos - 1];
788 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
789 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
791 return GCP_get_short (id, GNUNET_YES);
796 * Get the next hop in a connection
798 * @param c Connection.
800 * @return Next peer in the connection.
802 static struct CadetPeer *
803 get_next_hop (const struct CadetConnection *c)
810 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
811 GCC_2s (c), c->own_pos, c->path->length);
812 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
813 id = c->path->peers[c->path->length - 1];
815 id = c->path->peers[c->own_pos + 1];
817 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
818 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
820 return GCP_get_short (id, GNUNET_YES);
825 * Check that the direct neighbours (previous and next hop)
826 * are properly associated with this connection.
828 * @param c connection to check
831 check_neighbours (const struct CadetConnection *c)
834 return; /* nothing to check */
835 GCP_check_connection (get_next_hop (c), c);
836 GCP_check_connection (get_prev_hop (c), c);
841 * Helper for #check_connections(). Calls #check_neighbours().
845 * @param value the `struct CadetConnection` to check
846 * @return #GNUNET_OK (continue to iterate)
849 check_connection (void *cls,
850 const struct GNUNET_HashCode *key,
853 struct CadetConnection *c = value;
855 check_neighbours (c);
861 * Check invariants for all connections using #check_neighbours().
864 GCC_check_connections ()
866 if (0 == CHECK_INVARIANTS)
868 if (NULL == connections)
870 GNUNET_CONTAINER_multihashmap_iterate (connections,
877 * Get the hop in a connection.
879 * @param c Connection.
880 * @param fwd Next in the FWD direction?
882 * @return Next peer in the connection.
884 static struct CadetPeer *
885 get_hop (struct CadetConnection *c, int fwd)
887 return (fwd) ? get_next_hop (c) : get_prev_hop (c);
892 * Get a bit mask for a message received out-of-order.
894 * @param last_pid_recv Last PID we received prior to the out-of-order.
895 * @param ooo_pid PID of the out-of-order message.
898 get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
900 return 1 << (last_pid_recv - ooo_pid);
905 * Check is an out-of-order message is ok:
906 * - at most 31 messages behind.
909 * @param last_pid_recv Last in-order PID received.
912 is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
916 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
919 mask = get_recv_bitmask (last_pid_recv, ooo_pid);
920 if (0 != (ooo_bitmap & mask))
928 * Is traffic coming from this sender 'FWD' traffic?
930 * @param c Connection to check.
931 * @param sender Peer identity of neighbor.
933 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
934 * the traffic is 'FWD'.
935 * #GNUNET_NO for BCK.
936 * #GNUNET_SYSERR for errors.
939 is_fwd (const struct CadetConnection *c,
940 const struct GNUNET_PeerIdentity *sender)
944 id = GNUNET_PEER_search (sender);
945 if (GCP_get_short_id (get_prev_hop (c)) == id)
948 if (GCP_get_short_id (get_next_hop (c)) == id)
952 return GNUNET_SYSERR;
957 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
958 * or a first CONNECTION_ACK directed to us.
960 * @param connection Connection to confirm.
961 * @param fwd Should we send it FWD? (root->dest)
962 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
965 send_connection_ack (struct CadetConnection *connection, int fwd)
967 struct CadetTunnel *t;
969 GCC_check_connections ();
971 LOG (GNUNET_ERROR_TYPE_INFO, "---> {%14s ACK} on connection %s\n",
972 GC_f2s (!fwd), GCC_2s (connection));
973 GCP_queue_add (get_hop (connection, fwd), NULL,
974 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0, 0,
975 sizeof (struct GNUNET_CADET_ConnectionACK),
976 connection, fwd, &conn_message_sent, NULL);
977 connection->pending_messages++;
978 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
979 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
980 if (CADET_CONNECTION_READY != connection->state)
981 connection_change_state (connection, CADET_CONNECTION_SENT);
982 GCC_check_connections ();
987 * Send a notification that a connection is broken.
989 * @param c Connection that is broken.
990 * @param id1 Peer that has disconnected.
991 * @param id2 Peer that has disconnected.
992 * @param fwd Direction towards which to send it.
995 send_broken (struct CadetConnection *c,
996 const struct GNUNET_PeerIdentity *id1,
997 const struct GNUNET_PeerIdentity *id2,
1000 struct GNUNET_CADET_ConnectionBroken msg;
1002 GCC_check_connections ();
1003 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1004 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1008 GNUNET_assert (NULL ==
1009 GCC_send_prebuilt_message (&msg.header, 0, 0, c, fwd,
1010 GNUNET_YES, NULL, NULL));
1011 GCC_check_connections ();
1016 * Send a notification that a connection is broken, when a connection
1017 * isn't even known to the local peer or soon to be destroyed.
1019 * @param connection_id Connection ID.
1020 * @param id1 Peer that has disconnected, probably local peer.
1021 * @param id2 Peer that has disconnected can be NULL if unknown.
1022 * @param peer Peer to notify (neighbor who sent the connection).
1025 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
1026 const struct GNUNET_PeerIdentity *id1,
1027 const struct GNUNET_PeerIdentity *id2,
1028 const struct GNUNET_PeerIdentity *peer_id)
1030 struct GNUNET_CADET_ConnectionBroken *msg;
1031 struct CadetPeer *neighbor;
1033 GCC_check_connections ();
1034 LOG (GNUNET_ERROR_TYPE_INFO, "---> BROKEN on unknown connection %s\n",
1035 GNUNET_h2s (GC_h2hc (connection_id)));
1037 msg = GNUNET_new (struct GNUNET_CADET_ConnectionBroken);
1038 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1039 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1040 msg->cid = *connection_id;
1045 memset (&msg->peer2, 0, sizeof (msg->peer2));
1046 neighbor = GCP_get (peer_id, GNUNET_NO); /* We MUST know neighbor. */
1047 GNUNET_assert (NULL != neighbor);
1048 GCP_queue_add (neighbor, msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
1049 0, 2, sizeof (struct GNUNET_CADET_ConnectionBroken),
1050 NULL, GNUNET_SYSERR, /* connection, fwd */
1051 NULL, NULL); /* continuation */
1052 GCC_check_connections ();
1057 * Send keepalive packets for a connection.
1059 * @param c Connection to keep alive..
1060 * @param fwd Is this a FWD keepalive? (owner -> dest).
1063 send_connection_keepalive (struct CadetConnection *c, int fwd)
1065 struct GNUNET_MessageHeader msg;
1066 struct CadetFlowControl *fc;
1068 GCC_check_connections ();
1069 LOG (GNUNET_ERROR_TYPE_INFO,
1070 "keepalive %s for connection %s\n",
1071 GC_f2s (fwd), GCC_2s (c));
1073 GNUNET_assert (NULL != c->t);
1074 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1075 if (0 < fc->queue_n || GNUNET_YES == GCT_has_queued_traffic (c->t))
1077 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1081 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1083 GNUNET_assert (NULL != c->t);
1084 msg.size = htons (sizeof (msg));
1085 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
1087 GNUNET_assert (NULL ==
1088 GCT_send_prebuilt_message (&msg, c->t, c,
1089 GNUNET_NO, NULL, NULL));
1090 GCC_check_connections ();
1095 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1097 * @param c Connection for which to send the message.
1098 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1101 connection_recreate (struct CadetConnection *c, int fwd)
1103 LOG (GNUNET_ERROR_TYPE_DEBUG,
1104 "sending connection recreate\n");
1106 GCC_send_create (c);
1108 send_connection_ack (c, GNUNET_NO);
1113 * Generic connection timer management.
1114 * Depending on the role of the peer in the connection will send the
1115 * appropriate message (build or keepalive)
1117 * @param c Conncetion to maintain.
1118 * @param fwd Is FWD?
1121 connection_maintain (struct CadetConnection *c, int fwd)
1123 if (GNUNET_NO != c->destroy)
1125 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1132 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1136 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1138 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1140 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1141 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1142 schedule_next_keepalive (c, fwd);
1147 case CADET_CONNECTION_NEW:
1150 case CADET_CONNECTION_SENT:
1151 connection_recreate (c, fwd);
1153 case CADET_CONNECTION_READY:
1154 send_connection_keepalive (c, fwd);
1163 * Keep the connection alive.
1165 * @param c Connection to keep alive.
1166 * @param fwd Direction.
1167 * @param shutdown Are we shutting down? (Don't send traffic)
1168 * Non-zero value for true, not necessarily GNUNET_YES.
1171 connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
1173 GCC_check_connections ();
1174 LOG (GNUNET_ERROR_TYPE_DEBUG,
1175 "%s keepalive for %s\n",
1176 GC_f2s (fwd), GCC_2s (c));
1179 c->fwd_maintenance_task = NULL;
1181 c->bck_maintenance_task = NULL;
1183 if (GNUNET_NO != shutdown)
1186 connection_maintain (c, fwd);
1187 GCC_check_connections ();
1188 /* Next execution will be scheduled by message_sent or _maintain*/
1193 * Keep the connection alive in the FWD direction.
1195 * @param cls Closure (connection to keepalive).
1196 * @param tc TaskContext.
1199 connection_fwd_keepalive (void *cls,
1200 const struct GNUNET_SCHEDULER_TaskContext *tc)
1202 GCC_check_connections ();
1203 connection_keepalive ((struct CadetConnection *) cls,
1205 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1206 GCC_check_connections ();
1211 * Keep the connection alive in the BCK direction.
1213 * @param cls Closure (connection to keepalive).
1214 * @param tc TaskContext.
1217 connection_bck_keepalive (void *cls,
1218 const struct GNUNET_SCHEDULER_TaskContext *tc)
1220 GCC_check_connections ();
1221 connection_keepalive ((struct CadetConnection *) cls,
1223 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1224 GCC_check_connections ();
1229 * Schedule next keepalive task, taking in consideration
1230 * the connection state and number of retries.
1232 * If the peer is not the origin, do nothing.
1234 * @param c Connection for which to schedule the next keepalive.
1235 * @param fwd Direction for the next keepalive.
1238 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1240 struct GNUNET_TIME_Relative delay;
1241 struct GNUNET_SCHEDULER_Task * *task_id;
1242 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1244 GCC_check_connections ();
1245 if (GNUNET_NO == GCC_is_origin (c, fwd))
1248 /* Calculate delay to use, depending on the state of the connection */
1249 if (CADET_CONNECTION_READY == c->state)
1251 delay = refresh_connection_time;
1255 if (1 > c->create_retry)
1256 c->create_retry = 1;
1257 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1259 if (c->create_retry < 64)
1260 c->create_retry *= 2;
1263 /* Select direction-dependent parameters */
1264 if (GNUNET_YES == fwd)
1266 task_id = &c->fwd_maintenance_task;
1267 keepalive_task = &connection_fwd_keepalive;
1271 task_id = &c->bck_maintenance_task;
1272 keepalive_task = &connection_bck_keepalive;
1275 /* Check that no one scheduled it before us */
1276 if (NULL != *task_id)
1278 /* No need for a _break. It can happen for instance when sending a SYNACK
1279 * for a duplicate SYN: the first SYNACK scheduled the task. */
1280 GNUNET_SCHEDULER_cancel (*task_id);
1283 /* Schedule the task */
1284 *task_id = GNUNET_SCHEDULER_add_delayed (delay,
1287 LOG (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1288 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1289 GCC_check_connections ();
1294 * @brief Re-initiate traffic on this connection if necessary.
1296 * Check if there is traffic queued towards this peer
1297 * and the core transmit handle is NULL (traffic was stalled).
1298 * If so, call core tmt rdy.
1300 * @param c Connection on which initiate traffic.
1301 * @param fwd Is this about fwd traffic?
1304 connection_unlock_queue (struct CadetConnection *c, int fwd)
1306 struct CadetPeer *peer;
1308 GCC_check_connections ();
1309 LOG (GNUNET_ERROR_TYPE_DEBUG,
1310 "connection_unlock_queue %s on %s\n",
1311 GC_f2s (fwd), GCC_2s (c));
1313 if (GCC_is_terminal (c, fwd))
1315 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal, can unlock!\n");
1319 peer = get_hop (c, fwd);
1320 GCP_queue_unlock (peer, c);
1321 GCC_check_connections ();
1326 * Cancel all transmissions that belong to a certain connection.
1328 * If the connection is scheduled for destruction and no more messages are left,
1329 * the connection will be destroyed by the continuation call.
1331 * @param c Connection which to cancel. Might be destroyed during this call.
1332 * @param fwd Cancel fwd traffic?
1335 connection_cancel_queues (struct CadetConnection *c,
1338 struct CadetFlowControl *fc;
1339 struct CadetPeer *peer;
1341 GCC_check_connections ();
1342 LOG (GNUNET_ERROR_TYPE_DEBUG,
1343 "Cancel %s queues for connection %s\n",
1344 GC_f2s (fwd), GCC_2s (c));
1351 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1352 if (NULL != fc->poll_task)
1354 GNUNET_SCHEDULER_cancel (fc->poll_task);
1355 fc->poll_task = NULL;
1356 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cancel POLL in ccq for fc %p\n", fc);
1358 peer = get_hop (c, fwd);
1359 GCP_queue_cancel (peer, c);
1360 GCC_check_connections ();
1365 * Function called if a connection has been stalled for a while,
1366 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1368 * @param cls Closure (poll ctx).
1369 * @param tc TaskContext.
1372 connection_poll (void *cls,
1373 const struct GNUNET_SCHEDULER_TaskContext *tc);
1377 * Callback called when a queued POLL message is sent.
1379 * @param cls Closure (FC).
1380 * @param c Connection this message was on.
1381 * @param q Queue handler this call invalidates.
1382 * @param type Type of message sent.
1383 * @param fwd Was this a FWD going message?
1384 * @param size Size of the message.
1387 poll_sent (void *cls,
1388 struct CadetConnection *c,
1389 struct CadetConnectionQueue *q,
1390 uint16_t type, int fwd, size_t size)
1392 struct CadetFlowControl *fc = cls;
1394 if (2 == c->destroy)
1396 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1399 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1401 fc->poll_msg = NULL;
1402 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1403 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1406 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1411 * Function called if a connection has been stalled for a while,
1412 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1414 * @param cls Closure (poll ctx).
1415 * @param tc TaskContext.
1418 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1420 struct CadetFlowControl *fc = cls;
1421 struct GNUNET_CADET_Poll msg;
1422 struct CadetConnection *c;
1425 fc->poll_task = NULL;
1426 GCC_check_connections ();
1427 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1433 fwd = fc == &c->fwd_fc;
1434 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1435 GCC_2s (c), GC_f2s (fwd));
1437 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1438 msg.header.size = htons (sizeof (msg));
1439 msg.pid = htonl (fc->last_pid_sent);
1440 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1442 GCC_send_prebuilt_message (&msg.header, 0, fc->last_pid_sent, c,
1443 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1444 GNUNET_assert (NULL != fc->poll_msg);
1445 GCC_check_connections ();
1450 * Resend all queued messages for a connection on other connections of the
1451 * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
1453 * @param c Connection whose messages to resend.
1454 * @param fwd Resend fwd messages?
1457 resend_messages_and_destroy (struct CadetConnection *c, int fwd)
1459 struct GNUNET_MessageHeader *out_msg;
1460 struct CadetTunnel *t = c->t;
1461 struct CadetPeer *neighbor;
1462 unsigned int pending;
1465 GCC_check_connections ();
1466 c->state = CADET_CONNECTION_DESTROYED;
1467 c->destroy = GNUNET_YES;
1469 destroyed = GNUNET_NO;
1470 neighbor = get_hop (c, fwd);
1471 pending = c->pending_messages;
1473 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1476 GCT_resend_message (out_msg, t);
1477 GNUNET_free (out_msg);
1480 /* All pending messages should have been popped,
1481 * and the connection destroyed by the continuation.
1483 if (GNUNET_YES != destroyed)
1488 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1489 if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1493 GCC_check_connections ();
1498 * Generic connection timeout implementation.
1500 * Timeout function due to lack of keepalive/traffic from an endpoint.
1501 * Destroys connection if called.
1503 * @param c Connection to destroy.
1504 * @param fwd Was the timeout from the origin? (FWD timeout)
1507 connection_timeout (struct CadetConnection *c, int fwd)
1509 struct CadetFlowControl *reverse_fc;
1511 GCC_check_connections ();
1512 reverse_fc = fwd ? &c->bck_fc : &c->fwd_fc;
1514 LOG (GNUNET_ERROR_TYPE_INFO,
1515 "Connection %s %s timed out. Destroying.\n",
1518 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1520 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1526 /* If dest, salvage queued traffic. */
1527 if (GCC_is_origin (c, !fwd))
1529 const struct GNUNET_PeerIdentity *next_hop;
1531 next_hop = GCP_get_id (fwd ? get_prev_hop (c) : get_next_hop (c));
1532 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1533 if (0 < reverse_fc->queue_n)
1534 resend_messages_and_destroy (c, !fwd);
1535 GCC_check_connections ();
1540 GCC_check_connections ();
1545 * Timeout function due to lack of keepalive/traffic from the owner.
1546 * Destroys connection if called.
1548 * @param cls Closure (connection to destroy).
1549 * @param tc TaskContext.
1552 connection_fwd_timeout (void *cls,
1553 const struct GNUNET_SCHEDULER_TaskContext *tc)
1555 struct CadetConnection *c = cls;
1557 c->fwd_maintenance_task = NULL;
1558 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1560 GCC_check_connections ();
1561 connection_timeout (c, GNUNET_YES);
1562 GCC_check_connections ();
1567 * Timeout function due to lack of keepalive/traffic from the destination.
1568 * Destroys connection if called.
1570 * @param cls Closure (connection to destroy).
1571 * @param tc TaskContext
1574 connection_bck_timeout (void *cls,
1575 const struct GNUNET_SCHEDULER_TaskContext *tc)
1577 struct CadetConnection *c = cls;
1579 c->bck_maintenance_task = NULL;
1580 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1582 GCC_check_connections ();
1583 connection_timeout (c, GNUNET_NO);
1584 GCC_check_connections ();
1589 * Resets the connection timeout task, some other message has done the
1591 * - For the first peer on the direction this means to send
1592 * a keepalive or a path confirmation message (either create or ACK).
1593 * - For all other peers, this means to destroy the connection,
1594 * due to lack of activity.
1595 * Starts the timeout if no timeout was running (connection just created).
1597 * @param c Connection whose timeout to reset.
1598 * @param fwd Is this forward?
1600 * TODO use heap to improve efficiency of scheduler.
1603 connection_reset_timeout (struct CadetConnection *c, int fwd)
1605 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1606 if (GCC_is_origin (c, fwd)) /* Startpoint */
1608 schedule_next_keepalive (c, fwd);
1610 else /* Relay, endpoint. */
1612 struct GNUNET_TIME_Relative delay;
1613 struct GNUNET_SCHEDULER_Task * *ti;
1614 GNUNET_SCHEDULER_TaskCallback f;
1616 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1619 GNUNET_SCHEDULER_cancel (*ti);
1620 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1621 LOG (GNUNET_ERROR_TYPE_DEBUG, " timing out in %s\n",
1622 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1623 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1624 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1630 * Iterator to compare each connection's path with the path of a new connection.
1632 * If the connection conincides, the c member of path is set to the connection
1633 * and the destroy flag of the connection is set.
1635 * @param cls Closure (new path).
1636 * @param c Connection in the tunnel to check.
1639 check_path (void *cls, struct CadetConnection *c)
1641 struct CadetConnection *new_conn = cls;
1642 struct CadetPeerPath *path = new_conn->path;
1644 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1645 GCC_2s (c), c, c->path->length);
1648 && c->destroy == GNUNET_NO
1649 && c->state != CADET_CONNECTION_BROKEN
1650 && c->state != CADET_CONNECTION_DESTROYED
1651 && path_equivalent (path, c->path))
1653 new_conn->destroy = GNUNET_YES;
1654 new_conn->path->c = c;
1655 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1661 * Finds out if this path is already being used by an existing connection.
1663 * Checks the tunnel towards the destination to see if it contains
1664 * any connection with the same path.
1666 * If the existing connection is ready, it is kept.
1667 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1668 * the peer will eventually reject our attempt).
1670 * @param path Path to check.
1671 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1672 * #GNUNET_NO otherwise.
1675 does_connection_exist (struct CadetConnection *conn)
1677 struct CadetPeer *p;
1678 struct CadetTunnel *t;
1679 struct CadetConnection *c;
1681 p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
1684 t = GCP_get_tunnel (p);
1688 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1690 GCT_iterate_connections (t, &check_path, conn);
1692 if (GNUNET_YES == conn->destroy)
1695 conn->destroy = GNUNET_NO;
1696 conn->path->c = conn;
1697 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1698 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1699 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1700 if (CADET_CONNECTION_READY == c->state)
1702 /* The other peer confirmed a live connection with this path,
1703 * why is it trying to duplicate it. */
1704 GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
1707 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not valid, connection unique\n");
1712 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1719 * @brief Check if the tunnel this connection belongs to has any other
1720 * connection with the same path, and destroy one if so.
1722 * @param cls Closure (connection to check).
1723 * @param tc Task context.
1726 check_duplicates (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1728 struct CadetConnection *c = cls;
1730 c->check_duplicates_task = NULL;
1731 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1734 if (GNUNET_YES == does_connection_exist (c))
1736 GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1737 send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
1745 * Wait for enough time to let any dead connections time out and check for
1746 * any remaining duplicates.
1748 * @param c Connection that is a potential duplicate.
1751 schedule_check_duplicates (struct CadetConnection *c)
1753 struct GNUNET_TIME_Relative delay;
1755 if (NULL != c->check_duplicates_task)
1758 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 5);
1759 c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
1767 * Add the connection to the list of both neighbors.
1769 * @param c Connection.
1771 * @return #GNUNET_OK if everything went fine
1772 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1775 register_neighbors (struct CadetConnection *c)
1777 c->next_peer = get_next_hop (c);
1778 c->prev_peer = get_prev_hop (c);
1779 GNUNET_assert (c->next_peer != c->prev_peer);
1780 LOG (GNUNET_ERROR_TYPE_DEBUG,
1781 "register neighbors for connection %s\n",
1783 path_debug (c->path);
1784 LOG (GNUNET_ERROR_TYPE_DEBUG,
1785 "own pos %u\n", c->own_pos);
1786 LOG (GNUNET_ERROR_TYPE_DEBUG,
1787 "putting connection %s to next peer %p\n",
1790 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1792 GCP_2s (c->next_peer));
1793 LOG (GNUNET_ERROR_TYPE_DEBUG,
1794 "putting connection %s to prev peer %p\n",
1797 LOG (GNUNET_ERROR_TYPE_DEBUG,
1798 "prev peer %p %s\n",
1800 GCP_2s (c->prev_peer));
1802 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1803 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1805 if (GCC_is_origin (c, GNUNET_YES))
1806 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1807 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1809 LOG (GNUNET_ERROR_TYPE_DEBUG,
1810 " register neighbors failed\n");
1811 LOG (GNUNET_ERROR_TYPE_DEBUG,
1812 " prev: %s, neighbor?: %d\n",
1813 GCP_2s (c->prev_peer),
1814 GCP_is_neighbor (c->prev_peer));
1815 LOG (GNUNET_ERROR_TYPE_DEBUG,
1816 " next: %s, neighbor?: %d\n",
1817 GCP_2s (c->next_peer),
1818 GCP_is_neighbor (c->next_peer));
1819 return GNUNET_SYSERR;
1821 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1822 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1829 * Remove the connection from the list of both neighbors.
1831 * @param c Connection.
1834 unregister_neighbors (struct CadetConnection *c)
1836 /* Either already unregistered or never got registered, it's ok either way. */
1837 if (NULL == c->path)
1839 if (NULL != c->next_peer)
1841 GCP_remove_connection (c->next_peer, c);
1842 c->next_peer = NULL;
1844 if (NULL != c->prev_peer)
1846 GCP_remove_connection (c->prev_peer, c);
1847 c->prev_peer = NULL;
1853 * Bind the connection to the peer and the tunnel to that peer.
1855 * If the peer has no tunnel, create one. Update tunnel and connection
1856 * data structres to reflect new status.
1858 * @param c Connection.
1862 add_to_peer (struct CadetConnection *c,
1863 struct CadetPeer *peer)
1865 GCP_add_tunnel (peer);
1866 c->t = GCP_get_tunnel (peer);
1867 GCT_add_connection (c->t, c);
1872 * Log receipt of message on stderr (INFO level).
1874 * @param message Message received.
1875 * @param peer Peer who sent the message.
1876 * @param hash Connection ID.
1879 log_message (const struct GNUNET_MessageHeader *message,
1880 const struct GNUNET_PeerIdentity *peer,
1881 const struct GNUNET_CADET_Hash *hash)
1885 size = ntohs (message->size);
1886 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1887 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1888 LOG (GNUNET_ERROR_TYPE_INFO, "<-- %s on connection %s from %s, %6u bytes\n",
1889 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (hash)),
1890 GNUNET_i2s (peer), (unsigned int) size);
1893 /******************************************************************************/
1894 /******************************** API ***********************************/
1895 /******************************************************************************/
1898 * Core handler for connection creation.
1900 * @param cls Closure (unused).
1901 * @param peer Sender (neighbor).
1902 * @param message Message.
1903 * @return #GNUNET_OK to keep the connection open,
1904 * #GNUNET_SYSERR to close it (signal serious error)
1907 GCC_handle_create (void *cls,
1908 const struct GNUNET_PeerIdentity *peer,
1909 const struct GNUNET_MessageHeader *message)
1911 struct GNUNET_CADET_ConnectionCreate *msg;
1912 struct GNUNET_PeerIdentity *id;
1913 struct GNUNET_CADET_Hash *cid;
1914 struct CadetPeerPath *path;
1915 struct CadetPeer *dest_peer;
1916 struct CadetPeer *orig_peer;
1917 struct CadetConnection *c;
1918 unsigned int own_pos;
1921 GCC_check_connections ();
1923 size = ntohs (message->size);
1924 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1926 GNUNET_break_op (0);
1930 /* Calculate hops */
1931 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1932 if (size % sizeof (struct GNUNET_PeerIdentity))
1934 GNUNET_break_op (0);
1937 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1939 GNUNET_break_op (0);
1942 size /= sizeof (struct GNUNET_PeerIdentity);
1945 GNUNET_break_op (0);
1948 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1950 /* Get parameters */
1951 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
1953 log_message (message, peer, cid);
1954 id = (struct GNUNET_PeerIdentity *) &msg[1];
1955 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1957 /* Create connection */
1958 c = connection_get (cid);
1961 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1962 size, myid, &own_pos);
1965 /* Path was malformed, probably our own ID was not in it. */
1966 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
1967 GNUNET_break_op (0);
1973 /* We received this request from a neighbor, we cannot be origin */
1974 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
1975 GNUNET_break_op (0);
1976 path_destroy (path);
1980 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1981 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1982 c = GCC_new (cid, NULL, path, own_pos);
1985 if (path->length - 1 == own_pos)
1987 /* If we are destination, why did the creation fail? */
1989 path_destroy (path);
1990 GCC_check_connections ();
1993 send_broken_unknown (cid, &my_full_id,
1994 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1996 path_destroy (path);
1997 GCC_check_connections ();
2000 GCP_add_path_to_all (path, GNUNET_NO);
2001 connection_reset_timeout (c, GNUNET_YES);
2005 path = path_duplicate (c->path);
2007 if (CADET_CONNECTION_NEW == c->state)
2008 connection_change_state (c, CADET_CONNECTION_SENT);
2010 /* Remember peers */
2011 dest_peer = GCP_get (&id[size - 1], GNUNET_YES);
2012 orig_peer = GCP_get (&id[0], GNUNET_YES);
2014 /* Is it a connection to us? */
2015 if (c->own_pos == path->length - 1)
2017 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2018 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
2020 add_to_peer (c, orig_peer);
2021 if (GNUNET_YES == does_connection_exist (c))
2023 /* Peer created a connection equal to one we think exists
2025 * Solution: Keep both and postpone disambiguation. In the meantime
2026 * the connection will time out or peer will inform us it is broken.
2029 * - Use explicit duplicate.
2030 * - Accept new conn and destroy the old. (interruption in higher level)
2031 * - Keep the one with higher ID / created by peer with higher ID. */
2032 schedule_check_duplicates (c);
2035 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
2036 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
2038 send_connection_ack (c, GNUNET_NO);
2039 if (CADET_CONNECTION_SENT == c->state)
2040 connection_change_state (c, CADET_CONNECTION_ACK);
2044 /* It's for somebody else! Retransmit. */
2045 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2046 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2047 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2048 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
2049 GNUNET_YES, GNUNET_YES,
2052 path_destroy (path);
2053 GCC_check_connections ();
2059 * Core handler for path confirmations.
2061 * @param cls closure
2062 * @param message message
2063 * @param peer peer identity this notification is about
2064 * @return #GNUNET_OK to keep the connection open,
2065 * #GNUNET_SYSERR to close it (signal serious error)
2068 GCC_handle_confirm (void *cls,
2069 const struct GNUNET_PeerIdentity *peer,
2070 const struct GNUNET_MessageHeader *message)
2072 struct GNUNET_CADET_ConnectionACK *msg;
2073 struct CadetConnection *c;
2074 struct CadetPeerPath *p;
2075 struct CadetPeer *pi;
2076 enum CadetConnectionState oldstate;
2079 GCC_check_connections ();
2080 msg = (struct GNUNET_CADET_ConnectionACK *) message;
2081 log_message (message, peer, &msg->cid);
2082 c = connection_get (&msg->cid);
2085 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2087 LOG (GNUNET_ERROR_TYPE_DEBUG,
2088 " don't know the connection!\n");
2089 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2090 GCC_check_connections ();
2094 oldstate = c->state;
2095 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
2096 pi = GCP_get (peer, GNUNET_YES);
2097 if (get_next_hop (c) == pi)
2099 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2101 if (CADET_CONNECTION_SENT == oldstate)
2102 connection_change_state (c, CADET_CONNECTION_ACK);
2104 else if (get_prev_hop (c) == pi)
2106 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2108 connection_change_state (c, CADET_CONNECTION_READY);
2112 GNUNET_break_op (0);
2116 connection_reset_timeout (c, fwd);
2118 /* Add path to peers? */
2122 GCP_add_path_to_all (p, GNUNET_YES);
2129 /* Message for us as creator? */
2130 if (GCC_is_origin (c, GNUNET_YES))
2132 if (GNUNET_NO != fwd)
2134 GNUNET_break_op (0);
2137 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2139 /* If just created, cancel the short timeout and start a long one */
2140 if (CADET_CONNECTION_SENT == oldstate)
2141 connection_reset_timeout (c, GNUNET_YES);
2143 /* Change connection state */
2144 connection_change_state (c, CADET_CONNECTION_READY);
2145 send_connection_ack (c, GNUNET_YES);
2147 /* Change tunnel state, trigger KX */
2148 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2149 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2150 GCC_check_connections ();
2154 /* Message for us as destination? */
2155 if (GCC_is_terminal (c, GNUNET_YES))
2157 if (GNUNET_YES != fwd)
2159 GNUNET_break_op (0);
2162 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2164 /* If just created, cancel the short timeout and start a long one */
2165 if (CADET_CONNECTION_ACK == oldstate)
2166 connection_reset_timeout (c, GNUNET_NO);
2168 /* Change tunnel state */
2169 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2170 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2171 GCC_check_connections ();
2175 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2176 GNUNET_assert (NULL ==
2177 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2178 GNUNET_YES, NULL, NULL));
2179 GCC_check_connections ();
2185 * Core handler for notifications of broken connections.
2187 * @param cls Closure (unused).
2188 * @param id Peer identity of sending neighbor.
2189 * @param message Message.
2190 * @return #GNUNET_OK to keep the connection open,
2191 * #GNUNET_SYSERR to close it (signal serious error)
2194 GCC_handle_broken (void* cls,
2195 const struct GNUNET_PeerIdentity* id,
2196 const struct GNUNET_MessageHeader* message)
2198 struct GNUNET_CADET_ConnectionBroken *msg;
2199 struct CadetConnection *c;
2200 struct CadetTunnel *t;
2204 GCC_check_connections ();
2205 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
2206 log_message (message, id, &msg->cid);
2207 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2208 GNUNET_i2s (&msg->peer1));
2209 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2210 GNUNET_i2s (&msg->peer2));
2211 c = connection_get (&msg->cid);
2214 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2215 GCC_check_connections ();
2221 fwd = is_fwd (c, id);
2222 c->destroy = GNUNET_YES;
2223 if (GCC_is_terminal (c, fwd))
2225 struct CadetPeer *endpoint;
2229 /* A terminal connection should not have 't' set to NULL. */
2231 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2234 endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
2235 if (2 < c->path->length)
2236 path_invalidate (c->path);
2237 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2239 c->state = CADET_CONNECTION_BROKEN;
2240 GCT_remove_connection (t, c);
2243 pending = c->pending_messages;
2245 resend_messages_and_destroy (c, !fwd);
2251 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2252 GNUNET_YES, NULL, NULL));
2253 connection_cancel_queues (c, !fwd);
2255 GCC_check_connections ();
2261 * Core handler for tunnel destruction
2263 * @param cls Closure (unused).
2264 * @param peer Peer identity of sending neighbor.
2265 * @param message Message.
2266 * @return #GNUNET_OK to keep the connection open,
2267 * #GNUNET_SYSERR to close it (signal serious error)
2270 GCC_handle_destroy (void *cls,
2271 const struct GNUNET_PeerIdentity *peer,
2272 const struct GNUNET_MessageHeader *message)
2274 const struct GNUNET_CADET_ConnectionDestroy *msg;
2275 struct CadetConnection *c;
2278 GCC_check_connections ();
2279 msg = (const struct GNUNET_CADET_ConnectionDestroy *) message;
2280 log_message (message, peer, &msg->cid);
2281 c = connection_get (&msg->cid);
2284 /* Probably already got the message from another path,
2285 * destroyed the tunnel and retransmitted to children.
2288 GNUNET_STATISTICS_update (stats,
2289 "# control on unknown connection",
2291 LOG (GNUNET_ERROR_TYPE_DEBUG,
2292 " connection unknown: already destroyed?\n");
2293 GCC_check_connections ();
2296 fwd = is_fwd (c, peer);
2297 if (GNUNET_SYSERR == fwd)
2299 GNUNET_break_op (0); /* FIXME */
2302 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2304 GNUNET_assert (NULL ==
2305 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2306 GNUNET_YES, NULL, NULL));
2308 else if (0 == c->pending_messages)
2310 LOG (GNUNET_ERROR_TYPE_DEBUG,
2311 " directly destroying connection!\n");
2313 GCC_check_connections ();
2316 c->destroy = GNUNET_YES;
2317 c->state = CADET_CONNECTION_DESTROYED;
2320 GCT_remove_connection (c->t, c);
2323 GCC_check_connections ();
2329 * Check the message against internal state and test if it goes FWD or BCK.
2331 * Updates the PID, state and timeout values for the connection.
2333 * @param message Message to check. It must belong to an existing connection.
2334 * @param minimum_size The message cannot be smaller than this value.
2335 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2336 * @param c Connection this message should belong. If NULL, check fails.
2337 * @param neighbor Neighbor that sent the message.
2340 check_message (const struct GNUNET_MessageHeader *message,
2341 size_t minimum_size,
2342 const struct GNUNET_CADET_Hash* cid,
2343 struct CadetConnection *c,
2344 const struct GNUNET_PeerIdentity *neighbor,
2347 GNUNET_PEER_Id neighbor_id;
2348 struct CadetFlowControl *fc;
2349 struct CadetPeer *hop;
2354 if (ntohs (message->size) < minimum_size)
2356 GNUNET_break_op (0);
2357 LOG (GNUNET_ERROR_TYPE_WARNING, "Size %u < %u\n",
2358 ntohs (message->size), minimum_size);
2359 return GNUNET_SYSERR;
2362 /* Check connection */
2365 GNUNET_STATISTICS_update (stats,
2366 "# unknown connection",
2368 LOG (GNUNET_ERROR_TYPE_DEBUG,
2369 "%s on unknown connection %s\n",
2370 GC_m2s (ntohs (message->type)),
2371 GNUNET_h2s (GC_h2hc (cid)));
2372 send_broken_unknown (cid,
2376 return GNUNET_SYSERR;
2379 /* Check if origin is as expected */
2380 neighbor_id = GNUNET_PEER_search (neighbor);
2381 hop = get_prev_hop (c);
2382 if (neighbor_id == GCP_get_short_id (hop))
2388 hop = get_next_hop (c);
2389 GNUNET_break (hop == c->next_peer);
2390 if (neighbor_id == GCP_get_short_id (hop))
2396 /* Unexpected peer sending traffic on a connection. */
2397 GNUNET_break_op (0);
2398 return GNUNET_SYSERR;
2402 /* Check PID for payload messages */
2403 type = ntohs (message->type);
2404 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
2405 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
2407 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2408 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2409 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2410 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2412 GNUNET_break_op (0);
2413 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2414 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2415 pid, fc->last_pid_recv, fc->last_ack_sent);
2416 return GNUNET_SYSERR;
2418 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2422 delta = pid - fc->last_pid_recv;
2423 fc->last_pid_recv = pid;
2424 fc->recv_bitmap <<= delta;
2425 fc->recv_bitmap |= 1;
2429 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2430 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2432 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2433 pid, fc->last_pid_recv - 31);
2434 return GNUNET_SYSERR;
2436 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2440 /* Count as connection confirmation. */
2441 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2443 connection_change_state (c, CADET_CONNECTION_READY);
2446 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2447 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2450 connection_reset_timeout (c, fwd);
2457 * Generic handler for cadet network encrypted traffic.
2459 * @param peer Peer identity this notification is about.
2460 * @param msg Encrypted message.
2461 * @return #GNUNET_OK to keep the connection open,
2462 * #GNUNET_SYSERR to close it (signal serious error)
2465 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
2466 const struct GNUNET_MessageHeader *message)
2468 const struct GNUNET_CADET_Encrypted *otr_msg;
2469 const struct GNUNET_CADET_AX *ax_msg;
2470 const struct GNUNET_CADET_Hash* cid;
2471 struct CadetConnection *c;
2472 size_t minumum_size;
2478 GCC_check_connections ();
2479 if (GNUNET_MESSAGE_TYPE_CADET_AX == ntohs (message->type))
2481 overhead = sizeof (struct GNUNET_CADET_AX);
2482 ax_msg = (const struct GNUNET_CADET_AX *) message;
2484 pid = ntohl (ax_msg->pid);
2489 overhead = sizeof (struct GNUNET_CADET_Encrypted);
2490 otr_msg = (const struct GNUNET_CADET_Encrypted *) message;
2491 cid = &otr_msg->cid;
2492 pid = ntohl (otr_msg->pid);
2495 log_message (message, peer, cid);
2497 minumum_size = sizeof (struct GNUNET_MessageHeader) + overhead;
2498 c = connection_get (cid);
2499 fwd = check_message (message,
2506 /* If something went wrong, discard message. */
2507 if (GNUNET_SYSERR == fwd)
2509 GCC_check_connections ();
2513 /* Is this message for us? */
2514 if (GCC_is_terminal (c, fwd))
2516 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2520 GNUNET_break (GNUNET_NO != c->destroy);
2523 GCT_handle_encrypted (c->t, message);
2524 GCC_send_ack (c, fwd, GNUNET_NO);
2525 GCC_check_connections ();
2529 /* Message not for us: forward to next hop */
2530 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2531 if (NULL != otr_msg) /* only otr has ttl */
2533 ttl = ntohl (otr_msg->ttl);
2534 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2537 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2538 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2539 GCC_send_ack (c, fwd, GNUNET_NO);
2540 GCC_check_connections ();
2545 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2546 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2547 GNUNET_NO, NULL, NULL));
2548 GCC_check_connections ();
2554 * Generic handler for cadet network encrypted traffic.
2556 * @param peer Peer identity this notification is about.
2557 * @param msg Encrypted message.
2558 * @return #GNUNET_OK to keep the connection open,
2559 * #GNUNET_SYSERR to close it (signal serious error)
2562 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2563 const struct GNUNET_CADET_KX *msg)
2565 const struct GNUNET_CADET_Hash* cid;
2566 struct CadetConnection *c;
2567 size_t expected_size;
2570 GCC_check_connections ();
2572 log_message (&msg->header, peer, cid);
2574 expected_size = sizeof (struct GNUNET_CADET_KX)
2575 + sizeof (struct GNUNET_MessageHeader);
2576 c = connection_get (cid);
2577 fwd = check_message (&msg->header,
2584 /* If something went wrong, discard message. */
2585 if (GNUNET_SYSERR == fwd)
2588 /* Is this message for us? */
2589 if (GCC_is_terminal (c, fwd))
2591 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2592 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2598 GCT_handle_kx (c->t, &msg[1].header);
2599 GCC_check_connections ();
2603 /* Message not for us: forward to next hop */
2604 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2605 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2606 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2607 GNUNET_NO, NULL, NULL));
2608 GCC_check_connections ();
2614 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2616 * @param cls Closure (unused).
2617 * @param message Message received.
2618 * @param peer Peer who sent the message.
2619 * @return #GNUNET_OK to keep the connection open,
2620 * #GNUNET_SYSERR to close it (signal serious error)
2623 GCC_handle_kx (void *cls,
2624 const struct GNUNET_PeerIdentity *peer,
2625 const struct GNUNET_MessageHeader *message)
2627 GCC_check_connections ();
2628 return handle_cadet_kx (peer, (struct GNUNET_CADET_KX *) message);
2633 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2635 * @param cls Closure (unused).
2636 * @param message Message received.
2637 * @param peer Peer who sent the message.
2638 * @return #GNUNET_OK to keep the connection open,
2639 * #GNUNET_SYSERR to close it (signal serious error)
2642 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2643 const struct GNUNET_MessageHeader *message)
2645 GCC_check_connections ();
2646 return handle_cadet_encrypted (peer, message);
2651 * Core handler for cadet network traffic point-to-point acks.
2653 * @param cls closure
2654 * @param message message
2655 * @param peer peer identity this notification is about
2656 * @return #GNUNET_OK to keep the connection open,
2657 * #GNUNET_SYSERR to close it (signal serious error)
2660 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2661 const struct GNUNET_MessageHeader *message)
2663 struct GNUNET_CADET_ACK *msg;
2664 struct CadetConnection *c;
2665 struct CadetFlowControl *fc;
2670 GCC_check_connections ();
2671 msg = (struct GNUNET_CADET_ACK *) message;
2672 log_message (message, peer, &msg->cid);
2673 c = connection_get (&msg->cid);
2676 GNUNET_STATISTICS_update (stats,
2677 "# ack on unknown connection",
2680 send_broken_unknown (&msg->cid,
2684 GCC_check_connections ();
2688 /* Is this a forward or backward ACK? */
2689 id = GNUNET_PEER_search (peer);
2690 if (GCP_get_short_id (get_next_hop (c)) == id)
2695 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2702 GNUNET_break_op (0);
2706 ack = ntohl (msg->ack);
2707 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2708 GC_f2s (fwd), ack, fc->last_ack_recv);
2709 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2710 fc->last_ack_recv = ack;
2712 /* Cancel polling if the ACK is big enough. */
2713 if (NULL != fc->poll_task &&
2714 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2716 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2717 GNUNET_SCHEDULER_cancel (fc->poll_task);
2718 fc->poll_task = NULL;
2719 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2722 connection_unlock_queue (c, fwd);
2723 GCC_check_connections ();
2729 * Core handler for cadet network traffic point-to-point ack polls.
2731 * @param cls closure
2732 * @param message message
2733 * @param peer peer identity this notification is about
2734 * @return #GNUNET_OK to keep the connection open,
2735 * #GNUNET_SYSERR to close it (signal serious error)
2738 GCC_handle_poll (void *cls,
2739 const struct GNUNET_PeerIdentity *peer,
2740 const struct GNUNET_MessageHeader *message)
2742 struct GNUNET_CADET_Poll *msg;
2743 struct CadetConnection *c;
2744 struct CadetFlowControl *fc;
2749 GCC_check_connections ();
2750 msg = (struct GNUNET_CADET_Poll *) message;
2751 log_message (message, peer, &msg->cid);
2752 c = connection_get (&msg->cid);
2755 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2757 LOG (GNUNET_ERROR_TYPE_DEBUG,
2758 "POLL message on unknown connection %s!\n",
2759 GNUNET_h2s (GC_h2hc (&msg->cid)));
2760 send_broken_unknown (&msg->cid,
2764 GCC_check_connections ();
2768 /* Is this a forward or backward ACK?
2769 * Note: a poll should never be needed in a loopback case,
2770 * since there is no possiblility of packet loss there, so
2771 * this way of discerining FWD/BCK should not be a problem.
2773 id = GNUNET_PEER_search (peer);
2774 if (GCP_get_short_id (get_next_hop (c)) == id)
2776 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2779 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2781 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2786 GNUNET_break_op (0);
2790 pid = ntohl (msg->pid);
2791 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2792 fc->last_pid_recv = pid;
2793 fwd = fc == &c->bck_fc;
2794 GCC_send_ack (c, fwd, GNUNET_YES);
2795 GCC_check_connections ();
2802 * Send an ACK on the appropriate connection/channel, depending on
2803 * the direction and the position of the peer.
2805 * @param c Which connection to send the hop-by-hop ACK.
2806 * @param fwd Is this a fwd ACK? (will go dest->root).
2807 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2810 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2812 unsigned int buffer;
2814 GCC_check_connections ();
2815 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
2816 GC_f2s (fwd), GCC_2s (c));
2824 if (GNUNET_NO != c->destroy)
2826 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2827 GCC_check_connections ();
2831 /* Get available buffer space */
2832 if (GCC_is_terminal (c, fwd))
2834 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2835 buffer = GCT_get_channels_buffer (c->t);
2839 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2840 buffer = GCC_get_buffer (c, fwd);
2842 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2843 if (0 == buffer && GNUNET_NO == force)
2845 GCC_check_connections ();
2849 /* Send available buffer space */
2850 if (GCC_is_origin (c, fwd))
2852 GNUNET_assert (NULL != c->t);
2853 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2854 GCT_unchoke_channels (c->t);
2858 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2859 send_ack (c, buffer, fwd, force);
2861 GCC_check_connections ();
2866 * Initialize the connections subsystem
2868 * @param c Configuration handle.
2871 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2873 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2875 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2878 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2879 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2880 GNUNET_SCHEDULER_shutdown ();
2885 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2888 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2889 "CADET", "MAX_CONNECTIONS", "MISSING");
2890 GNUNET_SCHEDULER_shutdown ();
2895 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2896 &refresh_connection_time))
2898 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2899 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2900 GNUNET_SCHEDULER_shutdown ();
2903 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2904 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2909 * Destroy each connection on shutdown.
2911 * @param cls Closure (unused).
2912 * @param key Current key code (CID, unused).
2913 * @param value Value in the hash map (`struct CadetConnection`)
2915 * @return #GNUNET_YES, because we should continue to iterate
2918 shutdown_iterator (void *cls,
2919 const struct GNUNET_HashCode *key,
2922 struct CadetConnection *c = value;
2924 c->state = CADET_CONNECTION_DESTROYED;
2931 * Shut down the connections subsystem.
2936 GCC_check_connections ();
2937 GNUNET_CONTAINER_multihashmap_iterate (connections,
2940 GNUNET_CONTAINER_multihashmap_destroy (connections);
2946 * Create a connection.
2948 * @param cid Connection ID (either created locally or imposed remotely).
2949 * @param t Tunnel this connection belongs to (or NULL);
2950 * @param path Path this connection has to use (copy is made).
2951 * @param own_pos Own position in the @c path path.
2953 * @return Newly created connection, NULL in case of error (own id not in path).
2955 struct CadetConnection *
2956 GCC_new (const struct GNUNET_CADET_Hash *cid,
2957 struct CadetTunnel *t,
2958 struct CadetPeerPath *path,
2959 unsigned int own_pos)
2961 struct CadetConnection *c;
2962 struct CadetPeerPath *p;
2964 GCC_check_connections ();
2965 p = path_duplicate (path);
2966 c = GNUNET_new (struct CadetConnection);
2968 GNUNET_assert (GNUNET_OK ==
2969 GNUNET_CONTAINER_multihashmap_put (connections,
2971 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2972 fc_init (&c->fwd_fc);
2973 fc_init (&c->bck_fc);
2978 GNUNET_assert (own_pos <= p->length - 1);
2979 c->own_pos = own_pos;
2982 GNUNET_assert (NULL != p);
2983 if (GNUNET_OK != register_neighbors (c))
2987 /* We were the origin of this request, this means we have invalid
2988 * info about the paths to reach the destination. We must invalidate
2989 * the *original* path to avoid trying it again in the next minute.
2991 path_invalidate (path);
2994 path_destroy (c->path);
2999 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
3000 GCC_check_connections ();
3006 GCC_destroy (struct CadetConnection *c)
3008 GCC_check_connections ();
3015 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
3016 return; /* -> message_sent -> GCC_destroy. Don't loop. */
3019 LOG (GNUNET_ERROR_TYPE_DEBUG,
3020 "destroying connection %s\n",
3022 LOG (GNUNET_ERROR_TYPE_DEBUG,
3023 " fc's f: %p, b: %p\n",
3024 &c->fwd_fc, &c->bck_fc);
3025 LOG (GNUNET_ERROR_TYPE_DEBUG,
3026 " fc tasks f: %u, b: %u\n",
3027 c->fwd_fc.poll_task,
3028 c->bck_fc.poll_task);
3030 /* Cancel all traffic */
3031 if (NULL != c->path)
3033 connection_cancel_queues (c, GNUNET_YES);
3034 connection_cancel_queues (c, GNUNET_NO);
3036 unregister_neighbors (c);
3037 path_destroy (c->path);
3040 /* Cancel maintainance task (keepalive/timeout) */
3041 if (NULL != c->fwd_fc.poll_msg)
3043 GCC_cancel (c->fwd_fc.poll_msg);
3044 LOG (GNUNET_ERROR_TYPE_DEBUG,
3045 " POLL msg FWD canceled\n");
3047 if (NULL != c->bck_fc.poll_msg)
3049 GCC_cancel (c->bck_fc.poll_msg);
3050 LOG (GNUNET_ERROR_TYPE_DEBUG,
3051 " POLL msg BCK canceled\n");
3054 /* Delete from tunnel */
3056 GCT_remove_connection (c->t, c);
3058 if (NULL != c->check_duplicates_task)
3059 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
3060 if (NULL != c->fwd_maintenance_task)
3061 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
3062 if (NULL != c->bck_maintenance_task)
3063 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
3064 if (NULL != c->fwd_fc.poll_task)
3066 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
3067 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task FWD canceled\n");
3069 if (NULL != c->bck_fc.poll_task)
3071 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
3072 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task BCK canceled\n");
3074 if (GNUNET_NO == c->was_removed)
3076 GNUNET_break (GNUNET_YES ==
3077 GNUNET_CONTAINER_multihashmap_remove (connections,
3081 GNUNET_STATISTICS_update (stats,
3086 GCC_check_connections ();
3091 * Get the connection ID.
3093 * @param c Connection to get the ID from.
3095 * @return ID of the connection.
3097 const struct GNUNET_CADET_Hash *
3098 GCC_get_id (const struct CadetConnection *c)
3105 * Get the connection ID.
3107 * @param c Connection to get the ID from.
3109 * @return ID of the connection.
3111 const struct GNUNET_HashCode *
3112 GCC_get_h (const struct CadetConnection *c)
3114 return GC_h2hc (&c->id);
3119 * Get the connection path.
3121 * @param c Connection to get the path from.
3123 * @return path used by the connection.
3125 const struct CadetPeerPath *
3126 GCC_get_path (const struct CadetConnection *c)
3128 if (GNUNET_NO == c->destroy)
3135 * Get the connection state.
3137 * @param c Connection to get the state from.
3139 * @return state of the connection.
3141 enum CadetConnectionState
3142 GCC_get_state (const struct CadetConnection *c)
3148 * Get the connection tunnel.
3150 * @param c Connection to get the tunnel from.
3152 * @return tunnel of the connection.
3154 struct CadetTunnel *
3155 GCC_get_tunnel (const struct CadetConnection *c)
3162 * Get free buffer space in a connection.
3164 * @param c Connection.
3165 * @param fwd Is query about FWD traffic?
3167 * @return Free buffer space [0 - max_msgs_queue/max_connections]
3170 GCC_get_buffer (struct CadetConnection *c, int fwd)
3172 struct CadetFlowControl *fc;
3174 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3176 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
3177 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
3178 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
3180 return (fc->queue_max - fc->queue_n);
3185 * Get how many messages have we allowed to send to us from a direction.
3187 * @param c Connection.
3188 * @param fwd Are we asking about traffic from FWD (BCK messages)?
3190 * @return last_ack_sent - last_pid_recv
3193 GCC_get_allowed (struct CadetConnection *c, int fwd)
3195 struct CadetFlowControl *fc;
3197 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3198 if (CADET_CONNECTION_READY != c->state
3199 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
3203 return (fc->last_ack_sent - fc->last_pid_recv);
3208 * Get messages queued in a connection.
3210 * @param c Connection.
3211 * @param fwd Is query about FWD traffic?
3213 * @return Number of messages queued.
3216 GCC_get_qn (struct CadetConnection *c, int fwd)
3218 struct CadetFlowControl *fc;
3220 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3227 * Get next PID to use.
3229 * @param c Connection.
3230 * @param fwd Is query about FWD traffic?
3232 * @return Last PID used + 1.
3235 GCC_get_pid (struct CadetConnection *c, int fwd)
3237 struct CadetFlowControl *fc;
3239 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3241 return fc->last_pid_sent + 1;
3246 * Allow the connection to advertise a buffer of the given size.
3248 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3249 * allowing up to last_pid_recv + buffer.
3251 * @param c Connection.
3252 * @param buffer How many more messages the connection can accept.
3253 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3256 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3258 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3259 GCC_2s (c), buffer, GC_f2s (fwd));
3260 send_ack (c, buffer, fwd, GNUNET_NO);
3265 * Notify other peers on a connection of a broken link. Mark connections
3266 * to destroy after all traffic has been sent.
3268 * @param c Connection on which there has been a disconnection.
3269 * @param peer Peer that disconnected.
3272 GCC_notify_broken (struct CadetConnection *c,
3273 struct CadetPeer *peer)
3275 struct CadetPeer *hop;
3278 GCC_check_connections ();
3279 LOG (GNUNET_ERROR_TYPE_DEBUG,
3280 "Notify broken on %s due to %s disconnect\n",
3283 hop = get_prev_hop (c);
3286 /* Path was NULL, we should have deleted the connection. */
3290 fwd = (peer == hop);
3291 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3292 (GNUNET_YES == c->destroy) )
3294 /* Local shutdown, or other peer already down (hence 'c->destroy');
3295 so there is no one to notify about this, just clean up. */
3297 GCC_check_connections ();
3300 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3301 /* Connection will have at least one pending message
3302 * (the one we just scheduled), so delay destruction
3303 * and remove from map so we don't use accidentally. */
3304 c->destroy = GNUNET_YES;
3305 c->state = CADET_CONNECTION_DESTROYED;
3306 GNUNET_assert (GNUNET_NO == c->was_removed);
3307 c->was_removed = GNUNET_YES;
3308 GNUNET_break (GNUNET_YES ==
3309 GNUNET_CONTAINER_multihashmap_remove (connections,
3312 /* Cancel queue in the direction that just died. */
3313 connection_cancel_queues (c, ! fwd);
3314 unregister_neighbors (c);
3315 GCC_check_connections ();
3320 * Is this peer the first one on the connection?
3322 * @param c Connection.
3323 * @param fwd Is this about fwd traffic?
3325 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3328 GCC_is_origin (struct CadetConnection *c, int fwd)
3330 if (!fwd && c->path->length - 1 == c->own_pos )
3332 if (fwd && 0 == c->own_pos)
3339 * Is this peer the last one on the connection?
3341 * @param c Connection.
3342 * @param fwd Is this about fwd traffic?
3343 * Note that the ROOT is the terminal for BCK traffic!
3345 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3348 GCC_is_terminal (struct CadetConnection *c, int fwd)
3350 return GCC_is_origin (c, ! fwd);
3355 * See if we are allowed to send by the next hop in the given direction.
3357 * @param c Connection.
3358 * @param fwd Is this about fwd traffic?
3360 * @return #GNUNET_YES in case it's OK to send.
3363 GCC_is_sendable (struct CadetConnection *c, int fwd)
3365 struct CadetFlowControl *fc;
3367 LOG (GNUNET_ERROR_TYPE_DEBUG,
3368 " checking sendability of %s traffic on %s\n",
3369 GC_f2s (fwd), GCC_2s (c));
3375 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3376 LOG (GNUNET_ERROR_TYPE_DEBUG,
3377 " last ack recv: %u, last pid sent: %u\n",
3378 fc->last_ack_recv, fc->last_pid_sent);
3379 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3381 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3384 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3390 * Check if this connection is a direct one (never trim a direct connection).
3392 * @param c Connection.
3394 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3397 GCC_is_direct (struct CadetConnection *c)
3399 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3404 * Sends an already built message on a connection, properly registering
3405 * all used resources.
3407 * @param message Message to send. Function makes a copy of it.
3408 * If message is not hop-by-hop, decrements TTL of copy.
3409 * @param payload_type Type of payload, in case the message is encrypted.
3410 * @param c Connection on which this message is transmitted.
3411 * @param fwd Is this a fwd message?
3412 * @param force Force the connection to accept the message (buffer overfill).
3413 * @param cont Continuation called once message is sent. Can be NULL.
3414 * @param cont_cls Closure for @c cont.
3416 * @return Handle to cancel the message before it's sent.
3417 * NULL on error or if @c cont is NULL.
3418 * Invalid on @c cont call.
3420 struct CadetConnectionQueue *
3421 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3422 uint16_t payload_type, uint32_t payload_id,
3423 struct CadetConnection *c, int fwd, int force,
3424 GCC_sent cont, void *cont_cls)
3426 struct CadetFlowControl *fc;
3427 struct CadetConnectionQueue *q;
3433 GCC_check_connections ();
3434 size = ntohs (message->size);
3435 data = GNUNET_malloc (size);
3436 memcpy (data, message, size);
3437 type = ntohs (message->type);
3438 LOG (GNUNET_ERROR_TYPE_INFO, "--> %s (%s %4u) on connection %s (%u bytes)\n",
3439 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), size);
3441 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3442 droppable = GNUNET_NO == force;
3445 struct GNUNET_CADET_AX *axmsg;
3446 struct GNUNET_CADET_Encrypted *emsg;
3447 struct GNUNET_CADET_KX *kmsg;
3448 struct GNUNET_CADET_ACK *amsg;
3449 struct GNUNET_CADET_Poll *pmsg;
3450 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3451 struct GNUNET_CADET_ConnectionBroken *bmsg;
3454 case GNUNET_MESSAGE_TYPE_CADET_AX:
3455 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3456 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3458 emsg = (struct GNUNET_CADET_Encrypted *) data;
3459 ttl = ntohl (emsg->ttl);
3462 GNUNET_break_op (0);
3467 emsg->ttl = htonl (ttl - 1);
3471 axmsg = (struct GNUNET_CADET_AX *) data;
3474 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3475 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3476 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3477 if (GNUNET_YES == droppable)
3483 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3487 case GNUNET_MESSAGE_TYPE_CADET_KX:
3488 kmsg = (struct GNUNET_CADET_KX *) data;
3492 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3493 amsg = (struct GNUNET_CADET_ACK *) data;
3495 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3496 droppable = GNUNET_NO;
3499 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3500 pmsg = (struct GNUNET_CADET_Poll *) data;
3502 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
3503 droppable = GNUNET_NO;
3506 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3507 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
3511 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3512 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
3516 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3517 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3526 if (fc->queue_n > fc->queue_max && droppable)
3528 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3531 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3532 fc->queue_n, fc->queue_max);
3533 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
3534 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
3539 return NULL; /* Drop this message */
3542 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3543 GCC_2s (c), c->pending_messages);
3544 c->pending_messages++;
3546 q = GNUNET_new (struct CadetConnectionQueue);
3547 q->forced = !droppable;
3548 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3549 size, c, fwd, &conn_message_sent, q);
3552 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3555 GCC_check_connections ();
3559 q->cont_cls = cont_cls;
3560 GCC_check_connections ();
3561 return (NULL == cont) ? NULL : q;
3566 * Cancel a previously sent message while it's in the queue.
3568 * ONLY can be called before the continuation given to the send function
3569 * is called. Once the continuation is called, the message is no longer in the
3572 * @param q Handle to the queue.
3575 GCC_cancel (struct CadetConnectionQueue *q)
3577 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3579 /* queue destroy calls message_sent, which calls q->cont and frees q */
3580 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3581 GCC_check_connections ();
3586 * Sends a CREATE CONNECTION message for a path to a peer.
3587 * Changes the connection and tunnel states if necessary.
3589 * @param connection Connection to create.
3592 GCC_send_create (struct CadetConnection *connection)
3594 enum CadetTunnelCState state;
3597 GCC_check_connections ();
3598 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3599 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3601 LOG (GNUNET_ERROR_TYPE_INFO, "---> %s on connection %s (%u bytes)\n",
3602 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE),
3603 GCC_2s (connection), size);
3604 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3605 connection, connection->pending_messages);
3606 connection->pending_messages++;
3608 connection->maintenance_q =
3609 GCP_queue_add (get_next_hop (connection), NULL,
3610 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 0,
3611 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3613 state = GCT_get_cstate (connection->t);
3614 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3615 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3616 if (CADET_CONNECTION_NEW == connection->state)
3617 connection_change_state (connection, CADET_CONNECTION_SENT);
3618 GCC_check_connections ();
3623 * Send a message to all peers in this connection that the connection
3624 * is no longer valid.
3626 * If some peer should not receive the message, it should be zero'ed out
3627 * before calling this function.
3629 * @param c The connection whose peers to notify.
3632 GCC_send_destroy (struct CadetConnection *c)
3634 struct GNUNET_CADET_ConnectionDestroy msg;
3636 if (GNUNET_YES == c->destroy)
3638 GCC_check_connections ();
3639 msg.header.size = htons (sizeof (msg));
3640 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3642 LOG (GNUNET_ERROR_TYPE_DEBUG,
3643 " sending connection destroy for connection %s\n",
3646 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3647 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3648 GNUNET_YES, GNUNET_YES,
3650 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3651 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3652 GNUNET_NO, GNUNET_YES,
3654 c->destroy = GNUNET_YES;
3655 c->state = CADET_CONNECTION_DESTROYED;
3656 GCC_check_connections ();
3661 * @brief Start a polling timer for the connection.
3663 * When a neighbor does not accept more traffic on the connection it could be
3664 * caused by a simple congestion or by a lost ACK. Polling enables to check
3665 * for the lastest ACK status for a connection.
3667 * @param c Connection.
3668 * @param fwd Should we poll in the FWD direction?
3671 GCC_start_poll (struct CadetConnection *c, int fwd)
3673 struct CadetFlowControl *fc;
3675 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3676 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3678 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3680 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not needed (%p, %p)\n",
3681 fc->poll_task, fc->poll_msg);
3684 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3685 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3692 * @brief Stop polling a connection for ACKs.
3694 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3696 * @param c Connection.
3697 * @param fwd Should we stop the poll in the FWD direction?
3700 GCC_stop_poll (struct CadetConnection *c, int fwd)
3702 struct CadetFlowControl *fc;
3704 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3705 if (NULL != fc->poll_task)
3707 GNUNET_SCHEDULER_cancel (fc->poll_task);
3708 fc->poll_task = NULL;
3714 * Get a (static) string for a connection.
3716 * @param c Connection.
3719 GCC_2s (const struct CadetConnection *c)
3726 static char buf[128];
3728 SPRINTF (buf, "%s (->%s)",
3729 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3732 return GNUNET_h2s (GC_h2hc (&c->id));
3737 * Log all possible info about the connection state.
3739 * @param c Connection to debug.
3740 * @param level Debug level to use.
3743 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3748 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3750 __FILE__, __FUNCTION__, __LINE__);
3756 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3760 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3761 s = path_2s (c->path);
3762 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3764 LOG2 (level, "CCC state: %s, destroy: %u\n",
3765 GCC_state2s (c->state), c->destroy);
3766 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3767 if (NULL != c->perf)
3768 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3770 LOG2 (level, "CCC FWD flow control:\n");
3771 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3772 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3773 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3774 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3775 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3776 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3777 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3778 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3780 LOG2 (level, "CCC BCK flow control:\n");
3781 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3782 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3783 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3784 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3785 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3786 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3787 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3788 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3790 LOG2 (level, "CCC DEBUG CONNECTION END\n");