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_MARGIN 4
39 * All the states a connection can be in.
44 * Uninitialized status, should never appear in operation.
49 * Connection create message sent, waiting for ACK.
54 * Connection confirmed, ready to carry traffic..
62 * Info needed to retry a message in case it gets lost.
64 struct MeshReliableMessage
67 * Double linked list, FIFO style
69 struct MeshReliableMessage *next;
70 struct MeshReliableMessage *prev;
73 * Type of message (payload, channel management).
78 * Tunnel Reliability queue this message is in.
80 struct MeshChannelReliability *rel;
83 * ID of the message (ACK needed to free)
88 * When was this message issued (to calculate ACK delay)
90 struct GNUNET_TIME_Absolute timestamp;
92 /* struct GNUNET_MESH_Data with payload */
97 * Info about the traffic state for a client in a channel.
99 struct MeshChannelReliability
102 * Channel this is about.
104 struct MeshChannel *ch;
107 * DLL of messages sent and not yet ACK'd.
109 struct MeshReliableMessage *head_sent;
110 struct MeshReliableMessage *tail_sent;
113 * Messages pending to send.
118 * DLL of messages received out of order.
120 struct MeshReliableMessage *head_recv;
121 struct MeshReliableMessage *tail_recv;
129 * Next MID to use for outgoing traffic.
134 * Next MID expected for incoming traffic.
139 * Can we send data to the client?
144 * Task to resend/poll in case no ACK is received.
146 GNUNET_SCHEDULER_TaskIdentifier retry_task;
149 * Counter for exponential backoff.
151 struct GNUNET_TIME_Relative retry_timer;
154 * How long does it usually take to get an ACK.
156 struct GNUNET_TIME_Relative expected_delay;
161 * Struct containing all information regarding a channel to a remote client.
166 * Tunnel this channel is in.
168 struct MeshTunnel3 *t;
171 * Destination port of the channel.
176 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
178 MESH_ChannelNumber gid;
181 * Local tunnel number for root (owner) client.
182 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
184 MESH_ChannelNumber lid_root;
187 * Local tunnel number for local destination clients (incoming number)
188 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
190 MESH_ChannelNumber lid_dest;
195 enum MeshChannelState state;
198 * Is the tunnel bufferless (minimum latency)?
203 * Is the tunnel reliable?
208 * Last time the channel was used
210 struct GNUNET_TIME_Absolute timestamp;
213 * Client owner of the tunnel, if any
215 struct MeshClient *root;
218 * Client destination of the tunnel, if any.
220 struct MeshClient *dest;
223 * Flag to signal the destruction of the channel.
224 * If this is set GNUNET_YES the channel will be destroyed
225 * when the queue is empty.
230 * Total messages pending for this channel, payload or not.
232 unsigned int pending_messages;
236 * Only present (non-NULL) at the owner of a tunnel.
238 struct MeshChannelReliability *root_rel;
242 * Only present (non-NULL) at the destination of a tunnel.
244 struct MeshChannelReliability *dest_rel;
249 /******************************************************************************/
250 /******************************* GLOBALS ***********************************/
251 /******************************************************************************/
254 * Global handle to the statistics service.
256 extern struct GNUNET_STATISTICS_Handle *stats;
259 /******************************************************************************/
260 /******************************** STATIC ***********************************/
261 /******************************************************************************/
264 * Destroy a reliable message after it has been acknowledged, either by
265 * direct mid ACK or bitfield. Updates the appropriate data structures and
266 * timers and frees all memory.
268 * @param copy Message that is no longer needed: remote peer got it.
271 rel_message_free (struct MeshReliableMessage *copy);
274 * We have received a message out of order, or the client is not ready.
275 * Buffer it until we receive an ACK from the client or the missing
276 * message from the channel.
278 * @param msg Message to buffer (MUST be of type MESH_DATA).
279 * @param rel Reliability data to the corresponding direction.
282 add_buffered_data (const struct GNUNET_MESH_Data *msg,
283 struct MeshChannelReliability *rel)
285 struct MeshReliableMessage *copy;
286 struct MeshReliableMessage *prev;
290 size = ntohs (msg->header.size);
291 mid = ntohl (msg->mid);
293 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
295 copy = GNUNET_malloc (sizeof (*copy) + size);
298 memcpy (©[1], msg, size);
302 // FIXME do something better than O(n), although n < 64...
303 // FIXME start from the end (most messages are the latest ones)
304 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
306 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
307 if (GMC_is_pid_bigger (prev->mid, mid))
309 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
310 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
315 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
316 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
317 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
322 * Send data to a client.
324 * If the client is ready, send directly, otherwise buffer while listening
328 * @param msg Message.
329 * @param fwd Is this a fwd (root->dest) message?
332 send_client_data (struct MeshChannel *ch,
333 const struct GNUNET_MESH_Data *msg,
338 if (ch->dest_rel->client_ready)
339 GML_send_data (ch, msg, ch->dest, ch->lid_dest);
341 add_buffered_data (msg, ch->dest_rel);
345 if (ch->root_rel->client_ready)
346 GML_send_data (ch, msg, ch->root, ch->lid_root);
348 add_buffered_data (msg, ch->root_rel);
354 * Add a client to a channel, initializing all needed data structures.
356 * @param ch Channel to which add the client.
357 * @param c Client which to add to the channel.
360 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
362 struct MeshTunnel3 *t = ch->t;
364 if (NULL != ch->dest)
370 /* Assign local id as destination */
371 while (NULL != GML_channel_get (c, t->next_local_chid))
372 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
373 ch->lid_dest = t->next_local_chid++;
374 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
376 /* Store in client's hashmap */
378 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
380 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
386 GNUNET_break (NULL == ch->dest_rel);
387 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
388 ch->dest_rel->ch = ch;
389 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
396 * Destroy all reliable messages queued for a channel,
397 * during a channel destruction.
398 * Frees the reliability structure itself.
400 * @param rel Reliability data for a channel.
403 channel_rel_free_all (struct MeshChannelReliability *rel)
405 struct MeshReliableMessage *copy;
406 struct MeshReliableMessage *next;
411 for (copy = rel->head_recv; NULL != copy; copy = next)
414 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
417 for (copy = rel->head_sent; NULL != copy; copy = next)
420 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
423 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
424 GNUNET_SCHEDULER_cancel (rel->retry_task);
430 * Mark future messages as ACK'd.
432 * @param rel Reliability data.
433 * @param msg DataACK message with a bitfield of future ACK'd messages.
436 channel_rel_free_sent (struct MeshChannelReliability *rel,
437 const struct GNUNET_MESH_DataACK *msg)
439 struct MeshReliableMessage *copy;
440 struct MeshReliableMessage *next;
447 bitfield = msg->futures;
448 mid = ntohl (msg->mid);
449 LOG (GNUNET_ERROR_TYPE_DEBUG,
450 "free_sent_reliable %u %llX\n",
452 LOG (GNUNET_ERROR_TYPE_DEBUG,
453 " rel %p, head %p\n",
454 rel, rel->head_sent);
455 for (i = 0, copy = rel->head_sent;
456 i < 64 && NULL != copy && 0 != bitfield;
459 LOG (GNUNET_ERROR_TYPE_DEBUG,
460 " trying bit %u (mid %u)\n",
463 if (0 == (bitfield & mask))
466 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
467 /* Bit was set, clear the bit from the bitfield */
470 /* The i-th bit was set. Do we have that copy? */
471 /* Skip copies with mid < target */
472 target = mid + i + 1;
473 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
474 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
477 /* Did we run out of copies? (previously freed, it's ok) */
480 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
484 /* Did we overshoot the target? (previously freed, it's ok) */
485 if (GMC_is_pid_bigger (copy->mid, target))
487 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
491 /* Now copy->mid == target, free it */
493 rel_message_free (copy);
496 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
501 * We haven't received an ACK after a certain time: restransmit the message.
503 * @param cls Closure (MeshReliableMessage with the message to restransmit)
504 * @param tc TaskContext.
507 channel_retransmit_message (void *cls,
508 const struct GNUNET_SCHEDULER_TaskContext *tc)
510 struct MeshChannelReliability *rel = cls;
511 struct MeshReliableMessage *copy;
512 struct MeshPeerQueue *q;
513 struct MeshChannel *ch;
514 struct MeshConnection *c;
515 struct GNUNET_MESH_Data *payload;
516 struct MeshPeer *hop;
519 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
520 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
524 copy = rel->head_sent;
531 /* Search the message to be retransmitted in the outgoing queue.
532 * Check only the queue for the connection that is going to be used,
533 * if the message is stuck in some other connection's queue we shouldn't
535 * - cancelling it and sending the new one doesn't guarantee it's delivery,
536 * the old connection could be temporary stalled or the queue happened to
537 * be long at time of insertion.
538 * - not sending the new one could cause terrible delays the old connection
541 payload = (struct GNUNET_MESH_Data *) ©[1];
542 fwd = (rel == ch->root_rel);
543 c = tunnel_get_connection (ch->t, fwd);
544 hop = connection_get_hop (c, fwd);
545 for (q = hop->queue_head; NULL != q; q = q->next)
547 if (ntohs (payload->header.type) == q->type && ch == q->ch)
549 struct GNUNET_MESH_Data *queued_data = q->cls;
551 if (queued_data->mid == payload->mid)
556 /* Message not found in the queue that we are going to use. */
559 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
561 send_prebuilt_message_channel (&payload->header, ch, fwd);
562 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
566 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
569 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
570 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
571 &channel_retransmit_message,
577 * Send ACK on one or more connections due to buffer space to the client.
579 * Iterates all connections of the tunnel and sends ACKs appropriately.
581 * @param ch Channel which has some free buffer space.
582 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
585 channel_send_connections_ack (struct MeshChannel *ch,
589 struct MeshTunnel3 *t = ch->t;
590 struct MeshConnection *c;
591 struct MeshFlowControl *fc;
594 uint32_t allow_per_connection;
597 LOG (GNUNET_ERROR_TYPE_DEBUG,
598 "Channel send connection %s ack on %s:%X\n",
599 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
601 /* Count connections, how many messages are already allowed */
602 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
604 fc = fwd ? &c->fwd_fc : &c->bck_fc;
605 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
610 allowed += fc->last_ack_sent - fc->last_pid_recv;
614 /* Make sure there is no overflow */
615 if (allowed > buffer)
621 /* Authorize connections to send more data */
622 to_allow = buffer - allowed;
624 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
626 allow_per_connection = to_allow/cs;
627 to_allow -= allow_per_connection;
629 fc = fwd ? &c->fwd_fc : &c->bck_fc;
630 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
634 connection_send_ack (c, allow_per_connection, fwd);
637 LOG (GNUNET_ERROR_TYPE_DEBUG,
638 "Channel send connection %s ack on %s:%X\n",
639 fwd ? "FWD" : "BCK", GMT_2s (ch->t), ch->gid);
640 GNUNET_break (to_allow == 0);
645 * Destroy a reliable message after it has been acknowledged, either by
646 * direct mid ACK or bitfield. Updates the appropriate data structures and
647 * timers and frees all memory.
649 * @param copy Message that is no longer needed: remote peer got it.
652 rel_message_free (struct MeshReliableMessage *copy)
654 struct MeshChannelReliability *rel;
655 struct GNUNET_TIME_Relative time;
658 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
659 rel->expected_delay.rel_value_us *= 7;
660 rel->expected_delay.rel_value_us += time.rel_value_us;
661 rel->expected_delay.rel_value_us /= 8;
663 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
664 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
665 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
666 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
667 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
668 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
670 rel->retry_timer = rel->expected_delay;
671 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
678 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
680 * @param ch Channel to mark as ready.
681 * @param fwd Was the CREATE message sent fwd?
684 channel_confirm (struct MeshChannel *ch, int fwd)
686 struct MeshChannelReliability *rel;
687 struct MeshReliableMessage *copy;
688 struct MeshReliableMessage *next;
690 LOG (GNUNET_ERROR_TYPE_DEBUG,
691 " channel confirm %s %s:%X\n",
692 fwd ? "FWD" : "BCK", GMT_2s (ch->t), ch->gid);
693 ch->state = MESH_CHANNEL_READY;
695 rel = fwd ? ch->root_rel : ch->dest_rel;
696 for (copy = rel->head_sent; NULL != copy; copy = next)
698 struct GNUNET_MessageHeader *msg;
701 msg = (struct GNUNET_MessageHeader *) ©[1];
702 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
704 rel_message_free (copy);
708 send_ack (NULL, ch, fwd);
713 * Save a copy to retransmit in case it gets lost.
715 * Initializes all needed callbacks and timers.
717 * @param ch Channel this message goes on.
718 * @param msg Message to copy.
719 * @param fwd Is this fwd traffic?
722 channel_save_copy (struct MeshChannel *ch,
723 const struct GNUNET_MessageHeader *msg,
726 struct MeshChannelReliability *rel;
727 struct MeshReliableMessage *copy;
732 rel = fwd ? ch->root_rel : ch->dest_rel;
734 type = ntohs (msg->type);
735 size = ntohs (msg->size);
737 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
738 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
740 copy->timestamp = GNUNET_TIME_absolute_get ();
743 memcpy (©[1], msg, size);
745 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
746 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
747 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
750 GNUNET_TIME_relative_multiply (rel->expected_delay,
751 MESH_RETRANSMIT_MARGIN);
753 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
754 &channel_retransmit_message,
762 * Send a buffered message to the client, for in order delivery or
763 * as result of client ACK.
765 * @param ch Channel on which to empty the message buffer.
766 * @param c Client to send to.
767 * @param rel Reliability structure to corresponding peer.
768 * If rel == bck_rel, this is FWD data.
771 send_client_buffered_data (struct MeshChannel *ch,
772 struct MeshClient *c,
775 struct MeshReliableMessage *copy;
776 struct MeshChannelReliability *rel;
778 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
779 rel = fwd ? ch->dest_rel : ch->root_rel;
780 if (GNUNET_NO == rel->client_ready)
782 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
786 copy = rel->head_recv;
787 /* We never buffer channel management messages */
790 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
792 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
794 LOG (GNUNET_ERROR_TYPE_DEBUG,
795 " have %u! now expecting %u\n",
796 copy->mid, rel->mid_recv + 1);
797 send_client_data (ch, msg, fwd);
800 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
805 LOG (GNUNET_ERROR_TYPE_DEBUG,
806 " reliable && don't have %u, next is %u\n",
812 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
819 * Destroy a channel and free all resources.
821 * @param ch Channel to destroy.
824 channel_destroy (struct MeshChannel *ch)
826 struct MeshClient *c;
831 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
832 GMT_2s (ch->t), ch->gid);
838 GML_channel_remove (c, ch->lid_root, ch);
844 GML_channel_remove (c, ch->lid_dest, ch);
847 channel_rel_free_all (ch->root_rel);
848 channel_rel_free_all (ch->dest_rel);
850 GMT_remove_channel (ch->t, ch);
851 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
858 * Create a new channel.
860 * @param t Tunnel this channel is in.
861 * @param owner Client that owns the channel, NULL for foreign channels.
862 * @param lid_root Local ID for root client.
864 * @return A new initialized channel. NULL on error.
866 static struct MeshChannel *
867 channel_new (struct MeshTunnel3 *t,
868 struct MeshClient *owner, MESH_ChannelNumber lid_root)
870 struct MeshChannel *ch;
872 ch = GNUNET_new (struct MeshChannel);
874 ch->lid_root = lid_root;
877 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
881 ch->gid = GMT_get_next_chid (t);
882 GML_channel_add (owner, lid_root, ch);
884 GMT_add_channel (t, ch);
891 * Set options in a channel, extracted from a bit flag field
893 * @param ch Channel to set options to.
894 * @param options Bit array in host byte order.
897 channel_set_options (struct MeshChannel *ch, uint32_t options)
899 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
900 GNUNET_YES : GNUNET_NO;
901 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
902 GNUNET_YES : GNUNET_NO;
908 * Confirm we got a channel create.
910 * @param ch The channel to confirm.
911 * @param fwd Should we send the ACK fwd?
914 channel_send_ack (struct MeshChannel *ch, int fwd)
916 struct GNUNET_MESH_ChannelManage msg;
918 msg.header.size = htons (sizeof (msg));
919 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
920 LOG (GNUNET_ERROR_TYPE_DEBUG,
921 " sending channel %s ack for channel %s:%X\n",
922 fwd ? "FWD" : "BCK", GMT_2s (ch->t),
925 msg.chid = htonl (ch->gid);
926 GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
931 * Iterator for deleting each channel whose client endpoint disconnected.
933 * @param cls Closure (client that has disconnected).
934 * @param key The local channel id (used to access the hashmap).
935 * @param value The value stored at the key (channel to destroy).
937 * @return GNUNET_OK, keep iterating.
940 channel_destroy_iterator (void *cls,
944 struct MeshChannel *ch = value;
945 struct MeshClient *c = cls;
946 struct MeshTunnel3 *t;
948 LOG (GNUNET_ERROR_TYPE_DEBUG,
949 " Channel %X (%X / %X) destroy, due to client %s shutdown.\n",
950 ch->gid, ch->lid_root, ch->lid_dest, GML_2s (c));
955 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
959 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
963 GMCH_send_destroy (ch);
964 channel_destroy (ch);
965 GMT_destroy_if_empty (t);
972 * Handle a loopback message: call the appropriate handler for the message type.
974 * @param ch Channel this message is on.
975 * @param msgh Message header.
976 * @param fwd Is this FWD traffic?
979 handle_loopback (struct MeshChannel *ch,
980 struct GNUNET_MessageHeader *msgh,
985 type = ntohs (msgh->type);
986 LOG (GNUNET_ERROR_TYPE_DEBUG,
987 "Loopback %s message!\n",
988 GNUNET_MESH_DEBUG_M2S (type));
992 case GNUNET_MESSAGE_TYPE_MESH_DATA:
993 /* Don't send hop ACK, wait for client to ACK */
994 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
997 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
998 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1001 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1002 // FIXME store channel in loopback tunnel?
1003 GMCH_handle_create ((struct GNUNET_MESH_ChannelCreate *) msgh,
1007 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1008 GMCH_handle_ack (ch,
1009 (struct GNUNET_MESH_ChannelManage *) msgh,
1013 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1014 GMCH_handle_destroy (ch,
1015 (struct GNUNET_MESH_ChannelManage *) msgh,
1020 GNUNET_break_op (0);
1021 LOG (GNUNET_ERROR_TYPE_DEBUG,
1022 "end-to-end message not known (%u)\n",
1023 ntohs (msgh->type));
1029 /******************************************************************************/
1030 /******************************** API ***********************************/
1031 /******************************************************************************/
1036 * @param ch Channel.
1041 GMCH_get_id (const struct MeshChannel *ch)
1048 * Get the channel tunnel.
1050 * @param ch Channel to get the tunnel from.
1052 * @return tunnel of the channel.
1054 struct MeshTunnel3 *
1055 GMCH_get_tunnel (const struct MeshChannel *ch)
1062 * Get free buffer space towards the client on a specific channel.
1064 * @param ch Channel.
1065 * @param fwd Is query about FWD traffic?
1067 * @return Free buffer space [0 - 64]
1070 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
1072 struct MeshChannelReliability *rel;
1074 rel = fwd ? ch->dest_rel : ch->root_rel;
1076 /* If rel is NULL it means that the end is not yet created,
1077 * most probably is a loopback channel at the point of sending
1078 * the ChannelCreate to itself.
1083 return (64 - rel->n_recv);
1088 * Is the root client for this channel on this peer?
1090 * @param ch Channel.
1091 * @param fwd Is this for fwd traffic?
1093 * @return GNUNET_YES in case it is.
1096 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1098 struct MeshClient *c;
1100 c = fwd ? ch->root : ch->dest;
1106 * Is the destination client for this channel on this peer?
1108 * @param ch Channel.
1109 * @param fwd Is this for fwd traffic?
1111 * @return GNUNET_YES in case it is.
1114 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1116 struct MeshClient *c;
1118 c = fwd ? ch->dest : ch->root;
1124 * Notify the destination client that a new incoming channel was created.
1126 * @param ch Channel that was created.
1129 GMCH_send_create (struct MeshChannel *ch)
1131 struct GNUNET_MESH_ChannelMessage msg;
1134 if (NULL == ch->dest)
1138 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1139 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1140 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1141 GMT_get_destination (ch->t));
1146 * Notify a client that the channel is no longer valid.
1147 * FIXME send on tunnel if some client == NULL?
1149 * @param ch Channel that is destroyed.
1152 GMCH_send_destroy (struct MeshChannel *ch)
1154 if (NULL != ch->root)
1155 GML_send_channel_destroy (ch->root, ch->lid_root);
1157 if (NULL != ch->dest)
1158 GML_send_channel_destroy (ch->dest, ch->lid_dest);
1163 * Send data on a channel.
1165 * If the destination is local, send it to client, otherwise encrypt and
1169 * @param msg Message.
1170 * @param fwd Is this a fwd (root->dest) message?
1173 GMCH_send_data (struct MeshChannel *ch,
1174 const struct GNUNET_MESH_Data *msg,
1181 * Send an end-to-end ACK message for the most recent in-sequence payload.
1183 * If channel is not reliable, do nothing.
1185 * @param ch Channel this is about.
1186 * @param fwd Is for FWD traffic? (ACK dest->owner)
1189 GMCH_send_ack (struct MeshChannel *ch, int fwd)
1191 struct GNUNET_MESH_DataACK msg;
1192 struct MeshChannelReliability *rel;
1193 struct MeshReliableMessage *copy;
1198 if (GNUNET_NO == ch->reliable)
1202 rel = fwd ? ch->dest_rel : ch->root_rel;
1203 LOG (GNUNET_ERROR_TYPE_DEBUG,
1204 "send_data_ack for %u\n",
1207 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
1208 msg.header.type = htons (type);
1209 msg.header.size = htons (sizeof (msg));
1210 msg.chid = htonl (ch->gid);
1211 msg.mid = htonl (rel->mid_recv - 1);
1213 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1215 if (copy->type != type)
1217 delta = copy->mid - rel->mid_recv;
1220 mask = 0x1LL << delta;
1221 msg.futures |= mask;
1222 LOG (GNUNET_ERROR_TYPE_DEBUG,
1223 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1224 copy->mid, delta, mask, msg.futures);
1226 LOG (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
1228 GMCH_send_prebuilt_message (&msg.header, ch, fwd);
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1236 * @param ch Channel.
1239 GMCH_debug (struct MeshChannel *ch)
1243 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1246 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1247 GMT_2s (ch->t), ch->gid, ch);
1248 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1249 ch->root, ch->root_rel);
1250 if (NULL != ch->root)
1252 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1253 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1254 ch->root_rel->client_ready ? "YES" : "NO");
1255 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1257 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1258 ch->dest, ch->dest_rel);
1259 if (NULL != ch->dest)
1261 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1262 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1263 ch->dest_rel->client_ready ? "YES" : "NO");
1264 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1270 * Handler for mesh network payload traffic.
1272 * @param ch Channel for the message.
1273 * @param message Unencryted data message.
1274 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1277 GMCH_handle_data (struct MeshChannel *ch,
1278 const struct GNUNET_MESH_Data *msg,
1281 struct MeshChannelReliability *rel;
1282 struct MeshClient *c;
1287 /* Initialize FWD/BCK data */
1288 c = fwd ? ch->dest : ch->root;
1289 rel = fwd ? ch->dest_rel : ch->root_rel;
1297 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1299 mid = ntohl (msg->mid);
1300 LOG (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
1302 if (GNUNET_NO == ch->reliable ||
1303 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
1304 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1307 if (GNUNET_YES == ch->reliable)
1309 /* Is this the exact next expected messasge? */
1310 if (mid == rel->mid_recv)
1312 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1314 send_client_data (ch, msg, fwd);
1318 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1319 add_buffered_data (msg, rel);
1324 /* Tunnel is unreliable: send to clients directly */
1325 /* FIXME: accept Out Of Order traffic */
1326 rel->mid_recv = mid + 1;
1327 send_client_data (ch, msg, fwd);
1332 GNUNET_break_op (0);
1333 LOG (GNUNET_ERROR_TYPE_DEBUG,
1334 " MID %u not expected (%u - %u), dropping!\n",
1335 mid, rel->mid_recv, rel->mid_recv + 64);
1338 GMCH_send_ack (ch, fwd);
1343 * Handler for mesh network traffic end-to-end ACKs.
1345 * @param t Tunnel on which we got this message.
1346 * @param message Data message.
1347 * @param fwd Is this a fwd ACK? (dest->orig)
1350 GMCH_handle_data_ack (struct MeshChannel *ch,
1351 const struct GNUNET_MESH_DataACK *msg,
1354 struct MeshChannelReliability *rel;
1355 struct MeshReliableMessage *copy;
1356 struct MeshReliableMessage *next;
1360 ack = ntohl (msg->mid);
1361 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1362 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1364 if (GNUNET_YES == fwd)
1378 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1380 if (GMC_is_pid_bigger (copy->mid, ack))
1382 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1383 channel_rel_free_sent (rel, msg);
1387 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
1389 rel_message_free (copy);
1391 /* ACK client if needed */
1392 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
1394 /* If some message was free'd, update the retransmission delay*/
1395 if (GNUNET_YES == work)
1397 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1399 GNUNET_SCHEDULER_cancel (rel->retry_task);
1400 if (NULL == rel->head_sent)
1402 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1406 struct GNUNET_TIME_Absolute new_target;
1407 struct GNUNET_TIME_Relative delay;
1409 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1410 MESH_RETRANSMIT_MARGIN);
1411 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1413 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1415 GNUNET_SCHEDULER_add_delayed (delay,
1416 &channel_retransmit_message,
1427 * Handler for channel create messages.
1429 * @param msg Message.
1430 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1432 struct MeshChannel *
1433 GMCH_handle_create (const struct GNUNET_MESH_ChannelCreate *msg,
1436 MESH_ChannelNumber chid;
1437 struct MeshChannel *ch;
1438 struct MeshClient *c;
1441 chid = ntohl (msg->chid);
1443 /* Create channel */
1444 ch = channel_new (NULL, NULL, 0); /* FIXME t */
1446 channel_set_options (ch, ntohl (msg->opt));
1448 /* Find a destination client */
1449 port = ntohl (msg->port);
1450 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
1451 c = GML_client_get_by_port (port);
1454 /* TODO send reject */
1455 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
1460 channel_add_client (ch, c);
1461 if (GNUNET_YES == ch->reliable)
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1464 GMCH_send_create (ch);
1465 GMCH_send_ack (ch, fwd);
1466 GML_send_ack (ch, !fwd);
1473 * Handler for channel ack messages.
1475 * @param ch Channel.
1476 * @param msg Message.
1477 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1480 GMCH_handle_ack (struct MeshChannel *ch,
1481 const struct GNUNET_MESH_ChannelManage *msg,
1484 channel_confirm (ch, !fwd);
1489 * Handler for channel destroy messages.
1491 * @param ch Channel to be destroyed of.
1492 * @param msg Message.
1493 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1496 GMCH_handle_destroy (struct MeshChannel *ch,
1497 const struct GNUNET_MESH_ChannelManage *msg,
1500 MESH_ChannelNumber chid;
1502 /* Check if channel exists */
1503 chid = ntohl (msg->chid);
1504 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
1506 /* Not for us (don't destroy twice a half-open loopback channel) */
1510 GMCH_send_destroy (ch);
1511 channel_destroy (ch);
1516 * Sends an already built message on a channel.
1518 * @param message Message to send. Function makes a copy of it.
1519 * @param ch Channel on which this message is transmitted.
1520 * @param fwd Is this a fwd message?
1523 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1524 struct MeshChannel *ch, int fwd)
1526 size_t size = ntohs (message->size);
1528 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
1529 GMT_2s (ch->t), ch->gid, fwd ? "FWD" : "BCK");
1530 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1531 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1533 if (GMT_is_loopback (ch->t))
1535 handle_loopback (ch->t, message, fwd);
1539 GMT_send_prebuilt_message (message, ch->t, ch, fwd);