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.
282 * @param update_time Is the timing information relevant?
283 * If this message is ACK in a batch the timing information
284 * is skewed by the retransmission, count only for the
285 * retransmitted message.
288 rel_message_free (struct MeshReliableMessage *copy, int update_time);
291 * We have received a message out of order, or the client is not ready.
292 * Buffer it until we receive an ACK from the client or the missing
293 * message from the channel.
295 * @param msg Message to buffer (MUST be of type MESH_DATA).
296 * @param rel Reliability data to the corresponding direction.
299 add_buffered_data (const struct GNUNET_MESH_Data *msg,
300 struct MeshChannelReliability *rel)
302 struct MeshReliableMessage *copy;
303 struct MeshReliableMessage *prev;
307 size = ntohs (msg->header.size);
308 mid = ntohl (msg->mid);
310 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
312 copy = GNUNET_malloc (sizeof (*copy) + size);
315 copy->type = GNUNET_MESSAGE_TYPE_MESH_DATA;
316 memcpy (©[1], msg, size);
320 // FIXME do something better than O(n), although n < 64...
321 // FIXME start from the end (most messages are the latest ones)
322 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
324 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
325 if (GMC_is_pid_bigger (prev->mid, mid))
327 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
328 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
333 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
334 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
335 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
339 * Add a destination client to a channel, initializing all data structures
340 * in the channel and the client.
342 * @param ch Channel to which add the destination.
343 * @param c Client which to add to the channel.
346 add_destination (struct MeshChannel *ch, struct MeshClient *c)
348 if (NULL != ch->dest)
354 /* Assign local id as destination */
355 ch->lid_dest = GML_get_next_chid (c);
357 /* Store in client's hashmap */
358 GML_channel_add (c, ch->lid_dest, ch);
360 GNUNET_break (NULL == ch->dest_rel);
361 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
362 ch->dest_rel->ch = ch;
363 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
370 * Send data to a client.
372 * If the client is ready, send directly, otherwise buffer while listening
376 * @param msg Message.
377 * @param fwd Is this a fwd (root->dest) message?
380 send_client_data (struct MeshChannel *ch,
381 const struct GNUNET_MESH_Data *msg,
386 if (ch->dest_rel->client_ready)
387 GML_send_data (ch->dest, msg, ch->lid_dest);
389 add_buffered_data (msg, ch->dest_rel);
393 if (ch->root_rel->client_ready)
394 GML_send_data (ch->root, msg, ch->lid_root);
396 add_buffered_data (msg, ch->root_rel);
402 * Send a buffered message to the client, for in order delivery or
403 * as result of client ACK.
405 * @param ch Channel on which to empty the message buffer.
406 * @param c Client to send to.
407 * @param fwd Is this to send FWD data?.
410 send_client_buffered_data (struct MeshChannel *ch,
411 struct MeshClient *c,
414 struct MeshReliableMessage *copy;
415 struct MeshChannelReliability *rel;
417 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
418 rel = fwd ? ch->dest_rel : ch->root_rel;
419 if (GNUNET_NO == rel->client_ready)
421 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
425 copy = rel->head_recv;
426 /* We never buffer channel management messages */
429 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
431 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
433 LOG (GNUNET_ERROR_TYPE_DEBUG,
434 " have %u! now expecting %u\n",
435 copy->mid, rel->mid_recv + 1);
436 send_client_data (ch, msg, fwd);
439 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
444 LOG (GNUNET_ERROR_TYPE_DEBUG,
445 " reliable && don't have %u, next is %u\n",
451 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
456 * Allow a client to send more data.
458 * In case the client was already allowed to send data, do nothing.
461 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
464 send_client_ack (struct MeshChannel *ch, int fwd)
466 struct MeshChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
468 LOG (GNUNET_ERROR_TYPE_DEBUG,
469 " sending %s ack to client on channel %s\n",
470 fwd ? "FWD" : "BCK", GMCH_2s (ch));
478 if (GNUNET_YES == rel->client_allowed)
480 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
483 rel->client_allowed = GNUNET_YES;
485 GML_send_ack (fwd ? ch->root : ch->dest, fwd ? ch->lid_root : ch->lid_dest);
490 * Notify the root that the destination rejected the channel.
492 * @param ch Rejected channel.
495 send_client_nack (struct MeshChannel *ch)
497 if (NULL == ch->root)
502 GML_send_nack (ch->root, ch->lid_root);
507 * Destroy all reliable messages queued for a channel,
508 * during a channel destruction.
509 * Frees the reliability structure itself.
511 * @param rel Reliability data for a channel.
514 channel_rel_free_all (struct MeshChannelReliability *rel)
516 struct MeshReliableMessage *copy;
517 struct MeshReliableMessage *next;
522 for (copy = rel->head_recv; NULL != copy; copy = next)
525 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
528 for (copy = rel->head_sent; NULL != copy; copy = next)
531 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
534 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
535 GNUNET_SCHEDULER_cancel (rel->retry_task);
541 * Mark future messages as ACK'd.
543 * @param rel Reliability data.
544 * @param msg DataACK message with a bitfield of future ACK'd messages.
547 channel_rel_free_sent (struct MeshChannelReliability *rel,
548 const struct GNUNET_MESH_DataACK *msg)
550 struct MeshReliableMessage *copy;
551 struct MeshReliableMessage *next;
558 bitfield = msg->futures;
559 mid = ntohl (msg->mid);
560 LOG (GNUNET_ERROR_TYPE_DEBUG,
561 "!!! free_sent_reliable %u %llX\n",
563 LOG (GNUNET_ERROR_TYPE_DEBUG,
564 " rel %p, head %p\n",
565 rel, rel->head_sent);
566 for (i = 0, copy = rel->head_sent;
567 i < 64 && NULL != copy && 0 != bitfield;
570 LOG (GNUNET_ERROR_TYPE_DEBUG,
571 " trying bit %u (mid %u)\n",
574 if (0 == (bitfield & mask))
577 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
578 /* Bit was set, clear the bit from the bitfield */
581 /* The i-th bit was set. Do we have that copy? */
582 /* Skip copies with mid < target */
583 target = mid + i + 1;
584 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
585 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
588 /* Did we run out of copies? (previously freed, it's ok) */
591 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
595 /* Did we overshoot the target? (previously freed, it's ok) */
596 if (GMC_is_pid_bigger (copy->mid, target))
598 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
602 /* Now copy->mid == target, free it */
604 rel_message_free (copy, GNUNET_YES);
607 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
612 * We haven't received an ACK after a certain time: restransmit the message.
614 * @param cls Closure (MeshReliableMessage with the message to restransmit)
615 * @param tc TaskContext.
618 channel_retransmit_message (void *cls,
619 const struct GNUNET_SCHEDULER_TaskContext *tc)
621 struct MeshChannelReliability *rel = cls;
622 struct MeshReliableMessage *copy;
623 struct MeshChannel *ch;
624 struct GNUNET_MESH_Data *payload;
627 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
628 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
632 copy = rel->head_sent;
639 /* Search the message to be retransmitted in the outgoing queue.
640 * Check only the queue for the connection that is going to be used,
641 * if the message is stuck in some other connection's queue we shouldn't
643 * - cancelling it and sending the new one doesn't guarantee it's delivery,
644 * the old connection could be temporary stalled or the queue happened to
645 * be long at time of insertion.
646 * - not sending the new one could cause terrible delays the old connection
649 // FIXME access to queue elements is limited
650 payload = (struct GNUNET_MESH_Data *) ©[1];
651 fwd = (rel == ch->root_rel);
652 // c = GMT_get_connection (ch->t, fwd);
653 // hop = connection_get_hop (c, fwd);
654 // for (q = hop->queue_head; NULL != q; q = q->next)
656 // if (ntohs (payload->header.type) == q->type && ch == q->ch)
658 // struct GNUNET_MESH_Data *queued_data = q->cls;
660 // if (queued_data->mid == payload->mid)
665 /* Message not found in the queue that we are going to use. */
668 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
670 GMCH_send_prebuilt_message (&payload->header, ch, fwd);
671 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
675 // LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
678 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
679 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
680 &channel_retransmit_message,
686 * Destroy a reliable message after it has been acknowledged, either by
687 * direct mid ACK or bitfield. Updates the appropriate data structures and
688 * timers and frees all memory.
690 * @param copy Message that is no longer needed: remote peer got it.
691 * @param update_time Is the timing information relevant?
692 * If this message is ACK in a batch the timing information
693 * is skewed by the retransmission, count only for the
694 * retransmitted message.
697 rel_message_free (struct MeshReliableMessage *copy, int update_time)
699 struct MeshChannelReliability *rel;
700 struct GNUNET_TIME_Relative time;
703 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
704 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
707 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
708 rel->expected_delay.rel_value_us *= 19;
709 rel->expected_delay.rel_value_us += time.rel_value_us;
710 rel->expected_delay.rel_value_us /= 20;
712 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
713 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
714 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
715 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
717 rel->retry_timer = rel->expected_delay;
721 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! batch free, ignoring timing\n");
723 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
729 * Confirm we got a channel create.
731 * @param ch The channel to confirm.
732 * @param fwd Should we send a FWD ACK? (going dest->root)
735 channel_send_ack (struct MeshChannel *ch, int fwd)
737 struct GNUNET_MESH_ChannelManage msg;
739 msg.header.size = htons (sizeof (msg));
740 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
741 LOG (GNUNET_ERROR_TYPE_DEBUG,
742 " sending channel %s ack for channel %s\n",
743 fwd ? "FWD" : "BCK", GMCH_2s (ch));
745 msg.chid = htonl (ch->gid);
746 GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
751 * Notify that a channel create didn't succeed.
753 * @param ch The channel to reject.
756 channel_send_nack (struct MeshChannel *ch)
758 struct GNUNET_MESH_ChannelManage msg;
760 msg.header.size = htons (sizeof (msg));
761 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK);
762 LOG (GNUNET_ERROR_TYPE_DEBUG,
763 " sending channel NACK for channel %s\n",
766 msg.chid = htonl (ch->gid);
767 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO);
772 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
774 * @param ch Channel to mark as ready.
775 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
778 channel_confirm (struct MeshChannel *ch, int fwd)
780 struct MeshChannelReliability *rel;
781 struct MeshReliableMessage *copy;
782 struct MeshReliableMessage *next;
784 LOG (GNUNET_ERROR_TYPE_DEBUG,
785 " channel confirm %s %s:%X\n",
786 fwd ? "FWD" : "BCK", GMT_2s (ch->t), ch->gid);
787 ch->state = MESH_CHANNEL_READY;
789 rel = fwd ? ch->root_rel : ch->dest_rel;
790 rel->client_ready = GNUNET_YES;
791 for (copy = rel->head_sent; NULL != copy; copy = next)
793 struct GNUNET_MessageHeader *msg;
796 msg = (struct GNUNET_MessageHeader *) ©[1];
797 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
799 rel_message_free (copy, GNUNET_YES);
803 send_client_ack (ch, fwd);
805 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
807 channel_send_ack (ch, !fwd);
812 * Save a copy to retransmit in case it gets lost.
814 * Initializes all needed callbacks and timers.
816 * @param ch Channel this message goes on.
817 * @param msg Message to copy.
818 * @param fwd Is this fwd traffic?
821 channel_save_copy (struct MeshChannel *ch,
822 const struct GNUNET_MessageHeader *msg,
825 struct MeshChannelReliability *rel;
826 struct MeshReliableMessage *copy;
831 rel = fwd ? ch->root_rel : ch->dest_rel;
832 mid = rel->mid_send - 1;
833 type = ntohs (msg->type);
834 size = ntohs (msg->size);
836 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u %s\n",
837 mid, GNUNET_MESH_DEBUG_M2S (type));
838 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
840 copy->timestamp = GNUNET_TIME_absolute_get ();
843 memcpy (©[1], msg, size);
845 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
846 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
847 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
850 GNUNET_TIME_relative_multiply (rel->expected_delay,
851 MESH_RETRANSMIT_MARGIN);
853 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
854 &channel_retransmit_message,
861 * Destroy a channel and free all resources.
863 * @param ch Channel to destroy.
866 channel_destroy (struct MeshChannel *ch)
868 struct MeshClient *c;
873 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
874 GMT_2s (ch->t), ch->gid);
880 GML_channel_remove (c, ch->lid_root, ch);
886 GML_channel_remove (c, ch->lid_dest, ch);
889 channel_rel_free_all (ch->root_rel);
890 channel_rel_free_all (ch->dest_rel);
892 GMT_remove_channel (ch->t, ch);
893 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
900 * Create a new channel.
902 * @param t Tunnel this channel is in.
903 * @param owner Client that owns the channel, NULL for foreign channels.
904 * @param lid_root Local ID for root client.
906 * @return A new initialized channel. NULL on error.
908 static struct MeshChannel *
909 channel_new (struct MeshTunnel3 *t,
910 struct MeshClient *owner,
911 MESH_ChannelNumber lid_root)
913 struct MeshChannel *ch;
915 ch = GNUNET_new (struct MeshChannel);
917 ch->lid_root = lid_root;
920 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
924 ch->gid = GMT_get_next_chid (t);
925 GML_channel_add (owner, lid_root, ch);
927 GMT_add_channel (t, ch);
934 * Set options in a channel, extracted from a bit flag field
936 * @param ch Channel to set options to.
937 * @param options Bit array in host byte order.
940 channel_set_options (struct MeshChannel *ch, uint32_t options)
942 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
943 GNUNET_YES : GNUNET_NO;
944 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
945 GNUNET_YES : GNUNET_NO;
949 is_loopback (const struct MeshChannel *ch)
952 return GMT_is_loopback (ch->t);
954 return (NULL != ch->root && NULL != ch->dest);
959 * Handle a loopback message: call the appropriate handler for the message type.
961 * @param ch Channel this message is on.
962 * @param msgh Message header.
963 * @param fwd Is this FWD traffic?
966 handle_loopback (struct MeshChannel *ch,
967 const struct GNUNET_MessageHeader *msgh,
972 type = ntohs (msgh->type);
973 LOG (GNUNET_ERROR_TYPE_DEBUG,
974 "Loopback %s %s message!\n",
975 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type));
979 case GNUNET_MESSAGE_TYPE_MESH_DATA:
980 /* Don't send hop ACK, wait for client to ACK */
981 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
984 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
985 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
988 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
989 GMCH_handle_create (ch->t,
990 (struct GNUNET_MESH_ChannelCreate *) msgh);
993 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
995 (struct GNUNET_MESH_ChannelManage *) msgh,
999 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1000 GMCH_handle_nack (ch);
1003 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1004 GMCH_handle_destroy (ch,
1005 (struct GNUNET_MESH_ChannelManage *) msgh,
1010 GNUNET_break_op (0);
1011 LOG (GNUNET_ERROR_TYPE_DEBUG,
1012 "end-to-end message not known (%u)\n",
1013 ntohs (msgh->type));
1019 /******************************************************************************/
1020 /******************************** API ***********************************/
1021 /******************************************************************************/
1027 * @param ch Channel.
1032 GMCH_get_id (const struct MeshChannel *ch)
1039 * Get the channel tunnel.
1041 * @param ch Channel to get the tunnel from.
1043 * @return tunnel of the channel.
1045 struct MeshTunnel3 *
1046 GMCH_get_tunnel (const struct MeshChannel *ch)
1053 * Get free buffer space towards the client on a specific channel.
1055 * @param ch Channel.
1056 * @param fwd Is query about FWD traffic?
1058 * @return Free buffer space [0 - 64]
1061 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
1063 struct MeshChannelReliability *rel;
1065 rel = fwd ? ch->dest_rel : ch->root_rel;
1067 /* If rel is NULL it means that the end is not yet created,
1068 * most probably is a loopback channel at the point of sending
1069 * the ChannelCreate to itself.
1074 return (64 - rel->n_recv);
1079 * Get flow control status of end point: is client allow to send?
1081 * @param ch Channel.
1082 * @param fwd Is query about FWD traffic? (Request root status).
1084 * @return #GNUNET_YES if client is allowed to send us data.
1087 GMCH_get_allowed (struct MeshChannel *ch, int fwd)
1089 struct MeshChannelReliability *rel;
1091 rel = fwd ? ch->root_rel : ch->dest_rel;
1093 return rel->client_allowed;
1098 * Is the root client for this channel on this peer?
1100 * @param ch Channel.
1101 * @param fwd Is this for fwd traffic?
1103 * @return #GNUNET_YES in case it is.
1106 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1108 struct MeshClient *c;
1110 c = fwd ? ch->root : ch->dest;
1116 * Is the destination client for this channel on this peer?
1118 * @param ch Channel.
1119 * @param fwd Is this for fwd traffic?
1121 * @return #GNUNET_YES in case it is.
1124 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1126 struct MeshClient *c;
1128 c = fwd ? ch->dest : ch->root;
1134 * Notify the destination client that a new incoming channel was created.
1136 * @param ch Channel that was created.
1139 GMCH_send_create (struct MeshChannel *ch)
1143 if (NULL == ch->dest)
1147 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1148 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1149 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1150 GMT_get_destination (ch->t));
1155 * Notify a client that the channel is no longer valid.
1157 * @param ch Channel that is destroyed.
1160 GMCH_send_destroy (struct MeshChannel *ch)
1162 struct GNUNET_MESH_ChannelManage msg;
1164 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1165 msg.header.size = htons (sizeof (msg));
1166 msg.chid = htonl (ch->gid);
1168 /* If root is not NULL, notify.
1169 * If it's NULL, check lid_root. When a local destroy comes in, root
1170 * is set to NULL but lid_root is left untouched. In this case, do nothing,
1171 * the client is the one who reuqested the channel to be destroyed.
1173 if (NULL != ch->root)
1174 GML_send_channel_destroy (ch->root, ch->lid_root);
1175 else if (0 == ch->lid_root)
1176 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO);
1178 if (NULL != ch->dest)
1179 GML_send_channel_destroy (ch->dest, ch->lid_dest);
1180 else if (0 == ch->lid_dest)
1181 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES);
1186 * Send data on a channel.
1188 * If the destination is local, send it to client, otherwise encrypt and
1192 * @param msg Message.
1193 * @param fwd Is this a fwd (root->dest) message?
1196 GMCH_send_data (struct MeshChannel *ch,
1197 const struct GNUNET_MESH_Data *msg,
1200 if (GMCH_is_terminal (ch, fwd))
1202 GML_send_data (fwd ? ch->dest : ch->root,
1204 fwd ? ch->lid_dest : ch->lid_root);
1208 GMT_send_prebuilt_message (&msg->header, ch->t, ch, fwd);
1214 * Send an end-to-end ACK message for the most recent in-sequence payload.
1216 * If channel is not reliable, do nothing.
1218 * @param ch Channel this is about.
1219 * @param fwd Is for FWD traffic? (ACK dest->owner)
1222 GMCH_send_data_ack (struct MeshChannel *ch, int fwd)
1224 struct GNUNET_MESH_DataACK msg;
1225 struct MeshChannelReliability *rel;
1226 struct MeshReliableMessage *copy;
1231 if (GNUNET_NO == ch->reliable)
1235 rel = fwd ? ch->dest_rel : ch->root_rel;
1236 LOG (GNUNET_ERROR_TYPE_DEBUG,
1237 "!!! Send DATA_ACK for %u\n",
1240 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
1241 msg.header.type = htons (type);
1242 msg.header.size = htons (sizeof (msg));
1243 msg.chid = htonl (ch->gid);
1244 msg.mid = htonl (rel->mid_recv - 1);
1246 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1248 if (copy->type != GNUNET_MESSAGE_TYPE_MESH_DATA)
1250 LOG (GNUNET_ERROR_TYPE_DEBUG,
1252 GNUNET_MESH_DEBUG_M2S (copy->type));
1253 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 GNUNET_MESH_DEBUG_M2S (type));
1258 delta = copy->mid - rel->mid_recv;
1261 mask = 0x1LL << delta;
1262 msg.futures |= mask;
1263 LOG (GNUNET_ERROR_TYPE_DEBUG,
1264 " !! setting bit for %u (delta %u) (%llX) -> %llX\n",
1265 copy->mid, delta, mask, msg.futures);
1267 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! final futures %llX\n", msg.futures);
1269 GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
1270 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1275 * Allow a client to send us more data, in case it was choked.
1277 * @param ch Channel.
1278 * @param fwd Is this about FWD traffic? (Root client).
1281 GMCH_allow_client (struct MeshChannel *ch, int fwd)
1283 struct MeshChannelReliability *rel;
1285 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1287 if (MESH_CHANNEL_READY != ch->state)
1289 LOG (GNUNET_ERROR_TYPE_DEBUG, " not ready yet!\n");
1293 if (GNUNET_YES == ch->reliable)
1295 rel = fwd ? ch->root_rel : ch->dest_rel;
1301 if (64 <= rel->mid_send - rel->head_sent->mid)
1303 LOG (GNUNET_ERROR_TYPE_DEBUG,
1304 " too big mid gap! Wait for ACK.\n");
1309 send_client_ack (ch, fwd);
1316 * @param ch Channel.
1319 GMCH_debug (struct MeshChannel *ch)
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1326 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1327 GMT_2s (ch->t), ch->gid, ch);
1328 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1329 ch->root, ch->root_rel);
1330 if (NULL != ch->root)
1332 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1333 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1334 ch->root_rel->client_ready ? "YES" : "NO");
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1337 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1338 ch->dest, ch->dest_rel);
1339 if (NULL != ch->dest)
1341 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1342 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1343 ch->dest_rel->client_ready ? "YES" : "NO");
1344 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1350 * Handle an ACK given by a client.
1352 * Mark client as ready and send him any buffered data we could have for him.
1354 * @param ch Channel.
1355 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1358 GMCH_handle_local_ack (struct MeshChannel *ch, int fwd)
1360 struct MeshChannelReliability *rel;
1361 struct MeshClient *c;
1363 rel = fwd ? ch->dest_rel : ch->root_rel;
1364 c = fwd ? ch->dest : ch->root;
1366 rel->client_ready = GNUNET_YES;
1367 send_client_buffered_data (ch, c, fwd);
1368 if (is_loopback (ch))
1370 unsigned int buffer;
1372 buffer = GMCH_get_buffer (ch, fwd);
1374 GMCH_allow_client (ch, fwd);
1378 GMT_send_connection_acks (ch->t);
1383 * Handle data given by a client.
1385 * Check whether the client is allowed to send in this tunnel, save if channel
1386 * is reliable and send an ACK to the client if there is still buffer space
1389 * @param ch Channel.
1390 * @param c Client which sent the data.
1391 * @param message Message.
1392 * @param fwd Is this a FWD data?
1394 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1397 GMCH_handle_local_data (struct MeshChannel *ch,
1398 struct MeshClient *c,
1399 struct GNUNET_MessageHeader *message,
1402 struct MeshChannelReliability *rel;
1403 struct GNUNET_MESH_Data *payload;
1404 size_t size = ntohs (message->size);
1405 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
1406 unsigned char cbuf[p2p_size];
1408 /* Is the client in the channel? */
1415 GNUNET_break_op (0);
1416 return GNUNET_SYSERR;
1419 rel = fwd ? ch->root_rel : ch->dest_rel;
1421 if (GNUNET_NO == rel->client_allowed)
1423 GNUNET_break_op (0);
1424 return GNUNET_SYSERR;
1427 rel->client_allowed = GNUNET_NO;
1429 /* Ok, everything is correct, send the message. */
1430 payload = (struct GNUNET_MESH_Data *) cbuf;
1431 payload->mid = htonl (rel->mid_send);
1433 memcpy (&payload[1], message, size);
1434 payload->header.size = htons (p2p_size);
1435 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
1436 payload->chid = htonl (ch->gid);
1437 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1438 if (GNUNET_YES == ch->reliable)
1439 channel_save_copy (ch, &payload->header, fwd);
1440 GMCH_send_prebuilt_message (&payload->header, ch, fwd);
1442 if (is_loopback (ch))
1444 if (GMCH_get_buffer (ch, fwd) > 0);
1445 send_client_ack (ch, fwd);
1450 if (GMT_get_connections_buffer (ch->t) > 0)
1452 send_client_ack (ch, fwd);
1460 * Handle a channel destroy requested by a client.
1462 * Destroy the channel and the tunnel in case this was the last channel.
1464 * @param ch Channel.
1465 * @param c Client that requested the destruction (to avoid notifying him).
1468 GMCH_handle_local_destroy (struct MeshChannel *ch,
1469 struct MeshClient *c)
1471 struct MeshTunnel3 *t;
1473 /* Cleanup after the tunnel */
1476 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1477 GML_client_delete_channel (c, ch, ch->lid_dest);
1482 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1483 GML_client_delete_channel (c, ch, ch->lid_root);
1488 GMCH_send_destroy (ch);
1489 channel_destroy (ch);
1490 GMT_destroy_if_empty (t);
1495 * Handle a channel create requested by a client.
1497 * Create the channel and the tunnel in case this was the first0 channel.
1499 * @param c Client that requested the creation (will be the root).
1500 * @param msg Create Channel message.
1502 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1505 GMCH_handle_local_create (struct MeshClient *c,
1506 struct GNUNET_MESH_ChannelMessage *msg)
1508 struct MeshChannel *ch;
1509 struct MeshTunnel3 *t;
1510 struct MeshPeer *peer;
1511 MESH_ChannelNumber chid;
1513 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1514 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1515 chid = ntohl (msg->channel_id);
1517 /* Sanity check for duplicate channel IDs */
1518 if (NULL != GML_channel_get (c, chid))
1521 return GNUNET_SYSERR;
1524 peer = GMP_get (&msg->peer);
1525 GMP_add_tunnel (peer);
1526 t = GMP_get_tunnel (peer);
1528 if (GMP_get_short_id (peer) == myid)
1530 GMT_change_state (t, MESH_TUNNEL3_READY);
1537 /* Create channel */
1538 ch = channel_new (t, c, chid);
1542 return GNUNET_SYSERR;
1544 ch->port = ntohl (msg->port);
1545 channel_set_options (ch, ntohl (msg->opt));
1547 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1548 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
1549 ch->root_rel->ch = ch;
1550 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
1552 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
1554 /* Send create channel */
1556 struct GNUNET_MESH_ChannelCreate msgcc;
1558 msgcc.header.size = htons (sizeof (msgcc));
1559 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1560 msgcc.chid = htonl (ch->gid);
1561 msgcc.port = msg->port;
1562 msgcc.opt = msg->opt;
1564 GMT_send_prebuilt_message (&msgcc.header, t, ch, GNUNET_YES);
1571 * Handler for mesh network payload traffic.
1573 * @param ch Channel for the message.
1574 * @param msg Unencryted data message.
1575 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1576 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1577 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1578 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1581 GMCH_handle_data (struct MeshChannel *ch,
1582 const struct GNUNET_MESH_Data *msg,
1585 struct MeshChannelReliability *rel;
1586 struct MeshClient *c;
1589 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1590 if (GNUNET_SYSERR == fwd)
1592 if (is_loopback (ch))
1594 /* It is a loopback channel after all... */
1598 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1601 /* Initialize FWD/BCK data */
1602 c = fwd ? ch->dest : ch->root;
1603 rel = fwd ? ch->dest_rel : ch->root_rel;
1611 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1613 mid = ntohl (msg->mid);
1614 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! got mid %u\n", mid);
1616 if (GNUNET_NO == ch->reliable ||
1617 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
1618 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1620 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1621 if (GNUNET_YES == ch->reliable)
1623 /* Is this the exact next expected messasge? */
1624 if (mid == rel->mid_recv)
1626 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1628 send_client_data (ch, msg, fwd);
1632 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1633 add_buffered_data (msg, rel);
1638 /* Tunnel is unreliable: send to clients directly */
1639 /* FIXME: accept Out Of Order traffic */
1640 rel->mid_recv = mid + 1;
1641 send_client_data (ch, msg, fwd);
1646 GNUNET_break_op (0);
1647 LOG (GNUNET_ERROR_TYPE_DEBUG,
1648 " MID %u not expected (%u - %u), dropping!\n",
1649 mid, rel->mid_recv, rel->mid_recv + 63);
1652 GMCH_send_data_ack (ch, fwd);
1657 * Handler for mesh network traffic end-to-end ACKs.
1659 * @param ch Channel on which we got this message.
1660 * @param msg Data message.
1661 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1662 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1663 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1664 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1667 GMCH_handle_data_ack (struct MeshChannel *ch,
1668 const struct GNUNET_MESH_DataACK *msg,
1671 struct MeshChannelReliability *rel;
1672 struct MeshReliableMessage *copy;
1673 struct MeshReliableMessage *next;
1677 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1678 if (GNUNET_SYSERR == fwd)
1680 if (is_loopback (ch))
1682 /* It is a loopback channel after all... */
1686 /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
1687 fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
1690 ack = ntohl (msg->mid);
1691 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1692 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1694 if (GNUNET_YES == fwd)
1704 GNUNET_break_op (0);
1708 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1710 if (GMC_is_pid_bigger (copy->mid, ack))
1712 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1713 channel_rel_free_sent (rel, msg);
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
1719 rel_message_free (copy, copy->mid == ack);
1721 /* ACK client if needed */
1722 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
1724 /* If some message was free'd, update the retransmission delay */
1725 if (GNUNET_YES == work)
1727 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1729 GNUNET_SCHEDULER_cancel (rel->retry_task);
1730 if (NULL == rel->head_sent)
1732 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1736 struct GNUNET_TIME_Absolute new_target;
1737 struct GNUNET_TIME_Relative delay;
1739 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1740 MESH_RETRANSMIT_MARGIN);
1741 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1743 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1745 GNUNET_SCHEDULER_add_delayed (delay,
1746 &channel_retransmit_message,
1757 * Handler for channel create messages.
1759 * Does not have fwd parameter because it's always 'FWD': channel is incoming.
1761 * @param t Tunnel this channel will be in.
1762 * @param msg Channel crate message.
1764 struct MeshChannel *
1765 GMCH_handle_create (struct MeshTunnel3 *t,
1766 const struct GNUNET_MESH_ChannelCreate *msg)
1768 MESH_ChannelNumber chid;
1769 struct MeshChannel *ch;
1770 struct MeshClient *c;
1773 chid = ntohl (msg->chid);
1775 ch = GMT_get_channel (t, chid);
1778 /* Create channel */
1779 ch = channel_new (t, NULL, 0);
1782 channel_set_options (ch, ntohl (msg->opt));
1784 /* Find a destination client */
1785 port = ntohl (msg->port);
1786 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
1787 c = GML_client_get_by_port (port);
1790 /* TODO send reject */
1791 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
1792 if (is_loopback (ch))
1794 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback: destroy on handler\n");
1795 channel_send_nack (ch);
1799 LOG (GNUNET_ERROR_TYPE_DEBUG, " not loopback: destroy now\n");
1800 channel_send_nack (ch);
1801 channel_destroy (ch);
1807 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
1810 add_destination (ch, c);
1811 if (GNUNET_YES == ch->reliable)
1812 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1814 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
1816 GMCH_send_create (ch);
1817 channel_send_ack (ch, GNUNET_YES);
1824 * Handler for channel NACK messages.
1826 * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
1828 * @param ch Channel.
1831 GMCH_handle_nack (struct MeshChannel *ch)
1833 send_client_nack (ch);
1834 channel_destroy (ch);
1839 * Handler for channel ack messages.
1841 * @param ch Channel.
1842 * @param msg Message.
1843 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1844 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1845 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1846 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1849 GMCH_handle_ack (struct MeshChannel *ch,
1850 const struct GNUNET_MESH_ChannelManage *msg,
1853 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1854 if (GNUNET_SYSERR == fwd)
1856 if (is_loopback (ch))
1858 /* It is a loopback channel after all... */
1862 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1865 channel_confirm (ch, !fwd);
1870 * Handler for channel destroy messages.
1872 * @param ch Channel to be destroyed of.
1873 * @param msg Message.
1874 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1875 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1876 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1877 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1880 GMCH_handle_destroy (struct MeshChannel *ch,
1881 const struct GNUNET_MESH_ChannelManage *msg,
1884 struct MeshTunnel3 *t;
1886 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1887 if (GNUNET_SYSERR == fwd)
1889 if (is_loopback (ch))
1891 /* It is a loopback channel after all... */
1895 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1899 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
1901 /* Not for us (don't destroy twice a half-open loopback channel) */
1906 GMCH_send_destroy (ch);
1907 channel_destroy (ch);
1908 GMT_destroy_if_empty (t);
1913 * Sends an already built message on a channel.
1915 * If the channel is on a loopback tunnel, notifies the appropriate destination
1918 * On a normal channel passes the message to the tunnel for encryption and
1919 * sending on a connection.
1921 * This function DOES NOT save the message for retransmission.
1923 * @param message Message to send. Function makes a copy of it.
1924 * @param ch Channel on which this message is transmitted.
1925 * @param fwd Is this a fwd message?
1928 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1929 struct MeshChannel *ch, int fwd)
1931 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH Send %s %s on channel %s\n",
1932 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (ntohs (message->type)),
1935 if (GMT_is_loopback (ch->t))
1937 handle_loopback (ch, message, fwd);
1941 GMT_send_prebuilt_message (message, ch->t, ch, fwd);
1946 * Get the static string for identification of the channel.
1948 * @param ch Channel.
1950 * @return Static string with the channel IDs.
1953 GMCH_2s (const struct MeshChannel *ch)
1955 static char buf[64];
1958 return "(NULL Channel)";
1960 sprintf (buf, "%s:%X (%X / %X)",
1961 GMT_2s (ch->t), ch->gid, ch->lid_root, ch->lid_dest);