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"
13 #include <afs/param.h>
15 #include <afsconfig.h>
20 #include "../afs/sysincludes.h"
21 #include "../afs/afsincludes.h"
22 #define uuid_memcmp(A,B,C) bcmp(A,B,C)
23 #define uuid_memcpy(A,B,C) bcopy(B,A,C)
32 #include <netinet/in.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
38 #endif /* ITIMER_REAL */
43 #if !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV)
44 #include <netinet/if_ether.h>
48 #define uuid_memcmp(A,B,C) memcmp(A,B,C)
49 #define uuid_memcpy(A,B,C) memcpy(A,B,C)
54 char eaddr[6]; /* 6 bytes of ethernet hardware address */
55 } uuid_address_t, *uuid_address_p_t;
61 } uuid_time_t, *uuid_time_p_t;
63 static int uuid_get_address (uuid_address_p_t addr);
64 void uuid__get_os_time (uuid_time_t *os_time);
67 * |<------------------------- 32 bits -------------------------->|
69 * +--------------------------------------------------------------+
70 * | low 32 bits of time | 0-3 .time_low
71 * +-------------------------------+-------------------------------
72 * | mid 16 bits of time | 4-5 .time_mid
73 * +-------+-----------------------+
74 * | vers. | hi 12 bits of time | 6-7 .time_hi_and_version
75 * +-------+-------+---------------+
76 * |Res| clkSeqHi | 8 .clock_seq_hi_and_reserved
78 * | clkSeqLow | 9 .clock_seq_low
79 * +---------------+----------...-----+
80 * | node ID | 8-16 .node
81 * +--------------------------...-----+
84 afsUUID afs_uuid_g_nil_uuid = { 0 };
85 static uuid_time_t time_now, time_last;
86 static u_short uuid_time_adjust, clock_seq;
87 static afs_uint32 rand_m, rand_ia, rand_ib, rand_irand, uuid_init_done = 0;
89 #define uuid_create_nil(uuid) memset(uuid, 0, sizeof(afsUUID))
90 afs_uuid_equal(u1, u2) afsUUID *u1, *u2; { return(uuid_memcmp((void *)u1, (void *)u2, sizeof (afsUUID)) == 0); }
91 afs_uuid_is_nil(u1) afsUUID *u1; {
93 return(uuid_memcmp((void *)u1, (void *)&afs_uuid_g_nil_uuid, sizeof (afsUUID)) == 0);
97 void afs_htonuuid(uuidp)
99 uuidp->time_low = htonl(uuidp->time_low);
100 uuidp->time_mid = htons(uuidp->time_mid);
101 uuidp->time_hi_and_version = htons(uuidp->time_hi_and_version);
104 void afs_ntohuuid(uuidp)
106 uuidp->time_low = ntohl(uuidp->time_low);
107 uuidp->time_mid = ntohs(uuidp->time_mid);
108 uuidp->time_hi_and_version = ntohs(uuidp->time_hi_and_version);
111 static u_short true_random () {
115 if (rand_m >= 9973) rand_m -= 9871;
116 if (rand_ia >= 99991) rand_ia -= 89989;
117 if (rand_ib >= 224729) rand_ib -= 96233;
118 rand_irand = (rand_irand * rand_m) + rand_ia + rand_ib;
119 return (((rand_irand) >> 16) ^ (rand_irand & 0x3fff));
123 static afs_int32 time_cmp (time1, time2)
125 uuid_time_p_t time2; {
126 if (time1->hi < time2->hi) return (-1);
127 if (time1->hi > time2->hi) return (1);
128 if (time1->lo < time2->lo) return (-1);
129 if (time1->lo > time2->lo) return (1);
133 afs_uuid_create (uuid)
135 uuid_address_t eaddr;
136 afs_int32 got_no_time = 0, code;
138 if (!uuid_init_done) {
140 u_short *seedp, seed=0;
146 * Generating our 'seed' value
148 * We start with the current time, but, since the resolution of clocks is
149 * system hardware dependent (eg. Ultrix is 10 msec.) and most likely
150 * coarser than our resolution (10 usec) we 'mixup' the bits by xor'ing
151 * all the bits together. This will have the effect of involving all of
152 * the bits in the determination of the seed value while remaining system
153 * independent. Then for good measure to ensure a unique seed when there
154 * are multiple processes creating UUID's on a system, we add in the PID.
156 uuid__get_os_time(&t);
157 seedp = (u_short *)(&t);
162 rand_irand += seed + (afs_uint32)getpid();
163 uuid__get_os_time (&time_last);
164 clock_seq = true_random();
166 if (afs_winsockInit()<0) {
167 return WSAGetLastError();
172 if (code = uuid_get_address (&eaddr)) return code; /* get our hardware network address */
174 /* get the current time */
175 uuid__get_os_time (&time_now);
177 * check that our clock hasn't gone backwards and handle it
178 * accordingly with clock_seq
179 * check that we're not generating uuid's faster than we
180 * can accommodate with our uuid_time_adjust fudge factor
182 if ((code = time_cmp (&time_now, &time_last)) == -1) {
183 /* A clock_seq value of 0 indicates that it hasn't been initialized. */
184 if (clock_seq == 0) {
185 clock_seq = true_random();
187 clock_seq = (clock_seq + 1) & 0x3fff;
188 if (clock_seq == 0) clock_seq = clock_seq + 1;
189 uuid_time_adjust = 0;
190 } else if (code == 1) {
191 uuid_time_adjust = 0;
193 if (uuid_time_adjust == 0x7fff) /* spin while we wait for the clock to tick */
198 } while (got_no_time);
199 time_last.lo = time_now.lo;
200 time_last.hi = time_now.hi;
201 if (uuid_time_adjust != 0) {
202 if (time_now.lo & 0x80000000) {
203 time_now.lo += uuid_time_adjust;
204 if (!(time_now.lo & 0x80000000)) time_now.hi++;
206 time_now.lo += uuid_time_adjust;
208 uuid->time_low = time_now.lo;
209 uuid->time_mid = time_now.hi & 0x0000ffff;
210 uuid->time_hi_and_version = (time_now.hi & 0x0fff0000) >> 16;
211 uuid->time_hi_and_version |= (1 << 12);
212 uuid->clock_seq_low = clock_seq & 0xff;
213 uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3f00) >> 8;
214 uuid->clock_seq_hi_and_reserved |= 0x80;
215 uuid_memcpy ((void *)uuid->node, (void *)&eaddr, sizeof (uuid_address_t));
219 u_short afs_uuid_hash (uuid)
221 short c0=0, c1=0, x, y;
222 char *next_uuid = (char *) uuid;
225 * For speed lets unroll the following loop:
227 * for (i = 0; i < UUID_K_LENGTH; i++)
229 * c0 = c0 + *next_uuid++;
233 c0 = c0 + *next_uuid++;
235 c0 = c0 + *next_uuid++;
237 c0 = c0 + *next_uuid++;
239 c0 = c0 + *next_uuid++;
241 c0 = c0 + *next_uuid++;
243 c0 = c0 + *next_uuid++;
245 c0 = c0 + *next_uuid++;
247 c0 = c0 + *next_uuid++;
249 c0 = c0 + *next_uuid++;
251 c0 = c0 + *next_uuid++;
253 c0 = c0 + *next_uuid++;
255 c0 = c0 + *next_uuid++;
257 c0 = c0 + *next_uuid++;
259 c0 = c0 + *next_uuid++;
261 c0 = c0 + *next_uuid++;
263 c0 = c0 + *next_uuid++;
265 /* Calculate the value for "First octet" of the hash */
270 /* Calculate the value for "second octet" of the hash */
275 return ((y * 256) + x);
280 extern struct interfaceAddr afs_cb_interface;
282 static int uuid_get_address (uuid_address_p_t addr)
284 uuid_memcpy((void *)addr->eaddr, (void *)&afs_cb_interface.addr_in[0], 4);
285 addr->eaddr[4] = 0xaa;
286 addr->eaddr[5] = 0x77;
290 void uuid__get_os_time (uuid_time_t *os_time)
295 os_time->hi = tp.tv_sec;
296 os_time->lo = tp.tv_usec*10;
301 char hostName1[128] = "localhost";
302 static int uuid_get_address (uuid_address_p_t addr)
308 code = gethostname(hostName1, 64);
310 printf("gethostname() failed\n");
317 he = gethostbyname(hostName1);
319 printf("Can't find address for '%s'\n", hostName1);
326 uuid_memcpy(&addr1, he->h_addr_list[0], 4);
327 addr1 = ntohl(addr1);
328 uuid_memcpy(addr->eaddr, &addr1, 4);
329 addr->eaddr[4] = 0xaa;
330 addr->eaddr[5] = 0x77;
332 printf ("uuid_get_address: %02x-%02x-%02x-%02x-%02x-%02x\n",
333 addr->eaddr[0], addr->eaddr[1], addr->eaddr[2],
334 addr->eaddr[3], addr->eaddr[4], addr->eaddr[5]);
340 void uuid__get_os_time (uuid_time_t *os_time)
344 if (gettimeofday (&tp, (struct timezone *) 0)) {
345 perror ("uuid__get_time");
348 os_time->hi = tp.tv_sec;
349 os_time->lo = tp.tv_usec*10;