2009-08-29 20:46:39 -07:00
|
|
|
// Copyright (c) 2009 Satoshi Nakamoto
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "headers.h"
|
|
|
|
|
|
|
|
void ThreadMessageHandler2(void* parg);
|
|
|
|
void ThreadSocketHandler2(void* parg);
|
|
|
|
void ThreadOpenConnections2(void* parg);
|
2009-10-28 19:52:48 -07:00
|
|
|
bool OpenNetworkConnection(const CAddress& addrConnect);
|
2009-08-29 20:46:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Global state variables
|
|
|
|
//
|
|
|
|
bool fClient = false;
|
|
|
|
uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
|
|
|
|
CAddress addrLocalHost(0, DEFAULT_PORT, nLocalServices);
|
2009-10-31 18:16:51 -07:00
|
|
|
CNode* pnodeLocalHost = NULL;
|
2009-10-28 19:52:48 -07:00
|
|
|
uint64 nLocalHostNonce = 0;
|
2009-08-29 20:46:39 -07:00
|
|
|
bool fShutdown = false;
|
2009-10-20 18:08:05 -07:00
|
|
|
array<int, 10> vnThreadsRunning;
|
2009-10-28 19:52:48 -07:00
|
|
|
SOCKET hListenSocket = INVALID_SOCKET;
|
2009-10-20 18:08:05 -07:00
|
|
|
|
2009-08-29 20:46:39 -07:00
|
|
|
vector<CNode*> vNodes;
|
|
|
|
CCriticalSection cs_vNodes;
|
|
|
|
map<vector<unsigned char>, CAddress> mapAddresses;
|
|
|
|
CCriticalSection cs_mapAddresses;
|
|
|
|
map<CInv, CDataStream> mapRelay;
|
|
|
|
deque<pair<int64, CInv> > vRelayExpiration;
|
|
|
|
CCriticalSection cs_mapRelay;
|
|
|
|
map<CInv, int64> mapAlreadyAskedFor;
|
|
|
|
|
2009-10-28 19:52:48 -07:00
|
|
|
// Settings
|
|
|
|
int fUseProxy = false;
|
|
|
|
CAddress addrProxy("127.0.0.1:9050");
|
2009-08-29 20:46:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet)
|
|
|
|
{
|
|
|
|
hSocketRet = INVALID_SOCKET;
|
|
|
|
|
|
|
|
SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (hSocket == INVALID_SOCKET)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool fRoutable = !(addrConnect.GetByte(3) == 10 || (addrConnect.GetByte(3) == 192 && addrConnect.GetByte(2) == 168));
|
2009-10-28 19:52:48 -07:00
|
|
|
bool fProxy = (fUseProxy && fRoutable);
|
2009-08-29 20:46:39 -07:00
|
|
|
struct sockaddr_in sockaddr = (fProxy ? addrProxy.GetSockAddr() : addrConnect.GetSockAddr());
|
|
|
|
|
|
|
|
if (connect(hSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
closesocket(hSocket);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fProxy)
|
|
|
|
{
|
2009-10-20 18:08:05 -07:00
|
|
|
printf("Proxy connecting %s\n", addrConnect.ToStringLog().c_str());
|
2009-08-29 20:46:39 -07:00
|
|
|
char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user";
|
|
|
|
memcpy(pszSocks4IP + 2, &addrConnect.port, 2);
|
|
|
|
memcpy(pszSocks4IP + 4, &addrConnect.ip, 4);
|
|
|
|
char* pszSocks4 = pszSocks4IP;
|
|
|
|
int nSize = sizeof(pszSocks4IP);
|
|
|
|
|
|
|
|
int ret = send(hSocket, pszSocks4, nSize, 0);
|
|
|
|
if (ret != nSize)
|
|
|
|
{
|
|
|
|
closesocket(hSocket);
|
2009-10-28 19:52:48 -07:00
|
|
|
return error("Error sending to proxy");
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
|
|
|
char pchRet[8];
|
|
|
|
if (recv(hSocket, pchRet, 8, 0) != 8)
|
|
|
|
{
|
|
|
|
closesocket(hSocket);
|
2009-10-28 19:52:48 -07:00
|
|
|
return error("Error reading proxy response");
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
|
|
|
if (pchRet[1] != 0x5a)
|
|
|
|
{
|
|
|
|
closesocket(hSocket);
|
2009-10-28 19:52:48 -07:00
|
|
|
return error("Proxy returned error %d", pchRet[1]);
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
2009-10-20 18:08:05 -07:00
|
|
|
printf("Proxy connection established %s\n", addrConnect.ToStringLog().c_str());
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
hSocketRet = hSocket;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool GetMyExternalIP2(const CAddress& addrConnect, const char* pszGet, const char* pszKeyword, unsigned int& ipRet)
|
|
|
|
{
|
|
|
|
SOCKET hSocket;
|
|
|
|
if (!ConnectSocket(addrConnect, hSocket))
|
2009-10-28 19:52:48 -07:00
|
|
|
return error("GetMyExternalIP() : connection to %s failed", addrConnect.ToString().c_str());
|
2009-08-29 20:46:39 -07:00
|
|
|
|
|
|
|
send(hSocket, pszGet, strlen(pszGet), 0);
|
|
|
|
|
|
|
|
string strLine;
|
|
|
|
while (RecvLine(hSocket, strLine))
|
|
|
|
{
|
|
|
|
if (strLine.empty())
|
|
|
|
{
|
|
|
|
loop
|
|
|
|
{
|
|
|
|
if (!RecvLine(hSocket, strLine))
|
|
|
|
{
|
|
|
|
closesocket(hSocket);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (strLine.find(pszKeyword) != -1)
|
|
|
|
{
|
|
|
|
strLine = strLine.substr(strLine.find(pszKeyword) + strlen(pszKeyword));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closesocket(hSocket);
|
|
|
|
if (strLine.find("<"))
|
|
|
|
strLine = strLine.substr(0, strLine.find("<"));
|
|
|
|
strLine = strLine.substr(strspn(strLine.c_str(), " \t\n\r"));
|
|
|
|
strLine = wxString(strLine).Trim();
|
|
|
|
CAddress addr(strLine.c_str());
|
|
|
|
printf("GetMyExternalIP() received [%s] %s\n", strLine.c_str(), addr.ToString().c_str());
|
2009-10-31 18:16:51 -07:00
|
|
|
if (addr.ip == 0 || addr.ip == INADDR_NONE || !addr.IsRoutable())
|
2009-08-29 20:46:39 -07:00
|
|
|
return false;
|
|
|
|
ipRet = addr.ip;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closesocket(hSocket);
|
2009-10-28 19:52:48 -07:00
|
|
|
return error("GetMyExternalIP() : connection closed");
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GetMyExternalIP(unsigned int& ipRet)
|
|
|
|
{
|
|
|
|
CAddress addrConnect;
|
|
|
|
char* pszGet;
|
|
|
|
char* pszKeyword;
|
|
|
|
|
2009-10-28 19:52:48 -07:00
|
|
|
if (fUseProxy)
|
|
|
|
return false;
|
|
|
|
|
2009-08-29 20:46:39 -07:00
|
|
|
for (int nLookup = 0; nLookup <= 1; nLookup++)
|
|
|
|
for (int nHost = 1; nHost <= 2; nHost++)
|
|
|
|
{
|
|
|
|
if (nHost == 1)
|
|
|
|
{
|
|
|
|
addrConnect = CAddress("70.86.96.218:80"); // www.ipaddressworld.com
|
|
|
|
|
|
|
|
if (nLookup == 1)
|
|
|
|
{
|
|
|
|
struct hostent* phostent = gethostbyname("www.ipaddressworld.com");
|
|
|
|
if (phostent && phostent->h_addr_list && phostent->h_addr_list[0])
|
|
|
|
addrConnect = CAddress(*(u_long*)phostent->h_addr_list[0], htons(80));
|
|
|
|
}
|
|
|
|
|
|
|
|
pszGet = "GET /ip.php HTTP/1.1\r\n"
|
|
|
|
"Host: www.ipaddressworld.com\r\n"
|
|
|
|
"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"\r\n";
|
|
|
|
|
|
|
|
pszKeyword = "IP:";
|
|
|
|
}
|
|
|
|
else if (nHost == 2)
|
|
|
|
{
|
|
|
|
addrConnect = CAddress("208.78.68.70:80"); // checkip.dyndns.org
|
|
|
|
|
|
|
|
if (nLookup == 1)
|
|
|
|
{
|
|
|
|
struct hostent* phostent = gethostbyname("checkip.dyndns.org");
|
|
|
|
if (phostent && phostent->h_addr_list && phostent->h_addr_list[0])
|
|
|
|
addrConnect = CAddress(*(u_long*)phostent->h_addr_list[0], htons(80));
|
|
|
|
}
|
|
|
|
|
|
|
|
pszGet = "GET / HTTP/1.1\r\n"
|
|
|
|
"Host: checkip.dyndns.org\r\n"
|
|
|
|
"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"\r\n";
|
|
|
|
|
|
|
|
pszKeyword = "Address:";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetMyExternalIP2(addrConnect, pszGet, pszKeyword, ipRet))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-04 20:41:36 -08:00
|
|
|
bool AddAddress(CAddrDB& addrdb, CAddress addr, bool fCurrentlyOnline)
|
2009-08-29 20:46:39 -07:00
|
|
|
{
|
|
|
|
if (!addr.IsRoutable())
|
|
|
|
return false;
|
|
|
|
if (addr.ip == addrLocalHost.ip)
|
|
|
|
return false;
|
2009-11-04 20:41:36 -08:00
|
|
|
if (fCurrentlyOnline)
|
|
|
|
addr.nTime = GetAdjustedTime();
|
2009-08-29 20:46:39 -07:00
|
|
|
CRITICAL_BLOCK(cs_mapAddresses)
|
|
|
|
{
|
|
|
|
map<vector<unsigned char>, CAddress>::iterator it = mapAddresses.find(addr.GetKey());
|
|
|
|
if (it == mapAddresses.end())
|
|
|
|
{
|
|
|
|
// New address
|
|
|
|
mapAddresses.insert(make_pair(addr.GetKey(), addr));
|
|
|
|
addrdb.WriteAddress(addr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-04 20:41:36 -08:00
|
|
|
bool fUpdated = false;
|
2009-08-29 20:46:39 -07:00
|
|
|
CAddress& addrFound = (*it).second;
|
|
|
|
if ((addrFound.nServices | addr.nServices) != addrFound.nServices)
|
|
|
|
{
|
|
|
|
// Services have been added
|
|
|
|
addrFound.nServices |= addr.nServices;
|
2009-11-04 20:41:36 -08:00
|
|
|
fUpdated = true;
|
|
|
|
}
|
|
|
|
int64 nUpdateInterval = (fCurrentlyOnline ? 60 * 60 : 24 * 60 * 60);
|
|
|
|
if (addrFound.nTime < addr.nTime - nUpdateInterval)
|
|
|
|
{
|
|
|
|
// Periodically update most recently seen time
|
|
|
|
addrFound.nTime = addr.nTime;
|
|
|
|
fUpdated = true;
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
2009-11-04 20:41:36 -08:00
|
|
|
if (fUpdated)
|
|
|
|
addrdb.WriteAddress(addrFound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddressCurrentlyConnected(const CAddress& addr)
|
|
|
|
{
|
|
|
|
CRITICAL_BLOCK(cs_mapAddresses)
|
|
|
|
{
|
|
|
|
// Only if it's been published already
|
|
|
|
map<vector<unsigned char>, CAddress>::iterator it = mapAddresses.find(addr.GetKey());
|
|
|
|
if (it != mapAddresses.end())
|
|
|
|
{
|
|
|
|
CAddress& addrFound = (*it).second;
|
|
|
|
int64 nUpdateInterval = 60 * 60;
|
|
|
|
if (addrFound.nTime < GetAdjustedTime() - nUpdateInterval)
|
2009-10-20 18:08:05 -07:00
|
|
|
{
|
|
|
|
// Periodically update most recently seen time
|
|
|
|
addrFound.nTime = GetAdjustedTime();
|
2009-11-04 20:41:36 -08:00
|
|
|
CAddrDB addrdb;
|
2009-10-20 18:08:05 -07:00
|
|
|
addrdb.WriteAddress(addrFound);
|
|
|
|
}
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1)
|
|
|
|
{
|
|
|
|
// If the dialog might get closed before the reply comes back,
|
|
|
|
// call this in the destructor so it doesn't get called after it's deleted.
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
{
|
|
|
|
foreach(CNode* pnode, vNodes)
|
|
|
|
{
|
|
|
|
CRITICAL_BLOCK(pnode->cs_mapRequests)
|
|
|
|
{
|
|
|
|
for (map<uint256, CRequestTracker>::iterator mi = pnode->mapRequests.begin(); mi != pnode->mapRequests.end();)
|
|
|
|
{
|
|
|
|
CRequestTracker& tracker = (*mi).second;
|
|
|
|
if (tracker.fn == fn && tracker.param1 == param1)
|
|
|
|
pnode->mapRequests.erase(mi++);
|
|
|
|
else
|
|
|
|
mi++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Subscription methods for the broadcast and subscription system.
|
|
|
|
// Channel numbers are message numbers, i.e. MSG_TABLE and MSG_PRODUCT.
|
|
|
|
//
|
|
|
|
// The subscription system uses a meet-in-the-middle strategy.
|
|
|
|
// With 100,000 nodes, if senders broadcast to 1000 random nodes and receivers
|
|
|
|
// subscribe to 1000 random nodes, 99.995% (1 - 0.99^1000) of messages will get through.
|
|
|
|
//
|
|
|
|
|
|
|
|
bool AnySubscribed(unsigned int nChannel)
|
|
|
|
{
|
|
|
|
if (pnodeLocalHost->IsSubscribed(nChannel))
|
|
|
|
return true;
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
foreach(CNode* pnode, vNodes)
|
|
|
|
if (pnode->IsSubscribed(nChannel))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CNode::IsSubscribed(unsigned int nChannel)
|
|
|
|
{
|
|
|
|
if (nChannel >= vfSubscribe.size())
|
|
|
|
return false;
|
|
|
|
return vfSubscribe[nChannel];
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNode::Subscribe(unsigned int nChannel, unsigned int nHops)
|
|
|
|
{
|
|
|
|
if (nChannel >= vfSubscribe.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!AnySubscribed(nChannel))
|
|
|
|
{
|
|
|
|
// Relay subscribe
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
foreach(CNode* pnode, vNodes)
|
|
|
|
if (pnode != this)
|
|
|
|
pnode->PushMessage("subscribe", nChannel, nHops);
|
|
|
|
}
|
|
|
|
|
|
|
|
vfSubscribe[nChannel] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNode::CancelSubscribe(unsigned int nChannel)
|
|
|
|
{
|
|
|
|
if (nChannel >= vfSubscribe.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Prevent from relaying cancel if wasn't subscribed
|
|
|
|
if (!vfSubscribe[nChannel])
|
|
|
|
return;
|
|
|
|
vfSubscribe[nChannel] = false;
|
|
|
|
|
|
|
|
if (!AnySubscribed(nChannel))
|
|
|
|
{
|
|
|
|
// Relay subscription cancel
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
foreach(CNode* pnode, vNodes)
|
|
|
|
if (pnode != this)
|
|
|
|
pnode->PushMessage("sub-cancel", nChannel);
|
|
|
|
|
|
|
|
// Clear memory, no longer subscribed
|
|
|
|
if (nChannel == MSG_PRODUCT)
|
|
|
|
CRITICAL_BLOCK(cs_mapProducts)
|
|
|
|
mapProducts.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CNode* FindNode(unsigned int ip)
|
|
|
|
{
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
{
|
|
|
|
foreach(CNode* pnode, vNodes)
|
|
|
|
if (pnode->addr.ip == ip)
|
|
|
|
return (pnode);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CNode* FindNode(CAddress addr)
|
|
|
|
{
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
{
|
|
|
|
foreach(CNode* pnode, vNodes)
|
|
|
|
if (pnode->addr == addr)
|
|
|
|
return (pnode);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CNode* ConnectNode(CAddress addrConnect, int64 nTimeout)
|
|
|
|
{
|
|
|
|
if (addrConnect.ip == addrLocalHost.ip)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Look for an existing connection
|
|
|
|
CNode* pnode = FindNode(addrConnect.ip);
|
|
|
|
if (pnode)
|
|
|
|
{
|
|
|
|
if (nTimeout != 0)
|
|
|
|
pnode->AddRef(nTimeout);
|
|
|
|
else
|
|
|
|
pnode->AddRef();
|
|
|
|
return pnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// debug print
|
2009-10-20 18:08:05 -07:00
|
|
|
printf("trying connection %s\n", addrConnect.ToStringLog().c_str());
|
2009-08-29 20:46:39 -07:00
|
|
|
|
|
|
|
// Connect
|
|
|
|
SOCKET hSocket;
|
|
|
|
if (ConnectSocket(addrConnect, hSocket))
|
|
|
|
{
|
|
|
|
/// debug print
|
2009-10-20 18:08:05 -07:00
|
|
|
printf("connected %s\n", addrConnect.ToStringLog().c_str());
|
2009-08-29 20:46:39 -07:00
|
|
|
|
|
|
|
// Set to nonblocking
|
2009-11-04 20:41:36 -08:00
|
|
|
#ifdef __WXMSW__
|
|
|
|
u_long nOne = 1;
|
2009-08-29 20:46:39 -07:00
|
|
|
if (ioctlsocket(hSocket, FIONBIO, &nOne) == SOCKET_ERROR)
|
|
|
|
printf("ConnectSocket() : ioctlsocket nonblocking setting failed, error %d\n", WSAGetLastError());
|
2009-11-04 20:41:36 -08:00
|
|
|
#else
|
|
|
|
if (fcntl(hSocket, F_SETFL, O_NONBLOCK) == SOCKET_ERROR)
|
|
|
|
printf("ConnectSocket() : fcntl nonblocking setting failed, error %d\n", errno);
|
|
|
|
#endif
|
2009-08-29 20:46:39 -07:00
|
|
|
|
|
|
|
// Add node
|
|
|
|
CNode* pnode = new CNode(hSocket, addrConnect, false);
|
|
|
|
if (nTimeout != 0)
|
|
|
|
pnode->AddRef(nTimeout);
|
|
|
|
else
|
|
|
|
pnode->AddRef();
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
vNodes.push_back(pnode);
|
|
|
|
|
|
|
|
CRITICAL_BLOCK(cs_mapAddresses)
|
|
|
|
mapAddresses[addrConnect.GetKey()].nLastFailed = 0;
|
|
|
|
return pnode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CRITICAL_BLOCK(cs_mapAddresses)
|
2009-11-04 20:41:36 -08:00
|
|
|
mapAddresses[addrConnect.GetKey()].nLastFailed = GetAdjustedTime();
|
2009-08-29 20:46:39 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-28 19:52:48 -07:00
|
|
|
void CNode::DoDisconnect()
|
2009-08-29 20:46:39 -07:00
|
|
|
{
|
2009-10-20 18:08:05 -07:00
|
|
|
printf("disconnecting node %s\n", addr.ToStringLog().c_str());
|
2009-08-29 20:46:39 -07:00
|
|
|
|
|
|
|
closesocket(hSocket);
|
|
|
|
|
|
|
|
// If outbound and never got version message, mark address as failed
|
2009-10-28 19:52:48 -07:00
|
|
|
if (!fInbound && !fSuccessfullyConnected)
|
2009-08-29 20:46:39 -07:00
|
|
|
CRITICAL_BLOCK(cs_mapAddresses)
|
2009-11-04 20:41:36 -08:00
|
|
|
mapAddresses[addr.GetKey()].nLastFailed = GetAdjustedTime();
|
2009-08-29 20:46:39 -07:00
|
|
|
|
|
|
|
// All of a nodes broadcasts and subscriptions are automatically torn down
|
|
|
|
// when it goes down, so a node has to stay up to keep its broadcast going.
|
|
|
|
|
|
|
|
CRITICAL_BLOCK(cs_mapProducts)
|
|
|
|
for (map<uint256, CProduct>::iterator mi = mapProducts.begin(); mi != mapProducts.end();)
|
|
|
|
AdvertRemoveSource(this, MSG_PRODUCT, 0, (*(mi++)).second);
|
|
|
|
|
|
|
|
// Cancel subscriptions
|
|
|
|
for (unsigned int nChannel = 0; nChannel < vfSubscribe.size(); nChannel++)
|
|
|
|
if (vfSubscribe[nChannel])
|
|
|
|
CancelSubscribe(nChannel);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ThreadSocketHandler(void* parg)
|
|
|
|
{
|
|
|
|
IMPLEMENT_RANDOMIZE_STACK(ThreadSocketHandler(parg));
|
|
|
|
|
|
|
|
loop
|
|
|
|
{
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[0]++;
|
2009-08-29 20:46:39 -07:00
|
|
|
CheckForShutdown(0);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ThreadSocketHandler2(parg);
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[0]--;
|
2009-10-20 18:08:05 -07:00
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[0]--;
|
2009-10-20 18:08:05 -07:00
|
|
|
PrintException(&e, "ThreadSocketHandler()");
|
|
|
|
} catch (...) {
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[0]--;
|
2009-10-20 18:08:05 -07:00
|
|
|
PrintException(NULL, "ThreadSocketHandler()");
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
|
|
|
Sleep(5000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadSocketHandler2(void* parg)
|
|
|
|
{
|
|
|
|
printf("ThreadSocketHandler started\n");
|
|
|
|
list<CNode*> vNodesDisconnected;
|
|
|
|
int nPrevNodeCount = 0;
|
|
|
|
|
|
|
|
loop
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Disconnect nodes
|
|
|
|
//
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
{
|
|
|
|
// Disconnect unused nodes
|
|
|
|
vector<CNode*> vNodesCopy = vNodes;
|
|
|
|
foreach(CNode* pnode, vNodesCopy)
|
|
|
|
{
|
|
|
|
if (pnode->ReadyToDisconnect() && pnode->vRecv.empty() && pnode->vSend.empty())
|
|
|
|
{
|
|
|
|
// remove from vNodes
|
|
|
|
vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
|
2009-10-28 19:52:48 -07:00
|
|
|
pnode->DoDisconnect();
|
2009-08-29 20:46:39 -07:00
|
|
|
|
|
|
|
// hold in disconnected pool until all refs are released
|
|
|
|
pnode->nReleaseTime = max(pnode->nReleaseTime, GetTime() + 5 * 60);
|
|
|
|
if (pnode->fNetworkNode)
|
|
|
|
pnode->Release();
|
|
|
|
vNodesDisconnected.push_back(pnode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete disconnected nodes
|
|
|
|
list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
|
|
|
|
foreach(CNode* pnode, vNodesDisconnectedCopy)
|
|
|
|
{
|
|
|
|
// wait until threads are done using it
|
|
|
|
if (pnode->GetRefCount() <= 0)
|
|
|
|
{
|
|
|
|
bool fDelete = false;
|
|
|
|
TRY_CRITICAL_BLOCK(pnode->cs_vSend)
|
|
|
|
TRY_CRITICAL_BLOCK(pnode->cs_vRecv)
|
|
|
|
TRY_CRITICAL_BLOCK(pnode->cs_mapRequests)
|
|
|
|
TRY_CRITICAL_BLOCK(pnode->cs_inventory)
|
|
|
|
fDelete = true;
|
|
|
|
if (fDelete)
|
|
|
|
{
|
|
|
|
vNodesDisconnected.remove(pnode);
|
|
|
|
delete pnode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (vNodes.size() != nPrevNodeCount)
|
|
|
|
{
|
|
|
|
nPrevNodeCount = vNodes.size();
|
|
|
|
MainFrameRepaint();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Find which sockets have data to receive
|
|
|
|
//
|
|
|
|
struct timeval timeout;
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 50000; // frequency to poll pnode->vSend
|
|
|
|
|
2009-11-04 20:41:36 -08:00
|
|
|
fd_set fdsetRecv;
|
|
|
|
fd_set fdsetSend;
|
2009-08-29 20:46:39 -07:00
|
|
|
FD_ZERO(&fdsetRecv);
|
|
|
|
FD_ZERO(&fdsetSend);
|
|
|
|
SOCKET hSocketMax = 0;
|
|
|
|
FD_SET(hListenSocket, &fdsetRecv);
|
|
|
|
hSocketMax = max(hSocketMax, hListenSocket);
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
{
|
|
|
|
foreach(CNode* pnode, vNodes)
|
|
|
|
{
|
|
|
|
FD_SET(pnode->hSocket, &fdsetRecv);
|
|
|
|
hSocketMax = max(hSocketMax, pnode->hSocket);
|
|
|
|
TRY_CRITICAL_BLOCK(pnode->cs_vSend)
|
|
|
|
if (!pnode->vSend.empty())
|
|
|
|
FD_SET(pnode->hSocket, &fdsetSend);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[0]--;
|
2009-08-29 20:46:39 -07:00
|
|
|
int nSelect = select(hSocketMax + 1, &fdsetRecv, &fdsetSend, NULL, &timeout);
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[0]++;
|
2009-08-29 20:46:39 -07:00
|
|
|
CheckForShutdown(0);
|
|
|
|
if (nSelect == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
int nErr = WSAGetLastError();
|
|
|
|
printf("select failed: %d\n", nErr);
|
|
|
|
for (int i = 0; i <= hSocketMax; i++)
|
|
|
|
{
|
|
|
|
FD_SET(i, &fdsetRecv);
|
|
|
|
FD_SET(i, &fdsetSend);
|
|
|
|
}
|
|
|
|
Sleep(timeout.tv_usec/1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
//// debug print
|
|
|
|
//foreach(CNode* pnode, vNodes)
|
|
|
|
//{
|
|
|
|
// printf("vRecv = %-5d ", pnode->vRecv.size());
|
|
|
|
// printf("vSend = %-5d ", pnode->vSend.size());
|
|
|
|
//}
|
|
|
|
//printf("\n");
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Accept new connections
|
|
|
|
//
|
|
|
|
if (FD_ISSET(hListenSocket, &fdsetRecv))
|
|
|
|
{
|
|
|
|
struct sockaddr_in sockaddr;
|
2009-11-04 20:41:36 -08:00
|
|
|
#ifdef __WXMSW__
|
2009-08-29 20:46:39 -07:00
|
|
|
int len = sizeof(sockaddr);
|
2009-11-04 20:41:36 -08:00
|
|
|
#else
|
|
|
|
socklen_t len = sizeof(sockaddr);
|
|
|
|
#endif
|
2009-08-29 20:46:39 -07:00
|
|
|
SOCKET hSocket = accept(hListenSocket, (struct sockaddr*)&sockaddr, &len);
|
|
|
|
CAddress addr(sockaddr);
|
|
|
|
if (hSocket == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
if (WSAGetLastError() != WSAEWOULDBLOCK)
|
|
|
|
printf("ERROR ThreadSocketHandler accept failed: %d\n", WSAGetLastError());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-20 18:08:05 -07:00
|
|
|
printf("accepted connection %s\n", addr.ToStringLog().c_str());
|
2009-08-29 20:46:39 -07:00
|
|
|
CNode* pnode = new CNode(hSocket, addr, true);
|
|
|
|
pnode->AddRef();
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
vNodes.push_back(pnode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Service each socket
|
|
|
|
//
|
|
|
|
vector<CNode*> vNodesCopy;
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
vNodesCopy = vNodes;
|
|
|
|
foreach(CNode* pnode, vNodesCopy)
|
|
|
|
{
|
|
|
|
CheckForShutdown(0);
|
|
|
|
SOCKET hSocket = pnode->hSocket;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Receive
|
|
|
|
//
|
|
|
|
if (FD_ISSET(hSocket, &fdsetRecv))
|
|
|
|
{
|
|
|
|
TRY_CRITICAL_BLOCK(pnode->cs_vRecv)
|
|
|
|
{
|
|
|
|
CDataStream& vRecv = pnode->vRecv;
|
|
|
|
unsigned int nPos = vRecv.size();
|
|
|
|
|
|
|
|
// typical socket buffer is 8K-64K
|
|
|
|
const unsigned int nBufSize = 0x10000;
|
|
|
|
vRecv.resize(nPos + nBufSize);
|
|
|
|
int nBytes = recv(hSocket, &vRecv[nPos], nBufSize, 0);
|
|
|
|
vRecv.resize(nPos + max(nBytes, 0));
|
|
|
|
if (nBytes == 0)
|
|
|
|
{
|
|
|
|
// socket closed gracefully
|
|
|
|
if (!pnode->fDisconnect)
|
|
|
|
printf("recv: socket closed\n");
|
|
|
|
pnode->fDisconnect = true;
|
|
|
|
}
|
|
|
|
else if (nBytes < 0)
|
|
|
|
{
|
|
|
|
// socket error
|
|
|
|
int nErr = WSAGetLastError();
|
|
|
|
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
|
|
|
|
{
|
|
|
|
if (!pnode->fDisconnect)
|
|
|
|
printf("recv failed: %d\n", nErr);
|
|
|
|
pnode->fDisconnect = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Send
|
|
|
|
//
|
|
|
|
if (FD_ISSET(hSocket, &fdsetSend))
|
|
|
|
{
|
|
|
|
TRY_CRITICAL_BLOCK(pnode->cs_vSend)
|
|
|
|
{
|
|
|
|
CDataStream& vSend = pnode->vSend;
|
|
|
|
if (!vSend.empty())
|
|
|
|
{
|
|
|
|
int nBytes = send(hSocket, &vSend[0], vSend.size(), 0);
|
|
|
|
if (nBytes > 0)
|
|
|
|
{
|
|
|
|
vSend.erase(vSend.begin(), vSend.begin() + nBytes);
|
|
|
|
}
|
|
|
|
else if (nBytes == 0)
|
|
|
|
{
|
|
|
|
if (pnode->ReadyToDisconnect())
|
|
|
|
pnode->vSend.clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("send error %d\n", nBytes);
|
|
|
|
if (pnode->ReadyToDisconnect())
|
|
|
|
pnode->vSend.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Sleep(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ThreadOpenConnections(void* parg)
|
|
|
|
{
|
|
|
|
IMPLEMENT_RANDOMIZE_STACK(ThreadOpenConnections(parg));
|
|
|
|
|
|
|
|
loop
|
|
|
|
{
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[1]++;
|
2009-08-29 20:46:39 -07:00
|
|
|
CheckForShutdown(1);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ThreadOpenConnections2(parg);
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[1]--;
|
2009-10-20 18:08:05 -07:00
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[1]--;
|
2009-10-20 18:08:05 -07:00
|
|
|
PrintException(&e, "ThreadOpenConnections()");
|
|
|
|
} catch (...) {
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[1]--;
|
2009-10-20 18:08:05 -07:00
|
|
|
PrintException(NULL, "ThreadOpenConnections()");
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
|
|
|
Sleep(5000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadOpenConnections2(void* parg)
|
|
|
|
{
|
|
|
|
printf("ThreadOpenConnections started\n");
|
|
|
|
|
2009-10-28 19:52:48 -07:00
|
|
|
// Connect to one specified address
|
2009-10-31 18:16:51 -07:00
|
|
|
while (mapArgs.count("-connect"))
|
2009-10-28 19:52:48 -07:00
|
|
|
{
|
2009-10-31 18:16:51 -07:00
|
|
|
OpenNetworkConnection(CAddress(mapArgs["-connect"]));
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
Sleep(1000);
|
|
|
|
CheckForShutdown(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to manually added nodes first
|
|
|
|
if (mapArgs.count("-addnode"))
|
|
|
|
{
|
|
|
|
foreach(string strAddr, mapMultiArgs["-addnode"])
|
|
|
|
{
|
|
|
|
CAddress addr(strAddr, NODE_NETWORK);
|
|
|
|
if (addr.IsValid())
|
|
|
|
{
|
|
|
|
OpenNetworkConnection(addr);
|
|
|
|
Sleep(1000);
|
|
|
|
CheckForShutdown(1);
|
|
|
|
}
|
|
|
|
}
|
2009-10-28 19:52:48 -07:00
|
|
|
}
|
|
|
|
|
2009-08-29 20:46:39 -07:00
|
|
|
// Initiate network connections
|
|
|
|
loop
|
|
|
|
{
|
|
|
|
// Wait
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[1]--;
|
2009-08-29 20:46:39 -07:00
|
|
|
Sleep(500);
|
2009-11-04 20:41:36 -08:00
|
|
|
const int nMaxConnections = 15;
|
2009-08-29 20:46:39 -07:00
|
|
|
while (vNodes.size() >= nMaxConnections || vNodes.size() >= mapAddresses.size())
|
|
|
|
{
|
|
|
|
CheckForShutdown(1);
|
|
|
|
Sleep(2000);
|
|
|
|
}
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[1]++;
|
2009-08-29 20:46:39 -07:00
|
|
|
CheckForShutdown(1);
|
|
|
|
|
|
|
|
//
|
2009-11-04 20:41:36 -08:00
|
|
|
// Choose an address to connect to based on most recently seen
|
2009-08-29 20:46:39 -07:00
|
|
|
//
|
2009-11-04 20:41:36 -08:00
|
|
|
CAddress addrConnect;
|
|
|
|
int64 nBestTime = 0;
|
|
|
|
int64 nDelay = ((60 * 60) << vNodes.size());
|
|
|
|
if (vNodes.size() >= 3)
|
|
|
|
nDelay *= 4;
|
|
|
|
if (nGotIRCAddresses > 0)
|
|
|
|
nDelay *= 100;
|
|
|
|
|
|
|
|
// Do this here so we don't have to critsect vNodes inside mapAddresses critsect
|
|
|
|
set<unsigned int> setConnected;
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
foreach(CNode* pnode, vNodes)
|
|
|
|
setConnected.insert(pnode->addr.ip);
|
2009-08-29 20:46:39 -07:00
|
|
|
|
|
|
|
CRITICAL_BLOCK(cs_mapAddresses)
|
|
|
|
{
|
|
|
|
foreach(const PAIRTYPE(vector<unsigned char>, CAddress)& item, mapAddresses)
|
|
|
|
{
|
|
|
|
const CAddress& addr = item.second;
|
2009-11-04 20:41:36 -08:00
|
|
|
if (!addr.IsIPv4() || !addr.IsValid() || setConnected.count(addr.ip))
|
2009-08-29 20:46:39 -07:00
|
|
|
continue;
|
|
|
|
|
2009-11-04 20:41:36 -08:00
|
|
|
// Limit retry frequency
|
|
|
|
if (GetAdjustedTime() < addr.nLastFailed + nDelay)
|
|
|
|
continue;
|
2009-08-29 20:46:39 -07:00
|
|
|
|
2009-11-04 20:41:36 -08:00
|
|
|
// Try again only after all addresses had a first attempt
|
|
|
|
int64 nTime = addr.nTime;
|
|
|
|
if (addr.nLastFailed > addr.nTime)
|
|
|
|
nTime -= 365 * 24 * 60 * 60;
|
2009-08-29 20:46:39 -07:00
|
|
|
|
2009-11-04 20:41:36 -08:00
|
|
|
// Randomize the order a little, putting the standard port first
|
|
|
|
nTime += GetRand(1 * 60 * 60);
|
|
|
|
if (addr.port != DEFAULT_PORT)
|
|
|
|
nTime -= 1 * 60 * 60;
|
2009-08-29 20:46:39 -07:00
|
|
|
|
2009-11-04 20:41:36 -08:00
|
|
|
if (nTime > nBestTime)
|
|
|
|
{
|
|
|
|
nBestTime = nTime;
|
|
|
|
addrConnect = addr;
|
|
|
|
}
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-04 20:41:36 -08:00
|
|
|
if (addrConnect.IsValid())
|
|
|
|
OpenNetworkConnection(addrConnect);
|
2009-10-28 19:52:48 -07:00
|
|
|
}
|
|
|
|
}
|
2009-08-29 20:46:39 -07:00
|
|
|
|
2009-10-28 19:52:48 -07:00
|
|
|
bool OpenNetworkConnection(const CAddress& addrConnect)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Initiate outbound network connection
|
|
|
|
//
|
|
|
|
CheckForShutdown(1);
|
|
|
|
if (addrConnect.ip == addrLocalHost.ip || !addrConnect.IsIPv4() || FindNode(addrConnect.ip))
|
|
|
|
return false;
|
2009-08-29 20:46:39 -07:00
|
|
|
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[1]--;
|
|
|
|
CNode* pnode = ConnectNode(addrConnect);
|
|
|
|
vnThreadsRunning[1]++;
|
|
|
|
CheckForShutdown(1);
|
|
|
|
if (!pnode)
|
|
|
|
return false;
|
|
|
|
pnode->fNetworkNode = true;
|
2009-08-29 20:46:39 -07:00
|
|
|
|
2009-10-28 19:52:48 -07:00
|
|
|
if (addrLocalHost.IsRoutable() && !fUseProxy)
|
|
|
|
{
|
|
|
|
// Advertise our address
|
|
|
|
vector<CAddress> vAddrToSend;
|
|
|
|
vAddrToSend.push_back(addrLocalHost);
|
|
|
|
pnode->PushMessage("addr", vAddrToSend);
|
|
|
|
}
|
2009-08-29 20:46:39 -07:00
|
|
|
|
2009-10-28 19:52:48 -07:00
|
|
|
// Get as many addresses as we can
|
|
|
|
pnode->PushMessage("getaddr");
|
|
|
|
pnode->fGetAddr = true; // don't relay the results of the getaddr
|
2009-08-29 20:46:39 -07:00
|
|
|
|
2009-10-28 19:52:48 -07:00
|
|
|
////// should the one on the receiving end do this too?
|
|
|
|
// Subscribe our local subscription list
|
|
|
|
const unsigned int nHops = 0;
|
|
|
|
for (unsigned int nChannel = 0; nChannel < pnodeLocalHost->vfSubscribe.size(); nChannel++)
|
|
|
|
if (pnodeLocalHost->vfSubscribe[nChannel])
|
|
|
|
pnode->PushMessage("subscribe", nChannel, nHops);
|
|
|
|
|
|
|
|
return true;
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ThreadMessageHandler(void* parg)
|
|
|
|
{
|
|
|
|
IMPLEMENT_RANDOMIZE_STACK(ThreadMessageHandler(parg));
|
|
|
|
|
|
|
|
loop
|
|
|
|
{
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[2]++;
|
2009-08-29 20:46:39 -07:00
|
|
|
CheckForShutdown(2);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ThreadMessageHandler2(parg);
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[2]--;
|
2009-10-20 18:08:05 -07:00
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[2]--;
|
2009-10-20 18:08:05 -07:00
|
|
|
PrintException(&e, "ThreadMessageHandler()");
|
|
|
|
} catch (...) {
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[2]--;
|
2009-10-20 18:08:05 -07:00
|
|
|
PrintException(NULL, "ThreadMessageHandler()");
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
|
|
|
Sleep(5000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadMessageHandler2(void* parg)
|
|
|
|
{
|
|
|
|
printf("ThreadMessageHandler started\n");
|
|
|
|
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
|
|
|
|
loop
|
|
|
|
{
|
|
|
|
// Poll the connected nodes for messages
|
|
|
|
vector<CNode*> vNodesCopy;
|
|
|
|
CRITICAL_BLOCK(cs_vNodes)
|
|
|
|
vNodesCopy = vNodes;
|
|
|
|
foreach(CNode* pnode, vNodesCopy)
|
|
|
|
{
|
|
|
|
pnode->AddRef();
|
|
|
|
|
|
|
|
// Receive messages
|
|
|
|
TRY_CRITICAL_BLOCK(pnode->cs_vRecv)
|
|
|
|
ProcessMessages(pnode);
|
|
|
|
|
|
|
|
// Send messages
|
|
|
|
TRY_CRITICAL_BLOCK(pnode->cs_vSend)
|
|
|
|
SendMessages(pnode);
|
|
|
|
|
|
|
|
pnode->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait and allow messages to bunch up
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[2]--;
|
2009-08-29 20:46:39 -07:00
|
|
|
Sleep(100);
|
2009-10-28 19:52:48 -07:00
|
|
|
vnThreadsRunning[2]++;
|
2009-08-29 20:46:39 -07:00
|
|
|
CheckForShutdown(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool StartNode(string& strError)
|
|
|
|
{
|
2009-10-31 18:16:51 -07:00
|
|
|
if (pnodeLocalHost == NULL)
|
|
|
|
pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress("127.0.0.1", nLocalServices));
|
2009-08-29 20:46:39 -07:00
|
|
|
strError = "";
|
|
|
|
|
2009-11-04 20:41:36 -08:00
|
|
|
#ifdef __WXMSW__
|
2009-08-29 20:46:39 -07:00
|
|
|
// Sockets startup
|
|
|
|
WSADATA wsadata;
|
|
|
|
int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
|
|
|
|
if (ret != NO_ERROR)
|
|
|
|
{
|
|
|
|
strError = strprintf("Error: TCP/IP socket library failed to start (WSAStartup returned error %d)", ret);
|
|
|
|
printf("%s\n", strError.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-04 20:41:36 -08:00
|
|
|
#endif
|
2009-08-29 20:46:39 -07:00
|
|
|
|
|
|
|
// Get local host ip
|
|
|
|
char pszHostName[255];
|
|
|
|
if (gethostname(pszHostName, 255) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
strError = strprintf("Error: Unable to get IP address of this computer (gethostname returned error %d)", WSAGetLastError());
|
|
|
|
printf("%s\n", strError.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct hostent* phostent = gethostbyname(pszHostName);
|
|
|
|
if (!phostent)
|
|
|
|
{
|
|
|
|
strError = strprintf("Error: Unable to get IP address of this computer (gethostbyname returned error %d)", WSAGetLastError());
|
|
|
|
printf("%s\n", strError.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
addrLocalHost = CAddress(*(long*)(phostent->h_addr_list[0]),
|
|
|
|
DEFAULT_PORT,
|
|
|
|
nLocalServices);
|
|
|
|
printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str());
|
|
|
|
|
|
|
|
// Create socket for listening for incoming connections
|
2009-10-28 19:52:48 -07:00
|
|
|
hListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2009-08-29 20:46:39 -07:00
|
|
|
if (hListenSocket == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %d)", WSAGetLastError());
|
|
|
|
printf("%s\n", strError.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set to nonblocking, incoming connections will also inherit this
|
2009-11-04 20:41:36 -08:00
|
|
|
#ifdef __WXMSW__
|
2009-08-29 20:46:39 -07:00
|
|
|
u_long nOne = 1;
|
|
|
|
if (ioctlsocket(hListenSocket, FIONBIO, &nOne) == SOCKET_ERROR)
|
2009-11-04 20:41:36 -08:00
|
|
|
#else
|
|
|
|
if (fcntl(hListenSocket, F_SETFL, O_NONBLOCK) == SOCKET_ERROR)
|
|
|
|
#endif
|
2009-08-29 20:46:39 -07:00
|
|
|
{
|
2009-11-04 20:41:36 -08:00
|
|
|
strError = strprintf("Error: Couldn't set properties on socket for incoming connections (error %d)", WSAGetLastError());
|
2009-08-29 20:46:39 -07:00
|
|
|
printf("%s\n", strError.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The sockaddr_in structure specifies the address family,
|
|
|
|
// IP address, and port for the socket that is being bound
|
|
|
|
int nRetryLimit = 15;
|
|
|
|
struct sockaddr_in sockaddr = addrLocalHost.GetSockAddr();
|
2009-11-04 20:41:36 -08:00
|
|
|
if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
|
2009-08-29 20:46:39 -07:00
|
|
|
{
|
|
|
|
int nErr = WSAGetLastError();
|
|
|
|
if (nErr == WSAEADDRINUSE)
|
|
|
|
strError = strprintf("Error: Unable to bind to port %s on this computer. The program is probably already running.", addrLocalHost.ToString().c_str());
|
|
|
|
else
|
|
|
|
strError = strprintf("Error: Unable to bind to port %s on this computer (bind returned error %d)", addrLocalHost.ToString().c_str(), nErr);
|
|
|
|
printf("%s\n", strError.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-31 18:16:51 -07:00
|
|
|
printf("bound to addrLocalHost = %s\n", addrLocalHost.ToString().c_str());
|
2009-08-29 20:46:39 -07:00
|
|
|
|
|
|
|
// Listen for incoming connections
|
|
|
|
if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
strError = strprintf("Error: Listening for incoming connections failed (listen returned error %d)", WSAGetLastError());
|
|
|
|
printf("%s\n", strError.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get our external IP address for incoming connections
|
2009-10-28 19:52:48 -07:00
|
|
|
if (fUseProxy)
|
2009-08-29 20:46:39 -07:00
|
|
|
{
|
2009-10-28 19:52:48 -07:00
|
|
|
// Proxies can't take incoming connections
|
|
|
|
addrLocalHost.ip = CAddress("0.0.0.0").ip;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (addrIncoming.ip)
|
|
|
|
addrLocalHost.ip = addrIncoming.ip;
|
|
|
|
|
|
|
|
if (GetMyExternalIP(addrLocalHost.ip))
|
|
|
|
{
|
|
|
|
addrIncoming = addrLocalHost;
|
|
|
|
CWalletDB().WriteSetting("addrIncoming", addrIncoming);
|
|
|
|
}
|
2009-08-29 20:46:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get addresses from IRC and advertise ours
|
|
|
|
if (_beginthread(ThreadIRCSeed, 0, NULL) == -1)
|
|
|
|
printf("Error: _beginthread(ThreadIRCSeed) failed\n");
|
|
|
|
|
|
|
|
//
|
|
|
|
// Start threads
|
|
|
|
//
|
2009-10-28 19:52:48 -07:00
|
|
|
if (_beginthread(ThreadSocketHandler, 0, NULL) == -1)
|
2009-08-29 20:46:39 -07:00
|
|
|
{
|
|
|
|
strError = "Error: _beginthread(ThreadSocketHandler) failed";
|
|
|
|
printf("%s\n", strError.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_beginthread(ThreadOpenConnections, 0, NULL) == -1)
|
|
|
|
{
|
|
|
|
strError = "Error: _beginthread(ThreadOpenConnections) failed";
|
|
|
|
printf("%s\n", strError.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_beginthread(ThreadMessageHandler, 0, NULL) == -1)
|
|
|
|
{
|
|
|
|
strError = "Error: _beginthread(ThreadMessageHandler) failed";
|
|
|
|
printf("%s\n", strError.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StopNode()
|
|
|
|
{
|
|
|
|
printf("StopNode()\n");
|
|
|
|
fShutdown = true;
|
|
|
|
nTransactionsUpdated++;
|
|
|
|
int64 nStart = GetTime();
|
2009-10-20 18:08:05 -07:00
|
|
|
while (vnThreadsRunning[0] || vnThreadsRunning[2] || vnThreadsRunning[3])
|
2009-08-29 20:46:39 -07:00
|
|
|
{
|
|
|
|
if (GetTime() - nStart > 15)
|
|
|
|
break;
|
|
|
|
Sleep(20);
|
|
|
|
}
|
2009-10-20 18:08:05 -07:00
|
|
|
if (vnThreadsRunning[0]) printf("ThreadSocketHandler still running\n");
|
|
|
|
if (vnThreadsRunning[1]) printf("ThreadOpenConnections still running\n");
|
|
|
|
if (vnThreadsRunning[2]) printf("ThreadMessageHandler still running\n");
|
|
|
|
if (vnThreadsRunning[3]) printf("ThreadBitcoinMiner still running\n");
|
|
|
|
while (vnThreadsRunning[2])
|
2009-08-29 20:46:39 -07:00
|
|
|
Sleep(20);
|
|
|
|
Sleep(50);
|
|
|
|
|
|
|
|
// Sockets shutdown
|
2009-11-04 20:41:36 -08:00
|
|
|
#ifdef __WXMSW__
|
2009-08-29 20:46:39 -07:00
|
|
|
WSACleanup();
|
2009-11-04 20:41:36 -08:00
|
|
|
#endif
|
2009-08-29 20:46:39 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckForShutdown(int n)
|
|
|
|
{
|
|
|
|
if (fShutdown)
|
|
|
|
{
|
|
|
|
if (n != -1)
|
2009-10-28 19:52:48 -07:00
|
|
|
if (--vnThreadsRunning[n] < 0)
|
|
|
|
vnThreadsRunning[n] = 0;
|
2009-08-29 20:46:39 -07:00
|
|
|
if (n == 0)
|
2009-10-28 19:52:48 -07:00
|
|
|
{
|
2009-08-29 20:46:39 -07:00
|
|
|
foreach(CNode* pnode, vNodes)
|
|
|
|
closesocket(pnode->hSocket);
|
2009-10-28 19:52:48 -07:00
|
|
|
closesocket(hListenSocket);
|
|
|
|
}
|
|
|
|
printf("Thread %d exiting\n", n);
|
2009-08-29 20:46:39 -07:00
|
|
|
_endthread();
|
|
|
|
}
|
|
|
|
}
|