From 0f1e713439da5668d0ccea7f2a557ad74102a14d Mon Sep 17 00:00:00 2001 From: Aron Podrigal Date: Fri, 12 Sep 2025 11:47:33 -0500 Subject: [PATCH] Add support for UUID v4 and v7 generation Provide functionality to generate UUIDs of specific versions (4 and 7) via the new `switch_uuid_generate_version` API. Enhances the `create_uuid` command to allow specifying the desired UUID version, ensuring greater flexibility for UUID generation. --- src/include/switch_apr.h | 8 +++++ src/include/switch_utils.h | 1 + .../applications/mod_commands/mod_commands.c | 14 ++++++-- src/switch_apr.c | 34 ++++++++++++++++--- src/switch_utils.c | 15 ++++++++ 5 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/include/switch_apr.h b/src/include/switch_apr.h index 36f1531beff..2f46c55d060 100644 --- a/src/include/switch_apr.h +++ b/src/include/switch_apr.h @@ -562,6 +562,14 @@ SWITCH_DECLARE(void) switch_uuid_format(char *buffer, const switch_uuid_t *uuid) */ SWITCH_DECLARE(void) switch_uuid_get(switch_uuid_t *uuid); +/** + * + * @param uuid + * @param version + * @return switch_status_t + */ +SWITCH_DECLARE(switch_status_t) switch_uuid_generate_version(switch_uuid_t *uuid, int version); + /** * Parse a standard-format string into a UUID * @param uuid The resulting UUID diff --git a/src/include/switch_utils.h b/src/include/switch_utils.h index a0c91e6384f..71d8478d13c 100644 --- a/src/include/switch_utils.h +++ b/src/include/switch_utils.h @@ -1422,6 +1422,7 @@ SWITCH_DECLARE(const char *) switch_inet_ntop(int af, void const *src, char *dst #endif SWITCH_DECLARE(char *) switch_uuid_str(char *buf, switch_size_t len); +SWITCH_DECLARE(char *) switch_uuid_str_version(char *buf, switch_size_t len, int version); SWITCH_DECLARE(char *) switch_format_number(const char *num); SWITCH_DECLARE(unsigned int) switch_atoui(const char *nptr); diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index 9184a78b93b..33c33ac73e4 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -3173,11 +3173,21 @@ SWITCH_STANDARD_API(tone_detect_session_function) return SWITCH_STATUS_SUCCESS; } +#define UUID_GENERATE_SYNTAX " [4|7]" SWITCH_STANDARD_API(uuid_function) { char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; - switch_uuid_str(uuid_str, sizeof(uuid_str)); + int version = 0; + if (!zstr(cmd)) { + version = atoi(cmd); + } + + if (version) { + switch_uuid_str_version(uuid_str, sizeof(uuid_str), version); + } else { + switch_uuid_str(uuid_str, sizeof(uuid_str)); + } stream->write_function(stream, "%s", uuid_str); return SWITCH_STATUS_SUCCESS; @@ -7609,7 +7619,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load) SWITCH_ADD_API(commands_api_interface, "cond", "Evaluate a conditional", cond_function, " ? : "); SWITCH_ADD_API(commands_api_interface, "console_complete", "", console_complete_function, ""); SWITCH_ADD_API(commands_api_interface, "console_complete_xml", "", console_complete_xml_function, ""); - SWITCH_ADD_API(commands_api_interface, "create_uuid", "Create a uuid", uuid_function, UUID_SYNTAX); + SWITCH_ADD_API(commands_api_interface, "create_uuid", "Create a uuid", uuid_function, UUID_GENERATE_SYNTAX); SWITCH_ADD_API(commands_api_interface, "db_cache", "Manage db cache", db_cache_function, "status"); SWITCH_ADD_API(commands_api_interface, "domain_data", "Find domain data", domain_data_function, " [var|param|attr] "); SWITCH_ADD_API(commands_api_interface, "domain_exists", "Check if a domain exists", domain_exists_function, ""); diff --git a/src/switch_apr.c b/src/switch_apr.c index 60660d6e2eb..e5bad8403f3 100644 --- a/src/switch_apr.c +++ b/src/switch_apr.c @@ -1151,17 +1151,41 @@ SWITCH_DECLARE(void) switch_uuid_format(char *buffer, const switch_uuid_t *uuid) #endif } +#ifndef WIN32 +#define uuidv4_new(uuid) uuid_generate(uuid->data); +#else +#define uuidv4_new(uuid) UuidCreate((UUID *)uuid); +#endif + +SWITCH_DECLARE(switch_status_t) switch_uuid_generate_version(switch_uuid_t *uuid, int version) +{ + switch_status_t status = SWITCH_STATUS_SUCCESS; + switch_mutex_lock(runtime.uuid_mutex); + + switch(version) { + case 4: + uuidv4_new(uuid); + break; + case 7: + uuidv7_new(uuid->data); + break; + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ERROR: Unsupported UUID version %d\n", version); + status = SWITCH_STATUS_FALSE; + } + switch_mutex_unlock(runtime.uuid_mutex); + + return status; +} + + SWITCH_DECLARE(void) switch_uuid_get(switch_uuid_t *uuid) { switch_mutex_lock(runtime.uuid_mutex); if (runtime.uuid_version == 7) { uuidv7_new(uuid->data); } else { -#ifndef WIN32 - uuid_generate(uuid->data); -#else - UuidCreate((UUID *)uuid); -#endif + uuidv4_new(uuid); } switch_mutex_unlock(runtime.uuid_mutex); diff --git a/src/switch_utils.c b/src/switch_utils.c index 206f99218a1..b80896ee064 100644 --- a/src/switch_utils.c +++ b/src/switch_utils.c @@ -4139,6 +4139,21 @@ SWITCH_DECLARE(char *) switch_uuid_str(char *buf, switch_size_t len) return buf; } +SWITCH_DECLARE(char *) switch_uuid_str_version(char *buf, switch_size_t len, int version) +{ + switch_uuid_t uuid; + + if (len < (SWITCH_UUID_FORMATTED_LENGTH + 1)) { + switch_snprintf(buf, len, "INVALID"); + } else if (switch_uuid_generate_version(&uuid, version) != SWITCH_STATUS_SUCCESS) { + switch_snprintf(buf, len, "INVALID"); + } else { + switch_uuid_format(buf, &uuid); + } + + return buf; +} + SWITCH_DECLARE(char *) switch_format_number(const char *num) {