]>
Commit | Line | Data |
---|---|---|
2097c09a WL |
1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | // Distributed under the MIT/X11 software license, see the accompanying | |
3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. | |
69d605f4 | 4 | #include "headers.h" |
0eeb4f5d WL |
5 | #include "strlcpy.h" |
6 | #include <boost/program_options/detail/config_file.hpp> | |
7 | #include <boost/program_options/parsers.hpp> | |
8 | #include <boost/filesystem/fstream.hpp> | |
9 | #include <boost/interprocess/sync/interprocess_mutex.hpp> | |
10 | #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp> | |
11 | #include <boost/foreach.hpp> | |
2097c09a WL |
12 | |
13 | using namespace std; | |
69d605f4 | 14 | using namespace boost; |
2097c09a WL |
15 | |
16 | map<string, string> mapArgs; | |
17 | map<string, vector<string> > mapMultiArgs; | |
18 | bool fDebug = false; | |
19 | bool fPrintToConsole = false; | |
20 | bool fPrintToDebugger = false; | |
21 | char pszSetDataDir[MAX_PATH] = ""; | |
22 | bool fRequestShutdown = false; | |
23 | bool fShutdown = false; | |
24 | bool fDaemon = false; | |
25 | bool fServer = false; | |
26 | bool fCommandLine = false; | |
27 | string strMiscWarning; | |
28 | bool fTestNet = false; | |
29 | bool fNoListen = false; | |
30 | bool fLogTimestamps = false; | |
31 | ||
32 | ||
33 | ||
34 | ||
35 | // Workaround for "multiple definition of `_tls_used'" | |
36 | // http://svn.boost.org/trac/boost/ticket/4258 | |
37 | extern "C" void tss_cleanup_implemented() { } | |
38 | ||
39 | ||
40 | ||
41 | ||
42 | ||
43 | // Init openssl library multithreading support | |
44 | static boost::interprocess::interprocess_mutex** ppmutexOpenSSL; | |
45 | void locking_callback(int mode, int i, const char* file, int line) | |
46 | { | |
47 | if (mode & CRYPTO_LOCK) | |
48 | ppmutexOpenSSL[i]->lock(); | |
49 | else | |
50 | ppmutexOpenSSL[i]->unlock(); | |
51 | } | |
52 | ||
53 | // Init | |
54 | class CInit | |
55 | { | |
56 | public: | |
57 | CInit() | |
58 | { | |
59 | // Init openssl library multithreading support | |
60 | ppmutexOpenSSL = (boost::interprocess::interprocess_mutex**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(boost::interprocess::interprocess_mutex*)); | |
61 | for (int i = 0; i < CRYPTO_num_locks(); i++) | |
62 | ppmutexOpenSSL[i] = new boost::interprocess::interprocess_mutex(); | |
63 | CRYPTO_set_locking_callback(locking_callback); | |
64 | ||
65 | #ifdef __WXMSW__ | |
66 | // Seed random number generator with screen scrape and other hardware sources | |
67 | RAND_screen(); | |
68 | #endif | |
69 | ||
70 | // Seed random number generator with performance counter | |
71 | RandAddSeed(); | |
72 | } | |
73 | ~CInit() | |
74 | { | |
75 | // Shutdown openssl library multithreading support | |
76 | CRYPTO_set_locking_callback(NULL); | |
77 | for (int i = 0; i < CRYPTO_num_locks(); i++) | |
78 | delete ppmutexOpenSSL[i]; | |
79 | OPENSSL_free(ppmutexOpenSSL); | |
80 | } | |
81 | } | |
82 | instance_of_cinit; | |
83 | ||
84 | ||
85 | ||
86 | ||
87 | ||
88 | ||
89 | ||
90 | ||
91 | void RandAddSeed() | |
92 | { | |
93 | // Seed with CPU performance counter | |
94 | int64 nCounter = GetPerformanceCounter(); | |
95 | RAND_add(&nCounter, sizeof(nCounter), 1.5); | |
96 | memset(&nCounter, 0, sizeof(nCounter)); | |
97 | } | |
98 | ||
99 | void RandAddSeedPerfmon() | |
100 | { | |
101 | RandAddSeed(); | |
102 | ||
103 | // This can take up to 2 seconds, so only do it every 10 minutes | |
104 | static int64 nLastPerfmon; | |
105 | if (GetTime() < nLastPerfmon + 10 * 60) | |
106 | return; | |
107 | nLastPerfmon = GetTime(); | |
108 | ||
109 | #ifdef __WXMSW__ | |
110 | // Don't need this on Linux, OpenSSL automatically uses /dev/urandom | |
111 | // Seed with the entire set of perfmon data | |
112 | unsigned char pdata[250000]; | |
113 | memset(pdata, 0, sizeof(pdata)); | |
114 | unsigned long nSize = sizeof(pdata); | |
115 | long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize); | |
116 | RegCloseKey(HKEY_PERFORMANCE_DATA); | |
117 | if (ret == ERROR_SUCCESS) | |
118 | { | |
119 | RAND_add(pdata, nSize, nSize/100.0); | |
120 | memset(pdata, 0, nSize); | |
121 | printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize); | |
122 | } | |
123 | #endif | |
124 | } | |
125 | ||
126 | uint64 GetRand(uint64 nMax) | |
127 | { | |
128 | if (nMax == 0) | |
129 | return 0; | |
130 | ||
131 | // The range of the random source must be a multiple of the modulus | |
132 | // to give every possible output value an equal possibility | |
133 | uint64 nRange = (UINT64_MAX / nMax) * nMax; | |
134 | uint64 nRand = 0; | |
135 | do | |
136 | RAND_bytes((unsigned char*)&nRand, sizeof(nRand)); | |
137 | while (nRand >= nRange); | |
138 | return (nRand % nMax); | |
139 | } | |
140 | ||
141 | int GetRandInt(int nMax) | |
142 | { | |
143 | return GetRand(nMax); | |
144 | } | |
145 | ||
146 | ||
147 | ||
148 | ||
149 | ||
150 | ||
151 | ||
152 | ||
153 | ||
154 | ||
155 | ||
156 | inline int OutputDebugStringF(const char* pszFormat, ...) | |
157 | { | |
158 | int ret = 0; | |
159 | if (fPrintToConsole) | |
160 | { | |
161 | // print to console | |
162 | va_list arg_ptr; | |
163 | va_start(arg_ptr, pszFormat); | |
164 | ret = vprintf(pszFormat, arg_ptr); | |
165 | va_end(arg_ptr); | |
166 | } | |
167 | else | |
168 | { | |
169 | // print to debug.log | |
170 | static FILE* fileout = NULL; | |
171 | ||
172 | if (!fileout) | |
173 | { | |
174 | char pszFile[MAX_PATH+100]; | |
175 | GetDataDir(pszFile); | |
176 | strlcat(pszFile, "/debug.log", sizeof(pszFile)); | |
177 | fileout = fopen(pszFile, "a"); | |
178 | if (fileout) setbuf(fileout, NULL); // unbuffered | |
179 | } | |
180 | if (fileout) | |
181 | { | |
182 | static bool fStartedNewLine = true; | |
183 | ||
184 | // Debug print useful for profiling | |
185 | if (fLogTimestamps && fStartedNewLine) | |
186 | fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str()); | |
187 | if (pszFormat[strlen(pszFormat) - 1] == '\n') | |
188 | fStartedNewLine = true; | |
189 | else | |
190 | fStartedNewLine = false; | |
191 | ||
192 | va_list arg_ptr; | |
193 | va_start(arg_ptr, pszFormat); | |
194 | ret = vfprintf(fileout, pszFormat, arg_ptr); | |
195 | va_end(arg_ptr); | |
196 | } | |
197 | } | |
198 | ||
199 | #ifdef __WXMSW__ | |
200 | if (fPrintToDebugger) | |
201 | { | |
202 | static CCriticalSection cs_OutputDebugStringF; | |
203 | ||
204 | // accumulate a line at a time | |
205 | CRITICAL_BLOCK(cs_OutputDebugStringF) | |
206 | { | |
207 | static char pszBuffer[50000]; | |
208 | static char* pend; | |
209 | if (pend == NULL) | |
210 | pend = pszBuffer; | |
211 | va_list arg_ptr; | |
212 | va_start(arg_ptr, pszFormat); | |
213 | int limit = END(pszBuffer) - pend - 2; | |
214 | int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr); | |
215 | va_end(arg_ptr); | |
216 | if (ret < 0 || ret >= limit) | |
217 | { | |
218 | pend = END(pszBuffer) - 2; | |
219 | *pend++ = '\n'; | |
220 | } | |
221 | else | |
222 | pend += ret; | |
223 | *pend = '\0'; | |
224 | char* p1 = pszBuffer; | |
225 | char* p2; | |
226 | while (p2 = strchr(p1, '\n')) | |
227 | { | |
228 | p2++; | |
229 | char c = *p2; | |
230 | *p2 = '\0'; | |
231 | OutputDebugStringA(p1); | |
232 | *p2 = c; | |
233 | p1 = p2; | |
234 | } | |
235 | if (p1 != pszBuffer) | |
236 | memmove(pszBuffer, p1, pend - p1 + 1); | |
237 | pend -= (p1 - pszBuffer); | |
238 | } | |
239 | } | |
240 | #endif | |
241 | return ret; | |
242 | } | |
243 | ||
244 | ||
245 | // Safer snprintf | |
246 | // - prints up to limit-1 characters | |
247 | // - output string is always null terminated even if limit reached | |
248 | // - return value is the number of characters actually printed | |
249 | int my_snprintf(char* buffer, size_t limit, const char* format, ...) | |
250 | { | |
251 | if (limit == 0) | |
252 | return 0; | |
253 | va_list arg_ptr; | |
254 | va_start(arg_ptr, format); | |
255 | int ret = _vsnprintf(buffer, limit, format, arg_ptr); | |
256 | va_end(arg_ptr); | |
257 | if (ret < 0 || ret >= limit) | |
258 | { | |
259 | ret = limit - 1; | |
260 | buffer[limit-1] = 0; | |
261 | } | |
262 | return ret; | |
263 | } | |
264 | ||
39cf857d | 265 | string strprintf(const std::string &format, ...) |
2097c09a WL |
266 | { |
267 | char buffer[50000]; | |
268 | char* p = buffer; | |
269 | int limit = sizeof(buffer); | |
270 | int ret; | |
271 | loop | |
272 | { | |
273 | va_list arg_ptr; | |
274 | va_start(arg_ptr, format); | |
39cf857d | 275 | ret = _vsnprintf(p, limit, format.c_str(), arg_ptr); |
2097c09a WL |
276 | va_end(arg_ptr); |
277 | if (ret >= 0 && ret < limit) | |
278 | break; | |
279 | if (p != buffer) | |
822f2e3d | 280 | delete[] p; |
2097c09a WL |
281 | limit *= 2; |
282 | p = new char[limit]; | |
283 | if (p == NULL) | |
284 | throw std::bad_alloc(); | |
285 | } | |
286 | string str(p, p+ret); | |
287 | if (p != buffer) | |
822f2e3d | 288 | delete[] p; |
2097c09a WL |
289 | return str; |
290 | } | |
291 | ||
39cf857d | 292 | bool error(const std::string &format, ...) |
2097c09a WL |
293 | { |
294 | char buffer[50000]; | |
295 | int limit = sizeof(buffer); | |
296 | va_list arg_ptr; | |
297 | va_start(arg_ptr, format); | |
39cf857d | 298 | int ret = _vsnprintf(buffer, limit, format.c_str(), arg_ptr); |
2097c09a WL |
299 | va_end(arg_ptr); |
300 | if (ret < 0 || ret >= limit) | |
301 | { | |
302 | ret = limit - 1; | |
303 | buffer[limit-1] = 0; | |
304 | } | |
305 | printf("ERROR: %s\n", buffer); | |
306 | return false; | |
307 | } | |
308 | ||
309 | ||
310 | void ParseString(const string& str, char c, vector<string>& v) | |
311 | { | |
312 | if (str.empty()) | |
313 | return; | |
314 | string::size_type i1 = 0; | |
315 | string::size_type i2; | |
316 | loop | |
317 | { | |
318 | i2 = str.find(c, i1); | |
319 | if (i2 == str.npos) | |
320 | { | |
321 | v.push_back(str.substr(i1)); | |
322 | return; | |
323 | } | |
324 | v.push_back(str.substr(i1, i2-i1)); | |
325 | i1 = i2+1; | |
326 | } | |
327 | } | |
328 | ||
329 | ||
330 | string FormatMoney(int64 n, bool fPlus) | |
331 | { | |
332 | // Note: not using straight sprintf here because we do NOT want | |
333 | // localized number formatting. | |
334 | int64 n_abs = (n > 0 ? n : -n); | |
335 | int64 quotient = n_abs/COIN; | |
336 | int64 remainder = n_abs%COIN; | |
337 | string str = strprintf("%"PRI64d".%08"PRI64d, quotient, remainder); | |
338 | ||
339 | // Right-trim excess 0's before the decimal point: | |
340 | int nTrim = 0; | |
341 | for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i) | |
342 | ++nTrim; | |
343 | if (nTrim) | |
344 | str.erase(str.size()-nTrim, nTrim); | |
345 | ||
346 | // Insert thousands-separators: | |
347 | size_t point = str.find("."); | |
348 | for (int i = (str.size()-point)+3; i < str.size(); i += 4) | |
349 | if (isdigit(str[str.size() - i - 1])) | |
350 | str.insert(str.size() - i, 1, ','); | |
351 | if (n < 0) | |
352 | str.insert((unsigned int)0, 1, '-'); | |
353 | else if (fPlus && n > 0) | |
354 | str.insert((unsigned int)0, 1, '+'); | |
355 | return str; | |
356 | } | |
357 | ||
358 | ||
359 | bool ParseMoney(const string& str, int64& nRet) | |
360 | { | |
361 | return ParseMoney(str.c_str(), nRet); | |
362 | } | |
363 | ||
364 | bool ParseMoney(const char* pszIn, int64& nRet) | |
365 | { | |
366 | string strWhole; | |
367 | int64 nUnits = 0; | |
368 | const char* p = pszIn; | |
369 | while (isspace(*p)) | |
370 | p++; | |
371 | for (; *p; p++) | |
372 | { | |
373 | if (*p == ',' && p > pszIn && isdigit(p[-1]) && isdigit(p[1]) && isdigit(p[2]) && isdigit(p[3]) && !isdigit(p[4])) | |
374 | continue; | |
375 | if (*p == '.') | |
376 | { | |
377 | p++; | |
378 | int64 nMult = CENT*10; | |
379 | while (isdigit(*p) && (nMult > 0)) | |
380 | { | |
381 | nUnits += nMult * (*p++ - '0'); | |
382 | nMult /= 10; | |
383 | } | |
384 | break; | |
385 | } | |
386 | if (isspace(*p)) | |
387 | break; | |
388 | if (!isdigit(*p)) | |
389 | return false; | |
390 | strWhole.insert(strWhole.end(), *p); | |
391 | } | |
392 | for (; *p; p++) | |
393 | if (!isspace(*p)) | |
394 | return false; | |
395 | if (strWhole.size() > 14) | |
396 | return false; | |
397 | if (nUnits < 0 || nUnits > COIN) | |
398 | return false; | |
399 | int64 nWhole = atoi64(strWhole); | |
400 | int64 nValue = nWhole*COIN + nUnits; | |
401 | ||
402 | nRet = nValue; | |
403 | return true; | |
404 | } | |
405 | ||
406 | ||
407 | vector<unsigned char> ParseHex(const char* psz) | |
408 | { | |
409 | static char phexdigit[256] = | |
410 | { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
411 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
412 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
413 | 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1, | |
414 | -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
415 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
416 | -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1 | |
417 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
418 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
419 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
420 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
421 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
422 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
423 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
424 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
425 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, }; | |
426 | ||
427 | // convert hex dump to vector | |
428 | vector<unsigned char> vch; | |
429 | loop | |
430 | { | |
431 | while (isspace(*psz)) | |
432 | psz++; | |
433 | char c = phexdigit[(unsigned char)*psz++]; | |
434 | if (c == (char)-1) | |
435 | break; | |
436 | unsigned char n = (c << 4); | |
437 | c = phexdigit[(unsigned char)*psz++]; | |
438 | if (c == (char)-1) | |
439 | break; | |
440 | n |= c; | |
441 | vch.push_back(n); | |
442 | } | |
443 | return vch; | |
444 | } | |
445 | ||
446 | vector<unsigned char> ParseHex(const string& str) | |
447 | { | |
448 | return ParseHex(str.c_str()); | |
449 | } | |
450 | ||
451 | ||
452 | void ParseParameters(int argc, char* argv[]) | |
453 | { | |
454 | mapArgs.clear(); | |
455 | mapMultiArgs.clear(); | |
456 | for (int i = 1; i < argc; i++) | |
457 | { | |
458 | char psz[10000]; | |
459 | strlcpy(psz, argv[i], sizeof(psz)); | |
460 | char* pszValue = (char*)""; | |
461 | if (strchr(psz, '=')) | |
462 | { | |
463 | pszValue = strchr(psz, '='); | |
464 | *pszValue++ = '\0'; | |
465 | } | |
466 | #ifdef __WXMSW__ | |
467 | _strlwr(psz); | |
468 | if (psz[0] == '/') | |
469 | psz[0] = '-'; | |
470 | #endif | |
471 | if (psz[0] != '-') | |
472 | break; | |
473 | mapArgs[psz] = pszValue; | |
474 | mapMultiArgs[psz].push_back(pszValue); | |
475 | } | |
476 | } | |
477 | ||
478 | ||
479 | const char* wxGetTranslation(const char* pszEnglish) | |
480 | { | |
481 | #ifdef GUI | |
482 | // Wrapper of wxGetTranslation returning the same const char* type as was passed in | |
483 | static CCriticalSection cs; | |
484 | CRITICAL_BLOCK(cs) | |
485 | { | |
486 | // Look in cache | |
487 | static map<string, char*> mapCache; | |
488 | map<string, char*>::iterator mi = mapCache.find(pszEnglish); | |
489 | if (mi != mapCache.end()) | |
490 | return (*mi).second; | |
491 | ||
492 | // wxWidgets translation | |
493 | wxString strTranslated = wxGetTranslation(wxString(pszEnglish, wxConvUTF8)); | |
494 | ||
495 | // We don't cache unknown strings because caller might be passing in a | |
496 | // dynamic string and we would keep allocating memory for each variation. | |
497 | if (strcmp(pszEnglish, strTranslated.utf8_str()) == 0) | |
498 | return pszEnglish; | |
499 | ||
500 | // Add to cache, memory doesn't need to be freed. We only cache because | |
501 | // we must pass back a pointer to permanently allocated memory. | |
502 | char* pszCached = new char[strlen(strTranslated.utf8_str())+1]; | |
503 | strcpy(pszCached, strTranslated.utf8_str()); | |
504 | mapCache[pszEnglish] = pszCached; | |
505 | return pszCached; | |
506 | } | |
507 | return NULL; | |
508 | #else | |
509 | return pszEnglish; | |
510 | #endif | |
511 | } | |
512 | ||
513 | ||
514 | bool WildcardMatch(const char* psz, const char* mask) | |
515 | { | |
516 | loop | |
517 | { | |
518 | switch (*mask) | |
519 | { | |
520 | case '\0': | |
521 | return (*psz == '\0'); | |
522 | case '*': | |
523 | return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask)); | |
524 | case '?': | |
525 | if (*psz == '\0') | |
526 | return false; | |
527 | break; | |
528 | default: | |
529 | if (*psz != *mask) | |
530 | return false; | |
531 | break; | |
532 | } | |
533 | psz++; | |
534 | mask++; | |
535 | } | |
536 | } | |
537 | ||
538 | bool WildcardMatch(const string& str, const string& mask) | |
539 | { | |
540 | return WildcardMatch(str.c_str(), mask.c_str()); | |
541 | } | |
542 | ||
543 | ||
544 | ||
545 | ||
546 | ||
547 | ||
548 | ||
549 | ||
550 | void FormatException(char* pszMessage, std::exception* pex, const char* pszThread) | |
551 | { | |
552 | #ifdef __WXMSW__ | |
553 | char pszModule[MAX_PATH]; | |
554 | pszModule[0] = '\0'; | |
555 | GetModuleFileNameA(NULL, pszModule, sizeof(pszModule)); | |
556 | #else | |
557 | const char* pszModule = "bitcoin"; | |
558 | #endif | |
559 | if (pex) | |
560 | snprintf(pszMessage, 1000, | |
561 | "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread); | |
562 | else | |
563 | snprintf(pszMessage, 1000, | |
564 | "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread); | |
565 | } | |
566 | ||
567 | void LogException(std::exception* pex, const char* pszThread) | |
568 | { | |
569 | char pszMessage[10000]; | |
570 | FormatException(pszMessage, pex, pszThread); | |
571 | printf("\n%s", pszMessage); | |
572 | } | |
573 | ||
574 | void PrintException(std::exception* pex, const char* pszThread) | |
575 | { | |
576 | char pszMessage[10000]; | |
577 | FormatException(pszMessage, pex, pszThread); | |
578 | printf("\n\n************************\n%s\n", pszMessage); | |
579 | fprintf(stderr, "\n\n************************\n%s\n", pszMessage); | |
580 | strMiscWarning = pszMessage; | |
581 | #ifdef GUI | |
582 | if (wxTheApp && !fDaemon) | |
583 | MyMessageBox(pszMessage, "Bitcoin", wxOK | wxICON_ERROR); | |
584 | #endif | |
585 | throw; | |
586 | } | |
587 | ||
588 | void ThreadOneMessageBox(string strMessage) | |
589 | { | |
590 | // Skip message boxes if one is already open | |
591 | static bool fMessageBoxOpen; | |
592 | if (fMessageBoxOpen) | |
593 | return; | |
594 | fMessageBoxOpen = true; | |
595 | ThreadSafeMessageBox(strMessage, "Bitcoin", wxOK | wxICON_EXCLAMATION); | |
596 | fMessageBoxOpen = false; | |
597 | } | |
598 | ||
599 | void PrintExceptionContinue(std::exception* pex, const char* pszThread) | |
600 | { | |
601 | char pszMessage[10000]; | |
602 | FormatException(pszMessage, pex, pszThread); | |
603 | printf("\n\n************************\n%s\n", pszMessage); | |
604 | fprintf(stderr, "\n\n************************\n%s\n", pszMessage); | |
605 | strMiscWarning = pszMessage; | |
606 | #ifdef GUI | |
607 | if (wxTheApp && !fDaemon) | |
608 | boost::thread(boost::bind(ThreadOneMessageBox, string(pszMessage))); | |
609 | #endif | |
610 | } | |
611 | ||
612 | ||
613 | ||
614 | ||
615 | ||
616 | ||
617 | ||
618 | ||
619 | #ifdef __WXMSW__ | |
620 | typedef WINSHELLAPI BOOL (WINAPI *PSHGETSPECIALFOLDERPATHA)(HWND hwndOwner, LPSTR lpszPath, int nFolder, BOOL fCreate); | |
621 | ||
622 | string MyGetSpecialFolderPath(int nFolder, bool fCreate) | |
623 | { | |
624 | char pszPath[MAX_PATH+100] = ""; | |
625 | ||
626 | // SHGetSpecialFolderPath isn't always available on old Windows versions | |
627 | HMODULE hShell32 = LoadLibraryA("shell32.dll"); | |
628 | if (hShell32) | |
629 | { | |
630 | PSHGETSPECIALFOLDERPATHA pSHGetSpecialFolderPath = | |
631 | (PSHGETSPECIALFOLDERPATHA)GetProcAddress(hShell32, "SHGetSpecialFolderPathA"); | |
632 | if (pSHGetSpecialFolderPath) | |
633 | (*pSHGetSpecialFolderPath)(NULL, pszPath, nFolder, fCreate); | |
634 | FreeModule(hShell32); | |
635 | } | |
636 | ||
637 | // Backup option | |
638 | if (pszPath[0] == '\0') | |
639 | { | |
640 | if (nFolder == CSIDL_STARTUP) | |
641 | { | |
642 | strcpy(pszPath, getenv("USERPROFILE")); | |
643 | strcat(pszPath, "\\Start Menu\\Programs\\Startup"); | |
644 | } | |
645 | else if (nFolder == CSIDL_APPDATA) | |
646 | { | |
647 | strcpy(pszPath, getenv("APPDATA")); | |
648 | } | |
649 | } | |
650 | ||
651 | return pszPath; | |
652 | } | |
653 | #endif | |
654 | ||
655 | string GetDefaultDataDir() | |
656 | { | |
657 | // Windows: C:\Documents and Settings\username\Application Data\Bitcoin | |
658 | // Mac: ~/Library/Application Support/Bitcoin | |
659 | // Unix: ~/.bitcoin | |
660 | #ifdef __WXMSW__ | |
661 | // Windows | |
662 | return MyGetSpecialFolderPath(CSIDL_APPDATA, true) + "\\Bitcoin"; | |
663 | #else | |
664 | char* pszHome = getenv("HOME"); | |
665 | if (pszHome == NULL || strlen(pszHome) == 0) | |
666 | pszHome = (char*)"/"; | |
667 | string strHome = pszHome; | |
668 | if (strHome[strHome.size()-1] != '/') | |
669 | strHome += '/'; | |
670 | #ifdef __WXMAC_OSX__ | |
671 | // Mac | |
672 | strHome += "Library/Application Support/"; | |
673 | filesystem::create_directory(strHome.c_str()); | |
674 | return strHome + "Bitcoin"; | |
675 | #else | |
676 | // Unix | |
677 | return strHome + ".bitcoin"; | |
678 | #endif | |
679 | #endif | |
680 | } | |
681 | ||
682 | void GetDataDir(char* pszDir) | |
683 | { | |
684 | // pszDir must be at least MAX_PATH length. | |
685 | int nVariation; | |
686 | if (pszSetDataDir[0] != 0) | |
687 | { | |
688 | strlcpy(pszDir, pszSetDataDir, MAX_PATH); | |
689 | nVariation = 0; | |
690 | } | |
691 | else | |
692 | { | |
693 | // This can be called during exceptions by printf, so we cache the | |
694 | // value so we don't have to do memory allocations after that. | |
695 | static char pszCachedDir[MAX_PATH]; | |
696 | if (pszCachedDir[0] == 0) | |
697 | strlcpy(pszCachedDir, GetDefaultDataDir().c_str(), sizeof(pszCachedDir)); | |
698 | strlcpy(pszDir, pszCachedDir, MAX_PATH); | |
699 | nVariation = 1; | |
700 | } | |
701 | if (fTestNet) | |
702 | { | |
703 | char* p = pszDir + strlen(pszDir); | |
704 | if (p > pszDir && p[-1] != '/' && p[-1] != '\\') | |
705 | *p++ = '/'; | |
706 | strcpy(p, "testnet"); | |
707 | nVariation += 2; | |
708 | } | |
709 | static bool pfMkdir[4]; | |
710 | if (!pfMkdir[nVariation]) | |
711 | { | |
712 | pfMkdir[nVariation] = true; | |
69d605f4 | 713 | boost::filesystem::create_directory(pszDir); |
2097c09a WL |
714 | } |
715 | } | |
716 | ||
717 | string GetDataDir() | |
718 | { | |
719 | char pszDir[MAX_PATH]; | |
720 | GetDataDir(pszDir); | |
721 | return pszDir; | |
722 | } | |
723 | ||
724 | string GetConfigFile() | |
725 | { | |
726 | namespace fs = boost::filesystem; | |
727 | fs::path pathConfig(GetArg("-conf", "bitcoin.conf")); | |
728 | if (!pathConfig.is_complete()) | |
729 | pathConfig = fs::path(GetDataDir()) / pathConfig; | |
730 | return pathConfig.string(); | |
731 | } | |
732 | ||
733 | void ReadConfigFile(map<string, string>& mapSettingsRet, | |
734 | map<string, vector<string> >& mapMultiSettingsRet) | |
735 | { | |
736 | namespace fs = boost::filesystem; | |
737 | namespace pod = boost::program_options::detail; | |
738 | ||
739 | fs::ifstream streamConfig(GetConfigFile()); | |
740 | if (!streamConfig.good()) | |
741 | return; | |
742 | ||
743 | set<string> setOptions; | |
744 | setOptions.insert("*"); | |
745 | ||
746 | for (pod::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it) | |
747 | { | |
748 | // Don't overwrite existing settings so command line settings override bitcoin.conf | |
749 | string strKey = string("-") + it->string_key; | |
750 | if (mapSettingsRet.count(strKey) == 0) | |
751 | mapSettingsRet[strKey] = it->value[0]; | |
752 | mapMultiSettingsRet[strKey].push_back(it->value[0]); | |
753 | } | |
754 | } | |
755 | ||
756 | string GetPidFile() | |
757 | { | |
758 | namespace fs = boost::filesystem; | |
759 | fs::path pathConfig(GetArg("-pid", "bitcoind.pid")); | |
760 | if (!pathConfig.is_complete()) | |
761 | pathConfig = fs::path(GetDataDir()) / pathConfig; | |
762 | return pathConfig.string(); | |
763 | } | |
764 | ||
765 | void CreatePidFile(string pidFile, pid_t pid) | |
766 | { | |
767 | FILE* file; | |
768 | if (file = fopen(pidFile.c_str(), "w")) | |
769 | { | |
770 | fprintf(file, "%d\n", pid); | |
771 | fclose(file); | |
772 | } | |
773 | } | |
774 | ||
775 | int GetFilesize(FILE* file) | |
776 | { | |
777 | int nSavePos = ftell(file); | |
778 | int nFilesize = -1; | |
779 | if (fseek(file, 0, SEEK_END) == 0) | |
780 | nFilesize = ftell(file); | |
781 | fseek(file, nSavePos, SEEK_SET); | |
782 | return nFilesize; | |
783 | } | |
784 | ||
785 | void ShrinkDebugFile() | |
786 | { | |
787 | // Scroll debug.log if it's getting too big | |
788 | string strFile = GetDataDir() + "/debug.log"; | |
789 | FILE* file = fopen(strFile.c_str(), "r"); | |
790 | if (file && GetFilesize(file) > 10 * 1000000) | |
791 | { | |
792 | // Restart the file with some of the end | |
793 | char pch[200000]; | |
794 | fseek(file, -sizeof(pch), SEEK_END); | |
795 | int nBytes = fread(pch, 1, sizeof(pch), file); | |
796 | fclose(file); | |
797 | if (file = fopen(strFile.c_str(), "w")) | |
798 | { | |
799 | fwrite(pch, 1, nBytes, file); | |
800 | fclose(file); | |
801 | } | |
802 | } | |
803 | } | |
804 | ||
805 | ||
806 | ||
807 | ||
808 | ||
809 | ||
810 | ||
811 | ||
812 | // | |
813 | // "Never go to sea with two chronometers; take one or three." | |
814 | // Our three time sources are: | |
815 | // - System clock | |
816 | // - Median of other nodes's clocks | |
817 | // - The user (asking the user to fix the system clock if the first two disagree) | |
818 | // | |
819 | int64 GetTime() | |
820 | { | |
821 | return time(NULL); | |
822 | } | |
823 | ||
824 | static int64 nTimeOffset = 0; | |
825 | ||
826 | int64 GetAdjustedTime() | |
827 | { | |
828 | return GetTime() + nTimeOffset; | |
829 | } | |
830 | ||
831 | void AddTimeData(unsigned int ip, int64 nTime) | |
832 | { | |
833 | int64 nOffsetSample = nTime - GetTime(); | |
834 | ||
835 | // Ignore duplicates | |
836 | static set<unsigned int> setKnown; | |
837 | if (!setKnown.insert(ip).second) | |
838 | return; | |
839 | ||
840 | // Add data | |
841 | static vector<int64> vTimeOffsets; | |
842 | if (vTimeOffsets.empty()) | |
843 | vTimeOffsets.push_back(0); | |
844 | vTimeOffsets.push_back(nOffsetSample); | |
845 | printf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), vTimeOffsets.back(), vTimeOffsets.back()/60); | |
846 | if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1) | |
847 | { | |
848 | sort(vTimeOffsets.begin(), vTimeOffsets.end()); | |
849 | int64 nMedian = vTimeOffsets[vTimeOffsets.size()/2]; | |
850 | // Only let other nodes change our time by so much | |
851 | if (abs64(nMedian) < 70 * 60) | |
852 | { | |
853 | nTimeOffset = nMedian; | |
854 | } | |
855 | else | |
856 | { | |
857 | nTimeOffset = 0; | |
858 | ||
859 | static bool fDone; | |
860 | if (!fDone) | |
861 | { | |
862 | // If nobody has a time different than ours but within 5 minutes of ours, give a warning | |
863 | bool fMatch = false; | |
69d605f4 | 864 | BOOST_FOREACH(int64 nOffset, vTimeOffsets) |
2097c09a WL |
865 | if (nOffset != 0 && abs64(nOffset) < 5 * 60) |
866 | fMatch = true; | |
867 | ||
868 | if (!fMatch) | |
869 | { | |
870 | fDone = true; | |
871 | string strMessage = _("Warning: Please check that your computer's date and time are correct. If your clock is wrong Bitcoin will not work properly."); | |
872 | strMiscWarning = strMessage; | |
873 | printf("*** %s\n", strMessage.c_str()); | |
874 | boost::thread(boost::bind(ThreadSafeMessageBox, strMessage+" ", string("Bitcoin"), wxOK | wxICON_EXCLAMATION, (wxWindow*)NULL, -1, -1)); | |
875 | } | |
876 | } | |
877 | } | |
69d605f4 | 878 | BOOST_FOREACH(int64 n, vTimeOffsets) |
2097c09a WL |
879 | printf("%+"PRI64d" ", n); |
880 | printf("| nTimeOffset = %+"PRI64d" (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60); | |
881 | } | |
882 | } | |
883 | ||
884 | ||
885 | ||
886 | ||
887 | ||
888 | ||
889 | ||
890 | ||
891 | ||
892 | string FormatVersion(int nVersion) | |
893 | { | |
894 | if (nVersion%100 == 0) | |
895 | return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100); | |
896 | else | |
897 | return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100); | |
898 | } | |
899 | ||
900 | string FormatFullVersion() | |
901 | { | |
902 | string s = FormatVersion(VERSION) + pszSubVer; | |
0eeb4f5d WL |
903 | if (VERSION_IS_BETA) { |
904 | s += "-"; | |
905 | s += _("beta"); | |
906 | } | |
2097c09a WL |
907 | return s; |
908 | } | |
909 | ||
910 | ||
911 | ||
912 | ||
913 |