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, "Cancel POLL in ccq for fc %p\n", fc);
1359 peer = get_hop (c, fwd);
1360 GCP_queue_cancel (peer, c);
1361 GCC_check_connections ();
1366 * Function called if a connection has been stalled for a while,
1367 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1369 * @param cls Closure (poll ctx).
1370 * @param tc TaskContext.
1373 connection_poll (void *cls,
1374 const struct GNUNET_SCHEDULER_TaskContext *tc);
1378 * Callback called when a queued POLL message is sent.
1380 * @param cls Closure (FC).
1381 * @param c Connection this message was on.
1382 * @param q Queue handler this call invalidates.
1383 * @param type Type of message sent.
1384 * @param fwd Was this a FWD going message?
1385 * @param size Size of the message.
1388 poll_sent (void *cls,
1389 struct CadetConnection *c,
1390 struct CadetConnectionQueue *q,
1391 uint16_t type, int fwd, size_t size)
1393 struct CadetFlowControl *fc = cls;
1395 if (2 == c->destroy)
1397 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1400 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1402 fc->poll_msg = NULL;
1403 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1404 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1407 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1412 * Function called if a connection has been stalled for a while,
1413 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1415 * @param cls Closure (poll ctx).
1416 * @param tc TaskContext.
1419 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1421 struct CadetFlowControl *fc = cls;
1422 struct GNUNET_CADET_Poll msg;
1423 struct CadetConnection *c;
1426 fc->poll_task = NULL;
1427 GCC_check_connections ();
1428 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1434 fwd = fc == &c->fwd_fc;
1435 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1436 GCC_2s (c), GC_f2s (fwd));
1438 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1439 msg.header.size = htons (sizeof (msg));
1440 msg.pid = htonl (fc->last_pid_sent);
1441 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1443 GCC_send_prebuilt_message (&msg.header, 0, fc->last_pid_sent, c,
1444 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1445 GNUNET_assert (NULL != fc->poll_msg);
1446 GCC_check_connections ();
1451 * Resend all queued messages for a connection on other connections of the
1452 * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
1454 * @param c Connection whose messages to resend.
1455 * @param fwd Resend fwd messages?
1458 resend_messages_and_destroy (struct CadetConnection *c, int fwd)
1460 struct GNUNET_MessageHeader *out_msg;
1461 struct CadetTunnel *t = c->t;
1462 struct CadetPeer *neighbor;
1463 unsigned int pending;
1466 GCC_check_connections ();
1467 c->state = CADET_CONNECTION_DESTROYED;
1468 c->destroy = GNUNET_YES;
1470 destroyed = GNUNET_NO;
1471 neighbor = get_hop (c, fwd);
1472 pending = c->pending_messages;
1474 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1477 GCT_resend_message (out_msg, t);
1478 GNUNET_free (out_msg);
1481 /* All pending messages should have been popped,
1482 * and the connection destroyed by the continuation.
1484 if (GNUNET_YES != destroyed)
1489 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1490 if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1494 GCC_check_connections ();
1499 * Generic connection timeout implementation.
1501 * Timeout function due to lack of keepalive/traffic from an endpoint.
1502 * Destroys connection if called.
1504 * @param c Connection to destroy.
1505 * @param fwd Was the timeout from the origin? (FWD timeout)
1508 connection_timeout (struct CadetConnection *c, int fwd)
1510 struct CadetFlowControl *reverse_fc;
1512 GCC_check_connections ();
1513 reverse_fc = fwd ? &c->bck_fc : &c->fwd_fc;
1515 LOG (GNUNET_ERROR_TYPE_INFO,
1516 "Connection %s %s timed out. Destroying.\n",
1519 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1521 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1527 /* If dest, salvage queued traffic. */
1528 if (GCC_is_origin (c, !fwd))
1530 const struct GNUNET_PeerIdentity *next_hop;
1532 next_hop = GCP_get_id (fwd ? get_prev_hop (c) : get_next_hop (c));
1533 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1534 if (0 < reverse_fc->queue_n)
1535 resend_messages_and_destroy (c, !fwd);
1536 GCC_check_connections ();
1541 GCC_check_connections ();
1546 * Timeout function due to lack of keepalive/traffic from the owner.
1547 * Destroys connection if called.
1549 * @param cls Closure (connection to destroy).
1550 * @param tc TaskContext.
1553 connection_fwd_timeout (void *cls,
1554 const struct GNUNET_SCHEDULER_TaskContext *tc)
1556 struct CadetConnection *c = cls;
1558 c->fwd_maintenance_task = NULL;
1559 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1561 GCC_check_connections ();
1562 connection_timeout (c, GNUNET_YES);
1563 GCC_check_connections ();
1568 * Timeout function due to lack of keepalive/traffic from the destination.
1569 * Destroys connection if called.
1571 * @param cls Closure (connection to destroy).
1572 * @param tc TaskContext
1575 connection_bck_timeout (void *cls,
1576 const struct GNUNET_SCHEDULER_TaskContext *tc)
1578 struct CadetConnection *c = cls;
1580 c->bck_maintenance_task = NULL;
1581 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1583 GCC_check_connections ();
1584 connection_timeout (c, GNUNET_NO);
1585 GCC_check_connections ();
1590 * Resets the connection timeout task, some other message has done the
1592 * - For the first peer on the direction this means to send
1593 * a keepalive or a path confirmation message (either create or ACK).
1594 * - For all other peers, this means to destroy the connection,
1595 * due to lack of activity.
1596 * Starts the timeout if no timeout was running (connection just created).
1598 * @param c Connection whose timeout to reset.
1599 * @param fwd Is this forward?
1601 * TODO use heap to improve efficiency of scheduler.
1604 connection_reset_timeout (struct CadetConnection *c, int fwd)
1606 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1607 if (GCC_is_origin (c, fwd)) /* Startpoint */
1609 schedule_next_keepalive (c, fwd);
1611 else /* Relay, endpoint. */
1613 struct GNUNET_TIME_Relative delay;
1614 struct GNUNET_SCHEDULER_Task * *ti;
1615 GNUNET_SCHEDULER_TaskCallback f;
1617 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1620 GNUNET_SCHEDULER_cancel (*ti);
1621 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1622 LOG (GNUNET_ERROR_TYPE_DEBUG, " timing out in %s\n",
1623 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1624 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1625 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1631 * Iterator to compare each connection's path with the path of a new connection.
1633 * If the connection conincides, the c member of path is set to the connection
1634 * and the destroy flag of the connection is set.
1636 * @param cls Closure (new path).
1637 * @param c Connection in the tunnel to check.
1640 check_path (void *cls, struct CadetConnection *c)
1642 struct CadetConnection *new_conn = cls;
1643 struct CadetPeerPath *path = new_conn->path;
1645 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1646 GCC_2s (c), c, c->path->length);
1649 && c->destroy == GNUNET_NO
1650 && c->state != CADET_CONNECTION_BROKEN
1651 && c->state != CADET_CONNECTION_DESTROYED
1652 && path_equivalent (path, c->path))
1654 new_conn->destroy = GNUNET_YES;
1655 new_conn->path->c = c;
1656 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1662 * Finds out if this path is already being used by an existing connection.
1664 * Checks the tunnel towards the destination to see if it contains
1665 * any connection with the same path.
1667 * If the existing connection is ready, it is kept.
1668 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1669 * the peer will eventually reject our attempt).
1671 * @param path Path to check.
1672 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1673 * #GNUNET_NO otherwise.
1676 does_connection_exist (struct CadetConnection *conn)
1678 struct CadetPeer *p;
1679 struct CadetTunnel *t;
1680 struct CadetConnection *c;
1682 p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
1685 t = GCP_get_tunnel (p);
1689 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
1691 GCT_iterate_connections (t, &check_path, conn);
1693 if (GNUNET_YES == conn->destroy)
1696 conn->destroy = GNUNET_NO;
1697 conn->path->c = conn;
1698 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1699 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1700 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1701 if (CADET_CONNECTION_READY == c->state)
1703 /* The other peer confirmed a live connection with this path,
1704 * why is it trying to duplicate it. */
1705 GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
1708 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not valid, connection unique\n");
1713 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1720 * @brief Check if the tunnel this connection belongs to has any other
1721 * connection with the same path, and destroy one if so.
1723 * @param cls Closure (connection to check).
1724 * @param tc Task context.
1727 check_duplicates (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1729 struct CadetConnection *c = cls;
1731 c->check_duplicates_task = NULL;
1732 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1735 if (GNUNET_YES == does_connection_exist (c))
1737 GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
1738 send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
1746 * Wait for enough time to let any dead connections time out and check for
1747 * any remaining duplicates.
1749 * @param c Connection that is a potential duplicate.
1752 schedule_check_duplicates (struct CadetConnection *c)
1754 struct GNUNET_TIME_Relative delay;
1756 if (NULL != c->check_duplicates_task)
1759 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 5);
1760 c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
1768 * Add the connection to the list of both neighbors.
1770 * @param c Connection.
1772 * @return #GNUNET_OK if everything went fine
1773 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1776 register_neighbors (struct CadetConnection *c)
1778 c->next_peer = get_next_hop (c);
1779 c->prev_peer = get_prev_hop (c);
1780 GNUNET_assert (c->next_peer != c->prev_peer);
1781 LOG (GNUNET_ERROR_TYPE_DEBUG,
1782 "register neighbors for connection %s\n",
1784 path_debug (c->path);
1785 LOG (GNUNET_ERROR_TYPE_DEBUG,
1786 "own pos %u\n", c->own_pos);
1787 LOG (GNUNET_ERROR_TYPE_DEBUG,
1788 "putting connection %s to next peer %p\n",
1791 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1793 GCP_2s (c->next_peer));
1794 LOG (GNUNET_ERROR_TYPE_DEBUG,
1795 "putting connection %s to prev peer %p\n",
1798 LOG (GNUNET_ERROR_TYPE_DEBUG,
1799 "prev peer %p %s\n",
1801 GCP_2s (c->prev_peer));
1803 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1804 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1806 if (GCC_is_origin (c, GNUNET_YES))
1807 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1808 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1810 LOG (GNUNET_ERROR_TYPE_DEBUG,
1811 " register neighbors failed\n");
1812 LOG (GNUNET_ERROR_TYPE_DEBUG,
1813 " prev: %s, neighbor?: %d\n",
1814 GCP_2s (c->prev_peer),
1815 GCP_is_neighbor (c->prev_peer));
1816 LOG (GNUNET_ERROR_TYPE_DEBUG,
1817 " next: %s, neighbor?: %d\n",
1818 GCP_2s (c->next_peer),
1819 GCP_is_neighbor (c->next_peer));
1820 return GNUNET_SYSERR;
1822 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1823 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1830 * Remove the connection from the list of both neighbors.
1832 * @param c Connection.
1835 unregister_neighbors (struct CadetConnection *c)
1837 // struct CadetPeer *peer; FIXME dont use next_peer, prev_peer
1838 /* Either already unregistered or never got registered, it's ok either way. */
1839 if (NULL == c->path)
1841 if (NULL != c->next_peer)
1843 GCP_remove_connection (c->next_peer, c);
1844 c->next_peer = NULL;
1846 if (NULL != c->prev_peer)
1848 GCP_remove_connection (c->prev_peer, c);
1849 c->prev_peer = NULL;
1855 * Bind the connection to the peer and the tunnel to that peer.
1857 * If the peer has no tunnel, create one. Update tunnel and connection
1858 * data structres to reflect new status.
1860 * @param c Connection.
1864 add_to_peer (struct CadetConnection *c,
1865 struct CadetPeer *peer)
1867 GCP_add_tunnel (peer);
1868 c->t = GCP_get_tunnel (peer);
1869 GCT_add_connection (c->t, c);
1874 * Log receipt of message on stderr (INFO level).
1876 * @param message Message received.
1877 * @param peer Peer who sent the message.
1878 * @param hash Connection ID.
1881 log_message (const struct GNUNET_MessageHeader *message,
1882 const struct GNUNET_PeerIdentity *peer,
1883 const struct GNUNET_CADET_Hash *hash)
1887 size = ntohs (message->size);
1888 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1889 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1890 LOG (GNUNET_ERROR_TYPE_INFO, "<-- %s on conn %s from %s, %6u bytes\n",
1891 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (hash)),
1892 GNUNET_i2s (peer), (unsigned int) size);
1895 /******************************************************************************/
1896 /******************************** API ***********************************/
1897 /******************************************************************************/
1900 * Core handler for connection creation.
1902 * @param cls Closure (unused).
1903 * @param peer Sender (neighbor).
1904 * @param message Message.
1905 * @return #GNUNET_OK to keep the connection open,
1906 * #GNUNET_SYSERR to close it (signal serious error)
1909 GCC_handle_create (void *cls,
1910 const struct GNUNET_PeerIdentity *peer,
1911 const struct GNUNET_MessageHeader *message)
1913 struct GNUNET_CADET_ConnectionCreate *msg;
1914 struct GNUNET_PeerIdentity *id;
1915 struct GNUNET_CADET_Hash *cid;
1916 struct CadetPeerPath *path;
1917 struct CadetPeer *dest_peer;
1918 struct CadetPeer *orig_peer;
1919 struct CadetConnection *c;
1920 unsigned int own_pos;
1923 GCC_check_connections ();
1925 size = ntohs (message->size);
1926 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1928 GNUNET_break_op (0);
1932 /* Calculate hops */
1933 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1934 if (size % sizeof (struct GNUNET_PeerIdentity))
1936 GNUNET_break_op (0);
1939 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1941 GNUNET_break_op (0);
1944 size /= sizeof (struct GNUNET_PeerIdentity);
1947 GNUNET_break_op (0);
1950 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1952 /* Get parameters */
1953 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
1955 log_message (message, peer, cid);
1956 id = (struct GNUNET_PeerIdentity *) &msg[1];
1957 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1959 /* Create connection */
1960 c = connection_get (cid);
1963 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1964 size, myid, &own_pos);
1967 /* Path was malformed, probably our own ID was not in it. */
1968 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
1969 GNUNET_break_op (0);
1975 /* We received this request from a neighbor, we cannot be origin */
1976 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
1977 GNUNET_break_op (0);
1978 path_destroy (path);
1982 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1983 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1984 c = GCC_new (cid, NULL, path, own_pos);
1987 if (path->length - 1 == own_pos)
1989 /* If we are destination, why did the creation fail? */
1991 path_destroy (path);
1992 GCC_check_connections ();
1995 send_broken_unknown (cid, &my_full_id,
1996 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1998 path_destroy (path);
1999 GCC_check_connections ();
2002 GCP_add_path_to_all (path, GNUNET_NO);
2003 connection_reset_timeout (c, GNUNET_YES);
2007 path = path_duplicate (c->path);
2009 if (CADET_CONNECTION_NEW == c->state)
2010 connection_change_state (c, CADET_CONNECTION_SENT);
2012 /* Remember peers */
2013 dest_peer = GCP_get (&id[size - 1], GNUNET_YES);
2014 orig_peer = GCP_get (&id[0], GNUNET_YES);
2016 /* Is it a connection to us? */
2017 if (c->own_pos == path->length - 1)
2019 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2020 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
2022 add_to_peer (c, orig_peer);
2023 if (GNUNET_YES == does_connection_exist (c))
2025 /* Peer created a connection equal to one we think exists
2027 * Solution: Keep both and postpone disambiguation. In the meantime
2028 * the connection will time out or peer will inform us it is broken.
2031 * - Use explicit duplicate.
2032 * - Accept new conn and destroy the old. (interruption in higher level)
2033 * - Keep the one with higher ID / created by peer with higher ID. */
2034 schedule_check_duplicates (c);
2037 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
2038 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
2040 send_connection_ack (c, GNUNET_NO);
2041 if (CADET_CONNECTION_SENT == c->state)
2042 connection_change_state (c, CADET_CONNECTION_ACK);
2046 /* It's for somebody else! Retransmit. */
2047 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2048 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2049 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2050 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
2051 GNUNET_YES, GNUNET_YES,
2054 path_destroy (path);
2055 GCC_check_connections ();
2061 * Core handler for path confirmations.
2063 * @param cls closure
2064 * @param message message
2065 * @param peer peer identity this notification is about
2066 * @return #GNUNET_OK to keep the connection open,
2067 * #GNUNET_SYSERR to close it (signal serious error)
2070 GCC_handle_confirm (void *cls,
2071 const struct GNUNET_PeerIdentity *peer,
2072 const struct GNUNET_MessageHeader *message)
2074 struct GNUNET_CADET_ConnectionACK *msg;
2075 struct CadetConnection *c;
2076 struct CadetPeerPath *p;
2077 struct CadetPeer *pi;
2078 enum CadetConnectionState oldstate;
2081 GCC_check_connections ();
2082 msg = (struct GNUNET_CADET_ConnectionACK *) message;
2083 log_message (message, peer, &msg->cid);
2084 c = connection_get (&msg->cid);
2087 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2089 LOG (GNUNET_ERROR_TYPE_DEBUG,
2090 " don't know the connection!\n");
2091 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2092 GCC_check_connections ();
2096 oldstate = c->state;
2097 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
2098 pi = GCP_get (peer, GNUNET_YES);
2099 if (get_next_hop (c) == pi)
2101 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2103 if (CADET_CONNECTION_SENT == oldstate)
2104 connection_change_state (c, CADET_CONNECTION_ACK);
2106 else if (get_prev_hop (c) == pi)
2108 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2110 connection_change_state (c, CADET_CONNECTION_READY);
2114 GNUNET_break_op (0);
2118 connection_reset_timeout (c, fwd);
2120 /* Add path to peers? */
2124 GCP_add_path_to_all (p, GNUNET_YES);
2131 /* Message for us as creator? */
2132 if (GCC_is_origin (c, GNUNET_YES))
2134 if (GNUNET_NO != fwd)
2136 GNUNET_break_op (0);
2139 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2141 /* If just created, cancel the short timeout and start a long one */
2142 if (CADET_CONNECTION_SENT == oldstate)
2143 connection_reset_timeout (c, GNUNET_YES);
2145 /* Change connection state */
2146 connection_change_state (c, CADET_CONNECTION_READY);
2147 send_connection_ack (c, GNUNET_YES);
2149 /* Change tunnel state, trigger KX */
2150 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2151 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2152 GCC_check_connections ();
2156 /* Message for us as destination? */
2157 if (GCC_is_terminal (c, GNUNET_YES))
2159 if (GNUNET_YES != fwd)
2161 GNUNET_break_op (0);
2164 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2166 /* If just created, cancel the short timeout and start a long one */
2167 if (CADET_CONNECTION_ACK == oldstate)
2168 connection_reset_timeout (c, GNUNET_NO);
2170 /* Change tunnel state */
2171 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2172 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2173 GCC_check_connections ();
2177 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2178 GNUNET_assert (NULL ==
2179 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2180 GNUNET_YES, NULL, NULL));
2181 GCC_check_connections ();
2187 * Core handler for notifications of broken connections.
2189 * @param cls Closure (unused).
2190 * @param id Peer identity of sending neighbor.
2191 * @param message Message.
2192 * @return #GNUNET_OK to keep the connection open,
2193 * #GNUNET_SYSERR to close it (signal serious error)
2196 GCC_handle_broken (void* cls,
2197 const struct GNUNET_PeerIdentity* id,
2198 const struct GNUNET_MessageHeader* message)
2200 struct GNUNET_CADET_ConnectionBroken *msg;
2201 struct CadetConnection *c;
2202 struct CadetTunnel *t;
2206 GCC_check_connections ();
2207 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
2208 log_message (message, id, &msg->cid);
2209 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2210 GNUNET_i2s (&msg->peer1));
2211 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2212 GNUNET_i2s (&msg->peer2));
2213 c = connection_get (&msg->cid);
2216 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2217 GCC_check_connections ();
2223 fwd = is_fwd (c, id);
2224 c->destroy = GNUNET_YES;
2225 if (GCC_is_terminal (c, fwd))
2227 struct CadetPeer *endpoint;
2231 /* A terminal connection should not have 't' set to NULL. */
2233 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2236 endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
2237 if (2 < c->path->length)
2238 path_invalidate (c->path);
2239 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2241 c->state = CADET_CONNECTION_BROKEN;
2242 GCT_remove_connection (t, c);
2245 pending = c->pending_messages;
2247 resend_messages_and_destroy (c, !fwd);
2253 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2254 GNUNET_YES, NULL, NULL));
2255 connection_cancel_queues (c, !fwd);
2257 GCC_check_connections ();
2263 * Core handler for tunnel destruction
2265 * @param cls Closure (unused).
2266 * @param peer Peer identity of sending neighbor.
2267 * @param message Message.
2268 * @return #GNUNET_OK to keep the connection open,
2269 * #GNUNET_SYSERR to close it (signal serious error)
2272 GCC_handle_destroy (void *cls,
2273 const struct GNUNET_PeerIdentity *peer,
2274 const struct GNUNET_MessageHeader *message)
2276 const struct GNUNET_CADET_ConnectionDestroy *msg;
2277 struct CadetConnection *c;
2280 GCC_check_connections ();
2281 msg = (const struct GNUNET_CADET_ConnectionDestroy *) message;
2282 log_message (message, peer, &msg->cid);
2283 c = connection_get (&msg->cid);
2286 /* Probably already got the message from another path,
2287 * destroyed the tunnel and retransmitted to children.
2290 GNUNET_STATISTICS_update (stats,
2291 "# control on unknown connection",
2293 LOG (GNUNET_ERROR_TYPE_DEBUG,
2294 " connection unknown: already destroyed?\n");
2295 GCC_check_connections ();
2298 fwd = is_fwd (c, peer);
2299 if (GNUNET_SYSERR == fwd)
2301 GNUNET_break_op (0); /* FIXME */
2304 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2306 GNUNET_assert (NULL ==
2307 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2308 GNUNET_YES, NULL, NULL));
2310 else if (0 == c->pending_messages)
2312 LOG (GNUNET_ERROR_TYPE_DEBUG,
2313 " directly destroying connection!\n");
2315 GCC_check_connections ();
2318 c->destroy = GNUNET_YES;
2319 c->state = CADET_CONNECTION_DESTROYED;
2322 GCT_remove_connection (c->t, c);
2325 GCC_check_connections ();
2331 * Check the message against internal state and test if it goes FWD or BCK.
2333 * Updates the PID, state and timeout values for the connection.
2335 * @param message Message to check. It must belong to an existing connection.
2336 * @param minimum_size The message cannot be smaller than this value.
2337 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2338 * @param c Connection this message should belong. If NULL, check fails.
2339 * @param neighbor Neighbor that sent the message.
2342 check_message (const struct GNUNET_MessageHeader *message,
2343 size_t minimum_size,
2344 const struct GNUNET_CADET_Hash* cid,
2345 struct CadetConnection *c,
2346 const struct GNUNET_PeerIdentity *neighbor,
2349 GNUNET_PEER_Id neighbor_id;
2350 struct CadetFlowControl *fc;
2351 struct CadetPeer *hop;
2356 if (ntohs (message->size) < minimum_size)
2358 GNUNET_break_op (0);
2359 LOG (GNUNET_ERROR_TYPE_WARNING, "Size %u < %u\n",
2360 ntohs (message->size), minimum_size);
2361 return GNUNET_SYSERR;
2364 /* Check connection */
2367 GNUNET_STATISTICS_update (stats,
2368 "# unknown connection",
2370 LOG (GNUNET_ERROR_TYPE_DEBUG,
2371 "%s on unknown connection %s\n",
2372 GC_m2s (ntohs (message->type)),
2373 GNUNET_h2s (GC_h2hc (cid)));
2374 send_broken_unknown (cid,
2378 return GNUNET_SYSERR;
2381 /* Check if origin is as expected */
2382 neighbor_id = GNUNET_PEER_search (neighbor);
2383 hop = get_prev_hop (c);
2384 if (neighbor_id == GCP_get_short_id (hop))
2390 hop = get_next_hop (c);
2391 GNUNET_break (hop == c->next_peer);
2392 if (neighbor_id == GCP_get_short_id (hop))
2398 /* Unexpected peer sending traffic on a connection. */
2399 GNUNET_break_op (0);
2400 return GNUNET_SYSERR;
2404 /* Check PID for payload messages */
2405 type = ntohs (message->type);
2406 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
2407 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
2409 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2410 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2411 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2412 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2414 GNUNET_break_op (0);
2415 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2416 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2417 pid, fc->last_pid_recv, fc->last_ack_sent);
2418 return GNUNET_SYSERR;
2420 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2424 delta = pid - fc->last_pid_recv;
2425 fc->last_pid_recv = pid;
2426 fc->recv_bitmap <<= delta;
2427 fc->recv_bitmap |= 1;
2431 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2432 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2434 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2435 pid, fc->last_pid_recv - 31);
2436 return GNUNET_SYSERR;
2438 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2442 /* Count as connection confirmation. */
2443 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2445 connection_change_state (c, CADET_CONNECTION_READY);
2448 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2449 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2452 connection_reset_timeout (c, fwd);
2459 * Generic handler for cadet network encrypted traffic.
2461 * @param peer Peer identity this notification is about.
2462 * @param msg Encrypted message.
2463 * @return #GNUNET_OK to keep the connection open,
2464 * #GNUNET_SYSERR to close it (signal serious error)
2467 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
2468 const struct GNUNET_MessageHeader *message)
2470 const struct GNUNET_CADET_Encrypted *otr_msg;
2471 const struct GNUNET_CADET_AX *ax_msg;
2472 const struct GNUNET_CADET_Hash* cid;
2473 struct CadetConnection *c;
2474 size_t minumum_size;
2480 GCC_check_connections ();
2481 if (GNUNET_MESSAGE_TYPE_CADET_AX == ntohs (message->type))
2483 overhead = sizeof (struct GNUNET_CADET_AX);
2484 ax_msg = (const struct GNUNET_CADET_AX *) message;
2486 pid = ntohl (ax_msg->pid);
2491 overhead = sizeof (struct GNUNET_CADET_Encrypted);
2492 otr_msg = (const struct GNUNET_CADET_Encrypted *) message;
2493 cid = &otr_msg->cid;
2494 pid = ntohl (otr_msg->pid);
2497 log_message (message, peer, cid);
2499 minumum_size = sizeof (struct GNUNET_MessageHeader) + overhead;
2500 c = connection_get (cid);
2501 fwd = check_message (message,
2508 /* If something went wrong, discard message. */
2509 if (GNUNET_SYSERR == fwd)
2511 GCC_check_connections ();
2515 /* Is this message for us? */
2516 if (GCC_is_terminal (c, fwd))
2518 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2522 GNUNET_break (GNUNET_NO != c->destroy);
2525 GCT_handle_encrypted (c->t, message);
2526 GCC_send_ack (c, fwd, GNUNET_NO);
2527 GCC_check_connections ();
2531 /* Message not for us: forward to next hop */
2532 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2533 if (NULL != otr_msg) /* only otr has ttl */
2535 ttl = ntohl (otr_msg->ttl);
2536 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2539 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2540 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2541 GCC_send_ack (c, fwd, GNUNET_NO);
2542 GCC_check_connections ();
2547 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2548 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2549 GNUNET_NO, NULL, NULL));
2550 GCC_check_connections ();
2556 * Generic handler for cadet network encrypted traffic.
2558 * @param peer Peer identity this notification is about.
2559 * @param msg Encrypted message.
2560 * @return #GNUNET_OK to keep the connection open,
2561 * #GNUNET_SYSERR to close it (signal serious error)
2564 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2565 const struct GNUNET_CADET_KX *msg)
2567 const struct GNUNET_CADET_Hash* cid;
2568 struct CadetConnection *c;
2569 size_t expected_size;
2572 GCC_check_connections ();
2574 log_message (&msg->header, peer, cid);
2576 expected_size = sizeof (struct GNUNET_CADET_KX)
2577 + sizeof (struct GNUNET_MessageHeader);
2578 c = connection_get (cid);
2579 fwd = check_message (&msg->header,
2586 /* If something went wrong, discard message. */
2587 if (GNUNET_SYSERR == fwd)
2590 /* Is this message for us? */
2591 if (GCC_is_terminal (c, fwd))
2593 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2594 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2600 GCT_handle_kx (c->t, &msg[1].header);
2601 GCC_check_connections ();
2605 /* Message not for us: forward to next hop */
2606 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2607 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2608 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2609 GNUNET_NO, NULL, NULL));
2610 GCC_check_connections ();
2616 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2618 * @param cls Closure (unused).
2619 * @param message Message received.
2620 * @param peer Peer who sent the message.
2621 * @return #GNUNET_OK to keep the connection open,
2622 * #GNUNET_SYSERR to close it (signal serious error)
2625 GCC_handle_kx (void *cls,
2626 const struct GNUNET_PeerIdentity *peer,
2627 const struct GNUNET_MessageHeader *message)
2629 GCC_check_connections ();
2630 return handle_cadet_kx (peer, (struct GNUNET_CADET_KX *) message);
2635 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2637 * @param cls Closure (unused).
2638 * @param message Message received.
2639 * @param peer Peer who sent the message.
2640 * @return #GNUNET_OK to keep the connection open,
2641 * #GNUNET_SYSERR to close it (signal serious error)
2644 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2645 const struct GNUNET_MessageHeader *message)
2647 GCC_check_connections ();
2648 return handle_cadet_encrypted (peer, message);
2653 * Core handler for cadet network traffic point-to-point acks.
2655 * @param cls closure
2656 * @param message message
2657 * @param peer peer identity this notification is about
2658 * @return #GNUNET_OK to keep the connection open,
2659 * #GNUNET_SYSERR to close it (signal serious error)
2662 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2663 const struct GNUNET_MessageHeader *message)
2665 struct GNUNET_CADET_ACK *msg;
2666 struct CadetConnection *c;
2667 struct CadetFlowControl *fc;
2672 GCC_check_connections ();
2673 msg = (struct GNUNET_CADET_ACK *) message;
2674 log_message (message, peer, &msg->cid);
2675 c = connection_get (&msg->cid);
2678 GNUNET_STATISTICS_update (stats,
2679 "# ack on unknown connection",
2682 send_broken_unknown (&msg->cid,
2686 GCC_check_connections ();
2690 /* Is this a forward or backward ACK? */
2691 id = GNUNET_PEER_search (peer);
2692 if (GCP_get_short_id (get_next_hop (c)) == id)
2697 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2704 GNUNET_break_op (0);
2708 ack = ntohl (msg->ack);
2709 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2710 GC_f2s (fwd), ack, fc->last_ack_recv);
2711 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2712 fc->last_ack_recv = ack;
2714 /* Cancel polling if the ACK is big enough. */
2715 if (NULL != fc->poll_task &&
2716 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2718 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2719 GNUNET_SCHEDULER_cancel (fc->poll_task);
2720 fc->poll_task = NULL;
2721 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2724 connection_unlock_queue (c, fwd);
2725 GCC_check_connections ();
2731 * Core handler for cadet network traffic point-to-point ack polls.
2733 * @param cls closure
2734 * @param message message
2735 * @param peer peer identity this notification is about
2736 * @return #GNUNET_OK to keep the connection open,
2737 * #GNUNET_SYSERR to close it (signal serious error)
2740 GCC_handle_poll (void *cls,
2741 const struct GNUNET_PeerIdentity *peer,
2742 const struct GNUNET_MessageHeader *message)
2744 struct GNUNET_CADET_Poll *msg;
2745 struct CadetConnection *c;
2746 struct CadetFlowControl *fc;
2751 GCC_check_connections ();
2752 msg = (struct GNUNET_CADET_Poll *) message;
2753 log_message (message, peer, &msg->cid);
2754 c = connection_get (&msg->cid);
2757 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2759 LOG (GNUNET_ERROR_TYPE_DEBUG,
2760 "POLL message on unknown connection %s!\n",
2761 GNUNET_h2s (GC_h2hc (&msg->cid)));
2762 send_broken_unknown (&msg->cid,
2766 GCC_check_connections ();
2770 /* Is this a forward or backward ACK?
2771 * Note: a poll should never be needed in a loopback case,
2772 * since there is no possiblility of packet loss there, so
2773 * this way of discerining FWD/BCK should not be a problem.
2775 id = GNUNET_PEER_search (peer);
2776 if (GCP_get_short_id (get_next_hop (c)) == id)
2778 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2781 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2783 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2788 GNUNET_break_op (0);
2792 pid = ntohl (msg->pid);
2793 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2794 fc->last_pid_recv = pid;
2795 fwd = fc == &c->bck_fc;
2796 GCC_send_ack (c, fwd, GNUNET_YES);
2797 GCC_check_connections ();
2804 * Send an ACK on the appropriate connection/channel, depending on
2805 * the direction and the position of the peer.
2807 * @param c Which connection to send the hop-by-hop ACK.
2808 * @param fwd Is this a fwd ACK? (will go dest->root).
2809 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2812 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2814 unsigned int buffer;
2816 GCC_check_connections ();
2817 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
2818 GC_f2s (fwd), GCC_2s (c));
2826 if (GNUNET_NO != c->destroy)
2828 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2829 GCC_check_connections ();
2833 /* Get available buffer space */
2834 if (GCC_is_terminal (c, fwd))
2836 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2837 buffer = GCT_get_channels_buffer (c->t);
2841 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2842 buffer = GCC_get_buffer (c, fwd);
2844 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2845 if (0 == buffer && GNUNET_NO == force)
2847 GCC_check_connections ();
2851 /* Send available buffer space */
2852 if (GCC_is_origin (c, fwd))
2854 GNUNET_assert (NULL != c->t);
2855 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2856 GCT_unchoke_channels (c->t);
2860 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2861 send_ack (c, buffer, fwd, force);
2863 GCC_check_connections ();
2868 * Initialize the connections subsystem
2870 * @param c Configuration handle.
2873 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2875 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2877 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2880 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2881 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2882 GNUNET_SCHEDULER_shutdown ();
2887 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2890 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2891 "CADET", "MAX_CONNECTIONS", "MISSING");
2892 GNUNET_SCHEDULER_shutdown ();
2897 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2898 &refresh_connection_time))
2900 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2901 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2902 GNUNET_SCHEDULER_shutdown ();
2905 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2906 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2911 * Destroy each connection on shutdown.
2913 * @param cls Closure (unused).
2914 * @param key Current key code (CID, unused).
2915 * @param value Value in the hash map (`struct CadetConnection`)
2917 * @return #GNUNET_YES, because we should continue to iterate
2920 shutdown_iterator (void *cls,
2921 const struct GNUNET_HashCode *key,
2924 struct CadetConnection *c = value;
2926 c->state = CADET_CONNECTION_DESTROYED;
2933 * Shut down the connections subsystem.
2938 GCC_check_connections ();
2939 GNUNET_CONTAINER_multihashmap_iterate (connections,
2942 GNUNET_CONTAINER_multihashmap_destroy (connections);
2948 * Create a connection.
2950 * @param cid Connection ID (either created locally or imposed remotely).
2951 * @param t Tunnel this connection belongs to (or NULL);
2952 * @param path Path this connection has to use (copy is made).
2953 * @param own_pos Own position in the @c path path.
2955 * @return Newly created connection, NULL in case of error (own id not in path).
2957 struct CadetConnection *
2958 GCC_new (const struct GNUNET_CADET_Hash *cid,
2959 struct CadetTunnel *t,
2960 struct CadetPeerPath *path,
2961 unsigned int own_pos)
2963 struct CadetConnection *c;
2964 struct CadetPeerPath *p;
2966 GCC_check_connections ();
2967 p = path_duplicate (path);
2968 GNUNET_assert (NULL != p);
2969 c = GNUNET_new (struct CadetConnection);
2971 GNUNET_assert (GNUNET_OK ==
2972 GNUNET_CONTAINER_multihashmap_put (connections,
2974 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2975 fc_init (&c->fwd_fc);
2976 fc_init (&c->bck_fc);
2981 GNUNET_assert (own_pos <= p->length - 1);
2982 c->own_pos = own_pos;
2985 if (GNUNET_OK != register_neighbors (c))
2989 /* We were the origin of this request, this means we have invalid
2990 * info about the paths to reach the destination. We must invalidate
2991 * the *original* path to avoid trying it again in the next minute.
2993 path_invalidate (path);
2996 path_destroy (c->path);
3001 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
3002 GCC_check_connections ();
3008 GCC_destroy (struct CadetConnection *c)
3010 GCC_check_connections ();
3017 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
3018 return; /* -> message_sent -> GCC_destroy. Don't loop. */
3021 LOG (GNUNET_ERROR_TYPE_DEBUG,
3022 "destroying connection %s\n",
3024 LOG (GNUNET_ERROR_TYPE_DEBUG,
3025 " fc's f: %p, b: %p\n",
3026 &c->fwd_fc, &c->bck_fc);
3027 LOG (GNUNET_ERROR_TYPE_DEBUG,
3028 " fc tasks f: %u, b: %u\n",
3029 c->fwd_fc.poll_task,
3030 c->bck_fc.poll_task);
3032 /* Cancel all traffic */
3033 if (NULL != c->path)
3035 connection_cancel_queues (c, GNUNET_YES);
3036 connection_cancel_queues (c, GNUNET_NO);
3038 unregister_neighbors (c);
3039 path_destroy (c->path);
3042 /* Cancel maintainance task (keepalive/timeout) */
3043 if (NULL != c->fwd_fc.poll_msg)
3045 GCC_cancel (c->fwd_fc.poll_msg);
3046 LOG (GNUNET_ERROR_TYPE_DEBUG,
3047 " POLL msg FWD canceled\n");
3049 if (NULL != c->bck_fc.poll_msg)
3051 GCC_cancel (c->bck_fc.poll_msg);
3052 LOG (GNUNET_ERROR_TYPE_DEBUG,
3053 " POLL msg BCK canceled\n");
3056 /* Delete from tunnel */
3058 GCT_remove_connection (c->t, c);
3060 if (NULL != c->check_duplicates_task)
3061 GNUNET_SCHEDULER_cancel (c->check_duplicates_task);
3062 if (NULL != c->fwd_maintenance_task)
3063 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
3064 if (NULL != c->bck_maintenance_task)
3065 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
3066 if (NULL != c->fwd_fc.poll_task)
3068 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
3069 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task FWD canceled\n");
3071 if (NULL != c->bck_fc.poll_task)
3073 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
3074 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task BCK canceled\n");
3076 if (GNUNET_NO == c->was_removed)
3078 GNUNET_break (GNUNET_YES ==
3079 GNUNET_CONTAINER_multihashmap_remove (connections,
3083 GNUNET_STATISTICS_update (stats,
3088 GCC_check_connections ();
3093 * Get the connection ID.
3095 * @param c Connection to get the ID from.
3097 * @return ID of the connection.
3099 const struct GNUNET_CADET_Hash *
3100 GCC_get_id (const struct CadetConnection *c)
3107 * Get the connection ID.
3109 * @param c Connection to get the ID from.
3111 * @return ID of the connection.
3113 const struct GNUNET_HashCode *
3114 GCC_get_h (const struct CadetConnection *c)
3116 return GC_h2hc (&c->id);
3121 * Get the connection path.
3123 * @param c Connection to get the path from.
3125 * @return path used by the connection.
3127 const struct CadetPeerPath *
3128 GCC_get_path (const struct CadetConnection *c)
3130 if (GNUNET_NO == c->destroy)
3137 * Get the connection state.
3139 * @param c Connection to get the state from.
3141 * @return state of the connection.
3143 enum CadetConnectionState
3144 GCC_get_state (const struct CadetConnection *c)
3150 * Get the connection tunnel.
3152 * @param c Connection to get the tunnel from.
3154 * @return tunnel of the connection.
3156 struct CadetTunnel *
3157 GCC_get_tunnel (const struct CadetConnection *c)
3164 * Get free buffer space in a connection.
3166 * @param c Connection.
3167 * @param fwd Is query about FWD traffic?
3169 * @return Free buffer space [0 - max_msgs_queue/max_connections]
3172 GCC_get_buffer (struct CadetConnection *c, int fwd)
3174 struct CadetFlowControl *fc;
3176 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3178 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
3179 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
3180 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
3182 return (fc->queue_max - fc->queue_n);
3187 * Get how many messages have we allowed to send to us from a direction.
3189 * @param c Connection.
3190 * @param fwd Are we asking about traffic from FWD (BCK messages)?
3192 * @return last_ack_sent - last_pid_recv
3195 GCC_get_allowed (struct CadetConnection *c, int fwd)
3197 struct CadetFlowControl *fc;
3199 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3200 if (CADET_CONNECTION_READY != c->state
3201 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
3205 return (fc->last_ack_sent - fc->last_pid_recv);
3210 * Get messages queued in a connection.
3212 * @param c Connection.
3213 * @param fwd Is query about FWD traffic?
3215 * @return Number of messages queued.
3218 GCC_get_qn (struct CadetConnection *c, int fwd)
3220 struct CadetFlowControl *fc;
3222 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3229 * Get next PID to use.
3231 * @param c Connection.
3232 * @param fwd Is query about FWD traffic?
3234 * @return Last PID used + 1.
3237 GCC_get_pid (struct CadetConnection *c, int fwd)
3239 struct CadetFlowControl *fc;
3241 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3243 return fc->last_pid_sent + 1;
3248 * Allow the connection to advertise a buffer of the given size.
3250 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3251 * allowing up to last_pid_recv + buffer.
3253 * @param c Connection.
3254 * @param buffer How many more messages the connection can accept.
3255 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3258 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3260 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3261 GCC_2s (c), buffer, GC_f2s (fwd));
3262 send_ack (c, buffer, fwd, GNUNET_NO);
3267 * Notify other peers on a connection of a broken link. Mark connections
3268 * to destroy after all traffic has been sent.
3270 * @param c Connection on which there has been a disconnection.
3271 * @param peer Peer that disconnected.
3274 GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
3276 struct CadetPeer *hop;
3280 GCC_check_connections ();
3281 strncpy (peer_name, GCP_2s (peer), 16);
3282 peer_name[15] = '\0';
3283 LOG (GNUNET_ERROR_TYPE_DEBUG,
3284 "shutting down %s, %s disconnected\n",
3285 GCC_2s (c), peer_name);
3286 hop = get_prev_hop (c);
3289 /* Path was NULL, we should have deleted the connection. */
3293 fwd = (peer == hop);
3294 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3295 (GNUNET_NO != c->destroy) )
3297 /* Local shutdown, or other peer already down (hence 'c->destroy');
3298 so there is no one to notify about this, just clean up. */
3300 GCC_check_connections ();
3303 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3304 /* Connection will have at least one pending message
3305 * (the one we just scheduled), so delay destruction
3306 * and remove from map so we don't use accidentally. */
3307 c->destroy = GNUNET_YES;
3308 c->state = CADET_CONNECTION_DESTROYED;
3309 GNUNET_assert (GNUNET_NO == c->was_removed);
3310 c->was_removed = GNUNET_YES;
3311 GNUNET_break (GNUNET_YES ==
3312 GNUNET_CONTAINER_multihashmap_remove (connections,
3315 /* Cancel queue in the direction that just died. */
3316 connection_cancel_queues (c, ! fwd);
3317 GCC_stop_poll (c, ! fwd);
3318 unregister_neighbors (c);
3319 GCC_check_connections ();
3324 * Is this peer the first one on the connection?
3326 * @param c Connection.
3327 * @param fwd Is this about fwd traffic?
3329 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3332 GCC_is_origin (struct CadetConnection *c, int fwd)
3334 if (!fwd && c->path->length - 1 == c->own_pos )
3336 if (fwd && 0 == c->own_pos)
3343 * Is this peer the last one on the connection?
3345 * @param c Connection.
3346 * @param fwd Is this about fwd traffic?
3347 * Note that the ROOT is the terminal for BCK traffic!
3349 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3352 GCC_is_terminal (struct CadetConnection *c, int fwd)
3354 return GCC_is_origin (c, ! fwd);
3359 * See if we are allowed to send by the next hop in the given direction.
3361 * @param c Connection.
3362 * @param fwd Is this about fwd traffic?
3364 * @return #GNUNET_YES in case it's OK to send.
3367 GCC_is_sendable (struct CadetConnection *c, int fwd)
3369 struct CadetFlowControl *fc;
3371 LOG (GNUNET_ERROR_TYPE_DEBUG,
3372 " checking sendability of %s traffic on %s\n",
3373 GC_f2s (fwd), GCC_2s (c));
3379 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3380 LOG (GNUNET_ERROR_TYPE_DEBUG,
3381 " last ack recv: %u, last pid sent: %u\n",
3382 fc->last_ack_recv, fc->last_pid_sent);
3383 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3385 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3388 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3394 * Check if this connection is a direct one (never trim a direct connection).
3396 * @param c Connection.
3398 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3401 GCC_is_direct (struct CadetConnection *c)
3403 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3408 * Sends an already built message on a connection, properly registering
3409 * all used resources.
3411 * @param message Message to send. Function makes a copy of it.
3412 * If message is not hop-by-hop, decrements TTL of copy.
3413 * @param payload_type Type of payload, in case the message is encrypted.
3414 * @param c Connection on which this message is transmitted.
3415 * @param fwd Is this a fwd message?
3416 * @param force Force the connection to accept the message (buffer overfill).
3417 * @param cont Continuation called once message is sent. Can be NULL.
3418 * @param cont_cls Closure for @c cont.
3420 * @return Handle to cancel the message before it's sent.
3421 * NULL on error or if @c cont is NULL.
3422 * Invalid on @c cont call.
3424 struct CadetConnectionQueue *
3425 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3426 uint16_t payload_type, uint32_t payload_id,
3427 struct CadetConnection *c, int fwd, int force,
3428 GCC_sent cont, void *cont_cls)
3430 struct CadetFlowControl *fc;
3431 struct CadetConnectionQueue *q;
3437 GCC_check_connections ();
3438 size = ntohs (message->size);
3439 data = GNUNET_malloc (size);
3440 memcpy (data, message, size);
3441 type = ntohs (message->type);
3442 LOG (GNUNET_ERROR_TYPE_INFO,
3443 "--> %s (%s %4u) on conn %s (%p) %s (%u bytes)\n",
3444 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), c,
3447 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3448 droppable = GNUNET_NO == force;
3451 struct GNUNET_CADET_AX *axmsg;
3452 struct GNUNET_CADET_Encrypted *emsg;
3453 struct GNUNET_CADET_KX *kmsg;
3454 struct GNUNET_CADET_ACK *amsg;
3455 struct GNUNET_CADET_Poll *pmsg;
3456 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3457 struct GNUNET_CADET_ConnectionBroken *bmsg;
3460 case GNUNET_MESSAGE_TYPE_CADET_AX:
3461 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3462 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3464 emsg = (struct GNUNET_CADET_Encrypted *) data;
3465 ttl = ntohl (emsg->ttl);
3468 GNUNET_break_op (0);
3473 emsg->ttl = htonl (ttl - 1);
3477 axmsg = (struct GNUNET_CADET_AX *) data;
3480 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3481 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3482 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3483 if (GNUNET_YES == droppable)
3489 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3493 case GNUNET_MESSAGE_TYPE_CADET_KX:
3494 kmsg = (struct GNUNET_CADET_KX *) data;
3498 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3499 amsg = (struct GNUNET_CADET_ACK *) data;
3501 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3502 droppable = GNUNET_NO;
3505 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3506 pmsg = (struct GNUNET_CADET_Poll *) data;
3508 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
3509 droppable = GNUNET_NO;
3512 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3513 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
3517 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3518 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
3522 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3523 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3532 if (fc->queue_n > fc->queue_max && droppable)
3534 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3537 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3538 fc->queue_n, fc->queue_max);
3539 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
3540 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
3545 return NULL; /* Drop this message */
3548 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3549 GCC_2s (c), c->pending_messages);
3550 c->pending_messages++;
3552 q = GNUNET_new (struct CadetConnectionQueue);
3553 q->forced = !droppable;
3554 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3555 size, c, fwd, &conn_message_sent, q);
3558 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3561 GCC_check_connections ();
3565 q->cont_cls = cont_cls;
3566 GCC_check_connections ();
3567 return (NULL == cont) ? NULL : q;
3572 * Cancel a previously sent message while it's in the queue.
3574 * ONLY can be called before the continuation given to the send function
3575 * is called. Once the continuation is called, the message is no longer in the
3578 * @param q Handle to the queue.
3581 GCC_cancel (struct CadetConnectionQueue *q)
3583 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3585 /* queue destroy calls message_sent, which calls q->cont and frees q */
3586 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3587 GCC_check_connections ();
3592 * Sends a CREATE CONNECTION message for a path to a peer.
3593 * Changes the connection and tunnel states if necessary.
3595 * @param connection Connection to create.
3598 GCC_send_create (struct CadetConnection *connection)
3600 enum CadetTunnelCState state;
3603 GCC_check_connections ();
3604 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3605 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3607 LOG (GNUNET_ERROR_TYPE_INFO, "--> %s on conn %s (%u bytes)\n",
3608 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE),
3609 GCC_2s (connection), size);
3610 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3611 connection, connection->pending_messages);
3612 connection->pending_messages++;
3614 connection->maintenance_q =
3615 GCP_queue_add (get_next_hop (connection), NULL,
3616 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 0,
3617 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3619 state = GCT_get_cstate (connection->t);
3620 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3621 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3622 if (CADET_CONNECTION_NEW == connection->state)
3623 connection_change_state (connection, CADET_CONNECTION_SENT);
3624 GCC_check_connections ();
3629 * Send a message to all peers in this connection that the connection
3630 * is no longer valid.
3632 * If some peer should not receive the message, it should be zero'ed out
3633 * before calling this function.
3635 * @param c The connection whose peers to notify.
3638 GCC_send_destroy (struct CadetConnection *c)
3640 struct GNUNET_CADET_ConnectionDestroy msg;
3642 if (GNUNET_YES == c->destroy)
3644 GCC_check_connections ();
3645 msg.header.size = htons (sizeof (msg));
3646 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3648 LOG (GNUNET_ERROR_TYPE_DEBUG,
3649 " sending connection destroy for connection %s\n",
3652 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3653 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3654 GNUNET_YES, GNUNET_YES,
3656 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3657 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3658 GNUNET_NO, GNUNET_YES,
3660 c->destroy = GNUNET_YES;
3661 c->state = CADET_CONNECTION_DESTROYED;
3662 GCC_check_connections ();
3667 * @brief Start a polling timer for the connection.
3669 * When a neighbor does not accept more traffic on the connection it could be
3670 * caused by a simple congestion or by a lost ACK. Polling enables to check
3671 * for the lastest ACK status for a connection.
3673 * @param c Connection.
3674 * @param fwd Should we poll in the FWD direction?
3677 GCC_start_poll (struct CadetConnection *c, int fwd)
3679 struct CadetFlowControl *fc;
3681 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3682 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3684 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3686 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not needed (%p, %p)\n",
3687 fc->poll_task, fc->poll_msg);
3690 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3691 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3698 * @brief Stop polling a connection for ACKs.
3700 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3702 * @param c Connection.
3703 * @param fwd Should we stop the poll in the FWD direction?
3706 GCC_stop_poll (struct CadetConnection *c, int fwd)
3708 struct CadetFlowControl *fc;
3710 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3711 if (NULL != fc->poll_task)
3713 GNUNET_SCHEDULER_cancel (fc->poll_task);
3714 fc->poll_task = NULL;
3720 * Get a (static) string for a connection.
3722 * @param c Connection.
3725 GCC_2s (const struct CadetConnection *c)
3732 static char buf[128];
3734 SPRINTF (buf, "%s (->%s)",
3735 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3738 return GNUNET_h2s (GC_h2hc (&c->id));
3743 * Log all possible info about the connection state.
3745 * @param c Connection to debug.
3746 * @param level Debug level to use.
3749 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3754 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3756 __FILE__, __FUNCTION__, __LINE__);
3762 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3766 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3767 s = path_2s (c->path);
3768 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3770 LOG2 (level, "CCC state: %s, destroy: %u\n",
3771 GCC_state2s (c->state), c->destroy);
3772 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3773 if (NULL != c->perf)
3774 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3776 LOG2 (level, "CCC FWD flow control:\n");
3777 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3778 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3779 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3780 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3781 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3782 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3783 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3784 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3786 LOG2 (level, "CCC BCK flow control:\n");
3787 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3788 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3789 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3790 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3791 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3792 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3793 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3794 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3796 LOG2 (level, "CCC DEBUG CONNECTION END\n");