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