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.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.
64 * Info holder for channel messages in queues.
66 struct MeshChannelQueue
71 struct MeshTunnel3Queue *q;
74 * Message type (DATA/DATA_ACK)
79 * Message copy (for DATAs, to start retransmission timer)
81 struct MeshReliableMessage *copy;
84 * Reliability (for DATA_ACKs, to access rel->ack_q)
86 struct MeshChannelReliability *rel;
91 * Info needed to retry a message in case it gets lost.
93 struct MeshReliableMessage
96 * Double linked list, FIFO style
98 struct MeshReliableMessage *next;
99 struct MeshReliableMessage *prev;
102 * Type of message (payload, channel management).
107 * Tunnel Reliability queue this message is in.
109 struct MeshChannelReliability *rel;
112 * ID of the message (ACK needed to free)
119 struct MeshChannelQueue *q;
122 * When was this message issued (to calculate ACK delay)
124 struct GNUNET_TIME_Absolute timestamp;
126 /* struct GNUNET_MESH_Data with payload */
131 * Info about the traffic state for a client in a channel.
133 struct MeshChannelReliability
136 * Channel this is about.
138 struct MeshChannel *ch;
141 * DLL of messages sent and not yet ACK'd.
143 struct MeshReliableMessage *head_sent;
144 struct MeshReliableMessage *tail_sent;
147 * DLL of messages received out of order.
149 struct MeshReliableMessage *head_recv;
150 struct MeshReliableMessage *tail_recv;
158 * Next MID to use for outgoing traffic.
163 * Next MID expected for incoming traffic.
168 * Handle for queued DATA_ACKs.
170 struct MeshChannelQueue *ack_q;
173 * Can we send data to the client?
178 * Can the client send data to us?
183 * Task to resend/poll in case no ACK is received.
185 GNUNET_SCHEDULER_TaskIdentifier retry_task;
188 * Counter for exponential backoff.
190 struct GNUNET_TIME_Relative retry_timer;
193 * How long does it usually take to get an ACK.
195 struct GNUNET_TIME_Relative expected_delay;
200 * Struct containing all information regarding a channel to a remote client.
205 * Tunnel this channel is in.
207 struct MeshTunnel3 *t;
210 * Destination port of the channel.
215 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
217 MESH_ChannelNumber gid;
220 * Local tunnel number for root (owner) client.
221 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
223 MESH_ChannelNumber lid_root;
226 * Local tunnel number for local destination clients (incoming number)
227 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
229 MESH_ChannelNumber lid_dest;
234 enum MeshChannelState state;
237 * Is the tunnel bufferless (minimum latency)?
242 * Is the tunnel reliable?
247 * Last time the channel was used
249 struct GNUNET_TIME_Absolute timestamp;
252 * Client owner of the tunnel, if any
254 struct MeshClient *root;
257 * Client destination of the tunnel, if any.
259 struct MeshClient *dest;
262 * Flag to signal the destruction of the channel.
263 * If this is set GNUNET_YES the channel will be destroyed
264 * when the queue is empty.
269 * Total (reliable) messages pending ACK for this channel.
271 unsigned int pending_messages;
275 * Only present (non-NULL) at the owner of a tunnel.
277 struct MeshChannelReliability *root_rel;
281 * Only present (non-NULL) at the destination of a tunnel.
283 struct MeshChannelReliability *dest_rel;
288 /******************************************************************************/
289 /******************************* GLOBALS ***********************************/
290 /******************************************************************************/
293 * Global handle to the statistics service.
295 extern struct GNUNET_STATISTICS_Handle *stats;
298 * Local peer own ID (memory efficient handle).
300 extern GNUNET_PEER_Id myid;
303 /******************************************************************************/
304 /******************************** STATIC ***********************************/
305 /******************************************************************************/
308 * Destroy a reliable message after it has been acknowledged, either by
309 * direct mid ACK or bitfield. Updates the appropriate data structures and
310 * timers and frees all memory.
312 * @param copy Message that is no longer needed: remote peer got it.
313 * @param update_time Is the timing information relevant?
314 * If this message is ACK in a batch the timing information
315 * is skewed by the retransmission, count only for the
316 * retransmitted message.
319 rel_message_free (struct MeshReliableMessage *copy, int update_time);
322 * We have received a message out of order, or the client is not ready.
323 * Buffer it until we receive an ACK from the client or the missing
324 * message from the channel.
326 * @param msg Message to buffer (MUST be of type MESH_DATA).
327 * @param rel Reliability data to the corresponding direction.
330 add_buffered_data (const struct GNUNET_MESH_Data *msg,
331 struct MeshChannelReliability *rel)
333 struct MeshReliableMessage *copy;
334 struct MeshReliableMessage *prev;
338 size = ntohs (msg->header.size);
339 mid = ntohl (msg->mid);
341 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
343 copy = GNUNET_malloc (sizeof (*copy) + size);
346 copy->type = GNUNET_MESSAGE_TYPE_MESH_DATA;
347 memcpy (©[1], msg, size);
351 // FIXME do something better than O(n), although n < 64...
352 // FIXME start from the end (most messages are the latest ones)
353 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
355 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
356 if (GM_is_pid_bigger (prev->mid, mid))
358 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
359 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
364 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
365 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
366 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
370 * Add a destination client to a channel, initializing all data structures
371 * in the channel and the client.
373 * @param ch Channel to which add the destination.
374 * @param c Client which to add to the channel.
377 add_destination (struct MeshChannel *ch, struct MeshClient *c)
379 if (NULL != ch->dest)
385 /* Assign local id as destination */
386 ch->lid_dest = GML_get_next_chid (c);
388 /* Store in client's hashmap */
389 GML_channel_add (c, ch->lid_dest, ch);
391 GNUNET_break (NULL == ch->dest_rel);
392 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
393 ch->dest_rel->ch = ch;
394 ch->dest_rel->expected_delay.rel_value_us = 0;
401 * Send data to a client.
403 * If the client is ready, send directly, otherwise buffer while listening
407 * @param msg Message.
408 * @param fwd Is this a fwd (root->dest) message?
411 send_client_data (struct MeshChannel *ch,
412 const struct GNUNET_MESH_Data *msg,
417 if (ch->dest_rel->client_ready)
418 GML_send_data (ch->dest, msg, ch->lid_dest);
420 add_buffered_data (msg, ch->dest_rel);
424 if (ch->root_rel->client_ready)
425 GML_send_data (ch->root, msg, ch->lid_root);
427 add_buffered_data (msg, ch->root_rel);
433 * Send a buffered message to the client, for in order delivery or
434 * as result of client ACK.
436 * @param ch Channel on which to empty the message buffer.
437 * @param c Client to send to.
438 * @param fwd Is this to send FWD data?.
441 send_client_buffered_data (struct MeshChannel *ch,
442 struct MeshClient *c,
445 struct MeshReliableMessage *copy;
446 struct MeshChannelReliability *rel;
448 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
449 rel = fwd ? ch->dest_rel : ch->root_rel;
450 if (GNUNET_NO == rel->client_ready)
452 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
456 copy = rel->head_recv;
457 /* We never buffer channel management messages */
460 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
462 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
464 LOG (GNUNET_ERROR_TYPE_DEBUG,
465 " have %u! now expecting %u\n",
466 copy->mid, rel->mid_recv + 1);
467 send_client_data (ch, msg, fwd);
470 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 " reliable && don't have %u, next is %u\n",
482 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
487 * Allow a client to send more data.
489 * In case the client was already allowed to send data, do nothing.
492 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
495 send_client_ack (struct MeshChannel *ch, int fwd)
497 struct MeshChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
499 LOG (GNUNET_ERROR_TYPE_DEBUG,
500 " sending %s ack to client on channel %s\n",
501 GM_f2s (fwd), GMCH_2s (ch));
509 if (GNUNET_YES == rel->client_allowed)
511 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
514 rel->client_allowed = GNUNET_YES;
516 GML_send_ack (fwd ? ch->root : ch->dest, fwd ? ch->lid_root : ch->lid_dest);
521 * Notify the root that the destination rejected the channel.
523 * @param ch Rejected channel.
526 send_client_nack (struct MeshChannel *ch)
528 if (NULL == ch->root)
533 GML_send_nack (ch->root, ch->lid_root);
538 * Notify a client that the channel is no longer valid.
540 * @param ch Channel that is destroyed.
541 * @param local_only Should we avoid sending it to other peers?
544 send_destroy (struct MeshChannel *ch, int local_only)
546 struct GNUNET_MESH_ChannelManage msg;
548 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
549 msg.header.size = htons (sizeof (msg));
550 msg.chid = htonl (ch->gid);
552 /* If root is not NULL, notify.
553 * If it's NULL, check lid_root. When a local destroy comes in, root
554 * is set to NULL but lid_root is left untouched. In this case, do nothing,
555 * the client is the one who reuqested the channel to be destroyed.
557 if (NULL != ch->root)
558 GML_send_channel_destroy (ch->root, ch->lid_root);
559 else if (0 == ch->lid_root && GNUNET_NO == local_only)
560 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
562 if (NULL != ch->dest)
563 GML_send_channel_destroy (ch->dest, ch->lid_dest);
564 else if (0 == ch->lid_dest && GNUNET_NO == local_only)
565 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
570 * Destroy all reliable messages queued for a channel,
571 * during a channel destruction.
572 * Frees the reliability structure itself.
574 * @param rel Reliability data for a channel.
577 channel_rel_free_all (struct MeshChannelReliability *rel)
579 struct MeshReliableMessage *copy;
580 struct MeshReliableMessage *next;
585 for (copy = rel->head_recv; NULL != copy; copy = next)
588 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
591 for (copy = rel->head_sent; NULL != copy; copy = next)
594 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
597 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
598 GNUNET_SCHEDULER_cancel (rel->retry_task);
599 if (NULL != rel->ack_q)
600 GMT_cancel (rel->ack_q->q);
606 * Mark future messages as ACK'd.
608 * @param rel Reliability data.
609 * @param msg DataACK message with a bitfield of future ACK'd messages.
612 channel_rel_free_sent (struct MeshChannelReliability *rel,
613 const struct GNUNET_MESH_DataACK *msg)
615 struct MeshReliableMessage *copy;
616 struct MeshReliableMessage *next;
623 bitfield = msg->futures;
624 mid = ntohl (msg->mid);
625 LOG (GNUNET_ERROR_TYPE_DEBUG,
626 "!!! free_sent_reliable %u %llX\n",
628 LOG (GNUNET_ERROR_TYPE_DEBUG,
629 " rel %p, head %p\n",
630 rel, rel->head_sent);
631 for (i = 0, copy = rel->head_sent;
632 i < 64 && NULL != copy && 0 != bitfield;
635 LOG (GNUNET_ERROR_TYPE_DEBUG,
636 " trying bit %u (mid %u)\n",
639 if (0 == (bitfield & mask))
642 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
643 /* Bit was set, clear the bit from the bitfield */
646 /* The i-th bit was set. Do we have that copy? */
647 /* Skip copies with mid < target */
648 target = mid + i + 1;
649 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
650 while (NULL != copy && GM_is_pid_bigger (target, copy->mid))
653 /* Did we run out of copies? (previously freed, it's ok) */
656 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
660 /* Did we overshoot the target? (previously freed, it's ok) */
661 if (GM_is_pid_bigger (copy->mid, target))
663 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
667 /* Now copy->mid == target, free it */
669 rel_message_free (copy, GNUNET_YES);
672 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
677 * We haven't received an ACK after a certain time: restransmit the message.
679 * @param cls Closure (MeshReliableMessage with the message to restransmit)
680 * @param tc TaskContext.
683 channel_retransmit_message (void *cls,
684 const struct GNUNET_SCHEDULER_TaskContext *tc)
686 struct MeshChannelReliability *rel = cls;
687 struct MeshReliableMessage *copy;
688 struct MeshChannel *ch;
689 struct GNUNET_MESH_Data *payload;
692 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
693 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
697 copy = rel->head_sent;
704 payload = (struct GNUNET_MESH_Data *) ©[1];
705 fwd = (rel == ch->root_rel);
707 /* Message not found in the queue that we are going to use. */
708 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
710 GMCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
711 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
716 * Destroy a reliable message after it has been acknowledged, either by
717 * direct mid ACK or bitfield. Updates the appropriate data structures and
718 * timers and frees all memory.
720 * @param copy Message that is no longer needed: remote peer got it.
721 * @param update_time Is the timing information relevant?
722 * If this message is ACK in a batch the timing information
723 * is skewed by the retransmission, count only for the
724 * retransmitted message.
727 rel_message_free (struct MeshReliableMessage *copy, int update_time)
729 struct MeshChannelReliability *rel;
730 struct GNUNET_TIME_Relative time;
733 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
736 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
737 if (0 == rel->expected_delay.rel_value_us)
738 rel->expected_delay = time;
741 rel->expected_delay.rel_value_us *= 7;
742 rel->expected_delay.rel_value_us += time.rel_value_us;
743 rel->expected_delay.rel_value_us /= 8;
745 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
746 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
747 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
748 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
750 rel->retry_timer = rel->expected_delay;
754 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! batch free, ignoring timing\n");
756 rel->ch->pending_messages--;
757 if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
759 struct MeshTunnel3 *t = rel->ch->t;
760 GMCH_destroy (rel->ch);
761 GMT_destroy_if_empty (t);
765 GMT_cancel (copy->q->q);
767 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
773 * Confirm we got a channel create.
775 * @param ch The channel to confirm.
776 * @param fwd Should we send a FWD ACK? (going dest->root)
779 channel_send_ack (struct MeshChannel *ch, int fwd)
781 struct GNUNET_MESH_ChannelManage msg;
783 msg.header.size = htons (sizeof (msg));
784 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
785 LOG (GNUNET_ERROR_TYPE_DEBUG,
786 " sending channel %s ack for channel %s\n",
787 GM_f2s (fwd), GMCH_2s (ch));
789 msg.chid = htonl (ch->gid);
790 GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
795 * Notify that a channel create didn't succeed.
797 * @param ch The channel to reject.
800 channel_send_nack (struct MeshChannel *ch)
802 struct GNUNET_MESH_ChannelManage msg;
804 msg.header.size = htons (sizeof (msg));
805 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK);
806 LOG (GNUNET_ERROR_TYPE_DEBUG,
807 " sending channel NACK for channel %s\n",
810 msg.chid = htonl (ch->gid);
811 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
816 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
818 * @param ch Channel to mark as ready.
819 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
822 channel_confirm (struct MeshChannel *ch, int fwd)
824 struct MeshChannelReliability *rel;
825 struct MeshReliableMessage *copy;
826 struct MeshReliableMessage *next;
828 LOG (GNUNET_ERROR_TYPE_DEBUG,
829 " channel confirm %s %s:%X\n",
830 GM_f2s (fwd), GMT_2s (ch->t), ch->gid);
831 ch->state = MESH_CHANNEL_READY;
833 rel = fwd ? ch->root_rel : ch->dest_rel;
834 rel->client_ready = GNUNET_YES;
835 for (copy = rel->head_sent; NULL != copy; copy = next)
837 struct GNUNET_MessageHeader *msg;
840 msg = (struct GNUNET_MessageHeader *) ©[1];
841 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
843 rel_message_free (copy, GNUNET_YES);
847 send_client_ack (ch, fwd);
849 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
851 channel_send_ack (ch, !fwd);
856 * Message has been sent: start retransmission timer.
858 * @param cls Closure (queue structure).
860 * @param q Queue handler (no longer valid).
861 * @param type Type of message.
862 * @param size Size of the message.
865 ch_message_sent (void *cls,
866 struct MeshTunnel3 *t,
867 struct MeshTunnel3Queue *q,
868 uint16_t type, size_t size)
870 struct MeshChannelQueue *ch_q = cls;
871 struct MeshReliableMessage *copy = ch_q->copy;
872 struct MeshChannelReliability *rel;
876 case GNUNET_MESSAGE_TYPE_MESH_DATA:
877 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT %u %s\n",
878 NULL != copy ? copy->mid : 0, GM_m2s (type));
879 copy->timestamp = GNUNET_TIME_absolute_get ();
881 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
883 if (0 != rel->expected_delay.rel_value_us)
886 GNUNET_TIME_relative_multiply (rel->expected_delay,
887 MESH_RETRANSMIT_MARGIN);
891 rel->retry_timer = MESH_RETRANSMIT_TIME;
894 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
895 &channel_retransmit_message,
902 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
904 GNUNET_assert (rel->ack_q == ch_q);
920 * Save a copy to retransmit in case it gets lost.
922 * Initializes all needed callbacks and timers.
924 * @param ch Channel this message goes on.
925 * @param msg Message to copy.
926 * @param fwd Is this fwd traffic?
928 static struct MeshReliableMessage *
929 channel_save_copy (struct MeshChannel *ch,
930 const struct GNUNET_MessageHeader *msg,
933 struct MeshChannelReliability *rel;
934 struct MeshReliableMessage *copy;
939 rel = fwd ? ch->root_rel : ch->dest_rel;
940 mid = rel->mid_send - 1;
941 type = ntohs (msg->type);
942 size = ntohs (msg->size);
944 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u %s\n", mid, GM_m2s (type));
945 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
949 memcpy (©[1], msg, size);
950 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
951 ch->pending_messages++;
958 * Create a new channel.
960 * @param t Tunnel this channel is in.
961 * @param owner Client that owns the channel, NULL for foreign channels.
962 * @param lid_root Local ID for root client.
964 * @return A new initialized channel. NULL on error.
966 static struct MeshChannel *
967 channel_new (struct MeshTunnel3 *t,
968 struct MeshClient *owner,
969 MESH_ChannelNumber lid_root)
971 struct MeshChannel *ch;
973 ch = GNUNET_new (struct MeshChannel);
975 ch->lid_root = lid_root;
978 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
982 ch->gid = GMT_get_next_chid (t);
983 GML_channel_add (owner, lid_root, ch);
985 GMT_add_channel (t, ch);
992 * Set options in a channel, extracted from a bit flag field
994 * @param ch Channel to set options to.
995 * @param options Bit array in host byte order.
998 channel_set_options (struct MeshChannel *ch, uint32_t options)
1000 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
1001 GNUNET_YES : GNUNET_NO;
1002 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
1003 GNUNET_YES : GNUNET_NO;
1007 is_loopback (const struct MeshChannel *ch)
1010 return GMT_is_loopback (ch->t);
1012 return (NULL != ch->root && NULL != ch->dest);
1017 * Handle a loopback message: call the appropriate handler for the message type.
1019 * @param ch Channel this message is on.
1020 * @param msgh Message header.
1021 * @param fwd Is this FWD traffic?
1024 handle_loopback (struct MeshChannel *ch,
1025 const struct GNUNET_MessageHeader *msgh,
1030 type = ntohs (msgh->type);
1031 LOG (GNUNET_ERROR_TYPE_DEBUG,
1032 "Loopback %s %s message!\n",
1033 GM_f2s (fwd), GM_m2s (type));
1037 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1038 /* Don't send hop ACK, wait for client to ACK */
1039 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
1042 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
1043 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1046 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1047 GMCH_handle_create (ch->t,
1048 (struct GNUNET_MESH_ChannelCreate *) msgh);
1051 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1052 GMCH_handle_ack (ch,
1053 (struct GNUNET_MESH_ChannelManage *) msgh,
1057 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1058 GMCH_handle_nack (ch);
1061 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1062 GMCH_handle_destroy (ch,
1063 (struct GNUNET_MESH_ChannelManage *) msgh,
1068 GNUNET_break_op (0);
1069 LOG (GNUNET_ERROR_TYPE_DEBUG,
1070 "end-to-end message not known (%u)\n",
1071 ntohs (msgh->type));
1077 /******************************************************************************/
1078 /******************************** API ***********************************/
1079 /******************************************************************************/
1082 * Destroy a channel and free all resources.
1084 * @param ch Channel to destroy.
1087 GMCH_destroy (struct MeshChannel *ch)
1089 struct MeshClient *c;
1094 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
1095 GMT_2s (ch->t), ch->gid);
1101 GML_channel_remove (c, ch->lid_root, ch);
1107 GML_channel_remove (c, ch->lid_dest, ch);
1110 channel_rel_free_all (ch->root_rel);
1111 channel_rel_free_all (ch->dest_rel);
1113 GMT_remove_channel (ch->t, ch);
1114 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
1123 * @param ch Channel.
1128 GMCH_get_id (const struct MeshChannel *ch)
1135 * Get the channel tunnel.
1137 * @param ch Channel to get the tunnel from.
1139 * @return tunnel of the channel.
1141 struct MeshTunnel3 *
1142 GMCH_get_tunnel (const struct MeshChannel *ch)
1149 * Get free buffer space towards the client on a specific channel.
1151 * @param ch Channel.
1152 * @param fwd Is query about FWD traffic?
1154 * @return Free buffer space [0 - 64]
1157 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
1159 struct MeshChannelReliability *rel;
1161 rel = fwd ? ch->dest_rel : ch->root_rel;
1163 /* If rel is NULL it means that the end is not yet created,
1164 * most probably is a loopback channel at the point of sending
1165 * the ChannelCreate to itself.
1170 return (64 - rel->n_recv);
1175 * Get flow control status of end point: is client allow to send?
1177 * @param ch Channel.
1178 * @param fwd Is query about FWD traffic? (Request root status).
1180 * @return #GNUNET_YES if client is allowed to send us data.
1183 GMCH_get_allowed (struct MeshChannel *ch, int fwd)
1185 struct MeshChannelReliability *rel;
1187 rel = fwd ? ch->root_rel : ch->dest_rel;
1191 /* Probably shutting down: root/dest NULL'ed to mark disconnection */
1192 GNUNET_break (GNUNET_NO != ch->destroy);
1196 return rel->client_allowed;
1201 * Is the root client for this channel on this peer?
1203 * @param ch Channel.
1204 * @param fwd Is this for fwd traffic?
1206 * @return #GNUNET_YES in case it is.
1209 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1211 struct MeshClient *c;
1213 c = fwd ? ch->root : ch->dest;
1219 * Is the destination client for this channel on this peer?
1221 * @param ch Channel.
1222 * @param fwd Is this for fwd traffic?
1224 * @return #GNUNET_YES in case it is.
1227 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1229 struct MeshClient *c;
1231 c = fwd ? ch->dest : ch->root;
1237 * Notify the destination client that a new incoming channel was created.
1239 * @param ch Channel that was created.
1242 GMCH_send_create (struct MeshChannel *ch)
1246 if (NULL == ch->dest)
1250 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1251 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1252 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1253 GMT_get_destination (ch->t));
1259 * Send an end-to-end ACK message for the most recent in-sequence payload.
1261 * If channel is not reliable, do nothing.
1263 * @param ch Channel this is about.
1264 * @param fwd Is for FWD traffic? (ACK dest->owner)
1267 GMCH_send_data_ack (struct MeshChannel *ch, int fwd)
1269 struct GNUNET_MESH_DataACK msg;
1270 struct MeshChannelReliability *rel;
1271 struct MeshReliableMessage *copy;
1276 if (GNUNET_NO == ch->reliable)
1280 rel = fwd ? ch->dest_rel : ch->root_rel;
1281 ack = rel->mid_recv - 1;
1282 LOG (GNUNET_ERROR_TYPE_DEBUG,
1283 " !! Send DATA_ACK for %u\n",
1286 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
1287 msg.header.size = htons (sizeof (msg));
1288 msg.chid = htonl (ch->gid);
1290 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1292 if (copy->type != GNUNET_MESSAGE_TYPE_MESH_DATA)
1294 LOG (GNUNET_ERROR_TYPE_DEBUG,
1295 "!! Type %s, expected DATA\n",
1296 GM_m2s (copy->type));
1299 if (copy->mid == ack + 1)
1304 delta = copy->mid - (ack + 1);
1307 mask = 0x1LL << delta;
1308 msg.futures |= mask;
1309 LOG (GNUNET_ERROR_TYPE_DEBUG,
1310 " !! setting bit for %u (delta %u) (%llX) -> %llX\n",
1311 copy->mid, delta, mask, msg.futures);
1313 msg.mid = htonl (ack);
1314 LOG (GNUNET_ERROR_TYPE_DEBUG,
1315 "!!! ACK for %u, futures %llX\n",
1318 GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1319 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1324 * Allow a client to send us more data, in case it was choked.
1326 * @param ch Channel.
1327 * @param fwd Is this about FWD traffic? (Root client).
1330 GMCH_allow_client (struct MeshChannel *ch, int fwd)
1332 struct MeshChannelReliability *rel;
1333 unsigned int buffer;
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1337 if (MESH_CHANNEL_READY != ch->state)
1339 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel not ready yet!\n");
1343 if (GNUNET_YES == ch->reliable)
1345 rel = fwd ? ch->root_rel : ch->dest_rel;
1348 GNUNET_break (GNUNET_NO != ch->destroy);
1351 if (NULL != rel->head_sent && 64 <= rel->mid_send - rel->head_sent->mid)
1353 LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
1358 if (is_loopback (ch))
1359 buffer = GMCH_get_buffer (ch, fwd);
1361 buffer = GMT_get_connections_buffer (ch->t);
1365 LOG (GNUNET_ERROR_TYPE_DEBUG, " no buffer space.\n");
1369 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space %u, allowing\n", buffer);
1370 send_client_ack (ch, fwd);
1377 * @param ch Channel.
1380 GMCH_debug (struct MeshChannel *ch)
1384 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1387 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1388 GMT_2s (ch->t), ch->gid, ch);
1389 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1390 ch->root, ch->root_rel);
1391 if (NULL != ch->root)
1393 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1394 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1395 ch->root_rel->client_ready ? "YES" : "NO");
1396 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1398 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1399 ch->dest, ch->dest_rel);
1400 if (NULL != ch->dest)
1402 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1403 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1404 ch->dest_rel->client_ready ? "YES" : "NO");
1405 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1411 * Handle an ACK given by a client.
1413 * Mark client as ready and send him any buffered data we could have for him.
1415 * @param ch Channel.
1416 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1419 GMCH_handle_local_ack (struct MeshChannel *ch, int fwd)
1421 struct MeshChannelReliability *rel;
1422 struct MeshClient *c;
1424 rel = fwd ? ch->dest_rel : ch->root_rel;
1425 c = fwd ? ch->dest : ch->root;
1427 rel->client_ready = GNUNET_YES;
1428 send_client_buffered_data (ch, c, fwd);
1429 if (is_loopback (ch))
1431 unsigned int buffer;
1433 buffer = GMCH_get_buffer (ch, fwd);
1435 GMCH_allow_client (ch, fwd);
1439 GMT_send_connection_acks (ch->t);
1444 * Handle data given by a client.
1446 * Check whether the client is allowed to send in this tunnel, save if channel
1447 * is reliable and send an ACK to the client if there is still buffer space
1450 * @param ch Channel.
1451 * @param c Client which sent the data.
1452 * @param message Message.
1453 * @param fwd Is this a FWD data?
1455 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1458 GMCH_handle_local_data (struct MeshChannel *ch,
1459 struct MeshClient *c,
1460 struct GNUNET_MessageHeader *message,
1463 struct MeshChannelReliability *rel;
1464 struct GNUNET_MESH_Data *payload;
1465 size_t size = ntohs (message->size);
1466 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
1467 unsigned char cbuf[p2p_size];
1469 /* Is the client in the channel? */
1476 GNUNET_break_op (0);
1477 return GNUNET_SYSERR;
1480 rel = fwd ? ch->root_rel : ch->dest_rel;
1482 if (GNUNET_NO == rel->client_allowed)
1484 GNUNET_break_op (0);
1485 return GNUNET_SYSERR;
1488 rel->client_allowed = GNUNET_NO;
1490 /* Ok, everything is correct, send the message. */
1491 payload = (struct GNUNET_MESH_Data *) cbuf;
1492 payload->mid = htonl (rel->mid_send);
1494 memcpy (&payload[1], message, size);
1495 payload->header.size = htons (p2p_size);
1496 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
1497 payload->chid = htonl (ch->gid);
1498 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1499 GMCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1501 if (is_loopback (ch))
1503 if (GMCH_get_buffer (ch, fwd) > 0)
1504 send_client_ack (ch, fwd);
1509 if (GMT_get_connections_buffer (ch->t) > 0)
1511 send_client_ack (ch, fwd);
1519 * Handle a channel destroy requested by a client.
1521 * Destroy the channel and the tunnel in case this was the last channel.
1523 * @param ch Channel.
1524 * @param c Client that requested the destruction (to avoid notifying him).
1525 * @param is_root Is the request coming from root?
1528 GMCH_handle_local_destroy (struct MeshChannel *ch,
1529 struct MeshClient *c,
1532 struct MeshTunnel3 *t;
1534 ch->destroy = GNUNET_YES;
1535 /* Cleanup after the tunnel */
1536 if (GNUNET_NO == is_root && c == ch->dest)
1538 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1539 GML_client_delete_channel (c, ch, ch->lid_dest);
1542 if (GNUNET_YES == is_root && c == ch->root)
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1545 GML_client_delete_channel (c, ch, ch->lid_root);
1550 send_destroy (ch, GNUNET_NO);
1551 if (0 == ch->pending_messages)
1554 GMT_destroy_if_empty (t);
1560 * Handle a channel create requested by a client.
1562 * Create the channel and the tunnel in case this was the first0 channel.
1564 * @param c Client that requested the creation (will be the root).
1565 * @param msg Create Channel message.
1567 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1570 GMCH_handle_local_create (struct MeshClient *c,
1571 struct GNUNET_MESH_ChannelMessage *msg)
1573 struct MeshChannel *ch;
1574 struct MeshTunnel3 *t;
1575 struct MeshPeer *peer;
1576 MESH_ChannelNumber chid;
1578 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1579 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1580 chid = ntohl (msg->channel_id);
1582 /* Sanity check for duplicate channel IDs */
1583 if (NULL != GML_channel_get (c, chid))
1586 return GNUNET_SYSERR;
1589 peer = GMP_get (&msg->peer);
1590 GMP_add_tunnel (peer);
1591 t = GMP_get_tunnel (peer);
1593 if (GMP_get_short_id (peer) == myid)
1595 GMT_change_cstate (t, MESH_TUNNEL3_READY);
1602 /* Create channel */
1603 ch = channel_new (t, c, chid);
1607 return GNUNET_SYSERR;
1609 ch->port = ntohl (msg->port);
1610 channel_set_options (ch, ntohl (msg->opt));
1612 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1613 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
1614 ch->root_rel->ch = ch;
1615 ch->root_rel->expected_delay.rel_value_us = 0;
1617 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
1619 /* Send create channel */
1621 struct GNUNET_MESH_ChannelCreate msgcc;
1623 msgcc.header.size = htons (sizeof (msgcc));
1624 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1625 msgcc.chid = htonl (ch->gid);
1626 msgcc.port = msg->port;
1627 msgcc.opt = msg->opt;
1629 /* FIXME retransmit if lost */
1630 GMT_send_prebuilt_message (&msgcc.header, t, ch,
1631 GNUNET_YES, GNUNET_YES, NULL, NULL);
1638 * Handler for mesh network payload traffic.
1640 * @param ch Channel for the message.
1641 * @param msg Unencryted data message.
1642 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1643 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1644 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1645 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1648 GMCH_handle_data (struct MeshChannel *ch,
1649 const struct GNUNET_MESH_Data *msg,
1652 struct MeshChannelReliability *rel;
1653 struct MeshClient *c;
1656 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1657 if (GNUNET_SYSERR == fwd)
1659 if (is_loopback (ch))
1661 /* It is a loopback channel after all... */
1665 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1668 /* Initialize FWD/BCK data */
1669 c = fwd ? ch->dest : ch->root;
1670 rel = fwd ? ch->dest_rel : ch->root_rel;
1678 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1680 mid = ntohl (msg->mid);
1681 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! got mid %u\n", mid);
1683 if (GNUNET_NO == ch->reliable ||
1684 ( !GM_is_pid_bigger (rel->mid_recv, mid) &&
1685 GM_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1687 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1688 if (GNUNET_YES == ch->reliable)
1690 /* Is this the exact next expected messasge? */
1691 if (mid == rel->mid_recv)
1693 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1695 send_client_data (ch, msg, fwd);
1699 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1700 add_buffered_data (msg, rel);
1705 /* Tunnel is unreliable: send to clients directly */
1706 /* FIXME: accept Out Of Order traffic */
1707 rel->mid_recv = mid + 1;
1708 send_client_data (ch, msg, fwd);
1713 GNUNET_break_op (0);
1714 LOG (GNUNET_ERROR_TYPE_DEBUG,
1715 " !!! MID %u not expected (%u - %u), dropping!\n",
1716 mid, rel->mid_recv, rel->mid_recv + 63);
1719 GMCH_send_data_ack (ch, fwd);
1724 * Handler for mesh network traffic end-to-end ACKs.
1726 * @param ch Channel on which we got this message.
1727 * @param msg Data message.
1728 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1729 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1730 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1731 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1734 GMCH_handle_data_ack (struct MeshChannel *ch,
1735 const struct GNUNET_MESH_DataACK *msg,
1738 struct MeshChannelReliability *rel;
1739 struct MeshReliableMessage *copy;
1740 struct MeshReliableMessage *next;
1744 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1745 if (GNUNET_SYSERR == fwd)
1747 if (is_loopback (ch))
1749 /* It is a loopback channel after all... */
1753 /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
1754 fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
1757 ack = ntohl (msg->mid);
1758 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1759 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1761 if (GNUNET_YES == fwd)
1771 GNUNET_break_op (0);
1775 /* Free ACK'd copies: no need to retransmit those anymore */
1776 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1778 if (GM_is_pid_bigger (copy->mid, ack))
1780 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1781 channel_rel_free_sent (rel, msg);
1785 LOG (GNUNET_ERROR_TYPE_DEBUG, " !! id %u\n", copy->mid);
1787 rel_message_free (copy, GNUNET_YES);
1790 /* ACK client if needed */
1791 GMCH_allow_client (ch, fwd);
1793 /* If some message was free'd, update the retransmission delay */
1794 if (GNUNET_YES == work)
1796 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1798 GNUNET_SCHEDULER_cancel (rel->retry_task);
1799 if (NULL == rel->head_sent)
1801 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1803 else if (NULL == rel->head_sent->q)
1805 struct GNUNET_TIME_Absolute new_target;
1806 struct GNUNET_TIME_Relative delay;
1808 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1809 MESH_RETRANSMIT_MARGIN);
1810 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1812 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1814 GNUNET_SCHEDULER_add_delayed (delay,
1815 &channel_retransmit_message,
1826 * Handler for channel create messages.
1828 * Does not have fwd parameter because it's always 'FWD': channel is incoming.
1830 * @param t Tunnel this channel will be in.
1831 * @param msg Channel crate message.
1833 struct MeshChannel *
1834 GMCH_handle_create (struct MeshTunnel3 *t,
1835 const struct GNUNET_MESH_ChannelCreate *msg)
1837 MESH_ChannelNumber chid;
1838 struct MeshChannel *ch;
1839 struct MeshClient *c;
1841 chid = ntohl (msg->chid);
1843 ch = GMT_get_channel (t, chid);
1846 /* Create channel */
1847 ch = channel_new (t, NULL, 0);
1850 channel_set_options (ch, ntohl (msg->opt));
1852 /* Find a destination client */
1853 ch->port = ntohl (msg->port);
1854 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", ch->port);
1855 c = GML_client_get_by_port (ch->port);
1858 /* TODO send reject */
1859 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
1860 if (is_loopback (ch))
1862 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback: destroy on handler\n");
1863 channel_send_nack (ch);
1867 LOG (GNUNET_ERROR_TYPE_DEBUG, " not loopback: destroy now\n");
1868 channel_send_nack (ch);
1875 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
1878 add_destination (ch, c);
1879 if (GNUNET_YES == ch->reliable)
1880 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1882 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
1884 GMCH_send_create (ch);
1885 channel_send_ack (ch, GNUNET_YES);
1892 * Handler for channel NACK messages.
1894 * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
1896 * @param ch Channel.
1899 GMCH_handle_nack (struct MeshChannel *ch)
1901 send_client_nack (ch);
1907 * Handler for channel ack messages.
1909 * @param ch Channel.
1910 * @param msg Message.
1911 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1912 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1913 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1914 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1917 GMCH_handle_ack (struct MeshChannel *ch,
1918 const struct GNUNET_MESH_ChannelManage *msg,
1921 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1922 if (GNUNET_SYSERR == fwd)
1924 if (is_loopback (ch))
1926 /* It is a loopback channel after all... */
1930 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1933 channel_confirm (ch, !fwd);
1938 * Handler for channel destroy messages.
1940 * @param ch Channel to be destroyed of.
1941 * @param msg Message.
1942 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1943 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1944 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1945 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1948 GMCH_handle_destroy (struct MeshChannel *ch,
1949 const struct GNUNET_MESH_ChannelManage *msg,
1952 struct MeshTunnel3 *t;
1954 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1955 if (GNUNET_SYSERR == fwd)
1957 if (is_loopback (ch))
1959 /* It is a loopback channel after all... */
1963 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1967 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
1969 /* Not for us (don't destroy twice a half-open loopback channel) */
1974 send_destroy (ch, GNUNET_YES);
1976 GMT_destroy_if_empty (t);
1981 * Sends an already built message on a channel.
1983 * If the channel is on a loopback tunnel, notifies the appropriate destination
1986 * On a normal channel passes the message to the tunnel for encryption and
1987 * sending on a connection.
1989 * This function DOES NOT save the message for retransmission.
1991 * @param message Message to send. Function makes a copy of it.
1992 * @param ch Channel on which this message is transmitted.
1993 * @param fwd Is this a fwd message?
1994 * @param existing_copy This is a retransmission, don't save a copy.
1997 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1998 struct MeshChannel *ch, int fwd,
1999 void *existing_copy)
2003 type = ntohs (message->type);
2004 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH Send %s %s on channel %s\n",
2005 GM_f2s (fwd), GM_m2s (type),
2008 if (GMT_is_loopback (ch->t))
2010 handle_loopback (ch, message, fwd);
2016 case GNUNET_MESSAGE_TYPE_MESH_DATA:
2018 if (GNUNET_YES == ch->reliable)
2020 struct MeshChannelQueue *q;
2022 q = GNUNET_new (struct MeshChannelQueue);
2024 if (NULL == existing_copy)
2025 q->copy = channel_save_copy (ch, message, fwd);
2028 q->copy = (struct MeshReliableMessage *) existing_copy;
2029 LOG (GNUNET_ERROR_TYPE_DEBUG,
2030 " ### using existing copy: %p {r:0x%p q:0x%p t:%u}\n",
2032 q->copy->rel, q->copy->q, q->copy->type);
2035 q->q = GMT_send_prebuilt_message (message, ch->t, ch,
2036 fwd, NULL != existing_copy,
2037 &ch_message_sent, q);
2038 /* Don't store q itself: we never need to cancel messages */
2042 GNUNET_break (NULL == GMT_send_prebuilt_message (message, ch->t, ch,
2048 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2050 struct MeshChannelReliability *rel;
2052 rel = fwd ? ch->root_rel : ch->dest_rel;
2053 if (NULL != rel->ack_q)
2055 GMT_cancel (rel->ack_q->q);
2056 /* ch_message_sent is called, freeing ack_q */
2058 rel->ack_q = GNUNET_new (struct MeshChannelQueue);
2059 rel->ack_q->type = type;
2060 rel->ack_q->rel = rel;
2061 rel->ack_q->q = GMT_send_prebuilt_message (message, ch->t, ch,
2063 &ch_message_sent, rel->ack_q);
2067 GNUNET_break (NULL == GMT_send_prebuilt_message (message, ch->t, ch,
2075 * Get the static string for identification of the channel.
2077 * @param ch Channel.
2079 * @return Static string with the channel IDs.
2082 GMCH_2s (const struct MeshChannel *ch)
2084 static char buf[64];
2087 return "(NULL Channel)";
2089 sprintf (buf, "%s:%u gid:%X (%X / %X)",
2090 GMT_2s (ch->t), ch->port, ch->gid, ch->lid_root, ch->lid_dest);