From: Otto Moerbeek Date: Mon, 25 Mar 2024 08:54:18 +0000 (+0100) Subject: Sort result vector of getaddrinfo X-Git-Tag: rec-5.1.0-alpha1~82^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=eaf1d24ed606e4e97877843d55c89de233753d04;p=thirdparty%2Fpdns.git Sort result vector of getaddrinfo --- diff --git a/pdns/recursordist/configure.ac b/pdns/recursordist/configure.ac index a22a6049f8..171f4afc7c 100644 --- a/pdns/recursordist/configure.ac +++ b/pdns/recursordist/configure.ac @@ -122,7 +122,6 @@ dnl the *_r functions are in posix so we can use them unconditionally, but the e dnl using the defines. AC_CHECK_FUNCS_ONCE([localtime_r gmtime_r strcasestr]) AC_CHECK_FUNCS_ONCE([getrandom getentropy arc4random arc4random_uniform arc4random_buf]) - PDNS_CHECK_SECURE_MEMSET AC_CHECK_HEADERS([sys/random.h]) diff --git a/pdns/recursordist/rec-system-resolve.cc b/pdns/recursordist/rec-system-resolve.cc index 65fde36f55..46d3aa17f4 100644 --- a/pdns/recursordist/rec-system-resolve.cc +++ b/pdns/recursordist/rec-system-resolve.cc @@ -41,14 +41,27 @@ ComboAddress resolve(const std::string& name) hints.ai_flags = AI_ADDRCONFIG; hints.ai_family = 0; - struct addrinfo* res = nullptr; - auto ret = getaddrinfo(name.c_str(), nullptr, &hints, &res); - // We pick the first address returned for now. - // XXX This might trigger unwanted "changes detected "if the sort order varies - if (ret == 0) { - auto address = ComboAddress{res->ai_addr, res->ai_addrlen}; - freeaddrinfo(res); - return address; + struct addrinfo* res0 = nullptr; + auto ret = getaddrinfo(name.c_str(), nullptr, &hints, &res0); + // We pick the first address after sorting for now, no handling of multiple addresses or AF selection. + vector vec; + if (ret != 0) { + return {}; + } + auto* res = res0; + while (res != nullptr) { + try { + auto address = ComboAddress{res->ai_addr, res->ai_addrlen}; + vec.emplace_back(address); + } + catch (...) { + } + res = res->ai_next; + } + freeaddrinfo(res0); + if (!vec.empty()) { + std::sort(vec.begin(), vec.end()); + return vec.at(0); } return {}; } @@ -73,7 +86,8 @@ std::string serverID() if (buffer.empty()) { return {}; } - MOADNSParser parser(false, reinterpret_cast(buffer.data()), buffer.size()); // NOLINT + + MOADNSParser parser(false, static_cast(static_cast(buffer.data())), buffer.size()); if (parser.d_header.rcode != RCode::NoError || parser.d_answers.size() != 1) { return {}; } @@ -240,6 +254,7 @@ void pdns::RecResolve::Refresher::refreshLoop() while (!stop) { const time_t startTime = time(nullptr); time_t wakeTime = startTime; + // The expresion wakeTime - startTime is equal to the total amount of time slept while (wakeTime - startTime < d_interval) { std::unique_lock lock(mutex); time_t remaining = d_interval - (wakeTime - startTime); diff --git a/pdns/recursordist/rec-system-resolve.hh b/pdns/recursordist/rec-system-resolve.hh index 40c81e5887..3374ea6569 100644 --- a/pdns/recursordist/rec-system-resolve.hh +++ b/pdns/recursordist/rec-system-resolve.hh @@ -87,13 +87,13 @@ public: // Lookup a name which must be already registered ComboAddress lookup(const std::string& name); - // When an instance is created, it will runn a refresh thread, stop it wit this method + // When an instance is created, it will run a refresh thread, stop it with this method void stopRefresher(); // And restart it again void startRefresher(); // Wipe one or all names void wipe(const std::string& name = ""); - // Did we se a cahnage? Calling this functino will reset the flag. + // Did we see a change? Calling this function will reset the flag. bool changeDetected(); private: diff --git a/pdns/recursordist/reczones.cc b/pdns/recursordist/reczones.cc index c07cb600c1..f8d50bdfb9 100644 --- a/pdns/recursordist/reczones.cc +++ b/pdns/recursordist/reczones.cc @@ -73,7 +73,6 @@ static ComboAddress fromNameOrIP(const string& str, uint16_t defPort, Logr::log_ uint16_t port = defPort; string::size_type pos = str.rfind(':'); if (pos != string::npos) { - cerr << str.substr(pos) << endl; port = pdns::checked_stoi(str.substr(pos + 1)); } auto& res = pdns::RecResolve::getInstance(); diff --git a/pdns/recursordist/settings/docs-new-preamble-in.rst b/pdns/recursordist/settings/docs-new-preamble-in.rst index 394ff5d1a7..03e6d983d2 100644 --- a/pdns/recursordist/settings/docs-new-preamble-in.rst +++ b/pdns/recursordist/settings/docs-new-preamble-in.rst @@ -170,7 +170,7 @@ An example of a ``forward_zones`` entry, which consists of a sequence of forward Starting with version 5.1.0, names can be used if :ref:`setting-yaml-recursor.system_resolver_ttl` is set. -The names will be resolved using the system resolver and an automatic refresh of the forwarding zones will happend if a name starts resolving to a new address. +The names will be resolved using the system resolver and an automatic refresh of the forwarding zones will happen if a name starts resolving to a new address. Auth Zone ^^^^^^^^^