2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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_ChannelDataMessage 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.
214 struct GNUNET_HashCode port;
217 * Global channel number ( < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
219 struct GNUNET_CADET_ChannelNumber gid;
222 * Local tunnel number for root (owner) client.
223 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI or 0 )
225 struct GNUNET_CADET_ClientChannelNumber lid_root;
228 * Local tunnel number for local destination clients (incoming number)
229 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV or 0).
231 struct GNUNET_CADET_ClientChannelNumber 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 to #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_ChannelDataMessage *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_CHANNEL_DATA;
382 GNUNET_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_ChannelDataMessage *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_ChannelManageMessage msg;
518 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
519 msg.header.size = htons (sizeof (msg));
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.channel_of_client && 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.channel_of_client && 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,
559 GCT_get_destination (ch->t));
565 * Send data to a client.
567 * If the client is ready, send directly, otherwise buffer while listening
571 * @param msg Message.
572 * @param fwd Is this a fwd (root->dest) message?
575 send_client_data (struct CadetChannel *ch,
576 const struct GNUNET_CADET_ChannelDataMessage *msg,
581 if (ch->dest_rel->client_ready)
583 GML_send_data (ch->dest, msg, ch->lid_dest);
584 ch->dest_rel->client_ready = GNUNET_NO;
585 ch->dest_rel->mid_recv++;
588 add_buffered_data (msg, ch->dest_rel);
592 if (ch->root_rel->client_ready)
594 GML_send_data (ch->root, msg, ch->lid_root);
595 ch->root_rel->client_ready = GNUNET_NO;
596 ch->root_rel->mid_recv++;
599 add_buffered_data (msg, ch->root_rel);
605 * Send a buffered message to the client, for in order delivery or
606 * as result of client ACK.
608 * @param ch Channel on which to empty the message buffer.
609 * @param c Client to send to.
610 * @param fwd Is this to send FWD data?.
613 send_client_buffered_data (struct CadetChannel *ch,
614 struct CadetClient *c,
617 struct CadetReliableMessage *copy;
618 struct CadetChannelReliability *rel;
620 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
621 rel = fwd ? ch->dest_rel : ch->root_rel;
622 if (GNUNET_NO == rel->client_ready)
624 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
628 copy = rel->head_recv;
629 /* We never buffer channel management messages */
632 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
634 struct GNUNET_CADET_ChannelDataMessage *msg = (struct GNUNET_CADET_ChannelDataMessage *) ©[1];
636 LOG (GNUNET_ERROR_TYPE_DEBUG, " have %u! now expecting %u\n",
637 copy->mid, rel->mid_recv + 1);
638 send_client_data (ch, msg, fwd);
640 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
641 LOG (GNUNET_ERROR_TYPE_DEBUG, " free copy recv MID %u (%p), %u left\n",
642 copy->mid, copy, rel->n_recv);
644 GCCH_send_data_ack (ch, fwd);
648 LOG (GNUNET_ERROR_TYPE_DEBUG, " reliable && don't have %u, next is %u\n",
649 rel->mid_recv, copy->mid);
650 if (GNUNET_YES == ch->destroy)
652 /* We don't have the next data piece and the remote peer has closed the
653 * channel. We won't receive it anymore, so just destroy the channel.
654 * FIXME: wait some time to allow other connections to
655 * deliver missing messages
657 send_destroy (ch, GNUNET_YES);
662 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
667 * Allow a client to send more data.
669 * In case the client was already allowed to send data, do nothing.
672 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
675 send_client_ack (struct CadetChannel *ch, int fwd)
677 struct CadetChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
678 struct CadetClient *c = fwd ? ch->root : ch->dest;
682 GNUNET_break (GNUNET_NO != ch->destroy);
685 LOG (GNUNET_ERROR_TYPE_DEBUG,
686 " sending %s ack to client on channel %s\n",
687 GC_f2s (fwd), GCCH_2s (ch));
695 if (GNUNET_YES == rel->client_allowed)
697 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
700 rel->client_allowed = GNUNET_YES;
702 GML_send_ack (c, fwd ? ch->lid_root : ch->lid_dest);
707 * Notify the root that the destination rejected the channel.
709 * @param ch Rejected channel.
712 send_client_nack (struct CadetChannel *ch)
714 if (NULL == ch->root)
719 GML_send_channel_nack (ch->root, ch->lid_root);
724 * We haven't received an ACK after a certain time: restransmit the message.
726 * @param cls Closure (CadetChannelReliability with the message to restransmit)
729 channel_retransmit_message (void *cls)
731 struct CadetChannelReliability *rel = cls;
732 struct CadetReliableMessage *copy;
733 struct CadetChannel *ch;
734 struct GNUNET_CADET_ChannelDataMessage *payload;
737 rel->retry_task = NULL;
739 copy = rel->head_sent;
742 GNUNET_break (0); // FIXME tripped in rps testcase
746 payload = (struct GNUNET_CADET_ChannelDataMessage *) ©[1];
747 fwd = (rel == ch->root_rel);
749 /* Message not found in the queue that we are going to use. */
750 LOG (GNUNET_ERROR_TYPE_DEBUG, "RETRANSMIT MID %u\n", copy->mid);
752 GCCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
753 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
758 * We haven't received an Channel ACK after a certain time: resend the CREATE.
760 * @param cls Closure (CadetChannelReliability of the channel to recreate)
763 channel_recreate (void *cls)
765 struct CadetChannelReliability *rel = cls;
767 rel->retry_task = NULL;
768 LOG (GNUNET_ERROR_TYPE_DEBUG, "RE-CREATE\n");
769 GNUNET_STATISTICS_update (stats,
770 "# data retransmitted", 1, GNUNET_NO);
772 if (rel == rel->ch->root_rel)
774 send_create (rel->ch);
776 else if (rel == rel->ch->dest_rel)
778 send_ack (rel->ch, GNUNET_YES);
788 * Message has been sent: start retransmission timer.
790 * @param cls Closure (queue structure).
792 * @param q Queue handler (no longer valid).
793 * @param type Type of message.
794 * @param size Size of the message.
797 ch_message_sent (void *cls,
798 struct CadetTunnel *t,
799 struct CadetTunnelQueue *q,
800 uint16_t type, size_t size)
802 struct CadetChannelQueue *chq = cls;
803 struct CadetReliableMessage *copy = chq->copy;
804 struct CadetChannelReliability *rel;
806 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel_message_sent callback %s\n",
811 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA:
812 LOG (GNUNET_ERROR_TYPE_DEBUG, "data MID %u sent\n", copy->mid);
813 GNUNET_assert (chq == copy->chq);
814 copy->timestamp = GNUNET_TIME_absolute_get ();
816 if (NULL == rel->retry_task)
818 LOG (GNUNET_ERROR_TYPE_DEBUG, " scheduling retry in %d * %s\n",
819 CADET_RETRANSMIT_MARGIN,
820 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
822 if (0 != rel->expected_delay.rel_value_us)
825 GNUNET_TIME_relative_saturating_multiply (rel->expected_delay,
826 CADET_RETRANSMIT_MARGIN);
830 rel->retry_timer = CADET_RETRANSMIT_TIME;
832 LOG (GNUNET_ERROR_TYPE_DEBUG, " using delay %s\n",
833 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
836 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
837 &channel_retransmit_message, rel);
841 LOG (GNUNET_ERROR_TYPE_DEBUG, "retry running %p\n", rel->retry_task);
847 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK:
848 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
849 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK:
850 LOG (GNUNET_ERROR_TYPE_DEBUG, "sent %s\n", GC_m2s (chq->type));
852 GNUNET_assert (rel->uniq == chq);
855 if (CADET_CHANNEL_READY != rel->ch->state
856 && GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK != type
857 && GNUNET_NO == rel->ch->destroy)
859 GNUNET_assert (NULL == rel->retry_task);
860 LOG (GNUNET_ERROR_TYPE_DEBUG, "STD BACKOFF %s\n",
861 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
863 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
864 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
865 &channel_recreate, rel);
878 * send a channel create message.
880 * @param ch Channel for which to send.
883 send_create (struct CadetChannel *ch)
885 struct GNUNET_CADET_ChannelCreateMessage msgcc;
887 msgcc.header.size = htons (sizeof (msgcc));
888 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
889 msgcc.chid = ch->gid;
890 msgcc.port = ch->port;
891 msgcc.opt = htonl (channel_get_options (ch));
893 GCCH_send_prebuilt_message (&msgcc.header, ch, GNUNET_YES, NULL);
898 * Confirm we got a channel create or FWD ack.
900 * @param ch The channel to confirm.
901 * @param fwd Should we send a FWD ACK? (going dest->root)
904 send_ack (struct CadetChannel *ch, int fwd)
906 struct GNUNET_CADET_ChannelManageMessage msg;
908 msg.header.size = htons (sizeof (msg));
909 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK);
910 LOG (GNUNET_ERROR_TYPE_DEBUG,
911 " sending channel %s ack for channel %s\n",
912 GC_f2s (fwd), GCCH_2s (ch));
915 GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
920 * Send a message and don't keep any info about it: we won't need to cancel it
923 * @param msg Header of the message to fire away.
924 * @param ch Channel on which the message should go.
925 * @param force Is this a forced (undroppable) message?
928 fire_and_forget (const struct GNUNET_MessageHeader *msg,
929 struct CadetChannel *ch,
932 GNUNET_break (NULL ==
933 GCT_send_prebuilt_message (msg, ch->t, NULL,
939 * Notify that a channel create didn't succeed.
941 * @param ch The channel to reject.
944 send_nack (struct CadetChannel *ch)
946 struct GNUNET_CADET_ChannelManageMessage msg;
948 msg.header.size = htons (sizeof (msg));
949 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_NACK_DEPRECATED);
950 LOG (GNUNET_ERROR_TYPE_DEBUG,
951 " sending channel NACK for channel %s\n",
955 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
960 * Destroy all reliable messages queued for a channel,
961 * during a channel destruction.
962 * Frees the reliability structure itself.
964 * @param rel Reliability data for a channel.
967 channel_rel_free_all (struct CadetChannelReliability *rel)
969 struct CadetReliableMessage *copy;
970 struct CadetReliableMessage *next;
975 for (copy = rel->head_recv; NULL != copy; copy = next)
978 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
979 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE ALL RECV %p\n", copy);
980 GNUNET_break (NULL == copy->chq);
983 for (copy = rel->head_sent; NULL != copy; copy = next)
986 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
987 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE ALL SEND %p\n", copy);
988 if (NULL != copy->chq)
990 if (NULL != copy->chq->tq)
992 GCT_cancel (copy->chq->tq);
993 /* ch_message_sent will free copy->q */
997 GNUNET_free (copy->chq);
1003 if (NULL != rel->uniq && NULL != rel->uniq->tq)
1005 GCT_cancel (rel->uniq->tq);
1006 /* ch_message_sent is called freeing uniq */
1008 if (NULL != rel->retry_task)
1010 GNUNET_SCHEDULER_cancel (rel->retry_task);
1011 rel->retry_task = NULL;
1018 * Mark future messages as ACK'd.
1020 * @param rel Reliability data.
1021 * @param msg DataACK message with a bitfield of future ACK'd messages.
1023 * @return How many messages have been freed.
1026 channel_rel_free_sent (struct CadetChannelReliability *rel,
1027 const struct GNUNET_CADET_ChannelDataAckMessage *msg)
1029 struct CadetReliableMessage *copy;
1030 struct CadetReliableMessage *next;
1038 bitfield = msg->futures;
1039 mid = ntohl (msg->mid);
1040 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable %u %lX\n", mid, bitfield);
1041 LOG (GNUNET_ERROR_TYPE_DEBUG, " rel %p, head %p\n", rel, rel->head_sent);
1042 for (i = 0, r = 0, copy = rel->head_sent;
1043 i < 64 && NULL != copy && 0 != bitfield;
1046 LOG (GNUNET_ERROR_TYPE_DEBUG, " trying bit %u (mid %u)\n", i, mid + i + 1);
1048 if (0 == (bitfield & mask))
1051 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
1052 /* Bit was set, clear the bit from the bitfield */
1055 /* The i-th bit was set. Do we have that copy? */
1056 /* Skip copies with mid < target */
1057 target = mid + i + 1;
1058 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
1059 while (NULL != copy && GC_is_pid_bigger (target, copy->mid))
1062 /* Did we run out of copies? (previously freed, it's ok) */
1065 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
1069 /* Did we overshoot the target? (previously freed, it's ok) */
1070 if (GC_is_pid_bigger (copy->mid, target))
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
1073 i += copy->mid - target - 1; /* MID: 90, t = 85, i += 4 (i++ later) */
1074 mask = (0x1LL << (i + 1)) - 1; /* Mask = i-th bit and all before */
1075 bitfield &= ~mask; /* Clear all bits up to MID - 1 */
1079 /* Now copy->mid == target, free it */
1081 GNUNET_break (GNUNET_YES != rel_message_free (copy, GNUNET_YES));
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
1091 * Destroy a reliable message after it has been acknowledged, either by
1092 * direct mid ACK or bitfield. Updates the appropriate data structures and
1093 * timers and frees all memory.
1095 * @param copy Message that is no longer needed: remote peer got it.
1096 * @param update_time Is the timing information relevant?
1097 * If this message is ACK in a batch the timing information
1098 * is skewed by the retransmission, count only for the
1099 * retransmitted message.
1101 * @return #GNUNET_YES if channel was destroyed as a result of the call,
1102 * #GNUNET_NO otherwise.
1105 rel_message_free (struct CadetReliableMessage *copy, int update_time)
1107 struct CadetChannelReliability *rel;
1108 struct GNUNET_TIME_Relative time;
1111 LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing %u\n", copy->mid);
1112 if (GNUNET_YES == update_time)
1114 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
1115 if (0 == rel->expected_delay.rel_value_us)
1116 rel->expected_delay = time;
1119 rel->expected_delay.rel_value_us *= 7;
1120 rel->expected_delay.rel_value_us += time.rel_value_us;
1121 rel->expected_delay.rel_value_us /= 8;
1123 LOG (GNUNET_ERROR_TYPE_DEBUG, " message time %12s\n",
1124 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
1125 LOG (GNUNET_ERROR_TYPE_DEBUG, " new delay %12s\n",
1126 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
1128 rel->retry_timer = rel->expected_delay;
1132 LOG (GNUNET_ERROR_TYPE_DEBUG, "batch free, ignoring timing\n");
1134 rel->ch->pending_messages--;
1135 if (NULL != copy->chq)
1137 GCT_cancel (copy->chq->tq);
1138 /* copy->q is set to NULL by ch_message_sent */
1140 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, " free send copy MID %u at %p\n",
1145 if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
1147 GCCH_destroy (rel->ch);
1155 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
1157 * @param ch Channel to mark as ready.
1158 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
1161 channel_confirm (struct CadetChannel *ch, int fwd)
1163 struct CadetChannelReliability *rel;
1164 enum CadetChannelState oldstate;
1166 rel = fwd ? ch->root_rel : ch->dest_rel;
1169 GNUNET_break (GNUNET_NO != ch->destroy);
1172 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel confirm %s %s\n",
1173 GC_f2s (fwd), GCCH_2s (ch));
1174 oldstate = ch->state;
1175 ch->state = CADET_CHANNEL_READY;
1177 if (CADET_CHANNEL_READY != oldstate || GNUNET_YES == is_loopback (ch))
1179 rel->client_ready = GNUNET_YES;
1180 rel->expected_delay = rel->retry_timer;
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, " confirm retry timer %s\n",
1182 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer, GNUNET_NO));
1183 if (GCT_get_connections_buffer (ch->t) > 0 || GCT_is_loopback (ch->t))
1184 send_client_ack (ch, fwd);
1186 if (NULL != rel->retry_task)
1188 GNUNET_SCHEDULER_cancel (rel->retry_task);
1189 rel->retry_task = NULL;
1191 else if (NULL != rel->uniq)
1193 GCT_cancel (rel->uniq->tq);
1194 /* ch_message_sent will free and NULL uniq */
1196 else if (GNUNET_NO == is_loopback (ch))
1198 /* We SHOULD have been trying to retransmit this! */
1203 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
1204 if (GNUNET_YES == fwd)
1205 send_ack (ch, GNUNET_NO);
1210 * Save a copy to retransmit in case it gets lost.
1212 * Initializes all needed callbacks and timers.
1214 * @param ch Channel this message goes on.
1215 * @param msg Message to copy.
1216 * @param fwd Is this fwd traffic?
1218 static struct CadetReliableMessage *
1219 channel_save_copy (struct CadetChannel *ch,
1220 const struct GNUNET_MessageHeader *msg,
1223 struct CadetChannelReliability *rel;
1224 struct CadetReliableMessage *copy;
1229 rel = fwd ? ch->root_rel : ch->dest_rel;
1230 mid = rel->mid_send - 1;
1231 type = ntohs (msg->type);
1232 size = ntohs (msg->size);
1234 LOG (GNUNET_ERROR_TYPE_DEBUG, "save MID %u %s\n", mid, GC_m2s (type));
1235 copy = GNUNET_malloc (sizeof (struct CadetReliableMessage) + size);
1236 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", copy);
1240 GNUNET_memcpy (©[1], msg, size);
1241 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
1242 ch->pending_messages++;
1249 * Create a new channel.
1251 * @param t Tunnel this channel is in.
1252 * @param owner Client that owns the channel, NULL for foreign channels.
1253 * @param lid_root Local ID for root client.
1255 * @return A new initialized channel. NULL on error.
1257 static struct CadetChannel *
1258 channel_new (struct CadetTunnel *t,
1259 struct CadetClient *owner,
1260 struct GNUNET_CADET_ClientChannelNumber lid_root)
1262 struct CadetChannel *ch;
1264 ch = GNUNET_new (struct CadetChannel);
1266 ch->lid_root = lid_root;
1269 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
1273 ch->gid = GCT_get_next_chid (t);
1274 GML_channel_add (owner, lid_root, ch);
1276 GCT_add_channel (t, ch);
1283 * Handle a loopback message: call the appropriate handler for the message type.
1285 * @param ch Channel this message is on.
1286 * @param msgh Message header.
1287 * @param fwd Is this FWD traffic?
1290 handle_loopback (struct CadetChannel *ch,
1291 const struct GNUNET_MessageHeader *msgh,
1296 type = ntohs (msgh->type);
1297 LOG (GNUNET_ERROR_TYPE_DEBUG,
1298 "Loopback %s %s message!\n",
1299 GC_f2s (fwd), GC_m2s (type));
1303 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA:
1304 /* Don't send hop ACK, wait for client to ACK */
1305 LOG (GNUNET_ERROR_TYPE_DEBUG, "SEND loopback %u (%u)\n",
1306 ntohl (((struct GNUNET_CADET_ChannelDataMessage *) msgh)->mid), ntohs (msgh->size));
1307 GCCH_handle_data (ch, (struct GNUNET_CADET_ChannelDataMessage *) msgh, fwd);
1310 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK:
1311 GCCH_handle_data_ack (ch, (struct GNUNET_CADET_ChannelDataAckMessage *) msgh, fwd);
1314 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1315 GCCH_handle_create (ch->t,
1316 (struct GNUNET_CADET_ChannelCreateMessage *) msgh);
1319 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK:
1320 GCCH_handle_ack (ch,
1321 (struct GNUNET_CADET_ChannelManageMessage *) msgh,
1325 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_NACK_DEPRECATED:
1326 GCCH_handle_nack (ch);
1329 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1330 GCCH_handle_destroy (ch,
1331 (struct GNUNET_CADET_ChannelManageMessage *) msgh,
1336 GNUNET_break_op (0);
1337 LOG (GNUNET_ERROR_TYPE_DEBUG,
1338 "end-to-end message not known (%u)\n",
1339 ntohs (msgh->type));
1345 /******************************************************************************/
1346 /******************************** API ***********************************/
1347 /******************************************************************************/
1350 * Destroy a channel and free all resources.
1352 * @param ch Channel to destroy.
1355 GCCH_destroy (struct CadetChannel *ch)
1357 struct CadetClient *c;
1358 struct CadetTunnel *t;
1362 if (2 == ch->destroy)
1363 return; /* recursive call */
1366 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
1367 GCT_2s (ch->t), ch->gid);
1368 GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
1373 GML_channel_remove (c, ch->lid_root, ch);
1379 GML_channel_remove (c, ch->lid_dest, ch);
1382 channel_rel_free_all (ch->root_rel);
1383 channel_rel_free_all (ch->dest_rel);
1386 GCT_remove_channel (t, ch);
1387 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
1390 GCT_destroy_if_empty (t);
1395 * Get the channel's public ID.
1397 * @param ch Channel.
1399 * @return ID used to identify the channel with the remote peer.
1401 struct GNUNET_CADET_ChannelNumber
1402 GCCH_get_id (const struct CadetChannel *ch)
1409 * Get the channel tunnel.
1411 * @param ch Channel to get the tunnel from.
1413 * @return tunnel of the channel.
1415 struct CadetTunnel *
1416 GCCH_get_tunnel (const struct CadetChannel *ch)
1423 * Get free buffer space towards the client on a specific channel.
1425 * @param ch Channel.
1426 * @param fwd Is query about FWD traffic?
1428 * @return Free buffer space [0 - 64]
1431 GCCH_get_buffer (struct CadetChannel *ch, int fwd)
1433 struct CadetChannelReliability *rel;
1435 rel = fwd ? ch->dest_rel : ch->root_rel;
1436 LOG (GNUNET_ERROR_TYPE_DEBUG, " get buffer, channel %s\n", GCCH_2s (ch));
1437 GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
1438 /* If rel is NULL it means that the end is not yet created,
1439 * most probably is a loopback channel at the point of sending
1440 * the ChannelCreate to itself.
1444 LOG (GNUNET_ERROR_TYPE_DEBUG, " rel is NULL: max\n");
1448 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_recv %d\n", rel->n_recv);
1449 return (64 - rel->n_recv);
1454 * Get flow control status of end point: is client allow to send?
1456 * @param ch Channel.
1457 * @param fwd Is query about FWD traffic? (Request root status).
1459 * @return #GNUNET_YES if client is allowed to send us data.
1462 GCCH_get_allowed (struct CadetChannel *ch, int fwd)
1464 struct CadetChannelReliability *rel;
1466 rel = fwd ? ch->root_rel : ch->dest_rel;
1470 /* Probably shutting down: root/dest NULL'ed to mark disconnection */
1471 GNUNET_break (GNUNET_NO != ch->destroy);
1475 return rel->client_allowed;
1480 * Is the root client for this channel on this peer?
1482 * @param ch Channel.
1483 * @param fwd Is this for fwd traffic?
1485 * @return #GNUNET_YES in case it is.
1488 GCCH_is_origin (struct CadetChannel *ch, int fwd)
1490 struct CadetClient *c;
1492 c = fwd ? ch->root : ch->dest;
1498 * Is the destination client for this channel on this peer?
1500 * @param ch Channel.
1501 * @param fwd Is this for fwd traffic?
1503 * @return #GNUNET_YES in case it is.
1506 GCCH_is_terminal (struct CadetChannel *ch, int fwd)
1508 struct CadetClient *c;
1510 c = fwd ? ch->dest : ch->root;
1516 * Send an end-to-end ACK message for the most recent in-sequence payload.
1518 * If channel is not reliable, do nothing.
1520 * @param ch Channel this is about.
1521 * @param fwd Is for FWD traffic? (ACK dest->owner)
1524 GCCH_send_data_ack (struct CadetChannel *ch, int fwd)
1526 struct GNUNET_CADET_ChannelDataAckMessage msg;
1527 struct CadetChannelReliability *rel;
1528 struct CadetReliableMessage *copy;
1533 if (GNUNET_NO == ch->reliable)
1536 rel = fwd ? ch->dest_rel : ch->root_rel;
1537 ack = rel->mid_recv - 1;
1539 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK);
1540 msg.header.size = htons (sizeof (msg));
1542 msg.mid = htonl (ack);
1545 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1547 if (copy->type != GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA)
1549 LOG (GNUNET_ERROR_TYPE_DEBUG, " Type %s, expected DATA\n",
1550 GC_m2s (copy->type));
1553 GNUNET_assert (GC_is_pid_bigger(copy->mid, ack));
1554 delta = copy->mid - (ack + 1);
1557 mask = 0x1LL << delta;
1558 msg.futures |= mask;
1559 LOG (GNUNET_ERROR_TYPE_DEBUG,
1560 " setting bit for %u (delta %u) (%lX) -> %lX\n",
1561 copy->mid, delta, mask, msg.futures);
1564 GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1565 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1570 * Allow a client to send us more data, in case it was choked.
1572 * @param ch Channel.
1573 * @param fwd Is this about FWD traffic? (Root client).
1576 GCCH_allow_client (struct CadetChannel *ch, int fwd)
1578 struct CadetChannelReliability *rel;
1579 unsigned int buffer;
1581 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1583 if (CADET_CHANNEL_READY != ch->state)
1585 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel not ready yet!\n");
1589 if (GNUNET_YES == ch->reliable)
1591 rel = fwd ? ch->root_rel : ch->dest_rel;
1594 GNUNET_break (GNUNET_NO != ch->destroy);
1597 if (NULL != rel->head_sent)
1599 if (64 <= rel->mid_send - rel->head_sent->mid)
1601 LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
1606 LOG (GNUNET_ERROR_TYPE_DEBUG, " gap ok: %u - %u\n",
1607 rel->head_sent->mid, rel->mid_send);
1608 struct CadetReliableMessage *aux;
1609 for (aux = rel->head_sent; NULL != aux; aux = aux->next)
1611 LOG (GNUNET_ERROR_TYPE_DEBUG, " - sent mid %u\n", aux->mid);
1617 LOG (GNUNET_ERROR_TYPE_DEBUG, " head sent is NULL\n");
1621 if (is_loopback (ch))
1622 buffer = GCCH_get_buffer (ch, fwd);
1624 buffer = GCT_get_connections_buffer (ch->t);
1628 LOG (GNUNET_ERROR_TYPE_DEBUG, " no buffer space.\n");
1632 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space %u, allowing\n", buffer);
1633 send_client_ack (ch, fwd);
1640 * @param ch Channel.
1641 * @param level Debug level to use.
1644 GCCH_debug (struct CadetChannel *ch, enum GNUNET_ErrorType level)
1648 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
1650 __FILE__, __FUNCTION__, __LINE__);
1656 LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
1659 LOG2 (level, "CHN Channel %s:%X (%p)\n", GCT_2s (ch->t), ch->gid, ch);
1660 LOG2 (level, "CHN root %p/%p\n", ch->root, ch->root_rel);
1661 if (NULL != ch->root)
1663 LOG2 (level, "CHN cli %s\n", GML_2s (ch->root));
1664 LOG2 (level, "CHN ready %s\n", ch->root_rel->client_ready ? "YES" : "NO");
1665 LOG2 (level, "CHN id %X\n", ch->lid_root.channel_of_client);
1666 LOG2 (level, "CHN recv %d\n", ch->root_rel->n_recv);
1667 LOG2 (level, "CHN MID r: %d, s: %d\n",
1668 ch->root_rel->mid_recv, ch->root_rel->mid_send);
1670 LOG2 (level, "CHN dest %p/%p\n",
1671 ch->dest, ch->dest_rel);
1672 if (NULL != ch->dest)
1674 LOG2 (level, "CHN cli %s\n", GML_2s (ch->dest));
1675 LOG2 (level, "CHN ready %s\n", ch->dest_rel->client_ready ? "YES" : "NO");
1676 LOG2 (level, "CHN id %X\n", ch->lid_dest);
1677 LOG2 (level, "CHN recv %d\n", ch->dest_rel->n_recv);
1678 LOG2 (level, "CHN MID r: %d, s: %d\n",
1679 ch->dest_rel->mid_recv, ch->dest_rel->mid_send);
1686 * Handle an ACK given by a client.
1688 * Mark client as ready and send him any buffered data we could have for him.
1690 * @param ch Channel.
1691 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1694 GCCH_handle_local_ack (struct CadetChannel *ch, int fwd)
1696 struct CadetChannelReliability *rel;
1697 struct CadetClient *c;
1699 rel = fwd ? ch->dest_rel : ch->root_rel;
1700 c = fwd ? ch->dest : ch->root;
1702 rel->client_ready = GNUNET_YES;
1703 send_client_buffered_data (ch, c, fwd);
1705 if (GNUNET_YES == ch->destroy && 0 == rel->n_recv)
1707 send_destroy (ch, GNUNET_YES);
1711 /* if loopback is marked for destruction, no need to ACK to the other peer,
1712 * it requested the destruction and is already gone, therefore, else if.
1714 else if (is_loopback (ch))
1716 unsigned int buffer;
1718 buffer = GCCH_get_buffer (ch, fwd);
1720 GCCH_allow_client (ch, fwd);
1724 GCT_send_connection_acks (ch->t);
1729 * Handle data given by a client.
1731 * Check whether the client is allowed to send in this tunnel, save if channel
1732 * is reliable and send an ACK to the client if there is still buffer space
1735 * @param ch Channel.
1736 * @param c Client which sent the data.
1737 * @param fwd Is this a FWD data?
1738 * @param message Data message.
1739 * @param size Size of data.
1741 * @return #GNUNET_OK if everything goes well, #GNUNET_SYSERR in case of en error.
1744 GCCH_handle_local_data (struct CadetChannel *ch,
1745 struct CadetClient *c,
1747 const struct GNUNET_MessageHeader *message,
1750 struct CadetChannelReliability *rel;
1751 struct GNUNET_CADET_ChannelDataMessage *payload;
1752 uint16_t p2p_size = sizeof(struct GNUNET_CADET_ChannelDataMessage) + size;
1753 unsigned char cbuf[p2p_size];
1754 unsigned char buffer;
1756 /* Is the client in the channel? */
1763 GNUNET_break_op (0);
1764 return GNUNET_SYSERR;
1767 rel = fwd ? ch->root_rel : ch->dest_rel;
1769 if (GNUNET_NO == rel->client_allowed)
1771 GNUNET_break_op (0);
1772 return GNUNET_SYSERR;
1775 rel->client_allowed = GNUNET_NO;
1777 /* Ok, everything is correct, send the message. */
1778 payload = (struct GNUNET_CADET_ChannelDataMessage *) cbuf;
1779 payload->mid = htonl (rel->mid_send);
1781 GNUNET_memcpy (&payload[1], message, size);
1782 payload->header.size = htons (p2p_size);
1783 payload->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA);
1784 payload->chid = ch->gid;
1785 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1786 GCCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1788 if (is_loopback (ch))
1789 buffer = GCCH_get_buffer (ch, fwd);
1791 buffer = GCT_get_connections_buffer (ch->t);
1794 GCCH_allow_client (ch, fwd);
1801 * Handle a channel destroy requested by a client.
1803 * TODO: add "reason" field
1805 * Destroy the channel and the tunnel in case this was the last channel.
1807 * @param ch Channel.
1808 * @param c Client that requested the destruction (to avoid notifying him).
1809 * @param is_root Is the request coming from root?
1812 GCCH_handle_local_destroy (struct CadetChannel *ch,
1813 struct CadetClient *c,
1816 ch->destroy = GNUNET_YES;
1817 /* Cleanup after the tunnel */
1818 if (GNUNET_NO == is_root && c == ch->dest)
1820 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1821 GML_client_delete_channel (c, ch, ch->lid_dest);
1824 if (GNUNET_YES == is_root && c == ch->root)
1826 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1827 GML_client_delete_channel (c, ch, ch->lid_root);
1831 send_destroy (ch, GNUNET_NO);
1832 if (0 == ch->pending_messages)
1838 * Handle a channel create requested by a client.
1840 * Create the channel and the tunnel in case this was the first0 channel.
1842 * @param c Client that requested the creation (will be the root).
1843 * @param msg Create Channel message.
1845 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1848 GCCH_handle_local_create (struct CadetClient *c,
1849 struct GNUNET_CADET_ChannelCreateMessageMessage *msg)
1851 struct CadetChannel *ch;
1852 struct CadetTunnel *t;
1853 struct CadetPeer *peer;
1854 struct GNUNET_CADET_ClientChannelNumber chid;
1856 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1857 GNUNET_i2s (&msg->peer), GNUNET_h2s (&msg->port));
1858 chid = msg->channel_id;
1860 /* Sanity check for duplicate channel IDs */
1861 if (NULL != GML_channel_get (c, chid))
1864 return GNUNET_SYSERR;
1867 peer = GCP_get (&msg->peer, GNUNET_YES);
1868 GCP_add_tunnel (peer);
1869 t = GCP_get_tunnel (peer);
1871 if (GCP_get_short_id (peer) == myid)
1873 GCT_change_cstate (t, CADET_TUNNEL_READY);
1877 /* FIXME change to a tunnel API, eliminate ch <-> peer connection */
1881 /* Create channel */
1882 ch = channel_new (t, c, chid);
1886 return GNUNET_SYSERR;
1888 ch->port = msg->port;
1889 channel_set_options (ch, ntohl (msg->opt));
1891 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1892 ch->root_rel = GNUNET_new (struct CadetChannelReliability);
1893 ch->root_rel->ch = ch;
1894 ch->root_rel->retry_timer = CADET_RETRANSMIT_TIME;
1895 ch->root_rel->expected_delay.rel_value_us = 0;
1897 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GCCH_2s (ch));
1906 * Handler for cadet network payload traffic.
1908 * @param ch Channel for the message.
1909 * @param msg Unencryted data message.
1910 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1911 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1912 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1913 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1916 GCCH_handle_data (struct CadetChannel *ch,
1917 const struct GNUNET_CADET_ChannelDataMessage *msg,
1920 struct CadetChannelReliability *rel;
1921 struct CadetClient *c;
1922 struct GNUNET_MessageHeader *payload_msg;
1924 uint16_t payload_type;
1925 uint16_t payload_size;
1927 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1928 if (GNUNET_SYSERR == fwd)
1930 if (is_loopback (ch))
1932 /* It is a loopback channel after all... */
1936 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1939 /* Initialize FWD/BCK data */
1940 c = fwd ? ch->dest : ch->root;
1941 rel = fwd ? ch->dest_rel : ch->root_rel;
1945 GNUNET_break (GNUNET_NO != ch->destroy);
1949 if (CADET_CHANNEL_READY != ch->state)
1951 if (GNUNET_NO == fwd)
1953 /* If we are the root, this means the other peer has sent traffic before
1954 * receiving our ACK. Even if the SYNACK goes missing, no traffic should
1955 * be sent before the ACK.
1957 GNUNET_break_op (0);
1960 /* If we are the dest, this means that the SYNACK got to the root but
1961 * the ACK went missing. Treat this as an ACK.
1963 channel_confirm (ch, GNUNET_NO);
1966 payload_msg = (struct GNUNET_MessageHeader *) &msg[1];
1967 payload_type = ntohs (payload_msg->type);
1968 payload_size = ntohs (payload_msg->size);
1970 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1971 GNUNET_STATISTICS_update (stats, "# bytes received", payload_size, GNUNET_NO);
1973 mid = ntohl (msg->mid);
1974 LOG (GNUNET_ERROR_TYPE_INFO, "<== %s (%s %4u) on chan %s (%p) %s [%5u]\n",
1975 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA), GC_m2s (payload_type), mid,
1976 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
1978 if ( (GNUNET_NO == ch->reliable) ||
1979 ( (! GC_is_pid_bigger (rel->mid_recv, mid)) &&
1980 GC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1982 if (GNUNET_YES == ch->reliable)
1984 /* Is this the exact next expected messasge? */
1985 if (mid == rel->mid_recv)
1987 LOG (GNUNET_ERROR_TYPE_DEBUG,
1988 "as expected, sending to client\n");
1989 send_client_data (ch, msg, fwd);
1993 LOG (GNUNET_ERROR_TYPE_DEBUG,
1994 "save for later\n");
1995 add_buffered_data (msg, rel);
2000 /* Tunnel is unreliable: send to clients directly */
2001 /* FIXME: accept Out Of Order traffic */
2002 rel->mid_recv = mid + 1;
2003 send_client_data (ch, msg, fwd);
2008 GNUNET_STATISTICS_update (stats, "# duplicate MID", 1, GNUNET_NO);
2009 if (GC_is_pid_bigger (rel->mid_recv, mid))
2011 GNUNET_break_op (0);
2012 LOG (GNUNET_ERROR_TYPE_WARNING,
2013 "MID %u on channel %s not expected (window: %u - %u). Dropping!\n",
2014 mid, GCCH_2s (ch), rel->mid_recv, rel->mid_recv + 63);
2018 LOG (GNUNET_ERROR_TYPE_INFO,
2019 "Duplicate MID %u, channel %s (expecting MID %u). Re-sending ACK!\n",
2020 mid, GCCH_2s (ch), rel->mid_recv);
2021 if (NULL != rel->uniq)
2023 LOG (GNUNET_ERROR_TYPE_WARNING,
2024 "We are trying to send an ACK, but don't seem have the "
2025 "bandwidth. Have you set enough [ats] QUOTA in your config?\n");
2031 GCCH_send_data_ack (ch, fwd);
2036 * Handler for cadet network traffic end-to-end ACKs.
2038 * @param ch Channel on which we got this message.
2039 * @param msg Data message.
2040 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2041 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2042 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2043 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2046 GCCH_handle_data_ack (struct CadetChannel *ch,
2047 const struct GNUNET_CADET_ChannelDataAckMessage *msg,
2050 struct CadetChannelReliability *rel;
2051 struct CadetReliableMessage *copy;
2052 struct CadetReliableMessage *next;
2056 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2057 if (GNUNET_SYSERR == fwd)
2059 if (is_loopback (ch))
2061 /* It is a loopback channel after all... */
2065 /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
2066 fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
2069 ack = ntohl (msg->mid);
2070 LOG (GNUNET_ERROR_TYPE_INFO,
2071 "<== %s (0x%010lX %4u) on chan %s (%p) %s [%5u]\n",
2072 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK), msg->futures, ack,
2073 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
2075 if (GNUNET_YES == fwd)
2082 GNUNET_break (GNUNET_NO != ch->destroy);
2086 /* Free ACK'd copies: no need to retransmit those anymore FIXME refactor */
2087 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
2089 if (GC_is_pid_bigger (copy->mid, ack))
2091 LOG (GNUNET_ERROR_TYPE_DEBUG, " head %u, out!\n", copy->mid);
2092 if (0 < channel_rel_free_sent (rel, msg))
2097 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %u\n", copy->mid);
2099 if (GNUNET_YES == rel_message_free (copy, GNUNET_YES))
2101 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel destoyed\n");
2106 /* ACK client if needed and possible */
2107 GCCH_allow_client (ch, fwd);
2109 /* If some message was free'd, update the retransmission delay */
2110 if (GNUNET_YES == work)
2112 if (NULL != rel->retry_task)
2114 GNUNET_SCHEDULER_cancel (rel->retry_task);
2115 rel->retry_task = NULL;
2116 if (NULL != rel->head_sent && NULL == rel->head_sent->chq)
2118 struct GNUNET_TIME_Absolute new_target;
2119 struct GNUNET_TIME_Relative delay;
2121 delay = GNUNET_TIME_relative_saturating_multiply (rel->retry_timer,
2122 CADET_RETRANSMIT_MARGIN);
2123 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
2125 delay = GNUNET_TIME_absolute_get_remaining (new_target);
2127 GNUNET_SCHEDULER_add_delayed (delay,
2128 &channel_retransmit_message,
2134 /* Work was done but no task was pending.
2135 * Task was cancelled by a retransmission that is sitting in the queue.
2137 // FIXME add test to make sure this is the case, probably add return
2138 // value to GCCH_send_prebuilt_message
2145 * Handler for channel create messages.
2147 * Does not have fwd parameter because it's always 'FWD': channel is incoming.
2149 * @param t Tunnel this channel will be in.
2150 * @param msg Channel crate message.
2152 struct CadetChannel *
2153 GCCH_handle_create (struct CadetTunnel *t,
2154 const struct GNUNET_CADET_ChannelCreateMessage *msg)
2156 struct GNUNET_CADET_ClientChannelNumber chid;
2157 struct GNUNET_CADET_ChannelNumber gid;
2158 struct CadetChannel *ch;
2159 struct CadetClient *c;
2161 const struct GNUNET_HashCode *port;
2164 ch = GCT_get_channel (t, gid);
2167 /* Create channel */
2168 chid.channel_of_client = htonl (0);
2169 ch = channel_new (t, NULL, chid);
2171 channel_set_options (ch, ntohl (msg->opt));
2172 new_channel = GNUNET_YES;
2176 new_channel = GNUNET_NO;
2180 LOG (GNUNET_ERROR_TYPE_INFO,
2181 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2182 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE), chid, port,
2183 GCCH_2s (ch), ch, GC_f2s (GNUNET_YES), ntohs (msg->header.size));
2185 if (GNUNET_YES == new_channel || GCT_is_loopback (t))
2187 /* Find a destination client */
2189 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %s\n", GNUNET_h2s (port));
2190 c = GML_client_get_by_port (port);
2193 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
2194 if (is_loopback (ch))
2196 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback: destroy on handler\n");
2201 LOG (GNUNET_ERROR_TYPE_DEBUG, " not loopback: destroy now\n");
2209 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
2212 add_destination (ch, c);
2213 if (GNUNET_YES == ch->reliable)
2214 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reliable\n");
2216 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not Reliable\n");
2218 send_client_create (ch);
2219 ch->state = CADET_CHANNEL_SENT;
2223 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate create channel\n");
2224 if (NULL != ch->dest_rel->retry_task)
2226 LOG (GNUNET_ERROR_TYPE_DEBUG, " clearing retry task\n");
2227 /* we were waiting to re-send our 'SYNACK', wait no more! */
2228 GNUNET_SCHEDULER_cancel (ch->dest_rel->retry_task);
2229 ch->dest_rel->retry_task = NULL;
2231 else if (NULL != ch->dest_rel->uniq)
2233 /* we are waiting to for our 'SYNACK' to leave the queue, all done! */
2237 send_ack (ch, GNUNET_YES);
2244 * Handler for channel NACK messages.
2246 * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
2248 * @param ch Channel.
2251 GCCH_handle_nack (struct CadetChannel *ch)
2253 LOG (GNUNET_ERROR_TYPE_INFO,
2254 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2255 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_NACK_DEPRECATED), ch->gid, 0,
2256 GCCH_2s (ch), ch, "---", 0);
2258 send_client_nack (ch);
2264 * Handler for channel ack messages.
2266 * @param ch Channel.
2267 * @param msg Message.
2268 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2269 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2270 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2271 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2274 GCCH_handle_ack (struct CadetChannel *ch,
2275 const struct GNUNET_CADET_ChannelManageMessage *msg,
2278 LOG (GNUNET_ERROR_TYPE_INFO,
2279 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2280 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK), ch->gid, 0,
2281 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
2283 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2284 if (GNUNET_SYSERR == fwd)
2286 if (is_loopback (ch))
2288 /* It is a loopback channel after all... */
2292 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2295 channel_confirm (ch, !fwd);
2300 * Handler for channel destroy messages.
2302 * @param ch Channel to be destroyed of.
2303 * @param msg Message.
2304 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2305 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2306 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2307 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2310 GCCH_handle_destroy (struct CadetChannel *ch,
2311 const struct GNUNET_CADET_ChannelManageMessage *msg,
2314 struct CadetChannelReliability *rel;
2316 LOG (GNUNET_ERROR_TYPE_INFO,
2317 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2318 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY), ch->gid, 0,
2319 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
2321 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2322 if (GNUNET_SYSERR == fwd)
2324 if (is_loopback (ch))
2326 /* It is a loopback channel after all... */
2330 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2333 GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
2334 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
2336 /* Not for us (don't destroy twice a half-open loopback channel) */
2340 rel = fwd ? ch->dest_rel : ch->root_rel;
2341 if (0 == rel->n_recv)
2343 send_destroy (ch, GNUNET_YES);
2348 ch->destroy = GNUNET_YES;
2354 * Sends an already built message on a channel.
2356 * If the channel is on a loopback tunnel, notifies the appropriate destination
2359 * On a normal channel passes the message to the tunnel for encryption and
2360 * sending on a connection.
2362 * This function DOES NOT save the message for retransmission.
2364 * @param message Message to send. Function makes a copy of it.
2365 * @param ch Channel on which this message is transmitted.
2366 * @param fwd Is this a fwd message?
2367 * @param existing_copy This is a retransmission, don't save a copy.
2370 GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2371 struct CadetChannel *ch, int fwd,
2372 void *existing_copy)
2374 struct CadetChannelQueue *chq;
2380 type = ntohs (message->type);
2381 size = ntohs (message->size);
2386 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA:
2388 struct GNUNET_CADET_ChannelDataMessage *data_msg;
2389 struct GNUNET_MessageHeader *payload_msg;
2390 uint16_t payload_type;
2392 data_msg = (struct GNUNET_CADET_ChannelDataMessage *) message;
2393 data_id = ntohl (data_msg->mid);
2394 payload_msg = (struct GNUNET_MessageHeader *) &data_msg[1];
2395 payload_type = ntohs (payload_msg->type);
2396 strncpy (info, GC_m2s (payload_type), 31);
2400 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK:
2402 struct GNUNET_CADET_ChannelDataAckMessage *ack_msg;
2403 ack_msg = (struct GNUNET_CADET_ChannelDataAckMessage *) message;
2404 data_id = ntohl (ack_msg->mid);
2405 SPRINTF (info, "0x%010lX",
2406 (unsigned long int) ack_msg->futures);
2409 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
2411 struct GNUNET_CADET_ChannelCreateMessage *cc_msg;
2412 cc_msg = (struct GNUNET_CADET_ChannelCreateMessage *) message;
2413 SPRINTF (info, " 0x%08X", ntohl (cc_msg->chid.cn));
2416 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK:
2417 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_NACK_DEPRECATED:
2418 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2420 struct GNUNET_CADET_ChannelManageMessage *m_msg;
2421 m_msg = (struct GNUNET_CADET_ChannelManageMessage *) message;
2422 SPRINTF (info, " 0x%08X", ntohl (m_msg->chid.cn));
2428 LOG (GNUNET_ERROR_TYPE_INFO,
2429 "==> %s (%12s %4u) on chan %s (%p) %s [%5u]\n",
2430 GC_m2s (type), info, data_id,
2431 GCCH_2s (ch), ch, GC_f2s (fwd), size);
2433 if (GCT_is_loopback (ch->t))
2435 handle_loopback (ch, message, fwd);
2441 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA:
2442 if (GNUNET_YES == ch->reliable)
2444 chq = GNUNET_new (struct CadetChannelQueue);
2446 if (NULL == existing_copy)
2447 chq->copy = channel_save_copy (ch, message, fwd);
2450 chq->copy = (struct CadetReliableMessage *) existing_copy;
2451 if (NULL != chq->copy->chq)
2453 /* Last retransmission was queued but not yet sent!
2454 * This retransmission was scheduled by a ch_message_sent which
2455 * followed a very fast RTT, so the tiny delay made the
2456 * retransmission function to execute before the previous
2457 * retransmitted message even had a chance to leave the peer.
2458 * Cancel this message and wait until the pending
2459 * retransmission leaves the peer and ch_message_sent starts
2460 * the timer for the next one.
2463 LOG (GNUNET_ERROR_TYPE_DEBUG,
2464 " exisitng copy not yet transmitted!\n");
2467 LOG (GNUNET_ERROR_TYPE_DEBUG,
2468 " using existing copy: %p {r:%p q:%p t:%u}\n",
2470 chq->copy->rel, chq->copy->chq, chq->copy->type);
2472 LOG (GNUNET_ERROR_TYPE_DEBUG, " new chq: %p\n", chq);
2473 chq->copy->chq = chq;
2474 chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL,
2476 &ch_message_sent, chq);
2477 /* q itself is stored in copy */
2478 GNUNET_assert (NULL != chq->tq || GNUNET_NO != ch->destroy);
2482 fire_and_forget (message, ch, GNUNET_NO);
2487 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK:
2488 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
2489 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK:
2490 chq = GNUNET_new (struct CadetChannelQueue);
2492 chq->rel = fwd ? ch->root_rel : ch->dest_rel;
2493 if (NULL != chq->rel->uniq)
2495 if (NULL != chq->rel->uniq->tq)
2497 GCT_cancel (chq->rel->uniq->tq);
2498 /* ch_message_sent is called, freeing and NULLing uniq */
2499 GNUNET_break (NULL == chq->rel->uniq);
2504 GNUNET_free (chq->rel->uniq);
2508 chq->rel->uniq = chq;
2509 chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL, GNUNET_YES,
2510 &ch_message_sent, chq);
2511 if (NULL == chq->tq)
2514 chq->rel->uniq = NULL;
2515 GCT_debug (ch->t, GNUNET_ERROR_TYPE_ERROR);
2523 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2524 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_NACK_DEPRECATED:
2525 fire_and_forget (message, ch, GNUNET_YES);
2531 LOG (GNUNET_ERROR_TYPE_WARNING, "type %s unknown!\n", GC_m2s (type));
2532 fire_and_forget (message, ch, GNUNET_YES);
2538 * Get the static string for identification of the channel.
2540 * @param ch Channel.
2542 * @return Static string with the channel IDs.
2545 GCCH_2s (const struct CadetChannel *ch)
2547 static char buf[64];
2550 return "(NULL Channel)";
2553 "%s:%s gid:%X (%X / %X)",
2555 GNUNET_h2s (&ch->port),
2557 ntohl (ch->lid_root.channel_of_client),
2558 ntohl (ch->lid_dest.channel_of_client));