|
|
@ -4,6 +4,7 @@ |
|
|
|
#include "Log.h" |
|
|
|
#include "Log.h" |
|
|
|
#include "Timestamp.h" |
|
|
|
#include "Timestamp.h" |
|
|
|
#include "NetDb.h" |
|
|
|
#include "NetDb.h" |
|
|
|
|
|
|
|
#include "TunnelPool.h" |
|
|
|
#include "LeaseSet.h" |
|
|
|
#include "LeaseSet.h" |
|
|
|
|
|
|
|
|
|
|
|
namespace i2p |
|
|
|
namespace i2p |
|
|
@ -13,35 +14,60 @@ namespace data |
|
|
|
|
|
|
|
|
|
|
|
LeaseSet::LeaseSet (const uint8_t * buf, int len) |
|
|
|
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) |
|
|
|
void LeaseSet::Update (const uint8_t * buf, int len) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_Leases.clear (); |
|
|
|
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) |
|
|
|
const LeaseSetHeader * header = (const LeaseSetHeader *)m_Buffer; |
|
|
|
struct H |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Identity destination; |
|
|
|
|
|
|
|
uint8_t encryptionKey[256]; |
|
|
|
|
|
|
|
uint8_t signingKey[128]; |
|
|
|
|
|
|
|
uint8_t num; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
#pragma pack () |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const H * header = (const H *)buf; |
|
|
|
|
|
|
|
m_Identity = header->destination; |
|
|
|
m_Identity = header->destination; |
|
|
|
m_IdentHash = m_Identity.Hash(); |
|
|
|
m_IdentHash = m_Identity.Hash(); |
|
|
|
memcpy (m_EncryptionKey, header->encryptionKey, 256); |
|
|
|
memcpy (m_EncryptionKey, header->encryptionKey, 256); |
|
|
|
LogPrint ("LeaseSet num=", (int)header->num); |
|
|
|
LogPrint ("LeaseSet num=", (int)header->num); |
|
|
|
|
|
|
|
|
|
|
|
// process leases
|
|
|
|
// 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++) |
|
|
|
for (int i = 0; i < header->num; i++) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Lease lease = *(Lease *)leases; |
|
|
|
Lease lease = *(Lease *)leases; |
|
|
@ -64,7 +90,7 @@ namespace data |
|
|
|
pubKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, |
|
|
|
pubKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, |
|
|
|
CryptoPP::Integer (m_Identity.signingKey, 128)); |
|
|
|
CryptoPP::Integer (m_Identity.signingKey, 128)); |
|
|
|
CryptoPP::DSA::Verifier verifier (pubKey); |
|
|
|
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"); |
|
|
|
LogPrint ("LeaseSet verification failed"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|