i2pd/LeaseSet.cpp

87 lines
2.1 KiB
C++
Raw Normal View History

2014-01-15 05:57:33 +04:00
#include "I2PEndian.h"
2014-01-09 07:47:22 +04:00
#include <cryptopp/dsa.h>
#include "CryptoConst.h"
2013-11-25 03:10:27 +04:00
#include "Log.h"
2014-01-15 05:57:33 +04:00
#include "Timestamp.h"
#include "NetDb.h"
2013-11-25 03:10:27 +04:00
#include "LeaseSet.h"
namespace i2p
{
namespace data
{
2013-12-20 07:05:45 +04:00
2013-11-25 03:10:27 +04:00
LeaseSet::LeaseSet (const uint8_t * buf, int len)
{
#pragma pack(1)
struct H
{
Identity destination;
2013-11-25 03:10:27 +04:00
uint8_t encryptionKey[256];
uint8_t signingKey[128];
uint8_t num;
};
#pragma pack ()
const H * header = (const H *)buf;
2013-12-20 07:05:45 +04:00
m_Identity = header->destination;
m_IdentHash = m_Identity.Hash();
2013-11-25 03:10:27 +04:00
memcpy (m_EncryptionKey, header->encryptionKey, 256);
LogPrint ("LeaseSet num=", (int)header->num);
// process leases
2014-01-09 07:47:22 +04:00
const uint8_t * leases = buf + sizeof (H);
2013-11-25 03:10:27 +04:00
for (int i = 0; i < header->num; i++)
{
2014-01-09 07:47:22 +04:00
Lease lease = *(Lease *)leases;
lease.tunnelID = be32toh (lease.tunnelID);
2014-01-15 07:35:00 +04:00
lease.endDate = be64toh (lease.endDate);
2014-01-09 07:47:22 +04: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 07:47:22 +04:00
// verify
CryptoPP::DSA::PublicKey pubKey;
pubKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
CryptoPP::Integer (m_Identity.signingKey, 128));
CryptoPP::DSA::Verifier verifier (pubKey);
if (!verifier.VerifyMessage (buf, leases - buf, leases, 40))
LogPrint ("LeaseSet verification failed");
2013-11-25 03:10:27 +04:00
}
2014-01-15 05:57:33 +04:00
2014-03-23 17:25:16 +04:00
const std::vector<Lease> LeaseSet::GetNonExpiredLeases () const
2014-01-15 05:57:33 +04:00
{
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
2014-03-23 17:25:16 +04:00
std::vector<Lease> leases;
2014-01-15 05:57:33 +04:00
for (auto& it: m_Leases)
if (ts < it.endDate)
2014-03-23 17:25:16 +04:00
leases.push_back (it);
2014-01-15 05:57:33 +04: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-25 03:10:27 +04:00
}
}