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