take LeaseSet from GarlicDestination

This commit is contained in:
orignal 2014-10-07 21:08:00 -04:00
parent 49d67bada0
commit fccadb752f
5 changed files with 29 additions and 28 deletions

View File

@ -23,7 +23,6 @@ namespace stream
StreamingDestination (boost::asio::io_service& service, const i2p::data::PrivateKeys& keys, bool isPublic); StreamingDestination (boost::asio::io_service& service, const i2p::data::PrivateKeys& keys, bool isPublic);
~StreamingDestination (); ~StreamingDestination ();
const i2p::data::LeaseSet * GetLeaseSet ();
i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; }; i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; };
Stream * CreateNewOutgoingStream (const i2p::data::LeaseSet& remote); Stream * CreateNewOutgoingStream (const i2p::data::LeaseSet& remote);
@ -44,6 +43,9 @@ namespace stream
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionPublicKey; }; const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionPublicKey; };
void SetLeaseSetUpdated (); void SetLeaseSetUpdated ();
// implements GarlicDestination
const i2p::data::LeaseSet * GetLeaseSet ();
private: private:
Stream * CreateNewIncomingStream (); Stream * CreateNewIncomingStream ();

View File

@ -14,9 +14,10 @@ namespace i2p
{ {
namespace garlic namespace garlic
{ {
GarlicRoutingSession::GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags): GarlicRoutingSession::GarlicRoutingSession (GarlicDestination * owner,
m_Destination (destination), m_IsAcknowledged (false), m_NumTags (numTags), const i2p::data::RoutingDestination * destination, int numTags):
m_NextTag (-1), m_SessionTags (0), m_TagsCreationTime (0), m_LocalLeaseSet (nullptr) m_Owner (owner), m_Destination (destination), m_IsAcknowledged (false),
m_NumTags (numTags), m_NextTag (-1), m_SessionTags (0), m_TagsCreationTime (0)
{ {
// create new session tags and session key // create new session tags and session key
m_Rnd.GenerateBlock (m_SessionKey, 32); m_Rnd.GenerateBlock (m_SessionKey, 32);
@ -31,8 +32,8 @@ namespace garlic
} }
GarlicRoutingSession::GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag): GarlicRoutingSession::GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag):
m_Destination (nullptr), m_IsAcknowledged (true), m_NumTags (1), m_NextTag (0), m_Owner (nullptr), m_Destination (nullptr), m_IsAcknowledged (true), m_NumTags (1),
m_LocalLeaseSet (nullptr) m_NextTag (0)
{ {
memcpy (m_SessionKey, sessionKey, 32); memcpy (m_SessionKey, sessionKey, 32);
m_Encryption.SetKey (m_SessionKey); m_Encryption.SetKey (m_SessionKey);
@ -57,9 +58,8 @@ namespace garlic
} }
} }
I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg, const i2p::data::LeaseSet * leaseSet) I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg, bool attachLeaseSet)
{ {
if (leaseSet) m_LocalLeaseSet = leaseSet;
I2NPMessage * m = NewI2NPMessage (); I2NPMessage * m = NewI2NPMessage ();
size_t len = 0; size_t len = 0;
uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length
@ -117,7 +117,7 @@ namespace garlic
} }
} }
// AES block // AES block
len += CreateAESBlock (buf, msg, leaseSet); len += CreateAESBlock (buf, msg, attachLeaseSet);
m_NextTag++; m_NextTag++;
*(uint32_t *)(m->GetPayload ()) = htobe32 (len); *(uint32_t *)(m->GetPayload ()) = htobe32 (len);
m->len += len + 4; m->len += len + 4;
@ -166,7 +166,7 @@ namespace garlic
*numCloves = 0; *numCloves = 0;
size++; size++;
if (m_LocalLeaseSet) if (m_Owner)
{ {
if (m_NextTag < 0) // new session if (m_NextTag < 0) // new session
{ {
@ -183,7 +183,7 @@ namespace garlic
if (attachLeaseSet) if (attachLeaseSet)
{ {
// clove if our leaseSet must be attached // clove if our leaseSet must be attached
auto leaseSet = CreateDatabaseStoreMsg (m_LocalLeaseSet); auto leaseSet = CreateDatabaseStoreMsg (m_Owner->GetLeaseSet ());
size += CreateGarlicClove (payload + size, leaseSet, false); size += CreateGarlicClove (payload + size, leaseSet, false);
DeleteI2NPMessage (leaseSet); DeleteI2NPMessage (leaseSet);
(*numCloves)++; (*numCloves)++;
@ -235,9 +235,9 @@ namespace garlic
size_t GarlicRoutingSession::CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID) size_t GarlicRoutingSession::CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID)
{ {
size_t size = 0; size_t size = 0;
if (m_LocalLeaseSet) if (m_Owner)
{ {
auto leases = m_LocalLeaseSet->GetNonExpiredLeases (); auto leases = m_Owner->GetLeaseSet ()->GetNonExpiredLeases ();
if (!leases.empty ()) if (!leases.empty ())
{ {
buf[size] = eGarlicDeliveryTypeTunnel << 5; // delivery instructions flag tunnel buf[size] = eGarlicDeliveryTypeTunnel << 5; // delivery instructions flag tunnel
@ -443,7 +443,7 @@ namespace garlic
session = it->second; session = it->second;
if (!session) if (!session)
{ {
session = new GarlicRoutingSession (&destination, numTags); session = new GarlicRoutingSession (this, &destination, numTags);
std::unique_lock<std::mutex> l(m_SessionsMutex); std::unique_lock<std::mutex> l(m_SessionsMutex);
m_Sessions[destination.GetIdentHash ()] = session; m_Sessions[destination.GetIdentHash ()] = session;
} }

View File

@ -39,15 +39,16 @@ namespace garlic
const int TAGS_EXPIRATION_TIMEOUT = 900; // 15 minutes const int TAGS_EXPIRATION_TIMEOUT = 900; // 15 minutes
typedef i2p::data::Tag<32> SessionTag; typedef i2p::data::Tag<32> SessionTag;
class GarlicDestination;
class GarlicRoutingSession class GarlicRoutingSession
{ {
public: public:
GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags); GarlicRoutingSession (GarlicDestination * owner, const i2p::data::RoutingDestination * destination, int numTags);
GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag); // one time encryption GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag); // one time encryption
~GarlicRoutingSession (); ~GarlicRoutingSession ();
I2NPMessage * WrapSingleMessage (I2NPMessage * msg, const i2p::data::LeaseSet * leaseSet); I2NPMessage * WrapSingleMessage (I2NPMessage * msg, bool attachLeaseSet = false);
int GetNextTag () const { return m_NextTag; }; int GetNextTag () const { return m_NextTag; };
bool IsAcknowledged () const { return m_IsAcknowledged; }; bool IsAcknowledged () const { return m_IsAcknowledged; };
@ -64,13 +65,13 @@ namespace garlic
private: private:
GarlicDestination * m_Owner;
const i2p::data::RoutingDestination * m_Destination; const i2p::data::RoutingDestination * m_Destination;
uint8_t m_SessionKey[32]; uint8_t m_SessionKey[32];
bool m_IsAcknowledged; bool m_IsAcknowledged;
int m_NumTags, m_NextTag; int m_NumTags, m_NextTag;
SessionTag * m_SessionTags; // m_NumTags*32 bytes SessionTag * m_SessionTags; // m_NumTags*32 bytes
uint32_t m_TagsCreationTime; // seconds since epoch uint32_t m_TagsCreationTime; // seconds since epoch
const i2p::data::LeaseSet * m_LocalLeaseSet;
i2p::crypto::CBCEncryption m_Encryption; i2p::crypto::CBCEncryption m_Encryption;
CryptoPP::AutoSeededRandomPool m_Rnd; CryptoPP::AutoSeededRandomPool m_Rnd;
@ -90,6 +91,8 @@ namespace garlic
void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag
void HandleGarlicMessage (I2NPMessage * msg); void HandleGarlicMessage (I2NPMessage * msg);
virtual const i2p::data::LeaseSet * GetLeaseSet () = 0; // TODO
private: private:
void HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr<i2p::crypto::CBCDecryption> decryption, void HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr<i2p::crypto::CBCDecryption> decryption,

View File

@ -42,6 +42,9 @@ namespace i2p
const uint8_t * GetEncryptionPublicKey () const { return GetIdentity ().GetStandardIdentity ().publicKey; }; const uint8_t * GetEncryptionPublicKey () const { return GetIdentity ().GetStandardIdentity ().publicKey; };
void SetLeaseSetUpdated () {}; void SetLeaseSetUpdated () {};
void HandleDataMessage (const uint8_t * buf, size_t len) {}; void HandleDataMessage (const uint8_t * buf, size_t len) {};
// implements GarlicDestination
const i2p::data::LeaseSet * GetLeaseSet () { return nullptr; };
private: private:

View File

@ -410,13 +410,6 @@ namespace stream
} }
} }
const i2p::data::LeaseSet * leaseSet = nullptr;
if (m_LeaseSetUpdated)
{
leaseSet = m_LocalDestination.GetLeaseSet ();
m_LeaseSetUpdated = false;
}
auto ts = i2p::util::GetMillisecondsSinceEpoch (); auto ts = i2p::util::GetMillisecondsSinceEpoch ();
if (ts >= m_CurrentRemoteLease.endDate) if (ts >= m_CurrentRemoteLease.endDate)
UpdateCurrentRemoteLease (); UpdateCurrentRemoteLease ();
@ -427,14 +420,14 @@ namespace stream
{ {
auto msg = m_RoutingSession->WrapSingleMessage ( auto msg = m_RoutingSession->WrapSingleMessage (
m_LocalDestination.CreateDataMessage (it->GetBuffer (), it->GetLength ()), m_LocalDestination.CreateDataMessage (it->GetBuffer (), it->GetLength ()),
leaseSet); m_LeaseSetUpdated);
msgs.push_back (i2p::tunnel::TunnelMessageBlock msgs.push_back (i2p::tunnel::TunnelMessageBlock
{ {
i2p::tunnel::eDeliveryTypeTunnel, i2p::tunnel::eDeliveryTypeTunnel,
m_CurrentRemoteLease.tunnelGateway, m_CurrentRemoteLease.tunnelID, m_CurrentRemoteLease.tunnelGateway, m_CurrentRemoteLease.tunnelID,
msg msg
}); });
leaseSet = nullptr; // send leaseSet only one time m_LeaseSetUpdated = false; // send leaseSet only one time
} }
m_LocalDestination.SendTunnelDataMsgs (msgs); m_LocalDestination.SendTunnelDataMsgs (msgs);
} }