3 This file is part of GNUnet.
4 Copyright (C) 2001-2017 GNUnet e.V.
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 * @file cadet/gnunet-service-cadet-new_channel.c
23 * @brief logical links between CADET clients
24 * @author Bartlomiej Polot
25 * @author Christian Grothoff
28 * - Congestion/flow control:
29 * + calculate current RTT if possible, use that for initial retransmissions
30 * (NOTE: needs us to learn which connection the tunnel uses for the message!)
31 * + estimate max bandwidth using bursts and use to for CONGESTION CONTROL!
32 * (and figure out how/where to use this!)
33 * + figure out flow control without ACKs (unreliable traffic!)
34 * - revisit handling of 'unbuffered' traffic!
35 * (need to push down through tunnel into connection selection)
36 * - revisit handling of 'buffered' traffic: 4 is a rather small buffer; maybe
37 * reserve more bits in 'options' to allow for buffer size control?
40 #include "gnunet_util_lib.h"
42 #include "gnunet_statistics_service.h"
43 #include "gnunet-service-cadet-new.h"
44 #include "gnunet-service-cadet-new_channel.h"
45 #include "gnunet-service-cadet-new_connection.h"
46 #include "gnunet-service-cadet-new_tunnels.h"
47 #include "gnunet-service-cadet-new_peer.h"
48 #include "gnunet-service-cadet-new_paths.h"
50 #define LOG(level,...) GNUNET_log_from (level,"cadet-chn",__VA_ARGS__)
53 * How long do we initially wait before retransmitting?
55 #define CADET_INITIAL_RETRANSMIT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250)
58 * How long do we wait before dropping state about incoming
59 * connection to closed port?
61 #define TIMEOUT_CLOSED_PORT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
64 * How long do we wait at least before retransmitting ever?
66 #define MIN_RTT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 75)
69 * Maximum message ID into the future we accept for out-of-order messages.
70 * If the message is more than this into the future, we drop it. This is
71 * important both to detect values that are actually in the past, as well
72 * as to limit adversarially triggerable memory consumption.
74 * Note that right now we have "max_pending_messages = 4" hard-coded in
75 * the logic below, so a value of 4 would suffice here. But we plan to
76 * allow larger windows in the future...
78 #define MAX_OUT_OF_ORDER_DISTANCE 1024
82 * All the states a connection can be in.
84 enum CadetChannelState
87 * Uninitialized status, should never appear in operation.
92 * Connection create message sent, waiting for ACK.
94 CADET_CHANNEL_OPEN_SENT,
97 * Connection confirmed, ready to carry traffic.
104 * Info needed to retry a message in case it gets lost.
105 * Note that we DO use this structure also for unreliable
108 struct CadetReliableMessage
111 * Double linked list, FIFO style
113 struct CadetReliableMessage *next;
116 * Double linked list, FIFO style
118 struct CadetReliableMessage *prev;
121 * Which channel is this message in?
123 struct CadetChannel *ch;
126 * Entry in the tunnels queue for this message, NULL if it has left
127 * the tunnel. Used to cancel transmission in case we receive an
130 struct CadetTunnelQueueEntry *qe;
133 * How soon should we retry if we fail to get an ACK?
134 * Messages in the queue are sorted by this value.
136 struct GNUNET_TIME_Absolute next_retry;
139 * How long do we wait for an ACK after transmission?
140 * Use for the back-off calculation.
142 struct GNUNET_TIME_Relative retry_delay;
145 * Data message we are trying to send.
147 struct GNUNET_CADET_ChannelAppDataMessage *data_message;
153 * List of received out-of-order data messages.
155 struct CadetOutOfOrderMessage
158 * Double linked list, FIFO style
160 struct CadetOutOfOrderMessage *next;
163 * Double linked list, FIFO style
165 struct CadetOutOfOrderMessage *prev;
168 * ID of the message (messages up to this point needed
169 * before we give this one to the client).
171 struct ChannelMessageIdentifier mid;
174 * The envelope with the payload of the out-of-order message
176 struct GNUNET_MQ_Envelope *env;
182 * Client endpoint of a `struct CadetChannel`. A channel may be a
183 * loopback channel, in which case it has two of these endpoints.
184 * Note that flow control also is required in both directions.
186 struct CadetChannelClient
189 * Client handle. Not by itself sufficient to designate
190 * the client endpoint, as the same client handle may
191 * be used for both the owner and the destination, and
192 * we thus also need the channel ID to identify the client.
194 struct CadetClient *c;
197 * Head of DLL of messages received out of order or while client was unready.
199 struct CadetOutOfOrderMessage *head_recv;
202 * Tail DLL of messages received out of order or while client was unready.
204 struct CadetOutOfOrderMessage *tail_recv;
207 * Local tunnel number for this client.
208 * (if owner >= #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI,
209 * otherwise < #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
211 struct GNUNET_CADET_ClientChannelNumber ccn;
214 * Number of entries currently in @a head_recv DLL.
216 unsigned int num_recv;
219 * Can we send data to the client?
227 * Struct containing all information regarding a channel to a remote client.
232 * Tunnel this channel is in.
234 struct CadetTunnel *t;
237 * Client owner of the tunnel, if any.
238 * (Used if this channel represends the initiating end of the tunnel.)
240 struct CadetChannelClient *owner;
243 * Client destination of the tunnel, if any.
244 * (Used if this channel represents the listening end of the tunnel.)
246 struct CadetChannelClient *dest;
249 * Last entry in the tunnel's queue relating to control messages
250 * (#GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN or
251 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK). Used to cancel
252 * transmission in case we receive updated information.
254 struct CadetTunnelQueueEntry *last_control_qe;
257 * Head of DLL of messages sent and not yet ACK'd.
259 struct CadetReliableMessage *head_sent;
262 * Tail of DLL of messages sent and not yet ACK'd.
264 struct CadetReliableMessage *tail_sent;
267 * Task to resend/poll in case no ACK is received.
269 struct GNUNET_SCHEDULER_Task *retry_control_task;
272 * Task to resend/poll in case no ACK is received.
274 struct GNUNET_SCHEDULER_Task *retry_data_task;
277 * Last time the channel was used
279 struct GNUNET_TIME_Absolute timestamp;
282 * Destination port of the channel.
284 struct GNUNET_HashCode port;
287 * Counter for exponential backoff.
289 struct GNUNET_TIME_Relative retry_time;
292 * How long does it usually take to get an ACK.
294 struct GNUNET_TIME_Relative expected_delay;
297 * Bitfield of already-received messages past @e mid_recv.
299 uint64_t mid_futures;
302 * Next MID expected for incoming traffic.
304 struct ChannelMessageIdentifier mid_recv;
307 * Next MID to use for outgoing traffic.
309 struct ChannelMessageIdentifier mid_send;
312 * Total (reliable) messages pending ACK for this channel.
314 unsigned int pending_messages;
317 * Maximum (reliable) messages pending ACK for this channel
318 * before we throttle the client.
320 unsigned int max_pending_messages;
323 * Number identifying this channel in its tunnel.
325 struct GNUNET_CADET_ChannelTunnelNumber ctn;
330 enum CadetChannelState state;
333 * Count how many ACKs we skipped, used to prevent long
334 * sequences of ACK skipping.
336 unsigned int skip_ack_series;
339 * Is the tunnel bufferless (minimum latency)?
344 * Is the tunnel reliable?
349 * Is the tunnel out-of-order?
354 * Is this channel a loopback channel, where the destination is us again?
359 * Flag to signal the destruction of the channel. If this is set to
360 * #GNUNET_YES the channel will be destroyed once the queue is
369 * Get the static string for identification of the channel.
373 * @return Static string with the channel IDs.
376 GCCH_2s (const struct CadetChannel *ch)
378 static char buf[128];
380 GNUNET_snprintf (buf,
382 "Channel %s:%s ctn:%X(%X/%X)",
383 (GNUNET_YES == ch->is_loopback)
385 : GNUNET_i2s (GCP_get_id (GCT_get_destination (ch->t))),
386 GNUNET_h2s (&ch->port),
388 (NULL == ch->owner) ? 0 : ntohl (ch->owner->ccn.channel_of_client),
389 (NULL == ch->dest) ? 0 : ntohl (ch->dest->ccn.channel_of_client));
395 * Get the channel's public ID.
399 * @return ID used to identify the channel with the remote peer.
401 struct GNUNET_CADET_ChannelTunnelNumber
402 GCCH_get_id (const struct CadetChannel *ch)
409 * Release memory associated with @a ccc
411 * @param ccc data structure to clean up
414 free_channel_client (struct CadetChannelClient *ccc)
416 struct CadetOutOfOrderMessage *com;
418 while (NULL != (com = ccc->head_recv))
420 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
424 GNUNET_MQ_discard (com->env);
432 * Destroy the given channel.
434 * @param ch channel to destroy
437 channel_destroy (struct CadetChannel *ch)
439 struct CadetReliableMessage *crm;
441 while (NULL != (crm = ch->head_sent))
443 GNUNET_assert (ch == crm->ch);
446 GCT_send_cancel (crm->qe);
449 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
452 GNUNET_free (crm->data_message);
455 if (NULL != ch->owner)
457 free_channel_client (ch->owner);
460 if (NULL != ch->dest)
462 free_channel_client (ch->dest);
465 if (NULL != ch->last_control_qe)
467 GCT_send_cancel (ch->last_control_qe);
468 ch->last_control_qe = NULL;
470 if (NULL != ch->retry_data_task)
472 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
473 ch->retry_data_task = NULL;
475 if (NULL != ch->retry_control_task)
477 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
478 ch->retry_control_task = NULL;
480 if (GNUNET_NO == ch->is_loopback)
482 GCT_remove_channel (ch->t,
492 * Send a channel create message.
494 * @param cls Channel for which to send.
497 send_channel_open (void *cls);
501 * Function called once the tunnel confirms that we sent the
502 * create message. Delays for a bit until we retry.
504 * @param cls our `struct CadetChannel`.
507 channel_open_sent_cb (void *cls)
509 struct CadetChannel *ch = cls;
511 GNUNET_assert (NULL != ch->last_control_qe);
512 ch->last_control_qe = NULL;
513 ch->retry_time = GNUNET_TIME_STD_BACKOFF (ch->retry_time);
514 LOG (GNUNET_ERROR_TYPE_DEBUG,
515 "Sent CADET_CHANNEL_OPEN on %s, retrying in %s\n",
517 GNUNET_STRINGS_relative_time_to_string (ch->retry_time,
519 ch->retry_control_task
520 = GNUNET_SCHEDULER_add_delayed (ch->retry_time,
527 * Send a channel open message.
529 * @param cls Channel for which to send.
532 send_channel_open (void *cls)
534 struct CadetChannel *ch = cls;
535 struct GNUNET_CADET_ChannelOpenMessage msgcc;
538 ch->retry_control_task = NULL;
539 LOG (GNUNET_ERROR_TYPE_DEBUG,
540 "Sending CHANNEL_OPEN message for %s\n",
544 options |= GNUNET_CADET_OPTION_NOBUFFER;
546 options |= GNUNET_CADET_OPTION_RELIABLE;
547 if (ch->out_of_order)
548 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
549 msgcc.header.size = htons (sizeof (msgcc));
550 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
551 msgcc.opt = htonl (options);
552 msgcc.port = ch->port;
554 ch->state = CADET_CHANNEL_OPEN_SENT;
555 ch->last_control_qe = GCT_send (ch->t,
557 &channel_open_sent_cb,
559 GNUNET_assert (NULL == ch->retry_control_task);
564 * Function called once and only once after a channel was bound
565 * to its tunnel via #GCT_add_channel() is ready for transmission.
566 * Note that this is only the case for channels that this peer
567 * initiates, as for incoming channels we assume that they are
568 * ready for transmission immediately upon receiving the open
569 * message. Used to bootstrap the #GCT_send() process.
571 * @param ch the channel for which the tunnel is now ready
574 GCCH_tunnel_up (struct CadetChannel *ch)
576 GNUNET_assert (NULL == ch->retry_control_task);
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "Tunnel up, sending CHANNEL_OPEN on %s now\n",
580 ch->retry_control_task
581 = GNUNET_SCHEDULER_add_now (&send_channel_open,
587 * Create a new channel.
589 * @param owner local client owning the channel
590 * @param ccn local number of this channel at the @a owner
591 * @param destination peer to which we should build the channel
592 * @param port desired port at @a destination
593 * @param options options for the channel
594 * @return handle to the new channel
596 struct CadetChannel *
597 GCCH_channel_local_new (struct CadetClient *owner,
598 struct GNUNET_CADET_ClientChannelNumber ccn,
599 struct CadetPeer *destination,
600 const struct GNUNET_HashCode *port,
603 struct CadetChannel *ch;
604 struct CadetChannelClient *ccco;
606 ccco = GNUNET_new (struct CadetChannelClient);
609 ccco->client_ready = GNUNET_YES;
611 ch = GNUNET_new (struct CadetChannel);
612 ch->mid_recv.mid = htonl (1); /* The OPEN_ACK counts as message 0! */
613 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
614 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
615 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
616 ch->max_pending_messages = (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
619 if (0 == memcmp (&my_full_id,
620 GCP_get_id (destination),
621 sizeof (struct GNUNET_PeerIdentity)))
623 struct CadetClient *c;
625 ch->is_loopback = GNUNET_YES;
626 c = GNUNET_CONTAINER_multihashmap_get (open_ports,
630 /* port closed, wait for it to possibly open */
631 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
634 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
635 LOG (GNUNET_ERROR_TYPE_DEBUG,
636 "Created loose incoming loopback channel to port %s\n",
637 GNUNET_h2s (&ch->port));
641 ch->dest = GNUNET_new (struct CadetChannelClient);
643 ch->dest->client_ready = GNUNET_YES;
650 ch->t = GCP_get_tunnel (destination,
652 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
653 ch->ctn = GCT_add_channel (ch->t,
656 GNUNET_STATISTICS_update (stats,
660 LOG (GNUNET_ERROR_TYPE_DEBUG,
661 "Created channel to port %s at peer %s for %s using %s\n",
663 GCP_2s (destination),
665 (GNUNET_YES == ch->is_loopback) ? "loopback" : GCT_2s (ch->t));
671 * We had an incoming channel to a port that is closed.
672 * It has not been opened for a while, drop it.
674 * @param cls the channel to drop
677 timeout_closed_cb (void *cls)
679 struct CadetChannel *ch = cls;
681 ch->retry_control_task = NULL;
682 LOG (GNUNET_ERROR_TYPE_DEBUG,
683 "Closing incoming channel to port %s from peer %s due to timeout\n",
684 GNUNET_h2s (&ch->port),
685 GCP_2s (GCT_get_destination (ch->t)));
686 channel_destroy (ch);
691 * Create a new channel based on a request coming in over the network.
693 * @param t tunnel to the remote peer
694 * @param ctn identifier of this channel in the tunnel
695 * @param port desired local port
696 * @param options options for the channel
697 * @return handle to the new channel
699 struct CadetChannel *
700 GCCH_channel_incoming_new (struct CadetTunnel *t,
701 struct GNUNET_CADET_ChannelTunnelNumber ctn,
702 const struct GNUNET_HashCode *port,
705 struct CadetChannel *ch;
706 struct CadetClient *c;
708 ch = GNUNET_new (struct CadetChannel);
712 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
713 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
714 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
715 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
716 ch->max_pending_messages = (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
717 GNUNET_STATISTICS_update (stats,
722 c = GNUNET_CONTAINER_multihashmap_get (open_ports,
726 /* port closed, wait for it to possibly open */
727 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
730 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
731 ch->retry_control_task
732 = GNUNET_SCHEDULER_add_delayed (TIMEOUT_CLOSED_PORT,
735 LOG (GNUNET_ERROR_TYPE_DEBUG,
736 "Created loose incoming channel to port %s from peer %s\n",
737 GNUNET_h2s (&ch->port),
738 GCP_2s (GCT_get_destination (ch->t)));
745 GNUNET_STATISTICS_update (stats,
754 * Function called once the tunnel confirms that we sent the
755 * ACK message. Just remembers it was sent, we do not expect
758 * @param cls our `struct CadetChannel`.
761 send_ack_cb (void *cls)
763 struct CadetChannel *ch = cls;
765 GNUNET_assert (NULL != ch->last_control_qe);
766 ch->last_control_qe = NULL;
771 * Compute and send the current #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK to the other peer.
773 * @param ch channel to send the #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK for
776 send_channel_data_ack (struct CadetChannel *ch)
778 struct GNUNET_CADET_ChannelDataAckMessage msg;
780 if (GNUNET_NO == ch->reliable)
781 return; /* no ACKs */
782 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK);
783 msg.header.size = htons (sizeof (msg));
785 msg.mid.mid = htonl (ntohl (ch->mid_recv.mid));
786 msg.futures = GNUNET_htonll (ch->mid_futures);
787 if (NULL != ch->last_control_qe)
788 GCT_send_cancel (ch->last_control_qe);
789 LOG (GNUNET_ERROR_TYPE_DEBUG,
790 "Sending DATA_ACK %u:%llX via %s\n",
791 (unsigned int) ntohl (msg.mid.mid),
792 (unsigned long long) ch->mid_futures,
794 ch->last_control_qe = GCT_send (ch->t,
802 * Send our initial #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK to the client confirming that the
805 * @param cls the `struct CadetChannel`
808 send_open_ack (void *cls)
810 struct CadetChannel *ch = cls;
811 struct GNUNET_CADET_ChannelManageMessage msg;
813 LOG (GNUNET_ERROR_TYPE_DEBUG,
814 "Sending CHANNEL_OPEN_ACK on %s\n",
816 ch->retry_control_task = NULL;
817 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK);
818 msg.header.size = htons (sizeof (msg));
819 msg.reserved = htonl (0);
821 if (NULL != ch->last_control_qe)
822 GCT_send_cancel (ch->last_control_qe);
823 ch->last_control_qe = GCT_send (ch->t,
831 * We got a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN message again for
832 * this channel. If the binding was successful, (re)transmit the
833 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK.
835 * @param ch channel that got the duplicate open
838 GCCH_handle_duplicate_open (struct CadetChannel *ch)
840 if (NULL == ch->dest)
842 LOG (GNUNET_ERROR_TYPE_DEBUG,
843 "Ignoring duplicate channel OPEN on %s: port is closed\n",
847 if (NULL != ch->retry_control_task)
849 LOG (GNUNET_ERROR_TYPE_DEBUG,
850 "Ignoring duplicate channel OPEN on %s: control message is pending\n",
854 LOG (GNUNET_ERROR_TYPE_DEBUG,
855 "Retransmitting OPEN_ACK on %s\n",
857 ch->retry_control_task
858 = GNUNET_SCHEDULER_add_now (&send_open_ack,
864 * Send a #GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK to the client to solicit more messages.
866 * @param ch channel the ack is for
867 * @param to_owner #GNUNET_YES to send to owner,
868 * #GNUNET_NO to send to dest
871 send_ack_to_client (struct CadetChannel *ch,
874 struct GNUNET_MQ_Envelope *env;
875 struct GNUNET_CADET_LocalAck *ack;
876 struct CadetChannelClient *ccc;
878 ccc = (GNUNET_YES == to_owner) ? ch->owner : ch->dest;
881 /* This can happen if we are just getting ACKs after
882 our local client already disconnected. */
883 GNUNET_assert (GNUNET_YES == ch->destroy);
886 env = GNUNET_MQ_msg (ack,
887 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
889 LOG (GNUNET_ERROR_TYPE_DEBUG,
890 "Sending CADET_LOCAL_ACK to %s (%s) at ccn %X (%u/%u pending)\n",
892 (GNUNET_YES == to_owner) ? "owner" : "dest",
893 ntohl (ack->ccn.channel_of_client),
894 ch->pending_messages,
895 ch->max_pending_messages);
896 GSC_send_to_client (ccc->c,
902 * A client is bound to the port that we have a channel
903 * open to. Send the acknowledgement for the connection
904 * request and establish the link with the client.
906 * @param ch open incoming channel
907 * @param c client listening on the respective port
910 GCCH_bind (struct CadetChannel *ch,
911 struct CadetClient *c)
914 struct CadetChannelClient *cccd;
916 LOG (GNUNET_ERROR_TYPE_DEBUG,
917 "Binding %s from %s to port %s of %s\n",
920 GNUNET_h2s (&ch->port),
922 if (NULL != ch->retry_control_task)
924 /* there might be a timeout task here */
925 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
926 ch->retry_control_task = NULL;
930 options |= GNUNET_CADET_OPTION_NOBUFFER;
932 options |= GNUNET_CADET_OPTION_RELIABLE;
933 if (ch->out_of_order)
934 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
935 cccd = GNUNET_new (struct CadetChannelClient);
938 cccd->client_ready = GNUNET_YES;
939 cccd->ccn = GSC_bind (c,
941 (GNUNET_YES == ch->is_loopback)
942 ? GCP_get (&my_full_id,
944 : GCT_get_destination (ch->t),
947 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) <
948 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
949 ch->mid_recv.mid = htonl (1); /* The OPEN counts as message 0! */
950 if (GNUNET_YES == ch->is_loopback)
952 ch->state = CADET_CHANNEL_OPEN_SENT;
953 GCCH_handle_channel_open_ack (ch);
957 /* notify other peer that we accepted the connection */
958 ch->retry_control_task
959 = GNUNET_SCHEDULER_add_now (&send_open_ack,
962 /* give client it's initial supply of ACKs */
963 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) <
964 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
965 for (unsigned int i=0;i<ch->max_pending_messages;i++)
966 send_ack_to_client (ch,
972 * Destroy locally created channel. Called by the local client, so no
973 * need to tell the client.
975 * @param ch channel to destroy
976 * @param c client that caused the destruction
977 * @param ccn client number of the client @a c
980 GCCH_channel_local_destroy (struct CadetChannel *ch,
981 struct CadetClient *c,
982 struct GNUNET_CADET_ClientChannelNumber ccn)
984 LOG (GNUNET_ERROR_TYPE_DEBUG,
985 "%s asks for destruction of %s\n",
988 GNUNET_assert (NULL != c);
989 if ( (NULL != ch->owner) &&
990 (c == ch->owner->c) &&
991 (ccn.channel_of_client == ch->owner->ccn.channel_of_client) )
993 free_channel_client (ch->owner);
996 else if ( (NULL != ch->dest) &&
997 (c == ch->dest->c) &&
998 (ccn.channel_of_client == ch->dest->ccn.channel_of_client) )
1000 free_channel_client (ch->dest);
1008 if (GNUNET_YES == ch->destroy)
1010 /* other end already destroyed, with the local client gone, no need
1011 to finish transmissions, just destroy immediately. */
1012 channel_destroy (ch);
1015 if ( (NULL != ch->head_sent) ||
1016 (NULL != ch->owner) ||
1017 (NULL != ch->dest) )
1019 /* Wait for other end to destroy us as well,
1020 and otherwise allow send queue to be transmitted first */
1021 ch->destroy = GNUNET_YES;
1024 /* If the we ever sent the CHANNEL_CREATE, we need to send a destroy message. */
1025 if (CADET_CHANNEL_NEW != ch->state)
1026 GCT_send_channel_destroy (ch->t,
1028 /* Nothing left to do, just finish destruction */
1029 channel_destroy (ch);
1034 * We got an acknowledgement for the creation of the channel
1035 * (the port is open on the other side). Begin transmissions.
1037 * @param ch channel to destroy
1040 GCCH_handle_channel_open_ack (struct CadetChannel *ch)
1044 case CADET_CHANNEL_NEW:
1045 /* this should be impossible */
1048 case CADET_CHANNEL_OPEN_SENT:
1049 if (NULL == ch->owner)
1051 /* We're not the owner, wrong direction! */
1052 GNUNET_break_op (0);
1055 LOG (GNUNET_ERROR_TYPE_DEBUG,
1056 "Received CHANNEL_OPEN_ACK for waiting %s, entering READY state\n",
1058 if (NULL != ch->retry_control_task) /* can be NULL if ch->is_loopback */
1060 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
1061 ch->retry_control_task = NULL;
1063 ch->state = CADET_CHANNEL_READY;
1064 /* On first connect, send client as many ACKs as we allow messages
1066 for (unsigned int i=0;i<ch->max_pending_messages;i++)
1067 send_ack_to_client (ch,
1070 case CADET_CHANNEL_READY:
1071 /* duplicate ACK, maybe we retried the CREATE. Ignore. */
1072 LOG (GNUNET_ERROR_TYPE_DEBUG,
1073 "Received duplicate channel OPEN_ACK for %s\n",
1075 GNUNET_STATISTICS_update (stats,
1076 "# duplicate CREATE_ACKs",
1085 * Test if element @a e1 comes before element @a e2.
1087 * @param cls closure, to a flag where we indicate duplicate packets
1088 * @param e1 an element of to sort
1089 * @param e2 another element to sort
1090 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1093 is_before (void *cls,
1094 struct CadetOutOfOrderMessage *m1,
1095 struct CadetOutOfOrderMessage *m2)
1097 int *duplicate = cls;
1098 uint32_t v1 = ntohl (m1->mid.mid);
1099 uint32_t v2 = ntohl (m2->mid.mid);
1104 *duplicate = GNUNET_YES;
1105 if (delta > (uint32_t) INT_MAX)
1107 /* in overflow range, we can safely assume we wrapped around */
1112 /* result is small, thus v2 > v1, thus e1 < e2 */
1119 * We got payload data for a channel. Pass it on to the client
1120 * and send an ACK to the other end (once flow control allows it!)
1122 * @param ch channel that got data
1123 * @param msg message that was received
1126 GCCH_handle_channel_plaintext_data (struct CadetChannel *ch,
1127 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
1129 struct GNUNET_MQ_Envelope *env;
1130 struct GNUNET_CADET_LocalData *ld;
1131 struct CadetChannelClient *ccc;
1132 size_t payload_size;
1133 struct CadetOutOfOrderMessage *com;
1140 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1141 if ( (GNUNET_YES == ch->destroy) &&
1142 (NULL == ch->owner) &&
1143 (NULL == ch->dest) )
1145 /* This client is gone, but we still have messages to send to
1146 the other end (which is why @a ch is not yet dead). However,
1147 we cannot pass messages to our client anymore. */
1148 LOG (GNUNET_ERROR_TYPE_DEBUG,
1149 "Dropping incoming payload on %s as this end is already closed\n",
1151 /* send back DESTROY notification to stop further retransmissions! */
1152 GCT_send_channel_destroy (ch->t,
1156 payload_size = ntohs (msg->header.size) - sizeof (*msg);
1157 env = GNUNET_MQ_msg_extra (ld,
1159 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1160 ld->ccn = (NULL == ch->dest) ? ch->owner->ccn : ch->dest->ccn;
1161 GNUNET_memcpy (&ld[1],
1164 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1165 if ( (GNUNET_YES == ccc->client_ready) &&
1166 ( (GNUNET_YES == ch->out_of_order) ||
1167 (msg->mid.mid == ch->mid_recv.mid) ) )
1169 LOG (GNUNET_ERROR_TYPE_DEBUG,
1170 "Giving %u bytes of payload with MID %u from %s to client %s\n",
1171 (unsigned int) payload_size,
1172 ntohl (msg->mid.mid),
1175 ccc->client_ready = GNUNET_NO;
1176 GSC_send_to_client (ccc->c,
1178 ch->mid_recv.mid = htonl (1 + ntohl (ch->mid_recv.mid));
1179 ch->mid_futures >>= 1;
1180 send_channel_data_ack (ch);
1184 if (GNUNET_YES == ch->reliable)
1186 /* check if message ought to be dropped because it is ancient/too distant/duplicate */
1187 mid_min = ntohl (ch->mid_recv.mid);
1188 mid_max = mid_min + ch->max_pending_messages;
1189 mid_msg = ntohl (msg->mid.mid);
1190 if ( ( (uint32_t) (mid_msg - mid_min) > ch->max_pending_messages) ||
1191 ( (uint32_t) (mid_max - mid_msg) > ch->max_pending_messages) )
1193 LOG (GNUNET_ERROR_TYPE_DEBUG,
1194 "%s at %u drops ancient or far-future message %u\n",
1196 (unsigned int) mid_min,
1197 ntohl (msg->mid.mid));
1199 GNUNET_STATISTICS_update (stats,
1200 "# duplicate DATA (ancient or future)",
1203 GNUNET_MQ_discard (env);
1204 send_channel_data_ack (ch);
1207 /* mark bit for future ACKs */
1208 delta = mid_msg - mid_min - 1; /* overflow/underflow are OK here */
1211 if (0 != (ch->mid_futures & (1LLU << delta)))
1213 /* Duplicate within the queue, drop also */
1214 LOG (GNUNET_ERROR_TYPE_DEBUG,
1215 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1216 (unsigned int) payload_size,
1218 ntohl (msg->mid.mid));
1219 GNUNET_STATISTICS_update (stats,
1223 GNUNET_MQ_discard (env);
1224 send_channel_data_ack (ch);
1227 ch->mid_futures |= (1LLU << delta);
1228 LOG (GNUNET_ERROR_TYPE_DEBUG,
1229 "Marked bit %llX for mid %u (base: %u); now: %llX\n",
1236 else /* ! ch->reliable */
1238 /* Channel is unreliable, so we do not ACK. But we also cannot
1239 allow buffering everything, so check if we have space... */
1240 if (ccc->num_recv >= ch->max_pending_messages)
1242 struct CadetOutOfOrderMessage *drop;
1244 /* Yep, need to drop. Drop the oldest message in
1246 drop = ccc->head_recv;
1247 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1251 GNUNET_MQ_discard (drop->env);
1256 /* Insert message into sorted out-of-order queue */
1257 com = GNUNET_new (struct CadetOutOfOrderMessage);
1258 com->mid = msg->mid;
1260 duplicate = GNUNET_NO;
1261 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetOutOfOrderMessage,
1268 if (GNUNET_YES == duplicate)
1270 /* Duplicate within the queue, drop also (this is not covered by
1271 the case above if "delta" >= 64, which could be the case if
1272 max_pending_messages is also >= 64 or if our client is unready
1273 and we are seeing retransmissions of the message our client is
1275 LOG (GNUNET_ERROR_TYPE_DEBUG,
1276 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1277 (unsigned int) payload_size,
1279 ntohl (msg->mid.mid));
1280 GNUNET_STATISTICS_update (stats,
1284 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1288 GNUNET_MQ_discard (com->env);
1290 send_channel_data_ack (ch);
1293 LOG (GNUNET_ERROR_TYPE_DEBUG,
1294 "Queued %s payload of %u bytes on %s-%X(%p) (mid %u, need %u first)\n",
1295 (GNUNET_YES == ccc->client_ready)
1297 : "client-not-ready",
1298 (unsigned int) payload_size,
1300 ntohl (ccc->ccn.channel_of_client),
1302 ntohl (msg->mid.mid),
1303 ntohl (ch->mid_recv.mid));
1304 /* NOTE: this ACK we _could_ skip, as the packet is out-of-order and
1305 the sender may already be transmitting the previous one. Needs
1306 experimental evaluation to see if/when this ACK helps or
1307 hurts. (We might even want another option.) */
1308 send_channel_data_ack (ch);
1313 * Function called once the tunnel has sent one of our messages.
1314 * If the message is unreliable, simply frees the `crm`. If the
1315 * message was reliable, calculate retransmission time and
1316 * wait for ACK (or retransmit).
1318 * @param cls the `struct CadetReliableMessage` that was sent
1321 data_sent_cb (void *cls);
1325 * We need to retry a transmission, the last one took too long to
1328 * @param cls the `struct CadetChannel` where we need to retransmit
1331 retry_transmission (void *cls)
1333 struct CadetChannel *ch = cls;
1334 struct CadetReliableMessage *crm = ch->head_sent;
1336 ch->retry_data_task = NULL;
1337 GNUNET_assert (NULL == crm->qe);
1338 LOG (GNUNET_ERROR_TYPE_DEBUG,
1339 "Retrying transmission on %s of message %u\n",
1341 (unsigned int) ntohl (crm->data_message->mid.mid));
1342 crm->qe = GCT_send (ch->t,
1343 &crm->data_message->header,
1346 GNUNET_assert (NULL == ch->retry_data_task);
1351 * We got an PLAINTEXT_DATA_ACK for a message in our queue, remove it from
1352 * the queue and tell our client that it can send more.
1354 * @param ch the channel that got the PLAINTEXT_DATA_ACK
1355 * @param crm the message that got acknowledged
1358 handle_matching_ack (struct CadetChannel *ch,
1359 struct CadetReliableMessage *crm)
1361 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1364 ch->pending_messages--;
1365 GNUNET_assert (ch->pending_messages < ch->max_pending_messages);
1366 LOG (GNUNET_ERROR_TYPE_DEBUG,
1367 "Received DATA_ACK on %s for message %u (%u ACKs pending)\n",
1369 (unsigned int) ntohl (crm->data_message->mid.mid),
1370 ch->pending_messages);
1371 if (NULL != crm->qe)
1373 GCT_send_cancel (crm->qe);
1376 GNUNET_free (crm->data_message);
1378 send_ack_to_client (ch,
1386 * We got an acknowledgement for payload data for a channel.
1387 * Possibly resume transmissions.
1389 * @param ch channel that got the ack
1390 * @param ack details about what was received
1393 GCCH_handle_channel_plaintext_data_ack (struct CadetChannel *ch,
1394 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
1396 struct CadetReliableMessage *crm;
1397 struct CadetReliableMessage *crmn;
1403 GNUNET_break (GNUNET_NO == ch->is_loopback);
1404 if (GNUNET_NO == ch->reliable)
1406 /* not expecting ACKs on unreliable channel, odd */
1407 GNUNET_break_op (0);
1410 /* mid_base is the MID of the next message that the
1411 other peer expects (i.e. that is missing!), everything
1412 LOWER (but excluding mid_base itself) was received. */
1413 mid_base = ntohl (ack->mid.mid);
1414 mid_mask = GNUNET_htonll (ack->futures);
1416 for (crm = ch->head_sent;
1421 delta = (unsigned int) (ntohl (crm->data_message->mid.mid) - mid_base);
1422 if (delta >= UINT_MAX - ch->max_pending_messages)
1424 /* overflow, means crm was a bit in the past, so this ACK counts for it. */
1425 LOG (GNUNET_ERROR_TYPE_DEBUG,
1426 "Got DATA_ACK with base %u satisfying past message %u on %s\n",
1427 (unsigned int) mid_base,
1428 ntohl (crm->data_message->mid.mid),
1430 handle_matching_ack (ch,
1438 LOG (GNUNET_ERROR_TYPE_DEBUG,
1439 "Testing bit %llX for mid %u (base: %u)\n",
1441 ntohl (crm->data_message->mid.mid),
1443 if (0 != (mid_mask & (1LLU << delta)))
1445 LOG (GNUNET_ERROR_TYPE_DEBUG,
1446 "Got DATA_ACK with mask for %u on %s\n",
1447 ntohl (crm->data_message->mid.mid),
1449 handle_matching_ack (ch,
1454 if (GNUNET_NO == found)
1456 /* ACK for message we already dropped, might have been a
1457 duplicate ACK? Ignore. */
1458 LOG (GNUNET_ERROR_TYPE_DEBUG,
1459 "Duplicate DATA_ACK on %s, ignoring\n",
1461 GNUNET_STATISTICS_update (stats,
1462 "# duplicate DATA_ACKs",
1467 if (NULL != ch->retry_data_task)
1469 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1470 ch->retry_data_task = NULL;
1472 if ( (NULL != ch->head_sent) &&
1473 (NULL == ch->head_sent->qe) )
1475 = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry,
1476 &retry_transmission,
1482 * Destroy channel, based on the other peer closing the
1483 * connection. Also needs to remove this channel from
1486 * @param ch channel to destroy
1489 GCCH_handle_remote_destroy (struct CadetChannel *ch)
1491 struct CadetChannelClient *ccc;
1493 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1494 LOG (GNUNET_ERROR_TYPE_DEBUG,
1495 "Received remote channel DESTROY for %s\n",
1497 if (GNUNET_YES == ch->destroy)
1499 /* Local client already gone, this is instant-death. */
1500 channel_destroy (ch);
1503 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1504 if (NULL != ccc->head_recv)
1506 LOG (GNUNET_ERROR_TYPE_WARNING,
1507 "Lost end of transmission due to remote shutdown on %s\n",
1509 /* FIXME: change API to notify client about truncated transmission! */
1511 ch->destroy = GNUNET_YES;
1512 GSC_handle_remote_channel_destroy (ccc->c,
1515 channel_destroy (ch);
1520 * Test if element @a e1 comes before element @a e2.
1522 * @param cls closure, to a flag where we indicate duplicate packets
1523 * @param crm1 an element of to sort
1524 * @param crm2 another element to sort
1525 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1528 cmp_crm_by_next_retry (void *cls,
1529 struct CadetReliableMessage *crm1,
1530 struct CadetReliableMessage *crm2)
1532 if (crm1->next_retry.abs_value_us <
1533 crm2->next_retry.abs_value_us)
1540 * Function called once the tunnel has sent one of our messages.
1541 * If the message is unreliable, simply frees the `crm`. If the
1542 * message was reliable, calculate retransmission time and
1543 * wait for ACK (or retransmit).
1545 * @param cls the `struct CadetReliableMessage` that was sent
1548 data_sent_cb (void *cls)
1550 struct CadetReliableMessage *crm = cls;
1551 struct CadetChannel *ch = crm->ch;
1553 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1554 GNUNET_assert (NULL != crm->qe);
1556 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1559 if (GNUNET_NO == ch->reliable)
1561 GNUNET_free (crm->data_message);
1563 ch->pending_messages--;
1564 send_ack_to_client (ch,
1570 if (0 == crm->retry_delay.rel_value_us)
1571 crm->retry_delay = ch->expected_delay;
1573 crm->retry_delay = GNUNET_TIME_STD_BACKOFF (crm->retry_delay);
1574 crm->retry_delay = GNUNET_TIME_relative_max (crm->retry_delay,
1576 crm->next_retry = GNUNET_TIME_relative_to_absolute (crm->retry_delay);
1578 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetReliableMessage,
1579 cmp_crm_by_next_retry,
1584 LOG (GNUNET_ERROR_TYPE_DEBUG,
1585 "Message %u sent, next transmission on %s in %s\n",
1586 (unsigned int) ntohl (crm->data_message->mid.mid),
1588 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (ch->head_sent->next_retry),
1590 if (NULL == ch->head_sent->qe)
1592 if (NULL != ch->retry_data_task)
1593 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1595 = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry,
1596 &retry_transmission,
1603 * Handle data given by a client.
1605 * Check whether the client is allowed to send in this tunnel, save if
1606 * channel is reliable and send an ACK to the client if there is still
1607 * buffer space in the tunnel.
1609 * @param ch Channel.
1610 * @param sender_ccn ccn of the sender
1611 * @param buf payload to transmit.
1612 * @param buf_len number of bytes in @a buf
1613 * @return #GNUNET_OK if everything goes well,
1614 * #GNUNET_SYSERR in case of an error.
1617 GCCH_handle_local_data (struct CadetChannel *ch,
1618 struct GNUNET_CADET_ClientChannelNumber sender_ccn,
1622 struct CadetReliableMessage *crm;
1624 if (ch->pending_messages > ch->max_pending_messages)
1627 return GNUNET_SYSERR;
1629 ch->pending_messages++;
1631 if (GNUNET_YES == ch->is_loopback)
1633 struct CadetChannelClient *receiver;
1634 struct GNUNET_MQ_Envelope *env;
1635 struct GNUNET_CADET_LocalData *ld;
1638 env = GNUNET_MQ_msg_extra (ld,
1640 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1641 if (sender_ccn.channel_of_client ==
1642 ch->owner->ccn.channel_of_client)
1644 receiver = ch->dest;
1645 to_owner = GNUNET_NO;
1649 GNUNET_assert (sender_ccn.channel_of_client ==
1650 ch->dest->ccn.channel_of_client);
1651 receiver = ch->owner;
1652 to_owner = GNUNET_YES;
1654 ld->ccn = receiver->ccn;
1655 GNUNET_memcpy (&ld[1],
1658 if (GNUNET_YES == receiver->client_ready)
1660 GSC_send_to_client (receiver->c,
1662 send_ack_to_client (ch,
1667 struct CadetOutOfOrderMessage *oom;
1669 oom = GNUNET_new (struct CadetOutOfOrderMessage);
1671 GNUNET_CONTAINER_DLL_insert_tail (receiver->head_recv,
1672 receiver->tail_recv,
1674 receiver->num_recv++;
1679 /* Everything is correct, send the message. */
1680 crm = GNUNET_malloc (sizeof (*crm));
1682 crm->data_message = GNUNET_malloc (sizeof (struct GNUNET_CADET_ChannelAppDataMessage)
1684 crm->data_message->header.size = htons (sizeof (struct GNUNET_CADET_ChannelAppDataMessage) + buf_len);
1685 crm->data_message->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA);
1686 ch->mid_send.mid = htonl (ntohl (ch->mid_send.mid) + 1);
1687 crm->data_message->mid = ch->mid_send;
1688 crm->data_message->ctn = ch->ctn;
1689 GNUNET_memcpy (&crm->data_message[1],
1692 GNUNET_CONTAINER_DLL_insert_tail (ch->head_sent,
1695 LOG (GNUNET_ERROR_TYPE_DEBUG,
1696 "Sending message %u from local client to %s with %u bytes\n",
1697 ntohl (crm->data_message->mid.mid),
1700 if (NULL != ch->retry_data_task)
1702 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1703 ch->retry_data_task = NULL;
1705 crm->qe = GCT_send (ch->t,
1706 &crm->data_message->header,
1709 GNUNET_assert (NULL == ch->retry_data_task);
1715 * Handle ACK from client on local channel. Means the client is ready
1716 * for more data, see if we have any for it.
1718 * @param ch channel to destroy
1719 * @param client_ccn ccn of the client sending the ack
1722 GCCH_handle_local_ack (struct CadetChannel *ch,
1723 struct GNUNET_CADET_ClientChannelNumber client_ccn)
1725 struct CadetChannelClient *ccc;
1726 struct CadetOutOfOrderMessage *com;
1728 if ( (NULL != ch->owner) &&
1729 (ch->owner->ccn.channel_of_client == client_ccn.channel_of_client) )
1731 else if ( (NULL != ch->dest) &&
1732 (ch->dest->ccn.channel_of_client == client_ccn.channel_of_client) )
1736 ccc->client_ready = GNUNET_YES;
1737 com = ccc->head_recv;
1740 LOG (GNUNET_ERROR_TYPE_DEBUG,
1741 "Got LOCAL_ACK, %s-%X ready to receive more data, but none pending on %s-%X(%p)!\n",
1743 ntohl (client_ccn.channel_of_client),
1745 ntohl (ccc->ccn.channel_of_client),
1747 return; /* none pending */
1749 if (GNUNET_YES == ch->is_loopback)
1753 /* Messages are always in-order, just send */
1754 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1758 GSC_send_to_client (ccc->c,
1760 /* Notify sender that we can receive more */
1761 if (ccc->ccn.channel_of_client ==
1762 ch->owner->ccn.channel_of_client)
1764 to_owner = GNUNET_NO;
1768 GNUNET_assert (ccc->ccn.channel_of_client ==
1769 ch->dest->ccn.channel_of_client);
1770 to_owner = GNUNET_YES;
1772 send_ack_to_client (ch,
1778 if ( (com->mid.mid != ch->mid_recv.mid) &&
1779 (GNUNET_NO == ch->out_of_order) )
1781 LOG (GNUNET_ERROR_TYPE_DEBUG,
1782 "Got LOCAL_ACK, %s-%X ready to receive more data (but next one is out-of-order %u vs. %u)!\n",
1784 ntohl (ccc->ccn.channel_of_client),
1785 ntohl (com->mid.mid),
1786 ntohl (ch->mid_recv.mid));
1787 return; /* missing next one in-order */
1790 LOG (GNUNET_ERROR_TYPE_DEBUG,
1791 "Got LOCAL_ACK, giving payload message %u to %s-%X on %s\n",
1792 ntohl (com->mid.mid),
1794 ntohl (ccc->ccn.channel_of_client),
1797 /* all good, pass next message to client */
1798 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1802 /* FIXME: if unreliable, this is not aggressive
1803 enough, as it would be OK to have lost some! */
1805 ch->mid_recv.mid = htonl (1 + ntohl (com->mid.mid));
1806 ch->mid_futures >>= 1; /* equivalent to division by 2 */
1807 ccc->client_ready = GNUNET_NO;
1808 GSC_send_to_client (ccc->c,
1811 send_channel_data_ack (ch);
1812 if (NULL != ccc->head_recv)
1814 if (GNUNET_NO == ch->destroy)
1816 GCT_send_channel_destroy (ch->t,
1818 channel_destroy (ch);
1822 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-chn",__VA_ARGS__)
1828 * @param ch Channel.
1829 * @param level Debug level to use.
1832 GCCH_debug (struct CadetChannel *ch,
1833 enum GNUNET_ErrorType level)
1837 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
1839 __FILE__, __FUNCTION__, __LINE__);
1845 LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
1853 if (NULL != ch->owner)
1856 "CHN origin %s ready %s local-id: %u\n",
1857 GSC_2s (ch->owner->c),
1858 ch->owner->client_ready ? "YES" : "NO",
1859 ntohl (ch->owner->ccn.channel_of_client));
1861 if (NULL != ch->dest)
1864 "CHN destination %s ready %s local-id: %u\n",
1865 GSC_2s (ch->dest->c),
1866 ch->dest->client_ready ? "YES" : "NO",
1867 ntohl (ch->dest->ccn.channel_of_client));
1870 "CHN Message IDs recv: %d (%LLX), send: %d\n",
1871 ntohl (ch->mid_recv.mid),
1872 (unsigned long long) ch->mid_futures,
1873 ntohl (ch->mid_send.mid));
1878 /* end of gnunet-service-cadet-new_channel.c */