Skip to content

Commit 47a5993

Browse files
committed
Rework.
1 parent f019422 commit 47a5993

File tree

8 files changed

+56
-15
lines changed

8 files changed

+56
-15
lines changed

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ library_include_HEADERS = \
300300
libs/libteletone/src/libteletone_detect.h \
301301
libs/libteletone/src/libteletone_generate.h \
302302
libs/libteletone/src/libteletone.h \
303-
src/include/switch_uuidv7.h \
304303
src/include/switch_limit.h \
305304
src/include/switch_odbc.h \
306305
src/include/switch_hashtable.h \
@@ -812,3 +811,4 @@ support:
812811
@cp support-d/.screenrc ~
813812
@cp support-d/.bashrc ~
814813
@test -f ~/.cc-mode-installed || sh support-d/install-cc-mode.sh && touch ~/.cc-mode-installed
814+

src/include/switch_uuidv7.h renamed to src/include/private/switch_uuidv7_pvt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ static inline int uuidv7_from_string(const char *string, uint8_t *uuid_out) {
283283
* process. The implementation-dependent code must be out of
284284
* the range of `int8_t` and negative if it reports an error.
285285
*/
286-
int uuidv7_new(uint8_t *uuid_out);
286+
SWITCH_DECLARE(int) uuidv7_new(uint8_t *uuid_out);
287287

288288
/**
289289
* Generates an 8-4-4-4-12 hexadecimal string representation of new UUIDv7.

src/mod/applications/mod_commands/mod_commands.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,7 @@ SWITCH_STANDARD_API(ctl_function)
26602660
} else {
26612661
arg = 0;
26622662
}
2663+
26632664
switch_core_session_ctl(SCSC_SPS, &arg);
26642665
stream->write_function(stream, "+OK sessions per second: %d\n", arg);
26652666
} else if (!strcasecmp(argv[0], "uuid_version")) {
@@ -2668,6 +2669,7 @@ SWITCH_STANDARD_API(ctl_function)
26682669
} else {
26692670
arg = 0;
26702671
}
2672+
26712673
switch_core_session_ctl(SCSC_UUID_VERSION, &arg);
26722674
stream->write_function(stream, "+OK set uuid version: %d\n", arg);
26732675
} else if (!strcasecmp(argv[0], "sync_clock")) {

src/switch_apr.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
#ifndef WIN32
9090
#include <uuid/uuid.h>
9191
#endif
92-
#include <switch_uuidv7.h>
92+
93+
#include <private/switch_uuidv7_pvt.h>
9394

9495
/* apr stubs */
9596

@@ -1153,15 +1154,16 @@ SWITCH_DECLARE(void) switch_uuid_format(char *buffer, const switch_uuid_t *uuid)
11531154
SWITCH_DECLARE(void) switch_uuid_get(switch_uuid_t *uuid)
11541155
{
11551156
switch_mutex_lock(runtime.uuid_mutex);
1156-
#ifndef WIN32
11571157
if (runtime.uuid_version == 7) {
11581158
uuidv7_new(uuid->data);
11591159
} else {
1160+
#ifndef WIN32
11601161
uuid_generate(uuid->data);
1161-
}
11621162
#else
1163-
UuidCreate((UUID *) uuid);
1163+
UuidCreate((UUID *)uuid);
11641164
#endif
1165+
}
1166+
11651167
switch_mutex_unlock(runtime.uuid_mutex);
11661168
}
11671169

src/switch_core.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2963,13 +2963,15 @@ SWITCH_DECLARE(int32_t) switch_core_session_ctl(switch_session_ctl_t cmd, void *
29632963
if (oldintval > 0) {
29642964
runtime.sps_total = oldintval;
29652965
}
2966+
29662967
newintval = runtime.sps_total;
29672968
switch_mutex_unlock(runtime.throttle_mutex);
29682969
break;
29692970
case SCSC_UUID_VERSION:
29702971
if(oldintval > 0){
29712972
runtime.uuid_version = oldintval;
29722973
}
2974+
29732975
newintval = runtime.uuid_version;
29742976
break;
29752977
case SCSC_RECLAIM:

src/switch_uuidv7.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@
3030
*/
3131

3232
#include <switch.h>
33-
#include "switch_uuidv7.h"
33+
#include "private/switch_uuidv7_pvt.h"
3434
#ifdef __APPLE__
35-
#include <sys/random.h> // for macOS getentropy()
35+
#include <sys/random.h> /* for macOS getentropy() */
36+
#endif
37+
#ifdef _MSC_VER
38+
#include <bcrypt.h> /* for BCryptGenRandom */
3639
#endif
3740

3841
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
3942
#define SWITCH_THREAD_LOCAL static _Thread_local
4043
#else
41-
// Fallback to compiler-specific or other methods
44+
/* Fallback to compiler-specific or other methods */
4245
#ifdef _MSC_VER
4346
#define SWITCH_THREAD_LOCAL __declspec(thread)
4447
#elif defined(__GNUC__)
@@ -56,6 +59,24 @@ SWITCH_THREAD_LOCAL uint8_t rand_bytes[10] = {0};
5659
SWITCH_THREAD_LOCAL size_t n_rand_consumed = 10;
5760
#endif
5861

62+
static void switch_getentropy(unsigned char *rand_bytes, size_t n_rand_consumed) {
63+
#ifdef _MSC_VER
64+
NTSTATUS status = BCryptGenRandom(
65+
NULL, /* Algorithm handle (NULL for system-preferred RNG) */
66+
rand_bytes, /* Buffer to receive random bytes */
67+
(ULONG)n_rand_consumed, /* Size of buffer in bytes */
68+
BCRYPT_USE_SYSTEM_PREFERRED_RNG /* Flag for system RNG */
69+
);
70+
if (status != 0) {
71+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error generating random bytes: 0x%lx\n", status);
72+
}
73+
#else
74+
if (getentropy(rand_bytes, n_rand_consumed) != 0) {
75+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "getentropy failed");
76+
}
77+
#endif
78+
}
79+
5980
SWITCH_DECLARE(int) uuidv7_new(uint8_t *uuid_out)
6081
{
6182
int8_t status;
@@ -66,7 +87,7 @@ SWITCH_DECLARE(int) uuidv7_new(uint8_t *uuid_out)
6687
uint8_t rand_bytes[10] = {0};
6788
#endif
6889

69-
getentropy(rand_bytes, n_rand_consumed);
90+
switch_getentropy(rand_bytes, n_rand_consumed);
7091
#ifdef SWITCH_THREAD_LOCAL_NOT_SUPPORTED
7192
status = uuidv7_generate(uuid_out, unix_ts_ms, rand_bytes, NULL);
7293
#else
@@ -77,6 +98,7 @@ SWITCH_DECLARE(int) uuidv7_new(uint8_t *uuid_out)
7798
} else if (status == UUIDV7_STATUS_CLOCK_ROLLBACK) {
7899
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "uuidv7_generate: clock rollback detected\n");
79100
}
101+
80102
#ifdef SWITCH_THREAD_LOCAL_NOT_SUPPORTED
81103
return status;
82104
#else

