i2pd/libi2pd/RouterInfo.cpp

1486 lines
39 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2013-2023, 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
#include <stdio.h>
#include <string.h>
#include "I2PEndian.h"
2013-10-27 19:28:23 +04:00
#include <fstream>
#include <boost/lexical_cast.hpp>
#include <boost/make_shared.hpp>
2016-09-24 15:29:08 +03:00
#if (BOOST_VERSION >= 105300)
#include <boost/atomic.hpp>
#endif
2016-01-16 23:36:30 +03:00
#include "version.h"
2021-01-31 02:32:17 +03:00
#include "util.h"
2015-11-03 17:15:49 +03:00
#include "Crypto.h"
#include "Base.h"
2013-10-27 19:28:23 +04:00
#include "Timestamp.h"
#include "Log.h"
#include "NetDb.hpp"
2016-10-12 18:26:48 +03:00
#include "RouterContext.h"
2013-10-27 19:28:23 +04:00
#include "RouterInfo.h"
2014-01-22 10:46:58 +04:00
2013-10-27 19:28:23 +04:00
namespace i2p
{
namespace data
2018-01-06 06:48:51 +03:00
{
RouterInfo::Buffer::Buffer (const uint8_t * buf, size_t len)
{
if (len > size ()) len = size ();
memcpy (data (), buf, len);
}
2018-01-06 06:48:51 +03:00
RouterInfo::RouterInfo (): m_Buffer (nullptr)
{
m_Addresses = boost::make_shared<Addresses>(); // create empty list
}
2018-01-06 06:48:51 +03:00
2014-07-23 18:56:41 +04:00
RouterInfo::RouterInfo (const std::string& fullPath):
m_FamilyID (0), m_IsUpdated (false), m_IsUnreachable (false),
m_SupportedTransports (0),m_ReachableTransports (0),
2023-03-04 04:21:56 +03:00
m_Caps (0), m_Version (0), m_Congestion (eLowCongestion)
2013-10-27 19:28:23 +04:00
{
m_Addresses = boost::make_shared<Addresses>(); // create empty list
m_Buffer = NewBuffer (); // always RouterInfo's
2021-10-07 22:08:33 +03:00
ReadFromFile (fullPath);
2018-01-06 06:48:51 +03:00
}
2013-10-27 19:28:23 +04:00
RouterInfo::RouterInfo (std::shared_ptr<Buffer>&& buf, size_t len):
m_FamilyID (0), m_IsUpdated (true), m_IsUnreachable (false),
m_SupportedTransports (0), m_ReachableTransports (0),
2023-03-04 04:21:56 +03:00
m_Caps (0), m_Version (0), m_Congestion (eLowCongestion)
2013-10-27 19:28:23 +04:00
{
if (len <= MAX_RI_BUFFER_SIZE)
{
m_Addresses = boost::make_shared<Addresses>(); // create empty list
m_Buffer = buf;
m_BufferLen = len;
ReadFromBuffer (true);
}
else
{
LogPrint (eLogError, "RouterInfo: Buffer is too long ", len, ". Ignored");
m_Buffer = nullptr;
m_IsUnreachable = true;
}
}
RouterInfo::RouterInfo (const uint8_t * buf, size_t len):
RouterInfo (std::make_shared<Buffer> (buf, len), len)
{
}
2014-07-22 04:14:11 +04:00
RouterInfo::~RouterInfo ()
{
2018-01-06 06:48:51 +03:00
}
2023-02-13 02:02:16 +03:00
bool RouterInfo::Update (const uint8_t * buf, size_t len)
2014-07-22 04:14:11 +04:00
{
2019-05-23 18:49:54 +03:00
if (len > MAX_RI_BUFFER_SIZE)
{
2023-02-13 02:02:16 +03:00
LogPrint (eLogWarning, "RouterInfo: Updated buffer is too long ", len, ". Not changed");
return false;
2019-05-23 18:49:54 +03:00
}
2019-04-08 22:22:42 +03:00
// verify signature since we have identity already
2015-11-03 17:15:49 +03:00
int l = len - m_RouterIdentity->GetSignatureLen ();
if (m_RouterIdentity->Verify (buf, l, buf + l))
{
// clean up
m_IsUpdated = true;
m_IsUnreachable = false;
m_SupportedTransports = 0;
2021-06-02 19:55:08 +03:00
m_ReachableTransports = 0;
2015-11-03 17:15:49 +03:00
m_Caps = 0;
2016-07-13 19:56:23 +03:00
// don't clean up m_Addresses, it will be replaced in ReadFromStream
2022-01-16 03:26:11 +03:00
ClearProperties ();
2015-11-03 17:15:49 +03:00
// skip identity
size_t identityLen = m_RouterIdentity->GetFullLen ();
2018-01-06 06:48:51 +03:00
// read new RI
std::stringstream str (std::string ((char *)buf + identityLen, len - identityLen));
2015-11-03 17:15:49 +03:00
ReadFromStream (str);
if (!m_IsUnreachable)
UpdateBuffer (buf, len); // save buffer
2015-11-03 17:15:49 +03:00
// don't delete buffer until saved to the file
}
else
2023-02-13 02:02:16 +03:00
{
LogPrint (eLogWarning, "RouterInfo: Updated signature verification failed. Not changed");
return false;
}
return true;
2018-01-06 06:48:51 +03:00
}
2015-11-03 17:15:49 +03:00
void RouterInfo::SetRouterIdentity (std::shared_ptr<const IdentityEx> identity)
2018-01-06 06:48:51 +03:00
{
2013-10-27 19:28:23 +04:00
m_RouterIdentity = identity;
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
}
2018-01-06 06:48:51 +03:00
2021-10-07 22:08:33 +03:00
bool RouterInfo::LoadFile (const std::string& fullPath)
2013-10-27 19:28:23 +04:00
{
2021-10-07 22:08:33 +03:00
std::ifstream s(fullPath, std::ifstream::binary);
2018-01-06 06:48:51 +03:00
if (s.is_open ())
{
2013-10-27 19:28:23 +04:00
s.seekg (0,std::ios::end);
2014-02-03 02:22:00 +04:00
m_BufferLen = s.tellg ();
2016-02-02 19:55:38 +03:00
if (m_BufferLen < 40 || m_BufferLen > MAX_RI_BUFFER_SIZE)
2014-02-03 02:22:00 +04:00
{
2021-10-07 22:08:33 +03:00
LogPrint(eLogError, "RouterInfo: File", fullPath, " is malformed");
2014-07-23 18:56:41 +04:00
return false;
2014-02-03 02:22:00 +04:00
}
2013-10-27 19:28:23 +04:00
s.seekg(0, std::ios::beg);
2021-12-28 16:00:03 +03:00
if (!m_Buffer)
m_Buffer = NewBuffer ();
2021-12-30 23:16:13 +03:00
s.read((char *)m_Buffer->data (), m_BufferLen);
2018-01-06 06:48:51 +03:00
}
2013-10-27 19:28:23 +04:00
else
2014-07-23 18:56:41 +04:00
{
2021-10-07 22:08:33 +03:00
LogPrint (eLogError, "RouterInfo: Can't open file ", fullPath);
2018-01-06 06:48:51 +03:00
return false;
2014-07-23 18:56:41 +04:00
}
return true;
2018-01-06 06:48:51 +03:00
}
2014-07-23 18:56:41 +04:00
2021-10-07 22:08:33 +03:00
void RouterInfo::ReadFromFile (const std::string& fullPath)
2014-07-23 18:56:41 +04:00
{
2021-10-07 22:08:33 +03:00
if (LoadFile (fullPath))
2018-01-06 06:48:51 +03:00
ReadFromBuffer (false);
else
2018-01-06 06:48:51 +03:00
m_IsUnreachable = true;
}
2013-10-27 19:28:23 +04:00
void RouterInfo::ReadFromBuffer (bool verifySignature)
2013-10-27 19:28:23 +04:00
{
if (!m_Buffer)
{
m_IsUnreachable = true;
return;
}
2023-03-17 04:32:53 +03:00
m_RouterIdentity = NewIdentity (m_Buffer->data (), m_BufferLen);
2015-11-03 17:15:49 +03:00
size_t identityLen = m_RouterIdentity->GetFullLen ();
2016-02-02 19:55:38 +03:00
if (identityLen >= m_BufferLen)
{
LogPrint (eLogError, "RouterInfo: Identity length ", identityLen, " exceeds buffer size ", m_BufferLen);
2016-02-02 19:55:38 +03:00
m_IsUnreachable = true;
return;
}
if (verifySignature)
2018-01-06 06:48:51 +03:00
{
2017-11-13 19:25:42 +03:00
// reject RSA signatures
if (m_RouterIdentity->IsRSA ())
2017-11-13 19:25:42 +03:00
{
LogPrint (eLogError, "RouterInfo: RSA signature type is not allowed");
2017-11-13 19:25:42 +03:00
m_IsUnreachable = true;
return;
}
// verify signature
2018-01-06 06:48:51 +03:00
int l = m_BufferLen - m_RouterIdentity->GetSignatureLen ();
2021-12-30 23:16:13 +03:00
if (l < 0 || !m_RouterIdentity->Verify ((uint8_t *)m_Buffer->data (), l, (uint8_t *)m_Buffer->data () + l))
2018-01-06 06:48:51 +03:00
{
LogPrint (eLogError, "RouterInfo: Signature verification failed");
2014-11-26 18:28:06 +03:00
m_IsUnreachable = true;
2016-09-13 04:37:43 +03:00
return;
2014-11-26 18:28:06 +03:00
}
2018-01-06 06:48:51 +03:00
}
2016-09-13 04:37:43 +03:00
// parse RI
std::stringstream str;
2021-12-30 23:16:13 +03:00
str.write ((const char *)m_Buffer->data () + identityLen, m_BufferLen - identityLen);
2016-09-13 04:37:43 +03:00
ReadFromStream (str);
if (!str)
{
LogPrint (eLogError, "RouterInfo: Malformed message");
2016-09-13 04:37:43 +03:00
m_IsUnreachable = true;
}
2018-01-06 06:48:51 +03:00
}
2013-10-27 19:28:23 +04:00
void RouterInfo::ReadFromStream (std::istream& s)
{
2021-10-12 20:28:16 +03:00
if (!s) return;
2023-03-04 04:21:56 +03:00
m_Caps = 0; m_Congestion = eLowCongestion;
2013-10-27 19:28:23 +04:00
s.read ((char *)&m_Timestamp, sizeof (m_Timestamp));
m_Timestamp = be64toh (m_Timestamp);
// read addresses
auto addresses = NewAddresses ();
2013-10-27 19:28:23 +04:00
uint8_t numAddresses;
s.read ((char *)&numAddresses, sizeof (numAddresses));
2013-10-27 19:28:23 +04:00
for (int i = 0; i < numAddresses; i++)
{
uint8_t supportedTransports = 0;
auto address = NewAddress ();
2021-04-07 20:05:38 +03:00
uint8_t cost; // ignore
s.read ((char *)&cost, sizeof (cost));
2016-12-14 21:54:16 +03:00
s.read ((char *)&address->date, sizeof (address->date));
2022-11-23 02:28:56 +03:00
bool isHost = false, isStaticKey = false, isV2 = false;
char transportStyle[6];
ReadString (transportStyle, 6, s);
2018-06-15 19:52:43 +03:00
if (!strncmp (transportStyle, "NTCP", 4)) // NTCP or NTCP2
2022-11-23 23:45:00 +03:00
address->transportStyle = eTransportNTCP2;
2022-03-12 00:17:44 +03:00
else if (!strncmp (transportStyle, "SSU", 3)) // SSU or SSU2
2018-01-06 06:48:51 +03:00
{
2022-11-23 02:28:56 +03:00
address->transportStyle = eTransportSSU2;
address->ssu.reset (new SSUExt ());
address->ssu->mtu = 0;
2018-01-06 06:48:51 +03:00
}
2013-10-27 19:28:23 +04:00
else
2016-12-14 21:54:16 +03:00
address->transportStyle = eTransportUnknown;
2021-02-23 05:04:26 +03:00
address->caps = 0;
2016-12-14 21:54:16 +03:00
address->port = 0;
2013-10-27 19:28:23 +04:00
uint16_t size, r = 0;
2016-02-02 19:55:38 +03:00
s.read ((char *)&size, sizeof (size)); if (!s) return;
2013-10-27 19:28:23 +04:00
size = be16toh (size);
2022-03-14 22:54:55 +03:00
if (address->transportStyle == eTransportUnknown)
{
// skip unknown address
s.seekg (size, std::ios_base::cur);
if (s) continue; else return;
}
2013-10-27 19:28:23 +04:00
while (r < size)
{
2016-08-15 20:12:56 +03:00
char key[255], value[255];
r += ReadString (key, 255, s);
2013-10-27 19:28:23 +04:00
s.seekg (1, std::ios_base::cur); r++; // =
2018-01-06 06:48:51 +03:00
r += ReadString (value, 255, s);
2013-10-27 19:28:23 +04:00
s.seekg (1, std::ios_base::cur); r++; // ;
2016-09-12 18:39:33 +03:00
if (!s) return;
2013-10-27 19:28:23 +04:00
if (!strcmp (key, "host"))
2018-01-06 06:48:51 +03:00
{
2014-01-22 03:01:11 +04:00
boost::system::error_code ecode;
2016-12-14 21:54:16 +03:00
address->host = boost::asio::ip::address::from_string (value, ecode);
if (!ecode && !address->host.is_unspecified ())
{
if (!i2p::util::net::IsInReservedRange (address->host) ||
i2p::util::net::IsYggdrasilAddress (address->host))
isHost = true;
else
// we consider such address as invalid
address->transportStyle = eTransportUnknown;
}
2018-01-06 06:48:51 +03:00
}
2013-10-27 19:28:23 +04:00
else if (!strcmp (key, "port"))
{
try
{
2022-10-07 01:48:17 +03:00
address->port = boost::lexical_cast<int>(value);
}
catch (std::exception& ex)
{
LogPrint (eLogWarning, "RouterInfo: 'port' exception ", ex.what ());
}
}
2014-09-15 01:57:47 +04:00
else if (!strcmp (key, "mtu"))
{
if (address->ssu)
2022-10-07 01:48:17 +03:00
{
try
{
2022-10-07 01:48:17 +03:00
address->ssu->mtu = boost::lexical_cast<int>(value);
}
catch (std::exception& ex)
{
LogPrint (eLogWarning, "RouterInfo: 'mtu' exception ", ex.what ());
}
}
else
2022-10-07 01:48:17 +03:00
LogPrint (eLogWarning, "RouterInfo: Unexpected field 'mtu' for NTCP2");
2018-01-06 06:48:51 +03:00
}
2014-04-09 05:56:34 +04:00
else if (!strcmp (key, "caps"))
2021-02-23 06:53:25 +03:00
address->caps = ExtractAddressCaps (value);
2022-03-12 00:17:44 +03:00
else if (!strcmp (key, "s")) // ntcp2 or ssu2 static key
2018-06-06 18:51:34 +03:00
{
2022-02-06 01:14:25 +03:00
Base64ToByteStream (value, strlen (value), address->s, 32);
isStaticKey = true;
}
2022-03-12 00:17:44 +03:00
else if (!strcmp (key, "i")) // ntcp2 iv or ssu2 intro
2018-06-06 18:51:34 +03:00
{
if (address->IsNTCP2 ())
2022-03-18 20:33:33 +03:00
{
Base64ToByteStream (value, strlen (value), address->i, 16);
address->published = true; // presence of "i" means "published" NTCP2
2022-03-18 20:33:33 +03:00
}
else if (address->IsSSU2 ())
2022-03-18 20:33:33 +03:00
Base64ToByteStream (value, strlen (value), address->i, 32);
}
2022-03-20 00:34:07 +03:00
else if (!strcmp (key, "v"))
{
if (!strcmp (value, "2"))
isV2 = true;
else
LogPrint (eLogWarning, "RouterInfo: Unexpected value ", value, " for v");
}
2014-02-21 01:15:12 +04:00
else if (key[0] == 'i')
2018-01-06 06:48:51 +03:00
{
2014-02-21 01:15:12 +04:00
// introducers
2019-05-11 14:27:34 +03:00
if (!address->ssu)
{
LogPrint (eLogError, "RouterInfo: Introducer is presented for non-SSU address. Skipped");
continue;
}
2018-01-06 06:48:51 +03:00
size_t l = strlen(key);
2014-02-24 05:48:28 +04:00
unsigned char index = key[l-1] - '0'; // TODO:
2014-02-21 01:15:12 +04:00
key[l-1] = 0;
2016-09-12 18:39:33 +03:00
if (index > 9)
{
LogPrint (eLogError, "RouterInfo: Unexpected introducer's index ", index, " skipped");
if (s) continue; else return;
}
if (index >= address->ssu->introducers.size ())
{
2021-10-12 20:28:16 +03:00
if (address->ssu->introducers.empty ()) // first time
address->ssu->introducers.reserve (3);
2018-01-06 06:48:51 +03:00
address->ssu->introducers.resize (index + 1);
}
Introducer& introducer = address->ssu->introducers.at (index);
2023-03-02 06:05:24 +03:00
if (!strcmp (key, "itag"))
2022-10-07 01:48:17 +03:00
{
try
{
2022-10-07 01:48:17 +03:00
introducer.iTag = boost::lexical_cast<uint32_t>(value);
}
2022-10-07 01:48:17 +03:00
catch (std::exception& ex)
{
LogPrint (eLogWarning, "RouterInfo: 'itag' exception ", ex.what ());
}
2022-10-07 01:48:17 +03:00
}
2022-11-23 21:44:03 +03:00
else if (!strcmp (key, "ih"))
Base64ToByteStream (value, strlen (value), introducer.iH, 32);
2017-05-24 19:49:36 +03:00
else if (!strcmp (key, "iexp"))
2022-10-07 01:48:17 +03:00
{
try
{
2022-10-07 01:48:17 +03:00
introducer.iExp = boost::lexical_cast<uint32_t>(value);
}
catch (std::exception& ex)
{
LogPrint (eLogWarning, "RouterInfo: 'iexp' exception ", ex.what ());
}
}
2014-02-21 01:15:12 +04:00
}
2016-02-02 19:55:38 +03:00
if (!s) return;
2018-01-06 06:48:51 +03:00
}
2023-05-01 22:28:32 +03:00
2023-05-04 15:20:38 +03:00
if ((address->s[31] & 0x80) || !i2p::data::CheckStaticKey(address->s, GetIdentHash()))
2023-05-01 22:28:32 +03:00
continue; // skip address
2022-11-23 23:45:00 +03:00
if (address->transportStyle == eTransportNTCP2)
{
2021-01-31 02:32:17 +03:00
if (isStaticKey)
{
if (isHost && address->port)
2021-01-31 02:32:17 +03:00
{
if (address->host.is_v6 ())
supportedTransports |= (i2p::util::net::IsYggdrasilAddress (address->host) ? eNTCP2V6Mesh : eNTCP2V6);
2021-01-31 02:32:17 +03:00
else
supportedTransports |= eNTCP2V4;
2021-06-02 19:55:08 +03:00
m_ReachableTransports |= supportedTransports;
}
else
{
address->published = false;
if (address->caps)
{
2021-03-06 05:53:19 +03:00
if (address->caps & AddressCaps::eV4) supportedTransports |= eNTCP2V4;
if (address->caps & AddressCaps::eV6) supportedTransports |= eNTCP2V6;
}
else
supportedTransports |= eNTCP2V4; // most likely, since we don't have host
}
2021-01-31 02:32:17 +03:00
}
}
2022-11-23 02:28:56 +03:00
else if (address->transportStyle == eTransportSSU2 && isV2)
2022-03-12 00:17:44 +03:00
{
2022-03-17 04:11:48 +03:00
if (address->IsV4 ()) supportedTransports |= eSSU2V4;
if (address->IsV6 ()) supportedTransports |= eSSU2V6;
if (isHost && address->port)
2022-03-17 04:11:48 +03:00
{
if (address->host.is_v4 ()) m_ReachableTransports |= eSSU2V4;
if (address->host.is_v6 ()) m_ReachableTransports |= eSSU2V6;
2022-11-23 02:28:56 +03:00
address->published = true;
}
2022-11-23 02:28:56 +03:00
if (address->ssu && !address->ssu->introducers.empty ())
2022-06-24 01:23:25 +03:00
{
2022-11-23 02:28:56 +03:00
// exclude invalid introducers
2022-06-24 01:23:25 +03:00
uint32_t ts = i2p::util::GetSecondsSinceEpoch ();
UpdateIntroducers (address, ts);
if (!address->ssu->introducers.empty ()) // still has something
2022-11-23 02:28:56 +03:00
m_ReachableTransports |= supportedTransports;
}
}
if (supportedTransports)
{
if (!(m_SupportedTransports & supportedTransports)) // avoid duplicates
{
for (uint8_t i = 0; i < eNumTransports; i++)
if ((1 << i) & supportedTransports)
(*addresses)[i] = address;
}
m_SupportedTransports |= supportedTransports;
}
2018-01-06 06:48:51 +03:00
}
2022-11-29 03:16:21 +03:00
// update addresses
2018-01-06 06:48:51 +03:00
#if (BOOST_VERSION >= 105300)
2022-12-07 22:08:27 +03:00
boost::atomic_store (&m_Addresses, addresses);
2016-09-24 15:29:08 +03:00
#else
2022-12-07 22:08:27 +03:00
m_Addresses = addresses; // race condition
2018-01-06 06:48:51 +03:00
#endif
2013-10-27 19:28:23 +04:00
// read peers
uint8_t numPeers;
2016-02-02 19:55:38 +03:00
s.read ((char *)&numPeers, sizeof (numPeers)); if (!s) return;
2013-10-27 19:28:23 +04:00
s.seekg (numPeers*32, std::ios_base::cur); // TODO: read peers
// read properties
m_Version = 0;
bool isNetId = false;
2022-03-24 22:50:20 +03:00
std::string family;
2013-10-27 19:28:23 +04:00
uint16_t size, r = 0;
2016-02-02 19:55:38 +03:00
s.read ((char *)&size, sizeof (size)); if (!s) return;
2013-10-27 19:28:23 +04:00
size = be16toh (size);
while (r < size)
{
2018-01-06 06:48:51 +03:00
char key[255], value[255];
2016-08-15 20:12:56 +03:00
r += ReadString (key, 255, s);
2013-10-27 19:28:23 +04:00
s.seekg (1, std::ios_base::cur); r++; // =
2018-01-06 06:48:51 +03:00
r += ReadString (value, 255, s);
2013-10-27 19:28:23 +04:00
s.seekg (1, std::ios_base::cur); r++; // ;
2016-09-12 18:39:33 +03:00
if (!s) return;
2022-01-16 02:54:02 +03:00
SetProperty (key, value);
2018-01-06 06:48:51 +03:00
// extract caps
2014-03-20 01:27:37 +04:00
if (!strcmp (key, "caps"))
2021-02-23 06:53:25 +03:00
ExtractCaps (value);
// extract version
else if (!strcmp (key, ROUTER_INFO_PROPERTY_VERSION))
{
m_Version = 0;
char * ch = value;
while (*ch)
{
if (*ch >= '0' && *ch <= '9')
{
m_Version *= 10;
m_Version += (*ch - '0');
}
ch++;
}
}
2016-01-16 23:36:30 +03:00
// check netId
else if (!strcmp (key, ROUTER_INFO_PROPERTY_NETID))
2016-01-16 23:36:30 +03:00
{
isNetId = true;
if (atoi (value) != i2p::context.GetNetID ())
{
LogPrint (eLogError, "RouterInfo: Unexpected ", ROUTER_INFO_PROPERTY_NETID, "=", value);
m_IsUnreachable = true;
}
2018-01-06 06:48:51 +03:00
}
// family
2016-02-21 04:20:19 +03:00
else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY))
{
2022-03-24 22:50:20 +03:00
family = value;
boost::to_lower (family);
}
2016-02-21 04:20:19 +03:00
else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY_SIG))
{
2022-03-24 22:50:20 +03:00
if (netdb.GetFamilies ().VerifyFamily (family, GetIdentHash (), value))
m_FamilyID = netdb.GetFamilies ().GetFamilyID (family);
else
2022-03-24 22:50:20 +03:00
LogPrint (eLogWarning, "RouterInfo: Family ", family, " signature verification failed");
2018-01-06 06:48:51 +03:00
}
2016-02-02 19:55:38 +03:00
if (!s) return;
2018-01-06 06:48:51 +03:00
}
if (!m_SupportedTransports || !isNetId || !m_Version)
SetUnreachable (true);
2016-06-17 18:03:33 +03:00
}
2022-03-24 22:50:20 +03:00
bool RouterInfo::IsFamily (FamilyID famid) const
{
2022-03-24 22:50:20 +03:00
return m_FamilyID == famid;
}
2013-12-29 19:48:57 +04:00
2021-02-23 06:53:25 +03:00
void RouterInfo::ExtractCaps (const char * value)
2014-03-19 20:02:51 +04:00
{
const char * cap = value;
while (*cap)
{
switch (*cap)
{
2014-09-03 00:11:31 +04:00
case CAPS_FLAG_FLOODFILL:
2021-02-23 06:53:25 +03:00
m_Caps |= Caps::eFloodfill;
2014-03-19 20:02:51 +04:00
break;
2014-09-03 00:11:31 +04:00
case CAPS_FLAG_HIGH_BANDWIDTH1:
case CAPS_FLAG_HIGH_BANDWIDTH2:
case CAPS_FLAG_HIGH_BANDWIDTH3:
2021-02-23 06:53:25 +03:00
m_Caps |= Caps::eHighBandwidth;
2014-03-19 20:02:51 +04:00
break;
2016-01-03 06:17:04 +03:00
case CAPS_FLAG_EXTRA_BANDWIDTH1:
case CAPS_FLAG_EXTRA_BANDWIDTH2:
2021-02-23 06:53:25 +03:00
m_Caps |= Caps::eExtraBandwidth | Caps::eHighBandwidth;
2018-01-06 06:48:51 +03:00
break;
2014-09-03 00:11:31 +04:00
case CAPS_FLAG_HIDDEN:
2021-02-23 06:53:25 +03:00
m_Caps |= Caps::eHidden;
2018-01-06 06:48:51 +03:00
break;
2014-09-03 00:11:31 +04:00
case CAPS_FLAG_REACHABLE:
2021-02-23 06:53:25 +03:00
m_Caps |= Caps::eReachable;
2014-03-19 20:02:51 +04:00
break;
2014-09-03 00:11:31 +04:00
case CAPS_FLAG_UNREACHABLE:
2021-02-23 06:53:25 +03:00
m_Caps |= Caps::eUnreachable;
2018-01-06 06:48:51 +03:00
break;
2023-03-04 23:46:44 +03:00
case CAPS_FLAG_MEDIUM_CONGESTION:
2023-03-04 04:21:56 +03:00
m_Congestion = eMediumCongestion;
break;
2023-03-04 23:46:44 +03:00
case CAPS_FLAG_HIGH_CONGESTION:
2023-03-04 04:21:56 +03:00
m_Congestion = eHighCongestion;
break;
2023-03-04 23:46:44 +03:00
case CAPS_FLAG_REJECT_ALL_CONGESTION:
2023-03-04 04:21:56 +03:00
m_Congestion = eRejectAll;
break;
2021-02-23 06:53:25 +03:00
default: ;
}
cap++;
}
}
uint8_t RouterInfo::ExtractAddressCaps (const char * value) const
{
uint8_t caps = 0;
const char * cap = value;
while (*cap)
{
switch (*cap)
{
case CAPS_FLAG_V4:
caps |= AddressCaps::eV4;
break;
2021-02-23 06:53:25 +03:00
case CAPS_FLAG_V6:
caps |= AddressCaps::eV6;
break;
2022-11-23 23:45:00 +03:00
case CAPS_FLAG_SSU2_TESTING:
2021-02-23 06:53:25 +03:00
caps |= AddressCaps::eSSUTesting;
2018-01-06 06:48:51 +03:00
break;
2022-11-23 23:45:00 +03:00
case CAPS_FLAG_SSU2_INTRODUCER:
2021-02-23 06:53:25 +03:00
caps |= AddressCaps::eSSUIntroducer;
2018-01-06 06:48:51 +03:00
break;
2014-03-19 20:02:51 +04:00
default: ;
2018-01-06 06:48:51 +03:00
}
2014-03-19 20:02:51 +04:00
cap++;
}
2021-02-23 05:04:26 +03:00
return caps;
2014-03-19 20:02:51 +04:00
}
void RouterInfo::UpdateIntroducers (std::shared_ptr<Address> address, uint64_t ts)
{
if (!address || !address->ssu) return;
int numValid = 0;
for (auto& it: address->ssu->introducers)
{
if (it.iTag && ts < it.iExp)
numValid++;
else
it.iTag = 0;
}
if (!numValid)
address->ssu->introducers.resize (0);
}
2016-02-17 23:36:55 +03:00
bool RouterInfo::IsNewer (const uint8_t * buf, size_t len) const
{
if (!m_RouterIdentity) return false;
size_t size = m_RouterIdentity->GetFullLen ();
if (size + 8 > len) return false;
2018-01-06 06:48:51 +03:00
return bufbe64toh (buf + size) > m_Timestamp;
2016-02-17 23:36:55 +03:00
}
2021-10-07 22:08:33 +03:00
const uint8_t * RouterInfo::LoadBuffer (const std::string& fullPath)
2014-07-23 18:56:41 +04:00
{
if (!m_Buffer)
{
2021-10-07 22:08:33 +03:00
if (LoadFile (fullPath))
2015-12-18 17:07:50 +03:00
LogPrint (eLogDebug, "RouterInfo: Buffer for ", GetIdentHashAbbreviation (GetIdentHash ()), " loaded from file");
else
return nullptr;
2018-01-06 06:48:51 +03:00
}
2021-12-30 23:16:13 +03:00
return m_Buffer->data ();
2014-07-23 18:56:41 +04:00
}
bool RouterInfo::SaveToFile (const std::string& fullPath)
2014-07-10 23:33:42 +04:00
{
2023-02-12 23:28:06 +03:00
if (m_IsUnreachable) return false; // don't save bad router
if (!m_Buffer)
2021-10-07 22:08:33 +03:00
{
2023-02-12 23:28:06 +03:00
LogPrint (eLogWarning, "RouterInfo: Can't save, m_Buffer == NULL");
return false;
}
std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);
2023-02-12 23:28:06 +03:00
if (!f.is_open ())
{
LogPrint (eLogError, "RouterInfo: Can't save to ", fullPath);
return false;
}
2021-12-30 23:16:13 +03:00
f.write ((char *)m_Buffer->data (), m_BufferLen);
return true;
2014-07-10 23:33:42 +04:00
}
2018-01-06 06:48:51 +03:00
2016-08-15 20:12:56 +03:00
size_t RouterInfo::ReadString (char * str, size_t len, std::istream& s) const
2013-10-27 19:28:23 +04:00
{
2016-08-15 20:12:56 +03:00
uint8_t l;
s.read ((char *)&l, 1);
if (l < len)
2018-01-06 06:48:51 +03:00
{
2016-08-15 20:12:56 +03:00
s.read (str, l);
2016-09-12 18:39:33 +03:00
if (!s) l = 0; // failed, return empty string
2016-08-15 20:12:56 +03:00
str[l] = 0;
}
else
{
LogPrint (eLogWarning, "RouterInfo: String length ", (int)l, " exceeds buffer size ", len);
2016-08-15 20:12:56 +03:00
s.seekg (l, std::ios::cur); // skip
str[0] = 0;
2018-01-06 06:48:51 +03:00
}
2016-08-15 20:12:56 +03:00
return l+1;
2018-01-06 06:48:51 +03:00
}
2013-10-27 19:28:23 +04:00
void RouterInfo::AddNTCP2Address (const uint8_t * staticKey, const uint8_t * iv,int port, uint8_t caps)
{
auto addr = std::make_shared<Address>();
addr->port = port;
addr->transportStyle = eTransportNTCP2;
addr->caps = caps;
addr->date = 0;
addr->published = false;
memcpy (addr->s, staticKey, 32);
memcpy (addr->i, iv, 16);
if (addr->IsV4 ())
{
m_SupportedTransports |= eNTCP2V4;
(*m_Addresses)[eNTCP2V4Idx] = addr;
}
if (addr->IsV6 ())
{
m_SupportedTransports |= eNTCP2V6;
(*m_Addresses)[eNTCP2V6Idx] = addr;
}
}
void RouterInfo::AddNTCP2Address (const uint8_t * staticKey, const uint8_t * iv,
const boost::asio::ip::address& host, int port)
2018-06-15 19:52:43 +03:00
{
auto addr = std::make_shared<Address>();
2018-08-27 23:01:47 +03:00
addr->host = host;
addr->port = port;
2022-11-23 23:45:00 +03:00
addr->transportStyle = eTransportNTCP2;
2018-06-15 19:52:43 +03:00
addr->date = 0;
addr->published = true;
2022-02-06 01:14:25 +03:00
memcpy (addr->s, staticKey, 32);
memcpy (addr->i, iv, 16);
2023-02-18 04:08:05 +03:00
addr->caps = 0;
if (host.is_unspecified ())
{
if (host.is_v4 ()) addr->caps |= eV4;
if (host.is_v6 ()) addr->caps |= eV6;
}
if (addr->IsV4 ())
{
m_SupportedTransports |= eNTCP2V4;
m_ReachableTransports |= eNTCP2V4;
(*m_Addresses)[eNTCP2V4Idx] = addr;
}
if (addr->IsV6 ())
{
if (i2p::util::net::IsYggdrasilAddress (addr->host))
{
m_SupportedTransports |= eNTCP2V6Mesh;
m_ReachableTransports |= eNTCP2V6Mesh;
(*m_Addresses)[eNTCP2V6MeshIdx] = addr;
}
else
{
m_SupportedTransports |= eNTCP2V6;
m_ReachableTransports |= eNTCP2V6;
(*m_Addresses)[eNTCP2V6Idx] = addr;
}
}
2018-06-15 19:52:43 +03:00
}
void RouterInfo::RemoveNTCP2Address (bool v4)
{
if (v4)
{
if ((*m_Addresses)[eNTCP2V6Idx])
(*m_Addresses)[eNTCP2V6Idx]->caps &= ~AddressCaps::eV4;
(*m_Addresses)[eNTCP2V4Idx].reset ();
}
else
{
if ((*m_Addresses)[eNTCP2V4Idx])
(*m_Addresses)[eNTCP2V4Idx]->caps &= ~AddressCaps::eV6;
(*m_Addresses)[eNTCP2V6Idx].reset ();
}
UpdateSupportedTransports ();
}
void RouterInfo::AddSSU2Address (const uint8_t * staticKey, const uint8_t * introKey, int port, uint8_t caps)
2022-03-13 05:40:12 +03:00
{
auto addr = std::make_shared<Address>();
addr->transportStyle = eTransportSSU2;
addr->port = port;
2022-03-13 18:04:37 +03:00
addr->caps = caps;
2022-03-13 05:40:12 +03:00
addr->date = 0;
2022-04-27 03:01:32 +03:00
addr->ssu.reset (new SSUExt ());
addr->ssu->mtu = 0;
2022-03-13 05:40:12 +03:00
memcpy (addr->s, staticKey, 32);
memcpy (addr->i, introKey, 32);
if (addr->IsV4 ())
{
m_SupportedTransports |= eSSU2V4;
(*m_Addresses)[eSSU2V4Idx] = addr;
}
if (addr->IsV6 ())
{
m_SupportedTransports |= eSSU2V6;
(*m_Addresses)[eSSU2V6Idx] = addr;
}
}
2022-03-29 20:56:56 +03:00
void RouterInfo::AddSSU2Address (const uint8_t * staticKey, const uint8_t * introKey,
const boost::asio::ip::address& host, int port)
{
auto addr = std::make_shared<Address>();
addr->transportStyle = eTransportSSU2;
addr->host = host;
addr->port = port;
addr->published = true;
addr->date = 0;
2022-04-27 03:01:32 +03:00
addr->ssu.reset (new SSUExt ());
addr->ssu->mtu = 0;
2022-03-29 20:56:56 +03:00
memcpy (addr->s, staticKey, 32);
memcpy (addr->i, introKey, 32);
2023-02-18 04:08:05 +03:00
if (!host.is_unspecified ())
addr->caps = i2p::data::RouterInfo::eSSUTesting | i2p::data::RouterInfo::eSSUIntroducer; // BC;
else
{
addr->caps = 0;
if (host.is_v4 ()) addr->caps |= eV4;
if (host.is_v6 ()) addr->caps |= eV6;
}
2022-03-29 20:56:56 +03:00
if (addr->IsV4 ())
{
2022-03-29 20:56:56 +03:00
m_SupportedTransports |= eSSU2V4;
m_ReachableTransports |= eSSU2V4;
(*m_Addresses)[eSSU2V4Idx] = addr;
}
2022-03-29 20:56:56 +03:00
if (addr->IsV6 ())
{
2022-03-29 20:56:56 +03:00
m_SupportedTransports |= eSSU2V6;
m_ReachableTransports |= eSSU2V6;
(*m_Addresses)[eSSU2V6Idx] = addr;
}
}
void RouterInfo::RemoveSSU2Address (bool v4)
{
if (v4)
{
if ((*m_Addresses)[eSSU2V6Idx])
(*m_Addresses)[eSSU2V6Idx]->caps &= ~AddressCaps::eV4;
(*m_Addresses)[eSSU2V4Idx].reset ();
}
else
{
if ((*m_Addresses)[eSSU2V4Idx])
(*m_Addresses)[eSSU2V4Idx]->caps &= ~AddressCaps::eV6;
(*m_Addresses)[eSSU2V6Idx].reset ();
}
UpdateSupportedTransports ();
}
2018-06-06 18:51:34 +03:00
bool RouterInfo::IsNTCP2 (bool v4only) const
{
if (v4only)
return m_SupportedTransports & eNTCP2V4;
else
return m_SupportedTransports & (eNTCP2V4 | eNTCP2V6);
}
void RouterInfo::EnableV6 ()
{
if (!IsV6 ())
{
uint8_t addressCaps = AddressCaps::eV6;
if (IsV4 ()) addressCaps |= AddressCaps::eV4;
SetUnreachableAddressesTransportCaps (addressCaps);
2021-06-17 01:14:33 +03:00
UpdateSupportedTransports ();
}
}
2016-03-25 01:44:41 +03:00
void RouterInfo::EnableV4 ()
2016-03-25 01:44:41 +03:00
{
if (!IsV4 ())
{
uint8_t addressCaps = AddressCaps::eV4;
if (IsV6 ()) addressCaps |= AddressCaps::eV6;
SetUnreachableAddressesTransportCaps (addressCaps);
2021-06-17 01:14:33 +03:00
UpdateSupportedTransports ();
}
2016-03-25 01:44:41 +03:00
}
2018-01-06 06:48:51 +03:00
void RouterInfo::DisableV6 ()
2018-01-06 06:48:51 +03:00
{
if (IsV6 ())
2018-01-06 06:48:51 +03:00
{
if ((*m_Addresses)[eNTCP2V6Idx])
{
if ((*m_Addresses)[eNTCP2V6Idx]->IsV4 () && (*m_Addresses)[eNTCP2V4Idx])
(*m_Addresses)[eNTCP2V4Idx]->caps &= ~AddressCaps::eV6;
(*m_Addresses)[eNTCP2V6Idx].reset ();
}
if ((*m_Addresses)[eSSU2V6Idx])
{
if ((*m_Addresses)[eSSU2V6Idx]->IsV4 () && (*m_Addresses)[eSSU2V4Idx])
(*m_Addresses)[eSSU2V4Idx]->caps &= ~AddressCaps::eV6;
(*m_Addresses)[eSSU2V6Idx].reset ();
}
2021-06-17 01:14:33 +03:00
UpdateSupportedTransports ();
2018-01-06 06:48:51 +03:00
}
}
2016-03-25 01:44:41 +03:00
void RouterInfo::DisableV4 ()
2018-01-06 06:48:51 +03:00
{
2016-03-25 01:44:41 +03:00
if (IsV4 ())
2018-01-06 06:48:51 +03:00
{
if ((*m_Addresses)[eNTCP2V4Idx])
2016-03-25 01:44:41 +03:00
{
if ((*m_Addresses)[eNTCP2V4Idx]->IsV6 () && (*m_Addresses)[eNTCP2V6Idx])
(*m_Addresses)[eNTCP2V6Idx]->caps &= ~AddressCaps::eV4;
(*m_Addresses)[eNTCP2V4Idx].reset ();
}
if ((*m_Addresses)[eSSU2V4Idx])
{
if ((*m_Addresses)[eSSU2V4Idx]->IsV6 () && (*m_Addresses)[eSSU2V6Idx])
(*m_Addresses)[eSSU2V6Idx]->caps &= ~AddressCaps::eV4;
(*m_Addresses)[eSSU2V4Idx].reset ();
}
2021-06-17 01:14:33 +03:00
UpdateSupportedTransports ();
2018-01-06 06:48:51 +03:00
}
2016-03-25 01:44:41 +03:00
}
2018-01-06 06:48:51 +03:00
2021-02-01 01:25:07 +03:00
void RouterInfo::EnableMesh ()
{
if (!IsMesh ())
{
2021-02-01 01:25:07 +03:00
m_SupportedTransports |= eNTCP2V6Mesh;
2021-06-17 01:14:33 +03:00
m_ReachableTransports |= eNTCP2V6Mesh;
}
2021-02-01 01:25:07 +03:00
}
2021-02-01 01:25:07 +03:00
void RouterInfo::DisableMesh ()
{
2021-02-01 01:25:07 +03:00
if (IsMesh ())
{
m_SupportedTransports &= ~eNTCP2V6Mesh;
2021-06-17 01:14:33 +03:00
m_ReachableTransports &= ~eNTCP2V6Mesh;
(*m_Addresses)[eNTCP2V6MeshIdx].reset ();
}
}
2018-01-06 06:48:51 +03:00
2022-03-17 04:11:48 +03:00
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetSSU2V4Address () const
{
return (*GetAddresses ())[eSSU2V4Idx];
}
2022-03-17 04:11:48 +03:00
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetSSU2V6Address () const
{
return (*GetAddresses ())[eSSU2V6Idx];
}
2022-06-07 23:09:20 +03:00
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetSSU2Address (bool v4) const
{
if (v4)
{
2022-06-07 23:09:20 +03:00
if (m_SupportedTransports & eSSU2V4)
return GetSSU2V4Address ();
}
else
{
if (m_SupportedTransports & eSSU2V6)
return GetSSU2V6Address ();
}
2022-06-07 23:09:20 +03:00
return nullptr;
}
boost::shared_ptr<RouterInfo::Addresses> RouterInfo::GetAddresses () const
{
#if (BOOST_VERSION >= 105300)
return boost::atomic_load (&m_Addresses);
#else
return m_Addresses;
#endif
}
template<typename Filter>
2018-08-04 15:47:58 +03:00
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetAddress (Filter filter) const
2013-10-27 19:28:23 +04:00
{
2019-04-08 22:22:42 +03:00
// TODO: make it more generic using comparator
2016-10-01 22:05:35 +03:00
#if (BOOST_VERSION >= 105300)
auto addresses = boost::atomic_load (&m_Addresses);
2018-01-06 06:48:51 +03:00
#else
2016-08-10 03:51:54 +03:00
auto addresses = m_Addresses;
2018-01-06 06:48:51 +03:00
#endif
2016-08-10 03:51:54 +03:00
for (const auto& address : *addresses)
if (address && filter (address)) return address;
2013-10-27 19:28:23 +04:00
return nullptr;
2018-01-06 06:48:51 +03:00
}
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetNTCP2V4Address () const
2018-07-31 22:41:13 +03:00
{
return (*GetAddresses ())[eNTCP2V4Idx];
2018-07-31 22:41:13 +03:00
}
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetNTCP2V6Address () const
2022-03-23 21:06:55 +03:00
{
return (*GetAddresses ())[eNTCP2V6Idx];
2022-03-23 21:06:55 +03:00
}
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetPublishedNTCP2V4Address () const
{
auto addr = (*GetAddresses ())[eNTCP2V4Idx];
if (addr && addr->IsPublishedNTCP2 ()) return addr;
return nullptr;
}
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetPublishedNTCP2V6Address () const
{
auto addr = (*GetAddresses ())[eNTCP2V6Idx];
if (addr && addr->IsPublishedNTCP2 ()) return addr;
return nullptr;
}
2021-02-01 01:25:07 +03:00
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetYggdrasilAddress () const
{
return (*GetAddresses ())[eNTCP2V6MeshIdx];
}
2018-01-06 06:48:51 +03:00
std::shared_ptr<RouterProfile> RouterInfo::GetProfile () const
{
2023-02-17 18:34:14 +03:00
auto profile = m_Profile;
if (!profile)
{
profile = GetRouterProfile (GetIdentHash ());
m_Profile = profile;
}
return profile;
2018-01-06 06:48:51 +03:00
}
void RouterInfo::Encrypt (const uint8_t * data, uint8_t * encrypted) const
{
auto encryptor = m_RouterIdentity->CreateEncryptor (nullptr);
if (encryptor)
encryptor->Encrypt (data, encrypted);
}
2020-11-23 20:49:18 +03:00
bool RouterInfo::IsEligibleFloodfill () const
2020-11-23 20:49:18 +03:00
{
// floodfill must have published ipv4, >= 0.9.38 and not DSA
return m_Version >= NETDB_MIN_FLOODFILL_VERSION && IsPublished (true) &&
GetIdentity ()->GetSigningKeyType () != SIGNING_KEY_TYPE_DSA_SHA1;
}
2021-02-23 05:04:26 +03:00
bool RouterInfo::IsPublished (bool v4) const
{
auto addr = GetAddresses ();
if (v4)
2023-04-06 23:19:56 +03:00
return ((*addr)[eNTCP2V4Idx] && ((*addr)[eNTCP2V4Idx])->published) ||
((*addr)[eSSU2V4Idx] && ((*addr)[eSSU2V4Idx])->published);
else
2023-04-06 23:19:56 +03:00
return ((*addr)[eNTCP2V6Idx] && ((*addr)[eNTCP2V6Idx])->published) ||
((*addr)[eSSU2V6Idx] && ((*addr)[eSSU2V6Idx])->published);
}
2022-06-02 04:51:02 +03:00
bool RouterInfo::IsSSU2PeerTesting (bool v4) const
{
if (!(m_SupportedTransports & (v4 ? eSSU2V4 : eSSU2V6))) return false;
auto addr = (*GetAddresses ())[v4 ? eSSU2V4Idx : eSSU2V6Idx];
return addr && addr->IsPeerTesting () && addr->IsReachableSSU ();
}
2022-07-20 01:38:58 +03:00
bool RouterInfo::IsSSU2Introducer (bool v4) const
{
if (!(m_SupportedTransports & (v4 ? eSSU2V4 : eSSU2V6))) return false;
auto addr = (*GetAddresses ())[v4 ? eSSU2V4Idx : eSSU2V6Idx];
return addr && addr->IsIntroducer () && !addr->host.is_unspecified () && addr->port;
2022-07-20 01:38:58 +03:00
}
void RouterInfo::SetUnreachableAddressesTransportCaps (uint8_t transports)
{
for (auto& addr: *m_Addresses)
{
if (addr && !addr->published)
{
addr->caps &= ~(eV4 | eV6);
addr->caps |= transports;
}
}
}
2021-06-02 19:55:08 +03:00
void RouterInfo::UpdateSupportedTransports ()
{
m_SupportedTransports = 0;
m_ReachableTransports = 0;
for (const auto& addr: *m_Addresses)
{
if (!addr) continue;
2021-06-02 19:55:08 +03:00
uint8_t transports = 0;
2022-06-13 01:26:02 +03:00
switch (addr->transportStyle)
{
2022-11-23 23:45:00 +03:00
case eTransportNTCP2:
2022-06-13 01:26:02 +03:00
if (addr->IsV4 ()) transports |= eNTCP2V4;
if (addr->IsV6 ())
transports |= (i2p::util::net::IsYggdrasilAddress (addr->host) ? eNTCP2V6Mesh : eNTCP2V6);
if (addr->IsPublishedNTCP2 ())
m_ReachableTransports |= transports;
break;
case eTransportSSU2:
if (addr->IsV4 ()) transports |= eSSU2V4;
if (addr->IsV6 ()) transports |= eSSU2V6;
if (addr->IsReachableSSU ())
m_ReachableTransports |= transports;
break;
default: ;
}
2021-06-02 19:55:08 +03:00
m_SupportedTransports |= transports;
}
}
2022-01-15 20:48:49 +03:00
void RouterInfo::UpdateIntroducers (uint64_t ts)
{
if (ts*1000 < m_Timestamp + INTRODUCER_UPDATE_INTERVAL) return;
if (m_ReachableTransports & eSSU2V4)
{
auto addr = (*GetAddresses ())[eSSU2V4Idx];
if (addr && addr->UsesIntroducer ())
{
UpdateIntroducers (addr, ts);
if (!addr->UsesIntroducer ()) // no more valid introducers
m_ReachableTransports &= ~eSSU2V4;
}
}
if (m_ReachableTransports & eSSU2V6)
{
auto addr = (*GetAddresses ())[eSSU2V6Idx];
if (addr && addr->UsesIntroducer ())
{
UpdateIntroducers (addr, ts);
if (!addr->UsesIntroducer ()) // no more valid introducers
m_ReachableTransports &= ~eSSU2V6;
}
}
}
2022-01-15 20:48:49 +03:00
void RouterInfo::UpdateBuffer (const uint8_t * buf, size_t len)
{
if (!m_Buffer)
m_Buffer = NewBuffer ();
2022-01-15 20:48:49 +03:00
if (len > m_Buffer->size ()) len = m_Buffer->size ();
memcpy (m_Buffer->data (), buf, len);
m_BufferLen = len;
}
2022-01-15 20:48:49 +03:00
std::shared_ptr<RouterInfo::Buffer> RouterInfo::NewBuffer () const
{
return netdb.NewRouterInfoBuffer ();
}
std::shared_ptr<RouterInfo::Address> RouterInfo::NewAddress () const
{
return netdb.NewRouterInfoAddress ();
}
boost::shared_ptr<RouterInfo::Addresses> RouterInfo::NewAddresses () const
{
return netdb.NewRouterInfoAddresses ();
}
2023-03-17 04:32:53 +03:00
std::shared_ptr<IdentityEx> RouterInfo::NewIdentity (const uint8_t * buf, size_t len) const
{
return netdb.NewIdentity (buf, len);
}
2022-01-15 20:48:49 +03:00
void RouterInfo::RefreshTimestamp ()
{
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
}
2022-01-15 20:48:49 +03:00
bool RouterInfo::IsHighCongestion (bool highBandwidth) const
2023-03-04 04:21:56 +03:00
{
switch (m_Congestion)
{
case eLowCongestion:
return false;
break;
case eMediumCongestion:
return highBandwidth;
break;
case eHighCongestion:
return i2p::util::GetMillisecondsSinceEpoch () < m_Timestamp + HIGH_CONGESTION_INTERVAL*1000LL;
break;
case eRejectAll:
return true;
break;
default:
return false;
}
2023-03-07 03:48:04 +03:00
}
2023-03-04 04:21:56 +03:00
2022-01-15 20:48:49 +03:00
void LocalRouterInfo::CreateBuffer (const PrivateKeys& privateKeys)
{
RefreshTimestamp ();
std::stringstream s;
uint8_t ident[1024];
auto identLen = privateKeys.GetPublic ()->ToBuffer (ident, 1024);
auto signatureLen = privateKeys.GetPublic ()->GetSignatureLen ();
s.write ((char *)ident, identLen);
WriteToStream (s);
size_t len = s.str ().size ();
if (len + signatureLen < MAX_RI_BUFFER_SIZE)
{
UpdateBuffer ((const uint8_t *)s.str ().c_str (), len);
// signature
privateKeys.Sign (GetBuffer (), len, GetBufferPointer (len));
SetBufferLen (len + signatureLen);
}
else
LogPrint (eLogError, "RouterInfo: Our RouterInfo is too long ", len + signatureLen);
}
2022-01-16 02:54:02 +03:00
void LocalRouterInfo::UpdateCaps (uint8_t caps)
{
SetCaps (caps);
UpdateCapsProperty ();
}
2022-01-16 02:54:02 +03:00
void LocalRouterInfo::UpdateCapsProperty ()
{
std::string caps;
uint8_t c = GetCaps ();
if (c & eFloodfill)
{
if (c & eExtraBandwidth) caps += (c & eHighBandwidth) ?
CAPS_FLAG_EXTRA_BANDWIDTH2 : // 'X'
CAPS_FLAG_EXTRA_BANDWIDTH1; // 'P'
else
caps += CAPS_FLAG_HIGH_BANDWIDTH3; // 'O'
caps += CAPS_FLAG_FLOODFILL; // floodfill
}
else
{
if (c & eExtraBandwidth)
caps += (c & eHighBandwidth) ? CAPS_FLAG_EXTRA_BANDWIDTH2 /* 'X' */ : CAPS_FLAG_EXTRA_BANDWIDTH1; /*'P' */
else
caps += (c & eHighBandwidth) ? CAPS_FLAG_HIGH_BANDWIDTH3 /* 'O' */: CAPS_FLAG_LOW_BANDWIDTH2 /* 'L' */; // bandwidth
}
if (c & eHidden) caps += CAPS_FLAG_HIDDEN; // hidden
if (c & eReachable) caps += CAPS_FLAG_REACHABLE; // reachable
if (c & eUnreachable) caps += CAPS_FLAG_UNREACHABLE; // unreachable
2023-03-04 04:21:56 +03:00
switch (GetCongestion ())
{
case eMediumCongestion:
2023-03-04 23:46:44 +03:00
caps += CAPS_FLAG_MEDIUM_CONGESTION;
2023-03-04 04:21:56 +03:00
break;
case eHighCongestion:
2023-03-04 23:46:44 +03:00
caps += CAPS_FLAG_HIGH_CONGESTION;
2023-03-04 04:21:56 +03:00
break;
case eRejectAll:
2023-03-04 23:46:44 +03:00
caps += CAPS_FLAG_REJECT_ALL_CONGESTION;
2023-03-04 04:21:56 +03:00
break;
default: ;
};
2022-01-16 02:54:02 +03:00
SetProperty ("caps", caps);
}
2023-04-02 18:27:51 +03:00
bool LocalRouterInfo::UpdateCongestion (Congestion c)
2023-03-07 03:48:04 +03:00
{
if (c != GetCongestion ())
{
SetCongestion (c);
UpdateCapsProperty ();
return true;
2023-03-07 03:48:04 +03:00
}
return false;
2023-03-07 03:48:04 +03:00
}
2022-01-16 02:54:02 +03:00
void LocalRouterInfo::WriteToStream (std::ostream& s) const
{
auto addresses = GetAddresses ();
if (!addresses) return;
2022-01-16 02:54:02 +03:00
uint64_t ts = htobe64 (GetTimestamp ());
s.write ((const char *)&ts, sizeof (ts));
// addresses
uint8_t numAddresses = 0;
for (size_t idx = 0; idx < addresses->size(); idx++)
{
auto addr_ptr = (*addresses)[idx];
if (!addr_ptr) continue;
if (idx == eNTCP2V6Idx && addr_ptr == (*addresses)[eNTCP2V4Idx]) continue;
if (idx == eSSU2V6Idx && addr_ptr == (*addresses)[eSSU2V4Idx]) continue;
numAddresses++;
}
2022-01-16 02:54:02 +03:00
s.write ((char *)&numAddresses, sizeof (numAddresses));
for (size_t idx = 0; idx < addresses->size(); idx++)
2022-01-16 02:54:02 +03:00
{
auto addr_ptr = (*addresses)[idx];
if (!addr_ptr) continue;
if (idx == eNTCP2V6Idx && addr_ptr == (*addresses)[eNTCP2V4Idx]) continue;
if (idx == eSSU2V6Idx && addr_ptr == (*addresses)[eSSU2V4Idx]) continue;
2022-01-16 02:54:02 +03:00
const Address& address = *addr_ptr;
// calculate cost
uint8_t cost = 0x7f;
2022-11-23 23:45:00 +03:00
if (address.transportStyle == eTransportNTCP2)
2022-01-16 02:54:02 +03:00
cost = address.published ? COST_NTCP2_PUBLISHED : COST_NTCP2_NON_PUBLISHED;
2022-03-13 05:40:12 +03:00
else if (address.transportStyle == eTransportSSU2)
2022-04-02 21:32:26 +03:00
cost = address.published ? COST_SSU2_DIRECT : COST_SSU2_NON_PUBLISHED;
2022-11-23 23:45:00 +03:00
else
continue; // skip unknown address
2022-01-16 02:54:02 +03:00
s.write ((const char *)&cost, sizeof (cost));
s.write ((const char *)&address.date, sizeof (address.date));
std::stringstream properties;
2023-03-29 20:40:12 +03:00
bool isPublished = address.published && !address.host.is_unspecified () && address.port;
2022-11-23 23:45:00 +03:00
if (address.transportStyle == eTransportNTCP2)
2022-01-16 02:54:02 +03:00
{
2023-03-29 20:40:12 +03:00
WriteString ("NTCP2", s);
// caps
if (!isPublished)
2022-01-16 02:54:02 +03:00
{
2023-03-29 20:40:12 +03:00
WriteString ("caps", properties);
properties << '=';
std::string caps;
if (address.IsV4 ()) caps += CAPS_FLAG_V4;
if (address.IsV6 () || address.host.is_v6 ()) caps += CAPS_FLAG_V6; // we set 6 for unspecified ipv6
if (caps.empty ()) caps += CAPS_FLAG_V4;
WriteString (caps, properties);
properties << ';';
2022-01-16 02:54:02 +03:00
}
}
2022-03-12 00:17:44 +03:00
else if (address.transportStyle == eTransportSSU2)
{
WriteString ("SSU2", s);
// caps
std::string caps;
2023-03-29 20:40:12 +03:00
if (isPublished)
{
2022-11-23 23:45:00 +03:00
if (address.IsPeerTesting ()) caps += CAPS_FLAG_SSU2_TESTING;
if (address.IsIntroducer ()) caps += CAPS_FLAG_SSU2_INTRODUCER;
}
2022-03-29 20:56:56 +03:00
else
{
2022-03-29 20:56:56 +03:00
if (address.IsV4 ()) caps += CAPS_FLAG_V4;
2023-03-29 20:40:12 +03:00
if (address.IsV6 () || address.host.is_v6 ()) caps += CAPS_FLAG_V6; // we set 6 for unspecified ipv6
2022-03-29 20:56:56 +03:00
if (caps.empty ()) caps += CAPS_FLAG_V4;
}
if (!caps.empty ())
{
WriteString ("caps", properties);
properties << '=';
2022-03-29 20:56:56 +03:00
WriteString (caps, properties);
properties << ';';
}
}
2022-01-16 02:54:02 +03:00
else
WriteString ("", s);
2022-07-25 22:23:52 +03:00
if (isPublished && !address.host.is_unspecified ())
2022-01-16 02:54:02 +03:00
{
WriteString ("host", properties);
properties << '=';
WriteString (address.host.to_string (), properties);
properties << ';';
}
2022-04-26 02:57:46 +03:00
if ((address.IsNTCP2 () && isPublished) || address.IsSSU2 ())
{
// publish i for NTCP2 or SSU2
WriteString ("i", properties); properties << '=';
size_t len = address.IsSSU2 () ? 32 : 16;
WriteString (address.i.ToBase64 (len), properties); properties << ';';
}
2022-11-23 21:44:03 +03:00
if (address.transportStyle == eTransportSSU2)
2022-01-16 02:54:02 +03:00
{
// write introducers if any
2022-04-27 03:01:32 +03:00
if (address.ssu && !address.ssu->introducers.empty())
2022-01-16 02:54:02 +03:00
{
int i = 0;
for (const auto& introducer: address.ssu->introducers)
{
if (introducer.iExp) // expiration is specified
{
WriteString ("iexp" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
WriteString (boost::lexical_cast<std::string>(introducer.iExp), properties);
properties << ';';
}
i++;
}
i = 0;
for (const auto& introducer: address.ssu->introducers)
{
WriteString ("ih" + boost::lexical_cast<std::string>(i), properties);
2022-01-16 02:54:02 +03:00
properties << '=';
char value[64];
2022-11-23 21:44:03 +03:00
size_t l = ByteStreamToBase64 (introducer.iH, 32, value, 64);
2022-01-16 02:54:02 +03:00
value[l] = 0;
WriteString (value, properties);
properties << ';';
i++;
}
i = 0;
for (const auto& introducer: address.ssu->introducers)
{
WriteString ("itag" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
WriteString (boost::lexical_cast<std::string>(introducer.iTag), properties);
properties << ';';
i++;
}
}
2022-04-26 02:57:46 +03:00
}
2022-11-23 21:44:03 +03:00
if (address.transportStyle == eTransportSSU2)
{
2022-01-16 02:54:02 +03:00
// write mtu
2022-04-27 03:01:32 +03:00
if (address.ssu && address.ssu->mtu)
2022-01-16 02:54:02 +03:00
{
WriteString ("mtu", properties);
properties << '=';
WriteString (boost::lexical_cast<std::string>(address.ssu->mtu), properties);
properties << ';';
}
}
2022-11-23 21:44:03 +03:00
if (isPublished && address.port)
2022-01-16 02:54:02 +03:00
{
WriteString ("port", properties);
properties << '=';
WriteString (boost::lexical_cast<std::string>(address.port), properties);
properties << ';';
}
2022-03-12 00:17:44 +03:00
if (address.IsNTCP2 () || address.IsSSU2 ())
2022-01-16 02:54:02 +03:00
{
2022-03-12 00:17:44 +03:00
// publish s and v for NTCP2 or SSU2
2022-01-16 02:54:02 +03:00
WriteString ("s", properties); properties << '=';
2022-02-06 01:14:25 +03:00
WriteString (address.s.ToBase64 (), properties); properties << ';';
2022-01-16 02:54:02 +03:00
WriteString ("v", properties); properties << '=';
WriteString ("2", properties); properties << ';';
}
uint16_t size = htobe16 (properties.str ().size ());
s.write ((char *)&size, sizeof (size));
s.write (properties.str ().c_str (), properties.str ().size ());
}
// peers
uint8_t numPeers = 0;
s.write ((char *)&numPeers, sizeof (numPeers));
// properties
std::stringstream properties;
for (const auto& p : m_Properties)
{
WriteString (p.first, properties);
properties << '=';
WriteString (p.second, properties);
properties << ';';
}
uint16_t size = htobe16 (properties.str ().size ());
s.write ((char *)&size, sizeof (size));
s.write (properties.str ().c_str (), properties.str ().size ());
}
2022-01-16 02:54:02 +03:00
void LocalRouterInfo::SetProperty (const std::string& key, const std::string& value)
{
m_Properties[key] = value;
}
void LocalRouterInfo::DeleteProperty (const std::string& key)
{
m_Properties.erase (key);
}
std::string LocalRouterInfo::GetProperty (const std::string& key) const
{
auto it = m_Properties.find (key);
if (it != m_Properties.end ())
return it->second;
return "";
}
2022-01-16 02:54:02 +03:00
void LocalRouterInfo::WriteString (const std::string& str, std::ostream& s) const
{
uint8_t len = str.size ();
s.write ((char *)&len, 1);
s.write (str.c_str (), len);
}
std::shared_ptr<RouterInfo::Buffer> LocalRouterInfo::NewBuffer () const
{
return std::make_shared<Buffer> ();
}
std::shared_ptr<RouterInfo::Address> LocalRouterInfo::NewAddress () const
{
return std::make_shared<Address> ();
}
boost::shared_ptr<RouterInfo::Addresses> LocalRouterInfo::NewAddresses () const
{
return boost::make_shared<Addresses> ();
}
2023-03-17 04:32:53 +03:00
std::shared_ptr<IdentityEx> LocalRouterInfo::NewIdentity (const uint8_t * buf, size_t len) const
{
return std::make_shared<IdentityEx> (buf, len);
}
bool LocalRouterInfo::AddSSU2Introducer (const Introducer& introducer, bool v4)
{
auto addresses = GetAddresses ();
if (!addresses) return false;
auto addr = (*addresses)[v4 ? eSSU2V4Idx : eSSU2V6Idx];
if (addr)
{
for (auto& intro: addr->ssu->introducers)
if (intro.iTag == introducer.iTag) return false; // already presented
addr->ssu->introducers.push_back (introducer);
SetReachableTransports (GetReachableTransports () | ((addr->IsV4 () ? eSSU2V4 : eSSU2V6)));
return true;
}
return false;
}
bool LocalRouterInfo::RemoveSSU2Introducer (const IdentHash& h, bool v4)
{
auto addresses = GetAddresses ();
if (!addresses) return false;
auto addr = (*addresses)[v4 ? eSSU2V4Idx : eSSU2V6Idx];
if (addr)
{
for (auto it = addr->ssu->introducers.begin (); it != addr->ssu->introducers.end (); ++it)
if (h == it->iH)
{
addr->ssu->introducers.erase (it);
if (addr->ssu->introducers.empty ())
SetReachableTransports (GetReachableTransports () & ~(addr->IsV4 () ? eSSU2V4 : eSSU2V6));
return true;
}
}
return false;
}
2013-10-27 19:28:23 +04:00
}
}