[chrony-dev] [RFC PATCH 3/9] sources: save port of NTP sources

[ Thread Index | Date Index | More chrony.tuxfamily.org/chrony-dev Archives ]


Source and sourcestats instances keep a pointer to the IP address of an
NTP source, which identifies the source in lookups in ntp_sources.c,
reports, logs, and names of dump files. The reference module keeps the
address of the selected source for the tracking report and log.

None of them has the port of the source, so they cannot distinguish
sources with the same address and different ports.

Change the address in the source, sourcestats, and reference modules
from IPAddr to IPSockAddr, which includes the port, and provide the
port in the source, sourcestats, selection, and tracking reports.
---
 ntp_core.c          |  7 +++---
 reference.c         | 26 +++++++++++++---------
 reference.h         |  5 ++++-
 reports.h           |  4 ++++
 sources.c           | 54 ++++++++++++++++++++++++++-------------------
 sources.h           |  6 ++---
 sourcestats.c       | 16 +++++++-------
 sourcestats.h       |  6 ++---
 test/unit/sources.c | 15 +++++++++----
 9 files changed, 82 insertions(+), 57 deletions(-)

diff --git a/ntp_core.c b/ntp_core.c
index 71e2576..81886de 100644
--- a/ntp_core.c
+++ b/ntp_core.c
@@ -684,7 +684,7 @@ NCR_CreateInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type,
   /* Create a source instance for this NTP source */
   result->source = SRC_CreateNewInstance(UTI_IPToRefid(&remote_addr->ip_addr),
                                          SRC_NTP, NAU_IsAuthEnabled(result->auth),
-                                         params->sel_options, &result->remote_addr.ip_addr,
+                                         params->sel_options, &result->remote_addr,
                                          params->min_samples, params->max_samples,
                                          params->min_delay, params->asymmetry,
                                          params->max_unreach);
