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