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