Browse Source

pass local destination by pointer

pull/118/head
orignal 10 years ago
parent
commit
f357a5864c
  1. 6
      Destination.cpp
  2. 6
      I2NPProtocol.cpp
  3. 18
      LeaseSet.cpp
  4. 4
      NetDb.cpp
  5. 4
      Tunnel.cpp
  6. 2
      Tunnel.h
  7. 31
      TunnelPool.cpp
  8. 8
      TunnelPool.h

6
Destination.cpp

@ -43,7 +43,7 @@ namespace client
} }
} }
} }
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, inboundTunnelLen, outboundTunnelLen); m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (this, inboundTunnelLen, outboundTunnelLen);
if (m_IsPublic) if (m_IsPublic)
LogPrint (eLogInfo, "Local address ", GetIdentHash ().ToBase32 (), ".b32.i2p created"); LogPrint (eLogInfo, "Local address ", GetIdentHash ().ToBase32 (), ".b32.i2p created");
m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO: m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO:
@ -85,6 +85,7 @@ namespace client
m_Service = new boost::asio::io_service; m_Service = new boost::asio::io_service;
m_PublishConfirmationTimer = new boost::asio::deadline_timer (*m_Service); m_PublishConfirmationTimer = new boost::asio::deadline_timer (*m_Service);
m_Work = new boost::asio::io_service::work (*m_Service); m_Work = new boost::asio::io_service::work (*m_Service);
m_Pool->SetLocalDestination (this);
m_Pool->SetActive (true); m_Pool->SetActive (true);
m_IsRunning = true; m_IsRunning = true;
m_Thread = new std::thread (std::bind (&ClientDestination::Run, this)); m_Thread = new std::thread (std::bind (&ClientDestination::Run, this));
@ -101,7 +102,10 @@ namespace client
delete d; delete d;
} }
if (m_Pool) if (m_Pool)
{
m_Pool->SetLocalDestination (nullptr);
i2p::tunnel::tunnels.StopTunnelPool (m_Pool); i2p::tunnel::tunnels.StopTunnelPool (m_Pool);
}
m_IsRunning = false; m_IsRunning = false;
if (m_Service) if (m_Service)
m_Service->stop (); m_Service->stop ();

6
I2NPProtocol.cpp

@ -160,8 +160,8 @@ namespace i2p
rnd.GenerateBlock (buf, 32); // key rnd.GenerateBlock (buf, 32); // key
buf[32] = 1; // 1 tag buf[32] = 1; // 1 tag
rnd.GenerateBlock (buf + 33, 32); // tag rnd.GenerateBlock (buf + 33, 32); // tag
if (pool) if (pool && pool->GetLocalDestination ())
pool->GetLocalDestination ().SubmitSessionKey (buf, buf + 33); // introduce new key-tag to garlic engine pool->GetLocalDestination ()->SubmitSessionKey (buf, buf + 33); // introduce new key-tag to garlic engine
else else
LogPrint ("Destination for encrypteed reply not specified"); LogPrint ("Destination for encrypteed reply not specified");
buf += 65; buf += 65;
@ -563,7 +563,7 @@ namespace i2p
case eI2NPGarlic: case eI2NPGarlic:
LogPrint ("Garlic"); LogPrint ("Garlic");
if (msg->from && msg->from->GetTunnelPool ()) if (msg->from && msg->from->GetTunnelPool ())
msg->from->GetTunnelPool ()->GetLocalDestination ().ProcessGarlicMessage (msg); msg->from->GetTunnelPool ()->ProcessGarlicMessage (msg);
else else
i2p::context.ProcessGarlicMessage (msg); i2p::context.ProcessGarlicMessage (msg);
break; break;

18
LeaseSet.cpp

