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.
24 #include "mesh_protocol_enc.h"
25 #include "gnunet-service-mesh_channel.h"
26 #include "gnunet-service-mesh_local.h"
27 #include "gnunet-service-mesh_tunnel.h"
29 #define LOG(level, ...) GNUNET_log_from(level,"mesh-chn",__VA_ARGS__)
32 * All the states a connection can be in.
37 * Uninitialized status, should never appear in operation.
42 * Connection create message sent, waiting for ACK.
47 * Connection confirmed, ready to carry traffic..
55 * Info needed to retry a message in case it gets lost.
57 struct MeshReliableMessage
60 * Double linked list, FIFO style
62 struct MeshReliableMessage *next;
63 struct MeshReliableMessage *prev;
66 * Type of message (payload, channel management).
71 * Tunnel Reliability queue this message is in.
73 struct MeshChannelReliability *rel;
76 * ID of the message (ACK needed to free)
81 * When was this message issued (to calculate ACK delay)
83 struct GNUNET_TIME_Absolute timestamp;
85 /* struct GNUNET_MESH_Data with payload */
90 * Info about the traffic state for a client in a channel.
92 struct MeshChannelReliability
95 * Channel this is about.
97 struct MeshChannel *ch;
100 * DLL of messages sent and not yet ACK'd.
102 struct MeshReliableMessage *head_sent;
103 struct MeshReliableMessage *tail_sent;
106 * Messages pending to send.
111 * DLL of messages received out of order.
113 struct MeshReliableMessage *head_recv;
114 struct MeshReliableMessage *tail_recv;
122 * Next MID to use for outgoing traffic.
127 * Next MID expected for incoming traffic.
132 * Can we send data to the client?
137 * Task to resend/poll in case no ACK is received.
139 GNUNET_SCHEDULER_TaskIdentifier retry_task;
142 * Counter for exponential backoff.
144 struct GNUNET_TIME_Relative retry_timer;
147 * How long does it usually take to get an ACK.
149 struct GNUNET_TIME_Relative expected_delay;
154 * Struct containing all information regarding a channel to a remote client.
159 * Tunnel this channel is in.
161 struct MeshTunnel2 *t;
164 * Double linked list.
166 struct MeshChannel *next;
167 struct MeshChannel *prev;
170 * Destination port of the channel.
175 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
177 MESH_ChannelNumber gid;
180 * Local tunnel number for root (owner) client.
181 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
183 MESH_ChannelNumber lid_root;
186 * Local tunnel number for local destination clients (incoming number)
187 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
189 MESH_ChannelNumber lid_dest;
194 enum MeshChannelState state;
197 * Is the tunnel bufferless (minimum latency)?
202 * Is the tunnel reliable?
207 * Last time the channel was used
209 struct GNUNET_TIME_Absolute timestamp;
212 * Client owner of the tunnel, if any
214 struct MeshClient *root;
217 * Client destination of the tunnel, if any.
219 struct MeshClient *dest;
222 * Flag to signal the destruction of the channel.
223 * If this is set GNUNET_YES the channel will be destroyed
224 * when the queue is empty.
229 * Total messages pending for this channel, payload or not.
231 unsigned int pending_messages;
235 * Only present (non-NULL) at the owner of a tunnel.
237 struct MeshChannelReliability *root_rel;
241 * Only present (non-NULL) at the destination of a tunnel.
243 struct MeshChannelReliability *dest_rel;
249 * We have received a message out of order, or the client is not ready.
250 * Buffer it until we receive an ACK from the client or the missing
251 * message from the channel.
253 * @param msg Message to buffer (MUST be of type MESH_DATA).
254 * @param rel Reliability data to the corresponding direction.
257 add_buffered_data (const struct GNUNET_MESH_Data *msg,
258 struct MeshChannelReliability *rel)
260 struct MeshReliableMessage *copy;
261 struct MeshReliableMessage *prev;
265 size = ntohs (msg->header.size);
266 mid = ntohl (msg->mid);
268 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
270 copy = GNUNET_malloc (sizeof (*copy) + size);
273 memcpy (©[1], msg, size);
277 // FIXME do something better than O(n), although n < 64...
278 // FIXME start from the end (most messages are the latest ones)
279 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
281 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
282 if (GMC_is_pid_bigger (prev->mid, mid))
284 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
285 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
290 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
291 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
292 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
297 * Send data to a client.
299 * If the client is ready, send directly, otherwise buffer while listening
303 * @param msg Message.
304 * @param fwd Is this a fwd (root->dest) message?
307 send_client_data (struct MeshChannel *ch,
308 const struct GNUNET_MESH_Data *msg,
313 if (ch->dest_rel->client_ready)
314 GML_send_data (ch, msg, ch->dest, ch->lid_dest);
316 add_buffered_data (msg, ch->dest_rel);
320 if (ch->root_rel->client_ready)
321 GML_send_data (ch, msg, ch->root, ch->lid_root);
323 add_buffered_data (msg, ch->root_rel);
329 * Search for a channel among the channels for a client
331 * @param c the client whose channels to search in
332 * @param chid the local id of the channel
334 * @return channel handler, NULL if doesn't exist
336 static struct MeshChannel *
337 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
339 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid);
340 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
343 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
346 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
347 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
348 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
354 * Search for a channel by global ID using full PeerIdentities.
356 * @param t Tunnel containing the channel.
357 * @param chid Public channel number.
359 * @return channel handler, NULL if doesn't exist
361 static struct MeshChannel *
362 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
364 struct MeshChannel *ch;
369 for (ch = t->channel_head; NULL != ch; ch = ch->next)
380 * Add a client to a channel, initializing all needed data structures.
382 * @param ch Channel to which add the client.
383 * @param c Client which to add to the channel.
386 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
388 struct MeshTunnel2 *t = ch->t;
390 if (NULL != ch->dest)
396 /* Assign local id as destination */
397 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
398 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
399 ch->lid_dest = t->next_local_chid++;
400 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
402 /* Store in client's hashmap */
404 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
406 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
412 GNUNET_break (NULL == ch->dest_rel);
413 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
414 ch->dest_rel->ch = ch;
415 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
423 * Is the root client for this channel on this peer?
426 * @param fwd Is this for fwd traffic?
428 * @return GNUNET_YES in case it is.
431 channel_is_origin (struct MeshChannel *ch, int fwd)
433 struct MeshClient *c;
435 c = fwd ? ch->root : ch->dest;
441 * Is the destination client for this channel on this peer?
444 * @param fwd Is this for fwd traffic?
446 * @return GNUNET_YES in case it is.
449 channel_is_terminal (struct MeshChannel *ch, int fwd)
451 struct MeshClient *c;
453 c = fwd ? ch->dest : ch->root;
459 * Get free buffer space towards the client on a specific channel.
462 * @param fwd Is query about FWD traffic?
464 * @return Free buffer space [0 - 64]
467 channel_get_buffer (struct MeshChannel *ch, int fwd)
469 struct MeshChannelReliability *rel;
471 rel = fwd ? ch->dest_rel : ch->root_rel;
473 /* If rel is NULL it means that the end is not yet created,
474 * most probably is a loopback channel at the point of sending
475 * the ChannelCreate to itself.
480 return (64 - rel->n_recv);
487 * Destroy all reliable messages queued for a channel,
488 * during a channel destruction.
489 * Frees the reliability structure itself.
491 * @param rel Reliability data for a channel.
494 channel_rel_free_all (struct MeshChannelReliability *rel)
496 struct MeshReliableMessage *copy;
497 struct MeshReliableMessage *next;
502 for (copy = rel->head_recv; NULL != copy; copy = next)
505 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
508 for (copy = rel->head_sent; NULL != copy; copy = next)
511 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
514 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
515 GNUNET_SCHEDULER_cancel (rel->retry_task);
521 * Mark future messages as ACK'd.
523 * @param rel Reliability data.
524 * @param msg DataACK message with a bitfield of future ACK'd messages.
527 channel_rel_free_sent (struct MeshChannelReliability *rel,
528 const struct GNUNET_MESH_DataACK *msg)
530 struct MeshReliableMessage *copy;
531 struct MeshReliableMessage *next;
538 bitfield = msg->futures;
539 mid = ntohl (msg->mid);
540 LOG (GNUNET_ERROR_TYPE_DEBUG,
541 "free_sent_reliable %u %llX\n",
543 LOG (GNUNET_ERROR_TYPE_DEBUG,
544 " rel %p, head %p\n",
545 rel, rel->head_sent);
546 for (i = 0, copy = rel->head_sent;
547 i < 64 && NULL != copy && 0 != bitfield;
550 LOG (GNUNET_ERROR_TYPE_DEBUG,
551 " trying bit %u (mid %u)\n",
554 if (0 == (bitfield & mask))
557 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
558 /* Bit was set, clear the bit from the bitfield */
561 /* The i-th bit was set. Do we have that copy? */
562 /* Skip copies with mid < target */
563 target = mid + i + 1;
564 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
565 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
568 /* Did we run out of copies? (previously freed, it's ok) */
571 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
575 /* Did we overshoot the target? (previously freed, it's ok) */
576 if (GMC_is_pid_bigger (copy->mid, target))
578 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
582 /* Now copy->mid == target, free it */
584 rel_message_free (copy);
587 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
592 * We haven't received an ACK after a certain time: restransmit the message.
594 * @param cls Closure (MeshReliableMessage with the message to restransmit)
595 * @param tc TaskContext.
598 channel_retransmit_message (void *cls,
599 const struct GNUNET_SCHEDULER_TaskContext *tc)
601 struct MeshChannelReliability *rel = cls;
602 struct MeshReliableMessage *copy;
603 struct MeshPeerQueue *q;
604 struct MeshChannel *ch;
605 struct MeshConnection *c;
606 struct GNUNET_MESH_Data *payload;
607 struct MeshPeer *hop;
610 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
611 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
615 copy = rel->head_sent;
622 /* Search the message to be retransmitted in the outgoing queue.
623 * Check only the queue for the connection that is going to be used,
624 * if the message is stuck in some other connection's queue we shouldn't
626 * - cancelling it and sending the new one doesn't guarantee it's delivery,
627 * the old connection could be temporary stalled or the queue happened to
628 * be long at time of insertion.
629 * - not sending the new one could cause terrible delays the old connection
632 payload = (struct GNUNET_MESH_Data *) ©[1];
633 fwd = (rel == ch->root_rel);
634 c = tunnel_get_connection (ch->t, fwd);
635 hop = connection_get_hop (c, fwd);
636 for (q = hop->queue_head; NULL != q; q = q->next)
638 if (ntohs (payload->header.type) == q->type && ch == q->ch)
640 struct GNUNET_MESH_Data *queued_data = q->cls;
642 if (queued_data->mid == payload->mid)
647 /* Message not found in the queue that we are going to use. */
650 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
652 send_prebuilt_message_channel (&payload->header, ch, fwd);
653 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
657 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
660 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
661 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
662 &channel_retransmit_message,
668 * Send ACK on one or more connections due to buffer space to the client.
670 * Iterates all connections of the tunnel and sends ACKs appropriately.
672 * @param ch Channel which has some free buffer space.
673 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
676 channel_send_connections_ack (struct MeshChannel *ch,
680 struct MeshTunnel2 *t = ch->t;
681 struct MeshConnection *c;
682 struct MeshFlowControl *fc;
685 uint32_t allow_per_connection;
688 LOG (GNUNET_ERROR_TYPE_DEBUG,
689 "Channel send connection %s ack on %s:%X\n",
690 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
692 /* Count connections, how many messages are already allowed */
693 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
695 fc = fwd ? &c->fwd_fc : &c->bck_fc;
696 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
701 allowed += fc->last_ack_sent - fc->last_pid_recv;
705 /* Make sure there is no overflow */
706 if (allowed > buffer)
712 /* Authorize connections to send more data */
713 to_allow = buffer - allowed;
715 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
717 allow_per_connection = to_allow/cs;
718 to_allow -= allow_per_connection;
720 fc = fwd ? &c->fwd_fc : &c->bck_fc;
721 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
725 connection_send_ack (c, allow_per_connection, fwd);
728 LOG (GNUNET_ERROR_TYPE_DEBUG,
729 "Channel send connection %s ack on %s:%X\n",
730 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
731 GNUNET_break (to_allow == 0);
736 * Destroy a reliable message after it has been acknowledged, either by
737 * direct mid ACK or bitfield. Updates the appropriate data structures and
738 * timers and frees all memory.
740 * @param copy Message that is no longer needed: remote peer got it.
743 rel_message_free (struct MeshReliableMessage *copy)
745 struct MeshChannelReliability *rel;
746 struct GNUNET_TIME_Relative time;
749 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
750 rel->expected_delay.rel_value_us *= 7;
751 rel->expected_delay.rel_value_us += time.rel_value_us;
752 rel->expected_delay.rel_value_us /= 8;
754 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
755 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
756 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
757 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
758 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
759 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
761 rel->retry_timer = rel->expected_delay;
762 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
769 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
771 * @param ch Channel to mark as ready.
772 * @param fwd Was the CREATE message sent fwd?
775 channel_confirm (struct MeshChannel *ch, int fwd)
777 struct MeshChannelReliability *rel;
778 struct MeshReliableMessage *copy;
779 struct MeshReliableMessage *next;
781 LOG (GNUNET_ERROR_TYPE_DEBUG,
782 " channel confirm %s %s:%X\n",
783 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
784 ch->state = MESH_CHANNEL_READY;
786 rel = fwd ? ch->root_rel : ch->dest_rel;
787 for (copy = rel->head_sent; NULL != copy; copy = next)
789 struct GNUNET_MessageHeader *msg;
792 msg = (struct GNUNET_MessageHeader *) ©[1];
793 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
795 rel_message_free (copy);
799 send_ack (NULL, ch, fwd);
804 * Save a copy to retransmit in case it gets lost.
806 * Initializes all needed callbacks and timers.
808 * @param ch Channel this message goes on.
809 * @param msg Message to copy.
810 * @param fwd Is this fwd traffic?
813 channel_save_copy (struct MeshChannel *ch,
814 const struct GNUNET_MessageHeader *msg,
817 struct MeshChannelReliability *rel;
818 struct MeshReliableMessage *copy;
823 rel = fwd ? ch->root_rel : ch->dest_rel;
825 type = ntohs (msg->type);
826 size = ntohs (msg->size);
828 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
829 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
831 copy->timestamp = GNUNET_TIME_absolute_get ();
834 memcpy (©[1], msg, size);
836 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
837 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
838 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
841 GNUNET_TIME_relative_multiply (rel->expected_delay,
842 MESH_RETRANSMIT_MARGIN);
844 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
845 &channel_retransmit_message,
853 * Send a buffered message to the client, for in order delivery or
854 * as result of client ACK.
856 * @param ch Channel on which to empty the message buffer.
857 * @param c Client to send to.
858 * @param rel Reliability structure to corresponding peer.
859 * If rel == bck_rel, this is FWD data.
862 send_client_buffered_data (struct MeshChannel *ch,
863 struct MeshClient *c,
866 struct MeshReliableMessage *copy;
867 struct MeshChannelReliability *rel;
869 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
870 rel = fwd ? ch->dest_rel : ch->root_rel;
871 if (GNUNET_NO == rel->client_ready)
873 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
877 copy = rel->head_recv;
878 /* We never buffer channel management messages */
881 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
883 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
885 LOG (GNUNET_ERROR_TYPE_DEBUG,
886 " have %u! now expecting %u\n",
887 copy->mid, rel->mid_recv + 1);
888 send_client_data (ch, msg, fwd);
891 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
896 LOG (GNUNET_ERROR_TYPE_DEBUG,
897 " reliable && don't have %u, next is %u\n",
903 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
910 * Destroy a channel and free all resources.
912 * @param ch Channel to destroy.
915 channel_destroy (struct MeshChannel *ch)
917 struct MeshClient *c;
922 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
923 peer2s (ch->t->peer), ch->gid);
929 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
940 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
947 channel_rel_free_all (ch->root_rel);
948 channel_rel_free_all (ch->dest_rel);
950 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
951 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
958 * Create a new channel.
960 * @param t Tunnel this channel is in.
961 * @param owner Client that owns the channel, NULL for foreign channels.
962 * @param lid_root Local ID for root client.
964 * @return A new initialized channel. NULL on error.
966 static struct MeshChannel *
967 channel_new (struct MeshTunnel2 *t,
968 struct MeshClient *owner, MESH_ChannelNumber lid_root)
970 struct MeshChannel *ch;
972 ch = GNUNET_new (struct MeshChannel);
974 ch->lid_root = lid_root;
977 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
981 while (NULL != channel_get (t, t->next_chid))
983 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists (%p)...\n",
984 t->next_chid, channel_get (t, t->next_chid));
985 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
987 ch->gid = t->next_chid;
988 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
991 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
992 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
995 channel_destroy (ch);
996 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
1000 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
1007 * Set options in a channel, extracted from a bit flag field
1009 * @param ch Channel to set options to.
1010 * @param options Bit array in host byte order.
1013 channel_set_options (struct MeshChannel *ch, uint32_t options)
1015 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
1016 GNUNET_YES : GNUNET_NO;
1017 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
1018 GNUNET_YES : GNUNET_NO;
1024 * Confirm we got a channel create.
1026 * @param ch The channel to confirm.
1027 * @param fwd Should we send the ACK fwd?
1030 channel_send_ack (struct MeshChannel *ch, int fwd)
1032 struct GNUNET_MESH_ChannelManage msg;
1034 msg.header.size = htons (sizeof (msg));
1035 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
1036 LOG (GNUNET_ERROR_TYPE_DEBUG,
1037 " sending channel %s ack for channel %s:%X\n",
1038 fwd ? "FWD" : "BCK", peer2s (ch->t->peer),
1041 msg.chid = htonl (ch->gid);
1042 GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
1047 * Send a message to all clients (local and remote) of this channel
1048 * notifying that the channel is no longer valid.
1050 * If some peer or client should not receive the message,
1051 * should be zero'ed out before calling this function.
1053 * @param ch The channel whose clients to notify.
1056 channel_send_destroy (struct MeshChannel *ch)
1058 struct GNUNET_MESH_ChannelManage msg;
1060 msg.header.size = htons (sizeof (msg));
1061 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1062 LOG (GNUNET_ERROR_TYPE_DEBUG,
1063 " sending channel destroy for channel %s:%X\n",
1064 peer2s (ch->t->peer),
1067 if (channel_is_terminal (ch, GNUNET_NO))
1069 if (NULL != ch->root && GNUNET_NO == ch->root->shutting_down)
1071 msg.chid = htonl (ch->lid_root);
1072 send_local_channel_destroy (ch, GNUNET_NO);
1077 msg.chid = htonl (ch->gid);
1078 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO);
1081 if (channel_is_terminal (ch, GNUNET_YES))
1083 if (NULL != ch->dest && GNUNET_NO == ch->dest->shutting_down)
1085 msg.chid = htonl (ch->lid_dest);
1086 send_local_channel_destroy (ch, GNUNET_YES);
1091 msg.chid = htonl (ch->gid);
1092 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES);
1098 * Iterator for deleting each channel whose client endpoint disconnected.
1100 * @param cls Closure (client that has disconnected).
1101 * @param key The local channel id (used to access the hashmap).
1102 * @param value The value stored at the key (channel to destroy).
1104 * @return GNUNET_OK, keep iterating.
1107 channel_destroy_iterator (void *cls,
1111 struct MeshChannel *ch = value;
1112 struct MeshClient *c = cls;
1113 struct MeshTunnel2 *t;
1115 LOG (GNUNET_ERROR_TYPE_DEBUG,
1116 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
1117 ch->gid, ch->lid_root, ch->lid_dest, c->id);
1122 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
1126 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
1130 GMCH_send_destroy (ch);
1131 channel_send_destroy (ch);
1132 channel_destroy (ch);
1133 tunnel_destroy_if_empty (t);
1140 * Sends an already built message on a channel, properly registering
1141 * all used resources and encrypting the message with the tunnel's key.
1143 * @param message Message to send. Function makes a copy of it.
1144 * @param ch Channel on which this message is transmitted.
1145 * @param fwd Is this a fwd message?
1148 send (const struct GNUNET_MessageHeader *message,
1149 struct MeshChannel *ch, int fwd)
1151 struct GNUNET_MESH_Encrypted *msg;
1152 size_t size = ntohs (message->size);
1153 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1157 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
1158 peer2s (ch->t->peer), ch->gid, fwd ? "FWD" : "BCK");
1159 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1160 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1162 if (channel_is_terminal (ch, fwd) || ch->t->peer->id == myid)
1164 handle_decrypted (ch->t, message, fwd);
1168 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1169 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1171 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1172 msg->header.type = htons (type);
1173 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1174 msg->iv = GNUNET_htonll (iv);
1175 GMT_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1176 GMT_send_prebuilt_message (msg, ch->t, ch, fwd);
1181 * Count channels in a DLL.
1183 * @param head Head of the DLL.
1186 GMCH_count (const struct MeshChannel *head)
1189 struct MeshChannel *iter;
1191 for (count = 0, iter = head; NULL != iter; iter = iter->next, count++);
1198 * Notify the destination client that a new incoming channel was created.
1200 * @param ch Channel that was created.
1203 GMCH_send_create (struct MeshChannel *ch)
1205 struct GNUNET_MESH_ChannelMessage msg;
1206 struct MeshTunnel2 *t = ch->t;
1209 if (NULL == ch->dest)
1213 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1214 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1215 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1216 GNUNET_PEER_resolve2 (t->peer->id));
1221 * Notify a client that the channel is no longer valid.
1223 * @param ch Channel that is destroyed.
1224 * @param fwd Forward notification (owner->dest)?
1227 GMCH_send_destroy (struct MeshChannel *ch, int fwd)
1229 struct GNUNET_MeshClient *c = fwd ? ch->dest : ch->root;
1230 uint32_t id = fwd ? ch->lid_dest : ch->lid_root;
1234 // TODO: send on connection?
1238 GML_send_channel_destroy (c, id);
1243 * Send data on a channel.
1245 * If the destination is local, send it to client, otherwise encrypt and
1249 * @param msg Message.
1250 * @param fwd Is this a fwd (root->dest) message?
1253 GMCH_send_data (struct MeshChannel *ch,
1254 const struct GNUNET_MESH_Data *msg,
1261 * Send an end-to-end ACK message for the most recent in-sequence payload.
1263 * If channel is not reliable, do nothing.
1265 * @param ch Channel this is about.
1266 * @param fwd Is for FWD traffic? (ACK dest->owner)
1269 GMCH_send_ack (struct MeshChannel *ch, int fwd)
1271 struct GNUNET_MESH_DataACK msg;
1272 struct MeshChannelReliability *rel;
1273 struct MeshReliableMessage *copy;
1278 if (GNUNET_NO == ch->reliable)
1282 rel = fwd ? ch->dest_rel : ch->root_rel;
1283 LOG (GNUNET_ERROR_TYPE_DEBUG,
1284 "send_data_ack for %u\n",
1287 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
1288 msg.header.type = htons (type);
1289 msg.header.size = htons (sizeof (msg));
1290 msg.chid = htonl (ch->gid);
1291 msg.mid = htonl (rel->mid_recv - 1);
1293 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1295 if (copy->type != type)
1297 delta = copy->mid - rel->mid_recv;
1300 mask = 0x1LL << delta;
1301 msg.futures |= mask;
1302 LOG (GNUNET_ERROR_TYPE_DEBUG,
1303 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1304 copy->mid, delta, mask, msg.futures);
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
1308 send_prebuilt_message_channel (&msg.header, ch, fwd);
1309 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1316 * @param ch Channel.
1319 GMCH_debug (struct MeshChannel *ch)
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1326 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1327 peer2s (ch->t->peer), ch->gid, ch);
1328 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1329 ch->root, ch->root_rel);
1330 if (NULL != ch->root)
1332 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->root->id);
1333 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1334 ch->root_rel->client_ready ? "YES" : "NO");
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1337 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1338 ch->dest, ch->dest_rel);
1339 if (NULL != ch->dest)
1341 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->dest->id);
1342 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1343 ch->dest_rel->client_ready ? "YES" : "NO");
1344 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1350 * Handler for mesh network payload traffic.
1352 * @param t Tunnel on which we got this message.
1353 * @param message Unencryted data message.
1354 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1357 GMCH_handle_data (struct MeshTunnel2 *t,
1358 const struct GNUNET_MESH_Data *msg,
1361 struct MeshChannelReliability *rel;
1362 struct MeshChannel *ch;
1363 struct MeshClient *c;
1369 size = ntohs (msg->header.size);
1371 sizeof (struct GNUNET_MESH_Data) +
1372 sizeof (struct GNUNET_MessageHeader))
1377 type = ntohs (msg->header.type);
1378 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
1379 GNUNET_MESH_DEBUG_M2S (type));
1380 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
1381 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
1384 ch = channel_get (t, ntohl (msg->chid));
1387 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
1388 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1393 /* Initialize FWD/BCK data */
1394 c = fwd ? ch->dest : ch->root;
1395 rel = fwd ? ch->dest_rel : ch->root_rel;
1403 GMT_change_state (t, MESH_TUNNEL_READY);
1405 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1407 mid = ntohl (msg->mid);
1408 LOG (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
1410 if (GNUNET_NO == ch->reliable ||
1411 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
1412 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1414 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1415 if (GNUNET_YES == ch->reliable)
1417 /* Is this the exact next expected messasge? */
1418 if (mid == rel->mid_recv)
1420 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1422 send_client_data (ch, msg, fwd);
1426 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1427 add_buffered_data (msg, rel);
1432 /* Tunnel is unreliable: send to clients directly */
1433 /* FIXME: accept Out Of Order traffic */
1434 rel->mid_recv = mid + 1;
1435 send_client_data (ch, msg, fwd);
1440 GNUNET_break_op (0);
1441 LOG (GNUNET_ERROR_TYPE_DEBUG,
1442 " MID %u not expected (%u - %u), dropping!\n",
1443 mid, rel->mid_recv, rel->mid_recv + 64);
1446 GMCH_send_ack (ch, fwd);
1451 * Handler for mesh network traffic end-to-end ACKs.
1453 * @param t Tunnel on which we got this message.
1454 * @param message Data message.
1455 * @param fwd Is this a fwd ACK? (dest->orig)
1458 GMCH_handle_data_ack (struct MeshTunnel2 *t,
1459 const struct GNUNET_MESH_DataACK *msg,
1462 struct MeshChannelReliability *rel;
1463 struct MeshReliableMessage *copy;
1464 struct MeshReliableMessage *next;
1465 struct MeshChannel *ch;
1470 type = ntohs (msg->header.type);
1471 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
1472 GNUNET_MESH_DEBUG_M2S (type));
1473 ch = channel_get (t, ntohl (msg->chid));
1476 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
1479 ack = ntohl (msg->mid);
1480 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1481 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1483 if (GNUNET_YES == fwd)
1497 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1499 if (GMC_is_pid_bigger (copy->mid, ack))
1501 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1502 channel_rel_free_sent (rel, msg);
1506 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
1508 rel_message_free (copy);
1510 /* ACK client if needed */
1511 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
1513 /* If some message was free'd, update the retransmission delay*/
1514 if (GNUNET_YES == work)
1516 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1518 GNUNET_SCHEDULER_cancel (rel->retry_task);
1519 if (NULL == rel->head_sent)
1521 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1525 struct GNUNET_TIME_Absolute new_target;
1526 struct GNUNET_TIME_Relative delay;
1528 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1529 MESH_RETRANSMIT_MARGIN);
1530 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1532 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1534 GNUNET_SCHEDULER_add_delayed (delay,
1535 &channel_retransmit_message,
1546 * Handler for channel create messages.
1548 * @param t Tunnel this channel is to be created in.
1549 * @param msg Message.
1550 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1553 GMCH_handle_create (struct MeshTunnel2 *t,
1554 struct GNUNET_MESH_ChannelCreate *msg,
1557 MESH_ChannelNumber chid;
1558 struct MeshChannel *ch;
1559 struct MeshClient *c;
1562 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
1563 /* Check message size */
1564 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
1566 GNUNET_break_op (0);
1570 /* Check if channel exists */
1571 chid = ntohl (msg->chid);
1572 LOG (GNUNET_ERROR_TYPE_DEBUG, " chid %u\n", chid);
1573 ch = channel_get (t, chid);
1576 /* Probably a retransmission, safe to ignore */
1577 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
1578 if (NULL != ch->dest)
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n");
1581 GMCH_send_ack (ch, !fwd);
1587 /* Create channel */
1588 ch = channel_new (t, NULL, 0);
1590 channel_set_options (ch, ntohl (msg->opt));
1593 /* Find a destination client */
1594 port = ntohl (msg->port);
1595 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
1596 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
1599 /* TODO send reject */
1600 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
1605 channel_add_client (ch, c);
1606 if (GNUNET_YES == ch->reliable)
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1609 GMCH_send_create (ch);
1610 GMCH_send_ack (ch, fwd);
1611 GML_send_ack (ch, !fwd);
1616 * Handler for channel ack messages.
1618 * @param t Tunnel this channel is to be created in.
1619 * @param msg Message.
1620 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1623 GMCH_handle_ack (struct MeshTunnel2 *t,
1624 struct GNUNET_MESH_ChannelManage *msg,
1627 MESH_ChannelNumber chid;
1628 struct MeshChannel *ch;
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
1631 /* Check message size */
1632 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
1634 GNUNET_break_op (0);
1638 /* Check if channel exists */
1639 chid = ntohl (msg->chid);
1640 ch = channel_get (t, chid);
1643 GNUNET_break_op (0);
1644 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
1648 channel_confirm (ch, !fwd);
1653 * Handler for channel destroy messages.
1655 * @param t Tunnel this channel is to be destroyed of.
1656 * @param msg Message.
1657 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1660 GMCH_handle_destroy (struct MeshTunnel2 *t,
1661 struct GNUNET_MESH_ChannelManage *msg,
1664 MESH_ChannelNumber chid;
1665 struct MeshChannel *ch;
1667 /* Check message size */
1668 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
1670 GNUNET_break_op (0);
1674 /* Check if channel exists */
1675 chid = ntohl (msg->chid);
1676 ch = channel_get (t, chid);
1679 /* Probably a retransmission, safe to ignore */
1682 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
1684 /* Not for us (don't destroy twice a half-open loopback channel) */
1688 GMCH_send_destroy (ch, fwd);
1689 channel_destroy (ch);