]> git.ozlabs.org Git - ppp.git/blob - pppd/magic.c
add demand dialling support; rename fd to ttyfd
[ppp.git] / pppd / magic.c
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 #ifndef lint
21 static char rcsid[] = "$Id: magic.c,v 1.5 1995/06/06 01:52:25 paulus Exp $";
22 #endif
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/time.h>
27
28 #include "pppd.h"
29 #include "magic.h"
30
31 static u_int32_t next;          /* Next value to return */
32
33 extern int gethostid __P((void));
34 extern long mrand48 __P((void));
35 extern void srand48 __P((long));
36
37
38 /*
39  * magic_init - Initialize the magic number generator.
40  *
41  * Attempts to compute a random number seed which will not repeat.
42  * The current method uses the current hostid, current process ID
43  * and current time, currently.
44  */
45 void
46 magic_init()
47 {
48     long seed;
49     struct timeval t;
50
51     gettimeofday(&t, NULL);
52     seed = gethostid() ^ t.tv_sec ^ t.tv_usec ^ getpid();
53     srand48(seed);
54 }
55
56 /*
57  * magic - Returns the next magic number.
58  */
59 u_int32_t
60 magic()
61 {
62     return (u_int32_t) mrand48();
63 }
64
65 #ifdef NO_DRAND48
66 /*
67  * Substitute procedures for those systems which don't have
68  * drand48 et al.
69  */
70
71 double
72 drand48()
73 {
74     return (double)random() / (double)0x7fffffffL; /* 2**31-1 */
75 }
76
77 long
78 mrand48()
79 {
80     return random();
81 }
82
83 void
84 srand48(seedval)
85 long seedval;
86 {
87     srandom((int)seedval);
88 }
89
90 #endif