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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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__)
36 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-chn",__VA_ARGS__)
38 #define CADET_RETRANSMIT_TIME GNUNET_TIME_relative_multiply(\
39 GNUNET_TIME_UNIT_MILLISECONDS, 250)
40 #define CADET_RETRANSMIT_MARGIN 4
44 * All the states a connection can be in.
46 enum CadetChannelState
49 * Uninitialized status, should never appear in operation.
54 * Connection create message sent, waiting for ACK.
59 * Connection confirmed, ready to carry traffic.
66 * Info holder for channel messages in queues.
68 struct CadetChannelQueue
73 struct CadetTunnelQueue *tq;
76 * Message type (DATA/DATA_ACK)
81 * Message copy (for DATAs, to start retransmission timer)
83 struct CadetReliableMessage *copy;
86 * Reliability (for DATA_ACKs, to access rel->ack_q)
88 struct CadetChannelReliability *rel;
93 * Info needed to retry a message in case it gets lost.
95 struct CadetReliableMessage
98 * Double linked list, FIFO style
100 struct CadetReliableMessage *next;
101 struct CadetReliableMessage *prev;
104 * Type of message (payload, channel management).
109 * Tunnel Reliability queue this message is in.
111 struct CadetChannelReliability *rel;
114 * ID of the message (ACK needed to free)
121 struct CadetChannelQueue *chq;
124 * When was this message issued (to calculate ACK delay)
126 struct GNUNET_TIME_Absolute timestamp;
128 /* struct GNUNET_CADET_Data with payload */
133 * Info about the traffic state for a client in a channel.
135 struct CadetChannelReliability
138 * Channel this is about.
140 struct CadetChannel *ch;
143 * DLL of messages sent and not yet ACK'd.
145 struct CadetReliableMessage *head_sent;
146 struct CadetReliableMessage *tail_sent;
149 * DLL of messages received out of order.
151 struct CadetReliableMessage *head_recv;
152 struct CadetReliableMessage *tail_recv;
160 * Next MID to use for outgoing traffic.
165 * Next MID expected for incoming traffic.
170 * Handle for queued unique data CREATE, DATA_ACK.
172 struct CadetChannelQueue *uniq;
175 * Can we send data to the client?
180 * Can the client send data to us?
185 * Task to resend/poll in case no ACK is received.
187 struct GNUNET_SCHEDULER_Task * retry_task;
190 * Counter for exponential backoff.
192 struct GNUNET_TIME_Relative retry_timer;
195 * How long does it usually take to get an ACK.
197 struct GNUNET_TIME_Relative expected_delay;
202 * Struct containing all information regarding a channel to a remote client.
207 * Tunnel this channel is in.
209 struct CadetTunnel *t;
212 * Destination port of the channel.
217 * Global channel number ( < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
219 CADET_ChannelNumber gid;
222 * Local tunnel number for root (owner) client.
223 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI or 0 )
225 CADET_ChannelNumber lid_root;
228 * Local tunnel number for local destination clients (incoming number)
229 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV or 0).
231 CADET_ChannelNumber lid_dest;
236 enum CadetChannelState state;
239 * Is the tunnel bufferless (minimum latency)?
244 * Is the tunnel reliable?
249 * Last time the channel was used
251 struct GNUNET_TIME_Absolute timestamp;
254 * Client owner of the tunnel, if any
256 struct CadetClient *root;
259 * Client destination of the tunnel, if any.
261 struct CadetClient *dest;
264 * Flag to signal the destruction of the channel.
265 * If this is set GNUNET_YES the channel will be destroyed
266 * when the queue is empty.
271 * Total (reliable) messages pending ACK for this channel.
273 unsigned int pending_messages;
277 * Only present (non-NULL) at the owner of a tunnel.
279 struct CadetChannelReliability *root_rel;
283 * Only present (non-NULL) at the destination of a tunnel.
285 struct CadetChannelReliability *dest_rel;
290 /******************************************************************************/
291 /******************************* GLOBALS ***********************************/
292 /******************************************************************************/
295 * Global handle to the statistics service.
297 extern struct GNUNET_STATISTICS_Handle *stats;
300 * Local peer own ID (memory efficient handle).
302 extern GNUNET_PEER_Id myid;
305 /******************************************************************************/
306 /******************************** STATIC ***********************************/
307 /******************************************************************************/
311 * Destroy a reliable message after it has been acknowledged, either by
312 * direct mid ACK or bitfield. Updates the appropriate data structures and
313 * timers and frees all memory.
315 * @param copy Message that is no longer needed: remote peer got it.
316 * @param update_time Is the timing information relevant?
317 * If this message is ACK in a batch the timing information
318 * is skewed by the retransmission, count only for the
319 * retransmitted message.
321 * @return #GNUNET_YES if channel was destroyed as a result of the call,
322 * #GNUNET_NO otherwise.
325 rel_message_free (struct CadetReliableMessage *copy, int update_time);
328 * send a channel create message.
330 * @param ch Channel for which to send.
333 send_create (struct CadetChannel *ch);
336 * Confirm we got a channel create, FWD ack.
338 * @param ch The channel to confirm.
339 * @param fwd Should we send a FWD ACK? (going dest->root)
342 send_ack (struct CadetChannel *ch, int fwd);
347 * Test if the channel is loopback: both root and dest are on the local peer.
349 * @param ch Channel to test.
351 * @return #GNUNET_YES if channel is loopback, #GNUNET_NO otherwise.
354 is_loopback (const struct CadetChannel *ch)
357 return GCT_is_loopback (ch->t);
359 return (NULL != ch->root && NULL != ch->dest);
364 * Save a copy of the data message for later retransmission.
366 * @param msg Message to copy.
367 * @param mid Message ID.
368 * @param rel Reliability data for retransmission.
370 static struct CadetReliableMessage *
371 copy_message (const struct GNUNET_CADET_Data *msg, uint32_t mid,
372 struct CadetChannelReliability *rel)
374 struct CadetReliableMessage *copy;
377 size = ntohs (msg->header.size);
378 copy = GNUNET_malloc (sizeof (*copy) + size);
381 copy->type = GNUNET_MESSAGE_TYPE_CADET_DATA;
382 memcpy (©[1], msg, size);
388 * We have received a message out of order, or the client is not ready.
389 * Buffer it until we receive an ACK from the client or the missing
390 * message from the channel.
392 * @param msg Message to buffer (MUST be of type CADET_DATA).
393 * @param rel Reliability data to the corresponding direction.
396 add_buffered_data (const struct GNUNET_CADET_Data *msg,
397 struct CadetChannelReliability *rel)
399 struct CadetReliableMessage *copy;
400 struct CadetReliableMessage *prev;
403 mid = ntohl (msg->mid);
405 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data MID %u (%u)\n",
410 // FIXME do something better than O(n), although n < 64...
411 // FIXME start from the end (most messages are the latest ones)
412 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
414 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
415 if (prev->mid == mid)
417 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there!\n");
421 else if (GC_is_pid_bigger (prev->mid, mid))
423 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
424 copy = copy_message (msg, mid, rel);
425 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
430 copy = copy_message (msg, mid, rel);
431 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail! (now: %u)\n", rel->n_recv);
432 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
433 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
438 * Add a destination client to a channel, initializing all data structures
439 * in the channel and the client.
441 * @param ch Channel to which add the destination.
442 * @param c Client which to add to the channel.
445 add_destination (struct CadetChannel *ch, struct CadetClient *c)
447 if (NULL != ch->dest)
453 /* Assign local id as destination */
454 ch->lid_dest = GML_get_next_chid (c);
456 /* Store in client's hashmap */
457 GML_channel_add (c, ch->lid_dest, ch);
459 GNUNET_break (NULL == ch->dest_rel);
460 ch->dest_rel = GNUNET_new (struct CadetChannelReliability);
461 ch->dest_rel->ch = ch;
462 ch->dest_rel->expected_delay.rel_value_us = 0;
463 ch->dest_rel->retry_timer = CADET_RETRANSMIT_TIME;
470 * Set options in a channel, extracted from a bit flag field.
472 * @param ch Channel to set options to.
473 * @param options Bit array in host byte order.
476 channel_set_options (struct CadetChannel *ch, uint32_t options)
478 ch->nobuffer = (options & GNUNET_CADET_OPTION_NOBUFFER) != 0 ?
479 GNUNET_YES : GNUNET_NO;
480 ch->reliable = (options & GNUNET_CADET_OPTION_RELIABLE) != 0 ?
481 GNUNET_YES : GNUNET_NO;
486 * Get a bit flag field with the options of a channel.
488 * @param ch Channel to get options from.
490 * @return Bit array in host byte order.
493 channel_get_options (struct CadetChannel *ch)
499 options |= GNUNET_CADET_OPTION_NOBUFFER;
501 options |= GNUNET_CADET_OPTION_RELIABLE;
508 * Notify a client that the channel is no longer valid.
510 * @param ch Channel that is destroyed.
511 * @param local_only Should we avoid sending it to other peers?
514 send_destroy (struct CadetChannel *ch, int local_only)
516 struct GNUNET_CADET_ChannelManage msg;
518 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
519 msg.header.size = htons (sizeof (msg));
520 msg.chid = htonl (ch->gid);
522 /* If root is not NULL, notify.
523 * If it's NULL, check lid_root. When a local destroy comes in, root
524 * is set to NULL but lid_root is left untouched. In this case, do nothing,
525 * the client is the one who requested the channel to be destroyed.
527 if (NULL != ch->root)
528 GML_send_channel_destroy (ch->root, ch->lid_root);
529 else if (0 == ch->lid_root && GNUNET_NO == local_only)
530 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
532 if (NULL != ch->dest)
533 GML_send_channel_destroy (ch->dest, ch->lid_dest);
534 else if (0 == ch->lid_dest && GNUNET_NO == local_only)
535 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
540 * Notify the destination client that a new incoming channel was created.
542 * @param ch Channel that was created.
545 send_client_create (struct CadetChannel *ch)
549 if (NULL == ch->dest)
553 opt |= GNUNET_YES == ch->reliable ? GNUNET_CADET_OPTION_RELIABLE : 0;
554 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_CADET_OPTION_NOBUFFER : 0;
555 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
556 GCT_get_destination (ch->t));
562 * Send data to a client.
564 * If the client is ready, send directly, otherwise buffer while listening
568 * @param msg Message.
569 * @param fwd Is this a fwd (root->dest) message?
572 send_client_data (struct CadetChannel *ch,
573 const struct GNUNET_CADET_Data *msg,
578 if (ch->dest_rel->client_ready)
580 GML_send_data (ch->dest, msg, ch->lid_dest);
581 ch->dest_rel->client_ready = GNUNET_NO;
582 ch->dest_rel->mid_recv++;
585 add_buffered_data (msg, ch->dest_rel);
589 if (ch->root_rel->client_ready)
591 GML_send_data (ch->root, msg, ch->lid_root);
592 ch->root_rel->client_ready = GNUNET_NO;
593 ch->root_rel->mid_recv++;
596 add_buffered_data (msg, ch->root_rel);
602 * Send a buffered message to the client, for in order delivery or
603 * as result of client ACK.
605 * @param ch Channel on which to empty the message buffer.
606 * @param c Client to send to.
607 * @param fwd Is this to send FWD data?.
610 send_client_buffered_data (struct CadetChannel *ch,
611 struct CadetClient *c,
614 struct CadetReliableMessage *copy;
615 struct CadetChannelReliability *rel;
617 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
618 rel = fwd ? ch->dest_rel : ch->root_rel;
619 if (GNUNET_NO == rel->client_ready)
621 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
625 copy = rel->head_recv;
626 /* We never buffer channel management messages */
629 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
631 struct GNUNET_CADET_Data *msg = (struct GNUNET_CADET_Data *) ©[1];
633 LOG (GNUNET_ERROR_TYPE_DEBUG, " have %u! now expecting %u\n",
634 copy->mid, rel->mid_recv + 1);
635 send_client_data (ch, msg, fwd);
637 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
638 LOG (GNUNET_ERROR_TYPE_DEBUG, " free copy recv MID %u (%p), %u left\n",
639 copy->mid, copy, rel->n_recv);
641 GCCH_send_data_ack (ch, fwd);
645 LOG (GNUNET_ERROR_TYPE_DEBUG, " reliable && don't have %u, next is %u\n",
646 rel->mid_recv, copy->mid);
647 if (GNUNET_YES == ch->destroy)
649 /* We don't have the next data piece and the remote peer has closed the
650 * channel. We won't receive it anymore, so just destroy the channel.
651 * FIXME: wait some time to allow other connections to
652 * deliver missing messages
654 send_destroy (ch, GNUNET_YES);
659 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
664 * Allow a client to send more data.
666 * In case the client was already allowed to send data, do nothing.
669 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
672 send_client_ack (struct CadetChannel *ch, int fwd)
674 struct CadetChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
675 struct CadetClient *c = fwd ? ch->root : ch->dest;
679 GNUNET_break (GNUNET_NO != ch->destroy);
682 LOG (GNUNET_ERROR_TYPE_DEBUG,
683 " sending %s ack to client on channel %s\n",
684 GC_f2s (fwd), GCCH_2s (ch));
692 if (GNUNET_YES == rel->client_allowed)
694 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
697 rel->client_allowed = GNUNET_YES;
699 GML_send_ack (c, fwd ? ch->lid_root : ch->lid_dest);
704 * Notify the root that the destination rejected the channel.
706 * @param ch Rejected channel.
709 send_client_nack (struct CadetChannel *ch)
711 if (NULL == ch->root)
716 GML_send_channel_nack (ch->root, ch->lid_root);
721 * We haven't received an ACK after a certain time: restransmit the message.
723 * @param cls Closure (CadetChannelReliability with the message to restransmit)
724 * @param tc TaskContext.
727 channel_retransmit_message (void *cls,
728 const struct GNUNET_SCHEDULER_TaskContext *tc)
730 struct CadetChannelReliability *rel = cls;
731 struct CadetReliableMessage *copy;
732 struct CadetChannel *ch;
733 struct GNUNET_CADET_Data *payload;
736 rel->retry_task = NULL;
737 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
741 copy = rel->head_sent;
744 GNUNET_break (0); // FIXME tripped in rps testcase
748 payload = (struct GNUNET_CADET_Data *) ©[1];
749 fwd = (rel == ch->root_rel);
751 /* Message not found in the queue that we are going to use. */
752 LOG (GNUNET_ERROR_TYPE_DEBUG, "RETRANSMIT MID %u\n", copy->mid);
754 GCCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
755 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
760 * We haven't received an Channel ACK after a certain time: resend the CREATE.
762 * @param cls Closure (CadetChannelReliability of the channel to recreate)
763 * @param tc TaskContext.
766 channel_recreate (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
768 struct CadetChannelReliability *rel = cls;
770 rel->retry_task = NULL;
771 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
774 LOG (GNUNET_ERROR_TYPE_DEBUG, "RE-CREATE\n");
775 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
777 if (rel == rel->ch->root_rel)
779 send_create (rel->ch);
781 else if (rel == rel->ch->dest_rel)
783 send_ack (rel->ch, GNUNET_YES);
794 * Message has been sent: start retransmission timer.
796 * @param cls Closure (queue structure).
798 * @param q Queue handler (no longer valid).
799 * @param type Type of message.
800 * @param size Size of the message.
803 ch_message_sent (void *cls,
804 struct CadetTunnel *t,
805 struct CadetTunnelQueue *q,
806 uint16_t type, size_t size)
808 struct CadetChannelQueue *chq = cls;
809 struct CadetReliableMessage *copy = chq->copy;
810 struct CadetChannelReliability *rel;
812 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel_message_sent callback %s\n",
817 case GNUNET_MESSAGE_TYPE_CADET_DATA:
818 LOG (GNUNET_ERROR_TYPE_DEBUG, "data MID %u sent\n", copy->mid);
819 GNUNET_assert (chq == copy->chq);
820 copy->timestamp = GNUNET_TIME_absolute_get ();
822 if (NULL == rel->retry_task)
824 LOG (GNUNET_ERROR_TYPE_DEBUG, " scheduling retry in %d * %s\n",
825 CADET_RETRANSMIT_MARGIN,
826 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
828 if (0 != rel->expected_delay.rel_value_us)
831 GNUNET_TIME_relative_multiply (rel->expected_delay,
832 CADET_RETRANSMIT_MARGIN);
836 rel->retry_timer = CADET_RETRANSMIT_TIME;
838 LOG (GNUNET_ERROR_TYPE_DEBUG, " using delay %s\n",
839 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
842 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
843 &channel_retransmit_message, rel);
847 LOG (GNUNET_ERROR_TYPE_DEBUG, "retry running %p\n", rel->retry_task);
853 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
854 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
855 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
856 LOG (GNUNET_ERROR_TYPE_DEBUG, "sent %s\n", GC_m2s (chq->type));
858 GNUNET_assert (rel->uniq == chq);
861 if (CADET_CHANNEL_READY != rel->ch->state
862 && GNUNET_MESSAGE_TYPE_CADET_DATA_ACK != type
863 && GNUNET_NO == rel->ch->destroy)
865 GNUNET_assert (NULL == rel->retry_task);
866 LOG (GNUNET_ERROR_TYPE_DEBUG, "STD BACKOFF %s\n",
867 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
869 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
870 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
871 &channel_recreate, rel);
884 * send a channel create message.
886 * @param ch Channel for which to send.
889 send_create (struct CadetChannel *ch)
891 struct GNUNET_CADET_ChannelCreate msgcc;
893 msgcc.header.size = htons (sizeof (msgcc));
894 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
895 msgcc.chid = htonl (ch->gid);
896 msgcc.port = htonl (ch->port);
897 msgcc.opt = htonl (channel_get_options (ch));
899 GCCH_send_prebuilt_message (&msgcc.header, ch, GNUNET_YES, NULL);
904 * Confirm we got a channel create or FWD ack.
906 * @param ch The channel to confirm.
907 * @param fwd Should we send a FWD ACK? (going dest->root)
910 send_ack (struct CadetChannel *ch, int fwd)
912 struct GNUNET_CADET_ChannelManage msg;
914 msg.header.size = htons (sizeof (msg));
915 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK);
916 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending channel %s ack for channel %s\n",
917 GC_f2s (fwd), GCCH_2s (ch));
919 msg.chid = htonl (ch->gid);
920 GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
925 * Send a message and don't keep any info about it: we won't need to cancel it
928 * @param msg Header of the message to fire away.
929 * @param ch Channel on which the message should go.
930 * @param force Is this a forced (undroppable) message?
933 fire_and_forget (const struct GNUNET_MessageHeader *msg,
934 struct CadetChannel *ch,
937 GNUNET_break (NULL == GCT_send_prebuilt_message (msg, ch->t, NULL,
943 * Notify that a channel create didn't succeed.
945 * @param ch The channel to reject.
948 send_nack (struct CadetChannel *ch)
950 struct GNUNET_CADET_ChannelManage msg;
952 msg.header.size = htons (sizeof (msg));
953 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
954 LOG (GNUNET_ERROR_TYPE_DEBUG,
955 " sending channel NACK for channel %s\n",
958 msg.chid = htonl (ch->gid);
959 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
964 * Destroy all reliable messages queued for a channel,
965 * during a channel destruction.
966 * Frees the reliability structure itself.
968 * @param rel Reliability data for a channel.
971 channel_rel_free_all (struct CadetChannelReliability *rel)
973 struct CadetReliableMessage *copy;
974 struct CadetReliableMessage *next;
979 for (copy = rel->head_recv; NULL != copy; copy = next)
982 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
983 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE ALL RECV %p\n", copy);
984 GNUNET_break (NULL == copy->chq);
987 for (copy = rel->head_sent; NULL != copy; copy = next)
990 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
991 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE ALL SEND %p\n", copy);
992 if (NULL != copy->chq)
994 if (NULL != copy->chq->tq)
996 GCT_cancel (copy->chq->tq);
997 /* ch_message_sent will free copy->q */
1001 GNUNET_free (copy->chq);
1007 if (NULL != rel->uniq && NULL != rel->uniq->tq)
1009 GCT_cancel (rel->uniq->tq);
1010 /* ch_message_sent is called freeing uniq */
1012 if (NULL != rel->retry_task)
1014 GNUNET_SCHEDULER_cancel (rel->retry_task);
1015 rel->retry_task = NULL;
1022 * Mark future messages as ACK'd.
1024 * @param rel Reliability data.
1025 * @param msg DataACK message with a bitfield of future ACK'd messages.
1027 * @return How many messages have been freed.
1030 channel_rel_free_sent (struct CadetChannelReliability *rel,
1031 const struct GNUNET_CADET_DataACK *msg)
1033 struct CadetReliableMessage *copy;
1034 struct CadetReliableMessage *next;
1042 bitfield = msg->futures;
1043 mid = ntohl (msg->mid);
1044 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable %u %llX\n", mid, bitfield);
1045 LOG (GNUNET_ERROR_TYPE_DEBUG, " rel %p, head %p\n", rel, rel->head_sent);
1046 for (i = 0, r = 0, copy = rel->head_sent;
1047 i < 64 && NULL != copy && 0 != bitfield;
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, " trying bit %u (mid %u)\n", i, mid + i + 1);
1052 if (0 == (bitfield & mask))
1055 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
1056 /* Bit was set, clear the bit from the bitfield */
1059 /* The i-th bit was set. Do we have that copy? */
1060 /* Skip copies with mid < target */
1061 target = mid + i + 1;
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
1063 while (NULL != copy && GC_is_pid_bigger (target, copy->mid))
1066 /* Did we run out of copies? (previously freed, it's ok) */
1069 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
1073 /* Did we overshoot the target? (previously freed, it's ok) */
1074 if (GC_is_pid_bigger (copy->mid, target))
1076 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
1077 i += copy->mid - target - 1; /* MID: 90, t = 85, i += 4 (i++ later) */
1078 mask = (0x1LL << (i + 1)) - 1; /* Mask = i-th bit and all before */
1079 bitfield &= ~mask; /* Clear all bits up to MID - 1 */
1083 /* Now copy->mid == target, free it */
1085 GNUNET_break (GNUNET_YES != rel_message_free (copy, GNUNET_YES));
1089 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
1095 * Destroy a reliable message after it has been acknowledged, either by
1096 * direct mid ACK or bitfield. Updates the appropriate data structures and
1097 * timers and frees all memory.
1099 * @param copy Message that is no longer needed: remote peer got it.
1100 * @param update_time Is the timing information relevant?
1101 * If this message is ACK in a batch the timing information
1102 * is skewed by the retransmission, count only for the
1103 * retransmitted message.
1105 * @return #GNUNET_YES if channel was destroyed as a result of the call,
1106 * #GNUNET_NO otherwise.
1109 rel_message_free (struct CadetReliableMessage *copy, int update_time)
1111 struct CadetChannelReliability *rel;
1112 struct GNUNET_TIME_Relative time;
1115 LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing %u\n", copy->mid);
1116 if (GNUNET_YES == update_time)
1118 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
1119 if (0 == rel->expected_delay.rel_value_us)
1120 rel->expected_delay = time;
1123 rel->expected_delay.rel_value_us *= 7;
1124 rel->expected_delay.rel_value_us += time.rel_value_us;
1125 rel->expected_delay.rel_value_us /= 8;
1127 LOG (GNUNET_ERROR_TYPE_DEBUG, " message time %12s\n",
1128 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
1129 LOG (GNUNET_ERROR_TYPE_DEBUG, " new delay %12s\n",
1130 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
1132 rel->retry_timer = rel->expected_delay;
1136 LOG (GNUNET_ERROR_TYPE_DEBUG, "batch free, ignoring timing\n");
1138 rel->ch->pending_messages--;
1139 if (NULL != copy->chq)
1141 GCT_cancel (copy->chq->tq);
1142 /* copy->q is set to NULL by ch_message_sent */
1144 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, " free send copy MID %u at %p\n",
1149 if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
1151 GCCH_destroy (rel->ch);
1159 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
1161 * @param ch Channel to mark as ready.
1162 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
1165 channel_confirm (struct CadetChannel *ch, int fwd)
1167 struct CadetChannelReliability *rel;
1168 enum CadetChannelState oldstate;
1170 rel = fwd ? ch->root_rel : ch->dest_rel;
1173 GNUNET_break (GNUNET_NO != ch->destroy);
1176 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel confirm %s %s\n",
1177 GC_f2s (fwd), GCCH_2s (ch));
1178 oldstate = ch->state;
1179 ch->state = CADET_CHANNEL_READY;
1181 if (CADET_CHANNEL_READY != oldstate || GNUNET_YES == is_loopback (ch))
1183 rel->client_ready = GNUNET_YES;
1184 rel->expected_delay = rel->retry_timer;
1185 LOG (GNUNET_ERROR_TYPE_DEBUG, " confirm retry timer %s\n",
1186 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer, GNUNET_NO));
1187 if (GCT_get_connections_buffer (ch->t) > 0 || GCT_is_loopback (ch->t))
1188 send_client_ack (ch, fwd);
1190 if (NULL != rel->retry_task)
1192 GNUNET_SCHEDULER_cancel (rel->retry_task);
1193 rel->retry_task = NULL;
1195 else if (NULL != rel->uniq)
1197 GCT_cancel (rel->uniq->tq);
1198 /* ch_message_sent will free and NULL uniq */
1200 else if (GNUNET_NO == is_loopback (ch))
1202 /* We SHOULD have been trying to retransmit this! */
1207 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
1208 if (GNUNET_YES == fwd)
1209 send_ack (ch, GNUNET_NO);
1214 * Save a copy to retransmit in case it gets lost.
1216 * Initializes all needed callbacks and timers.
1218 * @param ch Channel this message goes on.
1219 * @param msg Message to copy.
1220 * @param fwd Is this fwd traffic?
1222 static struct CadetReliableMessage *
1223 channel_save_copy (struct CadetChannel *ch,
1224 const struct GNUNET_MessageHeader *msg,
1227 struct CadetChannelReliability *rel;
1228 struct CadetReliableMessage *copy;
1233 rel = fwd ? ch->root_rel : ch->dest_rel;
1234 mid = rel->mid_send - 1;
1235 type = ntohs (msg->type);
1236 size = ntohs (msg->size);
1238 LOG (GNUNET_ERROR_TYPE_DEBUG, "save MID %u %s\n", mid, GC_m2s (type));
1239 copy = GNUNET_malloc (sizeof (struct CadetReliableMessage) + size);
1240 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", copy);
1244 memcpy (©[1], msg, size);
1245 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
1246 ch->pending_messages++;
1253 * Create a new channel.
1255 * @param t Tunnel this channel is in.
1256 * @param owner Client that owns the channel, NULL for foreign channels.
1257 * @param lid_root Local ID for root client.
1259 * @return A new initialized channel. NULL on error.
1261 static struct CadetChannel *
1262 channel_new (struct CadetTunnel *t,
1263 struct CadetClient *owner,
1264 CADET_ChannelNumber lid_root)
1266 struct CadetChannel *ch;
1268 ch = GNUNET_new (struct CadetChannel);
1270 ch->lid_root = lid_root;
1273 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
1277 ch->gid = GCT_get_next_chid (t);
1278 GML_channel_add (owner, lid_root, ch);
1280 GCT_add_channel (t, ch);
1287 * Handle a loopback message: call the appropriate handler for the message type.
1289 * @param ch Channel this message is on.
1290 * @param msgh Message header.
1291 * @param fwd Is this FWD traffic?
1294 handle_loopback (struct CadetChannel *ch,
1295 const struct GNUNET_MessageHeader *msgh,
1300 type = ntohs (msgh->type);
1301 LOG (GNUNET_ERROR_TYPE_DEBUG,
1302 "Loopback %s %s message!\n",
1303 GC_f2s (fwd), GC_m2s (type));
1307 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1308 /* Don't send hop ACK, wait for client to ACK */
1309 LOG (GNUNET_ERROR_TYPE_DEBUG, "SEND loopback %u (%u)\n",
1310 ntohl (((struct GNUNET_CADET_Data *) msgh)->mid), ntohs (msgh->size));
1311 GCCH_handle_data (ch, (struct GNUNET_CADET_Data *) msgh, fwd);
1314 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
1315 GCCH_handle_data_ack (ch, (struct GNUNET_CADET_DataACK *) msgh, fwd);
1318 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1319 GCCH_handle_create (ch->t,
1320 (struct GNUNET_CADET_ChannelCreate *) msgh);
1323 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
1324 GCCH_handle_ack (ch,
1325 (struct GNUNET_CADET_ChannelManage *) msgh,
1329 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
1330 GCCH_handle_nack (ch);
1333 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1334 GCCH_handle_destroy (ch,
1335 (struct GNUNET_CADET_ChannelManage *) msgh,
1340 GNUNET_break_op (0);
1341 LOG (GNUNET_ERROR_TYPE_DEBUG,
1342 "end-to-end message not known (%u)\n",
1343 ntohs (msgh->type));
1349 /******************************************************************************/
1350 /******************************** API ***********************************/
1351 /******************************************************************************/
1354 * Destroy a channel and free all resources.
1356 * @param ch Channel to destroy.
1359 GCCH_destroy (struct CadetChannel *ch)
1361 struct CadetClient *c;
1362 struct CadetTunnel *t;
1366 if (2 == ch->destroy)
1367 return; /* recursive call */
1370 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
1371 GCT_2s (ch->t), ch->gid);
1372 GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
1377 GML_channel_remove (c, ch->lid_root, ch);
1383 GML_channel_remove (c, ch->lid_dest, ch);
1386 channel_rel_free_all (ch->root_rel);
1387 channel_rel_free_all (ch->dest_rel);
1390 GCT_remove_channel (t, ch);
1391 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
1394 GCT_destroy_if_empty (t);
1399 * Get the channel's public ID.
1401 * @param ch Channel.
1403 * @return ID used to identify the channel with the remote peer.
1406 GCCH_get_id (const struct CadetChannel *ch)
1413 * Get the channel tunnel.
1415 * @param ch Channel to get the tunnel from.
1417 * @return tunnel of the channel.
1419 struct CadetTunnel *
1420 GCCH_get_tunnel (const struct CadetChannel *ch)
1427 * Get free buffer space towards the client on a specific channel.
1429 * @param ch Channel.
1430 * @param fwd Is query about FWD traffic?
1432 * @return Free buffer space [0 - 64]
1435 GCCH_get_buffer (struct CadetChannel *ch, int fwd)
1437 struct CadetChannelReliability *rel;
1439 rel = fwd ? ch->dest_rel : ch->root_rel;
1440 LOG (GNUNET_ERROR_TYPE_DEBUG, " get buffer, channel %s\n", GCCH_2s (ch));
1441 GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
1442 /* If rel is NULL it means that the end is not yet created,
1443 * most probably is a loopback channel at the point of sending
1444 * the ChannelCreate to itself.
1448 LOG (GNUNET_ERROR_TYPE_DEBUG, " rel is NULL: max\n");
1452 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_recv %d\n", rel->n_recv);
1453 return (64 - rel->n_recv);
1458 * Get flow control status of end point: is client allow to send?
1460 * @param ch Channel.
1461 * @param fwd Is query about FWD traffic? (Request root status).
1463 * @return #GNUNET_YES if client is allowed to send us data.
1466 GCCH_get_allowed (struct CadetChannel *ch, int fwd)
1468 struct CadetChannelReliability *rel;
1470 rel = fwd ? ch->root_rel : ch->dest_rel;
1474 /* Probably shutting down: root/dest NULL'ed to mark disconnection */
1475 GNUNET_break (GNUNET_NO != ch->destroy);
1479 return rel->client_allowed;
1484 * Is the root client for this channel on this peer?
1486 * @param ch Channel.
1487 * @param fwd Is this for fwd traffic?
1489 * @return #GNUNET_YES in case it is.
1492 GCCH_is_origin (struct CadetChannel *ch, int fwd)
1494 struct CadetClient *c;
1496 c = fwd ? ch->root : ch->dest;
1502 * Is the destination client for this channel on this peer?
1504 * @param ch Channel.
1505 * @param fwd Is this for fwd traffic?
1507 * @return #GNUNET_YES in case it is.
1510 GCCH_is_terminal (struct CadetChannel *ch, int fwd)
1512 struct CadetClient *c;
1514 c = fwd ? ch->dest : ch->root;
1520 * Send an end-to-end ACK message for the most recent in-sequence payload.
1522 * If channel is not reliable, do nothing.
1524 * @param ch Channel this is about.
1525 * @param fwd Is for FWD traffic? (ACK dest->owner)
1528 GCCH_send_data_ack (struct CadetChannel *ch, int fwd)
1530 struct GNUNET_CADET_DataACK msg;
1531 struct CadetChannelReliability *rel;
1532 struct CadetReliableMessage *copy;
1537 if (GNUNET_NO == ch->reliable)
1540 rel = fwd ? ch->dest_rel : ch->root_rel;
1541 ack = rel->mid_recv - 1;
1543 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_DATA_ACK);
1544 msg.header.size = htons (sizeof (msg));
1545 msg.chid = htonl (ch->gid);
1546 msg.mid = htonl (ack);
1549 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1551 if (copy->type != GNUNET_MESSAGE_TYPE_CADET_DATA)
1553 LOG (GNUNET_ERROR_TYPE_DEBUG, " Type %s, expected DATA\n",
1554 GC_m2s (copy->type));
1557 GNUNET_assert (GC_is_pid_bigger(copy->mid, ack));
1558 delta = copy->mid - (ack + 1);
1561 mask = 0x1LL << delta;
1562 msg.futures |= mask;
1563 LOG (GNUNET_ERROR_TYPE_DEBUG,
1564 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1565 copy->mid, delta, mask, msg.futures);
1567 LOG (GNUNET_ERROR_TYPE_INFO, "===> DATA_ACK for %u + %llX\n",
1570 GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1571 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1576 * Allow a client to send us more data, in case it was choked.
1578 * @param ch Channel.
1579 * @param fwd Is this about FWD traffic? (Root client).
1582 GCCH_allow_client (struct CadetChannel *ch, int fwd)
1584 struct CadetChannelReliability *rel;
1585 unsigned int buffer;
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1589 if (CADET_CHANNEL_READY != ch->state)
1591 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel not ready yet!\n");
1595 if (GNUNET_YES == ch->reliable)
1597 rel = fwd ? ch->root_rel : ch->dest_rel;
1600 GNUNET_break (GNUNET_NO != ch->destroy);
1603 if (NULL != rel->head_sent)
1605 if (64 <= rel->mid_send - rel->head_sent->mid)
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
1612 LOG (GNUNET_ERROR_TYPE_DEBUG, " gap ok: %u - %u\n",
1613 rel->head_sent->mid, rel->mid_send);
1614 struct CadetReliableMessage *aux;
1615 for (aux = rel->head_sent; NULL != aux; aux = aux->next)
1617 LOG (GNUNET_ERROR_TYPE_DEBUG, " - sent mid %u\n", aux->mid);
1623 LOG (GNUNET_ERROR_TYPE_DEBUG, " head sent is NULL\n");
1627 if (is_loopback (ch))
1628 buffer = GCCH_get_buffer (ch, fwd);
1630 buffer = GCT_get_connections_buffer (ch->t);
1634 LOG (GNUNET_ERROR_TYPE_DEBUG, " no buffer space.\n");
1638 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space %u, allowing\n", buffer);
1639 send_client_ack (ch, fwd);
1646 * @param ch Channel.
1647 * @param level Debug level to use.
1650 GCCH_debug (struct CadetChannel *ch, enum GNUNET_ErrorType level)
1654 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
1656 __FILE__, __FUNCTION__, __LINE__);
1662 LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
1665 LOG2 (level, "CHN Channel %s:%X (%p)\n", GCT_2s (ch->t), ch->gid, ch);
1666 LOG2 (level, "CHN root %p/%p\n", ch->root, ch->root_rel);
1667 if (NULL != ch->root)
1669 LOG2 (level, "CHN cli %s\n", GML_2s (ch->root));
1670 LOG2 (level, "CHN ready %s\n", ch->root_rel->client_ready ? "YES" : "NO");
1671 LOG2 (level, "CHN id %X\n", ch->lid_root);
1672 LOG2 (level, "CHN recv %d\n", ch->root_rel->n_recv);
1673 LOG2 (level, "CHN MID r: %d, s: %d\n",
1674 ch->root_rel->mid_recv, ch->root_rel->mid_send);
1676 LOG2 (level, "CHN dest %p/%p\n",
1677 ch->dest, ch->dest_rel);
1678 if (NULL != ch->dest)
1680 LOG2 (level, "CHN cli %s\n", GML_2s (ch->dest));
1681 LOG2 (level, "CHN ready %s\n", ch->dest_rel->client_ready ? "YES" : "NO");
1682 LOG2 (level, "CHN id %X\n", ch->lid_dest);
1683 LOG2 (level, "CHN recv %d\n", ch->dest_rel->n_recv);
1684 LOG2 (level, "CHN MID r: %d, s: %d\n",
1685 ch->dest_rel->mid_recv, ch->dest_rel->mid_send);
1692 * Handle an ACK given by a client.
1694 * Mark client as ready and send him any buffered data we could have for him.
1696 * @param ch Channel.
1697 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1700 GCCH_handle_local_ack (struct CadetChannel *ch, int fwd)
1702 struct CadetChannelReliability *rel;
1703 struct CadetClient *c;
1705 rel = fwd ? ch->dest_rel : ch->root_rel;
1706 c = fwd ? ch->dest : ch->root;
1708 rel->client_ready = GNUNET_YES;
1709 send_client_buffered_data (ch, c, fwd);
1711 if (GNUNET_YES == ch->destroy && 0 == rel->n_recv)
1713 send_destroy (ch, GNUNET_YES);
1717 /* if loopback is marked for destruction, no need to ACK to the other peer,
1718 * it requested the destruction and is already gone, therefore, else if.
1720 else if (is_loopback (ch))
1722 unsigned int buffer;
1724 buffer = GCCH_get_buffer (ch, fwd);
1726 GCCH_allow_client (ch, fwd);
1730 GCT_send_connection_acks (ch->t);
1735 * Handle data given by a client.
1737 * Check whether the client is allowed to send in this tunnel, save if channel
1738 * is reliable and send an ACK to the client if there is still buffer space
1741 * @param ch Channel.
1742 * @param c Client which sent the data.
1743 * @param fwd Is this a FWD data?
1744 * @param message Data message.
1745 * @param size Size of data.
1747 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1750 GCCH_handle_local_data (struct CadetChannel *ch,
1751 struct CadetClient *c, int fwd,
1752 const struct GNUNET_MessageHeader *message,
1755 struct CadetChannelReliability *rel;
1756 struct GNUNET_CADET_Data *payload;
1757 uint16_t p2p_size = sizeof(struct GNUNET_CADET_Data) + size;
1758 unsigned char cbuf[p2p_size];
1759 unsigned char buffer;
1761 /* Is the client in the channel? */
1768 GNUNET_break_op (0);
1769 return GNUNET_SYSERR;
1772 rel = fwd ? ch->root_rel : ch->dest_rel;
1774 if (GNUNET_NO == rel->client_allowed)
1776 GNUNET_break_op (0);
1777 return GNUNET_SYSERR;
1780 rel->client_allowed = GNUNET_NO;
1782 /* Ok, everything is correct, send the message. */
1783 payload = (struct GNUNET_CADET_Data *) cbuf;
1784 payload->mid = htonl (rel->mid_send);
1786 memcpy (&payload[1], message, size);
1787 payload->header.size = htons (p2p_size);
1788 payload->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_DATA);
1789 payload->chid = htonl (ch->gid);
1790 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1791 GCCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1793 if (is_loopback (ch))
1794 buffer = GCCH_get_buffer (ch, fwd);
1796 buffer = GCT_get_connections_buffer (ch->t);
1799 GCCH_allow_client (ch, fwd);
1806 * Handle a channel destroy requested by a client.
1808 * TODO: add "reason" field
1810 * Destroy the channel and the tunnel in case this was the last channel.
1812 * @param ch Channel.
1813 * @param c Client that requested the destruction (to avoid notifying him).
1814 * @param is_root Is the request coming from root?
1817 GCCH_handle_local_destroy (struct CadetChannel *ch,
1818 struct CadetClient *c,
1821 ch->destroy = GNUNET_YES;
1822 /* Cleanup after the tunnel */
1823 if (GNUNET_NO == is_root && c == ch->dest)
1825 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1826 GML_client_delete_channel (c, ch, ch->lid_dest);
1829 if (GNUNET_YES == is_root && c == ch->root)
1831 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1832 GML_client_delete_channel (c, ch, ch->lid_root);
1836 send_destroy (ch, GNUNET_NO);
1837 if (0 == ch->pending_messages)
1843 * Handle a channel create requested by a client.
1845 * Create the channel and the tunnel in case this was the first0 channel.
1847 * @param c Client that requested the creation (will be the root).
1848 * @param msg Create Channel message.
1850 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1853 GCCH_handle_local_create (struct CadetClient *c,
1854 struct GNUNET_CADET_ChannelMessage *msg)
1856 struct CadetChannel *ch;
1857 struct CadetTunnel *t;
1858 struct CadetPeer *peer;
1859 CADET_ChannelNumber chid;
1861 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1862 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1863 chid = ntohl (msg->channel_id);
1865 /* Sanity check for duplicate channel IDs */
1866 if (NULL != GML_channel_get (c, chid))
1869 return GNUNET_SYSERR;
1872 peer = GCP_get (&msg->peer, GNUNET_YES);
1873 GCP_add_tunnel (peer);
1874 t = GCP_get_tunnel (peer);
1876 if (GCP_get_short_id (peer) == myid)
1878 GCT_change_cstate (t, CADET_TUNNEL_READY);
1882 /* FIXME change to a tunnel API, eliminate ch <-> peer connection */
1886 /* Create channel */
1887 ch = channel_new (t, c, chid);
1891 return GNUNET_SYSERR;
1893 ch->port = ntohl (msg->port);
1894 channel_set_options (ch, ntohl (msg->opt));
1896 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1897 ch->root_rel = GNUNET_new (struct CadetChannelReliability);
1898 ch->root_rel->ch = ch;
1899 ch->root_rel->retry_timer = CADET_RETRANSMIT_TIME;
1900 ch->root_rel->expected_delay.rel_value_us = 0;
1902 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GCCH_2s (ch));
1911 * Handler for cadet network payload traffic.
1913 * @param ch Channel for the message.
1914 * @param msg Unencryted data message.
1915 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1916 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1917 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1918 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1921 GCCH_handle_data (struct CadetChannel *ch,
1922 const struct GNUNET_CADET_Data *msg,
1925 struct CadetChannelReliability *rel;
1926 struct CadetClient *c;
1929 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1930 if (GNUNET_SYSERR == fwd)
1932 if (is_loopback (ch))
1934 /* It is a loopback channel after all... */
1938 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1941 /* Initialize FWD/BCK data */
1942 c = fwd ? ch->dest : ch->root;
1943 rel = fwd ? ch->dest_rel : ch->root_rel;
1947 GNUNET_break (GNUNET_NO != ch->destroy);
1951 if (CADET_CHANNEL_READY != ch->state)
1953 if (GNUNET_NO == fwd)
1955 /* If we are the root, this means the other peer has sent traffic before
1956 * receiving our ACK. Even if the SYNACK goes missing, no traffic should
1957 * be sent before the ACK.
1959 GNUNET_break_op (0);
1962 /* If we are the dest, this means that the SYNACK got to the root but
1963 * the ACK went missing. Treat this as an ACK.
1965 channel_confirm (ch, GNUNET_NO);
1968 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1970 mid = ntohl (msg->mid);
1971 LOG (GNUNET_ERROR_TYPE_INFO, "<=== DATA %u %s on channel %s\n",
1972 mid, GC_f2s (fwd), GCCH_2s (ch));
1974 if (GNUNET_NO == ch->reliable ||
1975 ( !GC_is_pid_bigger (rel->mid_recv, mid) &&
1976 GC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1978 LOG (GNUNET_ERROR_TYPE_DEBUG, "RECV MID %u (%u)\n",
1979 mid, ntohs (msg->header.size));
1980 if (GNUNET_YES == ch->reliable)
1982 /* Is this the exact next expected messasge? */
1983 if (mid == rel->mid_recv)
1985 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected, sending to client\n");
1986 send_client_data (ch, msg, fwd);
1990 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1991 add_buffered_data (msg, rel);
1996 /* Tunnel is unreliable: send to clients directly */
1997 /* FIXME: accept Out Of Order traffic */
1998 rel->mid_recv = mid + 1;
1999 send_client_data (ch, msg, fwd);
2004 if (GC_is_pid_bigger (rel->mid_recv, mid))
2006 GNUNET_break_op (0);
2007 LOG (GNUNET_ERROR_TYPE_WARNING,
2008 "MID %u on channel %s not expected (window: %u - %u). Dropping!\n",
2009 mid, GCCH_2s (ch), rel->mid_recv, rel->mid_recv + 63);
2013 LOG (GNUNET_ERROR_TYPE_WARNING,
2014 "Duplicate MID %u, channel %s (expecting MID %u). Re-sending ACK!\n",
2015 mid, GCCH_2s (ch), rel->mid_recv);
2016 if (NULL != rel->uniq)
2018 LOG (GNUNET_ERROR_TYPE_WARNING,
2019 "We are trying to send an ACK, but don't seem have the "
2020 "bandwidth. Try to increase your ats QUOTA in you config file\n");
2026 GCCH_send_data_ack (ch, fwd);
2031 * Handler for cadet network traffic end-to-end ACKs.
2033 * @param ch Channel on which we got this message.
2034 * @param msg Data message.
2035 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2036 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2037 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2038 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2041 GCCH_handle_data_ack (struct CadetChannel *ch,
2042 const struct GNUNET_CADET_DataACK *msg,
2045 struct CadetChannelReliability *rel;
2046 struct CadetReliableMessage *copy;
2047 struct CadetReliableMessage *next;
2051 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2052 if (GNUNET_SYSERR == fwd)
2054 if (is_loopback (ch))
2056 /* It is a loopback channel after all... */
2060 /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
2061 fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
2064 ack = ntohl (msg->mid);
2065 LOG (GNUNET_ERROR_TYPE_INFO, "<=== %s ACK %u + %llX\n",
2066 GC_f2s (fwd), ack, msg->futures);
2068 if (GNUNET_YES == fwd)
2075 GNUNET_break (GNUNET_NO != ch->destroy);
2079 /* Free ACK'd copies: no need to retransmit those anymore FIXME refactor */
2080 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
2082 if (GC_is_pid_bigger (copy->mid, ack))
2084 LOG (GNUNET_ERROR_TYPE_DEBUG, " head %u, out!\n", copy->mid);
2085 if (0 < channel_rel_free_sent (rel, msg))
2090 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %u\n", copy->mid);
2092 if (GNUNET_YES == rel_message_free (copy, GNUNET_YES))
2094 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel destoyed\n");
2099 /* ACK client if needed and possible */
2100 GCCH_allow_client (ch, fwd);
2102 /* If some message was free'd, update the retransmission delay */
2103 if (GNUNET_YES == work)
2105 if (NULL != rel->retry_task)
2107 GNUNET_SCHEDULER_cancel (rel->retry_task);
2108 rel->retry_task = NULL;
2109 if (NULL != rel->head_sent && NULL == rel->head_sent->chq)
2111 struct GNUNET_TIME_Absolute new_target;
2112 struct GNUNET_TIME_Relative delay;
2114 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
2115 CADET_RETRANSMIT_MARGIN);
2116 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
2118 delay = GNUNET_TIME_absolute_get_remaining (new_target);
2120 GNUNET_SCHEDULER_add_delayed (delay,
2121 &channel_retransmit_message,
2127 /* Work was done but no task was pending.
2128 * Task was cancelled by a retransmission that is sitting in the queue.
2130 // FIXME add test to make sure this is the case, probably add return
2131 // value to GCCH_send_prebuilt_message
2138 * Handler for channel create messages.
2140 * Does not have fwd parameter because it's always 'FWD': channel is incoming.
2142 * @param t Tunnel this channel will be in.
2143 * @param msg Channel crate message.
2145 struct CadetChannel *
2146 GCCH_handle_create (struct CadetTunnel *t,
2147 const struct GNUNET_CADET_ChannelCreate *msg)
2149 CADET_ChannelNumber chid;
2150 struct CadetChannel *ch;
2151 struct CadetClient *c;
2154 chid = ntohl (msg->chid);
2155 ch = GCT_get_channel (t, chid);
2158 /* Create channel */
2159 ch = channel_new (t, NULL, 0);
2161 channel_set_options (ch, ntohl (msg->opt));
2162 new_channel = GNUNET_YES;
2166 new_channel = GNUNET_NO;
2169 if (GNUNET_YES == new_channel || GCT_is_loopback (t))
2171 /* Find a destination client */
2172 ch->port = ntohl (msg->port);
2173 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", ch->port);
2174 c = GML_client_get_by_port (ch->port);
2177 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
2178 if (is_loopback (ch))
2180 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback: destroy on handler\n");
2185 LOG (GNUNET_ERROR_TYPE_DEBUG, " not loopback: destroy now\n");
2193 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
2196 add_destination (ch, c);
2197 if (GNUNET_YES == ch->reliable)
2198 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reliable\n");
2200 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not Reliable\n");
2202 send_client_create (ch);
2203 ch->state = CADET_CHANNEL_SENT;
2207 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate create channel\n");
2208 if (NULL != ch->dest_rel->retry_task)
2210 LOG (GNUNET_ERROR_TYPE_DEBUG, " clearing retry task\n");
2211 /* we were waiting to re-send our 'SYNACK', wait no more! */
2212 GNUNET_SCHEDULER_cancel (ch->dest_rel->retry_task);
2213 ch->dest_rel->retry_task = NULL;
2215 else if (NULL != ch->dest_rel->uniq)
2217 /* we are waiting to for our 'SYNACK' to leave the queue, all done! */
2221 send_ack (ch, GNUNET_YES);
2228 * Handler for channel NACK messages.
2230 * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
2232 * @param ch Channel.
2235 GCCH_handle_nack (struct CadetChannel *ch)
2237 send_client_nack (ch);
2243 * Handler for channel ack messages.
2245 * @param ch Channel.
2246 * @param msg Message.
2247 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2248 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2249 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2250 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2253 GCCH_handle_ack (struct CadetChannel *ch,
2254 const struct GNUNET_CADET_ChannelManage *msg,
2257 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2258 if (GNUNET_SYSERR == fwd)
2260 if (is_loopback (ch))
2262 /* It is a loopback channel after all... */
2266 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2269 channel_confirm (ch, !fwd);
2274 * Handler for channel destroy messages.
2276 * @param ch Channel to be destroyed of.
2277 * @param msg Message.
2278 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2279 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2280 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2281 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2284 GCCH_handle_destroy (struct CadetChannel *ch,
2285 const struct GNUNET_CADET_ChannelManage *msg,
2288 struct CadetChannelReliability *rel;
2290 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2291 if (GNUNET_SYSERR == fwd)
2293 if (is_loopback (ch))
2295 /* It is a loopback channel after all... */
2299 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2302 GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
2303 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
2305 /* Not for us (don't destroy twice a half-open loopback channel) */
2309 rel = fwd ? ch->dest_rel : ch->root_rel;
2310 if (0 == rel->n_recv)
2312 send_destroy (ch, GNUNET_YES);
2317 ch->destroy = GNUNET_YES;
2323 * Sends an already built message on a channel.
2325 * If the channel is on a loopback tunnel, notifies the appropriate destination
2328 * On a normal channel passes the message to the tunnel for encryption and
2329 * sending on a connection.
2331 * This function DOES NOT save the message for retransmission.
2333 * @param message Message to send. Function makes a copy of it.
2334 * @param ch Channel on which this message is transmitted.
2335 * @param fwd Is this a fwd message?
2336 * @param existing_copy This is a retransmission, don't save a copy.
2339 GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2340 struct CadetChannel *ch, int fwd,
2341 void *existing_copy)
2343 struct CadetChannelQueue *chq;
2346 type = ntohs (message->type);
2347 LOG (GNUNET_ERROR_TYPE_INFO, "===> %s %s on channel %s\n",
2348 GC_m2s (type), GC_f2s (fwd), GCCH_2s (ch));
2350 if (GCT_is_loopback (ch->t))
2352 handle_loopback (ch, message, fwd);
2358 struct GNUNET_CADET_Data *payload;
2359 case GNUNET_MESSAGE_TYPE_CADET_DATA:
2361 payload = (struct GNUNET_CADET_Data *) message;
2362 LOG (GNUNET_ERROR_TYPE_INFO, "===> %s %u\n",
2363 GC_m2s (type), ntohl (payload->mid));
2364 if (GNUNET_YES == ch->reliable)
2366 chq = GNUNET_new (struct CadetChannelQueue);
2368 if (NULL == existing_copy)
2369 chq->copy = channel_save_copy (ch, message, fwd);
2372 chq->copy = (struct CadetReliableMessage *) existing_copy;
2373 if (NULL != chq->copy->chq)
2375 /* Last retransmission was queued but not yet sent!
2376 * This retransmission was scheduled by a ch_message_sent which
2377 * followed a very fast RTT, so the tiny delay made the
2378 * retransmission function to execute before the previous
2379 * retransmitted message even had a chance to leave the peer.
2380 * Cancel this message and wait until the pending
2381 * retransmission leaves the peer and ch_message_sent starts
2382 * the timer for the next one.
2385 LOG (GNUNET_ERROR_TYPE_DEBUG,
2386 " exisitng copy not yet transmitted!\n");
2389 LOG (GNUNET_ERROR_TYPE_DEBUG,
2390 " using existing copy: %p {r:%p q:%p t:%u}\n",
2392 chq->copy->rel, chq->copy->chq, chq->copy->type);
2394 LOG (GNUNET_ERROR_TYPE_DEBUG, " new chq: %p\n", chq);
2395 chq->copy->chq = chq;
2396 chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL,
2397 NULL != existing_copy,
2398 &ch_message_sent, chq);
2399 /* q itself is stored in copy */
2400 GNUNET_assert (NULL != chq->tq || GNUNET_NO != ch->destroy);
2404 fire_and_forget (message, ch, GNUNET_NO);
2409 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
2410 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
2411 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
2412 chq = GNUNET_new (struct CadetChannelQueue);
2414 chq->rel = fwd ? ch->root_rel : ch->dest_rel;
2415 if (NULL != chq->rel->uniq)
2417 if (NULL != chq->rel->uniq->tq)
2419 GCT_cancel (chq->rel->uniq->tq);
2420 /* ch_message_sent is called, freeing and NULLing uniq */
2421 GNUNET_break (NULL == chq->rel->uniq);
2426 GNUNET_free (chq->rel->uniq);
2430 chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL, GNUNET_YES,
2431 &ch_message_sent, chq);
2432 if (NULL == chq->tq)
2435 GCT_debug (ch->t, GNUNET_ERROR_TYPE_ERROR);
2440 chq->rel->uniq = chq;
2444 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2445 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
2446 fire_and_forget (message, ch, GNUNET_YES);
2452 LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s unknown!\n", GC_m2s (type));
2453 fire_and_forget (message, ch, GNUNET_YES);
2459 * Get the static string for identification of the channel.
2461 * @param ch Channel.
2463 * @return Static string with the channel IDs.
2466 GCCH_2s (const struct CadetChannel *ch)
2468 static char buf[64];
2471 return "(NULL Channel)";
2473 SPRINTF (buf, "%s:%u gid:%X (%X / %X)",
2474 GCT_2s (ch->t), ch->port, ch->gid, ch->lid_root, ch->lid_dest);