]> git.saurik.com Git - apple/libc.git/blob - uuid/uuidsrc/gen_uuid.c
Libc-825.24.tar.gz
[apple/libc.git] / uuid / uuidsrc / gen_uuid.c
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 #define get_random_bytes(a,b) arc4random_buf((a),(b))
76
77 /*
78 * Get the ethernet hardware address, if we can find it...
79 */
80 static int get_node_id(unsigned char *node_id)
81 {
82 #ifdef HAVE_NET_IF_H
83 int sd;
84 struct ifreq ifr, *ifrp;
85 struct ifconf ifc;
86 char buf[1024];
87 int n, i;
88 unsigned char *a;
89 #ifdef AF_LINK
90 struct sockaddr_dl *sdlp;
91 #endif
92
93 /*
94 * BSD 4.4 defines the size of an ifreq to be
95 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
96 * However, under earlier systems, sa_len isn't present, so the size is
97 * just sizeof(struct ifreq)
98 */
99 #ifdef HAVE_SA_LEN
100 #ifndef max
101 #define max(a,b) ((a) > (b) ? (a) : (b))
102 #endif
103 #define ifreq_size(i) max(sizeof(struct ifreq),\
104 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
105 #else
106 #define ifreq_size(i) sizeof(struct ifreq)
107 #endif /* HAVE_SA_LEN*/
108
109 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
110 if (sd < 0) {
111 return -1;
112 }
113 memset(buf, 0, sizeof(buf));
114 ifc.ifc_len = sizeof(buf);
115 ifc.ifc_buf = buf;
116 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
117 close(sd);
118 return -1;
119 }
120 n = ifc.ifc_len;
121 for (i = 0; i < n; i+= ifreq_size(*ifrp) ) {
122 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
123 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
124 #ifdef SIOCGIFHWADDR
125 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
126 continue;
127 a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
128 #else
129 #ifdef SIOCGENADDR
130 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
131 continue;
132 a = (unsigned char *) ifr.ifr_enaddr;
133 #else
134 #ifdef AF_LINK
135 sdlp = (struct sockaddr_dl *) &ifrp->ifr_addr;
136 if ((sdlp->sdl_family != AF_LINK) || (sdlp->sdl_alen != 6))
137 continue;
138 a = (unsigned char *) &sdlp->sdl_data[sdlp->sdl_nlen];
139 #else
140 /*
141 * XXX we don't have a way of getting the hardware
142 * address
143 */
144 close(sd);
145 return 0;
146 #endif /* AF_LINK */
147 #endif /* SIOCGENADDR */
148 #endif /* SIOCGIFHWADDR */
149 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
150 continue;
151 if (node_id) {
152 memcpy(node_id, a, 6);
153 close(sd);
154 return 1;
155 }
156 }
157 close(sd);
158 #endif
159 return 0;
160 }
161
162 /* Assume that the gettimeofday() has microsecond granularity */
163 #define MAX_ADJUSTMENT 10
164
165 static int get_clock(uint32_t *clock_high, uint32_t *clock_low, uint16_t *ret_clock_seq)
166 {
167 static int adjustment = 0;
168 static struct timeval last = {0, 0};
169 static uint16_t clock_seq;
170 struct timeval tv;
171 unsigned long long clock_reg;
172
173 try_again:
174 gettimeofday(&tv, 0);
175 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
176 get_random_bytes(&clock_seq, sizeof(clock_seq));
177 clock_seq &= 0x3FFF;
178 last = tv;
179 last.tv_sec--;
180 }
181 if ((tv.tv_sec < last.tv_sec) ||
182 ((tv.tv_sec == last.tv_sec) &&
183 (tv.tv_usec < last.tv_usec))) {
184 clock_seq = (clock_seq+1) & 0x3FFF;
185 adjustment = 0;
186 last = tv;
187 } else if ((tv.tv_sec == last.tv_sec) &&
188 (tv.tv_usec == last.tv_usec)) {
189 if (adjustment >= MAX_ADJUSTMENT)
190 goto try_again;
191 adjustment++;
192 } else {
193 adjustment = 0;
194 last = tv;
195 }
196
197 clock_reg = tv.tv_usec*10 + adjustment;
198 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
199 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
200
201 *clock_high = clock_reg >> 32;
202 *clock_low = clock_reg;
203 *ret_clock_seq = clock_seq;
204 return 0;
205 }
206
207 void uuid_generate_time(uuid_t out)
208 {
209 static unsigned char node_id[6];
210 static int has_init = 0;
211 struct uuid uu;
212 uint32_t clock_mid;
213
214 if (!has_init) {
215 if (get_node_id(node_id) <= 0) {
216 get_random_bytes(node_id, 6);
217 /*
218 * Set multicast bit, to prevent conflicts
219 * with IEEE 802 addresses obtained from
220 * network cards
221 */
222 node_id[0] |= 0x01;
223 }
224 has_init = 1;
225 }
226 get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
227 uu.clock_seq |= 0x8000;
228 uu.time_mid = (uint16_t) clock_mid;
229 uu.time_hi_and_version = ((clock_mid >> 16) & 0x0FFF) | 0x1000;
230 memcpy(uu.node, node_id, 6);
231 uuid_pack(&uu, out);
232 }
233
234 void uuid_generate_random(uuid_t out)
235 {
236 uuid_t buf;
237 struct uuid uu;
238
239 get_random_bytes(buf, sizeof(buf));
240 uuid_unpack(buf, &uu);
241
242 uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
243 uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
244 uuid_pack(&uu, out);
245 }
246
247 /*
248 * This is the generic front-end
249 */
250 void uuid_generate(uuid_t out)
251 {
252 uuid_generate_random(out);
253 }