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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/gnunet-service-cadet-new_channel.c
22 * @brief logical links between CADET clients
23 * @author Bartlomiej Polot
24 * @author Christian Grothoff
27 * - Congestion/flow control:
28 * + estimate max bandwidth using bursts and use to for CONGESTION CONTROL!
29 * (and figure out how/where to use this!)
30 * + figure out flow control without ACKs (unreliable traffic!)
31 * - revisit handling of 'unbuffered' traffic!
32 * (need to push down through tunnel into connection selection)
33 * - revisit handling of 'buffered' traffic: 4 is a rather small buffer; maybe
34 * reserve more bits in 'options' to allow for buffer size control?
37 #include "gnunet_util_lib.h"
39 #include "gnunet_statistics_service.h"
40 #include "gnunet-service-cadet-new.h"
41 #include "gnunet-service-cadet-new_channel.h"
42 #include "gnunet-service-cadet-new_connection.h"
43 #include "gnunet-service-cadet-new_tunnels.h"
44 #include "gnunet-service-cadet-new_peer.h"
45 #include "gnunet-service-cadet-new_paths.h"
47 #define LOG(level,...) GNUNET_log_from (level,"cadet-chn",__VA_ARGS__)
50 * How long do we initially wait before retransmitting?
52 #define CADET_INITIAL_RETRANSMIT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250)
55 * How long do we wait before dropping state about incoming
56 * connection to closed port?
58 #define TIMEOUT_CLOSED_PORT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
61 * How long do we wait at least before retransmitting ever?
63 #define MIN_RTT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 75)
66 * Maximum message ID into the future we accept for out-of-order messages.
67 * If the message is more than this into the future, we drop it. This is
68 * important both to detect values that are actually in the past, as well
69 * as to limit adversarially triggerable memory consumption.
71 * Note that right now we have "max_pending_messages = 4" hard-coded in
72 * the logic below, so a value of 4 would suffice here. But we plan to
73 * allow larger windows in the future...
75 #define MAX_OUT_OF_ORDER_DISTANCE 1024
79 * All the states a connection can be in.
81 enum CadetChannelState
84 * Uninitialized status, should never appear in operation.
89 * Connection create message sent, waiting for ACK.
91 CADET_CHANNEL_OPEN_SENT,
94 * Connection confirmed, ready to carry traffic.
101 * Info needed to retry a message in case it gets lost.
102 * Note that we DO use this structure also for unreliable
105 struct CadetReliableMessage
108 * Double linked list, FIFO style
110 struct CadetReliableMessage *next;
113 * Double linked list, FIFO style
115 struct CadetReliableMessage *prev;
118 * Which channel is this message in?
120 struct CadetChannel *ch;
123 * Entry in the tunnels queue for this message, NULL if it has left
124 * the tunnel. Used to cancel transmission in case we receive an
127 struct CadetTunnelQueueEntry *qe;
130 * Data message we are trying to send.
132 struct GNUNET_CADET_ChannelAppDataMessage *data_message;
135 * How soon should we retry if we fail to get an ACK?
136 * Messages in the queue are sorted by this value.
138 struct GNUNET_TIME_Absolute next_retry;
141 * How long do we wait for an ACK after transmission?
142 * Use for the back-off calculation.
144 struct GNUNET_TIME_Relative retry_delay;
147 * Time when we first successfully transmitted the message
148 * (that is, set @e num_transmissions to 1).
150 struct GNUNET_TIME_Absolute first_transmission_time;
153 * Identifier of the connection that this message took when it
154 * was first transmitted. Only useful if @e num_transmissions is 1.
156 struct GNUNET_CADET_ConnectionTunnelIdentifier connection_taken;
159 * How often was this message transmitted? #GNUNET_SYSERR if there
160 * was an error transmitting the message, #GNUNET_NO if it was not
161 * yet transmitted ever, otherwise the number of (re) transmissions.
163 int num_transmissions;
169 * List of received out-of-order data messages.
171 struct CadetOutOfOrderMessage
174 * Double linked list, FIFO style
176 struct CadetOutOfOrderMessage *next;
179 * Double linked list, FIFO style
181 struct CadetOutOfOrderMessage *prev;
184 * ID of the message (messages up to this point needed
185 * before we give this one to the client).
187 struct ChannelMessageIdentifier mid;
190 * The envelope with the payload of the out-of-order message
192 struct GNUNET_MQ_Envelope *env;
198 * Client endpoint of a `struct CadetChannel`. A channel may be a
199 * loopback channel, in which case it has two of these endpoints.
200 * Note that flow control also is required in both directions.
202 struct CadetChannelClient
205 * Client handle. Not by itself sufficient to designate
206 * the client endpoint, as the same client handle may
207 * be used for both the owner and the destination, and
208 * we thus also need the channel ID to identify the client.
210 struct CadetClient *c;
213 * Head of DLL of messages received out of order or while client was unready.
215 struct CadetOutOfOrderMessage *head_recv;
218 * Tail DLL of messages received out of order or while client was unready.
220 struct CadetOutOfOrderMessage *tail_recv;
223 * Local tunnel number for this client.
224 * (if owner >= #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI,
225 * otherwise < #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
227 struct GNUNET_CADET_ClientChannelNumber ccn;
230 * Number of entries currently in @a head_recv DLL.
232 unsigned int num_recv;
235 * Can we send data to the client?
243 * Struct containing all information regarding a channel to a remote client.
248 * Tunnel this channel is in.
250 struct CadetTunnel *t;
253 * Client owner of the tunnel, if any.
254 * (Used if this channel represends the initiating end of the tunnel.)
256 struct CadetChannelClient *owner;
259 * Client destination of the tunnel, if any.
260 * (Used if this channel represents the listening end of the tunnel.)
262 struct CadetChannelClient *dest;
265 * Last entry in the tunnel's queue relating to control messages
266 * (#GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN or
267 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK). Used to cancel
268 * transmission in case we receive updated information.
270 struct CadetTunnelQueueEntry *last_control_qe;
273 * Head of DLL of messages sent and not yet ACK'd.
275 struct CadetReliableMessage *head_sent;
278 * Tail of DLL of messages sent and not yet ACK'd.
280 struct CadetReliableMessage *tail_sent;
283 * Task to resend/poll in case no ACK is received.
285 struct GNUNET_SCHEDULER_Task *retry_control_task;
288 * Task to resend/poll in case no ACK is received.
290 struct GNUNET_SCHEDULER_Task *retry_data_task;
293 * Last time the channel was used
295 struct GNUNET_TIME_Absolute timestamp;
298 * Destination port of the channel.
300 struct GNUNET_HashCode port;
303 * Counter for exponential backoff.
305 struct GNUNET_TIME_Relative retry_time;
308 * Bitfield of already-received messages past @e mid_recv.
310 uint64_t mid_futures;
313 * Next MID expected for incoming traffic.
315 struct ChannelMessageIdentifier mid_recv;
318 * Next MID to use for outgoing traffic.
320 struct ChannelMessageIdentifier mid_send;
323 * Total (reliable) messages pending ACK for this channel.
325 unsigned int pending_messages;
328 * Maximum (reliable) messages pending ACK for this channel
329 * before we throttle the client.
331 unsigned int max_pending_messages;
334 * Number identifying this channel in its tunnel.
336 struct GNUNET_CADET_ChannelTunnelNumber ctn;
341 enum CadetChannelState state;
344 * Count how many ACKs we skipped, used to prevent long
345 * sequences of ACK skipping.
347 unsigned int skip_ack_series;
350 * Is the tunnel bufferless (minimum latency)?
355 * Is the tunnel reliable?
360 * Is the tunnel out-of-order?
365 * Is this channel a loopback channel, where the destination is us again?
370 * Flag to signal the destruction of the channel. If this is set to
371 * #GNUNET_YES the channel will be destroyed once the queue is
380 * Get the static string for identification of the channel.
384 * @return Static string with the channel IDs.
387 GCCH_2s (const struct CadetChannel *ch)
389 static char buf[128];
391 GNUNET_snprintf (buf,
393 "Channel %s:%s ctn:%X(%X/%X)",
394 (GNUNET_YES == ch->is_loopback)
396 : GNUNET_i2s (GCP_get_id (GCT_get_destination (ch->t))),
397 GNUNET_h2s (&ch->port),
399 (NULL == ch->owner) ? 0 : ntohl (ch->owner->ccn.channel_of_client),
400 (NULL == ch->dest) ? 0 : ntohl (ch->dest->ccn.channel_of_client));
406 * Get the channel's public ID.
410 * @return ID used to identify the channel with the remote peer.
412 struct GNUNET_CADET_ChannelTunnelNumber
413 GCCH_get_id (const struct CadetChannel *ch)
420 * Release memory associated with @a ccc
422 * @param ccc data structure to clean up
425 free_channel_client (struct CadetChannelClient *ccc)
427 struct CadetOutOfOrderMessage *com;
429 while (NULL != (com = ccc->head_recv))
431 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
435 GNUNET_MQ_discard (com->env);
443 * Destroy the given channel.
445 * @param ch channel to destroy
448 channel_destroy (struct CadetChannel *ch)
450 struct CadetReliableMessage *crm;
452 while (NULL != (crm = ch->head_sent))
454 GNUNET_assert (ch == crm->ch);
457 GCT_send_cancel (crm->qe);
460 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
463 GNUNET_free (crm->data_message);
466 if (NULL != ch->owner)
468 free_channel_client (ch->owner);
471 if (NULL != ch->dest)
473 free_channel_client (ch->dest);
476 if (NULL != ch->last_control_qe)
478 GCT_send_cancel (ch->last_control_qe);
479 ch->last_control_qe = NULL;
481 if (NULL != ch->retry_data_task)
483 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
484 ch->retry_data_task = NULL;
486 if (NULL != ch->retry_control_task)
488 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
489 ch->retry_control_task = NULL;
491 if (GNUNET_NO == ch->is_loopback)
493 GCT_remove_channel (ch->t,
503 * Send a channel create message.
505 * @param cls Channel for which to send.
508 send_channel_open (void *cls);
512 * Function called once the tunnel confirms that we sent the
513 * create message. Delays for a bit until we retry.
515 * @param cls our `struct CadetChannel`.
516 * @param cid identifier of the connection within the tunnel, NULL
517 * if transmission failed
520 channel_open_sent_cb (void *cls,
521 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
523 struct CadetChannel *ch = cls;
525 GNUNET_assert (NULL != ch->last_control_qe);
526 ch->last_control_qe = NULL;
527 ch->retry_time = GNUNET_TIME_STD_BACKOFF (ch->retry_time);
528 LOG (GNUNET_ERROR_TYPE_DEBUG,
529 "Sent CADET_CHANNEL_OPEN on %s, retrying in %s\n",
531 GNUNET_STRINGS_relative_time_to_string (ch->retry_time,
533 ch->retry_control_task
534 = GNUNET_SCHEDULER_add_delayed (ch->retry_time,
541 * Send a channel open message.
543 * @param cls Channel for which to send.
546 send_channel_open (void *cls)
548 struct CadetChannel *ch = cls;
549 struct GNUNET_CADET_ChannelOpenMessage msgcc;
552 ch->retry_control_task = NULL;
553 LOG (GNUNET_ERROR_TYPE_DEBUG,
554 "Sending CHANNEL_OPEN message for %s\n",
558 options |= GNUNET_CADET_OPTION_NOBUFFER;
560 options |= GNUNET_CADET_OPTION_RELIABLE;
561 if (ch->out_of_order)
562 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
563 msgcc.header.size = htons (sizeof (msgcc));
564 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
565 msgcc.opt = htonl (options);
566 msgcc.port = ch->port;
568 ch->state = CADET_CHANNEL_OPEN_SENT;
569 ch->last_control_qe = GCT_send (ch->t,
571 &channel_open_sent_cb,
573 GNUNET_assert (NULL == ch->retry_control_task);
578 * Function called once and only once after a channel was bound
579 * to its tunnel via #GCT_add_channel() is ready for transmission.
580 * Note that this is only the case for channels that this peer
581 * initiates, as for incoming channels we assume that they are
582 * ready for transmission immediately upon receiving the open
583 * message. Used to bootstrap the #GCT_send() process.
585 * @param ch the channel for which the tunnel is now ready
588 GCCH_tunnel_up (struct CadetChannel *ch)
590 GNUNET_assert (NULL == ch->retry_control_task);
591 LOG (GNUNET_ERROR_TYPE_DEBUG,
592 "Tunnel up, sending CHANNEL_OPEN on %s now\n",
594 ch->retry_control_task
595 = GNUNET_SCHEDULER_add_now (&send_channel_open,
601 * Create a new channel.
603 * @param owner local client owning the channel
604 * @param ccn local number of this channel at the @a owner
605 * @param destination peer to which we should build the channel
606 * @param port desired port at @a destination
607 * @param options options for the channel
608 * @return handle to the new channel
610 struct CadetChannel *
611 GCCH_channel_local_new (struct CadetClient *owner,
612 struct GNUNET_CADET_ClientChannelNumber ccn,
613 struct CadetPeer *destination,
614 const struct GNUNET_HashCode *port,
617 struct CadetChannel *ch;
618 struct CadetChannelClient *ccco;
620 ccco = GNUNET_new (struct CadetChannelClient);
623 ccco->client_ready = GNUNET_YES;
625 ch = GNUNET_new (struct CadetChannel);
626 ch->mid_recv.mid = htonl (1); /* The OPEN_ACK counts as message 0! */
627 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
628 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
629 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
630 ch->max_pending_messages = (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
633 if (0 == memcmp (&my_full_id,
634 GCP_get_id (destination),
635 sizeof (struct GNUNET_PeerIdentity)))
637 struct CadetClient *c;
639 ch->is_loopback = GNUNET_YES;
640 c = GNUNET_CONTAINER_multihashmap_get (open_ports,
644 /* port closed, wait for it to possibly open */
645 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
648 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
649 LOG (GNUNET_ERROR_TYPE_DEBUG,
650 "Created loose incoming loopback channel to port %s\n",
651 GNUNET_h2s (&ch->port));
655 ch->dest = GNUNET_new (struct CadetChannelClient);
657 ch->dest->client_ready = GNUNET_YES;
664 ch->t = GCP_get_tunnel (destination,
666 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
667 ch->ctn = GCT_add_channel (ch->t,
670 GNUNET_STATISTICS_update (stats,
674 LOG (GNUNET_ERROR_TYPE_DEBUG,
675 "Created channel to port %s at peer %s for %s using %s\n",
677 GCP_2s (destination),
679 (GNUNET_YES == ch->is_loopback) ? "loopback" : GCT_2s (ch->t));
685 * We had an incoming channel to a port that is closed.
686 * It has not been opened for a while, drop it.
688 * @param cls the channel to drop
691 timeout_closed_cb (void *cls)
693 struct CadetChannel *ch = cls;
695 ch->retry_control_task = NULL;
696 LOG (GNUNET_ERROR_TYPE_DEBUG,
697 "Closing incoming channel to port %s from peer %s due to timeout\n",
698 GNUNET_h2s (&ch->port),
699 GCP_2s (GCT_get_destination (ch->t)));
700 channel_destroy (ch);
705 * Create a new channel based on a request coming in over the network.
707 * @param t tunnel to the remote peer
708 * @param ctn identifier of this channel in the tunnel
709 * @param port desired local port
710 * @param options options for the channel
711 * @return handle to the new channel
713 struct CadetChannel *
714 GCCH_channel_incoming_new (struct CadetTunnel *t,
715 struct GNUNET_CADET_ChannelTunnelNumber ctn,
716 const struct GNUNET_HashCode *port,
719 struct CadetChannel *ch;
720 struct CadetClient *c;
722 ch = GNUNET_new (struct CadetChannel);
726 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
727 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
728 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
729 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
730 ch->max_pending_messages = (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
731 GNUNET_STATISTICS_update (stats,
736 c = GNUNET_CONTAINER_multihashmap_get (open_ports,
740 /* port closed, wait for it to possibly open */
741 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
744 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
745 ch->retry_control_task
746 = GNUNET_SCHEDULER_add_delayed (TIMEOUT_CLOSED_PORT,
749 LOG (GNUNET_ERROR_TYPE_DEBUG,
750 "Created loose incoming channel to port %s from peer %s\n",
751 GNUNET_h2s (&ch->port),
752 GCP_2s (GCT_get_destination (ch->t)));
759 GNUNET_STATISTICS_update (stats,
768 * Function called once the tunnel confirms that we sent the
769 * ACK message. Just remembers it was sent, we do not expect
772 * @param cls our `struct CadetChannel`.
773 * @param cid identifier of the connection within the tunnel, NULL
774 * if transmission failed
777 send_ack_cb (void *cls,
778 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
780 struct CadetChannel *ch = cls;
782 GNUNET_assert (NULL != ch->last_control_qe);
783 ch->last_control_qe = NULL;
788 * Compute and send the current #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK to the other peer.
790 * @param ch channel to send the #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK for
793 send_channel_data_ack (struct CadetChannel *ch)
795 struct GNUNET_CADET_ChannelDataAckMessage msg;
797 if (GNUNET_NO == ch->reliable)
798 return; /* no ACKs */
799 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK);
800 msg.header.size = htons (sizeof (msg));
802 msg.mid.mid = htonl (ntohl (ch->mid_recv.mid));
803 msg.futures = GNUNET_htonll (ch->mid_futures);
804 if (NULL != ch->last_control_qe)
805 GCT_send_cancel (ch->last_control_qe);
806 LOG (GNUNET_ERROR_TYPE_DEBUG,
807 "Sending DATA_ACK %u:%llX via %s\n",
808 (unsigned int) ntohl (msg.mid.mid),
809 (unsigned long long) ch->mid_futures,
811 ch->last_control_qe = GCT_send (ch->t,
819 * Send our initial #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK to the client confirming that the
822 * @param cls the `struct CadetChannel`
825 send_open_ack (void *cls)
827 struct CadetChannel *ch = cls;
828 struct GNUNET_CADET_ChannelManageMessage msg;
830 LOG (GNUNET_ERROR_TYPE_DEBUG,
831 "Sending CHANNEL_OPEN_ACK on %s\n",
833 ch->retry_control_task = NULL;
834 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK);
835 msg.header.size = htons (sizeof (msg));
836 msg.reserved = htonl (0);
838 if (NULL != ch->last_control_qe)
839 GCT_send_cancel (ch->last_control_qe);
840 ch->last_control_qe = GCT_send (ch->t,
848 * We got a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN message again for
849 * this channel. If the binding was successful, (re)transmit the
850 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK.
852 * @param ch channel that got the duplicate open
853 * @param cti identifier of the connection that delivered the message
856 GCCH_handle_duplicate_open (struct CadetChannel *ch,
857 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
859 if (NULL == ch->dest)
861 LOG (GNUNET_ERROR_TYPE_DEBUG,
862 "Ignoring duplicate CHANNEL_OPEN on %s: port is closed\n",
866 if (NULL != ch->retry_control_task)
868 LOG (GNUNET_ERROR_TYPE_DEBUG,
869 "Ignoring duplicate CHANNEL_OPEN on %s: control message is pending\n",
873 LOG (GNUNET_ERROR_TYPE_DEBUG,
874 "Retransmitting CHANNEL_OPEN_ACK on %s\n",
876 ch->retry_control_task
877 = GNUNET_SCHEDULER_add_now (&send_open_ack,
883 * Send a #GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK to the client to solicit more messages.
885 * @param ch channel the ack is for
886 * @param to_owner #GNUNET_YES to send to owner,
887 * #GNUNET_NO to send to dest
890 send_ack_to_client (struct CadetChannel *ch,
893 struct GNUNET_MQ_Envelope *env;
894 struct GNUNET_CADET_LocalAck *ack;
895 struct CadetChannelClient *ccc;
897 ccc = (GNUNET_YES == to_owner) ? ch->owner : ch->dest;
900 /* This can happen if we are just getting ACKs after
901 our local client already disconnected. */
902 GNUNET_assert (GNUNET_YES == ch->destroy);
905 env = GNUNET_MQ_msg (ack,
906 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
908 LOG (GNUNET_ERROR_TYPE_DEBUG,
909 "Sending CADET_LOCAL_ACK to %s (%s) at ccn %X (%u/%u pending)\n",
911 (GNUNET_YES == to_owner) ? "owner" : "dest",
912 ntohl (ack->ccn.channel_of_client),
913 ch->pending_messages,
914 ch->max_pending_messages);
915 GSC_send_to_client (ccc->c,
921 * A client is bound to the port that we have a channel
922 * open to. Send the acknowledgement for the connection
923 * request and establish the link with the client.
925 * @param ch open incoming channel
926 * @param c client listening on the respective port
929 GCCH_bind (struct CadetChannel *ch,
930 struct CadetClient *c)
933 struct CadetChannelClient *cccd;
935 LOG (GNUNET_ERROR_TYPE_DEBUG,
936 "Binding %s from %s to port %s of %s\n",
939 GNUNET_h2s (&ch->port),
941 if (NULL != ch->retry_control_task)
943 /* there might be a timeout task here */
944 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
945 ch->retry_control_task = NULL;
949 options |= GNUNET_CADET_OPTION_NOBUFFER;
951 options |= GNUNET_CADET_OPTION_RELIABLE;
952 if (ch->out_of_order)
953 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
954 cccd = GNUNET_new (struct CadetChannelClient);
957 cccd->client_ready = GNUNET_YES;
958 cccd->ccn = GSC_bind (c,
960 (GNUNET_YES == ch->is_loopback)
961 ? GCP_get (&my_full_id,
963 : GCT_get_destination (ch->t),
966 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) <
967 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
968 ch->mid_recv.mid = htonl (1); /* The OPEN counts as message 0! */
969 if (GNUNET_YES == ch->is_loopback)
971 ch->state = CADET_CHANNEL_OPEN_SENT;
972 GCCH_handle_channel_open_ack (ch,
977 /* notify other peer that we accepted the connection */
978 ch->retry_control_task
979 = GNUNET_SCHEDULER_add_now (&send_open_ack,
982 /* give client it's initial supply of ACKs */
983 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) <
984 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
985 for (unsigned int i=0;i<ch->max_pending_messages;i++)
986 send_ack_to_client (ch,
992 * Destroy locally created channel. Called by the local client, so no
993 * need to tell the client.
995 * @param ch channel to destroy
996 * @param c client that caused the destruction
997 * @param ccn client number of the client @a c
1000 GCCH_channel_local_destroy (struct CadetChannel *ch,
1001 struct CadetClient *c,
1002 struct GNUNET_CADET_ClientChannelNumber ccn)
1004 LOG (GNUNET_ERROR_TYPE_DEBUG,
1005 "%s asks for destruction of %s\n",
1008 GNUNET_assert (NULL != c);
1009 if ( (NULL != ch->owner) &&
1010 (c == ch->owner->c) &&
1011 (ccn.channel_of_client == ch->owner->ccn.channel_of_client) )
1013 free_channel_client (ch->owner);
1016 else if ( (NULL != ch->dest) &&
1017 (c == ch->dest->c) &&
1018 (ccn.channel_of_client == ch->dest->ccn.channel_of_client) )
1020 free_channel_client (ch->dest);
1028 if (GNUNET_YES == ch->destroy)
1030 /* other end already destroyed, with the local client gone, no need
1031 to finish transmissions, just destroy immediately. */
1032 channel_destroy (ch);
1035 if ( (NULL != ch->head_sent) ||
1036 (NULL != ch->owner) ||
1037 (NULL != ch->dest) )
1039 /* Wait for other end to destroy us as well,
1040 and otherwise allow send queue to be transmitted first */
1041 ch->destroy = GNUNET_YES;
1044 /* If the we ever sent the CHANNEL_CREATE, we need to send a destroy message. */
1045 if (CADET_CHANNEL_NEW != ch->state)
1046 GCT_send_channel_destroy (ch->t,
1048 /* Nothing left to do, just finish destruction */
1049 channel_destroy (ch);
1054 * We got an acknowledgement for the creation of the channel
1055 * (the port is open on the other side). Begin transmissions.
1057 * @param ch channel to destroy
1058 * @param cti identifier of the connection that delivered the message
1061 GCCH_handle_channel_open_ack (struct CadetChannel *ch,
1062 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
1066 case CADET_CHANNEL_NEW:
1067 /* this should be impossible */
1070 case CADET_CHANNEL_OPEN_SENT:
1071 if (NULL == ch->owner)
1073 /* We're not the owner, wrong direction! */
1074 GNUNET_break_op (0);
1077 LOG (GNUNET_ERROR_TYPE_DEBUG,
1078 "Received CHANNEL_OPEN_ACK for waiting %s, entering READY state\n",
1080 if (NULL != ch->retry_control_task) /* can be NULL if ch->is_loopback */
1082 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
1083 ch->retry_control_task = NULL;
1085 ch->state = CADET_CHANNEL_READY;
1086 /* On first connect, send client as many ACKs as we allow messages
1088 for (unsigned int i=0;i<ch->max_pending_messages;i++)
1089 send_ack_to_client (ch,
1092 case CADET_CHANNEL_READY:
1093 /* duplicate ACK, maybe we retried the CREATE. Ignore. */
1094 LOG (GNUNET_ERROR_TYPE_DEBUG,
1095 "Received duplicate channel OPEN_ACK for %s\n",
1097 GNUNET_STATISTICS_update (stats,
1098 "# duplicate CREATE_ACKs",
1107 * Test if element @a e1 comes before element @a e2.
1109 * @param cls closure, to a flag where we indicate duplicate packets
1110 * @param m1 a message of to sort
1111 * @param m2 another message to sort
1112 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1115 is_before (void *cls,
1116 struct CadetOutOfOrderMessage *m1,
1117 struct CadetOutOfOrderMessage *m2)
1119 int *duplicate = cls;
1120 uint32_t v1 = ntohl (m1->mid.mid);
1121 uint32_t v2 = ntohl (m2->mid.mid);
1126 *duplicate = GNUNET_YES;
1127 if (delta > (uint32_t) INT_MAX)
1129 /* in overflow range, we can safely assume we wrapped around */
1134 /* result is small, thus v2 > v1, thus m1 < m2 */
1141 * We got payload data for a channel. Pass it on to the client
1142 * and send an ACK to the other end (once flow control allows it!)
1144 * @param ch channel that got data
1145 * @param cti identifier of the connection that delivered the message
1146 * @param msg message that was received
1149 GCCH_handle_channel_plaintext_data (struct CadetChannel *ch,
1150 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1151 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
1153 struct GNUNET_MQ_Envelope *env;
1154 struct GNUNET_CADET_LocalData *ld;
1155 struct CadetChannelClient *ccc;
1156 size_t payload_size;
1157 struct CadetOutOfOrderMessage *com;
1164 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1165 if ( (GNUNET_YES == ch->destroy) &&
1166 (NULL == ch->owner) &&
1167 (NULL == ch->dest) )
1169 /* This client is gone, but we still have messages to send to
1170 the other end (which is why @a ch is not yet dead). However,
1171 we cannot pass messages to our client anymore. */
1172 LOG (GNUNET_ERROR_TYPE_DEBUG,
1173 "Dropping incoming payload on %s as this end is already closed\n",
1175 /* send back DESTROY notification to stop further retransmissions! */
1176 GCT_send_channel_destroy (ch->t,
1180 payload_size = ntohs (msg->header.size) - sizeof (*msg);
1181 env = GNUNET_MQ_msg_extra (ld,
1183 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1184 ld->ccn = (NULL == ch->dest) ? ch->owner->ccn : ch->dest->ccn;
1185 GNUNET_memcpy (&ld[1],
1188 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1189 if ( (GNUNET_YES == ccc->client_ready) &&
1190 ( (GNUNET_YES == ch->out_of_order) ||
1191 (msg->mid.mid == ch->mid_recv.mid) ) )
1193 LOG (GNUNET_ERROR_TYPE_DEBUG,
1194 "Giving %u bytes of payload with MID %u from %s to client %s\n",
1195 (unsigned int) payload_size,
1196 ntohl (msg->mid.mid),
1199 ccc->client_ready = GNUNET_NO;
1200 GSC_send_to_client (ccc->c,
1202 ch->mid_recv.mid = htonl (1 + ntohl (ch->mid_recv.mid));
1203 ch->mid_futures >>= 1;
1204 send_channel_data_ack (ch);
1208 if (GNUNET_YES == ch->reliable)
1210 /* check if message ought to be dropped because it is ancient/too distant/duplicate */
1211 mid_min = ntohl (ch->mid_recv.mid);
1212 mid_max = mid_min + ch->max_pending_messages;
1213 mid_msg = ntohl (msg->mid.mid);
1214 if ( ( (uint32_t) (mid_msg - mid_min) > ch->max_pending_messages) ||
1215 ( (uint32_t) (mid_max - mid_msg) > ch->max_pending_messages) )
1217 LOG (GNUNET_ERROR_TYPE_DEBUG,
1218 "%s at %u drops ancient or far-future message %u\n",
1220 (unsigned int) mid_min,
1221 ntohl (msg->mid.mid));
1223 GNUNET_STATISTICS_update (stats,
1224 "# duplicate DATA (ancient or future)",
1227 GNUNET_MQ_discard (env);
1228 send_channel_data_ack (ch);
1231 /* mark bit for future ACKs */
1232 delta = mid_msg - mid_min - 1; /* overflow/underflow are OK here */
1235 if (0 != (ch->mid_futures & (1LLU << delta)))
1237 /* Duplicate within the queue, drop also */
1238 LOG (GNUNET_ERROR_TYPE_DEBUG,
1239 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1240 (unsigned int) payload_size,
1242 ntohl (msg->mid.mid));
1243 GNUNET_STATISTICS_update (stats,
1247 GNUNET_MQ_discard (env);
1248 send_channel_data_ack (ch);
1251 ch->mid_futures |= (1LLU << delta);
1252 LOG (GNUNET_ERROR_TYPE_DEBUG,
1253 "Marked bit %llX for mid %u (base: %u); now: %llX\n",
1260 else /* ! ch->reliable */
1262 /* Channel is unreliable, so we do not ACK. But we also cannot
1263 allow buffering everything, so check if we have space... */
1264 if (ccc->num_recv >= ch->max_pending_messages)
1266 struct CadetOutOfOrderMessage *drop;
1268 /* Yep, need to drop. Drop the oldest message in
1270 LOG (GNUNET_ERROR_TYPE_DEBUG,
1271 "Queue full due slow client on %s, dropping oldest message\n",
1273 GNUNET_STATISTICS_update (stats,
1274 "# messages dropped due to slow client",
1277 drop = ccc->head_recv;
1278 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1282 GNUNET_MQ_discard (drop->env);
1287 /* Insert message into sorted out-of-order queue */
1288 com = GNUNET_new (struct CadetOutOfOrderMessage);
1289 com->mid = msg->mid;
1291 duplicate = GNUNET_NO;
1292 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetOutOfOrderMessage,
1299 if (GNUNET_YES == duplicate)
1301 /* Duplicate within the queue, drop also (this is not covered by
1302 the case above if "delta" >= 64, which could be the case if
1303 max_pending_messages is also >= 64 or if our client is unready
1304 and we are seeing retransmissions of the message our client is
1306 LOG (GNUNET_ERROR_TYPE_DEBUG,
1307 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1308 (unsigned int) payload_size,
1310 ntohl (msg->mid.mid));
1311 GNUNET_STATISTICS_update (stats,
1315 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1319 GNUNET_MQ_discard (com->env);
1321 send_channel_data_ack (ch);
1324 LOG (GNUNET_ERROR_TYPE_DEBUG,
1325 "Queued %s payload of %u bytes on %s-%X(%p) (mid %u, need %u first)\n",
1326 (GNUNET_YES == ccc->client_ready)
1328 : "client-not-ready",
1329 (unsigned int) payload_size,
1331 ntohl (ccc->ccn.channel_of_client),
1333 ntohl (msg->mid.mid),
1334 ntohl (ch->mid_recv.mid));
1335 /* NOTE: this ACK we _could_ skip, as the packet is out-of-order and
1336 the sender may already be transmitting the previous one. Needs
1337 experimental evaluation to see if/when this ACK helps or
1338 hurts. (We might even want another option.) */
1339 send_channel_data_ack (ch);
1344 * Function called once the tunnel has sent one of our messages.
1345 * If the message is unreliable, simply frees the `crm`. If the
1346 * message was reliable, calculate retransmission time and
1347 * wait for ACK (or retransmit).
1349 * @param cls the `struct CadetReliableMessage` that was sent
1350 * @param cid identifier of the connection within the tunnel, NULL
1351 * if transmission failed
1354 data_sent_cb (void *cls,
1355 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
1359 * We need to retry a transmission, the last one took too long to
1362 * @param cls the `struct CadetChannel` where we need to retransmit
1365 retry_transmission (void *cls)
1367 struct CadetChannel *ch = cls;
1368 struct CadetReliableMessage *crm = ch->head_sent;
1370 ch->retry_data_task = NULL;
1371 GNUNET_assert (NULL == crm->qe);
1372 LOG (GNUNET_ERROR_TYPE_DEBUG,
1373 "Retrying transmission on %s of message %u\n",
1375 (unsigned int) ntohl (crm->data_message->mid.mid));
1376 crm->qe = GCT_send (ch->t,
1377 &crm->data_message->header,
1380 GNUNET_assert (NULL == ch->retry_data_task);
1385 * We got an PLAINTEXT_DATA_ACK for a message in our queue, remove it from
1386 * the queue and tell our client that it can send more.
1388 * @param ch the channel that got the PLAINTEXT_DATA_ACK
1389 * @param cti identifier of the connection that delivered the message
1390 * @param crm the message that got acknowledged
1393 handle_matching_ack (struct CadetChannel *ch,
1394 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1395 struct CadetReliableMessage *crm)
1397 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1400 ch->pending_messages--;
1401 GNUNET_assert (ch->pending_messages < ch->max_pending_messages);
1402 LOG (GNUNET_ERROR_TYPE_DEBUG,
1403 "Received DATA_ACK on %s for message %u (%u ACKs pending)\n",
1405 (unsigned int) ntohl (crm->data_message->mid.mid),
1406 ch->pending_messages);
1407 if (NULL != crm->qe)
1409 GCT_send_cancel (crm->qe);
1412 if ( (1 == crm->num_transmissions) &&
1415 GCC_ack_observed (cti);
1416 if (0 == memcmp (cti,
1417 &crm->connection_taken,
1418 sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier)))
1420 GCC_latency_observed (cti,
1421 GNUNET_TIME_absolute_get_duration (crm->first_transmission_time));
1424 GNUNET_free (crm->data_message);
1426 send_ack_to_client (ch,
1434 * We got an acknowledgement for payload data for a channel.
1435 * Possibly resume transmissions.
1437 * @param ch channel that got the ack
1438 * @param cti identifier of the connection that delivered the message
1439 * @param ack details about what was received
1442 GCCH_handle_channel_plaintext_data_ack (struct CadetChannel *ch,
1443 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1444 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
1446 struct CadetReliableMessage *crm;
1447 struct CadetReliableMessage *crmn;
1453 GNUNET_break (GNUNET_NO == ch->is_loopback);
1454 if (GNUNET_NO == ch->reliable)
1456 /* not expecting ACKs on unreliable channel, odd */
1457 GNUNET_break_op (0);
1460 /* mid_base is the MID of the next message that the
1461 other peer expects (i.e. that is missing!), everything
1462 LOWER (but excluding mid_base itself) was received. */
1463 mid_base = ntohl (ack->mid.mid);
1464 mid_mask = GNUNET_htonll (ack->futures);
1466 for (crm = ch->head_sent;
1471 delta = (unsigned int) (ntohl (crm->data_message->mid.mid) - mid_base);
1472 if (delta >= UINT_MAX - ch->max_pending_messages)
1474 /* overflow, means crm was a bit in the past, so this ACK counts for it. */
1475 LOG (GNUNET_ERROR_TYPE_DEBUG,
1476 "Got DATA_ACK with base %u satisfying past message %u on %s\n",
1477 (unsigned int) mid_base,
1478 ntohl (crm->data_message->mid.mid),
1480 handle_matching_ack (ch,
1489 LOG (GNUNET_ERROR_TYPE_DEBUG,
1490 "Testing bit %llX for mid %u (base: %u)\n",
1492 ntohl (crm->data_message->mid.mid),
1494 if (0 != (mid_mask & (1LLU << delta)))
1496 LOG (GNUNET_ERROR_TYPE_DEBUG,
1497 "Got DATA_ACK with mask for %u on %s\n",
1498 ntohl (crm->data_message->mid.mid),
1500 handle_matching_ack (ch,
1506 if (GNUNET_NO == found)
1508 /* ACK for message we already dropped, might have been a
1509 duplicate ACK? Ignore. */
1510 LOG (GNUNET_ERROR_TYPE_DEBUG,
1511 "Duplicate DATA_ACK on %s, ignoring\n",
1513 GNUNET_STATISTICS_update (stats,
1514 "# duplicate DATA_ACKs",
1519 if (NULL != ch->retry_data_task)
1521 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1522 ch->retry_data_task = NULL;
1524 if ( (NULL != ch->head_sent) &&
1525 (NULL == ch->head_sent->qe) )
1527 = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry,
1528 &retry_transmission,
1534 * Destroy channel, based on the other peer closing the
1535 * connection. Also needs to remove this channel from
1538 * @param ch channel to destroy
1539 * @param cti identifier of the connection that delivered the message,
1540 * NULL if we are simulating receiving a destroy due to shutdown
1543 GCCH_handle_remote_destroy (struct CadetChannel *ch,
1544 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
1546 struct CadetChannelClient *ccc;
1548 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1549 LOG (GNUNET_ERROR_TYPE_DEBUG,
1550 "Received remote channel DESTROY for %s\n",
1552 if (GNUNET_YES == ch->destroy)
1554 /* Local client already gone, this is instant-death. */
1555 channel_destroy (ch);
1558 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1559 if (NULL != ccc->head_recv)
1561 LOG (GNUNET_ERROR_TYPE_WARNING,
1562 "Lost end of transmission due to remote shutdown on %s\n",
1564 /* FIXME: change API to notify client about truncated transmission! */
1566 ch->destroy = GNUNET_YES;
1567 GSC_handle_remote_channel_destroy (ccc->c,
1570 channel_destroy (ch);
1575 * Test if element @a e1 comes before element @a e2.
1577 * @param cls closure, to a flag where we indicate duplicate packets
1578 * @param crm1 an element of to sort
1579 * @param crm2 another element to sort
1580 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1583 cmp_crm_by_next_retry (void *cls,
1584 struct CadetReliableMessage *crm1,
1585 struct CadetReliableMessage *crm2)
1587 if (crm1->next_retry.abs_value_us <
1588 crm2->next_retry.abs_value_us)
1595 * Function called once the tunnel has sent one of our messages.
1596 * If the message is unreliable, simply frees the `crm`. If the
1597 * message was reliable, calculate retransmission time and
1598 * wait for ACK (or retransmit).
1600 * @param cls the `struct CadetReliableMessage` that was sent
1601 * @param cid identifier of the connection within the tunnel, NULL
1602 * if transmission failed
1605 data_sent_cb (void *cls,
1606 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
1608 struct CadetReliableMessage *crm = cls;
1609 struct CadetChannel *ch = crm->ch;
1611 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1612 GNUNET_assert (NULL != crm->qe);
1614 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1617 if (GNUNET_NO == ch->reliable)
1619 GNUNET_free (crm->data_message);
1621 ch->pending_messages--;
1622 send_ack_to_client (ch,
1630 /* There was an error sending. */
1631 crm->num_transmissions = GNUNET_SYSERR;
1633 else if (GNUNET_SYSERR != crm->num_transmissions)
1635 /* Increment transmission counter, and possibly store @a cid
1636 if this was the first transmission. */
1637 crm->num_transmissions++;
1638 if (1 == crm->num_transmissions)
1640 crm->first_transmission_time = GNUNET_TIME_absolute_get ();
1641 crm->connection_taken = *cid;
1642 GCC_ack_expected (cid);
1645 if ( (0 == crm->retry_delay.rel_value_us) &&
1648 struct CadetConnection *cc = GCC_lookup (cid);
1651 crm->retry_delay = GCC_get_metrics (cc)->aged_latency;
1653 crm->retry_delay = ch->retry_time;
1655 crm->retry_delay = GNUNET_TIME_STD_BACKOFF (crm->retry_delay);
1656 crm->retry_delay = GNUNET_TIME_relative_max (crm->retry_delay,
1658 crm->next_retry = GNUNET_TIME_relative_to_absolute (crm->retry_delay);
1660 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetReliableMessage,
1661 cmp_crm_by_next_retry,
1666 LOG (GNUNET_ERROR_TYPE_DEBUG,
1667 "Message %u sent, next transmission on %s in %s\n",
1668 (unsigned int) ntohl (crm->data_message->mid.mid),
1670 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (ch->head_sent->next_retry),
1672 if (NULL == ch->head_sent->qe)
1674 if (NULL != ch->retry_data_task)
1675 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1677 = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry,
1678 &retry_transmission,
1685 * Handle data given by a client.
1687 * Check whether the client is allowed to send in this tunnel, save if
1688 * channel is reliable and send an ACK to the client if there is still
1689 * buffer space in the tunnel.
1691 * @param ch Channel.
1692 * @param sender_ccn ccn of the sender
1693 * @param buf payload to transmit.
1694 * @param buf_len number of bytes in @a buf
1695 * @return #GNUNET_OK if everything goes well,
1696 * #GNUNET_SYSERR in case of an error.
1699 GCCH_handle_local_data (struct CadetChannel *ch,
1700 struct GNUNET_CADET_ClientChannelNumber sender_ccn,
1704 struct CadetReliableMessage *crm;
1706 if (ch->pending_messages > ch->max_pending_messages)
1709 return GNUNET_SYSERR;
1711 ch->pending_messages++;
1713 if (GNUNET_YES == ch->is_loopback)
1715 struct CadetChannelClient *receiver;
1716 struct GNUNET_MQ_Envelope *env;
1717 struct GNUNET_CADET_LocalData *ld;
1720 env = GNUNET_MQ_msg_extra (ld,
1722 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1723 if (sender_ccn.channel_of_client ==
1724 ch->owner->ccn.channel_of_client)
1726 receiver = ch->dest;
1727 to_owner = GNUNET_NO;
1731 GNUNET_assert (sender_ccn.channel_of_client ==
1732 ch->dest->ccn.channel_of_client);
1733 receiver = ch->owner;
1734 to_owner = GNUNET_YES;
1736 ld->ccn = receiver->ccn;
1737 GNUNET_memcpy (&ld[1],
1740 if (GNUNET_YES == receiver->client_ready)
1742 GSC_send_to_client (receiver->c,
1744 send_ack_to_client (ch,
1749 struct CadetOutOfOrderMessage *oom;
1751 oom = GNUNET_new (struct CadetOutOfOrderMessage);
1753 GNUNET_CONTAINER_DLL_insert_tail (receiver->head_recv,
1754 receiver->tail_recv,
1756 receiver->num_recv++;
1761 /* Everything is correct, send the message. */
1762 crm = GNUNET_malloc (sizeof (*crm));
1764 crm->data_message = GNUNET_malloc (sizeof (struct GNUNET_CADET_ChannelAppDataMessage)
1766 crm->data_message->header.size = htons (sizeof (struct GNUNET_CADET_ChannelAppDataMessage) + buf_len);
1767 crm->data_message->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA);
1768 ch->mid_send.mid = htonl (ntohl (ch->mid_send.mid) + 1);
1769 crm->data_message->mid = ch->mid_send;
1770 crm->data_message->ctn = ch->ctn;
1771 GNUNET_memcpy (&crm->data_message[1],
1774 GNUNET_CONTAINER_DLL_insert_tail (ch->head_sent,
1777 LOG (GNUNET_ERROR_TYPE_DEBUG,
1778 "Sending message %u from local client to %s with %u bytes\n",
1779 ntohl (crm->data_message->mid.mid),
1782 if (NULL != ch->retry_data_task)
1784 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1785 ch->retry_data_task = NULL;
1787 crm->qe = GCT_send (ch->t,
1788 &crm->data_message->header,
1791 GNUNET_assert (NULL == ch->retry_data_task);
1797 * Handle ACK from client on local channel. Means the client is ready
1798 * for more data, see if we have any for it.
1800 * @param ch channel to destroy
1801 * @param client_ccn ccn of the client sending the ack
1804 GCCH_handle_local_ack (struct CadetChannel *ch,
1805 struct GNUNET_CADET_ClientChannelNumber client_ccn)
1807 struct CadetChannelClient *ccc;
1808 struct CadetOutOfOrderMessage *com;
1810 if ( (NULL != ch->owner) &&
1811 (ch->owner->ccn.channel_of_client == client_ccn.channel_of_client) )
1813 else if ( (NULL != ch->dest) &&
1814 (ch->dest->ccn.channel_of_client == client_ccn.channel_of_client) )
1818 ccc->client_ready = GNUNET_YES;
1819 com = ccc->head_recv;
1822 LOG (GNUNET_ERROR_TYPE_DEBUG,
1823 "Got LOCAL_ACK, %s-%X ready to receive more data, but none pending on %s-%X(%p)!\n",
1825 ntohl (client_ccn.channel_of_client),
1827 ntohl (ccc->ccn.channel_of_client),
1829 return; /* none pending */
1831 if (GNUNET_YES == ch->is_loopback)
1835 /* Messages are always in-order, just send */
1836 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1840 GSC_send_to_client (ccc->c,
1842 /* Notify sender that we can receive more */
1843 if (ccc->ccn.channel_of_client ==
1844 ch->owner->ccn.channel_of_client)
1846 to_owner = GNUNET_NO;
1850 GNUNET_assert (ccc->ccn.channel_of_client ==
1851 ch->dest->ccn.channel_of_client);
1852 to_owner = GNUNET_YES;
1854 send_ack_to_client (ch,
1860 if ( (com->mid.mid != ch->mid_recv.mid) &&
1861 (GNUNET_NO == ch->out_of_order) &&
1862 (GNUNET_YES == ch->reliable) )
1864 LOG (GNUNET_ERROR_TYPE_DEBUG,
1865 "Got LOCAL_ACK, %s-%X ready to receive more data (but next one is out-of-order %u vs. %u)!\n",
1867 ntohl (ccc->ccn.channel_of_client),
1868 ntohl (com->mid.mid),
1869 ntohl (ch->mid_recv.mid));
1870 return; /* missing next one in-order */
1873 LOG (GNUNET_ERROR_TYPE_DEBUG,
1874 "Got LOCAL_ACK, giving payload message %u to %s-%X on %s\n",
1875 ntohl (com->mid.mid),
1877 ntohl (ccc->ccn.channel_of_client),
1880 /* all good, pass next message to client */
1881 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1885 /* FIXME: if unreliable, this is not aggressive
1886 enough, as it would be OK to have lost some! */
1888 ch->mid_recv.mid = htonl (1 + ntohl (com->mid.mid));
1889 ch->mid_futures >>= 1; /* equivalent to division by 2 */
1890 ccc->client_ready = GNUNET_NO;
1891 GSC_send_to_client (ccc->c,
1894 send_channel_data_ack (ch);
1895 if (NULL != ccc->head_recv)
1897 if (GNUNET_NO == ch->destroy)
1899 GCT_send_channel_destroy (ch->t,
1901 channel_destroy (ch);
1905 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-chn",__VA_ARGS__)
1911 * @param ch Channel.
1912 * @param level Debug level to use.
1915 GCCH_debug (struct CadetChannel *ch,
1916 enum GNUNET_ErrorType level)
1920 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
1922 __FILE__, __FUNCTION__, __LINE__);
1928 LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
1936 if (NULL != ch->owner)
1939 "CHN origin %s ready %s local-id: %u\n",
1940 GSC_2s (ch->owner->c),
1941 ch->owner->client_ready ? "YES" : "NO",
1942 ntohl (ch->owner->ccn.channel_of_client));
1944 if (NULL != ch->dest)
1947 "CHN destination %s ready %s local-id: %u\n",
1948 GSC_2s (ch->dest->c),
1949 ch->dest->client_ready ? "YES" : "NO",
1950 ntohl (ch->dest->ccn.channel_of_client));
1953 "CHN Message IDs recv: %d (%LLX), send: %d\n",
1954 ntohl (ch->mid_recv.mid),
1955 (unsigned long long) ch->mid_futures,
1956 ntohl (ch->mid_send.mid));
1961 /* end of gnunet-service-cadet-new_channel.c */