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 (NULL != ch->owner)
505 free_channel_client (ch->owner);
508 if (NULL != ch->dest)
510 free_channel_client (ch->dest);
513 if (NULL != ch->last_control_qe)
515 GCT_send_cancel (ch->last_control_qe);
516 ch->last_control_qe = NULL;
518 if (NULL != ch->retry_data_task)
520 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
521 ch->retry_data_task = NULL;
523 if (NULL != ch->retry_control_task)
525 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
526 ch->retry_control_task = NULL;
528 if (GNUNET_NO == ch->is_loopback)
530 GCT_remove_channel (ch->t,
540 * Send a channel create message.
542 * @param cls Channel for which to send.
545 send_channel_open (void *cls);
549 * Function called once the tunnel confirms that we sent the
550 * create message. Delays for a bit until we retry.
552 * @param cls our `struct CadetChannel`.
553 * @param cid identifier of the connection within the tunnel, NULL
554 * if transmission failed
557 channel_open_sent_cb (void *cls,
558 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
560 struct CadetChannel *ch = cls;
562 GNUNET_assert (NULL != ch->last_control_qe);
563 ch->last_control_qe = NULL;
564 ch->retry_time = GNUNET_TIME_STD_BACKOFF (ch->retry_time);
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
566 "Sent CADET_CHANNEL_OPEN on %s, retrying in %s\n",
568 GNUNET_STRINGS_relative_time_to_string (ch->retry_time,
570 ch->retry_control_task
571 = GNUNET_SCHEDULER_add_delayed (ch->retry_time,
578 * Send a channel open message.
580 * @param cls Channel for which to send.
583 send_channel_open (void *cls)
585 struct CadetChannel *ch = cls;
586 struct GNUNET_CADET_ChannelOpenMessage msgcc;
589 ch->retry_control_task = NULL;
590 LOG (GNUNET_ERROR_TYPE_DEBUG,
591 "Sending CHANNEL_OPEN message for %s\n",
595 options |= GNUNET_CADET_OPTION_NOBUFFER;
597 options |= GNUNET_CADET_OPTION_RELIABLE;
598 if (ch->out_of_order)
599 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
600 msgcc.header.size = htons (sizeof (msgcc));
601 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
602 msgcc.opt = htonl (options);
603 msgcc.h_port = ch->h_port;
605 ch->state = CADET_CHANNEL_OPEN_SENT;
606 if (NULL != ch->last_control_qe)
607 GCT_send_cancel (ch->last_control_qe);
608 ch->last_control_qe = GCT_send (ch->t,
610 &channel_open_sent_cb,
612 GNUNET_assert (NULL == ch->retry_control_task);
617 * Function called once and only once after a channel was bound
618 * to its tunnel via #GCT_add_channel() is ready for transmission.
619 * Note that this is only the case for channels that this peer
620 * initiates, as for incoming channels we assume that they are
621 * ready for transmission immediately upon receiving the open
622 * message. Used to bootstrap the #GCT_send() process.
624 * @param ch the channel for which the tunnel is now ready
627 GCCH_tunnel_up (struct CadetChannel *ch)
629 GNUNET_assert (NULL == ch->retry_control_task);
630 LOG (GNUNET_ERROR_TYPE_DEBUG,
631 "Tunnel up, sending CHANNEL_OPEN on %s now\n",
633 ch->retry_control_task
634 = GNUNET_SCHEDULER_add_now (&send_channel_open,
640 * Create a new channel.
642 * @param owner local client owning the channel
643 * @param ccn local number of this channel at the @a owner
644 * @param destination peer to which we should build the channel
645 * @param port desired port at @a destination
646 * @param options options for the channel
647 * @return handle to the new channel
649 struct CadetChannel *
650 GCCH_channel_local_new (struct CadetClient *owner,
651 struct GNUNET_CADET_ClientChannelNumber ccn,
652 struct CadetPeer *destination,
653 const struct GNUNET_HashCode *port,
656 struct CadetChannel *ch;
657 struct CadetChannelClient *ccco;
659 ccco = GNUNET_new (struct CadetChannelClient);
662 ccco->client_ready = GNUNET_YES;
664 ch = GNUNET_new (struct CadetChannel);
665 ch->mid_recv.mid = htonl (1); /* The OPEN_ACK counts as message 0! */
666 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
667 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
668 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
669 ch->max_pending_messages = (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
672 GCCH_hash_port (&ch->h_port,
674 GCP_get_id (destination));
675 if (0 == memcmp (&my_full_id,
676 GCP_get_id (destination),
677 sizeof (struct GNUNET_PeerIdentity)))
681 ch->is_loopback = GNUNET_YES;
682 op = GNUNET_CONTAINER_multihashmap_get (open_ports,
686 /* port closed, wait for it to possibly open */
687 ch->state = CADET_CHANNEL_LOOSE;
688 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
691 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
692 LOG (GNUNET_ERROR_TYPE_DEBUG,
693 "Created loose incoming loopback channel to port %s\n",
694 GNUNET_h2s (&ch->port));
705 ch->t = GCP_get_tunnel (destination,
707 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
708 ch->ctn = GCT_add_channel (ch->t,
711 GNUNET_STATISTICS_update (stats,
715 LOG (GNUNET_ERROR_TYPE_DEBUG,
716 "Created channel to port %s at peer %s for %s using %s\n",
718 GCP_2s (destination),
720 (GNUNET_YES == ch->is_loopback) ? "loopback" : GCT_2s (ch->t));
726 * We had an incoming channel to a port that is closed.
727 * It has not been opened for a while, drop it.
729 * @param cls the channel to drop
732 timeout_closed_cb (void *cls)
734 struct CadetChannel *ch = cls;
736 ch->retry_control_task = NULL;
737 LOG (GNUNET_ERROR_TYPE_DEBUG,
738 "Closing incoming channel to port %s from peer %s due to timeout\n",
739 GNUNET_h2s (&ch->port),
740 GCP_2s (GCT_get_destination (ch->t)));
741 channel_destroy (ch);
746 * Create a new channel based on a request coming in over the network.
748 * @param t tunnel to the remote peer
749 * @param ctn identifier of this channel in the tunnel
750 * @param h_port desired hash of local port
751 * @param options options for the channel
752 * @return handle to the new channel
754 struct CadetChannel *
755 GCCH_channel_incoming_new (struct CadetTunnel *t,
756 struct GNUNET_CADET_ChannelTunnelNumber ctn,
757 const struct GNUNET_HashCode *h_port,
760 struct CadetChannel *ch;
763 ch = GNUNET_new (struct CadetChannel);
764 ch->h_port = *h_port;
767 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
768 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
769 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
770 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
771 ch->max_pending_messages = (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
772 GNUNET_STATISTICS_update (stats,
777 op = GNUNET_CONTAINER_multihashmap_get (open_ports,
781 /* port closed, wait for it to possibly open */
782 ch->state = CADET_CHANNEL_LOOSE;
783 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
786 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
787 GNUNET_assert (NULL == ch->retry_control_task);
788 ch->retry_control_task
789 = GNUNET_SCHEDULER_add_delayed (TIMEOUT_CLOSED_PORT,
792 LOG (GNUNET_ERROR_TYPE_DEBUG,
793 "Created loose incoming channel to port %s from peer %s\n",
794 GNUNET_h2s (&ch->port),
795 GCP_2s (GCT_get_destination (ch->t)));
803 GNUNET_STATISTICS_update (stats,
812 * Function called once the tunnel confirms that we sent the
813 * ACK message. Just remembers it was sent, we do not expect
816 * @param cls our `struct CadetChannel`.
817 * @param cid identifier of the connection within the tunnel, NULL
818 * if transmission failed
821 send_ack_cb (void *cls,
822 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
824 struct CadetChannel *ch = cls;
826 GNUNET_assert (NULL != ch->last_control_qe);
827 ch->last_control_qe = NULL;
832 * Compute and send the current #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK to the other peer.
834 * @param ch channel to send the #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK for
837 send_channel_data_ack (struct CadetChannel *ch)
839 struct GNUNET_CADET_ChannelDataAckMessage msg;
841 if (GNUNET_NO == ch->reliable)
842 return; /* no ACKs */
843 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK);
844 msg.header.size = htons (sizeof (msg));
846 msg.mid.mid = htonl (ntohl (ch->mid_recv.mid));
847 msg.futures = GNUNET_htonll (ch->mid_futures);
848 LOG (GNUNET_ERROR_TYPE_DEBUG,
849 "Sending DATA_ACK %u:%llX via %s\n",
850 (unsigned int) ntohl (msg.mid.mid),
851 (unsigned long long) ch->mid_futures,
853 if (NULL != ch->last_control_qe)
854 GCT_send_cancel (ch->last_control_qe);
855 ch->last_control_qe = GCT_send (ch->t,
863 * Send our initial #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK to the client confirming that the
866 * @param cls the `struct CadetChannel`
869 send_open_ack (void *cls)
871 struct CadetChannel *ch = cls;
872 struct GNUNET_CADET_ChannelOpenAckMessage msg;
874 ch->retry_control_task = NULL;
875 LOG (GNUNET_ERROR_TYPE_DEBUG,
876 "Sending CHANNEL_OPEN_ACK on %s\n",
878 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK);
879 msg.header.size = htons (sizeof (msg));
880 msg.reserved = htonl (0);
883 if (NULL != ch->last_control_qe)
884 GCT_send_cancel (ch->last_control_qe);
885 ch->last_control_qe = GCT_send (ch->t,
893 * We got a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN message again for
894 * this channel. If the binding was successful, (re)transmit the
895 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK.
897 * @param ch channel that got the duplicate open
898 * @param cti identifier of the connection that delivered the message
901 GCCH_handle_duplicate_open (struct CadetChannel *ch,
902 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
904 if (NULL == ch->dest)
906 LOG (GNUNET_ERROR_TYPE_DEBUG,
907 "Ignoring duplicate CHANNEL_OPEN on %s: port is closed\n",
911 if (NULL != ch->retry_control_task)
913 LOG (GNUNET_ERROR_TYPE_DEBUG,
914 "Ignoring duplicate CHANNEL_OPEN on %s: control message is pending\n",
918 LOG (GNUNET_ERROR_TYPE_DEBUG,
919 "Retransmitting CHANNEL_OPEN_ACK on %s\n",
921 ch->retry_control_task
922 = GNUNET_SCHEDULER_add_now (&send_open_ack,
928 * Send a #GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK to the client to solicit more messages.
930 * @param ch channel the ack is for
931 * @param to_owner #GNUNET_YES to send to owner,
932 * #GNUNET_NO to send to dest
935 send_ack_to_client (struct CadetChannel *ch,
938 struct GNUNET_MQ_Envelope *env;
939 struct GNUNET_CADET_LocalAck *ack;
940 struct CadetChannelClient *ccc;
942 ccc = (GNUNET_YES == to_owner) ? ch->owner : ch->dest;
945 /* This can happen if we are just getting ACKs after
946 our local client already disconnected. */
947 GNUNET_assert (GNUNET_YES == ch->destroy);
950 env = GNUNET_MQ_msg (ack,
951 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
953 LOG (GNUNET_ERROR_TYPE_DEBUG,
954 "Sending CADET_LOCAL_ACK to %s (%s) at ccn %X (%u/%u pending)\n",
956 (GNUNET_YES == to_owner) ? "owner" : "dest",
957 ntohl (ack->ccn.channel_of_client),
958 ch->pending_messages,
959 ch->max_pending_messages);
960 GSC_send_to_client (ccc->c,
966 * A client is bound to the port that we have a channel
967 * open to. Send the acknowledgement for the connection
968 * request and establish the link with the client.
970 * @param ch open incoming channel
971 * @param c client listening on the respective @a port
972 * @param port the port @a is listening on
975 GCCH_bind (struct CadetChannel *ch,
976 struct CadetClient *c,
977 const struct GNUNET_HashCode *port)
980 struct CadetChannelClient *cccd;
982 LOG (GNUNET_ERROR_TYPE_DEBUG,
983 "Binding %s from %s to port %s of %s\n",
986 GNUNET_h2s (&ch->port),
988 if (NULL != ch->retry_control_task)
990 /* there might be a timeout task here */
991 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
992 ch->retry_control_task = NULL;
996 options |= GNUNET_CADET_OPTION_NOBUFFER;
998 options |= GNUNET_CADET_OPTION_RELIABLE;
999 if (ch->out_of_order)
1000 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
1001 cccd = GNUNET_new (struct CadetChannelClient);
1002 GNUNET_assert (NULL == ch->dest);
1006 cccd->client_ready = GNUNET_YES;
1007 cccd->ccn = GSC_bind (c,
1009 (GNUNET_YES == ch->is_loopback)
1010 ? GCP_get (&my_full_id,
1012 : GCT_get_destination (ch->t),
1015 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) <
1016 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1017 ch->mid_recv.mid = htonl (1); /* The OPEN counts as message 0! */
1018 if (GNUNET_YES == ch->is_loopback)
1020 ch->state = CADET_CHANNEL_OPEN_SENT;
1021 GCCH_handle_channel_open_ack (ch,
1027 /* notify other peer that we accepted the connection */
1028 ch->state = CADET_CHANNEL_READY;
1029 ch->retry_control_task
1030 = GNUNET_SCHEDULER_add_now (&send_open_ack,
1033 /* give client it's initial supply of ACKs */
1034 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) <
1035 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1036 for (unsigned int i=0;i<ch->max_pending_messages;i++)
1037 send_ack_to_client (ch,
1043 * One of our clients has disconnected, tell the other one that we
1044 * are finished. Done asynchronously to avoid concurrent modification
1045 * issues if this is the same client.
1047 * @param cls the `struct CadetChannel` where one of the ends is now dead
1050 signal_remote_destroy_cb (void *cls)
1052 struct CadetChannel *ch = cls;
1053 struct CadetChannelClient *ccc;
1055 /* Find which end is left... */
1056 ch->retry_control_task = NULL;
1057 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1058 GSC_handle_remote_channel_destroy (ccc->c,
1061 channel_destroy (ch);
1066 * Destroy locally created channel. Called by the local client, so no
1067 * need to tell the client.
1069 * @param ch channel to destroy
1070 * @param c client that caused the destruction
1071 * @param ccn client number of the client @a c
1074 GCCH_channel_local_destroy (struct CadetChannel *ch,
1075 struct CadetClient *c,
1076 struct GNUNET_CADET_ClientChannelNumber ccn)
1078 LOG (GNUNET_ERROR_TYPE_DEBUG,
1079 "%s asks for destruction of %s\n",
1082 GNUNET_assert (NULL != c);
1083 if ( (NULL != ch->owner) &&
1084 (c == ch->owner->c) &&
1085 (ccn.channel_of_client == ch->owner->ccn.channel_of_client) )
1087 free_channel_client (ch->owner);
1090 else if ( (NULL != ch->dest) &&
1091 (c == ch->dest->c) &&
1092 (ccn.channel_of_client == ch->dest->ccn.channel_of_client) )
1094 free_channel_client (ch->dest);
1102 if (GNUNET_YES == ch->destroy)
1104 /* other end already destroyed, with the local client gone, no need
1105 to finish transmissions, just destroy immediately. */
1106 channel_destroy (ch);
1109 if ( (NULL != ch->head_sent) &&
1110 ( (NULL != ch->owner) ||
1111 (NULL != ch->dest) ) )
1113 /* Wait for other end to destroy us as well,
1114 and otherwise allow send queue to be transmitted first */
1115 ch->destroy = GNUNET_YES;
1118 if ( (GNUNET_YES == ch->is_loopback) &&
1119 ( (NULL != ch->owner) ||
1120 (NULL != ch->dest) ) )
1122 if (NULL != ch->retry_control_task)
1123 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
1124 ch->retry_control_task
1125 = GNUNET_SCHEDULER_add_now (&signal_remote_destroy_cb,
1129 if (GNUNET_NO == ch->is_loopback)
1131 /* If the we ever sent the CHANNEL_CREATE, we need to send a destroy message. */
1134 case CADET_CHANNEL_NEW:
1135 /* We gave up on a channel that we created as a client to a remote
1136 target, but that never went anywhere. Nothing to do here. */
1138 case CADET_CHANNEL_LOOSE:
1139 GSC_drop_loose_channel (&ch->h_port,
1143 GCT_send_channel_destroy (ch->t,
1147 /* Nothing left to do, just finish destruction */
1148 channel_destroy (ch);
1153 * We got an acknowledgement for the creation of the channel
1154 * (the port is open on the other side). Verify that the
1155 * other end really has the right port, and begin transmissions.
1157 * @param ch channel to destroy
1158 * @param cti identifier of the connection that delivered the message
1159 * @param port port number (needed to verify receiver knows the port)
1162 GCCH_handle_channel_open_ack (struct CadetChannel *ch,
1163 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1164 const struct GNUNET_HashCode *port)
1168 case CADET_CHANNEL_NEW:
1169 /* this should be impossible */
1172 case CADET_CHANNEL_LOOSE:
1173 /* This makes no sense. */
1174 GNUNET_break_op (0);
1176 case CADET_CHANNEL_OPEN_SENT:
1177 if (NULL == ch->owner)
1179 /* We're not the owner, wrong direction! */
1180 GNUNET_break_op (0);
1183 if (0 != memcmp (&ch->port,
1185 sizeof (struct GNUNET_HashCode)))
1187 /* Other peer failed to provide the right port,
1188 refuse connection. */
1189 GNUNET_break_op (0);
1192 LOG (GNUNET_ERROR_TYPE_DEBUG,
1193 "Received CHANNEL_OPEN_ACK for waiting %s, entering READY state\n",
1195 if (NULL != ch->retry_control_task) /* can be NULL if ch->is_loopback */
1197 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
1198 ch->retry_control_task = NULL;
1200 ch->state = CADET_CHANNEL_READY;
1201 /* On first connect, send client as many ACKs as we allow messages
1203 for (unsigned int i=0;i<ch->max_pending_messages;i++)
1204 send_ack_to_client (ch,
1207 case CADET_CHANNEL_READY:
1208 /* duplicate ACK, maybe we retried the CREATE. Ignore. */
1209 LOG (GNUNET_ERROR_TYPE_DEBUG,
1210 "Received duplicate channel OPEN_ACK for %s\n",
1212 GNUNET_STATISTICS_update (stats,
1213 "# duplicate CREATE_ACKs",
1222 * Test if element @a e1 comes before element @a e2.
1224 * @param cls closure, to a flag where we indicate duplicate packets
1225 * @param m1 a message of to sort
1226 * @param m2 another message to sort
1227 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1230 is_before (void *cls,
1231 struct CadetOutOfOrderMessage *m1,
1232 struct CadetOutOfOrderMessage *m2)
1234 int *duplicate = cls;
1235 uint32_t v1 = ntohl (m1->mid.mid);
1236 uint32_t v2 = ntohl (m2->mid.mid);
1241 *duplicate = GNUNET_YES;
1242 if (delta > (uint32_t) INT_MAX)
1244 /* in overflow range, we can safely assume we wrapped around */
1249 /* result is small, thus v2 > v1, thus m1 < m2 */
1256 * We got payload data for a channel. Pass it on to the client
1257 * and send an ACK to the other end (once flow control allows it!)
1259 * @param ch channel that got data
1260 * @param cti identifier of the connection that delivered the message
1261 * @param msg message that was received
1264 GCCH_handle_channel_plaintext_data (struct CadetChannel *ch,
1265 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1266 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
1268 struct GNUNET_MQ_Envelope *env;
1269 struct GNUNET_CADET_LocalData *ld;
1270 struct CadetChannelClient *ccc;
1271 size_t payload_size;
1272 struct CadetOutOfOrderMessage *com;
1279 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1280 if ( (NULL == ch->owner) &&
1281 (NULL == ch->dest) )
1283 /* This client is gone, but we still have messages to send to
1284 the other end (which is why @a ch is not yet dead). However,
1285 we cannot pass messages to our client anymore. */
1286 LOG (GNUNET_ERROR_TYPE_DEBUG,
1287 "Dropping incoming payload on %s as this end is already closed\n",
1289 /* send back DESTROY notification to stop further retransmissions! */
1290 if (GNUNET_YES == ch->destroy)
1291 GCT_send_channel_destroy (ch->t,
1295 payload_size = ntohs (msg->header.size) - sizeof (*msg);
1296 env = GNUNET_MQ_msg_extra (ld,
1298 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1299 ld->ccn = (NULL == ch->dest) ? ch->owner->ccn : ch->dest->ccn;
1300 GNUNET_memcpy (&ld[1],
1303 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1304 if ( (GNUNET_YES == ccc->client_ready) &&
1305 ( (GNUNET_YES == ch->out_of_order) ||
1306 (msg->mid.mid == ch->mid_recv.mid) ) )
1308 LOG (GNUNET_ERROR_TYPE_DEBUG,
1309 "Giving %u bytes of payload with MID %u from %s to client %s\n",
1310 (unsigned int) payload_size,
1311 ntohl (msg->mid.mid),
1314 ccc->client_ready = GNUNET_NO;
1315 GSC_send_to_client (ccc->c,
1317 ch->mid_recv.mid = htonl (1 + ntohl (ch->mid_recv.mid));
1318 ch->mid_futures >>= 1;
1319 send_channel_data_ack (ch);
1323 if (GNUNET_YES == ch->reliable)
1325 /* check if message ought to be dropped because it is ancient/too distant/duplicate */
1326 mid_min = ntohl (ch->mid_recv.mid);
1327 mid_max = mid_min + ch->max_pending_messages;
1328 mid_msg = ntohl (msg->mid.mid);
1329 if ( ( (uint32_t) (mid_msg - mid_min) > ch->max_pending_messages) ||
1330 ( (uint32_t) (mid_max - mid_msg) > ch->max_pending_messages) )
1332 LOG (GNUNET_ERROR_TYPE_DEBUG,
1333 "%s at %u drops ancient or far-future message %u\n",
1335 (unsigned int) mid_min,
1336 ntohl (msg->mid.mid));
1338 GNUNET_STATISTICS_update (stats,
1339 "# duplicate DATA (ancient or future)",
1342 GNUNET_MQ_discard (env);
1343 send_channel_data_ack (ch);
1346 /* mark bit for future ACKs */
1347 delta = mid_msg - mid_min - 1; /* overflow/underflow are OK here */
1350 if (0 != (ch->mid_futures & (1LLU << delta)))
1352 /* Duplicate within the queue, drop also */
1353 LOG (GNUNET_ERROR_TYPE_DEBUG,
1354 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1355 (unsigned int) payload_size,
1357 ntohl (msg->mid.mid));
1358 GNUNET_STATISTICS_update (stats,
1362 GNUNET_MQ_discard (env);
1363 send_channel_data_ack (ch);
1366 ch->mid_futures |= (1LLU << delta);
1367 LOG (GNUNET_ERROR_TYPE_DEBUG,
1368 "Marked bit %llX for mid %u (base: %u); now: %llX\n",
1375 else /* ! ch->reliable */
1377 /* Channel is unreliable, so we do not ACK. But we also cannot
1378 allow buffering everything, so check if we have space... */
1379 if (ccc->num_recv >= ch->max_pending_messages)
1381 struct CadetOutOfOrderMessage *drop;
1383 /* Yep, need to drop. Drop the oldest message in
1385 LOG (GNUNET_ERROR_TYPE_DEBUG,
1386 "Queue full due slow client on %s, dropping oldest message\n",
1388 GNUNET_STATISTICS_update (stats,
1389 "# messages dropped due to slow client",
1392 drop = ccc->head_recv;
1393 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1397 GNUNET_MQ_discard (drop->env);
1402 /* Insert message into sorted out-of-order queue */
1403 com = GNUNET_new (struct CadetOutOfOrderMessage);
1404 com->mid = msg->mid;
1406 duplicate = GNUNET_NO;
1407 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetOutOfOrderMessage,
1414 if (GNUNET_YES == duplicate)
1416 /* Duplicate within the queue, drop also (this is not covered by
1417 the case above if "delta" >= 64, which could be the case if
1418 max_pending_messages is also >= 64 or if our client is unready
1419 and we are seeing retransmissions of the message our client is
1421 LOG (GNUNET_ERROR_TYPE_DEBUG,
1422 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1423 (unsigned int) payload_size,
1425 ntohl (msg->mid.mid));
1426 GNUNET_STATISTICS_update (stats,
1430 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1434 GNUNET_MQ_discard (com->env);
1436 send_channel_data_ack (ch);
1439 LOG (GNUNET_ERROR_TYPE_DEBUG,
1440 "Queued %s payload of %u bytes on %s-%X(%p) (mid %u, need %u first)\n",
1441 (GNUNET_YES == ccc->client_ready)
1443 : "client-not-ready",
1444 (unsigned int) payload_size,
1446 ntohl (ccc->ccn.channel_of_client),
1448 ntohl (msg->mid.mid),
1449 ntohl (ch->mid_recv.mid));
1450 /* NOTE: this ACK we _could_ skip, as the packet is out-of-order and
1451 the sender may already be transmitting the previous one. Needs
1452 experimental evaluation to see if/when this ACK helps or
1453 hurts. (We might even want another option.) */
1454 send_channel_data_ack (ch);
1459 * Function called once the tunnel has sent one of our messages.
1460 * If the message is unreliable, simply frees the `crm`. If the
1461 * message was reliable, calculate retransmission time and
1462 * wait for ACK (or retransmit).
1464 * @param cls the `struct CadetReliableMessage` that was sent
1465 * @param cid identifier of the connection within the tunnel, NULL
1466 * if transmission failed
1469 data_sent_cb (void *cls,
1470 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
1474 * We need to retry a transmission, the last one took too long to
1477 * @param cls the `struct CadetChannel` where we need to retransmit
1480 retry_transmission (void *cls)
1482 struct CadetChannel *ch = cls;
1483 struct CadetReliableMessage *crm = ch->head_sent;
1485 ch->retry_data_task = NULL;
1486 GNUNET_assert (NULL == crm->qe);
1487 LOG (GNUNET_ERROR_TYPE_DEBUG,
1488 "Retrying transmission on %s of message %u\n",
1490 (unsigned int) ntohl (crm->data_message->mid.mid));
1491 crm->qe = GCT_send (ch->t,
1492 &crm->data_message->header,
1495 GNUNET_assert (NULL == ch->retry_data_task);
1500 * We got an PLAINTEXT_DATA_ACK for a message in our queue, remove it from
1501 * the queue and tell our client that it can send more.
1503 * @param ch the channel that got the PLAINTEXT_DATA_ACK
1504 * @param cti identifier of the connection that delivered the message
1505 * @param crm the message that got acknowledged
1508 handle_matching_ack (struct CadetChannel *ch,
1509 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1510 struct CadetReliableMessage *crm)
1512 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1515 ch->pending_messages--;
1516 GNUNET_assert (ch->pending_messages < ch->max_pending_messages);
1517 LOG (GNUNET_ERROR_TYPE_DEBUG,
1518 "Received DATA_ACK on %s for message %u (%u ACKs pending)\n",
1520 (unsigned int) ntohl (crm->data_message->mid.mid),
1521 ch->pending_messages);
1522 if (NULL != crm->qe)
1524 GCT_send_cancel (crm->qe);
1527 if ( (1 == crm->num_transmissions) &&
1530 GCC_ack_observed (cti);
1531 if (0 == memcmp (cti,
1532 &crm->connection_taken,
1533 sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier)))
1535 GCC_latency_observed (cti,
1536 GNUNET_TIME_absolute_get_duration (crm->first_transmission_time));
1539 GNUNET_free (crm->data_message);
1541 send_ack_to_client (ch,
1549 * We got an acknowledgement for payload data for a channel.
1550 * Possibly resume transmissions.
1552 * @param ch channel that got the ack
1553 * @param cti identifier of the connection that delivered the message
1554 * @param ack details about what was received
1557 GCCH_handle_channel_plaintext_data_ack (struct CadetChannel *ch,
1558 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1559 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
1561 struct CadetReliableMessage *crm;
1562 struct CadetReliableMessage *crmn;
1568 GNUNET_break (GNUNET_NO == ch->is_loopback);
1569 if (GNUNET_NO == ch->reliable)
1571 /* not expecting ACKs on unreliable channel, odd */
1572 GNUNET_break_op (0);
1575 /* mid_base is the MID of the next message that the
1576 other peer expects (i.e. that is missing!), everything
1577 LOWER (but excluding mid_base itself) was received. */
1578 mid_base = ntohl (ack->mid.mid);
1579 mid_mask = GNUNET_htonll (ack->futures);
1581 for (crm = ch->head_sent;
1586 delta = (unsigned int) (ntohl (crm->data_message->mid.mid) - mid_base);
1587 if (delta >= UINT_MAX - ch->max_pending_messages)
1589 /* overflow, means crm was a bit in the past, so this ACK counts for it. */
1590 LOG (GNUNET_ERROR_TYPE_DEBUG,
1591 "Got DATA_ACK with base %u satisfying past message %u on %s\n",
1592 (unsigned int) mid_base,
1593 ntohl (crm->data_message->mid.mid),
1595 handle_matching_ack (ch,
1604 LOG (GNUNET_ERROR_TYPE_DEBUG,
1605 "Testing bit %llX for mid %u (base: %u)\n",
1607 ntohl (crm->data_message->mid.mid),
1609 if (0 != (mid_mask & (1LLU << delta)))
1611 LOG (GNUNET_ERROR_TYPE_DEBUG,
1612 "Got DATA_ACK with mask for %u on %s\n",
1613 ntohl (crm->data_message->mid.mid),
1615 handle_matching_ack (ch,
1621 if (GNUNET_NO == found)
1623 /* ACK for message we already dropped, might have been a
1624 duplicate ACK? Ignore. */
1625 LOG (GNUNET_ERROR_TYPE_DEBUG,
1626 "Duplicate DATA_ACK on %s, ignoring\n",
1628 GNUNET_STATISTICS_update (stats,
1629 "# duplicate DATA_ACKs",
1634 if (NULL != ch->retry_data_task)
1636 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1637 ch->retry_data_task = NULL;
1639 if ( (NULL != ch->head_sent) &&
1640 (NULL == ch->head_sent->qe) )
1642 = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry,
1643 &retry_transmission,
1649 * Destroy channel, based on the other peer closing the
1650 * connection. Also needs to remove this channel from
1653 * @param ch channel to destroy
1654 * @param cti identifier of the connection that delivered the message,
1655 * NULL if we are simulating receiving a destroy due to shutdown
1658 GCCH_handle_remote_destroy (struct CadetChannel *ch,
1659 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
1661 struct CadetChannelClient *ccc;
1663 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1664 LOG (GNUNET_ERROR_TYPE_DEBUG,
1665 "Received remote channel DESTROY for %s\n",
1667 if (GNUNET_YES == ch->destroy)
1669 /* Local client already gone, this is instant-death. */
1670 channel_destroy (ch);
1673 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1674 if ( (NULL != ccc) &&
1675 (NULL != ccc->head_recv) )
1677 LOG (GNUNET_ERROR_TYPE_WARNING,
1678 "Lost end of transmission due to remote shutdown on %s\n",
1680 /* FIXME: change API to notify client about truncated transmission! */
1682 ch->destroy = GNUNET_YES;
1684 GSC_handle_remote_channel_destroy (ccc->c,
1687 channel_destroy (ch);
1692 * Test if element @a e1 comes before element @a e2.
1694 * @param cls closure, to a flag where we indicate duplicate packets
1695 * @param crm1 an element of to sort
1696 * @param crm2 another element to sort
1697 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1700 cmp_crm_by_next_retry (void *cls,
1701 struct CadetReliableMessage *crm1,
1702 struct CadetReliableMessage *crm2)
1704 if (crm1->next_retry.abs_value_us <
1705 crm2->next_retry.abs_value_us)
1712 * Function called once the tunnel has sent one of our messages.
1713 * If the message is unreliable, simply frees the `crm`. If the
1714 * message was reliable, calculate retransmission time and
1715 * wait for ACK (or retransmit).
1717 * @param cls the `struct CadetReliableMessage` that was sent
1718 * @param cid identifier of the connection within the tunnel, NULL
1719 * if transmission failed
1722 data_sent_cb (void *cls,
1723 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
1725 struct CadetReliableMessage *crm = cls;
1726 struct CadetChannel *ch = crm->ch;
1728 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1729 GNUNET_assert (NULL != crm->qe);
1731 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1734 if (GNUNET_NO == ch->reliable)
1736 GNUNET_free (crm->data_message);
1738 ch->pending_messages--;
1739 send_ack_to_client (ch,
1747 /* There was an error sending. */
1748 crm->num_transmissions = GNUNET_SYSERR;
1750 else if (GNUNET_SYSERR != crm->num_transmissions)
1752 /* Increment transmission counter, and possibly store @a cid
1753 if this was the first transmission. */
1754 crm->num_transmissions++;
1755 if (1 == crm->num_transmissions)
1757 crm->first_transmission_time = GNUNET_TIME_absolute_get ();
1758 crm->connection_taken = *cid;
1759 GCC_ack_expected (cid);
1762 if ( (0 == crm->retry_delay.rel_value_us) &&
1765 struct CadetConnection *cc = GCC_lookup (cid);
1768 crm->retry_delay = GCC_get_metrics (cc)->aged_latency;
1770 crm->retry_delay = ch->retry_time;
1772 crm->retry_delay = GNUNET_TIME_STD_BACKOFF (crm->retry_delay);
1773 crm->retry_delay = GNUNET_TIME_relative_max (crm->retry_delay,
1775 crm->next_retry = GNUNET_TIME_relative_to_absolute (crm->retry_delay);
1777 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetReliableMessage,
1778 cmp_crm_by_next_retry,
1783 LOG (GNUNET_ERROR_TYPE_DEBUG,
1784 "Message %u sent, next transmission on %s in %s\n",
1785 (unsigned int) ntohl (crm->data_message->mid.mid),
1787 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (ch->head_sent->next_retry),
1789 if (NULL == ch->head_sent->qe)
1791 if (NULL != ch->retry_data_task)
1792 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1794 = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry,
1795 &retry_transmission,
1802 * Handle data given by a client.
1804 * Check whether the client is allowed to send in this tunnel, save if
1805 * channel is reliable and send an ACK to the client if there is still
1806 * buffer space in the tunnel.
1808 * @param ch Channel.
1809 * @param sender_ccn ccn of the sender
1810 * @param buf payload to transmit.
1811 * @param buf_len number of bytes in @a buf
1812 * @return #GNUNET_OK if everything goes well,
1813 * #GNUNET_SYSERR in case of an error.
1816 GCCH_handle_local_data (struct CadetChannel *ch,
1817 struct GNUNET_CADET_ClientChannelNumber sender_ccn,
1821 struct CadetReliableMessage *crm;
1823 if (ch->pending_messages >= ch->max_pending_messages)
1826 return GNUNET_SYSERR;
1828 if (GNUNET_YES == ch->destroy)
1830 /* we are going down, drop messages */
1833 ch->pending_messages++;
1835 if (GNUNET_YES == ch->is_loopback)
1837 struct CadetChannelClient *receiver;
1838 struct GNUNET_MQ_Envelope *env;
1839 struct GNUNET_CADET_LocalData *ld;
1842 env = GNUNET_MQ_msg_extra (ld,
1844 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1845 if ( (NULL != ch->owner) &&
1846 (sender_ccn.channel_of_client ==
1847 ch->owner->ccn.channel_of_client) )
1849 receiver = ch->dest;
1850 ack_to_owner = GNUNET_YES;
1852 else if ( (NULL != ch->dest) &&
1853 (sender_ccn.channel_of_client ==
1854 ch->dest->ccn.channel_of_client) )
1856 receiver = ch->owner;
1857 ack_to_owner = GNUNET_NO;
1862 return GNUNET_SYSERR;
1864 GNUNET_assert (NULL != receiver);
1865 ld->ccn = receiver->ccn;
1866 GNUNET_memcpy (&ld[1],
1869 if (GNUNET_YES == receiver->client_ready)
1871 ch->pending_messages--;
1872 GSC_send_to_client (receiver->c,
1874 send_ack_to_client (ch,
1879 struct CadetOutOfOrderMessage *oom;
1881 oom = GNUNET_new (struct CadetOutOfOrderMessage);
1883 GNUNET_CONTAINER_DLL_insert_tail (receiver->head_recv,
1884 receiver->tail_recv,
1886 receiver->num_recv++;
1891 /* Everything is correct, send the message. */
1892 crm = GNUNET_malloc (sizeof (*crm));
1894 crm->data_message = GNUNET_malloc (sizeof (struct GNUNET_CADET_ChannelAppDataMessage)
1896 crm->data_message->header.size = htons (sizeof (struct GNUNET_CADET_ChannelAppDataMessage) + buf_len);
1897 crm->data_message->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA);
1898 ch->mid_send.mid = htonl (ntohl (ch->mid_send.mid) + 1);
1899 crm->data_message->mid = ch->mid_send;
1900 crm->data_message->ctn = ch->ctn;
1901 GNUNET_memcpy (&crm->data_message[1],
1904 GNUNET_CONTAINER_DLL_insert_tail (ch->head_sent,
1907 LOG (GNUNET_ERROR_TYPE_DEBUG,
1908 "Sending message %u from local client to %s with %u bytes\n",
1909 ntohl (crm->data_message->mid.mid),
1912 if (NULL != ch->retry_data_task)
1914 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1915 ch->retry_data_task = NULL;
1917 crm->qe = GCT_send (ch->t,
1918 &crm->data_message->header,
1921 GNUNET_assert (NULL == ch->retry_data_task);
1927 * Handle ACK from client on local channel. Means the client is ready
1928 * for more data, see if we have any for it.
1930 * @param ch channel to destroy
1931 * @param client_ccn ccn of the client sending the ack
1934 GCCH_handle_local_ack (struct CadetChannel *ch,
1935 struct GNUNET_CADET_ClientChannelNumber client_ccn)
1937 struct CadetChannelClient *ccc;
1938 struct CadetOutOfOrderMessage *com;
1940 if ( (NULL != ch->owner) &&
1941 (ch->owner->ccn.channel_of_client == client_ccn.channel_of_client) )
1943 else if ( (NULL != ch->dest) &&
1944 (ch->dest->ccn.channel_of_client == client_ccn.channel_of_client) )
1948 ccc->client_ready = GNUNET_YES;
1949 com = ccc->head_recv;
1952 LOG (GNUNET_ERROR_TYPE_DEBUG,
1953 "Got LOCAL_ACK, %s-%X ready to receive more data, but none pending on %s-%X(%p)!\n",
1955 ntohl (client_ccn.channel_of_client),
1957 ntohl (ccc->ccn.channel_of_client),
1959 return; /* none pending */
1961 if (GNUNET_YES == ch->is_loopback)
1965 /* Messages are always in-order, just send */
1966 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1970 GSC_send_to_client (ccc->c,
1972 /* Notify sender that we can receive more */
1973 if ( (NULL != ch->owner) &&
1974 (ccc->ccn.channel_of_client ==
1975 ch->owner->ccn.channel_of_client) )
1977 to_owner = GNUNET_NO;
1981 GNUNET_assert ( (NULL != ch->dest) &&
1982 (ccc->ccn.channel_of_client ==
1983 ch->dest->ccn.channel_of_client) );
1984 to_owner = GNUNET_YES;
1986 send_ack_to_client (ch,
1992 if ( (com->mid.mid != ch->mid_recv.mid) &&
1993 (GNUNET_NO == ch->out_of_order) &&
1994 (GNUNET_YES == ch->reliable) )
1996 LOG (GNUNET_ERROR_TYPE_DEBUG,
1997 "Got LOCAL_ACK, %s-%X ready to receive more data (but next one is out-of-order %u vs. %u)!\n",
1999 ntohl (ccc->ccn.channel_of_client),
2000 ntohl (com->mid.mid),
2001 ntohl (ch->mid_recv.mid));
2002 return; /* missing next one in-order */
2005 LOG (GNUNET_ERROR_TYPE_DEBUG,
2006 "Got LOCAL_ACK, giving payload message %u to %s-%X on %s\n",
2007 ntohl (com->mid.mid),
2009 ntohl (ccc->ccn.channel_of_client),
2012 /* all good, pass next message to client */
2013 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
2017 /* FIXME: if unreliable, this is not aggressive
2018 enough, as it would be OK to have lost some! */
2020 ch->mid_recv.mid = htonl (1 + ntohl (com->mid.mid));
2021 ch->mid_futures >>= 1; /* equivalent to division by 2 */
2022 ccc->client_ready = GNUNET_NO;
2023 GSC_send_to_client (ccc->c,
2026 send_channel_data_ack (ch);
2027 if (NULL != ccc->head_recv)
2029 if (GNUNET_NO == ch->destroy)
2031 GCT_send_channel_destroy (ch->t,
2033 channel_destroy (ch);
2037 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-chn",__VA_ARGS__)
2043 * @param ch Channel.
2044 * @param level Debug level to use.
2047 GCCH_debug (struct CadetChannel *ch,
2048 enum GNUNET_ErrorType level)
2050 #if !defined(GNUNET_CULL_LOGGING)
2053 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
2055 __FILE__, __FUNCTION__, __LINE__);
2061 LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
2069 if (NULL != ch->owner)
2072 "CHN origin %s ready %s local-id: %u\n",
2073 GSC_2s (ch->owner->c),
2074 ch->owner->client_ready ? "YES" : "NO",
2075 ntohl (ch->owner->ccn.channel_of_client));
2077 if (NULL != ch->dest)
2080 "CHN destination %s ready %s local-id: %u\n",
2081 GSC_2s (ch->dest->c),
2082 ch->dest->client_ready ? "YES" : "NO",
2083 ntohl (ch->dest->ccn.channel_of_client));
2086 "CHN Message IDs recv: %d (%LLX), send: %d\n",
2087 ntohl (ch->mid_recv.mid),
2088 (unsigned long long) ch->mid_futures,
2089 ntohl (ch->mid_send.mid));
2095 /* end of gnunet-service-cadet-new_channel.c */