blob: 52328cfc42535d3ff4ef815f9135333900c6c484 [file] [log] [blame]
Mike Frysinger38a33f92005-05-09 22:13:22 +00001/*
2 * gen_uuid.c --- generate a DCE-compatible uuid
3 *
4 * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
Mike Frysinger38a33f92005-05-09 22:13:22 +000035#include <unistd.h>
Mike Frysinger38a33f92005-05-09 22:13:22 +000036#include <stdlib.h>
Mike Frysinger38a33f92005-05-09 22:13:22 +000037#include <string.h>
38#include <fcntl.h>
39#include <errno.h>
40#include <sys/types.h>
41#include <sys/time.h>
42#include <sys/stat.h>
43#include <sys/file.h>
44#ifdef HAVE_SYS_IOCTL_H
45#include <sys/ioctl.h>
46#endif
47#ifdef HAVE_SYS_SOCKET_H
48#include <sys/socket.h>
49#endif
50#ifdef HAVE_SYS_SOCKIO_H
51#include <sys/sockio.h>
52#endif
53#ifdef HAVE_NET_IF_H
54#include <net/if.h>
55#endif
56#ifdef HAVE_NETINET_IN_H
57#include <netinet/in.h>
58#endif
59#ifdef HAVE_NET_IF_DL_H
60#include <net/if_dl.h>
61#endif
62
63#include "uuidP.h"
64
65#ifdef HAVE_SRANDOM
66#define srand(x) srandom(x)
67#define rand() random()
68#endif
69
70static int get_random_fd(void)
71{
72 struct timeval tv;
73 static int fd = -2;
74 int i;
75
76 if (fd == -2) {
77 gettimeofday(&tv, 0);
78 fd = open("/dev/urandom", O_RDONLY);
79 if (fd == -1)
80 fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
81 srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
82 }
83 /* Crank the random number generator a few times */
84 gettimeofday(&tv, 0);
85 for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
86 rand();
87 return fd;
88}
89
90
91/*
92 * Generate a series of random bytes. Use /dev/urandom if possible,
93 * and if not, use srandom/random.
94 */
95static void get_random_bytes(void *buf, int nbytes)
96{
97 int i, n = nbytes, fd = get_random_fd();
98 int lose_counter = 0;
99 unsigned char *cp = (unsigned char *) buf;
100
101 if (fd >= 0) {
102 while (n > 0) {
103 i = read(fd, cp, n);
104 if (i <= 0) {
105 if (lose_counter++ > 16)
106 break;
107 continue;
108 }
109 n -= i;
110 cp += i;
111 lose_counter = 0;
112 }
113 }
114
115 /*
116 * We do this all the time, but this is the only source of
117 * randomness if /dev/random/urandom is out to lunch.
118 */
119 for (cp = buf, i = 0; i < nbytes; i++)
120 *cp++ ^= (rand() >> 7) & 0xFF;
121 return;
122}
123
124/*
125 * Get the ethernet hardware address, if we can find it...
126 */
127static int get_node_id(unsigned char *node_id)
128{
129#ifdef HAVE_NET_IF_H
130 int sd;
131 struct ifreq ifr, *ifrp;
132 struct ifconf ifc;
133 char buf[1024];
134 int n, i;
135 unsigned char *a;
136#ifdef HAVE_NET_IF_DL_H
137 struct sockaddr_dl *sdlp;
138#endif
139
140/*
141 * BSD 4.4 defines the size of an ifreq to be
142 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
143 * However, under earlier systems, sa_len isn't present, so the size is
144 * just sizeof(struct ifreq)
145 */
146#ifdef HAVE_SA_LEN
147#ifndef max
148#define max(a,b) ((a) > (b) ? (a) : (b))
149#endif
150#define ifreq_size(i) max(sizeof(struct ifreq),\
151 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
152#else
153#define ifreq_size(i) sizeof(struct ifreq)
154#endif /* HAVE_SA_LEN*/
155
156 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
157 if (sd < 0) {
158 return -1;
159 }
160 memset(buf, 0, sizeof(buf));
161 ifc.ifc_len = sizeof(buf);
162 ifc.ifc_buf = buf;
163 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
164 close(sd);
165 return -1;
166 }
167 n = ifc.ifc_len;
168 for (i = 0; i < n; i+= ifreq_size(*ifrp) ) {
169 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
170 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
171#ifdef SIOCGIFHWADDR
172 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
173 continue;
174 a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
175#else
176#ifdef SIOCGENADDR
177 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
178 continue;
179 a = (unsigned char *) ifr.ifr_enaddr;
180#else
181#ifdef HAVE_NET_IF_DL_H
182 sdlp = (struct sockaddr_dl *) &ifrp->ifr_addr;
183 if ((sdlp->sdl_family != AF_LINK) || (sdlp->sdl_alen != 6))
184 continue;
185 a = (unsigned char *) &sdlp->sdl_data[sdlp->sdl_nlen];
186#else
187 /*
188 * XXX we don't have a way of getting the hardware
189 * address
190 */
191 close(sd);
192 return 0;
193#endif /* HAVE_NET_IF_DL_H */
194#endif /* SIOCGENADDR */
195#endif /* SIOCGIFHWADDR */
196 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
197 continue;
198 if (node_id) {
199 memcpy(node_id, a, 6);
200 close(sd);
201 return 1;
202 }
203 }
204 close(sd);
205#endif
206 return 0;
207}
208
209/* Assume that the gettimeofday() has microsecond granularity */
210#define MAX_ADJUSTMENT 10
211
212static int get_clock(uint32_t *clock_high, uint32_t *clock_low, uint16_t *ret_clock_seq)
213{
214 static int adjustment = 0;
215 static struct timeval last = {0, 0};
216 static uint16_t clock_seq;
217 struct timeval tv;
218 unsigned long long clock_reg;
219
220try_again:
221 gettimeofday(&tv, 0);
222 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
223 get_random_bytes(&clock_seq, sizeof(clock_seq));
224 clock_seq &= 0x3FFF;
225 last = tv;
226 last.tv_sec--;
227 }
228 if ((tv.tv_sec < last.tv_sec) ||
229 ((tv.tv_sec == last.tv_sec) &&
230 (tv.tv_usec < last.tv_usec))) {
231 clock_seq = (clock_seq+1) & 0x3FFF;
232 adjustment = 0;
233 last = tv;
234 } else if ((tv.tv_sec == last.tv_sec) &&
235 (tv.tv_usec == last.tv_usec)) {
236 if (adjustment >= MAX_ADJUSTMENT)
237 goto try_again;
238 adjustment++;
239 } else {
240 adjustment = 0;
241 last = tv;
242 }
243
244 clock_reg = tv.tv_usec*10 + adjustment;
245 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
246 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
247
248 *clock_high = clock_reg >> 32;
249 *clock_low = clock_reg;
250 *ret_clock_seq = clock_seq;
251 return 0;
252}
253
254void uuid_generate_time(uuid_t out)
255{
256 static unsigned char node_id[6];
257 static int has_init = 0;
258 struct uuid uu;
259 uint32_t clock_mid;
260
261 if (!has_init) {
262 if (get_node_id(node_id) <= 0) {
263 get_random_bytes(node_id, 6);
264 /*
265 * Set multicast bit, to prevent conflicts
266 * with IEEE 802 addresses obtained from
267 * network cards
268 */
269 node_id[0] |= 0x01;
270 }
271 has_init = 1;
272 }
273 get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
274 uu.clock_seq |= 0x8000;
275 uu.time_mid = (uint16_t) clock_mid;
276 uu.time_hi_and_version = ((clock_mid >> 16) & 0x0FFF) | 0x1000;
277 memcpy(uu.node, node_id, 6);
278 uuid_pack(&uu, out);
279}
280
281void uuid_generate_random(uuid_t out)
282{
283 uuid_t buf;
284 struct uuid uu;
285
286 get_random_bytes(buf, sizeof(buf));
287 uuid_unpack(buf, &uu);
288
289 uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
290 uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
291 uuid_pack(&uu, out);
292}
293
294/*
295 * This is the generic front-end to uuid_generate_random and
296 * uuid_generate_time. It uses uuid_generate_random only if
297 * /dev/urandom is available, since otherwise we won't have
298 * high-quality randomness.
299 */
300void uuid_generate(uuid_t out)
301{
302 if (get_random_fd() >= 0)
303 uuid_generate_random(out);
304 else
305 uuid_generate_time(out);
306}