Debian Codegraph @

libuuid/gen_uuid.c

raw symbols SCIP
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 #ifndef _SVID_SOURCE
40 # define _SVID_SOURCE
41 #endif
42
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46 #ifdef HAVE_STDLIB_H
47 #include <stdlib.h>
48 #endif
49 #include <string.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 #include <sys/types.h>
53 #include <sys/time.h>
54 #include <sys/stat.h>
55 #include <sys/file.h>
56 #ifdef HAVE_SYS_IOCTL_H
57 #include <sys/ioctl.h>
58 #endif
59 #ifdef HAVE_SYS_SOCKET_H
60 #include <sys/socket.h>
61 #endif
62 #ifdef HAVE_SYS_SOCKIO_H
63 #include <sys/sockio.h>
64 #endif
65 #ifdef HAVE_NET_IF_H
66 #include <net/if.h>
67 #endif
68 #ifdef HAVE_NETINET_IN_H
69 #include <netinet/in.h>
70 #endif
71 #ifdef HAVE_NET_IF_DL_H
72 #include <net/if_dl.h>
73 #endif
74
75 #include "psm.h"
76
77 #include "uuidP.h"
78
79 #ifdef HAVE_SRANDOM
80 #define srand(x) srandom(x)
81 #define rand() random()
82 #endif
83
84 static int get_random_fd(void)
85 {
86 struct timeval tv;
87 static int fd = -2;
88 int i;
89
90 if (fd == -2) {
91 gettimeofday(&tv, 0);
92 fd = open("/dev/urandom", O_RDONLY);
93 if (fd == -1)
94 fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
95 srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
96 }
97 /* Crank the random number generator a few times */
98 gettimeofday(&tv, 0);
99 for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
100 rand();
101 return fd;
102 }
103
104 /*
105 * Generate a series of random bytes. Use /dev/urandom if possible,
106 * and if not, use srandom/random.
107 */
108 static void get_random_bytes(void *buf, int nbytes)
109 {
110 int i, n = nbytes, fd = get_random_fd();
111 int lose_counter = 0;
112 unsigned char *cp = (unsigned char *) buf;
113
114 if (fd >= 0) {
115 while (n > 0) {
116 i = read(fd, cp, n);
117 if (i <= 0) {
118 if (lose_counter++ > 16)
119 break;
120 continue;
121 }
122 n -= i;
123 cp += i;
124 lose_counter = 0;
125 }
126 }
127
128 /*
129 * We do this all the time, but this is the only source of
130 * randomness if /dev/random/urandom is out to lunch.
131 */
132 for (cp = buf, i = 0; i < nbytes; i++)
133 *cp++ ^= (rand() >> 7) & 0xFF;
134 return;
135 }
136
137 /*
138 * Get the ethernet hardware address, if we can find it...
139 */
140 static int get_node_id(unsigned char *node_id)
141 {
142 #ifdef HAVE_NET_IF_H
143 int sd;
144 struct ifreq ifr, *ifrp;
145 struct ifconf ifc;
146 char buf[1024];
147 int n, i;
148 unsigned char *a;
149 #ifdef HAVE_NET_IF_DL_H
150 struct sockaddr_dl *sdlp;
151 #endif
152
153 /*
154 * BSD 4.4 defines the size of an ifreq to be
155 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
156 * However, under earlier systems, sa_len isn't present, so the size is
157 * just sizeof(struct ifreq)
158 */
159 #ifdef HAVE_SA_LEN
160 #ifndef max
161 #define max(a,b) ((a) > (b) ? (a) : (b))
162 #endif
163 #define ifreq_size(i) max(sizeof(struct ifreq),\
164 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
165 #else
166 #define ifreq_size(i) sizeof(struct ifreq)
167 #endif /* HAVE_SA_LEN*/
168
169 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
170 if (sd < 0) {
171 return -1;
172 }
173 memset(buf, 0, sizeof(buf));
174 ifc.ifc_len = sizeof(buf);
175 ifc.ifc_buf = buf;
176 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
177 close(sd);
178 return -1;
179 }
180 n = ifc.ifc_len;
181 for (i = 0; i < n; i+= ifreq_size(*ifrp) ) {
182 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
183 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
184 #ifdef SIOCGIFHWADDR
185 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
186 continue;
187 a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
188 #else
189 #ifdef SIOCGENADDR
190 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
191 continue;
192 a = (unsigned char *) ifr.ifr_enaddr;
193 #else
194 #ifdef HAVE_NET_IF_DL_H
195 sdlp = (struct sockaddr_dl *) &ifrp->ifr_addr;
196 if ((sdlp->sdl_family != AF_LINK) || (sdlp->sdl_alen != 6))
197 continue;
198 a = (unsigned char *) &sdlp->sdl_data[sdlp->sdl_nlen];
199 #else
200 /*
201 * XXX we don't have a way of getting the hardware
202 * address
203 */
204 close(sd);
205 return 0;
206 #endif /* HAVE_NET_IF_DL_H */
207 #endif /* SIOCGENADDR */
208 #endif /* SIOCGIFHWADDR */
209 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
210 continue;
211 if (node_id) {
212 memcpy(node_id, a, 6);
213 close(sd);
214 return 1;
215 }
216 }
217 close(sd);
218 #endif
219 return 0;
220 }
221
222 /* Assume that the gettimeofday() has microsecond granularity */
223 #define MAX_ADJUSTMENT 10
224
225 static int get_clock(uint32_t *clock_high, uint32_t *clock_low, uint16_t *ret_clock_seq)
226 {
227 static int adjustment = 0;
228 static struct timeval last = {0, 0};
229 static uint16_t clock_seq;
230 struct timeval tv;
231 unsigned long long clock_reg;
232
233 try_again:
234 gettimeofday(&tv, 0);
235 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
236 get_random_bytes(&clock_seq, sizeof(clock_seq));
237 clock_seq &= 0x3FFF;
238 last = tv;
239 last.tv_sec--;
240 }
241 if ((tv.tv_sec < last.tv_sec) ||
242 ((tv.tv_sec == last.tv_sec) &&
243 (tv.tv_usec < last.tv_usec))) {
244 clock_seq = (clock_seq+1) & 0x3FFF;
245 adjustment = 0;
246 last = tv;
247 } else if ((tv.tv_sec == last.tv_sec) &&
248 (tv.tv_usec == last.tv_usec)) {
249 if (adjustment >= MAX_ADJUSTMENT)
250 goto try_again;
251 adjustment++;
252 } else {
253 adjustment = 0;
254 last = tv;
255 }
256
257 clock_reg = tv.tv_usec*10 + adjustment;
258 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
259 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
260
261 *clock_high = clock_reg >> 32;
262 *clock_low = clock_reg;
263 *ret_clock_seq = clock_seq;
264 return 0;
265 }
266
267 UUID_STATIC
268 void uuid_generate_time(uuid_t out)
269 {
270 static unsigned char node_id[6];
271 static int has_init = 0;
272 struct uuid uu;
273 uint32_t clock_mid;
274
275 if (!has_init) {
276 if (get_node_id(node_id) <= 0) {
277 get_random_bytes(node_id, 6);
278 /*
279 * Set multicast bit, to prevent conflicts
280 * with IEEE 802 addresses obtained from
281 * network cards
282 */
283 node_id[0] |= 0x01;
284 }
285 has_init = 1;
286 }
287 get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
288 uu.clock_seq |= 0x8000;
289 uu.time_mid = (uint16_t) clock_mid;
290 uu.time_hi_and_version = ((clock_mid >> 16) & 0x0FFF) | 0x1000;
291 memcpy(uu.node, node_id, 6);
292 uuid_pack(&uu, out);
293 }
294
295 UUID_STATIC
296 void uuid_generate_random(uuid_t out)
297 {
298 uuid_t buf;
299 struct uuid uu;
300
301 get_random_bytes(buf, sizeof(buf));
302 uuid_unpack(buf, &uu);
303
304 uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
305 uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
306 uuid_pack(&uu, out);
307 }
308
309 /*
310 * This is the generic front-end to uuid_generate_random and
311 * uuid_generate_time. It uses uuid_generate_random only if
312 * /dev/urandom is available, since otherwise we won't have
313 * high-quality randomness.
314 */
315 UUID_STATIC
316 void uuid_generate(uuid_t out)
317 {
318 if (get_random_fd() >= 0)
319 uuid_generate_random(out);
320 else
321 uuid_generate_time(out);
322 }