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 channel can be in.
81 enum CadetChannelState
84 * Uninitialized status, should never appear in operation.
89 * Channel is to a port that is not open, we're waiting for the
95 * CHANNEL_OPEN message sent, waiting for CHANNEL_OPEN_ACK.
97 CADET_CHANNEL_OPEN_SENT,
100 * Connection confirmed, ready to carry traffic.
107 * Info needed to retry a message in case it gets lost.
108 * Note that we DO use this structure also for unreliable
111 struct CadetReliableMessage
114 * Double linked list, FIFO style
116 struct CadetReliableMessage *next;
119 * Double linked list, FIFO style
121 struct CadetReliableMessage *prev;
124 * Which channel is this message in?
126 struct CadetChannel *ch;
129 * Entry in the tunnels queue for this message, NULL if it has left
130 * the tunnel. Used to cancel transmission in case we receive an
133 struct CadetTunnelQueueEntry *qe;
136 * Data message we are trying to send.
138 struct GNUNET_CADET_ChannelAppDataMessage *data_message;
141 * How soon should we retry if we fail to get an ACK?
142 * Messages in the queue are sorted by this value.
144 struct GNUNET_TIME_Absolute next_retry;
147 * How long do we wait for an ACK after transmission?
148 * Use for the back-off calculation.
150 struct GNUNET_TIME_Relative retry_delay;
153 * Time when we first successfully transmitted the message
154 * (that is, set @e num_transmissions to 1).
156 struct GNUNET_TIME_Absolute first_transmission_time;
159 * Identifier of the connection that this message took when it
160 * was first transmitted. Only useful if @e num_transmissions is 1.
162 struct GNUNET_CADET_ConnectionTunnelIdentifier connection_taken;
165 * How often was this message transmitted? #GNUNET_SYSERR if there
166 * was an error transmitting the message, #GNUNET_NO if it was not
167 * yet transmitted ever, otherwise the number of (re) transmissions.
169 int num_transmissions;
175 * List of received out-of-order data messages.
177 struct CadetOutOfOrderMessage
180 * Double linked list, FIFO style
182 struct CadetOutOfOrderMessage *next;
185 * Double linked list, FIFO style
187 struct CadetOutOfOrderMessage *prev;
190 * ID of the message (messages up to this point needed
191 * before we give this one to the client).
193 struct ChannelMessageIdentifier mid;
196 * The envelope with the payload of the out-of-order message
198 struct GNUNET_MQ_Envelope *env;
204 * Client endpoint of a `struct CadetChannel`. A channel may be a
205 * loopback channel, in which case it has two of these endpoints.
206 * Note that flow control also is required in both directions.
208 struct CadetChannelClient
211 * Client handle. Not by itself sufficient to designate
212 * the client endpoint, as the same client handle may
213 * be used for both the owner and the destination, and
214 * we thus also need the channel ID to identify the client.
216 struct CadetClient *c;
219 * Head of DLL of messages received out of order or while client was unready.
221 struct CadetOutOfOrderMessage *head_recv;
224 * Tail DLL of messages received out of order or while client was unready.
226 struct CadetOutOfOrderMessage *tail_recv;
229 * Local tunnel number for this client.
230 * (if owner >= #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI,
231 * otherwise < #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
233 struct GNUNET_CADET_ClientChannelNumber ccn;
236 * Number of entries currently in @a head_recv DLL.
238 unsigned int num_recv;
241 * Can we send data to the client?
249 * Struct containing all information regarding a channel to a remote client.
254 * Tunnel this channel is in.
256 struct CadetTunnel *t;
259 * Client owner of the tunnel, if any.
260 * (Used if this channel represends the initiating end of the tunnel.)
262 struct CadetChannelClient *owner;
265 * Client destination of the tunnel, if any.
266 * (Used if this channel represents the listening end of the tunnel.)
268 struct CadetChannelClient *dest;
271 * Last entry in the tunnel's queue relating to control messages
272 * (#GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN or
273 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK). Used to cancel
274 * transmission in case we receive updated information.
276 struct CadetTunnelQueueEntry *last_control_qe;
279 * Head of DLL of messages sent and not yet ACK'd.
281 struct CadetReliableMessage *head_sent;
284 * Tail of DLL of messages sent and not yet ACK'd.
286 struct CadetReliableMessage *tail_sent;
289 * Task to resend/poll in case no ACK is received.
291 struct GNUNET_SCHEDULER_Task *retry_control_task;
294 * Task to resend/poll in case no ACK is received.
296 struct GNUNET_SCHEDULER_Task *retry_data_task;
299 * Last time the channel was used
301 struct GNUNET_TIME_Absolute timestamp;
304 * Destination port of the channel.
306 struct GNUNET_HashCode port;
309 * Counter for exponential backoff.
311 struct GNUNET_TIME_Relative retry_time;
314 * Bitfield of already-received messages past @e mid_recv.
316 uint64_t mid_futures;
319 * Next MID expected for incoming traffic.
321 struct ChannelMessageIdentifier mid_recv;
324 * Next MID to use for outgoing traffic.
326 struct ChannelMessageIdentifier mid_send;
329 * Total (reliable) messages pending ACK for this channel.
331 unsigned int pending_messages;
334 * Maximum (reliable) messages pending ACK for this channel
335 * before we throttle the client.
337 unsigned int max_pending_messages;
340 * Number identifying this channel in its tunnel.
342 struct GNUNET_CADET_ChannelTunnelNumber ctn;
347 enum CadetChannelState state;
350 * Count how many ACKs we skipped, used to prevent long
351 * sequences of ACK skipping.
353 unsigned int skip_ack_series;
356 * Is the tunnel bufferless (minimum latency)?
361 * Is the tunnel reliable?
366 * Is the tunnel out-of-order?
371 * Is this channel a loopback channel, where the destination is us again?
376 * Flag to signal the destruction of the channel. If this is set to
377 * #GNUNET_YES the channel will be destroyed once the queue is
386 * Get the static string for identification of the channel.
390 * @return Static string with the channel IDs.
393 GCCH_2s (const struct CadetChannel *ch)
395 static char buf[128];
397 GNUNET_snprintf (buf,
399 "Channel %s:%s ctn:%X(%X/%X)",
400 (GNUNET_YES == ch->is_loopback)
402 : GNUNET_i2s (GCP_get_id (GCT_get_destination (ch->t))),
403 GNUNET_h2s (&ch->port),
405 (NULL == ch->owner) ? 0 : ntohl (ch->owner->ccn.channel_of_client),
406 (NULL == ch->dest) ? 0 : ntohl (ch->dest->ccn.channel_of_client));
412 * Get the channel's public ID.
416 * @return ID used to identify the channel with the remote peer.
418 struct GNUNET_CADET_ChannelTunnelNumber
419 GCCH_get_id (const struct CadetChannel *ch)
426 * Release memory associated with @a ccc
428 * @param ccc data structure to clean up
431 free_channel_client (struct CadetChannelClient *ccc)
433 struct CadetOutOfOrderMessage *com;
435 while (NULL != (com = ccc->head_recv))
437 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
441 GNUNET_MQ_discard (com->env);
449 * Destroy the given channel.
451 * @param ch channel to destroy
454 channel_destroy (struct CadetChannel *ch)
456 struct CadetReliableMessage *crm;
458 while (NULL != (crm = ch->head_sent))
460 GNUNET_assert (ch == crm->ch);
463 GCT_send_cancel (crm->qe);
466 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
469 GNUNET_free (crm->data_message);
472 if (NULL != ch->owner)
474 free_channel_client (ch->owner);
477 if (NULL != ch->dest)
479 free_channel_client (ch->dest);
482 if (NULL != ch->last_control_qe)
484 GCT_send_cancel (ch->last_control_qe);
485 ch->last_control_qe = NULL;
487 if (NULL != ch->retry_data_task)
489 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
490 ch->retry_data_task = NULL;
492 if (NULL != ch->retry_control_task)
494 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
495 ch->retry_control_task = NULL;
497 if (GNUNET_NO == ch->is_loopback)
499 GCT_remove_channel (ch->t,
509 * Send a channel create message.
511 * @param cls Channel for which to send.
514 send_channel_open (void *cls);
518 * Function called once the tunnel confirms that we sent the
519 * create message. Delays for a bit until we retry.
521 * @param cls our `struct CadetChannel`.
522 * @param cid identifier of the connection within the tunnel, NULL
523 * if transmission failed
526 channel_open_sent_cb (void *cls,
527 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
529 struct CadetChannel *ch = cls;
531 GNUNET_assert (NULL != ch->last_control_qe);
532 ch->last_control_qe = NULL;
533 ch->retry_time = GNUNET_TIME_STD_BACKOFF (ch->retry_time);
534 LOG (GNUNET_ERROR_TYPE_DEBUG,
535 "Sent CADET_CHANNEL_OPEN on %s, retrying in %s\n",
537 GNUNET_STRINGS_relative_time_to_string (ch->retry_time,
539 ch->retry_control_task
540 = GNUNET_SCHEDULER_add_delayed (ch->retry_time,
547 * Send a channel open message.
549 * @param cls Channel for which to send.
552 send_channel_open (void *cls)
554 struct CadetChannel *ch = cls;
555 struct GNUNET_CADET_ChannelOpenMessage msgcc;
558 ch->retry_control_task = NULL;
559 LOG (GNUNET_ERROR_TYPE_DEBUG,
560 "Sending CHANNEL_OPEN message for %s\n",
564 options |= GNUNET_CADET_OPTION_NOBUFFER;
566 options |= GNUNET_CADET_OPTION_RELIABLE;
567 if (ch->out_of_order)
568 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
569 msgcc.header.size = htons (sizeof (msgcc));
570 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
571 msgcc.opt = htonl (options);
572 msgcc.port = ch->port;
574 ch->state = CADET_CHANNEL_OPEN_SENT;
575 if (NULL != ch->last_control_qe)
576 GCT_send_cancel (ch->last_control_qe);
577 ch->last_control_qe = GCT_send (ch->t,
579 &channel_open_sent_cb,
581 GNUNET_assert (NULL == ch->retry_control_task);
586 * Function called once and only once after a channel was bound
587 * to its tunnel via #GCT_add_channel() is ready for transmission.
588 * Note that this is only the case for channels that this peer
589 * initiates, as for incoming channels we assume that they are
590 * ready for transmission immediately upon receiving the open
591 * message. Used to bootstrap the #GCT_send() process.
593 * @param ch the channel for which the tunnel is now ready
596 GCCH_tunnel_up (struct CadetChannel *ch)
598 GNUNET_assert (NULL == ch->retry_control_task);
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 "Tunnel up, sending CHANNEL_OPEN on %s now\n",
602 ch->retry_control_task
603 = GNUNET_SCHEDULER_add_now (&send_channel_open,
609 * Create a new channel.
611 * @param owner local client owning the channel
612 * @param ccn local number of this channel at the @a owner
613 * @param destination peer to which we should build the channel
614 * @param port desired port at @a destination
615 * @param options options for the channel
616 * @return handle to the new channel
618 struct CadetChannel *
619 GCCH_channel_local_new (struct CadetClient *owner,
620 struct GNUNET_CADET_ClientChannelNumber ccn,
621 struct CadetPeer *destination,
622 const struct GNUNET_HashCode *port,
625 struct CadetChannel *ch;
626 struct CadetChannelClient *ccco;
628 ccco = GNUNET_new (struct CadetChannelClient);
631 ccco->client_ready = GNUNET_YES;
633 ch = GNUNET_new (struct CadetChannel);
634 ch->mid_recv.mid = htonl (1); /* The OPEN_ACK counts as message 0! */
635 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
636 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
637 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
638 ch->max_pending_messages = (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
641 if (0 == memcmp (&my_full_id,
642 GCP_get_id (destination),
643 sizeof (struct GNUNET_PeerIdentity)))
645 struct CadetClient *c;
647 ch->is_loopback = GNUNET_YES;
648 c = GNUNET_CONTAINER_multihashmap_get (open_ports,
652 /* port closed, wait for it to possibly open */
653 ch->state = CADET_CHANNEL_LOOSE;
654 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
657 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
658 LOG (GNUNET_ERROR_TYPE_DEBUG,
659 "Created loose incoming loopback channel to port %s\n",
660 GNUNET_h2s (&ch->port));
670 ch->t = GCP_get_tunnel (destination,
672 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
673 ch->ctn = GCT_add_channel (ch->t,
676 GNUNET_STATISTICS_update (stats,
680 LOG (GNUNET_ERROR_TYPE_DEBUG,
681 "Created channel to port %s at peer %s for %s using %s\n",
683 GCP_2s (destination),
685 (GNUNET_YES == ch->is_loopback) ? "loopback" : GCT_2s (ch->t));
691 * We had an incoming channel to a port that is closed.
692 * It has not been opened for a while, drop it.
694 * @param cls the channel to drop
697 timeout_closed_cb (void *cls)
699 struct CadetChannel *ch = cls;
701 ch->retry_control_task = NULL;
702 LOG (GNUNET_ERROR_TYPE_DEBUG,
703 "Closing incoming channel to port %s from peer %s due to timeout\n",
704 GNUNET_h2s (&ch->port),
705 GCP_2s (GCT_get_destination (ch->t)));
706 channel_destroy (ch);
711 * Create a new channel based on a request coming in over the network.
713 * @param t tunnel to the remote peer
714 * @param ctn identifier of this channel in the tunnel
715 * @param port desired local port
716 * @param options options for the channel
717 * @return handle to the new channel
719 struct CadetChannel *
720 GCCH_channel_incoming_new (struct CadetTunnel *t,
721 struct GNUNET_CADET_ChannelTunnelNumber ctn,
722 const struct GNUNET_HashCode *port,
725 struct CadetChannel *ch;
726 struct CadetClient *c;
728 ch = GNUNET_new (struct CadetChannel);
732 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
733 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
734 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
735 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
736 ch->max_pending_messages = (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
737 GNUNET_STATISTICS_update (stats,
742 c = GNUNET_CONTAINER_multihashmap_get (open_ports,
746 /* port closed, wait for it to possibly open */
747 ch->state = CADET_CHANNEL_LOOSE;
748 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
751 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
752 ch->retry_control_task
753 = GNUNET_SCHEDULER_add_delayed (TIMEOUT_CLOSED_PORT,
756 LOG (GNUNET_ERROR_TYPE_DEBUG,
757 "Created loose incoming channel to port %s from peer %s\n",
758 GNUNET_h2s (&ch->port),
759 GCP_2s (GCT_get_destination (ch->t)));
766 GNUNET_STATISTICS_update (stats,
775 * Function called once the tunnel confirms that we sent the
776 * ACK message. Just remembers it was sent, we do not expect
779 * @param cls our `struct CadetChannel`.
780 * @param cid identifier of the connection within the tunnel, NULL
781 * if transmission failed
784 send_ack_cb (void *cls,
785 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
787 struct CadetChannel *ch = cls;
789 GNUNET_assert (NULL != ch->last_control_qe);
790 ch->last_control_qe = NULL;
795 * Compute and send the current #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK to the other peer.
797 * @param ch channel to send the #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK for
800 send_channel_data_ack (struct CadetChannel *ch)
802 struct GNUNET_CADET_ChannelDataAckMessage msg;
804 if (GNUNET_NO == ch->reliable)
805 return; /* no ACKs */
806 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK);
807 msg.header.size = htons (sizeof (msg));
809 msg.mid.mid = htonl (ntohl (ch->mid_recv.mid));
810 msg.futures = GNUNET_htonll (ch->mid_futures);
811 LOG (GNUNET_ERROR_TYPE_DEBUG,
812 "Sending DATA_ACK %u:%llX via %s\n",
813 (unsigned int) ntohl (msg.mid.mid),
814 (unsigned long long) ch->mid_futures,
816 if (NULL != ch->last_control_qe)
817 GCT_send_cancel (ch->last_control_qe);
818 ch->last_control_qe = GCT_send (ch->t,
826 * Send our initial #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK to the client confirming that the
829 * @param cls the `struct CadetChannel`
832 send_open_ack (void *cls)
834 struct CadetChannel *ch = cls;
835 struct GNUNET_CADET_ChannelManageMessage msg;
837 LOG (GNUNET_ERROR_TYPE_DEBUG,
838 "Sending CHANNEL_OPEN_ACK on %s\n",
840 ch->retry_control_task = NULL;
841 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK);
842 msg.header.size = htons (sizeof (msg));
843 msg.reserved = htonl (0);
845 if (NULL != ch->last_control_qe)
846 GCT_send_cancel (ch->last_control_qe);
847 ch->last_control_qe = GCT_send (ch->t,
855 * We got a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN message again for
856 * this channel. If the binding was successful, (re)transmit the
857 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK.
859 * @param ch channel that got the duplicate open
860 * @param cti identifier of the connection that delivered the message
863 GCCH_handle_duplicate_open (struct CadetChannel *ch,
864 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
866 if (NULL == ch->dest)
868 LOG (GNUNET_ERROR_TYPE_DEBUG,
869 "Ignoring duplicate CHANNEL_OPEN on %s: port is closed\n",
873 if (NULL != ch->retry_control_task)
875 LOG (GNUNET_ERROR_TYPE_DEBUG,
876 "Ignoring duplicate CHANNEL_OPEN on %s: control message is pending\n",
880 LOG (GNUNET_ERROR_TYPE_DEBUG,
881 "Retransmitting CHANNEL_OPEN_ACK on %s\n",
883 ch->retry_control_task
884 = GNUNET_SCHEDULER_add_now (&send_open_ack,
890 * Send a #GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK to the client to solicit more messages.
892 * @param ch channel the ack is for
893 * @param to_owner #GNUNET_YES to send to owner,
894 * #GNUNET_NO to send to dest
897 send_ack_to_client (struct CadetChannel *ch,
900 struct GNUNET_MQ_Envelope *env;
901 struct GNUNET_CADET_LocalAck *ack;
902 struct CadetChannelClient *ccc;
904 ccc = (GNUNET_YES == to_owner) ? ch->owner : ch->dest;
907 /* This can happen if we are just getting ACKs after
908 our local client already disconnected. */
909 GNUNET_assert (GNUNET_YES == ch->destroy);
912 env = GNUNET_MQ_msg (ack,
913 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
915 LOG (GNUNET_ERROR_TYPE_DEBUG,
916 "Sending CADET_LOCAL_ACK to %s (%s) at ccn %X (%u/%u pending)\n",
918 (GNUNET_YES == to_owner) ? "owner" : "dest",
919 ntohl (ack->ccn.channel_of_client),
920 ch->pending_messages,
921 ch->max_pending_messages);
922 GSC_send_to_client (ccc->c,
928 * A client is bound to the port that we have a channel
929 * open to. Send the acknowledgement for the connection
930 * request and establish the link with the client.
932 * @param ch open incoming channel
933 * @param c client listening on the respective port
936 GCCH_bind (struct CadetChannel *ch,
937 struct CadetClient *c)
940 struct CadetChannelClient *cccd;
942 LOG (GNUNET_ERROR_TYPE_DEBUG,
943 "Binding %s from %s to port %s of %s\n",
946 GNUNET_h2s (&ch->port),
948 if (NULL != ch->retry_control_task)
950 /* there might be a timeout task here */
951 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
952 ch->retry_control_task = NULL;
956 options |= GNUNET_CADET_OPTION_NOBUFFER;
958 options |= GNUNET_CADET_OPTION_RELIABLE;
959 if (ch->out_of_order)
960 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
961 cccd = GNUNET_new (struct CadetChannelClient);
962 GNUNET_assert (NULL == ch->dest);
965 cccd->client_ready = GNUNET_YES;
966 cccd->ccn = GSC_bind (c,
968 (GNUNET_YES == ch->is_loopback)
969 ? GCP_get (&my_full_id,
971 : GCT_get_destination (ch->t),
974 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) <
975 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
976 ch->mid_recv.mid = htonl (1); /* The OPEN counts as message 0! */
977 if (GNUNET_YES == ch->is_loopback)
979 ch->state = CADET_CHANNEL_OPEN_SENT;
980 GCCH_handle_channel_open_ack (ch,
985 /* notify other peer that we accepted the connection */
986 ch->state = CADET_CHANNEL_READY;
987 ch->retry_control_task
988 = GNUNET_SCHEDULER_add_now (&send_open_ack,
991 /* give client it's initial supply of ACKs */
992 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) <
993 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
994 for (unsigned int i=0;i<ch->max_pending_messages;i++)
995 send_ack_to_client (ch,
1001 * One of our clients has disconnected, tell the other one that we
1002 * are finished. Done asynchronously to avoid concurrent modification
1003 * issues if this is the same client.
1005 * @param cls the `struct CadetChannel` where one of the ends is now dead
1008 signal_remote_destroy_cb (void *cls)
1010 struct CadetChannel *ch = cls;
1011 struct CadetChannelClient *ccc;
1013 /* Find which end is left... */
1014 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1015 GSC_handle_remote_channel_destroy (ccc->c,
1018 channel_destroy (ch);
1023 * Destroy locally created channel. Called by the local client, so no
1024 * need to tell the client.
1026 * @param ch channel to destroy
1027 * @param c client that caused the destruction
1028 * @param ccn client number of the client @a c
1031 GCCH_channel_local_destroy (struct CadetChannel *ch,
1032 struct CadetClient *c,
1033 struct GNUNET_CADET_ClientChannelNumber ccn)
1035 LOG (GNUNET_ERROR_TYPE_DEBUG,
1036 "%s asks for destruction of %s\n",
1039 GNUNET_assert (NULL != c);
1040 if ( (NULL != ch->owner) &&
1041 (c == ch->owner->c) &&
1042 (ccn.channel_of_client == ch->owner->ccn.channel_of_client) )
1044 free_channel_client (ch->owner);
1047 else if ( (NULL != ch->dest) &&
1048 (c == ch->dest->c) &&
1049 (ccn.channel_of_client == ch->dest->ccn.channel_of_client) )
1051 free_channel_client (ch->dest);
1059 if (GNUNET_YES == ch->destroy)
1061 /* other end already destroyed, with the local client gone, no need
1062 to finish transmissions, just destroy immediately. */
1063 channel_destroy (ch);
1066 if ( (NULL != ch->head_sent) &&
1067 ( (NULL != ch->owner) ||
1068 (NULL != ch->dest) ) )
1070 /* Wait for other end to destroy us as well,
1071 and otherwise allow send queue to be transmitted first */
1072 ch->destroy = GNUNET_YES;
1075 if ( (GNUNET_YES == ch->is_loopback) &&
1076 ( (NULL != ch->owner) ||
1077 (NULL != ch->dest) ) )
1079 if (NULL != ch->retry_control_task)
1080 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
1081 ch->retry_control_task
1082 = GNUNET_SCHEDULER_add_now (&signal_remote_destroy_cb,
1086 if (GNUNET_NO == ch->is_loopback)
1088 /* If the we ever sent the CHANNEL_CREATE, we need to send a destroy message. */
1091 case CADET_CHANNEL_NEW:
1092 /* We gave up on a channel that we created as a client to a remote
1093 target, but that never went anywhere. Nothing to do here. */
1095 case CADET_CHANNEL_LOOSE:
1096 GSC_drop_loose_channel (&ch->port,
1100 GCT_send_channel_destroy (ch->t,
1104 /* Nothing left to do, just finish destruction */
1105 channel_destroy (ch);
1110 * We got an acknowledgement for the creation of the channel
1111 * (the port is open on the other side). Begin transmissions.
1113 * @param ch channel to destroy
1114 * @param cti identifier of the connection that delivered the message
1117 GCCH_handle_channel_open_ack (struct CadetChannel *ch,
1118 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
1122 case CADET_CHANNEL_NEW:
1123 /* this should be impossible */
1126 case CADET_CHANNEL_LOOSE:
1127 /* This makes no sense. */
1128 GNUNET_break_op (0);
1130 case CADET_CHANNEL_OPEN_SENT:
1131 if (NULL == ch->owner)
1133 /* We're not the owner, wrong direction! */
1134 GNUNET_break_op (0);
1137 LOG (GNUNET_ERROR_TYPE_DEBUG,
1138 "Received CHANNEL_OPEN_ACK for waiting %s, entering READY state\n",
1140 if (NULL != ch->retry_control_task) /* can be NULL if ch->is_loopback */
1142 GNUNET_SCHEDULER_cancel (ch->retry_control_task);
1143 ch->retry_control_task = NULL;
1145 ch->state = CADET_CHANNEL_READY;
1146 /* On first connect, send client as many ACKs as we allow messages
1148 for (unsigned int i=0;i<ch->max_pending_messages;i++)
1149 send_ack_to_client (ch,
1152 case CADET_CHANNEL_READY:
1153 /* duplicate ACK, maybe we retried the CREATE. Ignore. */
1154 LOG (GNUNET_ERROR_TYPE_DEBUG,
1155 "Received duplicate channel OPEN_ACK for %s\n",
1157 GNUNET_STATISTICS_update (stats,
1158 "# duplicate CREATE_ACKs",
1167 * Test if element @a e1 comes before element @a e2.
1169 * @param cls closure, to a flag where we indicate duplicate packets
1170 * @param m1 a message of to sort
1171 * @param m2 another message to sort
1172 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1175 is_before (void *cls,
1176 struct CadetOutOfOrderMessage *m1,
1177 struct CadetOutOfOrderMessage *m2)
1179 int *duplicate = cls;
1180 uint32_t v1 = ntohl (m1->mid.mid);
1181 uint32_t v2 = ntohl (m2->mid.mid);
1186 *duplicate = GNUNET_YES;
1187 if (delta > (uint32_t) INT_MAX)
1189 /* in overflow range, we can safely assume we wrapped around */
1194 /* result is small, thus v2 > v1, thus m1 < m2 */
1201 * We got payload data for a channel. Pass it on to the client
1202 * and send an ACK to the other end (once flow control allows it!)
1204 * @param ch channel that got data
1205 * @param cti identifier of the connection that delivered the message
1206 * @param msg message that was received
1209 GCCH_handle_channel_plaintext_data (struct CadetChannel *ch,
1210 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1211 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
1213 struct GNUNET_MQ_Envelope *env;
1214 struct GNUNET_CADET_LocalData *ld;
1215 struct CadetChannelClient *ccc;
1216 size_t payload_size;
1217 struct CadetOutOfOrderMessage *com;
1224 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1225 if ( (GNUNET_YES == ch->destroy) &&
1226 (NULL == ch->owner) &&
1227 (NULL == ch->dest) )
1229 /* This client is gone, but we still have messages to send to
1230 the other end (which is why @a ch is not yet dead). However,
1231 we cannot pass messages to our client anymore. */
1232 LOG (GNUNET_ERROR_TYPE_DEBUG,
1233 "Dropping incoming payload on %s as this end is already closed\n",
1235 /* send back DESTROY notification to stop further retransmissions! */
1236 GCT_send_channel_destroy (ch->t,
1240 payload_size = ntohs (msg->header.size) - sizeof (*msg);
1241 env = GNUNET_MQ_msg_extra (ld,
1243 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1244 ld->ccn = (NULL == ch->dest) ? ch->owner->ccn : ch->dest->ccn;
1245 GNUNET_memcpy (&ld[1],
1248 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1249 if ( (GNUNET_YES == ccc->client_ready) &&
1250 ( (GNUNET_YES == ch->out_of_order) ||
1251 (msg->mid.mid == ch->mid_recv.mid) ) )
1253 LOG (GNUNET_ERROR_TYPE_DEBUG,
1254 "Giving %u bytes of payload with MID %u from %s to client %s\n",
1255 (unsigned int) payload_size,
1256 ntohl (msg->mid.mid),
1259 ccc->client_ready = GNUNET_NO;
1260 GSC_send_to_client (ccc->c,
1262 ch->mid_recv.mid = htonl (1 + ntohl (ch->mid_recv.mid));
1263 ch->mid_futures >>= 1;
1264 send_channel_data_ack (ch);
1268 if (GNUNET_YES == ch->reliable)
1270 /* check if message ought to be dropped because it is ancient/too distant/duplicate */
1271 mid_min = ntohl (ch->mid_recv.mid);
1272 mid_max = mid_min + ch->max_pending_messages;
1273 mid_msg = ntohl (msg->mid.mid);
1274 if ( ( (uint32_t) (mid_msg - mid_min) > ch->max_pending_messages) ||
1275 ( (uint32_t) (mid_max - mid_msg) > ch->max_pending_messages) )
1277 LOG (GNUNET_ERROR_TYPE_DEBUG,
1278 "%s at %u drops ancient or far-future message %u\n",
1280 (unsigned int) mid_min,
1281 ntohl (msg->mid.mid));
1283 GNUNET_STATISTICS_update (stats,
1284 "# duplicate DATA (ancient or future)",
1287 GNUNET_MQ_discard (env);
1288 send_channel_data_ack (ch);
1291 /* mark bit for future ACKs */
1292 delta = mid_msg - mid_min - 1; /* overflow/underflow are OK here */
1295 if (0 != (ch->mid_futures & (1LLU << delta)))
1297 /* Duplicate within the queue, drop also */
1298 LOG (GNUNET_ERROR_TYPE_DEBUG,
1299 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1300 (unsigned int) payload_size,
1302 ntohl (msg->mid.mid));
1303 GNUNET_STATISTICS_update (stats,
1307 GNUNET_MQ_discard (env);
1308 send_channel_data_ack (ch);
1311 ch->mid_futures |= (1LLU << delta);
1312 LOG (GNUNET_ERROR_TYPE_DEBUG,
1313 "Marked bit %llX for mid %u (base: %u); now: %llX\n",
1320 else /* ! ch->reliable */
1322 /* Channel is unreliable, so we do not ACK. But we also cannot
1323 allow buffering everything, so check if we have space... */
1324 if (ccc->num_recv >= ch->max_pending_messages)
1326 struct CadetOutOfOrderMessage *drop;
1328 /* Yep, need to drop. Drop the oldest message in
1330 LOG (GNUNET_ERROR_TYPE_DEBUG,
1331 "Queue full due slow client on %s, dropping oldest message\n",
1333 GNUNET_STATISTICS_update (stats,
1334 "# messages dropped due to slow client",
1337 drop = ccc->head_recv;
1338 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1342 GNUNET_MQ_discard (drop->env);
1347 /* Insert message into sorted out-of-order queue */
1348 com = GNUNET_new (struct CadetOutOfOrderMessage);
1349 com->mid = msg->mid;
1351 duplicate = GNUNET_NO;
1352 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetOutOfOrderMessage,
1359 if (GNUNET_YES == duplicate)
1361 /* Duplicate within the queue, drop also (this is not covered by
1362 the case above if "delta" >= 64, which could be the case if
1363 max_pending_messages is also >= 64 or if our client is unready
1364 and we are seeing retransmissions of the message our client is
1366 LOG (GNUNET_ERROR_TYPE_DEBUG,
1367 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1368 (unsigned int) payload_size,
1370 ntohl (msg->mid.mid));
1371 GNUNET_STATISTICS_update (stats,
1375 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1379 GNUNET_MQ_discard (com->env);
1381 send_channel_data_ack (ch);
1384 LOG (GNUNET_ERROR_TYPE_DEBUG,
1385 "Queued %s payload of %u bytes on %s-%X(%p) (mid %u, need %u first)\n",
1386 (GNUNET_YES == ccc->client_ready)
1388 : "client-not-ready",
1389 (unsigned int) payload_size,
1391 ntohl (ccc->ccn.channel_of_client),
1393 ntohl (msg->mid.mid),
1394 ntohl (ch->mid_recv.mid));
1395 /* NOTE: this ACK we _could_ skip, as the packet is out-of-order and
1396 the sender may already be transmitting the previous one. Needs
1397 experimental evaluation to see if/when this ACK helps or
1398 hurts. (We might even want another option.) */
1399 send_channel_data_ack (ch);
1404 * Function called once the tunnel has sent one of our messages.
1405 * If the message is unreliable, simply frees the `crm`. If the
1406 * message was reliable, calculate retransmission time and
1407 * wait for ACK (or retransmit).
1409 * @param cls the `struct CadetReliableMessage` that was sent
1410 * @param cid identifier of the connection within the tunnel, NULL
1411 * if transmission failed
1414 data_sent_cb (void *cls,
1415 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
1419 * We need to retry a transmission, the last one took too long to
1422 * @param cls the `struct CadetChannel` where we need to retransmit
1425 retry_transmission (void *cls)
1427 struct CadetChannel *ch = cls;
1428 struct CadetReliableMessage *crm = ch->head_sent;
1430 ch->retry_data_task = NULL;
1431 GNUNET_assert (NULL == crm->qe);
1432 LOG (GNUNET_ERROR_TYPE_DEBUG,
1433 "Retrying transmission on %s of message %u\n",
1435 (unsigned int) ntohl (crm->data_message->mid.mid));
1436 crm->qe = GCT_send (ch->t,
1437 &crm->data_message->header,
1440 GNUNET_assert (NULL == ch->retry_data_task);
1445 * We got an PLAINTEXT_DATA_ACK for a message in our queue, remove it from
1446 * the queue and tell our client that it can send more.
1448 * @param ch the channel that got the PLAINTEXT_DATA_ACK
1449 * @param cti identifier of the connection that delivered the message
1450 * @param crm the message that got acknowledged
1453 handle_matching_ack (struct CadetChannel *ch,
1454 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1455 struct CadetReliableMessage *crm)
1457 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1460 ch->pending_messages--;
1461 GNUNET_assert (ch->pending_messages < ch->max_pending_messages);
1462 LOG (GNUNET_ERROR_TYPE_DEBUG,
1463 "Received DATA_ACK on %s for message %u (%u ACKs pending)\n",
1465 (unsigned int) ntohl (crm->data_message->mid.mid),
1466 ch->pending_messages);
1467 if (NULL != crm->qe)
1469 GCT_send_cancel (crm->qe);
1472 if ( (1 == crm->num_transmissions) &&
1475 GCC_ack_observed (cti);
1476 if (0 == memcmp (cti,
1477 &crm->connection_taken,
1478 sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier)))
1480 GCC_latency_observed (cti,
1481 GNUNET_TIME_absolute_get_duration (crm->first_transmission_time));
1484 GNUNET_free (crm->data_message);
1486 send_ack_to_client (ch,
1494 * We got an acknowledgement for payload data for a channel.
1495 * Possibly resume transmissions.
1497 * @param ch channel that got the ack
1498 * @param cti identifier of the connection that delivered the message
1499 * @param ack details about what was received
1502 GCCH_handle_channel_plaintext_data_ack (struct CadetChannel *ch,
1503 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1504 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
1506 struct CadetReliableMessage *crm;
1507 struct CadetReliableMessage *crmn;
1513 GNUNET_break (GNUNET_NO == ch->is_loopback);
1514 if (GNUNET_NO == ch->reliable)
1516 /* not expecting ACKs on unreliable channel, odd */
1517 GNUNET_break_op (0);
1520 /* mid_base is the MID of the next message that the
1521 other peer expects (i.e. that is missing!), everything
1522 LOWER (but excluding mid_base itself) was received. */
1523 mid_base = ntohl (ack->mid.mid);
1524 mid_mask = GNUNET_htonll (ack->futures);
1526 for (crm = ch->head_sent;
1531 delta = (unsigned int) (ntohl (crm->data_message->mid.mid) - mid_base);
1532 if (delta >= UINT_MAX - ch->max_pending_messages)
1534 /* overflow, means crm was a bit in the past, so this ACK counts for it. */
1535 LOG (GNUNET_ERROR_TYPE_DEBUG,
1536 "Got DATA_ACK with base %u satisfying past message %u on %s\n",
1537 (unsigned int) mid_base,
1538 ntohl (crm->data_message->mid.mid),
1540 handle_matching_ack (ch,
1549 LOG (GNUNET_ERROR_TYPE_DEBUG,
1550 "Testing bit %llX for mid %u (base: %u)\n",
1552 ntohl (crm->data_message->mid.mid),
1554 if (0 != (mid_mask & (1LLU << delta)))
1556 LOG (GNUNET_ERROR_TYPE_DEBUG,
1557 "Got DATA_ACK with mask for %u on %s\n",
1558 ntohl (crm->data_message->mid.mid),
1560 handle_matching_ack (ch,
1566 if (GNUNET_NO == found)
1568 /* ACK for message we already dropped, might have been a
1569 duplicate ACK? Ignore. */
1570 LOG (GNUNET_ERROR_TYPE_DEBUG,
1571 "Duplicate DATA_ACK on %s, ignoring\n",
1573 GNUNET_STATISTICS_update (stats,
1574 "# duplicate DATA_ACKs",
1579 if (NULL != ch->retry_data_task)
1581 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1582 ch->retry_data_task = NULL;
1584 if ( (NULL != ch->head_sent) &&
1585 (NULL == ch->head_sent->qe) )
1587 = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry,
1588 &retry_transmission,
1594 * Destroy channel, based on the other peer closing the
1595 * connection. Also needs to remove this channel from
1598 * @param ch channel to destroy
1599 * @param cti identifier of the connection that delivered the message,
1600 * NULL if we are simulating receiving a destroy due to shutdown
1603 GCCH_handle_remote_destroy (struct CadetChannel *ch,
1604 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
1606 struct CadetChannelClient *ccc;
1608 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1609 LOG (GNUNET_ERROR_TYPE_DEBUG,
1610 "Received remote channel DESTROY for %s\n",
1612 if (GNUNET_YES == ch->destroy)
1614 /* Local client already gone, this is instant-death. */
1615 channel_destroy (ch);
1618 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1619 if (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;
1627 GSC_handle_remote_channel_destroy (ccc->c,
1630 channel_destroy (ch);
1635 * Test if element @a e1 comes before element @a e2.
1637 * @param cls closure, to a flag where we indicate duplicate packets
1638 * @param crm1 an element of to sort
1639 * @param crm2 another element to sort
1640 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1643 cmp_crm_by_next_retry (void *cls,
1644 struct CadetReliableMessage *crm1,
1645 struct CadetReliableMessage *crm2)
1647 if (crm1->next_retry.abs_value_us <
1648 crm2->next_retry.abs_value_us)
1655 * Function called once the tunnel has sent one of our messages.
1656 * If the message is unreliable, simply frees the `crm`. If the
1657 * message was reliable, calculate retransmission time and
1658 * wait for ACK (or retransmit).
1660 * @param cls the `struct CadetReliableMessage` that was sent
1661 * @param cid identifier of the connection within the tunnel, NULL
1662 * if transmission failed
1665 data_sent_cb (void *cls,
1666 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
1668 struct CadetReliableMessage *crm = cls;
1669 struct CadetChannel *ch = crm->ch;
1671 GNUNET_assert (GNUNET_NO == ch->is_loopback);
1672 GNUNET_assert (NULL != crm->qe);
1674 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
1677 if (GNUNET_NO == ch->reliable)
1679 GNUNET_free (crm->data_message);
1681 ch->pending_messages--;
1682 send_ack_to_client (ch,
1690 /* There was an error sending. */
1691 crm->num_transmissions = GNUNET_SYSERR;
1693 else if (GNUNET_SYSERR != crm->num_transmissions)
1695 /* Increment transmission counter, and possibly store @a cid
1696 if this was the first transmission. */
1697 crm->num_transmissions++;
1698 if (1 == crm->num_transmissions)
1700 crm->first_transmission_time = GNUNET_TIME_absolute_get ();
1701 crm->connection_taken = *cid;
1702 GCC_ack_expected (cid);
1705 if ( (0 == crm->retry_delay.rel_value_us) &&
1708 struct CadetConnection *cc = GCC_lookup (cid);
1711 crm->retry_delay = GCC_get_metrics (cc)->aged_latency;
1713 crm->retry_delay = ch->retry_time;
1715 crm->retry_delay = GNUNET_TIME_STD_BACKOFF (crm->retry_delay);
1716 crm->retry_delay = GNUNET_TIME_relative_max (crm->retry_delay,
1718 crm->next_retry = GNUNET_TIME_relative_to_absolute (crm->retry_delay);
1720 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetReliableMessage,
1721 cmp_crm_by_next_retry,
1726 LOG (GNUNET_ERROR_TYPE_DEBUG,
1727 "Message %u sent, next transmission on %s in %s\n",
1728 (unsigned int) ntohl (crm->data_message->mid.mid),
1730 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (ch->head_sent->next_retry),
1732 if (NULL == ch->head_sent->qe)
1734 if (NULL != ch->retry_data_task)
1735 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1737 = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry,
1738 &retry_transmission,
1745 * Handle data given by a client.
1747 * Check whether the client is allowed to send in this tunnel, save if
1748 * channel is reliable and send an ACK to the client if there is still
1749 * buffer space in the tunnel.
1751 * @param ch Channel.
1752 * @param sender_ccn ccn of the sender
1753 * @param buf payload to transmit.
1754 * @param buf_len number of bytes in @a buf
1755 * @return #GNUNET_OK if everything goes well,
1756 * #GNUNET_SYSERR in case of an error.
1759 GCCH_handle_local_data (struct CadetChannel *ch,
1760 struct GNUNET_CADET_ClientChannelNumber sender_ccn,
1764 struct CadetReliableMessage *crm;
1766 if (ch->pending_messages > ch->max_pending_messages)
1769 return GNUNET_SYSERR;
1771 if (GNUNET_YES == ch->destroy)
1773 /* we are going down, drop messages */
1776 ch->pending_messages++;
1778 if (GNUNET_YES == ch->is_loopback)
1780 struct CadetChannelClient *receiver;
1781 struct GNUNET_MQ_Envelope *env;
1782 struct GNUNET_CADET_LocalData *ld;
1785 env = GNUNET_MQ_msg_extra (ld,
1787 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1788 if ( (NULL != ch->owner) &&
1789 (sender_ccn.channel_of_client ==
1790 ch->owner->ccn.channel_of_client) )
1792 receiver = ch->dest;
1793 to_owner = GNUNET_NO;
1795 else if ( (NULL != ch->dest) &&
1796 (sender_ccn.channel_of_client ==
1797 ch->dest->ccn.channel_of_client) )
1799 receiver = ch->owner;
1800 to_owner = GNUNET_YES;
1805 return GNUNET_SYSERR;
1807 ld->ccn = receiver->ccn;
1808 GNUNET_memcpy (&ld[1],
1811 if (GNUNET_YES == receiver->client_ready)
1813 GSC_send_to_client (receiver->c,
1815 send_ack_to_client (ch,
1820 struct CadetOutOfOrderMessage *oom;
1822 oom = GNUNET_new (struct CadetOutOfOrderMessage);
1824 GNUNET_CONTAINER_DLL_insert_tail (receiver->head_recv,
1825 receiver->tail_recv,
1827 receiver->num_recv++;
1832 /* Everything is correct, send the message. */
1833 crm = GNUNET_malloc (sizeof (*crm));
1835 crm->data_message = GNUNET_malloc (sizeof (struct GNUNET_CADET_ChannelAppDataMessage)
1837 crm->data_message->header.size = htons (sizeof (struct GNUNET_CADET_ChannelAppDataMessage) + buf_len);
1838 crm->data_message->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA);
1839 ch->mid_send.mid = htonl (ntohl (ch->mid_send.mid) + 1);
1840 crm->data_message->mid = ch->mid_send;
1841 crm->data_message->ctn = ch->ctn;
1842 GNUNET_memcpy (&crm->data_message[1],
1845 GNUNET_CONTAINER_DLL_insert_tail (ch->head_sent,
1848 LOG (GNUNET_ERROR_TYPE_DEBUG,
1849 "Sending message %u from local client to %s with %u bytes\n",
1850 ntohl (crm->data_message->mid.mid),
1853 if (NULL != ch->retry_data_task)
1855 GNUNET_SCHEDULER_cancel (ch->retry_data_task);
1856 ch->retry_data_task = NULL;
1858 crm->qe = GCT_send (ch->t,
1859 &crm->data_message->header,
1862 GNUNET_assert (NULL == ch->retry_data_task);
1868 * Handle ACK from client on local channel. Means the client is ready
1869 * for more data, see if we have any for it.
1871 * @param ch channel to destroy
1872 * @param client_ccn ccn of the client sending the ack
1875 GCCH_handle_local_ack (struct CadetChannel *ch,
1876 struct GNUNET_CADET_ClientChannelNumber client_ccn)
1878 struct CadetChannelClient *ccc;
1879 struct CadetOutOfOrderMessage *com;
1881 if ( (NULL != ch->owner) &&
1882 (ch->owner->ccn.channel_of_client == client_ccn.channel_of_client) )
1884 else if ( (NULL != ch->dest) &&
1885 (ch->dest->ccn.channel_of_client == client_ccn.channel_of_client) )
1889 ccc->client_ready = GNUNET_YES;
1890 com = ccc->head_recv;
1893 LOG (GNUNET_ERROR_TYPE_DEBUG,
1894 "Got LOCAL_ACK, %s-%X ready to receive more data, but none pending on %s-%X(%p)!\n",
1896 ntohl (client_ccn.channel_of_client),
1898 ntohl (ccc->ccn.channel_of_client),
1900 return; /* none pending */
1902 if (GNUNET_YES == ch->is_loopback)
1906 /* Messages are always in-order, just send */
1907 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1911 GSC_send_to_client (ccc->c,
1913 /* Notify sender that we can receive more */
1914 if (ccc->ccn.channel_of_client ==
1915 ch->owner->ccn.channel_of_client)
1917 to_owner = GNUNET_NO;
1921 GNUNET_assert (ccc->ccn.channel_of_client ==
1922 ch->dest->ccn.channel_of_client);
1923 to_owner = GNUNET_YES;
1925 send_ack_to_client (ch,
1931 if ( (com->mid.mid != ch->mid_recv.mid) &&
1932 (GNUNET_NO == ch->out_of_order) &&
1933 (GNUNET_YES == ch->reliable) )
1935 LOG (GNUNET_ERROR_TYPE_DEBUG,
1936 "Got LOCAL_ACK, %s-%X ready to receive more data (but next one is out-of-order %u vs. %u)!\n",
1938 ntohl (ccc->ccn.channel_of_client),
1939 ntohl (com->mid.mid),
1940 ntohl (ch->mid_recv.mid));
1941 return; /* missing next one in-order */
1944 LOG (GNUNET_ERROR_TYPE_DEBUG,
1945 "Got LOCAL_ACK, giving payload message %u to %s-%X on %s\n",
1946 ntohl (com->mid.mid),
1948 ntohl (ccc->ccn.channel_of_client),
1951 /* all good, pass next message to client */
1952 GNUNET_CONTAINER_DLL_remove (ccc->head_recv,
1956 /* FIXME: if unreliable, this is not aggressive
1957 enough, as it would be OK to have lost some! */
1959 ch->mid_recv.mid = htonl (1 + ntohl (com->mid.mid));
1960 ch->mid_futures >>= 1; /* equivalent to division by 2 */
1961 ccc->client_ready = GNUNET_NO;
1962 GSC_send_to_client (ccc->c,
1965 send_channel_data_ack (ch);
1966 if (NULL != ccc->head_recv)
1968 if (GNUNET_NO == ch->destroy)
1970 GCT_send_channel_destroy (ch->t,
1972 channel_destroy (ch);
1976 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-chn",__VA_ARGS__)
1982 * @param ch Channel.
1983 * @param level Debug level to use.
1986 GCCH_debug (struct CadetChannel *ch,
1987 enum GNUNET_ErrorType level)
1991 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
1993 __FILE__, __FUNCTION__, __LINE__);
1999 LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
2007 if (NULL != ch->owner)
2010 "CHN origin %s ready %s local-id: %u\n",
2011 GSC_2s (ch->owner->c),
2012 ch->owner->client_ready ? "YES" : "NO",
2013 ntohl (ch->owner->ccn.channel_of_client));
2015 if (NULL != ch->dest)
2018 "CHN destination %s ready %s local-id: %u\n",
2019 GSC_2s (ch->dest->c),
2020 ch->dest->client_ready ? "YES" : "NO",
2021 ntohl (ch->dest->ccn.channel_of_client));
2024 "CHN Message IDs recv: %d (%LLX), send: %d\n",
2025 ntohl (ch->mid_recv.mid),
2026 (unsigned long long) ch->mid_futures,
2027 ntohl (ch->mid_send.mid));
2032 /* end of gnunet-service-cadet-new_channel.c */