diff --git a/src/net.h b/src/net.h --- a/src/net.h +++ b/src/net.h @@ -494,7 +494,7 @@ bool IsLimited(const CNetAddr &addr); bool AddLocal(const CService &addr, int nScore = LOCAL_NONE); bool AddLocal(const CNetAddr &addr, int nScore = LOCAL_NONE); -bool RemoveLocal(const CService &addr); +void RemoveLocal(const CService &addr); bool SeenLocal(const CService &addr); bool IsLocal(const CService &addr); bool GetLocal(CService &addr, const CNetAddr *paddrPeer = nullptr); diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -135,11 +135,10 @@ const int64_t nOneWeek = 7 * 24 * 60 * 60; std::vector vSeedsOut; vSeedsOut.reserve(vSeedsIn.size()); - for (std::vector::const_iterator i(vSeedsIn.begin()); - i != vSeedsIn.end(); ++i) { + for (const auto &seed_in : vSeedsIn) { struct in6_addr ip; - memcpy(&ip, i->addr, sizeof(ip)); - CAddress addr(CService(ip, i->port), NODE_NETWORK); + memcpy(&ip, seed_in.addr, sizeof(ip)); + CAddress addr(CService(ip, seed_in.port), NODE_NETWORK); addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek; vSeedsOut.push_back(addr); } @@ -160,7 +159,7 @@ return ret; } -int GetnScore(const CService &addr) { +static int GetnScore(const CService &addr) { LOCK(cs_mapLocalHost); if (mapLocalHost.count(addr) == LOCAL_NONE) { return 0; @@ -230,11 +229,10 @@ return AddLocal(CService(addr, GetListenPort()), nScore); } -bool RemoveLocal(const CService &addr) { +void RemoveLocal(const CService &addr) { LOCK(cs_mapLocalHost); LogPrintf("RemoveLocal(%s)\n", addr.ToString()); mapLocalHost.erase(addr); - return true; } /** @@ -288,7 +286,7 @@ CNode *CConnman::FindNode(const CNetAddr &ip) { LOCK(cs_vNodes); for (CNode *pnode : vNodes) { - if ((CNetAddr)pnode->addr == ip) { + if (static_cast(pnode->addr) == ip) { return pnode; } } @@ -298,7 +296,7 @@ CNode *CConnman::FindNode(const CSubNet &subNet) { LOCK(cs_vNodes); for (CNode *pnode : vNodes) { - if (subNet.Match((CNetAddr)pnode->addr)) { + if (subNet.Match(static_cast(pnode->addr))) { return pnode; } } @@ -318,7 +316,7 @@ CNode *CConnman::FindNode(const CService &addr) { LOCK(cs_vNodes); for (CNode *pnode : vNodes) { - if ((CService)pnode->addr == addr) { + if (static_cast(pnode->addr) == addr) { return pnode; } } @@ -327,7 +325,7 @@ bool CConnman::CheckIncomingNonce(uint64_t nonce) { LOCK(cs_vNodes); - for (CNode *pnode : vNodes) { + for (const CNode *pnode : vNodes) { if (!pnode->fSuccessfullyConnected && !pnode->fInbound && pnode->GetLocalNonce() == nonce) return false; @@ -359,7 +357,7 @@ } // Look for an existing connection - CNode *pnode = FindNode((CService)addrConnect); + CNode *pnode = FindNode(static_cast(addrConnect)); if (pnode) { LogPrintf("Failed to open new connection, already connected\n"); return nullptr; @@ -374,7 +372,7 @@ : (double)(GetAdjustedTime() - addrConnect.nTime) / 3600.0); // Connect - SOCKET hSocket; + SOCKET hSocket = INVALID_SOCKET; bool proxyConnectionFailed = false; if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, config->GetChainParams().GetDefaultPort(), @@ -395,7 +393,7 @@ // the name we used to connect in that CNode, so that future // FindNode() calls to that name catch this early. LOCK(cs_vNodes); - CNode *pnode = FindNode((CService)addrConnect); + CNode *pnode = FindNode(static_cast(addrConnect)); if (pnode) { pnode->MaybeSetAddrName(std::string(pszDest)); CloseSocket(hSocket); @@ -1122,10 +1120,9 @@ // Disconnect from the network group with the most connections NodeId evicted = vEvictionCandidates.front().id; LOCK(cs_vNodes); - for (std::vector::const_iterator it(vNodes.begin()); - it != vNodes.end(); ++it) { - if ((*it)->GetId() == evicted) { - (*it)->fDisconnect = true; + for (CNode *pnode : vNodes) { + if (pnode->GetId() == evicted) { + pnode->fDisconnect = true; return true; } } @@ -1150,7 +1147,7 @@ bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange(addr); { LOCK(cs_vNodes); - for (CNode *pnode : vNodes) { + for (const CNode *pnode : vNodes) { if (pnode->fInbound) { nInbound++; } @@ -1518,8 +1515,8 @@ (GetTimeMicros() - pnode->nPingUsecStart)); pnode->fDisconnect = true; } else if (!pnode->fSuccessfullyConnected) { - LogPrintf("version handshake timeout from %d\n", - pnode->GetId()); + LogPrint(BCLog::NET, "version handshake timeout from %d\n", + pnode->GetId()); pnode->fDisconnect = true; } } @@ -1544,11 +1541,11 @@ #ifdef USE_UPNP static CThreadInterrupt g_upnp_interrupt; static std::thread g_upnp_thread; -void ThreadMapPort() { +static void ThreadMapPort() { std::string port = strprintf("%u", GetListenPort()); - const char *multicastif = 0; - const char *minissdpdpath = 0; - struct UPNPDev *devlist = 0; + const char *multicastif = nullptr; + const char *minissdpdpath = nullptr; + struct UPNPDev *devlist = nullptr; char lanaddr[64]; #ifndef UPNPDISCOVER_SUCCESS @@ -1612,9 +1609,7 @@ } else { LogPrintf("UPnP Port Mapping successful.\n"); } - } - - while (g_upnp_interrupt.sleep_for(std::chrono::minutes(20))); + } while (g_upnp_interrupt.sleep_for(std::chrono::minutes(20))); r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0); @@ -1625,7 +1620,7 @@ } else { LogPrintf("No valid UPnP IGDs found\n"); freeUPNPDevlist(devlist); - devlist = 0; + devlist = nullptr; if (r != 0) { FreeUPNPUrls(&urls); } @@ -1693,7 +1688,7 @@ LOCK(cs_vNodes); int nRelevant = 0; - for (auto pnode : vNodes) { + for (const CNode *pnode : vNodes) { nRelevant += pnode->fSuccessfullyConnected && !pnode->fFeeler && !pnode->fOneShot && !pnode->m_manual_connection && !pnode->fInbound; @@ -1797,7 +1792,7 @@ int nOutbound = 0; { LOCK(cs_vNodes); - for (CNode *pnode : vNodes) { + for (const CNode *pnode : vNodes) { if (!pnode->fInbound && !pnode->m_manual_connection && !pnode->fFeeler && !pnode->fDisconnect && !pnode->fOneShot && pnode->fSuccessfullyConnected) { @@ -1874,7 +1869,7 @@ std::set> setConnected; { LOCK(cs_vNodes); - for (CNode *pnode : vNodes) { + for (const CNode *pnode : vNodes) { if (!pnode->fInbound && !pnode->m_manual_connection) { // Netgroups for inbound and addnode peers are not excluded // because our goal here is to not use multiple of our @@ -2454,7 +2449,7 @@ } if (clientInterface) { - clientInterface->InitMessage(_("Loading addresses...")); + clientInterface->InitMessage(_("Loading P2P addresses...")); } // Load addresses from peers.dat int64_t nStart = GetTimeMillis(); @@ -2701,9 +2696,8 @@ bool CConnman::AddNode(const std::string &strNode) { LOCK(cs_vAddedNodes); - for (std::vector::const_iterator it = vAddedNodes.begin(); - it != vAddedNodes.end(); ++it) { - if (strNode == *it) { + for (const std::string &it : vAddedNodes) { + if (strNode == it) { return false; } } @@ -2732,9 +2726,8 @@ } int nNum = 0; - for (std::vector::const_iterator it = vNodes.begin(); - it != vNodes.end(); ++it) { - if (flags & ((*it)->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT)) { + for (const auto &pnode : vNodes) { + if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT)) { nNum++; } }