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 conn */
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 conn %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, " cancelled POLL task for fc %p\n", fc);
1359 if (NULL != fc->poll_msg)
1361 GCC_cancel (fc->poll_msg);
1362 LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL msg for fc %p\n", fc);
1364 peer = get_hop (c, fwd);
1365 GCP_queue_cancel (peer, c);
1366 GCC_check_connections ();
1371 * Function called if a connection has been stalled for a while,
1372 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1374 * @param cls Closure (poll ctx).
1375 * @param tc TaskContext.
1378 connection_poll (void *cls,
1379 const struct GNUNET_SCHEDULER_TaskContext *tc);
1383 * Callback called when a queued POLL message is sent.
1385 * @param cls Closure (FC).
1386 * @param c Connection this message was on.
1387 * @param q Queue handler this call invalidates.
1388 * @param type Type of message sent.
1389 * @param fwd Was this a FWD going message?
1390 * @param size Size of the message.
1393 poll_sent (void *cls,
1394 struct CadetConnection *c,
1395 struct CadetConnectionQueue *q,
1396 uint16_t type, int fwd, size_t size)
1398 struct CadetFlowControl *fc = cls;
1400 if (2 == c->destroy)
1402 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1405 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1407 fc->poll_msg = NULL;
1408 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1409 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1412 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1417 * Function called if a connection has been stalled for a while,
1418 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1420 * @param cls Closure (poll ctx).
1421 * @param tc TaskContext.
1424 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1426 struct CadetFlowControl *fc = cls;
1427 struct GNUNET_CADET_Poll msg;
1428 struct CadetConnection *c;
1431 fc->poll_task = NULL;
1432 GCC_check_connections ();
1433 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1439 fwd = fc == &c->fwd_fc;
1440 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1441 GCC_2s (c), GC_f2s (fwd));
1443 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1444 msg.header.size = htons (sizeof (msg));
1445 msg.pid = htonl (fc->last_pid_sent);
1446 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1448 GCC_send_prebuilt_message (&msg.header, 0, fc->last_pid_sent, c,
1449 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1450 GNUNET_assert (NULL != fc->poll_msg);
1451 GCC_check_connections ();
1456 * Resend all queued messages for a connection on other connections of the
1457 * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
1459 * @param c Connection whose messages to resend.
1460 * @param fwd Resend fwd messages?
1463 resend_messages_and_destroy (struct CadetConnection *c, int fwd)
1465 struct GNUNET_MessageHeader *out_msg;
1466 struct CadetTunnel *t = c->t;
1467 struct CadetPeer *neighbor;
1468 unsigned int pending;
1471 GCC_check_connections ();
1472 c->state = CADET_CONNECTION_DESTROYED;
1473 c->destroy = GNUNET_YES;
1475 destroyed = GNUNET_NO;
1476 neighbor = get_hop (c, fwd);
1477 pending = c->pending_messages;
1479 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1482 GCT_resend_message (out_msg, t);
1483 GNUNET_free (out_msg);
1486 /* All pending messages should have been popped,
1487 * and the connection destroyed by the continuation.
1489 if (GNUNET_YES != destroyed)
1494 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1495 if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1499 GCC_check_connections ();
1504 * Generic connection timeout implementation.
1506 * Timeout function due to lack of keepalive/traffic from an endpoint.
1507 * Destroys connection if called.
1509 * @param c Connection to destroy.
1510 * @param fwd Was the timeout from the origin? (FWD timeout)
1513 connection_timeout (struct CadetConnection *c, int fwd)
1515 struct CadetFlowControl *reverse_fc;
1517 GCC_check_connections ();
1518 reverse_fc = fwd ? &c->bck_fc : &c->fwd_fc;
1520 LOG (GNUNET_ERROR_TYPE_INFO,
1521 "Connection %s %s timed out. Destroying.\n",
1524 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1526 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1532 /* If dest, salvage queued traffic. */
1533 if (GCC_is_origin (c, !fwd))
1535 const struct GNUNET_PeerIdentity *next_hop;
1537 next_hop = GCP_get_id (fwd ? get_prev_hop (c) : get_next_hop (c));
1538 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1539 if (0 < reverse_fc->queue_n)
1540 resend_messages_and_destroy (c, !fwd);
1541 GCC_check_connections ();
1546 GCC_check_connections ();
1551 * Timeout function due to lack of keepalive/traffic from the owner.
1552 * Destroys connection if called.
1554 * @param cls Closure (connection to destroy).
1555 * @param tc TaskContext.
1558 connection_fwd_timeout (void *cls,
1559 const struct GNUNET_SCHEDULER_TaskContext *tc)
1561 struct CadetConnection *c = cls;
1563 c->fwd_maintenance_task = NULL;
1564 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1566 GCC_check_connections ();
1567 connection_timeout (c, GNUNET_YES);
1568 GCC_check_connections ();
1573 * Timeout function due to lack of keepalive/traffic from the destination.
1574 * Destroys connection if called.
1576 * @param cls Closure (connection to destroy).
1577 * @param tc TaskContext
1580 connection_bck_timeout (void *cls,
1581 const struct GNUNET_SCHEDULER_TaskContext *tc)
1583 struct CadetConnection *c = cls;
1585 c->bck_maintenance_task = NULL;
1586 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1588 GCC_check_connections ();
1589 connection_timeout (c, GNUNET_NO);
1590 GCC_check_connections ();
1595 * Resets the connection timeout task, some other message has done the
1597 * - For the first peer on the direction this means to send
1598 * a keepalive or a path confirmation message (either create or ACK).
1599 * - For all other peers, this means to destroy the connection,
1600 * due to lack of activity.
1601 * Starts the timeout if no timeout was running (connection just created).
1603 * @param c Connection whose timeout to reset.
1604 * @param fwd Is this forward?
1606 * TODO use heap to improve efficiency of scheduler.
1609 connection_reset_timeout (struct CadetConnection *c, int fwd)
1611 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1612 if (GCC_is_origin (c, fwd)) /* Startpoint */
1614 schedule_next_keepalive (c, fwd);
1616 else /* Relay, endpoint. */
1618 struct GNUNET_TIME_Relative delay;
1619 struct GNUNET_SCHEDULER_Task * *ti;
1620 GNUNET_SCHEDULER_TaskCallback f;
1622 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1625 GNUNET_SCHEDULER_cancel (*ti);
1626 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1627 LOG (GNUNET_ERROR_TYPE_DEBUG, " timing out in %s\n",
1628 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1629 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1630 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1636 * Iterator to compare each connection's path with the path of a new connection.
1638 * If the connection conincides, the c member of path is set to the connection
1639 * and the destroy flag of the connection is set.
1641 * @param cls Closure (new path).
1642 * @param c Connection in the tunnel to check.
1645 check_path (void *cls, struct CadetConnection *c)
1647 struct CadetConnection *new_conn = cls;
1648 struct CadetPeerPath *path = new_conn->path;
1650 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1651 GCC_2s (c), c, c->path->length);
1654 && c->destroy == GNUNET_NO
1655 && c->state != CADET_CONNECTION_BROKEN
1656 && c->state != CADET_CONNECTION_DESTROYED
1657 && path_equivalent (path, c->path))
1659 new_conn->destroy = GNUNET_YES;
1660 new_conn->path->c = c;
1661 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1667 * Finds out if this path is already being used by an existing connection.
1669 * Checks the tunnel towards the destination to see if it contains
1670 * any connection with the same path.
1672 * If the existing connection is ready, it is kept.
1673 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1674 * the peer will eventually reject our attempt).
1676 * @param path Path to check.
1677 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1678 * #GNUNET_NO otherwise.
1681 does_connection_exist (struct CadetConnection *conn)
1683 struct CadetPeer *p;
1684 struct CadetTunnel *t;
1685 struct CadetConnection *c;
1687 p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
1690 t = GCP_get_tunnel (p);
1694 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1696 GCT_iterate_connections (t, &check_path, conn);
1698 if (GNUNET_YES == conn->destroy)
1701 conn->destroy = GNUNET_NO;
1702 conn->path->c = conn;
1703 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1704 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1705 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1706 if (CADET_CONNECTION_READY == c->state)
1708 /* The other peer confirmed a live connection with this path,
1709 * why is it trying to duplicate it. */
1710 GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
1713 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not valid, connection unique\n");
1718 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1725 * @brief Check if the tunnel this connection belongs to has any other
1726 * connection with the same path, and destroy one if so.
1728 * @param cls Closure (connection to check).
1729 * @param tc Task context.
1732 check_duplicates (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1734 struct CadetConnection *c = cls;
1736 c->check_duplicates_task = NULL;
1737 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1740 if (GNUNET_YES == does_connection_exist (c))
1742 GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1743 send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
1751 * Wait for enough time to let any dead connections time out and check for
1752 * any remaining duplicates.
1754 * @param c Connection that is a potential duplicate.
1757 schedule_check_duplicates (struct CadetConnection *c)
1759 struct GNUNET_TIME_Relative delay;
1761 if (NULL != c->check_duplicates_task)
1764 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 5);
1765 c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
1773 * Add the connection to the list of both neighbors.
1775 * @param c Connection.
1777 * @return #GNUNET_OK if everything went fine
1778 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1781 register_neighbors (struct CadetConnection *c)
1783 c->next_peer = get_next_hop (c);
1784 c->prev_peer = get_prev_hop (c);
1785 GNUNET_assert (c->next_peer != c->prev_peer);
1786 LOG (GNUNET_ERROR_TYPE_DEBUG,
1787 "register neighbors for connection %s\n",
1789 path_debug (c->path);
1790 LOG (GNUNET_ERROR_TYPE_DEBUG,
1791 "own pos %u\n", c->own_pos);
1792 LOG (GNUNET_ERROR_TYPE_DEBUG,
1793 "putting connection %s to next peer %p\n",
1796 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1798 GCP_2s (c->next_peer));
1799 LOG (GNUNET_ERROR_TYPE_DEBUG,
1800 "putting connection %s to prev peer %p\n",
1803 LOG (GNUNET_ERROR_TYPE_DEBUG,
1804 "prev peer %p %s\n",
1806 GCP_2s (c->prev_peer));
1808 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1809 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1811 if (GCC_is_origin (c, GNUNET_YES))
1812 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1813 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1815 LOG (GNUNET_ERROR_TYPE_DEBUG,
1816 " register neighbors failed\n");
1817 LOG (GNUNET_ERROR_TYPE_DEBUG,
1818 " prev: %s, neighbor?: %d\n",
1819 GCP_2s (c->prev_peer),
1820 GCP_is_neighbor (c->prev_peer));
1821 LOG (GNUNET_ERROR_TYPE_DEBUG,
1822 " next: %s, neighbor?: %d\n",
1823 GCP_2s (c->next_peer),
1824 GCP_is_neighbor (c->next_peer));
1825 return GNUNET_SYSERR;
1827 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1828 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1835 * Remove the connection from the list of both neighbors.
1837 * @param c Connection.
1840 unregister_neighbors (struct CadetConnection *c)
1842 // struct CadetPeer *peer; FIXME dont use next_peer, prev_peer
1843 /* Either already unregistered or never got registered, it's ok either way. */
1844 if (NULL == c->path)
1846 if (NULL != c->next_peer)
1848 GCP_remove_connection (c->next_peer, c);
1849 c->next_peer = NULL;
1851 if (NULL != c->prev_peer)
1853 GCP_remove_connection (c->prev_peer, c);
1854 c->prev_peer = NULL;
1860 * Invalidates all paths towards all peers that comprise the connection which
1861 * rely on the disconnected peer.
1863 * ~O(n^3) (peers in connection * paths/peer * links/path)
1865 * @param c Connection whose peers' paths to clean.
1866 * @param disconnected Peer that disconnected.
1869 invalidate_paths (struct CadetConnection *c, struct CadetPeer *disconnected)
1871 struct CadetPeer *peer;
1874 for (i = 0; i < c->path->length; i++)
1876 peer = GCP_get_short (c->path->peers[i], GNUNET_NO);
1878 GCP_notify_broken_link (peer, &my_full_id, GCP_get_id (disconnected));
1884 * Bind the connection to the peer and the tunnel to that peer.
1886 * If the peer has no tunnel, create one. Update tunnel and connection
1887 * data structres to reflect new status.
1889 * @param c Connection.
1893 add_to_peer (struct CadetConnection *c,
1894 struct CadetPeer *peer)
1896 GCP_add_tunnel (peer);
1897 c->t = GCP_get_tunnel (peer);
1898 GCT_add_connection (c->t, c);
1903 * Log receipt of message on stderr (INFO level).
1905 * @param message Message received.
1906 * @param peer Peer who sent the message.
1907 * @param hash Connection ID.
1910 log_message (const struct GNUNET_MessageHeader *message,
1911 const struct GNUNET_PeerIdentity *peer,
1912 const struct GNUNET_CADET_Hash *hash)
1916 size = ntohs (message->size);
1917 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1918 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1919 LOG (GNUNET_ERROR_TYPE_INFO, "<-- %s on conn %s from %s, %6u bytes\n",
1920 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (hash)),
1921 GNUNET_i2s (peer), (unsigned int) size);
1924 /******************************************************************************/
1925 /******************************** API ***********************************/
1926 /******************************************************************************/
1929 * Core handler for connection creation.
1931 * @param cls Closure (unused).
1932 * @param peer Sender (neighbor).
1933 * @param message Message.
1934 * @return #GNUNET_OK to keep the connection open,
1935 * #GNUNET_SYSERR to close it (signal serious error)
1938 GCC_handle_create (void *cls,
1939 const struct GNUNET_PeerIdentity *peer,
1940 const struct GNUNET_MessageHeader *message)
1942 struct GNUNET_CADET_ConnectionCreate *msg;
1943 struct GNUNET_PeerIdentity *id;
1944 struct GNUNET_CADET_Hash *cid;
1945 struct CadetPeerPath *path;
1946 struct CadetPeer *dest_peer;
1947 struct CadetPeer *orig_peer;
1948 struct CadetConnection *c;
1949 unsigned int own_pos;
1952 GCC_check_connections ();
1954 size = ntohs (message->size);
1955 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1957 GNUNET_break_op (0);
1961 /* Calculate hops */
1962 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1963 if (size % sizeof (struct GNUNET_PeerIdentity))
1965 GNUNET_break_op (0);
1968 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1970 GNUNET_break_op (0);
1973 size /= sizeof (struct GNUNET_PeerIdentity);
1976 GNUNET_break_op (0);
1979 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1981 /* Get parameters */
1982 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
1984 log_message (message, peer, cid);
1985 id = (struct GNUNET_PeerIdentity *) &msg[1];
1986 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1988 /* Create connection */
1989 c = connection_get (cid);
1992 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1993 size, myid, &own_pos);
1996 /* Path was malformed, probably our own ID was not in it. */
1997 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
1998 GNUNET_break_op (0);
2004 /* We received this request from a neighbor, we cannot be origin */
2005 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
2006 GNUNET_break_op (0);
2007 path_destroy (path);
2011 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2012 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
2013 c = GCC_new (cid, NULL, path, own_pos);
2016 if (path->length - 1 == own_pos)
2018 /* If we are destination, why did the creation fail? */
2020 path_destroy (path);
2021 GCC_check_connections ();
2024 send_broken_unknown (cid, &my_full_id,
2025 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
2027 path_destroy (path);
2028 GCC_check_connections ();
2031 GCP_add_path_to_all (path, GNUNET_NO);
2032 connection_reset_timeout (c, GNUNET_YES);
2036 path = path_duplicate (c->path);
2038 if (CADET_CONNECTION_NEW == c->state)
2039 connection_change_state (c, CADET_CONNECTION_SENT);
2041 /* Remember peers */
2042 dest_peer = GCP_get (&id[size - 1], GNUNET_YES);
2043 orig_peer = GCP_get (&id[0], GNUNET_YES);
2045 /* Is it a connection to us? */
2046 if (c->own_pos == path->length - 1)
2048 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2049 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
2051 add_to_peer (c, orig_peer);
2052 if (GNUNET_YES == does_connection_exist (c))
2054 /* Peer created a connection equal to one we think exists
2056 * Solution: Keep both and postpone disambiguation. In the meantime
2057 * the connection will time out or peer will inform us it is broken.
2060 * - Use explicit duplicate.
2061 * - Accept new conn and destroy the old. (interruption in higher level)
2062 * - Keep the one with higher ID / created by peer with higher ID. */
2063 schedule_check_duplicates (c);
2066 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
2067 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
2069 send_connection_ack (c, GNUNET_NO);
2070 if (CADET_CONNECTION_SENT == c->state)
2071 connection_change_state (c, CADET_CONNECTION_ACK);
2075 /* It's for somebody else! Retransmit. */
2076 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2077 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2078 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2079 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
2080 GNUNET_YES, GNUNET_YES,
2083 path_destroy (path);
2084 GCC_check_connections ();
2090 * Core handler for path confirmations.
2092 * @param cls closure
2093 * @param message message
2094 * @param peer peer identity this notification is about
2095 * @return #GNUNET_OK to keep the connection open,
2096 * #GNUNET_SYSERR to close it (signal serious error)
2099 GCC_handle_confirm (void *cls,
2100 const struct GNUNET_PeerIdentity *peer,
2101 const struct GNUNET_MessageHeader *message)
2103 struct GNUNET_CADET_ConnectionACK *msg;
2104 struct CadetConnection *c;
2105 struct CadetPeerPath *p;
2106 struct CadetPeer *pi;
2107 enum CadetConnectionState oldstate;
2110 GCC_check_connections ();
2111 msg = (struct GNUNET_CADET_ConnectionACK *) message;
2112 log_message (message, peer, &msg->cid);
2113 c = connection_get (&msg->cid);
2116 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2118 LOG (GNUNET_ERROR_TYPE_DEBUG,
2119 " don't know the connection!\n");
2120 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2121 GCC_check_connections ();
2125 oldstate = c->state;
2126 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
2127 pi = GCP_get (peer, GNUNET_YES);
2128 if (get_next_hop (c) == pi)
2130 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2132 if (CADET_CONNECTION_SENT == oldstate)
2133 connection_change_state (c, CADET_CONNECTION_ACK);
2135 else if (get_prev_hop (c) == pi)
2137 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2139 connection_change_state (c, CADET_CONNECTION_READY);
2143 GNUNET_break_op (0);
2147 connection_reset_timeout (c, fwd);
2149 /* Add path to peers? */
2153 GCP_add_path_to_all (p, GNUNET_YES);
2160 /* Message for us as creator? */
2161 if (GCC_is_origin (c, GNUNET_YES))
2163 if (GNUNET_NO != fwd)
2165 GNUNET_break_op (0);
2168 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2170 /* If just created, cancel the short timeout and start a long one */
2171 if (CADET_CONNECTION_SENT == oldstate)
2172 connection_reset_timeout (c, GNUNET_YES);
2174 /* Change connection state */
2175 connection_change_state (c, CADET_CONNECTION_READY);
2176 send_connection_ack (c, GNUNET_YES);
2178 /* Change tunnel state, trigger KX */
2179 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2180 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2181 GCC_check_connections ();
2185 /* Message for us as destination? */
2186 if (GCC_is_terminal (c, GNUNET_YES))
2188 if (GNUNET_YES != fwd)
2190 GNUNET_break_op (0);
2193 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2195 /* If just created, cancel the short timeout and start a long one */
2196 if (CADET_CONNECTION_ACK == oldstate)
2197 connection_reset_timeout (c, GNUNET_NO);
2199 /* Change tunnel state */
2200 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2201 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2202 GCC_check_connections ();
2206 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2207 GNUNET_assert (NULL ==
2208 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2209 GNUNET_YES, NULL, NULL));
2210 GCC_check_connections ();
2216 * Core handler for notifications of broken connections.
2218 * @param cls Closure (unused).
2219 * @param id Peer identity of sending neighbor.
2220 * @param message Message.
2221 * @return #GNUNET_OK to keep the connection open,
2222 * #GNUNET_SYSERR to close it (signal serious error)
2225 GCC_handle_broken (void* cls,
2226 const struct GNUNET_PeerIdentity* id,
2227 const struct GNUNET_MessageHeader* message)
2229 struct GNUNET_CADET_ConnectionBroken *msg;
2230 struct CadetConnection *c;
2231 struct CadetTunnel *t;
2235 GCC_check_connections ();
2236 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
2237 log_message (message, id, &msg->cid);
2238 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2239 GNUNET_i2s (&msg->peer1));
2240 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2241 GNUNET_i2s (&msg->peer2));
2242 c = connection_get (&msg->cid);
2245 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2246 GCC_check_connections ();
2252 fwd = is_fwd (c, id);
2253 c->destroy = GNUNET_YES;
2254 if (GCC_is_terminal (c, fwd))
2256 struct CadetPeer *endpoint;
2260 /* A terminal connection should not have 't' set to NULL. */
2262 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2265 endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
2266 if (2 < c->path->length)
2267 path_invalidate (c->path);
2268 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2270 c->state = CADET_CONNECTION_BROKEN;
2271 GCT_remove_connection (t, c);
2274 pending = c->pending_messages;
2276 resend_messages_and_destroy (c, !fwd);
2282 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2283 GNUNET_YES, NULL, NULL));
2284 connection_cancel_queues (c, !fwd);
2286 GCC_check_connections ();
2292 * Core handler for tunnel destruction
2294 * @param cls Closure (unused).
2295 * @param peer Peer identity of sending neighbor.
2296 * @param message Message.
2297 * @return #GNUNET_OK to keep the connection open,
2298 * #GNUNET_SYSERR to close it (signal serious error)
2301 GCC_handle_destroy (void *cls,
2302 const struct GNUNET_PeerIdentity *peer,
2303 const struct GNUNET_MessageHeader *message)
2305 const struct GNUNET_CADET_ConnectionDestroy *msg;
2306 struct CadetConnection *c;
2309 GCC_check_connections ();
2310 msg = (const struct GNUNET_CADET_ConnectionDestroy *) message;
2311 log_message (message, peer, &msg->cid);
2312 c = connection_get (&msg->cid);
2315 /* Probably already got the message from another path,
2316 * destroyed the tunnel and retransmitted to children.
2319 GNUNET_STATISTICS_update (stats,
2320 "# control on unknown connection",
2322 LOG (GNUNET_ERROR_TYPE_DEBUG,
2323 " connection unknown: already destroyed?\n");
2324 GCC_check_connections ();
2327 fwd = is_fwd (c, peer);
2328 if (GNUNET_SYSERR == fwd)
2330 GNUNET_break_op (0); /* FIXME */
2333 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2335 GNUNET_assert (NULL ==
2336 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2337 GNUNET_YES, NULL, NULL));
2339 else if (0 == c->pending_messages)
2341 LOG (GNUNET_ERROR_TYPE_DEBUG,
2342 " directly destroying connection!\n");
2344 GCC_check_connections ();
2347 c->destroy = GNUNET_YES;
2348 c->state = CADET_CONNECTION_DESTROYED;
2351 GCT_remove_connection (c->t, c);
2354 GCC_check_connections ();
2360 * Check the message against internal state and test if it goes FWD or BCK.
2362 * Updates the PID, state and timeout values for the connection.
2364 * @param message Message to check. It must belong to an existing connection.
2365 * @param minimum_size The message cannot be smaller than this value.
2366 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2367 * @param c Connection this message should belong. If NULL, check fails.
2368 * @param neighbor Neighbor that sent the message.
2371 check_message (const struct GNUNET_MessageHeader *message,
2372 size_t minimum_size,
2373 const struct GNUNET_CADET_Hash* cid,
2374 struct CadetConnection *c,
2375 const struct GNUNET_PeerIdentity *neighbor,
2378 GNUNET_PEER_Id neighbor_id;
2379 struct CadetFlowControl *fc;
2380 struct CadetPeer *hop;
2385 if (ntohs (message->size) < minimum_size)
2387 GNUNET_break_op (0);
2388 LOG (GNUNET_ERROR_TYPE_WARNING, "Size %u < %u\n",
2389 ntohs (message->size), minimum_size);
2390 return GNUNET_SYSERR;
2393 /* Check connection */
2396 GNUNET_STATISTICS_update (stats,
2397 "# unknown connection",
2399 LOG (GNUNET_ERROR_TYPE_DEBUG,
2400 "%s on unknown connection %s\n",
2401 GC_m2s (ntohs (message->type)),
2402 GNUNET_h2s (GC_h2hc (cid)));
2403 send_broken_unknown (cid,
2407 return GNUNET_SYSERR;
2410 /* Check if origin is as expected */
2411 neighbor_id = GNUNET_PEER_search (neighbor);
2412 hop = get_prev_hop (c);
2413 if (neighbor_id == GCP_get_short_id (hop))
2419 hop = get_next_hop (c);
2420 GNUNET_break (hop == c->next_peer);
2421 if (neighbor_id == GCP_get_short_id (hop))
2427 /* Unexpected peer sending traffic on a connection. */
2428 GNUNET_break_op (0);
2429 return GNUNET_SYSERR;
2433 /* Check PID for payload messages */
2434 type = ntohs (message->type);
2435 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
2436 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
2438 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2439 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2440 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2441 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2443 GNUNET_break_op (0);
2444 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2445 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2446 pid, fc->last_pid_recv, fc->last_ack_sent);
2447 return GNUNET_SYSERR;
2449 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2453 delta = pid - fc->last_pid_recv;
2454 fc->last_pid_recv = pid;
2455 fc->recv_bitmap <<= delta;
2456 fc->recv_bitmap |= 1;
2460 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2461 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2463 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2464 pid, fc->last_pid_recv - 31);
2465 return GNUNET_SYSERR;
2467 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2471 /* Count as connection confirmation. */
2472 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2474 connection_change_state (c, CADET_CONNECTION_READY);
2477 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2478 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2481 connection_reset_timeout (c, fwd);
2488 * Generic handler for cadet network encrypted traffic.
2490 * @param peer Peer identity this notification is about.
2491 * @param msg Encrypted message.
2492 * @return #GNUNET_OK to keep the connection open,
2493 * #GNUNET_SYSERR to close it (signal serious error)
2496 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
2497 const struct GNUNET_MessageHeader *message)
2499 const struct GNUNET_CADET_Encrypted *otr_msg;
2500 const struct GNUNET_CADET_AX *ax_msg;
2501 const struct GNUNET_CADET_Hash* cid;
2502 struct CadetConnection *c;
2503 size_t minumum_size;
2509 GCC_check_connections ();
2510 if (GNUNET_MESSAGE_TYPE_CADET_AX == ntohs (message->type))
2512 overhead = sizeof (struct GNUNET_CADET_AX);
2513 ax_msg = (const struct GNUNET_CADET_AX *) message;
2515 pid = ntohl (ax_msg->pid);
2520 overhead = sizeof (struct GNUNET_CADET_Encrypted);
2521 otr_msg = (const struct GNUNET_CADET_Encrypted *) message;
2522 cid = &otr_msg->cid;
2523 pid = ntohl (otr_msg->pid);
2526 log_message (message, peer, cid);
2528 minumum_size = sizeof (struct GNUNET_MessageHeader) + overhead;
2529 c = connection_get (cid);
2530 fwd = check_message (message,
2537 /* If something went wrong, discard message. */
2538 if (GNUNET_SYSERR == fwd)
2540 GCC_check_connections ();
2544 /* Is this message for us? */
2545 if (GCC_is_terminal (c, fwd))
2547 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2551 GNUNET_break (GNUNET_NO != c->destroy);
2554 GCT_handle_encrypted (c->t, message);
2555 GCC_send_ack (c, fwd, GNUNET_NO);
2556 GCC_check_connections ();
2560 /* Message not for us: forward to next hop */
2561 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2562 if (NULL != otr_msg) /* only otr has ttl */
2564 ttl = ntohl (otr_msg->ttl);
2565 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2568 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2569 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2570 GCC_send_ack (c, fwd, GNUNET_NO);
2571 GCC_check_connections ();
2576 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2577 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2578 GNUNET_NO, NULL, NULL));
2579 GCC_check_connections ();
2585 * Generic handler for cadet network encrypted traffic.
2587 * @param peer Peer identity this notification is about.
2588 * @param msg Encrypted message.
2589 * @return #GNUNET_OK to keep the connection open,
2590 * #GNUNET_SYSERR to close it (signal serious error)
2593 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2594 const struct GNUNET_CADET_KX *msg)
2596 const struct GNUNET_CADET_Hash* cid;
2597 struct CadetConnection *c;
2598 size_t expected_size;
2601 GCC_check_connections ();
2603 log_message (&msg->header, peer, cid);
2605 expected_size = sizeof (struct GNUNET_CADET_KX)
2606 + sizeof (struct GNUNET_MessageHeader);
2607 c = connection_get (cid);
2608 fwd = check_message (&msg->header,
2615 /* If something went wrong, discard message. */
2616 if (GNUNET_SYSERR == fwd)
2619 /* Is this message for us? */
2620 if (GCC_is_terminal (c, fwd))
2622 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2623 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2629 GCT_handle_kx (c->t, &msg[1].header);
2630 GCC_check_connections ();
2634 /* Message not for us: forward to next hop */
2635 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2636 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2637 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2638 GNUNET_NO, NULL, NULL));
2639 GCC_check_connections ();
2645 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2647 * @param cls Closure (unused).
2648 * @param message Message received.
2649 * @param peer Peer who sent the message.
2650 * @return #GNUNET_OK to keep the connection open,
2651 * #GNUNET_SYSERR to close it (signal serious error)
2654 GCC_handle_kx (void *cls,
2655 const struct GNUNET_PeerIdentity *peer,
2656 const struct GNUNET_MessageHeader *message)
2658 GCC_check_connections ();
2659 return handle_cadet_kx (peer, (struct GNUNET_CADET_KX *) message);
2664 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2666 * @param cls Closure (unused).
2667 * @param message Message received.
2668 * @param peer Peer who sent the message.
2669 * @return #GNUNET_OK to keep the connection open,
2670 * #GNUNET_SYSERR to close it (signal serious error)
2673 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2674 const struct GNUNET_MessageHeader *message)
2676 GCC_check_connections ();
2677 return handle_cadet_encrypted (peer, message);
2682 * Core handler for cadet network traffic point-to-point acks.
2684 * @param cls closure
2685 * @param message message
2686 * @param peer peer identity this notification is about
2687 * @return #GNUNET_OK to keep the connection open,
2688 * #GNUNET_SYSERR to close it (signal serious error)
2691 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2692 const struct GNUNET_MessageHeader *message)
2694 struct GNUNET_CADET_ACK *msg;
2695 struct CadetConnection *c;
2696 struct CadetFlowControl *fc;
2701 GCC_check_connections ();
2702 msg = (struct GNUNET_CADET_ACK *) message;
2703 log_message (message, peer, &msg->cid);
2704 c = connection_get (&msg->cid);
2707 GNUNET_STATISTICS_update (stats,
2708 "# ack on unknown connection",
2711 send_broken_unknown (&msg->cid,
2715 GCC_check_connections ();
2719 /* Is this a forward or backward ACK? */
2720 id = GNUNET_PEER_search (peer);
2721 if (GCP_get_short_id (get_next_hop (c)) == id)
2726 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2733 GNUNET_break_op (0);
2737 ack = ntohl (msg->ack);
2738 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2739 GC_f2s (fwd), ack, fc->last_ack_recv);
2740 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2741 fc->last_ack_recv = ack;
2743 /* Cancel polling if the ACK is big enough. */
2744 if (NULL != fc->poll_task &&
2745 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2747 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2748 GNUNET_SCHEDULER_cancel (fc->poll_task);
2749 fc->poll_task = NULL;
2750 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2753 connection_unlock_queue (c, fwd);
2754 GCC_check_connections ();
2760 * Core handler for cadet network traffic point-to-point ack polls.
2762 * @param cls closure
2763 * @param message message
2764 * @param peer peer identity this notification is about
2765 * @return #GNUNET_OK to keep the connection open,
2766 * #GNUNET_SYSERR to close it (signal serious error)
2769 GCC_handle_poll (void *cls,
2770 const struct GNUNET_PeerIdentity *peer,
2771 const struct GNUNET_MessageHeader *message)
2773 struct GNUNET_CADET_Poll *msg;
2774 struct CadetConnection *c;
2775 struct CadetFlowControl *fc;
2780 GCC_check_connections ();
2781 msg = (struct GNUNET_CADET_Poll *) message;
2782 log_message (message, peer, &msg->cid);
2783 c = connection_get (&msg->cid);
2786 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2788 LOG (GNUNET_ERROR_TYPE_DEBUG,
2789 "POLL message on unknown connection %s!\n",
2790 GNUNET_h2s (GC_h2hc (&msg->cid)));
2791 send_broken_unknown (&msg->cid,
2795 GCC_check_connections ();
2799 /* Is this a forward or backward ACK?
2800 * Note: a poll should never be needed in a loopback case,
2801 * since there is no possiblility of packet loss there, so
2802 * this way of discerining FWD/BCK should not be a problem.
2804 id = GNUNET_PEER_search (peer);
2805 if (GCP_get_short_id (get_next_hop (c)) == id)
2807 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2810 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2812 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2817 GNUNET_break_op (0);
2821 pid = ntohl (msg->pid);
2822 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2823 fc->last_pid_recv = pid;
2824 fwd = fc == &c->bck_fc;
2825 GCC_send_ack (c, fwd, GNUNET_YES);
2826 GCC_check_connections ();
2833 * Send an ACK on the appropriate connection/channel, depending on
2834 * the direction and the position of the peer.
2836 * @param c Which connection to send the hop-by-hop ACK.
2837 * @param fwd Is this a fwd ACK? (will go dest->root).
2838 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2841 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2843 unsigned int buffer;
2845 GCC_check_connections ();
2846 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
2847 GC_f2s (fwd), GCC_2s (c));
2855 if (GNUNET_NO != c->destroy)
2857 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2858 GCC_check_connections ();
2862 /* Get available buffer space */
2863 if (GCC_is_terminal (c, fwd))
2865 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2866 buffer = GCT_get_channels_buffer (c->t);
2870 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2871 buffer = GCC_get_buffer (c, fwd);
2873 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2874 if (0 == buffer && GNUNET_NO == force)
2876 GCC_check_connections ();
2880 /* Send available buffer space */
2881 if (GCC_is_origin (c, fwd))
2883 GNUNET_assert (NULL != c->t);
2884 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2885 GCT_unchoke_channels (c->t);
2889 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2890 send_ack (c, buffer, fwd, force);
2892 GCC_check_connections ();
2897 * Initialize the connections subsystem
2899 * @param c Configuration handle.
2902 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2904 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2906 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2909 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2910 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2911 GNUNET_SCHEDULER_shutdown ();
2916 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2919 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2920 "CADET", "MAX_CONNECTIONS", "MISSING");
2921 GNUNET_SCHEDULER_shutdown ();
2926 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2927 &refresh_connection_time))
2929 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2930 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2931 GNUNET_SCHEDULER_shutdown ();
2934 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2935 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2940 * Destroy each connection on shutdown.
2942 * @param cls Closure (unused).
2943 * @param key Current key code (CID, unused).
2944 * @param value Value in the hash map (`struct CadetConnection`)
2946 * @return #GNUNET_YES, because we should continue to iterate
2949 shutdown_iterator (void *cls,
2950 const struct GNUNET_HashCode *key,
2953 struct CadetConnection *c = value;
2955 c->state = CADET_CONNECTION_DESTROYED;
2962 * Shut down the connections subsystem.
2967 GCC_check_connections ();
2968 GNUNET_CONTAINER_multihashmap_iterate (connections,
2971 GNUNET_CONTAINER_multihashmap_destroy (connections);
2977 * Create a connection.
2979 * @param cid Connection ID (either created locally or imposed remotely).
2980 * @param t Tunnel this connection belongs to (or NULL);
2981 * @param path Path this connection has to use (copy is made).
2982 * @param own_pos Own position in the @c path path.
2984 * @return Newly created connection, NULL in case of error (own id not in path).
2986 struct CadetConnection *
2987 GCC_new (const struct GNUNET_CADET_Hash *cid,
2988 struct CadetTunnel *t,
2989 struct CadetPeerPath *path,
2990 unsigned int own_pos)
2992 struct CadetConnection *c;
2993 struct CadetPeerPath *cpath;
2995 GCC_check_connections ();
2996 cpath = path_duplicate (path);
2997 GNUNET_assert (NULL != cpath);
2998 c = GNUNET_new (struct CadetConnection);
3000 GNUNET_assert (GNUNET_OK ==
3001 GNUNET_CONTAINER_multihashmap_put (connections,
3003 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3004 fc_init (&c->fwd_fc);
3005 fc_init (&c->bck_fc);
3010 GNUNET_assert (own_pos <= cpath->length - 1);
3011 c->own_pos = own_pos;
3014 if (GNUNET_OK != register_neighbors (c))
3018 /* We were the origin of this request, this means we have invalid
3019 * info about the paths to reach the destination. We must invalidate
3020 * the *original* path to avoid trying it again in the next minute.
3022 if (2 < path->length)
3023 path_invalidate (path);
3027 GCT_debug(t, GNUNET_ERROR_TYPE_WARNING);
3031 path_destroy (c->path);
3036 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
3037 GCC_check_connections ();
3043 GCC_destroy (struct CadetConnection *c)
3045 GCC_check_connections ();
3052 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
3053 return; /* -> message_sent -> GCC_destroy. Don't loop. */
3056 LOG (GNUNET_ERROR_TYPE_DEBUG,
3057 "destroying connection %s\n",
3059 LOG (GNUNET_ERROR_TYPE_DEBUG,
3060 " fc's f: %p, b: %p\n",
3061 &c->fwd_fc, &c->bck_fc);
3062 LOG (GNUNET_ERROR_TYPE_DEBUG,
3063 " fc tasks f: %u, b: %u\n",
3064 c->fwd_fc.poll_task,
3065 c->bck_fc.poll_task);
3067 /* Cancel all traffic */
3068 if (NULL != c->path)
3070 connection_cancel_queues (c, GNUNET_YES);
3071 connection_cancel_queues (c, GNUNET_NO);
3073 unregister_neighbors (c);
3074 path_destroy (c->path);
3077 /* Delete from tunnel */
3079 GCT_remove_connection (c->t, c);
3081 if (NULL != c->check_duplicates_task)
3082 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
3083 if (NULL != c->fwd_maintenance_task)
3084 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
3085 if (NULL != c->bck_maintenance_task)
3086 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
3088 if (GNUNET_NO == c->was_removed)
3090 GNUNET_break (GNUNET_YES ==
3091 GNUNET_CONTAINER_multihashmap_remove (connections,
3095 GNUNET_STATISTICS_update (stats,
3100 GCC_check_connections ();
3105 * Get the connection ID.
3107 * @param c Connection to get the ID from.
3109 * @return ID of the connection.
3111 const struct GNUNET_CADET_Hash *
3112 GCC_get_id (const struct CadetConnection *c)
3119 * Get the connection ID.
3121 * @param c Connection to get the ID from.
3123 * @return ID of the connection.
3125 const struct GNUNET_HashCode *
3126 GCC_get_h (const struct CadetConnection *c)
3128 return GC_h2hc (&c->id);
3133 * Get the connection path.
3135 * @param c Connection to get the path from.
3137 * @return path used by the connection.
3139 const struct CadetPeerPath *
3140 GCC_get_path (const struct CadetConnection *c)
3142 if (GNUNET_NO == c->destroy)
3149 * Get the connection state.
3151 * @param c Connection to get the state from.
3153 * @return state of the connection.
3155 enum CadetConnectionState
3156 GCC_get_state (const struct CadetConnection *c)
3162 * Get the connection tunnel.
3164 * @param c Connection to get the tunnel from.
3166 * @return tunnel of the connection.
3168 struct CadetTunnel *
3169 GCC_get_tunnel (const struct CadetConnection *c)
3176 * Get free buffer space in a connection.
3178 * @param c Connection.
3179 * @param fwd Is query about FWD traffic?
3181 * @return Free buffer space [0 - max_msgs_queue/max_connections]
3184 GCC_get_buffer (struct CadetConnection *c, int fwd)
3186 struct CadetFlowControl *fc;
3188 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3190 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
3191 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
3192 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
3194 return (fc->queue_max - fc->queue_n);
3199 * Get how many messages have we allowed to send to us from a direction.
3201 * @param c Connection.
3202 * @param fwd Are we asking about traffic from FWD (BCK messages)?
3204 * @return last_ack_sent - last_pid_recv
3207 GCC_get_allowed (struct CadetConnection *c, int fwd)
3209 struct CadetFlowControl *fc;
3211 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3212 if (CADET_CONNECTION_READY != c->state
3213 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
3217 return (fc->last_ack_sent - fc->last_pid_recv);
3222 * Get messages queued in a connection.
3224 * @param c Connection.
3225 * @param fwd Is query about FWD traffic?
3227 * @return Number of messages queued.
3230 GCC_get_qn (struct CadetConnection *c, int fwd)
3232 struct CadetFlowControl *fc;
3234 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3241 * Get next PID to use.
3243 * @param c Connection.
3244 * @param fwd Is query about FWD traffic?
3246 * @return Last PID used + 1.
3249 GCC_get_pid (struct CadetConnection *c, int fwd)
3251 struct CadetFlowControl *fc;
3253 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3255 return fc->last_pid_sent + 1;
3260 * Allow the connection to advertise a buffer of the given size.
3262 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3263 * allowing up to last_pid_recv + buffer.
3265 * @param c Connection.
3266 * @param buffer How many more messages the connection can accept.
3267 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3270 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3272 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3273 GCC_2s (c), buffer, GC_f2s (fwd));
3274 send_ack (c, buffer, fwd, GNUNET_NO);
3279 * Notify other peers on a connection of a broken link. Mark connections
3280 * to destroy after all traffic has been sent.
3282 * @param c Connection on which there has been a disconnection.
3283 * @param peer Peer that disconnected.
3286 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
3288 struct CadetPeer *hop;
3292 GCC_check_connections ();
3293 strncpy (peer_name, GCP_2s (peer), 16);
3294 peer_name[15] = '\0';
3295 LOG (GNUNET_ERROR_TYPE_DEBUG,
3296 "shutting down %s, %s disconnected\n",
3297 GCC_2s (c), peer_name);
3299 invalidate_paths (c, peer);
3301 hop = get_prev_hop (c);
3304 /* Path was NULL, we should have deleted the connection. */
3308 fwd = (peer == hop);
3309 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3310 (GNUNET_NO != c->destroy) )
3312 /* Local shutdown, or other peer already down (hence 'c->destroy');
3313 so there is no one to notify about this, just clean up. */
3315 GCC_check_connections ();
3318 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3319 /* Connection will have at least one pending message
3320 * (the one we just scheduled), so delay destruction
3321 * and remove from map so we don't use accidentally. */
3322 c->destroy = GNUNET_YES;
3323 c->state = CADET_CONNECTION_DESTROYED;
3324 GNUNET_assert (GNUNET_NO == c->was_removed);
3325 c->was_removed = GNUNET_YES;
3326 GNUNET_break (GNUNET_YES ==
3327 GNUNET_CONTAINER_multihashmap_remove (connections,
3330 /* Cancel queue in the direction that just died. */
3331 connection_cancel_queues (c, ! fwd);
3332 GCC_stop_poll (c, ! fwd);
3333 unregister_neighbors (c);
3334 GCC_check_connections ();
3339 * Is this peer the first one on the connection?
3341 * @param c Connection.
3342 * @param fwd Is this about fwd traffic?
3344 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3347 GCC_is_origin (struct CadetConnection *c, int fwd)
3349 if (!fwd && c->path->length - 1 == c->own_pos )
3351 if (fwd && 0 == c->own_pos)
3358 * Is this peer the last one on the connection?
3360 * @param c Connection.
3361 * @param fwd Is this about fwd traffic?
3362 * Note that the ROOT is the terminal for BCK traffic!
3364 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3367 GCC_is_terminal (struct CadetConnection *c, int fwd)
3369 return GCC_is_origin (c, ! fwd);
3374 * See if we are allowed to send by the next hop in the given direction.
3376 * @param c Connection.
3377 * @param fwd Is this about fwd traffic?
3379 * @return #GNUNET_YES in case it's OK to send.
3382 GCC_is_sendable (struct CadetConnection *c, int fwd)
3384 struct CadetFlowControl *fc;
3386 LOG (GNUNET_ERROR_TYPE_DEBUG,
3387 " checking sendability of %s traffic on %s\n",
3388 GC_f2s (fwd), GCC_2s (c));
3394 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3395 LOG (GNUNET_ERROR_TYPE_DEBUG,
3396 " last ack recv: %u, last pid sent: %u\n",
3397 fc->last_ack_recv, fc->last_pid_sent);
3398 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3400 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3403 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3409 * Check if this connection is a direct one (never trim a direct connection).
3411 * @param c Connection.
3413 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3416 GCC_is_direct (struct CadetConnection *c)
3418 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3423 * Sends an already built message on a connection, properly registering
3424 * all used resources.
3426 * @param message Message to send. Function makes a copy of it.
3427 * If message is not hop-by-hop, decrements TTL of copy.
3428 * @param payload_type Type of payload, in case the message is encrypted.
3429 * @param c Connection on which this message is transmitted.
3430 * @param fwd Is this a fwd message?
3431 * @param force Force the connection to accept the message (buffer overfill).
3432 * @param cont Continuation called once message is sent. Can be NULL.
3433 * @param cont_cls Closure for @c cont.
3435 * @return Handle to cancel the message before it's sent.
3436 * NULL on error or if @c cont is NULL.
3437 * Invalid on @c cont call.
3439 struct CadetConnectionQueue *
3440 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3441 uint16_t payload_type, uint32_t payload_id,
3442 struct CadetConnection *c, int fwd, int force,
3443 GCC_sent cont, void *cont_cls)
3445 struct CadetFlowControl *fc;
3446 struct CadetConnectionQueue *q;
3452 GCC_check_connections ();
3453 size = ntohs (message->size);
3454 data = GNUNET_malloc (size);
3455 memcpy (data, message, size);
3456 type = ntohs (message->type);
3457 LOG (GNUNET_ERROR_TYPE_INFO,
3458 "--> %s (%s %4u) on conn %s (%p) %s (%u bytes)\n",
3459 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), c,
3462 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3463 droppable = GNUNET_NO == force;
3466 struct GNUNET_CADET_AX *axmsg;
3467 struct GNUNET_CADET_Encrypted *emsg;
3468 struct GNUNET_CADET_KX *kmsg;
3469 struct GNUNET_CADET_ACK *amsg;
3470 struct GNUNET_CADET_Poll *pmsg;
3471 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3472 struct GNUNET_CADET_ConnectionBroken *bmsg;
3475 case GNUNET_MESSAGE_TYPE_CADET_AX:
3476 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3477 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3479 emsg = (struct GNUNET_CADET_Encrypted *) data;
3480 ttl = ntohl (emsg->ttl);
3483 GNUNET_break_op (0);
3488 emsg->ttl = htonl (ttl - 1);
3492 axmsg = (struct GNUNET_CADET_AX *) data;
3495 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3496 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3497 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3498 if (GNUNET_YES == droppable)
3504 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3508 case GNUNET_MESSAGE_TYPE_CADET_KX:
3509 kmsg = (struct GNUNET_CADET_KX *) data;
3513 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3514 amsg = (struct GNUNET_CADET_ACK *) data;
3516 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3517 droppable = GNUNET_NO;
3520 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3521 pmsg = (struct GNUNET_CADET_Poll *) data;
3523 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
3524 droppable = GNUNET_NO;
3527 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3528 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
3532 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3533 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
3537 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3538 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3547 if (fc->queue_n > fc->queue_max && droppable)
3549 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3552 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3553 fc->queue_n, fc->queue_max);
3554 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
3555 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
3560 return NULL; /* Drop this message */
3563 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3564 GCC_2s (c), c->pending_messages);
3565 c->pending_messages++;
3567 q = GNUNET_new (struct CadetConnectionQueue);
3568 q->forced = !droppable;
3569 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3570 size, c, fwd, &conn_message_sent, q);
3573 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3576 GCC_check_connections ();
3580 q->cont_cls = cont_cls;
3581 GCC_check_connections ();
3582 return (NULL == cont) ? NULL : q;
3587 * Cancel a previously sent message while it's in the queue.
3589 * ONLY can be called before the continuation given to the send function
3590 * is called. Once the continuation is called, the message is no longer in the
3593 * @param q Handle to the queue.
3596 GCC_cancel (struct CadetConnectionQueue *q)
3598 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3600 /* queue destroy calls message_sent, which calls q->cont and frees q */
3601 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3602 GCC_check_connections ();
3607 * Sends a CREATE CONNECTION message for a path to a peer.
3608 * Changes the connection and tunnel states if necessary.
3610 * @param connection Connection to create.
3613 GCC_send_create (struct CadetConnection *connection)
3615 enum CadetTunnelCState state;
3618 GCC_check_connections ();
3619 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3620 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3622 LOG (GNUNET_ERROR_TYPE_INFO, "--> %s on conn %s (%u bytes)\n",
3623 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE),
3624 GCC_2s (connection), size);
3625 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3626 connection, connection->pending_messages);
3627 connection->pending_messages++;
3629 connection->maintenance_q =
3630 GCP_queue_add (get_next_hop (connection), NULL,
3631 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 0,
3632 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3634 state = GCT_get_cstate (connection->t);
3635 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3636 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3637 if (CADET_CONNECTION_NEW == connection->state)
3638 connection_change_state (connection, CADET_CONNECTION_SENT);
3639 GCC_check_connections ();
3644 * Send a message to all peers in this connection that the connection
3645 * is no longer valid.
3647 * If some peer should not receive the message, it should be zero'ed out
3648 * before calling this function.
3650 * @param c The connection whose peers to notify.
3653 GCC_send_destroy (struct CadetConnection *c)
3655 struct GNUNET_CADET_ConnectionDestroy msg;
3657 if (GNUNET_YES == c->destroy)
3659 GCC_check_connections ();
3660 msg.header.size = htons (sizeof (msg));
3661 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3663 LOG (GNUNET_ERROR_TYPE_DEBUG,
3664 " sending connection destroy for connection %s\n",
3667 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3668 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3669 GNUNET_YES, GNUNET_YES,
3671 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3672 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3673 GNUNET_NO, GNUNET_YES,
3675 c->destroy = GNUNET_YES;
3676 c->state = CADET_CONNECTION_DESTROYED;
3677 GCC_check_connections ();
3682 * @brief Start a polling timer for the connection.
3684 * When a neighbor does not accept more traffic on the connection it could be
3685 * caused by a simple congestion or by a lost ACK. Polling enables to check
3686 * for the lastest ACK status for a connection.
3688 * @param c Connection.
3689 * @param fwd Should we poll in the FWD direction?
3692 GCC_start_poll (struct CadetConnection *c, int fwd)
3694 struct CadetFlowControl *fc;
3696 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3697 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3699 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3701 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not needed (%p, %p)\n",
3702 fc->poll_task, fc->poll_msg);
3705 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3706 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3713 * @brief Stop polling a connection for ACKs.
3715 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3717 * @param c Connection.
3718 * @param fwd Should we stop the poll in the FWD direction?
3721 GCC_stop_poll (struct CadetConnection *c, int fwd)
3723 struct CadetFlowControl *fc;
3725 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3726 if (NULL != fc->poll_task)
3728 GNUNET_SCHEDULER_cancel (fc->poll_task);
3729 fc->poll_task = NULL;
3735 * Get a (static) string for a connection.
3737 * @param c Connection.
3740 GCC_2s (const struct CadetConnection *c)
3747 static char buf[128];
3749 SPRINTF (buf, "%s (->%s)",
3750 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3753 return GNUNET_h2s (GC_h2hc (&c->id));
3758 * Log all possible info about the connection state.
3760 * @param c Connection to debug.
3761 * @param level Debug level to use.
3764 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3769 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3771 __FILE__, __FUNCTION__, __LINE__);
3777 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3781 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3782 s = path_2s (c->path);
3783 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3785 LOG2 (level, "CCC state: %s, destroy: %u\n",
3786 GCC_state2s (c->state), c->destroy);
3787 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3788 if (NULL != c->perf)
3789 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3791 LOG2 (level, "CCC FWD flow control:\n");
3792 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3793 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3794 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3795 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3796 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3797 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3798 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3799 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3801 LOG2 (level, "CCC BCK flow control:\n");
3802 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3803 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3804 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3805 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3806 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3807 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3808 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3809 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3811 LOG2 (level, "CCC DEBUG CONNECTION END\n");