2 This file is part of GNUnet.
3 Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_statistics_service.h"
28 #include "cadet_protocol.h"
30 #include "gnunet-service-cadet_channel.h"
31 #include "gnunet-service-cadet_local.h"
32 #include "gnunet-service-cadet_tunnel.h"
33 #include "gnunet-service-cadet_peer.h"
35 #define LOG(level, ...) GNUNET_log_from(level,"cadet-chn",__VA_ARGS__)
37 #define CADET_RETRANSMIT_TIME GNUNET_TIME_relative_multiply(\
38 GNUNET_TIME_UNIT_MILLISECONDS, 250)
39 #define CADET_RETRANSMIT_MARGIN 4
43 * All the states a connection can be in.
45 enum CadetChannelState
48 * Uninitialized status, should never appear in operation.
53 * Connection create message sent, waiting for ACK.
58 * Connection confirmed, ready to carry traffic.
65 * Info holder for channel messages in queues.
67 struct CadetChannelQueue
72 struct CadetTunnelQueue *tq;
75 * Message type (DATA/DATA_ACK)
80 * Message copy (for DATAs, to start retransmission timer)
82 struct CadetReliableMessage *copy;
85 * Reliability (for DATA_ACKs, to access rel->ack_q)
87 struct CadetChannelReliability *rel;
92 * Info needed to retry a message in case it gets lost.
94 struct CadetReliableMessage
97 * Double linked list, FIFO style
99 struct CadetReliableMessage *next;
100 struct CadetReliableMessage *prev;
103 * Type of message (payload, channel management).
108 * Tunnel Reliability queue this message is in.
110 struct CadetChannelReliability *rel;
113 * ID of the message (ACK needed to free)
120 struct CadetChannelQueue *chq;
123 * When was this message issued (to calculate ACK delay)
125 struct GNUNET_TIME_Absolute timestamp;
127 /* struct GNUNET_CADET_Data with payload */
132 * Info about the traffic state for a client in a channel.
134 struct CadetChannelReliability
137 * Channel this is about.
139 struct CadetChannel *ch;
142 * DLL of messages sent and not yet ACK'd.
144 struct CadetReliableMessage *head_sent;
145 struct CadetReliableMessage *tail_sent;
148 * DLL of messages received out of order.
150 struct CadetReliableMessage *head_recv;
151 struct CadetReliableMessage *tail_recv;
159 * Next MID to use for outgoing traffic.
164 * Next MID expected for incoming traffic.
169 * Handle for queued unique data CREATE, DATA_ACK.
171 struct CadetChannelQueue *uniq;
174 * Can we send data to the client?
179 * Can the client send data to us?
184 * Task to resend/poll in case no ACK is received.
186 struct GNUNET_SCHEDULER_Task * retry_task;
189 * Counter for exponential backoff.
191 struct GNUNET_TIME_Relative retry_timer;
194 * How long does it usually take to get an ACK.
196 struct GNUNET_TIME_Relative expected_delay;
201 * Struct containing all information regarding a channel to a remote client.
206 * Tunnel this channel is in.
208 struct CadetTunnel *t;
211 * Destination port of the channel.
216 * Global channel number ( < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
218 CADET_ChannelNumber gid;
221 * Local tunnel number for root (owner) client.
222 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI or 0 )
224 CADET_ChannelNumber lid_root;
227 * Local tunnel number for local destination clients (incoming number)
228 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV or 0).
230 CADET_ChannelNumber lid_dest;
235 enum CadetChannelState state;
238 * Is the tunnel bufferless (minimum latency)?
243 * Is the tunnel reliable?
248 * Last time the channel was used
250 struct GNUNET_TIME_Absolute timestamp;
253 * Client owner of the tunnel, if any
255 struct CadetClient *root;
258 * Client destination of the tunnel, if any.
260 struct CadetClient *dest;
263 * Flag to signal the destruction of the channel.
264 * If this is set GNUNET_YES the channel will be destroyed
265 * when the queue is empty.
270 * Total (reliable) messages pending ACK for this channel.
272 unsigned int pending_messages;
276 * Only present (non-NULL) at the owner of a tunnel.
278 struct CadetChannelReliability *root_rel;
282 * Only present (non-NULL) at the destination of a tunnel.
284 struct CadetChannelReliability *dest_rel;
289 /******************************************************************************/
290 /******************************* GLOBALS ***********************************/
291 /******************************************************************************/
294 * Global handle to the statistics service.
296 extern struct GNUNET_STATISTICS_Handle *stats;
299 * Local peer own ID (memory efficient handle).
301 extern GNUNET_PEER_Id myid;
304 /******************************************************************************/
305 /******************************** STATIC ***********************************/
306 /******************************************************************************/
310 * Destroy a reliable message after it has been acknowledged, either by
311 * direct mid ACK or bitfield. Updates the appropriate data structures and
312 * timers and frees all memory.
314 * @param copy Message that is no longer needed: remote peer got it.
315 * @param update_time Is the timing information relevant?
316 * If this message is ACK in a batch the timing information
317 * is skewed by the retransmission, count only for the
318 * retransmitted message.
320 * @return #GNUNET_YES if channel was destroyed as a result of the call,
321 * #GNUNET_NO otherwise.
324 rel_message_free (struct CadetReliableMessage *copy, int update_time);
327 * send a channel create message.
329 * @param ch Channel for which to send.
332 send_create (struct CadetChannel *ch);
335 * Confirm we got a channel create, FWD ack.
337 * @param ch The channel to confirm.
338 * @param fwd Should we send a FWD ACK? (going dest->root)
341 send_ack (struct CadetChannel *ch, int fwd);
346 * Test if the channel is loopback: both root and dest are on the local peer.
348 * @param ch Channel to test.
350 * @return #GNUNET_YES if channel is loopback, #GNUNET_NO otherwise.
353 is_loopback (const struct CadetChannel *ch)
356 return GCT_is_loopback (ch->t);
358 return (NULL != ch->root && NULL != ch->dest);
363 * Save a copy of the data message for later retransmission.
365 * @param msg Message to copy.
366 * @param mid Message ID.
367 * @param rel Reliability data for retransmission.
369 static struct CadetReliableMessage *
370 copy_message (const struct GNUNET_CADET_Data *msg, uint32_t mid,
371 struct CadetChannelReliability *rel)
373 struct CadetReliableMessage *copy;
376 size = ntohs (msg->header.size);
377 copy = GNUNET_malloc (sizeof (*copy) + size);
380 copy->type = GNUNET_MESSAGE_TYPE_CADET_DATA;
381 memcpy (©[1], msg, size);
387 * We have received a message out of order, or the client is not ready.
388 * Buffer it until we receive an ACK from the client or the missing
389 * message from the channel.
391 * @param msg Message to buffer (MUST be of type CADET_DATA).
392 * @param rel Reliability data to the corresponding direction.
395 add_buffered_data (const struct GNUNET_CADET_Data *msg,
396 struct CadetChannelReliability *rel)
398 struct CadetReliableMessage *copy;
399 struct CadetReliableMessage *prev;
402 mid = ntohl (msg->mid);
404 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data MID %u (%u)\n",
409 // FIXME do something better than O(n), although n < 64...
410 // FIXME start from the end (most messages are the latest ones)
411 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
413 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
414 if (prev->mid == mid)
416 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there!\n");
420 else if (GC_is_pid_bigger (prev->mid, mid))
422 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
423 copy = copy_message (msg, mid, rel);
424 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
429 copy = copy_message (msg, mid, rel);
430 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail! (now: %u)\n", rel->n_recv);
431 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
432 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
437 * Add a destination client to a channel, initializing all data structures
438 * in the channel and the client.
440 * @param ch Channel to which add the destination.
441 * @param c Client which to add to the channel.
444 add_destination (struct CadetChannel *ch, struct CadetClient *c)
446 if (NULL != ch->dest)
452 /* Assign local id as destination */
453 ch->lid_dest = GML_get_next_chid (c);
455 /* Store in client's hashmap */
456 GML_channel_add (c, ch->lid_dest, ch);
458 GNUNET_break (NULL == ch->dest_rel);
459 ch->dest_rel = GNUNET_new (struct CadetChannelReliability);
460 ch->dest_rel->ch = ch;
461 ch->dest_rel->expected_delay.rel_value_us = 0;
462 ch->dest_rel->retry_timer = CADET_RETRANSMIT_TIME;
469 * Set options in a channel, extracted from a bit flag field.
471 * @param ch Channel to set options to.
472 * @param options Bit array in host byte order.
475 channel_set_options (struct CadetChannel *ch, uint32_t options)
477 ch->nobuffer = (options & GNUNET_CADET_OPTION_NOBUFFER) != 0 ?
478 GNUNET_YES : GNUNET_NO;
479 ch->reliable = (options & GNUNET_CADET_OPTION_RELIABLE) != 0 ?
480 GNUNET_YES : GNUNET_NO;
485 * Get a bit flag field with the options of a channel.
487 * @param ch Channel to get options from.
489 * @return Bit array in host byte order.
492 channel_get_options (struct CadetChannel *ch)
498 options |= GNUNET_CADET_OPTION_NOBUFFER;
500 options |= GNUNET_CADET_OPTION_RELIABLE;
507 * Notify a client that the channel is no longer valid.
509 * @param ch Channel that is destroyed.
510 * @param local_only Should we avoid sending it to other peers?
513 send_destroy (struct CadetChannel *ch, int local_only)
515 struct GNUNET_CADET_ChannelManage msg;
517 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
518 msg.header.size = htons (sizeof (msg));
519 msg.chid = htonl (ch->gid);
521 /* If root is not NULL, notify.
522 * If it's NULL, check lid_root. When a local destroy comes in, root
523 * is set to NULL but lid_root is left untouched. In this case, do nothing,
524 * the client is the one who requested the channel to be destroyed.
526 if (NULL != ch->root)
527 GML_send_channel_destroy (ch->root, ch->lid_root);
528 else if (0 == ch->lid_root && GNUNET_NO == local_only)
529 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
531 if (NULL != ch->dest)
532 GML_send_channel_destroy (ch->dest, ch->lid_dest);
533 else if (0 == ch->lid_dest && GNUNET_NO == local_only)
534 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
539 * Notify the destination client that a new incoming channel was created.
541 * @param ch Channel that was created.
544 send_client_create (struct CadetChannel *ch)
548 if (NULL == ch->dest)
552 opt |= GNUNET_YES == ch->reliable ? GNUNET_CADET_OPTION_RELIABLE : 0;
553 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_CADET_OPTION_NOBUFFER : 0;
554 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
555 GCT_get_destination (ch->t));
561 * Send data to a client.
563 * If the client is ready, send directly, otherwise buffer while listening
567 * @param msg Message.
568 * @param fwd Is this a fwd (root->dest) message?
571 send_client_data (struct CadetChannel *ch,
572 const struct GNUNET_CADET_Data *msg,
577 if (ch->dest_rel->client_ready)
579 GML_send_data (ch->dest, msg, ch->lid_dest);
580 ch->dest_rel->client_ready = GNUNET_NO;
581 ch->dest_rel->mid_recv++;
584 add_buffered_data (msg, ch->dest_rel);
588 if (ch->root_rel->client_ready)
590 GML_send_data (ch->root, msg, ch->lid_root);
591 ch->root_rel->client_ready = GNUNET_NO;
592 ch->root_rel->mid_recv++;
595 add_buffered_data (msg, ch->root_rel);
601 * Send a buffered message to the client, for in order delivery or
602 * as result of client ACK.
604 * @param ch Channel on which to empty the message buffer.
605 * @param c Client to send to.
606 * @param fwd Is this to send FWD data?.
609 send_client_buffered_data (struct CadetChannel *ch,
610 struct CadetClient *c,
613 struct CadetReliableMessage *copy;
614 struct CadetChannelReliability *rel;
616 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
617 rel = fwd ? ch->dest_rel : ch->root_rel;
618 if (GNUNET_NO == rel->client_ready)
620 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
624 copy = rel->head_recv;
625 /* We never buffer channel management messages */
628 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
630 struct GNUNET_CADET_Data *msg = (struct GNUNET_CADET_Data *) ©[1];
632 LOG (GNUNET_ERROR_TYPE_DEBUG, " have %u! now expecting %u\n",
633 copy->mid, rel->mid_recv + 1);
634 send_client_data (ch, msg, fwd);
636 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
637 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE RECV %u (%p), %u left\n",
638 copy->mid, copy, rel->n_recv);
640 GCCH_send_data_ack (ch, fwd);
644 LOG (GNUNET_ERROR_TYPE_DEBUG, " reliable && don't have %u, next is %u\n",
645 rel->mid_recv, copy->mid);
646 if (GNUNET_YES == ch->destroy)
648 /* We don't have the next data piece and the remote peer has closed the
649 * channel. We won't receive it anymore, so just destroy the channel.
650 * FIXME: wait some time to allow other connections to
651 * deliver missing messages
653 send_destroy (ch, GNUNET_YES);
658 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
663 * Allow a client to send more data.
665 * In case the client was already allowed to send data, do nothing.
668 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
671 send_client_ack (struct CadetChannel *ch, int fwd)
673 struct CadetChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
674 struct CadetClient *c = fwd ? ch->root : ch->dest;
678 GNUNET_break (GNUNET_NO != ch->destroy);
681 LOG (GNUNET_ERROR_TYPE_DEBUG,
682 " sending %s ack to client on channel %s\n",
683 GC_f2s (fwd), GCCH_2s (ch));
691 if (GNUNET_YES == rel->client_allowed)
693 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
696 rel->client_allowed = GNUNET_YES;
698 GML_send_ack (c, fwd ? ch->lid_root : ch->lid_dest);
703 * Notify the root that the destination rejected the channel.
705 * @param ch Rejected channel.
708 send_client_nack (struct CadetChannel *ch)
710 if (NULL == ch->root)
715 GML_send_channel_nack (ch->root, ch->lid_root);
720 * We haven't received an ACK after a certain time: restransmit the message.
722 * @param cls Closure (CadetChannelReliability with the message to restransmit)
723 * @param tc TaskContext.
726 channel_retransmit_message (void *cls,
727 const struct GNUNET_SCHEDULER_TaskContext *tc)
729 struct CadetChannelReliability *rel = cls;
730 struct CadetReliableMessage *copy;
731 struct CadetChannel *ch;
732 struct GNUNET_CADET_Data *payload;
735 rel->retry_task = NULL;
736 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
740 copy = rel->head_sent;
743 GNUNET_break (0); // FIXME tripped in rps testcase
747 payload = (struct GNUNET_CADET_Data *) ©[1];
748 fwd = (rel == ch->root_rel);
750 /* Message not found in the queue that we are going to use. */
751 LOG (GNUNET_ERROR_TYPE_DEBUG, "RETRANSMIT MID %u\n", copy->mid);
753 GCCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
754 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
759 * We haven't received an Channel ACK after a certain time: resend the CREATE.
761 * @param cls Closure (CadetChannelReliability of the channel to recreate)
762 * @param tc TaskContext.
765 channel_recreate (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
767 struct CadetChannelReliability *rel = cls;
769 rel->retry_task = NULL;
770 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
773 LOG (GNUNET_ERROR_TYPE_DEBUG, "RE-CREATE\n");
774 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
776 if (rel == rel->ch->root_rel)
778 send_create (rel->ch);
780 else if (rel == rel->ch->dest_rel)
782 send_ack (rel->ch, GNUNET_YES);
793 * Message has been sent: start retransmission timer.
795 * @param cls Closure (queue structure).
797 * @param q Queue handler (no longer valid).
798 * @param type Type of message.
799 * @param size Size of the message.
802 ch_message_sent (void *cls,
803 struct CadetTunnel *t,
804 struct CadetTunnelQueue *q,
805 uint16_t type, size_t size)
807 struct CadetChannelQueue *chq = cls;
808 struct CadetReliableMessage *copy = chq->copy;
809 struct CadetChannelReliability *rel;
811 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel_message_sent callback %s\n",
816 case GNUNET_MESSAGE_TYPE_CADET_DATA:
817 LOG (GNUNET_ERROR_TYPE_DEBUG, "data MID %u sent\n", copy->mid);
818 GNUNET_assert (chq == copy->chq);
819 copy->timestamp = GNUNET_TIME_absolute_get ();
821 if (NULL == rel->retry_task)
823 LOG (GNUNET_ERROR_TYPE_DEBUG, " scheduling retry in 4 * %s\n",
824 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
826 if (0 != rel->expected_delay.rel_value_us)
829 GNUNET_TIME_relative_multiply (rel->expected_delay,
830 CADET_RETRANSMIT_MARGIN);
834 rel->retry_timer = CADET_RETRANSMIT_TIME;
836 LOG (GNUNET_ERROR_TYPE_DEBUG, " using delay %s\n",
837 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
840 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
841 &channel_retransmit_message, rel);
845 LOG (GNUNET_ERROR_TYPE_DEBUG, "retry running %p\n", rel->retry_task);
851 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
852 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
853 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
854 LOG (GNUNET_ERROR_TYPE_DEBUG, "sent %s\n", GC_m2s (chq->type));
856 GNUNET_assert (rel->uniq == chq);
859 if (CADET_CHANNEL_READY != rel->ch->state
860 && GNUNET_MESSAGE_TYPE_CADET_DATA_ACK != type
861 && GNUNET_NO == rel->ch->destroy)
863 GNUNET_assert (NULL == rel->retry_task);
864 LOG (GNUNET_ERROR_TYPE_DEBUG, "STD BACKOFF %s\n",
865 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
867 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
868 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
869 &channel_recreate, rel);
882 * send a channel create message.
884 * @param ch Channel for which to send.
887 send_create (struct CadetChannel *ch)
889 struct GNUNET_CADET_ChannelCreate msgcc;
891 msgcc.header.size = htons (sizeof (msgcc));
892 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
893 msgcc.chid = htonl (ch->gid);
894 msgcc.port = htonl (ch->port);
895 msgcc.opt = htonl (channel_get_options (ch));
897 GCCH_send_prebuilt_message (&msgcc.header, ch, GNUNET_YES, NULL);
902 * Confirm we got a channel create or FWD ack.
904 * @param ch The channel to confirm.
905 * @param fwd Should we send a FWD ACK? (going dest->root)
908 send_ack (struct CadetChannel *ch, int fwd)
910 struct GNUNET_CADET_ChannelManage msg;
912 msg.header.size = htons (sizeof (msg));
913 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK);
914 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending channel %s ack for channel %s\n",
915 GC_f2s (fwd), GCCH_2s (ch));
917 msg.chid = htonl (ch->gid);
918 GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
923 * Send a message and don't keep any info about it: we won't need to cancel it
926 * @param msg Header of the message to fire away.
927 * @param ch Channel on which the message should go.
928 * @param force Is this a forced (undroppable) message?
931 fire_and_forget (const struct GNUNET_MessageHeader *msg,
932 struct CadetChannel *ch,
935 GNUNET_break (NULL == GCT_send_prebuilt_message (msg, ch->t, NULL,
941 * Notify that a channel create didn't succeed.
943 * @param ch The channel to reject.
946 send_nack (struct CadetChannel *ch)
948 struct GNUNET_CADET_ChannelManage msg;
950 msg.header.size = htons (sizeof (msg));
951 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
952 LOG (GNUNET_ERROR_TYPE_DEBUG,
953 " sending channel NACK for channel %s\n",
956 msg.chid = htonl (ch->gid);
957 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
962 * Destroy all reliable messages queued for a channel,
963 * during a channel destruction.
964 * Frees the reliability structure itself.
966 * @param rel Reliability data for a channel.
969 channel_rel_free_all (struct CadetChannelReliability *rel)
971 struct CadetReliableMessage *copy;
972 struct CadetReliableMessage *next;
977 for (copy = rel->head_recv; NULL != copy; copy = next)
980 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
981 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE ALL RECV %p\n", copy);
982 GNUNET_break (NULL == copy->chq);
985 for (copy = rel->head_sent; NULL != copy; copy = next)
988 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
989 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE ALL SEND %p\n", copy);
990 if (NULL != copy->chq)
992 if (NULL != copy->chq->tq)
994 GCT_cancel (copy->chq->tq);
995 /* ch_message_sent will free copy->q */
999 GNUNET_free (copy->chq);
1005 if (NULL != rel->uniq && NULL != rel->uniq->tq)
1007 GCT_cancel (rel->uniq->tq);
1008 /* ch_message_sent is called freeing uniq */
1010 if (NULL != rel->retry_task)
1012 GNUNET_SCHEDULER_cancel (rel->retry_task);
1013 rel->retry_task = NULL;
1020 * Mark future messages as ACK'd.
1022 * @param rel Reliability data.
1023 * @param msg DataACK message with a bitfield of future ACK'd messages.
1026 channel_rel_free_sent (struct CadetChannelReliability *rel,
1027 const struct GNUNET_CADET_DataACK *msg)
1029 struct CadetReliableMessage *copy;
1030 struct CadetReliableMessage *next;
1037 bitfield = msg->futures;
1038 mid = ntohl (msg->mid);
1039 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable %u %llX\n", mid, bitfield);
1040 LOG (GNUNET_ERROR_TYPE_DEBUG, " rel %p, head %p\n", rel, rel->head_sent);
1041 for (i = 0, copy = rel->head_sent;
1042 i < 64 && NULL != copy && 0 != bitfield;
1045 LOG (GNUNET_ERROR_TYPE_DEBUG, " trying bit %u (mid %u)\n", i, mid + i + 1);
1047 if (0 == (bitfield & mask))
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
1051 /* Bit was set, clear the bit from the bitfield */
1054 /* The i-th bit was set. Do we have that copy? */
1055 /* Skip copies with mid < target */
1056 target = mid + i + 1;
1057 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
1058 while (NULL != copy && GC_is_pid_bigger (target, copy->mid))
1061 /* Did we run out of copies? (previously freed, it's ok) */
1064 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
1068 /* Did we overshoot the target? (previously freed, it's ok) */
1069 if (GC_is_pid_bigger (copy->mid, target))
1071 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
1072 i += copy->mid - target - 1; /* MID: 90, t = 85, i += 4 (i++ later) */
1073 mask = (0x1LL << (i + 1)) - 1; /* Mask = i-th bit and all before */
1074 bitfield &= ~mask; /* Clear all bits up to MID - 1 */
1078 /* Now copy->mid == target, free it */
1080 GNUNET_break (GNUNET_YES != rel_message_free (copy, GNUNET_YES));
1083 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
1088 * Destroy a reliable message after it has been acknowledged, either by
1089 * direct mid ACK or bitfield. Updates the appropriate data structures and
1090 * timers and frees all memory.
1092 * @param copy Message that is no longer needed: remote peer got it.
1093 * @param update_time Is the timing information relevant?
1094 * If this message is ACK in a batch the timing information
1095 * is skewed by the retransmission, count only for the
1096 * retransmitted message.
1098 * @return #GNUNET_YES if channel was destroyed as a result of the call,
1099 * #GNUNET_NO otherwise.
1102 rel_message_free (struct CadetReliableMessage *copy, int update_time)
1104 struct CadetChannelReliability *rel;
1105 struct GNUNET_TIME_Relative time;
1108 LOG (GNUNET_ERROR_TYPE_DEBUG, "TIME Freeing %u\n", copy->mid);
1111 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
1112 if (0 == rel->expected_delay.rel_value_us)
1113 rel->expected_delay = time;
1116 rel->expected_delay.rel_value_us *= 7;
1117 rel->expected_delay.rel_value_us += time.rel_value_us;
1118 rel->expected_delay.rel_value_us /= 8;
1120 LOG (GNUNET_ERROR_TYPE_INFO, "TIME message %12s\n",
1121 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
1122 LOG (GNUNET_ERROR_TYPE_INFO, "TIME new delay %12s\n",
1123 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
1125 rel->retry_timer = rel->expected_delay;
1129 LOG (GNUNET_ERROR_TYPE_INFO, "TIME batch free, ignoring timing\n");
1131 rel->ch->pending_messages--;
1132 if (NULL != copy->chq)
1134 GCT_cancel (copy->chq->tq);
1135 /* copy->q is set to NULL by ch_message_sent */
1137 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
1138 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE SEND %p\n", copy);
1141 if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
1143 GCCH_destroy (rel->ch);
1151 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
1153 * @param ch Channel to mark as ready.
1154 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
1157 channel_confirm (struct CadetChannel *ch, int fwd)
1159 struct CadetChannelReliability *rel;
1160 enum CadetChannelState oldstate;
1162 rel = fwd ? ch->root_rel : ch->dest_rel;
1165 GNUNET_break (GNUNET_NO != ch->destroy);
1168 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel confirm %s %s\n",
1169 GC_f2s (fwd), GCCH_2s (ch));
1170 oldstate = ch->state;
1171 ch->state = CADET_CHANNEL_READY;
1173 if (CADET_CHANNEL_READY != oldstate || GNUNET_YES == is_loopback (ch))
1175 rel->client_ready = GNUNET_YES;
1176 rel->expected_delay = rel->retry_timer;
1177 LOG (GNUNET_ERROR_TYPE_DEBUG, " confirm retry timer %s\n",
1178 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer, GNUNET_NO));
1179 if (GCT_get_connections_buffer (ch->t) > 0 || GCT_is_loopback (ch->t))
1180 send_client_ack (ch, fwd);
1182 if (NULL != rel->retry_task)
1184 GNUNET_SCHEDULER_cancel (rel->retry_task);
1185 rel->retry_task = NULL;
1187 else if (NULL != rel->uniq)
1189 GCT_cancel (rel->uniq->tq);
1190 /* ch_message_sent will free and NULL uniq */
1192 else if (GNUNET_NO == is_loopback (ch))
1194 /* We SHOULD have been trying to retransmit this! */
1199 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
1200 if (GNUNET_YES == fwd)
1201 send_ack (ch, GNUNET_NO);
1206 * Save a copy to retransmit in case it gets lost.
1208 * Initializes all needed callbacks and timers.
1210 * @param ch Channel this message goes on.
1211 * @param msg Message to copy.
1212 * @param fwd Is this fwd traffic?
1214 static struct CadetReliableMessage *
1215 channel_save_copy (struct CadetChannel *ch,
1216 const struct GNUNET_MessageHeader *msg,
1219 struct CadetChannelReliability *rel;
1220 struct CadetReliableMessage *copy;
1225 rel = fwd ? ch->root_rel : ch->dest_rel;
1226 mid = rel->mid_send - 1;
1227 type = ntohs (msg->type);
1228 size = ntohs (msg->size);
1230 LOG (GNUNET_ERROR_TYPE_DEBUG, "save MID %u %s\n", mid, GC_m2s (type));
1231 copy = GNUNET_malloc (sizeof (struct CadetReliableMessage) + size);
1232 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", copy);
1236 memcpy (©[1], msg, size);
1237 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
1238 ch->pending_messages++;
1245 * Create a new channel.
1247 * @param t Tunnel this channel is in.
1248 * @param owner Client that owns the channel, NULL for foreign channels.
1249 * @param lid_root Local ID for root client.
1251 * @return A new initialized channel. NULL on error.
1253 static struct CadetChannel *
1254 channel_new (struct CadetTunnel *t,
1255 struct CadetClient *owner,
1256 CADET_ChannelNumber lid_root)
1258 struct CadetChannel *ch;
1260 ch = GNUNET_new (struct CadetChannel);
1262 ch->lid_root = lid_root;
1265 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
1269 ch->gid = GCT_get_next_chid (t);
1270 GML_channel_add (owner, lid_root, ch);
1272 GCT_add_channel (t, ch);
1279 * Handle a loopback message: call the appropriate handler for the message type.
1281 * @param ch Channel this message is on.
1282 * @param msgh Message header.
1283 * @param fwd Is this FWD traffic?
1286 handle_loopback (struct CadetChannel *ch,
1287 const struct GNUNET_MessageHeader *msgh,
1292 type = ntohs (msgh->type);
1293 LOG (GNUNET_ERROR_TYPE_DEBUG,
1294 "Loopback %s %s message!\n",
1295 GC_f2s (fwd), GC_m2s (type));
1299 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1300 /* Don't send hop ACK, wait for client to ACK */
1301 LOG (GNUNET_ERROR_TYPE_DEBUG, "SEND loopback %u (%u)\n",
1302 ntohl (((struct GNUNET_CADET_Data *) msgh)->mid), ntohs (msgh->size));
1303 GCCH_handle_data (ch, (struct GNUNET_CADET_Data *) msgh, fwd);
1306 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
1307 GCCH_handle_data_ack (ch, (struct GNUNET_CADET_DataACK *) msgh, fwd);
1310 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1311 GCCH_handle_create (ch->t,
1312 (struct GNUNET_CADET_ChannelCreate *) msgh);
1315 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
1316 GCCH_handle_ack (ch,
1317 (struct GNUNET_CADET_ChannelManage *) msgh,
1321 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
1322 GCCH_handle_nack (ch);
1325 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1326 GCCH_handle_destroy (ch,
1327 (struct GNUNET_CADET_ChannelManage *) msgh,
1332 GNUNET_break_op (0);
1333 LOG (GNUNET_ERROR_TYPE_DEBUG,
1334 "end-to-end message not known (%u)\n",
1335 ntohs (msgh->type));
1341 /******************************************************************************/
1342 /******************************** API ***********************************/
1343 /******************************************************************************/
1346 * Destroy a channel and free all resources.
1348 * @param ch Channel to destroy.
1351 GCCH_destroy (struct CadetChannel *ch)
1353 struct CadetClient *c;
1354 struct CadetTunnel *t;
1358 if (2 == ch->destroy)
1359 return; /* recursive call */
1362 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
1363 GCT_2s (ch->t), ch->gid);
1369 GML_channel_remove (c, ch->lid_root, ch);
1375 GML_channel_remove (c, ch->lid_dest, ch);
1378 channel_rel_free_all (ch->root_rel);
1379 channel_rel_free_all (ch->dest_rel);
1382 GCT_remove_channel (t, ch);
1383 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
1386 GCT_destroy_if_empty (t);
1391 * Get the channel's public ID.
1393 * @param ch Channel.
1395 * @return ID used to identify the channel with the remote peer.
1398 GCCH_get_id (const struct CadetChannel *ch)
1405 * Get the channel tunnel.
1407 * @param ch Channel to get the tunnel from.
1409 * @return tunnel of the channel.
1411 struct CadetTunnel *
1412 GCCH_get_tunnel (const struct CadetChannel *ch)
1419 * Get free buffer space towards the client on a specific channel.
1421 * @param ch Channel.
1422 * @param fwd Is query about FWD traffic?
1424 * @return Free buffer space [0 - 64]
1427 GCCH_get_buffer (struct CadetChannel *ch, int fwd)
1429 struct CadetChannelReliability *rel;
1431 rel = fwd ? ch->dest_rel : ch->root_rel;
1432 LOG (GNUNET_ERROR_TYPE_DEBUG, " get buffer, channel %s\n", GCCH_2s (ch));
1434 /* If rel is NULL it means that the end is not yet created,
1435 * most probably is a loopback channel at the point of sending
1436 * the ChannelCreate to itself.
1440 LOG (GNUNET_ERROR_TYPE_DEBUG, " rel is NULL: max\n");
1444 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_recv %d\n", rel->n_recv);
1445 return (64 - rel->n_recv);
1450 * Get flow control status of end point: is client allow to send?
1452 * @param ch Channel.
1453 * @param fwd Is query about FWD traffic? (Request root status).
1455 * @return #GNUNET_YES if client is allowed to send us data.
1458 GCCH_get_allowed (struct CadetChannel *ch, int fwd)
1460 struct CadetChannelReliability *rel;
1462 rel = fwd ? ch->root_rel : ch->dest_rel;
1466 /* Probably shutting down: root/dest NULL'ed to mark disconnection */
1467 GNUNET_break (GNUNET_NO != ch->destroy);
1471 return rel->client_allowed;
1476 * Is the root client for this channel on this peer?
1478 * @param ch Channel.
1479 * @param fwd Is this for fwd traffic?
1481 * @return #GNUNET_YES in case it is.
1484 GCCH_is_origin (struct CadetChannel *ch, int fwd)
1486 struct CadetClient *c;
1488 c = fwd ? ch->root : ch->dest;
1494 * Is the destination client for this channel on this peer?
1496 * @param ch Channel.
1497 * @param fwd Is this for fwd traffic?
1499 * @return #GNUNET_YES in case it is.
1502 GCCH_is_terminal (struct CadetChannel *ch, int fwd)
1504 struct CadetClient *c;
1506 c = fwd ? ch->dest : ch->root;
1512 * Send an end-to-end ACK message for the most recent in-sequence payload.
1514 * If channel is not reliable, do nothing.
1516 * @param ch Channel this is about.
1517 * @param fwd Is for FWD traffic? (ACK dest->owner)
1520 GCCH_send_data_ack (struct CadetChannel *ch, int fwd)
1522 struct GNUNET_CADET_DataACK msg;
1523 struct CadetChannelReliability *rel;
1524 struct CadetReliableMessage *copy;
1529 if (GNUNET_NO == ch->reliable)
1532 rel = fwd ? ch->dest_rel : ch->root_rel;
1533 ack = rel->mid_recv - 1;
1535 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_DATA_ACK);
1536 msg.header.size = htons (sizeof (msg));
1537 msg.chid = htonl (ch->gid);
1538 msg.mid = htonl (ack);
1541 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1543 if (copy->type != GNUNET_MESSAGE_TYPE_CADET_DATA)
1545 LOG (GNUNET_ERROR_TYPE_DEBUG, " Type %s, expected DATA\n",
1546 GC_m2s (copy->type));
1549 GNUNET_assert (GC_is_pid_bigger(copy->mid, ack));
1550 delta = copy->mid - (ack + 1);
1553 mask = 0x1LL << delta;
1554 msg.futures |= mask;
1555 LOG (GNUNET_ERROR_TYPE_DEBUG,
1556 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1557 copy->mid, delta, mask, msg.futures);
1559 LOG (GNUNET_ERROR_TYPE_INFO, "===> DATA_ACK for %u + %llX\n",
1562 GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1563 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1568 * Allow a client to send us more data, in case it was choked.
1570 * @param ch Channel.
1571 * @param fwd Is this about FWD traffic? (Root client).
1574 GCCH_allow_client (struct CadetChannel *ch, int fwd)
1576 struct CadetChannelReliability *rel;
1577 unsigned int buffer;
1579 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1581 if (CADET_CHANNEL_READY != ch->state)
1583 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel not ready yet!\n");
1587 if (GNUNET_YES == ch->reliable)
1589 rel = fwd ? ch->root_rel : ch->dest_rel;
1592 GNUNET_break (GNUNET_NO != ch->destroy);
1595 if (NULL != rel->head_sent)
1597 if (64 <= rel->mid_send - rel->head_sent->mid)
1599 LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
1604 LOG (GNUNET_ERROR_TYPE_DEBUG, " gap ok: %u - %u\n",
1605 rel->head_sent->mid, rel->mid_send);
1606 struct CadetReliableMessage *aux;
1607 for (aux = rel->head_sent; NULL != aux; aux = aux->next)
1609 LOG (GNUNET_ERROR_TYPE_DEBUG, " - sent mid %u\n", aux->mid);
1615 LOG (GNUNET_ERROR_TYPE_DEBUG, " head sent is NULL\n");
1619 if (is_loopback (ch))
1620 buffer = GCCH_get_buffer (ch, fwd);
1622 buffer = GCT_get_connections_buffer (ch->t);
1626 LOG (GNUNET_ERROR_TYPE_DEBUG, " no buffer space.\n");
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space %u, allowing\n", buffer);
1631 send_client_ack (ch, fwd);
1638 * @param ch Channel.
1641 GCCH_debug (struct CadetChannel *ch)
1645 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1648 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1649 GCT_2s (ch->t), ch->gid, ch);
1650 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1651 ch->root, ch->root_rel);
1652 if (NULL != ch->root)
1654 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1655 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1656 ch->root_rel->client_ready ? "YES" : "NO");
1657 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1658 LOG (GNUNET_ERROR_TYPE_DEBUG, " recv %d\n", ch->root_rel->n_recv);
1659 LOG (GNUNET_ERROR_TYPE_DEBUG, " MID r: %d, s: %d\n",
1660 ch->root_rel->mid_recv, ch->root_rel->mid_send);
1662 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1663 ch->dest, ch->dest_rel);
1664 if (NULL != ch->dest)
1666 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1667 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1668 ch->dest_rel->client_ready ? "YES" : "NO");
1669 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1670 LOG (GNUNET_ERROR_TYPE_DEBUG, " recv %d\n", ch->dest_rel->n_recv);
1671 LOG (GNUNET_ERROR_TYPE_DEBUG, " MID r: %d, s: %d\n",
1672 ch->dest_rel->mid_recv, ch->dest_rel->mid_send);
1679 * Handle an ACK given by a client.
1681 * Mark client as ready and send him any buffered data we could have for him.
1683 * @param ch Channel.
1684 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1687 GCCH_handle_local_ack (struct CadetChannel *ch, int fwd)
1689 struct CadetChannelReliability *rel;
1690 struct CadetClient *c;
1692 rel = fwd ? ch->dest_rel : ch->root_rel;
1693 c = fwd ? ch->dest : ch->root;
1695 rel->client_ready = GNUNET_YES;
1696 send_client_buffered_data (ch, c, fwd);
1698 if (GNUNET_YES == ch->destroy && 0 == rel->n_recv)
1700 send_destroy (ch, GNUNET_YES);
1704 /* if loopback is marked for destruction, no need to ACK to the other peer,
1705 * it requested the destruction and is already gone, therefore, else if.
1707 else if (is_loopback (ch))
1709 unsigned int buffer;
1711 buffer = GCCH_get_buffer (ch, fwd);
1713 GCCH_allow_client (ch, fwd);
1717 GCT_send_connection_acks (ch->t);
1722 * Handle data given by a client.
1724 * Check whether the client is allowed to send in this tunnel, save if channel
1725 * is reliable and send an ACK to the client if there is still buffer space
1728 * @param ch Channel.
1729 * @param c Client which sent the data.
1730 * @param fwd Is this a FWD data?
1731 * @param message Data message.
1732 * @param size Size of data.
1734 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1737 GCCH_handle_local_data (struct CadetChannel *ch,
1738 struct CadetClient *c, int fwd,
1739 const struct GNUNET_MessageHeader *message,
1742 struct CadetChannelReliability *rel;
1743 struct GNUNET_CADET_Data *payload;
1744 uint16_t p2p_size = sizeof(struct GNUNET_CADET_Data) + size;
1745 unsigned char cbuf[p2p_size];
1746 unsigned char buffer;
1748 /* Is the client in the channel? */
1755 GNUNET_break_op (0);
1756 return GNUNET_SYSERR;
1759 rel = fwd ? ch->root_rel : ch->dest_rel;
1761 if (GNUNET_NO == rel->client_allowed)
1763 GNUNET_break_op (0);
1764 return GNUNET_SYSERR;
1767 rel->client_allowed = GNUNET_NO;
1769 /* Ok, everything is correct, send the message. */
1770 payload = (struct GNUNET_CADET_Data *) cbuf;
1771 payload->mid = htonl (rel->mid_send);
1773 memcpy (&payload[1], message, size);
1774 payload->header.size = htons (p2p_size);
1775 payload->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_DATA);
1776 payload->chid = htonl (ch->gid);
1777 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1778 GCCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1780 if (is_loopback (ch))
1781 buffer = GCCH_get_buffer (ch, fwd);
1783 buffer = GCT_get_connections_buffer (ch->t);
1786 GCCH_allow_client (ch, fwd);
1793 * Handle a channel destroy requested by a client.
1795 * TODO: add "reason" field
1797 * Destroy the channel and the tunnel in case this was the last channel.
1799 * @param ch Channel.
1800 * @param c Client that requested the destruction (to avoid notifying him).
1801 * @param is_root Is the request coming from root?
1804 GCCH_handle_local_destroy (struct CadetChannel *ch,
1805 struct CadetClient *c,
1808 ch->destroy = GNUNET_YES;
1809 /* Cleanup after the tunnel */
1810 if (GNUNET_NO == is_root && c == ch->dest)
1812 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1813 GML_client_delete_channel (c, ch, ch->lid_dest);
1816 if (GNUNET_YES == is_root && c == ch->root)
1818 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1819 GML_client_delete_channel (c, ch, ch->lid_root);
1823 send_destroy (ch, GNUNET_NO);
1824 if (0 == ch->pending_messages)
1830 * Handle a channel create requested by a client.
1832 * Create the channel and the tunnel in case this was the first0 channel.
1834 * @param c Client that requested the creation (will be the root).
1835 * @param msg Create Channel message.
1837 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1840 GCCH_handle_local_create (struct CadetClient *c,
1841 struct GNUNET_CADET_ChannelMessage *msg)
1843 struct CadetChannel *ch;
1844 struct CadetTunnel *t;
1845 struct CadetPeer *peer;
1846 CADET_ChannelNumber chid;
1848 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1849 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1850 chid = ntohl (msg->channel_id);
1852 /* Sanity check for duplicate channel IDs */
1853 if (NULL != GML_channel_get (c, chid))
1856 return GNUNET_SYSERR;
1859 peer = GCP_get (&msg->peer);
1860 GCP_add_tunnel (peer);
1861 t = GCP_get_tunnel (peer);
1863 if (GCP_get_short_id (peer) == myid)
1865 GCT_change_cstate (t, CADET_TUNNEL_READY);
1869 /* FIXME change to a tunnel API, eliminate ch <-> peer connection */
1873 /* Create channel */
1874 ch = channel_new (t, c, chid);
1878 return GNUNET_SYSERR;
1880 ch->port = ntohl (msg->port);
1881 channel_set_options (ch, ntohl (msg->opt));
1883 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1884 ch->root_rel = GNUNET_new (struct CadetChannelReliability);
1885 ch->root_rel->ch = ch;
1886 ch->root_rel->retry_timer = CADET_RETRANSMIT_TIME;
1887 ch->root_rel->expected_delay.rel_value_us = 0;
1889 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GCCH_2s (ch));
1898 * Handler for cadet network payload traffic.
1900 * @param ch Channel for the message.
1901 * @param msg Unencryted data message.
1902 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1903 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1904 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1905 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1908 GCCH_handle_data (struct CadetChannel *ch,
1909 const struct GNUNET_CADET_Data *msg,
1912 struct CadetChannelReliability *rel;
1913 struct CadetClient *c;
1916 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1917 if (GNUNET_SYSERR == fwd)
1919 if (is_loopback (ch))
1921 /* It is a loopback channel after all... */
1925 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1928 /* Initialize FWD/BCK data */
1929 c = fwd ? ch->dest : ch->root;
1930 rel = fwd ? ch->dest_rel : ch->root_rel;
1934 GNUNET_break (GNUNET_NO != ch->destroy);
1938 if (CADET_CHANNEL_READY != ch->state)
1940 if (GNUNET_NO == fwd)
1942 /* If we are the root, this means the other peer has sent traffic before
1943 * receiving our ACK. Even if the SYNACK goes missing, no traffic should
1944 * be sent before the ACK.
1946 GNUNET_break_op (0);
1949 /* If we are the dest, this means that the SYNACK got to the root but
1950 * the ACK went missing. Treat this as an ACK.
1952 channel_confirm (ch, GNUNET_NO);
1955 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1957 mid = ntohl (msg->mid);
1958 LOG (GNUNET_ERROR_TYPE_INFO, "<=== DATA %u %s on channel %s\n",
1959 mid, GC_f2s (fwd), GCCH_2s (ch));
1961 if (GNUNET_NO == ch->reliable ||
1962 ( !GC_is_pid_bigger (rel->mid_recv, mid) &&
1963 GC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1965 LOG (GNUNET_ERROR_TYPE_DEBUG, "RECV MID %u (%u)\n",
1966 mid, ntohs (msg->header.size));
1967 if (GNUNET_YES == ch->reliable)
1969 /* Is this the exact next expected messasge? */
1970 if (mid == rel->mid_recv)
1972 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected, sending to client\n");
1973 send_client_data (ch, msg, fwd);
1977 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1978 add_buffered_data (msg, rel);
1983 /* Tunnel is unreliable: send to clients directly */
1984 /* FIXME: accept Out Of Order traffic */
1985 rel->mid_recv = mid + 1;
1986 send_client_data (ch, msg, fwd);
1991 if (GC_is_pid_bigger (rel->mid_recv, mid))
1993 GNUNET_break_op (0);
1994 LOG (GNUNET_ERROR_TYPE_WARNING,
1995 "MID %u on channel %s not expected (window: %u - %u). Dropping!\n",
1996 mid, GCCH_2s (ch), rel->mid_recv, rel->mid_recv + 63);
2000 LOG (GNUNET_ERROR_TYPE_WARNING,
2001 "Duplicate MID %u, channel %s (expecting MID %u). Re-sending ACK!\n",
2002 mid, GCCH_2s (ch), rel->mid_recv);
2003 if (NULL != rel->uniq)
2005 LOG (GNUNET_ERROR_TYPE_WARNING,
2006 "We are trying to send an ACK, but don't seem have the "
2007 "bandwidth. Try to increase your ats QUOTA in you config file\n");
2013 GCCH_send_data_ack (ch, fwd);
2018 * Handler for cadet network traffic end-to-end ACKs.
2020 * @param ch Channel on which we got this message.
2021 * @param msg Data message.
2022 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2023 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2024 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2025 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2028 GCCH_handle_data_ack (struct CadetChannel *ch,
2029 const struct GNUNET_CADET_DataACK *msg,
2032 struct CadetChannelReliability *rel;
2033 struct CadetReliableMessage *copy;
2034 struct CadetReliableMessage *next;
2038 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2039 if (GNUNET_SYSERR == fwd)
2041 if (is_loopback (ch))
2043 /* It is a loopback channel after all... */
2047 /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
2048 fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
2051 ack = ntohl (msg->mid);
2052 LOG (GNUNET_ERROR_TYPE_INFO, "<=== %s ACK %u + %llX\n",
2053 GC_f2s (fwd), ack, msg->futures);
2055 if (GNUNET_YES == fwd)
2065 GNUNET_break_op (GNUNET_NO != ch->destroy);
2069 /* Free ACK'd copies: no need to retransmit those anymore FIXME refactor */
2070 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
2072 if (GC_is_pid_bigger (copy->mid, ack))
2074 LOG (GNUNET_ERROR_TYPE_DEBUG, " head %u, out!\n", copy->mid);
2075 channel_rel_free_sent (rel, msg);
2079 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %u\n", copy->mid);
2081 if (GNUNET_YES == rel_message_free (copy, GNUNET_YES))
2085 /* ACK client if needed and possible */
2086 GCCH_allow_client (ch, fwd);
2088 /* If some message was free'd, update the retransmission delay */
2089 if (GNUNET_YES == work)
2091 if (NULL != rel->retry_task)
2093 GNUNET_SCHEDULER_cancel (rel->retry_task);
2094 rel->retry_task = NULL;
2095 if (NULL != rel->head_sent && NULL == rel->head_sent->chq)
2097 struct GNUNET_TIME_Absolute new_target;
2098 struct GNUNET_TIME_Relative delay;
2100 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
2101 CADET_RETRANSMIT_MARGIN);
2102 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
2104 delay = GNUNET_TIME_absolute_get_remaining (new_target);
2106 GNUNET_SCHEDULER_add_delayed (delay,
2107 &channel_retransmit_message,
2113 /* Work was done but no task was pending? Shouldn't happen! */
2121 * Handler for channel create messages.
2123 * Does not have fwd parameter because it's always 'FWD': channel is incoming.
2125 * @param t Tunnel this channel will be in.
2126 * @param msg Channel crate message.
2128 struct CadetChannel *
2129 GCCH_handle_create (struct CadetTunnel *t,
2130 const struct GNUNET_CADET_ChannelCreate *msg)
2132 CADET_ChannelNumber chid;
2133 struct CadetChannel *ch;
2134 struct CadetClient *c;
2137 chid = ntohl (msg->chid);
2138 ch = GCT_get_channel (t, chid);
2141 /* Create channel */
2142 ch = channel_new (t, NULL, 0);
2144 channel_set_options (ch, ntohl (msg->opt));
2145 new_channel = GNUNET_YES;
2149 new_channel = GNUNET_NO;
2152 if (GNUNET_YES == new_channel || GCT_is_loopback (t))
2154 /* Find a destination client */
2155 ch->port = ntohl (msg->port);
2156 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", ch->port);
2157 c = GML_client_get_by_port (ch->port);
2160 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
2161 if (is_loopback (ch))
2163 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback: destroy on handler\n");
2168 LOG (GNUNET_ERROR_TYPE_DEBUG, " not loopback: destroy now\n");
2176 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
2179 add_destination (ch, c);
2180 if (GNUNET_YES == ch->reliable)
2181 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reliable\n");
2183 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not Reliable\n");
2185 send_client_create (ch);
2186 ch->state = CADET_CHANNEL_SENT;
2190 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate create channel\n");
2191 if (NULL != ch->dest_rel->retry_task)
2193 LOG (GNUNET_ERROR_TYPE_DEBUG, " clearing retry task\n");
2194 /* we were waiting to re-send our 'SYNACK', wait no more! */
2195 GNUNET_SCHEDULER_cancel (ch->dest_rel->retry_task);
2196 ch->dest_rel->retry_task = NULL;
2198 else if (NULL != ch->dest_rel->uniq)
2200 /* we are waiting to for our 'SYNACK' to leave the queue, all done! */
2204 send_ack (ch, GNUNET_YES);
2211 * Handler for channel NACK messages.
2213 * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
2215 * @param ch Channel.
2218 GCCH_handle_nack (struct CadetChannel *ch)
2220 send_client_nack (ch);
2226 * Handler for channel ack messages.
2228 * @param ch Channel.
2229 * @param msg Message.
2230 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2231 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2232 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2233 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2236 GCCH_handle_ack (struct CadetChannel *ch,
2237 const struct GNUNET_CADET_ChannelManage *msg,
2240 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2241 if (GNUNET_SYSERR == fwd)
2243 if (is_loopback (ch))
2245 /* It is a loopback channel after all... */
2249 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2252 channel_confirm (ch, !fwd);
2257 * Handler for channel destroy messages.
2259 * @param ch Channel to be destroyed of.
2260 * @param msg Message.
2261 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2262 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2263 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2264 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2267 GCCH_handle_destroy (struct CadetChannel *ch,
2268 const struct GNUNET_CADET_ChannelManage *msg,
2271 struct CadetChannelReliability *rel;
2273 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2274 if (GNUNET_SYSERR == fwd)
2276 if (is_loopback (ch))
2278 /* It is a loopback channel after all... */
2282 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2286 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
2288 /* Not for us (don't destroy twice a half-open loopback channel) */
2292 rel = fwd ? ch->dest_rel : ch->root_rel;
2293 if (0 == rel->n_recv)
2295 send_destroy (ch, GNUNET_YES);
2300 ch->destroy = GNUNET_YES;
2306 * Sends an already built message on a channel.
2308 * If the channel is on a loopback tunnel, notifies the appropriate destination
2311 * On a normal channel passes the message to the tunnel for encryption and
2312 * sending on a connection.
2314 * This function DOES NOT save the message for retransmission.
2316 * @param message Message to send. Function makes a copy of it.
2317 * @param ch Channel on which this message is transmitted.
2318 * @param fwd Is this a fwd message?
2319 * @param existing_copy This is a retransmission, don't save a copy.
2322 GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2323 struct CadetChannel *ch, int fwd,
2324 void *existing_copy)
2326 struct CadetChannelQueue *chq;
2329 type = ntohs (message->type);
2330 LOG (GNUNET_ERROR_TYPE_INFO, "===> %s %s on channel %s\n",
2331 GC_m2s (type), GC_f2s (fwd), GCCH_2s (ch));
2333 if (GCT_is_loopback (ch->t))
2335 handle_loopback (ch, message, fwd);
2341 struct GNUNET_CADET_Data *payload;
2342 case GNUNET_MESSAGE_TYPE_CADET_DATA:
2344 payload = (struct GNUNET_CADET_Data *) message;
2345 LOG (GNUNET_ERROR_TYPE_INFO, "===> %s %u\n",
2346 GC_m2s (type), ntohl (payload->mid));
2347 if (GNUNET_YES == ch->reliable)
2349 chq = GNUNET_new (struct CadetChannelQueue);
2351 if (NULL == existing_copy)
2352 chq->copy = channel_save_copy (ch, message, fwd);
2355 chq->copy = (struct CadetReliableMessage *) existing_copy;
2356 if (NULL != chq->copy->chq)
2358 /* Last retransmission was queued but not yet sent!
2359 * This retransmission was scheduled by a ch_message_sent which
2360 * followed a very fast RTT, so the tiny delay made the
2361 * retransmission function to execute before the previous
2362 * retransmitted message even had a chance to leave the peer.
2363 * Cancel this message and wait until the pending
2364 * retransmission leaves the peer and ch_message_sent starts
2365 * the timer for the next one.
2368 LOG (GNUNET_ERROR_TYPE_DEBUG,
2369 " exisitng copy not yet transmitted!\n");
2372 LOG (GNUNET_ERROR_TYPE_DEBUG,
2373 " using existing copy: %p {r:%p q:%p t:%u}\n",
2375 chq->copy->rel, chq->copy->chq, chq->copy->type);
2377 LOG (GNUNET_ERROR_TYPE_DEBUG, " new chq: %p\n", chq);
2378 chq->copy->chq = chq;
2379 chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL,
2380 NULL != existing_copy,
2381 &ch_message_sent, chq);
2382 /* q itself is stored in copy */
2383 GNUNET_assert (NULL != chq->tq || GNUNET_NO != ch->destroy);
2387 fire_and_forget (message, ch, GNUNET_NO);
2392 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
2393 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
2394 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
2395 chq = GNUNET_new (struct CadetChannelQueue);
2397 chq->rel = fwd ? ch->root_rel : ch->dest_rel;
2398 if (NULL != chq->rel->uniq)
2400 if (NULL != chq->rel->uniq->tq)
2402 GCT_cancel (chq->rel->uniq->tq);
2403 /* ch_message_sent is called, freeing and NULLing uniq */
2404 GNUNET_break (NULL == chq->rel->uniq);
2409 GNUNET_free (chq->rel->uniq);
2413 chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL, GNUNET_YES,
2414 &ch_message_sent, chq);
2415 if (NULL == chq->tq)
2418 GCT_debug (ch->t, GNUNET_ERROR_TYPE_ERROR);
2423 chq->rel->uniq = chq;
2427 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2428 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
2429 fire_and_forget (message, ch, GNUNET_YES);
2435 LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s unknown!\n", GC_m2s (type));
2436 fire_and_forget (message, ch, GNUNET_YES);
2442 * Get the static string for identification of the channel.
2444 * @param ch Channel.
2446 * @return Static string with the channel IDs.
2449 GCCH_2s (const struct CadetChannel *ch)
2451 static char buf[64];
2454 return "(NULL Channel)";
2456 SPRINTF (buf, "%s:%u gid:%X (%X / %X)",
2457 GCT_2s (ch->t), ch->port, ch->gid, ch->lid_root, ch->lid_dest);