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
10 /* String conversion routines have the following copyright */
13 * Copyright (c) 2002 Kungliga Tekniska Högskolan
14 * (Royal Institute of Technology, Stockholm, Sweden).
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * 3. Neither the name of the Institute nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 #include <afsconfig.h>
47 #include "afs/param.h"
49 #include <afs/param.h>
56 #include "afs/sysincludes.h"
57 #include "afsincludes.h"
58 #define uuid_memcmp(A,B,C) memcmp(A, B, C)
59 #define uuid_memcpy(A,B,C) memcpy(A, B, C)
69 #include <netinet/in.h>
71 #include <sys/ioctl.h>
72 #include <sys/socket.h>
75 #endif /* ITIMER_REAL */
84 #if !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV)
85 #include <netinet/if_ether.h>
89 #define uuid_memcmp(A,B,C) memcmp(A,B,C)
90 #define uuid_memcpy(A,B,C) memcpy(A,B,C)
95 char eaddr[6]; /* 6 bytes of ethernet hardware address */
96 } uuid_address_t, *uuid_address_p_t;
102 } uuid_time_t, *uuid_time_p_t;
104 static int uuid_get_address(uuid_address_p_t addr);
105 void uuid__get_os_time(uuid_time_t * os_time);
108 * |<------------------------- 32 bits -------------------------->|
110 * +--------------------------------------------------------------+
111 * | low 32 bits of time | 0-3 .time_low
112 * +-------------------------------+-------------------------------
113 * | mid 16 bits of time | 4-5 .time_mid
114 * +-------+-----------------------+
115 * | vers. | hi 12 bits of time | 6-7 .time_hi_and_version
116 * +-------+-------+---------------+
117 * |Res| clkSeqHi | 8 .clock_seq_hi_and_reserved
119 * | clkSeqLow | 9 .clock_seq_low
120 * +---------------+----------...-----+
121 * | node ID | 8-16 .node
122 * +--------------------------...-----+
125 afsUUID afs_uuid_g_nil_uuid = { 0 };
126 static uuid_time_t time_now, time_last;
127 static u_short uuid_time_adjust, clock_seq;
128 static afs_uint32 rand_m, rand_ia, rand_ib, rand_irand, uuid_init_done = 0;
130 #define uuid_create_nil(uuid) memset(uuid, 0, sizeof(afsUUID))
133 afs_uuid_equal(afsUUID * u1, afsUUID * u2)
135 return (uuid_memcmp((void *)u1, (void *)u2, sizeof(afsUUID)) == 0);
139 afs_uuid_is_nil(afsUUID * u1)
144 ((void *)u1, (void *)&afs_uuid_g_nil_uuid, sizeof(afsUUID)) == 0);
148 afs_htonuuid(afsUUID * uuidp)
150 uuidp->time_low = htonl(uuidp->time_low);
151 uuidp->time_mid = htons(uuidp->time_mid);
152 uuidp->time_hi_and_version = htons(uuidp->time_hi_and_version);
156 afs_ntohuuid(afsUUID * uuidp)
158 uuidp->time_low = ntohl(uuidp->time_low);
159 uuidp->time_mid = ntohs(uuidp->time_mid);
160 uuidp->time_hi_and_version = ntohs(uuidp->time_hi_and_version);
171 if (rand_ia >= 99991)
173 if (rand_ib >= 224729)
175 rand_irand = (rand_irand * rand_m) + rand_ia + rand_ib;
176 return (((rand_irand) >> 16) ^ (rand_irand & 0x3fff));
181 time_cmp(uuid_time_p_t time1, uuid_time_p_t time2)
183 if (time1->hi < time2->hi)
185 if (time1->hi > time2->hi)
187 if (time1->lo < time2->lo)
189 if (time1->lo > time2->lo)
195 * Converts a string UUID to binary representation.
198 #if !defined(KERNEL) && !defined(UKERNEL)
200 afsUUID_from_string(const char *str, afsUUID * uuid)
202 unsigned int time_low, time_mid, time_hi_and_version;
203 unsigned int clock_seq_hi_and_reserved, clock_seq_low;
204 unsigned int node[6];
207 i = sscanf(str, "%08x-%04x-%04x-%02x-%02x-%02x%02x%02x%02x%02x%02x",
208 &time_low, &time_mid, &time_hi_and_version,
209 &clock_seq_hi_and_reserved, &clock_seq_low, &node[0], &node[1],
210 &node[2], &node[3], &node[4], &node[5]);
214 uuid->time_low = time_low;
215 uuid->time_mid = time_mid;
216 uuid->time_hi_and_version = time_hi_and_version;
217 uuid->clock_seq_hi_and_reserved = clock_seq_hi_and_reserved;
218 uuid->clock_seq_low = clock_seq_low;
220 for (i = 0; i < 6; i++)
221 uuid->node[i] = node[i];
227 * Converts a UUID from binary representation to a string representation.
231 afsUUID_to_string(const afsUUID * uuid, char *str, size_t strsz)
233 snprintf(str, strsz, "%08x-%04x-%04x-%02x-%02x-%02x%02x%02x%02x%02x%02x",
234 uuid->time_low, uuid->time_mid, uuid->time_hi_and_version,
235 (unsigned char)uuid->clock_seq_hi_and_reserved,
236 (unsigned char)uuid->clock_seq_low, (unsigned char)uuid->node[0],
237 (unsigned char)uuid->node[1], (unsigned char)uuid->node[2],
238 (unsigned char)uuid->node[3], (unsigned char)uuid->node[4],
239 (unsigned char)uuid->node[5]);
246 afs_uuid_create(afsUUID * uuid)
248 uuid_address_t eaddr;
249 afs_int32 got_no_time = 0, code;
251 if (!uuid_init_done) {
253 u_short *seedp, seed = 0;
259 * Generating our 'seed' value
261 * We start with the current time, but, since the resolution of clocks is
262 * system hardware dependent (eg. Ultrix is 10 msec.) and most likely
263 * coarser than our resolution (10 usec) we 'mixup' the bits by xor'ing
264 * all the bits together. This will have the effect of involving all of
265 * the bits in the determination of the seed value while remaining system
266 * independent. Then for good measure to ensure a unique seed when there
267 * are multiple processes creating UUID's on a system, we add in the PID.
269 uuid__get_os_time(&t);
270 seedp = (u_short *) (&t);
275 #if defined(KERNEL) && defined(AFS_XBSD_ENV)
276 rand_irand += seed + (afs_uint32) curproc->p_pid;
278 rand_irand += seed + (afs_uint32) getpid();
280 uuid__get_os_time(&time_last);
281 clock_seq = true_random();
283 if (afs_winsockInit() < 0) {
284 return WSAGetLastError();
289 if ((code = uuid_get_address(&eaddr)))
290 return code; /* get our hardware network address */
292 /* get the current time */
293 uuid__get_os_time(&time_now);
295 * check that our clock hasn't gone backwards and handle it
296 * accordingly with clock_seq
297 * check that we're not generating uuid's faster than we
298 * can accommodate with our uuid_time_adjust fudge factor
300 if ((code = time_cmp(&time_now, &time_last)) == -1) {
301 /* A clock_seq value of 0 indicates that it hasn't been initialized. */
302 if (clock_seq == 0) {
303 clock_seq = true_random();
305 clock_seq = (clock_seq + 1) & 0x3fff;
307 clock_seq = clock_seq + 1;
308 uuid_time_adjust = 0;
309 } else if (code == 1) {
310 uuid_time_adjust = 0;
312 if (uuid_time_adjust == 0x7fff) /* spin while we wait for the clock to tick */
317 } while (got_no_time);
318 time_last.lo = time_now.lo;
319 time_last.hi = time_now.hi;
320 if (uuid_time_adjust != 0) {
321 if (time_now.lo & 0x80000000) {
322 time_now.lo += uuid_time_adjust;
323 if (!(time_now.lo & 0x80000000))
326 time_now.lo += uuid_time_adjust;
328 uuid->time_low = time_now.lo;
329 uuid->time_mid = time_now.hi & 0x0000ffff;
330 uuid->time_hi_and_version = (time_now.hi & 0x0fff0000) >> 16;
331 uuid->time_hi_and_version |= (1 << 12);
332 uuid->clock_seq_low = clock_seq & 0xff;
333 uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3f00) >> 8;
334 uuid->clock_seq_hi_and_reserved |= 0x80;
335 uuid_memcpy((void *)uuid->node, (void *)&eaddr, sizeof(uuid_address_t));
340 afs_uuid_hash(afsUUID * uuid)
342 short c0 = 0, c1 = 0, x, y;
343 char *next_uuid = (char *)uuid;
346 * For speed lets unroll the following loop:
348 * for (i = 0; i < UUID_K_LENGTH; i++)
350 * c0 = c0 + *next_uuid++;
354 c0 = c0 + *next_uuid++;
356 c0 = c0 + *next_uuid++;
358 c0 = c0 + *next_uuid++;
360 c0 = c0 + *next_uuid++;
362 c0 = c0 + *next_uuid++;
364 c0 = c0 + *next_uuid++;
366 c0 = c0 + *next_uuid++;
368 c0 = c0 + *next_uuid++;
370 c0 = c0 + *next_uuid++;
372 c0 = c0 + *next_uuid++;
374 c0 = c0 + *next_uuid++;
376 c0 = c0 + *next_uuid++;
378 c0 = c0 + *next_uuid++;
380 c0 = c0 + *next_uuid++;
382 c0 = c0 + *next_uuid++;
384 c0 = c0 + *next_uuid++;
386 /* Calculate the value for "First octet" of the hash */
391 /* Calculate the value for "second octet" of the hash */
396 return ((y * 256) + x);
401 extern struct interfaceAddr afs_cb_interface;
404 uuid_get_address(uuid_address_p_t addr)
406 uuid_memcpy((void *)addr->eaddr, (void *)&afs_cb_interface.addr_in[0], 4);
407 addr->eaddr[4] = 0xaa;
408 addr->eaddr[5] = 0x77;
413 uuid__get_os_time(uuid_time_t * os_time)
418 os_time->hi = tp.tv_sec;
419 os_time->lo = tp.tv_usec * 10;
424 char hostName1[128] = "localhost";
426 uuid_get_address(uuid_address_p_t addr)
432 code = gethostname(hostName1, 64);
434 printf("gethostname() failed\n");
441 he = gethostbyname(hostName1);
443 printf("Can't find address for '%s'\n", hostName1);
450 uuid_memcpy(&addr1, he->h_addr_list[0], 4);
451 addr1 = ntohl(addr1);
452 uuid_memcpy(addr->eaddr, &addr1, 4);
453 addr->eaddr[4] = 0xaa;
454 addr->eaddr[5] = 0x77;
456 printf("uuid_get_address: %02x-%02x-%02x-%02x-%02x-%02x\n",
457 addr->eaddr[0], addr->eaddr[1], addr->eaddr[2], addr->eaddr[3],
458 addr->eaddr[4], addr->eaddr[5]);
465 uuid__get_os_time(uuid_time_t * os_time)
469 if (gettimeofday(&tp, NULL)) {
470 perror("uuid__get_time");
473 os_time->hi = tp.tv_sec;
474 os_time->lo = tp.tv_usec * 10;