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.
22 #include "gnunet_util_lib.h"
24 #include "gnunet_statistics_service.h"
26 #include "mesh_protocol_enc.h"
28 #include "gnunet-service-mesh_tunnel.h"
29 #include "gnunet-service-mesh_connection.h"
30 #include "gnunet-service-mesh_channel.h"
31 #include "gnunet-service-mesh_peer.h"
32 #include "mesh_path.h"
34 #define LOG(level, ...) GNUNET_log_from(level,"mesh-tun",__VA_ARGS__)
36 #define START_FUNCTION LOG(GNUNET_ERROR_TYPE_DEBUG, "%s start\n", __FUNCTION__)
37 #define END_FUNCTION LOG(GNUNET_ERROR_TYPE_DEBUG, "%s end\n", __FUNCTION__)
40 /******************************************************************************/
41 /******************************** STRUCTS **********************************/
42 /******************************************************************************/
46 struct MeshTChannel *next;
47 struct MeshTChannel *prev;
48 struct MeshChannel *ch;
51 struct MeshTConnection
53 struct MeshTConnection *next;
54 struct MeshTConnection *prev;
55 struct MeshConnection *c;
59 * Struct containing all information regarding a tunnel to a peer.
64 * Endpoint of the tunnel.
66 struct MeshPeer *peer;
69 * State of the tunnel.
71 enum MeshTunnel3State state;
74 * Local peer ephemeral private key
76 struct GNUNET_CRYPTO_EddsaPrivateKey *my_eph_key;
79 * Local peer ephemeral public key
81 struct GNUNET_CRYPTO_EddsaPublicKey *my_eph;
84 * Remote peer's public key.
86 struct GNUNET_CRYPTO_EddsaPublicKey *peers_eph;
89 * Encryption ("our") key.
91 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
94 * Decryption ("their") key.
96 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
99 * Paths that are actively used to reach the destination peer.
101 struct MeshTConnection *connection_head;
102 struct MeshTConnection *connection_tail;
105 * Next connection number.
110 * Channels inside this tunnel.
112 struct MeshTChannel *channel_head;
113 struct MeshTChannel *channel_tail;
116 * Channel ID for the next created channel.
118 MESH_ChannelNumber next_chid;
121 * Destroy flag: if true, destroy on last message.
126 * Queued messages, to transmit once tunnel gets connected.
128 struct MeshTunnelQueue *tq_head;
129 struct MeshTunnelQueue *tq_tail;
134 * Struct used to queue messages in a tunnel.
136 struct MeshTunnelQueue
141 struct MeshTunnelQueue *next;
142 struct MeshTunnelQueue *prev;
147 struct MeshChannel *ch;
152 /* struct GNUNET_MessageHeader *msg; */
155 /******************************************************************************/
156 /******************************* GLOBALS ***********************************/
157 /******************************************************************************/
160 * Global handle to the statistics service.
162 extern struct GNUNET_STATISTICS_Handle *stats;
165 * Local peer own ID (memory efficient handle).
167 extern GNUNET_PEER_Id myid;
170 * Local peer own ID (full value).
172 extern struct GNUNET_PeerIdentity my_full_id;
175 * Default TTL for payload packets.
177 static unsigned long long default_ttl;
182 const static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
185 /******************************************************************************/
186 /******************************** STATIC ***********************************/
187 /******************************************************************************/
190 * Get string description for tunnel state.
192 * @param s Tunnel state.
194 * @return String representation.
197 GMT_state2s (enum MeshTunnel3State s)
199 static char buf[128];
203 case MESH_TUNNEL3_NEW:
204 return "MESH_TUNNEL3_NEW";
205 case MESH_TUNNEL3_SEARCHING:
206 return "MESH_TUNNEL3_SEARCHING";
207 case MESH_TUNNEL3_WAITING:
208 return "MESH_TUNNEL3_WAITING";
209 case MESH_TUNNEL3_READY:
210 return "MESH_TUNNEL3_READY";
211 case MESH_TUNNEL3_RECONNECTING:
212 return "MESH_TUNNEL3_RECONNECTING";
215 sprintf (buf, "%u (UNKNOWN STATE)", s);
221 * Pick a connection on which send the next data message.
223 * @param t Tunnel on which to send the message.
224 * @param fwd Is this a fwd message?
226 * @return The connection on which to send the next message.
228 static struct MeshConnection *
229 tunnel_get_connection (struct MeshTunnel3 *t, int fwd)
231 struct MeshTConnection *iter;
232 struct MeshConnection *best;
234 unsigned int lowest_q;
236 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMP_2s (t->peer));
239 for (iter = t->connection_head; NULL != iter; iter = iter->next)
241 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
242 GNUNET_h2s (GMC_get_id (iter->c)), GMC_get_state (iter->c));
243 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
245 qn = GMC_get_qn (iter->c, fwd);
246 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
259 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
260 * Encrypt data with the tunnel key.
262 * @param t Tunnel whose key to use.
263 * @param dst Destination for the GMT_encrypted data.
264 * @param src Source of the plaintext.
265 * @param size Size of the plaintext.
266 * @param iv Initialization Vector to use.
267 * @param fwd Is this a fwd message?
270 GMT_encrypt (struct MeshTunnel3 *t,
271 void *dst, const void *src,
272 size_t size, uint64_t iv, int fwd)
274 memcpy (dst, src, size);
279 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
280 * Decrypt data with the tunnel key.
282 * @param t Tunnel whose key to use.
283 * @param dst Destination for the plaintext.
284 * @param src Source of the GMT_encrypted data.
285 * @param size Size of the GMT_encrypted data.
286 * @param iv Initialization Vector to use.
287 * @param fwd Is this a fwd message?
290 GMT_decrypt (struct MeshTunnel3 *t,
291 void *dst, const void *src,
292 size_t size, uint64_t iv, int fwd)
294 memcpy (dst, src, size);
299 handle_data (struct MeshTunnel3 *t,
300 const struct GNUNET_MESH_Data *msg,
303 struct MeshChannel *ch;
308 size = ntohs (msg->header.size);
310 sizeof (struct GNUNET_MESH_Data) +
311 sizeof (struct GNUNET_MessageHeader))
316 type = ntohs (msg->header.type);
317 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
318 GNUNET_MESH_DEBUG_M2S (type));
319 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
320 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
323 ch = GMT_get_channel (t, ntohl (msg->chid));
326 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
328 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
333 GMT_change_state (t, MESH_TUNNEL3_READY);
334 GMCH_handle_data (ch, msg, fwd);
338 handle_data_ack (struct MeshTunnel3 *t,
339 const struct GNUNET_MESH_DataACK *msg,
342 struct MeshChannel *ch;
346 size = ntohs (msg->header.size);
347 if (size != sizeof (struct GNUNET_MESH_DataACK))
354 ch = GMT_get_channel (t, ntohl (msg->chid));
357 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
359 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
364 GMCH_handle_data_ack (ch, msg, fwd);
368 handle_ch_create (struct MeshTunnel3 *t,
369 const struct GNUNET_MESH_ChannelCreate *msg,
372 struct MeshChannel *ch;
376 size = ntohs (msg->header.size);
377 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
384 ch = GMT_get_channel (t, ntohl (msg->chid));
387 /* Probably a retransmission, safe to ignore */
388 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
392 ch = GMCH_handle_create (t, msg, fwd);
394 GMT_add_channel (t, ch);
398 handle_ch_ack (struct MeshTunnel3 *t,
399 const struct GNUNET_MESH_ChannelManage *msg,
402 struct MeshChannel *ch;
406 size = ntohs (msg->header.size);
407 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
414 ch = GMT_get_channel (t, ntohl (msg->chid));
417 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
419 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
424 GMCH_handle_ack (ch, msg, fwd);
428 handle_ch_destroy (struct MeshTunnel3 *t,
429 const struct GNUNET_MESH_ChannelManage *msg,
432 struct MeshChannel *ch;
436 size = ntohs (msg->header.size);
437 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
444 ch = GMT_get_channel (t, ntohl (msg->chid));
447 /* Probably a retransmission, safe to ignore */
451 GMCH_handle_destroy (ch, msg, fwd);
456 * Demultiplex by message type and call appropriate handler for a message
457 * towards a channel of a local tunnel.
459 * @param t Tunnel this message came on.
460 * @param msgh Message header.
461 * @param fwd Is this message fwd?
464 handle_decrypted (struct MeshTunnel3 *t,
465 const struct GNUNET_MessageHeader *msgh,
470 type = ntohs (msgh->type);
471 LOG (GNUNET_ERROR_TYPE_DEBUG,
472 "Got a %s message!\n",
473 GNUNET_MESH_DEBUG_M2S (type));
477 case GNUNET_MESSAGE_TYPE_MESH_DATA:
478 /* Don't send hop ACK, wait for client to ACK */
479 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
482 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
483 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
486 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
488 (struct GNUNET_MESH_ChannelCreate *) msgh,
492 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
494 (struct GNUNET_MESH_ChannelManage *) msgh,
498 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
499 handle_ch_destroy (t,
500 (struct GNUNET_MESH_ChannelManage *) msgh,
506 LOG (GNUNET_ERROR_TYPE_DEBUG,
507 "end-to-end message not known (%u)\n",
512 /******************************************************************************/
513 /******************************** API ***********************************/
514 /******************************************************************************/
518 * Decrypt and demultiplex by message type. Call appropriate handler
521 * @param t Tunnel this message came on.
522 * @param msg Encrypted message.
523 * @param fwd Is this message fwd?
526 GMT_handle_encrypted (struct MeshTunnel3 *t,
527 const struct GNUNET_MESH_Encrypted *msg,
530 size_t size = ntohs (msg->header.size);
531 size_t payload_size = size - sizeof (struct GNUNET_MESH_Encrypted);
532 char cbuf[payload_size];
533 struct GNUNET_MessageHeader *msgh;
536 GMT_decrypt (t, cbuf, &msg[1], payload_size, msg->iv, fwd);
538 while (off < payload_size)
540 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
541 handle_decrypted (t, msgh, fwd);
542 off += ntohs (msgh->size);
548 * Cache a message to be sent once tunnel is online.
550 * @param t Tunnel to hold the message.
551 * @param ch Channel the message is about.
552 * @param msg Message itself (copy will be made).
553 * @param fwd Is this fwd?
556 GMT_queue_data (struct MeshTunnel3 *t,
557 struct MeshChannel *ch,
558 struct GNUNET_MessageHeader *msg,
561 struct MeshTunnelQueue *tq;
562 uint16_t size = ntohs (msg->size);
564 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
567 memcpy (&tq[1], msg, size);
568 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
570 if (MESH_TUNNEL3_READY == t->state)
571 GMT_send_queued_data (t, fwd);
576 * Send all cached messages that we can, tunnel is online.
578 * @param t Tunnel that holds the messages.
579 * @param fwd Is this fwd?
582 GMT_send_queued_data (struct MeshTunnel3 *t, int fwd)
584 struct MeshTunnelQueue *tq;
585 struct MeshTunnelQueue *next;
588 LOG (GNUNET_ERROR_TYPE_DEBUG,
589 "GMT_send_queued_data on tunnel %s\n",
591 room = GMT_get_buffer (t, fwd);
592 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
593 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
595 LOG (GNUNET_ERROR_TYPE_DEBUG, " data on channel %s\n", GMCH_2s (tq->ch));
598 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
599 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
604 LOG (GNUNET_ERROR_TYPE_DEBUG,
605 "GMT_send_queued_data end\n",
611 * Initialize the tunnel subsystem.
613 * @param c Configuration handle.
614 * @param key ECC private key, to derive all other keys and do crypto.
617 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
618 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
620 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
622 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
625 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
626 "MESH", "DEFAULT_TTL", "USING DEFAULT");
629 my_private_key = key;
634 * Shut down the tunnel subsystem.
645 * @param destination Peer this tunnel is towards.
648 GMT_new (struct MeshPeer *destination)
650 struct MeshTunnel3 *t;
652 t = GNUNET_new (struct MeshTunnel3);
654 t->peer = destination;
656 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
657 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
660 // tunnel_destroy (t);
664 // char salt[] = "salt";
665 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
666 // salt, sizeof (salt),
667 // &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
668 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
669 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
671 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
672 // salt, sizeof (salt),
673 // &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
674 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
675 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
683 * Change the tunnel state.
685 * @param t Tunnel whose state to change.
686 * @param state New state.
689 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnel3State state)
693 LOG (GNUNET_ERROR_TYPE_DEBUG,
694 "Tunnel %s state was %s\n",
696 GMT_state2s (t->state));
697 LOG (GNUNET_ERROR_TYPE_DEBUG,
698 "Tunnel %s state is now %s\n",
700 GMT_state2s (state));
702 if (MESH_TUNNEL3_READY == state && 3 <= GMT_count_connections (t))
704 GMP_stop_search (t->peer);
710 * Add a connection to a tunnel.
713 * @param c Connection.
716 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
718 struct MeshTConnection *aux;
720 for (aux = t->connection_head; aux != NULL; aux = aux->next)
724 aux = GNUNET_new (struct MeshTConnection);
726 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
731 * Remove a connection from a tunnel.
734 * @param c Connection.
737 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
739 struct MeshTConnection *aux;
741 for (aux = t->connection_head; aux != NULL; aux = aux->next)
744 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
752 * Add a channel to a tunnel.
758 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
760 struct MeshTChannel *aux;
762 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
764 for (aux = t->channel_head; aux != NULL; aux = aux->next)
766 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
771 aux = GNUNET_new (struct MeshTChannel);
773 LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
774 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
779 * Remove a channel from a tunnel.
785 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
787 struct MeshTChannel *aux;
789 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
790 for (aux = t->channel_head; aux != NULL; aux = aux->next)
794 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GMCH_2s (ch));
795 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
804 * Search for a channel by global ID.
806 * @param t Tunnel containing the channel.
807 * @param chid Public channel number.
809 * @return channel handler, NULL if doesn't exist
812 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
814 struct MeshTChannel *iter;
819 for (iter = t->channel_head; NULL != iter; iter = iter->next)
821 if (GMCH_get_id (iter->ch) == chid)
825 return NULL == iter ? NULL : iter->ch;
830 * Tunnel is empty: destroy it.
832 * Notifies all connections about the destruction.
834 * @param t Tunnel to destroy.
837 GMT_destroy_empty (struct MeshTunnel3 *t)
839 struct MeshTConnection *iter;
841 for (iter = t->connection_head; NULL != iter; iter = iter->next)
843 GMC_send_destroy (iter->c);
846 t->destroy = GNUNET_YES;
851 * Destroy tunnel if empty (no more channels).
853 * @param t Tunnel to destroy if empty.
856 GMT_destroy_if_empty (struct MeshTunnel3 *t)
858 if (1 < GMT_count_channels (t))
861 GMT_destroy_empty (t);
866 * Destroy the tunnel.
868 * This function does not generate any warning traffic to clients or peers.
871 * Cancel messages belonging to this tunnel queued to neighbors.
872 * Free any allocated resources linked to the tunnel.
874 * @param t The tunnel to destroy.
877 GMT_destroy (struct MeshTunnel3 *t)
879 struct MeshTConnection *iter;
880 struct MeshTConnection *next;
885 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
887 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
890 for (iter = t->connection_head; NULL != iter; iter = next)
893 GMC_destroy (iter->c);
897 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
898 GMP_set_tunnel (t->peer, NULL);
905 * @brief Use the given path for the tunnel.
906 * Update the next and prev hops (and RCs).
907 * (Re)start the path refresh in case the tunnel is locally owned.
909 * @param t Tunnel to update.
910 * @param p Path to use.
912 * @return Connection created.
914 struct MeshConnection *
915 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
917 struct MeshConnection *c;
918 struct GNUNET_HashCode cid;
919 unsigned int own_pos;
921 if (NULL == t || NULL == p)
927 for (own_pos = 0; own_pos < p->length; own_pos++)
929 if (p->peers[own_pos] == myid)
932 if (own_pos > p->length - 1)
938 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
939 c = GMC_new (&cid, t, p, own_pos);
940 GMT_add_connection (t, c);
946 * Count established (ready) connections of a tunnel.
948 * @param t Tunnel on which to count.
950 * @return Number of connections.
953 GMT_count_connections (struct MeshTunnel3 *t)
955 struct MeshTConnection *iter;
958 for (count = 0, iter = t->connection_head;
960 iter = iter->next, count++);
966 * Count channels of a tunnel.
968 * @param t Tunnel on which to count.
970 * @return Number of channels.
973 GMT_count_channels (struct MeshTunnel3 *t)
975 struct MeshTChannel *iter;
978 for (count = 0, iter = t->channel_head;
980 iter = iter->next, count++) /* skip */;
987 * Get the state of a tunnel.
991 * @return Tunnel's state.
993 enum MeshTunnel3State
994 GMT_get_state (struct MeshTunnel3 *t)
997 return (enum MeshTunnel3State) -1;
1002 * Get the total buffer space for a tunnel.
1004 * If terminal, use the biggest channel buffer (or 64) if no channel exists.
1005 * If not terminal, use the sum of all connection buffers.
1008 * @param fwd Is this for FWD traffic?
1010 * @return Buffer space offered by all entities (c/ch) in the tunnel.
1013 GMT_get_buffer (struct MeshTunnel3 *t, int fwd)
1015 struct MeshTConnection *iter;
1016 unsigned int buffer;
1018 iter = t->connection_head;
1021 /* If terminal, return biggest channel buffer */
1022 if (NULL == iter || GMC_is_terminal (iter->c, fwd))
1024 struct MeshTChannel *iter_ch;
1025 unsigned int ch_buf;
1027 if (NULL == t->channel_head)
1029 /* Probably getting buffer for a channel create/handshake. */
1033 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = iter_ch->next)
1035 ch_buf = GMCH_get_buffer (iter_ch->ch, fwd);
1036 if (ch_buf > buffer)
1042 /* If not terminal, return sum of connection buffers */
1043 while (NULL != iter)
1045 if (GMC_get_state (iter->c) != MESH_CONNECTION_READY)
1051 buffer += GMC_get_buffer (iter->c, fwd);
1060 * Get the tunnel's destination.
1064 * @return ID of the destination peer.
1066 const struct GNUNET_PeerIdentity *
1067 GMT_get_destination (struct MeshTunnel3 *t)
1069 return GMP_get_id (t->peer);
1074 * Get the tunnel's next free global channel ID.
1078 * @return GID of a channel free to use.
1081 GMT_get_next_chid (struct MeshTunnel3 *t)
1083 MESH_ChannelNumber chid;
1085 while (NULL != GMT_get_channel (t, t->next_chid))
1087 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
1088 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1090 chid = t->next_chid;
1091 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1098 * Send ACK on one or more channels due to buffer in connections.
1100 * @param t Channel which has some free buffer space.
1101 * @param fwd Is this for FWD traffic? (ACK goes to root)
1104 GMT_unchoke_channels (struct MeshTunnel3 *t, int fwd)
1106 struct MeshTChannel *iter;
1107 unsigned int buffer;
1108 unsigned int channels = GMT_count_channels (t);
1109 unsigned int choked_n;
1110 struct MeshChannel *choked[channels];
1112 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_unchoke_channels on %s\n", GMT_2s (t));
1113 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
1114 if (NULL != t->channel_head)
1115 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
1123 /* Get buffer space */
1124 buffer = GMT_get_buffer (t, fwd);
1130 /* Count and remember choked channels */
1132 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1134 if (GNUNET_NO == GMCH_get_allowed (iter->ch, fwd))
1136 choked[choked_n++] = iter->ch;
1140 /* Unchoke random channels */
1141 while (0 < buffer && 0 < choked_n)
1143 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1145 GMCH_allow_client (choked[r], fwd);
1148 choked[r] = choked[choked_n];
1154 * Send ACK on one or more connections due to buffer space to the client.
1156 * Iterates all connections of the tunnel and sends ACKs appropriately.
1159 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
1162 GMT_send_acks (struct MeshTunnel3 *t, int fwd)
1164 struct MeshTConnection *iter;
1167 uint32_t allow_per_connection;
1169 unsigned int buffer;
1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "Tunnel send %s ACKs on %s\n",
1173 fwd ? "FWD" : "BCK", GMT_2s (t));
1180 if (NULL == t->channel_head ||
1181 GNUNET_NO == GMCH_is_origin (t->channel_head->ch, !fwd))
1187 buffer = GMT_get_buffer (t, fwd);
1189 /* Count connections, how many messages are already allowed */
1190 cs = GMT_count_connections (t);
1191 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
1193 allowed += GMC_get_allowed (iter->c, fwd);
1196 /* Make sure there is no overflow */
1197 if (allowed > buffer)
1203 /* Authorize connections to send more data */
1204 to_allow = buffer; /* - allowed; */
1206 for (iter = t->connection_head; NULL != iter && to_allow > 0; iter = iter->next)
1208 allow_per_connection = to_allow/cs;
1209 to_allow -= allow_per_connection;
1211 if (GMC_get_allowed (iter->c, fwd) > 64 / 3)
1215 GMC_allow (iter->c, buffer, fwd);
1218 GNUNET_break (to_allow == 0);
1223 * Sends an already built message on a tunnel, GMT_encrypting it and
1224 * choosing the best connection.
1226 * @param message Message to send. Function modifies it.
1227 * @param t Tunnel on which this message is transmitted.
1228 * @param ch Channel on which this message is transmitted.
1229 * @param fwd Is this a fwd message?
1232 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1233 struct MeshTunnel3 *t,
1234 struct MeshChannel *ch,
1237 struct MeshConnection *c;
1238 struct GNUNET_MESH_Encrypted *msg;
1239 size_t size = ntohs (message->size);
1240 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1244 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMT_2s (t));
1246 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1247 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1248 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
1249 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1250 msg->iv = GNUNET_htonll (iv);
1251 GMT_encrypt (t, &msg[1], message, size, iv, fwd);
1252 c = tunnel_get_connection (t, fwd);
1255 GNUNET_break (GNUNET_YES == t->destroy);
1258 type = ntohs (message->type);
1261 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1262 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1263 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1264 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1265 msg->cid = *GMC_get_id (c);
1266 msg->ttl = htonl (default_ttl);
1269 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1270 GNUNET_MESH_DEBUG_M2S (type));
1275 GMC_send_prebuilt_message (&msg->header, c, fwd);
1279 * Is the tunnel directed towards the local peer?
1283 * @return GNUNET_YES if it is loopback.
1286 GMT_is_loopback (const struct MeshTunnel3 *t)
1288 return (myid == GMP_get_short_id(t->peer));
1293 * Is the tunnel using this path already?
1298 * @return GNUNET_YES a connection uses this path.
1301 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p)
1303 struct MeshTConnection *iter;
1305 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1306 if (GMC_get_path (iter->c) == p)
1314 * Get a cost of a path for a tunnel considering existing connections.
1317 * @param path Candidate path.
1319 * @return Cost of the path (path length + number of overlapping nodes)
1322 GMT_get_path_cost (const struct MeshTunnel3 *t,
1323 const struct MeshPeerPath *path)
1325 struct MeshTConnection *iter;
1326 unsigned int overlap;
1334 GNUNET_assert (NULL != t);
1336 for (i = 0; i < path->length; i++)
1338 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1340 for (j = 0; j < GMC_get_path (iter->c)->length; j++)
1342 if (path->peers[i] == GMC_get_path (iter->c)->peers[j])
1350 return (path->length + overlap) * (path->score * -1);
1355 * Get the static string for the peer this tunnel is directed.
1359 * @return Static string the destination peer's ID.
1362 GMT_2s (const struct MeshTunnel3 *t)
1364 return GMP_2s (t->peer);