Skip to content

Commit

Permalink
[GUI] MnWizard: validate IP
Browse files Browse the repository at this point in the history
- validateMasternodeIP: validate IPv4, IPv6 and onion addresses
- remove port validation: it's fixed, no need to check
Github-Pull: #1545
Rebased-From: de45ddc
  • Loading branch information
random-zebra authored and Fuzzbawls committed May 5, 2020
1 parent d352817 commit 9599424
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/net.cpp
Expand Up @@ -2356,3 +2356,11 @@ void DumpBanlist()
LogPrint(BCLog::NET, "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
banmap.size(), GetTimeMillis() - nStart);
}

// 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()));
}
1 change: 1 addition & 0 deletions src/net.h
Expand Up @@ -130,6 +130,7 @@ bool GetLocal(CService& addr, const CNetAddr* paddrPeer = NULL);
bool IsReachable(enum Network net);
bool IsReachable(const CNetAddr& addr);
CAddress GetLocalAddress(const CNetAddr* paddrPeer = NULL);
bool validateMasternodeIP(const std::string& addrStr); // valid, reachable and routable address


extern bool fDiscover;
Expand Down
14 changes: 8 additions & 6 deletions src/qt/pivx/masternodewizarddialog.cpp
Expand Up @@ -4,11 +4,14 @@

#include "qt/pivx/masternodewizarddialog.h"
#include "qt/pivx/forms/ui_masternodewizarddialog.h"
#include "qt/pivx/qtutils.h"

#include "activemasternode.h"
#include "optionsmodel.h"
#include "pairresult.h"
#include "activemasternode.h"
#include "qt/pivx/mnmodel.h"
#include "qt/pivx/guitransactionsutils.h"
#include "qt/pivx/qtutils.h"

#include <QFile>
#include <QIntValidator>
#include <QHostAddress>
Expand Down Expand Up @@ -182,12 +185,11 @@ bool MasterNodeWizardDialog::createMN()
returnStr = tr("IP or port cannot be empty");
return false;
}
// TODO: Validate IP address..
int portInt = portStr.toInt();
if (portInt <= 0 && portInt > 999999) {
returnStr = tr("Invalid port number");
if (!MNModel::validateMNIP(addressStr)) {
returnStr = tr("Invalid IP address");
return false;
}

// ip + port
std::string ipAddress = addressStr.toStdString();
std::string port = portStr.toStdString();
Expand Down
9 changes: 8 additions & 1 deletion src/qt/pivx/mnmodel.cpp
Expand Up @@ -3,9 +3,11 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "qt/pivx/mnmodel.h"

#include "activemasternode.h"
#include "masternode-sync.h"
#include "masternodeman.h"
#include "activemasternode.h"
#include "net.h" // for validateMasternodeIP
#include "sync.h"
#include "uint256.h"
#include "wallet/wallet.h"
Expand Down Expand Up @@ -185,3 +187,8 @@ bool MNModel::isMNsNetworkSynced()
{
return masternodeSync.IsSynced();
}

bool MNModel::validateMNIP(const QString& addrStr)
{
return validateMasternodeIP(addrStr.toStdString());
}
2 changes: 2 additions & 0 deletions src/qt/pivx/mnmodel.h
Expand Up @@ -51,6 +51,8 @@ class MNModel : public QAbstractTableModel
bool isMNActive(QString mnAlias);
// Masternode collateral has enough confirmations
bool isMNCollateralMature(QString mnAlias);
// Validate string representing a masternode IP address
static bool validateMNIP(const QString& addrStr);


private:
Expand Down
25 changes: 25 additions & 0 deletions src/test/netbase_tests.cpp
Expand Up @@ -235,4 +235,29 @@ BOOST_AUTO_TEST_CASE(subnet_test)
BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
}

BOOST_AUTO_TEST_CASE(validate_test)
{
std::list<std::string> validIPv4 = {"11.12.13.14", "50.168.168.150", "72.31.250.250"};
std::list<std::string> validIPv6 = {"1111:2222:3333:4444:5555:6666::8888", "2001:0002:6c::430", "2002:cb0a:3cdd:1::1"};
std::list<std::string> validTor = {"5wyqrzbvrdsumnok.onion", "FD87:D87E:EB43:edb1:8e4:3588:e546:35ca"};

for (const std::string& ipStr : validIPv4)
BOOST_CHECK_MESSAGE(validateMasternodeIP(ipStr), ipStr);
for (const std::string& ipStr : validIPv6)
BOOST_CHECK_MESSAGE(validateMasternodeIP(ipStr), ipStr);
for (const std::string& ipStr : validTor)
BOOST_CHECK_MESSAGE(validateMasternodeIP(ipStr), ipStr);

std::list<std::string> invalidIPv4 = {"11.12.13.14.15", "11.12.13.330", "30.168.1.255.1", "192.168.1.1", "255.255.255.255"};
std::list<std::string> invalidIPv6 = {"1111:2222:3333:4444:5555:6666:7777:8888:9999", "2002:cb0a:3cdd::1::1", "1111:2222:3333:::5555:6666:7777:8888"};
std::list<std::string> invalidTor = {"5wyqrzbvrdsumnok.noonion"};

for (const std::string& ipStr : invalidIPv4)
BOOST_CHECK_MESSAGE(!validateMasternodeIP(ipStr), ipStr);
for (const std::string& ipStr : invalidIPv6)
BOOST_CHECK_MESSAGE(!validateMasternodeIP(ipStr), ipStr);
for (const std::string& ipStr : invalidTor)
BOOST_CHECK_MESSAGE(!validateMasternodeIP(ipStr), ipStr);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 9599424

Please sign in to comment.