mirror of
https://github.com/PurpleI2P/i2pd
synced 2024-11-09 15:50:26 +03:00
kademlia
This commit is contained in:
parent
4f9a977022
commit
4f1f08b805
27
Identity.cpp
27
Identity.cpp
@ -1,3 +1,5 @@
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <cryptopp/sha.h>
|
||||
#include <cryptopp/osrng.h>
|
||||
#include <cryptopp/dh.h>
|
||||
@ -43,5 +45,30 @@ namespace data
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
RoutingKey CreateRoutingKey (const IdentHash& ident)
|
||||
{
|
||||
uint8_t buf[41]; // ident + yyyymmdd
|
||||
memcpy (buf, (const uint8_t *)ident, 32);
|
||||
time_t t = time (nullptr);
|
||||
struct tm tm;
|
||||
gmtime_r (&t, &tm);
|
||||
sprintf ((char *)(buf + 32),"%4i%2i%2i", tm.tm_year, tm.tm_mon, tm.tm_mday);
|
||||
|
||||
RoutingKey key;
|
||||
CryptoPP::SHA256().CalculateDigest(key.hash, buf, 40);
|
||||
return key;
|
||||
}
|
||||
|
||||
XORMetric operator^(const RoutingKey& key1, const RoutingKey& key2)
|
||||
{
|
||||
// TODO: implementation depends on CPU
|
||||
XORMetric m;
|
||||
((uint64_t *)m.metric)[0] = ((uint64_t *)key1.hash)[0] ^ ((uint64_t *)key2.hash)[0];
|
||||
((uint64_t *)m.metric)[1] = ((uint64_t *)key1.hash)[1] ^ ((uint64_t *)key2.hash)[1];
|
||||
((uint64_t *)m.metric)[2] = ((uint64_t *)key1.hash)[2] ^ ((uint64_t *)key2.hash)[2];
|
||||
((uint64_t *)m.metric)[3] = ((uint64_t *)key1.hash)[3] ^ ((uint64_t *)key2.hash)[3];
|
||||
return m;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
19
Identity.h
19
Identity.h
@ -57,7 +57,26 @@ namespace data
|
||||
|
||||
IdentHash CalculateIdentHash (const Identity& identity);
|
||||
Keys CreateRandomKeys ();
|
||||
|
||||
// kademlia
|
||||
struct RoutingKey
|
||||
{
|
||||
uint8_t hash[32];
|
||||
};
|
||||
|
||||
struct XORMetric
|
||||
{
|
||||
uint8_t metric[32];
|
||||
|
||||
void SetMin () { memset (metric, 0, 32); };
|
||||
void SetMax () { memset (metric, 0xFF, 32); };
|
||||
bool operator< (const XORMetric& other) const { return memcmp (metric, other.metric, 32) < 0; };
|
||||
};
|
||||
|
||||
RoutingKey CreateRoutingKey (const IdentHash& ident);
|
||||
XORMetric operator^(const RoutingKey& key1, const RoutingKey& key2);
|
||||
|
||||
// destination for delivery instuctions
|
||||
class RoutingDestination
|
||||
{
|
||||
public:
|
||||
|
21
NetDb.cpp
21
NetDb.cpp
@ -388,5 +388,26 @@ namespace data
|
||||
{
|
||||
if (msg) m_Queue.Put (msg);
|
||||
}
|
||||
|
||||
const RouterInfo * NetDb::GetClosestFloodfill (const IdentHash& destination) const
|
||||
{
|
||||
RouterInfo * r = nullptr;
|
||||
XORMetric minMetric;
|
||||
RoutingKey destKey = CreateRoutingKey (destination);
|
||||
minMetric.SetMax ();
|
||||
for (auto it: m_RouterInfos)
|
||||
{
|
||||
if (it.second->IsFloodfill () &&! it.second->IsUnreachable ())
|
||||
{
|
||||
XORMetric m = destKey ^ it.second->GetRoutingKey ();
|
||||
if (m < minMetric)
|
||||
{
|
||||
minMetric = m;
|
||||
r = it.second;
|
||||
}
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
1
NetDb.h
1
NetDb.h
@ -48,6 +48,7 @@ namespace data
|
||||
void SaveUpdated (const char * directory);
|
||||
void Run (); // exploratory thread
|
||||
void Explore ();
|
||||
const RouterInfo * GetClosestFloodfill (const IdentHash& destination) const;
|
||||
|
||||
private:
|
||||
|
||||
|
@ -35,6 +35,7 @@ namespace data
|
||||
m_RouterIdentity = identity;
|
||||
m_IdentHash = CalculateIdentHash (m_RouterIdentity);
|
||||
UpdateIdentHashBase64 ();
|
||||
UpdateRoutingKey ();
|
||||
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
|
||||
}
|
||||
|
||||
@ -126,6 +127,7 @@ namespace data
|
||||
|
||||
CryptoPP::SHA256().CalculateDigest(m_IdentHash, (uint8_t *)&m_RouterIdentity, sizeof (m_RouterIdentity));
|
||||
UpdateIdentHashBase64 ();
|
||||
UpdateRoutingKey ();
|
||||
}
|
||||
|
||||
void RouterInfo::UpdateIdentHashBase64 ()
|
||||
@ -135,6 +137,11 @@ namespace data
|
||||
memcpy (m_IdentHashAbbreviation, m_IdentHashBase64, 4);
|
||||
m_IdentHashAbbreviation[4] = 0;
|
||||
}
|
||||
|
||||
void RouterInfo::UpdateRoutingKey ()
|
||||
{
|
||||
m_RoutingKey = CreateRoutingKey (m_IdentHash);
|
||||
}
|
||||
|
||||
void RouterInfo::WriteToStream (std::ostream& s)
|
||||
{
|
||||
|
@ -46,6 +46,7 @@ namespace data
|
||||
uint64_t GetTimestamp () const { return m_Timestamp; };
|
||||
const std::vector<Address>& GetAddresses () const { return m_Addresses; };
|
||||
Address * GetNTCPAddress ();
|
||||
const RoutingKey& GetRoutingKey () const { return m_RoutingKey; };
|
||||
|
||||
void AddNTCPAddress (const char * host, int port);
|
||||
void SetProperty (const char * key, const char * value);
|
||||
@ -56,6 +57,7 @@ namespace data
|
||||
bool IsUnreachable () const { return m_IsUnreachable; };
|
||||
|
||||
void CreateBuffer ();
|
||||
void UpdateRoutingKey ();
|
||||
const char * GetBuffer () const { return m_Buffer; };
|
||||
int GetBufferLen () const { return m_BufferLen; };
|
||||
|
||||
@ -81,6 +83,7 @@ namespace data
|
||||
|
||||
Identity m_RouterIdentity;
|
||||
IdentHash m_IdentHash;
|
||||
RoutingKey m_RoutingKey;
|
||||
char m_IdentHashBase64[48], m_IdentHashAbbreviation[5];
|
||||
char m_Buffer[2048];
|
||||
int m_BufferLen;
|
||||
|
Loading…
Reference in New Issue
Block a user