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_enc.h"
30 #include "gnunet-service-mesh_channel.h"
31 #include "gnunet-service-mesh_local.h"
32 #include "gnunet-service-mesh_tunnel.h"
34 #define LOG(level, ...) GNUNET_log_from(level,"mesh-chn",__VA_ARGS__)
36 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
37 #define MESH_RETRANSMIT_MARGIN 4
41 * All the states a connection can be in.
46 * Uninitialized status, should never appear in operation.
51 * Connection create message sent, waiting for ACK.
56 * Connection confirmed, ready to carry traffic..
64 * Info needed to retry a message in case it gets lost.
66 struct MeshReliableMessage
69 * Double linked list, FIFO style
71 struct MeshReliableMessage *next;
72 struct MeshReliableMessage *prev;
75 * Type of message (payload, channel management).
80 * Tunnel Reliability queue this message is in.
82 struct MeshChannelReliability *rel;
85 * ID of the message (ACK needed to free)
90 * When was this message issued (to calculate ACK delay)
92 struct GNUNET_TIME_Absolute timestamp;
94 /* struct GNUNET_MESH_Data with payload */
99 * Info about the traffic state for a client in a channel.
101 struct MeshChannelReliability
104 * Channel this is about.
106 struct MeshChannel *ch;
109 * DLL of messages sent and not yet ACK'd.
111 struct MeshReliableMessage *head_sent;
112 struct MeshReliableMessage *tail_sent;
115 * Messages pending to send.
120 * DLL of messages received out of order.
122 struct MeshReliableMessage *head_recv;
123 struct MeshReliableMessage *tail_recv;
131 * Next MID to use for outgoing traffic.
136 * Next MID expected for incoming traffic.
141 * Can we send data to the client?
146 * Task to resend/poll in case no ACK is received.
148 GNUNET_SCHEDULER_TaskIdentifier retry_task;
151 * Counter for exponential backoff.
153 struct GNUNET_TIME_Relative retry_timer;
156 * How long does it usually take to get an ACK.
158 struct GNUNET_TIME_Relative expected_delay;
163 * Struct containing all information regarding a channel to a remote client.
168 * Tunnel this channel is in.
170 struct MeshTunnel3 *t;
173 * Destination port of the channel.
178 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
180 MESH_ChannelNumber gid;
183 * Local tunnel number for root (owner) client.
184 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
186 MESH_ChannelNumber lid_root;
189 * Local tunnel number for local destination clients (incoming number)
190 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
192 MESH_ChannelNumber lid_dest;
197 enum MeshChannelState state;
200 * Is the tunnel bufferless (minimum latency)?
205 * Is the tunnel reliable?
210 * Last time the channel was used
212 struct GNUNET_TIME_Absolute timestamp;
215 * Client owner of the tunnel, if any
217 struct MeshClient *root;
220 * Client destination of the tunnel, if any.
222 struct MeshClient *dest;
225 * Flag to signal the destruction of the channel.
226 * If this is set GNUNET_YES the channel will be destroyed
227 * when the queue is empty.
232 * Total messages pending for this channel, payload or not.
234 unsigned int pending_messages;
238 * Only present (non-NULL) at the owner of a tunnel.
240 struct MeshChannelReliability *root_rel;
244 * Only present (non-NULL) at the destination of a tunnel.
246 struct MeshChannelReliability *dest_rel;
251 /******************************************************************************/
252 /******************************* GLOBALS ***********************************/
253 /******************************************************************************/
256 * Global handle to the statistics service.
258 extern struct GNUNET_STATISTICS_Handle *stats;
261 /******************************************************************************/
262 /******************************** STATIC ***********************************/
263 /******************************************************************************/
266 * Destroy a reliable message after it has been acknowledged, either by
267 * direct mid ACK or bitfield. Updates the appropriate data structures and
268 * timers and frees all memory.
270 * @param copy Message that is no longer needed: remote peer got it.
273 rel_message_free (struct MeshReliableMessage *copy);
276 * We have received a message out of order, or the client is not ready.
277 * Buffer it until we receive an ACK from the client or the missing
278 * message from the channel.
280 * @param msg Message to buffer (MUST be of type MESH_DATA).
281 * @param rel Reliability data to the corresponding direction.
284 add_buffered_data (const struct GNUNET_MESH_Data *msg,
285 struct MeshChannelReliability *rel)
287 struct MeshReliableMessage *copy;
288 struct MeshReliableMessage *prev;
292 size = ntohs (msg->header.size);
293 mid = ntohl (msg->mid);
295 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
297 copy = GNUNET_malloc (sizeof (*copy) + size);
300 memcpy (©[1], msg, size);
304 // FIXME do something better than O(n), although n < 64...
305 // FIXME start from the end (most messages are the latest ones)
306 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
308 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
309 if (GMC_is_pid_bigger (prev->mid, mid))
311 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
312 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
317 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
318 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
319 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
324 * Send data to a client.
326 * If the client is ready, send directly, otherwise buffer while listening
330 * @param msg Message.
331 * @param fwd Is this a fwd (root->dest) message?
334 send_client_data (struct MeshChannel *ch,
335 const struct GNUNET_MESH_Data *msg,
340 if (ch->dest_rel->client_ready)
341 GML_send_data (ch, msg, ch->dest, ch->lid_dest);
343 add_buffered_data (msg, ch->dest_rel);
347 if (ch->root_rel->client_ready)
348 GML_send_data (ch, msg, ch->root, ch->lid_root);
350 add_buffered_data (msg, ch->root_rel);
356 * Add a client to a channel, initializing all needed data structures.
358 * @param ch Channel to which add the client.
359 * @param c Client which to add to the channel.
362 GMCH_add_client (struct MeshChannel *ch, struct MeshClient *c)
364 struct MeshTunnel3 *t = ch->t;
366 if (NULL != ch->dest)
372 /* Assign local id as destination */
373 ch->lid_dest = GML_get_next_chid (c);
375 /* Store in client's hashmap */
376 GML_channel_add (c, ch->lid_dest, ch);
378 GNUNET_break (NULL == ch->dest_rel);
379 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
380 ch->dest_rel->ch = ch;
381 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
388 * Destroy all reliable messages queued for a channel,
389 * during a channel destruction.
390 * Frees the reliability structure itself.
392 * @param rel Reliability data for a channel.
395 channel_rel_free_all (struct MeshChannelReliability *rel)
397 struct MeshReliableMessage *copy;
398 struct MeshReliableMessage *next;
403 for (copy = rel->head_recv; NULL != copy; copy = next)
406 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
409 for (copy = rel->head_sent; NULL != copy; copy = next)
412 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
415 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
416 GNUNET_SCHEDULER_cancel (rel->retry_task);
422 * Mark future messages as ACK'd.
424 * @param rel Reliability data.
425 * @param msg DataACK message with a bitfield of future ACK'd messages.
428 channel_rel_free_sent (struct MeshChannelReliability *rel,
429 const struct GNUNET_MESH_DataACK *msg)
431 struct MeshReliableMessage *copy;
432 struct MeshReliableMessage *next;
439 bitfield = msg->futures;
440 mid = ntohl (msg->mid);
441 LOG (GNUNET_ERROR_TYPE_DEBUG,
442 "free_sent_reliable %u %llX\n",
444 LOG (GNUNET_ERROR_TYPE_DEBUG,
445 " rel %p, head %p\n",
446 rel, rel->head_sent);
447 for (i = 0, copy = rel->head_sent;
448 i < 64 && NULL != copy && 0 != bitfield;
451 LOG (GNUNET_ERROR_TYPE_DEBUG,
452 " trying bit %u (mid %u)\n",
455 if (0 == (bitfield & mask))
458 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
459 /* Bit was set, clear the bit from the bitfield */
462 /* The i-th bit was set. Do we have that copy? */
463 /* Skip copies with mid < target */
464 target = mid + i + 1;
465 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
466 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
469 /* Did we run out of copies? (previously freed, it's ok) */
472 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
476 /* Did we overshoot the target? (previously freed, it's ok) */
477 if (GMC_is_pid_bigger (copy->mid, target))
479 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
483 /* Now copy->mid == target, free it */
485 rel_message_free (copy);
488 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
493 * We haven't received an ACK after a certain time: restransmit the message.
495 * @param cls Closure (MeshReliableMessage with the message to restransmit)
496 * @param tc TaskContext.
499 channel_retransmit_message (void *cls,
500 const struct GNUNET_SCHEDULER_TaskContext *tc)
502 struct MeshChannelReliability *rel = cls;
503 struct MeshReliableMessage *copy;
504 struct MeshPeerQueue *q;
505 struct MeshChannel *ch;
506 struct MeshConnection *c;
507 struct GNUNET_MESH_Data *payload;
508 struct MeshPeer *hop;
511 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
512 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
516 copy = rel->head_sent;
523 /* Search the message to be retransmitted in the outgoing queue.
524 * Check only the queue for the connection that is going to be used,
525 * if the message is stuck in some other connection's queue we shouldn't
527 * - cancelling it and sending the new one doesn't guarantee it's delivery,
528 * the old connection could be temporary stalled or the queue happened to
529 * be long at time of insertion.
530 * - not sending the new one could cause terrible delays the old connection
533 payload = (struct GNUNET_MESH_Data *) ©[1];
534 fwd = (rel == ch->root_rel);
535 c = GMT_get_connection (ch->t, fwd);
536 hop = connection_get_hop (c, fwd);
537 for (q = hop->queue_head; NULL != q; q = q->next)
539 if (ntohs (payload->header.type) == q->type && ch == q->ch)
541 struct GNUNET_MESH_Data *queued_data = q->cls;
543 if (queued_data->mid == payload->mid)
548 /* Message not found in the queue that we are going to use. */
551 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
553 GMCH_send_prebuilt_message (&payload->header, ch, fwd);
554 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
558 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
561 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
562 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
563 &channel_retransmit_message,
569 * Send ACK on one or more connections due to buffer space to the client.
571 * Iterates all connections of the tunnel and sends ACKs appropriately.
573 * @param ch Channel which has some free buffer space.
574 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
577 channel_send_connections_ack (struct MeshChannel *ch,
581 struct MeshTunnel3 *t = ch->t;
582 struct MeshConnection *c;
585 uint32_t allow_per_connection;
588 LOG (GNUNET_ERROR_TYPE_DEBUG,
589 "Channel send connection %s ack on %s:%X\n",
590 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
592 /* Count connections, how many messages are already allowed */
593 cs = GMT_count_connections (t);
594 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
596 fc = fwd ? &c->fwd_fc : &c->bck_fc;
597 if (GMC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
602 allowed += fc->last_ack_sent - fc->last_pid_recv;
606 /* Make sure there is no overflow */
607 if (allowed > buffer)
613 /* Authorize connections to send more data */
614 to_allow = buffer - allowed;
616 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
618 allow_per_connection = to_allow/cs;
619 to_allow -= allow_per_connection;
621 fc = fwd ? &c->fwd_fc : &c->bck_fc;
622 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
626 GMC_send_ack (c, allow_per_connection, fwd);
629 LOG (GNUNET_ERROR_TYPE_DEBUG,
630 "Channel send connection %s ack on %s:%X\n",
631 fwd ? "FWD" : "BCK", GMT_2s (ch->t), ch->gid);
632 GNUNET_break (to_allow == 0);
637 * Destroy a reliable message after it has been acknowledged, either by
638 * direct mid ACK or bitfield. Updates the appropriate data structures and
639 * timers and frees all memory.
641 * @param copy Message that is no longer needed: remote peer got it.
644 rel_message_free (struct MeshReliableMessage *copy)
646 struct MeshChannelReliability *rel;
647 struct GNUNET_TIME_Relative time;
650 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
651 rel->expected_delay.rel_value_us *= 7;
652 rel->expected_delay.rel_value_us += time.rel_value_us;
653 rel->expected_delay.rel_value_us /= 8;
655 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
656 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
657 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
658 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
659 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
660 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
662 rel->retry_timer = rel->expected_delay;
663 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
670 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
672 * @param ch Channel to mark as ready.
673 * @param fwd Was the CREATE message sent fwd?
676 channel_confirm (struct MeshChannel *ch, int fwd)
678 struct MeshChannelReliability *rel;
679 struct MeshReliableMessage *copy;
680 struct MeshReliableMessage *next;
682 LOG (GNUNET_ERROR_TYPE_DEBUG,
683 " channel confirm %s %s:%X\n",
684 fwd ? "FWD" : "BCK", GMT_2s (ch->t), ch->gid);
685 ch->state = MESH_CHANNEL_READY;
687 rel = fwd ? ch->root_rel : ch->dest_rel;
688 for (copy = rel->head_sent; NULL != copy; copy = next)
690 struct GNUNET_MessageHeader *msg;
693 msg = (struct GNUNET_MessageHeader *) ©[1];
694 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
696 rel_message_free (copy);
700 send_ack (NULL, ch, fwd);
705 * Save a copy to retransmit in case it gets lost.
707 * Initializes all needed callbacks and timers.
709 * @param ch Channel this message goes on.
710 * @param msg Message to copy.
711 * @param fwd Is this fwd traffic?
714 channel_save_copy (struct MeshChannel *ch,
715 const struct GNUNET_MessageHeader *msg,
718 struct MeshChannelReliability *rel;
719 struct MeshReliableMessage *copy;
724 rel = fwd ? ch->root_rel : ch->dest_rel;
726 type = ntohs (msg->type);
727 size = ntohs (msg->size);
729 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
730 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
732 copy->timestamp = GNUNET_TIME_absolute_get ();
735 memcpy (©[1], msg, size);
737 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
738 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
739 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
742 GNUNET_TIME_relative_multiply (rel->expected_delay,
743 MESH_RETRANSMIT_MARGIN);
745 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
746 &channel_retransmit_message,
754 * Send a buffered message to the client, for in order delivery or
755 * as result of client ACK.
757 * @param ch Channel on which to empty the message buffer.
758 * @param c Client to send to.
759 * @param rel Reliability structure to corresponding peer.
760 * If rel == bck_rel, this is FWD data.
763 send_client_buffered_data (struct MeshChannel *ch,
764 struct MeshClient *c,
767 struct MeshReliableMessage *copy;
768 struct MeshChannelReliability *rel;
770 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
771 rel = fwd ? ch->dest_rel : ch->root_rel;
772 if (GNUNET_NO == rel->client_ready)
774 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
778 copy = rel->head_recv;
779 /* We never buffer channel management messages */
782 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
784 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
786 LOG (GNUNET_ERROR_TYPE_DEBUG,
787 " have %u! now expecting %u\n",
788 copy->mid, rel->mid_recv + 1);
789 send_client_data (ch, msg, fwd);
792 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
797 LOG (GNUNET_ERROR_TYPE_DEBUG,
798 " reliable && don't have %u, next is %u\n",
804 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
811 * Destroy a channel and free all resources.
813 * @param ch Channel to destroy.
816 channel_destroy (struct MeshChannel *ch)
818 struct MeshClient *c;
823 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
824 GMT_2s (ch->t), ch->gid);
830 GML_channel_remove (c, ch->lid_root, ch);
836 GML_channel_remove (c, ch->lid_dest, ch);
839 channel_rel_free_all (ch->root_rel);
840 channel_rel_free_all (ch->dest_rel);
842 GMT_remove_channel (ch->t, ch);
843 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
850 * Create a new channel.
852 * @param t Tunnel this channel is in.
853 * @param owner Client that owns the channel, NULL for foreign channels.
854 * @param lid_root Local ID for root client.
856 * @return A new initialized channel. NULL on error.
858 static struct MeshChannel *
859 channel_new (struct MeshTunnel3 *t,
860 struct MeshClient *owner, MESH_ChannelNumber lid_root)
862 struct MeshChannel *ch;
864 ch = GNUNET_new (struct MeshChannel);
866 ch->lid_root = lid_root;
869 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
873 ch->gid = GMT_get_next_chid (t);
874 GML_channel_add (owner, lid_root, ch);
876 GMT_add_channel (t, ch);
883 * Set options in a channel, extracted from a bit flag field
885 * @param ch Channel to set options to.
886 * @param options Bit array in host byte order.
889 channel_set_options (struct MeshChannel *ch, uint32_t options)
891 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
892 GNUNET_YES : GNUNET_NO;
893 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
894 GNUNET_YES : GNUNET_NO;
900 * Confirm we got a channel create.
902 * @param ch The channel to confirm.
903 * @param fwd Should we send the ACK fwd?
906 channel_send_ack (struct MeshChannel *ch, int fwd)
908 struct GNUNET_MESH_ChannelManage msg;
910 msg.header.size = htons (sizeof (msg));
911 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
912 LOG (GNUNET_ERROR_TYPE_DEBUG,
913 " sending channel %s ack for channel %s:%X\n",
914 fwd ? "FWD" : "BCK", GMT_2s (ch->t),
917 msg.chid = htonl (ch->gid);
918 GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
923 * Iterator for deleting each channel whose client endpoint disconnected.
925 * @param cls Closure (client that has disconnected).
926 * @param key The local channel id (used to access the hashmap).
927 * @param value The value stored at the key (channel to destroy).
929 * @return GNUNET_OK, keep iterating.
932 channel_destroy_iterator (void *cls,
936 struct MeshChannel *ch = value;
937 struct MeshClient *c = cls;
938 struct MeshTunnel3 *t;
940 LOG (GNUNET_ERROR_TYPE_DEBUG,
941 " Channel %X (%X / %X) destroy, due to client %s shutdown.\n",
942 ch->gid, ch->lid_root, ch->lid_dest, GML_2s (c));
947 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
951 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
955 GMCH_send_destroy (ch);
956 channel_destroy (ch);
957 GMT_destroy_if_empty (t);
964 * Handle a loopback message: call the appropriate handler for the message type.
966 * @param ch Channel this message is on.
967 * @param msgh Message header.
968 * @param fwd Is this FWD traffic?
971 handle_loopback (struct MeshChannel *ch,
972 struct GNUNET_MessageHeader *msgh,
977 type = ntohs (msgh->type);
978 LOG (GNUNET_ERROR_TYPE_DEBUG,
979 "Loopback %s message!\n",
980 GNUNET_MESH_DEBUG_M2S (type));
984 case GNUNET_MESSAGE_TYPE_MESH_DATA:
985 /* Don't send hop ACK, wait for client to ACK */
986 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
989 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
990 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
993 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
994 // FIXME store channel in loopback tunnel?
995 GMCH_handle_create ((struct GNUNET_MESH_ChannelCreate *) msgh,
999 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1000 GMCH_handle_ack (ch,
1001 (struct GNUNET_MESH_ChannelManage *) msgh,
1005 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1006 GMCH_handle_destroy (ch,
1007 (struct GNUNET_MESH_ChannelManage *) msgh,
1012 GNUNET_break_op (0);
1013 LOG (GNUNET_ERROR_TYPE_DEBUG,
1014 "end-to-end message not known (%u)\n",
1015 ntohs (msgh->type));
1021 /******************************************************************************/
1022 /******************************** API ***********************************/
1023 /******************************************************************************/
1028 * @param ch Channel.
1033 GMCH_get_id (const struct MeshChannel *ch)
1040 * Get the channel tunnel.
1042 * @param ch Channel to get the tunnel from.
1044 * @return tunnel of the channel.
1046 struct MeshTunnel3 *
1047 GMCH_get_tunnel (const struct MeshChannel *ch)
1054 * Get free buffer space towards the client on a specific channel.
1056 * @param ch Channel.
1057 * @param fwd Is query about FWD traffic?
1059 * @return Free buffer space [0 - 64]
1062 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
1064 struct MeshChannelReliability *rel;
1066 rel = fwd ? ch->dest_rel : ch->root_rel;
1068 /* If rel is NULL it means that the end is not yet created,
1069 * most probably is a loopback channel at the point of sending
1070 * the ChannelCreate to itself.
1075 return (64 - rel->n_recv);
1080 * Is the root client for this channel on this peer?
1082 * @param ch Channel.
1083 * @param fwd Is this for fwd traffic?
1085 * @return GNUNET_YES in case it is.
1088 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1090 struct MeshClient *c;
1092 c = fwd ? ch->root : ch->dest;
1098 * Is the destination client for this channel on this peer?
1100 * @param ch Channel.
1101 * @param fwd Is this for fwd traffic?
1103 * @return GNUNET_YES in case it is.
1106 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1108 struct MeshClient *c;
1110 c = fwd ? ch->dest : ch->root;
1116 * Notify the destination client that a new incoming channel was created.
1118 * @param ch Channel that was created.
1121 GMCH_send_create (struct MeshChannel *ch)
1123 struct GNUNET_MESH_ChannelMessage msg;
1126 if (NULL == ch->dest)
1130 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1131 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1132 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1133 GMT_get_destination (ch->t));
1138 * Notify a client that the channel is no longer valid.
1139 * FIXME send on tunnel if some client == NULL?
1141 * @param ch Channel that is destroyed.
1144 GMCH_send_destroy (struct MeshChannel *ch)
1146 if (NULL != ch->root)
1147 GML_send_channel_destroy (ch->root, ch->lid_root);
1149 if (NULL != ch->dest)
1150 GML_send_channel_destroy (ch->dest, ch->lid_dest);
1155 * Send data on a channel.
1157 * If the destination is local, send it to client, otherwise encrypt and
1161 * @param msg Message.
1162 * @param fwd Is this a fwd (root->dest) message?
1165 GMCH_send_data (struct MeshChannel *ch,
1166 const struct GNUNET_MESH_Data *msg,
1173 * Send an end-to-end ACK message for the most recent in-sequence payload.
1175 * If channel is not reliable, do nothing.
1177 * @param ch Channel this is about.
1178 * @param fwd Is for FWD traffic? (ACK dest->owner)
1181 GMCH_send_ack (struct MeshChannel *ch, int fwd)
1183 struct GNUNET_MESH_DataACK msg;
1184 struct MeshChannelReliability *rel;
1185 struct MeshReliableMessage *copy;
1190 if (GNUNET_NO == ch->reliable)
1194 rel = fwd ? ch->dest_rel : ch->root_rel;
1195 LOG (GNUNET_ERROR_TYPE_DEBUG,
1196 "send_data_ack for %u\n",
1199 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
1200 msg.header.type = htons (type);
1201 msg.header.size = htons (sizeof (msg));
1202 msg.chid = htonl (ch->gid);
1203 msg.mid = htonl (rel->mid_recv - 1);
1205 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1207 if (copy->type != type)
1209 delta = copy->mid - rel->mid_recv;
1212 mask = 0x1LL << delta;
1213 msg.futures |= mask;
1214 LOG (GNUNET_ERROR_TYPE_DEBUG,
1215 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1216 copy->mid, delta, mask, msg.futures);
1218 LOG (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
1220 GMCH_send_prebuilt_message (&msg.header, ch, fwd);
1221 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1228 * @param ch Channel.
1231 GMCH_debug (struct MeshChannel *ch)
1235 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1238 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1239 GMT_2s (ch->t), ch->gid, ch);
1240 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1241 ch->root, ch->root_rel);
1242 if (NULL != ch->root)
1244 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1245 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1246 ch->root_rel->client_ready ? "YES" : "NO");
1247 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1249 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1250 ch->dest, ch->dest_rel);
1251 if (NULL != ch->dest)
1253 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1254 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1255 ch->dest_rel->client_ready ? "YES" : "NO");
1256 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1262 * Handler for mesh network payload traffic.
1264 * @param ch Channel for the message.
1265 * @param message Unencryted data message.
1266 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1269 GMCH_handle_data (struct MeshChannel *ch,
1270 const struct GNUNET_MESH_Data *msg,
1273 struct MeshChannelReliability *rel;
1274 struct MeshClient *c;
1279 /* Initialize FWD/BCK data */
1280 c = fwd ? ch->dest : ch->root;
1281 rel = fwd ? ch->dest_rel : ch->root_rel;
1289 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1291 mid = ntohl (msg->mid);
1292 LOG (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
1294 if (GNUNET_NO == ch->reliable ||
1295 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
1296 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1298 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1299 if (GNUNET_YES == ch->reliable)
1301 /* Is this the exact next expected messasge? */
1302 if (mid == rel->mid_recv)
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1306 send_client_data (ch, msg, fwd);
1310 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1311 add_buffered_data (msg, rel);
1316 /* Tunnel is unreliable: send to clients directly */
1317 /* FIXME: accept Out Of Order traffic */
1318 rel->mid_recv = mid + 1;
1319 send_client_data (ch, msg, fwd);
1324 GNUNET_break_op (0);
1325 LOG (GNUNET_ERROR_TYPE_DEBUG,
1326 " MID %u not expected (%u - %u), dropping!\n",
1327 mid, rel->mid_recv, rel->mid_recv + 64);
1330 GMCH_send_ack (ch, fwd);
1335 * Handler for mesh network traffic end-to-end ACKs.
1337 * @param t Tunnel on which we got this message.
1338 * @param message Data message.
1339 * @param fwd Is this a fwd ACK? (dest->orig)
1342 GMCH_handle_data_ack (struct MeshChannel *ch,
1343 const struct GNUNET_MESH_DataACK *msg,
1346 struct MeshChannelReliability *rel;
1347 struct MeshReliableMessage *copy;
1348 struct MeshReliableMessage *next;
1352 ack = ntohl (msg->mid);
1353 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1354 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1356 if (GNUNET_YES == fwd)
1370 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1372 if (GMC_is_pid_bigger (copy->mid, ack))
1374 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1375 channel_rel_free_sent (rel, msg);
1379 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
1381 rel_message_free (copy);
1383 /* ACK client if needed */
1384 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
1386 /* If some message was free'd, update the retransmission delay*/
1387 if (GNUNET_YES == work)
1389 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1391 GNUNET_SCHEDULER_cancel (rel->retry_task);
1392 if (NULL == rel->head_sent)
1394 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1398 struct GNUNET_TIME_Absolute new_target;
1399 struct GNUNET_TIME_Relative delay;
1401 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1402 MESH_RETRANSMIT_MARGIN);
1403 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1405 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1407 GNUNET_SCHEDULER_add_delayed (delay,
1408 &channel_retransmit_message,
1419 * Handler for channel create messages.
1421 * @param msg Message.
1422 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1424 struct MeshChannel *
1425 GMCH_handle_create (const struct GNUNET_MESH_ChannelCreate *msg,
1428 MESH_ChannelNumber chid;
1429 struct MeshChannel *ch;
1430 struct MeshClient *c;
1433 chid = ntohl (msg->chid);
1435 /* Create channel */
1436 ch = channel_new (NULL, NULL, 0); /* FIXME t */
1438 channel_set_options (ch, ntohl (msg->opt));
1440 /* Find a destination client */
1441 port = ntohl (msg->port);
1442 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
1443 c = GML_client_get_by_port (port);
1446 /* TODO send reject */
1447 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
1452 channel_add_client (ch, c);
1453 if (GNUNET_YES == ch->reliable)
1454 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1456 GMCH_send_create (ch);
1457 GMCH_send_ack (ch, fwd);
1458 GML_send_ack (ch, !fwd);
1465 * Handler for channel ack messages.
1467 * @param ch Channel.
1468 * @param msg Message.
1469 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1472 GMCH_handle_ack (struct MeshChannel *ch,
1473 const struct GNUNET_MESH_ChannelManage *msg,
1476 channel_confirm (ch, !fwd);
1481 * Handler for channel destroy messages.
1483 * @param ch Channel to be destroyed of.
1484 * @param msg Message.
1485 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1488 GMCH_handle_destroy (struct MeshChannel *ch,
1489 const struct GNUNET_MESH_ChannelManage *msg,
1492 MESH_ChannelNumber chid;
1494 /* Check if channel exists */
1495 chid = ntohl (msg->chid);
1496 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
1498 /* Not for us (don't destroy twice a half-open loopback channel) */
1502 GMCH_send_destroy (ch);
1503 channel_destroy (ch);
1508 * Sends an already built message on a channel.
1510 * @param message Message to send. Function makes a copy of it.
1511 * @param ch Channel on which this message is transmitted.
1512 * @param fwd Is this a fwd message?
1515 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1516 struct MeshChannel *ch, int fwd)
1518 size_t size = ntohs (message->size);
1520 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
1521 GMT_2s (ch->t), ch->gid, fwd ? "FWD" : "BCK");
1522 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1523 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1525 if (GMT_is_loopback (ch->t))
1527 handle_loopback (ch->t, message, fwd);
1531 GMT_send_prebuilt_message (message, ch->t, ch, fwd);