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);
332 * We have received a message out of order, or the client is not ready.
333 * Buffer it until we receive an ACK from the client or the missing
334 * message from the channel.
336 * @param msg Message to buffer (MUST be of type MESH_DATA).
337 * @param rel Reliability data to the corresponding direction.
340 add_buffered_data (const struct GNUNET_MESH_Data *msg,
341 struct MeshChannelReliability *rel)
343 struct MeshReliableMessage *copy;
344 struct MeshReliableMessage *prev;
348 size = ntohs (msg->header.size);
349 mid = ntohl (msg->mid);
351 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
353 copy = GNUNET_malloc (sizeof (*copy) + size);
356 copy->type = GNUNET_MESSAGE_TYPE_MESH_DATA;
357 memcpy (©[1], msg, size);
361 // FIXME do something better than O(n), although n < 64...
362 // FIXME start from the end (most messages are the latest ones)
363 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
365 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
366 if (GM_is_pid_bigger (prev->mid, mid))
368 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
369 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
374 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
375 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
376 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
380 * Add a destination client to a channel, initializing all data structures
381 * in the channel and the client.
383 * @param ch Channel to which add the destination.
384 * @param c Client which to add to the channel.
387 add_destination (struct MeshChannel *ch, struct MeshClient *c)
389 if (NULL != ch->dest)
395 /* Assign local id as destination */
396 ch->lid_dest = GML_get_next_chid (c);
398 /* Store in client's hashmap */
399 GML_channel_add (c, ch->lid_dest, ch);
401 GNUNET_break (NULL == ch->dest_rel);
402 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
403 ch->dest_rel->ch = ch;
404 ch->dest_rel->expected_delay.rel_value_us = 0;
411 * Set options in a channel, extracted from a bit flag field.
413 * @param ch Channel to set options to.
414 * @param options Bit array in host byte order.
417 channel_set_options (struct MeshChannel *ch, uint32_t options)
419 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
420 GNUNET_YES : GNUNET_NO;
421 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
422 GNUNET_YES : GNUNET_NO;
427 * Get a bit flag field with the options of a channel.
429 * @param ch Channel to get options from.
431 * @return Bit array in host byte order.
434 channel_get_options (struct MeshChannel *ch)
440 options |= GNUNET_MESH_OPTION_NOBUFFER;
442 options |= GNUNET_MESH_OPTION_RELIABLE;
449 * Notify the destination client that a new incoming channel was created.
451 * @param ch Channel that was created.
454 send_client_create (struct MeshChannel *ch)
458 if (NULL == ch->dest)
462 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
463 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
464 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
465 GMT_get_destination (ch->t));
471 * Send data to a client.
473 * If the client is ready, send directly, otherwise buffer while listening
477 * @param msg Message.
478 * @param fwd Is this a fwd (root->dest) message?
481 send_client_data (struct MeshChannel *ch,
482 const struct GNUNET_MESH_Data *msg,
487 if (ch->dest_rel->client_ready)
488 GML_send_data (ch->dest, msg, ch->lid_dest);
490 add_buffered_data (msg, ch->dest_rel);
494 if (ch->root_rel->client_ready)
495 GML_send_data (ch->root, msg, ch->lid_root);
497 add_buffered_data (msg, ch->root_rel);
503 * Send a buffered message to the client, for in order delivery or
504 * as result of client ACK.
506 * @param ch Channel on which to empty the message buffer.
507 * @param c Client to send to.
508 * @param fwd Is this to send FWD data?.
511 send_client_buffered_data (struct MeshChannel *ch,
512 struct MeshClient *c,
515 struct MeshReliableMessage *copy;
516 struct MeshChannelReliability *rel;
518 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
519 rel = fwd ? ch->dest_rel : ch->root_rel;
520 if (GNUNET_NO == rel->client_ready)
522 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
526 copy = rel->head_recv;
527 /* We never buffer channel management messages */
530 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
532 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
534 LOG (GNUNET_ERROR_TYPE_DEBUG,
535 " have %u! now expecting %u\n",
536 copy->mid, rel->mid_recv + 1);
537 send_client_data (ch, msg, fwd);
540 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
541 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE RECV %p\n", copy);
546 LOG (GNUNET_ERROR_TYPE_DEBUG,
547 " reliable && don't have %u, next is %u\n",
553 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
558 * Allow a client to send more data.
560 * In case the client was already allowed to send data, do nothing.
563 * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
566 send_client_ack (struct MeshChannel *ch, int fwd)
568 struct MeshChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
570 LOG (GNUNET_ERROR_TYPE_DEBUG,
571 " sending %s ack to client on channel %s\n",
572 GM_f2s (fwd), GMCH_2s (ch));
580 if (GNUNET_YES == rel->client_allowed)
582 LOG (GNUNET_ERROR_TYPE_DEBUG, " already allowed\n");
585 rel->client_allowed = GNUNET_YES;
587 GML_send_ack (fwd ? ch->root : ch->dest, fwd ? ch->lid_root : ch->lid_dest);
592 * Notify the root that the destination rejected the channel.
594 * @param ch Rejected channel.
597 send_client_nack (struct MeshChannel *ch)
599 if (NULL == ch->root)
604 GML_send_nack (ch->root, ch->lid_root);
609 * We haven't received an ACK after a certain time: restransmit the message.
611 * @param cls Closure (MeshChannelReliability with the message to restransmit)
612 * @param tc TaskContext.
615 channel_retransmit_message (void *cls,
616 const struct GNUNET_SCHEDULER_TaskContext *tc)
618 struct MeshChannelReliability *rel = cls;
619 struct MeshReliableMessage *copy;
620 struct MeshChannel *ch;
621 struct GNUNET_MESH_Data *payload;
624 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
625 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
629 copy = rel->head_sent;
636 payload = (struct GNUNET_MESH_Data *) ©[1];
637 fwd = (rel == ch->root_rel);
639 /* Message not found in the queue that we are going to use. */
640 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
642 GMCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
643 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
648 * We haven't received an Channel ACK after a certain time: resend the CREATE.
650 * @param cls Closure (MeshChannelReliability of the channel to recreate)
651 * @param tc TaskContext.
654 channel_recreate (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
656 struct MeshChannelReliability *rel = cls;
658 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
659 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
662 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RE-CREATE\n");
663 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
665 if (rel == rel->ch->root_rel)
667 send_create (rel->ch);
678 * Message has been sent: start retransmission timer.
680 * @param cls Closure (queue structure).
682 * @param q Queue handler (no longer valid).
683 * @param type Type of message.
684 * @param size Size of the message.
687 ch_message_sent (void *cls,
688 struct MeshTunnel3 *t,
689 struct MeshTunnel3Queue *q,
690 uint16_t type, size_t size)
692 struct MeshChannelQueue *ch_q = cls;
693 struct MeshReliableMessage *copy = ch_q->copy;
694 struct MeshChannelReliability *rel;
698 case GNUNET_MESSAGE_TYPE_MESH_DATA:
699 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT %u %s (c: %p, q: %p)\n",
700 copy->mid, GM_m2s (type), copy, copy->q);
701 GNUNET_assert (ch_q == copy->q);
702 copy->timestamp = GNUNET_TIME_absolute_get ();
704 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
706 if (0 != rel->expected_delay.rel_value_us)
709 GNUNET_TIME_relative_multiply (rel->expected_delay,
710 MESH_RETRANSMIT_MARGIN);
714 rel->retry_timer = MESH_RETRANSMIT_TIME;
717 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
718 &channel_retransmit_message, rel);
724 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
726 GNUNET_assert (rel->uniq == ch_q);
731 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
733 GNUNET_assert (rel->uniq == ch_q);
734 if (MESH_CHANNEL_READY != rel->ch->state)
736 struct GNUNET_TIME_Relative delay;
738 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rel->retry_task);
739 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
740 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
741 &channel_recreate, rel);
756 * send a channel create message.
758 * @param ch Channel for which to send.
761 send_create (struct MeshChannel *ch)
763 struct GNUNET_MESH_ChannelCreate msgcc;
764 struct MeshChannelQueue *q;
766 msgcc.header.size = htons (sizeof (msgcc));
767 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
768 msgcc.chid = htonl (ch->gid);
769 msgcc.port = htonl (ch->port);
770 msgcc.opt = htonl (channel_get_options (ch));
772 q = GNUNET_new (struct MeshChannelQueue);
773 q->rel = ch->root_rel;
775 /* FIXME cancel on confirm */
776 q->q = GMT_send_prebuilt_message (&msgcc.header, ch->t, ch,
777 GNUNET_YES, GNUNET_YES,
784 * Notify a client that the channel is no longer valid.
786 * @param ch Channel that is destroyed.
787 * @param local_only Should we avoid sending it to other peers?
790 send_destroy (struct MeshChannel *ch, int local_only)
792 struct GNUNET_MESH_ChannelManage msg;
794 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
795 msg.header.size = htons (sizeof (msg));
796 msg.chid = htonl (ch->gid);
798 /* If root is not NULL, notify.
799 * If it's NULL, check lid_root. When a local destroy comes in, root
800 * is set to NULL but lid_root is left untouched. In this case, do nothing,
801 * the client is the one who reuqested the channel to be destroyed.
803 if (NULL != ch->root)
804 GML_send_channel_destroy (ch->root, ch->lid_root);
805 else if (0 == ch->lid_root && GNUNET_NO == local_only)
806 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
808 if (NULL != ch->dest)
809 GML_send_channel_destroy (ch->dest, ch->lid_dest);
810 else if (0 == ch->lid_dest && GNUNET_NO == local_only)
811 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
816 * Destroy all reliable messages queued for a channel,
817 * during a channel destruction.
818 * Frees the reliability structure itself.
820 * @param rel Reliability data for a channel.
823 channel_rel_free_all (struct MeshChannelReliability *rel)
825 struct MeshReliableMessage *copy;
826 struct MeshReliableMessage *next;
831 for (copy = rel->head_recv; NULL != copy; copy = next)
834 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
835 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH RECV %p\n", copy);
838 for (copy = rel->head_sent; NULL != copy; copy = next)
841 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
842 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH %p\n", copy);
845 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
847 GNUNET_SCHEDULER_cancel (rel->retry_task);
849 if (NULL != rel->uniq)
850 GMT_cancel (rel->uniq->q);
856 * Mark future messages as ACK'd.
858 * @param rel Reliability data.
859 * @param msg DataACK message with a bitfield of future ACK'd messages.
862 channel_rel_free_sent (struct MeshChannelReliability *rel,
863 const struct GNUNET_MESH_DataACK *msg)
865 struct MeshReliableMessage *copy;
866 struct MeshReliableMessage *next;
873 bitfield = msg->futures;
874 mid = ntohl (msg->mid);
875 LOG (GNUNET_ERROR_TYPE_DEBUG,
876 "!!! free_sent_reliable %u %llX\n",
878 LOG (GNUNET_ERROR_TYPE_DEBUG,
879 " rel %p, head %p\n",
880 rel, rel->head_sent);
881 for (i = 0, copy = rel->head_sent;
882 i < 64 && NULL != copy && 0 != bitfield;
885 LOG (GNUNET_ERROR_TYPE_DEBUG,
886 " trying bit %u (mid %u)\n",
889 if (0 == (bitfield & mask))
892 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
893 /* Bit was set, clear the bit from the bitfield */
896 /* The i-th bit was set. Do we have that copy? */
897 /* Skip copies with mid < target */
898 target = mid + i + 1;
899 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
900 while (NULL != copy && GM_is_pid_bigger (target, copy->mid))
903 /* Did we run out of copies? (previously freed, it's ok) */
906 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
910 /* Did we overshoot the target? (previously freed, it's ok) */
911 if (GM_is_pid_bigger (copy->mid, target))
913 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
917 /* Now copy->mid == target, free it */
919 rel_message_free (copy, GNUNET_YES);
922 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
927 * Destroy a reliable message after it has been acknowledged, either by
928 * direct mid ACK or bitfield. Updates the appropriate data structures and
929 * timers and frees all memory.
931 * @param copy Message that is no longer needed: remote peer got it.
932 * @param update_time Is the timing information relevant?
933 * If this message is ACK in a batch the timing information
934 * is skewed by the retransmission, count only for the
935 * retransmitted message.
938 rel_message_free (struct MeshReliableMessage *copy, int update_time)
940 struct MeshChannelReliability *rel;
941 struct GNUNET_TIME_Relative time;
944 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
947 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
948 if (0 == rel->expected_delay.rel_value_us)
949 rel->expected_delay = time;
952 rel->expected_delay.rel_value_us *= 7;
953 rel->expected_delay.rel_value_us += time.rel_value_us;
954 rel->expected_delay.rel_value_us /= 8;
956 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
957 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
958 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
959 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
961 rel->retry_timer = rel->expected_delay;
965 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! batch free, ignoring timing\n");
967 rel->ch->pending_messages--;
968 if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
970 struct MeshTunnel3 *t = rel->ch->t;
971 GMCH_destroy (rel->ch);
972 GMT_destroy_if_empty (t);
976 GMT_cancel (copy->q->q);
978 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
979 LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE %p\n", copy);
985 * Confirm we got a channel create.
987 * @param ch The channel to confirm.
988 * @param fwd Should we send a FWD ACK? (going dest->root)
991 channel_send_ack (struct MeshChannel *ch, int fwd)
993 struct GNUNET_MESH_ChannelManage msg;
995 msg.header.size = htons (sizeof (msg));
996 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
997 LOG (GNUNET_ERROR_TYPE_DEBUG,
998 " sending channel %s ack for channel %s\n",
999 GM_f2s (fwd), GMCH_2s (ch));
1001 msg.chid = htonl (ch->gid);
1002 GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1007 * Notify that a channel create didn't succeed.
1009 * @param ch The channel to reject.
1012 channel_send_nack (struct MeshChannel *ch)
1014 struct GNUNET_MESH_ChannelManage msg;
1016 msg.header.size = htons (sizeof (msg));
1017 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK);
1018 LOG (GNUNET_ERROR_TYPE_DEBUG,
1019 " sending channel NACK for channel %s\n",
1022 msg.chid = htonl (ch->gid);
1023 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
1028 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
1030 * @param ch Channel to mark as ready.
1031 * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
1034 channel_confirm (struct MeshChannel *ch, int fwd)
1036 struct MeshChannelReliability *rel;
1038 LOG (GNUNET_ERROR_TYPE_DEBUG,
1039 " channel confirm %s %s:%X\n",
1040 GM_f2s (fwd), GMT_2s (ch->t), ch->gid);
1041 ch->state = MESH_CHANNEL_READY;
1043 rel = fwd ? ch->root_rel : ch->dest_rel;
1044 rel->client_ready = GNUNET_YES;
1045 rel->expected_delay = rel->retry_timer;
1046 send_client_ack (ch, fwd);
1048 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1050 GNUNET_SCHEDULER_cancel (rel->retry_task);
1051 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1053 else if (NULL != rel->uniq)
1055 GMT_cancel (rel->uniq->q);
1056 /* ch_sent_message will free and NULL uniq */
1060 /* We SHOULD have been trying to retransmit this! */
1064 /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
1065 if (GNUNET_YES == fwd)
1066 channel_send_ack (ch, GNUNET_NO);
1071 * Save a copy to retransmit in case it gets lost.
1073 * Initializes all needed callbacks and timers.
1075 * @param ch Channel this message goes on.
1076 * @param msg Message to copy.
1077 * @param fwd Is this fwd traffic?
1079 static struct MeshReliableMessage *
1080 channel_save_copy (struct MeshChannel *ch,
1081 const struct GNUNET_MessageHeader *msg,
1084 struct MeshChannelReliability *rel;
1085 struct MeshReliableMessage *copy;
1090 rel = fwd ? ch->root_rel : ch->dest_rel;
1091 mid = rel->mid_send - 1;
1092 type = ntohs (msg->type);
1093 size = ntohs (msg->size);
1095 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u %s\n", mid, GM_m2s (type));
1096 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
1097 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", copy);
1101 memcpy (©[1], msg, size);
1102 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
1103 ch->pending_messages++;
1110 * Create a new channel.
1112 * @param t Tunnel this channel is in.
1113 * @param owner Client that owns the channel, NULL for foreign channels.
1114 * @param lid_root Local ID for root client.
1116 * @return A new initialized channel. NULL on error.
1118 static struct MeshChannel *
1119 channel_new (struct MeshTunnel3 *t,
1120 struct MeshClient *owner,
1121 MESH_ChannelNumber lid_root)
1123 struct MeshChannel *ch;
1125 ch = GNUNET_new (struct MeshChannel);
1127 ch->lid_root = lid_root;
1130 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
1134 ch->gid = GMT_get_next_chid (t);
1135 GML_channel_add (owner, lid_root, ch);
1137 GMT_add_channel (t, ch);
1144 * Test if the channel is loopback: both root and dest are on the local peer.
1146 * @param ch Channel to test.
1148 * @return #GNUNET_YES if channel is loopback, #NGUNET_NO otherwise.
1151 is_loopback (const struct MeshChannel *ch)
1154 return GMT_is_loopback (ch->t);
1156 return (NULL != ch->root && NULL != ch->dest);
1161 * Handle a loopback message: call the appropriate handler for the message type.
1163 * @param ch Channel this message is on.
1164 * @param msgh Message header.
1165 * @param fwd Is this FWD traffic?
1168 handle_loopback (struct MeshChannel *ch,
1169 const struct GNUNET_MessageHeader *msgh,
1174 type = ntohs (msgh->type);
1175 LOG (GNUNET_ERROR_TYPE_DEBUG,
1176 "Loopback %s %s message!\n",
1177 GM_f2s (fwd), GM_m2s (type));
1181 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1182 /* Don't send hop ACK, wait for client to ACK */
1183 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
1186 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
1187 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1190 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1191 GMCH_handle_create (ch->t,
1192 (struct GNUNET_MESH_ChannelCreate *) msgh);
1195 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1196 GMCH_handle_ack (ch,
1197 (struct GNUNET_MESH_ChannelManage *) msgh,
1201 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1202 GMCH_handle_nack (ch);
1205 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1206 GMCH_handle_destroy (ch,
1207 (struct GNUNET_MESH_ChannelManage *) msgh,
1212 GNUNET_break_op (0);
1213 LOG (GNUNET_ERROR_TYPE_DEBUG,
1214 "end-to-end message not known (%u)\n",
1215 ntohs (msgh->type));
1221 /******************************************************************************/
1222 /******************************** API ***********************************/
1223 /******************************************************************************/
1226 * Destroy a channel and free all resources.
1228 * @param ch Channel to destroy.
1231 GMCH_destroy (struct MeshChannel *ch)
1233 struct MeshClient *c;
1238 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
1239 GMT_2s (ch->t), ch->gid);
1245 GML_channel_remove (c, ch->lid_root, ch);
1251 GML_channel_remove (c, ch->lid_dest, ch);
1254 channel_rel_free_all (ch->root_rel);
1255 channel_rel_free_all (ch->dest_rel);
1257 GMT_remove_channel (ch->t, ch);
1258 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
1267 * @param ch Channel.
1272 GMCH_get_id (const struct MeshChannel *ch)
1279 * Get the channel tunnel.
1281 * @param ch Channel to get the tunnel from.
1283 * @return tunnel of the channel.
1285 struct MeshTunnel3 *
1286 GMCH_get_tunnel (const struct MeshChannel *ch)
1293 * Get free buffer space towards the client on a specific channel.
1295 * @param ch Channel.
1296 * @param fwd Is query about FWD traffic?
1298 * @return Free buffer space [0 - 64]
1301 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
1303 struct MeshChannelReliability *rel;
1305 rel = fwd ? ch->dest_rel : ch->root_rel;
1307 /* If rel is NULL it means that the end is not yet created,
1308 * most probably is a loopback channel at the point of sending
1309 * the ChannelCreate to itself.
1314 return (64 - rel->n_recv);
1319 * Get flow control status of end point: is client allow to send?
1321 * @param ch Channel.
1322 * @param fwd Is query about FWD traffic? (Request root status).
1324 * @return #GNUNET_YES if client is allowed to send us data.
1327 GMCH_get_allowed (struct MeshChannel *ch, int fwd)
1329 struct MeshChannelReliability *rel;
1331 rel = fwd ? ch->root_rel : ch->dest_rel;
1335 /* Probably shutting down: root/dest NULL'ed to mark disconnection */
1336 GNUNET_break (GNUNET_NO != ch->destroy);
1340 return rel->client_allowed;
1345 * Is the root client for this channel on this peer?
1347 * @param ch Channel.
1348 * @param fwd Is this for fwd traffic?
1350 * @return #GNUNET_YES in case it is.
1353 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1355 struct MeshClient *c;
1357 c = fwd ? ch->root : ch->dest;
1363 * Is the destination client for this channel on this peer?
1365 * @param ch Channel.
1366 * @param fwd Is this for fwd traffic?
1368 * @return #GNUNET_YES in case it is.
1371 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1373 struct MeshClient *c;
1375 c = fwd ? ch->dest : ch->root;
1381 * Send an end-to-end ACK message for the most recent in-sequence payload.
1383 * If channel is not reliable, do nothing.
1385 * @param ch Channel this is about.
1386 * @param fwd Is for FWD traffic? (ACK dest->owner)
1389 GMCH_send_data_ack (struct MeshChannel *ch, int fwd)
1391 struct GNUNET_MESH_DataACK msg;
1392 struct MeshChannelReliability *rel;
1393 struct MeshReliableMessage *copy;
1398 if (GNUNET_NO == ch->reliable)
1402 rel = fwd ? ch->dest_rel : ch->root_rel;
1403 ack = rel->mid_recv - 1;
1404 LOG (GNUNET_ERROR_TYPE_DEBUG,
1405 " !! Send DATA_ACK for %u\n",
1408 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
1409 msg.header.size = htons (sizeof (msg));
1410 msg.chid = htonl (ch->gid);
1412 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1414 if (copy->type != GNUNET_MESSAGE_TYPE_MESH_DATA)
1416 LOG (GNUNET_ERROR_TYPE_DEBUG,
1417 "!! Type %s, expected DATA\n",
1418 GM_m2s (copy->type));
1421 if (copy->mid == ack + 1)
1426 delta = copy->mid - (ack + 1);
1429 mask = 0x1LL << delta;
1430 msg.futures |= mask;
1431 LOG (GNUNET_ERROR_TYPE_DEBUG,
1432 " !! setting bit for %u (delta %u) (%llX) -> %llX\n",
1433 copy->mid, delta, mask, msg.futures);
1435 msg.mid = htonl (ack);
1436 LOG (GNUNET_ERROR_TYPE_DEBUG,
1437 "!!! ACK for %u, futures %llX\n",
1440 GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1441 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1446 * Allow a client to send us more data, in case it was choked.
1448 * @param ch Channel.
1449 * @param fwd Is this about FWD traffic? (Root client).
1452 GMCH_allow_client (struct MeshChannel *ch, int fwd)
1454 struct MeshChannelReliability *rel;
1455 unsigned int buffer;
1457 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1459 if (MESH_CHANNEL_READY != ch->state)
1461 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel not ready yet!\n");
1465 if (GNUNET_YES == ch->reliable)
1467 rel = fwd ? ch->root_rel : ch->dest_rel;
1470 GNUNET_break (GNUNET_NO != ch->destroy);
1473 if (NULL != rel->head_sent && 64 <= rel->mid_send - rel->head_sent->mid)
1475 LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
1480 if (is_loopback (ch))
1481 buffer = GMCH_get_buffer (ch, fwd);
1483 buffer = GMT_get_connections_buffer (ch->t);
1487 LOG (GNUNET_ERROR_TYPE_DEBUG, " no buffer space.\n");
1491 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space %u, allowing\n", buffer);
1492 send_client_ack (ch, fwd);
1499 * @param ch Channel.
1502 GMCH_debug (struct MeshChannel *ch)
1506 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1509 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1510 GMT_2s (ch->t), ch->gid, ch);
1511 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1512 ch->root, ch->root_rel);
1513 if (NULL != ch->root)
1515 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1517 ch->root_rel->client_ready ? "YES" : "NO");
1518 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1520 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1521 ch->dest, ch->dest_rel);
1522 if (NULL != ch->dest)
1524 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1525 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1526 ch->dest_rel->client_ready ? "YES" : "NO");
1527 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1533 * Handle an ACK given by a client.
1535 * Mark client as ready and send him any buffered data we could have for him.
1537 * @param ch Channel.
1538 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1541 GMCH_handle_local_ack (struct MeshChannel *ch, int fwd)
1543 struct MeshChannelReliability *rel;
1544 struct MeshClient *c;
1546 rel = fwd ? ch->dest_rel : ch->root_rel;
1547 c = fwd ? ch->dest : ch->root;
1549 rel->client_ready = GNUNET_YES;
1550 send_client_buffered_data (ch, c, fwd);
1551 if (is_loopback (ch))
1553 unsigned int buffer;
1555 buffer = GMCH_get_buffer (ch, fwd);
1557 GMCH_allow_client (ch, fwd);
1561 GMT_send_connection_acks (ch->t);
1566 * Handle data given by a client.
1568 * Check whether the client is allowed to send in this tunnel, save if channel
1569 * is reliable and send an ACK to the client if there is still buffer space
1572 * @param ch Channel.
1573 * @param c Client which sent the data.
1574 * @param message Message.
1575 * @param fwd Is this a FWD data?
1577 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1580 GMCH_handle_local_data (struct MeshChannel *ch,
1581 struct MeshClient *c,
1582 struct GNUNET_MessageHeader *message,
1585 struct MeshChannelReliability *rel;
1586 struct GNUNET_MESH_Data *payload;
1587 size_t size = ntohs (message->size);
1588 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
1589 unsigned char cbuf[p2p_size];
1591 /* Is the client in the channel? */
1598 GNUNET_break_op (0);
1599 return GNUNET_SYSERR;
1602 rel = fwd ? ch->root_rel : ch->dest_rel;
1604 if (GNUNET_NO == rel->client_allowed)
1606 GNUNET_break_op (0);
1607 return GNUNET_SYSERR;
1610 rel->client_allowed = GNUNET_NO;
1612 /* Ok, everything is correct, send the message. */
1613 payload = (struct GNUNET_MESH_Data *) cbuf;
1614 payload->mid = htonl (rel->mid_send);
1616 memcpy (&payload[1], message, size);
1617 payload->header.size = htons (p2p_size);
1618 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
1619 payload->chid = htonl (ch->gid);
1620 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1621 GMCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1623 if (is_loopback (ch))
1625 if (GMCH_get_buffer (ch, fwd) > 0)
1626 send_client_ack (ch, fwd);
1631 if (GMT_get_connections_buffer (ch->t) > 0)
1633 send_client_ack (ch, fwd);
1641 * Handle a channel destroy requested by a client.
1643 * Destroy the channel and the tunnel in case this was the last channel.
1645 * @param ch Channel.
1646 * @param c Client that requested the destruction (to avoid notifying him).
1647 * @param is_root Is the request coming from root?
1650 GMCH_handle_local_destroy (struct MeshChannel *ch,
1651 struct MeshClient *c,
1654 struct MeshTunnel3 *t;
1656 ch->destroy = GNUNET_YES;
1657 /* Cleanup after the tunnel */
1658 if (GNUNET_NO == is_root && c == ch->dest)
1660 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1661 GML_client_delete_channel (c, ch, ch->lid_dest);
1664 if (GNUNET_YES == is_root && c == ch->root)
1666 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1667 GML_client_delete_channel (c, ch, ch->lid_root);
1672 send_destroy (ch, GNUNET_NO);
1673 if (0 == ch->pending_messages)
1676 GMT_destroy_if_empty (t);
1682 * Handle a channel create requested by a client.
1684 * Create the channel and the tunnel in case this was the first0 channel.
1686 * @param c Client that requested the creation (will be the root).
1687 * @param msg Create Channel message.
1689 * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1692 GMCH_handle_local_create (struct MeshClient *c,
1693 struct GNUNET_MESH_ChannelMessage *msg)
1695 struct MeshChannel *ch;
1696 struct MeshTunnel3 *t;
1697 struct MeshPeer *peer;
1698 MESH_ChannelNumber chid;
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1701 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1702 chid = ntohl (msg->channel_id);
1704 /* Sanity check for duplicate channel IDs */
1705 if (NULL != GML_channel_get (c, chid))
1708 return GNUNET_SYSERR;
1711 peer = GMP_get (&msg->peer);
1712 GMP_add_tunnel (peer);
1713 t = GMP_get_tunnel (peer);
1715 if (GMP_get_short_id (peer) == myid)
1717 GMT_change_cstate (t, MESH_TUNNEL3_READY);
1724 /* Create channel */
1725 ch = channel_new (t, c, chid);
1729 return GNUNET_SYSERR;
1731 ch->port = ntohl (msg->port);
1732 channel_set_options (ch, ntohl (msg->opt));
1734 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1735 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
1736 ch->root_rel->ch = ch;
1737 ch->root_rel->retry_timer = GNUNET_TIME_UNIT_SECONDS;
1738 ch->root_rel->expected_delay.rel_value_us = 0;
1740 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
1749 * Handler for mesh network payload traffic.
1751 * @param ch Channel for the message.
1752 * @param msg Unencryted data message.
1753 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1754 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1755 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1756 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1759 GMCH_handle_data (struct MeshChannel *ch,
1760 const struct GNUNET_MESH_Data *msg,
1763 struct MeshChannelReliability *rel;
1764 struct MeshClient *c;
1767 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1768 if (GNUNET_SYSERR == fwd)
1770 if (is_loopback (ch))
1772 /* It is a loopback channel after all... */
1776 fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1779 /* Initialize FWD/BCK data */
1780 c = fwd ? ch->dest : ch->root;
1781 rel = fwd ? ch->dest_rel : ch->root_rel;
1789 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1791 mid = ntohl (msg->mid);
1792 LOG (GNUNET_ERROR_TYPE_DEBUG, "!! got mid %u\n", mid);
1794 if (GNUNET_NO == ch->reliable ||
1795 ( !GM_is_pid_bigger (rel->mid_recv, mid) &&
1796 GM_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1798 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1799 if (GNUNET_YES == ch->reliable)
1801 /* Is this the exact next expected messasge? */
1802 if (mid == rel->mid_recv)
1804 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1806 send_client_data (ch, msg, fwd);
1810 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1811 add_buffered_data (msg, rel);
1816 /* Tunnel is unreliable: send to clients directly */
1817 /* FIXME: accept Out Of Order traffic */
1818 rel->mid_recv = mid + 1;
1819 send_client_data (ch, msg, fwd);
1824 GNUNET_break_op (GM_is_pid_bigger (rel->mid_recv, mid));
1825 LOG (GNUNET_ERROR_TYPE_DEBUG,
1826 " !!! MID %u not expected (%u - %u), dropping!\n",
1827 mid, rel->mid_recv, rel->mid_recv + 63);
1830 GMCH_send_data_ack (ch, fwd);
1835 * Handler for mesh network traffic end-to-end ACKs.
1837 * @param ch Channel on which we got this message.
1838 * @param msg Data message.
1839 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1840 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1841 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1842 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1845 GMCH_handle_data_ack (struct MeshChannel *ch,
1846 const struct GNUNET_MESH_DataACK *msg,
1849 struct MeshChannelReliability *rel;
1850 struct MeshReliableMessage *copy;
1851 struct MeshReliableMessage *next;
1855 /* If this is a remote (non-loopback) channel, find 'fwd'. */
1856 if (GNUNET_SYSERR == fwd)
1858 if (is_loopback (ch))
1860 /* It is a loopback channel after all... */
1864 /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
1865 fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
1868 ack = ntohl (msg->mid);
1869 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1870 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1872 if (GNUNET_YES == fwd)
1882 GNUNET_break_op (0);
1886 /* Free ACK'd copies: no need to retransmit those anymore */
1887 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1889 if (GM_is_pid_bigger (copy->mid, ack))
1891 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1892 channel_rel_free_sent (rel, msg);
1896 LOG (GNUNET_ERROR_TYPE_DEBUG, " !! id %u\n", copy->mid);
1898 rel_message_free (copy, GNUNET_YES);
1901 /* ACK client if needed */
1902 GMCH_allow_client (ch, fwd);
1904 /* If some message was free'd, update the retransmission delay */
1905 if (GNUNET_YES == work)
1907 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1909 GNUNET_SCHEDULER_cancel (rel->retry_task);
1910 if (NULL != rel->head_sent && NULL == rel->head_sent->q)
1912 struct GNUNET_TIME_Absolute new_target;
1913 struct GNUNET_TIME_Relative delay;
1915 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1916 MESH_RETRANSMIT_MARGIN);
1917 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1919 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1921 GNUNET_SCHEDULER_add_delayed (delay,
1922 &channel_retransmit_message,
1925 else /* either no more traffic to ack or traffic has just been queued */
1927 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1930 else /* work was done but no task was pending? shouldn't happen! */
1939 * Handler for channel create messages.
1941 * Does not have fwd parameter because it's always 'FWD': channel is incoming.
1943 * @param t Tunnel this channel will be in.
1944 * @param msg Channel crate message.
1946 struct MeshChannel *
1947 GMCH_handle_create (struct MeshTunnel3 *t,
1948 const struct GNUNET_MESH_ChannelCreate *msg)
1950 MESH_ChannelNumber chid;
1951 struct MeshChannel *ch;
1952 struct MeshClient *c;
1954 chid = ntohl (msg->chid);
1956 ch = GMT_get_channel (t, chid);
1959 /* Create channel */
1960 ch = channel_new (t, NULL, 0);
1963 channel_set_options (ch, ntohl (msg->opt));
1965 /* Find a destination client */
1966 ch->port = ntohl (msg->port);
1967 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", ch->port);
1968 c = GML_client_get_by_port (ch->port);
1971 /* TODO send reject */
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");
1976 channel_send_nack (ch);
1980 LOG (GNUNET_ERROR_TYPE_DEBUG, " not loopback: destroy now\n");
1981 channel_send_nack (ch);
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 channel_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)
2116 type = ntohs (message->type);
2117 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH Send %s %s on channel %s\n",
2118 GM_f2s (fwd), GM_m2s (type),
2121 if (GMT_is_loopback (ch->t))
2123 handle_loopback (ch, message, fwd);
2129 case GNUNET_MESSAGE_TYPE_MESH_DATA:
2131 if (GNUNET_YES == ch->reliable)
2133 struct MeshChannelQueue *q;
2135 q = GNUNET_new (struct MeshChannelQueue);
2137 if (NULL == existing_copy)
2138 q->copy = channel_save_copy (ch, message, fwd);
2141 q->copy = (struct MeshReliableMessage *) existing_copy;
2142 if (NULL != q->copy->q)
2144 /* Last retransmission was queued but not yet sent!
2145 * This retransmission was scheduled by a ch_message_sent which
2146 * followed a very fast RTT, so the tiny delay made the
2147 * retransmission function to execute before the previous
2148 * retransmitted message even had a chance to leave the peer.
2149 * Cancel this message and wait until the pending
2150 * retransmission leaves the peer and ch_message_sent starts
2151 * the timer for the next one.
2156 LOG (GNUNET_ERROR_TYPE_DEBUG,
2157 " using existing copy: %p {r:%p q:%p t:%u}\n",
2159 q->copy->rel, q->copy->q, q->copy->type);
2161 LOG (GNUNET_ERROR_TYPE_DEBUG, " new q: %p\n", q);
2163 q->q = GMT_send_prebuilt_message (message, ch->t, ch,
2164 fwd, NULL != existing_copy,
2165 &ch_message_sent, q);
2166 /* q itself is stored in copy */
2170 GNUNET_break (NULL == GMT_send_prebuilt_message (message, ch->t, ch,
2177 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2179 struct MeshChannelReliability *rel;
2181 rel = fwd ? ch->root_rel : ch->dest_rel;
2182 if (NULL != rel->uniq)
2184 GMT_cancel (rel->uniq->q);
2185 /* ch_message_sent is called, freeing ack_q */
2187 rel->uniq = GNUNET_new (struct MeshChannelQueue);
2188 rel->uniq->type = type;
2189 rel->uniq->rel = rel;
2190 rel->uniq->q = GMT_send_prebuilt_message (message, ch->t, ch,
2199 GNUNET_break (NULL == GMT_send_prebuilt_message (message, ch->t, ch,
2207 * Get the static string for identification of the channel.
2209 * @param ch Channel.
2211 * @return Static string with the channel IDs.
2214 GMCH_2s (const struct MeshChannel *ch)
2216 static char buf[64];
2219 return "(NULL Channel)";
2221 sprintf (buf, "%s:%u gid:%X (%X / %X)",
2222 GMT_2s (ch->t), ch->port, ch->gid, ch->lid_root, ch->lid_dest);