2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_statistics_service.h"
28 #include "cadet_protocol.h"
30 #include "gnunet-service-cadet_channel.h"
31 #include "gnunet-service-cadet_local.h"
32 #include "gnunet-service-cadet_tunnel.h"
33 #include "gnunet-service-cadet_peer.h"
35 #define LOG(level, ...) GNUNET_log_from(level,"cadet-chn",__VA_ARGS__)
37 #define CADET_RETRANSMIT_TIME GNUNET_TIME_relative_multiply(\
38 GNUNET_TIME_UNIT_MILLISECONDS, 250)
39 #define CADET_RETRANSMIT_MARGIN 4
43 * All the states a connection can be in.
45 enum CadetChannelState
48 * Uninitialized status, should never appear in operation.
53 * Connection create message sent, waiting for ACK.
58 * Connection confirmed, ready to carry traffic.
65 * Info holder for channel messages in queues.
67 struct CadetChannelQueue
72 struct CadetTunnelQueue *tq;
75 * Message type (DATA/DATA_ACK)
80 * Message copy (for DATAs, to start retransmission timer)
82 struct CadetReliableMessage *copy;
85 * Reliability (for DATA_ACKs, to access rel->ack_q)
87 struct CadetChannelReliability *rel;
92 * Info needed to retry a message in case it gets lost.
94 struct CadetReliableMessage
97 * Double linked list, FIFO style
99 struct CadetReliableMessage *next;
100 struct CadetReliableMessage *prev;
103 * Type of message (payload, channel management).
108 * Tunnel Reliability queue this message is in.
110 struct CadetChannelReliability *rel;
113 * ID of the message (ACK needed to free)
120 struct CadetChannelQueue *chq;
123 * When was this message issued (to calculate ACK delay)
125 struct GNUNET_TIME_Absolute timestamp;
127 /* struct GNUNET_CADET_Data with payload */
132 * Info about the traffic state for a client in a channel.
134 struct CadetChannelReliability
137 * Channel this is about.
139 struct CadetChannel *ch;
142 * DLL of messages sent and not yet ACK'd.
144 struct CadetReliableMessage *head_sent;
145 struct CadetReliableMessage *tail_sent;
148 * DLL of messages received out of order.
150 struct CadetReliableMessage *head_recv;
151 struct CadetReliableMessage *tail_recv;
159 * Next MID to use for outgoing traffic.
164 * Next MID expected for incoming traffic.
169 * Handle for queued unique data CREATE, DATA_ACK.
171 struct CadetChannelQueue *uniq;
174 * Can we send data to the client?
179 * Can the client send data to us?
184 * Task to resend/poll in case no ACK is received.
186 GNUNET_SCHEDULER_TaskIdentifier retry_task;
189 * Counter for exponential backoff.
191 struct GNUNET_TIME_Relative retry_timer;
194 * How long does it usually take to get an ACK.
196 struct GNUNET_TIME_Relative expected_delay;
201 * Struct containing all information regarding a channel to a remote client.
206 * Tunnel this channel is in.
208 struct CadetTunnel *t;
211 * Destination port of the channel.
216 * Global channel number ( < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
218 CADET_ChannelNumber gid;
221 * Local tunnel number for root (owner) client.
222 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI or 0 )
224 CADET_ChannelNumber lid_root;
227 * Local tunnel number for local destination clients (incoming number)
228 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV or 0).
230 CADET_ChannelNumber lid_dest;
235 enum CadetChannelState state;
238 * Is the tunnel bufferless (minimum latency)?
243 * Is the tunnel reliable?
248 * Last time the channel was used
250 struct GNUNET_TIME_Absolute timestamp;
253 * Client owner of the tunnel, if any
255 struct CadetClient *root;
258 * Client destination of the tunnel, if any.
260 struct CadetClient *dest;
263 * Flag to signal the destruction of the channel.
264 * If this is set GNUNET_YES the channel will be destroyed
265 * when the queue is empty.
270 * Total (reliable) messages pending ACK for this channel.
272 unsigned int pending_messages;
276 * Only present (non-NULL) at the owner of a tunnel.
278 struct CadetChannelReliability *root_rel;
282 * Only present (non-NULL) at the destination of a tunnel.
284 struct CadetChannelReliability *dest_rel;
289 /******************************************************************************/
290 /******************************* GLOBALS ***********************************/
291 /******************************************************************************/
294 * Global handle to the statistics service.
296 extern struct GNUNET_STATISTICS_Handle *stats;
299 * Local peer own ID (memory efficient handle).
301 extern GNUNET_PEER_Id myid;
304 /******************************************************************************/
305 /******************************** STATIC ***********************************/
306 /******************************************************************************/
310 * Destroy a reliable message after it has been acknowledged, either by
311 * direct mid ACK or bitfield. Updates the appropriate data structures and
312 * timers and frees all memory.
314 * @param copy Message that is no longer needed: remote peer got it.
315 * @param update_time Is the timing information relevant?
316 * If this message is ACK in a batch the timing information
317 * is skewed by the retransmission, count only for the
318 * retransmitted message.
320 * @return #GNUNET_YES if channel was destroyed as a result of the call,
321 * #GNUNET_NO otherwise.
324 rel_message_free (struct CadetReliableMessage *copy, int update_time);
327 * send a channel create message.
329 * @param ch Channel for which to send.
332 send_create (struct CadetChannel *ch);
335 * Confirm we got a channel create, FWD ack.
337 * @param ch The channel to confirm.
338 * @param fwd Should we send a FWD ACK? (going dest->root)
341 send_ack (struct CadetChannel *ch, int fwd);
346 * Test if the channel is loopback: both root and dest are on the local peer.
348 * @param ch Channel to test.
350 * @return #GNUNET_YES if channel is loopback, #GNUNET_NO otherwise.
353 is_loopback (const struct CadetChannel *ch)
356 return GCT_is_loopback (ch->t);
358 return (NULL != ch->root && NULL != ch->dest);
363 * Save a copy of the data message for later retransmission.
365 * @param msg Message to copy.
366 * @param mid Message ID.
367 * @param rel Reliability data for retransmission.
369 static struct CadetReliableMessage *
370 copy_message (const struct GNUNET_CADET_Data *msg, uint32_t mid,
371 struct CadetChannelReliability *rel)
373 struct CadetReliableMessage *copy;
376 size = ntohs (msg->header.size);
377 copy = GNUNET_malloc (sizeof (*copy) + size);
380 copy->type = GNUNET_MESSAGE_TYPE_CADET_DATA;
381 memcpy (©[1], msg, size);
387 * We have received a message out of order, or the client is not ready.
388 * Buffer it until we receive an ACK from the client or the missing
389 * message from the channel.
391 * @param msg Message to buffer (MUST be of type CADET_DATA).
392 * @param rel Reliability data to the corresponding direction.
395 add_buffered_data (const struct GNUNET_CADET_Data *msg,
396 struct CadetChannelReliability *rel)
398 struct CadetReliableMessage *copy;
399 struct CadetReliableMessage *prev;
402 mid = ntohl (msg->mid);
404 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
408 // FIXME do something better than O(n), although n < 64...
409 // FIXME start from the end (most messages are the latest ones)
410 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
412 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
413 if (prev->mid == mid)
415 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there!\n");
418 else if (GC_is_pid_bigger (prev->mid, mid))
420 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
421 copy = copy_message (msg, mid, rel);
422 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
427 copy = copy_message (msg, mid, rel);
428 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
429 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
430 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
435 * Add a destination client to a channel, initializing all data structures
436 * in the channel and the client.
438 * @param ch Channel to which add the destination.
439 * @param c Client which to add to the channel.
442 add_destination (struct CadetChannel *ch, struct CadetClient *c)
444 if (NULL != ch->dest)
450 /* Assign local id as destination */
451 ch->lid_dest = GML_get_next_chid (c);
453 /* Store in client's hashmap */
454 GML_channel_add (c, ch->lid_dest, ch);
456 GNUNET_break (NULL == ch->dest_rel);
457 ch->dest_rel = GNUNET_new (struct CadetChannelReliability);
458 ch->dest_rel->ch = ch;
459 ch->dest_rel->expected_delay.rel_value_us = 0;
460 ch->dest_rel->retry_timer = CADET_RETRANSMIT_TIME;
467 * Set options in a channel, extracted from a bit flag field.
469 * @param ch Channel to set options to.
470 * @param options Bit array in host byte order.
473 channel_set_options (struct CadetChannel *ch, uint32_t options)
475 ch->nobuffer = (options & GNUNET_CADET_OPTION_NOBUFFER) != 0 ?
476 GNUNET_YES : GNUNET_NO;
477 ch->reliable = (options & GNUNET_CADET_OPTION_RELIABLE) != 0 ?
478 GNUNET_YES : GNUNET_NO;
483 * Get a bit flag field with the options of a channel.
485 * @param ch Channel to get options from.
487 * @return Bit array in host byte order.
490 channel_get_options (struct CadetChannel *ch)
496 options |= GNUNET_CADET_OPTION_NOBUFFER;
498 options |= GNUNET_CADET_OPTION_RELIABLE;
505 * Notify a client that the channel is no longer valid.
507 * @param ch Channel that is destroyed.
508 * @param local_only Should we avoid sending it to other peers?
511 send_destroy (struct CadetChannel *ch, int local_only)
513 struct GNUNET_CADET_ChannelManage msg;
515 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
516 msg.header.size = htons (sizeof (msg));
517 msg.chid = htonl (ch->gid);
519 /* If root is not NULL, notify.
520 * If it's NULL, check lid_root. When a local destroy comes in, root
521 * is set to NULL but lid_root is left untouched. In this case, do nothing,
522 * the client is the one who requested the channel to be destroyed.
524 if (NULL != ch->root)
525 GML_send_channel_destroy (ch->root, ch->lid_root);
526 else if (0 == ch->lid_root && GNUNET_NO == local_only)
527 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
529 if (NULL != ch->dest)
530 GML_send_channel_destroy (ch->dest, ch->lid_dest);
531 else if (0 == ch->lid_dest && GNUNET_NO == local_only)
532 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
537 * Notify the destination client that a new incoming channel was created.
539 * @param ch Channel that was created.
542 send_client_create (struct CadetChannel *ch)
546 if (NULL == ch->dest)
550 opt |= GNUNET_YES == ch->reliable ? GNUNET_CADET_OPTION_RELIABLE : 0;
551 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_CADET_OPTION_NOBUFFER : 0;
552 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
553 GCT_get_destination (ch->t));
559 * Send data to a client.
561 * If the client is ready, send directly, otherwise buffer while listening
565 * @param msg Message.
566 * @param fwd Is this a fwd (root->dest) message?
569 send_client_data (struct CadetChannel *ch,
570 const struct GNUNET_CADET_Data *msg,
575 if (ch->dest_rel->client_ready)
577 GML_send_data (ch->dest, msg, ch->lid_dest);
578 ch->dest_rel->client_ready = GNUNET_NO;
579 ch->dest_rel->mid_recv++;
582 add_buffered_data (msg, ch->dest_rel);
586 if (ch->root_rel->client_ready)
588 GML_send_data (ch->root, msg, ch->lid_root);
589 ch->root_rel->client_ready = GNUNET_NO;
590 ch->root_rel->mid_recv++;
593 add_buffered_data (msg, ch->root_rel);
599 * Send a buffered message to the client, for in order delivery or
600 * as result of client ACK.
602 * @param ch Channel on which to empty the message buffer.
603 * @param c Client to send to.
604 * @param fwd Is this to send FWD data?.
607 send_client_buffered_data (struct CadetChannel *ch,
608 struct CadetClient *c,
611 struct CadetReliableMessage *copy;
612 struct CadetChannelReliability *rel;
614 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
615 rel = fwd ? ch->dest_rel : ch->root_rel;
616 if (GNUNET_NO == rel->client_ready)
618 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
622 copy = rel->head_recv;
623 /* We never buffer channel management messages */
626 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
628 struct GNUNET_CADET_Data *msg = (struct GNUNET_CADET_Data *) ©[1];
630 LOG (GNUNET_ERROR_TYPE_DEBUG, " have %u! now expecting %u\n",
631 copy->mid, rel->mid_recv + 1);
632 send_client_data (ch, msg, fwd);
634 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
635 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE RECV %p\n", copy);
637 GCCH_send_data_ack (ch, fwd);
641 LOG (GNUNET_ERROR_TYPE_DEBUG, " reliable && don't have %u, next is %u\n",
642 rel->mid_recv, copy->mid);
643 if (GNUNET_YES == ch->destroy)
645 /* We don't have the next data piece and the remote peer has closed the
646 * channel. We won't receive it anymore, so just destroy the channel.
647 * FIXME: wait some time to allow other connections to
648 * deliver missing messages
650 send_destroy (ch, GNUNET_YES);
655 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
660 * Allow a client to send more data.
662 * In case the client was already allowed to send data, do nothing.
665 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
668 send_client_ack (struct CadetChannel *ch, int fwd)
670 struct CadetChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
671 struct CadetClient *c = fwd ? ch->root : ch->dest;
675 GNUNET_break (GNUNET_NO != ch->destroy);
678 LOG (GNUNET_ERROR_TYPE_DEBUG,
679 " sending %s ack to client on channel %s\n",
680 GC_f2s (fwd), GCCH_2s (ch));
688 if (GNUNET_YES == rel->client_allowed)
690 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
693 rel->client_allowed = GNUNET_YES;
695 GML_send_ack (c, fwd ? ch->lid_root : ch->lid_dest);
700 * Notify the root that the destination rejected the channel.
702 * @param ch Rejected channel.
705 send_client_nack (struct CadetChannel *ch)
707 if (NULL == ch->root)
712 GML_send_channel_nack (ch->root, ch->lid_root);
717 * We haven't received an ACK after a certain time: restransmit the message.
719 * @param cls Closure (CadetChannelReliability with the message to restransmit)
720 * @param tc TaskContext.
723 channel_retransmit_message (void *cls,
724 const struct GNUNET_SCHEDULER_TaskContext *tc)
726 struct CadetChannelReliability *rel = cls;
727 struct CadetReliableMessage *copy;
728 struct CadetChannel *ch;
729 struct GNUNET_CADET_Data *payload;
732 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
733 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
737 copy = rel->head_sent;
744 payload = (struct GNUNET_CADET_Data *) ©[1];
745 fwd = (rel == ch->root_rel);
747 /* Message not found in the queue that we are going to use. */
748 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
750 GCCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
751 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
756 * We haven't received an Channel ACK after a certain time: resend the CREATE.
758 * @param cls Closure (CadetChannelReliability of the channel to recreate)
759 * @param tc TaskContext.
762 channel_recreate (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
764 struct CadetChannelReliability *rel = cls;
766 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
767 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
770 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RE-CREATE\n");
771 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
773 if (rel == rel->ch->root_rel)
775 send_create (rel->ch);
777 else if (rel == rel->ch->dest_rel)
779 send_ack (rel->ch, GNUNET_YES);
790 * Message has been sent: start retransmission timer.
792 * @param cls Closure (queue structure).
794 * @param q Queue handler (no longer valid).
795 * @param type Type of message.
796 * @param size Size of the message.
799 ch_message_sent (void *cls,
800 struct CadetTunnel *t,
801 struct CadetTunnelQueue *q,
802 uint16_t type, size_t size)
804 struct CadetChannelQueue *chq = cls;
805 struct CadetReliableMessage *copy = chq->copy;
806 struct CadetChannelReliability *rel;
808 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel message sent callback %s\n",
813 case GNUNET_MESSAGE_TYPE_CADET_DATA:
814 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT DATA MID %u\n", copy->mid);
815 GNUNET_assert (chq == copy->chq);
816 copy->timestamp = GNUNET_TIME_absolute_get ();
818 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
820 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! scheduling retry in 4 * %s\n",
821 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
823 if (0 != rel->expected_delay.rel_value_us)
825 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay != 0\n");
827 GNUNET_TIME_relative_multiply (rel->expected_delay,
828 CADET_RETRANSMIT_MARGIN);
832 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay reset\n");
833 rel->retry_timer = CADET_RETRANSMIT_TIME;
835 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! using delay %s\n",
836 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
839 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
840 &channel_retransmit_message, rel);
844 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! retry task %u\n", rel->retry_task);
850 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
851 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
852 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
853 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT %s\n", GC_m2s (chq->type));
855 GNUNET_assert (rel->uniq == chq);
858 if (CADET_CHANNEL_READY != rel->ch->state
859 && GNUNET_MESSAGE_TYPE_CADET_DATA_ACK != type
860 && GNUNET_NO == rel->ch->destroy)
862 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rel->retry_task);
863 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! STD BACKOFF %s\n",
864 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
866 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
867 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
868 &channel_recreate, rel);
881 * send a channel create message.
883 * @param ch Channel for which to send.
886 send_create (struct CadetChannel *ch)
888 struct GNUNET_CADET_ChannelCreate msgcc;
890 msgcc.header.size = htons (sizeof (msgcc));
891 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
892 msgcc.chid = htonl (ch->gid);
893 msgcc.port = htonl (ch->port);
894 msgcc.opt = htonl (channel_get_options (ch));
896 GCCH_send_prebuilt_message (&msgcc.header, ch, GNUNET_YES, NULL);
901 * Confirm we got a channel create or FWD ack.
903 * @param ch The channel to confirm.
904 * @param fwd Should we send a FWD ACK? (going dest->root)
907 send_ack (struct CadetChannel *ch, int fwd)
909 struct GNUNET_CADET_ChannelManage msg;
911 msg.header.size = htons (sizeof (msg));
912 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK);
913 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending channel %s ack for channel %s\n",
914 GC_f2s (fwd), GCCH_2s (ch));
916 msg.chid = htonl (ch->gid);
917 GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
922 * Send a message and don't keep any info about it: we won't need to cancel it
925 * @param msg Header of the message to fire away.
926 * @param ch Channel on which the message should go.
927 * @param force Is this a forced (undroppable) message?
930 fire_and_forget (const struct GNUNET_MessageHeader *msg,
931 struct CadetChannel *ch,
934 GNUNET_break (NULL == GCT_send_prebuilt_message (msg, ch->t, NULL,
940 * Notify that a channel create didn't succeed.
942 * @param ch The channel to reject.
945 send_nack (struct CadetChannel *ch)
947 struct GNUNET_CADET_ChannelManage msg;
949 msg.header.size = htons (sizeof (msg));
950 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
951 LOG (GNUNET_ERROR_TYPE_DEBUG,
952 " sending channel NACK for channel %s\n",
955 msg.chid = htonl (ch->gid);
956 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
961 * Destroy all reliable messages queued for a channel,
962 * during a channel destruction.
963 * Frees the reliability structure itself.
965 * @param rel Reliability data for a channel.
968 channel_rel_free_all (struct CadetChannelReliability *rel)
970 struct CadetReliableMessage *copy;
971 struct CadetReliableMessage *next;
976 for (copy = rel->head_recv; NULL != copy; copy = next)
979 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
980 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH RECV %p\n", copy);
981 GNUNET_break (NULL == copy->chq);
984 for (copy = rel->head_sent; NULL != copy; copy = next)
987 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
988 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH %p\n", copy);
989 if (NULL != copy->chq)
991 if (NULL != copy->chq->tq)
993 GCT_cancel (copy->chq->tq);
994 /* ch_message_sent will free copy->q */
998 GNUNET_free (copy->chq);
1004 if (NULL != rel->uniq && NULL != rel->uniq->tq)
1006 GCT_cancel (rel->uniq->tq);
1007 /* ch_message_sent is called freeing uniq */
1009 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1011 GNUNET_SCHEDULER_cancel (rel->retry_task);
1012 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1019 * Mark future messages as ACK'd.
1021 * @param rel Reliability data.
1022 * @param msg DataACK message with a bitfield of future ACK'd messages.
1025 channel_rel_free_sent (struct CadetChannelReliability *rel,
1026 const struct GNUNET_CADET_DataACK *msg)
1028 struct CadetReliableMessage *copy;
1029 struct CadetReliableMessage *next;
1036 bitfield = msg->futures;
1037 mid = ntohl (msg->mid);
1038 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable %u %llX\n", mid, bitfield);
1039 LOG (GNUNET_ERROR_TYPE_DEBUG, " rel %p, head %p\n", rel, rel->head_sent);
1040 for (i = 0, copy = rel->head_sent;
1041 i < 64 && NULL != copy && 0 != bitfield;
1044 LOG (GNUNET_ERROR_TYPE_DEBUG, " trying bit %u (mid %u)\n", i, mid + i + 1);
1046 if (0 == (bitfield & mask))
1049 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
1050 /* Bit was set, clear the bit from the bitfield */
1053 /* The i-th bit was set. Do we have that copy? */
1054 /* Skip copies with mid < target */
1055 target = mid + i + 1;
1056 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
1057 while (NULL != copy && GC_is_pid_bigger (target, copy->mid))
1060 /* Did we run out of copies? (previously freed, it's ok) */
1063 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
1067 /* Did we overshoot the target? (previously freed, it's ok) */
1068 if (GC_is_pid_bigger (copy->mid, target))
1070 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
1074 /* Now copy->mid == target, free it */
1076 GNUNET_break (GNUNET_YES != rel_message_free (copy, GNUNET_YES));
1079 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
1084 * Destroy a reliable message after it has been acknowledged, either by
1085 * direct mid ACK or bitfield. Updates the appropriate data structures and
1086 * timers and frees all memory.
1088 * @param copy Message that is no longer needed: remote peer got it.
1089 * @param update_time Is the timing information relevant?
1090 * If this message is ACK in a batch the timing information
1091 * is skewed by the retransmission, count only for the
1092 * retransmitted message.
1094 * @return #GNUNET_YES if channel was destroyed as a result of the call,
1095 * #GNUNET_NO otherwise.
1098 rel_message_free (struct CadetReliableMessage *copy, int update_time)
1100 struct CadetChannelReliability *rel;
1101 struct GNUNET_TIME_Relative time;
1104 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
1107 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
1108 if (0 == rel->expected_delay.rel_value_us)
1109 rel->expected_delay = time;
1112 rel->expected_delay.rel_value_us *= 7;
1113 rel->expected_delay.rel_value_us += time.rel_value_us;
1114 rel->expected_delay.rel_value_us /= 8;
1116 LOG (GNUNET_ERROR_TYPE_INFO, "!!! took %s, new delay %s\n",
1117 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO),
1118 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
1120 rel->retry_timer = rel->expected_delay;
1124 LOG (GNUNET_ERROR_TYPE_INFO, "!!! batch free, ignoring timing\n");
1126 rel->ch->pending_messages--;
1127 if (NULL != copy->chq)
1129 GCT_cancel (copy->chq->tq);
1130 /* copy->q is set to NULL by ch_message_sent */
1132 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
1133 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE %p\n", copy);
1136 if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
1138 GCCH_destroy (rel->ch);
1146 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
1148 * @param ch Channel to mark as ready.
1149 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
1152 channel_confirm (struct CadetChannel *ch, int fwd)
1154 struct CadetChannelReliability *rel;
1155 enum CadetChannelState oldstate;
1157 rel = fwd ? ch->root_rel : ch->dest_rel;
1160 GNUNET_break (GNUNET_NO != ch->destroy);
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel confirm %s %s\n",
1164 GC_f2s (fwd), GCCH_2s (ch));
1165 oldstate = ch->state;
1166 ch->state = CADET_CHANNEL_READY;
1168 if (CADET_CHANNEL_READY != oldstate || GNUNET_YES == is_loopback (ch))
1170 rel->client_ready = GNUNET_YES;
1171 rel->expected_delay = rel->retry_timer;
1172 LOG (GNUNET_ERROR_TYPE_DEBUG, " !! retry timer confirm %s\n",
1173 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer, GNUNET_NO));
1174 if (GCT_get_connections_buffer (ch->t) > 0 || GCT_is_loopback (ch->t))
1175 send_client_ack (ch, fwd);
1177 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1179 GNUNET_SCHEDULER_cancel (rel->retry_task);
1180 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1182 else if (NULL != rel->uniq)
1184 GCT_cancel (rel->uniq->tq);
1185 /* ch_message_sent will free and NULL uniq */
1187 else if (GNUNET_NO == is_loopback (ch))
1189 /* We SHOULD have been trying to retransmit this! */
1194 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
1195 if (GNUNET_YES == fwd)
1196 send_ack (ch, GNUNET_NO);
1201 * Save a copy to retransmit in case it gets lost.
1203 * Initializes all needed callbacks and timers.
1205 * @param ch Channel this message goes on.
1206 * @param msg Message to copy.
1207 * @param fwd Is this fwd traffic?
1209 static struct CadetReliableMessage *
1210 channel_save_copy (struct CadetChannel *ch,
1211 const struct GNUNET_MessageHeader *msg,
1214 struct CadetChannelReliability *rel;
1215 struct CadetReliableMessage *copy;
1220 rel = fwd ? ch->root_rel : ch->dest_rel;
1221 mid = rel->mid_send - 1;
1222 type = ntohs (msg->type);
1223 size = ntohs (msg->size);
1225 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u %s\n", mid, GC_m2s (type));
1226 copy = GNUNET_malloc (sizeof (struct CadetReliableMessage) + size);
1227 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", copy);
1231 memcpy (©[1], msg, size);
1232 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
1233 ch->pending_messages++;
1240 * Create a new channel.
1242 * @param t Tunnel this channel is in.
1243 * @param owner Client that owns the channel, NULL for foreign channels.
1244 * @param lid_root Local ID for root client.
1246 * @return A new initialized channel. NULL on error.
1248 static struct CadetChannel *
1249 channel_new (struct CadetTunnel *t,
1250 struct CadetClient *owner,
1251 CADET_ChannelNumber lid_root)
1253 struct CadetChannel *ch;
1255 ch = GNUNET_new (struct CadetChannel);
1257 ch->lid_root = lid_root;
1260 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
1264 ch->gid = GCT_get_next_chid (t);
1265 GML_channel_add (owner, lid_root, ch);
1267 GCT_add_channel (t, ch);
1274 * Handle a loopback message: call the appropriate handler for the message type.
1276 * @param ch Channel this message is on.
1277 * @param msgh Message header.
1278 * @param fwd Is this FWD traffic?
1281 handle_loopback (struct CadetChannel *ch,
1282 const struct GNUNET_MessageHeader *msgh,
1287 type = ntohs (msgh->type);
1288 LOG (GNUNET_ERROR_TYPE_DEBUG,
1289 "Loopback %s %s message!\n",
1290 GC_f2s (fwd), GC_m2s (type));
1294 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1295 /* Don't send hop ACK, wait for client to ACK */
1296 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SEND loopback %u (%u)\n",
1297 ntohl (((struct GNUNET_CADET_Data *) msgh)->mid), ntohs (msgh->size));
1298 GCCH_handle_data (ch, (struct GNUNET_CADET_Data *) msgh, fwd);
1301 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
1302 GCCH_handle_data_ack (ch, (struct GNUNET_CADET_DataACK *) msgh, fwd);
1305 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1306 GCCH_handle_create (ch->t,
1307 (struct GNUNET_CADET_ChannelCreate *) msgh);
1310 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
1311 GCCH_handle_ack (ch,
1312 (struct GNUNET_CADET_ChannelManage *) msgh,
1316 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
1317 GCCH_handle_nack (ch);
1320 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1321 GCCH_handle_destroy (ch,
1322 (struct GNUNET_CADET_ChannelManage *) msgh,
1327 GNUNET_break_op (0);
1328 LOG (GNUNET_ERROR_TYPE_DEBUG,
1329 "end-to-end message not known (%u)\n",
1330 ntohs (msgh->type));
1336 /******************************************************************************/
1337 /******************************** API ***********************************/
1338 /******************************************************************************/
1341 * Destroy a channel and free all resources.
1343 * @param ch Channel to destroy.
1346 GCCH_destroy (struct CadetChannel *ch)
1348 struct CadetClient *c;
1349 struct CadetTunnel *t;
1353 if (2 == ch->destroy)
1354 return; /* recursive call */
1357 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
1358 GCT_2s (ch->t), ch->gid);
1364 GML_channel_remove (c, ch->lid_root, ch);
1370 GML_channel_remove (c, ch->lid_dest, ch);
1373 channel_rel_free_all (ch->root_rel);
1374 channel_rel_free_all (ch->dest_rel);
1377 GCT_remove_channel (t, ch);
1378 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
1381 GCT_destroy_if_empty (t);
1386 * Get the channel's public ID.
1388 * @param ch Channel.
1390 * @return ID used to identify the channel with the remote peer.
1393 GCCH_get_id (const struct CadetChannel *ch)
1400 * Get the channel tunnel.
1402 * @param ch Channel to get the tunnel from.
1404 * @return tunnel of the channel.
1406 struct CadetTunnel *
1407 GCCH_get_tunnel (const struct CadetChannel *ch)
1414 * Get free buffer space towards the client on a specific channel.
1416 * @param ch Channel.
1417 * @param fwd Is query about FWD traffic?
1419 * @return Free buffer space [0 - 64]
1422 GCCH_get_buffer (struct CadetChannel *ch, int fwd)
1424 struct CadetChannelReliability *rel;
1426 rel = fwd ? ch->dest_rel : ch->root_rel;
1428 /* If rel is NULL it means that the end is not yet created,
1429 * most probably is a loopback channel at the point of sending
1430 * the ChannelCreate to itself.
1435 return (64 - rel->n_recv);
1440 * Get flow control status of end point: is client allow to send?
1442 * @param ch Channel.
1443 * @param fwd Is query about FWD traffic? (Request root status).
1445 * @return #GNUNET_YES if client is allowed to send us data.
1448 GCCH_get_allowed (struct CadetChannel *ch, int fwd)
1450 struct CadetChannelReliability *rel;
1452 rel = fwd ? ch->root_rel : ch->dest_rel;
1456 /* Probably shutting down: root/dest NULL'ed to mark disconnection */
1457 GNUNET_break (GNUNET_NO != ch->destroy);
1461 return rel->client_allowed;
1466 * Is the root client for this channel on this peer?
1468 * @param ch Channel.
1469 * @param fwd Is this for fwd traffic?
1471 * @return #GNUNET_YES in case it is.
1474 GCCH_is_origin (struct CadetChannel *ch, int fwd)
1476 struct CadetClient *c;
1478 c = fwd ? ch->root : ch->dest;
1484 * Is the destination 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_terminal (struct CadetChannel *ch, int fwd)
1494 struct CadetClient *c;
1496 c = fwd ? ch->dest : ch->root;
1502 * Send an end-to-end ACK message for the most recent in-sequence payload.
1504 * If channel is not reliable, do nothing.
1506 * @param ch Channel this is about.
1507 * @param fwd Is for FWD traffic? (ACK dest->owner)
1510 GCCH_send_data_ack (struct CadetChannel *ch, int fwd)
1512 struct GNUNET_CADET_DataACK msg;
1513 struct CadetChannelReliability *rel;
1514 struct CadetReliableMessage *copy;
1519 if (GNUNET_NO == ch->reliable)
1522 rel = fwd ? ch->dest_rel : ch->root_rel;
1523 ack = rel->mid_recv - 1;
1525 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_DATA_ACK);
1526 msg.header.size = htons (sizeof (msg));
1527 msg.chid = htonl (ch->gid);
1528 msg.mid = htonl (ack);
1531 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1533 if (copy->type != GNUNET_MESSAGE_TYPE_CADET_DATA)
1535 LOG (GNUNET_ERROR_TYPE_DEBUG, " Type %s, expected DATA\n",
1536 GC_m2s (copy->type));
1539 GNUNET_assert (copy->mid > ack);
1540 delta = copy->mid - (ack + 1);
1543 mask = 0x1LL << delta;
1544 msg.futures |= mask;
1545 LOG (GNUNET_ERROR_TYPE_DEBUG,
1546 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1547 copy->mid, delta, mask, msg.futures);
1549 LOG (GNUNET_ERROR_TYPE_INFO, "===> DATA_ACK for %u + %llX\n",
1552 GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1553 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1558 * Allow a client to send us more data, in case it was choked.
1560 * @param ch Channel.
1561 * @param fwd Is this about FWD traffic? (Root client).
1564 GCCH_allow_client (struct CadetChannel *ch, int fwd)
1566 struct CadetChannelReliability *rel;
1567 unsigned int buffer;
1569 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1571 if (CADET_CHANNEL_READY != ch->state)
1573 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel not ready yet!\n");
1577 if (GNUNET_YES == ch->reliable)
1579 rel = fwd ? ch->root_rel : ch->dest_rel;
1582 GNUNET_break (GNUNET_NO != ch->destroy);
1585 if (NULL != rel->head_sent)
1587 if (64 <= rel->mid_send - rel->head_sent->mid)
1589 LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
1594 LOG (GNUNET_ERROR_TYPE_DEBUG, " gap ok: %u - %u\n",
1595 rel->head_sent->mid, rel->mid_send);
1596 struct CadetReliableMessage *aux;
1597 for (aux = rel->head_sent; NULL != aux; aux = aux->next)
1599 LOG (GNUNET_ERROR_TYPE_DEBUG, " - sent MID %u\n", aux->mid);
1605 LOG (GNUNET_ERROR_TYPE_DEBUG, " head sent is NULL\n");
1609 if (is_loopback (ch))
1610 buffer = GCCH_get_buffer (ch, fwd);
1612 buffer = GCT_get_connections_buffer (ch->t);
1616 LOG (GNUNET_ERROR_TYPE_DEBUG, " no buffer space.\n");
1620 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space %u, allowing\n", buffer);
1621 send_client_ack (ch, fwd);
1628 * @param ch Channel.
1631 GCCH_debug (struct CadetChannel *ch)
1635 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1638 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1639 GCT_2s (ch->t), ch->gid, ch);
1640 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1641 ch->root, ch->root_rel);
1642 if (NULL != ch->root)
1644 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1645 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1646 ch->root_rel->client_ready ? "YES" : "NO");
1647 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1649 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1650 ch->dest, ch->dest_rel);
1651 if (NULL != ch->dest)
1653 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1654 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1655 ch->dest_rel->client_ready ? "YES" : "NO");
1656 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1662 * Handle an ACK given by a client.
1664 * Mark client as ready and send him any buffered data we could have for him.
1666 * @param ch Channel.
1667 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1670 GCCH_handle_local_ack (struct CadetChannel *ch, int fwd)
1672 struct CadetChannelReliability *rel;
1673 struct CadetClient *c;
1675 rel = fwd ? ch->dest_rel : ch->root_rel;
1676 c = fwd ? ch->dest : ch->root;
1678 rel->client_ready = GNUNET_YES;
1679 send_client_buffered_data (ch, c, fwd);
1681 if (GNUNET_YES == ch->destroy && 0 == rel->n_recv)
1683 send_destroy (ch, GNUNET_YES);
1686 /* if loopback is marked for destruction, no need to ACK to the other peer,
1687 * it requested the destruction and is already gone, therefore, else if.
1689 else if (is_loopback (ch))
1691 unsigned int buffer;
1693 buffer = GCCH_get_buffer (ch, fwd);
1695 GCCH_allow_client (ch, fwd);
1699 GCT_send_connection_acks (ch->t);
1704 * Handle data given by a client.
1706 * Check whether the client is allowed to send in this tunnel, save if channel
1707 * is reliable and send an ACK to the client if there is still buffer space
1710 * @param ch Channel.
1711 * @param c Client which sent the data.
1712 * @param message Message.
1713 * @param fwd Is this a FWD data?
1715 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1718 GCCH_handle_local_data (struct CadetChannel *ch,
1719 struct CadetClient *c,
1720 struct GNUNET_MessageHeader *message,
1723 struct CadetChannelReliability *rel;
1724 struct GNUNET_CADET_Data *payload;
1725 size_t size = ntohs (message->size);
1726 uint16_t p2p_size = sizeof(struct GNUNET_CADET_Data) + size;
1727 unsigned char cbuf[p2p_size];
1729 /* Is the client in the channel? */
1736 GNUNET_break_op (0);
1737 return GNUNET_SYSERR;
1740 rel = fwd ? ch->root_rel : ch->dest_rel;
1742 if (GNUNET_NO == rel->client_allowed)
1744 GNUNET_break_op (0);
1745 return GNUNET_SYSERR;
1748 rel->client_allowed = GNUNET_NO;
1750 /* Ok, everything is correct, send the message. */
1751 payload = (struct GNUNET_CADET_Data *) cbuf;
1752 payload->mid = htonl (rel->mid_send);
1754 memcpy (&payload[1], message, size);
1755 payload->header.size = htons (p2p_size);
1756 payload->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_DATA);
1757 payload->chid = htonl (ch->gid);
1758 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1759 GCCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1761 if (is_loopback (ch))
1763 if (GCCH_get_buffer (ch, fwd) > 0)
1764 GCCH_allow_client (ch, fwd);
1769 if (GCT_get_connections_buffer (ch->t) > 0)
1771 GCCH_allow_client (ch, fwd);
1779 * Handle a channel destroy requested by a client.
1781 * Destroy the channel and the tunnel in case this was the last channel.
1783 * @param ch Channel.
1784 * @param c Client that requested the destruction (to avoid notifying him).
1785 * @param is_root Is the request coming from root?
1788 GCCH_handle_local_destroy (struct CadetChannel *ch,
1789 struct CadetClient *c,
1792 ch->destroy = GNUNET_YES;
1793 /* Cleanup after the tunnel */
1794 if (GNUNET_NO == is_root && c == ch->dest)
1796 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1797 GML_client_delete_channel (c, ch, ch->lid_dest);
1800 if (GNUNET_YES == is_root && c == ch->root)
1802 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1803 GML_client_delete_channel (c, ch, ch->lid_root);
1807 send_destroy (ch, GNUNET_NO);
1808 if (0 == ch->pending_messages)
1814 * Handle a channel create requested by a client.
1816 * Create the channel and the tunnel in case this was the first0 channel.
1818 * @param c Client that requested the creation (will be the root).
1819 * @param msg Create Channel message.
1821 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1824 GCCH_handle_local_create (struct CadetClient *c,
1825 struct GNUNET_CADET_ChannelMessage *msg)
1827 struct CadetChannel *ch;
1828 struct CadetTunnel *t;
1829 struct CadetPeer *peer;
1830 CADET_ChannelNumber chid;
1832 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1833 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1834 chid = ntohl (msg->channel_id);
1836 /* Sanity check for duplicate channel IDs */
1837 if (NULL != GML_channel_get (c, chid))
1840 return GNUNET_SYSERR;
1843 peer = GCP_get (&msg->peer);
1844 GCP_add_tunnel (peer);
1845 t = GCP_get_tunnel (peer);
1847 if (GCP_get_short_id (peer) == myid)
1849 GCT_change_cstate (t, CADET_TUNNEL_READY);
1853 /* FIXME change to a tunnel API, eliminate ch <-> peer connection */
1857 /* Create channel */
1858 ch = channel_new (t, c, chid);
1862 return GNUNET_SYSERR;
1864 ch->port = ntohl (msg->port);
1865 channel_set_options (ch, ntohl (msg->opt));
1867 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1868 ch->root_rel = GNUNET_new (struct CadetChannelReliability);
1869 ch->root_rel->ch = ch;
1870 ch->root_rel->retry_timer = CADET_RETRANSMIT_TIME;
1871 ch->root_rel->expected_delay.rel_value_us = 0;
1873 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GCCH_2s (ch));
1882 * Handler for cadet network payload traffic.
1884 * @param ch Channel for the message.
1885 * @param msg Unencryted data message.
1886 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1887 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1888 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1889 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1892 GCCH_handle_data (struct CadetChannel *ch,
1893 const struct GNUNET_CADET_Data *msg,
1896 struct CadetChannelReliability *rel;
1897 struct CadetClient *c;
1900 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1901 if (GNUNET_SYSERR == fwd)
1903 if (is_loopback (ch))
1905 /* It is a loopback channel after all... */
1909 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1912 /* Initialize FWD/BCK data */
1913 c = fwd ? ch->dest : ch->root;
1914 rel = fwd ? ch->dest_rel : ch->root_rel;
1918 GNUNET_break (GNUNET_NO != ch->destroy);
1922 if (CADET_CHANNEL_READY != ch->state)
1924 if (GNUNET_NO == fwd)
1926 /* If we are the root, this means the other peer has sent traffic before
1927 * receiving our ACK. Even if the SYNACK goes missing, no traffic should
1928 * be sent before the ACK.
1930 GNUNET_break_op (0);
1933 /* If we are the dest, this means that the SYNACK got to the root but
1934 * the ACK went missing. Treat this as an ACK.
1936 channel_confirm (ch, GNUNET_NO);
1939 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1941 mid = ntohl (msg->mid);
1942 LOG (GNUNET_ERROR_TYPE_INFO, "<=== DATA %u %s on channel %s\n",
1943 mid, GC_f2s (fwd), GCCH_2s (ch));
1945 if (GNUNET_NO == ch->reliable ||
1946 ( !GC_is_pid_bigger (rel->mid_recv, mid) &&
1947 GC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1949 LOG (GNUNET_ERROR_TYPE_DEBUG, "RECV %u (%u)\n",
1950 mid, ntohs (msg->header.size));
1951 if (GNUNET_YES == ch->reliable)
1953 /* Is this the exact next expected messasge? */
1954 if (mid == rel->mid_recv)
1956 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected, sending to client\n");
1957 send_client_data (ch, msg, fwd);
1961 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1962 add_buffered_data (msg, rel);
1967 /* Tunnel is unreliable: send to clients directly */
1968 /* FIXME: accept Out Of Order traffic */
1969 rel->mid_recv = mid + 1;
1970 send_client_data (ch, msg, fwd);
1975 GNUNET_break_op (GC_is_pid_bigger (rel->mid_recv, mid));
1976 LOG (GNUNET_ERROR_TYPE_WARNING,
1977 "MID %u on channel %s not expected (window: %u - %u). Dropping!\n",
1978 mid, GCCH_2s (ch), rel->mid_recv, rel->mid_recv + 63);
1981 GCCH_send_data_ack (ch, fwd);
1986 * Handler for cadet network traffic end-to-end ACKs.
1988 * @param ch Channel on which we got this message.
1989 * @param msg Data message.
1990 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1991 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1992 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1993 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1996 GCCH_handle_data_ack (struct CadetChannel *ch,
1997 const struct GNUNET_CADET_DataACK *msg,
2000 struct CadetChannelReliability *rel;
2001 struct CadetReliableMessage *copy;
2002 struct CadetReliableMessage *next;
2006 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2007 if (GNUNET_SYSERR == fwd)
2009 if (is_loopback (ch))
2011 /* It is a loopback channel after all... */
2015 /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
2016 fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
2019 ack = ntohl (msg->mid);
2020 LOG (GNUNET_ERROR_TYPE_INFO, "<=== %s ACK %u + %llX\n",
2021 GC_f2s (fwd), ack, msg->futures);
2023 if (GNUNET_YES == fwd)
2033 GNUNET_break_op (GNUNET_NO != ch->destroy);
2037 /* Free ACK'd copies: no need to retransmit those anymore FIXME refactor */
2038 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
2040 if (GC_is_pid_bigger (copy->mid, ack))
2042 LOG (GNUNET_ERROR_TYPE_DEBUG, " head %u, out!\n", copy->mid);
2043 channel_rel_free_sent (rel, msg);
2047 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %u\n", copy->mid);
2049 if (GNUNET_YES == rel_message_free (copy, GNUNET_YES))
2053 /* ACK client if needed and possible */
2054 GCCH_allow_client (ch, fwd);
2056 /* If some message was free'd, update the retransmission delay */
2057 if (GNUNET_YES == work)
2059 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
2061 GNUNET_SCHEDULER_cancel (rel->retry_task);
2062 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2063 if (NULL != rel->head_sent && NULL == rel->head_sent->chq)
2065 struct GNUNET_TIME_Absolute new_target;
2066 struct GNUNET_TIME_Relative delay;
2068 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
2069 CADET_RETRANSMIT_MARGIN);
2070 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
2072 delay = GNUNET_TIME_absolute_get_remaining (new_target);
2074 GNUNET_SCHEDULER_add_delayed (delay,
2075 &channel_retransmit_message,
2081 /* Work was done but no task was pending? Shouldn't happen! */
2089 * Handler for channel create messages.
2091 * Does not have fwd parameter because it's always 'FWD': channel is incoming.
2093 * @param t Tunnel this channel will be in.
2094 * @param msg Channel crate message.
2096 struct CadetChannel *
2097 GCCH_handle_create (struct CadetTunnel *t,
2098 const struct GNUNET_CADET_ChannelCreate *msg)
2100 CADET_ChannelNumber chid;
2101 struct CadetChannel *ch;
2102 struct CadetClient *c;
2105 chid = ntohl (msg->chid);
2106 ch = GCT_get_channel (t, chid);
2109 /* Create channel */
2110 ch = channel_new (t, NULL, 0);
2112 channel_set_options (ch, ntohl (msg->opt));
2113 new_channel = GNUNET_YES;
2117 new_channel = GNUNET_NO;
2120 if (GNUNET_YES == new_channel || GCT_is_loopback (t))
2122 /* Find a destination client */
2123 ch->port = ntohl (msg->port);
2124 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", ch->port);
2125 c = GML_client_get_by_port (ch->port);
2128 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
2129 if (is_loopback (ch))
2131 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback: destroy on handler\n");
2136 LOG (GNUNET_ERROR_TYPE_DEBUG, " not loopback: destroy now\n");
2144 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
2147 add_destination (ch, c);
2148 if (GNUNET_YES == ch->reliable)
2149 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
2151 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
2153 send_client_create (ch);
2154 ch->state = CADET_CHANNEL_SENT;
2158 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate create channel\n");
2159 if (GNUNET_SCHEDULER_NO_TASK != ch->dest_rel->retry_task)
2161 LOG (GNUNET_ERROR_TYPE_DEBUG, " clearing retry task\n");
2162 /* we were waiting to re-send our 'SYNACK', wait no more! */
2163 GNUNET_SCHEDULER_cancel (ch->dest_rel->retry_task);
2164 ch->dest_rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2166 else if (NULL != ch->dest_rel->uniq)
2168 /* we are waiting to for our 'SYNACK' to leave the queue, all done! */
2172 send_ack (ch, GNUNET_YES);
2179 * Handler for channel NACK messages.
2181 * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
2183 * @param ch Channel.
2186 GCCH_handle_nack (struct CadetChannel *ch)
2188 send_client_nack (ch);
2194 * Handler for channel ack messages.
2196 * @param ch Channel.
2197 * @param msg Message.
2198 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2199 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2200 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2201 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2204 GCCH_handle_ack (struct CadetChannel *ch,
2205 const struct GNUNET_CADET_ChannelManage *msg,
2208 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2209 if (GNUNET_SYSERR == fwd)
2211 if (is_loopback (ch))
2213 /* It is a loopback channel after all... */
2217 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2220 channel_confirm (ch, !fwd);
2225 * Handler for channel destroy messages.
2227 * @param ch Channel to be destroyed of.
2228 * @param msg Message.
2229 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2230 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2231 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2232 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2235 GCCH_handle_destroy (struct CadetChannel *ch,
2236 const struct GNUNET_CADET_ChannelManage *msg,
2239 struct CadetChannelReliability *rel;
2241 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2242 if (GNUNET_SYSERR == fwd)
2244 if (is_loopback (ch))
2246 /* It is a loopback channel after all... */
2250 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2254 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
2256 /* Not for us (don't destroy twice a half-open loopback channel) */
2260 rel = fwd ? ch->dest_rel : ch->root_rel;
2261 if (0 == rel->n_recv)
2263 send_destroy (ch, GNUNET_YES);
2268 ch->destroy = GNUNET_YES;
2274 * Sends an already built message on a channel.
2276 * If the channel is on a loopback tunnel, notifies the appropriate destination
2279 * On a normal channel passes the message to the tunnel for encryption and
2280 * sending on a connection.
2282 * This function DOES NOT save the message for retransmission.
2284 * @param message Message to send. Function makes a copy of it.
2285 * @param ch Channel on which this message is transmitted.
2286 * @param fwd Is this a fwd message?
2287 * @param existing_copy This is a retransmission, don't save a copy.
2290 GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2291 struct CadetChannel *ch, int fwd,
2292 void *existing_copy)
2294 struct CadetChannelQueue *chq;
2297 type = ntohs (message->type);
2298 LOG (GNUNET_ERROR_TYPE_INFO, "===> %s %s on channel %s\n",
2299 GC_m2s (type), GC_f2s (fwd), GCCH_2s (ch));
2301 if (GCT_is_loopback (ch->t))
2303 handle_loopback (ch, message, fwd);
2309 struct GNUNET_CADET_Data *payload;
2310 case GNUNET_MESSAGE_TYPE_CADET_DATA:
2312 payload = (struct GNUNET_CADET_Data *) message;
2313 LOG (GNUNET_ERROR_TYPE_INFO, "===> %s %u\n",
2314 GC_m2s (type), ntohl (payload->mid));
2315 if (GNUNET_YES == ch->reliable)
2317 chq = GNUNET_new (struct CadetChannelQueue);
2319 if (NULL == existing_copy)
2320 chq->copy = channel_save_copy (ch, message, fwd);
2323 chq->copy = (struct CadetReliableMessage *) existing_copy;
2324 if (NULL != chq->copy->chq)
2326 /* Last retransmission was queued but not yet sent!
2327 * This retransmission was scheduled by a ch_message_sent which
2328 * followed a very fast RTT, so the tiny delay made the
2329 * retransmission function to execute before the previous
2330 * retransmitted message even had a chance to leave the peer.
2331 * Cancel this message and wait until the pending
2332 * retransmission leaves the peer and ch_message_sent starts
2333 * the timer for the next one.
2336 LOG (GNUNET_ERROR_TYPE_DEBUG,
2337 " exisitng copy not yet transmitted!\n");
2340 LOG (GNUNET_ERROR_TYPE_DEBUG,
2341 " using existing copy: %p {r:%p q:%p t:%u}\n",
2343 chq->copy->rel, chq->copy->chq, chq->copy->type);
2345 LOG (GNUNET_ERROR_TYPE_DEBUG, " new chq: %p\n", chq);
2346 chq->copy->chq = chq;
2347 chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL,
2348 NULL != existing_copy,
2349 &ch_message_sent, chq);
2350 /* q itself is stored in copy */
2351 GNUNET_assert (NULL != chq->tq || GNUNET_NO != ch->destroy);
2355 fire_and_forget (message, ch, GNUNET_NO);
2360 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
2361 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
2362 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
2363 chq = GNUNET_new (struct CadetChannelQueue);
2365 chq->rel = fwd ? ch->root_rel : ch->dest_rel;
2366 if (NULL != chq->rel->uniq)
2368 if (NULL != chq->rel->uniq->tq)
2370 GCT_cancel (chq->rel->uniq->tq);
2371 /* ch_message_sent is called, freeing and NULLing uniq */
2372 GNUNET_break (NULL == chq->rel->uniq);
2377 GNUNET_free (chq->rel->uniq);
2381 chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL, GNUNET_YES,
2382 &ch_message_sent, chq);
2383 if (NULL == chq->tq)
2386 GCT_debug (ch->t, GNUNET_ERROR_TYPE_ERROR);
2391 chq->rel->uniq = chq;
2395 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2396 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
2397 fire_and_forget (message, ch, GNUNET_YES);
2403 LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s unknown!\n", GC_m2s (type));
2404 fire_and_forget (message, ch, GNUNET_YES);
2410 * Get the static string for identification of the channel.
2412 * @param ch Channel.
2414 * @return Static string with the channel IDs.
2417 GCCH_2s (const struct CadetChannel *ch)
2419 static char buf[64];
2422 return "(NULL Channel)";
2424 sprintf (buf, "%s:%u gid:%X (%X / %X)",
2425 GCT_2s (ch->t), ch->port, ch->gid, ch->lid_root, ch->lid_dest);