Debian Codegraph @

pppd/magic.c

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