summaryrefslogtreecommitdiff
path: root/libtac/lib/magic.c
blob: d7950887a654e5306ac9601534bf89624645584b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* magic.c - PPP Magic Number routines.
 * 
 * Copyright (C) 1989 Carnegie Mellon University.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program - see the file COPYING.
 *
 * See `CHANGES' file for revision history.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>

/* u_int32_t support for sun */
#ifdef sun
typedef unsigned int u_int32_t;
#endif

#include "magic.h"

#ifndef __linux__
extern long mrand48 __P((void));
extern void srand48 __P((long));
#else
#include <sys/stat.h>
#include <fcntl.h>

/* on Linux we use /dev/urandom as random numbers source 
   I find it really cool :) */
int rfd = -1;	/* /dev/urandom */
int magic_inited = 0;
#endif

/*
 * magic_init - Initialize the magic number generator.
 *
 * Attempts to compute a random number seed which will not repeat.
 * The current method uses the current hostid, current process ID
 * and current time, currently.
 */
void
magic_init()
{
    long seed;
    struct timeval t;

    magic_inited = 1;
#ifdef __linux__
    rfd = open("/dev/urandom", O_RDONLY);
    if(rfd != -1) 
        return;
#endif
    /* if /dev/urandom fails, we try traditional method */
    gettimeofday(&t, NULL);
    seed = gethostid() ^ t.tv_sec ^ t.tv_usec ^ getpid();
    srand48(seed);
}

/*
 * magic - Returns the next magic number.
 */
u_int32_t
magic()
{
#ifdef __linux__
    u_int32_t ret = 0;
    int bytes = 0;

    if (magic_inited == 0 )
        magic_init();

	if(rfd > -1) {
            bytes = read(rfd, &ret, sizeof(ret));
            return ret;
        }
	else
            return (u_int32_t) mrand48();
#else
    return (u_int32_t) mrand48();
#endif
}

#ifdef NO_DRAND48
/*
 * Substitute procedures for those systems which don't have
 * drand48 et al.
 */

double
drand48()
{
    return (double)random() / (double)0x7fffffffL; /* 2**31-1 */
}

long
mrand48()
{
    return random();
}

void
srand48(seedval)
long seedval;
{
    srandom((int)seedval);
}

#endif