@ -22,11 +22,17 @@ namespace data
LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool) LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool)
{ {
// header // header
const i2p::data::LocalDestination& localDestination = pool.GetLocalDestination (); const i2p::data::LocalDestination * localDestination = pool.GetLocalDestination ();
m_BufferLen = localDestination.GetIdentity ().ToBuffer (m_Buffer, MAX_LS_BUFFER_SIZE); if (!localDestination)
memcpy (m_Buffer + m_BufferLen, localDestination.GetEncryptionPublicKey (), 256); {
m_BufferLen = 0;
LogPrint (eLogError, "Destination for local LeaseSet doesn't exist");
return;
}
m_BufferLen = localDestination->GetIdentity ().ToBuffer (m_Buffer, MAX_LS_BUFFER_SIZE);
memcpy (m_Buffer + m_BufferLen, localDestination->GetEncryptionPublicKey (), 256);
m_BufferLen += 256; m_BufferLen += 256;
auto signingKeyLen = localDestination.GetIdentity ().GetSigningPublicKeyLen (); auto signingKeyLen = localDestination->GetIdentity ().GetSigningPublicKeyLen ();
memset (m_Buffer + m_BufferLen, 0, signingKeyLen); memset (m_Buffer + m_BufferLen, 0, signingKeyLen);
m_BufferLen += signingKeyLen; m_BufferLen += signingKeyLen;
auto tunnels = pool.GetInboundTunnels (5); // 5 tunnels maximum auto tunnels = pool.GetInboundTunnels (5); // 5 tunnels maximum
@ -44,8 +50,8 @@ namespace data
m_BufferLen += sizeof (Lease); m_BufferLen += sizeof (Lease);
} }
// signature // signature
localDestination.Sign (m_Buffer, m_BufferLen, m_Buffer + m_BufferLen); localDestination->Sign (m_Buffer, m_BufferLen, m_Buffer + m_BufferLen);
m_BufferLen += localDestination.GetIdentity ().GetSignatureLen (); m_BufferLen += localDestination->GetIdentity ().GetSignatureLen ();
LogPrint ("Local LeaseSet of ", tunnels.size (), " leases created"); LogPrint ("Local LeaseSet of ", tunnels.size (), " leases created");
ReadFromBuffer (); ReadFromBuffer ();

4
NetDb.cpp

@ -29,8 +29,8 @@ namespace data
&m_ExcludedPeers, m_IsLeaseSet, m_Pool); &m_ExcludedPeers, m_IsLeaseSet, m_Pool);
if (m_IsLeaseSet) // wrap lookup message into garlic if (m_IsLeaseSet) // wrap lookup message into garlic
{ {
if (m_Pool) if (m_Pool && m_Pool->GetLocalDestination ())
msg = m_Pool->GetLocalDestination ().WrapMessage (*router, msg); msg = m_Pool->GetLocalDestination ()->WrapMessage (*router, msg);
else else
LogPrint ("Can't create garlic message without destination"); LogPrint ("Can't create garlic message without destination");
} }

4
Tunnel.cpp

@ -287,7 +287,7 @@ namespace tunnel
return tunnel; return tunnel;
} }
TunnelPool * Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination& localDestination, int numInboundHops, int numOutboundHops) TunnelPool * Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops)
{ {
auto pool = new TunnelPool (localDestination, numInboundHops, numOutboundHops); auto pool = new TunnelPool (localDestination, numInboundHops, numOutboundHops);
std::unique_lock<std::mutex> l(m_PoolsMutex); std::unique_lock<std::mutex> l(m_PoolsMutex);
@ -513,7 +513,7 @@ namespace tunnel
LogPrint ("Creating zero hops inbound tunnel..."); LogPrint ("Creating zero hops inbound tunnel...");
CreateZeroHopsInboundTunnel (); CreateZeroHopsInboundTunnel ();
if (!m_ExploratoryPool) if (!m_ExploratoryPool)
m_ExploratoryPool = CreateTunnelPool (i2p::context, 2, 2); // 2-hop exploratory m_ExploratoryPool = CreateTunnelPool (&i2p::context, 2, 2); // 2-hop exploratory
return; return;
} }

2
Tunnel.h

@ -129,7 +129,7 @@ namespace tunnel
void PostTunnelData (I2NPMessage * msg); void PostTunnelData (I2NPMessage * msg);
template<class TTunnel> template<class TTunnel>
TTunnel * CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel = 0); TTunnel * CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel = 0);
TunnelPool * CreateTunnelPool (i2p::garlic::GarlicDestination& localDestination, int numInboundHops, int numOuboundHops); TunnelPool * CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops);
void DeleteTunnelPool (TunnelPool * pool); void DeleteTunnelPool (TunnelPool * pool);
void StopTunnelPool (TunnelPool * pool); void StopTunnelPool (TunnelPool * pool);

31
TunnelPool.cpp

