]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
conf: add ipv4 and ipv6 options to server/pool/peer directive
authorMiroslav Lichvar <mlichvar@redhat.com>
Mon, 5 Feb 2024 13:20:19 +0000 (14:20 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 7 Feb 2024 09:23:40 +0000 (10:23 +0100)
Accept "ipv4" and "ipv6" options in the server/pool/peer directive to
use only IPv4 or IPv6 addresses respectively.

The configuration is different from the "server [-4|-6] hostname" syntax
supported by ntpd to avoid breaking existing scripts which expect the
hostname to always be the first argument of the directives.

cmdparse.c
cmdparse.h
conf.c
doc/chrony.conf.adoc

index ac5ace279a7ae9b7c61569a086802e32f3b56168..0a80fc0109c77607907ff229a9ebdccdcb114d47 100644 (file)
@@ -46,6 +46,7 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
   uint32_t ef_type;
   int n, sel_option;
   
+  src->family = IPADDR_UNSPEC;
   src->port = SRC_DEFAULT_PORT;
   src->params.minpoll = SRC_DEFAULT_MINPOLL;
   src->params.maxpoll = SRC_DEFAULT_MAXPOLL;
@@ -127,6 +128,10 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
     } else if (!strcasecmp(cmd, "filter")) {
       if (sscanf(line, "%d%n", &src->params.filter_length, &n) != 1)
         return 0;
+    } else if (!strcasecmp(cmd, "ipv4")) {
+      src->family = IPADDR_INET4;
+    } else if (!strcasecmp(cmd, "ipv6")) {
+      src->family = IPADDR_INET6;
     } else if (!strcasecmp(cmd, "maxdelay")) {
       if (sscanf(line, "%lf%n", &src->params.max_delay, &n) != 1)
         return 0;
index 7a87979cf026cfe85470ef2846c13f6ef89d9ccc..095a8e27c59ca955a73807eafe234ec59209ac15 100644 (file)
@@ -32,6 +32,7 @@
 
 typedef struct {
   char *name;
+  int family;
   int port;
   SourceParameters params;
 } CPS_NTP_Source;
diff --git a/conf.c b/conf.c
index 9bae265128c40688c05e8ac5e74b2f2a18226f8b..73fe774af0f136af820699ff20db118a44a4f9f5 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -1664,6 +1664,8 @@ compare_sources(const void *a, const void *b)
     return d;
   if ((d = (int)sa->pool - (int)sb->pool) != 0)
     return d;
+  if ((d = (int)sa->params.family - (int)sb->params.family) != 0)
+    return d;
   if ((d = (int)sa->params.port - (int)sb->params.port) != 0)
     return d;
   return memcmp(&sa->params.params, &sb->params.params, sizeof (sa->params.params));
@@ -1728,7 +1730,7 @@ reload_source_dirs(void)
       /* Add new sources */
       if (pass == 1 && d > 0) {
         source = &new_sources[j];
-        s = NSR_AddSourceByName(source->params.name, IPADDR_UNSPEC, source->params.port,
+        s = NSR_AddSourceByName(source->params.name, source->params.family, source->params.port,
                                 source->pool, source->type, &source->params.params,
                                 &new_ids[j]);
 
@@ -1843,7 +1845,7 @@ CNF_AddSources(void)
   for (i = 0; i < ARR_GetSize(ntp_sources); i++) {
     source = (NTP_Source *)ARR_GetElement(ntp_sources, i);
 
-    s = NSR_AddSourceByName(source->params.name, IPADDR_UNSPEC, source->params.port,
+    s = NSR_AddSourceByName(source->params.name, source->params.family, source->params.port,
                             source->pool, source->type, &source->params.params, NULL);
     if (s != NSR_Success && s != NSR_UnresolvedName)
       LOG(LOGS_ERR, "Could not add source %s", source->params.name);
index cb3f95c0ff013aa00ea31615769078acc41d612c..eeaa50100aba2005dad06be32c8dc8931189798e 100644 (file)
@@ -343,6 +343,12 @@ the PTP port. The corrections are applied only to NTP measurements with HW
 timestamps (enabled by the <<hwtimestamp,*hwtimestamp*>> directive). This
 field should be enabled only for servers known to be running *chronyd* version
 4.5 or later.
+*ipv4*:::
+*ipv6*:::
+These options force *chronyd* to use only IPv4 or IPv6 addresses respectively
+for this source. They do not override the *-4* or *-6* option on the *chronyd*
+command line.
+
 {blank}:::
 
 [[pool]]*pool* _name_ [_option_]...::