1 /* vi: set sw=4 ts=4: */
3 * gen_uuid.c --- generate a DCE-compatible uuid
5 * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, and the entire permission notice in its entirety,
13 * including the disclaimer of warranties.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
24 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
41 #include <sys/types.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
48 #include <sys/socket.h>
49 #ifdef HAVE_SYS_SOCKIO_H
50 #include <sys/sockio.h>
55 #ifdef HAVE_NETINET_IN_H
56 #include <netinet/in.h>
58 #ifdef HAVE_NET_IF_DL_H
59 #include <net/if_dl.h>
65 #define srand(x) srandom(x)
66 #define rand() random()
69 static int get_random_fd(void)
77 fd
= open("/dev/urandom", O_RDONLY
);
79 fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
);
80 srand((getpid() << 16) ^ getuid() ^ tv
.tv_sec
^ tv
.tv_usec
);
82 /* Crank the random number generator a few times */
84 for (i
= (tv
.tv_sec
^ tv
.tv_usec
) & 0x1F; i
> 0; i
--)
91 * Generate a series of random bytes. Use /dev/urandom if possible,
92 * and if not, use srandom/random.
94 static void get_random_bytes(void *buf
, int nbytes
)
96 int i
, n
= nbytes
, fd
= get_random_fd();
98 unsigned char *cp
= (unsigned char *) buf
;
104 if (lose_counter
++ > 16)
115 * We do this all the time, but this is the only source of
116 * randomness if /dev/random/urandom is out to lunch.
118 for (cp
= buf
, i
= 0; i
< nbytes
; i
++)
119 *cp
++ ^= (rand() >> 7) & 0xFF;
123 * Get the ethernet hardware address, if we can find it...
125 static int get_node_id(unsigned char *node_id
)
129 struct ifreq ifr
, *ifrp
;
134 #ifdef HAVE_NET_IF_DL_H
135 struct sockaddr_dl
*sdlp
;
139 * BSD 4.4 defines the size of an ifreq to be
140 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
141 * However, under earlier systems, sa_len isn't present, so the size is
142 * just sizeof(struct ifreq)
146 #define max(a,b) ((a) > (b) ? (a) : (b))
148 #define ifreq_size(i) max(sizeof(struct ifreq),\
149 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
151 #define ifreq_size(i) sizeof(struct ifreq)
152 #endif /* HAVE_SA_LEN*/
154 sd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
158 memset(buf
, 0, sizeof(buf
));
159 ifc
.ifc_len
= sizeof(buf
);
161 if (ioctl (sd
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
166 for (i
= 0; i
< n
; i
+= ifreq_size(*ifrp
) ) {
167 ifrp
= (struct ifreq
*)((char *) ifc
.ifc_buf
+i
);
168 strncpy_IFNAMSIZ(ifr
.ifr_name
, ifrp
->ifr_name
);
170 if (ioctl(sd
, SIOCGIFHWADDR
, &ifr
) < 0)
172 a
= (unsigned char *) &ifr
.ifr_hwaddr
.sa_data
;
175 if (ioctl(sd
, SIOCGENADDR
, &ifr
) < 0)
177 a
= (unsigned char *) ifr
.ifr_enaddr
;
179 #ifdef HAVE_NET_IF_DL_H
180 sdlp
= (struct sockaddr_dl
*) &ifrp
->ifr_addr
;
181 if ((sdlp
->sdl_family
!= AF_LINK
) || (sdlp
->sdl_alen
!= 6))
183 a
= (unsigned char *) &sdlp
->sdl_data
[sdlp
->sdl_nlen
];
186 * XXX we don't have a way of getting the hardware
191 #endif /* HAVE_NET_IF_DL_H */
192 #endif /* SIOCGENADDR */
193 #endif /* SIOCGIFHWADDR */
194 if (!a
[0] && !a
[1] && !a
[2] && !a
[3] && !a
[4] && !a
[5])
197 memcpy(node_id
, a
, 6);
207 /* Assume that the gettimeofday() has microsecond granularity */
208 #define MAX_ADJUSTMENT 10
210 static int get_clock(uint32_t *clock_high
, uint32_t *clock_low
, uint16_t *ret_clock_seq
)
212 static int adjustment
= 0;
213 static struct timeval last
= {0, 0};
214 static uint16_t clock_seq
;
216 unsigned long long clock_reg
;
219 gettimeofday(&tv
, 0);
220 if ((last
.tv_sec
== 0) && (last
.tv_usec
== 0)) {
221 get_random_bytes(&clock_seq
, sizeof(clock_seq
));
226 if ((tv
.tv_sec
< last
.tv_sec
) ||
227 ((tv
.tv_sec
== last
.tv_sec
) &&
228 (tv
.tv_usec
< last
.tv_usec
))) {
229 clock_seq
= (clock_seq
+1) & 0x3FFF;
232 } else if ((tv
.tv_sec
== last
.tv_sec
) &&
233 (tv
.tv_usec
== last
.tv_usec
)) {
234 if (adjustment
>= MAX_ADJUSTMENT
)
242 clock_reg
= tv
.tv_usec
*10 + adjustment
;
243 clock_reg
+= ((unsigned long long) tv
.tv_sec
)*10000000;
244 clock_reg
+= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
246 *clock_high
= clock_reg
>> 32;
247 *clock_low
= clock_reg
;
248 *ret_clock_seq
= clock_seq
;
252 void uuid_generate_time(uuid_t out
)
254 static unsigned char node_id
[6];
255 static int has_init
= 0;
260 if (get_node_id(node_id
) <= 0) {
261 get_random_bytes(node_id
, 6);
263 * Set multicast bit, to prevent conflicts
264 * with IEEE 802 addresses obtained from
271 get_clock(&clock_mid
, &uu
.time_low
, &uu
.clock_seq
);
272 uu
.clock_seq
|= 0x8000;
273 uu
.time_mid
= (uint16_t) clock_mid
;
274 uu
.time_hi_and_version
= ((clock_mid
>> 16) & 0x0FFF) | 0x1000;
275 memcpy(uu
.node
, node_id
, 6);
279 void uuid_generate_random(uuid_t out
)
284 get_random_bytes(buf
, sizeof(buf
));
285 uuid_unpack(buf
, &uu
);
287 uu
.clock_seq
= (uu
.clock_seq
& 0x3FFF) | 0x8000;
288 uu
.time_hi_and_version
= (uu
.time_hi_and_version
& 0x0FFF) | 0x4000;
293 * This is the generic front-end to uuid_generate_random and
294 * uuid_generate_time. It uses uuid_generate_random only if
295 * /dev/urandom is available, since otherwise we won't have
296 * high-quality randomness.
298 void uuid_generate(uuid_t out
)
300 if (get_random_fd() >= 0)
301 uuid_generate_random(out
);
303 uuid_generate_time(out
);