diff options
Diffstat (limited to 'src/libnetdata/uuid')
-rw-r--r-- | src/libnetdata/uuid/README.md | 13 | ||||
-rw-r--r-- | src/libnetdata/uuid/uuid.c | 220 | ||||
-rw-r--r-- | src/libnetdata/uuid/uuid.h | 112 |
3 files changed, 345 insertions, 0 deletions
diff --git a/src/libnetdata/uuid/README.md b/src/libnetdata/uuid/README.md new file mode 100644 index 00000000..a0da380a --- /dev/null +++ b/src/libnetdata/uuid/README.md @@ -0,0 +1,13 @@ +<!-- +title: "UUID" +custom_edit_url: https://github.com/netdata/netdata/edit/master/src/libnetdata/uuid/README.md +sidebar_label: "UUID" +learn_topic_type: "Tasks" +learn_rel_path: "Developers/libnetdata" +--> + +# UUID + +Netdata uses libuuid for managing UUIDs. + +In this folder are a few custom helpers.
\ No newline at end of file diff --git a/src/libnetdata/uuid/uuid.c b/src/libnetdata/uuid/uuid.c new file mode 100644 index 00000000..6b05229f --- /dev/null +++ b/src/libnetdata/uuid/uuid.c @@ -0,0 +1,220 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "../libnetdata.h" + +ND_UUID UUID_generate_from_hash(const void *payload, size_t payload_len) { + assert(sizeof(XXH128_hash_t) == sizeof(ND_UUID)); + + ND_UUID uuid = UUID_ZERO; + XXH128_hash_t *xxh3_128 = (XXH128_hash_t *)&uuid; + + // Hash the payload using XXH128 + // Assume xxh128_hash_function is your function to generate XXH128 hash + *xxh3_128 = XXH3_128bits(payload, payload_len); + + // Set the UUID version (here, setting it to 4) + uuid.uuid[6] = (uuid.uuid[6] & 0x0F) | 0x40; // Version 4 + + // Set the UUID variant (standard variant for UUID) + uuid.uuid[8] = (uuid.uuid[8] & 0x3F) | 0x80; // Variant is 10xxxxxx + + return uuid; +} + +void uuid_unparse_lower_compact(const nd_uuid_t uuid, char *out) { + static const char *hex_chars = "0123456789abcdef"; + for (int i = 0; i < 16; i++) { + out[i * 2] = hex_chars[(uuid[i] >> 4) & 0x0F]; + out[i * 2 + 1] = hex_chars[uuid[i] & 0x0F]; + } + out[32] = '\0'; // Null-terminate the string +} + +static inline void nd_uuid_unparse_full(const nd_uuid_t uuid, char *out, const char *hex_chars) { + int shifts = 0; + for (int i = 0; i < 16; i++) { + if (i == 4 || i == 6 || i == 8 || i == 10) { + out[i * 2 + shifts] = '-'; + shifts++; + } + out[i * 2 + shifts] = hex_chars[(uuid[i] >> 4) & 0x0F]; + out[i * 2 + 1 + shifts] = hex_chars[uuid[i] & 0x0F]; + } + out[36] = '\0'; // Null-terminate the string +} + +// Wrapper functions for lower and upper case hexadecimal representation +void nd_uuid_unparse_lower(const nd_uuid_t uuid, char *out) { + nd_uuid_unparse_full(uuid, out, "0123456789abcdef"); +} + +void nd_uuid_unparse_upper(const nd_uuid_t uuid, char *out) { + nd_uuid_unparse_full(uuid, out, "0123456789ABCDEF"); +} + +inline int uuid_parse_compact(const char *in, nd_uuid_t uuid) { + if (strlen(in) != 32) + return -1; // Invalid input length + + for (int i = 0; i < 16; i++) { + int high = hex_char_to_int(in[i * 2]); + int low = hex_char_to_int(in[i * 2 + 1]); + + if (high < 0 || low < 0) + return -1; // Invalid hexadecimal character + + uuid[i] = (high << 4) | low; + } + + return 0; // Success +} + +int uuid_parse_flexi(const char *in, nd_uuid_t uu) { + if(!in || !*in) + return -1; + + size_t hexCharCount = 0; + size_t hyphenCount = 0; + const char *s = in; + int byteIndex = 0; + nd_uuid_t uuid; // work on a temporary place, to not corrupt the previous value of uu if we fail + + while (*s && byteIndex < 16) { + if (*s == '-') { + s++; + hyphenCount++; + + if (unlikely(hyphenCount > 4)) + // Too many hyphens + return -2; + } + + if (likely(isxdigit((uint8_t)*s))) { + int high = hex_char_to_int(*s++); + hexCharCount++; + + if (likely(isxdigit((uint8_t)*s))) { + int low = hex_char_to_int(*s++); + hexCharCount++; + + uuid[byteIndex++] = (high << 4) | low; + } + else + // Not a valid UUID (expected a pair of hex digits) + return -3; + } + else + // Not a valid UUID + return -4; + } + + if (unlikely(byteIndex < 16)) + // Not enough data to form a UUID + return -5; + + if (unlikely(hexCharCount != 32)) + // wrong number of hex digits + return -6; + + if(unlikely(hyphenCount != 0 && hyphenCount != 4)) + // wrong number of hyphens + return -7; + + // copy the final value + memcpy(uu, uuid, sizeof(nd_uuid_t)); + + return 0; +} + + +// ---------------------------------------------------------------------------- +// unit test + +static inline void remove_hyphens(const char *uuid_with_hyphens, char *uuid_without_hyphens) { + while (*uuid_with_hyphens) { + if (*uuid_with_hyphens != '-') { + *uuid_without_hyphens++ = *uuid_with_hyphens; + } + uuid_with_hyphens++; + } + *uuid_without_hyphens = '\0'; +} + +int uuid_unittest(void) { + const int num_tests = 100000; + int failed_tests = 0; + + int i; + for (i = 0; i < num_tests; i++) { + nd_uuid_t original_uuid, parsed_uuid; + char uuid_str_with_hyphens[UUID_STR_LEN], uuid_str_without_hyphens[UUID_COMPACT_STR_LEN]; + + // Generate a random UUID + switch(i % 2) { + case 0: + uuid_generate(original_uuid); + break; + + case 1: + uuid_generate_random(original_uuid); + break; + } + + // Unparse it with hyphens + bool lower = false; + switch(i % 3) { + case 0: + uuid_unparse_lower(original_uuid, uuid_str_with_hyphens); + lower = true; + break; + + case 1: + uuid_unparse(original_uuid, uuid_str_with_hyphens); + break; + + case 2: + uuid_unparse_upper(original_uuid, uuid_str_with_hyphens); + break; + } + + // Remove the hyphens + remove_hyphens(uuid_str_with_hyphens, uuid_str_without_hyphens); + + if(lower) { + char test[UUID_COMPACT_STR_LEN]; + uuid_unparse_lower_compact(original_uuid, test); + if(strcmp(test, uuid_str_without_hyphens) != 0) { + printf("uuid_unparse_lower_compact() failed, expected '%s', got '%s'\n", + uuid_str_without_hyphens, test); + failed_tests++; + } + } + + // Parse the UUID string with hyphens + int parse_result = uuid_parse_flexi(uuid_str_with_hyphens, parsed_uuid); + if (parse_result != 0) { + printf("uuid_parse_flexi() returned -1 (parsing error) for UUID with hyphens: %s\n", uuid_str_with_hyphens); + failed_tests++; + } else if (uuid_compare(original_uuid, parsed_uuid) != 0) { + printf("uuid_parse_flexi() parsed value mismatch for UUID with hyphens: %s\n", uuid_str_with_hyphens); + failed_tests++; + } + + // Parse the UUID string without hyphens + parse_result = uuid_parse_flexi(uuid_str_without_hyphens, parsed_uuid); + if (parse_result != 0) { + printf("uuid_parse_flexi() returned -1 (parsing error) for UUID without hyphens: %s\n", uuid_str_without_hyphens); + failed_tests++; + } + else if(uuid_compare(original_uuid, parsed_uuid) != 0) { + printf("uuid_parse_flexi() parsed value mismatch for UUID without hyphens: %s\n", uuid_str_without_hyphens); + failed_tests++; + } + + if(failed_tests) + break; + } + + printf("UUID: failed %d out of %d tests.\n", failed_tests, i); + return failed_tests; +} diff --git a/src/libnetdata/uuid/uuid.h b/src/libnetdata/uuid/uuid.h new file mode 100644 index 00000000..cde45761 --- /dev/null +++ b/src/libnetdata/uuid/uuid.h @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef NETDATA_UUID_H +#define NETDATA_UUID_H + +// for compatibility with libuuid +typedef unsigned char nd_uuid_t[16]; + +// for quickly managing it as 2x 64-bit numbers +typedef struct _uuid { + union { + nd_uuid_t uuid; + struct { + uint64_t hig64; + uint64_t low64; + } parts; + }; +} ND_UUID; + +#ifdef __GNUC__ +#define ND_UUID_DEFINE(name,u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,u14,u15) \ + static const nd_uuid_t name __attribute__ ((unused)) = {u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,u14,u15} +#else +#define ND_UUID_DEFINE(name,u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,u14,u15) \ + static const nd_uuid_t name = {u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,u14,u15} +#endif + +static const ND_UUID UUID_ZERO = (ND_UUID){ { .parts = { .hig64 = 0, .low64 = 0 } }}; +ND_UUID_DEFINE(streaming_from_child_msgid, 0xed,0x4c,0xdb, 0x8f, 0x1b, 0xeb, 0x4a, 0xd3, 0xb5, 0x7c, 0xb3, 0xca, 0xe2, 0xd1, 0x62, 0xfa); +ND_UUID_DEFINE(streaming_to_parent_msgid, 0x6e, 0x2e, 0x38, 0x39, 0x06, 0x76, 0x48, 0x96, 0x8b, 0x64, 0x60, 0x45, 0xdb, 0xf2, 0x8d, 0x66); +ND_UUID_DEFINE(health_alert_transition_msgid, 0x9c, 0xe0, 0xcb, 0x58, 0xab, 0x8b, 0x44, 0xdf, 0x82, 0xc4, 0xbf, 0x1a, 0xd9, 0xee, 0x22, 0xde); + +// this is also defined in alarm-notify.sh.in +ND_UUID_DEFINE(health_alert_notification_msgid, 0x6d, 0xb0, 0x01, 0x8e, 0x83, 0xe3, 0x43, 0x20, 0xae, 0x2a, 0x65, 0x9d, 0x78, 0x01, 0x9f, 0xb7); + +ND_UUID UUID_generate_from_hash(const void *payload, size_t payload_len); + +#define UUIDeq(a, b) ((a).parts.hig64 == (b).parts.hig64 && (a).parts.low64 == (b).parts.low64) + +static inline ND_UUID uuid2UUID(const nd_uuid_t uu1) { + // uu1 may not be aligned, so copy it to the output + ND_UUID copy; + memcpy(copy.uuid, uu1, sizeof(nd_uuid_t)); + return copy; +} + +#ifndef UUID_STR_LEN +// CentOS 7 has older version that doesn't define this +// same goes for MacOS +#define UUID_STR_LEN 37 +#endif + +#define UUID_COMPACT_STR_LEN 33 + +void uuid_unparse_lower_compact(const nd_uuid_t uuid, char *out); +int uuid_parse_compact(const char *in, nd_uuid_t uuid); + +int uuid_parse_flexi(const char *in, nd_uuid_t uuid); +#define uuid_parse(in, uuid) uuid_parse_flexi(in, uuid) + +static inline int hex_char_to_int(char c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; // Invalid hexadecimal character +} + +static inline void nd_uuid_clear(nd_uuid_t uu) { + memset(uu, 0, sizeof(nd_uuid_t)); +} + +// Netdata does not need to sort UUIDs lexicographically and this kind +// of sorting does not need to be portable between little/big endian. +// So, any kind of sorting will work, as long as it compares UUIDs. +// The fastest possible, is good enough. +static inline int nd_uuid_compare(const nd_uuid_t uu1, const nd_uuid_t uu2) { + // IMPORTANT: + // uu1 or uu2 may not be aligned to word boundaries on this call, + // so casting this to a struct may give SIGBUS on some architectures. + return memcmp(uu1, uu2, sizeof(nd_uuid_t)); +} + +static inline void nd_uuid_copy(nd_uuid_t dst, const nd_uuid_t src) { + memcpy(dst, src, sizeof(nd_uuid_t)); +} + +static inline bool nd_uuid_eq(const nd_uuid_t uu1, const nd_uuid_t uu2) { + return nd_uuid_compare(uu1, uu2) == 0; +} + +static inline int nd_uuid_is_null(const nd_uuid_t uu) { + return nd_uuid_compare(uu, UUID_ZERO.uuid) == 0; +} + +void nd_uuid_unparse_lower(const nd_uuid_t uuid, char *out); +void nd_uuid_unparse_upper(const nd_uuid_t uuid, char *out); + +#define uuid_is_null(uu) nd_uuid_is_null(uu) +#define uuid_clear(uu) nd_uuid_clear(uu) +#define uuid_compare(uu1, uu2) nd_uuid_compare(uu1, uu2) +#define uuid_copy(dst, src) nd_uuid_copy(dst, src) +#define uuid_eq(uu1, uu2) nd_uuid_eq(uu1, uu2) + +#define uuid_generate(out) os_uuid_generate(out) +#define uuid_generate_random(out) os_uuid_generate_random(out) +#define uuid_generate_time(out) os_uuid_generate_time(out) + +#define uuid_unparse(uu, out) nd_uuid_unparse_lower(uu, out) +#define uuid_unparse_lower(uu, out) nd_uuid_unparse_lower(uu, out) +#define uuid_unparse_upper(uu, out) nd_uuid_unparse_upper(uu, out) + +#endif //NETDATA_UUID_H |