Skip to content

Commit

Permalink
kademlia
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jan 4, 2014
1 parent 4f9a977 commit 4f1f08b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Identity.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <time.h>
#include <stdio.h>
#include <cryptopp/sha.h>
#include <cryptopp/osrng.h>
#include <cryptopp/dh.h>
Expand Down Expand Up @@ -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 changes: 19 additions & 0 deletions Identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions NetDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 change: 1 addition & 0 deletions NetDb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
7 changes: 7 additions & 0 deletions RouterInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace data
m_RouterIdentity = identity;
m_IdentHash = CalculateIdentHash (m_RouterIdentity);
UpdateIdentHashBase64 ();
UpdateRoutingKey ();
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
}

Expand Down Expand Up @@ -126,6 +127,7 @@ namespace data

CryptoPP::SHA256().CalculateDigest(m_IdentHash, (uint8_t *)&m_RouterIdentity, sizeof (m_RouterIdentity));
UpdateIdentHashBase64 ();
UpdateRoutingKey ();
}

void RouterInfo::UpdateIdentHashBase64 ()
Expand All @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions RouterInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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; };

Expand All @@ -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;
Expand Down

0 comments on commit 4f1f08b

Please sign in to comment.