From: Harlan Stenn Date: Wed, 3 Aug 2005 00:19:27 +0000 (-0400) Subject: libntp/random.c now always provides ntp_[s]random(). Use them instead of RANDOM... X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32645894fe731eefbb61f6771b6796fdeb4f033d;p=thirdparty%2Fntp.git libntp/random.c now always provides ntp_[s]random(). Use them instead of RANDOM and SRANDOM. bk: 42f00d8fmVWhZW34W-3DASbeRxo5EA --- diff --git a/configure.ac b/configure.ac index 31a460a763..251f7069b3 100644 --- a/configure.ac +++ b/configure.ac @@ -984,7 +984,7 @@ case "$host" in *) AC_CHECK_FUNCS(mlockall) ;; esac -AC_CHECK_FUNCS(mrand48 srand48 nice nlist) +AC_CHECK_FUNCS(nice nlist) case "$host" in *-*-solaris2.6) # Broken... @@ -993,10 +993,6 @@ case "$host" in ;; esac AC_CHECK_FUNCS(plock pututline pututxline readlink recvmsg rtprio) -case "$ac_cv_func_mrand48" in - yes) ;; - *) AC_REPLACE_FUNCS(random) ;; -esac case "$host" in *-*-aix[[45]]*) # Just a stub in AIX 4. Idiots. @@ -1186,10 +1182,8 @@ case "$host" in AC_DEFINE(DECL_ADJTIME_0, 1, [Declaration style]) AC_DEFINE(DECL_INET_NTOA_0, 1, [Declaration style]) AC_DEFINE(DECL_MKTEMP_0, 1, [Declaration style]) - AC_DEFINE(DECL_MRAND48_0, 1, [Declaration style]) AC_DEFINE(DECL_SELECT_0, 1, [Declaration style]) AC_DEFINE(DECL_SETITIMER_0, 1, [Declaration style]) - AC_DEFINE(DECL_SRAND48_0, 1, [Declaration style]) AC_DEFINE(DECL_SYSLOG_0, 1, [Declaration style]) AC_DEFINE(DECL_TIMEOFDAY_0, 1, [Declaration style]) ;; @@ -1236,13 +1230,11 @@ case "$host" in AC_DEFINE(DECL_MEMMOVE_0, 1, [Declaration style]) AC_DEFINE(DECL_MKTEMP_0, 1, [Declaration style]) AC_DEFINE(DECL_MKSTEMP_0, 1, [Declaration style]) - AC_DEFINE(DECL_MRAND48_0, 1, [Declaration style]) AC_DEFINE(DECL_RENAME_0, 1, [Declaration style]) AC_DEFINE(DECL_SELECT_0, 1, [Declaration style]) AC_DEFINE(DECL_SETITIMER_0, 1, [Declaration style]) AC_DEFINE(DECL_SETPRIORITY_0, 1, [Declaration style]) AC_DEFINE(DECL_SIGVEC_0, 1, [Declaration style]) - AC_DEFINE(DECL_SRAND48_0, 1, [Declaration style]) case "`basename $ac_cv_prog_CC`" in acc*) ;; *) AC_DEFINE(DECL_STDIO_0, 1, [Declaration style]) @@ -1263,13 +1255,11 @@ case "$host" in AC_DEFINE(DECL_IOCTL_0, 1, [Declaration style]) AC_DEFINE(DECL_IPC_0, 1, [Declaration style]) AC_DEFINE(DECL_MKTEMP_0, 1, [Declaration style]) - AC_DEFINE(DECL_MRAND48_0, 1, [Declaration style]) AC_DEFINE(DECL_NLIST_0, 1, [Declaration style]) AC_DEFINE(DECL_PLOCK_0, 1, [Declaration style]) AC_DEFINE(DECL_SELECT_0, 1, [Declaration style]) AC_DEFINE(DECL_SETITIMER_0, 1, [Declaration style]) AC_DEFINE(DECL_SETPRIORITY_0, 1, [Declaration style]) - AC_DEFINE(DECL_SRAND48_0, 1, [Declaration style]) AC_DEFINE(DECL_STIME_0, 1, [Declaration style]) AC_DEFINE(DECL_SYSLOG_0, 1, [Declaration style]) AC_DEFINE(DECL_TIMEOFDAY_0, 1, [Declaration style]) diff --git a/include/l_stdlib.h b/include/l_stdlib.h index 3f242b7994..58bbeedc21 100644 --- a/include/l_stdlib.h +++ b/include/l_stdlib.h @@ -104,10 +104,6 @@ extern int mkstemp P((char *)); extern char *mktemp P((char *)); #endif -#ifdef DECL_MRAND48_0 -extern long mrand48 P((void)); -#endif - #ifdef DECL_NLIST_0 struct nlist; extern int nlist P((const char *, struct nlist *)); @@ -156,10 +152,6 @@ extern int snprintf P((char *, size_t, const char *, ...)); extern int vsnprintf P((char *, size_t, const char *, va_list)); #endif -#ifdef DECL_SRAND48_0 -extern void srand48 P((long)); -#endif - #ifdef DECL_STDIO_0 #if defined(FILE) || defined(BUFSIZ) extern int _flsbuf P((int, FILE *)); @@ -276,10 +268,7 @@ extern int fork P((void)); extern int getdtablesize P((void)); extern int qsort (void *, int , int, int P((*compar)(void *, void *))); -extern long random P((void)); -extern long mrand48 P((void)); extern int setpgrp P((int, int)); -extern void srandom P((unsigned int)); extern void bcopy P((const char *, char *, int)); #endif @@ -481,8 +470,6 @@ extern int execve P((char *, char **,char **)); extern int fork P((void)); extern int getdtablesize P((void)); extern int ran P((void)); -extern int rand P((void)); -extern void srand P((unsigned int)); #ifdef _TIME_H_ extern int gettimeofday P((struct timeval *, struct timezone *)); extern int settimeofday P((struct timeval *, struct timezone *)); diff --git a/include/ntp.h b/include/ntp.h index 136742bfa4..528726f5df 100644 --- a/include/ntp.h +++ b/include/ntp.h @@ -697,15 +697,7 @@ struct pkt { * How we randomize polls. The poll interval is a power of two. We chose * a random interval which is this value plus-minus one second. */ -#if defined(HAVE_MRAND48) -# define RANDOM (mrand48()) -# define SRANDOM(x) (srand48(x)) -#else -# define RANDOM (random()) -# define SRANDOM(x) (srandom(x)) -#endif - -#define RANDPOLL(x) ((1 << (x)) - 1 + (RANDOM & 0x3)) +#define RANDPOLL(x) ((1 << (x)) - 1 + (ntp_random() & 0x3)) /* * min, min3 and max. Makes it easier to transliterate the spec without diff --git a/include/ntp_machine.h b/include/ntp_machine.h index 850606cb24..0883230621 100644 --- a/include/ntp_machine.h +++ b/include/ntp_machine.h @@ -353,8 +353,6 @@ extern void alarm P((int seconds)); #define getclock clock_gettime #define fcntl ioctl #define _getch getchar -#define random rand -#define srandom srand /* define this away for vxWorks */ #define openlog(x,y) diff --git a/include/ntpd.h b/include/ntpd.h index 995f8a57d7..af29f62a6f 100644 --- a/include/ntpd.h +++ b/include/ntpd.h @@ -20,6 +20,9 @@ void worker_thread (void *); #define sleep(x) Sleep((DWORD) x * 1000 /* milliseconds */ ); #endif /* SYS_WINNT */ +extern void ntp_srandom P((unsigned long)); +extern int ntp_random P((void)); + /* ntp_config.c */ extern void getconfig P((int, char **)); diff --git a/libntp/Makefile.am b/libntp/Makefile.am index 544dca4e2c..e2ddb76071 100644 --- a/libntp/Makefile.am +++ b/libntp/Makefile.am @@ -11,7 +11,7 @@ libntp_a_SRCS = a_md5encrypt.c adjtime.c atoint.c atolfp.c atouint.c \ lib_strbuf.c machines.c md5c.c memmove.c mfptoa.c \ mfptoms.c mktime.c modetoa.c mstolfp.c msutotsf.c msyslog.c netof.c \ ntp_rfc2553.c numtoa.c numtohost.c octtoint.c prettydate.c \ - recvbuff.c refnumtoa.c snprintf.c socktoa.c socktohost.c \ + random.c recvbuff.c refnumtoa.c snprintf.c socktoa.c socktohost.c \ statestr.c strdup.c strerror.c strstr.c \ syssignal.c tsftomsu.c tstotv.c tvtoa.c tvtots.c \ uglydate.c uinttoa.c utvtoa.c ymd2yd.c \ @@ -25,7 +25,7 @@ libntp_a_SRCS = a_md5encrypt.c adjtime.c atoint.c atolfp.c atouint.c \ $(srcdir)/../libisc/netscope.c libntp_a_SOURCES = systime.c $(libntp_a_SRCS) libntpsim_a_SOURCES = systime_s.c $(libntp_a_SRCS) -EXTRA_libntp_a_SOURCES = adjtimex.c random.c +EXTRA_libntp_a_SOURCES = adjtimex.c INCLUDES = -I$(top_srcdir)/include -I../include ETAGS_ARGS = Makefile.am diff --git a/libntp/random.c b/libntp/random.c index c8d7b70204..e59fc854ab 100644 --- a/libntp/random.c +++ b/libntp/random.c @@ -218,7 +218,7 @@ static long rand_sep = SEP_3; static long *end_ptr = &randtbl[DEG_3 + 1]; static inline long good_rand P((long)); -long random P((void)); +long ntp_random P((void)); static inline long good_rand ( @@ -265,7 +265,7 @@ good_rand ( * for default usage relies on values produced by this routine. */ void -srandom( +ntp_srandom( unsigned long x ) { @@ -280,7 +280,7 @@ srandom( fptr = &state[rand_sep]; rptr = &state[0]; for (i = 0; i < 10 * rand_deg; i++) - (void)random(); + (void)ntp_random(); } } @@ -296,13 +296,13 @@ srandom( * a fixed seed. */ void -srandomdev( void ) +ntp_srandomdev( void ) { struct timeval tv; unsigned long junk; /* Purposely used uninitialized */ gettimeofday(&tv, NULL); - srandom(getpid() ^ tv.tv_sec ^ tv.tv_usec ^ junk); + ntp_srandom(getpid() ^ tv.tv_sec ^ tv.tv_usec ^ junk); return; } @@ -330,7 +330,7 @@ srandomdev( void ) * complain about mis-alignment, but you should disregard these messages. */ char * -initstate( +ntp_initstate( unsigned long seed, /* seed for R.N.G. */ char *arg_state, /* pointer to state array */ long n /* # bytes of state info */ @@ -371,7 +371,7 @@ initstate( } state = (long *) (long_arg_state + 1); /* first location */ end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ - srandom(seed); + ntp_srandom(seed); if (rand_type == TYPE_0) long_arg_state[0] = rand_type; else @@ -399,7 +399,7 @@ initstate( * complain about mis-alignment, but you should disregard these messages. */ char * -setstate( +ntp_setstate( char *arg_state /* pointer to state array */ ) { @@ -453,7 +453,7 @@ setstate( * Returns a 31-bit random number. */ long -random( void ) +ntp_random( void ) { register long i; register long *f, *r; diff --git a/libntp/ranny.c b/libntp/ranny.c deleted file mode 100644 index d6c4cc9d25..0000000000 --- a/libntp/ranny.c +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Random number generator is: - * - * Copyright 1988 by Rayan S. Zachariassen, all rights reserved. - * This will be free software, but only when it is finished. - * - * Used in ntp by permission of the author. If copyright is - * annoying to you, read no further. Instead, look up the reference, - * write me an equivalent to this and send it back to me. - */ - -/* - * Random number generator; see Knuth Vol 2. 2nd ed. p.27 (section 3.2.2) - */ -#include "ntp_stdlib.h" - -/* - * 55 random numbers, not all even. Note we don't initialize ran_y - * directly since I have had thoughts of putting this in an EPROM - */ -static time_t ran_y[55]; - -static time_t init_ran_y[55] = { - 1860909544, 231033423, 437666411, 1349655137, 2014584962, - 504613712, 656256107, 1246027206, 573713775, 643466871, - 540235388, 1630565153, 443649364, 729302839, 1933991552, - 944681982, 949111118, 406212522, 1065063137, 1712954727, - 73280612, 787623973, 1874130997, 801658492, 73395958, - 739165367, 596047144, 490055249, 1131094323, 662727104, - 483614097, 844520219, 893760527, 921280508, 46691708, - 760861842, 1425894220, 702947816, 2006889048, 1999607995, - 1346414687, 399640789, 1482689501, 1790064052, 1128943628, - 1269197405, 587262386, 2078054746, 1675409928, 1652325524, - 1643525825, 1748690540, 292465849, 1370173174, 402865384 -}; - -static int ran_j; -static int ran_k; - - -/* - * ranp2 - return a random integer in the range 0 .. (1 << m) - 1 - */ -u_long -ranp2( - int m - ) -{ - time_t r; - - ran_y[ran_k] += ran_y[ran_j]; /* overflow does a mod */ - r = ran_y[ran_k]; - if (ran_k-- == 0) - ran_k = 54; - if (ran_j-- == 0) - ran_j = 54; - return (u_long)(r & ((1 << m ) - 1)); -} - -/* - * init_random - do initialization of random number routine - */ -void -init_random(void) -{ - register int i; - register time_t now; - - ran_j = 23; - ran_k = 54; - - /* - * Randomize the seed array some more. The time of day - * should be initialized by now. - */ - now = time((time_t *)0) | 01; - - for (i = 0; i < 55; ++i) - ran_y[i] = now * init_ran_y[i]; /* overflow does a mod */ -} diff --git a/ntpd/ntp_config.c b/ntpd/ntp_config.c index c69adf6e37..3d8227ee53 100644 --- a/ntpd/ntp_config.c +++ b/ntpd/ntp_config.c @@ -1819,7 +1819,7 @@ getconfig( for (i = 0; i < 8; i++) for (j = 1; j < 100; ++j) { - rankey[i] = (char) (RANDOM & 0xff); + rankey[i] = (char) (ntp_random() & 0xff); if (rankey[i] != 0) break; } rankey[8] = 0; diff --git a/ntpd/ntp_crypto.c b/ntpd/ntp_crypto.c index b26def27ba..124887f6f7 100644 --- a/ntpd/ntp_crypto.c +++ b/ntpd/ntp_crypto.c @@ -274,7 +274,7 @@ make_keylist( * NTP_MAXKEY. */ while (1) { - keyid = (u_long)RANDOM & 0xffffffff; + keyid = (u_long)ntp_random(); /* 31 bits from ntp_random() */ if (keyid <= NTP_MAXKEY) continue; diff --git a/ntpd/ntp_monitor.c b/ntpd/ntp_monitor.c index b033dcd005..f998dcaba3 100644 --- a/ntpd/ntp_monitor.c +++ b/ntpd/ntp_monitor.c @@ -239,7 +239,8 @@ ntp_monitor( * Preempt from the MRU list if old enough. */ md = mon_mru_list.mru_prev; - if (((u_long)RANDOM & 0xffffffff) / FRAC > + /* We get 31 bits from ntp_random() */ + if (((u_long)ntp_random()) / FRAC > (double)(current_time - md->lasttime) / mon_age) return; diff --git a/ntpd/ntp_peer.c b/ntpd/ntp_peer.c index a35b9b5665..d6fd55d282 100644 --- a/ntpd/ntp_peer.c +++ b/ntpd/ntp_peer.c @@ -144,7 +144,7 @@ init_peer(void) /* * Initialize our first association ID */ - while ((current_association_ID = RANDOM & 0xffff) == 0); + while ((current_association_ID = ntp_random() & 0xffff) == 0); } diff --git a/ntpd/ntp_proto.c b/ntpd/ntp_proto.c index 9899941490..39c721b9d9 100644 --- a/ntpd/ntp_proto.c +++ b/ntpd/ntp_proto.c @@ -1524,7 +1524,7 @@ peer_clear( if (initializing) peer->nextdate = current_time + peer_associations; else - peer->nextdate = current_time + (RANDOM & ((1 << + peer->nextdate = current_time + (ntp_random() & ((1 << NTP_MINPOLL) - 1)); #ifdef DEBUG if (debug) diff --git a/ntpd/ntpd.c b/ntpd/ntpd.c index 8d782dcebf..ac92df92b2 100644 --- a/ntpd/ntpd.c +++ b/ntpd/ntpd.c @@ -464,7 +464,7 @@ ntpdmain( * Initialize random generator and public key pair */ get_systime(&now); - SRANDOM((int)(now.l_i * now.l_uf)); + ntp_srandom((int)(now.l_i * now.l_uf)); #ifdef HAVE_DNSREGISTRATION msyslog(LOG_INFO, "Attemping to register mDNS\n"); diff --git a/ntpd/ntpsim.c b/ntpd/ntpsim.c index b92084fdf0..73bdb6739b 100644 --- a/ntpd/ntpsim.c +++ b/ntpd/ntpsim.c @@ -80,7 +80,7 @@ ntpsim( * Watch out here, we want the real time, not the silly stuff. */ gettimeofday(&seed, NULL); - srand48(seed.tv_usec); + ntp_srandom(seed.tv_usec); /* * Push a beep and timer interrupt on the queue diff --git a/util/Makefile.am b/util/Makefile.am index 364d7cf86c..ae4202f8d4 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -9,7 +9,7 @@ EXTRA_PROGRAMS= audio-pcm byteorder hist jitter kern longsize \ INCLUDES= -I$(top_srcdir)/include # LDADD might need RESLIB and ADJLIB LDADD= ../libntp/libntp.a -ntp_keygen_LDADD= @LCRYPTO@ +ntp_keygen_LDADD= @LCRYPTO@ ../libntp/libntp.a #EXTRA_DIST= README TAGS EXTRA_DIST= ETAGS_ARGS= Makefile.am diff --git a/util/ntp-keygen.c b/util/ntp-keygen.c index 2cd763ec03..b76de1d3de 100644 --- a/util/ntp-keygen.c +++ b/util/ntp-keygen.c @@ -794,11 +794,11 @@ gen_md5( fprintf(stderr, "Generating MD5 keys...\n"); str = fheader("MD5key", hostname); - srandom(epoch); + ntp_srandom(epoch); for (i = 1; i <= MD5KEYS; i++) { for (j = 0; j < 16; j++) { while (1) { - temp = random() & 0xff; + temp = ntp_random() & 0xff; if (temp == '#') continue; if (temp > 0x20 && temp < 0x7f)