util.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include "util.h"
  3. #include "sync.h"
  4. #include "strlcpy.h"
  5. #include "version.h"
  6. #include "ui_interface.h"
  7. #include <boost/algorithm/string/join.hpp>
  8. namespace boost {
  9. namespace program_options {
  10. std::string to_internal(const std::string&);
  11. }
  12. }
  13. #include <boost/program_options/detail/config_file.hpp>
  14. #include <boost/program_options/parsers.hpp>
  15. #include <boost/filesystem.hpp>
  16. #include <boost/filesystem/fstream.hpp>
  17. #include <boost/foreach.hpp>
  18. #include <boost/thread.hpp>
  19. #include <openssl/crypto.h>
  20. #include <openssl/rand.h>
  21. #include <stdarg.h>
  22. #include <boost/bind/bind.hpp>
  23. #include <boost/bind/placeholders.hpp>
  24. using namespace boost::placeholders;
  25. #ifdef WIN32
  26. #ifdef _MSC_VER
  27. #pragma warning(disable:4786)
  28. #pragma warning(disable:4804)
  29. #pragma warning(disable:4805)
  30. #pragma warning(disable:4717)
  31. #endif
  32. #ifdef _WIN32_WINNT
  33. #undef _WIN32_WINNT
  34. #endif
  35. #define _WIN32_WINNT 0x0501
  36. #ifdef _WIN32_IE
  37. #undef _WIN32_IE
  38. #endif
  39. #define _WIN32_IE 0x0501
  40. #define WIN32_LEAN_AND_MEAN 1
  41. #ifndef NOMINMAX
  42. #define NOMINMAX
  43. #endif
  44. #include <io.h> /* for _commit */
  45. #include "shlobj.h"
  46. #elif defined(__linux__)
  47. # include <sys/prctl.h>
  48. #endif
  49. #ifndef WIN32
  50. #include <execinfo.h>
  51. #endif
  52. using namespace std;
  53. map<string, string> mapArgs;
  54. map<string, vector<string> > mapMultiArgs;
  55. bool fDebug = false;
  56. bool fDebugNet = false;
  57. bool fPrintToConsole = false;
  58. bool fPrintToDebugger = false;
  59. bool fRequestShutdown = false;
  60. bool fShutdown = false;
  61. bool fDaemon = false;
  62. bool fServer = false;
  63. bool fCommandLine = false;
  64. string strMiscWarning;
  65. bool fTestNet = false;
  66. bool fNoListen = false;
  67. bool fLogTimestamps = false;
  68. CMedianFilter<int64> vTimeOffsets(200,0);
  69. bool fReopenDebugLog = false;
  70. // Init OpenSSL library multithreading support
  71. static CCriticalSection** ppmutexOpenSSL;
  72. void locking_callback(int mode, int i, const char* file, int line)
  73. {
  74. if (mode & CRYPTO_LOCK) {
  75. ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
  76. } else {
  77. LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
  78. }
  79. }
  80. LockedPageManager LockedPageManager::instance;
  81. // Init
  82. class CInit
  83. {
  84. public:
  85. CInit()
  86. {
  87. // Init OpenSSL library multithreading support
  88. ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
  89. for (int i = 0; i < CRYPTO_num_locks(); i++)
  90. ppmutexOpenSSL[i] = new CCriticalSection();
  91. CRYPTO_set_locking_callback(locking_callback);
  92. #ifdef WIN32
  93. // Seed random number generator with screen scrape and other hardware sources
  94. RAND_screen();
  95. #endif
  96. // Seed random number generator with performance counter
  97. RandAddSeed();
  98. }
  99. ~CInit()
  100. {
  101. // Shutdown OpenSSL library multithreading support
  102. CRYPTO_set_locking_callback(NULL);
  103. for (int i = 0; i < CRYPTO_num_locks(); i++)
  104. delete ppmutexOpenSSL[i];
  105. OPENSSL_free(ppmutexOpenSSL);
  106. }
  107. }
  108. instance_of_cinit;
  109. void RandAddSeed()
  110. {
  111. // Seed with CPU performance counter
  112. int64 nCounter = GetPerformanceCounter();
  113. RAND_add(&nCounter, sizeof(nCounter), 1.5);
  114. memset(&nCounter, 0, sizeof(nCounter));
  115. }
  116. void RandAddSeedPerfmon()
  117. {
  118. RandAddSeed();
  119. // This can take up to 2 seconds, so only do it every 10 minutes
  120. static int64 nLastPerfmon;
  121. if (GetTime() < nLastPerfmon + 10 * 60)
  122. return;
  123. nLastPerfmon = GetTime();
  124. #ifdef WIN32
  125. // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
  126. // Seed with the entire set of perfmon data
  127. unsigned char pdata[250000];
  128. memset(pdata, 0, sizeof(pdata));
  129. unsigned long nSize = sizeof(pdata);
  130. long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
  131. RegCloseKey(HKEY_PERFORMANCE_DATA);
  132. if (ret == ERROR_SUCCESS)
  133. {
  134. RAND_add(pdata, nSize, nSize/100.0);
  135. memset(pdata, 0, nSize);
  136. printf("RandAddSeed() %lu bytes\n", nSize);
  137. }
  138. #endif
  139. }
  140. uint64 GetRand(uint64 nMax)
  141. {
  142. if (nMax == 0)
  143. return 0;
  144. // The range of the random source must be a multiple of the modulus
  145. // to give every possible output value an equal possibility
  146. uint64 nRange = (std::numeric_limits<uint64>::max() / nMax) * nMax;
  147. uint64 nRand = 0;
  148. do
  149. RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
  150. while (nRand >= nRange);
  151. return (nRand % nMax);
  152. }
  153. int GetRandInt(int nMax)
  154. {
  155. return GetRand(nMax);
  156. }
  157. uint256 GetRandHash()
  158. {
  159. uint256 hash;
  160. RAND_bytes((unsigned char*)&hash, sizeof(hash));
  161. return hash;
  162. }
  163. static FILE* fileout = NULL;
  164. inline int OutputDebugStringF(const char* pszFormat, ...)
  165. {
  166. int ret = 0;
  167. if (fPrintToConsole)
  168. {
  169. // print to console
  170. va_list arg_ptr;
  171. va_start(arg_ptr, pszFormat);
  172. ret = vprintf(pszFormat, arg_ptr);
  173. va_end(arg_ptr);
  174. }
  175. else if (!fPrintToDebugger)
  176. {
  177. // print to debug.log
  178. if (!fileout)
  179. {
  180. boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
  181. fileout = fopen(pathDebug.string().c_str(), "a");
  182. if (fileout) setbuf(fileout, NULL); // unbuffered
  183. }
  184. if (fileout)
  185. {
  186. static bool fStartedNewLine = true;
  187. // This routine may be called by global destructors during shutdown.
  188. // Since the order of destruction of static/global objects is undefined,
  189. // allocate mutexDebugLog on the heap the first time this routine
  190. // is called to avoid crashes during shutdown.
  191. static boost::mutex* mutexDebugLog = NULL;
  192. if (mutexDebugLog == NULL) mutexDebugLog = new boost::mutex();
  193. boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
  194. // reopen the log file, if requested
  195. if (fReopenDebugLog) {
  196. fReopenDebugLog = false;
  197. boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
  198. if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
  199. setbuf(fileout, NULL); // unbuffered
  200. }
  201. // Debug print useful for profiling
  202. if (fLogTimestamps && fStartedNewLine)
  203. fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
  204. if (pszFormat[strlen(pszFormat) - 1] == '\n')
  205. fStartedNewLine = true;
  206. else
  207. fStartedNewLine = false;
  208. va_list arg_ptr;
  209. va_start(arg_ptr, pszFormat);
  210. ret = vfprintf(fileout, pszFormat, arg_ptr);
  211. va_end(arg_ptr);
  212. }
  213. }
  214. #ifdef WIN32
  215. if (fPrintToDebugger)
  216. {
  217. static CCriticalSection cs_OutputDebugStringF;
  218. // accumulate and output a line at a time
  219. {
  220. LOCK(cs_OutputDebugStringF);
  221. static std::string buffer;
  222. va_list arg_ptr;
  223. va_start(arg_ptr, pszFormat);
  224. buffer += vstrprintf(pszFormat, arg_ptr);
  225. va_end(arg_ptr);
  226. int line_start = 0, line_end;
  227. while((line_end = buffer.find('\n', line_start)) != -1)
  228. {
  229. OutputDebugStringA(buffer.substr(line_start, line_end - line_start).c_str());
  230. line_start = line_end + 1;
  231. }
  232. buffer.erase(0, line_start);
  233. }
  234. }
  235. #endif
  236. return ret;
  237. }
  238. string vstrprintf(const char *format, va_list ap)
  239. {
  240. char buffer[50000];
  241. char* p = buffer;
  242. int limit = sizeof(buffer);
  243. int ret;
  244. while (true)
  245. {
  246. va_list arg_ptr;
  247. va_copy(arg_ptr, ap);
  248. #ifdef WIN32
  249. ret = _vsnprintf(p, limit, format, arg_ptr);
  250. #else
  251. ret = vsnprintf(p, limit, format, arg_ptr);
  252. #endif
  253. va_end(arg_ptr);
  254. if (ret >= 0 && ret < limit)
  255. break;
  256. if (p != buffer)
  257. delete[] p;
  258. limit *= 2;
  259. p = new char[limit];
  260. if (p == NULL)
  261. throw std::bad_alloc();
  262. }
  263. string str(p, p+ret);
  264. if (p != buffer)
  265. delete[] p;
  266. return str;
  267. }
  268. string real_strprintf(const char *format, int dummy, ...)
  269. {
  270. va_list arg_ptr;
  271. va_start(arg_ptr, dummy);
  272. string str = vstrprintf(format, arg_ptr);
  273. va_end(arg_ptr);
  274. return str;
  275. }
  276. string real_strprintf(const std::string &format, int dummy, ...)
  277. {
  278. va_list arg_ptr;
  279. va_start(arg_ptr, dummy);
  280. string str = vstrprintf(format.c_str(), arg_ptr);
  281. va_end(arg_ptr);
  282. return str;
  283. }
  284. bool error(const char *format, ...)
  285. {
  286. va_list arg_ptr;
  287. va_start(arg_ptr, format);
  288. std::string str = vstrprintf(format, arg_ptr);
  289. va_end(arg_ptr);
  290. printf("ERROR: %s\n", str.c_str());
  291. return false;
  292. }
  293. void ParseString(const string& str, char c, vector<string>& v)
  294. {
  295. if (str.empty())
  296. return;
  297. string::size_type i1 = 0;
  298. string::size_type i2;
  299. while (true)
  300. {
  301. i2 = str.find(c, i1);
  302. if (i2 == str.npos)
  303. {
  304. v.push_back(str.substr(i1));
  305. return;
  306. }
  307. v.push_back(str.substr(i1, i2-i1));
  308. i1 = i2+1;
  309. }
  310. }
  311. string FormatMoney(int64 n, bool fPlus)
  312. {
  313. // Note: not using straight sprintf here because we do NOT want
  314. // localized number formatting.
  315. int64 n_abs = (n > 0 ? n : -n);
  316. int64 quotient = n_abs/COIN;
  317. int64 remainder = n_abs%COIN;
  318. string str = strprintf("%" PRI64d".%08" PRI64d, quotient, remainder);
  319. // Right-trim excess zeros before the decimal point:
  320. int nTrim = 0;
  321. for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
  322. ++nTrim;
  323. if (nTrim)
  324. str.erase(str.size()-nTrim, nTrim);
  325. if (n < 0)
  326. str.insert((unsigned int)0, 1, '-');
  327. else if (fPlus && n > 0)
  328. str.insert((unsigned int)0, 1, '+');
  329. return str;
  330. }
  331. bool ParseMoney(const string& str, int64& nRet)
  332. {
  333. return ParseMoney(str.c_str(), nRet);
  334. }
  335. bool ParseMoney(const char* pszIn, int64& nRet)
  336. {
  337. string strWhole;
  338. int64 nUnits = 0;
  339. const char* p = pszIn;
  340. while (isspace(*p))
  341. p++;
  342. for (; *p; p++)
  343. {
  344. if (*p == '.')
  345. {
  346. p++;
  347. int64 nMult = CENT*10;
  348. while (isdigit(*p) && (nMult > 0))
  349. {
  350. nUnits += nMult * (*p++ - '0');
  351. nMult /= 10;
  352. }
  353. break;
  354. }
  355. if (isspace(*p))
  356. break;
  357. if (!isdigit(*p))
  358. return false;
  359. strWhole.insert(strWhole.end(), *p);
  360. }
  361. for (; *p; p++)
  362. if (!isspace(*p))
  363. return false;
  364. if (strWhole.size() > 10) // guard against 63 bit overflow
  365. return false;
  366. if (nUnits < 0 || nUnits > COIN)
  367. return false;
  368. int64 nWhole = atoi64(strWhole);
  369. int64 nValue = nWhole*COIN + nUnits;
  370. nRet = nValue;
  371. return true;
  372. }
  373. static const signed char phexdigit[256] =
  374. { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  375. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  376. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  377. 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
  378. -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  379. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  380. -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  381. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  382. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  383. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  384. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  385. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  386. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  387. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  388. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  389. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
  390. static const long hexGrid[] =
  391. {
  392. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  393. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-19
  394. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  395. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 30-39
  396. -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
  397. 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, // 50-59
  398. -1, -1, -1, -1, -1, 10, 11, 12, 13, 14,
  399. 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 70-79
  400. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  401. -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, // 90-99
  402. 13, 14, 15, -1, -1, -1, -1, -1, -1, -1,
  403. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 110-109
  404. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  405. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 130-139
  406. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  407. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 150-159
  408. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  409. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 170-179
  410. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  411. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 190-199
  412. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  413. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 210-219
  414. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  415. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 230-239
  416. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  417. -1, -1, -1, -1, -1, -1
  418. };
  419. long ConvertLong(const char* cHex)
  420. {
  421. long lSeed = 0;
  422. while (*cHex && lSeed >= 0)
  423. {
  424. lSeed = (lSeed << 4) | hexGrid[(uint8_t)*cHex++];
  425. }
  426. return lSeed;
  427. }
  428. bool IsHex(const string& str)
  429. {
  430. BOOST_FOREACH(unsigned char c, str)
  431. {
  432. if (phexdigit[c] < 0)
  433. return false;
  434. }
  435. return (str.size() > 0) && (str.size()%2 == 0);
  436. }
  437. vector<unsigned char> ParseHex(const char* psz)
  438. {
  439. // convert hex dump to vector
  440. vector<unsigned char> vch;
  441. while (true)
  442. {
  443. while (isspace(*psz))
  444. psz++;
  445. signed char c = phexdigit[(unsigned char)*psz++];
  446. if (c == (signed char)-1)
  447. break;
  448. unsigned char n = (c << 4);
  449. c = phexdigit[(unsigned char)*psz++];
  450. if (c == (signed char)-1)
  451. break;
  452. n |= c;
  453. vch.push_back(n);
  454. }
  455. return vch;
  456. }
  457. vector<unsigned char> ParseHex(const string& str)
  458. {
  459. return ParseHex(str.c_str());
  460. }
  461. static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
  462. {
  463. // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
  464. if (name.find("-no") == 0)
  465. {
  466. std::string positive("-");
  467. positive.append(name.begin()+3, name.end());
  468. if (mapSettingsRet.count(positive) == 0)
  469. {
  470. bool value = !GetBoolArg(name);
  471. mapSettingsRet[positive] = (value ? "1" : "0");
  472. }
  473. }
  474. }
  475. void ParseParameters(int argc, const char* const argv[])
  476. {
  477. mapArgs.clear();
  478. mapMultiArgs.clear();
  479. for (int i = 1; i < argc; i++)
  480. {
  481. char psz[10000];
  482. strlcpy(psz, argv[i], sizeof(psz));
  483. char* pszValue = (char*)"";
  484. if (strchr(psz, '='))
  485. {
  486. pszValue = strchr(psz, '=');
  487. *pszValue++ = '\0';
  488. }
  489. #ifdef WIN32
  490. _strlwr(psz);
  491. if (psz[0] == '/')
  492. psz[0] = '-';
  493. #endif
  494. if (psz[0] != '-')
  495. break;
  496. mapArgs[psz] = pszValue;
  497. mapMultiArgs[psz].push_back(pszValue);
  498. }
  499. // New 0.6 features:
  500. BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
  501. {
  502. string name = entry.first;
  503. // interpret --foo as -foo (as long as both are not set)
  504. if (name.find("--") == 0)
  505. {
  506. std::string singleDash(name.begin()+1, name.end());
  507. if (mapArgs.count(singleDash) == 0)
  508. mapArgs[singleDash] = entry.second;
  509. name = singleDash;
  510. }
  511. // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
  512. InterpretNegativeSetting(name, mapArgs);
  513. }
  514. }
  515. std::string GetArg(const std::string& strArg, const std::string& strDefault)
  516. {
  517. if (mapArgs.count(strArg))
  518. return mapArgs[strArg];
  519. return strDefault;
  520. }
  521. int64 GetArg(const std::string& strArg, int64 nDefault)
  522. {
  523. if (mapArgs.count(strArg))
  524. return atoi64(mapArgs[strArg]);
  525. return nDefault;
  526. }
  527. bool GetBoolArg(const std::string& strArg, bool fDefault)
  528. {
  529. if (mapArgs.count(strArg))
  530. {
  531. if (mapArgs[strArg].empty())
  532. return true;
  533. return (atoi(mapArgs[strArg]) != 0);
  534. }
  535. return fDefault;
  536. }
  537. bool SoftSetArg(const std::string& strArg, const std::string& strValue)
  538. {
  539. if (mapArgs.count(strArg))
  540. return false;
  541. mapArgs[strArg] = strValue;
  542. return true;
  543. }
  544. bool SoftSetBoolArg(const std::string& strArg, bool fValue)
  545. {
  546. if (fValue)
  547. return SoftSetArg(strArg, std::string("1"));
  548. else
  549. return SoftSetArg(strArg, std::string("0"));
  550. }
  551. string EncodeBase64(const unsigned char* pch, size_t len)
  552. {
  553. static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  554. string strRet="";
  555. strRet.reserve((len+2)/3*4);
  556. int mode=0, left=0;
  557. const unsigned char *pchEnd = pch+len;
  558. while (pch<pchEnd)
  559. {
  560. int enc = *(pch++);
  561. switch (mode)
  562. {
  563. case 0: // we have no bits
  564. strRet += pbase64[enc >> 2];
  565. left = (enc & 3) << 4;
  566. mode = 1;
  567. break;
  568. case 1: // we have two bits
  569. strRet += pbase64[left | (enc >> 4)];
  570. left = (enc & 15) << 2;
  571. mode = 2;
  572. break;
  573. case 2: // we have four bits
  574. strRet += pbase64[left | (enc >> 6)];
  575. strRet += pbase64[enc & 63];
  576. mode = 0;
  577. break;
  578. }
  579. }
  580. if (mode)
  581. {
  582. strRet += pbase64[left];
  583. strRet += '=';
  584. if (mode == 1)
  585. strRet += '=';
  586. }
  587. return strRet;
  588. }
  589. string EncodeBase64(const string& str)
  590. {
  591. return EncodeBase64((const unsigned char*)str.c_str(), str.size());
  592. }
  593. vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
  594. {
  595. static const int decode64_table[256] =
  596. {
  597. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  598. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  599. -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
  600. -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  601. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
  602. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  603. 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  604. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  605. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  606. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  607. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  608. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  609. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  610. };
  611. if (pfInvalid)
  612. *pfInvalid = false;
  613. vector<unsigned char> vchRet;
  614. vchRet.reserve(strlen(p)*3/4);
  615. int mode = 0;
  616. int left = 0;
  617. while (1)
  618. {
  619. int dec = decode64_table[(unsigned char)*p];
  620. if (dec == -1) break;
  621. p++;
  622. switch (mode)
  623. {
  624. case 0: // we have no bits and get 6
  625. left = dec;
  626. mode = 1;
  627. break;
  628. case 1: // we have 6 bits and keep 4
  629. vchRet.push_back((left<<2) | (dec>>4));
  630. left = dec & 15;
  631. mode = 2;
  632. break;
  633. case 2: // we have 4 bits and get 6, we keep 2
  634. vchRet.push_back((left<<4) | (dec>>2));
  635. left = dec & 3;
  636. mode = 3;
  637. break;
  638. case 3: // we have 2 bits and get 6
  639. vchRet.push_back((left<<6) | dec);
  640. mode = 0;
  641. break;
  642. }
  643. }
  644. if (pfInvalid)
  645. switch (mode)
  646. {
  647. case 0: // 4n base64 characters processed: ok
  648. break;
  649. case 1: // 4n+1 base64 character processed: impossible
  650. *pfInvalid = true;
  651. break;
  652. case 2: // 4n+2 base64 characters processed: require '=='
  653. if (left || p[0] != '=' || p[1] != '=' || decode64_table[(unsigned char)p[2]] != -1)
  654. *pfInvalid = true;
  655. break;
  656. case 3: // 4n+3 base64 characters processed: require '='
  657. if (left || p[0] != '=' || decode64_table[(unsigned char)p[1]] != -1)
  658. *pfInvalid = true;
  659. break;
  660. }
  661. return vchRet;
  662. }
  663. string DecodeBase64(const string& str)
  664. {
  665. vector<unsigned char> vchRet = DecodeBase64(str.c_str());
  666. return string((const char*)&vchRet[0], vchRet.size());
  667. }
  668. string EncodeBase32(const unsigned char* pch, size_t len)
  669. {
  670. static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
  671. string strRet="";
  672. strRet.reserve((len+4)/5*8);
  673. int mode=0, left=0;
  674. const unsigned char *pchEnd = pch+len;
  675. while (pch<pchEnd)
  676. {
  677. int enc = *(pch++);
  678. switch (mode)
  679. {
  680. case 0: // we have no bits
  681. strRet += pbase32[enc >> 3];
  682. left = (enc & 7) << 2;
  683. mode = 1;
  684. break;
  685. case 1: // we have three bits
  686. strRet += pbase32[left | (enc >> 6)];
  687. strRet += pbase32[(enc >> 1) & 31];
  688. left = (enc & 1) << 4;
  689. mode = 2;
  690. break;
  691. case 2: // we have one bit
  692. strRet += pbase32[left | (enc >> 4)];
  693. left = (enc & 15) << 1;
  694. mode = 3;
  695. break;
  696. case 3: // we have four bits
  697. strRet += pbase32[left | (enc >> 7)];
  698. strRet += pbase32[(enc >> 2) & 31];
  699. left = (enc & 3) << 3;
  700. mode = 4;
  701. break;
  702. case 4: // we have two bits
  703. strRet += pbase32[left | (enc >> 5)];
  704. strRet += pbase32[enc & 31];
  705. mode = 0;
  706. }
  707. }
  708. static const int nPadding[5] = {0, 6, 4, 3, 1};
  709. if (mode)
  710. {
  711. strRet += pbase32[left];
  712. for (int n=0; n<nPadding[mode]; n++)
  713. strRet += '=';
  714. }
  715. return strRet;
  716. }
  717. string EncodeBase32(const string& str)
  718. {
  719. return EncodeBase32((const unsigned char*)str.c_str(), str.size());
  720. }
  721. vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
  722. {
  723. static const int decode32_table[256] =
  724. {
  725. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  726. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  727. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
  728. -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  729. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
  730. 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
  731. 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  732. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  733. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  734. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  735. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  736. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  737. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  738. };
  739. if (pfInvalid)
  740. *pfInvalid = false;
  741. vector<unsigned char> vchRet;
  742. vchRet.reserve((strlen(p))*5/8);
  743. int mode = 0;
  744. int left = 0;
  745. while (1)
  746. {
  747. int dec = decode32_table[(unsigned char)*p];
  748. if (dec == -1) break;
  749. p++;
  750. switch (mode)
  751. {
  752. case 0: // we have no bits and get 5
  753. left = dec;
  754. mode = 1;
  755. break;
  756. case 1: // we have 5 bits and keep 2
  757. vchRet.push_back((left<<3) | (dec>>2));
  758. left = dec & 3;
  759. mode = 2;
  760. break;
  761. case 2: // we have 2 bits and keep 7
  762. left = left << 5 | dec;
  763. mode = 3;
  764. break;
  765. case 3: // we have 7 bits and keep 4
  766. vchRet.push_back((left<<1) | (dec>>4));
  767. left = dec & 15;
  768. mode = 4;
  769. break;
  770. case 4: // we have 4 bits, and keep 1
  771. vchRet.push_back((left<<4) | (dec>>1));
  772. left = dec & 1;
  773. mode = 5;
  774. break;
  775. case 5: // we have 1 bit, and keep 6
  776. left = left << 5 | dec;
  777. mode = 6;
  778. break;
  779. case 6: // we have 6 bits, and keep 3
  780. vchRet.push_back((left<<2) | (dec>>3));
  781. left = dec & 7;
  782. mode = 7;
  783. break;
  784. case 7: // we have 3 bits, and keep 0
  785. vchRet.push_back((left<<5) | dec);
  786. mode = 0;
  787. break;
  788. }
  789. }
  790. if (pfInvalid)
  791. switch (mode)
  792. {
  793. case 0: // 8n base32 characters processed: ok
  794. break;
  795. case 1: // 8n+1 base32 characters processed: impossible
  796. case 3: // +3
  797. case 6: // +6
  798. *pfInvalid = true;
  799. break;
  800. case 2: // 8n+2 base32 characters processed: require '======'
  801. if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || p[4] != '=' || p[5] != '=' || decode32_table[(unsigned char)p[6]] != -1)
  802. *pfInvalid = true;
  803. break;
  804. case 4: // 8n+4 base32 characters processed: require '===='
  805. if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || decode32_table[(unsigned char)p[4]] != -1)
  806. *pfInvalid = true;
  807. break;
  808. case 5: // 8n+5 base32 characters processed: require '==='
  809. if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || decode32_table[(unsigned char)p[3]] != -1)
  810. *pfInvalid = true;
  811. break;
  812. case 7: // 8n+7 base32 characters processed: require '='
  813. if (left || p[0] != '=' || decode32_table[(unsigned char)p[1]] != -1)
  814. *pfInvalid = true;
  815. break;
  816. }
  817. return vchRet;
  818. }
  819. string DecodeBase32(const string& str)
  820. {
  821. vector<unsigned char> vchRet = DecodeBase32(str.c_str());
  822. return string((const char*)&vchRet[0], vchRet.size());
  823. }
  824. bool WildcardMatch(const char* psz, const char* mask)
  825. {
  826. while (true)
  827. {
  828. switch (*mask)
  829. {
  830. case '\0':
  831. return (*psz == '\0');
  832. case '*':
  833. return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask));
  834. case '?':
  835. if (*psz == '\0')
  836. return false;
  837. break;
  838. default:
  839. if (*psz != *mask)
  840. return false;
  841. break;
  842. }
  843. psz++;
  844. mask++;
  845. }
  846. }
  847. bool WildcardMatch(const string& str, const string& mask)
  848. {
  849. return WildcardMatch(str.c_str(), mask.c_str());
  850. }
  851. static std::string FormatException(std::exception* pex, const char* pszThread)
  852. {
  853. #ifdef WIN32
  854. char pszModule[MAX_PATH] = "";
  855. GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
  856. #else
  857. const char* pszModule = "ecoin";
  858. #endif
  859. if (pex)
  860. return strprintf(
  861. "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
  862. else
  863. return strprintf(
  864. "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
  865. }
  866. void LogException(std::exception* pex, const char* pszThread)
  867. {
  868. std::string message = FormatException(pex, pszThread);
  869. printf("\n%s", message.c_str());
  870. }
  871. void PrintException(std::exception* pex, const char* pszThread)
  872. {
  873. std::string message = FormatException(pex, pszThread);
  874. printf("\n\n************************\n%s\n", message.c_str());
  875. fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
  876. strMiscWarning = message;
  877. throw;
  878. }
  879. void LogStackTrace() {
  880. printf("\n\n******* exception encountered *******\n");
  881. if (fileout)
  882. {
  883. #ifndef WIN32
  884. void* pszBuffer[32];
  885. size_t size;
  886. size = backtrace(pszBuffer, 32);
  887. backtrace_symbols_fd(pszBuffer, size, fileno(fileout));
  888. #endif
  889. }
  890. }
  891. void PrintExceptionContinue(std::exception* pex, const char* pszThread)
  892. {
  893. std::string message = FormatException(pex, pszThread);
  894. printf("\n\n************************\n%s\n", message.c_str());
  895. fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
  896. strMiscWarning = message;
  897. }
  898. boost::filesystem::path GetDefaultDataDir()
  899. {
  900. namespace fs = boost::filesystem;
  901. // Windows < Vista: C:\Documents and Settings\Username\Application Data\Ecoin
  902. // Windows >= Vista: C:\Users\Username\AppData\Roaming\Ecoin
  903. // Mac: ~/Library/Application Support/Ecoin
  904. // Unix: ~/.ecoin
  905. #ifdef WIN32
  906. // Windows
  907. return GetSpecialFolderPath(CSIDL_APPDATA) / "Ecoin";
  908. #else
  909. fs::path pathRet;
  910. char* pszHome = getenv("HOME");
  911. if (pszHome == NULL || strlen(pszHome) == 0)
  912. pathRet = fs::path("/");
  913. else
  914. pathRet = fs::path(pszHome);
  915. #ifdef MAC_OSX
  916. // Mac
  917. pathRet /= "Library/Application Support";
  918. fs::create_directory(pathRet);
  919. return pathRet / "Ecoin";
  920. #else
  921. // Unix
  922. return pathRet / ".ecoin";
  923. #endif
  924. #endif
  925. }
  926. const boost::filesystem::path &GetDataDir(bool fNetSpecific)
  927. {
  928. namespace fs = boost::filesystem;
  929. static fs::path pathCached[2];
  930. static CCriticalSection csPathCached;
  931. static bool cachedPath[2] = {false, false};
  932. fs::path &path = pathCached[fNetSpecific];
  933. // This can be called during exceptions by printf, so we cache the
  934. // value so we don't have to do memory allocations after that.
  935. if (cachedPath[fNetSpecific])
  936. return path;
  937. LOCK(csPathCached);
  938. if (mapArgs.count("-datadir")) {
  939. path = fs::system_complete(mapArgs["-datadir"]);
  940. if (!fs::is_directory(path)) {
  941. path = "";
  942. return path;
  943. }
  944. } else {
  945. path = GetDefaultDataDir();
  946. }
  947. if (fNetSpecific && GetBoolArg("-testnet", false))
  948. path /= "testnet";
  949. fs::create_directory(path);
  950. cachedPath[fNetSpecific]=true;
  951. return path;
  952. }
  953. boost::filesystem::path GetConfigFile()
  954. {
  955. boost::filesystem::path pathConfigFile(GetArg("-conf", "ecoin.conf"));
  956. if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir(false) / pathConfigFile;
  957. return pathConfigFile;
  958. }
  959. void ReadConfigFile(map<string, string>& mapSettingsRet,
  960. map<string, vector<string> >& mapMultiSettingsRet)
  961. {
  962. boost::filesystem::ifstream streamConfig(GetConfigFile());
  963. if (!streamConfig.good())
  964. return; // No ecoin.conf file is OK
  965. set<string> setOptions;
  966. setOptions.insert("*");
  967. for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
  968. {
  969. // Don't overwrite existing settings so command line settings override ecoin.conf
  970. string strKey = string("-") + it->string_key;
  971. if (mapSettingsRet.count(strKey) == 0)
  972. {
  973. mapSettingsRet[strKey] = it->value[0];
  974. // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
  975. InterpretNegativeSetting(strKey, mapSettingsRet);
  976. }
  977. mapMultiSettingsRet[strKey].push_back(it->value[0]);
  978. }
  979. }
  980. boost::filesystem::path GetPidFile()
  981. {
  982. boost::filesystem::path pathPidFile(GetArg("-pid", "ecoind.pid"));
  983. if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
  984. return pathPidFile;
  985. }
  986. #ifndef WIN32
  987. void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
  988. {
  989. FILE* file = fopen(path.string().c_str(), "w");
  990. if (file)
  991. {
  992. fprintf(file, "%d\n", pid);
  993. fclose(file);
  994. }
  995. }
  996. #endif
  997. bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
  998. {
  999. #ifdef WIN32
  1000. return MoveFileExA(src.string().c_str(), dest.string().c_str(),
  1001. MOVEFILE_REPLACE_EXISTING);
  1002. #else
  1003. int rc = std::rename(src.string().c_str(), dest.string().c_str());
  1004. return (rc == 0);
  1005. #endif /* WIN32 */
  1006. }
  1007. void FileCommit(FILE *fileout)
  1008. {
  1009. fflush(fileout); // harmless if redundantly called
  1010. #ifdef WIN32
  1011. _commit(_fileno(fileout));
  1012. #else
  1013. fsync(fileno(fileout));
  1014. #endif
  1015. }
  1016. int GetFilesize(FILE* file)
  1017. {
  1018. int nSavePos = ftell(file);
  1019. int nFilesize = -1;
  1020. if (fseek(file, 0, SEEK_END) == 0)
  1021. nFilesize = ftell(file);
  1022. fseek(file, nSavePos, SEEK_SET);
  1023. return nFilesize;
  1024. }
  1025. void ShrinkDebugFile()
  1026. {
  1027. // Scroll debug.log if it's getting too big
  1028. boost::filesystem::path pathLog = GetDataDir() / "debug.log";
  1029. FILE* file = fopen(pathLog.string().c_str(), "r");
  1030. if (file && GetFilesize(file) > 10 * 1000000)
  1031. {
  1032. // Restart the file with some of the end
  1033. char pch[200000];
  1034. fseek(file, -sizeof(pch), SEEK_END);
  1035. int nBytes = fread(pch, 1, sizeof(pch), file);
  1036. fclose(file);
  1037. file = fopen(pathLog.string().c_str(), "w");
  1038. if (file)
  1039. {
  1040. fwrite(pch, 1, nBytes, file);
  1041. fclose(file);
  1042. }
  1043. }
  1044. }
  1045. static int64 nMockTime = 0; // For unit testing
  1046. int64 GetTime()
  1047. {
  1048. if (nMockTime) return nMockTime;
  1049. return time(NULL);
  1050. }
  1051. void SetMockTime(int64 nMockTimeIn)
  1052. {
  1053. nMockTime = nMockTimeIn;
  1054. }
  1055. static int64 nTimeOffset = 0;
  1056. int64 GetTimeOffset()
  1057. {
  1058. return nTimeOffset;
  1059. }
  1060. int64 GetAdjustedTime()
  1061. {
  1062. return GetTime() + GetTimeOffset();
  1063. }
  1064. void AddTimeData(const CNetAddr& ip, int64 nTime)
  1065. {
  1066. int64 nOffsetSample = nTime - GetTime();
  1067. // Ignore duplicates
  1068. static set<CNetAddr> setKnown;
  1069. if (!setKnown.insert(ip).second)
  1070. return;
  1071. // Add data
  1072. vTimeOffsets.input(nOffsetSample);
  1073. printf("Added time data, samples %d, offset %+" PRI64d" (%+" PRI64d" minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
  1074. if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
  1075. {
  1076. int64 nMedian = vTimeOffsets.median();
  1077. std::vector<int64> vSorted = vTimeOffsets.sorted();
  1078. // Only let other nodes change our time by so much
  1079. if (abs64(nMedian) < 70 * 60)
  1080. {
  1081. nTimeOffset = nMedian;
  1082. }
  1083. else
  1084. {
  1085. nTimeOffset = 0;
  1086. static bool fDone;
  1087. if (!fDone)
  1088. {
  1089. // If nobody has a time different than ours but within 5 minutes of ours, give a warning
  1090. bool fMatch = false;
  1091. BOOST_FOREACH(int64 nOffset, vSorted)
  1092. if (nOffset != 0 && abs64(nOffset) < 5 * 60)
  1093. fMatch = true;
  1094. if (!fMatch)
  1095. {
  1096. fDone = true;
  1097. string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Ecoin will not work properly.");
  1098. strMiscWarning = strMessage;
  1099. printf("*** %s\n", strMessage.c_str());
  1100. uiInterface.ThreadSafeMessageBox(strMessage+" ", string("Ecoin"), CClientUIInterface::OK | CClientUIInterface::ICON_EXCLAMATION);
  1101. }
  1102. }
  1103. }
  1104. if (fDebug) {
  1105. BOOST_FOREACH(int64 n, vSorted)
  1106. printf("%+" PRI64d" ", n);
  1107. printf("| ");
  1108. }
  1109. printf("nTimeOffset = %+" PRI64d" (%+" PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60);
  1110. }
  1111. }
  1112. string FormatVersion(int nVersion)
  1113. {
  1114. if (nVersion%100 == 0)
  1115. return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
  1116. else
  1117. return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
  1118. }
  1119. string FormatFullVersion()
  1120. {
  1121. return CLIENT_BUILD;
  1122. }
  1123. // Format the subversion field according to BIP 14 spec (https://en.ecoin.it/wiki/BIP_0014)
  1124. std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments)
  1125. {
  1126. std::ostringstream ss;
  1127. ss << "/";
  1128. ss << name << ":" << FormatVersion(nClientVersion);
  1129. if (!comments.empty())
  1130. ss << "(" << boost::algorithm::join(comments, "; ") << ")";
  1131. ss << "/";
  1132. return ss.str();
  1133. }
  1134. #ifdef WIN32
  1135. boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
  1136. {
  1137. namespace fs = boost::filesystem;
  1138. char pszPath[MAX_PATH] = "";
  1139. if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
  1140. {
  1141. return fs::path(pszPath);
  1142. }
  1143. printf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
  1144. return fs::path("");
  1145. }
  1146. #endif
  1147. void runCommand(std::string strCommand)
  1148. {
  1149. int nErr = ::system(strCommand.c_str());
  1150. if (nErr)
  1151. printf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr);
  1152. }
  1153. void RenameThread(const char* name)
  1154. {
  1155. #if defined(PR_SET_NAME)
  1156. // Only the first 15 characters are used (16 - NUL terminator)
  1157. ::prctl(PR_SET_NAME, name, 0, 0, 0);
  1158. #elif 0 && (defined(__FreeBSD__) || defined(__OpenBSD__))
  1159. pthread_set_name_np(pthread_self(), name);
  1160. #else
  1161. // Prevent warnings for unused parameters...
  1162. (void)name;
  1163. #endif
  1164. }
  1165. bool NewThread(void(*pfn)(void*), void* parg)
  1166. {
  1167. try
  1168. {
  1169. boost::thread(pfn, parg); // thread detaches when out of scope
  1170. } catch(boost::thread_resource_error &e) {
  1171. printf("Error creating thread: %s\n", e.what());
  1172. return false;
  1173. }
  1174. return true;
  1175. }