Network decentralizatoin and bug fixes

This commit is contained in:
Michael Toutonghi
2018-08-09 15:32:48 -07:00
parent 5034d1c134
commit 0ba2065183
12 changed files with 83 additions and 906 deletions

View File

@@ -48,7 +48,8 @@
using namespace std;
namespace {
const int MAX_OUTBOUND_CONNECTIONS = 8;
const int MAX_OUTBOUND_CONNECTIONS = 4;
const int MAX_INBOUND_FROMIP = 3;
struct ListenSocket {
SOCKET socket;
@@ -61,6 +62,8 @@ namespace {
//
// Global state variables
//
extern uint16_t ASSETCHAINS_P2PPORT;
bool fDiscover = true;
bool fListen = true;
uint64_t nLocalServices = NODE_NETWORK;
@@ -907,11 +910,21 @@ static void AcceptConnection(const ListenSocket& hListenSocket) {
LogPrintf("Warning: Unknown socket family\n");
bool whitelisted = hListenSocket.whitelisted || CNode::IsWhitelistedRange(addr);
int nInboundThisIP = 0;
{
LOCK(cs_vNodes);
struct sockaddr_storage tmpsockaddr;
socklen_t tmplen = sizeof(sockaddr);
BOOST_FOREACH(CNode* pnode, vNodes)
{
if (pnode->fInbound)
{
nInbound++;
if (pnode->addr.GetSockAddr((struct sockaddr*)&tmpsockaddr, &tmplen) && (tmplen == len) && (memcmp(&sockaddr, &tmpsockaddr, tmplen) == 0))
nInboundThisIP++;
}
}
}
if (hSocket == INVALID_SOCKET)
@@ -946,6 +959,14 @@ static void AcceptConnection(const ListenSocket& hListenSocket) {
}
}
if (nInboundThisIP >= MAX_INBOUND_FROMIP)
{
// No connection to evict, disconnect the new connection
LogPrint("net", "too many connections from %s, connection refused\n", addr.ToString());
CloseSocket(hSocket);
return;
}
// According to the internet TCP_NODELAY is not carried into accepted sockets
// on all platforms. Set it again here just to be sure.
int set = 1;
@@ -1267,8 +1288,12 @@ void ThreadDNSAddressSeed()
int nOneDay = 24*3600;
CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()));
addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
vAdd.push_back(addr);
found++;
// only add seeds with the right port
if (addr.GetPort() == ASSETCHAINS_P2PPORT)
{
vAdd.push_back(addr);
found++;
}
}
}
addrman.Add(vAdd, CNetAddr(seed.name, true));
@@ -1279,16 +1304,6 @@ void ThreadDNSAddressSeed()
}
void DumpAddresses()
{
int64_t nStart = GetTimeMillis();
@@ -1587,10 +1602,6 @@ void ThreadMessageHandler()
}
bool BindListenPort(const CService &addrBind, string& strError, bool fWhitelisted)
{
strError = "";