X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fnet.cpp;h=f0c6bc6817bcff1fe95fe520171160fd936dafaa;hb=HEAD;hp=75280b3d3116f6f81ad1446f79aea39575d6fb82;hpb=5098ea454db9132aa0f576921ca9d1a69429d146;p=novacoin.git diff --git a/src/net.cpp b/src/net.cpp index 75280b3..f0c6bc6 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -9,15 +9,15 @@ #include "init.h" #include "addrman.h" #include "interface.h" +#include "main.h" #include "miner.h" #include "ntp.h" +#include "random.h" #ifdef WIN32 #include #endif -using namespace std; -using namespace boost; static const int MAX_OUTBOUND_CONNECTIONS = 16; @@ -50,36 +50,46 @@ bool fClient = false; bool fDiscover = true; uint64_t nLocalServices = (fClient ? 0 : NODE_NETWORK); static CCriticalSection cs_mapLocalHost; -static map mapLocalHost; +static std::map mapLocalHost; static bool vfReachable[NET_MAX] = {}; static bool vfLimited[NET_MAX] = {}; static CNode* pnodeLocalHost = NULL; static CNode* pnodeSync = NULL; CAddress addrSeenByPeer(CService("0.0.0.0", nPortZero), nLocalServices); uint64_t nLocalHostNonce = 0; -boost::array vnThreadsRunning; +std::array vnThreadsRunning; static std::vector vhListenSocket; CAddrMan addrman; -vector vNodes; +std::vector vNodes; CCriticalSection cs_vNodes; -map mapRelay; -deque > vRelayExpiration; +std::map mapRelay; +std::deque > vRelayExpiration; CCriticalSection cs_mapRelay; -map mapAlreadyAskedFor; +std::map mapAlreadyAskedFor; -static deque vOneShots; +static std::deque vOneShots; CCriticalSection cs_vOneShots; -set setservAddNodeAddresses; +std::set setservAddNodeAddresses; CCriticalSection cs_setservAddNodeAddresses; -vector vAddedNodes; +std::vector vAddedNodes; CCriticalSection cs_vAddedNodes; static CSemaphore *semOutbound = NULL; -void AddOneShot(string strDest) +inline void RelayInventory(const CInv& inv) +{ + // Put on lists to offer to the other nodes + { + LOCK(cs_vNodes); + for (CNode* pnode : vNodes) + pnode->PushInventory(inv); + } +} + +void AddOneShot(std::string strDest) { LOCK(cs_vOneShots); vOneShots.push_back(strDest); @@ -111,7 +121,7 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer) int nBestReachability = -1; { LOCK(cs_mapLocalHost); - for (map::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++) + for (auto it = mapLocalHost.begin(); it != mapLocalHost.end(); it++) { int nScore = (*it).second.nScore; int nReachability = (*it).first.GetReachabilityFrom(paddrPeer); @@ -140,7 +150,7 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer) return ret; } -bool RecvLine(SOCKET hSocket, string& strLine) +bool RecvLine(SOCKET hSocket, std::string& strLine) { strLine.clear(); for ( ; ; ) @@ -503,6 +513,39 @@ void CNode::Cleanup() { } +void CNode::EndMessage() +{ + if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) + { + printf("dropmessages DROPPING SEND MESSAGE\n"); + AbortMessage(); + return; + } + + if (nHeaderStart < 0) { + LEAVE_CRITICAL_SECTION(cs_vSend); + return; + } + + // Set the size + uint32_t nSize = (uint32_t) vSend.size() - nMessageStart; + memcpy((char*)&vSend[nHeaderStart] + CMessageHeader::MESSAGE_SIZE_OFFSET, &nSize, sizeof(nSize)); + + // Set the checksum + uint256 hash = Hash(vSend.begin() + nMessageStart, vSend.end()); + uint32_t nChecksum = 0; + memcpy(&nChecksum, &hash, sizeof(nChecksum)); + assert(nMessageStart - nHeaderStart >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum)); + memcpy((char*)&vSend[nHeaderStart] + CMessageHeader::CHECKSUM_OFFSET, &nChecksum, sizeof(nChecksum)); + + if (fDebug) { + printf("(%d bytes)\n", nSize); + } + + nHeaderStart = -1; + nMessageStart = std::numeric_limits::max(); + LEAVE_CRITICAL_SECTION(cs_vSend); +} void CNode::PushVersion() { @@ -528,10 +571,10 @@ void CNode::PushVersion() addrMe = GetLocalAddress(&addr); } - RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce)); + GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce)); printf("send version message: version %d, blocks=%d, us=%s, them=%s, peer=%s\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString().c_str(), addrYou.ToString().c_str(), addr.ToString().c_str()); PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe, - nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector()), nBestHeight); + nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector()), nBestHeight); } @@ -637,7 +680,7 @@ void ThreadSocketHandler(void* parg) printf("ThreadSocketHandler exited\n"); } -static list vNodesDisconnected; +static std::list vNodesDisconnected; void ThreadSocketHandler2(void* parg) { @@ -651,7 +694,7 @@ void ThreadSocketHandler2(void* parg) { LOCK(cs_vNodes); // Disconnect unused nodes - vector vNodesCopy = vNodes; + std::vector vNodesCopy = vNodes; for (CNode* pnode : vNodesCopy) { if (pnode->fDisconnect || @@ -668,7 +711,7 @@ void ThreadSocketHandler2(void* parg) pnode->Cleanup(); // hold in disconnected pool until all refs are released - pnode->nReleaseTime = max(pnode->nReleaseTime, GetTime() + 15 * 60); + pnode->nReleaseTime = std::max(pnode->nReleaseTime, GetTime() + 15 * 60); if (pnode->fNetworkNode || pnode->fInbound) pnode->Release(); vNodesDisconnected.push_back(pnode); @@ -676,7 +719,7 @@ void ThreadSocketHandler2(void* parg) } // Delete disconnected nodes - list vNodesDisconnectedCopy = vNodesDisconnected; + std::list vNodesDisconnectedCopy = vNodesDisconnected; for (CNode* pnode : vNodesDisconnectedCopy) { // wait until threads are done using it @@ -690,13 +733,9 @@ void ThreadSocketHandler2(void* parg) TRY_LOCK(pnode->cs_vRecv, lockRecv); if (lockRecv) { - TRY_LOCK(pnode->cs_mapRequests, lockReq); - if (lockReq) - { - TRY_LOCK(pnode->cs_inventory, lockInv); - if (lockInv) - fDelete = true; - } + TRY_LOCK(pnode->cs_inventory, lockInv); + if (lockInv) + fDelete = true; } } } @@ -733,7 +772,7 @@ void ThreadSocketHandler2(void* parg) for (SOCKET hListenSocket : vhListenSocket) { FD_SET(hListenSocket, &fdsetRecv); - hSocketMax = max(hSocketMax, hListenSocket); + hSocketMax = std::max(hSocketMax, hListenSocket); have_fds = true; } { @@ -744,7 +783,7 @@ void ThreadSocketHandler2(void* parg) continue; FD_SET(pnode->hSocket, &fdsetRecv); FD_SET(pnode->hSocket, &fdsetError); - hSocketMax = max(hSocketMax, pnode->hSocket); + hSocketMax = std::max(hSocketMax, pnode->hSocket); have_fds = true; { TRY_LOCK(pnode->cs_vSend, lockSend); @@ -837,7 +876,7 @@ void ThreadSocketHandler2(void* parg) // // Service each socket // - vector vNodesCopy; + std::vector vNodesCopy; { LOCK(cs_vNodes); vNodesCopy = vNodes; @@ -1014,8 +1053,8 @@ void ThreadDNSAddressSeed2(void* parg) if (HaveNameProxy()) { AddOneShot(strDNSSeed[seed_idx][1]); } else { - vector vaddr; - vector vAdd; + std::vector vaddr; + std::vector vAdd; if (LookupHost(strDNSSeed[seed_idx][1], vaddr)) { for (CNetAddr& ip : vaddr) @@ -1156,7 +1195,7 @@ void ThreadOpenConnections(void* parg) void static ProcessOneShot() { - string strDest; + std::string strDest; { LOCK(cs_vOneShots); if (vOneShots.empty()) @@ -1182,7 +1221,7 @@ void ThreadOpenConnections2(void* parg) for (int64_t nLoop = 0;; nLoop++) { ProcessOneShot(); - for (string strAddr : mapMultiArgs["-connect"]) + for (std::string strAddr : mapMultiArgs["-connect"]) { CAddress addr; OpenNetworkConnection(addr, NULL, strAddr.c_str()); @@ -1256,7 +1295,7 @@ void ThreadOpenConnections2(void* parg) // Only connect out to one peer per network group (/16 for IPv4). // Do this here so we don't have to critsect vNodes inside mapAddresses critsect. int nOutbound = 0; - set > setConnected; + std::set > setConnected; { LOCK(cs_vNodes); for (CNode* pnode : vNodes) { @@ -1273,7 +1312,7 @@ void ThreadOpenConnections2(void* parg) for ( ; ; ) { // use an nUnkBias between 10 (no outgoing connections) and 90 (8 outgoing connections) - CAddress addr = addrman.Select(10 + min(nOutbound,8)*10); + CAddress addr = addrman.Select(10 + std::min(nOutbound,8)*10); // if we selected an invalid address, restart if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr)) @@ -1338,13 +1377,13 @@ void ThreadOpenAddedConnections2(void* parg) if (HaveNameProxy()) { while(!fShutdown) { - list lAddresses(0); + std::list lAddresses(0); { LOCK(cs_vAddedNodes); - for (string& strAddNode : vAddedNodes) + for (std::string& strAddNode : vAddedNodes) lAddresses.push_back(strAddNode); } - for (string& strAddNode : lAddresses) { + for (std::string& strAddNode : lAddresses) { CAddress addr; CSemaphoreGrant grant(*semOutbound); OpenNetworkConnection(addr, &grant, strAddNode.c_str()); @@ -1359,17 +1398,17 @@ void ThreadOpenAddedConnections2(void* parg) for (uint32_t i = 0; true; i++) { - list lAddresses(0); + std::list lAddresses(0); { LOCK(cs_vAddedNodes); - for (string& strAddNode : vAddedNodes) + for (std::string& strAddNode : vAddedNodes) lAddresses.push_back(strAddNode); } - list > lservAddressesToAdd(0); - for (string& strAddNode : lAddresses) + std::list > lservAddressesToAdd(0); + for (std::string& strAddNode : lAddresses) { - vector vservNode(0); + std::vector vservNode(0); if (Lookup(strAddNode.c_str(), vservNode, GetDefaultPort(), fNameLookup, 0)) { lservAddressesToAdd.push_back(vservNode); @@ -1385,7 +1424,7 @@ void ThreadOpenAddedConnections2(void* parg) { LOCK(cs_vNodes); for (CNode* pnode : vNodes) - for (list >::iterator it = lservAddressesToAdd.begin(); it != lservAddressesToAdd.end(); it++) + for (auto it = lservAddressesToAdd.begin(); it != lservAddressesToAdd.end(); it++) { for (CService& addrNode : *(it)) if (pnode->addr == addrNode) @@ -1399,7 +1438,7 @@ void ThreadOpenAddedConnections2(void* parg) break; } } - for (vector& vserv : lservAddressesToAdd) + for (std::vector& vserv : lservAddressesToAdd) { if (vserv.size() == 0) continue; @@ -1457,7 +1496,7 @@ static int64_t NodeSyncScore(const CNode *pnode) { return pnode->nLastRecv; } -void static StartSync(const vector &vNodes) { +void static StartSync(const std::vector &vNodes) { CNode *pnodeNewSync = NULL; int64_t nBestScore = 0; @@ -1511,7 +1550,7 @@ void ThreadMessageHandler2(void* parg) while (!fShutdown) { bool fHaveSyncNode = false; - vector vNodesCopy; + std::vector vNodesCopy; { LOCK(cs_vNodes); vNodesCopy = vNodes; @@ -1575,7 +1614,7 @@ void ThreadMessageHandler2(void* parg) -bool BindListenPort(const CService &addrBind, string& strError) +bool BindListenPort(const CService &addrBind, std::string& strError) { strError.clear(); int nOne = 1; @@ -1682,7 +1721,7 @@ void static Discover() char pszHostName[1000] = ""; if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR) { - vector vaddr; + std::vector vaddr; if (LookupHost(pszHostName, vaddr)) { for (const CNetAddr &addr : vaddr) @@ -1735,7 +1774,7 @@ void StartNode(void* parg) if (semOutbound == NULL) { // initialize semaphore - int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, GetArgInt("-maxconnections", 125)); + int nMaxOutbound = std::min(MAX_OUTBOUND_CONNECTIONS, GetArgInt("-maxconnections", 125)); semOutbound = new CSemaphore(nMaxOutbound); }