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 "mesh_protocol.h"
30 #include "gnunet-service-mesh_channel.h"
31 #include "gnunet-service-mesh_local.h"
32 #include "gnunet-service-mesh_tunnel.h"
33 #include "gnunet-service-mesh_peer.h"
35 #define LOG(level, ...) GNUNET_log_from(level,"mesh-chn",__VA_ARGS__)
37 #define MESH_RETRANSMIT_TIME GNUNET_TIME_relative_multiply(\
38 GNUNET_TIME_UNIT_MILLISECONDS, 250)
39 #define MESH_RETRANSMIT_MARGIN 4
43 * All the states a connection can be in.
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 MeshChannelQueue
72 struct MeshTunnel3Queue *tq;
75 * Message type (DATA/DATA_ACK)
80 * Message copy (for DATAs, to start retransmission timer)
82 struct MeshReliableMessage *copy;
85 * Reliability (for DATA_ACKs, to access rel->ack_q)
87 struct MeshChannelReliability *rel;
92 * Info needed to retry a message in case it gets lost.
94 struct MeshReliableMessage
97 * Double linked list, FIFO style
99 struct MeshReliableMessage *next;
100 struct MeshReliableMessage *prev;
103 * Type of message (payload, channel management).
108 * Tunnel Reliability queue this message is in.
110 struct MeshChannelReliability *rel;
113 * ID of the message (ACK needed to free)
120 struct MeshChannelQueue *chq;
123 * When was this message issued (to calculate ACK delay)
125 struct GNUNET_TIME_Absolute timestamp;
127 /* struct GNUNET_MESH_Data with payload */
132 * Info about the traffic state for a client in a channel.
134 struct MeshChannelReliability
137 * Channel this is about.
139 struct MeshChannel *ch;
142 * DLL of messages sent and not yet ACK'd.
144 struct MeshReliableMessage *head_sent;
145 struct MeshReliableMessage *tail_sent;
148 * DLL of messages received out of order.
150 struct MeshReliableMessage *head_recv;
151 struct MeshReliableMessage *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 MeshChannelQueue *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 MeshTunnel3 *t;
211 * Destination port of the channel.
216 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
218 MESH_ChannelNumber gid;
221 * Local tunnel number for root (owner) client.
222 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
224 MESH_ChannelNumber lid_root;
227 * Local tunnel number for local destination clients (incoming number)
228 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
230 MESH_ChannelNumber lid_dest;
235 enum MeshChannelState 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 MeshClient *root;
258 * Client destination of the tunnel, if any.
260 struct MeshClient *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 MeshChannelReliability *root_rel;
282 * Only present (non-NULL) at the destination of a tunnel.
284 struct MeshChannelReliability *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 /******************************************************************************/
309 * Destroy a reliable message after it has been acknowledged, either by
310 * direct mid ACK or bitfield. Updates the appropriate data structures and
311 * timers and frees all memory.
313 * @param copy Message that is no longer needed: remote peer got it.
314 * @param update_time Is the timing information relevant?
315 * If this message is ACK in a batch the timing information
316 * is skewed by the retransmission, count only for the
317 * retransmitted message.
320 rel_message_free (struct MeshReliableMessage *copy, int update_time);
323 * send a channel create message.
325 * @param ch Channel for which to send.
328 send_create (struct MeshChannel *ch);
331 * Confirm we got a channel create, FWD ack.
333 * @param ch The channel to confirm.
334 * @param fwd Should we send a FWD ACK? (going dest->root)
337 send_ack (struct MeshChannel *ch, int fwd);
342 * Test if the channel is loopback: both root and dest are on the local peer.
344 * @param ch Channel to test.
346 * @return #GNUNET_YES if channel is loopback, #GNUNET_NO otherwise.
349 is_loopback (const struct MeshChannel *ch)
352 return GMT_is_loopback (ch->t);
354 return (NULL != ch->root && NULL != ch->dest);
359 * We have received a message out of order, or the client is not ready.
360 * Buffer it until we receive an ACK from the client or the missing
361 * message from the channel.
363 * @param msg Message to buffer (MUST be of type MESH_DATA).
364 * @param rel Reliability data to the corresponding direction.
367 add_buffered_data (const struct GNUNET_MESH_Data *msg,
368 struct MeshChannelReliability *rel)
370 struct MeshReliableMessage *copy;
371 struct MeshReliableMessage *prev;
375 size = ntohs (msg->header.size);
376 mid = ntohl (msg->mid);
378 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
380 copy = GNUNET_malloc (sizeof (*copy) + size);
383 copy->type = GNUNET_MESSAGE_TYPE_MESH_DATA;
384 memcpy (©[1], msg, size);
388 // FIXME do something better than O(n), although n < 64...
389 // FIXME start from the end (most messages are the latest ones)
390 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
392 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
393 if (GM_is_pid_bigger (prev->mid, mid))
395 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
396 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
401 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
402 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
403 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
408 * Add a destination client to a channel, initializing all data structures
409 * in the channel and the client.
411 * @param ch Channel to which add the destination.
412 * @param c Client which to add to the channel.
415 add_destination (struct MeshChannel *ch, struct MeshClient *c)
417 if (NULL != ch->dest)
423 /* Assign local id as destination */
424 ch->lid_dest = GML_get_next_chid (c);
426 /* Store in client's hashmap */
427 GML_channel_add (c, ch->lid_dest, ch);
429 GNUNET_break (NULL == ch->dest_rel);
430 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
431 ch->dest_rel->ch = ch;
432 ch->dest_rel->expected_delay.rel_value_us = 0;
433 ch->dest_rel->retry_timer = MESH_RETRANSMIT_TIME;
440 * Set options in a channel, extracted from a bit flag field.
442 * @param ch Channel to set options to.
443 * @param options Bit array in host byte order.
446 channel_set_options (struct MeshChannel *ch, uint32_t options)
448 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
449 GNUNET_YES : GNUNET_NO;
450 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
451 GNUNET_YES : GNUNET_NO;
456 * Get a bit flag field with the options of a channel.
458 * @param ch Channel to get options from.
460 * @return Bit array in host byte order.
463 channel_get_options (struct MeshChannel *ch)
469 options |= GNUNET_MESH_OPTION_NOBUFFER;
471 options |= GNUNET_MESH_OPTION_RELIABLE;
478 * Notify the destination client that a new incoming channel was created.
480 * @param ch Channel that was created.
483 send_client_create (struct MeshChannel *ch)
487 if (NULL == ch->dest)
491 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
492 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
493 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
494 GMT_get_destination (ch->t));
500 * Send data to a client.
502 * If the client is ready, send directly, otherwise buffer while listening
506 * @param msg Message.
507 * @param fwd Is this a fwd (root->dest) message?
510 send_client_data (struct MeshChannel *ch,
511 const struct GNUNET_MESH_Data *msg,
516 if (ch->dest_rel->client_ready)
517 GML_send_data (ch->dest, msg, ch->lid_dest);
519 add_buffered_data (msg, ch->dest_rel);
523 if (ch->root_rel->client_ready)
524 GML_send_data (ch->root, msg, ch->lid_root);
526 add_buffered_data (msg, ch->root_rel);
532 * Send a buffered message to the client, for in order delivery or
533 * as result of client ACK.
535 * @param ch Channel on which to empty the message buffer.
536 * @param c Client to send to.
537 * @param fwd Is this to send FWD data?.
540 send_client_buffered_data (struct MeshChannel *ch,
541 struct MeshClient *c,
544 struct MeshReliableMessage *copy;
545 struct MeshChannelReliability *rel;
547 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
548 rel = fwd ? ch->dest_rel : ch->root_rel;
549 if (GNUNET_NO == rel->client_ready)
551 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
555 copy = rel->head_recv;
556 /* We never buffer channel management messages */
559 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
561 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
563 LOG (GNUNET_ERROR_TYPE_DEBUG,
564 " have %u! now expecting %u\n",
565 copy->mid, rel->mid_recv + 1);
566 send_client_data (ch, msg, fwd);
569 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
570 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE RECV %p\n", copy);
575 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 " reliable && don't have %u, next is %u\n",
582 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
587 * Allow a client to send more data.
589 * In case the client was already allowed to send data, do nothing.
592 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
595 send_client_ack (struct MeshChannel *ch, int fwd)
597 struct MeshChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
598 struct MeshClient *c = fwd ? ch->root : ch->dest;
602 GNUNET_break (GNUNET_NO != ch->destroy);
605 LOG (GNUNET_ERROR_TYPE_DEBUG,
606 " sending %s ack to client on channel %s\n",
607 GM_f2s (fwd), GMCH_2s (ch));
615 if (GNUNET_YES == rel->client_allowed)
617 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
620 rel->client_allowed = GNUNET_YES;
622 GML_send_ack (c, fwd ? ch->lid_root : ch->lid_dest);
627 * Notify the root that the destination rejected the channel.
629 * @param ch Rejected channel.
632 send_client_nack (struct MeshChannel *ch)
634 if (NULL == ch->root)
639 GML_send_channel_nack (ch->root, ch->lid_root);
644 * We haven't received an ACK after a certain time: restransmit the message.
646 * @param cls Closure (MeshChannelReliability with the message to restransmit)
647 * @param tc TaskContext.
650 channel_retransmit_message (void *cls,
651 const struct GNUNET_SCHEDULER_TaskContext *tc)
653 struct MeshChannelReliability *rel = cls;
654 struct MeshReliableMessage *copy;
655 struct MeshChannel *ch;
656 struct GNUNET_MESH_Data *payload;
659 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
660 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
664 copy = rel->head_sent;
671 payload = (struct GNUNET_MESH_Data *) ©[1];
672 fwd = (rel == ch->root_rel);
674 /* Message not found in the queue that we are going to use. */
675 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
677 GMCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
678 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
683 * We haven't received an Channel ACK after a certain time: resend the CREATE.
685 * @param cls Closure (MeshChannelReliability of the channel to recreate)
686 * @param tc TaskContext.
689 channel_recreate (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
691 struct MeshChannelReliability *rel = cls;
693 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
694 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
697 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RE-CREATE\n");
698 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
700 if (rel == rel->ch->root_rel)
702 send_create (rel->ch);
704 else if (rel == rel->ch->dest_rel)
706 send_ack (rel->ch, GNUNET_YES);
717 * Message has been sent: start retransmission timer.
719 * @param cls Closure (queue structure).
721 * @param q Queue handler (no longer valid).
722 * @param type Type of message.
723 * @param size Size of the message.
726 ch_message_sent (void *cls,
727 struct MeshTunnel3 *t,
728 struct MeshTunnel3Queue *q,
729 uint16_t type, size_t size)
731 struct MeshChannelQueue *chq = cls;
732 struct MeshReliableMessage *copy = chq->copy;
733 struct MeshChannelReliability *rel;
737 case GNUNET_MESSAGE_TYPE_MESH_DATA:
738 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT DATA MID %u\n", copy->mid);
739 GNUNET_assert (chq == copy->chq);
740 copy->timestamp = GNUNET_TIME_absolute_get ();
742 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
744 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! scheduling retry in 4 * %s\n",
745 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
747 if (0 != rel->expected_delay.rel_value_us)
749 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay != 0\n");
751 GNUNET_TIME_relative_multiply (rel->expected_delay,
752 MESH_RETRANSMIT_MARGIN);
756 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay reset\n");
757 rel->retry_timer = MESH_RETRANSMIT_TIME;
759 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! using delay %s\n",
760 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
763 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
764 &channel_retransmit_message, rel);
768 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! retry task %u\n", rel->retry_task);
774 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
775 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
776 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
777 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT %s\n", GM_m2s (chq->type));
779 GNUNET_assert (rel->uniq == chq);
782 if (MESH_CHANNEL_READY != rel->ch->state
783 && GNUNET_MESSAGE_TYPE_MESH_DATA_ACK != type
784 && GNUNET_NO == rel->ch->destroy)
786 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rel->retry_task);
787 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! STD BACKOFF %s\n",
788 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
790 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
791 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
792 &channel_recreate, rel);
806 * send a channel create message.
808 * @param ch Channel for which to send.
811 send_create (struct MeshChannel *ch)
813 struct GNUNET_MESH_ChannelCreate msgcc;
815 msgcc.header.size = htons (sizeof (msgcc));
816 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
817 msgcc.chid = htonl (ch->gid);
818 msgcc.port = htonl (ch->port);
819 msgcc.opt = htonl (channel_get_options (ch));
821 GMCH_send_prebuilt_message (&msgcc.header, ch, GNUNET_YES, NULL);
826 * Confirm we got a channel create or FWD ack.
828 * @param ch The channel to confirm.
829 * @param fwd Should we send a FWD ACK? (going dest->root)
832 send_ack (struct MeshChannel *ch, int fwd)
834 struct GNUNET_MESH_ChannelManage msg;
836 msg.header.size = htons (sizeof (msg));
837 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
838 LOG (GNUNET_ERROR_TYPE_DEBUG,
839 " sending channel %s ack for channel %s\n",
840 GM_f2s (fwd), GMCH_2s (ch));
842 msg.chid = htonl (ch->gid);
843 GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
848 * Send a message and don't keep any info about it: we won't need to cancel it
851 * @param msg Header of the message to fire away.
852 * @param ch Channel on which the message should go.
853 * @param force Is this a forced (undroppable) message?
856 fire_and_forget (const struct GNUNET_MessageHeader *msg,
857 struct MeshChannel *ch,
860 GNUNET_break (NULL == GMT_send_prebuilt_message (msg, ch->t, NULL,
866 * Notify that a channel create didn't succeed.
868 * @param ch The channel to reject.
871 send_nack (struct MeshChannel *ch)
873 struct GNUNET_MESH_ChannelManage msg;
875 msg.header.size = htons (sizeof (msg));
876 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK);
877 LOG (GNUNET_ERROR_TYPE_DEBUG,
878 " sending channel NACK for channel %s\n",
881 msg.chid = htonl (ch->gid);
882 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
887 * Notify a client that the channel is no longer valid.
889 * @param ch Channel that is destroyed.
890 * @param local_only Should we avoid sending it to other peers?
893 send_destroy (struct MeshChannel *ch, int local_only)
895 struct GNUNET_MESH_ChannelManage msg;
897 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
898 msg.header.size = htons (sizeof (msg));
899 msg.chid = htonl (ch->gid);
901 /* If root is not NULL, notify.
902 * If it's NULL, check lid_root. When a local destroy comes in, root
903 * is set to NULL but lid_root is left untouched. In this case, do nothing,
904 * the client is the one who reuqested the channel to be destroyed.
906 if (NULL != ch->root)
907 GML_send_channel_destroy (ch->root, ch->lid_root);
908 else if (0 == ch->lid_root && GNUNET_NO == local_only)
909 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
911 if (NULL != ch->dest)
912 GML_send_channel_destroy (ch->dest, ch->lid_dest);
913 else if (0 == ch->lid_dest && GNUNET_NO == local_only)
914 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
919 * Destroy all reliable messages queued for a channel,
920 * during a channel destruction.
921 * Frees the reliability structure itself.
923 * @param rel Reliability data for a channel.
926 channel_rel_free_all (struct MeshChannelReliability *rel)
928 struct MeshReliableMessage *copy;
929 struct MeshReliableMessage *next;
934 for (copy = rel->head_recv; NULL != copy; copy = next)
937 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
938 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH RECV %p\n", copy);
939 GNUNET_break (NULL == copy->chq);
942 for (copy = rel->head_sent; NULL != copy; copy = next)
945 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
946 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH %p\n", copy);
947 if (NULL != copy->chq)
949 if (NULL != copy->chq->tq)
951 GMT_cancel (copy->chq->tq);
952 /* ch_message_sent will free copy->q */
956 GNUNET_free (copy->chq);
962 if (NULL != rel->uniq && NULL != rel->uniq->tq)
964 GMT_cancel (rel->uniq->tq);
965 /* ch_message_sent is called freeing uniq */
967 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
969 GNUNET_SCHEDULER_cancel (rel->retry_task);
970 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
977 * Mark future messages as ACK'd.
979 * @param rel Reliability data.
980 * @param msg DataACK message with a bitfield of future ACK'd messages.
983 channel_rel_free_sent (struct MeshChannelReliability *rel,
984 const struct GNUNET_MESH_DataACK *msg)
986 struct MeshReliableMessage *copy;
987 struct MeshReliableMessage *next;
994 bitfield = msg->futures;
995 mid = ntohl (msg->mid);
996 LOG (GNUNET_ERROR_TYPE_DEBUG,
997 "!!! free_sent_reliable %u %llX\n",
999 LOG (GNUNET_ERROR_TYPE_DEBUG,
1000 " rel %p, head %p\n",
1001 rel, rel->head_sent);
1002 for (i = 0, copy = rel->head_sent;
1003 i < 64 && NULL != copy && 0 != bitfield;
1006 LOG (GNUNET_ERROR_TYPE_DEBUG,
1007 " trying bit %u (mid %u)\n",
1010 if (0 == (bitfield & mask))
1013 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
1014 /* Bit was set, clear the bit from the bitfield */
1017 /* The i-th bit was set. Do we have that copy? */
1018 /* Skip copies with mid < target */
1019 target = mid + i + 1;
1020 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
1021 while (NULL != copy && GM_is_pid_bigger (target, copy->mid))
1024 /* Did we run out of copies? (previously freed, it's ok) */
1027 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
1031 /* Did we overshoot the target? (previously freed, it's ok) */
1032 if (GM_is_pid_bigger (copy->mid, target))
1034 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
1038 /* Now copy->mid == target, free it */
1040 GNUNET_break (GNUNET_YES != rel_message_free (copy, GNUNET_YES));
1043 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
1048 * Destroy a reliable message after it has been acknowledged, either by
1049 * direct mid ACK or bitfield. Updates the appropriate data structures and
1050 * timers and frees all memory.
1052 * @param copy Message that is no longer needed: remote peer got it.
1053 * @param update_time Is the timing information relevant?
1054 * If this message is ACK in a batch the timing information
1055 * is skewed by the retransmission, count only for the
1056 * retransmitted message.
1058 * @return #GNUNET_YES if channel was destroyed as a result of the call,
1059 * #GNUNET_NO otherwise.
1062 rel_message_free (struct MeshReliableMessage *copy, int update_time)
1064 struct MeshChannelReliability *rel;
1065 struct GNUNET_TIME_Relative time;
1068 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
1071 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
1072 if (0 == rel->expected_delay.rel_value_us)
1073 rel->expected_delay = time;
1076 rel->expected_delay.rel_value_us *= 7;
1077 rel->expected_delay.rel_value_us += time.rel_value_us;
1078 rel->expected_delay.rel_value_us /= 8;
1080 LOG (GNUNET_ERROR_TYPE_INFO, "!!! took %s, new delay %s\n",
1081 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO),
1082 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
1084 rel->retry_timer = rel->expected_delay;
1088 LOG (GNUNET_ERROR_TYPE_INFO, "!!! batch free, ignoring timing\n");
1090 rel->ch->pending_messages--;
1091 if (NULL != copy->chq)
1093 GMT_cancel (copy->chq->tq);
1094 /* copy->q is set to NULL by ch_message_sent */
1096 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
1097 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE %p\n", copy);
1100 if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
1102 struct MeshTunnel3 *t = rel->ch->t;
1103 GMCH_destroy (rel->ch);
1104 GMT_destroy_if_empty (t);
1112 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
1114 * @param ch Channel to mark as ready.
1115 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
1118 channel_confirm (struct MeshChannel *ch, int fwd)
1120 struct MeshChannelReliability *rel;
1121 enum MeshChannelState oldstate;
1123 rel = fwd ? ch->root_rel : ch->dest_rel;
1126 GNUNET_break (GNUNET_NO != ch->destroy);
1129 LOG (GNUNET_ERROR_TYPE_DEBUG,
1130 " channel confirm %s %s\n",
1131 GM_f2s (fwd), GMCH_2s (ch));
1132 oldstate = ch->state;
1133 ch->state = MESH_CHANNEL_READY;
1135 if (MESH_CHANNEL_READY != oldstate || GNUNET_YES == is_loopback (ch))
1137 rel->client_ready = GNUNET_YES;
1138 LOG (GNUNET_ERROR_TYPE_DEBUG,
1139 " !! retry timer confirm %s\n",
1140 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer, GNUNET_NO));
1141 rel->expected_delay = rel->retry_timer;
1142 if (GMT_get_connections_buffer (ch->t) > 0 || GMT_is_loopback (ch->t))
1143 send_client_ack (ch, fwd);
1145 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1147 GNUNET_SCHEDULER_cancel (rel->retry_task);
1148 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1150 else if (NULL != rel->uniq)
1152 GMT_cancel (rel->uniq->tq);
1153 /* ch_message_sent will free and NULL uniq */
1157 if (GNUNET_NO == is_loopback (ch))
1159 /* We SHOULD have been trying to retransmit this! */
1165 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
1166 if (GNUNET_YES == fwd)
1167 send_ack (ch, GNUNET_NO);
1172 * Save a copy to retransmit in case it gets lost.
1174 * Initializes all needed callbacks and timers.
1176 * @param ch Channel this message goes on.
1177 * @param msg Message to copy.
1178 * @param fwd Is this fwd traffic?
1180 static struct MeshReliableMessage *
1181 channel_save_copy (struct MeshChannel *ch,
1182 const struct GNUNET_MessageHeader *msg,
1185 struct MeshChannelReliability *rel;
1186 struct MeshReliableMessage *copy;
1191 rel = fwd ? ch->root_rel : ch->dest_rel;
1192 mid = rel->mid_send - 1;
1193 type = ntohs (msg->type);
1194 size = ntohs (msg->size);
1196 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u %s\n", mid, GM_m2s (type));
1197 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
1198 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", copy);
1202 memcpy (©[1], msg, size);
1203 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
1204 ch->pending_messages++;
1211 * Create a new channel.
1213 * @param t Tunnel this channel is in.
1214 * @param owner Client that owns the channel, NULL for foreign channels.
1215 * @param lid_root Local ID for root client.
1217 * @return A new initialized channel. NULL on error.
1219 static struct MeshChannel *
1220 channel_new (struct MeshTunnel3 *t,
1221 struct MeshClient *owner,
1222 MESH_ChannelNumber lid_root)
1224 struct MeshChannel *ch;
1226 ch = GNUNET_new (struct MeshChannel);
1228 ch->lid_root = lid_root;
1231 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
1235 ch->gid = GMT_get_next_chid (t);
1236 GML_channel_add (owner, lid_root, ch);
1238 GMT_add_channel (t, ch);
1245 * Handle a loopback message: call the appropriate handler for the message type.
1247 * @param ch Channel this message is on.
1248 * @param msgh Message header.
1249 * @param fwd Is this FWD traffic?
1252 handle_loopback (struct MeshChannel *ch,
1253 const struct GNUNET_MessageHeader *msgh,
1258 type = ntohs (msgh->type);
1259 LOG (GNUNET_ERROR_TYPE_DEBUG,
1260 "Loopback %s %s message!\n",
1261 GM_f2s (fwd), GM_m2s (type));
1265 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1266 /* Don't send hop ACK, wait for client to ACK */
1267 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SEND loopback %u (%u)\n",
1268 ntohl (((struct GNUNET_MESH_Data *) msgh)->mid), ntohs (msgh->size));
1269 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
1272 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
1273 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1276 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1277 GMCH_handle_create (ch->t,
1278 (struct GNUNET_MESH_ChannelCreate *) msgh);
1281 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1282 GMCH_handle_ack (ch,
1283 (struct GNUNET_MESH_ChannelManage *) msgh,
1287 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1288 GMCH_handle_nack (ch);
1291 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1292 GMCH_handle_destroy (ch,
1293 (struct GNUNET_MESH_ChannelManage *) msgh,
1298 GNUNET_break_op (0);
1299 LOG (GNUNET_ERROR_TYPE_DEBUG,
1300 "end-to-end message not known (%u)\n",
1301 ntohs (msgh->type));
1307 /******************************************************************************/
1308 /******************************** API ***********************************/
1309 /******************************************************************************/
1312 * Destroy a channel and free all resources.
1314 * @param ch Channel to destroy.
1317 GMCH_destroy (struct MeshChannel *ch)
1319 struct MeshClient *c;
1323 if (2 == ch->destroy)
1324 return; /* recursive call */
1327 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
1328 GMT_2s (ch->t), ch->gid);
1334 GML_channel_remove (c, ch->lid_root, ch);
1340 GML_channel_remove (c, ch->lid_dest, ch);
1343 channel_rel_free_all (ch->root_rel);
1344 channel_rel_free_all (ch->dest_rel);
1346 GMT_remove_channel (ch->t, ch);
1347 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
1354 * Get the channel's public ID.
1356 * @param ch Channel.
1358 * @return ID used to identify the channel with the remote peer.
1361 GMCH_get_id (const struct MeshChannel *ch)
1368 * Get the channel tunnel.
1370 * @param ch Channel to get the tunnel from.
1372 * @return tunnel of the channel.
1374 struct MeshTunnel3 *
1375 GMCH_get_tunnel (const struct MeshChannel *ch)
1382 * Get free buffer space towards the client on a specific channel.
1384 * @param ch Channel.
1385 * @param fwd Is query about FWD traffic?
1387 * @return Free buffer space [0 - 64]
1390 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
1392 struct MeshChannelReliability *rel;
1394 rel = fwd ? ch->dest_rel : ch->root_rel;
1396 /* If rel is NULL it means that the end is not yet created,
1397 * most probably is a loopback channel at the point of sending
1398 * the ChannelCreate to itself.
1403 return (64 - rel->n_recv);
1408 * Get flow control status of end point: is client allow to send?
1410 * @param ch Channel.
1411 * @param fwd Is query about FWD traffic? (Request root status).
1413 * @return #GNUNET_YES if client is allowed to send us data.
1416 GMCH_get_allowed (struct MeshChannel *ch, int fwd)
1418 struct MeshChannelReliability *rel;
1420 rel = fwd ? ch->root_rel : ch->dest_rel;
1424 /* Probably shutting down: root/dest NULL'ed to mark disconnection */
1425 GNUNET_break (GNUNET_NO != ch->destroy);
1429 return rel->client_allowed;
1434 * Is the root client for this channel on this peer?
1436 * @param ch Channel.
1437 * @param fwd Is this for fwd traffic?
1439 * @return #GNUNET_YES in case it is.
1442 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1444 struct MeshClient *c;
1446 c = fwd ? ch->root : ch->dest;
1452 * Is the destination client for this channel on this peer?
1454 * @param ch Channel.
1455 * @param fwd Is this for fwd traffic?
1457 * @return #GNUNET_YES in case it is.
1460 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1462 struct MeshClient *c;
1464 c = fwd ? ch->dest : ch->root;
1470 * Send an end-to-end ACK message for the most recent in-sequence payload.
1472 * If channel is not reliable, do nothing.
1474 * @param ch Channel this is about.
1475 * @param fwd Is for FWD traffic? (ACK dest->owner)
1478 GMCH_send_data_ack (struct MeshChannel *ch, int fwd)
1480 struct GNUNET_MESH_DataACK msg;
1481 struct MeshChannelReliability *rel;
1482 struct MeshReliableMessage *copy;
1487 if (GNUNET_NO == ch->reliable)
1491 rel = fwd ? ch->dest_rel : ch->root_rel;
1492 ack = rel->mid_recv - 1;
1493 LOG (GNUNET_ERROR_TYPE_INFO, "=> DATA_ACK for %u\n", ack);
1495 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
1496 msg.header.size = htons (sizeof (msg));
1497 msg.chid = htonl (ch->gid);
1499 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1501 if (copy->type != GNUNET_MESSAGE_TYPE_MESH_DATA)
1503 LOG (GNUNET_ERROR_TYPE_DEBUG,
1504 "!! Type %s, expected DATA\n",
1505 GM_m2s (copy->type));
1508 if (copy->mid == ack + 1)
1513 delta = copy->mid - (ack + 1);
1516 mask = 0x1LL << delta;
1517 msg.futures |= mask;
1518 LOG (GNUNET_ERROR_TYPE_DEBUG,
1519 " !! setting bit for %u (delta %u) (%llX) -> %llX\n",
1520 copy->mid, delta, mask, msg.futures);
1522 msg.mid = htonl (ack);
1523 LOG (GNUNET_ERROR_TYPE_DEBUG,
1524 "!!! ACK for %u, futures %llX\n",
1527 GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1528 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1533 * Allow a client to send us more data, in case it was choked.
1535 * @param ch Channel.
1536 * @param fwd Is this about FWD traffic? (Root client).
1539 GMCH_allow_client (struct MeshChannel *ch, int fwd)
1541 struct MeshChannelReliability *rel;
1542 unsigned int buffer;
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1546 if (MESH_CHANNEL_READY != ch->state)
1548 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel not ready yet!\n");
1552 if (GNUNET_YES == ch->reliable)
1554 rel = fwd ? ch->root_rel : ch->dest_rel;
1557 GNUNET_break (GNUNET_NO != ch->destroy);
1560 if (NULL != rel->head_sent)
1562 if (64 <= rel->mid_send - rel->head_sent->mid)
1564 LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
1568 LOG (GNUNET_ERROR_TYPE_DEBUG, " gap ok: %u - %u\n",
1569 rel->head_sent->mid, rel->mid_send);
1573 LOG (GNUNET_ERROR_TYPE_DEBUG, " head sent is NULL\n");
1577 if (is_loopback (ch))
1578 buffer = GMCH_get_buffer (ch, fwd);
1580 buffer = GMT_get_connections_buffer (ch->t);
1584 LOG (GNUNET_ERROR_TYPE_DEBUG, " no buffer space.\n");
1588 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space %u, allowing\n", buffer);
1589 send_client_ack (ch, fwd);
1596 * @param ch Channel.
1599 GMCH_debug (struct MeshChannel *ch)
1603 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1606 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1607 GMT_2s (ch->t), ch->gid, ch);
1608 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1609 ch->root, ch->root_rel);
1610 if (NULL != ch->root)
1612 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1613 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1614 ch->root_rel->client_ready ? "YES" : "NO");
1615 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1617 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1618 ch->dest, ch->dest_rel);
1619 if (NULL != ch->dest)
1621 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1622 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1623 ch->dest_rel->client_ready ? "YES" : "NO");
1624 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1630 * Handle an ACK given by a client.
1632 * Mark client as ready and send him any buffered data we could have for him.
1634 * @param ch Channel.
1635 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1638 GMCH_handle_local_ack (struct MeshChannel *ch, int fwd)
1640 struct MeshChannelReliability *rel;
1641 struct MeshClient *c;
1643 rel = fwd ? ch->dest_rel : ch->root_rel;
1644 c = fwd ? ch->dest : ch->root;
1646 rel->client_ready = GNUNET_YES;
1647 send_client_buffered_data (ch, c, fwd);
1648 if (is_loopback (ch))
1650 unsigned int buffer;
1652 buffer = GMCH_get_buffer (ch, fwd);
1654 GMCH_allow_client (ch, fwd);
1658 GMT_send_connection_acks (ch->t);
1663 * Handle data given by a client.
1665 * Check whether the client is allowed to send in this tunnel, save if channel
1666 * is reliable and send an ACK to the client if there is still buffer space
1669 * @param ch Channel.
1670 * @param c Client which sent the data.
1671 * @param message Message.
1672 * @param fwd Is this a FWD data?
1674 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1677 GMCH_handle_local_data (struct MeshChannel *ch,
1678 struct MeshClient *c,
1679 struct GNUNET_MessageHeader *message,
1682 struct MeshChannelReliability *rel;
1683 struct GNUNET_MESH_Data *payload;
1684 size_t size = ntohs (message->size);
1685 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
1686 unsigned char cbuf[p2p_size];
1688 /* Is the client in the channel? */
1695 GNUNET_break_op (0);
1696 return GNUNET_SYSERR;
1699 rel = fwd ? ch->root_rel : ch->dest_rel;
1701 if (GNUNET_NO == rel->client_allowed)
1703 GNUNET_break_op (0);
1704 return GNUNET_SYSERR;
1707 rel->client_allowed = GNUNET_NO;
1709 /* Ok, everything is correct, send the message. */
1710 payload = (struct GNUNET_MESH_Data *) cbuf;
1711 payload->mid = htonl (rel->mid_send);
1713 memcpy (&payload[1], message, size);
1714 payload->header.size = htons (p2p_size);
1715 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
1716 payload->chid = htonl (ch->gid);
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1718 GMCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1720 if (is_loopback (ch))
1722 if (GMCH_get_buffer (ch, fwd) > 0)
1723 GMCH_allow_client (ch, fwd);
1728 if (GMT_get_connections_buffer (ch->t) > 0)
1730 GMCH_allow_client (ch, fwd);
1738 * Handle a channel destroy requested by a client.
1740 * Destroy the channel and the tunnel in case this was the last channel.
1742 * @param ch Channel.
1743 * @param c Client that requested the destruction (to avoid notifying him).
1744 * @param is_root Is the request coming from root?
1747 GMCH_handle_local_destroy (struct MeshChannel *ch,
1748 struct MeshClient *c,
1751 struct MeshTunnel3 *t;
1753 ch->destroy = GNUNET_YES;
1754 /* Cleanup after the tunnel */
1755 if (GNUNET_NO == is_root && c == ch->dest)
1757 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1758 GML_client_delete_channel (c, ch, ch->lid_dest);
1761 if (GNUNET_YES == is_root && c == ch->root)
1763 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1764 GML_client_delete_channel (c, ch, ch->lid_root);
1769 send_destroy (ch, GNUNET_NO);
1770 if (0 == ch->pending_messages)
1773 GMT_destroy_if_empty (t);
1779 * Handle a channel create requested by a client.
1781 * Create the channel and the tunnel in case this was the first0 channel.
1783 * @param c Client that requested the creation (will be the root).
1784 * @param msg Create Channel message.
1786 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1789 GMCH_handle_local_create (struct MeshClient *c,
1790 struct GNUNET_MESH_ChannelMessage *msg)
1792 struct MeshChannel *ch;
1793 struct MeshTunnel3 *t;
1794 struct MeshPeer *peer;
1795 MESH_ChannelNumber chid;
1797 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1798 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1799 chid = ntohl (msg->channel_id);
1801 /* Sanity check for duplicate channel IDs */
1802 if (NULL != GML_channel_get (c, chid))
1805 return GNUNET_SYSERR;
1808 peer = GMP_get (&msg->peer);
1809 GMP_add_tunnel (peer);
1810 t = GMP_get_tunnel (peer);
1812 if (GMP_get_short_id (peer) == myid)
1814 GMT_change_cstate (t, MESH_TUNNEL3_READY);
1818 /* FIXME change to a tunnel API, eliminate ch <-> peer connection */
1822 /* Create channel */
1823 ch = channel_new (t, c, chid);
1827 return GNUNET_SYSERR;
1829 ch->port = ntohl (msg->port);
1830 channel_set_options (ch, ntohl (msg->opt));
1832 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1833 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
1834 ch->root_rel->ch = ch;
1835 ch->root_rel->retry_timer = MESH_RETRANSMIT_TIME;
1836 ch->root_rel->expected_delay.rel_value_us = 0;
1838 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
1847 * Handler for mesh network payload traffic.
1849 * @param ch Channel for the message.
1850 * @param msg Unencryted data message.
1851 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1852 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1853 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1854 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1857 GMCH_handle_data (struct MeshChannel *ch,
1858 const struct GNUNET_MESH_Data *msg,
1861 struct MeshChannelReliability *rel;
1862 struct MeshClient *c;
1865 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1866 if (GNUNET_SYSERR == fwd)
1868 if (is_loopback (ch))
1870 /* It is a loopback channel after all... */
1874 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1877 /* Initialize FWD/BCK data */
1878 c = fwd ? ch->dest : ch->root;
1879 rel = fwd ? ch->dest_rel : ch->root_rel;
1883 GNUNET_break (GNUNET_NO != ch->destroy);
1887 if (MESH_CHANNEL_READY != ch->state)
1889 if (GNUNET_NO == fwd)
1891 /* If we are the root, this means the other peer has sent traffic before
1892 * receiving our ACK. Even if the SYNACK goes missing, no traffic should
1893 * be sent before the ACK.
1895 GNUNET_break_op (0);
1898 /* If we are the dest, this means that the SYNACK got to the root but
1899 * the ACK went missing. Treat this as an ACK.
1901 channel_confirm (ch, GNUNET_NO);
1904 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1906 mid = ntohl (msg->mid);
1907 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! got mid %u\n", mid);
1909 if (GNUNET_NO == ch->reliable ||
1910 ( !GM_is_pid_bigger (rel->mid_recv, mid) &&
1911 GM_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1913 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u (%u)\n",
1914 mid, ntohs (msg->header.size));
1915 if (GNUNET_YES == ch->reliable)
1917 /* Is this the exact next expected messasge? */
1918 if (mid == rel->mid_recv)
1920 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1922 send_client_data (ch, msg, fwd);
1926 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1927 add_buffered_data (msg, rel);
1932 /* Tunnel is unreliable: send to clients directly */
1933 /* FIXME: accept Out Of Order traffic */
1934 rel->mid_recv = mid + 1;
1935 send_client_data (ch, msg, fwd);
1940 GNUNET_break_op (GM_is_pid_bigger (rel->mid_recv, mid));
1941 LOG (GNUNET_ERROR_TYPE_WARNING,
1942 "MID %u not expected (%u - %u), dropping!\n",
1943 mid, rel->mid_recv, rel->mid_recv + 63);
1946 GMCH_send_data_ack (ch, fwd);
1951 * Handler for mesh network traffic end-to-end ACKs.
1953 * @param ch Channel on which we got this message.
1954 * @param msg Data message.
1955 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1956 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1957 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1958 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1961 GMCH_handle_data_ack (struct MeshChannel *ch,
1962 const struct GNUNET_MESH_DataACK *msg,
1965 struct MeshChannelReliability *rel;
1966 struct MeshReliableMessage *copy;
1967 struct MeshReliableMessage *next;
1971 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1972 if (GNUNET_SYSERR == fwd)
1974 if (is_loopback (ch))
1976 /* It is a loopback channel after all... */
1980 /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
1981 fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
1984 ack = ntohl (msg->mid);
1985 LOG (GNUNET_ERROR_TYPE_INFO, "<= %s ACK %u\n", GM_f2s (fwd), ack);
1987 if (GNUNET_YES == fwd)
1997 GNUNET_break_op (GNUNET_NO != ch->destroy);
2001 /* Free ACK'd copies: no need to retransmit those anymore FIXME refactor */
2002 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
2004 if (GM_is_pid_bigger (copy->mid, ack))
2006 LOG (GNUNET_ERROR_TYPE_DEBUG, " head %u, out!\n", copy->mid);
2007 channel_rel_free_sent (rel, msg);
2011 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %u\n", copy->mid);
2013 if (GNUNET_YES == rel_message_free (copy, GNUNET_YES))
2017 /* ACK client if needed and possible */
2018 GMCH_allow_client (ch, fwd);
2020 /* If some message was free'd, update the retransmission delay */
2021 if (GNUNET_YES == work)
2023 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
2025 GNUNET_SCHEDULER_cancel (rel->retry_task);
2026 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2027 if (NULL != rel->head_sent && NULL == rel->head_sent->chq)
2029 struct GNUNET_TIME_Absolute new_target;
2030 struct GNUNET_TIME_Relative delay;
2032 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
2033 MESH_RETRANSMIT_MARGIN);
2034 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
2036 delay = GNUNET_TIME_absolute_get_remaining (new_target);
2038 GNUNET_SCHEDULER_add_delayed (delay,
2039 &channel_retransmit_message,
2045 /* Work was done but no task was pending? Shouldn't happen! */
2053 * Handler for channel create messages.
2055 * Does not have fwd parameter because it's always 'FWD': channel is incoming.
2057 * @param t Tunnel this channel will be in.
2058 * @param msg Channel crate message.
2060 struct MeshChannel *
2061 GMCH_handle_create (struct MeshTunnel3 *t,
2062 const struct GNUNET_MESH_ChannelCreate *msg)
2064 MESH_ChannelNumber chid;
2065 struct MeshChannel *ch;
2066 struct MeshClient *c;
2069 chid = ntohl (msg->chid);
2070 ch = GMT_get_channel (t, chid);
2073 /* Create channel */
2074 ch = channel_new (t, NULL, 0);
2076 channel_set_options (ch, ntohl (msg->opt));
2077 new_channel = GNUNET_YES;
2081 new_channel = GNUNET_NO;
2084 if (GNUNET_YES == new_channel || GMT_is_loopback (t))
2086 /* Find a destination client */
2087 ch->port = ntohl (msg->port);
2088 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", ch->port);
2089 c = GML_client_get_by_port (ch->port);
2092 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
2093 if (is_loopback (ch))
2095 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback: destroy on handler\n");
2100 LOG (GNUNET_ERROR_TYPE_DEBUG, " not loopback: destroy now\n");
2108 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
2111 add_destination (ch, c);
2112 if (GNUNET_YES == ch->reliable)
2113 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
2115 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
2117 send_client_create (ch);
2118 ch->state = MESH_CHANNEL_SENT;
2122 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate create channel\n");
2124 send_ack (ch, GNUNET_YES);
2131 * Handler for channel NACK messages.
2133 * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
2135 * @param ch Channel.
2138 GMCH_handle_nack (struct MeshChannel *ch)
2140 send_client_nack (ch);
2146 * Handler for channel ack messages.
2148 * @param ch Channel.
2149 * @param msg Message.
2150 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2151 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2152 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2153 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2156 GMCH_handle_ack (struct MeshChannel *ch,
2157 const struct GNUNET_MESH_ChannelManage *msg,
2160 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2161 if (GNUNET_SYSERR == fwd)
2163 if (is_loopback (ch))
2165 /* It is a loopback channel after all... */
2169 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2172 channel_confirm (ch, !fwd);
2177 * Handler for channel destroy messages.
2179 * @param ch Channel to be destroyed of.
2180 * @param msg Message.
2181 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2182 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2183 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2184 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2187 GMCH_handle_destroy (struct MeshChannel *ch,
2188 const struct GNUNET_MESH_ChannelManage *msg,
2191 struct MeshTunnel3 *t;
2193 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2194 if (GNUNET_SYSERR == fwd)
2196 if (is_loopback (ch))
2198 /* It is a loopback channel after all... */
2202 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2206 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
2208 /* Not for us (don't destroy twice a half-open loopback channel) */
2213 send_destroy (ch, GNUNET_YES);
2215 GMT_destroy_if_empty (t);
2220 * Sends an already built message on a channel.
2222 * If the channel is on a loopback tunnel, notifies the appropriate destination
2225 * On a normal channel passes the message to the tunnel for encryption and
2226 * sending on a connection.
2228 * This function DOES NOT save the message for retransmission.
2230 * @param message Message to send. Function makes a copy of it.
2231 * @param ch Channel on which this message is transmitted.
2232 * @param fwd Is this a fwd message?
2233 * @param existing_copy This is a retransmission, don't save a copy.
2236 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2237 struct MeshChannel *ch, int fwd,
2238 void *existing_copy)
2240 struct MeshChannelQueue *chq;
2243 type = ntohs (message->type);
2244 LOG (GNUNET_ERROR_TYPE_INFO, "=> %s %s on channel %s\n",
2245 GM_m2s (type), GM_f2s (fwd), GMCH_2s (ch));
2247 if (GMT_is_loopback (ch->t))
2249 handle_loopback (ch, message, fwd);
2255 struct GNUNET_MESH_Data *payload;
2256 case GNUNET_MESSAGE_TYPE_MESH_DATA:
2258 payload = (struct GNUNET_MESH_Data *) message;
2259 LOG (GNUNET_ERROR_TYPE_INFO, "=> %s %u\n",
2260 GM_m2s (type), ntohl(payload->mid));
2261 if (GNUNET_YES == ch->reliable)
2263 chq = GNUNET_new (struct MeshChannelQueue);
2265 if (NULL == existing_copy)
2266 chq->copy = channel_save_copy (ch, message, fwd);
2269 chq->copy = (struct MeshReliableMessage *) existing_copy;
2270 if (NULL != chq->copy->chq)
2272 /* Last retransmission was queued but not yet sent!
2273 * This retransmission was scheduled by a ch_message_sent which
2274 * followed a very fast RTT, so the tiny delay made the
2275 * retransmission function to execute before the previous
2276 * retransmitted message even had a chance to leave the peer.
2277 * Cancel this message and wait until the pending
2278 * retransmission leaves the peer and ch_message_sent starts
2279 * the timer for the next one.
2282 LOG (GNUNET_ERROR_TYPE_DEBUG,
2283 " exisitng copy not yet transmitted!\n");
2286 LOG (GNUNET_ERROR_TYPE_DEBUG,
2287 " using existing copy: %p {r:%p q:%p t:%u}\n",
2289 chq->copy->rel, chq->copy->chq, chq->copy->type);
2291 LOG (GNUNET_ERROR_TYPE_DEBUG, " new chq: %p\n", chq);
2292 chq->copy->chq = chq;
2293 chq->tq = GMT_send_prebuilt_message (message, ch->t, NULL,
2294 NULL != existing_copy,
2295 &ch_message_sent, chq);
2296 /* q itself is stored in copy */
2297 GNUNET_assert (NULL != chq->tq || GNUNET_NO != ch->destroy);
2301 fire_and_forget (message, ch, GNUNET_NO);
2306 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
2307 if (GNUNET_YES == fwd)
2309 /* BCK ACK (going FWD) is just a response for a SYNACK, don't keep*/
2310 fire_and_forget (message, ch, GNUNET_YES);
2314 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2315 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
2316 chq = GNUNET_new (struct MeshChannelQueue);
2318 chq->rel = fwd ? ch->root_rel : ch->dest_rel;
2319 if (NULL != chq->rel->uniq)
2321 if (NULL != chq->rel->uniq->tq)
2323 GMT_cancel (chq->rel->uniq->tq);
2324 /* ch_message_sent is called, freeing and NULLing uniq */
2329 GNUNET_free (chq->rel->uniq);
2332 chq->tq = GMT_send_prebuilt_message (message, ch->t, NULL, GNUNET_YES,
2333 &ch_message_sent, chq);
2334 if (NULL == chq->tq)
2341 chq->rel->uniq = chq;
2345 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
2346 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
2347 fire_and_forget (message, ch, GNUNET_YES);
2353 LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s unknown!\n", GM_m2s (type));
2354 fire_and_forget (message, ch, GNUNET_YES);
2360 * Get the static string for identification of the channel.
2362 * @param ch Channel.
2364 * @return Static string with the channel IDs.
2367 GMCH_2s (const struct MeshChannel *ch)
2369 static char buf[64];
2372 return "(NULL Channel)";
2374 sprintf (buf, "%s:%u gid:%X (%X / %X)",
2375 GMT_2s (ch->t), ch->port, ch->gid, ch->lid_root, ch->lid_dest);