Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * gen_uuid.c --- generate a DCE-compatible uuid |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 3 | * |
Theodore Ts'o | 3030daa | 2000-04-07 20:06:04 +0000 | [diff] [blame] | 4 | * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o. |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 5 | * |
| 6 | * %Begin-Header% |
Theodore Ts'o | 3030daa | 2000-04-07 20:06:04 +0000 | [diff] [blame] | 7 | * This file may be redistributed under the terms of the GNU |
| 8 | * Library General Public License. |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 9 | * %End-Header% |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Theodore Ts'o | b1416db | 2001-05-01 15:32:44 +0000 | [diff] [blame] | 12 | /* |
| 13 | * Force inclusion of SVID stuff since we need it if we're compiling in |
| 14 | * gcc-wall wall mode |
| 15 | */ |
| 16 | #define _SVID_SOURCE |
| 17 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 18 | #ifdef HAVE_UNISTD_H |
| 19 | #include <unistd.h> |
| 20 | #endif |
| 21 | #ifdef HAVE_STDLIB_H |
| 22 | #include <stdlib.h> |
| 23 | #endif |
| 24 | #include <string.h> |
| 25 | #include <fcntl.h> |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/file.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <sys/socket.h> |
| 33 | #ifdef HAVE_SYS_SOCKIO_H |
| 34 | #include <sys/sockio.h> |
| 35 | #endif |
| 36 | #ifdef HAVE_NET_IF_H |
| 37 | #include <net/if.h> |
| 38 | #endif |
| 39 | #ifdef HAVE_NETINET_IN_H |
| 40 | #include <netinet/in.h> |
| 41 | #endif |
| 42 | |
| 43 | #include "uuidP.h" |
| 44 | |
| 45 | #ifdef HAVE_SRANDOM |
| 46 | #define srand(x) srandom(x) |
| 47 | #define rand() random() |
| 48 | #endif |
| 49 | |
Theodore Ts'o | b1416db | 2001-05-01 15:32:44 +0000 | [diff] [blame] | 50 | static int get_random_fd(void) |
Theodore Ts'o | 5dd7ff0 | 2000-06-12 17:35:13 +0000 | [diff] [blame] | 51 | { |
Theodore Ts'o | 96394d1 | 2001-01-12 18:30:54 +0000 | [diff] [blame] | 52 | struct timeval tv; |
| 53 | static int fd = -2; |
| 54 | int i; |
Theodore Ts'o | 5dd7ff0 | 2000-06-12 17:35:13 +0000 | [diff] [blame] | 55 | |
| 56 | if (fd == -2) { |
Theodore Ts'o | 96394d1 | 2001-01-12 18:30:54 +0000 | [diff] [blame] | 57 | gettimeofday(&tv, 0); |
Theodore Ts'o | 5dd7ff0 | 2000-06-12 17:35:13 +0000 | [diff] [blame] | 58 | fd = open("/dev/urandom", O_RDONLY); |
| 59 | if (fd == -1) |
| 60 | fd = open("/dev/random", O_RDONLY | O_NONBLOCK); |
Theodore Ts'o | 96394d1 | 2001-01-12 18:30:54 +0000 | [diff] [blame] | 61 | srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec); |
Theodore Ts'o | 5dd7ff0 | 2000-06-12 17:35:13 +0000 | [diff] [blame] | 62 | } |
| 63 | /* Crank the random number generator a few times */ |
Theodore Ts'o | 96394d1 | 2001-01-12 18:30:54 +0000 | [diff] [blame] | 64 | gettimeofday(&tv, 0); |
| 65 | for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--) |
Theodore Ts'o | 5dd7ff0 | 2000-06-12 17:35:13 +0000 | [diff] [blame] | 66 | rand(); |
| 67 | return fd; |
| 68 | } |
| 69 | |
| 70 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 71 | /* |
| 72 | * Generate a series of random bytes. Use /dev/urandom if possible, |
| 73 | * and if not, use srandom/random. |
| 74 | */ |
| 75 | static void get_random_bytes(void *buf, int nbytes) |
| 76 | { |
Theodore Ts'o | 5dd7ff0 | 2000-06-12 17:35:13 +0000 | [diff] [blame] | 77 | int i, fd = get_random_fd(); |
Theodore Ts'o | e589f67 | 2000-04-03 13:45:40 +0000 | [diff] [blame] | 78 | int lose_counter = 0; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 79 | char *cp = (char *) buf; |
| 80 | |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 81 | if (fd >= 0) { |
| 82 | while (nbytes > 0) { |
| 83 | i = read(fd, cp, nbytes); |
Theodore Ts'o | e589f67 | 2000-04-03 13:45:40 +0000 | [diff] [blame] | 84 | if ((i < 0) && |
| 85 | ((errno == EINTR) || (errno == EAGAIN))) |
| 86 | continue; |
| 87 | if (i <= 0) { |
| 88 | if (lose_counter++ == 8) |
| 89 | break; |
| 90 | continue; |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 91 | } |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 92 | nbytes -= i; |
| 93 | cp += i; |
Theodore Ts'o | e589f67 | 2000-04-03 13:45:40 +0000 | [diff] [blame] | 94 | lose_counter = 0; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 97 | |
| 98 | /* XXX put something better here if no /dev/random! */ |
Theodore Ts'o | 96394d1 | 2001-01-12 18:30:54 +0000 | [diff] [blame] | 99 | for (i = 0; i < nbytes; i++) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 100 | *cp++ = rand() & 0xFF; |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 101 | return; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Get the ethernet hardware address, if we can find it... |
| 106 | */ |
| 107 | static int get_node_id(unsigned char *node_id) |
| 108 | { |
| 109 | #ifdef HAVE_NET_IF_H |
| 110 | int sd; |
| 111 | struct ifreq ifr, *ifrp; |
| 112 | struct ifconf ifc; |
| 113 | char buf[1024]; |
| 114 | int n, i; |
| 115 | unsigned char *a; |
| 116 | |
| 117 | /* |
| 118 | * BSD 4.4 defines the size of an ifreq to be |
| 119 | * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len |
| 120 | * However, under earlier systems, sa_len isn't present, so the size is |
| 121 | * just sizeof(struct ifreq) |
| 122 | */ |
| 123 | #ifdef HAVE_SA_LEN |
| 124 | #ifndef max |
| 125 | #define max(a,b) ((a) > (b) ? (a) : (b)) |
| 126 | #endif |
| 127 | #define ifreq_size(i) max(sizeof(struct ifreq),\ |
| 128 | sizeof((i).ifr_name)+(i).ifr_addr.sa_len) |
| 129 | #else |
| 130 | #define ifreq_size(i) sizeof(struct ifreq) |
| 131 | #endif /* HAVE_SA_LEN*/ |
| 132 | |
| 133 | sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 134 | if (sd < 0) { |
| 135 | return -1; |
| 136 | } |
| 137 | memset(buf, 0, sizeof(buf)); |
| 138 | ifc.ifc_len = sizeof(buf); |
| 139 | ifc.ifc_buf = buf; |
| 140 | if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) { |
| 141 | close(sd); |
| 142 | return -1; |
| 143 | } |
| 144 | n = ifc.ifc_len; |
| 145 | for (i = 0; i < n; i+= ifreq_size(*ifr) ) { |
Theodore Ts'o | dc3710e | 1998-03-09 03:34:48 +0000 | [diff] [blame] | 146 | ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 147 | strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ); |
| 148 | #ifdef SIOCGIFHWADDR |
| 149 | if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0) |
| 150 | continue; |
| 151 | a = (unsigned char *) &ifr.ifr_hwaddr.sa_data; |
| 152 | #else |
| 153 | #ifdef SIOCGENADDR |
| 154 | if (ioctl(sd, SIOCGENADDR, &ifr) < 0) |
| 155 | continue; |
| 156 | a = (unsigned char *) ifr.ifr_enaddr; |
| 157 | #else |
| 158 | /* |
| 159 | * XXX we don't have a way of getting the hardware |
| 160 | * address |
| 161 | */ |
| 162 | close(sd); |
| 163 | return 0; |
| 164 | #endif /* SIOCGENADDR */ |
| 165 | #endif /* SIOCGIFHWADDR */ |
| 166 | if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5]) |
| 167 | continue; |
| 168 | if (node_id) { |
| 169 | memcpy(node_id, a, 6); |
| 170 | close(sd); |
| 171 | return 1; |
| 172 | } |
| 173 | } |
| 174 | close(sd); |
| 175 | #endif |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* Assume that the gettimeofday() has microsecond granularity */ |
| 180 | #define MAX_ADJUSTMENT 10 |
| 181 | |
| 182 | static int get_clock(__u32 *clock_high, __u32 *clock_low, __u16 *ret_clock_seq) |
| 183 | { |
| 184 | static int adjustment = 0; |
| 185 | static struct timeval last = {0, 0}; |
| 186 | static __u16 clock_seq; |
| 187 | struct timeval tv; |
Theodore Ts'o | d546447 | 1997-10-15 01:52:44 +0000 | [diff] [blame] | 188 | unsigned long long clock_reg; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 189 | |
| 190 | try_again: |
| 191 | gettimeofday(&tv, 0); |
| 192 | if ((last.tv_sec == 0) && (last.tv_usec == 0)) { |
| 193 | get_random_bytes(&clock_seq, sizeof(clock_seq)); |
| 194 | clock_seq &= 0x1FFF; |
| 195 | last = tv; |
| 196 | last.tv_sec--; |
| 197 | } |
| 198 | if ((tv.tv_sec < last.tv_sec) || |
| 199 | ((tv.tv_sec == last.tv_sec) && |
| 200 | (tv.tv_usec < last.tv_usec))) { |
| 201 | clock_seq = (clock_seq+1) & 0x1FFF; |
| 202 | adjustment = 0; |
Theodore Ts'o | fa7cc28 | 2000-04-03 16:51:06 +0000 | [diff] [blame] | 203 | last = tv; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 204 | } else if ((tv.tv_sec == last.tv_sec) && |
| 205 | (tv.tv_usec == last.tv_usec)) { |
| 206 | if (adjustment >= MAX_ADJUSTMENT) |
| 207 | goto try_again; |
| 208 | adjustment++; |
Theodore Ts'o | fa7cc28 | 2000-04-03 16:51:06 +0000 | [diff] [blame] | 209 | } else { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 210 | adjustment = 0; |
Theodore Ts'o | fa7cc28 | 2000-04-03 16:51:06 +0000 | [diff] [blame] | 211 | last = tv; |
| 212 | } |
| 213 | |
Theodore Ts'o | d546447 | 1997-10-15 01:52:44 +0000 | [diff] [blame] | 214 | clock_reg = tv.tv_usec*10 + adjustment; |
| 215 | clock_reg += ((unsigned long long) tv.tv_sec)*10000000; |
| 216 | clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 217 | |
Theodore Ts'o | d546447 | 1997-10-15 01:52:44 +0000 | [diff] [blame] | 218 | *clock_high = clock_reg >> 32; |
| 219 | *clock_low = clock_reg; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 220 | *ret_clock_seq = clock_seq; |
| 221 | return 0; |
| 222 | } |
| 223 | |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 224 | void uuid_generate_time(uuid_t out) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 225 | { |
| 226 | static unsigned char node_id[6]; |
| 227 | static int has_init = 0; |
| 228 | struct uuid uu; |
| 229 | __u32 clock_mid; |
| 230 | |
| 231 | if (!has_init) { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 232 | if (get_node_id(node_id) <= 0) { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 233 | get_random_bytes(node_id, 6); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 234 | /* |
| 235 | * Set multicast bit, to prevent conflicts |
| 236 | * with IEEE 802 addresses obtained from |
| 237 | * network cards |
| 238 | */ |
| 239 | node_id[0] |= 0x80; |
| 240 | } |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 241 | has_init = 1; |
| 242 | } |
| 243 | get_clock(&clock_mid, &uu.time_low, &uu.clock_seq); |
| 244 | uu.clock_seq |= 0x8000; |
| 245 | uu.time_mid = (__u16) clock_mid; |
| 246 | uu.time_hi_and_version = (clock_mid >> 16) | 0x1000; |
| 247 | memcpy(uu.node, node_id, 6); |
| 248 | uuid_pack(&uu, out); |
| 249 | } |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 250 | |
| 251 | void uuid_generate_random(uuid_t out) |
| 252 | { |
| 253 | uuid_t buf; |
| 254 | struct uuid uu; |
| 255 | |
| 256 | get_random_bytes(buf, sizeof(buf)); |
| 257 | uuid_unpack(buf, &uu); |
| 258 | |
| 259 | uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000; |
| 260 | uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000; |
| 261 | uuid_pack(&uu, out); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * This is the generic front-end to uuid_generate_random and |
| 266 | * uuid_generate_time. It uses uuid_generate_random only if |
| 267 | * /dev/urandom is available, since otherwise we won't have |
| 268 | * high-quality randomness. |
| 269 | */ |
| 270 | void uuid_generate(uuid_t out) |
| 271 | { |
Theodore Ts'o | 5dd7ff0 | 2000-06-12 17:35:13 +0000 | [diff] [blame] | 272 | if (get_random_fd() >= 0) |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 273 | uuid_generate_random(out); |
| 274 | else |
| 275 | uuid_generate_time(out); |
| 276 | } |