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>
41#if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
45#include <sys/sysctl.h>
46#ifdef HAVE_VM_VM_PARAM_H
47#include <vm/vm_param.h>
49#ifdef HAVE_SYS_RESOURCES_H
50#include <sys/resources.h>
52#ifdef HAVE_SYS_VMMETER_H
53#include <sys/vmmeter.h>
56#if defined(HAVE_STRONG_GETAUXVAL)
73 static_assert(!std::is_same<typename std::decay<T>::type,
char*>::value,
"Calling operator<<(CSHA512, char*) is probably not what you want");
74 static_assert(!std::is_same<typename std::decay<T>::type,
unsigned char*>::value,
"Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75 static_assert(!std::is_same<typename std::decay<T>::type,
const char*>::value,
"Calling operator<<(CSHA512, const char*) is probably not what you want");
76 static_assert(!std::is_same<typename std::decay<T>::type,
const unsigned char*>::value,
"Calling operator<<(CSHA512, const unsigned char*) is probably not what you want");
82void AddSockaddr(
CSHA512& hasher,
const struct sockaddr *addr)
84 if (addr ==
nullptr)
return;
85 switch (addr->sa_family) {
87 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in));
90 hasher.
Write((
const unsigned char*)addr,
sizeof(sockaddr_in6));
93 hasher.
Write((
const unsigned char*)&addr->sa_family,
sizeof(addr->sa_family));
97void AddFile(
CSHA512& hasher,
const char *path)
100 int f = open(path, O_RDONLY);
103 unsigned char fbuf[4096];
105 hasher.
Write((
const unsigned char*)&f,
sizeof(f));
106 if (fstat(f, &sb) == 0) hasher << sb;
108 n = read(f, fbuf,
sizeof(fbuf));
109 if (n > 0) hasher.
Write(fbuf, n);
112 }
while (n ==
sizeof(fbuf) && total < 1048576);
117void AddPath(
CSHA512& hasher,
const char *path)
120 if (stat(path, &sb) == 0) {
121 hasher.
Write((
const unsigned char*)path, strlen(path) + 1);
131 int CTL[
sizeof...(S)] = {
S...};
132 unsigned char buffer[65536];
134 int ret = sysctl(CTL,
sizeof...(
S), buffer, &siz,
nullptr, 0);
135 if (
ret == 0 || (
ret == -1 && errno == ENOMEM)) {
136 hasher <<
sizeof(CTL);
137 hasher.
Write((
const unsigned char*)CTL,
sizeof(CTL));
138 if (siz >
sizeof(buffer)) siz =
sizeof(buffer);
140 hasher.
Write(buffer, siz);
146void inline AddCPUID(
CSHA512& hasher, uint32_t leaf, uint32_t subleaf, uint32_t& ax, uint32_t& bx, uint32_t& cx, uint32_t& dx)
148 GetCPUID(leaf, subleaf, ax, bx, cx, dx);
149 hasher << leaf << subleaf << ax << bx << cx << dx;
152void AddAllCPUID(
CSHA512& hasher)
154 uint32_t ax, bx, cx, dx;
156 AddCPUID(hasher, 0, 0, ax, bx, cx, dx);
158 for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
160 for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
161 AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
164 if ((ax & 0x1f) == 0)
break;
165 }
else if (leaf == 7) {
166 if (subleaf == 0) maxsub = ax;
167 if (subleaf == maxsub)
break;
168 }
else if (leaf == 11) {
169 if ((cx & 0xff00) == 0)
break;
170 }
else if (leaf == 13) {
171 if (ax == 0 && bx == 0 && cx == 0 && dx == 0)
break;
179 AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx);
180 uint32_t ext_max = ax;
181 for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
182 AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
193 GetSystemTimeAsFileTime(&ftime);
196 struct timespec ts = {};
197# ifdef CLOCK_MONOTONIC
198 clock_gettime(CLOCK_MONOTONIC, &ts);
201# ifdef CLOCK_REALTIME
202 clock_gettime(CLOCK_REALTIME, &ts);
205# ifdef CLOCK_BOOTTIME
206 clock_gettime(CLOCK_BOOTTIME, &ts);
210 struct timeval tv = {};
211 gettimeofday(&tv,
nullptr);
215 hasher << std::chrono::system_clock::now().time_since_epoch().count();
216 hasher << std::chrono::steady_clock::now().time_since_epoch().count();
217 hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();
221 struct rusage usage = {};
222 if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
226 AddFile(hasher,
"/proc/diskstats");
227 AddFile(hasher,
"/proc/vmstat");
228 AddFile(hasher,
"/proc/schedstat");
229 AddFile(hasher,
"/proc/zoneinfo");
230 AddFile(hasher,
"/proc/meminfo");
231 AddFile(hasher,
"/proc/softirqs");
232 AddFile(hasher,
"/proc/stat");
233 AddFile(hasher,
"/proc/self/schedstat");
234 AddFile(hasher,
"/proc/self/status");
239# if defined(KERN_PROC) && defined(KERN_PROC_ALL)
240 AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
245 AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
250 AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
253 AddSysctl<CTL_VM, VM_TOTAL>(hasher);
256 AddSysctl<CTL_VM, VM_METER>(hasher);
262 void* addr = malloc(4097);
263 hasher << &addr << addr;
270 hasher << (CHAR_MIN < 0) <<
sizeof(
void*) <<
sizeof(long) <<
sizeof(
int);
271#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
272 hasher << __GNUC__ << __GNUC_MINOR__ << __GNUC_PATCHLEVEL__;
277 hasher << __cplusplus;
279 hasher << _XOPEN_VERSION;
282 const char* COMPILER_VERSION = __VERSION__;
283 hasher.
Write((
const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
289#if defined(HAVE_STRONG_GETAUXVAL)
292 hasher << getauxval(AT_HWCAP);
295 hasher << getauxval(AT_HWCAP2);
298 const unsigned char* random_aux = (
const unsigned char*)getauxval(AT_RANDOM);
299 if (random_aux) hasher.
Write(random_aux, 16);
302 const char* platform_str = (
const char*)getauxval(AT_PLATFORM);
303 if (platform_str) hasher.
Write((
const unsigned char*)platform_str, strlen(platform_str) + 1);
306 const char* exec_str = (
const char*)getauxval(AT_EXECFN);
307 if (exec_str) hasher.
Write((
const unsigned char*)exec_str, strlen(exec_str) + 1);
320 constexpr DWORD max_size = MAX_COMPUTERNAME_LENGTH + 1;
321 char hname[max_size];
322 DWORD size = max_size;
323 if (GetComputerNameA(hname, &size) != 0) {
328 if (gethostname(hname, 256) == 0) {
329 hasher.
Write((
const unsigned char*)hname, strnlen(hname, 256));
333#if HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS
335 struct ifaddrs *ifad =
nullptr;
337 struct ifaddrs *ifit = ifad;
338 while (ifit !=
nullptr) {
339 hasher.
Write((
const unsigned char*)&ifit,
sizeof(ifit));
340 hasher.
Write((
const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
341 hasher.
Write((
const unsigned char*)&ifit->ifa_flags,
sizeof(ifit->ifa_flags));
342 AddSockaddr(hasher, ifit->ifa_addr);
343 AddSockaddr(hasher, ifit->ifa_netmask);
344 AddSockaddr(hasher, ifit->ifa_dstaddr);
345 ifit = ifit->ifa_next;
353 if (uname(&
name) != -1) {
354 hasher.
Write((
const unsigned char*)&
name.sysname, strlen(
name.sysname) + 1);
355 hasher.
Write((
const unsigned char*)&
name.nodename, strlen(
name.nodename) + 1);
356 hasher.
Write((
const unsigned char*)&
name.release, strlen(
name.release) + 1);
357 hasher.
Write((
const unsigned char*)&
name.version, strlen(
name.version) + 1);
358 hasher.
Write((
const unsigned char*)&
name.machine, strlen(
name.machine) + 1);
362 AddPath(hasher,
"/");
363 AddPath(hasher,
".");
364 AddPath(hasher,
"/tmp");
365 AddPath(hasher,
"/home");
366 AddPath(hasher,
"/proc");
368 AddFile(hasher,
"/proc/cmdline");
369 AddFile(hasher,
"/proc/cpuinfo");
370 AddFile(hasher,
"/proc/version");
372 AddFile(hasher,
"/etc/passwd");
373 AddFile(hasher,
"/etc/group");
374 AddFile(hasher,
"/etc/hosts");
375 AddFile(hasher,
"/etc/resolv.conf");
376 AddFile(hasher,
"/etc/timezone");
377 AddFile(hasher,
"/etc/localtime");
385 AddSysctl<CTL_HW, HW_MACHINE>(hasher);
388 AddSysctl<CTL_HW, HW_MODEL>(hasher);
391 AddSysctl<CTL_HW, HW_NCPU>(hasher);
394 AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
397 AddSysctl<CTL_HW, HW_USERMEM>(hasher);
399# ifdef HW_MACHINE_ARCH
400 AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
403 AddSysctl<CTL_HW, HW_REALMEM>(hasher);
406 AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
409 AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
412 AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
417 AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
420 AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
422# ifdef KERN_CLOCKRATE
423 AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
426 AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
429 AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
432 AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
434# ifdef KERN_OSRELDATE
435 AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
437# ifdef KERN_OSRELEASE
438 AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
441 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
444 AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
447 AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
450 AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
457 for (
size_t i = 0;
environ[i]; ++i) {
464 hasher << GetCurrentProcessId() << GetCurrentThreadId();
466 hasher << getpid() << getppid() << getsid(0) << getpgid(0) << getuid() << geteuid() << getgid() << getegid();
468 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
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
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)