mirror of
https://github.com/PurpleI2P/i2pd
synced 2024-11-10 08:00:38 +03:00
deep exploratory
This commit is contained in:
parent
e1716bc05e
commit
c93402ab40
@ -83,7 +83,7 @@ namespace i2p
|
|||||||
}
|
}
|
||||||
|
|
||||||
I2NPMessage * CreateDatabaseLookupMsg (const uint8_t * key, const uint8_t * from,
|
I2NPMessage * CreateDatabaseLookupMsg (const uint8_t * key, const uint8_t * from,
|
||||||
uint32_t replyTunnelID, bool exploratory)
|
uint32_t replyTunnelID, bool exploratory, std::set<i2p::data::IdentHash> * excludedPeers)
|
||||||
{
|
{
|
||||||
I2NPMessage * m = NewI2NPMessage ();
|
I2NPMessage * m = NewI2NPMessage ();
|
||||||
uint8_t * buf = m->GetPayload ();
|
uint8_t * buf = m->GetPayload ();
|
||||||
@ -113,9 +113,23 @@ namespace i2p
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// nothing to exclude
|
if (excludedPeers)
|
||||||
*(uint16_t *)buf = htobe16 (0);
|
{
|
||||||
buf += 2;
|
int cnt = excludedPeers->size ();
|
||||||
|
*(uint16_t *)buf = htobe16 (cnt);
|
||||||
|
buf += 2;
|
||||||
|
for (auto& it: *excludedPeers)
|
||||||
|
{
|
||||||
|
memcpy (buf, it, 32);
|
||||||
|
buf += 32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// nothing to exclude
|
||||||
|
*(uint16_t *)buf = htobe16 (0);
|
||||||
|
buf += 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
m->len += (buf - m->GetPayload ());
|
m->len += (buf - m->GetPayload ());
|
||||||
FillI2NPMessageHeader (m, eI2NPDatabaseLookup);
|
FillI2NPMessageHeader (m, eI2NPDatabaseLookup);
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#define I2NP_PROTOCOL_H__
|
#define I2NP_PROTOCOL_H__
|
||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <vector>
|
#include <set>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "RouterInfo.h"
|
#include "RouterInfo.h"
|
||||||
|
|
||||||
@ -105,7 +105,8 @@ namespace i2p
|
|||||||
|
|
||||||
I2NPMessage * CreateDeliveryStatusMsg ();
|
I2NPMessage * CreateDeliveryStatusMsg ();
|
||||||
I2NPMessage * CreateDatabaseLookupMsg (const uint8_t * key, const uint8_t * from,
|
I2NPMessage * CreateDatabaseLookupMsg (const uint8_t * key, const uint8_t * from,
|
||||||
uint32_t replyTunnelID, bool exploratory = false);
|
uint32_t replyTunnelID, bool exploratory = false,
|
||||||
|
std::set<i2p::data::IdentHash> * excludedPeers = nullptr);
|
||||||
|
|
||||||
I2NPMessage * CreateDatabaseStoreMsg ();
|
I2NPMessage * CreateDatabaseStoreMsg ();
|
||||||
|
|
||||||
|
172
NetDb.cpp
172
NetDb.cpp
@ -16,9 +16,22 @@ namespace i2p
|
|||||||
namespace data
|
namespace data
|
||||||
{
|
{
|
||||||
|
|
||||||
|
I2NPMessage * RequestedDestination::CreateRequestMessage (const RouterInfo * router,
|
||||||
|
const i2p::tunnel::InboundTunnel * replyTunnel)
|
||||||
|
{
|
||||||
|
I2NPMessage * msg = i2p::CreateDatabaseLookupMsg (m_Destination,
|
||||||
|
replyTunnel->GetNextIdentHash (), replyTunnel->GetNextTunnelID (), m_IsExploratory, &m_ExcludedPeers);
|
||||||
|
if (m_IsLeaseSet) // wrap lookup message into garlic
|
||||||
|
msg = i2p::garlic::routing.WrapSingleMessage (router, msg);
|
||||||
|
m_ExcludedPeers.insert (router->GetIdentHash ());
|
||||||
|
m_LastRouter = router;
|
||||||
|
m_LastReplyTunnel = replyTunnel;
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
NetDb netdb;
|
NetDb netdb;
|
||||||
|
|
||||||
NetDb::NetDb (): m_IsRunning (false), m_Thread (0), m_LastFloodfill (0)
|
NetDb::NetDb (): m_IsRunning (false), m_Thread (0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,6 +42,8 @@ namespace data
|
|||||||
delete l.second;
|
delete l.second;
|
||||||
for (auto r:m_RouterInfos)
|
for (auto r:m_RouterInfos)
|
||||||
delete r.second;
|
delete r.second;
|
||||||
|
for (auto r:m_RequestedDestinations)
|
||||||
|
delete r.second;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetDb::Start ()
|
void NetDb::Start ()
|
||||||
@ -97,6 +112,7 @@ namespace data
|
|||||||
void NetDb::AddRouterInfo (uint8_t * buf, int len)
|
void NetDb::AddRouterInfo (uint8_t * buf, int len)
|
||||||
{
|
{
|
||||||
RouterInfo * r = new RouterInfo (buf, len);
|
RouterInfo * r = new RouterInfo (buf, len);
|
||||||
|
DeleteRequestedDestination (r->GetIdentHash ());
|
||||||
auto it = m_RouterInfos.find(r->GetIdentHash ());
|
auto it = m_RouterInfos.find(r->GetIdentHash ());
|
||||||
if (it != m_RouterInfos.end ())
|
if (it != m_RouterInfos.end ())
|
||||||
{
|
{
|
||||||
@ -118,6 +134,7 @@ namespace data
|
|||||||
void NetDb::AddLeaseSet (uint8_t * buf, int len)
|
void NetDb::AddLeaseSet (uint8_t * buf, int len)
|
||||||
{
|
{
|
||||||
LeaseSet * l = new LeaseSet (buf, len);
|
LeaseSet * l = new LeaseSet (buf, len);
|
||||||
|
DeleteRequestedDestination (l->GetIdentHash ());
|
||||||
m_LeaseSets[l->GetIdentHash ()] = l;
|
m_LeaseSets[l->GetIdentHash ()] = l;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,10 +240,9 @@ namespace data
|
|||||||
i2p::tunnel::InboundTunnel * inbound = i2p::tunnel::tunnels.GetNextInboundTunnel ();
|
i2p::tunnel::InboundTunnel * inbound = i2p::tunnel::tunnels.GetNextInboundTunnel ();
|
||||||
if (inbound)
|
if (inbound)
|
||||||
{
|
{
|
||||||
I2NPMessage * msg = i2p::CreateDatabaseLookupMsg (destination, inbound->GetNextIdentHash (),
|
RequestedDestination * dest = CreateRequestedDestination (destination, isLeaseSet);
|
||||||
inbound->GetNextTunnelID ());
|
dest->SetLastOutboundTunnel (outbound);
|
||||||
if (isLeaseSet) // wrap lookup message into garlic
|
auto msg = dest->CreateRequestMessage (floodfill, inbound);
|
||||||
msg = i2p::garlic::routing.WrapSingleMessage (floodfill, msg);
|
|
||||||
outbound->SendTunnelDataMsg (floodfill->GetIdentHash (), 0, msg);
|
outbound->SendTunnelDataMsg (floodfill->GetIdentHash (), 0, msg);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -275,87 +291,133 @@ namespace data
|
|||||||
key[l] = 0;
|
key[l] = 0;
|
||||||
int num = buf[32]; // num
|
int num = buf[32]; // num
|
||||||
LogPrint ("DatabaseSearchReply for ", key, " num=", num);
|
LogPrint ("DatabaseSearchReply for ", key, " num=", num);
|
||||||
if (num > 0)
|
auto it = m_RequestedDestinations.find (IdentHash (buf));
|
||||||
|
if (it != m_RequestedDestinations.end ())
|
||||||
{
|
{
|
||||||
bool isExploratory = !memcmp (m_Exploratory, buf, 32) && m_LastFloodfill;
|
RequestedDestination * dest = it->second;
|
||||||
i2p::tunnel::OutboundTunnel * outbound = isExploratory ? m_LastOutboundTunnel : i2p::tunnel::tunnels.GetNextOutboundTunnel ();
|
if (num > 0)
|
||||||
i2p::tunnel::InboundTunnel * inbound = isExploratory ? m_LastInboundTunnel : i2p::tunnel::tunnels.GetNextInboundTunnel ();
|
|
||||||
|
|
||||||
for (int i = 0; i < num; i++)
|
|
||||||
{
|
{
|
||||||
uint8_t * router = buf + 33 + i*32;
|
i2p::tunnel::OutboundTunnel * outbound = dest->GetLastOutboundTunnel ();
|
||||||
char peerHash[48];
|
const i2p::tunnel::InboundTunnel * inbound = dest->GetLastReplyTunnel ();
|
||||||
int l1 = i2p::data::ByteStreamToBase64 (router, 32, peerHash, 48);
|
|
||||||
peerHash[l1] = 0;
|
|
||||||
LogPrint (i,": ", peerHash);
|
|
||||||
|
|
||||||
if (isExploratory)
|
for (int i = 0; i < num; i++)
|
||||||
{
|
{
|
||||||
if (m_RouterInfos.find (IdentHash(router)) == m_RouterInfos.end ())
|
uint8_t * router = buf + 33 + i*32;
|
||||||
|
char peerHash[48];
|
||||||
|
int l1 = i2p::data::ByteStreamToBase64 (router, 32, peerHash, 48);
|
||||||
|
peerHash[l1] = 0;
|
||||||
|
LogPrint (i,": ", peerHash);
|
||||||
|
|
||||||
|
if (dest->IsExploratory ())
|
||||||
{
|
{
|
||||||
LogPrint ("Found new router. Requesting RouterInfo ...");
|
if (!FindRouter (router)) // router with ident not found
|
||||||
if (outbound && inbound)
|
|
||||||
{
|
{
|
||||||
I2NPMessage * msg = i2p::CreateDatabaseLookupMsg (router, inbound->GetNextIdentHash (),
|
LogPrint ("Found new router. Requesting RouterInfo ...");
|
||||||
inbound->GetNextTunnelID ());
|
if (outbound && inbound)
|
||||||
outbound->GetTunnelGateway ().PutTunnelDataMsg (m_LastFloodfill->GetIdentHash (), 0, msg);
|
{
|
||||||
|
RequestedDestination * d1 = CreateRequestedDestination (router, false, false);
|
||||||
|
d1->SetLastOutboundTunnel (outbound);
|
||||||
|
auto msg = d1->CreateRequestMessage (dest->GetLastRouter (), dest->GetLastReplyTunnel ());
|
||||||
|
outbound->GetTunnelGateway ().PutTunnelDataMsg (dest->GetLastRouter ()->GetIdentHash (), 0, msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
LogPrint ("Bayan");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
LogPrint ("Bayan");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// reply to our destination. Try other floodfills
|
|
||||||
if (outbound && inbound)
|
|
||||||
{
|
{
|
||||||
// do we have that floodfill router in our database?
|
// reply to our destination. Try other floodfills
|
||||||
if (!FindRouter (router))
|
if (outbound && inbound)
|
||||||
{
|
{
|
||||||
// request router
|
auto r = FindRouter (router);
|
||||||
LogPrint ("Found new floodfill. Request it");
|
// do we have that floodfill router in our database?
|
||||||
msg = i2p::CreateDatabaseLookupMsg (router, inbound->GetNextIdentHash (),
|
if (r)
|
||||||
inbound->GetNextTunnelID ());
|
{
|
||||||
outbound->GetTunnelGateway ().PutTunnelDataMsg (
|
if (!dest->IsExcluded (r->GetIdentHash ()) && dest->GetNumExcludedPeers () < 10) // TODO: fix TunnelGateway first
|
||||||
GetRandomNTCPRouter (true)->GetIdentHash (), 0, msg);
|
{
|
||||||
// request destination
|
// request destination
|
||||||
I2NPMessage * msg = i2p::CreateDatabaseLookupMsg (buf, inbound->GetNextIdentHash (),
|
auto msg = dest->CreateRequestMessage (r, dest->GetLastReplyTunnel ());
|
||||||
inbound->GetNextTunnelID ());
|
outbound->GetTunnelGateway ().PutTunnelDataMsg (r->GetIdentHash (), 0, msg);
|
||||||
outbound->GetTunnelGateway ().PutTunnelDataMsg (router, 0, msg);
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// request router
|
||||||
|
LogPrint ("Found new floodfill. Request it");
|
||||||
|
RequestedDestination * d2 = CreateRequestedDestination (router, false, false);
|
||||||
|
d2->SetLastOutboundTunnel (outbound);
|
||||||
|
I2NPMessage * msg = d2->CreateRequestMessage (dest->GetLastRouter (), inbound);
|
||||||
|
outbound->GetTunnelGateway ().PutTunnelDataMsg (
|
||||||
|
dest->GetLastRouter ()->GetIdentHash (), 0, msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (outbound)
|
if (outbound)
|
||||||
outbound->GetTunnelGateway ().SendBuffer ();
|
outbound->GetTunnelGateway ().SendBuffer ();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// no more requests for detination possible. delete it
|
||||||
|
m_RequestedDestinations.erase (it);
|
||||||
|
delete it->second;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
LogPrint ("Requested destination for ", key, " not found");
|
||||||
i2p::DeleteI2NPMessage (msg);
|
i2p::DeleteI2NPMessage (msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetDb::Explore ()
|
void NetDb::Explore ()
|
||||||
{
|
{
|
||||||
m_LastOutboundTunnel = i2p::tunnel::tunnels.GetNextOutboundTunnel ();
|
auto outbound = i2p::tunnel::tunnels.GetNextOutboundTunnel ();
|
||||||
m_LastInboundTunnel = i2p::tunnel::tunnels.GetNextInboundTunnel ();
|
auto inbound = i2p::tunnel::tunnels.GetNextInboundTunnel ();
|
||||||
if (m_LastOutboundTunnel && m_LastInboundTunnel)
|
if (outbound && inbound)
|
||||||
{
|
{
|
||||||
m_LastFloodfill = GetRandomNTCPRouter (true);
|
auto floodfill = GetRandomNTCPRouter (true);
|
||||||
if (m_LastFloodfill)
|
if (floodfill)
|
||||||
{
|
{
|
||||||
LogPrint ("Exploring new routers ...");
|
LogPrint ("Exploring new routers ...");
|
||||||
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
||||||
rnd.GenerateBlock (m_Exploratory, 32);
|
uint8_t randomHash[32];
|
||||||
|
rnd.GenerateBlock (randomHash, 32);
|
||||||
|
RequestedDestination * dest = CreateRequestedDestination (IdentHash (randomHash), false, true);
|
||||||
|
dest->SetLastOutboundTunnel (outbound);
|
||||||
|
|
||||||
m_LastOutboundTunnel->GetTunnelGateway ().PutTunnelDataMsg (m_LastFloodfill->GetIdentHash (), 0,
|
outbound->GetTunnelGateway ().PutTunnelDataMsg (floodfill->GetIdentHash (), 0,
|
||||||
CreateDatabaseStoreMsg ()); // tell floodfill about us
|
CreateDatabaseStoreMsg ()); // tell floodfill about us
|
||||||
m_LastOutboundTunnel->GetTunnelGateway ().PutTunnelDataMsg (m_LastFloodfill->GetIdentHash (), 0,
|
outbound->GetTunnelGateway ().PutTunnelDataMsg (floodfill->GetIdentHash (), 0,
|
||||||
i2p::CreateDatabaseLookupMsg (m_Exploratory, m_LastInboundTunnel->GetNextIdentHash (),
|
dest->CreateRequestMessage (floodfill, inbound)); // explore
|
||||||
m_LastInboundTunnel->GetNextTunnelID (), true)); // explore
|
outbound->GetTunnelGateway ().SendBuffer ();
|
||||||
m_LastOutboundTunnel->GetTunnelGateway ().SendBuffer ();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RequestedDestination * NetDb::CreateRequestedDestination (const IdentHash& dest,
|
||||||
|
bool isLeaseSet, bool isExploratory)
|
||||||
|
{
|
||||||
|
auto it = m_RequestedDestinations.find (dest);
|
||||||
|
if (it == m_RequestedDestinations.end ()) // not exist yet
|
||||||
|
{
|
||||||
|
RequestedDestination * d = new RequestedDestination (dest, isLeaseSet, isExploratory);
|
||||||
|
m_RequestedDestinations[dest] = d;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetDb::DeleteRequestedDestination (const IdentHash& dest)
|
||||||
|
{
|
||||||
|
auto it = m_RequestedDestinations.find (dest);
|
||||||
|
if (it != m_RequestedDestinations.end ())
|
||||||
|
{
|
||||||
|
m_RequestedDestinations.erase (it);
|
||||||
|
delete it->second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const RouterInfo * NetDb::GetRandomNTCPRouter (bool floodfillOnly) const
|
const RouterInfo * NetDb::GetRandomNTCPRouter (bool floodfillOnly) const
|
||||||
{
|
{
|
||||||
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
||||||
|
39
NetDb.h
39
NetDb.h
@ -2,6 +2,7 @@
|
|||||||
#define NETDB_H__
|
#define NETDB_H__
|
||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <set>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@ -15,6 +16,35 @@ namespace i2p
|
|||||||
{
|
{
|
||||||
namespace data
|
namespace data
|
||||||
{
|
{
|
||||||
|
class RequestedDestination
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
RequestedDestination (const IdentHash& destination, bool isLeaseSet, bool isExploratory = false):
|
||||||
|
m_Destination (destination), m_IsLeaseSet (isLeaseSet), m_IsExploratory (isExploratory),
|
||||||
|
m_LastRouter (nullptr), m_LastReplyTunnel (nullptr), m_LastOutboundTunnel (nullptr) {};
|
||||||
|
|
||||||
|
const IdentHash& GetDestination () const { return m_Destination; };
|
||||||
|
int GetNumExcludedPeers () const { return m_ExcludedPeers.size (); };
|
||||||
|
const RouterInfo * GetLastRouter () const { return m_LastRouter; };
|
||||||
|
const i2p::tunnel::InboundTunnel * GetLastReplyTunnel () const { return m_LastReplyTunnel; };
|
||||||
|
bool IsExploratory () const { return m_IsExploratory; };
|
||||||
|
bool IsExcluded (const IdentHash& ident) const { return m_ExcludedPeers.count (ident); };
|
||||||
|
I2NPMessage * CreateRequestMessage (const RouterInfo * router, const i2p::tunnel::InboundTunnel * replyTunnel);
|
||||||
|
|
||||||
|
i2p::tunnel::OutboundTunnel * GetLastOutboundTunnel () const { return m_LastOutboundTunnel; };
|
||||||
|
void SetLastOutboundTunnel (i2p::tunnel::OutboundTunnel * tunnel) { m_LastOutboundTunnel = tunnel; };
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
IdentHash m_Destination;
|
||||||
|
bool m_IsLeaseSet, m_IsExploratory;
|
||||||
|
std::set<IdentHash> m_ExcludedPeers;
|
||||||
|
const RouterInfo * m_LastRouter;
|
||||||
|
const i2p::tunnel::InboundTunnel * m_LastReplyTunnel;
|
||||||
|
i2p::tunnel::OutboundTunnel * m_LastOutboundTunnel;
|
||||||
|
};
|
||||||
|
|
||||||
class NetDb
|
class NetDb
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -50,17 +80,18 @@ namespace data
|
|||||||
void Explore ();
|
void Explore ();
|
||||||
const RouterInfo * GetClosestFloodfill (const IdentHash& destination) const;
|
const RouterInfo * GetClosestFloodfill (const IdentHash& destination) const;
|
||||||
|
|
||||||
|
RequestedDestination * CreateRequestedDestination (const IdentHash& dest,
|
||||||
|
bool isLeaseSet, bool isExploratory = false);
|
||||||
|
void DeleteRequestedDestination (const IdentHash& dest);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::map<IdentHash, LeaseSet *> m_LeaseSets;
|
std::map<IdentHash, LeaseSet *> m_LeaseSets;
|
||||||
std::map<IdentHash, RouterInfo *> m_RouterInfos;
|
std::map<IdentHash, RouterInfo *> m_RouterInfos;
|
||||||
|
std::map<IdentHash, RequestedDestination *> m_RequestedDestinations;
|
||||||
|
|
||||||
bool m_IsRunning;
|
bool m_IsRunning;
|
||||||
std::thread * m_Thread;
|
std::thread * m_Thread;
|
||||||
uint8_t m_Exploratory[32];
|
|
||||||
const RouterInfo * m_LastFloodfill;
|
|
||||||
i2p::tunnel::OutboundTunnel * m_LastOutboundTunnel;
|
|
||||||
i2p::tunnel::InboundTunnel * m_LastInboundTunnel;
|
|
||||||
i2p::util::Queue<I2NPMessage> m_Queue; // of I2NPDatabaseStoreMsg
|
i2p::util::Queue<I2NPMessage> m_Queue; // of I2NPDatabaseStoreMsg
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user