]> git.sur5r.net Git - openldap/blob - doc/drafts/draft-leach-uuids-guids-xx.txt
Sync with HEAD
[openldap] / doc / drafts / draft-leach-uuids-guids-xx.txt
1
2
3
4
5
6
7 Network Working Group               Paul J. Leach, Microsoft
8 INTERNET-DRAFT                             Rich Salz, Certco
9 <draft-leach-uuids-guids-01.txt>
10 Category: Standards Track
11 Expires August 4, 1998                      February 4, 1998
12
13
14
15                              UUIDs and GUIDs
16
17 STATUS OF THIS MEMO
18
19   This document is an Internet-Draft. Internet-Drafts are working
20   documents of the Internet Engineering Task Force (IETF), its areas,
21   and its working groups. Note that other groups may also distribute
22   working documents as Internet-Drafts.
23
24   Internet-Drafts are draft documents valid for a maximum of six months
25   and may be updated, replaced, or obsoleted by other documents at any
26   time. It is inappropriate to use Internet-Drafts as reference
27   material or to cite them other than as "work in progress".
28
29   To learn the current status of any Internet-Draft, please check the
30   "1id-abstracts.txt" listing contained in the Internet-Drafts Shadow
31   Directories on ftp.is.co.za (Africa), nic.nordu.net (Europe),
32   munnari.oz.au (Pacific Rim), ds.internic.net (US East Coast), or
33   ftp.isi.edu (US West Coast).
34
35   Distribution of this document is unlimited.  Please send comments to
36   the authors or the CIFS mailing list at <cifs@discuss.microsoft.com>.
37   Discussions of the mailing list are archived at
38   <URL:http://discuss.microsoft.com/archives/index.
39
40
41 ABSTRACT
42
43   This specification defines the format of UUIDs (Universally Unique
44   IDentifier), also known as GUIDs (Globally Unique IDentifier). A UUID
45   is 128 bits long, and if generated according to the one of the
46   mechanisms in this document, is either guaranteed to be different
47   from all other UUIDs/GUIDs generated until 3400 A.D. or extremely
48   likely to be different (depending on the mechanism chosen). UUIDs
49   were originally used in the Network Computing System (NCS) [1] and
50   later in the Open Software Foundation's (OSF) Distributed Computing
51   Environment [2].
52
53   This specification is derived from the latter specification with the
54   kind permission of the OSF.
55
56
57 Table of Contents
58
59 1. Introduction .......................................................3
60
61
62 [Page 1]\f
63
64
65   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
66
67
68 2. Motivation .........................................................3
69
70 3. Specification ......................................................3
71
72  3.1 Format............................................................4
73
74    3.1.1  Variant......................................................4
75
76    3.1.2  UUID layout..................................................5
77
78    3.1.3  Version......................................................5
79
80    3.1.4  Timestamp....................................................6
81
82    3.1.5  Clock sequence...............................................6
83
84    3.1.6  Node.........................................................7
85
86    3.1.7  Nil UUID.....................................................7
87
88  3.2 Algorithms for creating a time-based UUID.........................7
89
90    3.2.1  Basic algorithm..............................................7
91
92    3.2.2  Reading stable storage.......................................8
93
94    3.2.3  System clock resolution......................................8
95
96    3.2.4  Writing stable storage.......................................9
97
98    3.2.5  Sharing state across processes...............................9
99
100    3.2.6  UUID Generation details......................................9
101
102  3.3 Algorithm for creating a name-based UUID.........................10
103
104  3.4 Algorithms for creating a UUID from truly random or pseudo-random
105  numbers .............................................................11
106
107  3.5 String Representation of UUIDs...................................12
108
109  3.6 Comparing UUIDs for equality.....................................12
110
111  3.7 Comparing UUIDs for relative order...............................13
112
113  3.8 Byte order of UUIDs..............................................13
114
115 4. Node IDs when no IEEE 802 network card is available ...............14
116
117 5. Obtaining IEEE 802 addresses ......................................15
118
119 6. Security Considerations ...........................................15
120
121 7. Acknowledgements ..................................................15
122
123   Leach, Salz              expires  Aug 1998                    [Page 2]\f
124
125
126   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
127
128
129 8. References ........................................................15
130
131 9. Authors' addresses ................................................16
132
133 10.Notice ............................................................16
134
135 11.Full Copyright Statement ..........................................16
136
137 Appendix A _ UUID Sample Implementation...............................17
138
139 Appendix B _ Sample output of utest...................................27
140
141 Appendix C _ Some name space IDs......................................27
142
143
144
145
146 1. Introduction
147
148   This specification defines the format of UUIDs (Universally Unique
149   IDentifiers), also known as GUIDs (Globally Unique IDentifiers). A
150   UUID is 128 bits long, and if generated according to the one of the
151   mechanisms in this document, is either guaranteed to be different
152   from all other UUIDs/GUIDs generated until 3400 A.D. or extremely
153   likely to be different (depending on the mechanism chosen).
154
155
156 2. Motivation
157
158   One of the main reasons for using UUIDs is that no centralized
159   authority is required to administer them (beyond the one that
160   allocates IEEE 802.1 node identifiers). As a result, generation on
161   demand can be completely automated, and they can be used for a wide
162   variety of purposes. The UUID generation algorithm described here
163   supports very high allocation rates: 10 million per second per
164   machine if you need it, so that they could even be used as
165   transaction IDs.
166
167   UUIDs are fixed-size (128-bits) which is reasonably small relative to
168   other alternatives. This fixed, relatively small size lends itself
169   well to sorting, ordering, and hashing of all sorts, storing in
170   databases, simple allocation, and ease of programming in general.
171
172
173 3. Specification
174
175   A UUID is an identifier that is unique across both space and time,
176   with respect to the space of all UUIDs. To be precise, the UUID
177   consists of a finite bit space. Thus the time value used for
178   constructing a UUID is limited and will roll over in the future
179   (approximately at A.D. 3400, based on the specified algorithm). A
180   UUID can be used for multiple purposes, from tagging objects with an
181   extremely short lifetime, to reliably identifying very persistent
182   objects across a network.
183
184   Leach, Salz              expires  Aug 1998                    [Page 3]\f
185
186
187   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
188
189
190   The generation of UUIDs does not require that a registration
191   authority be contacted for each identifier. Instead, it requires a
192   unique value over space for each UUID generator. This spatially
193   unique value is specified as an IEEE 802 address, which is usually
194   already available to network-connected systems. This 48-bit address
195   can be assigned based on an address block obtained through the IEEE
196   registration authority. This section of the UUID specification
197   assumes the availability of an IEEE 802 address to a system desiring
198   to generate a UUID, but if one is not available section 4 specifies a
199   way to generate a probabilistically unique one that can not conflict
200   with any properly assigned IEEE 802 address.
201
202
203 3.1 Format
204
205   In its most general form, all that can be said of the UUID format is
206   that a UUID is 16 octets, and that some bits of octet 8 of the UUID
207   called the variant field (specified in the next section) determine
208   finer structure.
209
210
211 3.1.1 Variant
212   The variant field determines the layout of the UUID. That is, the
213   interpretation of all other bits in the UUID depends on the setting
214   of the bits in the variant field. The variant field consists of a
215   variable number of the msbs of octet 8 of the UUID.
216
217   The following table lists the contents of the variant field.
218
219        Msb0  Msb1   Msb2  Description
220
221         0      -     -    Reserved, NCS backward compatibility.
222
223         1      0     -    The variant specified in this document.
224
225         1      1     0    Reserved, Microsoft Corporation backward
226                           compatibility
227
228         1      1     1    Reserved for future definition.
229
230
231
232   Other UUID variants may not interoperate with the UUID variant
233   specified in this document, where interoperability is defined as the
234   applicability of operations such as string conversion and lexical
235   ordering across different systems. However, UUIDs allocated according
236   to the stricture of different variants, though they may define
237   different interpretations of the bits outside the variant field, will
238   not result in duplicate UUID allocation, because of the differing
239   values of the variant field itself.
240
241   The remaining fields described below (version, timestamp, etc.) are
242   defined only for the UUID variant noted above.
243
244
245   Leach, Salz              expires  Aug 1998                    [Page 4]\f
246
247
248   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
249
250
251 3.1.2 UUID layout
252   The following table gives the format of a UUID for the variant
253   specified herein. The UUID consists of a record of 16 octets. To
254   minimize confusion about bit assignments within octets, the UUID
255   record definition is defined only in terms of fields that are
256   integral numbers of octets. The fields are in order of significance
257   for comparison purposes, with "time_low" the most significant, and
258   "node" the least significant.
259
260    Field                  Data Type     Octet  Note
261                                         #
262
263    time_low               unsigned 32   0-3    The low field of the
264                           bit integer          timestamp.
265
266    time_mid               unsigned 16   4-5    The middle field of the
267                           bit integer          timestamp.
268
269    time_hi_and_version    unsigned 16   6-7    The high field of the
270                           bit integer          timestamp multiplexed
271                                                with the version number.
272
273    clock_seq_hi_and_rese  unsigned 8    8      The high field of the
274    rved                   bit integer          clock sequence
275                                                multiplexed with the
276                                                variant.
277
278    clock_seq_low          unsigned 8    9      The low field of the
279                           bit integer          clock sequence.
280
281    node                   unsigned 48   10-15  The spatially unique
282                           bit integer          node identifier.
283
284
285
286
287 3.1.3 Version
288   The version number is in the most significant 4 bits of the time
289   stamp (time_hi_and_version).
290
291   The following table lists currently defined versions of the UUID.
292
293        Msb0  Msb1   Msb2  Msb3   Version  Description
294
295         0      0     0      1       1     The time-based version
296                                           specified in this
297                                           document.
298
299         0      0     1      0       2     Reserved for DCE
300                                           Security version, with
301                                           embedded POSIX UIDs.
302
303         0      0     1      1       3     The name-based version
304                                           specified in this
305
306   Leach, Salz              expires  Aug 1998                    [Page 5]\f
307
308
309   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
310
311
312                                           document
313
314         0      1     0      0       4     The randomly or pseudo-
315                                           randomly generated
316                                           version specified in
317                                           this document
318
319
320 3.1.4 Timestamp
321   The timestamp is a 60 bit value. For UUID version 1, this is
322   represented by Coordinated Universal Time (UTC) as a count of 100-
323   nanosecond intervals since 00:00:00.00, 15 October 1582 (the date of
324   Gregorian reform to the Christian calendar).
325
326   For systems that do not have UTC available, but do have local time,
327   they MAY use local time instead of UTC, as long as they do so
328   consistently throughout the system. This is NOT RECOMMENDED, however,
329   and it should be noted that all that is needed to generate UTC, given
330   local time, is a time zone offset.
331
332   For UUID version 3, it is a 60 bit value constructed from a name.
333
334   For UUID version 4, it is a randomly or pseudo-randomly generated 60
335   bit value.
336
337
338 3.1.5 Clock sequence
339   For UUID version 1, the clock sequence is used to help avoid
340   duplicates that could arise when the clock is set backwards in time
341   or if the node ID changes.
342
343   If the clock is set backwards, or even might have been set backwards
344   (e.g., while the system was powered off), and the UUID generator can
345   not be sure that no UUIDs were generated with timestamps larger than
346   the value to which the clock was set, then the clock sequence has to
347   be changed. If the previous value of the clock sequence is known, it
348   can be just incremented; otherwise it should be set to a random or
349   high-quality pseudo random value.
350
351   Similarly, if the node ID changes (e.g. because a network card has
352   been moved between machines), setting the clock sequence to a random
353   number minimizes the probability of a duplicate due to slight
354   differences in the clock settings of the machines. (If the value of
355   clock sequence associated with the changed node ID were known, then
356   the clock sequence could just be incremented, but that is unlikely.)
357
358   The clock sequence MUST be originally (i.e., once in the lifetime of
359   a system) initialized to a random number to minimize the correlation
360   across systems. This provides maximum protection against node
361   identifiers that may move or switch from system to system rapidly.
362   The initial value MUST NOT be correlated to the node identifier.
363
364   For UUID version 3, it is a 14 bit value constructed from a name.
365
366
367   Leach, Salz              expires  Aug 1998                    [Page 6]\f
368
369
370   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
371
372
373   For UUID version 4, it is a randomly or pseudo-randomly generated 14
374   bit value.
375
376
377 3.1.6 Node
378   For UUID version 1, the node field consists of the IEEE address,
379   usually the host address. For systems with multiple IEEE 802
380   addresses, any available address can be used. The lowest addressed
381   octet (octet number 10) contains the global/local bit and the
382   unicast/multicast bit, and is the first octet of the address
383   transmitted on an 802.3 LAN.
384
385   For systems with no IEEE address, a randomly or pseudo-randomly
386   generated value may be used (see section 4). The multicast bit must
387   be set in such addresses, in order that they will never conflict with
388   addresses obtained from network cards.
389
390   For UUID version 3, the node field is a 48 bit value constructed from
391   a name.
392
393   For UUID version 4, the node field is a randomly or pseudo-randomly
394   generated 48 bit value.
395
396
397 3.1.7 Nil UUID
398   The nil UUID is special form of UUID that is specified to have all
399   128 bits set to 0 (zero).
400
401
402 3.2 Algorithms for creating a time-based UUID
403
404   Various aspects of the algorithm for creating a version 1 UUID are
405   discussed in the following sections. UUID generation requires a
406   guarantee of uniqueness within the node ID for a given variant and
407   version. Interoperability is provided by complying with the specified
408   data structure.
409
410
411 3.2.1 Basic algorithm
412   The following algorithm is simple, correct, and inefficient:
413
414   .  Obtain a system wide global lock
415
416   .  From a system wide shared stable store (e.g., a file), read the
417      UUID generator state: the values of the time stamp, clock sequence,
418      and node ID used to generate the last UUID.
419
420   .  Get the current time as a 60 bit count of 100-nanosecond intervals
421      since 00:00:00.00, 15 October 1582
422
423   .  Get the current node ID
424
425
426
427
428   Leach, Salz              expires  Aug 1998                    [Page 7]\f
429
430
431   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
432
433
434   .  If the state was unavailable (non-existent or corrupted), or the
435      saved node ID is different than the current node ID, generate a
436      random clock sequence value
437
438   .  If the state was available, but the saved time stamp is later than
439      the current time stamp, increment the clock sequence value
440
441   .  Format a UUID from the current time stamp, clock sequence, and node
442      ID values according to the structure in section 3.1 (see section
443      3.2.6 for more details)
444
445   .  Save the state (current time stamp, clock sequence, and node ID)
446      back to the stable store
447
448   .  Release the system wide global lock
449
450   If UUIDs do not need to be frequently generated, the above algorithm
451   may be perfectly adequate. For higher performance requirements,
452   however, issues with the basic algorithm include:
453
454   .  Reading the state from stable storage each time is inefficient
455
456   .  The resolution of the system clock may not be 100-nanoseconds
457
458   .  Writing the state to stable storage each time is inefficient
459
460   .  Sharing the state across process boundaries may be inefficient
461
462   Each of these issues can be addressed in a modular fashion by local
463   improvements in the functions that read and write the state and read
464   the clock. We address each of them in turn in the following sections.
465
466
467 3.2.2 Reading stable storage
468   The state only needs to be read from stable storage once at boot
469   time, if it is read into a system wide shared volatile store (and
470   updated whenever the stable store is updated).
471
472   If an implementation does not have any stable store available, then
473   it can always say that the values were unavailable. This is the least
474   desirable implementation, because it will increase the frequency of
475   creation of new clock sequence numbers, which increases the
476   probability of duplicates.
477
478   If the node ID can never change (e.g., the net card is inseparable
479   from the system), or if any change also reinitializes the clock
480   sequence to a random value, then instead of keeping it in stable
481   store, the current node ID may be returned.
482
483
484 3.2.3 System clock resolution
485   The time stamp is generated from the system time, whose resolution
486   may be less than the resolution of the UUID time stamp.
487
488
489   Leach, Salz              expires  Aug 1998                    [Page 8]\f
490
491
492   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
493
494
495   If UUIDs do not need to be frequently generated, the time stamp can
496   simply be the system time multiplied by the number of 100-nanosecond
497   intervals per system time interval.
498
499   If a system overruns the generator by requesting too many UUIDs
500   within a single system time interval, the UUID service MUST either:
501   return an error, or stall the UUID generator until the system clock
502   catches up.
503
504   A high resolution time stamp can be simulated by keeping a count of
505   how many UUIDs have been generated with the same value of the system
506   time, and using it to construction the low-order bits of the time
507   stamp. The count will range between zero and the number of 100-
508   nanosecond intervals per system time interval.
509
510   Note: if the processors overrun the UUID generation frequently,
511   additional node identifiers can be allocated to the system, which
512   will permit higher speed allocation by making multiple UUIDs
513   potentially available for each time stamp value.
514
515
516 3.2.4 Writing stable storage
517   The state does not always need to be written to stable store every
518   time a UUID is generated. The timestamp in the stable store can be
519   periodically set to a value larger than any yet used in a UUID; as
520   long as the generated UUIDs have time stamps less than that value,
521   and the clock sequence and node ID remain unchanged, only the shared
522   volatile copy of the state needs to be updated. Furthermore, if the
523   time stamp value in stable store is in the future by less than the
524   typical time it takes the system to reboot, a crash will not cause a
525   reinitialization of the clock sequence.
526
527
528 3.2.5 Sharing state across processes
529   If it is too expensive to access shared state each time a UUID is
530   generated, then the system wide generator can be implemented to
531   allocate a block of time stamps each time it is called, and a per-
532   process generator can allocate from that block until it is exhausted.
533
534
535 3.2.6 UUID Generation details
536   UUIDs are generated according to the following algorithm:
537
538   - Determine the values for the UTC-based timestamp and clock sequence
539   to be used in the UUID, as described above.
540
541   - For the purposes of this algorithm, consider the timestamp to be a
542   60-bit unsigned integer and the clock sequence to be a 14-bit
543   unsigned integer. Sequentially number the bits in a field, starting
544   from 0 (zero) for the least significant bit.
545
546   - Set the time_low field equal to the least significant 32-bits (bits
547   numbered 0 to 31 inclusive) of the time stamp in the same order of
548   significance.
549
550   Leach, Salz              expires  Aug 1998                    [Page 9]\f
551
552
553   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
554
555
556   - Set the time_mid field equal to the bits numbered 32 to 47
557   inclusive of the time stamp in the same order of significance.
558
559   - Set the 12 least significant bits (bits numbered 0 to 11 inclusive)
560   of the time_hi_and_version field equal to the bits numbered 48 to 59
561   inclusive of the time stamp in the same order of significance.
562
563   - Set the 4 most significant bits (bits numbered 12 to 15 inclusive)
564   of the time_hi_and_version field to the 4-bit version number
565   corresponding to the UUID version being created, as shown in the
566   table in section 3.1.3.
567
568   - Set the clock_seq_low field to the 8 least significant bits (bits
569   numbered 0 to 7 inclusive) of the clock sequence in the same order of
570   significance.
571
572   - Set the 6 least significant bits (bits numbered 0 to 5 inclusive)
573   of the clock_seq_hi_and_reserved field to the 6 most significant bits
574   (bits numbered 8 to 13 inclusive) of the clock sequence in the same
575   order of significance.
576
577   - Set the 2 most significant bits (bits numbered 6 and 7) of the
578   clock_seq_hi_and_reserved to 0 and 1, respectively.
579
580   - Set the node field to the 48-bit IEEE address in the same order of
581   significance as the address.
582
583
584 3.3 Algorithm for creating a name-based UUID
585
586   The version 3 UUID is meant for generating UUIDs from "names" that
587   are drawn from, and unique within, some "name space". Some examples
588   of names (and, implicitly, name spaces) might be DNS names, URLs, ISO
589   Object IDs (OIDs), reserved words in a programming language, or X.500
590   Distinguished Names (DNs); thus, the concept of name and name space
591   should be broadly construed, and not limited to textual names. The
592   mechanisms or conventions for allocating names from, and ensuring
593   their uniqueness within, their name spaces are beyond the scope of
594   this specification.
595
596   The requirements for such UUIDs are as follows:
597
598   .  The UUIDs generated at different times from the same name in the
599      same namespace MUST be equal
600
601   .  The UUIDs generated from two different names in the same namespace
602      should be different (with very high probability)
603
604   .  The UUIDs generated from the same name in two different namespaces
605      should be different with (very high probability)
606
607   .  If two UUIDs that were generated from names are equal, then they
608      were generated from the same name in the same namespace (with very
609      high probability).
610
611   Leach, Salz              expires  Aug 1998                   [Page 10]\f
612
613
614   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
615
616
617   The algorithm for generating the a UUID from a name and a name space
618   are as follows:
619
620   .  Allocate a UUID to use as a "name space ID" for all UUIDs generated
621      from names in that name space
622
623   .  Convert the name to a canonical sequence of octets (as defined by
624      the standards or conventions of its name space); put the name space
625      ID in network byte order
626
627   .  Compute the MD5 [3] hash of the name space ID concatenated with the
628      name
629
630   .  Set octets 0-3 of  time_low field to octets 0-3 of the MD5 hash
631
632   .  Set octets 0-1 of  time_mid field to octets 4-5 of the MD5 hash
633
634   .  Set octets 0-1 of  time_hi_and_version field to octets 6-7 of the
635      MD5 hash
636
637   .  Set the clock_seq_hi_and_reserved field to octet 8 of the MD5 hash
638
639   .  Set the clock_seq_low field to octet 9 of the MD5 hash
640
641   .  Set octets 0-5 of the node field to octets 10-15 of the MD5 hash
642
643   .  Set the 2 most significant bits (bits numbered 6 and 7) of the
644      clock_seq_hi_and_reserved to 0 and 1, respectively.
645
646   .  Set the 4 most significant bits (bits numbered 12 to 15 inclusive)
647      of the time_hi_and_version field to the 4-bit version number
648      corresponding to the UUID version being created, as shown in the
649      table above.
650
651   .  Convert the resulting UUID to local byte order.
652
653
654 3.4 Algorithms for creating a UUID from truly random or pseudo-random
655 numbers
656
657   The version 4 UUID is meant for generating UUIDs from truly-random or
658   pseudo-random numbers.
659
660   The algorithm is as follows:
661
662   .  Set the 2 most significant bits (bits numbered 6 and 7) of the
663      clock_seq_hi_and_reserved to 0 and 1, respectively.
664
665   .  Set the 4 most significant bits (bits numbered 12 to 15 inclusive)
666      of the time_hi_and_version field to the 4-bit version number
667      corresponding to the UUID version being created, as shown in the
668      table above.
669
670
671
672   Leach, Salz              expires  Aug 1998                   [Page 11]\f
673
674
675   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
676
677
678   .  Set all the other bits to randomly (or pseudo-randomly) chosen
679      values.
680
681   Here are several possible ways to generate the random values:
682
683   .  Use a physical source of randomness: for example, a white noise
684      generator, radioactive decay, or a lava lamp.
685
686   .  Use a cryptographic strength random number generator.
687
688
689 3.5 String Representation of UUIDs
690
691   For use in human readable text, a UUID string representation is
692   specified as a sequence of fields, some of which are separated by
693   single dashes.
694
695   Each field is treated as an integer and has its value printed as a
696   zero-filled hexadecimal digit string with the most significant digit
697   first. The hexadecimal values a to f inclusive are output as lower
698   case characters, and are case insensitive on input. The sequence is
699   the same as the UUID constructed type.
700
701   The formal definition of the UUID string representation is provided
702   by the following extended BNF:
703
704   UUID                   = <time_low> "-" <time_mid> "-"
705                            <time_high_and_version> "-"
706                            <clock_seq_and_reserved>
707                            <clock_seq_low> "-" <node>
708   time_low               = 4*<hexOctet>
709   time_mid               = 2*<hexOctet>
710   time_high_and_version  = 2*<hexOctet>
711   clock_seq_and_reserved = <hexOctet>
712   clock_seq_low          = <hexOctet>
713   node                   = 6*<hexOctet
714   hexOctet               = <hexDigit> <hexDigit>
715   hexDigit =
716         "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
717         | "a" | "b" | "c" | "d" | "e" | "f"
718         | "A" | "B" | "C" | "D" | "E" | "F"
719
720   The following is an example of the string representation of a UUID:
721
722        f81d4fae-7dec-11d0-a765-00a0c91e6bf6
723
724 3.6 Comparing UUIDs for equality
725
726   Consider each field of the UUID to be an unsigned integer as shown in
727   the table in section 3.1. Then, to compare a pair of UUIDs,
728   arithmetically compare the corresponding fields from each UUID in
729   order of significance and according to their data type. Two UUIDs are
730   equal if and only if all the corresponding fields are equal.
731
732
733   Leach, Salz              expires  Aug 1998                   [Page 12]\f
734
735
736   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
737
738
739   Note: as a practical matter, on many systems comparison of two UUIDs
740   for equality can be performed simply by comparing the 128 bits of
741   their in-memory representation considered as a 128 bit unsigned
742   integer. Here, it is presumed that by the time the in-memory
743   representation is obtained the appropriate byte-order
744   canonicalizations have been carried out.
745
746
747 3.7 Comparing UUIDs for relative order
748
749   Two UUIDs allocated according to the same variant can also be ordered
750   lexicographically. For the UUID variant herein defined, the first of
751   two UUIDs follows the second if the most significant field in which
752   the UUIDs differ is greater for the first UUID. The first of a pair
753   of UUIDs precedes the second if the most significant field in which
754   the UUIDs differ is greater for the second UUID.
755
756
757 3.8 Byte order of UUIDs
758
759   UUIDs may be transmitted in many different forms, some of which may
760   be dependent on the presentation or application protocol where the
761   UUID may be used.  In such cases, the order, sizes and byte orders of
762   the UUIDs fields on the wire will depend on the relevant presentation
763   or application protocol.  However, it is strongly RECOMMENDED that
764   the order of the fields conform with ordering set out in section 3.1
765   above. Furthermore, the payload size of each field in the application
766   or presentation protocol MUST be large enough that no information
767   lost in the process of encoding them for transmission.
768
769   In the absence of explicit application or presentation protocol
770   specification to the contrary, a UUID is encoded as a 128-bit object,
771   as follows: the fields are encoded as 16 octets, with the sizes and
772   order of the fields defined in section 3.1, and with each field
773   encoded with the Most Significant Byte first (also known as network
774   byte order).
775
776   0                   1                   2                   3
777    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
778   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
779   |                          time_low                             |
780   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
781   |       time_mid                |         time_hi_and_version   |
782   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
783   |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
784   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
785   |                         node (2-5)                            |
786   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
787
788
789
790
791
792
793
794   Leach, Salz              expires  Aug 1998                   [Page 13]\f
795
796
797   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
798
799
800 4. Node IDs when no IEEE 802 network card is available
801
802   If a system wants to generate UUIDs but has no IEE 802 compliant
803   network card or other source of IEEE 802 addresses, then this section
804   describes how to generate one.
805
806   The ideal solution is to obtain a 47 bit cryptographic quality random
807   number, and use it as the low 47 bits of the node ID, with the most
808   significant bit of the first octet of the node ID set to 1. This bit
809   is the unicast/multicast bit, which will never be set in IEEE 802
810   addresses obtained from network cards; hence, there can never be a
811   conflict between UUIDs generated by machines with and without network
812   cards.
813
814   If a system does not have a primitive to generate cryptographic
815   quality random numbers, then in most systems there are usually a
816   fairly large number of sources of randomness available from which one
817   can be generated. Such sources are system specific, but often
818   include:
819
820   - the percent of memory in use
821   - the size of main memory in bytes
822   - the amount of free main memory in bytes
823   - the size of the paging or swap file in bytes
824   - free bytes of paging or swap file
825   - the total size of user virtual address space in bytes
826   - the total available user address space bytes
827   - the size of boot disk drive in bytes
828   - the free disk space on boot drive in bytes
829   - the current time
830   - the amount of time since the system booted
831   - the individual sizes of files in various system directories
832   - the creation, last read, and modification times of files in various
833   system directories
834   - the utilization factors of various system resources (heap, etc.)
835   - current mouse cursor position
836   - current caret position
837   - current number of running processes, threads
838   - handles or IDs of the desktop window and the active window
839   - the value of stack pointer of the caller
840   - the process and thread ID of caller
841   - various processor architecture specific performance counters
842   (instructions executed, cache misses, TLB misses)
843
844   (Note that it precisely the above kinds of sources of randomness that
845   are used to seed cryptographic quality random number generators on
846   systems without special hardware for their construction.)
847
848   In addition, items such as the computer's name and the name of the
849   operating system, while not strictly speaking random, will help
850   differentiate the results from those obtained by other systems.
851
852   The exact algorithm to generate a node ID using these data is system
853   specific, because both the data available and the functions to obtain
854
855   Leach, Salz              expires  Aug 1998                   [Page 14]\f
856
857
858   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
859
860
861   them are often very system specific. However, assuming that one can
862   concatenate all the values from the randomness sources into a buffer,
863   and that a cryptographic hash function such as MD5 [3] is available,
864   then any 6 bytes of the MD5 hash of the buffer, with the multicast
865   bit (the high bit of the first byte) set will be an appropriately
866   random node ID.
867
868   Other hash functions, such as SHA-1 [4], can also be used. The only
869   requirement is that the result be suitably random _ in the sense that
870   the outputs from a set uniformly distributed inputs are themselves
871   uniformly distributed, and that a single bit change in the input can
872   be expected to cause half of the output bits to change.
873
874
875 5. Obtaining IEEE 802 addresses
876
877   At the time of writing, the following URL
878
879        http://standards.ieee.org/db/oui/forms/
880
881   contains information on how to obtain an IEEE 802 address block. At
882   the time of writing, the cost is $1250 US.
883
884
885 6. Security Considerations
886
887   It should not be assumed that UUIDs are hard to guess; they should
888   not be used as capabilities.
889
890
891 7. Acknowledgements
892
893   This document draws heavily on the OSF DCE specification for UUIDs.
894   Ted Ts'o provided helpful comments, especially on the byte ordering
895   section which we mostly plagiarized from a proposed wording he
896   supplied (all errors in that section are our responsibility,
897   however).
898
899
900 8. References
901
902   [1]  Lisa Zahn, et. al., Network Computing Architecture, Prentice
903      Hall, Englewood Cliffs, NJ, 1990
904
905   [2] DCE: Remote Procedure Call, Open Group CAE Specification C309
906   ISBN 1-85912-041-5 28cm. 674p. pbk. 1,655g. 8/94
907
908   [3] R. Rivest, RFC 1321, "The MD5 Message-Digest Algorithm",
909      04/16/1992.
910
911   [4] NIST FIPS PUB 180-1, "Secure Hash Standard," National Institute
912   of Standards and Technology, U.S. Department of Commerce, DRAFT, May
913   31, 1994.
914
915
916   Leach, Salz              expires  Aug 1998                   [Page 15]\f
917
918
919   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
920
921
922 9. Authors' addresses
923
924   Paul J. Leach
925   Microsoft
926   1 Microsoft Way
927   Redmond, WA, 98052, U.S.A.
928   paulle@microsoft.com
929   Tel. 425 882 8080
930   Fax. 425 936 7329
931
932   Rich Salz
933   100 Cambridge Park Drive
934   Cambridge MA  02140
935   salzr@certco.com
936   Tel. 617 499 4075
937   Fax. 617 576 0019
938
939
940 10. Notice
941
942   The IETF takes no position regarding the validity or scope of any
943   intellectual property or other rights that might be claimed to
944   pertain to the implementation or use of the technology described in
945   this document or the extent to which any license under such rights
946   might or might not be available; neither does it represent that it
947   has made any effort to identify any such rights.  Information on the
948   IETF's procedures with respect to rights in standards-track and
949   standards-related documentation can be found in BCP-11.  Copies of
950   claims of rights made available for publication and any assurances of
951   licenses to be made available, or the result of an attempt made to
952   obtain a general license or permission for the use of such
953   proprietary rights by implementors or users of this specification can
954   be obtained from the IETF Secretariat.
955
956   The IETF invites any interested party to bring to its attention any
957   copyrights, patents or patent applications, or other proprietary
958   rights which may cover technology that may be required to practice
959   this standard.  Please address the information to the IETF Executive
960   Director.
961
962
963 11. Full Copyright Statement
964
965   Copyright (C) The Internet Society 1997. All Rights Reserved.
966
967   This document and translations of it may be copied and furnished to
968   others, and derivative works that comment on or otherwise explain it
969   or assist in its implementation may be prepared, copied, published
970   and distributed, in whole or in part, without restriction of any
971   kind, provided that the above copyright notice and this paragraph are
972   included on all such copies and derivative works.  However, this
973   document itself may not be modified in any way, such as by removing
974   the copyright notice or references to the Internet Society or other
975   Internet organizations, except as needed for the purpose of
976
977   Leach, Salz              expires  Aug 1998                   [Page 16]\f
978
979
980   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
981
982
983   developing Internet standards in which case the procedures for
984   copyrights defined in the Internet Standards process must be
985   followed, or as required to translate it into languages other than
986   English.
987
988   The limited permissions granted above are perpetual and will not be
989   revoked by the Internet Society or its successors or assigns.
990
991   This document and the information contained herein is provided on an
992   "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
993   TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
994   BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
995   HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
996   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
997
998
999  Appendix A _ UUID Sample Implementation
1000
1001   This implementation consists of 5 files: uuid.h, uuid.c, sysdep.h,
1002   sysdep.c and utest.c. The uuid.* files are the system independent
1003   implementation of the UUID generation algorithms described above,
1004   with all the optimizations described above except efficient state
1005   sharing across processes included. The code has been tested on Linux
1006   (Red Hat 4.0) with GCC (2.7.2), and Windows NT 4.0 with VC++ 5.0. The
1007   code assumes 64 bit integer support, which makes it a lot clearer.
1008
1009   All the following source files should be considered to have the
1010   following copyright notice included:
1011
1012   copyrt.h
1013
1014   /*
1015   ** Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
1016   ** Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
1017   ** Digital Equipment Corporation, Maynard, Mass.
1018   ** Copyright (c) 1998 Microsoft.
1019   ** To anyone who acknowledges that this file is provided "AS IS"
1020   ** without any express or implied warranty: permission to use, copy,
1021   ** modify, and distribute this file for any purpose is hereby
1022   ** granted without fee, provided that the above copyright notices and
1023   ** this notice appears in all source code copies, and that none of
1024   ** the names of Open Software Foundation, Inc., Hewlett-Packard
1025   ** Company, or Digital Equipment Corporation be used in advertising
1026   ** or publicity pertaining to distribution of the software without
1027   ** specific, written prior permission.  Neither Open Software
1028   ** Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital
1029   Equipment
1030   ** Corporation makes any representations about the suitability of
1031   ** this software for any purpose.
1032   */
1033
1034
1035   uuid.h
1036
1037
1038   Leach, Salz              expires  Aug 1998                   [Page 17]\f
1039
1040
1041   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
1042
1043
1044   #include "copyrt.h"
1045   #undef uuid_t
1046   typedef struct _uuid_t {
1047       unsigned32          time_low;
1048       unsigned16          time_mid;
1049       unsigned16          time_hi_and_version;
1050       unsigned8           clock_seq_hi_and_reserved;
1051       unsigned8           clock_seq_low;
1052       byte                node[6];
1053   } uuid_t;
1054
1055   /* uuid_create -- generate a UUID */
1056   int uuid_create(uuid_t * uuid);
1057
1058   /* uuid_create_from_name -- create a UUID using a "name"
1059      from a "name space" */
1060   void uuid_create_from_name(
1061     uuid_t * uuid,        /* resulting UUID */
1062     uuid_t nsid,          /* UUID to serve as context, so identical
1063                              names from different name spaces generate
1064                              different UUIDs */
1065     void * name,          /* the name from which to generate a UUID */
1066     int namelen           /* the length of the name */
1067   );
1068
1069   /* uuid_compare --  Compare two UUID's "lexically" and return
1070           -1   u1 is lexically before u2
1071            0   u1 is equal to u2
1072            1   u1 is lexically after u2
1073      Note:   lexical ordering is not temporal ordering!
1074   */
1075   int uuid_compare(uuid_t *u1, uuid_t *u2);
1076
1077   uuid.c
1078
1079   #include "copyrt.h"
1080   #include <string.h>
1081   #include <stdio.h>
1082   #include <stdlib.h>
1083   #include <time.h>
1084   #include "sysdep.h"
1085   #include "uuid.h"
1086
1087   /* various forward declarations */
1088   static int read_state(unsigned16 *clockseq, uuid_time_t *timestamp,
1089   uuid_node_t * node);
1090   static void write_state(unsigned16 clockseq, uuid_time_t timestamp,
1091   uuid_node_t node);
1092   static void format_uuid_v1(uuid_t * uuid, unsigned16 clockseq,
1093   uuid_time_t timestamp, uuid_node_t node);
1094   static void format_uuid_v3(uuid_t * uuid, unsigned char hash[16]);
1095   static void get_current_time(uuid_time_t * timestamp);
1096   static unsigned16 true_random(void);
1097
1098
1099   Leach, Salz              expires  Aug 1998                   [Page 18]\f
1100
1101
1102   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
1103
1104
1105   /* uuid_create -- generator a UUID */
1106   int uuid_create(uuid_t * uuid) {
1107     uuid_time_t timestamp, last_time;
1108     unsigned16 clockseq;
1109     uuid_node_t node;
1110     uuid_node_t last_node;
1111     int f;
1112
1113     /* acquire system wide lock so we're alone */
1114     LOCK;
1115
1116     /* get current time */
1117     get_current_time(&timestamp);
1118
1119     /* get node ID */
1120     get_ieee_node_identifier(&node);
1121
1122     /* get saved state from NV storage */
1123     f = read_state(&clockseq, &last_time, &last_node);
1124
1125     /* if no NV state, or if clock went backwards, or node ID changed
1126        (e.g., net card swap) change clockseq */
1127     if (!f || memcmp(&node, &last_node, sizeof(uuid_node_t)))
1128         clockseq = true_random();
1129     else if (timestamp < last_time)
1130         clockseq++;
1131
1132     /* stuff fields into the UUID */
1133     format_uuid_v1(uuid, clockseq, timestamp, node);
1134
1135     /* save the state for next time */
1136     write_state(clockseq, timestamp, node);
1137
1138     UNLOCK;
1139     return(1);
1140   };
1141
1142   /* format_uuid_v1 -- make a UUID from the timestamp, clockseq,
1143                        and node ID */
1144   void format_uuid_v1(uuid_t * uuid, unsigned16 clock_seq, uuid_time_t
1145   timestamp, uuid_node_t node) {
1146       /* Construct a version 1 uuid with the information we've gathered
1147        * plus a few constants. */
1148     uuid->time_low = (unsigned long)(timestamp & 0xFFFFFFFF);
1149       uuid->time_mid = (unsigned short)((timestamp >> 32) & 0xFFFF);
1150       uuid->time_hi_and_version = (unsigned short)((timestamp >> 48) &
1151          0x0FFF);
1152       uuid->time_hi_and_version |= (1 << 12);
1153       uuid->clock_seq_low = clock_seq & 0xFF;
1154       uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3F00) >> 8;
1155       uuid->clock_seq_hi_and_reserved |= 0x80;
1156       memcpy(&uuid->node, &node, sizeof uuid->node);
1157   };
1158
1159
1160   Leach, Salz              expires  Aug 1998                   [Page 19]\f
1161
1162
1163   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
1164
1165
1166   /* data type for UUID generator persistent state */
1167   typedef struct {
1168     uuid_time_t ts;       /* saved timestamp */
1169     uuid_node_t node;     /* saved node ID */
1170     unsigned16 cs;        /* saved clock sequence */
1171     } uuid_state;
1172
1173   static uuid_state st;
1174
1175   /* read_state -- read UUID generator state from non-volatile store */
1176   int read_state(unsigned16 *clockseq, uuid_time_t *timestamp,
1177   uuid_node_t *node) {
1178     FILE * fd;
1179     static int inited = 0;
1180
1181     /* only need to read state once per boot */
1182     if (!inited) {
1183         fd = fopen("state", "rb");
1184         if (!fd)
1185              return (0);
1186         fread(&st, sizeof(uuid_state), 1, fd);
1187         fclose(fd);
1188         inited = 1;
1189     };
1190     *clockseq = st.cs;
1191     *timestamp = st.ts;
1192     *node = st.node;
1193     return(1);
1194   };
1195
1196   /* write_state -- save UUID generator state back to non-volatile
1197   storage */
1198   void write_state(unsigned16 clockseq, uuid_time_t timestamp,
1199   uuid_node_t node) {
1200     FILE * fd;
1201     static int inited = 0;
1202     static uuid_time_t next_save;
1203
1204     if (!inited) {
1205         next_save = timestamp;
1206         inited = 1;
1207     };
1208     /* always save state to volatile shared state */
1209     st.cs = clockseq;
1210     st.ts = timestamp;
1211     st.node = node;
1212     if (timestamp >= next_save) {
1213         fd = fopen("state", "wb");
1214         fwrite(&st, sizeof(uuid_state), 1, fd);
1215         fclose(fd);
1216         /* schedule next save for 10 seconds from now */
1217         next_save = timestamp + (10 * 10 * 1000 * 1000);
1218     };
1219   };
1220
1221   Leach, Salz              expires  Aug 1998                   [Page 20]\f
1222
1223
1224   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
1225
1226
1227
1228   /* get-current_time -- get time as 60 bit 100ns ticks since whenever.
1229     Compensate for the fact that real clock resolution is
1230     less than 100ns. */
1231   void get_current_time(uuid_time_t * timestamp) {
1232       uuid_time_t                time_now;
1233       static uuid_time_t  time_last;
1234       static unsigned16   uuids_this_tick;
1235     static int                   inited = 0;
1236
1237     if (!inited) {
1238           get_system_time(&time_now);
1239         uuids_this_tick = UUIDS_PER_TICK;
1240         inited = 1;
1241     };
1242
1243       while (1) {
1244           get_system_time(&time_now);
1245
1246         /* if clock reading changed since last UUID generated... */
1247           if (time_last != time_now) {
1248              /* reset count of uuids gen'd with this clock reading */
1249               uuids_this_tick = 0;
1250              break;
1251         };
1252           if (uuids_this_tick < UUIDS_PER_TICK) {
1253              uuids_this_tick++;
1254              break;
1255         };
1256         /* going too fast for our clock; spin */
1257       };
1258     /* add the count of uuids to low order bits of the clock reading */
1259     *timestamp = time_now + uuids_this_tick;
1260   };
1261
1262   /* true_random -- generate a crypto-quality random number.
1263      This sample doesn't do that. */
1264   static unsigned16
1265   true_random(void)
1266   {
1267     static int inited = 0;
1268     uuid_time_t time_now;
1269
1270     if (!inited) {
1271         get_system_time(&time_now);
1272         time_now = time_now/UUIDS_PER_TICK;
1273         srand((unsigned int)(((time_now >> 32) ^ time_now)&0xffffffff));
1274         inited = 1;
1275     };
1276
1277       return (rand());
1278   }
1279
1280
1281
1282   Leach, Salz              expires  Aug 1998                   [Page 21]\f
1283
1284
1285   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
1286
1287
1288   /* uuid_create_from_name -- create a UUID using a "name" from a "name
1289   space" */
1290   void uuid_create_from_name(
1291     uuid_t * uuid,        /* resulting UUID */
1292     uuid_t nsid,          /* UUID to serve as context, so identical
1293                              names from different name spaces generate
1294                              different UUIDs */
1295     void * name,          /* the name from which to generate a UUID */
1296     int namelen           /* the length of the name */
1297   ) {
1298     MD5_CTX c;
1299     unsigned char hash[16];
1300     uuid_t net_nsid;      /* context UUID in network byte order */
1301
1302     /* put name space ID in network byte order so it hashes the same
1303         no matter what endian machine we're on */
1304     net_nsid = nsid;
1305     htonl(net_nsid.time_low);
1306     htons(net_nsid.time_mid);
1307     htons(net_nsid.time_hi_and_version);
1308
1309     MD5Init(&c);
1310     MD5Update(&c, &net_nsid, sizeof(uuid_t));
1311     MD5Update(&c, name, namelen);
1312     MD5Final(hash, &c);
1313
1314     /* the hash is in network byte order at this point */
1315     format_uuid_v3(uuid, hash);
1316   };
1317
1318   /* format_uuid_v3 -- make a UUID from a (pseudo)random 128 bit number
1319   */
1320   void format_uuid_v3(uuid_t * uuid, unsigned char hash[16]) {
1321       /* Construct a version 3 uuid with the (pseudo-)random number
1322        * plus a few constants. */
1323
1324       memcpy(uuid, hash, sizeof(uuid_t));
1325
1326     /* convert UUID to local byte order */
1327     ntohl(uuid->time_low);
1328     ntohs(uuid->time_mid);
1329     ntohs(uuid->time_hi_and_version);
1330
1331     /* put in the variant and version bits */
1332       uuid->time_hi_and_version &= 0x0FFF;
1333       uuid->time_hi_and_version |= (3 << 12);
1334       uuid->clock_seq_hi_and_reserved &= 0x3F;
1335       uuid->clock_seq_hi_and_reserved |= 0x80;
1336   };
1337
1338   /* uuid_compare --  Compare two UUID's "lexically" and return
1339          -1   u1 is lexically before u2
1340           0   u1 is equal to u2
1341           1   u1 is lexically after u2
1342
1343   Leach, Salz              expires  Aug 1998                   [Page 22]\f
1344
1345
1346   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
1347
1348
1349       Note:   lexical ordering is not temporal ordering!
1350   */
1351   int uuid_compare(uuid_t *u1, uuid_t *u2)
1352   {
1353     int i;
1354
1355   #define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1;
1356     CHECK(u1->time_low, u2->time_low);
1357     CHECK(u1->time_mid, u2->time_mid);
1358     CHECK(u1->time_hi_and_version, u2->time_hi_and_version);
1359     CHECK(u1->clock_seq_hi_and_reserved, u2->clock_seq_hi_and_reserved);
1360     CHECK(u1->clock_seq_low, u2->clock_seq_low)
1361     for (i = 0; i < 6; i++) {
1362         if (u1->node[i] < u2->node[i])
1363              return -1;
1364         if (u1->node[i] > u2->node[i])
1365         return 1;
1366       }
1367     return 0;
1368   };
1369
1370   sysdep.h
1371
1372   #include "copyrt.h"
1373   /* remove the following define if you aren't running WIN32 */
1374   #define WININC 0
1375
1376   #ifdef WININC
1377   #include <windows.h>
1378   #else
1379   #include <sys/types.h>
1380   #include <sys/time.h>
1381   #include <sys/sysinfo.h>
1382   #endif
1383
1384   /* change to point to where MD5 .h's live */
1385   /* get MD5 sample implementation from RFC 1321 */
1386   #include "global.h"
1387   #include "md5.h"
1388
1389   /* set the following to the number of 100ns ticks of the actual
1390   resolution of
1391   your system's clock */
1392   #define UUIDS_PER_TICK 1024
1393
1394   /* Set the following to a call to acquire a system wide global lock
1395   */
1396   #define LOCK
1397   #define UNLOCK
1398
1399   typedef unsigned long   unsigned32;
1400   typedef unsigned short  unsigned16;
1401   typedef unsigned char   unsigned8;
1402   typedef unsigned char   byte;
1403
1404   Leach, Salz              expires  Aug 1998                   [Page 23]\f
1405
1406
1407   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
1408
1409
1410
1411   /* Set this to what your compiler uses for 64 bit data type */
1412   #ifdef WININC
1413   #define unsigned64_t unsigned __int64
1414   #define I64(C) C
1415   #else
1416   #define unsigned64_t unsigned long long
1417   #define I64(C) C##LL
1418   #endif
1419
1420
1421   typedef unsigned64_t uuid_time_t;
1422   typedef struct {
1423     char nodeID[6];
1424   } uuid_node_t;
1425
1426   void get_ieee_node_identifier(uuid_node_t *node);
1427   void get_system_time(uuid_time_t *uuid_time);
1428   void get_random_info(char seed[16]);
1429
1430
1431   sysdep.c
1432
1433   #include "copyrt.h"
1434   #include <stdio.h>
1435   #include "sysdep.h"
1436
1437   /* system dependent call to get IEEE node ID.
1438      This sample implementation generates a random node ID
1439      */
1440   void get_ieee_node_identifier(uuid_node_t *node) {
1441     char seed[16];
1442     FILE * fd;
1443     static inited = 0;
1444     static uuid_node_t saved_node;
1445
1446     if (!inited) {
1447         fd = fopen("nodeid", "rb");
1448         if (fd) {
1449              fread(&saved_node, sizeof(uuid_node_t), 1, fd);
1450              fclose(fd);
1451         }
1452         else {
1453              get_random_info(seed);
1454              seed[0] |= 0x80;
1455              memcpy(&saved_node, seed, sizeof(uuid_node_t));
1456              fd = fopen("nodeid", "wb");
1457              if (fd) {
1458                     fwrite(&saved_node, sizeof(uuid_node_t), 1, fd);
1459                     fclose(fd);
1460              };
1461         };
1462         inited = 1;
1463     };
1464
1465   Leach, Salz              expires  Aug 1998                   [Page 24]\f
1466
1467
1468   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
1469
1470
1471     *node = saved_node;
1472   };
1473
1474   /* system dependent call to get the current system time.
1475      Returned as 100ns ticks since Oct 15, 1582, but resolution may be
1476      less than 100ns.
1477   */
1478   #ifdef _WINDOWS_
1479
1480   void get_system_time(uuid_time_t *uuid_time) {
1481     ULARGE_INTEGER time;
1482
1483     GetSystemTimeAsFileTime((FILETIME *)&time);
1484
1485       /* NT keeps time in FILETIME format which is 100ns ticks since
1486        Jan 1, 1601.  UUIDs use time in 100ns ticks since Oct 15, 1582.
1487        The difference is 17 Days in Oct + 30 (Nov) + 31 (Dec)
1488        + 18 years and 5 leap days.
1489     */
1490
1491       time.QuadPart +=
1492             (unsigned __int64) (1000*1000*10)       // seconds
1493           * (unsigned __int64) (60 * 60 * 24)       // days
1494           * (unsigned __int64) (17+30+31+365*18+5); // # of days
1495
1496     *uuid_time = time.QuadPart;
1497
1498   };
1499
1500   void get_random_info(char seed[16]) {
1501     MD5_CTX c;
1502     typedef struct {
1503         MEMORYSTATUS m;
1504         SYSTEM_INFO s;
1505         FILETIME t;
1506         LARGE_INTEGER pc;
1507         DWORD tc;
1508         DWORD l;
1509         char hostname[MAX_COMPUTERNAME_LENGTH + 1];
1510     } randomness;
1511     randomness r;
1512
1513     MD5Init(&c);
1514     /* memory usage stats */
1515     GlobalMemoryStatus(&r.m);
1516     /* random system stats */
1517     GetSystemInfo(&r.s);
1518     /* 100ns resolution (nominally) time of day */
1519     GetSystemTimeAsFileTime(&r.t);
1520     /* high resolution performance counter */
1521     QueryPerformanceCounter(&r.pc);
1522     /* milliseconds since last boot */
1523     r.tc = GetTickCount();
1524     r.l = MAX_COMPUTERNAME_LENGTH + 1;
1525
1526   Leach, Salz              expires  Aug 1998                   [Page 25]\f
1527
1528
1529   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
1530
1531
1532     GetComputerName(r.hostname, &r.l );
1533     MD5Update(&c, &r, sizeof(randomness));
1534     MD5Final(seed, &c);
1535   };
1536   #else
1537
1538   void get_system_time(uuid_time_t *uuid_time)
1539   {
1540       struct timeval tp;
1541
1542       gettimeofday(&tp, (struct timezone *)0);
1543
1544       /* Offset between UUID formatted times and Unix formatted times.
1545          UUID UTC base time is October 15, 1582.
1546          Unix base time is January 1, 1970.
1547       */
1548       *uuid_time = (tp.tv_sec * 10000000) + (tp.tv_usec * 10) +
1549         I64(0x01B21DD213814000);
1550   };
1551
1552   void get_random_info(char seed[16]) {
1553     MD5_CTX c;
1554     typedef struct {
1555         struct sysinfo s;
1556         struct timeval t;
1557         char hostname[257];
1558     } randomness;
1559     randomness r;
1560
1561     MD5Init(&c);
1562     sysinfo(&r.s);
1563     gettimeofday(&r.t, (struct timezone *)0);
1564     gethostname(r.hostname, 256);
1565     MD5Update(&c, &r, sizeof(randomness));
1566     MD5Final(seed, &c);
1567   };
1568
1569   #endif
1570
1571   utest.c
1572
1573   #include "copyrt.h"
1574   #include "sysdep.h"
1575   #include <stdio.h>
1576   #include "uuid.h"
1577
1578   uuid_t NameSpace_DNS = { /* 6ba7b810-9dad-11d1-80b4-00c04fd430c8 */
1579       0x6ba7b810,
1580       0x9dad,
1581       0x11d1,
1582       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
1583     };
1584
1585
1586
1587   Leach, Salz              expires  Aug 1998                   [Page 26]\f
1588
1589
1590   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
1591
1592
1593   /* puid -- print a UUID */
1594   void puid(uuid_t u);
1595
1596   /* Simple driver for UUID generator */
1597   void main(int argc, char **argv) {
1598     uuid_t u;
1599     int f;
1600
1601     uuid_create(&u);
1602     printf("uuid_create()             -> "); puid(u);
1603
1604     f = uuid_compare(&u, &u);
1605     printf("uuid_compare(u,u): %d\n", f);     /* should be 0 */
1606     f = uuid_compare(&u, &NameSpace_DNS);
1607     printf("uuid_compare(u, NameSpace_DNS): %d\n", f); /* s.b. 1 */
1608     f = uuid_compare(&NameSpace_DNS, &u);
1609     printf("uuid_compare(NameSpace_DNS, u): %d\n", f); /* s.b. -1 */
1610
1611     uuid_create_from_name(&u, NameSpace_DNS, "www.widgets.com", 15);
1612     printf("uuid_create_from_name() -> "); puid(u);
1613   };
1614
1615   void puid(uuid_t u) {
1616     int i;
1617
1618     printf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", u.time_low, u.time_mid,
1619         u.time_hi_and_version, u.clock_seq_hi_and_reserved,
1620         u.clock_seq_low);
1621     for (i = 0; i < 6; i++)
1622         printf("%2.2x", u.node[i]);
1623     printf("\n");
1624   };
1625
1626 Appendix B _ Sample output of utest
1627
1628   uuid_create()             -> 7d444840-9dc0-11d1-b245-5ffdce74fad2
1629   uuid_compare(u,u): 0
1630   uuid_compare(u, NameSpace_DNS): 1
1631   uuid_compare(NameSpace_DNS, u): -1
1632   uuid_create_from_name()   -> e902893a-9d22-3c7e-a7b8-d6e313b71d9f
1633
1634 Appendix C _ Some name space IDs
1635
1636   This appendix lists the name space IDs for some potentially
1637   interesting name spaces, as initialized C structures and in the
1638   string representation defined in section 3.5
1639
1640   uuid_t NameSpace_DNS = { /* 6ba7b810-9dad-11d1-80b4-00c04fd430c8 */
1641       0x6ba7b810,
1642       0x9dad,
1643       0x11d1,
1644       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
1645     };
1646
1647
1648   Leach, Salz              expires  Aug 1998                   [Page 27]\f
1649
1650
1651   Internet-Draft        UUIDs and GUIDs (DRAFT)                 02/04/98
1652
1653
1654   uuid_t NameSpace_URL = { /* 6ba7b811-9dad-11d1-80b4-00c04fd430c8 */
1655       0x6ba7b811,
1656       0x9dad,
1657       0x11d1,
1658       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
1659     };
1660
1661   uuid_t NameSpace_OID = { /* 6ba7b812-9dad-11d1-80b4-00c04fd430c8 */
1662       0x6ba7b812,
1663       0x9dad,
1664       0x11d1,
1665       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
1666     };
1667
1668   uuid_t NameSpace_X500 = { /* 6ba7b814-9dad-11d1-80b4-00c04fd430c8 */
1669       0x6ba7b814,
1670       0x9dad,
1671       0x11d1,
1672       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
1673     };
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708