Skip to content

Commit

Permalink
Merge #1646: [Net] Turn net structures into dumb storage classes
Browse files Browse the repository at this point in the history
b36f743 Fix masternode service lookup (furszy)
65feb7f fixup styling (Fuzzbawls)
059ca2c net: fixup nits (Cory Fields)
5a51a5f net: Have LookupNumeric return a CService directly (Cory Fields)
acd22b7 net: narrow include scope after moving to netaddress (Cory Fields)
e5bea4b net: move CNetAddr/CService/CSubNet out of netbase (Cory Fields)
840d826 net: Add direct tests for new CSubNet constructors (Cory Fields)
63c46c6 net: Split resolving out of CSubNet (Fuzzbawls)
a801a9e net: layer 2 CService abstraction (Fuzzbawls)
94732d2 Simplify checking of masternode.conf (Fuzzbawls)
d6f81b5 RPC: Remove masternodeconnect command (Fuzzbawls)
c4fe27e net: Split resolving out of CService (Cory Fields)
502343a net: Split resolving out of CNetAddr (Cory Fields)

Pull request description:

  Backport of bitcoin#8128 as part of #1374. Based on top of #1640

  Original Description:

  > CNetAddr/CService/CSubNet lose their string constructors, they must now have lookup operations performed externally. This means that functions/classes that depend on them are no longer dependent on any particular lookup mechanism.
  >
  > From a high level: New resolvers/parsers may now be used for net operations. libbtcnet will replace what remains of netbase with async versions.

  Additional work has been done in the following commits:
  * fcef585 - Removes a useless RPC command
  * 235c33e - Refactors the masternode.conf parsing with regards to valid ports
  * 3ddd35e - Adds additional sanity checks during client init for MN port numbers and listen options.

ACKs for top commit:
  furszy:
    tested ACK b36f743
  random-zebra:
    ACK b36f743 and merging...

Tree-SHA512: 1663dc0591e3a4eeb50c1d3265ae125451bd9185e1e44e0cf36d0675fe93daf21d873ba0baa48f0e0e50b20f9313de2da5ee257eeb75c779cd07cebca61a3f99
  • Loading branch information
random-zebra committed Jun 15, 2020
2 parents 56b4147 + b36f743 commit 4c829bb
Show file tree
Hide file tree
Showing 30 changed files with 1,311 additions and 1,208 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ set(COMMON_SOURCES
./src/invalid.cpp
./src/key.cpp
./src/keystore.cpp
./src/netaddress.cpp
./src/netbase.cpp
./src/protocol.cpp
./src/pubkey.cpp
Expand Down
4 changes: 3 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ BITCOIN_CORE_H = \
messagesigner.h \
miner.h \
mruset.h \
netbase.h \
net.h \
netaddress.h \
netbase.h \
noui.h \
policy/fees.h \
pow.h \
Expand Down Expand Up @@ -400,6 +401,7 @@ libbitcoin_common_a_SOURCES = \
invalid.cpp \
key.cpp \
keystore.cpp \
netaddress.cpp \
netbase.cpp \
protocol.cpp \
pubkey.cpp \
Expand Down
20 changes: 12 additions & 8 deletions src/activemasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "masternodeconfig.h"
#include "masternodeman.h"
#include "messagesigner.h"
#include "netbase.h"
#include "protocol.h"
#include "spork.h"

//
// Bootup the Masternode, look for a 10000 PIVX input and register on the network
Expand Down Expand Up @@ -67,11 +67,14 @@ void CActiveMasternode::ManageStatus()
return;
}
} else {
service = CService(strMasterNodeAddr);
int nPort;
std::string strHost;
SplitHostPort(strMasterNodeAddr, nPort, strHost);
service = LookupNumeric(strHost.c_str(), nPort);
}

// The service needs the correct default port to work properly
if(!CMasternodeBroadcast::CheckDefaultPort(strMasterNodeAddr, errorMessage, "CActiveMasternode::ManageStatus()"))
if (!CMasternodeBroadcast::CheckDefaultPort(service, errorMessage, "CActiveMasternode::ManageStatus()"))
return;

