From 51766e6549ebf0e8e9e668549f28cc1331d1912b Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 17 Jul 2014 13:08:37 -0700 Subject: BSD routing table works... that hurt much worse than it should have. --- node/InetAddress.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'node/InetAddress.cpp') diff --git a/node/InetAddress.cpp b/node/InetAddress.cpp index 706d804a..9ff5acea 100644 --- a/node/InetAddress.cpp +++ b/node/InetAddress.cpp @@ -215,6 +215,36 @@ bool InetAddress::sameNetworkAs(const InetAddress &ipnet) const return ((*a >> bits) == (*b >> bits)); } +bool InetAddress::within(const InetAddress &ipnet) const + throw() +{ + if (_sa.saddr.sa_family != ipnet._sa.saddr.sa_family) + return false; + + unsigned int bits = ipnet.netmaskBits(); + switch(_sa.saddr.sa_family) { + case AF_INET: + if (bits > 32) return false; + break; + case AF_INET6: + if (bits > 128) return false; + break; + default: return false; + } + + const uint8_t *a = (const uint8_t *)rawIpData(); + const uint8_t *b = (const uint8_t *)ipnet.rawIpData(); + while (bits >= 8) { + if (*(a++) != *(b++)) + return false; + bits -= 8; + } + if (bits) { + uint8_t mask = ((0xff << (8 - bits)) & 0xff); + return ((*a & mask) == (*b & mask)); + } else return true; +} + bool InetAddress::operator==(const InetAddress &a) const throw() { -- cgit v1.2.3