From 20c50d32d4193204947ee9720ec3331227759d82 Mon Sep 17 00:00:00 2001 From: Ykkrosh Date: Wed, 30 Jun 2010 23:40:51 +0000 Subject: [PATCH] Windows fixes. Avoid Boost.Random since it adds a dependency on a new library. This was SVN commit r7654. --- build/premake/extern_libs.lua | 4 ++-- source/lib/sysdep/os/unix/unix.cpp | 20 ++++++++++++++++++++ source/lib/sysdep/os/win/wsdl.cpp | 2 +- source/lib/sysdep/os/win/wsysdep.cpp | 17 +++++++++++++++++ source/lib/sysdep/sysdep.h | 8 ++++++++ source/lib/sysdep/tests/test_sysdep.h | 8 ++++++++ source/network/NetClient.cpp | 12 ++++-------- source/network/NetTurnManager.h | 1 + source/network/tests/test_Net.h | 6 ++++++ 9 files changed, 67 insertions(+), 11 deletions(-) diff --git a/build/premake/extern_libs.lua b/build/premake/extern_libs.lua index 852c572cd2..f1a4616e6a 100644 --- a/build/premake/extern_libs.lua +++ b/build/premake/extern_libs.lua @@ -71,8 +71,8 @@ end -- * defines: a table of symbols to define extern_lib_defs = { boost = { - unix_names = { "boost_signals-mt", "boost_filesystem-mt", "boost_system-mt", "boost_random-mt" }, - osx_names = { "boost_signals-mt", "boost_filesystem-mt", "boost_system-mt", "boost_random-mt" } + unix_names = { "boost_signals-mt", "boost_filesystem-mt", "boost_system-mt" }, + osx_names = { "boost_signals-mt", "boost_filesystem-mt", "boost_system-mt" } }, cryptopp = { win_names = { "cryptopp" }, diff --git a/source/lib/sysdep/os/unix/unix.cpp b/source/lib/sysdep/os/unix/unix.cpp index 1d7e5d21c5..4c8059b781 100644 --- a/source/lib/sysdep/os/unix/unix.cpp +++ b/source/lib/sysdep/os/unix/unix.cpp @@ -204,3 +204,23 @@ std::wstring sys_get_user_name() return L""; } + +LibError sys_generate_random_bytes(u8* buf, size_t count) +{ + FILE* f = fopen("/dev/urandom", "rb"); + if (!f) + WARN_RETURN(ERR::FAIL); + + while (count) + { + size_t numread = fread(buf, 1, count, f); + if (numread == 0) + WARN_RETURN(ERR::FAIL); + buf += numread; + count -= numread; + } + + fclose(f); + + return INFO::OK; +} diff --git a/source/lib/sysdep/os/win/wsdl.cpp b/source/lib/sysdep/os/win/wsdl.cpp index fbbd3e600f..6085c02c77 100644 --- a/source/lib/sysdep/os/win/wsdl.cpp +++ b/source/lib/sysdep/os/win/wsdl.cpp @@ -1020,7 +1020,7 @@ int SDL_ShowCursor(int toggle) // the flood (and only call SDL_SetVideoMode once a frame or similar). // note: SDL uses WM_WINDOWPOSCHANGING, which requires calling // GetClientRect and suffers from false alarms. -static void OnSize(HWND hWnd, UINT UNUSED(state), int clientWidth, int clientHeight) +static void OnSize(HWND UNUSED(hWnd), UINT UNUSED(state), int clientWidth, int clientHeight) { // if we don't prevent SDL_SetVideoMode from triggering SDL_VIDEORESIZE, // the app's once-per-frame throttle still results in infinite recursion. diff --git a/source/lib/sysdep/os/win/wsysdep.cpp b/source/lib/sysdep/os/win/wsysdep.cpp index 950889ba60..6be8b998bc 100644 --- a/source/lib/sysdep/os/win/wsysdep.cpp +++ b/source/lib/sysdep/os/win/wsysdep.cpp @@ -29,6 +29,7 @@ #include "lib/sysdep/os/win/win.h" // includes windows.h; must come before shlobj #include // pick_dir +#include #include "lib/sysdep/clipboard.h" #include "lib/sysdep/os/win/error_dialog.h" @@ -411,3 +412,19 @@ LibError sys_pick_directory(fs::wpath& path) return LibError_from_GLE(); } + +LibError sys_generate_random_bytes(u8* buf, size_t count) +{ + HCRYPTPROV hCryptProv; + + if(!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) + WARN_RETURN(ERR::FAIL); + + if(!CryptGenRandom(hCryptProv, count, buf)) + WARN_RETURN(ERR::FAIL); + + if (!CryptReleaseContext(hCryptProv, 0)) + WARN_RETURN(ERR::FAIL); + + return INFO::OK; +} diff --git a/source/lib/sysdep/sysdep.h b/source/lib/sysdep/sysdep.h index 79d3044cfc..382af20dff 100644 --- a/source/lib/sysdep/sysdep.h +++ b/source/lib/sysdep/sysdep.h @@ -146,6 +146,14 @@ extern LibError sys_pick_directory(fs::wpath& path); **/ extern size_t sys_max_sector_size(); +/** + * generate high-quality random bytes. + * + * this should only be used with small numbers of bytes, to avoid + * hogging the system's entropy. + **/ +extern LibError sys_generate_random_bytes(u8* buf, size_t count); + /** * directory separation character **/ diff --git a/source/lib/sysdep/tests/test_sysdep.h b/source/lib/sysdep/tests/test_sysdep.h index a3b1547ef0..a5900a1b5b 100644 --- a/source/lib/sysdep/tests/test_sysdep.h +++ b/source/lib/sysdep/tests/test_sysdep.h @@ -86,6 +86,14 @@ public: TS_ASSERT_EQUALS(fmaxf(0.001f, 0.00001f), 0.001f); } + void test_random() + { + u64 a = 0, b = 0; + TS_ASSERT_OK(sys_generate_random_bytes((u8*)&a, sizeof(a))); + TS_ASSERT_OK(sys_generate_random_bytes((u8*)&b, sizeof(b))); + TS_ASSERT_DIFFERS(a, b); + } + void test_sys_get_executable_name() { fs::wpath path; diff --git a/source/network/NetClient.cpp b/source/network/NetClient.cpp index 27c8bad5a2..20b0352f9e 100644 --- a/source/network/NetClient.cpp +++ b/source/network/NetClient.cpp @@ -23,6 +23,7 @@ #include "NetSession.h" #include "NetTurnManager.h" +#include "lib/sysdep/sysdep.h" #include "ps/CConsole.h" #include "ps/CLogger.h" #include "ps/CStr.h" @@ -30,9 +31,6 @@ #include "scriptinterface/ScriptInterface.h" #include "simulation2/Simulation2.h" -#include -#include - CNetClient *g_NetClient = NULL; CNetClient::CNetClient(CGame* game) : @@ -378,15 +376,13 @@ CStr CNetClient::GenerateGUID() // a host to masquerade as someone else. // For now, just try to pick a very random number. - boost::random_device rng; - boost::uniform_int dist(0, std::numeric_limits::max()); - boost::variate_generator > gen(rng, dist); - CStr guid; for (size_t i = 0; i < 2; ++i) { + u32 r = 0; + sys_generate_random_bytes((u8*)&r, sizeof(r)); char buf[32]; - sprintf_s(buf, ARRAY_SIZE(buf), "%08X", gen()); + sprintf_s(buf, ARRAY_SIZE(buf), "%08X", r); guid += buf; } diff --git a/source/network/NetTurnManager.h b/source/network/NetTurnManager.h index 871c9dcda0..a4e834aed7 100644 --- a/source/network/NetTurnManager.h +++ b/source/network/NetTurnManager.h @@ -184,6 +184,7 @@ protected: */ class CNetServerTurnManager { + NONCOPYABLE(CNetServerTurnManager); public: CNetServerTurnManager(CNetServer& server); diff --git a/source/network/tests/test_Net.h b/source/network/tests/test_Net.h index db791a17b5..e3b00350cc 100644 --- a/source/network/tests/test_Net.h +++ b/source/network/tests/test_Net.h @@ -29,6 +29,8 @@ #include "scriptinterface/ScriptInterface.h" #include "simulation2/Simulation2.h" +#include + class TestNetComms : public CxxTest::TestSuite { public: @@ -38,10 +40,14 @@ public: TS_ASSERT_OK(g_VFS->Mount(L"", DataDir()/L"mods/public", VFS_MOUNT_MUST_EXIST)); TS_ASSERT_OK(g_VFS->Mount(L"cache", DataDir()/L"_testcache")); CXeromyces::Startup(); + + enet_initialize(); } void tearDown() { + enet_deinitialize(); + CXeromyces::Terminate(); g_VFS.reset(); DeleteDirectory(DataDir()/L"_testcache");