64b7ef
commit 33a1fe7a9ce223d6287ab7b11bca3208e9255cdd
64b7ef
Author: Miroslav Lichvar <mlichvar@redhat.com>
64b7ef
Date:   Wed Mar 9 15:30:16 2022 +0100
64b7ef
64b7ef
    ntp: split out conf_id allocation
64b7ef
64b7ef
diff --git a/ntp_sources.c b/ntp_sources.c
64b7ef
index 3cbb2ae7..30770825 100644
64b7ef
--- a/ntp_sources.c
64b7ef
+++ b/ntp_sources.c
64b7ef
@@ -698,21 +698,25 @@ static int get_unused_pool_id(void)
64b7ef
 
64b7ef
 /* ================================================== */
64b7ef
 
64b7ef
-NSR_Status
64b7ef
-NSR_AddSource(NTP_Remote_Address *remote_addr, NTP_Source_Type type,
64b7ef
-              SourceParameters *params, uint32_t *conf_id)
64b7ef
+static uint32_t
64b7ef
+get_next_conf_id(uint32_t *conf_id)
64b7ef
 {
64b7ef
-  NSR_Status s;
64b7ef
-
64b7ef
-  s = add_source(remote_addr, NULL, type, params, INVALID_POOL, last_conf_id + 1);
64b7ef
-  if (s != NSR_Success)
64b7ef
-    return s;
64b7ef
-
64b7ef
   last_conf_id++;
64b7ef
+
64b7ef
   if (conf_id)
64b7ef
     *conf_id = last_conf_id;
64b7ef
 
64b7ef
-  return s;
64b7ef
+  return last_conf_id;
64b7ef
+}
64b7ef
+
64b7ef
+/* ================================================== */
64b7ef
+
64b7ef
+NSR_Status
64b7ef
+NSR_AddSource(NTP_Remote_Address *remote_addr, NTP_Source_Type type,
64b7ef
+              SourceParameters *params, uint32_t *conf_id)
64b7ef
+{
64b7ef
+  return add_source(remote_addr, NULL, type, params, INVALID_POOL,
64b7ef
+                    get_next_conf_id(conf_id));
64b7ef
 }
64b7ef
 
64b7ef
 /* ================================================== */
64b7ef
@@ -725,6 +729,7 @@ NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type,
64b7ef
   struct SourcePool *sp;
64b7ef
   NTP_Remote_Address remote_addr;
64b7ef
   int i, new_sources, pool_id;
64b7ef
+  uint32_t cid;
64b7ef
 
64b7ef
   /* If the name is an IP address, add the source with the address directly */
64b7ef
   if (UTI_StringToIP(name, &remote_addr.ip_addr)) {
64b7ef
@@ -770,14 +775,12 @@ NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type,
64b7ef
 
64b7ef
   append_unresolved_source(us);
64b7ef
 
64b7ef
-  last_conf_id++;
64b7ef
-  if (conf_id)
64b7ef
-    *conf_id = last_conf_id;
64b7ef
+  cid = get_next_conf_id(conf_id);
64b7ef
 
64b7ef
   for (i = 0; i < new_sources; i++) {
64b7ef
     if (i > 0)
64b7ef
       remote_addr.ip_addr.addr.id = ++last_address_id;
64b7ef
-    if (add_source(&remote_addr, name, type, params, us->pool_id, last_conf_id) != NSR_Success)
64b7ef
+    if (add_source(&remote_addr, name, type, params, us->pool_id, cid) != NSR_Success)
64b7ef
       return NSR_TooManySources;
64b7ef
   }
64b7ef
 
64b7ef
64b7ef
commit 1219f99935ca9597eb0e4f4c6039e536462cf1a6
64b7ef
Author: Miroslav Lichvar <mlichvar@redhat.com>
64b7ef
Date:   Wed Mar 9 15:34:16 2022 +0100
64b7ef
64b7ef
    ntp: keep original source IP address
64b7ef
    
64b7ef
    When an added source is specified by IP address, save the original
64b7ef
    string instead of formatting a new string from the parsed address, which
64b7ef
    can be different (e.g. compressed vs expanded IPv6 address).
64b7ef
    
64b7ef
    This fixes the chronyc sourcename command and -N option to print the IP
64b7ef
    address exactly as it was specified in the configuration file or chronyc
64b7ef
    add command.
64b7ef
64b7ef
diff --git a/ntp_sources.c b/ntp_sources.c
64b7ef
index 30770825..d46c211d 100644
64b7ef
--- a/ntp_sources.c
64b7ef
+++ b/ntp_sources.c
64b7ef
@@ -353,7 +353,6 @@ add_source(NTP_Remote_Address *remote_addr, char *name, NTP_Source_Type type,
64b7ef
       record_lock = 1;
64b7ef
 
64b7ef
       record = get_record(slot);
64b7ef
-      assert(!name || !UTI_IsStringIP(name));
64b7ef
       record->name = Strdup(name ? name : UTI_IPToString(&remote_addr->ip_addr));
64b7ef
       record->data = NCR_CreateInstance(remote_addr, type, params, record->name);
64b7ef
       record->remote_addr = NCR_GetRemoteAddress(record->data);
64b7ef
@@ -734,7 +733,8 @@ NSR_AddSourceByName(char *name, int port, int pool, NTP_Source_Type type,
64b7ef
   /* If the name is an IP address, add the source with the address directly */
64b7ef
   if (UTI_StringToIP(name, &remote_addr.ip_addr)) {
64b7ef
     remote_addr.port = port;
64b7ef
-    return NSR_AddSource(&remote_addr, type, params, conf_id);
64b7ef
+    return add_source(&remote_addr, name, type, params, INVALID_POOL,
64b7ef
+                      get_next_conf_id(conf_id));
64b7ef
   }
64b7ef
 
64b7ef
   /* Make sure the name is at least printable and has no spaces */