LogPrintf("CActiveMasternode::ManageStatus() - Checking inbound connection to '%s'\n", service.ToString());
Expand Down Expand Up @@ -238,15 +241,16 @@ bool CActiveMasternode::CreateBroadcast(std::string strService, std::string strK
return false;
}

CService service = CService(strService);
int nPort;
std::string strHost;
SplitHostPort(strService, nPort, strHost);
CService _service(LookupNumeric(strHost.c_str(), nPort));

// The service needs the correct default port to work properly
if(!CMasternodeBroadcast::CheckDefaultPort(strService, errorMessage, "CActiveMasternode::CreateBroadcast()"))
if (!CMasternodeBroadcast::CheckDefaultPort(_service, errorMessage, "CActiveMasternode::CreateBroadcast()"))
return false;

addrman.Add(CAddress(service, NODE_NETWORK), CNetAddr("127.0.0.1"), 2 * 60 * 60);

return CreateBroadcast(vin, CService(strService), keyCollateralAddress, pubKeyCollateralAddress, keyMasternode, pubKeyMasternode, errorMessage, mnb);
return CreateBroadcast(vin, _service, keyCollateralAddress, pubKeyCollateralAddress, keyMasternode, pubKeyMasternode, errorMessage, mnb);
}

bool CActiveMasternode::CreateBroadcast(CTxIn vin, CService service, CKey keyCollateralAddress, CPubKey pubKeyCollateralAddress, CKey keyMasternode, CPubKey pubKeyMasternode, std::string& errorMessage, CMasternodeBroadcast &mnb)
Expand Down
2 changes: 1 addition & 1 deletion src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifndef BITCOIN_ADDRMAN_H
#define BITCOIN_ADDRMAN_H

