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