From 321fd64921f13d6684b738fad6c455d802748389 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Mon, 10 Oct 2016 13:13:07 +0400 Subject: [PATCH] Masternodes should be allowed to annonce ipv4 addresses only from now (#1065) --- src/masternode.cpp | 16 ++++++++++++++++ src/masternode.h | 2 ++ src/masternodeman.cpp | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/src/masternode.cpp b/src/masternode.cpp index f3deb5151..a85039f0e 100644 --- a/src/masternode.cpp +++ b/src/masternode.cpp @@ -203,6 +203,14 @@ void CMasternode::Check(bool fForce) nActiveState = MASTERNODE_ENABLED; // OK } +bool CMasternode::IsValidNetAddr() +{ + // TODO: regtest is fine with any addresses for now, + // should probably be a bit smarter if one day we start to implement tests for this + return Params().NetworkIDString() == CBaseChainParams::REGTEST || + (addr.IsIPv4() && IsReachable(addr) && addr.IsRoutable()); +} + std::string CMasternode::GetStatus() { switch(nActiveState) { @@ -333,6 +341,14 @@ bool CMasternodeBroadcast::Create(CTxIn txin, CService service, CKey keyCollater } mnbRet = CMasternodeBroadcast(service, txin, pubKeyCollateralAddressNew, pubKeyMasternodeNew, PROTOCOL_VERSION); + + if(!mnbRet.IsValidNetAddr()) { + strErrorRet = strprintf("Invalid IP address, masternode=%s", txin.prevout.ToStringShort()); + LogPrintf("CMasternodeBroadcast::Create -- %s\n", strErrorRet); + mnbRet = CMasternodeBroadcast(); + return false; + } + mnbRet.lastPing = mnp; if(!mnbRet.Sign(keyCollateralAddressNew)) { strErrorRet = strprintf("Failed to sign broadcast, masternode=%s", txin.prevout.ToStringShort()); diff --git a/src/masternode.h b/src/masternode.h index 2227691bb..683585f13 100644 --- a/src/masternode.h +++ b/src/masternode.h @@ -212,6 +212,8 @@ public: bool IsEnabled() { return nActiveState == MASTERNODE_ENABLED; } bool IsPreEnabled() { return nActiveState == MASTERNODE_PRE_ENABLED; } + bool IsValidNetAddr(); + std::string GetStatus(); int GetCollateralAge(); diff --git a/src/masternodeman.cpp b/src/masternodeman.cpp index bf59aafe3..2d48ce8c7 100644 --- a/src/masternodeman.cpp +++ b/src/masternodeman.cpp @@ -726,6 +726,12 @@ bool CMasternodeMan::CheckMnbAndUpdateMasternodeList(CMasternodeBroadcast mnb, i mapSeenMasternodeBroadcast.insert(make_pair(mnb.GetHash(), mnb)); LogPrint("masternode", "CMasternodeMan::CheckMnbAndUpdateMasternodeList - Masternode broadcast, vin: %s new\n", mnb.vin.ToString()); + // We check addr before both initial mnb and update + if(!mnb.IsValidNetAddr()) { + LogPrintf("CMasternodeBroadcast::CheckMnbAndUpdateMasternodeList -- Invalid addr, rejected: masternode=%s sigTime=%lld addr=%s\n", + mnb.vin.prevout.ToStringShort(), mnb.sigTime, mnb.addr.ToString()); + return false; + } if(!mnb.CheckAndUpdate(nDos)){ LogPrint("masternode", "CMasternodeMan::CheckMnbAndUpdateMasternodeList - Masternode broadcast, vin: %s CheckAndUpdate failed\n", mnb.vin.ToString());