Browse Source

Adding CSubNet constructor over a single CNetAddr

0.13
Jonas Schnelli 10 years ago
parent
commit
177a0e4914
  1. 4
      src/net.cpp
  2. 7
      src/netbase.cpp
  3. 3
      src/netbase.h
  4. 11
      src/test/netbase_tests.cpp

4
src/net.cpp

@ -488,7 +488,7 @@ bool CNode::IsBanned(CSubNet subnet)
} }
void CNode::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) { void CNode::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
CSubNet subNet(addr.ToString()+(addr.IsIPv4() ? "/32" : "/128")); CSubNet subNet(addr);
Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch); Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch);
} }
@ -511,7 +511,7 @@ void CNode::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t banti
} }
bool CNode::Unban(const CNetAddr &addr) { bool CNode::Unban(const CNetAddr &addr) {
CSubNet subNet(addr.ToString()+(addr.IsIPv4() ? "/32" : "/128")); CSubNet subNet(addr);
return Unban(subNet); return Unban(subNet);
} }

7
src/netbase.cpp

@ -1291,6 +1291,13 @@ CSubNet::CSubNet(const std::string &strSubnet, bool fAllowLookup)
network.ip[x] &= netmask[x]; network.ip[x] &= netmask[x];
} }
CSubNet::CSubNet(const CNetAddr &addr):
valid(addr.IsValid())
{
memset(netmask, 255, sizeof(netmask));
network = addr;
}
bool CSubNet::Match(const CNetAddr &addr) const bool CSubNet::Match(const CNetAddr &addr) const
{ {
if (!valid || !addr.IsValid()) if (!valid || !addr.IsValid())

3
src/netbase.h

@ -118,6 +118,9 @@ class CSubNet
CSubNet(); CSubNet();
explicit CSubNet(const std::string &strSubnet, bool fAllowLookup = false); explicit CSubNet(const std::string &strSubnet, bool fAllowLookup = false);
//constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
explicit CSubNet(const CNetAddr &addr);
bool Match(const CNetAddr &addr) const; bool Match(const CNetAddr &addr) const;
std::string ToString() const; std::string ToString() const;

11
src/test/netbase_tests.cpp

@ -143,6 +143,17 @@ BOOST_AUTO_TEST_CASE(subnet_test)
BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8/128").IsValid()); BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8/128").IsValid());
BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8/129").IsValid()); BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8/129").IsValid());
BOOST_CHECK(!CSubNet("fuzzy").IsValid()); BOOST_CHECK(!CSubNet("fuzzy").IsValid());
//CNetAddr constructor test
BOOST_CHECK(CSubNet(CNetAddr("127.0.0.1")).IsValid());
BOOST_CHECK(CSubNet(CNetAddr("127.0.0.1")).Match(CNetAddr("127.0.0.1")));
BOOST_CHECK(!CSubNet(CNetAddr("127.0.0.1")).Match(CNetAddr("127.0.0.2")));
BOOST_CHECK(CSubNet(CNetAddr("127.0.0.1")).ToString() == "127.0.0.1/255.255.255.255");
BOOST_CHECK(CSubNet(CNetAddr("1:2:3:4:5:6:7:8")).IsValid());
BOOST_CHECK(CSubNet(CNetAddr("1:2:3:4:5:6:7:8")).Match(CNetAddr("1:2:3:4:5:6:7:8")));
BOOST_CHECK(!CSubNet(CNetAddr("1:2:3:4:5:6:7:8")).Match(CNetAddr("1:2:3:4:5:6:7:9")));
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");
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save