diff --git a/libi2pd/Destination.cpp b/libi2pd/Destination.cpp index d3632881..89b0047e 100644 --- a/libi2pd/Destination.cpp +++ b/libi2pd/Destination.cpp @@ -13,10 +13,10 @@ namespace i2p { namespace client { - LeaseSetDestination::LeaseSetDestination (bool isPublic, const std::map * params): - m_IsRunning (false), m_Thread (nullptr), m_IsPublic (isPublic), - m_PublishReplyToken (0), m_LastSubmissionTime (0), m_PublishConfirmationTimer (m_Service), - m_PublishVerificationTimer (m_Service), m_PublishDelayTimer (m_Service), m_CleanupTimer (m_Service) + LeaseSetDestination::LeaseSetDestination (bool isPublic, const std::map * params, boost::shared_ptr service): + m_Service(service), m_IsRunning (false), m_Thread (nullptr), m_IsPublic (isPublic), + m_PublishReplyToken (0), m_LastSubmissionTime (0), m_PublishConfirmationTimer (*m_Service.get()), + m_PublishVerificationTimer (*m_Service.get()), m_PublishDelayTimer (*m_Service.get()), m_CleanupTimer (*m_Service.get()) { int inLen = DEFAULT_INBOUND_TUNNEL_LENGTH; int inQty = DEFAULT_INBOUND_TUNNELS_QUANTITY; @@ -102,7 +102,7 @@ namespace client i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool); for (auto& it: m_LeaseSetRequests) it.second->Complete (nullptr); - } + } void LeaseSetDestination::Run () { @@ -110,7 +110,7 @@ namespace client { try { - m_Service.run (); + m_Service->run (); } catch (std::exception& ex) { @@ -154,7 +154,7 @@ namespace client m_Pool->SetLocalDestination (nullptr); i2p::tunnel::tunnels.StopTunnelPool (m_Pool); } - m_Service.stop (); + m_Service->stop (); if (m_Thread) { m_Thread->join (); @@ -282,7 +282,7 @@ namespace client if (m_IsPublic) { auto s = shared_from_this (); - m_Service.post ([s](void) + m_Service->post ([s](void) { s->m_PublishVerificationTimer.cancel (); s->Publish (); @@ -306,7 +306,7 @@ namespace client memcpy (data.k, key, 32); memcpy (data.t, tag, 32); auto s = shared_from_this (); - m_Service.post ([s,data](void) + m_Service->post ([s,data](void) { s->AddSessionKey (data.k, data.t); }); @@ -315,12 +315,12 @@ namespace client void LeaseSetDestination::ProcessGarlicMessage (std::shared_ptr msg) { - m_Service.post (std::bind (&LeaseSetDestination::HandleGarlicMessage, shared_from_this (), msg)); + m_Service->post (std::bind (&LeaseSetDestination::HandleGarlicMessage, shared_from_this (), msg)); } void LeaseSetDestination::ProcessDeliveryStatusMessage (std::shared_ptr msg) { - m_Service.post (std::bind (&LeaseSetDestination::HandleDeliveryStatusMessage, shared_from_this (), msg)); + m_Service->post (std::bind (&LeaseSetDestination::HandleDeliveryStatusMessage, shared_from_this (), msg)); } void LeaseSetDestination::HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr from) @@ -594,17 +594,17 @@ namespace client if (!m_Pool || !IsReady ()) { if (requestComplete) - m_Service.post ([requestComplete](void){requestComplete (nullptr);}); + m_Service->post ([requestComplete](void){requestComplete (nullptr);}); return false; } - m_Service.post (std::bind (&LeaseSetDestination::RequestLeaseSet, shared_from_this (), dest, requestComplete)); + m_Service->post (std::bind (&LeaseSetDestination::RequestLeaseSet, shared_from_this (), dest, requestComplete)); return true; } void LeaseSetDestination::CancelDestinationRequest (const i2p::data::IdentHash& dest, bool notify) { auto s = shared_from_this (); - m_Service.post ([dest, notify, s](void) + m_Service->post ([dest, notify, s](void) { auto it = s->m_LeaseSetRequests.find (dest); if (it != s->m_LeaseSetRequests.end ()) @@ -622,7 +622,7 @@ namespace client auto floodfill = i2p::data::netdb.GetClosestFloodfill (dest, excluded); if (floodfill) { - auto request = std::make_shared (m_Service); + auto request = std::make_shared ((boost::asio::io_service&)*m_Service.get()); if (requestComplete) request->requestComplete.push_back (requestComplete); auto ts = i2p::util::GetSecondsSinceEpoch (); @@ -762,30 +762,30 @@ namespace client else ++it; } - } + } - ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map * params): - LeaseSetDestination (isPublic, params), m_Keys (keys), m_StreamingAckDelay (DEFAULT_INITIAL_ACK_DELAY), - m_DatagramDestination (nullptr), m_RefCounter (0), - m_ReadyChecker(GetService()) - { - if (isPublic) - PersistTemporaryKeys (); - else - i2p::data::PrivateKeys::GenerateCryptoKeyPair(GetIdentity ()->GetCryptoKeyType (), - m_EncryptionPrivateKey, m_EncryptionPublicKey); - m_Decryptor = m_Keys.CreateDecryptor (m_EncryptionPrivateKey); - if (isPublic) - LogPrint (eLogInfo, "Destination: Local address ", GetIdentHash().ToBase32 (), " created"); + ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map * params, boost::shared_ptr service): + LeaseSetDestination (isPublic, params, service), m_Keys (keys), m_StreamingAckDelay (DEFAULT_INITIAL_ACK_DELAY), + m_DatagramDestination (nullptr), m_RefCounter (0), + m_ReadyChecker(GetService()) + { + if (isPublic) + PersistTemporaryKeys (); + else + i2p::data::PrivateKeys::GenerateCryptoKeyPair(GetIdentity ()->GetCryptoKeyType (), + m_EncryptionPrivateKey, m_EncryptionPublicKey); + m_Decryptor = m_Keys.CreateDecryptor (m_EncryptionPrivateKey); + if (isPublic) + LogPrint (eLogInfo, "Destination: Local address ", GetIdentHash().ToBase32 (), " created"); - // extract streaming params - if (params) - { - auto it = params->find (I2CP_PARAM_STREAMING_INITIAL_ACK_DELAY); - if (it != params->end ()) - m_StreamingAckDelay = std::stoi(it->second); - } - } + // extract streaming params + if (params) + { + auto it = params->find (I2CP_PARAM_STREAMING_INITIAL_ACK_DELAY); + if (it != params->end ()) + m_StreamingAckDelay = std::stoi(it->second); + } + } ClientDestination::~ClientDestination () { diff --git a/libi2pd/Destination.h b/libi2pd/Destination.h index 3f261bc9..d52c08f7 100644 --- a/libi2pd/Destination.h +++ b/libi2pd/Destination.h @@ -90,8 +90,8 @@ namespace client public: - LeaseSetDestination (bool isPublic, const std::map * params = nullptr); - ~LeaseSetDestination (); + LeaseSetDestination (bool isPublic, const std::map * params = nullptr, boost::shared_ptr service = boost::shared_ptr()); + ~LeaseSetDestination (); const std::string& GetNickname () const { return m_Nickname; }; virtual bool Start (); @@ -101,7 +101,7 @@ namespace client virtual bool Reconfigure(std::map i2cpOpts); bool IsRunning () const { return m_IsRunning; }; - boost::asio::io_service& GetService () { return m_Service; }; + boost::asio::io_service& GetService () { return (boost::asio::io_service&)*m_Service.get(); }; std::shared_ptr GetTunnelPool () { return m_Pool; }; bool IsReady () const { return m_LeaseSet && !m_LeaseSet->IsExpired () && m_Pool->GetOutboundTunnels ().size () > 0; }; std::shared_ptr FindLeaseSet (const i2p::data::IdentHash& ident); @@ -149,7 +149,7 @@ namespace client volatile bool m_IsRunning; std::thread * m_Thread; - boost::asio::io_service m_Service; + boost::shared_ptr m_Service; mutable std::mutex m_RemoteLeaseSetsMutex; std::map > m_RemoteLeaseSets; std::map > m_LeaseSetRequests; @@ -184,7 +184,7 @@ namespace client void Ready(ReadyPromise & p); #endif - ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map * params = nullptr); + ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map * params = nullptr, boost::shared_ptr service = boost::shared_ptr()); ~ClientDestination (); virtual bool Start (); diff --git a/libi2pd/api.cpp b/libi2pd/api.cpp index 415118ff..cd022061 100644 --- a/libi2pd/api.cpp +++ b/libi2pd/api.cpp @@ -83,7 +83,15 @@ namespace api auto localDestination = std::make_shared (keys, isPublic, params); localDestination->Start (); return localDestination; - } + } + + std::shared_ptr CreateLocalDestination (const i2p::data::PrivateKeys& keys, boost::shared_ptr service, + bool isPublic, const std::map * params) + { + auto localDestination = std::make_shared (keys, isPublic, params, service); + //localDestination->Start (); + return localDestination; + } std::shared_ptr CreateLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType, const std::map * params) diff --git a/libi2pd/api.h b/libi2pd/api.h index f64590d1..1f51c4a3 100644 --- a/libi2pd/api.h +++ b/libi2pd/api.h @@ -3,6 +3,8 @@ #include #include +#include +#include #include "Identity.h" #include "Destination.h" #include "Streaming.h" @@ -21,7 +23,10 @@ namespace api // destinations std::shared_ptr CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true, - const std::map * params = nullptr); + const std::map * params = nullptr); + std::shared_ptr CreateLocalDestination (const i2p::data::PrivateKeys& keys, boost::shared_ptr service, + bool isPublic = true, const std::map * params = nullptr); + std::shared_ptr CreateLocalDestination (bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256, const std::map * params = nullptr); // transient destinations usually not published void DestroyLocalDestination (std::shared_ptr dest);