Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/timed/timed timed(8): avoid hand-rolling arc4random...



details:   https://anonhg.NetBSD.org/src/rev/848c69b089ee
branches:  trunk
changeset: 990617:848c69b089ee
user:      nia <nia%NetBSD.org@localhost>
date:      Sun Oct 31 10:47:18 2021 +0000

description:
timed(8): avoid hand-rolling arc4random_uniform

diffstat:

 usr.sbin/timed/timed/timed.c |  28 ++++++----------------------
 1 files changed, 6 insertions(+), 22 deletions(-)

diffs (65 lines):

diff -r d369e4a057c8 -r 848c69b089ee usr.sbin/timed/timed/timed.c
--- a/usr.sbin/timed/timed/timed.c      Sun Oct 31 10:09:43 2021 +0000
+++ b/usr.sbin/timed/timed/timed.c      Sun Oct 31 10:47:18 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: timed.c,v 1.26 2018/02/04 09:01:13 mrg Exp $   */
+/*     $NetBSD: timed.c,v 1.27 2021/10/31 10:47:18 nia Exp $   */
 
 /*-
  * Copyright (c) 1985, 1993 The Regents of the University of California.
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)timed.c    8.2 (Berkeley) 3/26/95";
 #else
-__RCSID("$NetBSD: timed.c,v 1.26 2018/02/04 09:01:13 mrg Exp $");
+__RCSID("$NetBSD: timed.c,v 1.27 2021/10/31 10:47:18 nia Exp $");
 #endif
 #endif /* not lint */
 
@@ -239,11 +239,8 @@
                        err(EXIT_FAILURE, "bind");
        }
 
-       /* choose a unique seed for random number generation */
-       (void)gettimeofday(&ntime, 0);
-       srandom((unsigned long)(ntime.tv_sec + ntime.tv_usec));
-
-       sequence = (u_short)random();     /* initial seq number */
+       /* initial seq number */
+       sequence = (u_short)arc4random_uniform(UINT16_MAX);
 
        /* rounds kernel variable time to multiple of 5 ms. */
        ntime.tv_sec = 0;
@@ -334,11 +331,10 @@
 
 
        /* microseconds to delay before responding to a broadcast */
-       delay1 = casual(1L, 100*1000L);
+       delay1 = 1L + arc4random_uniform((100 * 1000L) - 1L);
 
        /* election timer delay in secs. */
-       delay2 = casual((long)MINTOUT, (long)MAXTOUT);
-
+       delay2 = MINTOUT + arc4random_uniform(MAXTOUT - MINTOUT);
 
        if (!debug) {
                daemon(debug, 0);
@@ -654,18 +650,6 @@
        makeslave(ntp);
 }
 
-/*
- * returns a random number in the range [inf, sup]
- */
-long
-casual(long inf, long sup)
-{
-       double value;
-
-       value = ((double)(random() & 0x7fffffff)) / (0x7fffffff*1.0);
-       return(inf + (sup - inf)*value);
-}
-
 char *
 date(void)
 {



Home | Main Index | Thread Index | Old Index