@@ -848,8 +848,7 @@ NCR_ChangeRemoteAddress(NCR_Instance inst, NTP_Remote_Address *remote_addr, int
     NCR_ResetPoll(inst);
 
   /* Update the reference ID and reset the source/sourcestats instances */
-  SRC_SetRefid(inst->source, UTI_IPToRefid(&remote_addr->ip_addr),
-               &inst->remote_addr.ip_addr);
+  SRC_SetRefid(inst->source, UTI_IPToRefid(&remote_addr->ip_addr), &inst->remote_addr);
   SRC_ResetInstance(inst->source);
 
   reset_report(inst);
@@ -2373,7 +2372,7 @@ process_response(NCR_Instance inst, int saved, NTP_Local_Address *local_addr,
       /* Assume the reference ID and stratum of the server */
       if (synced_packet && inst->remote_stratum > 0) {
         inst->remote_stratum--;
-        SRC_SetRefid(inst->source, ntohl(message->reference_id), &inst->remote_addr.ip_addr);
+        SRC_SetRefid(inst->source, ntohl(message->reference_id), &inst->remote_addr);
       } else {
         SRC_ResetInstance(inst->source);
       }
diff --git a/reference.c b/reference.c
index d720644..e7c3427 100644
--- a/reference.c
+++ b/reference.c
@@ -58,7 +58,7 @@ static double local_wait_unsynced;
 static struct timespec local_ref_time;
 static int our_stratum;
 static uint32_t our_ref_id;
-static IPAddr our_ref_ip;
+static IPSockAddr our_ref_addr;
 static struct timespec our_ref_time;
 static double unsynchronised_since;
 static double our_skew;
@@ -565,8 +565,8 @@ write_log(struct timespec *now, int combined_sources, double freq,
   LOG_FileWrite(logfileid,
                 "%s %-15s %2d %10.3f %10.3f %10.3e %1c %2d %10.3e %10.3e %10.3e %10.3e %10.3e",
                 UTI_TimeToLogForm(now->tv_sec),
-                our_ref_ip.family != IPADDR_UNSPEC ?
-                  UTI_IPToString(&our_ref_ip) : UTI_RefidToString(our_ref_id),
+                our_ref_addr.ip_addr.family != IPADDR_UNSPEC ?
+                  UTI_IPToString(&our_ref_addr.ip_addr) : UTI_RefidToString(our_ref_id),
                 our_stratum, freq, 1.0e6 * our_skew, offset,
                 are_we_synchronised ? 'N' : '?', combined_sources, offset_sd,
                 uncorrected_offset, our_root_delay, root_dispersion, max_error);
@@ -703,7 +703,7 @@ get_correction_rate(double offset_sd, double update_interval)
 
 void
 REF_SetReference(int stratum, int synced, int combined_sources,
-                 uint32_t ref_id, IPAddr *ref_ip, struct timespec *ref_time,
+                 uint32_t ref_id, IPSockAddr *ref_addr, struct timespec *ref_time,
                  double offset, double offset_sd,
                  double frequency, double frequency_sd, double skew,
                  double root_delay, double root_dispersion)
@@ -750,10 +750,12 @@ REF_SetReference(int stratum, int synced, int combined_sources,
   are_we_synchronised = synced;
   our_stratum = stratum + 1;
   our_ref_id = ref_id;
-  if (ref_ip)
-    our_ref_ip = *ref_ip;
-  else
-    our_ref_ip.family = IPADDR_UNSPEC;
+  if (ref_addr) {
+    our_ref_addr = *ref_addr;
+  } else {
+    our_ref_addr.ip_addr.family = IPADDR_UNSPEC;
+    our_ref_addr.port = 0;
+  }
   our_ref_time = *ref_time;
   our_skew = skew;
   our_residual_freq = residual_frequency;
@@ -889,8 +891,8 @@ REF_SetUnsynchronised(void)
     schedule_fb_drift();
   }
 
-  our_ref_ip.family = IPADDR_INET4;
-  our_ref_ip.addr.in4 = 0;
+  our_ref_addr.ip_addr.family = IPADDR_INET4;
+  our_ref_addr.ip_addr.addr.in4 = 0;
   our_stratum = 0;
 
   if (are_we_synchronised)
@@ -1109,6 +1111,7 @@ REF_GetTrackingReport(RPT_TrackingReport *rep)
     rep->stratum = 0;
 
   rep->ip_addr.family = IPADDR_UNSPEC;
+  rep->port = 0;
   rep->current_correction = correction;
   rep->freq_ppm = LCL_ReadAbsoluteFrequency();
   rep->resid_freq_ppm = 0.0;
@@ -1118,7 +1121,8 @@ REF_GetTrackingReport(RPT_TrackingReport *rep)
   rep->rms_offset = sqrt(avg2_offset);
 
   if (synchronised) {
-    rep->ip_addr = our_ref_ip;
+    rep->ip_addr = our_ref_addr.ip_addr;
+    rep->port = our_ref_addr.port;
     rep->resid_freq_ppm = 1.0e6 * our_residual_freq;
     rep->skew_ppm = 1.0e6 * our_skew;
   }
diff --git a/reference.h b/reference.h
index 25e3455..faa95cb 100644
--- a/reference.h
+++ b/reference.h
@@ -107,6 +107,9 @@ extern void REF_GetReferenceParams
 
    ref_id is the reference id of the reference
 
+   ref_addr is the IP address and port of the reference if it is an NTP
+   source, or NULL
+
    ref_time is the time at which the parameters are assumed to be
    correct, in terms of local time
 
@@ -128,7 +131,7 @@ extern void REF_SetReference
  int synced,
  int combined_sources,
  uint32_t ref_id,
- IPAddr *ref_ip,
+ IPSockAddr *ref_addr,
  struct timespec *ref_time,
  double offset,
  double offset_sd,
diff --git a/reports.h b/reports.h
index d32628f..ea68f9d 100644
--- a/reports.h
+++ b/reports.h
@@ -33,6 +33,7 @@
 
 typedef struct {
   IPAddr ip_addr;
+  uint16_t port;
   int stratum;
   int poll;
   enum {RPT_NTP_CLIENT, RPT_NTP_PEER, RPT_LOCAL_REFERENCE} mode;
@@ -55,6 +56,7 @@ typedef struct {
 typedef struct {
   uint32_t ref_id;
   IPAddr ip_addr;
+  uint16_t port;
   int stratum;
   NTP_Leap leap_status;
   struct timespec ref_time;
@@ -72,6 +74,7 @@ typedef struct {
 typedef struct {
   uint32_t ref_id;
   IPAddr ip_addr;
+  uint16_t port;
   unsigned long n_samples;
   unsigned long n_runs;
   unsigned long span_seconds;
@@ -201,6 +204,7 @@ typedef struct {
 typedef struct {
   uint32_t ref_id;
   IPAddr ip_addr;
+  uint16_t port;
   char state_char;
   int authentication;
   NTP_Leap leap;
diff --git a/sources.c b/sources.c
index e276ae3..3df5ba6 100644
--- a/sources.c
+++ b/sources.c
@@ -95,7 +95,7 @@ struct SRC_Instance_Record {
   uint32_t ref_id;              /* The reference ID of this source
                                    (i.e. from its IP address, NOT the
                                    reference _it_ is sync'd to) */
-  IPAddr *ip_addr;              /* Its IP address if NTP source */
+  IPSockAddr *addr;             /* Its IP address and port if NTP source */
 
   /* Flag indicating that the source is updating reachability */
   int active;
@@ -266,7 +266,7 @@ void SRC_Finalise(void)
    the individual source-type instance creation routines. */
 
 SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, int authenticated,
-                                   int sel_options, IPAddr *addr, int min_samples,
+                                   int sel_options, IPSockAddr *addr, int min_samples,
                                    int max_samples, double min_delay, double asymmetry,
                                    int max_unreach)
 {
@@ -381,10 +381,10 @@ SRC_ResetInstance(SRC_Instance instance)
 /* ================================================== */
 
 void
-SRC_SetRefid(SRC_Instance instance, uint32_t ref_id, IPAddr *addr)
+SRC_SetRefid(SRC_Instance instance, uint32_t ref_id, IPSockAddr *addr)
 {
   instance->ref_id = ref_id;
-  instance->ip_addr = addr;
+  instance->addr = addr;
   SST_SetRefid(instance->stats, ref_id, addr);
 }
 
@@ -480,7 +480,7 @@ handle_bad_source(SRC_Instance inst)
 {
   if (inst->type == SRC_NTP) {
     DEBUG_LOG("Bad source status=%c", get_status_char(inst->status));
-    NSR_HandleBadSource(inst->ip_addr);
+    NSR_HandleBadSource(&inst->addr->ip_addr);
   }
 }
 
@@ -639,7 +639,7 @@ log_selection_source(LOG_Severity severity, SRC_Instance inst, const char *forma
   s[1] = 's';
 
   name = source_to_string(inst);
-  ntp_name = inst->type == SRC_NTP ? NSR_GetName(inst->ip_addr) : NULL;
+  ntp_name = inst->type == SRC_NTP ? NSR_GetName(&inst->addr->ip_addr) : NULL;
 
   if (ntp_name && strcmp(name, ntp_name) != 0)
     snprintf(buf, sizeof (buf), "%s (%s)", name, ntp_name);
@@ -679,7 +679,7 @@ source_to_string(SRC_Instance inst)
 {
   switch (inst->type) {
     case SRC_NTP:
-      return UTI_IPToString(inst->ip_addr);
+      return UTI_IPToString(&inst->addr->ip_addr);
     case SRC_REFCLOCK:
       return UTI_RefidToString(inst->ref_id);
     default:
@@ -1090,7 +1090,7 @@ SRC_SelectSource(SRC_Instance updated_inst)
 
   /* If no selectable source is available, consider the orphan source */
   if (!n_sel_sources && orphan_source != INVALID_SOURCE) {
-    uint32_t local_ref_id = NSR_GetLocalRefid(sources[orphan_source]->ip_addr);
+    uint32_t local_ref_id = NSR_GetLocalRefid(&sources[orphan_source]->addr->ip_addr);
 
     if (!local_ref_id) {
       LOG(LOGS_ERR, "Unknown local refid in orphan mode");
@@ -1447,7 +1447,7 @@ SRC_SelectSource(SRC_Instance updated_inst)
   REF_SetReference(sources[selected_source_index]->stratum,
                    1, combined,
                    sources[selected_source_index]->ref_id,
-                   sources[selected_source_index]->ip_addr,
+                   sources[selected_source_index]->addr,
                    &ref_time, src_offset, src_offset_sd,
                    src_frequency, src_frequency_sd, src_skew,
                    src_root_delay, src_root_dispersion);
@@ -1523,7 +1523,7 @@ get_dumpfile(SRC_Instance inst, char *filename, size_t len)
   /* Use the IP address, or reference ID with reference clocks */
   switch (inst->type) {
     case SRC_NTP:
-      if (!UTI_IsIPReal(inst->ip_addr) ||
+      if (!UTI_IsIPReal(&inst->addr->ip_addr) ||
           snprintf(filename, len, "%s", source_to_string(inst)) >= len)
         return 0;
       break;
@@ -1557,7 +1557,7 @@ save_source(SRC_Instance inst)
   if (!f)
     return;
 
-  ntp_name = inst->type == SRC_NTP ? NSR_GetName(inst->ip_addr) : ".";
+  ntp_name = inst->type == SRC_NTP ? NSR_GetName(&inst->addr->ip_addr) : ".";
 
   if (fprintf(f, "%s%s\n%d %o %d %d %d\n",
               DUMP_IDENTIFIER, ntp_name, inst->authenticated,
@@ -1608,7 +1608,7 @@ load_source(SRC_Instance inst)
   if (!f)
     return;
 
-  ntp_name = inst->type == SRC_NTP ? NSR_GetName(inst->ip_addr) : NULL;
+  ntp_name = inst->type == SRC_NTP ? NSR_GetName(&inst->addr->ip_addr) : NULL;
 
   if (!fgets(line, sizeof (line), f) || strcmp(line, DUMP_IDENTIFIER) != 0 ||
       !fgets(line, sizeof (line), f) || UTI_SplitString(line, words, MAX_WORDS) != 1 ||
@@ -1757,7 +1757,7 @@ find_source(IPAddr *ip, uint32_t ref_id)
 
   for (i = 0; i < n_sources; i++) {
     if ((ip->family != IPADDR_UNSPEC && sources[i]->type == SRC_NTP &&
-         UTI_CompareIPs(ip, sources[i]->ip_addr, NULL) == 0) ||
+         UTI_CompareIPs(ip, &sources[i]->addr->ip_addr, NULL) == 0) ||
         (ip->family == IPADDR_UNSPEC && sources[i]->type == SRC_REFCLOCK &&
          ref_id == sources[i]->ref_id))
       return sources[i];
@@ -1799,12 +1799,14 @@ SRC_ReportSource(int index, RPT_SourceReport *report, struct timespec *now)
   } else {
     src = sources[index];
 
-    if (src->ip_addr)
-      report->ip_addr = *src->ip_addr;
-    else {
+    if (src->addr) {
+      report->ip_addr = src->addr->ip_addr;
+      report->port = src->addr->port;
+    } else {
       /* Use refid as an address */
       report->ip_addr.addr.in4 = src->ref_id;
       report->ip_addr.family = IPADDR_INET4;
+      report->port = 0;
     }
 
     report->stratum = src->stratum;
@@ -1856,10 +1858,13 @@ SRC_ReportSourcestats(int index, RPT_SourcestatsReport *report, struct timespec
   } else {
     src = sources[index];
     report->ref_id = src->ref_id;
-    if (src->ip_addr)
-      report->ip_addr = *src->ip_addr;
-    else
-      report->ip_addr.family = IPADDR_UNSPEC; 
+    if (src->addr) {
+      report->ip_addr = src->addr->ip_addr;
+      report->port = src->addr->port;
+    } else {
+      report->ip_addr.family = IPADDR_UNSPEC;
+      report->port = 0;
+    }
     SST_DoSourcestatsReport(src->stats, report, now);
     return 1;
   }
@@ -1925,10 +1930,13 @@ SRC_GetSelectReport(int index, RPT_SelectReport *report)
   inst = sources[index];
 
   report->ref_id = inst->ref_id;
-  if (inst->ip_addr)
-    report->ip_addr = *inst->ip_addr;
-  else
+  if (inst->addr) {
+    report->ip_addr = inst->addr->ip_addr;
+    report->port = inst->addr->port;
+  } else {
     report->ip_addr.family = IPADDR_UNSPEC;
+    report->port = 0;
+  }
   report->state_char = get_status_char(inst->status);
   report->authentication = inst->authenticated;
   report->leap = inst->synchronised ? LEAP_Normal : LEAP_Unsynchronised;
diff --git a/sources.h b/sources.h
index e0dcaca..ef9e3e0 100644
--- a/sources.h
+++ b/sources.h
@@ -68,7 +68,7 @@ typedef enum {
    the individual source-type instance creation routines. */
 
 extern SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, int authenticated,
-                                          int sel_options, IPAddr *addr, int min_samples,
+                                          int sel_options, IPSockAddr *addr, int min_samples,
                                           int max_samples, double min_delay, double asymmetry,
                                           int max_unreach);
 
@@ -82,8 +82,8 @@ extern void SRC_DestroyInstance(SRC_Instance instance);
 /* Function to reset a source */
 extern void SRC_ResetInstance(SRC_Instance instance);
 
-/* Function to change the sources's reference ID and IP address */
-extern void SRC_SetRefid(SRC_Instance instance, uint32_t ref_id, IPAddr *addr);
+/* Function to change the sources's reference ID and IP address with port */
+extern void SRC_SetRefid(SRC_Instance instance, uint32_t ref_id, IPSockAddr *addr);
 
 /* Function to get access to the sourcestats instance */
 extern SST_Stats SRC_GetSourcestats(SRC_Instance instance);
diff --git a/sourcestats.c b/sourcestats.c
index c7ea4f9..a65e3b5 100644
--- a/sourcestats.c
+++ b/sourcestats.c
@@ -80,9 +80,9 @@ static LOG_FileID logfileid;
 
 struct SST_Stats_Record {
 
-  /* Reference ID and IP address (NULL if not an NTP source) */
+  /* Reference ID and IP address with port (NULL if not an NTP source) */
   uint32_t refid;
-  IPAddr *ip_addr;
+  IPSockAddr *addr;
 
   /* User defined minimum and maximum number of samples */
   int min_samples;
@@ -205,7 +205,7 @@ SST_Finalise(void)
 /* This function creates a new instance of the statistics handler */
 
 SST_Stats
-SST_CreateInstance(uint32_t refid, IPAddr *addr, int min_samples, int max_samples,
+SST_CreateInstance(uint32_t refid, IPSockAddr *addr, int min_samples, int max_samples,
                    double min_delay, double asymmetry)
 {
   SST_Stats inst;
@@ -257,10 +257,10 @@ SST_ResetInstance(SST_Stats inst)
 /* ================================================== */
 
 void
-SST_SetRefid(SST_Stats inst, uint32_t refid, IPAddr *addr)
+SST_SetRefid(SST_Stats inst, uint32_t refid, IPSockAddr *addr)
 {
   inst->refid = refid;
-  inst->ip_addr = addr;
+  inst->addr = addr;
 }
 
 /* ================================================== */
@@ -301,7 +301,7 @@ SST_AccumulateSample(SST_Stats inst, NTP_Sample *sample)
   if (inst->n_samples &&
       UTI_CompareTimespecs(&inst->sample_times[inst->last_sample], &sample->time) >= 0) {
     LOG(LOGS_WARN, "Out of order sample detected, discarding history for %s",
-        inst->ip_addr ? UTI_IPToString(inst->ip_addr) : UTI_RefidToString(inst->refid));
+        inst->addr ? UTI_IPToString(&inst->addr->ip_addr) : UTI_RefidToString(inst->refid));
     SST_ResetInstance(inst);
   }
 
@@ -586,7 +586,7 @@ SST_DoNewRegression(SST_Stats inst)
     if (logfileid != -1) {
       LOG_FileWrite(logfileid, "%s %-15s %10.3e %10.3e %10.3e %10.3e %10.3e %7.1e %3d %3d %3d %5.2f",
               UTI_TimeToLogForm(inst->offset_time.tv_sec),
-              inst->ip_addr ? UTI_IPToString(inst->ip_addr) : UTI_RefidToString(inst->refid),
+              inst->addr ? UTI_IPToString(&inst->addr->ip_addr) : UTI_RefidToString(inst->refid),
               inst->std_dev,
               inst->estimated_offset, inst->estimated_offset_sd,
               inst->estimated_frequency, inst->skew, stress,
@@ -947,7 +947,7 @@ SST_DoSourceReport(SST_Stats inst, RPT_SourceReport *report, struct timespec *no
 
     /* Align the sample time to reduce the leak of the NTP receive timestamp */
     last_sample_time = inst->sample_times[i];
-    if (inst->ip_addr)
+    if (inst->addr)
       last_sample_time.tv_nsec = 0;
     report->latest_meas_ago = UTI_DiffTimespecsToDouble(now, &last_sample_time);
   } else {
diff --git a/sourcestats.h b/sourcestats.h
index ef50f62..e9acce7 100644
--- a/sourcestats.h
+++ b/sourcestats.h
@@ -38,7 +38,7 @@ extern void SST_Initialise(void);
 extern void SST_Finalise(void);
 
 /* This function creates a new instance of the statistics handler */
-extern SST_Stats SST_CreateInstance(uint32_t refid, IPAddr *addr,
+extern SST_Stats SST_CreateInstance(uint32_t refid, IPSockAddr *addr,
                                     int min_samples, int max_samples,
                                     double min_delay, double asymmetry);
 
@@ -48,8 +48,8 @@ extern void SST_DeleteInstance(SST_Stats inst);
 /* This function resets an instance */
 extern void SST_ResetInstance(SST_Stats inst);
 
-/* This function changes the reference ID and IP address */
-extern void SST_SetRefid(SST_Stats inst, uint32_t refid, IPAddr *addr);
+/* This function changes the reference ID and IP address with port */
+extern void SST_SetRefid(SST_Stats inst, uint32_t refid, IPSockAddr *addr);
 
 /* This function accumulates a single sample into the statistics handler */
 extern void SST_AccumulateSample(SST_Stats inst, NTP_Sample *sample);
diff --git a/test/unit/sources.c b/test/unit/sources.c
index b3a878a..0e3678f 100644
--- a/test/unit/sources.c
+++ b/test/unit/sources.c
@@ -22,11 +22,12 @@
 #include "test.h"
 
 static SRC_Instance
-create_source(SRC_Type type, IPAddr *addr, int authenticated, int sel_options)
+create_source(SRC_Type type, IPSockAddr *addr, int authenticated, int sel_options)
 {
-  TST_GetRandomAddress(addr, IPADDR_UNSPEC, -1);
+  TST_GetRandomAddress(&addr->ip_addr, IPADDR_UNSPEC, -1);
+  addr->port = random() % 2 ? 123 : 319;
 
-  return SRC_CreateNewInstance(UTI_IPToRefid(addr), type, authenticated, sel_options,
+  return SRC_CreateNewInstance(UTI_IPToRefid(&addr->ip_addr), type, authenticated, sel_options,
                                type == SRC_NTP ? addr : NULL,
                                SRC_DEFAULT_MINSAMPLES, SRC_DEFAULT_MAXSAMPLES, 0.0, 1.0,
                                SRC_DEFAULT_MAXUNREACH);
@@ -37,7 +38,7 @@ test_unit(void)
 {
   SRC_AuthSelectMode sel_mode;
   SRC_Instance srcs[16];
-  IPAddr addrs[16];
+  IPSockAddr addrs[16];
   RPT_SourceReport report;
   NTP_Sample sample;
   int i, j, k, l, n1, n2, n3, n4, samples, sel_options;
@@ -140,6 +141,12 @@ test_unit(void)
       }
     }
 
+    for (j = 0; j < sizeof (srcs) / sizeof (srcs[0]); j++) {
+      TEST_CHECK(SRC_ReportSource(j, &report, &sample.time));
+      TEST_CHECK(UTI_CompareIPs(&report.ip_addr, &addrs[j].ip_addr, NULL) == 0);
+      TEST_CHECK(report.port == addrs[j].port);
+    }
+
     for (j = 0; j < sizeof (srcs) / sizeof (srcs[0]); j++) {
       SRC_ReportSource(j, &report, &sample.time);
       SRC_DestroyInstance(srcs[j]);
-- 
2.43.0


-- 
To unsubscribe email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "unsubscribe" in the subject.
For help email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "help" in the subject.
Trouble?  Email listmaster@xxxxxxxxxxxxxxxxxxxx.


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/