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"
36 #define LOG(level, ...) GNUNET_log_from (level,"cadet-con",__VA_ARGS__)
37 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-con",__VA_ARGS__)
40 #define CADET_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
41 GNUNET_TIME_UNIT_MINUTES,\
46 /******************************************************************************/
47 /******************************** STRUCTS **********************************/
48 /******************************************************************************/
51 * Struct to encapsulate all the Flow Control information to a peer to which
52 * we are directly connected (on a core level).
54 struct CadetFlowControl
57 * Connection this controls.
59 struct CadetConnection *c;
62 * How many messages are in the queue on this connection.
67 * How many messages do we accept in the queue.
69 unsigned int queue_max;
72 * ID of the last packet sent towards the peer.
74 uint32_t last_pid_sent;
77 * ID of the last packet received from the peer.
79 uint32_t last_pid_recv;
82 * Bitmap of past 32 messages received:
83 * - LSB being @c last_pid_recv.
84 * - MSB being @c last_pid_recv - 31 (mod UINTMAX).
89 * Last ACK sent to the peer (peer can't send more than this PID).
91 uint32_t last_ack_sent;
94 * Last ACK sent towards the origin (for traffic towards leaf node).
96 uint32_t last_ack_recv;
99 * Task to poll the peer in case of a lost ACK causes stall.
101 struct GNUNET_SCHEDULER_Task *poll_task;
104 * How frequently to poll for ACKs.
106 struct GNUNET_TIME_Relative poll_time;
109 * Queued poll message, to cancel if not necessary anymore (got ACK).
111 struct CadetConnectionQueue *poll_msg;
114 * Queued poll message, to cancel if not necessary anymore (got ACK).
116 struct CadetConnectionQueue *ack_msg;
120 * Keep a record of the last messages sent on this connection.
122 struct CadetConnectionPerformance
125 * Circular buffer for storing measurements.
127 double usecsperbyte[AVG_MSGS];
130 * Running average of @c usecsperbyte.
135 * How many values of @c usecsperbyte are valid.
140 * Index of the next "free" position in @c usecsperbyte.
147 * Struct containing all information regarding a connection to a peer.
149 struct CadetConnection
152 * Tunnel this connection is part of.
154 struct CadetTunnel *t;
157 * Flow control information for traffic fwd.
159 struct CadetFlowControl fwd_fc;
162 * Flow control information for traffic bck.
164 struct CadetFlowControl bck_fc;
167 * Measure connection performance on the endpoint.
169 struct CadetConnectionPerformance *perf;
172 * ID of the connection.
174 struct GNUNET_CADET_Hash id;
177 * Path being used for the tunnel. At the origin of the connection
178 * it's a pointer to the destination's path pool, otherwise just a copy.
180 struct CadetPeerPath *path;
183 * Task to keep the used paths alive at the owner,
184 * time tunnel out on all the other peers.
186 struct GNUNET_SCHEDULER_Task *fwd_maintenance_task;
189 * Task to keep the used paths alive at the destination,
190 * time tunnel out on all the other peers.
192 struct GNUNET_SCHEDULER_Task *bck_maintenance_task;
195 * Queue handle for maintainance traffic. One handle for FWD and BCK since
196 * one peer never needs to maintain both directions (no loopback connections).
198 struct CadetPeerQueue *maintenance_q;
201 * Should equal #get_next_hop().
203 struct CadetPeer *next_peer;
206 * Should equal #get_prev_hop().
208 struct CadetPeer *prev_peer;
211 * State of the connection.
213 enum CadetConnectionState state;
216 * Position of the local peer in the path.
218 unsigned int own_pos;
221 * Pending message count.
223 unsigned int pending_messages;
227 * - if 0, connection in use.
228 * - if 1, destroy on last message.
229 * - if 2, connection is being destroyed don't re-enter.
234 * Counter to do exponential backoff when creating a connection (max 64).
236 unsigned short create_retry;
241 * Handle for messages queued but not yet sent.
243 struct CadetConnectionQueue
246 * Peer queue handle, to cancel if necessary.
248 struct CadetPeerQueue *q;
251 * Continuation to call once sent.
256 * Closure for @e cont.
261 * Was this a forced message? (Do not account for it)
266 /******************************************************************************/
267 /******************************* GLOBALS ***********************************/
268 /******************************************************************************/
271 * Global handle to the statistics service.
273 extern struct GNUNET_STATISTICS_Handle *stats;
276 * Local peer own ID (memory efficient handle).
278 extern GNUNET_PEER_Id myid;
281 * Local peer own ID (full value).
283 extern struct GNUNET_PeerIdentity my_full_id;
286 * Connections known, indexed by cid (CadetConnection).
288 static struct GNUNET_CONTAINER_MultiHashMap *connections;
291 * How many connections are we willing to maintain.
292 * Local connections are always allowed, even if there are more connections than max.
294 static unsigned long long max_connections;
297 * How many messages *in total* are we willing to queue, divide by number of
298 * connections to get connection queue size.
300 static unsigned long long max_msgs_queue;
303 * How often to send path keepalives. Paths timeout after 4 missed.
305 static struct GNUNET_TIME_Relative refresh_connection_time;
308 * How often to send path create / ACKs.
310 static struct GNUNET_TIME_Relative create_connection_time;
313 /******************************************************************************/
314 /******************************** STATIC ***********************************/
315 /******************************************************************************/
319 #if 0 // avoid compiler warning for unused static function
321 fc_debug (struct CadetFlowControl *fc)
323 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
324 fc->last_pid_recv, fc->last_ack_sent);
325 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
326 fc->last_pid_sent, fc->last_ack_recv);
327 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
328 fc->queue_n, fc->queue_max);
332 connection_debug (struct CadetConnection *c)
336 LOG (GNUNET_ERROR_TYPE_INFO, "DEBUG NULL CONNECTION\n");
339 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
340 peer2s (c->t->peer), GCC_2s (c));
341 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
342 c->state, c->pending_messages);
343 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
344 fc_debug (&c->fwd_fc);
345 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
346 fc_debug (&c->bck_fc);
352 * Schedule next keepalive task, taking in consideration
353 * the connection state and number of retries.
355 * @param c Connection for which to schedule the next keepalive.
356 * @param fwd Direction for the next keepalive.
359 schedule_next_keepalive (struct CadetConnection *c, int fwd);
363 * Resets the connection timeout task, some other message has done the
365 * - For the first peer on the direction this means to send
366 * a keepalive or a path confirmation message (either create or ACK).
367 * - For all other peers, this means to destroy the connection,
368 * due to lack of activity.
369 * Starts the timeout if no timeout was running (connection just created).
371 * @param c Connection whose timeout to reset.
372 * @param fwd Is this forward?
375 connection_reset_timeout (struct CadetConnection *c, int fwd);
379 * Get string description for tunnel state. Reentrant.
381 * @param s Tunnel state.
383 * @return String representation.
386 GCC_state2s (enum CadetConnectionState s)
390 case CADET_CONNECTION_NEW:
391 return "CADET_CONNECTION_NEW";
392 case CADET_CONNECTION_SENT:
393 return "CADET_CONNECTION_SENT";
394 case CADET_CONNECTION_ACK:
395 return "CADET_CONNECTION_ACK";
396 case CADET_CONNECTION_READY:
397 return "CADET_CONNECTION_READY";
398 case CADET_CONNECTION_DESTROYED:
399 return "CADET_CONNECTION_DESTROYED";
400 case CADET_CONNECTION_BROKEN:
401 return "CADET_CONNECTION_BROKEN";
404 LOG (GNUNET_ERROR_TYPE_ERROR, " conn state %u unknown!\n", s);
405 return "CADET_CONNECTION_STATE_ERROR";
411 * Initialize a Flow Control structure to the initial state.
413 * @param fc Flow Control structure to initialize.
416 fc_init (struct CadetFlowControl *fc)
418 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
419 fc->last_pid_recv = (uint32_t) -1;
420 fc->last_ack_sent = (uint32_t) 0;
421 fc->last_ack_recv = (uint32_t) 0;
422 fc->poll_task = NULL;
423 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
425 fc->queue_max = (max_msgs_queue / max_connections) + 1;
432 * @param cid Connection ID.
434 static struct CadetConnection *
435 connection_get (const struct GNUNET_CADET_Hash *cid)
437 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
442 connection_change_state (struct CadetConnection* c,
443 enum CadetConnectionState state)
445 LOG (GNUNET_ERROR_TYPE_DEBUG,
446 "Connection %s state %s -> %s\n",
447 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
448 if (CADET_CONNECTION_DESTROYED <= c->state) /* Destroyed or broken. */
450 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
454 if (CADET_CONNECTION_READY == state)
460 * Callback called when a queued ACK message is sent.
462 * @param cls Closure (FC).
463 * @param c Connection this message was on.
464 * @param q Queue handler this call invalidates.
465 * @param type Type of message sent.
466 * @param fwd Was this a FWD going message?
467 * @param size Size of the message.
471 struct CadetConnection *c,
472 struct CadetConnectionQueue *q,
473 uint16_t type, int fwd, size_t size)
475 struct CadetFlowControl *fc = cls;
482 * Send an ACK on the connection, informing the predecessor about
483 * the available buffer space. Should not be called in case the peer
484 * is origin (no predecessor) in the @c fwd direction.
486 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
487 * the ACK itself goes "back" (dest->root).
489 * @param c Connection on which to send the ACK.
490 * @param buffer How much space free to advertise?
491 * @param fwd Is this FWD ACK? (Going dest -> root)
492 * @param force Don't optimize out.
495 send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
497 struct CadetFlowControl *next_fc;
498 struct CadetFlowControl *prev_fc;
499 struct GNUNET_CADET_ACK msg;
503 /* If origin, there is no connection to send ACKs. Wrong function! */
504 if (GCC_is_origin (c, fwd))
506 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
507 GCC_2s (c), GC_f2s (fwd));
512 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
513 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
515 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
516 GC_f2s (fwd), GCC_2s (c));
518 /* Check if we need to transmit the ACK. */
519 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
520 if (3 < delta && buffer < delta && GNUNET_NO == force)
522 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
523 LOG (GNUNET_ERROR_TYPE_DEBUG,
524 " last pid recv: %u, last ack sent: %u\n",
525 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
529 /* Ok, ACK might be necessary, what PID to ACK? */
530 ack = prev_fc->last_pid_recv + buffer;
531 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
532 LOG (GNUNET_ERROR_TYPE_DEBUG,
533 " last pid %u, last ack %u, qmax %u, q %u\n",
534 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
535 next_fc->queue_max, next_fc->queue_n);
536 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
538 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
542 /* Check if message is already in queue */
543 if (NULL != prev_fc->ack_msg)
545 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
547 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
548 GCC_cancel (prev_fc->ack_msg);
549 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
553 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
558 prev_fc->last_ack_sent = ack;
560 /* Build ACK message and send on connection */
561 msg.header.size = htons (sizeof (msg));
562 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
563 msg.ack = htonl (ack);
566 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, 0, ack, c,
569 GNUNET_assert (NULL != prev_fc->ack_msg);
574 * Callback called when a connection queued message is sent.
576 * Calculates the average time and connection packet tracking.
578 * @param cls Closure (ConnectionQueue Handle).
579 * @param c Connection this message was on.
580 * @param sent Was it really sent? (Could have been canceled)
581 * @param type Type of message sent.
582 * @param pid Packet ID, or 0 if not applicable (create, destroy, etc).
583 * @param fwd Was this a FWD going message?
584 * @param size Size of the message.
585 * @param wait Time spent waiting for core (only the time for THIS message)
587 * @return #GNUNET_YES if connection was destroyed, #GNUNET_NO otherwise.
590 conn_message_sent (void *cls,
591 struct CadetConnection *c, int sent,
592 uint16_t type, uint32_t pid, int fwd, size_t size,
593 struct GNUNET_TIME_Relative wait)
595 struct CadetConnectionPerformance *p;
596 struct CadetFlowControl *fc;
597 struct CadetConnectionQueue *q = cls;
601 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
603 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
605 fc = fwd ? &c->fwd_fc : &c->bck_fc;
606 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
607 sent ? "" : "not ", GC_f2s (fwd), GC_m2s (type), pid);
613 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
614 q->cont (q->cont_cls, c, q, type, fwd, size);
618 else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED
619 || type == GNUNET_MESSAGE_TYPE_CADET_AX)
621 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
630 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
631 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
633 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
638 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
639 c->pending_messages--;
640 if ( (GNUNET_YES == c->destroy) &&
641 (0 == c->pending_messages) )
643 LOG (GNUNET_ERROR_TYPE_DEBUG,
644 "! destroying connection!\n");
648 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
651 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
652 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
653 c->maintenance_q = NULL;
654 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
655 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
656 schedule_next_keepalive (c, fwd);
659 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
660 case GNUNET_MESSAGE_TYPE_CADET_AX:
661 if (GNUNET_YES == sent)
663 GNUNET_assert (NULL != q);
664 fc->last_pid_sent = pid;
665 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
666 GCC_start_poll (c, fwd);
667 GCC_send_ack (c, fwd, GNUNET_NO);
668 connection_reset_timeout (c, fwd);
671 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
672 if (GNUNET_NO == forced)
675 LOG (GNUNET_ERROR_TYPE_DEBUG,
676 "! accounting pid %u\n",
681 LOG (GNUNET_ERROR_TYPE_DEBUG,
682 "! forced, Q_N not accounting pid %u\n",
687 case GNUNET_MESSAGE_TYPE_CADET_KX:
688 if (GNUNET_YES == sent)
689 connection_reset_timeout (c, fwd);
692 case GNUNET_MESSAGE_TYPE_CADET_POLL:
696 case GNUNET_MESSAGE_TYPE_CADET_ACK:
700 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
704 LOG (GNUNET_ERROR_TYPE_ERROR, "%s unknown\n", GC_m2s (type));
708 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
711 return GNUNET_NO; /* Only endpoints are interested in timing. */
714 usecsperbyte = ((double) wait.rel_value_us) / size;
715 if (p->size == AVG_MSGS)
717 /* Array is full. Substract oldest value, add new one and store. */
718 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
719 p->usecsperbyte[p->idx] = usecsperbyte;
720 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
724 /* Array not yet full. Add current value to avg and store. */
725 p->usecsperbyte[p->idx] = usecsperbyte;
727 p->avg += p->usecsperbyte[p->idx];
731 p->idx = (p->idx + 1) % AVG_MSGS;
737 * Get the previous hop in a connection
739 * @param c Connection.
741 * @return Previous peer in the connection.
743 static struct CadetPeer *
744 get_prev_hop (const struct CadetConnection *c)
750 LOG (GNUNET_ERROR_TYPE_DEBUG,
751 " get prev hop %s [%u/%u]\n",
752 GCC_2s (c), c->own_pos, c->path->length);
753 if (0 == c->own_pos || c->path->length < 2)
754 id = c->path->peers[0];
756 id = c->path->peers[c->own_pos - 1];
758 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
759 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
761 return GCP_get_short (id);
766 * Get the next hop in a connection
768 * @param c Connection.
770 * @return Next peer in the connection.
772 static struct CadetPeer *
773 get_next_hop (const struct CadetConnection *c)
780 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
781 GCC_2s (c), c->own_pos, c->path->length);
782 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
783 id = c->path->peers[c->path->length - 1];
785 id = c->path->peers[c->own_pos + 1];
787 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
788 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
790 return GCP_get_short (id);
795 * Check that the direct neighbours (previous and next hop)
796 * are properly associated with this connection.
798 * @param c connection to check
801 check_neighbours (const struct CadetConnection *c)
804 return; /* nothing to check */
805 GCP_check_connection (get_next_hop (c), c);
806 GCP_check_connection (get_prev_hop (c), c);
810 * Helper for #check_connections(). Calls #check_neighbours().
814 * @param value the `struct CadetConnection` to check
815 * @return #GNUNET_OK (continue to iterate)
818 check_connection (void *cls,
819 const struct GNUNET_HashCode *key,
822 struct CadetConnection *c = value;
824 check_neighbours (c);
830 * Check all connections using #check_neighbours().
835 GNUNET_CONTAINER_multihashmap_iterate (connections,
842 * Get the hop in a connection.
844 * @param c Connection.
845 * @param fwd Next in the FWD direction?
847 * @return Next peer in the connection.
849 static struct CadetPeer *
850 get_hop (struct CadetConnection *c, int fwd)
853 return get_next_hop (c);
854 return get_prev_hop (c);
859 * Get a bit mask for a message received out-of-order.
861 * @param last_pid_recv Last PID we received prior to the out-of-order.
862 * @param ooo_pid PID of the out-of-order message.
865 get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
867 return 1 << (last_pid_recv - ooo_pid);
872 * Check is an out-of-order message is ok:
873 * - at most 31 messages behind.
876 * @param last_pid_recv Last in-order PID received.
879 is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
883 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
886 mask = get_recv_bitmask (last_pid_recv, ooo_pid);
887 if (0 != (ooo_bitmap & mask))
895 * Is traffic coming from this sender 'FWD' traffic?
897 * @param c Connection to check.
898 * @param sender Peer identity of neighbor.
900 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
901 * the traffic is 'FWD'.
902 * #GNUNET_NO for BCK.
903 * #GNUNET_SYSERR for errors.
906 is_fwd (const struct CadetConnection *c,
907 const struct GNUNET_PeerIdentity *sender)
911 id = GNUNET_PEER_search (sender);
912 if (GCP_get_short_id (get_prev_hop (c)) == id)
915 if (GCP_get_short_id (get_next_hop (c)) == id)
919 return GNUNET_SYSERR;
924 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
925 * or a first CONNECTION_ACK directed to us.
927 * @param connection Connection to confirm.
928 * @param fwd Should we send it FWD? (root->dest)
929 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
932 send_connection_ack (struct CadetConnection *connection, int fwd)
934 struct CadetTunnel *t;
937 LOG (GNUNET_ERROR_TYPE_INFO, "---> {%14s ACK} on connection %s\n",
938 GC_f2s (!fwd), GCC_2s (connection));
939 GCP_queue_add (get_hop (connection, fwd), NULL,
940 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0, 0,
941 sizeof (struct GNUNET_CADET_ConnectionACK),
942 connection, fwd, &conn_message_sent, NULL);
943 connection->pending_messages++;
944 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
945 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
946 if (CADET_CONNECTION_READY != connection->state)
947 connection_change_state (connection, CADET_CONNECTION_SENT);
952 * Send a notification that a connection is broken.
954 * @param c Connection that is broken.
955 * @param id1 Peer that has disconnected.
956 * @param id2 Peer that has disconnected.
957 * @param fwd Direction towards which to send it.
960 send_broken (struct CadetConnection *c,
961 const struct GNUNET_PeerIdentity *id1,
962 const struct GNUNET_PeerIdentity *id2,
965 struct GNUNET_CADET_ConnectionBroken msg;
967 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
968 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
972 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c, fwd,
973 GNUNET_YES, NULL, NULL));
978 * Send a notification that a connection is broken, when a connection
979 * isn't even known to the local peer or soon to be destroyed.
981 * @param connection_id Connection ID.
982 * @param id1 Peer that has disconnected, probably local peer.
983 * @param id2 Peer that has disconnected can be NULL if unknown.
984 * @param peer Peer to notify (neighbor who sent the connection).
987 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
988 const struct GNUNET_PeerIdentity *id1,
989 const struct GNUNET_PeerIdentity *id2,
990 const struct GNUNET_PeerIdentity *peer_id)
992 struct GNUNET_CADET_ConnectionBroken *msg;
993 struct CadetPeer *neighbor;
995 LOG (GNUNET_ERROR_TYPE_INFO, "---> BROKEN on unknown connection %s\n",
996 GNUNET_h2s (GC_h2hc (connection_id)));
998 msg = GNUNET_new (struct GNUNET_CADET_ConnectionBroken);
999 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1000 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1001 msg->cid = *connection_id;
1006 memset (&msg->peer2, 0, sizeof (msg->peer2));
1007 neighbor = GCP_get (peer_id);
1008 GCP_queue_add (neighbor, msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
1009 0, 2, sizeof (struct GNUNET_CADET_ConnectionBroken),
1010 NULL, GNUNET_SYSERR, /* connection, fwd */
1011 NULL, NULL); /* continuation */
1016 * Send keepalive packets for a connection.
1018 * @param c Connection to keep alive..
1019 * @param fwd Is this a FWD keepalive? (owner -> dest).
1022 send_connection_keepalive (struct CadetConnection *c, int fwd)
1024 struct GNUNET_MessageHeader msg;
1025 struct CadetFlowControl *fc;
1027 LOG (GNUNET_ERROR_TYPE_INFO, "keepalive %s for connection %s\n",
1028 GC_f2s (fwd), GCC_2s (c));
1030 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1031 if (0 < fc->queue_n)
1033 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1037 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1039 GNUNET_assert (NULL != c->t);
1040 msg.size = htons (sizeof (msg));
1041 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
1043 GNUNET_assert (NULL ==
1044 GCT_send_prebuilt_message (&msg, c->t, c,
1045 GNUNET_NO, NULL, NULL));
1050 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1052 * @param c Connection for which to send the message.
1053 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1056 connection_recreate (struct CadetConnection *c, int fwd)
1058 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
1060 GCC_send_create (c);
1062 send_connection_ack (c, GNUNET_NO);
1067 * Generic connection timer management.
1068 * Depending on the role of the peer in the connection will send the
1069 * appropriate message (build or keepalive)
1071 * @param c Conncetion to maintain.
1072 * @param fwd Is FWD?
1075 connection_maintain (struct CadetConnection *c, int fwd)
1077 if (GNUNET_NO != c->destroy)
1079 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1086 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1090 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1092 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1094 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1095 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1096 schedule_next_keepalive (c, fwd);
1101 case CADET_CONNECTION_NEW:
1104 case CADET_CONNECTION_SENT:
1105 connection_recreate (c, fwd);
1107 case CADET_CONNECTION_READY:
1108 send_connection_keepalive (c, fwd);
1118 * Keep the connection alive.
1120 * @param c Connection to keep alive.
1121 * @param fwd Direction.
1122 * @param shutdown Are we shutting down? (Don't send traffic)
1123 * Non-zero value for true, not necessarily GNUNET_YES.
1126 connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
1128 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s keepalive for %s\n",
1129 GC_f2s (fwd), GCC_2s (c));
1132 c->fwd_maintenance_task = NULL;
1134 c->bck_maintenance_task = NULL;
1136 if (GNUNET_NO != shutdown)
1139 connection_maintain (c, fwd);
1141 /* Next execution will be scheduled by message_sent or _maintain*/
1146 * Keep the connection alive in the FWD direction.
1148 * @param cls Closure (connection to keepalive).
1149 * @param tc TaskContext.
1152 connection_fwd_keepalive (void *cls,
1153 const struct GNUNET_SCHEDULER_TaskContext *tc)
1155 connection_keepalive ((struct CadetConnection *) cls,
1157 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1162 * Keep the connection alive in the BCK direction.
1164 * @param cls Closure (connection to keepalive).
1165 * @param tc TaskContext.
1168 connection_bck_keepalive (void *cls,
1169 const struct GNUNET_SCHEDULER_TaskContext *tc)
1171 connection_keepalive ((struct CadetConnection *) cls,
1173 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1178 * Schedule next keepalive task, taking in consideration
1179 * the connection state and number of retries.
1181 * If the peer is not the origin, do nothing.
1183 * @param c Connection for which to schedule the next keepalive.
1184 * @param fwd Direction for the next keepalive.
1187 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1189 struct GNUNET_TIME_Relative delay;
1190 struct GNUNET_SCHEDULER_Task * *task_id;
1191 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1193 if (GNUNET_NO == GCC_is_origin (c, fwd))
1196 /* Calculate delay to use, depending on the state of the connection */
1197 if (CADET_CONNECTION_READY == c->state)
1199 delay = refresh_connection_time;
1203 if (1 > c->create_retry)
1204 c->create_retry = 1;
1205 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1207 if (c->create_retry < 64)
1208 c->create_retry *= 2;
1211 /* Select direction-dependent parameters */
1212 if (GNUNET_YES == fwd)
1214 task_id = &c->fwd_maintenance_task;
1215 keepalive_task = &connection_fwd_keepalive;
1219 task_id = &c->bck_maintenance_task;
1220 keepalive_task = &connection_bck_keepalive;
1223 /* Check that no one scheduled it before us */
1224 if (NULL != *task_id)
1226 /* No need for a _break. It can happen for instance when sending a SYNACK
1227 * for a duplicate SYN: the first SYNACK scheduled the task. */
1228 GNUNET_SCHEDULER_cancel (*task_id);
1231 /* Schedule the task */
1232 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1234 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1239 * @brief Re-initiate traffic on this connection if necessary.
1241 * Check if there is traffic queued towards this peer
1242 * and the core transmit handle is NULL (traffic was stalled).
1243 * If so, call core tmt rdy.
1245 * @param c Connection on which initiate traffic.
1246 * @param fwd Is this about fwd traffic?
1249 connection_unlock_queue (struct CadetConnection *c, int fwd)
1251 struct CadetPeer *peer;
1253 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_unlock_queue %s on %s\n",
1254 GC_f2s (fwd), GCC_2s (c));
1256 if (GCC_is_terminal (c, fwd))
1258 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal, can unlock!\n");
1262 peer = get_hop (c, fwd);
1263 GCP_queue_unlock (peer, c);
1268 * Cancel all transmissions that belong to a certain connection.
1270 * If the connection is scheduled for destruction and no more messages are left,
1271 * the connection will be destroyed by the continuation call.
1273 * @param c Connection which to cancel. Might be destroyed during this call.
1274 * @param fwd Cancel fwd traffic?
1277 connection_cancel_queues (struct CadetConnection *c, int fwd)
1279 struct CadetFlowControl *fc;
1280 struct CadetPeer *peer;
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cancel %s queues for connection %s\n",
1283 GC_f2s (fwd), GCC_2s (c));
1290 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1291 if (NULL != fc->poll_task)
1293 GNUNET_SCHEDULER_cancel (fc->poll_task);
1294 fc->poll_task = NULL;
1295 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cancel POLL in ccq for fc %p\n", fc);
1297 peer = get_hop (c, fwd);
1298 GCP_queue_cancel (peer, c);
1303 * Function called if a connection has been stalled for a while,
1304 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1306 * @param cls Closure (poll ctx).
1307 * @param tc TaskContext.
1310 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1314 * Callback called when a queued POLL message is sent.
1316 * @param cls Closure (FC).
1317 * @param c Connection this message was on.
1318 * @param q Queue handler this call invalidates.
1319 * @param type Type of message sent.
1320 * @param fwd Was this a FWD going message?
1321 * @param size Size of the message.
1324 poll_sent (void *cls,
1325 struct CadetConnection *c,
1326 struct CadetConnectionQueue *q,
1327 uint16_t type, int fwd, size_t size)
1329 struct CadetFlowControl *fc = cls;
1331 if (2 == c->destroy)
1333 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1336 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1338 fc->poll_msg = NULL;
1339 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1340 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1341 &connection_poll, fc);
1342 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1347 * Function called if a connection has been stalled for a while,
1348 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1350 * @param cls Closure (poll ctx).
1351 * @param tc TaskContext.
1354 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1356 struct CadetFlowControl *fc = cls;
1357 struct GNUNET_CADET_Poll msg;
1358 struct CadetConnection *c;
1361 fc->poll_task = NULL;
1362 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1368 fwd = fc == &c->fwd_fc;
1369 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1370 GCC_2s (c), GC_f2s (fwd));
1372 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1373 msg.header.size = htons (sizeof (msg));
1374 msg.pid = htonl (fc->last_pid_sent);
1375 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1377 GCC_send_prebuilt_message (&msg.header, 0, fc->last_pid_sent, c,
1378 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1379 GNUNET_assert (NULL != fc->poll_msg);
1384 * Resend all queued messages for a connection on other connections of the
1385 * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
1387 * @param c Connection whose messages to resend.
1388 * @param fwd Resend fwd messages?
1391 resend_messages_and_destroy (struct CadetConnection *c, int fwd)
1393 struct GNUNET_MessageHeader *out_msg;
1394 struct CadetTunnel *t = c->t;
1395 struct CadetPeer *neighbor;
1396 unsigned int pending;
1399 c->state = CADET_CONNECTION_DESTROYED;
1400 c->destroy = GNUNET_YES;
1402 destroyed = GNUNET_NO;
1403 neighbor = get_hop (c, fwd);
1404 pending = c->pending_messages;
1406 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1409 GCT_resend_message (out_msg, t);
1410 GNUNET_free (out_msg);
1413 /* All pending messages should have been popped,
1414 * and the connection destroyed by the continuation.
1416 if (GNUNET_YES != destroyed)
1421 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1422 if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1430 * Generic connection timeout implementation.
1432 * Timeout function due to lack of keepalive/traffic from an endpoint.
1433 * Destroys connection if called.
1435 * @param c Connection to destroy.
1436 * @param fwd Was the timeout from the origin? (FWD timeout)
1439 connection_timeout (struct CadetConnection *c, int fwd)
1441 struct CadetFlowControl *reverse_fc;
1443 reverse_fc = fwd ? &c->bck_fc : &c->fwd_fc;
1445 LOG (GNUNET_ERROR_TYPE_INFO,
1446 "Connection %s %s timed out. Destroying.\n",
1449 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1451 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1457 /* If dest, salvage queued traffic. */
1458 if (GCC_is_origin (c, !fwd))
1460 const struct GNUNET_PeerIdentity *next_hop;
1462 next_hop = GCP_get_id (fwd ? get_prev_hop (c) : get_next_hop (c));
1463 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1464 if (0 < reverse_fc->queue_n)
1465 resend_messages_and_destroy (c, !fwd);
1474 * Timeout function due to lack of keepalive/traffic from the owner.
1475 * Destroys connection if called.
1477 * @param cls Closure (connection to destroy).
1478 * @param tc TaskContext.
1481 connection_fwd_timeout (void *cls,
1482 const struct GNUNET_SCHEDULER_TaskContext *tc)
1484 struct CadetConnection *c = cls;
1486 c->fwd_maintenance_task = NULL;
1487 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1490 connection_timeout (c, GNUNET_YES);
1495 * Timeout function due to lack of keepalive/traffic from the destination.
1496 * Destroys connection if called.
1498 * @param cls Closure (connection to destroy).
1499 * @param tc TaskContext
1502 connection_bck_timeout (void *cls,
1503 const struct GNUNET_SCHEDULER_TaskContext *tc)
1505 struct CadetConnection *c = cls;
1507 c->bck_maintenance_task = NULL;
1508 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1511 connection_timeout (c, GNUNET_NO);
1516 * Resets the connection timeout task, some other message has done the
1518 * - For the first peer on the direction this means to send
1519 * a keepalive or a path confirmation message (either create or ACK).
1520 * - For all other peers, this means to destroy the connection,
1521 * due to lack of activity.
1522 * Starts the timeout if no timeout was running (connection just created).
1524 * @param c Connection whose timeout to reset.
1525 * @param fwd Is this forward?
1527 * TODO use heap to improve efficiency of scheduler.
1530 connection_reset_timeout (struct CadetConnection *c, int fwd)
1532 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1534 if (GCC_is_origin (c, fwd)) /* Startpoint */
1536 schedule_next_keepalive (c, fwd);
1538 else /* Relay, endpoint. */
1540 struct GNUNET_TIME_Relative delay;
1541 struct GNUNET_SCHEDULER_Task * *ti;
1542 GNUNET_SCHEDULER_TaskCallback f;
1544 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1547 GNUNET_SCHEDULER_cancel (*ti);
1548 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1549 LOG (GNUNET_ERROR_TYPE_DEBUG, " timing out in %s\n",
1550 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1551 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1552 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1558 * Add the connection to the list of both neighbors.
1560 * @param c Connection.
1562 * @return #GNUNET_OK if everything went fine
1563 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1566 register_neighbors (struct CadetConnection *c)
1568 c->next_peer = get_next_hop (c);
1569 c->prev_peer = get_prev_hop (c);
1570 GNUNET_assert (c->next_peer != c->prev_peer);
1571 LOG (GNUNET_ERROR_TYPE_DEBUG,
1572 "register neighbors for connection %s\n",
1574 path_debug (c->path);
1575 LOG (GNUNET_ERROR_TYPE_DEBUG,
1576 "own pos %u\n", c->own_pos);
1577 LOG (GNUNET_ERROR_TYPE_DEBUG,
1578 "putting connection %s to next peer %p\n",
1581 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1583 GCP_2s (c->next_peer));
1584 LOG (GNUNET_ERROR_TYPE_DEBUG,
1585 "putting connection %s to prev peer %p\n",
1588 LOG (GNUNET_ERROR_TYPE_DEBUG,
1589 "prev peer %p %s\n",
1591 GCP_2s (c->prev_peer));
1593 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1594 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1596 if (GCC_is_origin (c, GNUNET_YES))
1597 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1598 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1600 LOG (GNUNET_ERROR_TYPE_DEBUG,
1601 " register neighbors failed\n");
1602 LOG (GNUNET_ERROR_TYPE_DEBUG,
1603 " prev: %s, neighbor?: %d\n",
1604 GCP_2s (c->prev_peer),
1605 GCP_is_neighbor (c->prev_peer));
1606 LOG (GNUNET_ERROR_TYPE_DEBUG,
1607 " next: %s, neighbor?: %d\n",
1608 GCP_2s (c->next_peer),
1609 GCP_is_neighbor (c->next_peer));
1610 return GNUNET_SYSERR;
1612 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1613 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1620 * Remove the connection from the list of both neighbors.
1622 * @param c Connection.
1625 unregister_neighbors (struct CadetConnection *c)
1627 struct CadetPeer *peer;
1629 /* Either already unregistered or never got registered, it's ok either way. */
1630 if (NULL == c->path)
1633 peer = get_next_hop (c);
1634 GNUNET_assert (c->next_peer == peer);
1635 GCP_remove_connection (peer, c);
1636 peer = get_prev_hop (c);
1637 GNUNET_assert (c->prev_peer == peer);
1638 GCP_remove_connection (peer, c);
1643 * Bind the connection to the peer and the tunnel to that peer.
1645 * If the peer has no tunnel, create one. Update tunnel and connection
1646 * data structres to reflect new status.
1648 * @param c Connection.
1652 add_to_peer (struct CadetConnection *c,
1653 struct CadetPeer *peer)
1655 GCP_add_tunnel (peer);
1656 c->t = GCP_get_tunnel (peer);
1657 GCT_add_connection (c->t, c);
1663 * Iterator to compare each connection's path with the path of a new connection.
1665 * If the connection conincides, the c member of path is set to the connection
1666 * and the destroy flag of the connection is set.
1668 * @param cls Closure (new path).
1669 * @param c Connection in the tunnel to check.
1672 check_path (void *cls, struct CadetConnection *c)
1674 struct CadetConnection *new_conn = cls;
1675 struct CadetPeerPath *path = new_conn->path;
1677 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1678 GCC_2s (c), c, c->path->length);
1681 && c->destroy == GNUNET_NO
1682 && c->state != CADET_CONNECTION_BROKEN
1683 && c->state != CADET_CONNECTION_DESTROYED
1684 && path_equivalent (path, c->path))
1686 new_conn->destroy = GNUNET_YES;
1687 new_conn->path->c = c;
1688 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1693 * Finds out if this path is already being used by and existing connection.
1695 * Checks the tunnel towards the first peer in the path to see if it contains
1696 * any connection with the same path.
1698 * If the existing connection is ready, it is kept.
1699 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1700 * the peer will eventually reject our attempt).
1702 * @param path Path to check.
1704 * @return GNUNET_YES if the tunnel has a connection with the same path,
1705 * GNUNET_NO otherwise.
1708 does_connection_exist (struct CadetConnection *conn)
1710 struct CadetPeer *p;
1711 struct CadetTunnel *t;
1712 struct CadetConnection *c;
1714 p = GCP_get_short (conn->path->peers[0]);
1715 t = GCP_get_tunnel (p);
1719 LOG (GNUNET_ERROR_TYPE_DEBUG,
1720 "Checking for duplicates\n");
1722 GCT_iterate_connections (t, &check_path, conn);
1724 if (GNUNET_YES == conn->destroy)
1727 conn->destroy = GNUNET_NO;
1728 conn->path->c = conn;
1729 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1730 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1731 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1732 if (CADET_CONNECTION_READY == c->state)
1734 /* The other peer confirmed a live connection with this path,
1735 * why is it trying to duplicate it. */
1739 if (GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GCP_get_id (p)) > 0)
1741 struct CadetPeer *neighbor;
1743 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate allowed (killing old)\n");
1744 if (GCC_is_origin (c, GNUNET_YES))
1745 neighbor = get_next_hop (c);
1747 neighbor = get_prev_hop (c);
1748 send_broken_unknown (&c->id, &my_full_id, NULL,
1749 GCP_get_id (neighbor));
1758 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1765 * Log receipt of message on stderr (INFO level).
1767 * @param message Message received.
1768 * @param peer Peer who sent the message.
1769 * @param hash Connection ID.
1772 log_message (const struct GNUNET_MessageHeader *message,
1773 const struct GNUNET_PeerIdentity *peer,
1774 const struct GNUNET_CADET_Hash *hash)
1778 size = ntohs (message->size);
1779 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1780 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1781 LOG (GNUNET_ERROR_TYPE_INFO, "<-- %s on connection %s from %s, %6u bytes\n",
1782 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (hash)),
1783 GNUNET_i2s (peer), (unsigned int) size);
1786 /******************************************************************************/
1787 /******************************** API ***********************************/
1788 /******************************************************************************/
1791 * Core handler for connection creation.
1793 * @param cls Closure (unused).
1794 * @param peer Sender (neighbor).
1795 * @param message Message.
1796 * @return #GNUNET_OK to keep the connection open,
1797 * #GNUNET_SYSERR to close it (signal serious error)
1800 GCC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1801 const struct GNUNET_MessageHeader *message)
1803 struct GNUNET_CADET_ConnectionCreate *msg;
1804 struct GNUNET_PeerIdentity *id;
1805 struct GNUNET_CADET_Hash *cid;
1806 struct CadetPeerPath *path;
1807 struct CadetPeer *dest_peer;
1808 struct CadetPeer *orig_peer;
1809 struct CadetConnection *c;
1810 unsigned int own_pos;
1813 check_connections ();
1815 size = ntohs (message->size);
1816 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1818 GNUNET_break_op (0);
1822 /* Calculate hops */
1823 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1824 if (size % sizeof (struct GNUNET_PeerIdentity))
1826 GNUNET_break_op (0);
1829 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1831 GNUNET_break_op (0);
1834 size /= sizeof (struct GNUNET_PeerIdentity);
1837 GNUNET_break_op (0);
1840 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1842 /* Get parameters */
1843 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
1845 log_message (message, peer, cid);
1846 id = (struct GNUNET_PeerIdentity *) &msg[1];
1847 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1849 /* Create connection */
1850 c = connection_get (cid);
1853 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1854 size, myid, &own_pos);
1857 /* Path was malformed, probably our own ID was not in it. */
1858 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
1859 GNUNET_break_op (0);
1865 /* We received this request from a neighbor, we cannot be origin */
1866 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
1867 GNUNET_break_op (0);
1868 path_destroy (path);
1872 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1873 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1874 c = GCC_new (cid, NULL, path, own_pos);
1877 if (path->length - 1 == own_pos)
1879 /* If we are destination, why did the creation fail? */
1881 path_destroy (path);
1882 check_connections ();
1885 send_broken_unknown (cid, &my_full_id,
1886 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1888 path_destroy (path);
1889 check_connections ();
1892 GCP_add_path_to_all (path, GNUNET_NO);
1893 connection_reset_timeout (c, GNUNET_YES);
1897 path = path_duplicate (c->path);
1899 if (CADET_CONNECTION_NEW == c->state)
1900 connection_change_state (c, CADET_CONNECTION_SENT);
1902 /* Remember peers */
1903 dest_peer = GCP_get (&id[size - 1]);
1904 orig_peer = GCP_get (&id[0]);
1906 /* Is it a connection to us? */
1907 if (c->own_pos == path->length - 1)
1909 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1910 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1912 add_to_peer (c, orig_peer);
1913 if (GNUNET_YES == does_connection_exist (c))
1915 // FIXME Peer created a connection equal to one we think exists
1916 // and is fine. What should we do?
1917 // Use explicit duplicate?
1918 // Accept new conn and destroy the old? (interruption in higher level)
1919 // Keep both and postpone disambiguation?
1920 // Keep the one created by peer with higher ID?
1921 // For now: reject new connection until current confirmed dead
1922 GNUNET_break_op (0);
1923 GCC_debug (c, GNUNET_ERROR_TYPE_WARNING);
1924 path_destroy (path);
1926 send_broken_unknown (cid, &my_full_id, NULL, peer);
1927 check_connections ();
1931 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
1932 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
1934 send_connection_ack (c, GNUNET_NO);
1935 if (CADET_CONNECTION_SENT == c->state)
1936 connection_change_state (c, CADET_CONNECTION_ACK);
1940 /* It's for somebody else! Retransmit. */
1941 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1942 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1943 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1944 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
1945 GNUNET_YES, GNUNET_YES,
1948 path_destroy (path);
1949 check_connections ();
1955 * Core handler for path confirmations.
1957 * @param cls closure
1958 * @param message message
1959 * @param peer peer identity this notification is about
1960 * @return #GNUNET_OK to keep the connection open,
1961 * #GNUNET_SYSERR to close it (signal serious error)
1964 GCC_handle_confirm (void *cls,
1965 const struct GNUNET_PeerIdentity *peer,
1966 const struct GNUNET_MessageHeader *message)
1968 struct GNUNET_CADET_ConnectionACK *msg;
1969 struct CadetConnection *c;
1970 struct CadetPeerPath *p;
1971 struct CadetPeer *pi;
1972 enum CadetConnectionState oldstate;
1975 check_connections ();
1976 msg = (struct GNUNET_CADET_ConnectionACK *) message;
1977 log_message (message, peer, &msg->cid);
1978 c = connection_get (&msg->cid);
1981 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1983 LOG (GNUNET_ERROR_TYPE_DEBUG,
1984 " don't know the connection!\n");
1985 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
1986 check_connections ();
1990 if (GNUNET_NO != c->destroy)
1992 LOG (GNUNET_ERROR_TYPE_DEBUG,
1993 " connection being destroyed\n");
1994 check_connections ();
1998 oldstate = c->state;
1999 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
2000 pi = GCP_get (peer);
2001 if (get_next_hop (c) == pi)
2003 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2005 if (CADET_CONNECTION_SENT == oldstate)
2006 connection_change_state (c, CADET_CONNECTION_ACK);
2008 else if (get_prev_hop (c) == pi)
2010 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2012 connection_change_state (c, CADET_CONNECTION_READY);
2016 GNUNET_break_op (0);
2020 connection_reset_timeout (c, fwd);
2022 /* Add path to peers? */
2026 GCP_add_path_to_all (p, GNUNET_YES);
2033 /* Message for us as creator? */
2034 if (GCC_is_origin (c, GNUNET_YES))
2036 if (GNUNET_NO != fwd)
2038 GNUNET_break_op (0);
2041 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2043 /* If just created, cancel the short timeout and start a long one */
2044 if (CADET_CONNECTION_SENT == oldstate)
2045 connection_reset_timeout (c, GNUNET_YES);
2047 /* Change connection state */
2048 connection_change_state (c, CADET_CONNECTION_READY);
2049 send_connection_ack (c, GNUNET_YES);
2051 /* Change tunnel state, trigger KX */
2052 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2053 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2054 check_connections ();
2058 /* Message for us as destination? */
2059 if (GCC_is_terminal (c, GNUNET_YES))
2061 if (GNUNET_YES != fwd)
2063 GNUNET_break_op (0);
2066 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2068 /* If just created, cancel the short timeout and start a long one */
2069 if (CADET_CONNECTION_ACK == oldstate)
2070 connection_reset_timeout (c, GNUNET_NO);
2072 /* Change tunnel state */
2073 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2074 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2075 check_connections ();
2079 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2080 GNUNET_assert (NULL ==
2081 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2082 GNUNET_YES, NULL, NULL));
2083 check_connections ();
2089 * Core handler for notifications of broken connections.
2091 * @param cls Closure (unused).
2092 * @param id Peer identity of sending neighbor.
2093 * @param message Message.
2094 * @return #GNUNET_OK to keep the connection open,
2095 * #GNUNET_SYSERR to close it (signal serious error)
2098 GCC_handle_broken (void* cls,
2099 const struct GNUNET_PeerIdentity* id,
2100 const struct GNUNET_MessageHeader* message)
2102 struct GNUNET_CADET_ConnectionBroken *msg;
2103 struct CadetConnection *c;
2104 struct CadetTunnel *t;
2108 check_connections ();
2109 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
2110 log_message (message, id, &msg->cid);
2111 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2112 GNUNET_i2s (&msg->peer1));
2113 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2114 GNUNET_i2s (&msg->peer2));
2115 c = connection_get (&msg->cid);
2118 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2119 check_connections ();
2125 fwd = is_fwd (c, id);
2126 c->destroy = GNUNET_YES;
2127 if (GCC_is_terminal (c, fwd))
2129 struct CadetPeer *endpoint;
2133 /* A terminal connection should not have 't' set to NULL. */
2135 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2138 endpoint = GCP_get_short (c->path->peers[c->path->length - 1]);
2139 if (2 < c->path->length)
2140 path_invalidate (c->path);
2141 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2143 c->state = CADET_CONNECTION_BROKEN;
2144 GCT_remove_connection (t, c);
2147 pending = c->pending_messages;
2149 resend_messages_and_destroy (c, !fwd);
2155 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2156 GNUNET_YES, NULL, NULL));
2157 connection_cancel_queues (c, !fwd);
2159 check_connections ();
2165 * Core handler for tunnel destruction
2167 * @param cls Closure (unused).
2168 * @param peer Peer identity of sending neighbor.
2169 * @param message Message.
2170 * @return #GNUNET_OK to keep the connection open,
2171 * #GNUNET_SYSERR to close it (signal serious error)
2174 GCC_handle_destroy (void *cls,
2175 const struct GNUNET_PeerIdentity *peer,
2176 const struct GNUNET_MessageHeader *message)
2178 const struct GNUNET_CADET_ConnectionDestroy *msg;
2179 struct CadetConnection *c;
2182 check_connections ();
2183 msg = (const struct GNUNET_CADET_ConnectionDestroy *) message;
2184 log_message (message, peer, &msg->cid);
2185 c = connection_get (&msg->cid);
2188 /* Probably already got the message from another path,
2189 * destroyed the tunnel and retransmitted to children.
2192 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2194 LOG (GNUNET_ERROR_TYPE_DEBUG,
2195 " connection unknown: already destroyed?\n");
2196 check_connections ();
2199 fwd = is_fwd (c, peer);
2200 if (GNUNET_SYSERR == fwd)
2202 GNUNET_break_op (0); /* FIXME */
2205 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2206 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2207 GNUNET_YES, NULL, NULL));
2208 else if (0 == c->pending_messages)
2210 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
2212 check_connections ();
2215 c->destroy = GNUNET_YES;
2216 c->state = CADET_CONNECTION_DESTROYED;
2219 GCT_remove_connection (c->t, c);
2222 check_connections ();
2229 * Check the message against internal state and test if it goes FWD or BCK.
2231 * Updates the PID, state and timeout values for the connection.
2233 * @param message Message to check. It must belong to an existing connection.
2234 * @param minimum_size The message cannot be smaller than this value.
2235 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2236 * @param c Connection this message should belong. If NULL, check fails.
2237 * @param neighbor Neighbor that sent the message.
2240 check_message (const struct GNUNET_MessageHeader *message,
2241 size_t minimum_size,
2242 const struct GNUNET_CADET_Hash* cid,
2243 struct CadetConnection *c,
2244 const struct GNUNET_PeerIdentity *neighbor,
2247 GNUNET_PEER_Id neighbor_id;
2248 struct CadetFlowControl *fc;
2249 struct CadetPeer *hop;
2254 if (ntohs (message->size) < minimum_size)
2256 GNUNET_break_op (0);
2257 LOG (GNUNET_ERROR_TYPE_WARNING, "Size %u < %u\n",
2258 ntohs (message->size), minimum_size);
2259 return GNUNET_SYSERR;
2262 /* Check connection */
2265 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
2266 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s on unknown connection %s\n",
2267 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (cid)));
2268 send_broken_unknown (cid, &my_full_id, NULL, neighbor);
2269 return GNUNET_SYSERR;
2272 /* Check if origin is as expected */
2273 neighbor_id = GNUNET_PEER_search (neighbor);
2274 hop = get_prev_hop (c);
2275 if (neighbor_id == GCP_get_short_id (hop))
2281 hop = get_next_hop (c);
2282 GNUNET_break (hop == c->next_peer);
2283 if (neighbor_id == GCP_get_short_id (hop))
2289 /* Unexpected peer sending traffic on a connection. */
2290 GNUNET_break_op (0);
2291 return GNUNET_SYSERR;
2295 /* Check PID for payload messages */
2296 type = ntohs (message->type);
2297 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
2298 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
2300 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2301 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2302 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2303 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2305 GNUNET_break_op (0);
2306 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2307 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2308 pid, fc->last_pid_recv, fc->last_ack_sent);
2309 return GNUNET_SYSERR;
2311 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2315 delta = pid - fc->last_pid_recv;
2316 fc->last_pid_recv = pid;
2317 fc->recv_bitmap <<= delta;
2318 fc->recv_bitmap |= 1;
2322 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2323 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2325 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2326 pid, fc->last_pid_recv - 31);
2327 return GNUNET_SYSERR;
2329 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2333 /* Count as connection confirmation. */
2334 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2336 connection_change_state (c, CADET_CONNECTION_READY);
2339 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2340 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2343 connection_reset_timeout (c, fwd);
2350 * Generic handler for cadet network encrypted traffic.
2352 * @param peer Peer identity this notification is about.
2353 * @param msg Encrypted message.
2354 * @return #GNUNET_OK to keep the connection open,
2355 * #GNUNET_SYSERR to close it (signal serious error)
2358 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
2359 const struct GNUNET_MessageHeader *message)
2361 const struct GNUNET_CADET_Encrypted *otr_msg;
2362 const struct GNUNET_CADET_AX *ax_msg;
2363 const struct GNUNET_CADET_Hash* cid;
2364 struct CadetConnection *c;
2365 size_t minumum_size;
2371 check_connections ();
2372 if (GNUNET_MESSAGE_TYPE_CADET_AX == ntohs (message->type))
2374 overhead = sizeof (struct GNUNET_CADET_AX);
2375 ax_msg = (const struct GNUNET_CADET_AX *) message;
2377 pid = ntohl (ax_msg->pid);
2382 overhead = sizeof (struct GNUNET_CADET_Encrypted);
2383 otr_msg = (const struct GNUNET_CADET_Encrypted *) message;
2384 cid = &otr_msg->cid;
2385 pid = ntohl (otr_msg->pid);
2388 log_message (message, peer, cid);
2390 minumum_size = sizeof (struct GNUNET_MessageHeader) + overhead;
2391 c = connection_get (cid);
2392 fwd = check_message (message, minumum_size, cid, c, peer, pid);
2394 /* If something went wrong, discard message. */
2395 if (GNUNET_SYSERR == fwd)
2397 check_connections ();
2401 /* Is this message for us? */
2402 if (GCC_is_terminal (c, fwd))
2404 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2408 GNUNET_break (GNUNET_NO != c->destroy);
2411 GCT_handle_encrypted (c->t, message);
2412 GCC_send_ack (c, fwd, GNUNET_NO);
2413 check_connections ();
2417 /* Message not for us: forward to next hop */
2418 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2419 if (NULL != otr_msg) /* only otr has ttl */
2421 ttl = ntohl (otr_msg->ttl);
2422 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2425 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2426 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2427 GCC_send_ack (c, fwd, GNUNET_NO);
2428 check_connections ();
2433 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2434 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2435 GNUNET_NO, NULL, NULL));
2436 check_connections ();
2442 * Generic handler for cadet network encrypted traffic.
2444 * @param peer Peer identity this notification is about.
2445 * @param msg Encrypted message.
2446 * @return #GNUNET_OK to keep the connection open,
2447 * #GNUNET_SYSERR to close it (signal serious error)
2450 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2451 const struct GNUNET_CADET_KX *msg)
2453 const struct GNUNET_CADET_Hash* cid;
2454 struct CadetConnection *c;
2455 size_t expected_size;
2458 check_connections ();
2460 log_message (&msg->header, peer, cid);
2462 expected_size = sizeof (struct GNUNET_CADET_KX)
2463 + sizeof (struct GNUNET_MessageHeader);
2464 c = connection_get (cid);
2465 fwd = check_message (&msg->header, expected_size, cid, c, peer, 0);
2467 /* If something went wrong, discard message. */
2468 if (GNUNET_SYSERR == fwd)
2471 /* Is this message for us? */
2472 if (GCC_is_terminal (c, fwd))
2474 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2475 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2481 GCT_handle_kx (c->t, &msg[1].header);
2482 check_connections ();
2486 /* Message not for us: forward to next hop */
2487 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2488 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2489 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2490 GNUNET_NO, NULL, NULL));
2491 check_connections ();
2497 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2499 * @param cls Closure (unused).
2500 * @param message Message received.
2501 * @param peer Peer who sent the message.
2502 * @return #GNUNET_OK to keep the connection open,
2503 * #GNUNET_SYSERR to close it (signal serious error)
2506 GCC_handle_kx (void *cls,
2507 const struct GNUNET_PeerIdentity *peer,
2508 const struct GNUNET_MessageHeader *message)
2510 check_connections ();
2511 return handle_cadet_kx (peer, (struct GNUNET_CADET_KX *) message);
2516 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2518 * @param cls Closure (unused).
2519 * @param message Message received.
2520 * @param peer Peer who sent the message.
2521 * @return #GNUNET_OK to keep the connection open,
2522 * #GNUNET_SYSERR to close it (signal serious error)
2525 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2526 const struct GNUNET_MessageHeader *message)
2528 check_connections ();
2529 return handle_cadet_encrypted (peer, message);
2534 * Core handler for cadet network traffic point-to-point acks.
2536 * @param cls closure
2537 * @param message message
2538 * @param peer peer identity this notification is about
2539 * @return #GNUNET_OK to keep the connection open,
2540 * #GNUNET_SYSERR to close it (signal serious error)
2543 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2544 const struct GNUNET_MessageHeader *message)
2546 struct GNUNET_CADET_ACK *msg;
2547 struct CadetConnection *c;
2548 struct CadetFlowControl *fc;
2553 check_connections ();
2554 msg = (struct GNUNET_CADET_ACK *) message;
2555 log_message (message, peer, &msg->cid);
2556 c = connection_get (&msg->cid);
2559 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2561 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2562 check_connections ();
2566 /* Is this a forward or backward ACK? */
2567 id = GNUNET_PEER_search (peer);
2568 if (GCP_get_short_id (get_next_hop (c)) == id)
2573 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2580 GNUNET_break_op (0);
2584 ack = ntohl (msg->ack);
2585 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2586 GC_f2s (fwd), ack, fc->last_ack_recv);
2587 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2588 fc->last_ack_recv = ack;
2590 /* Cancel polling if the ACK is big enough. */
2591 if (NULL != fc->poll_task &&
2592 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2594 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2595 GNUNET_SCHEDULER_cancel (fc->poll_task);
2596 fc->poll_task = NULL;
2597 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2600 connection_unlock_queue (c, fwd);
2601 check_connections ();
2607 * Core handler for cadet network traffic point-to-point ack polls.
2609 * @param cls closure
2610 * @param message message
2611 * @param peer peer identity this notification is about
2612 * @return #GNUNET_OK to keep the connection open,
2613 * #GNUNET_SYSERR to close it (signal serious error)
2616 GCC_handle_poll (void *cls,
2617 const struct GNUNET_PeerIdentity *peer,
2618 const struct GNUNET_MessageHeader *message)
2620 struct GNUNET_CADET_Poll *msg;
2621 struct CadetConnection *c;
2622 struct CadetFlowControl *fc;
2627 check_connections ();
2628 msg = (struct GNUNET_CADET_Poll *) message;
2629 log_message (message, peer, &msg->cid);
2630 c = connection_get (&msg->cid);
2633 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2635 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL message on unknown connection %s!\n",
2636 GNUNET_h2s (GC_h2hc (&msg->cid)));
2637 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2638 check_connections ();
2642 /* Is this a forward or backward ACK?
2643 * Note: a poll should never be needed in a loopback case,
2644 * since there is no possiblility of packet loss there, so
2645 * this way of discerining FWD/BCK should not be a problem.
2647 id = GNUNET_PEER_search (peer);
2648 if (GCP_get_short_id (get_next_hop (c)) == id)
2650 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2653 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2655 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2660 GNUNET_break_op (0);
2664 pid = ntohl (msg->pid);
2665 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2666 fc->last_pid_recv = pid;
2667 fwd = fc == &c->bck_fc;
2668 GCC_send_ack (c, fwd, GNUNET_YES);
2669 check_connections ();
2676 * Send an ACK on the appropriate connection/channel, depending on
2677 * the direction and the position of the peer.
2679 * @param c Which connection to send the hop-by-hop ACK.
2680 * @param fwd Is this a fwd ACK? (will go dest->root).
2681 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2684 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2686 unsigned int buffer;
2688 check_connections ();
2689 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
2690 GC_f2s (fwd), GCC_2s (c));
2698 if (GNUNET_NO != c->destroy)
2700 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2701 check_connections ();
2705 /* Get available buffer space */
2706 if (GCC_is_terminal (c, fwd))
2708 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2709 buffer = GCT_get_channels_buffer (c->t);
2713 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2714 buffer = GCC_get_buffer (c, fwd);
2716 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2717 if (0 == buffer && GNUNET_NO == force)
2719 check_connections ();
2723 /* Send available buffer space */
2724 if (GCC_is_origin (c, fwd))
2726 GNUNET_assert (NULL != c->t);
2727 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2728 GCT_unchoke_channels (c->t);
2732 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2733 send_ack (c, buffer, fwd, force);
2735 check_connections ();
2740 * Initialize the connections subsystem
2742 * @param c Configuration handle.
2745 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2747 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2749 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2752 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2753 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2754 GNUNET_SCHEDULER_shutdown ();
2759 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2762 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2763 "CADET", "MAX_CONNECTIONS", "MISSING");
2764 GNUNET_SCHEDULER_shutdown ();
2769 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2770 &refresh_connection_time))
2772 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2773 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2774 GNUNET_SCHEDULER_shutdown ();
2777 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2778 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2783 * Destroy each connection on shutdown.
2785 * @param cls Closure (unused).
2786 * @param key Current key code (CID, unused).
2787 * @param value Value in the hash map (`struct CadetConnection`)
2789 * @return #GNUNET_YES, because we should continue to iterate
2792 shutdown_iterator (void *cls,
2793 const struct GNUNET_HashCode *key,
2796 struct CadetConnection *c = value;
2798 c->state = CADET_CONNECTION_DESTROYED;
2805 * Shut down the connections subsystem.
2810 check_connections ();
2811 GNUNET_CONTAINER_multihashmap_iterate (connections,
2814 GNUNET_CONTAINER_multihashmap_destroy (connections);
2820 * Create a connection.
2822 * @param cid Connection ID (either created locally or imposed remotely).
2823 * @param t Tunnel this connection belongs to (or NULL);
2824 * @param path Path this connection has to use (copy is made).
2825 * @param own_pos Own position in the @c path path.
2827 * @return Newly created connection, NULL in case of error (own id not in path).
2829 struct CadetConnection *
2830 GCC_new (const struct GNUNET_CADET_Hash *cid,
2831 struct CadetTunnel *t,
2832 struct CadetPeerPath *path,
2833 unsigned int own_pos)
2835 struct CadetConnection *c;
2836 struct CadetPeerPath *p;
2838 check_connections ();
2839 p = path_duplicate (path);
2840 c = GNUNET_new (struct CadetConnection);
2842 GNUNET_assert (GNUNET_OK ==
2843 GNUNET_CONTAINER_multihashmap_put (connections,
2845 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2846 fc_init (&c->fwd_fc);
2847 fc_init (&c->bck_fc);
2852 GNUNET_assert (own_pos <= p->length - 1);
2853 c->own_pos = own_pos;
2856 GNUNET_assert (NULL != p);
2857 if (GNUNET_OK != register_neighbors (c))
2861 /* We were the origin of this request, this means we have invalid
2862 * info about the paths to reach the destination. We must invalidate
2863 * the *original* path to avoid trying it again in the next minute.
2865 path_invalidate (path);
2868 path_destroy (c->path);
2873 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2874 check_connections ();
2880 GCC_destroy (struct CadetConnection *c)
2882 check_connections ();
2889 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2890 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2893 LOG (GNUNET_ERROR_TYPE_DEBUG,
2894 "destroying connection %s\n",
2896 LOG (GNUNET_ERROR_TYPE_DEBUG,
2897 " fc's f: %p, b: %p\n",
2898 &c->fwd_fc, &c->bck_fc);
2899 LOG (GNUNET_ERROR_TYPE_DEBUG,
2900 " fc tasks f: %u, b: %u\n",
2901 c->fwd_fc.poll_task,
2902 c->bck_fc.poll_task);
2904 /* Cancel all traffic */
2905 if (NULL != c->path)
2907 connection_cancel_queues (c, GNUNET_YES);
2908 connection_cancel_queues (c, GNUNET_NO);
2910 unregister_neighbors (c);
2911 path_destroy (c->path);
2914 /* Cancel maintainance task (keepalive/timeout) */
2915 if (NULL != c->fwd_fc.poll_msg)
2917 GCC_cancel (c->fwd_fc.poll_msg);
2918 LOG (GNUNET_ERROR_TYPE_DEBUG,
2919 " POLL msg FWD canceled\n");
2921 if (NULL != c->bck_fc.poll_msg)
2923 GCC_cancel (c->bck_fc.poll_msg);
2924 LOG (GNUNET_ERROR_TYPE_DEBUG,
2925 " POLL msg BCK canceled\n");
2928 /* Delete from tunnel */
2930 GCT_remove_connection (c->t, c);
2932 if (NULL != c->fwd_maintenance_task)
2933 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2934 if (NULL != c->bck_maintenance_task)
2935 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2936 if (NULL != c->fwd_fc.poll_task)
2938 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2939 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task FWD canceled\n");
2941 if (NULL != c->bck_fc.poll_task)
2943 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2944 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task BCK canceled\n");
2947 GNUNET_break (GNUNET_YES ==
2948 GNUNET_CONTAINER_multihashmap_remove (connections,
2951 GNUNET_STATISTICS_update (stats,
2956 check_connections ();
2961 * Get the connection ID.
2963 * @param c Connection to get the ID from.
2965 * @return ID of the connection.
2967 const struct GNUNET_CADET_Hash *
2968 GCC_get_id (const struct CadetConnection *c)
2975 * Get the connection ID.
2977 * @param c Connection to get the ID from.
2979 * @return ID of the connection.
2981 const struct GNUNET_HashCode *
2982 GCC_get_h (const struct CadetConnection *c)
2984 return GC_h2hc (&c->id);
2989 * Get the connection path.
2991 * @param c Connection to get the path from.
2993 * @return path used by the connection.
2995 const struct CadetPeerPath *
2996 GCC_get_path (const struct CadetConnection *c)
2998 if (GNUNET_NO == c->destroy)
3005 * Get the connection state.
3007 * @param c Connection to get the state from.
3009 * @return state of the connection.
3011 enum CadetConnectionState
3012 GCC_get_state (const struct CadetConnection *c)
3018 * Get the connection tunnel.
3020 * @param c Connection to get the tunnel from.
3022 * @return tunnel of the connection.
3024 struct CadetTunnel *
3025 GCC_get_tunnel (const struct CadetConnection *c)
3032 * Get free buffer space in a connection.
3034 * @param c Connection.
3035 * @param fwd Is query about FWD traffic?
3037 * @return Free buffer space [0 - max_msgs_queue/max_connections]
3040 GCC_get_buffer (struct CadetConnection *c, int fwd)
3042 struct CadetFlowControl *fc;
3044 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3046 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
3047 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
3048 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
3050 return (fc->queue_max - fc->queue_n);
3055 * Get how many messages have we allowed to send to us from a direction.
3057 * @param c Connection.
3058 * @param fwd Are we asking about traffic from FWD (BCK messages)?
3060 * @return last_ack_sent - last_pid_recv
3063 GCC_get_allowed (struct CadetConnection *c, int fwd)
3065 struct CadetFlowControl *fc;
3067 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3068 if (CADET_CONNECTION_READY != c->state
3069 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
3073 return (fc->last_ack_sent - fc->last_pid_recv);
3078 * Get messages queued in a connection.
3080 * @param c Connection.
3081 * @param fwd Is query about FWD traffic?
3083 * @return Number of messages queued.
3086 GCC_get_qn (struct CadetConnection *c, int fwd)
3088 struct CadetFlowControl *fc;
3090 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3097 * Get next PID to use.
3099 * @param c Connection.
3100 * @param fwd Is query about FWD traffic?
3102 * @return Last PID used + 1.
3105 GCC_get_pid (struct CadetConnection *c, int fwd)
3107 struct CadetFlowControl *fc;
3109 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3111 return fc->last_pid_sent + 1;
3116 * Allow the connection to advertise a buffer of the given size.
3118 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3119 * allowing up to last_pid_recv + buffer.
3121 * @param c Connection.
3122 * @param buffer How many more messages the connection can accept.
3123 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3126 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3128 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3129 GCC_2s (c), buffer, GC_f2s (fwd));
3130 send_ack (c, buffer, fwd, GNUNET_NO);
3135 * Notify other peers on a connection of a broken link. Mark connections
3136 * to destroy after all traffic has been sent.
3138 * @param c Connection on which there has been a disconnection.
3139 * @param peer Peer that disconnected.
3142 GCC_notify_broken (struct CadetConnection *c,
3143 struct CadetPeer *peer)
3145 struct CadetPeer *hop;
3148 check_connections ();
3149 LOG (GNUNET_ERROR_TYPE_DEBUG,
3150 "Notify broken on %s due to %s disconnect\n",
3153 hop = get_prev_hop (c);
3156 /* Path was NULL, we should have deleted the connection. */
3160 fwd = (peer == hop);
3161 if (GNUNET_YES == GCC_is_terminal (c, fwd))
3163 /* Local shutdown, no one to notify about this. */
3165 check_connections ();
3168 if (GNUNET_NO == c->destroy)
3169 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3171 /* Connection will have at least one pending message
3172 * (the one we just scheduled), so no point in checking whether to
3173 * destroy immediately. */
3174 c->destroy = GNUNET_YES;
3175 c->state = CADET_CONNECTION_DESTROYED;
3178 * Cancel all queues, if no message is left, connection will be destroyed.
3180 connection_cancel_queues (c, ! fwd);
3181 check_connections ();
3186 * Is this peer the first one on the connection?
3188 * @param c Connection.
3189 * @param fwd Is this about fwd traffic?
3191 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3194 GCC_is_origin (struct CadetConnection *c, int fwd)
3196 if (!fwd && c->path->length - 1 == c->own_pos )
3198 if (fwd && 0 == c->own_pos)
3205 * Is this peer the last one on the connection?
3207 * @param c Connection.
3208 * @param fwd Is this about fwd traffic?
3209 * Note that the ROOT is the terminal for BCK traffic!
3211 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3214 GCC_is_terminal (struct CadetConnection *c, int fwd)
3216 return GCC_is_origin (c, !fwd);
3221 * See if we are allowed to send by the next hop in the given direction.
3223 * @param c Connection.
3224 * @param fwd Is this about fwd traffic?
3226 * @return #GNUNET_YES in case it's OK to send.
3229 GCC_is_sendable (struct CadetConnection *c, int fwd)
3231 struct CadetFlowControl *fc;
3233 LOG (GNUNET_ERROR_TYPE_DEBUG,
3234 " checking sendability of %s traffic on %s\n",
3235 GC_f2s (fwd), GCC_2s (c));
3241 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3242 LOG (GNUNET_ERROR_TYPE_DEBUG,
3243 " last ack recv: %u, last pid sent: %u\n",
3244 fc->last_ack_recv, fc->last_pid_sent);
3245 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3247 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3250 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3256 * Check if this connection is a direct one (never trim a direct connection).
3258 * @param c Connection.
3260 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3263 GCC_is_direct (struct CadetConnection *c)
3265 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3270 * Sends an already built message on a connection, properly registering
3271 * all used resources.
3273 * @param message Message to send. Function makes a copy of it.
3274 * If message is not hop-by-hop, decrements TTL of copy.
3275 * @param payload_type Type of payload, in case the message is encrypted.
3276 * @param c Connection on which this message is transmitted.
3277 * @param fwd Is this a fwd message?
3278 * @param force Force the connection to accept the message (buffer overfill).
3279 * @param cont Continuation called once message is sent. Can be NULL.
3280 * @param cont_cls Closure for @c cont.
3282 * @return Handle to cancel the message before it's sent.
3283 * NULL on error or if @c cont is NULL.
3284 * Invalid on @c cont call.
3286 struct CadetConnectionQueue *
3287 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3288 uint16_t payload_type, uint32_t payload_id,
3289 struct CadetConnection *c, int fwd, int force,
3290 GCC_sent cont, void *cont_cls)
3292 struct CadetFlowControl *fc;
3293 struct CadetConnectionQueue *q;
3299 check_connections ();
3300 size = ntohs (message->size);
3301 data = GNUNET_malloc (size);
3302 memcpy (data, message, size);
3303 type = ntohs (message->type);
3304 LOG (GNUNET_ERROR_TYPE_INFO, "--> %s (%s %4u) on connection %s (%u bytes)\n",
3305 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), size);
3307 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3308 droppable = GNUNET_NO == force;
3311 struct GNUNET_CADET_AX *axmsg;
3312 struct GNUNET_CADET_Encrypted *emsg;
3313 struct GNUNET_CADET_KX *kmsg;
3314 struct GNUNET_CADET_ACK *amsg;
3315 struct GNUNET_CADET_Poll *pmsg;
3316 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3317 struct GNUNET_CADET_ConnectionBroken *bmsg;
3320 case GNUNET_MESSAGE_TYPE_CADET_AX:
3321 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3322 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3324 emsg = (struct GNUNET_CADET_Encrypted *) data;
3325 ttl = ntohl (emsg->ttl);
3328 GNUNET_break_op (0);
3333 emsg->ttl = htonl (ttl - 1);
3337 axmsg = (struct GNUNET_CADET_AX *) data;
3340 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3341 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3342 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3343 if (GNUNET_YES == droppable)
3349 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3353 case GNUNET_MESSAGE_TYPE_CADET_KX:
3354 kmsg = (struct GNUNET_CADET_KX *) data;
3358 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3359 amsg = (struct GNUNET_CADET_ACK *) data;
3361 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3362 droppable = GNUNET_NO;
3365 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3366 pmsg = (struct GNUNET_CADET_Poll *) data;
3368 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
3369 droppable = GNUNET_NO;
3372 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3373 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
3377 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3378 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
3382 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3383 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3392 if (fc->queue_n > fc->queue_max && droppable)
3394 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3397 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3398 fc->queue_n, fc->queue_max);
3399 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
3400 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
3405 return NULL; /* Drop this message */
3408 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3409 GCC_2s (c), c->pending_messages);
3410 c->pending_messages++;
3412 q = GNUNET_new (struct CadetConnectionQueue);
3413 q->forced = !droppable;
3414 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3415 size, c, fwd, &conn_message_sent, q);
3418 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3421 check_connections ();
3425 q->cont_cls = cont_cls;
3426 check_connections ();
3427 return (NULL == cont) ? NULL : q;
3432 * Cancel a previously sent message while it's in the queue.
3434 * ONLY can be called before the continuation given to the send function
3435 * is called. Once the continuation is called, the message is no longer in the
3438 * @param q Handle to the queue.
3441 GCC_cancel (struct CadetConnectionQueue *q)
3443 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3445 /* queue destroy calls message_sent, which calls q->cont and frees q */
3446 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3447 check_connections ();
3452 * Sends a CREATE CONNECTION message for a path to a peer.
3453 * Changes the connection and tunnel states if necessary.
3455 * @param connection Connection to create.
3458 GCC_send_create (struct CadetConnection *connection)
3460 enum CadetTunnelCState state;
3463 check_connections ();
3464 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3465 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3467 LOG (GNUNET_ERROR_TYPE_INFO, "---> %s on connection %s (%u bytes)\n",
3468 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE),
3469 GCC_2s (connection), size);
3470 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3471 connection, connection->pending_messages);
3472 connection->pending_messages++;
3474 connection->maintenance_q =
3475 GCP_queue_add (get_next_hop (connection), NULL,
3476 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 0,
3477 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3479 state = GCT_get_cstate (connection->t);
3480 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3481 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3482 if (CADET_CONNECTION_NEW == connection->state)
3483 connection_change_state (connection, CADET_CONNECTION_SENT);
3484 check_connections ();
3489 * Send a message to all peers in this connection that the connection
3490 * is no longer valid.
3492 * If some peer should not receive the message, it should be zero'ed out
3493 * before calling this function.
3495 * @param c The connection whose peers to notify.
3498 GCC_send_destroy (struct CadetConnection *c)
3500 struct GNUNET_CADET_ConnectionDestroy msg;
3502 if (GNUNET_YES == c->destroy)
3504 check_connections ();
3505 msg.header.size = htons (sizeof (msg));
3506 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3508 LOG (GNUNET_ERROR_TYPE_DEBUG,
3509 " sending connection destroy for connection %s\n",
3512 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3513 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3514 GNUNET_YES, GNUNET_YES,
3516 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3517 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3518 GNUNET_NO, GNUNET_YES,
3520 c->destroy = GNUNET_YES;
3521 c->state = CADET_CONNECTION_DESTROYED;
3522 check_connections ();
3527 * @brief Start a polling timer for the connection.
3529 * When a neighbor does not accept more traffic on the connection it could be
3530 * caused by a simple congestion or by a lost ACK. Polling enables to check
3531 * for the lastest ACK status for a connection.
3533 * @param c Connection.
3534 * @param fwd Should we poll in the FWD direction?
3537 GCC_start_poll (struct CadetConnection *c, int fwd)
3539 struct CadetFlowControl *fc;
3541 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3542 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3544 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3546 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not needed (%p, %p)\n",
3547 fc->poll_task, fc->poll_msg);
3550 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3551 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3558 * @brief Stop polling a connection for ACKs.
3560 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3562 * @param c Connection.
3563 * @param fwd Should we stop the poll in the FWD direction?
3566 GCC_stop_poll (struct CadetConnection *c, int fwd)
3568 struct CadetFlowControl *fc;
3570 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3571 if (NULL != fc->poll_task)
3573 GNUNET_SCHEDULER_cancel (fc->poll_task);
3574 fc->poll_task = NULL;
3580 * Get a (static) string for a connection.
3582 * @param c Connection.
3585 GCC_2s (const struct CadetConnection *c)
3592 static char buf[128];
3594 SPRINTF (buf, "%s (->%s)",
3595 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3598 return GNUNET_h2s (GC_h2hc (&c->id));
3603 * Log all possible info about the connection state.
3605 * @param c Connection to debug.
3606 * @param level Debug level to use.
3609 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3614 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3616 __FILE__, __FUNCTION__, __LINE__);
3622 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3626 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3627 s = path_2s (c->path);
3628 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3630 LOG2 (level, "CCC state: %s, destroy: %u\n",
3631 GCC_state2s (c->state), c->destroy);
3632 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3633 if (NULL != c->perf)
3634 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3636 LOG2 (level, "CCC FWD flow control:\n");
3637 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3638 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3639 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3640 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3641 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3642 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3643 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3644 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3646 LOG2 (level, "CCC BCK flow control:\n");
3647 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3648 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3649 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3650 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3651 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3652 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3653 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3654 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3656 LOG2 (level, "CCC DEBUG CONNECTION END\n");