diff --git a/README.md b/README.md
index 2727f6d22..caa3d0267 100755
--- a/README.md
+++ b/README.md
@@ -340,6 +340,8 @@ Remark:
## History
+* v2.0, 2021-06-26, [2.0 release9(2.0.274)](https://github.com/ossrs/srs/releases/tag/v2.0-r10) released. 87575 lines.
+* v2.0, 2021-07-04, For [#2424](https://github.com/ossrs/srs/issues/2424), use srandom/random to generate. 2.0.274
* v2.0, 2021-06-26, [2.0 release9(2.0.273)](https://github.com/ossrs/srs/releases/tag/v2.0-r9) released. 87552 lines.
* v2.0, 2021-06-25, For [#2424](https://github.com/ossrs/srs/issues/2424), query the latest available version. 2.0.273
* v2.0, 2020-01-25, [2.0 release8(2.0.272)][r2.0r8] released. 87292 lines.
diff --git a/trunk/src/app/srs_app_ingest.cpp b/trunk/src/app/srs_app_ingest.cpp
index a30069ba3..a46615595 100644
--- a/trunk/src/app/srs_app_ingest.cpp
+++ b/trunk/src/app/srs_app_ingest.cpp
@@ -35,6 +35,7 @@ using namespace std;
#include
#include
#include
+#include
// when error, ingester sleep for a while and retry.
// ingest never sleep a long time, for we must start the stream ASAP.
@@ -436,7 +437,7 @@ void SrsIngester::show_ingest_log_message()
}
// random choose one ingester to report.
- int index = rand() % (int)ingesters.size();
+ int index = srs_random() % (int)ingesters.size();
SrsIngesterFFMPEG* ingester = ingesters.at(index);
// reportable
diff --git a/trunk/src/app/srs_app_latest_version.cpp b/trunk/src/app/srs_app_latest_version.cpp
index 1c92878bb..86438a6b8 100644
--- a/trunk/src/app/srs_app_latest_version.cpp
+++ b/trunk/src/app/srs_app_latest_version.cpp
@@ -54,7 +54,7 @@ int SrsLatestVersion::start()
return ERROR_SUCCESS;
}
- char buf[10];
+ char buf[16];
srs_random_generate(buf, sizeof(buf));
for (int i = 0; i < (int)sizeof(buf); i++) {
buf[i] = 'a' + uint8_t(buf[i])%25;
diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp
index 0325acf4f..9b39b5597 100644
--- a/trunk/src/core/srs_core.hpp
+++ b/trunk/src/core/srs_core.hpp
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// current release version
#define VERSION_MAJOR 2
#define VERSION_MINOR 0
-#define VERSION_REVISION 273
+#define VERSION_REVISION 274
// generated by configure, only macros.
#include
diff --git a/trunk/src/kernel/srs_kernel_utility.cpp b/trunk/src/kernel/srs_kernel_utility.cpp
index ba663604e..95b24a181 100644
--- a/trunk/src/kernel/srs_kernel_utility.cpp
+++ b/trunk/src/kernel/srs_kernel_utility.cpp
@@ -110,9 +110,17 @@ int64_t srs_get_system_startup_time_ms()
if (_srs_system_time_startup_time <= 0) {
srs_update_system_time_ms();
}
-
+
return _srs_system_time_startup_time / 1000;
}
+int64_t srs_get_system_startup_time_us()
+{
+ if (_srs_system_time_startup_time <= 0) {
+ srs_update_system_time_ms();
+ }
+
+ return _srs_system_time_startup_time;
+}
int64_t srs_update_system_time_ms()
{
timeval now;
diff --git a/trunk/src/kernel/srs_kernel_utility.hpp b/trunk/src/kernel/srs_kernel_utility.hpp
index 085074848..de4023513 100644
--- a/trunk/src/kernel/srs_kernel_utility.hpp
+++ b/trunk/src/kernel/srs_kernel_utility.hpp
@@ -46,6 +46,7 @@ extern int srs_avc_nalu_read_bit(SrsBitStream* stream, int8_t& v);
// get current system time in ms, use cache to avoid performance problem
extern int64_t srs_get_system_time_ms();
extern int64_t srs_get_system_startup_time_ms();
+extern int64_t srs_get_system_startup_time_us();
// the deamon st-thread will update it.
extern int64_t srs_update_system_time_ms();
diff --git a/trunk/src/protocol/srs_rtmp_handshake.cpp b/trunk/src/protocol/srs_rtmp_handshake.cpp
index c0c11bf0b..77f0dc1bc 100644
--- a/trunk/src/protocol/srs_rtmp_handshake.cpp
+++ b/trunk/src/protocol/srs_rtmp_handshake.cpp
@@ -484,7 +484,7 @@ namespace _srs_internal
key_block::key_block()
{
- offset = (int32_t)rand();
+ offset = (int32_t)srs_random();
random0 = NULL;
random1 = NULL;
@@ -566,7 +566,7 @@ namespace _srs_internal
digest_block::digest_block()
{
- offset = (int32_t)rand();
+ offset = (int32_t)srs_random();
random0 = NULL;
random1 = NULL;
diff --git a/trunk/src/protocol/srs_rtmp_utility.cpp b/trunk/src/protocol/srs_rtmp_utility.cpp
index 8c9b9908a..206253b8a 100644
--- a/trunk/src/protocol/srs_rtmp_utility.cpp
+++ b/trunk/src/protocol/srs_rtmp_utility.cpp
@@ -116,19 +116,25 @@ void srs_vhost_resolve(string& vhost, string& app, string& param)
void srs_random_generate(char* bytes, int size)
{
- static bool _random_initialized = false;
- if (!_random_initialized) {
- srand(0);
- _random_initialized = true;
- srs_trace("srand initialized the random.");
- }
-
for (int i = 0; i < size; i++) {
// the common value in [0x0f, 0xf0]
- bytes[i] = 0x0f + (rand() % (256 - 0x0f - 0x0f));
+ bytes[i] = 0x0f + (srs_random() % (256 - 0x0f - 0x0f));
}
}
+long srs_random()
+{
+ static bool _random_initialized = false;
+ if (!_random_initialized) {
+ _random_initialized = true;
+
+ srandom((unsigned int)srs_get_system_startup_time_us());
+ srs_trace("srandom initialized the random.");
+ }
+
+ return random();
+}
+
string srs_generate_tc_url(string ip, string vhost, string app, string port, string param)
{
string tcUrl = "rtmp://";
diff --git a/trunk/src/protocol/srs_rtmp_utility.hpp b/trunk/src/protocol/srs_rtmp_utility.hpp
index 9344410c5..4a7211097 100644
--- a/trunk/src/protocol/srs_rtmp_utility.hpp
+++ b/trunk/src/protocol/srs_rtmp_utility.hpp
@@ -80,6 +80,8 @@ extern void srs_vhost_resolve(
* generate ramdom data for handshake.
*/
extern void srs_random_generate(char* bytes, int size);
+// Generate random value, use srandom(now_us) to init seed if not initialized.
+extern long srs_random();
/**
* generate the tcUrl.