From 9ef7ea2bcb75ad64017ee280de985c651f8ba929 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Thu, 5 Sep 2019 17:03:38 +0200 Subject: [PATCH] reference: rework adjustment of reference timestamp Instead of converting the reference timestamp to the NTP format and back, add a negative double value to the timestamp directly. Move the code to a separate function. This will allow the timestamp to stay outside the compiled-in NTP era, which is useful for testing of the cmdmon protocol. --- reference.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/reference.c b/reference.c index 40b7482..cdc6196 100644 --- a/reference.c +++ b/reference.c @@ -959,6 +959,18 @@ get_clock_estimates(int manual, /* ================================================== */ +static void +fuzz_ref_time(struct timespec *ts) +{ + uint32_t rnd; + + /* Add a random value from interval [-1.0, 0.0] */ + UTI_GetRandomBytes(&rnd, sizeof (rnd)); + UTI_AddDoubleToTimespec(ts, -(double)rnd / (uint32_t)-1, ts); +} + +/* ================================================== */ + void REF_SetReference(int stratum, NTP_Leap leap, int combined_sources, uint32_t ref_id, IPAddr *ref_ip, struct timespec *ref_time, @@ -970,7 +982,6 @@ REF_SetReference(int stratum, NTP_Leap leap, int combined_sources, double residual_frequency, local_abs_frequency; double elapsed, update_interval, correction_rate, orig_root_distance; struct timespec now, raw_now; - NTP_int64 ref_fuzz; int manual; assert(initialised); @@ -1065,11 +1076,7 @@ REF_SetReference(int stratum, NTP_Leap leap, int combined_sources, /* Add a random error of up to one second to the reference time to make it less useful when disclosed to NTP and cmdmon clients for estimating receive timestamps in the interleaved symmetric NTP mode */ - UTI_GetNtp64Fuzz(&ref_fuzz, 0); - UTI_TimespecToNtp64(&our_ref_time, &ref_fuzz, &ref_fuzz); - UTI_Ntp64ToTimespec(&ref_fuzz, &our_ref_time); - if (UTI_CompareTimespecs(&our_ref_time, ref_time) >= 0) - our_ref_time.tv_sec--; + fuzz_ref_time(&our_ref_time); local_abs_frequency = LCL_ReadAbsoluteFrequency();