diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2015-05-26 08:59:20 +0200 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2015-05-27 09:11:27 +0200 |
commit | ae3d8f371ab2e6886e1ffab8d16495ffccbfc5e4 (patch) | |
tree | bb5b7e59a6226852d81cdf577fe6dc584892e943 | |
parent | e4a7d51537509dab765976f8642f9e15b84408bb (diff) | |
download | bitcoin-ae3d8f371ab2e6886e1ffab8d16495ffccbfc5e4.tar.xz |
Fix two problems in CSubNet parsing
Fix two CSubNet constructor problems:
- The use of `/x` where 8 does not divide x was broken, due to a
bit-order issue
- The use of e.g. `1.2.3.4/24` where the netmasked bits in the network
are not 0 was broken. Fix this by explicitly normalizing the netwok
according to the bitmask.
Also add tests for these cases.
Fixes #6179. Thanks to @jonasschnelli for reporting and initial fix.
Rebased-From: b45c50ce511dbf541ea086ae40a3ad16ff06de0c
Github-Pull: #6186
-rw-r--r-- | src/netbase.cpp | 6 | ||||
-rw-r--r-- | src/test/netbase_tests.cpp | 5 |
2 files changed, 10 insertions, 1 deletions
diff --git a/src/netbase.cpp b/src/netbase.cpp index fe6e79efca..d7c263f347 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -1222,7 +1222,7 @@ CSubNet::CSubNet(const std::string &strSubnet, bool fAllowLookup) n += astartofs*8; // Clear bits [n..127] for (; n < 128; ++n) - netmask[n>>3] &= ~(1<<(n&7)); + netmask[n>>3] &= ~(1<<(7-(n&7))); } else { @@ -1249,6 +1249,10 @@ CSubNet::CSubNet(const std::string &strSubnet, bool fAllowLookup) { valid = false; } + + // Normalize network according to netmask + for(int x=0; x<16; ++x) + network.ip[x] &= netmask[x]; } bool CSubNet::Match(const CNetAddr &addr) const diff --git a/src/test/netbase_tests.cpp b/src/test/netbase_tests.cpp index c26e738384..d5188c3acc 100644 --- a/src/test/netbase_tests.cpp +++ b/src/test/netbase_tests.cpp @@ -116,6 +116,11 @@ BOOST_AUTO_TEST_CASE(subnet_test) BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8").Match(CNetAddr("1:2:3:4:5:6:7:8"))); BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8").Match(CNetAddr("1:2:3:4:5:6:7:9"))); BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:0/112").Match(CNetAddr("1:2:3:4:5:6:7:1234"))); + BOOST_CHECK(CSubNet("192.168.0.1/24").Match(CNetAddr("192.168.0.2"))); + BOOST_CHECK(CSubNet("192.168.0.20/29").Match(CNetAddr("192.168.0.18"))); + BOOST_CHECK(CSubNet("1.2.2.1/24").Match(CNetAddr("1.2.2.4"))); + BOOST_CHECK(CSubNet("1.2.2.110/31").Match(CNetAddr("1.2.2.111"))); + BOOST_CHECK(CSubNet("1.2.2.20/26").Match(CNetAddr("1.2.2.63"))); // All-Matching IPv6 Matches arbitrary IPv4 and IPv6 BOOST_CHECK(CSubNet("::/0").Match(CNetAddr("1:2:3:4:5:6:7:1234"))); BOOST_CHECK(CSubNet("::/0").Match(CNetAddr("1.2.3.4"))); |