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 "cadet_protocol.h"
28 #include "cadet_path.h"
30 #include "gnunet-service-cadet_tunnel.h"
31 #include "gnunet-service-cadet_connection.h"
32 #include "gnunet-service-cadet_channel.h"
33 #include "gnunet-service-cadet_peer.h"
35 #define LOG(level, ...) GNUNET_log_from(level,"cadet-tun",__VA_ARGS__)
37 #define REKEY_WAIT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
39 #define CONNECTIONS_PER_TUNNEL 3
41 /******************************************************************************/
42 /******************************** STRUCTS **********************************/
43 /******************************************************************************/
47 struct CadetTChannel *next;
48 struct CadetTChannel *prev;
49 struct CadetChannel *ch;
54 * Connection list and metadata.
56 struct CadetTConnection
61 struct CadetTConnection *next;
66 struct CadetTConnection *prev;
71 struct CadetConnection *c;
74 * Creation time, to keep oldest connection alive.
76 struct GNUNET_TIME_Absolute created;
79 * Connection throughput, to keep fastest connection alive.
85 * Structure used during a Key eXchange.
87 struct CadetTunnelKXCtx
90 * Encryption ("our") old key, for encrypting traffic sent by us
91 * end before the key exchange is finished or times out.
93 struct GNUNET_CRYPTO_SymmetricSessionKey e_key_old;
96 * Decryption ("their") old key, for decrypting traffic sent by the
97 * other end before the key exchange started.
99 struct GNUNET_CRYPTO_SymmetricSessionKey d_key_old;
102 * Challenge to send in a ping and expect in the pong.
107 * When the rekey started. One minute after this the new key will be used.
109 struct GNUNET_TIME_Absolute rekey_start_time;
113 * Struct containing all information regarding a tunnel to a peer.
118 * Endpoint of the tunnel.
120 struct CadetPeer *peer;
123 * State of the tunnel connectivity.
125 enum CadetTunnelCState cstate;
128 * State of the tunnel encryption.
130 enum CadetTunnelEState estate;
133 * Key eXchange context.
135 struct CadetTunnelKXCtx *kx_ctx;
138 * Peer's ephemeral key, to recreate @c e_key and @c d_key when own ephemeral
141 struct GNUNET_CRYPTO_EcdhePublicKey peers_ephemeral_key;
144 * Encryption ("our") key.
146 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
149 * Decryption ("their") key.
151 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
154 * Task to start the rekey process.
156 GNUNET_SCHEDULER_TaskIdentifier rekey_task;
159 * Paths that are actively used to reach the destination peer.
161 struct CadetTConnection *connection_head;
162 struct CadetTConnection *connection_tail;
165 * Next connection number.
170 * Channels inside this tunnel.
172 struct CadetTChannel *channel_head;
173 struct CadetTChannel *channel_tail;
176 * Channel ID for the next created channel.
178 CADET_ChannelNumber next_chid;
181 * Destroy flag: if true, destroy on last message.
183 GNUNET_SCHEDULER_TaskIdentifier destroy_task;
186 * Queued messages, to transmit once tunnel gets connected.
188 struct CadetTunnelDelayed *tq_head;
189 struct CadetTunnelDelayed *tq_tail;
194 * Struct used to save messages in a non-ready tunnel to send once connected.
196 struct CadetTunnelDelayed
201 struct CadetTunnelDelayed *next;
202 struct CadetTunnelDelayed *prev;
207 struct CadetTunnel *t;
210 * Tunnel queue given to the channel to cancel request. Update on send_queued.
212 struct CadetTunnelQueue *tq;
217 /* struct GNUNET_MessageHeader *msg; */
222 * Handle for messages queued but not yet sent.
224 struct CadetTunnelQueue
227 * Connection queue handle, to cancel if necessary.
229 struct CadetConnectionQueue *cq;
232 * Handle in case message hasn't been given to a connection yet.
234 struct CadetTunnelDelayed *tqd;
237 * Continuation to call once sent.
242 * Closure for @c cont.
248 /******************************************************************************/
249 /******************************* GLOBALS ***********************************/
250 /******************************************************************************/
253 * Global handle to the statistics service.
255 extern struct GNUNET_STATISTICS_Handle *stats;
258 * Local peer own ID (memory efficient handle).
260 extern GNUNET_PEER_Id myid;
263 * Local peer own ID (full value).
265 extern struct GNUNET_PeerIdentity my_full_id;
269 * Don't try to recover tunnels if shutting down.
271 extern int shutting_down;
275 * Set of all tunnels, in order to trigger a new exchange on rekey.
276 * Indexed by peer's ID.
278 static struct GNUNET_CONTAINER_MultiPeerMap *tunnels;
281 * Default TTL for payload packets.
283 static unsigned long long default_ttl;
288 const static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
291 * Own ephemeral private key.
293 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
296 * Cached message used to perform a key exchange.
298 static struct GNUNET_CADET_KX_Ephemeral kx_msg;
301 * Task to generate a new ephemeral key.
303 static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
308 static struct GNUNET_TIME_Relative rekey_period;
310 /******************************************************************************/
311 /******************************** STATIC ***********************************/
312 /******************************************************************************/
315 * Get string description for tunnel connectivity state.
317 * @param cs Tunnel state.
319 * @return String representation.
322 cstate2s (enum CadetTunnelCState cs)
324 static char buf[128];
328 case CADET_TUNNEL3_NEW:
329 return "CADET_TUNNEL3_NEW";
330 case CADET_TUNNEL3_SEARCHING:
331 return "CADET_TUNNEL3_SEARCHING";
332 case CADET_TUNNEL3_WAITING:
333 return "CADET_TUNNEL3_WAITING";
334 case CADET_TUNNEL3_READY:
335 return "CADET_TUNNEL3_READY";
338 sprintf (buf, "%u (UNKNOWN STATE)", cs);
346 * Get string description for tunnel encryption state.
348 * @param es Tunnel state.
350 * @return String representation.
353 estate2s (enum CadetTunnelEState es)
355 static char buf[128];
359 case CADET_TUNNEL3_KEY_UNINITIALIZED:
360 return "CADET_TUNNEL3_KEY_UNINITIALIZED";
361 case CADET_TUNNEL3_KEY_SENT:
362 return "CADET_TUNNEL3_KEY_SENT";
363 case CADET_TUNNEL3_KEY_PING:
364 return "CADET_TUNNEL3_KEY_PING";
365 case CADET_TUNNEL3_KEY_OK:
366 return "CADET_TUNNEL3_KEY_OK";
369 sprintf (buf, "%u (UNKNOWN STATE)", es);
377 * @brief Check if tunnel is ready to send traffic.
379 * Tunnel must be connected and with encryption correctly set up.
381 * @param t Tunnel to check.
383 * @return #GNUNET_YES if ready, #GNUNET_NO otherwise
386 is_ready (struct CadetTunnel *t)
391 ready = (CADET_TUNNEL3_READY == t->cstate && CADET_TUNNEL3_KEY_OK == t->estate);
392 ready = ready || GCT_is_loopback (t);
398 * Ephemeral key message purpose size.
400 * @return Size of the part of the ephemeral key message that must be signed.
403 ephemeral_purpose_size (void)
405 return sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
406 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
407 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
408 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
409 sizeof (struct GNUNET_PeerIdentity);
414 * Size of the encrypted part of a ping message.
416 * @return Size of the encrypted part of a ping message.
419 ping_encryption_size (void)
421 return sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t);
426 * Get the channel's buffer. ONLY FOR NON-LOOPBACK CHANNELS!!
428 * @param tch Tunnel's channel handle.
430 * @return Amount of messages the channel can still buffer towards the client.
433 get_channel_buffer (const struct CadetTChannel *tch)
437 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
438 fwd = GCCH_is_origin (tch->ch, GNUNET_YES);
440 return GCCH_get_buffer (tch->ch, fwd);
445 * Get the channel's allowance status.
447 * @param tch Tunnel's channel handle.
449 * @return #GNUNET_YES if we allowed the client to send data to us.
452 get_channel_allowed (const struct CadetTChannel *tch)
456 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
457 fwd = GCCH_is_origin (tch->ch, GNUNET_YES);
459 return GCCH_get_allowed (tch->ch, fwd);
464 * Get the connection's buffer.
466 * @param tc Tunnel's connection handle.
468 * @return Amount of messages the connection can still buffer.
471 get_connection_buffer (const struct CadetTConnection *tc)
475 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
476 fwd = GCC_is_origin (tc->c, GNUNET_YES);
478 return GCC_get_buffer (tc->c, fwd);
483 * Get the connection's allowance.
485 * @param tc Tunnel's connection handle.
487 * @return Amount of messages we have allowed the next peer to send us.
490 get_connection_allowed (const struct CadetTConnection *tc)
494 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
495 fwd = GCC_is_origin (tc->c, GNUNET_YES);
497 return GCC_get_allowed (tc->c, fwd);
502 * Check that a ephemeral key message s well formed and correctly signed.
504 * @param t Tunnel on which the message came.
505 * @param msg The ephemeral key message.
507 * @return GNUNET_OK if message is fine, GNUNET_SYSERR otherwise.
510 check_ephemeral (struct CadetTunnel *t,
511 const struct GNUNET_CADET_KX_Ephemeral *msg)
513 /* Check message size */
514 if (ntohs (msg->header.size) != sizeof (struct GNUNET_CADET_KX_Ephemeral))
515 return GNUNET_SYSERR;
517 /* Check signature size */
518 if (ntohl (msg->purpose.size) != ephemeral_purpose_size ())
519 return GNUNET_SYSERR;
522 if (0 != memcmp (&msg->origin_identity,
523 GCP_get_id (t->peer),
524 sizeof (struct GNUNET_PeerIdentity)))
525 return GNUNET_SYSERR;
527 /* Check signature */
529 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_CADET_KX,
532 &msg->origin_identity.public_key))
533 return GNUNET_SYSERR;
540 * Encrypt data with the tunnel key.
542 * @param t Tunnel whose key to use.
543 * @param dst Destination for the encrypted data.
544 * @param src Source of the plaintext. Can overlap with @c dst.
545 * @param size Size of the plaintext.
546 * @param iv Initialization Vector to use.
549 t_encrypt (struct CadetTunnel *t,
550 void *dst, const void *src,
551 size_t size, uint32_t iv)
553 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
556 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt start\n");
557 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (iv), NULL);
558 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt IV derived\n");
559 out_size = GNUNET_CRYPTO_symmetric_encrypt (src, size, &t->e_key, &siv, dst);
560 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt end\n");
567 * Decrypt data with the tunnel key.
569 * @param t Tunnel whose key to use.
570 * @param dst Destination for the plaintext.
571 * @param src Source of the encrypted data. Can overlap with @c dst.
572 * @param size Size of the encrypted data.
573 * @param iv Initialization Vector to use.
576 t_decrypt (struct CadetTunnel *t,
577 void *dst, const void *src,
578 size_t size, uint32_t iv)
580 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
581 struct GNUNET_CRYPTO_SymmetricSessionKey *key;
584 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt start\n");
585 if (t->estate == CADET_TUNNEL3_KEY_OK || t->estate == CADET_TUNNEL3_KEY_PING)
589 else if (NULL != t->kx_ctx)
591 key = &t->kx_ctx->d_key_old;
595 GNUNET_STATISTICS_update (stats, "# non decryptable data", 1, GNUNET_NO);
596 LOG (GNUNET_ERROR_TYPE_DEBUG,
597 "WARNING got data on %s without a valid key\n",
603 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt iv\n");
604 GNUNET_CRYPTO_symmetric_derive_iv (&siv, key, &iv, sizeof (iv), NULL);
605 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt iv done\n");
606 out_size = GNUNET_CRYPTO_symmetric_decrypt (src, size, key, &siv, dst);
607 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt end\n");
614 * Create key material by doing ECDH on the local and remote ephemeral keys.
616 * @param key_material Where to store the key material.
617 * @param ephemeral_key Peer's public ephemeral key.
620 derive_key_material (struct GNUNET_HashCode *key_material,
621 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key)
624 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
634 * Create a symmetic key from the identities of both ends and the key material
637 * @param key Destination for the generated key.
638 * @param sender ID of the peer that will encrypt with @c key.
639 * @param receiver ID of the peer that will decrypt with @c key.
640 * @param key_material Hash created with ECDH with the ephemeral keys.
643 derive_symmertic (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
644 const struct GNUNET_PeerIdentity *sender,
645 const struct GNUNET_PeerIdentity *receiver,
646 const struct GNUNET_HashCode *key_material)
648 const char salt[] = "CADET kx salt";
650 GNUNET_CRYPTO_kdf (key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
652 key_material, sizeof (struct GNUNET_HashCode),
653 sender, sizeof (struct GNUNET_PeerIdentity),
654 receiver, sizeof (struct GNUNET_PeerIdentity),
660 * Derive the tunnel's keys using our own and the peer's ephemeral keys.
662 * @param t Tunnel for which to create the keys.
665 create_keys (struct CadetTunnel *t)
667 struct GNUNET_HashCode km;
669 derive_key_material (&km, &t->peers_ephemeral_key);
670 derive_symmertic (&t->e_key, &my_full_id, GCP_get_id (t->peer), &km);
671 derive_symmertic (&t->d_key, GCP_get_id (t->peer), &my_full_id, &km);
676 * Pick a connection on which send the next data message.
678 * @param t Tunnel on which to send the message.
680 * @return The connection on which to send the next message.
682 static struct CadetConnection *
683 tunnel_get_connection (struct CadetTunnel *t)
685 struct CadetTConnection *iter;
686 struct CadetConnection *best;
688 unsigned int lowest_q;
690 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GCT_2s (t));
693 for (iter = t->connection_head; NULL != iter; iter = iter->next)
695 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
696 GCC_2s (iter->c), GCC_get_state (iter->c));
697 if (CADET_CONNECTION_READY == GCC_get_state (iter->c))
699 qn = GCC_get_qn (iter->c, GCC_is_origin (iter->c, GNUNET_YES));
700 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
708 LOG (GNUNET_ERROR_TYPE_DEBUG, " selected: connection %s\n", GCC_2s (best));
714 * Callback called when a queued message is sent.
716 * Calculates the average time and connection packet tracking.
718 * @param cls Closure (TunnelQueue handle).
719 * @param c Connection this message was on.
720 * @param q Connection queue handle (unused).
721 * @param type Type of message sent.
722 * @param fwd Was this a FWD going message?
723 * @param size Size of the message.
726 tun_message_sent (void *cls,
727 struct CadetConnection *c,
728 struct CadetConnectionQueue *q,
729 uint16_t type, int fwd, size_t size)
731 struct CadetTunnelQueue *qt = cls;
732 struct CadetTunnel *t;
734 LOG (GNUNET_ERROR_TYPE_DEBUG, "tun_message_sent\n");
736 GNUNET_assert (NULL != qt->cont);
737 t = NULL == c ? NULL : GCC_get_tunnel (c);
738 qt->cont (qt->cont_cls, t, qt, type, size);
744 * Delete a queued message: either was sent or the channel was destroyed
745 * before the tunnel's key exchange had a chance to finish.
747 * @param tqd Delayed queue handle.
750 unqueue_data (struct CadetTunnelDelayed *tqd)
752 GNUNET_CONTAINER_DLL_remove (tqd->t->tq_head, tqd->t->tq_tail, tqd);
758 * Cache a message to be sent once tunnel is online.
760 * @param t Tunnel to hold the message.
761 * @param msg Message itself (copy will be made).
763 static struct CadetTunnelDelayed *
764 queue_data (struct CadetTunnel *t, const struct GNUNET_MessageHeader *msg)
766 struct CadetTunnelDelayed *tqd;
767 uint16_t size = ntohs (msg->size);
769 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue data on Tunnel %s\n", GCT_2s (t));
771 if (GNUNET_YES == is_ready (t))
777 tqd = GNUNET_malloc (sizeof (struct CadetTunnelDelayed) + size);
780 memcpy (&tqd[1], msg, size);
781 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tqd);
789 * @param t Tunnel to get keys from.
790 * @param plaintext Content to HMAC.
791 * @param size Size of @c plaintext.
792 * @param iv Initialization vector for the message.
793 * @param outgoing Is this an outgoing message that we encrypted?
794 * @param hmac Destination to store the HMAC.
797 t_hmac (struct CadetTunnel *t, const void *plaintext, size_t size, uint32_t iv,
798 int outgoing, struct GNUNET_CADET_Hash *hmac)
800 struct GNUNET_CRYPTO_AuthKey auth_key;
801 static const char ctx[] = "cadet authentication key";
802 struct GNUNET_CRYPTO_SymmetricSessionKey *key;
803 struct GNUNET_HashCode hash;
805 key = outgoing ? &t->e_key : &t->d_key;
806 GNUNET_CRYPTO_hmac_derive_key (&auth_key, key,
811 GNUNET_CRYPTO_hmac (&auth_key, plaintext, size, &hash);
812 memcpy (hmac, &hash, sizeof (*hmac));
817 * Sends an already built message on a tunnel, encrypting it and
818 * choosing the best connection.
820 * @param message Message to send. Function modifies it.
821 * @param t Tunnel on which this message is transmitted.
822 * @param c Connection to use (autoselect if NULL).
823 * @param force Force the tunnel to take the message (buffer overfill).
824 * @param cont Continuation to call once message is really sent.
825 * @param cont_cls Closure for @c cont.
826 * @param existing_q In case this a transmission of previously queued data,
827 * this should be TunnelQueue given to the client.
830 * @return Handle to cancel message. NULL if @c cont is NULL.
832 static struct CadetTunnelQueue *
833 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
834 struct CadetTunnel *t, struct CadetConnection *c,
835 int force, GCT_sent cont, void *cont_cls,
836 struct CadetTunnelQueue *existing_q)
838 struct CadetTunnelQueue *tq;
839 struct GNUNET_CADET_Encrypted *msg;
840 size_t size = ntohs (message->size);
841 char cbuf[sizeof (struct GNUNET_CADET_Encrypted) + size];
847 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GCT_2s (t));
849 if (GNUNET_NO == is_ready (t))
851 struct CadetTunnelDelayed *tqd;
852 /* A non null existing_q indicates sending of queued data.
853 * Should only happen after tunnel becomes ready.
855 GNUNET_assert (NULL == existing_q);
856 tqd = queue_data (t, message);
859 tq = GNUNET_new (struct CadetTunnelQueue);
863 tq->cont_cls = cont_cls;
867 GNUNET_assert (GNUNET_NO == GCT_is_loopback (t));
869 iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
870 msg = (struct GNUNET_CADET_Encrypted *) cbuf;
871 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED);
873 GNUNET_assert (t_encrypt (t, &msg[1], message, size, iv) == size);
874 t_hmac (t, &msg[1], size, iv, GNUNET_YES, &msg->hmac);
875 msg->header.size = htons (sizeof (struct GNUNET_CADET_Encrypted) + size);
878 c = tunnel_get_connection (t);
881 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task
882 || CADET_TUNNEL3_SEARCHING != t->cstate)
891 type = ntohs (message->type);
894 case GNUNET_MESSAGE_TYPE_CADET_DATA:
895 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
896 if (GNUNET_MESSAGE_TYPE_CADET_DATA == type)
897 mid = ntohl (((struct GNUNET_CADET_Data *) message)->mid);
899 mid = ntohl (((struct GNUNET_CADET_DataACK *) message)->mid);
901 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
902 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
903 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
904 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
905 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
906 msg->cid = *GCC_get_id (c);
907 msg->ttl = htonl (default_ttl);
912 LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s\n", GC_m2s (type));
914 fwd = GCC_is_origin (c, GNUNET_YES);
918 GNUNET_break (NULL ==
919 GCC_send_prebuilt_message (&msg->header, type, mid,
920 c, fwd, force, NULL, NULL));
923 if (NULL == existing_q)
925 tq = GNUNET_new (struct CadetTunnelQueue); /* FIXME valgrind: leak*/
932 tq->cq = GCC_send_prebuilt_message (&msg->header, type, mid, c, fwd, force,
933 &tun_message_sent, tq);
935 tq->cont_cls = cont_cls;
942 * Send all cached messages that we can, tunnel is online.
944 * @param t Tunnel that holds the messages. Cannot be loopback.
947 send_queued_data (struct CadetTunnel *t)
949 struct CadetTunnelDelayed *tqd;
950 struct CadetTunnelDelayed *next;
953 LOG (GNUNET_ERROR_TYPE_DEBUG,
954 "GCT_send_queued_data on tunnel %s\n",
957 if (GCT_is_loopback (t))
963 if (GNUNET_NO == is_ready (t))
965 LOG (GNUNET_ERROR_TYPE_DEBUG, " not ready yet: %s/%s\n",
966 estate2s (t->estate), cstate2s (t->cstate));
970 room = GCT_get_connections_buffer (t);
971 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
972 LOG (GNUNET_ERROR_TYPE_DEBUG, " tq head: %p\n", t->tq_head);
973 for (tqd = t->tq_head; NULL != tqd && room > 0; tqd = next)
975 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending queued data\n");
978 send_prebuilt_message ((struct GNUNET_MessageHeader *) &tqd[1],
979 tqd->t, NULL, GNUNET_YES,
980 NULL != tqd->tq ? tqd->tq->cont : NULL,
981 NULL != tqd->tq ? tqd->tq->cont_cls : NULL,
985 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCT_send_queued_data end\n", GCP_2s (t->peer));
990 * Sends key exchange message on a tunnel, choosing the best connection.
991 * Should not be called on loopback tunnels.
993 * @param t Tunnel on which this message is transmitted.
994 * @param message Message to send. Function modifies it.
997 send_kx (struct CadetTunnel *t,
998 const struct GNUNET_MessageHeader *message)
1000 struct CadetConnection *c;
1001 struct GNUNET_CADET_KX *msg;
1002 size_t size = ntohs (message->size);
1003 char cbuf[sizeof (struct GNUNET_CADET_KX) + size];
1007 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT KX on Tunnel %s\n", GCT_2s (t));
1009 /* Avoid loopback. */
1010 if (GCT_is_loopback (t))
1012 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
1017 /* Even if tunnel is being destroyed, send anyway.
1018 * Could be a response to a rekey initiated by remote peer,
1019 * who is trying to create a new channel!
1022 /* Must have a connection. */
1023 if (NULL == t->connection_head)
1025 GNUNET_break (CADET_TUNNEL3_SEARCHING == t->cstate);
1030 msg = (struct GNUNET_CADET_KX *) cbuf;
1031 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX);
1032 msg->header.size = htons (sizeof (struct GNUNET_CADET_KX) + size);
1033 c = tunnel_get_connection (t);
1036 GNUNET_break (GNUNET_SCHEDULER_NO_TASK != t->destroy_task
1037 || CADET_TUNNEL3_READY != t->cstate);
1041 type = ntohs (message->type);
1044 case GNUNET_MESSAGE_TYPE_CADET_KX_EPHEMERAL:
1045 case GNUNET_MESSAGE_TYPE_CADET_KX_PING:
1046 case GNUNET_MESSAGE_TYPE_CADET_KX_PONG:
1047 memcpy (&msg[1], message, size);
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1055 fwd = GCC_is_origin (t->connection_head->c, GNUNET_YES);
1056 /* TODO save handle and cancel in case of a unneeded retransmission */
1057 GCC_send_prebuilt_message (&msg->header, GNUNET_MESSAGE_TYPE_CADET_KX,
1058 message->type, c, fwd, GNUNET_YES, NULL, NULL);
1063 * Send the ephemeral key on a tunnel.
1065 * @param t Tunnel on which to send the key.
1068 send_ephemeral (struct CadetTunnel *t)
1070 LOG (GNUNET_ERROR_TYPE_INFO, "=> EPHM for %s\n", GCT_2s (t));
1072 kx_msg.sender_status = htonl (t->estate);
1073 send_kx (t, &kx_msg.header);
1077 * Send a ping message on a tunnel.
1079 * @param t Tunnel on which to send the ping.
1082 send_ping (struct CadetTunnel *t)
1084 struct GNUNET_CADET_KX_Ping msg;
1086 LOG (GNUNET_ERROR_TYPE_INFO, "=> PING for %s\n", GCT_2s (t));
1087 msg.header.size = htons (sizeof (msg));
1088 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX_PING);
1089 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1090 msg.target = *GCP_get_id (t->peer);
1091 msg.nonce = t->kx_ctx->challenge;
1093 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
1094 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n", GNUNET_i2s (&msg.target));
1095 t_encrypt (t, &msg.target, &msg.target, ping_encryption_size(), msg.iv);
1096 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
1097 LOG (GNUNET_ERROR_TYPE_DEBUG, " e towards %s\n", GNUNET_i2s (&msg.target));
1099 send_kx (t, &msg.header);
1104 * Send a pong message on a tunnel.
1106 * @param t Tunnel on which to send the pong.
1107 * @param challenge Value sent in the ping that we have to send back.
1110 send_pong (struct CadetTunnel *t, uint32_t challenge)
1112 struct GNUNET_CADET_KX_Pong msg;
1114 LOG (GNUNET_ERROR_TYPE_INFO, "=> PONG for %s\n", GCT_2s (t));
1115 msg.header.size = htons (sizeof (msg));
1116 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX_PONG);
1117 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1118 msg.nonce = challenge;
1119 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
1120 t_encrypt (t, &msg.nonce, &msg.nonce, sizeof (msg.nonce), msg.iv);
1121 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
1123 send_kx (t, &msg.header);
1128 * Initiate a rekey with the remote peer.
1130 * @param cls Closure (tunnel).
1131 * @param tc TaskContext.
1134 rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1136 struct CadetTunnel *t = cls;
1138 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
1140 LOG (GNUNET_ERROR_TYPE_DEBUG, "Re-key Tunnel %s\n", GCT_2s (t));
1141 if (NULL != tc && 0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1144 if (NULL == t->kx_ctx)
1146 LOG (GNUNET_ERROR_TYPE_DEBUG, " new kx ctx\n");
1147 t->kx_ctx = GNUNET_new (struct CadetTunnelKXCtx);
1148 t->kx_ctx->challenge =
1149 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1150 t->kx_ctx->d_key_old = t->d_key;
1151 t->kx_ctx->e_key_old = t->e_key;
1153 t->kx_ctx->rekey_start_time = GNUNET_TIME_absolute_get ();
1154 LOG (GNUNET_ERROR_TYPE_DEBUG, " new challenge for %s: %u\n",
1155 GCT_2s (t), t->kx_ctx->challenge);
1162 case CADET_TUNNEL3_KEY_UNINITIALIZED:
1163 t->estate = CADET_TUNNEL3_KEY_SENT;
1165 case CADET_TUNNEL3_KEY_SENT:
1167 case CADET_TUNNEL3_KEY_PING:
1168 case CADET_TUNNEL3_KEY_OK:
1170 t->estate = CADET_TUNNEL3_KEY_PING;
1173 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unexpected state %u\n", t->estate);
1176 // FIXME exponential backoff
1177 LOG (GNUNET_ERROR_TYPE_DEBUG, " next call in %s\n",
1178 GNUNET_STRINGS_relative_time_to_string (REKEY_WAIT, GNUNET_YES));
1179 t->rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_WAIT, &rekey_tunnel, t);
1184 * Our ephemeral key has changed, create new session key on all tunnels.
1186 * Each tunnel will start the Key Exchange with a random delay between
1187 * 0 and number_of_tunnels*100 milliseconds, so there are 10 key exchanges
1188 * per second, on average.
1190 * @param cls Closure (size of the hashmap).
1191 * @param key Current public key.
1192 * @param value Value in the hash map (tunnel).
1194 * @return #GNUNET_YES, so we should continue to iterate,
1197 rekey_iterator (void *cls,
1198 const struct GNUNET_PeerIdentity *key,
1201 struct CadetTunnel *t = value;
1202 struct GNUNET_TIME_Relative delay;
1203 long n = (long) cls;
1206 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
1209 if (GNUNET_YES == GCT_is_loopback (t))
1212 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t) n * 100);
1213 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, r);
1214 t->rekey_task = GNUNET_SCHEDULER_add_delayed (delay, &rekey_tunnel, t);
1221 * Create a new ephemeral key and key message, schedule next rekeying.
1223 * @param cls Closure (unused).
1224 * @param tc TaskContext.
1227 rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1229 struct GNUNET_TIME_Absolute time;
1232 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1234 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1237 GNUNET_free_non_null (my_ephemeral_key);
1238 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1240 time = GNUNET_TIME_absolute_get ();
1241 kx_msg.creation_time = GNUNET_TIME_absolute_hton (time);
1242 time = GNUNET_TIME_absolute_add (time, rekey_period);
1243 time = GNUNET_TIME_absolute_add (time, GNUNET_TIME_UNIT_MINUTES);
1244 kx_msg.expiration_time = GNUNET_TIME_absolute_hton (time);
1245 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key, &kx_msg.ephemeral_key);
1247 GNUNET_assert (GNUNET_OK ==
1248 GNUNET_CRYPTO_eddsa_sign (my_private_key,
1250 &kx_msg.signature));
1252 n = (long) GNUNET_CONTAINER_multipeermap_size (tunnels);
1253 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &rekey_iterator, (void *) n);
1255 rekey_task = GNUNET_SCHEDULER_add_delayed (rekey_period, &rekey, NULL);
1260 * Called only on shutdown, destroy every tunnel.
1262 * @param cls Closure (unused).
1263 * @param key Current public key.
1264 * @param value Value in the hash map (tunnel).
1266 * @return #GNUNET_YES, so we should continue to iterate,
1269 destroy_iterator (void *cls,
1270 const struct GNUNET_PeerIdentity *key,
1273 struct CadetTunnel *t = value;
1275 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCT_shutdown destroying tunnel at %p\n", t);
1282 * Notify remote peer that we don't know a channel he is talking about,
1283 * probably CHANNEL_DESTROY was missed.
1285 * @param t Tunnel on which to notify.
1286 * @param gid ID of the channel.
1289 send_channel_destroy (struct CadetTunnel *t, unsigned int gid)
1291 struct GNUNET_CADET_ChannelManage msg;
1293 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1294 msg.header.size = htons (sizeof (msg));
1295 msg.chid = htonl (gid);
1297 LOG (GNUNET_ERROR_TYPE_DEBUG,
1298 "WARNING destroying unknown channel %u on tunnel %s\n",
1300 send_prebuilt_message (&msg.header, t, NULL, GNUNET_YES, NULL, NULL, NULL);
1305 * Demultiplex data per channel and call appropriate channel handler.
1307 * @param t Tunnel on which the data came.
1308 * @param msg Data message.
1309 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1310 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1311 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1312 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1315 handle_data (struct CadetTunnel *t,
1316 const struct GNUNET_CADET_Data *msg,
1319 struct CadetChannel *ch;
1323 size = ntohs (msg->header.size);
1325 sizeof (struct GNUNET_CADET_Data) +
1326 sizeof (struct GNUNET_MessageHeader))
1331 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
1332 GC_m2s (ntohs (msg[1].header.type)));
1335 ch = GCT_get_channel (t, ntohl (msg->chid));
1338 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
1340 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel 0x%X unknown\n",
1342 send_channel_destroy (t, ntohl (msg->chid));
1346 GCCH_handle_data (ch, msg, fwd);
1351 * Demultiplex data ACKs per channel and update appropriate channel buffer info.
1353 * @param t Tunnel on which the DATA ACK came.
1354 * @param msg DATA 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_data_ack (struct CadetTunnel *t,
1362 const struct GNUNET_CADET_DataACK *msg,
1365 struct CadetChannel *ch;
1369 size = ntohs (msg->header.size);
1370 if (size != sizeof (struct GNUNET_CADET_DataACK))
1377 ch = GCT_get_channel (t, ntohl (msg->chid));
1380 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
1382 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1387 GCCH_handle_data_ack (ch, msg, fwd);
1392 * Handle channel create.
1394 * @param t Tunnel on which the data came.
1395 * @param msg Data message.
1398 handle_ch_create (struct CadetTunnel *t,
1399 const struct GNUNET_CADET_ChannelCreate *msg)
1401 struct CadetChannel *ch;
1405 size = ntohs (msg->header.size);
1406 if (size != sizeof (struct GNUNET_CADET_ChannelCreate))
1413 ch = GCT_get_channel (t, ntohl (msg->chid));
1414 if (NULL != ch && ! GCT_is_loopback (t))
1416 /* Probably a retransmission, safe to ignore */
1417 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
1419 ch = GCCH_handle_create (t, msg);
1421 GCT_add_channel (t, ch);
1427 * Handle channel NACK: check correctness and call channel handler for NACKs.
1429 * @param t Tunnel on which the NACK came.
1430 * @param msg NACK message.
1433 handle_ch_nack (struct CadetTunnel *t,
1434 const struct GNUNET_CADET_ChannelManage *msg)
1436 struct CadetChannel *ch;
1440 size = ntohs (msg->header.size);
1441 if (size != sizeof (struct GNUNET_CADET_ChannelManage))
1448 ch = GCT_get_channel (t, ntohl (msg->chid));
1451 GNUNET_STATISTICS_update (stats, "# channel NACK on unknown channel",
1453 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1458 GCCH_handle_nack (ch);
1463 * Handle a CHANNEL ACK (SYNACK/ACK).
1465 * @param t Tunnel on which the CHANNEL ACK came.
1466 * @param msg CHANNEL ACK message.
1467 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1468 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1469 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1470 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1473 handle_ch_ack (struct CadetTunnel *t,
1474 const struct GNUNET_CADET_ChannelManage *msg,
1477 struct CadetChannel *ch;
1481 size = ntohs (msg->header.size);
1482 if (size != sizeof (struct GNUNET_CADET_ChannelManage))
1489 ch = GCT_get_channel (t, ntohl (msg->chid));
1492 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1499 GCCH_handle_ack (ch, msg, fwd);
1505 * Handle a channel destruction message.
1507 * @param t Tunnel on which the message came.
1508 * @param msg Channel destroy message.
1509 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1510 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1511 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1512 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1515 handle_ch_destroy (struct CadetTunnel *t,
1516 const struct GNUNET_CADET_ChannelManage *msg,
1519 struct CadetChannel *ch;
1523 size = ntohs (msg->header.size);
1524 if (size != sizeof (struct GNUNET_CADET_ChannelManage))
1531 ch = GCT_get_channel (t, ntohl (msg->chid));
1534 /* Probably a retransmission, safe to ignore */
1538 GCCH_handle_destroy (ch, msg, fwd);
1543 * The peer's ephemeral key has changed: update the symmetrical keys.
1545 * @param t Tunnel this message came on.
1546 * @param msg Key eXchange message.
1549 handle_ephemeral (struct CadetTunnel *t,
1550 const struct GNUNET_CADET_KX_Ephemeral *msg)
1552 LOG (GNUNET_ERROR_TYPE_INFO, "<=== EPHM for %s\n", GCT_2s (t));
1554 if (GNUNET_OK != check_ephemeral (t, msg))
1556 GNUNET_break_op (0);
1559 t->peers_ephemeral_key = msg->ephemeral_key;
1561 if (CADET_TUNNEL3_KEY_SENT == t->estate)
1563 LOG (GNUNET_ERROR_TYPE_DEBUG, " our key was sent, sending ping\n");
1565 t->estate = CADET_TUNNEL3_KEY_PING;
1571 * Peer wants to check our symmetrical keys by sending an encrypted challenge.
1572 * Answer with by retransmitting the challenge with the "opposite" key.
1574 * @param t Tunnel this message came on.
1575 * @param msg Key eXchange Ping message.
1578 handle_ping (struct CadetTunnel *t,
1579 const struct GNUNET_CADET_KX_Ping *msg)
1581 struct GNUNET_CADET_KX_Ping res;
1583 if (ntohs (msg->header.size) != sizeof (res))
1585 GNUNET_break_op (0);
1589 LOG (GNUNET_ERROR_TYPE_INFO, "<=== PING for %s\n", GCT_2s (t));
1590 t_decrypt (t, &res.target, &msg->target, ping_encryption_size (), msg->iv);
1591 if (0 != memcmp (&my_full_id, &res.target, sizeof (my_full_id)))
1593 GNUNET_STATISTICS_update (stats, "# malformed PINGs", 1, GNUNET_NO);
1594 LOG (GNUNET_ERROR_TYPE_WARNING, " malformed PING on %s\n", GCT_2s (t));
1595 LOG (GNUNET_ERROR_TYPE_DEBUG, " e got %u\n", msg->nonce);
1596 LOG (GNUNET_ERROR_TYPE_DEBUG, " e towards %s\n", GNUNET_i2s (&msg->target));
1597 LOG (GNUNET_ERROR_TYPE_DEBUG, " got %u\n", res.nonce);
1598 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n", GNUNET_i2s (&res.target));
1604 send_pong (t, res.nonce);
1609 * Peer has answer to our challenge.
1610 * If answer is successful, consider the key exchange finished and clean
1611 * up all related state.
1613 * @param t Tunnel this message came on.
1614 * @param msg Key eXchange Pong message.
1617 handle_pong (struct CadetTunnel *t,
1618 const struct GNUNET_CADET_KX_Pong *msg)
1622 LOG (GNUNET_ERROR_TYPE_INFO, "<=== PONG for %s\n", GCT_2s (t));
1623 if (GNUNET_SCHEDULER_NO_TASK == t->rekey_task)
1625 GNUNET_STATISTICS_update (stats, "# duplicate PONG messages", 1, GNUNET_NO);
1628 t_decrypt (t, &challenge, &msg->nonce, sizeof (uint32_t), msg->iv);
1630 if (challenge != t->kx_ctx->challenge)
1632 LOG (GNUNET_ERROR_TYPE_WARNING, "Wrong PONG challenge on %s\n", GCT_2s (t));
1633 LOG (GNUNET_ERROR_TYPE_DEBUG, "PONG: %u (e: %u). Expected: %u.\n",
1634 challenge, msg->nonce, t->kx_ctx->challenge);
1639 GNUNET_SCHEDULER_cancel (t->rekey_task);
1640 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
1641 GNUNET_free (t->kx_ctx);
1643 GCT_change_estate (t, CADET_TUNNEL3_KEY_OK);
1648 * Demultiplex by message type and call appropriate handler for a message
1649 * towards a channel of a local tunnel.
1651 * @param t Tunnel this message came on.
1652 * @param msgh Message header.
1653 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1654 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1655 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1656 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1659 handle_decrypted (struct CadetTunnel *t,
1660 const struct GNUNET_MessageHeader *msgh,
1665 type = ntohs (msgh->type);
1666 LOG (GNUNET_ERROR_TYPE_INFO, "<=== %s on %s\n", GC_m2s (type), GCT_2s (t));
1670 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
1671 /* Do nothing, connection aleady got updated. */
1672 GNUNET_STATISTICS_update (stats, "# keepalives received", 1, GNUNET_NO);
1675 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1676 /* Don't send hop ACK, wait for client to ACK */
1677 handle_data (t, (struct GNUNET_CADET_Data *) msgh, fwd);
1680 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
1681 handle_data_ack (t, (struct GNUNET_CADET_DataACK *) msgh, fwd);
1684 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1685 handle_ch_create (t,
1686 (struct GNUNET_CADET_ChannelCreate *) msgh);
1689 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
1691 (struct GNUNET_CADET_ChannelManage *) msgh);
1694 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
1696 (struct GNUNET_CADET_ChannelManage *) msgh,
1700 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1701 handle_ch_destroy (t,
1702 (struct GNUNET_CADET_ChannelManage *) msgh,
1707 GNUNET_break_op (0);
1708 LOG (GNUNET_ERROR_TYPE_WARNING,
1709 "end-to-end message not known (%u)\n",
1710 ntohs (msgh->type));
1715 /******************************************************************************/
1716 /******************************** API ***********************************/
1717 /******************************************************************************/
1720 * Decrypt and demultiplex by message type. Call appropriate handler
1721 * for every message.
1723 * @param t Tunnel this message came on.
1724 * @param msg Encrypted message.
1727 GCT_handle_encrypted (struct CadetTunnel *t,
1728 const struct GNUNET_CADET_Encrypted *msg)
1730 size_t size = ntohs (msg->header.size);
1731 size_t payload_size = size - sizeof (struct GNUNET_CADET_Encrypted);
1732 size_t decrypted_size;
1733 char cbuf [payload_size];
1734 struct GNUNET_MessageHeader *msgh;
1736 struct GNUNET_CADET_Hash hmac;
1738 decrypted_size = t_decrypt (t, cbuf, &msg[1], payload_size, msg->iv);
1739 t_hmac (t, &msg[1], payload_size, msg->iv, GNUNET_NO, &hmac);
1740 if (0 != memcmp (&hmac, &msg->hmac, sizeof (hmac)))
1742 /* checksum failed */
1743 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1744 "Failed checksum validation for a message on tunnel `%s'\n",
1746 GNUNET_STATISTICS_update (stats, "# wrong HMAC", 1, GNUNET_NO);
1750 while (off < decrypted_size)
1752 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1753 handle_decrypted (t, msgh, GNUNET_SYSERR);
1754 off += ntohs (msgh->size);
1760 * Demultiplex an encapsulated KX message by message type.
1762 * @param t Tunnel on which the message came.
1763 * @param message Payload of KX message.
1766 GCT_handle_kx (struct CadetTunnel *t,
1767 const struct GNUNET_MessageHeader *message)
1771 type = ntohs (message->type);
1772 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message received\n", type);
1775 case GNUNET_MESSAGE_TYPE_CADET_KX_EPHEMERAL:
1776 handle_ephemeral (t, (struct GNUNET_CADET_KX_Ephemeral *) message);
1779 case GNUNET_MESSAGE_TYPE_CADET_KX_PING:
1780 handle_ping (t, (struct GNUNET_CADET_KX_Ping *) message);
1783 case GNUNET_MESSAGE_TYPE_CADET_KX_PONG:
1784 handle_pong (t, (struct GNUNET_CADET_KX_Pong *) message);
1788 GNUNET_break_op (0);
1789 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message not known (%u)\n", type);
1795 * Initialize the tunnel subsystem.
1797 * @param c Configuration handle.
1798 * @param key ECC private key, to derive all other keys and do crypto.
1801 GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
1802 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
1804 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1806 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DEFAULT_TTL",
1809 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1810 "CADET", "DEFAULT_TTL", "USING DEFAULT");
1814 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REKEY_PERIOD",
1817 rekey_period = GNUNET_TIME_UNIT_DAYS;
1820 my_private_key = key;
1821 kx_msg.header.size = htons (sizeof (kx_msg));
1822 kx_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX_EPHEMERAL);
1823 kx_msg.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CADET_KX);
1824 kx_msg.purpose.size = htonl (ephemeral_purpose_size ());
1825 kx_msg.origin_identity = my_full_id;
1826 rekey_task = GNUNET_SCHEDULER_add_now (&rekey, NULL);
1828 tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
1833 * Shut down the tunnel subsystem.
1838 if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
1840 GNUNET_SCHEDULER_cancel (rekey_task);
1841 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1843 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &destroy_iterator, NULL);
1844 GNUNET_CONTAINER_multipeermap_destroy (tunnels);
1851 * @param destination Peer this tunnel is towards.
1853 struct CadetTunnel *
1854 GCT_new (struct CadetPeer *destination)
1856 struct CadetTunnel *t;
1858 t = GNUNET_new (struct CadetTunnel);
1860 t->peer = destination;
1863 GNUNET_CONTAINER_multipeermap_put (tunnels, GCP_get_id (destination), t,
1864 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1875 * Change the tunnel's connection state.
1877 * @param t Tunnel whose connection state to change.
1878 * @param cstate New connection state.
1881 GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate)
1885 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s cstate %s => %s\n",
1886 GCP_2s (t->peer), cstate2s (t->cstate), cstate2s (cstate));
1887 if (myid != GCP_get_short_id (t->peer) &&
1888 CADET_TUNNEL3_READY != t->cstate &&
1889 CADET_TUNNEL3_READY == cstate)
1892 if (CADET_TUNNEL3_KEY_OK == t->estate)
1894 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered send queued data\n");
1895 send_queued_data (t);
1897 else if (CADET_TUNNEL3_KEY_UNINITIALIZED == t->estate)
1899 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered rekey\n");
1900 rekey_tunnel (t, NULL);
1905 if (CADET_TUNNEL3_READY == cstate
1906 && CONNECTIONS_PER_TUNNEL <= GCT_count_connections (t))
1908 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered stop dht\n");
1909 GCP_stop_search (t->peer);
1914 * Change the tunnel encryption state.
1916 * @param t Tunnel whose encryption state to change.
1917 * @param state New encryption state.
1920 GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state)
1924 LOG (GNUNET_ERROR_TYPE_DEBUG,
1925 "Tunnel %s estate was %s\n",
1926 GCP_2s (t->peer), estate2s (t->estate));
1927 LOG (GNUNET_ERROR_TYPE_DEBUG,
1928 "Tunnel %s estate is now %s\n",
1929 GCP_2s (t->peer), estate2s (state));
1930 if (myid != GCP_get_short_id (t->peer) &&
1931 CADET_TUNNEL3_KEY_OK != t->estate && CADET_TUNNEL3_KEY_OK == state)
1934 send_queued_data (t);
1942 * @brief Check if tunnel has too many connections, and remove one if necessary.
1944 * Currently this means the newest connection, unless it is a direct one.
1945 * Implemented as a task to avoid freeing a connection that is in the middle
1946 * of being created/processed.
1948 * @param cls Closure (Tunnel to check).
1949 * @param tc Task context.
1952 trim_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1954 struct CadetTunnel *t = cls;
1956 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1959 if (GCT_count_connections (t) > 2 * CONNECTIONS_PER_TUNNEL)
1961 struct CadetTConnection *iter;
1962 struct CadetTConnection *c;
1964 for (c = iter = t->connection_head; NULL != iter; iter = iter->next)
1966 if ((NULL == c || iter->created.abs_value_us > c->created.abs_value_us)
1967 && GNUNET_NO == GCC_is_direct (iter->c))
1974 LOG (GNUNET_ERROR_TYPE_DEBUG, "Too many connections on tunnel %s\n",
1976 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying connection %s\n",
1989 * Add a connection to a tunnel.
1992 * @param c Connection.
1995 GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c)
1997 struct CadetTConnection *aux;
1999 GNUNET_assert (NULL != c);
2001 LOG (GNUNET_ERROR_TYPE_DEBUG, "add connection %s\n", GCC_2s (c));
2002 LOG (GNUNET_ERROR_TYPE_DEBUG, " to tunnel %s\n", GCT_2s (t));
2003 for (aux = t->connection_head; aux != NULL; aux = aux->next)
2007 aux = GNUNET_new (struct CadetTConnection);
2009 aux->created = GNUNET_TIME_absolute_get ();
2011 GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, aux);
2013 GNUNET_SCHEDULER_add_now (&trim_connections, t);
2018 * Mark a path as no longer valid for this tunnel: has been tried and failed.
2020 * @param t Tunnel to update.
2021 * @param path Invalid path to remove. Is destroyed after removal.
2024 GCT_remove_path (struct CadetTunnel *t, struct CadetPeerPath *path)
2026 GCP_remove_path (t->peer, path);
2031 * Remove a connection from a tunnel.
2034 * @param c Connection.
2037 GCT_remove_connection (struct CadetTunnel *t,
2038 struct CadetConnection *c)
2040 struct CadetTConnection *aux;
2041 struct CadetTConnection *next;
2043 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing connection %s from tunnel %s\n",
2044 GCC_2s (c), GCT_2s (t));
2045 for (aux = t->connection_head; aux != NULL; aux = next)
2050 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
2055 /* Start new connections if needed */
2056 if (CONNECTIONS_PER_TUNNEL < GCT_count_connections (t)
2057 && GNUNET_SCHEDULER_NO_TASK == t->destroy_task
2058 && CADET_TUNNEL3_SHUTDOWN != t->cstate
2059 && GNUNET_NO == shutting_down)
2061 LOG (GNUNET_ERROR_TYPE_DEBUG, " no more connections, getting new ones\n");
2062 t->cstate = CADET_TUNNEL3_SEARCHING;
2063 GCP_connect (t->peer);
2067 /* If not marked as ready, no change is needed */
2068 if (CADET_TUNNEL3_READY != t->cstate)
2071 /* Check if any connection is ready to maintaing cstate */
2072 for (aux = t->connection_head; aux != NULL; aux = aux->next)
2073 if (CADET_CONNECTION_READY == GCC_get_state (aux->c))
2076 t->cstate = CADET_TUNNEL3_WAITING;
2081 * Add a channel to a tunnel.
2084 * @param ch Channel.
2087 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch)
2089 struct CadetTChannel *aux;
2091 GNUNET_assert (NULL != ch);
2093 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
2095 for (aux = t->channel_head; aux != NULL; aux = aux->next)
2097 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
2102 aux = GNUNET_new (struct CadetTChannel);
2104 LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
2105 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
2107 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
2109 GNUNET_SCHEDULER_cancel (t->destroy_task);
2110 t->destroy_task = GNUNET_SCHEDULER_NO_TASK;
2111 LOG (GNUNET_ERROR_TYPE_DEBUG, " undo destroy!\n");
2117 * Remove a channel from a tunnel.
2120 * @param ch Channel.
2123 GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch)
2125 struct CadetTChannel *aux;
2127 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
2128 for (aux = t->channel_head; aux != NULL; aux = aux->next)
2132 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GCCH_2s (ch));
2133 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
2142 * Search for a channel by global ID.
2144 * @param t Tunnel containing the channel.
2145 * @param chid Public channel number.
2147 * @return channel handler, NULL if doesn't exist
2149 struct CadetChannel *
2150 GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid)
2152 struct CadetTChannel *iter;
2157 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2159 if (GCCH_get_id (iter->ch) == chid)
2163 return NULL == iter ? NULL : iter->ch;
2168 * @brief Destroy a tunnel and free all resources.
2170 * Should only be called a while after the tunnel has been marked as destroyed,
2171 * in case there is a new channel added to the same peer shortly after marking
2172 * the tunnel. This way we avoid a new public key handshake.
2174 * @param cls Closure (tunnel to destroy).
2175 * @param tc Task context.
2178 delayed_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2180 struct CadetTunnel *t = cls;
2181 struct CadetTConnection *iter;
2183 LOG (GNUNET_ERROR_TYPE_DEBUG, "delayed destroying tunnel %p\n", t);
2184 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2186 LOG (GNUNET_ERROR_TYPE_WARNING,
2187 "Not destroying tunnel, due to shutdown. "
2188 "Tunnel at %p should have been freed by GCT_shutdown\n", t);
2191 t->destroy_task = GNUNET_SCHEDULER_NO_TASK;
2192 t->cstate = CADET_TUNNEL3_SHUTDOWN;
2194 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2196 GCC_send_destroy (iter->c);
2203 * Tunnel is empty: destroy it.
2205 * Notifies all connections about the destruction.
2207 * @param t Tunnel to destroy.
2210 GCT_destroy_empty (struct CadetTunnel *t)
2212 if (GNUNET_YES == shutting_down)
2213 return; /* Will be destroyed immediately anyway */
2215 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
2217 LOG (GNUNET_ERROR_TYPE_DEBUG,
2218 "Tunnel %s is already scheduled for destruction\n",
2221 /* should never happen, tunnel can only become empty once, and the
2222 * task identifier should be NO_TASK (cleaned when the tunnel was created
2223 * or became un-empty)
2228 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s empty: destroying scheduled\n",
2231 t->destroy_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
2232 &delayed_destroy, t);
2233 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled destroy of %p as %llX\n",
2234 t, t->destroy_task);
2239 * Destroy tunnel if empty (no more channels).
2241 * @param t Tunnel to destroy if empty.
2244 GCT_destroy_if_empty (struct CadetTunnel *t)
2246 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s destroy if empty\n", GCT_2s (t));
2247 if (1 < GCT_count_channels (t))
2250 GCT_destroy_empty (t);
2255 * Destroy the tunnel.
2257 * This function does not generate any warning traffic to clients or peers.
2260 * Cancel messages belonging to this tunnel queued to neighbors.
2261 * Free any allocated resources linked to the tunnel.
2263 * @param t The tunnel to destroy.
2266 GCT_destroy (struct CadetTunnel *t)
2268 struct CadetTConnection *iter_c;
2269 struct CadetTConnection *next_c;
2270 struct CadetTChannel *iter_ch;
2271 struct CadetTChannel *next_ch;
2276 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GCP_2s (t->peer));
2278 GNUNET_break (GNUNET_YES ==
2279 GNUNET_CONTAINER_multipeermap_remove (tunnels,
2280 GCP_get_id (t->peer), t));
2282 for (iter_c = t->connection_head; NULL != iter_c; iter_c = next_c)
2284 next_c = iter_c->next;
2285 GCC_destroy (iter_c->c);
2287 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = next_ch)
2289 next_ch = iter_ch->next;
2290 GCCH_destroy (iter_ch->ch);
2291 /* Should only happen on shutdown, but it's ok. */
2294 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
2296 LOG (GNUNET_ERROR_TYPE_DEBUG, "cancelling %llX\n", t->destroy_task);
2297 GNUNET_SCHEDULER_cancel (t->destroy_task);
2298 t->destroy_task = GNUNET_SCHEDULER_NO_TASK;
2301 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2302 GCP_set_tunnel (t->peer, NULL);
2304 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
2306 GNUNET_SCHEDULER_cancel (t->rekey_task);
2307 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
2308 if (NULL != t->kx_ctx)
2309 GNUNET_free (t->kx_ctx);
2319 * @brief Use the given path for the tunnel.
2320 * Update the next and prev hops (and RCs).
2321 * (Re)start the path refresh in case the tunnel is locally owned.
2323 * @param t Tunnel to update.
2324 * @param p Path to use.
2326 * @return Connection created.
2328 struct CadetConnection *
2329 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p)
2331 struct CadetConnection *c;
2332 struct GNUNET_CADET_Hash cid;
2333 unsigned int own_pos;
2335 if (NULL == t || NULL == p)
2341 if (CADET_TUNNEL3_SHUTDOWN == t->cstate)
2347 for (own_pos = 0; own_pos < p->length; own_pos++)
2349 if (p->peers[own_pos] == myid)
2352 if (own_pos >= p->length)
2354 GNUNET_break_op (0);
2358 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE, &cid, sizeof (cid));
2359 c = GCC_new (&cid, t, p, own_pos);
2362 /* Path was flawed */
2365 GCT_add_connection (t, c);
2371 * Count established (ready) connections of a tunnel.
2373 * @param t Tunnel on which to count.
2375 * @return Number of connections.
2378 GCT_count_connections (struct CadetTunnel *t)
2380 struct CadetTConnection *iter;
2386 for (count = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
2387 if (CADET_CONNECTION_DESTROYED != GCC_get_state (iter->c))
2394 * Count channels of a tunnel.
2396 * @param t Tunnel on which to count.
2398 * @return Number of channels.
2401 GCT_count_channels (struct CadetTunnel *t)
2403 struct CadetTChannel *iter;
2406 for (count = 0, iter = t->channel_head;
2408 iter = iter->next, count++) /* skip */;
2415 * Get the connectivity state of a tunnel.
2419 * @return Tunnel's connectivity state.
2421 enum CadetTunnelCState
2422 GCT_get_cstate (struct CadetTunnel *t)
2427 return (enum CadetTunnelCState) -1;
2434 * Get the encryption state of a tunnel.
2438 * @return Tunnel's encryption state.
2440 enum CadetTunnelEState
2441 GCT_get_estate (struct CadetTunnel *t)
2446 return (enum CadetTunnelEState) -1;
2452 * Get the maximum buffer space for a tunnel towards a local client.
2456 * @return Biggest buffer space offered by any channel in the tunnel.
2459 GCT_get_channels_buffer (struct CadetTunnel *t)
2461 struct CadetTChannel *iter;
2462 unsigned int buffer;
2463 unsigned int ch_buf;
2465 if (NULL == t->channel_head)
2467 /* Probably getting buffer for a channel create/handshake. */
2472 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2474 ch_buf = get_channel_buffer (iter);
2475 if (ch_buf > buffer)
2483 * Get the total buffer space for a tunnel for P2P traffic.
2487 * @return Buffer space offered by all connections in the tunnel.
2490 GCT_get_connections_buffer (struct CadetTunnel *t)
2492 struct CadetTConnection *iter;
2493 unsigned int buffer;
2496 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2498 if (GCC_get_state (iter->c) != CADET_CONNECTION_READY)
2502 buffer += get_connection_buffer (iter);
2510 * Get the tunnel's destination.
2514 * @return ID of the destination peer.
2516 const struct GNUNET_PeerIdentity *
2517 GCT_get_destination (struct CadetTunnel *t)
2519 return GCP_get_id (t->peer);
2524 * Get the tunnel's next free global channel ID.
2528 * @return GID of a channel free to use.
2531 GCT_get_next_chid (struct CadetTunnel *t)
2533 CADET_ChannelNumber chid;
2534 CADET_ChannelNumber mask;
2537 /* Set bit 30 depending on the ID relationship. Bit 31 is always 0 for GID.
2538 * If our ID is bigger or loopback tunnel, start at 0, bit 30 = 0
2539 * If peer's ID is bigger, start at 0x4... bit 30 = 1
2541 result = GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GCP_get_id (t->peer));
2546 t->next_chid |= mask;
2548 while (NULL != GCT_get_channel (t, t->next_chid))
2550 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
2551 t->next_chid = (t->next_chid + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
2552 t->next_chid |= mask;
2554 chid = t->next_chid;
2555 t->next_chid = (t->next_chid + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
2556 t->next_chid |= mask;
2563 * Send ACK on one or more channels due to buffer in connections.
2565 * @param t Channel which has some free buffer space.
2568 GCT_unchoke_channels (struct CadetTunnel *t)
2570 struct CadetTChannel *iter;
2571 unsigned int buffer;
2572 unsigned int channels = GCT_count_channels (t);
2573 unsigned int choked_n;
2574 struct CadetChannel *choked[channels];
2576 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCT_unchoke_channels on %s\n", GCT_2s (t));
2577 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
2578 if (NULL != t->channel_head)
2579 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
2581 /* Get buffer space */
2582 buffer = GCT_get_connections_buffer (t);
2588 /* Count and remember choked channels */
2590 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2592 if (GNUNET_NO == get_channel_allowed (iter))
2594 choked[choked_n++] = iter->ch;
2598 /* Unchoke random channels */
2599 while (0 < buffer && 0 < choked_n)
2601 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2603 GCCH_allow_client (choked[r], GCCH_is_origin (choked[r], GNUNET_YES));
2606 choked[r] = choked[choked_n];
2612 * Send ACK on one or more connections due to buffer space to the client.
2614 * Iterates all connections of the tunnel and sends ACKs appropriately.
2619 GCT_send_connection_acks (struct CadetTunnel *t)
2621 struct CadetTConnection *iter;
2624 uint32_t allow_per_connection;
2626 unsigned int buffer;
2628 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel send connection ACKs on %s\n",
2637 buffer = GCT_get_channels_buffer (t);
2638 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer %u\n", buffer);
2640 /* Count connections, how many messages are already allowed */
2641 cs = GCT_count_connections (t);
2642 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
2644 allowed += get_connection_allowed (iter);
2646 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowed %u\n", allowed);
2648 /* Make sure there is no overflow */
2649 if (allowed > buffer)
2654 /* Authorize connections to send more data */
2655 to_allow = buffer; /* - allowed; */
2657 for (iter = t->connection_head;
2658 NULL != iter && to_allow > 0;
2661 allow_per_connection = to_allow/cs;
2662 to_allow -= allow_per_connection;
2664 if (get_connection_allowed (iter) > 64 / 3)
2668 GCC_allow (iter->c, allow_per_connection,
2669 GCC_is_origin (iter->c, GNUNET_NO));
2672 GNUNET_break (to_allow == 0);
2677 * Cancel a previously sent message while it's in the queue.
2679 * ONLY can be called before the continuation given to the send function
2680 * is called. Once the continuation is called, the message is no longer in the
2683 * @param q Handle to the queue.
2686 GCT_cancel (struct CadetTunnelQueue *q)
2691 /* tun_message_sent() will be called and free q */
2693 else if (NULL != q->tqd)
2695 unqueue_data (q->tqd);
2697 if (NULL != q->cont)
2698 q->cont (q->cont_cls, NULL, q, 0, 0);
2709 * Sends an already built message on a tunnel, encrypting it and
2710 * choosing the best connection if not provided.
2712 * @param message Message to send. Function modifies it.
2713 * @param t Tunnel on which this message is transmitted.
2714 * @param c Connection to use (autoselect if NULL).
2715 * @param force Force the tunnel to take the message (buffer overfill).
2716 * @param cont Continuation to call once message is really sent.
2717 * @param cont_cls Closure for @c cont.
2719 * @return Handle to cancel message. NULL if @c cont is NULL.
2721 struct CadetTunnelQueue *
2722 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2723 struct CadetTunnel *t, struct CadetConnection *c,
2724 int force, GCT_sent cont, void *cont_cls)
2726 return send_prebuilt_message (message, t, c, force, cont, cont_cls, NULL);
2731 * Is the tunnel directed towards the local peer?
2735 * @return #GNUNET_YES if it is loopback.
2738 GCT_is_loopback (const struct CadetTunnel *t)
2740 return (myid == GCP_get_short_id (t->peer));
2745 * Is the tunnel this path already?
2750 * @return #GNUNET_YES a connection uses this path.
2753 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p)
2755 struct CadetTConnection *iter;
2757 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2758 if (GCC_get_path (iter->c) == p)
2766 * Get a cost of a path for a tunnel considering existing connections.
2769 * @param path Candidate path.
2771 * @return Cost of the path (path length + number of overlapping nodes)
2774 GCT_get_path_cost (const struct CadetTunnel *t,
2775 const struct CadetPeerPath *path)
2777 struct CadetTConnection *iter;
2778 const struct CadetPeerPath *aux;
2779 unsigned int overlap;
2787 GNUNET_assert (NULL != t);
2789 for (i = 0; i < path->length; i++)
2791 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2793 aux = GCC_get_path (iter->c);
2797 for (j = 0; j < aux->length; j++)
2799 if (path->peers[i] == aux->peers[j])
2807 return path->length + overlap;
2812 * Get the static string for the peer this tunnel is directed.
2816 * @return Static string the destination peer's ID.
2819 GCT_2s (const struct CadetTunnel *t)
2824 return GCP_2s (t->peer);
2828 /******************************************************************************/
2829 /***************************** INFO/DEBUG *******************************/
2830 /******************************************************************************/
2834 * Log all possible info about the tunnel state to stderr.
2836 * @param t Tunnel to debug.
2839 GCT_debug (const struct CadetTunnel *t)
2841 struct CadetTChannel *iterch;
2842 struct CadetTConnection *iterc;
2844 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT DEBUG TUNNEL TOWARDS %s\n", GCT_2s (t));
2845 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT cstate %s, estate %s\n",
2846 cstate2s (t->cstate), estate2s (t->estate));
2847 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT kx_ctx %p, rekey_task %u\n",
2848 t->kx_ctx, t->rekey_task);
2849 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT tq_head %p, tq_tail %p\n",
2850 t->tq_head, t->tq_tail);
2851 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT destroy %u\n", t->destroy_task);
2853 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT channels:\n");
2854 for (iterch = t->channel_head; NULL != iterch; iterch = iterch->next)
2856 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT - %s\n", GCCH_2s (iterch->ch));
2859 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT connections:\n");
2860 for (iterc = t->connection_head; NULL != iterc; iterc = iterc->next)
2862 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT - %s [%u] buf: %u/%u (qn %u/%u)\n",
2863 GCC_2s (iterc->c), GCC_get_state (iterc->c),
2864 GCC_get_buffer (iterc->c, GNUNET_YES),
2865 GCC_get_buffer (iterc->c, GNUNET_NO),
2866 GCC_get_qn (iterc->c, GNUNET_YES),
2867 GCC_get_qn (iterc->c, GNUNET_NO));
2870 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT DEBUG TUNNEL END\n");
2875 * Iterate all tunnels.
2877 * @param iter Iterator.
2878 * @param cls Closure for @c iter.
2881 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2883 GNUNET_CONTAINER_multipeermap_iterate (tunnels, iter, cls);
2888 * Count all tunnels.
2890 * @return Number of tunnels to remote peers kept by this peer.
2893 GCT_count_all (void)
2895 return GNUNET_CONTAINER_multipeermap_size (tunnels);
2900 * Iterate all connections of a tunnel.
2902 * @param t Tunnel whose connections to iterate.
2903 * @param iter Iterator.
2904 * @param cls Closure for @c iter.
2907 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls)
2909 struct CadetTConnection *ct;
2911 for (ct = t->connection_head; NULL != ct; ct = ct->next)
2917 * Iterate all channels of a tunnel.
2919 * @param t Tunnel whose channels to iterate.
2920 * @param iter Iterator.
2921 * @param cls Closure for @c iter.
2924 GCT_iterate_channels (struct CadetTunnel *t, GCT_chan_iter iter, void *cls)
2926 struct CadetTChannel *cht;
2928 for (cht = t->channel_head; NULL != cht; cht = cht->next)
2929 iter (cls, cht->ch);