#include "netbase.h"
#include "netaddress.h"
#include "protocol.h"
#include "random.h"
#include "sync.h"
Expand Down
13 changes: 9 additions & 4 deletions src/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,17 @@ static bool ClientAllowed(const CNetAddr& netaddr)
static bool InitHTTPAllowList()
{
rpc_allow_subnets.clear();
rpc_allow_subnets.push_back(CSubNet("127.0.0.0/8")); // always allow IPv4 local subnet
rpc_allow_subnets.push_back(CSubNet("::1")); // always allow IPv6 localhost
CNetAddr localv4;
CNetAddr localv6;
LookupHost("127.0.0.1", localv4, false);
LookupHost("::1", localv6, false);
rpc_allow_subnets.push_back(CSubNet(localv4, 8)); // always allow IPv4 local subnet
rpc_allow_subnets.push_back(CSubNet(localv6)); // always allow IPv6 localhost
if (mapMultiArgs.count("-rpcallowip")) {
const std::vector<std::string>& vAllow = mapMultiArgs["-rpcallowip"];
for (std::string strAllow : vAllow) {
CSubNet subnet(strAllow);
CSubNet subnet;
LookupSubNet(strAllow.c_str(), subnet);
if (!subnet.IsValid()) {
uiInterface.ThreadSafeMessageBox(
strprintf("Invalid -rpcallowip subnet specification: %s. Valid are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24).", strAllow),
Expand Down Expand Up @@ -630,7 +635,7 @@ CService HTTPRequest::GetPeer()
const char* address = "";
uint16_t port = 0;
evhttp_connection_get_peer(con, (char**)&address, &port);
peer = CService(address, port);
peer = LookupNumeric(address, port);
}
return peer;
}
Expand Down
27 changes: 24 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "masternodeman.h"
#include "messagesigner.h"
#include "miner.h"
#include "netbase.h"
#include "net.h"
#include "rpc/server.h"
#include "script/standard.h"
Expand Down Expand Up @@ -989,6 +990,13 @@ bool AppInit2()
// Check level must be 4 for zerocoin checks
if (mapArgs.count("-checklevel"))
return UIError(_("Error: Unsupported argument -checklevel found. Checklevel must be level 4."));
// Exit early if -masternode=1 and -listen=0
if (GetBoolArg("-masternode", false) && !GetBoolArg("-listen", true))
return UIError(_("Error: -listen must be true if -masternode is set."));
// Exit early if -masternode=1 and -port is not the default port
if (GetBoolArg("-masternode", false) && GetListenPort() != Params().GetDefaultPort())
return UIError(strprintf(_("Error: Invalid port %d for running a masternode."), GetListenPort()) + "\n\n" +
strprintf(_("Masternodes are required to run on port %d for %s-net"), Params().GetDefaultPort(), Params().NetworkIDString()));

if (GetBoolArg("-benchmark", false))
UIWarning(_("Warning: Unsupported argument -benchmark ignored, use -debug=bench."));
Expand Down Expand Up @@ -1322,7 +1330,8 @@ bool AppInit2()

if (mapArgs.count("-whitelist")) {
for (const std::string& net : mapMultiArgs["-whitelist"]) {
CSubNet subnet(net);
CSubNet subnet;
LookupSubNet(net.c_str(), subnet);
if (!subnet.IsValid())
return UIError(strprintf(_("Invalid netmask specified in -whitelist: '%s'"), net));
CNode::AddWhitelistedRange(subnet);
Expand Down Expand Up @@ -1947,9 +1956,21 @@ bool AppInit2()
LogPrintf(" addr %s\n", strMasterNodeAddr.c_str());

if (!strMasterNodeAddr.empty()) {
CService addrTest = CService(strMasterNodeAddr);
int nPort;
int nDefaultPort = Params().GetDefaultPort();
std::string strHost;
SplitHostPort(strMasterNodeAddr, nPort, strHost);

// Allow for the port number to be omitted here and just double check
// that if a port is supplied, it matches the required default port.
if (nPort == 0) nPort = nDefaultPort;
if (nPort != nDefaultPort) {
return UIError(strprintf(_("Invalid -masternodeaddr port %d, only %d is supported on %s-net."),
nPort, nDefaultPort, Params().NetworkIDString()));
}
CService addrTest(LookupNumeric(strHost.c_str(), nPort));
if (!addrTest.IsValid()) {
return UIError("Invalid -masternodeaddr address: " + strMasterNodeAddr);
return UIError(strprintf(_("Invalid -masternodeaddr address: %s"), strMasterNodeAddr));
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "masternode-payments.h"
#include "masternode-sync.h"
#include "masternodeman.h"
#include "netbase.h"
#include "sync.h"
#include "util.h"
#include "wallet/wallet.h"
Expand Down Expand Up @@ -364,11 +365,18 @@ bool CMasternodeBroadcast::Create(std::string strService, std::string strKeyMast
return false;
}

int nPort;
int nDefaultPort = Params().GetDefaultPort();
std::string strHost;
SplitHostPort(strService, nPort, strHost);
if (nPort == 0) nPort = nDefaultPort;
CService _service(LookupNumeric(strHost.c_str(), nPort));

// The service needs the correct default port to work properly
if(!CheckDefaultPort(strService, strErrorRet, "CMasternodeBroadcast::Create"))
if (!CheckDefaultPort(_service, strErrorRet, "CMasternodeBroadcast::Create"))
return false;

return Create(txin, CService(strService), keyCollateralAddressNew, pubKeyCollateralAddressNew, keyMasternodeNew, pubKeyMasternodeNew, strErrorRet, mnbRet);
return Create(txin, _service, keyCollateralAddressNew, pubKeyCollateralAddressNew, keyMasternodeNew, pubKeyMasternodeNew, strErrorRet, mnbRet);
}

bool CMasternodeBroadcast::Create(CTxIn txin, CService service, CKey keyCollateralAddressNew, CPubKey pubKeyCollateralAddressNew, CKey keyMasternodeNew, CPubKey pubKeyMasternodeNew, std::string& strErrorRet, CMasternodeBroadcast& mnbRet)
Expand Down Expand Up @@ -466,14 +474,13 @@ bool CMasternodeBroadcast::CheckSignature() const
return true;
}

bool CMasternodeBroadcast::CheckDefaultPort(std::string strService, std::string& strErrorRet, std::string strContext)
bool CMasternodeBroadcast::CheckDefaultPort(CService service, std::string& strErrorRet, const std::string& strContext)
{
CService service = CService(strService);
int nDefaultPort = Params().GetDefaultPort();

if (service.GetPort() != nDefaultPort) {
strErrorRet = strprintf("Invalid port %u for masternode %s, only %d is supported on %s-net.",
service.GetPort(), strService, nDefaultPort, Params().NetworkIDString());
service.GetPort(), service.ToString(), nDefaultPort, Params().NetworkIDString());
LogPrint(BCLog::MASTERNODE, "%s - %s\n", strContext, strErrorRet);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/masternode.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class CMasternodeBroadcast : public CMasternode
/// Create Masternode broadcast, needs to be relayed manually after that
static bool Create(CTxIn vin, CService service, CKey keyCollateralAddressNew, CPubKey pubKeyCollateralAddressNew, CKey keyMasternodeNew, CPubKey pubKeyMasternodeNew, std::string& strErrorRet, CMasternodeBroadcast& mnbRet);
static bool Create(std::string strService, std::string strKey, std::string strTxHash, std::string strOutputIndex, std::string& strErrorRet, CMasternodeBroadcast& mnbRet, bool fOffline = false);
static bool CheckDefaultPort(std::string strService, std::string& strErrorRet, std::string strContext);
static bool CheckDefaultPort(CService service, std::string& strErrorRet, const std::string& strContext);
};

#endif
17 changes: 5 additions & 12 deletions src/masternodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ bool CMasternodeConfig::read(std::string& strErr)
}

int port = 0;
int nDefaultPort = Params().GetDefaultPort();
std::string hostname = "";
SplitHostPort(ip, port, hostname);
if(port == 0 || hostname == "") {
Expand All @@ -82,18 +83,10 @@ bool CMasternodeConfig::read(std::string& strErr)
return false;
}

if (Params().NetworkID() == CBaseChainParams::MAIN) {
if (port != 51472) {
strErr = _("Invalid port detected in masternode.conf") + "\n" +
strprintf(_("Line: %d"), linenumber) + "\n\"" + line + "\"" + "\n" +
_("(must be 51472 for mainnet)");
streamConfig.close();
return false;
}
} else if (port == 51472) {
strErr = _("Invalid port detected in masternode.conf") + "\n" +
strprintf(_("Line: %d"), linenumber) + "\n\"" + line + "\"" + "\n" +
_("(51472 could be used only on mainnet)");
if (port != nDefaultPort) {
strErr = strprintf(_("Invalid port %d detected in masternode.conf"), port) + "\n" +
strprintf(_("Line: %d"), linenumber) + "\n\"" + ip + "\"" + "\n" +
strprintf(_("(must be %d for %s-net)"), nDefaultPort, Params().NetworkIDString());
streamConfig.close();
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion src/masternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "masternode-sync.h"
#include "masternode.h"
#include "messagesigner.h"
#include "netbase.h"
#include "spork.h"
#include "swifttx.h"
#include "util.h"
Expand Down Expand Up @@ -403,7 +404,8 @@ void CMasternodeMan::CountNetworks(int protocolVersion, int& ipv4, int& ipv6, in
std::string strHost;
int port;
SplitHostPort(mn.addr.ToString(), port, strHost);
CNetAddr node = CNetAddr(strHost);
CNetAddr node;
LookupHost(strHost.c_str(), node, false);
int nNetwork = node.GetNetwork();
switch (nNetwork) {
case 1 :
Expand Down
36 changes: 24 additions & 12 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "main.h"
#include "miner.h"
#include "primitives/transaction.h"
#include "netbase.h"
#include "scheduler.h"

#ifdef WIN32
Expand Down Expand Up @@ -173,7 +174,7 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6>& vSeedsIn
// one by discovery.
CAddress GetLocalAddress(const CNetAddr* paddrPeer)
{
CAddress ret(CService("0.0.0.0", GetListenPort()), NODE_NONE);
CAddress ret(CService(CNetAddr(), GetListenPort()), NODE_NONE);
CService addr;
if (GetLocal(addr, paddrPeer)) {
ret = CAddress(addr, nLocalServices);
Expand Down Expand Up @@ -495,7 +496,7 @@ void CNode::PushVersion()

/// when NTP implemented, change to just nTime = GetAdjustedTime()
int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0", 0), addr.nServices));
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
CAddress addrMe = GetLocalAddress(&addr);
GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
if (fLogIPs)
Expand Down Expand Up @@ -1204,8 +1205,11 @@ void ThreadMapPort()
LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
else {
if (externalIPAddress[0]) {
LogPrintf("UPnP: ExternalIPAddress = %s\n", externalIPAddress);
AddLocal(CNetAddr(externalIPAddress), LOCAL_UPNP);
CNetAddr resolved;
if (LookupHost(externalIPAddress, resolved, false)) {
LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
AddLocal(resolved, LOCAL_UPNP);
}
} else
LogPrintf("UPnP: GetExternalIPAddress failed.\n");
}
Expand Down Expand Up @@ -1406,7 +1410,9 @@ void ThreadOpenConnections()
static bool done = false;
if (!done) {
LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
addrman.Add(convertSeed6(Params().FixedSeeds()), CNetAddr("127.0.0.1"));
CNetAddr local;
LookupHost("127.0.0.1", local, false);
addrman.Add(convertSeed6(Params().FixedSeeds()), local);
done = true;
}
}
Expand Down Expand Up @@ -1500,7 +1506,7 @@ std::vector<AddedNodeInfo> GetAddedNodeInfo()
}

for (std::string& strAddNode : lAddresses) {
CService service(strAddNode, Params().GetDefaultPort());
CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
if (service.IsValid()) {
// strAddNode is an IP:port
auto it = mapConnected.find(service);
Expand Down Expand Up @@ -1537,7 +1543,7 @@ void ThreadOpenAddedConnections()
CSemaphoreGrant grant(*semOutbound);
// If strAddedNode is an IP/port, decode it immediately, so
// OpenNetworkConnection can detect existing connections to that IP/port.
CService service(info.strAddedNode, Params().GetDefaultPort());
CService service(LookupNumeric(info.strAddedNode.c_str(), Params().GetDefaultPort()));
OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false);
MilliSleep(500);
}
Expand Down Expand Up @@ -1818,8 +1824,11 @@ void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler)
semOutbound = new CSemaphore(nMaxOutbound);
}

if (pnodeLocalHost == NULL)
pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService("127.0.0.1", 0), nLocalServices));
if (pnodeLocalHost == nullptr) {
CNetAddr local;
LookupHost("127.0.0.1", local, false);
pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService(local, 0), nLocalServices));
}

Discover(threadGroup);

Expand Down Expand Up @@ -2410,7 +2419,10 @@ void DumpBanlist()
// valid, reachable and routable address (except for RegTest)
bool validateMasternodeIP(const std::string& addrStr)
{
CNetAddr netAddr(addrStr.c_str());
return ((IsReachable(netAddr) && netAddr.IsRoutable()) ||
(Params().IsRegTestNet() && netAddr.IsValid()));
CNetAddr resolved;
if (LookupHost(addrStr.c_str(), resolved, false)) {
return ((IsReachable(resolved) && resolved.IsRoutable()) ||
(Params().IsRegTestNet() && resolved.IsValid()));
}
return false;
}
2 changes: 1 addition & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "hash.h"
#include "limitedmap.h"
#include "mruset.h"
#include "netbase.h"
#include "netaddress.h"
#include "protocol.h"
#include "random.h"
#include "streams.h"
Expand Down
Loading

0 comments on commit 4c829bb

Please sign in to comment.