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;
1022 * Iterator for deleting each channel whose client endpoint disconnected.
1024 * @param cls Closure (client that has disconnected).
1025 * @param key The local channel id (used to access the hashmap).
1026 * @param value The value stored at the key (channel to destroy).
1028 * @return GNUNET_OK, keep iterating.
1031 channel_destroy_iterator (void *cls,
1035 struct MeshChannel *ch = value;
1036 struct MeshClient *c = cls;
1037 struct MeshTunnel2 *t;
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1040 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
1041 ch->gid, ch->lid_root, ch->lid_dest, c->id);
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
1054 channel_send_destroy (ch);
1055 channel_destroy (ch);
1056 tunnel_destroy_if_empty (t);
1063 * Sends an already built message on a channel, properly registering
1064 * all used resources and encrypting the message with the tunnel's key.
1066 * @param message Message to send. Function makes a copy of it.
1067 * @param ch Channel on which this message is transmitted.
1068 * @param fwd Is this a fwd message?
1071 send (const struct GNUNET_MessageHeader *message,
1072 struct MeshChannel *ch, int fwd)
1074 struct GNUNET_MESH_Encrypted *msg;
1075 size_t size = ntohs (message->size);
1076 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
1081 peer2s (ch->t->peer), ch->gid, fwd ? "FWD" : "BCK");
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1083 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1085 if (channel_is_terminal (ch, fwd) || ch->t->peer->id == myid)
1087 handle_decrypted (ch->t, message, fwd);
1091 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1092 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1094 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1095 msg->header.type = htons (type);
1096 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1097 msg->iv = GNUNET_htonll (iv);
1098 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1099 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1105 * Notify the destination client that a new incoming channel was created.
1107 * @param ch Channel that was created.
1110 GMCH_send_channel_create (struct MeshChannel *ch)
1112 struct GNUNET_MESH_ChannelMessage msg;
1113 struct MeshTunnel2 *t = ch->t;
1116 if (NULL == ch->dest)
1120 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1121 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1122 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1123 GNUNET_PEER_resolve2 (t->peer->id));
1128 * Notify a client that the channel is no longer valid.
1130 * @param ch Channel that is destroyed.
1131 * @param fwd Forward notification (owner->dest)?
1134 GMCH_send_channel_destroy (struct MeshChannel *ch, int fwd)
1136 struct GNUNET_MeshClient *c = fwd ? ch->dest : ch->root;
1137 uint32_t id = fwd ? ch->lid_dest : ch->lid_root;
1142 GML_send_channel_destroy (c, id);
1147 * Send data on a channel.
1149 * If the destination is local, send it to client, otherwise encrypt and
1153 * @param msg Message.
1154 * @param fwd Is this a fwd (root->dest) message?
1157 GMCH_send_data (struct MeshChannel *ch,
1158 const struct GNUNET_MESH_Data *msg,
1165 * Send an end-to-end ACK message for the most recent in-sequence payload.
1167 * If channel is not reliable, do nothing.
1169 * @param ch Channel this is about.
1170 * @param fwd Is for FWD traffic? (ACK dest->owner)
1173 GMCH_send_ack (struct MeshChannel *ch, int fwd)
1175 struct GNUNET_MESH_DataACK msg;
1176 struct MeshChannelReliability *rel;
1177 struct MeshReliableMessage *copy;
1182 if (GNUNET_NO == ch->reliable)
1186 rel = fwd ? ch->dest_rel : ch->root_rel;
1187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1188 "send_data_ack for %u\n",
1191 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
1192 msg.header.type = htons (type);
1193 msg.header.size = htons (sizeof (msg));
1194 msg.chid = htonl (ch->gid);
1195 msg.mid = htonl (rel->mid_recv - 1);
1197 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1199 if (copy->type != type)
1201 delta = copy->mid - rel->mid_recv;
1204 mask = 0x1LL << delta;
1205 msg.futures |= mask;
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1208 copy->mid, delta, mask, msg.futures);
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
1212 send_prebuilt_message_channel (&msg.header, ch, fwd);
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1220 * @param ch Channel.
1223 GMCH_debug (struct MeshChannel *ch)
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1231 peer2s (ch->t->peer), ch->gid, ch);
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1233 ch->root, ch->root_rel);
1234 if (NULL != ch->root)
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->root->id);
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1238 ch->root_rel->client_ready ? "YES" : "NO");
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1242 ch->dest, ch->dest_rel);
1243 if (NULL != ch->dest)
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->dest->id);
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1247 ch->dest_rel->client_ready ? "YES" : "NO");
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);