|
|
@ -35,7 +35,7 @@ void ThreadOpenAddedConnections2(void* parg); |
|
|
|
void ThreadMapPort2(void* parg); |
|
|
|
void ThreadMapPort2(void* parg); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
void ThreadDNSAddressSeed2(void* parg); |
|
|
|
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); |
|
|
|
// Global state variables
|
|
|
|
// Global state variables
|
|
|
|
//
|
|
|
|
//
|
|
|
|
bool fClient = false; |
|
|
|
bool fClient = false; |
|
|
|
bool fAllowDNS = false; |
|
|
|
|
|
|
|
static bool fUseUPnP = false; |
|
|
|
static bool fUseUPnP = false; |
|
|
|
uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK); |
|
|
|
uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK); |
|
|
|
CAddress addrLocalHost(CService("0.0.0.0", 0), nLocalServices); |
|
|
|
CAddress addrLocalHost(CService("0.0.0.0", 0), nLocalServices); |
|
|
@ -307,6 +306,15 @@ CNode* FindNode(const CNetAddr& ip) |
|
|
|
return NULL; |
|
|
|
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) |
|
|
|
CNode* FindNode(const CService& addr) |
|
|
|
{ |
|
|
|
{ |
|
|
|
{ |
|
|
|
{ |
|
|
@ -318,35 +326,38 @@ CNode* FindNode(const CService& addr) |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CNode* ConnectNode(CAddress addrConnect, int64 nTimeout) |
|
|
|
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, int64 nTimeout) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost) |
|
|
|
if (pszDest != NULL) { |
|
|
|
return NULL; |
|
|
|
if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost) |
|
|
|
|
|
|
|
return NULL; |
|
|
|
|
|
|
|
|
|
|
|
// Look for an existing connection
|
|
|
|
// Look for an existing connection
|
|
|
|
CNode* pnode = FindNode((CService)addrConnect); |
|
|
|
CNode* pnode = FindNode((CService)addrConnect); |
|
|
|
if (pnode) |
|
|
|
if (pnode) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (nTimeout != 0) |
|
|
|
if (nTimeout != 0) |
|
|
|
pnode->AddRef(nTimeout); |
|
|
|
pnode->AddRef(nTimeout); |
|
|
|
else |
|
|
|
else |
|
|
|
pnode->AddRef(); |
|
|
|
pnode->AddRef(); |
|
|
|
return pnode; |
|
|
|
return pnode; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// debug print
|
|
|
|
/// debug print
|
|
|
|
printf("trying connection %s lastseen=%.1fhrs\n", |
|
|
|
printf("trying connection %s lastseen=%.1fhrs\n", |
|
|
|
addrConnect.ToString().c_str(), |
|
|
|
pszDest ? pszDest : addrConnect.ToString().c_str(), |
|
|
|
(double)(addrConnect.nTime - GetAdjustedTime())/3600.0); |
|
|
|
pszDest ? 0 : (double)(addrConnect.nTime - GetAdjustedTime())/3600.0); |
|
|
|
|
|
|
|
|
|
|
|
addrman.Attempt(addrConnect); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Connect
|
|
|
|
// Connect
|
|
|
|
SOCKET hSocket; |
|
|
|
SOCKET hSocket; |
|
|
|
if (ConnectSocket(addrConnect, hSocket)) |
|
|
|
if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, GetDefaultPort()) : ConnectSocket(addrConnect, hSocket)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
addrman.Attempt(addrConnect); |
|
|
|
|
|
|
|
|
|
|
|
/// debug print
|
|
|
|
/// debug print
|
|
|
|
printf("connected %s\n", addrConnect.ToString().c_str()); |
|
|
|
printf("connected %s\n", pszDest ? pszDest : addrConnect.ToString().c_str()); |
|
|
|
|
|
|
|
|
|
|
|
// Set to nonblocking
|
|
|
|
// Set to nonblocking
|
|
|
|
#ifdef WIN32 |
|
|
|
#ifdef WIN32 |
|
|
@ -359,11 +370,12 @@ CNode* ConnectNode(CAddress addrConnect, int64 nTimeout) |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
// Add node
|
|
|
|
// Add node
|
|
|
|
CNode* pnode = new CNode(hSocket, addrConnect, false); |
|
|
|
CNode* pnode = new CNode(hSocket, addrConnect, pszDest ? pszDest : "", false); |
|
|
|
if (nTimeout != 0) |
|
|
|
if (nTimeout != 0) |
|
|
|
pnode->AddRef(nTimeout); |
|
|
|
pnode->AddRef(nTimeout); |
|
|
|
else |
|
|
|
else |
|
|
|
pnode->AddRef(); |
|
|
|
pnode->AddRef(); |
|
|
|
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
LOCK(cs_vNodes); |
|
|
|
LOCK(cs_vNodes); |
|
|
|
vNodes.push_back(pnode); |
|
|
|
vNodes.push_back(pnode); |
|
|
@ -389,7 +401,7 @@ void CNode::CloseSocketDisconnect() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (fDebug) |
|
|
|
if (fDebug) |
|
|
|
printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str()); |
|
|
|
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); |
|
|
|
closesocket(hSocket); |
|
|
|
hSocket = INVALID_SOCKET; |
|
|
|
hSocket = INVALID_SOCKET; |
|
|
|
vRecv.clear(); |
|
|
|
vRecv.clear(); |
|
|
@ -444,7 +456,7 @@ bool CNode::Misbehaving(int howmuch) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (addr.IsLocal()) |
|
|
|
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; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -458,7 +470,7 @@ bool CNode::Misbehaving(int howmuch) |
|
|
|
setBanned[addr] = banTime; |
|
|
|
setBanned[addr] = banTime; |
|
|
|
} |
|
|
|
} |
|
|
|
CloseSocketDisconnect(); |
|
|
|
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 true; |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return false; |
|
|
@ -675,7 +687,7 @@ void ThreadSocketHandler2(void* parg) |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
printf("accepted connection %s\n", addr.ToString().c_str()); |
|
|
|
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(); |
|
|
|
pnode->AddRef(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
LOCK(cs_vNodes); |
|
|
|
LOCK(cs_vNodes); |
|
|
@ -1025,20 +1037,25 @@ void ThreadDNSAddressSeed2(void* parg) |
|
|
|
printf("Loading addresses from DNS seeds (could take a while)\n"); |
|
|
|
printf("Loading addresses from DNS seeds (could take a while)\n"); |
|
|
|
|
|
|
|
|
|
|
|
for (unsigned int seed_idx = 0; seed_idx < ARRAYLEN(strDNSSeed); seed_idx++) { |
|
|
|
for (unsigned int seed_idx = 0; seed_idx < ARRAYLEN(strDNSSeed); seed_idx++) { |
|
|
|
vector<CNetAddr> vaddr; |
|
|
|
if (fProxyNameLookup) { |
|
|
|
vector<CAddress> vAdd; |
|
|
|
CAddress addr; |
|
|
|
if (LookupHost(strDNSSeed[seed_idx][1], vaddr)) |
|
|
|
OpenNetworkConnection(addr, strDNSSeed[seed_idx][1]); |
|
|
|
{ |
|
|
|
} else { |
|
|
|
BOOST_FOREACH(CNetAddr& ip, vaddr) |
|
|
|
vector<CNetAddr> vaddr; |
|
|
|
|
|
|
|
vector<CAddress> vAdd; |
|
|
|
|
|
|
|
if (LookupHost(strDNSSeed[seed_idx][1], vaddr)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int nOneDay = 24*3600; |
|
|
|
BOOST_FOREACH(CNetAddr& ip, vaddr) |
|
|
|
CAddress addr = CAddress(CService(ip, GetDefaultPort())); |
|
|
|
{ |
|
|
|
addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
|
|
|
|
int nOneDay = 24*3600; |
|
|
|
vAdd.push_back(addr); |
|
|
|
CAddress addr = CAddress(CService(ip, GetDefaultPort())); |
|
|
|
found++; |
|
|
|
addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
|
|
|
|
|
|
|
|
vAdd.push_back(addr); |
|
|
|
|
|
|
|
found++; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
addrman.Add(vAdd, CNetAddr(strDNSSeed[seed_idx][0], true)); |
|
|
|
} |
|
|
|
} |
|
|
|
addrman.Add(vAdd, CNetAddr(strDNSSeed[seed_idx][0], true)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -1199,9 +1216,8 @@ void ThreadOpenConnections2(void* parg) |
|
|
|
{ |
|
|
|
{ |
|
|
|
BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"]) |
|
|
|
BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"]) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CAddress addr(CService(strAddr, GetDefaultPort(), fAllowDNS)); |
|
|
|
CAddress addr(CService("0.0.0.0:0")); |
|
|
|
if (addr.IsValid()) |
|
|
|
OpenNetworkConnection(addr, strAddr.c_str()); |
|
|
|
OpenNetworkConnection(addr); |
|
|
|
|
|
|
|
for (int i = 0; i < 10 && i < nLoop; i++) |
|
|
|
for (int i = 0; i < 10 && i < nLoop; i++) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Sleep(500); |
|
|
|
Sleep(500); |
|
|
@ -1325,11 +1341,25 @@ void ThreadOpenAddedConnections2(void* parg) |
|
|
|
if (mapArgs.count("-addnode") == 0) |
|
|
|
if (mapArgs.count("-addnode") == 0) |
|
|
|
return; |
|
|
|
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); |
|
|
|
vector<vector<CService> > vservAddressesToAdd(0); |
|
|
|
BOOST_FOREACH(string& strAddNode, mapMultiArgs["-addnode"]) |
|
|
|
BOOST_FOREACH(string& strAddNode, mapMultiArgs["-addnode"]) |
|
|
|
{ |
|
|
|
{ |
|
|
|
vector<CService> vservNode(0); |
|
|
|
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); |
|
|
|
vservAddressesToAdd.push_back(vservNode); |
|
|
|
{ |
|
|
|
{ |
|
|
@ -1343,7 +1373,7 @@ void ThreadOpenAddedConnections2(void* parg) |
|
|
|
{ |
|
|
|
{ |
|
|
|
vector<vector<CService> > vservConnectAddresses = vservAddressesToAdd; |
|
|
|
vector<vector<CService> > vservConnectAddresses = vservAddressesToAdd; |
|
|
|
// Attempt to connect to each IP for each addnode entry until at least one is successful per addnode entry
|
|
|
|
// 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); |
|
|
|
LOCK(cs_vNodes); |
|
|
|
BOOST_FOREACH(CNode* pnode, vNodes) |
|
|
|
BOOST_FOREACH(CNode* pnode, vNodes) |
|
|
@ -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
|
|
|
|
// Initiate outbound network connection
|
|
|
|
//
|
|
|
|
//
|
|
|
|
if (fShutdown) |
|
|
|
if (fShutdown) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost || !addrConnect.IsIPv4() || |
|
|
|
if (!strDest) |
|
|
|
FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect)) |
|
|
|
if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost || !addrConnect.IsIPv4() || |
|
|
|
|
|
|
|
FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect) || |
|
|
|
|
|
|
|
FindNode(addrConnect.ToStringIPPort().c_str())) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
if (strDest && FindNode(strDest)) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
vnThreadsRunning[THREAD_OPENCONNECTIONS]--; |
|
|
|
vnThreadsRunning[THREAD_OPENCONNECTIONS]--; |
|
|
|
CNode* pnode = ConnectNode(addrConnect); |
|
|
|
CNode* pnode = ConnectNode(addrConnect, strDest); |
|
|
|
vnThreadsRunning[THREAD_OPENCONNECTIONS]++; |
|
|
|
vnThreadsRunning[THREAD_OPENCONNECTIONS]++; |
|
|
|
if (fShutdown) |
|
|
|
if (fShutdown) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|