Browse Source

create LeaseSet from local tunnel pool

pull/93/head
orignal 10 years ago
parent
commit
ee2297c851
  1. 2
      Identity.h
  2. 58
      LeaseSet.cpp
  3. 20
      LeaseSet.h
  4. 4
      RouterContext.cpp
  5. 4
      RouterContext.h
  6. 2
      Streaming.cpp
  7. 6
      Streaming.h
  8. 1
      TunnelPool.h

2
Identity.h

@ -153,9 +153,11 @@ namespace data
virtual ~LocalDestination() {}; virtual ~LocalDestination() {};
virtual const IdentHash& GetIdentHash () const = 0; virtual const IdentHash& GetIdentHash () const = 0;
virtual const Identity& GetIdentity () const = 0;
virtual const uint8_t * GetEncryptionPrivateKey () const = 0; virtual const uint8_t * GetEncryptionPrivateKey () const = 0;
virtual const uint8_t * GetEncryptionPublicKey () const = 0; virtual const uint8_t * GetEncryptionPublicKey () const = 0;
virtual void UpdateLeaseSet () = 0; // LeaseSet must be updated virtual void UpdateLeaseSet () = 0; // LeaseSet must be updated
virtual void Sign (const uint8_t * buf, int len, uint8_t * signature) const = 0;
}; };
} }
} }

58
LeaseSet.cpp

@ -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");
} }

20
LeaseSet.h

@ -8,6 +8,12 @@
namespace i2p namespace i2p
{ {
namespace tunnel
{
class TunnelPool;
}
namespace data namespace data
{ {
@ -28,14 +34,24 @@ namespace data
} }
}; };
struct LeaseSetHeader
{
Identity destination;
uint8_t encryptionKey[256];
uint8_t signingKey[128];
uint8_t num;
};
#pragma pack() #pragma pack()
const int MAX_LS_BUFFER_SIZE = 2048;
class LeaseSet: public RoutingDestination class LeaseSet: public RoutingDestination
{ {
public: public:
LeaseSet (const uint8_t * buf, int len); LeaseSet (const uint8_t * buf, int len);
LeaseSet (const LeaseSet& ) = default; LeaseSet (const LeaseSet& ) = default;
LeaseSet (const i2p::tunnel::TunnelPool& pool);
LeaseSet& operator=(const LeaseSet& ) = default; LeaseSet& operator=(const LeaseSet& ) = default;
void Update (const uint8_t * buf, int len); void Update (const uint8_t * buf, int len);
@ -51,7 +67,7 @@ namespace data
private: private:
void ReadFromBuffer (const uint8_t * buf, int len); void ReadFromBuffer ();
private: private:
@ -59,6 +75,8 @@ namespace data
Identity m_Identity; Identity m_Identity;
IdentHash m_IdentHash; IdentHash m_IdentHash;
uint8_t m_EncryptionKey[256]; uint8_t m_EncryptionKey[256];
uint8_t m_Buffer[MAX_LS_BUFFER_SIZE];
size_t m_BufferLen;
}; };
} }
} }

4
RouterContext.cpp

@ -64,10 +64,10 @@ namespace i2p
m_RouterInfo.CreateBuffer (); m_RouterInfo.CreateBuffer ();
} }
void RouterContext::Sign (uint8_t * buf, int len, uint8_t * signature) void RouterContext::Sign (const uint8_t * buf, int len, uint8_t * signature) const
{ {
CryptoPP::DSA::Signer signer (m_SigningPrivateKey); CryptoPP::DSA::Signer signer (m_SigningPrivateKey);
signer.SignMessage (m_Rnd, buf, len, signature); signer.SignMessage (i2p::context.GetRandomNumberGenerator (), buf, len, signature);
} }
bool RouterContext::Load () bool RouterContext::Load ()

4
RouterContext.h

