blob: 328a58fd6ea87001fe772f1247fbe78254697fd7 [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 *
4 * Copyright (C) 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000010 */
11
12#ifdef HAVE_UNISTD_H
13#include <unistd.h>
14#endif
15#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
18#include <string.h>
19#include <fcntl.h>
20#include <sys/types.h>
21#include <sys/time.h>
22#include <sys/stat.h>
23#include <sys/file.h>
24#include <sys/ioctl.h>
25#include <sys/socket.h>
26#ifdef HAVE_SYS_SOCKIO_H
27#include <sys/sockio.h>
28#endif
29#ifdef HAVE_NET_IF_H
30#include <net/if.h>
31#endif
32#ifdef HAVE_NETINET_IN_H
33#include <netinet/in.h>
34#endif
35
36#include "uuidP.h"
37
38#ifdef HAVE_SRANDOM
39#define srand(x) srandom(x)
40#define rand() random()
41#endif
42
43/*
44 * Generate a series of random bytes. Use /dev/urandom if possible,
45 * and if not, use srandom/random.
46 */
47static void get_random_bytes(void *buf, int nbytes)
48{
49 static int fd = -2;
50 int i;
51 char *cp = (char *) buf;
52
53 if (fd == -2) {
54 fd = open("/dev/urandom", O_RDONLY);
55 srand((getpid() << 16) ^ getuid() ^ time(0));
56 }
57 if (fd > 0) {
58 i = read(fd, cp, nbytes);
59 if (i == nbytes)
60 return;
61 if (i > 0) {
62 nbytes -= i;
63 cp += i;
64 }
65 }
66 for (i=0; i < nbytes; i++)
67 *cp++ = rand() & 0xFF;
68}
69
70/*
71 * Get the ethernet hardware address, if we can find it...
72 */
73static int get_node_id(unsigned char *node_id)
74{
75#ifdef HAVE_NET_IF_H
76 int sd;
77 struct ifreq ifr, *ifrp;
78 struct ifconf ifc;
79 char buf[1024];
80 int n, i;
81 unsigned char *a;
82
83/*
84 * BSD 4.4 defines the size of an ifreq to be
85 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
86 * However, under earlier systems, sa_len isn't present, so the size is
87 * just sizeof(struct ifreq)
88 */
89#ifdef HAVE_SA_LEN
90#ifndef max
91#define max(a,b) ((a) > (b) ? (a) : (b))
92#endif
93#define ifreq_size(i) max(sizeof(struct ifreq),\
94 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
95#else
96#define ifreq_size(i) sizeof(struct ifreq)
97#endif /* HAVE_SA_LEN*/
98
99 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
100 if (sd < 0) {
101 return -1;
102 }
103 memset(buf, 0, sizeof(buf));
104 ifc.ifc_len = sizeof(buf);
105 ifc.ifc_buf = buf;
106 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
107 close(sd);
108 return -1;
109 }
110 n = ifc.ifc_len;
111 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
Theodore Ts'odc3710e1998-03-09 03:34:48 +0000112 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000113 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
114#ifdef SIOCGIFHWADDR
115 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
116 continue;
117 a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
118#else
119#ifdef SIOCGENADDR
120 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
121 continue;
122 a = (unsigned char *) ifr.ifr_enaddr;
123#else
124 /*
125 * XXX we don't have a way of getting the hardware
126 * address
127 */
128 close(sd);
129 return 0;
130#endif /* SIOCGENADDR */
131#endif /* SIOCGIFHWADDR */
132 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
133 continue;
134 if (node_id) {
135 memcpy(node_id, a, 6);
136 close(sd);
137 return 1;
138 }
139 }
140 close(sd);
141#endif
142 return 0;
143}
144
145/* Assume that the gettimeofday() has microsecond granularity */
146#define MAX_ADJUSTMENT 10
147
148static int get_clock(__u32 *clock_high, __u32 *clock_low, __u16 *ret_clock_seq)
149{
150 static int adjustment = 0;
151 static struct timeval last = {0, 0};
152 static __u16 clock_seq;
153 struct timeval tv;
Theodore Ts'od5464471997-10-15 01:52:44 +0000154 unsigned long long clock_reg;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000155
156try_again:
157 gettimeofday(&tv, 0);
158 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
159 get_random_bytes(&clock_seq, sizeof(clock_seq));
160 clock_seq &= 0x1FFF;
161 last = tv;
162 last.tv_sec--;
163 }
164 if ((tv.tv_sec < last.tv_sec) ||
165 ((tv.tv_sec == last.tv_sec) &&
166 (tv.tv_usec < last.tv_usec))) {
167 clock_seq = (clock_seq+1) & 0x1FFF;
168 adjustment = 0;
169 } else if ((tv.tv_sec == last.tv_sec) &&
170 (tv.tv_usec == last.tv_usec)) {
171 if (adjustment >= MAX_ADJUSTMENT)
172 goto try_again;
173 adjustment++;
174 } else
175 adjustment = 0;
176
Theodore Ts'od5464471997-10-15 01:52:44 +0000177 clock_reg = tv.tv_usec*10 + adjustment;
178 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
179 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000180
Theodore Ts'od5464471997-10-15 01:52:44 +0000181 *clock_high = clock_reg >> 32;
182 *clock_low = clock_reg;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000183 *ret_clock_seq = clock_seq;
184 return 0;
185}
186
187void uuid_generate(uuid_t out)
188{
189 static unsigned char node_id[6];
190 static int has_init = 0;
191 struct uuid uu;
192 __u32 clock_mid;
193
194 if (!has_init) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000195 if (get_node_id(node_id) <= 0) {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000196 get_random_bytes(node_id, 6);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000197 /*
198 * Set multicast bit, to prevent conflicts
199 * with IEEE 802 addresses obtained from
200 * network cards
201 */
202 node_id[0] |= 0x80;
203 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000204 has_init = 1;
205 }
206 get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
207 uu.clock_seq |= 0x8000;
208 uu.time_mid = (__u16) clock_mid;
209 uu.time_hi_and_version = (clock_mid >> 16) | 0x1000;
210 memcpy(uu.node, node_id, 6);
211 uuid_pack(&uu, out);
212}