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 * Can the client send data to us?
152 * Task to resend/poll in case no ACK is received.
154 GNUNET_SCHEDULER_TaskIdentifier retry_task;
157 * Counter for exponential backoff.
159 struct GNUNET_TIME_Relative retry_timer;
162 * How long does it usually take to get an ACK.
164 struct GNUNET_TIME_Relative expected_delay;
169 * Struct containing all information regarding a channel to a remote client.
174 * Tunnel this channel is in.
176 struct MeshTunnel3 *t;
179 * Destination port of the channel.
184 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
186 MESH_ChannelNumber gid;
189 * Local tunnel number for root (owner) client.
190 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
192 MESH_ChannelNumber lid_root;
195 * Local tunnel number for local destination clients (incoming number)
196 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
198 MESH_ChannelNumber lid_dest;
203 enum MeshChannelState state;
206 * Is the tunnel bufferless (minimum latency)?
211 * Is the tunnel reliable?
216 * Last time the channel was used
218 struct GNUNET_TIME_Absolute timestamp;
221 * Client owner of the tunnel, if any
223 struct MeshClient *root;
226 * Client destination of the tunnel, if any.
228 struct MeshClient *dest;
231 * Flag to signal the destruction of the channel.
232 * If this is set GNUNET_YES the channel will be destroyed
233 * when the queue is empty.
238 * Total messages pending for this channel, payload or not.
240 unsigned int pending_messages;
244 * Only present (non-NULL) at the owner of a tunnel.
246 struct MeshChannelReliability *root_rel;
250 * Only present (non-NULL) at the destination of a tunnel.
252 struct MeshChannelReliability *dest_rel;
257 /******************************************************************************/
258 /******************************* GLOBALS ***********************************/
259 /******************************************************************************/
262 * Global handle to the statistics service.
264 extern struct GNUNET_STATISTICS_Handle *stats;
267 * Local peer own ID (memory efficient handle).
269 extern GNUNET_PEER_Id myid;
272 /******************************************************************************/
273 /******************************** STATIC ***********************************/
274 /******************************************************************************/
277 * Destroy a reliable message after it has been acknowledged, either by
278 * direct mid ACK or bitfield. Updates the appropriate data structures and
279 * timers and frees all memory.
281 * @param copy Message that is no longer needed: remote peer got it.
284 rel_message_free (struct MeshReliableMessage *copy);
287 * We have received a message out of order, or the client is not ready.
288 * Buffer it until we receive an ACK from the client or the missing
289 * message from the channel.
291 * @param msg Message to buffer (MUST be of type MESH_DATA).
292 * @param rel Reliability data to the corresponding direction.
295 add_buffered_data (const struct GNUNET_MESH_Data *msg,
296 struct MeshChannelReliability *rel)
298 struct MeshReliableMessage *copy;
299 struct MeshReliableMessage *prev;
303 size = ntohs (msg->header.size);
304 mid = ntohl (msg->mid);
306 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
308 copy = GNUNET_malloc (sizeof (*copy) + size);
311 memcpy (©[1], msg, size);
315 // FIXME do something better than O(n), although n < 64...
316 // FIXME start from the end (most messages are the latest ones)
317 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
319 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
320 if (GMC_is_pid_bigger (prev->mid, mid))
322 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
323 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
328 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
329 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
330 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
334 * Add a destination client to a channel, initializing all data structures
335 * in the channel and the client.
337 * @param ch Channel to which add the destination.
338 * @param c Client which to add to the channel.
341 add_destination (struct MeshChannel *ch, struct MeshClient *c)
343 if (NULL != ch->dest)
349 /* Assign local id as destination */
350 ch->lid_dest = GML_get_next_chid (c);
352 /* Store in client's hashmap */
353 GML_channel_add (c, ch->lid_dest, ch);
355 GNUNET_break (NULL == ch->dest_rel);
356 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
357 ch->dest_rel->ch = ch;
358 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
365 * Send data to a client.
367 * If the client is ready, send directly, otherwise buffer while listening
371 * @param msg Message.
372 * @param fwd Is this a fwd (root->dest) message?
375 send_client_data (struct MeshChannel *ch,
376 const struct GNUNET_MESH_Data *msg,
381 if (ch->dest_rel->client_ready)
382 GML_send_data (ch->dest, msg, ch->lid_dest);
384 add_buffered_data (msg, ch->dest_rel);
388 if (ch->root_rel->client_ready)
389 GML_send_data (ch->root, msg, ch->lid_root);
391 add_buffered_data (msg, ch->root_rel);
397 * Send a buffered message to the client, for in order delivery or
398 * as result of client ACK.
400 * @param ch Channel on which to empty the message buffer.
401 * @param c Client to send to.
402 * @param fwd Is this to send FWD data?.
405 send_client_buffered_data (struct MeshChannel *ch,
406 struct MeshClient *c,
409 struct MeshReliableMessage *copy;
410 struct MeshChannelReliability *rel;
412 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
413 rel = fwd ? ch->dest_rel : ch->root_rel;
414 if (GNUNET_NO == rel->client_ready)
416 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
420 copy = rel->head_recv;
421 /* We never buffer channel management messages */
424 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
426 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
428 LOG (GNUNET_ERROR_TYPE_DEBUG,
429 " have %u! now expecting %u\n",
430 copy->mid, rel->mid_recv + 1);
431 send_client_data (ch, msg, fwd);
434 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
439 LOG (GNUNET_ERROR_TYPE_DEBUG,
440 " reliable && don't have %u, next is %u\n",
446 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
451 * Allow a client to send more data.
453 * In case the client was already allowed to send data, do nothing.
456 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
459 send_client_ack (struct MeshChannel *ch, int fwd)
461 struct MeshChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
463 LOG (GNUNET_ERROR_TYPE_DEBUG,
464 " sending %s ack to client on channel %s\n",
465 fwd ? "FWD" : "BCK", GMCH_2s (ch));
473 if (GNUNET_YES == rel->client_allowed)
475 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
478 rel->client_allowed = GNUNET_YES;
480 GML_send_ack (fwd ? ch->root : ch->dest, fwd ? ch->lid_root : ch->lid_dest);
485 * Destroy all reliable messages queued for a channel,
486 * during a channel destruction.
487 * Frees the reliability structure itself.
489 * @param rel Reliability data for a channel.
492 channel_rel_free_all (struct MeshChannelReliability *rel)
494 struct MeshReliableMessage *copy;
495 struct MeshReliableMessage *next;
500 for (copy = rel->head_recv; NULL != copy; copy = next)
503 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
506 for (copy = rel->head_sent; NULL != copy; copy = next)
509 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
512 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
513 GNUNET_SCHEDULER_cancel (rel->retry_task);
519 * Mark future messages as ACK'd.
521 * @param rel Reliability data.
522 * @param msg DataACK message with a bitfield of future ACK'd messages.
525 channel_rel_free_sent (struct MeshChannelReliability *rel,
526 const struct GNUNET_MESH_DataACK *msg)
528 struct MeshReliableMessage *copy;
529 struct MeshReliableMessage *next;
536 bitfield = msg->futures;
537 mid = ntohl (msg->mid);
538 LOG (GNUNET_ERROR_TYPE_DEBUG,
539 "free_sent_reliable %u %llX\n",
541 LOG (GNUNET_ERROR_TYPE_DEBUG,
542 " rel %p, head %p\n",
543 rel, rel->head_sent);
544 for (i = 0, copy = rel->head_sent;
545 i < 64 && NULL != copy && 0 != bitfield;
548 LOG (GNUNET_ERROR_TYPE_DEBUG,
549 " trying bit %u (mid %u)\n",
552 if (0 == (bitfield & mask))
555 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
556 /* Bit was set, clear the bit from the bitfield */
559 /* The i-th bit was set. Do we have that copy? */
560 /* Skip copies with mid < target */
561 target = mid + i + 1;
562 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
563 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
566 /* Did we run out of copies? (previously freed, it's ok) */
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
573 /* Did we overshoot the target? (previously freed, it's ok) */
574 if (GMC_is_pid_bigger (copy->mid, target))
576 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
580 /* Now copy->mid == target, free it */
582 rel_message_free (copy);
585 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
590 * We haven't received an ACK after a certain time: restransmit the message.
592 * @param cls Closure (MeshReliableMessage with the message to restransmit)
593 * @param tc TaskContext.
596 channel_retransmit_message (void *cls,
597 const struct GNUNET_SCHEDULER_TaskContext *tc)
599 struct MeshChannelReliability *rel = cls;
600 struct MeshReliableMessage *copy;
601 struct MeshChannel *ch;
602 struct GNUNET_MESH_Data *payload;
605 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
606 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
610 copy = rel->head_sent;
617 /* Search the message to be retransmitted in the outgoing queue.
618 * Check only the queue for the connection that is going to be used,
619 * if the message is stuck in some other connection's queue we shouldn't
621 * - cancelling it and sending the new one doesn't guarantee it's delivery,
622 * the old connection could be temporary stalled or the queue happened to
623 * be long at time of insertion.
624 * - not sending the new one could cause terrible delays the old connection
627 // FIXME access to queue elements is limited
628 payload = (struct GNUNET_MESH_Data *) ©[1];
629 fwd = (rel == ch->root_rel);
630 // c = GMT_get_connection (ch->t, fwd);
631 // hop = connection_get_hop (c, fwd);
632 // for (q = hop->queue_head; NULL != q; q = q->next)
634 // if (ntohs (payload->header.type) == q->type && ch == q->ch)
636 // struct GNUNET_MESH_Data *queued_data = q->cls;
638 // if (queued_data->mid == payload->mid)
643 /* Message not found in the queue that we are going to use. */
646 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
648 GMCH_send_prebuilt_message (&payload->header, ch, fwd);
649 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
653 // LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
656 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
657 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
658 &channel_retransmit_message,
664 * Destroy a reliable message after it has been acknowledged, either by
665 * direct mid ACK or bitfield. Updates the appropriate data structures and
666 * timers and frees all memory.
668 * @param copy Message that is no longer needed: remote peer got it.
671 rel_message_free (struct MeshReliableMessage *copy)
673 struct MeshChannelReliability *rel;
674 struct GNUNET_TIME_Relative time;
677 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
678 rel->expected_delay.rel_value_us *= 7;
679 rel->expected_delay.rel_value_us += time.rel_value_us;
680 rel->expected_delay.rel_value_us /= 8;
682 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
683 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
684 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
685 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
686 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
687 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
689 rel->retry_timer = rel->expected_delay;
690 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
696 * Confirm we got a channel create.
698 * @param ch The channel to confirm.
699 * @param fwd Should we send a FWD ACK? (going dest->root)
702 channel_send_ack (struct MeshChannel *ch, int fwd)
704 struct GNUNET_MESH_ChannelManage msg;
706 msg.header.size = htons (sizeof (msg));
707 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
708 LOG (GNUNET_ERROR_TYPE_DEBUG,
709 " sending channel %s ack for channel %s\n",
710 fwd ? "FWD" : "BCK", GMCH_2s (ch));
712 msg.chid = htonl (ch->gid);
713 GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
718 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
720 * @param ch Channel to mark as ready.
721 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
724 channel_confirm (struct MeshChannel *ch, int fwd)
726 struct MeshChannelReliability *rel;
727 struct MeshReliableMessage *copy;
728 struct MeshReliableMessage *next;
730 LOG (GNUNET_ERROR_TYPE_DEBUG,
731 " channel confirm %s %s:%X\n",
732 fwd ? "FWD" : "BCK", GMT_2s (ch->t), ch->gid);
733 ch->state = MESH_CHANNEL_READY;
735 rel = fwd ? ch->root_rel : ch->dest_rel;
736 rel->client_ready = GNUNET_YES;
737 for (copy = rel->head_sent; NULL != copy; copy = next)
739 struct GNUNET_MessageHeader *msg;
742 msg = (struct GNUNET_MessageHeader *) ©[1];
743 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
745 rel_message_free (copy);
749 send_client_ack (ch, fwd);
751 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
753 channel_send_ack (ch, !fwd);
758 * Save a copy to retransmit in case it gets lost.
760 * Initializes all needed callbacks and timers.
762 * @param ch Channel this message goes on.
763 * @param msg Message to copy.
764 * @param fwd Is this fwd traffic?
767 channel_save_copy (struct MeshChannel *ch,
768 const struct GNUNET_MessageHeader *msg,
771 struct MeshChannelReliability *rel;
772 struct MeshReliableMessage *copy;
777 rel = fwd ? ch->root_rel : ch->dest_rel;
779 type = ntohs (msg->type);
780 size = ntohs (msg->size);
782 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
783 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
785 copy->timestamp = GNUNET_TIME_absolute_get ();
788 memcpy (©[1], msg, size);
790 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
791 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
792 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
795 GNUNET_TIME_relative_multiply (rel->expected_delay,
796 MESH_RETRANSMIT_MARGIN);
798 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
799 &channel_retransmit_message,
806 * Destroy a channel and free all resources.
808 * @param ch Channel to destroy.
811 channel_destroy (struct MeshChannel *ch)
813 struct MeshClient *c;
818 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
819 GMT_2s (ch->t), ch->gid);
825 GML_channel_remove (c, ch->lid_root, ch);
831 GML_channel_remove (c, ch->lid_dest, ch);
834 channel_rel_free_all (ch->root_rel);
835 channel_rel_free_all (ch->dest_rel);
837 GMT_remove_channel (ch->t, ch);
838 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
845 * Create a new channel.
847 * @param t Tunnel this channel is in.
848 * @param owner Client that owns the channel, NULL for foreign channels.
849 * @param lid_root Local ID for root client.
851 * @return A new initialized channel. NULL on error.
853 static struct MeshChannel *
854 channel_new (struct MeshTunnel3 *t,
855 struct MeshClient *owner,
856 MESH_ChannelNumber lid_root)
858 struct MeshChannel *ch;
860 ch = GNUNET_new (struct MeshChannel);
862 ch->lid_root = lid_root;
865 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
869 ch->gid = GMT_get_next_chid (t);
870 GML_channel_add (owner, lid_root, ch);
872 GMT_add_channel (t, ch);
879 * Set options in a channel, extracted from a bit flag field
881 * @param ch Channel to set options to.
882 * @param options Bit array in host byte order.
885 channel_set_options (struct MeshChannel *ch, uint32_t options)
887 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
888 GNUNET_YES : GNUNET_NO;
889 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
890 GNUNET_YES : GNUNET_NO;
895 * Handle a loopback message: call the appropriate handler for the message type.
897 * @param ch Channel this message is on.
898 * @param msgh Message header.
899 * @param fwd Is this FWD traffic?
902 handle_loopback (struct MeshChannel *ch,
903 const struct GNUNET_MessageHeader *msgh,
908 type = ntohs (msgh->type);
909 LOG (GNUNET_ERROR_TYPE_DEBUG,
910 "Loopback %s %s message!\n",
911 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type));
915 case GNUNET_MESSAGE_TYPE_MESH_DATA:
916 /* Don't send hop ACK, wait for client to ACK */
917 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
920 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
921 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
924 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
925 GMCH_handle_create (ch->t,
926 (struct GNUNET_MESH_ChannelCreate *) msgh,
930 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
932 (struct GNUNET_MESH_ChannelManage *) msgh,
936 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
937 GMCH_handle_destroy (ch,
938 (struct GNUNET_MESH_ChannelManage *) msgh,
944 LOG (GNUNET_ERROR_TYPE_DEBUG,
945 "end-to-end message not known (%u)\n",
952 /******************************************************************************/
953 /******************************** API ***********************************/
954 /******************************************************************************/
965 GMCH_get_id (const struct MeshChannel *ch)
972 * Get the channel tunnel.
974 * @param ch Channel to get the tunnel from.
976 * @return tunnel of the channel.
979 GMCH_get_tunnel (const struct MeshChannel *ch)
986 * Get free buffer space towards the client on a specific channel.
989 * @param fwd Is query about FWD traffic?
991 * @return Free buffer space [0 - 64]
994 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
996 struct MeshChannelReliability *rel;
998 rel = fwd ? ch->dest_rel : ch->root_rel;
1000 /* If rel is NULL it means that the end is not yet created,
1001 * most probably is a loopback channel at the point of sending
1002 * the ChannelCreate to itself.
1007 return (64 - rel->n_recv);
1012 * Get flow control status of end point: is client allow to send?
1014 * @param ch Channel.
1015 * @param fwd Is query about FWD traffic? (Request root status).
1017 * @return GNUNET_YES if client is allowed to send us data.
1020 GMCH_get_allowed (struct MeshChannel *ch, int fwd)
1022 struct MeshChannelReliability *rel;
1024 rel = fwd ? ch->root_rel : ch->dest_rel;
1026 return rel->client_allowed;
1031 * Is the root client for this channel on this peer?
1033 * @param ch Channel.
1034 * @param fwd Is this for fwd traffic?
1036 * @return GNUNET_YES in case it is.
1039 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1041 struct MeshClient *c;
1043 c = fwd ? ch->root : ch->dest;
1049 * Is the destination client for this channel on this peer?
1051 * @param ch Channel.
1052 * @param fwd Is this for fwd traffic?
1054 * @return GNUNET_YES in case it is.
1057 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1059 struct MeshClient *c;
1061 c = fwd ? ch->dest : ch->root;
1067 * Notify the destination client that a new incoming channel was created.
1069 * @param ch Channel that was created.
1072 GMCH_send_create (struct MeshChannel *ch)
1076 if (NULL == ch->dest)
1080 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1081 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1082 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1083 GMT_get_destination (ch->t));
1088 * Notify a client that the channel is no longer valid.
1090 * @param ch Channel that is destroyed.
1093 GMCH_send_destroy (struct MeshChannel *ch)
1095 struct GNUNET_MESH_ChannelManage msg;
1097 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1098 msg.header.size = htons (sizeof (msg));
1099 msg.chid = htonl (ch->gid);
1101 /* If root is not NULL, notify.
1102 * If it's NULL, check lid_root. When a local destroy comes in, root
1103 * is set to NULL but lid_root is left untouched. In this case, do nothing,
1104 * the client is the one who reuqested the channel to be destroyed.
1106 if (NULL != ch->root)
1107 GML_send_channel_destroy (ch->root, ch->lid_root);
1108 else if (0 == ch->lid_root)
1109 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO);
1111 if (NULL != ch->dest)
1112 GML_send_channel_destroy (ch->dest, ch->lid_dest);
1113 else if (0 == ch->lid_dest)
1114 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES);
1119 * Send data on a channel.
1121 * If the destination is local, send it to client, otherwise encrypt and
1125 * @param msg Message.
1126 * @param fwd Is this a fwd (root->dest) message?
1129 GMCH_send_data (struct MeshChannel *ch,
1130 const struct GNUNET_MESH_Data *msg,
1133 if (GMCH_is_terminal (ch, fwd))
1135 GML_send_data (fwd ? ch->dest : ch->root,
1137 fwd ? ch->lid_dest : ch->lid_root);
1141 GMT_send_prebuilt_message (&msg->header, ch->t, ch, fwd);
1147 * Send an end-to-end ACK message for the most recent in-sequence payload.
1149 * If channel is not reliable, do nothing.
1151 * @param ch Channel this is about.
1152 * @param fwd Is for FWD traffic? (ACK dest->owner)
1155 GMCH_send_data_ack (struct MeshChannel *ch, int fwd)
1157 struct GNUNET_MESH_DataACK msg;
1158 struct MeshChannelReliability *rel;
1159 struct MeshReliableMessage *copy;
1164 if (GNUNET_NO == ch->reliable)
1168 rel = fwd ? ch->dest_rel : ch->root_rel;
1169 LOG (GNUNET_ERROR_TYPE_DEBUG,
1170 "send_data_ack for %u\n",
1173 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
1174 msg.header.type = htons (type);
1175 msg.header.size = htons (sizeof (msg));
1176 msg.chid = htonl (ch->gid);
1177 msg.mid = htonl (rel->mid_recv - 1);
1179 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1181 if (copy->type != type)
1183 delta = copy->mid - rel->mid_recv;
1186 mask = 0x1LL << delta;
1187 msg.futures |= mask;
1188 LOG (GNUNET_ERROR_TYPE_DEBUG,
1189 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1190 copy->mid, delta, mask, msg.futures);
1192 LOG (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
1194 GMCH_send_prebuilt_message (&msg.header, ch, fwd);
1195 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1200 * Allow a client to send us more data, in case it was choked.
1202 * @param ch Channel.
1203 * @param fwd Is this about FWD traffic? (Root client).
1206 GMCH_allow_client (struct MeshChannel *ch, int fwd)
1208 if (MESH_CHANNEL_READY != ch->state)
1211 send_client_ack (ch, fwd);
1218 * @param ch Channel.
1221 GMCH_debug (struct MeshChannel *ch)
1225 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1228 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1229 GMT_2s (ch->t), ch->gid, ch);
1230 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1231 ch->root, ch->root_rel);
1232 if (NULL != ch->root)
1234 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1235 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1236 ch->root_rel->client_ready ? "YES" : "NO");
1237 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1239 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1240 ch->dest, ch->dest_rel);
1241 if (NULL != ch->dest)
1243 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1244 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1245 ch->dest_rel->client_ready ? "YES" : "NO");
1246 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1252 * Handle an ACK given by a client.
1254 * Mark client as ready and send him any buffered data we could have for him.
1256 * @param ch Channel.
1257 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by root and go BCK)
1260 GMCH_handle_local_ack (struct MeshChannel *ch, int fwd)
1262 struct MeshChannelReliability *rel;
1263 struct MeshClient *c;
1265 rel = fwd ? ch->dest_rel : ch->root_rel;
1266 c = fwd ? ch->dest : ch->root;
1268 rel->client_ready = GNUNET_YES;
1269 send_client_buffered_data (ch, c, fwd);
1270 GMT_send_acks (ch->t, fwd);
1275 * Handle data given by a client.
1277 * Check whether the client is allowed to send in this tunnel, save if channel
1278 * is reliable and send an ACK to the client if there is still buffer space
1281 * @param ch Channel.
1282 * @param c Client which sent the data.
1283 * @param message Message.
1284 * @param fwd Is this a FWD data?
1286 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1289 GMCH_handle_local_data (struct MeshChannel *ch,
1290 struct MeshClient *c,
1291 struct GNUNET_MessageHeader *message,
1294 struct MeshChannelReliability *rel;
1295 struct GNUNET_MESH_Data *payload;
1296 size_t size = ntohs (message->size);
1297 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
1298 unsigned char cbuf[p2p_size];
1300 /* Is the client in the channel? */
1308 return GNUNET_SYSERR;
1311 rel = fwd ? ch->root_rel : ch->dest_rel;
1313 rel->client_allowed = GNUNET_NO;
1315 /* Ok, everything is correct, send the message. */
1316 payload = (struct GNUNET_MESH_Data *) cbuf;
1317 payload->mid = htonl (rel->mid_send);
1319 memcpy (&payload[1], message, size);
1320 payload->header.size = htons (p2p_size);
1321 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
1322 payload->chid = htonl (ch->gid);
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1324 if (GNUNET_YES == ch->reliable)
1325 channel_save_copy (ch, &payload->header, fwd);
1326 GMCH_send_prebuilt_message (&payload->header, ch, fwd);
1328 if (GMT_get_buffer (ch->t, fwd) > 0)
1330 send_client_ack (ch, fwd);
1338 * Handle a channel destroy requested by a client.
1340 * Destroy the channel and the tunnel in case this was the last channel.
1342 * @param ch Channel.
1343 * @param c Client that requested the destruction (to avoid notifying him).
1346 GMCH_handle_local_destroy (struct MeshChannel *ch,
1347 struct MeshClient *c)
1349 struct MeshTunnel3 *t;
1351 /* Cleanup after the tunnel */
1354 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1355 GML_client_delete_channel (c, ch, ch->lid_dest);
1360 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1361 GML_client_delete_channel (c, ch, ch->lid_root);
1366 GMCH_send_destroy (ch);
1367 channel_destroy (ch);
1368 GMT_destroy_if_empty (t);
1373 * Handle a channel create requested by a client.
1375 * Create the channel and the tunnel in case this was the first0 channel.
1377 * @param c Client that requested the creation (will be the root).
1378 * @param msg Create Channel message.
1380 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1383 GMCH_handle_local_create (struct MeshClient *c,
1384 struct GNUNET_MESH_ChannelMessage *msg)
1386 struct MeshChannel *ch;
1387 struct MeshTunnel3 *t;
1388 struct MeshPeer *peer;
1389 MESH_ChannelNumber chid;
1391 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1392 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1393 chid = ntohl (msg->channel_id);
1395 /* Sanity check for duplicate channel IDs */
1396 if (NULL != GML_channel_get (c, chid))
1399 return GNUNET_SYSERR;
1402 peer = GMP_get (&msg->peer);
1403 GMP_add_tunnel (peer);
1404 t = GMP_get_tunnel (peer);
1406 if (GMP_get_short_id (peer) == myid)
1408 GMT_change_state (t, MESH_TUNNEL3_READY);
1415 /* Create channel */
1416 ch = channel_new (t, c, chid);
1420 return GNUNET_SYSERR;
1422 ch->port = ntohl (msg->port);
1423 channel_set_options (ch, ntohl (msg->opt));
1425 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1426 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
1427 ch->root_rel->ch = ch;
1428 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
1430 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
1432 /* Send create channel */
1434 struct GNUNET_MESH_ChannelCreate msgcc;
1436 msgcc.header.size = htons (sizeof (msgcc));
1437 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1438 msgcc.chid = htonl (ch->gid);
1439 msgcc.port = msg->port;
1440 msgcc.opt = msg->opt;
1442 GMT_queue_data (t, ch, &msgcc.header, GNUNET_YES);
1448 * Handler for mesh network payload traffic.
1450 * @param ch Channel for the message.
1451 * @param msg Unencryted data message.
1452 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1455 GMCH_handle_data (struct MeshChannel *ch,
1456 const struct GNUNET_MESH_Data *msg,
1459 struct MeshChannelReliability *rel;
1460 struct MeshClient *c;
1463 /* Initialize FWD/BCK data */
1464 c = fwd ? ch->dest : ch->root;
1465 rel = fwd ? ch->dest_rel : ch->root_rel;
1473 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1475 mid = ntohl (msg->mid);
1476 LOG (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
1478 if (GNUNET_NO == ch->reliable ||
1479 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
1480 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1482 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1483 if (GNUNET_YES == ch->reliable)
1485 /* Is this the exact next expected messasge? */
1486 if (mid == rel->mid_recv)
1488 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1490 send_client_data (ch, msg, fwd);
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1495 add_buffered_data (msg, rel);
1500 /* Tunnel is unreliable: send to clients directly */
1501 /* FIXME: accept Out Of Order traffic */
1502 rel->mid_recv = mid + 1;
1503 send_client_data (ch, msg, fwd);
1508 GNUNET_break_op (0);
1509 LOG (GNUNET_ERROR_TYPE_DEBUG,
1510 " MID %u not expected (%u - %u), dropping!\n",
1511 mid, rel->mid_recv, rel->mid_recv + 64);
1514 GMCH_send_data_ack (ch, fwd);
1519 * Handler for mesh network traffic end-to-end ACKs.
1521 * @param ch Channel on which we got this message.
1522 * @param msg Data message.
1523 * @param fwd Is this a fwd ACK? (dest->orig)
1526 GMCH_handle_data_ack (struct MeshChannel *ch,
1527 const struct GNUNET_MESH_DataACK *msg,
1530 struct MeshChannelReliability *rel;
1531 struct MeshReliableMessage *copy;
1532 struct MeshReliableMessage *next;
1536 ack = ntohl (msg->mid);
1537 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1538 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1540 if (GNUNET_YES == fwd)
1554 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1556 if (GMC_is_pid_bigger (copy->mid, ack))
1558 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1559 channel_rel_free_sent (rel, msg);
1563 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
1565 rel_message_free (copy);
1567 /* ACK client if needed */
1568 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
1570 /* If some message was free'd, update the retransmission delay*/
1571 if (GNUNET_YES == work)
1573 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1575 GNUNET_SCHEDULER_cancel (rel->retry_task);
1576 if (NULL == rel->head_sent)
1578 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1582 struct GNUNET_TIME_Absolute new_target;
1583 struct GNUNET_TIME_Relative delay;
1585 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1586 MESH_RETRANSMIT_MARGIN);
1587 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1589 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1591 GNUNET_SCHEDULER_add_delayed (delay,
1592 &channel_retransmit_message,
1603 * Handler for channel create messages.
1605 * @param t Tunnel this channel will be in.
1606 * @param msg Message.
1607 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1609 struct MeshChannel *
1610 GMCH_handle_create (struct MeshTunnel3 *t,
1611 const struct GNUNET_MESH_ChannelCreate *msg,
1614 MESH_ChannelNumber chid;
1615 struct MeshChannel *ch;
1616 struct MeshClient *c;
1619 chid = ntohl (msg->chid);
1621 ch = GMT_get_channel (t, chid);
1624 /* Create channel */
1625 ch = channel_new (t, NULL, 0);
1628 channel_set_options (ch, ntohl (msg->opt));
1630 /* Find a destination client */
1631 port = ntohl (msg->port);
1632 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
1633 c = GML_client_get_by_port (port);
1636 /* TODO send reject */
1637 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
1638 channel_destroy (ch);
1643 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
1646 add_destination (ch, c);
1647 if (GNUNET_YES == ch->reliable)
1648 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1650 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
1652 GMCH_send_create (ch);
1653 channel_send_ack (ch, fwd);
1660 * Handler for channel ack messages.
1662 * @param ch Channel.
1663 * @param msg Message.
1664 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1667 GMCH_handle_ack (struct MeshChannel *ch,
1668 const struct GNUNET_MESH_ChannelManage *msg,
1671 channel_confirm (ch, !fwd);
1676 * Handler for channel destroy messages.
1678 * @param ch Channel to be destroyed of.
1679 * @param msg Message.
1680 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1683 GMCH_handle_destroy (struct MeshChannel *ch,
1684 const struct GNUNET_MESH_ChannelManage *msg,
1687 struct MeshTunnel3 *t;
1690 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
1692 /* Not for us (don't destroy twice a half-open loopback channel) */
1697 GMCH_send_destroy (ch);
1698 channel_destroy (ch);
1699 GMT_destroy_if_empty (t);
1704 * Sends an already built message on a channel.
1706 * If the channel is on a loopback tunnel, notifies the appropriate destination
1709 * On a normal channel passes the message to the tunnel for encryption and
1710 * sending on a connection.
1712 * This function DOES NOT save the message for retransmission.
1714 * @param message Message to send. Function makes a copy of it.
1715 * @param ch Channel on which this message is transmitted.
1716 * @param fwd Is this a fwd message?
1719 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1720 struct MeshChannel *ch, int fwd)
1722 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH Send %s %s on channel %s\n",
1723 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (ntohs (message->type)),
1726 if (GMT_is_loopback (ch->t))
1728 handle_loopback (ch, message, fwd);
1732 GMT_send_prebuilt_message (message, ch->t, ch, fwd);
1737 * Get the static string for identification of the channel.
1739 * @param ch Channel.
1741 * @return Static string with the channel IDs.
1744 GMCH_2s (const struct MeshChannel *ch)
1746 static char buf[64];
1749 return "(NULL Channel)";
1751 sprintf (buf, "%s:%X (%X / %X)",
1752 GMT_2s (ch->t), ch->gid, ch->lid_root, ch->lid_dest);