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 * Decryption ("their") old key, for decrypting traffic sent by the
91 * other end before the key exchange started.
93 struct GNUNET_CRYPTO_SymmetricSessionKey d_key_old;
96 * Challenge to send in a ping and expect in the pong.
102 * Struct containing all information regarding a tunnel to a peer.
107 * Endpoint of the tunnel.
109 struct CadetPeer *peer;
112 * State of the tunnel connectivity.
114 enum CadetTunnel3CState cstate;
117 * State of the tunnel encryption.
119 enum CadetTunnel3EState estate;
122 * Key eXchange context.
124 struct CadetTunnelKXCtx *kx_ctx;
127 * Encryption ("our") key.
129 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
132 * Decryption ("their") key.
134 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
137 * Task to start the rekey process.
139 GNUNET_SCHEDULER_TaskIdentifier rekey_task;
142 * Paths that are actively used to reach the destination peer.
144 struct CadetTConnection *connection_head;
145 struct CadetTConnection *connection_tail;
148 * Next connection number.
153 * Channels inside this tunnel.
155 struct CadetTChannel *channel_head;
156 struct CadetTChannel *channel_tail;
159 * Channel ID for the next created channel.
161 CADET_ChannelNumber next_chid;
164 * Destroy flag: if true, destroy on last message.
166 GNUNET_SCHEDULER_TaskIdentifier destroy_task;
169 * Queued messages, to transmit once tunnel gets connected.
171 struct CadetTunnelDelayed *tq_head;
172 struct CadetTunnelDelayed *tq_tail;
177 * Struct used to save messages in a non-ready tunnel to send once connected.
179 struct CadetTunnelDelayed
184 struct CadetTunnelDelayed *next;
185 struct CadetTunnelDelayed *prev;
190 struct CadetTunnel3 *t;
193 * Tunnel queue given to the channel to cancel request. Update on send_queued.
195 struct CadetTunnel3Queue *tq;
200 /* struct GNUNET_MessageHeader *msg; */
205 * Handle for messages queued but not yet sent.
207 struct CadetTunnel3Queue
210 * Connection queue handle, to cancel if necessary.
212 struct CadetConnectionQueue *cq;
215 * Handle in case message hasn't been given to a connection yet.
217 struct CadetTunnelDelayed *tqd;
220 * Continuation to call once sent.
225 * Closure for @c cont.
231 /******************************************************************************/
232 /******************************* GLOBALS ***********************************/
233 /******************************************************************************/
236 * Global handle to the statistics service.
238 extern struct GNUNET_STATISTICS_Handle *stats;
241 * Local peer own ID (memory efficient handle).
243 extern GNUNET_PEER_Id myid;
246 * Local peer own ID (full value).
248 extern struct GNUNET_PeerIdentity my_full_id;
252 * Don't try to recover tunnels if shutting down.
254 extern int shutting_down;
258 * Set of all tunnels, in order to trigger a new exchange on rekey.
259 * Indexed by peer's ID.
261 static struct GNUNET_CONTAINER_MultiPeerMap *tunnels;
264 * Default TTL for payload packets.
266 static unsigned long long default_ttl;
271 const static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
274 * Own ephemeral private key.
276 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
279 * Cached message used to perform a key exchange.
281 static struct GNUNET_CADET_KX_Ephemeral kx_msg;
284 * Task to generate a new ephemeral key.
286 static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
291 static struct GNUNET_TIME_Relative rekey_period;
293 /******************************************************************************/
294 /******************************** STATIC ***********************************/
295 /******************************************************************************/
298 * Get string description for tunnel connectivity state.
300 * @param cs Tunnel state.
302 * @return String representation.
305 cstate2s (enum CadetTunnel3CState cs)
307 static char buf[128];
311 case CADET_TUNNEL3_NEW:
312 return "CADET_TUNNEL3_NEW";
313 case CADET_TUNNEL3_SEARCHING:
314 return "CADET_TUNNEL3_SEARCHING";
315 case CADET_TUNNEL3_WAITING:
316 return "CADET_TUNNEL3_WAITING";
317 case CADET_TUNNEL3_READY:
318 return "CADET_TUNNEL3_READY";
321 sprintf (buf, "%u (UNKNOWN STATE)", cs);
329 * Get string description for tunnel encryption state.
331 * @param es Tunnel state.
333 * @return String representation.
336 estate2s (enum CadetTunnel3EState es)
338 static char buf[128];
342 case CADET_TUNNEL3_KEY_UNINITIALIZED:
343 return "CADET_TUNNEL3_KEY_UNINITIALIZED";
344 case CADET_TUNNEL3_KEY_SENT:
345 return "CADET_TUNNEL3_KEY_SENT";
346 case CADET_TUNNEL3_KEY_PING:
347 return "CADET_TUNNEL3_KEY_PING";
348 case CADET_TUNNEL3_KEY_OK:
349 return "CADET_TUNNEL3_KEY_OK";
352 sprintf (buf, "%u (UNKNOWN STATE)", es);
360 * @brief Check if tunnel is ready to send traffic.
362 * Tunnel must be connected and with encryption correctly set up.
364 * @param t Tunnel to check.
366 * @return #GNUNET_YES if ready, #GNUNET_NO otherwise
369 is_ready (struct CadetTunnel3 *t)
374 ready = (CADET_TUNNEL3_READY == t->cstate && CADET_TUNNEL3_KEY_OK == t->estate);
375 ready = ready || GMT_is_loopback (t);
381 * Ephemeral key message purpose size.
383 * @return Size of the part of the ephemeral key message that must be signed.
386 ephemeral_purpose_size (void)
388 return sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
389 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
390 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
391 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
392 sizeof (struct GNUNET_PeerIdentity);
397 * Size of the encrypted part of a ping message.
399 * @return Size of the encrypted part of a ping message.
402 ping_encryption_size (void)
404 return sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t);
409 * Get the channel's buffer. ONLY FOR NON-LOOPBACK CHANNELS!!
411 * @param tch Tunnel's channel handle.
413 * @return Amount of messages the channel can still buffer towards the client.
416 get_channel_buffer (const struct CadetTChannel *tch)
420 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
421 fwd = GMCH_is_origin (tch->ch, GNUNET_YES);
423 return GMCH_get_buffer (tch->ch, fwd);
428 * Get the channel's allowance status.
430 * @param tch Tunnel's channel handle.
432 * @return #GNUNET_YES if we allowed the client to send data to us.
435 get_channel_allowed (const struct CadetTChannel *tch)
439 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
440 fwd = GMCH_is_origin (tch->ch, GNUNET_YES);
442 return GMCH_get_allowed (tch->ch, fwd);
447 * Get the connection's buffer.
449 * @param tc Tunnel's connection handle.
451 * @return Amount of messages the connection can still buffer.
454 get_connection_buffer (const struct CadetTConnection *tc)
458 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
459 fwd = GMC_is_origin (tc->c, GNUNET_YES);
461 return GMC_get_buffer (tc->c, fwd);
466 * Get the connection's allowance.
468 * @param tc Tunnel's connection handle.
470 * @return Amount of messages we have allowed the next peer to send us.
473 get_connection_allowed (const struct CadetTConnection *tc)
477 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
478 fwd = GMC_is_origin (tc->c, GNUNET_YES);
480 return GMC_get_allowed (tc->c, fwd);
485 * Check that a ephemeral key message s well formed and correctly signed.
487 * @param t Tunnel on which the message came.
488 * @param msg The ephemeral key message.
490 * @return GNUNET_OK if message is fine, GNUNET_SYSERR otherwise.
493 check_ephemeral (struct CadetTunnel3 *t,
494 const struct GNUNET_CADET_KX_Ephemeral *msg)
496 /* Check message size */
497 if (ntohs (msg->header.size) != sizeof (struct GNUNET_CADET_KX_Ephemeral))
498 return GNUNET_SYSERR;
500 /* Check signature size */
501 if (ntohl (msg->purpose.size) != ephemeral_purpose_size ())
502 return GNUNET_SYSERR;
505 if (0 != memcmp (&msg->origin_identity,
506 GMP_get_id (t->peer),
507 sizeof (struct GNUNET_PeerIdentity)))
508 return GNUNET_SYSERR;
510 /* Check signature */
512 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_CADET_KX,
515 &msg->origin_identity.public_key))
516 return GNUNET_SYSERR;
523 * Encrypt data with the tunnel key.
525 * @param t Tunnel whose key to use.
526 * @param dst Destination for the encrypted data.
527 * @param src Source of the plaintext. Can overlap with @c dst.
528 * @param size Size of the plaintext.
529 * @param iv Initialization Vector to use.
532 t_encrypt (struct CadetTunnel3 *t,
533 void *dst, const void *src,
534 size_t size, uint32_t iv)
536 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
539 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt start\n");
540 GNUNET_CRYPTO_symmetric_derive_iv (&siv, &t->e_key, &iv, sizeof (iv), NULL);
541 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt IV derived\n");
542 out_size = GNUNET_CRYPTO_symmetric_encrypt (src, size, &t->e_key, &siv, dst);
543 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt end\n");
550 * Decrypt data with the tunnel key.
552 * @param t Tunnel whose key to use.
553 * @param dst Destination for the plaintext.
554 * @param src Source of the encrypted data. Can overlap with @c dst.
555 * @param size Size of the encrypted data.
556 * @param iv Initialization Vector to use.
559 t_decrypt (struct CadetTunnel3 *t,
560 void *dst, const void *src,
561 size_t size, uint32_t iv)
563 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
564 struct GNUNET_CRYPTO_SymmetricSessionKey *key;
567 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt start\n");
568 if (t->estate == CADET_TUNNEL3_KEY_OK || t->estate == CADET_TUNNEL3_KEY_PING)
572 else if (NULL != t->kx_ctx)
574 key = &t->kx_ctx->d_key_old;
578 GNUNET_STATISTICS_update (stats, "# non decryptable data", 1, GNUNET_NO);
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 "WARNING got data on %s without a valid key\n",
586 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt iv\n");
587 GNUNET_CRYPTO_symmetric_derive_iv (&siv, key, &iv, sizeof (iv), NULL);
588 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt iv done\n");
589 out_size = GNUNET_CRYPTO_symmetric_decrypt (src, size, key, &siv, dst);
590 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt end\n");
597 * Create key material by doing ECDH on the local and remote ephemeral keys.
599 * @param key_material Where to store the key material.
600 * @param ephemeral_key Peer's public ephemeral key.
603 derive_key_material (struct GNUNET_HashCode *key_material,
604 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key)
607 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
616 * Create a symmetic key from the identities of both ends and the key material
619 * @param key Destination for the generated key.
620 * @param sender ID of the peer that will encrypt with @c key.
621 * @param receiver ID of the peer that will decrypt with @c key.
622 * @param key_material Hash created with ECDH with the ephemeral keys.
625 derive_symmertic (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
626 const struct GNUNET_PeerIdentity *sender,
627 const struct GNUNET_PeerIdentity *receiver,
628 const struct GNUNET_HashCode *key_material)
630 const char salt[] = "CADET kx salt";
632 GNUNET_CRYPTO_kdf (key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
634 key_material, sizeof (struct GNUNET_HashCode),
635 sender, sizeof (struct GNUNET_PeerIdentity),
636 receiver, sizeof (struct GNUNET_PeerIdentity),
641 * Pick a connection on which send the next data message.
643 * @param t Tunnel on which to send the message.
645 * @return The connection on which to send the next message.
647 static struct CadetConnection *
648 tunnel_get_connection (struct CadetTunnel3 *t)
650 struct CadetTConnection *iter;
651 struct CadetConnection *best;
653 unsigned int lowest_q;
655 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMT_2s (t));
658 for (iter = t->connection_head; NULL != iter; iter = iter->next)
660 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
661 GMC_2s (iter->c), GMC_get_state (iter->c));
662 if (CADET_CONNECTION_READY == GMC_get_state (iter->c))
664 qn = GMC_get_qn (iter->c, GMC_is_origin (iter->c, GNUNET_YES));
665 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
673 LOG (GNUNET_ERROR_TYPE_DEBUG, " selected: connection %s\n", GMC_2s (best));
679 * Callback called when a queued message is sent.
681 * Calculates the average time and connection packet tracking.
683 * @param cls Closure (TunnelQueue handle).
684 * @param c Connection this message was on.
685 * @param q Connection queue handle (unused).
686 * @param type Type of message sent.
687 * @param fwd Was this a FWD going message?
688 * @param size Size of the message.
691 tun_message_sent (void *cls,
692 struct CadetConnection *c,
693 struct CadetConnectionQueue *q,
694 uint16_t type, int fwd, size_t size)
696 struct CadetTunnel3Queue *qt = cls;
697 struct CadetTunnel3 *t;
699 LOG (GNUNET_ERROR_TYPE_DEBUG, "tun_message_sent\n");
701 GNUNET_assert (NULL != qt->cont);
702 t = NULL == c ? NULL : GMC_get_tunnel (c);
703 qt->cont (qt->cont_cls, t, qt, type, size);
709 * Delete a queued message: either was sent or the channel was destroyed
710 * before the tunnel's key exchange had a chance to finish.
712 * @param tqd Delayed queue handle.
715 unqueue_data (struct CadetTunnelDelayed *tqd)
717 GNUNET_CONTAINER_DLL_remove (tqd->t->tq_head, tqd->t->tq_tail, tqd);
723 * Cache a message to be sent once tunnel is online.
725 * @param t Tunnel to hold the message.
726 * @param msg Message itself (copy will be made).
728 static struct CadetTunnelDelayed *
729 queue_data (struct CadetTunnel3 *t, const struct GNUNET_MessageHeader *msg)
731 struct CadetTunnelDelayed *tqd;
732 uint16_t size = ntohs (msg->size);
734 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue data on Tunnel %s\n", GMT_2s (t));
736 if (GNUNET_YES == is_ready (t))
742 tqd = GNUNET_malloc (sizeof (struct CadetTunnelDelayed) + size);
745 memcpy (&tqd[1], msg, size);
746 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tqd);
754 * @param t Tunnel to get keys from.
755 * @param plaintext Content to HMAC.
756 * @param size Size of @c plaintext.
757 * @param iv Initialization vector for the message.
758 * @param outgoing Is this an outgoing message that we encrypted?
759 * @param hmac Destination to store the HMAC.
762 t_hmac (struct CadetTunnel3 *t, const void *plaintext, size_t size, uint32_t iv,
763 int outgoing, struct GNUNET_CADET_Hash *hmac)
765 struct GNUNET_CRYPTO_AuthKey auth_key;
766 static const char ctx[] = "cadet authentication key";
767 struct GNUNET_CRYPTO_SymmetricSessionKey *key;
768 struct GNUNET_HashCode hash;
770 key = outgoing ? &t->e_key : &t->d_key;
771 GNUNET_CRYPTO_hmac_derive_key (&auth_key, key,
776 GNUNET_CRYPTO_hmac (&auth_key, plaintext, size, &hash);
777 memcpy (hmac, &hash, sizeof (*hmac));
782 * Sends an already built message on a tunnel, encrypting it and
783 * choosing the best connection.
785 * @param message Message to send. Function modifies it.
786 * @param t Tunnel on which this message is transmitted.
787 * @param c Connection to use (autoselect if NULL).
788 * @param force Force the tunnel to take the message (buffer overfill).
789 * @param cont Continuation to call once message is really sent.
790 * @param cont_cls Closure for @c cont.
791 * @param existing_q In case this a transmission of previously queued data,
792 * this should be TunnelQueue given to the client.
795 * @return Handle to cancel message. NULL if @c cont is NULL.
797 static struct CadetTunnel3Queue *
798 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
799 struct CadetTunnel3 *t, struct CadetConnection *c,
800 int force, GMT_sent cont, void *cont_cls,
801 struct CadetTunnel3Queue *existing_q)
803 struct CadetTunnel3Queue *tq;
804 struct GNUNET_CADET_Encrypted *msg;
805 size_t size = ntohs (message->size);
806 char cbuf[sizeof (struct GNUNET_CADET_Encrypted) + size];
812 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMT_2s (t));
814 if (GNUNET_NO == is_ready (t))
816 struct CadetTunnelDelayed *tqd;
817 /* A non null existing_q indicates sending of queued data.
818 * Should only happen after tunnel becomes ready.
820 GNUNET_assert (NULL == existing_q);
821 tqd = queue_data (t, message);
824 tq = GNUNET_new (struct CadetTunnel3Queue);
828 tq->cont_cls = cont_cls;
832 GNUNET_assert (GNUNET_NO == GMT_is_loopback (t));
834 iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
835 msg = (struct GNUNET_CADET_Encrypted *) cbuf;
836 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED);
838 GNUNET_assert (t_encrypt (t, &msg[1], message, size, iv) == size);
839 t_hmac (t, &msg[1], size, iv, GNUNET_YES, &msg->hmac);
840 msg->header.size = htons (sizeof (struct GNUNET_CADET_Encrypted) + size);
843 c = tunnel_get_connection (t);
846 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task
847 || CADET_TUNNEL3_SEARCHING != t->cstate)
856 type = ntohs (message->type);
859 case GNUNET_MESSAGE_TYPE_CADET_DATA:
860 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
861 if (GNUNET_MESSAGE_TYPE_CADET_DATA == type)
862 mid = ntohl (((struct GNUNET_CADET_Data *) message)->mid);
864 mid = ntohl (((struct GNUNET_CADET_DataACK *) message)->mid);
866 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
867 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
868 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
869 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
870 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
871 msg->cid = *GMC_get_id (c);
872 msg->ttl = htonl (default_ttl);
877 LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s\n", GM_m2s (type));
879 fwd = GMC_is_origin (c, GNUNET_YES);
883 GNUNET_break (NULL ==
884 GMC_send_prebuilt_message (&msg->header, type, mid,
885 c, fwd, force, NULL, NULL));
888 if (NULL == existing_q)
890 tq = GNUNET_new (struct CadetTunnel3Queue); /* FIXME valgrind: leak*/
897 tq->cq = GMC_send_prebuilt_message (&msg->header, type, mid, c, fwd, force,
898 &tun_message_sent, tq);
900 tq->cont_cls = cont_cls;
907 * Send all cached messages that we can, tunnel is online.
909 * @param t Tunnel that holds the messages. Cannot be loopback.
912 send_queued_data (struct CadetTunnel3 *t)
914 struct CadetTunnelDelayed *tqd;
915 struct CadetTunnelDelayed *next;
918 LOG (GNUNET_ERROR_TYPE_DEBUG,
919 "GMT_send_queued_data on tunnel %s\n",
922 if (GMT_is_loopback (t))
928 if (GNUNET_NO == is_ready (t))
930 LOG (GNUNET_ERROR_TYPE_DEBUG, " not ready yet: %s/%s\n",
931 estate2s (t->estate), cstate2s (t->cstate));
935 room = GMT_get_connections_buffer (t);
936 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
937 LOG (GNUNET_ERROR_TYPE_DEBUG, " tq head: %p\n", t->tq_head);
938 for (tqd = t->tq_head; NULL != tqd && room > 0; tqd = next)
940 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending queued data\n");
943 send_prebuilt_message ((struct GNUNET_MessageHeader *) &tqd[1],
944 tqd->t, NULL, GNUNET_YES,
945 NULL != tqd->tq ? tqd->tq->cont : NULL,
946 NULL != tqd->tq ? tqd->tq->cont_cls : NULL,
950 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_send_queued_data end\n", GMP_2s (t->peer));
955 * Sends key exchange message on a tunnel, choosing the best connection.
956 * Should not be called on loopback tunnels.
958 * @param t Tunnel on which this message is transmitted.
959 * @param message Message to send. Function modifies it.
962 send_kx (struct CadetTunnel3 *t,
963 const struct GNUNET_MessageHeader *message)
965 struct CadetConnection *c;
966 struct GNUNET_CADET_KX *msg;
967 size_t size = ntohs (message->size);
968 char cbuf[sizeof (struct GNUNET_CADET_KX) + size];
972 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT KX on Tunnel %s\n", GMT_2s (t));
974 /* Avoid loopback. */
975 if (GMT_is_loopback (t))
977 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
982 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
984 LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother\n");
988 /* Must have a connection. */
989 if (NULL == t->connection_head)
991 GNUNET_break (CADET_TUNNEL3_SEARCHING == t->cstate);
996 msg = (struct GNUNET_CADET_KX *) cbuf;
997 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX);
998 msg->header.size = htons (sizeof (struct GNUNET_CADET_KX) + size);
999 c = tunnel_get_connection (t);
1002 GNUNET_break (GNUNET_SCHEDULER_NO_TASK != t->destroy_task
1003 || CADET_TUNNEL3_READY != t->cstate);
1007 type = ntohs (message->type);
1010 case GNUNET_MESSAGE_TYPE_CADET_KX_EPHEMERAL:
1011 case GNUNET_MESSAGE_TYPE_CADET_KX_PING:
1012 case GNUNET_MESSAGE_TYPE_CADET_KX_PONG:
1013 memcpy (&msg[1], message, size);
1016 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1021 fwd = GMC_is_origin (t->connection_head->c, GNUNET_YES);
1022 /* TODO save handle and cancel in case of a unneeded retransmission */
1023 GMC_send_prebuilt_message (&msg->header, GNUNET_MESSAGE_TYPE_CADET_KX,
1024 message->type, c, fwd, GNUNET_YES, NULL, NULL);
1029 * Send the ephemeral key on a tunnel.
1031 * @param t Tunnel on which to send the key.
1034 send_ephemeral (struct CadetTunnel3 *t)
1036 LOG (GNUNET_ERROR_TYPE_INFO, "=> EPHM for %s\n", GMT_2s (t));
1038 kx_msg.sender_status = htonl (t->estate);
1039 send_kx (t, &kx_msg.header);
1043 * Send a ping message on a tunnel.
1045 * @param t Tunnel on which to send the ping.
1048 send_ping (struct CadetTunnel3 *t)
1050 struct GNUNET_CADET_KX_Ping msg;
1052 LOG (GNUNET_ERROR_TYPE_INFO, "=> PING for %s\n", GMT_2s (t));
1053 msg.header.size = htons (sizeof (msg));
1054 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX_PING);
1055 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1056 msg.target = *GMP_get_id (t->peer);
1057 msg.nonce = t->kx_ctx->challenge;
1059 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
1060 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n", GNUNET_i2s (&msg.target));
1061 t_encrypt (t, &msg.target, &msg.target, ping_encryption_size(), msg.iv);
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
1063 LOG (GNUNET_ERROR_TYPE_DEBUG, " e towards %s\n", GNUNET_i2s (&msg.target));
1065 send_kx (t, &msg.header);
1070 * Send a pong message on a tunnel.
1072 * @param t Tunnel on which to send the pong.
1073 * @param challenge Value sent in the ping that we have to send back.
1076 send_pong (struct CadetTunnel3 *t, uint32_t challenge)
1078 struct GNUNET_CADET_KX_Pong msg;
1080 LOG (GNUNET_ERROR_TYPE_INFO, "=> PONG for %s\n", GMT_2s (t));
1081 msg.header.size = htons (sizeof (msg));
1082 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX_PONG);
1083 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1084 msg.nonce = challenge;
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
1086 t_encrypt (t, &msg.nonce, &msg.nonce, sizeof (msg.nonce), msg.iv);
1087 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
1089 send_kx (t, &msg.header);
1094 * Initiate a rekey with the remote peer.
1096 * @param cls Closure (tunnel).
1097 * @param tc TaskContext.
1100 rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1102 struct CadetTunnel3 *t = cls;
1104 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
1106 LOG (GNUNET_ERROR_TYPE_DEBUG, "Re-key Tunnel %s\n", GMT_2s (t));
1107 if (NULL != tc && 0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1110 if (NULL == t->kx_ctx)
1112 LOG (GNUNET_ERROR_TYPE_DEBUG, " new kx ctx\n");
1113 t->kx_ctx = GNUNET_new (struct CadetTunnelKXCtx);
1114 t->kx_ctx->challenge =
1115 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1116 t->kx_ctx->d_key_old = t->d_key;
1117 LOG (GNUNET_ERROR_TYPE_DEBUG, " new challenge for %s: %u\n",
1118 GMT_2s (t), t->kx_ctx->challenge);
1123 case CADET_TUNNEL3_KEY_UNINITIALIZED:
1124 t->estate = CADET_TUNNEL3_KEY_SENT;
1126 case CADET_TUNNEL3_KEY_SENT:
1128 case CADET_TUNNEL3_KEY_PING:
1129 case CADET_TUNNEL3_KEY_OK:
1131 t->estate = CADET_TUNNEL3_KEY_PING;
1134 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unexpected state %u\n", t->estate);
1137 LOG (GNUNET_ERROR_TYPE_DEBUG, " next call in %s\n",
1138 GNUNET_STRINGS_relative_time_to_string (REKEY_WAIT, GNUNET_YES));
1139 t->rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_WAIT, &rekey_tunnel, t);
1144 * Out ephemeral key has changed, create new session key on all tunnels.
1146 * @param cls Closure (size of the hashmap).
1147 * @param key Current public key.
1148 * @param value Value in the hash map (tunnel).
1150 * @return #GNUNET_YES, so we should continue to iterate,
1153 rekey_iterator (void *cls,
1154 const struct GNUNET_PeerIdentity *key,
1157 struct CadetTunnel3 *t = value;
1158 struct GNUNET_TIME_Relative delay;
1159 long n = (long) cls;
1162 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
1165 if (GNUNET_YES == GMT_is_loopback (t))
1168 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t) n * 100);
1169 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, r);
1170 t->rekey_task = GNUNET_SCHEDULER_add_delayed (delay, &rekey_tunnel, t);
1177 * Create a new ephemeral key and key message, schedule next rekeying.
1179 * @param cls Closure (unused).
1180 * @param tc TaskContext.
1183 rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1185 struct GNUNET_TIME_Absolute time;
1188 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1190 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1193 GNUNET_free_non_null (my_ephemeral_key);
1194 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1196 time = GNUNET_TIME_absolute_get ();
1197 kx_msg.creation_time = GNUNET_TIME_absolute_hton (time);
1198 time = GNUNET_TIME_absolute_add (time, rekey_period);
1199 time = GNUNET_TIME_absolute_add (time, GNUNET_TIME_UNIT_MINUTES);
1200 kx_msg.expiration_time = GNUNET_TIME_absolute_hton (time);
1201 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key, &kx_msg.ephemeral_key);
1203 GNUNET_assert (GNUNET_OK ==
1204 GNUNET_CRYPTO_eddsa_sign (my_private_key,
1206 &kx_msg.signature));
1208 n = (long) GNUNET_CONTAINER_multipeermap_size (tunnels);
1209 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &rekey_iterator, (void *) n);
1211 rekey_task = GNUNET_SCHEDULER_add_delayed (rekey_period, &rekey, NULL);
1216 * Called only on shutdown, destroy every tunnel.
1218 * @param cls Closure (unused).
1219 * @param key Current public key.
1220 * @param value Value in the hash map (tunnel).
1222 * @return #GNUNET_YES, so we should continue to iterate,
1225 destroy_iterator (void *cls,
1226 const struct GNUNET_PeerIdentity *key,
1229 struct CadetTunnel3 *t = value;
1231 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_shutdown destroying tunnel at %p\n", t);
1238 * Notify remote peer that we don't know a channel he is talking about,
1239 * probably CHANNEL_DESTROY was missed.
1241 * @param t Tunnel on which to notify.
1242 * @param gid ID of the channel.
1245 send_channel_destroy (struct CadetTunnel3 *t, unsigned int gid)
1247 struct GNUNET_CADET_ChannelManage msg;
1249 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1250 msg.header.size = htons (sizeof (msg));
1251 msg.chid = htonl (gid);
1253 LOG (GNUNET_ERROR_TYPE_DEBUG,
1254 "WARNING destroying unknown channel %u on tunnel %s\n",
1256 send_prebuilt_message (&msg.header, t, NULL, GNUNET_YES, NULL, NULL, NULL);
1261 * Demultiplex data per channel and call appropriate channel handler.
1263 * @param t Tunnel on which the data came.
1264 * @param msg Data message.
1265 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1266 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1267 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1268 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1271 handle_data (struct CadetTunnel3 *t,
1272 const struct GNUNET_CADET_Data *msg,
1275 struct CadetChannel *ch;
1279 size = ntohs (msg->header.size);
1281 sizeof (struct GNUNET_CADET_Data) +
1282 sizeof (struct GNUNET_MessageHeader))
1287 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
1288 GM_m2s (ntohs (msg[1].header.type)));
1291 ch = GMT_get_channel (t, ntohl (msg->chid));
1294 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
1296 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel 0x%X unknown\n",
1298 send_channel_destroy (t, ntohl (msg->chid));
1302 GMCH_handle_data (ch, msg, fwd);
1307 * Demultiplex data ACKs per channel and update appropriate channel buffer info.
1309 * @param t Tunnel on which the DATA ACK came.
1310 * @param msg DATA ACK message.
1311 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1312 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1313 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1314 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1317 handle_data_ack (struct CadetTunnel3 *t,
1318 const struct GNUNET_CADET_DataACK *msg,
1321 struct CadetChannel *ch;
1325 size = ntohs (msg->header.size);
1326 if (size != sizeof (struct GNUNET_CADET_DataACK))
1333 ch = GMT_get_channel (t, ntohl (msg->chid));
1336 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1343 GMCH_handle_data_ack (ch, msg, fwd);
1348 * Handle channel create.
1350 * @param t Tunnel on which the data came.
1351 * @param msg Data message.
1354 handle_ch_create (struct CadetTunnel3 *t,
1355 const struct GNUNET_CADET_ChannelCreate *msg)
1357 struct CadetChannel *ch;
1361 size = ntohs (msg->header.size);
1362 if (size != sizeof (struct GNUNET_CADET_ChannelCreate))
1369 ch = GMT_get_channel (t, ntohl (msg->chid));
1370 if (NULL != ch && ! GMT_is_loopback (t))
1372 /* Probably a retransmission, safe to ignore */
1373 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
1375 ch = GMCH_handle_create (t, msg);
1377 GMT_add_channel (t, ch);
1383 * Handle channel NACK: check correctness and call channel handler for NACKs.
1385 * @param t Tunnel on which the NACK came.
1386 * @param msg NACK message.
1389 handle_ch_nack (struct CadetTunnel3 *t,
1390 const struct GNUNET_CADET_ChannelManage *msg)
1392 struct CadetChannel *ch;
1396 size = ntohs (msg->header.size);
1397 if (size != sizeof (struct GNUNET_CADET_ChannelManage))
1404 ch = GMT_get_channel (t, ntohl (msg->chid));
1407 GNUNET_STATISTICS_update (stats, "# channel NACK on unknown channel",
1409 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1414 GMCH_handle_nack (ch);
1419 * Handle a CHANNEL ACK (SYNACK/ACK).
1421 * @param t Tunnel on which the CHANNEL ACK came.
1422 * @param msg CHANNEL ACK message.
1423 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1424 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1425 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1426 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1429 handle_ch_ack (struct CadetTunnel3 *t,
1430 const struct GNUNET_CADET_ChannelManage *msg,
1433 struct CadetChannel *ch;
1437 size = ntohs (msg->header.size);
1438 if (size != sizeof (struct GNUNET_CADET_ChannelManage))
1445 ch = GMT_get_channel (t, ntohl (msg->chid));
1448 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
1450 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1455 GMCH_handle_ack (ch, msg, fwd);
1461 * Handle a channel destruction message.
1463 * @param t Tunnel on which the message came.
1464 * @param msg Channel destroy message.
1465 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1466 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1467 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1468 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1471 handle_ch_destroy (struct CadetTunnel3 *t,
1472 const struct GNUNET_CADET_ChannelManage *msg,
1475 struct CadetChannel *ch;
1479 size = ntohs (msg->header.size);
1480 if (size != sizeof (struct GNUNET_CADET_ChannelManage))
1487 ch = GMT_get_channel (t, ntohl (msg->chid));
1490 /* Probably a retransmission, safe to ignore */
1494 GMCH_handle_destroy (ch, msg, fwd);
1499 * The peer's ephemeral key has changed: update the symmetrical keys.
1501 * @param t Tunnel this message came on.
1502 * @param msg Key eXchange message.
1505 handle_ephemeral (struct CadetTunnel3 *t,
1506 const struct GNUNET_CADET_KX_Ephemeral *msg)
1508 struct GNUNET_HashCode km;
1509 LOG (GNUNET_ERROR_TYPE_INFO, "<=== EPHM for %s\n", GMT_2s (t));
1511 if (GNUNET_OK != check_ephemeral (t, msg))
1513 GNUNET_break_op (0);
1516 derive_key_material (&km, &msg->ephemeral_key);
1517 LOG (GNUNET_ERROR_TYPE_DEBUG, " km is %s\n", GNUNET_h2s (&km));
1518 derive_symmertic (&t->e_key, &my_full_id, GMP_get_id (t->peer), &km);
1519 derive_symmertic (&t->d_key, GMP_get_id (t->peer), &my_full_id, &km);
1520 if (CADET_TUNNEL3_KEY_SENT == t->estate)
1522 LOG (GNUNET_ERROR_TYPE_DEBUG, " our key was sent, send ping\n");
1524 t->estate = CADET_TUNNEL3_KEY_PING;
1530 * Peer wants to check our symmetrical keys by sending an encrypted challenge.
1531 * Answer with by retransmitting the challenge with the "opposite" key.
1533 * @param t Tunnel this message came on.
1534 * @param msg Key eXchange Ping message.
1537 handle_ping (struct CadetTunnel3 *t,
1538 const struct GNUNET_CADET_KX_Ping *msg)
1540 struct GNUNET_CADET_KX_Ping res;
1542 if (ntohs (msg->header.size) != sizeof (res))
1544 GNUNET_break_op (0);
1548 LOG (GNUNET_ERROR_TYPE_INFO, "<=== PING for %s\n", GMT_2s (t));
1549 t_decrypt (t, &res.target, &msg->target, ping_encryption_size (), msg->iv);
1550 if (0 != memcmp (&my_full_id, &res.target, sizeof (my_full_id)))
1552 // FIXME: move to debug
1553 GNUNET_STATISTICS_update (stats, "# malformed PINGs", 1, GNUNET_NO);
1554 LOG (GNUNET_ERROR_TYPE_WARNING, " malformed PING on %s\n", GMT_2s (t));
1555 LOG (GNUNET_ERROR_TYPE_WARNING, " e got %u\n", msg->nonce);
1556 LOG (GNUNET_ERROR_TYPE_WARNING, " e towards %s\n", GNUNET_i2s (&msg->target));
1557 LOG (GNUNET_ERROR_TYPE_WARNING, " got %u\n", res.nonce);
1558 LOG (GNUNET_ERROR_TYPE_WARNING, " towards %s\n", GNUNET_i2s (&res.target));
1562 send_pong (t, res.nonce);
1567 * Peer has answer to our challenge.
1568 * If answer is successful, consider the key exchange finished and clean
1569 * up all related state.
1571 * @param t Tunnel this message came on.
1572 * @param msg Key eXchange Pong message.
1575 handle_pong (struct CadetTunnel3 *t,
1576 const struct GNUNET_CADET_KX_Pong *msg)
1580 LOG (GNUNET_ERROR_TYPE_INFO, "<=== PONG for %s\n", GMT_2s (t));
1581 if (GNUNET_SCHEDULER_NO_TASK == t->rekey_task)
1583 GNUNET_STATISTICS_update (stats, "# duplicate PONG messages", 1, GNUNET_NO);
1586 t_decrypt (t, &challenge, &msg->nonce, sizeof (uint32_t), msg->iv);
1588 if (challenge != t->kx_ctx->challenge)
1590 LOG (GNUNET_ERROR_TYPE_WARNING, "Wrong PONG challenge\n");
1591 LOG (GNUNET_ERROR_TYPE_DEBUG, "PONG: %u (e: %u). Expected: %u.\n",
1592 challenge, msg->nonce, t->kx_ctx->challenge);
1593 GNUNET_break_op (0);
1596 GNUNET_SCHEDULER_cancel (t->rekey_task);
1597 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
1598 GNUNET_free (t->kx_ctx);
1600 GMT_change_estate (t, CADET_TUNNEL3_KEY_OK);
1605 * Demultiplex by message type and call appropriate handler for a message
1606 * towards a channel of a local tunnel.
1608 * @param t Tunnel this message came on.
1609 * @param msgh Message header.
1610 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1611 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1612 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1613 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1616 handle_decrypted (struct CadetTunnel3 *t,
1617 const struct GNUNET_MessageHeader *msgh,
1622 type = ntohs (msgh->type);
1623 LOG (GNUNET_ERROR_TYPE_INFO, "<=== %s on %s\n", GM_m2s (type), GMT_2s (t));
1627 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
1628 /* Do nothing, connection aleady got updated. */
1629 GNUNET_STATISTICS_update (stats, "# keepalives received", 1, GNUNET_NO);
1632 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1633 /* Don't send hop ACK, wait for client to ACK */
1634 handle_data (t, (struct GNUNET_CADET_Data *) msgh, fwd);
1637 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
1638 handle_data_ack (t, (struct GNUNET_CADET_DataACK *) msgh, fwd);
1641 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1642 handle_ch_create (t,
1643 (struct GNUNET_CADET_ChannelCreate *) msgh);
1646 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
1648 (struct GNUNET_CADET_ChannelManage *) msgh);
1651 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
1653 (struct GNUNET_CADET_ChannelManage *) msgh,
1657 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1658 handle_ch_destroy (t,
1659 (struct GNUNET_CADET_ChannelManage *) msgh,
1664 GNUNET_break_op (0);
1665 LOG (GNUNET_ERROR_TYPE_WARNING,
1666 "end-to-end message not known (%u)\n",
1667 ntohs (msgh->type));
1672 /******************************************************************************/
1673 /******************************** API ***********************************/
1674 /******************************************************************************/
1677 * Decrypt and demultiplex by message type. Call appropriate handler
1678 * for every message.
1680 * @param t Tunnel this message came on.
1681 * @param msg Encrypted message.
1684 GMT_handle_encrypted (struct CadetTunnel3 *t,
1685 const struct GNUNET_CADET_Encrypted *msg)
1687 size_t size = ntohs (msg->header.size);
1688 size_t payload_size = size - sizeof (struct GNUNET_CADET_Encrypted);
1689 size_t decrypted_size;
1690 char cbuf [payload_size];
1691 struct GNUNET_MessageHeader *msgh;
1693 struct GNUNET_CADET_Hash hmac;
1695 decrypted_size = t_decrypt (t, cbuf, &msg[1], payload_size, msg->iv);
1696 t_hmac (t, &msg[1], payload_size, msg->iv, GNUNET_NO, &hmac);
1697 if (0 != memcmp (&hmac, &msg->hmac, sizeof (hmac)))
1699 /* checksum failed */
1700 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1701 "Failed checksum validation for a message on tunnel `%s'\n",
1703 GNUNET_STATISTICS_update (stats, "# wrong HMAC", 1, GNUNET_NO);
1707 while (off < decrypted_size)
1709 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1710 handle_decrypted (t, msgh, GNUNET_SYSERR);
1711 off += ntohs (msgh->size);
1717 * Demultiplex an encapsulated KX message by message type.
1719 * @param t Tunnel on which the message came.
1720 * @param message Payload of KX message.
1723 GMT_handle_kx (struct CadetTunnel3 *t,
1724 const struct GNUNET_MessageHeader *message)
1728 type = ntohs (message->type);
1729 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message received\n", type);
1732 case GNUNET_MESSAGE_TYPE_CADET_KX_EPHEMERAL:
1733 handle_ephemeral (t, (struct GNUNET_CADET_KX_Ephemeral *) message);
1736 case GNUNET_MESSAGE_TYPE_CADET_KX_PING:
1737 handle_ping (t, (struct GNUNET_CADET_KX_Ping *) message);
1740 case GNUNET_MESSAGE_TYPE_CADET_KX_PONG:
1741 handle_pong (t, (struct GNUNET_CADET_KX_Pong *) message);
1745 GNUNET_break_op (0);
1746 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message not known (%u)\n", type);
1752 * Initialize the tunnel subsystem.
1754 * @param c Configuration handle.
1755 * @param key ECC private key, to derive all other keys and do crypto.
1758 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
1759 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
1761 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1763 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DEFAULT_TTL",
1766 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1767 "CADET", "DEFAULT_TTL", "USING DEFAULT");
1771 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REKEY_PERIOD",
1774 rekey_period = GNUNET_TIME_UNIT_DAYS;
1777 my_private_key = key;
1778 kx_msg.header.size = htons (sizeof (kx_msg));
1779 kx_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX_EPHEMERAL);
1780 kx_msg.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CADET_KX);
1781 kx_msg.purpose.size = htonl (ephemeral_purpose_size ());
1782 kx_msg.origin_identity = my_full_id;
1783 rekey_task = GNUNET_SCHEDULER_add_now (&rekey, NULL);
1785 tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
1790 * Shut down the tunnel subsystem.
1795 if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
1797 GNUNET_SCHEDULER_cancel (rekey_task);
1798 rekey_task = GNUNET_SCHEDULER_NO_TASK;
1800 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &destroy_iterator, NULL);
1801 GNUNET_CONTAINER_multipeermap_destroy (tunnels);
1808 * @param destination Peer this tunnel is towards.
1810 struct CadetTunnel3 *
1811 GMT_new (struct CadetPeer *destination)
1813 struct CadetTunnel3 *t;
1815 t = GNUNET_new (struct CadetTunnel3);
1817 t->peer = destination;
1820 GNUNET_CONTAINER_multipeermap_put (tunnels, GMP_get_id (destination), t,
1821 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1832 * Change the tunnel's connection state.
1834 * @param t Tunnel whose connection state to change.
1835 * @param cstate New connection state.
1838 GMT_change_cstate (struct CadetTunnel3* t, enum CadetTunnel3CState cstate)
1842 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s cstate %s => %s\n",
1843 GMP_2s (t->peer), cstate2s (t->cstate), cstate2s (cstate));
1844 if (myid != GMP_get_short_id (t->peer) &&
1845 CADET_TUNNEL3_READY != t->cstate &&
1846 CADET_TUNNEL3_READY == cstate)
1849 if (CADET_TUNNEL3_KEY_OK == t->estate)
1851 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered send queued data\n");
1852 send_queued_data (t);
1854 else if (CADET_TUNNEL3_KEY_UNINITIALIZED == t->estate)
1856 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered rekey\n");
1857 rekey_tunnel (t, NULL);
1862 if (CADET_TUNNEL3_READY == cstate
1863 && CONNECTIONS_PER_TUNNEL <= GMT_count_connections (t))
1865 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered stop dht\n");
1866 GMP_stop_search (t->peer);
1871 * Change the tunnel encryption state.
1873 * @param t Tunnel whose encryption state to change.
1874 * @param state New encryption state.
1877 GMT_change_estate (struct CadetTunnel3* t, enum CadetTunnel3EState state)
1881 LOG (GNUNET_ERROR_TYPE_DEBUG,
1882 "Tunnel %s estate was %s\n",
1883 GMP_2s (t->peer), estate2s (t->estate));
1884 LOG (GNUNET_ERROR_TYPE_DEBUG,
1885 "Tunnel %s estate is now %s\n",
1886 GMP_2s (t->peer), estate2s (state));
1887 if (myid != GMP_get_short_id (t->peer) &&
1888 CADET_TUNNEL3_KEY_OK != t->estate && CADET_TUNNEL3_KEY_OK == state)
1891 send_queued_data (t);
1899 * @brief Check if tunnel has too many connections, and remove one if necessary.
1901 * Currently this means the newest connection, unless it is a direct one.
1902 * Implemented as a task to avoid freeing a connection that is in the middle
1903 * of being created/processed.
1905 * @param cls Closure (Tunnel to check).
1906 * @param tc Task context.
1909 trim_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1911 struct CadetTunnel3 *t = cls;
1913 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1916 if (GMT_count_connections (t) > 2 * CONNECTIONS_PER_TUNNEL)
1918 struct CadetTConnection *iter;
1919 struct CadetTConnection *c;
1921 for (c = iter = t->connection_head; NULL != iter; iter = iter->next)
1923 if ((NULL == c || iter->created.abs_value_us > c->created.abs_value_us)
1924 && GNUNET_NO == GMC_is_direct (iter->c))
1931 LOG (GNUNET_ERROR_TYPE_DEBUG, "Too many connections on tunnel %s\n",
1933 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying connection %s\n",
1946 * Add a connection to a tunnel.
1949 * @param c Connection.
1952 GMT_add_connection (struct CadetTunnel3 *t, struct CadetConnection *c)
1954 struct CadetTConnection *aux;
1956 GNUNET_assert (NULL != c);
1958 LOG (GNUNET_ERROR_TYPE_DEBUG, "add connection %s\n", GMC_2s (c));
1959 LOG (GNUNET_ERROR_TYPE_DEBUG, " to tunnel %s\n", GMT_2s (t));
1960 for (aux = t->connection_head; aux != NULL; aux = aux->next)
1964 aux = GNUNET_new (struct CadetTConnection);
1966 aux->created = GNUNET_TIME_absolute_get ();
1968 GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, aux);
1970 GNUNET_SCHEDULER_add_now (&trim_connections, t);
1975 * Mark a path as no longer valid for this tunnel: has been tried and failed.
1977 * @param t Tunnel to update.
1978 * @param path Invalid path to remove. Is destroyed after removal.
1981 GMT_remove_path (struct CadetTunnel3 *t, struct CadetPeerPath *path)
1983 GMP_remove_path (t->peer, path);
1988 * Remove a connection from a tunnel.
1991 * @param c Connection.
1994 GMT_remove_connection (struct CadetTunnel3 *t,
1995 struct CadetConnection *c)
1997 struct CadetTConnection *aux;
1998 struct CadetTConnection *next;
2000 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing connection %s from tunnel %s\n",
2001 GMC_2s (c), GMT_2s (t));
2002 for (aux = t->connection_head; aux != NULL; aux = next)
2007 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
2012 /* Start new connections if needed */
2013 if (CONNECTIONS_PER_TUNNEL < GMT_count_connections (t)
2014 && GNUNET_SCHEDULER_NO_TASK == t->destroy_task
2015 && CADET_TUNNEL3_SHUTDOWN != t->cstate
2016 && GNUNET_NO == shutting_down)
2018 LOG (GNUNET_ERROR_TYPE_DEBUG, " no more connections, getting new ones\n");
2019 t->cstate = CADET_TUNNEL3_SEARCHING;
2020 GMP_connect (t->peer);
2024 /* If not marked as ready, no change is needed */
2025 if (CADET_TUNNEL3_READY != t->cstate)
2028 /* Check if any connection is ready to maintaing cstate */
2029 for (aux = t->connection_head; aux != NULL; aux = aux->next)
2030 if (CADET_CONNECTION_READY == GMC_get_state (aux->c))
2033 t->cstate = CADET_TUNNEL3_WAITING;
2038 * Add a channel to a tunnel.
2041 * @param ch Channel.
2044 GMT_add_channel (struct CadetTunnel3 *t, struct CadetChannel *ch)
2046 struct CadetTChannel *aux;
2048 GNUNET_assert (NULL != ch);
2050 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
2052 for (aux = t->channel_head; aux != NULL; aux = aux->next)
2054 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
2059 aux = GNUNET_new (struct CadetTChannel);
2061 LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
2062 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
2064 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
2066 GNUNET_SCHEDULER_cancel (t->destroy_task);
2067 t->destroy_task = GNUNET_SCHEDULER_NO_TASK;
2068 LOG (GNUNET_ERROR_TYPE_DEBUG, " undo destroy!\n");
2074 * Remove a channel from a tunnel.
2077 * @param ch Channel.
2080 GMT_remove_channel (struct CadetTunnel3 *t, struct CadetChannel *ch)
2082 struct CadetTChannel *aux;
2084 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
2085 for (aux = t->channel_head; aux != NULL; aux = aux->next)
2089 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GMCH_2s (ch));
2090 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
2099 * Search for a channel by global ID.
2101 * @param t Tunnel containing the channel.
2102 * @param chid Public channel number.
2104 * @return channel handler, NULL if doesn't exist
2106 struct CadetChannel *
2107 GMT_get_channel (struct CadetTunnel3 *t, CADET_ChannelNumber chid)
2109 struct CadetTChannel *iter;
2114 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2116 if (GMCH_get_id (iter->ch) == chid)
2120 return NULL == iter ? NULL : iter->ch;
2125 * @brief Destroy a tunnel and free all resources.
2127 * Should only be called a while after the tunnel has been marked as destroyed,
2128 * in case there is a new channel added to the same peer shortly after marking
2129 * the tunnel. This way we avoid a new public key handshake.
2131 * @param cls Closure (tunnel to destroy).
2132 * @param tc Task context.
2135 delayed_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2137 struct CadetTunnel3 *t = cls;
2138 struct CadetTConnection *iter;
2140 LOG (GNUNET_ERROR_TYPE_DEBUG, "delayed destroying tunnel %p\n", t);
2141 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2143 LOG (GNUNET_ERROR_TYPE_WARNING,
2144 "Not destroying tunnel, due to shutdown. "
2145 "Tunnel at %p should have been freed by GMT_shutdown\n", t);
2148 t->destroy_task = GNUNET_SCHEDULER_NO_TASK;
2149 t->cstate = CADET_TUNNEL3_SHUTDOWN;
2151 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2153 GMC_send_destroy (iter->c);
2160 * Tunnel is empty: destroy it.
2162 * Notifies all connections about the destruction.
2164 * @param t Tunnel to destroy.
2167 GMT_destroy_empty (struct CadetTunnel3 *t)
2169 if (GNUNET_YES == shutting_down)
2170 return; /* Will be destroyed immediately anyway */
2172 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
2174 LOG (GNUNET_ERROR_TYPE_DEBUG,
2175 "Tunnel %s is already scheduled for destruction\n",
2178 /* should never happen, tunnel can only become empty once, and the
2179 * task identifier should be NO_TASK (cleaned when the tunnel was created
2180 * or became un-empty)
2185 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s empty: destroying scheduled\n",
2188 t->destroy_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
2189 &delayed_destroy, t);
2190 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled destroy of %p as %llX\n",
2191 t, t->destroy_task);
2196 * Destroy tunnel if empty (no more channels).
2198 * @param t Tunnel to destroy if empty.
2201 GMT_destroy_if_empty (struct CadetTunnel3 *t)
2203 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s destroy if empty\n", GMT_2s (t));
2204 if (1 < GMT_count_channels (t))
2207 GMT_destroy_empty (t);
2212 * Destroy the tunnel.
2214 * This function does not generate any warning traffic to clients or peers.
2217 * Cancel messages belonging to this tunnel queued to neighbors.
2218 * Free any allocated resources linked to the tunnel.
2220 * @param t The tunnel to destroy.
2223 GMT_destroy (struct CadetTunnel3 *t)
2225 struct CadetTConnection *iter_c;
2226 struct CadetTConnection *next_c;
2227 struct CadetTChannel *iter_ch;
2228 struct CadetTChannel *next_ch;
2233 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
2235 GNUNET_break (GNUNET_YES ==
2236 GNUNET_CONTAINER_multipeermap_remove (tunnels,
2237 GMP_get_id (t->peer), t));
2239 for (iter_c = t->connection_head; NULL != iter_c; iter_c = next_c)
2241 next_c = iter_c->next;
2242 GMC_destroy (iter_c->c);
2244 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = next_ch)
2246 next_ch = iter_ch->next;
2247 GMCH_destroy (iter_ch->ch);
2248 /* Should only happen on shutdown, but it's ok. */
2251 if (GNUNET_SCHEDULER_NO_TASK != t->destroy_task)
2253 LOG (GNUNET_ERROR_TYPE_DEBUG, "cancelling %llX\n", t->destroy_task);
2254 GNUNET_SCHEDULER_cancel (t->destroy_task);
2255 t->destroy_task = GNUNET_SCHEDULER_NO_TASK;
2258 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2259 GMP_set_tunnel (t->peer, NULL);
2261 if (GNUNET_SCHEDULER_NO_TASK != t->rekey_task)
2263 GNUNET_SCHEDULER_cancel (t->rekey_task);
2264 t->rekey_task = GNUNET_SCHEDULER_NO_TASK;
2265 if (NULL != t->kx_ctx)
2266 GNUNET_free (t->kx_ctx);
2276 * @brief Use the given path for the tunnel.
2277 * Update the next and prev hops (and RCs).
2278 * (Re)start the path refresh in case the tunnel is locally owned.
2280 * @param t Tunnel to update.
2281 * @param p Path to use.
2283 * @return Connection created.
2285 struct CadetConnection *
2286 GMT_use_path (struct CadetTunnel3 *t, struct CadetPeerPath *p)
2288 struct CadetConnection *c;
2289 struct GNUNET_CADET_Hash cid;
2290 unsigned int own_pos;
2292 if (NULL == t || NULL == p)
2298 if (CADET_TUNNEL3_SHUTDOWN == t->cstate)
2304 for (own_pos = 0; own_pos < p->length; own_pos++)
2306 if (p->peers[own_pos] == myid)
2309 if (own_pos >= p->length)
2311 GNUNET_break_op (0);
2315 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE, &cid, sizeof (cid));
2316 c = GMC_new (&cid, t, p, own_pos);
2319 /* Path was flawed */
2322 GMT_add_connection (t, c);
2328 * Count established (ready) connections of a tunnel.
2330 * @param t Tunnel on which to count.
2332 * @return Number of connections.
2335 GMT_count_connections (struct CadetTunnel3 *t)
2337 struct CadetTConnection *iter;
2343 for (count = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
2344 if (CADET_CONNECTION_DESTROYED != GMC_get_state (iter->c))
2351 * Count channels of a tunnel.
2353 * @param t Tunnel on which to count.
2355 * @return Number of channels.
2358 GMT_count_channels (struct CadetTunnel3 *t)
2360 struct CadetTChannel *iter;
2363 for (count = 0, iter = t->channel_head;
2365 iter = iter->next, count++) /* skip */;
2372 * Get the connectivity state of a tunnel.
2376 * @return Tunnel's connectivity state.
2378 enum CadetTunnel3CState
2379 GMT_get_cstate (struct CadetTunnel3 *t)
2384 return (enum CadetTunnel3CState) -1;
2391 * Get the encryption state of a tunnel.
2395 * @return Tunnel's encryption state.
2397 enum CadetTunnel3EState
2398 GMT_get_estate (struct CadetTunnel3 *t)
2403 return (enum CadetTunnel3EState) -1;
2409 * Get the maximum buffer space for a tunnel towards a local client.
2413 * @return Biggest buffer space offered by any channel in the tunnel.
2416 GMT_get_channels_buffer (struct CadetTunnel3 *t)
2418 struct CadetTChannel *iter;
2419 unsigned int buffer;
2420 unsigned int ch_buf;
2422 if (NULL == t->channel_head)
2424 /* Probably getting buffer for a channel create/handshake. */
2429 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2431 ch_buf = get_channel_buffer (iter);
2432 if (ch_buf > buffer)
2440 * Get the total buffer space for a tunnel for P2P traffic.
2444 * @return Buffer space offered by all connections in the tunnel.
2447 GMT_get_connections_buffer (struct CadetTunnel3 *t)
2449 struct CadetTConnection *iter;
2450 unsigned int buffer;
2453 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2455 if (GMC_get_state (iter->c) != CADET_CONNECTION_READY)
2459 buffer += get_connection_buffer (iter);
2467 * Get the tunnel's destination.
2471 * @return ID of the destination peer.
2473 const struct GNUNET_PeerIdentity *
2474 GMT_get_destination (struct CadetTunnel3 *t)
2476 return GMP_get_id (t->peer);
2481 * Get the tunnel's next free global channel ID.
2485 * @return GID of a channel free to use.
2488 GMT_get_next_chid (struct CadetTunnel3 *t)
2490 CADET_ChannelNumber chid;
2491 CADET_ChannelNumber mask;
2494 /* Set bit 30 depending on the ID relationship. Bit 31 is always 0 for GID.
2495 * If our ID is bigger or loopback tunnel, start at 0, bit 30 = 0
2496 * If peer's ID is bigger, start at 0x4... bit 30 = 1
2498 result = GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GMP_get_id (t->peer));
2503 t->next_chid |= mask;
2505 while (NULL != GMT_get_channel (t, t->next_chid))
2507 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
2508 t->next_chid = (t->next_chid + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
2509 t->next_chid |= mask;
2511 chid = t->next_chid;
2512 t->next_chid = (t->next_chid + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
2513 t->next_chid |= mask;
2520 * Send ACK on one or more channels due to buffer in connections.
2522 * @param t Channel which has some free buffer space.
2525 GMT_unchoke_channels (struct CadetTunnel3 *t)
2527 struct CadetTChannel *iter;
2528 unsigned int buffer;
2529 unsigned int channels = GMT_count_channels (t);
2530 unsigned int choked_n;
2531 struct CadetChannel *choked[channels];
2533 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_unchoke_channels on %s\n", GMT_2s (t));
2534 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
2535 if (NULL != t->channel_head)
2536 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
2538 /* Get buffer space */
2539 buffer = GMT_get_connections_buffer (t);
2545 /* Count and remember choked channels */
2547 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2549 if (GNUNET_NO == get_channel_allowed (iter))
2551 choked[choked_n++] = iter->ch;
2555 /* Unchoke random channels */
2556 while (0 < buffer && 0 < choked_n)
2558 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2560 GMCH_allow_client (choked[r], GMCH_is_origin (choked[r], GNUNET_YES));
2563 choked[r] = choked[choked_n];
2569 * Send ACK on one or more connections due to buffer space to the client.
2571 * Iterates all connections of the tunnel and sends ACKs appropriately.
2576 GMT_send_connection_acks (struct CadetTunnel3 *t)
2578 struct CadetTConnection *iter;
2581 uint32_t allow_per_connection;
2583 unsigned int buffer;
2585 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel send connection ACKs on %s\n",
2594 buffer = GMT_get_channels_buffer (t);
2595 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer %u\n", buffer);
2597 /* Count connections, how many messages are already allowed */
2598 cs = GMT_count_connections (t);
2599 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
2601 allowed += get_connection_allowed (iter);
2603 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowed %u\n", allowed);
2605 /* Make sure there is no overflow */
2606 if (allowed > buffer)
2611 /* Authorize connections to send more data */
2612 to_allow = buffer; /* - allowed; */
2614 for (iter = t->connection_head;
2615 NULL != iter && to_allow > 0;
2618 allow_per_connection = to_allow/cs;
2619 to_allow -= allow_per_connection;
2621 if (get_connection_allowed (iter) > 64 / 3)
2625 GMC_allow (iter->c, allow_per_connection,
2626 GMC_is_origin (iter->c, GNUNET_NO));
2629 GNUNET_break (to_allow == 0);
2634 * Cancel a previously sent message while it's in the queue.
2636 * ONLY can be called before the continuation given to the send function
2637 * is called. Once the continuation is called, the message is no longer in the
2640 * @param q Handle to the queue.
2643 GMT_cancel (struct CadetTunnel3Queue *q)
2648 /* tun_message_sent() will be called and free q */
2650 else if (NULL != q->tqd)
2652 unqueue_data (q->tqd);
2654 if (NULL != q->cont)
2655 q->cont (q->cont_cls, NULL, q, 0, 0);
2666 * Sends an already built message on a tunnel, encrypting it and
2667 * choosing the best connection if not provided.
2669 * @param message Message to send. Function modifies it.
2670 * @param t Tunnel on which this message is transmitted.
2671 * @param c Connection to use (autoselect if NULL).
2672 * @param force Force the tunnel to take the message (buffer overfill).
2673 * @param cont Continuation to call once message is really sent.
2674 * @param cont_cls Closure for @c cont.
2676 * @return Handle to cancel message. NULL if @c cont is NULL.
2678 struct CadetTunnel3Queue *
2679 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2680 struct CadetTunnel3 *t, struct CadetConnection *c,
2681 int force, GMT_sent cont, void *cont_cls)
2683 return send_prebuilt_message (message, t, c, force, cont, cont_cls, NULL);
2688 * Is the tunnel directed towards the local peer?
2692 * @return #GNUNET_YES if it is loopback.
2695 GMT_is_loopback (const struct CadetTunnel3 *t)
2697 return (myid == GMP_get_short_id (t->peer));
2702 * Is the tunnel this path already?
2707 * @return #GNUNET_YES a connection uses this path.
2710 GMT_is_path_used (const struct CadetTunnel3 *t, const struct CadetPeerPath *p)
2712 struct CadetTConnection *iter;
2714 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2715 if (GMC_get_path (iter->c) == p)
2723 * Get a cost of a path for a tunnel considering existing connections.
2726 * @param path Candidate path.
2728 * @return Cost of the path (path length + number of overlapping nodes)
2731 GMT_get_path_cost (const struct CadetTunnel3 *t,
2732 const struct CadetPeerPath *path)
2734 struct CadetTConnection *iter;
2735 const struct CadetPeerPath *aux;
2736 unsigned int overlap;
2744 GNUNET_assert (NULL != t);
2746 for (i = 0; i < path->length; i++)
2748 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2750 aux = GMC_get_path (iter->c);
2754 for (j = 0; j < aux->length; j++)
2756 if (path->peers[i] == aux->peers[j])
2764 return path->length + overlap;
2769 * Get the static string for the peer this tunnel is directed.
2773 * @return Static string the destination peer's ID.
2776 GMT_2s (const struct CadetTunnel3 *t)
2781 return GMP_2s (t->peer);
2785 /******************************************************************************/
2786 /***************************** INFO/DEBUG *******************************/
2787 /******************************************************************************/
2791 * Log all possible info about the tunnel state to stderr.
2793 * @param t Tunnel to debug.
2796 GMT_debug (const struct CadetTunnel3 *t)
2798 struct CadetTChannel *iterch;
2799 struct CadetTConnection *iterc;
2801 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT DEBUG TUNNEL TOWARDS %s\n", GMT_2s (t));
2802 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT cstate %s, estate %s\n",
2803 cstate2s (t->cstate), estate2s (t->estate));
2804 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT kx_ctx %p, rekey_task %u\n",
2805 t->kx_ctx, t->rekey_task);
2806 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT tq_head %p, tq_tail %p\n",
2807 t->tq_head, t->tq_tail);
2808 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT destroy %u\n", t->destroy_task);
2810 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT channels:\n");
2811 for (iterch = t->channel_head; NULL != iterch; iterch = iterch->next)
2813 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT - %s\n", GMCH_2s (iterch->ch));
2816 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT connections:\n");
2817 for (iterc = t->connection_head; NULL != iterc; iterc = iterc->next)
2819 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT - %s [%u] buf: %u/%u (qn %u/%u)\n",
2820 GMC_2s (iterc->c), GMC_get_state (iterc->c),
2821 GMC_get_buffer (iterc->c, GNUNET_YES),
2822 GMC_get_buffer (iterc->c, GNUNET_NO),
2823 GMC_get_qn (iterc->c, GNUNET_YES),
2824 GMC_get_qn (iterc->c, GNUNET_NO));
2827 LOG (GNUNET_ERROR_TYPE_DEBUG, "TTT DEBUG TUNNEL END\n");
2832 * Iterate all tunnels.
2834 * @param iter Iterator.
2835 * @param cls Closure for @c iter.
2838 GMT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2840 GNUNET_CONTAINER_multipeermap_iterate (tunnels, iter, cls);
2845 * Count all tunnels.
2847 * @return Number of tunnels to remote peers kept by this peer.
2850 GMT_count_all (void)
2852 return GNUNET_CONTAINER_multipeermap_size (tunnels);
2857 * Iterate all connections of a tunnel.
2859 * @param t Tunnel whose connections to iterate.
2860 * @param iter Iterator.
2861 * @param cls Closure for @c iter.
2864 GMT_iterate_connections (struct CadetTunnel3 *t, GMT_conn_iter iter, void *cls)
2866 struct CadetTConnection *ct;
2868 for (ct = t->connection_head; NULL != ct; ct = ct->next)
2874 * Iterate all channels of a tunnel.
2876 * @param t Tunnel whose channels to iterate.
2877 * @param iter Iterator.
2878 * @param cls Closure for @c iter.
2881 GMT_iterate_channels (struct CadetTunnel3 *t, GMT_chan_iter iter, void *cls)
2883 struct CadetTChannel *cht;
2885 for (cht = t->channel_head; NULL != cht; cht = cht->next)
2886 iter (cls, cht->ch);