diff --git a/dAuthServer/AuthServer.cpp b/dAuthServer/AuthServer.cpp index 262886d7..38910823 100644 --- a/dAuthServer/AuthServer.cpp +++ b/dAuthServer/AuthServer.cpp @@ -29,6 +29,7 @@ namespace Game { dServer* server = nullptr; dConfig* config = nullptr; bool shouldShutdown = false; + std::mt19937 randomEngine; } dLogger* SetupLogger(); @@ -83,6 +84,8 @@ int main(int argc, char** argv) { delete res; delete stmt; + Game::randomEngine = std::mt19937(time(0)); + //It's safe to pass 'localhost' here, as the IP is only used as the external IP. uint32_t maxClients = 50; uint32_t ourPort = 1001; //LU client is hardcoded to use this for auth port, so I'm making it the default. diff --git a/dCommon/GeneralUtils.h b/dCommon/GeneralUtils.h index 0a8a0a16..e9e20ba0 100644 --- a/dCommon/GeneralUtils.h +++ b/dCommon/GeneralUtils.h @@ -111,29 +111,6 @@ namespace GeneralUtils { */ bool CheckBit(int64_t value, uint32_t index); - // MARK: Random Number Generation - - //! Generates a random number - /*! - \param min The minimum the generate from - \param max The maximum to generate to - */ - template - inline T GenerateRandomNumber(std::size_t min, std::size_t max) { - // Make sure it is a numeric type - static_assert(std::is_arithmetic::value, "Not an arithmetic type"); - - if constexpr (std::is_integral_v) { // constexpr only necessary on first statement - std::uniform_int_distribution distribution(min, max); - return distribution(Game::randomEngine); - } else if (std::is_floating_point_v) { - std::uniform_real_distribution distribution(min, max); - return distribution(Game::randomEngine); - } - - return T(); - } - bool ReplaceInString(std::string& str, const std::string& from, const std::string& to); std::u16string ReadWString(RakNet::BitStream* inStream); @@ -223,4 +200,42 @@ namespace GeneralUtils { std::hash h; s ^= h(v) + 0x9e3779b9 + (s << 6) + (s >> 2); } + + // MARK: Random Number Generation + + //! Generates a random number + /*! + \param min The minimum the generate from + \param max The maximum to generate to + */ + template + inline T GenerateRandomNumber(std::size_t min, std::size_t max) { + // Make sure it is a numeric type + static_assert(std::is_arithmetic::value, "Not an arithmetic type"); + + if constexpr (std::is_integral_v) { // constexpr only necessary on first statement + std::uniform_int_distribution distribution(min, max); + return distribution(Game::randomEngine); + } else if (std::is_floating_point_v) { + std::uniform_real_distribution distribution(min, max); + return distribution(Game::randomEngine); + } + + return T(); + } + +// on Windows we need to undef these or else they conflict with our numeric limits calls +// DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS +#ifdef _WIN32 +#undef min +#undef max +#endif + + template + inline T GenerateRandomNumber() { + // Make sure it is a numeric type + static_assert(std::is_arithmetic::value, "Not an arithmetic type"); + + return GenerateRandomNumber(std::numeric_limits::min(), std::numeric_limits::max()); + } } diff --git a/dNet/AuthPackets.cpp b/dNet/AuthPackets.cpp index 4bbb0576..978540c1 100644 --- a/dNet/AuthPackets.cpp +++ b/dNet/AuthPackets.cpp @@ -8,6 +8,7 @@ #include "ZoneInstanceManager.h" #include "MD5.h" #include "SHA512.h" +#include "GeneralUtils.h" #ifdef _WIN32 #include @@ -211,7 +212,7 @@ void AuthPackets::SendLoginResponse(dServer* server, const SystemAddress& sysAdd packet.Write(static_cast(64)); // Version Minor // Writes the user key - uint32_t sessionKey = rand(); // not mt but whatever + uint32_t sessionKey = GeneralUtils::GenerateRandomNumber(); std::string userHash = std::to_string(sessionKey); userHash = md5(userHash); PacketUtils::WritePacketWString(userHash, 33, &packet);