NTCP2 transports through the Yggdrasil

This commit is contained in:
orignal 2021-01-31 17:25:07 -05:00
parent 9e5935aea5
commit aad2d68edb
7 changed files with 72 additions and 2 deletions

View File

@ -276,6 +276,11 @@ namespace config {
("cpuext.force", bool_switch()->default_value(false), "Force usage of CPU extensions. Useful when cpuinfo is not available on virtual machines")
;
options_description meshnets("Meshnet transports options");
meshnets.add_options()
("meshnets.yggdrasil", bool_switch()->default_value(false), "Support transports through the Yggdrasil (deafult: false)")
;
m_OptionsDesc
.add(general)
.add(limits)
@ -297,6 +302,7 @@ namespace config {
.add(nettime)
.add(persist)
.add(cpuext)
.add(meshnets)
;
}

View File

@ -1181,7 +1181,7 @@ namespace transport
auto conn = std::make_shared<NTCP2Session>(*this);
m_NTCP2Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCP2Server::HandleAccept, this, conn, std::placeholders::_1));
}
else if (address->host.is_v6() && context.SupportsV6 ())
else if (address->host.is_v6() && (context.SupportsV6 () || context.SupportsMesh ()))
{
m_NTCP2V6Acceptor.reset (new boost::asio::ip::tcp::acceptor (GetService ()));
try

View File

@ -521,6 +521,15 @@ namespace i2p
UpdateRouterInfo ();
}
void RouterContext::SetSupportsMesh (bool supportsmesh)
{
if (supportsmesh)
m_RouterInfo.EnableMesh ();
else
m_RouterInfo.DisableMesh ();
UpdateRouterInfo ();
}
void RouterContext::UpdateNTCP2V6Address (const boost::asio::ip::address& host)
{
bool updated = false;

View File

@ -107,8 +107,10 @@ namespace i2p
void SetAcceptsTunnels (bool acceptsTunnels) { m_AcceptsTunnels = acceptsTunnels; };
bool SupportsV6 () const { return m_RouterInfo.IsV6 (); };
bool SupportsV4 () const { return m_RouterInfo.IsV4 (); };
bool SupportsMesh () const { return m_RouterInfo.IsMesh (); };
void SetSupportsV6 (bool supportsV6);
void SetSupportsV4 (bool supportsV4);
void SetSupportsMesh (bool supportsmesh);
bool IsECIES () const { return GetIdentity ()->GetCryptoKeyType () == i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD; };
std::unique_ptr<i2p::crypto::NoiseSymmetricState>& GetCurrentNoiseState () { return m_CurrentNoiseState; };

View File

@ -847,6 +847,11 @@ namespace data
return m_SupportedTransports & (eSSUV4 | eNTCP2V4);
}
bool RouterInfo::IsMesh () const
{
return m_SupportedTransports & eNTCP2V6Mesh;
}
void RouterInfo::EnableV6 ()
{
if (!IsV6 ())
@ -892,6 +897,27 @@ namespace data
}
}
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;
}
}
}
bool RouterInfo::UsesIntroducer () const
{
@ -957,6 +983,15 @@ namespace data
return address->IsPublishedNTCP2 () && address->host.is_v6 ();
});
}
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetYggdrasilAddress () const
{
return GetAddress (
[](std::shared_ptr<const RouterInfo::Address> address)->bool
{
return i2p::util::net::IsYggdrasilAddress (address->host);
});
}
std::shared_ptr<RouterProfile> RouterInfo::GetProfile () const
{

View File

@ -155,6 +155,7 @@ namespace data
std::shared_ptr<const Address> GetPublishedNTCP2V6Address () const;
std::shared_ptr<const Address> GetSSUAddress (bool v4only = true) const;
std::shared_ptr<const Address> GetSSUV6Address () const;
std::shared_ptr<const Address> GetYggdrasilAddress () const;
void AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu = 0);
void AddNTCP2Address (const uint8_t * staticKey, const uint8_t * iv, const boost::asio::ip::address& host = boost::asio::ip::address(), int port = 0);
@ -171,10 +172,13 @@ namespace data
bool IsNTCP2 (bool v4only = true) const;
bool IsV6 () const;
bool IsV4 () const;
bool IsMesh () const;
void EnableV6 ();
void DisableV6 ();
void EnableV4 ();
void DisableV4 ();
void EnableMesh ();
void DisableMesh ();
bool IsCompatible (const RouterInfo& other) const { return m_SupportedTransports & other.m_SupportedTransports; };
bool HasValidAddresses () const { return m_SupportedTransports; };
bool UsesIntroducer () const;

View File

@ -442,13 +442,27 @@ namespace transport
if (m_SSUServer && peer.router->IsSSU (!context.SupportsV6 ()))
{
auto address = peer.router->GetSSUAddress (!context.SupportsV6 ());
if (!m_CheckReserved || !i2p::util::net::IsInReservedRange(address->host))
if (address && (!m_CheckReserved || !i2p::util::net::IsInReservedRange(address->host)))
{
m_SSUServer->CreateSession (peer.router, address->host, address->port);
return true;
}
}
}
if (peer.numAttempts == 3) // Mesh
{
peer.numAttempts++;
if (context.SupportsMesh () && m_NTCP2Server)
{
auto address = peer.router->GetYggdrasilAddress ();
if (address)
{
auto s = std::make_shared<NTCP2Session> (*m_NTCP2Server, peer.router, address);
m_NTCP2Server->Connect (address->host, address->port, s);
return true;
}
}
}
LogPrint (eLogInfo, "Transports: No NTCP or SSU addresses available");
i2p::data::netdb.SetUnreachable (ident, true); // we are here because all connection attempts failed
peer.Done ();