Skip to content

Commit

Permalink
Inroduced IdentHash
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Nov 29, 2013
1 parent fbbcc69 commit 885e996
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 125 deletions.
36 changes: 32 additions & 4 deletions LeaseSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#define LEASE_SET_H__

#include <inttypes.h>
#include <string.h>
#include <list>

namespace i2p
{
namespace data
{
{

#pragma pack(1)
struct Lease
{
Expand All @@ -17,10 +19,36 @@ namespace data
};
#pragma pack()

class IdentHash
{
public:

IdentHash (const uint8_t * hash) { memcpy (m_Hash, hash, 32); };
IdentHash (const IdentHash& ) = default;
IdentHash (IdentHash&& ) = default;
IdentHash () = default;

IdentHash& operator= (const IdentHash& ) = default;
IdentHash& operator= (IdentHash&& ) = default;

uint8_t * operator()() { return m_Hash; };
const uint8_t * operator()() const { return m_Hash; };

operator uint8_t * () { return m_Hash; };
operator const uint8_t * () const { return m_Hash; };

bool operator== (const IdentHash& other) const { return !memcmp (m_Hash, other.m_Hash, 32); };
bool operator< (const IdentHash& other) const { return memcmp (m_Hash, other.m_Hash, 32) < 0; };

private:

uint8_t m_Hash[32];
};

class RoutingDestination // TODO: move to separate file later
{
public:
virtual const uint8_t * GetIdentHash () const = 0;
virtual const IdentHash& GetIdentHash () const = 0;
virtual const uint8_t * GetEncryptionPublicKey () const = 0;
virtual bool IsDestination () const = 0; // for garlic
};
Expand All @@ -32,14 +60,14 @@ namespace data
LeaseSet (const uint8_t * buf, int len);

// implements RoutingDestination
const uint8_t * GetIdentHash () const { return m_IdentHash; };
const IdentHash& GetIdentHash () const { return m_IdentHash; };
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; };
bool IsDestination () const { return true; };

private:

std::list<Lease> m_Leases;
uint8_t m_IdentHash[32];
IdentHash m_IdentHash;
uint8_t m_EncryptionKey[256];
};
}
Expand Down
58 changes: 41 additions & 17 deletions NTCPSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace i2p
{
namespace ntcp
{
NTCPSession::NTCPSession (boost::asio::ip::tcp::socket& s, const i2p::data::RouterInfo * in_RemoteRouterInfo):
m_Socket (s), m_IsEstablished (false), m_ReceiveBufferOffset (0),
m_NextMessage (nullptr), m_DelayedMessage (nullptr)
NTCPSession::NTCPSession (boost::asio::io_service& service, const i2p::data::RouterInfo * in_RemoteRouterInfo):
m_Socket (service), m_TerminationTimer (service), m_IsEstablished (false),
m_ReceiveBufferOffset (0), m_NextMessage (nullptr)
{
if (in_RemoteRouterInfo)
m_RemoteRouterInfo = *in_RemoteRouterInfo;
Expand Down Expand Up @@ -52,8 +52,9 @@ namespace ntcp
{
m_IsEstablished = false;
m_Socket.close ();
if (m_DelayedMessage)
delete m_DelayedMessage;
for (auto it :m_DelayedMessages)
delete it;
m_DelayedMessages.clear ();
// TODO: notify tunnels
i2p::transports.RemoveNTCPSession (this);
delete this;
Expand All @@ -64,16 +65,15 @@ namespace ntcp
{
LogPrint ("NTCP session connected");
m_IsEstablished = true;
i2p::transports.AddNTCPSession (this);

SendTimeSyncMessage ();
SendI2NPMessage (CreateDatabaseStoreMsg ()); // we tell immediately who we are

if (m_DelayedMessage)
if (!m_DelayedMessages.empty ())
{
i2p::I2NPMessage * delayedMessage = m_DelayedMessage;
m_DelayedMessage = 0;
SendI2NPMessage (delayedMessage);
for (auto it :m_DelayedMessages)
SendI2NPMessage (it);
m_DelayedMessages.clear ();
}
}

Expand Down Expand Up @@ -467,7 +467,8 @@ namespace ntcp
}

boost::asio::async_write (m_Socket, boost::asio::buffer (sendBuffer, l), boost::asio::transfer_all (),
boost::bind(&NTCPSession::HandleSent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, msg));
boost::bind(&NTCPSession::HandleSent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, msg));
ScheduleTermination (); // reset termination timer
}

void NTCPSession::HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg)
Expand Down Expand Up @@ -497,21 +498,39 @@ namespace ntcp
if (m_IsEstablished)
Send (msg);
else
m_DelayedMessage = msg;
m_DelayedMessages.push_back (msg);
}
}

