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.
220 lines
7.5 KiB
220 lines
7.5 KiB
11 years ago
|
#ifndef ROUTER_INFO_H__
|
||
|
#define ROUTER_INFO_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
|
#include <string>
|
||
|
#include <map>
|
||
|
#include <vector>
|
||
8 years ago
|
#include <list>
|
||
11 years ago
|
#include <iostream>
|
||
|
#include <boost/asio.hpp>
|
||
8 years ago
|
#include <boost/shared_ptr.hpp>
|
||
11 years ago
|
#include "Identity.h"
|
||
10 years ago
|
#include "Profiling.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace data
|
||
10 years ago
|
{
|
||
9 years ago
|
const char ROUTER_INFO_PROPERTY_LEASESETS[] = "netdb.knownLeaseSets";
|
||
|
const char ROUTER_INFO_PROPERTY_ROUTERS[] = "netdb.knownRouters";
|
||
|
const char ROUTER_INFO_PROPERTY_NETID[] = "netId";
|
||
|
const char ROUTER_INFO_PROPERTY_FAMILY[] = "family";
|
||
|
const char ROUTER_INFO_PROPERTY_FAMILY_SIG[] = "family.sig";
|
||
|
|
||
10 years ago
|
const char CAPS_FLAG_FLOODFILL = 'f';
|
||
|
const char CAPS_FLAG_HIDDEN = 'H';
|
||
|
const char CAPS_FLAG_REACHABLE = 'R';
|
||
|
const char CAPS_FLAG_UNREACHABLE = 'U';
|
||
9 years ago
|
/* bandwidth flags */
|
||
|
const char CAPS_FLAG_LOW_BANDWIDTH1 = 'K'; /* < 12 KBps */
|
||
|
const char CAPS_FLAG_LOW_BANDWIDTH2 = 'L'; /* 12-48 KBps */
|
||
|
const char CAPS_FLAG_HIGH_BANDWIDTH1 = 'M'; /* 48-64 KBps */
|
||
|
const char CAPS_FLAG_HIGH_BANDWIDTH2 = 'N'; /* 64-128 KBps */
|
||
|
const char CAPS_FLAG_HIGH_BANDWIDTH3 = 'O'; /* 128-256 KBps */
|
||
|
const char CAPS_FLAG_EXTRA_BANDWIDTH1 = 'P'; /* 256-2000 KBps */
|
||
|
const char CAPS_FLAG_EXTRA_BANDWIDTH2 = 'X'; /* > 2000 KBps */
|
||
9 years ago
|
|
||
10 years ago
|
const char CAPS_FLAG_SSU_TESTING = 'B';
|
||
|
const char CAPS_FLAG_SSU_INTRODUCER = 'C';
|
||
|
|
||
10 years ago
|
const int MAX_RI_BUFFER_SIZE = 2048;
|
||
11 years ago
|
class RouterInfo: public RoutingDestination
|
||
11 years ago
|
{
|
||
|
public:
|
||
|
|
||
11 years ago
|
enum SupportedTranports
|
||
|
{
|
||
|
eNTCPV4 = 0x01,
|
||
11 years ago
|
eNTCPV6 = 0x02,
|
||
|
eSSUV4 = 0x04,
|
||
|
eSSUV6 = 0x08
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
enum Caps
|
||
|
{
|
||
|
eFloodfill = 0x01,
|
||
11 years ago
|
eHighBandwidth = 0x02,
|
||
9 years ago
|
eExtraBandwidth = 0x04,
|
||
|
eReachable = 0x08,
|
||
|
eSSUTesting = 0x10,
|
||
|
eSSUIntroducer = 0x20,
|
||
|
eHidden = 0x40,
|
||
|
eUnreachable = 0x80
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
enum TransportStyle
|
||
|
{
|
||
|
eTransportUnknown = 0,
|
||
|
eTransportNTCP,
|
||
|
eTransportSSU
|
||
|
};
|
||
|
|
||
9 years ago
|
typedef Tag<32> IntroKey; // should be castable to MacKey and AESKey
|
||
11 years ago
|
struct Introducer
|
||
|
{
|
||
|
boost::asio::ip::address iHost;
|
||
|
int iPort;
|
||
9 years ago
|
IntroKey iKey;
|
||
11 years ago
|
uint32_t iTag;
|
||
|
};
|
||
|
|
||
8 years ago
|
struct SSUExt
|
||
|
{
|
||
|
int mtu;
|
||
|
IntroKey key; // intro key for SSU
|
||
|
std::vector<Introducer> introducers;
|
||
|
};
|
||
|
|
||
11 years ago
|
struct Address
|
||
|
{
|
||
|
TransportStyle transportStyle;
|
||
11 years ago
|
boost::asio::ip::address host;
|
||
10 years ago
|
std::string addressString;
|
||
8 years ago
|
int port;
|
||
11 years ago
|
uint64_t date;
|
||
|
uint8_t cost;
|
||
8 years ago
|
std::unique_ptr<SSUExt> ssu; // not null for SSU
|
||
10 years ago
|
|
||
|
bool IsCompatible (const boost::asio::ip::address& other) const
|
||
|
{
|
||
|
return (host.is_v4 () && other.is_v4 ()) ||
|
||
|
(host.is_v6 () && other.is_v6 ());
|
||
|
}
|
||
9 years ago
|
|
||
|
bool operator==(const Address& other) const
|
||
|
{
|
||
|
return transportStyle == other.transportStyle && host == other.host && port == other.port;
|
||
|
}
|
||
|
|
||
|
bool operator!=(const Address& other) const
|
||
|
{
|
||
|
return !(*this == other);
|
||
|
}
|
||
11 years ago
|
};
|
||
8 years ago
|
typedef std::list<std::shared_ptr<Address> > Addresses;
|
||
8 years ago
|
|
||
8 years ago
|
RouterInfo ();
|
||
10 years ago
|
RouterInfo (const std::string& fullPath);
|
||
11 years ago
|
RouterInfo (const RouterInfo& ) = default;
|
||
11 years ago
|
RouterInfo& operator=(const RouterInfo& ) = default;
|
||
11 years ago
|
RouterInfo (const uint8_t * buf, int len);
|
||
10 years ago
|
~RouterInfo ();
|
||
11 years ago
|
|
||
9 years ago
|
std::shared_ptr<const IdentityEx> GetRouterIdentity () const { return m_RouterIdentity; };
|
||
|
void SetRouterIdentity (std::shared_ptr<const IdentityEx> identity);
|
||
10 years ago
|
std::string GetIdentHashBase64 () const { return GetIdentHash ().ToBase64 (); };
|
||
11 years ago
|
uint64_t GetTimestamp () const { return m_Timestamp; };
|
||
8 years ago
|
Addresses& GetAddresses () { return *m_Addresses; }; // should be called for local RI only, otherwise must return shared_ptr
|
||
9 years ago
|
std::shared_ptr<const Address> GetNTCPAddress (bool v4only = true) const;
|
||
|
std::shared_ptr<const Address> GetSSUAddress (bool v4only = true) const;
|
||
|
std::shared_ptr<const Address> GetSSUV6Address () const;
|
||
11 years ago
|
|
||
|
void AddNTCPAddress (const char * host, int port);
|
||
10 years ago
|
void AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu = 0);
|
||
9 years ago
|
bool AddIntroducer (const Introducer& introducer);
|
||
10 years ago
|
bool RemoveIntroducer (const boost::asio::ip::udp::endpoint& e);
|
||
10 years ago
|
void SetProperty (const std::string& key, const std::string& value); // called from RouterContext only
|
||
|
void DeleteProperty (const std::string& key); // called from RouterContext only
|
||
9 years ago
|
std::string GetProperty (const std::string& key) const; // called from RouterContext only
|
||
10 years ago
|
void ClearProperties () { m_Properties.clear (); };
|
||
9 years ago
|
bool IsFloodfill () const { return m_Caps & Caps::eFloodfill; };
|
||
|
bool IsReachable () const { return m_Caps & Caps::eReachable; };
|
||
11 years ago
|
bool IsNTCP (bool v4only = true) const;
|
||
11 years ago
|
bool IsSSU (bool v4only = true) const;
|
||
10 years ago
|
bool IsV6 () const;
|
||
9 years ago
|
bool IsV4 () const;
|
||
10 years ago
|
void EnableV6 ();
|
||
|
void DisableV6 ();
|
||
9 years ago
|
void EnableV4 ();
|
||
|
void DisableV4 ();
|
||
11 years ago
|
bool IsCompatible (const RouterInfo& other) const { return m_SupportedTransports & other.m_SupportedTransports; };
|
||
11 years ago
|
bool UsesIntroducer () const;
|
||
11 years ago
|
bool IsIntroducer () const { return m_Caps & eSSUIntroducer; };
|
||
|
bool IsPeerTesting () const { return m_Caps & eSSUTesting; };
|
||
11 years ago
|
bool IsHidden () const { return m_Caps & eHidden; };
|
||
10 years ago
|
bool IsHighBandwidth () const { return m_Caps & RouterInfo::eHighBandwidth; };
|
||
9 years ago
|
bool IsExtraBandwidth () const { return m_Caps & RouterInfo::eExtraBandwidth; };
|
||
|
|
||
10 years ago
|
uint8_t GetCaps () const { return m_Caps; };
|
||
10 years ago
|
void SetCaps (uint8_t caps);
|
||
10 years ago
|
void SetCaps (const char * caps);
|
||
11 years ago
|
|
||
11 years ago
|
void SetUnreachable (bool unreachable) { m_IsUnreachable = unreachable; };
|
||
|
bool IsUnreachable () const { return m_IsUnreachable; };
|
||
10 years ago
|
|
||
|
const uint8_t * GetBuffer () const { return m_Buffer; };
|
||
10 years ago
|
const uint8_t * LoadBuffer (); // load if necessary
|
||
10 years ago
|
int GetBufferLen () const { return m_BufferLen; };
|
||
10 years ago
|
void CreateBuffer (const PrivateKeys& privateKeys);
|
||
11 years ago
|
|
||
|
bool IsUpdated () const { return m_IsUpdated; };
|
||
|
void SetUpdated (bool updated) { m_IsUpdated = updated; };
|
||
9 years ago
|
bool SaveToFile (const std::string& fullPath);
|
||
11 years ago
|
|
||
10 years ago
|
std::shared_ptr<RouterProfile> GetProfile () const;
|
||
8 years ago
|
void SaveProfile () { if (m_Profile) m_Profile->Save (GetIdentHash ()); };
|
||
10 years ago
|
|
||
10 years ago
|
void Update (const uint8_t * buf, int len);
|
||
10 years ago
|
void DeleteBuffer () { delete[] m_Buffer; m_Buffer = nullptr; };
|
||
9 years ago
|
bool IsNewer (const uint8_t * buf, size_t len) const;
|
||
|
|
||
9 years ago
|
/** return true if we are in a router family and the signature is valid */
|
||
|
bool IsFamily(const std::string & fam) const;
|
||
|
|
||
11 years ago
|
// implements RoutingDestination
|
||
9 years ago
|
const IdentHash& GetIdentHash () const { return m_RouterIdentity->GetIdentHash (); };
|
||
|
const uint8_t * GetEncryptionPublicKey () const { return m_RouterIdentity->GetStandardIdentity ().publicKey; };
|
||
11 years ago
|
bool IsDestination () const { return false; };
|
||
10 years ago
|
|
||
11 years ago
|
private:
|
||
|
|
||
10 years ago
|
bool LoadFile ();
|
||
|
void ReadFromFile ();
|
||
11 years ago
|
void ReadFromStream (std::istream& s);
|
||
10 years ago
|
void ReadFromBuffer (bool verifySignature);
|
||
8 years ago
|
void WriteToStream (std::ostream& s) const;
|
||
8 years ago
|
size_t ReadString (char* str, size_t len, std::istream& s) const;
|
||
|
void WriteString (const std::string& str, std::ostream& s) const;
|
||
11 years ago
|
void ExtractCaps (const char * value);
|
||
9 years ago
|
std::shared_ptr<const Address> GetAddress (TransportStyle s, bool v4only, bool v6only = false) const;
|
||
10 years ago
|
void UpdateCapsProperty ();
|
||
|
|
||
11 years ago
|
private:
|
||
|
|
||
9 years ago
|
std::string m_FullPath, m_Family;
|
||
9 years ago
|
std::shared_ptr<const IdentityEx> m_RouterIdentity;
|
||
10 years ago
|
uint8_t * m_Buffer;
|
||
9 years ago
|
size_t m_BufferLen;
|
||
11 years ago
|
uint64_t m_Timestamp;
|
||
8 years ago
|
boost::shared_ptr<Addresses> m_Addresses; // TODO: use std::shared_ptr and std::atomic_store for gcc >= 4.9
|
||
11 years ago
|
std::map<std::string, std::string> m_Properties;
|
||
11 years ago
|
bool m_IsUpdated, m_IsUnreachable;
|
||
11 years ago
|
uint8_t m_SupportedTransports, m_Caps;
|
||
10 years ago
|
mutable std::shared_ptr<RouterProfile> m_Profile;
|
||
11 years ago
|
};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|