Commit | Line | Data |
---|---|---|
2a905848 RG |
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 | ||
f381ea37 JH |
20 | #ifndef lint |
21 | static char rcsid[] = "$Id: magic.c,v 1.1 1993/11/11 03:54:25 paulus Exp $"; | |
22 | #endif | |
23 | ||
2a905848 RG |
24 | #include <stdio.h> |
25 | #include <sys/types.h> | |
26 | #include <sys/time.h> | |
27 | ||
28 | #include "magic.h" | |
29 | ||
30 | ||
31 | static u_long next; /* Next value to return */ | |
32 | ||
33 | extern u_long gethostid __ARGS((void)); | |
34 | extern long random __ARGS((void)); | |
35 | extern void srandom __ARGS((int)); | |
36 | ||
37 | ||
38 | /* | |
39 | * magic_init - Initialize the magic number generator. | |
40 | * | |
41 | * Computes first magic number and seed for random number generator. | |
42 | * Attempts to compute a random number seed which will not repeat. | |
43 | * The current method uses the current hostid and current time. | |
44 | */ | |
45 | void magic_init() | |
46 | { | |
47 | struct timeval tv; | |
48 | ||
49 | next = gethostid(); | |
50 | if (gettimeofday(&tv, NULL)) { | |
51 | perror("gettimeofday"); | |
52 | exit(1); | |
53 | } | |
54 | next ^= (u_long) tv.tv_sec ^ (u_long) tv.tv_usec; | |
55 | ||
56 | srandom((int) next); | |
57 | } | |
58 | ||
59 | ||
60 | /* | |
61 | * magic - Returns the next magic number. | |
62 | */ | |
63 | u_long magic() | |
64 | { | |
65 | u_long m; | |
66 | ||
67 | m = next; | |
68 | next = (u_long) random(); | |
69 | return (m); | |
70 | } |