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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 /******************************************************************************/
317 #if 0 // avoid compiler warning for unused static function
319 fc_debug (struct CadetFlowControl *fc)
321 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
322 fc->last_pid_recv, fc->last_ack_sent);
323 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
324 fc->last_pid_sent, fc->last_ack_recv);
325 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
326 fc->queue_n, fc->queue_max);
330 connection_debug (struct CadetConnection *c)
334 LOG (GNUNET_ERROR_TYPE_INFO, "DEBUG NULL CONNECTION\n");
337 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
338 peer2s (c->t->peer), GCC_2s (c));
339 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
340 c->state, c->pending_messages);
341 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
342 fc_debug (&c->fwd_fc);
343 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
344 fc_debug (&c->bck_fc);
350 * Schedule next keepalive task, taking in consideration
351 * the connection state and number of retries.
353 * @param c Connection for which to schedule the next keepalive.
354 * @param fwd Direction for the next keepalive.
357 schedule_next_keepalive (struct CadetConnection *c, int fwd);
361 * Resets the connection timeout task, some other message has done the
363 * - For the first peer on the direction this means to send
364 * a keepalive or a path confirmation message (either create or ACK).
365 * - For all other peers, this means to destroy the connection,
366 * due to lack of activity.
367 * Starts the timeout if no timeout was running (connection just created).
369 * @param c Connection whose timeout to reset.
370 * @param fwd Is this forward?
373 connection_reset_timeout (struct CadetConnection *c, int fwd);
377 * Get string description for tunnel state. Reentrant.
379 * @param s Tunnel state.
381 * @return String representation.
384 GCC_state2s (enum CadetConnectionState s)
388 case CADET_CONNECTION_NEW:
389 return "CADET_CONNECTION_NEW";
390 case CADET_CONNECTION_SENT:
391 return "CADET_CONNECTION_SENT";
392 case CADET_CONNECTION_ACK:
393 return "CADET_CONNECTION_ACK";
394 case CADET_CONNECTION_READY:
395 return "CADET_CONNECTION_READY";
396 case CADET_CONNECTION_DESTROYED:
397 return "CADET_CONNECTION_DESTROYED";
398 case CADET_CONNECTION_BROKEN:
399 return "CADET_CONNECTION_BROKEN";
402 LOG (GNUNET_ERROR_TYPE_ERROR, " conn state %u unknown!\n", s);
403 return "CADET_CONNECTION_STATE_ERROR";
409 * Initialize a Flow Control structure to the initial state.
411 * @param fc Flow Control structure to initialize.
414 fc_init (struct CadetFlowControl *fc)
416 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
417 fc->last_pid_recv = (uint32_t) -1;
418 fc->last_ack_sent = (uint32_t) 0;
419 fc->last_ack_recv = (uint32_t) 0;
420 fc->poll_task = NULL;
421 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
423 fc->queue_max = (max_msgs_queue / max_connections) + 1;
430 * @param cid Connection ID.
432 static struct CadetConnection *
433 connection_get (const struct GNUNET_CADET_Hash *cid)
435 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
440 connection_change_state (struct CadetConnection* c,
441 enum CadetConnectionState state)
443 LOG (GNUNET_ERROR_TYPE_DEBUG,
444 "Connection %s state %s -> %s\n",
445 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
446 if (CADET_CONNECTION_DESTROYED <= c->state) /* Destroyed or broken. */
448 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
452 if (CADET_CONNECTION_READY == state)
458 * Callback called when a queued ACK message is sent.
460 * @param cls Closure (FC).
461 * @param c Connection this message was on.
462 * @param q Queue handler this call invalidates.
463 * @param type Type of message sent.
464 * @param fwd Was this a FWD going message?
465 * @param size Size of the message.
469 struct CadetConnection *c,
470 struct CadetConnectionQueue *q,
471 uint16_t type, int fwd, size_t size)
473 struct CadetFlowControl *fc = cls;
480 * Send an ACK on the connection, informing the predecessor about
481 * the available buffer space. Should not be called in case the peer
482 * is origin (no predecessor) in the @c fwd direction.
484 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
485 * the ACK itself goes "back" (dest->root).
487 * @param c Connection on which to send the ACK.
488 * @param buffer How much space free to advertise?
489 * @param fwd Is this FWD ACK? (Going dest -> root)
490 * @param force Don't optimize out.
493 send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
495 struct CadetFlowControl *next_fc;
496 struct CadetFlowControl *prev_fc;
497 struct GNUNET_CADET_ACK msg;
501 /* If origin, there is no connection to send ACKs. Wrong function! */
502 if (GCC_is_origin (c, fwd))
504 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
505 GCC_2s (c), GC_f2s (fwd));
510 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
511 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
513 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
514 GC_f2s (fwd), GCC_2s (c));
516 /* Check if we need to transmit the ACK. */
517 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
518 if (3 < delta && buffer < delta && GNUNET_NO == force)
520 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 " last pid recv: %u, last ack sent: %u\n",
523 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
527 /* Ok, ACK might be necessary, what PID to ACK? */
528 ack = prev_fc->last_pid_recv + buffer;
529 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 " last pid %u, last ack %u, qmax %u, q %u\n",
532 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
533 next_fc->queue_max, next_fc->queue_n);
534 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
536 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
540 /* Check if message is already in queue */
541 if (NULL != prev_fc->ack_msg)
543 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
545 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
546 GCC_cancel (prev_fc->ack_msg);
547 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
551 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
556 prev_fc->last_ack_sent = ack;
558 /* Build ACK message and send on connection */
559 msg.header.size = htons (sizeof (msg));
560 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
561 msg.ack = htonl (ack);
564 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, 0, ack, c,
567 GNUNET_assert (NULL != prev_fc->ack_msg);
572 * Callback called when a connection queued message is sent.
574 * Calculates the average time and connection packet tracking.
576 * @param cls Closure (ConnectionQueue Handle).
577 * @param c Connection this message was on.
578 * @param sent Was it really sent? (Could have been canceled)
579 * @param type Type of message sent.
580 * @param pid Packet ID, or 0 if not applicable (create, destroy, etc).
581 * @param fwd Was this a FWD going message?
582 * @param size Size of the message.
583 * @param wait Time spent waiting for core (only the time for THIS message)
585 * @return #GNUNET_YES if connection was destroyed, #GNUNET_NO otherwise.
588 conn_message_sent (void *cls,
589 struct CadetConnection *c, int sent,
590 uint16_t type, uint32_t pid, int fwd, size_t size,
591 struct GNUNET_TIME_Relative wait)
593 struct CadetConnectionPerformance *p;
594 struct CadetFlowControl *fc;
595 struct CadetConnectionQueue *q = cls;
599 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
601 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
603 fc = fwd ? &c->fwd_fc : &c->bck_fc;
604 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
605 sent ? "" : "not ", GC_f2s (fwd), GC_m2s (type), pid);
611 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
612 q->cont (q->cont_cls, c, q, type, fwd, size);
616 else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED
617 || type == GNUNET_MESSAGE_TYPE_CADET_AX)
619 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
628 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
629 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
631 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
636 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
637 c->pending_messages--;
638 if ( (GNUNET_YES == c->destroy) &&
639 (0 == c->pending_messages) )
641 LOG (GNUNET_ERROR_TYPE_DEBUG,
642 "! destroying connection!\n");
646 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
649 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
650 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
651 c->maintenance_q = NULL;
652 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
653 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
654 schedule_next_keepalive (c, fwd);
657 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
658 case GNUNET_MESSAGE_TYPE_CADET_AX:
659 if (GNUNET_YES == sent)
661 GNUNET_assert (NULL != q);
662 fc->last_pid_sent = pid;
663 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
664 GCC_start_poll (c, fwd);
665 GCC_send_ack (c, fwd, GNUNET_NO);
666 connection_reset_timeout (c, fwd);
669 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
670 if (GNUNET_NO == forced)
673 LOG (GNUNET_ERROR_TYPE_DEBUG,
674 "! accounting pid %u\n",
679 LOG (GNUNET_ERROR_TYPE_DEBUG,
680 "! forced, Q_N not accounting pid %u\n",
685 case GNUNET_MESSAGE_TYPE_CADET_KX:
686 if (GNUNET_YES == sent)
687 connection_reset_timeout (c, fwd);
690 case GNUNET_MESSAGE_TYPE_CADET_POLL:
694 case GNUNET_MESSAGE_TYPE_CADET_ACK:
698 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
702 LOG (GNUNET_ERROR_TYPE_ERROR, "%s unknown\n", GC_m2s (type));
706 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
709 return GNUNET_NO; /* Only endpoints are interested in timing. */
712 usecsperbyte = ((double) wait.rel_value_us) / size;
713 if (p->size == AVG_MSGS)
715 /* Array is full. Substract oldest value, add new one and store. */
716 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
717 p->usecsperbyte[p->idx] = usecsperbyte;
718 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
722 /* Array not yet full. Add current value to avg and store. */
723 p->usecsperbyte[p->idx] = usecsperbyte;
725 p->avg += p->usecsperbyte[p->idx];
729 p->idx = (p->idx + 1) % AVG_MSGS;
735 * Get the previous hop in a connection
737 * @param c Connection.
739 * @return Previous peer in the connection.
741 static struct CadetPeer *
742 get_prev_hop (const struct CadetConnection *c)
748 LOG (GNUNET_ERROR_TYPE_DEBUG,
749 " get prev hop %s [%u/%u]\n",
750 GCC_2s (c), c->own_pos, c->path->length);
751 if (0 == c->own_pos || c->path->length < 2)
752 id = c->path->peers[0];
754 id = c->path->peers[c->own_pos - 1];
756 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
757 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
759 return GCP_get_short (id);
764 * Get the next hop in a connection
766 * @param c Connection.
768 * @return Next peer in the connection.
770 static struct CadetPeer *
771 get_next_hop (const struct CadetConnection *c)
778 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
779 GCC_2s (c), c->own_pos, c->path->length);
780 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
781 id = c->path->peers[c->path->length - 1];
783 id = c->path->peers[c->own_pos + 1];
785 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
786 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
788 return GCP_get_short (id);
793 * Get the hop in a connection.
795 * @param c Connection.
796 * @param fwd Next in the FWD direction?
798 * @return Next peer in the connection.
800 static struct CadetPeer *
801 get_hop (struct CadetConnection *c, int fwd)
804 return get_next_hop (c);
805 return get_prev_hop (c);
810 * Get a bit mask for a message received out-of-order.
812 * @param last_pid_recv Last PID we received prior to the out-of-order.
813 * @param ooo_pid PID of the out-of-order message.
816 get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
818 return 1 << (last_pid_recv - ooo_pid);
823 * Check is an out-of-order message is ok:
824 * - at most 31 messages behind.
827 * @param last_pid_recv Last in-order PID received.
830 is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
834 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
837 mask = get_recv_bitmask (last_pid_recv, ooo_pid);
838 if (0 != (ooo_bitmap & mask))
846 * Is traffic coming from this sender 'FWD' traffic?
848 * @param c Connection to check.
849 * @param sender Peer identity of neighbor.
851 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
852 * the traffic is 'FWD'.
853 * #GNUNET_NO for BCK.
854 * #GNUNET_SYSERR for errors.
857 is_fwd (const struct CadetConnection *c,
858 const struct GNUNET_PeerIdentity *sender)
862 id = GNUNET_PEER_search (sender);
863 if (GCP_get_short_id (get_prev_hop (c)) == id)
866 if (GCP_get_short_id (get_next_hop (c)) == id)
870 return GNUNET_SYSERR;
875 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
876 * or a first CONNECTION_ACK directed to us.
878 * @param connection Connection to confirm.
879 * @param fwd Should we send it FWD? (root->dest)
880 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
883 send_connection_ack (struct CadetConnection *connection, int fwd)
885 struct CadetTunnel *t;
888 LOG (GNUNET_ERROR_TYPE_INFO, "---> {%14s ACK} on connection %s\n",
889 GC_f2s (!fwd), GCC_2s (connection));
890 GCP_queue_add (get_hop (connection, fwd), NULL,
891 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0, 0,
892 sizeof (struct GNUNET_CADET_ConnectionACK),
893 connection, fwd, &conn_message_sent, NULL);
894 connection->pending_messages++;
895 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
896 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
897 if (CADET_CONNECTION_READY != connection->state)
898 connection_change_state (connection, CADET_CONNECTION_SENT);
903 * Send a notification that a connection is broken.
905 * @param c Connection that is broken.
906 * @param id1 Peer that has disconnected.
907 * @param id2 Peer that has disconnected.
908 * @param fwd Direction towards which to send it.
911 send_broken (struct CadetConnection *c,
912 const struct GNUNET_PeerIdentity *id1,
913 const struct GNUNET_PeerIdentity *id2,
916 struct GNUNET_CADET_ConnectionBroken msg;
918 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
919 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
923 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c, fwd,
924 GNUNET_YES, NULL, NULL));
929 * Send a notification that a connection is broken, when a connection
930 * isn't even known to the local peer or soon to be destroyed.
932 * @param connection_id Connection ID.
933 * @param id1 Peer that has disconnected, probably local peer.
934 * @param id2 Peer that has disconnected can be NULL if unknown.
935 * @param peer Peer to notify (neighbor who sent the connection).
938 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
939 const struct GNUNET_PeerIdentity *id1,
940 const struct GNUNET_PeerIdentity *id2,
941 const struct GNUNET_PeerIdentity *peer_id)
943 struct GNUNET_CADET_ConnectionBroken *msg;
944 struct CadetPeer *neighbor;
946 LOG (GNUNET_ERROR_TYPE_INFO, "---> BROKEN on unknown connection %s\n",
947 GNUNET_h2s (GC_h2hc (connection_id)));
949 msg = GNUNET_new (struct GNUNET_CADET_ConnectionBroken);
950 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
951 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
952 msg->cid = *connection_id;
957 memset (&msg->peer2, 0, sizeof (msg->peer2));
958 neighbor = GCP_get (peer_id);
959 GCP_queue_add (neighbor, msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
960 0, 2, sizeof (struct GNUNET_CADET_ConnectionBroken),
961 NULL, GNUNET_SYSERR, /* connection, fwd */
962 NULL, NULL); /* continuation */
967 * Send keepalive packets for a connection.
969 * @param c Connection to keep alive..
970 * @param fwd Is this a FWD keepalive? (owner -> dest).
973 send_connection_keepalive (struct CadetConnection *c, int fwd)
975 struct GNUNET_MessageHeader msg;
976 struct CadetFlowControl *fc;
978 LOG (GNUNET_ERROR_TYPE_INFO, "keepalive %s for connection %s\n",
979 GC_f2s (fwd), GCC_2s (c));
981 fc = fwd ? &c->fwd_fc : &c->bck_fc;
984 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
988 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
990 GNUNET_assert (NULL != c->t);
991 msg.size = htons (sizeof (msg));
992 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
994 GNUNET_assert (NULL ==
995 GCT_send_prebuilt_message (&msg, c->t, c,
996 GNUNET_NO, NULL, NULL));
1001 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1003 * @param c Connection for which to send the message.
1004 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1007 connection_recreate (struct CadetConnection *c, int fwd)
1009 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
1011 GCC_send_create (c);
1013 send_connection_ack (c, GNUNET_NO);
1018 * Generic connection timer management.
1019 * Depending on the role of the peer in the connection will send the
1020 * appropriate message (build or keepalive)
1022 * @param c Conncetion to maintain.
1023 * @param fwd Is FWD?
1026 connection_maintain (struct CadetConnection *c, int fwd)
1028 if (GNUNET_NO != c->destroy)
1030 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1037 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1041 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1043 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1045 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1046 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1047 schedule_next_keepalive (c, fwd);
1052 case CADET_CONNECTION_NEW:
1055 case CADET_CONNECTION_SENT:
1056 connection_recreate (c, fwd);
1058 case CADET_CONNECTION_READY:
1059 send_connection_keepalive (c, fwd);
1069 * Keep the connection alive.
1071 * @param c Connection to keep alive.
1072 * @param fwd Direction.
1073 * @param shutdown Are we shutting down? (Don't send traffic)
1074 * Non-zero value for true, not necessarily GNUNET_YES.
1077 connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
1079 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s keepalive for %s\n",
1080 GC_f2s (fwd), GCC_2s (c));
1083 c->fwd_maintenance_task = NULL;
1085 c->bck_maintenance_task = NULL;
1087 if (GNUNET_NO != shutdown)
1090 connection_maintain (c, fwd);
1092 /* Next execution will be scheduled by message_sent or _maintain*/
1097 * Keep the connection alive in the FWD direction.
1099 * @param cls Closure (connection to keepalive).
1100 * @param tc TaskContext.
1103 connection_fwd_keepalive (void *cls,
1104 const struct GNUNET_SCHEDULER_TaskContext *tc)
1106 connection_keepalive ((struct CadetConnection *) cls,
1108 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1113 * Keep the connection alive in the BCK direction.
1115 * @param cls Closure (connection to keepalive).
1116 * @param tc TaskContext.
1119 connection_bck_keepalive (void *cls,
1120 const struct GNUNET_SCHEDULER_TaskContext *tc)
1122 connection_keepalive ((struct CadetConnection *) cls,
1124 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1129 * Schedule next keepalive task, taking in consideration
1130 * the connection state and number of retries.
1132 * If the peer is not the origin, do nothing.
1134 * @param c Connection for which to schedule the next keepalive.
1135 * @param fwd Direction for the next keepalive.
1138 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1140 struct GNUNET_TIME_Relative delay;
1141 struct GNUNET_SCHEDULER_Task * *task_id;
1142 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1144 if (GNUNET_NO == GCC_is_origin (c, fwd))
1147 /* Calculate delay to use, depending on the state of the connection */
1148 if (CADET_CONNECTION_READY == c->state)
1150 delay = refresh_connection_time;
1154 if (1 > c->create_retry)
1155 c->create_retry = 1;
1156 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1158 if (c->create_retry < 64)
1159 c->create_retry *= 2;
1162 /* Select direction-dependent parameters */
1163 if (GNUNET_YES == fwd)
1165 task_id = &c->fwd_maintenance_task;
1166 keepalive_task = &connection_fwd_keepalive;
1170 task_id = &c->bck_maintenance_task;
1171 keepalive_task = &connection_bck_keepalive;
1174 /* Check that no one scheduled it before us */
1175 if (NULL != *task_id)
1177 /* No need for a _break. It can happen for instance when sending a SYNACK
1178 * for a duplicate SYN: the first SYNACK scheduled the task. */
1179 GNUNET_SCHEDULER_cancel (*task_id);
1182 /* Schedule the task */
1183 *task_id = GNUNET_SCHEDULER_add_delayed (delay, keepalive_task, c);
1184 LOG (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1185 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1190 * @brief Re-initiate traffic on this connection if necessary.
1192 * Check if there is traffic queued towards this peer
1193 * and the core transmit handle is NULL (traffic was stalled).
1194 * If so, call core tmt rdy.
1196 * @param c Connection on which initiate traffic.
1197 * @param fwd Is this about fwd traffic?
1200 connection_unlock_queue (struct CadetConnection *c, int fwd)
1202 struct CadetPeer *peer;
1204 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_unlock_queue %s on %s\n",
1205 GC_f2s (fwd), GCC_2s (c));
1207 if (GCC_is_terminal (c, fwd))
1209 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal, can unlock!\n");
1213 peer = get_hop (c, fwd);
1214 GCP_queue_unlock (peer, c);
1219 * Cancel all transmissions that belong to a certain connection.
1221 * If the connection is scheduled for destruction and no more messages are left,
1222 * the connection will be destroyed by the continuation call.
1224 * @param c Connection which to cancel. Might be destroyed during this call.
1225 * @param fwd Cancel fwd traffic?
1228 connection_cancel_queues (struct CadetConnection *c, int fwd)
1230 struct CadetFlowControl *fc;
1231 struct CadetPeer *peer;
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cancel %s queues for connection %s\n",
1234 GC_f2s (fwd), GCC_2s (c));
1241 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1242 if (NULL != fc->poll_task)
1244 GNUNET_SCHEDULER_cancel (fc->poll_task);
1245 fc->poll_task = NULL;
1246 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cancel POLL in ccq for fc %p\n", fc);
1248 peer = get_hop (c, fwd);
1249 GCP_queue_cancel (peer, c);
1254 * Function called if a connection has been stalled for a while,
1255 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1257 * @param cls Closure (poll ctx).
1258 * @param tc TaskContext.
1261 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1265 * Callback called when a queued POLL message is sent.
1267 * @param cls Closure (FC).
1268 * @param c Connection this message was on.
1269 * @param q Queue handler this call invalidates.
1270 * @param type Type of message sent.
1271 * @param fwd Was this a FWD going message?
1272 * @param size Size of the message.
1275 poll_sent (void *cls,
1276 struct CadetConnection *c,
1277 struct CadetConnectionQueue *q,
1278 uint16_t type, int fwd, size_t size)
1280 struct CadetFlowControl *fc = cls;
1282 if (2 == c->destroy)
1284 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1287 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1289 fc->poll_msg = NULL;
1290 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1291 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1292 &connection_poll, fc);
1293 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1298 * Function called if a connection has been stalled for a while,
1299 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1301 * @param cls Closure (poll ctx).
1302 * @param tc TaskContext.
1305 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1307 struct CadetFlowControl *fc = cls;
1308 struct GNUNET_CADET_Poll msg;
1309 struct CadetConnection *c;
1312 fc->poll_task = NULL;
1313 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1319 fwd = fc == &c->fwd_fc;
1320 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1321 GCC_2s (c), GC_f2s (fwd));
1323 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1324 msg.header.size = htons (sizeof (msg));
1325 msg.pid = htonl (fc->last_pid_sent);
1326 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1328 GCC_send_prebuilt_message (&msg.header, 0, fc->last_pid_sent, c,
1329 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1330 GNUNET_assert (NULL != fc->poll_msg);
1335 * Resend all queued messages for a connection on other connections of the
1336 * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
1338 * @param c Connection whose messages to resend.
1339 * @param fwd Resend fwd messages?
1342 resend_messages_and_destroy (struct CadetConnection *c, int fwd)
1344 struct GNUNET_MessageHeader *out_msg;
1345 struct CadetTunnel *t = c->t;
1346 struct CadetPeer *neighbor;
1347 unsigned int pending;
1350 c->state = CADET_CONNECTION_DESTROYED;
1351 c->destroy = GNUNET_YES;
1353 destroyed = GNUNET_NO;
1354 neighbor = get_hop (c, fwd);
1355 pending = c->pending_messages;
1357 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1360 GCT_resend_message (out_msg, t);
1361 GNUNET_free (out_msg);
1364 /* All pending messages should have been popped,
1365 * and the connection destroyed by the continuation.
1367 if (GNUNET_YES != destroyed)
1372 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1373 if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1381 * Generic connection timeout implementation.
1383 * Timeout function due to lack of keepalive/traffic from an endpoint.
1384 * Destroys connection if called.
1386 * @param c Connection to destroy.
1387 * @param fwd Was the timeout from the origin? (FWD timeout)
1390 connection_timeout (struct CadetConnection *c, int fwd)
1392 struct CadetFlowControl *reverse_fc;
1394 reverse_fc = fwd ? &c->bck_fc : &c->fwd_fc;
1396 LOG (GNUNET_ERROR_TYPE_INFO,
1397 "Connection %s %s timed out. Destroying.\n",
1400 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1402 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1408 /* If dest, salvage queued traffic. */
1409 if (GCC_is_origin (c, !fwd))
1411 const struct GNUNET_PeerIdentity *next_hop;
1413 next_hop = GCP_get_id (fwd ? get_prev_hop (c) : get_next_hop (c));
1414 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1415 if (0 < reverse_fc->queue_n)
1416 resend_messages_and_destroy (c, !fwd);
1425 * Timeout function due to lack of keepalive/traffic from the owner.
1426 * Destroys connection if called.
1428 * @param cls Closure (connection to destroy).
1429 * @param tc TaskContext.
1432 connection_fwd_timeout (void *cls,
1433 const struct GNUNET_SCHEDULER_TaskContext *tc)
1435 struct CadetConnection *c = cls;
1437 c->fwd_maintenance_task = NULL;
1438 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1441 connection_timeout (c, GNUNET_YES);
1446 * Timeout function due to lack of keepalive/traffic from the destination.
1447 * Destroys connection if called.
1449 * @param cls Closure (connection to destroy).
1450 * @param tc TaskContext
1453 connection_bck_timeout (void *cls,
1454 const struct GNUNET_SCHEDULER_TaskContext *tc)
1456 struct CadetConnection *c = cls;
1458 c->bck_maintenance_task = NULL;
1459 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1462 connection_timeout (c, GNUNET_NO);
1467 * Resets the connection timeout task, some other message has done the
1469 * - For the first peer on the direction this means to send
1470 * a keepalive or a path confirmation message (either create or ACK).
1471 * - For all other peers, this means to destroy the connection,
1472 * due to lack of activity.
1473 * Starts the timeout if no timeout was running (connection just created).
1475 * @param c Connection whose timeout to reset.
1476 * @param fwd Is this forward?
1478 * TODO use heap to improve efficiency of scheduler.
1481 connection_reset_timeout (struct CadetConnection *c, int fwd)
1483 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1485 if (GCC_is_origin (c, fwd)) /* Startpoint */
1487 schedule_next_keepalive (c, fwd);
1489 else /* Relay, endpoint. */
1491 struct GNUNET_TIME_Relative delay;
1492 struct GNUNET_SCHEDULER_Task * *ti;
1493 GNUNET_SCHEDULER_TaskCallback f;
1495 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1498 GNUNET_SCHEDULER_cancel (*ti);
1499 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1500 LOG (GNUNET_ERROR_TYPE_DEBUG, " timing out in %s\n",
1501 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1502 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1503 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1509 * Add the connection to the list of both neighbors.
1511 * @param c Connection.
1513 * @return #GNUNET_OK if everything went fine
1514 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1517 register_neighbors (struct CadetConnection *c)
1519 c->next_peer = get_next_hop (c);
1520 c->prev_peer = get_prev_hop (c);
1521 GNUNET_break (c->next_peer != c->prev_peer);
1522 LOG (GNUNET_ERROR_TYPE_DEBUG,
1523 "register neighbors for connection %s\n",
1525 path_debug (c->path);
1526 LOG (GNUNET_ERROR_TYPE_DEBUG,
1527 "own pos %u\n", c->own_pos);
1528 LOG (GNUNET_ERROR_TYPE_DEBUG,
1529 "putting connection %s to next peer %p\n",
1532 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1534 GCP_2s (c->next_peer));
1535 LOG (GNUNET_ERROR_TYPE_DEBUG,
1536 "putting connection %s to prev peer %p\n",
1539 LOG (GNUNET_ERROR_TYPE_DEBUG,
1540 "prev peer %p %s\n",
1542 GCP_2s (c->prev_peer));
1544 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1545 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1547 if (GCC_is_origin (c, GNUNET_YES))
1548 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1549 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1551 LOG (GNUNET_ERROR_TYPE_DEBUG,
1552 " register neighbors failed\n");
1553 LOG (GNUNET_ERROR_TYPE_DEBUG,
1554 " prev: %s, neighbor?: %d\n",
1555 GCP_2s (c->prev_peer),
1556 GCP_is_neighbor (c->prev_peer));
1557 LOG (GNUNET_ERROR_TYPE_DEBUG,
1558 " next: %s, neighbor?: %d\n",
1559 GCP_2s (c->next_peer),
1560 GCP_is_neighbor (c->next_peer));
1561 return GNUNET_SYSERR;
1563 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1564 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1571 * Remove the connection from the list of both neighbors.
1573 * @param c Connection.
1576 unregister_neighbors (struct CadetConnection *c)
1578 struct CadetPeer *peer;
1580 peer = get_next_hop (c);
1581 GNUNET_assert (c->next_peer == peer);
1582 GCP_remove_connection (peer, c);
1583 peer = get_prev_hop (c);
1584 GNUNET_assert (c->prev_peer == peer);
1585 GCP_remove_connection (peer, c);
1590 * Bind the connection to the peer and the tunnel to that peer.
1592 * If the peer has no tunnel, create one. Update tunnel and connection
1593 * data structres to reflect new status.
1595 * @param c Connection.
1599 add_to_peer (struct CadetConnection *c,
1600 struct CadetPeer *peer)
1602 GCP_add_tunnel (peer);
1603 c->t = GCP_get_tunnel (peer);
1604 GCT_add_connection (c->t, c);
1610 * Iterator to compare each connection's path with the path of a new connection.
1612 * If the connection conincides, the c member of path is set to the connection
1613 * and the destroy flag of the connection is set.
1615 * @param cls Closure (new path).
1616 * @param c Connection in the tunnel to check.
1619 check_path (void *cls, struct CadetConnection *c)
1621 struct CadetConnection *new_conn = cls;
1622 struct CadetPeerPath *path = new_conn->path;
1624 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1625 GCC_2s (c), c, c->path->length);
1628 && c->destroy == GNUNET_NO
1629 && c->state != CADET_CONNECTION_BROKEN
1630 && c->state != CADET_CONNECTION_DESTROYED
1631 && path_equivalent (path, c->path))
1633 new_conn->destroy = GNUNET_YES;
1634 new_conn->path->c = c;
1635 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1640 * Finds out if this path is already being used by and existing connection.
1642 * Checks the tunnel towards the first peer in the path to see if it contains
1643 * any connection with the same path.
1645 * If the existing connection is ready, it is kept.
1646 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1647 * the peer will eventually reject our attempt).
1649 * @param path Path to check.
1651 * @return GNUNET_YES if the tunnel has a connection with the same path,
1652 * GNUNET_NO otherwise.
1655 does_connection_exist (struct CadetConnection *conn)
1657 struct CadetPeer *p;
1658 struct CadetTunnel *t;
1659 struct CadetConnection *c;
1661 p = GCP_get_short (conn->path->peers[0]);
1662 t = GCP_get_tunnel (p);
1666 LOG (GNUNET_ERROR_TYPE_DEBUG,
1667 "Checking for duplicates\n");
1669 GCT_iterate_connections (t, &check_path, conn);
1671 if (GNUNET_YES == conn->destroy)
1674 conn->destroy = GNUNET_NO;
1675 conn->path->c = conn;
1676 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1677 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1678 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1679 if (CADET_CONNECTION_READY == c->state)
1681 /* The other peer confirmed a live connection with this path,
1682 * why is it trying to duplicate it. */
1686 if (GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GCP_get_id (p)) > 0)
1688 struct CadetPeer *neighbor;
1690 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate allowed (killing old)\n");
1691 if (GCC_is_origin (c, GNUNET_YES))
1692 neighbor = get_next_hop (c);
1694 neighbor = get_prev_hop (c);
1695 send_broken_unknown (&c->id, &my_full_id, NULL,
1696 GCP_get_id (neighbor));
1705 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1712 * Log receipt of message on stderr (INFO level).
1714 * @param message Message received.
1715 * @param peer Peer who sent the message.
1716 * @param hash Connection ID.
1719 log_message (const struct GNUNET_MessageHeader *message,
1720 const struct GNUNET_PeerIdentity *peer,
1721 const struct GNUNET_CADET_Hash *hash)
1725 size = ntohs (message->size);
1726 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1727 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1728 LOG (GNUNET_ERROR_TYPE_INFO, "<-- %s on connection %s from %s, %6u bytes\n",
1729 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (hash)),
1730 GNUNET_i2s (peer), (unsigned int) size);
1733 /******************************************************************************/
1734 /******************************** API ***********************************/
1735 /******************************************************************************/
1738 * Core handler for connection creation.
1740 * @param cls Closure (unused).
1741 * @param peer Sender (neighbor).
1742 * @param message Message.
1744 * @return GNUNET_OK to keep the connection open,
1745 * GNUNET_SYSERR to close it (signal serious error)
1748 GCC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1749 const struct GNUNET_MessageHeader *message)
1751 struct GNUNET_CADET_ConnectionCreate *msg;
1752 struct GNUNET_PeerIdentity *id;
1753 struct GNUNET_CADET_Hash *cid;
1754 struct CadetPeerPath *path;
1755 struct CadetPeer *dest_peer;
1756 struct CadetPeer *orig_peer;
1757 struct CadetConnection *c;
1758 unsigned int own_pos;
1762 size = ntohs (message->size);
1763 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1765 GNUNET_break_op (0);
1769 /* Calculate hops */
1770 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1771 if (size % sizeof (struct GNUNET_PeerIdentity))
1773 GNUNET_break_op (0);
1776 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1778 GNUNET_break_op (0);
1781 size /= sizeof (struct GNUNET_PeerIdentity);
1784 GNUNET_break_op (0);
1787 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1789 /* Get parameters */
1790 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
1792 log_message (message, peer, cid);
1793 id = (struct GNUNET_PeerIdentity *) &msg[1];
1794 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1796 /* Create connection */
1797 c = connection_get (cid);
1800 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1801 size, myid, &own_pos);
1807 GNUNET_break_op (0);
1808 path_destroy (path);
1812 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1813 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1814 c = GCC_new (cid, NULL, path, own_pos);
1817 if (path->length - 1 == own_pos)
1819 /* If we are destination, why did the creation fail? */
1821 path_destroy (path);
1824 send_broken_unknown (cid, &my_full_id,
1825 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1827 path_destroy (path);
1830 GCP_add_path_to_all (path, GNUNET_NO);
1831 connection_reset_timeout (c, GNUNET_YES);
1835 path = path_duplicate (c->path);
1837 if (CADET_CONNECTION_NEW == c->state)
1838 connection_change_state (c, CADET_CONNECTION_SENT);
1840 /* Remember peers */
1841 dest_peer = GCP_get (&id[size - 1]);
1842 orig_peer = GCP_get (&id[0]);
1844 /* Is it a connection to us? */
1845 if (c->own_pos == path->length - 1)
1847 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1848 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1850 add_to_peer (c, orig_peer);
1851 if (GNUNET_YES == does_connection_exist (c))
1853 path_destroy (path);
1855 // FIXME Peer created a connection equal to one we think exists
1856 // and is fine. What should we do?
1857 // Use explicit duplicate?
1858 // Accept new conn and destroy the old? (interruption in higher level)
1859 // Keep both and postpone disambiguation?
1860 // Keep the one created by peer with higher ID?
1861 // For now: reject new connection until current confirmed dead
1862 GNUNET_break_op (0);
1863 send_broken_unknown (cid, &my_full_id, NULL, peer);
1868 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
1869 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
1871 send_connection_ack (c, GNUNET_NO);
1872 if (CADET_CONNECTION_SENT == c->state)
1873 connection_change_state (c, CADET_CONNECTION_ACK);
1877 /* It's for somebody else! Retransmit. */
1878 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
1879 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1880 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1881 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
1882 GNUNET_YES, GNUNET_YES,
1885 path_destroy (path);
1891 * Core handler for path confirmations.
1893 * @param cls closure
1894 * @param message message
1895 * @param peer peer identity this notification is about
1897 * @return GNUNET_OK to keep the connection open,
1898 * GNUNET_SYSERR to close it (signal serious error)
1901 GCC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1902 const struct GNUNET_MessageHeader *message)
1904 struct GNUNET_CADET_ConnectionACK *msg;
1905 struct CadetConnection *c;
1906 struct CadetPeerPath *p;
1907 struct CadetPeer *pi;
1908 enum CadetConnectionState oldstate;
1911 msg = (struct GNUNET_CADET_ConnectionACK *) message;
1912 log_message (message, peer, &msg->cid);
1913 c = connection_get (&msg->cid);
1916 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1918 LOG (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
1919 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
1923 if (GNUNET_NO != c->destroy)
1925 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection being destroyed\n");
1929 oldstate = c->state;
1930 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
1931 pi = GCP_get (peer);
1932 if (get_next_hop (c) == pi)
1934 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
1936 if (CADET_CONNECTION_SENT == oldstate)
1937 connection_change_state (c, CADET_CONNECTION_ACK);
1939 else if (get_prev_hop (c) == pi)
1941 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
1943 connection_change_state (c, CADET_CONNECTION_READY);
1947 GNUNET_break_op (0);
1951 connection_reset_timeout (c, fwd);
1953 /* Add path to peers? */
1957 GCP_add_path_to_all (p, GNUNET_YES);
1964 /* Message for us as creator? */
1965 if (GCC_is_origin (c, GNUNET_YES))
1967 if (GNUNET_NO != fwd)
1969 GNUNET_break_op (0);
1972 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
1974 /* If just created, cancel the short timeout and start a long one */
1975 if (CADET_CONNECTION_SENT == oldstate)
1976 connection_reset_timeout (c, GNUNET_YES);
1978 /* Change connection state */
1979 connection_change_state (c, CADET_CONNECTION_READY);
1980 send_connection_ack (c, GNUNET_YES);
1982 /* Change tunnel state, trigger KX */
1983 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
1984 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
1989 /* Message for us as destination? */
1990 if (GCC_is_terminal (c, GNUNET_YES))
1992 if (GNUNET_YES != fwd)
1994 GNUNET_break_op (0);
1997 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
1999 /* If just created, cancel the short timeout and start a long one */
2000 if (CADET_CONNECTION_ACK == oldstate)
2001 connection_reset_timeout (c, GNUNET_NO);
2003 /* Change tunnel state */
2004 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2005 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2010 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2011 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2012 GNUNET_YES, NULL, NULL));
2018 * Core handler for notifications of broken connections.
2020 * @param cls Closure (unused).
2021 * @param id Peer identity of sending neighbor.
2022 * @param message Message.
2024 * @return #GNUNET_OK to keep the connection open,
2025 * #GNUNET_SYSERR to close it (signal serious error)
2028 GCC_handle_broken (void* cls,
2029 const struct GNUNET_PeerIdentity* id,
2030 const struct GNUNET_MessageHeader* message)
2032 struct GNUNET_CADET_ConnectionBroken *msg;
2033 struct CadetConnection *c;
2034 struct CadetTunnel *t;
2038 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
2039 log_message (message, id, &msg->cid);
2040 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2041 GNUNET_i2s (&msg->peer1));
2042 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2043 GNUNET_i2s (&msg->peer2));
2044 c = connection_get (&msg->cid);
2047 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2053 fwd = is_fwd (c, id);
2054 c->destroy = GNUNET_YES;
2055 if (GCC_is_terminal (c, fwd))
2057 struct CadetPeer *endpoint;
2061 /* A terminal connection should not have 't' set to NULL. */
2063 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2066 endpoint = GCP_get_short (c->path->peers[c->path->length - 1]);
2067 if (2 < c->path->length)
2068 path_invalidate (c->path);
2069 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2071 c->state = CADET_CONNECTION_BROKEN;
2072 GCT_remove_connection (t, c);
2075 pending = c->pending_messages;
2077 resend_messages_and_destroy (c, !fwd);
2083 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2084 GNUNET_YES, NULL, NULL));
2085 connection_cancel_queues (c, !fwd);
2093 * Core handler for tunnel destruction
2095 * @param cls Closure (unused).
2096 * @param peer Peer identity of sending neighbor.
2097 * @param message Message.
2098 * @return #GNUNET_OK to keep the connection open,
2099 * #GNUNET_SYSERR to close it (signal serious error)
2102 GCC_handle_destroy (void *cls,
2103 const struct GNUNET_PeerIdentity *peer,
2104 const struct GNUNET_MessageHeader *message)
2106 const struct GNUNET_CADET_ConnectionDestroy *msg;
2107 struct CadetConnection *c;
2110 msg = (const struct GNUNET_CADET_ConnectionDestroy *) message;
2111 log_message (message, peer, &msg->cid);
2112 c = connection_get (&msg->cid);
2115 /* Probably already got the message from another path,
2116 * destroyed the tunnel and retransmitted to children.
2119 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2121 LOG (GNUNET_ERROR_TYPE_DEBUG,
2122 " connection unknown: already destroyed?\n");
2125 fwd = is_fwd (c, peer);
2126 if (GNUNET_SYSERR == fwd)
2128 GNUNET_break_op (0); /* FIXME */
2131 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2132 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2133 GNUNET_YES, NULL, NULL));
2134 else if (0 == c->pending_messages)
2136 LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
2140 c->destroy = GNUNET_YES;
2141 c->state = CADET_CONNECTION_DESTROYED;
2144 GCT_remove_connection (c->t, c);
2153 * Check the message against internal state and test if it goes FWD or BCK.
2155 * Updates the PID, state and timeout values for the connection.
2157 * @param message Message to check. It must belong to an existing connection.
2158 * @param minimum_size The message cannot be smaller than this value.
2159 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2160 * @param c Connection this message should belong. If NULL, check fails.
2161 * @param neighbor Neighbor that sent the message.
2164 check_message (const struct GNUNET_MessageHeader *message,
2165 size_t minimum_size,
2166 const struct GNUNET_CADET_Hash* cid,
2167 struct CadetConnection *c,
2168 const struct GNUNET_PeerIdentity *neighbor,
2171 GNUNET_PEER_Id neighbor_id;
2172 struct CadetFlowControl *fc;
2173 struct CadetPeer *hop;
2178 if (ntohs (message->size) < minimum_size)
2180 GNUNET_break_op (0);
2181 LOG (GNUNET_ERROR_TYPE_WARNING, "Size %u < %u\n",
2182 ntohs (message->size), minimum_size);
2183 return GNUNET_SYSERR;
2186 /* Check connection */
2189 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
2190 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s on unknown connection %s\n",
2191 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (cid)));
2192 send_broken_unknown (cid, &my_full_id, NULL, neighbor);
2193 return GNUNET_SYSERR;
2196 /* Check if origin is as expected */
2197 neighbor_id = GNUNET_PEER_search (neighbor);
2198 hop = get_prev_hop (c);
2199 if (neighbor_id == GCP_get_short_id (hop))
2205 hop = get_next_hop (c);
2206 GNUNET_break (hop == c->next_peer);
2207 if (neighbor_id == GCP_get_short_id (hop))
2213 /* Unexpected peer sending traffic on a connection. */
2214 GNUNET_break_op (0);
2215 return GNUNET_SYSERR;
2219 /* Check PID for payload messages */
2220 type = ntohs (message->type);
2221 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
2222 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
2224 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2225 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2226 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2227 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2229 GNUNET_break_op (0);
2230 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2231 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2232 pid, fc->last_pid_recv, fc->last_ack_sent);
2233 return GNUNET_SYSERR;
2235 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2239 delta = pid - fc->last_pid_recv;
2240 fc->last_pid_recv = pid;
2241 fc->recv_bitmap <<= delta;
2242 fc->recv_bitmap |= 1;
2246 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2247 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2249 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2250 pid, fc->last_pid_recv - 31);
2251 return GNUNET_SYSERR;
2253 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2257 /* Count as connection confirmation. */
2258 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2260 connection_change_state (c, CADET_CONNECTION_READY);
2263 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2264 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2267 connection_reset_timeout (c, fwd);
2274 * Generic handler for cadet network encrypted traffic.
2276 * @param peer Peer identity this notification is about.
2277 * @param msg Encrypted message.
2279 * @return GNUNET_OK to keep the connection open,
2280 * GNUNET_SYSERR to close it (signal serious error)
2283 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
2284 const struct GNUNET_MessageHeader *message)
2286 const struct GNUNET_CADET_Encrypted *otr_msg;
2287 const struct GNUNET_CADET_AX *ax_msg;
2288 const struct GNUNET_CADET_Hash* cid;
2289 struct CadetConnection *c;
2290 size_t minumum_size;
2296 if (GNUNET_MESSAGE_TYPE_CADET_AX == ntohs (message->type))
2298 overhead = sizeof (struct GNUNET_CADET_AX);
2299 ax_msg = (const struct GNUNET_CADET_AX *) message;
2301 pid = ntohl (ax_msg->pid);
2306 overhead = sizeof (struct GNUNET_CADET_Encrypted);
2307 otr_msg = (const struct GNUNET_CADET_Encrypted *) message;
2308 cid = &otr_msg->cid;
2309 pid = ntohl (otr_msg->pid);
2312 log_message (message, peer, cid);
2314 minumum_size = sizeof (struct GNUNET_MessageHeader) + overhead;
2315 c = connection_get (cid);
2316 fwd = check_message (message, minumum_size, cid, c, peer, pid);
2318 /* If something went wrong, discard message. */
2319 if (GNUNET_SYSERR == fwd)
2322 /* Is this message for us? */
2323 if (GCC_is_terminal (c, fwd))
2325 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2329 GNUNET_break (GNUNET_NO != c->destroy);
2332 GCT_handle_encrypted (c->t, message);
2333 GCC_send_ack (c, fwd, GNUNET_NO);
2337 /* Message not for us: forward to next hop */
2338 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2339 if (NULL != otr_msg) /* only otr has ttl */
2341 ttl = ntohl (otr_msg->ttl);
2342 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2345 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2346 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2347 GCC_send_ack (c, fwd, GNUNET_NO);
2352 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2353 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2354 GNUNET_NO, NULL, NULL));
2360 * Generic handler for cadet network encrypted traffic.
2362 * @param peer Peer identity this notification is about.
2363 * @param msg Encrypted message.
2365 * @return GNUNET_OK to keep the connection open,
2366 * GNUNET_SYSERR to close it (signal serious error)
2369 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2370 const struct GNUNET_CADET_KX *msg)
2372 const struct GNUNET_CADET_Hash* cid;
2373 struct CadetConnection *c;
2374 size_t expected_size;
2378 log_message (&msg->header, peer, cid);
2380 expected_size = sizeof (struct GNUNET_CADET_KX)
2381 + sizeof (struct GNUNET_MessageHeader);
2382 c = connection_get (cid);
2383 fwd = check_message (&msg->header, expected_size, cid, c, peer, 0);
2385 /* If something went wrong, discard message. */
2386 if (GNUNET_SYSERR == fwd)
2389 /* Is this message for us? */
2390 if (GCC_is_terminal (c, fwd))
2392 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2393 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2399 GCT_handle_kx (c->t, &msg[1].header);
2403 /* Message not for us: forward to next hop */
2404 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2405 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2406 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2407 GNUNET_NO, NULL, NULL));
2414 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2416 * @param cls Closure (unused).
2417 * @param message Message received.
2418 * @param peer Peer who sent the message.
2420 * @return GNUNET_OK to keep the connection open,
2421 * GNUNET_SYSERR to close it (signal serious error)
2424 GCC_handle_kx (void *cls, const struct GNUNET_PeerIdentity *peer,
2425 const struct GNUNET_MessageHeader *message)
2427 return handle_cadet_kx (peer, (struct GNUNET_CADET_KX *) message);
2432 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2434 * @param cls Closure (unused).
2435 * @param message Message received.
2436 * @param peer Peer who sent the message.
2438 * @return GNUNET_OK to keep the connection open,
2439 * GNUNET_SYSERR to close it (signal serious error)
2442 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2443 const struct GNUNET_MessageHeader *message)
2445 return handle_cadet_encrypted (peer, message);
2450 * Core handler for cadet network traffic point-to-point acks.
2452 * @param cls closure
2453 * @param message message
2454 * @param peer peer identity this notification is about
2456 * @return GNUNET_OK to keep the connection open,
2457 * GNUNET_SYSERR to close it (signal serious error)
2460 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2461 const struct GNUNET_MessageHeader *message)
2463 struct GNUNET_CADET_ACK *msg;
2464 struct CadetConnection *c;
2465 struct CadetFlowControl *fc;
2470 msg = (struct GNUNET_CADET_ACK *) message;
2471 log_message (message, peer, &msg->cid);
2472 c = connection_get (&msg->cid);
2475 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
2477 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2481 /* Is this a forward or backward ACK? */
2482 id = GNUNET_PEER_search (peer);
2483 if (GCP_get_short_id (get_next_hop (c)) == id)
2488 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2495 GNUNET_break_op (0);
2499 ack = ntohl (msg->ack);
2500 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2501 GC_f2s (fwd), ack, fc->last_ack_recv);
2502 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2503 fc->last_ack_recv = ack;
2505 /* Cancel polling if the ACK is big enough. */
2506 if (NULL != fc->poll_task &&
2507 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2509 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2510 GNUNET_SCHEDULER_cancel (fc->poll_task);
2511 fc->poll_task = NULL;
2512 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2515 connection_unlock_queue (c, fwd);
2522 * Core handler for cadet network traffic point-to-point ack polls.
2524 * @param cls closure
2525 * @param message message
2526 * @param peer peer identity this notification is about
2528 * @return GNUNET_OK to keep the connection open,
2529 * GNUNET_SYSERR to close it (signal serious error)
2532 GCC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
2533 const struct GNUNET_MessageHeader *message)
2535 struct GNUNET_CADET_Poll *msg;
2536 struct CadetConnection *c;
2537 struct CadetFlowControl *fc;
2542 msg = (struct GNUNET_CADET_Poll *) message;
2543 log_message (message, peer, &msg->cid);
2544 c = connection_get (&msg->cid);
2547 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2549 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL message on unknown connection %s!\n",
2550 GNUNET_h2s (GC_h2hc (&msg->cid)));
2551 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2555 /* Is this a forward or backward ACK?
2556 * Note: a poll should never be needed in a loopback case,
2557 * since there is no possiblility of packet loss there, so
2558 * this way of discerining FWD/BCK should not be a problem.
2560 id = GNUNET_PEER_search (peer);
2561 if (GCP_get_short_id (get_next_hop (c)) == id)
2563 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2566 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2568 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2573 GNUNET_break_op (0);
2577 pid = ntohl (msg->pid);
2578 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2579 fc->last_pid_recv = pid;
2580 fwd = fc == &c->bck_fc;
2581 GCC_send_ack (c, fwd, GNUNET_YES);
2588 * Send an ACK on the appropriate connection/channel, depending on
2589 * the direction and the position of the peer.
2591 * @param c Which connection to send the hop-by-hop ACK.
2592 * @param fwd Is this a fwd ACK? (will go dest->root).
2593 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2596 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2598 unsigned int buffer;
2600 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
2601 GC_f2s (fwd), GCC_2s (c));
2609 if (GNUNET_NO != c->destroy)
2611 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2615 /* Get available buffer space */
2616 if (GCC_is_terminal (c, fwd))
2618 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2619 buffer = GCT_get_channels_buffer (c->t);
2623 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2624 buffer = GCC_get_buffer (c, fwd);
2626 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2627 if (0 == buffer && GNUNET_NO == force)
2630 /* Send available buffer space */
2631 if (GCC_is_origin (c, fwd))
2633 GNUNET_assert (NULL != c->t);
2634 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2635 GCT_unchoke_channels (c->t);
2639 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2640 send_ack (c, buffer, fwd, force);
2646 * Initialize the connections subsystem
2648 * @param c Configuration handle.
2651 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2653 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2655 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2658 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2659 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2660 GNUNET_SCHEDULER_shutdown ();
2665 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2668 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2669 "CADET", "MAX_CONNECTIONS", "MISSING");
2670 GNUNET_SCHEDULER_shutdown ();
2675 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2676 &refresh_connection_time))
2678 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2679 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2680 GNUNET_SCHEDULER_shutdown ();
2683 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2684 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2689 * Destroy each connection on shutdown.
2691 * @param cls Closure (unused).
2692 * @param key Current key code (CID, unused).
2693 * @param value Value in the hash map (`struct CadetConnection`)
2695 * @return #GNUNET_YES, because we should continue to iterate
2698 shutdown_iterator (void *cls,
2699 const struct GNUNET_HashCode *key,
2702 struct CadetConnection *c = value;
2704 c->state = CADET_CONNECTION_DESTROYED;
2711 * Shut down the connections subsystem.
2716 GNUNET_CONTAINER_multihashmap_iterate (connections,
2719 GNUNET_CONTAINER_multihashmap_destroy (connections);
2725 * Create a connection.
2727 * @param cid Connection ID (either created locally or imposed remotely).
2728 * @param t Tunnel this connection belongs to (or NULL);
2729 * @param path Path this connection has to use (copy is made).
2730 * @param own_pos Own position in the @c path path.
2732 * @return Newly created connection, NULL in case of error (own id not in path).
2734 struct CadetConnection *
2735 GCC_new (const struct GNUNET_CADET_Hash *cid,
2736 struct CadetTunnel *t,
2737 struct CadetPeerPath *path,
2738 unsigned int own_pos)
2740 struct CadetConnection *c;
2741 struct CadetPeerPath *p;
2743 p = path_duplicate (path);
2744 c = GNUNET_new (struct CadetConnection);
2746 GNUNET_assert (GNUNET_OK ==
2747 GNUNET_CONTAINER_multihashmap_put (connections,
2749 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2750 fc_init (&c->fwd_fc);
2751 fc_init (&c->bck_fc);
2756 GNUNET_assert (own_pos <= p->length - 1);
2757 c->own_pos = own_pos;
2760 GNUNET_assert (NULL != p);
2761 if (GNUNET_OK != register_neighbors (c))
2765 path_invalidate (c->path);
2772 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2778 GCC_destroy (struct CadetConnection *c)
2786 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2787 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2790 LOG (GNUNET_ERROR_TYPE_DEBUG,
2791 "destroying connection %s\n",
2793 LOG (GNUNET_ERROR_TYPE_DEBUG,
2794 " fc's f: %p, b: %p\n",
2795 &c->fwd_fc, &c->bck_fc);
2796 LOG (GNUNET_ERROR_TYPE_DEBUG,
2797 " fc tasks f: %u, b: %u\n",
2798 c->fwd_fc.poll_task,
2799 c->bck_fc.poll_task);
2801 /* Cancel all traffic */
2802 if (NULL != c->path)
2804 connection_cancel_queues (c, GNUNET_YES);
2805 connection_cancel_queues (c, GNUNET_NO);
2807 unregister_neighbors (c);
2808 path_destroy (c->path);
2811 /* Cancel maintainance task (keepalive/timeout) */
2812 if (NULL != c->fwd_fc.poll_msg)
2814 GCC_cancel (c->fwd_fc.poll_msg);
2815 LOG (GNUNET_ERROR_TYPE_DEBUG,
2816 " POLL msg FWD canceled\n");
2818 if (NULL != c->bck_fc.poll_msg)
2820 GCC_cancel (c->bck_fc.poll_msg);
2821 LOG (GNUNET_ERROR_TYPE_DEBUG,
2822 " POLL msg BCK canceled\n");
2825 /* Delete from tunnel */
2827 GCT_remove_connection (c->t, c);
2829 if (NULL != c->fwd_maintenance_task)
2830 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
2831 if (NULL != c->bck_maintenance_task)
2832 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
2833 if (NULL != c->fwd_fc.poll_task)
2835 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
2836 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task FWD canceled\n");
2838 if (NULL != c->bck_fc.poll_task)
2840 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
2841 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task BCK canceled\n");
2844 GNUNET_break (GNUNET_YES ==
2845 GNUNET_CONTAINER_multihashmap_remove (connections,
2848 GNUNET_STATISTICS_update (stats,
2857 * Get the connection ID.
2859 * @param c Connection to get the ID from.
2861 * @return ID of the connection.
2863 const struct GNUNET_CADET_Hash *
2864 GCC_get_id (const struct CadetConnection *c)
2871 * Get the connection ID.
2873 * @param c Connection to get the ID from.
2875 * @return ID of the connection.
2877 const struct GNUNET_HashCode *
2878 GCC_get_h (const struct CadetConnection *c)
2880 return GC_h2hc (&c->id);
2885 * Get the connection path.
2887 * @param c Connection to get the path from.
2889 * @return path used by the connection.
2891 const struct CadetPeerPath *
2892 GCC_get_path (const struct CadetConnection *c)
2894 if (GNUNET_NO == c->destroy)
2901 * Get the connection state.
2903 * @param c Connection to get the state from.
2905 * @return state of the connection.
2907 enum CadetConnectionState
2908 GCC_get_state (const struct CadetConnection *c)
2914 * Get the connection tunnel.
2916 * @param c Connection to get the tunnel from.
2918 * @return tunnel of the connection.
2920 struct CadetTunnel *
2921 GCC_get_tunnel (const struct CadetConnection *c)
2928 * Get free buffer space in a connection.
2930 * @param c Connection.
2931 * @param fwd Is query about FWD traffic?
2933 * @return Free buffer space [0 - max_msgs_queue/max_connections]
2936 GCC_get_buffer (struct CadetConnection *c, int fwd)
2938 struct CadetFlowControl *fc;
2940 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2942 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
2943 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
2944 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
2946 return (fc->queue_max - fc->queue_n);
2950 * Get how many messages have we allowed to send to us from a direction.
2952 * @param c Connection.
2953 * @param fwd Are we asking about traffic from FWD (BCK messages)?
2955 * @return last_ack_sent - last_pid_recv
2958 GCC_get_allowed (struct CadetConnection *c, int fwd)
2960 struct CadetFlowControl *fc;
2962 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2963 if (CADET_CONNECTION_READY != c->state
2964 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
2968 return (fc->last_ack_sent - fc->last_pid_recv);
2972 * Get messages queued in a connection.
2974 * @param c Connection.
2975 * @param fwd Is query about FWD traffic?
2977 * @return Number of messages queued.
2980 GCC_get_qn (struct CadetConnection *c, int fwd)
2982 struct CadetFlowControl *fc;
2984 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2991 * Get next PID to use.
2993 * @param c Connection.
2994 * @param fwd Is query about FWD traffic?
2996 * @return Last PID used + 1.
2999 GCC_get_pid (struct CadetConnection *c, int fwd)
3001 struct CadetFlowControl *fc;
3003 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3005 return fc->last_pid_sent + 1;
3010 * Allow the connection to advertise a buffer of the given size.
3012 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3013 * allowing up to last_pid_recv + buffer.
3015 * @param c Connection.
3016 * @param buffer How many more messages the connection can accept.
3017 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3020 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3022 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3023 GCC_2s (c), buffer, GC_f2s (fwd));
3024 send_ack (c, buffer, fwd, GNUNET_NO);
3029 * Notify other peers on a connection of a broken link. Mark connections
3030 * to destroy after all traffic has been sent.
3032 * @param c Connection on which there has been a disconnection.
3033 * @param peer Peer that disconnected.
3036 GCC_notify_broken (struct CadetConnection *c,
3037 struct CadetPeer *peer)
3039 struct CadetPeer *hop;
3042 LOG (GNUNET_ERROR_TYPE_DEBUG,
3043 "Notify broken on %s due to %s disconnect\n",
3046 hop = get_prev_hop (c);
3049 /* Path was NULL, we should have deleted the connection. */
3053 fwd = (peer == hop);
3054 if (GNUNET_YES == GCC_is_terminal (c, fwd))
3056 /* Local shutdown, no one to notify about this. */
3060 if (GNUNET_NO == c->destroy)
3061 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3063 /* Connection will have at least one pending message
3064 * (the one we just scheduled), so no point in checking whether to
3065 * destroy immediately. */
3066 c->destroy = GNUNET_YES;
3067 c->state = CADET_CONNECTION_DESTROYED;
3070 * Cancel all queues, if no message is left, connection will be destroyed.
3072 connection_cancel_queues (c, !fwd);
3077 * Is this peer the first one on the connection?
3079 * @param c Connection.
3080 * @param fwd Is this about fwd traffic?
3082 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3085 GCC_is_origin (struct CadetConnection *c, int fwd)
3087 if (!fwd && c->path->length - 1 == c->own_pos )
3089 if (fwd && 0 == c->own_pos)
3096 * Is this peer the last one on the connection?
3098 * @param c Connection.
3099 * @param fwd Is this about fwd traffic?
3100 * Note that the ROOT is the terminal for BCK traffic!
3102 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3105 GCC_is_terminal (struct CadetConnection *c, int fwd)
3107 return GCC_is_origin (c, !fwd);
3112 * See if we are allowed to send by the next hop in the given direction.
3114 * @param c Connection.
3115 * @param fwd Is this about fwd traffic?
3117 * @return #GNUNET_YES in case it's OK to send.
3120 GCC_is_sendable (struct CadetConnection *c, int fwd)
3122 struct CadetFlowControl *fc;
3124 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking sendability of %s traffic on %s\n",
3125 GC_f2s (fwd), GCC_2s (c));
3131 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3132 LOG (GNUNET_ERROR_TYPE_DEBUG,
3133 " last ack recv: %u, last pid sent: %u\n",
3134 fc->last_ack_recv, fc->last_pid_sent);
3135 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3137 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3140 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3146 * Check if this connection is a direct one (never trim a direct connection).
3148 * @param c Connection.
3150 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3153 GCC_is_direct (struct CadetConnection *c)
3155 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3159 * Sends an already built message on a connection, properly registering
3160 * all used resources.
3162 * @param message Message to send. Function makes a copy of it.
3163 * If message is not hop-by-hop, decrements TTL of copy.
3164 * @param payload_type Type of payload, in case the message is encrypted.
3165 * @param c Connection on which this message is transmitted.
3166 * @param fwd Is this a fwd message?
3167 * @param force Force the connection to accept the message (buffer overfill).
3168 * @param cont Continuation called once message is sent. Can be NULL.
3169 * @param cont_cls Closure for @c cont.
3171 * @return Handle to cancel the message before it's sent.
3172 * NULL on error or if @c cont is NULL.
3173 * Invalid on @c cont call.
3175 struct CadetConnectionQueue *
3176 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3177 uint16_t payload_type, uint32_t payload_id,
3178 struct CadetConnection *c, int fwd, int force,
3179 GCC_sent cont, void *cont_cls)
3181 struct CadetFlowControl *fc;
3182 struct CadetConnectionQueue *q;
3188 size = ntohs (message->size);
3189 data = GNUNET_malloc (size);
3190 memcpy (data, message, size);
3191 type = ntohs (message->type);
3192 LOG (GNUNET_ERROR_TYPE_INFO, "--> %s (%s %4u) on connection %s (%u bytes)\n",
3193 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), size);
3195 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3196 droppable = GNUNET_NO == force;
3199 struct GNUNET_CADET_AX *axmsg;
3200 struct GNUNET_CADET_Encrypted *emsg;
3201 struct GNUNET_CADET_KX *kmsg;
3202 struct GNUNET_CADET_ACK *amsg;
3203 struct GNUNET_CADET_Poll *pmsg;
3204 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3205 struct GNUNET_CADET_ConnectionBroken *bmsg;
3208 case GNUNET_MESSAGE_TYPE_CADET_AX:
3209 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3210 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3212 emsg = (struct GNUNET_CADET_Encrypted *) data;
3213 ttl = ntohl (emsg->ttl);
3216 GNUNET_break_op (0);
3221 emsg->ttl = htonl (ttl - 1);
3225 axmsg = (struct GNUNET_CADET_AX *) data;
3228 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3229 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3230 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3231 if (GNUNET_YES == droppable)
3237 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3241 case GNUNET_MESSAGE_TYPE_CADET_KX:
3242 kmsg = (struct GNUNET_CADET_KX *) data;
3246 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3247 amsg = (struct GNUNET_CADET_ACK *) data;
3249 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3250 droppable = GNUNET_NO;
3253 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3254 pmsg = (struct GNUNET_CADET_Poll *) data;
3256 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
3257 droppable = GNUNET_NO;
3260 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3261 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
3265 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3266 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
3270 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3271 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3280 if (fc->queue_n > fc->queue_max && droppable)
3282 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3285 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3286 fc->queue_n, fc->queue_max);
3287 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
3288 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
3293 return NULL; /* Drop this message */
3296 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3297 GCC_2s (c), c->pending_messages);
3298 c->pending_messages++;
3300 q = GNUNET_new (struct CadetConnectionQueue);
3301 q->forced = !droppable;
3302 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3303 size, c, fwd, &conn_message_sent, q);
3306 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3312 q->cont_cls = cont_cls;
3313 return (NULL == cont) ? NULL : q;
3318 * Cancel a previously sent message while it's in the queue.
3320 * ONLY can be called before the continuation given to the send function
3321 * is called. Once the continuation is called, the message is no longer in the
3324 * @param q Handle to the queue.
3327 GCC_cancel (struct CadetConnectionQueue *q)
3329 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3331 /* queue destroy calls message_sent, which calls q->cont and frees q */
3332 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3337 * Sends a CREATE CONNECTION message for a path to a peer.
3338 * Changes the connection and tunnel states if necessary.
3340 * @param connection Connection to create.
3343 GCC_send_create (struct CadetConnection *connection)
3345 enum CadetTunnelCState state;
3348 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3349 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3351 LOG (GNUNET_ERROR_TYPE_INFO, "---> %s on connection %s (%u bytes)\n",
3352 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE),
3353 GCC_2s (connection), size);
3354 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3355 connection, connection->pending_messages);
3356 connection->pending_messages++;
3358 connection->maintenance_q =
3359 GCP_queue_add (get_next_hop (connection), NULL,
3360 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 0,
3361 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3363 state = GCT_get_cstate (connection->t);
3364 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3365 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3366 if (CADET_CONNECTION_NEW == connection->state)
3367 connection_change_state (connection, CADET_CONNECTION_SENT);
3372 * Send a message to all peers in this connection that the connection
3373 * is no longer valid.
3375 * If some peer should not receive the message, it should be zero'ed out
3376 * before calling this function.
3378 * @param c The connection whose peers to notify.
3381 GCC_send_destroy (struct CadetConnection *c)
3383 struct GNUNET_CADET_ConnectionDestroy msg;
3385 if (GNUNET_YES == c->destroy)
3388 msg.header.size = htons (sizeof (msg));
3389 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3391 LOG (GNUNET_ERROR_TYPE_DEBUG,
3392 " sending connection destroy for connection %s\n",
3395 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3396 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3397 GNUNET_YES, GNUNET_YES,
3399 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3400 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3401 GNUNET_NO, GNUNET_YES,
3403 c->destroy = GNUNET_YES;
3404 c->state = CADET_CONNECTION_DESTROYED;
3409 * @brief Start a polling timer for the connection.
3411 * When a neighbor does not accept more traffic on the connection it could be
3412 * caused by a simple congestion or by a lost ACK. Polling enables to check
3413 * for the lastest ACK status for a connection.
3415 * @param c Connection.
3416 * @param fwd Should we poll in the FWD direction?
3419 GCC_start_poll (struct CadetConnection *c, int fwd)
3421 struct CadetFlowControl *fc;
3423 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3424 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3426 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3428 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not needed (%p, %p)\n",
3429 fc->poll_task, fc->poll_msg);
3432 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3433 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3440 * @brief Stop polling a connection for ACKs.
3442 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3444 * @param c Connection.
3445 * @param fwd Should we stop the poll in the FWD direction?
3448 GCC_stop_poll (struct CadetConnection *c, int fwd)
3450 struct CadetFlowControl *fc;
3452 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3453 if (NULL != fc->poll_task)
3455 GNUNET_SCHEDULER_cancel (fc->poll_task);
3456 fc->poll_task = NULL;
3461 * Get a (static) string for a connection.
3463 * @param c Connection.
3466 GCC_2s (const struct CadetConnection *c)
3473 static char buf[128];
3475 SPRINTF (buf, "%s (->%s)",
3476 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3479 return GNUNET_h2s (GC_h2hc (&c->id));
3484 * Log all possible info about the connection state.
3486 * @param c Connection to debug.
3487 * @param level Debug level to use.
3490 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3495 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3497 __FILE__, __FUNCTION__, __LINE__);
3503 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3507 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3508 s = path_2s (c->path);
3509 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3511 LOG2 (level, "CCC state: %s, destroy: %u\n",
3512 GCC_state2s (c->state), c->destroy);
3513 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3514 if (NULL != c->perf)
3515 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3517 LOG2 (level, "CCC FWD flow control:\n");
3518 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3519 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3520 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3521 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3522 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3523 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3524 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3525 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3527 LOG2 (level, "CCC BCK flow control:\n");
3528 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3529 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3530 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3531 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3532 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3533 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3534 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3535 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3537 LOG2 (level, "CCC DEBUG CONNECTION END\n");