Skip to content

Commit 19e8d7b

Browse files
committed
Simplify code for CSubnet
Simplify the code by using CAddress.ip directly, instead of the reversed GetByte() semantics.
1 parent e1412d3 commit 19e8d7b

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/netbase.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,12 +1252,12 @@ 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)
12631263
netmask[n>>3] &= ~(1<<(n&7));
@@ -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
{
@@ -1296,7 +1294,7 @@ bool CSubNet::Match(const CNetAddr &addr) const
12961294
if (!valid || !addr.IsValid())
12971295
return false;
12981296
for(int x=0; x<16; ++x)
1299-
if ((addr.GetByte(x) & netmask[15-x]) != network.GetByte(x))
1297+
if ((addr.ip[x] & netmask[x]) != network.ip[x])
13001298
return false;
13011299
return true;
13021300
}

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

0 commit comments

Comments
 (0)