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 connectivity.
87 enum MeshTunnel3CState cstate;
90 * State of the tunnel encryption.
92 enum MeshTunnel3EState estate;
95 * Key eXchange context.
97 struct MeshTunnelKXCtx *kx_ctx;
100 * Encryption ("our") key.
102 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
105 * Decryption ("their") key.
107 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
110 * Task to start the rekey process.
112 GNUNET_SCHEDULER_TaskIdentifier rekey_task;
115 * Paths that are actively used to reach the destination peer.
117 struct MeshTConnection *connection_head;
118 struct MeshTConnection *connection_tail;
121 * Next connection number.
126 * Channels inside this tunnel.
128 struct MeshTChannel *channel_head;
129 struct MeshTChannel *channel_tail;
132 * Channel ID for the next created channel.
134 MESH_ChannelNumber next_chid;
137 * Destroy flag: if true, destroy on last message.
139 GNUNET_SCHEDULER_TaskIdentifier destroy_task;
142 * Queued messages, to transmit once tunnel gets connected.
144 struct MeshTunnelDelayed *tq_head;
145 struct MeshTunnelDelayed *tq_tail;
150 * Struct used to save messages in a non-ready tunnel to send once connected.
152 struct MeshTunnelDelayed
157 struct MeshTunnelDelayed *next;
158 struct MeshTunnelDelayed *prev;
163 struct MeshTunnel3 *t;
166 * Tunnel queue given to the channel to cancel request. Update on send_queued.
168 struct MeshTunnel3Queue *tq;
173 /* struct GNUNET_MessageHeader *msg; */
178 * Handle for messages queued but not yet sent.
180 struct MeshTunnel3Queue
183 * Connection queue handle, to cancel if necessary.
185 struct MeshConnectionQueue *cq;
188 * Handle in case message hasn't been given to a connection yet.
190 struct MeshTunnelDelayed *tqd;
193 * Continuation to call once sent.
198 * Closure for @c cont.
204 /******************************************************************************/
205 /******************************* GLOBALS ***********************************/
206 /******************************************************************************/
209 * Global handle to the statistics service.
211 extern struct GNUNET_STATISTICS_Handle *stats;
214 * Local peer own ID (memory efficient handle).
216 extern GNUNET_PEER_Id myid;
219 * Local peer own ID (full value).
221 extern struct GNUNET_PeerIdentity my_full_id;
225 * Don't try to recover tunnels if shutting down.
227 extern int shutting_down;
231 * Set of all tunnels, in order to trigger a new exchange on rekey.
232 * Indexed by peer's ID.
234 static struct GNUNET_CONTAINER_MultiPeerMap *tunnels;
237 * Default TTL for payload packets.
239 static unsigned long long default_ttl;
244 const static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
247 * Own ephemeral private key.
249 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
252 * Cached message used to perform a key exchange.
254 static struct GNUNET_MESH_KX_Ephemeral kx_msg;
257 * Task to generate a new ephemeral key.
259 static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
264 static struct GNUNET_TIME_Relative rekey_period;
266 /******************************************************************************/
267 /******************************** STATIC ***********************************/
268 /******************************************************************************/
271 * Get string description for tunnel connectivity state.
273 * @param cs Tunnel state.
275 * @return String representation.
278 cstate2s (enum MeshTunnel3CState cs)
280 static char buf[128];
284 case MESH_TUNNEL3_NEW:
285 return "MESH_TUNNEL3_NEW";
286 case MESH_TUNNEL3_SEARCHING:
287 return "MESH_TUNNEL3_SEARCHING";
288 case MESH_TUNNEL3_WAITING:
289 return "MESH_TUNNEL3_WAITING";
290 case MESH_TUNNEL3_READY:
291 return "MESH_TUNNEL3_READY";
294 sprintf (buf, "%u (UNKNOWN STATE)", cs);
302 * Get string description for tunnel encryption state.
304 * @param es Tunnel state.
306 * @return String representation.
309 estate2s (enum MeshTunnel3EState es)
311 static char buf[128];
315 case MESH_TUNNEL3_KEY_UNINITIALIZED:
316 return "MESH_TUNNEL3_KEY_UNINITIALIZED";
317 case MESH_TUNNEL3_KEY_SENT:
318 return "MESH_TUNNEL3_KEY_SENT";
319 case MESH_TUNNEL3_KEY_PING:
320 return "MESH_TUNNEL3_KEY_PING";
321 case MESH_TUNNEL3_KEY_OK:
322 return "MESH_TUNNEL3_KEY_OK";
325 sprintf (buf, "%u (UNKNOWN STATE)", es);
333 * @brief Check if tunnel is ready to send traffic.
335 * Tunnel must be connected and with encryption correctly set up.
337 * @param t Tunnel to check.
339 * @return #GNUNET_YES if ready, #GNUNET_NO otherwise
342 is_ready (struct MeshTunnel3 *t)
347 ready = (MESH_TUNNEL3_READY == t->cstate && MESH_TUNNEL3_KEY_OK == t->estate);
348 ready = ready || GMT_is_loopback (t);
354 * Ephemeral key message purpose size.
356 * @return Size of the part of the ephemeral key message that must be signed.
359 ephemeral_purpose_size (void)
361 return sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
362 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
363 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
364 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
365 sizeof (struct GNUNET_PeerIdentity);
370 * Size of the encrypted part of a ping message.
372 * @return Size of the encrypted part of a ping message.
375 ping_encryption_size (void)
377 return sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t);
382 * Get the channel's buffer. ONLY FOR NON-LOOPBACK CHANNELS!!
384 * @param tch Tunnel's channel handle.
386 * @return Amount of messages the channel can still buffer towards the client.
389 get_channel_buffer (const struct MeshTChannel *tch)
393 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
394 fwd = GMCH_is_origin (tch->ch, GNUNET_YES);
396 return GMCH_get_buffer (tch->ch, fwd);
401 * Get the channel's allowance status.
403 * @param tch Tunnel's channel handle.
405 * @return #GNUNET_YES if we allowed the client to send data to us.
408 get_channel_allowed (const struct MeshTChannel *tch)
412 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
413 fwd = GMCH_is_origin (tch->ch, GNUNET_YES);
415 return GMCH_get_allowed (tch->ch, fwd);
420 * Get the connection's buffer.
422 * @param tc Tunnel's connection handle.
424 * @return Amount of messages the connection can still buffer.
427 get_connection_buffer (const struct MeshTConnection *tc)
431 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
432 fwd = GMC_is_origin (tc->c, GNUNET_YES);
434 return GMC_get_buffer (tc->c, fwd);
439 * Get the connection's allowance.
441 * @param tc Tunnel's connection handle.
443 * @return Amount of messages we have allowed the next peer to send us.
446 get_connection_allowed (const struct MeshTConnection *tc)
450 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
451 fwd = GMC_is_origin (tc->c, GNUNET_YES);
453 return GMC_get_allowed (tc->c, fwd);
458 * Check that a ephemeral key message s well formed and correctly signed.
460 * @param t Tunnel on which the message came.
461 * @param msg The ephemeral key message.
463 * @return GNUNET_OK if message is fine, GNUNET_SYSERR otherwise.
466 check_ephemeral (struct MeshTunnel3 *t,
467 const struct GNUNET_MESH_KX_Ephemeral *msg)
469 /* Check message size */
470 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_KX_Ephemeral))
471 return GNUNET_SYSERR;
473 /* Check signature size */
474 if (ntohl (msg->purpose.size) != ephemeral_purpose_size ())
475 return GNUNET_SYSERR;
478 if (0 != memcmp (&msg->origin_identity,
479 GMP_get_id (t->peer),
480 sizeof (struct GNUNET_PeerIdentity)))
481 return GNUNET_SYSERR;
483 /* Check signature */
485 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_MESH_KX,
488 &msg->origin_identity.public_key))
489 return GNUNET_SYSERR;
496 * Encrypt data with the tunnel key.
498 * @param t Tunnel whose key to use.
499 * @param dst Destination for the encrypted data.
500 * @param src Source of the plaintext. Can overlap with @c dst.
501 * @param size Size of the plaintext.
502 * @param iv Initialization Vector to use.
505 t_encrypt (struct MeshTunnel3 *t,
506 void *dst, const void *src,
507 size_t size, uint32_t iv)
509 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
512 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt start\n");
513 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (iv), NULL);
514 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt IV derived\n");
515 out_size = GNUNET_CRYPTO_symmetric_encrypt (src, size, &t->e_key, &siv, dst);
516 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt end\n");
523 * Decrypt data with the tunnel key.
525 * @param t Tunnel whose key to use.
526 * @param dst Destination for the plaintext.
527 * @param src Source of the encrypted data. Can overlap with @c dst.
528 * @param size Size of the encrypted data.
529 * @param iv Initialization Vector to use.
532 t_decrypt (struct MeshTunnel3 *t,
533 void *dst, const void *src,
534 size_t size, uint32_t iv)
536 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
537 struct GNUNET_CRYPTO_SymmetricSessionKey *key;
540 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt start\n");
541 if (t->estate == MESH_TUNNEL3_KEY_OK || t->estate == MESH_TUNNEL3_KEY_PING)
545 else if (NULL != t->kx_ctx)
547 key = &t->kx_ctx->d_key_old;
551 GNUNET_STATISTICS_update (stats, "# non decryptable data", 1, GNUNET_NO);
552 LOG (GNUNET_ERROR_TYPE_DEBUG,
553 "WARNING got data on %s without a valid key\n",
559 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt iv\n");
560 GNUNET_CRYPTO_symmetric_derive_iv (&siv, key, &iv, sizeof (iv), NULL);
561 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt iv done\n");
562 out_size = GNUNET_CRYPTO_symmetric_decrypt (src, size, key, &siv, dst);
563 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt end\n");
570 * Create key material by doing ECDH on the local and remote ephemeral keys.
572 * @param key_material Where to store the key material.
573 * @param ephemeral_key Peer's public ephemeral key.
576 derive_key_material (struct GNUNET_HashCode *key_material,
577 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key)
580 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
589 * Create a symmetic key from the identities of both ends and the key material
592 * @param key Destination for the generated key.
593 * @param sender ID of the peer that will encrypt with @c key.
594 * @param receiver ID of the peer that will decrypt with @c key.
595 * @param key_material Hash created with ECDH with the ephemeral keys.
598 derive_symmertic (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
599 const struct GNUNET_PeerIdentity *sender,
600 const struct GNUNET_PeerIdentity *receiver,
601 const struct GNUNET_HashCode *key_material)
603 const char salt[] = "MESH kx salt";
605 GNUNET_CRYPTO_kdf (key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
607 key_material, sizeof (struct GNUNET_HashCode),
608 sender, sizeof (struct GNUNET_PeerIdentity),
609 receiver, sizeof (struct GNUNET_PeerIdentity),
614 * Pick a connection on which send the next data message.
616 * @param t Tunnel on which to send the message.
618 * @return The connection on which to send the next message.
620 static struct MeshConnection *
621 tunnel_get_connection (struct MeshTunnel3 *t)
623 struct MeshTConnection *iter;
624 struct MeshConnection *best;
626 unsigned int lowest_q;
628 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMP_2s (t->peer));
631 for (iter = t->connection_head; NULL != iter; iter = iter->next)
633 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
634 GMC_2s (iter->c), GMC_get_state (iter->c));
635 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
637 qn = GMC_get_qn (iter->c, GMC_is_origin (iter->c, GNUNET_YES));
638 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
646 LOG (GNUNET_ERROR_TYPE_DEBUG, " selected: connection %s\n", GMC_2s (best));
652 * Callback called when a queued message is sent.
654 * Calculates the average time and connection packet tracking.
656 * @param cls Closure (TunnelQueue handle).
657 * @param c Connection this message was on.
658 * @param q Connection queue handle (unused).
659 * @param type Type of message sent.
660 * @param fwd Was this a FWD going message?
661 * @param size Size of the message.
664 message_sent (void *cls,
665 struct MeshConnection *c,
666 struct MeshConnectionQueue *q,
667 uint16_t type, int fwd, size_t size)
669 struct MeshTunnel3Queue *qt = cls;
671 GNUNET_assert (NULL != qt->cont);
672 qt->cont (qt->cont_cls, GMC_get_tunnel (c), qt, type, size);
678 * Delete a queued message: either was sent or the channel was destroyed
679 * before the tunnel's key exchange had a chance to finish.
681 * @param tqd Delayed queue handle.
684 unqueue_data (struct MeshTunnelDelayed *tqd)
686 GNUNET_CONTAINER_DLL_remove (tqd->t->tq_head, tqd->t->tq_tail, tqd);
692 * Cache a message to be sent once tunnel is online.
694 * @param t Tunnel to hold the message.
695 * @param msg Message itself (copy will be made).
697 static struct MeshTunnelDelayed *
698 queue_data (struct MeshTunnel3 *t, const struct GNUNET_MessageHeader *msg)
700 struct MeshTunnelDelayed *tqd;
701 uint16_t size = ntohs (msg->size);
703 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue data on Tunnel %s\n", GMT_2s (t));
705 if (GNUNET_YES == is_ready (t))
711 tqd = GNUNET_malloc (sizeof (struct MeshTunnelDelayed) + size);
714 memcpy (&tqd[1], msg, size);
715 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tqd);
722 * Sends an already built message on a tunnel, encrypting it and
723 * choosing the best connection.
725 * @param message Message to send. Function modifies it.
726 * @param t Tunnel on which this message is transmitted.
727 * @param c Connection to use (autoselect if NULL).
728 * @param force Force the tunnel to take the message (buffer overfill).
729 * @param cont Continuation to call once message is really sent.
730 * @param cont_cls Closure for @c cont.
731 * @param existing_q In case this a transmission of previously queued data,
732 * this should be TunnelQueue given to the client.
735 * @return Handle to cancel message. NULL if @c cont is NULL.
737 static struct MeshTunnel3Queue *
738 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
739 struct MeshTunnel3 *t, struct MeshConnection *c,
740 int force, GMT_sent cont, void *cont_cls,
741 struct MeshTunnel3Queue *existing_q)
743 struct MeshTunnel3Queue *tq;
744 struct GNUNET_MESH_Encrypted *msg;
745 size_t size = ntohs (message->size);
746 char cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
751 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMT_2s (t));
753 if (GNUNET_NO == is_ready (t))
755 struct MeshTunnelDelayed *tqd;
756 /* A non null existing_q indicates sending of queued data.
757 * Should only happen after tunnel becomes ready.
759 GNUNET_assert (NULL == existing_q);
760 tqd = queue_data (t, message);
763 tq = GNUNET_new (struct MeshTunnel3Queue);
767 tq->cont_cls = cont_cls;
771 GNUNET_assert (GNUNET_NO == GMT_is_loopback (t));
773 iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
774 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
775 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
777 GNUNET_assert (t_encrypt (t, &msg[1], message, size, iv) == size);
778 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
780 c = tunnel_get_connection (t);
783 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task
784 || MESH_TUNNEL3_SEARCHING != t->cstate)
792 type = ntohs (message->type);
795 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
796 case GNUNET_MESSAGE_TYPE_MESH_DATA:
797 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
798 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
799 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
800 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
801 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
802 msg->cid = *GMC_get_id (c);
803 msg->ttl = htonl (default_ttl);
806 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
811 fwd = GMC_is_origin (c, GNUNET_YES);
815 (void) GMC_send_prebuilt_message (&msg->header, c, fwd, force, NULL, NULL);
818 if (NULL == existing_q)
820 tq = GNUNET_new (struct MeshTunnel3Queue); /* FIXME valgrind: leak*/
827 tq->cq = GMC_send_prebuilt_message (&msg->header, c, fwd, force,
830 tq->cont_cls = cont_cls;
837 * Send all cached messages that we can, tunnel is online.
839 * @param t Tunnel that holds the messages. Cannot be loopback.
842 send_queued_data (struct MeshTunnel3 *t)
844 struct MeshTunnelDelayed *tqd;
845 struct MeshTunnelDelayed *next;
848 LOG (GNUNET_ERROR_TYPE_DEBUG,
849 "GMT_send_queued_data on tunnel %s\n",
852 if (GMT_is_loopback (t))
858 if (GNUNET_NO == is_ready (t))
860 LOG (GNUNET_ERROR_TYPE_DEBUG, " not ready yet: %s/%s\n",
861 estate2s (t->estate), cstate2s (t->cstate));
865 room = GMT_get_connections_buffer (t);
866 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
867 LOG (GNUNET_ERROR_TYPE_DEBUG, " tq head: %p\n", t->tq_head);
868 for (tqd = t->tq_head; NULL != tqd && room > 0; tqd = next)
870 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending queued data\n");
873 send_prebuilt_message ((struct GNUNET_MessageHeader *) &tqd[1],
874 tqd->t, NULL, GNUNET_YES,
875 NULL != tqd->tq ? tqd->tq->cont : NULL,
876 NULL != tqd->tq ? tqd->tq->cont_cls : NULL,
880 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_send_queued_data end\n", GMP_2s (t->peer));
885 * Sends key exchange message on a tunnel, choosing the best connection.
886 * Should not be called on loopback tunnels.
888 * @param t Tunnel on which this message is transmitted.
889 * @param message Message to send. Function modifies it.
892 send_kx (struct MeshTunnel3 *t,
893 const struct GNUNET_MessageHeader *message)
895 struct MeshConnection *c;
896 struct GNUNET_MESH_KX *msg;
897 size_t size = ntohs (message->size);
898 char cbuf[sizeof (struct GNUNET_MESH_KX) + size];
902 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT KX on Tunnel %s\n", GMT_2s (t));
904 /* Avoid loopback. */
905 if (GMT_is_loopback (t))
907 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
912 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
914 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother\n");
918 /* Must have a connection. */
919 if (NULL == t->connection_head)
921 GNUNET_break (MESH_TUNNEL3_SEARCHING == t->cstate);
926 msg = (struct GNUNET_MESH_KX *) cbuf;
927 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX);
928 msg->header.size = htons (sizeof (struct GNUNET_MESH_KX) + size);
929 c = tunnel_get_connection (t);
932 GNUNET_break (GNUNET_SCHEDULER_NO_TASK != t->destroy_task
933 || MESH_TUNNEL3_READY != t->cstate);
937 type = ntohs (message->type);
940 case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
941 case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
942 case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
943 msg->reserved = htonl (0);
944 memcpy (&msg[1], message, size);
947 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
952 fwd = GMC_is_origin (t->connection_head->c, GNUNET_YES);
953 /* TODO save handle and cancel in case of a unneeded retransmission */
954 GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_YES, NULL, NULL);
959 * Send the ephemeral key on a tunnel.
961 * @param t Tunnel on which to send the key.
964 send_ephemeral (struct MeshTunnel3 *t)
966 LOG (GNUNET_ERROR_TYPE_INFO, "=> EPHM for %s\n", GMT_2s (t));
968 kx_msg.sender_status = htonl (t->estate);
969 send_kx (t, &kx_msg.header);
973 * Send a ping message on a tunnel.
975 * @param t Tunnel on which to send the ping.
978 send_ping (struct MeshTunnel3 *t)
980 struct GNUNET_MESH_KX_Ping msg;
982 LOG (GNUNET_ERROR_TYPE_INFO, "=> PING for %s\n", GMT_2s (t));
983 msg.header.size = htons (sizeof (msg));
984 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_PING);
985 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
986 msg.target = *GMP_get_id (t->peer);
987 msg.nonce = t->kx_ctx->challenge;
989 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
990 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n", GNUNET_i2s (&msg.target));
991 t_encrypt (t, &msg.target, &msg.target, ping_encryption_size(), msg.iv);
992 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
993 LOG (GNUNET_ERROR_TYPE_DEBUG, " e towards %s\n", GNUNET_i2s (&msg.target));
995 send_kx (t, &msg.header);
1000 * Send a pong message on a tunnel.
1002 * @param t Tunnel on which to send the pong.
1003 * @param challenge Value sent in the ping that we have to send back.
1006 send_pong (struct MeshTunnel3 *t, uint32_t challenge)
1008 struct GNUNET_MESH_KX_Pong msg;
1010 LOG (GNUNET_ERROR_TYPE_INFO, "=> PONG for %s\n", GMT_2s (t));
1011 msg.header.size = htons (sizeof (msg));
1012 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_PONG);
1013 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1014 msg.nonce = challenge;
1015 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
1016 t_encrypt (t, &msg.nonce, &msg.nonce, sizeof (msg.nonce), msg.iv);
1017 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
1019 send_kx (t, &msg.header);
1024 * Initiate a rekey with the remote peer.
1026 * @param cls Closure (tunnel).
1027 * @param tc TaskContext.
1030 rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1032 struct MeshTunnel3 *t = cls;
1034 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
1036 LOG (GNUNET_ERROR_TYPE_DEBUG, "Re-key Tunnel %s\n", GMT_2s (t));
1037 if (NULL != tc && 0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1040 if (NULL == t->kx_ctx)
1042 LOG (GNUNET_ERROR_TYPE_DEBUG, " new kx ctx\n");
1043 t->kx_ctx = GNUNET_new (struct MeshTunnelKXCtx);
1044 t->kx_ctx->challenge =
1045 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1046 t->kx_ctx->d_key_old = t->d_key;
1047 LOG (GNUNET_ERROR_TYPE_DEBUG, " new challenge for %s: %u\n",
1048 GMT_2s (t), t->kx_ctx->challenge);
1053 case MESH_TUNNEL3_KEY_UNINITIALIZED:
1054 t->estate = MESH_TUNNEL3_KEY_SENT;
1056 case MESH_TUNNEL3_KEY_SENT:
1058 case MESH_TUNNEL3_KEY_PING:
1059 case MESH_TUNNEL3_KEY_OK:
1061 t->estate = MESH_TUNNEL3_KEY_PING;
1064 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unexpected state %u\n", t->estate);
1067 LOG (GNUNET_ERROR_TYPE_DEBUG, " next call in %s\n",
1068 GNUNET_STRINGS_relative_time_to_string (REKEY_WAIT, GNUNET_YES));
1069 t->rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_WAIT, &rekey_tunnel, t);
1074 * Out ephemeral key has changed, create new session key on all tunnels.
1076 * @param cls Closure (size of the hashmap).
1077 * @param key Current public key.
1078 * @param value Value in the hash map (tunnel).
1080 * @return #GNUNET_YES, so we should continue to iterate,
1083 rekey_iterator (void *cls,
1084 const struct GNUNET_PeerIdentity *key,
1087 struct MeshTunnel3 *t = value;
1088 struct GNUNET_TIME_Relative delay;
1089 long n = (long) cls;
1092 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
1095 if (GNUNET_YES == GMT_is_loopback (t))
1098 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t) n * 100);
1099 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, r);
1100 t->rekey_task = GNUNET_SCHEDULER_add_delayed (delay, &rekey_tunnel, t);
1107 * Create a new ephemeral key and key message, schedule next rekeying.
1109 * @param cls Closure (unused).
1110 * @param tc TaskContext.
1113 rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1115 struct GNUNET_TIME_Absolute time;
1118 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1120 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1123 GNUNET_free_non_null (my_ephemeral_key);
1124 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1126 time = GNUNET_TIME_absolute_get ();
1127 kx_msg.creation_time = GNUNET_TIME_absolute_hton (time);
1128 time = GNUNET_TIME_absolute_add (time, rekey_period);
1129 time = GNUNET_TIME_absolute_add (time, GNUNET_TIME_UNIT_MINUTES);
1130 kx_msg.expiration_time = GNUNET_TIME_absolute_hton (time);
1131 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key, &kx_msg.ephemeral_key);
1133 GNUNET_assert (GNUNET_OK ==
1134 GNUNET_CRYPTO_eddsa_sign (my_private_key,
1136 &kx_msg.signature));
1138 n = (long) GNUNET_CONTAINER_multipeermap_size (tunnels);
1139 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &rekey_iterator, (void *) n);
1141 rekey_task = GNUNET_SCHEDULER_add_delayed (rekey_period, &rekey, NULL);
1146 * Called only on shutdown, destroy every tunnel.
1148 * @param cls Closure (unused).
1149 * @param key Current public key.
1150 * @param value Value in the hash map (tunnel).
1152 * @return #GNUNET_YES, so we should continue to iterate,
1155 destroy_iterator (void *cls,
1156 const struct GNUNET_PeerIdentity *key,
1159 struct MeshTunnel3 *t = value;
1167 * Notify remote peer that we don't know a channel he is talking about,
1168 * probably CHANNEL_DESTROY was missed.
1170 * @param t Tunnel on which to notify.
1171 * @param gid ID of the channel.
1174 send_channel_destroy (struct MeshTunnel3 *t, unsigned int gid)
1176 struct GNUNET_MESH_ChannelManage msg;
1178 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1179 msg.header.size = htons (sizeof (msg));
1180 msg.chid = htonl (gid);
1182 LOG (GNUNET_ERROR_TYPE_DEBUG,
1183 "WARNING destroying unknown channel %u on tunnel %s\n",
1185 send_prebuilt_message (&msg.header, t, NULL, GNUNET_YES, NULL, NULL, NULL);
1190 * Demultiplex data per channel and call appropriate channel handler.
1192 * @param t Tunnel on which the data came.
1193 * @param msg Data message.
1194 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1195 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1196 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1197 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1200 handle_data (struct MeshTunnel3 *t,
1201 const struct GNUNET_MESH_Data *msg,
1204 struct MeshChannel *ch;
1208 size = ntohs (msg->header.size);
1210 sizeof (struct GNUNET_MESH_Data) +
1211 sizeof (struct GNUNET_MessageHeader))
1216 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
1217 GM_m2s (ntohs (msg[1].header.type)));
1220 ch = GMT_get_channel (t, ntohl (msg->chid));
1223 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
1225 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel 0x%X unknown\n",
1227 send_channel_destroy (t, ntohl (msg->chid));
1231 GMCH_handle_data (ch, msg, fwd);
1236 * Demultiplex data ACKs per channel and update appropriate channel buffer info.
1238 * @param t Tunnel on which the DATA ACK came.
1239 * @param msg DATA ACK message.
1240 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1241 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1242 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1243 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1246 handle_data_ack (struct MeshTunnel3 *t,
1247 const struct GNUNET_MESH_DataACK *msg,
1250 struct MeshChannel *ch;
1254 size = ntohs (msg->header.size);
1255 if (size != sizeof (struct GNUNET_MESH_DataACK))
1262 ch = GMT_get_channel (t, ntohl (msg->chid));
1265 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
1267 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1272 GMCH_handle_data_ack (ch, msg, fwd);
1277 * Handle channel create.
1279 * @param t Tunnel on which the data came.
1280 * @param msg Data message.
1283 handle_ch_create (struct MeshTunnel3 *t,
1284 const struct GNUNET_MESH_ChannelCreate *msg)
1286 struct MeshChannel *ch;
1290 size = ntohs (msg->header.size);
1291 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
1298 ch = GMT_get_channel (t, ntohl (msg->chid));
1299 if (NULL != ch && ! GMT_is_loopback (t))
1301 /* Probably a retransmission, safe to ignore */
1302 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
1306 ch = GMCH_handle_create (t, msg);
1309 GMT_add_channel (t, ch);
1315 * Handle channel NACK: check correctness and call channel handler for NACKs.
1317 * @param t Tunnel on which the NACK came.
1318 * @param msg NACK message.
1321 handle_ch_nack (struct MeshTunnel3 *t,
1322 const struct GNUNET_MESH_ChannelManage *msg)
1324 struct MeshChannel *ch;
1328 size = ntohs (msg->header.size);
1329 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
1336 ch = GMT_get_channel (t, ntohl (msg->chid));
1339 GNUNET_STATISTICS_update (stats, "# channel NACK on unknown channel",
1341 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1346 GMCH_handle_nack (ch);
1351 * Handle a CHANNEL ACK (SYNACK/ACK).
1353 * @param t Tunnel on which the CHANNEL ACK came.
1354 * @param msg CHANNEL ACK message.
1355 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1356 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1357 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1358 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1361 handle_ch_ack (struct MeshTunnel3 *t,
1362 const struct GNUNET_MESH_ChannelManage *msg,
1365 struct MeshChannel *ch;
1369 size = ntohs (msg->header.size);
1370 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
1377 ch = GMT_get_channel (t, ntohl (msg->chid));
1380 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
1382 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1387 GMCH_handle_ack (ch, msg, fwd);
1393 * Handle a channel destruction message.
1395 * @param t Tunnel on which the message came.
1396 * @param msg Channel destroy message.
1397 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1398 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1399 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1400 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1403 handle_ch_destroy (struct MeshTunnel3 *t,
1404 const struct GNUNET_MESH_ChannelManage *msg,
1407 struct MeshChannel *ch;
1411 size = ntohs (msg->header.size);
1412 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
1419 ch = GMT_get_channel (t, ntohl (msg->chid));
1422 /* Probably a retransmission, safe to ignore */
1426 GMCH_handle_destroy (ch, msg, fwd);
1431 * The peer's ephemeral key has changed: update the symmetrical keys.
1433 * @param t Tunnel this message came on.
1434 * @param msg Key eXchange message.
1437 handle_ephemeral (struct MeshTunnel3 *t,
1438 const struct GNUNET_MESH_KX_Ephemeral *msg)
1440 struct GNUNET_HashCode km;
1441 LOG (GNUNET_ERROR_TYPE_INFO, "<= EPHM for %s\n", GMT_2s (t));
1443 if (GNUNET_OK != check_ephemeral (t, msg))
1445 GNUNET_break_op (0);
1448 derive_key_material (&km, &msg->ephemeral_key);
1449 LOG (GNUNET_ERROR_TYPE_DEBUG, " km is %s\n", GNUNET_h2s (&km));
1450 derive_symmertic (&t->e_key, &my_full_id, GMP_get_id (t->peer), &km);
1451 derive_symmertic (&t->d_key, GMP_get_id (t->peer), &my_full_id, &km);
1452 if (MESH_TUNNEL3_KEY_SENT == t->estate)
1454 LOG (GNUNET_ERROR_TYPE_DEBUG, " our key was sent, send ping\n");
1456 t->estate = MESH_TUNNEL3_KEY_PING;
1462 * Peer wants to check our symmetrical keys by sending an encrypted challenge.
1463 * Answer with by retransmitting the challenge with the "opposite" key.
1465 * @param t Tunnel this message came on.
1466 * @param msg Key eXchange Ping message.
1469 handle_ping (struct MeshTunnel3 *t,
1470 const struct GNUNET_MESH_KX_Ping *msg)
1472 struct GNUNET_MESH_KX_Ping res;
1474 if (ntohs (msg->header.size) != sizeof (res))
1476 GNUNET_break_op (0);
1480 LOG (GNUNET_ERROR_TYPE_INFO, "<= PING for %s\n", GMT_2s (t));
1481 t_decrypt (t, &res.target, &msg->target, ping_encryption_size (), msg->iv);
1482 if (0 != memcmp (&my_full_id, &res.target, sizeof (my_full_id)))
1484 GNUNET_STATISTICS_update (stats, "# malformed PINGs", 1, GNUNET_NO);
1485 LOG (GNUNET_ERROR_TYPE_WARNING, " malformed PING\n");
1486 LOG (GNUNET_ERROR_TYPE_DEBUG, " e got %u\n", msg->nonce);
1487 LOG (GNUNET_ERROR_TYPE_DEBUG, " e towards %s\n", GNUNET_i2s (&msg->target));
1488 LOG (GNUNET_ERROR_TYPE_DEBUG, " got %u\n", res.nonce);
1489 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n", GNUNET_i2s (&res.target));
1493 send_pong (t, res.nonce);
1498 * Peer has answer to our challenge.
1499 * If answer is successful, consider the key exchange finished and clean
1500 * up all related state.
1502 * @param t Tunnel this message came on.
1503 * @param msg Key eXchange Pong message.
1506 handle_pong (struct MeshTunnel3 *t,
1507 const struct GNUNET_MESH_KX_Pong *msg)
1511 LOG (GNUNET_ERROR_TYPE_INFO, "<= PONG for %s\n", GMT_2s (t));
1512 if (GNUNET_SCHEDULER_NO_TASK == t->rekey_task)
1514 GNUNET_STATISTICS_update (stats, "# duplicate PONG messages", 1, GNUNET_NO);
1517 t_decrypt (t, &challenge, &msg->nonce, sizeof (uint32_t), msg->iv);
1519 if (challenge != t->kx_ctx->challenge)
1521 LOG (GNUNET_ERROR_TYPE_WARNING, "Wrong PONG challenge\n");
1522 LOG (GNUNET_ERROR_TYPE_DEBUG, "PONG: %u (e: %u). Expected: %u.\n",
1523 challenge, msg->nonce, t->kx_ctx->challenge);
1524 GNUNET_break_op (0);
1527 GNUNET_SCHEDULER_cancel (t->rekey_task);
1528 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
1529 GNUNET_free (t->kx_ctx);
1531 GMT_change_estate (t, MESH_TUNNEL3_KEY_OK);
1536 * Demultiplex by message type and call appropriate handler for a message
1537 * towards a channel of a local tunnel.
1539 * @param t Tunnel this message came on.
1540 * @param msgh Message header.
1541 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1542 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1543 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1544 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1547 handle_decrypted (struct MeshTunnel3 *t,
1548 const struct GNUNET_MessageHeader *msgh,
1553 type = ntohs (msgh->type);
1554 LOG (GNUNET_ERROR_TYPE_INFO, "<= %s on %s\n", GM_m2s (type), GMT_2s (t));
1558 case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
1559 /* Do nothing, connection aleady got updated. */
1562 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1563 /* Don't send hop ACK, wait for client to ACK */
1564 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
1567 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
1568 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
1571 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1572 handle_ch_create (t,
1573 (struct GNUNET_MESH_ChannelCreate *) msgh);
1576 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1578 (struct GNUNET_MESH_ChannelManage *) msgh);
1581 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1583 (struct GNUNET_MESH_ChannelManage *) msgh,
1587 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1588 handle_ch_destroy (t,
1589 (struct GNUNET_MESH_ChannelManage *) msgh,
1594 GNUNET_break_op (0);
1595 LOG (GNUNET_ERROR_TYPE_WARNING,
1596 "end-to-end message not known (%u)\n",
1597 ntohs (msgh->type));
1602 /******************************************************************************/
1603 /******************************** API ***********************************/
1604 /******************************************************************************/
1607 * Decrypt and demultiplex by message type. Call appropriate handler
1608 * for every message.
1610 * @param t Tunnel this message came on.
1611 * @param msg Encrypted message.
1614 GMT_handle_encrypted (struct MeshTunnel3 *t,
1615 const struct GNUNET_MESH_Encrypted *msg)
1617 size_t size = ntohs (msg->header.size);
1618 size_t payload_size = size - sizeof (struct GNUNET_MESH_Encrypted);
1619 size_t decrypted_size;
1620 char cbuf [payload_size];
1621 struct GNUNET_MessageHeader *msgh;
1624 decrypted_size = t_decrypt (t, cbuf, &msg[1], payload_size, msg->iv);
1626 while (off < decrypted_size)
1628 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1629 handle_decrypted (t, msgh, GNUNET_SYSERR);
1630 off += ntohs (msgh->size);
1636 * Demultiplex an encapsulated KX message by message type.
1638 * @param t Tunnel on which the message came.
1639 * @param message Payload of KX message.
1642 GMT_handle_kx (struct MeshTunnel3 *t,
1643 const struct GNUNET_MessageHeader *message)
1647 type = ntohs (message->type);
1648 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message received\n", type);
1651 case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
1652 handle_ephemeral (t, (struct GNUNET_MESH_KX_Ephemeral *) message);
1655 case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
1656 handle_ping (t, (struct GNUNET_MESH_KX_Ping *) message);
1659 case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
1660 handle_pong (t, (struct GNUNET_MESH_KX_Pong *) message);
1664 GNUNET_break_op (0);
1665 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message not known (%u)\n", type);
1671 * Initialize the tunnel subsystem.
1673 * @param c Configuration handle.
1674 * @param key ECC private key, to derive all other keys and do crypto.
1677 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
1678 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
1680 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1682 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
1685 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1686 "MESH", "DEFAULT_TTL", "USING DEFAULT");
1690 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REKEY_PERIOD",
1693 rekey_period = GNUNET_TIME_UNIT_DAYS;
1696 my_private_key = key;
1697 kx_msg.header.size = htons (sizeof (kx_msg));
1698 kx_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL);
1699 kx_msg.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MESH_KX);
1700 kx_msg.purpose.size = htonl (ephemeral_purpose_size ());
1701 kx_msg.origin_identity = my_full_id;
1702 rekey_task = GNUNET_SCHEDULER_add_now (&rekey, NULL);
1704 tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
1709 * Shut down the tunnel subsystem.
1714 if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
1716 GNUNET_SCHEDULER_cancel (rekey_task);
1717 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1719 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &destroy_iterator, NULL);
1720 GNUNET_CONTAINER_multipeermap_destroy (tunnels);
1727 * @param destination Peer this tunnel is towards.
1729 struct MeshTunnel3 *
1730 GMT_new (struct MeshPeer *destination)
1732 struct MeshTunnel3 *t;
1734 t = GNUNET_new (struct MeshTunnel3);
1736 t->peer = destination;
1739 GNUNET_CONTAINER_multipeermap_put (tunnels, GMP_get_id (destination), t,
1740 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1751 * Change the tunnel's connection state.
1753 * @param t Tunnel whose connection state to change.
1754 * @param cstate New connection state.
1757 GMT_change_cstate (struct MeshTunnel3* t, enum MeshTunnel3CState cstate)
1761 LOG (GNUNET_ERROR_TYPE_DEBUG,
1762 "Tunnel %s cstate was %s\n",
1763 GMP_2s (t->peer), cstate2s (t->cstate));
1764 LOG (GNUNET_ERROR_TYPE_DEBUG,
1765 "Tunnel %s cstate is now %s\n",
1766 GMP_2s (t->peer), cstate2s (cstate));
1767 if (myid != GMP_get_short_id (t->peer) &&
1768 MESH_TUNNEL3_READY != t->cstate &&
1769 MESH_TUNNEL3_READY == cstate)
1772 if (MESH_TUNNEL3_KEY_OK == t->estate)
1774 LOG (GNUNET_ERROR_TYPE_DEBUG, " triggered send queued data\n");
1775 send_queued_data (t);
1777 else if (MESH_TUNNEL3_KEY_UNINITIALIZED == t->estate)
1779 LOG (GNUNET_ERROR_TYPE_DEBUG, " triggered rekey\n");
1780 rekey_tunnel (t, NULL);
1785 if (MESH_TUNNEL3_READY == cstate && 3 <= GMT_count_connections (t))
1787 GMP_stop_search (t->peer);
1792 * Change the tunnel encryption state.
1794 * @param t Tunnel whose encryption state to change.
1795 * @param state New encryption state.
1798 GMT_change_estate (struct MeshTunnel3* t, enum MeshTunnel3EState state)
1802 LOG (GNUNET_ERROR_TYPE_DEBUG,
1803 "Tunnel %s estate was %s\n",
1804 GMP_2s (t->peer), estate2s (t->estate));
1805 LOG (GNUNET_ERROR_TYPE_DEBUG,
1806 "Tunnel %s estate is now %s\n",
1807 GMP_2s (t->peer), estate2s (state));
1808 if (myid != GMP_get_short_id (t->peer) &&
1809 MESH_TUNNEL3_KEY_OK != t->estate && MESH_TUNNEL3_KEY_OK == state)
1812 send_queued_data (t);
1820 * Add a connection to a tunnel.
1823 * @param c Connection.
1826 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
1828 struct MeshTConnection *aux;
1830 GNUNET_assert (NULL != c);
1832 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1836 aux = GNUNET_new (struct MeshTConnection);
1838 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
1843 * Mark a path as no longer valid for this tunnel: has been tried and failed.
1845 * @param t Tunnel to update.
1846 * @param path Invalid path to remove. Is destroyed after removal.
1849 GMT_remove_path (struct MeshTunnel3 *t, struct MeshPeerPath *path)
1851 GMP_remove_path (t->peer, path);
1856 * Remove a connection from a tunnel.
1859 * @param c Connection.
1862 GMT_remove_connection (struct MeshTunnel3 *t,
1863 struct MeshConnection *c)
1865 struct MeshTConnection *aux;
1866 struct MeshTConnection *next;
1868 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing connection %s from tunnel %s\n",
1869 GMC_2s (c), GMT_2s (t));
1870 for (aux = t->connection_head; aux != NULL; aux = next)
1875 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
1880 /* Start new connections if needed */
1881 if (NULL == t->connection_head
1882 && GNUNET_SCHEDULER_NO_TASK == t->destroy_task
1883 && MESH_TUNNEL3_SHUTDOWN != t->cstate
1884 && GNUNET_NO == shutting_down)
1886 LOG (GNUNET_ERROR_TYPE_DEBUG, " no more connections, getting new ones\n");
1887 GMP_connect (t->peer);
1888 t->cstate = MESH_TUNNEL3_SEARCHING;
1892 /* If not marked as ready, no change is needed */
1893 if (MESH_TUNNEL3_READY != t->cstate)
1896 /* Check if any connection is ready to maintaing cstate */
1897 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1898 if (MESH_CONNECTION_READY == GMC_get_state (aux->c))
1901 t->cstate = MESH_TUNNEL3_WAITING;
1906 * Add a channel to a tunnel.
1909 * @param ch Channel.
1912 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
1914 struct MeshTChannel *aux;
1916 GNUNET_assert (NULL != ch);
1918 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
1920 for (aux = t->channel_head; aux != NULL; aux = aux->next)
1922 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
1927 aux = GNUNET_new (struct MeshTChannel);
1929 LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
1930 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
1932 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
1934 GNUNET_SCHEDULER_cancel (t->destroy_task);
1935 t->destroy_task = GNUNET_SCHEDULER_NO_TASK;
1936 LOG (GNUNET_ERROR_TYPE_DEBUG, " undo destroy!\n");
1942 * Remove a channel from a tunnel.
1945 * @param ch Channel.
1948 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
1950 struct MeshTChannel *aux;
1952 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
1953 for (aux = t->channel_head; aux != NULL; aux = aux->next)
1957 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GMCH_2s (ch));
1958 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
1967 * Search for a channel by global ID.
1969 * @param t Tunnel containing the channel.
1970 * @param chid Public channel number.
1972 * @return channel handler, NULL if doesn't exist
1974 struct MeshChannel *
1975 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
1977 struct MeshTChannel *iter;
1982 for (iter = t->channel_head; NULL != iter; iter = iter->next)
1984 if (GMCH_get_id (iter->ch) == chid)
1988 return NULL == iter ? NULL : iter->ch;
1993 * @brief Destroy a tunnel and free all resources.
1995 * Should only be called a while after the tunnel has been marked as destroyed,
1996 * in case there is a new channel added to the same peer shortly after marking
1997 * the tunnel. This way we avoid a new public key handshake.
1999 * @param cls Closure (tunnel to destroy).
2000 * @param tc Task context.
2003 delayed_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2005 struct MeshTunnel3 *t = cls;
2006 struct MeshTConnection *iter;
2008 t->destroy_task = GNUNET_SCHEDULER_NO_TASK;
2009 t->cstate = MESH_TUNNEL3_SHUTDOWN;
2011 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2013 GMC_send_destroy (iter->c);
2020 * Tunnel is empty: destroy it.
2022 * Notifies all connections about the destruction.
2024 * @param t Tunnel to destroy.
2027 GMT_destroy_empty (struct MeshTunnel3 *t)
2029 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
2031 LOG (GNUNET_ERROR_TYPE_DEBUG,
2032 "Tunnel %s is already scheduled for destruction\n",
2035 /* should never happen, tunnel can only become empty once, and the
2036 * task identifier should be NO_TASK (cleaned when the tunnel was created
2037 * or became un-empty)
2042 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s empty: destroying scheduled\n",
2045 t->destroy_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
2046 &delayed_destroy, t);
2051 * Destroy tunnel if empty (no more channels).
2053 * @param t Tunnel to destroy if empty.
2056 GMT_destroy_if_empty (struct MeshTunnel3 *t)
2058 if (1 < GMT_count_channels (t))
2061 GMT_destroy_empty (t);
2066 * Destroy the tunnel.
2068 * This function does not generate any warning traffic to clients or peers.
2071 * Cancel messages belonging to this tunnel queued to neighbors.
2072 * Free any allocated resources linked to the tunnel.
2074 * @param t The tunnel to destroy.
2077 GMT_destroy (struct MeshTunnel3 *t)
2079 struct MeshTConnection *iter_c;
2080 struct MeshTConnection *next_c;
2081 struct MeshTChannel *iter_ch;
2082 struct MeshTChannel *next_ch;
2087 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
2089 GNUNET_SCHEDULER_cancel (t->destroy_task);
2090 t->destroy_task = GNUNET_SCHEDULER_NO_TASK;
2093 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
2095 GNUNET_break (GNUNET_YES ==
2096 GNUNET_CONTAINER_multipeermap_remove (tunnels,
2097 GMP_get_id (t->peer), t));
2099 for (iter_c = t->connection_head; NULL != iter_c; iter_c = next_c)
2101 next_c = iter_c->next;
2102 GMC_destroy (iter_c->c);
2104 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = next_ch)
2106 next_ch = iter_ch->next;
2107 GMCH_destroy (iter_ch->ch);
2108 /* Should only happen on shutdown, but it's ok. */
2111 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2112 GMP_set_tunnel (t->peer, NULL);
2114 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
2116 GNUNET_SCHEDULER_cancel (t->rekey_task);
2117 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
2118 if (NULL != t->kx_ctx)
2119 GNUNET_free (t->kx_ctx);
2129 * @brief Use the given path for the tunnel.
2130 * Update the next and prev hops (and RCs).
2131 * (Re)start the path refresh in case the tunnel is locally owned.
2133 * @param t Tunnel to update.
2134 * @param p Path to use.
2136 * @return Connection created.
2138 struct MeshConnection *
2139 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
2141 struct MeshConnection *c;
2142 struct GNUNET_HashCode cid;
2143 unsigned int own_pos;
2145 if (NULL == t || NULL == p)
2151 if (MESH_TUNNEL3_SHUTDOWN == t->cstate)
2157 for (own_pos = 0; own_pos < p->length; own_pos++)
2159 if (p->peers[own_pos] == myid)
2162 if (own_pos > p->length - 1)
2164 GNUNET_break_op (0);
2168 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
2169 c = GMC_new (&cid, t, p, own_pos);
2172 /* Path was flawed */
2175 GMT_add_connection (t, c);
2181 * Count established (ready) connections of a tunnel.
2183 * @param t Tunnel on which to count.
2185 * @return Number of connections.
2188 GMT_count_connections (struct MeshTunnel3 *t)
2190 struct MeshTConnection *iter;
2193 for (count = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
2194 if (MESH_CONNECTION_DESTROYED != GMC_get_state (iter->c))
2201 * Count channels of a tunnel.
2203 * @param t Tunnel on which to count.
2205 * @return Number of channels.
2208 GMT_count_channels (struct MeshTunnel3 *t)
2210 struct MeshTChannel *iter;
2213 for (count = 0, iter = t->channel_head;
2215 iter = iter->next, count++) /* skip */;
2222 * Get the connectivity state of a tunnel.
2226 * @return Tunnel's connectivity state.
2228 enum MeshTunnel3CState
2229 GMT_get_cstate (struct MeshTunnel3 *t)
2234 return (enum MeshTunnel3CState) -1;
2241 * Get the encryption state of a tunnel.
2245 * @return Tunnel's encryption state.
2247 enum MeshTunnel3EState
2248 GMT_get_estate (struct MeshTunnel3 *t)
2253 return (enum MeshTunnel3EState) -1;
2259 * Get the maximum buffer space for a tunnel towards a local client.
2263 * @return Biggest buffer space offered by any channel in the tunnel.
2266 GMT_get_channels_buffer (struct MeshTunnel3 *t)
2268 struct MeshTChannel *iter;
2269 unsigned int buffer;
2270 unsigned int ch_buf;
2272 if (NULL == t->channel_head)
2274 /* Probably getting buffer for a channel create/handshake. */
2279 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2281 ch_buf = get_channel_buffer (iter);
2282 if (ch_buf > buffer)
2290 * Get the total buffer space for a tunnel for P2P traffic.
2294 * @return Buffer space offered by all connections in the tunnel.
2297 GMT_get_connections_buffer (struct MeshTunnel3 *t)
2299 struct MeshTConnection *iter;
2300 unsigned int buffer;
2303 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2305 if (GMC_get_state (iter->c) != MESH_CONNECTION_READY)
2309 buffer += get_connection_buffer (iter);
2317 * Get the tunnel's destination.
2321 * @return ID of the destination peer.
2323 const struct GNUNET_PeerIdentity *
2324 GMT_get_destination (struct MeshTunnel3 *t)
2326 return GMP_get_id (t->peer);
2331 * Get the tunnel's next free global channel ID.
2335 * @return GID of a channel free to use.
2338 GMT_get_next_chid (struct MeshTunnel3 *t)
2340 MESH_ChannelNumber chid;
2341 MESH_ChannelNumber mask;
2344 /* Set bit 30 depending on the ID relationship. Bit 31 is always 0 for GID.
2345 * If our ID is bigger or loopback tunnel, start at 0, bit 30 = 0
2346 * If peer's ID is bigger, start at 0x4... bit 30 = 1
2348 result = GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GMP_get_id (t->peer));
2354 while (NULL != GMT_get_channel (t, t->next_chid))
2356 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
2357 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
2358 t->next_chid |= mask;
2360 chid = t->next_chid;
2361 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
2362 t->next_chid |= mask;
2369 * Send ACK on one or more channels due to buffer in connections.
2371 * @param t Channel which has some free buffer space.
2374 GMT_unchoke_channels (struct MeshTunnel3 *t)
2376 struct MeshTChannel *iter;
2377 unsigned int buffer;
2378 unsigned int channels = GMT_count_channels (t);
2379 unsigned int choked_n;
2380 struct MeshChannel *choked[channels];
2382 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_unchoke_channels on %s\n", GMT_2s (t));
2383 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
2384 if (NULL != t->channel_head)
2385 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
2387 /* Get buffer space */
2388 buffer = GMT_get_connections_buffer (t);
2394 /* Count and remember choked channels */
2396 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2398 if (GNUNET_NO == get_channel_allowed (iter))
2400 choked[choked_n++] = iter->ch;
2404 /* Unchoke random channels */
2405 while (0 < buffer && 0 < choked_n)
2407 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2409 GMCH_allow_client (choked[r], GMCH_is_origin (choked[r], GNUNET_YES));
2412 choked[r] = choked[choked_n];
2418 * Send ACK on one or more connections due to buffer space to the client.
2420 * Iterates all connections of the tunnel and sends ACKs appropriately.
2425 GMT_send_connection_acks (struct MeshTunnel3 *t)
2427 struct MeshTConnection *iter;
2430 uint32_t allow_per_connection;
2432 unsigned int buffer;
2434 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel send connection ACKs on %s\n",
2443 buffer = GMT_get_channels_buffer (t);
2444 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer %u\n", buffer);
2446 /* Count connections, how many messages are already allowed */
2447 cs = GMT_count_connections (t);
2448 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
2450 allowed += get_connection_allowed (iter);
2452 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowed %u\n", allowed);
2454 /* Make sure there is no overflow */
2455 if (allowed > buffer)
2460 /* Authorize connections to send more data */
2461 to_allow = buffer; /* - allowed; */
2463 for (iter = t->connection_head;
2464 NULL != iter && to_allow > 0;
2467 allow_per_connection = to_allow/cs;
2468 to_allow -= allow_per_connection;
2470 if (get_connection_allowed (iter) > 64 / 3)
2474 GMC_allow (iter->c, allow_per_connection,
2475 GMC_is_origin (iter->c, GNUNET_NO));
2478 GNUNET_break (to_allow == 0);
2483 * Cancel a previously sent message while it's in the queue.
2485 * ONLY can be called before the continuation given to the send function
2486 * is called. Once the continuation is called, the message is no longer in the
2489 * @param q Handle to the queue.
2492 GMT_cancel (struct MeshTunnel3Queue *q)
2497 /* message_sent() will be called and free q */
2499 else if (NULL != q->tqd)
2501 unqueue_data (q->tqd);
2503 if (NULL != q->cont)
2504 q->cont (q->cont_cls, NULL, q, 0, 0);
2515 * Sends an already built message on a tunnel, encrypting it and
2516 * choosing the best connection if not provided.
2518 * @param message Message to send. Function modifies it.
2519 * @param t Tunnel on which this message is transmitted.
2520 * @param c Connection to use (autoselect if NULL).
2521 * @param force Force the tunnel to take the message (buffer overfill).
2522 * @param cont Continuation to call once message is really sent.
2523 * @param cont_cls Closure for @c cont.
2525 * @return Handle to cancel message. NULL if @c cont is NULL.
2527 struct MeshTunnel3Queue *
2528 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2529 struct MeshTunnel3 *t, struct MeshConnection *c,
2530 int force, GMT_sent cont, void *cont_cls)
2532 return send_prebuilt_message (message, t, c, force, cont, cont_cls, NULL);
2537 * Is the tunnel directed towards the local peer?
2541 * @return #GNUNET_YES if it is loopback.
2544 GMT_is_loopback (const struct MeshTunnel3 *t)
2546 return (myid == GMP_get_short_id (t->peer));
2551 * Is the tunnel this path already?
2556 * @return #GNUNET_YES a connection uses this path.
2559 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p)
2561 struct MeshTConnection *iter;
2563 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2564 if (GMC_get_path (iter->c) == p)
2572 * Get a cost of a path for a tunnel considering existing connections.
2575 * @param path Candidate path.
2577 * @return Cost of the path (path length + number of overlapping nodes)
2580 GMT_get_path_cost (const struct MeshTunnel3 *t,
2581 const struct MeshPeerPath *path)
2583 struct MeshTConnection *iter;
2584 const struct MeshPeerPath *aux;
2585 unsigned int overlap;
2593 GNUNET_assert (NULL != t);
2595 for (i = 0; i < path->length; i++)
2597 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2599 aux = GMC_get_path (iter->c);
2603 for (j = 0; j < aux->length; j++)
2605 if (path->peers[i] == aux->peers[j])
2613 return (path->length + overlap) * (path->score * -1);
2618 * Get the static string for the peer this tunnel is directed.
2622 * @return Static string the destination peer's ID.
2625 GMT_2s (const struct MeshTunnel3 *t)
2630 return GMP_2s (t->peer);
2634 /******************************************************************************/
2635 /***************************** INFO/DEBUG *******************************/
2636 /******************************************************************************/
2640 * Log all possible info about the tunnel state to stderr.
2642 * @param t Tunnel to debug.
2645 GMT_debug (const struct MeshTunnel3 *t)
2647 struct MeshTChannel *iterch;
2648 struct MeshTConnection *iterc;
2650 LOG (GNUNET_ERROR_TYPE_DEBUG, "DEBUG TUNNEL TOWARDS %s\n", GMT_2s (t));
2651 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate %s, estate %s\n",
2652 cstate2s (t->cstate), estate2s (t->estate));
2653 LOG (GNUNET_ERROR_TYPE_DEBUG, " kx_ctx %p, rekey_task %u\n",
2654 t->kx_ctx, t->rekey_task);
2655 LOG (GNUNET_ERROR_TYPE_DEBUG, " tq_head %p, tq_tail %p\n",
2656 t->tq_head, t->tq_tail);
2657 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy %u\n", t->destroy_task);
2659 LOG (GNUNET_ERROR_TYPE_DEBUG, " channels:\n");
2660 for (iterch = t->channel_head; NULL != iterch; iterch = iterch->next)
2662 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %s\n", GMCH_2s (iterch->ch));
2665 LOG (GNUNET_ERROR_TYPE_DEBUG, " connections:\n");
2666 for (iterc = t->connection_head; NULL != iterc; iterc = iterc->next)
2668 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %s [%u] buf: %u/%u (qn %u/%u)\n",
2669 GMC_2s (iterc->c), GMC_get_state (iterc->c),
2670 GMC_get_buffer (iterc->c, GNUNET_YES),
2671 GMC_get_buffer (iterc->c, GNUNET_NO),
2672 GMC_get_qn (iterc->c, GNUNET_YES),
2673 GMC_get_qn (iterc->c, GNUNET_NO));
2676 LOG (GNUNET_ERROR_TYPE_DEBUG, "DEBUG TUNNEL END\n");
2681 * Iterate all tunnels.
2683 * @param iter Iterator.
2684 * @param cls Closure for @c iter.
2687 GMT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2689 GNUNET_CONTAINER_multipeermap_iterate (tunnels, iter, cls);
2694 * Count all tunnels.
2696 * @return Number of tunnels to remote peers kept by this peer.
2699 GMT_count_all (void)
2701 return GNUNET_CONTAINER_multipeermap_size (tunnels);
2706 * Iterate all connections of a tunnel.
2708 * @param t Tunnel whose connections to iterate.
2709 * @param iter Iterator.
2710 * @param cls Closure for @c iter.
2713 GMT_iterate_connections (struct MeshTunnel3 *t, GMT_conn_iter iter, void *cls)
2715 struct MeshTConnection *ct;
2717 for (ct = t->connection_head; NULL != ct; ct = ct->next)
2723 * Iterate all channels of a tunnel.
2725 * @param t Tunnel whose channels to iterate.
2726 * @param iter Iterator.
2727 * @param cls Closure for @c iter.
2730 GMT_iterate_channels (struct MeshTunnel3 *t, GMT_chan_iter iter, void *cls)
2732 struct MeshTChannel *cht;
2734 for (cht = t->channel_head; NULL != cht; cht = cht->next)
2735 iter (cls, cht->ch);