master
c 52 lines 1.27 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "../libnetdata.h"
4 #undef uuid_generate
5 #undef uuid_generate_random
6 #undef uuid_generate_time
7
8 #ifdef OS_WINDOWS
9 void os_uuid_generate(nd_uuid_t out) {
10 // nd_uuid_t is byte-aligned storage, so use aligned local UUID storage
11 // before copying the generated bytes to the caller's output buffer.
12 UUID uuid;
13 RPC_STATUS status = UuidCreate(&uuid);
14 while (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY) {
15 tinysleep();
16 status = UuidCreate(&uuid);
17 }
18 memcpy(out, &uuid, sizeof(uuid));
19 }
20
21 void os_uuid_generate_random(nd_uuid_t out) {
22 os_uuid_generate(out);
23 }
24
25 void os_uuid_generate_time(nd_uuid_t out) {
26 os_uuid_generate(out);
27 }
28
29 #else
30
31 #if defined(OS_MACOS)
32 #include <uuid/uuid.h>
33 #else
34 #include <uuid.h>
35 #endif
36
37 void os_uuid_generate(nd_uuid_t out) {
38 // IMPORTANT: this generates a UUIDv4, which is random
39 // and falls back to uuid_generate_time() if high resolution random generated is not available
40 uuid_generate(out);
41 }
42
43 void os_uuid_generate_random(nd_uuid_t out) {
44 uuid_generate_random(out);
45 }
46
47 void os_uuid_generate_time(nd_uuid_t out) {
48 // IMPORTANT: this generates a UUIDv1, which is not random and may suffer from collisions
49 uuid_generate_time(out);
50 }
51
52 #endif