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 #define CONNECTIONS_PER_TUNNEL 3
41 /******************************************************************************/
42 /******************************** STRUCTS **********************************/
43 /******************************************************************************/
47 struct MeshTChannel *next;
48 struct MeshTChannel *prev;
49 struct MeshChannel *ch;
54 * Connection list and metadata.
56 struct MeshTConnection
61 struct MeshTConnection *next;
66 struct MeshTConnection *prev;
71 struct MeshConnection *c;
74 * Creation time, to keep oldest connection alive.
76 struct GNUNET_TIME_Absolute created;
79 * Connection throughput, to keep fastest connection alive.
85 * Structure used during a Key eXchange.
87 struct MeshTunnelKXCtx
90 * Decryption ("their") old key, for decrypting traffic sent by the
91 * other end before the key exchange started.
93 struct GNUNET_CRYPTO_SymmetricSessionKey d_key_old;
96 * Challenge to send in a ping and expect in the pong.
102 * Struct containing all information regarding a tunnel to a peer.
107 * Endpoint of the tunnel.
109 struct MeshPeer *peer;
112 * State of the tunnel connectivity.
114 enum MeshTunnel3CState cstate;
117 * State of the tunnel encryption.
119 enum MeshTunnel3EState estate;
122 * Key eXchange context.
124 struct MeshTunnelKXCtx *kx_ctx;
127 * Encryption ("our") key.
129 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
132 * Decryption ("their") key.
134 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
137 * Task to start the rekey process.
139 GNUNET_SCHEDULER_TaskIdentifier rekey_task;
142 * Paths that are actively used to reach the destination peer.
144 struct MeshTConnection *connection_head;
145 struct MeshTConnection *connection_tail;
148 * Next connection number.
153 * Channels inside this tunnel.
155 struct MeshTChannel *channel_head;
156 struct MeshTChannel *channel_tail;
159 * Channel ID for the next created channel.
161 MESH_ChannelNumber next_chid;
164 * Destroy flag: if true, destroy on last message.
166 GNUNET_SCHEDULER_TaskIdentifier destroy_task;
169 * Queued messages, to transmit once tunnel gets connected.
171 struct MeshTunnelDelayed *tq_head;
172 struct MeshTunnelDelayed *tq_tail;
177 * Struct used to save messages in a non-ready tunnel to send once connected.
179 struct MeshTunnelDelayed
184 struct MeshTunnelDelayed *next;
185 struct MeshTunnelDelayed *prev;
190 struct MeshTunnel3 *t;
193 * Tunnel queue given to the channel to cancel request. Update on send_queued.
195 struct MeshTunnel3Queue *tq;
200 /* struct GNUNET_MessageHeader *msg; */
205 * Handle for messages queued but not yet sent.
207 struct MeshTunnel3Queue
210 * Connection queue handle, to cancel if necessary.
212 struct MeshConnectionQueue *cq;
215 * Handle in case message hasn't been given to a connection yet.
217 struct MeshTunnelDelayed *tqd;
220 * Continuation to call once sent.
225 * Closure for @c cont.
231 /******************************************************************************/
232 /******************************* GLOBALS ***********************************/
233 /******************************************************************************/
236 * Global handle to the statistics service.
238 extern struct GNUNET_STATISTICS_Handle *stats;
241 * Local peer own ID (memory efficient handle).
243 extern GNUNET_PEER_Id myid;
246 * Local peer own ID (full value).
248 extern struct GNUNET_PeerIdentity my_full_id;
252 * Don't try to recover tunnels if shutting down.
254 extern int shutting_down;
258 * Set of all tunnels, in order to trigger a new exchange on rekey.
259 * Indexed by peer's ID.
261 static struct GNUNET_CONTAINER_MultiPeerMap *tunnels;
264 * Default TTL for payload packets.
266 static unsigned long long default_ttl;
271 const static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
274 * Own ephemeral private key.
276 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
279 * Cached message used to perform a key exchange.
281 static struct GNUNET_MESH_KX_Ephemeral kx_msg;
284 * Task to generate a new ephemeral key.
286 static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
291 static struct GNUNET_TIME_Relative rekey_period;
293 /******************************************************************************/
294 /******************************** STATIC ***********************************/
295 /******************************************************************************/
298 * Get string description for tunnel connectivity state.
300 * @param cs Tunnel state.
302 * @return String representation.
305 cstate2s (enum MeshTunnel3CState cs)
307 static char buf[128];
311 case MESH_TUNNEL3_NEW:
312 return "MESH_TUNNEL3_NEW";
313 case MESH_TUNNEL3_SEARCHING:
314 return "MESH_TUNNEL3_SEARCHING";
315 case MESH_TUNNEL3_WAITING:
316 return "MESH_TUNNEL3_WAITING";
317 case MESH_TUNNEL3_READY:
318 return "MESH_TUNNEL3_READY";
321 sprintf (buf, "%u (UNKNOWN STATE)", cs);
329 * Get string description for tunnel encryption state.
331 * @param es Tunnel state.
333 * @return String representation.
336 estate2s (enum MeshTunnel3EState es)
338 static char buf[128];
342 case MESH_TUNNEL3_KEY_UNINITIALIZED:
343 return "MESH_TUNNEL3_KEY_UNINITIALIZED";
344 case MESH_TUNNEL3_KEY_SENT:
345 return "MESH_TUNNEL3_KEY_SENT";
346 case MESH_TUNNEL3_KEY_PING:
347 return "MESH_TUNNEL3_KEY_PING";
348 case MESH_TUNNEL3_KEY_OK:
349 return "MESH_TUNNEL3_KEY_OK";
352 sprintf (buf, "%u (UNKNOWN STATE)", es);
360 * @brief Check if tunnel is ready to send traffic.
362 * Tunnel must be connected and with encryption correctly set up.
364 * @param t Tunnel to check.
366 * @return #GNUNET_YES if ready, #GNUNET_NO otherwise
369 is_ready (struct MeshTunnel3 *t)
374 ready = (MESH_TUNNEL3_READY == t->cstate && MESH_TUNNEL3_KEY_OK == t->estate);
375 ready = ready || GMT_is_loopback (t);
381 * Ephemeral key message purpose size.
383 * @return Size of the part of the ephemeral key message that must be signed.
386 ephemeral_purpose_size (void)
388 return sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
389 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
390 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
391 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
392 sizeof (struct GNUNET_PeerIdentity);
397 * Size of the encrypted part of a ping message.
399 * @return Size of the encrypted part of a ping message.
402 ping_encryption_size (void)
404 return sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t);
409 * Get the channel's buffer. ONLY FOR NON-LOOPBACK CHANNELS!!
411 * @param tch Tunnel's channel handle.
413 * @return Amount of messages the channel can still buffer towards the client.
416 get_channel_buffer (const struct MeshTChannel *tch)
420 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
421 fwd = GMCH_is_origin (tch->ch, GNUNET_YES);
423 return GMCH_get_buffer (tch->ch, fwd);
428 * Get the channel's allowance status.
430 * @param tch Tunnel's channel handle.
432 * @return #GNUNET_YES if we allowed the client to send data to us.
435 get_channel_allowed (const struct MeshTChannel *tch)
439 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
440 fwd = GMCH_is_origin (tch->ch, GNUNET_YES);
442 return GMCH_get_allowed (tch->ch, fwd);
447 * Get the connection's buffer.
449 * @param tc Tunnel's connection handle.
451 * @return Amount of messages the connection can still buffer.
454 get_connection_buffer (const struct MeshTConnection *tc)
458 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
459 fwd = GMC_is_origin (tc->c, GNUNET_YES);
461 return GMC_get_buffer (tc->c, fwd);
466 * Get the connection's allowance.
468 * @param tc Tunnel's connection handle.
470 * @return Amount of messages we have allowed the next peer to send us.
473 get_connection_allowed (const struct MeshTConnection *tc)
477 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
478 fwd = GMC_is_origin (tc->c, GNUNET_YES);
480 return GMC_get_allowed (tc->c, fwd);
485 * Check that a ephemeral key message s well formed and correctly signed.
487 * @param t Tunnel on which the message came.
488 * @param msg The ephemeral key message.
490 * @return GNUNET_OK if message is fine, GNUNET_SYSERR otherwise.
493 check_ephemeral (struct MeshTunnel3 *t,
494 const struct GNUNET_MESH_KX_Ephemeral *msg)
496 /* Check message size */
497 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_KX_Ephemeral))
498 return GNUNET_SYSERR;
500 /* Check signature size */
501 if (ntohl (msg->purpose.size) != ephemeral_purpose_size ())
502 return GNUNET_SYSERR;
505 if (0 != memcmp (&msg->origin_identity,
506 GMP_get_id (t->peer),
507 sizeof (struct GNUNET_PeerIdentity)))
508 return GNUNET_SYSERR;
510 /* Check signature */
512 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_MESH_KX,
515 &msg->origin_identity.public_key))
516 return GNUNET_SYSERR;
523 * Encrypt data with the tunnel key.
525 * @param t Tunnel whose key to use.
526 * @param dst Destination for the encrypted data.
527 * @param src Source of the plaintext. Can overlap with @c dst.
528 * @param size Size of the plaintext.
529 * @param iv Initialization Vector to use.
532 t_encrypt (struct MeshTunnel3 *t,
533 void *dst, const void *src,
534 size_t size, uint32_t iv)
536 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
539 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt start\n");
540 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (iv), NULL);
541 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt IV derived\n");
542 out_size = GNUNET_CRYPTO_symmetric_encrypt (src, size, &t->e_key, &siv, dst);
543 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt end\n");
550 * Decrypt data with the tunnel key.
552 * @param t Tunnel whose key to use.
553 * @param dst Destination for the plaintext.
554 * @param src Source of the encrypted data. Can overlap with @c dst.
555 * @param size Size of the encrypted data.
556 * @param iv Initialization Vector to use.
559 t_decrypt (struct MeshTunnel3 *t,
560 void *dst, const void *src,
561 size_t size, uint32_t iv)
563 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
564 struct GNUNET_CRYPTO_SymmetricSessionKey *key;
567 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt start\n");
568 if (t->estate == MESH_TUNNEL3_KEY_OK || t->estate == MESH_TUNNEL3_KEY_PING)
572 else if (NULL != t->kx_ctx)
574 key = &t->kx_ctx->d_key_old;
578 GNUNET_STATISTICS_update (stats, "# non decryptable data", 1, GNUNET_NO);
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 "WARNING got data on %s without a valid key\n",
586 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt iv\n");
587 GNUNET_CRYPTO_symmetric_derive_iv (&siv, key, &iv, sizeof (iv), NULL);
588 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt iv done\n");
589 out_size = GNUNET_CRYPTO_symmetric_decrypt (src, size, key, &siv, dst);
590 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt end\n");
597 * Create key material by doing ECDH on the local and remote ephemeral keys.
599 * @param key_material Where to store the key material.
600 * @param ephemeral_key Peer's public ephemeral key.
603 derive_key_material (struct GNUNET_HashCode *key_material,
604 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key)
607 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
616 * Create a symmetic key from the identities of both ends and the key material
619 * @param key Destination for the generated key.
620 * @param sender ID of the peer that will encrypt with @c key.
621 * @param receiver ID of the peer that will decrypt with @c key.
622 * @param key_material Hash created with ECDH with the ephemeral keys.
625 derive_symmertic (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
626 const struct GNUNET_PeerIdentity *sender,
627 const struct GNUNET_PeerIdentity *receiver,
628 const struct GNUNET_HashCode *key_material)
630 const char salt[] = "MESH kx salt";
632 GNUNET_CRYPTO_kdf (key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
634 key_material, sizeof (struct GNUNET_HashCode),
635 sender, sizeof (struct GNUNET_PeerIdentity),
636 receiver, sizeof (struct GNUNET_PeerIdentity),
641 * Pick a connection on which send the next data message.
643 * @param t Tunnel on which to send the message.
645 * @return The connection on which to send the next message.
647 static struct MeshConnection *
648 tunnel_get_connection (struct MeshTunnel3 *t)
650 struct MeshTConnection *iter;
651 struct MeshConnection *best;
653 unsigned int lowest_q;
655 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMT_2s (t));
658 for (iter = t->connection_head; NULL != iter; iter = iter->next)
660 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
661 GMC_2s (iter->c), GMC_get_state (iter->c));
662 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
664 qn = GMC_get_qn (iter->c, GMC_is_origin (iter->c, GNUNET_YES));
665 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
673 LOG (GNUNET_ERROR_TYPE_DEBUG, " selected: connection %s\n", GMC_2s (best));
679 * Callback called when a queued message is sent.
681 * Calculates the average time and connection packet tracking.
683 * @param cls Closure (TunnelQueue handle).
684 * @param c Connection this message was on.
685 * @param q Connection queue handle (unused).
686 * @param type Type of message sent.
687 * @param fwd Was this a FWD going message?
688 * @param size Size of the message.
691 message_sent (void *cls,
692 struct MeshConnection *c,
693 struct MeshConnectionQueue *q,
694 uint16_t type, int fwd, size_t size)
696 struct MeshTunnel3Queue *qt = cls;
698 GNUNET_assert (NULL != qt->cont);
699 qt->cont (qt->cont_cls, GMC_get_tunnel (c), qt, type, size);
705 * Delete a queued message: either was sent or the channel was destroyed
706 * before the tunnel's key exchange had a chance to finish.
708 * @param tqd Delayed queue handle.
711 unqueue_data (struct MeshTunnelDelayed *tqd)
713 GNUNET_CONTAINER_DLL_remove (tqd->t->tq_head, tqd->t->tq_tail, tqd);
719 * Cache a message to be sent once tunnel is online.
721 * @param t Tunnel to hold the message.
722 * @param msg Message itself (copy will be made).
724 static struct MeshTunnelDelayed *
725 queue_data (struct MeshTunnel3 *t, const struct GNUNET_MessageHeader *msg)
727 struct MeshTunnelDelayed *tqd;
728 uint16_t size = ntohs (msg->size);
730 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue data on Tunnel %s\n", GMT_2s (t));
732 if (GNUNET_YES == is_ready (t))
738 tqd = GNUNET_malloc (sizeof (struct MeshTunnelDelayed) + size);
741 memcpy (&tqd[1], msg, size);
742 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tqd);
750 * @param t Tunnel to get keys from.
751 * @param plaintext Content to HMAC.
752 * @param size Size of @c plaintext.
753 * @param iv Initialization vector for the message.
754 * @param outgoing Is this an outgoing message that we encrypted?
755 * @param hmac Destination to store the HMAC.
758 t_hmac (struct MeshTunnel3 *t, const void *plaintext, size_t size, uint32_t iv,
759 int outgoing, struct GNUNET_MeshHash *hmac)
761 struct GNUNET_CRYPTO_AuthKey auth_key;
762 static const char ctx[] = "mesh authentication key";
763 struct GNUNET_CRYPTO_SymmetricSessionKey *key;
764 struct GNUNET_HashCode hash;
766 key = outgoing ? &t->e_key : &t->d_key;
767 GNUNET_CRYPTO_hmac_derive_key (&auth_key, key,
772 GNUNET_CRYPTO_hmac (&auth_key, plaintext, size, &hash);
773 memcpy (hmac, &hash, sizeof (*hmac));
778 * Sends an already built message on a tunnel, encrypting it and
779 * choosing the best connection.
781 * @param message Message to send. Function modifies it.
782 * @param t Tunnel on which this message is transmitted.
783 * @param c Connection to use (autoselect if NULL).
784 * @param force Force the tunnel to take the message (buffer overfill).
785 * @param cont Continuation to call once message is really sent.
786 * @param cont_cls Closure for @c cont.
787 * @param existing_q In case this a transmission of previously queued data,
788 * this should be TunnelQueue given to the client.
791 * @return Handle to cancel message. NULL if @c cont is NULL.
793 static struct MeshTunnel3Queue *
794 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
795 struct MeshTunnel3 *t, struct MeshConnection *c,
796 int force, GMT_sent cont, void *cont_cls,
797 struct MeshTunnel3Queue *existing_q)
799 struct MeshTunnel3Queue *tq;
800 struct GNUNET_MESH_Encrypted *msg;
801 size_t size = ntohs (message->size);
802 char cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
807 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMT_2s (t));
809 if (GNUNET_NO == is_ready (t))
811 struct MeshTunnelDelayed *tqd;
812 /* A non null existing_q indicates sending of queued data.
813 * Should only happen after tunnel becomes ready.
815 GNUNET_assert (NULL == existing_q);
816 tqd = queue_data (t, message);
819 tq = GNUNET_new (struct MeshTunnel3Queue);
823 tq->cont_cls = cont_cls;
827 GNUNET_assert (GNUNET_NO == GMT_is_loopback (t));
829 iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
830 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
831 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
833 GNUNET_assert (t_encrypt (t, &msg[1], message, size, iv) == size);
834 t_hmac (t, &msg[1], size, iv, GNUNET_YES, &msg->hmac);
835 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
838 c = tunnel_get_connection (t);
841 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task
842 || MESH_TUNNEL3_SEARCHING != t->cstate)
850 type = ntohs (message->type);
853 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
854 case GNUNET_MESSAGE_TYPE_MESH_DATA:
855 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
856 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
857 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
858 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
859 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
860 msg->cid = *GMC_get_id (c);
861 msg->ttl = htonl (default_ttl);
864 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
869 fwd = GMC_is_origin (c, GNUNET_YES);
873 (void) GMC_send_prebuilt_message (&msg->header, c, fwd, force, NULL, NULL);
876 if (NULL == existing_q)
878 tq = GNUNET_new (struct MeshTunnel3Queue); /* FIXME valgrind: leak*/
885 tq->cq = GMC_send_prebuilt_message (&msg->header, c, fwd, force,
888 tq->cont_cls = cont_cls;
895 * Send all cached messages that we can, tunnel is online.
897 * @param t Tunnel that holds the messages. Cannot be loopback.
900 send_queued_data (struct MeshTunnel3 *t)
902 struct MeshTunnelDelayed *tqd;
903 struct MeshTunnelDelayed *next;
906 LOG (GNUNET_ERROR_TYPE_DEBUG,
907 "GMT_send_queued_data on tunnel %s\n",
910 if (GMT_is_loopback (t))
916 if (GNUNET_NO == is_ready (t))
918 LOG (GNUNET_ERROR_TYPE_DEBUG, " not ready yet: %s/%s\n",
919 estate2s (t->estate), cstate2s (t->cstate));
923 room = GMT_get_connections_buffer (t);
924 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
925 LOG (GNUNET_ERROR_TYPE_DEBUG, " tq head: %p\n", t->tq_head);
926 for (tqd = t->tq_head; NULL != tqd && room > 0; tqd = next)
928 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending queued data\n");
931 send_prebuilt_message ((struct GNUNET_MessageHeader *) &tqd[1],
932 tqd->t, NULL, GNUNET_YES,
933 NULL != tqd->tq ? tqd->tq->cont : NULL,
934 NULL != tqd->tq ? tqd->tq->cont_cls : NULL,
938 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_send_queued_data end\n", GMP_2s (t->peer));
943 * Sends key exchange message on a tunnel, choosing the best connection.
944 * Should not be called on loopback tunnels.
946 * @param t Tunnel on which this message is transmitted.
947 * @param message Message to send. Function modifies it.
950 send_kx (struct MeshTunnel3 *t,
951 const struct GNUNET_MessageHeader *message)
953 struct MeshConnection *c;
954 struct GNUNET_MESH_KX *msg;
955 size_t size = ntohs (message->size);
956 char cbuf[sizeof (struct GNUNET_MESH_KX) + size];
960 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT KX on Tunnel %s\n", GMT_2s (t));
962 /* Avoid loopback. */
963 if (GMT_is_loopback (t))
965 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
970 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
972 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother\n");
976 /* Must have a connection. */
977 if (NULL == t->connection_head)
979 GNUNET_break (MESH_TUNNEL3_SEARCHING == t->cstate);
984 msg = (struct GNUNET_MESH_KX *) cbuf;
985 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX);
986 msg->header.size = htons (sizeof (struct GNUNET_MESH_KX) + size);
987 c = tunnel_get_connection (t);
990 GNUNET_break (GNUNET_SCHEDULER_NO_TASK != t->destroy_task
991 || MESH_TUNNEL3_READY != t->cstate);
995 type = ntohs (message->type);
998 case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
999 case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
1000 case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
1001 memcpy (&msg[1], message, size);
1004 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1009 fwd = GMC_is_origin (t->connection_head->c, GNUNET_YES);
1010 /* TODO save handle and cancel in case of a unneeded retransmission */
1011 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_YES, NULL, NULL);
1016 * Send the ephemeral key on a tunnel.
1018 * @param t Tunnel on which to send the key.
1021 send_ephemeral (struct MeshTunnel3 *t)
1023 LOG (GNUNET_ERROR_TYPE_INFO, "=> EPHM for %s\n", GMT_2s (t));
1025 kx_msg.sender_status = htonl (t->estate);
1026 send_kx (t, &kx_msg.header);
1030 * Send a ping message on a tunnel.
1032 * @param t Tunnel on which to send the ping.
1035 send_ping (struct MeshTunnel3 *t)
1037 struct GNUNET_MESH_KX_Ping msg;
1039 LOG (GNUNET_ERROR_TYPE_INFO, "=> PING for %s\n", GMT_2s (t));
1040 msg.header.size = htons (sizeof (msg));
1041 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_PING);
1042 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1043 msg.target = *GMP_get_id (t->peer);
1044 msg.nonce = t->kx_ctx->challenge;
1046 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
1047 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n", GNUNET_i2s (&msg.target));
1048 t_encrypt (t, &msg.target, &msg.target, ping_encryption_size(), msg.iv);
1049 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, " e towards %s\n", GNUNET_i2s (&msg.target));
1052 send_kx (t, &msg.header);
1057 * Send a pong message on a tunnel.
1059 * @param t Tunnel on which to send the pong.
1060 * @param challenge Value sent in the ping that we have to send back.
1063 send_pong (struct MeshTunnel3 *t, uint32_t challenge)
1065 struct GNUNET_MESH_KX_Pong msg;
1067 LOG (GNUNET_ERROR_TYPE_INFO, "=> PONG for %s\n", GMT_2s (t));
1068 msg.header.size = htons (sizeof (msg));
1069 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_PONG);
1070 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1071 msg.nonce = challenge;
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
1073 t_encrypt (t, &msg.nonce, &msg.nonce, sizeof (msg.nonce), msg.iv);
1074 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
1076 send_kx (t, &msg.header);
1081 * Initiate a rekey with the remote peer.
1083 * @param cls Closure (tunnel).
1084 * @param tc TaskContext.
1087 rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1089 struct MeshTunnel3 *t = cls;
1091 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
1093 LOG (GNUNET_ERROR_TYPE_DEBUG, "Re-key Tunnel %s\n", GMT_2s (t));
1094 if (NULL != tc && 0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1097 if (NULL == t->kx_ctx)
1099 LOG (GNUNET_ERROR_TYPE_DEBUG, " new kx ctx\n");
1100 t->kx_ctx = GNUNET_new (struct MeshTunnelKXCtx);
1101 t->kx_ctx->challenge =
1102 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1103 t->kx_ctx->d_key_old = t->d_key;
1104 LOG (GNUNET_ERROR_TYPE_DEBUG, " new challenge for %s: %u\n",
1105 GMT_2s (t), t->kx_ctx->challenge);
1110 case MESH_TUNNEL3_KEY_UNINITIALIZED:
1111 t->estate = MESH_TUNNEL3_KEY_SENT;
1113 case MESH_TUNNEL3_KEY_SENT:
1115 case MESH_TUNNEL3_KEY_PING:
1116 case MESH_TUNNEL3_KEY_OK:
1118 t->estate = MESH_TUNNEL3_KEY_PING;
1121 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unexpected state %u\n", t->estate);
1124 LOG (GNUNET_ERROR_TYPE_DEBUG, " next call in %s\n",
1125 GNUNET_STRINGS_relative_time_to_string (REKEY_WAIT, GNUNET_YES));
1126 t->rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_WAIT, &rekey_tunnel, t);
1131 * Out ephemeral key has changed, create new session key on all tunnels.
1133 * @param cls Closure (size of the hashmap).
1134 * @param key Current public key.
1135 * @param value Value in the hash map (tunnel).
1137 * @return #GNUNET_YES, so we should continue to iterate,
1140 rekey_iterator (void *cls,
1141 const struct GNUNET_PeerIdentity *key,
1144 struct MeshTunnel3 *t = value;
1145 struct GNUNET_TIME_Relative delay;
1146 long n = (long) cls;
1149 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
1152 if (GNUNET_YES == GMT_is_loopback (t))
1155 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t) n * 100);
1156 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, r);
1157 t->rekey_task = GNUNET_SCHEDULER_add_delayed (delay, &rekey_tunnel, t);
1164 * Create a new ephemeral key and key message, schedule next rekeying.
1166 * @param cls Closure (unused).
1167 * @param tc TaskContext.
1170 rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1172 struct GNUNET_TIME_Absolute time;
1175 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1177 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1180 GNUNET_free_non_null (my_ephemeral_key);
1181 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1183 time = GNUNET_TIME_absolute_get ();
1184 kx_msg.creation_time = GNUNET_TIME_absolute_hton (time);
1185 time = GNUNET_TIME_absolute_add (time, rekey_period);
1186 time = GNUNET_TIME_absolute_add (time, GNUNET_TIME_UNIT_MINUTES);
1187 kx_msg.expiration_time = GNUNET_TIME_absolute_hton (time);
1188 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key, &kx_msg.ephemeral_key);
1190 GNUNET_assert (GNUNET_OK ==
1191 GNUNET_CRYPTO_eddsa_sign (my_private_key,
1193 &kx_msg.signature));
1195 n = (long) GNUNET_CONTAINER_multipeermap_size (tunnels);
1196 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &rekey_iterator, (void *) n);
1198 rekey_task = GNUNET_SCHEDULER_add_delayed (rekey_period, &rekey, NULL);
1203 * Called only on shutdown, destroy every tunnel.
1205 * @param cls Closure (unused).
1206 * @param key Current public key.
1207 * @param value Value in the hash map (tunnel).
1209 * @return #GNUNET_YES, so we should continue to iterate,
1212 destroy_iterator (void *cls,
1213 const struct GNUNET_PeerIdentity *key,
1216 struct MeshTunnel3 *t = value;
1224 * Notify remote peer that we don't know a channel he is talking about,
1225 * probably CHANNEL_DESTROY was missed.
1227 * @param t Tunnel on which to notify.
1228 * @param gid ID of the channel.
1231 send_channel_destroy (struct MeshTunnel3 *t, unsigned int gid)
1233 struct GNUNET_MESH_ChannelManage msg;
1235 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1236 msg.header.size = htons (sizeof (msg));
1237 msg.chid = htonl (gid);
1239 LOG (GNUNET_ERROR_TYPE_DEBUG,
1240 "WARNING destroying unknown channel %u on tunnel %s\n",
1242 send_prebuilt_message (&msg.header, t, NULL, GNUNET_YES, NULL, NULL, NULL);
1247 * Demultiplex data per channel and call appropriate channel handler.
1249 * @param t Tunnel on which the data came.
1250 * @param msg Data message.
1251 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1252 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1253 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1254 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1257 handle_data (struct MeshTunnel3 *t,
1258 const struct GNUNET_MESH_Data *msg,
1261 struct MeshChannel *ch;
1265 size = ntohs (msg->header.size);
1267 sizeof (struct GNUNET_MESH_Data) +
1268 sizeof (struct GNUNET_MessageHeader))
1273 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
1274 GM_m2s (ntohs (msg[1].header.type)));
1277 ch = GMT_get_channel (t, ntohl (msg->chid));
1280 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel 0x%X unknown\n",
1284 send_channel_destroy (t, ntohl (msg->chid));
1288 GMCH_handle_data (ch, msg, fwd);
1293 * Demultiplex data ACKs per channel and update appropriate channel buffer info.
1295 * @param t Tunnel on which the DATA ACK came.
1296 * @param msg DATA ACK message.
1297 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1298 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1299 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1300 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1303 handle_data_ack (struct MeshTunnel3 *t,
1304 const struct GNUNET_MESH_DataACK *msg,
1307 struct MeshChannel *ch;
1311 size = ntohs (msg->header.size);
1312 if (size != sizeof (struct GNUNET_MESH_DataACK))
1319 ch = GMT_get_channel (t, ntohl (msg->chid));
1322 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
1324 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1329 GMCH_handle_data_ack (ch, msg, fwd);
1334 * Handle channel create.
1336 * @param t Tunnel on which the data came.
1337 * @param msg Data message.
1340 handle_ch_create (struct MeshTunnel3 *t,
1341 const struct GNUNET_MESH_ChannelCreate *msg)
1343 struct MeshChannel *ch;
1347 size = ntohs (msg->header.size);
1348 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
1355 ch = GMT_get_channel (t, ntohl (msg->chid));
1356 if (NULL != ch && ! GMT_is_loopback (t))
1358 /* Probably a retransmission, safe to ignore */
1359 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
1363 ch = GMCH_handle_create (t, msg);
1366 GMT_add_channel (t, ch);
1372 * Handle channel NACK: check correctness and call channel handler for NACKs.
1374 * @param t Tunnel on which the NACK came.
1375 * @param msg NACK message.
1378 handle_ch_nack (struct MeshTunnel3 *t,
1379 const struct GNUNET_MESH_ChannelManage *msg)
1381 struct MeshChannel *ch;
1385 size = ntohs (msg->header.size);
1386 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
1393 ch = GMT_get_channel (t, ntohl (msg->chid));
1396 GNUNET_STATISTICS_update (stats, "# channel NACK on unknown channel",
1398 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1403 GMCH_handle_nack (ch);
1408 * Handle a CHANNEL ACK (SYNACK/ACK).
1410 * @param t Tunnel on which the CHANNEL ACK came.
1411 * @param msg CHANNEL ACK message.
1412 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1413 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1414 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1415 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1418 handle_ch_ack (struct MeshTunnel3 *t,
1419 const struct GNUNET_MESH_ChannelManage *msg,
1422 struct MeshChannel *ch;
1426 size = ntohs (msg->header.size);
1427 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
1434 ch = GMT_get_channel (t, ntohl (msg->chid));
1437 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
1439 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1444 GMCH_handle_ack (ch, msg, fwd);
1450 * Handle a channel destruction message.
1452 * @param t Tunnel on which the message came.
1453 * @param msg Channel destroy message.
1454 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1455 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1456 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1457 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1460 handle_ch_destroy (struct MeshTunnel3 *t,
1461 const struct GNUNET_MESH_ChannelManage *msg,
1464 struct MeshChannel *ch;
1468 size = ntohs (msg->header.size);
1469 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
1476 ch = GMT_get_channel (t, ntohl (msg->chid));
1479 /* Probably a retransmission, safe to ignore */
1483 GMCH_handle_destroy (ch, msg, fwd);
1488 * The peer's ephemeral key has changed: update the symmetrical keys.
1490 * @param t Tunnel this message came on.
1491 * @param msg Key eXchange message.
1494 handle_ephemeral (struct MeshTunnel3 *t,
1495 const struct GNUNET_MESH_KX_Ephemeral *msg)
1497 struct GNUNET_HashCode km;
1498 LOG (GNUNET_ERROR_TYPE_INFO, "<= EPHM for %s\n", GMT_2s (t));
1500 if (GNUNET_OK != check_ephemeral (t, msg))
1502 GNUNET_break_op (0);
1505 derive_key_material (&km, &msg->ephemeral_key);
1506 LOG (GNUNET_ERROR_TYPE_DEBUG, " km is %s\n", GNUNET_h2s (&km));
1507 derive_symmertic (&t->e_key, &my_full_id, GMP_get_id (t->peer), &km);
1508 derive_symmertic (&t->d_key, GMP_get_id (t->peer), &my_full_id, &km);
1509 if (MESH_TUNNEL3_KEY_SENT == t->estate)
1511 LOG (GNUNET_ERROR_TYPE_DEBUG, " our key was sent, send ping\n");
1513 t->estate = MESH_TUNNEL3_KEY_PING;
1519 * Peer wants to check our symmetrical keys by sending an encrypted challenge.
1520 * Answer with by retransmitting the challenge with the "opposite" key.
1522 * @param t Tunnel this message came on.
1523 * @param msg Key eXchange Ping message.
1526 handle_ping (struct MeshTunnel3 *t,
1527 const struct GNUNET_MESH_KX_Ping *msg)
1529 struct GNUNET_MESH_KX_Ping res;
1531 if (ntohs (msg->header.size) != sizeof (res))
1533 GNUNET_break_op (0);
1537 LOG (GNUNET_ERROR_TYPE_INFO, "<= PING for %s\n", GMT_2s (t));
1538 t_decrypt (t, &res.target, &msg->target, ping_encryption_size (), msg->iv);
1539 if (0 != memcmp (&my_full_id, &res.target, sizeof (my_full_id)))
1541 GNUNET_STATISTICS_update (stats, "# malformed PINGs", 1, GNUNET_NO);
1542 LOG (GNUNET_ERROR_TYPE_WARNING, " malformed PING\n");
1543 LOG (GNUNET_ERROR_TYPE_DEBUG, " e got %u\n", msg->nonce);
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, " e towards %s\n", GNUNET_i2s (&msg->target));
1545 LOG (GNUNET_ERROR_TYPE_DEBUG, " got %u\n", res.nonce);
1546 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n", GNUNET_i2s (&res.target));
1550 send_pong (t, res.nonce);
1555 * Peer has answer to our challenge.
1556 * If answer is successful, consider the key exchange finished and clean
1557 * up all related state.
1559 * @param t Tunnel this message came on.
1560 * @param msg Key eXchange Pong message.
1563 handle_pong (struct MeshTunnel3 *t,
1564 const struct GNUNET_MESH_KX_Pong *msg)
1568 LOG (GNUNET_ERROR_TYPE_INFO, "<= PONG for %s\n", GMT_2s (t));
1569 if (GNUNET_SCHEDULER_NO_TASK == t->rekey_task)
1571 GNUNET_STATISTICS_update (stats, "# duplicate PONG messages", 1, GNUNET_NO);
1574 t_decrypt (t, &challenge, &msg->nonce, sizeof (uint32_t), msg->iv);
1576 if (challenge != t->kx_ctx->challenge)
1578 LOG (GNUNET_ERROR_TYPE_WARNING, "Wrong PONG challenge\n");
1579 LOG (GNUNET_ERROR_TYPE_DEBUG, "PONG: %u (e: %u). Expected: %u.\n",
1580 challenge, msg->nonce, t->kx_ctx->challenge);
1581 GNUNET_break_op (0);
1584 GNUNET_SCHEDULER_cancel (t->rekey_task);
1585 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
1586 GNUNET_free (t->kx_ctx);
1588 GMT_change_estate (t, MESH_TUNNEL3_KEY_OK);
1593 * Demultiplex by message type and call appropriate handler for a message
1594 * towards a channel of a local tunnel.
1596 * @param t Tunnel this message came on.
1597 * @param msgh Message header.
1598 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1599 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1600 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1601 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1604 handle_decrypted (struct MeshTunnel3 *t,
1605 const struct GNUNET_MessageHeader *msgh,
1610 type = ntohs (msgh->type);
1611 LOG (GNUNET_ERROR_TYPE_INFO, "<= %s on %s\n", GM_m2s (type), GMT_2s (t));
1615 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
1616 /* Do nothing, connection aleady got updated. */
1617 GNUNET_STATISTICS_update (stats, "# keepalives received", 1, GNUNET_NO);
1620 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1621 /* Don't send hop ACK, wait for client to ACK */
1622 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
1625 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
1626 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1629 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1630 handle_ch_create (t,
1631 (struct GNUNET_MESH_ChannelCreate *) msgh);
1634 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1636 (struct GNUNET_MESH_ChannelManage *) msgh);
1639 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1641 (struct GNUNET_MESH_ChannelManage *) msgh,
1645 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1646 handle_ch_destroy (t,
1647 (struct GNUNET_MESH_ChannelManage *) msgh,
1652 GNUNET_break_op (0);
1653 LOG (GNUNET_ERROR_TYPE_WARNING,
1654 "end-to-end message not known (%u)\n",
1655 ntohs (msgh->type));
1660 /******************************************************************************/
1661 /******************************** API ***********************************/
1662 /******************************************************************************/
1665 * Decrypt and demultiplex by message type. Call appropriate handler
1666 * for every message.
1668 * @param t Tunnel this message came on.
1669 * @param msg Encrypted message.
1672 GMT_handle_encrypted (struct MeshTunnel3 *t,
1673 const struct GNUNET_MESH_Encrypted *msg)
1675 size_t size = ntohs (msg->header.size);
1676 size_t payload_size = size - sizeof (struct GNUNET_MESH_Encrypted);
1677 size_t decrypted_size;
1678 char cbuf [payload_size];
1679 struct GNUNET_MessageHeader *msgh;
1681 struct GNUNET_MeshHash hmac;
1683 decrypted_size = t_decrypt (t, cbuf, &msg[1], payload_size, msg->iv);
1684 t_hmac (t, &msg[1], payload_size, msg->iv, GNUNET_NO, &hmac);
1685 if (0 != memcmp (&hmac, &msg->hmac, sizeof (hmac)))
1687 /* checksum failed */
1688 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1689 "Failed checksum validation for a message on tunnel `%s'\n",
1691 GNUNET_STATISTICS_update (stats, "# wrong HMAC", 1, GNUNET_NO);
1695 while (off < decrypted_size)
1697 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1698 handle_decrypted (t, msgh, GNUNET_SYSERR);
1699 off += ntohs (msgh->size);
1705 * Demultiplex an encapsulated KX message by message type.
1707 * @param t Tunnel on which the message came.
1708 * @param message Payload of KX message.
1711 GMT_handle_kx (struct MeshTunnel3 *t,
1712 const struct GNUNET_MessageHeader *message)
1716 type = ntohs (message->type);
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message received\n", type);
1720 case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
1721 handle_ephemeral (t, (struct GNUNET_MESH_KX_Ephemeral *) message);
1724 case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
1725 handle_ping (t, (struct GNUNET_MESH_KX_Ping *) message);
1728 case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
1729 handle_pong (t, (struct GNUNET_MESH_KX_Pong *) message);
1733 GNUNET_break_op (0);
1734 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message not known (%u)\n", type);
1740 * Initialize the tunnel subsystem.
1742 * @param c Configuration handle.
1743 * @param key ECC private key, to derive all other keys and do crypto.
1746 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
1747 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
1749 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1751 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
1754 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1755 "MESH", "DEFAULT_TTL", "USING DEFAULT");
1759 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REKEY_PERIOD",
1762 rekey_period = GNUNET_TIME_UNIT_DAYS;
1765 my_private_key = key;
1766 kx_msg.header.size = htons (sizeof (kx_msg));
1767 kx_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL);
1768 kx_msg.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MESH_KX);
1769 kx_msg.purpose.size = htonl (ephemeral_purpose_size ());
1770 kx_msg.origin_identity = my_full_id;
1771 rekey_task = GNUNET_SCHEDULER_add_now (&rekey, NULL);
1773 tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
1778 * Shut down the tunnel subsystem.
1783 if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
1785 GNUNET_SCHEDULER_cancel (rekey_task);
1786 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1788 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &destroy_iterator, NULL);
1789 GNUNET_CONTAINER_multipeermap_destroy (tunnels);
1796 * @param destination Peer this tunnel is towards.
1798 struct MeshTunnel3 *
1799 GMT_new (struct MeshPeer *destination)
1801 struct MeshTunnel3 *t;
1803 t = GNUNET_new (struct MeshTunnel3);
1805 t->peer = destination;
1808 GNUNET_CONTAINER_multipeermap_put (tunnels, GMP_get_id (destination), t,
1809 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1820 * Change the tunnel's connection state.
1822 * @param t Tunnel whose connection state to change.
1823 * @param cstate New connection state.
1826 GMT_change_cstate (struct MeshTunnel3* t, enum MeshTunnel3CState cstate)
1830 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s cstate %s => %s\n",
1831 GMP_2s (t->peer), cstate2s (t->cstate), cstate2s (cstate));
1832 if (myid != GMP_get_short_id (t->peer) &&
1833 MESH_TUNNEL3_READY != t->cstate &&
1834 MESH_TUNNEL3_READY == cstate)
1837 if (MESH_TUNNEL3_KEY_OK == t->estate)
1839 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered send queued data\n");
1840 send_queued_data (t);
1842 else if (MESH_TUNNEL3_KEY_UNINITIALIZED == t->estate)
1844 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered rekey\n");
1845 rekey_tunnel (t, NULL);
1850 if (MESH_TUNNEL3_READY == cstate
1851 && CONNECTIONS_PER_TUNNEL <= GMT_count_connections (t))
1853 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered stop dht\n");
1854 GMP_stop_search (t->peer);
1859 * Change the tunnel encryption state.
1861 * @param t Tunnel whose encryption state to change.
1862 * @param state New encryption state.
1865 GMT_change_estate (struct MeshTunnel3* t, enum MeshTunnel3EState state)
1869 LOG (GNUNET_ERROR_TYPE_DEBUG,
1870 "Tunnel %s estate was %s\n",
1871 GMP_2s (t->peer), estate2s (t->estate));
1872 LOG (GNUNET_ERROR_TYPE_DEBUG,
1873 "Tunnel %s estate is now %s\n",
1874 GMP_2s (t->peer), estate2s (state));
1875 if (myid != GMP_get_short_id (t->peer) &&
1876 MESH_TUNNEL3_KEY_OK != t->estate && MESH_TUNNEL3_KEY_OK == state)
1879 send_queued_data (t);
1887 * Check that the tunnel doesn't have too many connections,
1888 * remove one if necessary.
1890 * For the time being, this means the newest connection.
1892 * @param t Tunnel to check.
1895 check_connection_count (struct MeshTunnel3 *t)
1897 if (GMT_count_connections (t) > CONNECTIONS_PER_TUNNEL)
1899 struct MeshTConnection *iter;
1900 struct MeshTConnection *c;
1902 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1904 if (NULL == c || iter->created.abs_value_us > c->created.abs_value_us)
1917 * Add a connection to a tunnel.
1920 * @param c Connection.
1923 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
1925 struct MeshTConnection *aux;
1927 GNUNET_assert (NULL != c);
1929 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1933 aux = GNUNET_new (struct MeshTConnection);
1935 aux->created = GNUNET_TIME_absolute_get ();
1937 GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, aux);
1939 check_connection_count (t);
1944 * Mark a path as no longer valid for this tunnel: has been tried and failed.
1946 * @param t Tunnel to update.
1947 * @param path Invalid path to remove. Is destroyed after removal.
1950 GMT_remove_path (struct MeshTunnel3 *t, struct MeshPeerPath *path)
1952 GMP_remove_path (t->peer, path);
1957 * Remove a connection from a tunnel.
1960 * @param c Connection.
1963 GMT_remove_connection (struct MeshTunnel3 *t,
1964 struct MeshConnection *c)
1966 struct MeshTConnection *aux;
1967 struct MeshTConnection *next;
1969 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing connection %s from tunnel %s\n",
1970 GMC_2s (c), GMT_2s (t));
1971 for (aux = t->connection_head; aux != NULL; aux = next)
1976 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
1981 /* Start new connections if needed */
1982 if (NULL == t->connection_head
1983 && GNUNET_SCHEDULER_NO_TASK == t->destroy_task
1984 && MESH_TUNNEL3_SHUTDOWN != t->cstate
1985 && GNUNET_NO == shutting_down)
1987 LOG (GNUNET_ERROR_TYPE_DEBUG, " no more connections, getting new ones\n");
1988 t->cstate = MESH_TUNNEL3_SEARCHING;
1989 GMP_connect (t->peer);
1993 /* If not marked as ready, no change is needed */
1994 if (MESH_TUNNEL3_READY != t->cstate)
1997 /* Check if any connection is ready to maintaing cstate */
1998 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1999 if (MESH_CONNECTION_READY == GMC_get_state (aux->c))
2002 t->cstate = MESH_TUNNEL3_WAITING;
2007 * Add a channel to a tunnel.
2010 * @param ch Channel.
2013 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
2015 struct MeshTChannel *aux;
2017 GNUNET_assert (NULL != ch);
2019 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
2021 for (aux = t->channel_head; aux != NULL; aux = aux->next)
2023 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
2028 aux = GNUNET_new (struct MeshTChannel);
2030 LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
2031 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
2033 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
2035 GNUNET_SCHEDULER_cancel (t->destroy_task);
2036 t->destroy_task = GNUNET_SCHEDULER_NO_TASK;
2037 LOG (GNUNET_ERROR_TYPE_DEBUG, " undo destroy!\n");
2043 * Remove a channel from a tunnel.
2046 * @param ch Channel.
2049 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
2051 struct MeshTChannel *aux;
2053 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
2054 for (aux = t->channel_head; aux != NULL; aux = aux->next)
2058 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GMCH_2s (ch));
2059 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
2068 * Search for a channel by global ID.
2070 * @param t Tunnel containing the channel.
2071 * @param chid Public channel number.
2073 * @return channel handler, NULL if doesn't exist
2075 struct MeshChannel *
2076 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
2078 struct MeshTChannel *iter;
2083 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2085 if (GMCH_get_id (iter->ch) == chid)
2089 return NULL == iter ? NULL : iter->ch;
2094 * @brief Destroy a tunnel and free all resources.
2096 * Should only be called a while after the tunnel has been marked as destroyed,
2097 * in case there is a new channel added to the same peer shortly after marking
2098 * the tunnel. This way we avoid a new public key handshake.
2100 * @param cls Closure (tunnel to destroy).
2101 * @param tc Task context.
2104 delayed_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2106 struct MeshTunnel3 *t = cls;
2107 struct MeshTConnection *iter;
2109 t->destroy_task = GNUNET_SCHEDULER_NO_TASK;
2110 t->cstate = MESH_TUNNEL3_SHUTDOWN;
2112 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2114 GMC_send_destroy (iter->c);
2121 * Tunnel is empty: destroy it.
2123 * Notifies all connections about the destruction.
2125 * @param t Tunnel to destroy.
2128 GMT_destroy_empty (struct MeshTunnel3 *t)
2130 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
2132 LOG (GNUNET_ERROR_TYPE_DEBUG,
2133 "Tunnel %s is already scheduled for destruction\n",
2136 /* should never happen, tunnel can only become empty once, and the
2137 * task identifier should be NO_TASK (cleaned when the tunnel was created
2138 * or became un-empty)
2143 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s empty: destroying scheduled\n",
2146 t->destroy_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
2147 &delayed_destroy, t);
2152 * Destroy tunnel if empty (no more channels).
2154 * @param t Tunnel to destroy if empty.
2157 GMT_destroy_if_empty (struct MeshTunnel3 *t)
2159 if (1 < GMT_count_channels (t))
2162 GMT_destroy_empty (t);
2167 * Destroy the tunnel.
2169 * This function does not generate any warning traffic to clients or peers.
2172 * Cancel messages belonging to this tunnel queued to neighbors.
2173 * Free any allocated resources linked to the tunnel.
2175 * @param t The tunnel to destroy.
2178 GMT_destroy (struct MeshTunnel3 *t)
2180 struct MeshTConnection *iter_c;
2181 struct MeshTConnection *next_c;
2182 struct MeshTChannel *iter_ch;
2183 struct MeshTChannel *next_ch;
2188 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
2190 GNUNET_SCHEDULER_cancel (t->destroy_task);
2191 t->destroy_task = GNUNET_SCHEDULER_NO_TASK;
2194 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
2196 GNUNET_break (GNUNET_YES ==
2197 GNUNET_CONTAINER_multipeermap_remove (tunnels,
2198 GMP_get_id (t->peer), t));
2200 for (iter_c = t->connection_head; NULL != iter_c; iter_c = next_c)
2202 next_c = iter_c->next;
2203 GMC_destroy (iter_c->c);
2205 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = next_ch)
2207 next_ch = iter_ch->next;
2208 GMCH_destroy (iter_ch->ch);
2209 /* Should only happen on shutdown, but it's ok. */
2212 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2213 GMP_set_tunnel (t->peer, NULL);
2215 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
2217 GNUNET_SCHEDULER_cancel (t->rekey_task);
2218 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
2219 if (NULL != t->kx_ctx)
2220 GNUNET_free (t->kx_ctx);
2230 * @brief Use the given path for the tunnel.
2231 * Update the next and prev hops (and RCs).
2232 * (Re)start the path refresh in case the tunnel is locally owned.
2234 * @param t Tunnel to update.
2235 * @param p Path to use.
2237 * @return Connection created.
2239 struct MeshConnection *
2240 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
2242 struct MeshConnection *c;
2243 struct GNUNET_MeshHash cid;
2244 unsigned int own_pos;
2246 if (NULL == t || NULL == p)
2252 if (MESH_TUNNEL3_SHUTDOWN == t->cstate)
2258 for (own_pos = 0; own_pos < p->length; own_pos++)
2260 if (p->peers[own_pos] == myid)
2263 if (own_pos >= p->length)
2265 GNUNET_break_op (0);
2269 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE, &cid, sizeof (cid));
2270 c = GMC_new (&cid, t, p, own_pos);
2273 /* Path was flawed */
2276 GMT_add_connection (t, c);
2282 * Count established (ready) connections of a tunnel.
2284 * @param t Tunnel on which to count.
2286 * @return Number of connections.
2289 GMT_count_connections (struct MeshTunnel3 *t)
2291 struct MeshTConnection *iter;
2294 for (count = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
2295 if (MESH_CONNECTION_DESTROYED != GMC_get_state (iter->c))
2302 * Count channels of a tunnel.
2304 * @param t Tunnel on which to count.
2306 * @return Number of channels.
2309 GMT_count_channels (struct MeshTunnel3 *t)
2311 struct MeshTChannel *iter;
2314 for (count = 0, iter = t->channel_head;
2316 iter = iter->next, count++) /* skip */;
2323 * Get the connectivity state of a tunnel.
2327 * @return Tunnel's connectivity state.
2329 enum MeshTunnel3CState
2330 GMT_get_cstate (struct MeshTunnel3 *t)
2335 return (enum MeshTunnel3CState) -1;
2342 * Get the encryption state of a tunnel.
2346 * @return Tunnel's encryption state.
2348 enum MeshTunnel3EState
2349 GMT_get_estate (struct MeshTunnel3 *t)
2354 return (enum MeshTunnel3EState) -1;
2360 * Get the maximum buffer space for a tunnel towards a local client.
2364 * @return Biggest buffer space offered by any channel in the tunnel.
2367 GMT_get_channels_buffer (struct MeshTunnel3 *t)
2369 struct MeshTChannel *iter;
2370 unsigned int buffer;
2371 unsigned int ch_buf;
2373 if (NULL == t->channel_head)
2375 /* Probably getting buffer for a channel create/handshake. */
2380 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2382 ch_buf = get_channel_buffer (iter);
2383 if (ch_buf > buffer)
2391 * Get the total buffer space for a tunnel for P2P traffic.
2395 * @return Buffer space offered by all connections in the tunnel.
2398 GMT_get_connections_buffer (struct MeshTunnel3 *t)
2400 struct MeshTConnection *iter;
2401 unsigned int buffer;
2404 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2406 if (GMC_get_state (iter->c) != MESH_CONNECTION_READY)
2410 buffer += get_connection_buffer (iter);
2418 * Get the tunnel's destination.
2422 * @return ID of the destination peer.
2424 const struct GNUNET_PeerIdentity *
2425 GMT_get_destination (struct MeshTunnel3 *t)
2427 return GMP_get_id (t->peer);
2432 * Get the tunnel's next free global channel ID.
2436 * @return GID of a channel free to use.
2439 GMT_get_next_chid (struct MeshTunnel3 *t)
2441 MESH_ChannelNumber chid;
2442 MESH_ChannelNumber mask;
2445 /* Set bit 30 depending on the ID relationship. Bit 31 is always 0 for GID.
2446 * If our ID is bigger or loopback tunnel, start at 0, bit 30 = 0
2447 * If peer's ID is bigger, start at 0x4... bit 30 = 1
2449 result = GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GMP_get_id (t->peer));
2455 while (NULL != GMT_get_channel (t, t->next_chid))
2457 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
2458 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
2459 t->next_chid |= mask;
2461 chid = t->next_chid;
2462 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
2463 t->next_chid |= mask;
2470 * Send ACK on one or more channels due to buffer in connections.
2472 * @param t Channel which has some free buffer space.
2475 GMT_unchoke_channels (struct MeshTunnel3 *t)
2477 struct MeshTChannel *iter;
2478 unsigned int buffer;
2479 unsigned int channels = GMT_count_channels (t);
2480 unsigned int choked_n;
2481 struct MeshChannel *choked[channels];
2483 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_unchoke_channels on %s\n", GMT_2s (t));
2484 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
2485 if (NULL != t->channel_head)
2486 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
2488 /* Get buffer space */
2489 buffer = GMT_get_connections_buffer (t);
2495 /* Count and remember choked channels */
2497 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2499 if (GNUNET_NO == get_channel_allowed (iter))
2501 choked[choked_n++] = iter->ch;
2505 /* Unchoke random channels */
2506 while (0 < buffer && 0 < choked_n)
2508 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2510 GMCH_allow_client (choked[r], GMCH_is_origin (choked[r], GNUNET_YES));
2513 choked[r] = choked[choked_n];
2519 * Send ACK on one or more connections due to buffer space to the client.
2521 * Iterates all connections of the tunnel and sends ACKs appropriately.
2526 GMT_send_connection_acks (struct MeshTunnel3 *t)
2528 struct MeshTConnection *iter;
2531 uint32_t allow_per_connection;
2533 unsigned int buffer;
2535 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel send connection ACKs on %s\n",
2544 buffer = GMT_get_channels_buffer (t);
2545 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer %u\n", buffer);
2547 /* Count connections, how many messages are already allowed */
2548 cs = GMT_count_connections (t);
2549 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
2551 allowed += get_connection_allowed (iter);
2553 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowed %u\n", allowed);
2555 /* Make sure there is no overflow */
2556 if (allowed > buffer)
2561 /* Authorize connections to send more data */
2562 to_allow = buffer; /* - allowed; */
2564 for (iter = t->connection_head;
2565 NULL != iter && to_allow > 0;
2568 allow_per_connection = to_allow/cs;
2569 to_allow -= allow_per_connection;
2571 if (get_connection_allowed (iter) > 64 / 3)
2575 GMC_allow (iter->c, allow_per_connection,
2576 GMC_is_origin (iter->c, GNUNET_NO));
2579 GNUNET_break (to_allow == 0);
2584 * Cancel a previously sent message while it's in the queue.
2586 * ONLY can be called before the continuation given to the send function
2587 * is called. Once the continuation is called, the message is no longer in the
2590 * @param q Handle to the queue.
2593 GMT_cancel (struct MeshTunnel3Queue *q)
2598 /* message_sent() will be called and free q */
2600 else if (NULL != q->tqd)
2602 unqueue_data (q->tqd);
2604 if (NULL != q->cont)
2605 q->cont (q->cont_cls, NULL, q, 0, 0);
2616 * Sends an already built message on a tunnel, encrypting it and
2617 * choosing the best connection if not provided.
2619 * @param message Message to send. Function modifies it.
2620 * @param t Tunnel on which this message is transmitted.
2621 * @param c Connection to use (autoselect if NULL).
2622 * @param force Force the tunnel to take the message (buffer overfill).
2623 * @param cont Continuation to call once message is really sent.
2624 * @param cont_cls Closure for @c cont.
2626 * @return Handle to cancel message. NULL if @c cont is NULL.
2628 struct MeshTunnel3Queue *
2629 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2630 struct MeshTunnel3 *t, struct MeshConnection *c,
2631 int force, GMT_sent cont, void *cont_cls)
2633 return send_prebuilt_message (message, t, c, force, cont, cont_cls, NULL);
2638 * Is the tunnel directed towards the local peer?
2642 * @return #GNUNET_YES if it is loopback.
2645 GMT_is_loopback (const struct MeshTunnel3 *t)
2647 return (myid == GMP_get_short_id (t->peer));
2652 * Is the tunnel this path already?
2657 * @return #GNUNET_YES a connection uses this path.
2660 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p)
2662 struct MeshTConnection *iter;
2664 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2665 if (GMC_get_path (iter->c) == p)
2673 * Get a cost of a path for a tunnel considering existing connections.
2676 * @param path Candidate path.
2678 * @return Cost of the path (path length + number of overlapping nodes)
2681 GMT_get_path_cost (const struct MeshTunnel3 *t,
2682 const struct MeshPeerPath *path)
2684 struct MeshTConnection *iter;
2685 const struct MeshPeerPath *aux;
2686 unsigned int overlap;
2694 GNUNET_assert (NULL != t);
2696 for (i = 0; i < path->length; i++)
2698 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2700 aux = GMC_get_path (iter->c);
2704 for (j = 0; j < aux->length; j++)
2706 if (path->peers[i] == aux->peers[j])
2714 return path->length + overlap;
2719 * Get the static string for the peer this tunnel is directed.
2723 * @return Static string the destination peer's ID.
2726 GMT_2s (const struct MeshTunnel3 *t)
2731 return GMP_2s (t->peer);
2735 /******************************************************************************/
2736 /***************************** INFO/DEBUG *******************************/
2737 /******************************************************************************/
2741 * Log all possible info about the tunnel state to stderr.
2743 * @param t Tunnel to debug.
2746 GMT_debug (const struct MeshTunnel3 *t)
2748 struct MeshTChannel *iterch;
2749 struct MeshTConnection *iterc;
2751 LOG (GNUNET_ERROR_TYPE_DEBUG, "DEBUG TUNNEL TOWARDS %s\n", GMT_2s (t));
2752 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate %s, estate %s\n",
2753 cstate2s (t->cstate), estate2s (t->estate));
2754 LOG (GNUNET_ERROR_TYPE_DEBUG, " kx_ctx %p, rekey_task %u\n",
2755 t->kx_ctx, t->rekey_task);
2756 LOG (GNUNET_ERROR_TYPE_DEBUG, " tq_head %p, tq_tail %p\n",
2757 t->tq_head, t->tq_tail);
2758 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy %u\n", t->destroy_task);
2760 LOG (GNUNET_ERROR_TYPE_DEBUG, " channels:\n");
2761 for (iterch = t->channel_head; NULL != iterch; iterch = iterch->next)
2763 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %s\n", GMCH_2s (iterch->ch));
2766 LOG (GNUNET_ERROR_TYPE_DEBUG, " connections:\n");
2767 for (iterc = t->connection_head; NULL != iterc; iterc = iterc->next)
2769 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %s [%u] buf: %u/%u (qn %u/%u)\n",
2770 GMC_2s (iterc->c), GMC_get_state (iterc->c),
2771 GMC_get_buffer (iterc->c, GNUNET_YES),
2772 GMC_get_buffer (iterc->c, GNUNET_NO),
2773 GMC_get_qn (iterc->c, GNUNET_YES),
2774 GMC_get_qn (iterc->c, GNUNET_NO));
2777 LOG (GNUNET_ERROR_TYPE_DEBUG, "DEBUG TUNNEL END\n");
2782 * Iterate all tunnels.
2784 * @param iter Iterator.
2785 * @param cls Closure for @c iter.
2788 GMT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2790 GNUNET_CONTAINER_multipeermap_iterate (tunnels, iter, cls);
2795 * Count all tunnels.
2797 * @return Number of tunnels to remote peers kept by this peer.
2800 GMT_count_all (void)
2802 return GNUNET_CONTAINER_multipeermap_size (tunnels);
2807 * Iterate all connections of a tunnel.
2809 * @param t Tunnel whose connections to iterate.
2810 * @param iter Iterator.
2811 * @param cls Closure for @c iter.
2814 GMT_iterate_connections (struct MeshTunnel3 *t, GMT_conn_iter iter, void *cls)
2816 struct MeshTConnection *ct;
2818 for (ct = t->connection_head; NULL != ct; ct = ct->next)
2824 * Iterate all channels of a tunnel.
2826 * @param t Tunnel whose channels to iterate.
2827 * @param iter Iterator.
2828 * @param cls Closure for @c iter.
2831 GMT_iterate_channels (struct MeshTunnel3 *t, GMT_chan_iter iter, void *cls)
2833 struct MeshTChannel *cht;
2835 for (cht = t->channel_head; NULL != cht; cht = cht->next)
2836 iter (cls, cht->ch);