2013-10-27 19:28:23 +04:00
|
|
|
#ifndef ROUTER_INFO_H__
|
|
|
|
#define ROUTER_INFO_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <boost/asio.hpp>
|
2014-01-10 07:26:30 +04:00
|
|
|
#include "Identity.h"
|
2013-10-27 19:28:23 +04:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace data
|
2014-09-03 00:11:31 +04:00
|
|
|
{
|
|
|
|
const char CAPS_FLAG_FLOODFILL = 'f';
|
|
|
|
const char CAPS_FLAG_HIDDEN = 'H';
|
|
|
|
const char CAPS_FLAG_REACHABLE = 'R';
|
|
|
|
const char CAPS_FLAG_UNREACHABLE = 'U';
|
|
|
|
const char CAPS_FLAG_LOW_BANDWIDTH1 = 'K';
|
|
|
|
const char CAPS_FLAG_LOW_BANDWIDTH2 = 'L';
|
|
|
|
const char CAPS_FLAG_HIGH_BANDWIDTH1 = 'M';
|
|
|
|
const char CAPS_FLAG_HIGH_BANDWIDTH2 = 'N';
|
|
|
|
const char CAPS_FLAG_HIGH_BANDWIDTH3 = 'O';
|
|
|
|
|
|
|
|
const char CAPS_FLAG_SSU_TESTING = 'B';
|
|
|
|
const char CAPS_FLAG_SSU_INTRODUCER = 'C';
|
|
|
|
|
2014-07-10 23:33:42 +04:00
|
|
|
const int MAX_RI_BUFFER_SIZE = 2048;
|
2013-11-25 03:10:27 +04:00
|
|
|
class RouterInfo: public RoutingDestination
|
2013-10-27 19:28:23 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-01-26 06:47:01 +04:00
|
|
|
enum SupportedTranports
|
|
|
|
{
|
|
|
|
eNTCPV4 = 0x01,
|
2014-03-19 20:02:51 +04:00
|
|
|
eNTCPV6 = 0x02,
|
|
|
|
eSSUV4 = 0x04,
|
|
|
|
eSSUV6 = 0x08
|
2014-01-26 06:47:01 +04:00
|
|
|
};
|
|
|
|
|
2014-03-19 20:02:51 +04:00
|
|
|
enum Caps
|
|
|
|
{
|
|
|
|
eFloodfill = 0x01,
|
2014-03-19 23:58:57 +04:00
|
|
|
eHighBandwidth = 0x02,
|
2014-04-09 05:56:34 +04:00
|
|
|
eReachable = 0x04,
|
|
|
|
eSSUTesting = 0x08,
|
2014-06-18 18:41:59 +04:00
|
|
|
eSSUIntroducer = 0x10,
|
2014-08-19 19:01:11 +04:00
|
|
|
eHidden = 0x20,
|
|
|
|
eUnreachable = 0x40
|
2014-03-19 20:02:51 +04:00
|
|
|
};
|
|
|
|
|
2013-10-27 19:28:23 +04:00
|
|
|
enum TransportStyle
|
|
|
|
{
|
|
|
|
eTransportUnknown = 0,
|
|
|
|
eTransportNTCP,
|
|
|
|
eTransportSSU
|
|
|
|
};
|
|
|
|
|
2014-02-21 01:15:12 +04:00
|
|
|
struct Introducer
|
|
|
|
{
|
|
|
|
boost::asio::ip::address iHost;
|
|
|
|
int iPort;
|
2014-09-18 04:43:47 +04:00
|
|
|
Tag<32> iKey;
|
2014-02-21 01:15:12 +04:00
|
|
|
uint32_t iTag;
|
|
|
|
};
|
|
|
|
|
2013-10-27 19:28:23 +04:00
|
|
|
struct Address
|
|
|
|
{
|
|
|
|
TransportStyle transportStyle;
|
2014-01-22 01:07:16 +04:00
|
|
|
boost::asio::ip::address host;
|
2015-01-17 07:01:40 +03:00
|
|
|
std::string addressString;
|
2014-09-15 01:57:47 +04:00
|
|
|
int port, mtu;
|
2013-10-27 19:28:23 +04:00
|
|
|
uint64_t date;
|
|
|
|
uint8_t cost;
|
2014-02-21 01:15:12 +04:00
|
|
|
// SSU only
|
2014-09-18 04:43:47 +04:00
|
|
|
Tag<32> key; // intro key for SSU
|
2014-02-21 01:15:12 +04:00
|
|
|
std::vector<Introducer> introducers;
|
2014-10-27 04:32:06 +03:00
|
|
|
|
|
|
|
bool IsCompatible (const boost::asio::ip::address& other) const
|
|
|
|
{
|
|
|
|
return (host.is_v4 () && other.is_v4 ()) ||
|
|
|
|
(host.is_v6 () && other.is_v6 ());
|
|
|
|
}
|
2013-10-27 19:28:23 +04:00
|
|
|
};
|
|
|
|
|
2014-07-23 18:56:41 +04:00
|
|
|
RouterInfo (const std::string& fullPath);
|
2014-10-26 05:23:28 +03:00
|
|
|
RouterInfo (): m_Buffer (nullptr) { };
|
2013-10-27 19:28:23 +04:00
|
|
|
RouterInfo (const RouterInfo& ) = default;
|
2013-12-29 19:48:57 +04:00
|
|
|
RouterInfo& operator=(const RouterInfo& ) = default;
|
2013-10-27 19:28:23 +04:00
|
|
|
RouterInfo (const uint8_t * buf, int len);
|
2014-07-22 06:59:57 +04:00
|
|
|
~RouterInfo ();
|
2013-10-27 19:28:23 +04:00
|
|
|
|
2014-11-20 20:21:27 +03:00
|
|
|
const IdentityEx& GetRouterIdentity () const { return m_RouterIdentity; };
|
|
|
|
void SetRouterIdentity (const IdentityEx& identity);
|
|
|
|
std::string GetIdentHashBase64 () const { return GetIdentHash ().ToBase64 (); };
|
|
|
|
std::string GetIdentHashAbbreviation () const { return GetIdentHash ().ToBase64 ().substr (0, 4); };
|
2013-11-29 16:52:09 +04:00
|
|
|
uint64_t GetTimestamp () const { return m_Timestamp; };
|
2014-02-09 06:06:40 +04:00
|
|
|
std::vector<Address>& GetAddresses () { return m_Addresses; };
|
2014-02-09 17:52:56 +04:00
|
|
|
const Address * GetNTCPAddress (bool v4only = true) const;
|
|
|
|
const Address * GetSSUAddress (bool v4only = true) const;
|
2014-10-30 04:16:27 +03:00
|
|
|
const Address * GetSSUV6Address () const;
|
2013-10-27 19:28:23 +04:00
|
|
|
|
|
|
|
void AddNTCPAddress (const char * host, int port);
|
2014-10-30 17:07:39 +03:00
|
|
|
void AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu = 0);
|
2014-09-02 01:34:20 +04:00
|
|
|
bool AddIntroducer (const Address * address, uint32_t tag);
|
2014-09-07 04:43:20 +04:00
|
|
|
bool RemoveIntroducer (const boost::asio::ip::udp::endpoint& e);
|
2015-03-18 22:06:15 +03:00
|
|
|
void SetProperty (const std::string& key, const std::string& value); // called from RouterContext only
|
|
|
|
void DeleteProperty (const std::string& key); // called from RouterContext only
|
2015-03-12 23:26:08 +03:00
|
|
|
void ClearProperties () { m_Properties.clear (); };
|
2013-10-27 19:28:23 +04:00
|
|
|
bool IsFloodfill () const;
|
2014-01-22 04:14:30 +04:00
|
|
|
bool IsNTCP (bool v4only = true) const;
|
2014-02-09 06:06:40 +04:00
|
|
|
bool IsSSU (bool v4only = true) const;
|
2014-10-27 04:32:06 +03:00
|
|
|
bool IsV6 () const;
|
|
|
|
void EnableV6 ();
|
|
|
|
void DisableV6 ();
|
2014-01-26 06:47:01 +04:00
|
|
|
bool IsCompatible (const RouterInfo& other) const { return m_SupportedTransports & other.m_SupportedTransports; };
|
2014-02-22 01:13:36 +04:00
|
|
|
bool UsesIntroducer () const;
|
2014-04-09 05:56:34 +04:00
|
|
|
bool IsIntroducer () const { return m_Caps & eSSUIntroducer; };
|
|
|
|
bool IsPeerTesting () const { return m_Caps & eSSUTesting; };
|
2014-06-18 18:41:59 +04:00
|
|
|
bool IsHidden () const { return m_Caps & eHidden; };
|
2015-03-18 20:07:11 +03:00
|
|
|
bool IsHighBandwidth () const { return m_Caps & RouterInfo::eHighBandwidth; };
|
2014-06-18 18:41:59 +04:00
|
|
|
|
2014-09-02 01:34:20 +04:00
|
|
|
uint8_t GetCaps () const { return m_Caps; };
|
2014-09-03 00:11:31 +04:00
|
|
|
void SetCaps (uint8_t caps);
|
2014-09-02 01:34:20 +04:00
|
|
|
void SetCaps (const char * caps);
|
2014-03-19 23:58:57 +04:00
|
|
|
|
2013-12-10 17:10:49 +04:00
|
|
|
void SetUnreachable (bool unreachable) { m_IsUnreachable = unreachable; };
|
|
|
|
bool IsUnreachable () const { return m_IsUnreachable; };
|
2014-07-10 23:33:42 +04:00
|
|
|
|
|
|
|
const uint8_t * GetBuffer () const { return m_Buffer; };
|
2014-07-24 23:59:00 +04:00
|
|
|
const uint8_t * LoadBuffer (); // load if necessary
|
2014-07-23 18:56:41 +04:00
|
|
|
int GetBufferLen () const { return m_BufferLen; };
|
2014-08-26 06:47:12 +04:00
|
|
|
void CreateBuffer (const PrivateKeys& privateKeys);
|
2013-11-20 16:46:09 +04:00
|
|
|
|
|
|
|
bool IsUpdated () const { return m_IsUpdated; };
|
|
|
|
void SetUpdated (bool updated) { m_IsUpdated = updated; };
|
2014-07-10 23:33:42 +04:00
|
|
|
void SaveToFile (const std::string& fullPath);
|
2013-11-25 03:10:27 +04:00
|
|
|
|
2014-07-22 06:59:57 +04:00
|
|
|
void Update (const uint8_t * buf, int len);
|
|
|
|
void DeleteBuffer () { delete m_Buffer; m_Buffer = nullptr; };
|
|
|
|
|
2013-11-25 03:10:27 +04:00
|
|
|
// implements RoutingDestination
|
2014-11-20 20:21:27 +03:00
|
|
|
const IdentHash& GetIdentHash () const { return m_RouterIdentity.GetIdentHash (); };
|
|
|
|
const uint8_t * GetEncryptionPublicKey () const { return m_RouterIdentity.GetStandardIdentity ().publicKey; };
|
2013-11-27 05:59:25 +04:00
|
|
|
bool IsDestination () const { return false; };
|
2014-07-22 06:59:57 +04:00
|
|
|
|
2013-10-27 19:28:23 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-07-23 18:56:41 +04:00
|
|
|
bool LoadFile ();
|
|
|
|
void ReadFromFile ();
|
2013-10-27 19:28:23 +04:00
|
|
|
void ReadFromStream (std::istream& s);
|
2014-10-29 23:03:07 +03:00
|
|
|
void ReadFromBuffer (bool verifySignature);
|
2013-10-27 19:28:23 +04:00
|
|
|
void WriteToStream (std::ostream& s);
|
|
|
|
size_t ReadString (char * str, std::istream& s);
|
|
|
|
void WriteString (const std::string& str, std::ostream& s);
|
2014-03-19 20:02:51 +04:00
|
|
|
void ExtractCaps (const char * value);
|
2014-10-30 04:16:27 +03:00
|
|
|
const Address * GetAddress (TransportStyle s, bool v4only, bool v6only = false) const;
|
2014-09-03 00:11:31 +04:00
|
|
|
void UpdateCapsProperty ();
|
|
|
|
|
2013-10-27 19:28:23 +04:00
|
|
|
private:
|
|
|
|
|
2014-07-23 18:56:41 +04:00
|
|
|
std::string m_FullPath;
|
2014-11-20 20:21:27 +03:00
|
|
|
IdentityEx m_RouterIdentity;
|
2014-07-22 06:59:57 +04:00
|
|
|
uint8_t * m_Buffer;
|
2013-10-27 19:28:23 +04:00
|
|
|
int m_BufferLen;
|
|
|
|
uint64_t m_Timestamp;
|
|
|
|
std::vector<Address> m_Addresses;
|
|
|
|
std::map<std::string, std::string> m_Properties;
|
2013-12-10 17:10:49 +04:00
|
|
|
bool m_IsUpdated, m_IsUnreachable;
|
2014-03-19 20:02:51 +04:00
|
|
|
uint8_t m_SupportedTransports, m_Caps;
|
2013-10-27 19:28:23 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|