raw
experimental-genesis    1 //  /****************************\
experimental-genesis 2 // * EXPERIMENTAL BRANCH. *
experimental-genesis 3 // * FOR LABORATORY USE ONLY. *
experimental-genesis 4 // ********************************
experimental-genesis 5 // ************
experimental-genesis 6 // **************
experimental-genesis 7 // ****************
experimental-genesis 8 // **** **** ****
experimental-genesis 9 // *** *** ***
experimental-genesis 10 // *** *** ***
experimental-genesis 11 // *** * * **
experimental-genesis 12 // ******** ********
experimental-genesis 13 // ******* ******
experimental-genesis 14 // *** **
experimental-genesis 15 // * ******* **
experimental-genesis 16 // ** * * * * *
experimental-genesis 17 // ** * * ***
experimental-genesis 18 // **** * * * * ****
experimental-genesis 19 // **** *** * * ** ***
experimental-genesis 20 // **** ********* ******
experimental-genesis 21 // ******* ***** *******
experimental-genesis 22 // ********* ****** **
experimental-genesis 23 // ** ****** ******
experimental-genesis 24 // ** ******* **
experimental-genesis 25 // ** ******* ***
experimental-genesis 26 // **** ******** ************
experimental-genesis 27 // ************ ************
experimental-genesis 28 // ******** *******
experimental-genesis 29 // ****** ****
experimental-genesis 30 // *** ***
experimental-genesis 31 // ********************************
experimental-genesis 32 // Copyright (c) 2009-2010 Satoshi Nakamoto
experimental-genesis 33 // Copyright (c) 2009-2012 The Bitcoin developers
experimental-genesis 34 // Distributed under the MIT/X11 software license, see the accompanying
experimental-genesis 35 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
experimental-genesis 36
experimental-genesis 37 #include "headers.h"
experimental-genesis 38 #include "irc.h"
experimental-genesis 39 #include "db.h"
experimental-genesis 40 #include "net.h"
experimental-genesis 41 #include "init.h"
experimental-genesis 42 #include "strlcpy.h"
experimental-genesis 43
experimental-genesis 44 #ifdef WIN32
experimental-genesis 45 #include <string.h>
experimental-genesis 46 #endif
experimental-genesis 47
experimental-genesis 48 #ifdef USE_UPNP
experimental-genesis 49 #include <miniupnpc/miniwget.h>
experimental-genesis 50 #include <miniupnpc/miniupnpc.h>
experimental-genesis 51 #include <miniupnpc/upnpcommands.h>
experimental-genesis 52 #include <miniupnpc/upnperrors.h>
experimental-genesis 53 #endif
experimental-genesis 54
experimental-genesis 55 using namespace std;
experimental-genesis 56 using namespace boost;
experimental-genesis 57
experimental-genesis 58 static const int MAX_OUTBOUND_CONNECTIONS = 8;
experimental-genesis 59
experimental-genesis 60 void ThreadMessageHandler2(void* parg);
experimental-genesis 61 void ThreadSocketHandler2(void* parg);
experimental-genesis 62 void ThreadOpenConnections2(void* parg);
experimental-genesis 63 #ifdef USE_UPNP
experimental-genesis 64 void ThreadMapPort2(void* parg);
experimental-genesis 65 #endif
experimental-genesis 66 void ThreadDNSAddressSeed2(void* parg);
experimental-genesis 67 bool OpenNetworkConnection(const CAddress& addrConnect);
experimental-genesis 68
experimental-genesis 69
experimental-genesis 70
experimental-genesis 71
experimental-genesis 72
experimental-genesis 73 //
experimental-genesis 74 // Global state variables
experimental-genesis 75 //
experimental-genesis 76 bool fClient = false;
experimental-genesis 77 bool fAllowDNS = false;
experimental-genesis 78 uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
experimental-genesis 79 CAddress addrLocalHost("0.0.0.0", 0, false, nLocalServices);
experimental-genesis 80 static CNode* pnodeLocalHost = NULL;
experimental-genesis 81 uint64 nLocalHostNonce = 0;
experimental-genesis 82 array<int, 10> vnThreadsRunning;
experimental-genesis 83 static SOCKET hListenSocket = INVALID_SOCKET;
experimental-genesis 84
experimental-genesis 85 vector<CNode*> vNodes;
experimental-genesis 86 CCriticalSection cs_vNodes;
experimental-genesis 87 map<vector<unsigned char>, CAddress> mapAddresses;
experimental-genesis 88 CCriticalSection cs_mapAddresses;
experimental-genesis 89 map<CInv, CDataStream> mapRelay;
experimental-genesis 90 deque<pair<int64, CInv> > vRelayExpiration;
experimental-genesis 91 CCriticalSection cs_mapRelay;
experimental-genesis 92 map<CInv, int64> mapAlreadyAskedFor;
experimental-genesis 93
experimental-genesis 94 // Settings
experimental-genesis 95 int fUseProxy = false;
experimental-genesis 96 int nConnectTimeout = 5000;
experimental-genesis 97 CAddress addrProxy("127.0.0.1",9050);
experimental-genesis 98
experimental-genesis 99
experimental-genesis 100
experimental-genesis 101
experimental-genesis 102 unsigned short GetListenPort()
experimental-genesis 103 {
experimental-genesis 104 return (unsigned short)(GetArg("-port", GetDefaultPort()));
experimental-genesis 105 }
experimental-genesis 106
experimental-genesis 107 void CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd)
experimental-genesis 108 {
experimental-genesis 109 // Filter out duplicate requests
experimental-genesis 110 if (pindexBegin == pindexLastGetBlocksBegin && hashEnd == hashLastGetBlocksEnd)
experimental-genesis 111 return;
experimental-genesis 112 pindexLastGetBlocksBegin = pindexBegin;
experimental-genesis 113 hashLastGetBlocksEnd = hashEnd;
experimental-genesis 114
experimental-genesis 115 PushMessage("getblocks", CBlockLocator(pindexBegin), hashEnd);
experimental-genesis 116 }
experimental-genesis 117
experimental-genesis 118
experimental-genesis 119
experimental-genesis 120
experimental-genesis 121
experimental-genesis 122 bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet, int nTimeout)
experimental-genesis 123 {
experimental-genesis 124 hSocketRet = INVALID_SOCKET;
experimental-genesis 125
experimental-genesis 126 SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
experimental-genesis 127 if (hSocket == INVALID_SOCKET)
experimental-genesis 128 return false;
experimental-genesis 129 #ifdef SO_NOSIGPIPE
experimental-genesis 130 int set = 1;
experimental-genesis 131 setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
experimental-genesis 132 #endif
experimental-genesis 133
experimental-genesis 134 bool fProxy = (fUseProxy && addrConnect.IsRoutable());
experimental-genesis 135 struct sockaddr_in sockaddr = (fProxy ? addrProxy.GetSockAddr() : addrConnect.GetSockAddr());
experimental-genesis 136
experimental-genesis 137 #ifdef WIN32
experimental-genesis 138 u_long fNonblock = 1;
experimental-genesis 139 if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
experimental-genesis 140 #else
experimental-genesis 141 int fFlags = fcntl(hSocket, F_GETFL, 0);
experimental-genesis 142 if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1)
experimental-genesis 143 #endif
experimental-genesis 144 {
experimental-genesis 145 closesocket(hSocket);
experimental-genesis 146 return false;
experimental-genesis 147 }
experimental-genesis 148
experimental-genesis 149
experimental-genesis 150 if (connect(hSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
experimental-genesis 151 {
experimental-genesis 152 // WSAEINVAL is here because some legacy version of winsock uses it
experimental-genesis 153 if (WSAGetLastError() == WSAEINPROGRESS || WSAGetLastError() == WSAEWOULDBLOCK || WSAGetLastError() == WSAEINVAL)
experimental-genesis 154 {
experimental-genesis 155 struct timeval timeout;
experimental-genesis 156 timeout.tv_sec = nTimeout / 1000;
experimental-genesis 157 timeout.tv_usec = (nTimeout % 1000) * 1000;
experimental-genesis 158
experimental-genesis 159 fd_set fdset;
experimental-genesis 160 FD_ZERO(&fdset);
experimental-genesis 161 FD_SET(hSocket, &fdset);
experimental-genesis 162 int nRet = select(hSocket + 1, NULL, &fdset, NULL, &timeout);
experimental-genesis 163 if (nRet == 0)
experimental-genesis 164 {
experimental-genesis 165 printf("connection timeout\n");
experimental-genesis 166 closesocket(hSocket);
experimental-genesis 167 return false;
experimental-genesis 168 }
experimental-genesis 169 if (nRet == SOCKET_ERROR)
experimental-genesis 170 {
experimental-genesis 171 printf("select() for connection failed: %i\n",WSAGetLastError());
experimental-genesis 172 closesocket(hSocket);
experimental-genesis 173 return false;
experimental-genesis 174 }
experimental-genesis 175 socklen_t nRetSize = sizeof(nRet);
experimental-genesis 176 #ifdef WIN32
experimental-genesis 177 if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char*)(&nRet), &nRetSize) == SOCKET_ERROR)
experimental-genesis 178 #else
experimental-genesis 179 if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR)
experimental-genesis 180 #endif
experimental-genesis 181 {
experimental-genesis 182 printf("getsockopt() for connection failed: %i\n",WSAGetLastError());
experimental-genesis 183 closesocket(hSocket);
experimental-genesis 184 return false;
experimental-genesis 185 }
experimental-genesis 186 if (nRet != 0)
experimental-genesis 187 {
experimental-genesis 188 printf("connect() failed after select(): %s\n",strerror(nRet));
experimental-genesis 189 closesocket(hSocket);
experimental-genesis 190 return false;
experimental-genesis 191 }
experimental-genesis 192 }
experimental-genesis 193 #ifdef WIN32
experimental-genesis 194 else if (WSAGetLastError() != WSAEISCONN)
experimental-genesis 195 #else
experimental-genesis 196 else
experimental-genesis 197 #endif
experimental-genesis 198 {
experimental-genesis 199 printf("connect() failed: %i\n",WSAGetLastError());
experimental-genesis 200 closesocket(hSocket);
experimental-genesis 201 return false;
experimental-genesis 202 }
experimental-genesis 203 }
experimental-genesis 204
experimental-genesis 205 /*
experimental-genesis 206 this isn't even strictly necessary
experimental-genesis 207 CNode::ConnectNode immediately turns the socket back to non-blocking
experimental-genesis 208 but we'll turn it back to blocking just in case
experimental-genesis 209 */
experimental-genesis 210 #ifdef WIN32
experimental-genesis 211 fNonblock = 0;
experimental-genesis 212 if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
experimental-genesis 213 #else
experimental-genesis 214 fFlags = fcntl(hSocket, F_GETFL, 0);
experimental-genesis 215 if (fcntl(hSocket, F_SETFL, fFlags & !O_NONBLOCK) == SOCKET_ERROR)
experimental-genesis 216 #endif
experimental-genesis 217 {
experimental-genesis 218 closesocket(hSocket);
experimental-genesis 219 return false;
experimental-genesis 220 }
experimental-genesis 221
experimental-genesis 222 if (fProxy)
experimental-genesis 223 {
experimental-genesis 224 printf("proxy connecting %s\n", addrConnect.ToString().c_str());
experimental-genesis 225 char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user";
experimental-genesis 226 memcpy(pszSocks4IP + 2, &addrConnect.port, 2);
experimental-genesis 227 memcpy(pszSocks4IP + 4, &addrConnect.ip, 4);
experimental-genesis 228 char* pszSocks4 = pszSocks4IP;
experimental-genesis 229 int nSize = sizeof(pszSocks4IP);
experimental-genesis 230
experimental-genesis 231 int ret = send(hSocket, pszSocks4, nSize, MSG_NOSIGNAL);
experimental-genesis 232 if (ret != nSize)
experimental-genesis 233 {
experimental-genesis 234 closesocket(hSocket);
experimental-genesis 235 return error("Error sending to proxy");
experimental-genesis 236 }
experimental-genesis 237 char pchRet[8];
experimental-genesis 238 if (recv(hSocket, pchRet, 8, 0) != 8)
experimental-genesis 239 {
experimental-genesis 240 closesocket(hSocket);
experimental-genesis 241 return error("Error reading proxy response");
experimental-genesis 242 }
experimental-genesis 243 if (pchRet[1] != 0x5a)
experimental-genesis 244 {
experimental-genesis 245 closesocket(hSocket);
experimental-genesis 246 if (pchRet[1] != 0x5b)
experimental-genesis 247 printf("ERROR: Proxy returned error %d\n", pchRet[1]);
experimental-genesis 248 return false;
experimental-genesis 249 }
experimental-genesis 250 printf("proxy connected %s\n", addrConnect.ToString().c_str());
experimental-genesis 251 }
experimental-genesis 252
experimental-genesis 253 hSocketRet = hSocket;
experimental-genesis 254 return true;
experimental-genesis 255 }
experimental-genesis 256
experimental-genesis 257 // portDefault is in host order
experimental-genesis 258 bool Lookup(const char *pszName, vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup, int portDefault, bool fAllowPort)
experimental-genesis 259 {
experimental-genesis 260 vaddr.clear();
experimental-genesis 261 if (pszName[0] == 0)
experimental-genesis 262 return false;
experimental-genesis 263 int port = portDefault;
experimental-genesis 264 char psz[256];
experimental-genesis 265 char *pszHost = psz;
experimental-genesis 266 strlcpy(psz, pszName, sizeof(psz));
experimental-genesis 267 if (fAllowPort)
experimental-genesis 268 {
experimental-genesis 269 char* pszColon = strrchr(psz+1,':');
experimental-genesis 270 char *pszPortEnd = NULL;
experimental-genesis 271 int portParsed = pszColon ? strtoul(pszColon+1, &pszPortEnd, 10) : 0;
experimental-genesis 272 if (pszColon && pszPortEnd && pszPortEnd[0] == 0)
experimental-genesis 273 {
experimental-genesis 274 if (psz[0] == '[' && pszColon[-1] == ']')
experimental-genesis 275 {
experimental-genesis 276 // Future: enable IPv6 colon-notation inside []
experimental-genesis 277 pszHost = psz+1;
experimental-genesis 278 pszColon[-1] = 0;
experimental-genesis 279 }
experimental-genesis 280 else
experimental-genesis 281 pszColon[0] = 0;
experimental-genesis 282 port = portParsed;
experimental-genesis 283 if (port < 0 || port > USHRT_MAX)
experimental-genesis 284 port = USHRT_MAX;
experimental-genesis 285 }
experimental-genesis 286 }
experimental-genesis 287
experimental-genesis 288 unsigned int addrIP = inet_addr(pszHost);
experimental-genesis 289 if (addrIP != INADDR_NONE)
experimental-genesis 290 {
experimental-genesis 291 // valid IP address passed
experimental-genesis 292 vaddr.push_back(CAddress(addrIP, port, nServices));
experimental-genesis 293 return true;
experimental-genesis 294 }
experimental-genesis 295
experimental-genesis 296 if (!fAllowLookup)
experimental-genesis 297 return false;
experimental-genesis 298
experimental-genesis 299 struct hostent* phostent = gethostbyname(pszHost);
experimental-genesis 300 if (!phostent)
experimental-genesis 301 return false;
experimental-genesis 302
experimental-genesis 303 if (phostent->h_addrtype != AF_INET)
experimental-genesis 304 return false;
experimental-genesis 305
experimental-genesis 306 char** ppAddr = phostent->h_addr_list;
experimental-genesis 307 while (*ppAddr != NULL && vaddr.size() != nMaxSolutions)
experimental-genesis 308 {
experimental-genesis 309 CAddress addr(((struct in_addr*)ppAddr[0])->s_addr, port, nServices);
experimental-genesis 310 if (addr.IsValid())
experimental-genesis 311 vaddr.push_back(addr);
experimental-genesis 312 ppAddr++;
experimental-genesis 313 }
experimental-genesis 314
experimental-genesis 315 return (vaddr.size() > 0);
experimental-genesis 316 }
experimental-genesis 317
experimental-genesis 318 // portDefault is in host order
experimental-genesis 319 bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup, int portDefault, bool fAllowPort)
experimental-genesis 320 {
experimental-genesis 321 vector<CAddress> vaddr;
experimental-genesis 322 bool fRet = Lookup(pszName, vaddr, nServices, 1, fAllowLookup, portDefault, fAllowPort);
experimental-genesis 323 if (fRet)
experimental-genesis 324 addr = vaddr[0];
experimental-genesis 325 return fRet;
experimental-genesis 326 }
experimental-genesis 327
experimental-genesis 328 bool GetMyExternalIP2(const CAddress& addrConnect, const char* pszGet, const char* pszKeyword, unsigned int& ipRet)
experimental-genesis 329 {
experimental-genesis 330 SOCKET hSocket;
experimental-genesis 331 if (!ConnectSocket(addrConnect, hSocket))
experimental-genesis 332 return error("GetMyExternalIP() : connection to %s failed", addrConnect.ToString().c_str());
experimental-genesis 333
experimental-genesis 334 send(hSocket, pszGet, strlen(pszGet), MSG_NOSIGNAL);
experimental-genesis 335
experimental-genesis 336 string strLine;
experimental-genesis 337 while (RecvLine(hSocket, strLine))
experimental-genesis 338 {
experimental-genesis 339 if (strLine.empty()) // HTTP response is separated from headers by blank line
experimental-genesis 340 {
experimental-genesis 341 loop
experimental-genesis 342 {
experimental-genesis 343 if (!RecvLine(hSocket, strLine))
experimental-genesis 344 {
experimental-genesis 345 closesocket(hSocket);
experimental-genesis 346 return false;
experimental-genesis 347 }
experimental-genesis 348 if (pszKeyword == NULL)
experimental-genesis 349 break;
experimental-genesis 350 if (strLine.find(pszKeyword) != -1)
experimental-genesis 351 {
experimental-genesis 352 strLine = strLine.substr(strLine.find(pszKeyword) + strlen(pszKeyword));
experimental-genesis 353 break;
experimental-genesis 354 }
experimental-genesis 355 }
experimental-genesis 356 closesocket(hSocket);
experimental-genesis 357 if (strLine.find("<") != -1)
experimental-genesis 358 strLine = strLine.substr(0, strLine.find("<"));
experimental-genesis 359 strLine = strLine.substr(strspn(strLine.c_str(), " \t\n\r"));
experimental-genesis 360 while (strLine.size() > 0 && isspace(strLine[strLine.size()-1]))
experimental-genesis 361 strLine.resize(strLine.size()-1);
experimental-genesis 362 CAddress addr(strLine,0,true);
experimental-genesis 363 printf("GetMyExternalIP() received [%s] %s\n", strLine.c_str(), addr.ToString().c_str());
experimental-genesis 364 if (addr.ip == 0 || addr.ip == INADDR_NONE || !addr.IsRoutable())
experimental-genesis 365 return false;
experimental-genesis 366 ipRet = addr.ip;
experimental-genesis 367 return true;
experimental-genesis 368 }
experimental-genesis 369 }
experimental-genesis 370 closesocket(hSocket);
experimental-genesis 371 return error("GetMyExternalIP() : connection closed");
experimental-genesis 372 }
experimental-genesis 373
experimental-genesis 374 // We now get our external IP from the IRC server first and only use this as a backup
experimental-genesis 375 bool GetMyExternalIP(unsigned int& ipRet)
experimental-genesis 376 {
experimental-genesis 377 CAddress addrConnect;
experimental-genesis 378 const char* pszGet;
experimental-genesis 379 const char* pszKeyword;
experimental-genesis 380
experimental-genesis 381 if (fUseProxy)
experimental-genesis 382 return false;
experimental-genesis 383
experimental-genesis 384 for (int nLookup = 0; nLookup <= 1; nLookup++)
experimental-genesis 385 for (int nHost = 1; nHost <= 2; nHost++)
experimental-genesis 386 {
experimental-genesis 387 // We should be phasing out our use of sites like these. If we need
experimental-genesis 388 // replacements, we should ask for volunteers to put this simple
experimental-genesis 389 // php file on their webserver that prints the client IP:
experimental-genesis 390 // <?php echo $_SERVER["REMOTE_ADDR"]; ?>
experimental-genesis 391 if (nHost == 1)
experimental-genesis 392 {
experimental-genesis 393 addrConnect = CAddress("91.198.22.70",80); // checkip.dyndns.org
experimental-genesis 394
experimental-genesis 395 if (nLookup == 1)
experimental-genesis 396 {
experimental-genesis 397 CAddress addrIP("checkip.dyndns.org", 80, true);
experimental-genesis 398 if (addrIP.IsValid())
experimental-genesis 399 addrConnect = addrIP;
experimental-genesis 400 }
experimental-genesis 401
experimental-genesis 402 pszGet = "GET / HTTP/1.1\r\n"
experimental-genesis 403 "Host: checkip.dyndns.org\r\n"
experimental-genesis 404 "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n"
experimental-genesis 405 "Connection: close\r\n"
experimental-genesis 406 "\r\n";
experimental-genesis 407
experimental-genesis 408 pszKeyword = "Address:";
experimental-genesis 409 }
experimental-genesis 410 else if (nHost == 2)
experimental-genesis 411 {
experimental-genesis 412 addrConnect = CAddress("74.208.43.192", 80); // www.showmyip.com
experimental-genesis 413
experimental-genesis 414 if (nLookup == 1)
experimental-genesis 415 {
experimental-genesis 416 CAddress addrIP("www.showmyip.com", 80, true);
experimental-genesis 417 if (addrIP.IsValid())
experimental-genesis 418 addrConnect = addrIP;
experimental-genesis 419 }
experimental-genesis 420
experimental-genesis 421 pszGet = "GET /simple/ HTTP/1.1\r\n"
experimental-genesis 422 "Host: www.showmyip.com\r\n"
experimental-genesis 423 "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n"
experimental-genesis 424 "Connection: close\r\n"
experimental-genesis 425 "\r\n";
experimental-genesis 426
experimental-genesis 427 pszKeyword = NULL; // Returns just IP address
experimental-genesis 428 }
experimental-genesis 429
experimental-genesis 430 if (GetMyExternalIP2(addrConnect, pszGet, pszKeyword, ipRet))
experimental-genesis 431 return true;
experimental-genesis 432 }
experimental-genesis 433
experimental-genesis 434 return false;
experimental-genesis 435 }
experimental-genesis 436
experimental-genesis 437 void ThreadGetMyExternalIP(void* parg)
experimental-genesis 438 {
experimental-genesis 439 // Wait for IRC to get it first
experimental-genesis 440 if (!GetBoolArg("-noirc"))
experimental-genesis 441 {
experimental-genesis 442 for (int i = 0; i < 2 * 60; i++)
experimental-genesis 443 {
experimental-genesis 444 Sleep(1000);
experimental-genesis 445 if (fGotExternalIP || fShutdown)
experimental-genesis 446 return;
experimental-genesis 447 }
experimental-genesis 448 }
experimental-genesis 449
experimental-genesis 450 // Fallback in case IRC fails to get it
experimental-genesis 451 if (GetMyExternalIP(addrLocalHost.ip))
experimental-genesis 452 {
experimental-genesis 453 printf("GetMyExternalIP() returned %s\n", addrLocalHost.ToStringIP().c_str());
experimental-genesis 454 if (addrLocalHost.IsRoutable())
experimental-genesis 455 {
experimental-genesis 456 // If we already connected to a few before we had our IP, go back and addr them.
experimental-genesis 457 // setAddrKnown automatically filters any duplicate sends.
experimental-genesis 458 CAddress addr(addrLocalHost);
experimental-genesis 459 addr.nTime = GetAdjustedTime();
experimental-genesis 460 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 461 BOOST_FOREACH(CNode* pnode, vNodes)
experimental-genesis 462 pnode->PushAddress(addr);
experimental-genesis 463 }
experimental-genesis 464 }
experimental-genesis 465 }
experimental-genesis 466
experimental-genesis 467
experimental-genesis 468
experimental-genesis 469
experimental-genesis 470
experimental-genesis 471 bool AddAddress(CAddress addr, int64 nTimePenalty, CAddrDB *pAddrDB)
experimental-genesis 472 {
experimental-genesis 473 if (!addr.IsRoutable())
experimental-genesis 474 return false;
experimental-genesis 475 if (addr.ip == addrLocalHost.ip)
experimental-genesis 476 return false;
experimental-genesis 477 addr.nTime = max((int64)0, (int64)addr.nTime - nTimePenalty);
experimental-genesis 478 bool fUpdated = false;
experimental-genesis 479 bool fNew = false;
experimental-genesis 480 CAddress addrFound = addr;
experimental-genesis 481
experimental-genesis 482 CRITICAL_BLOCK(cs_mapAddresses)
experimental-genesis 483 {
experimental-genesis 484 map<vector<unsigned char>, CAddress>::iterator it = mapAddresses.find(addr.GetKey());
experimental-genesis 485 if (it == mapAddresses.end())
experimental-genesis 486 {
experimental-genesis 487 // New address
experimental-genesis 488 printf("AddAddress(%s)\n", addr.ToString().c_str());
experimental-genesis 489 mapAddresses.insert(make_pair(addr.GetKey(), addr));
experimental-genesis 490 fUpdated = true;
experimental-genesis 491 fNew = true;
experimental-genesis 492 }
experimental-genesis 493 else
experimental-genesis 494 {
experimental-genesis 495 addrFound = (*it).second;
experimental-genesis 496 if ((addrFound.nServices | addr.nServices) != addrFound.nServices)
experimental-genesis 497 {
experimental-genesis 498 // Services have been added
experimental-genesis 499 addrFound.nServices |= addr.nServices;
experimental-genesis 500 fUpdated = true;
experimental-genesis 501 }
experimental-genesis 502 bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < 24 * 60 * 60);
experimental-genesis 503 int64 nUpdateInterval = (fCurrentlyOnline ? 60 * 60 : 24 * 60 * 60);
experimental-genesis 504 if (addrFound.nTime < addr.nTime - nUpdateInterval)
experimental-genesis 505 {
experimental-genesis 506 // Periodically update most recently seen time
experimental-genesis 507 addrFound.nTime = addr.nTime;
experimental-genesis 508 fUpdated = true;
experimental-genesis 509 }
experimental-genesis 510 }
experimental-genesis 511 }
experimental-genesis 512 // There is a nasty deadlock bug if this is done inside the cs_mapAddresses
experimental-genesis 513 // CRITICAL_BLOCK:
experimental-genesis 514 // Thread 1: begin db transaction (locks inside-db-mutex)
experimental-genesis 515 // then AddAddress (locks cs_mapAddresses)
experimental-genesis 516 // Thread 2: AddAddress (locks cs_mapAddresses)
experimental-genesis 517 // ... then db operation hangs waiting for inside-db-mutex
experimental-genesis 518 if (fUpdated)
experimental-genesis 519 {
experimental-genesis 520 if (pAddrDB)
experimental-genesis 521 pAddrDB->WriteAddress(addrFound);
experimental-genesis 522 else
experimental-genesis 523 CAddrDB().WriteAddress(addrFound);
experimental-genesis 524 }
experimental-genesis 525 return fNew;
experimental-genesis 526 }
experimental-genesis 527
experimental-genesis 528 void AddressCurrentlyConnected(const CAddress& addr)
experimental-genesis 529 {
experimental-genesis 530 CAddress *paddrFound = NULL;
experimental-genesis 531
experimental-genesis 532 CRITICAL_BLOCK(cs_mapAddresses)
experimental-genesis 533 {
experimental-genesis 534 // Only if it's been published already
experimental-genesis 535 map<vector<unsigned char>, CAddress>::iterator it = mapAddresses.find(addr.GetKey());
experimental-genesis 536 if (it != mapAddresses.end())
experimental-genesis 537 paddrFound = &(*it).second;
experimental-genesis 538 }
experimental-genesis 539
experimental-genesis 540 if (paddrFound)
experimental-genesis 541 {
experimental-genesis 542 int64 nUpdateInterval = 20 * 60;
experimental-genesis 543 if (paddrFound->nTime < GetAdjustedTime() - nUpdateInterval)
experimental-genesis 544 {
experimental-genesis 545 // Periodically update most recently seen time
experimental-genesis 546 paddrFound->nTime = GetAdjustedTime();
experimental-genesis 547 CAddrDB addrdb;
experimental-genesis 548 addrdb.WriteAddress(*paddrFound);
experimental-genesis 549 }
experimental-genesis 550 }
experimental-genesis 551 }
experimental-genesis 552
experimental-genesis 553
experimental-genesis 554
experimental-genesis 555
experimental-genesis 556
experimental-genesis 557 void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1)
experimental-genesis 558 {
experimental-genesis 559 // If the dialog might get closed before the reply comes back,
experimental-genesis 560 // call this in the destructor so it doesn't get called after it's deleted.
experimental-genesis 561 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 562 {
experimental-genesis 563 BOOST_FOREACH(CNode* pnode, vNodes)
experimental-genesis 564 {
experimental-genesis 565 CRITICAL_BLOCK(pnode->cs_mapRequests)
experimental-genesis 566 {
experimental-genesis 567 for (map<uint256, CRequestTracker>::iterator mi = pnode->mapRequests.begin(); mi != pnode->mapRequests.end();)
experimental-genesis 568 {
experimental-genesis 569 CRequestTracker& tracker = (*mi).second;
experimental-genesis 570 if (tracker.fn == fn && tracker.param1 == param1)
experimental-genesis 571 pnode->mapRequests.erase(mi++);
experimental-genesis 572 else
experimental-genesis 573 mi++;
experimental-genesis 574 }
experimental-genesis 575 }
experimental-genesis 576 }
experimental-genesis 577 }
experimental-genesis 578 }
experimental-genesis 579
experimental-genesis 580
experimental-genesis 581
experimental-genesis 582
experimental-genesis 583
experimental-genesis 584
experimental-genesis 585
experimental-genesis 586 //
experimental-genesis 587 // Subscription methods for the broadcast and subscription system.
experimental-genesis 588 // Channel numbers are message numbers, i.e. MSG_TABLE and MSG_PRODUCT.
experimental-genesis 589 //
experimental-genesis 590 // The subscription system uses a meet-in-the-middle strategy.
experimental-genesis 591 // With 100,000 nodes, if senders broadcast to 1000 random nodes and receivers
experimental-genesis 592 // subscribe to 1000 random nodes, 99.995% (1 - 0.99^1000) of messages will get through.
experimental-genesis 593 //
experimental-genesis 594
experimental-genesis 595 bool AnySubscribed(unsigned int nChannel)
experimental-genesis 596 {
experimental-genesis 597 if (pnodeLocalHost->IsSubscribed(nChannel))
experimental-genesis 598 return true;
experimental-genesis 599 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 600 BOOST_FOREACH(CNode* pnode, vNodes)
experimental-genesis 601 if (pnode->IsSubscribed(nChannel))
experimental-genesis 602 return true;
experimental-genesis 603 return false;
experimental-genesis 604 }
experimental-genesis 605
experimental-genesis 606 bool CNode::IsSubscribed(unsigned int nChannel)
experimental-genesis 607 {
experimental-genesis 608 if (nChannel >= vfSubscribe.size())
experimental-genesis 609 return false;
experimental-genesis 610 return vfSubscribe[nChannel];
experimental-genesis 611 }
experimental-genesis 612
experimental-genesis 613 void CNode::Subscribe(unsigned int nChannel, unsigned int nHops)
experimental-genesis 614 {
experimental-genesis 615 if (nChannel >= vfSubscribe.size())
experimental-genesis 616 return;
experimental-genesis 617
experimental-genesis 618 if (!AnySubscribed(nChannel))
experimental-genesis 619 {
experimental-genesis 620 // Relay subscribe
experimental-genesis 621 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 622 BOOST_FOREACH(CNode* pnode, vNodes)
experimental-genesis 623 if (pnode != this)
experimental-genesis 624 pnode->PushMessage("subscribe", nChannel, nHops);
experimental-genesis 625 }
experimental-genesis 626
experimental-genesis 627 vfSubscribe[nChannel] = true;
experimental-genesis 628 }
experimental-genesis 629
experimental-genesis 630 void CNode::CancelSubscribe(unsigned int nChannel)
experimental-genesis 631 {
experimental-genesis 632 if (nChannel >= vfSubscribe.size())
experimental-genesis 633 return;
experimental-genesis 634
experimental-genesis 635 // Prevent from relaying cancel if wasn't subscribed
experimental-genesis 636 if (!vfSubscribe[nChannel])
experimental-genesis 637 return;
experimental-genesis 638 vfSubscribe[nChannel] = false;
experimental-genesis 639
experimental-genesis 640 if (!AnySubscribed(nChannel))
experimental-genesis 641 {
experimental-genesis 642 // Relay subscription cancel
experimental-genesis 643 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 644 BOOST_FOREACH(CNode* pnode, vNodes)
experimental-genesis 645 if (pnode != this)
experimental-genesis 646 pnode->PushMessage("sub-cancel", nChannel);
experimental-genesis 647 }
experimental-genesis 648 }
experimental-genesis 649
experimental-genesis 650
experimental-genesis 651
experimental-genesis 652
experimental-genesis 653
experimental-genesis 654
experimental-genesis 655
experimental-genesis 656
experimental-genesis 657
experimental-genesis 658 CNode* FindNode(unsigned int ip)
experimental-genesis 659 {
experimental-genesis 660 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 661 {
experimental-genesis 662 BOOST_FOREACH(CNode* pnode, vNodes)
experimental-genesis 663 if (pnode->addr.ip == ip)
experimental-genesis 664 return (pnode);
experimental-genesis 665 }
experimental-genesis 666 return NULL;
experimental-genesis 667 }
experimental-genesis 668
experimental-genesis 669 CNode* FindNode(CAddress addr)
experimental-genesis 670 {
experimental-genesis 671 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 672 {
experimental-genesis 673 BOOST_FOREACH(CNode* pnode, vNodes)
experimental-genesis 674 if (pnode->addr == addr)
experimental-genesis 675 return (pnode);
experimental-genesis 676 }
experimental-genesis 677 return NULL;
experimental-genesis 678 }
experimental-genesis 679
experimental-genesis 680 CNode* ConnectNode(CAddress addrConnect, int64 nTimeout)
experimental-genesis 681 {
experimental-genesis 682 if (addrConnect.ip == addrLocalHost.ip)
experimental-genesis 683 return NULL;
experimental-genesis 684
experimental-genesis 685 // Look for an existing connection
experimental-genesis 686 CNode* pnode = FindNode(addrConnect.ip);
experimental-genesis 687 if (pnode)
experimental-genesis 688 {
experimental-genesis 689 if (nTimeout != 0)
experimental-genesis 690 pnode->AddRef(nTimeout);
experimental-genesis 691 else
experimental-genesis 692 pnode->AddRef();
experimental-genesis 693 return pnode;
experimental-genesis 694 }
experimental-genesis 695
experimental-genesis 696 /// debug print
experimental-genesis 697 printf("trying connection %s lastseen=%.1fhrs lasttry=%.1fhrs\n",
experimental-genesis 698 addrConnect.ToString().c_str(),
experimental-genesis 699 (double)(addrConnect.nTime - GetAdjustedTime())/3600.0,
experimental-genesis 700 (double)(addrConnect.nLastTry - GetAdjustedTime())/3600.0);
experimental-genesis 701
experimental-genesis 702 CRITICAL_BLOCK(cs_mapAddresses)
experimental-genesis 703 mapAddresses[addrConnect.GetKey()].nLastTry = GetAdjustedTime();
experimental-genesis 704
experimental-genesis 705 // Connect
experimental-genesis 706 SOCKET hSocket;
experimental-genesis 707 if (ConnectSocket(addrConnect, hSocket))
experimental-genesis 708 {
experimental-genesis 709 /// debug print
experimental-genesis 710 printf("connected %s\n", addrConnect.ToString().c_str());
experimental-genesis 711
experimental-genesis 712 // Set to nonblocking
experimental-genesis 713 #ifdef WIN32
experimental-genesis 714 u_long nOne = 1;
experimental-genesis 715 if (ioctlsocket(hSocket, FIONBIO, &nOne) == SOCKET_ERROR)
experimental-genesis 716 printf("ConnectSocket() : ioctlsocket nonblocking setting failed, error %d\n", WSAGetLastError());
experimental-genesis 717 #else
experimental-genesis 718 if (fcntl(hSocket, F_SETFL, O_NONBLOCK) == SOCKET_ERROR)
experimental-genesis 719 printf("ConnectSocket() : fcntl nonblocking setting failed, error %d\n", errno);
experimental-genesis 720 #endif
experimental-genesis 721
experimental-genesis 722 // Add node
experimental-genesis 723 CNode* pnode = new CNode(hSocket, addrConnect, false);
experimental-genesis 724 if (nTimeout != 0)
experimental-genesis 725 pnode->AddRef(nTimeout);
experimental-genesis 726 else
experimental-genesis 727 pnode->AddRef();
experimental-genesis 728 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 729 vNodes.push_back(pnode);
experimental-genesis 730
experimental-genesis 731 pnode->nTimeConnected = GetTime();
experimental-genesis 732 return pnode;
experimental-genesis 733 }
experimental-genesis 734 else
experimental-genesis 735 {
experimental-genesis 736 return NULL;
experimental-genesis 737 }
experimental-genesis 738 }
experimental-genesis 739
experimental-genesis 740 void CNode::CloseSocketDisconnect()
experimental-genesis 741 {
experimental-genesis 742 fDisconnect = true;
experimental-genesis 743 if (hSocket != INVALID_SOCKET)
experimental-genesis 744 {
experimental-genesis 745 if (fDebug)
experimental-genesis 746 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
experimental-genesis 747 printf("disconnecting node %s\n", addr.ToString().c_str());
experimental-genesis 748 closesocket(hSocket);
experimental-genesis 749 hSocket = INVALID_SOCKET;
experimental-genesis 750 }
experimental-genesis 751 }
experimental-genesis 752
experimental-genesis 753 void CNode::Cleanup()
experimental-genesis 754 {
experimental-genesis 755 // All of a nodes broadcasts and subscriptions are automatically torn down
experimental-genesis 756 // when it goes down, so a node has to stay up to keep its broadcast going.
experimental-genesis 757
experimental-genesis 758 // Cancel subscriptions
experimental-genesis 759 for (unsigned int nChannel = 0; nChannel < vfSubscribe.size(); nChannel++)
experimental-genesis 760 if (vfSubscribe[nChannel])
experimental-genesis 761 CancelSubscribe(nChannel);
experimental-genesis 762 }
experimental-genesis 763
experimental-genesis 764
experimental-genesis 765 std::map<unsigned int, int64> CNode::setBanned;
experimental-genesis 766 CCriticalSection CNode::cs_setBanned;
experimental-genesis 767
experimental-genesis 768 void CNode::ClearBanned()
experimental-genesis 769 {
experimental-genesis 770 setBanned.clear();
experimental-genesis 771 }
experimental-genesis 772
experimental-genesis 773 bool CNode::IsBanned(unsigned int ip)
experimental-genesis 774 {
experimental-genesis 775 bool fResult = false;
experimental-genesis 776 CRITICAL_BLOCK(cs_setBanned)
experimental-genesis 777 {
experimental-genesis 778 std::map<unsigned int, int64>::iterator i = setBanned.find(ip);
experimental-genesis 779 if (i != setBanned.end())
experimental-genesis 780 {
experimental-genesis 781 int64 t = (*i).second;
experimental-genesis 782 if (GetTime() < t)
experimental-genesis 783 fResult = true;
experimental-genesis 784 }
experimental-genesis 785 }
experimental-genesis 786 return fResult;
experimental-genesis 787 }
experimental-genesis 788
experimental-genesis 789 bool CNode::Misbehaving(int howmuch)
experimental-genesis 790 {
experimental-genesis 791 if (addr.IsLocal())
experimental-genesis 792 {
experimental-genesis 793 printf("Warning: local node %s misbehaving\n", addr.ToString().c_str());
experimental-genesis 794 return false;
experimental-genesis 795 }
experimental-genesis 796
experimental-genesis 797 nMisbehavior += howmuch;
experimental-genesis 798 if (nMisbehavior >= GetArg("-banscore", 100))
experimental-genesis 799 {
experimental-genesis 800 int64 banTime = GetTime()+GetArg("-bantime", 60*60*24); // Default 24-hour ban
experimental-genesis 801 CRITICAL_BLOCK(cs_setBanned)
experimental-genesis 802 if (setBanned[addr.ip] < banTime)
experimental-genesis 803 setBanned[addr.ip] = banTime;
experimental-genesis 804 CloseSocketDisconnect();
experimental-genesis 805 printf("Disconnected %s for misbehavior (score=%d)\n", addr.ToString().c_str(), nMisbehavior);
experimental-genesis 806 return true;
experimental-genesis 807 }
experimental-genesis 808 return false;
experimental-genesis 809 }
experimental-genesis 810
experimental-genesis 811
experimental-genesis 812
experimental-genesis 813
experimental-genesis 814
experimental-genesis 815
experimental-genesis 816
experimental-genesis 817
experimental-genesis 818
experimental-genesis 819
experimental-genesis 820
experimental-genesis 821
experimental-genesis 822 void ThreadSocketHandler(void* parg)
experimental-genesis 823 {
experimental-genesis 824 IMPLEMENT_RANDOMIZE_STACK(ThreadSocketHandler(parg));
experimental-genesis 825 try
experimental-genesis 826 {
experimental-genesis 827 vnThreadsRunning[0]++;
experimental-genesis 828 ThreadSocketHandler2(parg);
experimental-genesis 829 vnThreadsRunning[0]--;
experimental-genesis 830 }
experimental-genesis 831 catch (std::exception& e) {
experimental-genesis 832 vnThreadsRunning[0]--;
experimental-genesis 833 PrintException(&e, "ThreadSocketHandler()");
experimental-genesis 834 } catch (...) {
experimental-genesis 835 vnThreadsRunning[0]--;
experimental-genesis 836 throw; // support pthread_cancel()
experimental-genesis 837 }
experimental-genesis 838 printf("ThreadSocketHandler exiting\n");
experimental-genesis 839 }
experimental-genesis 840
experimental-genesis 841 void ThreadSocketHandler2(void* parg)
experimental-genesis 842 {
experimental-genesis 843 printf("ThreadSocketHandler started\n");
experimental-genesis 844 list<CNode*> vNodesDisconnected;
experimental-genesis 845 int nPrevNodeCount = 0;
experimental-genesis 846
experimental-genesis 847 loop
experimental-genesis 848 {
experimental-genesis 849 //
experimental-genesis 850 // Disconnect nodes
experimental-genesis 851 //
experimental-genesis 852 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 853 {
experimental-genesis 854 // Disconnect unused nodes
experimental-genesis 855 vector<CNode*> vNodesCopy = vNodes;
experimental-genesis 856 BOOST_FOREACH(CNode* pnode, vNodesCopy)
experimental-genesis 857 {
experimental-genesis 858 if (pnode->fDisconnect ||
experimental-genesis 859 (pnode->GetRefCount() <= 0 && pnode->vRecv.empty() && pnode->vSend.empty()))
experimental-genesis 860 {
experimental-genesis 861 // remove from vNodes
experimental-genesis 862 vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
experimental-genesis 863
experimental-genesis 864 // close socket and cleanup
experimental-genesis 865 pnode->CloseSocketDisconnect();
experimental-genesis 866 pnode->Cleanup();
experimental-genesis 867
experimental-genesis 868 // hold in disconnected pool until all refs are released
experimental-genesis 869 pnode->nReleaseTime = max(pnode->nReleaseTime, GetTime() + 15 * 60);
experimental-genesis 870 if (pnode->fNetworkNode || pnode->fInbound)
experimental-genesis 871 pnode->Release();
experimental-genesis 872 vNodesDisconnected.push_back(pnode);
experimental-genesis 873 }
experimental-genesis 874 }
experimental-genesis 875
experimental-genesis 876 // Delete disconnected nodes
experimental-genesis 877 list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
experimental-genesis 878 BOOST_FOREACH(CNode* pnode, vNodesDisconnectedCopy)
experimental-genesis 879 {
experimental-genesis 880 // wait until threads are done using it
experimental-genesis 881 if (pnode->GetRefCount() <= 0)
experimental-genesis 882 {
experimental-genesis 883 bool fDelete = false;
experimental-genesis 884 TRY_CRITICAL_BLOCK(pnode->cs_vSend)
experimental-genesis 885 TRY_CRITICAL_BLOCK(pnode->cs_vRecv)
experimental-genesis 886 TRY_CRITICAL_BLOCK(pnode->cs_mapRequests)
experimental-genesis 887 TRY_CRITICAL_BLOCK(pnode->cs_inventory)
experimental-genesis 888 fDelete = true;
experimental-genesis 889 if (fDelete)
experimental-genesis 890 {
experimental-genesis 891 vNodesDisconnected.remove(pnode);
experimental-genesis 892 delete pnode;
experimental-genesis 893 }
experimental-genesis 894 }
experimental-genesis 895 }
experimental-genesis 896 }
experimental-genesis 897 if (vNodes.size() != nPrevNodeCount)
experimental-genesis 898 {
experimental-genesis 899 nPrevNodeCount = vNodes.size();
experimental-genesis 900 MainFrameRepaint();
experimental-genesis 901 }
experimental-genesis 902
experimental-genesis 903
experimental-genesis 904 //
experimental-genesis 905 // Find which sockets have data to receive
experimental-genesis 906 //
experimental-genesis 907 struct timeval timeout;
experimental-genesis 908 timeout.tv_sec = 0;
experimental-genesis 909 timeout.tv_usec = 50000; // frequency to poll pnode->vSend
experimental-genesis 910
experimental-genesis 911 fd_set fdsetRecv;
experimental-genesis 912 fd_set fdsetSend;
experimental-genesis 913 fd_set fdsetError;
experimental-genesis 914 FD_ZERO(&fdsetRecv);
experimental-genesis 915 FD_ZERO(&fdsetSend);
experimental-genesis 916 FD_ZERO(&fdsetError);
experimental-genesis 917 SOCKET hSocketMax = 0;
experimental-genesis 918
experimental-genesis 919 if(hListenSocket != INVALID_SOCKET)
experimental-genesis 920 FD_SET(hListenSocket, &fdsetRecv);
experimental-genesis 921 hSocketMax = max(hSocketMax, hListenSocket);
experimental-genesis 922 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 923 {
experimental-genesis 924 BOOST_FOREACH(CNode* pnode, vNodes)
experimental-genesis 925 {
experimental-genesis 926 if (pnode->hSocket == INVALID_SOCKET)
experimental-genesis 927 continue;
experimental-genesis 928 FD_SET(pnode->hSocket, &fdsetRecv);
experimental-genesis 929 FD_SET(pnode->hSocket, &fdsetError);
experimental-genesis 930 hSocketMax = max(hSocketMax, pnode->hSocket);
experimental-genesis 931 TRY_CRITICAL_BLOCK(pnode->cs_vSend)
experimental-genesis 932 if (!pnode->vSend.empty())
experimental-genesis 933 FD_SET(pnode->hSocket, &fdsetSend);
experimental-genesis 934 }
experimental-genesis 935 }
experimental-genesis 936
experimental-genesis 937 vnThreadsRunning[0]--;
experimental-genesis 938 int nSelect = select(hSocketMax + 1, &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
experimental-genesis 939 vnThreadsRunning[0]++;
experimental-genesis 940 if (fShutdown)
experimental-genesis 941 return;
experimental-genesis 942 if (nSelect == SOCKET_ERROR)
experimental-genesis 943 {
experimental-genesis 944 int nErr = WSAGetLastError();
experimental-genesis 945 if (hSocketMax > -1)
experimental-genesis 946 {
experimental-genesis 947 printf("socket select error %d\n", nErr);
experimental-genesis 948 for (int i = 0; i <= hSocketMax; i++)
experimental-genesis 949 FD_SET(i, &fdsetRecv);
experimental-genesis 950 }
experimental-genesis 951 FD_ZERO(&fdsetSend);
experimental-genesis 952 FD_ZERO(&fdsetError);
experimental-genesis 953 Sleep(timeout.tv_usec/1000);
experimental-genesis 954 }
experimental-genesis 955
experimental-genesis 956
experimental-genesis 957 //
experimental-genesis 958 // Accept new connections
experimental-genesis 959 //
experimental-genesis 960 if (hListenSocket != INVALID_SOCKET && FD_ISSET(hListenSocket, &fdsetRecv))
experimental-genesis 961 {
experimental-genesis 962 struct sockaddr_in sockaddr;
experimental-genesis 963 socklen_t len = sizeof(sockaddr);
experimental-genesis 964 SOCKET hSocket = accept(hListenSocket, (struct sockaddr*)&sockaddr, &len);
experimental-genesis 965 CAddress addr;
experimental-genesis 966 int nInbound = 0;
experimental-genesis 967
experimental-genesis 968 if (hSocket != INVALID_SOCKET)
experimental-genesis 969 addr = CAddress(sockaddr);
experimental-genesis 970
experimental-genesis 971 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 972 BOOST_FOREACH(CNode* pnode, vNodes)
experimental-genesis 973 if (pnode->fInbound)
experimental-genesis 974 nInbound++;
experimental-genesis 975
experimental-genesis 976 if (hSocket == INVALID_SOCKET)
experimental-genesis 977 {
experimental-genesis 978 if (WSAGetLastError() != WSAEWOULDBLOCK)
experimental-genesis 979 printf("socket error accept failed: %d\n", WSAGetLastError());
experimental-genesis 980 }
experimental-genesis 981 else if (nInbound >= GetArg("-maxconnections", 125) - MAX_OUTBOUND_CONNECTIONS)
experimental-genesis 982 {
experimental-genesis 983 closesocket(hSocket);
experimental-genesis 984 }
experimental-genesis 985 else if (CNode::IsBanned(addr.ip))
experimental-genesis 986 {
experimental-genesis 987 printf("connection from %s dropped (banned)\n", addr.ToString().c_str());
experimental-genesis 988 closesocket(hSocket);
experimental-genesis 989 }
experimental-genesis 990 else
experimental-genesis 991 {
experimental-genesis 992 printf("accepted connection %s\n", addr.ToString().c_str());
experimental-genesis 993 CNode* pnode = new CNode(hSocket, addr, true);
experimental-genesis 994 pnode->AddRef();
experimental-genesis 995 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 996 vNodes.push_back(pnode);
experimental-genesis 997 }
experimental-genesis 998 }
experimental-genesis 999
experimental-genesis 1000
experimental-genesis 1001 //
experimental-genesis 1002 // Service each socket
experimental-genesis 1003 //
experimental-genesis 1004 vector<CNode*> vNodesCopy;
experimental-genesis 1005 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 1006 {
experimental-genesis 1007 vNodesCopy = vNodes;
experimental-genesis 1008 BOOST_FOREACH(CNode* pnode, vNodesCopy)
experimental-genesis 1009 pnode->AddRef();
experimental-genesis 1010 }
experimental-genesis 1011 BOOST_FOREACH(CNode* pnode, vNodesCopy)
experimental-genesis 1012 {
experimental-genesis 1013 if (fShutdown)
experimental-genesis 1014 return;
experimental-genesis 1015
experimental-genesis 1016 //
experimental-genesis 1017 // Receive
experimental-genesis 1018 //
experimental-genesis 1019 if (pnode->hSocket == INVALID_SOCKET)
experimental-genesis 1020 continue;
experimental-genesis 1021 if (FD_ISSET(pnode->hSocket, &fdsetRecv) || FD_ISSET(pnode->hSocket, &fdsetError))
experimental-genesis 1022 {
experimental-genesis 1023 TRY_CRITICAL_BLOCK(pnode->cs_vRecv)
experimental-genesis 1024 {
experimental-genesis 1025 CDataStream& vRecv = pnode->vRecv;
experimental-genesis 1026 unsigned int nPos = vRecv.size();
experimental-genesis 1027
experimental-genesis 1028 if (nPos > ReceiveBufferSize()) {
experimental-genesis 1029 if (!pnode->fDisconnect)
experimental-genesis 1030 printf("socket recv flood control disconnect (%d bytes)\n", vRecv.size());
experimental-genesis 1031 pnode->CloseSocketDisconnect();
experimental-genesis 1032 }
experimental-genesis 1033 else {
experimental-genesis 1034 // typical socket buffer is 8K-64K
experimental-genesis 1035 char pchBuf[0x10000];
experimental-genesis 1036 int nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
experimental-genesis 1037 if (nBytes > 0)
experimental-genesis 1038 {
experimental-genesis 1039 vRecv.resize(nPos + nBytes);
experimental-genesis 1040 memcpy(&vRecv[nPos], pchBuf, nBytes);
experimental-genesis 1041 pnode->nLastRecv = GetTime();
experimental-genesis 1042 }
experimental-genesis 1043 else if (nBytes == 0)
experimental-genesis 1044 {
experimental-genesis 1045 // socket closed gracefully
experimental-genesis 1046 if (!pnode->fDisconnect)
experimental-genesis 1047 printf("socket closed\n");
experimental-genesis 1048 pnode->CloseSocketDisconnect();
experimental-genesis 1049 }
experimental-genesis 1050 else if (nBytes < 0)
experimental-genesis 1051 {
experimental-genesis 1052 // error
experimental-genesis 1053 int nErr = WSAGetLastError();
experimental-genesis 1054 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
experimental-genesis 1055 {
experimental-genesis 1056 if (!pnode->fDisconnect)
experimental-genesis 1057 printf("socket recv error %d\n", nErr);
experimental-genesis 1058 pnode->CloseSocketDisconnect();
experimental-genesis 1059 }
experimental-genesis 1060 }
experimental-genesis 1061 }
experimental-genesis 1062 }
experimental-genesis 1063 }
experimental-genesis 1064
experimental-genesis 1065 //
experimental-genesis 1066 // Send
experimental-genesis 1067 //
experimental-genesis 1068 if (pnode->hSocket == INVALID_SOCKET)
experimental-genesis 1069 continue;
experimental-genesis 1070 if (FD_ISSET(pnode->hSocket, &fdsetSend))
experimental-genesis 1071 {
experimental-genesis 1072 TRY_CRITICAL_BLOCK(pnode->cs_vSend)
experimental-genesis 1073 {
experimental-genesis 1074 CDataStream& vSend = pnode->vSend;
experimental-genesis 1075 if (!vSend.empty())
experimental-genesis 1076 {
experimental-genesis 1077 int nBytes = send(pnode->hSocket, &vSend[0], vSend.size(), MSG_NOSIGNAL | MSG_DONTWAIT);
experimental-genesis 1078 if (nBytes > 0)
experimental-genesis 1079 {
experimental-genesis 1080 vSend.erase(vSend.begin(), vSend.begin() + nBytes);
experimental-genesis 1081 pnode->nLastSend = GetTime();
experimental-genesis 1082 }
experimental-genesis 1083 else if (nBytes < 0)
experimental-genesis 1084 {
experimental-genesis 1085 // error
experimental-genesis 1086 int nErr = WSAGetLastError();
experimental-genesis 1087 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
experimental-genesis 1088 {
experimental-genesis 1089 printf("socket send error %d\n", nErr);
experimental-genesis 1090 pnode->CloseSocketDisconnect();
experimental-genesis 1091 }
experimental-genesis 1092 }
experimental-genesis 1093 if (vSend.size() > SendBufferSize()) {
experimental-genesis 1094 if (!pnode->fDisconnect)
experimental-genesis 1095 printf("socket send flood control disconnect (%d bytes)\n", vSend.size());
experimental-genesis 1096 pnode->CloseSocketDisconnect();
experimental-genesis 1097 }
experimental-genesis 1098 }
experimental-genesis 1099 }
experimental-genesis 1100 }
experimental-genesis 1101
experimental-genesis 1102 //
experimental-genesis 1103 // Inactivity checking
experimental-genesis 1104 //
experimental-genesis 1105 if (pnode->vSend.empty())
experimental-genesis 1106 pnode->nLastSendEmpty = GetTime();
experimental-genesis 1107 if (GetTime() - pnode->nTimeConnected > 60)
experimental-genesis 1108 {
experimental-genesis 1109 if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
experimental-genesis 1110 {
experimental-genesis 1111 printf("socket no message in first 60 seconds, %d %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0);
experimental-genesis 1112 pnode->fDisconnect = true;
experimental-genesis 1113 }
experimental-genesis 1114 else if (GetTime() - pnode->nLastSend > 90*60 && GetTime() - pnode->nLastSendEmpty > 90*60)
experimental-genesis 1115 {
experimental-genesis 1116 printf("socket not sending\n");
experimental-genesis 1117 pnode->fDisconnect = true;
experimental-genesis 1118 }
experimental-genesis 1119 else if (GetTime() - pnode->nLastRecv > 90*60)
experimental-genesis 1120 {
experimental-genesis 1121 printf("socket inactivity timeout\n");
experimental-genesis 1122 pnode->fDisconnect = true;
experimental-genesis 1123 }
experimental-genesis 1124 }
experimental-genesis 1125 }
experimental-genesis 1126 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 1127 {
experimental-genesis 1128 BOOST_FOREACH(CNode* pnode, vNodesCopy)
experimental-genesis 1129 pnode->Release();
experimental-genesis 1130 }
experimental-genesis 1131
experimental-genesis 1132 Sleep(10);
experimental-genesis 1133 }
experimental-genesis 1134 }
experimental-genesis 1135
experimental-genesis 1136
experimental-genesis 1137
experimental-genesis 1138
experimental-genesis 1139
experimental-genesis 1140
experimental-genesis 1141
experimental-genesis 1142
experimental-genesis 1143
experimental-genesis 1144 #ifdef USE_UPNP
experimental-genesis 1145 void ThreadMapPort(void* parg)
experimental-genesis 1146 {
experimental-genesis 1147 IMPLEMENT_RANDOMIZE_STACK(ThreadMapPort(parg));
experimental-genesis 1148 try
experimental-genesis 1149 {
experimental-genesis 1150 vnThreadsRunning[5]++;
experimental-genesis 1151 ThreadMapPort2(parg);
experimental-genesis 1152 vnThreadsRunning[5]--;
experimental-genesis 1153 }
experimental-genesis 1154 catch (std::exception& e) {
experimental-genesis 1155 vnThreadsRunning[5]--;
experimental-genesis 1156 PrintException(&e, "ThreadMapPort()");
experimental-genesis 1157 } catch (...) {
experimental-genesis 1158 vnThreadsRunning[5]--;
experimental-genesis 1159 PrintException(NULL, "ThreadMapPort()");
experimental-genesis 1160 }
experimental-genesis 1161 printf("ThreadMapPort exiting\n");
experimental-genesis 1162 }
experimental-genesis 1163
experimental-genesis 1164 void ThreadMapPort2(void* parg)
experimental-genesis 1165 {
experimental-genesis 1166 printf("ThreadMapPort started\n");
experimental-genesis 1167
experimental-genesis 1168 char port[6];
experimental-genesis 1169 sprintf(port, "%d", GetListenPort());
experimental-genesis 1170
experimental-genesis 1171 const char * multicastif = 0;
experimental-genesis 1172 const char * minissdpdpath = 0;
experimental-genesis 1173 struct UPNPDev * devlist = 0;
experimental-genesis 1174 char lanaddr[64];
experimental-genesis 1175
experimental-genesis 1176 #ifndef UPNPDISCOVER_SUCCESS
experimental-genesis 1177 /* miniupnpc 1.5 */
experimental-genesis 1178 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
experimental-genesis 1179 #else
experimental-genesis 1180 /* miniupnpc 1.6 */
experimental-genesis 1181 int error = 0;
experimental-genesis 1182 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
experimental-genesis 1183 #endif
experimental-genesis 1184
experimental-genesis 1185 struct UPNPUrls urls;
experimental-genesis 1186 struct IGDdatas data;
experimental-genesis 1187 int r;
experimental-genesis 1188
experimental-genesis 1189 r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
experimental-genesis 1190 if (r == 1)
experimental-genesis 1191 {
experimental-genesis 1192 if (!addrLocalHost.IsRoutable())
experimental-genesis 1193 {
experimental-genesis 1194 char externalIPAddress[40];
experimental-genesis 1195 r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
experimental-genesis 1196 if(r != UPNPCOMMAND_SUCCESS)
experimental-genesis 1197 printf("UPnP: GetExternalIPAddress() returned %d\n", r);
experimental-genesis 1198 else
experimental-genesis 1199 {
experimental-genesis 1200 if(externalIPAddress[0])
experimental-genesis 1201 {
experimental-genesis 1202 printf("UPnP: ExternalIPAddress = %s\n", externalIPAddress);
experimental-genesis 1203 CAddress addrExternalFromUPnP(externalIPAddress, 0, false, nLocalServices);
experimental-genesis 1204 if (addrExternalFromUPnP.IsRoutable())
experimental-genesis 1205 addrLocalHost = addrExternalFromUPnP;
experimental-genesis 1206 }
experimental-genesis 1207 else
experimental-genesis 1208 printf("UPnP: GetExternalIPAddress failed.\n");
experimental-genesis 1209 }
experimental-genesis 1210 }
experimental-genesis 1211
experimental-genesis 1212 string strDesc = "Bitcoin " + FormatFullVersion();
experimental-genesis 1213 #ifndef UPNPDISCOVER_SUCCESS
experimental-genesis 1214 /* miniupnpc 1.5 */
experimental-genesis 1215 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
experimental-genesis 1216 port, port, lanaddr, strDesc.c_str(), "TCP", 0);
experimental-genesis 1217 #else
experimental-genesis 1218 /* miniupnpc 1.6 */
experimental-genesis 1219 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
experimental-genesis 1220 port, port, lanaddr, strDesc.c_str(), "TCP", 0, "0");
experimental-genesis 1221 #endif
experimental-genesis 1222
experimental-genesis 1223 if(r!=UPNPCOMMAND_SUCCESS)
experimental-genesis 1224 printf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
experimental-genesis 1225 port, port, lanaddr, r, strupnperror(r));
experimental-genesis 1226 else
experimental-genesis 1227 printf("UPnP Port Mapping successful.\n");
experimental-genesis 1228 int i = 1;
experimental-genesis 1229 loop {
experimental-genesis 1230 if (fShutdown || !fUseUPnP)
experimental-genesis 1231 {
experimental-genesis 1232 r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port, "TCP", 0);
experimental-genesis 1233 printf("UPNP_DeletePortMapping() returned : %d\n", r);
experimental-genesis 1234 freeUPNPDevlist(devlist); devlist = 0;
experimental-genesis 1235 FreeUPNPUrls(&urls);
experimental-genesis 1236 return;
experimental-genesis 1237 }
experimental-genesis 1238 if (i % 600 == 0) // Refresh every 20 minutes
experimental-genesis 1239 {
experimental-genesis 1240 #ifndef UPNPDISCOVER_SUCCESS
experimental-genesis 1241 /* miniupnpc 1.5 */
experimental-genesis 1242 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
experimental-genesis 1243 port, port, lanaddr, strDesc.c_str(), "TCP", 0);
experimental-genesis 1244 #else
experimental-genesis 1245 /* miniupnpc 1.6 */
experimental-genesis 1246 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
experimental-genesis 1247 port, port, lanaddr, strDesc.c_str(), "TCP", 0, "0");
experimental-genesis 1248 #endif
experimental-genesis 1249
experimental-genesis 1250 if(r!=UPNPCOMMAND_SUCCESS)
experimental-genesis 1251 printf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
experimental-genesis 1252 port, port, lanaddr, r, strupnperror(r));
experimental-genesis 1253 else
experimental-genesis 1254 printf("UPnP Port Mapping successful.\n");;
experimental-genesis 1255 }
experimental-genesis 1256 Sleep(2000);
experimental-genesis 1257 i++;
experimental-genesis 1258 }
experimental-genesis 1259 } else {
experimental-genesis 1260 printf("No valid UPnP IGDs found\n");
experimental-genesis 1261 freeUPNPDevlist(devlist); devlist = 0;
experimental-genesis 1262 if (r != 0)
experimental-genesis 1263 FreeUPNPUrls(&urls);
experimental-genesis 1264 loop {
experimental-genesis 1265 if (fShutdown || !fUseUPnP)
experimental-genesis 1266 return;
experimental-genesis 1267 Sleep(2000);
experimental-genesis 1268 }
experimental-genesis 1269 }
experimental-genesis 1270 }
experimental-genesis 1271
experimental-genesis 1272 void MapPort(bool fMapPort)
experimental-genesis 1273 {
experimental-genesis 1274 if (fUseUPnP != fMapPort)
experimental-genesis 1275 {
experimental-genesis 1276 fUseUPnP = fMapPort;
experimental-genesis 1277 WriteSetting("fUseUPnP", fUseUPnP);
experimental-genesis 1278 }
experimental-genesis 1279 if (fUseUPnP && vnThreadsRunning[5] < 1)
experimental-genesis 1280 {
experimental-genesis 1281 if (!CreateThread(ThreadMapPort, NULL))
experimental-genesis 1282 printf("Error: ThreadMapPort(ThreadMapPort) failed\n");
experimental-genesis 1283 }
experimental-genesis 1284 }
experimental-genesis 1285 #else
experimental-genesis 1286 void MapPort(bool /* unused fMapPort */)
experimental-genesis 1287 {
experimental-genesis 1288 // Intentionally left blank.
experimental-genesis 1289 }
experimental-genesis 1290 #endif
experimental-genesis 1291
experimental-genesis 1292
experimental-genesis 1293
experimental-genesis 1294
experimental-genesis 1295
experimental-genesis 1296
experimental-genesis 1297
experimental-genesis 1298
experimental-genesis 1299
experimental-genesis 1300
experimental-genesis 1301 static const char *strDNSSeed[] = {
experimental-genesis 1302 "bitseed.xf2.org",
experimental-genesis 1303 "dnsseed.bluematt.me",
experimental-genesis 1304 "seed.bitcoin.sipa.be",
experimental-genesis 1305 "dnsseed.bitcoin.dashjr.org",
experimental-genesis 1306 };
experimental-genesis 1307
experimental-genesis 1308 void ThreadDNSAddressSeed(void* parg)
experimental-genesis 1309 {
experimental-genesis 1310 IMPLEMENT_RANDOMIZE_STACK(ThreadDNSAddressSeed(parg));
experimental-genesis 1311 try
experimental-genesis 1312 {
experimental-genesis 1313 vnThreadsRunning[6]++;
experimental-genesis 1314 ThreadDNSAddressSeed2(parg);
experimental-genesis 1315 vnThreadsRunning[6]--;
experimental-genesis 1316 }
experimental-genesis 1317 catch (std::exception& e) {
experimental-genesis 1318 vnThreadsRunning[6]--;
experimental-genesis 1319 PrintException(&e, "ThreadDNSAddressSeed()");
experimental-genesis 1320 } catch (...) {
experimental-genesis 1321 vnThreadsRunning[6]--;
experimental-genesis 1322 throw; // support pthread_cancel()
experimental-genesis 1323 }
experimental-genesis 1324 printf("ThreadDNSAddressSeed exiting\n");
experimental-genesis 1325 }
experimental-genesis 1326
experimental-genesis 1327 void ThreadDNSAddressSeed2(void* parg)
experimental-genesis 1328 {
experimental-genesis 1329 printf("ThreadDNSAddressSeed started\n");
experimental-genesis 1330 int found = 0;
experimental-genesis 1331
experimental-genesis 1332 if (!fTestNet)
experimental-genesis 1333 {
experimental-genesis 1334 printf("Loading addresses from DNS seeds (could take a while)\n");
experimental-genesis 1335
experimental-genesis 1336 for (int seed_idx = 0; seed_idx < ARRAYLEN(strDNSSeed); seed_idx++) {
experimental-genesis 1337 vector<CAddress> vaddr;
experimental-genesis 1338 if (Lookup(strDNSSeed[seed_idx], vaddr, NODE_NETWORK, -1, true))
experimental-genesis 1339 {
experimental-genesis 1340 CAddrDB addrDB;
experimental-genesis 1341 addrDB.TxnBegin();
experimental-genesis 1342 BOOST_FOREACH (CAddress& addr, vaddr)
experimental-genesis 1343 {
experimental-genesis 1344 if (addr.GetByte(3) != 127)
experimental-genesis 1345 {
experimental-genesis 1346 addr.nTime = 0;
experimental-genesis 1347 AddAddress(addr, 0, &addrDB);
experimental-genesis 1348 found++;
experimental-genesis 1349 }
experimental-genesis 1350 }
experimental-genesis 1351 addrDB.TxnCommit(); // Save addresses (it's ok if this fails)
experimental-genesis 1352 }
experimental-genesis 1353 }
experimental-genesis 1354 }
experimental-genesis 1355
experimental-genesis 1356 printf("%d addresses found from DNS seeds\n", found);
experimental-genesis 1357 }
experimental-genesis 1358
experimental-genesis 1359
experimental-genesis 1360
experimental-genesis 1361
experimental-genesis 1362
experimental-genesis 1363
experimental-genesis 1364
experimental-genesis 1365
experimental-genesis 1366
experimental-genesis 1367
experimental-genesis 1368
experimental-genesis 1369
experimental-genesis 1370 unsigned int pnSeed[] =
experimental-genesis 1371 {
experimental-genesis 1372 0x959bd347, 0xf8de42b2, 0x73bc0518, 0xea6edc50, 0x21b00a4d, 0xc725b43d, 0xd665464d, 0x1a2a770e,
experimental-genesis 1373 0x27c93946, 0x65b2fa46, 0xb80ae255, 0x66b3b446, 0xb1877a3e, 0x6ee89e3e, 0xc3175b40, 0x2a01a83c,
experimental-genesis 1374 0x95b1363a, 0xa079ad3d, 0xe6ca801f, 0x027f4f4a, 0x34f7f03a, 0xf790f04a, 0x16ca801f, 0x2f4d5e40,
experimental-genesis 1375 0x3a4d5e40, 0xc43a322e, 0xc8159753, 0x14d4724c, 0x7919a118, 0xe0bdb34e, 0x68a16b2e, 0xff64b44d,
experimental-genesis 1376 0x6099115b, 0x9b57b05b, 0x7bd1b4ad, 0xdf95944f, 0x29d2b73d, 0xafa8db79, 0xe247ba41, 0x24078348,
experimental-genesis 1377 0xf722f03c, 0x33567ebc, 0xace64ed4, 0x984d3932, 0xb5f34e55, 0x27b7024d, 0x94579247, 0x8894042e,
experimental-genesis 1378 0x9357d34c, 0x1063c24b, 0xcaa228b1, 0xa3c5a8b2, 0x5dc64857, 0xa2c23643, 0xa8369a54, 0x31203077,
experimental-genesis 1379 0x00707c5c, 0x09fc0b3a, 0x272e9e2e, 0xf80f043e, 0x9449ca3e, 0x5512c33e, 0xd106b555, 0xe8024157,
experimental-genesis 1380 0xe288ec29, 0xc79c5461, 0xafb63932, 0xdb02ab4b, 0x0e512777, 0x8a145a4c, 0xb201ff4f, 0x5e09314b,
experimental-genesis 1381 0xcd9bfbcd, 0x1c023765, 0x4394e75c, 0xa728bd4d, 0x65331552, 0xa98420b1, 0x89ecf559, 0x6e80801f,
experimental-genesis 1382 0xf404f118, 0xefd62b51, 0x05918346, 0x9b186d5f, 0xacabab46, 0xf912e255, 0xc188ea62, 0xcc55734e,
experimental-genesis 1383 0xc668064d, 0xd77a4558, 0x46201c55, 0xf17dfc80, 0xf7142f2e, 0x87bfb718, 0x8aa54fb2, 0xc451d518,
experimental-genesis 1384 0xc4ae8831, 0x8dd44d55, 0x5bbd206c, 0x64536b5d, 0x5c667e60, 0x3b064242, 0xfe963a42, 0xa28e6dc8,
experimental-genesis 1385 0xe8a9604a, 0xc989464e, 0xd124a659, 0x50065140, 0xa44dfe5e, 0x1079e655, 0x3fb986d5, 0x47895b18,
experimental-genesis 1386 0x7d3ce4ad, 0x4561ba50, 0x296eec62, 0x255b41ad, 0xaed35ec9, 0x55556f12, 0xc7d3154d, 0x3297b65d,
experimental-genesis 1387 0x8930121f, 0xabf42e4e, 0x4a29e044, 0x1212685d, 0x676c1e40, 0xce009744, 0x383a8948, 0xa2dbd0ad,
experimental-genesis 1388 0xecc2564d, 0x07dbc252, 0x887ee24b, 0x5171644c, 0x6bb798c1, 0x847f495d, 0x4cbb7145, 0x3bb81c32,
experimental-genesis 1389 0x45eb262e, 0xc8015a4e, 0x250a361b, 0xf694f946, 0xd64a183e, 0xd4f1dd59, 0x8f20ffd4, 0x51d9e55c,
experimental-genesis 1390 0x09521763, 0x5e02002e, 0x32c8074d, 0xe685762e, 0x8290b0bc, 0x762a922e, 0xfc5ee754, 0x83a24829,
experimental-genesis 1391 0x775b224d, 0x6295bb4d, 0x38ec0555, 0xbffbba50, 0xe5560260, 0x86b16a7c, 0xd372234e, 0x49a3c24b,
experimental-genesis 1392 0x2f6a171f, 0x4d75ed60, 0xae94115b, 0xcb543744, 0x63080c59, 0x3f9c724c, 0xc977ce18, 0x532efb18,
experimental-genesis 1393 0x69dc3b2e, 0x5f94d929, 0x1732bb4d, 0x9c814b4d, 0xe6b3762e, 0xc024f662, 0x8face35b, 0x6b5b044d,
experimental-genesis 1394 0x798c7b57, 0x79a6b44c, 0x067d3057, 0xf9e94e5f, 0x91cbe15b, 0x71405eb2, 0x2662234e, 0xcbcc4a6d,
experimental-genesis 1395 0xbf69d54b, 0xa79b4e55, 0xec6d3e51, 0x7c0b3c02, 0x60f83653, 0x24c1e15c, 0x1110b62e, 0x10350f59,
experimental-genesis 1396 0xa56f1d55, 0x3509e7a9, 0xeb128354, 0x14268e2e, 0x934e28bc, 0x8e32692e, 0x8331a21f, 0x3e633932,
experimental-genesis 1397 0xc812b12e, 0xc684bf2e, 0x80112d2e, 0xe0ddc96c, 0xc630ca4a, 0x5c09b3b2, 0x0b580518, 0xc8e9d54b,
experimental-genesis 1398 0xd169aa43, 0x17d0d655, 0x1d029963, 0x7ff87559, 0xcb701f1f, 0x6fa3e85d, 0xe45e9a54, 0xf05d1802,
experimental-genesis 1399 0x44d03b2e, 0x837b692e, 0xccd4354e, 0x3d6da13c, 0x3423084d, 0xf707c34a, 0x55f6db3a, 0xad26e442,
experimental-genesis 1400 0x6233a21f, 0x09e80e59, 0x8caeb54d, 0xbe870941, 0xb407d20e, 0x20b51018, 0x56fb152e, 0x460d2a4e,
experimental-genesis 1401 0xbb9a2946, 0x560eb12e, 0xed83dd29, 0xd6724f53, 0xa50aafb8, 0x451346d9, 0x88348e2e, 0x7312fead,
experimental-genesis 1402 0x8ecaf96f, 0x1bda4e5f, 0xf1671e40, 0x3c8c3e3b, 0x4716324d, 0xdde24ede, 0xf98cd17d, 0xa91d4644,
experimental-genesis 1403 0x28124eb2, 0x147d5129, 0xd022042e, 0x61733d3b, 0xad0d5e02, 0x8ce2932e, 0xe5c18502, 0x549c1e32,
experimental-genesis 1404 0x9685801f, 0x86e217ad, 0xd948214b, 0x4110f462, 0x3a2e894e, 0xbd35492e, 0x87e0d558, 0x64b8ef7d,
experimental-genesis 1405 0x7c3eb962, 0x72a84b3e, 0x7cd667c9, 0x28370a2e, 0x4bc60e7b, 0x6fc1ec60, 0x14a6983f, 0x86739a4b,
experimental-genesis 1406 0x46954e5f, 0x32e2e15c, 0x2e9326cf, 0xe5801c5e, 0x379607b2, 0x32151145, 0xf0e39744, 0xacb54c55,
experimental-genesis 1407 0xa37dfb60, 0x83b55cc9, 0x388f7ca5, 0x15034f5f, 0x3e94965b, 0x68e0ffad, 0x35280f59, 0x8fe190cf,
experimental-genesis 1408 0x7c6ba5b2, 0xa5e9db43, 0x4ee1fc60, 0xd9d94e5f, 0x04040677, 0x0ea9b35e, 0x5961f14f, 0x67fda063,
experimental-genesis 1409 0xa48a5a31, 0xc6524e55, 0x283d325e, 0x3f37515f, 0x96b94b3e, 0xacce620e, 0x6481cc5b, 0xa4a06d4b,
experimental-genesis 1410 0x9e95d2d9, 0xe40c03d5, 0xc2f4514b, 0xb79aad44, 0xf64be843, 0xb2064070, 0xfca00455, 0x429dfa4e,
experimental-genesis 1411 0x2323f173, 0xeda4185e, 0xabd5227d, 0x9efd4d58, 0xb1104758, 0x4811e955, 0xbd9ab355, 0xe921f44b,
experimental-genesis 1412 0x9f166dce, 0x09e279b2, 0xe0c9ac7b, 0x7901a5ad, 0xa145d4b0, 0x79104671, 0xec31e35a, 0x4fe0b555,
experimental-genesis 1413 0xc7d9cbad, 0xad057f55, 0xe94cc759, 0x7fe0b043, 0xe4529f2e, 0x0d4dd4b2, 0x9f11a54d, 0x031e2e4e,
experimental-genesis 1414 0xe6014f5f, 0x11d1ca6c, 0x26bd7f61, 0xeb86854f, 0x4d347b57, 0x116bbe2e, 0xdba7234e, 0x7bcbfd2e,
experimental-genesis 1415 0x174dd4b2, 0x6686762e, 0xb089ba50, 0xc6258246, 0x087e767b, 0xc4a8cb4a, 0x595dba50, 0x7f0ae502,
experimental-genesis 1416 0x7b1dbd5a, 0xa0603492, 0x57d1af4b, 0x9e21ffd4, 0x6393064d, 0x7407376e, 0xe484762e, 0x122a4e53,
experimental-genesis 1417 0x4a37aa43, 0x3888a6be, 0xee77864e, 0x039c8dd5, 0x688d89af, 0x0e988f62, 0x08218246, 0xfc2f8246,
experimental-genesis 1418 0xd1d97040, 0xd64cd4b2, 0x5ae4a6b8, 0x7d0de9bc, 0x8d304d61, 0x06c5c672, 0xa4c8bd4d, 0xe0fd373b,
experimental-genesis 1419 0x575ebe4d, 0x72d26277, 0x55570f55, 0x77b154d9, 0xe214293a, 0xfc740f4b, 0xfe3f6a57, 0xa9c55f02,
experimental-genesis 1420 0xae4054db, 0x2394d918, 0xb511b24a, 0xb8741ab2, 0x0758e65e, 0xc7b5795b, 0xb0a30a4c, 0xaf7f170c,
experimental-genesis 1421 0xf3b4762e, 0x8179576d, 0x738a1581, 0x4b95b64c, 0x9829b618, 0x1bea932e, 0x7bdeaa4b, 0xcb5e0281,
experimental-genesis 1422 0x65618f54, 0x0658474b, 0x27066acf, 0x40556d65, 0x7d204d53, 0xf28bc244, 0xdce23455, 0xadc0ff54,
experimental-genesis 1423 0x3863c948, 0xcee34e5f, 0xdeb85e02, 0x2ed17a61, 0x6a7b094d, 0x7f0cfc40, 0x59603f54, 0x3220afbc,
experimental-genesis 1424 0xb5dfd962, 0x125d21c0, 0x13f8d243, 0xacfefb4e, 0x86c2c147, 0x3d8bbd59, 0xbd02a21f, 0x2593042e,
experimental-genesis 1425 0xc6a17a7c, 0x28925861, 0xb487ed44, 0xb5f4fd6d, 0x90c28a45, 0x5a14f74d, 0x43d71b4c, 0x728ebb5d,
experimental-genesis 1426 0x885bf950, 0x08134dd0, 0x38ec046e, 0xc575684b, 0x50082d2e, 0xa2f47757, 0x270f86ae, 0xf3ff6462,
experimental-genesis 1427 0x10ed3f4e, 0x4b58d462, 0xe01ce23e, 0x8c5b092e, 0x63e52f4e, 0x22c1e85d, 0xa908f54e, 0x8591624f,
experimental-genesis 1428 0x2c0fb94e, 0xa280ba3c, 0xb6f41b4c, 0x24f9aa47, 0x27201647, 0x3a3ea6dc, 0xa14fc3be, 0x3c34bdd5,
experimental-genesis 1429 0x5b8d4f5b, 0xaadeaf4b, 0xc71cab50, 0x15697a4c, 0x9a1a734c, 0x2a037d81, 0x2590bd59, 0x48ec2741,
experimental-genesis 1430 0x53489c5b, 0x7f00314b, 0x2170d362, 0xf2e92542, 0x42c10b44, 0x98f0f118, 0x883a3456, 0x099a932e,
experimental-genesis 1431 0xea38f7bc, 0x644e9247, 0xbb61b62e, 0x30e0863d, 0x5f51be54, 0x207215c7, 0x5f306c45, 0xaa7f3932,
experimental-genesis 1432 0x98da7d45, 0x4e339b59, 0x2e411581, 0xa808f618, 0xad2c0c59, 0x54476741, 0x09e99fd1, 0x5db8f752,
experimental-genesis 1433 0xc16df8bd, 0x1dd4b44f, 0x106edf2e, 0x9e15c180, 0x2ad6b56f, 0x633a5332, 0xff33787c, 0x077cb545,
experimental-genesis 1434 0x6610be6d, 0x75aad2c4, 0x72fb4d5b, 0xe81e0f59, 0x576f6332, 0x47333373, 0x351ed783, 0x2d90fb50,
experimental-genesis 1435 0x8d5e0f6c, 0x5b27a552, 0xdb293ebb, 0xe55ef950, 0x4b133ad8, 0x75df975a, 0x7b6a8740, 0xa899464b,
experimental-genesis 1436 0xfab15161, 0x10f8b64d, 0xd055ea4d, 0xee8e146b, 0x4b14afb8, 0x4bc1c44a, 0x9b961dcc, 0xd111ff43,
experimental-genesis 1437 0xfca0b745, 0xc800e412, 0x0afad9d1, 0xf751c350, 0xf9f0cccf, 0xa290a545, 0x8ef13763, 0x7ec70d59,
experimental-genesis 1438 0x2b066acf, 0x65496c45, 0xade02c1b, 0xae6eb077, 0x92c1e65b, 0xc064e6a9, 0xc649e56d, 0x5287a243,
experimental-genesis 1439 0x36de4f5b, 0x5b1df6ad, 0x65c39a59, 0xdba805b2, 0x20067aa8, 0x6457e56d, 0x3cee26cf, 0xfd3ff26d,
experimental-genesis 1440 0x04f86d4a, 0x06b8e048, 0xa93bcd5c, 0x91135852, 0xbe90a643, 0x8fa0094d, 0x06d8215f, 0x2677094d,
experimental-genesis 1441 0xd735685c, 0x164a00c9, 0x5209ac5f, 0xa9564c5c, 0x3b504f5f, 0xcc826bd0, 0x4615042e, 0x5fe13b4a,
experimental-genesis 1442 0x8c81b86d, 0x879ab68c, 0x1de564b8, 0x434487d8, 0x2dcb1b63, 0x82ab524a, 0xb0676abb, 0xa13d9c62,
experimental-genesis 1443 0xdbb5b86d, 0x5b7f4b59, 0xaddfb44d, 0xad773532, 0x3997054c, 0x72cebd89, 0xb194544c, 0xc5b8046e,
experimental-genesis 1444 0x6e1adeb2, 0xaa5abb51, 0xefb54b44, 0x15efc54f, 0xe9f1bc4d, 0x5f401b6c, 0x97f018ad, 0xc82f9252,
experimental-genesis 1445 0x2cdc762e, 0x8e52e56d, 0x1827175e, 0x9b7d7d80, 0xb2ad6845, 0x51065140, 0x71180a18, 0x5b27006c,
experimental-genesis 1446 0x0621e255, 0x721cbe58, 0x670c0cb8, 0xf8bd715d, 0xe0bdc5d9, 0xed843501, 0x4b84554d, 0x7f1a18bc,
experimental-genesis 1447 0x53bcaf47, 0x5729d35f, 0xf0dda246, 0x22382bd0, 0x4d641fb0, 0x316afcde, 0x50a22f1f, 0x73608046,
experimental-genesis 1448 0xc461d84a, 0xb2dbe247,
experimental-genesis 1449 };
experimental-genesis 1450
experimental-genesis 1451
experimental-genesis 1452
experimental-genesis 1453 void ThreadOpenConnections(void* parg)
experimental-genesis 1454 {
experimental-genesis 1455 IMPLEMENT_RANDOMIZE_STACK(ThreadOpenConnections(parg));
experimental-genesis 1456 try
experimental-genesis 1457 {
experimental-genesis 1458 vnThreadsRunning[1]++;
experimental-genesis 1459 ThreadOpenConnections2(parg);
experimental-genesis 1460 vnThreadsRunning[1]--;
experimental-genesis 1461 }
experimental-genesis 1462 catch (std::exception& e) {
experimental-genesis 1463 vnThreadsRunning[1]--;
experimental-genesis 1464 PrintException(&e, "ThreadOpenConnections()");
experimental-genesis 1465 } catch (...) {
experimental-genesis 1466 vnThreadsRunning[1]--;
experimental-genesis 1467 PrintException(NULL, "ThreadOpenConnections()");
experimental-genesis 1468 }
experimental-genesis 1469 printf("ThreadOpenConnections exiting\n");
experimental-genesis 1470 }
experimental-genesis 1471
experimental-genesis 1472 void ThreadOpenConnections2(void* parg)
experimental-genesis 1473 {
experimental-genesis 1474 printf("ThreadOpenConnections started\n");
experimental-genesis 1475
experimental-genesis 1476 // Connect to specific addresses
experimental-genesis 1477 if (mapArgs.count("-connect"))
experimental-genesis 1478 {
experimental-genesis 1479 for (int64 nLoop = 0;; nLoop++)
experimental-genesis 1480 {
experimental-genesis 1481 BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"])
experimental-genesis 1482 {
experimental-genesis 1483 CAddress addr(strAddr, fAllowDNS);
experimental-genesis 1484 if (addr.IsValid())
experimental-genesis 1485 OpenNetworkConnection(addr);
experimental-genesis 1486 for (int i = 0; i < 10 && i < nLoop; i++)
experimental-genesis 1487 {
experimental-genesis 1488 Sleep(500);
experimental-genesis 1489 if (fShutdown)
experimental-genesis 1490 return;
experimental-genesis 1491 }
experimental-genesis 1492 }
experimental-genesis 1493 }
experimental-genesis 1494 }
experimental-genesis 1495
experimental-genesis 1496 // Connect to manually added nodes first
experimental-genesis 1497 if (mapArgs.count("-addnode"))
experimental-genesis 1498 {
experimental-genesis 1499 BOOST_FOREACH(string strAddr, mapMultiArgs["-addnode"])
experimental-genesis 1500 {
experimental-genesis 1501 CAddress addr(strAddr, fAllowDNS);
experimental-genesis 1502 if (addr.IsValid())
experimental-genesis 1503 {
experimental-genesis 1504 OpenNetworkConnection(addr);
experimental-genesis 1505 Sleep(500);
experimental-genesis 1506 if (fShutdown)
experimental-genesis 1507 return;
experimental-genesis 1508 }
experimental-genesis 1509 }
experimental-genesis 1510 }
experimental-genesis 1511
experimental-genesis 1512 // Initiate network connections
experimental-genesis 1513 int64 nStart = GetTime();
experimental-genesis 1514 loop
experimental-genesis 1515 {
experimental-genesis 1516 vnThreadsRunning[1]--;
experimental-genesis 1517 Sleep(500);
experimental-genesis 1518 vnThreadsRunning[1]++;
experimental-genesis 1519 if (fShutdown)
experimental-genesis 1520 return;
experimental-genesis 1521
experimental-genesis 1522 // Limit outbound connections
experimental-genesis 1523 loop
experimental-genesis 1524 {
experimental-genesis 1525 int nOutbound = 0;
experimental-genesis 1526 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 1527 BOOST_FOREACH(CNode* pnode, vNodes)
experimental-genesis 1528 if (!pnode->fInbound)
experimental-genesis 1529 nOutbound++;
experimental-genesis 1530 int nMaxOutboundConnections = MAX_OUTBOUND_CONNECTIONS;
experimental-genesis 1531 nMaxOutboundConnections = min(nMaxOutboundConnections, (int)GetArg("-maxconnections", 125));
experimental-genesis 1532 if (nOutbound < nMaxOutboundConnections)
experimental-genesis 1533 break;
experimental-genesis 1534 vnThreadsRunning[1]--;
experimental-genesis 1535 Sleep(2000);
experimental-genesis 1536 vnThreadsRunning[1]++;
experimental-genesis 1537 if (fShutdown)
experimental-genesis 1538 return;
experimental-genesis 1539 }
experimental-genesis 1540
experimental-genesis 1541 bool fAddSeeds = false;
experimental-genesis 1542
experimental-genesis 1543 CRITICAL_BLOCK(cs_mapAddresses)
experimental-genesis 1544 {
experimental-genesis 1545 // Add seed nodes if IRC isn't working
experimental-genesis 1546 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
experimental-genesis 1547 if (mapAddresses.empty() && (GetTime() - nStart > 60 || fUseProxy) && !fTestNet)
experimental-genesis 1548 fAddSeeds = true;
experimental-genesis 1549 }
experimental-genesis 1550
experimental-genesis 1551 if (fAddSeeds)
experimental-genesis 1552 {
experimental-genesis 1553 for (int i = 0; i < ARRAYLEN(pnSeed); i++)
experimental-genesis 1554 {
experimental-genesis 1555 // It'll only connect to one or two seed nodes because once it connects,
experimental-genesis 1556 // it'll get a pile of addresses with newer timestamps.
experimental-genesis 1557 // Seed nodes are given a random 'last seen time' of between one and two
experimental-genesis 1558 // weeks ago.
experimental-genesis 1559 const int64 nOneWeek = 7*24*60*60;
experimental-genesis 1560 CAddress addr;
experimental-genesis 1561 addr.ip = pnSeed[i];
experimental-genesis 1562 addr.nTime = GetTime()-GetRand(nOneWeek)-nOneWeek;
experimental-genesis 1563 AddAddress(addr);
experimental-genesis 1564 }
experimental-genesis 1565 }
experimental-genesis 1566
experimental-genesis 1567 //
experimental-genesis 1568 // Choose an address to connect to based on most recently seen
experimental-genesis 1569 //
experimental-genesis 1570 CAddress addrConnect;
experimental-genesis 1571 int64 nBest = INT64_MIN;
experimental-genesis 1572
experimental-genesis 1573 // Only connect to one address per a.b.?.? range.
experimental-genesis 1574 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
experimental-genesis 1575 set<unsigned int> setConnected;
experimental-genesis 1576 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 1577 BOOST_FOREACH(CNode* pnode, vNodes)
experimental-genesis 1578 setConnected.insert(pnode->addr.ip & 0x0000ffff);
experimental-genesis 1579
experimental-genesis 1580 int64 nANow = GetAdjustedTime();
experimental-genesis 1581
experimental-genesis 1582 CRITICAL_BLOCK(cs_mapAddresses)
experimental-genesis 1583 {
experimental-genesis 1584 BOOST_FOREACH(const PAIRTYPE(vector<unsigned char>, CAddress)& item, mapAddresses)
experimental-genesis 1585 {
experimental-genesis 1586 const CAddress& addr = item.second;
experimental-genesis 1587 if (!addr.IsIPv4() || !addr.IsValid() || setConnected.count(addr.ip & 0x0000ffff))
experimental-genesis 1588 continue;
experimental-genesis 1589 int64 nSinceLastSeen = nANow - addr.nTime;
experimental-genesis 1590 int64 nSinceLastTry = nANow - addr.nLastTry;
experimental-genesis 1591
experimental-genesis 1592 // Randomize the order in a deterministic way, putting the standard port first
experimental-genesis 1593 int64 nRandomizer = (uint64)(nStart * 4951 + addr.nLastTry * 9567851 + addr.ip * 7789) % (2 * 60 * 60);
experimental-genesis 1594 if (addr.port != htons(GetDefaultPort()))
experimental-genesis 1595 nRandomizer += 2 * 60 * 60;
experimental-genesis 1596
experimental-genesis 1597 // Last seen Base retry frequency
experimental-genesis 1598 // <1 hour 10 min
experimental-genesis 1599 // 1 hour 1 hour
experimental-genesis 1600 // 4 hours 2 hours
experimental-genesis 1601 // 24 hours 5 hours
experimental-genesis 1602 // 48 hours 7 hours
experimental-genesis 1603 // 7 days 13 hours
experimental-genesis 1604 // 30 days 27 hours
experimental-genesis 1605 // 90 days 46 hours
experimental-genesis 1606 // 365 days 93 hours
experimental-genesis 1607 int64 nDelay = (int64)(3600.0 * sqrt(fabs((double)nSinceLastSeen) / 3600.0) + nRandomizer);
experimental-genesis 1608
experimental-genesis 1609 // Fast reconnect for one hour after last seen
experimental-genesis 1610 if (nSinceLastSeen < 60 * 60)
experimental-genesis 1611 nDelay = 10 * 60;
experimental-genesis 1612
experimental-genesis 1613 // Limit retry frequency
experimental-genesis 1614 if (nSinceLastTry < nDelay)
experimental-genesis 1615 continue;
experimental-genesis 1616
experimental-genesis 1617 // If we have IRC, we'll be notified when they first come online,
experimental-genesis 1618 // and again every 24 hours by the refresh broadcast.
experimental-genesis 1619 if (nGotIRCAddresses > 0 && vNodes.size() >= 2 && nSinceLastSeen > 24 * 60 * 60)
experimental-genesis 1620 continue;
experimental-genesis 1621
experimental-genesis 1622 // Only try the old stuff if we don't have enough connections
experimental-genesis 1623 if (vNodes.size() >= 8 && nSinceLastSeen > 24 * 60 * 60)
experimental-genesis 1624 continue;
experimental-genesis 1625
experimental-genesis 1626 // If multiple addresses are ready, prioritize by time since
experimental-genesis 1627 // last seen and time since last tried.
experimental-genesis 1628 int64 nScore = min(nSinceLastTry, (int64)24 * 60 * 60) - nSinceLastSeen - nRandomizer;
experimental-genesis 1629 if (nScore > nBest)
experimental-genesis 1630 {
experimental-genesis 1631 nBest = nScore;
experimental-genesis 1632 addrConnect = addr;
experimental-genesis 1633 }
experimental-genesis 1634 }
experimental-genesis 1635 }
experimental-genesis 1636
experimental-genesis 1637 if (addrConnect.IsValid())
experimental-genesis 1638 OpenNetworkConnection(addrConnect);
experimental-genesis 1639 }
experimental-genesis 1640 }
experimental-genesis 1641
experimental-genesis 1642 bool OpenNetworkConnection(const CAddress& addrConnect)
experimental-genesis 1643 {
experimental-genesis 1644 //
experimental-genesis 1645 // Initiate outbound network connection
experimental-genesis 1646 //
experimental-genesis 1647 if (fShutdown)
experimental-genesis 1648 return false;
experimental-genesis 1649 if (addrConnect.ip == addrLocalHost.ip || !addrConnect.IsIPv4() ||
experimental-genesis 1650 FindNode(addrConnect.ip) || CNode::IsBanned(addrConnect.ip))
experimental-genesis 1651 return false;
experimental-genesis 1652
experimental-genesis 1653 vnThreadsRunning[1]--;
experimental-genesis 1654 CNode* pnode = ConnectNode(addrConnect);
experimental-genesis 1655 vnThreadsRunning[1]++;
experimental-genesis 1656 if (fShutdown)
experimental-genesis 1657 return false;
experimental-genesis 1658 if (!pnode)
experimental-genesis 1659 return false;
experimental-genesis 1660 pnode->fNetworkNode = true;
experimental-genesis 1661
experimental-genesis 1662 return true;
experimental-genesis 1663 }
experimental-genesis 1664
experimental-genesis 1665
experimental-genesis 1666
experimental-genesis 1667
experimental-genesis 1668
experimental-genesis 1669
experimental-genesis 1670
experimental-genesis 1671
experimental-genesis 1672 void ThreadMessageHandler(void* parg)
experimental-genesis 1673 {
experimental-genesis 1674 IMPLEMENT_RANDOMIZE_STACK(ThreadMessageHandler(parg));
experimental-genesis 1675 try
experimental-genesis 1676 {
experimental-genesis 1677 vnThreadsRunning[2]++;
experimental-genesis 1678 ThreadMessageHandler2(parg);
experimental-genesis 1679 vnThreadsRunning[2]--;
experimental-genesis 1680 }
experimental-genesis 1681 catch (std::exception& e) {
experimental-genesis 1682 vnThreadsRunning[2]--;
experimental-genesis 1683 PrintException(&e, "ThreadMessageHandler()");
experimental-genesis 1684 } catch (...) {
experimental-genesis 1685 vnThreadsRunning[2]--;
experimental-genesis 1686 PrintException(NULL, "ThreadMessageHandler()");
experimental-genesis 1687 }
experimental-genesis 1688 printf("ThreadMessageHandler exiting\n");
experimental-genesis 1689 }
experimental-genesis 1690
experimental-genesis 1691 void ThreadMessageHandler2(void* parg)
experimental-genesis 1692 {
experimental-genesis 1693 printf("ThreadMessageHandler started\n");
experimental-genesis 1694 SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL);
experimental-genesis 1695 while (!fShutdown)
experimental-genesis 1696 {
experimental-genesis 1697 vector<CNode*> vNodesCopy;
experimental-genesis 1698 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 1699 {
experimental-genesis 1700 vNodesCopy = vNodes;
experimental-genesis 1701 BOOST_FOREACH(CNode* pnode, vNodesCopy)
experimental-genesis 1702 pnode->AddRef();
experimental-genesis 1703 }
experimental-genesis 1704
experimental-genesis 1705 // Poll the connected nodes for messages
experimental-genesis 1706 CNode* pnodeTrickle = NULL;
experimental-genesis 1707 if (!vNodesCopy.empty())
experimental-genesis 1708 pnodeTrickle = vNodesCopy[GetRand(vNodesCopy.size())];
experimental-genesis 1709 BOOST_FOREACH(CNode* pnode, vNodesCopy)
experimental-genesis 1710 {
experimental-genesis 1711 // Receive messages
experimental-genesis 1712 TRY_CRITICAL_BLOCK(pnode->cs_vRecv)
experimental-genesis 1713 ProcessMessages(pnode);
experimental-genesis 1714 if (fShutdown)
experimental-genesis 1715 return;
experimental-genesis 1716
experimental-genesis 1717 // Send messages
experimental-genesis 1718 TRY_CRITICAL_BLOCK(pnode->cs_vSend)
experimental-genesis 1719 SendMessages(pnode, pnode == pnodeTrickle);
experimental-genesis 1720 if (fShutdown)
experimental-genesis 1721 return;
experimental-genesis 1722 }
experimental-genesis 1723
experimental-genesis 1724 CRITICAL_BLOCK(cs_vNodes)
experimental-genesis 1725 {
experimental-genesis 1726 BOOST_FOREACH(CNode* pnode, vNodesCopy)
experimental-genesis 1727 pnode->Release();
experimental-genesis 1728 }
experimental-genesis 1729
experimental-genesis 1730 // Wait and allow messages to bunch up.
experimental-genesis 1731 // Reduce vnThreadsRunning so StopNode has permission to exit while
experimental-genesis 1732 // we're sleeping, but we must always check fShutdown after doing this.
experimental-genesis 1733 vnThreadsRunning[2]--;
experimental-genesis 1734 Sleep(100);
experimental-genesis 1735 if (fRequestShutdown)
experimental-genesis 1736 Shutdown(NULL);
experimental-genesis 1737 vnThreadsRunning[2]++;
experimental-genesis 1738 if (fShutdown)
experimental-genesis 1739 return;
experimental-genesis 1740 }
experimental-genesis 1741 }
experimental-genesis 1742
experimental-genesis 1743
experimental-genesis 1744
experimental-genesis 1745
experimental-genesis 1746
experimental-genesis 1747
experimental-genesis 1748 bool BindListenPort(string& strError)
experimental-genesis 1749 {
experimental-genesis 1750 strError = "";
experimental-genesis 1751 int nOne = 1;
experimental-genesis 1752 addrLocalHost.port = htons(GetListenPort());
experimental-genesis 1753
experimental-genesis 1754 #ifdef WIN32
experimental-genesis 1755 // Initialize Windows Sockets
experimental-genesis 1756 WSADATA wsadata;
experimental-genesis 1757 int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
experimental-genesis 1758 if (ret != NO_ERROR)
experimental-genesis 1759 {
experimental-genesis 1760 strError = strprintf("Error: TCP/IP socket library failed to start (WSAStartup returned error %d)", ret);
experimental-genesis 1761 printf("%s\n", strError.c_str());
experimental-genesis 1762 return false;
experimental-genesis 1763 }
experimental-genesis 1764 #endif
experimental-genesis 1765
experimental-genesis 1766 // Create socket for listening for incoming connections
experimental-genesis 1767 hListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
experimental-genesis 1768 if (hListenSocket == INVALID_SOCKET)
experimental-genesis 1769 {
experimental-genesis 1770 strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %d)", WSAGetLastError());
experimental-genesis 1771 printf("%s\n", strError.c_str());
experimental-genesis 1772 return false;
experimental-genesis 1773 }
experimental-genesis 1774
experimental-genesis 1775 #ifdef SO_NOSIGPIPE
experimental-genesis 1776 // Different way of disabling SIGPIPE on BSD
experimental-genesis 1777 setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
experimental-genesis 1778 #endif
experimental-genesis 1779
experimental-genesis 1780 #ifndef WIN32
experimental-genesis 1781 // Allow binding if the port is still in TIME_WAIT state after
experimental-genesis 1782 // the program was closed and restarted. Not an issue on windows.
experimental-genesis 1783 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
experimental-genesis 1784 #endif
experimental-genesis 1785
experimental-genesis 1786 #ifdef WIN32
experimental-genesis 1787 // Set to nonblocking, incoming connections will also inherit this
experimental-genesis 1788 if (ioctlsocket(hListenSocket, FIONBIO, (u_long*)&nOne) == SOCKET_ERROR)
experimental-genesis 1789 #else
experimental-genesis 1790 if (fcntl(hListenSocket, F_SETFL, O_NONBLOCK) == SOCKET_ERROR)
experimental-genesis 1791 #endif
experimental-genesis 1792 {
experimental-genesis 1793 strError = strprintf("Error: Couldn't set properties on socket for incoming connections (error %d)", WSAGetLastError());
experimental-genesis 1794 printf("%s\n", strError.c_str());
experimental-genesis 1795 return false;
experimental-genesis 1796 }
experimental-genesis 1797
experimental-genesis 1798 // The sockaddr_in structure specifies the address family,
experimental-genesis 1799 // IP address, and port for the socket that is being bound
experimental-genesis 1800 struct sockaddr_in sockaddr;
experimental-genesis 1801 memset(&sockaddr, 0, sizeof(sockaddr));
experimental-genesis 1802 sockaddr.sin_family = AF_INET;
experimental-genesis 1803 sockaddr.sin_addr.s_addr = INADDR_ANY; // bind to all IPs on this computer
experimental-genesis 1804 sockaddr.sin_port = htons(GetListenPort());
experimental-genesis 1805 if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
experimental-genesis 1806 {
experimental-genesis 1807 int nErr = WSAGetLastError();
experimental-genesis 1808 if (nErr == WSAEADDRINUSE)
experimental-genesis 1809 strError = strprintf(_("Unable to bind to port %d on this computer. Bitcoin is probably already running."), ntohs(sockaddr.sin_port));
experimental-genesis 1810 else
experimental-genesis 1811 strError = strprintf("Error: Unable to bind to port %d on this computer (bind returned error %d)", ntohs(sockaddr.sin_port), nErr);
experimental-genesis 1812 printf("%s\n", strError.c_str());
experimental-genesis 1813 return false;
experimental-genesis 1814 }
experimental-genesis 1815 printf("Bound to port %d\n", ntohs(sockaddr.sin_port));
experimental-genesis 1816
experimental-genesis 1817 // Listen for incoming connections
experimental-genesis 1818 if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
experimental-genesis 1819 {
experimental-genesis 1820 strError = strprintf("Error: Listening for incoming connections failed (listen returned error %d)", WSAGetLastError());
experimental-genesis 1821 printf("%s\n", strError.c_str());
experimental-genesis 1822 return false;
experimental-genesis 1823 }
experimental-genesis 1824
experimental-genesis 1825 return true;
experimental-genesis 1826 }
experimental-genesis 1827
experimental-genesis 1828 void StartNode(void* parg)
experimental-genesis 1829 {
experimental-genesis 1830 if (pnodeLocalHost == NULL)
experimental-genesis 1831 pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress("127.0.0.1", 0, false, nLocalServices));
experimental-genesis 1832
experimental-genesis 1833 #ifdef WIN32
experimental-genesis 1834 // Get local host ip
experimental-genesis 1835 char pszHostName[1000] = "";
experimental-genesis 1836 if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
experimental-genesis 1837 {
experimental-genesis 1838 vector<CAddress> vaddr;
experimental-genesis 1839 if (Lookup(pszHostName, vaddr, nLocalServices, -1, true))
experimental-genesis 1840 BOOST_FOREACH (const CAddress &addr, vaddr)
experimental-genesis 1841 if (addr.GetByte(3) != 127)
experimental-genesis 1842 {
experimental-genesis 1843 addrLocalHost = addr;
experimental-genesis 1844 break;
experimental-genesis 1845 }
experimental-genesis 1846 }
experimental-genesis 1847 #else
experimental-genesis 1848 // Get local host ip
experimental-genesis 1849 struct ifaddrs* myaddrs;
experimental-genesis 1850 if (getifaddrs(&myaddrs) == 0)
experimental-genesis 1851 {
experimental-genesis 1852 for (struct ifaddrs* ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
experimental-genesis 1853 {
experimental-genesis 1854 if (ifa->ifa_addr == NULL) continue;
experimental-genesis 1855 if ((ifa->ifa_flags & IFF_UP) == 0) continue;
experimental-genesis 1856 if (strcmp(ifa->ifa_name, "lo") == 0) continue;
experimental-genesis 1857 if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
experimental-genesis 1858 char pszIP[100];
experimental-genesis 1859 if (ifa->ifa_addr->sa_family == AF_INET)
experimental-genesis 1860 {
experimental-genesis 1861 struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
experimental-genesis 1862 if (inet_ntop(ifa->ifa_addr->sa_family, (void*)&(s4->sin_addr), pszIP, sizeof(pszIP)) != NULL)
experimental-genesis 1863 printf("ipv4 %s: %s\n", ifa->ifa_name, pszIP);
experimental-genesis 1864
experimental-genesis 1865 // Take the first IP that isn't loopback 127.x.x.x
experimental-genesis 1866 CAddress addr(*(unsigned int*)&s4->sin_addr, GetListenPort(), nLocalServices);
experimental-genesis 1867 if (addr.IsValid() && addr.GetByte(3) != 127)
experimental-genesis 1868 {
experimental-genesis 1869 addrLocalHost = addr;
experimental-genesis 1870 break;
experimental-genesis 1871 }
experimental-genesis 1872 }
experimental-genesis 1873 else if (ifa->ifa_addr->sa_family == AF_INET6)
experimental-genesis 1874 {
experimental-genesis 1875 struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
experimental-genesis 1876 if (inet_ntop(ifa->ifa_addr->sa_family, (void*)&(s6->sin6_addr), pszIP, sizeof(pszIP)) != NULL)
experimental-genesis 1877 printf("ipv6 %s: %s\n", ifa->ifa_name, pszIP);
experimental-genesis 1878 }
experimental-genesis 1879 }
experimental-genesis 1880 freeifaddrs(myaddrs);
experimental-genesis 1881 }
experimental-genesis 1882 #endif
experimental-genesis 1883 printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str());
experimental-genesis 1884
experimental-genesis 1885 if (fUseProxy || mapArgs.count("-connect") || fNoListen)
experimental-genesis 1886 {
experimental-genesis 1887 // Proxies can't take incoming connections
experimental-genesis 1888 addrLocalHost.ip = CAddress("0.0.0.0").ip;
experimental-genesis 1889 printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str());
experimental-genesis 1890 }
experimental-genesis 1891 else
experimental-genesis 1892 {
experimental-genesis 1893 CreateThread(ThreadGetMyExternalIP, NULL);
experimental-genesis 1894 }
experimental-genesis 1895
experimental-genesis 1896 //
experimental-genesis 1897 // Start threads
experimental-genesis 1898 //
experimental-genesis 1899
experimental-genesis 1900 if (GetBoolArg("-nodnsseed"))
experimental-genesis 1901 printf("DNS seeding disabled\n");
experimental-genesis 1902 else
experimental-genesis 1903 if (!CreateThread(ThreadDNSAddressSeed, NULL))
experimental-genesis 1904 printf("Error: CreateThread(ThreadDNSAddressSeed) failed\n");
experimental-genesis 1905
experimental-genesis 1906 // Map ports with UPnP
experimental-genesis 1907 if (fHaveUPnP)
experimental-genesis 1908 MapPort(fUseUPnP);
experimental-genesis 1909
experimental-genesis 1910 // Get addresses from IRC and advertise ours
experimental-genesis 1911 if (!CreateThread(ThreadIRCSeed, NULL))
experimental-genesis 1912 printf("Error: CreateThread(ThreadIRCSeed) failed\n");
experimental-genesis 1913
experimental-genesis 1914 // Send and receive from sockets, accept connections
experimental-genesis 1915 if (!CreateThread(ThreadSocketHandler, NULL))
experimental-genesis 1916 printf("Error: CreateThread(ThreadSocketHandler) failed\n");
experimental-genesis 1917
experimental-genesis 1918 // Initiate outbound connections
experimental-genesis 1919 if (!CreateThread(ThreadOpenConnections, NULL))
experimental-genesis 1920 printf("Error: CreateThread(ThreadOpenConnections) failed\n");
experimental-genesis 1921
experimental-genesis 1922 // Process messages
experimental-genesis 1923 if (!CreateThread(ThreadMessageHandler, NULL))
experimental-genesis 1924 printf("Error: CreateThread(ThreadMessageHandler) failed\n");
experimental-genesis 1925
experimental-genesis 1926 // Generate coins in the background
experimental-genesis 1927 GenerateBitcoins(fGenerateBitcoins, pwalletMain);
experimental-genesis 1928 }
experimental-genesis 1929
experimental-genesis 1930 bool StopNode()
experimental-genesis 1931 {
experimental-genesis 1932 printf("StopNode()\n");
experimental-genesis 1933 fShutdown = true;
experimental-genesis 1934 nTransactionsUpdated++;
experimental-genesis 1935 int64 nStart = GetTime();
experimental-genesis 1936 while (vnThreadsRunning[0] > 0 || vnThreadsRunning[1] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0
experimental-genesis 1937 #ifdef USE_UPNP
experimental-genesis 1938 || vnThreadsRunning[5] > 0
experimental-genesis 1939 #endif
experimental-genesis 1940 )
experimental-genesis 1941 {
experimental-genesis 1942 if (GetTime() - nStart > 20)
experimental-genesis 1943 break;
experimental-genesis 1944 Sleep(20);
experimental-genesis 1945 }
experimental-genesis 1946 if (vnThreadsRunning[0] > 0) printf("ThreadSocketHandler still running\n");
experimental-genesis 1947 if (vnThreadsRunning[1] > 0) printf("ThreadOpenConnections still running\n");
experimental-genesis 1948 if (vnThreadsRunning[2] > 0) printf("ThreadMessageHandler still running\n");
experimental-genesis 1949 if (vnThreadsRunning[3] > 0) printf("ThreadBitcoinMiner still running\n");
experimental-genesis 1950 if (vnThreadsRunning[4] > 0) printf("ThreadRPCServer still running\n");
experimental-genesis 1951 if (fHaveUPnP && vnThreadsRunning[5] > 0) printf("ThreadMapPort still running\n");
experimental-genesis 1952 if (vnThreadsRunning[6] > 0) printf("ThreadDNSAddressSeed still running\n");
experimental-genesis 1953 while (vnThreadsRunning[2] > 0 || vnThreadsRunning[4] > 0)
experimental-genesis 1954 Sleep(20);
experimental-genesis 1955 Sleep(50);
experimental-genesis 1956
experimental-genesis 1957 return true;
experimental-genesis 1958 }
experimental-genesis 1959
experimental-genesis 1960 class CNetCleanup
experimental-genesis 1961 {
experimental-genesis 1962 public:
experimental-genesis 1963 CNetCleanup()
experimental-genesis 1964 {
experimental-genesis 1965 }
experimental-genesis 1966 ~CNetCleanup()
experimental-genesis 1967 {
experimental-genesis 1968 // Close sockets
experimental-genesis 1969 BOOST_FOREACH(CNode* pnode, vNodes)
experimental-genesis 1970 if (pnode->hSocket != INVALID_SOCKET)
experimental-genesis 1971 closesocket(pnode->hSocket);
experimental-genesis 1972 if (hListenSocket != INVALID_SOCKET)
experimental-genesis 1973 if (closesocket(hListenSocket) == SOCKET_ERROR)
experimental-genesis 1974 printf("closesocket(hListenSocket) failed with error %d\n", WSAGetLastError());
experimental-genesis 1975
experimental-genesis 1976 #ifdef WIN32
experimental-genesis 1977 // Shutdown Windows Sockets
experimental-genesis 1978 WSACleanup();
experimental-genesis 1979 #endif
experimental-genesis 1980 }
experimental-genesis 1981 }
experimental-genesis 1982 instance_of_cnetcleanup;