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.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, 5)
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_READY:
250 return "MESH_TUNNEL3_READY";
251 case MESH_TUNNEL3_RECONNECTING:
252 return "MESH_TUNNEL3_RECONNECTING";
253 case MESH_TUNNEL3_REKEY:
254 return "MESH_TUNNEL3_REKEY";
257 sprintf (buf, "%u (UNKNOWN STATE)", s);
264 * Ephemeral key message purpose size.
266 * @return Size of the part of the ephemeral key message that must be signed.
269 ephemeral_purpose_size (void)
271 return sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
272 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
273 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
274 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
275 sizeof (struct GNUNET_PeerIdentity);
280 * Size of the encrypted part of a ping message.
282 * @return Size of the encrypted part of a ping message.
285 ping_encryption_size (void)
287 return sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t);
292 * Get the channel's buffer. ONLY FOR NON-LOOPBACK CHANNELS!!
294 * @param tch Tunnel's channel handle.
296 * @return Amount of messages the channel can still buffer towards the client.
299 get_channel_buffer (const struct MeshTChannel *tch)
303 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
304 fwd = GMCH_is_origin (tch->ch, GNUNET_YES);
306 return GMCH_get_buffer (tch->ch, fwd);
311 * Get the channel's allowance status.
313 * @param tch Tunnel's channel handle.
315 * @return #GNUNET_YES if we allowed the client to send data to us.
318 get_channel_allowed (const struct MeshTChannel *tch)
322 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
323 fwd = GMCH_is_origin (tch->ch, GNUNET_YES);
325 return GMCH_get_allowed (tch->ch, fwd);
330 * Get the connection's buffer.
332 * @param tc Tunnel's connection handle.
334 * @return Amount of messages the connection can still buffer.
337 get_connection_buffer (const struct MeshTConnection *tc)
341 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
342 fwd = GMC_is_origin (tc->c, GNUNET_YES);
344 return GMC_get_buffer (tc->c, fwd);
349 * Get the connection's allowance.
351 * @param tc Tunnel's connection handle.
353 * @return Amount of messages we have allowed the next peer to send us.
356 get_connection_allowed (const struct MeshTConnection *tc)
360 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
361 fwd = GMC_is_origin (tc->c, GNUNET_YES);
363 return GMC_get_allowed (tc->c, fwd);
368 * Check that a ephemeral key message s well formed and correctly signed.
370 * @param t Tunnel on which the message came.
371 * @param msg The ephemeral key message.
373 * @return GNUNET_OK if message is fine, GNUNET_SYSERR otherwise.
376 check_ephemeral (struct MeshTunnel3 *t,
377 const struct GNUNET_MESH_KX_Ephemeral *msg)
379 /* Check message size */
380 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_KX_Ephemeral))
381 return GNUNET_SYSERR;
383 /* Check signature size */
384 if (ntohl (msg->purpose.size) != ephemeral_purpose_size ())
385 return GNUNET_SYSERR;
388 if (0 != memcmp (&msg->origin_identity,
389 GMP_get_id (t->peer),
390 sizeof (struct GNUNET_PeerIdentity)))
391 return GNUNET_SYSERR;
393 /* Check signature */
395 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_MESH_KX,
398 &msg->origin_identity.public_key))
399 return GNUNET_SYSERR;
406 * Encrypt data with the tunnel key.
408 * @param t Tunnel whose key to use.
409 * @param dst Destination for the encrypted data.
410 * @param src Source of the plaintext. Can overlap with @c dst.
411 * @param size Size of the plaintext.
412 * @param iv Initialization Vector to use.
415 t_encrypt (struct MeshTunnel3 *t,
416 void *dst, const void *src,
417 size_t size, uint32_t iv)
419 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
421 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (uint32_t), NULL);
422 return GNUNET_CRYPTO_symmetric_encrypt (src, size, &t->e_key, &siv, dst);
427 * Decrypt data with the tunnel key.
429 * @param t Tunnel whose key to use.
430 * @param dst Destination for the plaintext.
431 * @param src Source of the encrypted data. Can overlap with @c dst.
432 * @param size Size of the encrypted data.
433 * @param iv Initialization Vector to use.
436 t_decrypt (struct MeshTunnel3 *t,
437 void *dst, const void *src,
438 size_t size, uint32_t iv)
440 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
442 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->d_key, &iv, sizeof (uint32_t), NULL);
443 return GNUNET_CRYPTO_symmetric_decrypt (src, size, &t->d_key, &siv, dst);
448 * Create key material by doing ECDH on the local and remote ephemeral keys.
450 * @param key_material Where to store the key material.
451 * @param ephemeral_key Peer's public ephemeral key.
454 derive_key_material (struct GNUNET_HashCode *key_material,
455 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key)
458 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
467 * Create a symmetic key from the identities of both ends and the key material
470 * @param key Destination for the generated key.
471 * @param sender ID of the peer that will encrypt with @c key.
472 * @param receiver ID of the peer that will decrypt with @c key.
473 * @param key_material Hash created with ECDH with the ephemeral keys.
476 derive_symmertic (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
477 const struct GNUNET_PeerIdentity *sender,
478 const struct GNUNET_PeerIdentity *receiver,
479 const struct GNUNET_HashCode *key_material)
481 const char salt[] = "MESH kx salt";
483 GNUNET_CRYPTO_kdf (key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
485 key_material, sizeof (struct GNUNET_HashCode),
486 sender, sizeof (struct GNUNET_PeerIdentity),
487 receiver, sizeof (struct GNUNET_PeerIdentity),
492 * Pick a connection on which send the next data message.
494 * @param t Tunnel on which to send the message.
496 * @return The connection on which to send the next message.
498 static struct MeshConnection *
499 tunnel_get_connection (struct MeshTunnel3 *t)
501 struct MeshTConnection *iter;
502 struct MeshConnection *best;
504 unsigned int lowest_q;
506 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMP_2s (t->peer));
509 for (iter = t->connection_head; NULL != iter; iter = iter->next)
511 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
512 GNUNET_h2s (GMC_get_id (iter->c)), GMC_get_state (iter->c));
513 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
515 qn = GMC_get_qn (iter->c, GMC_is_origin (iter->c, GNUNET_YES));
516 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
529 * Send all cached messages that we can, tunnel is online.
531 * @param t Tunnel that holds the messages. Cannot be loopback.
534 send_queued_data (struct MeshTunnel3 *t)
536 struct MeshTunnelQueue *tq;
537 struct MeshTunnelQueue *next;
540 LOG (GNUNET_ERROR_TYPE_DEBUG,
541 "GMT_send_queued_data on tunnel %s\n",
544 if (GMT_is_loopback (t))
550 room = GMT_get_connections_buffer (t);
551 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
552 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
554 LOG (GNUNET_ERROR_TYPE_DEBUG, " data on channel %s\n", GMCH_2s (tq->ch));
557 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
558 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
559 tq->ch, GMCH_is_origin (tq->ch, GNUNET_YES));
563 LOG (GNUNET_ERROR_TYPE_DEBUG,
564 "GMT_send_queued_data end\n",
572 * Cache a message to be sent once tunnel is online.
574 * @param t Tunnel to hold the message.
575 * @param ch Channel the message is about.
576 * @param msg Message itself (copy will be made).
579 queue_data (struct MeshTunnel3 *t,
580 struct MeshChannel *ch,
581 const struct GNUNET_MessageHeader *msg)
583 struct MeshTunnelQueue *tq;
584 uint16_t size = ntohs (msg->size);
586 if (MESH_TUNNEL3_READY == t->state)
592 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
595 memcpy (&tq[1], msg, size);
596 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
602 * Sends key exchange message on a tunnel, choosing the best connection.
603 * Should not be called on loopback tunnels.
605 * @param t Tunnel on which this message is transmitted.
606 * @param message Message to send. Function modifies it.
609 send_kx (struct MeshTunnel3 *t,
610 const struct GNUNET_MessageHeader *message)
612 struct MeshConnection *c;
613 struct GNUNET_MESH_KX *msg;
614 size_t size = ntohs (message->size);
615 char cbuf[sizeof (struct GNUNET_MESH_KX) + size];
619 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT KX on Tunnel %s\n", GMT_2s (t));
621 /* Avoid loopback. */
622 if (GMT_is_loopback (t))
624 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
629 /* Must have a connection. */
630 if (NULL == t->connection_head)
636 msg = (struct GNUNET_MESH_KX *) cbuf;
637 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX);
638 msg->header.size = htons (sizeof (struct GNUNET_MESH_KX) + size);
639 c = tunnel_get_connection (t);
642 GNUNET_break (GNUNET_YES == t->destroy);
645 type = ntohs (message->type);
648 case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
649 case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
650 case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
651 msg->reserved = htonl (0);
652 memcpy (&msg[1], message, size);
655 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
656 GNUNET_MESH_DEBUG_M2S (type));
660 fwd = GMC_is_origin (t->connection_head->c, GNUNET_YES);
661 /* TODO save handle and cancel in case of a unneeded retransmission */
662 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
667 * Send the ephemeral key on a tunnel.
669 * @param t Tunnel on which to send the key.
672 send_ephemeral (struct MeshTunnel3 *t)
674 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __FUNCTION__);
676 kx_msg.sender_status = htonl (t->state);
677 send_kx (t, &kx_msg.header);
681 * Send a ping message on a tunnel.
683 * @param t Tunnel on which to send the ping.
686 send_ping (struct MeshTunnel3 *t)
688 struct GNUNET_MESH_KX_Ping msg;
690 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __FUNCTION__);
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;
697 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
698 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n", GNUNET_i2s (&msg.target));
699 t_encrypt (t, &msg.target, &msg.target, ping_encryption_size(), msg.iv);
700 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
701 LOG (GNUNET_ERROR_TYPE_DEBUG, " e towards %s\n", GNUNET_i2s (&msg.target));
703 send_kx (t, &msg.header);
708 * Send a pong message on a tunnel.
710 * @param t Tunnel on which to send the pong.
711 * @param challenge Value sent in the ping that we have to send back.
714 send_pong (struct MeshTunnel3 *t, uint32_t challenge)
716 struct GNUNET_MESH_KX_Pong msg;
718 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __FUNCTION__);
719 msg.header.size = htons (sizeof (msg));
720 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_PONG);
721 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
722 msg.nonce = challenge;
723 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
724 t_encrypt (t, &msg.nonce, &msg.nonce, sizeof (msg.nonce), msg.iv);
725 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
727 send_kx (t, &msg.header);
732 * Initiate a rekey with the remote peer.
734 * @param cls Closure (tunnel).
735 * @param tc TaskContext.
738 rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
740 struct MeshTunnel3 *t = cls;
742 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
744 LOG (GNUNET_ERROR_TYPE_DEBUG, "Re-key Tunnel\n");
745 if (NULL != tc && 0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
748 if (NULL == t->kx_ctx)
750 LOG (GNUNET_ERROR_TYPE_DEBUG, " new kx ctx\n");
751 t->kx_ctx = GNUNET_new (struct MeshTunnelKXCtx);
752 t->kx_ctx->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
754 t->kx_ctx->d_key_old = t->d_key;
757 if (MESH_TUNNEL3_READY == t->state || MESH_TUNNEL3_REKEY == t->state)
760 t->state = MESH_TUNNEL3_REKEY;
762 else if (MESH_TUNNEL3_WAITING == t->state)
764 t->state = MESH_TUNNEL3_KEY_SENT;
768 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unexpected state %u\n", t->state);
771 LOG (GNUNET_ERROR_TYPE_DEBUG, " next call in %s\n",
772 GNUNET_STRINGS_relative_time_to_string (REKEY_WAIT, GNUNET_YES));
773 t->rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_WAIT, &rekey_tunnel, t);
778 * Out ephemeral key has changed, create new session key on all tunnels.
780 * @param cls Closure (size of the hashmap).
781 * @param key Current public key.
782 * @param value Value in the hash map (tunnel).
784 * @return #GNUNET_YES, so we should continue to iterate,
787 rekey_iterator (void *cls,
788 const struct GNUNET_PeerIdentity *key,
791 struct MeshTunnel3 *t = value;
792 struct GNUNET_TIME_Relative delay;
796 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
799 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t) n * 100);
800 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, r);
801 t->rekey_task = GNUNET_SCHEDULER_add_delayed (delay, &rekey_tunnel, t);
808 * Create a new ephemeral key and key message, schedule next rekeying.
810 * @param cls Closure (unused).
811 * @param tc TaskContext.
814 rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
816 struct GNUNET_TIME_Absolute time;
819 rekey_task = GNUNET_SCHEDULER_NO_TASK;
821 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
824 GNUNET_free_non_null (my_ephemeral_key);
825 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
827 time = GNUNET_TIME_absolute_get ();
828 kx_msg.creation_time = GNUNET_TIME_absolute_hton (time);
829 time = GNUNET_TIME_absolute_add (time, rekey_period);
830 time = GNUNET_TIME_absolute_add (time, GNUNET_TIME_UNIT_MINUTES);
831 kx_msg.expiration_time = GNUNET_TIME_absolute_hton (time);
832 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key, &kx_msg.ephemeral_key);
834 GNUNET_assert (GNUNET_OK ==
835 GNUNET_CRYPTO_eddsa_sign (my_private_key,
839 n = (long) GNUNET_CONTAINER_multipeermap_size (tunnels);
840 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &rekey_iterator, (void *) n);
842 rekey_task = GNUNET_SCHEDULER_add_delayed (rekey_period, &rekey, NULL);
847 * Demultiplex data per channel and call appropriate channel handler.
849 * @param t Tunnel on which the data came.
850 * @param msg Data message.
851 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
852 * #GNUNET_YES if message is FWD on the respective channel (loopback)
853 * #GNUNET_NO if message is BCK on the respective channel (loopback)
854 * #GNUNET_SYSERR if message on a one-ended channel (remote)
857 handle_data (struct MeshTunnel3 *t,
858 const struct GNUNET_MESH_Data *msg,
861 struct MeshChannel *ch;
866 size = ntohs (msg->header.size);
868 sizeof (struct GNUNET_MESH_Data) +
869 sizeof (struct GNUNET_MessageHeader))
874 type = ntohs (msg->header.type);
875 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
876 GNUNET_MESH_DEBUG_M2S (type));
877 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
878 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
881 ch = GMT_get_channel (t, ntohl (msg->chid));
884 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
886 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
891 GMT_change_state (t, MESH_TUNNEL3_READY);
892 GMCH_handle_data (ch, msg, fwd);
897 * Demultiplex data ACKs per channel and update appropriate channel buffer info.
899 * @param t Tunnel on which the DATA ACK came.
900 * @param msg DATA ACK message.
901 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
902 * #GNUNET_YES if message is FWD on the respective channel (loopback)
903 * #GNUNET_NO if message is BCK on the respective channel (loopback)
904 * #GNUNET_SYSERR if message on a one-ended channel (remote)
907 handle_data_ack (struct MeshTunnel3 *t,
908 const struct GNUNET_MESH_DataACK *msg,
911 struct MeshChannel *ch;
915 size = ntohs (msg->header.size);
916 if (size != sizeof (struct GNUNET_MESH_DataACK))
923 ch = GMT_get_channel (t, ntohl (msg->chid));
926 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
928 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
933 GMCH_handle_data_ack (ch, msg, fwd);
938 * Handle channel create.
940 * @param t Tunnel on which the data came.
941 * @param msg Data message.
944 handle_ch_create (struct MeshTunnel3 *t,
945 const struct GNUNET_MESH_ChannelCreate *msg)
947 struct MeshChannel *ch;
951 size = ntohs (msg->header.size);
952 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
959 ch = GMT_get_channel (t, ntohl (msg->chid));
960 if (NULL != ch && ! GMT_is_loopback (t))
962 /* Probably a retransmission, safe to ignore */
963 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
967 ch = GMCH_handle_create (t, msg);
970 GMT_add_channel (t, ch);
976 * Handle channel NACK.
978 * @param t Tunnel on which the data came.
979 * @param msg Data message.
982 handle_ch_nack (struct MeshTunnel3 *t,
983 const struct GNUNET_MESH_ChannelManage *msg)
985 struct MeshChannel *ch;
989 size = ntohs (msg->header.size);
990 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
997 ch = GMT_get_channel (t, ntohl (msg->chid));
998 if (NULL != ch && ! GMT_is_loopback (t))
1000 GNUNET_STATISTICS_update (stats, "# channel NACK on unknown channel",
1002 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1007 GMCH_handle_nack (ch);
1012 * Handle a CHANNEL ACK (SYNACK/ACK).
1014 * @param t Tunnel on which the CHANNEL ACK came.
1015 * @param msg CHANNEL ACK message.
1016 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1017 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1018 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1019 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1022 handle_ch_ack (struct MeshTunnel3 *t,
1023 const struct GNUNET_MESH_ChannelManage *msg,
1026 struct MeshChannel *ch;
1030 size = ntohs (msg->header.size);
1031 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
1038 ch = GMT_get_channel (t, ntohl (msg->chid));
1041 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
1043 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1048 GMCH_handle_ack (ch, msg, fwd);
1054 * Handle a channel destruction message.
1056 * @param t Tunnel on which the message came.
1057 * @param msg Channel destroy message.
1058 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1059 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1060 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1061 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1064 handle_ch_destroy (struct MeshTunnel3 *t,
1065 const struct GNUNET_MESH_ChannelManage *msg,
1068 struct MeshChannel *ch;
1072 size = ntohs (msg->header.size);
1073 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
1080 ch = GMT_get_channel (t, ntohl (msg->chid));
1083 /* Probably a retransmission, safe to ignore */
1087 GMCH_handle_destroy (ch, msg, fwd);
1092 * The peer's ephemeral key has changed: update the symmetrical keys.
1094 * @param t Tunnel this message came on.
1095 * @param msg Key eXchange message.
1098 handle_ephemeral (struct MeshTunnel3 *t,
1099 const struct GNUNET_MESH_KX_Ephemeral *msg)
1101 struct GNUNET_HashCode km;
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, " ephemeral key message\n");
1104 if (GNUNET_OK != check_ephemeral (t, msg))
1106 GNUNET_break_op (0);
1109 derive_key_material (&km, &msg->ephemeral_key);
1110 LOG (GNUNET_ERROR_TYPE_DEBUG, " km is %s\n", GNUNET_h2s (&km));
1111 derive_symmertic (&t->e_key, &my_full_id, GMP_get_id (t->peer), &km);
1112 derive_symmertic (&t->d_key, GMP_get_id (t->peer), &my_full_id, &km);
1113 if (MESH_TUNNEL3_KEY_SENT == t->state)
1115 LOG (GNUNET_ERROR_TYPE_DEBUG, " our key was sent, send ping\n");
1117 t->state = MESH_TUNNEL3_REKEY;
1123 * Peer wants to check our symmetrical keys by sending an encrypted challenge.
1124 * Answer with by retransmitting the challenge with the "opposite" key.
1126 * @param t Tunnel this message came on.
1127 * @param msg Key eXchange Ping message.
1130 handle_ping (struct MeshTunnel3 *t,
1131 const struct GNUNET_MESH_KX_Ping *msg)
1133 struct GNUNET_MESH_KX_Ping res;
1135 if (ntohs (msg->header.size) != sizeof (res))
1137 GNUNET_break_op (0);
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, " ping message\n");
1142 t_decrypt (t, &res.target, &msg->target, ping_encryption_size (), msg->iv);
1143 if (0 != memcmp (&my_full_id, &res.target, sizeof (my_full_id)))
1145 GNUNET_break_op (0);
1146 LOG (GNUNET_ERROR_TYPE_DEBUG, " e got %u\n", msg->nonce);
1147 LOG (GNUNET_ERROR_TYPE_DEBUG, " e towards %s\n", GNUNET_i2s (&msg->target));
1148 LOG (GNUNET_ERROR_TYPE_DEBUG, " got %u\n", res.nonce);
1149 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n", GNUNET_i2s (&res.target));
1153 send_pong (t, res.nonce);
1158 * Peer has answer to our challenge.
1159 * If answer is successful, consider the key exchange finished and clean
1160 * up all related state.
1162 * @param t Tunnel this message came on.
1163 * @param msg Key eXchange Pong message.
1166 handle_pong (struct MeshTunnel3 *t,
1167 const struct GNUNET_MESH_KX_Pong *msg)
1171 LOG (GNUNET_ERROR_TYPE_DEBUG, "PONG received\n");
1172 if (GNUNET_SCHEDULER_NO_TASK == t->rekey_task)
1174 GNUNET_break_op (0);
1177 t_decrypt (t, &challenge, &msg->nonce, sizeof (uint32_t), msg->iv);
1179 if (challenge != t->kx_ctx->challenge)
1181 LOG (GNUNET_ERROR_TYPE_DEBUG,
1182 "Wrong PONG challenge: %u (e: %u). Expected: %u.\n",
1183 challenge, msg->nonce, t->kx_ctx->challenge);
1184 GNUNET_break_op (0);
1187 GNUNET_SCHEDULER_cancel (t->rekey_task);
1188 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
1189 GNUNET_free (t->kx_ctx);
1191 t->state = MESH_TUNNEL3_READY;
1192 send_queued_data (t);
1197 * Demultiplex by message type and call appropriate handler for a message
1198 * towards a channel of a local tunnel.
1200 * @param t Tunnel this message came on.
1201 * @param msgh Message header.
1202 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1203 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1204 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1205 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1208 handle_decrypted (struct MeshTunnel3 *t,
1209 const struct GNUNET_MessageHeader *msgh,
1214 type = ntohs (msgh->type);
1215 LOG (GNUNET_ERROR_TYPE_DEBUG,
1216 "Got a %s message!\n",
1217 GNUNET_MESH_DEBUG_M2S (type));
1221 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1222 /* Don't send hop ACK, wait for client to ACK */
1223 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
1226 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
1227 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1230 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1231 handle_ch_create (t,
1232 (struct GNUNET_MESH_ChannelCreate *) msgh);
1235 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1237 (struct GNUNET_MESH_ChannelManage *) msgh);
1240 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1242 (struct GNUNET_MESH_ChannelManage *) msgh,
1246 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1247 handle_ch_destroy (t,
1248 (struct GNUNET_MESH_ChannelManage *) msgh,
1253 GNUNET_break_op (0);
1254 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 "end-to-end message not known (%u)\n",
1256 ntohs (msgh->type));
1260 /******************************************************************************/
1261 /******************************** API ***********************************/
1262 /******************************************************************************/
1265 * Decrypt and demultiplex by message type. Call appropriate handler
1266 * for every message.
1268 * @param t Tunnel this message came on.
1269 * @param msg Encrypted message.
1272 GMT_handle_encrypted (struct MeshTunnel3 *t,
1273 const struct GNUNET_MESH_Encrypted *msg)
1275 size_t size = ntohs (msg->header.size);
1276 size_t payload_size = size - sizeof (struct GNUNET_MESH_Encrypted);
1277 size_t decrypted_size;
1278 char cbuf [payload_size];
1279 struct GNUNET_MessageHeader *msgh;
1282 decrypted_size = t_decrypt (t, cbuf, &msg[1], payload_size, msg->iv);
1284 while (off < decrypted_size)
1286 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1287 handle_decrypted (t, msgh, GNUNET_SYSERR);
1288 off += ntohs (msgh->size);
1294 * Demultiplex an encapsulated KX message by message type.
1296 * @param t Tunnel on which the message came.
1297 * @param message Payload of KX message.
1300 GMT_handle_kx (struct MeshTunnel3 *t,
1301 const struct GNUNET_MessageHeader *message)
1305 type = ntohs (message->type);
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message received\n", type);
1309 case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
1310 handle_ephemeral (t, (struct GNUNET_MESH_KX_Ephemeral *) message);
1313 case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
1314 handle_ping (t, (struct GNUNET_MESH_KX_Ping *) message);
1317 case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
1318 handle_pong (t, (struct GNUNET_MESH_KX_Pong *) message);
1322 GNUNET_break_op (0);
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message not known (%u)\n", type);
1329 * Initialize the tunnel subsystem.
1331 * @param c Configuration handle.
1332 * @param key ECC private key, to derive all other keys and do crypto.
1335 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
1336 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1340 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
1343 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1344 "MESH", "DEFAULT_TTL", "USING DEFAULT");
1348 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REKEY_PERIOD",
1351 rekey_period = GNUNET_TIME_UNIT_DAYS;
1354 my_private_key = key;
1355 kx_msg.header.size = htons (sizeof (kx_msg));
1356 kx_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL);
1357 kx_msg.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MESH_KX);
1358 kx_msg.purpose.size = htonl (ephemeral_purpose_size ());
1359 kx_msg.origin_identity = my_full_id;
1360 rekey_task = GNUNET_SCHEDULER_add_now (&rekey, NULL);
1362 tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
1367 * Shut down the tunnel subsystem.
1372 if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
1374 GNUNET_SCHEDULER_cancel (rekey_task);
1375 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1377 GNUNET_CONTAINER_multipeermap_destroy (tunnels);
1384 * @param destination Peer this tunnel is towards.
1386 struct MeshTunnel3 *
1387 GMT_new (struct MeshPeer *destination)
1389 struct MeshTunnel3 *t;
1391 t = GNUNET_new (struct MeshTunnel3);
1393 t->peer = destination;
1396 GNUNET_CONTAINER_multipeermap_put (tunnels, GMP_get_id (destination), t,
1397 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1408 * Change the tunnel state.
1410 * @param t Tunnel whose state to change.
1411 * @param state New state.
1414 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnel3State state)
1418 LOG (GNUNET_ERROR_TYPE_DEBUG,
1419 "Tunnel %s state was %s\n",
1421 GMT_state2s (t->state));
1422 LOG (GNUNET_ERROR_TYPE_DEBUG,
1423 "Tunnel %s state is now %s\n",
1425 GMT_state2s (state));
1426 if (myid != GMP_get_short_id (t->peer) &&
1427 MESH_TUNNEL3_WAITING == t->state && MESH_TUNNEL3_READY == state)
1429 LOG (GNUNET_ERROR_TYPE_DEBUG, " triggered rekey\n");
1430 rekey_tunnel (t, NULL);
1431 LOG (GNUNET_ERROR_TYPE_DEBUG,
1432 "Tunnel %s state is now %s\n",
1434 GMT_state2s (t->state));
1440 if (MESH_TUNNEL3_READY == state && 3 <= GMT_count_connections (t))
1442 GMP_stop_search (t->peer);
1448 * Add a connection to a tunnel.
1451 * @param c Connection.
1454 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
1456 struct MeshTConnection *aux;
1458 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1462 aux = GNUNET_new (struct MeshTConnection);
1464 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
1469 * Remove a connection from a tunnel.
1472 * @param c Connection.
1475 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
1477 struct MeshTConnection *aux;
1479 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1482 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
1490 * Add a channel to a tunnel.
1493 * @param ch Channel.
1496 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
1498 struct MeshTChannel *aux;
1500 GNUNET_assert (NULL != ch);
1502 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
1504 for (aux = t->channel_head; aux != NULL; aux = aux->next)
1506 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
1511 aux = GNUNET_new (struct MeshTChannel);
1513 LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
1514 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
1519 * Remove a channel from a tunnel.
1522 * @param ch Channel.
1525 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
1527 struct MeshTChannel *aux;
1529 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
1530 for (aux = t->channel_head; aux != NULL; aux = aux->next)
1534 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GMCH_2s (ch));
1535 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
1544 * Search for a channel by global ID.
1546 * @param t Tunnel containing the channel.
1547 * @param chid Public channel number.
1549 * @return channel handler, NULL if doesn't exist
1551 struct MeshChannel *
1552 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
1554 struct MeshTChannel *iter;
1559 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1561 if (GMCH_get_id (iter->ch) == chid)
1565 return NULL == iter ? NULL : iter->ch;
1570 * Tunnel is empty: destroy it.
1572 * Notifies all connections about the destruction.
1574 * @param t Tunnel to destroy.
1577 GMT_destroy_empty (struct MeshTunnel3 *t)
1579 struct MeshTConnection *iter;
1581 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1583 GMC_send_destroy (iter->c);
1586 t->destroy = GNUNET_YES;
1591 * Destroy tunnel if empty (no more channels).
1593 * @param t Tunnel to destroy if empty.
1596 GMT_destroy_if_empty (struct MeshTunnel3 *t)
1598 if (1 < GMT_count_channels (t))
1601 GMT_destroy_empty (t);
1606 * Destroy the tunnel.
1608 * This function does not generate any warning traffic to clients or peers.
1611 * Cancel messages belonging to this tunnel queued to neighbors.
1612 * Free any allocated resources linked to the tunnel.
1614 * @param t The tunnel to destroy.
1617 GMT_destroy (struct MeshTunnel3 *t)
1619 struct MeshTConnection *iter;
1620 struct MeshTConnection *next;
1625 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
1627 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
1628 // GNUNET_break (0);
1630 for (iter = t->connection_head; NULL != iter; iter = next)
1633 GMC_destroy (iter->c);
1636 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
1637 GMP_set_tunnel (t->peer, NULL);
1639 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
1640 GNUNET_SCHEDULER_cancel (t->rekey_task);
1647 * @brief Use the given path for the tunnel.
1648 * Update the next and prev hops (and RCs).
1649 * (Re)start the path refresh in case the tunnel is locally owned.
1651 * @param t Tunnel to update.
1652 * @param p Path to use.
1654 * @return Connection created.
1656 struct MeshConnection *
1657 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
1659 struct MeshConnection *c;
1660 struct GNUNET_HashCode cid;
1661 unsigned int own_pos;
1663 if (NULL == t || NULL == p)
1669 for (own_pos = 0; own_pos < p->length; own_pos++)
1671 if (p->peers[own_pos] == myid)
1674 if (own_pos > p->length - 1)
1680 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
1681 c = GMC_new (&cid, t, p, own_pos);
1682 GMT_add_connection (t, c);
1688 * Count established (ready) connections of a tunnel.
1690 * @param t Tunnel on which to count.
1692 * @return Number of connections.
1695 GMT_count_connections (struct MeshTunnel3 *t)
1697 struct MeshTConnection *iter;
1700 for (count = 0, iter = t->connection_head;
1702 iter = iter->next, count++);
1708 * Count channels of a tunnel.
1710 * @param t Tunnel on which to count.
1712 * @return Number of channels.
1715 GMT_count_channels (struct MeshTunnel3 *t)
1717 struct MeshTChannel *iter;
1720 for (count = 0, iter = t->channel_head;
1722 iter = iter->next, count++) /* skip */;
1729 * Get the state of a tunnel.
1733 * @return Tunnel's state.
1735 enum MeshTunnel3State
1736 GMT_get_state (struct MeshTunnel3 *t)
1741 return (enum MeshTunnel3State) -1;
1748 * Get the maximum buffer space for a tunnel towards a local client.
1752 * @return Biggest buffer space offered by any channel in the tunnel.
1755 GMT_get_channels_buffer (struct MeshTunnel3 *t)
1757 struct MeshTChannel *iter;
1758 unsigned int buffer;
1759 unsigned int ch_buf;
1761 if (NULL == t->channel_head)
1763 /* Probably getting buffer for a channel create/handshake. */
1768 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1770 ch_buf = get_channel_buffer (iter);
1771 if (ch_buf > buffer)
1779 * Get the total buffer space for a tunnel for P2P traffic.
1783 * @return Buffer space offered by all connections in the tunnel.
1786 GMT_get_connections_buffer (struct MeshTunnel3 *t)
1788 struct MeshTConnection *iter;
1789 unsigned int buffer;
1791 iter = t->connection_head;
1793 while (NULL != iter)
1795 if (GMC_get_state (iter->c) != MESH_CONNECTION_READY)
1801 buffer += get_connection_buffer (iter);
1810 * Get the tunnel's destination.
1814 * @return ID of the destination peer.
1816 const struct GNUNET_PeerIdentity *
1817 GMT_get_destination (struct MeshTunnel3 *t)
1819 return GMP_get_id (t->peer);
1824 * Get the tunnel's next free global channel ID.
1828 * @return GID of a channel free to use.
1831 GMT_get_next_chid (struct MeshTunnel3 *t)
1833 MESH_ChannelNumber chid;
1835 while (NULL != GMT_get_channel (t, t->next_chid))
1837 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
1838 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1840 chid = t->next_chid;
1841 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1848 * Send ACK on one or more channels due to buffer in connections.
1850 * @param t Channel which has some free buffer space.
1853 GMT_unchoke_channels (struct MeshTunnel3 *t)
1855 struct MeshTChannel *iter;
1856 unsigned int buffer;
1857 unsigned int channels = GMT_count_channels (t);
1858 unsigned int choked_n;
1859 struct MeshChannel *choked[channels];
1861 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_unchoke_channels on %s\n", GMT_2s (t));
1862 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
1863 if (NULL != t->channel_head)
1864 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
1866 /* Get buffer space */
1867 buffer = GMT_get_connections_buffer (t);
1873 /* Count and remember choked channels */
1875 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1877 if (GNUNET_NO == get_channel_allowed (iter))
1879 choked[choked_n++] = iter->ch;
1883 /* Unchoke random channels */
1884 while (0 < buffer && 0 < choked_n)
1886 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1888 GMCH_allow_client (choked[r], GMCH_is_origin (choked[r], GNUNET_YES));
1891 choked[r] = choked[choked_n];
1897 * Send ACK on one or more connections due to buffer space to the client.
1899 * Iterates all connections of the tunnel and sends ACKs appropriately.
1904 GMT_send_connection_acks (struct MeshTunnel3 *t)
1906 struct MeshTConnection *iter;
1909 uint32_t allow_per_connection;
1911 unsigned int buffer;
1913 LOG (GNUNET_ERROR_TYPE_DEBUG,
1914 "Tunnel send connection ACKs on %s\n",
1923 buffer = GMT_get_channels_buffer (t);
1925 /* Count connections, how many messages are already allowed */
1926 cs = GMT_count_connections (t);
1927 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
1929 allowed += get_connection_allowed (iter);
1932 /* Make sure there is no overflow */
1933 if (allowed > buffer)
1938 /* Authorize connections to send more data */
1939 to_allow = buffer; /* - allowed; */
1941 for (iter = t->connection_head; NULL != iter && to_allow > 0; iter = iter->next)
1943 allow_per_connection = to_allow/cs;
1944 to_allow -= allow_per_connection;
1946 if (get_connection_allowed (iter) > 64 / 3)
1950 GMC_allow (iter->c, buffer, GMC_is_origin (iter->c, GNUNET_YES));
1953 GNUNET_break (to_allow == 0);
1958 * Sends an already built message on a tunnel, encrypting it and
1959 * choosing the best connection.
1961 * @param message Message to send. Function modifies it.
1962 * @param t Tunnel on which this message is transmitted.
1963 * @param ch Channel on which this message is transmitted.
1964 * @param fwd Is this a fwd message on @c ch?
1967 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1968 struct MeshTunnel3 *t,
1969 struct MeshChannel *ch,
1972 struct MeshConnection *c;
1973 struct GNUNET_MESH_Encrypted *msg;
1974 size_t size = ntohs (message->size);
1975 size_t encrypted_size;
1976 char cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1980 if (MESH_TUNNEL3_READY != t->state)
1982 queue_data (t, ch, message);
1985 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMT_2s (t));
1987 if (GMT_is_loopback (t))
1989 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
1990 handle_decrypted (t, message, fwd);
1994 iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1995 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1996 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
1998 encrypted_size = t_encrypt (t, &msg[1], message, size, iv);
1999 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + encrypted_size);
2000 c = tunnel_get_connection (t);
2003 GNUNET_break (GNUNET_YES == t->destroy);
2006 type = ntohs (message->type);
2009 case GNUNET_MESSAGE_TYPE_MESH_DATA:
2010 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2011 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
2012 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
2013 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
2014 msg->cid = *GMC_get_id (c);
2015 msg->ttl = htonl (default_ttl);
2018 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
2019 GNUNET_MESH_DEBUG_M2S (type));
2023 fwd = GMC_is_origin (c, GNUNET_YES);
2024 /* FIXME allow channels to cancel */
2025 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
2029 * Is the tunnel directed towards the local peer?
2033 * @return #GNUNET_YES if it is loopback.
2036 GMT_is_loopback (const struct MeshTunnel3 *t)
2038 return (myid == GMP_get_short_id (t->peer));
2043 * Is the tunnel this path already?
2048 * @return #GNUNET_YES a connection uses this path.
2051 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p)
2053 struct MeshTConnection *iter;
2055 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2056 if (GMC_get_path (iter->c) == p)
2064 * Get a cost of a path for a tunnel considering existing connections.
2067 * @param path Candidate path.
2069 * @return Cost of the path (path length + number of overlapping nodes)
2072 GMT_get_path_cost (const struct MeshTunnel3 *t,
2073 const struct MeshPeerPath *path)
2075 struct MeshTConnection *iter;
2076 unsigned int overlap;
2084 GNUNET_assert (NULL != t);
2086 for (i = 0; i < path->length; i++)
2088 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2090 for (j = 0; j < GMC_get_path (iter->c)->length; j++)
2092 if (path->peers[i] == GMC_get_path (iter->c)->peers[j])
2100 return (path->length + overlap) * (path->score * -1);
2105 * Get the static string for the peer this tunnel is directed.
2109 * @return Static string the destination peer's ID.
2112 GMT_2s (const struct MeshTunnel3 *t)
2117 return GMP_2s (t->peer);