From: Juergen Perlinger Date: Sun, 13 Nov 2022 08:47:08 +0000 (+0100) Subject: [Bug 3640] sidekick: fixed "discard monitor" implementation X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dc7838cb9b23915d1ba4ead9481e170693733d1d;p=thirdparty%2Fntp.git [Bug 3640] sidekick: fixed "discard monitor" implementation bk: 6370af0c2dixLDDTTrOHMhX27Qc33A --- diff --git a/ChangeLog b/ChangeLog index 3addb7b81..51a731231 100644 --- a/ChangeLog +++ b/ChangeLog @@ -37,6 +37,8 @@ * [Bug 3672] fix biased selection in median cut * [Bug 3666] avoid unlimited receive buffer allocation - follow-up: fix inverted sense in check, reset shortfall counter +* [Bug 3640] ntp.conf: missing documentation for "discard monitor" default value + - sidekick: fixed a bug in discard monitor implementation * [Bug 3626] (SNTP) UTC offset calculation needs dst flag - applied patch by Gerry Garvey * [Bug 3432] refclocks that 'write()' should check the result diff --git a/include/ntp_random.h b/include/ntp_random.h index fa77f6553..931b21990 100644 --- a/include/ntp_random.h +++ b/include/ntp_random.h @@ -5,6 +5,7 @@ void ntp_crypto_srandom(void); int ntp_crypto_random_buf(void *buf, size_t nbytes); long ntp_random (void); +double ntp_uurandom(void); void ntp_srandom (unsigned long); void ntp_srandomdev (void); char * ntp_initstate (unsigned long, /* seed for R.N.G. */ diff --git a/libntp/ntp_random.c b/libntp/ntp_random.c index 0ada44f25..83806c420 100644 --- a/libntp/ntp_random.c +++ b/libntp/ntp_random.c @@ -497,3 +497,18 @@ ntp_random( void ) } return(i); } + +/* + * ntp_uurandom() + * + * Generate a Uniform-distributed Unity based random number. Replaces a + * few locations where the transformation was made in an ad-hoc style + * (and in one instance, wrong...) + * + * returns a number in [0.0 .. 1.0], both ends inclusive + */ +double +ntp_uurandom( void ) +{ + return (double)ntp_random() / 0x7FFFFFFFu; +} diff --git a/libntp/systime.c b/libntp/systime.c index a738c8d92..e2f07f71a 100644 --- a/libntp/systime.c +++ b/libntp/systime.c @@ -235,7 +235,7 @@ get_systime( * must scale up the result by 2.0 to cover the full fractional * range. */ - dfuzz = ntp_random() * 2. / FRAC * sys_fuzz; + dfuzz = ntp_uurandom() * sys_fuzz; DTOLFP(dfuzz, &lfpfuzz); L_ADD(&result, &lfpfuzz); diff --git a/ntpd/ntp_io.c b/ntpd/ntp_io.c index c0dcea598..6b88f5e4d 100644 --- a/ntpd/ntp_io.c +++ b/ntpd/ntp_io.c @@ -3444,7 +3444,7 @@ fetch_timestamp( break; #endif /* HAVE_TIMESTAMP */ } - fuzz = ntp_random() * 2. / FRAC * sys_fuzz; + fuzz = ntp_uurandom() * sys_fuzz; DTOLFP(fuzz, &lfpfuzz); L_ADD(&nts, &lfpfuzz); #ifdef DEBUG_TIMING diff --git a/ntpd/ntp_monitor.c b/ntpd/ntp_monitor.c index a07a1aaef..5252794fd 100644 --- a/ntpd/ntp_monitor.c +++ b/ntpd/ntp_monitor.c @@ -459,7 +459,7 @@ ntp_monitor( mon_getmoremem(); UNLINK_HEAD_SLIST(mon, mon_free, hash_next); /* Preempt from the MRU list if old enough. */ - } else if (ntp_random() / (2. * FRAC) > + } else if (ntp_uurandom() > (double)oldest_age / mon_age) { return ~(RES_LIMITED | RES_KOD) & flags; } else { diff --git a/ntpd/ntp_proto.c b/ntpd/ntp_proto.c index 898ff73ee..7992ef0f0 100644 --- a/ntpd/ntp_proto.c +++ b/ntpd/ntp_proto.c @@ -740,7 +740,7 @@ receive( * surviving packets. */ if (restrict_mask & RES_FLAKE) { - if ((double)ntp_random() / 0x7fffffff < .1) { + if (ntp_uurandom() < .1) { DPRINTF(2, ("receive: drop: RES_FLAKE\n")); sys_restricted++; return; /* no flakeway */