2
* gen_uuid.c --- generate a DCE-compatible uuid
4
* Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
7
* This file may be redistributed under the terms of the GNU
8
* Library General Public License.
13
* Force inclusion of SVID stuff since we need it if we're compiling in
18
#include "autoconfig.h"
29
#include <sys/types.h>
33
#ifdef HAVE_SYS_IOCTL_H
34
#include <sys/ioctl.h>
36
#ifdef HAVE_SYS_SOCKET_H
37
#include <sys/socket.h>
39
#ifdef HAVE_SYS_SOCKIO_H
40
#include <sys/sockio.h>
45
#ifdef HAVE_NETINET_IN_H
46
#include <netinet/in.h>
52
#define srand(x) srandom(x)
53
#define rand() random()
56
static int get_random_fd(void)
64
fd = open("/dev/urandom", O_RDONLY);
66
fd = open("/dev/random", O_RDONLY);
67
//fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
68
srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
70
/* Crank the random number generator a few times */
72
for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
79
* Generate a series of random bytes. Use /dev/urandom if possible,
80
* and if not, use srandom/random.
82
static void get_random_bytes(void *buf, int nbytes)
84
int i, n = nbytes, fd = get_random_fd();
86
unsigned char *cp = (unsigned char *) buf;
92
if (lose_counter++ > 16)
103
* We do this all the time, but this is the only source of
104
* randomness if /dev/random/urandom is out to lunch.
106
for (cp = buf, i = 0; i < nbytes; i++)
107
*cp++ ^= (rand() >> 7) & 0xFF;
112
* Get the ethernet hardware address, if we can find it...
114
static int get_node_id(unsigned char *node_id)
118
struct ifreq ifr, *ifrp;
125
* BSD 4.4 defines the size of an ifreq to be
126
* max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
127
* However, under earlier systems, sa_len isn't present, so the size is
128
* just sizeof(struct ifreq)
132
#define max(a,b) ((a) > (b) ? (a) : (b))
134
#define ifreq_size(i) max(sizeof(struct ifreq),\
135
sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
137
#define ifreq_size(i) sizeof(struct ifreq)
138
#endif /* HAVE_SA_LEN*/
140
sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
144
memset(buf, 0, sizeof(buf));
145
ifc.ifc_len = sizeof(buf);
147
if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
152
for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
153
ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
154
strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
156
if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
158
a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
161
if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
163
a = (unsigned char *) ifr.ifr_enaddr;
166
* XXX we don't have a way of getting the hardware
171
#endif /* SIOCGENADDR */
172
#endif /* SIOCGIFHWADDR */
173
if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
176
memcpy(node_id, a, 6);
186
/* Assume that the gettimeofday() has microsecond granularity */
187
#define MAX_ADJUSTMENT 10
189
static int get_clock(__u32 *clock_high, __u32 *clock_low, __u16 *ret_clock_seq)
191
static int adjustment = 0;
192
static struct timeval last = {0, 0};
193
static __u16 clock_seq;
195
unsigned long long clock_reg;
198
gettimeofday(&tv, 0);
199
if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
200
get_random_bytes(&clock_seq, sizeof(clock_seq));
205
if ((tv.tv_sec < last.tv_sec) ||
206
((tv.tv_sec == last.tv_sec) &&
207
(tv.tv_usec < last.tv_usec))) {
208
clock_seq = (clock_seq+1) & 0x1FFF;
211
} else if ((tv.tv_sec == last.tv_sec) &&
212
(tv.tv_usec == last.tv_usec)) {
213
if (adjustment >= MAX_ADJUSTMENT)
221
clock_reg = tv.tv_usec*10 + adjustment;
222
clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
223
clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
225
*clock_high = clock_reg >> 32;
226
*clock_low = clock_reg;
227
*ret_clock_seq = clock_seq;
231
void uuid_generate_time(uuid_t out)
233
static unsigned char node_id[6];
234
static int has_init = 0;
239
if (get_node_id(node_id) <= 0) {
240
get_random_bytes(node_id, 6);
242
* Set multicast bit, to prevent conflicts
243
* with IEEE 802 addresses obtained from
250
get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
251
uu.clock_seq |= 0x8000;
252
uu.time_mid = (__u16) clock_mid;
253
uu.time_hi_and_version = (clock_mid >> 16) | 0x1000;
254
memcpy(uu.node, node_id, 6);
255
uuid_pack2(&uu, out);
258
void uuid_generate_random(uuid_t out)
263
get_random_bytes(buf, sizeof(buf));
264
uuid_unpack2(buf, &uu);
266
uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
267
uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
268
uuid_pack2(&uu, out);
272
* This is the generic front-end to uuid_generate_random and
273
* uuid_generate_time. It uses uuid_generate_random only if
274
* /dev/urandom is available, since otherwise we won't have
275
* high-quality randomness.
277
void uuid_generate(uuid_t out)
279
if (get_random_fd() >= 0)
280
uuid_generate_random(out);
282
uuid_generate_time(out);