| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 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 |
|
| 106 |
|
| 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 |
|
| 130 |
|
| 131 |
|
| 132 |
for (cp = buf, i = 0; i < nbytes; i++) |
| 133 |
*cp++ ^= (rand() >> 7) & 0xFF; |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 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 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
| 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 |
|
| 202 |
|
| 203 |
|
| 204 |
close(sd); |
| 205 |
return 0; |
| 206 |
#endif |
| 207 |
#endif |
| 208 |
#endif |
| 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 |
|
| 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 |
|
| 280 |
|
| 281 |
|
| 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 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 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 |
} |