2 This file is part of GNUnet.
3 Copyright (C) 2001-2015 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/gnunet-service-cadet_connection.c
22 * @brief GNUnet CADET service connection handling
23 * @author Bartlomiej Polot
26 #include "gnunet_util_lib.h"
27 #include "gnunet_statistics_service.h"
28 #include "cadet_path.h"
29 #include "cadet_protocol.h"
31 #include "gnunet-service-cadet_connection.h"
32 #include "gnunet-service-cadet_peer.h"
33 #include "gnunet-service-cadet_tunnel.h"
37 * Should we run somewhat expensive checks on our invariants?
39 #define CHECK_INVARIANTS 0
42 #define LOG(level, ...) GNUNET_log_from (level,"cadet-con",__VA_ARGS__)
43 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-con",__VA_ARGS__)
46 #define CADET_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
47 GNUNET_TIME_UNIT_MINUTES,\
52 /******************************************************************************/
53 /******************************** STRUCTS **********************************/
54 /******************************************************************************/
57 * Struct to encapsulate all the Flow Control information to a peer to which
58 * we are directly connected (on a core level).
60 struct CadetFlowControl
63 * Connection this controls.
65 struct CadetConnection *c;
68 * How many messages are in the queue on this connection.
73 * How many messages do we accept in the queue.
75 unsigned int queue_max;
78 * ID of the last packet sent towards the peer.
80 uint32_t last_pid_sent;
83 * ID of the last packet received from the peer.
85 uint32_t last_pid_recv;
88 * Bitmap of past 32 messages received:
89 * - LSB being @c last_pid_recv.
90 * - MSB being @c last_pid_recv - 31 (mod UINTMAX).
95 * Last ACK sent to the peer (peer can't send more than this PID).
97 uint32_t last_ack_sent;
100 * Last ACK sent towards the origin (for traffic towards leaf node).
102 uint32_t last_ack_recv;
105 * Task to poll the peer in case of a lost ACK causes stall.
107 struct GNUNET_SCHEDULER_Task *poll_task;
110 * How frequently to poll for ACKs.
112 struct GNUNET_TIME_Relative poll_time;
115 * Queued poll message, to cancel if not necessary anymore (got ACK).
117 struct CadetConnectionQueue *poll_msg;
120 * Queued poll message, to cancel if not necessary anymore (got ACK).
122 struct CadetConnectionQueue *ack_msg;
126 * Keep a record of the last messages sent on this connection.
128 struct CadetConnectionPerformance
131 * Circular buffer for storing measurements.
133 double usecsperbyte[AVG_MSGS];
136 * Running average of @c usecsperbyte.
141 * How many values of @c usecsperbyte are valid.
146 * Index of the next "free" position in @c usecsperbyte.
153 * Struct containing all information regarding a connection to a peer.
155 struct CadetConnection
158 * Tunnel this connection is part of.
160 struct CadetTunnel *t;
163 * Flow control information for traffic fwd.
165 struct CadetFlowControl fwd_fc;
168 * Flow control information for traffic bck.
170 struct CadetFlowControl bck_fc;
173 * Measure connection performance on the endpoint.
175 struct CadetConnectionPerformance *perf;
178 * ID of the connection.
180 struct GNUNET_CADET_Hash id;
183 * Path being used for the tunnel. At the origin of the connection
184 * it's a pointer to the destination's path pool, otherwise just a copy.
186 struct CadetPeerPath *path;
189 * Task to keep the used paths alive at the owner,
190 * time tunnel out on all the other peers.
192 struct GNUNET_SCHEDULER_Task *fwd_maintenance_task;
195 * Task to keep the used paths alive at the destination,
196 * time tunnel out on all the other peers.
198 struct GNUNET_SCHEDULER_Task *bck_maintenance_task;
201 * Queue handle for maintainance traffic. One handle for FWD and BCK since
202 * one peer never needs to maintain both directions (no loopback connections).
204 struct CadetPeerQueue *maintenance_q;
207 * Should equal #get_next_hop(), or NULL if that peer disconnected.
209 struct CadetPeer *next_peer;
212 * Should equal #get_prev_hop(), or NULL if that peer disconnected.
214 struct CadetPeer *prev_peer;
217 * State of the connection.
219 enum CadetConnectionState state;
222 * Position of the local peer in the path.
224 unsigned int own_pos;
227 * Pending message count.
229 unsigned int pending_messages;
233 * - if 0, connection in use.
234 * - if 1, destroy on last message.
235 * - if 2, connection is being destroyed don't re-enter.
240 * In-connection-map flag. Sometimes, when @e destroy is set but
241 * actual destruction is delayed to enable us to finish processing
242 * queues (i.e. in the direction that is still working), we remove
243 * the connection from the map to prevent it from still being
244 * found (and used) by accident. This flag is set to #GNUNET_YES
245 * for a connection that is not in the #connections map. Should
246 * only be #GNUNET_YES if #destroy is also non-zero.
251 * Counter to do exponential backoff when creating a connection (max 64).
253 unsigned short create_retry;
258 * Handle for messages queued but not yet sent.
260 struct CadetConnectionQueue
263 * Peer queue handle, to cancel if necessary.
265 struct CadetPeerQueue *q;
268 * Continuation to call once sent.
273 * Closure for @e cont.
278 * Was this a forced message? (Do not account for it)
283 /******************************************************************************/
284 /******************************* GLOBALS ***********************************/
285 /******************************************************************************/
288 * Global handle to the statistics service.
290 extern struct GNUNET_STATISTICS_Handle *stats;
293 * Local peer own ID (memory efficient handle).
295 extern GNUNET_PEER_Id myid;
298 * Local peer own ID (full value).
300 extern struct GNUNET_PeerIdentity my_full_id;
303 * Connections known, indexed by cid (CadetConnection).
305 static struct GNUNET_CONTAINER_MultiHashMap *connections;
308 * How many connections are we willing to maintain.
309 * Local connections are always allowed, even if there are more connections than max.
311 static unsigned long long max_connections;
314 * How many messages *in total* are we willing to queue, divide by number of
315 * connections to get connection queue size.
317 static unsigned long long max_msgs_queue;
320 * How often to send path keepalives. Paths timeout after 4 missed.
322 static struct GNUNET_TIME_Relative refresh_connection_time;
325 * How often to send path create / ACKs.
327 static struct GNUNET_TIME_Relative create_connection_time;
330 /******************************************************************************/
331 /******************************** STATIC ***********************************/
332 /******************************************************************************/
336 #if 0 // avoid compiler warning for unused static function
338 fc_debug (struct CadetFlowControl *fc)
340 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
341 fc->last_pid_recv, fc->last_ack_sent);
342 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
343 fc->last_pid_sent, fc->last_ack_recv);
344 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
345 fc->queue_n, fc->queue_max);
349 connection_debug (struct CadetConnection *c)
353 LOG (GNUNET_ERROR_TYPE_INFO, "DEBUG NULL CONNECTION\n");
356 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
357 peer2s (c->t->peer), GCC_2s (c));
358 LOG (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
359 c->state, c->pending_messages);
360 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
361 fc_debug (&c->fwd_fc);
362 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
363 fc_debug (&c->bck_fc);
369 * Schedule next keepalive task, taking in consideration
370 * the connection state and number of retries.
372 * @param c Connection for which to schedule the next keepalive.
373 * @param fwd Direction for the next keepalive.
376 schedule_next_keepalive (struct CadetConnection *c, int fwd);
380 * Resets the connection timeout task, some other message has done the
382 * - For the first peer on the direction this means to send
383 * a keepalive or a path confirmation message (either create or ACK).
384 * - For all other peers, this means to destroy the connection,
385 * due to lack of activity.
386 * Starts the timeout if no timeout was running (connection just created).
388 * @param c Connection whose timeout to reset.
389 * @param fwd Is this forward?
392 connection_reset_timeout (struct CadetConnection *c, int fwd);
396 * Get string description for tunnel state. Reentrant.
398 * @param s Tunnel state.
400 * @return String representation.
403 GCC_state2s (enum CadetConnectionState s)
407 case CADET_CONNECTION_NEW:
408 return "CADET_CONNECTION_NEW";
409 case CADET_CONNECTION_SENT:
410 return "CADET_CONNECTION_SENT";
411 case CADET_CONNECTION_ACK:
412 return "CADET_CONNECTION_ACK";
413 case CADET_CONNECTION_READY:
414 return "CADET_CONNECTION_READY";
415 case CADET_CONNECTION_DESTROYED:
416 return "CADET_CONNECTION_DESTROYED";
417 case CADET_CONNECTION_BROKEN:
418 return "CADET_CONNECTION_BROKEN";
421 LOG (GNUNET_ERROR_TYPE_ERROR, " conn state %u unknown!\n", s);
422 return "CADET_CONNECTION_STATE_ERROR";
428 * Initialize a Flow Control structure to the initial state.
430 * @param fc Flow Control structure to initialize.
433 fc_init (struct CadetFlowControl *fc)
435 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
436 fc->last_pid_recv = (uint32_t) -1;
437 fc->last_ack_sent = (uint32_t) 0;
438 fc->last_ack_recv = (uint32_t) 0;
439 fc->poll_task = NULL;
440 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
442 fc->queue_max = (max_msgs_queue / max_connections) + 1;
449 * @param cid Connection ID.
451 static struct CadetConnection *
452 connection_get (const struct GNUNET_CADET_Hash *cid)
454 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
459 connection_change_state (struct CadetConnection* c,
460 enum CadetConnectionState state)
462 LOG (GNUNET_ERROR_TYPE_DEBUG,
463 "Connection %s state %s -> %s\n",
464 GCC_2s (c), GCC_state2s (c->state), GCC_state2s (state));
465 if (CADET_CONNECTION_DESTROYED <= c->state) /* Destroyed or broken. */
467 LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
471 if (CADET_CONNECTION_READY == state)
477 * Callback called when a queued ACK message is sent.
479 * @param cls Closure (FC).
480 * @param c Connection this message was on.
481 * @param q Queue handler this call invalidates.
482 * @param type Type of message sent.
483 * @param fwd Was this a FWD going message?
484 * @param size Size of the message.
488 struct CadetConnection *c,
489 struct CadetConnectionQueue *q,
490 uint16_t type, int fwd, size_t size)
492 struct CadetFlowControl *fc = cls;
499 * Send an ACK on the connection, informing the predecessor about
500 * the available buffer space. Should not be called in case the peer
501 * is origin (no predecessor) in the @c fwd direction.
503 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
504 * the ACK itself goes "back" (dest->root).
506 * @param c Connection on which to send the ACK.
507 * @param buffer How much space free to advertise?
508 * @param fwd Is this FWD ACK? (Going dest -> root)
509 * @param force Don't optimize out.
512 send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
514 struct CadetFlowControl *next_fc;
515 struct CadetFlowControl *prev_fc;
516 struct GNUNET_CADET_ACK msg;
520 /* If origin, there is no connection to send ACKs. Wrong function! */
521 GCC_check_connections ();
522 if (GCC_is_origin (c, fwd))
524 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
525 GCC_2s (c), GC_f2s (fwd));
530 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
531 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
533 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
534 GC_f2s (fwd), GCC_2s (c));
536 /* Check if we need to transmit the ACK. */
537 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
538 if (3 < delta && buffer < delta && GNUNET_NO == force)
540 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
541 LOG (GNUNET_ERROR_TYPE_DEBUG,
542 " last pid recv: %u, last ack sent: %u\n",
543 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
544 GCC_check_connections ();
548 /* Ok, ACK might be necessary, what PID to ACK? */
549 ack = prev_fc->last_pid_recv + buffer;
550 LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
551 LOG (GNUNET_ERROR_TYPE_DEBUG,
552 " last pid %u, last ack %u, qmax %u, q %u\n",
553 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
554 next_fc->queue_max, next_fc->queue_n);
555 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
557 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
558 GCC_check_connections ();
562 /* Check if message is already in queue */
563 if (NULL != prev_fc->ack_msg)
565 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
567 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
568 GCC_cancel (prev_fc->ack_msg);
569 /* GCC_cancel triggers ack_sent(), which clears fc->ack_msg */
573 LOG (GNUNET_ERROR_TYPE_DEBUG, " same ACK already in queue\n");
574 GCC_check_connections ();
579 prev_fc->last_ack_sent = ack;
581 /* Build ACK message and send on connection */
582 msg.header.size = htons (sizeof (msg));
583 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
584 msg.ack = htonl (ack);
587 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, 0, ack, c,
590 GNUNET_assert (NULL != prev_fc->ack_msg);
591 GCC_check_connections ();
596 * Callback called when a connection queued message is sent.
598 * Calculates the average time and connection packet tracking.
600 * @param cls Closure (ConnectionQueue Handle).
601 * @param c Connection this message was on.
602 * @param sent Was it really sent? (Could have been canceled)
603 * @param type Type of message sent.
604 * @param pid Packet ID, or 0 if not applicable (create, destroy, etc).
605 * @param fwd Was this a FWD going message?
606 * @param size Size of the message.
607 * @param wait Time spent waiting for core (only the time for THIS message)
608 * @return #GNUNET_YES if connection was destroyed, #GNUNET_NO otherwise.
611 conn_message_sent (void *cls,
612 struct CadetConnection *c, int sent,
613 uint16_t type, uint32_t pid, int fwd, size_t size,
614 struct GNUNET_TIME_Relative wait)
616 struct CadetConnectionPerformance *p;
617 struct CadetFlowControl *fc;
618 struct CadetConnectionQueue *q = cls;
622 GCC_check_connections ();
623 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
625 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
627 fc = fwd ? &c->fwd_fc : &c->bck_fc;
628 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
629 sent ? "" : "not ", GC_f2s (fwd), GC_m2s (type), pid);
635 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cont\n");
636 q->cont (q->cont_cls, c, q, type, fwd, size);
640 else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED
641 || type == GNUNET_MESSAGE_TYPE_CADET_AX)
643 /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
652 if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
653 && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
655 LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
658 GCC_check_connections ();
661 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
662 c->pending_messages--;
663 if ( (GNUNET_YES == c->destroy) &&
664 (0 == c->pending_messages) )
666 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "! destroying connection!\n");
669 GCC_check_connections ();
672 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
675 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
676 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
677 c->maintenance_q = NULL;
678 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
679 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
680 schedule_next_keepalive (c, fwd);
683 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
684 case GNUNET_MESSAGE_TYPE_CADET_AX:
685 if (GNUNET_YES == sent)
687 GNUNET_assert (NULL != q);
688 fc->last_pid_sent = pid;
689 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
690 GCC_start_poll (c, fwd);
691 GCC_send_ack (c, fwd, GNUNET_NO);
692 connection_reset_timeout (c, fwd);
695 LOG (GNUNET_ERROR_TYPE_DEBUG, "! Q_N- %p %u\n", fc, fc->queue_n);
696 if (GNUNET_NO == forced)
699 LOG (GNUNET_ERROR_TYPE_DEBUG,
700 "! accounting pid %u\n",
705 LOG (GNUNET_ERROR_TYPE_DEBUG,
706 "! forced, Q_N not accounting pid %u\n",
711 case GNUNET_MESSAGE_TYPE_CADET_KX:
712 if (GNUNET_YES == sent)
713 connection_reset_timeout (c, fwd);
716 case GNUNET_MESSAGE_TYPE_CADET_POLL:
720 case GNUNET_MESSAGE_TYPE_CADET_ACK:
724 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
728 LOG (GNUNET_ERROR_TYPE_ERROR, "%s unknown\n", GC_m2s (type));
732 LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
735 return GNUNET_NO; /* Only endpoints are interested in timing. */
738 usecsperbyte = ((double) wait.rel_value_us) / size;
739 if (p->size == AVG_MSGS)
741 /* Array is full. Substract oldest value, add new one and store. */
742 p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
743 p->usecsperbyte[p->idx] = usecsperbyte;
744 p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
748 /* Array not yet full. Add current value to avg and store. */
749 p->usecsperbyte[p->idx] = usecsperbyte;
751 p->avg += p->usecsperbyte[p->idx];
755 p->idx = (p->idx + 1) % AVG_MSGS;
756 GCC_check_connections ();
762 * Get the previous hop in a connection
764 * @param c Connection.
766 * @return Previous peer in the connection.
768 static struct CadetPeer *
769 get_prev_hop (const struct CadetConnection *c)
775 LOG (GNUNET_ERROR_TYPE_DEBUG,
776 " get prev hop %s [%u/%u]\n",
777 GCC_2s (c), c->own_pos, c->path->length);
778 if (0 == c->own_pos || c->path->length < 2)
779 id = c->path->peers[0];
781 id = c->path->peers[c->own_pos - 1];
783 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
784 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
786 return GCP_get_short (id);
791 * Get the next hop in a connection
793 * @param c Connection.
795 * @return Next peer in the connection.
797 static struct CadetPeer *
798 get_next_hop (const struct CadetConnection *c)
805 LOG (GNUNET_ERROR_TYPE_DEBUG, " get next hop %s [%u/%u]\n",
806 GCC_2s (c), c->own_pos, c->path->length);
807 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
808 id = c->path->peers[c->path->length - 1];
810 id = c->path->peers[c->own_pos + 1];
812 LOG (GNUNET_ERROR_TYPE_DEBUG, " ID: %s (%u)\n",
813 GNUNET_i2s (GNUNET_PEER_resolve2 (id)), id);
815 return GCP_get_short (id);
820 * Check that the direct neighbours (previous and next hop)
821 * are properly associated with this connection.
823 * @param c connection to check
826 check_neighbours (const struct CadetConnection *c)
829 return; /* nothing to check */
830 GCP_check_connection (get_next_hop (c), c);
831 GCP_check_connection (get_prev_hop (c), c);
836 * Helper for #check_connections(). Calls #check_neighbours().
840 * @param value the `struct CadetConnection` to check
841 * @return #GNUNET_OK (continue to iterate)
844 check_connection (void *cls,
845 const struct GNUNET_HashCode *key,
848 struct CadetConnection *c = value;
850 check_neighbours (c);
856 * Check invariants for all connections using #check_neighbours().
859 GCC_check_connections ()
861 if (0 == CHECK_INVARIANTS)
863 if (NULL == connections)
865 GNUNET_CONTAINER_multihashmap_iterate (connections,
872 * Get the hop in a connection.
874 * @param c Connection.
875 * @param fwd Next in the FWD direction?
877 * @return Next peer in the connection.
879 static struct CadetPeer *
880 get_hop (struct CadetConnection *c, int fwd)
882 return (fwd) ? get_next_hop (c) : get_prev_hop (c);
887 * Get a bit mask for a message received out-of-order.
889 * @param last_pid_recv Last PID we received prior to the out-of-order.
890 * @param ooo_pid PID of the out-of-order message.
893 get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
895 return 1 << (last_pid_recv - ooo_pid);
900 * Check is an out-of-order message is ok:
901 * - at most 31 messages behind.
904 * @param last_pid_recv Last in-order PID received.
907 is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
911 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
914 mask = get_recv_bitmask (last_pid_recv, ooo_pid);
915 if (0 != (ooo_bitmap & mask))
923 * Is traffic coming from this sender 'FWD' traffic?
925 * @param c Connection to check.
926 * @param sender Peer identity of neighbor.
928 * @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
929 * the traffic is 'FWD'.
930 * #GNUNET_NO for BCK.
931 * #GNUNET_SYSERR for errors.
934 is_fwd (const struct CadetConnection *c,
935 const struct GNUNET_PeerIdentity *sender)
939 id = GNUNET_PEER_search (sender);
940 if (GCP_get_short_id (get_prev_hop (c)) == id)
943 if (GCP_get_short_id (get_next_hop (c)) == id)
947 return GNUNET_SYSERR;
952 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
953 * or a first CONNECTION_ACK directed to us.
955 * @param connection Connection to confirm.
956 * @param fwd Should we send it FWD? (root->dest)
957 * (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
960 send_connection_ack (struct CadetConnection *connection, int fwd)
962 struct CadetTunnel *t;
964 GCC_check_connections ();
966 LOG (GNUNET_ERROR_TYPE_INFO, "---> {%14s ACK} on connection %s\n",
967 GC_f2s (!fwd), GCC_2s (connection));
968 GCP_queue_add (get_hop (connection, fwd), NULL,
969 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0, 0,
970 sizeof (struct GNUNET_CADET_ConnectionACK),
971 connection, fwd, &conn_message_sent, NULL);
972 connection->pending_messages++;
973 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
974 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
975 if (CADET_CONNECTION_READY != connection->state)
976 connection_change_state (connection, CADET_CONNECTION_SENT);
977 GCC_check_connections ();
982 * Send a notification that a connection is broken.
984 * @param c Connection that is broken.
985 * @param id1 Peer that has disconnected.
986 * @param id2 Peer that has disconnected.
987 * @param fwd Direction towards which to send it.
990 send_broken (struct CadetConnection *c,
991 const struct GNUNET_PeerIdentity *id1,
992 const struct GNUNET_PeerIdentity *id2,
995 struct GNUNET_CADET_ConnectionBroken msg;
997 GCC_check_connections ();
998 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
999 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1003 GNUNET_assert (NULL ==
1004 GCC_send_prebuilt_message (&msg.header, 0, 0, c, fwd,
1005 GNUNET_YES, NULL, NULL));
1006 GCC_check_connections ();
1011 * Send a notification that a connection is broken, when a connection
1012 * isn't even known to the local peer or soon to be destroyed.
1014 * @param connection_id Connection ID.
1015 * @param id1 Peer that has disconnected, probably local peer.
1016 * @param id2 Peer that has disconnected can be NULL if unknown.
1017 * @param peer Peer to notify (neighbor who sent the connection).
1020 send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
1021 const struct GNUNET_PeerIdentity *id1,
1022 const struct GNUNET_PeerIdentity *id2,
1023 const struct GNUNET_PeerIdentity *peer_id)
1025 struct GNUNET_CADET_ConnectionBroken *msg;
1026 struct CadetPeer *neighbor;
1028 GCC_check_connections ();
1029 LOG (GNUNET_ERROR_TYPE_INFO, "---> BROKEN on unknown connection %s\n",
1030 GNUNET_h2s (GC_h2hc (connection_id)));
1032 msg = GNUNET_new (struct GNUNET_CADET_ConnectionBroken);
1033 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
1034 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1035 msg->cid = *connection_id;
1040 memset (&msg->peer2, 0, sizeof (msg->peer2));
1041 neighbor = GCP_get (peer_id);
1042 GCP_queue_add (neighbor, msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
1043 0, 2, sizeof (struct GNUNET_CADET_ConnectionBroken),
1044 NULL, GNUNET_SYSERR, /* connection, fwd */
1045 NULL, NULL); /* continuation */
1046 GCC_check_connections ();
1051 * Send keepalive packets for a connection.
1053 * @param c Connection to keep alive..
1054 * @param fwd Is this a FWD keepalive? (owner -> dest).
1057 send_connection_keepalive (struct CadetConnection *c, int fwd)
1059 struct GNUNET_MessageHeader msg;
1060 struct CadetFlowControl *fc;
1062 GCC_check_connections ();
1063 LOG (GNUNET_ERROR_TYPE_INFO,
1064 "keepalive %s for connection %s\n",
1065 GC_f2s (fwd), GCC_2s (c));
1067 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1068 if (0 < fc->queue_n)
1070 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1074 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1076 GNUNET_assert (NULL != c->t);
1077 msg.size = htons (sizeof (msg));
1078 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
1080 GNUNET_assert (NULL ==
1081 GCT_send_prebuilt_message (&msg, c->t, c,
1082 GNUNET_NO, NULL, NULL));
1083 GCC_check_connections ();
1088 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1090 * @param c Connection for which to send the message.
1091 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1094 connection_recreate (struct CadetConnection *c, int fwd)
1096 LOG (GNUNET_ERROR_TYPE_DEBUG,
1097 "sending connection recreate\n");
1099 GCC_send_create (c);
1101 send_connection_ack (c, GNUNET_NO);
1106 * Generic connection timer management.
1107 * Depending on the role of the peer in the connection will send the
1108 * appropriate message (build or keepalive)
1110 * @param c Conncetion to maintain.
1111 * @param fwd Is FWD?
1114 connection_maintain (struct CadetConnection *c, int fwd)
1116 if (GNUNET_NO != c->destroy)
1118 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1125 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1129 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1131 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1133 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1134 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1135 schedule_next_keepalive (c, fwd);
1140 case CADET_CONNECTION_NEW:
1143 case CADET_CONNECTION_SENT:
1144 connection_recreate (c, fwd);
1146 case CADET_CONNECTION_READY:
1147 send_connection_keepalive (c, fwd);
1156 * Keep the connection alive.
1158 * @param c Connection to keep alive.
1159 * @param fwd Direction.
1160 * @param shutdown Are we shutting down? (Don't send traffic)
1161 * Non-zero value for true, not necessarily GNUNET_YES.
1164 connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
1166 GCC_check_connections ();
1167 LOG (GNUNET_ERROR_TYPE_DEBUG,
1168 "%s keepalive for %s\n",
1169 GC_f2s (fwd), GCC_2s (c));
1172 c->fwd_maintenance_task = NULL;
1174 c->bck_maintenance_task = NULL;
1176 if (GNUNET_NO != shutdown)
1179 connection_maintain (c, fwd);
1180 GCC_check_connections ();
1181 /* Next execution will be scheduled by message_sent or _maintain*/
1186 * Keep the connection alive in the FWD direction.
1188 * @param cls Closure (connection to keepalive).
1189 * @param tc TaskContext.
1192 connection_fwd_keepalive (void *cls,
1193 const struct GNUNET_SCHEDULER_TaskContext *tc)
1195 GCC_check_connections ();
1196 connection_keepalive ((struct CadetConnection *) cls,
1198 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1199 GCC_check_connections ();
1204 * Keep the connection alive in the BCK direction.
1206 * @param cls Closure (connection to keepalive).
1207 * @param tc TaskContext.
1210 connection_bck_keepalive (void *cls,
1211 const struct GNUNET_SCHEDULER_TaskContext *tc)
1213 GCC_check_connections ();
1214 connection_keepalive ((struct CadetConnection *) cls,
1216 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1217 GCC_check_connections ();
1222 * Schedule next keepalive task, taking in consideration
1223 * the connection state and number of retries.
1225 * If the peer is not the origin, do nothing.
1227 * @param c Connection for which to schedule the next keepalive.
1228 * @param fwd Direction for the next keepalive.
1231 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1233 struct GNUNET_TIME_Relative delay;
1234 struct GNUNET_SCHEDULER_Task * *task_id;
1235 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1237 GCC_check_connections ();
1238 if (GNUNET_NO == GCC_is_origin (c, fwd))
1241 /* Calculate delay to use, depending on the state of the connection */
1242 if (CADET_CONNECTION_READY == c->state)
1244 delay = refresh_connection_time;
1248 if (1 > c->create_retry)
1249 c->create_retry = 1;
1250 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1252 if (c->create_retry < 64)
1253 c->create_retry *= 2;
1256 /* Select direction-dependent parameters */
1257 if (GNUNET_YES == fwd)
1259 task_id = &c->fwd_maintenance_task;
1260 keepalive_task = &connection_fwd_keepalive;
1264 task_id = &c->bck_maintenance_task;
1265 keepalive_task = &connection_bck_keepalive;
1268 /* Check that no one scheduled it before us */
1269 if (NULL != *task_id)
1271 /* No need for a _break. It can happen for instance when sending a SYNACK
1272 * for a duplicate SYN: the first SYNACK scheduled the task. */
1273 GNUNET_SCHEDULER_cancel (*task_id);
1276 /* Schedule the task */
1277 *task_id = GNUNET_SCHEDULER_add_delayed (delay,
1280 LOG (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1281 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1282 GCC_check_connections ();
1287 * @brief Re-initiate traffic on this connection if necessary.
1289 * Check if there is traffic queued towards this peer
1290 * and the core transmit handle is NULL (traffic was stalled).
1291 * If so, call core tmt rdy.
1293 * @param c Connection on which initiate traffic.
1294 * @param fwd Is this about fwd traffic?
1297 connection_unlock_queue (struct CadetConnection *c, int fwd)
1299 struct CadetPeer *peer;
1301 GCC_check_connections ();
1302 LOG (GNUNET_ERROR_TYPE_DEBUG,
1303 "connection_unlock_queue %s on %s\n",
1304 GC_f2s (fwd), GCC_2s (c));
1306 if (GCC_is_terminal (c, fwd))
1308 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal, can unlock!\n");
1312 peer = get_hop (c, fwd);
1313 GCP_queue_unlock (peer, c);
1314 GCC_check_connections ();
1319 * Cancel all transmissions that belong to a certain connection.
1321 * If the connection is scheduled for destruction and no more messages are left,
1322 * the connection will be destroyed by the continuation call.
1324 * @param c Connection which to cancel. Might be destroyed during this call.
1325 * @param fwd Cancel fwd traffic?
1328 connection_cancel_queues (struct CadetConnection *c,
1331 struct CadetFlowControl *fc;
1332 struct CadetPeer *peer;
1334 GCC_check_connections ();
1335 LOG (GNUNET_ERROR_TYPE_DEBUG,
1336 "Cancel %s queues for connection %s\n",
1337 GC_f2s (fwd), GCC_2s (c));
1344 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1345 if (NULL != fc->poll_task)
1347 GNUNET_SCHEDULER_cancel (fc->poll_task);
1348 fc->poll_task = NULL;
1349 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cancel POLL in ccq for fc %p\n", fc);
1351 peer = get_hop (c, fwd);
1352 GCP_queue_cancel (peer, c);
1353 GCC_check_connections ();
1358 * Function called if a connection has been stalled for a while,
1359 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1361 * @param cls Closure (poll ctx).
1362 * @param tc TaskContext.
1365 connection_poll (void *cls,
1366 const struct GNUNET_SCHEDULER_TaskContext *tc);
1370 * Callback called when a queued POLL message is sent.
1372 * @param cls Closure (FC).
1373 * @param c Connection this message was on.
1374 * @param q Queue handler this call invalidates.
1375 * @param type Type of message sent.
1376 * @param fwd Was this a FWD going message?
1377 * @param size Size of the message.
1380 poll_sent (void *cls,
1381 struct CadetConnection *c,
1382 struct CadetConnectionQueue *q,
1383 uint16_t type, int fwd, size_t size)
1385 struct CadetFlowControl *fc = cls;
1387 if (2 == c->destroy)
1389 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1392 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1394 fc->poll_msg = NULL;
1395 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1396 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1399 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1404 * Function called if a connection has been stalled for a while,
1405 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1407 * @param cls Closure (poll ctx).
1408 * @param tc TaskContext.
1411 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1413 struct CadetFlowControl *fc = cls;
1414 struct GNUNET_CADET_Poll msg;
1415 struct CadetConnection *c;
1418 fc->poll_task = NULL;
1419 GCC_check_connections ();
1420 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1426 fwd = fc == &c->fwd_fc;
1427 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1428 GCC_2s (c), GC_f2s (fwd));
1430 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1431 msg.header.size = htons (sizeof (msg));
1432 msg.pid = htonl (fc->last_pid_sent);
1433 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1435 GCC_send_prebuilt_message (&msg.header, 0, fc->last_pid_sent, c,
1436 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1437 GNUNET_assert (NULL != fc->poll_msg);
1438 GCC_check_connections ();
1443 * Resend all queued messages for a connection on other connections of the
1444 * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
1446 * @param c Connection whose messages to resend.
1447 * @param fwd Resend fwd messages?
1450 resend_messages_and_destroy (struct CadetConnection *c, int fwd)
1452 struct GNUNET_MessageHeader *out_msg;
1453 struct CadetTunnel *t = c->t;
1454 struct CadetPeer *neighbor;
1455 unsigned int pending;
1458 GCC_check_connections ();
1459 c->state = CADET_CONNECTION_DESTROYED;
1460 c->destroy = GNUNET_YES;
1462 destroyed = GNUNET_NO;
1463 neighbor = get_hop (c, fwd);
1464 pending = c->pending_messages;
1466 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1469 GCT_resend_message (out_msg, t);
1470 GNUNET_free (out_msg);
1473 /* All pending messages should have been popped,
1474 * and the connection destroyed by the continuation.
1476 if (GNUNET_YES != destroyed)
1481 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1482 if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1486 GCC_check_connections ();
1491 * Generic connection timeout implementation.
1493 * Timeout function due to lack of keepalive/traffic from an endpoint.
1494 * Destroys connection if called.
1496 * @param c Connection to destroy.
1497 * @param fwd Was the timeout from the origin? (FWD timeout)
1500 connection_timeout (struct CadetConnection *c, int fwd)
1502 struct CadetFlowControl *reverse_fc;
1504 GCC_check_connections ();
1505 reverse_fc = fwd ? &c->bck_fc : &c->fwd_fc;
1507 LOG (GNUNET_ERROR_TYPE_INFO,
1508 "Connection %s %s timed out. Destroying.\n",
1511 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1513 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1519 /* If dest, salvage queued traffic. */
1520 if (GCC_is_origin (c, !fwd))
1522 const struct GNUNET_PeerIdentity *next_hop;
1524 next_hop = GCP_get_id (fwd ? get_prev_hop (c) : get_next_hop (c));
1525 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1526 if (0 < reverse_fc->queue_n)
1527 resend_messages_and_destroy (c, !fwd);
1528 GCC_check_connections ();
1533 GCC_check_connections ();
1538 * Timeout function due to lack of keepalive/traffic from the owner.
1539 * Destroys connection if called.
1541 * @param cls Closure (connection to destroy).
1542 * @param tc TaskContext.
1545 connection_fwd_timeout (void *cls,
1546 const struct GNUNET_SCHEDULER_TaskContext *tc)
1548 struct CadetConnection *c = cls;
1550 c->fwd_maintenance_task = NULL;
1551 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1553 GCC_check_connections ();
1554 connection_timeout (c, GNUNET_YES);
1555 GCC_check_connections ();
1560 * Timeout function due to lack of keepalive/traffic from the destination.
1561 * Destroys connection if called.
1563 * @param cls Closure (connection to destroy).
1564 * @param tc TaskContext
1567 connection_bck_timeout (void *cls,
1568 const struct GNUNET_SCHEDULER_TaskContext *tc)
1570 struct CadetConnection *c = cls;
1572 c->bck_maintenance_task = NULL;
1573 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1575 GCC_check_connections ();
1576 connection_timeout (c, GNUNET_NO);
1577 GCC_check_connections ();
1582 * Resets the connection timeout task, some other message has done the
1584 * - For the first peer on the direction this means to send
1585 * a keepalive or a path confirmation message (either create or ACK).
1586 * - For all other peers, this means to destroy the connection,
1587 * due to lack of activity.
1588 * Starts the timeout if no timeout was running (connection just created).
1590 * @param c Connection whose timeout to reset.
1591 * @param fwd Is this forward?
1593 * TODO use heap to improve efficiency of scheduler.
1596 connection_reset_timeout (struct CadetConnection *c, int fwd)
1598 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1599 if (GCC_is_origin (c, fwd)) /* Startpoint */
1601 schedule_next_keepalive (c, fwd);
1603 else /* Relay, endpoint. */
1605 struct GNUNET_TIME_Relative delay;
1606 struct GNUNET_SCHEDULER_Task * *ti;
1607 GNUNET_SCHEDULER_TaskCallback f;
1609 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1612 GNUNET_SCHEDULER_cancel (*ti);
1613 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1614 LOG (GNUNET_ERROR_TYPE_DEBUG, " timing out in %s\n",
1615 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1616 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1617 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1623 * Add the connection to the list of both neighbors.
1625 * @param c Connection.
1627 * @return #GNUNET_OK if everything went fine
1628 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1631 register_neighbors (struct CadetConnection *c)
1633 c->next_peer = get_next_hop (c);
1634 c->prev_peer = get_prev_hop (c);
1635 GNUNET_assert (c->next_peer != c->prev_peer);
1636 LOG (GNUNET_ERROR_TYPE_DEBUG,
1637 "register neighbors for connection %s\n",
1639 path_debug (c->path);
1640 LOG (GNUNET_ERROR_TYPE_DEBUG,
1641 "own pos %u\n", c->own_pos);
1642 LOG (GNUNET_ERROR_TYPE_DEBUG,
1643 "putting connection %s to next peer %p\n",
1646 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1648 GCP_2s (c->next_peer));
1649 LOG (GNUNET_ERROR_TYPE_DEBUG,
1650 "putting connection %s to prev peer %p\n",
1653 LOG (GNUNET_ERROR_TYPE_DEBUG,
1654 "prev peer %p %s\n",
1656 GCP_2s (c->prev_peer));
1658 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1659 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1661 if (GCC_is_origin (c, GNUNET_YES))
1662 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1663 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1665 LOG (GNUNET_ERROR_TYPE_DEBUG,
1666 " register neighbors failed\n");
1667 LOG (GNUNET_ERROR_TYPE_DEBUG,
1668 " prev: %s, neighbor?: %d\n",
1669 GCP_2s (c->prev_peer),
1670 GCP_is_neighbor (c->prev_peer));
1671 LOG (GNUNET_ERROR_TYPE_DEBUG,
1672 " next: %s, neighbor?: %d\n",
1673 GCP_2s (c->next_peer),
1674 GCP_is_neighbor (c->next_peer));
1675 return GNUNET_SYSERR;
1677 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1678 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1685 * Remove the connection from the list of both neighbors.
1687 * @param c Connection.
1690 unregister_neighbors (struct CadetConnection *c)
1692 /* Either already unregistered or never got registered, it's ok either way. */
1693 if (NULL == c->path)
1695 if (NULL != c->next_peer)
1696 GCP_remove_connection (c->next_peer, c);
1697 if (NULL != c->prev_peer)
1698 GCP_remove_connection (c->prev_peer, c);
1703 * Bind the connection to the peer and the tunnel to that peer.
1705 * If the peer has no tunnel, create one. Update tunnel and connection
1706 * data structres to reflect new status.
1708 * @param c Connection.
1712 add_to_peer (struct CadetConnection *c,
1713 struct CadetPeer *peer)
1715 GCP_add_tunnel (peer);
1716 c->t = GCP_get_tunnel (peer);
1717 GCT_add_connection (c->t, c);
1722 * Iterator to compare each connection's path with the path of a new connection.
1724 * If the connection conincides, the c member of path is set to the connection
1725 * and the destroy flag of the connection is set.
1727 * @param cls Closure (new path).
1728 * @param c Connection in the tunnel to check.
1731 check_path (void *cls, struct CadetConnection *c)
1733 struct CadetConnection *new_conn = cls;
1734 struct CadetPeerPath *path = new_conn->path;
1736 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1737 GCC_2s (c), c, c->path->length);
1740 && c->destroy == GNUNET_NO
1741 && c->state != CADET_CONNECTION_BROKEN
1742 && c->state != CADET_CONNECTION_DESTROYED
1743 && path_equivalent (path, c->path))
1745 new_conn->destroy = GNUNET_YES;
1746 new_conn->path->c = c;
1747 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1753 * Finds out if this path is already being used by and existing connection.
1755 * Checks the tunnel towards the first peer in the path to see if it contains
1756 * any connection with the same path.
1758 * If the existing connection is ready, it is kept.
1759 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1760 * the peer will eventually reject our attempt).
1762 * @param path Path to check.
1763 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1764 * #GNUNET_NO otherwise.
1767 does_connection_exist (struct CadetConnection *conn)
1769 struct CadetPeer *p;
1770 struct CadetTunnel *t;
1771 struct CadetConnection *c;
1773 p = GCP_get_short (conn->path->peers[0]);
1774 t = GCP_get_tunnel (p);
1778 LOG (GNUNET_ERROR_TYPE_DEBUG,
1779 "Checking for duplicates\n");
1781 GCT_iterate_connections (t, &check_path, conn);
1783 if (GNUNET_YES == conn->destroy)
1786 conn->destroy = GNUNET_NO;
1787 conn->path->c = conn;
1788 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1789 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1790 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1791 if (CADET_CONNECTION_READY == c->state)
1793 /* The other peer confirmed a live connection with this path,
1794 * why is it trying to duplicate it. */
1798 if (GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GCP_get_id (p)) > 0)
1800 struct CadetPeer *neighbor;
1802 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate allowed (killing old)\n");
1803 if (GCC_is_origin (c, GNUNET_YES))
1804 neighbor = get_next_hop (c);
1806 neighbor = get_prev_hop (c);
1807 send_broken_unknown (&c->id, &my_full_id, NULL,
1808 GCP_get_id (neighbor));
1817 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1824 * Log receipt of message on stderr (INFO level).
1826 * @param message Message received.
1827 * @param peer Peer who sent the message.
1828 * @param hash Connection ID.
1831 log_message (const struct GNUNET_MessageHeader *message,
1832 const struct GNUNET_PeerIdentity *peer,
1833 const struct GNUNET_CADET_Hash *hash)
1837 size = ntohs (message->size);
1838 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1839 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1840 LOG (GNUNET_ERROR_TYPE_INFO, "<-- %s on connection %s from %s, %6u bytes\n",
1841 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (hash)),
1842 GNUNET_i2s (peer), (unsigned int) size);
1845 /******************************************************************************/
1846 /******************************** API ***********************************/
1847 /******************************************************************************/
1850 * Core handler for connection creation.
1852 * @param cls Closure (unused).
1853 * @param peer Sender (neighbor).
1854 * @param message Message.
1855 * @return #GNUNET_OK to keep the connection open,
1856 * #GNUNET_SYSERR to close it (signal serious error)
1859 GCC_handle_create (void *cls,
1860 const struct GNUNET_PeerIdentity *peer,
1861 const struct GNUNET_MessageHeader *message)
1863 struct GNUNET_CADET_ConnectionCreate *msg;
1864 struct GNUNET_PeerIdentity *id;
1865 struct GNUNET_CADET_Hash *cid;
1866 struct CadetPeerPath *path;
1867 struct CadetPeer *dest_peer;
1868 struct CadetPeer *orig_peer;
1869 struct CadetConnection *c;
1870 unsigned int own_pos;
1873 GCC_check_connections ();
1875 size = ntohs (message->size);
1876 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1878 GNUNET_break_op (0);
1882 /* Calculate hops */
1883 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1884 if (size % sizeof (struct GNUNET_PeerIdentity))
1886 GNUNET_break_op (0);
1889 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1891 GNUNET_break_op (0);
1894 size /= sizeof (struct GNUNET_PeerIdentity);
1897 GNUNET_break_op (0);
1900 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1902 /* Get parameters */
1903 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
1905 log_message (message, peer, cid);
1906 id = (struct GNUNET_PeerIdentity *) &msg[1];
1907 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1909 /* Create connection */
1910 c = connection_get (cid);
1913 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1914 size, myid, &own_pos);
1917 /* Path was malformed, probably our own ID was not in it. */
1918 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
1919 GNUNET_break_op (0);
1925 /* We received this request from a neighbor, we cannot be origin */
1926 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
1927 GNUNET_break_op (0);
1928 path_destroy (path);
1932 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1933 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1934 c = GCC_new (cid, NULL, path, own_pos);
1937 if (path->length - 1 == own_pos)
1939 /* If we are destination, why did the creation fail? */
1941 path_destroy (path);
1942 GCC_check_connections ();
1945 send_broken_unknown (cid, &my_full_id,
1946 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1948 path_destroy (path);
1949 GCC_check_connections ();
1952 GCP_add_path_to_all (path, GNUNET_NO);
1953 connection_reset_timeout (c, GNUNET_YES);
1957 path = path_duplicate (c->path);
1959 if (CADET_CONNECTION_NEW == c->state)
1960 connection_change_state (c, CADET_CONNECTION_SENT);
1962 /* Remember peers */
1963 dest_peer = GCP_get (&id[size - 1]);
1964 orig_peer = GCP_get (&id[0]);
1966 /* Is it a connection to us? */
1967 if (c->own_pos == path->length - 1)
1969 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1970 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1972 add_to_peer (c, orig_peer);
1973 if (GNUNET_YES == does_connection_exist (c))
1975 // FIXME Peer created a connection equal to one we think exists
1976 // and is fine. What should we do?
1977 // Use explicit duplicate?
1978 // Accept new conn and destroy the old? (interruption in higher level)
1979 // Keep both and postpone disambiguation?
1980 // Keep the one created by peer with higher ID?
1981 // For now: reject new connection until current confirmed dead
1982 GNUNET_break_op (0);
1983 GCC_debug (c, GNUNET_ERROR_TYPE_WARNING);
1984 path_destroy (path);
1986 send_broken_unknown (cid, &my_full_id, NULL, peer);
1987 GCC_check_connections ();
1991 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
1992 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
1994 send_connection_ack (c, GNUNET_NO);
1995 if (CADET_CONNECTION_SENT == c->state)
1996 connection_change_state (c, CADET_CONNECTION_ACK);
2000 /* It's for somebody else! Retransmit. */
2001 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2002 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2003 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2004 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
2005 GNUNET_YES, GNUNET_YES,
2008 path_destroy (path);
2009 GCC_check_connections ();
2015 * Core handler for path confirmations.
2017 * @param cls closure
2018 * @param message message
2019 * @param peer peer identity this notification is about
2020 * @return #GNUNET_OK to keep the connection open,
2021 * #GNUNET_SYSERR to close it (signal serious error)
2024 GCC_handle_confirm (void *cls,
2025 const struct GNUNET_PeerIdentity *peer,
2026 const struct GNUNET_MessageHeader *message)
2028 struct GNUNET_CADET_ConnectionACK *msg;
2029 struct CadetConnection *c;
2030 struct CadetPeerPath *p;
2031 struct CadetPeer *pi;
2032 enum CadetConnectionState oldstate;
2035 GCC_check_connections ();
2036 msg = (struct GNUNET_CADET_ConnectionACK *) message;
2037 log_message (message, peer, &msg->cid);
2038 c = connection_get (&msg->cid);
2041 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2043 LOG (GNUNET_ERROR_TYPE_DEBUG,
2044 " don't know the connection!\n");
2045 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2046 GCC_check_connections ();
2050 oldstate = c->state;
2051 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
2052 pi = GCP_get (peer);
2053 if (get_next_hop (c) == pi)
2055 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2057 if (CADET_CONNECTION_SENT == oldstate)
2058 connection_change_state (c, CADET_CONNECTION_ACK);
2060 else if (get_prev_hop (c) == pi)
2062 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2064 connection_change_state (c, CADET_CONNECTION_READY);
2068 GNUNET_break_op (0);
2072 connection_reset_timeout (c, fwd);
2074 /* Add path to peers? */
2078 GCP_add_path_to_all (p, GNUNET_YES);
2085 /* Message for us as creator? */
2086 if (GCC_is_origin (c, GNUNET_YES))
2088 if (GNUNET_NO != fwd)
2090 GNUNET_break_op (0);
2093 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2095 /* If just created, cancel the short timeout and start a long one */
2096 if (CADET_CONNECTION_SENT == oldstate)
2097 connection_reset_timeout (c, GNUNET_YES);
2099 /* Change connection state */
2100 connection_change_state (c, CADET_CONNECTION_READY);
2101 send_connection_ack (c, GNUNET_YES);
2103 /* Change tunnel state, trigger KX */
2104 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2105 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2106 GCC_check_connections ();
2110 /* Message for us as destination? */
2111 if (GCC_is_terminal (c, GNUNET_YES))
2113 if (GNUNET_YES != fwd)
2115 GNUNET_break_op (0);
2118 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2120 /* If just created, cancel the short timeout and start a long one */
2121 if (CADET_CONNECTION_ACK == oldstate)
2122 connection_reset_timeout (c, GNUNET_NO);
2124 /* Change tunnel state */
2125 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2126 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2127 GCC_check_connections ();
2131 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2132 GNUNET_assert (NULL ==
2133 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2134 GNUNET_YES, NULL, NULL));
2135 GCC_check_connections ();
2141 * Core handler for notifications of broken connections.
2143 * @param cls Closure (unused).
2144 * @param id Peer identity of sending neighbor.
2145 * @param message Message.
2146 * @return #GNUNET_OK to keep the connection open,
2147 * #GNUNET_SYSERR to close it (signal serious error)
2150 GCC_handle_broken (void* cls,
2151 const struct GNUNET_PeerIdentity* id,
2152 const struct GNUNET_MessageHeader* message)
2154 struct GNUNET_CADET_ConnectionBroken *msg;
2155 struct CadetConnection *c;
2156 struct CadetTunnel *t;
2160 GCC_check_connections ();
2161 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
2162 log_message (message, id, &msg->cid);
2163 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2164 GNUNET_i2s (&msg->peer1));
2165 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2166 GNUNET_i2s (&msg->peer2));
2167 c = connection_get (&msg->cid);
2170 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2171 GCC_check_connections ();
2177 fwd = is_fwd (c, id);
2178 c->destroy = GNUNET_YES;
2179 if (GCC_is_terminal (c, fwd))
2181 struct CadetPeer *endpoint;
2185 /* A terminal connection should not have 't' set to NULL. */
2187 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2190 endpoint = GCP_get_short (c->path->peers[c->path->length - 1]);
2191 if (2 < c->path->length)
2192 path_invalidate (c->path);
2193 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2195 c->state = CADET_CONNECTION_BROKEN;
2196 GCT_remove_connection (t, c);
2199 pending = c->pending_messages;
2201 resend_messages_and_destroy (c, !fwd);
2207 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2208 GNUNET_YES, NULL, NULL));
2209 connection_cancel_queues (c, !fwd);
2211 GCC_check_connections ();
2217 * Core handler for tunnel destruction
2219 * @param cls Closure (unused).
2220 * @param peer Peer identity of sending neighbor.
2221 * @param message Message.
2222 * @return #GNUNET_OK to keep the connection open,
2223 * #GNUNET_SYSERR to close it (signal serious error)
2226 GCC_handle_destroy (void *cls,
2227 const struct GNUNET_PeerIdentity *peer,
2228 const struct GNUNET_MessageHeader *message)
2230 const struct GNUNET_CADET_ConnectionDestroy *msg;
2231 struct CadetConnection *c;
2234 GCC_check_connections ();
2235 msg = (const struct GNUNET_CADET_ConnectionDestroy *) message;
2236 log_message (message, peer, &msg->cid);
2237 c = connection_get (&msg->cid);
2240 /* Probably already got the message from another path,
2241 * destroyed the tunnel and retransmitted to children.
2244 GNUNET_STATISTICS_update (stats,
2245 "# control on unknown connection",
2247 LOG (GNUNET_ERROR_TYPE_DEBUG,
2248 " connection unknown: already destroyed?\n");
2249 GCC_check_connections ();
2252 fwd = is_fwd (c, peer);
2253 if (GNUNET_SYSERR == fwd)
2255 GNUNET_break_op (0); /* FIXME */
2258 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2260 GNUNET_assert (NULL ==
2261 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2262 GNUNET_YES, NULL, NULL));
2264 else if (0 == c->pending_messages)
2266 LOG (GNUNET_ERROR_TYPE_DEBUG,
2267 " directly destroying connection!\n");
2269 GCC_check_connections ();
2272 c->destroy = GNUNET_YES;
2273 c->state = CADET_CONNECTION_DESTROYED;
2276 GCT_remove_connection (c->t, c);
2279 GCC_check_connections ();
2285 * Check the message against internal state and test if it goes FWD or BCK.
2287 * Updates the PID, state and timeout values for the connection.
2289 * @param message Message to check. It must belong to an existing connection.
2290 * @param minimum_size The message cannot be smaller than this value.
2291 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2292 * @param c Connection this message should belong. If NULL, check fails.
2293 * @param neighbor Neighbor that sent the message.
2296 check_message (const struct GNUNET_MessageHeader *message,
2297 size_t minimum_size,
2298 const struct GNUNET_CADET_Hash* cid,
2299 struct CadetConnection *c,
2300 const struct GNUNET_PeerIdentity *neighbor,
2303 GNUNET_PEER_Id neighbor_id;
2304 struct CadetFlowControl *fc;
2305 struct CadetPeer *hop;
2310 if (ntohs (message->size) < minimum_size)
2312 GNUNET_break_op (0);
2313 LOG (GNUNET_ERROR_TYPE_WARNING, "Size %u < %u\n",
2314 ntohs (message->size), minimum_size);
2315 return GNUNET_SYSERR;
2318 /* Check connection */
2321 GNUNET_STATISTICS_update (stats,
2322 "# unknown connection",
2324 LOG (GNUNET_ERROR_TYPE_DEBUG,
2325 "%s on unknown connection %s\n",
2326 GC_m2s (ntohs (message->type)),
2327 GNUNET_h2s (GC_h2hc (cid)));
2328 send_broken_unknown (cid,
2332 return GNUNET_SYSERR;
2335 /* Check if origin is as expected */
2336 neighbor_id = GNUNET_PEER_search (neighbor);
2337 hop = get_prev_hop (c);
2338 if (neighbor_id == GCP_get_short_id (hop))
2344 hop = get_next_hop (c);
2345 GNUNET_break (hop == c->next_peer);
2346 if (neighbor_id == GCP_get_short_id (hop))
2352 /* Unexpected peer sending traffic on a connection. */
2353 GNUNET_break_op (0);
2354 return GNUNET_SYSERR;
2358 /* Check PID for payload messages */
2359 type = ntohs (message->type);
2360 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
2361 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
2363 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2364 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2365 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2366 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2368 GNUNET_break_op (0);
2369 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2370 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2371 pid, fc->last_pid_recv, fc->last_ack_sent);
2372 return GNUNET_SYSERR;
2374 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2378 delta = pid - fc->last_pid_recv;
2379 fc->last_pid_recv = pid;
2380 fc->recv_bitmap <<= delta;
2381 fc->recv_bitmap |= 1;
2385 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2386 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2388 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2389 pid, fc->last_pid_recv - 31);
2390 return GNUNET_SYSERR;
2392 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2396 /* Count as connection confirmation. */
2397 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2399 connection_change_state (c, CADET_CONNECTION_READY);
2402 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2403 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2406 connection_reset_timeout (c, fwd);
2413 * Generic handler for cadet network encrypted traffic.
2415 * @param peer Peer identity this notification is about.
2416 * @param msg Encrypted message.
2417 * @return #GNUNET_OK to keep the connection open,
2418 * #GNUNET_SYSERR to close it (signal serious error)
2421 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
2422 const struct GNUNET_MessageHeader *message)
2424 const struct GNUNET_CADET_Encrypted *otr_msg;
2425 const struct GNUNET_CADET_AX *ax_msg;
2426 const struct GNUNET_CADET_Hash* cid;
2427 struct CadetConnection *c;
2428 size_t minumum_size;
2434 GCC_check_connections ();
2435 if (GNUNET_MESSAGE_TYPE_CADET_AX == ntohs (message->type))
2437 overhead = sizeof (struct GNUNET_CADET_AX);
2438 ax_msg = (const struct GNUNET_CADET_AX *) message;
2440 pid = ntohl (ax_msg->pid);
2445 overhead = sizeof (struct GNUNET_CADET_Encrypted);
2446 otr_msg = (const struct GNUNET_CADET_Encrypted *) message;
2447 cid = &otr_msg->cid;
2448 pid = ntohl (otr_msg->pid);
2451 log_message (message, peer, cid);
2453 minumum_size = sizeof (struct GNUNET_MessageHeader) + overhead;
2454 c = connection_get (cid);
2455 fwd = check_message (message,
2462 /* If something went wrong, discard message. */
2463 if (GNUNET_SYSERR == fwd)
2465 GCC_check_connections ();
2469 /* Is this message for us? */
2470 if (GCC_is_terminal (c, fwd))
2472 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2476 GNUNET_break (GNUNET_NO != c->destroy);
2479 GCT_handle_encrypted (c->t, message);
2480 GCC_send_ack (c, fwd, GNUNET_NO);
2481 GCC_check_connections ();
2485 /* Message not for us: forward to next hop */
2486 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2487 if (NULL != otr_msg) /* only otr has ttl */
2489 ttl = ntohl (otr_msg->ttl);
2490 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2493 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2494 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2495 GCC_send_ack (c, fwd, GNUNET_NO);
2496 GCC_check_connections ();
2501 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2502 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2503 GNUNET_NO, NULL, NULL));
2504 GCC_check_connections ();
2510 * Generic handler for cadet network encrypted traffic.
2512 * @param peer Peer identity this notification is about.
2513 * @param msg Encrypted message.
2514 * @return #GNUNET_OK to keep the connection open,
2515 * #GNUNET_SYSERR to close it (signal serious error)
2518 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2519 const struct GNUNET_CADET_KX *msg)
2521 const struct GNUNET_CADET_Hash* cid;
2522 struct CadetConnection *c;
2523 size_t expected_size;
2526 GCC_check_connections ();
2528 log_message (&msg->header, peer, cid);
2530 expected_size = sizeof (struct GNUNET_CADET_KX)
2531 + sizeof (struct GNUNET_MessageHeader);
2532 c = connection_get (cid);
2533 fwd = check_message (&msg->header,
2540 /* If something went wrong, discard message. */
2541 if (GNUNET_SYSERR == fwd)
2544 /* Is this message for us? */
2545 if (GCC_is_terminal (c, fwd))
2547 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2548 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
2554 GCT_handle_kx (c->t, &msg[1].header);
2555 GCC_check_connections ();
2559 /* Message not for us: forward to next hop */
2560 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2561 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2562 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2563 GNUNET_NO, NULL, NULL));
2564 GCC_check_connections ();
2570 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2572 * @param cls Closure (unused).
2573 * @param message Message received.
2574 * @param peer Peer who sent the message.
2575 * @return #GNUNET_OK to keep the connection open,
2576 * #GNUNET_SYSERR to close it (signal serious error)
2579 GCC_handle_kx (void *cls,
2580 const struct GNUNET_PeerIdentity *peer,
2581 const struct GNUNET_MessageHeader *message)
2583 GCC_check_connections ();
2584 return handle_cadet_kx (peer, (struct GNUNET_CADET_KX *) message);
2589 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2591 * @param cls Closure (unused).
2592 * @param message Message received.
2593 * @param peer Peer who sent the message.
2594 * @return #GNUNET_OK to keep the connection open,
2595 * #GNUNET_SYSERR to close it (signal serious error)
2598 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2599 const struct GNUNET_MessageHeader *message)
2601 GCC_check_connections ();
2602 return handle_cadet_encrypted (peer, message);
2607 * Core handler for cadet network traffic point-to-point acks.
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_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2617 const struct GNUNET_MessageHeader *message)
2619 struct GNUNET_CADET_ACK *msg;
2620 struct CadetConnection *c;
2621 struct CadetFlowControl *fc;
2626 GCC_check_connections ();
2627 msg = (struct GNUNET_CADET_ACK *) message;
2628 log_message (message, peer, &msg->cid);
2629 c = connection_get (&msg->cid);
2632 GNUNET_STATISTICS_update (stats,
2633 "# ack on unknown connection",
2636 send_broken_unknown (&msg->cid,
2640 GCC_check_connections ();
2644 /* Is this a forward or backward ACK? */
2645 id = GNUNET_PEER_search (peer);
2646 if (GCP_get_short_id (get_next_hop (c)) == id)
2651 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2658 GNUNET_break_op (0);
2662 ack = ntohl (msg->ack);
2663 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2664 GC_f2s (fwd), ack, fc->last_ack_recv);
2665 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2666 fc->last_ack_recv = ack;
2668 /* Cancel polling if the ACK is big enough. */
2669 if (NULL != fc->poll_task &&
2670 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2672 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2673 GNUNET_SCHEDULER_cancel (fc->poll_task);
2674 fc->poll_task = NULL;
2675 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2678 connection_unlock_queue (c, fwd);
2679 GCC_check_connections ();
2685 * Core handler for cadet network traffic point-to-point ack polls.
2687 * @param cls closure
2688 * @param message message
2689 * @param peer peer identity this notification is about
2690 * @return #GNUNET_OK to keep the connection open,
2691 * #GNUNET_SYSERR to close it (signal serious error)
2694 GCC_handle_poll (void *cls,
2695 const struct GNUNET_PeerIdentity *peer,
2696 const struct GNUNET_MessageHeader *message)
2698 struct GNUNET_CADET_Poll *msg;
2699 struct CadetConnection *c;
2700 struct CadetFlowControl *fc;
2705 GCC_check_connections ();
2706 msg = (struct GNUNET_CADET_Poll *) message;
2707 log_message (message, peer, &msg->cid);
2708 c = connection_get (&msg->cid);
2711 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2713 LOG (GNUNET_ERROR_TYPE_DEBUG,
2714 "POLL message on unknown connection %s!\n",
2715 GNUNET_h2s (GC_h2hc (&msg->cid)));
2716 send_broken_unknown (&msg->cid,
2720 GCC_check_connections ();
2724 /* Is this a forward or backward ACK?
2725 * Note: a poll should never be needed in a loopback case,
2726 * since there is no possiblility of packet loss there, so
2727 * this way of discerining FWD/BCK should not be a problem.
2729 id = GNUNET_PEER_search (peer);
2730 if (GCP_get_short_id (get_next_hop (c)) == id)
2732 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2735 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2737 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2742 GNUNET_break_op (0);
2746 pid = ntohl (msg->pid);
2747 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2748 fc->last_pid_recv = pid;
2749 fwd = fc == &c->bck_fc;
2750 GCC_send_ack (c, fwd, GNUNET_YES);
2751 GCC_check_connections ();
2758 * Send an ACK on the appropriate connection/channel, depending on
2759 * the direction and the position of the peer.
2761 * @param c Which connection to send the hop-by-hop ACK.
2762 * @param fwd Is this a fwd ACK? (will go dest->root).
2763 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2766 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2768 unsigned int buffer;
2770 GCC_check_connections ();
2771 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
2772 GC_f2s (fwd), GCC_2s (c));
2780 if (GNUNET_NO != c->destroy)
2782 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2783 GCC_check_connections ();
2787 /* Get available buffer space */
2788 if (GCC_is_terminal (c, fwd))
2790 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2791 buffer = GCT_get_channels_buffer (c->t);
2795 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2796 buffer = GCC_get_buffer (c, fwd);
2798 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2799 if (0 == buffer && GNUNET_NO == force)
2801 GCC_check_connections ();
2805 /* Send available buffer space */
2806 if (GCC_is_origin (c, fwd))
2808 GNUNET_assert (NULL != c->t);
2809 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2810 GCT_unchoke_channels (c->t);
2814 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2815 send_ack (c, buffer, fwd, force);
2817 GCC_check_connections ();
2822 * Initialize the connections subsystem
2824 * @param c Configuration handle.
2827 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2829 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2831 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2834 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2835 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2836 GNUNET_SCHEDULER_shutdown ();
2841 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2844 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2845 "CADET", "MAX_CONNECTIONS", "MISSING");
2846 GNUNET_SCHEDULER_shutdown ();
2851 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2852 &refresh_connection_time))
2854 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2855 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2856 GNUNET_SCHEDULER_shutdown ();
2859 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2860 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2865 * Destroy each connection on shutdown.
2867 * @param cls Closure (unused).
2868 * @param key Current key code (CID, unused).
2869 * @param value Value in the hash map (`struct CadetConnection`)
2871 * @return #GNUNET_YES, because we should continue to iterate
2874 shutdown_iterator (void *cls,
2875 const struct GNUNET_HashCode *key,
2878 struct CadetConnection *c = value;
2880 c->state = CADET_CONNECTION_DESTROYED;
2887 * Shut down the connections subsystem.
2892 GCC_check_connections ();
2893 GNUNET_CONTAINER_multihashmap_iterate (connections,
2896 GNUNET_CONTAINER_multihashmap_destroy (connections);
2902 * Create a connection.
2904 * @param cid Connection ID (either created locally or imposed remotely).
2905 * @param t Tunnel this connection belongs to (or NULL);
2906 * @param path Path this connection has to use (copy is made).
2907 * @param own_pos Own position in the @c path path.
2909 * @return Newly created connection, NULL in case of error (own id not in path).
2911 struct CadetConnection *
2912 GCC_new (const struct GNUNET_CADET_Hash *cid,
2913 struct CadetTunnel *t,
2914 struct CadetPeerPath *path,
2915 unsigned int own_pos)
2917 struct CadetConnection *c;
2918 struct CadetPeerPath *p;
2920 GCC_check_connections ();
2921 p = path_duplicate (path);
2922 c = GNUNET_new (struct CadetConnection);
2924 GNUNET_assert (GNUNET_OK ==
2925 GNUNET_CONTAINER_multihashmap_put (connections,
2927 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2928 fc_init (&c->fwd_fc);
2929 fc_init (&c->bck_fc);
2934 GNUNET_assert (own_pos <= p->length - 1);
2935 c->own_pos = own_pos;
2938 GNUNET_assert (NULL != p);
2939 if (GNUNET_OK != register_neighbors (c))
2943 /* We were the origin of this request, this means we have invalid
2944 * info about the paths to reach the destination. We must invalidate
2945 * the *original* path to avoid trying it again in the next minute.
2947 path_invalidate (path);
2950 path_destroy (c->path);
2955 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2956 GCC_check_connections ();
2962 GCC_destroy (struct CadetConnection *c)
2964 GCC_check_connections ();
2971 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2972 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2975 LOG (GNUNET_ERROR_TYPE_DEBUG,
2976 "destroying connection %s\n",
2978 LOG (GNUNET_ERROR_TYPE_DEBUG,
2979 " fc's f: %p, b: %p\n",
2980 &c->fwd_fc, &c->bck_fc);
2981 LOG (GNUNET_ERROR_TYPE_DEBUG,
2982 " fc tasks f: %u, b: %u\n",
2983 c->fwd_fc.poll_task,
2984 c->bck_fc.poll_task);
2986 /* Cancel all traffic */
2987 if (NULL != c->path)
2989 connection_cancel_queues (c, GNUNET_YES);
2990 connection_cancel_queues (c, GNUNET_NO);
2992 unregister_neighbors (c);
2993 path_destroy (c->path);
2996 /* Cancel maintainance task (keepalive/timeout) */
2997 if (NULL != c->fwd_fc.poll_msg)
2999 GCC_cancel (c->fwd_fc.poll_msg);
3000 LOG (GNUNET_ERROR_TYPE_DEBUG,
3001 " POLL msg FWD canceled\n");
3003 if (NULL != c->bck_fc.poll_msg)
3005 GCC_cancel (c->bck_fc.poll_msg);
3006 LOG (GNUNET_ERROR_TYPE_DEBUG,
3007 " POLL msg BCK canceled\n");
3010 /* Delete from tunnel */
3012 GCT_remove_connection (c->t, c);
3014 if (NULL != c->fwd_maintenance_task)
3015 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
3016 if (NULL != c->bck_maintenance_task)
3017 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
3018 if (NULL != c->fwd_fc.poll_task)
3020 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
3021 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task FWD canceled\n");
3023 if (NULL != c->bck_fc.poll_task)
3025 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
3026 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task BCK canceled\n");
3028 if (GNUNET_NO == c->was_removed)
3030 GNUNET_break (GNUNET_YES ==
3031 GNUNET_CONTAINER_multihashmap_remove (connections,
3035 GNUNET_STATISTICS_update (stats,
3040 GCC_check_connections ();
3045 * Get the connection ID.
3047 * @param c Connection to get the ID from.
3049 * @return ID of the connection.
3051 const struct GNUNET_CADET_Hash *
3052 GCC_get_id (const struct CadetConnection *c)
3059 * Get the connection ID.
3061 * @param c Connection to get the ID from.
3063 * @return ID of the connection.
3065 const struct GNUNET_HashCode *
3066 GCC_get_h (const struct CadetConnection *c)
3068 return GC_h2hc (&c->id);
3073 * Get the connection path.
3075 * @param c Connection to get the path from.
3077 * @return path used by the connection.
3079 const struct CadetPeerPath *
3080 GCC_get_path (const struct CadetConnection *c)
3082 if (GNUNET_NO == c->destroy)
3089 * Get the connection state.
3091 * @param c Connection to get the state from.
3093 * @return state of the connection.
3095 enum CadetConnectionState
3096 GCC_get_state (const struct CadetConnection *c)
3102 * Get the connection tunnel.
3104 * @param c Connection to get the tunnel from.
3106 * @return tunnel of the connection.
3108 struct CadetTunnel *
3109 GCC_get_tunnel (const struct CadetConnection *c)
3116 * Get free buffer space in a connection.
3118 * @param c Connection.
3119 * @param fwd Is query about FWD traffic?
3121 * @return Free buffer space [0 - max_msgs_queue/max_connections]
3124 GCC_get_buffer (struct CadetConnection *c, int fwd)
3126 struct CadetFlowControl *fc;
3128 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3130 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
3131 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
3132 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
3134 return (fc->queue_max - fc->queue_n);
3139 * Get how many messages have we allowed to send to us from a direction.
3141 * @param c Connection.
3142 * @param fwd Are we asking about traffic from FWD (BCK messages)?
3144 * @return last_ack_sent - last_pid_recv
3147 GCC_get_allowed (struct CadetConnection *c, int fwd)
3149 struct CadetFlowControl *fc;
3151 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3152 if (CADET_CONNECTION_READY != c->state
3153 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
3157 return (fc->last_ack_sent - fc->last_pid_recv);
3162 * Get messages queued in a connection.
3164 * @param c Connection.
3165 * @param fwd Is query about FWD traffic?
3167 * @return Number of messages queued.
3170 GCC_get_qn (struct CadetConnection *c, int fwd)
3172 struct CadetFlowControl *fc;
3174 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3181 * Get next PID to use.
3183 * @param c Connection.
3184 * @param fwd Is query about FWD traffic?
3186 * @return Last PID used + 1.
3189 GCC_get_pid (struct CadetConnection *c, int fwd)
3191 struct CadetFlowControl *fc;
3193 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3195 return fc->last_pid_sent + 1;
3200 * Allow the connection to advertise a buffer of the given size.
3202 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3203 * allowing up to last_pid_recv + buffer.
3205 * @param c Connection.
3206 * @param buffer How many more messages the connection can accept.
3207 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3210 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3212 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3213 GCC_2s (c), buffer, GC_f2s (fwd));
3214 send_ack (c, buffer, fwd, GNUNET_NO);
3219 * Notify other peers on a connection of a broken link. Mark connections
3220 * to destroy after all traffic has been sent.
3222 * @param c Connection on which there has been a disconnection.
3223 * @param peer Peer that disconnected.
3226 GCC_notify_broken (struct CadetConnection *c,
3227 struct CadetPeer *peer)
3229 struct CadetPeer *hop;
3232 GCC_check_connections ();
3233 LOG (GNUNET_ERROR_TYPE_DEBUG,
3234 "Notify broken on %s due to %s disconnect\n",
3237 hop = get_prev_hop (c);
3240 /* Path was NULL, we should have deleted the connection. */
3244 fwd = (peer == hop);
3245 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3246 (GNUNET_YES == c->destroy) )
3248 /* Local shutdown, or other peer already down (hence 'c->destroy');
3249 so there is no one to notify about this, just clean up. */
3251 GCC_check_connections ();
3254 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3255 /* Connection will have at least one pending message
3256 * (the one we just scheduled), so delay destruction
3257 * and remove from map so we don't use accidentally. */
3258 c->destroy = GNUNET_YES;
3259 c->state = CADET_CONNECTION_DESTROYED;
3260 GNUNET_assert (GNUNET_NO == c->was_removed);
3261 c->was_removed = GNUNET_YES;
3262 GNUNET_break (GNUNET_YES ==
3263 GNUNET_CONTAINER_multihashmap_remove (connections,
3266 /* Cancel queue in the direction that just died. */
3267 connection_cancel_queues (c, ! fwd);
3270 GCP_remove_connection (c->prev_peer, c);
3271 c->prev_peer = NULL;
3275 GCP_remove_connection (c->next_peer, c);
3276 c->next_peer = NULL;
3278 GNUNET_assert (NULL != ( (fwd) ? c->next_peer : c->prev_peer) );
3279 GCC_check_connections ();
3284 * Is this peer the first one on the connection?
3286 * @param c Connection.
3287 * @param fwd Is this about fwd traffic?
3289 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3292 GCC_is_origin (struct CadetConnection *c, int fwd)
3294 if (!fwd && c->path->length - 1 == c->own_pos )
3296 if (fwd && 0 == c->own_pos)
3303 * Is this peer the last one on the connection?
3305 * @param c Connection.
3306 * @param fwd Is this about fwd traffic?
3307 * Note that the ROOT is the terminal for BCK traffic!
3309 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3312 GCC_is_terminal (struct CadetConnection *c, int fwd)
3314 return GCC_is_origin (c, ! fwd);
3319 * See if we are allowed to send by the next hop in the given direction.
3321 * @param c Connection.
3322 * @param fwd Is this about fwd traffic?
3324 * @return #GNUNET_YES in case it's OK to send.
3327 GCC_is_sendable (struct CadetConnection *c, int fwd)
3329 struct CadetFlowControl *fc;
3331 LOG (GNUNET_ERROR_TYPE_DEBUG,
3332 " checking sendability of %s traffic on %s\n",
3333 GC_f2s (fwd), GCC_2s (c));
3339 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3340 LOG (GNUNET_ERROR_TYPE_DEBUG,
3341 " last ack recv: %u, last pid sent: %u\n",
3342 fc->last_ack_recv, fc->last_pid_sent);
3343 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3345 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3348 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3354 * Check if this connection is a direct one (never trim a direct connection).
3356 * @param c Connection.
3358 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3361 GCC_is_direct (struct CadetConnection *c)
3363 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3368 * Sends an already built message on a connection, properly registering
3369 * all used resources.
3371 * @param message Message to send. Function makes a copy of it.
3372 * If message is not hop-by-hop, decrements TTL of copy.
3373 * @param payload_type Type of payload, in case the message is encrypted.
3374 * @param c Connection on which this message is transmitted.
3375 * @param fwd Is this a fwd message?
3376 * @param force Force the connection to accept the message (buffer overfill).
3377 * @param cont Continuation called once message is sent. Can be NULL.
3378 * @param cont_cls Closure for @c cont.
3380 * @return Handle to cancel the message before it's sent.
3381 * NULL on error or if @c cont is NULL.
3382 * Invalid on @c cont call.
3384 struct CadetConnectionQueue *
3385 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3386 uint16_t payload_type, uint32_t payload_id,
3387 struct CadetConnection *c, int fwd, int force,
3388 GCC_sent cont, void *cont_cls)
3390 struct CadetFlowControl *fc;
3391 struct CadetConnectionQueue *q;
3397 GCC_check_connections ();
3398 size = ntohs (message->size);
3399 data = GNUNET_malloc (size);
3400 memcpy (data, message, size);
3401 type = ntohs (message->type);
3402 LOG (GNUNET_ERROR_TYPE_INFO, "--> %s (%s %4u) on connection %s (%u bytes)\n",
3403 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), size);
3405 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3406 droppable = GNUNET_NO == force;
3409 struct GNUNET_CADET_AX *axmsg;
3410 struct GNUNET_CADET_Encrypted *emsg;
3411 struct GNUNET_CADET_KX *kmsg;
3412 struct GNUNET_CADET_ACK *amsg;
3413 struct GNUNET_CADET_Poll *pmsg;
3414 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3415 struct GNUNET_CADET_ConnectionBroken *bmsg;
3418 case GNUNET_MESSAGE_TYPE_CADET_AX:
3419 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3420 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3422 emsg = (struct GNUNET_CADET_Encrypted *) data;
3423 ttl = ntohl (emsg->ttl);
3426 GNUNET_break_op (0);
3431 emsg->ttl = htonl (ttl - 1);
3435 axmsg = (struct GNUNET_CADET_AX *) data;
3438 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3439 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3440 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3441 if (GNUNET_YES == droppable)
3447 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3451 case GNUNET_MESSAGE_TYPE_CADET_KX:
3452 kmsg = (struct GNUNET_CADET_KX *) data;
3456 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3457 amsg = (struct GNUNET_CADET_ACK *) data;
3459 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3460 droppable = GNUNET_NO;
3463 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3464 pmsg = (struct GNUNET_CADET_Poll *) data;
3466 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
3467 droppable = GNUNET_NO;
3470 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3471 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
3475 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3476 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
3480 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3481 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3490 if (fc->queue_n > fc->queue_max && droppable)
3492 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3495 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3496 fc->queue_n, fc->queue_max);
3497 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
3498 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
3503 return NULL; /* Drop this message */
3506 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3507 GCC_2s (c), c->pending_messages);
3508 c->pending_messages++;
3510 q = GNUNET_new (struct CadetConnectionQueue);
3511 q->forced = !droppable;
3512 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3513 size, c, fwd, &conn_message_sent, q);
3516 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3519 GCC_check_connections ();
3523 q->cont_cls = cont_cls;
3524 GCC_check_connections ();
3525 return (NULL == cont) ? NULL : q;
3530 * Cancel a previously sent message while it's in the queue.
3532 * ONLY can be called before the continuation given to the send function
3533 * is called. Once the continuation is called, the message is no longer in the
3536 * @param q Handle to the queue.
3539 GCC_cancel (struct CadetConnectionQueue *q)
3541 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3543 /* queue destroy calls message_sent, which calls q->cont and frees q */
3544 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3545 GCC_check_connections ();
3550 * Sends a CREATE CONNECTION message for a path to a peer.
3551 * Changes the connection and tunnel states if necessary.
3553 * @param connection Connection to create.
3556 GCC_send_create (struct CadetConnection *connection)
3558 enum CadetTunnelCState state;
3561 GCC_check_connections ();
3562 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3563 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3565 LOG (GNUNET_ERROR_TYPE_INFO, "---> %s on connection %s (%u bytes)\n",
3566 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE),
3567 GCC_2s (connection), size);
3568 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3569 connection, connection->pending_messages);
3570 connection->pending_messages++;
3572 connection->maintenance_q =
3573 GCP_queue_add (get_next_hop (connection), NULL,
3574 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 0,
3575 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3577 state = GCT_get_cstate (connection->t);
3578 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3579 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3580 if (CADET_CONNECTION_NEW == connection->state)
3581 connection_change_state (connection, CADET_CONNECTION_SENT);
3582 GCC_check_connections ();
3587 * Send a message to all peers in this connection that the connection
3588 * is no longer valid.
3590 * If some peer should not receive the message, it should be zero'ed out
3591 * before calling this function.
3593 * @param c The connection whose peers to notify.
3596 GCC_send_destroy (struct CadetConnection *c)
3598 struct GNUNET_CADET_ConnectionDestroy msg;
3600 if (GNUNET_YES == c->destroy)
3602 GCC_check_connections ();
3603 msg.header.size = htons (sizeof (msg));
3604 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3606 LOG (GNUNET_ERROR_TYPE_DEBUG,
3607 " sending connection destroy for connection %s\n",
3610 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3611 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3612 GNUNET_YES, GNUNET_YES,
3614 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3615 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3616 GNUNET_NO, GNUNET_YES,
3618 c->destroy = GNUNET_YES;
3619 c->state = CADET_CONNECTION_DESTROYED;
3620 GCC_check_connections ();
3625 * @brief Start a polling timer for the connection.
3627 * When a neighbor does not accept more traffic on the connection it could be
3628 * caused by a simple congestion or by a lost ACK. Polling enables to check
3629 * for the lastest ACK status for a connection.
3631 * @param c Connection.
3632 * @param fwd Should we poll in the FWD direction?
3635 GCC_start_poll (struct CadetConnection *c, int fwd)
3637 struct CadetFlowControl *fc;
3639 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3640 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3642 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3644 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not needed (%p, %p)\n",
3645 fc->poll_task, fc->poll_msg);
3648 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3649 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3656 * @brief Stop polling a connection for ACKs.
3658 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3660 * @param c Connection.
3661 * @param fwd Should we stop the poll in the FWD direction?
3664 GCC_stop_poll (struct CadetConnection *c, int fwd)
3666 struct CadetFlowControl *fc;
3668 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3669 if (NULL != fc->poll_task)
3671 GNUNET_SCHEDULER_cancel (fc->poll_task);
3672 fc->poll_task = NULL;
3678 * Get a (static) string for a connection.
3680 * @param c Connection.
3683 GCC_2s (const struct CadetConnection *c)
3690 static char buf[128];
3692 SPRINTF (buf, "%s (->%s)",
3693 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3696 return GNUNET_h2s (GC_h2hc (&c->id));
3701 * Log all possible info about the connection state.
3703 * @param c Connection to debug.
3704 * @param level Debug level to use.
3707 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3712 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3714 __FILE__, __FUNCTION__, __LINE__);
3720 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3724 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3725 s = path_2s (c->path);
3726 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3728 LOG2 (level, "CCC state: %s, destroy: %u\n",
3729 GCC_state2s (c->state), c->destroy);
3730 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3731 if (NULL != c->perf)
3732 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3734 LOG2 (level, "CCC FWD flow control:\n");
3735 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3736 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3737 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3738 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3739 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3740 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3741 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3742 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3744 LOG2 (level, "CCC BCK flow control:\n");
3745 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3746 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3747 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3748 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3749 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3750 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3751 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3752 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3754 LOG2 (level, "CCC DEBUG CONNECTION END\n");