From c6d801d2d69985d01e72e07b4c12f037d3de1ae7 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 11 Oct 2024 16:42:59 -0700 Subject: [PATCH] [wpinet] ParallelTcpConnector: Add option to resolve only IPv4 addresses (#7194) --- wpinet/src/main/native/cpp/ParallelTcpConnector.cpp | 5 +++-- .../native/include/wpinet/ParallelTcpConnector.h | 13 +++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/wpinet/src/main/native/cpp/ParallelTcpConnector.cpp b/wpinet/src/main/native/cpp/ParallelTcpConnector.cpp index 1ee303e522..02ea5c9140 100644 --- a/wpinet/src/main/native/cpp/ParallelTcpConnector.cpp +++ b/wpinet/src/main/native/cpp/ParallelTcpConnector.cpp @@ -24,10 +24,11 @@ using namespace wpi; ParallelTcpConnector::ParallelTcpConnector( wpi::uv::Loop& loop, wpi::uv::Timer::Time reconnectRate, wpi::Logger& logger, std::function connected, - const private_init&) + bool ipv4Only, const private_init&) : m_loop{loop}, m_logger{logger}, m_reconnectRate{reconnectRate}, + m_ipv4Only{ipv4Only}, m_connected{std::move(connected)}, m_reconnectTimer{uv::Timer::Create(loop)} { if (!m_reconnectTimer) { @@ -193,7 +194,7 @@ void ParallelTcpConnector::Connect() { static_cast(req.get()), server.first, server.second); addrinfo hints; std::memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; + hints.ai_family = m_ipv4Only ? AF_INET : AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = AI_NUMERICSERV | AI_ADDRCONFIG; diff --git a/wpinet/src/main/native/include/wpinet/ParallelTcpConnector.h b/wpinet/src/main/native/include/wpinet/ParallelTcpConnector.h index c3c869cc50..352cf77088 100644 --- a/wpinet/src/main/native/include/wpinet/ParallelTcpConnector.h +++ b/wpinet/src/main/native/include/wpinet/ParallelTcpConnector.h @@ -54,22 +54,26 @@ class ParallelTcpConnector * @param connected callback function when a connection succeeds; may be * called multiple times if it does not call Succeeded() * before returning + * @param ipv4Only true if only IPv4 addresses should be returned; otherwise + * both IPv4 and IPv6 addresses are returned * @return Parallel connector */ static std::shared_ptr Create( wpi::uv::Loop& loop, wpi::uv::Timer::Time reconnectRate, - wpi::Logger& logger, std::function connected) { + wpi::Logger& logger, std::function connected, + bool ipv4Only = false) { if (loop.IsClosing()) { return nullptr; } - return std::make_shared( - loop, reconnectRate, logger, std::move(connected), private_init{}); + return std::make_shared(loop, reconnectRate, logger, + std::move(connected), + ipv4Only, private_init{}); } ParallelTcpConnector(wpi::uv::Loop& loop, wpi::uv::Timer::Time reconnectRate, wpi::Logger& logger, std::function connected, - const private_init&); + bool ipv4Only, const private_init&); ~ParallelTcpConnector(); ParallelTcpConnector(const ParallelTcpConnector&) = delete; @@ -111,6 +115,7 @@ class ParallelTcpConnector wpi::uv::Loop& m_loop; wpi::Logger& m_logger; wpi::uv::Timer::Time m_reconnectRate; + bool m_ipv4Only; std::function m_connected; std::shared_ptr m_reconnectTimer; std::vector> m_servers;