aboutsummaryrefslogtreecommitdiff
path: root/src/netbase.cpp
diff options
context:
space:
mode:
authorJonas Schnelli <jonas.schnelli@include7.ch>2015-06-23 17:20:00 +0200
committerJonas Schnelli <jonas.schnelli@include7.ch>2015-09-16 16:50:19 +0200
commit607809f037950f114f64f1ee09c1486a3c66638b (patch)
tree23975e3b7a45b0dbe10d68626d96a845175ebab7 /src/netbase.cpp
parent53caec66cc43e1f16ba26e16147b77f5cfba22bb (diff)
net: use CIDR notation in CSubNet::ToString()
Diffstat (limited to 'src/netbase.cpp')
-rw-r--r--src/netbase.cpp20
1 files changed, 11 insertions, 9 deletions
diff --git a/src/netbase.cpp b/src/netbase.cpp
index 7a87d125c2..4163b18086 100644
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -1314,15 +1314,17 @@ bool CSubNet::Match(const CNetAddr &addr) const
std::string CSubNet::ToString() const
{
std::string strNetmask;
- if (network.IsIPv4())
- strNetmask = strprintf("%u.%u.%u.%u", netmask[12], netmask[13], netmask[14], netmask[15]);
- else
- strNetmask = strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
- netmask[0] << 8 | netmask[1], netmask[2] << 8 | netmask[3],
- netmask[4] << 8 | netmask[5], netmask[6] << 8 | netmask[7],
- netmask[8] << 8 | netmask[9], netmask[10] << 8 | netmask[11],
- netmask[12] << 8 | netmask[13], netmask[14] << 8 | netmask[15]);
- return network.ToString() + "/" + strNetmask;
+ int cidr = 0;
+ for (int n = network.IsIPv4() ? 12 : 0 ; n < 16; ++n)
+ {
+ uint8_t netmaskpart = netmask[n];
+ while (netmaskpart)
+ {
+ cidr += ( netmaskpart & 0x01 );
+ netmaskpart >>= 1;
+ }
+ }
+ return network.ToString() + strprintf("/%u", cidr);
}
bool CSubNet::IsValid() const