1 /* $OpenBSD: magic.c,v 1.9 2009/10/27 23:59:53 deraadt Exp $ */ 2 3 /* 4 * magic.c - PPP Magic Number routines. 5 * 6 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. The name "Carnegie Mellon University" must not be used to 21 * endorse or promote products derived from this software without 22 * prior written permission. For permission or any legal 23 * details, please contact 24 * Office of Technology Transfer 25 * Carnegie Mellon University 26 * 5000 Forbes Avenue 27 * Pittsburgh, PA 15213-3890 28 * (412) 268-4387, fax: (412) 268-7395 29 * tech-transfer@andrew.cmu.edu 30 * 31 * 4. Redistributions of any form whatsoever must retain the following 32 * acknowledgment: 33 * "This product includes software developed by Computing Services 34 * at Carnegie Mellon University (http://www.cmu.edu/computing/)." 35 * 36 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO 37 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 38 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE 39 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 40 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 41 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 42 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 43 */ 44 45 #include <stdio.h> 46 #include <unistd.h> 47 #include <sys/types.h> 48 #include <sys/time.h> 49 #include <stdlib.h> 50 51 #include "pppd.h" 52 #include "magic.h" 53 54 extern long mrand48(void); 55 extern void srand48(long); 56 57 /* 58 * magic_init - Initialize the magic number generator. 59 * 60 * Attempts to compute a random number seed which will not repeat. 61 * The current method uses the current hostid, current process ID 62 * and current time, currently. 63 */ 64 void 65 magic_init() 66 { 67 #if 0 68 long seed; 69 struct timeval t; 70 71 gettimeofday(&t, NULL); 72 seed = get_host_seed() ^ t.tv_sec ^ t.tv_usec ^ getpid(); 73 srand48(seed); 74 #endif 75 } 76 77 /* 78 * magic - Returns the next magic number. 79 */ 80 u_int32_t 81 magic() 82 { 83 #if 0 84 return (u_int32_t) mrand48(); 85 #else 86 return arc4random(); 87 #endif 88 } 89 90 #ifdef NO_DRAND48 91 /* 92 * Substitute procedures for those systems which don't have 93 * drand48 et al. 94 */ 95 96 double 97 drand48() 98 { 99 return (double)random() / (double)0x7fffffffL; /* 2**31-1 */ 100 } 101 102 long 103 mrand48() 104 { 105 return random(); 106 } 107 108 void 109 srand48(seedval) 110 long seedval; 111 { 112 srandom((int)seedval); 113 } 114 115 #endif 116