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 * Get the channel's buffer. ONLY FOR NON-LOOPBACK CHANNELS!!
296 * @param tch Tunnel's channel handle.
298 * @return Amount of messages the channel can still buffer towards the client.
301 get_channel_buffer (const struct MeshTChannel *tch)
305 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
306 fwd = GMCH_is_origin (tch->ch, GNUNET_YES);
308 return GMCH_get_buffer (tch->ch, fwd);
313 * Get the channel's allowance status.
315 * @param tch Tunnel's channel handle.
317 * @return #GNUNET_YES if we allowed the client to send data to us.
320 get_channel_allowed (const struct MeshTChannel *tch)
324 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
325 fwd = GMCH_is_origin (tch->ch, GNUNET_YES);
327 return GMCH_get_allowed (tch->ch, fwd);
332 * Get the connection's buffer.
334 * @param tc Tunnel's connection handle.
336 * @return Amount of messages the connection can still buffer.
339 get_connection_buffer (const struct MeshTConnection *tc)
343 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
344 fwd = GMC_is_origin (tc->c, GNUNET_YES);
346 return GMC_get_buffer (tc->c, fwd);
351 * Get the connection's allowance.
353 * @param tc Tunnel's connection handle.
355 * @return Amount of messages we have allowed the next peer to send us.
358 get_connection_allowed (const struct MeshTConnection *tc)
362 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
363 fwd = GMC_is_origin (tc->c, GNUNET_YES);
365 return GMC_get_allowed (tc->c, fwd);
370 * Check that a ephemeral key message s well formed and correctly signed.
372 * @param t Tunnel on which the message came.
373 * @param msg The ephemeral key message.
375 * @return GNUNET_OK if message is fine, GNUNET_SYSERR otherwise.
378 check_ephemeral (struct MeshTunnel3 *t,
379 const struct GNUNET_MESH_KX_Ephemeral *msg)
381 /* Check message size */
382 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_KX_Ephemeral))
383 return GNUNET_SYSERR;
385 /* Check signature size */
386 if (ntohl (msg->purpose.size) != ephemeral_purpose_size ())
387 return GNUNET_SYSERR;
390 if (0 != memcmp (&msg->origin_identity,
391 GMP_get_id (t->peer),
392 sizeof (struct GNUNET_PeerIdentity)))
393 return GNUNET_SYSERR;
395 /* Check signature */
397 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_MESH_KX,
400 &msg->origin_identity.public_key))
401 return GNUNET_SYSERR;
408 * Encrypt data with the tunnel key.
410 * @param t Tunnel whose key to use.
411 * @param dst Destination for the encrypted data.
412 * @param src Source of the plaintext. Can overlap with @c dst.
413 * @param size Size of the plaintext.
414 * @param iv Initialization Vector to use.
417 t_encrypt (struct MeshTunnel3 *t,
418 void *dst, const void *src,
419 size_t size, uint32_t iv)
421 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
423 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (uint32_t), NULL);
424 return GNUNET_CRYPTO_symmetric_encrypt (src, size, &t->e_key, &siv, dst);
429 * Decrypt data with the tunnel key.
431 * @param t Tunnel whose key to use.
432 * @param dst Destination for the plaintext.
433 * @param src Source of the encrypted data. Can overlap with @c dst.
434 * @param size Size of the encrypted data.
435 * @param iv Initialization Vector to use.
438 t_decrypt (struct MeshTunnel3 *t,
439 void *dst, const void *src,
440 size_t size, uint32_t iv)
442 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
444 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (uint32_t), NULL);
445 return GNUNET_CRYPTO_symmetric_decrypt (src, size, &t->d_key, &siv, dst);
450 * Create key material by doing ECDH on the local and remote ephemeral keys.
452 * @param key_material Where to store the key material.
453 * @param ephemeral_key Peer's public ephemeral key.
456 derive_key_material (struct GNUNET_HashCode *key_material,
457 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key)
460 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
469 * Create a symmetic key from the identities of both ends and the key material
472 * @param key Destination for the generated key.
473 * @param sender ID of the peer that will encrypt with @c key.
474 * @param receiver ID of the peer that will decrypt with @c key.
475 * @param key_material Hash created with ECDH with the ephemeral keys.
478 derive_symmertic (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
479 const struct GNUNET_PeerIdentity *sender,
480 const struct GNUNET_PeerIdentity *receiver,
481 const struct GNUNET_HashCode *key_material)
483 const char salt[] = "MESH kx salt";
485 GNUNET_CRYPTO_kdf (key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
487 key_material, sizeof (struct GNUNET_HashCode),
488 sender, sizeof (struct GNUNET_PeerIdentity),
489 receiver, sizeof (struct GNUNET_PeerIdentity),
494 * Pick a connection on which send the next data message.
496 * @param t Tunnel on which to send the message.
498 * @return The connection on which to send the next message.
500 static struct MeshConnection *
501 tunnel_get_connection (struct MeshTunnel3 *t)
503 struct MeshTConnection *iter;
504 struct MeshConnection *best;
506 unsigned int lowest_q;
508 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMP_2s (t->peer));
511 for (iter = t->connection_head; NULL != iter; iter = iter->next)
513 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
514 GNUNET_h2s (GMC_get_id (iter->c)), GMC_get_state (iter->c));
515 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
517 qn = GMC_get_qn (iter->c, GMC_is_origin (iter->c, GNUNET_YES));
518 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
531 * Send all cached messages that we can, tunnel is online.
533 * @param t Tunnel that holds the messages. Cannot be loopback.
536 send_queued_data (struct MeshTunnel3 *t)
538 struct MeshTunnelQueue *tq;
539 struct MeshTunnelQueue *next;
542 LOG (GNUNET_ERROR_TYPE_DEBUG,
543 "GMT_send_queued_data on tunnel %s\n",
546 if (GMT_is_loopback (t))
552 room = GMT_get_connections_buffer (t);
553 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
554 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
556 LOG (GNUNET_ERROR_TYPE_DEBUG, " data on channel %s\n", GMCH_2s (tq->ch));
559 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
560 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
561 tq->ch, GMCH_is_origin (tq->ch, GNUNET_YES));
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
566 "GMT_send_queued_data end\n",
574 * Cache a message to be sent once tunnel is online.
576 * @param t Tunnel to hold the message.
577 * @param ch Channel the message is about.
578 * @param msg Message itself (copy will be made).
581 queue_data (struct MeshTunnel3 *t,
582 struct MeshChannel *ch,
583 const struct GNUNET_MessageHeader *msg)
585 struct MeshTunnelQueue *tq;
586 uint16_t size = ntohs (msg->size);
588 if (MESH_TUNNEL3_READY == t->state)
594 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
597 memcpy (&tq[1], msg, size);
598 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
604 * Sends key exchange message on a tunnel, choosing the best connection.
605 * Should not be called on loopback tunnels.
607 * @param t Tunnel on which this message is transmitted.
608 * @param message Message to send. Function modifies it.
611 send_kx (struct MeshTunnel3 *t,
612 const struct GNUNET_MessageHeader *message)
614 struct MeshConnection *c;
615 struct GNUNET_MESH_KX *msg;
616 size_t size = ntohs (message->size);
617 char cbuf[sizeof (struct GNUNET_MESH_KX) + size];
621 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT KX on Tunnel %s\n", GMT_2s (t));
623 /* Avoid loopback. */
624 if (GMT_is_loopback (t))
626 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
631 /* Must have a connection. */
632 if (NULL == t->connection_head)
638 msg = (struct GNUNET_MESH_KX *) cbuf;
639 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX);
640 msg->header.size = htons (sizeof (struct GNUNET_MESH_KX) + size);
641 c = tunnel_get_connection (t);
644 GNUNET_break (GNUNET_YES == t->destroy);
647 type = ntohs (message->type);
650 case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
651 case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
652 case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
653 msg->cid = *GMC_get_id (c);
654 msg->reserved = htonl (0);
655 memcpy (&msg[1], message, size);
658 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
659 GNUNET_MESH_DEBUG_M2S (type));
663 fwd = GMC_is_origin (t->connection_head->c, GNUNET_YES);
664 GMC_send_prebuilt_message (&msg->header, c, fwd);
669 * Send the ephemeral key on a tunnel.
671 * @param t Tunnel on which to send the key.
674 send_ephemeral (struct MeshTunnel3 *t)
676 kx_msg.sender_status = htonl (t->state);
678 send_kx (t, &kx_msg.header);
682 * Send a ping message on a tunnel.
684 * @param t Tunnel on which to send the ping.
687 send_ping (struct MeshTunnel3 *t)
689 struct GNUNET_MESH_KX_Ping msg;
691 msg.header.size = htons (sizeof (msg));
692 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_PING);
693 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
694 msg.target = *GMP_get_id (t->peer);
695 msg.nonce = t->kx_ctx->challenge;
696 t_encrypt (t, &msg.target, &msg.target, ping_encryption_size(), msg.iv);
698 send_kx (t, &msg.header);
703 * Send a pong message on a tunnel.
705 * @param t Tunnel on which to send the pong.
706 * @param challenge Value sent in the ping that we have to send back.
709 send_pong (struct MeshTunnel3 *t, uint32_t challenge)
711 struct GNUNET_MESH_KX_Pong msg;
713 msg.header.size = htons (sizeof (msg));
714 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_PONG);
715 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
716 msg.nonce = challenge;
717 t_encrypt (t, &msg.nonce, &msg.nonce, sizeof (msg.nonce), msg.iv);
719 send_kx (t, &msg.header);
724 * Initiate a rekey with the remote peer.
726 * @param cls Closure (tunnel).
727 * @param tc TaskContext.
730 rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
732 struct MeshTunnel3 *t = cls;
734 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
736 if (NULL != tc && 0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
739 t->kx_ctx = GNUNET_new (struct MeshTunnelKXCtx);
740 t->kx_ctx->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
742 t->kx_ctx->d_key_old = t->d_key;
744 if (MESH_TUNNEL3_READY == t->state)
747 t->state = MESH_TUNNEL3_REKEY;
749 else if (MESH_TUNNEL3_WAITING == t->state)
751 t->state = MESH_TUNNEL3_KEY_SENT;
755 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unexpected state %u\n", t->state);
758 t->rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_WAIT, &rekey_tunnel, t);
763 * Out ephemeral key has changed, create new session key on all tunnels.
765 * @param cls Closure (size of the hashmap).
766 * @param key Current public key.
767 * @param value Value in the hash map (tunnel).
769 * @return #GNUNET_YES, so we should continue to iterate,
772 rekey_iterator (void *cls,
773 const struct GNUNET_PeerIdentity *key,
776 struct MeshTunnel3 *t = value;
777 struct GNUNET_TIME_Relative delay;
781 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
784 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t) n * 100);
785 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, r);
786 t->rekey_task = GNUNET_SCHEDULER_add_delayed (delay, &rekey_tunnel, t);
793 * Create a new ephemeral key and key message, schedule next rekeying.
795 * @param cls Closure (unused).
796 * @param tc TaskContext.
799 rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
801 struct GNUNET_TIME_Absolute time;
804 rekey_task = GNUNET_SCHEDULER_NO_TASK;
806 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
809 GNUNET_free_non_null (my_ephemeral_key);
810 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
812 time = GNUNET_TIME_absolute_get ();
813 kx_msg.creation_time = GNUNET_TIME_absolute_hton (time);
814 time = GNUNET_TIME_absolute_add (time, rekey_period);
815 time = GNUNET_TIME_absolute_add (time, GNUNET_TIME_UNIT_MINUTES);
816 kx_msg.expiration_time = GNUNET_TIME_absolute_hton (time);
817 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key, &kx_msg.ephemeral_key);
819 GNUNET_assert (GNUNET_OK ==
820 GNUNET_CRYPTO_eddsa_sign (my_private_key,
824 n = (long) GNUNET_CONTAINER_multipeermap_size (tunnels);
825 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &rekey_iterator, (void *) n);
827 rekey_task = GNUNET_SCHEDULER_add_delayed (rekey_period, &rekey, NULL);
832 * Demultiplex data per channel and call appropriate channel handler.
834 * @param t Tunnel on which the data came.
835 * @param msg Data message.
836 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
837 * #GNUNET_YES if message is FWD on the respective channel (loopback)
838 * #GNUNET_NO if message is BCK on the respective channel (loopback)
839 * #GNUNET_SYSERR if message on a one-ended channel (remote)
842 handle_data (struct MeshTunnel3 *t,
843 const struct GNUNET_MESH_Data *msg,
846 struct MeshChannel *ch;
851 size = ntohs (msg->header.size);
853 sizeof (struct GNUNET_MESH_Data) +
854 sizeof (struct GNUNET_MessageHeader))
859 type = ntohs (msg->header.type);
860 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
861 GNUNET_MESH_DEBUG_M2S (type));
862 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
863 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
866 ch = GMT_get_channel (t, ntohl (msg->chid));
869 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
871 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
876 GMT_change_state (t, MESH_TUNNEL3_READY);
877 GMCH_handle_data (ch, msg, fwd);
882 * Demultiplex data ACKs per channel and update appropriate channel buffer info.
884 * @param t Tunnel on which the DATA ACK came.
885 * @param msg DATA ACK message.
886 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
887 * #GNUNET_YES if message is FWD on the respective channel (loopback)
888 * #GNUNET_NO if message is BCK on the respective channel (loopback)
889 * #GNUNET_SYSERR if message on a one-ended channel (remote)
892 handle_data_ack (struct MeshTunnel3 *t,
893 const struct GNUNET_MESH_DataACK *msg,
896 struct MeshChannel *ch;
900 size = ntohs (msg->header.size);
901 if (size != sizeof (struct GNUNET_MESH_DataACK))
908 ch = GMT_get_channel (t, ntohl (msg->chid));
911 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
913 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
918 GMCH_handle_data_ack (ch, msg, fwd);
923 * Handle channel create.
925 * @param t Tunnel on which the data came.
926 * @param msg Data message.
929 handle_ch_create (struct MeshTunnel3 *t,
930 const struct GNUNET_MESH_ChannelCreate *msg)
932 struct MeshChannel *ch;
936 size = ntohs (msg->header.size);
937 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
944 ch = GMT_get_channel (t, ntohl (msg->chid));
945 if (NULL != ch && ! GMT_is_loopback (t))
947 /* Probably a retransmission, safe to ignore */
948 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
952 ch = GMCH_handle_create (t, msg);
954 GMT_add_channel (t, ch);
959 * Handle a CHANNEL ACK (SYNACK/ACK).
961 * @param t Tunnel on which the CHANNEL ACK came.
962 * @param msg CHANNEL ACK message.
963 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
964 * #GNUNET_YES if message is FWD on the respective channel (loopback)
965 * #GNUNET_NO if message is BCK on the respective channel (loopback)
966 * #GNUNET_SYSERR if message on a one-ended channel (remote)
969 handle_ch_ack (struct MeshTunnel3 *t,
970 const struct GNUNET_MESH_ChannelManage *msg,
973 struct MeshChannel *ch;
977 size = ntohs (msg->header.size);
978 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
985 ch = GMT_get_channel (t, ntohl (msg->chid));
988 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
990 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
995 GMCH_handle_ack (ch, msg, fwd);
1001 * Handle a channel destruction message.
1003 * @param t Tunnel on which the message came.
1004 * @param msg Channel destroy message.
1005 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1006 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1007 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1008 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1011 handle_ch_destroy (struct MeshTunnel3 *t,
1012 const struct GNUNET_MESH_ChannelManage *msg,
1015 struct MeshChannel *ch;
1019 size = ntohs (msg->header.size);
1020 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
1027 ch = GMT_get_channel (t, ntohl (msg->chid));
1030 /* Probably a retransmission, safe to ignore */
1034 GMCH_handle_destroy (ch, msg, fwd);
1039 * The peer's ephemeral key has changed: update the symmetrical keys.
1041 * @param t Tunnel this message came on.
1042 * @param msg Key eXchange message.
1045 handle_ephemeral (struct MeshTunnel3 *t,
1046 const struct GNUNET_MESH_KX_Ephemeral *msg)
1048 struct GNUNET_HashCode km;
1049 LOG (GNUNET_ERROR_TYPE_DEBUG, " ephemeral key message\n");
1051 if (GNUNET_OK != check_ephemeral (t, msg))
1053 GNUNET_break_op (0);
1056 derive_key_material (&km, &msg->ephemeral_key);
1057 derive_symmertic (&t->e_key, &my_full_id, GMP_get_id (t->peer), &km);
1058 derive_symmertic (&t->d_key, GMP_get_id (t->peer), &my_full_id, &km);
1059 if (MESH_TUNNEL3_KEY_SENT == t->state)
1062 t->state = MESH_TUNNEL3_PING_SENT;
1068 * Peer wants to check our symmetrical keys by sending an encrypted challenge.
1069 * Answer with by retransmitting the challenge with the "opposite" key.
1071 * @param t Tunnel this message came on.
1072 * @param msg Key eXchange Ping message.
1075 handle_ping (struct MeshTunnel3 *t,
1076 const struct GNUNET_MESH_KX_Ping *msg)
1078 struct GNUNET_MESH_KX_Ping res;
1080 LOG (GNUNET_ERROR_TYPE_DEBUG, " ping message\n");
1081 t_decrypt (t, &res.target, &msg->target, ping_encryption_size(), msg->iv);
1082 if (0 != memcmp (&my_full_id, &msg->target, sizeof (my_full_id)))
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %s\n", GNUNET_i2s (&my_full_id));
1086 LOG (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&msg->target));
1090 send_pong (t, res.iv);
1095 * Peer has answer to our challenge.
1096 * If answer is successful, consider the key exchange finished and clean
1097 * up all related state.
1099 * @param t Tunnel this message came on.
1100 * @param msg Key eXchange Pong message.
1103 handle_pong (struct MeshTunnel3 *t,
1104 const struct GNUNET_MESH_KX_Pong *msg)
1108 LOG (GNUNET_ERROR_TYPE_DEBUG, "PONG received\n");
1109 if (GNUNET_SCHEDULER_NO_TASK == t->rekey_task)
1111 GNUNET_break_op (0);
1114 t_decrypt (t, &challenge, &msg->nonce, sizeof (uint32_t), msg->iv);
1116 if (challenge != t->kx_ctx->challenge)
1118 LOG (GNUNET_ERROR_TYPE_DEBUG,
1119 "Wrong PONG challenge: %u. Expected: %u.\n",
1120 challenge, t->kx_ctx->challenge);
1121 GNUNET_break_op (0);
1124 GNUNET_SCHEDULER_cancel (t->rekey_task);
1125 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
1126 GNUNET_free (t->kx_ctx);
1128 t->state = MESH_TUNNEL3_READY;
1129 send_queued_data (t);
1134 * Demultiplex by message type and call appropriate handler for a message
1135 * towards a channel of a local tunnel.
1137 * @param t Tunnel this message came on.
1138 * @param msgh Message header.
1139 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1140 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1141 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1142 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1145 handle_decrypted (struct MeshTunnel3 *t,
1146 const struct GNUNET_MessageHeader *msgh,
1151 type = ntohs (msgh->type);
1152 LOG (GNUNET_ERROR_TYPE_DEBUG,
1153 "Got a %s message!\n",
1154 GNUNET_MESH_DEBUG_M2S (type));
1158 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1159 /* Don't send hop ACK, wait for client to ACK */
1160 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
1163 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
1164 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1167 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1168 handle_ch_create (t,
1169 (struct GNUNET_MESH_ChannelCreate *) msgh);
1172 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1174 (struct GNUNET_MESH_ChannelManage *) msgh,
1178 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1179 handle_ch_destroy (t,
1180 (struct GNUNET_MESH_ChannelManage *) msgh,
1185 GNUNET_break_op (0);
1186 LOG (GNUNET_ERROR_TYPE_DEBUG,
1187 "end-to-end message not known (%u)\n",
1188 ntohs (msgh->type));
1192 /******************************************************************************/
1193 /******************************** API ***********************************/
1194 /******************************************************************************/
1197 * Decrypt and demultiplex by message type. Call appropriate handler
1198 * for every message.
1200 * @param t Tunnel this message came on.
1201 * @param msg Encrypted message.
1204 GMT_handle_encrypted (struct MeshTunnel3 *t,
1205 const struct GNUNET_MESH_Encrypted *msg)
1207 size_t size = ntohs (msg->header.size);
1208 size_t payload_size = size - sizeof (struct GNUNET_MESH_Encrypted);
1209 size_t decrypted_size;
1210 char cbuf [payload_size];
1211 struct GNUNET_MessageHeader *msgh;
1214 decrypted_size = t_decrypt (t, cbuf, &msg[1], payload_size, msg->iv);
1216 while (off < decrypted_size)
1218 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1219 handle_decrypted (t, msgh, GNUNET_SYSERR);
1220 off += ntohs (msgh->size);
1226 * Demultiplex an encapsulated KX message by message type.
1228 * @param t Tunnel on which the message came.
1229 * @param message Payload of KX message.
1232 GMT_handle_kx (struct MeshTunnel3 *t,
1233 const struct GNUNET_MessageHeader *message)
1237 type = ntohs (message->type);
1238 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message received\n", type);
1241 case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
1242 handle_ephemeral (t, (struct GNUNET_MESH_KX_Ephemeral *) message);
1245 case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
1246 handle_ping (t, (struct GNUNET_MESH_KX_Ping *) message);
1249 case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
1250 handle_pong (t, (struct GNUNET_MESH_KX_Pong *) message);
1254 GNUNET_break_op (0);
1255 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message not known (%u)\n", type);
1261 * Initialize the tunnel subsystem.
1263 * @param c Configuration handle.
1264 * @param key ECC private key, to derive all other keys and do crypto.
1267 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
1268 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
1270 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1272 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
1275 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1276 "MESH", "DEFAULT_TTL", "USING DEFAULT");
1280 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REKEY_PERIOD",
1283 rekey_period = GNUNET_TIME_UNIT_DAYS;
1286 my_private_key = key;
1287 kx_msg.header.size = htons (sizeof (kx_msg));
1288 kx_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL);
1289 kx_msg.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MESH_KX);
1290 kx_msg.purpose.size = htonl (ephemeral_purpose_size ());
1291 kx_msg.origin_identity = my_full_id;
1292 rekey_task = GNUNET_SCHEDULER_add_now (&rekey, NULL);
1294 tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
1299 * Shut down the tunnel subsystem.
1304 if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
1306 GNUNET_SCHEDULER_cancel (rekey_task);
1307 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1309 GNUNET_CONTAINER_multipeermap_destroy (tunnels);
1316 * @param destination Peer this tunnel is towards.
1318 struct MeshTunnel3 *
1319 GMT_new (struct MeshPeer *destination)
1321 struct MeshTunnel3 *t;
1323 t = GNUNET_new (struct MeshTunnel3);
1325 t->peer = destination;
1328 GNUNET_CONTAINER_multipeermap_put (tunnels, GMP_get_id (destination), t,
1329 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1340 * Change the tunnel state.
1342 * @param t Tunnel whose state to change.
1343 * @param state New state.
1346 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnel3State state)
1350 LOG (GNUNET_ERROR_TYPE_DEBUG,
1351 "Tunnel %s state was %s\n",
1353 GMT_state2s (t->state));
1354 LOG (GNUNET_ERROR_TYPE_DEBUG,
1355 "Tunnel %s state is now %s\n",
1357 GMT_state2s (state));
1358 if (myid != GMP_get_short_id (t->peer) &&
1359 MESH_TUNNEL3_WAITING == t->state && MESH_TUNNEL3_READY == state)
1361 LOG (GNUNET_ERROR_TYPE_DEBUG, " triggered rekey\n");
1362 rekey_tunnel (t, NULL);
1363 LOG (GNUNET_ERROR_TYPE_DEBUG,
1364 "Tunnel %s state is now %s\n",
1366 GMT_state2s (t->state));
1372 if (MESH_TUNNEL3_READY == state && 3 <= GMT_count_connections (t))
1374 GMP_stop_search (t->peer);
1380 * Add a connection to a tunnel.
1383 * @param c Connection.
1386 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
1388 struct MeshTConnection *aux;
1390 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1394 aux = GNUNET_new (struct MeshTConnection);
1396 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
1401 * Remove a connection from a tunnel.
1404 * @param c Connection.
1407 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
1409 struct MeshTConnection *aux;
1411 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1414 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
1422 * Add a channel to a tunnel.
1425 * @param ch Channel.
1428 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
1430 struct MeshTChannel *aux;
1432 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
1434 for (aux = t->channel_head; aux != NULL; aux = aux->next)
1436 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
1441 aux = GNUNET_new (struct MeshTChannel);
1443 LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
1444 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
1449 * Remove a channel from a tunnel.
1452 * @param ch Channel.
1455 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
1457 struct MeshTChannel *aux;
1459 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
1460 for (aux = t->channel_head; aux != NULL; aux = aux->next)
1464 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GMCH_2s (ch));
1465 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
1474 * Search for a channel by global ID.
1476 * @param t Tunnel containing the channel.
1477 * @param chid Public channel number.
1479 * @return channel handler, NULL if doesn't exist
1481 struct MeshChannel *
1482 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
1484 struct MeshTChannel *iter;
1489 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1491 if (GMCH_get_id (iter->ch) == chid)
1495 return NULL == iter ? NULL : iter->ch;
1500 * Tunnel is empty: destroy it.
1502 * Notifies all connections about the destruction.
1504 * @param t Tunnel to destroy.
1507 GMT_destroy_empty (struct MeshTunnel3 *t)
1509 struct MeshTConnection *iter;
1511 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1513 GMC_send_destroy (iter->c);
1516 t->destroy = GNUNET_YES;
1521 * Destroy tunnel if empty (no more channels).
1523 * @param t Tunnel to destroy if empty.
1526 GMT_destroy_if_empty (struct MeshTunnel3 *t)
1528 if (1 < GMT_count_channels (t))
1531 GMT_destroy_empty (t);
1536 * Destroy the tunnel.
1538 * This function does not generate any warning traffic to clients or peers.
1541 * Cancel messages belonging to this tunnel queued to neighbors.
1542 * Free any allocated resources linked to the tunnel.
1544 * @param t The tunnel to destroy.
1547 GMT_destroy (struct MeshTunnel3 *t)
1549 struct MeshTConnection *iter;
1550 struct MeshTConnection *next;
1555 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
1557 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
1558 // GNUNET_break (0);
1560 for (iter = t->connection_head; NULL != iter; iter = next)
1563 GMC_destroy (iter->c);
1566 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
1567 GMP_set_tunnel (t->peer, NULL);
1569 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
1570 GNUNET_SCHEDULER_cancel (t->rekey_task);
1577 * @brief Use the given path for the tunnel.
1578 * Update the next and prev hops (and RCs).
1579 * (Re)start the path refresh in case the tunnel is locally owned.
1581 * @param t Tunnel to update.
1582 * @param p Path to use.
1584 * @return Connection created.
1586 struct MeshConnection *
1587 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
1589 struct MeshConnection *c;
1590 struct GNUNET_HashCode cid;
1591 unsigned int own_pos;
1593 if (NULL == t || NULL == p)
1599 for (own_pos = 0; own_pos < p->length; own_pos++)
1601 if (p->peers[own_pos] == myid)
1604 if (own_pos > p->length - 1)
1610 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
1611 c = GMC_new (&cid, t, p, own_pos);
1612 GMT_add_connection (t, c);
1618 * Count established (ready) connections of a tunnel.
1620 * @param t Tunnel on which to count.
1622 * @return Number of connections.
1625 GMT_count_connections (struct MeshTunnel3 *t)
1627 struct MeshTConnection *iter;
1630 for (count = 0, iter = t->connection_head;
1632 iter = iter->next, count++);
1638 * Count channels of a tunnel.
1640 * @param t Tunnel on which to count.
1642 * @return Number of channels.
1645 GMT_count_channels (struct MeshTunnel3 *t)
1647 struct MeshTChannel *iter;
1650 for (count = 0, iter = t->channel_head;
1652 iter = iter->next, count++) /* skip */;
1659 * Get the state of a tunnel.
1663 * @return Tunnel's state.
1665 enum MeshTunnel3State
1666 GMT_get_state (struct MeshTunnel3 *t)
1671 return (enum MeshTunnel3State) -1;
1678 * Get the maximum buffer space for a tunnel towards a local client.
1682 * @return Biggest buffer space offered by any channel in the tunnel.
1685 GMT_get_channels_buffer (struct MeshTunnel3 *t)
1687 struct MeshTChannel *iter;
1688 unsigned int buffer;
1689 unsigned int ch_buf;
1691 if (NULL == t->channel_head)
1693 /* Probably getting buffer for a channel create/handshake. */
1698 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1700 ch_buf = get_channel_buffer (iter);
1701 if (ch_buf > buffer)
1709 * Get the total buffer space for a tunnel for P2P traffic.
1713 * @return Buffer space offered by all connections in the tunnel.
1716 GMT_get_connections_buffer (struct MeshTunnel3 *t)
1718 struct MeshTConnection *iter;
1719 unsigned int buffer;
1721 iter = t->connection_head;
1723 while (NULL != iter)
1725 if (GMC_get_state (iter->c) != MESH_CONNECTION_READY)
1731 buffer += get_connection_buffer (iter);
1740 * Get the tunnel's destination.
1744 * @return ID of the destination peer.
1746 const struct GNUNET_PeerIdentity *
1747 GMT_get_destination (struct MeshTunnel3 *t)
1749 return GMP_get_id (t->peer);
1754 * Get the tunnel's next free global channel ID.
1758 * @return GID of a channel free to use.
1761 GMT_get_next_chid (struct MeshTunnel3 *t)
1763 MESH_ChannelNumber chid;
1765 while (NULL != GMT_get_channel (t, t->next_chid))
1767 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
1768 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1770 chid = t->next_chid;
1771 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1778 * Send ACK on one or more channels due to buffer in connections.
1780 * @param t Channel which has some free buffer space.
1783 GMT_unchoke_channels (struct MeshTunnel3 *t)
1785 struct MeshTChannel *iter;
1786 unsigned int buffer;
1787 unsigned int channels = GMT_count_channels (t);
1788 unsigned int choked_n;
1789 struct MeshChannel *choked[channels];
1791 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_unchoke_channels on %s\n", GMT_2s (t));
1792 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
1793 if (NULL != t->channel_head)
1794 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
1802 /* Get buffer space */
1803 buffer = GMT_get_connections_buffer (t);
1809 /* Count and remember choked channels */
1811 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1813 if (GNUNET_NO == get_channel_allowed (iter))
1815 choked[choked_n++] = iter->ch;
1819 /* Unchoke random channels */
1820 while (0 < buffer && 0 < choked_n)
1822 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1824 GMCH_allow_client (choked[r], GMCH_is_origin (choked[r], GNUNET_YES));
1827 choked[r] = choked[choked_n];
1833 * Send ACK on one or more connections due to buffer space to the client.
1835 * Iterates all connections of the tunnel and sends ACKs appropriately.
1840 GMT_send_connection_acks (struct MeshTunnel3 *t)
1842 struct MeshTConnection *iter;
1845 uint32_t allow_per_connection;
1847 unsigned int buffer;
1849 LOG (GNUNET_ERROR_TYPE_DEBUG,
1850 "Tunnel send connection ACKs on %s\n",
1859 buffer = GMT_get_channels_buffer (t);
1861 /* Count connections, how many messages are already allowed */
1862 cs = GMT_count_connections (t);
1863 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
1865 allowed += get_connection_allowed (iter);
1868 /* Make sure there is no overflow */
1869 if (allowed > buffer)
1874 /* Authorize connections to send more data */
1875 to_allow = buffer; /* - allowed; */
1877 for (iter = t->connection_head; NULL != iter && to_allow > 0; iter = iter->next)
1879 allow_per_connection = to_allow/cs;
1880 to_allow -= allow_per_connection;
1882 if (get_connection_allowed (iter) > 64 / 3)
1886 GMC_allow (iter->c, buffer, GMC_is_origin (iter->c, GNUNET_YES));
1889 GNUNET_break (to_allow == 0);
1894 * Sends an already built message on a tunnel, encrypting it and
1895 * choosing the best connection.
1897 * @param message Message to send. Function modifies it.
1898 * @param t Tunnel on which this message is transmitted.
1899 * @param ch Channel on which this message is transmitted.
1900 * @param fwd Is this a fwd message on @c ch?
1903 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1904 struct MeshTunnel3 *t,
1905 struct MeshChannel *ch,
1908 struct MeshConnection *c;
1909 struct GNUNET_MESH_Encrypted *msg;
1910 size_t size = ntohs (message->size);
1911 size_t encrypted_size;
1912 char cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1916 if (MESH_TUNNEL3_READY != t->state)
1918 queue_data (t, ch, message);
1921 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMT_2s (t));
1923 if (GMT_is_loopback (t))
1925 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
1926 handle_decrypted (t, message, fwd);
1930 iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1931 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1932 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
1934 encrypted_size = t_encrypt (t, &msg[1], message, size, iv);
1935 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + encrypted_size);
1936 c = tunnel_get_connection (t);
1939 GNUNET_break (GNUNET_YES == t->destroy);
1942 type = ntohs (message->type);
1945 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1946 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1947 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1948 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1949 msg->cid = *GMC_get_id (c);
1950 msg->ttl = htonl (default_ttl);
1953 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1954 GNUNET_MESH_DEBUG_M2S (type));
1958 fwd = GMC_is_origin (c, GNUNET_YES);
1959 GMC_send_prebuilt_message (&msg->header, c, fwd);
1963 * Is the tunnel directed towards the local peer?
1967 * @return #GNUNET_YES if it is loopback.
1970 GMT_is_loopback (const struct MeshTunnel3 *t)
1972 return (myid == GMP_get_short_id (t->peer));
1977 * Is the tunnel using this path already?
1982 * @return #GNUNET_YES a connection uses this path.
1985 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p)
1987 struct MeshTConnection *iter;
1989 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1990 if (GMC_get_path (iter->c) == p)
1998 * Get a cost of a path for a tunnel considering existing connections.
2001 * @param path Candidate path.
2003 * @return Cost of the path (path length + number of overlapping nodes)
2006 GMT_get_path_cost (const struct MeshTunnel3 *t,
2007 const struct MeshPeerPath *path)
2009 struct MeshTConnection *iter;
2010 unsigned int overlap;
2018 GNUNET_assert (NULL != t);
2020 for (i = 0; i < path->length; i++)
2022 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2024 for (j = 0; j < GMC_get_path (iter->c)->length; j++)
2026 if (path->peers[i] == GMC_get_path (iter->c)->peers[j])
2034 return (path->length + overlap) * (path->score * -1);
2039 * Get the static string for the peer this tunnel is directed.
2043 * @return Static string the destination peer's ID.
2046 GMT_2s (const struct MeshTunnel3 *t)
2051 return GMP_2s (t->peer);