From 354c9187db403e38a4c78cf224d387009b770c10 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 23 May 2019 15:59:44 -0400 Subject: [PATCH] detect our ipv6 address --- libi2pd/NetDb.cpp | 9 +++++++++ libi2pd/NetDb.hpp | 1 + libi2pd/RouterInfo.cpp | 5 +++++ libi2pd/RouterInfo.h | 1 + libi2pd/Transports.cpp | 14 ++++++++++++++ 5 files changed, 30 insertions(+) diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index 328f8550..952c8017 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -1076,6 +1076,15 @@ namespace data }); } + std::shared_ptr NetDb::GetRandomSSUV6Router () const + { + return GetRandomRouter ( + [](std::shared_ptr router)->bool + { + return !router->IsHidden () && router->IsSSUV6 (); + }); + } + std::shared_ptr NetDb::GetRandomIntroducer () const { return GetRandomRouter ( diff --git a/libi2pd/NetDb.hpp b/libi2pd/NetDb.hpp index 93e9e48f..d9e10504 100644 --- a/libi2pd/NetDb.hpp +++ b/libi2pd/NetDb.hpp @@ -72,6 +72,7 @@ namespace data std::shared_ptr GetRandomRouter (std::shared_ptr compatibleWith) const; std::shared_ptr GetHighBandwidthRandomRouter (std::shared_ptr compatibleWith) const; std::shared_ptr GetRandomPeerTestRouter (bool v4only = true) const; + std::shared_ptr GetRandomSSUV6Router () const; // TODO: change to v6 peer test later std::shared_ptr GetRandomIntroducer () const; std::shared_ptr GetClosestFloodfill (const IdentHash& destination, const std::set& excluded, bool closeThanUsOnly = false) const; std::vector GetClosestFloodfills (const IdentHash& destination, size_t num, diff --git a/libi2pd/RouterInfo.cpp b/libi2pd/RouterInfo.cpp index 4760ffa9..39aa70e1 100644 --- a/libi2pd/RouterInfo.cpp +++ b/libi2pd/RouterInfo.cpp @@ -808,6 +808,11 @@ namespace data return m_SupportedTransports & (eSSUV4 | eSSUV6); } + bool RouterInfo::IsSSUV6 () const + { + return m_SupportedTransports & eSSUV6; + } + bool RouterInfo::IsNTCP2 (bool v4only) const { if (v4only) diff --git a/libi2pd/RouterInfo.h b/libi2pd/RouterInfo.h index 64aaed37..b23625e0 100644 --- a/libi2pd/RouterInfo.h +++ b/libi2pd/RouterInfo.h @@ -161,6 +161,7 @@ namespace data bool IsReachable () const { return m_Caps & Caps::eReachable; }; bool IsNTCP (bool v4only = true) const; bool IsSSU (bool v4only = true) const; + bool IsSSUV6 () const; bool IsNTCP2 (bool v4only = true) const; bool IsV6 () const; bool IsV4 () const; diff --git a/libi2pd/Transports.cpp b/libi2pd/Transports.cpp index c00e5d52..b76d9312 100644 --- a/libi2pd/Transports.cpp +++ b/libi2pd/Transports.cpp @@ -552,6 +552,20 @@ namespace transport m_SSUServer->CreateSession (router); // no peer test } } + if (i2p::context.SupportsV6 ()) + { + // try to connect to few v6 addresses to get our address back + for (int i = 0; i < 3; i++) + { + auto router = i2p::data::netdb.GetRandomSSUV6Router (); + if (router) + { + auto addr = router->GetSSUV6Address (); + if (addr) + m_SSUServer->CreateDirectSession (router, { addr->host, (uint16_t)addr->port }, false); + } + } + } } else LogPrint (eLogError, "Transports: Can't detect external IP. SSU is not available");