|
|
|
@ -35,7 +35,7 @@ void ThreadOpenAddedConnections2(void* parg);
@@ -35,7 +35,7 @@ void ThreadOpenAddedConnections2(void* parg);
|
|
|
|
|
void ThreadMapPort2(void* parg); |
|
|
|
|
#endif |
|
|
|
|
void ThreadDNSAddressSeed2(void* parg); |
|
|
|
|
bool OpenNetworkConnection(const CAddress& addrConnect); |
|
|
|
|
bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest = NULL); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -43,7 +43,6 @@ bool OpenNetworkConnection(const CAddress& addrConnect);
@@ -43,7 +43,6 @@ bool OpenNetworkConnection(const CAddress& addrConnect);
|
|
|
|
|
// Global state variables
|
|
|
|
|
//
|
|
|
|
|
bool fClient = false; |
|
|
|
|
bool fAllowDNS = false; |
|
|
|
|
static bool fUseUPnP = false; |
|
|
|
|
uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK); |
|
|
|
|
CAddress addrLocalHost(CService("0.0.0.0", 0), nLocalServices); |
|
|
|
@ -307,6 +306,15 @@ CNode* FindNode(const CNetAddr& ip)
@@ -307,6 +306,15 @@ CNode* FindNode(const CNetAddr& ip)
|
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CNode* FindNode(std::string addrName) |
|
|
|
|
{ |
|
|
|
|
LOCK(cs_vNodes); |
|
|
|
|
BOOST_FOREACH(CNode* pnode, vNodes) |
|
|
|
|
if (pnode->addrName == addrName) |
|
|
|
|
return (pnode); |
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CNode* FindNode(const CService& addr) |
|
|
|
|
{ |
|
|
|
|
{ |
|
|
|
@ -318,8 +326,9 @@ CNode* FindNode(const CService& addr)
@@ -318,8 +326,9 @@ CNode* FindNode(const CService& addr)
|
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CNode* ConnectNode(CAddress addrConnect, int64 nTimeout) |
|
|
|
|
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, int64 nTimeout) |
|
|
|
|
{ |
|
|
|
|
if (pszDest != NULL) { |
|
|
|
|
if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost) |
|
|
|
|
return NULL; |
|
|
|
|
|
|
|
|
@ -333,20 +342,22 @@ CNode* ConnectNode(CAddress addrConnect, int64 nTimeout)
@@ -333,20 +342,22 @@ CNode* ConnectNode(CAddress addrConnect, int64 nTimeout)
|
|
|
|
|
pnode->AddRef(); |
|
|
|
|
return pnode; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// debug print
|
|
|
|
|
printf("trying connection %s lastseen=%.1fhrs\n", |
|
|
|
|
addrConnect.ToString().c_str(), |
|
|
|
|
(double)(addrConnect.nTime - GetAdjustedTime())/3600.0); |
|
|
|
|
|
|
|
|
|
addrman.Attempt(addrConnect); |
|
|
|
|
pszDest ? pszDest : addrConnect.ToString().c_str(), |
|
|
|
|
pszDest ? 0 : (double)(addrConnect.nTime - GetAdjustedTime())/3600.0); |
|
|
|
|
|
|
|
|
|
// Connect
|
|
|
|
|
SOCKET hSocket; |
|
|
|
|
if (ConnectSocket(addrConnect, hSocket)) |
|
|
|
|
if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, GetDefaultPort()) : ConnectSocket(addrConnect, hSocket)) |
|
|
|
|
{ |
|
|
|
|
addrman.Attempt(addrConnect); |
|
|
|
|
|
|
|
|
|
/// debug print
|
|
|
|
|
printf("connected %s\n", addrConnect.ToString().c_str()); |
|
|
|
|
printf("connected %s\n", pszDest ? pszDest : addrConnect.ToString().c_str()); |
|
|
|
|
|
|
|
|
|
// Set to nonblocking
|
|
|
|
|
#ifdef WIN32 |
|
|
|
@ -359,11 +370,12 @@ CNode* ConnectNode(CAddress addrConnect, int64 nTimeout)
@@ -359,11 +370,12 @@ CNode* ConnectNode(CAddress addrConnect, int64 nTimeout)
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
// Add node
|
|
|
|
|
CNode* pnode = new CNode(hSocket, addrConnect, false); |
|
|
|
|
CNode* pnode = new CNode(hSocket, addrConnect, pszDest ? pszDest : "", false); |
|
|
|
|
if (nTimeout != 0) |
|
|
|
|
pnode->AddRef(nTimeout); |
|
|
|
|
else |
|
|
|
|
pnode->AddRef(); |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
LOCK(cs_vNodes); |
|
|
|
|
vNodes.push_back(pnode); |
|
|
|
@ -389,7 +401,7 @@ void CNode::CloseSocketDisconnect()
@@ -389,7 +401,7 @@ void CNode::CloseSocketDisconnect()
|
|
|
|
|
{ |
|
|
|
|
if (fDebug) |
|
|
|
|
printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str()); |
|
|
|
|
printf("disconnecting node %s\n", addr.ToString().c_str()); |
|
|
|
|
printf("disconnecting node %s\n", addrName.c_str()); |
|
|
|
|
closesocket(hSocket); |
|
|
|
|
hSocket = INVALID_SOCKET; |
|
|
|
|
vRecv.clear(); |
|
|
|
@ -444,7 +456,7 @@ bool CNode::Misbehaving(int howmuch)
@@ -444,7 +456,7 @@ bool CNode::Misbehaving(int howmuch)
|
|
|
|
|
{ |
|
|
|
|
if (addr.IsLocal()) |
|
|
|
|
{ |
|
|
|
|
printf("Warning: local node %s misbehaving\n", addr.ToString().c_str()); |
|
|
|
|
printf("Warning: local node %s misbehaving\n", addrName.c_str()); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -458,7 +470,7 @@ bool CNode::Misbehaving(int howmuch)
@@ -458,7 +470,7 @@ bool CNode::Misbehaving(int howmuch)
|
|
|
|
|
setBanned[addr] = banTime; |
|
|
|
|
} |
|
|
|
|
CloseSocketDisconnect(); |
|
|
|
|
printf("Disconnected %s for misbehavior (score=%d)\n", addr.ToString().c_str(), nMisbehavior); |
|
|
|
|
printf("Disconnected %s for misbehavior (score=%d)\n", addrName.c_str(), nMisbehavior); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
@ -675,7 +687,7 @@ void ThreadSocketHandler2(void* parg)
@@ -675,7 +687,7 @@ void ThreadSocketHandler2(void* parg)
|
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
printf("accepted connection %s\n", addr.ToString().c_str()); |
|
|
|
|
CNode* pnode = new CNode(hSocket, addr, true); |
|
|
|
|
CNode* pnode = new CNode(hSocket, addr, "", true); |
|
|
|
|
pnode->AddRef(); |
|
|
|
|
{ |
|
|
|
|
LOCK(cs_vNodes); |
|
|
|
@ -1025,6 +1037,10 @@ void ThreadDNSAddressSeed2(void* parg)
@@ -1025,6 +1037,10 @@ void ThreadDNSAddressSeed2(void* parg)
|
|
|
|
|
printf("Loading addresses from DNS seeds (could take a while)\n"); |
|
|
|
|
|
|
|
|
|
for (unsigned int seed_idx = 0; seed_idx < ARRAYLEN(strDNSSeed); seed_idx++) { |
|
|
|
|
if (fProxyNameLookup) { |
|
|
|
|
CAddress addr; |
|
|
|
|
OpenNetworkConnection(addr, strDNSSeed[seed_idx][1]); |
|
|
|
|
} else { |
|
|
|
|
vector<CNetAddr> vaddr; |
|
|
|
|
vector<CAddress> vAdd; |
|
|
|
|
if (LookupHost(strDNSSeed[seed_idx][1], vaddr)) |
|
|
|
@ -1041,6 +1057,7 @@ void ThreadDNSAddressSeed2(void* parg)
@@ -1041,6 +1057,7 @@ void ThreadDNSAddressSeed2(void* parg)
|
|
|
|
|
addrman.Add(vAdd, CNetAddr(strDNSSeed[seed_idx][0], true)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
printf("%d addresses found from DNS seeds\n", found); |
|
|
|
|
} |
|
|
|
@ -1199,9 +1216,8 @@ void ThreadOpenConnections2(void* parg)
@@ -1199,9 +1216,8 @@ void ThreadOpenConnections2(void* parg)
|
|
|
|
|
{ |
|
|
|
|
BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"]) |
|
|
|
|
{ |
|
|
|
|
CAddress addr(CService(strAddr, GetDefaultPort(), fAllowDNS)); |
|
|
|
|
if (addr.IsValid()) |
|
|
|
|
OpenNetworkConnection(addr); |
|
|
|
|
CAddress addr(CService("0.0.0.0:0")); |
|
|
|
|
OpenNetworkConnection(addr, strAddr.c_str()); |
|
|
|
|
for (int i = 0; i < 10 && i < nLoop; i++) |
|
|
|
|
{ |
|
|
|
|
Sleep(500); |
|
|
|
@ -1325,11 +1341,25 @@ void ThreadOpenAddedConnections2(void* parg)
@@ -1325,11 +1341,25 @@ void ThreadOpenAddedConnections2(void* parg)
|
|
|
|
|
if (mapArgs.count("-addnode") == 0) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
if (fProxyNameLookup) { |
|
|
|
|
while(!fShutdown) { |
|
|
|
|
BOOST_FOREACH(string& strAddNode, mapMultiArgs["-addnode"]) { |
|
|
|
|
CAddress addr; |
|
|
|
|
OpenNetworkConnection(addr, strAddNode.c_str()); |
|
|
|
|
Sleep(500); |
|
|
|
|
} |
|
|
|
|
vnThreadsRunning[THREAD_ADDEDCONNECTIONS]--; |
|
|
|
|
Sleep(120000); // Retry every 2 minutes
|
|
|
|
|
vnThreadsRunning[THREAD_ADDEDCONNECTIONS]++; |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
vector<vector<CService> > vservAddressesToAdd(0); |
|
|
|
|
BOOST_FOREACH(string& strAddNode, mapMultiArgs["-addnode"]) |
|
|
|
|
{ |
|
|
|
|
vector<CService> vservNode(0); |
|
|
|
|
if(Lookup(strAddNode.c_str(), vservNode, GetDefaultPort(), fAllowDNS, 0)) |
|
|
|
|
if(Lookup(strAddNode.c_str(), vservNode, GetDefaultPort(), fNameLookup, 0)) |
|
|
|
|
{ |
|
|
|
|
vservAddressesToAdd.push_back(vservNode); |
|
|
|
|
{ |
|
|
|
@ -1343,7 +1373,7 @@ void ThreadOpenAddedConnections2(void* parg)
@@ -1343,7 +1373,7 @@ void ThreadOpenAddedConnections2(void* parg)
|
|
|
|
|
{ |
|
|
|
|
vector<vector<CService> > vservConnectAddresses = vservAddressesToAdd; |
|
|
|
|
// Attempt to connect to each IP for each addnode entry until at least one is successful per addnode entry
|
|
|
|
|
// (keeping in mind that addnode entries can have many IPs if fAllowDNS)
|
|
|
|
|
// (keeping in mind that addnode entries can have many IPs if fNameLookup)
|
|
|
|
|
{ |
|
|
|
|
LOCK(cs_vNodes); |
|
|
|
|
BOOST_FOREACH(CNode* pnode, vNodes) |
|
|
|
@ -1373,19 +1403,23 @@ void ThreadOpenAddedConnections2(void* parg)
@@ -1373,19 +1403,23 @@ void ThreadOpenAddedConnections2(void* parg)
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool OpenNetworkConnection(const CAddress& addrConnect) |
|
|
|
|
bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest) |
|
|
|
|
{ |
|
|
|
|
//
|
|
|
|
|
// Initiate outbound network connection
|
|
|
|
|
//
|
|
|
|
|
if (fShutdown) |
|
|
|
|
return false; |
|
|
|
|
if (!strDest) |
|
|
|
|
if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost || !addrConnect.IsIPv4() || |
|
|
|
|
FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect)) |
|
|
|
|
FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect) || |
|
|
|
|
FindNode(addrConnect.ToStringIPPort().c_str())) |
|
|
|
|
return false; |
|
|
|
|
if (strDest && FindNode(strDest)) |
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
vnThreadsRunning[THREAD_OPENCONNECTIONS]--; |
|
|
|
|
CNode* pnode = ConnectNode(addrConnect); |
|
|
|
|
CNode* pnode = ConnectNode(addrConnect, strDest); |
|
|
|
|
vnThreadsRunning[THREAD_OPENCONNECTIONS]++; |
|
|
|
|
if (fShutdown) |
|
|
|
|
return false; |
|
|
|
|