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_signatures.h"
25 #include "gnunet_statistics_service.h"
27 #include "mesh_protocol_enc.h"
28 #include "mesh_path.h"
30 #include "gnunet-service-mesh_tunnel.h"
31 #include "gnunet-service-mesh_connection.h"
32 #include "gnunet-service-mesh_channel.h"
33 #include "gnunet-service-mesh_peer.h"
35 #define LOG(level, ...) GNUNET_log_from(level,"mesh-tun",__VA_ARGS__)
37 #define REKEY_WAIT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
39 /******************************************************************************/
40 /******************************** STRUCTS **********************************/
41 /******************************************************************************/
45 struct MeshTChannel *next;
46 struct MeshTChannel *prev;
47 struct MeshChannel *ch;
50 struct MeshTConnection
52 struct MeshTConnection *next;
53 struct MeshTConnection *prev;
54 struct MeshConnection *c;
58 * Struct containing all information regarding a tunnel to a peer.
63 * Endpoint of the tunnel.
65 struct MeshPeer *peer;
68 * State of the tunnel.
70 enum MeshTunnel3State state;
73 * Encryption ("our") key.
75 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
78 * Decryption ("their") key.
80 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
83 * Encryption ("our") key.
85 struct GNUNET_CRYPTO_SymmetricSessionKey e_key_old;
88 * Decryption ("their") key.
90 struct GNUNET_CRYPTO_SymmetricSessionKey d_key_old;
93 * Task to start the rekey process.
95 GNUNET_SCHEDULER_TaskIdentifier rekey_task;
98 * Paths that are actively used to reach the destination peer.
100 struct MeshTConnection *connection_head;
101 struct MeshTConnection *connection_tail;
104 * Next connection number.
109 * Channels inside this tunnel.
111 struct MeshTChannel *channel_head;
112 struct MeshTChannel *channel_tail;
115 * Channel ID for the next created channel.
117 MESH_ChannelNumber next_chid;
120 * Destroy flag: if true, destroy on last message.
125 * Queued messages, to transmit once tunnel gets connected.
127 struct MeshTunnelQueue *tq_head;
128 struct MeshTunnelQueue *tq_tail;
133 * Struct used to queue messages in a tunnel.
135 struct MeshTunnelQueue
140 struct MeshTunnelQueue *next;
141 struct MeshTunnelQueue *prev;
146 struct MeshChannel *ch;
151 /* 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;
176 * Set of all tunnels, in order to trigger a new exchange on rekey.
177 * Indexed by peer's ID.
179 static struct GNUNET_CONTAINER_MultiPeerMap *tunnels;
182 * Default TTL for payload packets.
184 static unsigned long long default_ttl;
189 const static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
192 * Own ephemeral private key.
194 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
197 * Cached message used to perform a key exchange.
199 static struct GNUNET_MESH_KX kx_msg;
202 * Task to generate a new ephemeral key.
204 static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
209 static struct GNUNET_TIME_Relative rekey_period;
211 /******************************************************************************/
212 /******************************** STATIC ***********************************/
213 /******************************************************************************/
216 * Get string description for tunnel state.
218 * @param s Tunnel state.
220 * @return String representation.
223 GMT_state2s (enum MeshTunnel3State s)
225 static char buf[128];
229 case MESH_TUNNEL3_NEW:
230 return "MESH_TUNNEL3_NEW";
231 case MESH_TUNNEL3_SEARCHING:
232 return "MESH_TUNNEL3_SEARCHING";
233 case MESH_TUNNEL3_WAITING:
234 return "MESH_TUNNEL3_WAITING";
235 case MESH_TUNNEL3_READY:
236 return "MESH_TUNNEL3_READY";
237 case MESH_TUNNEL3_RECONNECTING:
238 return "MESH_TUNNEL3_RECONNECTING";
241 sprintf (buf, "%u (UNKNOWN STATE)", s);
247 * Pick a connection on which send the next data message.
249 * @param t Tunnel on which to send the message.
250 * @param fwd Is this a fwd message?
252 * @return The connection on which to send the next message.
254 static struct MeshConnection *
255 tunnel_get_connection (struct MeshTunnel3 *t, int fwd)
257 struct MeshTConnection *iter;
258 struct MeshConnection *best;
260 unsigned int lowest_q;
262 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMP_2s (t->peer));
265 for (iter = t->connection_head; NULL != iter; iter = iter->next)
267 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
268 GNUNET_h2s (GMC_get_id (iter->c)), GMC_get_state (iter->c));
269 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
271 qn = GMC_get_qn (iter->c, fwd);
272 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
285 * Send the ephemeral key on a tunnel.
287 * @param t Tunnel on which to send the key.
290 send_ephemeral (struct MeshTunnel3 *t)
292 kx_msg.sender_status = htonl (t->state);
294 /* When channel is NULL, fwd is irrelevant. */
295 GMT_send_prebuilt_message (&kx_msg.header, t, NULL, GNUNET_YES);
300 * Initiate a rekey with the remote peer.
302 * @param cls Closure (tunnel).
303 * @param tc TaskContext.
306 rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
308 struct MeshTunnel3 *t = cls;
310 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
312 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
316 t->rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_WAIT, &rekey_tunnel, t);
321 * Out ephemeral key has changed, create new session key on all tunnels.
323 * @param cls Closure (size of the hashmap).
324 * @param key Current public key.
325 * @param value Value in the hash map (tunnel).
327 * @return #GNUNET_YES, so we should continue to iterate,
330 rekey_iterator (void *cls,
331 const struct GNUNET_PeerIdentity *key,
334 struct MeshTunnel3 *t = value;
335 struct GNUNET_TIME_Relative delay;
339 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t) n * 100);
340 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, r);
341 t->rekey_task = GNUNET_SCHEDULER_add_delayed (delay, &rekey_tunnel, t);
348 * Create a new ephemeral key and key message, schedule next rekeying.
350 * @param cls Closure (unused).
351 * @param tc TaskContext.
354 rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
356 struct GNUNET_TIME_Absolute time;
359 rekey_task = GNUNET_SCHEDULER_NO_TASK;
361 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
364 GNUNET_free_non_null (my_ephemeral_key);
365 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
367 time = GNUNET_TIME_absolute_get ();
368 kx_msg.creation_time = GNUNET_TIME_absolute_hton (time);
369 time = GNUNET_TIME_absolute_add (time, rekey_period);
370 time = GNUNET_TIME_absolute_add (time, GNUNET_TIME_UNIT_MINUTES);
371 kx_msg.expiration_time = GNUNET_TIME_absolute_hton (time);
372 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key, &kx_msg.ephemeral_key);
374 GNUNET_assert (GNUNET_OK ==
375 GNUNET_CRYPTO_eddsa_sign (my_private_key,
379 n = (long) GNUNET_CONTAINER_multipeermap_size (tunnels);
380 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &rekey_iterator, (void *) n);
382 rekey_task = GNUNET_SCHEDULER_add_delayed (rekey_period, &rekey, NULL);
387 * Encrypt data with the tunnel key.
389 * @param t Tunnel whose key to use.
390 * @param dst Destination for the encrypted data.
391 * @param src Source of the plaintext.
392 * @param size Size of the plaintext.
393 * @param iv Initialization Vector to use.
394 * @param fwd Is this a fwd message?
397 t_encrypt (struct MeshTunnel3 *t,
398 void *dst, const void *src,
399 size_t size, uint64_t iv, int fwd)
401 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
403 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (uint64_t), NULL);
404 return GNUNET_CRYPTO_symmetric_encrypt (src, size, &t->e_key, &siv, dst);
409 * Decrypt data with the tunnel key.
411 * @param t Tunnel whose key to use.
412 * @param dst Destination for the plaintext.
413 * @param src Source of the encrypted data.
414 * @param size Size of the encrypted data.
415 * @param iv Initialization Vector to use.
416 * @param fwd Is this a fwd message?
419 t_decrypt (struct MeshTunnel3 *t,
420 void *dst, const void *src,
421 size_t size, uint64_t iv, int fwd)
423 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
425 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (uint64_t), NULL);
426 return GNUNET_CRYPTO_symmetric_decrypt (src, size, &t->d_key, &siv, dst);
431 handle_data (struct MeshTunnel3 *t,
432 const struct GNUNET_MESH_Data *msg,
435 struct MeshChannel *ch;
440 size = ntohs (msg->header.size);
442 sizeof (struct GNUNET_MESH_Data) +
443 sizeof (struct GNUNET_MessageHeader))
448 type = ntohs (msg->header.type);
449 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
450 GNUNET_MESH_DEBUG_M2S (type));
451 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
452 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
455 ch = GMT_get_channel (t, ntohl (msg->chid));
458 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
460 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
465 GMT_change_state (t, MESH_TUNNEL3_READY);
466 GMCH_handle_data (ch, msg, fwd);
470 handle_data_ack (struct MeshTunnel3 *t,
471 const struct GNUNET_MESH_DataACK *msg,
474 struct MeshChannel *ch;
478 size = ntohs (msg->header.size);
479 if (size != sizeof (struct GNUNET_MESH_DataACK))
486 ch = GMT_get_channel (t, ntohl (msg->chid));
489 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
491 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
496 GMCH_handle_data_ack (ch, msg, fwd);
500 handle_ch_create (struct MeshTunnel3 *t,
501 const struct GNUNET_MESH_ChannelCreate *msg,
504 struct MeshChannel *ch;
508 size = ntohs (msg->header.size);
509 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
516 ch = GMT_get_channel (t, ntohl (msg->chid));
519 /* Probably a retransmission, safe to ignore */
520 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
524 ch = GMCH_handle_create (t, msg, fwd);
526 GMT_add_channel (t, ch);
530 handle_ch_ack (struct MeshTunnel3 *t,
531 const struct GNUNET_MESH_ChannelManage *msg,
534 struct MeshChannel *ch;
538 size = ntohs (msg->header.size);
539 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
546 ch = GMT_get_channel (t, ntohl (msg->chid));
549 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
551 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
556 GMCH_handle_ack (ch, msg, fwd);
560 handle_ch_destroy (struct MeshTunnel3 *t,
561 const struct GNUNET_MESH_ChannelManage *msg,
564 struct MeshChannel *ch;
568 size = ntohs (msg->header.size);
569 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
576 ch = GMT_get_channel (t, ntohl (msg->chid));
579 /* Probably a retransmission, safe to ignore */
583 GMCH_handle_destroy (ch, msg, fwd);
588 * Demultiplex by message type and call appropriate handler for a message
589 * towards a channel of a local tunnel.
591 * @param t Tunnel this message came on.
592 * @param msgh Message header.
593 * @param fwd Is this message fwd?
596 handle_decrypted (struct MeshTunnel3 *t,
597 const struct GNUNET_MessageHeader *msgh,
602 type = ntohs (msgh->type);
603 LOG (GNUNET_ERROR_TYPE_DEBUG,
604 "Got a %s message!\n",
605 GNUNET_MESH_DEBUG_M2S (type));
609 case GNUNET_MESSAGE_TYPE_MESH_DATA:
610 /* Don't send hop ACK, wait for client to ACK */
611 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
614 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
615 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
618 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
620 (struct GNUNET_MESH_ChannelCreate *) msgh,
624 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
626 (struct GNUNET_MESH_ChannelManage *) msgh,
630 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
631 handle_ch_destroy (t,
632 (struct GNUNET_MESH_ChannelManage *) msgh,
638 LOG (GNUNET_ERROR_TYPE_DEBUG,
639 "end-to-end message not known (%u)\n",
644 /******************************************************************************/
645 /******************************** API ***********************************/
646 /******************************************************************************/
650 GMT_send_kx (struct MeshTunnel3 *t)
658 * Decrypt and demultiplex by message type. Call appropriate handler
661 * @param t Tunnel this message came on.
662 * @param msg Encrypted message.
663 * @param fwd Is this message fwd?
666 GMT_handle_encrypted (struct MeshTunnel3 *t,
667 const struct GNUNET_MESH_Encrypted *msg,
670 size_t size = ntohs (msg->header.size);
671 size_t payload_size = size - sizeof (struct GNUNET_MESH_Encrypted);
672 size_t decrypted_size;
673 char cbuf [payload_size];
674 struct GNUNET_MessageHeader *msgh;
677 decrypted_size = t_decrypt (t, cbuf, &msg[1], payload_size, msg->iv, fwd);
679 while (off < decrypted_size)
681 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
682 handle_decrypted (t, msgh, fwd);
683 off += ntohs (msgh->size);
689 * Cache a message to be sent once tunnel is online.
691 * @param t Tunnel to hold the message.
692 * @param ch Channel the message is about.
693 * @param msg Message itself (copy will be made).
694 * @param fwd Is this fwd?
697 GMT_queue_data (struct MeshTunnel3 *t,
698 struct MeshChannel *ch,
699 struct GNUNET_MessageHeader *msg,
702 struct MeshTunnelQueue *tq;
703 uint16_t size = ntohs (msg->size);
705 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
708 memcpy (&tq[1], msg, size);
709 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
711 if (MESH_TUNNEL3_READY == t->state)
712 GMT_send_queued_data (t, fwd);
717 * Send all cached messages that we can, tunnel is online.
719 * @param t Tunnel that holds the messages.
720 * @param fwd Is this fwd?
723 GMT_send_queued_data (struct MeshTunnel3 *t, int fwd)
725 struct MeshTunnelQueue *tq;
726 struct MeshTunnelQueue *next;
729 LOG (GNUNET_ERROR_TYPE_DEBUG,
730 "GMT_send_queued_data on tunnel %s\n",
732 room = GMT_get_buffer (t, fwd);
733 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
734 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
736 LOG (GNUNET_ERROR_TYPE_DEBUG, " data on channel %s\n", GMCH_2s (tq->ch));
739 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
740 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
745 LOG (GNUNET_ERROR_TYPE_DEBUG,
746 "GMT_send_queued_data end\n",
752 * Initialize the tunnel subsystem.
754 * @param c Configuration handle.
755 * @param key ECC private key, to derive all other keys and do crypto.
758 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
759 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
761 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
763 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
766 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
767 "MESH", "DEFAULT_TTL", "USING DEFAULT");
771 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REKEY_PERIOD",
774 rekey_period = GNUNET_TIME_UNIT_DAYS;
777 my_private_key = key;
778 kx_msg.header.size = htons (sizeof (kx_msg));
779 kx_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX);
780 kx_msg.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MESH_KX);
781 kx_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
782 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
783 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
784 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
785 sizeof (struct GNUNET_PeerIdentity));
786 kx_msg.origin_identity = my_full_id;
787 rekey_task = GNUNET_SCHEDULER_add_now (&rekey, NULL);
789 tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
794 * Shut down the tunnel subsystem.
799 if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
801 GNUNET_SCHEDULER_cancel (rekey_task);
802 rekey_task = GNUNET_SCHEDULER_NO_TASK;
804 GNUNET_CONTAINER_multipeermap_destroy (tunnels);
811 * @param destination Peer this tunnel is towards.
814 GMT_new (struct MeshPeer *destination)
816 struct MeshTunnel3 *t;
818 t = GNUNET_new (struct MeshTunnel3);
820 t->peer = destination;
822 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
823 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
826 // tunnel_destroy (t);
830 // char salt[] = "salt";
831 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
832 // salt, sizeof (salt),
833 // &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
834 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
835 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
837 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
838 // salt, sizeof (salt),
839 // &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
840 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
841 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
844 GNUNET_CONTAINER_multipeermap_put (tunnels, GMP_get_id (destination), t,
845 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
851 * Change the tunnel state.
853 * @param t Tunnel whose state to change.
854 * @param state New state.
857 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnel3State state)
861 LOG (GNUNET_ERROR_TYPE_DEBUG,
862 "Tunnel %s state was %s\n",
864 GMT_state2s (t->state));
865 LOG (GNUNET_ERROR_TYPE_DEBUG,
866 "Tunnel %s state is now %s\n",
868 GMT_state2s (state));
870 if (MESH_TUNNEL3_READY == state && 3 <= GMT_count_connections (t))
872 GMP_stop_search (t->peer);
878 * Add a connection to a tunnel.
881 * @param c Connection.
884 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
886 struct MeshTConnection *aux;
888 for (aux = t->connection_head; aux != NULL; aux = aux->next)
892 aux = GNUNET_new (struct MeshTConnection);
894 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
899 * Remove a connection from a tunnel.
902 * @param c Connection.
905 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
907 struct MeshTConnection *aux;
909 for (aux = t->connection_head; aux != NULL; aux = aux->next)
912 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
920 * Add a channel to a tunnel.
926 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
928 struct MeshTChannel *aux;
930 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
932 for (aux = t->channel_head; aux != NULL; aux = aux->next)
934 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
939 aux = GNUNET_new (struct MeshTChannel);
941 LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
942 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
947 * Remove a channel from a tunnel.
953 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
955 struct MeshTChannel *aux;
957 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
958 for (aux = t->channel_head; aux != NULL; aux = aux->next)
962 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GMCH_2s (ch));
963 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
972 * Search for a channel by global ID.
974 * @param t Tunnel containing the channel.
975 * @param chid Public channel number.
977 * @return channel handler, NULL if doesn't exist
980 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
982 struct MeshTChannel *iter;
987 for (iter = t->channel_head; NULL != iter; iter = iter->next)
989 if (GMCH_get_id (iter->ch) == chid)
993 return NULL == iter ? NULL : iter->ch;
998 * Tunnel is empty: destroy it.
1000 * Notifies all connections about the destruction.
1002 * @param t Tunnel to destroy.
1005 GMT_destroy_empty (struct MeshTunnel3 *t)
1007 struct MeshTConnection *iter;
1009 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1011 GMC_send_destroy (iter->c);
1014 t->destroy = GNUNET_YES;
1019 * Destroy tunnel if empty (no more channels).
1021 * @param t Tunnel to destroy if empty.
1024 GMT_destroy_if_empty (struct MeshTunnel3 *t)
1026 if (1 < GMT_count_channels (t))
1029 GMT_destroy_empty (t);
1034 * Destroy the tunnel.
1036 * This function does not generate any warning traffic to clients or peers.
1039 * Cancel messages belonging to this tunnel queued to neighbors.
1040 * Free any allocated resources linked to the tunnel.
1042 * @param t The tunnel to destroy.
1045 GMT_destroy (struct MeshTunnel3 *t)
1047 struct MeshTConnection *iter;
1048 struct MeshTConnection *next;
1053 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
1055 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
1056 // GNUNET_break (0);
1058 for (iter = t->connection_head; NULL != iter; iter = next)
1061 GMC_destroy (iter->c);
1064 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
1065 GMP_set_tunnel (t->peer, NULL);
1067 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
1068 GNUNET_SCHEDULER_cancel (t->rekey_task);
1075 * @brief Use the given path for the tunnel.
1076 * Update the next and prev hops (and RCs).
1077 * (Re)start the path refresh in case the tunnel is locally owned.
1079 * @param t Tunnel to update.
1080 * @param p Path to use.
1082 * @return Connection created.
1084 struct MeshConnection *
1085 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
1087 struct MeshConnection *c;
1088 struct GNUNET_HashCode cid;
1089 unsigned int own_pos;
1091 if (NULL == t || NULL == p)
1097 for (own_pos = 0; own_pos < p->length; own_pos++)
1099 if (p->peers[own_pos] == myid)
1102 if (own_pos > p->length - 1)
1108 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
1109 c = GMC_new (&cid, t, p, own_pos);
1110 GMT_add_connection (t, c);
1116 * Count established (ready) connections of a tunnel.
1118 * @param t Tunnel on which to count.
1120 * @return Number of connections.
1123 GMT_count_connections (struct MeshTunnel3 *t)
1125 struct MeshTConnection *iter;
1128 for (count = 0, iter = t->connection_head;
1130 iter = iter->next, count++);
1136 * Count channels of a tunnel.
1138 * @param t Tunnel on which to count.
1140 * @return Number of channels.
1143 GMT_count_channels (struct MeshTunnel3 *t)
1145 struct MeshTChannel *iter;
1148 for (count = 0, iter = t->channel_head;
1150 iter = iter->next, count++) /* skip */;
1157 * Get the state of a tunnel.
1161 * @return Tunnel's state.
1163 enum MeshTunnel3State
1164 GMT_get_state (struct MeshTunnel3 *t)
1167 return (enum MeshTunnel3State) -1;
1172 * Get the total buffer space for a tunnel.
1174 * If terminal, use the biggest channel buffer (or 64) if no channel exists.
1175 * If not terminal, use the sum of all connection buffers.
1178 * @param fwd Is this for FWD traffic?
1180 * @return Buffer space offered by all entities (c/ch) in the tunnel.
1183 GMT_get_buffer (struct MeshTunnel3 *t, int fwd)
1185 struct MeshTConnection *iter;
1186 unsigned int buffer;
1188 iter = t->connection_head;
1191 /* If terminal, return biggest channel buffer */
1192 if (NULL == iter || GMC_is_terminal (iter->c, fwd))
1194 struct MeshTChannel *iter_ch;
1195 unsigned int ch_buf;
1197 if (NULL == t->channel_head)
1199 /* Probably getting buffer for a channel create/handshake. */
1203 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = iter_ch->next)
1205 ch_buf = GMCH_get_buffer (iter_ch->ch, fwd);
1206 if (ch_buf > buffer)
1212 /* If not terminal, return sum of connection buffers */
1213 while (NULL != iter)
1215 if (GMC_get_state (iter->c) != MESH_CONNECTION_READY)
1221 buffer += GMC_get_buffer (iter->c, fwd);
1230 * Get the tunnel's destination.
1234 * @return ID of the destination peer.
1236 const struct GNUNET_PeerIdentity *
1237 GMT_get_destination (struct MeshTunnel3 *t)
1239 return GMP_get_id (t->peer);
1244 * Get the tunnel's next free global channel ID.
1248 * @return GID of a channel free to use.
1251 GMT_get_next_chid (struct MeshTunnel3 *t)
1253 MESH_ChannelNumber chid;
1255 while (NULL != GMT_get_channel (t, t->next_chid))
1257 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
1258 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1260 chid = t->next_chid;
1261 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1268 * Send ACK on one or more channels due to buffer in connections.
1270 * @param t Channel which has some free buffer space.
1271 * @param fwd Is this for FWD traffic? (ACK goes to root)
1274 GMT_unchoke_channels (struct MeshTunnel3 *t, int fwd)
1276 struct MeshTChannel *iter;
1277 unsigned int buffer;
1278 unsigned int channels = GMT_count_channels (t);
1279 unsigned int choked_n;
1280 struct MeshChannel *choked[channels];
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_unchoke_channels on %s\n", GMT_2s (t));
1283 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
1284 if (NULL != t->channel_head)
1285 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
1293 /* Get buffer space */
1294 buffer = GMT_get_buffer (t, fwd);
1300 /* Count and remember choked channels */
1302 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1304 if (GNUNET_NO == GMCH_get_allowed (iter->ch, fwd))
1306 choked[choked_n++] = iter->ch;
1310 /* Unchoke random channels */
1311 while (0 < buffer && 0 < choked_n)
1313 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1315 GMCH_allow_client (choked[r], fwd);
1318 choked[r] = choked[choked_n];
1324 * Send ACK on one or more connections due to buffer space to the client.
1326 * Iterates all connections of the tunnel and sends ACKs appropriately.
1329 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
1332 GMT_send_acks (struct MeshTunnel3 *t, int fwd)
1334 struct MeshTConnection *iter;
1337 uint32_t allow_per_connection;
1339 unsigned int buffer;
1341 LOG (GNUNET_ERROR_TYPE_DEBUG,
1342 "Tunnel send %s ACKs on %s\n",
1343 fwd ? "FWD" : "BCK", GMT_2s (t));
1350 if (NULL == t->channel_head ||
1351 GNUNET_NO == GMCH_is_origin (t->channel_head->ch, !fwd))
1357 buffer = GMT_get_buffer (t, fwd);
1359 /* Count connections, how many messages are already allowed */
1360 cs = GMT_count_connections (t);
1361 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
1363 allowed += GMC_get_allowed (iter->c, fwd);
1366 /* Make sure there is no overflow */
1367 if (allowed > buffer)
1373 /* Authorize connections to send more data */
1374 to_allow = buffer; /* - allowed; */
1376 for (iter = t->connection_head; NULL != iter && to_allow > 0; iter = iter->next)
1378 allow_per_connection = to_allow/cs;
1379 to_allow -= allow_per_connection;
1381 if (GMC_get_allowed (iter->c, fwd) > 64 / 3)
1385 GMC_allow (iter->c, buffer, fwd);
1388 GNUNET_break (to_allow == 0);
1393 * Sends an already built message on a tunnel, encrypting it and
1394 * choosing the best connection.
1396 * @param message Message to send. Function modifies it.
1397 * @param t Tunnel on which this message is transmitted.
1398 * @param ch Channel on which this message is transmitted.
1399 * @param fwd Is this a fwd message?
1402 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1403 struct MeshTunnel3 *t,
1404 struct MeshChannel *ch,
1407 struct MeshConnection *c;
1408 struct GNUNET_MESH_Encrypted *msg;
1409 size_t size = ntohs (message->size);
1410 size_t encrypted_size;
1411 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size + 64];
1415 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMT_2s (t));
1417 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1418 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1419 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
1420 msg->iv = GNUNET_htonll (iv);
1421 encrypted_size = t_encrypt (t, &msg[1], message, size, iv, fwd);
1422 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + encrypted_size);
1423 c = tunnel_get_connection (t, fwd);
1426 GNUNET_break (GNUNET_YES == t->destroy);
1429 type = ntohs (message->type);
1432 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1433 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1434 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1435 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1436 msg->cid = *GMC_get_id (c);
1437 msg->ttl = htonl (default_ttl);
1440 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1441 GNUNET_MESH_DEBUG_M2S (type));
1445 GMC_send_prebuilt_message (&msg->header, c, fwd);
1449 * Is the tunnel directed towards the local peer?
1453 * @return GNUNET_YES if it is loopback.
1456 GMT_is_loopback (const struct MeshTunnel3 *t)
1458 return (myid == GMP_get_short_id(t->peer));
1463 * Is the tunnel using this path already?
1468 * @return GNUNET_YES a connection uses this path.
1471 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p)
1473 struct MeshTConnection *iter;
1475 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1476 if (GMC_get_path (iter->c) == p)
1484 * Get a cost of a path for a tunnel considering existing connections.
1487 * @param path Candidate path.
1489 * @return Cost of the path (path length + number of overlapping nodes)
1492 GMT_get_path_cost (const struct MeshTunnel3 *t,
1493 const struct MeshPeerPath *path)
1495 struct MeshTConnection *iter;
1496 unsigned int overlap;
1504 GNUNET_assert (NULL != t);
1506 for (i = 0; i < path->length; i++)
1508 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1510 for (j = 0; j < GMC_get_path (iter->c)->length; j++)
1512 if (path->peers[i] == GMC_get_path (iter->c)->peers[j])
1520 return (path->length + overlap) * (path->score * -1);
1525 * Get the static string for the peer this tunnel is directed.
1529 * @return Static string the destination peer's ID.
1532 GMT_2s (const struct MeshTunnel3 *t)
1534 return GMP_2s (t->peer);