summaryrefslogtreecommitdiff
path: root/ACE/ace/UUID.cpp
blob: b481c53e3013ad121384b91dd8e4aedd37b72678 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include "ace/UUID.h"
#include "ace/Guard_T.h"

#if !defined (__ACE_INLINE__)
#include "ace/UUID.inl"
#endif /* __ACE_INLINE__ */

#include "ace/Log_Category.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_sys_time.h"
#include "ace/OS_NS_netdb.h"
#include "ace/OS_NS_unistd.h"
#include "ace/ACE.h"
#include "ace/Auto_Ptr.h"

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE_Utils
{
  // NIL version of the UUID
  const UUID UUID::NIL_UUID;

#ifndef ACE_LACKS_SSCANF
  UUID::UUID (const ACE_CString& uuid_string)
  {
    this->init ();
    this->from_string_i (uuid_string);
  }
#endif /* ACE_LACKS_SSCANF */

  ACE_ALLOC_HOOK_DEFINE(UUID);

  const UUID &
  UUID::operator = (const UUID & rhs)
  {
    if (this != &rhs)
      {
        // Reset the string version of the UUID a string version
        // exist, and the UUID is not equal to the old UUID.
        if (0 != this->as_string_.get ())
          {
            if (0 == rhs.as_string_.get () || *this != rhs)
              this->as_string_.reset ();
          }

        // Copy the contents of the UUID.
        ACE_OS::memcpy (&this->uuid_, &rhs.uuid_, BINARY_SIZE);

        /// @todo We should create an UUID_Ex class for UUIDs that
        ///       contain the thread id and process id.
        this->thr_id_ = rhs.thr_id_;
        this->pid_ = rhs.pid_;
      }

    return *this;
  }

  const ACE_CString * UUID::to_string () const
  {
    // Compute the string representation only once.
    if (0 != this->as_string_.get ())
      return this->as_string_.get ();

    // Get a buffer exactly the correct size. Use the nil UUID as a
    // gauge.  Don't forget the trailing nul.
    ACE_Auto_Array_Ptr <char> auto_clean;
    size_t UUID_STRING_LENGTH = 36 + thr_id_.length () + pid_.length ();
    char *buf = 0;

    if (36 == UUID_STRING_LENGTH)
      {
#if defined (ACE_HAS_ALLOC_HOOKS)
        ACE_ALLOCATOR_RETURN (buf,
                              static_cast<char*> (ACE_Allocator::instance()->malloc(sizeof (char) * (UUID_STRING_LENGTH + 1))),
                              0);
#else
        ACE_NEW_RETURN (buf,
                        char[UUID_STRING_LENGTH + 1],
                        0);
#endif /* ACE_HAS_ALLOC_HOOKS */

        // Let the auto array pointer manage the buffer.
        auto_clean.reset (buf);

        ACE_OS::snprintf (buf, UUID_STRING_LENGTH + 1,
                          "%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
                          this->uuid_.time_low_,
                          this->uuid_.time_mid_,
                          this->uuid_.time_hi_and_version_,
                          this->uuid_.clock_seq_hi_and_reserved_,
                          this->uuid_.clock_seq_low_,
                          this->uuid_.node_.node_ID ()[0],
                          this->uuid_.node_.node_ID ()[1],
                          this->uuid_.node_.node_ID ()[2],
                          this->uuid_.node_.node_ID ()[3],
                          this->uuid_.node_.node_ID ()[4],
                          this->uuid_.node_.node_ID ()[5]);
      }
    else
      {
        UUID_STRING_LENGTH += 2; //for '-'

#if defined (ACE_HAS_ALLOC_HOOKS)
        ACE_ALLOCATOR_RETURN (buf,
                              static_cast<char*> (ACE_Allocator::instance()->malloc(sizeof (char) * (UUID_STRING_LENGTH + 1))),
                              0);
#else
        ACE_NEW_RETURN (buf,
                        char[UUID_STRING_LENGTH + 1],
                        0);
#endif /* ACE_HAS_ALLOC_HOOKS */

        // Let the auto array pointer manage the buffer.
        auto_clean.reset (buf);

        ACE_OS::snprintf (buf, UUID_STRING_LENGTH + 1,
                          "%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x-%s-%s",
                          this->uuid_.time_low_,
                          this->uuid_.time_mid_,
                          this->uuid_.time_hi_and_version_,
                          this->uuid_.clock_seq_hi_and_reserved_,
                          this->uuid_.clock_seq_low_,
                          this->uuid_.node_.node_ID ()[0],
                          this->uuid_.node_.node_ID ()[1],
                          this->uuid_.node_.node_ID ()[2],
                          this->uuid_.node_.node_ID ()[3],
                          this->uuid_.node_.node_ID ()[4],
                          this->uuid_.node_.node_ID ()[5],
                          thr_id_.c_str (),
                          pid_.c_str ());
      }

    // Save the string.
    ACE_CString * as_string = 0;

    ACE_NEW_RETURN (as_string,
                    ACE_CString (buf, UUID_STRING_LENGTH),
                    0);

    this->as_string_.reset (as_string);
    return this->as_string_.get ();
  }

#ifndef ACE_LACKS_SSCANF
  void
  UUID::from_string_i (const ACE_CString& uuid_string)
  {
    if (uuid_string.length () < NIL_UUID.to_string ()->length ())
      {
        ACELIB_ERROR ((LM_ERROR,
                    "%N ACE_UUID::from_string_i - "
                    "IllegalArgument (incorrect string length)\n"));
        return;
      }

    /// Special case for the nil UUID.
    if (uuid_string == *NIL_UUID.to_string ())
      {
        *this = NIL_UUID;
        return;
      }

    unsigned int time_low;
    unsigned int time_mid;
    unsigned int time_hi_and_version;
    unsigned int clock_seq_hi_and_reserved;
    unsigned int clock_seq_low;
    unsigned int node [UUID_Node::NODE_ID_SIZE];
    char thr_pid_buf [BUFSIZ];

    if (uuid_string.length () == NIL_UUID.to_string ()->length ())
      {
        // This might seem quite strange this being in ACE, but it
        // seems to be a bit difficult to write a facade for ::sscanf
        // because some compilers dont support vsscanf, including
        // MSVC. It appears that most platforms support sscanf though
        // so we need to use it directly.
        const int nScanned =
#if defined (ACE_HAS_TR24731_2005_CRT)
          sscanf_s (
                   uuid_string.c_str (),
                   "%8x-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x",
                   &time_low,
                   &time_mid,
                   &time_hi_and_version,
                   &clock_seq_hi_and_reserved,
                   &clock_seq_low,
                   &node[0],
                   &node[1],
                   &node[2],
                   &node[3],
                   &node[4],
                   &node[5]
                   );
#else
          ::sscanf (
                   uuid_string.c_str (),
                   "%8x-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x",
                   &time_low,
                   &time_mid,
                   &time_hi_and_version,
                   &clock_seq_hi_and_reserved,
                   &clock_seq_low,
                   &node[0],
                   &node[1],
                   &node[2],
                   &node[3],
                   &node[4],
                   &node[5]
                   );
#endif /* ACE_HAS_TR24731_2005_CRT */

        if (nScanned != 11)
          {
            ACELIB_DEBUG ((LM_DEBUG,
                        "UUID::from_string_i - "
                        "IllegalArgument (invalid string representation)\n"));
            return;
          }
      }
    else
      {
        const int nScanned =
#if defined (ACE_HAS_TR24731_2005_CRT)
          sscanf_s (uuid_string.c_str (),
                    "%8x-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x-%s",
                    &time_low,
                    &time_mid,
                    &time_hi_and_version,
                    &clock_seq_hi_and_reserved,
                    &clock_seq_low,
                    &node[0],
                    &node[1],
                    &node[2],
                    &node[3],
                    &node[4],
                    &node[5],
                    thr_pid_buf,
                    BUFSIZ
                    );
#else
          ::sscanf (uuid_string.c_str (),
                    "%8x-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x-%s",
                    &time_low,
                    &time_mid,
                    &time_hi_and_version,
                    &clock_seq_hi_and_reserved,
                    &clock_seq_low,
                    &node[0],
                    &node[1],
                    &node[2],
                    &node[3],
                    &node[4],
                    &node[5],
                    thr_pid_buf
                    );
#endif /* ACE_HAS_TR24731_2005_CRT */

        if (nScanned != 12)
          {
            ACELIB_DEBUG ((LM_DEBUG,
                        "ACE_UUID::from_string_i - "
                        "IllegalArgument (invalid string representation)\n"));
            return;
          }
      }

    this->uuid_.time_low_ = static_cast<ACE_UINT32> (time_low);
    this->uuid_.time_mid_ = static_cast<ACE_UINT16> (time_mid);
    this->uuid_.time_hi_and_version_ = static_cast<ACE_UINT16> (time_hi_and_version);
    this->uuid_.clock_seq_hi_and_reserved_ = static_cast<u_char> (clock_seq_hi_and_reserved);
    this->uuid_.clock_seq_low_ = static_cast<u_char> (clock_seq_low);

    for (size_t i = 0; i < UUID_Node::NODE_ID_SIZE; ++ i)
      this->uuid_.node_.node_ID ()[i] = static_cast <u_char> (node[i]);

    // Support varient 10- only
    if ((this->uuid_.clock_seq_hi_and_reserved_ & 0xc0) != 0x80 &&
        (this->uuid_.clock_seq_hi_and_reserved_ & 0xc0) != 0xc0)
      {
        ACELIB_DEBUG ((LM_DEBUG,
                    "ACE_UUID::from_string_i - "
                    "IllegalArgument (unsupported variant)\n"));
        return;
      }

    /// Support versions 1, 3, and 4 only
    ACE_UINT16 V1 = this->uuid_.time_hi_and_version_;

    if ((V1 & 0xF000) != 0x1000 &&
        (V1 & 0xF000) != 0x3000 &&
        (V1 & 0xF000) != 0x4000)
      {
        ACELIB_DEBUG ((LM_DEBUG,
                    "ACE_UUID::from_string_i - "
                    "IllegalArgument (unsupported version)\n"));
        return;
      }

    if ((this->uuid_.clock_seq_hi_and_reserved_ & 0xc0) == 0xc0)
      {
        if (uuid_string.length () == NIL_UUID.to_string ()->length ())
          {
            ACELIB_DEBUG ((LM_DEBUG,
                      "ACE_UUID::from_string_i - "
                        "IllegalArgument (Missing Thread and Process Id)\n"));
            return;
          }
        ACE_CString thr_pid_str (thr_pid_buf);
        ssize_t pos = static_cast<ssize_t> (thr_pid_str.find ('-'));
        if (pos == -1)
          ACELIB_DEBUG ((LM_DEBUG,
                      "ACE_UUID::from_string_i - "
                      "IllegalArgument (Thread and Process Id format incorrect)\n"));

        this->thr_id_ = thr_pid_str.substr (0, pos);
        this->pid_ = thr_pid_str.substr (pos+1, thr_pid_str.length ()-pos-1);
      }
  }
#endif // ACE_LACKS_SSCANF

  UUID_Generator::UUID_Generator ()
    : time_last_ (0),
      destroy_lock_ (true),
      is_init_ (false)
  {
    ACE_NEW (lock_, ACE_SYNCH_MUTEX);
    this->init ();
  }

  UUID_Generator::~UUID_Generator ()
  {
    if (destroy_lock_)
      delete lock_;
  }

  void
  UUID_Generator::init ()
  {
    if (this->is_init_)
      return;

    ACE_OS::macaddr_node_t macaddress;
    int const result = ACE_OS::getmacaddress (&macaddress);

    UUID_Node::Node_ID node_id;

    if (-1 != result)
      {
        ACE_OS::memcpy (node_id,
                        macaddress.node,
                        UUID_Node::NODE_ID_SIZE);
      }
    else
      {
        node_id [0] = static_cast<u_char> (ACE_OS::rand ());
        node_id [1] = static_cast<u_char> (ACE_OS::rand ());
        node_id [2] = static_cast<u_char> (ACE_OS::rand ());
        node_id [3] = static_cast<u_char> (ACE_OS::rand ());
        node_id [4] = static_cast<u_char> (ACE_OS::rand ());
        node_id [5] = static_cast<u_char> (ACE_OS::rand ());
      }

    this->get_timestamp (time_last_);

    {
      ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, *lock_);
      uuid_state_.timestamp = time_last_;

      ACE_OS::memcpy (uuid_state_.node.node_ID (),
                      node_id,
                      UUID_Node::NODE_ID_SIZE);
    }

    this->is_init_ = true;
  }

  void
  UUID_Generator::
  generate_UUID (UUID& uuid, ACE_UINT16 version, u_char variant)
  {
    UUID_Time timestamp = 0;
    ACE_UINT16 clock_sequence = 0;

    this->get_timestamp_and_clocksequence (timestamp,
                                           clock_sequence);

    // Construct a Version 1 UUID with the information in the arguements.
    uuid.time_low (static_cast<ACE_UINT32> (timestamp & 0xFFFFFFFF));
    uuid.time_mid (static_cast<ACE_UINT16> ((timestamp >> 32) & 0xFFFF));

    ACE_UINT16 tHAV = static_cast<ACE_UINT16> ((timestamp >> 48) & 0xFFFF);
    tHAV = static_cast<ACE_UINT16> (tHAV | (version << 12));
    uuid.time_hi_and_version (tHAV);

    u_char cseqHAV;
    uuid.clock_seq_low (static_cast<u_char> (clock_sequence & 0xFF));
    cseqHAV = static_cast<u_char> ((clock_sequence & 0x3f00) >> 8);
    uuid_state_.timestamp = timestamp;

    cseqHAV = static_cast<u_char> (cseqHAV | variant);
    uuid.clock_seq_hi_and_reserved (cseqHAV);
    uuid.node (uuid_state_.node);

    if (variant == 0xc0)
      {
        ACE_Thread_ID thread_id;
        char buf [BUFSIZ];
        thread_id.to_string (buf, BUFSIZ);
        uuid.thr_id (buf);

        ACE_OS::snprintf (buf, BUFSIZ, "%d",
                          static_cast<int> (ACE_OS::getpid ()));
        uuid.pid (buf);
      }
  }

  UUID*
  UUID_Generator::generate_UUID (ACE_UINT16 version, u_char variant)
  {
    UUID* uuid = 0;
    ACE_NEW_RETURN (uuid,
                    UUID,
                    0);

    this->generate_UUID (*uuid, version, variant);
    return uuid;
  }

  /// Obtain a new timestamp. If UUID's are being generated too quickly
  /// the clock sequence will be incremented
  void
  UUID_Generator::get_timestamp (UUID_Time& timestamp)
  {
    ACE_GUARD (ACE_SYNCH_MUTEX, mon, *lock_);

    this->get_systemtime (timestamp);

    // Account for the clock being set back. Increment the clock /
    // sequence.
    if (timestamp <= time_last_)
      {
        uuid_state_.clock_sequence = static_cast<ACE_UINT16>
          ((uuid_state_.clock_sequence + 1) & ACE_UUID_CLOCK_SEQ_MASK);
      }
    // If the system time ticked since the last UUID was
    // generated. Set / the clock sequence back.
    else if (timestamp > time_last_)
      {
        uuid_state_.clock_sequence = 0;
      }

    time_last_ = timestamp;
  }

  void
  UUID_Generator::get_timestamp_and_clocksequence (UUID_Time& timestamp,
                                                   ACE_UINT16& clock_sequence)
  {
    ACE_GUARD (ACE_SYNCH_MUTEX, mon, *lock_);

    this->get_systemtime (timestamp);

    // Account for the clock being set back. Increment the clock /
    // sequence.
    if (timestamp <= time_last_)
      uuid_state_.clock_sequence = static_cast<ACE_UINT16> ((uuid_state_.clock_sequence + 1) & ACE_UUID_CLOCK_SEQ_MASK);

    // If the system time ticked since the last UUID was
    // generated. Set / the clock sequence back.
    else if (timestamp > time_last_)
      uuid_state_.clock_sequence = 0;

    time_last_ = timestamp;
    clock_sequence = uuid_state_.clock_sequence;
  }

  /**
   * ACE_Time_Value is in POSIX time, seconds since Jan 1, 1970. UUIDs use
   * time in 100ns ticks since 15 October 1582. The difference is:
   *   15 Oct 1582 - 1 Jan 1600: 17 days in Oct, 30 in Nov,  31 in Dec +
   *       17 years and 4 leap days (1584, 88, 92 and 96)
   *   1 Jan 1600 - 1 Jan 1900: 3 centuries + 73 leap days ( 25 in 17th cent.
   *       and 24 each in 18th and 19th centuries)
   *   1 Jan 1900 - 1 Jan 1970: 70 years + 17 leap days.
   * This adds up, in days: (17+30+31+365*17+4)+ (365*300+73)+ (365*70+17) or
   * 122192928000000000U (0x1B21DD213814000) 100 ns ticks.
   */
  void
  UUID_Generator::get_systemtime (UUID_Time & timestamp)
  {
    const UUID_Time timeOffset =
      ACE_UINT64_LITERAL (0x1B21DD213814000);

    /// Get the time of day, convert to 100ns ticks then add the offset.
    ACE_Time_Value now = ACE_OS::gettimeofday ();
    ACE_UINT64 time;
    now.to_usec (time);
    time = time * 10;
    timestamp = time + timeOffset;
  }

  ACE_SYNCH_MUTEX*
  UUID_Generator::lock ()
  {
    return this->lock_;
  }

  void
  UUID_Generator::lock (ACE_SYNCH_MUTEX* lock, bool release_lock)
  {
    if (this->destroy_lock_)
      delete this->lock_;

    this->lock_ = lock;
    this->destroy_lock_ = release_lock;
  }
}

ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, ACE_Utils::UUID_Generator, ACE_SYNCH_MUTEX);

ACE_END_VERSIONED_NAMESPACE_DECL