From 742826a6554786cad08bab8f19468e86335dc65e Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 9 Jul 2020 16:51:20 +0800 Subject: [PATCH] Log: Use 8 bytes random string as context id --- trunk/src/app/srs_app_rtc_conn.cpp | 14 ------------ trunk/src/app/srs_app_rtc_conn.hpp | 3 --- trunk/src/app/srs_app_rtc_server.cpp | 9 ++++---- trunk/src/protocol/srs_protocol_utility.cpp | 25 ++++++++++++++++++--- trunk/src/protocol/srs_protocol_utility.hpp | 7 +++--- trunk/src/protocol/srs_service_log.cpp | 10 ++------- 6 files changed, 33 insertions(+), 35 deletions(-) diff --git a/trunk/src/app/srs_app_rtc_conn.cpp b/trunk/src/app/srs_app_rtc_conn.cpp index acae80ebd..2495dac09 100644 --- a/trunk/src/app/srs_app_rtc_conn.cpp +++ b/trunk/src/app/srs_app_rtc_conn.cpp @@ -57,20 +57,6 @@ using namespace std; #include #include -// TODO: FIXME: Move to utility. -string gen_random_str(int len) -{ - static string random_table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - - string ret; - ret.reserve(len); - for (int i = 0; i < len; ++i) { - ret.append(1, random_table[random() % random_table.size()]); - } - - return ret; -} - uint64_t SrsNtp::kMagicNtpFractionalUnit = 1ULL << 32; SrsNtp::SrsNtp() diff --git a/trunk/src/app/srs_app_rtc_conn.hpp b/trunk/src/app/srs_app_rtc_conn.hpp index 880fdbc3f..e9a501bc7 100644 --- a/trunk/src/app/srs_app_rtc_conn.hpp +++ b/trunk/src/app/srs_app_rtc_conn.hpp @@ -75,9 +75,6 @@ const uint8_t kSLI = 2; const uint8_t kRPSI = 3; const uint8_t kAFB = 15; -// TODO: FIXME: Move to utility. -extern std::string gen_random_str(int len); - class SrsNtp { public: diff --git a/trunk/src/app/srs_app_rtc_server.cpp b/trunk/src/app/srs_app_rtc_server.cpp index 97ca437b3..9a3e5880b 100644 --- a/trunk/src/app/srs_app_rtc_server.cpp +++ b/trunk/src/app/srs_app_rtc_server.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -310,12 +311,12 @@ srs_error_t SrsRtcServer::create_session( return srs_error_new(ERROR_RTC_SOURCE_BUSY, "stream %s busy", req->get_stream_url().c_str()); } - std::string local_pwd = gen_random_str(32); + std::string local_pwd = srs_random_str(32); std::string local_ufrag = ""; // TODO: FIXME: Rename for a better name, it's not an username. std::string username = ""; while (true) { - local_ufrag = gen_random_str(8); + local_ufrag = srs_random_str(8); username = local_ufrag + ":" + remote_sdp.get_ice_ufrag(); if (!map_username_session.count(username)) { @@ -361,9 +362,9 @@ srs_error_t SrsRtcServer::create_session2(SrsSdp& local_sdp, SrsRtcConnection** { srs_error_t err = srs_success; - std::string local_pwd = gen_random_str(32); + std::string local_pwd = srs_random_str(32); // TODO: FIXME: Collision detect. - std::string local_ufrag = gen_random_str(8); + std::string local_ufrag = srs_random_str(8); SrsRtcConnection* session = new SrsRtcConnection(this); *psession = session; diff --git a/trunk/src/protocol/srs_protocol_utility.cpp b/trunk/src/protocol/srs_protocol_utility.cpp index 57e65ecf0..69b5bc3bd 100644 --- a/trunk/src/protocol/srs_protocol_utility.cpp +++ b/trunk/src/protocol/srs_protocol_utility.cpp @@ -148,20 +148,39 @@ void srs_parse_query_string(string q, map& query) } } +static bool _random_initialized = false; + void srs_random_generate(char* bytes, int size) { - static bool _random_initialized = false; if (!_random_initialized) { - srand(0); _random_initialized = true; + ::srandom((unsigned long)(srs_update_system_time() | (::getpid()<<13))); } for (int i = 0; i < size; i++) { // the common value in [0x0f, 0xf0] - bytes[i] = 0x0f + (rand() % (256 - 0x0f - 0x0f)); + bytes[i] = 0x0f + (random() % (256 - 0x0f - 0x0f)); } } +std::string srs_random_str(int len) +{ + if (!_random_initialized) { + _random_initialized = true; + ::srandom((unsigned long)(srs_update_system_time() | (::getpid()<<13))); + } + + static string random_table = "01234567890123456789012345678901234567890123456789abcdefghijklmnopqrstuvwxyz"; + + string ret; + ret.reserve(len); + for (int i = 0; i < len; ++i) { + ret.append(1, random_table[random() % random_table.size()]); + } + + return ret; +} + string srs_generate_tc_url(string host, string vhost, string app, int port) { string tcUrl = "rtmp://"; diff --git a/trunk/src/protocol/srs_protocol_utility.hpp b/trunk/src/protocol/srs_protocol_utility.hpp index 3f4cc03d2..aaf907ec3 100644 --- a/trunk/src/protocol/srs_protocol_utility.hpp +++ b/trunk/src/protocol/srs_protocol_utility.hpp @@ -65,11 +65,12 @@ extern void srs_discovery_tc_url(std::string tcUrl, std::string& schema, std::st // must format as key=value&...&keyN=valueN extern void srs_parse_query_string(std::string q, std::map& query); -/** - * generate ramdom data for handshake. - */ +// Generate ramdom data for handshake. extern void srs_random_generate(char* bytes, int size); +// Generate random string [0-9a-z] in size of len bytes. +extern std::string srs_random_str(int len); + /** * generate the tcUrl without param. * @remark Use host as tcUrl.vhost if vhost is default vhost. diff --git a/trunk/src/protocol/srs_service_log.cpp b/trunk/src/protocol/srs_service_log.cpp index 8c733e77f..437dc672b 100644 --- a/trunk/src/protocol/srs_service_log.cpp +++ b/trunk/src/protocol/srs_service_log.cpp @@ -31,6 +31,7 @@ using namespace std; #include #include +#include #define SRS_BASIC_LOG_SIZE 1024 @@ -44,14 +45,7 @@ SrsThreadContext::~SrsThreadContext() SrsContextId SrsThreadContext::generate_id() { - static int id = 0; - - if (id == 0) { - id = (100 + ((uint32_t)(int64_t)this)%1000); - } - int gid = id++; - - SrsContextId cid = SrsContextId(srs_int2str(gid)); + SrsContextId cid = SrsContextId(srs_random_str(8)); cache[srs_thread_self()] = cid; return cid; }