From 32a767dc91c55fbc32ad4e911c67a4e785a33546 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 30 Nov 2014 10:51:22 -0500 Subject: [PATCH] pass I2CP options to local destination. Process tunnel length --- ClientContext.cpp | 12 +++++++----- ClientContext.h | 6 ++++-- Destination.cpp | 43 ++++++++++++++++++++++++++++++++++++++++--- Destination.h | 10 +++++++++- SAM.cpp | 9 +++++---- SAM.h | 3 ++- 6 files changed, 67 insertions(+), 16 deletions(-) diff --git a/ClientContext.cpp b/ClientContext.cpp index f7ee20ec..c5a6e9af 100644 --- a/ClientContext.cpp +++ b/ClientContext.cpp @@ -29,7 +29,7 @@ namespace client { if (!m_SharedLocalDestination) { - m_SharedLocalDestination = CreateNewLocalDestination (false, i2p::data::SIGNING_KEY_TYPE_DSA_SHA1); // non-public, DSA + m_SharedLocalDestination = CreateNewLocalDestination (); // non-public, DSA m_Destinations[m_SharedLocalDestination->GetIdentity ().GetIdentHash ()] = m_SharedLocalDestination; m_SharedLocalDestination->Start (); } @@ -149,10 +149,11 @@ namespace client return localDestination; } - ClientDestination * ClientContext::CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType) + ClientDestination * ClientContext::CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType, + const std::map * params) { i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType); - auto localDestination = new ClientDestination (keys, isPublic); + auto localDestination = new ClientDestination (keys, isPublic, params); std::unique_lock l(m_DestinationsMutex); m_Destinations[localDestination->GetIdentHash ()] = localDestination; localDestination->Start (); @@ -175,7 +176,8 @@ namespace client } } - ClientDestination * ClientContext::CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic) + ClientDestination * ClientContext::CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic, + const std::map * params) { auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ()); if (it != m_Destinations.end ()) @@ -188,7 +190,7 @@ namespace client } return nullptr; } - auto localDestination = new ClientDestination (keys, isPublic); + auto localDestination = new ClientDestination (keys, isPublic, params); std::unique_lock l(m_DestinationsMutex); m_Destinations[keys.GetPublic ().GetIdentHash ()] = localDestination; localDestination->Start (); diff --git a/ClientContext.h b/ClientContext.h index d864fdfe..a413e21d 100644 --- a/ClientContext.h +++ b/ClientContext.h @@ -24,8 +24,10 @@ namespace client void Stop (); ClientDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; }; - ClientDestination * CreateNewLocalDestination (bool isPublic = true, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1); // transient - ClientDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true); + ClientDestination * CreateNewLocalDestination (bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1, + const std::map * params = nullptr); // transient + ClientDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true, + const std::map * params = nullptr); void DeleteLocalDestination (ClientDestination * destination); ClientDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const; ClientDestination * LoadLocalDestination (const std::string& filename, bool isPublic); diff --git a/Destination.cpp b/Destination.cpp index ed71d8f0..98f617eb 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "Log.h" #include "util.h" @@ -9,16 +10,42 @@ namespace i2p { namespace client { - ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic): + ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, + const std::map * params): m_IsRunning (false), m_Thread (nullptr), m_Service (nullptr), m_Work (nullptr), m_Keys (keys), m_LeaseSet (nullptr), m_IsPublic (isPublic), m_PublishReplyToken (0), m_DatagramDestination (nullptr), m_PublishConfirmationTimer (nullptr) { CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey); - m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3, 3); // 3-hops tunnel + int inboundTunnelLen = DEFAULT_INBOUND_TUNNEL_LENGTH; + int outboundTunnelLen = DEFAULT_OUTBOUND_TUNNEL_LENGTH; + if (params) + { + auto it = params->find (I2CP_PARAM_INBOUND_TUNNEL_LENGTH); + if (it != params->end ()) + { + int len = boost::lexical_cast(it->second); + if (len > 0) + { + inboundTunnelLen = len; + LogPrint (eLogInfo, "Inbound tunnel length set to ", len); + } + } + it = params->find (I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH); + if (it != params->end ()) + { + int len = boost::lexical_cast(it->second); + if (len > 0) + { + outboundTunnelLen = len; + LogPrint (eLogInfo, "Outbound tunnel length set to ", len); + } + } + } + m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, inboundTunnelLen, outboundTunnelLen); if (m_IsPublic) - LogPrint ("Local address ", GetIdentHash ().ToBase32 (), ".b32.i2p created"); + LogPrint (eLogInfo, "Local address ", GetIdentHash ().ToBase32 (), ".b32.i2p created"); m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO: } @@ -130,12 +157,22 @@ namespace client { if (m_Service) m_Service->post (std::bind (&ClientDestination::HandleGarlicMessage, this, msg)); + else + { + LogPrint (eLogWarning, "Destination's thread is not running"); + i2p::DeleteI2NPMessage (msg); + } } void ClientDestination::ProcessDeliveryStatusMessage (I2NPMessage * msg) { if (m_Service) m_Service->post (std::bind (&ClientDestination::HandleDeliveryStatusMessage, this, msg)); + else + { + LogPrint (eLogWarning, "Destination's thread is not running"); + i2p::DeleteI2NPMessage (msg); + } } void ClientDestination::HandleI2NPMessage (const uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from) diff --git a/Destination.h b/Destination.h index 723ff9d2..87a0bb66 100644 --- a/Destination.h +++ b/Destination.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "Identity.h" #include "TunnelPool.h" #include "CryptoConst.h" @@ -21,11 +23,17 @@ namespace client const uint8_t PROTOCOL_TYPE_RAW = 18; const int PUBLISH_CONFIRMATION_TIMEOUT = 5; // in seconds + // I2CP + const char I2CP_PARAM_INBOUND_TUNNEL_LENGTH[] = "inbound.length"; + const int DEFAULT_INBOUND_TUNNEL_LENGTH = 3; + const char I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH[] = "outbound.length"; + const int DEFAULT_OUTBOUND_TUNNEL_LENGTH = 3; + class ClientDestination: public i2p::garlic::GarlicDestination { public: - ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic); + ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map * params = nullptr); ~ClientDestination (); virtual void Start (); diff --git a/SAM.cpp b/SAM.cpp index 790c7dd5..e6dc5847 100644 --- a/SAM.cpp +++ b/SAM.cpp @@ -219,7 +219,7 @@ namespace client SendMessageReply (SAM_SESSION_CREATE_DUPLICATED_ID, strlen(SAM_SESSION_CREATE_DUPLICATED_ID), true); return; } - m_Session = m_Owner.CreateSession (id, destination == SAM_VALUE_TRANSIENT ? "" : destination); + m_Session = m_Owner.CreateSession (id, destination == SAM_VALUE_TRANSIENT ? "" : destination, ¶ms); if (m_Session) { m_SocketType = eSAMSocketTypeSession; @@ -621,7 +621,8 @@ namespace client Accept (); } - SAMSession * SAMBridge::CreateSession (const std::string& id, const std::string& destination) + SAMSession * SAMBridge::CreateSession (const std::string& id, const std::string& destination, + const std::map * params) { ClientDestination * localDestination = nullptr; if (destination != "") @@ -631,10 +632,10 @@ namespace client i2p::data::PrivateKeys keys; keys.FromBuffer (buf, l); delete[] buf; - localDestination = i2p::client::context.CreateNewLocalDestination (keys); + localDestination = i2p::client::context.CreateNewLocalDestination (keys, true, params); } else // transient - localDestination = i2p::client::context.CreateNewLocalDestination (); + localDestination = i2p::client::context.CreateNewLocalDestination (false, i2p::data::SIGNING_KEY_TYPE_DSA_SHA1, params); if (localDestination) { SAMSession session; diff --git a/SAM.h b/SAM.h index 5ef74eaa..32cc80fe 100644 --- a/SAM.h +++ b/SAM.h @@ -139,7 +139,8 @@ namespace client void Stop (); boost::asio::io_service& GetService () { return m_Service; }; - SAMSession * CreateSession (const std::string& id, const std::string& destination = ""); // empty string means transient + SAMSession * CreateSession (const std::string& id, const std::string& destination = "", // empty string means transient + const std::map * params = nullptr); void CloseSession (const std::string& id); SAMSession * FindSession (const std::string& id);