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 MeshTunnelDelayed *tq_head;
140 struct MeshTunnelDelayed *tq_tail;
145 * Struct used to save messages in a non-ready tunnel to send once connected.
147 struct MeshTunnelDelayed
152 struct MeshTunnelDelayed *next;
153 struct MeshTunnelDelayed *prev;
158 struct MeshChannel *ch;
163 /* struct GNUNET_MessageHeader *msg; */
168 * Handle for messages queued but not yet sent.
170 struct MeshTunnel3Queue
173 * Connection queue handle, to cancel if necessary.
175 struct MeshConnectionQueue *q;
178 * Continuation to call once sent.
183 * Closure for @c cont.
189 /******************************************************************************/
190 /******************************* GLOBALS ***********************************/
191 /******************************************************************************/
194 * Global handle to the statistics service.
196 extern struct GNUNET_STATISTICS_Handle *stats;
199 * Local peer own ID (memory efficient handle).
201 extern GNUNET_PEER_Id myid;
204 * Local peer own ID (full value).
206 extern struct GNUNET_PeerIdentity my_full_id;
210 * Set of all tunnels, in order to trigger a new exchange on rekey.
211 * Indexed by peer's ID.
213 static struct GNUNET_CONTAINER_MultiPeerMap *tunnels;
216 * Default TTL for payload packets.
218 static unsigned long long default_ttl;
223 const static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
226 * Own ephemeral private key.
228 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
231 * Cached message used to perform a key exchange.
233 static struct GNUNET_MESH_KX_Ephemeral kx_msg;
236 * Task to generate a new ephemeral key.
238 static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
243 static struct GNUNET_TIME_Relative rekey_period;
245 /******************************************************************************/
246 /******************************** STATIC ***********************************/
247 /******************************************************************************/
250 * Get string description for tunnel state.
252 * @param s Tunnel state.
254 * @return String representation.
257 GMT_state2s (enum MeshTunnel3State s)
259 static char buf[128];
263 case MESH_TUNNEL3_NEW:
264 return "MESH_TUNNEL3_NEW";
265 case MESH_TUNNEL3_SEARCHING:
266 return "MESH_TUNNEL3_SEARCHING";
267 case MESH_TUNNEL3_WAITING:
268 return "MESH_TUNNEL3_WAITING";
269 case MESH_TUNNEL3_KEY_SENT:
270 return "MESH_TUNNEL3_KEY_SENT";
271 case MESH_TUNNEL3_READY:
272 return "MESH_TUNNEL3_READY";
273 case MESH_TUNNEL3_RECONNECTING:
274 return "MESH_TUNNEL3_RECONNECTING";
275 case MESH_TUNNEL3_REKEY:
276 return "MESH_TUNNEL3_REKEY";
279 sprintf (buf, "%u (UNKNOWN STATE)", s);
286 * Ephemeral key message purpose size.
288 * @return Size of the part of the ephemeral key message that must be signed.
291 ephemeral_purpose_size (void)
293 return sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
294 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
295 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
296 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
297 sizeof (struct GNUNET_PeerIdentity);
302 * Size of the encrypted part of a ping message.
304 * @return Size of the encrypted part of a ping message.
307 ping_encryption_size (void)
309 return sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t);
314 * Get the channel's buffer. ONLY FOR NON-LOOPBACK CHANNELS!!
316 * @param tch Tunnel's channel handle.
318 * @return Amount of messages the channel can still buffer towards the client.
321 get_channel_buffer (const struct MeshTChannel *tch)
325 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
326 fwd = GMCH_is_origin (tch->ch, GNUNET_YES);
328 return GMCH_get_buffer (tch->ch, fwd);
333 * Get the channel's allowance status.
335 * @param tch Tunnel's channel handle.
337 * @return #GNUNET_YES if we allowed the client to send data to us.
340 get_channel_allowed (const struct MeshTChannel *tch)
344 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
345 fwd = GMCH_is_origin (tch->ch, GNUNET_YES);
347 return GMCH_get_allowed (tch->ch, fwd);
352 * Get the connection's buffer.
354 * @param tc Tunnel's connection handle.
356 * @return Amount of messages the connection can still buffer.
359 get_connection_buffer (const struct MeshTConnection *tc)
363 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
364 fwd = GMC_is_origin (tc->c, GNUNET_YES);
366 return GMC_get_buffer (tc->c, fwd);
371 * Get the connection's allowance.
373 * @param tc Tunnel's connection handle.
375 * @return Amount of messages we have allowed the next peer to send us.
378 get_connection_allowed (const struct MeshTConnection *tc)
382 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
383 fwd = GMC_is_origin (tc->c, GNUNET_YES);
385 return GMC_get_allowed (tc->c, fwd);
390 * Check that a ephemeral key message s well formed and correctly signed.
392 * @param t Tunnel on which the message came.
393 * @param msg The ephemeral key message.
395 * @return GNUNET_OK if message is fine, GNUNET_SYSERR otherwise.
398 check_ephemeral (struct MeshTunnel3 *t,
399 const struct GNUNET_MESH_KX_Ephemeral *msg)
401 /* Check message size */
402 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_KX_Ephemeral))
403 return GNUNET_SYSERR;
405 /* Check signature size */
406 if (ntohl (msg->purpose.size) != ephemeral_purpose_size ())
407 return GNUNET_SYSERR;
410 if (0 != memcmp (&msg->origin_identity,
411 GMP_get_id (t->peer),
412 sizeof (struct GNUNET_PeerIdentity)))
413 return GNUNET_SYSERR;
415 /* Check signature */
417 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_MESH_KX,
420 &msg->origin_identity.public_key))
421 return GNUNET_SYSERR;
428 * Encrypt data with the tunnel key.
430 * @param t Tunnel whose key to use.
431 * @param dst Destination for the encrypted data.
432 * @param src Source of the plaintext. Can overlap with @c dst.
433 * @param size Size of the plaintext.
434 * @param iv Initialization Vector to use.
437 t_encrypt (struct MeshTunnel3 *t,
438 void *dst, const void *src,
439 size_t size, uint32_t iv)
441 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
443 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (uint32_t), NULL);
444 return GNUNET_CRYPTO_symmetric_encrypt (src, size, &t->e_key, &siv, dst);
449 * Decrypt data with the tunnel key.
451 * @param t Tunnel whose key to use.
452 * @param dst Destination for the plaintext.
453 * @param src Source of the encrypted data. Can overlap with @c dst.
454 * @param size Size of the encrypted data.
455 * @param iv Initialization Vector to use.
458 t_decrypt (struct MeshTunnel3 *t,
459 void *dst, const void *src,
460 size_t size, uint32_t iv)
462 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
464 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->d_key, &iv, sizeof (uint32_t), NULL);
465 return GNUNET_CRYPTO_symmetric_decrypt (src, size, &t->d_key, &siv, dst);
470 * Create key material by doing ECDH on the local and remote ephemeral keys.
472 * @param key_material Where to store the key material.
473 * @param ephemeral_key Peer's public ephemeral key.
476 derive_key_material (struct GNUNET_HashCode *key_material,
477 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key)
480 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
489 * Create a symmetic key from the identities of both ends and the key material
492 * @param key Destination for the generated key.
493 * @param sender ID of the peer that will encrypt with @c key.
494 * @param receiver ID of the peer that will decrypt with @c key.
495 * @param key_material Hash created with ECDH with the ephemeral keys.
498 derive_symmertic (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
499 const struct GNUNET_PeerIdentity *sender,
500 const struct GNUNET_PeerIdentity *receiver,
501 const struct GNUNET_HashCode *key_material)
503 const char salt[] = "MESH kx salt";
505 GNUNET_CRYPTO_kdf (key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
507 key_material, sizeof (struct GNUNET_HashCode),
508 sender, sizeof (struct GNUNET_PeerIdentity),
509 receiver, sizeof (struct GNUNET_PeerIdentity),
514 * Pick a connection on which send the next data message.
516 * @param t Tunnel on which to send the message.
518 * @return The connection on which to send the next message.
520 static struct MeshConnection *
521 tunnel_get_connection (struct MeshTunnel3 *t)
523 struct MeshTConnection *iter;
524 struct MeshConnection *best;
526 unsigned int lowest_q;
528 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMP_2s (t->peer));
531 for (iter = t->connection_head; NULL != iter; iter = iter->next)
533 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
534 GNUNET_h2s (GMC_get_id (iter->c)), GMC_get_state (iter->c));
535 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
537 qn = GMC_get_qn (iter->c, GMC_is_origin (iter->c, GNUNET_YES));
538 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
551 * Send all cached messages that we can, tunnel is online.
553 * @param t Tunnel that holds the messages. Cannot be loopback.
556 send_queued_data (struct MeshTunnel3 *t)
558 struct MeshTunnelDelayed *tq;
559 struct MeshTunnelDelayed *next;
562 LOG (GNUNET_ERROR_TYPE_DEBUG,
563 "GMT_send_queued_data on tunnel %s\n",
566 if (GMT_is_loopback (t))
572 room = GMT_get_connections_buffer (t);
573 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
574 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
576 LOG (GNUNET_ERROR_TYPE_DEBUG, " data on channel %s\n", GMCH_2s (tq->ch));
579 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
580 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
581 tq->ch, GMCH_is_origin (tq->ch, GNUNET_YES),
586 LOG (GNUNET_ERROR_TYPE_DEBUG,
587 "GMT_send_queued_data end\n",
595 * Cache a message to be sent once tunnel is online.
597 * @param t Tunnel to hold the message.
598 * @param ch Channel the message is about.
599 * @param msg Message itself (copy will be made).
602 queue_data (struct MeshTunnel3 *t,
603 struct MeshChannel *ch,
604 const struct GNUNET_MessageHeader *msg)
606 struct MeshTunnelDelayed *tq;
607 uint16_t size = ntohs (msg->size);
609 if (MESH_TUNNEL3_READY == t->state)
615 tq = GNUNET_malloc (sizeof (struct MeshTunnelDelayed) + size);
618 memcpy (&tq[1], msg, size);
619 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
625 * Sends key exchange message on a tunnel, choosing the best connection.
626 * Should not be called on loopback tunnels.
628 * @param t Tunnel on which this message is transmitted.
629 * @param message Message to send. Function modifies it.
632 send_kx (struct MeshTunnel3 *t,
633 const struct GNUNET_MessageHeader *message)
635 struct MeshConnection *c;
636 struct GNUNET_MESH_KX *msg;
637 size_t size = ntohs (message->size);
638 char cbuf[sizeof (struct GNUNET_MESH_KX) + size];
642 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT KX on Tunnel %s\n", GMT_2s (t));
644 /* Avoid loopback. */
645 if (GMT_is_loopback (t))
647 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
652 /* Must have a connection. */
653 if (NULL == t->connection_head)
659 msg = (struct GNUNET_MESH_KX *) cbuf;
660 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX);
661 msg->header.size = htons (sizeof (struct GNUNET_MESH_KX) + size);
662 c = tunnel_get_connection (t);
665 GNUNET_break (GNUNET_YES == t->destroy);
668 type = ntohs (message->type);
671 case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
672 case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
673 case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
674 msg->reserved = htonl (0);
675 memcpy (&msg[1], message, size);
678 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
679 GNUNET_MESH_DEBUG_M2S (type));
683 fwd = GMC_is_origin (t->connection_head->c, GNUNET_YES);
684 /* TODO save handle and cancel in case of a unneeded retransmission */
685 GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
690 * Send the ephemeral key on a tunnel.
692 * @param t Tunnel on which to send the key.
695 send_ephemeral (struct MeshTunnel3 *t)
697 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __FUNCTION__);
699 kx_msg.sender_status = htonl (t->state);
700 send_kx (t, &kx_msg.header);
704 * Send a ping message on a tunnel.
706 * @param t Tunnel on which to send the ping.
709 send_ping (struct MeshTunnel3 *t)
711 struct GNUNET_MESH_KX_Ping msg;
713 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __FUNCTION__);
714 msg.header.size = htons (sizeof (msg));
715 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_PING);
716 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
717 msg.target = *GMP_get_id (t->peer);
718 msg.nonce = t->kx_ctx->challenge;
720 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
721 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n", GNUNET_i2s (&msg.target));
722 t_encrypt (t, &msg.target, &msg.target, ping_encryption_size(), msg.iv);
723 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
724 LOG (GNUNET_ERROR_TYPE_DEBUG, " e towards %s\n", GNUNET_i2s (&msg.target));
726 send_kx (t, &msg.header);
731 * Send a pong message on a tunnel.
733 * @param t Tunnel on which to send the pong.
734 * @param challenge Value sent in the ping that we have to send back.
737 send_pong (struct MeshTunnel3 *t, uint32_t challenge)
739 struct GNUNET_MESH_KX_Pong msg;
741 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __FUNCTION__);
742 msg.header.size = htons (sizeof (msg));
743 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_PONG);
744 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
745 msg.nonce = challenge;
746 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
747 t_encrypt (t, &msg.nonce, &msg.nonce, sizeof (msg.nonce), msg.iv);
748 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
750 send_kx (t, &msg.header);
755 * Initiate a rekey with the remote peer.
757 * @param cls Closure (tunnel).
758 * @param tc TaskContext.
761 rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
763 struct MeshTunnel3 *t = cls;
765 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
767 LOG (GNUNET_ERROR_TYPE_DEBUG, "Re-key Tunnel\n");
768 if (NULL != tc && 0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
771 if (NULL == t->kx_ctx)
773 LOG (GNUNET_ERROR_TYPE_DEBUG, " new kx ctx\n");
774 t->kx_ctx = GNUNET_new (struct MeshTunnelKXCtx);
775 t->kx_ctx->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
777 t->kx_ctx->d_key_old = t->d_key;
780 if (MESH_TUNNEL3_READY == t->state || MESH_TUNNEL3_REKEY == t->state)
783 t->state = MESH_TUNNEL3_REKEY;
785 else if (MESH_TUNNEL3_WAITING == t->state)
787 t->state = MESH_TUNNEL3_KEY_SENT;
791 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unexpected state %u\n", t->state);
794 LOG (GNUNET_ERROR_TYPE_DEBUG, " next call in %s\n",
795 GNUNET_STRINGS_relative_time_to_string (REKEY_WAIT, GNUNET_YES));
796 t->rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_WAIT, &rekey_tunnel, t);
801 * Out ephemeral key has changed, create new session key on all tunnels.
803 * @param cls Closure (size of the hashmap).
804 * @param key Current public key.
805 * @param value Value in the hash map (tunnel).
807 * @return #GNUNET_YES, so we should continue to iterate,
810 rekey_iterator (void *cls,
811 const struct GNUNET_PeerIdentity *key,
814 struct MeshTunnel3 *t = value;
815 struct GNUNET_TIME_Relative delay;
819 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
822 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t) n * 100);
823 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, r);
824 t->rekey_task = GNUNET_SCHEDULER_add_delayed (delay, &rekey_tunnel, t);
831 * Create a new ephemeral key and key message, schedule next rekeying.
833 * @param cls Closure (unused).
834 * @param tc TaskContext.
837 rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
839 struct GNUNET_TIME_Absolute time;
842 rekey_task = GNUNET_SCHEDULER_NO_TASK;
844 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
847 GNUNET_free_non_null (my_ephemeral_key);
848 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
850 time = GNUNET_TIME_absolute_get ();
851 kx_msg.creation_time = GNUNET_TIME_absolute_hton (time);
852 time = GNUNET_TIME_absolute_add (time, rekey_period);
853 time = GNUNET_TIME_absolute_add (time, GNUNET_TIME_UNIT_MINUTES);
854 kx_msg.expiration_time = GNUNET_TIME_absolute_hton (time);
855 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key, &kx_msg.ephemeral_key);
857 GNUNET_assert (GNUNET_OK ==
858 GNUNET_CRYPTO_eddsa_sign (my_private_key,
862 n = (long) GNUNET_CONTAINER_multipeermap_size (tunnels);
863 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &rekey_iterator, (void *) n);
865 rekey_task = GNUNET_SCHEDULER_add_delayed (rekey_period, &rekey, NULL);
870 * Demultiplex data per channel and call appropriate channel handler.
872 * @param t Tunnel on which the data came.
873 * @param msg Data message.
874 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
875 * #GNUNET_YES if message is FWD on the respective channel (loopback)
876 * #GNUNET_NO if message is BCK on the respective channel (loopback)
877 * #GNUNET_SYSERR if message on a one-ended channel (remote)
880 handle_data (struct MeshTunnel3 *t,
881 const struct GNUNET_MESH_Data *msg,
884 struct MeshChannel *ch;
889 size = ntohs (msg->header.size);
891 sizeof (struct GNUNET_MESH_Data) +
892 sizeof (struct GNUNET_MessageHeader))
897 type = ntohs (msg->header.type);
898 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
899 GNUNET_MESH_DEBUG_M2S (type));
900 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
901 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
904 ch = GMT_get_channel (t, ntohl (msg->chid));
907 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
909 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
914 GMT_change_state (t, MESH_TUNNEL3_READY);
915 GMCH_handle_data (ch, msg, fwd);
920 * Demultiplex data ACKs per channel and update appropriate channel buffer info.
922 * @param t Tunnel on which the DATA ACK came.
923 * @param msg DATA ACK message.
924 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
925 * #GNUNET_YES if message is FWD on the respective channel (loopback)
926 * #GNUNET_NO if message is BCK on the respective channel (loopback)
927 * #GNUNET_SYSERR if message on a one-ended channel (remote)
930 handle_data_ack (struct MeshTunnel3 *t,
931 const struct GNUNET_MESH_DataACK *msg,
934 struct MeshChannel *ch;
938 size = ntohs (msg->header.size);
939 if (size != sizeof (struct GNUNET_MESH_DataACK))
946 ch = GMT_get_channel (t, ntohl (msg->chid));
949 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
951 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
956 GMCH_handle_data_ack (ch, msg, fwd);
961 * Handle channel create.
963 * @param t Tunnel on which the data came.
964 * @param msg Data message.
967 handle_ch_create (struct MeshTunnel3 *t,
968 const struct GNUNET_MESH_ChannelCreate *msg)
970 struct MeshChannel *ch;
974 size = ntohs (msg->header.size);
975 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
982 ch = GMT_get_channel (t, ntohl (msg->chid));
983 if (NULL != ch && ! GMT_is_loopback (t))
985 /* Probably a retransmission, safe to ignore */
986 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
990 ch = GMCH_handle_create (t, msg);
993 GMT_add_channel (t, ch);
999 * Handle channel NACK: check correctness and call channel handler for NACKs.
1001 * @param t Tunnel on which the NACK came.
1002 * @param msg NACK message.
1005 handle_ch_nack (struct MeshTunnel3 *t,
1006 const struct GNUNET_MESH_ChannelManage *msg)
1008 struct MeshChannel *ch;
1012 size = ntohs (msg->header.size);
1013 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
1020 ch = GMT_get_channel (t, ntohl (msg->chid));
1023 GNUNET_STATISTICS_update (stats, "# channel NACK on unknown channel",
1025 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1030 GMCH_handle_nack (ch);
1035 * Handle a CHANNEL ACK (SYNACK/ACK).
1037 * @param t Tunnel on which the CHANNEL ACK came.
1038 * @param msg CHANNEL ACK message.
1039 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1040 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1041 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1042 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1045 handle_ch_ack (struct MeshTunnel3 *t,
1046 const struct GNUNET_MESH_ChannelManage *msg,
1049 struct MeshChannel *ch;
1053 size = ntohs (msg->header.size);
1054 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
1061 ch = GMT_get_channel (t, ntohl (msg->chid));
1064 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
1066 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1071 GMCH_handle_ack (ch, msg, fwd);
1077 * Handle a channel destruction message.
1079 * @param t Tunnel on which the message came.
1080 * @param msg Channel destroy message.
1081 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1082 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1083 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1084 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1087 handle_ch_destroy (struct MeshTunnel3 *t,
1088 const struct GNUNET_MESH_ChannelManage *msg,
1091 struct MeshChannel *ch;
1095 size = ntohs (msg->header.size);
1096 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
1103 ch = GMT_get_channel (t, ntohl (msg->chid));
1106 /* Probably a retransmission, safe to ignore */
1110 GMCH_handle_destroy (ch, msg, fwd);
1115 * The peer's ephemeral key has changed: update the symmetrical keys.
1117 * @param t Tunnel this message came on.
1118 * @param msg Key eXchange message.
1121 handle_ephemeral (struct MeshTunnel3 *t,
1122 const struct GNUNET_MESH_KX_Ephemeral *msg)
1124 struct GNUNET_HashCode km;
1125 LOG (GNUNET_ERROR_TYPE_DEBUG, " ephemeral key message\n");
1127 if (GNUNET_OK != check_ephemeral (t, msg))
1129 GNUNET_break_op (0);
1132 derive_key_material (&km, &msg->ephemeral_key);
1133 LOG (GNUNET_ERROR_TYPE_DEBUG, " km is %s\n", GNUNET_h2s (&km));
1134 derive_symmertic (&t->e_key, &my_full_id, GMP_get_id (t->peer), &km);
1135 derive_symmertic (&t->d_key, GMP_get_id (t->peer), &my_full_id, &km);
1136 if (MESH_TUNNEL3_KEY_SENT == t->state)
1138 LOG (GNUNET_ERROR_TYPE_DEBUG, " our key was sent, send ping\n");
1140 t->state = MESH_TUNNEL3_REKEY;
1146 * Peer wants to check our symmetrical keys by sending an encrypted challenge.
1147 * Answer with by retransmitting the challenge with the "opposite" key.
1149 * @param t Tunnel this message came on.
1150 * @param msg Key eXchange Ping message.
1153 handle_ping (struct MeshTunnel3 *t,
1154 const struct GNUNET_MESH_KX_Ping *msg)
1156 struct GNUNET_MESH_KX_Ping res;
1158 if (ntohs (msg->header.size) != sizeof (res))
1160 GNUNET_break_op (0);
1164 LOG (GNUNET_ERROR_TYPE_DEBUG, " ping message\n");
1165 t_decrypt (t, &res.target, &msg->target, ping_encryption_size (), msg->iv);
1166 if (0 != memcmp (&my_full_id, &res.target, sizeof (my_full_id)))
1168 GNUNET_break_op (0);
1169 LOG (GNUNET_ERROR_TYPE_DEBUG, " e got %u\n", msg->nonce);
1170 LOG (GNUNET_ERROR_TYPE_DEBUG, " e towards %s\n", GNUNET_i2s (&msg->target));
1171 LOG (GNUNET_ERROR_TYPE_DEBUG, " got %u\n", res.nonce);
1172 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n", GNUNET_i2s (&res.target));
1176 send_pong (t, res.nonce);
1181 * Peer has answer to our challenge.
1182 * If answer is successful, consider the key exchange finished and clean
1183 * up all related state.
1185 * @param t Tunnel this message came on.
1186 * @param msg Key eXchange Pong message.
1189 handle_pong (struct MeshTunnel3 *t,
1190 const struct GNUNET_MESH_KX_Pong *msg)
1194 LOG (GNUNET_ERROR_TYPE_DEBUG, "PONG received\n");
1195 if (GNUNET_SCHEDULER_NO_TASK == t->rekey_task)
1197 GNUNET_break_op (0);
1200 t_decrypt (t, &challenge, &msg->nonce, sizeof (uint32_t), msg->iv);
1202 if (challenge != t->kx_ctx->challenge)
1204 LOG (GNUNET_ERROR_TYPE_DEBUG,
1205 "Wrong PONG challenge: %u (e: %u). Expected: %u.\n",
1206 challenge, msg->nonce, t->kx_ctx->challenge);
1207 GNUNET_break_op (0);
1210 GNUNET_SCHEDULER_cancel (t->rekey_task);
1211 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
1212 GNUNET_free (t->kx_ctx);
1214 t->state = MESH_TUNNEL3_READY;
1215 send_queued_data (t);
1220 * Demultiplex by message type and call appropriate handler for a message
1221 * towards a channel of a local tunnel.
1223 * @param t Tunnel this message came on.
1224 * @param msgh Message header.
1225 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1226 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1227 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1228 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1231 handle_decrypted (struct MeshTunnel3 *t,
1232 const struct GNUNET_MessageHeader *msgh,
1237 type = ntohs (msgh->type);
1238 LOG (GNUNET_ERROR_TYPE_DEBUG,
1239 "Got a %s message!\n",
1240 GNUNET_MESH_DEBUG_M2S (type));
1244 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1245 /* Don't send hop ACK, wait for client to ACK */
1246 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
1249 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
1250 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1253 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1254 handle_ch_create (t,
1255 (struct GNUNET_MESH_ChannelCreate *) msgh);
1258 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1260 (struct GNUNET_MESH_ChannelManage *) msgh);
1263 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1265 (struct GNUNET_MESH_ChannelManage *) msgh,
1269 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1270 handle_ch_destroy (t,
1271 (struct GNUNET_MESH_ChannelManage *) msgh,
1276 GNUNET_break_op (0);
1277 LOG (GNUNET_ERROR_TYPE_DEBUG,
1278 "end-to-end message not known (%u)\n",
1279 ntohs (msgh->type));
1283 /******************************************************************************/
1284 /******************************** API ***********************************/
1285 /******************************************************************************/
1288 * Decrypt and demultiplex by message type. Call appropriate handler
1289 * for every message.
1291 * @param t Tunnel this message came on.
1292 * @param msg Encrypted message.
1295 GMT_handle_encrypted (struct MeshTunnel3 *t,
1296 const struct GNUNET_MESH_Encrypted *msg)
1298 size_t size = ntohs (msg->header.size);
1299 size_t payload_size = size - sizeof (struct GNUNET_MESH_Encrypted);
1300 size_t decrypted_size;
1301 char cbuf [payload_size];
1302 struct GNUNET_MessageHeader *msgh;
1305 decrypted_size = t_decrypt (t, cbuf, &msg[1], payload_size, msg->iv);
1307 while (off < decrypted_size)
1309 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1310 handle_decrypted (t, msgh, GNUNET_SYSERR);
1311 off += ntohs (msgh->size);
1317 * Demultiplex an encapsulated KX message by message type.
1319 * @param t Tunnel on which the message came.
1320 * @param message Payload of KX message.
1323 GMT_handle_kx (struct MeshTunnel3 *t,
1324 const struct GNUNET_MessageHeader *message)
1328 type = ntohs (message->type);
1329 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message received\n", type);
1332 case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
1333 handle_ephemeral (t, (struct GNUNET_MESH_KX_Ephemeral *) message);
1336 case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
1337 handle_ping (t, (struct GNUNET_MESH_KX_Ping *) message);
1340 case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
1341 handle_pong (t, (struct GNUNET_MESH_KX_Pong *) message);
1345 GNUNET_break_op (0);
1346 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message not known (%u)\n", type);
1352 * Initialize the tunnel subsystem.
1354 * @param c Configuration handle.
1355 * @param key ECC private key, to derive all other keys and do crypto.
1358 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
1359 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
1361 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1363 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
1366 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1367 "MESH", "DEFAULT_TTL", "USING DEFAULT");
1371 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REKEY_PERIOD",
1374 rekey_period = GNUNET_TIME_UNIT_DAYS;
1377 my_private_key = key;
1378 kx_msg.header.size = htons (sizeof (kx_msg));
1379 kx_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL);
1380 kx_msg.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MESH_KX);
1381 kx_msg.purpose.size = htonl (ephemeral_purpose_size ());
1382 kx_msg.origin_identity = my_full_id;
1383 rekey_task = GNUNET_SCHEDULER_add_now (&rekey, NULL);
1385 tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
1390 * Shut down the tunnel subsystem.
1395 if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
1397 GNUNET_SCHEDULER_cancel (rekey_task);
1398 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1400 GNUNET_CONTAINER_multipeermap_destroy (tunnels);
1407 * @param destination Peer this tunnel is towards.
1409 struct MeshTunnel3 *
1410 GMT_new (struct MeshPeer *destination)
1412 struct MeshTunnel3 *t;
1414 t = GNUNET_new (struct MeshTunnel3);
1416 t->peer = destination;
1419 GNUNET_CONTAINER_multipeermap_put (tunnels, GMP_get_id (destination), t,
1420 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1431 * Change the tunnel state.
1433 * @param t Tunnel whose state to change.
1434 * @param state New state.
1437 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnel3State state)
1441 LOG (GNUNET_ERROR_TYPE_DEBUG,
1442 "Tunnel %s state was %s\n",
1444 GMT_state2s (t->state));
1445 LOG (GNUNET_ERROR_TYPE_DEBUG,
1446 "Tunnel %s state is now %s\n",
1448 GMT_state2s (state));
1449 if (myid != GMP_get_short_id (t->peer) &&
1450 MESH_TUNNEL3_WAITING == t->state && MESH_TUNNEL3_READY == state)
1452 LOG (GNUNET_ERROR_TYPE_DEBUG, " triggered rekey\n");
1453 rekey_tunnel (t, NULL);
1454 LOG (GNUNET_ERROR_TYPE_DEBUG,
1455 "Tunnel %s state is now %s\n",
1457 GMT_state2s (t->state));
1463 if (MESH_TUNNEL3_READY == state && 3 <= GMT_count_connections (t))
1465 GMP_stop_search (t->peer);
1471 * Add a connection to a tunnel.
1474 * @param c Connection.
1477 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
1479 struct MeshTConnection *aux;
1481 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1485 aux = GNUNET_new (struct MeshTConnection);
1487 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
1492 * Remove a connection from a tunnel.
1495 * @param c Connection.
1498 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
1500 struct MeshTConnection *aux;
1502 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1505 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
1513 * Add a channel to a tunnel.
1516 * @param ch Channel.
1519 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
1521 struct MeshTChannel *aux;
1523 GNUNET_assert (NULL != ch);
1525 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
1527 for (aux = t->channel_head; aux != NULL; aux = aux->next)
1529 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
1534 aux = GNUNET_new (struct MeshTChannel);
1536 LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
1537 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
1542 * Remove a channel from a tunnel.
1545 * @param ch Channel.
1548 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
1550 struct MeshTChannel *aux;
1552 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
1553 for (aux = t->channel_head; aux != NULL; aux = aux->next)
1557 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GMCH_2s (ch));
1558 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
1567 * Search for a channel by global ID.
1569 * @param t Tunnel containing the channel.
1570 * @param chid Public channel number.
1572 * @return channel handler, NULL if doesn't exist
1574 struct MeshChannel *
1575 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
1577 struct MeshTChannel *iter;
1582 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1584 if (GMCH_get_id (iter->ch) == chid)
1588 return NULL == iter ? NULL : iter->ch;
1593 * Tunnel is empty: destroy it.
1595 * Notifies all connections about the destruction.
1597 * @param t Tunnel to destroy.
1600 GMT_destroy_empty (struct MeshTunnel3 *t)
1602 struct MeshTConnection *iter;
1604 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1606 GMC_send_destroy (iter->c);
1609 t->destroy = GNUNET_YES;
1614 * Destroy tunnel if empty (no more channels).
1616 * @param t Tunnel to destroy if empty.
1619 GMT_destroy_if_empty (struct MeshTunnel3 *t)
1621 if (1 < GMT_count_channels (t))
1624 GMT_destroy_empty (t);
1629 * Destroy the tunnel.
1631 * This function does not generate any warning traffic to clients or peers.
1634 * Cancel messages belonging to this tunnel queued to neighbors.
1635 * Free any allocated resources linked to the tunnel.
1637 * @param t The tunnel to destroy.
1640 GMT_destroy (struct MeshTunnel3 *t)
1642 struct MeshTConnection *iter;
1643 struct MeshTConnection *next;
1648 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
1650 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
1651 // GNUNET_break (0);
1653 for (iter = t->connection_head; NULL != iter; iter = next)
1656 GMC_destroy (iter->c);
1659 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
1660 GMP_set_tunnel (t->peer, NULL);
1662 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
1663 GNUNET_SCHEDULER_cancel (t->rekey_task);
1670 * @brief Use the given path for the tunnel.
1671 * Update the next and prev hops (and RCs).
1672 * (Re)start the path refresh in case the tunnel is locally owned.
1674 * @param t Tunnel to update.
1675 * @param p Path to use.
1677 * @return Connection created.
1679 struct MeshConnection *
1680 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
1682 struct MeshConnection *c;
1683 struct GNUNET_HashCode cid;
1684 unsigned int own_pos;
1686 if (NULL == t || NULL == p)
1692 for (own_pos = 0; own_pos < p->length; own_pos++)
1694 if (p->peers[own_pos] == myid)
1697 if (own_pos > p->length - 1)
1703 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
1704 c = GMC_new (&cid, t, p, own_pos);
1705 GMT_add_connection (t, c);
1711 * Count established (ready) connections of a tunnel.
1713 * @param t Tunnel on which to count.
1715 * @return Number of connections.
1718 GMT_count_connections (struct MeshTunnel3 *t)
1720 struct MeshTConnection *iter;
1723 for (count = 0, iter = t->connection_head;
1725 iter = iter->next, count++);
1731 * Count channels of a tunnel.
1733 * @param t Tunnel on which to count.
1735 * @return Number of channels.
1738 GMT_count_channels (struct MeshTunnel3 *t)
1740 struct MeshTChannel *iter;
1743 for (count = 0, iter = t->channel_head;
1745 iter = iter->next, count++) /* skip */;
1752 * Get the state of a tunnel.
1756 * @return Tunnel's state.
1758 enum MeshTunnel3State
1759 GMT_get_state (struct MeshTunnel3 *t)
1764 return (enum MeshTunnel3State) -1;
1771 * Get the maximum buffer space for a tunnel towards a local client.
1775 * @return Biggest buffer space offered by any channel in the tunnel.
1778 GMT_get_channels_buffer (struct MeshTunnel3 *t)
1780 struct MeshTChannel *iter;
1781 unsigned int buffer;
1782 unsigned int ch_buf;
1784 if (NULL == t->channel_head)
1786 /* Probably getting buffer for a channel create/handshake. */
1791 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1793 ch_buf = get_channel_buffer (iter);
1794 if (ch_buf > buffer)
1802 * Get the total buffer space for a tunnel for P2P traffic.
1806 * @return Buffer space offered by all connections in the tunnel.
1809 GMT_get_connections_buffer (struct MeshTunnel3 *t)
1811 struct MeshTConnection *iter;
1812 unsigned int buffer;
1814 iter = t->connection_head;
1816 while (NULL != iter)
1818 if (GMC_get_state (iter->c) != MESH_CONNECTION_READY)
1824 buffer += get_connection_buffer (iter);
1833 * Get the tunnel's destination.
1837 * @return ID of the destination peer.
1839 const struct GNUNET_PeerIdentity *
1840 GMT_get_destination (struct MeshTunnel3 *t)
1842 return GMP_get_id (t->peer);
1847 * Get the tunnel's next free global channel ID.
1851 * @return GID of a channel free to use.
1854 GMT_get_next_chid (struct MeshTunnel3 *t)
1856 MESH_ChannelNumber chid;
1858 while (NULL != GMT_get_channel (t, t->next_chid))
1860 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
1861 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1863 chid = t->next_chid;
1864 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1871 * Send ACK on one or more channels due to buffer in connections.
1873 * @param t Channel which has some free buffer space.
1876 GMT_unchoke_channels (struct MeshTunnel3 *t)
1878 struct MeshTChannel *iter;
1879 unsigned int buffer;
1880 unsigned int channels = GMT_count_channels (t);
1881 unsigned int choked_n;
1882 struct MeshChannel *choked[channels];
1884 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_unchoke_channels on %s\n", GMT_2s (t));
1885 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
1886 if (NULL != t->channel_head)
1887 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
1889 /* Get buffer space */
1890 buffer = GMT_get_connections_buffer (t);
1896 /* Count and remember choked channels */
1898 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1900 if (GNUNET_NO == get_channel_allowed (iter))
1902 choked[choked_n++] = iter->ch;
1906 /* Unchoke random channels */
1907 while (0 < buffer && 0 < choked_n)
1909 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1911 GMCH_allow_client (choked[r], GMCH_is_origin (choked[r], GNUNET_YES));
1914 choked[r] = choked[choked_n];
1920 * Send ACK on one or more connections due to buffer space to the client.
1922 * Iterates all connections of the tunnel and sends ACKs appropriately.
1927 GMT_send_connection_acks (struct MeshTunnel3 *t)
1929 struct MeshTConnection *iter;
1932 uint32_t allow_per_connection;
1934 unsigned int buffer;
1936 LOG (GNUNET_ERROR_TYPE_DEBUG,
1937 "Tunnel send connection ACKs on %s\n",
1946 buffer = GMT_get_channels_buffer (t);
1948 /* Count connections, how many messages are already allowed */
1949 cs = GMT_count_connections (t);
1950 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
1952 allowed += get_connection_allowed (iter);
1955 /* Make sure there is no overflow */
1956 if (allowed > buffer)
1961 /* Authorize connections to send more data */
1962 to_allow = buffer; /* - allowed; */
1964 for (iter = t->connection_head; NULL != iter && to_allow > 0; iter = iter->next)
1966 allow_per_connection = to_allow/cs;
1967 to_allow -= allow_per_connection;
1969 if (get_connection_allowed (iter) > 64 / 3)
1973 GMC_allow (iter->c, buffer, GMC_is_origin (iter->c, GNUNET_YES));
1976 GNUNET_break (to_allow == 0);
1981 * Callback called when a queued message is sent.
1983 * Calculates the average time and connection packet tracking.
1985 * @param cls Closure (TunnelQueue handle).
1986 * @param c Connection this message was on.
1987 * @param type Type of message sent.
1988 * @param fwd Was this a FWD going message?
1989 * @param size Size of the message.
1992 message_sent (void *cls,
1993 struct MeshConnection *c,
1994 struct MeshConnectionQueue *q,
1995 uint16_t type, int fwd, size_t size)
1997 struct MeshTunnel3Queue *qt = cls;
1999 GNUNET_assert (NULL != qt->cont);
2000 qt->cont (qt->cont_cls, GMC_get_tunnel (c), qt, type, size);
2006 * Cancel a previously sent message while it's in the queue.
2008 * ONLY can be called before the continuation given to the send function
2009 * is called. Once the continuation is called, the message is no longer in the
2012 * @param q Handle to the queue.
2015 GMT_cancel (struct MeshTunnel3Queue *q)
2018 /* message_sent() will be called and free q */
2022 * Sends an already built message on a tunnel, encrypting it and
2023 * choosing the best connection.
2025 * @param message Message to send. Function modifies it.
2026 * @param t Tunnel on which this message is transmitted.
2027 * @param ch Channel on which this message is transmitted.
2028 * @param fwd Is this a fwd message on @c ch?
2029 * @param cont Continuation to call once message is really sent.
2030 * @param cont_cls Closure for @c cont.
2032 * @return Handle to cancel message. NULL if @c cont is NULL.
2034 struct MeshTunnel3Queue *
2035 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2036 struct MeshTunnel3 *t,
2037 struct MeshChannel *ch, int fwd,
2038 GMT_sent cont, void *cont_cls)
2040 struct MeshTunnel3Queue *q;
2041 struct MeshConnection *c;
2042 struct GNUNET_MESH_Encrypted *msg;
2043 size_t size = ntohs (message->size);
2044 size_t encrypted_size;
2045 char cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
2049 if (MESH_TUNNEL3_READY != t->state)
2051 queue_data (t, ch, message);
2055 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMT_2s (t));
2057 if (GMT_is_loopback (t))
2059 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
2060 handle_decrypted (t, message, fwd);
2061 /* FIXME: call cont? */
2062 return NULL; /* Already delivered, cannot cancel */
2065 iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
2066 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
2067 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
2069 encrypted_size = t_encrypt (t, &msg[1], message, size, iv);
2070 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + encrypted_size);
2071 c = tunnel_get_connection (t);
2074 GNUNET_break (GNUNET_YES == t->destroy);
2077 type = ntohs (message->type);
2080 case GNUNET_MESSAGE_TYPE_MESH_DATA:
2081 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2082 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
2083 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
2084 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
2085 msg->cid = *GMC_get_id (c);
2086 msg->ttl = htonl (default_ttl);
2089 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
2090 GNUNET_MESH_DEBUG_M2S (type));
2094 fwd = GMC_is_origin (c, GNUNET_YES);
2098 (void) GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
2101 q = GNUNET_new (struct MeshTunnel3Queue); /* FIXME valgrind: leak*/
2102 q->q = GMC_send_prebuilt_message (&msg->header, c, fwd, &message_sent, q);
2104 q->cont_cls = cont_cls;
2110 * Is the tunnel directed towards the local peer?
2114 * @return #GNUNET_YES if it is loopback.
2117 GMT_is_loopback (const struct MeshTunnel3 *t)
2119 return (myid == GMP_get_short_id (t->peer));
2124 * Is the tunnel this path already?
2129 * @return #GNUNET_YES a connection uses this path.
2132 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p)
2134 struct MeshTConnection *iter;
2136 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2137 if (GMC_get_path (iter->c) == p)
2145 * Get a cost of a path for a tunnel considering existing connections.
2148 * @param path Candidate path.
2150 * @return Cost of the path (path length + number of overlapping nodes)
2153 GMT_get_path_cost (const struct MeshTunnel3 *t,
2154 const struct MeshPeerPath *path)
2156 struct MeshTConnection *iter;
2157 unsigned int overlap;
2165 GNUNET_assert (NULL != t);
2167 for (i = 0; i < path->length; i++)
2169 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2171 for (j = 0; j < GMC_get_path (iter->c)->length; j++)
2173 if (path->peers[i] == GMC_get_path (iter->c)->peers[j])
2181 return (path->length + overlap) * (path->score * -1);
2186 * Get the static string for the peer this tunnel is directed.
2190 * @return Static string the destination peer's ID.
2193 GMT_2s (const struct MeshTunnel3 *t)
2198 return GMP_2s (t->peer);