Skip to content

Commit

Permalink
Drop Windows Socket dependency for randomenv.cpp
Browse files Browse the repository at this point in the history
The current implementation has two issues.
1. The `gethostname` call requires the Winsock DLL being initiated in
advance, which is not always guaranteed, for example, in the kernel
library.
2. It introduces a dependency on the ws2_32 library for our
libbitcoinkernel, which may not be desirable.
  • Loading branch information
hebasto committed Apr 2, 2024
1 parent 23ba394 commit 459559e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/randomenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <compat/compat.h>
#include <compat/cpuid.h>
#include <crypto/sha512.h>
#include <span.h>
#include <support/cleanse.h>
#include <util/time.h>

Expand Down Expand Up @@ -357,10 +358,21 @@ void RandAddStaticEnv(CSHA512& hasher)
hasher << &hasher << &RandAddStaticEnv << &malloc << &errno << &environ;

// Hostname
#ifdef WIN32
// Not using gethostname from Windows Sockets because
// the use of the Winsock DLL might be not initiated.
constexpr DWORD max_size = MAX_COMPUTERNAME_LENGTH + 1;
char hname[max_size];
DWORD size = max_size;
if (GetComputerNameA(hname, &size) != 0) {
hasher.Write(UCharCast(hname), size);
}
#else
char hname[256];
if (gethostname(hname, 256) == 0) {
hasher.Write((const unsigned char*)hname, strnlen(hname, 256));
}
#endif

#if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
// Network interfaces
Expand Down

0 comments on commit 459559e

Please sign in to comment.