From 1570f97ee2a5b47ba3718e9a649b79c91644542c Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Tue, 13 Oct 2009 16:04:24 +0200 Subject: [PATCH] Include both refid and IP address in tracking and sourcestats reports ref_id is not sufficient for IPv6 addresses and ref_id is needed for reference clocks. --- candm.h | 5 ++++- client.c | 23 +++++++++++++++++++---- cmdmon.c | 2 ++ reference.c | 11 ++++++++++- reference.h | 1 + reports.h | 2 ++ sources.c | 2 ++ 7 files changed, 40 insertions(+), 6 deletions(-) diff --git a/candm.h b/candm.h index 0abda60..435b802 100644 --- a/candm.h +++ b/candm.h @@ -321,7 +321,8 @@ typedef struct { Version 3 : NTP_Source message lengthened (auto_offline) - Version 4 : IPv6 addressing added, 64-bit time values + Version 4 : IPv6 addressing added, 64-bit time values, sourcestats + and tracking reports extended */ @@ -472,6 +473,7 @@ typedef struct { typedef struct { uint32_t ref_id; + IPAddr ip_addr; uint32_t stratum; Timeval ref_time; uint32_t current_correction_s; @@ -485,6 +487,7 @@ typedef struct { } RPY_Tracking; typedef struct { + uint32_t ref_id; IPAddr ip_addr; uint32_t n_samples; uint32_t n_runs; diff --git a/client.c b/client.c index cce46ca..713341f 100644 --- a/client.c +++ b/client.c @@ -1585,6 +1585,7 @@ process_cmd_sourcestats(char *line) unsigned long n_samples, n_runs, span_seconds; double resid_freq_ppm, skew_ppm; unsigned long sd_us; + unsigned long ref_id; IPAddr ip_addr; unsigned short status; @@ -1634,6 +1635,7 @@ process_cmd_sourcestats(char *line) if (submit_ok) { if (ntohs(reply.status) == STT_SUCCESS) { + ref_id = ntohl(reply.data.sourcestats.ref_id); UTI_IPNetworkToHost(&reply.data.sourcestats.ip_addr, &ip_addr); n_samples = ntohl(reply.data.sourcestats.n_samples); n_runs = ntohl(reply.data.sourcestats.n_runs); @@ -1642,7 +1644,9 @@ process_cmd_sourcestats(char *line) skew_ppm = WIRE2REAL(reply.data.sourcestats.skew_ppm); sd_us = ntohl(reply.data.sourcestats.sd_us); - if (no_dns) { + if (ip_addr.family == IPADDR_UNSPEC) + snprintf(hostname_buf, sizeof(hostname_buf), "%s", UTI_RefidToString(ref_id)); + else if (no_dns) { snprintf(hostname_buf, sizeof(hostname_buf), "%s", UTI_IPToString(&ip_addr)); } else { DNS_IPAddress2Name(&ip_addr, hostname_buf, sizeof(hostname_buf)); @@ -1673,7 +1677,10 @@ process_cmd_tracking(char *line) int auth_ok; CMD_Request request; CMD_Reply reply; + IPAddr ip_addr; unsigned long ref_id; + char host[50]; + char *ref_ip; struct timeval ref_time; struct tm ref_time_tm; unsigned long a, b, c, d; @@ -1709,9 +1716,17 @@ process_cmd_tracking(char *line) c = (ref_id >> 8) & 0xff; d = (ref_id) & 0xff; - printf("Reference ID : %lu.%lu.%lu.%lu (%s)\n", - a, b, c, d, ""); - //* TODO (no_dns) ? UTI_IPToDottedQuad(ref_id) : DNS_IPAddress2Name(ref_id)); */ + UTI_IPNetworkToHost(&reply.data.tracking.ip_addr, &ip_addr); + if (ip_addr.family == IPADDR_UNSPEC) { + ref_ip = UTI_RefidToString(ref_id); + } else if (no_dns) { + ref_ip = UTI_IPToString(&ip_addr); + } else { + DNS_IPAddress2Name(&ip_addr, host, sizeof (host)); + ref_ip = host; + } + + printf("Reference ID : %lu.%lu.%lu.%lu (%s)\n", a, b, c, d, ref_ip); printf("Stratum : %lu\n", (unsigned long) ntohl(reply.data.tracking.stratum)); UTI_TimevalNetworkToHost(&reply.data.tracking.ref_time, &ref_time); ref_time_tm = *gmtime((time_t *)&ref_time.tv_sec); diff --git a/cmdmon.c b/cmdmon.c index ca80255..eda4527 100644 --- a/cmdmon.c +++ b/cmdmon.c @@ -1370,6 +1370,7 @@ handle_tracking(CMD_Request *rx_message, CMD_Reply *tx_message) tx_message->status = htons(STT_SUCCESS); tx_message->reply = htons(RPY_TRACKING); tx_message->data.tracking.ref_id = htonl(rpt.ref_id); + UTI_IPHostToNetwork(&rpt.ip_addr, &tx_message->data.tracking.ip_addr); tx_message->data.tracking.stratum = htonl(rpt.stratum); UTI_TimevalHostToNetwork(&rpt.ref_time, &tx_message->data.tracking.ref_time); tx_message->data.tracking.current_correction_s = htonl(rpt.current_correction.tv_sec); @@ -1394,6 +1395,7 @@ handle_sourcestats(CMD_Request *rx_message, CMD_Reply *tx_message) if (status) { tx_message->status = htons(STT_SUCCESS); tx_message->reply = htons(RPY_SOURCESTATS); + tx_message->data.sourcestats.ref_id = htonl(report.ref_id); UTI_IPHostToNetwork(&report.ip_addr, &tx_message->data.sourcestats.ip_addr); tx_message->data.sourcestats.n_samples = htonl(report.n_samples); tx_message->data.sourcestats.n_runs = htonl(report.n_runs); diff --git a/reference.c b/reference.c index 1be04b1..25e9cf6 100644 --- a/reference.c +++ b/reference.c @@ -47,6 +47,7 @@ static NTP_Leap our_leap_status; static int our_leap_sec; static int our_stratum; static unsigned long our_ref_id; +static IPAddr our_ref_ip; struct timeval our_ref_time; /* Stored relative to reference, NOT local time */ static double our_offset; static double our_skew; @@ -360,6 +361,7 @@ void REF_SetReference(int stratum, NTP_Leap leap, unsigned long ref_id, + IPAddr *ref_ip, struct timeval *ref_time, double offset, double frequency, @@ -407,6 +409,10 @@ REF_SetReference(int stratum, are_we_synchronised = 1; our_stratum = stratum + 1; our_ref_id = ref_id; + if (ref_ip) + our_ref_ip = *ref_ip; + else + our_ref_ip.family = IPADDR_UNSPEC; our_ref_time = *ref_time; our_offset = offset; our_root_delay = root_delay; @@ -474,7 +480,7 @@ REF_SetReference(int stratum, fprintf(logfile, "%s %-15s %2d %10.3f %10.3f %10.3e\n", UTI_TimeToLogForm(ref_time->tv_sec), - UTI_IPToDottedQuad(our_ref_id), + our_ref_ip.family != IPADDR_UNSPEC ? UTI_IPToString(&our_ref_ip) : UTI_RefidToString(our_ref_id), our_stratum, abs_freq_ppm, 1.0e6*our_skew, @@ -707,6 +713,7 @@ REF_GetTrackingReport(RPT_TrackingReport *rep) extra_dispersion = (our_skew + fabs(our_residual_freq)) * elapsed; rep->ref_id = our_ref_id; + rep->ip_addr = our_ref_ip; rep->stratum = our_stratum; rep->ref_time = our_ref_time; UTI_DoubleToTimeval(correction, &rep->current_correction); @@ -719,6 +726,7 @@ REF_GetTrackingReport(RPT_TrackingReport *rep) } else if (enable_local_stratum) { rep->ref_id = LOCAL_REFERENCE_ID; + rep->ip_addr.family = IPADDR_UNSPEC; rep->stratum = local_stratum; rep->ref_time = now_cooked; UTI_DoubleToTimeval(correction, &rep->current_correction); @@ -731,6 +739,7 @@ REF_GetTrackingReport(RPT_TrackingReport *rep) } else { rep->ref_id = 0UL; + rep->ip_addr.family = IPADDR_UNSPEC; rep->stratum = 0; rep->ref_time.tv_sec = 0; rep->ref_time.tv_usec = 0; diff --git a/reference.h b/reference.h index 19b3f0e..cffadb9 100644 --- a/reference.h +++ b/reference.h @@ -110,6 +110,7 @@ extern void REF_SetReference int stratum, NTP_Leap leap, unsigned long ref_id, + IPAddr *ref_ip, struct timeval *ref_time, double offset, double frequency, diff --git a/reports.h b/reports.h index 763b80d..10ecde9 100644 --- a/reports.h +++ b/reports.h @@ -55,6 +55,7 @@ typedef struct { typedef struct { unsigned long ref_id; + IPAddr ip_addr; unsigned long stratum; struct timeval ref_time; struct timeval current_correction; @@ -66,6 +67,7 @@ typedef struct { } RPT_TrackingReport; typedef struct { + unsigned long ref_id; IPAddr ip_addr; unsigned long n_samples; unsigned long n_runs; diff --git a/sources.c b/sources.c index ffe63cd..b77a5b7 100644 --- a/sources.c +++ b/sources.c @@ -733,6 +733,7 @@ SRC_SelectSource(unsigned long match_addr) REF_SetReference(min_stratum, leap_status, sources[selected_source_index]->ref_id, + sources[selected_source_index]->ip_addr, &now, src_offset, src_frequency, @@ -960,6 +961,7 @@ SRC_ReportSourcestats(int index, RPT_SourcestatsReport *report) return 0; } else { src = sources[index]; + report->ref_id = src->ref_id; if (src->ip_addr) report->ip_addr = *src->ip_addr; else