2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 #include "../afs/param.h"
12 #include "../afs/sysincludes.h"
13 #include "../afs/afsincludes.h"
14 #define uuid_memcmp(A,B,C) bcmp(A,B,C)
15 #define uuid_memcpy(A,B,C) bcopy(B,A,C)
17 #include <afs/param.h>
25 #include <netinet/in.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
31 #endif /* ITIMER_REAL */
36 #if !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV)
37 #include <netinet/if_ether.h>
41 #define uuid_memcmp(A,B,C) memcmp(A,B,C)
42 #define uuid_memcpy(A,B,C) memcpy(A,B,C)
47 char eaddr[6]; /* 6 bytes of ethernet hardware address */
48 } uuid_address_t, *uuid_address_p_t;
54 } uuid_time_t, *uuid_time_p_t;
56 static int uuid_get_address (uuid_address_p_t addr);
57 void uuid__get_os_time (uuid_time_t *os_time);
60 * |<------------------------- 32 bits -------------------------->|
62 * +--------------------------------------------------------------+
63 * | low 32 bits of time | 0-3 .time_low
64 * +-------------------------------+-------------------------------
65 * | mid 16 bits of time | 4-5 .time_mid
66 * +-------+-----------------------+
67 * | vers. | hi 12 bits of time | 6-7 .time_hi_and_version
68 * +-------+-------+---------------+
69 * |Res| clkSeqHi | 8 .clock_seq_hi_and_reserved
71 * | clkSeqLow | 9 .clock_seq_low
72 * +---------------+----------...-----+
73 * | node ID | 8-16 .node
74 * +--------------------------...-----+
77 afsUUID afs_uuid_g_nil_uuid = { 0 };
78 static uuid_time_t time_now, time_last;
79 static u_short uuid_time_adjust, clock_seq;
80 static afs_uint32 rand_m, rand_ia, rand_ib, rand_irand, uuid_init_done = 0;
82 #define uuid_create_nil(uuid) memset(uuid, 0, sizeof(afsUUID))
83 afs_uuid_equal(u1, u2) afsUUID *u1, *u2; { return(uuid_memcmp((void *)u1, (void *)u2, sizeof (afsUUID)) == 0); }
84 afs_uuid_is_nil(u1) afsUUID *u1; {
86 return(uuid_memcmp((void *)u1, (void *)&afs_uuid_g_nil_uuid, sizeof (afsUUID)) == 0);
90 void afs_htonuuid(uuidp)
92 uuidp->time_low = htonl(uuidp->time_low);
93 uuidp->time_mid = htons(uuidp->time_mid);
94 uuidp->time_hi_and_version = htons(uuidp->time_hi_and_version);
97 void afs_ntohuuid(uuidp)
99 uuidp->time_low = ntohl(uuidp->time_low);
100 uuidp->time_mid = ntohs(uuidp->time_mid);
101 uuidp->time_hi_and_version = ntohs(uuidp->time_hi_and_version);
104 static u_short true_random () {
108 if (rand_m >= 9973) rand_m -= 9871;
109 if (rand_ia >= 99991) rand_ia -= 89989;
110 if (rand_ib >= 224729) rand_ib -= 96233;
111 rand_irand = (rand_irand * rand_m) + rand_ia + rand_ib;
112 return (((rand_irand) >> 16) ^ (rand_irand & 0x3fff));
116 static afs_int32 time_cmp (time1, time2)
118 uuid_time_p_t time2; {
119 if (time1->hi < time2->hi) return (-1);
120 if (time1->hi > time2->hi) return (1);
121 if (time1->lo < time2->lo) return (-1);
122 if (time1->lo > time2->lo) return (1);
126 afs_uuid_create (uuid)
128 uuid_address_t eaddr;
129 afs_int32 got_no_time = 0, code;
131 if (!uuid_init_done) {
133 u_short *seedp, seed=0;
139 * Generating our 'seed' value
141 * We start with the current time, but, since the resolution of clocks is
142 * system hardware dependent (eg. Ultrix is 10 msec.) and most likely
143 * coarser than our resolution (10 usec) we 'mixup' the bits by xor'ing
144 * all the bits together. This will have the effect of involving all of
145 * the bits in the determination of the seed value while remaining system
146 * independent. Then for good measure to ensure a unique seed when there
147 * are multiple processes creating UUID's on a system, we add in the PID.
149 uuid__get_os_time(&t);
150 seedp = (u_short *)(&t);
155 rand_irand += seed + (afs_uint32)getpid();
156 uuid__get_os_time (&time_last);
157 clock_seq = true_random();
159 if (afs_winsockInit()<0) {
160 return WSAGetLastError();
165 if (code = uuid_get_address (&eaddr)) return code; /* get our hardware network address */
167 /* get the current time */
168 uuid__get_os_time (&time_now);
170 * check that our clock hasn't gone backwards and handle it
171 * accordingly with clock_seq
172 * check that we're not generating uuid's faster than we
173 * can accommodate with our uuid_time_adjust fudge factor
175 if ((code = time_cmp (&time_now, &time_last)) == -1) {
176 /* A clock_seq value of 0 indicates that it hasn't been initialized. */
177 if (clock_seq == 0) {
178 clock_seq = true_random();
180 clock_seq = (clock_seq + 1) & 0x3fff;
181 if (clock_seq == 0) clock_seq = clock_seq + 1;
182 uuid_time_adjust = 0;
183 } else if (code == 1) {
184 uuid_time_adjust = 0;
186 if (uuid_time_adjust == 0x7fff) /* spin while we wait for the clock to tick */
191 } while (got_no_time);
192 time_last.lo = time_now.lo;
193 time_last.hi = time_now.hi;
194 if (uuid_time_adjust != 0) {
195 if (time_now.lo & 0x80000000) {
196 time_now.lo += uuid_time_adjust;
197 if (!(time_now.lo & 0x80000000)) time_now.hi++;
199 time_now.lo += uuid_time_adjust;
201 uuid->time_low = time_now.lo;
202 uuid->time_mid = time_now.hi & 0x0000ffff;
203 uuid->time_hi_and_version = (time_now.hi & 0x0fff0000) >> 16;
204 uuid->time_hi_and_version |= (1 << 12);
205 uuid->clock_seq_low = clock_seq & 0xff;
206 uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3f00) >> 8;
207 uuid->clock_seq_hi_and_reserved |= 0x80;
208 uuid_memcpy ((void *)uuid->node, (void *)&eaddr, sizeof (uuid_address_t));
212 u_short afs_uuid_hash (uuid)
214 short c0=0, c1=0, x, y;
215 char *next_uuid = (char *) uuid;
218 * For speed lets unroll the following loop:
220 * for (i = 0; i < UUID_K_LENGTH; i++)
222 * c0 = c0 + *next_uuid++;
226 c0 = c0 + *next_uuid++;
228 c0 = c0 + *next_uuid++;
230 c0 = c0 + *next_uuid++;
232 c0 = c0 + *next_uuid++;
234 c0 = c0 + *next_uuid++;
236 c0 = c0 + *next_uuid++;
238 c0 = c0 + *next_uuid++;
240 c0 = c0 + *next_uuid++;
242 c0 = c0 + *next_uuid++;
244 c0 = c0 + *next_uuid++;
246 c0 = c0 + *next_uuid++;
248 c0 = c0 + *next_uuid++;
250 c0 = c0 + *next_uuid++;
252 c0 = c0 + *next_uuid++;
254 c0 = c0 + *next_uuid++;
256 c0 = c0 + *next_uuid++;
258 /* Calculate the value for "First octet" of the hash */
263 /* Calculate the value for "second octet" of the hash */
268 return ((y * 256) + x);
273 extern struct interfaceAddr afs_cb_interface;
275 static int uuid_get_address (uuid_address_p_t addr)
277 uuid_memcpy((void *)addr->eaddr, (void *)&afs_cb_interface.addr_in[0], 4);
278 addr->eaddr[4] = 0xaa;
279 addr->eaddr[5] = 0x77;
283 void uuid__get_os_time (uuid_time_t *os_time)
288 os_time->hi = tp.tv_sec;
289 os_time->lo = tp.tv_usec*10;
294 char hostName1[128] = "localhost";
295 static int uuid_get_address (uuid_address_p_t addr)
301 code = gethostname(hostName1, 64);
303 printf("gethostname() failed\n");
310 he = gethostbyname(hostName1);
312 printf("Can't find address for '%s'\n", hostName1);
319 uuid_memcpy(&addr1, he->h_addr_list[0], 4);
320 addr1 = ntohl(addr1);
321 uuid_memcpy(addr->eaddr, &addr1, 4);
322 addr->eaddr[4] = 0xaa;
323 addr->eaddr[5] = 0x77;
325 printf ("uuid_get_address: %02x-%02x-%02x-%02x-%02x-%02x\n",
326 addr->eaddr[0], addr->eaddr[1], addr->eaddr[2],
327 addr->eaddr[3], addr->eaddr[4], addr->eaddr[5]);
333 void uuid__get_os_time (uuid_time_t *os_time)
337 if (gettimeofday (&tp, (struct timezone *) 0)) {
338 perror ("uuid__get_time");
341 os_time->hi = tp.tv_sec;
342 os_time->lo = tp.tv_usec*10;