2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_statistics_service.h"
28 #include "mesh_protocol_enc.h"
30 #include "gnunet-service-mesh_channel.h"
31 #include "gnunet-service-mesh_local.h"
32 #include "gnunet-service-mesh_tunnel.h"
33 #include "gnunet-service-mesh_peer.h"
35 #define LOG(level, ...) GNUNET_log_from(level,"mesh-chn",__VA_ARGS__)
37 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
38 #define MESH_RETRANSMIT_MARGIN 4
42 * All the states a connection can be in.
47 * Uninitialized status, should never appear in operation.
52 * Connection create message sent, waiting for ACK.
57 * Connection confirmed, ready to carry traffic..
65 * Info needed to retry a message in case it gets lost.
67 struct MeshReliableMessage
70 * Double linked list, FIFO style
72 struct MeshReliableMessage *next;
73 struct MeshReliableMessage *prev;
76 * Type of message (payload, channel management).
81 * Tunnel Reliability queue this message is in.
83 struct MeshChannelReliability *rel;
86 * ID of the message (ACK needed to free)
91 * When was this message issued (to calculate ACK delay)
93 struct GNUNET_TIME_Absolute timestamp;
95 /* struct GNUNET_MESH_Data with payload */
100 * Info about the traffic state for a client in a channel.
102 struct MeshChannelReliability
105 * Channel this is about.
107 struct MeshChannel *ch;
110 * DLL of messages sent and not yet ACK'd.
112 struct MeshReliableMessage *head_sent;
113 struct MeshReliableMessage *tail_sent;
116 * Messages pending to send.
121 * DLL of messages received out of order.
123 struct MeshReliableMessage *head_recv;
124 struct MeshReliableMessage *tail_recv;
132 * Next MID to use for outgoing traffic.
137 * Next MID expected for incoming traffic.
142 * Can we send data to the client?
147 * Task to resend/poll in case no ACK is received.
149 GNUNET_SCHEDULER_TaskIdentifier retry_task;
152 * Counter for exponential backoff.
154 struct GNUNET_TIME_Relative retry_timer;
157 * How long does it usually take to get an ACK.
159 struct GNUNET_TIME_Relative expected_delay;
164 * Struct containing all information regarding a channel to a remote client.
169 * Tunnel this channel is in.
171 struct MeshTunnel3 *t;
174 * Destination port of the channel.
179 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
181 MESH_ChannelNumber gid;
184 * Local tunnel number for root (owner) client.
185 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
187 MESH_ChannelNumber lid_root;
190 * Local tunnel number for local destination clients (incoming number)
191 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
193 MESH_ChannelNumber lid_dest;
198 enum MeshChannelState state;
201 * Is the tunnel bufferless (minimum latency)?
206 * Is the tunnel reliable?
211 * Last time the channel was used
213 struct GNUNET_TIME_Absolute timestamp;
216 * Client owner of the tunnel, if any
218 struct MeshClient *root;
221 * Client destination of the tunnel, if any.
223 struct MeshClient *dest;
226 * Flag to signal the destruction of the channel.
227 * If this is set GNUNET_YES the channel will be destroyed
228 * when the queue is empty.
233 * Total messages pending for this channel, payload or not.
235 unsigned int pending_messages;
239 * Only present (non-NULL) at the owner of a tunnel.
241 struct MeshChannelReliability *root_rel;
245 * Only present (non-NULL) at the destination of a tunnel.
247 struct MeshChannelReliability *dest_rel;
252 /******************************************************************************/
253 /******************************* GLOBALS ***********************************/
254 /******************************************************************************/
257 * Global handle to the statistics service.
259 extern struct GNUNET_STATISTICS_Handle *stats;
262 * Local peer own ID (memory efficient handle).
264 extern GNUNET_PEER_Id myid;
267 /******************************************************************************/
268 /******************************** STATIC ***********************************/
269 /******************************************************************************/
272 * Destroy a reliable message after it has been acknowledged, either by
273 * direct mid ACK or bitfield. Updates the appropriate data structures and
274 * timers and frees all memory.
276 * @param copy Message that is no longer needed: remote peer got it.
279 rel_message_free (struct MeshReliableMessage *copy);
282 * We have received a message out of order, or the client is not ready.
283 * Buffer it until we receive an ACK from the client or the missing
284 * message from the channel.
286 * @param msg Message to buffer (MUST be of type MESH_DATA).
287 * @param rel Reliability data to the corresponding direction.
290 add_buffered_data (const struct GNUNET_MESH_Data *msg,
291 struct MeshChannelReliability *rel)
293 struct MeshReliableMessage *copy;
294 struct MeshReliableMessage *prev;
298 size = ntohs (msg->header.size);
299 mid = ntohl (msg->mid);
301 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
303 copy = GNUNET_malloc (sizeof (*copy) + size);
306 memcpy (©[1], msg, size);
310 // FIXME do something better than O(n), although n < 64...
311 // FIXME start from the end (most messages are the latest ones)
312 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
314 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
315 if (GMC_is_pid_bigger (prev->mid, mid))
317 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
318 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
323 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
324 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
325 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
330 * Send data to a client.
332 * If the client is ready, send directly, otherwise buffer while listening
336 * @param msg Message.
337 * @param fwd Is this a fwd (root->dest) message?
340 send_client_data (struct MeshChannel *ch,
341 const struct GNUNET_MESH_Data *msg,
346 if (ch->dest_rel->client_ready)
347 GML_send_data (ch->dest, msg, ch->lid_dest);
349 add_buffered_data (msg, ch->dest_rel);
353 if (ch->root_rel->client_ready)
354 GML_send_data (ch->root, msg, ch->lid_root);
356 add_buffered_data (msg, ch->root_rel);
362 * Add a client to a channel, initializing all needed data structures.
364 * @param ch Channel to which add the client.
365 * @param c Client which to add to the channel.
368 GMCH_add_client (struct MeshChannel *ch, struct MeshClient *c)
370 if (NULL != ch->dest)
376 /* Assign local id as destination */
377 ch->lid_dest = GML_get_next_chid (c);
379 /* Store in client's hashmap */
380 GML_channel_add (c, ch->lid_dest, ch);
382 GNUNET_break (NULL == ch->dest_rel);
383 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
384 ch->dest_rel->ch = ch;
385 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
392 * Destroy all reliable messages queued for a channel,
393 * during a channel destruction.
394 * Frees the reliability structure itself.
396 * @param rel Reliability data for a channel.
399 channel_rel_free_all (struct MeshChannelReliability *rel)
401 struct MeshReliableMessage *copy;
402 struct MeshReliableMessage *next;
407 for (copy = rel->head_recv; NULL != copy; copy = next)
410 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
413 for (copy = rel->head_sent; NULL != copy; copy = next)
416 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
419 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
420 GNUNET_SCHEDULER_cancel (rel->retry_task);
426 * Mark future messages as ACK'd.
428 * @param rel Reliability data.
429 * @param msg DataACK message with a bitfield of future ACK'd messages.
432 channel_rel_free_sent (struct MeshChannelReliability *rel,
433 const struct GNUNET_MESH_DataACK *msg)
435 struct MeshReliableMessage *copy;
436 struct MeshReliableMessage *next;
443 bitfield = msg->futures;
444 mid = ntohl (msg->mid);
445 LOG (GNUNET_ERROR_TYPE_DEBUG,
446 "free_sent_reliable %u %llX\n",
448 LOG (GNUNET_ERROR_TYPE_DEBUG,
449 " rel %p, head %p\n",
450 rel, rel->head_sent);
451 for (i = 0, copy = rel->head_sent;
452 i < 64 && NULL != copy && 0 != bitfield;
455 LOG (GNUNET_ERROR_TYPE_DEBUG,
456 " trying bit %u (mid %u)\n",
459 if (0 == (bitfield & mask))
462 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
463 /* Bit was set, clear the bit from the bitfield */
466 /* The i-th bit was set. Do we have that copy? */
467 /* Skip copies with mid < target */
468 target = mid + i + 1;
469 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
470 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
473 /* Did we run out of copies? (previously freed, it's ok) */
476 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
480 /* Did we overshoot the target? (previously freed, it's ok) */
481 if (GMC_is_pid_bigger (copy->mid, target))
483 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
487 /* Now copy->mid == target, free it */
489 rel_message_free (copy);
492 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
497 * We haven't received an ACK after a certain time: restransmit the message.
499 * @param cls Closure (MeshReliableMessage with the message to restransmit)
500 * @param tc TaskContext.
503 channel_retransmit_message (void *cls,
504 const struct GNUNET_SCHEDULER_TaskContext *tc)
506 struct MeshChannelReliability *rel = cls;
507 struct MeshReliableMessage *copy;
508 struct MeshChannel *ch;
509 struct GNUNET_MESH_Data *payload;
512 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
513 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
517 copy = rel->head_sent;
524 /* Search the message to be retransmitted in the outgoing queue.
525 * Check only the queue for the connection that is going to be used,
526 * if the message is stuck in some other connection's queue we shouldn't
528 * - cancelling it and sending the new one doesn't guarantee it's delivery,
529 * the old connection could be temporary stalled or the queue happened to
530 * be long at time of insertion.
531 * - not sending the new one could cause terrible delays the old connection
534 // FIXME access to queue elements is limited
535 payload = (struct GNUNET_MESH_Data *) ©[1];
536 fwd = (rel == ch->root_rel);
537 // c = GMT_get_connection (ch->t, fwd);
538 // hop = connection_get_hop (c, fwd);
539 // for (q = hop->queue_head; NULL != q; q = q->next)
541 // if (ntohs (payload->header.type) == q->type && ch == q->ch)
543 // struct GNUNET_MESH_Data *queued_data = q->cls;
545 // if (queued_data->mid == payload->mid)
550 /* Message not found in the queue that we are going to use. */
553 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
555 GMCH_send_prebuilt_message (&payload->header, ch, fwd);
556 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
560 // LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
563 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
564 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
565 &channel_retransmit_message,
571 * Destroy a reliable message after it has been acknowledged, either by
572 * direct mid ACK or bitfield. Updates the appropriate data structures and
573 * timers and frees all memory.
575 * @param copy Message that is no longer needed: remote peer got it.
578 rel_message_free (struct MeshReliableMessage *copy)
580 struct MeshChannelReliability *rel;
581 struct GNUNET_TIME_Relative time;
584 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
585 rel->expected_delay.rel_value_us *= 7;
586 rel->expected_delay.rel_value_us += time.rel_value_us;
587 rel->expected_delay.rel_value_us /= 8;
589 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
590 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
591 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
592 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
593 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
594 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
596 rel->retry_timer = rel->expected_delay;
597 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
604 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
606 * @param ch Channel to mark as ready.
607 * @param fwd Was the CREATE message sent fwd?
610 channel_confirm (struct MeshChannel *ch, int fwd)
612 struct MeshChannelReliability *rel;
613 struct MeshReliableMessage *copy;
614 struct MeshReliableMessage *next;
616 LOG (GNUNET_ERROR_TYPE_DEBUG,
617 " channel confirm %s %s:%X\n",
618 fwd ? "FWD" : "BCK", GMT_2s (ch->t), ch->gid);
619 ch->state = MESH_CHANNEL_READY;
621 rel = fwd ? ch->root_rel : ch->dest_rel;
622 for (copy = rel->head_sent; NULL != copy; copy = next)
624 struct GNUNET_MessageHeader *msg;
627 msg = (struct GNUNET_MessageHeader *) ©[1];
628 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
630 rel_message_free (copy);
634 send_ack (NULL, ch, fwd);
639 * Save a copy to retransmit in case it gets lost.
641 * Initializes all needed callbacks and timers.
643 * @param ch Channel this message goes on.
644 * @param msg Message to copy.
645 * @param fwd Is this fwd traffic?
648 channel_save_copy (struct MeshChannel *ch,
649 const struct GNUNET_MessageHeader *msg,
652 struct MeshChannelReliability *rel;
653 struct MeshReliableMessage *copy;
658 rel = fwd ? ch->root_rel : ch->dest_rel;
660 type = ntohs (msg->type);
661 size = ntohs (msg->size);
663 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
664 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
666 copy->timestamp = GNUNET_TIME_absolute_get ();
669 memcpy (©[1], msg, size);
671 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
672 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
673 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
676 GNUNET_TIME_relative_multiply (rel->expected_delay,
677 MESH_RETRANSMIT_MARGIN);
679 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
680 &channel_retransmit_message,
688 * Send a buffered message to the client, for in order delivery or
689 * as result of client ACK.
691 * @param ch Channel on which to empty the message buffer.
692 * @param c Client to send to.
693 * @param fwd Is this to send FWD data?.
696 send_client_buffered_data (struct MeshChannel *ch,
697 struct MeshClient *c,
700 struct MeshReliableMessage *copy;
701 struct MeshChannelReliability *rel;
703 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
704 rel = fwd ? ch->dest_rel : ch->root_rel;
705 if (GNUNET_NO == rel->client_ready)
707 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
711 copy = rel->head_recv;
712 /* We never buffer channel management messages */
715 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
717 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
719 LOG (GNUNET_ERROR_TYPE_DEBUG,
720 " have %u! now expecting %u\n",
721 copy->mid, rel->mid_recv + 1);
722 send_client_data (ch, msg, fwd);
725 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
730 LOG (GNUNET_ERROR_TYPE_DEBUG,
731 " reliable && don't have %u, next is %u\n",
737 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
744 * Destroy a channel and free all resources.
746 * @param ch Channel to destroy.
749 channel_destroy (struct MeshChannel *ch)
751 struct MeshClient *c;
756 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
757 GMT_2s (ch->t), ch->gid);
763 GML_channel_remove (c, ch->lid_root, ch);
769 GML_channel_remove (c, ch->lid_dest, ch);
772 channel_rel_free_all (ch->root_rel);
773 channel_rel_free_all (ch->dest_rel);
775 GMT_remove_channel (ch->t, ch);
776 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
783 * Create a new channel.
785 * @param t Tunnel this channel is in.
786 * @param owner Client that owns the channel, NULL for foreign channels.
787 * @param lid_root Local ID for root client.
789 * @return A new initialized channel. NULL on error.
791 static struct MeshChannel *
792 channel_new (struct MeshTunnel3 *t,
793 struct MeshClient *owner, MESH_ChannelNumber lid_root)
795 struct MeshChannel *ch;
797 ch = GNUNET_new (struct MeshChannel);
799 ch->lid_root = lid_root;
802 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
806 ch->gid = GMT_get_next_chid (t);
807 GML_channel_add (owner, lid_root, ch);
809 GMT_add_channel (t, ch);
816 * Set options in a channel, extracted from a bit flag field
818 * @param ch Channel to set options to.
819 * @param options Bit array in host byte order.
822 channel_set_options (struct MeshChannel *ch, uint32_t options)
824 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
825 GNUNET_YES : GNUNET_NO;
826 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
827 GNUNET_YES : GNUNET_NO;
833 * Confirm we got a channel create.
835 * @param ch The channel to confirm.
836 * @param fwd Should we send the ACK fwd?
839 channel_send_ack (struct MeshChannel *ch, int fwd)
841 struct GNUNET_MESH_ChannelManage msg;
843 msg.header.size = htons (sizeof (msg));
844 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
845 LOG (GNUNET_ERROR_TYPE_DEBUG,
846 " sending channel %s ack for channel %s:%X\n",
847 fwd ? "FWD" : "BCK", GMT_2s (ch->t),
850 msg.chid = htonl (ch->gid);
851 GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
856 * Iterator for deleting each channel whose client endpoint disconnected.
858 * @param cls Closure (client that has disconnected).
859 * @param key The local channel id (used to access the hashmap).
860 * @param value The value stored at the key (channel to destroy).
862 * @return GNUNET_OK, keep iterating.
865 channel_destroy_iterator (void *cls,
869 struct MeshChannel *ch = value;
870 struct MeshClient *c = cls;
871 struct MeshTunnel3 *t;
873 LOG (GNUNET_ERROR_TYPE_DEBUG,
874 " Channel %X (%X / %X) destroy, due to client %s shutdown.\n",
875 ch->gid, ch->lid_root, ch->lid_dest, GML_2s (c));
880 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
884 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
888 GMCH_send_destroy (ch);
889 channel_destroy (ch);
890 GMT_destroy_if_empty (t);
897 * Handle a loopback message: call the appropriate handler for the message type.
899 * @param ch Channel this message is on.
900 * @param msgh Message header.
901 * @param fwd Is this FWD traffic?
904 handle_loopback (struct MeshChannel *ch,
905 const struct GNUNET_MessageHeader *msgh,
910 type = ntohs (msgh->type);
911 LOG (GNUNET_ERROR_TYPE_DEBUG,
912 "Loopback %s message!\n",
913 GNUNET_MESH_DEBUG_M2S (type));
917 case GNUNET_MESSAGE_TYPE_MESH_DATA:
918 /* Don't send hop ACK, wait for client to ACK */
919 GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
922 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
923 GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
926 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
927 // FIXME store channel in loopback tunnel?
928 GMCH_handle_create ((struct GNUNET_MESH_ChannelCreate *) msgh,
932 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
934 (struct GNUNET_MESH_ChannelManage *) msgh,
938 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
939 GMCH_handle_destroy (ch,
940 (struct GNUNET_MESH_ChannelManage *) msgh,
946 LOG (GNUNET_ERROR_TYPE_DEBUG,
947 "end-to-end message not known (%u)\n",
954 /******************************************************************************/
955 /******************************** API ***********************************/
956 /******************************************************************************/
966 GMCH_get_id (const struct MeshChannel *ch)
973 * Get the channel tunnel.
975 * @param ch Channel to get the tunnel from.
977 * @return tunnel of the channel.
980 GMCH_get_tunnel (const struct MeshChannel *ch)
987 * Get free buffer space towards the client on a specific channel.
990 * @param fwd Is query about FWD traffic?
992 * @return Free buffer space [0 - 64]
995 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
997 struct MeshChannelReliability *rel;
999 rel = fwd ? ch->dest_rel : ch->root_rel;
1001 /* If rel is NULL it means that the end is not yet created,
1002 * most probably is a loopback channel at the point of sending
1003 * the ChannelCreate to itself.
1008 return (64 - rel->n_recv);
1013 * Is the root client for this channel on this peer?
1015 * @param ch Channel.
1016 * @param fwd Is this for fwd traffic?
1018 * @return GNUNET_YES in case it is.
1021 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1023 struct MeshClient *c;
1025 c = fwd ? ch->root : ch->dest;
1031 * Is the destination client for this channel on this peer?
1033 * @param ch Channel.
1034 * @param fwd Is this for fwd traffic?
1036 * @return GNUNET_YES in case it is.
1039 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1041 struct MeshClient *c;
1043 c = fwd ? ch->dest : ch->root;
1049 * Notify the destination client that a new incoming channel was created.
1051 * @param ch Channel that was created.
1054 GMCH_send_create (struct MeshChannel *ch)
1058 if (NULL == ch->dest)
1062 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1063 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1064 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1065 GMT_get_destination (ch->t));
1070 * Notify a client that the channel is no longer valid.
1071 * FIXME send on tunnel if some client == NULL?
1073 * @param ch Channel that is destroyed.
1076 GMCH_send_destroy (struct MeshChannel *ch)
1078 if (NULL != ch->root)
1079 GML_send_channel_destroy (ch->root, ch->lid_root);
1081 if (NULL != ch->dest)
1082 GML_send_channel_destroy (ch->dest, ch->lid_dest);
1087 * Send data on a channel.
1089 * If the destination is local, send it to client, otherwise encrypt and
1093 * @param msg Message.
1094 * @param fwd Is this a fwd (root->dest) message?
1097 GMCH_send_data (struct MeshChannel *ch,
1098 const struct GNUNET_MESH_Data *msg,
1105 * Send an end-to-end ACK message for the most recent in-sequence payload.
1107 * If channel is not reliable, do nothing.
1109 * @param ch Channel this is about.
1110 * @param fwd Is for FWD traffic? (ACK dest->owner)
1113 GMCH_send_ack (struct MeshChannel *ch, int fwd)
1115 struct GNUNET_MESH_DataACK msg;
1116 struct MeshChannelReliability *rel;
1117 struct MeshReliableMessage *copy;
1122 if (GNUNET_NO == ch->reliable)
1126 rel = fwd ? ch->dest_rel : ch->root_rel;
1127 LOG (GNUNET_ERROR_TYPE_DEBUG,
1128 "send_data_ack for %u\n",
1131 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
1132 msg.header.type = htons (type);
1133 msg.header.size = htons (sizeof (msg));
1134 msg.chid = htonl (ch->gid);
1135 msg.mid = htonl (rel->mid_recv - 1);
1137 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1139 if (copy->type != type)
1141 delta = copy->mid - rel->mid_recv;
1144 mask = 0x1LL << delta;
1145 msg.futures |= mask;
1146 LOG (GNUNET_ERROR_TYPE_DEBUG,
1147 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1148 copy->mid, delta, mask, msg.futures);
1150 LOG (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
1152 GMCH_send_prebuilt_message (&msg.header, ch, fwd);
1153 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1160 * @param ch Channel.
1163 GMCH_debug (struct MeshChannel *ch)
1167 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1170 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1171 GMT_2s (ch->t), ch->gid, ch);
1172 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1173 ch->root, ch->root_rel);
1174 if (NULL != ch->root)
1176 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->root));
1177 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1178 ch->root_rel->client_ready ? "YES" : "NO");
1179 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1182 ch->dest, ch->dest_rel);
1183 if (NULL != ch->dest)
1185 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %s\n", GML_2s (ch->dest));
1186 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1187 ch->dest_rel->client_ready ? "YES" : "NO");
1188 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1194 * Handle an ACK given by a client.
1196 * Mark client as ready and send him any buffered data we could have for him.
1198 * @param ch Channel.
1199 * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by root and go BCK)
1202 GMCH_handle_local_ack (struct MeshChannel *ch, int fwd)
1204 struct MeshChannelReliability *rel;
1205 struct MeshClient *c;
1207 rel = fwd ? ch->dest_rel : ch->root_rel;
1208 c = fwd ? ch->dest : ch->root;
1210 rel->client_ready = GNUNET_YES;
1211 send_client_buffered_data (ch, c, fwd);
1212 send_ack (NULL, ch, fwd);
1217 * Handle data given by a client.
1219 * Check whether the client is allowed to send in this tunnel, save if channel
1220 * is reliable and send an ACK to the client if there is still buffer space
1223 * @param ch Channel.
1224 * @param fwd Is this a FWD data?
1226 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1229 GMCH_handle_local_data (struct MeshChannel *ch,
1230 struct MeshClient *c,
1231 struct GNUNET_MessageHeader *message,
1234 struct MeshChannelReliability *rel;
1235 struct GNUNET_MESH_Data *payload;
1236 size_t size = ntohs (message->size);
1237 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
1238 unsigned char cbuf[p2p_size];
1240 /* Is the client in the channel? */
1248 return GNUNET_SYSERR;
1251 rel = fwd ? ch->root_rel : ch->dest_rel;
1253 /* Ok, everything is correct, send the message. */
1254 payload = (struct GNUNET_MESH_Data *) cbuf;
1255 payload->mid = htonl (rel->mid_send);
1257 memcpy (&payload[1], message, size);
1258 payload->header.size = htons (p2p_size);
1259 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
1260 payload->chid = htonl (ch->gid);
1261 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1262 GMCH_send_prebuilt_message (&payload->header, ch, fwd);
1264 if (GNUNET_YES == ch->reliable)
1265 channel_save_copy (ch, &payload->header, fwd);
1266 if (GMT_get_buffer (ch->t, fwd) > 0)
1267 GML_send_ack (c, fwd ? ch->lid_root : ch->lid_dest);
1274 * Handle a channel destroy requested by a client.
1276 * Destroy the channel and the tunnel in case this was the last channel.
1278 * @param ch Channel.
1279 * @param c Client that requested the destruction (to avoid notifying him).
1280 * @param chid Channel ID used.
1283 GMCH_handle_local_destroy (struct MeshChannel *ch,
1284 struct MeshClient *c,
1285 MESH_ChannelNumber chid)
1287 struct MeshTunnel3 *t;
1289 /* Cleanup after the tunnel */
1290 GML_client_delete_channel (c, ch, chid);
1291 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
1295 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
1301 LOG (GNUNET_ERROR_TYPE_ERROR,
1302 " channel %X client %p (%p, %p)\n",
1303 chid, c, ch->root, ch->dest);
1308 channel_destroy (ch);
1309 GMT_destroy_if_empty (t);
1314 * Handle a channel create requested by a client.
1316 * Create the channel and the tunnel in case this was the first0 channel.
1318 * @param c Client that requested the creation (will be the root).
1319 * @param msg Create Channel message.
1322 GMCH_handle_local_create (struct MeshClient *c,
1323 struct GNUNET_MESH_ChannelMessage *msg)
1325 struct MeshChannel *ch;
1326 struct MeshTunnel3 *t;
1327 struct MeshPeer *peer;
1328 MESH_ChannelNumber chid;
1330 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1331 GNUNET_i2s (&msg->peer), ntohl (msg->port));
1332 chid = ntohl (msg->channel_id);
1334 /* Sanity check for duplicate channel IDs */
1335 if (NULL != GML_channel_get (c, chid))
1338 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1342 peer = GMP_get (&msg->peer);
1343 GMP_add_tunnel (peer);
1344 t = GMP_get_tunnel(peer);
1346 if (GMP_get_short_id(peer) == myid)
1348 GMT_change_state (t, MESH_TUNNEL3_READY);
1355 /* Create channel */
1356 ch = channel_new (t, c, chid);
1362 ch->port = ntohl (msg->port);
1363 channel_set_options (ch, ntohl (msg->opt));
1365 /* In unreliable channels, we'll use the DLL to buffer BCK data */
1366 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
1367 ch->root_rel->ch = ch;
1368 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
1370 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
1371 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
1373 /* Send create channel */
1375 struct GNUNET_MESH_ChannelCreate msgcc;
1377 msgcc.header.size = htons (sizeof (msgcc));
1378 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1379 msgcc.chid = htonl (ch->gid);
1380 msgcc.port = msg->port;
1381 msgcc.opt = msg->opt;
1383 GMT_queue_data (t, ch, &msgcc.header, GNUNET_YES);
1388 * Handler for mesh network payload traffic.
1390 * @param ch Channel for the message.
1391 * @param message Unencryted data message.
1392 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1395 GMCH_handle_data (struct MeshChannel *ch,
1396 const struct GNUNET_MESH_Data *msg,
1399 struct MeshChannelReliability *rel;
1400 struct MeshClient *c;
1403 /* Initialize FWD/BCK data */
1404 c = fwd ? ch->dest : ch->root;
1405 rel = fwd ? ch->dest_rel : ch->root_rel;
1413 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1415 mid = ntohl (msg->mid);
1416 LOG (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
1418 if (GNUNET_NO == ch->reliable ||
1419 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
1420 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1422 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1423 if (GNUNET_YES == ch->reliable)
1425 /* Is this the exact next expected messasge? */
1426 if (mid == rel->mid_recv)
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1430 send_client_data (ch, msg, fwd);
1434 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1435 add_buffered_data (msg, rel);
1440 /* Tunnel is unreliable: send to clients directly */
1441 /* FIXME: accept Out Of Order traffic */
1442 rel->mid_recv = mid + 1;
1443 send_client_data (ch, msg, fwd);
1448 GNUNET_break_op (0);
1449 LOG (GNUNET_ERROR_TYPE_DEBUG,
1450 " MID %u not expected (%u - %u), dropping!\n",
1451 mid, rel->mid_recv, rel->mid_recv + 64);
1454 GMCH_send_ack (ch, fwd);
1459 * Handler for mesh network traffic end-to-end ACKs.
1461 * @param t Tunnel on which we got this message.
1462 * @param message Data message.
1463 * @param fwd Is this a fwd ACK? (dest->orig)
1466 GMCH_handle_data_ack (struct MeshChannel *ch,
1467 const struct GNUNET_MESH_DataACK *msg,
1470 struct MeshChannelReliability *rel;
1471 struct MeshReliableMessage *copy;
1472 struct MeshReliableMessage *next;
1476 ack = ntohl (msg->mid);
1477 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1478 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1480 if (GNUNET_YES == fwd)
1494 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1496 if (GMC_is_pid_bigger (copy->mid, ack))
1498 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1499 channel_rel_free_sent (rel, msg);
1503 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
1505 rel_message_free (copy);
1507 /* ACK client if needed */
1508 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
1510 /* If some message was free'd, update the retransmission delay*/
1511 if (GNUNET_YES == work)
1513 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1515 GNUNET_SCHEDULER_cancel (rel->retry_task);
1516 if (NULL == rel->head_sent)
1518 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1522 struct GNUNET_TIME_Absolute new_target;
1523 struct GNUNET_TIME_Relative delay;
1525 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1526 MESH_RETRANSMIT_MARGIN);
1527 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1529 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1531 GNUNET_SCHEDULER_add_delayed (delay,
1532 &channel_retransmit_message,
1543 * Handler for channel create messages.
1545 * @param msg Message.
1546 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1548 struct MeshChannel *
1549 GMCH_handle_create (const struct GNUNET_MESH_ChannelCreate *msg,
1552 MESH_ChannelNumber chid;
1553 struct MeshChannel *ch;
1554 struct MeshClient *c;
1557 chid = ntohl (msg->chid);
1559 /* Create channel */
1560 ch = channel_new (NULL, NULL, 0); /* FIXME pass t */
1562 channel_set_options (ch, ntohl (msg->opt));
1564 /* Find a destination client */
1565 port = ntohl (msg->port);
1566 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
1567 c = GML_client_get_by_port (port);
1570 /* TODO send reject */
1571 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
1572 channel_destroy (ch);
1576 GMCH_add_client (ch, c);
1577 if (GNUNET_YES == ch->reliable)
1578 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1580 GMCH_send_create (ch);
1581 GMCH_send_ack (ch, fwd);
1583 if (GNUNET_NO == ch->dest_rel->client_ready)
1585 GML_send_ack (ch->dest, ch->lid_dest);
1586 ch->dest_rel->client_ready = GNUNET_YES;
1594 * Handler for channel ack messages.
1596 * @param ch Channel.
1597 * @param msg Message.
1598 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1601 GMCH_handle_ack (struct MeshChannel *ch,
1602 const struct GNUNET_MESH_ChannelManage *msg,
1605 channel_confirm (ch, !fwd);
1610 * Handler for channel destroy messages.
1612 * @param ch Channel to be destroyed of.
1613 * @param msg Message.
1614 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1617 GMCH_handle_destroy (struct MeshChannel *ch,
1618 const struct GNUNET_MESH_ChannelManage *msg,
1621 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
1623 /* Not for us (don't destroy twice a half-open loopback channel) */
1627 GMCH_send_destroy (ch);
1628 channel_destroy (ch);
1633 * Sends an already built message on a channel.
1635 * If the channel is on a loopback tunnel, notifies the appropriate destination
1638 * On a normal channel passes the message to the tunnel for encryption and
1639 * sending on a connection.
1641 * This function DOES NOT save the message for retransmission.
1643 * @param message Message to send. Function makes a copy of it.
1644 * @param ch Channel on which this message is transmitted.
1645 * @param fwd Is this a fwd message?
1648 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1649 struct MeshChannel *ch, int fwd)
1651 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
1652 GMT_2s (ch->t), ch->gid, fwd ? "FWD" : "BCK");
1653 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1654 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1656 if (GMT_is_loopback (ch->t))
1658 handle_loopback (ch, message, fwd);
1662 GMT_send_prebuilt_message (message, ch->t, ch, fwd);