void NTCPSession::ScheduleTermination ()
{
m_TerminationTimer.cancel ();
m_TerminationTimer.expires_from_now (boost::posix_time::seconds(TERMINATION_TIMEOUT));
m_TerminationTimer.async_wait (boost::bind (&NTCPSession::HandleTerminationTimer,
this, boost::asio::placeholders::error));
}

void NTCPSession::HandleTerminationTimer (const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
LogPrint ("No activity fo ", TERMINATION_TIMEOUT, " seconds");
m_Socket.close ();
}
}


NTCPClient::NTCPClient (boost::asio::io_service& service, const char * address,
int port, const i2p::data::RouterInfo& in_RouterInfo): NTCPSession (m_Socket, &in_RouterInfo),
m_Socket (service), m_Endpoint (boost::asio::ip::address::from_string (address), port)
int port, const i2p::data::RouterInfo& in_RouterInfo): NTCPSession (service, &in_RouterInfo),
m_Endpoint (boost::asio::ip::address::from_string (address), port)
{
Connect ();
}

void NTCPClient::Connect ()
{
LogPrint ("Connecting to ", m_Endpoint.address ().to_string (),":", m_Endpoint.port ());
m_Socket.async_connect (m_Endpoint, boost::bind (&NTCPClient::HandleConnect,
GetSocket ().async_connect (m_Endpoint, boost::bind (&NTCPClient::HandleConnect,
this, boost::asio::placeholders::error));
}

Expand All @@ -529,9 +548,14 @@ namespace ntcp
}
}

NTCPServerConnection::NTCPServerConnection (boost::asio::io_service& service):
NTCPSession (m_Socket), m_Socket (service)
void NTCPServerConnection::Connected ()
{
LogPrint ("NTCP server session connected");
SetIsEstablished (true);
i2p::transports.AddNTCPSession (this);

SendTimeSyncMessage ();
SendI2NPMessage (CreateDatabaseStoreMsg ()); // we tell immediately who we are
}
}
}
28 changes: 18 additions & 10 deletions NTCPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <inttypes.h>
#include <mutex>
#include <list>
#include <boost/asio.hpp>
#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
Expand Down Expand Up @@ -60,13 +61,15 @@ namespace ntcp

#pragma pack()

const int TERMINATION_TIMEOUT = 60; // 1 minute
class NTCPSession
{
public:

NTCPSession (boost::asio::ip::tcp::socket& s, const i2p::data::RouterInfo * in_RemoteRouterInfo = 0);
NTCPSession (boost::asio::io_service& service, const i2p::data::RouterInfo * in_RemoteRouterInfo = 0);
virtual ~NTCPSession () {};

boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
bool IsEstablished () const { return m_IsEstablished; };
const i2p::data::RouterInfo& GetRemoteRouterInfo () const { return m_RemoteRouterInfo; };

Expand All @@ -77,7 +80,9 @@ namespace ntcp
protected:

void Terminate ();
void Connected ();
virtual void Connected ();
void SendTimeSyncMessage ();
void SetIsEstablished (bool isEstablished) { m_IsEstablished = isEstablished; }

private:

Expand Down Expand Up @@ -106,11 +111,15 @@ namespace ntcp
void Send (i2p::I2NPMessage * msg);
void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg);

void SendTimeSyncMessage ();

// timer
void ScheduleTermination ();
void HandleTerminationTimer (const boost::system::error_code& ecode);

private:

boost::asio::ip::tcp::socket& m_Socket;
boost::asio::ip::tcp::socket m_Socket;
boost::asio::deadline_timer m_TerminationTimer;
bool m_IsEstablished;

CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption m_Decryption;
Expand All @@ -127,7 +136,8 @@ namespace ntcp
uint8_t m_ReceiveBuffer[i2p::NTCP_MAX_MESSAGE_SIZE*2], m_TimeSyncBuffer[16];
int m_ReceiveBufferOffset;

i2p::I2NPMessage * m_NextMessage, * m_DelayedMessage;
i2p::I2NPMessage * m_NextMessage;
std::list<i2p::I2NPMessage *> m_DelayedMessages;
size_t m_NextMessageOffset;

std::mutex m_EncryptionMutex;
Expand All @@ -146,20 +156,18 @@ namespace ntcp

private:

boost::asio::ip::tcp::socket m_Socket;
boost::asio::ip::tcp::endpoint m_Endpoint;
};