@ -23,8 +23,6 @@ namespace i2p
const uint8_t * GetSigningPrivateKey () const { return m_Keys.signingPrivateKey; }; const uint8_t * GetSigningPrivateKey () const { return m_Keys.signingPrivateKey; };
const i2p::data::Identity& GetRouterIdentity () const { return m_RouterInfo.GetRouterIdentity (); }; const i2p::data::Identity& GetRouterIdentity () const { return m_RouterInfo.GetRouterIdentity (); };
CryptoPP::RandomNumberGenerator& GetRandomNumberGenerator () { return m_Rnd; }; CryptoPP::RandomNumberGenerator& GetRandomNumberGenerator () { return m_Rnd; };
void Sign (uint8_t * buf, int len, uint8_t * signature);
void OverrideNTCPAddress (const char * host, int port); // temporary void OverrideNTCPAddress (const char * host, int port); // temporary
void UpdateAddress (const char * host); // called from SSU void UpdateAddress (const char * host); // called from SSU
@ -32,8 +30,10 @@ namespace i2p
// implements LocalDestination // implements LocalDestination
void UpdateLeaseSet () {}; void UpdateLeaseSet () {};
const i2p::data::IdentHash& GetIdentHash () const { return m_RouterInfo.GetIdentHash (); }; const i2p::data::IdentHash& GetIdentHash () const { return m_RouterInfo.GetIdentHash (); };
const i2p::data::Identity& GetIdentity () const { return GetRouterIdentity (); };
const uint8_t * GetEncryptionPrivateKey () const { return GetPrivateKey (); }; const uint8_t * GetEncryptionPrivateKey () const { return GetPrivateKey (); };
const uint8_t * GetEncryptionPublicKey () const { return m_Keys.publicKey; }; const uint8_t * GetEncryptionPublicKey () const { return m_Keys.publicKey; };
void Sign (const uint8_t * buf, int len, uint8_t * signature) const;
private: private:

2
Streaming.cpp

@ -463,7 +463,7 @@ namespace stream
return m; return m;
} }
void StreamingDestination::Sign (uint8_t * buf, int len, uint8_t * signature) const void StreamingDestination::Sign (const uint8_t * buf, int len, uint8_t * signature) const
{ {
CryptoPP::DSA::Signer signer (m_SigningPrivateKey); CryptoPP::DSA::Signer signer (m_SigningPrivateKey);
signer.SignMessage (i2p::context.GetRandomNumberGenerator (), buf, len, signature); signer.SignMessage (i2p::context.GetRandomNumberGenerator (), buf, len, signature);

6
Streaming.h

@ -124,10 +124,8 @@ namespace stream
~StreamingDestination (); ~StreamingDestination ();
const i2p::data::PrivateKeys& GetKeys () const { return m_Keys; }; const i2p::data::PrivateKeys& GetKeys () const { return m_Keys; };
const i2p::data::Identity& GetIdentity () const { return m_Keys.pub; };
const I2NPMessage * GetLeaseSet (); const I2NPMessage * GetLeaseSet ();
i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; }; i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; };
void Sign (uint8_t * buf, int len, uint8_t * signature) const;
Stream * CreateNewStream (boost::asio::io_service& service, const i2p::data::LeaseSet& remote); Stream * CreateNewStream (boost::asio::io_service& service, const i2p::data::LeaseSet& remote);
void DeleteStream (Stream * stream); void DeleteStream (Stream * stream);
@ -136,8 +134,10 @@ namespace stream
// implements LocalDestination // implements LocalDestination
void UpdateLeaseSet (); void UpdateLeaseSet ();
const i2p::data::IdentHash& GetIdentHash () const { return m_IdentHash; }; const i2p::data::IdentHash& GetIdentHash () const { return m_IdentHash; };
const i2p::data::Identity& GetIdentity () const { return m_Keys.pub; };
const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; }; const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; };
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionPublicKey; }; const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionPublicKey; };
void Sign (const uint8_t * buf, int len, uint8_t * signature) const;
private: private:

1
TunnelPool.h

@ -28,6 +28,7 @@ namespace tunnel
const uint8_t * GetEncryptionPrivateKey () const { return m_LocalDestination.GetEncryptionPrivateKey (); }; const uint8_t * GetEncryptionPrivateKey () const { return m_LocalDestination.GetEncryptionPrivateKey (); };
const uint8_t * GetEncryptionPublicKey () const { return m_LocalDestination.GetEncryptionPublicKey (); }; const uint8_t * GetEncryptionPublicKey () const { return m_LocalDestination.GetEncryptionPublicKey (); };
const i2p::data::LocalDestination& GetLocalDestination () const { return m_LocalDestination; };
bool IsExploratory () const { return m_LocalDestination.GetIdentHash () == i2p::context.GetIdentHash (); }; bool IsExploratory () const { return m_LocalDestination.GetIdentHash () == i2p::context.GetIdentHash (); };
void CreateTunnels (); void CreateTunnels ();

Loading…
Cancel
Save