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