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 GNUNET_assert (NULL != c->t);
1068 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1069 if (0 < fc->queue_n || GNUNET_YES == GCT_has_queued_traffic (c->t))
1071 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
1075 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
1077 GNUNET_assert (NULL != c->t);
1078 msg.size = htons (sizeof (msg));
1079 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
1081 GNUNET_assert (NULL ==
1082 GCT_send_prebuilt_message (&msg, c->t, c,
1083 GNUNET_NO, NULL, NULL));
1084 GCC_check_connections ();
1089 * Send CONNECTION_{CREATE/ACK} packets for a connection.
1091 * @param c Connection for which to send the message.
1092 * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
1095 connection_recreate (struct CadetConnection *c, int fwd)
1097 LOG (GNUNET_ERROR_TYPE_DEBUG,
1098 "sending connection recreate\n");
1100 GCC_send_create (c);
1102 send_connection_ack (c, GNUNET_NO);
1107 * Generic connection timer management.
1108 * Depending on the role of the peer in the connection will send the
1109 * appropriate message (build or keepalive)
1111 * @param c Conncetion to maintain.
1112 * @param fwd Is FWD?
1115 connection_maintain (struct CadetConnection *c, int fwd)
1117 if (GNUNET_NO != c->destroy)
1119 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
1126 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1130 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
1132 /* If status is SEARCHING, why is there a connection? Should be WAITING */
1134 GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
1135 LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
1136 schedule_next_keepalive (c, fwd);
1141 case CADET_CONNECTION_NEW:
1144 case CADET_CONNECTION_SENT:
1145 connection_recreate (c, fwd);
1147 case CADET_CONNECTION_READY:
1148 send_connection_keepalive (c, fwd);
1157 * Keep the connection alive.
1159 * @param c Connection to keep alive.
1160 * @param fwd Direction.
1161 * @param shutdown Are we shutting down? (Don't send traffic)
1162 * Non-zero value for true, not necessarily GNUNET_YES.
1165 connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
1167 GCC_check_connections ();
1168 LOG (GNUNET_ERROR_TYPE_DEBUG,
1169 "%s keepalive for %s\n",
1170 GC_f2s (fwd), GCC_2s (c));
1173 c->fwd_maintenance_task = NULL;
1175 c->bck_maintenance_task = NULL;
1177 if (GNUNET_NO != shutdown)
1180 connection_maintain (c, fwd);
1181 GCC_check_connections ();
1182 /* Next execution will be scheduled by message_sent or _maintain*/
1187 * Keep the connection alive in the FWD direction.
1189 * @param cls Closure (connection to keepalive).
1190 * @param tc TaskContext.
1193 connection_fwd_keepalive (void *cls,
1194 const struct GNUNET_SCHEDULER_TaskContext *tc)
1196 GCC_check_connections ();
1197 connection_keepalive ((struct CadetConnection *) cls,
1199 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1200 GCC_check_connections ();
1205 * Keep the connection alive in the BCK direction.
1207 * @param cls Closure (connection to keepalive).
1208 * @param tc TaskContext.
1211 connection_bck_keepalive (void *cls,
1212 const struct GNUNET_SCHEDULER_TaskContext *tc)
1214 GCC_check_connections ();
1215 connection_keepalive ((struct CadetConnection *) cls,
1217 tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
1218 GCC_check_connections ();
1223 * Schedule next keepalive task, taking in consideration
1224 * the connection state and number of retries.
1226 * If the peer is not the origin, do nothing.
1228 * @param c Connection for which to schedule the next keepalive.
1229 * @param fwd Direction for the next keepalive.
1232 schedule_next_keepalive (struct CadetConnection *c, int fwd)
1234 struct GNUNET_TIME_Relative delay;
1235 struct GNUNET_SCHEDULER_Task * *task_id;
1236 GNUNET_SCHEDULER_TaskCallback keepalive_task;
1238 GCC_check_connections ();
1239 if (GNUNET_NO == GCC_is_origin (c, fwd))
1242 /* Calculate delay to use, depending on the state of the connection */
1243 if (CADET_CONNECTION_READY == c->state)
1245 delay = refresh_connection_time;
1249 if (1 > c->create_retry)
1250 c->create_retry = 1;
1251 delay = GNUNET_TIME_relative_multiply (create_connection_time,
1253 if (c->create_retry < 64)
1254 c->create_retry *= 2;
1257 /* Select direction-dependent parameters */
1258 if (GNUNET_YES == fwd)
1260 task_id = &c->fwd_maintenance_task;
1261 keepalive_task = &connection_fwd_keepalive;
1265 task_id = &c->bck_maintenance_task;
1266 keepalive_task = &connection_bck_keepalive;
1269 /* Check that no one scheduled it before us */
1270 if (NULL != *task_id)
1272 /* No need for a _break. It can happen for instance when sending a SYNACK
1273 * for a duplicate SYN: the first SYNACK scheduled the task. */
1274 GNUNET_SCHEDULER_cancel (*task_id);
1277 /* Schedule the task */
1278 *task_id = GNUNET_SCHEDULER_add_delayed (delay,
1281 LOG (GNUNET_ERROR_TYPE_DEBUG, "next keepalive in %s\n",
1282 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1283 GCC_check_connections ();
1288 * @brief Re-initiate traffic on this connection if necessary.
1290 * Check if there is traffic queued towards this peer
1291 * and the core transmit handle is NULL (traffic was stalled).
1292 * If so, call core tmt rdy.
1294 * @param c Connection on which initiate traffic.
1295 * @param fwd Is this about fwd traffic?
1298 connection_unlock_queue (struct CadetConnection *c, int fwd)
1300 struct CadetPeer *peer;
1302 GCC_check_connections ();
1303 LOG (GNUNET_ERROR_TYPE_DEBUG,
1304 "connection_unlock_queue %s on %s\n",
1305 GC_f2s (fwd), GCC_2s (c));
1307 if (GCC_is_terminal (c, fwd))
1309 LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal, can unlock!\n");
1313 peer = get_hop (c, fwd);
1314 GCP_queue_unlock (peer, c);
1315 GCC_check_connections ();
1320 * Cancel all transmissions that belong to a certain connection.
1322 * If the connection is scheduled for destruction and no more messages are left,
1323 * the connection will be destroyed by the continuation call.
1325 * @param c Connection which to cancel. Might be destroyed during this call.
1326 * @param fwd Cancel fwd traffic?
1329 connection_cancel_queues (struct CadetConnection *c,
1332 struct CadetFlowControl *fc;
1333 struct CadetPeer *peer;
1335 GCC_check_connections ();
1336 LOG (GNUNET_ERROR_TYPE_DEBUG,
1337 "Cancel %s queues for connection %s\n",
1338 GC_f2s (fwd), GCC_2s (c));
1345 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1346 if (NULL != fc->poll_task)
1348 GNUNET_SCHEDULER_cancel (fc->poll_task);
1349 fc->poll_task = NULL;
1350 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cancel POLL in ccq for fc %p\n", fc);
1352 peer = get_hop (c, fwd);
1353 GCP_queue_cancel (peer, c);
1354 GCC_check_connections ();
1359 * Function called if a connection has been stalled for a while,
1360 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1362 * @param cls Closure (poll ctx).
1363 * @param tc TaskContext.
1366 connection_poll (void *cls,
1367 const struct GNUNET_SCHEDULER_TaskContext *tc);
1371 * Callback called when a queued POLL message is sent.
1373 * @param cls Closure (FC).
1374 * @param c Connection this message was on.
1375 * @param q Queue handler this call invalidates.
1376 * @param type Type of message sent.
1377 * @param fwd Was this a FWD going message?
1378 * @param size Size of the message.
1381 poll_sent (void *cls,
1382 struct CadetConnection *c,
1383 struct CadetConnectionQueue *q,
1384 uint16_t type, int fwd, size_t size)
1386 struct CadetFlowControl *fc = cls;
1388 if (2 == c->destroy)
1390 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
1393 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
1395 fc->poll_msg = NULL;
1396 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1397 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1400 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
1405 * Function called if a connection has been stalled for a while,
1406 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
1408 * @param cls Closure (poll ctx).
1409 * @param tc TaskContext.
1412 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1414 struct CadetFlowControl *fc = cls;
1415 struct GNUNET_CADET_Poll msg;
1416 struct CadetConnection *c;
1419 fc->poll_task = NULL;
1420 GCC_check_connections ();
1421 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1427 fwd = fc == &c->fwd_fc;
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1429 GCC_2s (c), GC_f2s (fwd));
1431 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
1432 msg.header.size = htons (sizeof (msg));
1433 msg.pid = htonl (fc->last_pid_sent);
1434 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
1436 GCC_send_prebuilt_message (&msg.header, 0, fc->last_pid_sent, c,
1437 fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
1438 GNUNET_assert (NULL != fc->poll_msg);
1439 GCC_check_connections ();
1444 * Resend all queued messages for a connection on other connections of the
1445 * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
1447 * @param c Connection whose messages to resend.
1448 * @param fwd Resend fwd messages?
1451 resend_messages_and_destroy (struct CadetConnection *c, int fwd)
1453 struct GNUNET_MessageHeader *out_msg;
1454 struct CadetTunnel *t = c->t;
1455 struct CadetPeer *neighbor;
1456 unsigned int pending;
1459 GCC_check_connections ();
1460 c->state = CADET_CONNECTION_DESTROYED;
1461 c->destroy = GNUNET_YES;
1463 destroyed = GNUNET_NO;
1464 neighbor = get_hop (c, fwd);
1465 pending = c->pending_messages;
1467 while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
1470 GCT_resend_message (out_msg, t);
1471 GNUNET_free (out_msg);
1474 /* All pending messages should have been popped,
1475 * and the connection destroyed by the continuation.
1477 if (GNUNET_YES != destroyed)
1482 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
1483 if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1487 GCC_check_connections ();
1492 * Generic connection timeout implementation.
1494 * Timeout function due to lack of keepalive/traffic from an endpoint.
1495 * Destroys connection if called.
1497 * @param c Connection to destroy.
1498 * @param fwd Was the timeout from the origin? (FWD timeout)
1501 connection_timeout (struct CadetConnection *c, int fwd)
1503 struct CadetFlowControl *reverse_fc;
1505 GCC_check_connections ();
1506 reverse_fc = fwd ? &c->bck_fc : &c->fwd_fc;
1508 LOG (GNUNET_ERROR_TYPE_INFO,
1509 "Connection %s %s timed out. Destroying.\n",
1512 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1514 if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
1520 /* If dest, salvage queued traffic. */
1521 if (GCC_is_origin (c, !fwd))
1523 const struct GNUNET_PeerIdentity *next_hop;
1525 next_hop = GCP_get_id (fwd ? get_prev_hop (c) : get_next_hop (c));
1526 send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
1527 if (0 < reverse_fc->queue_n)
1528 resend_messages_and_destroy (c, !fwd);
1529 GCC_check_connections ();
1534 GCC_check_connections ();
1539 * Timeout function due to lack of keepalive/traffic from the owner.
1540 * Destroys connection if called.
1542 * @param cls Closure (connection to destroy).
1543 * @param tc TaskContext.
1546 connection_fwd_timeout (void *cls,
1547 const struct GNUNET_SCHEDULER_TaskContext *tc)
1549 struct CadetConnection *c = cls;
1551 c->fwd_maintenance_task = NULL;
1552 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1554 GCC_check_connections ();
1555 connection_timeout (c, GNUNET_YES);
1556 GCC_check_connections ();
1561 * Timeout function due to lack of keepalive/traffic from the destination.
1562 * Destroys connection if called.
1564 * @param cls Closure (connection to destroy).
1565 * @param tc TaskContext
1568 connection_bck_timeout (void *cls,
1569 const struct GNUNET_SCHEDULER_TaskContext *tc)
1571 struct CadetConnection *c = cls;
1573 c->bck_maintenance_task = NULL;
1574 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1576 GCC_check_connections ();
1577 connection_timeout (c, GNUNET_NO);
1578 GCC_check_connections ();
1583 * Resets the connection timeout task, some other message has done the
1585 * - For the first peer on the direction this means to send
1586 * a keepalive or a path confirmation message (either create or ACK).
1587 * - For all other peers, this means to destroy the connection,
1588 * due to lack of activity.
1589 * Starts the timeout if no timeout was running (connection just created).
1591 * @param c Connection whose timeout to reset.
1592 * @param fwd Is this forward?
1594 * TODO use heap to improve efficiency of scheduler.
1597 connection_reset_timeout (struct CadetConnection *c, int fwd)
1599 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
1600 if (GCC_is_origin (c, fwd)) /* Startpoint */
1602 schedule_next_keepalive (c, fwd);
1604 else /* Relay, endpoint. */
1606 struct GNUNET_TIME_Relative delay;
1607 struct GNUNET_SCHEDULER_Task * *ti;
1608 GNUNET_SCHEDULER_TaskCallback f;
1610 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
1613 GNUNET_SCHEDULER_cancel (*ti);
1614 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
1615 LOG (GNUNET_ERROR_TYPE_DEBUG, " timing out in %s\n",
1616 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
1617 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
1618 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
1624 * Add the connection to the list of both neighbors.
1626 * @param c Connection.
1628 * @return #GNUNET_OK if everything went fine
1629 * #GNUNET_SYSERR if the was an error and @c c is malformed.
1632 register_neighbors (struct CadetConnection *c)
1634 c->next_peer = get_next_hop (c);
1635 c->prev_peer = get_prev_hop (c);
1636 GNUNET_assert (c->next_peer != c->prev_peer);
1637 LOG (GNUNET_ERROR_TYPE_DEBUG,
1638 "register neighbors for connection %s\n",
1640 path_debug (c->path);
1641 LOG (GNUNET_ERROR_TYPE_DEBUG,
1642 "own pos %u\n", c->own_pos);
1643 LOG (GNUNET_ERROR_TYPE_DEBUG,
1644 "putting connection %s to next peer %p\n",
1647 LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
1649 GCP_2s (c->next_peer));
1650 LOG (GNUNET_ERROR_TYPE_DEBUG,
1651 "putting connection %s to prev peer %p\n",
1654 LOG (GNUNET_ERROR_TYPE_DEBUG,
1655 "prev peer %p %s\n",
1657 GCP_2s (c->prev_peer));
1659 if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
1660 (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
1662 if (GCC_is_origin (c, GNUNET_YES))
1663 GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
1664 GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
1666 LOG (GNUNET_ERROR_TYPE_DEBUG,
1667 " register neighbors failed\n");
1668 LOG (GNUNET_ERROR_TYPE_DEBUG,
1669 " prev: %s, neighbor?: %d\n",
1670 GCP_2s (c->prev_peer),
1671 GCP_is_neighbor (c->prev_peer));
1672 LOG (GNUNET_ERROR_TYPE_DEBUG,
1673 " next: %s, neighbor?: %d\n",
1674 GCP_2s (c->next_peer),
1675 GCP_is_neighbor (c->next_peer));
1676 return GNUNET_SYSERR;
1678 GCP_add_connection (c->next_peer, c, GNUNET_NO);
1679 GCP_add_connection (c->prev_peer, c, GNUNET_YES);
1686 * Remove the connection from the list of both neighbors.
1688 * @param c Connection.
1691 unregister_neighbors (struct CadetConnection *c)
1693 /* Either already unregistered or never got registered, it's ok either way. */
1694 if (NULL == c->path)
1696 if (NULL != c->next_peer)
1698 GCP_remove_connection (c->next_peer, c);
1699 c->next_peer = NULL;
1701 if (NULL != c->prev_peer)
1703 GCP_remove_connection (c->prev_peer, c);
1704 c->prev_peer = NULL;
1710 * Bind the connection to the peer and the tunnel to that peer.
1712 * If the peer has no tunnel, create one. Update tunnel and connection
1713 * data structres to reflect new status.
1715 * @param c Connection.
1719 add_to_peer (struct CadetConnection *c,
1720 struct CadetPeer *peer)
1722 GCP_add_tunnel (peer);
1723 c->t = GCP_get_tunnel (peer);
1724 GCT_add_connection (c->t, c);
1729 * Iterator to compare each connection's path with the path of a new connection.
1731 * If the connection conincides, the c member of path is set to the connection
1732 * and the destroy flag of the connection is set.
1734 * @param cls Closure (new path).
1735 * @param c Connection in the tunnel to check.
1738 check_path (void *cls, struct CadetConnection *c)
1740 struct CadetConnection *new_conn = cls;
1741 struct CadetPeerPath *path = new_conn->path;
1743 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
1744 GCC_2s (c), c, c->path->length);
1747 && c->destroy == GNUNET_NO
1748 && c->state != CADET_CONNECTION_BROKEN
1749 && c->state != CADET_CONNECTION_DESTROYED
1750 && path_equivalent (path, c->path))
1752 new_conn->destroy = GNUNET_YES;
1753 new_conn->path->c = c;
1754 LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
1760 * Finds out if this path is already being used by and existing connection.
1762 * Checks the tunnel towards the first peer in the path to see if it contains
1763 * any connection with the same path.
1765 * If the existing connection is ready, it is kept.
1766 * Otherwise if the sender has a smaller ID that ours, we accept it (and
1767 * the peer will eventually reject our attempt).
1769 * @param path Path to check.
1770 * @return #GNUNET_YES if the tunnel has a connection with the same path,
1771 * #GNUNET_NO otherwise.
1774 does_connection_exist (struct CadetConnection *conn)
1776 struct CadetPeer *p;
1777 struct CadetTunnel *t;
1778 struct CadetConnection *c;
1780 p = GCP_get_short (conn->path->peers[0]);
1781 t = GCP_get_tunnel (p);
1785 LOG (GNUNET_ERROR_TYPE_DEBUG,
1786 "Checking for duplicates\n");
1788 GCT_iterate_connections (t, &check_path, conn);
1790 if (GNUNET_YES == conn->destroy)
1793 conn->destroy = GNUNET_NO;
1794 conn->path->c = conn;
1795 LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
1796 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
1797 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
1798 if (CADET_CONNECTION_READY == c->state)
1800 /* The other peer confirmed a live connection with this path,
1801 * why is it trying to duplicate it. */
1805 if (GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GCP_get_id (p)) > 0)
1807 struct CadetPeer *neighbor;
1809 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate allowed (killing old)\n");
1810 if (GCC_is_origin (c, GNUNET_YES))
1811 neighbor = get_next_hop (c);
1813 neighbor = get_prev_hop (c);
1814 send_broken_unknown (&c->id, &my_full_id, NULL,
1815 GCP_get_id (neighbor));
1824 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
1831 * Log receipt of message on stderr (INFO level).
1833 * @param message Message received.
1834 * @param peer Peer who sent the message.
1835 * @param hash Connection ID.
1838 log_message (const struct GNUNET_MessageHeader *message,
1839 const struct GNUNET_PeerIdentity *peer,
1840 const struct GNUNET_CADET_Hash *hash)
1844 size = ntohs (message->size);
1845 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1846 LOG (GNUNET_ERROR_TYPE_INFO, "\n");
1847 LOG (GNUNET_ERROR_TYPE_INFO, "<-- %s on connection %s from %s, %6u bytes\n",
1848 GC_m2s (ntohs (message->type)), GNUNET_h2s (GC_h2hc (hash)),
1849 GNUNET_i2s (peer), (unsigned int) size);
1852 /******************************************************************************/
1853 /******************************** API ***********************************/
1854 /******************************************************************************/
1857 * Core handler for connection creation.
1859 * @param cls Closure (unused).
1860 * @param peer Sender (neighbor).
1861 * @param message Message.
1862 * @return #GNUNET_OK to keep the connection open,
1863 * #GNUNET_SYSERR to close it (signal serious error)
1866 GCC_handle_create (void *cls,
1867 const struct GNUNET_PeerIdentity *peer,
1868 const struct GNUNET_MessageHeader *message)
1870 struct GNUNET_CADET_ConnectionCreate *msg;
1871 struct GNUNET_PeerIdentity *id;
1872 struct GNUNET_CADET_Hash *cid;
1873 struct CadetPeerPath *path;
1874 struct CadetPeer *dest_peer;
1875 struct CadetPeer *orig_peer;
1876 struct CadetConnection *c;
1877 unsigned int own_pos;
1880 GCC_check_connections ();
1882 size = ntohs (message->size);
1883 if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
1885 GNUNET_break_op (0);
1889 /* Calculate hops */
1890 size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
1891 if (size % sizeof (struct GNUNET_PeerIdentity))
1893 GNUNET_break_op (0);
1896 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1898 GNUNET_break_op (0);
1901 size /= sizeof (struct GNUNET_PeerIdentity);
1904 GNUNET_break_op (0);
1907 LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
1909 /* Get parameters */
1910 msg = (struct GNUNET_CADET_ConnectionCreate *) message;
1912 log_message (message, peer, cid);
1913 id = (struct GNUNET_PeerIdentity *) &msg[1];
1914 LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
1916 /* Create connection */
1917 c = connection_get (cid);
1920 path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
1921 size, myid, &own_pos);
1924 /* Path was malformed, probably our own ID was not in it. */
1925 GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
1926 GNUNET_break_op (0);
1932 /* We received this request from a neighbor, we cannot be origin */
1933 GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
1934 GNUNET_break_op (0);
1935 path_destroy (path);
1939 LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
1940 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
1941 c = GCC_new (cid, NULL, path, own_pos);
1944 if (path->length - 1 == own_pos)
1946 /* If we are destination, why did the creation fail? */
1948 path_destroy (path);
1949 GCC_check_connections ();
1952 send_broken_unknown (cid, &my_full_id,
1953 GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
1955 path_destroy (path);
1956 GCC_check_connections ();
1959 GCP_add_path_to_all (path, GNUNET_NO);
1960 connection_reset_timeout (c, GNUNET_YES);
1964 path = path_duplicate (c->path);
1966 if (CADET_CONNECTION_NEW == c->state)
1967 connection_change_state (c, CADET_CONNECTION_SENT);
1969 /* Remember peers */
1970 dest_peer = GCP_get (&id[size - 1]);
1971 orig_peer = GCP_get (&id[0]);
1973 /* Is it a connection to us? */
1974 if (c->own_pos == path->length - 1)
1976 LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
1977 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
1979 add_to_peer (c, orig_peer);
1980 if (GNUNET_YES == does_connection_exist (c))
1982 // FIXME Peer created a connection equal to one we think exists
1983 // and is fine. What should we do?
1984 // Use explicit duplicate?
1985 // Accept new conn and destroy the old? (interruption in higher level)
1986 // Keep both and postpone disambiguation?
1987 // Keep the one created by peer with higher ID?
1988 // For now: reject new connection until current confirmed dead
1989 GNUNET_break_op (0);
1990 GCC_debug (c, GNUNET_ERROR_TYPE_WARNING);
1991 path_destroy (path);
1993 send_broken_unknown (cid, &my_full_id, NULL, peer);
1994 GCC_check_connections ();
1998 if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
1999 GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
2001 send_connection_ack (c, GNUNET_NO);
2002 if (CADET_CONNECTION_SENT == c->state)
2003 connection_change_state (c, CADET_CONNECTION_ACK);
2007 /* It's for somebody else! Retransmit. */
2008 LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2009 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
2010 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
2011 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
2012 GNUNET_YES, GNUNET_YES,
2015 path_destroy (path);
2016 GCC_check_connections ();
2022 * Core handler for path confirmations.
2024 * @param cls closure
2025 * @param message message
2026 * @param peer peer identity this notification is about
2027 * @return #GNUNET_OK to keep the connection open,
2028 * #GNUNET_SYSERR to close it (signal serious error)
2031 GCC_handle_confirm (void *cls,
2032 const struct GNUNET_PeerIdentity *peer,
2033 const struct GNUNET_MessageHeader *message)
2035 struct GNUNET_CADET_ConnectionACK *msg;
2036 struct CadetConnection *c;
2037 struct CadetPeerPath *p;
2038 struct CadetPeer *pi;
2039 enum CadetConnectionState oldstate;
2042 GCC_check_connections ();
2043 msg = (struct GNUNET_CADET_ConnectionACK *) message;
2044 log_message (message, peer, &msg->cid);
2045 c = connection_get (&msg->cid);
2048 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
2050 LOG (GNUNET_ERROR_TYPE_DEBUG,
2051 " don't know the connection!\n");
2052 send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
2053 GCC_check_connections ();
2057 oldstate = c->state;
2058 LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
2059 pi = GCP_get (peer);
2060 if (get_next_hop (c) == pi)
2062 LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
2064 if (CADET_CONNECTION_SENT == oldstate)
2065 connection_change_state (c, CADET_CONNECTION_ACK);
2067 else if (get_prev_hop (c) == pi)
2069 LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
2071 connection_change_state (c, CADET_CONNECTION_READY);
2075 GNUNET_break_op (0);
2079 connection_reset_timeout (c, fwd);
2081 /* Add path to peers? */
2085 GCP_add_path_to_all (p, GNUNET_YES);
2092 /* Message for us as creator? */
2093 if (GCC_is_origin (c, GNUNET_YES))
2095 if (GNUNET_NO != fwd)
2097 GNUNET_break_op (0);
2100 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
2102 /* If just created, cancel the short timeout and start a long one */
2103 if (CADET_CONNECTION_SENT == oldstate)
2104 connection_reset_timeout (c, GNUNET_YES);
2106 /* Change connection state */
2107 connection_change_state (c, CADET_CONNECTION_READY);
2108 send_connection_ack (c, GNUNET_YES);
2110 /* Change tunnel state, trigger KX */
2111 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2112 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2113 GCC_check_connections ();
2117 /* Message for us as destination? */
2118 if (GCC_is_terminal (c, GNUNET_YES))
2120 if (GNUNET_YES != fwd)
2122 GNUNET_break_op (0);
2125 LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
2127 /* If just created, cancel the short timeout and start a long one */
2128 if (CADET_CONNECTION_ACK == oldstate)
2129 connection_reset_timeout (c, GNUNET_NO);
2131 /* Change tunnel state */
2132 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2133 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2134 GCC_check_connections ();
2138 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2139 GNUNET_assert (NULL ==
2140 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2141 GNUNET_YES, NULL, NULL));
2142 GCC_check_connections ();
2148 * Core handler for notifications of broken connections.
2150 * @param cls Closure (unused).
2151 * @param id Peer identity of sending neighbor.
2152 * @param message Message.
2153 * @return #GNUNET_OK to keep the connection open,
2154 * #GNUNET_SYSERR to close it (signal serious error)
2157 GCC_handle_broken (void* cls,
2158 const struct GNUNET_PeerIdentity* id,
2159 const struct GNUNET_MessageHeader* message)
2161 struct GNUNET_CADET_ConnectionBroken *msg;
2162 struct CadetConnection *c;
2163 struct CadetTunnel *t;
2167 GCC_check_connections ();
2168 msg = (struct GNUNET_CADET_ConnectionBroken *) message;
2169 log_message (message, id, &msg->cid);
2170 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2171 GNUNET_i2s (&msg->peer1));
2172 LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
2173 GNUNET_i2s (&msg->peer2));
2174 c = connection_get (&msg->cid);
2177 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
2178 GCC_check_connections ();
2184 fwd = is_fwd (c, id);
2185 c->destroy = GNUNET_YES;
2186 if (GCC_is_terminal (c, fwd))
2188 struct CadetPeer *endpoint;
2192 /* A terminal connection should not have 't' set to NULL. */
2194 GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
2197 endpoint = GCP_get_short (c->path->peers[c->path->length - 1]);
2198 if (2 < c->path->length)
2199 path_invalidate (c->path);
2200 GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
2202 c->state = CADET_CONNECTION_BROKEN;
2203 GCT_remove_connection (t, c);
2206 pending = c->pending_messages;
2208 resend_messages_and_destroy (c, !fwd);
2214 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2215 GNUNET_YES, NULL, NULL));
2216 connection_cancel_queues (c, !fwd);
2218 GCC_check_connections ();
2224 * Core handler for tunnel destruction
2226 * @param cls Closure (unused).
2227 * @param peer Peer identity of sending neighbor.
2228 * @param message Message.
2229 * @return #GNUNET_OK to keep the connection open,
2230 * #GNUNET_SYSERR to close it (signal serious error)
2233 GCC_handle_destroy (void *cls,
2234 const struct GNUNET_PeerIdentity *peer,
2235 const struct GNUNET_MessageHeader *message)
2237 const struct GNUNET_CADET_ConnectionDestroy *msg;
2238 struct CadetConnection *c;
2241 GCC_check_connections ();
2242 msg = (const struct GNUNET_CADET_ConnectionDestroy *) message;
2243 log_message (message, peer, &msg->cid);
2244 c = connection_get (&msg->cid);
2247 /* Probably already got the message from another path,
2248 * destroyed the tunnel and retransmitted to children.
2251 GNUNET_STATISTICS_update (stats,
2252 "# control on unknown connection",
2254 LOG (GNUNET_ERROR_TYPE_DEBUG,
2255 " connection unknown: already destroyed?\n");
2256 GCC_check_connections ();
2259 fwd = is_fwd (c, peer);
2260 if (GNUNET_SYSERR == fwd)
2262 GNUNET_break_op (0); /* FIXME */
2265 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2267 GNUNET_assert (NULL ==
2268 GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2269 GNUNET_YES, NULL, NULL));
2271 else if (0 == c->pending_messages)
2273 LOG (GNUNET_ERROR_TYPE_DEBUG,
2274 " directly destroying connection!\n");
2276 GCC_check_connections ();
2279 c->destroy = GNUNET_YES;
2280 c->state = CADET_CONNECTION_DESTROYED;
2283 GCT_remove_connection (c->t, c);
2286 GCC_check_connections ();
2292 * Check the message against internal state and test if it goes FWD or BCK.
2294 * Updates the PID, state and timeout values for the connection.
2296 * @param message Message to check. It must belong to an existing connection.
2297 * @param minimum_size The message cannot be smaller than this value.
2298 * @param cid Connection ID (even if @a c is NULL, the ID is still needed).
2299 * @param c Connection this message should belong. If NULL, check fails.
2300 * @param neighbor Neighbor that sent the message.
2303 check_message (const struct GNUNET_MessageHeader *message,
2304 size_t minimum_size,
2305 const struct GNUNET_CADET_Hash* cid,
2306 struct CadetConnection *c,
2307 const struct GNUNET_PeerIdentity *neighbor,
2310 GNUNET_PEER_Id neighbor_id;
2311 struct CadetFlowControl *fc;
2312 struct CadetPeer *hop;
2317 if (ntohs (message->size) < minimum_size)
2319 GNUNET_break_op (0);
2320 LOG (GNUNET_ERROR_TYPE_WARNING, "Size %u < %u\n",
2321 ntohs (message->size), minimum_size);
2322 return GNUNET_SYSERR;
2325 /* Check connection */
2328 GNUNET_STATISTICS_update (stats,
2329 "# unknown connection",
2331 LOG (GNUNET_ERROR_TYPE_DEBUG,
2332 "%s on unknown connection %s\n",
2333 GC_m2s (ntohs (message->type)),
2334 GNUNET_h2s (GC_h2hc (cid)));
2335 send_broken_unknown (cid,
2339 return GNUNET_SYSERR;
2342 /* Check if origin is as expected */
2343 neighbor_id = GNUNET_PEER_search (neighbor);
2344 hop = get_prev_hop (c);
2345 if (neighbor_id == GCP_get_short_id (hop))
2351 hop = get_next_hop (c);
2352 GNUNET_break (hop == c->next_peer);
2353 if (neighbor_id == GCP_get_short_id (hop))
2359 /* Unexpected peer sending traffic on a connection. */
2360 GNUNET_break_op (0);
2361 return GNUNET_SYSERR;
2365 /* Check PID for payload messages */
2366 type = ntohs (message->type);
2367 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
2368 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
2370 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2371 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
2372 pid, fc->last_pid_recv + 1, fc->last_ack_sent);
2373 if (GC_is_pid_bigger (pid, fc->last_ack_sent))
2375 GNUNET_break_op (0);
2376 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
2377 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
2378 pid, fc->last_pid_recv, fc->last_ack_sent);
2379 return GNUNET_SYSERR;
2381 if (GC_is_pid_bigger (pid, fc->last_pid_recv))
2385 delta = pid - fc->last_pid_recv;
2386 fc->last_pid_recv = pid;
2387 fc->recv_bitmap <<= delta;
2388 fc->recv_bitmap |= 1;
2392 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
2393 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
2395 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
2396 pid, fc->last_pid_recv - 31);
2397 return GNUNET_SYSERR;
2399 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
2403 /* Count as connection confirmation. */
2404 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
2406 connection_change_state (c, CADET_CONNECTION_READY);
2409 if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
2410 GCT_change_cstate (c->t, CADET_TUNNEL_READY);
2413 connection_reset_timeout (c, fwd);
2420 * Generic handler for cadet network encrypted traffic.
2422 * @param peer Peer identity this notification is about.
2423 * @param msg Encrypted message.
2424 * @return #GNUNET_OK to keep the connection open,
2425 * #GNUNET_SYSERR to close it (signal serious error)
2428 handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
2429 const struct GNUNET_MessageHeader *message)
2431 const struct GNUNET_CADET_Encrypted *otr_msg;
2432 const struct GNUNET_CADET_AX *ax_msg;
2433 const struct GNUNET_CADET_Hash* cid;
2434 struct CadetConnection *c;
2435 size_t minumum_size;
2441 GCC_check_connections ();
2442 if (GNUNET_MESSAGE_TYPE_CADET_AX == ntohs (message->type))
2444 overhead = sizeof (struct GNUNET_CADET_AX);
2445 ax_msg = (const struct GNUNET_CADET_AX *) message;
2447 pid = ntohl (ax_msg->pid);
2452 overhead = sizeof (struct GNUNET_CADET_Encrypted);
2453 otr_msg = (const struct GNUNET_CADET_Encrypted *) message;
2454 cid = &otr_msg->cid;
2455 pid = ntohl (otr_msg->pid);
2458 log_message (message, peer, cid);
2460 minumum_size = sizeof (struct GNUNET_MessageHeader) + overhead;
2461 c = connection_get (cid);
2462 fwd = check_message (message,
2469 /* If something went wrong, discard message. */
2470 if (GNUNET_SYSERR == fwd)
2472 GCC_check_connections ();
2476 /* Is this message for us? */
2477 if (GCC_is_terminal (c, fwd))
2479 GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
2483 GNUNET_break (GNUNET_NO != c->destroy);
2486 GCT_handle_encrypted (c->t, message);
2487 GCC_send_ack (c, fwd, GNUNET_NO);
2488 GCC_check_connections ();
2492 /* Message not for us: forward to next hop */
2493 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2494 if (NULL != otr_msg) /* only otr has ttl */
2496 ttl = ntohl (otr_msg->ttl);
2497 LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
2500 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
2501 LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
2502 GCC_send_ack (c, fwd, GNUNET_NO);
2503 GCC_check_connections ();
2508 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2509 GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
2510 GNUNET_NO, NULL, NULL));
2511 GCC_check_connections ();
2517 * Generic handler for cadet network encrypted traffic.
2519 * @param peer Peer identity this notification is about.
2520 * @param msg Encrypted message.
2521 * @return #GNUNET_OK to keep the connection open,
2522 * #GNUNET_SYSERR to close it (signal serious error)
2525 handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
2526 const struct GNUNET_CADET_KX *msg)
2528 const struct GNUNET_CADET_Hash* cid;
2529 struct CadetConnection *c;
2530 size_t expected_size;
2533 GCC_check_connections ();
2535 log_message (&msg->header, peer, cid);
2537 expected_size = sizeof (struct GNUNET_CADET_KX)
2538 + sizeof (struct GNUNET_MessageHeader);
2539 c = connection_get (cid);
2540 fwd = check_message (&msg->header,
2547 /* If something went wrong, discard message. */
2548 if (GNUNET_SYSERR == fwd)
2551 /* Is this message for us? */
2552 if (GCC_is_terminal (c, fwd))
2554 LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
2555 GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
2561 GCT_handle_kx (c->t, &msg[1].header);
2562 GCC_check_connections ();
2566 /* Message not for us: forward to next hop */
2567 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2568 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2569 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
2570 GNUNET_NO, NULL, NULL));
2571 GCC_check_connections ();
2577 * Core handler for key exchange traffic (ephemeral key, ping, pong).
2579 * @param cls Closure (unused).
2580 * @param message Message received.
2581 * @param peer Peer who sent the message.
2582 * @return #GNUNET_OK to keep the connection open,
2583 * #GNUNET_SYSERR to close it (signal serious error)
2586 GCC_handle_kx (void *cls,
2587 const struct GNUNET_PeerIdentity *peer,
2588 const struct GNUNET_MessageHeader *message)
2590 GCC_check_connections ();
2591 return handle_cadet_kx (peer, (struct GNUNET_CADET_KX *) message);
2596 * Core handler for encrypted cadet network traffic (channel mgmt, data).
2598 * @param cls Closure (unused).
2599 * @param message Message received.
2600 * @param peer Peer who sent the message.
2601 * @return #GNUNET_OK to keep the connection open,
2602 * #GNUNET_SYSERR to close it (signal serious error)
2605 GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
2606 const struct GNUNET_MessageHeader *message)
2608 GCC_check_connections ();
2609 return handle_cadet_encrypted (peer, message);
2614 * Core handler for cadet network traffic point-to-point acks.
2616 * @param cls closure
2617 * @param message message
2618 * @param peer peer identity this notification is about
2619 * @return #GNUNET_OK to keep the connection open,
2620 * #GNUNET_SYSERR to close it (signal serious error)
2623 GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2624 const struct GNUNET_MessageHeader *message)
2626 struct GNUNET_CADET_ACK *msg;
2627 struct CadetConnection *c;
2628 struct CadetFlowControl *fc;
2633 GCC_check_connections ();
2634 msg = (struct GNUNET_CADET_ACK *) message;
2635 log_message (message, peer, &msg->cid);
2636 c = connection_get (&msg->cid);
2639 GNUNET_STATISTICS_update (stats,
2640 "# ack on unknown connection",
2643 send_broken_unknown (&msg->cid,
2647 GCC_check_connections ();
2651 /* Is this a forward or backward ACK? */
2652 id = GNUNET_PEER_search (peer);
2653 if (GCP_get_short_id (get_next_hop (c)) == id)
2658 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2665 GNUNET_break_op (0);
2669 ack = ntohl (msg->ack);
2670 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2671 GC_f2s (fwd), ack, fc->last_ack_recv);
2672 if (GC_is_pid_bigger (ack, fc->last_ack_recv))
2673 fc->last_ack_recv = ack;
2675 /* Cancel polling if the ACK is big enough. */
2676 if (NULL != fc->poll_task &&
2677 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2679 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2680 GNUNET_SCHEDULER_cancel (fc->poll_task);
2681 fc->poll_task = NULL;
2682 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2685 connection_unlock_queue (c, fwd);
2686 GCC_check_connections ();
2692 * Core handler for cadet network traffic point-to-point ack polls.
2694 * @param cls closure
2695 * @param message message
2696 * @param peer peer identity this notification is about
2697 * @return #GNUNET_OK to keep the connection open,
2698 * #GNUNET_SYSERR to close it (signal serious error)
2701 GCC_handle_poll (void *cls,
2702 const struct GNUNET_PeerIdentity *peer,
2703 const struct GNUNET_MessageHeader *message)
2705 struct GNUNET_CADET_Poll *msg;
2706 struct CadetConnection *c;
2707 struct CadetFlowControl *fc;
2712 GCC_check_connections ();
2713 msg = (struct GNUNET_CADET_Poll *) message;
2714 log_message (message, peer, &msg->cid);
2715 c = connection_get (&msg->cid);
2718 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
2720 LOG (GNUNET_ERROR_TYPE_DEBUG,
2721 "POLL message on unknown connection %s!\n",
2722 GNUNET_h2s (GC_h2hc (&msg->cid)));
2723 send_broken_unknown (&msg->cid,
2727 GCC_check_connections ();
2731 /* Is this a forward or backward ACK?
2732 * Note: a poll should never be needed in a loopback case,
2733 * since there is no possiblility of packet loss there, so
2734 * this way of discerining FWD/BCK should not be a problem.
2736 id = GNUNET_PEER_search (peer);
2737 if (GCP_get_short_id (get_next_hop (c)) == id)
2739 LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
2742 else if (GCP_get_short_id (get_prev_hop (c)) == id)
2744 LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
2749 GNUNET_break_op (0);
2753 pid = ntohl (msg->pid);
2754 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
2755 fc->last_pid_recv = pid;
2756 fwd = fc == &c->bck_fc;
2757 GCC_send_ack (c, fwd, GNUNET_YES);
2758 GCC_check_connections ();
2765 * Send an ACK on the appropriate connection/channel, depending on
2766 * the direction and the position of the peer.
2768 * @param c Which connection to send the hop-by-hop ACK.
2769 * @param fwd Is this a fwd ACK? (will go dest->root).
2770 * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
2773 GCC_send_ack (struct CadetConnection *c, int fwd, int force)
2775 unsigned int buffer;
2777 GCC_check_connections ();
2778 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
2779 GC_f2s (fwd), GCC_2s (c));
2787 if (GNUNET_NO != c->destroy)
2789 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
2790 GCC_check_connections ();
2794 /* Get available buffer space */
2795 if (GCC_is_terminal (c, fwd))
2797 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
2798 buffer = GCT_get_channels_buffer (c->t);
2802 LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
2803 buffer = GCC_get_buffer (c, fwd);
2805 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
2806 if (0 == buffer && GNUNET_NO == force)
2808 GCC_check_connections ();
2812 /* Send available buffer space */
2813 if (GCC_is_origin (c, fwd))
2815 GNUNET_assert (NULL != c->t);
2816 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
2817 GCT_unchoke_channels (c->t);
2821 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
2822 send_ack (c, buffer, fwd, force);
2824 GCC_check_connections ();
2829 * Initialize the connections subsystem
2831 * @param c Configuration handle.
2834 GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2836 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2838 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
2841 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2842 "CADET", "MAX_MSGS_QUEUE", "MISSING");
2843 GNUNET_SCHEDULER_shutdown ();
2848 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
2851 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2852 "CADET", "MAX_CONNECTIONS", "MISSING");
2853 GNUNET_SCHEDULER_shutdown ();
2858 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
2859 &refresh_connection_time))
2861 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2862 "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
2863 GNUNET_SCHEDULER_shutdown ();
2866 create_connection_time = GNUNET_TIME_UNIT_SECONDS;
2867 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
2872 * Destroy each connection on shutdown.
2874 * @param cls Closure (unused).
2875 * @param key Current key code (CID, unused).
2876 * @param value Value in the hash map (`struct CadetConnection`)
2878 * @return #GNUNET_YES, because we should continue to iterate
2881 shutdown_iterator (void *cls,
2882 const struct GNUNET_HashCode *key,
2885 struct CadetConnection *c = value;
2887 c->state = CADET_CONNECTION_DESTROYED;
2894 * Shut down the connections subsystem.
2899 GCC_check_connections ();
2900 GNUNET_CONTAINER_multihashmap_iterate (connections,
2903 GNUNET_CONTAINER_multihashmap_destroy (connections);
2909 * Create a connection.
2911 * @param cid Connection ID (either created locally or imposed remotely).
2912 * @param t Tunnel this connection belongs to (or NULL);
2913 * @param path Path this connection has to use (copy is made).
2914 * @param own_pos Own position in the @c path path.
2916 * @return Newly created connection, NULL in case of error (own id not in path).
2918 struct CadetConnection *
2919 GCC_new (const struct GNUNET_CADET_Hash *cid,
2920 struct CadetTunnel *t,
2921 struct CadetPeerPath *path,
2922 unsigned int own_pos)
2924 struct CadetConnection *c;
2925 struct CadetPeerPath *p;
2927 GCC_check_connections ();
2928 p = path_duplicate (path);
2929 c = GNUNET_new (struct CadetConnection);
2931 GNUNET_assert (GNUNET_OK ==
2932 GNUNET_CONTAINER_multihashmap_put (connections,
2934 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2935 fc_init (&c->fwd_fc);
2936 fc_init (&c->bck_fc);
2941 GNUNET_assert (own_pos <= p->length - 1);
2942 c->own_pos = own_pos;
2945 GNUNET_assert (NULL != p);
2946 if (GNUNET_OK != register_neighbors (c))
2950 /* We were the origin of this request, this means we have invalid
2951 * info about the paths to reach the destination. We must invalidate
2952 * the *original* path to avoid trying it again in the next minute.
2954 path_invalidate (path);
2957 path_destroy (c->path);
2962 LOG (GNUNET_ERROR_TYPE_INFO, "New connection %s\n", GCC_2s (c));
2963 GCC_check_connections ();
2969 GCC_destroy (struct CadetConnection *c)
2971 GCC_check_connections ();
2978 if (2 == c->destroy) /* cancel queues -> GCP_queue_cancel -> q_destroy -> */
2979 return; /* -> message_sent -> GCC_destroy. Don't loop. */
2982 LOG (GNUNET_ERROR_TYPE_DEBUG,
2983 "destroying connection %s\n",
2985 LOG (GNUNET_ERROR_TYPE_DEBUG,
2986 " fc's f: %p, b: %p\n",
2987 &c->fwd_fc, &c->bck_fc);
2988 LOG (GNUNET_ERROR_TYPE_DEBUG,
2989 " fc tasks f: %u, b: %u\n",
2990 c->fwd_fc.poll_task,
2991 c->bck_fc.poll_task);
2993 /* Cancel all traffic */
2994 if (NULL != c->path)
2996 connection_cancel_queues (c, GNUNET_YES);
2997 connection_cancel_queues (c, GNUNET_NO);
2999 unregister_neighbors (c);
3000 path_destroy (c->path);
3003 /* Cancel maintainance task (keepalive/timeout) */
3004 if (NULL != c->fwd_fc.poll_msg)
3006 GCC_cancel (c->fwd_fc.poll_msg);
3007 LOG (GNUNET_ERROR_TYPE_DEBUG,
3008 " POLL msg FWD canceled\n");
3010 if (NULL != c->bck_fc.poll_msg)
3012 GCC_cancel (c->bck_fc.poll_msg);
3013 LOG (GNUNET_ERROR_TYPE_DEBUG,
3014 " POLL msg BCK canceled\n");
3017 /* Delete from tunnel */
3019 GCT_remove_connection (c->t, c);
3021 if (NULL != c->fwd_maintenance_task)
3022 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
3023 if (NULL != c->bck_maintenance_task)
3024 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
3025 if (NULL != c->fwd_fc.poll_task)
3027 GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
3028 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task FWD canceled\n");
3030 if (NULL != c->bck_fc.poll_task)
3032 GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
3033 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL task BCK canceled\n");
3035 if (GNUNET_NO == c->was_removed)
3037 GNUNET_break (GNUNET_YES ==
3038 GNUNET_CONTAINER_multihashmap_remove (connections,
3042 GNUNET_STATISTICS_update (stats,
3047 GCC_check_connections ();
3052 * Get the connection ID.
3054 * @param c Connection to get the ID from.
3056 * @return ID of the connection.
3058 const struct GNUNET_CADET_Hash *
3059 GCC_get_id (const struct CadetConnection *c)
3066 * Get the connection ID.
3068 * @param c Connection to get the ID from.
3070 * @return ID of the connection.
3072 const struct GNUNET_HashCode *
3073 GCC_get_h (const struct CadetConnection *c)
3075 return GC_h2hc (&c->id);
3080 * Get the connection path.
3082 * @param c Connection to get the path from.
3084 * @return path used by the connection.
3086 const struct CadetPeerPath *
3087 GCC_get_path (const struct CadetConnection *c)
3089 if (GNUNET_NO == c->destroy)
3096 * Get the connection state.
3098 * @param c Connection to get the state from.
3100 * @return state of the connection.
3102 enum CadetConnectionState
3103 GCC_get_state (const struct CadetConnection *c)
3109 * Get the connection tunnel.
3111 * @param c Connection to get the tunnel from.
3113 * @return tunnel of the connection.
3115 struct CadetTunnel *
3116 GCC_get_tunnel (const struct CadetConnection *c)
3123 * Get free buffer space in a connection.
3125 * @param c Connection.
3126 * @param fwd Is query about FWD traffic?
3128 * @return Free buffer space [0 - max_msgs_queue/max_connections]
3131 GCC_get_buffer (struct CadetConnection *c, int fwd)
3133 struct CadetFlowControl *fc;
3135 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3137 LOG (GNUNET_ERROR_TYPE_DEBUG, " Get %s buffer on %s: %u - %u\n",
3138 GC_f2s (fwd), GCC_2s (c), fc->queue_max, fc->queue_n);
3139 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
3141 return (fc->queue_max - fc->queue_n);
3146 * Get how many messages have we allowed to send to us from a direction.
3148 * @param c Connection.
3149 * @param fwd Are we asking about traffic from FWD (BCK messages)?
3151 * @return last_ack_sent - last_pid_recv
3154 GCC_get_allowed (struct CadetConnection *c, int fwd)
3156 struct CadetFlowControl *fc;
3158 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3159 if (CADET_CONNECTION_READY != c->state
3160 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
3164 return (fc->last_ack_sent - fc->last_pid_recv);
3169 * Get messages queued in a connection.
3171 * @param c Connection.
3172 * @param fwd Is query about FWD traffic?
3174 * @return Number of messages queued.
3177 GCC_get_qn (struct CadetConnection *c, int fwd)
3179 struct CadetFlowControl *fc;
3181 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3188 * Get next PID to use.
3190 * @param c Connection.
3191 * @param fwd Is query about FWD traffic?
3193 * @return Last PID used + 1.
3196 GCC_get_pid (struct CadetConnection *c, int fwd)
3198 struct CadetFlowControl *fc;
3200 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3202 return fc->last_pid_sent + 1;
3207 * Allow the connection to advertise a buffer of the given size.
3209 * The connection will send an @c fwd ACK message (so: in direction !fwd)
3210 * allowing up to last_pid_recv + buffer.
3212 * @param c Connection.
3213 * @param buffer How many more messages the connection can accept.
3214 * @param fwd Is this about FWD traffic? (The ack will go dest->root).
3217 GCC_allow (struct CadetConnection *c, unsigned int buffer, int fwd)
3219 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowing %s %u messages %s\n",
3220 GCC_2s (c), buffer, GC_f2s (fwd));
3221 send_ack (c, buffer, fwd, GNUNET_NO);
3226 * Notify other peers on a connection of a broken link. Mark connections
3227 * to destroy after all traffic has been sent.
3229 * @param c Connection on which there has been a disconnection.
3230 * @param peer Peer that disconnected.
3233 GCC_notify_broken (struct CadetConnection *c,
3234 struct CadetPeer *peer)
3236 struct CadetPeer *hop;
3239 GCC_check_connections ();
3240 LOG (GNUNET_ERROR_TYPE_DEBUG,
3241 "Notify broken on %s due to %s disconnect\n",
3244 hop = get_prev_hop (c);
3247 /* Path was NULL, we should have deleted the connection. */
3251 fwd = (peer == hop);
3252 if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
3253 (GNUNET_YES == c->destroy) )
3255 /* Local shutdown, or other peer already down (hence 'c->destroy');
3256 so there is no one to notify about this, just clean up. */
3258 GCC_check_connections ();
3261 send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
3262 /* Connection will have at least one pending message
3263 * (the one we just scheduled), so delay destruction
3264 * and remove from map so we don't use accidentally. */
3265 c->destroy = GNUNET_YES;
3266 c->state = CADET_CONNECTION_DESTROYED;
3267 GNUNET_assert (GNUNET_NO == c->was_removed);
3268 c->was_removed = GNUNET_YES;
3269 GNUNET_break (GNUNET_YES ==
3270 GNUNET_CONTAINER_multihashmap_remove (connections,
3273 /* Cancel queue in the direction that just died. */
3274 connection_cancel_queues (c, ! fwd);
3275 unregister_neighbors (c);
3276 GCC_check_connections ();
3281 * Is this peer the first one on the connection?
3283 * @param c Connection.
3284 * @param fwd Is this about fwd traffic?
3286 * @return #GNUNET_YES if origin, #GNUNET_NO if relay/terminal.
3289 GCC_is_origin (struct CadetConnection *c, int fwd)
3291 if (!fwd && c->path->length - 1 == c->own_pos )
3293 if (fwd && 0 == c->own_pos)
3300 * Is this peer the last one on the connection?
3302 * @param c Connection.
3303 * @param fwd Is this about fwd traffic?
3304 * Note that the ROOT is the terminal for BCK traffic!
3306 * @return #GNUNET_YES if terminal, #GNUNET_NO if relay/origin.
3309 GCC_is_terminal (struct CadetConnection *c, int fwd)
3311 return GCC_is_origin (c, ! fwd);
3316 * See if we are allowed to send by the next hop in the given direction.
3318 * @param c Connection.
3319 * @param fwd Is this about fwd traffic?
3321 * @return #GNUNET_YES in case it's OK to send.
3324 GCC_is_sendable (struct CadetConnection *c, int fwd)
3326 struct CadetFlowControl *fc;
3328 LOG (GNUNET_ERROR_TYPE_DEBUG,
3329 " checking sendability of %s traffic on %s\n",
3330 GC_f2s (fwd), GCC_2s (c));
3336 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3337 LOG (GNUNET_ERROR_TYPE_DEBUG,
3338 " last ack recv: %u, last pid sent: %u\n",
3339 fc->last_ack_recv, fc->last_pid_sent);
3340 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
3342 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3345 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
3351 * Check if this connection is a direct one (never trim a direct connection).
3353 * @param c Connection.
3355 * @return #GNUNET_YES in case it's a direct connection, #GNUNET_NO otherwise.
3358 GCC_is_direct (struct CadetConnection *c)
3360 return (c->path->length == 2) ? GNUNET_YES : GNUNET_NO;
3365 * Sends an already built message on a connection, properly registering
3366 * all used resources.
3368 * @param message Message to send. Function makes a copy of it.
3369 * If message is not hop-by-hop, decrements TTL of copy.
3370 * @param payload_type Type of payload, in case the message is encrypted.
3371 * @param c Connection on which this message is transmitted.
3372 * @param fwd Is this a fwd message?
3373 * @param force Force the connection to accept the message (buffer overfill).
3374 * @param cont Continuation called once message is sent. Can be NULL.
3375 * @param cont_cls Closure for @c cont.
3377 * @return Handle to cancel the message before it's sent.
3378 * NULL on error or if @c cont is NULL.
3379 * Invalid on @c cont call.
3381 struct CadetConnectionQueue *
3382 GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3383 uint16_t payload_type, uint32_t payload_id,
3384 struct CadetConnection *c, int fwd, int force,
3385 GCC_sent cont, void *cont_cls)
3387 struct CadetFlowControl *fc;
3388 struct CadetConnectionQueue *q;
3394 GCC_check_connections ();
3395 size = ntohs (message->size);
3396 data = GNUNET_malloc (size);
3397 memcpy (data, message, size);
3398 type = ntohs (message->type);
3399 LOG (GNUNET_ERROR_TYPE_INFO, "--> %s (%s %4u) on connection %s (%u bytes)\n",
3400 GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), size);
3402 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3403 droppable = GNUNET_NO == force;
3406 struct GNUNET_CADET_AX *axmsg;
3407 struct GNUNET_CADET_Encrypted *emsg;
3408 struct GNUNET_CADET_KX *kmsg;
3409 struct GNUNET_CADET_ACK *amsg;
3410 struct GNUNET_CADET_Poll *pmsg;
3411 struct GNUNET_CADET_ConnectionDestroy *dmsg;
3412 struct GNUNET_CADET_ConnectionBroken *bmsg;
3415 case GNUNET_MESSAGE_TYPE_CADET_AX:
3416 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
3417 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
3419 emsg = (struct GNUNET_CADET_Encrypted *) data;
3420 ttl = ntohl (emsg->ttl);
3423 GNUNET_break_op (0);
3428 emsg->ttl = htonl (ttl - 1);
3432 axmsg = (struct GNUNET_CADET_AX *) data;
3435 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
3436 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
3437 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
3438 if (GNUNET_YES == droppable)
3444 LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
3448 case GNUNET_MESSAGE_TYPE_CADET_KX:
3449 kmsg = (struct GNUNET_CADET_KX *) data;
3453 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3454 amsg = (struct GNUNET_CADET_ACK *) data;
3456 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3457 droppable = GNUNET_NO;
3460 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3461 pmsg = (struct GNUNET_CADET_Poll *) data;
3463 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
3464 droppable = GNUNET_NO;
3467 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3468 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
3472 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3473 bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
3477 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3478 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
3487 if (fc->queue_n > fc->queue_max && droppable)
3489 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3492 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3493 fc->queue_n, fc->queue_max);
3494 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
3495 || GNUNET_MESSAGE_TYPE_CADET_AX == type)
3500 return NULL; /* Drop this message */
3503 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %s %u\n",
3504 GCC_2s (c), c->pending_messages);
3505 c->pending_messages++;
3507 q = GNUNET_new (struct CadetConnectionQueue);
3508 q->forced = !droppable;
3509 q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
3510 size, c, fwd, &conn_message_sent, q);
3513 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3516 GCC_check_connections ();
3520 q->cont_cls = cont_cls;
3521 GCC_check_connections ();
3522 return (NULL == cont) ? NULL : q;
3527 * Cancel a previously sent message while it's in the queue.
3529 * ONLY can be called before the continuation given to the send function
3530 * is called. Once the continuation is called, the message is no longer in the
3533 * @param q Handle to the queue.
3536 GCC_cancel (struct CadetConnectionQueue *q)
3538 LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
3540 /* queue destroy calls message_sent, which calls q->cont and frees q */
3541 GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
3542 GCC_check_connections ();
3547 * Sends a CREATE CONNECTION message for a path to a peer.
3548 * Changes the connection and tunnel states if necessary.
3550 * @param connection Connection to create.
3553 GCC_send_create (struct CadetConnection *connection)
3555 enum CadetTunnelCState state;
3558 GCC_check_connections ();
3559 size = sizeof (struct GNUNET_CADET_ConnectionCreate);
3560 size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
3562 LOG (GNUNET_ERROR_TYPE_INFO, "---> %s on connection %s (%u bytes)\n",
3563 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE),
3564 GCC_2s (connection), size);
3565 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
3566 connection, connection->pending_messages);
3567 connection->pending_messages++;
3569 connection->maintenance_q =
3570 GCP_queue_add (get_next_hop (connection), NULL,
3571 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 0,
3572 size, connection, GNUNET_YES, &conn_message_sent, NULL);
3574 state = GCT_get_cstate (connection->t);
3575 if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
3576 GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
3577 if (CADET_CONNECTION_NEW == connection->state)
3578 connection_change_state (connection, CADET_CONNECTION_SENT);
3579 GCC_check_connections ();
3584 * Send a message to all peers in this connection that the connection
3585 * is no longer valid.
3587 * If some peer should not receive the message, it should be zero'ed out
3588 * before calling this function.
3590 * @param c The connection whose peers to notify.
3593 GCC_send_destroy (struct CadetConnection *c)
3595 struct GNUNET_CADET_ConnectionDestroy msg;
3597 if (GNUNET_YES == c->destroy)
3599 GCC_check_connections ();
3600 msg.header.size = htons (sizeof (msg));
3601 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
3603 LOG (GNUNET_ERROR_TYPE_DEBUG,
3604 " sending connection destroy for connection %s\n",
3607 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3608 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3609 GNUNET_YES, GNUNET_YES,
3611 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3612 GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, 0, 0, c,
3613 GNUNET_NO, GNUNET_YES,
3615 c->destroy = GNUNET_YES;
3616 c->state = CADET_CONNECTION_DESTROYED;
3617 GCC_check_connections ();
3622 * @brief Start a polling timer for the connection.
3624 * When a neighbor does not accept more traffic on the connection it could be
3625 * caused by a simple congestion or by a lost ACK. Polling enables to check
3626 * for the lastest ACK status for a connection.
3628 * @param c Connection.
3629 * @param fwd Should we poll in the FWD direction?
3632 GCC_start_poll (struct CadetConnection *c, int fwd)
3634 struct CadetFlowControl *fc;
3636 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3637 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL %s requested\n",
3639 if (NULL != fc->poll_task || NULL != fc->poll_msg)
3641 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not needed (%p, %p)\n",
3642 fc->poll_task, fc->poll_msg);
3645 LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
3646 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3653 * @brief Stop polling a connection for ACKs.
3655 * Once we have enough ACKs for future traffic, polls are no longer necessary.
3657 * @param c Connection.
3658 * @param fwd Should we stop the poll in the FWD direction?
3661 GCC_stop_poll (struct CadetConnection *c, int fwd)
3663 struct CadetFlowControl *fc;
3665 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3666 if (NULL != fc->poll_task)
3668 GNUNET_SCHEDULER_cancel (fc->poll_task);
3669 fc->poll_task = NULL;
3675 * Get a (static) string for a connection.
3677 * @param c Connection.
3680 GCC_2s (const struct CadetConnection *c)
3687 static char buf[128];
3689 SPRINTF (buf, "%s (->%s)",
3690 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
3693 return GNUNET_h2s (GC_h2hc (&c->id));
3698 * Log all possible info about the connection state.
3700 * @param c Connection to debug.
3701 * @param level Debug level to use.
3704 GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3709 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3711 __FILE__, __FUNCTION__, __LINE__);
3717 LOG2 (level, "CCC DEBUG NULL CONNECTION\n");
3721 LOG2 (level, "CCC DEBUG CONNECTION %s\n", GCC_2s (c));
3722 s = path_2s (c->path);
3723 LOG2 (level, "CCC path %s, own pos: %u\n", s, c->own_pos);
3725 LOG2 (level, "CCC state: %s, destroy: %u\n",
3726 GCC_state2s (c->state), c->destroy);
3727 LOG2 (level, "CCC pending messages: %u\n", c->pending_messages);
3728 if (NULL != c->perf)
3729 LOG2 (level, "CCC us/byte: %f\n", c->perf->avg);
3731 LOG2 (level, "CCC FWD flow control:\n");
3732 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3733 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3734 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
3735 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3736 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
3737 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3738 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3739 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
3741 LOG2 (level, "CCC BCK flow control:\n");
3742 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3743 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3744 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
3745 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3746 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
3747 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3748 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3749 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
3751 LOG2 (level, "CCC DEBUG CONNECTION END\n");