2 This file is part of GNUnet.
3 Copyright (C) 2001-2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file cadet/gnunet-service-cadet_channel.c
20 * @brief logical links between CADET clients
21 * @author Bartlomiej Polot
22 * @author Christian Grothoff
25 * - Congestion/flow control:
26 * + estimate max bandwidth using bursts and use to for CONGESTION CONTROL!
27 * (and figure out how/where to use this!)
28 * + figure out flow control without ACKs (unreliable traffic!)
29 * - revisit handling of 'unbuffered' traffic!
30 * (need to push down through tunnel into connection selection)
31 * - revisit handling of 'buffered' traffic: 4 is a rather small buffer; maybe
32 * reserve more bits in 'options' to allow for buffer size control?
36 #include "gnunet_statistics_service.h"
37 #include "gnunet-service-cadet_channel.h"
38 #include "gnunet-service-cadet_connection.h"
39 #include "gnunet-service-cadet_tunnels.h"
40 #include "gnunet-service-cadet_paths.h"
42 #define LOG(level,...) GNUNET_log_from (level,"cadet-chn",__VA_ARGS__)
45 * How long do we initially wait before retransmitting?
47 #define CADET_INITIAL_RETRANSMIT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250)
50 * How long do we wait before dropping state about incoming
51 * connection to closed port?
53 #define TIMEOUT_CLOSED_PORT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
56 * How long do we wait at least before retransmitting ever?
58 #define MIN_RTT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 75)
61 * Maximum message ID into the future we accept for out-of-order messages.
62 * If the message is more than this into the future, we drop it. This is
63 * important both to detect values that are actually in the past, as well
64 * as to limit adversarially triggerable memory consumption.
66 * Note that right now we have "max_pending_messages = 4" hard-coded in
67 * the logic below, so a value of 4 would suffice here. But we plan to
68 * allow larger windows in the future...
70 #define MAX_OUT_OF_ORDER_DISTANCE 1024
74 * All the states a channel can be in.
76 enum CadetChannelState
79 * Uninitialized status, should never appear in operation.
84 * Channel is to a port that is not open, we're waiting for the
90 * CHANNEL_OPEN message sent, waiting for CHANNEL_OPEN_ACK.
92 CADET_CHANNEL_OPEN_SENT,
95 * Connection confirmed, ready to carry traffic.
102 * Info needed to retry a message in case it gets lost.
103 * Note that we DO use this structure also for unreliable
106 struct CadetReliableMessage
109 * Double linked list, FIFO style
111 struct CadetReliableMessage *next;
114 * Double linked list, FIFO style
116 struct CadetReliableMessage *prev;
119 * Which channel is this message in?
121 struct CadetChannel *ch;
124 * Entry in the tunnels queue for this message, NULL if it has left
125 * the tunnel. Used to cancel transmission in case we receive an
128 struct CadetTunnelQueueEntry *qe;
131 * Data message we are trying to send.
133 struct GNUNET_CADET_ChannelAppDataMessage *data_message;
136 * How soon should we retry if we fail to get an ACK?
137 * Messages in the queue are sorted by this value.
139 struct GNUNET_TIME_Absolute next_retry;
142 * How long do we wait for an ACK after transmission?
143 * Use for the back-off calculation.
145 struct GNUNET_TIME_Relative retry_delay;
148 * Time when we first successfully transmitted the message
149 * (that is, set @e num_transmissions to 1).
151 struct GNUNET_TIME_Absolute first_transmission_time;
154 * Identifier of the connection that this message took when it
155 * was first transmitted. Only useful if @e num_transmissions is 1.
157 struct GNUNET_CADET_ConnectionTunnelIdentifier connection_taken;
160 * How often was this message transmitted? #GNUNET_SYSERR if there
161 * was an error transmitting the message, #GNUNET_NO if it was not
162 * yet transmitted ever, otherwise the number of (re) transmissions.
164 int num_transmissions;
170 * List of received out-of-order data messages.
172 struct CadetOutOfOrderMessage
175 * Double linked list, FIFO style
177 struct CadetOutOfOrderMessage *next;
180 * Double linked list, FIFO style
182 struct CadetOutOfOrderMessage *prev;
185 * ID of the message (messages up to this point needed
186 * before we give this one to the client).
188 struct ChannelMessageIdentifier mid;
191 * The envelope with the payload of the out-of-order message
193 struct GNUNET_MQ_Envelope *env;
199 * Client endpoint of a `struct CadetChannel`. A channel may be a
200 * loopback channel, in which case it has two of these endpoints.
201 * Note that flow control also is required in both directions.
203 struct CadetChannelClient
206 * Client handle. Not by itself sufficient to designate
207 * the client endpoint, as the same client handle may
208 * be used for both the owner and the destination, and
209 * we thus also need the channel ID to identify the client.
211 struct CadetClient *c;
214 * Head of DLL of messages received out of order or while client was unready.
216 struct CadetOutOfOrderMessage *head_recv;
219 * Tail DLL of messages received out of order or while client was unready.
221 struct CadetOutOfOrderMessage *tail_recv;
224 * Local tunnel number for this client.
225 * (if owner >= #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI,
226 * otherwise < #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
228 struct GNUNET_CADET_ClientChannelNumber ccn;
231 * Number of entries currently in @a head_recv DLL.
233 unsigned int num_recv;
236 * Can we send data to the client?
244 * Struct containing all information regarding a channel to a remote client.
249 * Tunnel this channel is in.
251 struct CadetTunnel *t;
254 * Client owner of the tunnel, if any.
255 * (Used if this channel represends the initiating end of the tunnel.)
257 struct CadetChannelClient *owner;
260 * Client destination of the tunnel, if any.
261 * (Used if this channel represents the listening end of the tunnel.)
263 struct CadetChannelClient *dest;
266 * Last entry in the tunnel's queue relating to control messages
267 * (#GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN or
268 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK). Used to cancel
269 * transmission in case we receive updated information.
271 struct CadetTunnelQueueEntry *last_control_qe;
274 * Head of DLL of messages sent and not yet ACK'd.
276 struct CadetReliableMessage *head_sent;
279 * Tail of DLL of messages sent and not yet ACK'd.
281 struct CadetReliableMessage *tail_sent;
284 * Task to resend/poll in case no ACK is received.
286 struct GNUNET_SCHEDULER_Task *retry_control_task;
289 * Task to resend/poll in case no ACK is received.
291 struct GNUNET_SCHEDULER_Task *retry_data_task;
294 * Last time the channel was used
296 struct GNUNET_TIME_Absolute timestamp;
299 * Destination port of the channel.
301 struct GNUNET_HashCode port;
304 * Hash'ed port of the channel with initiator and destination PID.
306 struct GNUNET_HashCode h_port;
309 * Counter for exponential backoff.
311 struct GNUNET_TIME_Relative retry_time;
314 * Bitfield of already-received messages past @e mid_recv.
316 uint64_t mid_futures;
319 * Next MID expected for incoming traffic.
321 struct ChannelMessageIdentifier mid_recv;
324 * Next MID to use for outgoing traffic.
326 struct ChannelMessageIdentifier mid_send;
329 * Total (reliable) messages pending ACK for this channel.
331 unsigned int pending_messages;
334 * Maximum (reliable) messages pending ACK for this channel
335 * before we throttle the client.
337 unsigned int max_pending_messages;
340 * Number identifying this channel in its tunnel.
342 struct GNUNET_CADET_ChannelTunnelNumber ctn;
347 enum CadetChannelState state;
350 * Count how many ACKs we skipped, used to prevent long
351 * sequences of ACK skipping.
353 unsigned int skip_ack_series;
356 * Is the tunnel bufferless (minimum latency)?
361 * Is the tunnel reliable?
366 * Is the tunnel out-of-order?
371 * Is this channel a loopback channel, where the destination is us again?
376 * Flag to signal the destruction of the channel. If this is set to
377 * #GNUNET_YES the channel will be destroyed once the queue is
386 * Get the static string for identification of the channel.
390 * @return Static string with the channel IDs.
393 GCCH_2s (const struct CadetChannel *ch)
395 static char buf[128];
397 GNUNET_snprintf (buf,
399 "Channel %s:%s ctn:%X(%X/%X)",
400 (GNUNET_YES == ch->is_loopback)
402 : GNUNET_i2s (GCP_get_id (GCT_get_destination (ch->t))),
403 GNUNET_h2s (&ch->port),
405 (NULL == ch->owner) ? 0 : ntohl (ch->owner->ccn.channel_of_client),
406 (NULL == ch->dest) ? 0 : ntohl (ch->dest->ccn.channel_of_client));
412 * Hash the @a port and @a initiator and @a listener to
413 * calculate the "challenge" @a h_port we send to the other
414 * peer on #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN.
416 * @param[out] h_port set to the hash of @a port, @a initiator and @a listener
417 * @param port cadet port, as seen by CADET clients
418 * @param listener peer that is listining on @a port
421 GCCH_hash_port (struct GNUNET_HashCode *h_port,
422 const struct GNUNET_HashCode *port,
423 const struct GNUNET_PeerIdentity *listener)
425 struct GNUNET_HashContext *hc;
427 hc = GNUNET_CRYPTO_hash_context_start ();
428 GNUNET_CRYPTO_hash_context_read (hc,
431 GNUNET_CRYPTO_hash_context_read (hc,
434 GNUNET_CRYPTO_hash_context_finish (hc,
436 LOG (GNUNET_ERROR_TYPE_DEBUG,
437 "Calculated port hash %s\n",
438 GNUNET_h2s (h_port));
443 * Get the channel's public ID.
447 * @return ID used to identify the channel with the remote peer.
449 struct GNUNET_CADET_ChannelTunnelNumber
450 GCCH_get_id (const struct CadetChannel *ch)
457 * Release memory associated with @a ccc
459 * @param ccc data structure to clean up
462 free_channel_client (struct CadetChannelClient *ccc)
464 struct CadetOutOfOrderMessage *com;
466 while (NULL != (com = ccc->head_recv))
468 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
472 GNUNET_MQ_discard (com->env);
480 * Destroy the given channel.
482 * @param ch channel to destroy
485 channel_destroy (struct CadetChannel *ch)
487 struct CadetReliableMessage *crm;
489 while (NULL != (crm = ch->head_sent))
491 GNUNET_assert (ch == crm->ch);
494 GCT_send_cancel (crm->qe);
497 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
500 GNUNET_free (crm->data_message);
503 if (CADET_CHANNEL_LOOSE == ch->state)
505 GSC_drop_loose_channel (&ch->h_port,
508 if (NULL != ch->owner)
510 free_channel_client (ch->owner);
513 if (NULL != ch->dest)
515 free_channel_client (ch->dest);
518 if (NULL != ch->last_control_qe)
520 GCT_send_cancel (ch->last_control_qe);
521 ch->last_control_qe = NULL;
523 if (NULL != ch->retry_data_task)
525 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
526 ch->retry_data_task = NULL;
528 if (NULL != ch->retry_control_task)
530 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
531 ch->retry_control_task = NULL;
533 if (GNUNET_NO == ch->is_loopback)
535 GCT_remove_channel (ch->t,
545 * Send a channel create message.
547 * @param cls Channel for which to send.
550 send_channel_open (void *cls);
554 * Function called once the tunnel confirms that we sent the
555 * create message. Delays for a bit until we retry.
557 * @param cls our `struct CadetChannel`.
558 * @param cid identifier of the connection within the tunnel, NULL
559 * if transmission failed
562 channel_open_sent_cb (void *cls,
563 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
565 struct CadetChannel *ch = cls;
567 GNUNET_assert (NULL != ch->last_control_qe);
568 ch->last_control_qe = NULL;
569 ch->retry_time = GNUNET_TIME_STD_BACKOFF (ch->retry_time);
570 LOG (GNUNET_ERROR_TYPE_DEBUG,
571 "Sent CADET_CHANNEL_OPEN on %s, retrying in %s\n",
573 GNUNET_STRINGS_relative_time_to_string (ch->retry_time,
575 ch->retry_control_task
576 = GNUNET_SCHEDULER_add_delayed (ch->retry_time,
583 * Send a channel open message.
585 * @param cls Channel for which to send.
588 send_channel_open (void *cls)
590 struct CadetChannel *ch = cls;
591 struct GNUNET_CADET_ChannelOpenMessage msgcc;
594 ch->retry_control_task = NULL;
595 LOG (GNUNET_ERROR_TYPE_DEBUG,
596 "Sending CHANNEL_OPEN message for %s\n",
600 options |= GNUNET_CADET_OPTION_NOBUFFER;
602 options |= GNUNET_CADET_OPTION_RELIABLE;
603 if (ch->out_of_order)
604 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
605 msgcc.header.size = htons (sizeof (msgcc));
606 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
607 msgcc.opt = htonl (options);
608 msgcc.h_port = ch->h_port;
610 ch->state = CADET_CHANNEL_OPEN_SENT;
611 if (NULL != ch->last_control_qe)
612 GCT_send_cancel (ch->last_control_qe);
613 ch->last_control_qe = GCT_send (ch->t,
615 &channel_open_sent_cb,
617 GNUNET_assert (NULL == ch->retry_control_task);
622 * Function called once and only once after a channel was bound
623 * to its tunnel via #GCT_add_channel() is ready for transmission.
624 * Note that this is only the case for channels that this peer
625 * initiates, as for incoming channels we assume that they are
626 * ready for transmission immediately upon receiving the open
627 * message. Used to bootstrap the #GCT_send() process.
629 * @param ch the channel for which the tunnel is now ready
632 GCCH_tunnel_up (struct CadetChannel *ch)
634 GNUNET_assert (NULL == ch->retry_control_task);
635 LOG (GNUNET_ERROR_TYPE_DEBUG,
636 "Tunnel up, sending CHANNEL_OPEN on %s now\n",
638 ch->retry_control_task
639 = GNUNET_SCHEDULER_add_now (&send_channel_open,
645 * Create a new channel.
647 * @param owner local client owning the channel
648 * @param ccn local number of this channel at the @a owner
649 * @param destination peer to which we should build the channel
650 * @param port desired port at @a destination
651 * @param options options for the channel
652 * @return handle to the new channel
654 struct CadetChannel *
655 GCCH_channel_local_new (struct CadetClient *owner,
656 struct GNUNET_CADET_ClientChannelNumber ccn,
657 struct CadetPeer *destination,
658 const struct GNUNET_HashCode *port,
661 struct CadetChannel *ch;
662 struct CadetChannelClient *ccco;
664 ccco = GNUNET_new (struct CadetChannelClient);
667 ccco->client_ready = GNUNET_YES;
669 ch = GNUNET_new (struct CadetChannel);
670 ch->mid_recv.mid = htonl (1); /* The OPEN_ACK counts as message 0! */
671 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
672 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
673 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
674 ch->max_pending_messages = (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
677 GCCH_hash_port (&ch->h_port,
679 GCP_get_id (destination));
680 if (0 == memcmp (&my_full_id,
681 GCP_get_id (destination),
682 sizeof (struct GNUNET_PeerIdentity)))
686 ch->is_loopback = GNUNET_YES;
687 op = GNUNET_CONTAINER_multihashmap_get (open_ports,
691 /* port closed, wait for it to possibly open */
692 ch->state = CADET_CHANNEL_LOOSE;
693 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
696 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
697 LOG (GNUNET_ERROR_TYPE_DEBUG,
698 "Created loose incoming loopback channel to port %s\n",
699 GNUNET_h2s (&ch->port));
710 ch->t = GCP_get_tunnel (destination,
712 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
713 ch->ctn = GCT_add_channel (ch->t,
716 GNUNET_STATISTICS_update (stats,
720 LOG (GNUNET_ERROR_TYPE_DEBUG,
721 "Created channel to port %s at peer %s for %s using %s\n",
723 GCP_2s (destination),
725 (GNUNET_YES == ch->is_loopback) ? "loopback" : GCT_2s (ch->t));
731 * We had an incoming channel to a port that is closed.
732 * It has not been opened for a while, drop it.
734 * @param cls the channel to drop
737 timeout_closed_cb (void *cls)
739 struct CadetChannel *ch = cls;
741 ch->retry_control_task = NULL;
742 LOG (GNUNET_ERROR_TYPE_DEBUG,
743 "Closing incoming channel to port %s from peer %s due to timeout\n",
744 GNUNET_h2s (&ch->port),
745 GCP_2s (GCT_get_destination (ch->t)));
746 channel_destroy (ch);
751 * Create a new channel based on a request coming in over the network.
753 * @param t tunnel to the remote peer
754 * @param ctn identifier of this channel in the tunnel
755 * @param h_port desired hash of local port
756 * @param options options for the channel
757 * @return handle to the new channel
759 struct CadetChannel *
760 GCCH_channel_incoming_new (struct CadetTunnel *t,
761 struct GNUNET_CADET_ChannelTunnelNumber ctn,
762 const struct GNUNET_HashCode *h_port,
765 struct CadetChannel *ch;
768 ch = GNUNET_new (struct CadetChannel);
769 ch->h_port = *h_port;
772 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
773 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
774 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
775 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
776 ch->max_pending_messages = (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
777 GNUNET_STATISTICS_update (stats,
782 op = GNUNET_CONTAINER_multihashmap_get (open_ports,
786 /* port closed, wait for it to possibly open */
787 ch->state = CADET_CHANNEL_LOOSE;
788 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
791 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
792 GNUNET_assert (NULL == ch->retry_control_task);
793 ch->retry_control_task
794 = GNUNET_SCHEDULER_add_delayed (TIMEOUT_CLOSED_PORT,
797 LOG (GNUNET_ERROR_TYPE_DEBUG,
798 "Created loose incoming channel to port %s from peer %s\n",
799 GNUNET_h2s (&ch->port),
800 GCP_2s (GCT_get_destination (ch->t)));
808 GNUNET_STATISTICS_update (stats,
817 * Function called once the tunnel confirms that we sent the
818 * ACK message. Just remembers it was sent, we do not expect
821 * @param cls our `struct CadetChannel`.
822 * @param cid identifier of the connection within the tunnel, NULL
823 * if transmission failed
826 send_ack_cb (void *cls,
827 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
829 struct CadetChannel *ch = cls;
831 GNUNET_assert (NULL != ch->last_control_qe);
832 ch->last_control_qe = NULL;
837 * Compute and send the current #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK to the other peer.
839 * @param ch channel to send the #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK for
842 send_channel_data_ack (struct CadetChannel *ch)
844 struct GNUNET_CADET_ChannelDataAckMessage msg;
846 if (GNUNET_NO == ch->reliable)
847 return; /* no ACKs */
848 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK);
849 msg.header.size = htons (sizeof (msg));
851 msg.mid.mid = htonl (ntohl (ch->mid_recv.mid));
852 msg.futures = GNUNET_htonll (ch->mid_futures);
853 LOG (GNUNET_ERROR_TYPE_DEBUG,
854 "Sending DATA_ACK %u:%llX via %s\n",
855 (unsigned int) ntohl (msg.mid.mid),
856 (unsigned long long) ch->mid_futures,
858 if (NULL != ch->last_control_qe)
859 GCT_send_cancel (ch->last_control_qe);
860 ch->last_control_qe = GCT_send (ch->t,
868 * Send our initial #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK to the client confirming that the
871 * @param cls the `struct CadetChannel`
874 send_open_ack (void *cls)
876 struct CadetChannel *ch = cls;
877 struct GNUNET_CADET_ChannelOpenAckMessage msg;
879 ch->retry_control_task = NULL;
880 LOG (GNUNET_ERROR_TYPE_DEBUG,
881 "Sending CHANNEL_OPEN_ACK on %s\n",
883 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK);
884 msg.header.size = htons (sizeof (msg));
885 msg.reserved = htonl (0);
888 if (NULL != ch->last_control_qe)
889 GCT_send_cancel (ch->last_control_qe);
890 ch->last_control_qe = GCT_send (ch->t,
898 * We got a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN message again for
899 * this channel. If the binding was successful, (re)transmit the
900 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK.
902 * @param ch channel that got the duplicate open
903 * @param cti identifier of the connection that delivered the message
906 GCCH_handle_duplicate_open (struct CadetChannel *ch,
907 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
909 if (NULL == ch->dest)
911 LOG (GNUNET_ERROR_TYPE_DEBUG,
912 "Ignoring duplicate CHANNEL_OPEN on %s: port is closed\n",
916 if (NULL != ch->retry_control_task)
918 LOG (GNUNET_ERROR_TYPE_DEBUG,
919 "Ignoring duplicate CHANNEL_OPEN on %s: control message is pending\n",
923 LOG (GNUNET_ERROR_TYPE_DEBUG,
924 "Retransmitting CHANNEL_OPEN_ACK on %s\n",
926 ch->retry_control_task
927 = GNUNET_SCHEDULER_add_now (&send_open_ack,
933 * Send a #GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK to the client to solicit more messages.
935 * @param ch channel the ack is for
936 * @param to_owner #GNUNET_YES to send to owner,
937 * #GNUNET_NO to send to dest
940 send_ack_to_client (struct CadetChannel *ch,
943 struct GNUNET_MQ_Envelope *env;
944 struct GNUNET_CADET_LocalAck *ack;
945 struct CadetChannelClient *ccc;
947 ccc = (GNUNET_YES == to_owner) ? ch->owner : ch->dest;
950 /* This can happen if we are just getting ACKs after
951 our local client already disconnected. */
952 GNUNET_assert (GNUNET_YES == ch->destroy);
955 env = GNUNET_MQ_msg (ack,
956 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
958 LOG (GNUNET_ERROR_TYPE_DEBUG,
959 "Sending CADET_LOCAL_ACK to %s (%s) at ccn %X (%u/%u pending)\n",
961 (GNUNET_YES == to_owner) ? "owner" : "dest",
962 ntohl (ack->ccn.channel_of_client),
963 ch->pending_messages,
964 ch->max_pending_messages);
965 GSC_send_to_client (ccc->c,
971 * A client is bound to the port that we have a channel
972 * open to. Send the acknowledgement for the connection
973 * request and establish the link with the client.
975 * @param ch open incoming channel
976 * @param c client listening on the respective @a port
977 * @param port the port @a is listening on
980 GCCH_bind (struct CadetChannel *ch,
981 struct CadetClient *c,
982 const struct GNUNET_HashCode *port)
985 struct CadetChannelClient *cccd;
987 LOG (GNUNET_ERROR_TYPE_DEBUG,
988 "Binding %s from %s to port %s of %s\n",
991 GNUNET_h2s (&ch->port),
993 if (NULL != ch->retry_control_task)
995 /* there might be a timeout task here */
996 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
997 ch->retry_control_task = NULL;
1001 options |= GNUNET_CADET_OPTION_NOBUFFER;
1003 options |= GNUNET_CADET_OPTION_RELIABLE;
1004 if (ch->out_of_order)
1005 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
1006 cccd = GNUNET_new (struct CadetChannelClient);
1007 GNUNET_assert (NULL == ch->dest);
1011 cccd->client_ready = GNUNET_YES;
1012 cccd->ccn = GSC_bind (c,
1014 (GNUNET_YES == ch->is_loopback)
1015 ? GCP_get (&my_full_id,
1017 : GCT_get_destination (ch->t),
1020 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) <
1021 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1022 ch->mid_recv.mid = htonl (1); /* The OPEN counts as message 0! */
1023 if (GNUNET_YES == ch->is_loopback)
1025 ch->state = CADET_CHANNEL_OPEN_SENT;
1026 GCCH_handle_channel_open_ack (ch,
1032 /* notify other peer that we accepted the connection */
1033 ch->state = CADET_CHANNEL_READY;
1034 ch->retry_control_task
1035 = GNUNET_SCHEDULER_add_now (&send_open_ack,
1038 /* give client it's initial supply of ACKs */
1039 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) <
1040 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1041 for (unsigned int i=0;i<ch->max_pending_messages;i++)
1042 send_ack_to_client (ch,
1048 * One of our clients has disconnected, tell the other one that we
1049 * are finished. Done asynchronously to avoid concurrent modification
1050 * issues if this is the same client.
1052 * @param cls the `struct CadetChannel` where one of the ends is now dead
1055 signal_remote_destroy_cb (void *cls)
1057 struct CadetChannel *ch = cls;
1058 struct CadetChannelClient *ccc;
1060 /* Find which end is left... */
1061 ch->retry_control_task = NULL;
1062 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1063 GSC_handle_remote_channel_destroy (ccc->c,
1066 channel_destroy (ch);
1071 * Destroy locally created channel. Called by the local client, so no
1072 * need to tell the client.
1074 * @param ch channel to destroy
1075 * @param c client that caused the destruction
1076 * @param ccn client number of the client @a c
1079 GCCH_channel_local_destroy (struct CadetChannel *ch,
1080 struct CadetClient *c,
1081 struct GNUNET_CADET_ClientChannelNumber ccn)
1083 LOG (GNUNET_ERROR_TYPE_DEBUG,
1084 "%s asks for destruction of %s\n",
1087 GNUNET_assert (NULL != c);
1088 if ( (NULL != ch->owner) &&
1089 (c == ch->owner->c) &&
1090 (ccn.channel_of_client == ch->owner->ccn.channel_of_client) )
1092 free_channel_client (ch->owner);
1095 else if ( (NULL != ch->dest) &&
1096 (c == ch->dest->c) &&
1097 (ccn.channel_of_client == ch->dest->ccn.channel_of_client) )
1099 free_channel_client (ch->dest);
1107 if (GNUNET_YES == ch->destroy)
1109 /* other end already destroyed, with the local client gone, no need
1110 to finish transmissions, just destroy immediately. */
1111 channel_destroy (ch);
1114 if ( (NULL != ch->head_sent) &&
1115 ( (NULL != ch->owner) ||
1116 (NULL != ch->dest) ) )
1118 /* Wait for other end to destroy us as well,
1119 and otherwise allow send queue to be transmitted first */
1120 ch->destroy = GNUNET_YES;
1123 if ( (GNUNET_YES == ch->is_loopback) &&
1124 ( (NULL != ch->owner) ||
1125 (NULL != ch->dest) ) )
1127 if (NULL != ch->retry_control_task)
1128 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
1129 ch->retry_control_task
1130 = GNUNET_SCHEDULER_add_now (&signal_remote_destroy_cb,
1134 if (GNUNET_NO == ch->is_loopback)
1136 /* If the we ever sent the CHANNEL_CREATE, we need to send a destroy message. */
1139 case CADET_CHANNEL_NEW:
1140 /* We gave up on a channel that we created as a client to a remote
1141 target, but that never went anywhere. Nothing to do here. */
1143 case CADET_CHANNEL_LOOSE:
1146 GCT_send_channel_destroy (ch->t,
1150 /* Nothing left to do, just finish destruction */
1151 channel_destroy (ch);
1156 * We got an acknowledgement for the creation of the channel
1157 * (the port is open on the other side). Verify that the
1158 * other end really has the right port, and begin transmissions.
1160 * @param ch channel to destroy
1161 * @param cti identifier of the connection that delivered the message
1162 * @param port port number (needed to verify receiver knows the port)
1165 GCCH_handle_channel_open_ack (struct CadetChannel *ch,
1166 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1167 const struct GNUNET_HashCode *port)
1171 case CADET_CHANNEL_NEW:
1172 /* this should be impossible */
1175 case CADET_CHANNEL_LOOSE:
1176 /* This makes no sense. */
1177 GNUNET_break_op (0);
1179 case CADET_CHANNEL_OPEN_SENT:
1180 if (NULL == ch->owner)
1182 /* We're not the owner, wrong direction! */
1183 GNUNET_break_op (0);
1186 if (0 != memcmp (&ch->port,
1188 sizeof (struct GNUNET_HashCode)))
1190 /* Other peer failed to provide the right port,
1191 refuse connection. */
1192 GNUNET_break_op (0);
1195 LOG (GNUNET_ERROR_TYPE_DEBUG,
1196 "Received CHANNEL_OPEN_ACK for waiting %s, entering READY state\n",
1198 if (NULL != ch->retry_control_task) /* can be NULL if ch->is_loopback */
1200 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
1201 ch->retry_control_task = NULL;
1203 ch->state = CADET_CHANNEL_READY;
1204 /* On first connect, send client as many ACKs as we allow messages
1206 for (unsigned int i=0;i<ch->max_pending_messages;i++)
1207 send_ack_to_client (ch,
1210 case CADET_CHANNEL_READY:
1211 /* duplicate ACK, maybe we retried the CREATE. Ignore. */
1212 LOG (GNUNET_ERROR_TYPE_DEBUG,
1213 "Received duplicate channel OPEN_ACK for %s\n",
1215 GNUNET_STATISTICS_update (stats,
1216 "# duplicate CREATE_ACKs",
1225 * Test if element @a e1 comes before element @a e2.
1227 * @param cls closure, to a flag where we indicate duplicate packets
1228 * @param m1 a message of to sort
1229 * @param m2 another message to sort
1230 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1233 is_before (void *cls,
1234 struct CadetOutOfOrderMessage *m1,
1235 struct CadetOutOfOrderMessage *m2)
1237 int *duplicate = cls;
1238 uint32_t v1 = ntohl (m1->mid.mid);
1239 uint32_t v2 = ntohl (m2->mid.mid);
1244 *duplicate = GNUNET_YES;
1245 if (delta > (uint32_t) INT_MAX)
1247 /* in overflow range, we can safely assume we wrapped around */
1252 /* result is small, thus v2 > v1, thus m1 < m2 */
1259 * We got payload data for a channel. Pass it on to the client
1260 * and send an ACK to the other end (once flow control allows it!)
1262 * @param ch channel that got data
1263 * @param cti identifier of the connection that delivered the message
1264 * @param msg message that was received
1267 GCCH_handle_channel_plaintext_data (struct CadetChannel *ch,
1268 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1269 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
1271 struct GNUNET_MQ_Envelope *env;
1272 struct GNUNET_CADET_LocalData *ld;
1273 struct CadetChannelClient *ccc;
1274 size_t payload_size;
1275 struct CadetOutOfOrderMessage *com;
1282 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1283 if ( (NULL == ch->owner) &&
1284 (NULL == ch->dest) )
1286 /* This client is gone, but we still have messages to send to
1287 the other end (which is why @a ch is not yet dead). However,
1288 we cannot pass messages to our client anymore. */
1289 LOG (GNUNET_ERROR_TYPE_DEBUG,
1290 "Dropping incoming payload on %s as this end is already closed\n",
1292 /* send back DESTROY notification to stop further retransmissions! */
1293 if (GNUNET_YES == ch->destroy)
1294 GCT_send_channel_destroy (ch->t,
1298 payload_size = ntohs (msg->header.size) - sizeof (*msg);
1299 env = GNUNET_MQ_msg_extra (ld,
1301 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1302 ld->ccn = (NULL == ch->dest) ? ch->owner->ccn : ch->dest->ccn;
1303 GNUNET_memcpy (&ld[1],
1306 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1307 if ( (GNUNET_YES == ccc->client_ready) &&
1308 ( (GNUNET_YES == ch->out_of_order) ||
1309 (msg->mid.mid == ch->mid_recv.mid) ) )
1311 LOG (GNUNET_ERROR_TYPE_DEBUG,
1312 "Giving %u bytes of payload with MID %u from %s to client %s\n",
1313 (unsigned int) payload_size,
1314 ntohl (msg->mid.mid),
1317 ccc->client_ready = GNUNET_NO;
1318 GSC_send_to_client (ccc->c,
1320 ch->mid_recv.mid = htonl (1 + ntohl (ch->mid_recv.mid));
1321 ch->mid_futures >>= 1;
1322 send_channel_data_ack (ch);
1326 if (GNUNET_YES == ch->reliable)
1328 /* check if message ought to be dropped because it is ancient/too distant/duplicate */
1329 mid_min = ntohl (ch->mid_recv.mid);
1330 mid_max = mid_min + ch->max_pending_messages;
1331 mid_msg = ntohl (msg->mid.mid);
1332 if ( ( (uint32_t) (mid_msg - mid_min) > ch->max_pending_messages) ||
1333 ( (uint32_t) (mid_max - mid_msg) > ch->max_pending_messages) )
1335 LOG (GNUNET_ERROR_TYPE_DEBUG,
1336 "%s at %u drops ancient or far-future message %u\n",
1338 (unsigned int) mid_min,
1339 ntohl (msg->mid.mid));
1341 GNUNET_STATISTICS_update (stats,
1342 "# duplicate DATA (ancient or future)",
1345 GNUNET_MQ_discard (env);
1346 send_channel_data_ack (ch);
1349 /* mark bit for future ACKs */
1350 delta = mid_msg - mid_min - 1; /* overflow/underflow are OK here */
1353 if (0 != (ch->mid_futures & (1LLU << delta)))
1355 /* Duplicate within the queue, drop also */
1356 LOG (GNUNET_ERROR_TYPE_DEBUG,
1357 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1358 (unsigned int) payload_size,
1360 ntohl (msg->mid.mid));
1361 GNUNET_STATISTICS_update (stats,
1365 GNUNET_MQ_discard (env);
1366 send_channel_data_ack (ch);
1369 ch->mid_futures |= (1LLU << delta);
1370 LOG (GNUNET_ERROR_TYPE_DEBUG,
1371 "Marked bit %llX for mid %u (base: %u); now: %llX\n",
1378 else /* ! ch->reliable */
1380 /* Channel is unreliable, so we do not ACK. But we also cannot
1381 allow buffering everything, so check if we have space... */
1382 if (ccc->num_recv >= ch->max_pending_messages)
1384 struct CadetOutOfOrderMessage *drop;
1386 /* Yep, need to drop. Drop the oldest message in
1388 LOG (GNUNET_ERROR_TYPE_DEBUG,
1389 "Queue full due slow client on %s, dropping oldest message\n",
1391 GNUNET_STATISTICS_update (stats,
1392 "# messages dropped due to slow client",
1395 drop = ccc->head_recv;
1396 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1400 GNUNET_MQ_discard (drop->env);
1405 /* Insert message into sorted out-of-order queue */
1406 com = GNUNET_new (struct CadetOutOfOrderMessage);
1407 com->mid = msg->mid;
1409 duplicate = GNUNET_NO;
1410 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetOutOfOrderMessage,
1417 if (GNUNET_YES == duplicate)
1419 /* Duplicate within the queue, drop also (this is not covered by
1420 the case above if "delta" >= 64, which could be the case if
1421 max_pending_messages is also >= 64 or if our client is unready
1422 and we are seeing retransmissions of the message our client is
1424 LOG (GNUNET_ERROR_TYPE_DEBUG,
1425 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1426 (unsigned int) payload_size,
1428 ntohl (msg->mid.mid));
1429 GNUNET_STATISTICS_update (stats,
1433 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1437 GNUNET_MQ_discard (com->env);
1439 send_channel_data_ack (ch);
1442 LOG (GNUNET_ERROR_TYPE_DEBUG,
1443 "Queued %s payload of %u bytes on %s-%X(%p) (mid %u, need %u first)\n",
1444 (GNUNET_YES == ccc->client_ready)
1446 : "client-not-ready",
1447 (unsigned int) payload_size,
1449 ntohl (ccc->ccn.channel_of_client),
1451 ntohl (msg->mid.mid),
1452 ntohl (ch->mid_recv.mid));
1453 /* NOTE: this ACK we _could_ skip, as the packet is out-of-order and
1454 the sender may already be transmitting the previous one. Needs
1455 experimental evaluation to see if/when this ACK helps or
1456 hurts. (We might even want another option.) */
1457 send_channel_data_ack (ch);
1462 * Function called once the tunnel has sent one of our messages.
1463 * If the message is unreliable, simply frees the `crm`. If the
1464 * message was reliable, calculate retransmission time and
1465 * wait for ACK (or retransmit).
1467 * @param cls the `struct CadetReliableMessage` that was sent
1468 * @param cid identifier of the connection within the tunnel, NULL
1469 * if transmission failed
1472 data_sent_cb (void *cls,
1473 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
1477 * We need to retry a transmission, the last one took too long to
1480 * @param cls the `struct CadetChannel` where we need to retransmit
1483 retry_transmission (void *cls)
1485 struct CadetChannel *ch = cls;
1486 struct CadetReliableMessage *crm = ch->head_sent;
1488 ch->retry_data_task = NULL;
1489 GNUNET_assert (NULL == crm->qe);
1490 LOG (GNUNET_ERROR_TYPE_DEBUG,
1491 "Retrying transmission on %s of message %u\n",
1493 (unsigned int) ntohl (crm->data_message->mid.mid));
1494 crm->qe = GCT_send (ch->t,
1495 &crm->data_message->header,
1498 GNUNET_assert (NULL == ch->retry_data_task);
1503 * We got an PLAINTEXT_DATA_ACK for a message in our queue, remove it from
1504 * the queue and tell our client that it can send more.
1506 * @param ch the channel that got the PLAINTEXT_DATA_ACK
1507 * @param cti identifier of the connection that delivered the message
1508 * @param crm the message that got acknowledged
1511 handle_matching_ack (struct CadetChannel *ch,
1512 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1513 struct CadetReliableMessage *crm)
1515 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1518 ch->pending_messages--;
1519 GNUNET_assert (ch->pending_messages < ch->max_pending_messages);
1520 LOG (GNUNET_ERROR_TYPE_DEBUG,
1521 "Received DATA_ACK on %s for message %u (%u ACKs pending)\n",
1523 (unsigned int) ntohl (crm->data_message->mid.mid),
1524 ch->pending_messages);
1525 if (NULL != crm->qe)
1527 GCT_send_cancel (crm->qe);
1530 if ( (1 == crm->num_transmissions) &&
1533 GCC_ack_observed (cti);
1534 if (0 == memcmp (cti,
1535 &crm->connection_taken,
1536 sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier)))
1538 GCC_latency_observed (cti,
1539 GNUNET_TIME_absolute_get_duration (crm->first_transmission_time));
1542 GNUNET_free (crm->data_message);
1544 send_ack_to_client (ch,
1552 * We got an acknowledgement for payload data for a channel.
1553 * Possibly resume transmissions.
1555 * @param ch channel that got the ack
1556 * @param cti identifier of the connection that delivered the message
1557 * @param ack details about what was received
1560 GCCH_handle_channel_plaintext_data_ack (struct CadetChannel *ch,
1561 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1562 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
1564 struct CadetReliableMessage *crm;
1565 struct CadetReliableMessage *crmn;
1571 GNUNET_break (GNUNET_NO == ch->is_loopback);
1572 if (GNUNET_NO == ch->reliable)
1574 /* not expecting ACKs on unreliable channel, odd */
1575 GNUNET_break_op (0);
1578 /* mid_base is the MID of the next message that the
1579 other peer expects (i.e. that is missing!), everything
1580 LOWER (but excluding mid_base itself) was received. */
1581 mid_base = ntohl (ack->mid.mid);
1582 mid_mask = GNUNET_htonll (ack->futures);
1584 for (crm = ch->head_sent;
1589 delta = (unsigned int) (ntohl (crm->data_message->mid.mid) - mid_base);
1590 if (delta >= UINT_MAX - ch->max_pending_messages)
1592 /* overflow, means crm was a bit in the past, so this ACK counts for it. */
1593 LOG (GNUNET_ERROR_TYPE_DEBUG,
1594 "Got DATA_ACK with base %u satisfying past message %u on %s\n",
1595 (unsigned int) mid_base,
1596 ntohl (crm->data_message->mid.mid),
1598 handle_matching_ack (ch,
1607 LOG (GNUNET_ERROR_TYPE_DEBUG,
1608 "Testing bit %llX for mid %u (base: %u)\n",
1610 ntohl (crm->data_message->mid.mid),
1612 if (0 != (mid_mask & (1LLU << delta)))
1614 LOG (GNUNET_ERROR_TYPE_DEBUG,
1615 "Got DATA_ACK with mask for %u on %s\n",
1616 ntohl (crm->data_message->mid.mid),
1618 handle_matching_ack (ch,
1624 if (GNUNET_NO == found)
1626 /* ACK for message we already dropped, might have been a
1627 duplicate ACK? Ignore. */
1628 LOG (GNUNET_ERROR_TYPE_DEBUG,
1629 "Duplicate DATA_ACK on %s, ignoring\n",
1631 GNUNET_STATISTICS_update (stats,
1632 "# duplicate DATA_ACKs",
1637 if (NULL != ch->retry_data_task)
1639 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1640 ch->retry_data_task = NULL;
1642 if ( (NULL != ch->head_sent) &&
1643 (NULL == ch->head_sent->qe) )
1645 = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry,
1646 &retry_transmission,
1652 * Destroy channel, based on the other peer closing the
1653 * connection. Also needs to remove this channel from
1656 * @param ch channel to destroy
1657 * @param cti identifier of the connection that delivered the message,
1658 * NULL if we are simulating receiving a destroy due to shutdown
1661 GCCH_handle_remote_destroy (struct CadetChannel *ch,
1662 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
1664 struct CadetChannelClient *ccc;
1666 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1667 LOG (GNUNET_ERROR_TYPE_DEBUG,
1668 "Received remote channel DESTROY for %s\n",
1670 if (GNUNET_YES == ch->destroy)
1672 /* Local client already gone, this is instant-death. */
1673 channel_destroy (ch);
1676 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1677 if ( (NULL != ccc) &&
1678 (NULL != ccc->head_recv) )
1680 LOG (GNUNET_ERROR_TYPE_WARNING,
1681 "Lost end of transmission due to remote shutdown on %s\n",
1683 /* FIXME: change API to notify client about truncated transmission! */
1685 ch->destroy = GNUNET_YES;
1687 GSC_handle_remote_channel_destroy (ccc->c,
1690 channel_destroy (ch);
1695 * Test if element @a e1 comes before element @a e2.
1697 * @param cls closure, to a flag where we indicate duplicate packets
1698 * @param crm1 an element of to sort
1699 * @param crm2 another element to sort
1700 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1703 cmp_crm_by_next_retry (void *cls,
1704 struct CadetReliableMessage *crm1,
1705 struct CadetReliableMessage *crm2)
1707 if (crm1->next_retry.abs_value_us <
1708 crm2->next_retry.abs_value_us)
1715 * Function called once the tunnel has sent one of our messages.
1716 * If the message is unreliable, simply frees the `crm`. If the
1717 * message was reliable, calculate retransmission time and
1718 * wait for ACK (or retransmit).
1720 * @param cls the `struct CadetReliableMessage` that was sent
1721 * @param cid identifier of the connection within the tunnel, NULL
1722 * if transmission failed
1725 data_sent_cb (void *cls,
1726 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
1728 struct CadetReliableMessage *crm = cls;
1729 struct CadetChannel *ch = crm->ch;
1731 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1732 GNUNET_assert (NULL != crm->qe);
1734 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1737 if (GNUNET_NO == ch->reliable)
1739 GNUNET_free (crm->data_message);
1741 ch->pending_messages--;
1742 send_ack_to_client (ch,
1750 /* There was an error sending. */
1751 crm->num_transmissions = GNUNET_SYSERR;
1753 else if (GNUNET_SYSERR != crm->num_transmissions)
1755 /* Increment transmission counter, and possibly store @a cid
1756 if this was the first transmission. */
1757 crm->num_transmissions++;
1758 if (1 == crm->num_transmissions)
1760 crm->first_transmission_time = GNUNET_TIME_absolute_get ();
1761 crm->connection_taken = *cid;
1762 GCC_ack_expected (cid);
1765 if ( (0 == crm->retry_delay.rel_value_us) &&
1768 struct CadetConnection *cc = GCC_lookup (cid);
1771 crm->retry_delay = GCC_get_metrics (cc)->aged_latency;
1773 crm->retry_delay = ch->retry_time;
1775 crm->retry_delay = GNUNET_TIME_STD_BACKOFF (crm->retry_delay);
1776 crm->retry_delay = GNUNET_TIME_relative_max (crm->retry_delay,
1778 crm->next_retry = GNUNET_TIME_relative_to_absolute (crm->retry_delay);
1780 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetReliableMessage,
1781 cmp_crm_by_next_retry,
1786 LOG (GNUNET_ERROR_TYPE_DEBUG,
1787 "Message %u sent, next transmission on %s in %s\n",
1788 (unsigned int) ntohl (crm->data_message->mid.mid),
1790 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (ch->head_sent->next_retry),
1792 if (NULL == ch->head_sent->qe)
1794 if (NULL != ch->retry_data_task)
1795 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1797 = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry,
1798 &retry_transmission,
1805 * Handle data given by a client.
1807 * Check whether the client is allowed to send in this tunnel, save if
1808 * channel is reliable and send an ACK to the client if there is still
1809 * buffer space in the tunnel.
1811 * @param ch Channel.
1812 * @param sender_ccn ccn of the sender
1813 * @param buf payload to transmit.
1814 * @param buf_len number of bytes in @a buf
1815 * @return #GNUNET_OK if everything goes well,
1816 * #GNUNET_SYSERR in case of an error.
1819 GCCH_handle_local_data (struct CadetChannel *ch,
1820 struct GNUNET_CADET_ClientChannelNumber sender_ccn,
1824 struct CadetReliableMessage *crm;
1826 if (ch->pending_messages >= ch->max_pending_messages)
1829 return GNUNET_SYSERR;
1831 if (GNUNET_YES == ch->destroy)
1833 /* we are going down, drop messages */
1836 ch->pending_messages++;
1838 if (GNUNET_YES == ch->is_loopback)
1840 struct CadetChannelClient *receiver;
1841 struct GNUNET_MQ_Envelope *env;
1842 struct GNUNET_CADET_LocalData *ld;
1845 env = GNUNET_MQ_msg_extra (ld,
1847 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1848 if ( (NULL != ch->owner) &&
1849 (sender_ccn.channel_of_client ==
1850 ch->owner->ccn.channel_of_client) )
1852 receiver = ch->dest;
1853 ack_to_owner = GNUNET_YES;
1855 else if ( (NULL != ch->dest) &&
1856 (sender_ccn.channel_of_client ==
1857 ch->dest->ccn.channel_of_client) )
1859 receiver = ch->owner;
1860 ack_to_owner = GNUNET_NO;
1865 return GNUNET_SYSERR;
1867 GNUNET_assert (NULL != receiver);
1868 ld->ccn = receiver->ccn;
1869 GNUNET_memcpy (&ld[1],
1872 if (GNUNET_YES == receiver->client_ready)
1874 ch->pending_messages--;
1875 GSC_send_to_client (receiver->c,
1877 send_ack_to_client (ch,
1882 struct CadetOutOfOrderMessage *oom;
1884 oom = GNUNET_new (struct CadetOutOfOrderMessage);
1886 GNUNET_CONTAINER_DLL_insert_tail (receiver->head_recv,
1887 receiver->tail_recv,
1889 receiver->num_recv++;
1894 /* Everything is correct, send the message. */
1895 crm = GNUNET_malloc (sizeof (*crm));
1897 crm->data_message = GNUNET_malloc (sizeof (struct GNUNET_CADET_ChannelAppDataMessage)
1899 crm->data_message->header.size = htons (sizeof (struct GNUNET_CADET_ChannelAppDataMessage) + buf_len);
1900 crm->data_message->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA);
1901 ch->mid_send.mid = htonl (ntohl (ch->mid_send.mid) + 1);
1902 crm->data_message->mid = ch->mid_send;
1903 crm->data_message->ctn = ch->ctn;
1904 GNUNET_memcpy (&crm->data_message[1],
1907 GNUNET_CONTAINER_DLL_insert_tail (ch->head_sent,
1910 LOG (GNUNET_ERROR_TYPE_DEBUG,
1911 "Sending message %u from local client to %s with %u bytes\n",
1912 ntohl (crm->data_message->mid.mid),
1915 if (NULL != ch->retry_data_task)
1917 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1918 ch->retry_data_task = NULL;
1920 crm->qe = GCT_send (ch->t,
1921 &crm->data_message->header,
1924 GNUNET_assert (NULL == ch->retry_data_task);
1930 * Handle ACK from client on local channel. Means the client is ready
1931 * for more data, see if we have any for it.
1933 * @param ch channel to destroy
1934 * @param client_ccn ccn of the client sending the ack
1937 GCCH_handle_local_ack (struct CadetChannel *ch,
1938 struct GNUNET_CADET_ClientChannelNumber client_ccn)
1940 struct CadetChannelClient *ccc;
1941 struct CadetOutOfOrderMessage *com;
1943 if ( (NULL != ch->owner) &&
1944 (ch->owner->ccn.channel_of_client == client_ccn.channel_of_client) )
1946 else if ( (NULL != ch->dest) &&
1947 (ch->dest->ccn.channel_of_client == client_ccn.channel_of_client) )
1951 ccc->client_ready = GNUNET_YES;
1952 com = ccc->head_recv;
1955 LOG (GNUNET_ERROR_TYPE_DEBUG,
1956 "Got LOCAL_ACK, %s-%X ready to receive more data, but none pending on %s-%X(%p)!\n",
1958 ntohl (client_ccn.channel_of_client),
1960 ntohl (ccc->ccn.channel_of_client),
1962 return; /* none pending */
1964 if (GNUNET_YES == ch->is_loopback)
1968 /* Messages are always in-order, just send */
1969 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1973 GSC_send_to_client (ccc->c,
1975 /* Notify sender that we can receive more */
1976 if ( (NULL != ch->owner) &&
1977 (ccc->ccn.channel_of_client ==
1978 ch->owner->ccn.channel_of_client) )
1980 to_owner = GNUNET_NO;
1984 GNUNET_assert ( (NULL != ch->dest) &&
1985 (ccc->ccn.channel_of_client ==
1986 ch->dest->ccn.channel_of_client) );
1987 to_owner = GNUNET_YES;
1989 send_ack_to_client (ch,
1995 if ( (com->mid.mid != ch->mid_recv.mid) &&
1996 (GNUNET_NO == ch->out_of_order) &&
1997 (GNUNET_YES == ch->reliable) )
1999 LOG (GNUNET_ERROR_TYPE_DEBUG,
2000 "Got LOCAL_ACK, %s-%X ready to receive more data (but next one is out-of-order %u vs. %u)!\n",
2002 ntohl (ccc->ccn.channel_of_client),
2003 ntohl (com->mid.mid),
2004 ntohl (ch->mid_recv.mid));
2005 return; /* missing next one in-order */
2008 LOG (GNUNET_ERROR_TYPE_DEBUG,
2009 "Got LOCAL_ACK, giving payload message %u to %s-%X on %s\n",
2010 ntohl (com->mid.mid),
2012 ntohl (ccc->ccn.channel_of_client),
2015 /* all good, pass next message to client */
2016 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
2020 /* FIXME: if unreliable, this is not aggressive
2021 enough, as it would be OK to have lost some! */
2023 ch->mid_recv.mid = htonl (1 + ntohl (com->mid.mid));
2024 ch->mid_futures >>= 1; /* equivalent to division by 2 */
2025 ccc->client_ready = GNUNET_NO;
2026 GSC_send_to_client (ccc->c,
2029 send_channel_data_ack (ch);
2030 if (NULL != ccc->head_recv)
2032 if (GNUNET_NO == ch->destroy)
2034 GCT_send_channel_destroy (ch->t,
2036 channel_destroy (ch);
2040 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-chn",__VA_ARGS__)
2046 * @param ch Channel.
2047 * @param level Debug level to use.
2050 GCCH_debug (struct CadetChannel *ch,
2051 enum GNUNET_ErrorType level)
2053 #if !defined(GNUNET_CULL_LOGGING)
2056 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
2058 __FILE__, __FUNCTION__, __LINE__);
2064 LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
2072 if (NULL != ch->owner)
2075 "CHN origin %s ready %s local-id: %u\n",
2076 GSC_2s (ch->owner->c),
2077 ch->owner->client_ready ? "YES" : "NO",
2078 ntohl (ch->owner->ccn.channel_of_client));
2080 if (NULL != ch->dest)
2083 "CHN destination %s ready %s local-id: %u\n",
2084 GSC_2s (ch->dest->c),
2085 ch->dest->client_ready ? "YES" : "NO",
2086 ntohl (ch->dest->ccn.channel_of_client));
2089 "CHN Message IDs recv: %d (%LLX), send: %d\n",
2090 ntohl (ch->mid_recv.mid),
2091 (unsigned long long) ch->mid_futures,
2092 ntohl (ch->mid_send.mid));
2098 /* end of gnunet-service-cadet-new_channel.c */