2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_statistics_service.h"
28 #include "mesh_protocol_enc.h"
30 #include "gnunet-service-mesh_channel.h"
31 #include "gnunet-service-mesh_local.h"
32 #include "gnunet-service-mesh_tunnel.h"
34 #define LOG(level, ...) GNUNET_log_from(level,"mesh-chn",__VA_ARGS__)
37 * All the states a connection can be in.
42 * Uninitialized status, should never appear in operation.
47 * Connection create message sent, waiting for ACK.
52 * Connection confirmed, ready to carry traffic..
60 * Info needed to retry a message in case it gets lost.
62 struct MeshReliableMessage
65 * Double linked list, FIFO style
67 struct MeshReliableMessage *next;
68 struct MeshReliableMessage *prev;
71 * Type of message (payload, channel management).
76 * Tunnel Reliability queue this message is in.
78 struct MeshChannelReliability *rel;
81 * ID of the message (ACK needed to free)
86 * When was this message issued (to calculate ACK delay)
88 struct GNUNET_TIME_Absolute timestamp;
90 /* struct GNUNET_MESH_Data with payload */
95 * Info about the traffic state for a client in a channel.
97 struct MeshChannelReliability
100 * Channel this is about.
102 struct MeshChannel *ch;
105 * DLL of messages sent and not yet ACK'd.
107 struct MeshReliableMessage *head_sent;
108 struct MeshReliableMessage *tail_sent;
111 * Messages pending to send.
116 * DLL of messages received out of order.
118 struct MeshReliableMessage *head_recv;
119 struct MeshReliableMessage *tail_recv;
127 * Next MID to use for outgoing traffic.
132 * Next MID expected for incoming traffic.
137 * Can we send data to the client?
142 * Task to resend/poll in case no ACK is received.
144 GNUNET_SCHEDULER_TaskIdentifier retry_task;
147 * Counter for exponential backoff.
149 struct GNUNET_TIME_Relative retry_timer;
152 * How long does it usually take to get an ACK.
154 struct GNUNET_TIME_Relative expected_delay;
159 * Struct containing all information regarding a channel to a remote client.
164 * Tunnel this channel is in.
166 struct MeshTunnel2 *t;
169 * Double linked list.
171 struct MeshChannel *next;
172 struct MeshChannel *prev;
175 * Destination port of the channel.
180 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
182 MESH_ChannelNumber gid;
185 * Local tunnel number for root (owner) client.
186 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
188 MESH_ChannelNumber lid_root;
191 * Local tunnel number for local destination clients (incoming number)
192 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
194 MESH_ChannelNumber lid_dest;
199 enum MeshChannelState state;
202 * Is the tunnel bufferless (minimum latency)?
207 * Is the tunnel reliable?
212 * Last time the channel was used
214 struct GNUNET_TIME_Absolute timestamp;
217 * Client owner of the tunnel, if any
219 struct MeshClient *root;
222 * Client destination of the tunnel, if any.
224 struct MeshClient *dest;
227 * Flag to signal the destruction of the channel.
228 * If this is set GNUNET_YES the channel will be destroyed
229 * when the queue is empty.
234 * Total messages pending for this channel, payload or not.
236 unsigned int pending_messages;
240 * Only present (non-NULL) at the owner of a tunnel.
242 struct MeshChannelReliability *root_rel;
246 * Only present (non-NULL) at the destination of a tunnel.
248 struct MeshChannelReliability *dest_rel;
253 /******************************************************************************/
254 /******************************* GLOBALS ***********************************/
255 /******************************************************************************/
258 * Global handle to the statistics service.
260 extern struct GNUNET_STATISTICS_Handle *stats;
263 /******************************************************************************/
264 /******************************** STATIC ***********************************/
265 /******************************************************************************/
268 * We have received a message out of order, or the client is not ready.
269 * Buffer it until we receive an ACK from the client or the missing
270 * message from the channel.
272 * @param msg Message to buffer (MUST be of type MESH_DATA).
273 * @param rel Reliability data to the corresponding direction.
276 add_buffered_data (const struct GNUNET_MESH_Data *msg,
277 struct MeshChannelReliability *rel)
279 struct MeshReliableMessage *copy;
280 struct MeshReliableMessage *prev;
284 size = ntohs (msg->header.size);
285 mid = ntohl (msg->mid);
287 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
289 copy = GNUNET_malloc (sizeof (*copy) + size);
292 memcpy (©[1], msg, size);
296 // FIXME do something better than O(n), although n < 64...
297 // FIXME start from the end (most messages are the latest ones)
298 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
300 LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
301 if (GMC_is_pid_bigger (prev->mid, mid))
303 LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
304 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
309 LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
310 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
311 LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
316 * Send data to a client.
318 * If the client is ready, send directly, otherwise buffer while listening
322 * @param msg Message.
323 * @param fwd Is this a fwd (root->dest) message?
326 send_client_data (struct MeshChannel *ch,
327 const struct GNUNET_MESH_Data *msg,
332 if (ch->dest_rel->client_ready)
333 GML_send_data (ch, msg, ch->dest, ch->lid_dest);
335 add_buffered_data (msg, ch->dest_rel);
339 if (ch->root_rel->client_ready)
340 GML_send_data (ch, msg, ch->root, ch->lid_root);
342 add_buffered_data (msg, ch->root_rel);
348 * Search for a channel among the channels for a client
350 * @param c the client whose channels to search in
351 * @param chid the local id of the channel
353 * @return channel handler, NULL if doesn't exist
355 static struct MeshChannel *
356 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
358 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid);
359 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
362 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
365 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
366 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
367 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
373 * Search for a channel by global ID using full PeerIdentities.
375 * @param t Tunnel containing the channel.
376 * @param chid Public channel number.
378 * @return channel handler, NULL if doesn't exist
380 static struct MeshChannel *
381 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
383 struct MeshChannel *ch;
388 for (ch = t->channel_head; NULL != ch; ch = ch->next)
399 * Add a client to a channel, initializing all needed data structures.
401 * @param ch Channel to which add the client.
402 * @param c Client which to add to the channel.
405 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
407 struct MeshTunnel2 *t = ch->t;
409 if (NULL != ch->dest)
415 /* Assign local id as destination */
416 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
417 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
418 ch->lid_dest = t->next_local_chid++;
419 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
421 /* Store in client's hashmap */
423 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
425 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
431 GNUNET_break (NULL == ch->dest_rel);
432 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
433 ch->dest_rel->ch = ch;
434 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
442 * Is the root client for this channel on this peer?
445 * @param fwd Is this for fwd traffic?
447 * @return GNUNET_YES in case it is.
450 channel_is_origin (struct MeshChannel *ch, int fwd)
452 struct MeshClient *c;
454 c = fwd ? ch->root : ch->dest;
460 * Is the destination client for this channel on this peer?
463 * @param fwd Is this for fwd traffic?
465 * @return GNUNET_YES in case it is.
468 channel_is_terminal (struct MeshChannel *ch, int fwd)
470 struct MeshClient *c;
472 c = fwd ? ch->dest : ch->root;
478 * Get free buffer space towards the client on a specific channel.
481 * @param fwd Is query about FWD traffic?
483 * @return Free buffer space [0 - 64]
486 channel_get_buffer (struct MeshChannel *ch, int fwd)
488 struct MeshChannelReliability *rel;
490 rel = fwd ? ch->dest_rel : ch->root_rel;
492 /* If rel is NULL it means that the end is not yet created,
493 * most probably is a loopback channel at the point of sending
494 * the ChannelCreate to itself.
499 return (64 - rel->n_recv);
506 * Destroy all reliable messages queued for a channel,
507 * during a channel destruction.
508 * Frees the reliability structure itself.
510 * @param rel Reliability data for a channel.
513 channel_rel_free_all (struct MeshChannelReliability *rel)
515 struct MeshReliableMessage *copy;
516 struct MeshReliableMessage *next;
521 for (copy = rel->head_recv; NULL != copy; copy = next)
524 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
527 for (copy = rel->head_sent; NULL != copy; copy = next)
530 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
533 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
534 GNUNET_SCHEDULER_cancel (rel->retry_task);
540 * Mark future messages as ACK'd.
542 * @param rel Reliability data.
543 * @param msg DataACK message with a bitfield of future ACK'd messages.
546 channel_rel_free_sent (struct MeshChannelReliability *rel,
547 const struct GNUNET_MESH_DataACK *msg)
549 struct MeshReliableMessage *copy;
550 struct MeshReliableMessage *next;
557 bitfield = msg->futures;
558 mid = ntohl (msg->mid);
559 LOG (GNUNET_ERROR_TYPE_DEBUG,
560 "free_sent_reliable %u %llX\n",
562 LOG (GNUNET_ERROR_TYPE_DEBUG,
563 " rel %p, head %p\n",
564 rel, rel->head_sent);
565 for (i = 0, copy = rel->head_sent;
566 i < 64 && NULL != copy && 0 != bitfield;
569 LOG (GNUNET_ERROR_TYPE_DEBUG,
570 " trying bit %u (mid %u)\n",
573 if (0 == (bitfield & mask))
576 LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
577 /* Bit was set, clear the bit from the bitfield */
580 /* The i-th bit was set. Do we have that copy? */
581 /* Skip copies with mid < target */
582 target = mid + i + 1;
583 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
584 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
587 /* Did we run out of copies? (previously freed, it's ok) */
590 LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
594 /* Did we overshoot the target? (previously freed, it's ok) */
595 if (GMC_is_pid_bigger (copy->mid, target))
597 LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
601 /* Now copy->mid == target, free it */
603 rel_message_free (copy);
606 LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
611 * We haven't received an ACK after a certain time: restransmit the message.
613 * @param cls Closure (MeshReliableMessage with the message to restransmit)
614 * @param tc TaskContext.
617 channel_retransmit_message (void *cls,
618 const struct GNUNET_SCHEDULER_TaskContext *tc)
620 struct MeshChannelReliability *rel = cls;
621 struct MeshReliableMessage *copy;
622 struct MeshPeerQueue *q;
623 struct MeshChannel *ch;
624 struct MeshConnection *c;
625 struct GNUNET_MESH_Data *payload;
626 struct MeshPeer *hop;
629 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
630 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
634 copy = rel->head_sent;
641 /* Search the message to be retransmitted in the outgoing queue.
642 * Check only the queue for the connection that is going to be used,
643 * if the message is stuck in some other connection's queue we shouldn't
645 * - cancelling it and sending the new one doesn't guarantee it's delivery,
646 * the old connection could be temporary stalled or the queue happened to
647 * be long at time of insertion.
648 * - not sending the new one could cause terrible delays the old connection
651 payload = (struct GNUNET_MESH_Data *) ©[1];
652 fwd = (rel == ch->root_rel);
653 c = tunnel_get_connection (ch->t, fwd);
654 hop = connection_get_hop (c, fwd);
655 for (q = hop->queue_head; NULL != q; q = q->next)
657 if (ntohs (payload->header.type) == q->type && ch == q->ch)
659 struct GNUNET_MESH_Data *queued_data = q->cls;
661 if (queued_data->mid == payload->mid)
666 /* Message not found in the queue that we are going to use. */
669 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
671 send_prebuilt_message_channel (&payload->header, ch, fwd);
672 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
676 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
679 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
680 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
681 &channel_retransmit_message,
687 * Send ACK on one or more connections due to buffer space to the client.
689 * Iterates all connections of the tunnel and sends ACKs appropriately.
691 * @param ch Channel which has some free buffer space.
692 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
695 channel_send_connections_ack (struct MeshChannel *ch,
699 struct MeshTunnel2 *t = ch->t;
700 struct MeshConnection *c;
701 struct MeshFlowControl *fc;
704 uint32_t allow_per_connection;
707 LOG (GNUNET_ERROR_TYPE_DEBUG,
708 "Channel send connection %s ack on %s:%X\n",
709 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
711 /* Count connections, how many messages are already allowed */
712 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
714 fc = fwd ? &c->fwd_fc : &c->bck_fc;
715 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
720 allowed += fc->last_ack_sent - fc->last_pid_recv;
724 /* Make sure there is no overflow */
725 if (allowed > buffer)
731 /* Authorize connections to send more data */
732 to_allow = buffer - allowed;
734 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
736 allow_per_connection = to_allow/cs;
737 to_allow -= allow_per_connection;
739 fc = fwd ? &c->fwd_fc : &c->bck_fc;
740 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
744 connection_send_ack (c, allow_per_connection, fwd);
747 LOG (GNUNET_ERROR_TYPE_DEBUG,
748 "Channel send connection %s ack on %s:%X\n",
749 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
750 GNUNET_break (to_allow == 0);
755 * Destroy a reliable message after it has been acknowledged, either by
756 * direct mid ACK or bitfield. Updates the appropriate data structures and
757 * timers and frees all memory.
759 * @param copy Message that is no longer needed: remote peer got it.
762 rel_message_free (struct MeshReliableMessage *copy)
764 struct MeshChannelReliability *rel;
765 struct GNUNET_TIME_Relative time;
768 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
769 rel->expected_delay.rel_value_us *= 7;
770 rel->expected_delay.rel_value_us += time.rel_value_us;
771 rel->expected_delay.rel_value_us /= 8;
773 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
774 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
775 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
776 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
777 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
778 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
780 rel->retry_timer = rel->expected_delay;
781 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
788 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
790 * @param ch Channel to mark as ready.
791 * @param fwd Was the CREATE message sent fwd?
794 channel_confirm (struct MeshChannel *ch, int fwd)
796 struct MeshChannelReliability *rel;
797 struct MeshReliableMessage *copy;
798 struct MeshReliableMessage *next;
800 LOG (GNUNET_ERROR_TYPE_DEBUG,
801 " channel confirm %s %s:%X\n",
802 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
803 ch->state = MESH_CHANNEL_READY;
805 rel = fwd ? ch->root_rel : ch->dest_rel;
806 for (copy = rel->head_sent; NULL != copy; copy = next)
808 struct GNUNET_MessageHeader *msg;
811 msg = (struct GNUNET_MessageHeader *) ©[1];
812 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
814 rel_message_free (copy);
818 send_ack (NULL, ch, fwd);
823 * Save a copy to retransmit in case it gets lost.
825 * Initializes all needed callbacks and timers.
827 * @param ch Channel this message goes on.
828 * @param msg Message to copy.
829 * @param fwd Is this fwd traffic?
832 channel_save_copy (struct MeshChannel *ch,
833 const struct GNUNET_MessageHeader *msg,
836 struct MeshChannelReliability *rel;
837 struct MeshReliableMessage *copy;
842 rel = fwd ? ch->root_rel : ch->dest_rel;
844 type = ntohs (msg->type);
845 size = ntohs (msg->size);
847 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
848 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
850 copy->timestamp = GNUNET_TIME_absolute_get ();
853 memcpy (©[1], msg, size);
855 LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
856 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
857 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
860 GNUNET_TIME_relative_multiply (rel->expected_delay,
861 MESH_RETRANSMIT_MARGIN);
863 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
864 &channel_retransmit_message,
872 * Send a buffered message to the client, for in order delivery or
873 * as result of client ACK.
875 * @param ch Channel on which to empty the message buffer.
876 * @param c Client to send to.
877 * @param rel Reliability structure to corresponding peer.
878 * If rel == bck_rel, this is FWD data.
881 send_client_buffered_data (struct MeshChannel *ch,
882 struct MeshClient *c,
885 struct MeshReliableMessage *copy;
886 struct MeshChannelReliability *rel;
888 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
889 rel = fwd ? ch->dest_rel : ch->root_rel;
890 if (GNUNET_NO == rel->client_ready)
892 LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
896 copy = rel->head_recv;
897 /* We never buffer channel management messages */
900 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
902 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
904 LOG (GNUNET_ERROR_TYPE_DEBUG,
905 " have %u! now expecting %u\n",
906 copy->mid, rel->mid_recv + 1);
907 send_client_data (ch, msg, fwd);
910 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
915 LOG (GNUNET_ERROR_TYPE_DEBUG,
916 " reliable && don't have %u, next is %u\n",
922 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
929 * Destroy a channel and free all resources.
931 * @param ch Channel to destroy.
934 channel_destroy (struct MeshChannel *ch)
936 struct MeshClient *c;
941 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
942 peer2s (ch->t->peer), ch->gid);
948 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
959 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
966 channel_rel_free_all (ch->root_rel);
967 channel_rel_free_all (ch->dest_rel);
969 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
970 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
977 * Create a new channel.
979 * @param t Tunnel this channel is in.
980 * @param owner Client that owns the channel, NULL for foreign channels.
981 * @param lid_root Local ID for root client.
983 * @return A new initialized channel. NULL on error.
985 static struct MeshChannel *
986 channel_new (struct MeshTunnel2 *t,
987 struct MeshClient *owner, MESH_ChannelNumber lid_root)
989 struct MeshChannel *ch;
991 ch = GNUNET_new (struct MeshChannel);
993 ch->lid_root = lid_root;
996 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
1000 while (NULL != channel_get (t, t->next_chid))
1002 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists (%p)...\n",
1003 t->next_chid, channel_get (t, t->next_chid));
1004 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1006 ch->gid = t->next_chid;
1007 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1010 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
1011 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1014 channel_destroy (ch);
1015 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
1019 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
1026 * Set options in a channel, extracted from a bit flag field
1028 * @param ch Channel to set options to.
1029 * @param options Bit array in host byte order.
1032 channel_set_options (struct MeshChannel *ch, uint32_t options)
1034 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
1035 GNUNET_YES : GNUNET_NO;
1036 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
1037 GNUNET_YES : GNUNET_NO;
1043 * Confirm we got a channel create.
1045 * @param ch The channel to confirm.
1046 * @param fwd Should we send the ACK fwd?
1049 channel_send_ack (struct MeshChannel *ch, int fwd)
1051 struct GNUNET_MESH_ChannelManage msg;
1053 msg.header.size = htons (sizeof (msg));
1054 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
1055 LOG (GNUNET_ERROR_TYPE_DEBUG,
1056 " sending channel %s ack for channel %s:%X\n",
1057 fwd ? "FWD" : "BCK", peer2s (ch->t->peer),
1060 msg.chid = htonl (ch->gid);
1061 GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
1066 * Send a message to all clients (local and remote) of this channel
1067 * notifying that the channel is no longer valid.
1069 * If some peer or client should not receive the message,
1070 * should be zero'ed out before calling this function.
1072 * @param ch The channel whose clients to notify.
1075 channel_send_destroy (struct MeshChannel *ch)
1077 struct GNUNET_MESH_ChannelManage msg;
1079 msg.header.size = htons (sizeof (msg));
1080 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1081 LOG (GNUNET_ERROR_TYPE_DEBUG,
1082 " sending channel destroy for channel %s:%X\n",
1083 peer2s (ch->t->peer),
1086 if (channel_is_terminal (ch, GNUNET_NO))
1088 if (NULL != ch->root && GNUNET_NO == ch->root->shutting_down)
1090 msg.chid = htonl (ch->lid_root);
1091 send_local_channel_destroy (ch, GNUNET_NO);
1096 msg.chid = htonl (ch->gid);
1097 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO);
1100 if (channel_is_terminal (ch, GNUNET_YES))
1102 if (NULL != ch->dest && GNUNET_NO == ch->dest->shutting_down)
1104 msg.chid = htonl (ch->lid_dest);
1105 send_local_channel_destroy (ch, GNUNET_YES);
1110 msg.chid = htonl (ch->gid);
1111 GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES);
1117 * Iterator for deleting each channel whose client endpoint disconnected.
1119 * @param cls Closure (client that has disconnected).
1120 * @param key The local channel id (used to access the hashmap).
1121 * @param value The value stored at the key (channel to destroy).
1123 * @return GNUNET_OK, keep iterating.
1126 channel_destroy_iterator (void *cls,
1130 struct MeshChannel *ch = value;
1131 struct MeshClient *c = cls;
1132 struct MeshTunnel2 *t;
1134 LOG (GNUNET_ERROR_TYPE_DEBUG,
1135 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
1136 ch->gid, ch->lid_root, ch->lid_dest, c->id);
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
1149 GMCH_send_destroy (ch);
1150 channel_send_destroy (ch);
1151 channel_destroy (ch);
1152 tunnel_destroy_if_empty (t);
1159 * Sends an already built message on a channel, properly registering
1160 * all used resources and encrypting the message with the tunnel's key.
1162 * @param message Message to send. Function makes a copy of it.
1163 * @param ch Channel on which this message is transmitted.
1164 * @param fwd Is this a fwd message?
1167 send (const struct GNUNET_MessageHeader *message,
1168 struct MeshChannel *ch, int fwd)
1170 struct GNUNET_MESH_Encrypted *msg;
1171 size_t size = ntohs (message->size);
1172 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1176 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
1177 peer2s (ch->t->peer), ch->gid, fwd ? "FWD" : "BCK");
1178 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1179 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1181 if (channel_is_terminal (ch, fwd) || ch->t->peer->id == myid)
1183 handle_decrypted (ch->t, message, fwd);
1187 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1188 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1190 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1191 msg->header.type = htons (type);
1192 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1193 msg->iv = GNUNET_htonll (iv);
1194 GMT_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1195 GMT_send_prebuilt_message (msg, ch->t, ch, fwd);
1199 /******************************************************************************/
1200 /******************************** API ***********************************/
1201 /******************************************************************************/
1204 * Count channels in a DLL.
1206 * @param head Head of the DLL.
1209 GMCH_count (const struct MeshChannel *head)
1212 struct MeshChannel *iter;
1214 for (count = 0, iter = head; NULL != iter; iter = iter->next, count++);
1221 * Notify the destination client that a new incoming channel was created.
1223 * @param ch Channel that was created.
1226 GMCH_send_create (struct MeshChannel *ch)
1228 struct GNUNET_MESH_ChannelMessage msg;
1229 struct MeshTunnel2 *t = ch->t;
1232 if (NULL == ch->dest)
1236 opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1237 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1238 GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1239 GNUNET_PEER_resolve2 (t->peer->id));
1244 * Notify a client that the channel is no longer valid.
1246 * @param ch Channel that is destroyed.
1247 * @param fwd Forward notification (owner->dest)?
1250 GMCH_send_destroy (struct MeshChannel *ch, int fwd)
1252 struct GNUNET_MeshClient *c = fwd ? ch->dest : ch->root;
1253 uint32_t id = fwd ? ch->lid_dest : ch->lid_root;
1257 // TODO: send on connection?
1261 GML_send_channel_destroy (c, id);
1266 * Send data on a channel.
1268 * If the destination is local, send it to client, otherwise encrypt and
1272 * @param msg Message.
1273 * @param fwd Is this a fwd (root->dest) message?
1276 GMCH_send_data (struct MeshChannel *ch,
1277 const struct GNUNET_MESH_Data *msg,
1284 * Send an end-to-end ACK message for the most recent in-sequence payload.
1286 * If channel is not reliable, do nothing.
1288 * @param ch Channel this is about.
1289 * @param fwd Is for FWD traffic? (ACK dest->owner)
1292 GMCH_send_ack (struct MeshChannel *ch, int fwd)
1294 struct GNUNET_MESH_DataACK msg;
1295 struct MeshChannelReliability *rel;
1296 struct MeshReliableMessage *copy;
1301 if (GNUNET_NO == ch->reliable)
1305 rel = fwd ? ch->dest_rel : ch->root_rel;
1306 LOG (GNUNET_ERROR_TYPE_DEBUG,
1307 "send_data_ack for %u\n",
1310 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
1311 msg.header.type = htons (type);
1312 msg.header.size = htons (sizeof (msg));
1313 msg.chid = htonl (ch->gid);
1314 msg.mid = htonl (rel->mid_recv - 1);
1316 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1318 if (copy->type != type)
1320 delta = copy->mid - rel->mid_recv;
1323 mask = 0x1LL << delta;
1324 msg.futures |= mask;
1325 LOG (GNUNET_ERROR_TYPE_DEBUG,
1326 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1327 copy->mid, delta, mask, msg.futures);
1329 LOG (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
1331 send_prebuilt_message_channel (&msg.header, ch, fwd);
1332 LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1339 * @param ch Channel.
1342 GMCH_debug (struct MeshChannel *ch)
1346 LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1349 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1350 peer2s (ch->t->peer), ch->gid, ch);
1351 LOG (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
1352 ch->root, ch->root_rel);
1353 if (NULL != ch->root)
1355 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->root->id);
1356 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1357 ch->root_rel->client_ready ? "YES" : "NO");
1358 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
1360 LOG (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n",
1361 ch->dest, ch->dest_rel);
1362 if (NULL != ch->dest)
1364 LOG (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->dest->id);
1365 LOG (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
1366 ch->dest_rel->client_ready ? "YES" : "NO");
1367 LOG (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
1373 * Handler for mesh network payload traffic.
1375 * @param t Tunnel on which we got this message.
1376 * @param message Unencryted data message.
1377 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1380 GMCH_handle_data (struct MeshTunnel2 *t,
1381 const struct GNUNET_MESH_Data *msg,
1384 struct MeshChannelReliability *rel;
1385 struct MeshChannel *ch;
1386 struct MeshClient *c;
1392 size = ntohs (msg->header.size);
1394 sizeof (struct GNUNET_MESH_Data) +
1395 sizeof (struct GNUNET_MessageHeader))
1400 type = ntohs (msg->header.type);
1401 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
1402 GNUNET_MESH_DEBUG_M2S (type));
1403 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
1404 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
1407 ch = channel_get (t, ntohl (msg->chid));
1410 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
1411 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1416 /* Initialize FWD/BCK data */
1417 c = fwd ? ch->dest : ch->root;
1418 rel = fwd ? ch->dest_rel : ch->root_rel;
1426 GMT_change_state (t, MESH_TUNNEL_READY);
1428 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1430 mid = ntohl (msg->mid);
1431 LOG (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
1433 if (GNUNET_NO == ch->reliable ||
1434 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
1435 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1437 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1438 if (GNUNET_YES == ch->reliable)
1440 /* Is this the exact next expected messasge? */
1441 if (mid == rel->mid_recv)
1443 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1445 send_client_data (ch, msg, fwd);
1449 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1450 add_buffered_data (msg, rel);
1455 /* Tunnel is unreliable: send to clients directly */
1456 /* FIXME: accept Out Of Order traffic */
1457 rel->mid_recv = mid + 1;
1458 send_client_data (ch, msg, fwd);
1463 GNUNET_break_op (0);
1464 LOG (GNUNET_ERROR_TYPE_DEBUG,
1465 " MID %u not expected (%u - %u), dropping!\n",
1466 mid, rel->mid_recv, rel->mid_recv + 64);
1469 GMCH_send_ack (ch, fwd);
1474 * Handler for mesh network traffic end-to-end ACKs.
1476 * @param t Tunnel on which we got this message.
1477 * @param message Data message.
1478 * @param fwd Is this a fwd ACK? (dest->orig)
1481 GMCH_handle_data_ack (struct MeshTunnel2 *t,
1482 const struct GNUNET_MESH_DataACK *msg,
1485 struct MeshChannelReliability *rel;
1486 struct MeshReliableMessage *copy;
1487 struct MeshReliableMessage *next;
1488 struct MeshChannel *ch;
1493 type = ntohs (msg->header.type);
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
1495 GNUNET_MESH_DEBUG_M2S (type));
1496 ch = channel_get (t, ntohl (msg->chid));
1499 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
1502 ack = ntohl (msg->mid);
1503 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1504 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1506 if (GNUNET_YES == fwd)
1520 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1522 if (GMC_is_pid_bigger (copy->mid, ack))
1524 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
1525 channel_rel_free_sent (rel, msg);
1529 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
1531 rel_message_free (copy);
1533 /* ACK client if needed */
1534 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
1536 /* If some message was free'd, update the retransmission delay*/
1537 if (GNUNET_YES == work)
1539 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1541 GNUNET_SCHEDULER_cancel (rel->retry_task);
1542 if (NULL == rel->head_sent)
1544 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1548 struct GNUNET_TIME_Absolute new_target;
1549 struct GNUNET_TIME_Relative delay;
1551 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1552 MESH_RETRANSMIT_MARGIN);
1553 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1555 delay = GNUNET_TIME_absolute_get_remaining (new_target);
1557 GNUNET_SCHEDULER_add_delayed (delay,
1558 &channel_retransmit_message,
1569 * Handler for channel create messages.
1571 * @param t Tunnel this channel is to be created in.
1572 * @param msg Message.
1573 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1576 GMCH_handle_create (struct MeshTunnel2 *t,
1577 struct GNUNET_MESH_ChannelCreate *msg,
1580 MESH_ChannelNumber chid;
1581 struct MeshChannel *ch;
1582 struct MeshClient *c;
1585 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
1586 /* Check message size */
1587 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
1589 GNUNET_break_op (0);
1593 /* Check if channel exists */
1594 chid = ntohl (msg->chid);
1595 LOG (GNUNET_ERROR_TYPE_DEBUG, " chid %u\n", chid);
1596 ch = channel_get (t, chid);
1599 /* Probably a retransmission, safe to ignore */
1600 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
1601 if (NULL != ch->dest)
1603 LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n");
1604 GMCH_send_ack (ch, !fwd);
1610 /* Create channel */
1611 ch = channel_new (t, NULL, 0);
1613 channel_set_options (ch, ntohl (msg->opt));
1616 /* Find a destination client */
1617 port = ntohl (msg->port);
1618 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
1619 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
1622 /* TODO send reject */
1623 LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
1628 channel_add_client (ch, c);
1629 if (GNUNET_YES == ch->reliable)
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1632 GMCH_send_create (ch);
1633 GMCH_send_ack (ch, fwd);
1634 GML_send_ack (ch, !fwd);
1639 * Handler for channel ack messages.
1641 * @param t Tunnel this channel is to be created in.
1642 * @param msg Message.
1643 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1646 GMCH_handle_ack (struct MeshTunnel2 *t,
1647 struct GNUNET_MESH_ChannelManage *msg,
1650 MESH_ChannelNumber chid;
1651 struct MeshChannel *ch;
1653 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
1654 /* Check message size */
1655 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
1657 GNUNET_break_op (0);
1661 /* Check if channel exists */
1662 chid = ntohl (msg->chid);
1663 ch = channel_get (t, chid);
1666 GNUNET_break_op (0);
1667 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
1671 channel_confirm (ch, !fwd);
1676 * Handler for channel destroy messages.
1678 * @param t Tunnel this channel is to be destroyed of.
1679 * @param msg Message.
1680 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1683 GMCH_handle_destroy (struct MeshTunnel2 *t,
1684 struct GNUNET_MESH_ChannelManage *msg,
1687 MESH_ChannelNumber chid;
1688 struct MeshChannel *ch;
1690 /* Check message size */
1691 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
1693 GNUNET_break_op (0);
1697 /* Check if channel exists */
1698 chid = ntohl (msg->chid);
1699 ch = channel_get (t, chid);
1702 /* Probably a retransmission, safe to ignore */
1705 if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
1707 /* Not for us (don't destroy twice a half-open loopback channel) */
1711 GMCH_send_destroy (ch, fwd);
1712 channel_destroy (ch);