3 This file is part of GNUnet.
4 Copyright (C) 2013, 2017 GNUnet e.V.
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
23 * @file cadet/gnunet-service-cadet-new_tunnels.c
24 * @brief Information we track per tunnel.
25 * @author Bartlomiej Polot
26 * @author Christian Grothoff
29 * - when managing connections, distinguish those that
30 * have (recently) had traffic from those that were
31 * never ready (or not recently)
32 * - implement sending and receiving KX messages
33 * - implement processing of incoming decrypted plaintext messages
34 * - clean up KX logic!
37 #include "gnunet_util_lib.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_signatures.h"
40 #include "cadet_protocol.h"
41 #include "cadet_path.h"
42 #include "gnunet-service-cadet-new.h"
43 #include "gnunet-service-cadet-new_channel.h"
44 #include "gnunet-service-cadet-new_connection.h"
45 #include "gnunet-service-cadet-new_tunnels.h"
46 #include "gnunet-service-cadet-new_peer.h"
47 #include "gnunet-service-cadet-new_paths.h"
50 #define LOG(level, ...) GNUNET_log_from(level,"cadet-tun",__VA_ARGS__)
54 * How long do we wait until tearing down an idle tunnel?
56 #define IDLE_DESTROY_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
59 * Yuck, replace by 'offsetof' expression?
62 #define AX_HEADER_SIZE (sizeof (uint32_t) * 2\
63 + sizeof (struct GNUNET_CRYPTO_EcdhePublicKey))
67 * Maximum number of skipped keys we keep in memory per tunnel.
69 #define MAX_SKIPPED_KEYS 64
72 * Maximum number of keys (and thus ratchet steps) we are willing to
73 * skip before we decide this is either a bogus packet or a DoS-attempt.
75 #define MAX_KEY_GAP 256
79 * Struct to old keys for skipped messages while advancing the Axolotl ratchet.
81 struct CadetTunnelSkippedKey
86 struct CadetTunnelSkippedKey *next;
91 struct CadetTunnelSkippedKey *prev;
94 * When was this key stored (for timeout).
96 struct GNUNET_TIME_Absolute timestamp;
101 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
106 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
109 * Key number for a given HK.
116 * Axolotl data, according to https://github.com/trevp/axolotl/wiki .
118 struct CadetTunnelAxolotl
121 * A (double linked) list of stored message keys and associated header keys
122 * for "skipped" messages, i.e. messages that have not been
123 * received despite the reception of more recent messages, (head).
125 struct CadetTunnelSkippedKey *skipped_head;
128 * Skipped messages' keys DLL, tail.
130 struct CadetTunnelSkippedKey *skipped_tail;
133 * 32-byte root key which gets updated by DH ratchet.
135 struct GNUNET_CRYPTO_SymmetricSessionKey RK;
138 * 32-byte header key (send).
140 struct GNUNET_CRYPTO_SymmetricSessionKey HKs;
143 * 32-byte header key (recv)
145 struct GNUNET_CRYPTO_SymmetricSessionKey HKr;
148 * 32-byte next header key (send).
150 struct GNUNET_CRYPTO_SymmetricSessionKey NHKs;
153 * 32-byte next header key (recv).
155 struct GNUNET_CRYPTO_SymmetricSessionKey NHKr;
158 * 32-byte chain keys (used for forward-secrecy updating, send).
160 struct GNUNET_CRYPTO_SymmetricSessionKey CKs;
163 * 32-byte chain keys (used for forward-secrecy updating, recv).
165 struct GNUNET_CRYPTO_SymmetricSessionKey CKr;
168 * ECDH for key exchange (A0 / B0).
170 struct GNUNET_CRYPTO_EcdhePrivateKey *kx_0;
173 * ECDH Ratchet key (send).
175 struct GNUNET_CRYPTO_EcdhePrivateKey *DHRs;
178 * ECDH Ratchet key (recv).
180 struct GNUNET_CRYPTO_EcdhePublicKey DHRr;
183 * When does this ratchet expire and a new one is triggered.
185 struct GNUNET_TIME_Absolute ratchet_expiration;
188 * Number of elements in @a skipped_head <-> @a skipped_tail.
190 unsigned int skipped;
193 * Message number (reset to 0 with each new ratchet, next message to send).
198 * Message number (reset to 0 with each new ratchet, next message to recv).
203 * Previous message numbers (# of msgs sent under prev ratchet)
208 * True (#GNUNET_YES) if we have to send a new ratchet key in next msg.
213 * Number of messages recieved since our last ratchet advance.
214 * - If this counter = 0, we cannot send a new ratchet key in next msg.
215 * - If this counter > 0, we can (but don't yet have to) send a new key.
217 unsigned int ratchet_allowed;
220 * Number of messages recieved since our last ratchet advance.
221 * - If this counter = 0, we cannot send a new ratchet key in next msg.
222 * - If this counter > 0, we can (but don't yet have to) send a new key.
224 unsigned int ratchet_counter;
230 * Struct used to save messages in a non-ready tunnel to send once connected.
232 struct CadetTunnelQueueEntry
235 * We are entries in a DLL
237 struct CadetTunnelQueueEntry *next;
240 * We are entries in a DLL
242 struct CadetTunnelQueueEntry *prev;
245 * Tunnel these messages belong in.
247 struct CadetTunnel *t;
250 * Continuation to call once sent (on the channel layer).
252 GNUNET_SCHEDULER_TaskCallback cont;
255 * Closure for @c cont.
260 * Envelope of message to send follows.
262 struct GNUNET_MQ_Envelope *env;
265 * Where to put the connection identifier into the payload
266 * of the message in @e env once we have it?
268 struct GNUNET_CADET_ConnectionTunnelIdentifier *cid;
273 * Struct containing all information regarding a tunnel to a peer.
278 * Destination of the tunnel.
280 struct CadetPeer *destination;
283 * Peer's ephemeral key, to recreate @c e_key and @c d_key when own
284 * ephemeral key changes.
286 struct GNUNET_CRYPTO_EcdhePublicKey peers_ephemeral_key;
289 * Encryption ("our") key. It is only "confirmed" if kx_ctx is NULL.
291 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
294 * Decryption ("their") key. It is only "confirmed" if kx_ctx is NULL.
296 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
301 struct CadetTunnelAxolotl ax;
304 * State of the tunnel connectivity.
306 enum CadetTunnelCState cstate;
309 * State of the tunnel encryption.
311 enum CadetTunnelEState estate;
314 * Task to start the rekey process.
316 struct GNUNET_SCHEDULER_Task *rekey_task;
319 * Tokenizer for decrypted messages.
321 struct GNUNET_MessageStreamTokenizer *mst;
324 * Dispatcher for decrypted messages only (do NOT use for sending!).
326 struct GNUNET_MQ_Handle *mq;
329 * DLL of connections that are actively used to reach the destination peer.
331 struct CadetTConnection *connection_head;
334 * DLL of connections that are actively used to reach the destination peer.
336 struct CadetTConnection *connection_tail;
339 * Channels inside this tunnel. Maps
340 * `struct GNUNET_CADET_ChannelTunnelNumber` to a `struct CadetChannel`.
342 struct GNUNET_CONTAINER_MultiHashMap32 *channels;
345 * Channel ID for the next created channel in this tunnel.
347 struct GNUNET_CADET_ChannelTunnelNumber next_chid;
350 * Queued messages, to transmit once tunnel gets connected.
352 struct CadetTunnelQueueEntry *tq_head;
355 * Queued messages, to transmit once tunnel gets connected.
357 struct CadetTunnelQueueEntry *tq_tail;
360 * Task scheduled if there are no more channels using the tunnel.
362 struct GNUNET_SCHEDULER_Task *destroy_task;
365 * Task to trim connections if too many are present.
367 struct GNUNET_SCHEDULER_Task *maintain_connections_task;
370 * Ephemeral message in the queue (to avoid queueing more than one).
372 struct CadetConnectionQueue *ephm_hKILL;
375 * Pong message in the queue.
377 struct CadetConnectionQueue *pong_hKILL;
380 * Number of connections in the @e connection_head DLL.
382 unsigned int num_connections;
385 * Number of entries in the @e tq_head DLL.
392 * Get the static string for the peer this tunnel is directed.
396 * @return Static string the destination peer's ID.
399 GCT_2s (const struct CadetTunnel *t)
406 GNUNET_snprintf (buf,
409 GCP_2s (t->destination));
415 * Return the peer to which this tunnel goes.
418 * @return the destination of the tunnel
421 GCT_get_destination (struct CadetTunnel *t)
423 return t->destination;
428 * Count channels of a tunnel.
430 * @param t Tunnel on which to count.
432 * @return Number of channels.
435 GCT_count_channels (struct CadetTunnel *t)
437 return GNUNET_CONTAINER_multihashmap32_size (t->channels);
442 * Lookup a channel by its @a chid.
444 * @param t tunnel to look in
445 * @param chid number of channel to find
446 * @return NULL if channel does not exist
448 struct CadetChannel *
449 lookup_channel (struct CadetTunnel *t,
450 struct GNUNET_CADET_ChannelTunnelNumber chid)
452 return GNUNET_CONTAINER_multihashmap32_get (t->channels,
458 * Count all created connections of a tunnel. Not necessarily ready connections!
460 * @param t Tunnel on which to count.
462 * @return Number of connections created, either being established or ready.
465 GCT_count_any_connections (struct CadetTunnel *t)
467 return t->num_connections;
472 * Get the connectivity state of a tunnel.
476 * @return Tunnel's connectivity state.
478 enum CadetTunnelCState
479 GCT_get_cstate (struct CadetTunnel *t)
486 * Get the encryption state of a tunnel.
490 * @return Tunnel's encryption state.
492 enum CadetTunnelEState
493 GCT_get_estate (struct CadetTunnel *t)
500 * Create a new Axolotl ephemeral (ratchet) key.
505 new_ephemeral (struct CadetTunnel *t)
507 GNUNET_free_non_null (t->ax.DHRs);
508 t->ax.DHRs = GNUNET_CRYPTO_ecdhe_key_create ();
512 /* ************************************** start core crypto ***************************** */
518 * @param plaintext Content to HMAC.
519 * @param size Size of @c plaintext.
520 * @param iv Initialization vector for the message.
521 * @param key Key to use.
522 * @param hmac[out] Destination to store the HMAC.
525 t_hmac (const void *plaintext,
528 const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
529 struct GNUNET_ShortHashCode *hmac)
531 static const char ctx[] = "cadet authentication key";
532 struct GNUNET_CRYPTO_AuthKey auth_key;
533 struct GNUNET_HashCode hash;
535 GNUNET_CRYPTO_hmac_derive_key (&auth_key,
541 /* Two step: CADET_Hash is only 256 bits, HashCode is 512. */
542 GNUNET_CRYPTO_hmac (&auth_key,
555 * @param key Key to use.
556 * @param hash[out] Resulting HMAC.
557 * @param source Source key material (data to HMAC).
558 * @param len Length of @a source.
561 t_ax_hmac_hash (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
562 struct GNUNET_HashCode *hash,
566 static const char ctx[] = "axolotl HMAC-HASH";
567 struct GNUNET_CRYPTO_AuthKey auth_key;
569 GNUNET_CRYPTO_hmac_derive_key (&auth_key,
573 GNUNET_CRYPTO_hmac (&auth_key,
581 * Derive a symmetric encryption key from an HMAC-HASH.
583 * @param key Key to use for the HMAC.
584 * @param[out] out Key to generate.
585 * @param source Source key material (data to HMAC).
586 * @param len Length of @a source.
589 t_hmac_derive_key (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
590 struct GNUNET_CRYPTO_SymmetricSessionKey *out,
594 static const char ctx[] = "axolotl derive key";
595 struct GNUNET_HashCode h;
601 GNUNET_CRYPTO_kdf (out, sizeof (*out),
609 * Encrypt data with the axolotl tunnel key.
611 * @param t Tunnel whose key to use.
612 * @param dst Destination with @a size bytes for the encrypted data.
613 * @param src Source of the plaintext. Can overlap with @c dst, must contain @a size bytes
614 * @param size Size of the buffers at @a src and @a dst
617 t_ax_encrypt (struct CadetTunnel *t,
622 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
623 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
624 struct CadetTunnelAxolotl *ax;
628 ax->ratchet_counter++;
629 if ( (GNUNET_YES == ax->ratchet_allowed) &&
630 ( (ratchet_messages <= ax->ratchet_counter) ||
631 (0 == GNUNET_TIME_absolute_get_remaining (ax->ratchet_expiration).rel_value_us)) )
633 ax->ratchet_flag = GNUNET_YES;
635 if (GNUNET_YES == ax->ratchet_flag)
637 /* Advance ratchet */
638 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3];
639 struct GNUNET_HashCode dh;
640 struct GNUNET_HashCode hmac;
641 static const char ctx[] = "axolotl ratchet";
646 /* RK, NHKs, CKs = KDF( HMAC-HASH(RK, DH(DHRs, DHRr)) ) */
647 GNUNET_CRYPTO_ecc_ecdh (ax->DHRs,
650 t_ax_hmac_hash (&ax->RK,
654 GNUNET_CRYPTO_kdf (keys, sizeof (keys),
656 &hmac, sizeof (hmac),
664 ax->ratchet_flag = GNUNET_NO;
665 ax->ratchet_allowed = GNUNET_NO;
666 ax->ratchet_counter = 0;
667 ax->ratchet_expiration
668 = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
672 t_hmac_derive_key (&ax->CKs,
676 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
681 out_size = GNUNET_CRYPTO_symmetric_encrypt (src,
686 GNUNET_assert (size == out_size);
687 t_hmac_derive_key (&ax->CKs,
695 * Decrypt data with the axolotl tunnel key.
697 * @param t Tunnel whose key to use.
698 * @param dst Destination for the decrypted data, must contain @a size bytes.
699 * @param src Source of the ciphertext. Can overlap with @c dst, must contain @a size bytes.
700 * @param size Size of the @a src and @a dst buffers
703 t_ax_decrypt (struct CadetTunnel *t,
708 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
709 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
710 struct CadetTunnelAxolotl *ax;
714 t_hmac_derive_key (&ax->CKr,
718 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
722 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
723 out_size = GNUNET_CRYPTO_symmetric_decrypt (src,
728 GNUNET_assert (out_size == size);
729 t_hmac_derive_key (&ax->CKr,
737 * Encrypt header with the axolotl header key.
739 * @param t Tunnel whose key to use.
740 * @param msg Message whose header to encrypt.
743 t_h_encrypt (struct CadetTunnel *t,
744 struct GNUNET_CADET_TunnelEncryptedMessage *msg)
746 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
747 struct CadetTunnelAxolotl *ax;
751 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
755 out_size = GNUNET_CRYPTO_symmetric_encrypt (&msg->Ns,
760 GNUNET_assert (AX_HEADER_SIZE == out_size);
765 * Decrypt header with the current axolotl header key.
767 * @param t Tunnel whose current ax HK to use.
768 * @param src Message whose header to decrypt.
769 * @param dst Where to decrypt header to.
772 t_h_decrypt (struct CadetTunnel *t,
773 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
774 struct GNUNET_CADET_TunnelEncryptedMessage *dst)
776 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
777 struct CadetTunnelAxolotl *ax;
781 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
785 out_size = GNUNET_CRYPTO_symmetric_decrypt (&src->Ns,
790 GNUNET_assert (AX_HEADER_SIZE == out_size);
795 * Delete a key from the list of skipped keys.
797 * @param t Tunnel to delete from.
798 * @param key Key to delete.
801 delete_skipped_key (struct CadetTunnel *t,
802 struct CadetTunnelSkippedKey *key)
804 GNUNET_CONTAINER_DLL_remove (t->ax.skipped_head,
813 * Decrypt and verify data with the appropriate tunnel key and verify that the
814 * data has not been altered since it was sent by the remote peer.
816 * @param t Tunnel whose key to use.
817 * @param dst Destination for the plaintext.
818 * @param src Source of the message. Can overlap with @c dst.
819 * @param size Size of the message.
820 * @return Size of the decrypted data, -1 if an error was encountered.
823 try_old_ax_keys (struct CadetTunnel *t,
825 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
828 struct CadetTunnelSkippedKey *key;
829 struct GNUNET_ShortHashCode *hmac;
830 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
831 struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
832 struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK;
838 LOG (GNUNET_ERROR_TYPE_DEBUG,
839 "Trying skipped keys\n");
840 hmac = &plaintext_header.hmac;
841 esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
843 /* Find a correct Header Key */
845 for (key = t->ax.skipped_head; NULL != key; key = key->next)
848 AX_HEADER_SIZE + esize,
852 if (0 == memcmp (hmac,
863 /* Should've been checked in -cadet_connection.c handle_cadet_encrypted. */
864 GNUNET_assert (size > sizeof (struct GNUNET_CADET_TunnelEncryptedMessage));
865 len = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
866 GNUNET_assert (len >= sizeof (struct GNUNET_MessageHeader));
869 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
873 res = GNUNET_CRYPTO_symmetric_decrypt (&src->Ns,
877 &plaintext_header.Ns);
878 GNUNET_assert (AX_HEADER_SIZE == res);
880 /* Find the correct message key */
881 N = ntohl (plaintext_header.Ns);
882 while ( (NULL != key) &&
885 if ( (NULL == key) ||
886 (0 != memcmp (&key->HK,
888 sizeof (*valid_HK))) )
891 /* Decrypt payload */
892 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
897 res = GNUNET_CRYPTO_symmetric_decrypt (&src[1],
902 delete_skipped_key (t,
909 * Delete a key from the list of skipped keys.
911 * @param t Tunnel to delete from.
912 * @param HKr Header Key to use.
915 store_skipped_key (struct CadetTunnel *t,
916 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr)
918 struct CadetTunnelSkippedKey *key;
920 key = GNUNET_new (struct CadetTunnelSkippedKey);
921 key->timestamp = GNUNET_TIME_absolute_get ();
924 t_hmac_derive_key (&t->ax.CKr,
928 t_hmac_derive_key (&t->ax.CKr,
932 GNUNET_CONTAINER_DLL_insert (t->ax.skipped_head,
941 * Stage skipped AX keys and calculate the message key.
942 * Stores each HK and MK for skipped messages.
944 * @param t Tunnel where to stage the keys.
945 * @param HKr Header key.
946 * @param Np Received meesage number.
947 * @return #GNUNET_OK if keys were stored.
948 * #GNUNET_SYSERR if an error ocurred (Np not expected).
951 store_ax_keys (struct CadetTunnel *t,
952 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr,
958 LOG (GNUNET_ERROR_TYPE_DEBUG,
959 "Storing skipped keys [%u, %u)\n",
962 if (MAX_KEY_GAP < gap)
964 /* Avoid DoS (forcing peer to do 2^33 chain HMAC operations) */
965 /* TODO: start new key exchange on return */
967 LOG (GNUNET_ERROR_TYPE_WARNING,
968 "Got message %u, expected %u+\n",
971 return GNUNET_SYSERR;
975 /* Delayed message: don't store keys, flag to try old keys. */
976 return GNUNET_SYSERR;
979 while (t->ax.Nr < Np)
980 store_skipped_key (t,
983 while (t->ax.skipped > MAX_SKIPPED_KEYS)
984 delete_skipped_key (t,
991 * Decrypt and verify data with the appropriate tunnel key and verify that the
992 * data has not been altered since it was sent by the remote peer.
994 * @param t Tunnel whose key to use.
995 * @param dst Destination for the plaintext.
996 * @param src Source of the message. Can overlap with @c dst.
997 * @param size Size of the message.
998 * @return Size of the decrypted data, -1 if an error was encountered.
1001 t_ax_decrypt_and_validate (struct CadetTunnel *t,
1003 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
1006 struct CadetTunnelAxolotl *ax;
1007 struct GNUNET_ShortHashCode msg_hmac;
1008 struct GNUNET_HashCode hmac;
1009 struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
1012 size_t esize; /* Size of encryped payload */
1014 esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
1017 /* Try current HK */
1019 AX_HEADER_SIZE + esize,
1022 if (0 != memcmp (&msg_hmac,
1026 static const char ctx[] = "axolotl ratchet";
1027 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3]; /* RKp, NHKp, CKp */
1028 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
1029 struct GNUNET_HashCode dh;
1030 struct GNUNET_CRYPTO_EcdhePublicKey *DHRp;
1034 AX_HEADER_SIZE + esize,
1038 if (0 != memcmp (&msg_hmac,
1042 /* Try the skipped keys, if that fails, we're out of luck. */
1043 return try_old_ax_keys (t,
1053 Np = ntohl (plaintext_header.Ns);
1054 PNp = ntohl (plaintext_header.PNs);
1055 DHRp = &plaintext_header.DHRs;
1060 /* RKp, NHKp, CKp = KDF (HMAC-HASH (RK, DH (DHRp, DHRs))) */
1061 GNUNET_CRYPTO_ecc_ecdh (ax->DHRs,
1064 t_ax_hmac_hash (&ax->RK,
1067 GNUNET_CRYPTO_kdf (keys, sizeof (keys),
1069 &hmac, sizeof (hmac),
1072 /* Commit "purported" keys */
1078 ax->ratchet_allowed = GNUNET_YES;
1085 Np = ntohl (plaintext_header.Ns);
1086 PNp = ntohl (plaintext_header.PNs);
1088 if ( (Np != ax->Nr) &&
1089 (GNUNET_OK != store_ax_keys (t,
1093 /* Try the skipped keys, if that fails, we're out of luck. */
1094 return try_old_ax_keys (t,
1110 * Send a KX message.
1112 * FIXME: does not take care of sender-authentication yet!
1114 * @param t Tunnel on which to send it.
1115 * @param force_reply Force the other peer to reply with a KX message.
1118 send_kx (struct CadetTunnel *t,
1121 struct CadetTunnelAxolotl *ax = &t->ax;
1122 struct CadetConnection *c;
1123 struct GNUNET_MQ_Envelope *env;
1124 struct GNUNET_CADET_TunnelKeyExchangeMessage *msg;
1125 enum GNUNET_CADET_KX_Flags flags;
1128 if (NULL != t->ephm_h)
1130 LOG (GNUNET_ERROR_TYPE_INFO,
1131 " already queued, nop\n");
1135 c = NULL; // FIXME: figure out where to transmit...
1137 // GNUNET_assert (GNUNET_NO == GCT_is_loopback (t));
1138 env = GNUNET_MQ_msg (msg,
1139 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX);
1140 flags = GNUNET_CADET_KX_FLAG_NONE;
1141 if (GNUNET_YES == force_reply)
1142 flags |= GNUNET_CADET_KX_FLAG_FORCE_REPLY;
1143 msg->flags = htonl (flags);
1144 msg->cid = *GCC_get_id (c);
1145 GNUNET_CRYPTO_ecdhe_key_get_public (ax->kx_0,
1146 &msg->ephemeral_key);
1147 GNUNET_CRYPTO_ecdhe_key_get_public (ax->DHRs,
1150 // FIXME: send 'env'.
1152 t->ephm_h = GCC_send_prebuilt_message (&msg.header,
1156 GCC_is_origin (c, GNUNET_YES),
1157 GNUNET_YES, &ephm_sent, t);
1158 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
1159 GCT_change_estate (t, CADET_TUNNEL_KEY_SENT);
1165 * Handle KX message.
1167 * FIXME: sender-authentication in KX is missing!
1169 * @param ct connection/tunnel combo that received encrypted message
1170 * @param msg the key exchange message
1173 GCT_handle_kx (struct CadetTConnection *ct,
1174 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
1176 struct CadetTunnel *t = ct->t;
1177 struct CadetTunnelAxolotl *ax = &t->ax;
1178 struct GNUNET_HashCode key_material[3];
1179 struct GNUNET_CRYPTO_SymmetricSessionKey keys[5];
1180 const char salt[] = "CADET Axolotl salt";
1181 const struct GNUNET_PeerIdentity *pid;
1184 pid = GCP_get_id (t->destination);
1185 if (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id,
1187 am_I_alice = GNUNET_YES;
1188 else if (0 < GNUNET_CRYPTO_cmp_peer_identity (&my_full_id,
1190 am_I_alice = GNUNET_NO;
1193 GNUNET_break_op (0);
1197 if (0 != (GNUNET_CADET_KX_FLAG_FORCE_REPLY & ntohl (msg->flags)))
1199 if (NULL != t->rekey_task)
1201 GNUNET_SCHEDULER_cancel (t->rekey_task);
1202 t->rekey_task = NULL;
1208 if (0 == memcmp (&ax->DHRr,
1210 sizeof (msg->ratchet_key)))
1212 LOG (GNUNET_ERROR_TYPE_INFO,
1213 " known ratchet key, exit\n");
1217 ax->DHRr = msg->ratchet_key;
1220 if (GNUNET_YES == am_I_alice)
1222 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, /* A */
1223 &msg->ephemeral_key, /* B0 */
1228 GNUNET_CRYPTO_ecdh_eddsa (ax->kx_0, /* B0 */
1229 &pid->public_key, /* A */
1234 if (GNUNET_YES == am_I_alice)
1236 GNUNET_CRYPTO_ecdh_eddsa (ax->kx_0, /* A0 */
1237 &pid->public_key, /* B */
1242 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, /* A */
1243 &msg->ephemeral_key, /* B0 */
1250 /* (This is the triple-DH, we could probably safely skip this,
1251 as A0/B0 are already in the key material.) */
1252 GNUNET_CRYPTO_ecc_ecdh (ax->kx_0, /* A0 or B0 */
1253 &msg->ephemeral_key, /* B0 or A0 */
1257 GNUNET_CRYPTO_kdf (keys, sizeof (keys),
1258 salt, sizeof (salt),
1259 &key_material, sizeof (key_material),
1262 if (0 == memcmp (&ax->RK,
1266 LOG (GNUNET_ERROR_TYPE_INFO,
1267 " known handshake key, exit\n");
1271 if (GNUNET_YES == am_I_alice)
1277 ax->ratchet_flag = GNUNET_YES;
1285 ax->ratchet_flag = GNUNET_NO;
1286 ax->ratchet_allowed = GNUNET_NO;
1287 ax->ratchet_counter = 0;
1288 ax->ratchet_expiration
1289 = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
1297 /* After KX is done, update state machine and begin transmissions... */
1298 GCT_change_estate (t,
1299 CADET_TUNNEL_KEY_PING);
1300 send_queued_data (t);
1305 /* ************************************** end core crypto ***************************** */
1309 * Add a channel to a tunnel.
1313 * @return unique number identifying @a ch within @a t
1315 struct GNUNET_CADET_ChannelTunnelNumber
1316 GCT_add_channel (struct CadetTunnel *t,
1317 struct CadetChannel *ch)
1319 struct GNUNET_CADET_ChannelTunnelNumber ret;
1322 chid = ntohl (t->next_chid.cn);
1324 GNUNET_CONTAINER_multihashmap32_get (t->channels,
1327 GNUNET_assert (GNUNET_YES ==
1328 GNUNET_CONTAINER_multihashmap32_put (t->channels,
1331 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1332 t->next_chid.cn = htonl (chid + 1);
1333 ret.cn = htonl (chid);
1339 * This tunnel is no longer used, destroy it.
1341 * @param cls the idle tunnel
1344 destroy_tunnel (void *cls)
1346 struct CadetTunnel *t = cls;
1347 struct CadetTConnection *ct;
1348 struct CadetTunnelQueueEntry *tqe;
1350 t->destroy_task = NULL;
1351 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (t->channels));
1352 while (NULL != (ct = t->connection_head))
1354 GNUNET_assert (ct->t == t);
1355 GNUNET_CONTAINER_DLL_remove (t->connection_head,
1358 GCC_destroy (ct->cc);
1361 while (NULL != (tqe = t->tq_head))
1363 GNUNET_CONTAINER_DLL_remove (t->tq_head,
1366 GNUNET_MQ_discard (tqe->env);
1369 GCP_drop_tunnel (t->destination,
1371 GNUNET_CONTAINER_multihashmap32_destroy (t->channels);
1372 if (NULL != t->maintain_connections_task)
1374 GNUNET_SCHEDULER_cancel (t->maintain_connections_task);
1375 t->maintain_connections_task = NULL;
1377 GNUNET_MST_destroy (t->mst);
1378 GNUNET_MQ_destroy (t->mq);
1384 * A connection is @a is_ready for transmission. Looks at our message
1385 * queue and if there is a message, sends it out via the connection.
1387 * @param cls the `struct CadetTConnection` that is @a is_ready
1388 * @param is_ready #GNUNET_YES if connection are now ready,
1389 * #GNUNET_NO if connection are no longer ready
1392 connection_ready_cb (void *cls,
1395 struct CadetTConnection *ct = cls;
1396 struct CadetTunnel *t = ct->t;
1397 struct CadetTunnelQueueEntry *tq = t->tq_head;
1399 if (GNUNET_NO == ct->is_ready)
1401 ct->is_ready = GNUNET_NO;
1404 ct->is_ready = GNUNET_YES;
1406 return; /* no messages pending right now */
1408 /* ready to send message 'tq' on tunnel 'ct' */
1409 GNUNET_assert (t == tq->t);
1410 GNUNET_CONTAINER_DLL_remove (t->tq_head,
1413 if (NULL != tq->cid)
1414 *tq->cid = *GCC_get_id (ct->cc);
1415 ct->is_ready = GNUNET_NO;
1416 GCC_transmit (ct->cc,
1418 tq->cont (tq->cont_cls);
1424 * Called when either we have a new connection, or a new message in the
1425 * queue, or some existing connection has transmission capacity. Looks
1426 * at our message queue and if there is a message, picks a connection
1429 * FIXME: yuck... Need better selection logic!
1431 * @param t tunnel to process messages on
1434 trigger_transmissions (struct CadetTunnel *t)
1436 struct CadetTConnection *ct;
1438 if (NULL == t->tq_head)
1439 return; /* no messages pending right now */
1440 for (ct = t->connection_head;
1443 if (GNUNET_YES == ct->is_ready)
1446 return; /* no connections ready */
1448 /* FIXME: a bit hackish to do it like this... */
1449 connection_ready_cb (ct,
1455 * Function called to maintain the connections underlying our tunnel.
1456 * Tries to maintain (incl. tear down) connections for the tunnel, and
1457 * if there is a significant change, may trigger transmissions.
1459 * Basically, needs to check if there are connections that perform
1460 * badly, and if so eventually kill them and trigger a replacement.
1461 * The strategy is to open one more connection than
1462 * #DESIRED_CONNECTIONS_PER_TUNNEL, and then periodically kick out the
1463 * least-performing one, and then inquire for new ones.
1465 * @param cls the `struct CadetTunnel`
1468 maintain_connections_cb (void *cls)
1470 struct CadetTunnel *t = cls;
1472 GNUNET_break (0); // FIXME: implement!
1477 * Consider using the path @a p for the tunnel @a t.
1478 * The tunnel destination is at offset @a off in path @a p.
1480 * @param cls our tunnel
1481 * @param path a path to our destination
1482 * @param off offset of the destination on path @a path
1483 * @return #GNUNET_YES (should keep iterating)
1486 consider_path_cb (void *cls,
1487 struct CadetPeerPath *path,
1490 struct CadetTunnel *t = cls;
1491 unsigned int min_length = UINT_MAX;
1492 GNUNET_CONTAINER_HeapCostType max_desire = 0;
1493 struct CadetTConnection *ct;
1495 /* Check if we care about the new path. */
1496 for (ct = t->connection_head;
1500 struct CadetPeerPath *ps;
1502 ps = GCC_get_path (ct->cc);
1504 return GNUNET_YES; /* duplicate */
1505 min_length = GNUNET_MIN (min_length,
1506 GCPP_get_length (ps));
1507 max_desire = GNUNET_MAX (max_desire,
1508 GCPP_get_desirability (ps));
1511 /* FIXME: not sure we should really just count
1512 'num_connections' here, as they may all have
1513 consistently failed to connect. */
1515 /* We iterate by increasing path length; if we have enough paths and
1516 this one is more than twice as long than what we are currently
1517 using, then ignore all of these super-long ones! */
1518 if ( (t->num_connections > DESIRED_CONNECTIONS_PER_TUNNEL) &&
1519 (min_length * 2 < off) )
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522 "Ignoring paths of length %u, they are way too long.\n",
1526 /* If we have enough paths and this one looks no better, ignore it. */
1527 if ( (t->num_connections >= DESIRED_CONNECTIONS_PER_TUNNEL) &&
1528 (min_length < GCPP_get_length (path)) &&
1529 (max_desire > GCPP_get_desirability (path)) )
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "Ignoring path (%u/%llu) to %s, got something better already.\n",
1533 GCPP_get_length (path),
1534 (unsigned long long) GCPP_get_desirability (path),
1535 GCP_2s (t->destination));
1539 /* Path is interesting (better by some metric, or we don't have
1540 enough paths yet). */
1541 ct = GNUNET_new (struct CadetTConnection);
1542 ct->created = GNUNET_TIME_absolute_get ();
1544 ct->cc = GCC_create (t->destination,
1547 &connection_ready_cb,
1549 /* FIXME: schedule job to kill connection (and path?) if it takes
1550 too long to get ready! (And track performance data on how long
1551 other connections took with the tunnel!)
1552 => Note: to be done within 'connection'-logic! */
1553 GNUNET_CONTAINER_DLL_insert (t->connection_head,
1556 t->num_connections++;
1562 * Consider using the path @a p for the tunnel @a t.
1563 * The tunnel destination is at offset @a off in path @a p.
1565 * @param cls our tunnel
1566 * @param path a path to our destination
1567 * @param off offset of the destination on path @a path
1570 GCT_consider_path (struct CadetTunnel *t,
1571 struct CadetPeerPath *p,
1574 (void) consider_path_cb (t,
1583 * @param cls the `struct CadetTunnel` for which we decrypted the message
1584 * @param msg the message we received on the tunnel
1587 handle_plaintext_keepalive (void *cls,
1588 const struct GNUNET_MessageHeader *msg)
1590 struct CadetTunnel *t = cls;
1591 GNUNET_break (0); // FIXME
1596 * Check that @a msg is well-formed.
1598 * @param cls the `struct CadetTunnel` for which we decrypted the message
1599 * @param msg the message we received on the tunnel
1600 * @return #GNUNET_OK (any variable-size payload goes)
1603 check_plaintext_data (void *cls,
1604 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
1613 * @param cls the `struct CadetTunnel` for which we decrypted the message
1614 * @param msg the message we received on the tunnel
1617 handle_plaintext_data (void *cls,
1618 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
1620 struct CadetTunnel *t = cls;
1621 GNUNET_break (0); // FIXME!
1628 * @param cls the `struct CadetTunnel` for which we decrypted the message
1629 * @param ack the message we received on the tunnel
1632 handle_plaintext_data_ack (void *cls,
1633 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
1635 struct CadetTunnel *t = cls;
1636 GNUNET_break (0); // FIXME!
1643 * @param cls the `struct CadetTunnel` for which we decrypted the message
1644 * @param cc the message we received on the tunnel
1647 handle_plaintext_channel_create (void *cls,
1648 const struct GNUNET_CADET_ChannelOpenMessage *cc)
1650 struct CadetTunnel *t = cls;
1651 GNUNET_break (0); // FIXME!
1658 * @param cls the `struct CadetTunnel` for which we decrypted the message
1659 * @param cm the message we received on the tunnel
1662 handle_plaintext_channel_nack (void *cls,
1663 const struct GNUNET_CADET_ChannelManageMessage *cm)
1665 struct CadetTunnel *t = cls;
1666 GNUNET_break (0); // FIXME!
1673 * @param cls the `struct CadetTunnel` for which we decrypted the message
1674 * @param cm the message we received on the tunnel
1677 handle_plaintext_channel_ack (void *cls,
1678 const struct GNUNET_CADET_ChannelManageMessage *cm)
1680 struct CadetTunnel *t = cls;
1681 GNUNET_break (0); // FIXME!
1686 * We received a message saying that a channel should be destroyed.
1687 * Pass it on to the correct channel.
1689 * @param cls the `struct CadetTunnel` for which we decrypted the message
1690 * @param cm the message we received on the tunnel
1693 handle_plaintext_channel_destroy (void *cls,
1694 const struct GNUNET_CADET_ChannelManageMessage *cm)
1696 struct CadetTunnel *t = cls;
1697 struct CadetChannel *cc = lookup_channel (t,
1700 GCCH_channel_remote_destroy (cc);
1705 * Handles a message we decrypted, by injecting it into
1706 * our message queue (which will do the dispatching).
1708 * @param cls the `struct CadetTunnel` that got the message
1709 * @param msg the message
1710 * @return #GNUNET_OK (continue to process)
1713 handle_decrypted (void *cls,
1714 const struct GNUNET_MessageHeader *msg)
1716 struct CadetTunnel *t = cls;
1718 GNUNET_MQ_inject_message (t->mq,
1725 * Function called if we had an error processing
1726 * an incoming decrypted message.
1728 * @param cls the `struct CadetTunnel`
1729 * @param error error code
1732 decrypted_error_cb (void *cls,
1733 enum GNUNET_MQ_Error error)
1735 GNUNET_break_op (0);
1740 * Create a tunnel to @a destionation. Must only be called
1741 * from within #GCP_get_tunnel().
1743 * @param destination where to create the tunnel to
1744 * @return new tunnel to @a destination
1746 struct CadetTunnel *
1747 GCT_create_tunnel (struct CadetPeer *destination)
1749 struct GNUNET_MQ_MessageHandler handlers[] = {
1750 GNUNET_MQ_hd_fixed_size (plaintext_keepalive,
1751 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE,
1752 struct GNUNET_MessageHeader,
1754 GNUNET_MQ_hd_var_size (plaintext_data,
1755 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA,
1756 struct GNUNET_CADET_ChannelAppDataMessage,
1758 GNUNET_MQ_hd_fixed_size (plaintext_data_ack,
1759 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK,
1760 struct GNUNET_CADET_ChannelDataAckMessage,
1762 GNUNET_MQ_hd_fixed_size (plaintext_channel_create,
1763 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN,
1764 struct GNUNET_CADET_ChannelOpenMessage,
1766 GNUNET_MQ_hd_fixed_size (plaintext_channel_nack,
1767 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1768 struct GNUNET_CADET_ChannelManageMessage,
1770 GNUNET_MQ_hd_fixed_size (plaintext_channel_ack,
1771 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK,
1772 struct GNUNET_CADET_ChannelManageMessage,
1774 GNUNET_MQ_hd_fixed_size (plaintext_channel_destroy,
1775 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1776 struct GNUNET_CADET_ChannelManageMessage,
1778 GNUNET_MQ_handler_end ()
1780 struct CadetTunnel *t;
1782 t = GNUNET_new (struct CadetTunnel);
1783 t->destination = destination;
1784 t->channels = GNUNET_CONTAINER_multihashmap32_create (8);
1785 (void) GCP_iterate_paths (destination,
1788 t->maintain_connections_task
1789 = GNUNET_SCHEDULER_add_now (&maintain_connections_cb,
1791 t->mq = GNUNET_MQ_queue_for_callbacks (NULL,
1796 &decrypted_error_cb,
1798 t->mst = GNUNET_MST_create (&handle_decrypted,
1805 * Remove a channel from a tunnel.
1809 * @param gid unique number identifying @a ch within @a t
1812 GCT_remove_channel (struct CadetTunnel *t,
1813 struct CadetChannel *ch,
1814 struct GNUNET_CADET_ChannelTunnelNumber gid)
1816 GNUNET_assert (GNUNET_YES ==
1817 GNUNET_CONTAINER_multihashmap32_remove (t->channels,
1821 GNUNET_CONTAINER_multihashmap32_size (t->channels))
1823 t->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_DESTROY_DELAY,
1831 * Change the tunnel encryption state.
1832 * If the encryption state changes to OK, stop the rekey task.
1834 * @param t Tunnel whose encryption state to change, or NULL.
1835 * @param state New encryption state.
1838 GCT_change_estate (struct CadetTunnel *t,
1839 enum CadetTunnelEState state)
1841 enum CadetTunnelEState old = t->estate;
1844 LOG (GNUNET_ERROR_TYPE_DEBUG,
1845 "Tunnel %s estate changed from %d to %d\n",
1850 if ( (CADET_TUNNEL_KEY_OK != old) &&
1851 (CADET_TUNNEL_KEY_OK == t->estate) )
1853 if (NULL != t->rekey_task)
1855 GNUNET_SCHEDULER_cancel (t->rekey_task);
1856 t->rekey_task = NULL;
1859 /* Send queued data if tunnel is not loopback */
1860 if (myid != GCP_get_short_id (t->peer))
1861 send_queued_data (t);
1868 * Add a @a connection to the @a tunnel.
1871 * @param cid connection identifer to use for the connection
1872 * @param path path to use for the connection
1875 GCT_add_inbound_connection (struct CadetTunnel *t,
1876 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
1877 struct CadetPeerPath *path)
1879 struct CadetConnection *cc;
1880 struct CadetTConnection *ct;
1882 ct = GNUNET_new (struct CadetTConnection);
1883 ct->created = GNUNET_TIME_absolute_get ();
1885 ct->cc = GCC_create_inbound (t->destination,
1889 &connection_ready_cb,
1891 /* FIXME: schedule job to kill connection (and path?) if it takes
1892 too long to get ready! (And track performance data on how long
1893 other connections took with the tunnel!)
1894 => Note: to be done within 'connection'-logic! */
1895 GNUNET_CONTAINER_DLL_insert (t->connection_head,
1898 t->num_connections++;
1903 * Handle encrypted message.
1905 * @param ct connection/tunnel combo that received encrypted message
1906 * @param msg the encrypted message to decrypt
1909 GCT_handle_encrypted (struct CadetTConnection *ct,
1910 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
1912 struct CadetTunnel *t = ct->t;
1913 uint16_t size = ntohs (msg->header.size);
1914 char cbuf [size] GNUNET_ALIGN;
1915 ssize_t decrypted_size;
1917 GNUNET_STATISTICS_update (stats,
1918 "# received encrypted",
1922 decrypted_size = t_ax_decrypt_and_validate (t,
1927 if (-1 == decrypted_size)
1929 GNUNET_STATISTICS_update (stats,
1930 "# unable to decrypt",
1933 if (CADET_TUNNEL_KEY_PING <= t->estate)
1935 GNUNET_break_op (0);
1936 LOG (GNUNET_ERROR_TYPE_WARNING,
1937 "Wrong crypto, tunnel %s\n",
1940 GNUNET_ERROR_TYPE_WARNING);
1945 GCT_change_estate (t,
1946 CADET_TUNNEL_KEY_OK);
1947 /* The MST will ultimately call #handle_decrypted() on each message. */
1948 GNUNET_break_op (GNUNET_OK ==
1949 GNUNET_MST_from_buffer (t->mst,
1958 * Sends an already built message on a tunnel, encrypting it and
1959 * choosing the best connection if not provided.
1961 * @param message Message to send. Function modifies it.
1962 * @param t Tunnel on which this message is transmitted.
1963 * @param cont Continuation to call once message is really sent.
1964 * @param cont_cls Closure for @c cont.
1965 * @return Handle to cancel message. NULL if @c cont is NULL.
1967 struct CadetTunnelQueueEntry *
1968 GCT_send (struct CadetTunnel *t,
1969 const struct GNUNET_MessageHeader *message,
1970 GNUNET_SCHEDULER_TaskCallback cont,
1973 struct CadetTunnelQueueEntry *tq;
1974 uint16_t payload_size;
1975 struct GNUNET_MQ_Envelope *env;
1976 struct GNUNET_CADET_TunnelEncryptedMessage *ax_msg;
1978 /* FIXME: what about KX not yet being ready? (see "is_ready()" check in old code!) */
1980 payload_size = ntohs (message->size);
1981 env = GNUNET_MQ_msg_extra (ax_msg,
1983 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED);
1988 ax_msg->Ns = htonl (t->ax.Ns++);
1989 ax_msg->PNs = htonl (t->ax.PNs);
1990 GNUNET_CRYPTO_ecdhe_key_get_public (t->ax.DHRs,
1994 t_hmac (&ax_msg->Ns,
1995 AX_HEADER_SIZE + payload_size,
1999 // ax_msg->pid = htonl (GCC_get_pid (c, fwd)); // FIXME: connection flow-control not (re)implemented yet!
2001 tq = GNUNET_malloc (sizeof (*tq));
2004 tq->cid = &ax_msg->cid;
2006 tq->cont_cls = cont_cls;
2007 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head,
2010 trigger_transmissions (t);
2016 * Cancel a previously sent message while it's in the queue.
2018 * ONLY can be called before the continuation given to the send
2019 * function is called. Once the continuation is called, the message is
2020 * no longer in the queue!
2022 * @param q Handle to the queue entry to cancel.
2025 GCT_send_cancel (struct CadetTunnelQueueEntry *q)
2027 struct CadetTunnel *t = q->t;
2029 GNUNET_CONTAINER_DLL_remove (t->tq_head,
2037 * Iterate over all connections of a tunnel.
2039 * @param t Tunnel whose connections to iterate.
2040 * @param iter Iterator.
2041 * @param iter_cls Closure for @c iter.
2044 GCT_iterate_connections (struct CadetTunnel *t,
2045 GCT_ConnectionIterator iter,
2048 for (struct CadetTConnection *ct = t->connection_head;
2057 * Closure for #iterate_channels_cb.
2064 GCT_ChannelIterator iter;
2067 * Closure for @e iter.
2074 * Helper function for #GCT_iterate_channels.
2076 * @param cls the `struct ChanIterCls`
2078 * @param value a `struct CadetChannel`
2079 * @return #GNUNET_OK
2082 iterate_channels_cb (void *cls,
2086 struct ChanIterCls *ctx = cls;
2087 struct CadetChannel *ch = value;
2089 ctx->iter (ctx->iter_cls,
2096 * Iterate over all channels of a tunnel.
2098 * @param t Tunnel whose channels to iterate.
2099 * @param iter Iterator.
2100 * @param iter_cls Closure for @c iter.
2103 GCT_iterate_channels (struct CadetTunnel *t,
2104 GCT_ChannelIterator iter,
2107 struct ChanIterCls ctx;
2110 ctx.iter_cls = iter_cls;
2111 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
2112 &iterate_channels_cb,
2119 * Call #GCCH_debug() on a channel.
2121 * @param cls points to the log level to use
2123 * @param value the `struct CadetChannel` to dump
2124 * @return #GNUNET_OK (continue iteration)
2127 debug_channel (void *cls,
2131 const enum GNUNET_ErrorType *level = cls;
2132 struct CadetChannel *ch = value;
2134 GCCH_debug (ch, *level);
2140 * Get string description for tunnel connectivity state.
2142 * @param cs Tunnel state.
2144 * @return String representation.
2147 cstate2s (enum CadetTunnelCState cs)
2149 static char buf[32];
2153 case CADET_TUNNEL_NEW:
2154 return "CADET_TUNNEL_NEW";
2155 case CADET_TUNNEL_SEARCHING:
2156 return "CADET_TUNNEL_SEARCHING";
2157 case CADET_TUNNEL_WAITING:
2158 return "CADET_TUNNEL_WAITING";
2159 case CADET_TUNNEL_READY:
2160 return "CADET_TUNNEL_READY";
2161 case CADET_TUNNEL_SHUTDOWN:
2162 return "CADET_TUNNEL_SHUTDOWN";
2164 SPRINTF (buf, "%u (UNKNOWN STATE)", cs);
2171 * Get string description for tunnel encryption state.
2173 * @param es Tunnel state.
2175 * @return String representation.
2178 estate2s (enum CadetTunnelEState es)
2180 static char buf[32];
2184 case CADET_TUNNEL_KEY_UNINITIALIZED:
2185 return "CADET_TUNNEL_KEY_UNINITIALIZED";
2186 case CADET_TUNNEL_KEY_SENT:
2187 return "CADET_TUNNEL_KEY_SENT";
2188 case CADET_TUNNEL_KEY_PING:
2189 return "CADET_TUNNEL_KEY_PING";
2190 case CADET_TUNNEL_KEY_OK:
2191 return "CADET_TUNNEL_KEY_OK";
2192 case CADET_TUNNEL_KEY_REKEY:
2193 return "CADET_TUNNEL_KEY_REKEY";
2195 SPRINTF (buf, "%u (UNKNOWN STATE)", es);
2201 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-tun",__VA_ARGS__)
2205 * Log all possible info about the tunnel state.
2207 * @param t Tunnel to debug.
2208 * @param level Debug level to use.
2211 GCT_debug (const struct CadetTunnel *t,
2212 enum GNUNET_ErrorType level)
2214 struct CadetTConnection *iter_c;
2217 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
2219 __FILE__, __FUNCTION__, __LINE__);
2224 "TTT TUNNEL TOWARDS %s in cstate %s, estate %s tq_len: %u #cons: %u\n",
2226 cstate2s (t->cstate),
2227 estate2s (t->estate),
2229 t->num_connections);
2230 #if DUMP_KEYS_TO_STDERR
2231 ax_debug (t->ax, level);
2235 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
2239 "TTT connections:\n");
2240 for (iter_c = t->connection_head; NULL != iter_c; iter_c = iter_c->next)
2241 GCC_debug (iter_c->cc,
2245 "TTT TUNNEL END\n");
2249 /* end of gnunet-service-cadet-new_tunnels.c */