Skip to content

Commit

Permalink
net: layer 2 CService abstraction
Browse files Browse the repository at this point in the history
This also adds some sanity checking to masternode startup arguments:
* -listen can't be 0 when -masternode is 1
* when -masternode is set, require that the client uses the default port
* validate the port supplied (if any) for -masternodeaddr

Lastly, `CMasternodeBroadcast::CheckDefaultPort` now takes a `CService`
as it's first argument instead of a string.
  • Loading branch information
Fuzzbawls committed May 25, 2020
1 parent 235c33e commit 3ddd35e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
14 changes: 10 additions & 4 deletions src/activemasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ void CActiveMasternode::ManageStatus()
return;
}
} else {
service = CService(strMasterNodeAddr);
LookupNumeric(strMasterNodeAddr.c_str(), service, GetListenPort());
}

// 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 @@ -218,6 +218,7 @@ bool CActiveMasternode::CreateBroadcast(std::string strService, std::string strK
CKey keyCollateralAddress;
CPubKey pubKeyMasternode;
CKey keyMasternode;
CService _service;

//need correct blocks to send ping
if (!fOffline && !masternodeSync.IsBlockchainSynced()) {
Expand All @@ -238,11 +239,16 @@ bool CActiveMasternode::CreateBroadcast(std::string strService, std::string strK
return false;
}

int nPort;
std::string strHost;
SplitHostPort(strService, nPort, strHost);
LookupNumeric(strHost.c_str(), _service, 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;

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
24 changes: 22 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,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 @@ -1926,9 +1933,22 @@ 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(), addrTest, 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 @@ -367,6 +367,7 @@ bool CMasternodeBroadcast::Create(std::string strService, std::string strKeyMast
CKey keyCollateralAddressNew;
CPubKey pubKeyMasternodeNew;
CKey keyMasternodeNew;
CService _service;

//need correct blocks to send ping
if (!fOffline && !masternodeSync.IsBlockchainSynced()) {
Expand All @@ -387,11 +388,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;
LookupNumeric(strHost.c_str(), _service, 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 @@ -489,14 +497,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 @@ -331,7 +331,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

0 comments on commit 3ddd35e

Please sign in to comment.