From 44ae197d4486f744a4a35722cd636b9c74e75c52 Mon Sep 17 00:00:00 2001 From: Michael Penick Date: Wed, 28 Oct 2020 16:27:02 -0400 Subject: [PATCH] Fix: Only using a single resolved IP when connecting with hostname `SocketConnector::resolved_address_offset_` is never incremented because the first parameter was wrong. Also, this adds a default filter to for resolver hints to prevent duplicates in the resolved IPs. --- src/resolver.hpp | 9 +++++++++ src/socket_connector.cpp | 7 ++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/resolver.hpp b/src/resolver.hpp index 9b48a44a8..2895d81ef 100644 --- a/src/resolver.hpp +++ b/src/resolver.hpp @@ -73,6 +73,15 @@ class Resolver : public RefCounted { inc_ref(); // For the event loop + // If no hints are provided then use a default filter. + struct addrinfo default_hints; + if (hints == NULL) { + hints = &default_hints; + memset(hints, 0, sizeof(struct addrinfo)); + hints->ai_family = AF_UNSPEC; + hints->ai_socktype = SOCK_STREAM; + } + if (timeout > 0) { timer_.start(loop, timeout, bind_callback(&Resolver::on_timeout, this)); } diff --git a/src/socket_connector.cpp b/src/socket_connector.cpp index 4ad4835b2..3d396a4df 100644 --- a/src/socket_connector.cpp +++ b/src/socket_connector.cpp @@ -286,9 +286,10 @@ void SocketConnector::on_resolve(Resolver* resolver) { const AddressVec& addresses(resolver->addresses()); LOG_DEBUG("Resolved the addresses %s for hostname %s", to_string(addresses).c_str(), hostname_.c_str()); - resolved_address_ = Address( - addresses[resolved_address_offset_.fetch_add(MEMORY_ORDER_RELAXED) % addresses.size()], - address_.server_name()); // Keep the server name for debugging + + size_t offset = resolved_address_offset_.fetch_add(1, MEMORY_ORDER_RELAXED); + resolved_address_ = Address(addresses[offset % addresses.size()], + address_.server_name()); // Keep the server name for debugging internal_connect(resolver->loop()); } else if (is_canceled() || resolver->is_canceled()) { finish();