X-Git-Url: https://repo.jachan.dev/VerusCoin.git/blobdiff_plain/0ba07d5842dcafedb35097accb7204e7a0bee8f0..b7c685b8a275c1fe6d692a4949c1c9ede2a61d48:/src/net.cpp diff --git a/src/net.cpp b/src/net.cpp index fa2e83710..1311ff9aa 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -18,7 +18,7 @@ #include "ui_interface.h" #include "crypto/common.h" -#ifdef WIN32 +#ifdef _WIN32 #include #else #include @@ -36,7 +36,7 @@ // Fix for ancient MinGW versions, that don't have defined these in ws2tcpip.h. // Todo: Can be removed when our pull-tester is upgraded to a modern MinGW version. -#ifdef WIN32 +#ifdef _WIN32 #ifndef PROTECTION_LEVEL_UNRESTRICTED #define PROTECTION_LEVEL_UNRESTRICTED 10 #endif @@ -48,7 +48,8 @@ using namespace std; namespace { - const int MAX_OUTBOUND_CONNECTIONS = 8; + const int MAX_OUTBOUND_CONNECTIONS = 5; + const int MAX_INBOUND_FROMIP = 3; struct ListenSocket { SOCKET socket; @@ -61,6 +62,10 @@ namespace { // // Global state variables // +extern uint16_t ASSETCHAINS_P2PPORT; +extern std::string NOTARY_PUBKEY; +extern int32_t USE_EXTERNAL_PUBKEY; + bool fDiscover = true; bool fListen = true; uint64_t nLocalServices = NODE_NETWORK; @@ -109,6 +114,7 @@ void AddOneShot(const std::string& strDest) unsigned short GetListenPort() { + //printf("Listenport.%u\n",Params().GetDefaultPort()); return (unsigned short)(GetArg("-port", Params().GetDefaultPort())); } @@ -435,8 +441,26 @@ void CNode::PushVersion() LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), addrYou.ToString(), id); else LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id); - PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe, - nLocalHostNonce, strSubVersion, nBestHeight, true); + + if (CConstVerusSolutionVector::activationHeight.ActiveVersion(nBestHeight + 1) >= CConstVerusSolutionVector::activationHeight.SOLUTION_VERUSV3) + { + CKeyID nodePaymentAddress; + if (USE_EXTERNAL_PUBKEY) + { + CPubKey pubKey = CPubKey(ParseHex(NOTARY_PUBKEY)); + if (pubKey.IsFullyValid()) + { + nodePaymentAddress = pubKey.GetID(); + } + LogPrint("net", "send PBaaS node payment pubkey hash -- pubkey: %s, hash: %s\n", NOTARY_PUBKEY, nodePaymentAddress.ToString()); + } + PushMessage("version", PROTOCOL_VERSION > MIN_PBAAS_VERSION ? PROTOCOL_VERSION : MIN_PBAAS_VERSION, + nLocalServices, nTime, addrYou, addrMe, nLocalHostNonce, nodePaymentAddress, strSubVersion, nBestHeight, true); + } + else + { + PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe, nLocalHostNonce, strSubVersion, nBestHeight, true); + } } @@ -911,11 +935,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) @@ -950,10 +984,18 @@ 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; -#ifdef WIN32 +#ifdef _WIN32 setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int)); #else setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int)); @@ -1271,8 +1313,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)); @@ -1283,16 +1329,6 @@ void ThreadDNSAddressSeed() } - - - - - - - - - - void DumpAddresses() { int64_t nStart = GetTimeMillis(); @@ -1355,10 +1391,12 @@ void ThreadOpenConnections() boost::this_thread::interruption_point(); // Add seed nodes if DNS seeds are all down (an infrastructure attack?). - if (addrman.size() == 0 && (GetTime() - nStart > 60)) { + // if (addrman.size() == 0 && (GetTime() - nStart > 60)) { + if (GetTime() - nStart > 60) { static bool done = false; if (!done) { - LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n"); + //LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n"); + LogPrintf("Adding fixed seed nodes.\n"); addrman.Add(convertSeed6(Params().FixedSeeds()), CNetAddr("127.0.0.1")); done = true; } @@ -1591,10 +1629,6 @@ void ThreadMessageHandler() } - - - - bool BindListenPort(const CService &addrBind, string& strError, bool fWhitelisted) { strError = ""; @@ -1625,7 +1659,7 @@ bool BindListenPort(const CService &addrBind, string& strError, bool fWhiteliste } -#ifndef WIN32 +#ifndef _WIN32 #ifdef SO_NOSIGPIPE // Different way of disabling SIGPIPE on BSD setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int)); @@ -1651,13 +1685,13 @@ bool BindListenPort(const CService &addrBind, string& strError, bool fWhiteliste // and enable it by default or not. Try to enable it, if possible. if (addrBind.IsIPv6()) { #ifdef IPV6_V6ONLY -#ifdef WIN32 +#ifdef _WIN32 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int)); #else setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int)); #endif #endif -#ifdef WIN32 +#ifdef _WIN32 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED; setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int)); #endif @@ -1667,7 +1701,7 @@ bool BindListenPort(const CService &addrBind, string& strError, bool fWhiteliste { int nErr = WSAGetLastError(); if (nErr == WSAEADDRINUSE) - strError = strprintf(_("Unable to bind to %s on this computer. Zcash is probably already running."), addrBind.ToString()); + strError = strprintf(_("Unable to bind to %s on this computer. Verus is probably already running."), addrBind.ToString()); else strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToString(), NetworkErrorString(nErr)); LogPrintf("%s\n", strError); @@ -1698,7 +1732,7 @@ void static Discover(boost::thread_group& threadGroup) if (!fDiscover) return; -#ifdef WIN32 +#ifdef _WIN32 // Get local host IP char pszHostName[256] = ""; if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR) @@ -1839,7 +1873,7 @@ public: delete pnodeLocalHost; pnodeLocalHost = NULL; -#ifdef WIN32 +#ifdef _WIN32 // Shutdown Windows Sockets WSACleanup(); #endif @@ -1847,12 +1881,6 @@ public: } instance_of_cnetcleanup; - - - - - - void RelayTransaction(const CTransaction& tx) { CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);