Optimize RNG and limit packet input

This commit is contained in:
momo5502 2016-02-19 23:57:06 +01:00
parent 698961b57b
commit 0ec1029a89
7 changed files with 38 additions and 35 deletions

View File

@ -83,7 +83,7 @@ namespace Components
bool Loader::PerformingUnitTests() bool Loader::PerformingUnitTests()
{ {
#if DEBUG #if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
return Flags::HasFlag("tests"); return Flags::HasFlag("tests");
#else #else
return false; return false;

View File

@ -181,6 +181,21 @@ namespace Components
int Network::PacketInterceptionHandler(const char* packet) int Network::PacketInterceptionHandler(const char* packet)
{ {
// Packet rate limit.
static uint32_t packets = 0;
static int lastClean = 0;
if ((Game::Com_Milliseconds() - lastClean) > 1'000)
{
packets = 0;
lastClean = Game::Com_Milliseconds();
}
if ((++packets) > NETWORK_MAX_PACKETS_PER_SECOND)
{
return 1;
}
std::string packetCommand = packet; std::string packetCommand = packet;
auto pos = packetCommand.find_first_of("\\\n "); auto pos = packetCommand.find_first_of("\\\n ");
if (pos != std::string::npos) if (pos != std::string::npos)

View File

@ -1,3 +1,5 @@
#define NETWORK_MAX_PACKETS_PER_SECOND 100'000
namespace Components namespace Components
{ {
class Network : public Component class Network : public Component

View File

@ -7,9 +7,11 @@ namespace Main
void Initialize() void Initialize()
{ {
Main::EntryPointHook.Uninstall(); Main::EntryPointHook.Uninstall();
Utils::Cryptography::Rand::Initialize();
Components::Loader::Initialize(); Components::Loader::Initialize();
#ifdef DEBUG #if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
if (Components::Loader::PerformingUnitTests()) if (Components::Loader::PerformingUnitTests())
{ {
DWORD result = (Components::Loader::PerformUnitTests() ? 0 : -1); DWORD result = (Components::Loader::PerformUnitTests() ? 0 : -1);

View File

@ -105,3 +105,6 @@
#define VERSION_STR "4.2." REVISION_STR #define VERSION_STR "4.2." REVISION_STR
#define Assert_Size(x, size) static_assert(sizeof(x) == size, STRINGIZE(x) " structure has an invalid size.") #define Assert_Size(x, size) static_assert(sizeof(x) == size, STRINGIZE(x) " structure has an invalid size.")
// Enable unit-test flag for release builds
//#define FORCE_UNIT_TESTS

View File

@ -8,42 +8,19 @@ namespace Utils
{ {
#pragma region Rand #pragma region Rand
prng_state Rand::State;
uint32_t Rand::GenerateInt() uint32_t Rand::GenerateInt()
{ {
static int length = 0; uint32_t number = 0;
static int time = 0; fortuna_read(reinterpret_cast<uint8_t*>(&number), sizeof(number), &Rand::State);
static int access = 0; return number;
static uint8_t randPool[2048] = { 0 }; }
int mseconds = Game::Com_Milliseconds(); void Rand::Initialize()
{
if ((mseconds - time) > (1000 * 60 * 5) || (access > (sizeof(randPool) * 30))) register_prng(&fortuna_desc);
{ rng_make_prng(128, find_prng("fortuna"), &Rand::State, NULL);
access = 0;
length = 0;
}
while (length != sizeof(randPool))
{
length += sprng_read(randPool, sizeof(randPool), NULL);
}
uint8_t numberBuf[4] = { 0 };
for (int i = 0; i < sizeof(numberBuf); ++i)
{
numberBuf[i] = randPool[(rand() + mseconds + i + numberBuf[(i > 0 ? (i - 1) : 0)]) % sizeof(randPool)];
}
uint32_t num = *reinterpret_cast<uint32_t*>(numberBuf);
if (!(access % 100))
{
srand(num);
}
access++;
return num;
} }
#pragma endregion #pragma endregion

View File

@ -6,6 +6,10 @@ namespace Utils
{ {
public: public:
static uint32_t GenerateInt(); static uint32_t GenerateInt();
static void Initialize();
private:
static prng_state State;
}; };
class ECDSA class ECDSA