Browse Source

pass actual LeaseSet instead of I2NP message

pull/93/head
orignal 11 years ago
parent
commit
2b5382ce9b
  1. 116
      Garlic.cpp
  2. 11
      Garlic.h
  3. 9
      Streaming.cpp
  4. 1
      Streaming.h

116
Garlic.cpp

@ -15,8 +15,8 @@ namespace i2p @@ -15,8 +15,8 @@ namespace i2p
namespace garlic
{
GarlicRoutingSession::GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags):
m_Destination (destination), m_FirstMsgID (0), m_IsAcknowledged (false),
m_NumTags (numTags), m_NextTag (-1), m_SessionTags (0), m_TagsCreationTime (0)
m_Destination (destination), m_FirstMsgID (0), m_IsAcknowledged (false), m_NumTags (numTags),
m_NextTag (-1), m_SessionTags (0), m_TagsCreationTime (0), m_LocalLeaseSet (nullptr)
{
// create new session tags and session key
m_Rnd.GenerateBlock (m_SessionKey, 32);
@ -31,7 +31,8 @@ namespace garlic @@ -31,7 +31,8 @@ namespace garlic
}
GarlicRoutingSession::GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag):
m_Destination (nullptr), m_FirstMsgID (0), m_IsAcknowledged (true), m_NumTags (1), m_NextTag (0)
m_Destination (nullptr), m_FirstMsgID (0), m_IsAcknowledged (true), m_NumTags (1), m_NextTag (0),
m_LocalLeaseSet (nullptr)
{
memcpy (m_SessionKey, sessionKey, 32);
m_Encryption.SetKey (m_SessionKey);
@ -56,8 +57,9 @@ namespace garlic @@ -56,8 +57,9 @@ namespace garlic
}
}
I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet)
I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg, const i2p::data::LeaseSet * leaseSet)
{
if (leaseSet) m_LocalLeaseSet = leaseSet;
I2NPMessage * m = NewI2NPMessage ();
size_t len = 0;
uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length
@ -122,12 +124,10 @@ namespace garlic @@ -122,12 +124,10 @@ namespace garlic
FillI2NPMessageHeader (m, eI2NPGarlic);
if (msg)
DeleteI2NPMessage (msg);
if (leaseSet)
DeleteI2NPMessage (leaseSet);
return m;
}
size_t GarlicRoutingSession::CreateAESBlock (uint8_t * buf, const I2NPMessage * msg, const I2NPMessage * leaseSet)
size_t GarlicRoutingSession::CreateAESBlock (uint8_t * buf, const I2NPMessage * msg, bool attachLeaseSet)
{
size_t blockSize = 0;
*(uint16_t *)buf = m_NextTag < 0 ? htobe16 (m_NumTags) : 0; // tag count
@ -146,7 +146,7 @@ namespace garlic @@ -146,7 +146,7 @@ namespace garlic
blockSize += 32;
buf[blockSize] = 0; // flag
blockSize++;
size_t len = CreateGarlicPayload (buf + blockSize, msg, leaseSet);
size_t len = CreateGarlicPayload (buf + blockSize, msg, attachLeaseSet);
*payloadSize = htobe32 (len);
CryptoPP::SHA256().CalculateDigest(payloadHash, buf + blockSize, len);
blockSize += len;
@ -157,7 +157,7 @@ namespace garlic @@ -157,7 +157,7 @@ namespace garlic
return blockSize;
}
size_t GarlicRoutingSession::CreateGarlicPayload (uint8_t * payload, const I2NPMessage * msg, const I2NPMessage * leaseSet)
size_t GarlicRoutingSession::CreateGarlicPayload (uint8_t * payload, const I2NPMessage * msg, bool attachLeaseSet)
{
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
uint32_t msgID = m_Rnd.GenerateWord32 ();
@ -166,18 +166,28 @@ namespace garlic @@ -166,18 +166,28 @@ namespace garlic
*numCloves = 0;
size++;
if (m_NextTag < 0) // new session
{
// clove is DeliveryStatus
size += CreateDeliveryStatusClove (payload + size, msgID);
(*numCloves)++;
m_FirstMsgID = msgID;
}
if (leaseSet)
{
// clove is our leaseSet if presented
size += CreateGarlicClove (payload + size, leaseSet, false);
(*numCloves)++;
if (m_LocalLeaseSet)
{
if (m_NextTag < 0) // new session
{
// clove is DeliveryStatus
size += CreateDeliveryStatusClove (payload + size, msgID);
if (size > 0) // successive?
{
(*numCloves)++;
m_FirstMsgID = msgID;
}
else
LogPrint ("DeliveryStatus clove was not created");
}
if (attachLeaseSet)
{
// clove if our leaseSet must be attached
auto leaseSet = CreateDatabaseStoreMsg (m_LocalLeaseSet);
size += CreateGarlicClove (payload + size, leaseSet, false);
DeleteI2NPMessage (leaseSet);
(*numCloves)++;
}
}
if (msg) // clove message ifself if presented
{
@ -225,37 +235,39 @@ namespace garlic @@ -225,37 +235,39 @@ namespace garlic
size_t GarlicRoutingSession::CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID)
{
size_t size = 0;
auto tunnel = i2p::tunnel::tunnels.GetNextInboundTunnel ();
if (tunnel)
{
buf[size] = eGarlicDeliveryTypeTunnel << 5; // delivery instructions flag tunnel
size++;
// hash and tunnelID sequence is reversed for Garlic
memcpy (buf + size, tunnel->GetNextIdentHash (), 32); // To Hash
size += 32;
*(uint32_t *)(buf + size) = htobe32 (tunnel->GetNextTunnelID ()); // tunnelID
size += 4;
}
else
{
LogPrint ("No reply tunnels for garlic DeliveryStatus found");
buf[size] = 0;// delivery instructions flag local
size++;
if (m_LocalLeaseSet)
{
auto leases = m_LocalLeaseSet->GetNonExpiredLeases ();
if (!leases.empty ())
{
buf[size] = eGarlicDeliveryTypeTunnel << 5; // delivery instructions flag tunnel
size++;
uint32_t i = m_Rnd.GenerateWord32 (0, leases.size () - 1);
// hash and tunnelID sequence is reversed for Garlic
memcpy (buf + size, leases[i].tunnelGateway, 32); // To Hash
size += 32;
*(uint32_t *)(buf + size) = htobe32 (leases[i].tunnelID); // tunnelID
size += 4;
// create msg
I2NPMessage * msg = CreateDeliveryStatusMsg (msgID);
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
size += msg->GetLength ();
DeleteI2NPMessage (msg);
// fill clove
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
*(uint32_t *)(buf + size) = htobe32 (m_Rnd.GenerateWord32 ()); // CloveID
size += 4;
*(uint64_t *)(buf + size) = htobe64 (ts); // Expiration of clove
size += 8;
memset (buf + size, 0, 3); // certificate of clove
size += 3;
}
else
LogPrint ("All tunnels of local LeaseSet expired");
}
I2NPMessage * msg = CreateDeliveryStatusMsg (msgID);
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
size += msg->GetLength ();
DeleteI2NPMessage (msg);
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
*(uint32_t *)(buf + size) = htobe32 (m_Rnd.GenerateWord32 ()); // CloveID
size += 4;
*(uint64_t *)(buf + size) = htobe64 (ts); // Expiration of clove
size += 8;
memset (buf + size, 0, 3); // certificate of clove
size += 3;
else
LogPrint ("Missing local LeaseSet");
return size;
}
@ -296,7 +308,7 @@ namespace garlic @@ -296,7 +308,7 @@ namespace garlic
}
I2NPMessage * GarlicRouting::WrapMessage (const i2p::data::RoutingDestination& destination,
I2NPMessage * msg, I2NPMessage * leaseSet)
I2NPMessage * msg, const i2p::data::LeaseSet * leaseSet)
{
auto it = m_Sessions.find (destination.GetIdentHash ());
GarlicRoutingSession * session = nullptr;

11
Garlic.h

@ -45,7 +45,7 @@ namespace garlic @@ -45,7 +45,7 @@ namespace garlic
GarlicRoutingSession (const i2p::data::RoutingDestination * destination, int numTags);
GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag); // one time encryption
~GarlicRoutingSession ();
I2NPMessage * WrapSingleMessage (I2NPMessage * msg, I2NPMessage * leaseSet);
I2NPMessage * WrapSingleMessage (I2NPMessage * msg, const i2p::data::LeaseSet * leaseSet);
int GetNextTag () const { return m_NextTag; };
uint32_t GetFirstMsgID () const { return m_FirstMsgID; };
@ -54,8 +54,8 @@ namespace garlic @@ -54,8 +54,8 @@ namespace garlic
private:
size_t CreateAESBlock (uint8_t * buf, const I2NPMessage * msg, const I2NPMessage * leaseSet);
size_t CreateGarlicPayload (uint8_t * payload, const I2NPMessage * msg, const I2NPMessage * leaseSet);
size_t CreateAESBlock (uint8_t * buf, const I2NPMessage * msg, bool attachLeaseSet);
size_t CreateGarlicPayload (uint8_t * payload, const I2NPMessage * msg, bool attachLeaseSet);
size_t CreateGarlicClove (uint8_t * buf, const I2NPMessage * msg, bool isDestination);
size_t CreateDeliveryStatusClove (uint8_t * buf, uint32_t msgID);
@ -70,7 +70,8 @@ namespace garlic @@ -70,7 +70,8 @@ namespace garlic
int m_NumTags, m_NextTag;
SessionTag * m_SessionTags; // m_NumTags*32 bytes
uint32_t m_TagsCreationTime; // seconds since epoch
const i2p::data::LeaseSet * m_LocalLeaseSet;
i2p::crypto::CBCEncryption m_Encryption;
CryptoPP::AutoSeededRandomPool m_Rnd;
};
@ -106,7 +107,7 @@ namespace garlic @@ -106,7 +107,7 @@ namespace garlic
I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * msg);
I2NPMessage * WrapMessage (const i2p::data::RoutingDestination& destination,
I2NPMessage * msg, I2NPMessage * leaseSet = nullptr);
I2NPMessage * msg, const i2p::data::LeaseSet * leaseSet = nullptr);
private:

9
Streaming.cpp

@ -394,10 +394,10 @@ namespace stream @@ -394,10 +394,10 @@ namespace stream
}
}
I2NPMessage * leaseSet = nullptr;
const i2p::data::LeaseSet * leaseSet = nullptr;
if (m_LeaseSetUpdated)
{
leaseSet = m_LocalDestination->GetLeaseSetMsg ();
leaseSet = m_LocalDestination->GetLeaseSet ();
m_LeaseSetUpdated = false;
}
@ -571,11 +571,6 @@ namespace stream @@ -571,11 +571,6 @@ namespace stream
delete stream;
}
}
I2NPMessage * StreamingDestination::GetLeaseSetMsg ()
{
return CreateDatabaseStoreMsg (GetLeaseSet ());
}
const i2p::data::LeaseSet * StreamingDestination::GetLeaseSet ()
{

1
Streaming.h

@ -143,7 +143,6 @@ namespace stream @@ -143,7 +143,6 @@ namespace stream
~StreamingDestination ();
const i2p::data::PrivateKeys& GetKeys () const { return m_Keys; };
I2NPMessage * GetLeaseSetMsg ();
const i2p::data::LeaseSet * GetLeaseSet ();
i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; };

Loading…
Cancel
Save