include-afsconfig-before-param-h-20010712
[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 #include <afsconfig.h>
11 #ifdef KERNEL
12 #include "../afs/param.h"
13 #else
14 #include <afs/param.h>
15 #endif
16
17 RCSID("$Header$");
18
19 #ifdef KERNEL
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)
24 #else /* KERNEL */
25 #include <stdio.h>
26 #include <errno.h>
27 #ifdef AFS_NT40_ENV
28 #include <winsock2.h>
29 #include <process.h>
30 #else
31 #include <sys/file.h>
32 #include <netinet/in.h>
33 #include <netdb.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #ifndef ITIMER_REAL
37 #include <sys/time.h>
38 #endif /* ITIMER_REAL */
39 #include <net/if.h>
40 #endif
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #if !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV)
44 #include <netinet/if_ether.h>
45 #endif
46 #include "afsutil.h"
47
48 #define uuid_memcmp(A,B,C)      memcmp(A,B,C)
49 #define uuid_memcpy(A,B,C)      memcpy(A,B,C)
50 #endif /* KERNEL */
51
52
53 typedef struct {
54     char eaddr[6];      /* 6 bytes of ethernet hardware address */
55 } uuid_address_t, *uuid_address_p_t;
56
57
58 typedef struct {
59     afs_uint32  lo;
60     afs_uint32  hi;
61 } uuid_time_t, *uuid_time_p_t;
62
63 static int uuid_get_address (uuid_address_p_t addr);
64 void uuid__get_os_time (uuid_time_t *os_time);
65
66 /*
67  * |<------------------------- 32 bits -------------------------->|
68  *
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
77  * +---------------+
78  * |   clkSeqLow   |  9                                 .clock_seq_low
79  * +---------------+----------...-----+
80  * |            node ID               |  8-16           .node
81  * +--------------------------...-----+
82  */
83
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;
88
89 #define uuid_create_nil(uuid) memset(uuid, 0, sizeof(afsUUID))
90 afs_int32 afs_uuid_equal(u1, u2) afsUUID *u1, *u2;  { return(uuid_memcmp((void *)u1, (void *)u2, sizeof (afsUUID)) == 0); }
91 afs_int32 afs_uuid_is_nil(u1) afsUUID *u1; { 
92     if (!u1) return 1;
93     return(uuid_memcmp((void *)u1, (void *)&afs_uuid_g_nil_uuid, sizeof (afsUUID)) == 0); 
94 }
95
96
97 void afs_htonuuid(uuidp)
98 afsUUID *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);
102 }
103
104 void afs_ntohuuid(uuidp)
105 afsUUID *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);
109 }
110  
111 static u_short true_random () {
112     rand_m += 7;
113     rand_ia += 1907;
114     rand_ib += 73939;
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));
120 }
121
122
123 static afs_int32 time_cmp (time1, time2)
124 uuid_time_p_t           time1;
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);
130     return (0);
131 }
132
133 afs_int32 afs_uuid_create (uuid)
134 afsUUID *uuid; {
135     uuid_address_t eaddr;
136     afs_int32 got_no_time = 0, code;
137
138     if (!uuid_init_done) {
139         uuid_time_t t;
140         u_short *seedp, seed=0;
141         rand_m = 971;;
142         rand_ia = 11113;
143         rand_ib = 104322;
144         rand_irand = 4181;
145         /*
146          * Generating our 'seed' value
147          *
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.
155          */
156         uuid__get_os_time(&t);
157         seedp = (u_short *)(&t);
158         seed ^= *seedp++;
159         seed ^= *seedp++;
160         seed ^= *seedp++;
161         seed ^= *seedp++;
162         rand_irand += seed + (afs_uint32)getpid();
163         uuid__get_os_time (&time_last);
164         clock_seq = true_random();
165 #ifdef AFS_NT40_ENV
166         if (afs_winsockInit()<0) {
167             return WSAGetLastError();
168         }
169 #endif  
170         uuid_init_done = 1;
171     }
172     if ((code = uuid_get_address (&eaddr))) return code;     /* get our hardware network address */
173     do {
174         /* get the current time */
175         uuid__get_os_time (&time_now);
176         /*
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
181          */
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();
186             }
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;
192         } else {
193             if (uuid_time_adjust == 0x7fff) /* spin while we wait for the clock to tick */
194                 got_no_time = 1;
195             else
196                 uuid_time_adjust++;
197         }
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++;
205         } else
206             time_now.lo += uuid_time_adjust;
207     }
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));
216     return 0;
217 }
218
219 u_short afs_uuid_hash (uuid)
220 afsUUID *uuid; {
221     short               c0=0, c1=0, x, y;
222     char             *next_uuid = (char *) uuid;
223
224     /*
225      * For speed lets unroll the following loop:
226      *
227      *   for (i = 0; i < UUID_K_LENGTH; i++)
228      *   {
229      *       c0 = c0 + *next_uuid++;
230      *       c1 = c1 + c0;
231      *   }
232      */
233     c0 = c0 + *next_uuid++;
234     c1 = c1 + c0;
235     c0 = c0 + *next_uuid++;
236     c1 = c1 + c0;
237     c0 = c0 + *next_uuid++;
238     c1 = c1 + c0;
239     c0 = c0 + *next_uuid++;
240     c1 = c1 + c0;
241     c0 = c0 + *next_uuid++;
242     c1 = c1 + c0;
243     c0 = c0 + *next_uuid++;
244     c1 = c1 + c0;
245     c0 = c0 + *next_uuid++;
246     c1 = c1 + c0;
247     c0 = c0 + *next_uuid++;
248     c1 = c1 + c0;
249     c0 = c0 + *next_uuid++;
250     c1 = c1 + c0;
251     c0 = c0 + *next_uuid++;
252     c1 = c1 + c0;
253     c0 = c0 + *next_uuid++;
254     c1 = c1 + c0;
255     c0 = c0 + *next_uuid++;
256     c1 = c1 + c0;
257     c0 = c0 + *next_uuid++;
258     c1 = c1 + c0;
259     c0 = c0 + *next_uuid++;
260     c1 = c1 + c0;
261     c0 = c0 + *next_uuid++;
262     c1 = c1 + c0;
263     c0 = c0 + *next_uuid++;
264     c1 = c1 + c0;
265     /*  Calculate the value for "First octet" of the hash  */
266     x = -c1 % 255;
267     if (x < 0) {
268         x = x + 255;
269     }
270     /*  Calculate the value for "second octet" of the hash */
271     y = (c1 - c0) % 255;
272     if (y < 0) {
273         y = y + 255;
274     }
275     return ((y * 256) + x);
276 }
277
278 #ifdef KERNEL
279
280 extern struct interfaceAddr afs_cb_interface;
281
282 static int uuid_get_address (uuid_address_p_t addr)
283 {
284     uuid_memcpy((void *)addr->eaddr, (void *)&afs_cb_interface.addr_in[0], 4);
285     addr->eaddr[4] = 0xaa;
286     addr->eaddr[5] = 0x77;
287     return 0;
288 }
289
290 void uuid__get_os_time (uuid_time_t *os_time)
291 {
292     struct timeval      tp;
293
294     osi_GetTime(&tp);
295     os_time->hi = tp.tv_sec;
296     os_time->lo = tp.tv_usec*10;
297 }
298
299 #else /* KERNEL */
300
301 char hostName1[128] = "localhost";
302 static int uuid_get_address (uuid_address_p_t addr)
303 {
304     afs_int32 code;
305     afs_uint32 addr1;
306     struct hostent *he;
307
308     code = gethostname(hostName1, 64);
309     if (code) {
310         printf("gethostname() failed\n");
311 #ifdef AFS_NT40_ENV
312         return ENOENT;
313 #else
314         return errno;
315 #endif
316     }
317     he = gethostbyname(hostName1);
318     if (!he) {
319         printf("Can't find address for '%s'\n", hostName1);
320 #ifdef AFS_NT40_ENV
321         return ENOENT;
322 #else
323         return errno;
324 #endif
325     } else {
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;
331 #ifdef  UUID_DEBUG
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]);
335 #endif
336     }
337     return 0;
338 }
339
340 void uuid__get_os_time (uuid_time_t *os_time)
341 {
342     struct timeval      tp;
343
344     if (gettimeofday (&tp, (struct timezone *) 0)) {
345         perror ("uuid__get_time");
346         exit (-1);
347     }
348     os_time->hi = tp.tv_sec;
349     os_time->lo = tp.tv_usec*10;
350 }
351
352 #endif /* KERNEL */