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"
31 * All the states a connection can be in.
36 * Uninitialized status, should never appear in operation.
41 * Connection create message sent, waiting for ACK.
46 * Connection confirmed, ready to carry traffic..
54 * Info needed to retry a message in case it gets lost.
56 struct MeshReliableMessage
59 * Double linked list, FIFO style
61 struct MeshReliableMessage *next;
62 struct MeshReliableMessage *prev;
65 * Type of message (payload, channel management).
70 * Tunnel Reliability queue this message is in.
72 struct MeshChannelReliability *rel;
75 * ID of the message (ACK needed to free)
80 * When was this message issued (to calculate ACK delay)
82 struct GNUNET_TIME_Absolute timestamp;
84 /* struct GNUNET_MESH_Data with payload */
89 * Info about the traffic state for a client in a channel.
91 struct MeshChannelReliability
94 * Channel this is about.
96 struct MeshChannel *ch;
99 * DLL of messages sent and not yet ACK'd.
101 struct MeshReliableMessage *head_sent;
102 struct MeshReliableMessage *tail_sent;
105 * Messages pending to send.
110 * DLL of messages received out of order.
112 struct MeshReliableMessage *head_recv;
113 struct MeshReliableMessage *tail_recv;
121 * Next MID to use for outgoing traffic.
126 * Next MID expected for incoming traffic.
131 * Can we send data to the client?
136 * Task to resend/poll in case no ACK is received.
138 GNUNET_SCHEDULER_TaskIdentifier retry_task;
141 * Counter for exponential backoff.
143 struct GNUNET_TIME_Relative retry_timer;
146 * How long does it usually take to get an ACK.
148 struct GNUNET_TIME_Relative expected_delay;
153 * Struct containing all information regarding a channel to a remote client.
158 * Tunnel this channel is in.
160 struct MeshTunnel2 *t;
163 * Double linked list.
165 struct MeshChannel *next;
166 struct MeshChannel *prev;
169 * Destination port of the channel.
174 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
176 MESH_ChannelNumber gid;
179 * Local tunnel number for root (owner) client.
180 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
182 MESH_ChannelNumber lid_root;
185 * Local tunnel number for local destination clients (incoming number)
186 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
188 MESH_ChannelNumber lid_dest;
193 enum MeshChannelState state;
196 * Is the tunnel bufferless (minimum latency)?
201 * Is the tunnel reliable?
206 * Last time the channel was used
208 struct GNUNET_TIME_Absolute timestamp;
211 * Client owner of the tunnel, if any
213 struct MeshClient *root;
216 * Client destination of the tunnel, if any.
218 struct MeshClient *dest;
221 * Flag to signal the destruction of the channel.
222 * If this is set GNUNET_YES the channel will be destroyed
223 * when the queue is empty.
228 * Total messages pending for this channel, payload or not.
230 unsigned int pending_messages;
234 * Only present (non-NULL) at the owner of a tunnel.
236 struct MeshChannelReliability *root_rel;
240 * Only present (non-NULL) at the destination of a tunnel.
242 struct MeshChannelReliability *dest_rel;
248 * We have received a message out of order, or the client is not ready.
249 * Buffer it until we receive an ACK from the client or the missing
250 * message from the channel.
252 * @param msg Message to buffer (MUST be of type MESH_DATA).
253 * @param rel Reliability data to the corresponding direction.
256 add_buffered_data (const struct GNUNET_MESH_Data *msg,
257 struct MeshChannelReliability *rel)
259 struct MeshReliableMessage *copy;
260 struct MeshReliableMessage *prev;
264 size = ntohs (msg->header.size);
265 mid = ntohl (msg->mid);
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
269 copy = GNUNET_malloc (sizeof (*copy) + size);
272 memcpy (©[1], msg, size);
276 // FIXME do something better than O(n), although n < 64...
277 // FIXME start from the end (most messages are the latest ones)
278 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
281 if (GMC_is_pid_bigger (prev->mid, mid))
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
284 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
290 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
296 * Send data to a client.
298 * If the client is ready, send directly, otherwise buffer while listening
302 * @param msg Message.
303 * @param fwd Is this a fwd (root->dest) message?
306 send_client_data (struct MeshChannel *ch,
307 const struct GNUNET_MESH_Data *msg,
312 if (ch->dest_rel->client_ready)
313 GML_send_data (ch, msg, ch->dest, ch->lid_dest);
315 add_buffered_data (msg, ch->dest_rel);
319 if (ch->root_rel->client_ready)
320 GML_send_data (ch, msg, ch->root, ch->lid_root);
322 add_buffered_data (msg, ch->root_rel);
328 * Search for a channel among the channels for a client
330 * @param c the client whose channels to search in
331 * @param chid the local id of the channel
333 * @return channel handler, NULL if doesn't exist
335 static struct MeshChannel *
336 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid);
339 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
345 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
346 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
347 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
353 * Search for a channel by global ID using full PeerIdentities.
355 * @param t Tunnel containing the channel.
356 * @param chid Public channel number.
358 * @return channel handler, NULL if doesn't exist
360 static struct MeshChannel *
361 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
363 struct MeshChannel *ch;
368 for (ch = t->channel_head; NULL != ch; ch = ch->next)
379 * Add a client to a channel, initializing all needed data structures.
381 * @param ch Channel to which add the client.
382 * @param c Client which to add to the channel.
385 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
387 struct MeshTunnel2 *t = ch->t;
389 if (NULL != ch->dest)
395 /* Assign local id as destination */
396 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
397 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
398 ch->lid_dest = t->next_local_chid++;
399 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
401 /* Store in client's hashmap */
403 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
405 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
411 GNUNET_break (NULL == ch->dest_rel);
412 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
413 ch->dest_rel->ch = ch;
414 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
422 * Is the root client for this channel on this peer?
425 * @param fwd Is this for fwd traffic?
427 * @return GNUNET_YES in case it is.
430 channel_is_origin (struct MeshChannel *ch, int fwd)
432 struct MeshClient *c;
434 c = fwd ? ch->root : ch->dest;
440 * Is the destination client for this channel on this peer?
443 * @param fwd Is this for fwd traffic?
445 * @return GNUNET_YES in case it is.
448 channel_is_terminal (struct MeshChannel *ch, int fwd)
450 struct MeshClient *c;
452 c = fwd ? ch->dest : ch->root;
458 * Get free buffer space towards the client on a specific channel.
461 * @param fwd Is query about FWD traffic?
463 * @return Free buffer space [0 - 64]
466 channel_get_buffer (struct MeshChannel *ch, int fwd)
468 struct MeshChannelReliability *rel;
470 rel = fwd ? ch->dest_rel : ch->root_rel;
472 /* If rel is NULL it means that the end is not yet created,
473 * most probably is a loopback channel at the point of sending
474 * the ChannelCreate to itself.
479 return (64 - rel->n_recv);
486 * Destroy all reliable messages queued for a channel,
487 * during a channel destruction.
488 * Frees the reliability structure itself.
490 * @param rel Reliability data for a channel.
493 channel_rel_free_all (struct MeshChannelReliability *rel)
495 struct MeshReliableMessage *copy;
496 struct MeshReliableMessage *next;
501 for (copy = rel->head_recv; NULL != copy; copy = next)
504 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
507 for (copy = rel->head_sent; NULL != copy; copy = next)
510 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
513 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
514 GNUNET_SCHEDULER_cancel (rel->retry_task);
520 * Mark future messages as ACK'd.
522 * @param rel Reliability data.
523 * @param msg DataACK message with a bitfield of future ACK'd messages.
526 channel_rel_free_sent (struct MeshChannelReliability *rel,
527 const struct GNUNET_MESH_DataACK *msg)
529 struct MeshReliableMessage *copy;
530 struct MeshReliableMessage *next;
537 bitfield = msg->futures;
538 mid = ntohl (msg->mid);
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540 "free_sent_reliable %u %llX\n",
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 " rel %p, head %p\n",
544 rel, rel->head_sent);
545 for (i = 0, copy = rel->head_sent;
546 i < 64 && NULL != copy && 0 != bitfield;
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550 " trying bit %u (mid %u)\n",
553 if (0 == (bitfield & mask))
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
557 /* Bit was set, clear the bit from the bitfield */
560 /* The i-th bit was set. Do we have that copy? */
561 /* Skip copies with mid < target */
562 target = mid + i + 1;
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
564 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
567 /* Did we run out of copies? (previously freed, it's ok) */
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
574 /* Did we overshoot the target? (previously freed, it's ok) */
575 if (GMC_is_pid_bigger (copy->mid, target))
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
581 /* Now copy->mid == target, free it */
583 rel_message_free (copy);
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
591 * We haven't received an ACK after a certain time: restransmit the message.
593 * @param cls Closure (MeshReliableMessage with the message to restransmit)
594 * @param tc TaskContext.
597 channel_retransmit_message (void *cls,
598 const struct GNUNET_SCHEDULER_TaskContext *tc)
600 struct MeshChannelReliability *rel = cls;
601 struct MeshReliableMessage *copy;
602 struct MeshPeerQueue *q;
603 struct MeshChannel *ch;
604 struct MeshConnection *c;
605 struct GNUNET_MESH_Data *payload;
606 struct MeshPeer *hop;
609 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
610 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
614 copy = rel->head_sent;
621 /* Search the message to be retransmitted in the outgoing queue.
622 * Check only the queue for the connection that is going to be used,
623 * if the message is stuck in some other connection's queue we shouldn't
625 * - cancelling it and sending the new one doesn't guarantee it's delivery,
626 * the old connection could be temporary stalled or the queue happened to
627 * be long at time of insertion.
628 * - not sending the new one could cause terrible delays the old connection
631 payload = (struct GNUNET_MESH_Data *) ©[1];
632 fwd = (rel == ch->root_rel);
633 c = tunnel_get_connection (ch->t, fwd);
634 hop = connection_get_hop (c, fwd);
635 for (q = hop->queue_head; NULL != q; q = q->next)
637 if (ntohs (payload->header.type) == q->type && ch == q->ch)
639 struct GNUNET_MESH_Data *queued_data = q->cls;
641 if (queued_data->mid == payload->mid)
646 /* Message not found in the queue that we are going to use. */
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
651 send_prebuilt_message_channel (&payload->header, ch, fwd);
652 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
659 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
660 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
661 &channel_retransmit_message,
667 * Send ACK on one or more connections due to buffer space to the client.
669 * Iterates all connections of the tunnel and sends ACKs appropriately.
671 * @param ch Channel which has some free buffer space.
672 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
675 channel_send_connections_ack (struct MeshChannel *ch,
679 struct MeshTunnel2 *t = ch->t;
680 struct MeshConnection *c;
681 struct MeshFlowControl *fc;
684 uint32_t allow_per_connection;
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 "Channel send connection %s ack on %s:%X\n",
689 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
691 /* Count connections, how many messages are already allowed */
692 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
694 fc = fwd ? &c->fwd_fc : &c->bck_fc;
695 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
700 allowed += fc->last_ack_sent - fc->last_pid_recv;
704 /* Make sure there is no overflow */
705 if (allowed > buffer)
711 /* Authorize connections to send more data */
712 to_allow = buffer - allowed;
714 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
716 allow_per_connection = to_allow/cs;
717 to_allow -= allow_per_connection;
719 fc = fwd ? &c->fwd_fc : &c->bck_fc;
720 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
724 connection_send_ack (c, allow_per_connection, fwd);
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 "Channel send connection %s ack on %s:%X\n",
729 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
730 GNUNET_break (to_allow == 0);
735 * Destroy a reliable message after it has been acknowledged, either by
736 * direct mid ACK or bitfield. Updates the appropriate data structures and
737 * timers and frees all memory.
739 * @param copy Message that is no longer needed: remote peer got it.
742 rel_message_free (struct MeshReliableMessage *copy)
744 struct MeshChannelReliability *rel;
745 struct GNUNET_TIME_Relative time;
748 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
749 rel->expected_delay.rel_value_us *= 7;
750 rel->expected_delay.rel_value_us += time.rel_value_us;
751 rel->expected_delay.rel_value_us /= 8;
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
756 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
758 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
760 rel->retry_timer = rel->expected_delay;
761 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
768 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
770 * @param ch Channel to mark as ready.
771 * @param fwd Was the CREATE message sent fwd?
774 channel_confirm (struct MeshChannel *ch, int fwd)
776 struct MeshChannelReliability *rel;
777 struct MeshReliableMessage *copy;
778 struct MeshReliableMessage *next;
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781 " channel confirm %s %s:%X\n",
782 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
783 ch->state = MESH_CHANNEL_READY;
785 rel = fwd ? ch->root_rel : ch->dest_rel;
786 for (copy = rel->head_sent; NULL != copy; copy = next)
788 struct GNUNET_MessageHeader *msg;
791 msg = (struct GNUNET_MessageHeader *) ©[1];
792 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
794 rel_message_free (copy);
798 send_ack (NULL, ch, fwd);
803 * Save a copy to retransmit in case it gets lost.
805 * Initializes all needed callbacks and timers.
807 * @param ch Channel this message goes on.
808 * @param msg Message to copy.
809 * @param fwd Is this fwd traffic?
812 channel_save_copy (struct MeshChannel *ch,
813 const struct GNUNET_MessageHeader *msg,
816 struct MeshChannelReliability *rel;
817 struct MeshReliableMessage *copy;
822 rel = fwd ? ch->root_rel : ch->dest_rel;
824 type = ntohs (msg->type);
825 size = ntohs (msg->size);
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
828 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
830 copy->timestamp = GNUNET_TIME_absolute_get ();
833 memcpy (©[1], msg, size);
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
836 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
837 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
840 GNUNET_TIME_relative_multiply (rel->expected_delay,
841 MESH_RETRANSMIT_MARGIN);
843 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
844 &channel_retransmit_message,
852 * Send a buffered message to the client, for in order delivery or
853 * as result of client ACK.
855 * @param ch Channel on which to empty the message buffer.
856 * @param c Client to send to.
857 * @param rel Reliability structure to corresponding peer.
858 * If rel == bck_rel, this is FWD data.
861 send_client_buffered_data (struct MeshChannel *ch,
862 struct MeshClient *c,
865 struct MeshReliableMessage *copy;
866 struct MeshChannelReliability *rel;
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
869 rel = fwd ? ch->dest_rel : ch->root_rel;
870 if (GNUNET_NO == rel->client_ready)
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
876 copy = rel->head_recv;
877 /* We never buffer channel management messages */
880 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
882 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
885 " have %u! now expecting %u\n",
886 copy->mid, rel->mid_recv + 1);
887 send_client_data (ch, msg, fwd);
890 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 " reliable && don't have %u, next is %u\n",
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
909 * Destroy a channel and free all resources.
911 * @param ch Channel to destroy.
914 channel_destroy (struct MeshChannel *ch)
916 struct MeshClient *c;
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
922 peer2s (ch->t->peer), ch->gid);
928 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
939 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
946 channel_rel_free_all (ch->root_rel);
947 channel_rel_free_all (ch->dest_rel);
949 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
950 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
957 * Create a new channel.
959 * @param t Tunnel this channel is in.
960 * @param owner Client that owns the channel, NULL for foreign channels.
961 * @param lid_root Local ID for root client.
963 * @return A new initialized channel. NULL on error.
965 static struct MeshChannel *
966 channel_new (struct MeshTunnel2 *t,
967 struct MeshClient *owner, MESH_ChannelNumber lid_root)
969 struct MeshChannel *ch;
971 ch = GNUNET_new (struct MeshChannel);
973 ch->lid_root = lid_root;
976 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
980 while (NULL != channel_get (t, t->next_chid))
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists (%p)...\n",
983 t->next_chid, channel_get (t, t->next_chid));
984 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
986 ch->gid = t->next_chid;
987 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
990 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
991 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
994 channel_destroy (ch);
995 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
999 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
1006 * Set options in a channel, extracted from a bit flag field
1008 * @param ch Channel to set options to.
1009 * @param options Bit array in host byte order.
1012 channel_set_options (struct MeshChannel *ch, uint32_t options)
1014 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
1015 GNUNET_YES : GNUNET_NO;
1016 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
1017 GNUNET_YES : GNUNET_NO;
1023 * Confirm we got a channel create.
1025 * @param ch The channel to confirm.
1026 * @param fwd Should we send the ACK fwd?
1029 channel_send_ack (struct MeshChannel *ch, int fwd)
1031 struct GNUNET_MESH_ChannelManage msg;
1033 msg.header.size = htons (sizeof (msg));
1034 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 " sending channel %s ack for channel %s:%X\n",
1037 fwd ? "FWD" : "BCK", peer2s (ch->t->peer),
1040 msg.chid = htonl (ch->gid);
1041 GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
1046 * Send a message to all clients (local and remote) of this channel
1047 * notifying that the channel is no longer valid.
1049 * If some peer or client should not receive the message,
1050 * should be zero'ed out before calling this function.
1052 * @param ch The channel whose clients to notify.
1055 channel_send_destroy (struct MeshChannel *ch)
1057 struct GNUNET_MESH_ChannelManage msg;
1059 msg.header.size = htons (sizeof (msg));
1060 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1062 " sending channel destroy for channel %s:%X\n",
1063 peer2s (ch->t->peer),
1066 if (channel_is_terminal (ch, GNUNET_NO))
1068 if (NULL != ch->root && GNUNET_NO == ch->root->shutting_down)
1070 msg.chid = htonl (ch->lid_root);
1071 send_local_channel_destroy (ch, GNUNET_NO);
1076 msg.chid = htonl (ch->gid);
1077 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO);
1080 if (channel_is_terminal (ch, GNUNET_YES))
1082 if (NULL != ch->dest && GNUNET_NO == ch->dest->shutting_down)
1084 msg.chid = htonl (ch->lid_dest);
1085 send_local_channel_destroy (ch, GNUNET_YES);
1090 msg.chid = htonl (ch->gid);
1091 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES);
1097 * Iterator for deleting each channel whose client endpoint disconnected.
1099 * @param cls Closure (client that has disconnected).
1100 * @param key The local channel id (used to access the hashmap).
1101 * @param value The value stored at the key (channel to destroy).
1103 * @return GNUNET_OK, keep iterating.
1106 channel_destroy_iterator (void *cls,
1110 struct MeshChannel *ch = value;
1111 struct MeshClient *c = cls;
1112 struct MeshTunnel2 *t;
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1115 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
1116 ch->gid, ch->lid_root, ch->lid_dest, c->id);
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
1129 GMCH_send_destroy (ch);
1130 channel_send_destroy (ch);
1131 channel_destroy (ch);
1132 tunnel_destroy_if_empty (t);
1139 * Sends an already built message on a channel, properly registering
1140 * all used resources and encrypting the message with the tunnel's key.
1142 * @param message Message to send. Function makes a copy of it.
1143 * @param ch Channel on which this message is transmitted.
1144 * @param fwd Is this a fwd message?
1147 send (const struct GNUNET_MessageHeader *message,
1148 struct MeshChannel *ch, int fwd)
1150 struct GNUNET_MESH_Encrypted *msg;
1151 size_t size = ntohs (message->size);
1152 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
1157 peer2s (ch->t->peer), ch->gid, fwd ? "FWD" : "BCK");
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1159 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1161 if (channel_is_terminal (ch, fwd) || ch->t->peer->id == myid)
1163 handle_decrypted (ch->t, message, fwd);
1167 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1168 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1170 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1171 msg->header.type = htons (type);
1172 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1173 msg->iv = GNUNET_htonll (iv);
1174 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1175 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1181 * Notify the destination client that a new incoming channel was created.
1183 * @param ch Channel that was created.
1186 GMCH_send_create (struct MeshChannel *ch)
1188 struct GNUNET_MESH_ChannelMessage msg;
1189 struct MeshTunnel2 *t = ch->t;
1192 if (NULL == ch->dest)
1196 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1197 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1198 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1199 GNUNET_PEER_resolve2 (t->peer->id));
1204 * Notify a client that the channel is no longer valid.
1206 * @param ch Channel that is destroyed.
1207 * @param fwd Forward notification (owner->dest)?
1210 GMCH_send_destroy (struct MeshChannel *ch, int fwd)
1212 struct GNUNET_MeshClient *c = fwd ? ch->dest : ch->root;
1213 uint32_t id = fwd ? ch->lid_dest : ch->lid_root;
1217 // TODO: send on connection?
1221 GML_send_channel_destroy (c, id);
1226 * Send data on a channel.
1228 * If the destination is local, send it to client, otherwise encrypt and
1232 * @param msg Message.
1233 * @param fwd Is this a fwd (root->dest) message?
1236 GMCH_send_data (struct MeshChannel *ch,
1237 const struct GNUNET_MESH_Data *msg,
1244 * Send an end-to-end ACK message for the most recent in-sequence payload.
1246 * If channel is not reliable, do nothing.
1248 * @param ch Channel this is about.
1249 * @param fwd Is for FWD traffic? (ACK dest->owner)
1252 GMCH_send_ack (struct MeshChannel *ch, int fwd)
1254 struct GNUNET_MESH_DataACK msg;
1255 struct MeshChannelReliability *rel;
1256 struct MeshReliableMessage *copy;
1261 if (GNUNET_NO == ch->reliable)
1265 rel = fwd ? ch->dest_rel : ch->root_rel;
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 "send_data_ack for %u\n",
1270 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
1271 msg.header.type = htons (type);
1272 msg.header.size = htons (sizeof (msg));
1273 msg.chid = htonl (ch->gid);
1274 msg.mid = htonl (rel->mid_recv - 1);
1276 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1278 if (copy->type != type)
1280 delta = copy->mid - rel->mid_recv;
1283 mask = 0x1LL << delta;
1284 msg.futures |= mask;
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1286 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1287 copy->mid, delta, mask, msg.futures);
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
1291 send_prebuilt_message_channel (&msg.header, ch, fwd);
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1299 * @param ch Channel.
1302 GMCH_debug (struct MeshChannel *ch)
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1310 peer2s (ch->t->peer), ch->gid, ch);
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1312 ch->root, ch->root_rel);
1313 if (NULL != ch->root)
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->root->id);
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1317 ch->root_rel->client_ready ? "YES" : "NO");
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1321 ch->dest, ch->dest_rel);
1322 if (NULL != ch->dest)
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->dest->id);
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1326 ch->dest_rel->client_ready ? "YES" : "NO");
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1333 * Handler for mesh network payload traffic.
1335 * @param t Tunnel on which we got this message.
1336 * @param message Unencryted data message.
1337 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1340 GMCH_handle_data (struct MeshTunnel2 *t,
1341 const struct GNUNET_MESH_Data *msg,
1344 struct MeshChannelReliability *rel;
1345 struct MeshChannel *ch;
1346 struct MeshClient *c;
1352 size = ntohs (msg->header.size);
1354 sizeof (struct GNUNET_MESH_Data) +
1355 sizeof (struct GNUNET_MessageHeader))
1360 type = ntohs (msg->header.type);
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
1362 GNUNET_MESH_DEBUG_M2S (type));
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
1364 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
1367 ch = channel_get (t, ntohl (msg->chid));
1370 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1376 /* Initialize FWD/BCK data */
1377 c = fwd ? ch->dest : ch->root;
1378 rel = fwd ? ch->dest_rel : ch->root_rel;
1386 tunnel_change_state (t, MESH_TUNNEL_READY);
1388 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1390 mid = ntohl (msg->mid);
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
1393 if (GNUNET_NO == ch->reliable ||
1394 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
1395 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1398 if (GNUNET_YES == ch->reliable)
1400 /* Is this the exact next expected messasge? */
1401 if (mid == rel->mid_recv)
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1405 send_client_data (ch, msg, fwd);
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1410 add_buffered_data (msg, rel);
1415 /* Tunnel is unreliable: send to clients directly */
1416 /* FIXME: accept Out Of Order traffic */
1417 rel->mid_recv = mid + 1;
1418 send_client_data (ch, msg, fwd);
1423 GNUNET_break_op (0);
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1425 " MID %u not expected (%u - %u), dropping!\n",
1426 mid, rel->mid_recv, rel->mid_recv + 64);
1429 GMCH_send_ack (ch, fwd);
1434 * Handler for mesh network traffic end-to-end ACKs.
1436 * @param t Tunnel on which we got this message.
1437 * @param message Data message.
1438 * @param fwd Is this a fwd ACK? (dest->orig)
1441 GMCH_handle_data_ack (struct MeshTunnel2 *t,
1442 const struct GNUNET_MESH_DataACK *msg,
1445 struct MeshChannelReliability *rel;
1446 struct MeshReliableMessage *copy;
1447 struct MeshReliableMessage *next;
1448 struct MeshChannel *ch;
1453 type = ntohs (msg->header.type);
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
1455 GNUNET_MESH_DEBUG_M2S (type));
1456 ch = channel_get (t, ntohl (msg->chid));
1459 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
1462 ack = ntohl (msg->mid);
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1464 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1466 if (GNUNET_YES == fwd)
1480 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1482 if (GMC_is_pid_bigger (copy->mid, ack))
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1485 channel_rel_free_sent (rel, msg);
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
1491 rel_message_free (copy);
1493 /* ACK client if needed */
1494 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
1496 /* If some message was free'd, update the retransmission delay*/
1497 if (GNUNET_YES == work)
1499 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1501 GNUNET_SCHEDULER_cancel (rel->retry_task);
1502 if (NULL == rel->head_sent)
1504 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1508 struct GNUNET_TIME_Absolute new_target;
1509 struct GNUNET_TIME_Relative delay;
1511 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1512 MESH_RETRANSMIT_MARGIN);
1513 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1515 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1517 GNUNET_SCHEDULER_add_delayed (delay,
1518 &channel_retransmit_message,
1529 * Handler for channel create messages.
1531 * @param t Tunnel this channel is to be created in.
1532 * @param msg Message.
1533 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1536 GMCH_handle_create (struct MeshTunnel2 *t,
1537 struct GNUNET_MESH_ChannelCreate *msg,
1540 MESH_ChannelNumber chid;
1541 struct MeshChannel *ch;
1542 struct MeshClient *c;
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
1546 /* Check message size */
1547 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
1549 GNUNET_break_op (0);
1553 /* Check if channel exists */
1554 chid = ntohl (msg->chid);
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " chid %u\n", chid);
1556 ch = channel_get (t, chid);
1559 /* Probably a retransmission, safe to ignore */
1560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
1561 if (NULL != ch->dest)
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n");
1564 GMCH_send_ack (ch, !fwd);
1570 /* Create channel */
1571 ch = channel_new (t, NULL, 0);
1573 channel_set_options (ch, ntohl (msg->opt));
1576 /* Find a destination client */
1577 port = ntohl (msg->port);
1578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
1579 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
1582 /* TODO send reject */
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
1588 channel_add_client (ch, c);
1589 if (GNUNET_YES == ch->reliable)
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1592 GMCH_send_create (ch);
1593 GMCH_send_ack (ch, fwd);
1594 GML_send_ack (ch, !fwd);
1599 * Handler for channel ack messages.
1601 * @param t Tunnel this channel is to be created in.
1602 * @param msg Message.
1603 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1606 GMCH_handle_ack (struct MeshTunnel2 *t,
1607 struct GNUNET_MESH_ChannelManage *msg,
1610 MESH_ChannelNumber chid;
1611 struct MeshChannel *ch;
1613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
1614 /* Check message size */
1615 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
1617 GNUNET_break_op (0);
1621 /* Check if channel exists */
1622 chid = ntohl (msg->chid);
1623 ch = channel_get (t, chid);
1626 GNUNET_break_op (0);
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
1631 channel_confirm (ch, !fwd);
1636 * Handler for channel destroy messages.
1638 * @param t Tunnel this channel is to be destroyed of.
1639 * @param msg Message.
1640 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1643 GMCH_handle_destroy (struct MeshTunnel2 *t,
1644 struct GNUNET_MESH_ChannelManage *msg,
1647 MESH_ChannelNumber chid;
1648 struct MeshChannel *ch;
1650 /* Check message size */
1651 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
1653 GNUNET_break_op (0);
1657 /* Check if channel exists */
1658 chid = ntohl (msg->chid);
1659 ch = channel_get (t, chid);
1662 /* Probably a retransmission, safe to ignore */
1665 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
1667 /* Not for us (don't destroy twice a half-open loopback channel) */
1671 GMCH_send_destroy (ch, fwd);
1672 channel_destroy (ch);