Browse Source

don't support NTCP1 address in RouterInfo

pull/1634/head
orignal 4 years ago
parent
commit
129b4a2135
  1. 60
      libi2pd/RouterInfo.cpp
  2. 10
      libi2pd/RouterInfo.h

60
libi2pd/RouterInfo.cpp

@ -198,13 +198,13 @@ namespace data
auto address = std::make_shared<Address>(); auto address = std::make_shared<Address>();
s.read ((char *)&address->cost, sizeof (address->cost)); s.read ((char *)&address->cost, sizeof (address->cost));
s.read ((char *)&address->date, sizeof (address->date)); s.read ((char *)&address->date, sizeof (address->date));
bool isNTCP2Only = false; bool isHost = false, isIntroKey = false, isStaticKey = false;
char transportStyle[6]; char transportStyle[6];
auto transportStyleLen = ReadString (transportStyle, 6, s) - 1; ReadString (transportStyle, 6, s);
if (!strncmp (transportStyle, "NTCP", 4)) // NTCP or NTCP2 if (!strncmp (transportStyle, "NTCP", 4)) // NTCP or NTCP2
{ {
address->transportStyle = eTransportNTCP; address->transportStyle = eTransportNTCP;
if (transportStyleLen > 4 && transportStyle[4] == '2') isNTCP2Only= true; address->ntcp2.reset (new NTCP2Ext ());
} }
else if (!strcmp (transportStyle, "SSU")) else if (!strcmp (transportStyle, "SSU"))
{ {
@ -230,22 +230,7 @@ namespace data
{ {
boost::system::error_code ecode; boost::system::error_code ecode;
address->host = boost::asio::ip::address::from_string (value, ecode); address->host = boost::asio::ip::address::from_string (value, ecode);
if (!ecode) if (!ecode && !address->host.is_unspecified ()) isHost = true;
{
#if BOOST_VERSION >= 104900
if (!address->host.is_unspecified ()) // check if address is valid
#else
address->host.to_string (ecode);
if (!ecode)
#endif
{
// add supported protocol
if (address->host.is_v4 ())
supportedTransports |= (address->transportStyle == eTransportNTCP) ? eNTCPV4 : eSSUV4;
else
supportedTransports |= (address->transportStyle == eTransportNTCP) ? eNTCPV6 : eSSUV6;
}
}
} }
else if (!strcmp (key, "port")) else if (!strcmp (key, "port"))
address->port = boost::lexical_cast<int>(value); address->port = boost::lexical_cast<int>(value);
@ -259,7 +244,7 @@ namespace data
else if (!strcmp (key, "key")) else if (!strcmp (key, "key"))
{ {
if (address->ssu) if (address->ssu)
Base64ToByteStream (value, strlen (value), address->ssu->key, 32); isIntroKey = (Base64ToByteStream (value, strlen (value), address->ssu->key, 32) == 32);
else else
LogPrint (eLogWarning, "RouterInfo: Unexpected field 'key' for NTCP"); LogPrint (eLogWarning, "RouterInfo: Unexpected field 'key' for NTCP");
} }
@ -267,14 +252,11 @@ namespace data
ExtractCaps (value); ExtractCaps (value);
else if (!strcmp (key, "s")) // ntcp2 static key else if (!strcmp (key, "s")) // ntcp2 static key
{ {
if (!address->ntcp2) address->ntcp2.reset (new NTCP2Ext ());
supportedTransports |= (address->host.is_v4 ()) ? eNTCP2V4 : eNTCP2V6;
Base64ToByteStream (value, strlen (value), address->ntcp2->staticKey, 32); Base64ToByteStream (value, strlen (value), address->ntcp2->staticKey, 32);
isStaticKey = true;
} }
else if (!strcmp (key, "i")) // ntcp2 iv else if (!strcmp (key, "i")) // ntcp2 iv
{ {
if (!address->ntcp2) address->ntcp2.reset (new NTCP2Ext ());
supportedTransports |= (address->host.is_v4 ()) ? eNTCP2V4 : eNTCP2V6;
Base64ToByteStream (value, strlen (value), address->ntcp2->iv, 16); Base64ToByteStream (value, strlen (value), address->ntcp2->iv, 16);
address->ntcp2->isPublished = true; // presence if "i" means "published" address->ntcp2->isPublished = true; // presence if "i" means "published"
} }
@ -314,9 +296,22 @@ namespace data
} }
if (!s) return; if (!s) return;
} }
if (address->transportStyle == eTransportNTCP)
{
if (isStaticKey && isHost)
supportedTransports |= address->host.is_v4 () ? eNTCP2V4 : eNTCP2V6;
}
else if (address->transportStyle == eTransportSSU)
{
if (isIntroKey)
{
if (isHost)
supportedTransports |= address->host.is_v4 () ? eSSUV4 : eSSUV6;
else
if (introducers) supportedTransports |= eSSUV4; // in case if host is not presented if (introducers) supportedTransports |= eSSUV4; // in case if host is not presented
if (isNTCP2Only && address->ntcp2) address->ntcp2->isNTCP2Only = true; }
if (supportedTransports & ~(eNTCPV4 | eNTCPV6)) // exclude NTCP only }
if (supportedTransports)
{ {
addresses->push_back(address); addresses->push_back(address);
m_SupportedTransports |= supportedTransports; m_SupportedTransports |= supportedTransports;
@ -741,7 +736,6 @@ namespace data
addr->cost = port ? 3 : 14; // override from RouterContext::PublishNTCP2Address addr->cost = port ? 3 : 14; // override from RouterContext::PublishNTCP2Address
addr->date = 0; addr->date = 0;
addr->ntcp2.reset (new NTCP2Ext ()); addr->ntcp2.reset (new NTCP2Ext ());
addr->ntcp2->isNTCP2Only = true; // NTCP2 only address
if (port) addr->ntcp2->isPublished = true; if (port) addr->ntcp2->isPublished = true;
memcpy (addr->ntcp2->staticKey, staticKey, 32); memcpy (addr->ntcp2->staticKey, staticKey, 32);
memcpy (addr->ntcp2->iv, iv, 16); memcpy (addr->ntcp2->iv, iv, 16);
@ -834,24 +828,24 @@ namespace data
bool RouterInfo::IsV6 () const bool RouterInfo::IsV6 () const
{ {
return m_SupportedTransports & (eNTCPV6 | eSSUV6 | eNTCP2V6); return m_SupportedTransports & (eSSUV6 | eNTCP2V6);
} }
bool RouterInfo::IsV4 () const bool RouterInfo::IsV4 () const
{ {
return m_SupportedTransports & (eNTCPV4 | eSSUV4 | eNTCP2V4); return m_SupportedTransports & (eSSUV4 | eNTCP2V4);
} }
void RouterInfo::EnableV6 () void RouterInfo::EnableV6 ()
{ {
if (!IsV6 ()) if (!IsV6 ())
m_SupportedTransports |= eNTCPV6 | eSSUV6 | eNTCP2V6; m_SupportedTransports |= eSSUV6 | eNTCP2V6;
} }
void RouterInfo::EnableV4 () void RouterInfo::EnableV4 ()
{ {
if (!IsV4 ()) if (!IsV4 ())
m_SupportedTransports |= eNTCPV4 | eSSUV4 | eNTCP2V4; m_SupportedTransports |= eSSUV4 | eNTCP2V4;
} }
@ -859,7 +853,7 @@ namespace data
{ {
if (IsV6 ()) if (IsV6 ())
{ {
m_SupportedTransports &= ~(eNTCPV6 | eSSUV6 | eNTCP2V6); m_SupportedTransports &= ~(eSSUV6 | eNTCP2V6);
for (auto it = m_Addresses->begin (); it != m_Addresses->end ();) for (auto it = m_Addresses->begin (); it != m_Addresses->end ();)
{ {
auto addr = *it; auto addr = *it;
@ -875,7 +869,7 @@ namespace data
{ {
if (IsV4 ()) if (IsV4 ())
{ {
m_SupportedTransports &= ~(eNTCPV4 | eSSUV4 | eNTCP2V4); m_SupportedTransports &= ~(eSSUV4 | eNTCP2V4);
for (auto it = m_Addresses->begin (); it != m_Addresses->end ();) for (auto it = m_Addresses->begin (); it != m_Addresses->end ();)
{ {
auto addr = *it; auto addr = *it;

10
libi2pd/RouterInfo.h

@ -54,12 +54,10 @@ namespace data
enum SupportedTranports enum SupportedTranports
{ {
eNTCPV4 = 0x01, eNTCP2V4 = 0x01,
eNTCPV6 = 0x02, eNTCP2V6 = 0x02,
eSSUV4 = 0x04, eSSUV4 = 0x04,
eSSUV6 = 0x08, eSSUV6 = 0x08
eNTCP2V4 = 0x10,
eNTCP2V6 = 0x20
}; };
enum Caps enum Caps
@ -104,7 +102,6 @@ namespace data
Tag<32> staticKey; Tag<32> staticKey;
Tag<16> iv; Tag<16> iv;
bool isPublished = false; bool isPublished = false;
bool isNTCP2Only = false;
}; };
struct Address struct Address
@ -136,7 +133,6 @@ namespace data
bool IsNTCP2 () const { return (bool)ntcp2; }; bool IsNTCP2 () const { return (bool)ntcp2; };
bool IsPublishedNTCP2 () const { return IsNTCP2 () && ntcp2->isPublished; }; bool IsPublishedNTCP2 () const { return IsNTCP2 () && ntcp2->isPublished; };
bool IsNTCP2Only () const { return ntcp2 && ntcp2->isNTCP2Only; };
}; };
typedef std::list<std::shared_ptr<Address> > Addresses; typedef std::list<std::shared_ptr<Address> > Addresses;

Loading…
Cancel
Save