From f7415c8a8fa9942f48ee089c5035dd2155081d18 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 3 Aug 2018 14:49:28 -0400 Subject: [PATCH] enable/disable NTCP2 address --- daemon/Daemon.cpp | 4 +++- libi2pd/RouterContext.cpp | 41 ++++++++++++++++++++++++++++++++------- libi2pd/RouterContext.h | 3 ++- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/daemon/Daemon.cpp b/daemon/Daemon.cpp index 5efdc693..1302ebe8 100644 --- a/daemon/Daemon.cpp +++ b/daemon/Daemon.cpp @@ -159,8 +159,10 @@ namespace i2p if (published) { uint16_t port; i2p::config::GetOption("ntcp2.port", port); - i2p::context.PublishNTCP2Address (port); + i2p::context.PublishNTCP2Address (port, true); // publish } + else + i2p::context.PublishNTCP2Address (port, false); // unpublish } bool transit; i2p::config::GetOption("notransit", transit); diff --git a/libi2pd/RouterContext.cpp b/libi2pd/RouterContext.cpp index 93cb741c..d34acc57 100644 --- a/libi2pd/RouterContext.cpp +++ b/libi2pd/RouterContext.cpp @@ -96,8 +96,7 @@ namespace i2p if (ntcp2) // TODO: should update routerInfo, but we ignore upublished NTCP2 addresses for now { NewNTCP2Keys (); - m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv); - UpdateRouterInfo (); + UpdateNTCP2Address (true); } } @@ -156,17 +155,17 @@ namespace i2p UpdateRouterInfo (); } - void RouterContext::PublishNTCP2Address (int port) + void RouterContext::PublishNTCP2Address (int port, bool publish) { if (!port) port = rand () % (30777 - 9111) + 9111; // I2P network ports range bool updated = false; for (auto& address : m_RouterInfo.GetAddresses ()) { - if (address->IsNTCP2 () && address->port != port) + if (address->IsNTCP2 () && (address->port != port || address->ntcp2->isPublished != publish)) { address->port = port; - address->ntcp2->isPublished = true; + address->ntcp2->isPublished = publish; updated = true; } } @@ -174,6 +173,32 @@ namespace i2p UpdateRouterInfo (); } + void RouterContext::UpdateNTCP2Address (bool enable) + { + auto& addresses = m_RouterInfo.GetAddresses (); + bool found = false, updated = false; + for (auto it = addresses.begin (); it != addresses.end (); ++it) + { + if ((*it)->IsNTCP2 ()) + { + found = true; + if (!enable) + { + addresses.erase (it); + updated= true; + } + break; + } + } + if (enable && !found) + { + m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv); + updated = true; + } + if (updated) + UpdateRouterInfo (); + } + void RouterContext::UpdateAddress (const boost::asio::ip::address& host) { bool updated = false; @@ -310,7 +335,7 @@ namespace i2p auto& addresses = m_RouterInfo.GetAddresses (); for (auto it = addresses.begin (); it != addresses.end (); ++it) { - if ((*it)->transportStyle == i2p::data::RouterInfo::eTransportNTCP && + if ((*it)->transportStyle == i2p::data::RouterInfo::eTransportNTCP && !(*it)->IsNTCP2 () && (*it)->host.is_v4 ()) { addresses.erase (it); @@ -490,9 +515,11 @@ namespace i2p if (!m_NTCP2Keys) { NewNTCP2Keys (); - m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv); + UpdateNTCP2Address (true); // enable NTCP2 } } + else + UpdateNTCP2Address (false); // disable NTCP2 return true; } diff --git a/libi2pd/RouterContext.h b/libi2pd/RouterContext.h index b840e19a..f1a62c5a 100644 --- a/libi2pd/RouterContext.h +++ b/libi2pd/RouterContext.h @@ -78,7 +78,8 @@ namespace i2p void UpdatePort (int port); // called from Daemon void UpdateAddress (const boost::asio::ip::address& host); // called from SSU or Daemon - void PublishNTCP2Address (int port); + void PublishNTCP2Address (int port, bool publish = true); + void UpdateNTCP2Address (bool enable); bool AddIntroducer (const i2p::data::RouterInfo::Introducer& introducer); void RemoveIntroducer (const boost::asio::ip::udp::endpoint& e); bool IsUnreachable () const;