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->dest, msg, ch->lid_dest);
343 add_buffered_data (msg, ch->dest_rel);
347 if (ch->root_rel->client_ready)
348 GML_send_data (ch->root, msg, 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 if (NULL != ch->dest)
370 /* Assign local id as destination */
371 ch->lid_dest = GML_get_next_chid (c);
373 /* Store in client's hashmap */
374 GML_channel_add (c, ch->lid_dest, ch);
376 GNUNET_break (NULL == ch->dest_rel);
377 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
378 ch->dest_rel->ch = ch;
379 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
386 * Destroy all reliable messages queued for a channel,
387 * during a channel destruction.
388 * Frees the reliability structure itself.
390 * @param rel Reliability data for a channel.
393 channel_rel_free_all (struct MeshChannelReliability *rel)
395 struct MeshReliableMessage *copy;
396 struct MeshReliableMessage *next;
401 for (copy = rel->head_recv; NULL != copy; copy = next)
404 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
407 for (copy = rel->head_sent; NULL != copy; copy = next)
410 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
413 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
414 GNUNET_SCHEDULER_cancel (rel->retry_task);
420 * Mark future messages as ACK'd.
422 * @param rel Reliability data.
423 * @param msg DataACK message with a bitfield of future ACK'd messages.
426 channel_rel_free_sent (struct MeshChannelReliability *rel,
427 const struct GNUNET_MESH_DataACK *msg)
429 struct MeshReliableMessage *copy;
430 struct MeshReliableMessage *next;
437 bitfield = msg->futures;
438 mid = ntohl (msg->mid);
439 LOG (GNUNET_ERROR_TYPE_DEBUG,
440 "free_sent_reliable %u %llX\n",
442 LOG (GNUNET_ERROR_TYPE_DEBUG,
443 " rel %p, head %p\n",
444 rel, rel->head_sent);
445 for (i = 0, copy = rel->head_sent;
446 i < 64 && NULL != copy && 0 != bitfield;
449 LOG (GNUNET_ERROR_TYPE_DEBUG,
450 " trying bit %u (mid %u)\n",
453 if (0 == (bitfield & mask))
456 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
457 /* Bit was set, clear the bit from the bitfield */
460 /* The i-th bit was set. Do we have that copy? */
461 /* Skip copies with mid < target */
462 target = mid + i + 1;
463 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
464 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
467 /* Did we run out of copies? (previously freed, it's ok) */
470 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
474 /* Did we overshoot the target? (previously freed, it's ok) */
475 if (GMC_is_pid_bigger (copy->mid, target))
477 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
481 /* Now copy->mid == target, free it */
483 rel_message_free (copy);
486 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
491 * We haven't received an ACK after a certain time: restransmit the message.
493 * @param cls Closure (MeshReliableMessage with the message to restransmit)
494 * @param tc TaskContext.
497 channel_retransmit_message (void *cls,
498 const struct GNUNET_SCHEDULER_TaskContext *tc)
500 struct MeshChannelReliability *rel = cls;
501 struct MeshReliableMessage *copy;
502 struct MeshChannel *ch;
503 struct GNUNET_MESH_Data *payload;
506 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
507 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
511 copy = rel->head_sent;
518 /* Search the message to be retransmitted in the outgoing queue.
519 * Check only the queue for the connection that is going to be used,
520 * if the message is stuck in some other connection's queue we shouldn't
522 * - cancelling it and sending the new one doesn't guarantee it's delivery,
523 * the old connection could be temporary stalled or the queue happened to
524 * be long at time of insertion.
525 * - not sending the new one could cause terrible delays the old connection
528 // FIXME access to queue elements is limited
529 payload = (struct GNUNET_MESH_Data *) ©[1];
530 fwd = (rel == ch->root_rel);
531 // c = GMT_get_connection (ch->t, fwd);
532 // hop = connection_get_hop (c, fwd);
533 // for (q = hop->queue_head; NULL != q; q = q->next)
535 // if (ntohs (payload->header.type) == q->type && ch == q->ch)
537 // struct GNUNET_MESH_Data *queued_data = q->cls;
539 // if (queued_data->mid == payload->mid)
544 /* Message not found in the queue that we are going to use. */
547 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
549 GMCH_send_prebuilt_message (&payload->header, ch, fwd);
550 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
554 // LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
557 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
558 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
559 &channel_retransmit_message,
565 * Destroy a reliable message after it has been acknowledged, either by
566 * direct mid ACK or bitfield. Updates the appropriate data structures and
567 * timers and frees all memory.
569 * @param copy Message that is no longer needed: remote peer got it.
572 rel_message_free (struct MeshReliableMessage *copy)
574 struct MeshChannelReliability *rel;
575 struct GNUNET_TIME_Relative time;
578 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
579 rel->expected_delay.rel_value_us *= 7;
580 rel->expected_delay.rel_value_us += time.rel_value_us;
581 rel->expected_delay.rel_value_us /= 8;
583 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
584 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
585 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
586 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
587 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
588 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
590 rel->retry_timer = rel->expected_delay;
591 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
598 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
600 * @param ch Channel to mark as ready.
601 * @param fwd Was the CREATE message sent fwd?
604 channel_confirm (struct MeshChannel *ch, int fwd)
606 struct MeshChannelReliability *rel;
607 struct MeshReliableMessage *copy;
608 struct MeshReliableMessage *next;
610 LOG (GNUNET_ERROR_TYPE_DEBUG,
611 " channel confirm %s %s:%X\n",
612 fwd ? "FWD" : "BCK", GMT_2s (ch->t), ch->gid);
613 ch->state = MESH_CHANNEL_READY;
615 rel = fwd ? ch->root_rel : ch->dest_rel;
616 for (copy = rel->head_sent; NULL != copy; copy = next)
618 struct GNUNET_MessageHeader *msg;
621 msg = (struct GNUNET_MessageHeader *) ©[1];
622 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
624 rel_message_free (copy);
628 send_ack (NULL, ch, fwd);
633 * Save a copy to retransmit in case it gets lost.
635 * Initializes all needed callbacks and timers.
637 * @param ch Channel this message goes on.
638 * @param msg Message to copy.
639 * @param fwd Is this fwd traffic?
642 channel_save_copy (struct MeshChannel *ch,
643 const struct GNUNET_MessageHeader *msg,
646 struct MeshChannelReliability *rel;
647 struct MeshReliableMessage *copy;
652 rel = fwd ? ch->root_rel : ch->dest_rel;
654 type = ntohs (msg->type);
655 size = ntohs (msg->size);
657 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
658 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
660 copy->timestamp = GNUNET_TIME_absolute_get ();
663 memcpy (©[1], msg, size);
665 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
666 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
667 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
670 GNUNET_TIME_relative_multiply (rel->expected_delay,
671 MESH_RETRANSMIT_MARGIN);
673 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
674 &channel_retransmit_message,
682 * Send a buffered message to the client, for in order delivery or
683 * as result of client ACK.
685 * @param ch Channel on which to empty the message buffer.
686 * @param c Client to send to.
687 * @param rel Reliability structure to corresponding peer.
688 * If rel == bck_rel, this is FWD data.
691 send_client_buffered_data (struct MeshChannel *ch,
692 struct MeshClient *c,
695 struct MeshReliableMessage *copy;
696 struct MeshChannelReliability *rel;
698 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
699 rel = fwd ? ch->dest_rel : ch->root_rel;
700 if (GNUNET_NO == rel->client_ready)
702 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
706 copy = rel->head_recv;
707 /* We never buffer channel management messages */
710 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
712 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
714 LOG (GNUNET_ERROR_TYPE_DEBUG,
715 " have %u! now expecting %u\n",
716 copy->mid, rel->mid_recv + 1);
717 send_client_data (ch, msg, fwd);
720 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
725 LOG (GNUNET_ERROR_TYPE_DEBUG,
726 " reliable && don't have %u, next is %u\n",
732 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
739 * Destroy a channel and free all resources.
741 * @param ch Channel to destroy.
744 channel_destroy (struct MeshChannel *ch)
746 struct MeshClient *c;
751 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
752 GMT_2s (ch->t), ch->gid);
758 GML_channel_remove (c, ch->lid_root, ch);
764 GML_channel_remove (c, ch->lid_dest, ch);
767 channel_rel_free_all (ch->root_rel);
768 channel_rel_free_all (ch->dest_rel);
770 GMT_remove_channel (ch->t, ch);
771 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
778 * Create a new channel.
780 * @param t Tunnel this channel is in.
781 * @param owner Client that owns the channel, NULL for foreign channels.
782 * @param lid_root Local ID for root client.
784 * @return A new initialized channel. NULL on error.
786 static struct MeshChannel *
787 channel_new (struct MeshTunnel3 *t,
788 struct MeshClient *owner, MESH_ChannelNumber lid_root)
790 struct MeshChannel *ch;
792 ch = GNUNET_new (struct MeshChannel);
794 ch->lid_root = lid_root;
797 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
801 ch->gid = GMT_get_next_chid (t);
802 GML_channel_add (owner, lid_root, ch);
804 GMT_add_channel (t, ch);
811 * Set options in a channel, extracted from a bit flag field
813 * @param ch Channel to set options to.
814 * @param options Bit array in host byte order.
817 channel_set_options (struct MeshChannel *ch, uint32_t options)
819 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
820 GNUNET_YES : GNUNET_NO;
821 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
822 GNUNET_YES : GNUNET_NO;
828 * Confirm we got a channel create.
830 * @param ch The channel to confirm.
831 * @param fwd Should we send the ACK fwd?
834 channel_send_ack (struct MeshChannel *ch, int fwd)
836 struct GNUNET_MESH_ChannelManage msg;
838 msg.header.size = htons (sizeof (msg));
839 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
840 LOG (GNUNET_ERROR_TYPE_DEBUG,
841 " sending channel %s ack for channel %s:%X\n",
842 fwd ? "FWD" : "BCK", GMT_2s (ch->t),
845 msg.chid = htonl (ch->gid);
846 GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
851 * Iterator for deleting each channel whose client endpoint disconnected.
853 * @param cls Closure (client that has disconnected).
854 * @param key The local channel id (used to access the hashmap).
855 * @param value The value stored at the key (channel to destroy).
857 * @return GNUNET_OK, keep iterating.
860 channel_destroy_iterator (void *cls,
864 struct MeshChannel *ch = value;
865 struct MeshClient *c = cls;
866 struct MeshTunnel3 *t;
868 LOG (GNUNET_ERROR_TYPE_DEBUG,
869 " Channel %X (%X / %X) destroy, due to client %s shutdown.\n",
870 ch->gid, ch->lid_root, ch->lid_dest, GML_2s (c));
875 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
879 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
883 GMCH_send_destroy (ch);
884 channel_destroy (ch);
885 GMT_destroy_if_empty (t);
892 * Handle a loopback message: call the appropriate handler for the message type.
894 * @param ch Channel this message is on.
895 * @param msgh Message header.
896 * @param fwd Is this FWD traffic?
899 handle_loopback (struct MeshChannel *ch,
900 const struct GNUNET_MessageHeader *msgh,
905 type = ntohs (msgh->type);
906 LOG (GNUNET_ERROR_TYPE_DEBUG,
907 "Loopback %s message!\n",
908 GNUNET_MESH_DEBUG_M2S (type));
912 case GNUNET_MESSAGE_TYPE_MESH_DATA:
913 /* Don't send hop ACK, wait for client to ACK */
914 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
917 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
918 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
921 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
922 // FIXME store channel in loopback tunnel?
923 GMCH_handle_create ((struct GNUNET_MESH_ChannelCreate *) msgh,
927 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
929 (struct GNUNET_MESH_ChannelManage *) msgh,
933 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
934 GMCH_handle_destroy (ch,
935 (struct GNUNET_MESH_ChannelManage *) msgh,
941 LOG (GNUNET_ERROR_TYPE_DEBUG,
942 "end-to-end message not known (%u)\n",
949 /******************************************************************************/
950 /******************************** API ***********************************/
951 /******************************************************************************/
961 GMCH_get_id (const struct MeshChannel *ch)
968 * Get the channel tunnel.
970 * @param ch Channel to get the tunnel from.
972 * @return tunnel of the channel.
975 GMCH_get_tunnel (const struct MeshChannel *ch)
982 * Get free buffer space towards the client on a specific channel.
985 * @param fwd Is query about FWD traffic?
987 * @return Free buffer space [0 - 64]
990 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
992 struct MeshChannelReliability *rel;
994 rel = fwd ? ch->dest_rel : ch->root_rel;
996 /* If rel is NULL it means that the end is not yet created,
997 * most probably is a loopback channel at the point of sending
998 * the ChannelCreate to itself.
1003 return (64 - rel->n_recv);
1008 * Is the root client for this channel on this peer?
1010 * @param ch Channel.
1011 * @param fwd Is this for fwd traffic?
1013 * @return GNUNET_YES in case it is.
1016 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1018 struct MeshClient *c;
1020 c = fwd ? ch->root : ch->dest;
1026 * Is the destination client for this channel on this peer?
1028 * @param ch Channel.
1029 * @param fwd Is this for fwd traffic?
1031 * @return GNUNET_YES in case it is.
1034 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1036 struct MeshClient *c;
1038 c = fwd ? ch->dest : ch->root;
1044 * Notify the destination client that a new incoming channel was created.
1046 * @param ch Channel that was created.
1049 GMCH_send_create (struct MeshChannel *ch)
1053 if (NULL == ch->dest)
1057 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1058 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1059 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1060 GMT_get_destination (ch->t));
1065 * Notify a client that the channel is no longer valid.
1066 * FIXME send on tunnel if some client == NULL?
1068 * @param ch Channel that is destroyed.
1071 GMCH_send_destroy (struct MeshChannel *ch)
1073 if (NULL != ch->root)
1074 GML_send_channel_destroy (ch->root, ch->lid_root);
1076 if (NULL != ch->dest)
1077 GML_send_channel_destroy (ch->dest, ch->lid_dest);
1082 * Send data on a channel.
1084 * If the destination is local, send it to client, otherwise encrypt and
1088 * @param msg Message.
1089 * @param fwd Is this a fwd (root->dest) message?
1092 GMCH_send_data (struct MeshChannel *ch,
1093 const struct GNUNET_MESH_Data *msg,
1100 * Send an end-to-end ACK message for the most recent in-sequence payload.
1102 * If channel is not reliable, do nothing.
1104 * @param ch Channel this is about.
1105 * @param fwd Is for FWD traffic? (ACK dest->owner)
1108 GMCH_send_ack (struct MeshChannel *ch, int fwd)
1110 struct GNUNET_MESH_DataACK msg;
1111 struct MeshChannelReliability *rel;
1112 struct MeshReliableMessage *copy;
1117 if (GNUNET_NO == ch->reliable)
1121 rel = fwd ? ch->dest_rel : ch->root_rel;
1122 LOG (GNUNET_ERROR_TYPE_DEBUG,
1123 "send_data_ack for %u\n",
1126 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
1127 msg.header.type = htons (type);
1128 msg.header.size = htons (sizeof (msg));
1129 msg.chid = htonl (ch->gid);
1130 msg.mid = htonl (rel->mid_recv - 1);
1132 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1134 if (copy->type != type)
1136 delta = copy->mid - rel->mid_recv;
1139 mask = 0x1LL << delta;
1140 msg.futures |= mask;
1141 LOG (GNUNET_ERROR_TYPE_DEBUG,
1142 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1143 copy->mid, delta, mask, msg.futures);
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
1147 GMCH_send_prebuilt_message (&msg.header, ch, fwd);
1148 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1155 * @param ch Channel.
1158 GMCH_debug (struct MeshChannel *ch)
1162 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1165 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1166 GMT_2s (ch->t), ch->gid, ch);
1167 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1168 ch->root, ch->root_rel);
1169 if (NULL != ch->root)
1171 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1172 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1173 ch->root_rel->client_ready ? "YES" : "NO");
1174 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1176 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1177 ch->dest, ch->dest_rel);
1178 if (NULL != ch->dest)
1180 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1182 ch->dest_rel->client_ready ? "YES" : "NO");
1183 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1189 * Handler for mesh network payload traffic.
1191 * @param ch Channel for the message.
1192 * @param message Unencryted data message.
1193 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1196 GMCH_handle_data (struct MeshChannel *ch,
1197 const struct GNUNET_MESH_Data *msg,
1200 struct MeshChannelReliability *rel;
1201 struct MeshClient *c;
1204 /* Initialize FWD/BCK data */
1205 c = fwd ? ch->dest : ch->root;
1206 rel = fwd ? ch->dest_rel : ch->root_rel;
1214 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1216 mid = ntohl (msg->mid);
1217 LOG (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
1219 if (GNUNET_NO == ch->reliable ||
1220 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
1221 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1223 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1224 if (GNUNET_YES == ch->reliable)
1226 /* Is this the exact next expected messasge? */
1227 if (mid == rel->mid_recv)
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1231 send_client_data (ch, msg, fwd);
1235 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1236 add_buffered_data (msg, rel);
1241 /* Tunnel is unreliable: send to clients directly */
1242 /* FIXME: accept Out Of Order traffic */
1243 rel->mid_recv = mid + 1;
1244 send_client_data (ch, msg, fwd);
1249 GNUNET_break_op (0);
1250 LOG (GNUNET_ERROR_TYPE_DEBUG,
1251 " MID %u not expected (%u - %u), dropping!\n",
1252 mid, rel->mid_recv, rel->mid_recv + 64);
1255 GMCH_send_ack (ch, fwd);
1260 * Handler for mesh network traffic end-to-end ACKs.
1262 * @param t Tunnel on which we got this message.
1263 * @param message Data message.
1264 * @param fwd Is this a fwd ACK? (dest->orig)
1267 GMCH_handle_data_ack (struct MeshChannel *ch,
1268 const struct GNUNET_MESH_DataACK *msg,
1271 struct MeshChannelReliability *rel;
1272 struct MeshReliableMessage *copy;
1273 struct MeshReliableMessage *next;
1277 ack = ntohl (msg->mid);
1278 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1279 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1281 if (GNUNET_YES == fwd)
1295 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1297 if (GMC_is_pid_bigger (copy->mid, ack))
1299 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1300 channel_rel_free_sent (rel, msg);
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
1306 rel_message_free (copy);
1308 /* ACK client if needed */
1309 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
1311 /* If some message was free'd, update the retransmission delay*/
1312 if (GNUNET_YES == work)
1314 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1316 GNUNET_SCHEDULER_cancel (rel->retry_task);
1317 if (NULL == rel->head_sent)
1319 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1323 struct GNUNET_TIME_Absolute new_target;
1324 struct GNUNET_TIME_Relative delay;
1326 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1327 MESH_RETRANSMIT_MARGIN);
1328 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1330 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1332 GNUNET_SCHEDULER_add_delayed (delay,
1333 &channel_retransmit_message,
1344 * Handler for channel create messages.
1346 * @param msg Message.
1347 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1349 struct MeshChannel *
1350 GMCH_handle_create (const struct GNUNET_MESH_ChannelCreate *msg,
1353 MESH_ChannelNumber chid;
1354 struct MeshChannel *ch;
1355 struct MeshClient *c;
1358 chid = ntohl (msg->chid);
1360 /* Create channel */
1361 ch = channel_new (NULL, NULL, 0); /* FIXME pass t */
1363 channel_set_options (ch, ntohl (msg->opt));
1365 /* Find a destination client */
1366 port = ntohl (msg->port);
1367 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
1368 c = GML_client_get_by_port (port);
1371 /* TODO send reject */
1372 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
1373 channel_destroy (ch);
1377 GMCH_add_client (ch, c);
1378 if (GNUNET_YES == ch->reliable)
1379 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1381 GMCH_send_create (ch);
1382 GMCH_send_ack (ch, fwd);
1384 if (GNUNET_NO == ch->dest_rel->client_ready)
1386 GML_send_ack (ch->dest, ch->lid_dest);
1387 ch->dest_rel->client_ready = GNUNET_YES;
1395 * Handler for channel ack messages.
1397 * @param ch Channel.
1398 * @param msg Message.
1399 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1402 GMCH_handle_ack (struct MeshChannel *ch,
1403 const struct GNUNET_MESH_ChannelManage *msg,
1406 channel_confirm (ch, !fwd);
1411 * Handler for channel destroy messages.
1413 * @param ch Channel to be destroyed of.
1414 * @param msg Message.
1415 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1418 GMCH_handle_destroy (struct MeshChannel *ch,
1419 const struct GNUNET_MESH_ChannelManage *msg,
1422 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
1424 /* Not for us (don't destroy twice a half-open loopback channel) */
1428 GMCH_send_destroy (ch);
1429 channel_destroy (ch);
1434 * Sends an already built message on a channel.
1436 * @param message Message to send. Function makes a copy of it.
1437 * @param ch Channel on which this message is transmitted.
1438 * @param fwd Is this a fwd message?
1441 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1442 struct MeshChannel *ch, int fwd)
1444 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
1445 GMT_2s (ch->t), ch->gid, fwd ? "FWD" : "BCK");
1446 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1447 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1449 if (GMT_is_loopback (ch->t))
1451 handle_loopback (ch, message, fwd);
1455 GMT_send_prebuilt_message (message, ch->t, ch, fwd);