Commit 28c8124e authored by aligungr's avatar aligungr

RNG improvements and fix of initial ID conflict

parent 04a4dbbb
......@@ -20,10 +20,25 @@
#include <utils/common.hpp>
#include <utils/constants.hpp>
#include <utils/io.hpp>
#include <utils/random.hpp>
namespace app
{
static std::string RandomFileName(const std::vector<std::string> &nodes, int cmdPort)
{
std::string s;
for (auto &node : nodes)
{
s += node;
s += "|";
}
s += std::to_string(cmdPort);
Random rnd = Random::Mixed(s);
return utils::IntToHex(rnd.nextL()) + utils::IntToHex(rnd.nextL());
}
void CreateProcTable(const std::vector<std::string> &nodes, int cmdPort)
{
try
......@@ -33,7 +48,7 @@ void CreateProcTable(const std::vector<std::string> &nodes, int cmdPort)
std::string filePath;
while (true)
{
std::string fileName = utils::IntToHex(utils::Random64()) + utils::IntToHex(utils::Random64());
std::string fileName = RandomFileName(nodes, cmdPort);
filePath = cons::PROC_TABLE_DIR + fileName;
if (!io::Exists(filePath))
......
......@@ -13,6 +13,7 @@
#include <ue/rrc/task.hpp>
#include <utils/common.hpp>
#include <utils/constants.hpp>
#include <utils/random.hpp>
namespace nr::ue
{
......@@ -22,7 +23,7 @@ UeRlsTask::UeRlsTask(TaskBase *base) : m_base{base}
m_logger = m_base->logBase->makeUniqueLogger(m_base->config->getLoggerPrefix() + "rls");
m_shCtx = new RlsSharedContext();
m_shCtx->sti = utils::Random64();
m_shCtx->sti = Random::Mixed(base->config->getNodeName()).nextL();
m_udpTask = new RlsUdpTask(base, m_shCtx, base->config->gnbSearchList);
m_ctlTask = new RlsControlTask(base, m_shCtx);
......@@ -108,7 +109,7 @@ void UeRlsTask::onLoop()
break;
}
case NmUeRrcToRls::RESET_STI: {
m_shCtx->sti = utils::Random64();
m_shCtx->sti = Random::Mixed(m_base->config->getNodeName()).nextL();
break;
}
}
......
......@@ -11,6 +11,7 @@
#include <lib/rrc/encode.hpp>
#include <ue/nas/task.hpp>
#include <ue/nts.hpp>
#include <utils/random.hpp>
#include <asn/rrc/ASN_RRC_RRCSetup-IEs.h>
#include <asn/rrc/ASN_RRC_RRCSetup.h>
......@@ -58,7 +59,7 @@ void UeRrcTask::startConnectionEstablishment(OctetString &&nasPdu)
if (m_initialId.present == ASN_RRC_InitialUE_Identity_PR_NOTHING)
{
m_initialId.present = ASN_RRC_InitialUE_Identity_PR_randomValue;
asn::SetBitStringLong<39>(static_cast<int64_t>(utils::Random64()), m_initialId.choice.randomValue);
asn::SetBitStringLong<39>(Random::Mixed(m_base->config->getNodeName()).nextL(), m_initialId.choice.randomValue);
}
m_initialNasPdu = std::move(nasPdu);
......
......@@ -262,27 +262,6 @@ int utils::ParseInt(const char *str)
return n;
}
uint64_t utils::Random64()
{
static bool initiated = false;
static std::random_device randomDevice{};
static std::mt19937_64 randomEngine(randomDevice());
if (!initiated)
{
randomEngine.seed(std::chrono::high_resolution_clock::now().time_since_epoch().count());
initiated = true;
}
while (true)
{
std::uniform_int_distribution<uint64_t> distribution;
uint64_t r = distribution(randomEngine);
if (r != 0)
return r;
}
}
void utils::Sleep(int ms)
{
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
......
......@@ -34,7 +34,6 @@ int ParseInt(const std::string &str);
int ParseInt(const char *str);
bool TryParseInt(const std::string &str, int &output);
bool TryParseInt(const char *str, int &output);
uint64_t Random64();
void Sleep(int ms);
bool IsRoot();
bool IsNumeric(const std::string &str);
......
//
// This file is a part of UERANSIM open source project.
// Copyright (c) 2021 ALİ GÜNGÖR.
//
// The software and all associated files are licensed under GPL-3.0
// and subject to the terms and conditions defined in LICENSE file.
//
#include "random.hpp"
#include <atomic>
#include <chrono>
static std::atomic<int64_t> s_counter;
Random::Random() : Random(std::chrono::high_resolution_clock::now().time_since_epoch().count() + s_counter++)
{
}
Random::Random(int64_t seed) : m_seed{seed}
{
}
int64_t Random::nextL()
{
m_seed = (8253729LL * m_seed + 2396403LL);
m_seed &= 0x7FFFFFFFFFFFFFFFLL;
return m_seed;
}
int64_t Random::nextL(int end)
{
return nextL() % static_cast<int64_t>(end);
}
int64_t Random::nextL(int start, int end)
{
return nextL(end - start) + start;
}
uint64_t Random::nextUL()
{
return static_cast<uint64_t>(nextL());
}
uint64_t Random::nextUL(int end)
{
return static_cast<uint64_t>(nextL(end));
}
uint64_t Random::nextUL(int start, int end)
{
return static_cast<uint64_t>(nextL(start, end));
}
int32_t Random::nextI()
{
return static_cast<int32_t>(nextL() & 0x7FFFFFFFLL);
}
int32_t Random::nextI(int end)
{
return static_cast<int32_t>(nextL(end) & 0x7FFFFFFFLL);
}
int32_t Random::nextI(int start, int end)
{
return static_cast<int32_t>(nextL(start, end) & 0x7FFFFFFFLL);
}
uint32_t Random::nextUI()
{
return static_cast<uint32_t>(nextI());
}
uint32_t Random::nextUI(int end)
{
return static_cast<uint32_t>(nextI(end));
}
uint32_t Random::nextUI(int start, int end)
{
return static_cast<uint32_t>(nextI(start, end));
}
\ No newline at end of file
//
// This file is a part of UERANSIM open source project.
// Copyright (c) 2021 ALİ GÜNGÖR.
//
// The software and all associated files are licensed under GPL-3.0
// and subject to the terms and conditions defined in LICENSE file.
//
#pragma once
#include <cstdint>
#include <functional>
#include <string>
class Random
{
private:
int64_t m_seed;
public:
Random();
explicit Random(int64_t seed);
int64_t nextL();
int64_t nextL(int end);
int64_t nextL(int start, int end);
uint64_t nextUL();
uint64_t nextUL(int end);
uint64_t nextUL(int start, int end);
int32_t nextI();
int32_t nextI(int end);
int32_t nextI(int start, int end);
uint32_t nextUI();
uint32_t nextUI(int end);
uint32_t nextUI(int start, int end);
template <typename T>
static inline Random Mixed(const T &v)
{
auto hash = static_cast<int64_t>(std::hash<T>{}(v));
Random r;
r.m_seed ^= hash;
return r;
}
};
\ No newline at end of file
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