aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2015-05-26 08:59:13 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2015-05-27 09:10:56 +0200
commite4a7d51537509dab765976f8642f9e15b84408bb (patch)
treec1e911d286a8bc33cc47b88601714b2e71c18f94
parent16f45600c8c372a738ffef544292864256382601 (diff)
Simplify code for CSubnet
Simplify the code by using CAddress.ip directly, instead of the reversed GetByte() semantics. Rebased-From: 19e8d7be42039724b4893515ec6457d0187024a9 Github-Pull: #6186
-rw-r--r--src/netbase.cpp14
-rw-r--r--src/netbase.h2
2 files changed, 8 insertions, 8 deletions
diff --git a/src/netbase.cpp b/src/netbase.cpp
index 053c645a1b..fe6e79efca 100644
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -1214,12 +1214,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));
@@ -1233,12 +1233,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
{
@@ -1258,7 +1256,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;
}
diff --git a/src/netbase.h b/src/netbase.h
index 09fe094946..27b2fa78d0 100644
--- a/src/netbase.h
+++ b/src/netbase.h
@@ -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