You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

net.cpp 56KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2011 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file license.txt or http://www.opensource.org/licenses/mit-license.php.
  5. #include "headers.h"
  6. #include "irc.h"
  7. #include "db.h"
  8. #include "net.h"
  9. #include "init.h"
  10. #include "strlcpy.h"
  11. #ifdef __WXMSW__
  12. #include <string.h>
  13. #endif
  14. #ifdef USE_UPNP
  15. #include <miniupnpc/miniwget.h>
  16. #include <miniupnpc/miniupnpc.h>
  17. #include <miniupnpc/upnpcommands.h>
  18. #include <miniupnpc/upnperrors.h>
  19. #endif
  20. using namespace std;
  21. using namespace boost;
  22. static const int MAX_OUTBOUND_CONNECTIONS = 8;
  23. void ThreadMessageHandler2(void* parg);
  24. void ThreadSocketHandler2(void* parg);
  25. void ThreadOpenConnections2(void* parg);
  26. #ifdef USE_UPNP
  27. void ThreadMapPort2(void* parg);
  28. #endif
  29. bool OpenNetworkConnection(const CAddress& addrConnect);
  30. //
  31. // Global state variables
  32. //
  33. bool fClient = false;
  34. bool fAllowDNS = false;
  35. uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
  36. CAddress addrLocalHost("0.0.0.0", 0, false, nLocalServices);
  37. CNode* pnodeLocalHost = NULL;
  38. uint64 nLocalHostNonce = 0;
  39. array<int, 10> vnThreadsRunning;
  40. SOCKET hListenSocket = INVALID_SOCKET;
  41. vector<CNode*> vNodes;
  42. CCriticalSection cs_vNodes;
  43. map<vector<unsigned char>, CAddress> mapAddresses;
  44. CCriticalSection cs_mapAddresses;
  45. map<CInv, CDataStream> mapRelay;
  46. deque<pair<int64, CInv> > vRelayExpiration;
  47. CCriticalSection cs_mapRelay;
  48. map<CInv, int64> mapAlreadyAskedFor;
  49. // Settings
  50. int fUseProxy = false;
  51. int nConnectTimeout = 5000;
  52. CAddress addrProxy("127.0.0.1",9050);
  53. unsigned short GetListenPort()
  54. {
  55. return (unsigned short)(GetArg("-port", GetDefaultPort()));
  56. }
  57. void CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd)
  58. {
  59. // Filter out duplicate requests
  60. if (pindexBegin == pindexLastGetBlocksBegin && hashEnd == hashLastGetBlocksEnd)
  61. return;
  62. pindexLastGetBlocksBegin = pindexBegin;
  63. hashLastGetBlocksEnd = hashEnd;
  64. PushMessage("getblocks", CBlockLocator(pindexBegin), hashEnd);
  65. }
  66. bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet, int nTimeout)
  67. {
  68. hSocketRet = INVALID_SOCKET;
  69. SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  70. if (hSocket == INVALID_SOCKET)
  71. return false;
  72. #ifdef SO_NOSIGPIPE
  73. int set = 1;
  74. setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
  75. #endif
  76. bool fProxy = (fUseProxy && addrConnect.IsRoutable());
  77. struct sockaddr_in sockaddr = (fProxy ? addrProxy.GetSockAddr() : addrConnect.GetSockAddr());
  78. #ifdef __WXMSW__
  79. u_long fNonblock = 1;
  80. if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
  81. #else
  82. int fFlags = fcntl(hSocket, F_GETFL, 0);
  83. if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1)
  84. #endif
  85. {
  86. closesocket(hSocket);
  87. return false;
  88. }
  89. if (connect(hSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
  90. {
  91. // WSAEINVAL is here because some legacy version of winsock uses it
  92. if (WSAGetLastError() == WSAEINPROGRESS || WSAGetLastError() == WSAEWOULDBLOCK || WSAGetLastError() == WSAEINVAL)
  93. {
  94. struct timeval timeout;
  95. timeout.tv_sec = nTimeout / 1000;
  96. timeout.tv_usec = (nTimeout % 1000) * 1000;
  97. fd_set fdset;
  98. FD_ZERO(&fdset);
  99. FD_SET(hSocket, &fdset);
  100. int nRet = select(hSocket + 1, NULL, &fdset, NULL, &timeout);
  101. if (nRet == 0)
  102. {
  103. printf("connection timeout\n");
  104. closesocket(hSocket);
  105. return false;
  106. }
  107. if (nRet == SOCKET_ERROR)
  108. {
  109. printf("select() for connection failed: %i\n",WSAGetLastError());
  110. closesocket(hSocket);
  111. return false;
  112. }
  113. socklen_t nRetSize = sizeof(nRet);
  114. #ifdef __WXMSW__
  115. if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char*)(&nRet), &nRetSize) == SOCKET_ERROR)
  116. #else
  117. if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR)
  118. #endif
  119. {
  120. printf("getsockopt() for connection failed: %i\n",WSAGetLastError());
  121. closesocket(hSocket);
  122. return false;
  123. }
  124. if (nRet != 0)
  125. {
  126. printf("connect() failed after select(): %s\n",strerror(nRet));
  127. closesocket(hSocket);
  128. return false;
  129. }
  130. }
  131. #ifdef __WXMSW__
  132. else if (WSAGetLastError() != WSAEISCONN)
  133. #else
  134. else
  135. #endif
  136. {
  137. printf("connect() failed: %i\n",WSAGetLastError());
  138. closesocket(hSocket);
  139. return false;
  140. }
  141. }
  142. /*
  143. this isn't even strictly necessary
  144. CNode::ConnectNode immediately turns the socket back to non-blocking
  145. but we'll turn it back to blocking just in case
  146. */
  147. #ifdef __WXMSW__
  148. fNonblock = 0;
  149. if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
  150. #else
  151. fFlags = fcntl(hSocket, F_GETFL, 0);
  152. if (fcntl(hSocket, F_SETFL, fFlags & !O_NONBLOCK) == SOCKET_ERROR)
  153. #endif
  154. {
  155. closesocket(hSocket);
  156. return false;
  157. }
  158. if (fProxy)
  159. {
  160. printf("proxy connecting %s\n", addrConnect.ToString().c_str());
  161. char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user";
  162. memcpy(pszSocks4IP + 2, &addrConnect.port, 2);
  163. memcpy(pszSocks4IP + 4, &addrConnect.ip, 4);
  164. char* pszSocks4 = pszSocks4IP;
  165. int nSize = sizeof(pszSocks4IP);
  166. int ret = send(hSocket, pszSocks4, nSize, MSG_NOSIGNAL);
  167. if (ret != nSize)
  168. {
  169. closesocket(hSocket);
  170. return error("Error sending to proxy");
  171. }
  172. char pchRet[8];
  173. if (recv(hSocket, pchRet, 8, 0) != 8)
  174. {
  175. closesocket(hSocket);
  176. return error("Error reading proxy response");
  177. }
  178. if (pchRet[1] != 0x5a)
  179. {
  180. closesocket(hSocket);
  181. if (pchRet[1] != 0x5b)
  182. printf("ERROR: Proxy returned error %d\n", pchRet[1]);
  183. return false;
  184. }
  185. printf("proxy connected %s\n", addrConnect.ToString().c_str());
  186. }
  187. hSocketRet = hSocket;
  188. return true;
  189. }
  190. // portDefault is in host order
  191. bool Lookup(const char *pszName, vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup, int portDefault, bool fAllowPort)
  192. {
  193. vaddr.clear();
  194. if (pszName[0] == 0)
  195. return false;
  196. int port = portDefault;
  197. char psz[256];
  198. char *pszHost = psz;
  199. strlcpy(psz, pszName, sizeof(psz));
  200. if (fAllowPort)
  201. {
  202. char* pszColon = strrchr(psz+1,':');
  203. char *pszPortEnd = NULL;
  204. int portParsed = pszColon ? strtoul(pszColon+1, &pszPortEnd, 10) : 0;
  205. if (pszColon && pszPortEnd && pszPortEnd[0] == 0)
  206. {
  207. if (psz[0] == '[' && pszColon[-1] == ']')
  208. {
  209. // Future: enable IPv6 colon-notation inside []
  210. pszHost = psz+1;
  211. pszColon[-1] = 0;
  212. }
  213. else
  214. pszColon[0] = 0;
  215. port = portParsed;
  216. if (port < 0 || port > USHRT_MAX)
  217. port = USHRT_MAX;
  218. }
  219. }
  220. unsigned int addrIP = inet_addr(pszHost);
  221. if (addrIP != INADDR_NONE)
  222. {
  223. // valid IP address passed
  224. vaddr.push_back(CAddress(addrIP, port, nServices));
  225. return true;
  226. }
  227. if (!fAllowLookup)
  228. return false;
  229. struct hostent* phostent = gethostbyname(pszHost);
  230. if (!phostent)
  231. return false;
  232. if (phostent->h_addrtype != AF_INET)
  233. return false;
  234. char** ppAddr = phostent->h_addr_list;
  235. while (*ppAddr != NULL && vaddr.size() != nMaxSolutions)
  236. {
  237. CAddress addr(((struct in_addr*)ppAddr[0])->s_addr, port, nServices);
  238. if (addr.IsValid())
  239. vaddr.push_back(addr);
  240. ppAddr++;
  241. }
  242. return (vaddr.size() > 0);
  243. }
  244. // portDefault is in host order
  245. bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup, int portDefault, bool fAllowPort)
  246. {
  247. vector<CAddress> vaddr;
  248. bool fRet = Lookup(pszName, vaddr, nServices, 1, fAllowLookup, portDefault, fAllowPort);
  249. if (fRet)
  250. addr = vaddr[0];
  251. return fRet;
  252. }
  253. bool GetMyExternalIP2(const CAddress& addrConnect, const char* pszGet, const char* pszKeyword, unsigned int& ipRet)
  254. {
  255. SOCKET hSocket;
  256. if (!ConnectSocket(addrConnect, hSocket))
  257. return error("GetMyExternalIP() : connection to %s failed", addrConnect.ToString().c_str());
  258. send(hSocket, pszGet, strlen(pszGet), MSG_NOSIGNAL);
  259. string strLine;
  260. while (RecvLine(hSocket, strLine))
  261. {
  262. if (strLine.empty()) // HTTP response is separated from headers by blank line
  263. {
  264. loop
  265. {
  266. if (!RecvLine(hSocket, strLine))
  267. {
  268. closesocket(hSocket);
  269. return false;
  270. }
  271. if (pszKeyword == NULL)
  272. break;
  273. if (strLine.find(pszKeyword) != -1)
  274. {
  275. strLine = strLine.substr(strLine.find(pszKeyword) + strlen(pszKeyword));
  276. break;
  277. }
  278. }
  279. closesocket(hSocket);
  280. if (strLine.find("<") != -1)
  281. strLine = strLine.substr(0, strLine.find("<"));
  282. strLine = strLine.substr(strspn(strLine.c_str(), " \t\n\r"));
  283. while (strLine.size() > 0 && isspace(strLine[strLine.size()-1]))
  284. strLine.resize(strLine.size()-1);
  285. CAddress addr(strLine,0,true);
  286. printf("GetMyExternalIP() received [%s] %s\n", strLine.c_str(), addr.ToString().c_str());
  287. if (addr.ip == 0 || addr.ip == INADDR_NONE || !addr.IsRoutable())
  288. return false;
  289. ipRet = addr.ip;
  290. return true;
  291. }
  292. }
  293. closesocket(hSocket);
  294. return error("GetMyExternalIP() : connection closed");
  295. }
  296. // We now get our external IP from the IRC server first and only use this as a backup
  297. bool GetMyExternalIP(unsigned int& ipRet)
  298. {
  299. CAddress addrConnect;
  300. const char* pszGet;
  301. const char* pszKeyword;
  302. if (fUseProxy)
  303. return false;
  304. for (int nLookup = 0; nLookup <= 1; nLookup++)
  305. for (int nHost = 1; nHost <= 2; nHost++)
  306. {
  307. // We should be phasing out our use of sites like these. If we need
  308. // replacements, we should ask for volunteers to put this simple
  309. // php file on their webserver that prints the client IP:
  310. // <?php echo $_SERVER["REMOTE_ADDR"]; ?>
  311. if (nHost == 1)
  312. {
  313. addrConnect = CAddress("91.198.22.70",80); // checkip.dyndns.org
  314. if (nLookup == 1)
  315. {
  316. CAddress addrIP("checkip.dyndns.org", 80, true);
  317. if (addrIP.IsValid())
  318. addrConnect = addrIP;
  319. }
  320. pszGet = "GET / HTTP/1.1\r\n"
  321. "Host: checkip.dyndns.org\r\n"
  322. "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n"
  323. "Connection: close\r\n"
  324. "\r\n";
  325. pszKeyword = "Address:";
  326. }
  327. else if (nHost == 2)
  328. {
  329. addrConnect = CAddress("74.208.43.192", 80); // www.showmyip.com
  330. if (nLookup == 1)
  331. {
  332. CAddress addrIP("www.showmyip.com", 80, true);
  333. if (addrIP.IsValid())
  334. addrConnect = addrIP;
  335. }
  336. pszGet = "GET /simple/ HTTP/1.1\r\n"
  337. "Host: www.showmyip.com\r\n"
  338. "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n"
  339. "Connection: close\r\n"
  340. "\r\n";
  341. pszKeyword = NULL; // Returns just IP address
  342. }
  343. if (GetMyExternalIP2(addrConnect, pszGet, pszKeyword, ipRet))
  344. return true;
  345. }
  346. return false;
  347. }
  348. void ThreadGetMyExternalIP(void* parg)
  349. {
  350. // Wait for IRC to get it first
  351. if (!GetBoolArg("-noirc"))
  352. {
  353. for (int i = 0; i < 2 * 60; i++)
  354. {
  355. Sleep(1000);
  356. if (fGotExternalIP || fShutdown)
  357. return;
  358. }
  359. }
  360. // Fallback in case IRC fails to get it
  361. if (GetMyExternalIP(addrLocalHost.ip))
  362. {
  363. printf("GetMyExternalIP() returned %s\n", addrLocalHost.ToStringIP().c_str());
  364. if (addrLocalHost.IsRoutable())
  365. {
  366. // If we already connected to a few before we had our IP, go back and addr them.
  367. // setAddrKnown automatically filters any duplicate sends.
  368. CAddress addr(addrLocalHost);
  369. addr.nTime = GetAdjustedTime();
  370. CRITICAL_BLOCK(cs_vNodes)
  371. BOOST_FOREACH(CNode* pnode, vNodes)
  372. pnode->PushAddress(addr);
  373. }
  374. }
  375. }
  376. bool AddAddress(CAddress addr, int64 nTimePenalty, CAddrDB *pAddrDB)
  377. {
  378. if (!addr.IsRoutable())
  379. return false;
  380. if (addr.ip == addrLocalHost.ip)
  381. return false;
  382. addr.nTime = max((int64)0, (int64)addr.nTime - nTimePenalty);
  383. CRITICAL_BLOCK(cs_mapAddresses)
  384. {
  385. map<vector<unsigned char>, CAddress>::iterator it = mapAddresses.find(addr.GetKey());
  386. if (it == mapAddresses.end())
  387. {
  388. // New address
  389. printf("AddAddress(%s)\n", addr.ToString().c_str());
  390. mapAddresses.insert(make_pair(addr.GetKey(), addr));
  391. if (pAddrDB)
  392. pAddrDB->WriteAddress(addr);
  393. else
  394. CAddrDB().WriteAddress(addr);
  395. return true;
  396. }
  397. else
  398. {
  399. bool fUpdated = false;
  400. CAddress& addrFound = (*it).second;
  401. if ((addrFound.nServices | addr.nServices) != addrFound.nServices)
  402. {
  403. // Services have been added
  404. addrFound.nServices |= addr.nServices;
  405. fUpdated = true;
  406. }
  407. bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < 24 * 60 * 60);
  408. int64 nUpdateInterval = (fCurrentlyOnline ? 60 * 60 : 24 * 60 * 60);
  409. if (addrFound.nTime < addr.nTime - nUpdateInterval)
  410. {
  411. // Periodically update most recently seen time
  412. addrFound.nTime = addr.nTime;
  413. fUpdated = true;
  414. }
  415. if (fUpdated)
  416. {
  417. if (pAddrDB)
  418. pAddrDB->WriteAddress(addrFound);
  419. else
  420. CAddrDB().WriteAddress(addrFound);
  421. }
  422. }
  423. }
  424. return false;
  425. }
  426. void AddressCurrentlyConnected(const CAddress& addr)
  427. {
  428. CRITICAL_BLOCK(cs_mapAddresses)
  429. {
  430. // Only if it's been published already
  431. map<vector<unsigned char>, CAddress>::iterator it = mapAddresses.find(addr.GetKey());
  432. if (it != mapAddresses.end())
  433. {
  434. CAddress& addrFound = (*it).second;
  435. int64 nUpdateInterval = 20 * 60;
  436. if (addrFound.nTime < GetAdjustedTime() - nUpdateInterval)
  437. {
  438. // Periodically update most recently seen time
  439. addrFound.nTime = GetAdjustedTime();
  440. CAddrDB addrdb;
  441. addrdb.WriteAddress(addrFound);
  442. }
  443. }
  444. }
  445. }
  446. void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1)
  447. {
  448. // If the dialog might get closed before the reply comes back,
  449. // call this in the destructor so it doesn't get called after it's deleted.
  450. CRITICAL_BLOCK(cs_vNodes)
  451. {
  452. BOOST_FOREACH(CNode* pnode, vNodes)
  453. {
  454. CRITICAL_BLOCK(pnode->cs_mapRequests)
  455. {
  456. for (map<uint256, CRequestTracker>::iterator mi = pnode->mapRequests.begin(); mi != pnode->mapRequests.end();)
  457. {
  458. CRequestTracker& tracker = (*mi).second;
  459. if (tracker.fn == fn && tracker.param1 == param1)
  460. pnode->mapRequests.erase(mi++);
  461. else
  462. mi++;
  463. }
  464. }
  465. }
  466. }
  467. }
  468. //
  469. // Subscription methods for the broadcast and subscription system.
  470. // Channel numbers are message numbers, i.e. MSG_TABLE and MSG_PRODUCT.
  471. //
  472. // The subscription system uses a meet-in-the-middle strategy.
  473. // With 100,000 nodes, if senders broadcast to 1000 random nodes and receivers
  474. // subscribe to 1000 random nodes, 99.995% (1 - 0.99^1000) of messages will get through.
  475. //
  476. bool AnySubscribed(unsigned int nChannel)
  477. {
  478. if (pnodeLocalHost->IsSubscribed(nChannel))
  479. return true;
  480. CRITICAL_BLOCK(cs_vNodes)
  481. BOOST_FOREACH(CNode* pnode, vNodes)
  482. if (pnode->IsSubscribed(nChannel))
  483. return true;
  484. return false;
  485. }
  486. bool CNode::IsSubscribed(unsigned int nChannel)
  487. {
  488. if (nChannel >= vfSubscribe.size())
  489. return false;
  490. return vfSubscribe[nChannel];
  491. }
  492. void CNode::Subscribe(unsigned int nChannel, unsigned int nHops)
  493. {
  494. if (nChannel >= vfSubscribe.size())
  495. return;
  496. if (!AnySubscribed(nChannel))
  497. {
  498. // Relay subscribe
  499. CRITICAL_BLOCK(cs_vNodes)
  500. BOOST_FOREACH(CNode* pnode, vNodes)
  501. if (pnode != this)
  502. pnode->PushMessage("subscribe", nChannel, nHops);
  503. }
  504. vfSubscribe[nChannel] = true;
  505. }
  506. void CNode::CancelSubscribe(unsigned int nChannel)
  507. {
  508. if (nChannel >= vfSubscribe.size())
  509. return;
  510. // Prevent from relaying cancel if wasn't subscribed
  511. if (!vfSubscribe[nChannel])
  512. return;
  513. vfSubscribe[nChannel] = false;
  514. if (!AnySubscribed(nChannel))
  515. {
  516. // Relay subscription cancel
  517. CRITICAL_BLOCK(cs_vNodes)
  518. BOOST_FOREACH(CNode* pnode, vNodes)
  519. if (pnode != this)
  520. pnode->PushMessage("sub-cancel", nChannel);
  521. }
  522. }
  523. CNode* FindNode(unsigned int ip)
  524. {
  525. CRITICAL_BLOCK(cs_vNodes)
  526. {
  527. BOOST_FOREACH(CNode* pnode, vNodes)
  528. if (pnode->addr.ip == ip)
  529. return (pnode);
  530. }
  531. return NULL;
  532. }
  533. CNode* FindNode(CAddress addr)
  534. {
  535. CRITICAL_BLOCK(cs_vNodes)
  536. {
  537. BOOST_FOREACH(CNode* pnode, vNodes)
  538. if (pnode->addr == addr)
  539. return (pnode);
  540. }
  541. return NULL;
  542. }
  543. CNode* ConnectNode(CAddress addrConnect, int64 nTimeout)
  544. {
  545. if (addrConnect.ip == addrLocalHost.ip)
  546. return NULL;
  547. // Look for an existing connection
  548. CNode* pnode = FindNode(addrConnect.ip);
  549. if (pnode)
  550. {
  551. if (nTimeout != 0)
  552. pnode->AddRef(nTimeout);
  553. else
  554. pnode->AddRef();
  555. return pnode;
  556. }
  557. /// debug print
  558. printf("trying connection %s lastseen=%.1fhrs lasttry=%.1fhrs\n",
  559. addrConnect.ToString().c_str(),
  560. (double)(addrConnect.nTime - GetAdjustedTime())/3600.0,
  561. (double)(addrConnect.nLastTry - GetAdjustedTime())/3600.0);
  562. CRITICAL_BLOCK(cs_mapAddresses)
  563. mapAddresses[addrConnect.GetKey()].nLastTry = GetAdjustedTime();
  564. // Connect
  565. SOCKET hSocket;
  566. if (ConnectSocket(addrConnect, hSocket))
  567. {
  568. /// debug print
  569. printf("connected %s\n", addrConnect.ToString().c_str());
  570. // Set to nonblocking
  571. #ifdef __WXMSW__
  572. u_long nOne = 1;
  573. if (ioctlsocket(hSocket, FIONBIO, &nOne) == SOCKET_ERROR)
  574. printf("ConnectSocket() : ioctlsocket nonblocking setting failed, error %d\n", WSAGetLastError());
  575. #else
  576. if (fcntl(hSocket, F_SETFL, O_NONBLOCK) == SOCKET_ERROR)
  577. printf("ConnectSocket() : fcntl nonblocking setting failed, error %d\n", errno);
  578. #endif
  579. // Add node
  580. CNode* pnode = new CNode(hSocket, addrConnect, false);
  581. if (nTimeout != 0)
  582. pnode->AddRef(nTimeout);
  583. else
  584. pnode->AddRef();
  585. CRITICAL_BLOCK(cs_vNodes)
  586. vNodes.push_back(pnode);
  587. pnode->nTimeConnected = GetTime();
  588. return pnode;
  589. }
  590. else
  591. {
  592. return NULL;
  593. }
  594. }
  595. void CNode::CloseSocketDisconnect()
  596. {
  597. fDisconnect = true;
  598. if (hSocket != INVALID_SOCKET)
  599. {
  600. if (fDebug)
  601. printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
  602. printf("disconnecting node %s\n", addr.ToString().c_str());
  603. closesocket(hSocket);
  604. hSocket = INVALID_SOCKET;
  605. }
  606. }
  607. void CNode::Cleanup()
  608. {
  609. // All of a nodes broadcasts and subscriptions are automatically torn down
  610. // when it goes down, so a node has to stay up to keep its broadcast going.
  611. // Cancel subscriptions
  612. for (unsigned int nChannel = 0; nChannel < vfSubscribe.size(); nChannel++)
  613. if (vfSubscribe[nChannel])
  614. CancelSubscribe(nChannel);
  615. }
  616. void ThreadSocketHandler(void* parg)
  617. {
  618. IMPLEMENT_RANDOMIZE_STACK(ThreadSocketHandler(parg));
  619. try
  620. {
  621. vnThreadsRunning[0]++;
  622. ThreadSocketHandler2(parg);
  623. vnThreadsRunning[0]--;
  624. }
  625. catch (std::exception& e) {
  626. vnThreadsRunning[0]--;
  627. PrintException(&e, "ThreadSocketHandler()");
  628. } catch (...) {
  629. vnThreadsRunning[0]--;
  630. throw; // support pthread_cancel()
  631. }
  632. printf("ThreadSocketHandler exiting\n");
  633. }
  634. void ThreadSocketHandler2(void* parg)
  635. {
  636. printf("ThreadSocketHandler started\n");
  637. list<CNode*> vNodesDisconnected;
  638. int nPrevNodeCount = 0;
  639. loop
  640. {
  641. //
  642. // Disconnect nodes
  643. //
  644. CRITICAL_BLOCK(cs_vNodes)
  645. {
  646. // Disconnect unused nodes
  647. vector<CNode*> vNodesCopy = vNodes;
  648. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  649. {
  650. if (pnode->fDisconnect ||
  651. (pnode->GetRefCount() <= 0 && pnode->vRecv.empty() && pnode->vSend.empty()))
  652. {
  653. // remove from vNodes
  654. vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
  655. // close socket and cleanup
  656. pnode->CloseSocketDisconnect();
  657. pnode->Cleanup();
  658. // hold in disconnected pool until all refs are released
  659. pnode->nReleaseTime = max(pnode->nReleaseTime, GetTime() + 15 * 60);
  660. if (pnode->fNetworkNode || pnode->fInbound)
  661. pnode->Release();
  662. vNodesDisconnected.push_back(pnode);
  663. }
  664. }
  665. // Delete disconnected nodes
  666. list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
  667. BOOST_FOREACH(CNode* pnode, vNodesDisconnectedCopy)
  668. {
  669. // wait until threads are done using it
  670. if (pnode->GetRefCount() <= 0)
  671. {
  672. bool fDelete = false;
  673. TRY_CRITICAL_BLOCK(pnode->cs_vSend)
  674. TRY_CRITICAL_BLOCK(pnode->cs_vRecv)
  675. TRY_CRITICAL_BLOCK(pnode->cs_mapRequests)
  676. TRY_CRITICAL_BLOCK(pnode->cs_inventory)
  677. fDelete = true;
  678. if (fDelete)
  679. {
  680. vNodesDisconnected.remove(pnode);
  681. delete pnode;
  682. }
  683. }
  684. }
  685. }
  686. if (vNodes.size() != nPrevNodeCount)
  687. {
  688. nPrevNodeCount = vNodes.size();
  689. MainFrameRepaint();
  690. }
  691. //
  692. // Find which sockets have data to receive
  693. //
  694. struct timeval timeout;
  695. timeout.tv_sec = 0;
  696. timeout.tv_usec = 50000; // frequency to poll pnode->vSend
  697. fd_set fdsetRecv;
  698. fd_set fdsetSend;
  699. fd_set fdsetError;
  700. FD_ZERO(&fdsetRecv);
  701. FD_ZERO(&fdsetSend);
  702. FD_ZERO(&fdsetError);
  703. SOCKET hSocketMax = 0;
  704. if(hListenSocket != INVALID_SOCKET)
  705. FD_SET(hListenSocket, &fdsetRecv);
  706. hSocketMax = max(hSocketMax, hListenSocket);
  707. CRITICAL_BLOCK(cs_vNodes)
  708. {
  709. BOOST_FOREACH(CNode* pnode, vNodes)
  710. {
  711. if (pnode->hSocket == INVALID_SOCKET)
  712. continue;
  713. FD_SET(pnode->hSocket, &fdsetRecv);
  714. FD_SET(pnode->hSocket, &fdsetError);
  715. hSocketMax = max(hSocketMax, pnode->hSocket);
  716. TRY_CRITICAL_BLOCK(pnode->cs_vSend)
  717. if (!pnode->vSend.empty())
  718. FD_SET(pnode->hSocket, &fdsetSend);
  719. }
  720. }
  721. vnThreadsRunning[0]--;
  722. int nSelect = select(hSocketMax + 1, &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
  723. vnThreadsRunning[0]++;
  724. if (fShutdown)
  725. return;
  726. if (nSelect == SOCKET_ERROR)
  727. {
  728. int nErr = WSAGetLastError();
  729. if (hSocketMax > -1)
  730. {
  731. printf("socket select error %d\n", nErr);
  732. for (int i = 0; i <= hSocketMax; i++)
  733. FD_SET(i, &fdsetRecv);
  734. }
  735. FD_ZERO(&fdsetSend);
  736. FD_ZERO(&fdsetError);
  737. Sleep(timeout.tv_usec/1000);
  738. }
  739. //
  740. // Accept new connections
  741. //
  742. if (hListenSocket != INVALID_SOCKET && FD_ISSET(hListenSocket, &fdsetRecv))
  743. {
  744. struct sockaddr_in sockaddr;
  745. socklen_t len = sizeof(sockaddr);
  746. SOCKET hSocket = accept(hListenSocket, (struct sockaddr*)&sockaddr, &len);
  747. CAddress addr(sockaddr);
  748. int nInbound = 0;
  749. CRITICAL_BLOCK(cs_vNodes)
  750. BOOST_FOREACH(CNode* pnode, vNodes)
  751. if (pnode->fInbound)
  752. nInbound++;
  753. if (hSocket == INVALID_SOCKET)
  754. {
  755. if (WSAGetLastError() != WSAEWOULDBLOCK)
  756. printf("socket error accept failed: %d\n", WSAGetLastError());
  757. }
  758. else if (nInbound >= GetArg("-maxconnections", 125) - MAX_OUTBOUND_CONNECTIONS)
  759. {
  760. closesocket(hSocket);
  761. }
  762. else
  763. {
  764. printf("accepted connection %s\n", addr.ToString().c_str());
  765. CNode* pnode = new CNode(hSocket, addr, true);
  766. pnode->AddRef();
  767. CRITICAL_BLOCK(cs_vNodes)
  768. vNodes.push_back(pnode);
  769. }
  770. }
  771. //
  772. // Service each socket
  773. //
  774. vector<CNode*> vNodesCopy;
  775. CRITICAL_BLOCK(cs_vNodes)
  776. {
  777. vNodesCopy = vNodes;
  778. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  779. pnode->AddRef();
  780. }
  781. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  782. {
  783. if (fShutdown)
  784. return;
  785. //
  786. // Receive
  787. //
  788. if (pnode->hSocket == INVALID_SOCKET)
  789. continue;
  790. if (FD_ISSET(pnode->hSocket, &fdsetRecv) || FD_ISSET(pnode->hSocket, &fdsetError))
  791. {
  792. TRY_CRITICAL_BLOCK(pnode->cs_vRecv)
  793. {
  794. CDataStream& vRecv = pnode->vRecv;
  795. unsigned int nPos = vRecv.size();
  796. if (nPos > ReceiveBufferSize()) {
  797. if (!pnode->fDisconnect)
  798. printf("socket recv flood control disconnect (%d bytes)\n", vRecv.size());
  799. pnode->CloseSocketDisconnect();
  800. }
  801. else {
  802. // typical socket buffer is 8K-64K
  803. char pchBuf[0x10000];
  804. int nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
  805. if (nBytes > 0)
  806. {
  807. vRecv.resize(nPos + nBytes);
  808. memcpy(&vRecv[nPos], pchBuf, nBytes);
  809. pnode->nLastRecv = GetTime();
  810. }
  811. else if (nBytes == 0)
  812. {
  813. // socket closed gracefully
  814. if (!pnode->fDisconnect)
  815. printf("socket closed\n");
  816. pnode->CloseSocketDisconnect();
  817. }
  818. else if (nBytes < 0)
  819. {
  820. // error
  821. int nErr = WSAGetLastError();
  822. if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
  823. {
  824. if (!pnode->fDisconnect)
  825. printf("socket recv error %d\n", nErr);
  826. pnode->CloseSocketDisconnect();
  827. }
  828. }
  829. }
  830. }
  831. }
  832. //
  833. // Send
  834. //
  835. if (pnode->hSocket == INVALID_SOCKET)
  836. continue;
  837. if (FD_ISSET(pnode->hSocket, &fdsetSend))
  838. {
  839. TRY_CRITICAL_BLOCK(pnode->cs_vSend)
  840. {
  841. CDataStream& vSend = pnode->vSend;
  842. if (!vSend.empty())
  843. {
  844. int nBytes = send(pnode->hSocket, &vSend[0], vSend.size(), MSG_NOSIGNAL | MSG_DONTWAIT);
  845. if (nBytes > 0)
  846. {
  847. vSend.erase(vSend.begin(), vSend.begin() + nBytes);
  848. pnode->nLastSend = GetTime();
  849. }
  850. else if (nBytes < 0)
  851. {
  852. // error
  853. int nErr = WSAGetLastError();
  854. if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
  855. {
  856. printf("socket send error %d\n", nErr);
  857. pnode->CloseSocketDisconnect();
  858. }
  859. }
  860. if (vSend.size() > SendBufferSize()) {
  861. if (!pnode->fDisconnect)
  862. printf("socket send flood control disconnect (%d bytes)\n", vSend.size());
  863. pnode->CloseSocketDisconnect();
  864. }
  865. }
  866. }
  867. }
  868. //
  869. // Inactivity checking
  870. //
  871. if (pnode->vSend.empty())
  872. pnode->nLastSendEmpty = GetTime();
  873. if (GetTime() - pnode->nTimeConnected > 60)
  874. {
  875. if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
  876. {
  877. printf("socket no message in first 60 seconds, %d %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0);
  878. pnode->fDisconnect = true;
  879. }
  880. else if (GetTime() - pnode->nLastSend > 90*60 && GetTime() - pnode->nLastSendEmpty > 90*60)
  881. {
  882. printf("socket not sending\n");
  883. pnode->fDisconnect = true;
  884. }
  885. else if (GetTime() - pnode->nLastRecv > 90*60)
  886. {
  887. printf("socket inactivity timeout\n");
  888. pnode->fDisconnect = true;
  889. }
  890. }
  891. }
  892. CRITICAL_BLOCK(cs_vNodes)
  893. {
  894. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  895. pnode->Release();
  896. }
  897. Sleep(10);
  898. }
  899. }
  900. #ifdef USE_UPNP
  901. void ThreadMapPort(void* parg)
  902. {
  903. IMPLEMENT_RANDOMIZE_STACK(ThreadMapPort(parg));
  904. try
  905. {
  906. vnThreadsRunning[5]++;
  907. ThreadMapPort2(parg);
  908. vnThreadsRunning[5]--;
  909. }
  910. catch (std::exception& e) {
  911. vnThreadsRunning[5]--;
  912. PrintException(&e, "ThreadMapPort()");
  913. } catch (...) {
  914. vnThreadsRunning[5]--;
  915. PrintException(NULL, "ThreadMapPort()");
  916. }
  917. printf("ThreadMapPort exiting\n");
  918. }
  919. void ThreadMapPort2(void* parg)
  920. {
  921. printf("ThreadMapPort started\n");
  922. char port[6];
  923. sprintf(port, "%d", GetListenPort());
  924. const char * rootdescurl = 0;
  925. const char * multicastif = 0;
  926. const char * minissdpdpath = 0;
  927. int error = 0;
  928. struct UPNPDev * devlist = 0;
  929. char lanaddr[64];
  930. devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
  931. struct UPNPUrls urls;
  932. struct IGDdatas data;
  933. int r;
  934. r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
  935. if (r == 1)
  936. {
  937. char intClient[16];
  938. char intPort[6];
  939. string strDesc = "Bitcoin " + FormatFullVersion();
  940. r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
  941. port, port, lanaddr, strDesc.c_str(), "TCP", 0, "0");
  942. if(r!=UPNPCOMMAND_SUCCESS)
  943. printf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
  944. port, port, lanaddr, r, strupnperror(r));
  945. else
  946. printf("UPnP Port Mapping successful.\n");
  947. loop {
  948. if (fShutdown || !fUseUPnP)
  949. {
  950. r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port, "TCP", 0);
  951. printf("UPNP_DeletePortMapping() returned : %d\n", r);
  952. freeUPNPDevlist(devlist); devlist = 0;
  953. FreeUPNPUrls(&urls);
  954. return;
  955. }
  956. Sleep(2000);
  957. }
  958. } else {
  959. printf("No valid UPnP IGDs found\n");
  960. freeUPNPDevlist(devlist); devlist = 0;
  961. if (r != 0)
  962. FreeUPNPUrls(&urls);
  963. loop {
  964. if (fShutdown || !fUseUPnP)
  965. return;
  966. Sleep(2000);
  967. }
  968. }
  969. }
  970. void MapPort(bool fMapPort)
  971. {
  972. if (fUseUPnP != fMapPort)
  973. {
  974. fUseUPnP = fMapPort;
  975. WriteSetting("fUseUPnP", fUseUPnP);
  976. }
  977. if (fUseUPnP && vnThreadsRunning[5] < 1)
  978. {
  979. if (!CreateThread(ThreadMapPort, NULL))
  980. printf("Error: ThreadMapPort(ThreadMapPort) failed\n");
  981. }
  982. }
  983. #else
  984. void MapPort(bool /* unused fMapPort */)
  985. {
  986. // Intentionally left blank.
  987. }
  988. #endif
  989. static const char *strDNSSeed[] = {
  990. "bitseed.xf2.org",
  991. "bitseed.bitcoin.org.uk",
  992. "dnsseed.bluematt.me",
  993. };
  994. void DNSAddressSeed()
  995. {
  996. int found = 0;
  997. if (!fTestNet)
  998. {
  999. printf("Loading addresses from DNS seeds (could take a while)\n");
  1000. CAddrDB addrDB;
  1001. addrDB.TxnBegin();
  1002. for (int seed_idx = 0; seed_idx < ARRAYLEN(strDNSSeed); seed_idx++) {
  1003. vector<CAddress> vaddr;
  1004. if (Lookup(strDNSSeed[seed_idx], vaddr, NODE_NETWORK, -1, true))
  1005. {
  1006. BOOST_FOREACH (CAddress& addr, vaddr)
  1007. {
  1008. if (addr.GetByte(3) != 127)
  1009. {
  1010. addr.nTime = 0;
  1011. AddAddress(addr, 0, &addrDB);
  1012. found++;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. addrDB.TxnCommit(); // Save addresses (it's ok if this fails)
  1018. }
  1019. printf("%d addresses found from DNS seeds\n", found);
  1020. }
  1021. unsigned int pnSeed[] =
  1022. {
  1023. 0x1ddb1032, 0x6242ce40, 0x52d6a445, 0x2dd7a445, 0x8a53cd47, 0x73263750, 0xda23c257, 0xecd4ed57,
  1024. 0x0a40ec59, 0x75dce160, 0x7df76791, 0x89370bad, 0xa4f214ad, 0x767700ae, 0x638b0418, 0x868a1018,
  1025. 0xcd9f332e, 0x0129653e, 0xcc92dc3e, 0x96671640, 0x56487e40, 0x5b66f440, 0xb1d01f41, 0xf1dc6041,
  1026. 0xc1d12b42, 0x86ba1243, 0x6be4df43, 0x6d4cef43, 0xd18e0644, 0x1ab0b344, 0x6584a345, 0xe7c1a445,
  1027. 0x58cea445, 0xc5daa445, 0x21dda445, 0x3d3b5346, 0x13e55347, 0x1080d24a, 0x8e611e4b, 0x81518e4b,
  1028. 0x6c839e4b, 0xe2ad0a4c, 0xfbbc0a4c, 0x7f5b6e4c, 0x7244224e, 0x1300554e, 0x20690652, 0x5a48b652,
  1029. 0x75c5c752, 0x4335cc54, 0x340fd154, 0x87c07455, 0x087b2b56, 0x8a133a57, 0xac23c257, 0x70374959,
  1030. 0xfb63d45b, 0xb9a1685c, 0x180d765c, 0x674f645d, 0x04d3495e, 0x1de44b5e, 0x4ee8a362, 0x0ded1b63,
  1031. 0xc1b04b6d, 0x8d921581, 0x97b7ea82, 0x1cf83a8e, 0x91490bad, 0x09dc75ae, 0x9a6d79ae, 0xa26d79ae,
  1032. 0x0fd08fae, 0x0f3e3fb2, 0x4f944fb2, 0xcca448b8, 0x3ecd6ab8, 0xa9d5a5bc, 0x8d0119c1, 0x045997d5,
  1033. 0xca019dd9, 0x0d526c4d, 0xabf1ba44, 0x66b1ab55, 0x1165f462, 0x3ed7cbad, 0xa38fae6e, 0x3bd2cbad,
  1034. 0xd36f0547, 0x20df7840, 0x7a337742, 0x549f8e4b, 0x9062365c, 0xd399f562, 0x2b5274a1, 0x8edfa153,
  1035. 0x3bffb347, 0x7074bf58, 0xb74fcbad, 0x5b5a795b, 0x02fa29ce, 0x5a6738d4, 0xe8a1d23e, 0xef98c445,
  1036. 0x4b0f494c, 0xa2bc1e56, 0x7694ad63, 0xa4a800c3, 0x05fda6cd, 0x9f22175e, 0x364a795b, 0x536285d5,
  1037. 0xac44c9d4, 0x0b06254d, 0x150c2fd4, 0x32a50dcc, 0xfd79ce48, 0xf15cfa53, 0x66c01e60, 0x6bc26661,
  1038. 0xc03b47ae, 0x4dda1b81, 0x3285a4c1, 0x883ca96d, 0x35d60a4c, 0xdae09744, 0x2e314d61, 0x84e247cf,
  1039. 0x6c814552, 0x3a1cc658, 0x98d8f382, 0xe584cb5b, 0x15e86057, 0x7b01504e, 0xd852dd48, 0x56382f56,
  1040. 0x0a5df454, 0xa0d18d18, 0x2e89b148, 0xa79c114c, 0xcbdcd054, 0x5523bc43, 0xa9832640, 0x8a066144,
  1041. 0x3894c3bc, 0xab76bf58, 0x6a018ac1, 0xfebf4f43, 0x2f26c658, 0x31102f4e, 0x85e929d5, 0x2a1c175e,
  1042. 0xfc6c2cd1, 0x27b04b6d, 0xdf024650, 0x161748b8, 0x28be6580, 0x57be6580, 0x1cee677a, 0xaa6bb742,
  1043. 0x9a53964b, 0x0a5a2d4d, 0x2434c658, 0x9a494f57, 0x1ebb0e48, 0xf610b85d, 0x077ecf44, 0x085128bc,
  1044. 0x5ba17a18, 0x27ca1b42, 0xf8a00b56, 0xfcd4c257, 0xcf2fc15e, 0xd897e052, 0x4cada04f, 0x2f35f6d5,
  1045. 0x382ce8c9, 0xe523984b, 0x3f946846, 0x60c8be43, 0x41da6257, 0xde0be142, 0xae8a544b, 0xeff0c254,
  1046. 0x1e0f795b, 0xaeb28890, 0xca16acd9, 0x1e47ddd8, 0x8c8c4829, 0xd27dc747, 0xd53b1663, 0x4096b163,
  1047. 0x9c8dd958, 0xcb12f860, 0x9e79305c, 0x40c1a445, 0x4a90c2bc, 0x2c3a464d, 0x2727f23c, 0x30b04b6d,
  1048. 0x59024cb8, 0xa091e6ad, 0x31b04b6d, 0xc29d46a6, 0x63934fb2, 0xd9224dbe, 0x9f5910d8, 0x7f530a6b,
  1049. 0x752e9c95, 0x65453548, 0xa484be46, 0xce5a1b59, 0x710e0718, 0x46a13d18, 0xdaaf5318, 0xc4a8ff53,
  1050. 0x87abaa52, 0xb764cf51, 0xb2025d4a, 0x6d351e41, 0xc035c33e, 0xa432c162, 0x61ef34ae, 0xd16fddbc,
  1051. 0x0870e8c1, 0x3070e8c1, 0x9c71e8c1, 0xa4992363, 0x85a1f663, 0x4184e559, 0x18d96ed8, 0x17b8dbd5,
  1052. 0x60e7cd18, 0xe5ee104c, 0xab17ac62, 0x1e786e1b, 0x5d23b762, 0xf2388fae, 0x88270360, 0x9e5b3d80,
  1053. 0x7da518b2, 0xb5613b45, 0x1ad41f3e, 0xd550854a, 0x8617e9a9, 0x925b229c, 0xf2e92542, 0x47af0544,
  1054. 0x73b5a843, 0xb9b7a0ad, 0x03a748d0, 0x0a6ff862, 0x6694df62, 0x3bfac948, 0x8e098f4f, 0x746916c3,
  1055. 0x02f38e4f, 0x40bb1243, 0x6a54d162, 0x6008414b, 0xa513794c, 0x514aa343, 0x63781747, 0xdbb6795b,
  1056. 0xed065058, 0x42d24b46, 0x1518794c, 0x9b271681, 0x73e4ffad, 0x0654784f, 0x438dc945, 0x641846a6,
  1057. 0x2d1b0944, 0x94b59148, 0x8d369558, 0xa5a97662, 0x8b705b42, 0xce9204ae, 0x8d584450, 0x2df61555,
  1058. 0xeebff943, 0x2e75fb4d, 0x3ef8fc57, 0x9921135e, 0x8e31042e, 0xb5afad43, 0x89ecedd1, 0x9cfcc047,
  1059. 0x8fcd0f4c, 0xbe49f5ad, 0x146a8d45, 0x98669ab8, 0x98d9175e, 0xd1a8e46d, 0x839a3ab8, 0x40a0016c,
  1060. 0x6d27c257, 0x977fffad, 0x7baa5d5d, 0x1213be43, 0xb167e5a9, 0x640fe8ca, 0xbc9ea655, 0x0f820a4c,
  1061. 0x0f097059, 0x69ac957c, 0x366d8453, 0xb1ba2844, 0x8857f081, 0x70b5be63, 0xc545454b, 0xaf36ded1,
  1062. 0xb5a4b052, 0x21f062d1, 0x72ab89b2, 0x74a45318, 0x8312e6bc, 0xb916965f, 0x8aa7c858, 0xfe7effad,
  1063. };
  1064. void ThreadOpenConnections(void* parg)
  1065. {
  1066. IMPLEMENT_RANDOMIZE_STACK(ThreadOpenConnections(parg));
  1067. try
  1068. {
  1069. vnThreadsRunning[1]++;
  1070. ThreadOpenConnections2(parg);
  1071. vnThreadsRunning[1]--;
  1072. }
  1073. catch (std::exception& e) {
  1074. vnThreadsRunning[1]--;
  1075. PrintException(&e, "ThreadOpenConnections()");
  1076. } catch (...) {
  1077. vnThreadsRunning[1]--;
  1078. PrintException(NULL, "ThreadOpenConnections()");
  1079. }
  1080. printf("ThreadOpenConnections exiting\n");
  1081. }
  1082. void ThreadOpenConnections2(void* parg)
  1083. {
  1084. printf("ThreadOpenConnections started\n");
  1085. // Connect to specific addresses
  1086. if (mapArgs.count("-connect"))
  1087. {
  1088. for (int64 nLoop = 0;; nLoop++)
  1089. {
  1090. BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"])
  1091. {
  1092. CAddress addr(strAddr, fAllowDNS);
  1093. if (addr.IsValid())
  1094. OpenNetworkConnection(addr);
  1095. for (int i = 0; i < 10 && i < nLoop; i++)
  1096. {
  1097. Sleep(500);
  1098. if (fShutdown)
  1099. return;
  1100. }
  1101. }
  1102. }
  1103. }
  1104. // Connect to manually added nodes first
  1105. if (mapArgs.count("-addnode"))
  1106. {
  1107. BOOST_FOREACH(string strAddr, mapMultiArgs["-addnode"])
  1108. {
  1109. CAddress addr(strAddr, fAllowDNS);
  1110. if (addr.IsValid())
  1111. {
  1112. OpenNetworkConnection(addr);
  1113. Sleep(500);
  1114. if (fShutdown)
  1115. return;
  1116. }
  1117. }
  1118. }
  1119. // Initiate network connections
  1120. int64 nStart = GetTime();
  1121. loop
  1122. {
  1123. // Limit outbound connections
  1124. vnThreadsRunning[1]--;
  1125. Sleep(500);
  1126. loop
  1127. {
  1128. int nOutbound = 0;
  1129. CRITICAL_BLOCK(cs_vNodes)
  1130. BOOST_FOREACH(CNode* pnode, vNodes)
  1131. if (!pnode->fInbound)
  1132. nOutbound++;
  1133. int nMaxOutboundConnections = MAX_OUTBOUND_CONNECTIONS;
  1134. nMaxOutboundConnections = min(nMaxOutboundConnections, (int)GetArg("-maxconnections", 125));
  1135. if (nOutbound < nMaxOutboundConnections)
  1136. break;
  1137. Sleep(2000);
  1138. if (fShutdown)
  1139. return;
  1140. }
  1141. vnThreadsRunning[1]++;
  1142. if (fShutdown)
  1143. return;
  1144. CRITICAL_BLOCK(cs_mapAddresses)
  1145. {
  1146. // Add seed nodes if IRC isn't working
  1147. static bool fSeedUsed;
  1148. bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
  1149. if (mapAddresses.empty() && (GetTime() - nStart > 60 || fTOR) && !fTestNet)
  1150. {
  1151. for (int i = 0; i < ARRAYLEN(pnSeed); i++)
  1152. {
  1153. // It'll only connect to one or two seed nodes because once it connects,
  1154. // it'll get a pile of addresses with newer timestamps.
  1155. CAddress addr;
  1156. addr.ip = pnSeed[i];
  1157. addr.nTime = 0;
  1158. AddAddress(addr);
  1159. }
  1160. fSeedUsed = true;
  1161. }
  1162. if (fSeedUsed && mapAddresses.size() > ARRAYLEN(pnSeed) + 100)
  1163. {
  1164. // Disconnect seed nodes
  1165. set<unsigned int> setSeed(pnSeed, pnSeed + ARRAYLEN(pnSeed));
  1166. static int64 nSeedDisconnected;
  1167. if (nSeedDisconnected == 0)
  1168. {
  1169. nSeedDisconnected = GetTime();
  1170. CRITICAL_BLOCK(cs_vNodes)
  1171. BOOST_FOREACH(CNode* pnode, vNodes)
  1172. if (setSeed.count(pnode->addr.ip))
  1173. pnode->fDisconnect = true;
  1174. }
  1175. // Keep setting timestamps to 0 so they won't reconnect
  1176. if (GetTime() - nSeedDisconnected < 60 * 60)
  1177. {
  1178. BOOST_FOREACH(PAIRTYPE(const vector<unsigned char>, CAddress)& item, mapAddresses)
  1179. {
  1180. if (setSeed.count(item.second.ip) && item.second.nTime != 0)
  1181. {
  1182. item.second.nTime = 0;
  1183. CAddrDB().WriteAddress(item.second);
  1184. }
  1185. }
  1186. }
  1187. }
  1188. }
  1189. //
  1190. // Choose an address to connect to based on most recently seen
  1191. //
  1192. CAddress addrConnect;
  1193. int64 nBest = INT64_MIN;
  1194. // Only connect to one address per a.b.?.? range.
  1195. // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
  1196. set<unsigned int> setConnected;
  1197. CRITICAL_BLOCK(cs_vNodes)
  1198. BOOST_FOREACH(CNode* pnode, vNodes)
  1199. setConnected.insert(pnode->addr.ip & 0x0000ffff);
  1200. CRITICAL_BLOCK(cs_mapAddresses)
  1201. {
  1202. BOOST_FOREACH(const PAIRTYPE(vector<unsigned char>, CAddress)& item, mapAddresses)
  1203. {
  1204. const CAddress& addr = item.second;
  1205. if (!addr.IsIPv4() || !addr.IsValid() || setConnected.count(addr.ip & 0x0000ffff))
  1206. continue;
  1207. int64 nSinceLastSeen = GetAdjustedTime() - addr.nTime;
  1208. int64 nSinceLastTry = GetAdjustedTime() - addr.nLastTry;
  1209. // Randomize the order in a deterministic way, putting the standard port first
  1210. int64 nRandomizer = (uint64)(nStart * 4951 + addr.nLastTry * 9567851 + addr.ip * 7789) % (2 * 60 * 60);
  1211. if (addr.port != htons(GetDefaultPort()))
  1212. nRandomizer += 2 * 60 * 60;
  1213. // Last seen Base retry frequency
  1214. // <1 hour 10 min
  1215. // 1 hour 1 hour
  1216. // 4 hours 2 hours
  1217. // 24 hours 5 hours
  1218. // 48 hours 7 hours
  1219. // 7 days 13 hours
  1220. // 30 days 27 hours
  1221. // 90 days 46 hours
  1222. // 365 days 93 hours
  1223. int64 nDelay = (int64)(3600.0 * sqrt(fabs((double)nSinceLastSeen) / 3600.0) + nRandomizer);
  1224. // Fast reconnect for one hour after last seen
  1225. if (nSinceLastSeen < 60 * 60)
  1226. nDelay = 10 * 60;
  1227. // Limit retry frequency
  1228. if (nSinceLastTry < nDelay)
  1229. continue;
  1230. // If we have IRC, we'll be notified when they first come online,
  1231. // and again every 24 hours by the refresh broadcast.
  1232. if (nGotIRCAddresses > 0 && vNodes.size() >= 2 && nSinceLastSeen > 24 * 60 * 60)
  1233. continue;
  1234. // Only try the old stuff if we don't have enough connections
  1235. if (vNodes.size() >= 8 && nSinceLastSeen > 24 * 60 * 60)
  1236. continue;
  1237. // If multiple addresses are ready, prioritize by time since
  1238. // last seen and time since last tried.
  1239. int64 nScore = min(nSinceLastTry, (int64)24 * 60 * 60) - nSinceLastSeen - nRandomizer;
  1240. if (nScore > nBest)
  1241. {
  1242. nBest = nScore;
  1243. addrConnect = addr;
  1244. }
  1245. }
  1246. }
  1247. if (addrConnect.IsValid())
  1248. OpenNetworkConnection(addrConnect);
  1249. }
  1250. }
  1251. bool OpenNetworkConnection(const CAddress& addrConnect)
  1252. {
  1253. //
  1254. // Initiate outbound network connection
  1255. //
  1256. if (fShutdown)
  1257. return false;
  1258. if (addrConnect.ip == addrLocalHost.ip || !addrConnect.IsIPv4() || FindNode(addrConnect.ip))
  1259. return false;
  1260. vnThreadsRunning[1]--;
  1261. CNode* pnode = ConnectNode(addrConnect);
  1262. vnThreadsRunning[1]++;
  1263. if (fShutdown)
  1264. return false;
  1265. if (!pnode)
  1266. return false;
  1267. pnode->fNetworkNode = true;
  1268. return true;
  1269. }
  1270. void ThreadMessageHandler(void* parg)
  1271. {
  1272. IMPLEMENT_RANDOMIZE_STACK(ThreadMessageHandler(parg));
  1273. try
  1274. {
  1275. vnThreadsRunning[2]++;
  1276. ThreadMessageHandler2(parg);
  1277. vnThreadsRunning[2]--;
  1278. }
  1279. catch (std::exception& e) {
  1280. vnThreadsRunning[2]--;
  1281. PrintException(&e, "ThreadMessageHandler()");
  1282. } catch (...) {
  1283. vnThreadsRunning[2]--;
  1284. PrintException(NULL, "ThreadMessageHandler()");
  1285. }
  1286. printf("ThreadMessageHandler exiting\n");
  1287. }
  1288. void ThreadMessageHandler2(void* parg)
  1289. {
  1290. printf("ThreadMessageHandler started\n");
  1291. SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL);
  1292. while (!fShutdown)
  1293. {
  1294. vector<CNode*> vNodesCopy;
  1295. CRITICAL_BLOCK(cs_vNodes)
  1296. {
  1297. vNodesCopy = vNodes;
  1298. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  1299. pnode->AddRef();
  1300. }
  1301. // Poll the connected nodes for messages
  1302. CNode* pnodeTrickle = NULL;
  1303. if (!vNodesCopy.empty())
  1304. pnodeTrickle = vNodesCopy[GetRand(vNodesCopy.size())];
  1305. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  1306. {
  1307. // Receive messages
  1308. TRY_CRITICAL_BLOCK(pnode->cs_vRecv)
  1309. ProcessMessages(pnode);
  1310. if (fShutdown)
  1311. return;
  1312. // Send messages
  1313. TRY_CRITICAL_BLOCK(pnode->cs_vSend)
  1314. SendMessages(pnode, pnode == pnodeTrickle);
  1315. if (fShutdown)
  1316. return;
  1317. }
  1318. CRITICAL_BLOCK(cs_vNodes)
  1319. {
  1320. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  1321. pnode->Release();
  1322. }
  1323. // Wait and allow messages to bunch up.
  1324. // Reduce vnThreadsRunning so StopNode has permission to exit while
  1325. // we're sleeping, but we must always check fShutdown after doing this.
  1326. vnThreadsRunning[2]--;
  1327. Sleep(100);
  1328. if (fRequestShutdown)
  1329. Shutdown(NULL);
  1330. vnThreadsRunning[2]++;
  1331. if (fShutdown)
  1332. return;
  1333. }
  1334. }
  1335. bool BindListenPort(string& strError)
  1336. {
  1337. strError = "";
  1338. int nOne = 1;
  1339. addrLocalHost.port = htons(GetListenPort());
  1340. #ifdef __WXMSW__
  1341. // Initialize Windows Sockets
  1342. WSADATA wsadata;
  1343. int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
  1344. if (ret != NO_ERROR)
  1345. {
  1346. strError = strprintf("Error: TCP/IP socket library failed to start (WSAStartup returned error %d)", ret);
  1347. printf("%s\n", strError.c_str());
  1348. return false;
  1349. }
  1350. #endif
  1351. // Create socket for listening for incoming connections
  1352. hListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  1353. if (hListenSocket == INVALID_SOCKET)
  1354. {
  1355. strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %d)", WSAGetLastError());
  1356. printf("%s\n", strError.c_str());
  1357. return false;
  1358. }
  1359. #ifdef SO_NOSIGPIPE
  1360. // Different way of disabling SIGPIPE on BSD
  1361. setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
  1362. #endif
  1363. #ifndef __WXMSW__
  1364. // Allow binding if the port is still in TIME_WAIT state after
  1365. // the program was closed and restarted. Not an issue on windows.
  1366. setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
  1367. #endif
  1368. #ifdef __WXMSW__
  1369. // Set to nonblocking, incoming connections will also inherit this
  1370. if (ioctlsocket(hListenSocket, FIONBIO, (u_long*)&nOne) == SOCKET_ERROR)
  1371. #else
  1372. if (fcntl(hListenSocket, F_SETFL, O_NONBLOCK) == SOCKET_ERROR)
  1373. #endif
  1374. {
  1375. strError = strprintf("Error: Couldn't set properties on socket for incoming connections (error %d)", WSAGetLastError());
  1376. printf("%s\n", strError.c_str());
  1377. return false;
  1378. }
  1379. // The sockaddr_in structure specifies the address family,
  1380. // IP address, and port for the socket that is being bound
  1381. struct sockaddr_in sockaddr;
  1382. memset(&sockaddr, 0, sizeof(sockaddr));
  1383. sockaddr.sin_family = AF_INET;
  1384. sockaddr.sin_addr.s_addr = INADDR_ANY; // bind to all IPs on this computer
  1385. sockaddr.sin_port = htons(GetListenPort());
  1386. if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
  1387. {
  1388. int nErr = WSAGetLastError();
  1389. if (nErr == WSAEADDRINUSE)
  1390. strError = strprintf(_("Unable to bind to port %d on this computer. Bitcoin is probably already running."), ntohs(sockaddr.sin_port));
  1391. else
  1392. strError = strprintf("Error: Unable to bind to port %d on this computer (bind returned error %d)", ntohs(sockaddr.sin_port), nErr);
  1393. printf("%s\n", strError.c_str());
  1394. return false;
  1395. }
  1396. printf("Bound to port %d\n", ntohs(sockaddr.sin_port));
  1397. // Listen for incoming connections
  1398. if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
  1399. {
  1400. strError = strprintf("Error: Listening for incoming connections failed (listen returned error %d)", WSAGetLastError());
  1401. printf("%s\n", strError.c_str());
  1402. return false;
  1403. }
  1404. return true;
  1405. }
  1406. void StartNode(void* parg)
  1407. {
  1408. if (pnodeLocalHost == NULL)
  1409. pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress("127.0.0.1", 0, false, nLocalServices));
  1410. #ifdef __WXMSW__
  1411. // Get local host ip
  1412. char pszHostName[1000] = "";
  1413. if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
  1414. {
  1415. vector<CAddress> vaddr;
  1416. if (Lookup(pszHostName, vaddr, nLocalServices, -1, true))
  1417. BOOST_FOREACH (const CAddress &addr, vaddr)
  1418. if (addr.GetByte(3) != 127)
  1419. {
  1420. addrLocalHost = addr;
  1421. break;
  1422. }
  1423. }
  1424. #else
  1425. // Get local host ip
  1426. struct ifaddrs* myaddrs;
  1427. if (getifaddrs(&myaddrs) == 0)
  1428. {
  1429. for (struct ifaddrs* ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
  1430. {
  1431. if (ifa->ifa_addr == NULL) continue;
  1432. if ((ifa->ifa_flags & IFF_UP) == 0) continue;
  1433. if (strcmp(ifa->ifa_name, "lo") == 0) continue;
  1434. if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
  1435. char pszIP[100];
  1436. if (ifa->ifa_addr->sa_family == AF_INET)
  1437. {
  1438. struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
  1439. if (inet_ntop(ifa->ifa_addr->sa_family, (void*)&(s4->sin_addr), pszIP, sizeof(pszIP)) != NULL)
  1440. printf("ipv4 %s: %s\n", ifa->ifa_name, pszIP);
  1441. // Take the first IP that isn't loopback 127.x.x.x
  1442. CAddress addr(*(unsigned int*)&s4->sin_addr, GetListenPort(), nLocalServices);
  1443. if (addr.IsValid() && addr.GetByte(3) != 127)
  1444. {
  1445. addrLocalHost = addr;
  1446. break;
  1447. }
  1448. }
  1449. else if (ifa->ifa_addr->sa_family == AF_INET6)
  1450. {
  1451. struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
  1452. if (inet_ntop(ifa->ifa_addr->sa_family, (void*)&(s6->sin6_addr), pszIP, sizeof(pszIP)) != NULL)
  1453. printf("ipv6 %s: %s\n", ifa->ifa_name, pszIP);
  1454. }
  1455. }
  1456. freeifaddrs(myaddrs);
  1457. }
  1458. #endif
  1459. printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str());
  1460. if (fUseProxy || mapArgs.count("-connect") || fNoListen)
  1461. {
  1462. // Proxies can't take incoming connections
  1463. addrLocalHost.ip = CAddress("0.0.0.0").ip;
  1464. printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str());
  1465. }
  1466. else
  1467. {
  1468. CreateThread(ThreadGetMyExternalIP, NULL);
  1469. }
  1470. //
  1471. // Start threads
  1472. //
  1473. // Map ports with UPnP
  1474. if (fHaveUPnP)
  1475. MapPort(fUseUPnP);
  1476. // Get addresses from IRC and advertise ours
  1477. if (!CreateThread(ThreadIRCSeed, NULL))
  1478. printf("Error: CreateThread(ThreadIRCSeed) failed\n");
  1479. // Send and receive from sockets, accept connections
  1480. CreateThread(ThreadSocketHandler, NULL, true);
  1481. // Initiate outbound connections
  1482. if (!CreateThread(ThreadOpenConnections, NULL))
  1483. printf("Error: CreateThread(ThreadOpenConnections) failed\n");
  1484. // Process messages
  1485. if (!CreateThread(ThreadMessageHandler, NULL))
  1486. printf("Error: CreateThread(ThreadMessageHandler) failed\n");
  1487. // Generate coins in the background
  1488. GenerateBitcoins(fGenerateBitcoins, pwalletMain);
  1489. }
  1490. bool StopNode()
  1491. {
  1492. printf("StopNode()\n");
  1493. fShutdown = true;
  1494. nTransactionsUpdated++;
  1495. int64 nStart = GetTime();
  1496. while (vnThreadsRunning[0] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0
  1497. #ifdef USE_UPNP
  1498. || vnThreadsRunning[5] > 0
  1499. #endif
  1500. )
  1501. {
  1502. if (GetTime() - nStart > 20)
  1503. break;
  1504. Sleep(20);
  1505. }
  1506. if (vnThreadsRunning[0] > 0) printf("ThreadSocketHandler still running\n");
  1507. if (vnThreadsRunning[1] > 0) printf("ThreadOpenConnections still running\n");
  1508. if (vnThreadsRunning[2] > 0) printf("ThreadMessageHandler still running\n");
  1509. if (vnThreadsRunning[3] > 0) printf("ThreadBitcoinMiner still running\n");
  1510. if (vnThreadsRunning[4] > 0) printf("ThreadRPCServer still running\n");
  1511. if (fHaveUPnP && vnThreadsRunning[5] > 0) printf("ThreadMapPort still running\n");
  1512. while (vnThreadsRunning[2] > 0 || vnThreadsRunning[4] > 0)
  1513. Sleep(20);
  1514. Sleep(50);
  1515. return true;
  1516. }
  1517. class CNetCleanup
  1518. {
  1519. public:
  1520. CNetCleanup()
  1521. {
  1522. }
  1523. ~CNetCleanup()
  1524. {
  1525. // Close sockets
  1526. BOOST_FOREACH(CNode* pnode, vNodes)
  1527. if (pnode->hSocket != INVALID_SOCKET)
  1528. closesocket(pnode->hSocket);
  1529. if (hListenSocket != INVALID_SOCKET)
  1530. if (closesocket(hListenSocket) == SOCKET_ERROR)
  1531. printf("closesocket(hListenSocket) failed with error %d\n", WSAGetLastError());
  1532. #ifdef __WXMSW__
  1533. // Shutdown Windows Sockets
  1534. WSACleanup();
  1535. #endif
  1536. }
  1537. }
  1538. instance_of_cnetcleanup;