From 3236827781f303700009010718804bd03a2dc4c3 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 16 Apr 2019 21:04:04 -0400 Subject: [PATCH] add/removed NTCP addresses --- daemon/Daemon.cpp | 7 +++- libi2pd/RouterContext.cpp | 78 +++++++++++++++++++++++++-------------- libi2pd/RouterContext.h | 1 + 3 files changed, 57 insertions(+), 29 deletions(-) diff --git a/daemon/Daemon.cpp b/daemon/Daemon.cpp index b693ec52..0022b186 100644 --- a/daemon/Daemon.cpp +++ b/daemon/Daemon.cpp @@ -154,14 +154,17 @@ namespace i2p i2p::context.SetSupportsV6 (ipv6); i2p::context.SetSupportsV4 (ipv4); + bool ntcp; i2p::config::GetOption("ntcp", ntcp); + i2p::context.PublishNTCPAddress (ntcp, !ipv6); bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2); if (ntcp2) { bool published; i2p::config::GetOption("ntcp2.published", published); if (published) { - uint16_t port; i2p::config::GetOption("ntcp2.port", port); - i2p::context.PublishNTCP2Address (port, true); // publish + uint16_t ntcp2port; i2p::config::GetOption("ntcp2.port", ntcp2port); + if (!ntcp && !ntcp2port) ntcp2port = port; // use standard port + i2p::context.PublishNTCP2Address (ntcp2port, true); // publish } else i2p::context.PublishNTCP2Address (port, false); // unpublish diff --git a/libi2pd/RouterContext.cpp b/libi2pd/RouterContext.cpp index 27523993..dacb66af 100644 --- a/libi2pd/RouterContext.cpp +++ b/libi2pd/RouterContext.cpp @@ -344,6 +344,51 @@ namespace i2p return m_RouterInfo.GetCaps () & i2p::data::RouterInfo::eUnreachable; } + void RouterContext::PublishNTCPAddress (bool publish, bool v4only) + { + auto& addresses = m_RouterInfo.GetAddresses (); + if (publish) + { + for (const auto& addr : addresses) // v4 + { + if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU && + addr->host.is_v4 ()) + { + // insert NTCP address with host/port from SSU + m_RouterInfo.AddNTCPAddress (addr->host.to_string ().c_str (), addr->port); + break; + } + } + if (!v4only) + { + for (const auto& addr : addresses) // v6 + { + if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU && + addr->host.is_v6 ()) + { + // insert NTCP address with host/port from SSU + m_RouterInfo.AddNTCPAddress (addr->host.to_string ().c_str (), addr->port); + break; + } + } + } + } + else + { + for (auto it = addresses.begin (); it != addresses.end ();) + { + if ((*it)->transportStyle == i2p::data::RouterInfo::eTransportNTCP && !(*it)->IsNTCP2 () && + (!v4only || (*it)->host.is_v4 ())) + { + it = addresses.erase (it); + if (v4only) break; // otherwise might be more than one address + } + else + ++it; + } + } + } + void RouterContext::SetUnreachable () { // set caps @@ -353,22 +398,13 @@ namespace i2p caps &= ~i2p::data::RouterInfo::eFloodfill; // can't be floodfill caps &= ~i2p::data::RouterInfo::eSSUIntroducer; // can't be introducer m_RouterInfo.SetCaps (caps); - // remove NTCP address - auto& addresses = m_RouterInfo.GetAddresses (); - for (auto it = addresses.begin (); it != addresses.end (); ++it) - { - if ((*it)->transportStyle == i2p::data::RouterInfo::eTransportNTCP && !(*it)->IsNTCP2 () && - (*it)->host.is_v4 ()) - { - addresses.erase (it); - break; - } - } + // remove NTCP v4 address + PublishNTCPAddress (false); // delete previous introducers + auto& addresses = m_RouterInfo.GetAddresses (); for (auto& addr : addresses) if (addr->ssu) addr->ssu->introducers.clear (); - // update UpdateRouterInfo (); } @@ -383,27 +419,15 @@ namespace i2p if (m_IsFloodfill) caps |= i2p::data::RouterInfo::eFloodfill; m_RouterInfo.SetCaps (caps); - - auto& addresses = m_RouterInfo.GetAddresses (); // insert NTCP back bool ntcp; i2p::config::GetOption("ntcp", ntcp); - if (ntcp) { - for (const auto& addr : addresses) - { - if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU && - addr->host.is_v4 ()) - { - // insert NTCP address with host/port from SSU - m_RouterInfo.AddNTCPAddress (addr->host.to_string ().c_str (), addr->port); - break; - } - } - } + if (ntcp) + PublishNTCPAddress (true); // delete previous introducers + auto& addresses = m_RouterInfo.GetAddresses (); for (auto& addr : addresses) if (addr->ssu) addr->ssu->introducers.clear (); - // update UpdateRouterInfo (); } diff --git a/libi2pd/RouterContext.h b/libi2pd/RouterContext.h index f65bc702..19fe08b2 100644 --- a/libi2pd/RouterContext.h +++ b/libi2pd/RouterContext.h @@ -81,6 +81,7 @@ namespace i2p void UpdateAddress (const boost::asio::ip::address& host); // called from SSU or Daemon void PublishNTCP2Address (int port, bool publish = true); void UpdateNTCP2Address (bool enable); + void PublishNTCPAddress (bool publish, bool v4only = true); bool AddIntroducer (const i2p::data::RouterInfo::Introducer& introducer); void RemoveIntroducer (const boost::asio::ip::udp::endpoint& e); bool IsUnreachable () const;