i2pd/libi2pd/RouterInfo.cpp

1166 lines
32 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2013-2021, 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 15:28:23 +00:00
#include <stdio.h>
#include <string.h>
#include "I2PEndian.h"
2013-10-27 15:28:23 +00:00
#include <fstream>
#include <boost/lexical_cast.hpp>
#include <boost/make_shared.hpp>
2016-09-24 12:29:08 +00:00
#if (BOOST_VERSION >= 105300)
#include <boost/atomic.hpp>
#endif
2016-01-16 20:36:30 +00:00
#include "version.h"
2021-01-30 23:32:17 +00:00
#include "util.h"
2015-11-03 14:15:49 +00:00
#include "Crypto.h"
#include "Base.h"
2013-10-27 15:28:23 +00:00
#include "Timestamp.h"
#include "Log.h"
#include "NetDb.hpp"
2016-10-12 15:26:48 +00:00
#include "RouterContext.h"
2013-10-27 15:28:23 +00:00
#include "RouterInfo.h"
2014-01-22 06:46:58 +00:00
2013-10-27 15:28:23 +00:00
namespace i2p
{
namespace data
2018-01-06 03:48:51 +00:00
{
RouterInfo::RouterInfo (): m_Buffer (nullptr)
{
m_Addresses = boost::make_shared<Addresses>(); // create empty list
}
2018-01-06 03:48:51 +00:00
2014-07-23 14:56:41 +00:00
RouterInfo::RouterInfo (const std::string& fullPath):
2019-05-23 15:49:54 +00:00
m_FullPath (fullPath), m_IsUpdated (false), m_IsUnreachable (false),
m_SupportedTransports (0), m_Caps (0), m_Version (0)
2013-10-27 15:28:23 +00:00
{
m_Addresses = boost::make_shared<Addresses>(); // create empty list
2019-05-23 15:49:54 +00:00
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
2014-07-23 14:56:41 +00:00
ReadFromFile ();
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:28:23 +00:00
2013-11-20 12:46:09 +00:00
RouterInfo::RouterInfo (const uint8_t * buf, int len):
m_IsUpdated (true), m_IsUnreachable (false), m_SupportedTransports (0),
m_Caps (0), m_Version (0)
2013-10-27 15:28:23 +00:00
{
m_Addresses = boost::make_shared<Addresses>(); // create empty list
if (len <= MAX_RI_BUFFER_SIZE)
{
2019-05-23 15:49:54 +00:00
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
memcpy (m_Buffer, buf, len);
m_BufferLen = len;
ReadFromBuffer (true);
}
else
{
LogPrint (eLogError, "RouterInfo: Buffer is too long ", len, ". Ignored");
m_Buffer = nullptr;
m_IsUnreachable = true;
}
2018-01-06 03:48:51 +00:00
}
2014-07-22 00:14:11 +00:00
RouterInfo::~RouterInfo ()
{
2015-04-07 20:26:35 +00:00
delete[] m_Buffer;
2018-01-06 03:48:51 +00:00
}
void RouterInfo::Update (const uint8_t * buf, size_t len)
2014-07-22 00:14:11 +00:00
{
2019-05-23 15:49:54 +00:00
if (len > MAX_RI_BUFFER_SIZE)
{
LogPrint (eLogError, "RouterInfo: Buffer is too long ", len);
m_IsUnreachable = true;
return;
}
2019-04-08 19:22:42 +00:00
// verify signature since we have identity already
2015-11-03 14:15:49 +00: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;
m_Caps = 0;
2016-07-13 16:56:23 +00:00
// don't clean up m_Addresses, it will be replaced in ReadFromStream
2015-11-03 14:15:49 +00:00
m_Properties.clear ();
// copy buffer
2018-01-06 03:48:51 +00:00
if (!m_Buffer)
2019-05-23 15:49:54 +00:00
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
2015-11-03 14:15:49 +00:00
memcpy (m_Buffer, buf, len);
m_BufferLen = len;
// skip identity
size_t identityLen = m_RouterIdentity->GetFullLen ();
2018-01-06 03:48:51 +00:00
// read new RI
2015-11-03 14:15:49 +00:00
std::stringstream str (std::string ((char *)m_Buffer + identityLen, m_BufferLen - identityLen));
ReadFromStream (str);
// don't delete buffer until saved to the file
}
else
2018-01-06 03:48:51 +00:00
{
2015-12-18 14:07:50 +00:00
LogPrint (eLogError, "RouterInfo: signature verification failed");
2015-11-03 14:15:49 +00:00
m_IsUnreachable = true;
}
2018-01-06 03:48:51 +00:00
}
2015-11-03 14:15:49 +00:00
void RouterInfo::SetRouterIdentity (std::shared_ptr<const IdentityEx> identity)
2018-01-06 03:48:51 +00:00
{
2013-10-27 15:28:23 +00:00
m_RouterIdentity = identity;
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
}
2018-01-06 03:48:51 +00:00
2014-07-23 14:56:41 +00:00
bool RouterInfo::LoadFile ()
2013-10-27 15:28:23 +00:00
{
2016-03-31 00:00:00 +00:00
std::ifstream s(m_FullPath, std::ifstream::binary);
2018-01-06 03:48:51 +00:00
if (s.is_open ())
{
2013-10-27 15:28:23 +00:00
s.seekg (0,std::ios::end);
2014-02-02 22:22:00 +00:00
m_BufferLen = s.tellg ();
2016-02-02 16:55:38 +00:00
if (m_BufferLen < 40 || m_BufferLen > MAX_RI_BUFFER_SIZE)
2014-02-02 22:22:00 +00:00
{
2015-12-18 14:07:50 +00:00
LogPrint(eLogError, "RouterInfo: File", m_FullPath, " is malformed");
2014-07-23 14:56:41 +00:00
return false;
2014-02-02 22:22:00 +00:00
}
2013-10-27 15:28:23 +00:00
s.seekg(0, std::ios::beg);
2019-05-25 18:58:10 +00:00
if (!m_Buffer) m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
2014-07-10 19:33:42 +00:00
s.read((char *)m_Buffer, m_BufferLen);
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:28:23 +00:00
else
2014-07-23 14:56:41 +00:00
{
2015-12-18 14:07:50 +00:00
LogPrint (eLogError, "RouterInfo: Can't open file ", m_FullPath);
2018-01-06 03:48:51 +00:00
return false;
2014-07-23 14:56:41 +00:00
}
return true;
2018-01-06 03:48:51 +00:00
}
2014-07-23 14:56:41 +00:00
void RouterInfo::ReadFromFile ()
{
if (LoadFile ())
2018-01-06 03:48:51 +00:00
ReadFromBuffer (false);
else
2018-01-06 03:48:51 +00:00
m_IsUnreachable = true;
}
2013-10-27 15:28:23 +00:00
void RouterInfo::ReadFromBuffer (bool verifySignature)
2013-10-27 15:28:23 +00:00
{
2015-11-03 14:15:49 +00:00
m_RouterIdentity = std::make_shared<IdentityEx>(m_Buffer, m_BufferLen);
size_t identityLen = m_RouterIdentity->GetFullLen ();
2016-02-02 16:55:38 +00:00
if (identityLen >= m_BufferLen)
{
LogPrint (eLogError, "RouterInfo: identity length ", identityLen, " exceeds buffer size ", m_BufferLen);
m_IsUnreachable = true;
return;
}
if (verifySignature)
2018-01-06 03:48:51 +00:00
{
2017-11-13 16:25:42 +00:00
// reject RSA signatures
if (m_RouterIdentity->IsRSA ())
2017-11-13 16:25:42 +00:00
{
LogPrint (eLogError, "RouterInfo: RSA signature type is not allowed");
2017-11-13 16:25:42 +00:00
m_IsUnreachable = true;
return;
}
// verify signature
2018-01-06 03:48:51 +00:00
int l = m_BufferLen - m_RouterIdentity->GetSignatureLen ();
2016-02-02 16:55:38 +00:00
if (l < 0 || !m_RouterIdentity->Verify ((uint8_t *)m_Buffer, l, (uint8_t *)m_Buffer + l))
2018-01-06 03:48:51 +00:00
{
2015-12-18 14:07:50 +00:00
LogPrint (eLogError, "RouterInfo: signature verification failed");
2014-11-26 15:28:06 +00:00
m_IsUnreachable = true;
2016-09-13 01:37:43 +00:00
return;
2014-11-26 15:28:06 +00:00
}
2015-11-03 14:15:49 +00:00
m_RouterIdentity->DropVerifier ();
2018-01-06 03:48:51 +00:00
}
2016-09-13 01:37:43 +00:00
// parse RI
std::stringstream str;
str.write ((const char *)m_Buffer + identityLen, m_BufferLen - identityLen);
ReadFromStream (str);
if (!str)
{
LogPrint (eLogError, "RouterInfo: malformed message");
m_IsUnreachable = true;
}
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:28:23 +00:00
void RouterInfo::ReadFromStream (std::istream& s)
{
2021-02-23 03:53:25 +00:00
m_Caps = 0;
2013-10-27 15:28:23 +00:00
s.read ((char *)&m_Timestamp, sizeof (m_Timestamp));
m_Timestamp = be64toh (m_Timestamp);
// read addresses
2018-01-06 03:48:51 +00:00
auto addresses = boost::make_shared<Addresses>();
2013-10-27 15:28:23 +00:00
uint8_t numAddresses;
2018-01-06 03:48:51 +00:00
s.read ((char *)&numAddresses, sizeof (numAddresses)); if (!s) return;
2013-10-27 15:28:23 +00:00
for (int i = 0; i < numAddresses; i++)
{
uint8_t supportedTransports = 0;
2016-12-14 18:54:16 +00:00
auto address = std::make_shared<Address>();
s.read ((char *)&address->cost, sizeof (address->cost));
s.read ((char *)&address->date, sizeof (address->date));
bool isHost = false, isIntroKey = false, isStaticKey = false;
char transportStyle[6];
ReadString (transportStyle, 6, s);
2018-06-15 16:52:43 +00:00
if (!strncmp (transportStyle, "NTCP", 4)) // NTCP or NTCP2
{
2016-12-14 18:54:16 +00:00
address->transportStyle = eTransportNTCP;
address->ntcp2.reset (new NTCP2Ext ());
}
2013-10-27 15:28:23 +00:00
else if (!strcmp (transportStyle, "SSU"))
2018-01-06 03:48:51 +00:00
{
2016-12-14 18:54:16 +00:00
address->transportStyle = eTransportSSU;
address->ssu.reset (new SSUExt ());
address->ssu->mtu = 0;
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:28:23 +00:00
else
2016-12-14 18:54:16 +00:00
address->transportStyle = eTransportUnknown;
2021-02-23 02:04:26 +00:00
address->caps = 0;
2016-12-14 18:54:16 +00:00
address->port = 0;
2013-10-27 15:28:23 +00:00
uint16_t size, r = 0;
2016-02-02 16:55:38 +00:00
s.read ((char *)&size, sizeof (size)); if (!s) return;
2013-10-27 15:28:23 +00:00
size = be16toh (size);
while (r < size)
{
2016-08-15 17:12:56 +00:00
char key[255], value[255];
r += ReadString (key, 255, s);
2013-10-27 15:28:23 +00:00
s.seekg (1, std::ios_base::cur); r++; // =
2018-01-06 03:48:51 +00:00
r += ReadString (value, 255, s);
2013-10-27 15:28:23 +00:00
s.seekg (1, std::ios_base::cur); r++; // ;
2016-09-12 15:39:33 +00:00
if (!s) return;
2013-10-27 15:28:23 +00:00
if (!strcmp (key, "host"))
2018-01-06 03:48:51 +00:00
{
2014-01-21 23:01:11 +00:00
boost::system::error_code ecode;
2016-12-14 18:54:16 +00:00
address->host = boost::asio::ip::address::from_string (value, ecode);
if (!ecode && !address->host.is_unspecified ()) isHost = true;
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:28:23 +00:00
else if (!strcmp (key, "port"))
2016-12-14 18:54:16 +00:00
address->port = boost::lexical_cast<int>(value);
2014-09-14 21:57:47 +00:00
else if (!strcmp (key, "mtu"))
{
if (address->ssu)
address->ssu->mtu = boost::lexical_cast<int>(value);
else
LogPrint (eLogWarning, "RouterInfo: Unexpected field 'mtu' for NTCP");
2018-01-06 03:48:51 +00:00
}
2014-01-28 00:48:46 +00:00
else if (!strcmp (key, "key"))
2018-01-06 03:48:51 +00:00
{
if (address->ssu)
isIntroKey = (Base64ToByteStream (value, strlen (value), address->ssu->key, 32) == 32);
else
LogPrint (eLogWarning, "RouterInfo: Unexpected field 'key' for NTCP");
2018-01-06 03:48:51 +00:00
}
2014-04-09 01:56:34 +00:00
else if (!strcmp (key, "caps"))
2021-02-23 03:53:25 +00:00
address->caps = ExtractAddressCaps (value);
2018-06-06 15:51:34 +00:00
else if (!strcmp (key, "s")) // ntcp2 static key
{
Base64ToByteStream (value, strlen (value), address->ntcp2->staticKey, 32);
isStaticKey = true;
}
2018-06-06 15:51:34 +00:00
else if (!strcmp (key, "i")) // ntcp2 iv
{
Base64ToByteStream (value, strlen (value), address->ntcp2->iv, 16);
address->ntcp2->isPublished = true; // presence if "i" means "published"
}
2014-02-20 21:15:12 +00:00
else if (key[0] == 'i')
2018-01-06 03:48:51 +00:00
{
2014-02-20 21:15:12 +00:00
// introducers
2019-05-11 11:27:34 +00:00
if (!address->ssu)
{
LogPrint (eLogError, "RouterInfo: Introducer is presented for non-SSU address. Skipped");
continue;
}
2018-01-06 03:48:51 +00:00
size_t l = strlen(key);
2014-02-24 01:48:28 +00:00
unsigned char index = key[l-1] - '0'; // TODO:
2014-02-20 21:15:12 +00:00
key[l-1] = 0;
2016-09-12 15:39:33 +00:00
if (index > 9)
{
LogPrint (eLogError, "RouterInfo: Unexpected introducer's index ", index, " skipped");
if (s) continue; else return;
}
if (index >= address->ssu->introducers.size ())
2018-01-06 03:48:51 +00:00
address->ssu->introducers.resize (index + 1);
Introducer& introducer = address->ssu->introducers.at (index);
2014-02-20 21:15:12 +00:00
if (!strcmp (key, "ihost"))
{
boost::system::error_code ecode;
introducer.iHost = boost::asio::ip::address::from_string (value, ecode);
2018-01-06 03:48:51 +00:00
}
2014-02-20 21:15:12 +00:00
else if (!strcmp (key, "iport"))
introducer.iPort = boost::lexical_cast<int>(value);
else if (!strcmp (key, "itag"))
introducer.iTag = boost::lexical_cast<uint32_t>(value);
else if (!strcmp (key, "ikey"))
Base64ToByteStream (value, strlen (value), introducer.iKey, 32);
2017-05-24 16:49:36 +00:00
else if (!strcmp (key, "iexp"))
introducer.iExp = boost::lexical_cast<uint32_t>(value);
2014-02-20 21:15:12 +00:00
}
2016-02-02 16:55:38 +00:00
if (!s) return;
2018-01-06 03:48:51 +00:00
}
if (address->transportStyle == eTransportNTCP)
{
2021-01-30 23:32:17 +00:00
if (isStaticKey)
{
if (isHost)
{
if (address->host.is_v6 ())
supportedTransports |= i2p::util::net::IsYggdrasilAddress (address->host) ? eNTCP2V6Mesh : eNTCP2V6;
else
supportedTransports |= eNTCP2V4;
}
else if (!address->ntcp2->isPublished)
{
if (address->caps)
{
2021-03-06 02:53:19 +00: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-30 23:32:17 +00:00
}
}
else if (address->transportStyle == eTransportSSU)
{
if (isIntroKey)
{
if (isHost)
supportedTransports |= address->host.is_v4 () ? eSSUV4 : eSSUV6;
2021-03-06 02:53:19 +00:00
else if (address->caps & AddressCaps::eV6)
2021-03-05 03:47:56 +00:00
{
supportedTransports |= eSSUV6;
2021-03-06 02:53:19 +00:00
if (address->caps & AddressCaps::eV4) supportedTransports |= eSSUV4; // in additional to v6
2021-03-05 03:47:56 +00:00
}
else
2021-03-05 03:47:56 +00:00
supportedTransports |= eSSUV4; // in case if host or 6 caps is not preasented, we assume 4
}
}
if (supportedTransports)
{
2016-12-14 18:54:16 +00:00
addresses->push_back(address);
m_SupportedTransports |= supportedTransports;
}
2018-01-06 03:48:51 +00:00
}
#if (BOOST_VERSION >= 105300)
boost::atomic_store (&m_Addresses, addresses);
2016-09-24 12:29:08 +00:00
#else
m_Addresses = addresses; // race condition
2018-01-06 03:48:51 +00:00
#endif
2013-10-27 15:28:23 +00:00
// read peers
uint8_t numPeers;
2016-02-02 16:55:38 +00:00
s.read ((char *)&numPeers, sizeof (numPeers)); if (!s) return;
2013-10-27 15:28:23 +00:00
s.seekg (numPeers*32, std::ios_base::cur); // TODO: read peers
// read properties
uint16_t size, r = 0;
2016-02-02 16:55:38 +00:00
s.read ((char *)&size, sizeof (size)); if (!s) return;
2013-10-27 15:28:23 +00:00
size = be16toh (size);
while (r < size)
{
2018-01-06 03:48:51 +00:00
char key[255], value[255];
2016-08-15 17:12:56 +00:00
r += ReadString (key, 255, s);
2013-10-27 15:28:23 +00:00
s.seekg (1, std::ios_base::cur); r++; // =
2018-01-06 03:48:51 +00:00
r += ReadString (value, 255, s);
2013-10-27 15:28:23 +00:00
s.seekg (1, std::ios_base::cur); r++; // ;
2016-09-12 15:39:33 +00:00
if (!s) return;
2013-10-27 15:28:23 +00:00
m_Properties[key] = value;
2018-01-06 03:48:51 +00:00
// extract caps
2014-03-19 21:27:37 +00:00
if (!strcmp (key, "caps"))
2021-02-23 03:53:25 +00: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 20:36:30 +00:00
// check netId
2016-10-12 15:26:48 +00:00
else if (!strcmp (key, ROUTER_INFO_PROPERTY_NETID) && atoi (value) != i2p::context.GetNetID ())
2016-01-16 20:36:30 +00:00
{
2016-09-12 15:39:33 +00:00
LogPrint (eLogError, "RouterInfo: Unexpected ", ROUTER_INFO_PROPERTY_NETID, "=", value);
2018-01-06 03:48:51 +00:00
m_IsUnreachable = true;
}
// family
2016-02-21 01:20:19 +00:00
else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY))
{
m_Family = value;
boost::to_lower (m_Family);
}
2016-02-21 01:20:19 +00:00
else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY_SIG))
{
if (!netdb.GetFamilies ().VerifyFamily (m_Family, GetIdentHash (), value))
{
LogPrint (eLogWarning, "RouterInfo: family signature verification failed");
m_Family.clear ();
}
2018-01-06 03:48:51 +00:00
}
2016-02-02 16:55:38 +00:00
if (!s) return;
2018-01-06 03:48:51 +00:00
}
2021-03-05 03:47:56 +00:00
if (!m_SupportedTransports)
SetUnreachable (true);
2016-06-17 15:03:33 +00:00
}
bool RouterInfo::IsFamily(const std::string & fam) const
{
return m_Family == fam;
}
2013-12-29 15:48:57 +00:00
2021-02-23 03:53:25 +00:00
void RouterInfo::ExtractCaps (const char * value)
2014-03-19 16:02:51 +00:00
{
const char * cap = value;
while (*cap)
{
switch (*cap)
{
2014-09-02 20:11:31 +00:00
case CAPS_FLAG_FLOODFILL:
2021-02-23 03:53:25 +00:00
m_Caps |= Caps::eFloodfill;
2014-03-19 16:02:51 +00:00
break;
2014-09-02 20:11:31 +00:00
case CAPS_FLAG_HIGH_BANDWIDTH1:
case CAPS_FLAG_HIGH_BANDWIDTH2:
case CAPS_FLAG_HIGH_BANDWIDTH3:
2021-02-23 03:53:25 +00:00
m_Caps |= Caps::eHighBandwidth;
2014-03-19 16:02:51 +00:00
break;
2016-01-03 03:17:04 +00:00
case CAPS_FLAG_EXTRA_BANDWIDTH1:
case CAPS_FLAG_EXTRA_BANDWIDTH2:
2021-02-23 03:53:25 +00:00
m_Caps |= Caps::eExtraBandwidth | Caps::eHighBandwidth;
2018-01-06 03:48:51 +00:00
break;
2014-09-02 20:11:31 +00:00
case CAPS_FLAG_HIDDEN:
2021-02-23 03:53:25 +00:00
m_Caps |= Caps::eHidden;
2018-01-06 03:48:51 +00:00
break;
2014-09-02 20:11:31 +00:00
case CAPS_FLAG_REACHABLE:
2021-02-23 03:53:25 +00:00
m_Caps |= Caps::eReachable;
2014-03-19 16:02:51 +00:00
break;
2014-09-02 20:11:31 +00:00
case CAPS_FLAG_UNREACHABLE:
2021-02-23 03:53:25 +00:00
m_Caps |= Caps::eUnreachable;
2018-01-06 03:48:51 +00:00
break;
2021-02-23 03:53:25 +00: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;
case CAPS_FLAG_V6:
caps |= AddressCaps::eV6;
break;
2014-09-02 20:11:31 +00:00
case CAPS_FLAG_SSU_TESTING:
2021-02-23 03:53:25 +00:00
caps |= AddressCaps::eSSUTesting;
2018-01-06 03:48:51 +00:00
break;
2014-09-02 20:11:31 +00:00
case CAPS_FLAG_SSU_INTRODUCER:
2021-02-23 03:53:25 +00:00
caps |= AddressCaps::eSSUIntroducer;
2018-01-06 03:48:51 +00:00
break;
2014-03-19 16:02:51 +00:00
default: ;
2018-01-06 03:48:51 +00:00
}
2014-03-19 16:02:51 +00:00
cap++;
}
2021-02-23 02:04:26 +00:00
return caps;
2014-03-19 16:02:51 +00:00
}
2021-02-23 03:53:25 +00:00
2014-09-02 20:11:31 +00:00
void RouterInfo::UpdateCapsProperty ()
2018-01-06 03:48:51 +00:00
{
2014-09-02 20:11:31 +00:00
std::string caps;
2018-01-06 03:48:51 +00:00
if (m_Caps & eFloodfill)
2016-08-14 21:52:11 +00:00
{
2017-01-25 21:14:01 +00:00
if (m_Caps & eExtraBandwidth) caps += (m_Caps & eHighBandwidth) ?
CAPS_FLAG_EXTRA_BANDWIDTH2 : // 'X'
CAPS_FLAG_EXTRA_BANDWIDTH1; // 'P'
2018-09-30 20:08:26 +00:00
else
caps += CAPS_FLAG_HIGH_BANDWIDTH3; // 'O'
2018-01-06 03:48:51 +00:00
caps += CAPS_FLAG_FLOODFILL; // floodfill
}
else
2016-08-14 21:52:11 +00:00
{
2018-01-06 03:48:51 +00:00
if (m_Caps & eExtraBandwidth)
2017-01-25 21:14:01 +00:00
caps += (m_Caps & eHighBandwidth) ? CAPS_FLAG_EXTRA_BANDWIDTH2 /* 'X' */ : CAPS_FLAG_EXTRA_BANDWIDTH1; /*'P' */
2018-01-06 03:48:51 +00:00
else
caps += (m_Caps & eHighBandwidth) ? CAPS_FLAG_HIGH_BANDWIDTH3 /* 'O' */: CAPS_FLAG_LOW_BANDWIDTH2 /* 'L' */; // bandwidth
}
2014-09-02 20:11:31 +00:00
if (m_Caps & eHidden) caps += CAPS_FLAG_HIDDEN; // hidden
if (m_Caps & eReachable) caps += CAPS_FLAG_REACHABLE; // reachable
if (m_Caps & eUnreachable) caps += CAPS_FLAG_UNREACHABLE; // unreachable
2016-06-01 00:00:00 +00:00
SetProperty ("caps", caps);
2014-09-02 20:11:31 +00:00
}
2018-01-06 03:48:51 +00:00
2016-08-07 21:52:18 +00:00
void RouterInfo::WriteToStream (std::ostream& s) const
2013-10-27 15:28:23 +00:00
{
uint64_t ts = htobe64 (m_Timestamp);
2016-08-07 21:52:18 +00:00
s.write ((const char *)&ts, sizeof (ts));
2013-10-27 15:28:23 +00:00
// addresses
2016-07-13 16:56:23 +00:00
uint8_t numAddresses = m_Addresses->size ();
2013-10-27 15:28:23 +00:00
s.write ((char *)&numAddresses, sizeof (numAddresses));
2016-08-07 21:52:18 +00:00
for (const auto& addr_ptr : *m_Addresses)
2013-10-27 15:28:23 +00:00
{
2016-08-07 21:52:18 +00:00
const Address& address = *addr_ptr;
s.write ((const char *)&address.cost, sizeof (address.cost));
s.write ((const char *)&address.date, sizeof (address.date));
2014-02-23 16:48:09 +00:00
std::stringstream properties;
bool isPublished = false;
2013-10-27 15:28:23 +00:00
if (address.transportStyle == eTransportNTCP)
2020-10-03 22:46:12 +00:00
{
if (address.IsNTCP2 ())
{
2020-10-03 22:46:12 +00:00
WriteString ("NTCP2", s);
if (address.IsPublishedNTCP2 () && !address.host.is_unspecified ())
isPublished = true;
else
{
WriteString ("caps", properties);
properties << '=';
std::string caps;
if (address.caps & AddressCaps::eV4) caps += CAPS_FLAG_V4;
if (address.caps & AddressCaps::eV6) caps += CAPS_FLAG_V6;
if (caps.empty ()) caps += CAPS_FLAG_V4;
WriteString (caps, properties);
properties << ';';
}
}
2020-10-03 22:46:12 +00:00
else
continue; // don't write NTCP address
}
2013-10-27 15:28:23 +00:00
else if (address.transportStyle == eTransportSSU)
2018-01-06 03:48:51 +00:00
{
2013-10-27 15:28:23 +00:00
WriteString ("SSU", s);
2014-02-24 01:48:28 +00:00
// caps
WriteString ("caps", properties);
2014-02-23 16:48:09 +00:00
properties << '=';
std::string caps;
if (address.IsPeerTesting ()) caps += CAPS_FLAG_SSU_TESTING;
if (address.host.is_v4 ())
{
if (IsReachable ())
{
isPublished = true;
if (address.IsIntroducer ()) caps += CAPS_FLAG_SSU_INTRODUCER;
}
else
caps += CAPS_FLAG_V4;
}
else if (address.host.is_v6 ())
{
isPublished = true;
if (address.IsIntroducer ()) caps += CAPS_FLAG_SSU_INTRODUCER;
}
else
{
if (address.caps & AddressCaps::eV4) caps += CAPS_FLAG_V4;
if (address.caps & AddressCaps::eV6) caps += CAPS_FLAG_V6;
if (caps.empty ()) caps += CAPS_FLAG_V4;
}
2014-04-09 01:56:34 +00:00
WriteString (caps, properties);
2014-02-23 16:48:09 +00:00
properties << ';';
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:28:23 +00:00
else
WriteString ("", s);
if (isPublished)
2018-06-15 16:52:43 +00:00
{
WriteString ("host", properties);
properties << '=';
WriteString (address.host.to_string (), properties);
properties << ';';
}
2014-02-24 01:48:28 +00:00
if (address.transportStyle == eTransportSSU)
{
2014-09-01 21:34:20 +00:00
// write introducers if any
if (!address.ssu->introducers.empty())
2018-01-06 03:48:51 +00:00
{
2014-09-01 21:34:20 +00:00
int i = 0;
for (const auto& introducer: address.ssu->introducers)
2014-09-01 21:34:20 +00:00
{
WriteString ("ihost" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
WriteString (introducer.iHost.to_string (), properties);
properties << ';';
i++;
2018-01-06 03:48:51 +00:00
}
2014-09-01 21:34:20 +00:00
i = 0;
for (const auto& introducer: address.ssu->introducers)
2014-09-01 21:34:20 +00:00
{
WriteString ("ikey" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
char value[64];
size_t l = ByteStreamToBase64 (introducer.iKey, 32, value, 64);
value[l] = 0;
WriteString (value, properties);
properties << ';';
i++;
2018-01-06 03:48:51 +00:00
}
2014-09-01 21:34:20 +00:00
i = 0;
for (const auto& introducer: address.ssu->introducers)
2014-09-01 21:34:20 +00:00
{
WriteString ("iport" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
WriteString (boost::lexical_cast<std::string>(introducer.iPort), properties);
properties << ';';
i++;
2018-01-06 03:48:51 +00:00
}
2014-09-01 21:34:20 +00:00
i = 0;
for (const auto& introducer: address.ssu->introducers)
2014-09-01 21:34:20 +00:00
{
WriteString ("itag" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
WriteString (boost::lexical_cast<std::string>(introducer.iTag), properties);
properties << ';';
i++;
2018-01-06 03:48:51 +00:00
}
2017-05-24 16:49:36 +00:00
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++;
2018-01-06 03:48:51 +00:00
}
}
2014-09-01 21:34:20 +00:00
// write intro key
2014-02-24 01:48:28 +00:00
WriteString ("key", properties);
properties << '=';
char value[64];
size_t l = ByteStreamToBase64 (address.ssu->key, 32, value, 64);
2014-02-24 01:48:28 +00:00
value[l] = 0;
WriteString (value, properties);
properties << ';';
2014-10-30 14:07:39 +00:00
// write mtu
if (address.ssu->mtu)
2014-10-30 14:07:39 +00:00
{
WriteString ("mtu", properties);
properties << '=';
WriteString (boost::lexical_cast<std::string>(address.ssu->mtu), properties);
2014-10-30 14:07:39 +00:00
properties << ';';
2018-01-06 03:48:51 +00:00
}
}
2018-06-15 16:52:43 +00:00
if (address.IsNTCP2 () && isPublished)
2018-08-01 16:28:34 +00:00
{
// publish i for NTCP2
WriteString ("i", properties); properties << '=';
WriteString (address.ntcp2->iv.ToBase64 (), properties); properties << ';';
}
2021-02-24 02:15:17 +00:00
if (isPublished || address.ssu)
2018-06-15 16:52:43 +00:00
{
WriteString ("port", properties);
properties << '=';
WriteString (boost::lexical_cast<std::string>(address.port), properties);
properties << ';';
}
2018-06-15 16:52:43 +00:00
if (address.IsNTCP2 ())
{
// publish s and v for NTCP2
WriteString ("s", properties); properties << '=';
WriteString (address.ntcp2->staticKey.ToBase64 (), properties); properties << ';';
WriteString ("v", properties); properties << '=';
WriteString ("2", properties); properties << ';';
}
2018-01-06 03:48:51 +00:00
2013-10-27 15:28:23 +00:00
uint16_t size = htobe16 (properties.str ().size ());
s.write ((char *)&size, sizeof (size));
s.write (properties.str ().c_str (), properties.str ().size ());
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:28:23 +00:00
// peers
uint8_t numPeers = 0;
s.write ((char *)&numPeers, sizeof (numPeers));
// properties
std::stringstream properties;
2016-08-07 21:52:18 +00:00
for (const auto& p : m_Properties)
2013-10-27 15:28:23 +00:00
{
WriteString (p.first, properties);
properties << '=';
WriteString (p.second, properties);
properties << ';';
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:28:23 +00:00
uint16_t size = htobe16 (properties.str ().size ());
s.write ((char *)&size, sizeof (size));
s.write (properties.str ().c_str (), properties.str ().size ());
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:28:23 +00:00
2016-02-17 20:36:55 +00: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 03:48:51 +00:00
return bufbe64toh (buf + size) > m_Timestamp;
2016-02-17 20:36:55 +00:00
}
const uint8_t * RouterInfo::LoadBuffer ()
2014-07-23 14:56:41 +00:00
{
if (!m_Buffer)
{
if (LoadFile ())
2015-12-18 14:07:50 +00:00
LogPrint (eLogDebug, "RouterInfo: Buffer for ", GetIdentHashAbbreviation (GetIdentHash ()), " loaded from file");
2018-01-06 03:48:51 +00:00
}
return m_Buffer;
2014-07-23 14:56:41 +00:00
}
2014-08-26 02:47:12 +00:00
void RouterInfo::CreateBuffer (const PrivateKeys& privateKeys)
2013-10-27 15:28:23 +00:00
{
2013-11-20 12:46:09 +00:00
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); // refresh timstamp
2013-10-27 15:28:23 +00:00
std::stringstream s;
uint8_t ident[1024];
2015-11-03 14:15:49 +00:00
auto identLen = privateKeys.GetPublic ()->ToBuffer (ident, 1024);
auto signatureLen = privateKeys.GetPublic ()->GetSignatureLen ();
2018-01-06 03:48:51 +00:00
s.write ((char *)ident, identLen);
2013-10-27 15:28:23 +00:00
WriteToStream (s);
m_BufferLen = s.str ().size ();
2014-07-22 12:03:02 +00:00
if (!m_Buffer)
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
if (m_BufferLen + signatureLen < MAX_RI_BUFFER_SIZE)
{
memcpy (m_Buffer, s.str ().c_str (), m_BufferLen);
// signature
privateKeys.Sign ((uint8_t *)m_Buffer, m_BufferLen, (uint8_t *)m_Buffer + m_BufferLen);
m_BufferLen += signatureLen;
}
else
LogPrint (eLogError, "RouterInfo: Our RouterInfo is too long ", m_BufferLen + signatureLen);
2018-01-06 03:48:51 +00:00
}
2014-07-10 19:33:42 +00:00
bool RouterInfo::SaveToFile (const std::string& fullPath)
2014-07-10 19:33:42 +00:00
{
2014-07-23 14:56:41 +00:00
m_FullPath = fullPath;
if (!m_Buffer) {
2015-12-18 14:07:50 +00:00
LogPrint (eLogError, "RouterInfo: Can't save, m_Buffer == NULL");
return false;
}
std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);
if (!f.is_open ()) {
LogPrint(eLogError, "RouterInfo: Can't save to ", fullPath);
return false;
}
f.write ((char *)m_Buffer, m_BufferLen);
return true;
2014-07-10 19:33:42 +00:00
}
2018-01-06 03:48:51 +00:00
2016-08-15 17:12:56 +00:00
size_t RouterInfo::ReadString (char * str, size_t len, std::istream& s) const
2013-10-27 15:28:23 +00:00
{
2016-08-15 17:12:56 +00:00
uint8_t l;
s.read ((char *)&l, 1);
if (l < len)
2018-01-06 03:48:51 +00:00
{
2016-08-15 17:12:56 +00:00
s.read (str, l);
2016-09-12 15:39:33 +00:00
if (!s) l = 0; // failed, return empty string
2016-08-15 17:12:56 +00:00
str[l] = 0;
}
else
{
LogPrint (eLogWarning, "RouterInfo: string length ", (int)l, " exceeds buffer size ", len);
s.seekg (l, std::ios::cur); // skip
str[0] = 0;
2018-01-06 03:48:51 +00:00
}
2016-08-15 17:12:56 +00:00
return l+1;
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:28:23 +00:00
2016-08-15 17:12:56 +00:00
void RouterInfo::WriteString (const std::string& str, std::ostream& s) const
2013-10-27 15:28:23 +00:00
{
uint8_t len = str.size ();
s.write ((char *)&len, 1);
s.write (str.c_str (), len);
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:28:23 +00:00
2014-10-30 14:07:39 +00:00
void RouterInfo::AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu)
2014-02-23 16:48:09 +00:00
{
2016-03-21 17:02:51 +00:00
auto addr = std::make_shared<Address>();
addr->host = boost::asio::ip::address::from_string (host);
addr->port = port;
addr->transportStyle = eTransportSSU;
2021-03-01 16:09:25 +00:00
addr->cost = COST_SSU_DIRECT; // NTCP2 should have priority over SSU
2021-02-23 02:04:26 +00:00
addr->caps = i2p::data::RouterInfo::eSSUTesting | i2p::data::RouterInfo::eSSUIntroducer; // BC;
2016-03-21 17:02:51 +00:00
addr->date = 0;
addr->ssu.reset (new SSUExt ());
2018-01-06 03:48:51 +00:00
addr->ssu->mtu = mtu;
2020-11-21 23:44:40 +00:00
if (key)
memcpy (addr->ssu->key, key, 32);
else
RAND_bytes (addr->ssu->key, 32);
2016-08-07 21:52:18 +00:00
for (const auto& it: *m_Addresses) // don't insert same address twice
2016-03-22 11:30:16 +00:00
if (*it == *addr) return;
2016-03-21 17:02:51 +00:00
m_SupportedTransports |= addr->host.is_v6 () ? eSSUV6 : eSSUV4;
2016-08-07 21:52:18 +00:00
m_Addresses->push_back(std::move(addr));
2018-01-06 03:48:51 +00:00
}
2014-09-01 21:34:20 +00:00
2018-08-27 20:01:47 +00:00
void RouterInfo::AddNTCP2Address (const uint8_t * staticKey, const uint8_t * iv, const boost::asio::ip::address& host, int port)
2018-06-15 16:52:43 +00:00
{
auto addr = std::make_shared<Address>();
2018-08-27 20:01:47 +00:00
addr->host = host;
addr->port = port;
2018-06-15 16:52:43 +00:00
addr->transportStyle = eTransportNTCP;
2021-03-01 16:09:25 +00:00
addr->cost = port ? COST_NTCP2_PUBLISHED : COST_NTCP2_NON_PUBLISHED; // override from RouterContext::PublishNTCP2Address
2021-02-23 02:04:26 +00:00
addr->caps = 0;
2018-06-15 16:52:43 +00:00
addr->date = 0;
addr->ntcp2.reset (new NTCP2Ext ());
if (port) addr->ntcp2->isPublished = true;
2018-06-15 16:52:43 +00:00
memcpy (addr->ntcp2->staticKey, staticKey, 32);
memcpy (addr->ntcp2->iv, iv, 16);
2018-06-15 16:52:43 +00:00
m_Addresses->push_back(std::move(addr));
}
2015-11-03 14:15:49 +00:00
bool RouterInfo::AddIntroducer (const Introducer& introducer)
2014-09-01 21:34:20 +00:00
{
2016-08-07 21:52:18 +00:00
for (auto& addr : *m_Addresses)
2014-09-01 21:34:20 +00:00
{
if (addr->transportStyle == eTransportSSU &&
((addr->IsV4 () && introducer.iHost.is_v4 ()) || (addr->IsV6 () && introducer.iHost.is_v6 ())))
2018-01-06 03:48:51 +00:00
{
for (auto& intro: addr->ssu->introducers)
2015-11-03 14:15:49 +00:00
if (intro.iTag == introducer.iTag) return false; // already presented
addr->ssu->introducers.push_back (introducer);
2014-09-01 21:34:20 +00:00
return true;
2018-01-06 03:48:51 +00:00
}
}
2014-09-01 21:34:20 +00:00
return false;
2018-01-06 03:48:51 +00:00
}
2014-09-01 21:34:20 +00:00
2014-09-07 00:43:20 +00:00
bool RouterInfo::RemoveIntroducer (const boost::asio::ip::udp::endpoint& e)
2018-01-06 03:48:51 +00:00
{
2016-08-07 21:52:18 +00:00
for (auto& addr: *m_Addresses)
2014-09-01 21:34:20 +00:00
{
if (addr->transportStyle == eTransportSSU &&
((addr->IsV4 () && e.address ().is_v4 ()) || (addr->IsV6 () && e.address ().is_v6 ())))
2018-01-06 03:48:51 +00:00
{
for (auto it = addr->ssu->introducers.begin (); it != addr->ssu->introducers.end (); ++it)
if (boost::asio::ip::udp::endpoint (it->iHost, it->iPort) == e)
2014-09-01 21:34:20 +00:00
{
addr->ssu->introducers.erase (it);
2014-09-01 21:34:20 +00:00
return true;
2016-08-07 21:52:18 +00:00
}
2018-01-06 03:48:51 +00:00
}
}
2014-09-01 21:34:20 +00:00
return false;
}
2014-09-02 20:11:31 +00:00
void RouterInfo::SetCaps (uint8_t caps)
{
m_Caps = caps;
UpdateCapsProperty ();
}
2018-01-06 03:48:51 +00:00
2014-09-01 21:34:20 +00:00
void RouterInfo::SetCaps (const char * caps)
{
SetProperty ("caps", caps);
2021-02-23 03:53:25 +00:00
m_Caps = 0;
ExtractCaps (caps);
2018-01-06 03:48:51 +00:00
}
void RouterInfo::SetProperty (const std::string& key, const std::string& value)
2013-10-27 15:28:23 +00:00
{
m_Properties[key] = value;
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:28:23 +00:00
void RouterInfo::DeleteProperty (const std::string& key)
{
m_Properties.erase (key);
}
2018-01-06 03:48:51 +00:00
std::string RouterInfo::GetProperty (const std::string& key) const
{
auto it = m_Properties.find (key);
if (it != m_Properties.end ())
return it->second;
return "";
2018-01-06 03:48:51 +00:00
}
2014-02-09 02:06:40 +00:00
bool RouterInfo::IsSSU (bool v4only) const
{
if (v4only)
return m_SupportedTransports & eSSUV4;
else
return m_SupportedTransports & (eSSUV4 | eSSUV6);
}
2014-02-21 21:13:36 +00:00
2019-05-23 19:59:44 +00:00
bool RouterInfo::IsSSUV6 () const
{
return m_SupportedTransports & eSSUV6;
}
2018-06-06 15:51:34 +00:00
bool RouterInfo::IsNTCP2 (bool v4only) const
{
if (v4only)
return m_SupportedTransports & eNTCP2V4;
else
return m_SupportedTransports & (eNTCP2V4 | eNTCP2V6);
}
bool RouterInfo::IsNTCP2V6 () const
{
return m_SupportedTransports & eNTCP2V6;
}
bool RouterInfo::IsV6 () const
{
return m_SupportedTransports & (eSSUV6 | eNTCP2V6);
}
2016-03-24 22:44:41 +00:00
bool RouterInfo::IsV4 () const
{
return m_SupportedTransports & (eSSUV4 | eNTCP2V4);
2016-03-24 22:44:41 +00:00
}
2018-01-06 03:48:51 +00:00
2021-01-31 22:25:07 +00:00
bool RouterInfo::IsMesh () const
{
return m_SupportedTransports & eNTCP2V6Mesh;
}
void RouterInfo::EnableV6 ()
{
if (!IsV6 ())
{
m_SupportedTransports |= eSSUV6 | eNTCP2V6;
uint8_t addressCaps = AddressCaps::eV6;
if (IsV4 ()) addressCaps |= AddressCaps::eV4;
SetUnreachableAddressesTransportCaps (addressCaps);
}
}
2016-03-24 22:44:41 +00:00
void RouterInfo::EnableV4 ()
2016-03-24 22:44:41 +00:00
{
if (!IsV4 ())
{
m_SupportedTransports |= eSSUV4 | eNTCP2V4;
uint8_t addressCaps = AddressCaps::eV4;
if (IsV6 ()) addressCaps |= AddressCaps::eV6;
SetUnreachableAddressesTransportCaps (addressCaps);
}
2016-03-24 22:44:41 +00:00
}
2018-01-06 03:48:51 +00:00
void RouterInfo::DisableV6 ()
2018-01-06 03:48:51 +00:00
{
if (IsV6 ())
2018-01-06 03:48:51 +00:00
{
m_SupportedTransports &= ~(eSSUV6 | eNTCP2V6);
2016-07-14 18:10:38 +00:00
for (auto it = m_Addresses->begin (); it != m_Addresses->end ();)
{
2016-07-14 18:10:38 +00:00
auto addr = *it;
addr->caps &= ~AddressCaps::eV6;
2016-07-14 18:10:38 +00:00
if (addr->host.is_v6 ())
it = m_Addresses->erase (it);
else
2016-08-07 21:52:18 +00:00
++it;
2018-01-06 03:48:51 +00:00
}
}
}
2016-03-24 22:44:41 +00:00
void RouterInfo::DisableV4 ()
2018-01-06 03:48:51 +00:00
{
2016-03-24 22:44:41 +00:00
if (IsV4 ())
2018-01-06 03:48:51 +00:00
{
m_SupportedTransports &= ~(eSSUV4 | eNTCP2V4);
2016-07-14 18:10:38 +00:00
for (auto it = m_Addresses->begin (); it != m_Addresses->end ();)
2016-03-24 22:44:41 +00:00
{
2016-07-14 18:10:38 +00:00
auto addr = *it;
addr->caps &= ~AddressCaps::eV4;
2016-07-14 18:10:38 +00:00
if (addr->host.is_v4 ())
it = m_Addresses->erase (it);
else
2016-08-07 21:52:18 +00:00
++it;
2018-01-06 03:48:51 +00:00
}
}
2016-03-24 22:44:41 +00:00
}
2018-01-06 03:48:51 +00:00
2021-01-31 22:25:07 +00:00
void RouterInfo::EnableMesh ()
{
if (!IsMesh ())
m_SupportedTransports |= eNTCP2V6Mesh;
}
void RouterInfo::DisableMesh ()
{
if (IsMesh ())
{
m_SupportedTransports &= ~eNTCP2V6Mesh;
for (auto it = m_Addresses->begin (); it != m_Addresses->end ();)
{
auto addr = *it;
if (i2p::util::net::IsYggdrasilAddress (addr->host))
it = m_Addresses->erase (it);
else
++it;
}
}
}
2018-01-06 03:48:51 +00:00
2014-02-21 21:13:36 +00:00
bool RouterInfo::UsesIntroducer () const
{
2014-08-19 15:01:11 +00:00
return m_Caps & Caps::eUnreachable; // non-reachable
2018-01-06 03:48:51 +00:00
}
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetSSUAddress (bool v4only) const
2014-01-28 00:48:46 +00:00
{
2018-08-04 12:47:58 +00:00
return GetAddress (
[v4only](std::shared_ptr<const RouterInfo::Address> address)->bool
{
return (address->transportStyle == eTransportSSU) && (!v4only || address->host.is_v4 ());
});
2018-01-06 03:48:51 +00:00
}
2014-01-28 00:48:46 +00:00
2018-01-06 03:48:51 +00:00
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetSSUV6Address () const
{
2018-08-04 12:47:58 +00:00
return GetAddress (
[](std::shared_ptr<const RouterInfo::Address> address)->bool
{
return (address->transportStyle == eTransportSSU) && address->host.is_v6 ();
});
2018-01-06 03:48:51 +00:00
}
template<typename Filter>
2018-08-04 12:47:58 +00:00
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetAddress (Filter filter) const
2013-10-27 15:28:23 +00:00
{
2019-04-08 19:22:42 +00:00
// TODO: make it more generic using comparator
2016-10-01 19:05:35 +00:00
#if (BOOST_VERSION >= 105300)
auto addresses = boost::atomic_load (&m_Addresses);
2018-01-06 03:48:51 +00:00
#else
2016-08-10 00:51:54 +00:00
auto addresses = m_Addresses;
2018-01-06 03:48:51 +00:00
#endif
2016-08-10 00:51:54 +00:00
for (const auto& address : *addresses)
2018-08-04 12:47:58 +00:00
if (filter (address)) return address;
2013-10-27 15:28:23 +00:00
return nullptr;
2018-01-06 03:48:51 +00:00
}
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetNTCP2AddressWithStaticKey (const uint8_t * key) const
2018-07-31 19:41:13 +00:00
{
if (!key) return nullptr;
2018-08-04 12:47:58 +00:00
return GetAddress (
[key](std::shared_ptr<const RouterInfo::Address> address)->bool
2021-01-30 23:32:17 +00:00
{
return address->IsNTCP2 () && !memcmp (address->ntcp2->staticKey, key, 32);
2018-08-04 12:47:58 +00:00
});
2018-07-31 19:41:13 +00:00
}
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetPublishedNTCP2V4Address () const
{
return GetAddress (
[](std::shared_ptr<const RouterInfo::Address> address)->bool
{
return address->IsPublishedNTCP2 () && address->host.is_v4 ();
});
}
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetPublishedNTCP2V6Address () const
{
return GetAddress (
[](std::shared_ptr<const RouterInfo::Address> address)->bool
{
2021-02-01 23:00:03 +00:00
return address->IsPublishedNTCP2 () && address->host.is_v6 () &&
!i2p::util::net::IsYggdrasilAddress (address->host);
});
}
2021-01-31 22:25:07 +00:00
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetYggdrasilAddress () const
{
return GetAddress (
[](std::shared_ptr<const RouterInfo::Address> address)->bool
{
2021-02-02 03:24:51 +00:00
return address->IsPublishedNTCP2 () && i2p::util::net::IsYggdrasilAddress (address->host);
2021-01-31 22:25:07 +00:00
});
}
2018-01-06 03:48:51 +00:00
std::shared_ptr<RouterProfile> RouterInfo::GetProfile () const
{
if (!m_Profile)
m_Profile = GetRouterProfile (GetIdentHash ());
return m_Profile;
2018-01-06 03:48:51 +00:00
}
void RouterInfo::Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) const
{
auto encryptor = m_RouterIdentity->CreateEncryptor (nullptr);
if (encryptor)
encryptor->Encrypt (data, encrypted, ctx, true);
}
2020-11-23 17:49:18 +00:00
bool RouterInfo::IsEligibleFloodfill () const
{
2021-03-02 19:13:28 +00:00
// floodfill must be reachable somehow, >= 0.9.28 and not DSA
return (IsReachable () || (m_SupportedTransports & eSSUV4)) &&
m_Version >= NETDB_MIN_FLOODFILL_VERSION &&
2020-11-23 17:49:18 +00:00
GetIdentity ()->GetSigningKeyType () != SIGNING_KEY_TYPE_DSA_SHA1;
}
2021-02-23 02:04:26 +00:00
2021-03-23 19:36:57 +00:00
bool RouterInfo::IsPeerTesting (bool v4) const
2021-02-23 02:04:26 +00:00
{
2021-03-30 15:31:11 +00:00
if (!(m_SupportedTransports & (v4 ? eSSUV4 : eSSUV6))) return false;
return (bool)GetAddress (
2021-03-23 19:36:57 +00:00
[v4](std::shared_ptr<const RouterInfo::Address> address)->bool
2021-02-23 02:04:26 +00:00
{
2021-03-23 19:36:57 +00:00
return (address->transportStyle == eTransportSSU) && address->IsPeerTesting () &&
((v4 && address->IsV4 ()) || (!v4 && address->IsV6 ()));
});
2021-02-23 02:04:26 +00:00
}
2021-03-30 15:31:11 +00:00
bool RouterInfo::IsIntroducer (bool v4) const
2021-02-23 02:04:26 +00:00
{
2021-03-30 15:31:11 +00:00
if (!(m_SupportedTransports & (v4 ? eSSUV4 : eSSUV6))) return false;
return (bool)GetAddress (
2021-03-30 15:31:11 +00:00
[v4](std::shared_ptr<const RouterInfo::Address> address)->bool
2021-02-23 02:04:26 +00:00
{
2021-03-30 15:31:11 +00:00
return (address->transportStyle == eTransportSSU) && address->IsIntroducer () &&
((v4 && address->IsV4 ()) || (!v4 && address->IsV6 ()));
});
}
bool RouterInfo::IsReachableFrom (const RouterInfo& other) const
{
auto commonTransports = m_SupportedTransports & other.m_SupportedTransports;
if (!commonTransports) return false;
if (commonTransports & eNTCP2V6Mesh) return true;
return (bool)GetAddress (
[commonTransports](std::shared_ptr<const RouterInfo::Address> address)->bool
{
2021-03-13 01:51:12 +00:00
if (address->IsPublishedNTCP2 ())
{
if ((commonTransports & eNTCP2V4) && address->IsV4 ()) return true;
if ((commonTransports & eNTCP2V6) && address->IsV6 ()) return true;
}
else if (address->IsReachableSSU ())
{
if ((commonTransports & eSSUV4) && address->IsV4 ()) return true;
if ((commonTransports & eSSUV6) && address->IsV6 ()) return true;
}
return false;
});
2021-02-23 02:04:26 +00:00
}
void RouterInfo::SetUnreachableAddressesTransportCaps (uint8_t transports)
{
for (auto& addr: *m_Addresses)
{
// TODO: implement SSU
if (addr->transportStyle == eTransportNTCP && (!addr->IsPublishedNTCP2 () || addr->port))
{
addr->caps &= ~(eV4 | eV6);
addr->caps |= transports;
}
}
}
2013-10-27 15:28:23 +00:00
}
}