i2pd/LeaseSet.cpp

169 lines
4.7 KiB
C++
Raw Normal View History

2014-12-31 14:14:53 +00:00
#include <string.h>
2014-01-15 01:57:33 +00:00
#include "I2PEndian.h"
2015-11-03 14:15:49 +00:00
#include "Crypto.h"
2013-11-24 23:10:27 +00:00
#include "Log.h"
2014-01-15 01:57:33 +00:00
#include "Timestamp.h"
#include "NetDb.h"
2014-07-29 17:44:54 +00:00
#include "TunnelPool.h"
2013-11-24 23:10:27 +00:00
#include "LeaseSet.h"
namespace i2p
{
namespace data
{
2013-12-20 03:05:45 +00:00
2015-04-08 14:34:16 +00:00
LeaseSet::LeaseSet (const uint8_t * buf, size_t len):
m_IsValid (true)
2013-11-24 23:10:27 +00:00
{
2015-04-08 13:39:02 +00:00
m_Buffer = new uint8_t[len];
2014-07-29 17:44:54 +00:00
memcpy (m_Buffer, buf, len);
m_BufferLen = len;
ReadFromBuffer ();
}
LeaseSet::LeaseSet (std::shared_ptr<const i2p::tunnel::TunnelPool> pool):
2015-04-08 14:34:16 +00:00
m_IsValid (true)
{
if (!pool) return;
2014-07-29 17:44:54 +00:00
// header
auto localDestination = pool->GetLocalDestination ();
2014-12-16 02:24:01 +00:00
if (!localDestination)
{
2015-04-08 13:39:02 +00:00
m_Buffer = nullptr;
2014-12-16 02:24:01 +00:00
m_BufferLen = 0;
2015-04-08 14:34:16 +00:00
m_IsValid = false;
2015-12-18 14:08:23 +00:00
LogPrint (eLogError, "LeaseSet: Destination for local LeaseSet doesn't exist");
2014-12-16 02:24:01 +00:00
return;
}
2015-04-08 23:06:47 +00:00
m_Buffer = new uint8_t[MAX_LS_BUFFER_SIZE];
2015-11-03 14:15:49 +00:00
m_BufferLen = localDestination->GetIdentity ()->ToBuffer (m_Buffer, MAX_LS_BUFFER_SIZE);
2014-12-16 02:24:01 +00:00
memcpy (m_Buffer + m_BufferLen, localDestination->GetEncryptionPublicKey (), 256);
m_BufferLen += 256;
2015-11-03 14:15:49 +00:00
auto signingKeyLen = localDestination->GetIdentity ()->GetSigningPublicKeyLen ();
memset (m_Buffer + m_BufferLen, 0, signingKeyLen);
m_BufferLen += signingKeyLen;
auto tunnels = pool->GetInboundTunnels (5); // 5 tunnels maximum
m_Buffer[m_BufferLen] = tunnels.size (); // num leases
m_BufferLen++;
2014-07-29 17:44:54 +00:00
// leases
for (auto it: tunnels)
{
2015-03-23 16:55:42 +00:00
memcpy (m_Buffer + m_BufferLen, it->GetNextIdentHash (), 32);
m_BufferLen += 32; // gateway id
htobe32buf (m_Buffer + m_BufferLen, it->GetNextTunnelID ());
m_BufferLen += 4; // tunnel id
2015-03-26 14:30:29 +00:00
uint64_t ts = it->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD; // 1 minute before expiration
2014-07-29 17:44:54 +00:00
ts *= 1000; // in milliseconds
2015-11-03 14:15:49 +00:00
ts += rand () % 6; // + random milliseconds 0-5
2015-03-23 16:55:42 +00:00
htobe64buf (m_Buffer + m_BufferLen, ts);
m_BufferLen += 8; // end date
2014-12-31 14:14:53 +00:00
}
2014-07-29 17:44:54 +00:00
// signature
2014-12-16 02:24:01 +00:00
localDestination->Sign (m_Buffer, m_BufferLen, m_Buffer + m_BufferLen);
2015-11-03 14:15:49 +00:00
m_BufferLen += localDestination->GetIdentity ()->GetSignatureLen ();
2015-12-18 14:08:23 +00:00
LogPrint (eLogDebug, "LeaseSet: Local LeaseSet of ", tunnels.size (), " leases created");
2014-07-29 17:44:54 +00:00
ReadFromBuffer ();
2014-07-22 00:14:11 +00:00
}
2015-04-08 13:39:02 +00:00
void LeaseSet::Update (const uint8_t * buf, size_t len)
2014-07-22 00:14:11 +00:00
{
m_Leases.clear ();
2015-04-08 13:39:02 +00:00
if (len > m_BufferLen)
{
auto oldBuffer = m_Buffer;
m_Buffer = new uint8_t[len];
delete[] oldBuffer;
}
2014-07-29 17:44:54 +00:00
memcpy (m_Buffer, buf, len);
m_BufferLen = len;
2015-11-03 14:15:49 +00:00
ReadFromBuffer (false);
2014-07-22 00:14:11 +00:00
}
2015-11-03 14:15:49 +00:00
void LeaseSet::ReadFromBuffer (bool readIdentity)
2014-07-22 00:14:11 +00:00
{
2015-11-03 14:15:49 +00:00
if (readIdentity || !m_Identity)
m_Identity = std::make_shared<IdentityEx>(m_Buffer, m_BufferLen);
size_t size = m_Identity->GetFullLen ();
2016-02-02 16:55:38 +00:00
if (size > m_BufferLen)
{
LogPrint (eLogError, "LeaseSet: identity length ", size, " exceeds buffer size ", m_BufferLen);
m_IsValid = false;
return;
}
memcpy (m_EncryptionKey, m_Buffer + size, 256);
size += 256; // encryption key
2015-11-03 14:15:49 +00:00
size += m_Identity->GetSigningPublicKeyLen (); // unused signing key
uint8_t num = m_Buffer[size];
size++; // num
2015-12-18 14:08:23 +00:00
LogPrint (eLogDebug, "LeaseSet: read num=", (int)num);
2016-02-02 16:55:38 +00:00
if (!num || num > MAX_NUM_LEASES)
{
LogPrint (eLogError, "LeaseSet: incorrect number of leases", (int)num);
m_IsValid = false;
return;
}
2013-11-24 23:10:27 +00:00
// process leases
const uint8_t * leases = m_Buffer + size;
for (int i = 0; i < num; i++)
2013-11-24 23:10:27 +00:00
{
2014-12-31 14:14:53 +00:00
Lease lease;
2015-03-23 16:55:42 +00:00
lease.tunnelGateway = leases;
leases += 32; // gateway
lease.tunnelID = bufbe32toh (leases);
leases += 4; // tunnel ID
lease.endDate = bufbe64toh (leases);
leases += 8; // end date
2014-01-09 03:47:22 +00:00
m_Leases.push_back (lease);
// check if lease's gateway is in our netDb
if (!netdb.FindRouter (lease.tunnelGateway))
{
// if not found request it
2015-12-18 14:08:23 +00:00
LogPrint (eLogInfo, "LeaseSet: Lease's tunnel gateway not found, requesting");
netdb.RequestDestination (lease.tunnelGateway);
}
}
2014-01-09 03:47:22 +00:00
// verify
2015-11-03 14:15:49 +00:00
if (!m_Identity->Verify (m_Buffer, leases - m_Buffer, leases))
2015-04-08 14:34:16 +00:00
{
2015-12-18 14:08:23 +00:00
LogPrint (eLogWarning, "LeaseSet: verification failed");
2015-04-08 14:34:16 +00:00
m_IsValid = false;
}
2014-07-22 00:14:11 +00:00
}
2015-03-26 14:30:29 +00:00
const std::vector<Lease> LeaseSet::GetNonExpiredLeases (bool withThreshold) const
2014-01-15 01:57:33 +00:00
{
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
2014-03-23 13:25:16 +00:00
std::vector<Lease> leases;
2014-01-15 01:57:33 +00:00
for (auto& it: m_Leases)
2015-03-26 14:30:29 +00:00
{
auto endDate = it.endDate;
if (!withThreshold)
endDate -= i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD*1000;
if (ts < endDate)
2014-03-23 13:25:16 +00:00
leases.push_back (it);
2015-03-26 14:30:29 +00:00
}
2014-01-15 01:57:33 +00:00
return leases;
}
bool LeaseSet::HasExpiredLeases () const
{
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
for (auto& it: m_Leases)
if (ts >= it.endDate) return true;
return false;
}
bool LeaseSet::HasNonExpiredLeases () const
{
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
for (auto& it: m_Leases)
if (ts < it.endDate) return true;
return false;
}
2013-11-24 23:10:27 +00:00
}
}