i2pd/LeaseSet.cpp

123 lines
3.4 KiB
C++
Raw Normal View History

2014-01-15 01:57:33 +00:00
#include "I2PEndian.h"
2014-01-09 03:47:22 +00:00
#include <cryptopp/dsa.h>
#include "CryptoConst.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
LeaseSet::LeaseSet (const uint8_t * buf, int len)
2013-11-24 23:10:27 +00:00
{
2014-07-29 17:44:54 +00:00
memcpy (m_Buffer, buf, len);
m_BufferLen = len;
ReadFromBuffer ();
}
LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool)
{
2014-07-29 17:44:54 +00:00
// header
const i2p::data::LocalDestination& localDestination = pool.GetLocalDestination ();
m_BufferLen = localDestination.GetIdentity ().ToBuffer (m_Buffer, MAX_LS_BUFFER_SIZE);
memcpy (m_Buffer + m_BufferLen, localDestination.GetEncryptionPublicKey (), 256);
m_BufferLen += 256;
auto signingKeyLen = localDestination.GetIdentity ().GetSigningPublicKeyLen ();
memset (m_Buffer + m_BufferLen, 0, signingKeyLen);
m_BufferLen += signingKeyLen;
2014-07-29 17:44:54 +00:00
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)
{
Lease * lease = (Lease *)(m_Buffer + m_BufferLen);
memcpy (lease->tunnelGateway, it->GetNextIdentHash (), 32);
lease->tunnelID = htobe32 (it->GetNextTunnelID ());
uint64_t ts = it->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - 60; // 1 minute before expiration
ts *= 1000; // in milliseconds
lease->endDate = htobe64 (ts);
m_BufferLen += sizeof (Lease);
}
// signature
localDestination.Sign (m_Buffer, m_BufferLen, m_Buffer + m_BufferLen);
2014-08-23 12:41:06 +00:00
m_BufferLen += localDestination.GetIdentity ().GetSignatureLen ();
2014-07-29 17:44:54 +00:00
LogPrint ("Local LeaseSet of ", tunnels.size (), " leases created");
ReadFromBuffer ();
2014-07-22 00:14:11 +00:00
}
void LeaseSet::Update (const uint8_t * buf, int len)
{
m_Leases.clear ();
2014-07-29 17:44:54 +00:00
memcpy (m_Buffer, buf, len);
m_BufferLen = len;
ReadFromBuffer ();
2014-07-22 00:14:11 +00:00
}
2014-07-29 17:44:54 +00:00
void LeaseSet::ReadFromBuffer ()
2014-07-22 00:14:11 +00:00
{
size_t size = m_Identity.FromBuffer (m_Buffer, m_BufferLen);
memcpy (m_EncryptionKey, m_Buffer + size, 256);
size += 256; // encryption key
size += m_Identity.GetSigningPublicKeyLen (); // unused signing key
uint8_t num = m_Buffer[size];
size++; // num
LogPrint ("LeaseSet num=", (int)num);
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-01-09 03:47:22 +00:00
Lease lease = *(Lease *)leases;
lease.tunnelID = be32toh (lease.tunnelID);
2014-01-15 03:35:00 +00:00
lease.endDate = be64toh (lease.endDate);
2014-01-09 03:47:22 +00:00
m_Leases.push_back (lease);
leases += sizeof (Lease);
// check if lease's gateway is in our netDb
if (!netdb.FindRouter (lease.tunnelGateway))
{
// if not found request it
LogPrint ("Lease's tunnel gateway not found. Requested");
netdb.RequestDestination (lease.tunnelGateway);
}
}
2014-01-09 03:47:22 +00:00
// verify
if (!m_Identity.Verify (m_Buffer, leases - m_Buffer, leases))
2014-01-09 03:47:22 +00:00
LogPrint ("LeaseSet verification failed");
2014-07-22 00:14:11 +00:00
}
2014-03-23 13:25:16 +00:00
const std::vector<Lease> LeaseSet::GetNonExpiredLeases () 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)
if (ts < it.endDate)
2014-03-23 13:25:16 +00:00
leases.push_back (it);
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
}
}