6#include <bitcoin-build-config.h>
33#include <netinet/in.h>
34#include <sys/resource.h>
35#include <sys/socket.h>
38#include <sys/utsname.h>
45#include <sys/sysctl.h>
46#if __has_include(<vm/vm_param.h>)
47#include <vm/vm_param.h>
49#if __has_include(<sys/resources.h>)
50#include <sys/resources.h>
52#if __has_include(<sys/vmmeter.h>)
53#include <sys/vmmeter.h>
56#if defined(HAVE_STRONG_GETAUXVAL)
60#if defined(__APPLE__) || \
61 defined(__FreeBSD__) || \
62 defined(__NetBSD__) || \
63 defined(__OpenBSD__) || \
77 static_assert(!std::is_same_v<std::decay_t<T>,
char*>,
"Calling operator<<(CSHA512, char*) is probably not what you want");
78 static_assert(!std::is_same_v<std::decay_t<T>,
unsigned char*>,
"Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
79 static_assert(!std::is_same_v<std::decay_t<T>,
const char*>,
"Calling operator<<(CSHA512, const char*) is probably not what you want");
80 static_assert(!std::is_same_v<std::decay_t<T>,
const unsigned char*>,
"Calling operator<<(CSHA512, const unsigned char*) is probably not what you want");
86void AddSockaddr(
CSHA512& hasher,
const struct sockaddr *addr)
88 if (addr ==
nullptr)
return;
89 switch (addr->sa_family) {
91 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in));
94 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in6));
97 hasher.
Write((
const unsigned char*)&addr->sa_family,
sizeof(addr->sa_family));
101void AddFile(
CSHA512& hasher,
const char *path)
104 int f = open(path, O_RDONLY);
107 unsigned char fbuf[4096];
109 hasher.
Write((
const unsigned char*)&f,
sizeof(f));
110 if (fstat(f, &sb) == 0) hasher << sb;
112 n = read(f, fbuf,
sizeof(fbuf));
113 if (n > 0) hasher.
Write(fbuf, n);
116 }
while (n ==
sizeof(fbuf) && total < 1048576);
121void AddPath(
CSHA512& hasher,
const char *path)
124 if (stat(path, &sb) == 0) {
125 hasher.
Write((
const unsigned char*)path, strlen(path) + 1);
135 int CTL[
sizeof...(S)] = {
S...};
136 unsigned char buffer[65536];
138 int ret = sysctl(CTL,
sizeof...(
S), buffer, &siz,
nullptr, 0);
139 if (
ret == 0 || (
ret == -1 && errno == ENOMEM)) {
140 hasher <<
sizeof(CTL);
141 hasher.
Write((
const unsigned char*)CTL,
sizeof(CTL));
142 if (siz >
sizeof(buffer)) siz =
sizeof(buffer);
144 hasher.
Write(buffer, siz);
150void inline AddCPUID(
CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
152 GetCPUID(leaf, subleaf, ax, bx, cx, dx);
153 hasher << leaf << subleaf << ax << bx << cx << dx;
156void AddAllCPUID(
CSHA512& hasher)
158 uint32_t ax, bx, cx, dx;
160 AddCPUID(hasher, 0, 0, ax, bx, cx, dx);
162 for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
164 for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
165 AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
168 if ((ax & 0x1f) == 0)
break;
169 }
else if (leaf == 7) {
170 if (subleaf == 0) maxsub = ax;
171 if (subleaf == maxsub)
break;
172 }
else if (leaf == 11) {
173 if ((cx & 0xff00) == 0)
break;
174 }
else if (leaf == 13) {
175 if (ax == 0 && bx == 0 && cx == 0 && dx == 0)
break;
183 AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx);
184 uint32_t ext_max = ax;
185 for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
186 AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
197 GetSystemTimeAsFileTime(&ftime);
200 struct timespec ts = {};
201# ifdef CLOCK_MONOTONIC
202 clock_gettime(CLOCK_MONOTONIC, &ts);
205# ifdef CLOCK_REALTIME
206 clock_gettime(CLOCK_REALTIME, &ts);
209# ifdef CLOCK_BOOTTIME
210 clock_gettime(CLOCK_BOOTTIME, &ts);
214 struct timeval tv = {};
215 gettimeofday(&tv,
nullptr);
219 hasher << std::chrono::system_clock::now().time_since_epoch().count();
220 hasher << std::chrono::steady_clock::now().time_since_epoch().count();
221 hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
225 struct rusage usage = {};
226 if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
230 AddFile(hasher,
"/proc/diskstats");
231 AddFile(hasher,
"/proc/vmstat");
232 AddFile(hasher,
"/proc/schedstat");
233 AddFile(hasher,
"/proc/zoneinfo");
234 AddFile(hasher,
"/proc/meminfo");
235 AddFile(hasher,
"/proc/softirqs");
236 AddFile(hasher,
"/proc/stat");
237 AddFile(hasher,
"/proc/self/schedstat");
238 AddFile(hasher,
"/proc/self/status");
243# if defined(KERN_PROC) && defined(KERN_PROC_ALL)
244 AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
249 AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
254 AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
257 AddSysctl<CTL_VM, VM_TOTAL>(hasher);
260 AddSysctl<CTL_VM, VM_METER>(hasher);
266 void* addr = malloc(4097);
267 hasher << &addr << addr;
274 hasher << (CHAR_MIN < 0) <<
sizeof(
void*) <<
sizeof(long) <<
sizeof(
int);
275#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
276 hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
281 hasher << __cplusplus;
283 hasher << _XOPEN_VERSION;
286 const char* COMPILER_VERSION = __VERSION__;
287 hasher.
Write((
const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
293#if defined(HAVE_STRONG_GETAUXVAL)
296 hasher << getauxval(AT_HWCAP);
299 hasher << getauxval(AT_HWCAP2);
302 const unsigned char* random_aux = (
const unsigned char*)getauxval(AT_RANDOM);
303 if (random_aux) hasher.
Write(random_aux, 16);
306 const char* platform_str = (
const char*)getauxval(AT_PLATFORM);
307 if (platform_str) hasher.
Write((
const unsigned char*)platform_str, strlen(platform_str) + 1);
310 const char* exec_str = (
const char*)getauxval(AT_EXECFN);
311 if (exec_str) hasher.
Write((
const unsigned char*)exec_str, strlen(exec_str) + 1);
324 constexpr DWORD max_size = MAX_COMPUTERNAME_LENGTH + 1;
325 char hname[max_size];
326 DWORD size = max_size;
327 if (GetComputerNameA(hname, &size) != 0) {
332 if (gethostname(hname, 256) == 0) {
333 hasher.
Write((
const unsigned char*)hname, strnlen(hname, 256));
339 struct ifaddrs *ifad =
nullptr;
341 struct ifaddrs *ifit = ifad;
342 while (ifit !=
nullptr) {
343 hasher.
Write((
const unsigned char*)&ifit,
sizeof(ifit));
344 hasher.
Write((
const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
345 hasher.
Write((
const unsigned char*)&ifit->ifa_flags,
sizeof(ifit->ifa_flags));
346 AddSockaddr(hasher, ifit->ifa_addr);
347 AddSockaddr(hasher, ifit->ifa_netmask);
348 AddSockaddr(hasher, ifit->ifa_dstaddr);
349 ifit = ifit->ifa_next;
357 if (uname(&
name) != -1) {
358 hasher.
Write((
const unsigned char*)&
name.sysname, strlen(
name.sysname) + 1);
359 hasher.
Write((
const unsigned char*)&
name.nodename, strlen(
name.nodename) + 1);
360 hasher.
Write((
const unsigned char*)&
name.release, strlen(
name.release) + 1);
361 hasher.
Write((
const unsigned char*)&
name.version, strlen(
name.version) + 1);
362 hasher.
Write((
const unsigned char*)&
name.machine, strlen(
name.machine) + 1);
366 AddPath(hasher,
"/");
367 AddPath(hasher,
".");
368 AddPath(hasher,
"/tmp");
369 AddPath(hasher,
"/home");
370 AddPath(hasher,
"/proc");
372 AddFile(hasher,
"/proc/cmdline");
373 AddFile(hasher,
"/proc/cpuinfo");
374 AddFile(hasher,
"/proc/version");
376 AddFile(hasher,
"/etc/passwd");
377 AddFile(hasher,
"/etc/group");
378 AddFile(hasher,
"/etc/hosts");
379 AddFile(hasher,
"/etc/resolv.conf");
380 AddFile(hasher,
"/etc/timezone");
381 AddFile(hasher,
"/etc/localtime");
389 AddSysctl<CTL_HW, HW_MACHINE>(hasher);
392 AddSysctl<CTL_HW, HW_MODEL>(hasher);
395 AddSysctl<CTL_HW, HW_NCPU>(hasher);
398 AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
401 AddSysctl<CTL_HW, HW_USERMEM>(hasher);
403# ifdef HW_MACHINE_ARCH
404 AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
407 AddSysctl<CTL_HW, HW_REALMEM>(hasher);
410 AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
413 AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
416 AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
421 AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
424 AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
426# ifdef KERN_CLOCKRATE
427 AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
430 AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
433 AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
436 AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
438# ifdef KERN_OSRELDATE
439 AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
441# ifdef KERN_OSRELEASE
442 AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
445 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
448 AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
451 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
454 AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
461 for (
size_t i = 0; environ[i]; ++i) {
462 hasher.
Write((
const unsigned char*)environ[i], strlen(environ[i]));
468 hasher << GetCurrentProcessId() << GetCurrentThreadId();
470 hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
472 hasher << std::this_thread::get_id();
A hasher class for SHA-512.
CSHA512 & Write(const unsigned char *data, size_t len)
static const int CLIENT_VERSION
std::ostream & operator<<(std::ostream &os, BigO const &bigO)
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
void RandAddStaticEnv(CSHA512 &hasher)
Gather non-cryptographic environment data that does not change over time.
void RandAddDynamicEnv(CSHA512 &hasher)
Gather non-cryptographic environment data that changes over time.
unsigned char * UCharCast(char *c)