Commit 4fcf78bc authored by Adrien Béraud's avatar Adrien Béraud

daemon: avoid use of rand()

Refs #67228

Change-Id: Ic4a6449c74301dde54f31be18adef26e5b2260d0
parent af900e85
......@@ -391,8 +391,9 @@ class Account : public Serializable, public std::enable_shared_from_this<Account
/**
* Random generator engine
* Logical account state shall never rely on the state of the random generator.
*/
std::mt19937_64 rand_ {};
mutable std::mt19937_64 rand_ {};
/**
* UPnP IGD controller and the mutex to access it
......
......@@ -94,7 +94,7 @@ std::map<std::string, std::string> IAXAccount::getVolatileAccountDetails() const
void IAXAccount::doRegister()
{
try {
link_->init();
link_->init(rand_);
sendRegister();
} catch (const VoipLinkException &e) {
RING_ERR("IAXAccount: %s", e.what());
......
......@@ -63,13 +63,14 @@ IAXVoIPLink::~IAXVoIPLink()
}
void
IAXVoIPLink::init()
IAXVoIPLink::init(std::mt19937_64& rand_generator)
{
if (initDone_)
return;
std::uniform_int_distribution<int> port_dist(1024, 65024);
std::lock_guard<std::mutex> lock(mutexIAX);
for (int port = IAX_DEFAULT_PORTNO, nbTry = 0; nbTry < 3 ; port = rand() % 64000 + 1024, nbTry++) {
for (int port = IAX_DEFAULT_PORTNO, nbTry = 0; nbTry < 3 ; port = port_dist(rand_generator), nbTry++) {
if (iax_init(port) >= 0) {
Manager::instance().registerEventHandler((uintptr_t)this, std::bind(&IAXVoIPLink::handleEvents, this));
initDone_ = true;
......
......@@ -45,6 +45,7 @@
#include <mutex>
#include <memory>
#include <random>
namespace ring {
......@@ -73,7 +74,7 @@ class IAXVoIPLink {
/**
* Init the voip link
*/
void init();
void init(std::mt19937_64& rand);
/**
* Terminate a voip link by clearing the call list
......
......@@ -922,7 +922,7 @@ void SIPAccount::startKeepAliveTimer()
pj_time_val keepAliveDelay_;
keepAliveTimer_.cb = &SIPAccount::keepAliveRegistrationCb;
keepAliveTimer_.user_data = this;
keepAliveTimer_.id = rand();
keepAliveTimer_.id = timerIdDist_(rand_);
// expiration may be undetermined during the first registration request
if (registrationExpire_ == 0) {
......@@ -2125,10 +2125,10 @@ SIPAccount::scheduleReregistration(pjsip_endpoint *endpt)
/* Randomize interval by +/- 10 secs */
if (delay.sec >= 10) {
delay.msec = -10000 + (pj_rand() % 20000);
delay.msec = delay10ZeroDist_(rand_);
} else {
delay.sec = 0;
delay.msec = (pj_rand() % 10000);
delay.msec = delay10PosDist_(rand_);
}
pj_time_val_normalize(&delay);
......
......@@ -526,6 +526,9 @@ class SIPAccount : public SIPAccountBase {
unsigned attempt_cnt; /**< Attempt counter. */
} auto_rereg_; /**< Reregister/reconnect data. */
std::uniform_int_distribution<decltype(pj_time_val::msec)> delay10ZeroDist_ {-10000, 10000};
std::uniform_int_distribution<decltype(pj_time_val::msec)> delay10PosDist_ {0, 10000};
static void autoReregTimerCb(pj_timer_heap_t *th, pj_timer_entry *te);
/**
......@@ -700,6 +703,7 @@ class SIPAccount : public SIPAccountBase {
* on the "Expire" sip header (or the "expire" Contact parameter)
*/
pj_timer_entry keepAliveTimer_;
std::uniform_int_distribution<decltype(pj_timer_entry::id)> timerIdDist_ {};
/**
* Once enabled, this variable tells if the keepalive timer is activated
......
......@@ -321,13 +321,12 @@ SIPAccountBase::setTransport(const std::shared_ptr<SipTransport>& t)
// returns even number in range [lower, upper]
uint16_t
SIPAccountBase::getRandomEvenNumber(const std::pair<uint16_t, uint16_t> &range)
SIPAccountBase::acquireRandomEvenPort(const std::pair<uint16_t, uint16_t>& range) const
{
const uint16_t halfUpper = range.second * 0.5;
const uint16_t halfLower = range.first * 0.5;
std::uniform_int_distribution<uint16_t> dist(range.first/2, range.second/2);
uint16_t result;
do {
result = 2 * (halfLower + rand() % (halfUpper - halfLower + 1));
result = 2 * dist(rand_);
} while (portsInUse_[result / 2]);
portsInUse_[result / 2] = true;
......@@ -343,14 +342,14 @@ SIPAccountBase::releasePort(uint16_t port)
uint16_t
SIPAccountBase::generateAudioPort() const
{
return getRandomEvenNumber(audioPortRange_);
return acquireRandomEvenPort(audioPortRange_);
}
#ifdef RING_VIDEO
uint16_t
SIPAccountBase::generateVideoPort() const
{
return getRandomEvenNumber(videoPortRange_);
return acquireRandomEvenPort(videoPortRange_);
}
#endif
......
......@@ -363,7 +363,7 @@ protected:
std::pair<uint16_t, uint16_t> videoPortRange_ {49152, (MAX_PORT) - 2};
static bool portsInUse_[HALF_MAX_PORT];
static uint16_t getRandomEvenNumber(const std::pair<uint16_t, uint16_t> &range);
uint16_t acquireRandomEvenPort(const std::pair<uint16_t, uint16_t>& range) const;
static void
addRangeToDetails(std::map<std::string, std::string> &a, const char *minKey, const char *maxKey, const std::pair<uint16_t, uint16_t> &range)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment