From 3ddd35e410ff7421e8ba33a153c546c1e5d5741e Mon Sep 17 00:00:00 2001 From: Fuzzbawls Date: Sun, 24 May 2020 22:00:16 -0700 Subject: [PATCH] net: layer 2 CService abstraction 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. --- src/activemasternode.cpp | 14 ++++++++++---- src/init.cpp | 24 ++++++++++++++++++++++-- src/masternode.cpp | 17 ++++++++++++----- src/masternode.h | 2 +- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/activemasternode.cpp b/src/activemasternode.cpp index 6d414bba38a8f1..67100d20722e4b 100644 --- a/src/activemasternode.cpp +++ b/src/activemasternode.cpp @@ -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()); @@ -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()) { @@ -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) diff --git a/src/init.cpp b/src/init.cpp index d7e1c68069e7bd..29e3ce941f208a 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -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.")); @@ -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)); } } diff --git a/src/masternode.cpp b/src/masternode.cpp index 07dafbcc6f136e..86dac8e07b64e1 100644 --- a/src/masternode.cpp +++ b/src/masternode.cpp @@ -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()) { @@ -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) @@ -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; } diff --git a/src/masternode.h b/src/masternode.h index a80ba84127c217..83ebcdd36b4ad0 100644 --- a/src/masternode.h +++ b/src/masternode.h @@ -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