Browse Source

NTCP2 transports through the Yggdrasil

pull/1634/head
orignal 4 years ago
parent
commit
aad2d68edb
  1. 6
      libi2pd/Config.cpp
  2. 2
      libi2pd/NTCP2.cpp
  3. 9
      libi2pd/RouterContext.cpp
  4. 2
      libi2pd/RouterContext.h
  5. 35
      libi2pd/RouterInfo.cpp
  6. 4
      libi2pd/RouterInfo.h
  7. 16
      libi2pd/Transports.cpp

6
libi2pd/Config.cpp

@ -276,6 +276,11 @@ namespace config { @@ -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 { @@ -297,6 +302,7 @@ namespace config {
.add(nettime)
.add(persist)
.add(cpuext)
.add(meshnets)
;
}

2
libi2pd/NTCP2.cpp

@ -1181,7 +1181,7 @@ namespace transport @@ -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

9
libi2pd/RouterContext.cpp

@ -521,6 +521,15 @@ namespace i2p @@ -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;

2
libi2pd/RouterContext.h

@ -107,8 +107,10 @@ namespace i2p @@ -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; };

35
libi2pd/RouterInfo.cpp

@ -847,6 +847,11 @@ namespace data @@ -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 @@ -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
{
@ -958,6 +984,15 @@ namespace data @@ -958,6 +984,15 @@ namespace data
});
}
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
{
if (!m_Profile)

4
libi2pd/RouterInfo.h

@ -155,6 +155,7 @@ namespace data @@ -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 @@ -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;

16
libi2pd/Transports.cpp

@ -442,13 +442,27 @@ namespace transport @@ -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 ();

Loading…
Cancel
Save