1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// src/magic.c
|
4 |
|
|
//
|
5 |
|
|
//==========================================================================
|
6 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
7 |
|
|
// -------------------------------------------
|
8 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
9 |
|
|
// Copyright (C) 2003 Free Software Foundation, Inc.
|
10 |
|
|
//
|
11 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
12 |
|
|
// the terms of the GNU General Public License as published by the Free
|
13 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
14 |
|
|
// version.
|
15 |
|
|
//
|
16 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
17 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
18 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
19 |
|
|
// for more details.
|
20 |
|
|
//
|
21 |
|
|
// You should have received a copy of the GNU General Public License
|
22 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
23 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
24 |
|
|
//
|
25 |
|
|
// As a special exception, if other files instantiate templates or use
|
26 |
|
|
// macros or inline functions from this file, or you compile this file
|
27 |
|
|
// and link it with other works to produce a work based on this file,
|
28 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
29 |
|
|
// the GNU General Public License. However the source code for this file
|
30 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
31 |
|
|
// General Public License v2.
|
32 |
|
|
//
|
33 |
|
|
// This exception does not invalidate any other reasons why a work based
|
34 |
|
|
// on this file might be covered by the GNU General Public License.
|
35 |
|
|
// -------------------------------------------
|
36 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
37 |
|
|
// ####BSDALTCOPYRIGHTBEGIN####
|
38 |
|
|
// -------------------------------------------
|
39 |
|
|
// Portions of this software may have been derived from FreeBSD, OpenBSD,
|
40 |
|
|
// or other sources, and if so are covered by the appropriate copyright
|
41 |
|
|
// and license included herein.
|
42 |
|
|
// -------------------------------------------
|
43 |
|
|
// ####BSDALTCOPYRIGHTEND####
|
44 |
|
|
//==========================================================================
|
45 |
|
|
|
46 |
|
|
/*
|
47 |
|
|
* magic.c - PPP Magic Number routines.
|
48 |
|
|
*
|
49 |
|
|
* Copyright (c) 1989 Carnegie Mellon University.
|
50 |
|
|
* All rights reserved.
|
51 |
|
|
*
|
52 |
|
|
* Redistribution and use in source and binary forms are permitted
|
53 |
|
|
* provided that the above copyright notice and this paragraph are
|
54 |
|
|
* duplicated in all such forms and that any documentation,
|
55 |
|
|
* advertising materials, and other materials related to such
|
56 |
|
|
* distribution and use acknowledge that the software was developed
|
57 |
|
|
* by Carnegie Mellon University. The name of the
|
58 |
|
|
* University may not be used to endorse or promote products derived
|
59 |
|
|
* from this software without specific prior written permission.
|
60 |
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
61 |
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
62 |
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
63 |
|
|
*/
|
64 |
|
|
|
65 |
|
|
#ifndef lint
|
66 |
|
|
//static char rcsid[] = "$FreeBSD: src/usr.sbin/pppd/magic.c,v 1.8 1999/08/28 01:19:05 peter Exp $";
|
67 |
|
|
#endif
|
68 |
|
|
|
69 |
|
|
#include <stdio.h>
|
70 |
|
|
#include <unistd.h>
|
71 |
|
|
#include <sys/types.h>
|
72 |
|
|
#include <sys/time.h>
|
73 |
|
|
|
74 |
|
|
#include "cyg/ppp/pppd.h"
|
75 |
|
|
#include "cyg/ppp/magic.h"
|
76 |
|
|
|
77 |
|
|
extern long mrand48 __P((void));
|
78 |
|
|
extern void srand48 __P((long));
|
79 |
|
|
|
80 |
|
|
/*
|
81 |
|
|
* magic_init - Initialize the magic number generator.
|
82 |
|
|
*
|
83 |
|
|
* Attempts to compute a random number seed which will not repeat.
|
84 |
|
|
* The current method uses the current hostid, current process ID
|
85 |
|
|
* and current time, currently.
|
86 |
|
|
*/
|
87 |
|
|
void
|
88 |
|
|
magic_init()
|
89 |
|
|
{
|
90 |
|
|
long seed;
|
91 |
|
|
struct timeval t;
|
92 |
|
|
|
93 |
|
|
gettimeofday(&t, NULL);
|
94 |
|
|
#ifndef __ECOS
|
95 |
|
|
seed = get_host_seed() ^ t.tv_sec ^ t.tv_usec ^ getpid();
|
96 |
|
|
#else
|
97 |
|
|
seed = get_host_seed() ^ t.tv_sec ^ t.tv_usec ^ 42;
|
98 |
|
|
#endif
|
99 |
|
|
srand48(seed);
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
/*
|
103 |
|
|
* magic - Returns the next magic number.
|
104 |
|
|
*/
|
105 |
|
|
u_int32_t
|
106 |
|
|
magic()
|
107 |
|
|
{
|
108 |
|
|
return (u_int32_t) mrand48();
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
#ifdef NO_DRAND48
|
112 |
|
|
/*
|
113 |
|
|
* Substitute procedures for those systems which don't have
|
114 |
|
|
* drand48 et al.
|
115 |
|
|
*/
|
116 |
|
|
|
117 |
|
|
double
|
118 |
|
|
drand48()
|
119 |
|
|
{
|
120 |
|
|
return (double)random() / (double)0x7fffffffL; /* 2**31-1 */
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
long
|
124 |
|
|
mrand48()
|
125 |
|
|
{
|
126 |
|
|
return random();
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
void
|
130 |
|
|
srand48(seedval)
|
131 |
|
|
long seedval;
|
132 |
|
|
{
|
133 |
|
|
srandom((int)seedval);
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
#endif
|