i2pd/LeaseSet.cpp

250 lines
7.1 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"
2016-05-25 18:17:34 +00:00
#include "Tunnel.h"
2013-11-24 23:10:27 +00:00
#include "LeaseSet.h"
namespace i2p
{
namespace data
{
2013-12-20 03:05:45 +00:00
2016-02-08 00:45:06 +00:00
LeaseSet::LeaseSet (const uint8_t * buf, size_t len, bool storeLeases):
m_IsValid (true), m_StoreLeases (storeLeases), m_ExpirationTime (0)
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 ();
}
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
{
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
}
2016-02-08 00:45:06 +00:00
void LeaseSet::PopulateLeases ()
{
m_StoreLeases = true;
ReadFromBuffer (false);
}
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
2016-02-09 20:27:23 +00:00
// reset existing leases
if (m_StoreLeases)
for (auto it: m_Leases)
it->isUpdated = false;
else
m_Leases.clear ();
// process leases
2016-02-08 00:45:06 +00:00
m_ExpirationTime = 0;
2016-02-10 03:42:01 +00:00
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
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
2016-02-08 00:45:06 +00:00
lease.endDate = bufbe64toh (leases);
2015-03-23 16:55:42 +00:00
leases += 8; // end date
2016-02-12 03:18:24 +00:00
if (ts < lease.endDate + LEASE_ENDDATE_THRESHOLD)
2016-02-08 00:45:06 +00:00
{
2016-02-10 03:42:01 +00:00
if (lease.endDate > m_ExpirationTime)
m_ExpirationTime = lease.endDate;
if (m_StoreLeases)
{
auto ret = m_Leases.insert (std::make_shared<Lease>(lease));
if (!ret.second) *(*ret.first) = lease; // update existing
(*ret.first)->isUpdated = true;
// check if lease's gateway is in our netDb
if (!netdb.FindRouter (lease.tunnelGateway))
{
// if not found request it
LogPrint (eLogInfo, "LeaseSet: Lease's tunnel gateway not found, requesting");
netdb.RequestDestination (lease.tunnelGateway);
}
}
}
else
LogPrint (eLogWarning, "LeaseSet: Lease is expired already ");
}
2016-02-14 23:30:07 +00:00
if (!m_ExpirationTime)
2016-02-10 03:42:01 +00:00
{
LogPrint (eLogWarning, "LeaseSet: all leases are expired. Dropped");
m_IsValid = false;
return;
}
2016-02-14 23:30:07 +00:00
m_ExpirationTime += LEASE_ENDDATE_THRESHOLD;
2016-02-09 20:27:23 +00:00
// delete old leases
if (m_StoreLeases)
{
for (auto it = m_Leases.begin (); it != m_Leases.end ();)
{
if (!(*it)->isUpdated)
{
(*it)->endDate = 0; // somebody might still hold it
m_Leases.erase (it++);
}
else
it++;
}
}
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
}
2016-02-17 03:57:38 +00:00
uint64_t LeaseSet::ExtractTimestamp (const uint8_t * buf, size_t len) const
{
if (!m_Identity) return 0;
size_t size = m_Identity->GetFullLen ();
if (size > len) return 0;
size += 256; // encryption key
size += m_Identity->GetSigningPublicKeyLen (); // unused signing key
if (size > len) return 0;
uint8_t num = buf[size];
size++; // num
2016-05-25 18:17:34 +00:00
if (size + num*LEASE_SIZE > len) return 0;
2016-02-17 03:57:38 +00:00
uint64_t timestamp= 0 ;
for (int i = 0; i < num; i++)
{
size += 36; // gateway (32) + tunnelId(4)
auto endDate = bufbe64toh (buf + size);
size += 8; // end date
if (!timestamp || endDate < timestamp)
timestamp = endDate;
}
return timestamp;
}
bool LeaseSet::IsNewer (const uint8_t * buf, size_t len) const
{
return ExtractTimestamp (buf, len) > ExtractTimestamp (m_Buffer, m_BufferLen);
2016-02-17 03:57:38 +00:00
}
2016-02-11 03:51:08 +00:00
const std::vector<std::shared_ptr<const Lease> > LeaseSet::GetNonExpiredLeases (bool withThreshold) const
2014-01-15 01:57:33 +00:00
{
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
2016-02-11 03:51:08 +00:00
std::vector<std::shared_ptr<const Lease> > leases;
2016-02-09 20:27:23 +00:00
for (auto it: m_Leases)
2015-03-26 14:30:29 +00:00
{
2016-02-09 15:46:27 +00:00
auto endDate = it->endDate;
2016-02-14 23:30:07 +00:00
if (withThreshold)
endDate += LEASE_ENDDATE_THRESHOLD;
else
endDate -= LEASE_ENDDATE_THRESHOLD;
2015-03-26 14:30:29 +00:00
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
2016-02-08 00:45:06 +00:00
{
2014-01-15 01:57:33 +00:00
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
2016-02-09 20:27:23 +00:00
for (auto it: m_Leases)
2016-02-09 15:46:27 +00:00
if (ts >= it->endDate) return true;
2014-01-15 01:57:33 +00:00
return false;
2016-02-08 00:45:06 +00:00
}
2014-01-15 01:57:33 +00:00
2016-02-08 00:45:06 +00:00
bool LeaseSet::IsExpired () const
2014-01-15 01:57:33 +00:00
{
2016-06-30 21:21:18 +00:00
if (m_StoreLeases && IsEmpty ()) return true;
2014-01-15 01:57:33 +00:00
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
2016-02-08 00:45:06 +00:00
return ts > m_ExpirationTime;
2016-05-25 18:17:34 +00:00
}
LocalLeaseSet::LocalLeaseSet (std::shared_ptr<const IdentityEx> identity, const uint8_t * encryptionPublicKey, std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels):
2016-05-25 19:10:28 +00:00
m_ExpirationTime (0), m_Identity (identity)
2016-05-25 18:17:34 +00:00
{
int num = tunnels.size ();
if (num > MAX_NUM_LEASES) num = MAX_NUM_LEASES;
// identity
2016-05-25 21:41:24 +00:00
auto signingKeyLen = m_Identity->GetSigningPublicKeyLen ();
2016-05-25 22:47:16 +00:00
m_BufferLen = m_Identity->GetFullLen () + 256 + signingKeyLen + 1 + num*LEASE_SIZE + m_Identity->GetSignatureLen ();
2016-05-25 18:17:34 +00:00
m_Buffer = new uint8_t[m_BufferLen];
auto offset = m_Identity->ToBuffer (m_Buffer, m_BufferLen);
memcpy (m_Buffer + offset, encryptionPublicKey, 256);
offset += 256;
memset (m_Buffer + offset, 0, signingKeyLen);
offset += signingKeyLen;
// num leases
m_Buffer[offset] = num;
offset++;
// leases
2016-05-29 20:35:57 +00:00
m_Leases = m_Buffer + offset;
2016-05-25 18:17:34 +00:00
auto currentTime = i2p::util::GetMillisecondsSinceEpoch ();
for (int i = 0; i < num; i++)
{
memcpy (m_Buffer + offset, tunnels[i]->GetNextIdentHash (), 32);
offset += 32; // gateway id
htobe32buf (m_Buffer + offset, tunnels[i]->GetNextTunnelID ());
offset += 4; // tunnel id
uint64_t ts = tunnels[i]->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD; // 1 minute before expiration
ts *= 1000; // in milliseconds
2016-05-25 19:10:28 +00:00
if (ts > m_ExpirationTime) m_ExpirationTime = ts;
2016-05-25 18:17:34 +00:00
// make sure leaseset is newer than previous, but adding some time to expiration date
ts += (currentTime - tunnels[i]->GetCreationTime ()*1000LL)*2/i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT; // up to 2 secs
htobe64buf (m_Buffer + offset, ts);
offset += 8; // end date
}
// we don't sign it yet. must be signed later on
2016-05-25 19:10:28 +00:00
}
2016-05-25 18:17:34 +00:00
2016-05-30 16:56:42 +00:00
LocalLeaseSet::LocalLeaseSet (std::shared_ptr<const IdentityEx> identity, const uint8_t * buf, size_t len):
m_ExpirationTime (0), m_Identity (identity)
{
m_BufferLen = len;
m_Buffer = new uint8_t[m_BufferLen];
memcpy (m_Buffer, buf, len);
}
2016-05-25 19:10:28 +00:00
bool LocalLeaseSet::IsExpired () const
2016-05-25 18:17:34 +00:00
{
2016-05-25 19:10:28 +00:00
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
return ts > m_ExpirationTime;
}
2013-11-24 23:10:27 +00:00
}
}