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 * Entry in list of connections used by tunnel, with metadata.
232 struct CadetTConnection
237 struct CadetTConnection *next;
242 struct CadetTConnection *prev;
247 struct CadetConnection *cc;
250 * Tunnel this connection belongs to.
252 struct CadetTunnel *t;
255 * Creation time, to keep oldest connection alive.
257 struct GNUNET_TIME_Absolute created;
260 * Connection throughput, to keep fastest connection alive.
267 * Struct used to save messages in a non-ready tunnel to send once connected.
269 struct CadetTunnelQueueEntry
272 * We are entries in a DLL
274 struct CadetTunnelQueueEntry *next;
277 * We are entries in a DLL
279 struct CadetTunnelQueueEntry *prev;
282 * Tunnel these messages belong in.
284 struct CadetTunnel *t;
287 * Continuation to call once sent (on the channel layer).
289 GNUNET_SCHEDULER_TaskCallback cont;
292 * Closure for @c cont.
297 * Envelope of message to send follows.
299 struct GNUNET_MQ_Envelope *env;
302 * Where to put the connection identifier into the payload
303 * of the message in @e env once we have it?
305 struct GNUNET_CADET_ConnectionTunnelIdentifier *cid;
310 * Struct containing all information regarding a tunnel to a peer.
315 * Destination of the tunnel.
317 struct CadetPeer *destination;
320 * Peer's ephemeral key, to recreate @c e_key and @c d_key when own
321 * ephemeral key changes.
323 struct GNUNET_CRYPTO_EcdhePublicKey peers_ephemeral_key;
326 * Encryption ("our") key. It is only "confirmed" if kx_ctx is NULL.
328 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
331 * Decryption ("their") key. It is only "confirmed" if kx_ctx is NULL.
333 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
338 struct CadetTunnelAxolotl ax;
341 * State of the tunnel connectivity.
343 enum CadetTunnelCState cstate;
346 * State of the tunnel encryption.
348 enum CadetTunnelEState estate;
351 * Task to start the rekey process.
353 struct GNUNET_SCHEDULER_Task *rekey_task;
356 * Tokenizer for decrypted messages.
358 struct GNUNET_MessageStreamTokenizer *mst;
361 * Dispatcher for decrypted messages only (do NOT use for sending!).
363 struct GNUNET_MQ_Handle *mq;
366 * DLL of connections that are actively used to reach the destination peer.
368 struct CadetTConnection *connection_head;
371 * DLL of connections that are actively used to reach the destination peer.
373 struct CadetTConnection *connection_tail;
376 * Channels inside this tunnel. Maps
377 * `struct GNUNET_CADET_ChannelTunnelNumber` to a `struct CadetChannel`.
379 struct GNUNET_CONTAINER_MultiHashMap32 *channels;
382 * Channel ID for the next created channel in this tunnel.
384 struct GNUNET_CADET_ChannelTunnelNumber next_chid;
387 * Queued messages, to transmit once tunnel gets connected.
389 struct CadetTunnelQueueEntry *tq_head;
392 * Queued messages, to transmit once tunnel gets connected.
394 struct CadetTunnelQueueEntry *tq_tail;
397 * Task scheduled if there are no more channels using the tunnel.
399 struct GNUNET_SCHEDULER_Task *destroy_task;
402 * Task to trim connections if too many are present.
404 struct GNUNET_SCHEDULER_Task *maintain_connections_task;
407 * Ephemeral message in the queue (to avoid queueing more than one).
409 struct CadetConnectionQueue *ephm_hKILL;
412 * Pong message in the queue.
414 struct CadetConnectionQueue *pong_hKILL;
417 * Number of connections in the @e connection_head DLL.
419 unsigned int num_connections;
422 * Number of entries in the @e tq_head DLL.
429 * Get the static string for the peer this tunnel is directed.
433 * @return Static string the destination peer's ID.
436 GCT_2s (const struct CadetTunnel *t)
443 GNUNET_snprintf (buf,
446 GCP_2s (t->destination));
452 * Return the peer to which this tunnel goes.
455 * @return the destination of the tunnel
458 GCT_get_destination (struct CadetTunnel *t)
460 return t->destination;
465 * Count channels of a tunnel.
467 * @param t Tunnel on which to count.
469 * @return Number of channels.
472 GCT_count_channels (struct CadetTunnel *t)
474 return GNUNET_CONTAINER_multihashmap32_size (t->channels);
479 * Lookup a channel by its @a chid.
481 * @param t tunnel to look in
482 * @param chid number of channel to find
483 * @return NULL if channel does not exist
485 struct CadetChannel *
486 lookup_channel (struct CadetTunnel *t,
487 struct GNUNET_CADET_ChannelTunnelNumber chid)
489 return GNUNET_CONTAINER_multihashmap32_get (t->channels,
495 * Count all created connections of a tunnel. Not necessarily ready connections!
497 * @param t Tunnel on which to count.
499 * @return Number of connections created, either being established or ready.
502 GCT_count_any_connections (struct CadetTunnel *t)
504 return t->num_connections;
509 * Get the connectivity state of a tunnel.
513 * @return Tunnel's connectivity state.
515 enum CadetTunnelCState
516 GCT_get_cstate (struct CadetTunnel *t)
523 * Get the encryption state of a tunnel.
527 * @return Tunnel's encryption state.
529 enum CadetTunnelEState
530 GCT_get_estate (struct CadetTunnel *t)
537 * Create a new Axolotl ephemeral (ratchet) key.
542 new_ephemeral (struct CadetTunnel *t)
544 GNUNET_free_non_null (t->ax.DHRs);
545 t->ax.DHRs = GNUNET_CRYPTO_ecdhe_key_create ();
549 /* ************************************** start core crypto ***************************** */
555 * @param plaintext Content to HMAC.
556 * @param size Size of @c plaintext.
557 * @param iv Initialization vector for the message.
558 * @param key Key to use.
559 * @param hmac[out] Destination to store the HMAC.
562 t_hmac (const void *plaintext,
565 const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
566 struct GNUNET_ShortHashCode *hmac)
568 static const char ctx[] = "cadet authentication key";
569 struct GNUNET_CRYPTO_AuthKey auth_key;
570 struct GNUNET_HashCode hash;
572 GNUNET_CRYPTO_hmac_derive_key (&auth_key,
578 /* Two step: CADET_Hash is only 256 bits, HashCode is 512. */
579 GNUNET_CRYPTO_hmac (&auth_key,
592 * @param key Key to use.
593 * @param hash[out] Resulting HMAC.
594 * @param source Source key material (data to HMAC).
595 * @param len Length of @a source.
598 t_ax_hmac_hash (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
599 struct GNUNET_HashCode *hash,
603 static const char ctx[] = "axolotl HMAC-HASH";
604 struct GNUNET_CRYPTO_AuthKey auth_key;
606 GNUNET_CRYPTO_hmac_derive_key (&auth_key,
610 GNUNET_CRYPTO_hmac (&auth_key,
618 * Derive a symmetric encryption key from an HMAC-HASH.
620 * @param key Key to use for the HMAC.
621 * @param[out] out Key to generate.
622 * @param source Source key material (data to HMAC).
623 * @param len Length of @a source.
626 t_hmac_derive_key (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
627 struct GNUNET_CRYPTO_SymmetricSessionKey *out,
631 static const char ctx[] = "axolotl derive key";
632 struct GNUNET_HashCode h;
638 GNUNET_CRYPTO_kdf (out, sizeof (*out),
646 * Encrypt data with the axolotl tunnel key.
648 * @param t Tunnel whose key to use.
649 * @param dst Destination with @a size bytes for the encrypted data.
650 * @param src Source of the plaintext. Can overlap with @c dst, must contain @a size bytes
651 * @param size Size of the buffers at @a src and @a dst
654 t_ax_encrypt (struct CadetTunnel *t,
659 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
660 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
661 struct CadetTunnelAxolotl *ax;
665 ax->ratchet_counter++;
666 if ( (GNUNET_YES == ax->ratchet_allowed) &&
667 ( (ratchet_messages <= ax->ratchet_counter) ||
668 (0 == GNUNET_TIME_absolute_get_remaining (ax->ratchet_expiration).rel_value_us)) )
670 ax->ratchet_flag = GNUNET_YES;
672 if (GNUNET_YES == ax->ratchet_flag)
674 /* Advance ratchet */
675 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3];
676 struct GNUNET_HashCode dh;
677 struct GNUNET_HashCode hmac;
678 static const char ctx[] = "axolotl ratchet";
683 /* RK, NHKs, CKs = KDF( HMAC-HASH(RK, DH(DHRs, DHRr)) ) */
684 GNUNET_CRYPTO_ecc_ecdh (ax->DHRs,
687 t_ax_hmac_hash (&ax->RK,
691 GNUNET_CRYPTO_kdf (keys, sizeof (keys),
693 &hmac, sizeof (hmac),
701 ax->ratchet_flag = GNUNET_NO;
702 ax->ratchet_allowed = GNUNET_NO;
703 ax->ratchet_counter = 0;
704 ax->ratchet_expiration
705 = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
709 t_hmac_derive_key (&ax->CKs,
713 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
718 out_size = GNUNET_CRYPTO_symmetric_encrypt (src,
723 GNUNET_assert (size == out_size);
724 t_hmac_derive_key (&ax->CKs,
732 * Decrypt data with the axolotl tunnel key.
734 * @param t Tunnel whose key to use.
735 * @param dst Destination for the decrypted data, must contain @a size bytes.
736 * @param src Source of the ciphertext. Can overlap with @c dst, must contain @a size bytes.
737 * @param size Size of the @a src and @a dst buffers
740 t_ax_decrypt (struct CadetTunnel *t,
745 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
746 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
747 struct CadetTunnelAxolotl *ax;
751 t_hmac_derive_key (&ax->CKr,
755 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
759 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
760 out_size = GNUNET_CRYPTO_symmetric_decrypt (src,
765 GNUNET_assert (out_size == size);
766 t_hmac_derive_key (&ax->CKr,
774 * Encrypt header with the axolotl header key.
776 * @param t Tunnel whose key to use.
777 * @param msg Message whose header to encrypt.
780 t_h_encrypt (struct CadetTunnel *t,
781 struct GNUNET_CADET_TunnelEncryptedMessage *msg)
783 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
784 struct CadetTunnelAxolotl *ax;
788 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
792 out_size = GNUNET_CRYPTO_symmetric_encrypt (&msg->Ns,
797 GNUNET_assert (AX_HEADER_SIZE == out_size);
802 * Decrypt header with the current axolotl header key.
804 * @param t Tunnel whose current ax HK to use.
805 * @param src Message whose header to decrypt.
806 * @param dst Where to decrypt header to.
809 t_h_decrypt (struct CadetTunnel *t,
810 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
811 struct GNUNET_CADET_TunnelEncryptedMessage *dst)
813 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
814 struct CadetTunnelAxolotl *ax;
818 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
822 out_size = GNUNET_CRYPTO_symmetric_decrypt (&src->Ns,
827 GNUNET_assert (AX_HEADER_SIZE == out_size);
832 * Delete a key from the list of skipped keys.
834 * @param t Tunnel to delete from.
835 * @param key Key to delete.
838 delete_skipped_key (struct CadetTunnel *t,
839 struct CadetTunnelSkippedKey *key)
841 GNUNET_CONTAINER_DLL_remove (t->ax.skipped_head,
850 * Decrypt and verify data with the appropriate tunnel key and verify that the
851 * data has not been altered since it was sent by the remote peer.
853 * @param t Tunnel whose key to use.
854 * @param dst Destination for the plaintext.
855 * @param src Source of the message. Can overlap with @c dst.
856 * @param size Size of the message.
857 * @return Size of the decrypted data, -1 if an error was encountered.
860 try_old_ax_keys (struct CadetTunnel *t,
862 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
865 struct CadetTunnelSkippedKey *key;
866 struct GNUNET_ShortHashCode *hmac;
867 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
868 struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
869 struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK;
875 LOG (GNUNET_ERROR_TYPE_DEBUG,
876 "Trying skipped keys\n");
877 hmac = &plaintext_header.hmac;
878 esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
880 /* Find a correct Header Key */
882 for (key = t->ax.skipped_head; NULL != key; key = key->next)
885 AX_HEADER_SIZE + esize,
889 if (0 == memcmp (hmac,
900 /* Should've been checked in -cadet_connection.c handle_cadet_encrypted. */
901 GNUNET_assert (size > sizeof (struct GNUNET_CADET_TunnelEncryptedMessage));
902 len = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
903 GNUNET_assert (len >= sizeof (struct GNUNET_MessageHeader));
906 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
910 res = GNUNET_CRYPTO_symmetric_decrypt (&src->Ns,
914 &plaintext_header.Ns);
915 GNUNET_assert (AX_HEADER_SIZE == res);
917 /* Find the correct message key */
918 N = ntohl (plaintext_header.Ns);
919 while ( (NULL != key) &&
922 if ( (NULL == key) ||
923 (0 != memcmp (&key->HK,
925 sizeof (*valid_HK))) )
928 /* Decrypt payload */
929 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
934 res = GNUNET_CRYPTO_symmetric_decrypt (&src[1],
939 delete_skipped_key (t,
946 * Delete a key from the list of skipped keys.
948 * @param t Tunnel to delete from.
949 * @param HKr Header Key to use.
952 store_skipped_key (struct CadetTunnel *t,
953 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr)
955 struct CadetTunnelSkippedKey *key;
957 key = GNUNET_new (struct CadetTunnelSkippedKey);
958 key->timestamp = GNUNET_TIME_absolute_get ();
961 t_hmac_derive_key (&t->ax.CKr,
965 t_hmac_derive_key (&t->ax.CKr,
969 GNUNET_CONTAINER_DLL_insert (t->ax.skipped_head,
978 * Stage skipped AX keys and calculate the message key.
979 * Stores each HK and MK for skipped messages.
981 * @param t Tunnel where to stage the keys.
982 * @param HKr Header key.
983 * @param Np Received meesage number.
984 * @return #GNUNET_OK if keys were stored.
985 * #GNUNET_SYSERR if an error ocurred (Np not expected).
988 store_ax_keys (struct CadetTunnel *t,
989 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr,
995 LOG (GNUNET_ERROR_TYPE_DEBUG,
996 "Storing skipped keys [%u, %u)\n",
999 if (MAX_KEY_GAP < gap)
1001 /* Avoid DoS (forcing peer to do 2^33 chain HMAC operations) */
1002 /* TODO: start new key exchange on return */
1003 GNUNET_break_op (0);
1004 LOG (GNUNET_ERROR_TYPE_WARNING,
1005 "Got message %u, expected %u+\n",
1008 return GNUNET_SYSERR;
1012 /* Delayed message: don't store keys, flag to try old keys. */
1013 return GNUNET_SYSERR;
1016 while (t->ax.Nr < Np)
1017 store_skipped_key (t,
1020 while (t->ax.skipped > MAX_SKIPPED_KEYS)
1021 delete_skipped_key (t,
1022 t->ax.skipped_tail);
1028 * Decrypt and verify data with the appropriate tunnel key and verify that the
1029 * data has not been altered since it was sent by the remote peer.
1031 * @param t Tunnel whose key to use.
1032 * @param dst Destination for the plaintext.
1033 * @param src Source of the message. Can overlap with @c dst.
1034 * @param size Size of the message.
1035 * @return Size of the decrypted data, -1 if an error was encountered.
1038 t_ax_decrypt_and_validate (struct CadetTunnel *t,
1040 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
1043 struct CadetTunnelAxolotl *ax;
1044 struct GNUNET_ShortHashCode msg_hmac;
1045 struct GNUNET_HashCode hmac;
1046 struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
1049 size_t esize; /* Size of encryped payload */
1051 esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
1054 /* Try current HK */
1056 AX_HEADER_SIZE + esize,
1059 if (0 != memcmp (&msg_hmac,
1063 static const char ctx[] = "axolotl ratchet";
1064 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3]; /* RKp, NHKp, CKp */
1065 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
1066 struct GNUNET_HashCode dh;
1067 struct GNUNET_CRYPTO_EcdhePublicKey *DHRp;
1071 AX_HEADER_SIZE + esize,
1075 if (0 != memcmp (&msg_hmac,
1079 /* Try the skipped keys, if that fails, we're out of luck. */
1080 return try_old_ax_keys (t,
1090 Np = ntohl (plaintext_header.Ns);
1091 PNp = ntohl (plaintext_header.PNs);
1092 DHRp = &plaintext_header.DHRs;
1097 /* RKp, NHKp, CKp = KDF (HMAC-HASH (RK, DH (DHRp, DHRs))) */
1098 GNUNET_CRYPTO_ecc_ecdh (ax->DHRs,
1101 t_ax_hmac_hash (&ax->RK,
1104 GNUNET_CRYPTO_kdf (keys, sizeof (keys),
1106 &hmac, sizeof (hmac),
1109 /* Commit "purported" keys */
1115 ax->ratchet_allowed = GNUNET_YES;
1122 Np = ntohl (plaintext_header.Ns);
1123 PNp = ntohl (plaintext_header.PNs);
1125 if ( (Np != ax->Nr) &&
1126 (GNUNET_OK != store_ax_keys (t,
1130 /* Try the skipped keys, if that fails, we're out of luck. */
1131 return try_old_ax_keys (t,
1147 * Send a KX message.
1149 * FIXME: does not take care of sender-authentication yet!
1151 * @param t Tunnel on which to send it.
1152 * @param force_reply Force the other peer to reply with a KX message.
1155 send_kx (struct CadetTunnel *t,
1158 struct CadetTunnelAxolotl *ax = &t->ax;
1159 struct CadetConnection *c;
1160 struct GNUNET_MQ_Envelope *env;
1161 struct GNUNET_CADET_TunnelKeyExchangeMessage *msg;
1162 enum GNUNET_CADET_KX_Flags flags;
1165 if (NULL != t->ephm_h)
1167 LOG (GNUNET_ERROR_TYPE_INFO,
1168 " already queued, nop\n");
1172 c = NULL; // FIXME: figure out where to transmit...
1174 // GNUNET_assert (GNUNET_NO == GCT_is_loopback (t));
1175 env = GNUNET_MQ_msg (msg,
1176 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX);
1177 flags = GNUNET_CADET_KX_FLAG_NONE;
1178 if (GNUNET_YES == force_reply)
1179 flags |= GNUNET_CADET_KX_FLAG_FORCE_REPLY;
1180 msg->flags = htonl (flags);
1181 msg->cid = *GCC_get_id (c);
1182 GNUNET_CRYPTO_ecdhe_key_get_public (ax->kx_0,
1183 &msg->ephemeral_key);
1184 GNUNET_CRYPTO_ecdhe_key_get_public (ax->DHRs,
1187 // FIXME: send 'env'.
1189 t->ephm_h = GCC_send_prebuilt_message (&msg.header,
1193 GCC_is_origin (c, GNUNET_YES),
1194 GNUNET_YES, &ephm_sent, t);
1195 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
1196 GCT_change_estate (t, CADET_TUNNEL_KEY_SENT);
1202 * Handle KX message.
1204 * FIXME: sender-authentication in KX is missing!
1206 * @param ct connection/tunnel combo that received encrypted message
1207 * @param msg the key exchange message
1210 GCT_handle_kx (struct CadetTConnection *ct,
1211 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
1213 struct CadetTunnel *t = ct->t;
1214 struct CadetTunnelAxolotl *ax = &t->ax;
1215 struct GNUNET_HashCode key_material[3];
1216 struct GNUNET_CRYPTO_SymmetricSessionKey keys[5];
1217 const char salt[] = "CADET Axolotl salt";
1218 const struct GNUNET_PeerIdentity *pid;
1221 pid = GCP_get_id (t->destination);
1222 if (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id,
1224 am_I_alice = GNUNET_YES;
1225 else if (0 < GNUNET_CRYPTO_cmp_peer_identity (&my_full_id,
1227 am_I_alice = GNUNET_NO;
1230 GNUNET_break_op (0);
1234 if (0 != (GNUNET_CADET_KX_FLAG_FORCE_REPLY & ntohl (msg->flags)))
1236 if (NULL != t->rekey_task)
1238 GNUNET_SCHEDULER_cancel (t->rekey_task);
1239 t->rekey_task = NULL;
1245 if (0 == memcmp (&ax->DHRr,
1247 sizeof (msg->ratchet_key)))
1249 LOG (GNUNET_ERROR_TYPE_INFO,
1250 " known ratchet key, exit\n");
1254 ax->DHRr = msg->ratchet_key;
1257 if (GNUNET_YES == am_I_alice)
1259 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, /* A */
1260 &msg->ephemeral_key, /* B0 */
1265 GNUNET_CRYPTO_ecdh_eddsa (ax->kx_0, /* B0 */
1266 &pid->public_key, /* A */
1271 if (GNUNET_YES == am_I_alice)
1273 GNUNET_CRYPTO_ecdh_eddsa (ax->kx_0, /* A0 */
1274 &pid->public_key, /* B */
1279 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, /* A */
1280 &msg->ephemeral_key, /* B0 */
1287 /* (This is the triple-DH, we could probably safely skip this,
1288 as A0/B0 are already in the key material.) */
1289 GNUNET_CRYPTO_ecc_ecdh (ax->kx_0, /* A0 or B0 */
1290 &msg->ephemeral_key, /* B0 or A0 */
1294 GNUNET_CRYPTO_kdf (keys, sizeof (keys),
1295 salt, sizeof (salt),
1296 &key_material, sizeof (key_material),
1299 if (0 == memcmp (&ax->RK,
1303 LOG (GNUNET_ERROR_TYPE_INFO,
1304 " known handshake key, exit\n");
1308 if (GNUNET_YES == am_I_alice)
1314 ax->ratchet_flag = GNUNET_YES;
1322 ax->ratchet_flag = GNUNET_NO;
1323 ax->ratchet_allowed = GNUNET_NO;
1324 ax->ratchet_counter = 0;
1325 ax->ratchet_expiration
1326 = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
1334 /* After KX is done, update state machine and begin transmissions... */
1335 GCT_change_estate (t,
1336 CADET_TUNNEL_KEY_PING);
1337 send_queued_data (t);
1342 /* ************************************** end core crypto ***************************** */
1346 * Add a channel to a tunnel.
1350 * @return unique number identifying @a ch within @a t
1352 struct GNUNET_CADET_ChannelTunnelNumber
1353 GCT_add_channel (struct CadetTunnel *t,
1354 struct CadetChannel *ch)
1356 struct GNUNET_CADET_ChannelTunnelNumber ret;
1359 chid = ntohl (t->next_chid.cn);
1361 GNUNET_CONTAINER_multihashmap32_get (t->channels,
1364 GNUNET_assert (GNUNET_YES ==
1365 GNUNET_CONTAINER_multihashmap32_put (t->channels,
1368 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1369 t->next_chid.cn = htonl (chid + 1);
1370 ret.cn = htonl (chid);
1376 * This tunnel is no longer used, destroy it.
1378 * @param cls the idle tunnel
1381 destroy_tunnel (void *cls)
1383 struct CadetTunnel *t = cls;
1384 struct CadetTConnection *ct;
1385 struct CadetTunnelQueueEntry *tqe;
1387 t->destroy_task = NULL;
1388 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (t->channels));
1389 while (NULL != (ct = t->connection_head))
1391 GNUNET_assert (ct->t == t);
1392 GNUNET_CONTAINER_DLL_remove (t->connection_head,
1395 GCC_destroy (ct->cc);
1398 while (NULL != (tqe = t->tq_head))
1400 GNUNET_CONTAINER_DLL_remove (t->tq_head,
1403 GNUNET_MQ_discard (tqe->env);
1406 GCP_drop_tunnel (t->destination,
1408 GNUNET_CONTAINER_multihashmap32_destroy (t->channels);
1409 if (NULL != t->maintain_connections_task)
1411 GNUNET_SCHEDULER_cancel (t->maintain_connections_task);
1412 t->maintain_connections_task = NULL;
1414 GNUNET_MST_destroy (t->mst);
1415 GNUNET_MQ_destroy (t->mq);
1421 * A connection is ready for transmission. Looks at our message queue
1422 * and if there is a message, sends it out via the connection.
1424 * @param cls the `struct CadetTConnection` that is ready
1427 connection_ready_cb (void *cls)
1429 struct CadetTConnection *ct = cls;
1430 struct CadetTunnel *t = ct->t;
1431 struct CadetTunnelQueueEntry *tq = t->tq_head;
1434 return; /* no messages pending right now */
1436 /* ready to send message 'tq' on tunnel 'ct' */
1437 GNUNET_assert (t == tq->t);
1438 GNUNET_CONTAINER_DLL_remove (t->tq_head,
1441 if (NULL != tq->cid)
1442 *tq->cid = *GCC_get_id (ct->cc);
1443 GCC_transmit (ct->cc,
1445 tq->cont (tq->cont_cls);
1451 * Called when either we have a new connection, or a new message in the
1452 * queue, or some existing connection has transmission capacity. Looks
1453 * at our message queue and if there is a message, picks a connection
1456 * @param t tunnel to process messages on
1459 trigger_transmissions (struct CadetTunnel *t)
1461 struct CadetTConnection *ct;
1463 if (NULL == t->tq_head)
1464 return; /* no messages pending right now */
1465 for (ct = t->connection_head;
1468 if (GNUNET_YES == GCC_is_ready (ct->cc))
1471 return; /* no connections ready */
1472 connection_ready_cb (ct);
1477 * Function called to maintain the connections underlying our tunnel.
1478 * Tries to maintain (incl. tear down) connections for the tunnel, and
1479 * if there is a significant change, may trigger transmissions.
1481 * Basically, needs to check if there are connections that perform
1482 * badly, and if so eventually kill them and trigger a replacement.
1483 * The strategy is to open one more connection than
1484 * #DESIRED_CONNECTIONS_PER_TUNNEL, and then periodically kick out the
1485 * least-performing one, and then inquire for new ones.
1487 * @param cls the `struct CadetTunnel`
1490 maintain_connections_cb (void *cls)
1492 struct CadetTunnel *t = cls;
1494 GNUNET_break (0); // FIXME: implement!
1499 * Consider using the path @a p for the tunnel @a t.
1500 * The tunnel destination is at offset @a off in path @a p.
1502 * @param cls our tunnel
1503 * @param path a path to our destination
1504 * @param off offset of the destination on path @a path
1505 * @return #GNUNET_YES (should keep iterating)
1508 consider_path_cb (void *cls,
1509 struct CadetPeerPath *path,
1512 struct CadetTunnel *t = cls;
1513 unsigned int min_length = UINT_MAX;
1514 GNUNET_CONTAINER_HeapCostType max_desire = 0;
1515 struct CadetTConnection *ct;
1517 /* Check if we care about the new path. */
1518 for (ct = t->connection_head;
1522 struct CadetPeerPath *ps;
1524 ps = GCC_get_path (ct->cc);
1526 return GNUNET_YES; /* duplicate */
1527 min_length = GNUNET_MIN (min_length,
1528 GCPP_get_length (ps));
1529 max_desire = GNUNET_MAX (max_desire,
1530 GCPP_get_desirability (ps));
1533 /* FIXME: not sure we should really just count
1534 'num_connections' here, as they may all have
1535 consistently failed to connect. */
1537 /* We iterate by increasing path length; if we have enough paths and
1538 this one is more than twice as long than what we are currently
1539 using, then ignore all of these super-long ones! */
1540 if ( (t->num_connections > DESIRED_CONNECTIONS_PER_TUNNEL) &&
1541 (min_length * 2 < off) )
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1544 "Ignoring paths of length %u, they are way too long.\n",
1548 /* If we have enough paths and this one looks no better, ignore it. */
1549 if ( (t->num_connections >= DESIRED_CONNECTIONS_PER_TUNNEL) &&
1550 (min_length < GCPP_get_length (path)) &&
1551 (max_desire > GCPP_get_desirability (path)) )
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1554 "Ignoring path (%u/%llu) to %s, got something better already.\n",
1555 GCPP_get_length (path),
1556 (unsigned long long) GCPP_get_desirability (path),
1557 GCP_2s (t->destination));
1561 /* Path is interesting (better by some metric, or we don't have
1562 enough paths yet). */
1563 ct = GNUNET_new (struct CadetTConnection);
1564 ct->created = GNUNET_TIME_absolute_get ();
1566 ct->cc = GCC_create (t->destination,
1569 &connection_ready_cb,
1571 /* FIXME: schedule job to kill connection (and path?) if it takes
1572 too long to get ready! (And track performance data on how long
1573 other connections took with the tunnel!)
1574 => Note: to be done within 'connection'-logic! */
1575 GNUNET_CONTAINER_DLL_insert (t->connection_head,
1578 t->num_connections++;
1584 * Consider using the path @a p for the tunnel @a t.
1585 * The tunnel destination is at offset @a off in path @a p.
1587 * @param cls our tunnel
1588 * @param path a path to our destination
1589 * @param off offset of the destination on path @a path
1592 GCT_consider_path (struct CadetTunnel *t,
1593 struct CadetPeerPath *p,
1596 (void) consider_path_cb (t,
1605 * @param cls the `struct CadetTunnel` for which we decrypted the message
1606 * @param msg the message we received on the tunnel
1609 handle_plaintext_keepalive (void *cls,
1610 const struct GNUNET_MessageHeader *msg)
1612 struct CadetTunnel *t = cls;
1613 GNUNET_break (0); // FIXME
1618 * Check that @a msg is well-formed.
1620 * @param cls the `struct CadetTunnel` for which we decrypted the message
1621 * @param msg the message we received on the tunnel
1622 * @return #GNUNET_OK (any variable-size payload goes)
1625 check_plaintext_data (void *cls,
1626 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
1635 * @param cls the `struct CadetTunnel` for which we decrypted the message
1636 * @param msg the message we received on the tunnel
1639 handle_plaintext_data (void *cls,
1640 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
1642 struct CadetTunnel *t = cls;
1643 GNUNET_break (0); // FIXME!
1650 * @param cls the `struct CadetTunnel` for which we decrypted the message
1651 * @param ack the message we received on the tunnel
1654 handle_plaintext_data_ack (void *cls,
1655 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
1657 struct CadetTunnel *t = cls;
1658 GNUNET_break (0); // FIXME!
1665 * @param cls the `struct CadetTunnel` for which we decrypted the message
1666 * @param cc the message we received on the tunnel
1669 handle_plaintext_channel_create (void *cls,
1670 const struct GNUNET_CADET_ChannelOpenMessage *cc)
1672 struct CadetTunnel *t = cls;
1673 GNUNET_break (0); // FIXME!
1680 * @param cls the `struct CadetTunnel` for which we decrypted the message
1681 * @param cm the message we received on the tunnel
1684 handle_plaintext_channel_nack (void *cls,
1685 const struct GNUNET_CADET_ChannelManageMessage *cm)
1687 struct CadetTunnel *t = cls;
1688 GNUNET_break (0); // FIXME!
1695 * @param cls the `struct CadetTunnel` for which we decrypted the message
1696 * @param cm the message we received on the tunnel
1699 handle_plaintext_channel_ack (void *cls,
1700 const struct GNUNET_CADET_ChannelManageMessage *cm)
1702 struct CadetTunnel *t = cls;
1703 GNUNET_break (0); // FIXME!
1708 * We received a message saying that a channel should be destroyed.
1709 * Pass it on to the correct channel.
1711 * @param cls the `struct CadetTunnel` for which we decrypted the message
1712 * @param cm the message we received on the tunnel
1715 handle_plaintext_channel_destroy (void *cls,
1716 const struct GNUNET_CADET_ChannelManageMessage *cm)
1718 struct CadetTunnel *t = cls;
1719 struct CadetChannel *cc = lookup_channel (t,
1722 GCCH_channel_remote_destroy (cc);
1727 * Handles a message we decrypted, by injecting it into
1728 * our message queue (which will do the dispatching).
1730 * @param cls the `struct CadetTunnel` that got the message
1731 * @param msg the message
1732 * @return #GNUNET_OK (continue to process)
1735 handle_decrypted (void *cls,
1736 const struct GNUNET_MessageHeader *msg)
1738 struct CadetTunnel *t = cls;
1740 GNUNET_MQ_inject_message (t->mq,
1747 * Function called if we had an error processing
1748 * an incoming decrypted message.
1750 * @param cls the `struct CadetTunnel`
1751 * @param error error code
1754 decrypted_error_cb (void *cls,
1755 enum GNUNET_MQ_Error error)
1757 GNUNET_break_op (0);
1762 * Create a tunnel to @a destionation. Must only be called
1763 * from within #GCP_get_tunnel().
1765 * @param destination where to create the tunnel to
1766 * @return new tunnel to @a destination
1768 struct CadetTunnel *
1769 GCT_create_tunnel (struct CadetPeer *destination)
1771 struct GNUNET_MQ_MessageHandler handlers[] = {
1772 GNUNET_MQ_hd_fixed_size (plaintext_keepalive,
1773 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE,
1774 struct GNUNET_MessageHeader,
1776 GNUNET_MQ_hd_var_size (plaintext_data,
1777 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA,
1778 struct GNUNET_CADET_ChannelAppDataMessage,
1780 GNUNET_MQ_hd_fixed_size (plaintext_data_ack,
1781 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK,
1782 struct GNUNET_CADET_ChannelDataAckMessage,
1784 GNUNET_MQ_hd_fixed_size (plaintext_channel_create,
1785 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN,
1786 struct GNUNET_CADET_ChannelOpenMessage,
1788 GNUNET_MQ_hd_fixed_size (plaintext_channel_nack,
1789 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1790 struct GNUNET_CADET_ChannelManageMessage,
1792 GNUNET_MQ_hd_fixed_size (plaintext_channel_ack,
1793 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK,
1794 struct GNUNET_CADET_ChannelManageMessage,
1796 GNUNET_MQ_hd_fixed_size (plaintext_channel_destroy,
1797 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1798 struct GNUNET_CADET_ChannelManageMessage,
1800 GNUNET_MQ_handler_end ()
1802 struct CadetTunnel *t;
1804 t = GNUNET_new (struct CadetTunnel);
1805 t->destination = destination;
1806 t->channels = GNUNET_CONTAINER_multihashmap32_create (8);
1807 (void) GCP_iterate_paths (destination,
1810 t->maintain_connections_task
1811 = GNUNET_SCHEDULER_add_now (&maintain_connections_cb,
1813 t->mq = GNUNET_MQ_queue_for_callbacks (NULL,
1818 &decrypted_error_cb,
1820 t->mst = GNUNET_MST_create (&handle_decrypted,
1827 * Remove a channel from a tunnel.
1831 * @param gid unique number identifying @a ch within @a t
1834 GCT_remove_channel (struct CadetTunnel *t,
1835 struct CadetChannel *ch,
1836 struct GNUNET_CADET_ChannelTunnelNumber gid)
1838 GNUNET_assert (GNUNET_YES ==
1839 GNUNET_CONTAINER_multihashmap32_remove (t->channels,
1843 GNUNET_CONTAINER_multihashmap32_size (t->channels))
1845 t->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_DESTROY_DELAY,
1853 * Change the tunnel encryption state.
1854 * If the encryption state changes to OK, stop the rekey task.
1856 * @param t Tunnel whose encryption state to change, or NULL.
1857 * @param state New encryption state.
1860 GCT_change_estate (struct CadetTunnel *t,
1861 enum CadetTunnelEState state)
1863 enum CadetTunnelEState old = t->estate;
1866 LOG (GNUNET_ERROR_TYPE_DEBUG,
1867 "Tunnel %s estate changed from %d to %d\n",
1872 if ( (CADET_TUNNEL_KEY_OK != old) &&
1873 (CADET_TUNNEL_KEY_OK == t->estate) )
1875 if (NULL != t->rekey_task)
1877 GNUNET_SCHEDULER_cancel (t->rekey_task);
1878 t->rekey_task = NULL;
1881 /* Send queued data if tunnel is not loopback */
1882 if (myid != GCP_get_short_id (t->peer))
1883 send_queued_data (t);
1890 * Add a @a connection to the @a tunnel.
1893 * @param cid connection identifer to use for the connection
1894 * @param path path to use for the connection
1897 GCT_add_inbound_connection (struct CadetTunnel *t,
1898 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
1899 struct CadetPeerPath *path)
1901 struct CadetConnection *cc;
1902 struct CadetTConnection *ct;
1904 ct = GNUNET_new (struct CadetTConnection);
1905 ct->created = GNUNET_TIME_absolute_get ();
1907 ct->cc = GCC_create_inbound (t->destination,
1911 &connection_ready_cb,
1913 /* FIXME: schedule job to kill connection (and path?) if it takes
1914 too long to get ready! (And track performance data on how long
1915 other connections took with the tunnel!)
1916 => Note: to be done within 'connection'-logic! */
1917 GNUNET_CONTAINER_DLL_insert (t->connection_head,
1920 t->num_connections++;
1925 * Handle encrypted message.
1927 * @param ct connection/tunnel combo that received encrypted message
1928 * @param msg the encrypted message to decrypt
1931 GCT_handle_encrypted (struct CadetTConnection *ct,
1932 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
1934 struct CadetTunnel *t = ct->t;
1935 uint16_t size = ntohs (msg->header.size);
1936 char cbuf [size] GNUNET_ALIGN;
1937 ssize_t decrypted_size;
1939 GNUNET_STATISTICS_update (stats,
1940 "# received encrypted",
1944 decrypted_size = t_ax_decrypt_and_validate (t,
1949 if (-1 == decrypted_size)
1951 GNUNET_STATISTICS_update (stats,
1952 "# unable to decrypt",
1955 if (CADET_TUNNEL_KEY_PING <= t->estate)
1957 GNUNET_break_op (0);
1958 LOG (GNUNET_ERROR_TYPE_WARNING,
1959 "Wrong crypto, tunnel %s\n",
1962 GNUNET_ERROR_TYPE_WARNING);
1967 GCT_change_estate (t,
1968 CADET_TUNNEL_KEY_OK);
1969 /* The MST will ultimately call #handle_decrypted() on each message. */
1970 GNUNET_break_op (GNUNET_OK ==
1971 GNUNET_MST_from_buffer (t->mst,
1980 * Sends an already built message on a tunnel, encrypting it and
1981 * choosing the best connection if not provided.
1983 * @param message Message to send. Function modifies it.
1984 * @param t Tunnel on which this message is transmitted.
1985 * @param cont Continuation to call once message is really sent.
1986 * @param cont_cls Closure for @c cont.
1987 * @return Handle to cancel message. NULL if @c cont is NULL.
1989 struct CadetTunnelQueueEntry *
1990 GCT_send (struct CadetTunnel *t,
1991 const struct GNUNET_MessageHeader *message,
1992 GNUNET_SCHEDULER_TaskCallback cont,
1995 struct CadetTunnelQueueEntry *tq;
1996 uint16_t payload_size;
1997 struct GNUNET_MQ_Envelope *env;
1998 struct GNUNET_CADET_TunnelEncryptedMessage *ax_msg;
2000 /* FIXME: what about KX not yet being ready? (see "is_ready()" check in old code!) */
2002 payload_size = ntohs (message->size);
2003 env = GNUNET_MQ_msg_extra (ax_msg,
2005 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED);
2010 ax_msg->Ns = htonl (t->ax.Ns++);
2011 ax_msg->PNs = htonl (t->ax.PNs);
2012 GNUNET_CRYPTO_ecdhe_key_get_public (t->ax.DHRs,
2016 t_hmac (&ax_msg->Ns,
2017 AX_HEADER_SIZE + payload_size,
2021 // ax_msg->pid = htonl (GCC_get_pid (c, fwd)); // FIXME: connection flow-control not (re)implemented yet!
2023 tq = GNUNET_malloc (sizeof (*tq));
2026 tq->cid = &ax_msg->cid;
2028 tq->cont_cls = cont_cls;
2029 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head,
2032 trigger_transmissions (t);
2038 * Cancel a previously sent message while it's in the queue.
2040 * ONLY can be called before the continuation given to the send
2041 * function is called. Once the continuation is called, the message is
2042 * no longer in the queue!
2044 * @param q Handle to the queue entry to cancel.
2047 GCT_send_cancel (struct CadetTunnelQueueEntry *q)
2049 struct CadetTunnel *t = q->t;
2051 GNUNET_CONTAINER_DLL_remove (t->tq_head,
2059 * Iterate over all connections of a tunnel.
2061 * @param t Tunnel whose connections to iterate.
2062 * @param iter Iterator.
2063 * @param iter_cls Closure for @c iter.
2066 GCT_iterate_connections (struct CadetTunnel *t,
2067 GCT_ConnectionIterator iter,
2070 for (struct CadetTConnection *ct = t->connection_head;
2079 * Closure for #iterate_channels_cb.
2086 GCT_ChannelIterator iter;
2089 * Closure for @e iter.
2096 * Helper function for #GCT_iterate_channels.
2098 * @param cls the `struct ChanIterCls`
2100 * @param value a `struct CadetChannel`
2101 * @return #GNUNET_OK
2104 iterate_channels_cb (void *cls,
2108 struct ChanIterCls *ctx = cls;
2109 struct CadetChannel *ch = value;
2111 ctx->iter (ctx->iter_cls,
2118 * Iterate over all channels of a tunnel.
2120 * @param t Tunnel whose channels to iterate.
2121 * @param iter Iterator.
2122 * @param iter_cls Closure for @c iter.
2125 GCT_iterate_channels (struct CadetTunnel *t,
2126 GCT_ChannelIterator iter,
2129 struct ChanIterCls ctx;
2132 ctx.iter_cls = iter_cls;
2133 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
2134 &iterate_channels_cb,
2141 * Call #GCCH_debug() on a channel.
2143 * @param cls points to the log level to use
2145 * @param value the `struct CadetChannel` to dump
2146 * @return #GNUNET_OK (continue iteration)
2149 debug_channel (void *cls,
2153 const enum GNUNET_ErrorType *level = cls;
2154 struct CadetChannel *ch = value;
2156 GCCH_debug (ch, *level);
2162 * Get string description for tunnel connectivity state.
2164 * @param cs Tunnel state.
2166 * @return String representation.
2169 cstate2s (enum CadetTunnelCState cs)
2171 static char buf[32];
2175 case CADET_TUNNEL_NEW:
2176 return "CADET_TUNNEL_NEW";
2177 case CADET_TUNNEL_SEARCHING:
2178 return "CADET_TUNNEL_SEARCHING";
2179 case CADET_TUNNEL_WAITING:
2180 return "CADET_TUNNEL_WAITING";
2181 case CADET_TUNNEL_READY:
2182 return "CADET_TUNNEL_READY";
2183 case CADET_TUNNEL_SHUTDOWN:
2184 return "CADET_TUNNEL_SHUTDOWN";
2186 SPRINTF (buf, "%u (UNKNOWN STATE)", cs);
2193 * Get string description for tunnel encryption state.
2195 * @param es Tunnel state.
2197 * @return String representation.
2200 estate2s (enum CadetTunnelEState es)
2202 static char buf[32];
2206 case CADET_TUNNEL_KEY_UNINITIALIZED:
2207 return "CADET_TUNNEL_KEY_UNINITIALIZED";
2208 case CADET_TUNNEL_KEY_SENT:
2209 return "CADET_TUNNEL_KEY_SENT";
2210 case CADET_TUNNEL_KEY_PING:
2211 return "CADET_TUNNEL_KEY_PING";
2212 case CADET_TUNNEL_KEY_OK:
2213 return "CADET_TUNNEL_KEY_OK";
2214 case CADET_TUNNEL_KEY_REKEY:
2215 return "CADET_TUNNEL_KEY_REKEY";
2217 SPRINTF (buf, "%u (UNKNOWN STATE)", es);
2223 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-tun",__VA_ARGS__)
2227 * Log all possible info about the tunnel state.
2229 * @param t Tunnel to debug.
2230 * @param level Debug level to use.
2233 GCT_debug (const struct CadetTunnel *t,
2234 enum GNUNET_ErrorType level)
2236 struct CadetTConnection *iter_c;
2239 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
2241 __FILE__, __FUNCTION__, __LINE__);
2246 "TTT TUNNEL TOWARDS %s in cstate %s, estate %s tq_len: %u #cons: %u\n",
2248 cstate2s (t->cstate),
2249 estate2s (t->estate),
2251 t->num_connections);
2252 #if DUMP_KEYS_TO_STDERR
2253 ax_debug (t->ax, level);
2257 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
2261 "TTT connections:\n");
2262 for (iter_c = t->connection_head; NULL != iter_c; iter_c = iter_c->next)
2263 GCC_debug (iter_c->cc,
2267 "TTT TUNNEL END\n");
2271 /* end of gnunet-service-cadet-new_tunnels.c */