mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-27 11:17:49 +02:00
create LeaseSet from local tunnel pool
This commit is contained in:
parent
4236299879
commit
ee2297c851
8 changed files with 72 additions and 25 deletions
58
LeaseSet.cpp
58
LeaseSet.cpp
|
@ -4,6 +4,7 @@
|
|||
#include "Log.h"
|
||||
#include "Timestamp.h"
|
||||
#include "NetDb.h"
|
||||
#include "TunnelPool.h"
|
||||
#include "LeaseSet.h"
|
||||
|
||||
namespace i2p
|
||||
|
@ -13,35 +14,60 @@ namespace data
|
|||
|
||||
LeaseSet::LeaseSet (const uint8_t * buf, int len)
|
||||
{
|
||||
ReadFromBuffer (buf, len);
|
||||
memcpy (m_Buffer, buf, len);
|
||||
m_BufferLen = len;
|
||||
ReadFromBuffer ();
|
||||
}
|
||||
|
||||
LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool)
|
||||
{
|
||||
m_BufferLen = 0;
|
||||
// header
|
||||
const i2p::data::LocalDestination& localDestination = pool.GetLocalDestination ();
|
||||
LeaseSetHeader * header = (LeaseSetHeader *)m_Buffer;
|
||||
header->destination = localDestination.GetIdentity ();
|
||||
memcpy (header->encryptionKey, localDestination.GetEncryptionPublicKey (), 256);
|
||||
memset (header->signingKey, 0, 128);
|
||||
auto tunnels = pool.GetInboundTunnels (5); // 5 tunnels maximum
|
||||
header->num = tunnels.size (); // num leases
|
||||
m_BufferLen += sizeof (LeaseSetHeader);
|
||||
// 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);
|
||||
m_BufferLen += 40;
|
||||
LogPrint ("Local LeaseSet of ", tunnels.size (), " leases created");
|
||||
|
||||
ReadFromBuffer ();
|
||||
}
|
||||
|
||||
void LeaseSet::Update (const uint8_t * buf, int len)
|
||||
{
|
||||
m_Leases.clear ();
|
||||
ReadFromBuffer (buf, len);
|
||||
memcpy (m_Buffer, buf, len);
|
||||
m_BufferLen = len;
|
||||
ReadFromBuffer ();
|
||||
}
|
||||
|
||||
void LeaseSet::ReadFromBuffer (const uint8_t * buf, int len)
|
||||
void LeaseSet::ReadFromBuffer ()
|
||||
{
|
||||
#pragma pack(1)
|
||||
struct H
|
||||
{
|
||||
Identity destination;
|
||||
uint8_t encryptionKey[256];
|
||||
uint8_t signingKey[128];
|
||||
uint8_t num;
|
||||
};
|
||||
#pragma pack ()
|
||||
|
||||
const H * header = (const H *)buf;
|
||||
const LeaseSetHeader * header = (const LeaseSetHeader *)m_Buffer;
|
||||
m_Identity = header->destination;
|
||||
m_IdentHash = m_Identity.Hash();
|
||||
memcpy (m_EncryptionKey, header->encryptionKey, 256);
|
||||
LogPrint ("LeaseSet num=", (int)header->num);
|
||||
|
||||
// process leases
|
||||
const uint8_t * leases = buf + sizeof (H);
|
||||
const uint8_t * leases = m_Buffer + sizeof (LeaseSetHeader);
|
||||
for (int i = 0; i < header->num; i++)
|
||||
{
|
||||
Lease lease = *(Lease *)leases;
|
||||
|
@ -64,7 +90,7 @@ namespace data
|
|||
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))
|
||||
if (!verifier.VerifyMessage (m_Buffer, leases - m_Buffer, leases, 40))
|
||||
LogPrint ("LeaseSet verification failed");
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue