]> git.saurik.com Git - apple/libc.git/blame_incremental - uuid/gen_uuid-uuid.c
Libc-763.13.tar.gz
[apple/libc.git] / uuid / gen_uuid-uuid.c
... / ...
CommitLineData
1/*
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
35/*
36 * Force inclusion of SVID stuff since we need it if we're compiling in
37 * gcc-wall wall mode
38 */
39#define _SVID_SOURCE
40
41#ifdef HAVE_UNISTD_H
42#include <unistd.h>
43#endif
44#ifdef HAVE_STDLIB_H
45#include <stdlib.h>
46#endif
47#include <string.h>
48#include <fcntl.h>
49#include <errno.h>
50#include <sys/types.h>
51#include <sys/time.h>
52#include <sys/stat.h>
53#include <sys/file.h>
54#ifdef HAVE_SYS_IOCTL_H
55#include <sys/ioctl.h>
56#endif
57#ifdef HAVE_SYS_SOCKET_H
58#include <sys/socket.h>
59#endif
60#ifdef HAVE_SYS_SOCKIO_H
61#include <sys/sockio.h>
62#endif
63#ifdef HAVE_NET_IF_H
64#include <net/if.h>
65#endif
66#ifdef HAVE_NETINET_IN_H
67#include <netinet/in.h>
68#endif
69#ifdef HAVE_NET_IF_DL_H
70#include <net/if_dl.h>
71#endif
72
73#include "uuidP.h"
74
75/*
76 * Generate a series of random bytes, using arc4random
77 */
78static void get_random_bytes(void *buf, int nbytes)
79{
80 unsigned char *cp = (unsigned char *) buf;
81 u_int32_t u;
82 int n = nbytes / sizeof(u);
83
84 while (n-- > 0) {
85 u = arc4random();
86 memcpy(cp, &u, sizeof(u));
87 cp += sizeof(u);
88 }
89 if ((n = nbytes % sizeof(u)) > 0) {
90 u = arc4random();
91 memcpy(cp, &u, n);
92 }
93 return;
94}
95
96/*
97 * Get the ethernet hardware address, if we can find it...
98 */
99static int get_node_id(unsigned char *node_id)
100{
101#ifdef HAVE_NET_IF_H
102 int sd;
103 struct ifreq ifr, *ifrp;
104 struct ifconf ifc;
105 char buf[1024];
106 int n, i;
107 unsigned char *a;
108#ifdef AF_LINK
109 struct sockaddr_dl *sdlp;
110#endif
111
112/*
113 * BSD 4.4 defines the size of an ifreq to be
114 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
115 * However, under earlier systems, sa_len isn't present, so the size is
116 * just sizeof(struct ifreq)
117 */
118#ifdef HAVE_SA_LEN
119#ifndef max
120#define max(a,b) ((a) > (b) ? (a) : (b))
121#endif
122#define ifreq_size(i) max(sizeof(struct ifreq),\
123 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
124#else
125#define ifreq_size(i) sizeof(struct ifreq)
126#endif /* HAVE_SA_LEN*/
127
128 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
129 if (sd < 0) {
130 return -1;
131 }
132 memset(buf, 0, sizeof(buf));
133 ifc.ifc_len = sizeof(buf);
134 ifc.ifc_buf = buf;
135 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
136 close(sd);
137 return -1;
138 }
139 n = ifc.ifc_len;
140 for (i = 0; i < n; i+= ifreq_size(*ifrp) ) {
141 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
142 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
143#ifdef SIOCGIFHWADDR
144 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
145 continue;
146 a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
147#else
148#ifdef SIOCGENADDR
149 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
150 continue;
151 a = (unsigned char *) ifr.ifr_enaddr;
152#else
153#ifdef AF_LINK
154 sdlp = (struct sockaddr_dl *) &ifrp->ifr_addr;
155 if ((sdlp->sdl_family != AF_LINK) || (sdlp->sdl_alen != 6))
156 continue;
157 a = (unsigned char *) &sdlp->sdl_data[sdlp->sdl_nlen];
158#else
159 /*
160 * XXX we don't have a way of getting the hardware
161 * address
162 */
163 close(sd);
164 return 0;
165#endif /* AF_LINK */
166#endif /* SIOCGENADDR */
167#endif /* SIOCGIFHWADDR */
168 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
169 continue;
170 if (node_id) {
171 memcpy(node_id, a, 6);
172 close(sd);
173 return 1;
174 }
175 }
176 close(sd);
177#endif
178 return 0;
179}
180
181/* Assume that the gettimeofday() has microsecond granularity */
182#define MAX_ADJUSTMENT 10
183
184static int get_clock(uint32_t *clock_high, uint32_t *clock_low, uint16_t *ret_clock_seq)
185{
186 static int adjustment = 0;
187 static struct timeval last = {0, 0};
188 static uint16_t clock_seq;
189 struct timeval tv;
190 unsigned long long clock_reg;
191
192try_again:
193 gettimeofday(&tv, 0);
194 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
195 get_random_bytes(&clock_seq, sizeof(clock_seq));
196 clock_seq &= 0x3FFF;
197 last = tv;
198 last.tv_sec--;
199 }
200 if ((tv.tv_sec < last.tv_sec) ||
201 ((tv.tv_sec == last.tv_sec) &&
202 (tv.tv_usec < last.tv_usec))) {
203 clock_seq = (clock_seq+1) & 0x3FFF;
204 adjustment = 0;
205 last = tv;
206 } else if ((tv.tv_sec == last.tv_sec) &&
207 (tv.tv_usec == last.tv_usec)) {
208 if (adjustment >= MAX_ADJUSTMENT)
209 goto try_again;
210 adjustment++;
211 } else {
212 adjustment = 0;
213 last = tv;
214 }
215
216 clock_reg = tv.tv_usec*10 + adjustment;
217 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
218 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
219
220 *clock_high = clock_reg >> 32;
221 *clock_low = clock_reg;
222 *ret_clock_seq = clock_seq;
223 return 0;
224}
225
226void uuid_generate_time(uuid_t out)
227{
228 static unsigned char node_id[6];
229 static int has_init = 0;
230 struct uuid uu;
231 uint32_t clock_mid;
232
233 if (!has_init) {
234 if (get_node_id(node_id) <= 0) {
235 get_random_bytes(node_id, 6);
236 /*
237 * Set multicast bit, to prevent conflicts
238 * with IEEE 802 addresses obtained from
239 * network cards
240 */
241 node_id[0] |= 0x01;
242 }
243 has_init = 1;
244 }
245 get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
246 uu.clock_seq |= 0x8000;
247 uu.time_mid = (uint16_t) clock_mid;
248 uu.time_hi_and_version = ((clock_mid >> 16) & 0x0FFF) | 0x1000;
249 memcpy(uu.node, node_id, 6);
250 uuid_pack(&uu, out);
251}
252
253void uuid_generate_random(uuid_t out)
254{
255 uuid_t buf;
256 struct uuid uu;
257
258 get_random_bytes(buf, sizeof(buf));
259 uuid_unpack(buf, &uu);
260
261 uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
262 uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
263 uuid_pack(&uu, out);
264}
265
266/*
267 * This is the generic front-end
268 */
269void uuid_generate(uuid_t out)
270{
271 uuid_generate_random(out);
272}