mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
263 lines
7.6 KiB
263 lines
7.6 KiB
10 years ago
|
#include <string.h>
|
||
11 years ago
|
#include "I2PEndian.h"
|
||
9 years ago
|
#include "Crypto.h"
|
||
11 years ago
|
#include "Log.h"
|
||
11 years ago
|
#include "Timestamp.h"
|
||
11 years ago
|
#include "NetDb.h"
|
||
9 years ago
|
#include "Tunnel.h"
|
||
11 years ago
|
#include "LeaseSet.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace data
|
||
|
{
|
||
11 years ago
|
|
||
9 years ago
|
LeaseSet::LeaseSet (const uint8_t * buf, size_t len, bool storeLeases):
|
||
|
m_IsValid (true), m_StoreLeases (storeLeases), m_ExpirationTime (0)
|
||
11 years ago
|
{
|
||
10 years ago
|
m_Buffer = new uint8_t[len];
|
||
10 years ago
|
memcpy (m_Buffer, buf, len);
|
||
|
m_BufferLen = len;
|
||
|
ReadFromBuffer ();
|
||
|
}
|
||
|
|
||
10 years ago
|
void LeaseSet::Update (const uint8_t * buf, size_t len)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (len > m_BufferLen)
|
||
|
{
|
||
|
auto oldBuffer = m_Buffer;
|
||
|
m_Buffer = new uint8_t[len];
|
||
|
delete[] oldBuffer;
|
||
|
}
|
||
10 years ago
|
memcpy (m_Buffer, buf, len);
|
||
|
m_BufferLen = len;
|
||
9 years ago
|
ReadFromBuffer (false);
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
void LeaseSet::PopulateLeases ()
|
||
|
{
|
||
|
m_StoreLeases = true;
|
||
|
ReadFromBuffer (false);
|
||
|
}
|
||
|
|
||
9 years ago
|
void LeaseSet::ReadFromBuffer (bool readIdentity)
|
||
10 years ago
|
{
|
||
9 years ago
|
if (readIdentity || !m_Identity)
|
||
|
m_Identity = std::make_shared<IdentityEx>(m_Buffer, m_BufferLen);
|
||
|
size_t size = m_Identity->GetFullLen ();
|
||
9 years ago
|
if (size > m_BufferLen)
|
||
|
{
|
||
|
LogPrint (eLogError, "LeaseSet: identity length ", size, " exceeds buffer size ", m_BufferLen);
|
||
|
m_IsValid = false;
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
memcpy (m_EncryptionKey, m_Buffer + size, 256);
|
||
|
size += 256; // encryption key
|
||
9 years ago
|
size += m_Identity->GetSigningPublicKeyLen (); // unused signing key
|
||
10 years ago
|
uint8_t num = m_Buffer[size];
|
||
|
size++; // num
|
||
9 years ago
|
LogPrint (eLogDebug, "LeaseSet: read num=", (int)num);
|
||
9 years ago
|
if (!num || num > MAX_NUM_LEASES)
|
||
|
{
|
||
|
LogPrint (eLogError, "LeaseSet: incorrect number of leases", (int)num);
|
||
|
m_IsValid = false;
|
||
|
return;
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
// reset existing leases
|
||
9 years ago
|
if (m_StoreLeases)
|
||
8 years ago
|
for (auto& it: m_Leases)
|
||
|
it->isUpdated = false;
|
||
9 years ago
|
else
|
||
|
m_Leases.clear ();
|
||
|
|
||
11 years ago
|
// process leases
|
||
9 years ago
|
m_ExpirationTime = 0;
|
||
9 years ago
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
||
10 years ago
|
const uint8_t * leases = m_Buffer + size;
|
||
|
for (int i = 0; i < num; i++)
|
||
11 years ago
|
{
|
||
10 years ago
|
Lease lease;
|
||
10 years ago
|
lease.tunnelGateway = leases;
|
||
|
leases += 32; // gateway
|
||
|
lease.tunnelID = bufbe32toh (leases);
|
||
|
leases += 4; // tunnel ID
|
||
9 years ago
|
lease.endDate = bufbe64toh (leases);
|
||
10 years ago
|
leases += 8; // end date
|
||
9 years ago
|
if (ts < lease.endDate + LEASE_ENDDATE_THRESHOLD)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (lease.endDate > m_ExpirationTime)
|
||
|
m_ExpirationTime = lease.endDate;
|
||
|
if (m_StoreLeases)
|
||
|
{
|
||
|
auto ret = m_Leases.insert (std::make_shared<Lease>(lease));
|
||
8 years ago
|
if (!ret.second) (*ret.first)->endDate = lease.endDate; // update existing
|
||
9 years ago
|
(*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 ");
|
||
|
}
|
||
9 years ago
|
if (!m_ExpirationTime)
|
||
9 years ago
|
{
|
||
|
LogPrint (eLogWarning, "LeaseSet: all leases are expired. Dropped");
|
||
|
m_IsValid = false;
|
||
|
return;
|
||
11 years ago
|
}
|
||
9 years ago
|
m_ExpirationTime += LEASE_ENDDATE_THRESHOLD;
|
||
9 years ago
|
// 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
|
||
8 years ago
|
++it;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
// verify
|
||
9 years ago
|
if (!m_Identity->Verify (m_Buffer, leases - m_Buffer, leases))
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "LeaseSet: verification failed");
|
||
10 years ago
|
m_IsValid = false;
|
||
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
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
|
||
9 years ago
|
if (size + num*LEASE_SIZE > len) return 0;
|
||
9 years ago
|
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
|
||
|
{
|
||
9 years ago
|
return ExtractTimestamp (buf, len) > ExtractTimestamp (m_Buffer, m_BufferLen);
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
bool LeaseSet::ExpiresSoon(const uint64_t dlt, const uint64_t fudge) const
|
||
8 years ago
|
{
|
||
|
auto now = i2p::util::GetMillisecondsSinceEpoch ();
|
||
8 years ago
|
if (fudge) now += rand() % fudge;
|
||
8 years ago
|
if (now >= m_ExpirationTime) return true;
|
||
|
return m_ExpirationTime - now <= dlt;
|
||
|
}
|
||
8 years ago
|
|
||
|
const std::vector<std::shared_ptr<const Lease> > LeaseSet::GetNonExpiredLeases (bool withThreshold) const
|
||
|
{
|
||
|
return GetNonExpiredLeasesExcluding( [] (const Lease & l) -> bool { return false; }, withThreshold);
|
||
|
}
|
||
|
|
||
|
const std::vector<std::shared_ptr<const Lease> > LeaseSet::GetNonExpiredLeasesExcluding (LeaseInspectFunc exclude, bool withThreshold) const
|
||
11 years ago
|
{
|
||
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
||
9 years ago
|
std::vector<std::shared_ptr<const Lease> > leases;
|
||
8 years ago
|
for (const auto& it: m_Leases)
|
||
10 years ago
|
{
|
||
9 years ago
|
auto endDate = it->endDate;
|
||
9 years ago
|
if (withThreshold)
|
||
|
endDate += LEASE_ENDDATE_THRESHOLD;
|
||
|
else
|
||
|
endDate -= LEASE_ENDDATE_THRESHOLD;
|
||
8 years ago
|
if (ts < endDate && !exclude(*it))
|
||
11 years ago
|
leases.push_back (it);
|
||
10 years ago
|
}
|
||
11 years ago
|
return leases;
|
||
|
}
|
||
|
|
||
|
bool LeaseSet::HasExpiredLeases () const
|
||
9 years ago
|
{
|
||
11 years ago
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
||
8 years ago
|
for (const auto& it: m_Leases)
|
||
9 years ago
|
if (ts >= it->endDate) return true;
|
||
11 years ago
|
return false;
|
||
9 years ago
|
}
|
||
11 years ago
|
|
||
9 years ago
|
bool LeaseSet::IsExpired () const
|
||
11 years ago
|
{
|
||
8 years ago
|
if (m_StoreLeases && IsEmpty ()) return true;
|
||
11 years ago
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
||
9 years ago
|
return ts > m_ExpirationTime;
|
||
9 years ago
|
}
|
||
|
|
||
|
LocalLeaseSet::LocalLeaseSet (std::shared_ptr<const IdentityEx> identity, const uint8_t * encryptionPublicKey, std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels):
|
||
9 years ago
|
m_ExpirationTime (0), m_Identity (identity)
|
||
9 years ago
|
{
|
||
|
int num = tunnels.size ();
|
||
|
if (num > MAX_NUM_LEASES) num = MAX_NUM_LEASES;
|
||
|
// identity
|
||
9 years ago
|
auto signingKeyLen = m_Identity->GetSigningPublicKeyLen ();
|
||
9 years ago
|
m_BufferLen = m_Identity->GetFullLen () + 256 + signingKeyLen + 1 + num*LEASE_SIZE + m_Identity->GetSignatureLen ();
|
||
9 years ago
|
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
|
||
9 years ago
|
m_Leases = m_Buffer + offset;
|
||
9 years ago
|
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
|
||
9 years ago
|
if (ts > m_ExpirationTime) m_ExpirationTime = ts;
|
||
9 years ago
|
// 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
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
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);
|
||
|
}
|
||
|
|
||
9 years ago
|
bool LocalLeaseSet::IsExpired () const
|
||
9 years ago
|
{
|
||
9 years ago
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
||
|
return ts > m_ExpirationTime;
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|