Skip to content

Commit 182686c

Browse files
committed
Merge pull request #6186
b45c50c Fix two problems in CSubNet parsing (Wladimir J. van der Laan) 19e8d7b Simplify code for CSubnet (Wladimir J. van der Laan)
2 parents 165e323 + b45c50c commit 182686c

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/netbase.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,15 +1252,15 @@ CSubNet::CSubNet(const std::string &strSubnet, bool fAllowLookup)
12521252
std::string strNetmask = strSubnet.substr(slash + 1);
12531253
int32_t n;
12541254
// IPv4 addresses start at offset 12, and first 12 bytes must match, so just offset n
1255-
int noffset = network.IsIPv4() ? (12 * 8) : 0;
1255+
const int astartofs = network.IsIPv4() ? 12 : 0;
12561256
if (ParseInt32(strNetmask, &n)) // If valid number, assume /24 symtex
12571257
{
1258-
if(n >= 0 && n <= (128 - noffset)) // Only valid if in range of bits of address
1258+
if(n >= 0 && n <= (128 - astartofs*8)) // Only valid if in range of bits of address
12591259
{
1260-
n += noffset;
1260+
n += astartofs*8;
12611261
// Clear bits [n..127]
12621262
for (; n < 128; ++n)
1263-
netmask[n>>3] &= ~(1<<(n&7));
1263+
netmask[n>>3] &= ~(1<<(7-(n&7)));
12641264
}
12651265
else
12661266
{
@@ -1271,12 +1271,10 @@ CSubNet::CSubNet(const std::string &strSubnet, bool fAllowLookup)
12711271
{
12721272
if (LookupHost(strNetmask.c_str(), vIP, 1, false)) // Never allow lookup for netmask
12731273
{
1274-
// Remember: GetByte returns bytes in reversed order
12751274
// Copy only the *last* four bytes in case of IPv4, the rest of the mask should stay 1's as
12761275
// we don't want pchIPv4 to be part of the mask.
1277-
int asize = network.IsIPv4() ? 4 : 16;
1278-
for(int x=0; x<asize; ++x)
1279-
netmask[15-x] = vIP[0].GetByte(x);
1276+
for(int x=astartofs; x<16; ++x)
1277+
netmask[x] = vIP[0].ip[x];
12801278
}
12811279
else
12821280
{
@@ -1289,14 +1287,18 @@ CSubNet::CSubNet(const std::string &strSubnet, bool fAllowLookup)
12891287
{
12901288
valid = false;
12911289
}
1290+
1291+
// Normalize network according to netmask
1292+
for(int x=0; x<16; ++x)
1293+
network.ip[x] &= netmask[x];
12921294
}
12931295

12941296
bool CSubNet::Match(const CNetAddr &addr) const
12951297
{
12961298
if (!valid || !addr.IsValid())
12971299
return false;
12981300
for(int x=0; x<16; ++x)
1299-
if ((addr.GetByte(x) & netmask[15-x]) != network.GetByte(x))
1301+
if ((addr.ip[x] & netmask[x]) != network.ip[x])
13001302
return false;
13011303
return true;
13021304
}

src/netbase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ class CNetAddr
100100
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
101101
READWRITE(FLATDATA(ip));
102102
}
103+
104+
friend class CSubNet;
103105
};
104106

105107
class CSubNet

src/test/netbase_tests.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ BOOST_AUTO_TEST_CASE(subnet_test)
117117
BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8").Match(CNetAddr("1:2:3:4:5:6:7:8")));
118118
BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8").Match(CNetAddr("1:2:3:4:5:6:7:9")));
119119
BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:0/112").Match(CNetAddr("1:2:3:4:5:6:7:1234")));
120+
BOOST_CHECK(CSubNet("192.168.0.1/24").Match(CNetAddr("192.168.0.2")));
121+
BOOST_CHECK(CSubNet("192.168.0.20/29").Match(CNetAddr("192.168.0.18")));
122+
BOOST_CHECK(CSubNet("1.2.2.1/24").Match(CNetAddr("1.2.2.4")));
123+
BOOST_CHECK(CSubNet("1.2.2.110/31").Match(CNetAddr("1.2.2.111")));
124+
BOOST_CHECK(CSubNet("1.2.2.20/26").Match(CNetAddr("1.2.2.63")));
120125
// All-Matching IPv6 Matches arbitrary IPv4 and IPv6
121126
BOOST_CHECK(CSubNet("::/0").Match(CNetAddr("1:2:3:4:5:6:7:1234")));
122127
BOOST_CHECK(CSubNet("::/0").Match(CNetAddr("1.2.3.4")));

0 commit comments

Comments
 (0)