@ -10,7 +10,7 @@ namespace i2p
{ {
namespace tunnel namespace tunnel
{ {
TunnelPool::TunnelPool (i2p::garlic::GarlicDestination& localDestination, int numInboundHops, int numOutboundHops, int numTunnels): TunnelPool::TunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops, int numTunnels):
m_LocalDestination (localDestination), m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops), m_LocalDestination (localDestination), m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops),
m_NumTunnels (numTunnels), m_IsActive (true) m_NumTunnels (numTunnels), m_IsActive (true)
{ {
@ -45,7 +45,8 @@ namespace tunnel
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex); std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
m_InboundTunnels.insert (createdTunnel); m_InboundTunnels.insert (createdTunnel);
} }
m_LocalDestination.SetLeaseSetUpdated (); if (m_LocalDestination)
m_LocalDestination->SetLeaseSetUpdated ();
} }
void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel) void TunnelPool::TunnelExpired (InboundTunnel * expiredTunnel)
@ -183,7 +184,8 @@ namespace tunnel
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex); std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
m_InboundTunnels.erase (it.second.second); m_InboundTunnels.erase (it.second.second);
} }
m_LocalDestination.SetLeaseSetUpdated (); if (m_LocalDestination)
m_LocalDestination->SetLeaseSetUpdated ();
} }
else else
it.second.second->SetState (eTunnelStateTestFailed); it.second.second->SetState (eTunnelStateTestFailed);
@ -217,6 +219,17 @@ namespace tunnel
} }
} }
void TunnelPool::ProcessGarlicMessage (I2NPMessage * msg)
{
if (m_LocalDestination)
m_LocalDestination->ProcessGarlicMessage (msg);
else
{
LogPrint (eLogWarning, "Local destination doesn't exist. Dropped");
DeleteI2NPMessage (msg);
}
}
void TunnelPool::ProcessDeliveryStatus (I2NPMessage * msg) void TunnelPool::ProcessDeliveryStatus (I2NPMessage * msg)
{ {
I2NPDeliveryStatusMsg * deliveryStatus = (I2NPDeliveryStatusMsg *)msg->GetPayload (); I2NPDeliveryStatusMsg * deliveryStatus = (I2NPDeliveryStatusMsg *)msg->GetPayload ();
@ -233,12 +246,20 @@ namespace tunnel
DeleteI2NPMessage (msg); DeleteI2NPMessage (msg);
} }
else else
m_LocalDestination.ProcessDeliveryStatusMessage (msg); {
if (m_LocalDestination)
m_LocalDestination->ProcessDeliveryStatusMessage (msg);
else
{
LogPrint (eLogWarning, "Local destination doesn't exist. Dropped");
DeleteI2NPMessage (msg);
}
}
} }
std::shared_ptr<const i2p::data::RouterInfo> TunnelPool::SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop) const std::shared_ptr<const i2p::data::RouterInfo> TunnelPool::SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop) const
{ {
bool isExploratory = (&m_LocalDestination == &i2p::context); // TODO: implement it better bool isExploratory = (m_LocalDestination == &i2p::context); // TODO: implement it better
auto hop = isExploratory ? i2p::data::netdb.GetRandomRouter (prevHop): auto hop = isExploratory ? i2p::data::netdb.GetRandomRouter (prevHop):
i2p::data::netdb.GetHighBandwidthRandomRouter (prevHop); i2p::data::netdb.GetHighBandwidthRandomRouter (prevHop);

8
TunnelPool.h

@ -26,10 +26,11 @@ namespace tunnel
{ {
public: public:
TunnelPool (i2p::garlic::GarlicDestination& localDestination, int numInboundHops, int numOutboundHops, int numTunnels = 5); TunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops, int numTunnels = 5);
~TunnelPool (); ~TunnelPool ();
i2p::garlic::GarlicDestination& GetLocalDestination () const { return m_LocalDestination; }; i2p::garlic::GarlicDestination * GetLocalDestination () const { return m_LocalDestination; };
void SetLocalDestination (i2p::garlic::GarlicDestination * destination) { m_LocalDestination = destination; };
void CreateTunnels (); void CreateTunnels ();
void TunnelCreated (InboundTunnel * createdTunnel); void TunnelCreated (InboundTunnel * createdTunnel);
@ -41,6 +42,7 @@ namespace tunnel
InboundTunnel * GetNextInboundTunnel (InboundTunnel * suggested = nullptr) const; InboundTunnel * GetNextInboundTunnel (InboundTunnel * suggested = nullptr) const;
void TestTunnels (); void TestTunnels ();
void ProcessGarlicMessage (I2NPMessage * msg);
void ProcessDeliveryStatus (I2NPMessage * msg); void ProcessDeliveryStatus (I2NPMessage * msg);
bool IsActive () const { return m_IsActive; }; bool IsActive () const { return m_IsActive; };
@ -60,7 +62,7 @@ namespace tunnel
private: private:
i2p::garlic::GarlicDestination& m_LocalDestination; i2p::garlic::GarlicDestination * m_LocalDestination;
int m_NumInboundHops, m_NumOutboundHops, m_NumTunnels; int m_NumInboundHops, m_NumOutboundHops, m_NumTunnels;
mutable std::mutex m_InboundTunnelsMutex; mutable std::mutex m_InboundTunnelsMutex;
std::set<InboundTunnel *, TunnelCreationTimeCmp> m_InboundTunnels; // recent tunnel appears first std::set<InboundTunnel *, TunnelCreationTimeCmp> m_InboundTunnels; // recent tunnel appears first

Loading…
Cancel
Save