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_Data with payload */
133 * Info about the traffic state for a client in a channel.
135 struct CadetChannelReliability
138 * Channel this is about.
140 struct CadetChannel *ch;
143 * DLL of messages sent and not yet ACK'd.
145 struct CadetReliableMessage *head_sent;
146 struct CadetReliableMessage *tail_sent;
149 * DLL of messages received out of order.
151 struct CadetReliableMessage *head_recv;
152 struct CadetReliableMessage *tail_recv;
160 * Next MID to use for outgoing traffic.
165 * Next MID expected for incoming traffic.
170 * Handle for queued unique data CREATE, DATA_ACK.
172 struct CadetChannelQueue *uniq;
175 * Can we send data to the client?
180 * Can the client send data to us?
185 * Task to resend/poll in case no ACK is received.
187 struct GNUNET_SCHEDULER_Task * retry_task;
190 * Counter for exponential backoff.
192 struct GNUNET_TIME_Relative retry_timer;
195 * How long does it usually take to get an ACK.
197 struct GNUNET_TIME_Relative expected_delay;
202 * Struct containing all information regarding a channel to a remote client.
207 * Tunnel this channel is in.
209 struct CadetTunnel *t;
212 * Destination port of the channel.
217 * Global channel number ( < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
219 CADET_ChannelNumber gid;
222 * Local tunnel number for root (owner) client.
223 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI or 0 )
225 CADET_ChannelNumber lid_root;
228 * Local tunnel number for local destination clients (incoming number)
229 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV or 0).
231 CADET_ChannelNumber lid_dest;
236 enum CadetChannelState state;
239 * Is the tunnel bufferless (minimum latency)?
244 * Is the tunnel reliable?
249 * Last time the channel was used
251 struct GNUNET_TIME_Absolute timestamp;
254 * Client owner of the tunnel, if any
256 struct CadetClient *root;
259 * Client destination of the tunnel, if any.
261 struct CadetClient *dest;
264 * Flag to signal the destruction of the channel.
265 * If this is set GNUNET_YES the channel will be destroyed
266 * when the queue is empty.
271 * Total (reliable) messages pending ACK for this channel.
273 unsigned int pending_messages;
277 * Only present (non-NULL) at the owner of a tunnel.
279 struct CadetChannelReliability *root_rel;
283 * Only present (non-NULL) at the destination of a tunnel.
285 struct CadetChannelReliability *dest_rel;
290 /******************************************************************************/
291 /******************************* GLOBALS ***********************************/
292 /******************************************************************************/
295 * Global handle to the statistics service.
297 extern struct GNUNET_STATISTICS_Handle *stats;
300 * Local peer own ID (memory efficient handle).
302 extern GNUNET_PEER_Id myid;
305 /******************************************************************************/
306 /******************************** STATIC ***********************************/
307 /******************************************************************************/
311 * Destroy a reliable message after it has been acknowledged, either by
312 * direct mid ACK or bitfield. Updates the appropriate data structures and
313 * timers and frees all memory.
315 * @param copy Message that is no longer needed: remote peer got it.
316 * @param update_time Is the timing information relevant?
317 * If this message is ACK in a batch the timing information
318 * is skewed by the retransmission, count only for the
319 * retransmitted message.
321 * @return #GNUNET_YES if channel was destroyed as a result of the call,
322 * #GNUNET_NO otherwise.
325 rel_message_free (struct CadetReliableMessage *copy, int update_time);
328 * send a channel create message.
330 * @param ch Channel for which to send.
333 send_create (struct CadetChannel *ch);
336 * Confirm we got a channel create, FWD ack.
338 * @param ch The channel to confirm.
339 * @param fwd Should we send a FWD ACK? (going dest->root)
342 send_ack (struct CadetChannel *ch, int fwd);
347 * Test if the channel is loopback: both root and dest are on the local peer.
349 * @param ch Channel to test.
351 * @return #GNUNET_YES if channel is loopback, #GNUNET_NO otherwise.
354 is_loopback (const struct CadetChannel *ch)
357 return GCT_is_loopback (ch->t);
359 return (NULL != ch->root && NULL != ch->dest);
364 * Save a copy of the data message for later retransmission.
366 * @param msg Message to copy.
367 * @param mid Message ID.
368 * @param rel Reliability data for retransmission.
370 static struct CadetReliableMessage *
371 copy_message (const struct GNUNET_CADET_Data *msg, uint32_t mid,
372 struct CadetChannelReliability *rel)
374 struct CadetReliableMessage *copy;
377 size = ntohs (msg->header.size);
378 copy = GNUNET_malloc (sizeof (*copy) + size);
381 copy->type = GNUNET_MESSAGE_TYPE_CADET_DATA;
382 memcpy (©[1], msg, size);
388 * We have received a message out of order, or the client is not ready.
389 * Buffer it until we receive an ACK from the client or the missing
390 * message from the channel.
392 * @param msg Message to buffer (MUST be of type CADET_DATA).
393 * @param rel Reliability data to the corresponding direction.
396 add_buffered_data (const struct GNUNET_CADET_Data *msg,
397 struct CadetChannelReliability *rel)
399 struct CadetReliableMessage *copy;
400 struct CadetReliableMessage *prev;
403 mid = ntohl (msg->mid);
405 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data MID %u (%u)\n",
410 // FIXME do something better than O(n), although n < 64...
411 // FIXME start from the end (most messages are the latest ones)
412 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
414 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
415 if (prev->mid == mid)
417 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there!\n");
421 else if (GC_is_pid_bigger (prev->mid, mid))
423 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
424 copy = copy_message (msg, mid, rel);
425 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
430 copy = copy_message (msg, mid, rel);
431 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail! (now: %u)\n", rel->n_recv);
432 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
433 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
438 * Add a destination client to a channel, initializing all data structures
439 * in the channel and the client.
441 * @param ch Channel to which add the destination.
442 * @param c Client which to add to the channel.
445 add_destination (struct CadetChannel *ch, struct CadetClient *c)
447 if (NULL != ch->dest)
453 /* Assign local id as destination */
454 ch->lid_dest = GML_get_next_chid (c);
456 /* Store in client's hashmap */
457 GML_channel_add (c, ch->lid_dest, ch);
459 GNUNET_break (NULL == ch->dest_rel);
460 ch->dest_rel = GNUNET_new (struct CadetChannelReliability);
461 ch->dest_rel->ch = ch;
462 ch->dest_rel->expected_delay.rel_value_us = 0;
463 ch->dest_rel->retry_timer = CADET_RETRANSMIT_TIME;
470 * Set options in a channel, extracted from a bit flag field.
472 * @param ch Channel to set options to.
473 * @param options Bit array in host byte order.
476 channel_set_options (struct CadetChannel *ch, uint32_t options)
478 ch->nobuffer = (options & GNUNET_CADET_OPTION_NOBUFFER) != 0 ?
479 GNUNET_YES : GNUNET_NO;
480 ch->reliable = (options & GNUNET_CADET_OPTION_RELIABLE) != 0 ?
481 GNUNET_YES : GNUNET_NO;
486 * Get a bit flag field with the options of a channel.
488 * @param ch Channel to get options from.
490 * @return Bit array in host byte order.
493 channel_get_options (struct CadetChannel *ch)
499 options |= GNUNET_CADET_OPTION_NOBUFFER;
501 options |= GNUNET_CADET_OPTION_RELIABLE;
508 * Notify a client that the channel is no longer valid.
510 * @param ch Channel that is destroyed.
511 * @param local_only Should we avoid sending it to other peers?
514 send_destroy (struct CadetChannel *ch, int local_only)
516 struct GNUNET_CADET_ChannelManage msg;
518 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
519 msg.header.size = htons (sizeof (msg));
520 msg.chid = htonl (ch->gid);
522 /* If root is not NULL, notify.
523 * If it's NULL, check lid_root. When a local destroy comes in, root
524 * is set to NULL but lid_root is left untouched. In this case, do nothing,
525 * the client is the one who requested the channel to be destroyed.
527 if (NULL != ch->root)
528 GML_send_channel_destroy (ch->root, ch->lid_root);
529 else if (0 == ch->lid_root && GNUNET_NO == local_only)
530 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
532 if (NULL != ch->dest)
533 GML_send_channel_destroy (ch->dest, ch->lid_dest);
534 else if (0 == ch->lid_dest && GNUNET_NO == local_only)
535 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
540 * Notify the destination client that a new incoming channel was created.
542 * @param ch Channel that was created.
545 send_client_create (struct CadetChannel *ch)
549 if (NULL == ch->dest)
553 opt |= GNUNET_YES == ch->reliable ? GNUNET_CADET_OPTION_RELIABLE : 0;
554 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_CADET_OPTION_NOBUFFER : 0;
555 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
556 GCT_get_destination (ch->t));
562 * Send data to a client.
564 * If the client is ready, send directly, otherwise buffer while listening
568 * @param msg Message.
569 * @param fwd Is this a fwd (root->dest) message?
572 send_client_data (struct CadetChannel *ch,
573 const struct GNUNET_CADET_Data *msg,
578 if (ch->dest_rel->client_ready)
580 GML_send_data (ch->dest, msg, ch->lid_dest);
581 ch->dest_rel->client_ready = GNUNET_NO;
582 ch->dest_rel->mid_recv++;
585 add_buffered_data (msg, ch->dest_rel);
589 if (ch->root_rel->client_ready)
591 GML_send_data (ch->root, msg, ch->lid_root);
592 ch->root_rel->client_ready = GNUNET_NO;
593 ch->root_rel->mid_recv++;
596 add_buffered_data (msg, ch->root_rel);
602 * Send a buffered message to the client, for in order delivery or
603 * as result of client ACK.
605 * @param ch Channel on which to empty the message buffer.
606 * @param c Client to send to.
607 * @param fwd Is this to send FWD data?.
610 send_client_buffered_data (struct CadetChannel *ch,
611 struct CadetClient *c,
614 struct CadetReliableMessage *copy;
615 struct CadetChannelReliability *rel;
617 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
618 rel = fwd ? ch->dest_rel : ch->root_rel;
619 if (GNUNET_NO == rel->client_ready)
621 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
625 copy = rel->head_recv;
626 /* We never buffer channel management messages */
629 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
631 struct GNUNET_CADET_Data *msg = (struct GNUNET_CADET_Data *) ©[1];
633 LOG (GNUNET_ERROR_TYPE_DEBUG, " have %u! now expecting %u\n",
634 copy->mid, rel->mid_recv + 1);
635 send_client_data (ch, msg, fwd);
637 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
638 LOG (GNUNET_ERROR_TYPE_DEBUG, " free copy recv MID %u (%p), %u left\n",
639 copy->mid, copy, rel->n_recv);
641 GCCH_send_data_ack (ch, fwd);
645 LOG (GNUNET_ERROR_TYPE_DEBUG, " reliable && don't have %u, next is %u\n",
646 rel->mid_recv, copy->mid);
647 if (GNUNET_YES == ch->destroy)
649 /* We don't have the next data piece and the remote peer has closed the
650 * channel. We won't receive it anymore, so just destroy the channel.
651 * FIXME: wait some time to allow other connections to
652 * deliver missing messages
654 send_destroy (ch, GNUNET_YES);
659 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
664 * Allow a client to send more data.
666 * In case the client was already allowed to send data, do nothing.
669 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
672 send_client_ack (struct CadetChannel *ch, int fwd)
674 struct CadetChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
675 struct CadetClient *c = fwd ? ch->root : ch->dest;
679 GNUNET_break (GNUNET_NO != ch->destroy);
682 LOG (GNUNET_ERROR_TYPE_DEBUG,
683 " sending %s ack to client on channel %s\n",
684 GC_f2s (fwd), GCCH_2s (ch));
692 if (GNUNET_YES == rel->client_allowed)
694 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
697 rel->client_allowed = GNUNET_YES;
699 GML_send_ack (c, fwd ? ch->lid_root : ch->lid_dest);
704 * Notify the root that the destination rejected the channel.
706 * @param ch Rejected channel.
709 send_client_nack (struct CadetChannel *ch)
711 if (NULL == ch->root)
716 GML_send_channel_nack (ch->root, ch->lid_root);
721 * We haven't received an ACK after a certain time: restransmit the message.
723 * @param cls Closure (CadetChannelReliability with the message to restransmit)
726 channel_retransmit_message (void *cls)
728 struct CadetChannelReliability *rel = cls;
729 struct CadetReliableMessage *copy;
730 struct CadetChannel *ch;
731 struct GNUNET_CADET_Data *payload;
734 rel->retry_task = NULL;
736 copy = rel->head_sent;
739 GNUNET_break (0); // FIXME tripped in rps testcase
743 payload = (struct GNUNET_CADET_Data *) ©[1];
744 fwd = (rel == ch->root_rel);
746 /* Message not found in the queue that we are going to use. */
747 LOG (GNUNET_ERROR_TYPE_DEBUG, "RETRANSMIT MID %u\n", copy->mid);
749 GCCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
750 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
755 * We haven't received an Channel ACK after a certain time: resend the CREATE.
757 * @param cls Closure (CadetChannelReliability of the channel to recreate)
760 channel_recreate (void *cls)
762 struct CadetChannelReliability *rel = cls;
764 rel->retry_task = NULL;
765 LOG (GNUNET_ERROR_TYPE_DEBUG, "RE-CREATE\n");
766 GNUNET_STATISTICS_update (stats,
767 "# data retransmitted", 1, GNUNET_NO);
769 if (rel == rel->ch->root_rel)
771 send_create (rel->ch);
773 else if (rel == rel->ch->dest_rel)
775 send_ack (rel->ch, GNUNET_YES);
785 * Message has been sent: start retransmission timer.
787 * @param cls Closure (queue structure).
789 * @param q Queue handler (no longer valid).
790 * @param type Type of message.
791 * @param size Size of the message.
794 ch_message_sent (void *cls,
795 struct CadetTunnel *t,
796 struct CadetTunnelQueue *q,
797 uint16_t type, size_t size)
799 struct CadetChannelQueue *chq = cls;
800 struct CadetReliableMessage *copy = chq->copy;
801 struct CadetChannelReliability *rel;
803 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel_message_sent callback %s\n",
808 case GNUNET_MESSAGE_TYPE_CADET_DATA:
809 LOG (GNUNET_ERROR_TYPE_DEBUG, "data MID %u sent\n", copy->mid);
810 GNUNET_assert (chq == copy->chq);
811 copy->timestamp = GNUNET_TIME_absolute_get ();
813 if (NULL == rel->retry_task)
815 LOG (GNUNET_ERROR_TYPE_DEBUG, " scheduling retry in %d * %s\n",
816 CADET_RETRANSMIT_MARGIN,
817 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
819 if (0 != rel->expected_delay.rel_value_us)
822 GNUNET_TIME_relative_multiply (rel->expected_delay,
823 CADET_RETRANSMIT_MARGIN);
827 rel->retry_timer = CADET_RETRANSMIT_TIME;
829 LOG (GNUNET_ERROR_TYPE_DEBUG, " using delay %s\n",
830 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
833 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
834 &channel_retransmit_message, rel);
838 LOG (GNUNET_ERROR_TYPE_DEBUG, "retry running %p\n", rel->retry_task);
844 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
845 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
846 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
847 LOG (GNUNET_ERROR_TYPE_DEBUG, "sent %s\n", GC_m2s (chq->type));
849 GNUNET_assert (rel->uniq == chq);
852 if (CADET_CHANNEL_READY != rel->ch->state
853 && GNUNET_MESSAGE_TYPE_CADET_DATA_ACK != type
854 && GNUNET_NO == rel->ch->destroy)
856 GNUNET_assert (NULL == rel->retry_task);
857 LOG (GNUNET_ERROR_TYPE_DEBUG, "STD BACKOFF %s\n",
858 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
860 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
861 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
862 &channel_recreate, rel);
875 * send a channel create message.
877 * @param ch Channel for which to send.
880 send_create (struct CadetChannel *ch)
882 struct GNUNET_CADET_ChannelCreate msgcc;
884 msgcc.header.size = htons (sizeof (msgcc));
885 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
886 msgcc.chid = htonl (ch->gid);
887 msgcc.port = htonl (ch->port);
888 msgcc.opt = htonl (channel_get_options (ch));
890 GCCH_send_prebuilt_message (&msgcc.header, ch, GNUNET_YES, NULL);
895 * Confirm we got a channel create or FWD ack.
897 * @param ch The channel to confirm.
898 * @param fwd Should we send a FWD ACK? (going dest->root)
901 send_ack (struct CadetChannel *ch, int fwd)
903 struct GNUNET_CADET_ChannelManage msg;
905 msg.header.size = htons (sizeof (msg));
906 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK);
907 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending channel %s ack for channel %s\n",
908 GC_f2s (fwd), GCCH_2s (ch));
910 msg.chid = htonl (ch->gid);
911 GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
916 * Send a message and don't keep any info about it: we won't need to cancel it
919 * @param msg Header of the message to fire away.
920 * @param ch Channel on which the message should go.
921 * @param force Is this a forced (undroppable) message?
924 fire_and_forget (const struct GNUNET_MessageHeader *msg,
925 struct CadetChannel *ch,
928 GNUNET_break (NULL == GCT_send_prebuilt_message (msg, ch->t, NULL,
934 * Notify that a channel create didn't succeed.
936 * @param ch The channel to reject.
939 send_nack (struct CadetChannel *ch)
941 struct GNUNET_CADET_ChannelManage msg;
943 msg.header.size = htons (sizeof (msg));
944 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
945 LOG (GNUNET_ERROR_TYPE_DEBUG,
946 " sending channel NACK for channel %s\n",
949 msg.chid = htonl (ch->gid);
950 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
955 * Destroy all reliable messages queued for a channel,
956 * during a channel destruction.
957 * Frees the reliability structure itself.
959 * @param rel Reliability data for a channel.
962 channel_rel_free_all (struct CadetChannelReliability *rel)
964 struct CadetReliableMessage *copy;
965 struct CadetReliableMessage *next;
970 for (copy = rel->head_recv; NULL != copy; copy = next)
973 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
974 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE ALL RECV %p\n", copy);
975 GNUNET_break (NULL == copy->chq);
978 for (copy = rel->head_sent; NULL != copy; copy = next)
981 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
982 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE ALL SEND %p\n", copy);
983 if (NULL != copy->chq)
985 if (NULL != copy->chq->tq)
987 GCT_cancel (copy->chq->tq);
988 /* ch_message_sent will free copy->q */
992 GNUNET_free (copy->chq);
998 if (NULL != rel->uniq && NULL != rel->uniq->tq)
1000 GCT_cancel (rel->uniq->tq);
1001 /* ch_message_sent is called freeing uniq */
1003 if (NULL != rel->retry_task)
1005 GNUNET_SCHEDULER_cancel (rel->retry_task);
1006 rel->retry_task = NULL;
1013 * Mark future messages as ACK'd.
1015 * @param rel Reliability data.
1016 * @param msg DataACK message with a bitfield of future ACK'd messages.
1018 * @return How many messages have been freed.
1021 channel_rel_free_sent (struct CadetChannelReliability *rel,
1022 const struct GNUNET_CADET_DataACK *msg)
1024 struct CadetReliableMessage *copy;
1025 struct CadetReliableMessage *next;
1033 bitfield = msg->futures;
1034 mid = ntohl (msg->mid);
1035 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable %u %lX\n", mid, bitfield);
1036 LOG (GNUNET_ERROR_TYPE_DEBUG, " rel %p, head %p\n", rel, rel->head_sent);
1037 for (i = 0, r = 0, copy = rel->head_sent;
1038 i < 64 && NULL != copy && 0 != bitfield;
1041 LOG (GNUNET_ERROR_TYPE_DEBUG, " trying bit %u (mid %u)\n", i, mid + i + 1);
1043 if (0 == (bitfield & mask))
1046 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
1047 /* Bit was set, clear the bit from the bitfield */
1050 /* The i-th bit was set. Do we have that copy? */
1051 /* Skip copies with mid < target */
1052 target = mid + i + 1;
1053 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
1054 while (NULL != copy && GC_is_pid_bigger (target, copy->mid))
1057 /* Did we run out of copies? (previously freed, it's ok) */
1060 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
1064 /* Did we overshoot the target? (previously freed, it's ok) */
1065 if (GC_is_pid_bigger (copy->mid, target))
1067 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
1068 i += copy->mid - target - 1; /* MID: 90, t = 85, i += 4 (i++ later) */
1069 mask = (0x1LL << (i + 1)) - 1; /* Mask = i-th bit and all before */
1070 bitfield &= ~mask; /* Clear all bits up to MID - 1 */
1074 /* Now copy->mid == target, free it */
1076 GNUNET_break (GNUNET_YES != rel_message_free (copy, GNUNET_YES));
1080 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
1086 * Destroy a reliable message after it has been acknowledged, either by
1087 * direct mid ACK or bitfield. Updates the appropriate data structures and
1088 * timers and frees all memory.
1090 * @param copy Message that is no longer needed: remote peer got it.
1091 * @param update_time Is the timing information relevant?
1092 * If this message is ACK in a batch the timing information
1093 * is skewed by the retransmission, count only for the
1094 * retransmitted message.
1096 * @return #GNUNET_YES if channel was destroyed as a result of the call,
1097 * #GNUNET_NO otherwise.
1100 rel_message_free (struct CadetReliableMessage *copy, int update_time)
1102 struct CadetChannelReliability *rel;
1103 struct GNUNET_TIME_Relative time;
1106 LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing %u\n", copy->mid);
1107 if (GNUNET_YES == update_time)
1109 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
1110 if (0 == rel->expected_delay.rel_value_us)
1111 rel->expected_delay = time;
1114 rel->expected_delay.rel_value_us *= 7;
1115 rel->expected_delay.rel_value_us += time.rel_value_us;
1116 rel->expected_delay.rel_value_us /= 8;
1118 LOG (GNUNET_ERROR_TYPE_DEBUG, " message time %12s\n",
1119 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
1120 LOG (GNUNET_ERROR_TYPE_DEBUG, " new delay %12s\n",
1121 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
1123 rel->retry_timer = rel->expected_delay;
1127 LOG (GNUNET_ERROR_TYPE_DEBUG, "batch free, ignoring timing\n");
1129 rel->ch->pending_messages--;
1130 if (NULL != copy->chq)
1132 GCT_cancel (copy->chq->tq);
1133 /* copy->q is set to NULL by ch_message_sent */
1135 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
1136 LOG (GNUNET_ERROR_TYPE_DEBUG, " free send copy MID %u at %p\n",
1140 if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
1142 GCCH_destroy (rel->ch);
1150 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
1152 * @param ch Channel to mark as ready.
1153 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
1156 channel_confirm (struct CadetChannel *ch, int fwd)
1158 struct CadetChannelReliability *rel;
1159 enum CadetChannelState oldstate;
1161 rel = fwd ? ch->root_rel : ch->dest_rel;
1164 GNUNET_break (GNUNET_NO != ch->destroy);
1167 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel confirm %s %s\n",
1168 GC_f2s (fwd), GCCH_2s (ch));
1169 oldstate = ch->state;
1170 ch->state = CADET_CHANNEL_READY;
1172 if (CADET_CHANNEL_READY != oldstate || GNUNET_YES == is_loopback (ch))
1174 rel->client_ready = GNUNET_YES;
1175 rel->expected_delay = rel->retry_timer;
1176 LOG (GNUNET_ERROR_TYPE_DEBUG, " confirm retry timer %s\n",
1177 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer, GNUNET_NO));
1178 if (GCT_get_connections_buffer (ch->t) > 0 || GCT_is_loopback (ch->t))
1179 send_client_ack (ch, fwd);
1181 if (NULL != rel->retry_task)
1183 GNUNET_SCHEDULER_cancel (rel->retry_task);
1184 rel->retry_task = NULL;
1186 else if (NULL != rel->uniq)
1188 GCT_cancel (rel->uniq->tq);
1189 /* ch_message_sent will free and NULL uniq */
1191 else if (GNUNET_NO == is_loopback (ch))
1193 /* We SHOULD have been trying to retransmit this! */
1198 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
1199 if (GNUNET_YES == fwd)
1200 send_ack (ch, GNUNET_NO);
1205 * Save a copy to retransmit in case it gets lost.
1207 * Initializes all needed callbacks and timers.
1209 * @param ch Channel this message goes on.
1210 * @param msg Message to copy.
1211 * @param fwd Is this fwd traffic?
1213 static struct CadetReliableMessage *
1214 channel_save_copy (struct CadetChannel *ch,
1215 const struct GNUNET_MessageHeader *msg,
1218 struct CadetChannelReliability *rel;
1219 struct CadetReliableMessage *copy;
1224 rel = fwd ? ch->root_rel : ch->dest_rel;
1225 mid = rel->mid_send - 1;
1226 type = ntohs (msg->type);
1227 size = ntohs (msg->size);
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, "save MID %u %s\n", mid, GC_m2s (type));
1230 copy = GNUNET_malloc (sizeof (struct CadetReliableMessage) + size);
1231 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", copy);
1235 memcpy (©[1], msg, size);
1236 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
1237 ch->pending_messages++;
1244 * Create a new channel.
1246 * @param t Tunnel this channel is in.
1247 * @param owner Client that owns the channel, NULL for foreign channels.
1248 * @param lid_root Local ID for root client.
1250 * @return A new initialized channel. NULL on error.
1252 static struct CadetChannel *
1253 channel_new (struct CadetTunnel *t,
1254 struct CadetClient *owner,
1255 CADET_ChannelNumber lid_root)
1257 struct CadetChannel *ch;
1259 ch = GNUNET_new (struct CadetChannel);
1261 ch->lid_root = lid_root;
1264 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
1268 ch->gid = GCT_get_next_chid (t);
1269 GML_channel_add (owner, lid_root, ch);
1271 GCT_add_channel (t, ch);
1278 * Handle a loopback message: call the appropriate handler for the message type.
1280 * @param ch Channel this message is on.
1281 * @param msgh Message header.
1282 * @param fwd Is this FWD traffic?
1285 handle_loopback (struct CadetChannel *ch,
1286 const struct GNUNET_MessageHeader *msgh,
1291 type = ntohs (msgh->type);
1292 LOG (GNUNET_ERROR_TYPE_DEBUG,
1293 "Loopback %s %s message!\n",
1294 GC_f2s (fwd), GC_m2s (type));
1298 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1299 /* Don't send hop ACK, wait for client to ACK */
1300 LOG (GNUNET_ERROR_TYPE_DEBUG, "SEND loopback %u (%u)\n",
1301 ntohl (((struct GNUNET_CADET_Data *) msgh)->mid), ntohs (msgh->size));
1302 GCCH_handle_data (ch, (struct GNUNET_CADET_Data *) msgh, fwd);
1305 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
1306 GCCH_handle_data_ack (ch, (struct GNUNET_CADET_DataACK *) msgh, fwd);
1309 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1310 GCCH_handle_create (ch->t,
1311 (struct GNUNET_CADET_ChannelCreate *) msgh);
1314 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
1315 GCCH_handle_ack (ch,
1316 (struct GNUNET_CADET_ChannelManage *) msgh,
1320 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
1321 GCCH_handle_nack (ch);
1324 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1325 GCCH_handle_destroy (ch,
1326 (struct GNUNET_CADET_ChannelManage *) msgh,
1331 GNUNET_break_op (0);
1332 LOG (GNUNET_ERROR_TYPE_DEBUG,
1333 "end-to-end message not known (%u)\n",
1334 ntohs (msgh->type));
1340 /******************************************************************************/
1341 /******************************** API ***********************************/
1342 /******************************************************************************/
1345 * Destroy a channel and free all resources.
1347 * @param ch Channel to destroy.
1350 GCCH_destroy (struct CadetChannel *ch)
1352 struct CadetClient *c;
1353 struct CadetTunnel *t;
1357 if (2 == ch->destroy)
1358 return; /* recursive call */
1361 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
1362 GCT_2s (ch->t), ch->gid);
1363 GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
1368 GML_channel_remove (c, ch->lid_root, ch);
1374 GML_channel_remove (c, ch->lid_dest, ch);
1377 channel_rel_free_all (ch->root_rel);
1378 channel_rel_free_all (ch->dest_rel);
1381 GCT_remove_channel (t, ch);
1382 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
1385 GCT_destroy_if_empty (t);
1390 * Get the channel's public ID.
1392 * @param ch Channel.
1394 * @return ID used to identify the channel with the remote peer.
1397 GCCH_get_id (const struct CadetChannel *ch)
1404 * Get the channel tunnel.
1406 * @param ch Channel to get the tunnel from.
1408 * @return tunnel of the channel.
1410 struct CadetTunnel *
1411 GCCH_get_tunnel (const struct CadetChannel *ch)
1418 * Get free buffer space towards the client on a specific channel.
1420 * @param ch Channel.
1421 * @param fwd Is query about FWD traffic?
1423 * @return Free buffer space [0 - 64]
1426 GCCH_get_buffer (struct CadetChannel *ch, int fwd)
1428 struct CadetChannelReliability *rel;
1430 rel = fwd ? ch->dest_rel : ch->root_rel;
1431 LOG (GNUNET_ERROR_TYPE_DEBUG, " get buffer, channel %s\n", GCCH_2s (ch));
1432 GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
1433 /* If rel is NULL it means that the end is not yet created,
1434 * most probably is a loopback channel at the point of sending
1435 * the ChannelCreate to itself.
1439 LOG (GNUNET_ERROR_TYPE_DEBUG, " rel is NULL: max\n");
1443 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_recv %d\n", rel->n_recv);
1444 return (64 - rel->n_recv);
1449 * Get flow control status of end point: is client allow to send?
1451 * @param ch Channel.
1452 * @param fwd Is query about FWD traffic? (Request root status).
1454 * @return #GNUNET_YES if client is allowed to send us data.
1457 GCCH_get_allowed (struct CadetChannel *ch, int fwd)
1459 struct CadetChannelReliability *rel;
1461 rel = fwd ? ch->root_rel : ch->dest_rel;
1465 /* Probably shutting down: root/dest NULL'ed to mark disconnection */
1466 GNUNET_break (GNUNET_NO != ch->destroy);
1470 return rel->client_allowed;
1475 * Is the root client for this channel on this peer?
1477 * @param ch Channel.
1478 * @param fwd Is this for fwd traffic?
1480 * @return #GNUNET_YES in case it is.
1483 GCCH_is_origin (struct CadetChannel *ch, int fwd)
1485 struct CadetClient *c;
1487 c = fwd ? ch->root : ch->dest;
1493 * Is the destination client for this channel on this peer?
1495 * @param ch Channel.
1496 * @param fwd Is this for fwd traffic?
1498 * @return #GNUNET_YES in case it is.
1501 GCCH_is_terminal (struct CadetChannel *ch, int fwd)
1503 struct CadetClient *c;
1505 c = fwd ? ch->dest : ch->root;
1511 * Send an end-to-end ACK message for the most recent in-sequence payload.
1513 * If channel is not reliable, do nothing.
1515 * @param ch Channel this is about.
1516 * @param fwd Is for FWD traffic? (ACK dest->owner)
1519 GCCH_send_data_ack (struct CadetChannel *ch, int fwd)
1521 struct GNUNET_CADET_DataACK msg;
1522 struct CadetChannelReliability *rel;
1523 struct CadetReliableMessage *copy;
1528 if (GNUNET_NO == ch->reliable)
1531 rel = fwd ? ch->dest_rel : ch->root_rel;
1532 ack = rel->mid_recv - 1;
1534 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_DATA_ACK);
1535 msg.header.size = htons (sizeof (msg));
1536 msg.chid = htonl (ch->gid);
1537 msg.mid = htonl (ack);
1540 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1542 if (copy->type != GNUNET_MESSAGE_TYPE_CADET_DATA)
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, " Type %s, expected DATA\n",
1545 GC_m2s (copy->type));
1548 GNUNET_assert (GC_is_pid_bigger(copy->mid, ack));
1549 delta = copy->mid - (ack + 1);
1552 mask = 0x1LL << delta;
1553 msg.futures |= mask;
1554 LOG (GNUNET_ERROR_TYPE_DEBUG,
1555 " setting bit for %u (delta %u) (%lX) -> %lX\n",
1556 copy->mid, delta, mask, msg.futures);
1559 GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1560 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1565 * Allow a client to send us more data, in case it was choked.
1567 * @param ch Channel.
1568 * @param fwd Is this about FWD traffic? (Root client).
1571 GCCH_allow_client (struct CadetChannel *ch, int fwd)
1573 struct CadetChannelReliability *rel;
1574 unsigned int buffer;
1576 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1578 if (CADET_CHANNEL_READY != ch->state)
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel not ready yet!\n");
1584 if (GNUNET_YES == ch->reliable)
1586 rel = fwd ? ch->root_rel : ch->dest_rel;
1589 GNUNET_break (GNUNET_NO != ch->destroy);
1592 if (NULL != rel->head_sent)
1594 if (64 <= rel->mid_send - rel->head_sent->mid)
1596 LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
1601 LOG (GNUNET_ERROR_TYPE_DEBUG, " gap ok: %u - %u\n",
1602 rel->head_sent->mid, rel->mid_send);
1603 struct CadetReliableMessage *aux;
1604 for (aux = rel->head_sent; NULL != aux; aux = aux->next)
1606 LOG (GNUNET_ERROR_TYPE_DEBUG, " - sent mid %u\n", aux->mid);
1612 LOG (GNUNET_ERROR_TYPE_DEBUG, " head sent is NULL\n");
1616 if (is_loopback (ch))
1617 buffer = GCCH_get_buffer (ch, fwd);
1619 buffer = GCT_get_connections_buffer (ch->t);
1623 LOG (GNUNET_ERROR_TYPE_DEBUG, " no buffer space.\n");
1627 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space %u, allowing\n", buffer);
1628 send_client_ack (ch, fwd);
1635 * @param ch Channel.
1636 * @param level Debug level to use.
1639 GCCH_debug (struct CadetChannel *ch, enum GNUNET_ErrorType level)
1643 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
1645 __FILE__, __FUNCTION__, __LINE__);
1651 LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
1654 LOG2 (level, "CHN Channel %s:%X (%p)\n", GCT_2s (ch->t), ch->gid, ch);
1655 LOG2 (level, "CHN root %p/%p\n", ch->root, ch->root_rel);
1656 if (NULL != ch->root)
1658 LOG2 (level, "CHN cli %s\n", GML_2s (ch->root));
1659 LOG2 (level, "CHN ready %s\n", ch->root_rel->client_ready ? "YES" : "NO");
1660 LOG2 (level, "CHN id %X\n", ch->lid_root);
1661 LOG2 (level, "CHN recv %d\n", ch->root_rel->n_recv);
1662 LOG2 (level, "CHN MID r: %d, s: %d\n",
1663 ch->root_rel->mid_recv, ch->root_rel->mid_send);
1665 LOG2 (level, "CHN dest %p/%p\n",
1666 ch->dest, ch->dest_rel);
1667 if (NULL != ch->dest)
1669 LOG2 (level, "CHN cli %s\n", GML_2s (ch->dest));
1670 LOG2 (level, "CHN ready %s\n", ch->dest_rel->client_ready ? "YES" : "NO");
1671 LOG2 (level, "CHN id %X\n", ch->lid_dest);
1672 LOG2 (level, "CHN recv %d\n", ch->dest_rel->n_recv);
1673 LOG2 (level, "CHN MID r: %d, s: %d\n",
1674 ch->dest_rel->mid_recv, ch->dest_rel->mid_send);
1681 * Handle an ACK given by a client.
1683 * Mark client as ready and send him any buffered data we could have for him.
1685 * @param ch Channel.
1686 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1689 GCCH_handle_local_ack (struct CadetChannel *ch, int fwd)
1691 struct CadetChannelReliability *rel;
1692 struct CadetClient *c;
1694 rel = fwd ? ch->dest_rel : ch->root_rel;
1695 c = fwd ? ch->dest : ch->root;
1697 rel->client_ready = GNUNET_YES;
1698 send_client_buffered_data (ch, c, fwd);
1700 if (GNUNET_YES == ch->destroy && 0 == rel->n_recv)
1702 send_destroy (ch, GNUNET_YES);
1706 /* if loopback is marked for destruction, no need to ACK to the other peer,
1707 * it requested the destruction and is already gone, therefore, else if.
1709 else if (is_loopback (ch))
1711 unsigned int buffer;
1713 buffer = GCCH_get_buffer (ch, fwd);
1715 GCCH_allow_client (ch, fwd);
1719 GCT_send_connection_acks (ch->t);
1724 * Handle data given by a client.
1726 * Check whether the client is allowed to send in this tunnel, save if channel
1727 * is reliable and send an ACK to the client if there is still buffer space
1730 * @param ch Channel.
1731 * @param c Client which sent the data.
1732 * @param fwd Is this a FWD data?
1733 * @param message Data message.
1734 * @param size Size of data.
1736 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1739 GCCH_handle_local_data (struct CadetChannel *ch,
1740 struct CadetClient *c, int fwd,
1741 const struct GNUNET_MessageHeader *message,
1744 struct CadetChannelReliability *rel;
1745 struct GNUNET_CADET_Data *payload;
1746 uint16_t p2p_size = sizeof(struct GNUNET_CADET_Data) + size;
1747 unsigned char cbuf[p2p_size];
1748 unsigned char buffer;
1750 /* Is the client in the channel? */
1757 GNUNET_break_op (0);
1758 return GNUNET_SYSERR;
1761 rel = fwd ? ch->root_rel : ch->dest_rel;
1763 if (GNUNET_NO == rel->client_allowed)
1765 GNUNET_break_op (0);
1766 return GNUNET_SYSERR;
1769 rel->client_allowed = GNUNET_NO;
1771 /* Ok, everything is correct, send the message. */
1772 payload = (struct GNUNET_CADET_Data *) cbuf;
1773 payload->mid = htonl (rel->mid_send);
1775 memcpy (&payload[1], message, size);
1776 payload->header.size = htons (p2p_size);
1777 payload->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_DATA);
1778 payload->chid = htonl (ch->gid);
1779 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1780 GCCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1782 if (is_loopback (ch))
1783 buffer = GCCH_get_buffer (ch, fwd);
1785 buffer = GCT_get_connections_buffer (ch->t);
1788 GCCH_allow_client (ch, fwd);
1795 * Handle a channel destroy requested by a client.
1797 * TODO: add "reason" field
1799 * Destroy the channel and the tunnel in case this was the last channel.
1801 * @param ch Channel.
1802 * @param c Client that requested the destruction (to avoid notifying him).
1803 * @param is_root Is the request coming from root?
1806 GCCH_handle_local_destroy (struct CadetChannel *ch,
1807 struct CadetClient *c,
1810 ch->destroy = GNUNET_YES;
1811 /* Cleanup after the tunnel */
1812 if (GNUNET_NO == is_root && c == ch->dest)
1814 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1815 GML_client_delete_channel (c, ch, ch->lid_dest);
1818 if (GNUNET_YES == is_root && c == ch->root)
1820 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1821 GML_client_delete_channel (c, ch, ch->lid_root);
1825 send_destroy (ch, GNUNET_NO);
1826 if (0 == ch->pending_messages)
1832 * Handle a channel create requested by a client.
1834 * Create the channel and the tunnel in case this was the first0 channel.
1836 * @param c Client that requested the creation (will be the root).
1837 * @param msg Create Channel message.
1839 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1842 GCCH_handle_local_create (struct CadetClient *c,
1843 struct GNUNET_CADET_ChannelMessage *msg)
1845 struct CadetChannel *ch;
1846 struct CadetTunnel *t;
1847 struct CadetPeer *peer;
1848 CADET_ChannelNumber chid;
1850 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1851 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1852 chid = ntohl (msg->channel_id);
1854 /* Sanity check for duplicate channel IDs */
1855 if (NULL != GML_channel_get (c, chid))
1858 return GNUNET_SYSERR;
1861 peer = GCP_get (&msg->peer, GNUNET_YES);
1862 GCP_add_tunnel (peer);
1863 t = GCP_get_tunnel (peer);
1865 if (GCP_get_short_id (peer) == myid)
1867 GCT_change_cstate (t, CADET_TUNNEL_READY);
1871 /* FIXME change to a tunnel API, eliminate ch <-> peer connection */
1875 /* Create channel */
1876 ch = channel_new (t, c, chid);
1880 return GNUNET_SYSERR;
1882 ch->port = ntohl (msg->port);
1883 channel_set_options (ch, ntohl (msg->opt));
1885 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1886 ch->root_rel = GNUNET_new (struct CadetChannelReliability);
1887 ch->root_rel->ch = ch;
1888 ch->root_rel->retry_timer = CADET_RETRANSMIT_TIME;
1889 ch->root_rel->expected_delay.rel_value_us = 0;
1891 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GCCH_2s (ch));
1900 * Handler for cadet network payload traffic.
1902 * @param ch Channel for the message.
1903 * @param msg Unencryted data message.
1904 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1905 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1906 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1907 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1910 GCCH_handle_data (struct CadetChannel *ch,
1911 const struct GNUNET_CADET_Data *msg,
1914 struct CadetChannelReliability *rel;
1915 struct CadetClient *c;
1916 struct GNUNET_MessageHeader *payload_msg;
1918 uint16_t payload_type;
1919 uint16_t payload_size;
1921 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1922 if (GNUNET_SYSERR == fwd)
1924 if (is_loopback (ch))
1926 /* It is a loopback channel after all... */
1930 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1933 /* Initialize FWD/BCK data */
1934 c = fwd ? ch->dest : ch->root;
1935 rel = fwd ? ch->dest_rel : ch->root_rel;
1939 GNUNET_break (GNUNET_NO != ch->destroy);
1943 if (CADET_CHANNEL_READY != ch->state)
1945 if (GNUNET_NO == fwd)
1947 /* If we are the root, this means the other peer has sent traffic before
1948 * receiving our ACK. Even if the SYNACK goes missing, no traffic should
1949 * be sent before the ACK.
1951 GNUNET_break_op (0);
1954 /* If we are the dest, this means that the SYNACK got to the root but
1955 * the ACK went missing. Treat this as an ACK.
1957 channel_confirm (ch, GNUNET_NO);
1960 payload_msg = (struct GNUNET_MessageHeader *) &msg[1];
1961 payload_type = ntohs (payload_msg->type);
1962 payload_size = ntohs (payload_msg->size);
1964 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1965 GNUNET_STATISTICS_update (stats, "# bytes received", payload_size, GNUNET_NO);
1967 mid = ntohl (msg->mid);
1968 LOG (GNUNET_ERROR_TYPE_INFO, "<== %s (%s %4u) on chan %s (%p) %s [%5u]\n",
1969 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_DATA), GC_m2s (payload_type), mid,
1970 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
1972 if (GNUNET_NO == ch->reliable ||
1973 ( !GC_is_pid_bigger (rel->mid_recv, mid) &&
1974 GC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1976 if (GNUNET_YES == ch->reliable)
1978 /* Is this the exact next expected messasge? */
1979 if (mid == rel->mid_recv)
1981 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected, sending to client\n");
1982 send_client_data (ch, msg, fwd);
1986 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1987 add_buffered_data (msg, rel);
1992 /* Tunnel is unreliable: send to clients directly */
1993 /* FIXME: accept Out Of Order traffic */
1994 rel->mid_recv = mid + 1;
1995 send_client_data (ch, msg, fwd);
2000 GNUNET_STATISTICS_update (stats, "# duplicate MID", 1, GNUNET_NO);
2001 if (GC_is_pid_bigger (rel->mid_recv, mid))
2003 GNUNET_break_op (0);
2004 LOG (GNUNET_ERROR_TYPE_INFO,
2005 "MID %u on channel %s not expected (window: %u - %u). Dropping!\n",
2006 mid, GCCH_2s (ch), rel->mid_recv, rel->mid_recv + 63);
2010 LOG (GNUNET_ERROR_TYPE_INFO,
2011 "Duplicate MID %u, channel %s (expecting MID %u). Re-sending ACK!\n",
2012 mid, GCCH_2s (ch), rel->mid_recv);
2013 if (NULL != rel->uniq)
2015 LOG (GNUNET_ERROR_TYPE_WARNING,
2016 "We are trying to send an ACK, but don't seem have the "
2017 "bandwidth. Have you set enough [ats] QUOTA in your config?\n");
2023 GCCH_send_data_ack (ch, fwd);
2028 * Handler for cadet network traffic end-to-end ACKs.
2030 * @param ch Channel on which we got this message.
2031 * @param msg Data message.
2032 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2033 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2034 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2035 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2038 GCCH_handle_data_ack (struct CadetChannel *ch,
2039 const struct GNUNET_CADET_DataACK *msg,
2042 struct CadetChannelReliability *rel;
2043 struct CadetReliableMessage *copy;
2044 struct CadetReliableMessage *next;
2048 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2049 if (GNUNET_SYSERR == fwd)
2051 if (is_loopback (ch))
2053 /* It is a loopback channel after all... */
2057 /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
2058 fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
2061 ack = ntohl (msg->mid);
2062 LOG (GNUNET_ERROR_TYPE_INFO,
2063 "<== %s (0x%010lX %4u) on chan %s (%p) %s [%5u]\n",
2064 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_DATA_ACK), msg->futures, ack,
2065 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
2067 if (GNUNET_YES == fwd)
2074 GNUNET_break (GNUNET_NO != ch->destroy);
2078 /* Free ACK'd copies: no need to retransmit those anymore FIXME refactor */
2079 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
2081 if (GC_is_pid_bigger (copy->mid, ack))
2083 LOG (GNUNET_ERROR_TYPE_DEBUG, " head %u, out!\n", copy->mid);
2084 if (0 < channel_rel_free_sent (rel, msg))
2089 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %u\n", copy->mid);
2091 if (GNUNET_YES == rel_message_free (copy, GNUNET_YES))
2093 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel destoyed\n");
2098 /* ACK client if needed and possible */
2099 GCCH_allow_client (ch, fwd);
2101 /* If some message was free'd, update the retransmission delay */
2102 if (GNUNET_YES == work)
2104 if (NULL != rel->retry_task)
2106 GNUNET_SCHEDULER_cancel (rel->retry_task);
2107 rel->retry_task = NULL;
2108 if (NULL != rel->head_sent && NULL == rel->head_sent->chq)
2110 struct GNUNET_TIME_Absolute new_target;
2111 struct GNUNET_TIME_Relative delay;
2113 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
2114 CADET_RETRANSMIT_MARGIN);
2115 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
2117 delay = GNUNET_TIME_absolute_get_remaining (new_target);
2119 GNUNET_SCHEDULER_add_delayed (delay,
2120 &channel_retransmit_message,
2126 /* Work was done but no task was pending.
2127 * Task was cancelled by a retransmission that is sitting in the queue.
2129 // FIXME add test to make sure this is the case, probably add return
2130 // value to GCCH_send_prebuilt_message
2137 * Handler for channel create messages.
2139 * Does not have fwd parameter because it's always 'FWD': channel is incoming.
2141 * @param t Tunnel this channel will be in.
2142 * @param msg Channel crate message.
2144 struct CadetChannel *
2145 GCCH_handle_create (struct CadetTunnel *t,
2146 const struct GNUNET_CADET_ChannelCreate *msg)
2148 CADET_ChannelNumber chid;
2149 struct CadetChannel *ch;
2150 struct CadetClient *c;
2154 chid = ntohl (msg->chid);
2156 ch = GCT_get_channel (t, chid);
2159 /* Create channel */
2160 ch = channel_new (t, NULL, 0);
2162 channel_set_options (ch, ntohl (msg->opt));
2163 new_channel = GNUNET_YES;
2167 new_channel = GNUNET_NO;
2169 port = ntohl (msg->port);
2171 LOG (GNUNET_ERROR_TYPE_INFO,
2172 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2173 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE), chid, port,
2174 GCCH_2s (ch), ch, GC_f2s (GNUNET_YES), ntohs (msg->header.size));
2176 if (GNUNET_YES == new_channel || GCT_is_loopback (t))
2178 /* Find a destination client */
2180 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", ch->port);
2181 c = GML_client_get_by_port (ch->port);
2184 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
2185 if (is_loopback (ch))
2187 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback: destroy on handler\n");
2192 LOG (GNUNET_ERROR_TYPE_DEBUG, " not loopback: destroy now\n");
2200 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
2203 add_destination (ch, c);
2204 if (GNUNET_YES == ch->reliable)
2205 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reliable\n");
2207 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not Reliable\n");
2209 send_client_create (ch);
2210 ch->state = CADET_CHANNEL_SENT;
2214 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate create channel\n");
2215 if (NULL != ch->dest_rel->retry_task)
2217 LOG (GNUNET_ERROR_TYPE_DEBUG, " clearing retry task\n");
2218 /* we were waiting to re-send our 'SYNACK', wait no more! */
2219 GNUNET_SCHEDULER_cancel (ch->dest_rel->retry_task);
2220 ch->dest_rel->retry_task = NULL;
2222 else if (NULL != ch->dest_rel->uniq)
2224 /* we are waiting to for our 'SYNACK' to leave the queue, all done! */
2228 send_ack (ch, GNUNET_YES);
2235 * Handler for channel NACK messages.
2237 * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
2239 * @param ch Channel.
2242 GCCH_handle_nack (struct CadetChannel *ch)
2244 LOG (GNUNET_ERROR_TYPE_INFO,
2245 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2246 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK), ch->gid, 0,
2247 GCCH_2s (ch), ch, "---", 0);
2249 send_client_nack (ch);
2255 * Handler for channel ack messages.
2257 * @param ch Channel.
2258 * @param msg Message.
2259 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2260 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2261 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2262 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2265 GCCH_handle_ack (struct CadetChannel *ch,
2266 const struct GNUNET_CADET_ChannelManage *msg,
2269 LOG (GNUNET_ERROR_TYPE_INFO,
2270 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2271 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK), ch->gid, 0,
2272 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
2274 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2275 if (GNUNET_SYSERR == fwd)
2277 if (is_loopback (ch))
2279 /* It is a loopback channel after all... */
2283 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2286 channel_confirm (ch, !fwd);
2291 * Handler for channel destroy messages.
2293 * @param ch Channel to be destroyed of.
2294 * @param msg Message.
2295 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2296 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2297 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2298 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2301 GCCH_handle_destroy (struct CadetChannel *ch,
2302 const struct GNUNET_CADET_ChannelManage *msg,
2305 struct CadetChannelReliability *rel;
2307 LOG (GNUNET_ERROR_TYPE_INFO,
2308 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2309 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY), ch->gid, 0,
2310 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
2312 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2313 if (GNUNET_SYSERR == fwd)
2315 if (is_loopback (ch))
2317 /* It is a loopback channel after all... */
2321 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2324 GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
2325 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
2327 /* Not for us (don't destroy twice a half-open loopback channel) */
2331 rel = fwd ? ch->dest_rel : ch->root_rel;
2332 if (0 == rel->n_recv)
2334 send_destroy (ch, GNUNET_YES);
2339 ch->destroy = GNUNET_YES;
2345 * Sends an already built message on a channel.
2347 * If the channel is on a loopback tunnel, notifies the appropriate destination
2350 * On a normal channel passes the message to the tunnel for encryption and
2351 * sending on a connection.
2353 * This function DOES NOT save the message for retransmission.
2355 * @param message Message to send. Function makes a copy of it.
2356 * @param ch Channel on which this message is transmitted.
2357 * @param fwd Is this a fwd message?
2358 * @param existing_copy This is a retransmission, don't save a copy.
2361 GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2362 struct CadetChannel *ch, int fwd,
2363 void *existing_copy)
2365 struct CadetChannelQueue *chq;
2371 type = ntohs (message->type);
2372 size = ntohs (message->size);
2376 case GNUNET_MESSAGE_TYPE_CADET_DATA:
2378 struct GNUNET_CADET_Data *data_msg;
2379 struct GNUNET_MessageHeader *payload_msg;
2380 uint16_t payload_type;
2382 data_msg = (struct GNUNET_CADET_Data *) message;
2383 data_id = ntohl (data_msg->mid);
2384 payload_msg = (struct GNUNET_MessageHeader *) &data_msg[1];
2385 payload_type = ntohs (payload_msg->type);
2386 strncpy (info, GC_m2s (payload_type), 31);
2390 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
2392 struct GNUNET_CADET_DataACK *ack_msg;
2393 ack_msg = (struct GNUNET_CADET_DataACK *) message;
2394 data_id = ntohl (ack_msg->mid);
2395 SPRINTF (info, "0x%010lX", ack_msg->futures);
2398 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
2400 struct GNUNET_CADET_ChannelCreate *cc_msg;
2401 cc_msg = (struct GNUNET_CADET_ChannelCreate *) message;
2402 data_id = ntohl (cc_msg->port);
2403 SPRINTF (info, " 0x%08X", ntohl (cc_msg->chid));
2406 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
2407 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
2408 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2410 struct GNUNET_CADET_ChannelManage *m_msg;
2411 m_msg = (struct GNUNET_CADET_ChannelManage *) message;
2413 SPRINTF (info, " 0x%08X", ntohl (m_msg->chid));
2420 LOG (GNUNET_ERROR_TYPE_INFO,
2421 "==> %s (%12s %4u) on chan %s (%p) %s [%5u]\n",
2422 GC_m2s (type), info, data_id,
2423 GCCH_2s (ch), ch, GC_f2s (fwd), size);
2425 if (GCT_is_loopback (ch->t))
2427 handle_loopback (ch, message, fwd);
2433 case GNUNET_MESSAGE_TYPE_CADET_DATA:
2434 if (GNUNET_YES == ch->reliable)
2436 chq = GNUNET_new (struct CadetChannelQueue);
2438 if (NULL == existing_copy)
2439 chq->copy = channel_save_copy (ch, message, fwd);
2442 chq->copy = (struct CadetReliableMessage *) existing_copy;
2443 if (NULL != chq->copy->chq)
2445 /* Last retransmission was queued but not yet sent!
2446 * This retransmission was scheduled by a ch_message_sent which
2447 * followed a very fast RTT, so the tiny delay made the
2448 * retransmission function to execute before the previous
2449 * retransmitted message even had a chance to leave the peer.
2450 * Cancel this message and wait until the pending
2451 * retransmission leaves the peer and ch_message_sent starts
2452 * the timer for the next one.
2455 LOG (GNUNET_ERROR_TYPE_DEBUG,
2456 " exisitng copy not yet transmitted!\n");
2459 LOG (GNUNET_ERROR_TYPE_DEBUG,
2460 " using existing copy: %p {r:%p q:%p t:%u}\n",
2462 chq->copy->rel, chq->copy->chq, chq->copy->type);
2464 LOG (GNUNET_ERROR_TYPE_DEBUG, " new chq: %p\n", chq);
2465 chq->copy->chq = chq;
2466 chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL,
2468 &ch_message_sent, chq);
2469 /* q itself is stored in copy */
2470 GNUNET_assert (NULL != chq->tq || GNUNET_NO != ch->destroy);
2474 fire_and_forget (message, ch, GNUNET_NO);
2479 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
2480 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
2481 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
2482 chq = GNUNET_new (struct CadetChannelQueue);
2484 chq->rel = fwd ? ch->root_rel : ch->dest_rel;
2485 if (NULL != chq->rel->uniq)
2487 if (NULL != chq->rel->uniq->tq)
2489 GCT_cancel (chq->rel->uniq->tq);
2490 /* ch_message_sent is called, freeing and NULLing uniq */
2491 GNUNET_break (NULL == chq->rel->uniq);
2496 GNUNET_free (chq->rel->uniq);
2500 chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL, GNUNET_YES,
2501 &ch_message_sent, chq);
2502 if (NULL == chq->tq)
2505 GCT_debug (ch->t, GNUNET_ERROR_TYPE_ERROR);
2510 chq->rel->uniq = chq;
2514 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2515 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
2516 fire_and_forget (message, ch, GNUNET_YES);
2522 LOG (GNUNET_ERROR_TYPE_WARNING, "type %s unknown!\n", GC_m2s (type));
2523 fire_and_forget (message, ch, GNUNET_YES);
2529 * Get the static string for identification of the channel.
2531 * @param ch Channel.
2533 * @return Static string with the channel IDs.
2536 GCCH_2s (const struct CadetChannel *ch)
2538 static char buf[64];
2541 return "(NULL Channel)";
2543 SPRINTF (buf, "%s:%u gid:%X (%X / %X)",
2544 GCT_2s (ch->t), ch->port, ch->gid, ch->lid_root, ch->lid_dest);