tests/unit/switch_core.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
#include <test/switch_test.h>
3434

3535
#include <string.h>
36-
#include <uuid/uuid.h>
37-
#include <switch_uuidv7.h>
36+
#include <private/switch_uuidv7_pvt.h>
3837

3938
#if defined(HAVE_OPENSSL)
4039
#include <openssl/ssl.h>
@@ -621,14 +620,20 @@ FST_CORE_BEGIN("./conf")
621620
{
622621
switch_uuid_t uuid;
623622
char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1] = { 0 };
623+
int version = 7;
624+
624625
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "test_create_uuid:\n");
626+
switch_core_session_ctl(SCSC_UUID_VERSION, &version);
625627
for (int i = 0; i < 100; i++) {
626628
uint8_t status = uuidv7_new(uuid.data);
627629
switch_uuid_format(uuid_str, &uuid);
628630
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "uuidv7: %s\n", uuid_str);
629631
fst_check(status >=0 && status < 4);
630632
}
631-
uuid_generate(uuid.data);
633+
634+
version = 4;
635+
switch_core_session_ctl(SCSC_UUID_VERSION, &version);
636+
switch_uuid_get(&uuid);
632637
switch_uuid_format(uuid_str, &uuid);
633638
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "uuidv4: %s\n", uuid_str);
634639
}
@@ -640,7 +645,9 @@ FST_CORE_BEGIN("./conf")
640645
switch_thread_t *thread[10] = {0};
641646
switch_threadattr_t *thd_attr = NULL;
642647
switch_status_t status;
648+
int version = 7;
643649

650+
switch_core_session_ctl(SCSC_UUID_VERSION, &version);
644651
switch_threadattr_create(&thd_attr, fst_pool);
645652
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
646653

@@ -657,20 +664,24 @@ FST_CORE_BEGIN("./conf")
657664
FST_TEST_BEGIN(test_create_uuid_speed)
658665
{
659666
int n;
667+
int version = 4;
668+
660669
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "test_create_uuid_speed:\n");
670+
switch_core_session_ctl(SCSC_UUID_VERSION, &version);
661671
for (n = 1; n < 4; n++) {
662672
switch_time_t started_at = switch_time_now();
663673
double delta = 0;
664674
switch_uuid_t uuid;
675+
665676
for (int i = 0; i < 1000 * pow(10, n); i++) {
666677
uuidv7_new(uuid.data);
667678
}
668679
delta = (switch_time_now() - started_at) / 1000000.0;
669680
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%d uuidv7_new used: %f seconds\n", 1000 * (int)pow(10, n), delta);
670-
681+
671682
started_at = switch_time_now();
672683
for (long long int i = 0; i < 1000 * pow(10, n); i++) {
673-
uuid_generate(uuid.data);
684+
switch_uuid_get(&uuid);
674685
}
675686
delta = (switch_time_now() - started_at) / 1000000.0;
676687
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%d uuid_generate used: %f seconds\n", 1000 * (int)pow(10, n), delta);

w32/Library/FreeSwitchCore.2017.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ if not exist "$(OutDir)fonts" xcopy "$(SolutionDir)fonts\*.*" "$(OutDir)fonts\"
412412
<ClCompile Include="..\..\src\switch_stun.c" />
413413
<ClCompile Include="..\..\src\switch_time.c" />
414414
<ClCompile Include="..\..\src\switch_utils.c" />
415+
<ClCompile Include="..\..\src\switch_uuidv7.c" />
415416
<ClCompile Include="..\..\src\switch_vad.c" />
416417
<ClCompile Include="..\..\src\switch_version.c" />
417418
<ClCompile Include="..\..\src\switch_vpx.c" />
@@ -748,6 +749,7 @@ if not exist "$(OutDir)fonts" xcopy "$(SolutionDir)fonts\*.*" "$(OutDir)fonts\"
748749
<ClInclude Include="..\..\src\include\switch_stun.h" />
749750
<ClInclude Include="..\..\src\include\switch_types.h" />
750751
<ClInclude Include="..\..\src\include\switch_utils.h" />
752+
<ClInclude Include="..\..\src\include\private\switch_uuidv7_pvt.h" />
751753
<ClInclude Include="..\..\src\include\switch_vad.h" />
752754
<ClInclude Include="..\..\src\include\switch_version.h" />
753755
<ClInclude Include="..\..\src\include\switch_vpx.h" />

0 commit comments

Comments
 (0)