Commit | Line | Data |
---|---|---|
c70636a7 MJ |
1 | /* |
2 | * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com> | |
4 | * | |
ab5be9fa | 5 | * SPDX-License-Identifier: LGPL-2.1-only |
c70636a7 | 6 | * |
c70636a7 MJ |
7 | */ |
8 | ||
c9e313bc | 9 | #include <common/compat/string.hpp> |
c70636a7 MJ |
10 | #include <stddef.h> |
11 | #include <stdint.h> | |
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | #include <time.h> | |
16 | ||
c9e313bc | 17 | #include "uuid.hpp" |
c70636a7 | 18 | |
328c2fe7 JG |
19 | namespace { |
20 | const lttng_uuid nil_uuid = {}; | |
21 | bool lttng_uuid_is_init; | |
22 | } /* namespace */ | |
c70636a7 | 23 | |
328c2fe7 | 24 | void lttng_uuid_to_str(const lttng_uuid& uuid, char *uuid_str) |
c70636a7 MJ |
25 | { |
26 | sprintf(uuid_str, LTTNG_UUID_FMT, LTTNG_UUID_FMT_VALUES(uuid)); | |
27 | } | |
28 | ||
23083fa0 JG |
29 | std::string lttng::utils::uuid_to_str(const lttng_uuid& uuid) |
30 | { | |
31 | std::string uuid_str(LTTNG_UUID_STR_LEN, '\0'); | |
32 | ||
33 | ::lttng_uuid_to_str(uuid, &uuid_str[0]); | |
34 | ||
35 | /* Don't include '\0' in the C++ string. */ | |
36 | uuid_str.resize(uuid_str.size() - 1); | |
37 | ||
38 | return uuid_str; | |
39 | } | |
40 | ||
328c2fe7 | 41 | int lttng_uuid_from_str(const char *str_in, lttng_uuid& uuid_out) |
c70636a7 MJ |
42 | { |
43 | int ret = 0; | |
44 | lttng_uuid uuid_scan; | |
45 | ||
328c2fe7 | 46 | if (str_in == nullptr) { |
c70636a7 MJ |
47 | ret = -1; |
48 | goto end; | |
49 | } | |
50 | ||
a1298db6 | 51 | if (lttng_strnlen(str_in, LTTNG_UUID_STR_LEN) != LTTNG_UUID_STR_LEN - 1) { |
c70636a7 MJ |
52 | ret = -1; |
53 | goto end; | |
54 | } | |
55 | ||
56 | /* Scan to a temporary location in case of a partial match. */ | |
57 | if (sscanf(str_in, LTTNG_UUID_FMT, LTTNG_UUID_SCAN_VALUES(uuid_scan)) != | |
58 | LTTNG_UUID_LEN) { | |
59 | ret = -1; | |
52cb8c0f | 60 | goto end; |
c70636a7 MJ |
61 | } |
62 | ||
328c2fe7 | 63 | uuid_out = uuid_scan; |
c70636a7 MJ |
64 | end: |
65 | return ret; | |
66 | } | |
67 | ||
328c2fe7 | 68 | bool lttng_uuid_is_nil(const lttng_uuid& uuid) |
c70636a7 | 69 | { |
328c2fe7 | 70 | return uuid == nil_uuid; |
c70636a7 MJ |
71 | } |
72 | ||
73 | /* | |
74 | * Generate a random UUID according to RFC4122, section 4.4. | |
75 | */ | |
328c2fe7 | 76 | int lttng_uuid_generate(lttng_uuid& uuid_out) |
c70636a7 MJ |
77 | { |
78 | int i, ret = 0; | |
79 | ||
c70636a7 MJ |
80 | if (!lttng_uuid_is_init) { |
81 | /* | |
82 | * We don't need cryptographic quality randomness to | |
83 | * generate UUIDs, seed rand with the epoch. | |
84 | */ | |
85 | const time_t epoch = time(NULL); | |
86 | ||
87 | if (epoch == (time_t) -1) { | |
88 | ret = -1; | |
89 | goto end; | |
90 | } | |
c70636a7 | 91 | |
aeeb48c6 | 92 | srand(epoch); |
c70636a7 MJ |
93 | lttng_uuid_is_init = true; |
94 | } | |
95 | ||
96 | /* | |
97 | * Generate 16 bytes of random bits. | |
98 | */ | |
99 | for (i = 0; i < LTTNG_UUID_LEN; i++) { | |
100 | uuid_out[i] = (uint8_t) rand(); | |
101 | } | |
102 | ||
103 | /* | |
104 | * Set the two most significant bits (bits 6 and 7) of the | |
105 | * clock_seq_hi_and_reserved to zero and one, respectively. | |
106 | */ | |
107 | uuid_out[8] &= ~(1 << 6); | |
108 | uuid_out[8] |= (1 << 7); | |
109 | ||
110 | /* | |
111 | * Set the four most significant bits (bits 12 through 15) of the | |
112 | * time_hi_and_version field to the 4-bit version number from | |
113 | * Section 4.1.3. | |
114 | */ | |
115 | uuid_out[6] &= 0x0f; | |
116 | uuid_out[6] |= (LTTNG_UUID_VER << 4); | |
117 | ||
118 | end: | |
119 | return ret; | |
120 | } |