From bcb26fd638bd8c543cd3cc42837b120ff86d44b1 Mon Sep 17 00:00:00 2001 From: arvidn Date: Thu, 28 Mar 2019 20:45:39 +0100 Subject: [PATCH] fix seeding of random number generator on mingw --- include/libtorrent/config.hpp | 7 +++++++ src/random.cpp | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/libtorrent/config.hpp b/include/libtorrent/config.hpp index 9ddcdb94a..e58f0f6e5 100644 --- a/include/libtorrent/config.hpp +++ b/include/libtorrent/config.hpp @@ -236,6 +236,9 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_USE_PREADV 1 #define TORRENT_USE_PWRITEV 1 +// mingw doesn't implement random_device. +#define TORRENT_BROKEN_RANDOM_DEVICE 1 + # if !defined TORRENT_USE_LIBCRYPTO && !defined TORRENT_USE_LIBGCRYPT // unless some other crypto library has been specified, default to the native // windows CryptoAPI @@ -354,6 +357,10 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_FORMAT(fmt, ellipsis) #endif +#ifndef TORRENT_BROKEN_RANDOM_DEVICE +#define TORRENT_BROKEN_RANDOM_DEVICE 0 +#endif + // libiconv presence detection is not implemented yet #ifndef TORRENT_USE_ICONV #define TORRENT_USE_ICONV 1 diff --git a/src/random.cpp b/src/random.cpp index 67e5309ad..3943aa8f7 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -40,6 +40,11 @@ POSSIBILITY OF SUCH DAMAGE. #include #endif +#if TORRENT_BROKEN_RANDOM_DEVICE +#include "libtorrent/time.hpp" +#include +#endif + #if TORRENT_USE_CRYPTOAPI #include "libtorrent/aux_/win_crypto_provider.hpp" @@ -67,7 +72,8 @@ namespace { } #endif -namespace libtorrent { namespace aux { +namespace libtorrent { +namespace aux { std::mt19937& random_engine() { @@ -76,7 +82,18 @@ namespace libtorrent { namespace aux { static std::mt19937 rng(0x82daf973); #else +#if TORRENT_BROKEN_RANDOM_DEVICE + struct { + std::uint32_t operator()() const + { + static std::atomic seed{static_cast(duration_cast( + std::chrono::high_resolution_clock::now().time_since_epoch()).count())}; + return seed++; + } + } dev; +#else static std::random_device dev; +#endif #ifdef BOOST_NO_CXX11_THREAD_LOCAL static std::mt19937 rng(dev()); #else