1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* The copyright in this file is taken from the original Leach
* & Salz UUID specification, from which this implementation
* is derived.
*/
/*
* Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
* Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
* Digital Equipment Corporation, Maynard, Mass. Copyright (c) 1998
* Microsoft. To anyone who acknowledges that this file is provided
* "AS IS" without any express or implied warranty: permission to use,
* copy, modify, and distribute this file for any purpose is hereby
* granted without fee, provided that the above copyright notices and
* this notice appears in all source code copies, and that none of the
* names of Open Software Foundation, Inc., Hewlett-Packard Company,
* or Digital Equipment Corporation be used in advertising or
* publicity pertaining to distribution of the software without
* specific, written prior permission. Neither Open Software
* Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital
* Equipment Corporation makes any representations about the
* suitability of this software for any purpose.
*/
#include <uuid/uuid.h>
#include <stdlib.h>
#include <strings.h>
#include "uuid_misc.h"
#define UUCMP(u1, u2) if (u1 != u2) return ((u1 < u2) ? -1 : 1)
#define UUIDS_PER_TOD_CALL 10 /* tv_usec is multiplied by 10 */
void struct_to_string(uuid_t, struct uuid *);
void string_to_struct(struct uuid *, uuid_t);
void get_system_time(uuid_time_t *);
/*
* Name: get_current_time
*
* Description: get-current_time -- get time as 60 bit 100ns ticks
* since the beginning of unix time.
* Compensate for the fact that real clock resolution is
* less than 100ns.
*
* Returns: None.
*
*/
void
get_current_time(uuid_time_t *timestamp)
{
uuid_time_t time_now;
static uuid_time_t time_last = 0;
static uint16_t uuids_this_tick = 0;
int done;
done = 0;
while (!done) {
get_system_time(&time_now);
/*
* if clock reading changed since last UUID generated...
*/
if (time_last != time_now) {
/*
* reset count of uuids generated with
* this clock reading
*/
uuids_this_tick = 0;
done = 1;
} else {
uuids_this_tick++;
if (uuids_this_tick < UUIDS_PER_TOD_CALL)
done = 1;
}
/*
* too many UUIDs for this gettimeofday call; spin
*/
}
time_last = time_now;
/*
* add the count of uuids to low order bits of the clock reading
*/
*timestamp = time_now + uuids_this_tick;
}
/*
* Name: get_random
*
* Description: Gets a random number.
*
* Returns: nbytes of random information.
*
*/
uint16_t
get_random(void)
{
static int initted = 0;
uuid_time_t time_now;
long seed;
if (!initted) {
get_system_time(&time_now);
time_now = time_now/UUIDS_PER_TOD_CALL;
seed = (unsigned)(((time_now >> 32) ^ time_now)&0xffffffff);
srand48(seed);
initted = 1;
}
return (mrand48());
}
/*
* Name: uuid_compare
*
* Description: Compares 2 uuid strings
*
* Returns: -1 if u1 < u2, 1 if u1 > u2 and 0 if both are equal
*/
int
uuid_compare(uuid_t uu1, uuid_t uu2)
{
struct uuid uuid1, uuid2;
string_to_struct(&uuid1, uu1);
string_to_struct(&uuid2, uu2);
UUCMP(uuid1.time_low, uuid2.time_low);
UUCMP(uuid1.time_mid, uuid2.time_mid);
UUCMP(uuid1.time_hi_and_version, uuid2.time_hi_and_version);
UUCMP(uuid1.clock_seq_hi_and_reserved, uuid2.clock_seq_hi_and_reserved);
UUCMP(uuid1.clock_seq_low, uuid2.clock_seq_low);
return (memcmp(uuid1.node_addr, uuid2.node_addr, 6));
}
/*
* Name: get_system_time
*
* Description: system dependent call to get the current system time.
* Returned as 100ns ticks since Oct 15, 1582, but
* resolution may be less than 100ns.
*
* Returns: None
*/
void
get_system_time(uuid_time_t *uuid_time)
{
struct timeval tp;
(void) gettimeofday(&tp, (struct timezone *)0);
/*
* Offset between UUID formatted times and Unix formatted times.
* UUID UTC base time is October 15, 1582.
* Unix base time is January 1, 1970.
*/
*uuid_time = (uint64_t)tp.tv_sec * 10000000;
*uuid_time += tp.tv_usec * 10;
*uuid_time += 0x01B21DD213814000ULL;
}
|