Skip to content

Commit 177a0e4

Browse files
committed
Adding CSubNet constructor over a single CNetAddr
1 parent 409bccf commit 177a0e4

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ bool CNode::IsBanned(CSubNet subnet)
488488
}
489489

490490
void CNode::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
491-
CSubNet subNet(addr.ToString()+(addr.IsIPv4() ? "/32" : "/128"));
491+
CSubNet subNet(addr);
492492
Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch);
493493
}
494494

@@ -511,7 +511,7 @@ void CNode::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t banti
511511
}
512512

513513
bool CNode::Unban(const CNetAddr &addr) {
514-
CSubNet subNet(addr.ToString()+(addr.IsIPv4() ? "/32" : "/128"));
514+
CSubNet subNet(addr);
515515
return Unban(subNet);
516516
}
517517

src/netbase.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,13 @@ CSubNet::CSubNet(const std::string &strSubnet, bool fAllowLookup)
12911291
network.ip[x] &= netmask[x];
12921292
}
12931293

1294+
CSubNet::CSubNet(const CNetAddr &addr):
1295+
valid(addr.IsValid())
1296+
{
1297+
memset(netmask, 255, sizeof(netmask));
1298+
network = addr;
1299+
}
1300+
12941301
bool CSubNet::Match(const CNetAddr &addr) const
12951302
{
12961303
if (!valid || !addr.IsValid())

src/netbase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ class CSubNet
118118
CSubNet();
119119
explicit CSubNet(const std::string &strSubnet, bool fAllowLookup = false);
120120

121+
//constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
122+
explicit CSubNet(const CNetAddr &addr);
123+
121124
bool Match(const CNetAddr &addr) const;
122125

123126
std::string ToString() const;

src/test/netbase_tests.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ BOOST_AUTO_TEST_CASE(subnet_test)
143143
BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8/128").IsValid());
144144
BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8/129").IsValid());
145145
BOOST_CHECK(!CSubNet("fuzzy").IsValid());
146+
147+
//CNetAddr constructor test
148+
BOOST_CHECK(CSubNet(CNetAddr("127.0.0.1")).IsValid());
149+
BOOST_CHECK(CSubNet(CNetAddr("127.0.0.1")).Match(CNetAddr("127.0.0.1")));
150+
BOOST_CHECK(!CSubNet(CNetAddr("127.0.0.1")).Match(CNetAddr("127.0.0.2")));
151+
BOOST_CHECK(CSubNet(CNetAddr("127.0.0.1")).ToString() == "127.0.0.1/255.255.255.255");
152+
153+
BOOST_CHECK(CSubNet(CNetAddr("1:2:3:4:5:6:7:8")).IsValid());
154+
BOOST_CHECK(CSubNet(CNetAddr("1:2:3:4:5:6:7:8")).Match(CNetAddr("1:2:3:4:5:6:7:8")));
155+
BOOST_CHECK(!CSubNet(CNetAddr("1:2:3:4:5:6:7:8")).Match(CNetAddr("1:2:3:4:5:6:7:9")));
156+
BOOST_CHECK(CSubNet(CNetAddr("1:2:3:4:5:6:7:8")).ToString() == "1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
146157
}
147158

148159
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)