2 #include "../afs/param.h"
3 #include "../afs/sysincludes.h"
4 #include "../afs/afsincludes.h"
5 #define uuid_memcmp(A,B,C) bcmp(A,B,C)
6 #define uuid_memcpy(A,B,C) bcopy(B,A,C)
16 #include <netinet/in.h>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
22 #endif /* ITIMER_REAL */
27 #if !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV)
28 #include <netinet/if_ether.h>
32 #define uuid_memcmp(A,B,C) memcmp(A,B,C)
33 #define uuid_memcpy(A,B,C) memcpy(A,B,C)
38 char eaddr[6]; /* 6 bytes of ethernet hardware address */
39 } uuid_address_t, *uuid_address_p_t;
45 } uuid_time_t, *uuid_time_p_t;
47 static int uuid_get_address (uuid_address_p_t addr);
48 void uuid__get_os_time (uuid_time_t *os_time);
51 * |<------------------------- 32 bits -------------------------->|
53 * +--------------------------------------------------------------+
54 * | low 32 bits of time | 0-3 .time_low
55 * +-------------------------------+-------------------------------
56 * | mid 16 bits of time | 4-5 .time_mid
57 * +-------+-----------------------+
58 * | vers. | hi 12 bits of time | 6-7 .time_hi_and_version
59 * +-------+-------+---------------+
60 * |Res| clkSeqHi | 8 .clock_seq_hi_and_reserved
62 * | clkSeqLow | 9 .clock_seq_low
63 * +---------------+----------...-----+
64 * | node ID | 8-16 .node
65 * +--------------------------...-----+
68 afsUUID afs_uuid_g_nil_uuid = { 0 };
69 static uuid_time_t time_now, time_last;
70 static u_short uuid_time_adjust, clock_seq;
71 static afs_uint32 rand_m, rand_ia, rand_ib, rand_irand, uuid_init_done = 0;
73 #define uuid_create_nil(uuid) memset(uuid, 0, sizeof(afsUUID))
74 afs_uuid_equal(u1, u2) afsUUID *u1, *u2; { return(uuid_memcmp((void *)u1, (void *)u2, sizeof (afsUUID)) == 0); }
75 afs_uuid_is_nil(u1) afsUUID *u1; {
77 return(uuid_memcmp((void *)u1, (void *)&afs_uuid_g_nil_uuid, sizeof (afsUUID)) == 0);
81 void afs_htonuuid(uuidp)
83 uuidp->time_low = htonl(uuidp->time_low);
84 uuidp->time_mid = htons(uuidp->time_mid);
85 uuidp->time_hi_and_version = htons(uuidp->time_hi_and_version);
88 void afs_ntohuuid(uuidp)
90 uuidp->time_low = ntohl(uuidp->time_low);
91 uuidp->time_mid = ntohs(uuidp->time_mid);
92 uuidp->time_hi_and_version = ntohs(uuidp->time_hi_and_version);
95 static u_short true_random () {
99 if (rand_m >= 9973) rand_m -= 9871;
100 if (rand_ia >= 99991) rand_ia -= 89989;
101 if (rand_ib >= 224729) rand_ib -= 96233;
102 rand_irand = (rand_irand * rand_m) + rand_ia + rand_ib;
103 return (((rand_irand) >> 16) ^ (rand_irand & 0x3fff));
107 static afs_int32 time_cmp (time1, time2)
109 uuid_time_p_t time2; {
110 if (time1->hi < time2->hi) return (-1);
111 if (time1->hi > time2->hi) return (1);
112 if (time1->lo < time2->lo) return (-1);
113 if (time1->lo > time2->lo) return (1);
117 afs_uuid_create (uuid)
119 uuid_address_t eaddr;
120 afs_int32 got_no_time = 0, code;
122 if (!uuid_init_done) {
124 u_short *seedp, seed=0;
130 * Generating our 'seed' value
132 * We start with the current time, but, since the resolution of clocks is
133 * system hardware dependent (eg. Ultrix is 10 msec.) and most likely
134 * coarser than our resolution (10 usec) we 'mixup' the bits by xor'ing
135 * all the bits together. This will have the effect of involving all of
136 * the bits in the determination of the seed value while remaining system
137 * independent. Then for good measure to ensure a unique seed when there
138 * are multiple processes creating UUID's on a system, we add in the PID.
140 uuid__get_os_time(&t);
141 seedp = (u_short *)(&t);
146 rand_irand += seed + (afs_uint32)getpid();
147 uuid__get_os_time (&time_last);
148 clock_seq = true_random();
150 if (afs_winsockInit()<0) {
151 return WSAGetLastError();
156 if (code = uuid_get_address (&eaddr)) return code; /* get our hardware network address */
158 /* get the current time */
159 uuid__get_os_time (&time_now);
161 * check that our clock hasn't gone backwards and handle it
162 * accordingly with clock_seq
163 * check that we're not generating uuid's faster than we
164 * can accommodate with our uuid_time_adjust fudge factor
166 if ((code = time_cmp (&time_now, &time_last)) == -1) {
167 /* A clock_seq value of 0 indicates that it hasn't been initialized. */
168 if (clock_seq == 0) {
169 clock_seq = true_random();
171 clock_seq = (clock_seq + 1) & 0x3fff;
172 if (clock_seq == 0) clock_seq = clock_seq + 1;
173 uuid_time_adjust = 0;
174 } else if (code == 1) {
175 uuid_time_adjust = 0;
177 if (uuid_time_adjust == 0x7fff) /* spin while we wait for the clock to tick */
182 } while (got_no_time);
183 time_last.lo = time_now.lo;
184 time_last.hi = time_now.hi;
185 if (uuid_time_adjust != 0) {
186 if (time_now.lo & 0x80000000) {
187 time_now.lo += uuid_time_adjust;
188 if (!(time_now.lo & 0x80000000)) time_now.hi++;
190 time_now.lo += uuid_time_adjust;
192 uuid->time_low = time_now.lo;
193 uuid->time_mid = time_now.hi & 0x0000ffff;
194 uuid->time_hi_and_version = (time_now.hi & 0x0fff0000) >> 16;
195 uuid->time_hi_and_version |= (1 << 12);
196 uuid->clock_seq_low = clock_seq & 0xff;
197 uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3f00) >> 8;
198 uuid->clock_seq_hi_and_reserved |= 0x80;
199 uuid_memcpy ((void *)uuid->node, (void *)&eaddr, sizeof (uuid_address_t));
203 u_short afs_uuid_hash (uuid)
205 short c0=0, c1=0, x, y;
206 char *next_uuid = (char *) uuid;
209 * For speed lets unroll the following loop:
211 * for (i = 0; i < UUID_K_LENGTH; i++)
213 * c0 = c0 + *next_uuid++;
217 c0 = c0 + *next_uuid++;
219 c0 = c0 + *next_uuid++;
221 c0 = c0 + *next_uuid++;
223 c0 = c0 + *next_uuid++;
225 c0 = c0 + *next_uuid++;
227 c0 = c0 + *next_uuid++;
229 c0 = c0 + *next_uuid++;
231 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 /* Calculate the value for "First octet" of the hash */
254 /* Calculate the value for "second octet" of the hash */
259 return ((y * 256) + x);
264 extern struct interfaceAddr afs_cb_interface;
266 static int uuid_get_address (uuid_address_p_t addr)
268 uuid_memcpy((void *)addr->eaddr, (void *)&afs_cb_interface.addr_in[0], 4);
269 addr->eaddr[4] = 0xaa;
270 addr->eaddr[5] = 0x77;
274 void uuid__get_os_time (uuid_time_t *os_time)
279 os_time->hi = tp.tv_sec;
280 os_time->lo = tp.tv_usec*10;
285 char hostName1[128] = "localhost";
286 static int uuid_get_address (uuid_address_p_t addr)
288 afs_int32 code, addr1;
291 code = gethostname(hostName1, 64);
293 printf("gethostname() failed\n");
300 he = gethostbyname(hostName1);
302 printf("Can't find address for '%s'\n", hostName1);
309 uuid_memcpy(&addr1, he->h_addr_list[0], 4);
310 addr1 = ntohl(addr1);
311 uuid_memcpy(addr->eaddr, &addr1, 4);
312 addr->eaddr[4] = 0xaa;
313 addr->eaddr[5] = 0x77;
315 printf ("uuid_get_address: %02x-%02x-%02x-%02x-%02x-%02x\n",
316 addr->eaddr[0], addr->eaddr[1], addr->eaddr[2],
317 addr->eaddr[3], addr->eaddr[4], addr->eaddr[5]);
323 void uuid__get_os_time (uuid_time_t *os_time)
327 if (gettimeofday (&tp, (struct timezone *) 0)) {
328 perror ("uuid__get_time");
331 os_time->hi = tp.tv_sec;
332 os_time->lo = tp.tv_usec*10;