From 3313a5af5c956258616357b01f384ac0aa2c7043 Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 15 Mar 2014 09:16:55 -0400 Subject: [PATCH] generate LeaseSet from tunnel pool --- Streaming.cpp | 33 +++++++++++++++++++++++++++------ Streaming.h | 6 +++++- TunnelPool.cpp | 7 +++++-- i2p.cpp | 3 +++ 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/Streaming.cpp b/Streaming.cpp index 0bd88f66..b7282145 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -308,13 +308,15 @@ namespace stream m_IdentHash = i2p::data::CalculateIdentHash (m_Identity); m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, CryptoPP::Integer (m_Keys.signingPrivateKey, 20)); - i2p::tunnel::tunnels.CreateTunnelPool (this); + m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (this); } StreamingDestination::~StreamingDestination () { if (m_LeaseSet) DeleteI2NPMessage (m_LeaseSet); + if (m_Pool) + i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool); } void StreamingDestination::HandleNextPacket (Packet * packet) @@ -346,12 +348,20 @@ namespace stream } } - I2NPMessage * StreamingDestination::GetLeaseSet () + void StreamingDestination::UpdateLeaseSet () { - if (m_LeaseSet) // temporary always create new LeaseSet - DeleteI2NPMessage (m_LeaseSet); - m_LeaseSet = CreateLeaseSet (); + auto newLeaseSet = CreateLeaseSet (); + // TODO: make it atomic + auto oldLeaseSet = m_LeaseSet; + m_LeaseSet = newLeaseSet; + if (oldLeaseSet) + DeleteI2NPMessage (oldLeaseSet); + } + I2NPMessage * StreamingDestination::GetLeaseSet () + { + if (!m_LeaseSet) + m_LeaseSet = CreateLeaseSet (); return m_LeaseSet; } @@ -371,7 +381,7 @@ namespace stream size += 256; // encryption key memset (buf + size, 0, 128); size += 128; // signing key - auto tunnels = i2p::tunnel::tunnels.GetInboundTunnels (5); // 5 tunnels maximum + auto tunnels = m_Pool->GetInboundTunnels (5); // 5 tunnels maximum buf[size] = tunnels.size (); // num leases size++; // num for (auto it: tunnels) @@ -412,6 +422,17 @@ namespace stream if (sharedLocalDestination) sharedLocalDestination->DeleteStream (stream); } + + void StartStreaming () + { + if (!sharedLocalDestination) + sharedLocalDestination = new StreamingDestination (); + } + + void StopStreaming () + { + delete sharedLocalDestination; + } void HandleDataMessage (i2p::data::IdentHash * destination, const uint8_t * buf, size_t len) { diff --git a/Streaming.h b/Streaming.h index e4438e0c..8f0f289a 100644 --- a/Streaming.h +++ b/Streaming.h @@ -11,6 +11,7 @@ #include "LeaseSet.h" #include "I2NPProtocol.h" #include "Tunnel.h" +#include "TunnelPool.h" namespace i2p { @@ -114,7 +115,7 @@ namespace stream void HandleNextPacket (Packet * packet); // implements LocalDestination - void UpdateLeaseSet () {}; // TODO: + void UpdateLeaseSet (); private: @@ -127,6 +128,7 @@ namespace stream i2p::data::Identity m_Identity; i2p::data::IdentHash m_IdentHash; + i2p::tunnel::TunnelPool * m_Pool; I2NPMessage * m_LeaseSet; CryptoPP::DSA::PrivateKey m_SigningPrivateKey; @@ -134,6 +136,8 @@ namespace stream Stream * CreateStream (const i2p::data::LeaseSet& remote); void DeleteStream (Stream * stream); + void StartStreaming (); + void StopStreaming (); // assuming data is I2CP message void HandleDataMessage (i2p::data::IdentHash * destination, const uint8_t * buf, size_t len); diff --git a/TunnelPool.cpp b/TunnelPool.cpp index 7f14dbec..d9d38354 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -21,6 +21,8 @@ namespace tunnel void TunnelPool::TunnelCreated (InboundTunnel * createdTunnel) { m_InboundTunnels.insert (createdTunnel); + if (m_Owner) + m_Owner->UpdateLeaseSet (); } void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel) @@ -60,8 +62,9 @@ namespace tunnel new TunnelConfig (std::vector { firstHop, - secondHop, - i2p::data::netdb.GetRandomRouter (secondHop) + secondHop + // TODO: swithc to 3-hops later + /*i2p::data::netdb.GetRandomRouter (secondHop) */ }), outboundTunnel); tunnel->SetTunnelPool (this); diff --git a/i2p.cpp b/i2p.cpp index 839e7c01..b13b1791 100644 --- a/i2p.cpp +++ b/i2p.cpp @@ -24,6 +24,7 @@ #include "HTTPServer.h" #include "Garlic.h" #include "util.h" +#include "Streaming.h" // Global @@ -154,6 +155,7 @@ int main( int argc, char* argv[] ) i2p::transports.Start (); i2p::tunnel::tunnels.Start (); i2p::garlic::routing.Start (); + i2p::stream::StartStreaming (); while (running) { @@ -162,6 +164,7 @@ int main( int argc, char* argv[] ) } LogPrint("Shutdown started."); + i2p::stream::StopStreaming (); i2p::garlic::routing.Stop (); i2p::tunnel::tunnels.Stop (); i2p::transports.Stop ();