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 4d8ad85
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/randomenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,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 (GetComputerName(hname, &size) != 0) {
hasher.Write((const unsigned char*)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 4d8ad85

Please sign in to comment.