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 {
("cpuext.force", bool_switch()->default_value(false), "Force usage of CPU extensions. Useful when cpuinfo is not available on virtual machines") ("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 m_OptionsDesc
.add(general) .add(general)
.add(limits) .add(limits)
@ -297,6 +302,7 @@ namespace config {
.add(nettime) .add(nettime)
.add(persist) .add(persist)
.add(cpuext) .add(cpuext)
.add(meshnets)
; ;
} }

2
libi2pd/NTCP2.cpp

@ -1181,7 +1181,7 @@ namespace transport
auto conn = std::make_shared<NTCP2Session>(*this); auto conn = std::make_shared<NTCP2Session>(*this);
m_NTCP2Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCP2Server::HandleAccept, this, conn, std::placeholders::_1)); 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 ())); m_NTCP2V6Acceptor.reset (new boost::asio::ip::tcp::acceptor (GetService ()));
try try

9
libi2pd/RouterContext.cpp

@ -521,6 +521,15 @@ namespace i2p
UpdateRouterInfo (); 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) void RouterContext::UpdateNTCP2V6Address (const boost::asio::ip::address& host)
{ {
bool updated = false; bool updated = false;

2
libi2pd/RouterContext.h

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

35
libi2pd/RouterInfo.cpp

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

4
libi2pd/RouterInfo.h

@ -155,6 +155,7 @@ namespace data
std::shared_ptr<const Address> GetPublishedNTCP2V6Address () const; std::shared_ptr<const Address> GetPublishedNTCP2V6Address () const;
std::shared_ptr<const Address> GetSSUAddress (bool v4only = true) const; std::shared_ptr<const Address> GetSSUAddress (bool v4only = true) const;
std::shared_ptr<const Address> GetSSUV6Address () 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 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); 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 IsNTCP2 (bool v4only = true) const;
bool IsV6 () const; bool IsV6 () const;
bool IsV4 () const; bool IsV4 () const;
bool IsMesh () const;
void EnableV6 (); void EnableV6 ();
void DisableV6 (); void DisableV6 ();
void EnableV4 (); void EnableV4 ();
void DisableV4 (); void DisableV4 ();
void EnableMesh ();
void DisableMesh ();
bool IsCompatible (const RouterInfo& other) const { return m_SupportedTransports & other.m_SupportedTransports; }; bool IsCompatible (const RouterInfo& other) const { return m_SupportedTransports & other.m_SupportedTransports; };
bool HasValidAddresses () const { return m_SupportedTransports; }; bool HasValidAddresses () const { return m_SupportedTransports; };
bool UsesIntroducer () const; bool UsesIntroducer () const;

16
libi2pd/Transports.cpp

@ -442,13 +442,27 @@ namespace transport
if (m_SSUServer && peer.router->IsSSU (!context.SupportsV6 ())) if (m_SSUServer && peer.router->IsSSU (!context.SupportsV6 ()))
{ {
auto address = peer.router->GetSSUAddress (!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); m_SSUServer->CreateSession (peer.router, address->host, address->port);
return true; 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"); LogPrint (eLogInfo, "Transports: No NTCP or SSU addresses available");
i2p::data::netdb.SetUnreachable (ident, true); // we are here because all connection attempts failed i2p::data::netdb.SetUnreachable (ident, true); // we are here because all connection attempts failed
peer.Done (); peer.Done ();

Loading…
Cancel
Save