blob: 158b6bd7dcffc5f146efa49ba51b1fee6fd75849 [file] [log] [blame]
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001/*
2 * gen_uuid.c --- generate a DCE-compatible uuid
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00003 *
Theodore Ts'o3030daa2000-04-07 20:06:04 +00004 * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00005 *
6 * %Begin-Header%
Theodore Ts'o3030daa2000-04-07 20:06:04 +00007 * This file may be redistributed under the terms of the GNU
8 * Library General Public License.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000010 */
11
Theodore Ts'ob1416db2001-05-01 15:32:44 +000012/*
13 * Force inclusion of SVID stuff since we need it if we're compiling in
14 * gcc-wall wall mode
15 */
16#define _SVID_SOURCE
17
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000018#ifdef HAVE_UNISTD_H
19#include <unistd.h>
20#endif
21#ifdef HAVE_STDLIB_H
22#include <stdlib.h>
23#endif
24#include <string.h>
25#include <fcntl.h>
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000026#include <errno.h>
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000027#include <sys/types.h>
28#include <sys/time.h>
29#include <sys/stat.h>
30#include <sys/file.h>
31#include <sys/ioctl.h>
32#include <sys/socket.h>
33#ifdef HAVE_SYS_SOCKIO_H
34#include <sys/sockio.h>
35#endif
36#ifdef HAVE_NET_IF_H
37#include <net/if.h>
38#endif
39#ifdef HAVE_NETINET_IN_H
40#include <netinet/in.h>
41#endif
42
43#include "uuidP.h"
44
45#ifdef HAVE_SRANDOM
46#define srand(x) srandom(x)
47#define rand() random()
48#endif
49
Theodore Ts'ob1416db2001-05-01 15:32:44 +000050static int get_random_fd(void)
Theodore Ts'o5dd7ff02000-06-12 17:35:13 +000051{
Theodore Ts'o96394d12001-01-12 18:30:54 +000052 struct timeval tv;
53 static int fd = -2;
54 int i;
Theodore Ts'o5dd7ff02000-06-12 17:35:13 +000055
56 if (fd == -2) {
Theodore Ts'o96394d12001-01-12 18:30:54 +000057 gettimeofday(&tv, 0);
Theodore Ts'o5dd7ff02000-06-12 17:35:13 +000058 fd = open("/dev/urandom", O_RDONLY);
59 if (fd == -1)
60 fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
Theodore Ts'o96394d12001-01-12 18:30:54 +000061 srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
Theodore Ts'o5dd7ff02000-06-12 17:35:13 +000062 }
63 /* Crank the random number generator a few times */
Theodore Ts'o96394d12001-01-12 18:30:54 +000064 gettimeofday(&tv, 0);
65 for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
Theodore Ts'o5dd7ff02000-06-12 17:35:13 +000066 rand();
67 return fd;
68}
69
70
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000071/*
72 * Generate a series of random bytes. Use /dev/urandom if possible,
73 * and if not, use srandom/random.
74 */
75static void get_random_bytes(void *buf, int nbytes)
76{
Theodore Ts'oedab2942003-04-03 08:25:15 -050077 int i, n = nbytes, fd = get_random_fd();
Theodore Ts'oe589f672000-04-03 13:45:40 +000078 int lose_counter = 0;
Theodore Ts'oedab2942003-04-03 08:25:15 -050079 unsigned char *cp = (unsigned char *) buf;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000080
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000081 if (fd >= 0) {
Theodore Ts'oedab2942003-04-03 08:25:15 -050082 while (n > 0) {
83 i = read(fd, cp, n);
Theodore Ts'oe589f672000-04-03 13:45:40 +000084 if (i <= 0) {
Theodore Ts'o61bee882002-10-31 15:17:16 -050085 if (lose_counter++ > 16)
Theodore Ts'oe589f672000-04-03 13:45:40 +000086 break;
87 continue;
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000088 }
Theodore Ts'oedab2942003-04-03 08:25:15 -050089 n -= i;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000090 cp += i;
Theodore Ts'oe589f672000-04-03 13:45:40 +000091 lose_counter = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000092 }
93 }
Theodore Ts'oedab2942003-04-03 08:25:15 -050094
95 /*
96 * We do this all the time, but this is the only source of
97 * randomness if /dev/random/urandom is out to lunch.
98 */
99 for (cp = buf, i = 0; i < nbytes; i++)
100 *cp++ ^= (rand() >> 7) & 0xFF;
Theodore Ts'ob19d1a91999-06-18 00:32:03 +0000101 return;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000102}
103
104/*
105 * Get the ethernet hardware address, if we can find it...
106 */
107static int get_node_id(unsigned char *node_id)
108{
109#ifdef HAVE_NET_IF_H
110 int sd;
111 struct ifreq ifr, *ifrp;
112 struct ifconf ifc;
113 char buf[1024];
114 int n, i;
115 unsigned char *a;
116
117/*
118 * BSD 4.4 defines the size of an ifreq to be
119 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
120 * However, under earlier systems, sa_len isn't present, so the size is
121 * just sizeof(struct ifreq)
122 */
123#ifdef HAVE_SA_LEN
124#ifndef max
125#define max(a,b) ((a) > (b) ? (a) : (b))
126#endif
127#define ifreq_size(i) max(sizeof(struct ifreq),\
128 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
129#else
130#define ifreq_size(i) sizeof(struct ifreq)
131#endif /* HAVE_SA_LEN*/
132
133 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
134 if (sd < 0) {
135 return -1;
136 }
137 memset(buf, 0, sizeof(buf));
138 ifc.ifc_len = sizeof(buf);
139 ifc.ifc_buf = buf;
140 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
141 close(sd);
142 return -1;
143 }
144 n = ifc.ifc_len;
145 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
Theodore Ts'odc3710e1998-03-09 03:34:48 +0000146 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000147 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
148#ifdef SIOCGIFHWADDR
149 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
150 continue;
151 a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
152#else
153#ifdef SIOCGENADDR
154 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
155 continue;
156 a = (unsigned char *) ifr.ifr_enaddr;
157#else
158 /*
159 * XXX we don't have a way of getting the hardware
160 * address
161 */
162 close(sd);
163 return 0;
164#endif /* SIOCGENADDR */
165#endif /* SIOCGIFHWADDR */
166 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
167 continue;
168 if (node_id) {
169 memcpy(node_id, a, 6);
170 close(sd);
171 return 1;
172 }
173 }
174 close(sd);
175#endif
176 return 0;
177}
178
179/* Assume that the gettimeofday() has microsecond granularity */
180#define MAX_ADJUSTMENT 10
181
182static int get_clock(__u32 *clock_high, __u32 *clock_low, __u16 *ret_clock_seq)
183{
184 static int adjustment = 0;
185 static struct timeval last = {0, 0};
186 static __u16 clock_seq;
187 struct timeval tv;
Theodore Ts'od5464471997-10-15 01:52:44 +0000188 unsigned long long clock_reg;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000189
190try_again:
191 gettimeofday(&tv, 0);
192 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
193 get_random_bytes(&clock_seq, sizeof(clock_seq));
194 clock_seq &= 0x1FFF;
195 last = tv;
196 last.tv_sec--;
197 }
198 if ((tv.tv_sec < last.tv_sec) ||
199 ((tv.tv_sec == last.tv_sec) &&
200 (tv.tv_usec < last.tv_usec))) {
201 clock_seq = (clock_seq+1) & 0x1FFF;
202 adjustment = 0;
Theodore Ts'ofa7cc282000-04-03 16:51:06 +0000203 last = tv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000204 } else if ((tv.tv_sec == last.tv_sec) &&
205 (tv.tv_usec == last.tv_usec)) {
206 if (adjustment >= MAX_ADJUSTMENT)
207 goto try_again;
208 adjustment++;
Theodore Ts'ofa7cc282000-04-03 16:51:06 +0000209 } else {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000210 adjustment = 0;
Theodore Ts'ofa7cc282000-04-03 16:51:06 +0000211 last = tv;
212 }
213
Theodore Ts'od5464471997-10-15 01:52:44 +0000214 clock_reg = tv.tv_usec*10 + adjustment;
215 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
216 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000217
Theodore Ts'od5464471997-10-15 01:52:44 +0000218 *clock_high = clock_reg >> 32;
219 *clock_low = clock_reg;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000220 *ret_clock_seq = clock_seq;
221 return 0;
222}
223
Theodore Ts'ob19d1a91999-06-18 00:32:03 +0000224void uuid_generate_time(uuid_t out)
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000225{
226 static unsigned char node_id[6];
227 static int has_init = 0;
228 struct uuid uu;
229 __u32 clock_mid;
230
231 if (!has_init) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000232 if (get_node_id(node_id) <= 0) {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000233 get_random_bytes(node_id, 6);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000234 /*
235 * Set multicast bit, to prevent conflicts
236 * with IEEE 802 addresses obtained from
237 * network cards
238 */
239 node_id[0] |= 0x80;
240 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000241 has_init = 1;
242 }
243 get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
244 uu.clock_seq |= 0x8000;
245 uu.time_mid = (__u16) clock_mid;
246 uu.time_hi_and_version = (clock_mid >> 16) | 0x1000;
247 memcpy(uu.node, node_id, 6);
248 uuid_pack(&uu, out);
249}
Theodore Ts'ob19d1a91999-06-18 00:32:03 +0000250
251void uuid_generate_random(uuid_t out)
252{
253 uuid_t buf;
254 struct uuid uu;
255
256 get_random_bytes(buf, sizeof(buf));
257 uuid_unpack(buf, &uu);
258
259 uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
260 uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
261 uuid_pack(&uu, out);
262}
263
264/*
265 * This is the generic front-end to uuid_generate_random and
266 * uuid_generate_time. It uses uuid_generate_random only if
267 * /dev/urandom is available, since otherwise we won't have
268 * high-quality randomness.
269 */
270void uuid_generate(uuid_t out)
271{
Theodore Ts'o5dd7ff02000-06-12 17:35:13 +0000272 if (get_random_fd() >= 0)
Theodore Ts'ob19d1a91999-06-18 00:32:03 +0000273 uuid_generate_random(out);
274 else
275 uuid_generate_time(out);
276}