]>
git.saurik.com Git - apple/libc.git/blob - uuid/uuidsrc/gen_uuid.c
2 * gen_uuid.c --- generate a DCE-compatible uuid
4 * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
36 * Force inclusion of SVID stuff since we need it if we're compiling in
50 #include <sys/types.h>
54 #ifdef HAVE_SYS_IOCTL_H
55 #include <sys/ioctl.h>
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
60 #ifdef HAVE_SYS_SOCKIO_H
61 #include <sys/sockio.h>
66 #ifdef HAVE_NETINET_IN_H
67 #include <netinet/in.h>
69 #ifdef HAVE_NET_IF_DL_H
70 #include <net/if_dl.h>
76 #define srand(x) srandom(x)
77 #define rand() random()
80 static int get_random_fd(void)
88 fd
= open("/dev/urandom", O_RDONLY
);
90 fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
);
91 srand((getpid() << 16) ^ getuid() ^ tv
.tv_sec
^ tv
.tv_usec
);
93 /* Crank the random number generator a few times */
95 for (i
= (tv
.tv_sec
^ tv
.tv_usec
) & 0x1F; i
> 0; i
--)
102 * Generate a series of random bytes. Use /dev/urandom if possible,
103 * and if not, use srandom/random.
105 static void get_random_bytes(void *buf
, int nbytes
)
107 int i
, n
= nbytes
, fd
= get_random_fd();
108 int lose_counter
= 0;
109 unsigned char *cp
= (unsigned char *) buf
;
115 if (lose_counter
++ > 16)
126 * We do this all the time, but this is the only source of
127 * randomness if /dev/random/urandom is out to lunch.
129 for (cp
= buf
, i
= 0; i
< nbytes
; i
++)
130 *cp
++ ^= (rand() >> 7) & 0xFF;
135 * Get the ethernet hardware address, if we can find it...
137 static int get_node_id(unsigned char *node_id
)
141 struct ifreq ifr
, *ifrp
;
147 struct sockaddr_dl
*sdlp
;
151 * BSD 4.4 defines the size of an ifreq to be
152 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
153 * However, under earlier systems, sa_len isn't present, so the size is
154 * just sizeof(struct ifreq)
158 #define max(a,b) ((a) > (b) ? (a) : (b))
160 #define ifreq_size(i) max(sizeof(struct ifreq),\
161 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
163 #define ifreq_size(i) sizeof(struct ifreq)
164 #endif /* HAVE_SA_LEN*/
166 sd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
170 memset(buf
, 0, sizeof(buf
));
171 ifc
.ifc_len
= sizeof(buf
);
173 if (ioctl (sd
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
178 for (i
= 0; i
< n
; i
+= ifreq_size(*ifrp
) ) {
179 ifrp
= (struct ifreq
*)((char *) ifc
.ifc_buf
+i
);
180 strncpy(ifr
.ifr_name
, ifrp
->ifr_name
, IFNAMSIZ
);
182 if (ioctl(sd
, SIOCGIFHWADDR
, &ifr
) < 0)
184 a
= (unsigned char *) &ifr
.ifr_hwaddr
.sa_data
;
187 if (ioctl(sd
, SIOCGENADDR
, &ifr
) < 0)
189 a
= (unsigned char *) ifr
.ifr_enaddr
;
192 sdlp
= (struct sockaddr_dl
*) &ifrp
->ifr_addr
;
193 if ((sdlp
->sdl_family
!= AF_LINK
) || (sdlp
->sdl_alen
!= 6))
195 a
= (unsigned char *) &sdlp
->sdl_data
[sdlp
->sdl_nlen
];
198 * XXX we don't have a way of getting the hardware
204 #endif /* SIOCGENADDR */
205 #endif /* SIOCGIFHWADDR */
206 if (!a
[0] && !a
[1] && !a
[2] && !a
[3] && !a
[4] && !a
[5])
209 memcpy(node_id
, a
, 6);
219 /* Assume that the gettimeofday() has microsecond granularity */
220 #define MAX_ADJUSTMENT 10
222 static int get_clock(uint32_t *clock_high
, uint32_t *clock_low
, uint16_t *ret_clock_seq
)
224 static int adjustment
= 0;
225 static struct timeval last
= {0, 0};
226 static uint16_t clock_seq
;
228 unsigned long long clock_reg
;
231 gettimeofday(&tv
, 0);
232 if ((last
.tv_sec
== 0) && (last
.tv_usec
== 0)) {
233 get_random_bytes(&clock_seq
, sizeof(clock_seq
));
238 if ((tv
.tv_sec
< last
.tv_sec
) ||
239 ((tv
.tv_sec
== last
.tv_sec
) &&
240 (tv
.tv_usec
< last
.tv_usec
))) {
241 clock_seq
= (clock_seq
+1) & 0x3FFF;
244 } else if ((tv
.tv_sec
== last
.tv_sec
) &&
245 (tv
.tv_usec
== last
.tv_usec
)) {
246 if (adjustment
>= MAX_ADJUSTMENT
)
254 clock_reg
= tv
.tv_usec
*10 + adjustment
;
255 clock_reg
+= ((unsigned long long) tv
.tv_sec
)*10000000;
256 clock_reg
+= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
258 *clock_high
= clock_reg
>> 32;
259 *clock_low
= clock_reg
;
260 *ret_clock_seq
= clock_seq
;
264 void uuid_generate_time(uuid_t out
)
266 static unsigned char node_id
[6];
267 static int has_init
= 0;
272 if (get_node_id(node_id
) <= 0) {
273 get_random_bytes(node_id
, 6);
275 * Set multicast bit, to prevent conflicts
276 * with IEEE 802 addresses obtained from
283 get_clock(&clock_mid
, &uu
.time_low
, &uu
.clock_seq
);
284 uu
.clock_seq
|= 0x8000;
285 uu
.time_mid
= (uint16_t) clock_mid
;
286 uu
.time_hi_and_version
= ((clock_mid
>> 16) & 0x0FFF) | 0x1000;
287 memcpy(uu
.node
, node_id
, 6);
291 void uuid_generate_random(uuid_t out
)
296 get_random_bytes(buf
, sizeof(buf
));
297 uuid_unpack(buf
, &uu
);
299 uu
.clock_seq
= (uu
.clock_seq
& 0x3FFF) | 0x8000;
300 uu
.time_hi_and_version
= (uu
.time_hi_and_version
& 0x0FFF) | 0x4000;
305 * This is the generic front-end to uuid_generate_random and
306 * uuid_generate_time. It uses uuid_generate_random only if
307 * /dev/urandom is available, since otherwise we won't have
308 * high-quality randomness.
310 void uuid_generate(uuid_t out
)
312 if (get_random_fd() >= 0)
313 uuid_generate_random(out
);
315 uuid_generate_time(out
);