remove dead routers

This commit is contained in:
orignal 2013-12-22 11:29:57 -05:00
parent 6341b10556
commit 5d9f292506
7 changed files with 62 additions and 16 deletions

View File

@ -9,6 +9,14 @@ namespace i2p
{
namespace data
{
Identity& Identity::operator=(const Keys& keys)
{
// copy public and signing keys together
memcpy (publicKey, keys.publicKey, sizeof (publicKey) + sizeof (signingKey));
memset (certificate, 0, sizeof (certificate));
return *this;
}
IdentHash CalculateIdentHash (const Identity& identity)
{
IdentHash hash;

View File

@ -23,6 +23,8 @@ namespace data
uint8_t publicKey[256];
uint8_t signingKey[128];
uint8_t certificate[3];
Identity& operator=(const Keys& keys);
};
#pragma pack()

View File

@ -20,12 +20,10 @@ namespace i2p
{
namespace ntcp
{
NTCPSession::NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo * in_RemoteRouterInfo):
NTCPSession::NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo& in_RemoteRouterInfo):
m_Socket (service), m_TerminationTimer (service), m_IsEstablished (false),
m_ReceiveBufferOffset (0), m_NextMessage (nullptr)
m_RemoteRouterInfo (in_RemoteRouterInfo), m_ReceiveBufferOffset (0), m_NextMessage (nullptr)
{
if (in_RemoteRouterInfo)
m_RemoteRouterInfo = *in_RemoteRouterInfo;
}
void NTCPSession::CreateAESKey (uint8_t * pubKey, uint8_t * aesKey)
@ -521,7 +519,8 @@ namespace ntcp
NTCPClient::NTCPClient (boost::asio::io_service& service, const char * address,
int port, i2p::data::RouterInfo& in_RouterInfo): NTCPSession (service, &in_RouterInfo),
int port, i2p::data::RouterInfo& in_RouterInfo):
NTCPSession (service, in_RouterInfo),
m_Endpoint (boost::asio::ip::address::from_string (address), port)
{
Connect ();

View File

@ -66,7 +66,7 @@ namespace ntcp
{
public:
NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo * in_RemoteRouterInfo = 0);
NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo& in_RemoteRouterInfo);
virtual ~NTCPSession () {};
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
@ -126,7 +126,7 @@ namespace ntcp
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption m_Encryption;
CryptoPP::Adler32 m_Adler;
i2p::data::RouterInfo m_RemoteRouterInfo;
i2p::data::RouterInfo& m_RemoteRouterInfo;
NTCPPhase1 m_Phase1;
NTCPPhase2 m_Phase2;
@ -163,11 +163,16 @@ namespace ntcp
{
public:
NTCPServerConnection (boost::asio::io_service& service): NTCPSession (service) {};
NTCPServerConnection (boost::asio::io_service& service):
NTCPSession (service, m_DummyRemoteRouterInfo) {};
protected:
virtual void Connected ();
private:
i2p::data::RouterInfo m_DummyRemoteRouterInfo;
};
}
}

View File

@ -129,6 +129,15 @@ namespace data
return nullptr;
}
LeaseSet * NetDb::FindLeaseSet (const IdentHash& destination) const
{
auto it = m_LeaseSets.find (destination);
if (it != m_LeaseSets.end ())
return it->second;
else
return nullptr;
}
void NetDb::Load (const char * directory)
{
boost::filesystem::path p (directory);
@ -156,19 +165,36 @@ namespace data
void NetDb::SaveUpdated (const char * directory)
{
int count = 0;
auto GetFilePath = [](const char * directory, const RouterInfo * routerInfo)
{
return std::string (directory) + "/r" +
routerInfo->GetIdentHashBase64 ()[0] + "/routerInfo-" +
routerInfo->GetIdentHashBase64 () + ".dat";
};
int count = 0, deletedCount = 0;
for (auto it: m_RouterInfos)
{
if (it.second->IsUpdated ())
{
std::ofstream r (std::string (directory) + "/r" +
it.second->GetIdentHashBase64 ()[0] + "/routerInfo-" +
it.second->GetIdentHashBase64 () + ".dat");
std::ofstream r (GetFilePath(directory, it.second));
r.write ((char *)it.second->GetBuffer (), it.second->GetBufferLen ());
it.second->SetUpdated (false);
count++;
}
else if (it.second->IsUnreachable ())
{
if (boost::filesystem::exists (GetFilePath (directory, it.second)))
{
boost::filesystem::remove (GetFilePath (directory, it.second));
deletedCount++;
}
}
}
if (count > 0)
LogPrint (count," new/updated routers saved");
if (deletedCount > 0)
LogPrint (deletedCount," routers deleted");
}
void NetDb::RequestDestination (const char * b32, const uint8_t * router)
@ -197,6 +223,11 @@ namespace data
LogPrint ("No outbound tunnels found");
}
void NetDb::RequestDestination (const IdentHash& destination)
{
RequestDestination ((const uint8_t *)destination, GetRandomNTCPRouter (true)->GetIdentHash ());
}
void NetDb::HandleDatabaseStoreMsg (uint8_t * buf, size_t len)
{
I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)buf;

View File

@ -27,9 +27,12 @@ namespace data
void AddRouterInfo (uint8_t * buf, int len);
void AddLeaseSet (uint8_t * buf, int len);
RouterInfo * FindRouter (const IdentHash& ident) const;
LeaseSet * FindLeaseSet (const IdentHash& destination) const;
void RequestDestination (const char * b32, const uint8_t * router); // in base32
void RequestDestination (const uint8_t * destination, const uint8_t * router);
void RequestDestination (const IdentHash& destination);
void HandleDatabaseStoreMsg (uint8_t * buf, size_t len);
void HandleDatabaseSearchReplyMsg (I2NPMessage * msg);

View File

@ -26,9 +26,7 @@ namespace i2p
CryptoPP::Integer (m_Keys.signingPrivateKey, 20));
i2p::data::Identity ident;
// copy public and signing keys together
memcpy (ident.publicKey, m_Keys.publicKey, sizeof (ident.publicKey) + sizeof (ident.signingKey));
memset (ident.certificate, 0, sizeof (ident.certificate));
ident = m_Keys;
m_RouterInfo.SetRouterIdentity (ident);
m_RouterInfo.AddNTCPAddress ("127.0.0.1", 17007); // TODO: