mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
4.5 KiB
132 lines
4.5 KiB
11 years ago
|
#ifndef LEASE_SET_H__
|
||
|
#define LEASE_SET_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
11 years ago
|
#include <string.h>
|
||
11 years ago
|
#include <vector>
|
||
9 years ago
|
#include <set>
|
||
9 years ago
|
#include <memory>
|
||
11 years ago
|
#include "Identity.h"
|
||
8 years ago
|
#include "Timestamp.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
10 years ago
|
|
||
|
namespace tunnel
|
||
|
{
|
||
9 years ago
|
class InboundTunnel;
|
||
10 years ago
|
}
|
||
|
|
||
11 years ago
|
namespace data
|
||
11 years ago
|
{
|
||
9 years ago
|
const int LEASE_ENDDATE_THRESHOLD = 51000; // in milliseconds
|
||
11 years ago
|
struct Lease
|
||
|
{
|
||
10 years ago
|
IdentHash tunnelGateway;
|
||
11 years ago
|
uint32_t tunnelID;
|
||
9 years ago
|
uint64_t endDate; // 0 means invalid
|
||
8 years ago
|
bool isUpdated; // trasient
|
||
|
/* return true if this lease expires within t millisecond + fudge factor */
|
||
|
bool ExpiresWithin( const uint64_t t, const uint64_t fudge = 1000 ) const {
|
||
|
auto expire = i2p::util::GetMillisecondsSinceEpoch ();
|
||
|
if(fudge) expire += rand() % fudge;
|
||
8 years ago
|
if (endDate < expire) return true;
|
||
|
return (endDate - expire) < t;
|
||
8 years ago
|
}
|
||
9 years ago
|
};
|
||
|
|
||
|
struct LeaseCmp
|
||
|
{
|
||
|
bool operator() (std::shared_ptr<const Lease> l1, std::shared_ptr<const Lease> l2) const
|
||
|
{
|
||
|
if (l1->tunnelID != l2->tunnelID)
|
||
|
return l1->tunnelID < l2->tunnelID;
|
||
|
else
|
||
|
return l1->tunnelGateway < l2->tunnelGateway;
|
||
|
};
|
||
11 years ago
|
};
|
||
|
|
||
8 years ago
|
typedef std::function<bool(const Lease & l)> LeaseInspectFunc;
|
||
|
|
||
9 years ago
|
const size_t MAX_LS_BUFFER_SIZE = 3072;
|
||
|
const size_t LEASE_SIZE = 44; // 32 + 4 + 8
|
||
9 years ago
|
const uint8_t MAX_NUM_LEASES = 16;
|
||
11 years ago
|
class LeaseSet: public RoutingDestination
|
||
|
{
|
||
|
public:
|
||
|
|
||
9 years ago
|
LeaseSet (const uint8_t * buf, size_t len, bool storeLeases = true);
|
||
10 years ago
|
~LeaseSet () { delete[] m_Buffer; };
|
||
|
void Update (const uint8_t * buf, size_t len);
|
||
9 years ago
|
bool IsNewer (const uint8_t * buf, size_t len) const;
|
||
9 years ago
|
void PopulateLeases (); // from buffer
|
||
9 years ago
|
std::shared_ptr<const IdentityEx> GetIdentity () const { return m_Identity; };
|
||
10 years ago
|
|
||
10 years ago
|
const uint8_t * GetBuffer () const { return m_Buffer; };
|
||
|
size_t GetBufferLen () const { return m_BufferLen; };
|
||
10 years ago
|
bool IsValid () const { return m_IsValid; };
|
||
9 years ago
|
const std::vector<std::shared_ptr<const Lease> > GetNonExpiredLeases (bool withThreshold = true) const;
|
||
8 years ago
|
const std::vector<std::shared_ptr<const Lease> > GetNonExpiredLeasesExcluding (LeaseInspectFunc exclude, bool withThreshold = true) const;
|
||
11 years ago
|
bool HasExpiredLeases () const;
|
||
9 years ago
|
bool IsExpired () const;
|
||
9 years ago
|
bool IsEmpty () const { return m_Leases.empty (); };
|
||
9 years ago
|
uint64_t GetExpirationTime () const { return m_ExpirationTime; };
|
||
8 years ago
|
bool ExpiresSoon(const uint64_t dlt=1000 * 5, const uint64_t fudge = 0) const ;
|
||
9 years ago
|
bool operator== (const LeaseSet& other) const
|
||
|
{ return m_BufferLen == other.m_BufferLen && !memcmp (m_Buffer, other.m_Buffer, m_BufferLen); };
|
||
9 years ago
|
|
||
|
// implements RoutingDestination
|
||
|
const IdentHash& GetIdentHash () const { return m_Identity->GetIdentHash (); };
|
||
11 years ago
|
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; };
|
||
11 years ago
|
bool IsDestination () const { return true; };
|
||
11 years ago
|
|
||
|
private:
|
||
|
|
||
9 years ago
|
void ReadFromBuffer (bool readIdentity = true);
|
||
9 years ago
|
uint64_t ExtractTimestamp (const uint8_t * buf, size_t len) const; // min expiration time
|
||
11 years ago
|
|
||
|
private:
|
||
|
|
||
9 years ago
|
bool m_IsValid, m_StoreLeases; // we don't need to store leases for floodfill
|
||
9 years ago
|
std::set<std::shared_ptr<Lease>, LeaseCmp> m_Leases;
|
||
9 years ago
|
uint64_t m_ExpirationTime; // in milliseconds
|
||
9 years ago
|
std::shared_ptr<const IdentityEx> m_Identity;
|
||
11 years ago
|
uint8_t m_EncryptionKey[256];
|
||
10 years ago
|
uint8_t * m_Buffer;
|
||
10 years ago
|
size_t m_BufferLen;
|
||
11 years ago
|
};
|
||
9 years ago
|
|
||
|
class LocalLeaseSet
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
LocalLeaseSet (std::shared_ptr<const IdentityEx> identity, const uint8_t * encryptionPublicKey, std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels);
|
||
9 years ago
|
LocalLeaseSet (std::shared_ptr<const IdentityEx> identity, const uint8_t * buf, size_t len);
|
||
9 years ago
|
~LocalLeaseSet () { delete[] m_Buffer; };
|
||
|
|
||
|
const uint8_t * GetBuffer () const { return m_Buffer; };
|
||
9 years ago
|
uint8_t * GetSignature () { return m_Buffer + m_BufferLen - GetSignatureLen (); };
|
||
9 years ago
|
size_t GetBufferLen () const { return m_BufferLen; };
|
||
|
size_t GetSignatureLen () const { return m_Identity->GetSignatureLen (); };
|
||
9 years ago
|
uint8_t * GetLeases () { return m_Leases; };
|
||
|
|
||
9 years ago
|
const IdentHash& GetIdentHash () const { return m_Identity->GetIdentHash (); };
|
||
9 years ago
|
bool IsExpired () const;
|
||
9 years ago
|
uint64_t GetExpirationTime () const { return m_ExpirationTime; };
|
||
|
void SetExpirationTime (uint64_t expirationTime) { m_ExpirationTime = expirationTime; };
|
||
9 years ago
|
bool operator== (const LeaseSet& other) const
|
||
|
{ return m_BufferLen == other.GetBufferLen () && !memcmp (other.GetBuffer (), other.GetBuffer (), m_BufferLen); };
|
||
|
|
||
9 years ago
|
|
||
|
private:
|
||
|
|
||
9 years ago
|
uint64_t m_ExpirationTime; // in milliseconds
|
||
9 years ago
|
std::shared_ptr<const IdentityEx> m_Identity;
|
||
9 years ago
|
uint8_t * m_Buffer, * m_Leases;
|
||
9 years ago
|
size_t m_BufferLen;
|
||
|
};
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|