Skip to content

Commit

Permalink
Simplify code for CSubnet
Browse files Browse the repository at this point in the history
Simplify the code by using CAddress.ip directly, instead of the reversed
GetByte() semantics.
  • Loading branch information
laanwj committed May 26, 2015
1 parent e1412d3 commit 19e8d7b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,12 +1252,12 @@ CSubNet::CSubNet(const std::string &strSubnet, bool fAllowLookup)
std::string strNetmask = strSubnet.substr(slash + 1);
int32_t n;
// IPv4 addresses start at offset 12, and first 12 bytes must match, so just offset n
int noffset = network.IsIPv4() ? (12 * 8) : 0;
const int astartofs = network.IsIPv4() ? 12 : 0;
if (ParseInt32(strNetmask, &n)) // If valid number, assume /24 symtex
{
if(n >= 0 && n <= (128 - noffset)) // Only valid if in range of bits of address
if(n >= 0 && n <= (128 - astartofs*8)) // Only valid if in range of bits of address
{
n += noffset;
n += astartofs*8;
// Clear bits [n..127]
for (; n < 128; ++n)
netmask[n>>3] &= ~(1<<(n&7));
Expand All @@ -1271,12 +1271,10 @@ CSubNet::CSubNet(const std::string &strSubnet, bool fAllowLookup)
{
if (LookupHost(strNetmask.c_str(), vIP, 1, false)) // Never allow lookup for netmask
{
// Remember: GetByte returns bytes in reversed order
// Copy only the *last* four bytes in case of IPv4, the rest of the mask should stay 1's as
// we don't want pchIPv4 to be part of the mask.
int asize = network.IsIPv4() ? 4 : 16;
for(int x=0; x<asize; ++x)
netmask[15-x] = vIP[0].GetByte(x);
for(int x=astartofs; x<16; ++x)
netmask[x] = vIP[0].ip[x];
}
else
{
Expand All @@ -1296,7 +1294,7 @@ bool CSubNet::Match(const CNetAddr &addr) const
if (!valid || !addr.IsValid())
return false;
for(int x=0; x<16; ++x)
if ((addr.GetByte(x) & netmask[15-x]) != network.GetByte(x))
if ((addr.ip[x] & netmask[x]) != network.ip[x])
return false;
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions src/netbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class CNetAddr
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(FLATDATA(ip));
}

friend class CSubNet;
};

class CSubNet
Expand Down

0 comments on commit 19e8d7b

Please sign in to comment.