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__)
37 /******************************************************************************/
38 /******************************** STRUCTS **********************************/
39 /******************************************************************************/
43 struct MeshTChannel *next;
44 struct MeshTChannel *prev;
45 struct MeshChannel *ch;
48 struct MeshTConnection
50 struct MeshTConnection *next;
51 struct MeshTConnection *prev;
52 struct MeshConnection *c;
56 * Struct containing all information regarding a tunnel to a peer.
61 * Endpoint of the tunnel.
63 struct MeshPeer *peer;
66 * State of the tunnel.
68 enum MeshTunnel3State state;
71 * Local peer ephemeral private key
73 struct GNUNET_CRYPTO_EddsaPrivateKey *my_eph_key;
76 * Local peer ephemeral public key
78 struct GNUNET_CRYPTO_EddsaPublicKey *my_eph;
81 * Remote peer's public key.
83 struct GNUNET_CRYPTO_EddsaPublicKey *peers_eph;
86 * Encryption ("our") key.
88 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
91 * Decryption ("their") key.
93 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
96 * Paths that are actively used to reach the destination peer.
98 struct MeshTConnection *connection_head;
99 struct MeshTConnection *connection_tail;
102 * Next connection number.
107 * Channels inside this tunnel.
109 struct MeshTChannel *channel_head;
110 struct MeshTChannel *channel_tail;
113 * Channel ID for the next created channel.
115 MESH_ChannelNumber next_chid;
118 * Destroy flag: if true, destroy on last message.
123 * Queued messages, to transmit once tunnel gets connected.
125 struct MeshTunnelQueue *tq_head;
126 struct MeshTunnelQueue *tq_tail;
131 * Struct used to queue messages in a tunnel.
133 struct MeshTunnelQueue
138 struct MeshTunnelQueue *next;
139 struct MeshTunnelQueue *prev;
144 struct MeshChannel *ch;
149 /* struct GNUNET_MessageHeader *msg; */
153 /******************************************************************************/
154 /******************************* GLOBALS ***********************************/
155 /******************************************************************************/
158 * Global handle to the statistics service.
160 extern struct GNUNET_STATISTICS_Handle *stats;
163 * Local peer own ID (memory efficient handle).
165 extern GNUNET_PEER_Id myid;
168 * Local peer own ID (full value).
170 extern struct GNUNET_PeerIdentity my_full_id;
174 * Set of all tunnels, in order to trigger a new exchange on rekey.
175 * Indexed by peer's ID.
177 static struct GNUNET_CONTAINER_MultiPeerMap *tunnels;
180 * Default TTL for payload packets.
182 static unsigned long long default_ttl;
187 const static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
190 * Own ephemeral private key.
192 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
195 * Cached message used to perform a key exchange.
197 static struct GNUNET_MESH_KX kx_msg;
200 * Task to generate a new ephemeral key.
202 static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
207 static struct GNUNET_TIME_Relative rekey_period;
209 /******************************************************************************/
210 /******************************** STATIC ***********************************/
211 /******************************************************************************/
214 * Get string description for tunnel state.
216 * @param s Tunnel state.
218 * @return String representation.
221 GMT_state2s (enum MeshTunnel3State s)
223 static char buf[128];
227 case MESH_TUNNEL3_NEW:
228 return "MESH_TUNNEL3_NEW";
229 case MESH_TUNNEL3_SEARCHING:
230 return "MESH_TUNNEL3_SEARCHING";
231 case MESH_TUNNEL3_WAITING:
232 return "MESH_TUNNEL3_WAITING";
233 case MESH_TUNNEL3_READY:
234 return "MESH_TUNNEL3_READY";
235 case MESH_TUNNEL3_RECONNECTING:
236 return "MESH_TUNNEL3_RECONNECTING";
239 sprintf (buf, "%u (UNKNOWN STATE)", s);
245 * Pick a connection on which send the next data message.
247 * @param t Tunnel on which to send the message.
248 * @param fwd Is this a fwd message?
250 * @return The connection on which to send the next message.
252 static struct MeshConnection *
253 tunnel_get_connection (struct MeshTunnel3 *t, int fwd)
255 struct MeshTConnection *iter;
256 struct MeshConnection *best;
258 unsigned int lowest_q;
260 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMP_2s (t->peer));
263 for (iter = t->connection_head; NULL != iter; iter = iter->next)
265 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
266 GNUNET_h2s (GMC_get_id (iter->c)), GMC_get_state (iter->c));
267 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
269 qn = GMC_get_qn (iter->c, fwd);
270 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
283 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
284 * Encrypt data with the tunnel key.
286 * @param t Tunnel whose key to use.
287 * @param dst Destination for the encrypted data.
288 * @param src Source of the plaintext.
289 * @param size Size of the plaintext.
290 * @param iv Initialization Vector to use.
291 * @param fwd Is this a fwd message?
294 t_encrypt (struct MeshTunnel3 *t,
295 void *dst, const void *src,
296 size_t size, uint64_t iv, int fwd)
298 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
300 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (uint64_t), NULL);
301 return GNUNET_CRYPTO_symmetric_encrypt (src, size, &t->e_key, &siv, dst);
306 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
307 * Decrypt data with the tunnel key.
309 * @param t Tunnel whose key to use.
310 * @param dst Destination for the plaintext.
311 * @param src Source of the encrypted data.
312 * @param size Size of the encrypted data.
313 * @param iv Initialization Vector to use.
314 * @param fwd Is this a fwd message?
317 t_decrypt (struct MeshTunnel3 *t,
318 void *dst, const void *src,
319 size_t size, uint64_t iv, int fwd)
321 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
323 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (uint64_t), NULL);
324 return GNUNET_CRYPTO_symmetric_decrypt (src, size, &t->d_key, &siv, dst);
329 handle_data (struct MeshTunnel3 *t,
330 const struct GNUNET_MESH_Data *msg,
333 struct MeshChannel *ch;
338 size = ntohs (msg->header.size);
340 sizeof (struct GNUNET_MESH_Data) +
341 sizeof (struct GNUNET_MessageHeader))
346 type = ntohs (msg->header.type);
347 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
348 GNUNET_MESH_DEBUG_M2S (type));
349 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
350 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
353 ch = GMT_get_channel (t, ntohl (msg->chid));
356 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
358 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
363 GMT_change_state (t, MESH_TUNNEL3_READY);
364 GMCH_handle_data (ch, msg, fwd);
368 handle_data_ack (struct MeshTunnel3 *t,
369 const struct GNUNET_MESH_DataACK *msg,
372 struct MeshChannel *ch;
376 size = ntohs (msg->header.size);
377 if (size != sizeof (struct GNUNET_MESH_DataACK))
384 ch = GMT_get_channel (t, ntohl (msg->chid));
387 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
389 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
394 GMCH_handle_data_ack (ch, msg, fwd);
398 handle_ch_create (struct MeshTunnel3 *t,
399 const struct GNUNET_MESH_ChannelCreate *msg,
402 struct MeshChannel *ch;
406 size = ntohs (msg->header.size);
407 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
414 ch = GMT_get_channel (t, ntohl (msg->chid));
417 /* Probably a retransmission, safe to ignore */
418 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
422 ch = GMCH_handle_create (t, msg, fwd);
424 GMT_add_channel (t, ch);
428 handle_ch_ack (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 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
449 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
454 GMCH_handle_ack (ch, msg, fwd);
458 handle_ch_destroy (struct MeshTunnel3 *t,
459 const struct GNUNET_MESH_ChannelManage *msg,
462 struct MeshChannel *ch;
466 size = ntohs (msg->header.size);
467 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
474 ch = GMT_get_channel (t, ntohl (msg->chid));
477 /* Probably a retransmission, safe to ignore */
481 GMCH_handle_destroy (ch, msg, fwd);
486 * Demultiplex by message type and call appropriate handler for a message
487 * towards a channel of a local tunnel.
489 * @param t Tunnel this message came on.
490 * @param msgh Message header.
491 * @param fwd Is this message fwd?
494 handle_decrypted (struct MeshTunnel3 *t,
495 const struct GNUNET_MessageHeader *msgh,
500 type = ntohs (msgh->type);
501 LOG (GNUNET_ERROR_TYPE_DEBUG,
502 "Got a %s message!\n",
503 GNUNET_MESH_DEBUG_M2S (type));
507 case GNUNET_MESSAGE_TYPE_MESH_DATA:
508 /* Don't send hop ACK, wait for client to ACK */
509 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
512 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
513 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
516 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
518 (struct GNUNET_MESH_ChannelCreate *) msgh,
522 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
524 (struct GNUNET_MESH_ChannelManage *) msgh,
528 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
529 handle_ch_destroy (t,
530 (struct GNUNET_MESH_ChannelManage *) msgh,
536 LOG (GNUNET_ERROR_TYPE_DEBUG,
537 "end-to-end message not known (%u)\n",
542 /******************************************************************************/
543 /******************************** API ***********************************/
544 /******************************************************************************/
548 GMT_send_kx (struct MeshTunnel3 *t)
556 * Decrypt and demultiplex by message type. Call appropriate handler
559 * @param t Tunnel this message came on.
560 * @param msg Encrypted message.
561 * @param fwd Is this message fwd?
564 GMT_handle_encrypted (struct MeshTunnel3 *t,
565 const struct GNUNET_MESH_Encrypted *msg,
568 size_t size = ntohs (msg->header.size);
569 size_t payload_size = size - sizeof (struct GNUNET_MESH_Encrypted);
570 size_t decrypted_size;
571 char cbuf [payload_size];
572 struct GNUNET_MessageHeader *msgh;
575 decrypted_size = t_decrypt (t, cbuf, &msg[1], payload_size, msg->iv, fwd);
577 while (off < decrypted_size)
579 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
580 handle_decrypted (t, msgh, fwd);
581 off += ntohs (msgh->size);
587 * Cache a message to be sent once tunnel is online.
589 * @param t Tunnel to hold the message.
590 * @param ch Channel the message is about.
591 * @param msg Message itself (copy will be made).
592 * @param fwd Is this fwd?
595 GMT_queue_data (struct MeshTunnel3 *t,
596 struct MeshChannel *ch,
597 struct GNUNET_MessageHeader *msg,
600 struct MeshTunnelQueue *tq;
601 uint16_t size = ntohs (msg->size);
603 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
606 memcpy (&tq[1], msg, size);
607 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
609 if (MESH_TUNNEL3_READY == t->state)
610 GMT_send_queued_data (t, fwd);
615 * Send all cached messages that we can, tunnel is online.
617 * @param t Tunnel that holds the messages.
618 * @param fwd Is this fwd?
621 GMT_send_queued_data (struct MeshTunnel3 *t, int fwd)
623 struct MeshTunnelQueue *tq;
624 struct MeshTunnelQueue *next;
627 LOG (GNUNET_ERROR_TYPE_DEBUG,
628 "GMT_send_queued_data on tunnel %s\n",
630 room = GMT_get_buffer (t, fwd);
631 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
632 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
634 LOG (GNUNET_ERROR_TYPE_DEBUG, " data on channel %s\n", GMCH_2s (tq->ch));
637 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
638 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
643 LOG (GNUNET_ERROR_TYPE_DEBUG,
644 "GMT_send_queued_data end\n",
649 rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
651 rekey_task = GNUNET_SCHEDULER_NO_TASK;
653 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
656 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
657 kx_msg.header.size = htons (sizeof (kx_msg));
659 rekey_task = GNUNET_SCHEDULER_add_delayed (rekey_period, &rekey, NULL);
663 * Initialize the tunnel subsystem.
665 * @param c Configuration handle.
666 * @param key ECC private key, to derive all other keys and do crypto.
669 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
670 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
672 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
674 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
677 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
678 "MESH", "DEFAULT_TTL", "USING DEFAULT");
682 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REKEY_PERIOD",
685 rekey_period = GNUNET_TIME_UNIT_DAYS;
688 my_private_key = key;
689 rekey_task = GNUNET_SCHEDULER_add_now (&rekey, NULL);
691 tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
696 * Shut down the tunnel subsystem.
701 if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
703 GNUNET_SCHEDULER_cancel (rekey_task);
704 rekey_task = GNUNET_SCHEDULER_NO_TASK;
706 GNUNET_CONTAINER_multipeermap_destroy (tunnels);
713 * @param destination Peer this tunnel is towards.
716 GMT_new (struct MeshPeer *destination)
718 struct MeshTunnel3 *t;
720 t = GNUNET_new (struct MeshTunnel3);
722 t->peer = destination;
724 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
725 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
728 // tunnel_destroy (t);
732 // char salt[] = "salt";
733 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
734 // salt, sizeof (salt),
735 // &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
736 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
737 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
739 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
740 // salt, sizeof (salt),
741 // &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
742 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
743 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
746 GNUNET_CONTAINER_multipeermap_put (tunnels, GMP_get_id (destination), t,
747 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
753 * Change the tunnel state.
755 * @param t Tunnel whose state to change.
756 * @param state New state.
759 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnel3State state)
763 LOG (GNUNET_ERROR_TYPE_DEBUG,
764 "Tunnel %s state was %s\n",
766 GMT_state2s (t->state));
767 LOG (GNUNET_ERROR_TYPE_DEBUG,
768 "Tunnel %s state is now %s\n",
770 GMT_state2s (state));
772 if (MESH_TUNNEL3_READY == state && 3 <= GMT_count_connections (t))
774 GMP_stop_search (t->peer);
780 * Add a connection to a tunnel.
783 * @param c Connection.
786 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
788 struct MeshTConnection *aux;
790 for (aux = t->connection_head; aux != NULL; aux = aux->next)
794 aux = GNUNET_new (struct MeshTConnection);
796 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
801 * Remove a connection from a tunnel.
804 * @param c Connection.
807 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
809 struct MeshTConnection *aux;
811 for (aux = t->connection_head; aux != NULL; aux = aux->next)
814 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
822 * Add a channel to a tunnel.
828 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
830 struct MeshTChannel *aux;
832 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
834 for (aux = t->channel_head; aux != NULL; aux = aux->next)
836 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
841 aux = GNUNET_new (struct MeshTChannel);
843 LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
844 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
849 * Remove a channel from a tunnel.
855 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
857 struct MeshTChannel *aux;
859 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
860 for (aux = t->channel_head; aux != NULL; aux = aux->next)
864 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GMCH_2s (ch));
865 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
874 * Search for a channel by global ID.
876 * @param t Tunnel containing the channel.
877 * @param chid Public channel number.
879 * @return channel handler, NULL if doesn't exist
882 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
884 struct MeshTChannel *iter;
889 for (iter = t->channel_head; NULL != iter; iter = iter->next)
891 if (GMCH_get_id (iter->ch) == chid)
895 return NULL == iter ? NULL : iter->ch;
900 * Tunnel is empty: destroy it.
902 * Notifies all connections about the destruction.
904 * @param t Tunnel to destroy.
907 GMT_destroy_empty (struct MeshTunnel3 *t)
909 struct MeshTConnection *iter;
911 for (iter = t->connection_head; NULL != iter; iter = iter->next)
913 GMC_send_destroy (iter->c);
916 t->destroy = GNUNET_YES;
921 * Destroy tunnel if empty (no more channels).
923 * @param t Tunnel to destroy if empty.
926 GMT_destroy_if_empty (struct MeshTunnel3 *t)
928 if (1 < GMT_count_channels (t))
931 GMT_destroy_empty (t);
936 * Destroy the tunnel.
938 * This function does not generate any warning traffic to clients or peers.
941 * Cancel messages belonging to this tunnel queued to neighbors.
942 * Free any allocated resources linked to the tunnel.
944 * @param t The tunnel to destroy.
947 GMT_destroy (struct MeshTunnel3 *t)
949 struct MeshTConnection *iter;
950 struct MeshTConnection *next;
955 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
957 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
960 for (iter = t->connection_head; NULL != iter; iter = next)
963 GMC_destroy (iter->c);
966 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
967 GMP_set_tunnel (t->peer, NULL);
974 * @brief Use the given path for the tunnel.
975 * Update the next and prev hops (and RCs).
976 * (Re)start the path refresh in case the tunnel is locally owned.
978 * @param t Tunnel to update.
979 * @param p Path to use.
981 * @return Connection created.
983 struct MeshConnection *
984 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
986 struct MeshConnection *c;
987 struct GNUNET_HashCode cid;
988 unsigned int own_pos;
990 if (NULL == t || NULL == p)
996 for (own_pos = 0; own_pos < p->length; own_pos++)
998 if (p->peers[own_pos] == myid)
1001 if (own_pos > p->length - 1)
1007 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
1008 c = GMC_new (&cid, t, p, own_pos);
1009 GMT_add_connection (t, c);
1015 * Count established (ready) connections of a tunnel.
1017 * @param t Tunnel on which to count.
1019 * @return Number of connections.
1022 GMT_count_connections (struct MeshTunnel3 *t)
1024 struct MeshTConnection *iter;
1027 for (count = 0, iter = t->connection_head;
1029 iter = iter->next, count++);
1035 * Count channels of a tunnel.
1037 * @param t Tunnel on which to count.
1039 * @return Number of channels.
1042 GMT_count_channels (struct MeshTunnel3 *t)
1044 struct MeshTChannel *iter;
1047 for (count = 0, iter = t->channel_head;
1049 iter = iter->next, count++) /* skip */;
1056 * Get the state of a tunnel.
1060 * @return Tunnel's state.
1062 enum MeshTunnel3State
1063 GMT_get_state (struct MeshTunnel3 *t)
1066 return (enum MeshTunnel3State) -1;
1071 * Get the total buffer space for a tunnel.
1073 * If terminal, use the biggest channel buffer (or 64) if no channel exists.
1074 * If not terminal, use the sum of all connection buffers.
1077 * @param fwd Is this for FWD traffic?
1079 * @return Buffer space offered by all entities (c/ch) in the tunnel.
1082 GMT_get_buffer (struct MeshTunnel3 *t, int fwd)
1084 struct MeshTConnection *iter;
1085 unsigned int buffer;
1087 iter = t->connection_head;
1090 /* If terminal, return biggest channel buffer */
1091 if (NULL == iter || GMC_is_terminal (iter->c, fwd))
1093 struct MeshTChannel *iter_ch;
1094 unsigned int ch_buf;
1096 if (NULL == t->channel_head)
1098 /* Probably getting buffer for a channel create/handshake. */
1102 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = iter_ch->next)
1104 ch_buf = GMCH_get_buffer (iter_ch->ch, fwd);
1105 if (ch_buf > buffer)
1111 /* If not terminal, return sum of connection buffers */
1112 while (NULL != iter)
1114 if (GMC_get_state (iter->c) != MESH_CONNECTION_READY)
1120 buffer += GMC_get_buffer (iter->c, fwd);
1129 * Get the tunnel's destination.
1133 * @return ID of the destination peer.
1135 const struct GNUNET_PeerIdentity *
1136 GMT_get_destination (struct MeshTunnel3 *t)
1138 return GMP_get_id (t->peer);
1143 * Get the tunnel's next free global channel ID.
1147 * @return GID of a channel free to use.
1150 GMT_get_next_chid (struct MeshTunnel3 *t)
1152 MESH_ChannelNumber chid;
1154 while (NULL != GMT_get_channel (t, t->next_chid))
1156 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
1157 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1159 chid = t->next_chid;
1160 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1167 * Send ACK on one or more channels due to buffer in connections.
1169 * @param t Channel which has some free buffer space.
1170 * @param fwd Is this for FWD traffic? (ACK goes to root)
1173 GMT_unchoke_channels (struct MeshTunnel3 *t, int fwd)
1175 struct MeshTChannel *iter;
1176 unsigned int buffer;
1177 unsigned int channels = GMT_count_channels (t);
1178 unsigned int choked_n;
1179 struct MeshChannel *choked[channels];
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_unchoke_channels on %s\n", GMT_2s (t));
1182 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
1183 if (NULL != t->channel_head)
1184 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
1192 /* Get buffer space */
1193 buffer = GMT_get_buffer (t, fwd);
1199 /* Count and remember choked channels */
1201 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1203 if (GNUNET_NO == GMCH_get_allowed (iter->ch, fwd))
1205 choked[choked_n++] = iter->ch;
1209 /* Unchoke random channels */
1210 while (0 < buffer && 0 < choked_n)
1212 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1214 GMCH_allow_client (choked[r], fwd);
1217 choked[r] = choked[choked_n];
1223 * Send ACK on one or more connections due to buffer space to the client.
1225 * Iterates all connections of the tunnel and sends ACKs appropriately.
1228 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
1231 GMT_send_acks (struct MeshTunnel3 *t, int fwd)
1233 struct MeshTConnection *iter;
1236 uint32_t allow_per_connection;
1238 unsigned int buffer;
1240 LOG (GNUNET_ERROR_TYPE_DEBUG,
1241 "Tunnel send %s ACKs on %s\n",
1242 fwd ? "FWD" : "BCK", GMT_2s (t));
1249 if (NULL == t->channel_head ||
1250 GNUNET_NO == GMCH_is_origin (t->channel_head->ch, !fwd))
1256 buffer = GMT_get_buffer (t, fwd);
1258 /* Count connections, how many messages are already allowed */
1259 cs = GMT_count_connections (t);
1260 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
1262 allowed += GMC_get_allowed (iter->c, fwd);
1265 /* Make sure there is no overflow */
1266 if (allowed > buffer)
1272 /* Authorize connections to send more data */
1273 to_allow = buffer; /* - allowed; */
1275 for (iter = t->connection_head; NULL != iter && to_allow > 0; iter = iter->next)
1277 allow_per_connection = to_allow/cs;
1278 to_allow -= allow_per_connection;
1280 if (GMC_get_allowed (iter->c, fwd) > 64 / 3)
1284 GMC_allow (iter->c, buffer, fwd);
1287 GNUNET_break (to_allow == 0);
1292 * Sends an already built message on a tunnel, encrypting it and
1293 * choosing the best connection.
1295 * @param message Message to send. Function modifies it.
1296 * @param t Tunnel on which this message is transmitted.
1297 * @param ch Channel on which this message is transmitted.
1298 * @param fwd Is this a fwd message?
1301 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1302 struct MeshTunnel3 *t,
1303 struct MeshChannel *ch,
1306 struct MeshConnection *c;
1307 struct GNUNET_MESH_Encrypted *msg;
1308 size_t size = ntohs (message->size);
1309 size_t encrypted_size;
1310 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size + 64];
1314 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMT_2s (t));
1316 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1317 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1318 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
1319 msg->iv = GNUNET_htonll (iv);
1320 encrypted_size = t_encrypt (t, &msg[1], message, size, iv, fwd);
1321 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + encrypted_size);
1322 c = tunnel_get_connection (t, fwd);
1325 GNUNET_break (GNUNET_YES == t->destroy);
1328 type = ntohs (message->type);
1331 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1332 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1333 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1334 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1335 msg->cid = *GMC_get_id (c);
1336 msg->ttl = htonl (default_ttl);
1339 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1340 GNUNET_MESH_DEBUG_M2S (type));
1344 GMC_send_prebuilt_message (&msg->header, c, fwd);
1348 * Is the tunnel directed towards the local peer?
1352 * @return GNUNET_YES if it is loopback.
1355 GMT_is_loopback (const struct MeshTunnel3 *t)
1357 return (myid == GMP_get_short_id(t->peer));
1362 * Is the tunnel using this path already?
1367 * @return GNUNET_YES a connection uses this path.
1370 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p)
1372 struct MeshTConnection *iter;
1374 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1375 if (GMC_get_path (iter->c) == p)
1383 * Get a cost of a path for a tunnel considering existing connections.
1386 * @param path Candidate path.
1388 * @return Cost of the path (path length + number of overlapping nodes)
1391 GMT_get_path_cost (const struct MeshTunnel3 *t,
1392 const struct MeshPeerPath *path)
1394 struct MeshTConnection *iter;
1395 unsigned int overlap;
1403 GNUNET_assert (NULL != t);
1405 for (i = 0; i < path->length; i++)
1407 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1409 for (j = 0; j < GMC_get_path (iter->c)->length; j++)
1411 if (path->peers[i] == GMC_get_path (iter->c)->peers[j])
1419 return (path->length + overlap) * (path->score * -1);
1424 * Get the static string for the peer this tunnel is directed.
1428 * @return Static string the destination peer's ID.
1431 GMT_2s (const struct MeshTunnel3 *t)
1433 return GMP_2s (t->peer);