1/*	$NetBSD$	*/
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 <sys/cdefs.h>
46#ifndef lint
47#if 0
48#define RCSID	"Id: magic.c,v 1.11 2003/06/11 23:56:26 paulus Exp"
49#else
50__RCSID("$NetBSD$");
51#endif
52#endif
53
54#include <stdio.h>
55#include <stdlib.h>
56#include <unistd.h>
57#include <sys/types.h>
58#include <sys/time.h>
59
60#include "pppd.h"
61#include "magic.h"
62
63#ifdef RCSID
64static const char rcsid[] = RCSID;
65#endif
66
67extern long mrand48 __P((void));
68extern void srand48 __P((long));
69
70/*
71 * magic_init - Initialize the magic number generator.
72 *
73 * Attempts to compute a random number seed which will not repeat.
74 * The current method uses the current hostid, current process ID
75 * and current time, currently.
76 */
77void
78magic_init()
79{
80    long seed;
81    struct timeval t;
82
83    gettimeofday(&t, NULL);
84    seed = get_host_seed() ^ t.tv_sec ^ t.tv_usec ^ getpid();
85    srand48(seed);
86}
87
88/*
89 * magic - Returns the next magic number.
90 */
91u_int32_t
92magic()
93{
94    return (u_int32_t) mrand48();
95}
96
97/*
98 * random_bytes - Fill a buffer with random bytes.
99 */
100void
101random_bytes(unsigned char *buf, int len)
102{
103	int i;
104
105	for (i = 0; i < len; ++i)
106		buf[i] = mrand48() >> 24;
107}
108
109#ifdef NO_DRAND48
110/*
111 * Substitute procedures for those systems which don't have
112 * drand48 et al.
113 */
114
115double
116drand48()
117{
118    return (double)random() / (double)0x7fffffffL; /* 2**31-1 */
119}
120
121long
122mrand48()
123{
124    return random();
125}
126
127void
128srand48(seedval)
129long seedval;
130{
131    srandom((int)seedval);
132}
133
134#endif
135