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
122
123
124
125
126
127
128
129
130
131
|
/* 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>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "magic.h"
#ifdef _MSC_VER
# pragma section(".CRT$XCU",read)
# define INITIALIZER2_(f,p) \
static void f(void); \
__declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \
__pragma(comment(linker,"/include:" p #f "_")) \
static void f(void)
# ifdef _WIN64
# define INITIALIZER(f) INITIALIZER2_(f,"")
# else
# define INITIALIZER(f) INITIALIZER2_(f,"_")
# endif
#else /* __GNUC__ */
# define INITIALIZER(f) \
static void f(void) __attribute__((constructor)); \
static void f(void)
#endif
/* if OpenSSL library is available this legacy code will not be compiled in */
#if defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO)
#include <openssl/rand.h>
/*
* magic - Returns the next magic number.
*/
u_int32_t
magic()
{
u_int32_t num;
RAND_pseudo_bytes((unsigned char *)&num, sizeof(num));
return num;
}
#elif defined(HAVE_GETRANDOM)
# if defined(HAVE_LINUX_RANDOM_H)
# include <linux/random.h>
# elif defined(HAVE_SYS_RANDOM_H)
# include <sys/random.h>
# endif
/*
* magic - Returns the next magic number.
*/
u_int32_t
magic()
{
u_int32_t num;
getrandom(&num, sizeof(num), GRND_NONBLOCK);
return num;
}
#else
/*
* magic_init - Initialize the magic number generator.
*
* Attempts to compute a random number seed which will not repeat.
*/
INITIALIZER(magic_init)
{
struct stat statbuf;
long seed = 0;
struct timeval t;
// try to initialise seed from urandom
if (!lstat("/dev/urandom", &statbuf) && S_ISCHR(statbuf.st_mode)) {
int rfd = open("/dev/urandom", O_RDONLY);
if(rfd >= 0) {
(void) read(rfd, &seed, sizeof(seed));
close(rfd);
}
}
// fallback
gettimeofday(&t, NULL);
seed ^= gethostid() ^ t.tv_sec ^ t.tv_usec ^ getpid();
// finally seed the PRNG
srandom(seed);
}
/*
* magic - Returns the next magic number.
*/
u_int32_t
magic()
{
return (u_int32_t)random();
}
#endif
|