i2pd/RouterInfo.cpp

659 lines
18 KiB
C++
Raw Normal View History

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 <cryptopp/sha.h>
#include <cryptopp/dsa.h>
#include "CryptoConst.h"
#include "base64.h"
#include "Timestamp.h"
#include "Log.h"
#include "RouterInfo.h"
#include "RouterContext.h"
2014-01-22 10:46:58 +04:00
2013-10-27 19:28:23 +04:00
namespace i2p
{
namespace data
{
2014-07-23 18:56:41 +04:00
RouterInfo::RouterInfo (const std::string& fullPath):
m_FullPath (fullPath), m_IsUpdated (false), m_IsUnreachable (false),
m_SupportedTransports (0), m_Caps (0)
2013-10-27 19:28:23 +04:00
{
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
2014-07-23 18:56:41 +04:00
ReadFromFile ();
2013-10-27 19:28:23 +04:00
}
2013-11-20 16:46:09 +04:00
RouterInfo::RouterInfo (const uint8_t * buf, int len):
2014-03-19 20:02:51 +04:00
m_IsUpdated (true), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0)
2013-10-27 19:28:23 +04:00
{
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
2013-10-27 19:28:23 +04:00
memcpy (m_Buffer, buf, len);
m_BufferLen = len;
ReadFromBuffer (true);
2013-10-27 19:28:23 +04:00
}
2014-07-22 04:14:11 +04:00
RouterInfo::~RouterInfo ()
{
delete m_Buffer;
}
2014-07-22 04:14:11 +04:00
void RouterInfo::Update (const uint8_t * buf, int len)
{
if (!m_Buffer)
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
2014-07-22 04:14:11 +04:00
m_IsUpdated = true;
m_IsUnreachable = false;
m_SupportedTransports = 0;
m_Caps = 0;
m_Addresses.clear ();
m_Properties.clear ();
memcpy (m_Buffer, buf, len);
m_BufferLen = len;
ReadFromBuffer (true);
// don't delete buffer until save to file
2014-07-22 04:14:11 +04:00
}
void RouterInfo::SetRouterIdentity (const Identity& identity)
2013-10-27 19:28:23 +04:00
{
m_RouterIdentity = identity;
m_IdentHash = m_RouterIdentity.Hash ();
2013-10-27 19:28:23 +04:00
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
}
2014-07-23 18:56:41 +04:00
bool RouterInfo::LoadFile ()
2013-10-27 19:28:23 +04:00
{
2014-07-23 18:56:41 +04:00
std::ifstream s(m_FullPath.c_str (), std::ifstream::binary);
2013-10-27 19:28:23 +04:00
if (s.is_open ())
{
s.seekg (0,std::ios::end);
2014-02-03 02:22:00 +04:00
m_BufferLen = s.tellg ();
if (m_BufferLen < 40)
{
2014-10-29 23:27:42 +03:00
LogPrint(eLogError, "File", m_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);
2014-07-23 18:56:41 +04:00
if (!m_Buffer)
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
2014-07-10 23:33:42 +04:00
s.read((char *)m_Buffer, m_BufferLen);
2013-10-27 19:28:23 +04:00
}
else
2014-07-23 18:56:41 +04:00
{
2014-10-29 23:27:42 +03:00
LogPrint (eLogError, "Can't open file ", m_FullPath);
2014-07-23 18:56:41 +04:00
return false;
}
return true;
}
void RouterInfo::ReadFromFile ()
{
if (LoadFile ())
ReadFromBuffer (false);
2013-10-27 19:28:23 +04:00
}
void RouterInfo::ReadFromBuffer (bool verifySignature)
2013-10-27 19:28:23 +04:00
{
2014-11-20 00:21:40 +03:00
size_t identityLen = DEFAULT_IDENTITY_SIZE;
memcpy (&m_RouterIdentity, m_Buffer, DEFAULT_IDENTITY_SIZE);
if (m_RouterIdentity.certificate.type != CERTIFICATE_TYPE_NULL)
{
LogPrint (eLogError, "Certificate type ", m_RouterIdentity.certificate.type, " is not supported");
SetUnreachable (true);
return;
}
std::stringstream str (std::string ((char *)m_Buffer + identityLen, m_BufferLen - identityLen));
2013-10-27 19:28:23 +04:00
ReadFromStream (str);
if (verifySignature)
2013-10-27 19:28:23 +04:00
{
// verify signature
CryptoPP::DSA::PublicKey pubKey;
pubKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, CryptoPP::Integer (m_RouterIdentity.signingKey, 128));
CryptoPP::DSA::Verifier verifier (pubKey);
int l = m_BufferLen - 40;
if (!verifier.VerifyMessage ((uint8_t *)m_Buffer, l, (uint8_t *)m_Buffer + l, 40))
{
2014-10-29 23:27:42 +03:00
LogPrint (eLogError, "signature verification failed");
}
2013-10-27 19:28:23 +04:00
}
}
void RouterInfo::ReadFromStream (std::istream& s)
{
s.read ((char *)&m_Timestamp, sizeof (m_Timestamp));
m_Timestamp = be64toh (m_Timestamp);
// read addresses
uint8_t numAddresses;
s.read ((char *)&numAddresses, sizeof (numAddresses));
2014-07-25 04:47:12 +04:00
bool introducers = false;
2013-10-27 19:28:23 +04:00
for (int i = 0; i < numAddresses; i++)
{
2014-07-25 04:47:12 +04:00
bool isValidAddress = true;
2013-10-27 19:28:23 +04:00
Address address;
s.read ((char *)&address.cost, sizeof (address.cost));
s.read ((char *)&address.date, sizeof (address.date));
char transportStyle[5];
ReadString (transportStyle, s);
if (!strcmp (transportStyle, "NTCP"))
address.transportStyle = eTransportNTCP;
else if (!strcmp (transportStyle, "SSU"))
address.transportStyle = eTransportSSU;
else
address.transportStyle = eTransportUnknown;
2014-09-15 01:57:47 +04:00
address.port = 0;
address.mtu = 0;
2013-10-27 19:28:23 +04:00
uint16_t size, r = 0;
s.read ((char *)&size, sizeof (size));
size = be16toh (size);
while (r < size)
{
2014-01-22 06:02:22 +04:00
char key[500], value[500];
2013-10-27 19:28:23 +04:00
r += ReadString (key, s);
s.seekg (1, std::ios_base::cur); r++; // =
r += ReadString (value, s);
s.seekg (1, std::ios_base::cur); r++; // ;
if (!strcmp (key, "host"))
2014-01-22 03:01:11 +04:00
{
boost::system::error_code ecode;
address.host = boost::asio::ip::address::from_string (value, ecode);
if (ecode)
{
// TODO: we should try to resolve address here
2014-10-29 23:27:42 +03:00
LogPrint (eLogWarning, "Unexpected address ", value);
2014-07-25 04:47:12 +04:00
isValidAddress = false;
}
else
{
// add supported protocol
if (address.host.is_v4 ())
m_SupportedTransports |= (address.transportStyle == eTransportNTCP) ? eNTCPV4 : eSSUV4;
else
m_SupportedTransports |= (address.transportStyle == eTransportNTCP) ? eNTCPV6 : eSSUV6;
}
2014-01-22 03:01:11 +04:00
}
2013-10-27 19:28:23 +04:00
else if (!strcmp (key, "port"))
address.port = boost::lexical_cast<int>(value);
2014-09-15 01:57:47 +04:00
else if (!strcmp (key, "mtu"))
address.mtu = boost::lexical_cast<int>(value);
2014-01-28 04:48:46 +04:00
else if (!strcmp (key, "key"))
Base64ToByteStream (value, strlen (value), address.key, 32);
2014-04-09 05:56:34 +04:00
else if (!strcmp (key, "caps"))
ExtractCaps (value);
2014-02-21 01:15:12 +04:00
else if (key[0] == 'i')
{
// introducers
2014-07-25 04:47:12 +04:00
introducers = true;
2014-02-21 01:15:12 +04: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;
if (index >= address.introducers.size ())
address.introducers.resize (index + 1);
Introducer& introducer = address.introducers.at (index);
if (!strcmp (key, "ihost"))
{
boost::system::error_code ecode;
introducer.iHost = boost::asio::ip::address::from_string (value, ecode);
}
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);
}
2013-10-27 19:28:23 +04:00
}
2014-07-25 04:47:12 +04:00
if (isValidAddress)
m_Addresses.push_back(address);
2013-10-27 19:28:23 +04:00
}
// read peers
uint8_t numPeers;
s.read ((char *)&numPeers, sizeof (numPeers));
s.seekg (numPeers*32, std::ios_base::cur); // TODO: read peers
// read properties
uint16_t size, r = 0;
s.read ((char *)&size, sizeof (size));
size = be16toh (size);
while (r < size)
{
2014-01-22 16:51:55 +04:00
#ifdef _WIN32
2014-01-22 06:02:22 +04:00
char key[500], value[500];
2014-01-22 16:51:55 +04:00
// TODO: investigate why properties get read as one long string under Windows
// length should not be more than 44
#else
char key[50], value[50];
#endif
2013-10-27 19:28:23 +04:00
r += ReadString (key, s);
s.seekg (1, std::ios_base::cur); r++; // =
r += ReadString (value, s);
s.seekg (1, std::ios_base::cur); r++; // ;
m_Properties[key] = value;
2014-03-19 20:02:51 +04:00
// extract caps
2014-03-20 01:27:37 +04:00
if (!strcmp (key, "caps"))
2014-03-19 20:02:51 +04:00
ExtractCaps (value);
2013-10-27 19:28:23 +04:00
}
CryptoPP::SHA256().CalculateDigest(m_IdentHash, (uint8_t *)&m_RouterIdentity, sizeof (m_RouterIdentity));
2014-07-25 04:47:12 +04:00
if (!m_SupportedTransports || !m_Addresses.size() || (UsesIntroducer () && !introducers))
SetUnreachable (true);
2013-12-29 19:48:57 +04:00
}
2014-03-19 20:02:51 +04:00
void RouterInfo::ExtractCaps (const char * value)
{
const char * cap = value;
while (*cap)
{
switch (*cap)
{
2014-09-03 00:11:31 +04:00
case CAPS_FLAG_FLOODFILL:
2014-03-19 20:02:51 +04:00
m_Caps |= Caps::eFloodfill;
break;
2014-09-03 00:11:31 +04:00
case CAPS_FLAG_HIGH_BANDWIDTH1:
case CAPS_FLAG_HIGH_BANDWIDTH2:
case CAPS_FLAG_HIGH_BANDWIDTH3:
2014-03-19 23:58:57 +04:00
m_Caps |= Caps::eHighBandwidth;
2014-03-19 20:02:51 +04:00
break;
2014-09-03 00:11:31 +04:00
case CAPS_FLAG_HIDDEN:
m_Caps |= Caps::eHidden;
break;
case CAPS_FLAG_REACHABLE:
2014-03-19 20:02:51 +04:00
m_Caps |= Caps::eReachable;
break;
2014-09-03 00:11:31 +04:00
case CAPS_FLAG_UNREACHABLE:
m_Caps |= Caps::eUnreachable;
break;
case CAPS_FLAG_SSU_TESTING:
2014-04-09 05:56:34 +04:00
m_Caps |= Caps::eSSUTesting;
break;
2014-09-03 00:11:31 +04:00
case CAPS_FLAG_SSU_INTRODUCER:
2014-04-09 05:56:34 +04:00
m_Caps |= Caps::eSSUIntroducer;
2014-06-18 18:41:59 +04:00
break;
2014-03-19 20:02:51 +04:00
default: ;
}
cap++;
}
}
2014-09-03 00:11:31 +04:00
void RouterInfo::UpdateCapsProperty ()
{
std::string caps;
caps += (m_Caps & eHighBandwidth) ? CAPS_FLAG_HIGH_BANDWIDTH1 : CAPS_FLAG_LOW_BANDWIDTH2; // bandwidth
if (m_Caps & eFloodfill) caps += CAPS_FLAG_FLOODFILL; // floodfill
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
SetProperty ("caps", caps.c_str ());
}
2013-12-29 19:48:57 +04:00
2013-10-27 19:28:23 +04:00
void RouterInfo::WriteToStream (std::ostream& s)
{
uint64_t ts = htobe64 (m_Timestamp);
s.write ((char *)&ts, sizeof (ts));
// addresses
uint8_t numAddresses = m_Addresses.size ();
s.write ((char *)&numAddresses, sizeof (numAddresses));
for (auto& address : m_Addresses)
{
s.write ((char *)&address.cost, sizeof (address.cost));
s.write ((char *)&address.date, sizeof (address.date));
2014-02-23 20:48:09 +04:00
std::stringstream properties;
2013-10-27 19:28:23 +04:00
if (address.transportStyle == eTransportNTCP)
WriteString ("NTCP", s);
else if (address.transportStyle == eTransportSSU)
2014-02-23 20:48:09 +04:00
{
2013-10-27 19:28:23 +04:00
WriteString ("SSU", s);
2014-02-24 05:48:28 +04:00
// caps
WriteString ("caps", properties);
2014-02-23 20:48:09 +04:00
properties << '=';
2014-04-09 05:56:34 +04:00
std::string caps;
2014-09-03 00:11:31 +04:00
if (IsPeerTesting ()) caps += CAPS_FLAG_SSU_TESTING;
if (IsIntroducer ()) caps += CAPS_FLAG_SSU_INTRODUCER;
2014-04-09 05:56:34 +04:00
WriteString (caps, properties);
2014-02-23 20:48:09 +04:00
properties << ';';
}
2013-10-27 19:28:23 +04:00
else
WriteString ("", s);
WriteString ("host", properties);
properties << '=';
2014-01-22 01:07:16 +04:00
WriteString (address.host.to_string (), properties);
2013-10-27 19:28:23 +04:00
properties << ';';
2014-02-24 05:48:28 +04:00
if (address.transportStyle == eTransportSSU)
{
2014-09-02 01:34:20 +04:00
// write introducers if any
if (address.introducers.size () > 0)
{
int i = 0;
for (auto introducer: address.introducers)
{
WriteString ("ihost" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
WriteString (introducer.iHost.to_string (), properties);
properties << ';';
i++;
}
i = 0;
for (auto introducer: address.introducers)
{
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++;
}
i = 0;
for (auto introducer: address.introducers)
{
WriteString ("iport" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
WriteString (boost::lexical_cast<std::string>(introducer.iPort), properties);
properties << ';';
i++;
}
i = 0;
for (auto introducer: address.introducers)
{
WriteString ("itag" + boost::lexical_cast<std::string>(i), properties);
properties << '=';
WriteString (boost::lexical_cast<std::string>(introducer.iTag), properties);
properties << ';';
i++;
}
}
// write intro key
2014-02-24 05:48:28 +04:00
WriteString ("key", properties);
properties << '=';
char value[64];
size_t l = ByteStreamToBase64 (address.key, 32, value, 64);
value[l] = 0;
WriteString (value, properties);
properties << ';';
2014-10-30 17:07:39 +03:00
// write mtu
if (address.mtu)
{
WriteString ("mtu", properties);
properties << '=';
WriteString (boost::lexical_cast<std::string>(address.mtu), properties);
properties << ';';
}
2014-02-24 05:48:28 +04:00
}
2013-10-27 19:28:23 +04:00
WriteString ("port", properties);
properties << '=';
WriteString (boost::lexical_cast<std::string>(address.port), 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 (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 ());
}
const uint8_t * RouterInfo::LoadBuffer ()
2014-07-23 18:56:41 +04:00
{
if (!m_Buffer)
{
if (LoadFile ())
2014-10-26 05:23:28 +03:00
LogPrint ("Buffer for ", GetIdentHashAbbreviation (), " loaded from file");
2014-07-23 18:56:41 +04:00
}
return m_Buffer;
}
2014-08-26 06:47:12 +04:00
void RouterInfo::CreateBuffer (const PrivateKeys& privateKeys)
2013-10-27 19:28:23 +04:00
{
2013-11-20 16:46:09 +04:00
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); // refresh timstamp
2013-10-27 19:28:23 +04:00
std::stringstream s;
uint8_t ident[1024];
auto identLen = privateKeys.GetPublic ().ToBuffer (ident, 1024);
s.write ((char *)ident, identLen);
2013-10-27 19:28:23 +04:00
WriteToStream (s);
m_BufferLen = s.str ().size ();
2014-07-22 16:03:02 +04:00
if (!m_Buffer)
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
2013-10-27 19:28:23 +04:00
memcpy (m_Buffer, s.str ().c_str (), m_BufferLen);
// signature
2014-08-26 06:47:12 +04:00
privateKeys.Sign ((uint8_t *)m_Buffer, m_BufferLen, (uint8_t *)m_Buffer + m_BufferLen);
m_BufferLen += privateKeys.GetPublic ().GetSignatureLen ();
2013-10-27 19:28:23 +04:00
}
2014-07-10 23:33:42 +04:00
void RouterInfo::SaveToFile (const std::string& fullPath)
{
2014-07-23 18:56:41 +04:00
m_FullPath = fullPath;
if (m_Buffer)
{
std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);
f.write ((char *)m_Buffer, m_BufferLen);
}
else
2014-10-29 23:27:42 +03:00
LogPrint (eLogError, "Can't save to file");
2014-07-10 23:33:42 +04:00
}
2013-10-27 19:28:23 +04:00
size_t RouterInfo::ReadString (char * str, std::istream& s)
{
uint8_t len;
s.read ((char *)&len, 1);
s.read (str, len);
str[len] = 0;
return len+1;
}
void RouterInfo::WriteString (const std::string& str, std::ostream& s)
{
uint8_t len = str.size ();
s.write ((char *)&len, 1);
s.write (str.c_str (), len);
}
void RouterInfo::AddNTCPAddress (const char * host, int port)
{
Address addr;
2014-01-22 01:07:16 +04:00
addr.host = boost::asio::ip::address::from_string (host);
2013-10-27 19:28:23 +04:00
addr.port = port;
addr.transportStyle = eTransportNTCP;
addr.cost = 2;
addr.date = 0;
2014-10-30 17:07:39 +03:00
addr.mtu = 0;
2013-10-27 19:28:23 +04:00
m_Addresses.push_back(addr);
m_SupportedTransports |= addr.host.is_v6 () ? eNTCPV6 : eNTCPV4;
2013-10-27 19:28:23 +04:00
}
2014-10-30 17:07:39 +03:00
void RouterInfo::AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu)
2014-02-23 20:48:09 +04:00
{
Address addr;
addr.host = boost::asio::ip::address::from_string (host);
addr.port = port;
addr.transportStyle = eTransportSSU;
2014-04-09 05:56:34 +04:00
addr.cost = 10; // NTCP should have priority over SSU
2014-02-23 20:48:09 +04:00
addr.date = 0;
2014-10-30 17:07:39 +03:00
addr.mtu = mtu;
2014-02-23 20:48:09 +04:00
memcpy (addr.key, key, 32);
m_Addresses.push_back(addr);
m_SupportedTransports |= addr.host.is_v6 () ? eNTCPV6 : eSSUV4;
2014-04-17 02:28:44 +04:00
m_Caps |= eSSUTesting;
m_Caps |= eSSUIntroducer;
2014-02-23 20:48:09 +04:00
}
2014-09-02 01:34:20 +04:00
bool RouterInfo::AddIntroducer (const Address * address, uint32_t tag)
{
for (auto& addr : m_Addresses)
{
if (addr.transportStyle == eTransportSSU && addr.host.is_v4 ())
{
for (auto intro: addr.introducers)
if (intro.iTag == tag) return false; // already presented
Introducer x;
x.iHost = address->host;
x.iPort = address->port;
x.iTag = tag;
memcpy (x.iKey, address->key, 32); // TODO: replace to Tag<32>
addr.introducers.push_back (x);
return true;
}
}
return false;
}
2014-09-07 04:43:20 +04:00
bool RouterInfo::RemoveIntroducer (const boost::asio::ip::udp::endpoint& e)
2014-09-02 01:34:20 +04:00
{
for (auto& addr : m_Addresses)
{
if (addr.transportStyle == eTransportSSU && addr.host.is_v4 ())
{
2014-09-15 19:22:22 +04:00
for (std::vector<Introducer>::iterator it = addr.introducers.begin (); it != addr.introducers.end (); it++)
2014-09-07 04:43:20 +04:00
if ( boost::asio::ip::udp::endpoint (it->iHost, it->iPort) == e)
2014-09-02 01:34:20 +04:00
{
addr.introducers.erase (it);
return true;
}
}
}
return false;
}
2014-09-03 00:11:31 +04:00
void RouterInfo::SetCaps (uint8_t caps)
{
m_Caps = caps;
UpdateCapsProperty ();
}
2014-09-02 01:34:20 +04:00
void RouterInfo::SetCaps (const char * caps)
{
SetProperty ("caps", caps);
m_Caps = 0;
ExtractCaps (caps);
}
2014-02-23 20:48:09 +04:00
2013-10-27 19:28:23 +04:00
void RouterInfo::SetProperty (const char * key, const char * value)
{
m_Properties[key] = value;
}
const char * RouterInfo::GetProperty (const char * key) const
{
auto it = m_Properties.find (key);
if (it != m_Properties.end ())
return it->second.c_str ();
return 0;
}
bool RouterInfo::IsFloodfill () const
{
2014-03-19 20:02:51 +04:00
return m_Caps & Caps::eFloodfill;
2013-10-27 19:28:23 +04:00
}
2014-01-22 04:14:30 +04:00
bool RouterInfo::IsNTCP (bool v4only) const
2013-10-27 19:28:23 +04:00
{
if (v4only)
return m_SupportedTransports & eNTCPV4;
else
return m_SupportedTransports & (eNTCPV4 | eNTCPV6);
}
2014-02-09 06:06:40 +04:00
bool RouterInfo::IsSSU (bool v4only) const
{
if (v4only)
return m_SupportedTransports & eSSUV4;
else
return m_SupportedTransports & (eSSUV4 | eSSUV6);
}
2014-02-22 01:13:36 +04:00
bool RouterInfo::IsV6 () const
{
return m_SupportedTransports & (eNTCPV6 | eSSUV6);
}
void RouterInfo::EnableV6 ()
{
if (!IsV6 ())
2014-10-30 01:46:35 +03:00
m_SupportedTransports |= eNTCPV6 | eSSUV6;
}
void RouterInfo::DisableV6 ()
{
if (IsV6 ())
{
2014-10-29 23:08:01 +03:00
// NTCP
m_SupportedTransports &= ~eNTCPV6;
for (size_t i = 0; i < m_Addresses.size (); i++)
{
if (m_Addresses[i].transportStyle == i2p::data::RouterInfo::eTransportNTCP &&
m_Addresses[i].host.is_v6 ())
{
m_Addresses.erase (m_Addresses.begin () + i);
break;
}
}
2014-10-29 23:08:01 +03:00
// SSU
m_SupportedTransports &= ~eSSUV6;
for (size_t i = 0; i < m_Addresses.size (); i++)
{
if (m_Addresses[i].transportStyle == i2p::data::RouterInfo::eTransportSSU &&
m_Addresses[i].host.is_v6 ())
{
m_Addresses.erase (m_Addresses.begin () + i);
break;
}
}
}
}
2014-02-22 01:13:36 +04:00
bool RouterInfo::UsesIntroducer () const
{
2014-08-19 19:01:11 +04:00
return m_Caps & Caps::eUnreachable; // non-reachable
2014-02-22 01:13:36 +04:00
}
2014-02-09 17:52:56 +04:00
const RouterInfo::Address * RouterInfo::GetNTCPAddress (bool v4only) const
2014-01-28 04:48:46 +04:00
{
return GetAddress (eTransportNTCP, v4only);
}
2014-02-09 17:52:56 +04:00
const RouterInfo::Address * RouterInfo::GetSSUAddress (bool v4only) const
2014-01-28 04:48:46 +04:00
{
return GetAddress (eTransportSSU, v4only);
}
const RouterInfo::Address * RouterInfo::GetSSUV6Address () const
{
return GetAddress (eTransportSSU, false, true);
}
const RouterInfo::Address * RouterInfo::GetAddress (TransportStyle s, bool v4only, bool v6only) const
2013-10-27 19:28:23 +04:00
{
for (auto& address : m_Addresses)
{
2014-01-28 04:48:46 +04:00
if (address.transportStyle == s)
2014-01-22 04:14:30 +04:00
{
if ((!v4only || address.host.is_v4 ()) && (!v6only || address.host.is_v6 ()))
2014-01-22 04:14:30 +04:00
return &address;
}
2013-10-27 19:28:23 +04:00
}
return nullptr;
}
}
}