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_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?
38 #include "gnunet_statistics_service.h"
39 #include "gnunet-service-cadet_channel.h"
40 #include "gnunet-service-cadet_connection.h"
41 #include "gnunet-service-cadet_tunnels.h"
42 #include "gnunet-service-cadet_paths.h"
44 #define LOG(level,...) GNUNET_log_from (level,"cadet-chn",__VA_ARGS__)
47 * How long do we initially wait before retransmitting?
49 #define CADET_INITIAL_RETRANSMIT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250)
52 * How long do we wait before dropping state about incoming
53 * connection to closed port?
55 #define TIMEOUT_CLOSED_PORT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
58 * How long do we wait at least before retransmitting ever?
60 #define MIN_RTT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 75)
63 * Maximum message ID into the future we accept for out-of-order messages.
64 * If the message is more than this into the future, we drop it. This is
65 * important both to detect values that are actually in the past, as well
66 * as to limit adversarially triggerable memory consumption.
68 * Note that right now we have "max_pending_messages = 4" hard-coded in
69 * the logic below, so a value of 4 would suffice here. But we plan to
70 * allow larger windows in the future...
72 #define MAX_OUT_OF_ORDER_DISTANCE 1024
76 * All the states a channel can be in.
78 enum CadetChannelState
81 * Uninitialized status, should never appear in operation.
86 * Channel is to a port that is not open, we're waiting for the
92 * CHANNEL_OPEN message sent, waiting for CHANNEL_OPEN_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 * Data message we are trying to send.
135 struct GNUNET_CADET_ChannelAppDataMessage *data_message;
138 * How soon should we retry if we fail to get an ACK?
139 * Messages in the queue are sorted by this value.
141 struct GNUNET_TIME_Absolute next_retry;
144 * How long do we wait for an ACK after transmission?
145 * Use for the back-off calculation.
147 struct GNUNET_TIME_Relative retry_delay;
150 * Time when we first successfully transmitted the message
151 * (that is, set @e num_transmissions to 1).
153 struct GNUNET_TIME_Absolute first_transmission_time;
156 * Identifier of the connection that this message took when it
157 * was first transmitted. Only useful if @e num_transmissions is 1.
159 struct GNUNET_CADET_ConnectionTunnelIdentifier connection_taken;
162 * How often was this message transmitted? #GNUNET_SYSERR if there
163 * was an error transmitting the message, #GNUNET_NO if it was not
164 * yet transmitted ever, otherwise the number of (re) transmissions.
166 int num_transmissions;
172 * List of received out-of-order data messages.
174 struct CadetOutOfOrderMessage
177 * Double linked list, FIFO style
179 struct CadetOutOfOrderMessage *next;
182 * Double linked list, FIFO style
184 struct CadetOutOfOrderMessage *prev;
187 * ID of the message (messages up to this point needed
188 * before we give this one to the client).
190 struct ChannelMessageIdentifier mid;
193 * The envelope with the payload of the out-of-order message
195 struct GNUNET_MQ_Envelope *env;
201 * Client endpoint of a `struct CadetChannel`. A channel may be a
202 * loopback channel, in which case it has two of these endpoints.
203 * Note that flow control also is required in both directions.
205 struct CadetChannelClient
208 * Client handle. Not by itself sufficient to designate
209 * the client endpoint, as the same client handle may
210 * be used for both the owner and the destination, and
211 * we thus also need the channel ID to identify the client.
213 struct CadetClient *c;
216 * Head of DLL of messages received out of order or while client was unready.
218 struct CadetOutOfOrderMessage *head_recv;
221 * Tail DLL of messages received out of order or while client was unready.
223 struct CadetOutOfOrderMessage *tail_recv;
226 * Local tunnel number for this client.
227 * (if owner >= #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI,
228 * otherwise < #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
230 struct GNUNET_CADET_ClientChannelNumber ccn;
233 * Number of entries currently in @a head_recv DLL.
235 unsigned int num_recv;
238 * Can we send data to the client?
246 * Struct containing all information regarding a channel to a remote client.
251 * Tunnel this channel is in.
253 struct CadetTunnel *t;
256 * Client owner of the tunnel, if any.
257 * (Used if this channel represends the initiating end of the tunnel.)
259 struct CadetChannelClient *owner;
262 * Client destination of the tunnel, if any.
263 * (Used if this channel represents the listening end of the tunnel.)
265 struct CadetChannelClient *dest;
268 * Last entry in the tunnel's queue relating to control messages
269 * (#GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN or
270 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK). Used to cancel
271 * transmission in case we receive updated information.
273 struct CadetTunnelQueueEntry *last_control_qe;
276 * Head of DLL of messages sent and not yet ACK'd.
278 struct CadetReliableMessage *head_sent;
281 * Tail of DLL of messages sent and not yet ACK'd.
283 struct CadetReliableMessage *tail_sent;
286 * Task to resend/poll in case no ACK is received.
288 struct GNUNET_SCHEDULER_Task *retry_control_task;
291 * Task to resend/poll in case no ACK is received.
293 struct GNUNET_SCHEDULER_Task *retry_data_task;
296 * Last time the channel was used
298 struct GNUNET_TIME_Absolute timestamp;
301 * Destination port of the channel.
303 struct GNUNET_HashCode port;
306 * Counter for exponential backoff.
308 struct GNUNET_TIME_Relative retry_time;
311 * Bitfield of already-received messages past @e mid_recv.
313 uint64_t mid_futures;
316 * Next MID expected for incoming traffic.
318 struct ChannelMessageIdentifier mid_recv;
321 * Next MID to use for outgoing traffic.
323 struct ChannelMessageIdentifier mid_send;
326 * Total (reliable) messages pending ACK for this channel.
328 unsigned int pending_messages;
331 * Maximum (reliable) messages pending ACK for this channel
332 * before we throttle the client.
334 unsigned int max_pending_messages;
337 * Number identifying this channel in its tunnel.
339 struct GNUNET_CADET_ChannelTunnelNumber ctn;
344 enum CadetChannelState state;
347 * Count how many ACKs we skipped, used to prevent long
348 * sequences of ACK skipping.
350 unsigned int skip_ack_series;
353 * Is the tunnel bufferless (minimum latency)?
358 * Is the tunnel reliable?
363 * Is the tunnel out-of-order?
368 * Is this channel a loopback channel, where the destination is us again?
373 * Flag to signal the destruction of the channel. If this is set to
374 * #GNUNET_YES the channel will be destroyed once the queue is
383 * Get the static string for identification of the channel.
387 * @return Static string with the channel IDs.
390 GCCH_2s (const struct CadetChannel *ch)
392 static char buf[128];
394 GNUNET_snprintf (buf,
396 "Channel %s:%s ctn:%X(%X/%X)",
397 (GNUNET_YES == ch->is_loopback)
399 : GNUNET_i2s (GCP_get_id (GCT_get_destination (ch->t))),
400 GNUNET_h2s (&ch->port),
402 (NULL == ch->owner) ? 0 : ntohl (ch->owner->ccn.channel_of_client),
403 (NULL == ch->dest) ? 0 : ntohl (ch->dest->ccn.channel_of_client));
409 * Get the channel's public ID.
413 * @return ID used to identify the channel with the remote peer.
415 struct GNUNET_CADET_ChannelTunnelNumber
416 GCCH_get_id (const struct CadetChannel *ch)
423 * Release memory associated with @a ccc
425 * @param ccc data structure to clean up
428 free_channel_client (struct CadetChannelClient *ccc)
430 struct CadetOutOfOrderMessage *com;
432 while (NULL != (com = ccc->head_recv))
434 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
438 GNUNET_MQ_discard (com->env);
446 * Destroy the given channel.
448 * @param ch channel to destroy
451 channel_destroy (struct CadetChannel *ch)
453 struct CadetReliableMessage *crm;
455 while (NULL != (crm = ch->head_sent))
457 GNUNET_assert (ch == crm->ch);
460 GCT_send_cancel (crm->qe);
463 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
466 GNUNET_free (crm->data_message);
469 if (NULL != ch->owner)
471 free_channel_client (ch->owner);
474 if (NULL != ch->dest)
476 free_channel_client (ch->dest);
479 if (NULL != ch->last_control_qe)
481 GCT_send_cancel (ch->last_control_qe);
482 ch->last_control_qe = NULL;
484 if (NULL != ch->retry_data_task)
486 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
487 ch->retry_data_task = NULL;
489 if (NULL != ch->retry_control_task)
491 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
492 ch->retry_control_task = NULL;
494 if (GNUNET_NO == ch->is_loopback)
496 GCT_remove_channel (ch->t,
506 * Send a channel create message.
508 * @param cls Channel for which to send.
511 send_channel_open (void *cls);
515 * Function called once the tunnel confirms that we sent the
516 * create message. Delays for a bit until we retry.
518 * @param cls our `struct CadetChannel`.
519 * @param cid identifier of the connection within the tunnel, NULL
520 * if transmission failed
523 channel_open_sent_cb (void *cls,
524 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
526 struct CadetChannel *ch = cls;
528 GNUNET_assert (NULL != ch->last_control_qe);
529 ch->last_control_qe = NULL;
530 ch->retry_time = GNUNET_TIME_STD_BACKOFF (ch->retry_time);
531 LOG (GNUNET_ERROR_TYPE_DEBUG,
532 "Sent CADET_CHANNEL_OPEN on %s, retrying in %s\n",
534 GNUNET_STRINGS_relative_time_to_string (ch->retry_time,
536 ch->retry_control_task
537 = GNUNET_SCHEDULER_add_delayed (ch->retry_time,
544 * Send a channel open message.
546 * @param cls Channel for which to send.
549 send_channel_open (void *cls)
551 struct CadetChannel *ch = cls;
552 struct GNUNET_CADET_ChannelOpenMessage msgcc;
555 ch->retry_control_task = NULL;
556 LOG (GNUNET_ERROR_TYPE_DEBUG,
557 "Sending CHANNEL_OPEN message for %s\n",
561 options |= GNUNET_CADET_OPTION_NOBUFFER;
563 options |= GNUNET_CADET_OPTION_RELIABLE;
564 if (ch->out_of_order)
565 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
566 msgcc.header.size = htons (sizeof (msgcc));
567 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
568 msgcc.opt = htonl (options);
569 msgcc.port = ch->port;
571 ch->state = CADET_CHANNEL_OPEN_SENT;
572 if (NULL != ch->last_control_qe)
573 GCT_send_cancel (ch->last_control_qe);
574 ch->last_control_qe = GCT_send (ch->t,
576 &channel_open_sent_cb,
578 GNUNET_assert (NULL == ch->retry_control_task);
583 * Function called once and only once after a channel was bound
584 * to its tunnel via #GCT_add_channel() is ready for transmission.
585 * Note that this is only the case for channels that this peer
586 * initiates, as for incoming channels we assume that they are
587 * ready for transmission immediately upon receiving the open
588 * message. Used to bootstrap the #GCT_send() process.
590 * @param ch the channel for which the tunnel is now ready
593 GCCH_tunnel_up (struct CadetChannel *ch)
595 GNUNET_assert (NULL == ch->retry_control_task);
596 LOG (GNUNET_ERROR_TYPE_DEBUG,
597 "Tunnel up, sending CHANNEL_OPEN on %s now\n",
599 ch->retry_control_task
600 = GNUNET_SCHEDULER_add_now (&send_channel_open,
606 * Create a new channel.
608 * @param owner local client owning the channel
609 * @param ccn local number of this channel at the @a owner
610 * @param destination peer to which we should build the channel
611 * @param port desired port at @a destination
612 * @param options options for the channel
613 * @return handle to the new channel
615 struct CadetChannel *
616 GCCH_channel_local_new (struct CadetClient *owner,
617 struct GNUNET_CADET_ClientChannelNumber ccn,
618 struct CadetPeer *destination,
619 const struct GNUNET_HashCode *port,
622 struct CadetChannel *ch;
623 struct CadetChannelClient *ccco;
625 ccco = GNUNET_new (struct CadetChannelClient);
628 ccco->client_ready = GNUNET_YES;
630 ch = GNUNET_new (struct CadetChannel);
631 ch->mid_recv.mid = htonl (1); /* The OPEN_ACK counts as message 0! */
632 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
633 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
634 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
635 ch->max_pending_messages = (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
638 if (0 == memcmp (&my_full_id,
639 GCP_get_id (destination),
640 sizeof (struct GNUNET_PeerIdentity)))
642 struct CadetClient *c;
644 ch->is_loopback = GNUNET_YES;
645 c = GNUNET_CONTAINER_multihashmap_get (open_ports,
649 /* port closed, wait for it to possibly open */
650 ch->state = CADET_CHANNEL_LOOSE;
651 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
654 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
655 LOG (GNUNET_ERROR_TYPE_DEBUG,
656 "Created loose incoming loopback channel to port %s\n",
657 GNUNET_h2s (&ch->port));
667 ch->t = GCP_get_tunnel (destination,
669 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
670 ch->ctn = GCT_add_channel (ch->t,
673 GNUNET_STATISTICS_update (stats,
677 LOG (GNUNET_ERROR_TYPE_DEBUG,
678 "Created channel to port %s at peer %s for %s using %s\n",
680 GCP_2s (destination),
682 (GNUNET_YES == ch->is_loopback) ? "loopback" : GCT_2s (ch->t));
688 * We had an incoming channel to a port that is closed.
689 * It has not been opened for a while, drop it.
691 * @param cls the channel to drop
694 timeout_closed_cb (void *cls)
696 struct CadetChannel *ch = cls;
698 ch->retry_control_task = NULL;
699 LOG (GNUNET_ERROR_TYPE_DEBUG,
700 "Closing incoming channel to port %s from peer %s due to timeout\n",
701 GNUNET_h2s (&ch->port),
702 GCP_2s (GCT_get_destination (ch->t)));
703 channel_destroy (ch);
708 * Create a new channel based on a request coming in over the network.
710 * @param t tunnel to the remote peer
711 * @param ctn identifier of this channel in the tunnel
712 * @param port desired local port
713 * @param options options for the channel
714 * @return handle to the new channel
716 struct CadetChannel *
717 GCCH_channel_incoming_new (struct CadetTunnel *t,
718 struct GNUNET_CADET_ChannelTunnelNumber ctn,
719 const struct GNUNET_HashCode *port,
722 struct CadetChannel *ch;
723 struct CadetClient *c;
725 ch = GNUNET_new (struct CadetChannel);
729 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
730 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
731 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
732 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
733 ch->max_pending_messages = (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
734 GNUNET_STATISTICS_update (stats,
739 c = GNUNET_CONTAINER_multihashmap_get (open_ports,
743 /* port closed, wait for it to possibly open */
744 ch->state = CADET_CHANNEL_LOOSE;
745 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
748 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
749 GNUNET_assert (NULL == ch->retry_control_task);
750 ch->retry_control_task
751 = GNUNET_SCHEDULER_add_delayed (TIMEOUT_CLOSED_PORT,
754 LOG (GNUNET_ERROR_TYPE_DEBUG,
755 "Created loose incoming channel to port %s from peer %s\n",
756 GNUNET_h2s (&ch->port),
757 GCP_2s (GCT_get_destination (ch->t)));
764 GNUNET_STATISTICS_update (stats,
773 * Function called once the tunnel confirms that we sent the
774 * ACK message. Just remembers it was sent, we do not expect
777 * @param cls our `struct CadetChannel`.
778 * @param cid identifier of the connection within the tunnel, NULL
779 * if transmission failed
782 send_ack_cb (void *cls,
783 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
785 struct CadetChannel *ch = cls;
787 GNUNET_assert (NULL != ch->last_control_qe);
788 ch->last_control_qe = NULL;
793 * Compute and send the current #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK to the other peer.
795 * @param ch channel to send the #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK for
798 send_channel_data_ack (struct CadetChannel *ch)
800 struct GNUNET_CADET_ChannelDataAckMessage msg;
802 if (GNUNET_NO == ch->reliable)
803 return; /* no ACKs */
804 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK);
805 msg.header.size = htons (sizeof (msg));
807 msg.mid.mid = htonl (ntohl (ch->mid_recv.mid));
808 msg.futures = GNUNET_htonll (ch->mid_futures);
809 LOG (GNUNET_ERROR_TYPE_DEBUG,
810 "Sending DATA_ACK %u:%llX via %s\n",
811 (unsigned int) ntohl (msg.mid.mid),
812 (unsigned long long) ch->mid_futures,
814 if (NULL != ch->last_control_qe)
815 GCT_send_cancel (ch->last_control_qe);
816 ch->last_control_qe = GCT_send (ch->t,
824 * Send our initial #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK to the client confirming that the
827 * @param cls the `struct CadetChannel`
830 send_open_ack (void *cls)
832 struct CadetChannel *ch = cls;
833 struct GNUNET_CADET_ChannelManageMessage msg;
835 ch->retry_control_task = NULL;
836 LOG (GNUNET_ERROR_TYPE_DEBUG,
837 "Sending CHANNEL_OPEN_ACK on %s\n",
839 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK);
840 msg.header.size = htons (sizeof (msg));
841 msg.reserved = htonl (0);
843 if (NULL != ch->last_control_qe)
844 GCT_send_cancel (ch->last_control_qe);
845 ch->last_control_qe = GCT_send (ch->t,
853 * We got a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN message again for
854 * this channel. If the binding was successful, (re)transmit the
855 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK.
857 * @param ch channel that got the duplicate open
858 * @param cti identifier of the connection that delivered the message
861 GCCH_handle_duplicate_open (struct CadetChannel *ch,
862 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
864 if (NULL == ch->dest)
866 LOG (GNUNET_ERROR_TYPE_DEBUG,
867 "Ignoring duplicate CHANNEL_OPEN on %s: port is closed\n",
871 if (NULL != ch->retry_control_task)
873 LOG (GNUNET_ERROR_TYPE_DEBUG,
874 "Ignoring duplicate CHANNEL_OPEN on %s: control message is pending\n",
878 LOG (GNUNET_ERROR_TYPE_DEBUG,
879 "Retransmitting CHANNEL_OPEN_ACK on %s\n",
881 ch->retry_control_task
882 = GNUNET_SCHEDULER_add_now (&send_open_ack,
888 * Send a #GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK to the client to solicit more messages.
890 * @param ch channel the ack is for
891 * @param to_owner #GNUNET_YES to send to owner,
892 * #GNUNET_NO to send to dest
895 send_ack_to_client (struct CadetChannel *ch,
898 struct GNUNET_MQ_Envelope *env;
899 struct GNUNET_CADET_LocalAck *ack;
900 struct CadetChannelClient *ccc;
902 ccc = (GNUNET_YES == to_owner) ? ch->owner : ch->dest;
905 /* This can happen if we are just getting ACKs after
906 our local client already disconnected. */
907 GNUNET_assert (GNUNET_YES == ch->destroy);
910 env = GNUNET_MQ_msg (ack,
911 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
913 LOG (GNUNET_ERROR_TYPE_DEBUG,
914 "Sending CADET_LOCAL_ACK to %s (%s) at ccn %X (%u/%u pending)\n",
916 (GNUNET_YES == to_owner) ? "owner" : "dest",
917 ntohl (ack->ccn.channel_of_client),
918 ch->pending_messages,
919 ch->max_pending_messages);
920 GSC_send_to_client (ccc->c,
926 * A client is bound to the port that we have a channel
927 * open to. Send the acknowledgement for the connection
928 * request and establish the link with the client.
930 * @param ch open incoming channel
931 * @param c client listening on the respective port
934 GCCH_bind (struct CadetChannel *ch,
935 struct CadetClient *c)
938 struct CadetChannelClient *cccd;
940 LOG (GNUNET_ERROR_TYPE_DEBUG,
941 "Binding %s from %s to port %s of %s\n",
944 GNUNET_h2s (&ch->port),
946 if (NULL != ch->retry_control_task)
948 /* there might be a timeout task here */
949 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
950 ch->retry_control_task = NULL;
954 options |= GNUNET_CADET_OPTION_NOBUFFER;
956 options |= GNUNET_CADET_OPTION_RELIABLE;
957 if (ch->out_of_order)
958 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
959 cccd = GNUNET_new (struct CadetChannelClient);
960 GNUNET_assert (NULL == ch->dest);
963 cccd->client_ready = GNUNET_YES;
964 cccd->ccn = GSC_bind (c,
966 (GNUNET_YES == ch->is_loopback)
967 ? GCP_get (&my_full_id,
969 : GCT_get_destination (ch->t),
972 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) <
973 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
974 ch->mid_recv.mid = htonl (1); /* The OPEN counts as message 0! */
975 if (GNUNET_YES == ch->is_loopback)
977 ch->state = CADET_CHANNEL_OPEN_SENT;
978 GCCH_handle_channel_open_ack (ch,
983 /* notify other peer that we accepted the connection */
984 ch->state = CADET_CHANNEL_READY;
985 ch->retry_control_task
986 = GNUNET_SCHEDULER_add_now (&send_open_ack,
989 /* give client it's initial supply of ACKs */
990 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) <
991 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
992 for (unsigned int i=0;i<ch->max_pending_messages;i++)
993 send_ack_to_client (ch,
999 * One of our clients has disconnected, tell the other one that we
1000 * are finished. Done asynchronously to avoid concurrent modification
1001 * issues if this is the same client.
1003 * @param cls the `struct CadetChannel` where one of the ends is now dead
1006 signal_remote_destroy_cb (void *cls)
1008 struct CadetChannel *ch = cls;
1009 struct CadetChannelClient *ccc;
1011 /* Find which end is left... */
1012 ch->retry_control_task = NULL;
1013 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1014 GSC_handle_remote_channel_destroy (ccc->c,
1017 channel_destroy (ch);
1022 * Destroy locally created channel. Called by the local client, so no
1023 * need to tell the client.
1025 * @param ch channel to destroy
1026 * @param c client that caused the destruction
1027 * @param ccn client number of the client @a c
1030 GCCH_channel_local_destroy (struct CadetChannel *ch,
1031 struct CadetClient *c,
1032 struct GNUNET_CADET_ClientChannelNumber ccn)
1034 LOG (GNUNET_ERROR_TYPE_DEBUG,
1035 "%s asks for destruction of %s\n",
1038 GNUNET_assert (NULL != c);
1039 if ( (NULL != ch->owner) &&
1040 (c == ch->owner->c) &&
1041 (ccn.channel_of_client == ch->owner->ccn.channel_of_client) )
1043 free_channel_client (ch->owner);
1046 else if ( (NULL != ch->dest) &&
1047 (c == ch->dest->c) &&
1048 (ccn.channel_of_client == ch->dest->ccn.channel_of_client) )
1050 free_channel_client (ch->dest);
1058 if (GNUNET_YES == ch->destroy)
1060 /* other end already destroyed, with the local client gone, no need
1061 to finish transmissions, just destroy immediately. */
1062 channel_destroy (ch);
1065 if ( (NULL != ch->head_sent) &&
1066 ( (NULL != ch->owner) ||
1067 (NULL != ch->dest) ) )
1069 /* Wait for other end to destroy us as well,
1070 and otherwise allow send queue to be transmitted first */
1071 ch->destroy = GNUNET_YES;
1074 if ( (GNUNET_YES == ch->is_loopback) &&
1075 ( (NULL != ch->owner) ||
1076 (NULL != ch->dest) ) )
1078 if (NULL != ch->retry_control_task)
1079 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
1080 ch->retry_control_task
1081 = GNUNET_SCHEDULER_add_now (&signal_remote_destroy_cb,
1085 if (GNUNET_NO == ch->is_loopback)
1087 /* If the we ever sent the CHANNEL_CREATE, we need to send a destroy message. */
1090 case CADET_CHANNEL_NEW:
1091 /* We gave up on a channel that we created as a client to a remote
1092 target, but that never went anywhere. Nothing to do here. */
1094 case CADET_CHANNEL_LOOSE:
1095 GSC_drop_loose_channel (&ch->port,
1099 GCT_send_channel_destroy (ch->t,
1103 /* Nothing left to do, just finish destruction */
1104 channel_destroy (ch);
1109 * We got an acknowledgement for the creation of the channel
1110 * (the port is open on the other side). Begin transmissions.
1112 * @param ch channel to destroy
1113 * @param cti identifier of the connection that delivered the message
1116 GCCH_handle_channel_open_ack (struct CadetChannel *ch,
1117 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
1121 case CADET_CHANNEL_NEW:
1122 /* this should be impossible */
1125 case CADET_CHANNEL_LOOSE:
1126 /* This makes no sense. */
1127 GNUNET_break_op (0);
1129 case CADET_CHANNEL_OPEN_SENT:
1130 if (NULL == ch->owner)
1132 /* We're not the owner, wrong direction! */
1133 GNUNET_break_op (0);
1136 LOG (GNUNET_ERROR_TYPE_DEBUG,
1137 "Received CHANNEL_OPEN_ACK for waiting %s, entering READY state\n",
1139 if (NULL != ch->retry_control_task) /* can be NULL if ch->is_loopback */
1141 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
1142 ch->retry_control_task = NULL;
1144 ch->state = CADET_CHANNEL_READY;
1145 /* On first connect, send client as many ACKs as we allow messages
1147 for (unsigned int i=0;i<ch->max_pending_messages;i++)
1148 send_ack_to_client (ch,
1151 case CADET_CHANNEL_READY:
1152 /* duplicate ACK, maybe we retried the CREATE. Ignore. */
1153 LOG (GNUNET_ERROR_TYPE_DEBUG,
1154 "Received duplicate channel OPEN_ACK for %s\n",
1156 GNUNET_STATISTICS_update (stats,
1157 "# duplicate CREATE_ACKs",
1166 * Test if element @a e1 comes before element @a e2.
1168 * @param cls closure, to a flag where we indicate duplicate packets
1169 * @param m1 a message of to sort
1170 * @param m2 another message to sort
1171 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1174 is_before (void *cls,
1175 struct CadetOutOfOrderMessage *m1,
1176 struct CadetOutOfOrderMessage *m2)
1178 int *duplicate = cls;
1179 uint32_t v1 = ntohl (m1->mid.mid);
1180 uint32_t v2 = ntohl (m2->mid.mid);
1185 *duplicate = GNUNET_YES;
1186 if (delta > (uint32_t) INT_MAX)
1188 /* in overflow range, we can safely assume we wrapped around */
1193 /* result is small, thus v2 > v1, thus m1 < m2 */
1200 * We got payload data for a channel. Pass it on to the client
1201 * and send an ACK to the other end (once flow control allows it!)
1203 * @param ch channel that got data
1204 * @param cti identifier of the connection that delivered the message
1205 * @param msg message that was received
1208 GCCH_handle_channel_plaintext_data (struct CadetChannel *ch,
1209 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1210 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
1212 struct GNUNET_MQ_Envelope *env;
1213 struct GNUNET_CADET_LocalData *ld;
1214 struct CadetChannelClient *ccc;
1215 size_t payload_size;
1216 struct CadetOutOfOrderMessage *com;
1223 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1224 if ( (GNUNET_YES == ch->destroy) &&
1225 (NULL == ch->owner) &&
1226 (NULL == ch->dest) )
1228 /* This client is gone, but we still have messages to send to
1229 the other end (which is why @a ch is not yet dead). However,
1230 we cannot pass messages to our client anymore. */
1231 LOG (GNUNET_ERROR_TYPE_DEBUG,
1232 "Dropping incoming payload on %s as this end is already closed\n",
1234 /* send back DESTROY notification to stop further retransmissions! */
1235 GCT_send_channel_destroy (ch->t,
1239 payload_size = ntohs (msg->header.size) - sizeof (*msg);
1240 env = GNUNET_MQ_msg_extra (ld,
1242 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1243 ld->ccn = (NULL == ch->dest) ? ch->owner->ccn : ch->dest->ccn;
1244 GNUNET_memcpy (&ld[1],
1247 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1248 if ( (GNUNET_YES == ccc->client_ready) &&
1249 ( (GNUNET_YES == ch->out_of_order) ||
1250 (msg->mid.mid == ch->mid_recv.mid) ) )
1252 LOG (GNUNET_ERROR_TYPE_DEBUG,
1253 "Giving %u bytes of payload with MID %u from %s to client %s\n",
1254 (unsigned int) payload_size,
1255 ntohl (msg->mid.mid),
1258 ccc->client_ready = GNUNET_NO;
1259 GSC_send_to_client (ccc->c,
1261 ch->mid_recv.mid = htonl (1 + ntohl (ch->mid_recv.mid));
1262 ch->mid_futures >>= 1;
1263 send_channel_data_ack (ch);
1267 if (GNUNET_YES == ch->reliable)
1269 /* check if message ought to be dropped because it is ancient/too distant/duplicate */
1270 mid_min = ntohl (ch->mid_recv.mid);
1271 mid_max = mid_min + ch->max_pending_messages;
1272 mid_msg = ntohl (msg->mid.mid);
1273 if ( ( (uint32_t) (mid_msg - mid_min) > ch->max_pending_messages) ||
1274 ( (uint32_t) (mid_max - mid_msg) > ch->max_pending_messages) )
1276 LOG (GNUNET_ERROR_TYPE_DEBUG,
1277 "%s at %u drops ancient or far-future message %u\n",
1279 (unsigned int) mid_min,
1280 ntohl (msg->mid.mid));
1282 GNUNET_STATISTICS_update (stats,
1283 "# duplicate DATA (ancient or future)",
1286 GNUNET_MQ_discard (env);
1287 send_channel_data_ack (ch);
1290 /* mark bit for future ACKs */
1291 delta = mid_msg - mid_min - 1; /* overflow/underflow are OK here */
1294 if (0 != (ch->mid_futures & (1LLU << delta)))
1296 /* Duplicate within the queue, drop also */
1297 LOG (GNUNET_ERROR_TYPE_DEBUG,
1298 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1299 (unsigned int) payload_size,
1301 ntohl (msg->mid.mid));
1302 GNUNET_STATISTICS_update (stats,
1306 GNUNET_MQ_discard (env);
1307 send_channel_data_ack (ch);
1310 ch->mid_futures |= (1LLU << delta);
1311 LOG (GNUNET_ERROR_TYPE_DEBUG,
1312 "Marked bit %llX for mid %u (base: %u); now: %llX\n",
1319 else /* ! ch->reliable */
1321 /* Channel is unreliable, so we do not ACK. But we also cannot
1322 allow buffering everything, so check if we have space... */
1323 if (ccc->num_recv >= ch->max_pending_messages)
1325 struct CadetOutOfOrderMessage *drop;
1327 /* Yep, need to drop. Drop the oldest message in
1329 LOG (GNUNET_ERROR_TYPE_DEBUG,
1330 "Queue full due slow client on %s, dropping oldest message\n",
1332 GNUNET_STATISTICS_update (stats,
1333 "# messages dropped due to slow client",
1336 drop = ccc->head_recv;
1337 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1341 GNUNET_MQ_discard (drop->env);
1346 /* Insert message into sorted out-of-order queue */
1347 com = GNUNET_new (struct CadetOutOfOrderMessage);
1348 com->mid = msg->mid;
1350 duplicate = GNUNET_NO;
1351 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetOutOfOrderMessage,
1358 if (GNUNET_YES == duplicate)
1360 /* Duplicate within the queue, drop also (this is not covered by
1361 the case above if "delta" >= 64, which could be the case if
1362 max_pending_messages is also >= 64 or if our client is unready
1363 and we are seeing retransmissions of the message our client is
1365 LOG (GNUNET_ERROR_TYPE_DEBUG,
1366 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1367 (unsigned int) payload_size,
1369 ntohl (msg->mid.mid));
1370 GNUNET_STATISTICS_update (stats,
1374 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1378 GNUNET_MQ_discard (com->env);
1380 send_channel_data_ack (ch);
1383 LOG (GNUNET_ERROR_TYPE_DEBUG,
1384 "Queued %s payload of %u bytes on %s-%X(%p) (mid %u, need %u first)\n",
1385 (GNUNET_YES == ccc->client_ready)
1387 : "client-not-ready",
1388 (unsigned int) payload_size,
1390 ntohl (ccc->ccn.channel_of_client),
1392 ntohl (msg->mid.mid),
1393 ntohl (ch->mid_recv.mid));
1394 /* NOTE: this ACK we _could_ skip, as the packet is out-of-order and
1395 the sender may already be transmitting the previous one. Needs
1396 experimental evaluation to see if/when this ACK helps or
1397 hurts. (We might even want another option.) */
1398 send_channel_data_ack (ch);
1403 * Function called once the tunnel has sent one of our messages.
1404 * If the message is unreliable, simply frees the `crm`. If the
1405 * message was reliable, calculate retransmission time and
1406 * wait for ACK (or retransmit).
1408 * @param cls the `struct CadetReliableMessage` that was sent
1409 * @param cid identifier of the connection within the tunnel, NULL
1410 * if transmission failed
1413 data_sent_cb (void *cls,
1414 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
1418 * We need to retry a transmission, the last one took too long to
1421 * @param cls the `struct CadetChannel` where we need to retransmit
1424 retry_transmission (void *cls)
1426 struct CadetChannel *ch = cls;
1427 struct CadetReliableMessage *crm = ch->head_sent;
1429 ch->retry_data_task = NULL;
1430 GNUNET_assert (NULL == crm->qe);
1431 LOG (GNUNET_ERROR_TYPE_DEBUG,
1432 "Retrying transmission on %s of message %u\n",
1434 (unsigned int) ntohl (crm->data_message->mid.mid));
1435 crm->qe = GCT_send (ch->t,
1436 &crm->data_message->header,
1439 GNUNET_assert (NULL == ch->retry_data_task);
1444 * We got an PLAINTEXT_DATA_ACK for a message in our queue, remove it from
1445 * the queue and tell our client that it can send more.
1447 * @param ch the channel that got the PLAINTEXT_DATA_ACK
1448 * @param cti identifier of the connection that delivered the message
1449 * @param crm the message that got acknowledged
1452 handle_matching_ack (struct CadetChannel *ch,
1453 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1454 struct CadetReliableMessage *crm)
1456 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1459 ch->pending_messages--;
1460 GNUNET_assert (ch->pending_messages < ch->max_pending_messages);
1461 LOG (GNUNET_ERROR_TYPE_DEBUG,
1462 "Received DATA_ACK on %s for message %u (%u ACKs pending)\n",
1464 (unsigned int) ntohl (crm->data_message->mid.mid),
1465 ch->pending_messages);
1466 if (NULL != crm->qe)
1468 GCT_send_cancel (crm->qe);
1471 if ( (1 == crm->num_transmissions) &&
1474 GCC_ack_observed (cti);
1475 if (0 == memcmp (cti,
1476 &crm->connection_taken,
1477 sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier)))
1479 GCC_latency_observed (cti,
1480 GNUNET_TIME_absolute_get_duration (crm->first_transmission_time));
1483 GNUNET_free (crm->data_message);
1485 send_ack_to_client (ch,
1493 * We got an acknowledgement for payload data for a channel.
1494 * Possibly resume transmissions.
1496 * @param ch channel that got the ack
1497 * @param cti identifier of the connection that delivered the message
1498 * @param ack details about what was received
1501 GCCH_handle_channel_plaintext_data_ack (struct CadetChannel *ch,
1502 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1503 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
1505 struct CadetReliableMessage *crm;
1506 struct CadetReliableMessage *crmn;
1512 GNUNET_break (GNUNET_NO == ch->is_loopback);
1513 if (GNUNET_NO == ch->reliable)
1515 /* not expecting ACKs on unreliable channel, odd */
1516 GNUNET_break_op (0);
1519 /* mid_base is the MID of the next message that the
1520 other peer expects (i.e. that is missing!), everything
1521 LOWER (but excluding mid_base itself) was received. */
1522 mid_base = ntohl (ack->mid.mid);
1523 mid_mask = GNUNET_htonll (ack->futures);
1525 for (crm = ch->head_sent;
1530 delta = (unsigned int) (ntohl (crm->data_message->mid.mid) - mid_base);
1531 if (delta >= UINT_MAX - ch->max_pending_messages)
1533 /* overflow, means crm was a bit in the past, so this ACK counts for it. */
1534 LOG (GNUNET_ERROR_TYPE_DEBUG,
1535 "Got DATA_ACK with base %u satisfying past message %u on %s\n",
1536 (unsigned int) mid_base,
1537 ntohl (crm->data_message->mid.mid),
1539 handle_matching_ack (ch,
1548 LOG (GNUNET_ERROR_TYPE_DEBUG,
1549 "Testing bit %llX for mid %u (base: %u)\n",
1551 ntohl (crm->data_message->mid.mid),
1553 if (0 != (mid_mask & (1LLU << delta)))
1555 LOG (GNUNET_ERROR_TYPE_DEBUG,
1556 "Got DATA_ACK with mask for %u on %s\n",
1557 ntohl (crm->data_message->mid.mid),
1559 handle_matching_ack (ch,
1565 if (GNUNET_NO == found)
1567 /* ACK for message we already dropped, might have been a
1568 duplicate ACK? Ignore. */
1569 LOG (GNUNET_ERROR_TYPE_DEBUG,
1570 "Duplicate DATA_ACK on %s, ignoring\n",
1572 GNUNET_STATISTICS_update (stats,
1573 "# duplicate DATA_ACKs",
1578 if (NULL != ch->retry_data_task)
1580 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1581 ch->retry_data_task = NULL;
1583 if ( (NULL != ch->head_sent) &&
1584 (NULL == ch->head_sent->qe) )
1586 = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry,
1587 &retry_transmission,
1593 * Destroy channel, based on the other peer closing the
1594 * connection. Also needs to remove this channel from
1597 * @param ch channel to destroy
1598 * @param cti identifier of the connection that delivered the message,
1599 * NULL if we are simulating receiving a destroy due to shutdown
1602 GCCH_handle_remote_destroy (struct CadetChannel *ch,
1603 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
1605 struct CadetChannelClient *ccc;
1607 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1608 LOG (GNUNET_ERROR_TYPE_DEBUG,
1609 "Received remote channel DESTROY for %s\n",
1611 if (GNUNET_YES == ch->destroy)
1613 /* Local client already gone, this is instant-death. */
1614 channel_destroy (ch);
1617 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1618 if ( (NULL != ccc) &&
1619 (NULL != ccc->head_recv) )
1621 LOG (GNUNET_ERROR_TYPE_WARNING,
1622 "Lost end of transmission due to remote shutdown on %s\n",
1624 /* FIXME: change API to notify client about truncated transmission! */
1626 ch->destroy = GNUNET_YES;
1628 GSC_handle_remote_channel_destroy (ccc->c,
1631 channel_destroy (ch);
1636 * Test if element @a e1 comes before element @a e2.
1638 * @param cls closure, to a flag where we indicate duplicate packets
1639 * @param crm1 an element of to sort
1640 * @param crm2 another element to sort
1641 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1644 cmp_crm_by_next_retry (void *cls,
1645 struct CadetReliableMessage *crm1,
1646 struct CadetReliableMessage *crm2)
1648 if (crm1->next_retry.abs_value_us <
1649 crm2->next_retry.abs_value_us)
1656 * Function called once the tunnel has sent one of our messages.
1657 * If the message is unreliable, simply frees the `crm`. If the
1658 * message was reliable, calculate retransmission time and
1659 * wait for ACK (or retransmit).
1661 * @param cls the `struct CadetReliableMessage` that was sent
1662 * @param cid identifier of the connection within the tunnel, NULL
1663 * if transmission failed
1666 data_sent_cb (void *cls,
1667 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
1669 struct CadetReliableMessage *crm = cls;
1670 struct CadetChannel *ch = crm->ch;
1672 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1673 GNUNET_assert (NULL != crm->qe);
1675 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1678 if (GNUNET_NO == ch->reliable)
1680 GNUNET_free (crm->data_message);
1682 ch->pending_messages--;
1683 send_ack_to_client (ch,
1691 /* There was an error sending. */
1692 crm->num_transmissions = GNUNET_SYSERR;
1694 else if (GNUNET_SYSERR != crm->num_transmissions)
1696 /* Increment transmission counter, and possibly store @a cid
1697 if this was the first transmission. */
1698 crm->num_transmissions++;
1699 if (1 == crm->num_transmissions)
1701 crm->first_transmission_time = GNUNET_TIME_absolute_get ();
1702 crm->connection_taken = *cid;
1703 GCC_ack_expected (cid);
1706 if ( (0 == crm->retry_delay.rel_value_us) &&
1709 struct CadetConnection *cc = GCC_lookup (cid);
1712 crm->retry_delay = GCC_get_metrics (cc)->aged_latency;
1714 crm->retry_delay = ch->retry_time;
1716 crm->retry_delay = GNUNET_TIME_STD_BACKOFF (crm->retry_delay);
1717 crm->retry_delay = GNUNET_TIME_relative_max (crm->retry_delay,
1719 crm->next_retry = GNUNET_TIME_relative_to_absolute (crm->retry_delay);
1721 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetReliableMessage,
1722 cmp_crm_by_next_retry,
1727 LOG (GNUNET_ERROR_TYPE_DEBUG,
1728 "Message %u sent, next transmission on %s in %s\n",
1729 (unsigned int) ntohl (crm->data_message->mid.mid),
1731 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (ch->head_sent->next_retry),
1733 if (NULL == ch->head_sent->qe)
1735 if (NULL != ch->retry_data_task)
1736 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1738 = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry,
1739 &retry_transmission,
1746 * Handle data given by a client.
1748 * Check whether the client is allowed to send in this tunnel, save if
1749 * channel is reliable and send an ACK to the client if there is still
1750 * buffer space in the tunnel.
1752 * @param ch Channel.
1753 * @param sender_ccn ccn of the sender
1754 * @param buf payload to transmit.
1755 * @param buf_len number of bytes in @a buf
1756 * @return #GNUNET_OK if everything goes well,
1757 * #GNUNET_SYSERR in case of an error.
1760 GCCH_handle_local_data (struct CadetChannel *ch,
1761 struct GNUNET_CADET_ClientChannelNumber sender_ccn,
1765 struct CadetReliableMessage *crm;
1767 if (ch->pending_messages > ch->max_pending_messages)
1770 return GNUNET_SYSERR;
1772 if (GNUNET_YES == ch->destroy)
1774 /* we are going down, drop messages */
1777 ch->pending_messages++;
1779 if (GNUNET_YES == ch->is_loopback)
1781 struct CadetChannelClient *receiver;
1782 struct GNUNET_MQ_Envelope *env;
1783 struct GNUNET_CADET_LocalData *ld;
1786 env = GNUNET_MQ_msg_extra (ld,
1788 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1789 if ( (NULL != ch->owner) &&
1790 (sender_ccn.channel_of_client ==
1791 ch->owner->ccn.channel_of_client) )
1793 receiver = ch->dest;
1794 ack_to_owner = GNUNET_YES;
1796 else if ( (NULL != ch->dest) &&
1797 (sender_ccn.channel_of_client ==
1798 ch->dest->ccn.channel_of_client) )
1800 receiver = ch->owner;
1801 ack_to_owner = GNUNET_NO;
1806 return GNUNET_SYSERR;
1808 GNUNET_assert (NULL != receiver);
1809 ld->ccn = receiver->ccn;
1810 GNUNET_memcpy (&ld[1],
1813 if (GNUNET_YES == receiver->client_ready)
1815 ch->pending_messages--;
1816 GSC_send_to_client (receiver->c,
1818 send_ack_to_client (ch,
1823 struct CadetOutOfOrderMessage *oom;
1825 oom = GNUNET_new (struct CadetOutOfOrderMessage);
1827 GNUNET_CONTAINER_DLL_insert_tail (receiver->head_recv,
1828 receiver->tail_recv,
1830 receiver->num_recv++;
1835 /* Everything is correct, send the message. */
1836 crm = GNUNET_malloc (sizeof (*crm));
1838 crm->data_message = GNUNET_malloc (sizeof (struct GNUNET_CADET_ChannelAppDataMessage)
1840 crm->data_message->header.size = htons (sizeof (struct GNUNET_CADET_ChannelAppDataMessage) + buf_len);
1841 crm->data_message->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA);
1842 ch->mid_send.mid = htonl (ntohl (ch->mid_send.mid) + 1);
1843 crm->data_message->mid = ch->mid_send;
1844 crm->data_message->ctn = ch->ctn;
1845 GNUNET_memcpy (&crm->data_message[1],
1848 GNUNET_CONTAINER_DLL_insert_tail (ch->head_sent,
1851 LOG (GNUNET_ERROR_TYPE_DEBUG,
1852 "Sending message %u from local client to %s with %u bytes\n",
1853 ntohl (crm->data_message->mid.mid),
1856 if (NULL != ch->retry_data_task)
1858 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1859 ch->retry_data_task = NULL;
1861 crm->qe = GCT_send (ch->t,
1862 &crm->data_message->header,
1865 GNUNET_assert (NULL == ch->retry_data_task);
1871 * Handle ACK from client on local channel. Means the client is ready
1872 * for more data, see if we have any for it.
1874 * @param ch channel to destroy
1875 * @param client_ccn ccn of the client sending the ack
1878 GCCH_handle_local_ack (struct CadetChannel *ch,
1879 struct GNUNET_CADET_ClientChannelNumber client_ccn)
1881 struct CadetChannelClient *ccc;
1882 struct CadetOutOfOrderMessage *com;
1884 if ( (NULL != ch->owner) &&
1885 (ch->owner->ccn.channel_of_client == client_ccn.channel_of_client) )
1887 else if ( (NULL != ch->dest) &&
1888 (ch->dest->ccn.channel_of_client == client_ccn.channel_of_client) )
1892 ccc->client_ready = GNUNET_YES;
1893 com = ccc->head_recv;
1896 LOG (GNUNET_ERROR_TYPE_DEBUG,
1897 "Got LOCAL_ACK, %s-%X ready to receive more data, but none pending on %s-%X(%p)!\n",
1899 ntohl (client_ccn.channel_of_client),
1901 ntohl (ccc->ccn.channel_of_client),
1903 return; /* none pending */
1905 if (GNUNET_YES == ch->is_loopback)
1909 /* Messages are always in-order, just send */
1910 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1914 GSC_send_to_client (ccc->c,
1916 /* Notify sender that we can receive more */
1917 if ( (NULL != ch->owner) &&
1918 (ccc->ccn.channel_of_client ==
1919 ch->owner->ccn.channel_of_client) )
1921 to_owner = GNUNET_NO;
1925 GNUNET_assert ( (NULL != ch->dest) &&
1926 (ccc->ccn.channel_of_client ==
1927 ch->dest->ccn.channel_of_client) );
1928 to_owner = GNUNET_YES;
1930 send_ack_to_client (ch,
1936 if ( (com->mid.mid != ch->mid_recv.mid) &&
1937 (GNUNET_NO == ch->out_of_order) &&
1938 (GNUNET_YES == ch->reliable) )
1940 LOG (GNUNET_ERROR_TYPE_DEBUG,
1941 "Got LOCAL_ACK, %s-%X ready to receive more data (but next one is out-of-order %u vs. %u)!\n",
1943 ntohl (ccc->ccn.channel_of_client),
1944 ntohl (com->mid.mid),
1945 ntohl (ch->mid_recv.mid));
1946 return; /* missing next one in-order */
1949 LOG (GNUNET_ERROR_TYPE_DEBUG,
1950 "Got LOCAL_ACK, giving payload message %u to %s-%X on %s\n",
1951 ntohl (com->mid.mid),
1953 ntohl (ccc->ccn.channel_of_client),
1956 /* all good, pass next message to client */
1957 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1961 /* FIXME: if unreliable, this is not aggressive
1962 enough, as it would be OK to have lost some! */
1964 ch->mid_recv.mid = htonl (1 + ntohl (com->mid.mid));
1965 ch->mid_futures >>= 1; /* equivalent to division by 2 */
1966 ccc->client_ready = GNUNET_NO;
1967 GSC_send_to_client (ccc->c,
1970 send_channel_data_ack (ch);
1971 if (NULL != ccc->head_recv)
1973 if (GNUNET_NO == ch->destroy)
1975 GCT_send_channel_destroy (ch->t,
1977 channel_destroy (ch);
1981 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-chn",__VA_ARGS__)
1987 * @param ch Channel.
1988 * @param level Debug level to use.
1991 GCCH_debug (struct CadetChannel *ch,
1992 enum GNUNET_ErrorType level)
1996 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
1998 __FILE__, __FUNCTION__, __LINE__);
2004 LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
2012 if (NULL != ch->owner)
2015 "CHN origin %s ready %s local-id: %u\n",
2016 GSC_2s (ch->owner->c),
2017 ch->owner->client_ready ? "YES" : "NO",
2018 ntohl (ch->owner->ccn.channel_of_client));
2020 if (NULL != ch->dest)
2023 "CHN destination %s ready %s local-id: %u\n",
2024 GSC_2s (ch->dest->c),
2025 ch->dest->client_ready ? "YES" : "NO",
2026 ntohl (ch->dest->ccn.channel_of_client));
2029 "CHN Message IDs recv: %d (%LLX), send: %d\n",
2030 ntohl (ch->mid_recv.mid),
2031 (unsigned long long) ch->mid_futures,
2032 ntohl (ch->mid_send.mid));
2037 /* end of gnunet-service-cadet-new_channel.c */