From 5a09adebfd5987ee81789604faf7964de71509a4 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Mon, 24 Feb 2020 10:42:29 +0100 Subject: [PATCH] ntp: don't replace sources with unroutable addresses 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 | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/ntp_sources.c b/ntp_sources.c index eb99e6d..8bb5dc4 100644 --- a/ntp_sources.c +++ b/ntp_sources.c @@ -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; } }