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 *q;
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 *q;
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;
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 " sending %s ack to client on channel %s\n",
601 GM_f2s (fwd), GMCH_2s (ch));
609 if (GNUNET_YES == rel->client_allowed)
611 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
614 rel->client_allowed = GNUNET_YES;
616 GML_send_ack (fwd ? ch->root : ch->dest, fwd ? ch->lid_root : ch->lid_dest);
621 * Notify the root that the destination rejected the channel.
623 * @param ch Rejected channel.
626 send_client_nack (struct MeshChannel *ch)
628 if (NULL == ch->root)
633 GML_send_nack (ch->root, ch->lid_root);
638 * We haven't received an ACK after a certain time: restransmit the message.
640 * @param cls Closure (MeshChannelReliability with the message to restransmit)
641 * @param tc TaskContext.
644 channel_retransmit_message (void *cls,
645 const struct GNUNET_SCHEDULER_TaskContext *tc)
647 struct MeshChannelReliability *rel = cls;
648 struct MeshReliableMessage *copy;
649 struct MeshChannel *ch;
650 struct GNUNET_MESH_Data *payload;
653 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
654 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
658 copy = rel->head_sent;
665 payload = (struct GNUNET_MESH_Data *) ©[1];
666 fwd = (rel == ch->root_rel);
668 /* Message not found in the queue that we are going to use. */
669 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
671 GMCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
672 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
677 * We haven't received an Channel ACK after a certain time: resend the CREATE.
679 * @param cls Closure (MeshChannelReliability of the channel to recreate)
680 * @param tc TaskContext.
683 channel_recreate (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
685 struct MeshChannelReliability *rel = cls;
687 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
688 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
691 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RE-CREATE\n");
692 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
694 if (rel == rel->ch->root_rel)
696 send_create (rel->ch);
698 else if (rel == rel->ch->dest_rel)
700 send_ack (rel->ch, GNUNET_YES);
711 * Message has been sent: start retransmission timer.
713 * @param cls Closure (queue structure).
715 * @param q Queue handler (no longer valid).
716 * @param type Type of message.
717 * @param size Size of the message.
720 ch_message_sent (void *cls,
721 struct MeshTunnel3 *t,
722 struct MeshTunnel3Queue *q,
723 uint16_t type, size_t size)
725 struct MeshChannelQueue *ch_q = cls;
726 struct MeshReliableMessage *copy = ch_q->copy;
727 struct MeshChannelReliability *rel;
731 case GNUNET_MESSAGE_TYPE_MESH_DATA:
732 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT DATA MID %u\n", copy->mid);
733 GNUNET_assert (ch_q == copy->q);
734 copy->timestamp = GNUNET_TIME_absolute_get ();
736 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
738 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! scheduling retry %u\n");
739 if (0 != rel->expected_delay.rel_value_us)
741 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay != 0\n");
743 GNUNET_TIME_relative_multiply (rel->expected_delay,
744 MESH_RETRANSMIT_MARGIN);
748 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay 0\n");
749 rel->retry_timer = MESH_RETRANSMIT_TIME;
751 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! using delay %s\n",
752 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
755 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
756 &channel_retransmit_message, rel);
760 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! retry task %u\n", rel->retry_task);
766 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
767 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
768 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
769 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT %s\n", GM_m2s (ch_q->type));
771 GNUNET_assert (rel->uniq == ch_q);
774 if (MESH_CHANNEL_READY != rel->ch->state
775 && GNUNET_MESSAGE_TYPE_MESH_DATA_ACK != type)
777 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rel->retry_task);
778 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! STD BACKOFF %s\n",
779 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
781 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
782 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
783 &channel_recreate, rel);
797 * send a channel create message.
799 * @param ch Channel for which to send.
802 send_create (struct MeshChannel *ch)
804 struct GNUNET_MESH_ChannelCreate msgcc;
806 msgcc.header.size = htons (sizeof (msgcc));
807 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
808 msgcc.chid = htonl (ch->gid);
809 msgcc.port = htonl (ch->port);
810 msgcc.opt = htonl (channel_get_options (ch));
812 GMCH_send_prebuilt_message (&msgcc.header, ch, GNUNET_YES, NULL);
817 * Confirm we got a channel create or FWD ack.
819 * @param ch The channel to confirm.
820 * @param fwd Should we send a FWD ACK? (going dest->root)
823 send_ack (struct MeshChannel *ch, int fwd)
825 struct GNUNET_MESH_ChannelManage msg;
827 msg.header.size = htons (sizeof (msg));
828 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
829 LOG (GNUNET_ERROR_TYPE_DEBUG,
830 " sending channel %s ack for channel %s\n",
831 GM_f2s (fwd), GMCH_2s (ch));
833 msg.chid = htonl (ch->gid);
834 GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
839 * Send a message and don't keep any info about it: we won't need to cancel it
842 * @param msg Header of the message to fire away.
843 * @param ch Channel on which the message should go.
844 * @param force Is this a forced (undroppable) message?
847 fire_and_forget (const struct GNUNET_MessageHeader *msg,
848 struct MeshChannel *ch,
851 GNUNET_break (NULL == GMT_send_prebuilt_message (msg, ch->t, ch, force,
857 * Notify that a channel create didn't succeed.
859 * @param ch The channel to reject.
862 send_nack (struct MeshChannel *ch)
864 struct GNUNET_MESH_ChannelManage msg;
866 msg.header.size = htons (sizeof (msg));
867 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK);
868 LOG (GNUNET_ERROR_TYPE_DEBUG,
869 " sending channel NACK for channel %s\n",
872 msg.chid = htonl (ch->gid);
873 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
878 * Notify a client that the channel is no longer valid.
880 * @param ch Channel that is destroyed.
881 * @param local_only Should we avoid sending it to other peers?
884 send_destroy (struct MeshChannel *ch, int local_only)
886 struct GNUNET_MESH_ChannelManage msg;
888 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
889 msg.header.size = htons (sizeof (msg));
890 msg.chid = htonl (ch->gid);
892 /* If root is not NULL, notify.
893 * If it's NULL, check lid_root. When a local destroy comes in, root
894 * is set to NULL but lid_root is left untouched. In this case, do nothing,
895 * the client is the one who reuqested the channel to be destroyed.
897 if (NULL != ch->root)
898 GML_send_channel_destroy (ch->root, ch->lid_root);
899 else if (0 == ch->lid_root && GNUNET_NO == local_only)
900 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
902 if (NULL != ch->dest)
903 GML_send_channel_destroy (ch->dest, ch->lid_dest);
904 else if (0 == ch->lid_dest && GNUNET_NO == local_only)
905 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
910 * Destroy all reliable messages queued for a channel,
911 * during a channel destruction.
912 * Frees the reliability structure itself.
914 * @param rel Reliability data for a channel.
917 channel_rel_free_all (struct MeshChannelReliability *rel)
919 struct MeshReliableMessage *copy;
920 struct MeshReliableMessage *next;
925 for (copy = rel->head_recv; NULL != copy; copy = next)
928 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
929 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH RECV %p\n", copy);
932 for (copy = rel->head_sent; NULL != copy; copy = next)
935 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
936 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH %p\n", copy);
939 if (NULL != rel->uniq && NULL != rel->uniq->q)
940 GMT_cancel (rel->uniq->q);
941 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
943 GNUNET_SCHEDULER_cancel (rel->retry_task);
944 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
951 * Mark future messages as ACK'd.
953 * @param rel Reliability data.
954 * @param msg DataACK message with a bitfield of future ACK'd messages.
957 channel_rel_free_sent (struct MeshChannelReliability *rel,
958 const struct GNUNET_MESH_DataACK *msg)
960 struct MeshReliableMessage *copy;
961 struct MeshReliableMessage *next;
968 bitfield = msg->futures;
969 mid = ntohl (msg->mid);
970 LOG (GNUNET_ERROR_TYPE_DEBUG,
971 "!!! free_sent_reliable %u %llX\n",
973 LOG (GNUNET_ERROR_TYPE_DEBUG,
974 " rel %p, head %p\n",
975 rel, rel->head_sent);
976 for (i = 0, copy = rel->head_sent;
977 i < 64 && NULL != copy && 0 != bitfield;
980 LOG (GNUNET_ERROR_TYPE_DEBUG,
981 " trying bit %u (mid %u)\n",
984 if (0 == (bitfield & mask))
987 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
988 /* Bit was set, clear the bit from the bitfield */
991 /* The i-th bit was set. Do we have that copy? */
992 /* Skip copies with mid < target */
993 target = mid + i + 1;
994 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
995 while (NULL != copy && GM_is_pid_bigger (target, copy->mid))
998 /* Did we run out of copies? (previously freed, it's ok) */
1001 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
1005 /* Did we overshoot the target? (previously freed, it's ok) */
1006 if (GM_is_pid_bigger (copy->mid, target))
1008 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
1012 /* Now copy->mid == target, free it */
1014 GNUNET_break (GNUNET_YES != rel_message_free (copy, GNUNET_YES));
1017 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
1022 * Destroy a reliable message after it has been acknowledged, either by
1023 * direct mid ACK or bitfield. Updates the appropriate data structures and
1024 * timers and frees all memory.
1026 * @param copy Message that is no longer needed: remote peer got it.
1027 * @param update_time Is the timing information relevant?
1028 * If this message is ACK in a batch the timing information
1029 * is skewed by the retransmission, count only for the
1030 * retransmitted message.
1032 * @return #GNUNET_YES if channel was destroyed as a result of the call,
1033 * #GNUNET_NO otherwise.
1036 rel_message_free (struct MeshReliableMessage *copy, int update_time)
1038 struct MeshChannelReliability *rel;
1039 struct GNUNET_TIME_Relative time;
1042 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
1045 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
1046 if (0 == rel->expected_delay.rel_value_us)
1047 rel->expected_delay = time;
1050 rel->expected_delay.rel_value_us *= 7;
1051 rel->expected_delay.rel_value_us += time.rel_value_us;
1052 rel->expected_delay.rel_value_us /= 8;
1054 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
1055 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
1056 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
1057 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
1059 rel->retry_timer = rel->expected_delay;
1063 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! batch free, ignoring timing\n");
1065 rel->ch->pending_messages--;
1066 if (NULL != copy->q)
1068 GMT_cancel (copy->q->q);
1069 /* copy->q is set to NULL by ch_message_sent */
1071 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE %p\n", copy);
1075 if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
1077 struct MeshTunnel3 *t = rel->ch->t;
1078 GMCH_destroy (rel->ch);
1079 GMT_destroy_if_empty (t);
1087 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
1089 * @param ch Channel to mark as ready.
1090 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
1093 channel_confirm (struct MeshChannel *ch, int fwd)
1095 struct MeshChannelReliability *rel;
1096 enum MeshChannelState oldstate;
1098 LOG (GNUNET_ERROR_TYPE_DEBUG,
1099 " channel confirm %s %s:%X\n",
1100 GM_f2s (fwd), GMT_2s (ch->t), ch->gid);
1101 oldstate = ch->state;
1102 ch->state = MESH_CHANNEL_READY;
1104 rel = fwd ? ch->root_rel : ch->dest_rel;
1105 rel->client_ready = GNUNET_YES;
1106 LOG (GNUNET_ERROR_TYPE_DEBUG,
1107 " !! retry timer confirm %s\n",
1108 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer, GNUNET_NO));
1109 rel->expected_delay = rel->retry_timer;
1110 if (GMT_get_connections_buffer (ch->t) > 0 || GMT_is_loopback (ch->t))
1111 send_client_ack (ch, fwd);
1113 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1115 GNUNET_SCHEDULER_cancel (rel->retry_task);
1116 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1118 else if (NULL != rel->uniq)
1120 GMT_cancel (rel->uniq->q);
1121 /* ch_sent_message will free and NULL uniq */
1125 if (GNUNET_NO == is_loopback (ch))
1127 /* We SHOULD have been trying to retransmit this! */
1128 GNUNET_break (oldstate == MESH_CHANNEL_READY);
1132 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
1133 if (GNUNET_YES == fwd)
1134 send_ack (ch, GNUNET_NO);
1139 * Save a copy to retransmit in case it gets lost.
1141 * Initializes all needed callbacks and timers.
1143 * @param ch Channel this message goes on.
1144 * @param msg Message to copy.
1145 * @param fwd Is this fwd traffic?
1147 static struct MeshReliableMessage *
1148 channel_save_copy (struct MeshChannel *ch,
1149 const struct GNUNET_MessageHeader *msg,
1152 struct MeshChannelReliability *rel;
1153 struct MeshReliableMessage *copy;
1158 rel = fwd ? ch->root_rel : ch->dest_rel;
1159 mid = rel->mid_send - 1;
1160 type = ntohs (msg->type);
1161 size = ntohs (msg->size);
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u %s\n", mid, GM_m2s (type));
1164 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
1165 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", copy);
1169 memcpy (©[1], msg, size);
1170 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
1171 ch->pending_messages++;
1178 * Create a new channel.
1180 * @param t Tunnel this channel is in.
1181 * @param owner Client that owns the channel, NULL for foreign channels.
1182 * @param lid_root Local ID for root client.
1184 * @return A new initialized channel. NULL on error.
1186 static struct MeshChannel *
1187 channel_new (struct MeshTunnel3 *t,
1188 struct MeshClient *owner,
1189 MESH_ChannelNumber lid_root)
1191 struct MeshChannel *ch;
1193 ch = GNUNET_new (struct MeshChannel);
1195 ch->lid_root = lid_root;
1198 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
1202 ch->gid = GMT_get_next_chid (t);
1203 GML_channel_add (owner, lid_root, ch);
1205 GMT_add_channel (t, ch);
1212 * Handle a loopback message: call the appropriate handler for the message type.
1214 * @param ch Channel this message is on.
1215 * @param msgh Message header.
1216 * @param fwd Is this FWD traffic?
1219 handle_loopback (struct MeshChannel *ch,
1220 const struct GNUNET_MessageHeader *msgh,
1225 type = ntohs (msgh->type);
1226 LOG (GNUNET_ERROR_TYPE_DEBUG,
1227 "Loopback %s %s message!\n",
1228 GM_f2s (fwd), GM_m2s (type));
1232 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1233 /* Don't send hop ACK, wait for client to ACK */
1234 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
1237 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
1238 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1241 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1242 GMCH_handle_create (ch->t,
1243 (struct GNUNET_MESH_ChannelCreate *) msgh);
1246 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1247 GMCH_handle_ack (ch,
1248 (struct GNUNET_MESH_ChannelManage *) msgh,
1252 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1253 GMCH_handle_nack (ch);
1256 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1257 GMCH_handle_destroy (ch,
1258 (struct GNUNET_MESH_ChannelManage *) msgh,
1263 GNUNET_break_op (0);
1264 LOG (GNUNET_ERROR_TYPE_DEBUG,
1265 "end-to-end message not known (%u)\n",
1266 ntohs (msgh->type));
1272 /******************************************************************************/
1273 /******************************** API ***********************************/
1274 /******************************************************************************/
1277 * Destroy a channel and free all resources.
1279 * @param ch Channel to destroy.
1282 GMCH_destroy (struct MeshChannel *ch)
1284 struct MeshClient *c;
1289 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
1290 GMT_2s (ch->t), ch->gid);
1296 GML_channel_remove (c, ch->lid_root, ch);
1302 GML_channel_remove (c, ch->lid_dest, ch);
1305 channel_rel_free_all (ch->root_rel);
1306 channel_rel_free_all (ch->dest_rel);
1308 GMT_remove_channel (ch->t, ch);
1309 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
1318 * @param ch Channel.
1323 GMCH_get_id (const struct MeshChannel *ch)
1330 * Get the channel tunnel.
1332 * @param ch Channel to get the tunnel from.
1334 * @return tunnel of the channel.
1336 struct MeshTunnel3 *
1337 GMCH_get_tunnel (const struct MeshChannel *ch)
1344 * Get free buffer space towards the client on a specific channel.
1346 * @param ch Channel.
1347 * @param fwd Is query about FWD traffic?
1349 * @return Free buffer space [0 - 64]
1352 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
1354 struct MeshChannelReliability *rel;
1356 rel = fwd ? ch->dest_rel : ch->root_rel;
1358 /* If rel is NULL it means that the end is not yet created,
1359 * most probably is a loopback channel at the point of sending
1360 * the ChannelCreate to itself.
1365 return (64 - rel->n_recv);
1370 * Get flow control status of end point: is client allow to send?
1372 * @param ch Channel.
1373 * @param fwd Is query about FWD traffic? (Request root status).
1375 * @return #GNUNET_YES if client is allowed to send us data.
1378 GMCH_get_allowed (struct MeshChannel *ch, int fwd)
1380 struct MeshChannelReliability *rel;
1382 rel = fwd ? ch->root_rel : ch->dest_rel;
1386 /* Probably shutting down: root/dest NULL'ed to mark disconnection */
1387 GNUNET_break (GNUNET_NO != ch->destroy);
1391 return rel->client_allowed;
1396 * Is the root client for this channel on this peer?
1398 * @param ch Channel.
1399 * @param fwd Is this for fwd traffic?
1401 * @return #GNUNET_YES in case it is.
1404 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1406 struct MeshClient *c;
1408 c = fwd ? ch->root : ch->dest;
1414 * Is the destination client for this channel on this peer?
1416 * @param ch Channel.
1417 * @param fwd Is this for fwd traffic?
1419 * @return #GNUNET_YES in case it is.
1422 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1424 struct MeshClient *c;
1426 c = fwd ? ch->dest : ch->root;
1432 * Send an end-to-end ACK message for the most recent in-sequence payload.
1434 * If channel is not reliable, do nothing.
1436 * @param ch Channel this is about.
1437 * @param fwd Is for FWD traffic? (ACK dest->owner)
1440 GMCH_send_data_ack (struct MeshChannel *ch, int fwd)
1442 struct GNUNET_MESH_DataACK msg;
1443 struct MeshChannelReliability *rel;
1444 struct MeshReliableMessage *copy;
1449 if (GNUNET_NO == ch->reliable)
1453 rel = fwd ? ch->dest_rel : ch->root_rel;
1454 ack = rel->mid_recv - 1;
1455 LOG (GNUNET_ERROR_TYPE_DEBUG,
1456 " !! Send DATA_ACK for %u\n",
1459 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
1460 msg.header.size = htons (sizeof (msg));
1461 msg.chid = htonl (ch->gid);
1463 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1465 if (copy->type != GNUNET_MESSAGE_TYPE_MESH_DATA)
1467 LOG (GNUNET_ERROR_TYPE_DEBUG,
1468 "!! Type %s, expected DATA\n",
1469 GM_m2s (copy->type));
1472 if (copy->mid == ack + 1)
1477 delta = copy->mid - (ack + 1);
1480 mask = 0x1LL << delta;
1481 msg.futures |= mask;
1482 LOG (GNUNET_ERROR_TYPE_DEBUG,
1483 " !! setting bit for %u (delta %u) (%llX) -> %llX\n",
1484 copy->mid, delta, mask, msg.futures);
1486 msg.mid = htonl (ack);
1487 LOG (GNUNET_ERROR_TYPE_DEBUG,
1488 "!!! ACK for %u, futures %llX\n",
1491 GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1492 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1497 * Allow a client to send us more data, in case it was choked.
1499 * @param ch Channel.
1500 * @param fwd Is this about FWD traffic? (Root client).
1503 GMCH_allow_client (struct MeshChannel *ch, int fwd)
1505 struct MeshChannelReliability *rel;
1506 unsigned int buffer;
1508 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1510 if (MESH_CHANNEL_READY != ch->state)
1512 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel not ready yet!\n");
1516 if (GNUNET_YES == ch->reliable)
1518 rel = fwd ? ch->root_rel : ch->dest_rel;
1521 GNUNET_break (GNUNET_NO != ch->destroy);
1524 if (NULL != rel->head_sent)
1526 if (64 <= rel->mid_send - rel->head_sent->mid)
1528 LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
1532 LOG (GNUNET_ERROR_TYPE_DEBUG, " gap ok: %u - %u\n",
1533 rel->head_sent->mid, rel->mid_send);
1537 LOG (GNUNET_ERROR_TYPE_DEBUG, " head sent is NULL\n");
1541 if (is_loopback (ch))
1542 buffer = GMCH_get_buffer (ch, fwd);
1544 buffer = GMT_get_connections_buffer (ch->t);
1548 LOG (GNUNET_ERROR_TYPE_DEBUG, " no buffer space.\n");
1552 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space %u, allowing\n", buffer);
1553 send_client_ack (ch, fwd);
1560 * @param ch Channel.
1563 GMCH_debug (struct MeshChannel *ch)
1567 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1570 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1571 GMT_2s (ch->t), ch->gid, ch);
1572 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1573 ch->root, ch->root_rel);
1574 if (NULL != ch->root)
1576 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1577 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1578 ch->root_rel->client_ready ? "YES" : "NO");
1579 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1581 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1582 ch->dest, ch->dest_rel);
1583 if (NULL != ch->dest)
1585 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1586 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1587 ch->dest_rel->client_ready ? "YES" : "NO");
1588 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1594 * Handle an ACK given by a client.
1596 * Mark client as ready and send him any buffered data we could have for him.
1598 * @param ch Channel.
1599 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1602 GMCH_handle_local_ack (struct MeshChannel *ch, int fwd)
1604 struct MeshChannelReliability *rel;
1605 struct MeshClient *c;
1607 rel = fwd ? ch->dest_rel : ch->root_rel;
1608 c = fwd ? ch->dest : ch->root;
1610 rel->client_ready = GNUNET_YES;
1611 send_client_buffered_data (ch, c, fwd);
1612 if (is_loopback (ch))
1614 unsigned int buffer;
1616 buffer = GMCH_get_buffer (ch, fwd);
1618 GMCH_allow_client (ch, fwd);
1622 GMT_send_connection_acks (ch->t);
1627 * Handle data given by a client.
1629 * Check whether the client is allowed to send in this tunnel, save if channel
1630 * is reliable and send an ACK to the client if there is still buffer space
1633 * @param ch Channel.
1634 * @param c Client which sent the data.
1635 * @param message Message.
1636 * @param fwd Is this a FWD data?
1638 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1641 GMCH_handle_local_data (struct MeshChannel *ch,
1642 struct MeshClient *c,
1643 struct GNUNET_MessageHeader *message,
1646 struct MeshChannelReliability *rel;
1647 struct GNUNET_MESH_Data *payload;
1648 size_t size = ntohs (message->size);
1649 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
1650 unsigned char cbuf[p2p_size];
1652 /* Is the client in the channel? */
1659 GNUNET_break_op (0);
1660 return GNUNET_SYSERR;
1663 rel = fwd ? ch->root_rel : ch->dest_rel;
1665 if (GNUNET_NO == rel->client_allowed)
1667 GNUNET_break_op (0);
1668 return GNUNET_SYSERR;
1671 rel->client_allowed = GNUNET_NO;
1673 /* Ok, everything is correct, send the message. */
1674 payload = (struct GNUNET_MESH_Data *) cbuf;
1675 payload->mid = htonl (rel->mid_send);
1677 memcpy (&payload[1], message, size);
1678 payload->header.size = htons (p2p_size);
1679 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
1680 payload->chid = htonl (ch->gid);
1681 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1682 GMCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1684 if (is_loopback (ch))
1686 if (GMCH_get_buffer (ch, fwd) > 0)
1687 GMCH_allow_client (ch, fwd);
1692 if (GMT_get_connections_buffer (ch->t) > 0)
1694 GMCH_allow_client (ch, fwd);
1702 * Handle a channel destroy requested by a client.
1704 * Destroy the channel and the tunnel in case this was the last channel.
1706 * @param ch Channel.
1707 * @param c Client that requested the destruction (to avoid notifying him).
1708 * @param is_root Is the request coming from root?
1711 GMCH_handle_local_destroy (struct MeshChannel *ch,
1712 struct MeshClient *c,
1715 struct MeshTunnel3 *t;
1717 ch->destroy = GNUNET_YES;
1718 /* Cleanup after the tunnel */
1719 if (GNUNET_NO == is_root && c == ch->dest)
1721 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1722 GML_client_delete_channel (c, ch, ch->lid_dest);
1725 if (GNUNET_YES == is_root && c == ch->root)
1727 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1728 GML_client_delete_channel (c, ch, ch->lid_root);
1733 send_destroy (ch, GNUNET_NO);
1734 if (0 == ch->pending_messages)
1737 GMT_destroy_if_empty (t);
1743 * Handle a channel create requested by a client.
1745 * Create the channel and the tunnel in case this was the first0 channel.
1747 * @param c Client that requested the creation (will be the root).
1748 * @param msg Create Channel message.
1750 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1753 GMCH_handle_local_create (struct MeshClient *c,
1754 struct GNUNET_MESH_ChannelMessage *msg)
1756 struct MeshChannel *ch;
1757 struct MeshTunnel3 *t;
1758 struct MeshPeer *peer;
1759 MESH_ChannelNumber chid;
1761 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1762 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1763 chid = ntohl (msg->channel_id);
1765 /* Sanity check for duplicate channel IDs */
1766 if (NULL != GML_channel_get (c, chid))
1769 return GNUNET_SYSERR;
1772 peer = GMP_get (&msg->peer);
1773 GMP_add_tunnel (peer);
1774 t = GMP_get_tunnel (peer);
1776 if (GMP_get_short_id (peer) == myid)
1778 GMT_change_cstate (t, MESH_TUNNEL3_READY);
1785 /* Create channel */
1786 ch = channel_new (t, c, chid);
1790 return GNUNET_SYSERR;
1792 ch->port = ntohl (msg->port);
1793 channel_set_options (ch, ntohl (msg->opt));
1795 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1796 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
1797 ch->root_rel->ch = ch;
1798 ch->root_rel->retry_timer = MESH_RETRANSMIT_TIME;
1799 ch->root_rel->expected_delay.rel_value_us = 0;
1801 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
1810 * Handler for mesh network payload traffic.
1812 * @param ch Channel for the message.
1813 * @param msg Unencryted data message.
1814 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1815 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1816 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1817 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1820 GMCH_handle_data (struct MeshChannel *ch,
1821 const struct GNUNET_MESH_Data *msg,
1824 struct MeshChannelReliability *rel;
1825 struct MeshClient *c;
1828 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1829 if (GNUNET_SYSERR == fwd)
1831 if (is_loopback (ch))
1833 /* It is a loopback channel after all... */
1837 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1840 /* Initialize FWD/BCK data */
1841 c = fwd ? ch->dest : ch->root;
1842 rel = fwd ? ch->dest_rel : ch->root_rel;
1850 if (MESH_CHANNEL_READY != ch->state)
1852 if (GNUNET_NO == fwd)
1854 /* If we are the root, this means the other peer has sent traffic before
1855 * receiving our ACK. Even if the SYNACK goes missing, no traffic should
1856 * be sent before the ACK.
1858 GNUNET_break_op (0);
1861 /* If we are the dest, this means that the SYNACK got to the root but
1862 * the ACK went missing. Treat this as an ACK.
1864 channel_confirm (ch, GNUNET_NO);
1867 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1869 mid = ntohl (msg->mid);
1870 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! got mid %u\n", mid);
1872 if (GNUNET_NO == ch->reliable ||
1873 ( !GM_is_pid_bigger (rel->mid_recv, mid) &&
1874 GM_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1876 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1877 if (GNUNET_YES == ch->reliable)
1879 /* Is this the exact next expected messasge? */
1880 if (mid == rel->mid_recv)
1882 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1884 send_client_data (ch, msg, fwd);
1888 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1889 add_buffered_data (msg, rel);
1894 /* Tunnel is unreliable: send to clients directly */
1895 /* FIXME: accept Out Of Order traffic */
1896 rel->mid_recv = mid + 1;
1897 send_client_data (ch, msg, fwd);
1902 GNUNET_break_op (GM_is_pid_bigger (rel->mid_recv, mid));
1903 LOG (GNUNET_ERROR_TYPE_DEBUG,
1904 " !!! MID %u not expected (%u - %u), dropping!\n",
1905 mid, rel->mid_recv, rel->mid_recv + 63);
1908 GMCH_send_data_ack (ch, fwd);
1913 * Handler for mesh network traffic end-to-end ACKs.
1915 * @param ch Channel on which we got this message.
1916 * @param msg Data message.
1917 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1918 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1919 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1920 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1923 GMCH_handle_data_ack (struct MeshChannel *ch,
1924 const struct GNUNET_MESH_DataACK *msg,
1927 struct MeshChannelReliability *rel;
1928 struct MeshReliableMessage *copy;
1929 struct MeshReliableMessage *next;
1933 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1934 if (GNUNET_SYSERR == fwd)
1936 if (is_loopback (ch))
1938 /* It is a loopback channel after all... */
1942 /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
1943 fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
1946 ack = ntohl (msg->mid);
1947 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1948 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1950 if (GNUNET_YES == fwd)
1960 GNUNET_break_op (0);
1964 /* Free ACK'd copies: no need to retransmit those anymore */
1965 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1967 if (GM_is_pid_bigger (copy->mid, ack))
1969 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1970 channel_rel_free_sent (rel, msg);
1974 LOG (GNUNET_ERROR_TYPE_DEBUG, " !! id %u\n", copy->mid);
1976 if (GNUNET_YES == rel_message_free (copy, GNUNET_YES))
1980 /* ACK client if needed */
1981 GMCH_allow_client (ch, fwd);
1983 /* If some message was free'd, update the retransmission delay */
1984 if (GNUNET_YES == work)
1986 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1988 GNUNET_SCHEDULER_cancel (rel->retry_task);
1989 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1990 if (NULL != rel->head_sent && NULL == rel->head_sent->q)
1992 struct GNUNET_TIME_Absolute new_target;
1993 struct GNUNET_TIME_Relative delay;
1995 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1996 MESH_RETRANSMIT_MARGIN);
1997 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1999 delay = GNUNET_TIME_absolute_get_remaining (new_target);
2001 GNUNET_SCHEDULER_add_delayed (delay,
2002 &channel_retransmit_message,
2006 else /* work was done but no task was pending? shouldn't happen! */
2015 * Handler for channel create messages.
2017 * Does not have fwd parameter because it's always 'FWD': channel is incoming.
2019 * @param t Tunnel this channel will be in.
2020 * @param msg Channel crate message.
2022 struct MeshChannel *
2023 GMCH_handle_create (struct MeshTunnel3 *t,
2024 const struct GNUNET_MESH_ChannelCreate *msg)
2026 MESH_ChannelNumber chid;
2027 struct MeshChannel *ch;
2028 struct MeshClient *c;
2031 chid = ntohl (msg->chid);
2032 ch = GMT_get_channel (t, chid);
2035 /* Create channel */
2036 ch = channel_new (t, NULL, 0);
2038 channel_set_options (ch, ntohl (msg->opt));
2039 new_channel = GNUNET_YES;
2043 new_channel = GNUNET_NO;
2046 if (GNUNET_YES == new_channel || GMT_is_loopback (t))
2048 /* Find a destination client */
2049 ch->port = ntohl (msg->port);
2050 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", ch->port);
2051 c = GML_client_get_by_port (ch->port);
2054 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
2055 if (is_loopback (ch))
2057 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback: destroy on handler\n");
2062 LOG (GNUNET_ERROR_TYPE_DEBUG, " not loopback: destroy now\n");
2070 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
2073 add_destination (ch, c);
2074 if (GNUNET_YES == ch->reliable)
2075 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
2077 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
2079 send_client_create (ch);
2080 ch->state = MESH_CHANNEL_SENT;
2084 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate create channel\n");
2086 send_ack (ch, GNUNET_YES);
2093 * Handler for channel NACK messages.
2095 * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
2097 * @param ch Channel.
2100 GMCH_handle_nack (struct MeshChannel *ch)
2102 send_client_nack (ch);
2108 * Handler for channel ack messages.
2110 * @param ch Channel.
2111 * @param msg Message.
2112 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2113 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2114 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2115 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2118 GMCH_handle_ack (struct MeshChannel *ch,
2119 const struct GNUNET_MESH_ChannelManage *msg,
2122 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2123 if (GNUNET_SYSERR == fwd)
2125 if (is_loopback (ch))
2127 /* It is a loopback channel after all... */
2131 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2134 channel_confirm (ch, !fwd);
2139 * Handler for channel destroy messages.
2141 * @param ch Channel to be destroyed of.
2142 * @param msg Message.
2143 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2144 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2145 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2146 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2149 GMCH_handle_destroy (struct MeshChannel *ch,
2150 const struct GNUNET_MESH_ChannelManage *msg,
2153 struct MeshTunnel3 *t;
2155 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2156 if (GNUNET_SYSERR == fwd)
2158 if (is_loopback (ch))
2160 /* It is a loopback channel after all... */
2164 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2168 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
2170 /* Not for us (don't destroy twice a half-open loopback channel) */
2175 send_destroy (ch, GNUNET_YES);
2177 GMT_destroy_if_empty (t);
2182 * Sends an already built message on a channel.
2184 * If the channel is on a loopback tunnel, notifies the appropriate destination
2187 * On a normal channel passes the message to the tunnel for encryption and
2188 * sending on a connection.
2190 * This function DOES NOT save the message for retransmission.
2192 * @param message Message to send. Function makes a copy of it.
2193 * @param ch Channel on which this message is transmitted.
2194 * @param fwd Is this a fwd message?
2195 * @param existing_copy This is a retransmission, don't save a copy.
2198 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2199 struct MeshChannel *ch, int fwd,
2200 void *existing_copy)
2202 struct MeshChannelQueue *q;
2205 type = ntohs (message->type);
2206 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH Send %s %s on channel %s\n",
2207 GM_f2s (fwd), GM_m2s (type),
2210 if (GMT_is_loopback (ch->t))
2212 handle_loopback (ch, message, fwd);
2218 case GNUNET_MESSAGE_TYPE_MESH_DATA:
2220 if (GNUNET_YES == ch->reliable)
2222 q = GNUNET_new (struct MeshChannelQueue);
2224 if (NULL == existing_copy)
2225 q->copy = channel_save_copy (ch, message, fwd);
2228 q->copy = (struct MeshReliableMessage *) existing_copy;
2229 if (NULL != q->copy->q)
2231 /* Last retransmission was queued but not yet sent!
2232 * This retransmission was scheduled by a ch_message_sent which
2233 * followed a very fast RTT, so the tiny delay made the
2234 * retransmission function to execute before the previous
2235 * retransmitted message even had a chance to leave the peer.
2236 * Cancel this message and wait until the pending
2237 * retransmission leaves the peer and ch_message_sent starts
2238 * the timer for the next one.
2241 LOG (GNUNET_ERROR_TYPE_DEBUG,
2242 " exisitng copy not yet transmitted!\n");
2245 LOG (GNUNET_ERROR_TYPE_DEBUG,
2246 " using existing copy: %p {r:%p q:%p t:%u}\n",
2248 q->copy->rel, q->copy->q, q->copy->type);
2250 LOG (GNUNET_ERROR_TYPE_DEBUG, " new q: %p\n", q);
2252 q->q = GMT_send_prebuilt_message (message, ch->t, ch,
2253 NULL != existing_copy,
2254 &ch_message_sent, q);
2255 /* q itself is stored in copy */
2259 fire_and_forget (message, ch, GNUNET_NO);
2264 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
2265 if (GNUNET_YES == fwd)
2267 /* BCK ACK (going FWD) is just a response for a SYNACK, don't keep*/
2268 fire_and_forget (message, ch, GNUNET_YES);
2272 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2273 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
2274 q = GNUNET_new (struct MeshChannelQueue);
2276 q->rel = fwd ? ch->root_rel : ch->dest_rel;
2277 if (NULL != q->rel->uniq)
2279 if (NULL != q->rel->uniq->q)
2281 GMT_cancel (q->rel->uniq->q);
2282 /* ch_message_sent is called, freeing and NULLing uniq */
2286 GNUNET_free (q->rel->uniq);
2289 q->q = GMT_send_prebuilt_message (message, ch->t, ch, GNUNET_YES,
2290 &ch_message_sent, q);
2295 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
2296 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
2297 fire_and_forget (message, ch, GNUNET_YES);
2303 LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s unknown!\n", GM_m2s (type));
2304 fire_and_forget (message, ch, GNUNET_YES);
2310 * Get the static string for identification of the channel.
2312 * @param ch Channel.
2314 * @return Static string with the channel IDs.
2317 GMCH_2s (const struct MeshChannel *ch)
2319 static char buf[64];
2322 return "(NULL Channel)";
2324 sprintf (buf, "%s:%u gid:%X (%X / %X)",
2325 GMT_2s (ch->t), ch->port, ch->gid, ch->lid_root, ch->lid_dest);