i2pd/libi2pd/RouterInfo.h

329 lines
12 KiB
C
Raw Normal View History

/*
* Copyright (c) 2013-2022, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
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>
2021-12-30 23:16:13 +03:00
#include <array>
2013-10-27 19:28:23 +04:00
#include <iostream>
#include <boost/asio.hpp>
2018-01-06 06:48:51 +03:00
#include <boost/shared_ptr.hpp>
2014-01-10 07:26:30 +04:00
#include "Identity.h"
2015-03-24 19:47:57 +03:00
#include "Profiling.h"
2022-03-24 22:50:20 +03:00
#include "Family.h"
2013-10-27 19:28:23 +04:00
namespace i2p
{
namespace data
2014-09-03 00:11:31 +04:00
{
2016-02-21 04:20:19 +03:00
const char ROUTER_INFO_PROPERTY_LEASESETS[] = "netdb.knownLeaseSets";
2018-01-06 06:48:51 +03:00
const char ROUTER_INFO_PROPERTY_ROUTERS[] = "netdb.knownRouters";
2016-02-21 04:20:19 +03:00
const char ROUTER_INFO_PROPERTY_NETID[] = "netId";
const char ROUTER_INFO_PROPERTY_VERSION[] = "router.version";
2018-01-06 06:48:51 +03:00
const char ROUTER_INFO_PROPERTY_FAMILY[] = "family";
2016-02-21 04:20:19 +03:00
const char ROUTER_INFO_PROPERTY_FAMILY_SIG[] = "family.sig";
2018-01-06 06:48:51 +03:00
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';
2018-01-06 06:48:51 +03:00
const char CAPS_FLAG_UNREACHABLE = 'U';
/* 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 */
2018-01-06 06:48:51 +03:00
2021-02-23 06:53:25 +03:00
const char CAPS_FLAG_V4 = '4';
const char CAPS_FLAG_V6 = '6';
2014-09-03 00:11:31 +04:00
const char CAPS_FLAG_SSU_TESTING = 'B';
const char CAPS_FLAG_SSU_INTRODUCER = 'C';
2021-03-01 19:09:25 +03:00
const uint8_t COST_NTCP2_PUBLISHED = 3;
const uint8_t COST_NTCP2_NON_PUBLISHED = 14;
const uint8_t COST_SSU_DIRECT = 9;
const uint8_t COST_SSU_THROUGH_INTRODUCERS = 11;
2022-03-13 05:40:12 +03:00
const uint8_t COST_SSU2_NON_PUBLISHED = 15;
const size_t MAX_RI_BUFFER_SIZE = 2048; // if RouterInfo exceeds 2048 we consider it as malformed, might be changed later
2013-11-25 03:10:27 +04:00
class RouterInfo: public RoutingDestination
2013-10-27 19:28:23 +04:00
{
public:
2021-06-02 19:55:08 +03:00
enum SupportedTransports
2018-01-06 06:48:51 +03:00
{
eNTCP2V4 = 0x01,
eNTCP2V6 = 0x02,
2014-03-19 20:02:51 +04:00
eSSUV4 = 0x04,
2021-01-31 02:32:17 +03:00
eSSUV6 = 0x08,
eNTCP2V6Mesh = 0x10,
2022-03-17 04:11:48 +03:00
eSSU2V4 = 0x20,
eSSU2V6 = 0x40,
eAllTransports = 0xFF
};
typedef uint8_t CompatibleTransports;
2014-03-19 20:02:51 +04:00
enum Caps
{
eFloodfill = 0x01,
2014-03-19 23:58:57 +04:00
eHighBandwidth = 0x02,
2016-01-03 06:17:04 +03:00
eExtraBandwidth = 0x04,
eReachable = 0x08,
2021-02-23 06:53:25 +03:00
eHidden = 0x10,
eUnreachable = 0x20
2014-03-19 20:02:51 +04:00
};
2021-02-23 06:53:25 +03:00
enum AddressCaps
{
eV4 = 0x01,
eV6 = 0x02,
eSSUTesting = 0x04,
eSSUIntroducer = 0x08
};
2013-10-27 19:28:23 +04:00
enum TransportStyle
{
eTransportUnknown = 0,
eTransportNTCP,
2022-03-12 00:17:44 +03:00
eTransportSSU,
eTransportSSU2
2013-10-27 19:28:23 +04:00
};
2015-11-03 17:15:49 +03:00
typedef Tag<32> IntroKey; // should be castable to MacKey and AESKey
2018-01-06 06:48:51 +03:00
struct Introducer
2014-02-21 01:15:12 +04:00
{
2021-04-04 17:36:22 +03:00
Introducer (): iPort (0), iExp (0) {};
2014-02-21 01:15:12 +04:00
boost::asio::ip::address iHost;
int iPort;
2015-11-03 17:15:49 +03:00
IntroKey iKey;
2014-02-21 01:15:12 +04:00
uint32_t iTag;
2017-05-24 19:49:36 +03:00
uint32_t iExp;
2014-02-21 01:15:12 +04:00
};
struct SSUExt
{
int mtu;
2018-01-06 06:48:51 +03:00
std::vector<Introducer> introducers;
};
2018-01-06 06:48:51 +03:00
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;
2022-02-06 18:17:35 +03:00
Tag<32> s, i; // keys, i is first 16 bytes for NTCP2 and 32 bytes intro key for SSU
int port;
2013-10-27 19:28:23 +04:00
uint64_t date;
2021-04-07 20:05:38 +03:00
uint8_t caps;
2021-04-06 04:45:48 +03:00
bool published = false;
std::unique_ptr<SSUExt> ssu; // not null for SSU
2018-01-06 06:48:51 +03:00
bool IsCompatible (const boost::asio::ip::address& other) const
{
2021-03-13 04:51:12 +03:00
return (IsV4 () && other.is_v4 ()) ||
(IsV6 () && other.is_v6 ());
2018-01-06 06:48:51 +03:00
}
2015-11-03 17:15:49 +03:00
bool operator==(const Address& other) const
{
2018-08-26 16:40:27 +03:00
return transportStyle == other.transportStyle && IsNTCP2 () == other.IsNTCP2 () &&
host == other.host && port == other.port;
2018-01-06 06:48:51 +03:00
}
2015-11-03 17:15:49 +03:00
bool operator!=(const Address& other) const
{
return !(*this == other);
2018-01-06 06:48:51 +03:00
}
2018-06-15 19:52:43 +03:00
2022-02-06 01:14:25 +03:00
bool IsNTCP2 () const { return transportStyle == eTransportNTCP; };
2022-03-12 00:17:44 +03:00
bool IsSSU2 () const { return transportStyle == eTransportSSU2; };
2021-04-06 04:45:48 +03:00
bool IsPublishedNTCP2 () const { return IsNTCP2 () && published; };
2021-06-02 19:55:08 +03:00
bool IsReachableSSU () const { return (bool)ssu && (published || !ssu->introducers.empty ()); };
bool UsesIntroducer () const { return (bool)ssu && !ssu->introducers.empty (); };
2021-02-23 05:04:26 +03:00
bool IsIntroducer () const { return caps & eSSUIntroducer; };
bool IsPeerTesting () const { return caps & eSSUTesting; };
2021-03-13 00:13:01 +03:00
2021-04-04 05:18:09 +03:00
bool IsV4 () const { return (caps & AddressCaps::eV4) || (host.is_v4 () && !host.is_unspecified ()); };
bool IsV6 () const { return (caps & AddressCaps::eV6) || (host.is_v6 () && !host.is_unspecified ()); };
2013-10-27 19:28:23 +04:00
};
class Buffer: public std::array<uint8_t, MAX_RI_BUFFER_SIZE>
{
public:
Buffer () = default;
Buffer (const uint8_t * buf, size_t len);
};
2021-10-10 16:53:21 +03:00
typedef std::vector<std::shared_ptr<Address> > Addresses;
2016-07-13 19:56:23 +03:00
2014-07-23 18:56:41 +04:00
RouterInfo (const std::string& fullPath);
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;
RouterInfo (std::shared_ptr<Buffer>&& buf, size_t len);
RouterInfo (const uint8_t * buf, size_t len);
2022-01-15 20:48:49 +03:00
virtual ~RouterInfo ();
2018-01-06 06:48:51 +03:00
2015-11-03 17:15:49 +03:00
std::shared_ptr<const IdentityEx> GetRouterIdentity () const { return m_RouterIdentity; };
void SetRouterIdentity (std::shared_ptr<const IdentityEx> identity);
2014-11-20 20:21:27 +03:00
std::string GetIdentHashBase64 () const { return GetIdentHash ().ToBase64 (); };
2013-11-29 16:52:09 +04:00
uint64_t GetTimestamp () const { return m_Timestamp; };
int GetVersion () const { return m_Version; };
2022-01-16 02:54:02 +03:00
virtual void SetProperty (const std::string& key, const std::string& value) {};
2022-01-16 03:26:11 +03:00
virtual void ClearProperties () {};
2016-07-13 19:56:23 +03:00
Addresses& GetAddresses () { return *m_Addresses; }; // should be called for local RI only, otherwise must return shared_ptr
std::shared_ptr<const Address> GetNTCP2AddressWithStaticKey (const uint8_t * key) const;
2022-03-29 01:03:22 +03:00
std::shared_ptr<const Address> GetSSU2AddressWithStaticKey (const uint8_t * key, bool isV6) const;
std::shared_ptr<const Address> GetPublishedNTCP2V4Address () const;
std::shared_ptr<const Address> GetPublishedNTCP2V6Address () const;
2016-03-21 20:02:51 +03:00
std::shared_ptr<const Address> GetSSUAddress (bool v4only = true) const;
std::shared_ptr<const Address> GetSSUV6Address () const;
2021-02-01 01:25:07 +03:00
std::shared_ptr<const Address> GetYggdrasilAddress () const;
2022-03-17 04:11:48 +03:00
std::shared_ptr<const Address> GetSSU2V4Address () const;
std::shared_ptr<const Address> GetSSU2V6Address () const;
2018-01-06 06:48:51 +03:00
2014-10-30 17:07:39 +03:00
void AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu = 0);
void AddNTCP2Address (const uint8_t * staticKey, const uint8_t * iv,
const boost::asio::ip::address& host = boost::asio::ip::address(), int port = 0, uint8_t caps = 0);
void AddSSU2Address (const uint8_t * staticKey, const uint8_t * introKey, uint8_t caps = 0); // non published
2015-11-03 17:15:49 +03:00
bool AddIntroducer (const Introducer& introducer);
2014-09-07 04:43:20 +04:00
bool RemoveIntroducer (const boost::asio::ip::udp::endpoint& e);
void SetUnreachableAddressesTransportCaps (uint8_t transports); // bitmask of AddressCaps
2021-06-02 19:55:08 +03:00
void UpdateSupportedTransports ();
2016-02-22 18:27:43 +03:00
bool IsFloodfill () const { return m_Caps & Caps::eFloodfill; };
bool IsReachable () const { return m_Caps & Caps::eReachable; };
bool IsECIES () const { return m_RouterIdentity->GetCryptoKeyType () == i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD; };
2014-02-09 06:06:40 +04:00
bool IsSSU (bool v4only = true) const;
2022-03-17 04:11:48 +03:00
bool IsSSUV6 () const { return m_SupportedTransports & eSSUV6; };
2018-06-06 18:51:34 +03:00
bool IsNTCP2 (bool v4only = true) const;
2022-03-17 04:11:48 +03:00
bool IsNTCP2V6 () const { return m_SupportedTransports & eNTCP2V6; };
bool IsSSU2V4 () const { return m_SupportedTransports & eSSU2V4; };
bool IsSSU2V6 () const { return m_SupportedTransports & eSSU2V6; };
bool IsV6 () const { return m_SupportedTransports & (eSSUV6 | eNTCP2V6 | eSSU2V6); };
bool IsV4 () const { return m_SupportedTransports & (eSSUV4 | eNTCP2V4 | eSSU2V4); };
bool IsMesh () const { return m_SupportedTransports & eNTCP2V6Mesh; };
void EnableV6 ();
void DisableV6 ();
2016-03-25 01:44:41 +03:00
void EnableV4 ();
void DisableV4 ();
2021-02-01 01:25:07 +03:00
void EnableMesh ();
void DisableMesh ();
bool IsCompatible (const RouterInfo& other) const { return m_SupportedTransports & other.m_SupportedTransports; };
bool IsReachableFrom (const RouterInfo& other) const { return m_ReachableTransports & other.m_SupportedTransports; };
bool IsReachableBy (CompatibleTransports transports) const { return m_ReachableTransports & transports; };
CompatibleTransports GetCompatibleTransports (bool incoming) const { return incoming ? m_ReachableTransports : m_SupportedTransports; };
bool HasValidAddresses () const { return m_SupportedTransports; };
2014-06-18 18:41:59 +04:00
bool IsHidden () const { return m_Caps & eHidden; };
bool IsHighBandwidth () const { return m_Caps & RouterInfo::eHighBandwidth; };
2018-01-06 06:48:51 +03:00
bool IsExtraBandwidth () const { return m_Caps & RouterInfo::eExtraBandwidth; };
2020-11-23 20:49:18 +03:00
bool IsEligibleFloodfill () const;
bool IsPeerTesting (bool v4) const;
bool IsIntroducer (bool v4) const;
2018-01-06 06:48:51 +03:00
uint8_t GetCaps () const { return m_Caps; };
2022-01-16 02:54:02 +03:00
void SetCaps (uint8_t caps) { m_Caps = caps; };
2014-03-19 23:58:57 +04:00
2018-01-06 06:48:51 +03:00
void SetUnreachable (bool unreachable) { m_IsUnreachable = unreachable; };
2013-12-10 17:10:49 +04:00
bool IsUnreachable () const { return m_IsUnreachable; };
2014-07-10 23:33:42 +04:00
2021-12-30 23:16:13 +03:00
const uint8_t * GetBuffer () const { return m_Buffer->data (); };
2021-10-07 22:08:33 +03:00
const uint8_t * LoadBuffer (const std::string& fullPath); // load if necessary
2022-01-15 20:48:49 +03:00
size_t GetBufferLen () const { return m_BufferLen; };
2013-11-20 16:46:09 +04:00
bool IsUpdated () const { return m_IsUpdated; };
2018-01-06 06:48:51 +03:00
void SetUpdated (bool updated) { m_IsUpdated = updated; };
bool SaveToFile (const std::string& fullPath);
2013-11-25 03:10:27 +04:00
std::shared_ptr<RouterProfile> GetProfile () const;
2016-12-31 04:59:18 +03:00
void SaveProfile () { if (m_Profile) m_Profile->Save (GetIdentHash ()); };
2018-01-06 06:48:51 +03:00
void Update (const uint8_t * buf, size_t len);
2021-12-30 23:16:13 +03:00
void DeleteBuffer () { m_Buffer = nullptr; };
2018-01-06 06:48:51 +03:00
bool IsNewer (const uint8_t * buf, size_t len) const;
2016-02-17 23:36:55 +03:00
/** return true if we are in a router family and the signature is valid */
2022-03-24 22:50:20 +03:00
bool IsFamily (FamilyID famid) const;
2018-01-06 06:48:51 +03:00
2013-11-25 03:10:27 +04:00
// implements RoutingDestination
2017-11-02 21:50:57 +03:00
std::shared_ptr<const IdentityEx> GetIdentity () const { return m_RouterIdentity; };
void Encrypt (const uint8_t * data, uint8_t * encrypted) const;
bool IsDestination () const { return false; };
2022-01-15 20:48:49 +03:00
protected:
RouterInfo ();
uint8_t * GetBufferPointer (size_t offset = 0 ) { return m_Buffer->data () + offset; };
void UpdateBuffer (const uint8_t * buf, size_t len);
void SetBufferLen (size_t len) { m_BufferLen = len; };
void RefreshTimestamp ();
2022-01-16 02:54:02 +03:00
const Addresses& GetAddresses () const { return *m_Addresses; };
2022-01-15 20:48:49 +03:00
2013-10-27 19:28:23 +04:00
private:
2021-10-07 22:08:33 +03:00
bool LoadFile (const std::string& fullPath);
void ReadFromFile (const std::string& fullPath);
2013-10-27 19:28:23 +04:00
void ReadFromStream (std::istream& s);
void ReadFromBuffer (bool verifySignature);
2016-08-15 20:12:56 +03:00
size_t ReadString (char* str, size_t len, std::istream& s) const;
2021-02-23 06:53:25 +03:00
void ExtractCaps (const char * value);
uint8_t ExtractAddressCaps (const char * value) const;
2018-08-04 15:47:58 +03:00
template<typename Filter>
std::shared_ptr<const Address> GetAddress (Filter filter) const;
virtual std::shared_ptr<Buffer> NewBuffer () const;
2013-10-27 19:28:23 +04:00
private:
2022-03-24 22:50:20 +03:00
FamilyID m_FamilyID;
2015-11-03 17:15:49 +03:00
std::shared_ptr<const IdentityEx> m_RouterIdentity;
2021-12-30 23:16:13 +03:00
std::shared_ptr<Buffer> m_Buffer;
2016-02-02 19:55:38 +03:00
size_t m_BufferLen;
2013-10-27 19:28:23 +04:00
uint64_t m_Timestamp;
2018-01-06 06:48:51 +03:00
boost::shared_ptr<Addresses> m_Addresses; // TODO: use std::shared_ptr and std::atomic_store for gcc >= 4.9
2013-12-10 17:10:49 +04:00
bool m_IsUpdated, m_IsUnreachable;
CompatibleTransports m_SupportedTransports, m_ReachableTransports;
uint8_t m_Caps;
int m_Version;
mutable std::shared_ptr<RouterProfile> m_Profile;
2018-01-06 06:48:51 +03:00
};
2022-01-15 20:48:49 +03:00
class LocalRouterInfo: public RouterInfo
{
public:
LocalRouterInfo () = default;
void CreateBuffer (const PrivateKeys& privateKeys);
2022-01-16 02:54:02 +03:00
void UpdateCaps (uint8_t caps);
void SetProperty (const std::string& key, const std::string& value) override;
void DeleteProperty (const std::string& key);
std::string GetProperty (const std::string& key) const;
2022-01-16 03:26:11 +03:00
void ClearProperties () override { m_Properties.clear (); };
2022-01-16 02:54:02 +03:00
private:
void WriteToStream (std::ostream& s) const;
void UpdateCapsProperty ();
void WriteString (const std::string& str, std::ostream& s) const;
std::shared_ptr<Buffer> NewBuffer () const override;
2022-01-16 02:54:02 +03:00
private:
std::map<std::string, std::string> m_Properties;
2022-01-15 20:48:49 +03:00
};
2018-01-06 06:48:51 +03:00
}
2013-10-27 19:28:23 +04:00
}
#endif