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 unique data CREATE, DATA_ACK.
170 struct MeshChannelQueue *uniq;
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 * send a channel create message.
324 * @param ch Channel for which to send.
327 send_create (struct MeshChannel *ch);
330 * Confirm we got a channel create, FWD ack.
332 * @param ch The channel to confirm.
333 * @param fwd Should we send a FWD ACK? (going dest->root)
336 send_ack (struct MeshChannel *ch, int fwd);
341 * We have received a message out of order, or the client is not ready.
342 * Buffer it until we receive an ACK from the client or the missing
343 * message from the channel.
345 * @param msg Message to buffer (MUST be of type MESH_DATA).
346 * @param rel Reliability data to the corresponding direction.
349 add_buffered_data (const struct GNUNET_MESH_Data *msg,
350 struct MeshChannelReliability *rel)
352 struct MeshReliableMessage *copy;
353 struct MeshReliableMessage *prev;
357 size = ntohs (msg->header.size);
358 mid = ntohl (msg->mid);
360 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
362 copy = GNUNET_malloc (sizeof (*copy) + size);
365 copy->type = GNUNET_MESSAGE_TYPE_MESH_DATA;
366 memcpy (©[1], msg, size);
370 // FIXME do something better than O(n), although n < 64...
371 // FIXME start from the end (most messages are the latest ones)
372 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
374 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
375 if (GM_is_pid_bigger (prev->mid, mid))
377 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
378 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
383 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
384 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
385 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
389 * Add a destination client to a channel, initializing all data structures
390 * in the channel and the client.
392 * @param ch Channel to which add the destination.
393 * @param c Client which to add to the channel.
396 add_destination (struct MeshChannel *ch, struct MeshClient *c)
398 if (NULL != ch->dest)
404 /* Assign local id as destination */
405 ch->lid_dest = GML_get_next_chid (c);
407 /* Store in client's hashmap */
408 GML_channel_add (c, ch->lid_dest, ch);
410 GNUNET_break (NULL == ch->dest_rel);
411 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
412 ch->dest_rel->ch = ch;
413 ch->dest_rel->expected_delay.rel_value_us = 0;
420 * Set options in a channel, extracted from a bit flag field.
422 * @param ch Channel to set options to.
423 * @param options Bit array in host byte order.
426 channel_set_options (struct MeshChannel *ch, uint32_t options)
428 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
429 GNUNET_YES : GNUNET_NO;
430 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
431 GNUNET_YES : GNUNET_NO;
436 * Get a bit flag field with the options of a channel.
438 * @param ch Channel to get options from.
440 * @return Bit array in host byte order.
443 channel_get_options (struct MeshChannel *ch)
449 options |= GNUNET_MESH_OPTION_NOBUFFER;
451 options |= GNUNET_MESH_OPTION_RELIABLE;
458 * Notify the destination client that a new incoming channel was created.
460 * @param ch Channel that was created.
463 send_client_create (struct MeshChannel *ch)
467 if (NULL == ch->dest)
471 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
472 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
473 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
474 GMT_get_destination (ch->t));
480 * Send data to a client.
482 * If the client is ready, send directly, otherwise buffer while listening
486 * @param msg Message.
487 * @param fwd Is this a fwd (root->dest) message?
490 send_client_data (struct MeshChannel *ch,
491 const struct GNUNET_MESH_Data *msg,
496 if (ch->dest_rel->client_ready)
497 GML_send_data (ch->dest, msg, ch->lid_dest);
499 add_buffered_data (msg, ch->dest_rel);
503 if (ch->root_rel->client_ready)
504 GML_send_data (ch->root, msg, ch->lid_root);
506 add_buffered_data (msg, ch->root_rel);
512 * Send a buffered message to the client, for in order delivery or
513 * as result of client ACK.
515 * @param ch Channel on which to empty the message buffer.
516 * @param c Client to send to.
517 * @param fwd Is this to send FWD data?.
520 send_client_buffered_data (struct MeshChannel *ch,
521 struct MeshClient *c,
524 struct MeshReliableMessage *copy;
525 struct MeshChannelReliability *rel;
527 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
528 rel = fwd ? ch->dest_rel : ch->root_rel;
529 if (GNUNET_NO == rel->client_ready)
531 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
535 copy = rel->head_recv;
536 /* We never buffer channel management messages */
539 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
541 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
543 LOG (GNUNET_ERROR_TYPE_DEBUG,
544 " have %u! now expecting %u\n",
545 copy->mid, rel->mid_recv + 1);
546 send_client_data (ch, msg, fwd);
549 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
550 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE RECV %p\n", copy);
555 LOG (GNUNET_ERROR_TYPE_DEBUG,
556 " reliable && don't have %u, next is %u\n",
562 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
567 * Allow a client to send more data.
569 * In case the client was already allowed to send data, do nothing.
572 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
575 send_client_ack (struct MeshChannel *ch, int fwd)
577 struct MeshChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 " sending %s ack to client on channel %s\n",
581 GM_f2s (fwd), GMCH_2s (ch));
589 if (GNUNET_YES == rel->client_allowed)
591 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
594 rel->client_allowed = GNUNET_YES;
596 GML_send_ack (fwd ? ch->root : ch->dest, fwd ? ch->lid_root : ch->lid_dest);
601 * Notify the root that the destination rejected the channel.
603 * @param ch Rejected channel.
606 send_client_nack (struct MeshChannel *ch)
608 if (NULL == ch->root)
613 GML_send_nack (ch->root, ch->lid_root);
618 * We haven't received an ACK after a certain time: restransmit the message.
620 * @param cls Closure (MeshChannelReliability with the message to restransmit)
621 * @param tc TaskContext.
624 channel_retransmit_message (void *cls,
625 const struct GNUNET_SCHEDULER_TaskContext *tc)
627 struct MeshChannelReliability *rel = cls;
628 struct MeshReliableMessage *copy;
629 struct MeshChannel *ch;
630 struct GNUNET_MESH_Data *payload;
633 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
634 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
638 copy = rel->head_sent;
645 payload = (struct GNUNET_MESH_Data *) ©[1];
646 fwd = (rel == ch->root_rel);
648 /* Message not found in the queue that we are going to use. */
649 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
651 GMCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
652 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
657 * We haven't received an Channel ACK after a certain time: resend the CREATE.
659 * @param cls Closure (MeshChannelReliability of the channel to recreate)
660 * @param tc TaskContext.
663 channel_recreate (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
665 struct MeshChannelReliability *rel = cls;
667 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
668 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
671 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RE-CREATE\n");
672 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
674 if (rel == rel->ch->root_rel)
676 send_create (rel->ch);
678 else if (rel == rel->ch->dest_rel)
680 send_ack (rel->ch, GNUNET_YES);
691 * Message has been sent: start retransmission timer.
693 * @param cls Closure (queue structure).
695 * @param q Queue handler (no longer valid).
696 * @param type Type of message.
697 * @param size Size of the message.
700 ch_message_sent (void *cls,
701 struct MeshTunnel3 *t,
702 struct MeshTunnel3Queue *q,
703 uint16_t type, size_t size)
705 struct MeshChannelQueue *ch_q = cls;
706 struct MeshReliableMessage *copy = ch_q->copy;
707 struct MeshChannelReliability *rel;
711 case GNUNET_MESSAGE_TYPE_MESH_DATA:
712 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT %u %s (c: %p, q: %p)\n",
713 copy->mid, GM_m2s (type), copy, copy->q);
714 GNUNET_assert (ch_q == copy->q);
715 copy->timestamp = GNUNET_TIME_absolute_get ();
717 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
719 if (0 != rel->expected_delay.rel_value_us)
722 GNUNET_TIME_relative_multiply (rel->expected_delay,
723 MESH_RETRANSMIT_MARGIN);
727 rel->retry_timer = MESH_RETRANSMIT_TIME;
730 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
731 &channel_retransmit_message, rel);
737 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
738 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
739 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
740 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT %s\n", GM_m2s (type));
742 GNUNET_assert (rel->uniq == ch_q);
745 if (MESH_CHANNEL_READY != rel->ch->state
746 && GNUNET_MESSAGE_TYPE_MESH_DATA_ACK != type)
748 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rel->retry_task);
749 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
750 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
751 &channel_recreate, rel);
765 * send a channel create message.
767 * @param ch Channel for which to send.
770 send_create (struct MeshChannel *ch)
772 struct GNUNET_MESH_ChannelCreate msgcc;
774 msgcc.header.size = htons (sizeof (msgcc));
775 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
776 msgcc.chid = htonl (ch->gid);
777 msgcc.port = htonl (ch->port);
778 msgcc.opt = htonl (channel_get_options (ch));
780 GMCH_send_prebuilt_message (&msgcc.header, ch, GNUNET_YES, NULL);
785 * Confirm we got a channel create, FWD ack.
787 * @param ch The channel to confirm.
788 * @param fwd Should we send a FWD ACK? (going dest->root)
791 send_ack (struct MeshChannel *ch, int fwd)
793 struct GNUNET_MESH_ChannelManage msg;
795 msg.header.size = htons (sizeof (msg));
796 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
797 LOG (GNUNET_ERROR_TYPE_DEBUG,
798 " sending channel %s ack for channel %s\n",
799 GM_f2s (fwd), GMCH_2s (ch));
801 msg.chid = htonl (ch->gid);
802 GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
807 * Notify that a channel create didn't succeed.
809 * @param ch The channel to reject.
812 send_nack (struct MeshChannel *ch)
814 struct GNUNET_MESH_ChannelManage msg;
816 msg.header.size = htons (sizeof (msg));
817 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK);
818 LOG (GNUNET_ERROR_TYPE_DEBUG,
819 " sending channel NACK for channel %s\n",
822 msg.chid = htonl (ch->gid);
823 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
828 * Notify a client that the channel is no longer valid.
830 * @param ch Channel that is destroyed.
831 * @param local_only Should we avoid sending it to other peers?
834 send_destroy (struct MeshChannel *ch, int local_only)
836 struct GNUNET_MESH_ChannelManage msg;
838 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
839 msg.header.size = htons (sizeof (msg));
840 msg.chid = htonl (ch->gid);
842 /* If root is not NULL, notify.
843 * If it's NULL, check lid_root. When a local destroy comes in, root
844 * is set to NULL but lid_root is left untouched. In this case, do nothing,
845 * the client is the one who reuqested the channel to be destroyed.
847 if (NULL != ch->root)
848 GML_send_channel_destroy (ch->root, ch->lid_root);
849 else if (0 == ch->lid_root && GNUNET_NO == local_only)
850 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
852 if (NULL != ch->dest)
853 GML_send_channel_destroy (ch->dest, ch->lid_dest);
854 else if (0 == ch->lid_dest && GNUNET_NO == local_only)
855 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
860 * Destroy all reliable messages queued for a channel,
861 * during a channel destruction.
862 * Frees the reliability structure itself.
864 * @param rel Reliability data for a channel.
867 channel_rel_free_all (struct MeshChannelReliability *rel)
869 struct MeshReliableMessage *copy;
870 struct MeshReliableMessage *next;
875 for (copy = rel->head_recv; NULL != copy; copy = next)
878 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
879 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH RECV %p\n", copy);
882 for (copy = rel->head_sent; NULL != copy; copy = next)
885 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
886 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH %p\n", copy);
889 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
891 GNUNET_SCHEDULER_cancel (rel->retry_task);
893 if (NULL != rel->uniq)
894 GMT_cancel (rel->uniq->q);
900 * Mark future messages as ACK'd.
902 * @param rel Reliability data.
903 * @param msg DataACK message with a bitfield of future ACK'd messages.
906 channel_rel_free_sent (struct MeshChannelReliability *rel,
907 const struct GNUNET_MESH_DataACK *msg)
909 struct MeshReliableMessage *copy;
910 struct MeshReliableMessage *next;
917 bitfield = msg->futures;
918 mid = ntohl (msg->mid);
919 LOG (GNUNET_ERROR_TYPE_DEBUG,
920 "!!! free_sent_reliable %u %llX\n",
922 LOG (GNUNET_ERROR_TYPE_DEBUG,
923 " rel %p, head %p\n",
924 rel, rel->head_sent);
925 for (i = 0, copy = rel->head_sent;
926 i < 64 && NULL != copy && 0 != bitfield;
929 LOG (GNUNET_ERROR_TYPE_DEBUG,
930 " trying bit %u (mid %u)\n",
933 if (0 == (bitfield & mask))
936 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
937 /* Bit was set, clear the bit from the bitfield */
940 /* The i-th bit was set. Do we have that copy? */
941 /* Skip copies with mid < target */
942 target = mid + i + 1;
943 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
944 while (NULL != copy && GM_is_pid_bigger (target, copy->mid))
947 /* Did we run out of copies? (previously freed, it's ok) */
950 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
954 /* Did we overshoot the target? (previously freed, it's ok) */
955 if (GM_is_pid_bigger (copy->mid, target))
957 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
961 /* Now copy->mid == target, free it */
963 rel_message_free (copy, GNUNET_YES);
966 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
971 * Destroy a reliable message after it has been acknowledged, either by
972 * direct mid ACK or bitfield. Updates the appropriate data structures and
973 * timers and frees all memory.
975 * @param copy Message that is no longer needed: remote peer got it.
976 * @param update_time Is the timing information relevant?
977 * If this message is ACK in a batch the timing information
978 * is skewed by the retransmission, count only for the
979 * retransmitted message.
982 rel_message_free (struct MeshReliableMessage *copy, int update_time)
984 struct MeshChannelReliability *rel;
985 struct GNUNET_TIME_Relative time;
988 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
991 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
992 if (0 == rel->expected_delay.rel_value_us)
993 rel->expected_delay = time;
996 rel->expected_delay.rel_value_us *= 7;
997 rel->expected_delay.rel_value_us += time.rel_value_us;
998 rel->expected_delay.rel_value_us /= 8;
1000 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
1001 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
1002 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
1003 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
1005 rel->retry_timer = rel->expected_delay;
1009 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! batch free, ignoring timing\n");
1011 rel->ch->pending_messages--;
1012 if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
1014 struct MeshTunnel3 *t = rel->ch->t;
1015 GMCH_destroy (rel->ch);
1016 GMT_destroy_if_empty (t);
1018 if (NULL != copy->q)
1020 GMT_cancel (copy->q->q);
1022 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
1023 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE %p\n", copy);
1029 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
1031 * @param ch Channel to mark as ready.
1032 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
1035 channel_confirm (struct MeshChannel *ch, int fwd)
1037 struct MeshChannelReliability *rel;
1039 LOG (GNUNET_ERROR_TYPE_DEBUG,
1040 " channel confirm %s %s:%X\n",
1041 GM_f2s (fwd), GMT_2s (ch->t), ch->gid);
1042 ch->state = MESH_CHANNEL_READY;
1044 rel = fwd ? ch->root_rel : ch->dest_rel;
1045 rel->client_ready = GNUNET_YES;
1046 rel->expected_delay = rel->retry_timer;
1047 send_client_ack (ch, fwd);
1049 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1051 GNUNET_SCHEDULER_cancel (rel->retry_task);
1052 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1054 else if (NULL != rel->uniq)
1056 GMT_cancel (rel->uniq->q);
1057 /* ch_sent_message will free and NULL uniq */
1061 /* We SHOULD have been trying to retransmit this! */
1065 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
1066 if (GNUNET_YES == fwd)
1067 send_ack (ch, GNUNET_NO);
1072 * Save a copy to retransmit in case it gets lost.
1074 * Initializes all needed callbacks and timers.
1076 * @param ch Channel this message goes on.
1077 * @param msg Message to copy.
1078 * @param fwd Is this fwd traffic?
1080 static struct MeshReliableMessage *
1081 channel_save_copy (struct MeshChannel *ch,
1082 const struct GNUNET_MessageHeader *msg,
1085 struct MeshChannelReliability *rel;
1086 struct MeshReliableMessage *copy;
1091 rel = fwd ? ch->root_rel : ch->dest_rel;
1092 mid = rel->mid_send - 1;
1093 type = ntohs (msg->type);
1094 size = ntohs (msg->size);
1096 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u %s\n", mid, GM_m2s (type));
1097 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", copy);
1102 memcpy (©[1], msg, size);
1103 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
1104 ch->pending_messages++;
1111 * Create a new channel.
1113 * @param t Tunnel this channel is in.
1114 * @param owner Client that owns the channel, NULL for foreign channels.
1115 * @param lid_root Local ID for root client.
1117 * @return A new initialized channel. NULL on error.
1119 static struct MeshChannel *
1120 channel_new (struct MeshTunnel3 *t,
1121 struct MeshClient *owner,
1122 MESH_ChannelNumber lid_root)
1124 struct MeshChannel *ch;
1126 ch = GNUNET_new (struct MeshChannel);
1128 ch->lid_root = lid_root;
1131 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
1135 ch->gid = GMT_get_next_chid (t);
1136 GML_channel_add (owner, lid_root, ch);
1138 GMT_add_channel (t, ch);
1145 * Test if the channel is loopback: both root and dest are on the local peer.
1147 * @param ch Channel to test.
1149 * @return #GNUNET_YES if channel is loopback, #NGUNET_NO otherwise.
1152 is_loopback (const struct MeshChannel *ch)
1155 return GMT_is_loopback (ch->t);
1157 return (NULL != ch->root && NULL != ch->dest);
1162 * Handle a loopback message: call the appropriate handler for the message type.
1164 * @param ch Channel this message is on.
1165 * @param msgh Message header.
1166 * @param fwd Is this FWD traffic?
1169 handle_loopback (struct MeshChannel *ch,
1170 const struct GNUNET_MessageHeader *msgh,
1175 type = ntohs (msgh->type);
1176 LOG (GNUNET_ERROR_TYPE_DEBUG,
1177 "Loopback %s %s message!\n",
1178 GM_f2s (fwd), GM_m2s (type));
1182 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1183 /* Don't send hop ACK, wait for client to ACK */
1184 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
1187 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
1188 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1191 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1192 GMCH_handle_create (ch->t,
1193 (struct GNUNET_MESH_ChannelCreate *) msgh);
1196 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1197 GMCH_handle_ack (ch,
1198 (struct GNUNET_MESH_ChannelManage *) msgh,
1202 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1203 GMCH_handle_nack (ch);
1206 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1207 GMCH_handle_destroy (ch,
1208 (struct GNUNET_MESH_ChannelManage *) msgh,
1213 GNUNET_break_op (0);
1214 LOG (GNUNET_ERROR_TYPE_DEBUG,
1215 "end-to-end message not known (%u)\n",
1216 ntohs (msgh->type));
1222 /******************************************************************************/
1223 /******************************** API ***********************************/
1224 /******************************************************************************/
1227 * Destroy a channel and free all resources.
1229 * @param ch Channel to destroy.
1232 GMCH_destroy (struct MeshChannel *ch)
1234 struct MeshClient *c;
1239 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
1240 GMT_2s (ch->t), ch->gid);
1246 GML_channel_remove (c, ch->lid_root, ch);
1252 GML_channel_remove (c, ch->lid_dest, ch);
1255 channel_rel_free_all (ch->root_rel);
1256 channel_rel_free_all (ch->dest_rel);
1258 GMT_remove_channel (ch->t, ch);
1259 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
1268 * @param ch Channel.
1273 GMCH_get_id (const struct MeshChannel *ch)
1280 * Get the channel tunnel.
1282 * @param ch Channel to get the tunnel from.
1284 * @return tunnel of the channel.
1286 struct MeshTunnel3 *
1287 GMCH_get_tunnel (const struct MeshChannel *ch)
1294 * Get free buffer space towards the client on a specific channel.
1296 * @param ch Channel.
1297 * @param fwd Is query about FWD traffic?
1299 * @return Free buffer space [0 - 64]
1302 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
1304 struct MeshChannelReliability *rel;
1306 rel = fwd ? ch->dest_rel : ch->root_rel;
1308 /* If rel is NULL it means that the end is not yet created,
1309 * most probably is a loopback channel at the point of sending
1310 * the ChannelCreate to itself.
1315 return (64 - rel->n_recv);
1320 * Get flow control status of end point: is client allow to send?
1322 * @param ch Channel.
1323 * @param fwd Is query about FWD traffic? (Request root status).
1325 * @return #GNUNET_YES if client is allowed to send us data.
1328 GMCH_get_allowed (struct MeshChannel *ch, int fwd)
1330 struct MeshChannelReliability *rel;
1332 rel = fwd ? ch->root_rel : ch->dest_rel;
1336 /* Probably shutting down: root/dest NULL'ed to mark disconnection */
1337 GNUNET_break (GNUNET_NO != ch->destroy);
1341 return rel->client_allowed;
1346 * Is the root client for this channel on this peer?
1348 * @param ch Channel.
1349 * @param fwd Is this for fwd traffic?
1351 * @return #GNUNET_YES in case it is.
1354 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1356 struct MeshClient *c;
1358 c = fwd ? ch->root : ch->dest;
1364 * Is the destination client for this channel on this peer?
1366 * @param ch Channel.
1367 * @param fwd Is this for fwd traffic?
1369 * @return #GNUNET_YES in case it is.
1372 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1374 struct MeshClient *c;
1376 c = fwd ? ch->dest : ch->root;
1382 * Send an end-to-end ACK message for the most recent in-sequence payload.
1384 * If channel is not reliable, do nothing.
1386 * @param ch Channel this is about.
1387 * @param fwd Is for FWD traffic? (ACK dest->owner)
1390 GMCH_send_data_ack (struct MeshChannel *ch, int fwd)
1392 struct GNUNET_MESH_DataACK msg;
1393 struct MeshChannelReliability *rel;
1394 struct MeshReliableMessage *copy;
1399 if (GNUNET_NO == ch->reliable)
1403 rel = fwd ? ch->dest_rel : ch->root_rel;
1404 ack = rel->mid_recv - 1;
1405 LOG (GNUNET_ERROR_TYPE_DEBUG,
1406 " !! Send DATA_ACK for %u\n",
1409 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
1410 msg.header.size = htons (sizeof (msg));
1411 msg.chid = htonl (ch->gid);
1413 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1415 if (copy->type != GNUNET_MESSAGE_TYPE_MESH_DATA)
1417 LOG (GNUNET_ERROR_TYPE_DEBUG,
1418 "!! Type %s, expected DATA\n",
1419 GM_m2s (copy->type));
1422 if (copy->mid == ack + 1)
1427 delta = copy->mid - (ack + 1);
1430 mask = 0x1LL << delta;
1431 msg.futures |= mask;
1432 LOG (GNUNET_ERROR_TYPE_DEBUG,
1433 " !! setting bit for %u (delta %u) (%llX) -> %llX\n",
1434 copy->mid, delta, mask, msg.futures);
1436 msg.mid = htonl (ack);
1437 LOG (GNUNET_ERROR_TYPE_DEBUG,
1438 "!!! ACK for %u, futures %llX\n",
1441 GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1442 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1447 * Allow a client to send us more data, in case it was choked.
1449 * @param ch Channel.
1450 * @param fwd Is this about FWD traffic? (Root client).
1453 GMCH_allow_client (struct MeshChannel *ch, int fwd)
1455 struct MeshChannelReliability *rel;
1456 unsigned int buffer;
1458 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1460 if (MESH_CHANNEL_READY != ch->state)
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel not ready yet!\n");
1466 if (GNUNET_YES == ch->reliable)
1468 rel = fwd ? ch->root_rel : ch->dest_rel;
1471 GNUNET_break (GNUNET_NO != ch->destroy);
1474 if (NULL != rel->head_sent && 64 <= rel->mid_send - rel->head_sent->mid)
1476 LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
1481 if (is_loopback (ch))
1482 buffer = GMCH_get_buffer (ch, fwd);
1484 buffer = GMT_get_connections_buffer (ch->t);
1488 LOG (GNUNET_ERROR_TYPE_DEBUG, " no buffer space.\n");
1492 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space %u, allowing\n", buffer);
1493 send_client_ack (ch, fwd);
1500 * @param ch Channel.
1503 GMCH_debug (struct MeshChannel *ch)
1507 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1510 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1511 GMT_2s (ch->t), ch->gid, ch);
1512 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1513 ch->root, ch->root_rel);
1514 if (NULL != ch->root)
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1517 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1518 ch->root_rel->client_ready ? "YES" : "NO");
1519 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1521 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1522 ch->dest, ch->dest_rel);
1523 if (NULL != ch->dest)
1525 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1526 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1527 ch->dest_rel->client_ready ? "YES" : "NO");
1528 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1534 * Handle an ACK given by a client.
1536 * Mark client as ready and send him any buffered data we could have for him.
1538 * @param ch Channel.
1539 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1542 GMCH_handle_local_ack (struct MeshChannel *ch, int fwd)
1544 struct MeshChannelReliability *rel;
1545 struct MeshClient *c;
1547 rel = fwd ? ch->dest_rel : ch->root_rel;
1548 c = fwd ? ch->dest : ch->root;
1550 rel->client_ready = GNUNET_YES;
1551 send_client_buffered_data (ch, c, fwd);
1552 if (is_loopback (ch))
1554 unsigned int buffer;
1556 buffer = GMCH_get_buffer (ch, fwd);
1558 GMCH_allow_client (ch, fwd);
1562 GMT_send_connection_acks (ch->t);
1567 * Handle data given by a client.
1569 * Check whether the client is allowed to send in this tunnel, save if channel
1570 * is reliable and send an ACK to the client if there is still buffer space
1573 * @param ch Channel.
1574 * @param c Client which sent the data.
1575 * @param message Message.
1576 * @param fwd Is this a FWD data?
1578 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1581 GMCH_handle_local_data (struct MeshChannel *ch,
1582 struct MeshClient *c,
1583 struct GNUNET_MessageHeader *message,
1586 struct MeshChannelReliability *rel;
1587 struct GNUNET_MESH_Data *payload;
1588 size_t size = ntohs (message->size);
1589 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
1590 unsigned char cbuf[p2p_size];
1592 /* Is the client in the channel? */
1599 GNUNET_break_op (0);
1600 return GNUNET_SYSERR;
1603 rel = fwd ? ch->root_rel : ch->dest_rel;
1605 if (GNUNET_NO == rel->client_allowed)
1607 GNUNET_break_op (0);
1608 return GNUNET_SYSERR;
1611 rel->client_allowed = GNUNET_NO;
1613 /* Ok, everything is correct, send the message. */
1614 payload = (struct GNUNET_MESH_Data *) cbuf;
1615 payload->mid = htonl (rel->mid_send);
1617 memcpy (&payload[1], message, size);
1618 payload->header.size = htons (p2p_size);
1619 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
1620 payload->chid = htonl (ch->gid);
1621 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1622 GMCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1624 if (is_loopback (ch))
1626 if (GMCH_get_buffer (ch, fwd) > 0)
1627 send_client_ack (ch, fwd);
1632 if (GMT_get_connections_buffer (ch->t) > 0)
1634 send_client_ack (ch, fwd);
1642 * Handle a channel destroy requested by a client.
1644 * Destroy the channel and the tunnel in case this was the last channel.
1646 * @param ch Channel.
1647 * @param c Client that requested the destruction (to avoid notifying him).
1648 * @param is_root Is the request coming from root?
1651 GMCH_handle_local_destroy (struct MeshChannel *ch,
1652 struct MeshClient *c,
1655 struct MeshTunnel3 *t;
1657 ch->destroy = GNUNET_YES;
1658 /* Cleanup after the tunnel */
1659 if (GNUNET_NO == is_root && c == ch->dest)
1661 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1662 GML_client_delete_channel (c, ch, ch->lid_dest);
1665 if (GNUNET_YES == is_root && c == ch->root)
1667 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1668 GML_client_delete_channel (c, ch, ch->lid_root);
1673 send_destroy (ch, GNUNET_NO);
1674 if (0 == ch->pending_messages)
1677 GMT_destroy_if_empty (t);
1683 * Handle a channel create requested by a client.
1685 * Create the channel and the tunnel in case this was the first0 channel.
1687 * @param c Client that requested the creation (will be the root).
1688 * @param msg Create Channel message.
1690 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1693 GMCH_handle_local_create (struct MeshClient *c,
1694 struct GNUNET_MESH_ChannelMessage *msg)
1696 struct MeshChannel *ch;
1697 struct MeshTunnel3 *t;
1698 struct MeshPeer *peer;
1699 MESH_ChannelNumber chid;
1701 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1702 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1703 chid = ntohl (msg->channel_id);
1705 /* Sanity check for duplicate channel IDs */
1706 if (NULL != GML_channel_get (c, chid))
1709 return GNUNET_SYSERR;
1712 peer = GMP_get (&msg->peer);
1713 GMP_add_tunnel (peer);
1714 t = GMP_get_tunnel (peer);
1716 if (GMP_get_short_id (peer) == myid)
1718 GMT_change_cstate (t, MESH_TUNNEL3_READY);
1725 /* Create channel */
1726 ch = channel_new (t, c, chid);
1730 return GNUNET_SYSERR;
1732 ch->port = ntohl (msg->port);
1733 channel_set_options (ch, ntohl (msg->opt));
1735 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1736 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
1737 ch->root_rel->ch = ch;
1738 ch->root_rel->retry_timer = GNUNET_TIME_UNIT_SECONDS;
1739 ch->root_rel->expected_delay.rel_value_us = 0;
1741 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
1750 * Handler for mesh network payload traffic.
1752 * @param ch Channel for the message.
1753 * @param msg Unencryted data message.
1754 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1755 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1756 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1757 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1760 GMCH_handle_data (struct MeshChannel *ch,
1761 const struct GNUNET_MESH_Data *msg,
1764 struct MeshChannelReliability *rel;
1765 struct MeshClient *c;
1768 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1769 if (GNUNET_SYSERR == fwd)
1771 if (is_loopback (ch))
1773 /* It is a loopback channel after all... */
1777 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1780 /* Initialize FWD/BCK data */
1781 c = fwd ? ch->dest : ch->root;
1782 rel = fwd ? ch->dest_rel : ch->root_rel;
1790 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1792 mid = ntohl (msg->mid);
1793 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! got mid %u\n", mid);
1795 if (GNUNET_NO == ch->reliable ||
1796 ( !GM_is_pid_bigger (rel->mid_recv, mid) &&
1797 GM_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1799 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1800 if (GNUNET_YES == ch->reliable)
1802 /* Is this the exact next expected messasge? */
1803 if (mid == rel->mid_recv)
1805 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1807 send_client_data (ch, msg, fwd);
1811 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1812 add_buffered_data (msg, rel);
1817 /* Tunnel is unreliable: send to clients directly */
1818 /* FIXME: accept Out Of Order traffic */
1819 rel->mid_recv = mid + 1;
1820 send_client_data (ch, msg, fwd);
1825 GNUNET_break_op (GM_is_pid_bigger (rel->mid_recv, mid));
1826 LOG (GNUNET_ERROR_TYPE_DEBUG,
1827 " !!! MID %u not expected (%u - %u), dropping!\n",
1828 mid, rel->mid_recv, rel->mid_recv + 63);
1831 GMCH_send_data_ack (ch, fwd);
1836 * Handler for mesh network traffic end-to-end ACKs.
1838 * @param ch Channel on which we got this message.
1839 * @param msg Data message.
1840 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1841 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1842 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1843 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1846 GMCH_handle_data_ack (struct MeshChannel *ch,
1847 const struct GNUNET_MESH_DataACK *msg,
1850 struct MeshChannelReliability *rel;
1851 struct MeshReliableMessage *copy;
1852 struct MeshReliableMessage *next;
1856 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1857 if (GNUNET_SYSERR == fwd)
1859 if (is_loopback (ch))
1861 /* It is a loopback channel after all... */
1865 /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
1866 fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
1869 ack = ntohl (msg->mid);
1870 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1871 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1873 if (GNUNET_YES == fwd)
1883 GNUNET_break_op (0);
1887 /* Free ACK'd copies: no need to retransmit those anymore */
1888 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1890 if (GM_is_pid_bigger (copy->mid, ack))
1892 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1893 channel_rel_free_sent (rel, msg);
1897 LOG (GNUNET_ERROR_TYPE_DEBUG, " !! id %u\n", copy->mid);
1899 rel_message_free (copy, GNUNET_YES);
1902 /* ACK client if needed */
1903 GMCH_allow_client (ch, fwd);
1905 /* If some message was free'd, update the retransmission delay */
1906 if (GNUNET_YES == work)
1908 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1910 GNUNET_SCHEDULER_cancel (rel->retry_task);
1911 if (NULL != rel->head_sent && NULL == rel->head_sent->q)
1913 struct GNUNET_TIME_Absolute new_target;
1914 struct GNUNET_TIME_Relative delay;
1916 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1917 MESH_RETRANSMIT_MARGIN);
1918 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1920 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1922 GNUNET_SCHEDULER_add_delayed (delay,
1923 &channel_retransmit_message,
1926 else /* either no more traffic to ack or traffic has just been queued */
1928 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1931 else /* work was done but no task was pending? shouldn't happen! */
1940 * Handler for channel create messages.
1942 * Does not have fwd parameter because it's always 'FWD': channel is incoming.
1944 * @param t Tunnel this channel will be in.
1945 * @param msg Channel crate message.
1947 struct MeshChannel *
1948 GMCH_handle_create (struct MeshTunnel3 *t,
1949 const struct GNUNET_MESH_ChannelCreate *msg)
1951 MESH_ChannelNumber chid;
1952 struct MeshChannel *ch;
1953 struct MeshClient *c;
1955 chid = ntohl (msg->chid);
1957 ch = GMT_get_channel (t, chid);
1960 /* Create channel */
1961 ch = channel_new (t, NULL, 0);
1964 channel_set_options (ch, ntohl (msg->opt));
1966 /* Find a destination client */
1967 ch->port = ntohl (msg->port);
1968 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", ch->port);
1969 c = GML_client_get_by_port (ch->port);
1972 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
1973 if (is_loopback (ch))
1975 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback: destroy on handler\n");
1980 LOG (GNUNET_ERROR_TYPE_DEBUG, " not loopback: destroy now\n");
1988 LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
1991 add_destination (ch, c);
1992 if (GNUNET_YES == ch->reliable)
1993 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1995 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
1997 send_client_create (ch);
1998 send_ack (ch, GNUNET_YES);
2005 * Handler for channel NACK messages.
2007 * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
2009 * @param ch Channel.
2012 GMCH_handle_nack (struct MeshChannel *ch)
2014 send_client_nack (ch);
2020 * Handler for channel ack messages.
2022 * @param ch Channel.
2023 * @param msg Message.
2024 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2025 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2026 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2027 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2030 GMCH_handle_ack (struct MeshChannel *ch,
2031 const struct GNUNET_MESH_ChannelManage *msg,
2034 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2035 if (GNUNET_SYSERR == fwd)
2037 if (is_loopback (ch))
2039 /* It is a loopback channel after all... */
2043 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2046 channel_confirm (ch, !fwd);
2051 * Handler for channel destroy messages.
2053 * @param ch Channel to be destroyed of.
2054 * @param msg Message.
2055 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2056 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2057 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2058 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2061 GMCH_handle_destroy (struct MeshChannel *ch,
2062 const struct GNUNET_MESH_ChannelManage *msg,
2065 struct MeshTunnel3 *t;
2067 /* If this is a remote (non-loopback) channel, find 'fwd'. */
2068 if (GNUNET_SYSERR == fwd)
2070 if (is_loopback (ch))
2072 /* It is a loopback channel after all... */
2076 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2080 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
2082 /* Not for us (don't destroy twice a half-open loopback channel) */
2087 send_destroy (ch, GNUNET_YES);
2089 GMT_destroy_if_empty (t);
2094 * Sends an already built message on a channel.
2096 * If the channel is on a loopback tunnel, notifies the appropriate destination
2099 * On a normal channel passes the message to the tunnel for encryption and
2100 * sending on a connection.
2102 * This function DOES NOT save the message for retransmission.
2104 * @param message Message to send. Function makes a copy of it.
2105 * @param ch Channel on which this message is transmitted.
2106 * @param fwd Is this a fwd message?
2107 * @param existing_copy This is a retransmission, don't save a copy.
2110 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2111 struct MeshChannel *ch, int fwd,
2112 void *existing_copy)
2114 struct MeshChannelQueue *q;
2117 type = ntohs (message->type);
2118 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH Send %s %s on channel %s\n",
2119 GM_f2s (fwd), GM_m2s (type),
2122 if (GMT_is_loopback (ch->t))
2124 handle_loopback (ch, message, fwd);
2130 case GNUNET_MESSAGE_TYPE_MESH_DATA:
2132 if (GNUNET_YES == ch->reliable)
2134 q = GNUNET_new (struct MeshChannelQueue);
2136 if (NULL == existing_copy)
2137 q->copy = channel_save_copy (ch, message, fwd);
2140 q->copy = (struct MeshReliableMessage *) existing_copy;
2141 if (NULL != q->copy->q)
2143 /* Last retransmission was queued but not yet sent!
2144 * This retransmission was scheduled by a ch_message_sent which
2145 * followed a very fast RTT, so the tiny delay made the
2146 * retransmission function to execute before the previous
2147 * retransmitted message even had a chance to leave the peer.
2148 * Cancel this message and wait until the pending
2149 * retransmission leaves the peer and ch_message_sent starts
2150 * the timer for the next one.
2153 LOG (GNUNET_ERROR_TYPE_DEBUG,
2154 " exisitng copy not yet transmitted!\n");
2157 LOG (GNUNET_ERROR_TYPE_DEBUG,
2158 " using existing copy: %p {r:%p q:%p t:%u}\n",
2160 q->copy->rel, q->copy->q, q->copy->type);
2162 LOG (GNUNET_ERROR_TYPE_DEBUG, " new q: %p\n", q);
2164 q->q = GMT_send_prebuilt_message (message, ch->t, ch,
2165 fwd, NULL != existing_copy,
2166 &ch_message_sent, q);
2167 /* q itself is stored in copy */
2171 goto fire_and_forget;
2176 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
2177 if (GNUNET_YES == fwd)
2179 /* BCK ACK (going FWD) is just a response for a SYNACK, don't keep*/
2180 goto fire_and_forget;
2183 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2184 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
2185 q = GNUNET_new (struct MeshChannelQueue);
2187 q->rel = fwd ? ch->root_rel : ch->dest_rel;
2188 if (NULL != q->rel->uniq)
2190 if (NULL != q->rel->uniq->q)
2192 GMT_cancel (q->rel->uniq->q);
2193 /* ch_message_sent is called, freeing and NULLing uniq */
2197 GNUNET_free (q->rel->uniq);
2200 q->q = GMT_send_prebuilt_message (message, ch->t, ch,
2202 &ch_message_sent, q);
2209 GNUNET_break (NULL == GMT_send_prebuilt_message (message, ch->t, ch,
2217 * Get the static string for identification of the channel.
2219 * @param ch Channel.
2221 * @return Static string with the channel IDs.
2224 GMCH_2s (const struct MeshChannel *ch)
2226 static char buf[64];
2229 return "(NULL Channel)";
2231 sprintf (buf, "%s:%u gid:%X (%X / %X)",
2232 GMT_2s (ch->t), ch->port, ch->gid, ch->lid_root, ch->lid_dest);