]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Sort result vector of getaddrinfo
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 25 Mar 2024 08:54:18 +0000 (09:54 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 25 Mar 2024 09:25:35 +0000 (10:25 +0100)
pdns/recursordist/configure.ac
pdns/recursordist/rec-system-resolve.cc
pdns/recursordist/rec-system-resolve.hh
pdns/recursordist/reczones.cc
pdns/recursordist/settings/docs-new-preamble-in.rst

index a22a6049f85cbb0366c6fd18674ff71ff02dd7d1..171f4afc7c240a911ea1ad09b5b0c4756aa14b80 100644 (file)
@@ -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])
index 65fde36f5509dbb702ef1ccdba8fd8470318df92..46d3aa17f49d09fbce197405acccb84e26e5dddb 100644 (file)
@@ -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<ComboAddress> 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<char*>(buffer.data()), buffer.size()); // NOLINT
+
+  MOADNSParser parser(false, static_cast<const char*>(static_cast<void*>(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);
index 40c81e58877740282dcb74448bb5bcd75ac3622b..3374ea65693e756497af19c23433f2132cd7e82b 100644 (file)
@@ -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:
index c07cb600c13ba6ed5ecd0486df9ca27e2584c34b..f8d50bdfb9cb5aecc1c81616f81bb4d08d2592b5 100644 (file)
@@ -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<uint16_t>(str.substr(pos + 1));
     }
     auto& res = pdns::RecResolve::getInstance();
index 394ff5d1a797cd3ab43afe4972d92fb571ec8a8f..03e6d983d26d8f906461d801060cb17732a81bc8 100644 (file)
@@ -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
 ^^^^^^^^^