]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
ntp: don't replace sources with unroutable addresses
authorMiroslav Lichvar <mlichvar@redhat.com>
Mon, 24 Feb 2020 09:42:29 +0000 (10:42 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 5 Mar 2020 15:02:15 +0000 (16:02 +0100)
When changing an address of a source (both known and unknown), make sure
the new address is connectable. This should avoid useless replacements,
e.g. polling an IPv6 address on IPv4-only systems.

ntp_sources.c

index eb99e6d925666bd8f2f3409875c0c3c8bc38c7d7..8bb5dc4f9f600785f0549772df3881919a5ef6a8 100644 (file)
@@ -34,6 +34,7 @@
 #include "array.h"
 #include "ntp_sources.h"
 #include "ntp_core.h"
+#include "ntp_io.h"
 #include "util.h"
 #include "logging.h"
 #include "local.h"
@@ -389,6 +390,22 @@ replace_source(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr)
 
 /* ================================================== */
 
+static int
+replace_source_connectable(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr)
+{
+  if (!NIO_IsServerConnectable(new_addr)) {
+    DEBUG_LOG("%s not connectable", UTI_IPToString(&new_addr->ip_addr));
+    return 0;
+  }
+
+  if (replace_source(old_addr, new_addr) == NSR_AlreadyInUse)
+    return 0;
+
+  return 1;
+}
+
+/* ================================================== */
+
 static void
 process_resolved_name(struct UnresolvedSource *us, IPAddr *ip_addrs, int n_addrs)
 {
@@ -415,12 +432,12 @@ process_resolved_name(struct UnresolvedSource *us, IPAddr *ip_addrs, int n_addrs
           continue;
         old_addr = *record->remote_addr;
         new_addr.port = old_addr.port;
-        if (replace_source(&old_addr, &new_addr) != NSR_AlreadyInUse)
+        if (replace_source_connectable(&old_addr, &new_addr))
           break;
       }
     } else {
       new_addr.port = us->address.port;
-      if (replace_source(&us->address, &new_addr) != NSR_AlreadyInUse)
+      if (replace_source_connectable(&us->address, &new_addr))
         break;
     }
   }