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"
33 #include "gnunet-service-mesh_peer.h"
35 #define LOG(level, ...) GNUNET_log_from(level,"mesh-chn",__VA_ARGS__)
37 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
38 #define MESH_RETRANSMIT_MARGIN 4
42 * All the states a connection can be in.
47 * Uninitialized status, should never appear in operation.
52 * Connection create message sent, waiting for ACK.
57 * Connection confirmed, ready to carry traffic..
65 * Info needed to retry a message in case it gets lost.
67 struct MeshReliableMessage
70 * Double linked list, FIFO style
72 struct MeshReliableMessage *next;
73 struct MeshReliableMessage *prev;
76 * Type of message (payload, channel management).
81 * Tunnel Reliability queue this message is in.
83 struct MeshChannelReliability *rel;
86 * ID of the message (ACK needed to free)
91 * When was this message issued (to calculate ACK delay)
93 struct GNUNET_TIME_Absolute timestamp;
95 /* struct GNUNET_MESH_Data with payload */
100 * Info about the traffic state for a client in a channel.
102 struct MeshChannelReliability
105 * Channel this is about.
107 struct MeshChannel *ch;
110 * DLL of messages sent and not yet ACK'd.
112 struct MeshReliableMessage *head_sent;
113 struct MeshReliableMessage *tail_sent;
116 * Messages pending to send.
121 * DLL of messages received out of order.
123 struct MeshReliableMessage *head_recv;
124 struct MeshReliableMessage *tail_recv;
132 * Next MID to use for outgoing traffic.
137 * Next MID expected for incoming traffic.
142 * Can we send data to the client?
147 * Task to resend/poll in case no ACK is received.
149 GNUNET_SCHEDULER_TaskIdentifier retry_task;
152 * Counter for exponential backoff.
154 struct GNUNET_TIME_Relative retry_timer;
157 * How long does it usually take to get an ACK.
159 struct GNUNET_TIME_Relative expected_delay;
164 * Struct containing all information regarding a channel to a remote client.
169 * Tunnel this channel is in.
171 struct MeshTunnel3 *t;
174 * Destination port of the channel.
179 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
181 MESH_ChannelNumber gid;
184 * Local tunnel number for root (owner) client.
185 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
187 MESH_ChannelNumber lid_root;
190 * Local tunnel number for local destination clients (incoming number)
191 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
193 MESH_ChannelNumber lid_dest;
198 enum MeshChannelState state;
201 * Is the tunnel bufferless (minimum latency)?
206 * Is the tunnel reliable?
211 * Last time the channel was used
213 struct GNUNET_TIME_Absolute timestamp;
216 * Client owner of the tunnel, if any
218 struct MeshClient *root;
221 * Client destination of the tunnel, if any.
223 struct MeshClient *dest;
226 * Flag to signal the destruction of the channel.
227 * If this is set GNUNET_YES the channel will be destroyed
228 * when the queue is empty.
233 * Total messages pending for this channel, payload or not.
235 unsigned int pending_messages;
239 * Only present (non-NULL) at the owner of a tunnel.
241 struct MeshChannelReliability *root_rel;
245 * Only present (non-NULL) at the destination of a tunnel.
247 struct MeshChannelReliability *dest_rel;
252 /******************************************************************************/
253 /******************************* GLOBALS ***********************************/
254 /******************************************************************************/
257 * Global handle to the statistics service.
259 extern struct GNUNET_STATISTICS_Handle *stats;
262 * Local peer own ID (memory efficient handle).
264 extern GNUNET_PEER_Id myid;
267 /******************************************************************************/
268 /******************************** STATIC ***********************************/
269 /******************************************************************************/
272 * Destroy a reliable message after it has been acknowledged, either by
273 * direct mid ACK or bitfield. Updates the appropriate data structures and
274 * timers and frees all memory.
276 * @param copy Message that is no longer needed: remote peer got it.
279 rel_message_free (struct MeshReliableMessage *copy);
282 * We have received a message out of order, or the client is not ready.
283 * Buffer it until we receive an ACK from the client or the missing
284 * message from the channel.
286 * @param msg Message to buffer (MUST be of type MESH_DATA).
287 * @param rel Reliability data to the corresponding direction.
290 add_buffered_data (const struct GNUNET_MESH_Data *msg,
291 struct MeshChannelReliability *rel)
293 struct MeshReliableMessage *copy;
294 struct MeshReliableMessage *prev;
298 size = ntohs (msg->header.size);
299 mid = ntohl (msg->mid);
301 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
303 copy = GNUNET_malloc (sizeof (*copy) + size);
306 memcpy (©[1], msg, size);
310 // FIXME do something better than O(n), although n < 64...
311 // FIXME start from the end (most messages are the latest ones)
312 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
314 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
315 if (GMC_is_pid_bigger (prev->mid, mid))
317 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
318 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
323 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
324 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
325 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
329 * Add a destination client to a channel, initializing all data structures
330 * in the channel and the client.
332 * @param ch Channel to which add the destination.
333 * @param c Client which to add to the channel.
336 add_destination (struct MeshChannel *ch, struct MeshClient *c)
338 if (NULL != ch->dest)
344 /* Assign local id as destination */
345 ch->lid_dest = GML_get_next_chid (c);
347 /* Store in client's hashmap */
348 GML_channel_add (c, ch->lid_dest, ch);
350 GNUNET_break (NULL == ch->dest_rel);
351 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
352 ch->dest_rel->ch = ch;
353 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
360 * Send data to a client.
362 * If the client is ready, send directly, otherwise buffer while listening
366 * @param msg Message.
367 * @param fwd Is this a fwd (root->dest) message?
370 send_client_data (struct MeshChannel *ch,
371 const struct GNUNET_MESH_Data *msg,
376 if (ch->dest_rel->client_ready)
377 GML_send_data (ch->dest, msg, ch->lid_dest);
379 add_buffered_data (msg, ch->dest_rel);
383 if (ch->root_rel->client_ready)
384 GML_send_data (ch->root, msg, ch->lid_root);
386 add_buffered_data (msg, ch->root_rel);
392 * Send a buffered message to the client, for in order delivery or
393 * as result of client ACK.
395 * @param ch Channel on which to empty the message buffer.
396 * @param c Client to send to.
397 * @param fwd Is this to send FWD data?.
400 send_client_buffered_data (struct MeshChannel *ch,
401 struct MeshClient *c,
404 struct MeshReliableMessage *copy;
405 struct MeshChannelReliability *rel;
407 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
408 rel = fwd ? ch->dest_rel : ch->root_rel;
409 if (GNUNET_NO == rel->client_ready)
411 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
415 copy = rel->head_recv;
416 /* We never buffer channel management messages */
419 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
421 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
423 LOG (GNUNET_ERROR_TYPE_DEBUG,
424 " have %u! now expecting %u\n",
425 copy->mid, rel->mid_recv + 1);
426 send_client_data (ch, msg, fwd);
429 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
434 LOG (GNUNET_ERROR_TYPE_DEBUG,
435 " reliable && don't have %u, next is %u\n",
441 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
446 * Allow a client to send more data.
448 * In case the client was already allowed to send data, do nothing.
451 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
454 send_client_ack (struct MeshChannel *ch, int fwd)
456 struct MeshChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
463 if (GNUNET_YES == rel->client_ready)
466 GML_send_ack (fwd ? ch->root : ch->dest, fwd ? ch->lid_root : ch->lid_dest);
467 rel->client_ready = GNUNET_YES;
472 * Destroy all reliable messages queued for a channel,
473 * during a channel destruction.
474 * Frees the reliability structure itself.
476 * @param rel Reliability data for a channel.
479 channel_rel_free_all (struct MeshChannelReliability *rel)
481 struct MeshReliableMessage *copy;
482 struct MeshReliableMessage *next;
487 for (copy = rel->head_recv; NULL != copy; copy = next)
490 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
493 for (copy = rel->head_sent; NULL != copy; copy = next)
496 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
499 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
500 GNUNET_SCHEDULER_cancel (rel->retry_task);
506 * Mark future messages as ACK'd.
508 * @param rel Reliability data.
509 * @param msg DataACK message with a bitfield of future ACK'd messages.
512 channel_rel_free_sent (struct MeshChannelReliability *rel,
513 const struct GNUNET_MESH_DataACK *msg)
515 struct MeshReliableMessage *copy;
516 struct MeshReliableMessage *next;
523 bitfield = msg->futures;
524 mid = ntohl (msg->mid);
525 LOG (GNUNET_ERROR_TYPE_DEBUG,
526 "free_sent_reliable %u %llX\n",
528 LOG (GNUNET_ERROR_TYPE_DEBUG,
529 " rel %p, head %p\n",
530 rel, rel->head_sent);
531 for (i = 0, copy = rel->head_sent;
532 i < 64 && NULL != copy && 0 != bitfield;
535 LOG (GNUNET_ERROR_TYPE_DEBUG,
536 " trying bit %u (mid %u)\n",
539 if (0 == (bitfield & mask))
542 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
543 /* Bit was set, clear the bit from the bitfield */
546 /* The i-th bit was set. Do we have that copy? */
547 /* Skip copies with mid < target */
548 target = mid + i + 1;
549 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
550 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
553 /* Did we run out of copies? (previously freed, it's ok) */
556 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
560 /* Did we overshoot the target? (previously freed, it's ok) */
561 if (GMC_is_pid_bigger (copy->mid, target))
563 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
567 /* Now copy->mid == target, free it */
569 rel_message_free (copy);
572 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
577 * We haven't received an ACK after a certain time: restransmit the message.
579 * @param cls Closure (MeshReliableMessage with the message to restransmit)
580 * @param tc TaskContext.
583 channel_retransmit_message (void *cls,
584 const struct GNUNET_SCHEDULER_TaskContext *tc)
586 struct MeshChannelReliability *rel = cls;
587 struct MeshReliableMessage *copy;
588 struct MeshChannel *ch;
589 struct GNUNET_MESH_Data *payload;
592 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
593 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
597 copy = rel->head_sent;
604 /* Search the message to be retransmitted in the outgoing queue.
605 * Check only the queue for the connection that is going to be used,
606 * if the message is stuck in some other connection's queue we shouldn't
608 * - cancelling it and sending the new one doesn't guarantee it's delivery,
609 * the old connection could be temporary stalled or the queue happened to
610 * be long at time of insertion.
611 * - not sending the new one could cause terrible delays the old connection
614 // FIXME access to queue elements is limited
615 payload = (struct GNUNET_MESH_Data *) ©[1];
616 fwd = (rel == ch->root_rel);
617 // c = GMT_get_connection (ch->t, fwd);
618 // hop = connection_get_hop (c, fwd);
619 // for (q = hop->queue_head; NULL != q; q = q->next)
621 // if (ntohs (payload->header.type) == q->type && ch == q->ch)
623 // struct GNUNET_MESH_Data *queued_data = q->cls;
625 // if (queued_data->mid == payload->mid)
630 /* Message not found in the queue that we are going to use. */
633 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
635 GMCH_send_prebuilt_message (&payload->header, ch, fwd);
636 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
640 // LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
643 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
644 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
645 &channel_retransmit_message,
651 * Destroy a reliable message after it has been acknowledged, either by
652 * direct mid ACK or bitfield. Updates the appropriate data structures and
653 * timers and frees all memory.
655 * @param copy Message that is no longer needed: remote peer got it.
658 rel_message_free (struct MeshReliableMessage *copy)
660 struct MeshChannelReliability *rel;
661 struct GNUNET_TIME_Relative time;
664 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
665 rel->expected_delay.rel_value_us *= 7;
666 rel->expected_delay.rel_value_us += time.rel_value_us;
667 rel->expected_delay.rel_value_us /= 8;
669 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
670 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
671 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
672 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
673 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
674 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
676 rel->retry_timer = rel->expected_delay;
677 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
683 * Confirm we got a channel create.
685 * @param ch The channel to confirm.
686 * @param fwd Should we send a FWD ACK? (going dest->root)
689 channel_send_ack (struct MeshChannel *ch, int fwd)
691 struct GNUNET_MESH_ChannelManage msg;
693 msg.header.size = htons (sizeof (msg));
694 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
695 LOG (GNUNET_ERROR_TYPE_DEBUG,
696 " sending channel %s ack for channel %s\n",
697 fwd ? "FWD" : "BCK", GMCH_2s (ch));
699 msg.chid = htonl (ch->gid);
700 GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
705 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
707 * @param ch Channel to mark as ready.
708 * @param fwd Was the ACK message sent fwd? (dest->root, SYNACK)
711 channel_confirm (struct MeshChannel *ch, int fwd)
713 struct MeshChannelReliability *rel;
714 struct MeshReliableMessage *copy;
715 struct MeshReliableMessage *next;
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 " channel confirm %s %s:%X\n",
719 fwd ? "FWD" : "BCK", GMT_2s (ch->t), ch->gid);
720 ch->state = MESH_CHANNEL_READY;
722 rel = fwd ? ch->root_rel : ch->dest_rel;
723 for (copy = rel->head_sent; NULL != copy; copy = next)
725 struct GNUNET_MessageHeader *msg;
728 msg = (struct GNUNET_MessageHeader *) ©[1];
729 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
731 rel_message_free (copy);
735 send_client_ack (ch, fwd);
737 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
739 channel_send_ack (ch, !fwd);
744 * Save a copy to retransmit in case it gets lost.
746 * Initializes all needed callbacks and timers.
748 * @param ch Channel this message goes on.
749 * @param msg Message to copy.
750 * @param fwd Is this fwd traffic?
753 channel_save_copy (struct MeshChannel *ch,
754 const struct GNUNET_MessageHeader *msg,
757 struct MeshChannelReliability *rel;
758 struct MeshReliableMessage *copy;
763 rel = fwd ? ch->root_rel : ch->dest_rel;
765 type = ntohs (msg->type);
766 size = ntohs (msg->size);
768 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
769 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
771 copy->timestamp = GNUNET_TIME_absolute_get ();
774 memcpy (©[1], msg, size);
776 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
777 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
778 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
781 GNUNET_TIME_relative_multiply (rel->expected_delay,
782 MESH_RETRANSMIT_MARGIN);
784 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
785 &channel_retransmit_message,
792 * Destroy a channel and free all resources.
794 * @param ch Channel to destroy.
797 channel_destroy (struct MeshChannel *ch)
799 struct MeshClient *c;
804 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
805 GMT_2s (ch->t), ch->gid);
811 GML_channel_remove (c, ch->lid_root, ch);
817 GML_channel_remove (c, ch->lid_dest, ch);
820 channel_rel_free_all (ch->root_rel);
821 channel_rel_free_all (ch->dest_rel);
823 GMT_remove_channel (ch->t, ch);
824 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
831 * Create a new channel.
833 * @param t Tunnel this channel is in.
834 * @param owner Client that owns the channel, NULL for foreign channels.
835 * @param lid_root Local ID for root client.
837 * @return A new initialized channel. NULL on error.
839 static struct MeshChannel *
840 channel_new (struct MeshTunnel3 *t,
841 struct MeshClient *owner,
842 MESH_ChannelNumber lid_root)
844 struct MeshChannel *ch;
846 ch = GNUNET_new (struct MeshChannel);
848 ch->lid_root = lid_root;
851 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
855 ch->gid = GMT_get_next_chid (t);
856 GML_channel_add (owner, lid_root, ch);
858 GMT_add_channel (t, ch);
865 * Set options in a channel, extracted from a bit flag field
867 * @param ch Channel to set options to.
868 * @param options Bit array in host byte order.
871 channel_set_options (struct MeshChannel *ch, uint32_t options)
873 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
874 GNUNET_YES : GNUNET_NO;
875 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
876 GNUNET_YES : GNUNET_NO;
881 * Handle a loopback message: call the appropriate handler for the message type.
883 * @param ch Channel this message is on.
884 * @param msgh Message header.
885 * @param fwd Is this FWD traffic?
888 handle_loopback (struct MeshChannel *ch,
889 const struct GNUNET_MessageHeader *msgh,
894 type = ntohs (msgh->type);
895 LOG (GNUNET_ERROR_TYPE_DEBUG,
896 "Loopback %s %s message!\n",
897 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type));
901 case GNUNET_MESSAGE_TYPE_MESH_DATA:
902 /* Don't send hop ACK, wait for client to ACK */
903 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
906 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
907 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
910 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
911 GMCH_handle_create (ch->t,
912 (struct GNUNET_MESH_ChannelCreate *) msgh,
916 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
918 (struct GNUNET_MESH_ChannelManage *) msgh,
922 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
923 GMCH_handle_destroy (ch,
924 (struct GNUNET_MESH_ChannelManage *) msgh,
930 LOG (GNUNET_ERROR_TYPE_DEBUG,
931 "end-to-end message not known (%u)\n",
938 /******************************************************************************/
939 /******************************** API ***********************************/
940 /******************************************************************************/
951 GMCH_get_id (const struct MeshChannel *ch)
958 * Get the channel tunnel.
960 * @param ch Channel to get the tunnel from.
962 * @return tunnel of the channel.
965 GMCH_get_tunnel (const struct MeshChannel *ch)
972 * Get free buffer space towards the client on a specific channel.
975 * @param fwd Is query about FWD traffic?
977 * @return Free buffer space [0 - 64]
980 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
982 struct MeshChannelReliability *rel;
984 rel = fwd ? ch->dest_rel : ch->root_rel;
986 /* If rel is NULL it means that the end is not yet created,
987 * most probably is a loopback channel at the point of sending
988 * the ChannelCreate to itself.
993 return (64 - rel->n_recv);
998 * Is the root client for this channel on this peer?
1000 * @param ch Channel.
1001 * @param fwd Is this for fwd traffic?
1003 * @return GNUNET_YES in case it is.
1006 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1008 struct MeshClient *c;
1010 c = fwd ? ch->root : ch->dest;
1016 * Is the destination client for this channel on this peer?
1018 * @param ch Channel.
1019 * @param fwd Is this for fwd traffic?
1021 * @return GNUNET_YES in case it is.
1024 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1026 struct MeshClient *c;
1028 c = fwd ? ch->dest : ch->root;
1034 * Notify the destination client that a new incoming channel was created.
1036 * @param ch Channel that was created.
1039 GMCH_send_create (struct MeshChannel *ch)
1043 if (NULL == ch->dest)
1047 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1048 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1049 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1050 GMT_get_destination (ch->t));
1055 * Notify a client that the channel is no longer valid.
1056 * FIXME send on tunnel if some client == NULL?
1058 * @param ch Channel that is destroyed.
1061 GMCH_send_destroy (struct MeshChannel *ch)
1063 if (NULL != ch->root)
1064 GML_send_channel_destroy (ch->root, ch->lid_root);
1066 if (NULL != ch->dest)
1067 GML_send_channel_destroy (ch->dest, ch->lid_dest);
1072 * Send data on a channel.
1074 * If the destination is local, send it to client, otherwise encrypt and
1078 * @param msg Message.
1079 * @param fwd Is this a fwd (root->dest) message?
1082 GMCH_send_data (struct MeshChannel *ch,
1083 const struct GNUNET_MESH_Data *msg,
1086 if (GMCH_is_terminal (ch, fwd))
1088 GML_send_data (fwd ? ch->dest : ch->root,
1090 fwd ? ch->lid_dest : ch->lid_root);
1094 GMT_send_prebuilt_message (&msg->header, ch->t, ch, fwd);
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_data_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 * Handle an ACK given by a client.
1191 * Mark client as ready and send him any buffered data we could have for him.
1193 * @param ch Channel.
1194 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by root and go BCK)
1197 GMCH_handle_local_ack (struct MeshChannel *ch, int fwd)
1199 struct MeshChannelReliability *rel;
1200 struct MeshClient *c;
1202 rel = fwd ? ch->dest_rel : ch->root_rel;
1203 c = fwd ? ch->dest : ch->root;
1205 rel->client_ready = GNUNET_YES;
1206 send_client_buffered_data (ch, c, fwd);
1207 GMC_send_ack (NULL, ch, fwd);
1212 * Handle data given by a client.
1214 * Check whether the client is allowed to send in this tunnel, save if channel
1215 * is reliable and send an ACK to the client if there is still buffer space
1218 * @param ch Channel.
1219 * @param fwd Is this a FWD data?
1221 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1224 GMCH_handle_local_data (struct MeshChannel *ch,
1225 struct MeshClient *c,
1226 struct GNUNET_MessageHeader *message,
1229 struct MeshChannelReliability *rel;
1230 struct GNUNET_MESH_Data *payload;
1231 size_t size = ntohs (message->size);
1232 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
1233 unsigned char cbuf[p2p_size];
1235 /* Is the client in the channel? */
1243 return GNUNET_SYSERR;
1246 rel = fwd ? ch->root_rel : ch->dest_rel;
1248 /* Ok, everything is correct, send the message. */
1249 payload = (struct GNUNET_MESH_Data *) cbuf;
1250 payload->mid = htonl (rel->mid_send);
1252 memcpy (&payload[1], message, size);
1253 payload->header.size = htons (p2p_size);
1254 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
1255 payload->chid = htonl (ch->gid);
1256 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1257 GMCH_send_prebuilt_message (&payload->header, ch, fwd);
1259 if (GNUNET_YES == ch->reliable)
1260 channel_save_copy (ch, &payload->header, fwd);
1261 if (GMT_get_buffer (ch->t, fwd) > 0)
1263 MESH_ChannelNumber ackid;
1264 ackid = fwd ? ch->lid_root : ch->lid_dest;
1265 LOG (GNUNET_ERROR_TYPE_DEBUG,
1266 " sending ack to client (%X)\n", ackid);
1267 GML_send_ack (c, ackid);
1275 * Handle a channel destroy requested by a client.
1277 * Destroy the channel and the tunnel in case this was the last channel.
1279 * @param ch Channel.
1280 * @param c Client that requested the destruction (to avoid notifying him).
1283 GMCH_handle_local_destroy (struct MeshChannel *ch,
1284 struct MeshClient *c)
1286 struct MeshTunnel3 *t;
1288 /* Cleanup after the tunnel */
1291 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1292 GML_client_delete_channel (c, ch, ch->lid_dest);
1297 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1298 GML_client_delete_channel (c, ch, ch->lid_root);
1303 GMCH_send_destroy (ch);
1304 channel_destroy (ch);
1305 GMT_destroy_if_empty (t);
1310 * Handle a channel create requested by a client.
1312 * Create the channel and the tunnel in case this was the first0 channel.
1314 * @param c Client that requested the creation (will be the root).
1315 * @param msg Create Channel message.
1317 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1320 GMCH_handle_local_create (struct MeshClient *c,
1321 struct GNUNET_MESH_ChannelMessage *msg)
1323 struct MeshChannel *ch;
1324 struct MeshTunnel3 *t;
1325 struct MeshPeer *peer;
1326 MESH_ChannelNumber chid;
1328 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1329 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1330 chid = ntohl (msg->channel_id);
1332 /* Sanity check for duplicate channel IDs */
1333 if (NULL != GML_channel_get (c, chid))
1336 return GNUNET_SYSERR;
1339 peer = GMP_get (&msg->peer);
1340 GMP_add_tunnel (peer);
1341 t = GMP_get_tunnel (peer);
1343 if (GMP_get_short_id (peer) == myid)
1345 GMT_change_state (t, MESH_TUNNEL3_READY);
1352 /* Create channel */
1353 ch = channel_new (t, c, chid);
1357 return GNUNET_SYSERR;
1359 ch->port = ntohl (msg->port);
1360 channel_set_options (ch, ntohl (msg->opt));
1362 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1363 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
1364 ch->root_rel->ch = ch;
1365 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
1367 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
1369 /* Send create channel */
1371 struct GNUNET_MESH_ChannelCreate msgcc;
1373 msgcc.header.size = htons (sizeof (msgcc));
1374 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1375 msgcc.chid = htonl (ch->gid);
1376 msgcc.port = msg->port;
1377 msgcc.opt = msg->opt;
1379 GMT_queue_data (t, ch, &msgcc.header, GNUNET_YES);
1385 * Handler for mesh network payload traffic.
1387 * @param ch Channel for the message.
1388 * @param message Unencryted data message.
1389 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1392 GMCH_handle_data (struct MeshChannel *ch,
1393 const struct GNUNET_MESH_Data *msg,
1396 struct MeshChannelReliability *rel;
1397 struct MeshClient *c;
1400 /* Initialize FWD/BCK data */
1401 c = fwd ? ch->dest : ch->root;
1402 rel = fwd ? ch->dest_rel : ch->root_rel;
1410 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1412 mid = ntohl (msg->mid);
1413 LOG (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
1415 if (GNUNET_NO == ch->reliable ||
1416 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
1417 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1419 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1420 if (GNUNET_YES == ch->reliable)
1422 /* Is this the exact next expected messasge? */
1423 if (mid == rel->mid_recv)
1425 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1427 send_client_data (ch, msg, fwd);
1431 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1432 add_buffered_data (msg, rel);
1437 /* Tunnel is unreliable: send to clients directly */
1438 /* FIXME: accept Out Of Order traffic */
1439 rel->mid_recv = mid + 1;
1440 send_client_data (ch, msg, fwd);
1445 GNUNET_break_op (0);
1446 LOG (GNUNET_ERROR_TYPE_DEBUG,
1447 " MID %u not expected (%u - %u), dropping!\n",
1448 mid, rel->mid_recv, rel->mid_recv + 64);
1451 GMCH_send_data_ack (ch, fwd);
1456 * Handler for mesh network traffic end-to-end ACKs.
1458 * @param t Tunnel on which we got this message.
1459 * @param message Data message.
1460 * @param fwd Is this a fwd ACK? (dest->orig)
1463 GMCH_handle_data_ack (struct MeshChannel *ch,
1464 const struct GNUNET_MESH_DataACK *msg,
1467 struct MeshChannelReliability *rel;
1468 struct MeshReliableMessage *copy;
1469 struct MeshReliableMessage *next;
1473 ack = ntohl (msg->mid);
1474 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1475 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1477 if (GNUNET_YES == fwd)
1491 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1493 if (GMC_is_pid_bigger (copy->mid, ack))
1495 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1496 channel_rel_free_sent (rel, msg);
1500 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
1502 rel_message_free (copy);
1504 /* ACK client if needed */
1505 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
1507 /* If some message was free'd, update the retransmission delay*/
1508 if (GNUNET_YES == work)
1510 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1512 GNUNET_SCHEDULER_cancel (rel->retry_task);
1513 if (NULL == rel->head_sent)
1515 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1519 struct GNUNET_TIME_Absolute new_target;
1520 struct GNUNET_TIME_Relative delay;
1522 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1523 MESH_RETRANSMIT_MARGIN);
1524 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1526 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1528 GNUNET_SCHEDULER_add_delayed (delay,
1529 &channel_retransmit_message,
1540 * Handler for channel create messages.
1542 * @param t Tunnel this channel will be in.
1543 * @param msg Message.
1544 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1546 struct MeshChannel *
1547 GMCH_handle_create (struct MeshTunnel3 *t,
1548 const struct GNUNET_MESH_ChannelCreate *msg,
1551 MESH_ChannelNumber chid;
1552 struct MeshChannel *ch;
1553 struct MeshClient *c;
1556 chid = ntohl (msg->chid);
1558 ch = GMT_get_channel (t, chid);
1561 /* Create channel */
1562 ch = channel_new (t, NULL, 0);
1565 channel_set_options (ch, ntohl (msg->opt));
1567 /* Find a destination client */
1568 port = ntohl (msg->port);
1569 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
1570 c = GML_client_get_by_port (port);
1573 /* TODO send reject */
1574 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
1575 channel_destroy (ch);
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
1583 add_destination (ch, c);
1584 if (GNUNET_YES == ch->reliable)
1585 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
1589 GMCH_send_create (ch);
1590 channel_send_ack (ch, fwd);
1597 * Handler for channel ack messages.
1599 * @param ch Channel.
1600 * @param msg Message.
1601 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1604 GMCH_handle_ack (struct MeshChannel *ch,
1605 const struct GNUNET_MESH_ChannelManage *msg,
1608 channel_confirm (ch, !fwd);
1613 * Handler for channel destroy messages.
1615 * @param ch Channel to be destroyed of.
1616 * @param msg Message.
1617 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1620 GMCH_handle_destroy (struct MeshChannel *ch,
1621 const struct GNUNET_MESH_ChannelManage *msg,
1624 struct MeshTunnel3 *t;
1627 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
1629 /* Not for us (don't destroy twice a half-open loopback channel) */
1634 GMCH_send_destroy (ch);
1635 channel_destroy (ch);
1636 GMT_destroy_if_empty (t);
1641 * Sends an already built message on a channel.
1643 * If the channel is on a loopback tunnel, notifies the appropriate destination
1646 * On a normal channel passes the message to the tunnel for encryption and
1647 * sending on a connection.
1649 * This function DOES NOT save the message for retransmission.
1651 * @param message Message to send. Function makes a copy of it.
1652 * @param ch Channel on which this message is transmitted.
1653 * @param fwd Is this a fwd message?
1656 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1657 struct MeshChannel *ch, int fwd)
1659 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH Send %s %s on channel %s\n",
1660 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (ntohs (message->type)),
1663 if (GMT_is_loopback (ch->t))
1665 handle_loopback (ch, message, fwd);
1669 GMT_send_prebuilt_message (message, ch->t, ch, fwd);
1674 * Get the static string for identification of the channel.
1676 * @param ch Channel.
1678 * @return Static string with the channel IDs.
1681 GMCH_2s (const struct MeshChannel *ch)
1683 static char buf[64];
1686 return "(NULL Channel)";
1688 sprintf (buf, "%s:%X (%X / %X)",
1689 GMT_2s (ch->t), ch->gid, ch->lid_root, ch->lid_dest);