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 * Structure used during a Key eXchange.
60 struct MeshTunnelKXCtx
63 * Decryption ("their") old key, for decrypting traffic sent by the
64 * other end before the key exchange started.
66 struct GNUNET_CRYPTO_SymmetricSessionKey d_key_old;
69 * Challenge to send in a ping and expect in the pong.
75 * Struct containing all information regarding a tunnel to a peer.
80 * Endpoint of the tunnel.
82 struct MeshPeer *peer;
85 * State of the tunnel.
87 enum MeshTunnel3State state;
90 * Key eXchange context.
92 struct MeshTunnelKXCtx *kx_ctx;
95 * Encryption ("our") key.
97 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
100 * Decryption ("their") key.
102 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
105 * Task to start the rekey process.
107 GNUNET_SCHEDULER_TaskIdentifier rekey_task;
110 * Paths that are actively used to reach the destination peer.
112 struct MeshTConnection *connection_head;
113 struct MeshTConnection *connection_tail;
116 * Next connection number.
121 * Channels inside this tunnel.
123 struct MeshTChannel *channel_head;
124 struct MeshTChannel *channel_tail;
127 * Channel ID for the next created channel.
129 MESH_ChannelNumber next_chid;
132 * Destroy flag: if true, destroy on last message.
137 * Queued messages, to transmit once tunnel gets connected.
139 struct MeshTunnelQueue *tq_head;
140 struct MeshTunnelQueue *tq_tail;
145 * Struct used to queue messages in a tunnel.
147 struct MeshTunnelQueue
152 struct MeshTunnelQueue *next;
153 struct MeshTunnelQueue *prev;
158 struct MeshChannel *ch;
163 /* struct GNUNET_MessageHeader *msg; */
167 /******************************************************************************/
168 /******************************* GLOBALS ***********************************/
169 /******************************************************************************/
172 * Global handle to the statistics service.
174 extern struct GNUNET_STATISTICS_Handle *stats;
177 * Local peer own ID (memory efficient handle).
179 extern GNUNET_PEER_Id myid;
182 * Local peer own ID (full value).
184 extern struct GNUNET_PeerIdentity my_full_id;
188 * Set of all tunnels, in order to trigger a new exchange on rekey.
189 * Indexed by peer's ID.
191 static struct GNUNET_CONTAINER_MultiPeerMap *tunnels;
194 * Default TTL for payload packets.
196 static unsigned long long default_ttl;
201 const static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
204 * Own ephemeral private key.
206 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
209 * Cached message used to perform a key exchange.
211 static struct GNUNET_MESH_KX_Ephemeral kx_msg;
214 * Task to generate a new ephemeral key.
216 static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
221 static struct GNUNET_TIME_Relative rekey_period;
223 /******************************************************************************/
224 /******************************** STATIC ***********************************/
225 /******************************************************************************/
228 * Get string description for tunnel state.
230 * @param s Tunnel state.
232 * @return String representation.
235 GMT_state2s (enum MeshTunnel3State s)
237 static char buf[128];
241 case MESH_TUNNEL3_NEW:
242 return "MESH_TUNNEL3_NEW";
243 case MESH_TUNNEL3_SEARCHING:
244 return "MESH_TUNNEL3_SEARCHING";
245 case MESH_TUNNEL3_WAITING:
246 return "MESH_TUNNEL3_WAITING";
247 case MESH_TUNNEL3_KEY_SENT:
248 return "MESH_TUNNEL3_KEY_SENT";
249 case MESH_TUNNEL3_PING_SENT:
250 return "MESH_TUNNEL3_PING_SENT";
251 case MESH_TUNNEL3_READY:
252 return "MESH_TUNNEL3_READY";
253 case MESH_TUNNEL3_RECONNECTING:
254 return "MESH_TUNNEL3_RECONNECTING";
255 case MESH_TUNNEL3_REKEY:
256 return "MESH_TUNNEL3_REKEY";
259 sprintf (buf, "%u (UNKNOWN STATE)", s);
266 * Ephemeral key message purpose size.
268 * @return Size of the part of the ephemeral key message that must be signed.
271 ephemeral_purpose_size (void)
273 return sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
274 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
275 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
276 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
277 sizeof (struct GNUNET_PeerIdentity);
282 * Size of the encrypted part of a ping message.
284 * @return Size of the encrypted part of a ping message.
287 ping_encryption_size (void)
289 return sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t);
294 * Check that a ephemeral key message s well formed and correctly signed.
296 * @param t Tunnel on which the message came.
297 * @param msg The ephemeral key message.
299 * @return GNUNET_OK if message is fine, GNUNET_SYSERR otherwise.
302 check_ephemeral (struct MeshTunnel3 *t,
303 const struct GNUNET_MESH_KX_Ephemeral *msg)
305 /* Check message size */
306 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_KX_Ephemeral))
307 return GNUNET_SYSERR;
309 /* Check signature size */
310 if (ntohl (msg->purpose.size) != ephemeral_purpose_size ())
311 return GNUNET_SYSERR;
314 if (0 != memcmp (&msg->origin_identity,
315 GMP_get_id (t->peer),
316 sizeof (struct GNUNET_PeerIdentity)))
317 return GNUNET_SYSERR;
319 /* Check signature */
321 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_MESH_KX,
324 &msg->origin_identity.public_key))
325 return GNUNET_SYSERR;
332 * Encrypt data with the tunnel key.
334 * @param t Tunnel whose key to use.
335 * @param dst Destination for the encrypted data.
336 * @param src Source of the plaintext. Can overlap with @c dst.
337 * @param size Size of the plaintext.
338 * @param iv Initialization Vector to use.
341 t_encrypt (struct MeshTunnel3 *t,
342 void *dst, const void *src,
343 size_t size, uint32_t iv)
345 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
347 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (uint32_t), NULL);
348 return GNUNET_CRYPTO_symmetric_encrypt (src, size, &t->e_key, &siv, dst);
353 * Decrypt data with the tunnel key.
355 * @param t Tunnel whose key to use.
356 * @param dst Destination for the plaintext.
357 * @param src Source of the encrypted data. Can overlap with @c dst.
358 * @param size Size of the encrypted data.
359 * @param iv Initialization Vector to use.
362 t_decrypt (struct MeshTunnel3 *t,
363 void *dst, const void *src,
364 size_t size, uint32_t iv)
366 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
368 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (uint32_t), NULL);
369 return GNUNET_CRYPTO_symmetric_decrypt (src, size, &t->d_key, &siv, dst);
374 * Create key material by doing ECDH on the local and remote ephemeral keys.
376 * @param key_material Where to store the key material.
377 * @param ephemeral_key Peer's public ephemeral key.
380 derive_key_material (struct GNUNET_HashCode *key_material,
381 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key)
384 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
393 * Create a symmetic key from the identities of both ends and the key material
396 * @param key Destination for the generated key.
397 * @param sender ID of the peer that will encrypt with @c key.
398 * @param receiver ID of the peer that will decrypt with @c key.
399 * @param key_material Hash created with ECDH with the ephemeral keys.
402 derive_symmertic (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
403 const struct GNUNET_PeerIdentity *sender,
404 const struct GNUNET_PeerIdentity *receiver,
405 const struct GNUNET_HashCode *key_material)
407 const char salt[] = "MESH kx salt";
409 GNUNET_CRYPTO_kdf (key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
411 key_material, sizeof (struct GNUNET_HashCode),
412 sender, sizeof (struct GNUNET_PeerIdentity),
413 receiver, sizeof (struct GNUNET_PeerIdentity),
418 * Pick a connection on which send the next data message.
420 * @param t Tunnel on which to send the message.
422 * @return The connection on which to send the next message.
424 static struct MeshConnection *
425 tunnel_get_connection (struct MeshTunnel3 *t)
427 struct MeshTConnection *iter;
428 struct MeshConnection *best;
430 unsigned int lowest_q;
432 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMP_2s (t->peer));
435 for (iter = t->connection_head; NULL != iter; iter = iter->next)
437 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
438 GNUNET_h2s (GMC_get_id (iter->c)), GMC_get_state (iter->c));
439 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
441 qn = GMC_get_qn (iter->c, GMC_is_origin (iter->c, GNUNET_YES));
442 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
455 * Send all cached messages that we can, tunnel is online.
457 * @param t Tunnel that holds the messages.
458 * @param fwd Is this fwd?
461 send_queued_data (struct MeshTunnel3 *t, int fwd)
463 struct MeshTunnelQueue *tq;
464 struct MeshTunnelQueue *next;
467 LOG (GNUNET_ERROR_TYPE_DEBUG,
468 "GMT_send_queued_data on tunnel %s\n",
471 if (NULL == t->channel_head ||
472 GNUNET_NO == GMCH_is_origin (t->channel_head->ch, fwd))
475 room = GMT_get_buffer (t, fwd);
476 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
477 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
479 LOG (GNUNET_ERROR_TYPE_DEBUG, " data on channel %s\n", GMCH_2s (tq->ch));
482 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
483 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
488 LOG (GNUNET_ERROR_TYPE_DEBUG,
489 "GMT_send_queued_data end\n",
497 * Cache a message to be sent once tunnel is online.
499 * @param t Tunnel to hold the message.
500 * @param ch Channel the message is about.
501 * @param msg Message itself (copy will be made).
502 * @param fwd Is this fwd?
505 queue_data (struct MeshTunnel3 *t,
506 struct MeshChannel *ch,
507 const struct GNUNET_MessageHeader *msg,
510 struct MeshTunnelQueue *tq;
511 uint16_t size = ntohs (msg->size);
513 if (MESH_TUNNEL3_READY == t->state)
516 GMT_send_prebuilt_message (msg, t, ch, fwd);
520 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
523 memcpy (&tq[1], msg, size);
524 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
530 * Sends key exchange message on a tunnel, choosing the best connection.
531 * Should not be called on loopback tunnels.
533 * @param t Tunnel on which this message is transmitted.
534 * @param message Message to send. Function modifies it.
537 send_kx (struct MeshTunnel3 *t,
538 const struct GNUNET_MessageHeader *message)
540 struct MeshConnection *c;
541 struct GNUNET_MESH_KX *msg;
542 size_t size = ntohs (message->size);
543 char cbuf[sizeof (struct GNUNET_MESH_KX) + size];
547 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT KX on Tunnel %s\n", GMT_2s (t));
549 /* Avoid loopback. */
550 if (myid == GMP_get_short_id (t->peer))
552 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
557 /* Must have a connection. */
558 if (NULL == t->connection_head)
564 msg = (struct GNUNET_MESH_KX *) cbuf;
565 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX);
566 msg->header.size = htons (sizeof (struct GNUNET_MESH_KX) + size);
567 c = tunnel_get_connection (t);
570 GNUNET_break (GNUNET_YES == t->destroy);
573 type = ntohs (message->type);
576 case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
577 case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
578 case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
579 msg->cid = *GMC_get_id (c);
580 msg->reserved = htonl (0);
581 memcpy (&msg[1], message, size);
584 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
585 GNUNET_MESH_DEBUG_M2S (type));
589 fwd = GMC_is_origin (t->connection_head->c, GNUNET_YES);
590 GMC_send_prebuilt_message (&msg->header, c, fwd);
595 * Send the ephemeral key on a tunnel.
597 * @param t Tunnel on which to send the key.
600 send_ephemeral (struct MeshTunnel3 *t)
602 kx_msg.sender_status = htonl (t->state);
604 send_kx (t, &kx_msg.header);
608 * Send a ping message on a tunnel.
610 * @param t Tunnel on which to send the ping.
613 send_ping (struct MeshTunnel3 *t)
615 struct GNUNET_MESH_KX_Ping msg;
617 msg.header.size = htons (sizeof (msg));
618 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_PING);
619 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
620 msg.target = *GMP_get_id (t->peer);
621 msg.nonce = t->kx_ctx->challenge;
622 t_encrypt (t, &msg.target, &msg.target, ping_encryption_size(), msg.iv);
624 send_kx (t, &msg.header);
629 * Send a pong message on a tunnel.
631 * @param t Tunnel on which to send the pong.
632 * @param challenge Value sent in the ping that we have to send back.
635 send_pong (struct MeshTunnel3 *t, uint32_t challenge)
637 struct GNUNET_MESH_KX_Pong msg;
639 msg.header.size = htons (sizeof (msg));
640 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_PONG);
641 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
642 msg.nonce = challenge;
643 t_encrypt (t, &msg.nonce, &msg.nonce, sizeof (msg.nonce), msg.iv);
645 send_kx (t, &msg.header);
650 * Initiate a rekey with the remote peer.
652 * @param cls Closure (tunnel).
653 * @param tc TaskContext.
656 rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
658 struct MeshTunnel3 *t = cls;
660 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
662 if (NULL != tc && 0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
665 t->kx_ctx = GNUNET_new (struct MeshTunnelKXCtx);
666 t->kx_ctx->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
668 t->kx_ctx->d_key_old = t->d_key;
670 if (MESH_TUNNEL3_READY == t->state)
673 t->state = MESH_TUNNEL3_REKEY;
675 else if (MESH_TUNNEL3_WAITING == t->state)
677 t->state = MESH_TUNNEL3_KEY_SENT;
681 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unexpected state %u\n", t->state);
684 t->rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_WAIT, &rekey_tunnel, t);
689 * Out ephemeral key has changed, create new session key on all tunnels.
691 * @param cls Closure (size of the hashmap).
692 * @param key Current public key.
693 * @param value Value in the hash map (tunnel).
695 * @return #GNUNET_YES, so we should continue to iterate,
698 rekey_iterator (void *cls,
699 const struct GNUNET_PeerIdentity *key,
702 struct MeshTunnel3 *t = value;
703 struct GNUNET_TIME_Relative delay;
707 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
710 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t) n * 100);
711 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, r);
712 t->rekey_task = GNUNET_SCHEDULER_add_delayed (delay, &rekey_tunnel, t);
719 * Create a new ephemeral key and key message, schedule next rekeying.
721 * @param cls Closure (unused).
722 * @param tc TaskContext.
725 rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
727 struct GNUNET_TIME_Absolute time;
730 rekey_task = GNUNET_SCHEDULER_NO_TASK;
732 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
735 GNUNET_free_non_null (my_ephemeral_key);
736 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
738 time = GNUNET_TIME_absolute_get ();
739 kx_msg.creation_time = GNUNET_TIME_absolute_hton (time);
740 time = GNUNET_TIME_absolute_add (time, rekey_period);
741 time = GNUNET_TIME_absolute_add (time, GNUNET_TIME_UNIT_MINUTES);
742 kx_msg.expiration_time = GNUNET_TIME_absolute_hton (time);
743 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key, &kx_msg.ephemeral_key);
745 GNUNET_assert (GNUNET_OK ==
746 GNUNET_CRYPTO_eddsa_sign (my_private_key,
750 n = (long) GNUNET_CONTAINER_multipeermap_size (tunnels);
751 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &rekey_iterator, (void *) n);
753 rekey_task = GNUNET_SCHEDULER_add_delayed (rekey_period, &rekey, NULL);
758 * Demultiplex data per channel and call appropriate channel handler.
760 * @param t Tunnel on which the data came.
761 * @param msg Data message.
762 * @param fwd Is this FWD data? (root -> dest)
765 handle_data (struct MeshTunnel3 *t,
766 const struct GNUNET_MESH_Data *msg,
769 struct MeshChannel *ch;
774 size = ntohs (msg->header.size);
776 sizeof (struct GNUNET_MESH_Data) +
777 sizeof (struct GNUNET_MessageHeader))
782 type = ntohs (msg->header.type);
783 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
784 GNUNET_MESH_DEBUG_M2S (type));
785 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
786 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
789 ch = GMT_get_channel (t, ntohl (msg->chid));
792 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
794 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
799 GMT_change_state (t, MESH_TUNNEL3_READY);
800 GMCH_handle_data (ch, msg, fwd);
804 handle_data_ack (struct MeshTunnel3 *t,
805 const struct GNUNET_MESH_DataACK *msg,
808 struct MeshChannel *ch;
812 size = ntohs (msg->header.size);
813 if (size != sizeof (struct GNUNET_MESH_DataACK))
820 ch = GMT_get_channel (t, ntohl (msg->chid));
823 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
825 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
830 GMCH_handle_data_ack (ch, msg, fwd);
834 handle_ch_create (struct MeshTunnel3 *t,
835 const struct GNUNET_MESH_ChannelCreate *msg,
838 struct MeshChannel *ch;
842 size = ntohs (msg->header.size);
843 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
850 ch = GMT_get_channel (t, ntohl (msg->chid));
853 /* Probably a retransmission, safe to ignore */
854 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
858 ch = GMCH_handle_create (t, msg, fwd);
860 GMT_add_channel (t, ch);
864 handle_ch_ack (struct MeshTunnel3 *t,
865 const struct GNUNET_MESH_ChannelManage *msg,
868 struct MeshChannel *ch;
872 size = ntohs (msg->header.size);
873 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
880 ch = GMT_get_channel (t, ntohl (msg->chid));
883 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
885 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
890 GMCH_handle_ack (ch, msg, fwd);
894 handle_ch_destroy (struct MeshTunnel3 *t,
895 const struct GNUNET_MESH_ChannelManage *msg,
898 struct MeshChannel *ch;
902 size = ntohs (msg->header.size);
903 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
910 ch = GMT_get_channel (t, ntohl (msg->chid));
913 /* Probably a retransmission, safe to ignore */
917 GMCH_handle_destroy (ch, msg, fwd);
922 * The peer's ephemeral key has changed: update the symmetrical keys.
924 * @param t Tunnel this message came on.
925 * @param msg Key eXchange message.
928 handle_ephemeral (struct MeshTunnel3 *t,
929 const struct GNUNET_MESH_KX_Ephemeral *msg)
931 struct GNUNET_HashCode km;
932 LOG (GNUNET_ERROR_TYPE_DEBUG, " ephemeral key message\n");
934 if (GNUNET_OK != check_ephemeral (t, msg))
939 derive_key_material (&km, &msg->ephemeral_key);
940 derive_symmertic (&t->e_key, &my_full_id, GMP_get_id (t->peer), &km);
941 derive_symmertic (&t->d_key, GMP_get_id (t->peer), &my_full_id, &km);
942 if (MESH_TUNNEL3_KEY_SENT == t->state)
945 t->state = MESH_TUNNEL3_PING_SENT;
951 * Peer wants to check our symmetrical keys by sending an encrypted challenge.
952 * Answer with by retransmitting the challenge with the "opposite" key.
954 * @param t Tunnel this message came on.
955 * @param msg Key eXchange Ping message.
958 handle_ping (struct MeshTunnel3 *t,
959 const struct GNUNET_MESH_KX_Ping *msg)
961 struct GNUNET_MESH_KX_Ping res;
963 LOG (GNUNET_ERROR_TYPE_DEBUG, " ping message\n");
964 t_decrypt (t, &res.target, &msg->target, ping_encryption_size(), msg->iv);
965 if (0 != memcmp (&my_full_id, &msg->target, sizeof (my_full_id)))
968 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %s\n", GNUNET_i2s (&my_full_id));
969 LOG (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&msg->target));
973 send_pong (t, res.iv);
978 * Peer has answer to our challenge.
979 * If answer is successful, consider the key exchange finished and clean
980 * up all related state.
982 * @param t Tunnel this message came on.
983 * @param msg Key eXchange Pong message.
986 handle_pong (struct MeshTunnel3 *t,
987 const struct GNUNET_MESH_KX_Pong *msg)
991 LOG (GNUNET_ERROR_TYPE_DEBUG, "PONG received\n");
992 if (GNUNET_SCHEDULER_NO_TASK == t->rekey_task)
997 t_decrypt (t, &challenge, &msg->nonce, sizeof (uint32_t), msg->iv);
999 if (challenge != t->kx_ctx->challenge)
1001 LOG (GNUNET_ERROR_TYPE_DEBUG,
1002 "Wrong PONG challenge: %u. Expected: %u.\n",
1003 challenge, t->kx_ctx->challenge);
1004 GNUNET_break_op (0);
1007 GNUNET_SCHEDULER_cancel (t->rekey_task);
1008 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
1009 GNUNET_free (t->kx_ctx);
1011 t->state = MESH_TUNNEL3_READY;
1012 send_queued_data (t, GNUNET_YES);
1013 send_queued_data (t, GNUNET_NO);
1018 * Demultiplex by message type and call appropriate handler for a message
1019 * towards a channel of a local tunnel.
1021 * @param t Tunnel this message came on.
1022 * @param msgh Message header.
1023 * @param fwd Is this message fwd?
1026 handle_decrypted (struct MeshTunnel3 *t,
1027 const struct GNUNET_MessageHeader *msgh,
1032 type = ntohs (msgh->type);
1033 LOG (GNUNET_ERROR_TYPE_DEBUG,
1034 "Got a %s message!\n",
1035 GNUNET_MESH_DEBUG_M2S (type));
1039 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1040 /* Don't send hop ACK, wait for client to ACK */
1041 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
1044 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
1045 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1048 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1049 handle_ch_create (t,
1050 (struct GNUNET_MESH_ChannelCreate *) msgh,
1054 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1056 (struct GNUNET_MESH_ChannelManage *) msgh,
1060 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1061 handle_ch_destroy (t,
1062 (struct GNUNET_MESH_ChannelManage *) msgh,
1067 GNUNET_break_op (0);
1068 LOG (GNUNET_ERROR_TYPE_DEBUG,
1069 "end-to-end message not known (%u)\n",
1070 ntohs (msgh->type));
1074 /******************************************************************************/
1075 /******************************** API ***********************************/
1076 /******************************************************************************/
1079 * Decrypt and demultiplex by message type. Call appropriate handler
1080 * for every message.
1082 * @param t Tunnel this message came on.
1083 * @param msg Encrypted message.
1084 * @param fwd Is this message fwd?
1087 GMT_handle_encrypted (struct MeshTunnel3 *t,
1088 const struct GNUNET_MESH_Encrypted *msg,
1091 size_t size = ntohs (msg->header.size);
1092 size_t payload_size = size - sizeof (struct GNUNET_MESH_Encrypted);
1093 size_t decrypted_size;
1094 char cbuf [payload_size];
1095 struct GNUNET_MessageHeader *msgh;
1098 decrypted_size = t_decrypt (t, cbuf, &msg[1], payload_size, msg->iv);
1100 while (off < decrypted_size)
1102 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1103 handle_decrypted (t, msgh, fwd);
1104 off += ntohs (msgh->size);
1110 * Demultiplex an encapsulated KX message by message type.
1112 * @param t Tunnel on which the message came.
1113 * @param message Payload of KX message.
1116 GMT_handle_kx (struct MeshTunnel3 *t,
1117 const struct GNUNET_MessageHeader *message)
1121 type = ntohs (message->type);
1122 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message received\n", type);
1125 case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
1126 handle_ephemeral (t, (struct GNUNET_MESH_KX_Ephemeral *) message);
1129 case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
1130 handle_ping (t, (struct GNUNET_MESH_KX_Ping *) message);
1133 case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
1134 handle_pong (t, (struct GNUNET_MESH_KX_Pong *) message);
1138 GNUNET_break_op (0);
1139 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message not known (%u)\n", type);
1145 * Initialize the tunnel subsystem.
1147 * @param c Configuration handle.
1148 * @param key ECC private key, to derive all other keys and do crypto.
1151 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
1152 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
1154 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1156 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
1159 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1160 "MESH", "DEFAULT_TTL", "USING DEFAULT");
1164 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REKEY_PERIOD",
1167 rekey_period = GNUNET_TIME_UNIT_DAYS;
1170 my_private_key = key;
1171 kx_msg.header.size = htons (sizeof (kx_msg));
1172 kx_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL);
1173 kx_msg.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MESH_KX);
1174 kx_msg.purpose.size = htonl (ephemeral_purpose_size ());
1175 kx_msg.origin_identity = my_full_id;
1176 rekey_task = GNUNET_SCHEDULER_add_now (&rekey, NULL);
1178 tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
1183 * Shut down the tunnel subsystem.
1188 if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
1190 GNUNET_SCHEDULER_cancel (rekey_task);
1191 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1193 GNUNET_CONTAINER_multipeermap_destroy (tunnels);
1200 * @param destination Peer this tunnel is towards.
1202 struct MeshTunnel3 *
1203 GMT_new (struct MeshPeer *destination)
1205 struct MeshTunnel3 *t;
1207 t = GNUNET_new (struct MeshTunnel3);
1209 t->peer = destination;
1212 GNUNET_CONTAINER_multipeermap_put (tunnels, GMP_get_id (destination), t,
1213 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1224 * Change the tunnel state.
1226 * @param t Tunnel whose state to change.
1227 * @param state New state.
1230 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnel3State state)
1234 LOG (GNUNET_ERROR_TYPE_DEBUG,
1235 "Tunnel %s state was %s\n",
1237 GMT_state2s (t->state));
1238 LOG (GNUNET_ERROR_TYPE_DEBUG,
1239 "Tunnel %s state is now %s\n",
1241 GMT_state2s (state));
1242 if (myid != GMP_get_short_id (t->peer) &&
1243 MESH_TUNNEL3_WAITING == t->state && MESH_TUNNEL3_READY == state)
1245 LOG (GNUNET_ERROR_TYPE_DEBUG, " triggered rekey\n");
1246 rekey_tunnel (t, NULL);
1247 LOG (GNUNET_ERROR_TYPE_DEBUG,
1248 "Tunnel %s state is now %s\n",
1250 GMT_state2s (t->state));
1256 if (MESH_TUNNEL3_READY == state && 3 <= GMT_count_connections (t))
1258 GMP_stop_search (t->peer);
1264 * Add a connection to a tunnel.
1267 * @param c Connection.
1270 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
1272 struct MeshTConnection *aux;
1274 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1278 aux = GNUNET_new (struct MeshTConnection);
1280 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
1285 * Remove a connection from a tunnel.
1288 * @param c Connection.
1291 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
1293 struct MeshTConnection *aux;
1295 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1298 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
1306 * Add a channel to a tunnel.
1309 * @param ch Channel.
1312 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
1314 struct MeshTChannel *aux;
1316 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
1318 for (aux = t->channel_head; aux != NULL; aux = aux->next)
1320 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
1325 aux = GNUNET_new (struct MeshTChannel);
1327 LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
1328 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
1333 * Remove a channel from a tunnel.
1336 * @param ch Channel.
1339 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
1341 struct MeshTChannel *aux;
1343 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
1344 for (aux = t->channel_head; aux != NULL; aux = aux->next)
1348 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GMCH_2s (ch));
1349 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
1358 * Search for a channel by global ID.
1360 * @param t Tunnel containing the channel.
1361 * @param chid Public channel number.
1363 * @return channel handler, NULL if doesn't exist
1365 struct MeshChannel *
1366 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
1368 struct MeshTChannel *iter;
1373 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1375 if (GMCH_get_id (iter->ch) == chid)
1379 return NULL == iter ? NULL : iter->ch;
1384 * Tunnel is empty: destroy it.
1386 * Notifies all connections about the destruction.
1388 * @param t Tunnel to destroy.
1391 GMT_destroy_empty (struct MeshTunnel3 *t)
1393 struct MeshTConnection *iter;
1395 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1397 GMC_send_destroy (iter->c);
1400 t->destroy = GNUNET_YES;
1405 * Destroy tunnel if empty (no more channels).
1407 * @param t Tunnel to destroy if empty.
1410 GMT_destroy_if_empty (struct MeshTunnel3 *t)
1412 if (1 < GMT_count_channels (t))
1415 GMT_destroy_empty (t);
1420 * Destroy the tunnel.
1422 * This function does not generate any warning traffic to clients or peers.
1425 * Cancel messages belonging to this tunnel queued to neighbors.
1426 * Free any allocated resources linked to the tunnel.
1428 * @param t The tunnel to destroy.
1431 GMT_destroy (struct MeshTunnel3 *t)
1433 struct MeshTConnection *iter;
1434 struct MeshTConnection *next;
1439 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
1441 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
1442 // GNUNET_break (0);
1444 for (iter = t->connection_head; NULL != iter; iter = next)
1447 GMC_destroy (iter->c);
1450 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
1451 GMP_set_tunnel (t->peer, NULL);
1453 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
1454 GNUNET_SCHEDULER_cancel (t->rekey_task);
1461 * @brief Use the given path for the tunnel.
1462 * Update the next and prev hops (and RCs).
1463 * (Re)start the path refresh in case the tunnel is locally owned.
1465 * @param t Tunnel to update.
1466 * @param p Path to use.
1468 * @return Connection created.
1470 struct MeshConnection *
1471 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
1473 struct MeshConnection *c;
1474 struct GNUNET_HashCode cid;
1475 unsigned int own_pos;
1477 if (NULL == t || NULL == p)
1483 for (own_pos = 0; own_pos < p->length; own_pos++)
1485 if (p->peers[own_pos] == myid)
1488 if (own_pos > p->length - 1)
1494 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
1495 c = GMC_new (&cid, t, p, own_pos);
1496 GMT_add_connection (t, c);
1502 * Count established (ready) connections of a tunnel.
1504 * @param t Tunnel on which to count.
1506 * @return Number of connections.
1509 GMT_count_connections (struct MeshTunnel3 *t)
1511 struct MeshTConnection *iter;
1514 for (count = 0, iter = t->connection_head;
1516 iter = iter->next, count++);
1522 * Count channels of a tunnel.
1524 * @param t Tunnel on which to count.
1526 * @return Number of channels.
1529 GMT_count_channels (struct MeshTunnel3 *t)
1531 struct MeshTChannel *iter;
1534 for (count = 0, iter = t->channel_head;
1536 iter = iter->next, count++) /* skip */;
1543 * Get the state of a tunnel.
1547 * @return Tunnel's state.
1549 enum MeshTunnel3State
1550 GMT_get_state (struct MeshTunnel3 *t)
1553 return (enum MeshTunnel3State) -1;
1558 * Get the total buffer space for a tunnel.
1560 * If terminal, use the biggest channel buffer (or 64) if no channel exists.
1561 * If not terminal, use the sum of all connection buffers.
1564 * @param fwd Is this for FWD traffic?
1566 * @return Buffer space offered by all entities (c/ch) in the tunnel.
1569 GMT_get_buffer (struct MeshTunnel3 *t, int fwd)
1571 struct MeshTConnection *iter;
1572 unsigned int buffer;
1574 iter = t->connection_head;
1577 /* If terminal, return biggest channel buffer */
1578 if (NULL == iter || GMC_is_terminal (iter->c, fwd))
1580 struct MeshTChannel *iter_ch;
1581 unsigned int ch_buf;
1583 if (NULL == t->channel_head)
1585 /* Probably getting buffer for a channel create/handshake. */
1589 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = iter_ch->next)
1591 ch_buf = GMCH_get_buffer (iter_ch->ch, fwd);
1592 if (ch_buf > buffer)
1598 /* If not terminal, return sum of connection buffers */
1599 while (NULL != iter)
1601 if (GMC_get_state (iter->c) != MESH_CONNECTION_READY)
1607 buffer += GMC_get_buffer (iter->c, fwd);
1616 * Get the tunnel's destination.
1620 * @return ID of the destination peer.
1622 const struct GNUNET_PeerIdentity *
1623 GMT_get_destination (struct MeshTunnel3 *t)
1625 return GMP_get_id (t->peer);
1630 * Get the tunnel's next free global channel ID.
1634 * @return GID of a channel free to use.
1637 GMT_get_next_chid (struct MeshTunnel3 *t)
1639 MESH_ChannelNumber chid;
1641 while (NULL != GMT_get_channel (t, t->next_chid))
1643 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
1644 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1646 chid = t->next_chid;
1647 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1654 * Send ACK on one or more channels due to buffer in connections.
1656 * @param t Channel which has some free buffer space.
1657 * @param fwd Is this for FWD traffic? (ACK goes to root)
1660 GMT_unchoke_channels (struct MeshTunnel3 *t, int fwd)
1662 struct MeshTChannel *iter;
1663 unsigned int buffer;
1664 unsigned int channels = GMT_count_channels (t);
1665 unsigned int choked_n;
1666 struct MeshChannel *choked[channels];
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_unchoke_channels on %s\n", GMT_2s (t));
1669 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
1670 if (NULL != t->channel_head)
1671 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
1679 /* Get buffer space */
1680 buffer = GMT_get_buffer (t, fwd);
1686 /* Count and remember choked channels */
1688 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1690 if (GNUNET_NO == GMCH_get_allowed (iter->ch, fwd))
1692 choked[choked_n++] = iter->ch;
1696 /* Unchoke random channels */
1697 while (0 < buffer && 0 < choked_n)
1699 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1701 GMCH_allow_client (choked[r], fwd);
1704 choked[r] = choked[choked_n];
1710 * Send ACK on one or more connections due to buffer space to the client.
1712 * Iterates all connections of the tunnel and sends ACKs appropriately.
1715 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
1718 GMT_send_acks (struct MeshTunnel3 *t, int fwd)
1720 struct MeshTConnection *iter;
1723 uint32_t allow_per_connection;
1725 unsigned int buffer;
1727 LOG (GNUNET_ERROR_TYPE_DEBUG,
1728 "Tunnel send %s ACKs on %s\n",
1729 fwd ? "FWD" : "BCK", GMT_2s (t));
1736 if (NULL == t->channel_head ||
1737 GNUNET_NO == GMCH_is_origin (t->channel_head->ch, !fwd))
1743 buffer = GMT_get_buffer (t, fwd);
1745 /* Count connections, how many messages are already allowed */
1746 cs = GMT_count_connections (t);
1747 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
1749 allowed += GMC_get_allowed (iter->c, fwd);
1752 /* Make sure there is no overflow */
1753 if (allowed > buffer)
1759 /* Authorize connections to send more data */
1760 to_allow = buffer; /* - allowed; */
1762 for (iter = t->connection_head; NULL != iter && to_allow > 0; iter = iter->next)
1764 allow_per_connection = to_allow/cs;
1765 to_allow -= allow_per_connection;
1767 if (GMC_get_allowed (iter->c, fwd) > 64 / 3)
1771 GMC_allow (iter->c, buffer, fwd);
1774 GNUNET_break (to_allow == 0);
1779 * Sends an already built message on a tunnel, encrypting it and
1780 * choosing the best connection.
1782 * @param message Message to send. Function modifies it.
1783 * @param t Tunnel on which this message is transmitted.
1784 * @param ch Channel on which this message is transmitted.
1785 * @param fwd Is this a fwd message?
1788 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1789 struct MeshTunnel3 *t,
1790 struct MeshChannel *ch,
1793 struct MeshConnection *c;
1794 struct GNUNET_MESH_Encrypted *msg;
1795 size_t size = ntohs (message->size);
1796 size_t encrypted_size;
1797 char cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1801 if (MESH_TUNNEL3_READY != t->state)
1803 queue_data (t, ch, message, fwd);
1806 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMT_2s (t));
1808 if (myid == GMP_get_short_id (t->peer))
1810 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
1811 handle_decrypted (t, message, fwd);
1815 iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1816 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1817 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
1819 encrypted_size = t_encrypt (t, &msg[1], message, size, iv);
1820 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + encrypted_size);
1821 c = tunnel_get_connection (t);
1824 GNUNET_break (GNUNET_YES == t->destroy);
1827 type = ntohs (message->type);
1830 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1831 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1832 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1833 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1834 msg->cid = *GMC_get_id (c);
1835 msg->ttl = htonl (default_ttl);
1838 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1839 GNUNET_MESH_DEBUG_M2S (type));
1843 GMC_send_prebuilt_message (&msg->header, c, fwd);
1847 * Is the tunnel directed towards the local peer?
1851 * @return #GNUNET_YES if it is loopback.
1854 GMT_is_loopback (const struct MeshTunnel3 *t)
1856 return (myid == GMP_get_short_id(t->peer));
1861 * Is the tunnel using this path already?
1866 * @return #GNUNET_YES a connection uses this path.
1869 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p)
1871 struct MeshTConnection *iter;
1873 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1874 if (GMC_get_path (iter->c) == p)
1882 * Get a cost of a path for a tunnel considering existing connections.
1885 * @param path Candidate path.
1887 * @return Cost of the path (path length + number of overlapping nodes)
1890 GMT_get_path_cost (const struct MeshTunnel3 *t,
1891 const struct MeshPeerPath *path)
1893 struct MeshTConnection *iter;
1894 unsigned int overlap;
1902 GNUNET_assert (NULL != t);
1904 for (i = 0; i < path->length; i++)
1906 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1908 for (j = 0; j < GMC_get_path (iter->c)->length; j++)
1910 if (path->peers[i] == GMC_get_path (iter->c)->peers[j])
1918 return (path->length + overlap) * (path->score * -1);
1923 * Get the static string for the peer this tunnel is directed.
1927 * @return Static string the destination peer's ID.
1930 GMT_2s (const struct MeshTunnel3 *t)
1932 return GMP_2s (t->peer);