1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * magic.c - PPP Magic Number routines. 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * Copyright (c) 1989 Carnegie Mellon University. 5*7c478bd9Sstevel@tonic-gate * All rights reserved. 6*7c478bd9Sstevel@tonic-gate * 7*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted 8*7c478bd9Sstevel@tonic-gate * provided that the above copyright notice and this paragraph are 9*7c478bd9Sstevel@tonic-gate * duplicated in all such forms and that any documentation, 10*7c478bd9Sstevel@tonic-gate * advertising materials, and other materials related to such 11*7c478bd9Sstevel@tonic-gate * distribution and use acknowledge that the software was developed 12*7c478bd9Sstevel@tonic-gate * by Carnegie Mellon University. The name of the 13*7c478bd9Sstevel@tonic-gate * University may not be used to endorse or promote products derived 14*7c478bd9Sstevel@tonic-gate * from this software without specific prior written permission. 15*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 16*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 17*7c478bd9Sstevel@tonic-gate * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 18*7c478bd9Sstevel@tonic-gate */ 19*7c478bd9Sstevel@tonic-gate 20*7c478bd9Sstevel@tonic-gate #include <stdio.h> 21*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 22*7c478bd9Sstevel@tonic-gate #include <unistd.h> 23*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 24*7c478bd9Sstevel@tonic-gate #include <sys/time.h> 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate #include "pppd.h" 27*7c478bd9Sstevel@tonic-gate #include "magic.h" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #ifdef NO_DRAND48 30*7c478bd9Sstevel@tonic-gate long mrand48 __P((void)); 31*7c478bd9Sstevel@tonic-gate void srand48 __P((long)); 32*7c478bd9Sstevel@tonic-gate #endif 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate /* 35*7c478bd9Sstevel@tonic-gate * magic_init - Initialize the magic number generator. 36*7c478bd9Sstevel@tonic-gate * 37*7c478bd9Sstevel@tonic-gate * Attempts to compute a random number seed which will not repeat. 38*7c478bd9Sstevel@tonic-gate * The current method uses the current hostid, current process ID 39*7c478bd9Sstevel@tonic-gate * and current time, currently. 40*7c478bd9Sstevel@tonic-gate */ 41*7c478bd9Sstevel@tonic-gate void magic_init()42*7c478bd9Sstevel@tonic-gatemagic_init() 43*7c478bd9Sstevel@tonic-gate { 44*7c478bd9Sstevel@tonic-gate long seed; 45*7c478bd9Sstevel@tonic-gate struct timeval t; 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate (void) gettimeofday(&t, NULL); 48*7c478bd9Sstevel@tonic-gate seed = get_host_seed() ^ t.tv_sec ^ t.tv_usec ^ getpid(); 49*7c478bd9Sstevel@tonic-gate srand48(seed); 50*7c478bd9Sstevel@tonic-gate } 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate /* 53*7c478bd9Sstevel@tonic-gate * magic - Returns the next magic number. 54*7c478bd9Sstevel@tonic-gate */ 55*7c478bd9Sstevel@tonic-gate u_int32_t magic()56*7c478bd9Sstevel@tonic-gatemagic() 57*7c478bd9Sstevel@tonic-gate { 58*7c478bd9Sstevel@tonic-gate return (u_int32_t) mrand48(); 59*7c478bd9Sstevel@tonic-gate } 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate #ifdef NO_DRAND48 62*7c478bd9Sstevel@tonic-gate /* 63*7c478bd9Sstevel@tonic-gate * Substitute procedures for those systems which don't have 64*7c478bd9Sstevel@tonic-gate * drand48 et al. 65*7c478bd9Sstevel@tonic-gate */ 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate double drand48()68*7c478bd9Sstevel@tonic-gatedrand48() 69*7c478bd9Sstevel@tonic-gate { 70*7c478bd9Sstevel@tonic-gate return (double)random() / (double)0x7fffffffL; /* 2**31-1 */ 71*7c478bd9Sstevel@tonic-gate } 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate long mrand48()74*7c478bd9Sstevel@tonic-gatemrand48() 75*7c478bd9Sstevel@tonic-gate { 76*7c478bd9Sstevel@tonic-gate return random(); 77*7c478bd9Sstevel@tonic-gate } 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate void srand48(seedval)80*7c478bd9Sstevel@tonic-gatesrand48(seedval) 81*7c478bd9Sstevel@tonic-gate long seedval; 82*7c478bd9Sstevel@tonic-gate { 83*7c478bd9Sstevel@tonic-gate srandom((int)seedval); 84*7c478bd9Sstevel@tonic-gate } 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate #endif 87