class NTCPServerConnection: public NTCPSession
{
public:

NTCPServerConnection (boost::asio::io_service& service);
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
NTCPServerConnection (boost::asio::io_service& service): NTCPSession (service) {};

private:
protected:

boost::asio::ip::tcp::socket m_Socket;
virtual void Connected ();
};
}
}
Expand Down
17 changes: 8 additions & 9 deletions NetDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,34 +95,33 @@ namespace data
void NetDb::AddRouterInfo (uint8_t * buf, int len)
{
RouterInfo * r = new RouterInfo (buf, len);
std::string hash((const char *)r->GetIdentHash (), 32);
auto it = m_RouterInfos.find(hash);
auto it = m_RouterInfos.find(r->GetIdentHash ());
if (it != m_RouterInfos.end ())
{
if (r->GetTimestamp () > it->second->GetTimestamp ())
{
LogPrint ("RouterInfo updated");
*m_RouterInfos[hash] = *r; // we can't replace point because it's used by tunnels
*m_RouterInfos[r->GetIdentHash ()] = *r; // we can't replace point because it's used by tunnels
}
else
delete r;
}
else
{
LogPrint ("New RouterInfo added");
m_RouterInfos[hash] = r;
m_RouterInfos[r->GetIdentHash ()] = r;
}
}

void NetDb::AddLeaseSet (uint8_t * buf, int len)
{
LeaseSet * l = new LeaseSet (buf, len);
m_LeaseSets[std::string ((const char *)l->GetIdentHash (), 32)] = l;
m_LeaseSets[l->GetIdentHash ()] = l;
}

RouterInfo * NetDb::FindRouter (const uint8_t * ident) const
RouterInfo * NetDb::FindRouter (const IdentHash& ident) const
{
auto it = m_RouterInfos.find (std::string ((const char *)ident, 32));
auto it = m_RouterInfos.find (ident);
if (it != m_RouterInfos.end ())
return it->second;
else
Expand All @@ -143,7 +142,7 @@ namespace data
for (boost::filesystem::directory_iterator it1 (it->path ()); it1 != end; ++it1)
{
RouterInfo * r = new RouterInfo (it1->path ().c_str ());
m_RouterInfos[std::string ((const char *)r->GetIdentHash (), 32)] = r;
m_RouterInfos[r->GetIdentHash ()] = r;
numRouters++;
}
}
Expand Down Expand Up @@ -252,7 +251,7 @@ namespace data

if (isExploratory)
{
if (m_RouterInfos.find (std::string ((const char *)router, 32)) == m_RouterInfos.end ())
if (m_RouterInfos.find (IdentHash(router)) == m_RouterInfos.end ())
{
LogPrint ("Found new router. Requesting RouterInfo ...");
if (outbound && inbound)
Expand Down
6 changes: 3 additions & 3 deletions NetDb.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace data

void AddRouterInfo (uint8_t * buf, int len);
void AddLeaseSet (uint8_t * buf, int len);
RouterInfo * FindRouter (const uint8_t * ident) const;
RouterInfo * FindRouter (const IdentHash& ident) const;

void RequestDestination (const char * b32, const uint8_t * router); // in base32
void RequestDestination (const uint8_t * destination, const uint8_t * router);
Expand All @@ -47,8 +47,8 @@ namespace data

private:

std::map<std::string, LeaseSet *> m_LeaseSets;
std::map<std::string, RouterInfo *> m_RouterInfos;
std::map<IdentHash, LeaseSet *> m_LeaseSets;
std::map<IdentHash, RouterInfo *> m_RouterInfos;

bool m_IsRunning;
std::thread * m_Thread;
Expand Down
5 changes: 3 additions & 2 deletions RouterInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace data
void SetRouterIdentity (const RouterIdentity& identity);
const char * GetIdentHashBase64 () const { return m_IdentHashBase64; };
const char * GetIdentHashAbbreviation () const { return m_IdentHashAbbreviation; };
uint64_t GetTimestamp () const { return m_Timestamp; };
const std::vector<Address>& GetAddresses () const { return m_Addresses; };
Address * GetNTCPAddress ();

Expand All @@ -70,7 +71,7 @@ namespace data
void SetUpdated (bool updated) { m_IsUpdated = updated; };

// implements RoutingDestination
const uint8_t * GetIdentHash () const { return m_IdentHash; };
const IdentHash& GetIdentHash () const { return m_IdentHash; };
const uint8_t * GetEncryptionPublicKey () const { return m_RouterIdentity.publicKey; };
bool IsDestination () const { return false; };

Expand All @@ -86,7 +87,7 @@ namespace data
private:

RouterIdentity m_RouterIdentity;
uint8_t m_IdentHash[32];
IdentHash m_IdentHash;
char m_IdentHashBase64[48], m_IdentHashAbbreviation[5];
char m_Buffer[2048];
int m_BufferLen;
Expand Down
Loading

0 comments on commit 885e996

Please sign in to comment.