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 * We have received a message out of order, or the client is not ready.
343 * Buffer it until we receive an ACK from the client or the missing
344 * message from the channel.
346 * @param msg Message to buffer (MUST be of type MESH_DATA).
347 * @param rel Reliability data to the corresponding direction.
350 add_buffered_data (const struct GNUNET_MESH_Data *msg,
351 struct MeshChannelReliability *rel)
353 struct MeshReliableMessage *copy;
354 struct MeshReliableMessage *prev;
358 size = ntohs (msg->header.size);
359 mid = ntohl (msg->mid);
361 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
363 copy = GNUNET_malloc (sizeof (*copy) + size);
366 copy->type = GNUNET_MESSAGE_TYPE_MESH_DATA;
367 memcpy (©[1], msg, size);
371 // FIXME do something better than O(n), although n < 64...
372 // FIXME start from the end (most messages are the latest ones)
373 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
375 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
376 if (GM_is_pid_bigger (prev->mid, mid))
378 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
379 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
384 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
385 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
386 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
390 * Add a destination client to a channel, initializing all data structures
391 * in the channel and the client.
393 * @param ch Channel to which add the destination.
394 * @param c Client which to add to the channel.
397 add_destination (struct MeshChannel *ch, struct MeshClient *c)
399 if (NULL != ch->dest)
405 /* Assign local id as destination */
406 ch->lid_dest = GML_get_next_chid (c);
408 /* Store in client's hashmap */
409 GML_channel_add (c, ch->lid_dest, ch);
411 GNUNET_break (NULL == ch->dest_rel);
412 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
413 ch->dest_rel->ch = ch;
414 ch->dest_rel->expected_delay.rel_value_us = 0;
415 ch->dest_rel->retry_timer = MESH_RETRANSMIT_TIME;
422 * Set options in a channel, extracted from a bit flag field.
424 * @param ch Channel to set options to.
425 * @param options Bit array in host byte order.
428 channel_set_options (struct MeshChannel *ch, uint32_t options)
430 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
431 GNUNET_YES : GNUNET_NO;
432 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
433 GNUNET_YES : GNUNET_NO;
438 * Get a bit flag field with the options of a channel.
440 * @param ch Channel to get options from.
442 * @return Bit array in host byte order.
445 channel_get_options (struct MeshChannel *ch)
451 options |= GNUNET_MESH_OPTION_NOBUFFER;
453 options |= GNUNET_MESH_OPTION_RELIABLE;
460 * Notify the destination client that a new incoming channel was created.
462 * @param ch Channel that was created.
465 send_client_create (struct MeshChannel *ch)
469 if (NULL == ch->dest)
473 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
474 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
475 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
476 GMT_get_destination (ch->t));
482 * Send data to a client.
484 * If the client is ready, send directly, otherwise buffer while listening
488 * @param msg Message.
489 * @param fwd Is this a fwd (root->dest) message?
492 send_client_data (struct MeshChannel *ch,
493 const struct GNUNET_MESH_Data *msg,
498 if (ch->dest_rel->client_ready)
499 GML_send_data (ch->dest, msg, ch->lid_dest);
501 add_buffered_data (msg, ch->dest_rel);
505 if (ch->root_rel->client_ready)
506 GML_send_data (ch->root, msg, ch->lid_root);
508 add_buffered_data (msg, ch->root_rel);
514 * Send a buffered message to the client, for in order delivery or
515 * as result of client ACK.
517 * @param ch Channel on which to empty the message buffer.
518 * @param c Client to send to.
519 * @param fwd Is this to send FWD data?.
522 send_client_buffered_data (struct MeshChannel *ch,
523 struct MeshClient *c,
526 struct MeshReliableMessage *copy;
527 struct MeshChannelReliability *rel;
529 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
530 rel = fwd ? ch->dest_rel : ch->root_rel;
531 if (GNUNET_NO == rel->client_ready)
533 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
537 copy = rel->head_recv;
538 /* We never buffer channel management messages */
541 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
543 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
545 LOG (GNUNET_ERROR_TYPE_DEBUG,
546 " have %u! now expecting %u\n",
547 copy->mid, rel->mid_recv + 1);
548 send_client_data (ch, msg, fwd);
551 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
552 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE RECV %p\n", copy);
557 LOG (GNUNET_ERROR_TYPE_DEBUG,
558 " reliable && don't have %u, next is %u\n",
564 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
569 * Allow a client to send more data.
571 * In case the client was already allowed to send data, do nothing.
574 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
577 send_client_ack (struct MeshChannel *ch, int fwd)
579 struct MeshChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
581 LOG (GNUNET_ERROR_TYPE_DEBUG,
582 " sending %s ack to client on channel %s\n",
583 GM_f2s (fwd), GMCH_2s (ch));
591 if (GNUNET_YES == rel->client_allowed)
593 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
596 rel->client_allowed = GNUNET_YES;
598 GML_send_ack (fwd ? ch->root : ch->dest, fwd ? ch->lid_root : ch->lid_dest);
603 * Notify the root that the destination rejected the channel.
605 * @param ch Rejected channel.
608 send_client_nack (struct MeshChannel *ch)
610 if (NULL == ch->root)
615 GML_send_nack (ch->root, ch->lid_root);
620 * We haven't received an ACK after a certain time: restransmit the message.
622 * @param cls Closure (MeshChannelReliability with the message to restransmit)
623 * @param tc TaskContext.
626 channel_retransmit_message (void *cls,
627 const struct GNUNET_SCHEDULER_TaskContext *tc)
629 struct MeshChannelReliability *rel = cls;
630 struct MeshReliableMessage *copy;
631 struct MeshChannel *ch;
632 struct GNUNET_MESH_Data *payload;
635 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
636 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
640 copy = rel->head_sent;
647 payload = (struct GNUNET_MESH_Data *) ©[1];
648 fwd = (rel == ch->root_rel);
650 /* Message not found in the queue that we are going to use. */
651 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
653 GMCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
654 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
659 * We haven't received an Channel ACK after a certain time: resend the CREATE.
661 * @param cls Closure (MeshChannelReliability of the channel to recreate)
662 * @param tc TaskContext.
665 channel_recreate (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
667 struct MeshChannelReliability *rel = cls;
669 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
670 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
673 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RE-CREATE\n");
674 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
676 if (rel == rel->ch->root_rel)
678 send_create (rel->ch);
680 else if (rel == rel->ch->dest_rel)
682 send_ack (rel->ch, GNUNET_YES);
693 * Message has been sent: start retransmission timer.
695 * @param cls Closure (queue structure).
697 * @param q Queue handler (no longer valid).
698 * @param type Type of message.
699 * @param size Size of the message.
702 ch_message_sent (void *cls,
703 struct MeshTunnel3 *t,
704 struct MeshTunnel3Queue *q,
705 uint16_t type, size_t size)
707 struct MeshChannelQueue *ch_q = cls;
708 struct MeshReliableMessage *copy = ch_q->copy;
709 struct MeshChannelReliability *rel;
713 case GNUNET_MESSAGE_TYPE_MESH_DATA:
714 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT DATA MID %u\n", copy->mid);
715 GNUNET_assert (ch_q == copy->q);
716 copy->timestamp = GNUNET_TIME_absolute_get ();
718 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
720 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! scheduling retry %u\n");
721 if (0 != rel->expected_delay.rel_value_us)
723 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay != 0\n");
725 GNUNET_TIME_relative_multiply (rel->expected_delay,
726 MESH_RETRANSMIT_MARGIN);
730 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay 0\n");
731 rel->retry_timer = MESH_RETRANSMIT_TIME;
733 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! using delay %s\n",
734 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
737 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
738 &channel_retransmit_message, rel);
742 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! retry task %u\n", rel->retry_task);
748 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
749 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
750 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
751 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT %s\n", GM_m2s (ch_q->type));
753 GNUNET_assert (rel->uniq == ch_q);
756 if (MESH_CHANNEL_READY != rel->ch->state
757 && GNUNET_MESSAGE_TYPE_MESH_DATA_ACK != type)
759 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rel->retry_task);
760 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! STD BACKOFF %s\n",
761 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
763 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
764 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
765 &channel_recreate, rel);
779 * send a channel create message.
781 * @param ch Channel for which to send.
784 send_create (struct MeshChannel *ch)
786 struct GNUNET_MESH_ChannelCreate msgcc;
788 msgcc.header.size = htons (sizeof (msgcc));
789 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
790 msgcc.chid = htonl (ch->gid);
791 msgcc.port = htonl (ch->port);
792 msgcc.opt = htonl (channel_get_options (ch));
794 GMCH_send_prebuilt_message (&msgcc.header, ch, GNUNET_YES, NULL);
799 * Confirm we got a channel create, FWD ack.
801 * @param ch The channel to confirm.
802 * @param fwd Should we send a FWD ACK? (going dest->root)
805 send_ack (struct MeshChannel *ch, int fwd)
807 struct GNUNET_MESH_ChannelManage msg;
809 msg.header.size = htons (sizeof (msg));
810 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
811 LOG (GNUNET_ERROR_TYPE_DEBUG,
812 " sending channel %s ack for channel %s\n",
813 GM_f2s (fwd), GMCH_2s (ch));
815 msg.chid = htonl (ch->gid);
816 GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
821 * Sent a message and don't keep any info about it: we won't need to cancel it
824 * @param msg Header of the message to fire away.
825 * @param ch Channel on which the message should go.
826 * @param force Is this a forced (undroppable) message?
829 fire_and_forget (const struct GNUNET_MessageHeader *msg,
830 struct MeshChannel *ch,
833 GNUNET_break (NULL == GMT_send_prebuilt_message (msg, ch->t, ch, force,
839 * Notify that a channel create didn't succeed.
841 * @param ch The channel to reject.
844 send_nack (struct MeshChannel *ch)
846 struct GNUNET_MESH_ChannelManage msg;
848 msg.header.size = htons (sizeof (msg));
849 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK);
850 LOG (GNUNET_ERROR_TYPE_DEBUG,
851 " sending channel NACK for channel %s\n",
854 msg.chid = htonl (ch->gid);
855 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
860 * Notify a client that the channel is no longer valid.
862 * @param ch Channel that is destroyed.
863 * @param local_only Should we avoid sending it to other peers?
866 send_destroy (struct MeshChannel *ch, int local_only)
868 struct GNUNET_MESH_ChannelManage msg;
870 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
871 msg.header.size = htons (sizeof (msg));
872 msg.chid = htonl (ch->gid);
874 /* If root is not NULL, notify.
875 * If it's NULL, check lid_root. When a local destroy comes in, root
876 * is set to NULL but lid_root is left untouched. In this case, do nothing,
877 * the client is the one who reuqested the channel to be destroyed.
879 if (NULL != ch->root)
880 GML_send_channel_destroy (ch->root, ch->lid_root);
881 else if (0 == ch->lid_root && GNUNET_NO == local_only)
882 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
884 if (NULL != ch->dest)
885 GML_send_channel_destroy (ch->dest, ch->lid_dest);
886 else if (0 == ch->lid_dest && GNUNET_NO == local_only)
887 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
892 * Destroy all reliable messages queued for a channel,
893 * during a channel destruction.
894 * Frees the reliability structure itself.
896 * @param rel Reliability data for a channel.
899 channel_rel_free_all (struct MeshChannelReliability *rel)
901 struct MeshReliableMessage *copy;
902 struct MeshReliableMessage *next;
907 for (copy = rel->head_recv; NULL != copy; copy = next)
910 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
911 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH RECV %p\n", copy);
914 for (copy = rel->head_sent; NULL != copy; copy = next)
917 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
918 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH %p\n", copy);
921 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
923 GNUNET_SCHEDULER_cancel (rel->retry_task);
925 if (NULL != rel->uniq && NULL != rel->uniq->q)
926 GMT_cancel (rel->uniq->q);
932 * Mark future messages as ACK'd.
934 * @param rel Reliability data.
935 * @param msg DataACK message with a bitfield of future ACK'd messages.
938 channel_rel_free_sent (struct MeshChannelReliability *rel,
939 const struct GNUNET_MESH_DataACK *msg)
941 struct MeshReliableMessage *copy;
942 struct MeshReliableMessage *next;
949 bitfield = msg->futures;
950 mid = ntohl (msg->mid);
951 LOG (GNUNET_ERROR_TYPE_DEBUG,
952 "!!! free_sent_reliable %u %llX\n",
954 LOG (GNUNET_ERROR_TYPE_DEBUG,
955 " rel %p, head %p\n",
956 rel, rel->head_sent);
957 for (i = 0, copy = rel->head_sent;
958 i < 64 && NULL != copy && 0 != bitfield;
961 LOG (GNUNET_ERROR_TYPE_DEBUG,
962 " trying bit %u (mid %u)\n",
965 if (0 == (bitfield & mask))
968 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
969 /* Bit was set, clear the bit from the bitfield */
972 /* The i-th bit was set. Do we have that copy? */
973 /* Skip copies with mid < target */
974 target = mid + i + 1;
975 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
976 while (NULL != copy && GM_is_pid_bigger (target, copy->mid))
979 /* Did we run out of copies? (previously freed, it's ok) */
982 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
986 /* Did we overshoot the target? (previously freed, it's ok) */
987 if (GM_is_pid_bigger (copy->mid, target))
989 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
993 /* Now copy->mid == target, free it */
995 rel_message_free (copy, GNUNET_YES);
998 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
1003 * Destroy a reliable message after it has been acknowledged, either by
1004 * direct mid ACK or bitfield. Updates the appropriate data structures and
1005 * timers and frees all memory.
1007 * @param copy Message that is no longer needed: remote peer got it.
1008 * @param update_time Is the timing information relevant?
1009 * If this message is ACK in a batch the timing information
1010 * is skewed by the retransmission, count only for the
1011 * retransmitted message.
1014 rel_message_free (struct MeshReliableMessage *copy, int update_time)
1016 struct MeshChannelReliability *rel;
1017 struct GNUNET_TIME_Relative time;
1020 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
1023 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
1024 if (0 == rel->expected_delay.rel_value_us)
1025 rel->expected_delay = time;
1028 rel->expected_delay.rel_value_us *= 7;
1029 rel->expected_delay.rel_value_us += time.rel_value_us;
1030 rel->expected_delay.rel_value_us /= 8;
1032 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
1033 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
1034 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
1035 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
1037 rel->retry_timer = rel->expected_delay;
1041 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! batch free, ignoring timing\n");
1043 rel->ch->pending_messages--;
1044 if (NULL != copy->q)
1046 GMT_cancel (copy->q->q);
1047 /* copy->q is set to NULL by ch_message_sent */
1049 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE %p\n", copy);
1053 if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
1055 struct MeshTunnel3 *t = rel->ch->t;
1056 GMCH_destroy (rel->ch);
1057 GMT_destroy_if_empty (t);
1063 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
1065 * @param ch Channel to mark as ready.
1066 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
1069 channel_confirm (struct MeshChannel *ch, int fwd)
1071 struct MeshChannelReliability *rel;
1072 enum MeshChannelState oldstate;
1074 LOG (GNUNET_ERROR_TYPE_DEBUG,
1075 " channel confirm %s %s:%X\n",
1076 GM_f2s (fwd), GMT_2s (ch->t), ch->gid);
1077 oldstate = ch->state;
1078 ch->state = MESH_CHANNEL_READY;
1080 rel = fwd ? ch->root_rel : ch->dest_rel;
1081 rel->client_ready = GNUNET_YES;
1082 LOG (GNUNET_ERROR_TYPE_DEBUG,
1083 " !! retry timer confirm %s\n",
1084 GNUNET_STRINGS_relative_time_to_string (rel->retry_timer, GNUNET_NO));
1085 rel->expected_delay = rel->retry_timer;
1086 if (GMT_get_connections_buffer (ch->t) > 0 || GMT_is_loopback (ch->t))
1087 send_client_ack (ch, fwd);
1089 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1091 GNUNET_SCHEDULER_cancel (rel->retry_task);
1092 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1094 else if (NULL != rel->uniq)
1096 GMT_cancel (rel->uniq->q);
1097 /* ch_sent_message will free and NULL uniq */
1101 /* We SHOULD have been trying to retransmit this! */
1102 GNUNET_break (oldstate == MESH_CHANNEL_READY);
1105 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
1106 if (GNUNET_YES == fwd)
1107 send_ack (ch, GNUNET_NO);
1112 * Save a copy to retransmit in case it gets lost.
1114 * Initializes all needed callbacks and timers.
1116 * @param ch Channel this message goes on.
1117 * @param msg Message to copy.
1118 * @param fwd Is this fwd traffic?
1120 static struct MeshReliableMessage *
1121 channel_save_copy (struct MeshChannel *ch,
1122 const struct GNUNET_MessageHeader *msg,
1125 struct MeshChannelReliability *rel;
1126 struct MeshReliableMessage *copy;
1131 rel = fwd ? ch->root_rel : ch->dest_rel;
1132 mid = rel->mid_send - 1;
1133 type = ntohs (msg->type);
1134 size = ntohs (msg->size);
1136 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u %s\n", mid, GM_m2s (type));
1137 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
1138 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", copy);
1142 memcpy (©[1], msg, size);
1143 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
1144 ch->pending_messages++;
1151 * Create a new channel.
1153 * @param t Tunnel this channel is in.
1154 * @param owner Client that owns the channel, NULL for foreign channels.
1155 * @param lid_root Local ID for root client.
1157 * @return A new initialized channel. NULL on error.
1159 static struct MeshChannel *
1160 channel_new (struct MeshTunnel3 *t,
1161 struct MeshClient *owner,
1162 MESH_ChannelNumber lid_root)
1164 struct MeshChannel *ch;
1166 ch = GNUNET_new (struct MeshChannel);
1168 ch->lid_root = lid_root;
1171 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
1175 ch->gid = GMT_get_next_chid (t);
1176 GML_channel_add (owner, lid_root, ch);
1178 GMT_add_channel (t, ch);
1185 * Test if the channel is loopback: both root and dest are on the local peer.
1187 * @param ch Channel to test.
1189 * @return #GNUNET_YES if channel is loopback, #NGUNET_NO otherwise.
1192 is_loopback (const struct MeshChannel *ch)
1195 return GMT_is_loopback (ch->t);
1197 return (NULL != ch->root && NULL != ch->dest);
1202 * Handle a loopback message: call the appropriate handler for the message type.
1204 * @param ch Channel this message is on.
1205 * @param msgh Message header.
1206 * @param fwd Is this FWD traffic?
1209 handle_loopback (struct MeshChannel *ch,
1210 const struct GNUNET_MessageHeader *msgh,
1215 type = ntohs (msgh->type);
1216 LOG (GNUNET_ERROR_TYPE_DEBUG,
1217 "Loopback %s %s message!\n",
1218 GM_f2s (fwd), GM_m2s (type));
1222 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1223 /* Don't send hop ACK, wait for client to ACK */
1224 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
1227 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
1228 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1231 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1232 GMCH_handle_create (ch->t,
1233 (struct GNUNET_MESH_ChannelCreate *) msgh);
1236 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1237 GMCH_handle_ack (ch,
1238 (struct GNUNET_MESH_ChannelManage *) msgh,
1242 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1243 GMCH_handle_nack (ch);
1246 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1247 GMCH_handle_destroy (ch,
1248 (struct GNUNET_MESH_ChannelManage *) msgh,
1253 GNUNET_break_op (0);
1254 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 "end-to-end message not known (%u)\n",
1256 ntohs (msgh->type));
1262 /******************************************************************************/
1263 /******************************** API ***********************************/
1264 /******************************************************************************/
1267 * Destroy a channel and free all resources.
1269 * @param ch Channel to destroy.
1272 GMCH_destroy (struct MeshChannel *ch)
1274 struct MeshClient *c;
1279 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
1280 GMT_2s (ch->t), ch->gid);
1286 GML_channel_remove (c, ch->lid_root, ch);
1292 GML_channel_remove (c, ch->lid_dest, ch);
1295 channel_rel_free_all (ch->root_rel);
1296 channel_rel_free_all (ch->dest_rel);
1298 GMT_remove_channel (ch->t, ch);
1299 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
1308 * @param ch Channel.
1313 GMCH_get_id (const struct MeshChannel *ch)
1320 * Get the channel tunnel.
1322 * @param ch Channel to get the tunnel from.
1324 * @return tunnel of the channel.
1326 struct MeshTunnel3 *
1327 GMCH_get_tunnel (const struct MeshChannel *ch)
1334 * Get free buffer space towards the client on a specific channel.
1336 * @param ch Channel.
1337 * @param fwd Is query about FWD traffic?
1339 * @return Free buffer space [0 - 64]
1342 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
1344 struct MeshChannelReliability *rel;
1346 rel = fwd ? ch->dest_rel : ch->root_rel;
1348 /* If rel is NULL it means that the end is not yet created,
1349 * most probably is a loopback channel at the point of sending
1350 * the ChannelCreate to itself.
1355 return (64 - rel->n_recv);
1360 * Get flow control status of end point: is client allow to send?
1362 * @param ch Channel.
1363 * @param fwd Is query about FWD traffic? (Request root status).
1365 * @return #GNUNET_YES if client is allowed to send us data.
1368 GMCH_get_allowed (struct MeshChannel *ch, int fwd)
1370 struct MeshChannelReliability *rel;
1372 rel = fwd ? ch->root_rel : ch->dest_rel;
1376 /* Probably shutting down: root/dest NULL'ed to mark disconnection */
1377 GNUNET_break (GNUNET_NO != ch->destroy);
1381 return rel->client_allowed;
1386 * Is the root client for this channel on this peer?
1388 * @param ch Channel.
1389 * @param fwd Is this for fwd traffic?
1391 * @return #GNUNET_YES in case it is.
1394 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1396 struct MeshClient *c;
1398 c = fwd ? ch->root : ch->dest;
1404 * Is the destination client for this channel on this peer?
1406 * @param ch Channel.
1407 * @param fwd Is this for fwd traffic?
1409 * @return #GNUNET_YES in case it is.
1412 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1414 struct MeshClient *c;
1416 c = fwd ? ch->dest : ch->root;
1422 * Send an end-to-end ACK message for the most recent in-sequence payload.
1424 * If channel is not reliable, do nothing.
1426 * @param ch Channel this is about.
1427 * @param fwd Is for FWD traffic? (ACK dest->owner)
1430 GMCH_send_data_ack (struct MeshChannel *ch, int fwd)
1432 struct GNUNET_MESH_DataACK msg;
1433 struct MeshChannelReliability *rel;
1434 struct MeshReliableMessage *copy;
1439 if (GNUNET_NO == ch->reliable)
1443 rel = fwd ? ch->dest_rel : ch->root_rel;
1444 ack = rel->mid_recv - 1;
1445 LOG (GNUNET_ERROR_TYPE_DEBUG,
1446 " !! Send DATA_ACK for %u\n",
1449 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
1450 msg.header.size = htons (sizeof (msg));
1451 msg.chid = htonl (ch->gid);
1453 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1455 if (copy->type != GNUNET_MESSAGE_TYPE_MESH_DATA)
1457 LOG (GNUNET_ERROR_TYPE_DEBUG,
1458 "!! Type %s, expected DATA\n",
1459 GM_m2s (copy->type));
1462 if (copy->mid == ack + 1)
1467 delta = copy->mid - (ack + 1);
1470 mask = 0x1LL << delta;
1471 msg.futures |= mask;
1472 LOG (GNUNET_ERROR_TYPE_DEBUG,
1473 " !! setting bit for %u (delta %u) (%llX) -> %llX\n",
1474 copy->mid, delta, mask, msg.futures);
1476 msg.mid = htonl (ack);
1477 LOG (GNUNET_ERROR_TYPE_DEBUG,
1478 "!!! ACK for %u, futures %llX\n",
1481 GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1482 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1487 * Allow a client to send us more data, in case it was choked.
1489 * @param ch Channel.
1490 * @param fwd Is this about FWD traffic? (Root client).
1493 GMCH_allow_client (struct MeshChannel *ch, int fwd)
1495 struct MeshChannelReliability *rel;
1496 unsigned int buffer;
1498 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1500 if (MESH_CHANNEL_READY != ch->state)
1502 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel not ready yet!\n");
1506 if (GNUNET_YES == ch->reliable)
1508 rel = fwd ? ch->root_rel : ch->dest_rel;
1511 GNUNET_break (GNUNET_NO != ch->destroy);
1514 if (NULL != rel->head_sent && 64 <= rel->mid_send - rel->head_sent->mid)
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
1521 if (is_loopback (ch))
1522 buffer = GMCH_get_buffer (ch, fwd);
1524 buffer = GMT_get_connections_buffer (ch->t);
1528 LOG (GNUNET_ERROR_TYPE_DEBUG, " no buffer space.\n");
1532 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space %u, allowing\n", buffer);
1533 send_client_ack (ch, fwd);
1540 * @param ch Channel.
1543 GMCH_debug (struct MeshChannel *ch)
1547 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1550 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1551 GMT_2s (ch->t), ch->gid, ch);
1552 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1553 ch->root, ch->root_rel);
1554 if (NULL != ch->root)
1556 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1557 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1558 ch->root_rel->client_ready ? "YES" : "NO");
1559 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1561 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1562 ch->dest, ch->dest_rel);
1563 if (NULL != ch->dest)
1565 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1566 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1567 ch->dest_rel->client_ready ? "YES" : "NO");
1568 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1574 * Handle an ACK given by a client.
1576 * Mark client as ready and send him any buffered data we could have for him.
1578 * @param ch Channel.
1579 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1582 GMCH_handle_local_ack (struct MeshChannel *ch, int fwd)
1584 struct MeshChannelReliability *rel;
1585 struct MeshClient *c;
1587 rel = fwd ? ch->dest_rel : ch->root_rel;
1588 c = fwd ? ch->dest : ch->root;
1590 rel->client_ready = GNUNET_YES;
1591 send_client_buffered_data (ch, c, fwd);
1592 if (is_loopback (ch))
1594 unsigned int buffer;
1596 buffer = GMCH_get_buffer (ch, fwd);
1598 GMCH_allow_client (ch, fwd);
1602 GMT_send_connection_acks (ch->t);
1607 * Handle data given by a client.
1609 * Check whether the client is allowed to send in this tunnel, save if channel
1610 * is reliable and send an ACK to the client if there is still buffer space
1613 * @param ch Channel.
1614 * @param c Client which sent the data.
1615 * @param message Message.
1616 * @param fwd Is this a FWD data?
1618 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1621 GMCH_handle_local_data (struct MeshChannel *ch,
1622 struct MeshClient *c,
1623 struct GNUNET_MessageHeader *message,
1626 struct MeshChannelReliability *rel;
1627 struct GNUNET_MESH_Data *payload;
1628 size_t size = ntohs (message->size);
1629 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
1630 unsigned char cbuf[p2p_size];
1632 /* Is the client in the channel? */
1639 GNUNET_break_op (0);
1640 return GNUNET_SYSERR;
1643 rel = fwd ? ch->root_rel : ch->dest_rel;
1645 if (GNUNET_NO == rel->client_allowed)
1647 GNUNET_break_op (0);
1648 return GNUNET_SYSERR;
1651 rel->client_allowed = GNUNET_NO;
1653 /* Ok, everything is correct, send the message. */
1654 payload = (struct GNUNET_MESH_Data *) cbuf;
1655 payload->mid = htonl (rel->mid_send);
1657 memcpy (&payload[1], message, size);
1658 payload->header.size = htons (p2p_size);
1659 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
1660 payload->chid = htonl (ch->gid);
1661 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1662 GMCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1664 if (is_loopback (ch))
1666 if (GMCH_get_buffer (ch, fwd) > 0)
1667 send_client_ack (ch, fwd);
1672 if (GMT_get_connections_buffer (ch->t) > 0)
1674 send_client_ack (ch, fwd);
1682 * Handle a channel destroy requested by a client.
1684 * Destroy the channel and the tunnel in case this was the last channel.
1686 * @param ch Channel.
1687 * @param c Client that requested the destruction (to avoid notifying him).
1688 * @param is_root Is the request coming from root?
1691 GMCH_handle_local_destroy (struct MeshChannel *ch,
1692 struct MeshClient *c,
1695 struct MeshTunnel3 *t;
1697 ch->destroy = GNUNET_YES;
1698 /* Cleanup after the tunnel */
1699 if (GNUNET_NO == is_root && c == ch->dest)
1701 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1702 GML_client_delete_channel (c, ch, ch->lid_dest);
1705 if (GNUNET_YES == is_root && c == ch->root)
1707 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1708 GML_client_delete_channel (c, ch, ch->lid_root);
1713 send_destroy (ch, GNUNET_NO);
1714 if (0 == ch->pending_messages)
1717 GMT_destroy_if_empty (t);
1723 * Handle a channel create requested by a client.
1725 * Create the channel and the tunnel in case this was the first0 channel.
1727 * @param c Client that requested the creation (will be the root).
1728 * @param msg Create Channel message.
1730 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1733 GMCH_handle_local_create (struct MeshClient *c,
1734 struct GNUNET_MESH_ChannelMessage *msg)
1736 struct MeshChannel *ch;
1737 struct MeshTunnel3 *t;
1738 struct MeshPeer *peer;
1739 MESH_ChannelNumber chid;
1741 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1742 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1743 chid = ntohl (msg->channel_id);
1745 /* Sanity check for duplicate channel IDs */
1746 if (NULL != GML_channel_get (c, chid))
1749 return GNUNET_SYSERR;
1752 peer = GMP_get (&msg->peer);
1753 GMP_add_tunnel (peer);
1754 t = GMP_get_tunnel (peer);
1756 if (GMP_get_short_id (peer) == myid)
1758 GMT_change_cstate (t, MESH_TUNNEL3_READY);
1765 /* Create channel */
1766 ch = channel_new (t, c, chid);
1770 return GNUNET_SYSERR;
1772 ch->port = ntohl (msg->port);
1773 channel_set_options (ch, ntohl (msg->opt));
1775 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1776 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
1777 ch->root_rel->ch = ch;
1778 ch->root_rel->retry_timer = MESH_RETRANSMIT_TIME;
1779 ch->root_rel->expected_delay.rel_value_us = 0;
1781 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
1790 * Handler for mesh network payload traffic.
1792 * @param ch Channel for the message.
1793 * @param msg Unencryted data message.
1794 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1795 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1796 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1797 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1800 GMCH_handle_data (struct MeshChannel *ch,
1801 const struct GNUNET_MESH_Data *msg,
1804 struct MeshChannelReliability *rel;
1805 struct MeshClient *c;
1808 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1809 if (GNUNET_SYSERR == fwd)
1811 if (is_loopback (ch))
1813 /* It is a loopback channel after all... */
1817 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1820 /* Initialize FWD/BCK data */
1821 c = fwd ? ch->dest : ch->root;
1822 rel = fwd ? ch->dest_rel : ch->root_rel;
1830 if (MESH_CHANNEL_READY != ch->state)
1832 if (GNUNET_NO == fwd)
1834 /* If we are the root, this means the other peer has sent traffic before
1835 * receiving our ACK. Even if the SYNACK goes missing, no traffic should
1836 * be sent before the ACK.
1838 GNUNET_break_op (0);
1841 /* If we are the dest, this means that the SYNACK got to the root but
1842 * the ACK went missing. Treat this as an ACK.
1844 channel_confirm (ch, GNUNET_NO);
1847 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1849 mid = ntohl (msg->mid);
1850 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! got mid %u\n", mid);
1852 if (GNUNET_NO == ch->reliable ||
1853 ( !GM_is_pid_bigger (rel->mid_recv, mid) &&
1854 GM_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1856 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1857 if (GNUNET_YES == ch->reliable)
1859 /* Is this the exact next expected messasge? */
1860 if (mid == rel->mid_recv)
1862 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1864 send_client_data (ch, msg, fwd);
1868 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1869 add_buffered_data (msg, rel);
1874 /* Tunnel is unreliable: send to clients directly */
1875 /* FIXME: accept Out Of Order traffic */
1876 rel->mid_recv = mid + 1;
1877 send_client_data (ch, msg, fwd);
1882 GNUNET_break_op (GM_is_pid_bigger (rel->mid_recv, mid));
1883 LOG (GNUNET_ERROR_TYPE_DEBUG,
1884 " !!! MID %u not expected (%u - %u), dropping!\n",
1885 mid, rel->mid_recv, rel->mid_recv + 63);
1888 GMCH_send_data_ack (ch, fwd);
1893 * Handler for mesh network traffic end-to-end ACKs.
1895 * @param ch Channel on which we got this message.
1896 * @param msg Data message.
1897 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1898 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1899 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1900 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1903 GMCH_handle_data_ack (struct MeshChannel *ch,
1904 const struct GNUNET_MESH_DataACK *msg,
1907 struct MeshChannelReliability *rel;
1908 struct MeshReliableMessage *copy;
1909 struct MeshReliableMessage *next;
1913 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1914 if (GNUNET_SYSERR == fwd)
1916 if (is_loopback (ch))
1918 /* It is a loopback channel after all... */
1922 /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
1923 fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
1926 ack = ntohl (msg->mid);
1927 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1928 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1930 if (GNUNET_YES == fwd)
1940 GNUNET_break_op (0);
1944 /* Free ACK'd copies: no need to retransmit those anymore */
1945 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1947 if (GM_is_pid_bigger (copy->mid, ack))
1949 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1950 channel_rel_free_sent (rel, msg);
1954 LOG (GNUNET_ERROR_TYPE_DEBUG, " !! id %u\n", copy->mid);
1956 rel_message_free (copy, GNUNET_YES);
1959 /* ACK client if needed */
1960 GMCH_allow_client (ch, fwd);
1962 /* If some message was free'd, update the retransmission delay */
1963 if (GNUNET_YES == work)
1965 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1967 GNUNET_SCHEDULER_cancel (rel->retry_task);
1968 if (NULL != rel->head_sent && NULL == rel->head_sent->q)
1970 struct GNUNET_TIME_Absolute new_target;
1971 struct GNUNET_TIME_Relative delay;
1973 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1974 MESH_RETRANSMIT_MARGIN);
1975 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1977 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1979 GNUNET_SCHEDULER_add_delayed (delay,
1980 &channel_retransmit_message,
1983 else /* either no more traffic to ack or traffic has just been queued */
1985 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1988 else /* work was done but no task was pending? shouldn't happen! */
1997 * Handler for channel create messages.
1999 * Does not have fwd parameter because it's always 'FWD': channel is incoming.
2001 * @param t Tunnel this channel will be in.
2002 * @param msg Channel crate message.
2004 struct MeshChannel *
2005 GMCH_handle_create (struct MeshTunnel3 *t,
2006 const struct GNUNET_MESH_ChannelCreate *msg)
2008 MESH_ChannelNumber chid;
2009 struct MeshChannel *ch;
2010 struct MeshClient *c;
2013 chid = ntohl (msg->chid);
2014 ch = GMT_get_channel (t, chid);
2017 /* Create channel */
2018 ch = channel_new (t, NULL, 0);
2020 channel_set_options (ch, ntohl (msg->opt));
2021 new_channel = GNUNET_YES;
2025 new_channel = GNUNET_NO;
2028 if (GNUNET_YES == new_channel || GMT_is_loopback (t))
2030 /* Find a destination client */
2031 ch->port = ntohl (msg->port);
2032 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", ch->port);
2033 c = GML_client_get_by_port (ch->port);
2036 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
2037 if (is_loopback (ch))
2039 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback: destroy on handler\n");
2044 LOG (GNUNET_ERROR_TYPE_DEBUG, " not loopback: destroy now\n");
2052 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
2055 add_destination (ch, c);
2056 if (GNUNET_YES == ch->reliable)
2057 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
2059 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
2061 send_client_create (ch);
2062 ch->state = MESH_CHANNEL_SENT;
2066 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate create channel\n");
2068 send_ack (ch, GNUNET_YES);
2075 * Handler for channel NACK messages.
2077 * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
2079 * @param ch Channel.
2082 GMCH_handle_nack (struct MeshChannel *ch)
2084 send_client_nack (ch);
2090 * Handler for channel ack messages.
2092 * @param ch Channel.
2093 * @param msg Message.
2094 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2095 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2096 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2097 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2100 GMCH_handle_ack (struct MeshChannel *ch,
2101 const struct GNUNET_MESH_ChannelManage *msg,
2104 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2105 if (GNUNET_SYSERR == fwd)
2107 if (is_loopback (ch))
2109 /* It is a loopback channel after all... */
2113 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2116 channel_confirm (ch, !fwd);
2121 * Handler for channel destroy messages.
2123 * @param ch Channel to be destroyed of.
2124 * @param msg Message.
2125 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2126 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2127 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2128 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2131 GMCH_handle_destroy (struct MeshChannel *ch,
2132 const struct GNUNET_MESH_ChannelManage *msg,
2135 struct MeshTunnel3 *t;
2137 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2138 if (GNUNET_SYSERR == fwd)
2140 if (is_loopback (ch))
2142 /* It is a loopback channel after all... */
2146 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2150 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
2152 /* Not for us (don't destroy twice a half-open loopback channel) */
2157 send_destroy (ch, GNUNET_YES);
2159 GMT_destroy_if_empty (t);
2164 * Sends an already built message on a channel.
2166 * If the channel is on a loopback tunnel, notifies the appropriate destination
2169 * On a normal channel passes the message to the tunnel for encryption and
2170 * sending on a connection.
2172 * This function DOES NOT save the message for retransmission.
2174 * @param message Message to send. Function makes a copy of it.
2175 * @param ch Channel on which this message is transmitted.
2176 * @param fwd Is this a fwd message?
2177 * @param existing_copy This is a retransmission, don't save a copy.
2180 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2181 struct MeshChannel *ch, int fwd,
2182 void *existing_copy)
2184 struct MeshChannelQueue *q;
2187 type = ntohs (message->type);
2188 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH Send %s %s on channel %s\n",
2189 GM_f2s (fwd), GM_m2s (type),
2192 if (GMT_is_loopback (ch->t))
2194 handle_loopback (ch, message, fwd);
2200 case GNUNET_MESSAGE_TYPE_MESH_DATA:
2202 if (GNUNET_YES == ch->reliable)
2204 q = GNUNET_new (struct MeshChannelQueue);
2206 if (NULL == existing_copy)
2207 q->copy = channel_save_copy (ch, message, fwd);
2210 q->copy = (struct MeshReliableMessage *) existing_copy;
2211 if (NULL != q->copy->q)
2213 /* Last retransmission was queued but not yet sent!
2214 * This retransmission was scheduled by a ch_message_sent which
2215 * followed a very fast RTT, so the tiny delay made the
2216 * retransmission function to execute before the previous
2217 * retransmitted message even had a chance to leave the peer.
2218 * Cancel this message and wait until the pending
2219 * retransmission leaves the peer and ch_message_sent starts
2220 * the timer for the next one.
2223 LOG (GNUNET_ERROR_TYPE_DEBUG,
2224 " exisitng copy not yet transmitted!\n");
2227 LOG (GNUNET_ERROR_TYPE_DEBUG,
2228 " using existing copy: %p {r:%p q:%p t:%u}\n",
2230 q->copy->rel, q->copy->q, q->copy->type);
2232 LOG (GNUNET_ERROR_TYPE_DEBUG, " new q: %p\n", q);
2234 q->q = GMT_send_prebuilt_message (message, ch->t, ch,
2235 NULL != existing_copy,
2236 &ch_message_sent, q);
2237 /* q itself is stored in copy */
2241 fire_and_forget (message, ch, GNUNET_NO);
2246 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
2247 if (GNUNET_YES == fwd)
2249 /* BCK ACK (going FWD) is just a response for a SYNACK, don't keep*/
2250 fire_and_forget (message, ch, GNUNET_YES);
2254 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2255 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
2256 q = GNUNET_new (struct MeshChannelQueue);
2258 q->rel = fwd ? ch->root_rel : ch->dest_rel;
2259 if (NULL != q->rel->uniq)
2261 if (NULL != q->rel->uniq->q)
2263 GMT_cancel (q->rel->uniq->q);
2264 /* ch_message_sent is called, freeing and NULLing uniq */
2268 GNUNET_free (q->rel->uniq);
2271 q->q = GMT_send_prebuilt_message (message, ch->t, ch, GNUNET_YES,
2272 &ch_message_sent, q);
2277 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
2278 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
2279 fire_and_forget (message, ch, GNUNET_YES);
2285 LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s unknown!\n", GM_m2s (type));
2286 fire_and_forget (message, ch, GNUNET_YES);
2292 * Get the static string for identification of the channel.
2294 * @param ch Channel.
2296 * @return Static string with the channel IDs.
2299 GMCH_2s (const struct MeshChannel *ch)
2301 static char buf[64];
2304 return "(NULL Channel)";
2306 sprintf (buf, "%s:%u gid:%X (%X / %X)",
2307 GMT_2s (ch->t), ch->port, ch->gid, ch->lid_root, ch->lid_dest);