Remove the RCSID macro
[openafs.git] / src / util / uuid.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /* String conversion routines have the following copyright */
11
12 /*
13  * Copyright (c) 2002 Kungliga Tekniska Högskolan
14  * (Royal Institute of Technology, Stockholm, Sweden).
15  * All rights reserved.
16  * 
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 
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.
27  * 
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.
31  * 
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
42  * SUCH DAMAGE.
43  */
44
45 #include <afsconfig.h>
46 #ifdef KERNEL
47 #include "afs/param.h"
48 #else
49 #include <afs/param.h>
50 #endif
51
52
53 #ifdef KERNEL
54 #include "afs/sysincludes.h"
55 #include "afsincludes.h"
56 #define uuid_memcmp(A,B,C)      memcmp(A, B, C)
57 #define uuid_memcpy(A,B,C)      memcpy(A, B, C)
58 #else /* KERNEL */
59 #include <stdio.h>
60 #include <errno.h>
61 #include <string.h>
62 #ifdef AFS_NT40_ENV
63 #include <winsock2.h>
64 #include <process.h>
65 #else
66 #include <sys/file.h>
67 #include <netinet/in.h>
68 #include <netdb.h>
69 #include <sys/ioctl.h>
70 #include <sys/socket.h>
71 #ifndef ITIMER_REAL
72 #include <sys/time.h>
73 #endif /* ITIMER_REAL */
74 #include <net/if.h>
75 #ifdef HAVE_UNISTD_H
76 #include <unistd.h>
77 #endif
78 #include <stdlib.h>
79 #endif
80 #include <sys/stat.h>
81 #include <fcntl.h>
82 #if !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV)
83 #include <netinet/if_ether.h>
84 #endif
85 #include "afsutil.h"
86
87 #define uuid_memcmp(A,B,C)      memcmp(A,B,C)
88 #define uuid_memcpy(A,B,C)      memcpy(A,B,C)
89 #endif /* KERNEL */
90
91
92 typedef struct {
93     char eaddr[6];              /* 6 bytes of ethernet hardware address */
94 } uuid_address_t, *uuid_address_p_t;
95
96
97 typedef struct {
98     afs_uint32 lo;
99     afs_uint32 hi;
100 } uuid_time_t, *uuid_time_p_t;
101
102 static int uuid_get_address(uuid_address_p_t addr);
103 void uuid__get_os_time(uuid_time_t * os_time);
104
105 /*
106  * |<------------------------- 32 bits -------------------------->|
107  *
108  * +--------------------------------------------------------------+
109  * |                     low 32 bits of time                      |  0-3  .time_low
110  * +-------------------------------+-------------------------------
111  * |     mid 16 bits of time       |  4-5               .time_mid
112  * +-------+-----------------------+
113  * | vers. |   hi 12 bits of time  |  6-7               .time_hi_and_version
114  * +-------+-------+---------------+
115  * |Res|  clkSeqHi |  8                                 .clock_seq_hi_and_reserved
116  * +---------------+
117  * |   clkSeqLow   |  9                                 .clock_seq_low
118  * +---------------+----------...-----+
119  * |            node ID               |  8-16           .node
120  * +--------------------------...-----+
121  */
122
123 afsUUID afs_uuid_g_nil_uuid = { 0 };
124 static uuid_time_t time_now, time_last;
125 static u_short uuid_time_adjust, clock_seq;
126 static afs_uint32 rand_m, rand_ia, rand_ib, rand_irand, uuid_init_done = 0;
127
128 #define uuid_create_nil(uuid) memset(uuid, 0, sizeof(afsUUID))
129
130 afs_int32
131 afs_uuid_equal(afsUUID * u1, afsUUID * u2)
132 {
133     return (uuid_memcmp((void *)u1, (void *)u2, sizeof(afsUUID)) == 0);
134 }
135
136 afs_int32
137 afs_uuid_is_nil(afsUUID * u1)
138 {
139     if (!u1)
140         return 1;
141     return (uuid_memcmp
142             ((void *)u1, (void *)&afs_uuid_g_nil_uuid, sizeof(afsUUID)) == 0);
143 }
144
145 void
146 afs_htonuuid(afsUUID * uuidp)
147 {
148     uuidp->time_low = htonl(uuidp->time_low);
149     uuidp->time_mid = htons(uuidp->time_mid);
150     uuidp->time_hi_and_version = htons(uuidp->time_hi_and_version);
151 }
152
153 void
154 afs_ntohuuid(afsUUID * uuidp)
155 {
156     uuidp->time_low = ntohl(uuidp->time_low);
157     uuidp->time_mid = ntohs(uuidp->time_mid);
158     uuidp->time_hi_and_version = ntohs(uuidp->time_hi_and_version);
159 }
160
161 static u_short
162 true_random(void)
163 {
164     rand_m += 7;
165     rand_ia += 1907;
166     rand_ib += 73939;
167     if (rand_m >= 9973)
168         rand_m -= 9871;
169     if (rand_ia >= 99991)
170         rand_ia -= 89989;
171     if (rand_ib >= 224729)
172         rand_ib -= 96233;
173     rand_irand = (rand_irand * rand_m) + rand_ia + rand_ib;
174     return (((rand_irand) >> 16) ^ (rand_irand & 0x3fff));
175 }
176
177
178 static afs_int32
179 time_cmp(uuid_time_p_t time1, uuid_time_p_t time2)
180 {
181     if (time1->hi < time2->hi)
182         return (-1);
183     if (time1->hi > time2->hi)
184         return (1);
185     if (time1->lo < time2->lo)
186         return (-1);
187     if (time1->lo > time2->lo)
188         return (1);
189     return (0);
190 }
191
192 /*
193  *    Converts a string UUID to binary representation.
194  */
195
196 #if !defined(KERNEL) && !defined(UKERNEL)
197 int
198 afsUUID_from_string(const char *str, afsUUID * uuid)
199 {
200     unsigned int time_low, time_mid, time_hi_and_version;
201     unsigned int clock_seq_hi_and_reserved, clock_seq_low;
202     unsigned int node[6];
203     int i;
204
205     i = sscanf(str, "%08x-%04x-%04x-%02x-%02x-%02x%02x%02x%02x%02x%02x",
206                &time_low, &time_mid, &time_hi_and_version,
207                &clock_seq_hi_and_reserved, &clock_seq_low, &node[0], &node[1],
208                &node[2], &node[3], &node[4], &node[5]);
209     if (i != 11)
210         return -1;
211
212     uuid->time_low = time_low;
213     uuid->time_mid = time_mid;
214     uuid->time_hi_and_version = time_hi_and_version;
215     uuid->clock_seq_hi_and_reserved = clock_seq_hi_and_reserved;
216     uuid->clock_seq_low = clock_seq_low;
217
218     for (i = 0; i < 6; i++)
219         uuid->node[i] = node[i];
220
221     return 0;
222 }
223
224 /*
225  *    Converts a UUID from binary representation to a string representation.
226  */
227
228 int
229 afsUUID_to_string(const afsUUID * uuid, char *str, size_t strsz)
230 {
231     snprintf(str, strsz, "%08x-%04x-%04x-%02x-%02x-%02x%02x%02x%02x%02x%02x",
232              uuid->time_low, uuid->time_mid, uuid->time_hi_and_version,
233              (unsigned char)uuid->clock_seq_hi_and_reserved,
234              (unsigned char)uuid->clock_seq_low, (unsigned char)uuid->node[0],
235              (unsigned char)uuid->node[1], (unsigned char)uuid->node[2],
236              (unsigned char)uuid->node[3], (unsigned char)uuid->node[4],
237              (unsigned char)uuid->node[5]);
238
239     return 0;
240 }
241 #endif
242
243 afs_int32
244 afs_uuid_create(afsUUID * uuid)
245 {
246     uuid_address_t eaddr;
247     afs_int32 got_no_time = 0, code;
248
249     if (!uuid_init_done) {
250         uuid_time_t t;
251         u_short *seedp, seed = 0;
252         rand_m = 971;;
253         rand_ia = 11113;
254         rand_ib = 104322;
255         rand_irand = 4181;
256         /*
257          * Generating our 'seed' value
258          *
259          * We start with the current time, but, since the resolution of clocks is
260          * system hardware dependent (eg. Ultrix is 10 msec.) and most likely
261          * coarser than our resolution (10 usec) we 'mixup' the bits by xor'ing
262          * all the bits together.  This will have the effect of involving all of
263          * the bits in the determination of the seed value while remaining system
264          * independent.  Then for good measure to ensure a unique seed when there
265          * are multiple processes creating UUID's on a system, we add in the PID.
266          */
267         uuid__get_os_time(&t);
268         seedp = (u_short *) (&t);
269         seed ^= *seedp++;
270         seed ^= *seedp++;
271         seed ^= *seedp++;
272         seed ^= *seedp++;
273 #if defined(KERNEL) && defined(AFS_XBSD_ENV)
274         rand_irand += seed + (afs_uint32) curproc->p_pid;
275 #else
276         rand_irand += seed + (afs_uint32) getpid();
277 #endif
278         uuid__get_os_time(&time_last);
279         clock_seq = true_random();
280 #ifdef AFS_NT40_ENV
281         if (afs_winsockInit() < 0) {
282             return WSAGetLastError();
283         }
284 #endif
285         uuid_init_done = 1;
286     }
287     if ((code = uuid_get_address(&eaddr)))
288         return code;            /* get our hardware network address */
289     do {
290         /* get the current time */
291         uuid__get_os_time(&time_now);
292         /*
293          * check that our clock hasn't gone backwards and handle it
294          *    accordingly with clock_seq
295          * check that we're not generating uuid's faster than we
296          *    can accommodate with our uuid_time_adjust fudge factor
297          */
298         if ((code = time_cmp(&time_now, &time_last)) == -1) {
299             /* A clock_seq value of 0 indicates that it hasn't been initialized. */
300             if (clock_seq == 0) {
301                 clock_seq = true_random();
302             }
303             clock_seq = (clock_seq + 1) & 0x3fff;
304             if (clock_seq == 0)
305                 clock_seq = clock_seq + 1;
306             uuid_time_adjust = 0;
307         } else if (code == 1) {
308             uuid_time_adjust = 0;
309         } else {
310             if (uuid_time_adjust == 0x7fff)     /* spin while we wait for the clock to tick */
311                 got_no_time = 1;
312             else
313                 uuid_time_adjust++;
314         }
315     } while (got_no_time);
316     time_last.lo = time_now.lo;
317     time_last.hi = time_now.hi;
318     if (uuid_time_adjust != 0) {
319         if (time_now.lo & 0x80000000) {
320             time_now.lo += uuid_time_adjust;
321             if (!(time_now.lo & 0x80000000))
322                 time_now.hi++;
323         } else
324             time_now.lo += uuid_time_adjust;
325     }
326     uuid->time_low = time_now.lo;
327     uuid->time_mid = time_now.hi & 0x0000ffff;
328     uuid->time_hi_and_version = (time_now.hi & 0x0fff0000) >> 16;
329     uuid->time_hi_and_version |= (1 << 12);
330     uuid->clock_seq_low = clock_seq & 0xff;
331     uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3f00) >> 8;
332     uuid->clock_seq_hi_and_reserved |= 0x80;
333     uuid_memcpy((void *)uuid->node, (void *)&eaddr, sizeof(uuid_address_t));
334     return 0;
335 }
336
337 u_short
338 afs_uuid_hash(afsUUID * uuid)
339 {
340     short c0 = 0, c1 = 0, x, y;
341     char *next_uuid = (char *)uuid;
342
343     /*
344      * For speed lets unroll the following loop:
345      *
346      *   for (i = 0; i < UUID_K_LENGTH; i++)
347      *   {
348      *       c0 = c0 + *next_uuid++;
349      *       c1 = c1 + c0;
350      *   }
351      */
352     c0 = c0 + *next_uuid++;
353     c1 = c1 + c0;
354     c0 = c0 + *next_uuid++;
355     c1 = c1 + c0;
356     c0 = c0 + *next_uuid++;
357     c1 = c1 + c0;
358     c0 = c0 + *next_uuid++;
359     c1 = c1 + c0;
360     c0 = c0 + *next_uuid++;
361     c1 = c1 + c0;
362     c0 = c0 + *next_uuid++;
363     c1 = c1 + c0;
364     c0 = c0 + *next_uuid++;
365     c1 = c1 + c0;
366     c0 = c0 + *next_uuid++;
367     c1 = c1 + c0;
368     c0 = c0 + *next_uuid++;
369     c1 = c1 + c0;
370     c0 = c0 + *next_uuid++;
371     c1 = c1 + c0;
372     c0 = c0 + *next_uuid++;
373     c1 = c1 + c0;
374     c0 = c0 + *next_uuid++;
375     c1 = c1 + c0;
376     c0 = c0 + *next_uuid++;
377     c1 = c1 + c0;
378     c0 = c0 + *next_uuid++;
379     c1 = c1 + c0;
380     c0 = c0 + *next_uuid++;
381     c1 = c1 + c0;
382     c0 = c0 + *next_uuid++;
383     c1 = c1 + c0;
384     /*  Calculate the value for "First octet" of the hash  */
385     x = -c1 % 255;
386     if (x < 0) {
387         x = x + 255;
388     }
389     /*  Calculate the value for "second octet" of the hash */
390     y = (c1 - c0) % 255;
391     if (y < 0) {
392         y = y + 255;
393     }
394     return ((y * 256) + x);
395 }
396
397 #ifdef KERNEL
398
399 extern struct interfaceAddr afs_cb_interface;
400
401 static int
402 uuid_get_address(uuid_address_p_t addr)
403 {
404     uuid_memcpy((void *)addr->eaddr, (void *)&afs_cb_interface.addr_in[0], 4);
405     addr->eaddr[4] = 0xaa;
406     addr->eaddr[5] = 0x77;
407     return 0;
408 }
409
410 void
411 uuid__get_os_time(uuid_time_t * os_time)
412 {
413     osi_timeval_t tp;
414
415     osi_GetTime(&tp);
416     os_time->hi = tp.tv_sec;
417     os_time->lo = tp.tv_usec * 10;
418 }
419
420 #else /* KERNEL */
421
422 char hostName1[128] = "localhost";
423 static int
424 uuid_get_address(uuid_address_p_t addr)
425 {
426     afs_int32 code;
427     afs_uint32 addr1;
428     struct hostent *he;
429
430     code = gethostname(hostName1, 64);
431     if (code) {
432         printf("gethostname() failed\n");
433 #ifdef AFS_NT40_ENV
434         return ENOENT;
435 #else
436         return errno;
437 #endif
438     }
439     he = gethostbyname(hostName1);
440     if (!he) {
441         printf("Can't find address for '%s'\n", hostName1);
442 #ifdef AFS_NT40_ENV
443         return ENOENT;
444 #else
445         return errno;
446 #endif
447     } else {
448         uuid_memcpy(&addr1, he->h_addr_list[0], 4);
449         addr1 = ntohl(addr1);
450         uuid_memcpy(addr->eaddr, &addr1, 4);
451         addr->eaddr[4] = 0xaa;
452         addr->eaddr[5] = 0x77;
453 #ifdef  UUID_DEBUG
454         printf("uuid_get_address: %02x-%02x-%02x-%02x-%02x-%02x\n",
455                addr->eaddr[0], addr->eaddr[1], addr->eaddr[2], addr->eaddr[3],
456                addr->eaddr[4], addr->eaddr[5]);
457 #endif
458     }
459     return 0;
460 }
461
462 void
463 uuid__get_os_time(uuid_time_t * os_time)
464 {
465     struct timeval tp;
466
467     if (gettimeofday(&tp, NULL)) {
468         perror("uuid__get_time");
469         exit(-1);
470     }
471     os_time->hi = tp.tv_sec;
472     os_time->lo = tp.tv_usec * 10;
473 }
474
475 #endif /* KERNEL */