summaryrefslogtreecommitdiff
path: root/node
diff options
context:
space:
mode:
authorJoseph Henry <josephjah@gmail.com>2017-05-04 15:25:48 -0700
committerJoseph Henry <josephjah@gmail.com>2017-05-04 15:25:48 -0700
commitceeb8ee0bcd591d79dc61a684ede2c9cae91323c (patch)
tree166a0f4577cc28c4bc9637467b0c4150ff49c058 /node
parent6bb855873d98309cf7ae9ed117615638366e5d8b (diff)
downloadinfinitytier-ceeb8ee0bcd591d79dc61a684ede2c9cae91323c.tar.gz
infinitytier-ceeb8ee0bcd591d79dc61a684ede2c9cae91323c.zip
added isEqualPrefix to InetAddress
Diffstat (limited to 'node')
-rw-r--r--node/InetAddress.cpp24
-rw-r--r--node/InetAddress.hpp10
2 files changed, 34 insertions, 0 deletions
diff --git a/node/InetAddress.cpp b/node/InetAddress.cpp
index 62bb8145..0fbb2d68 100644
--- a/node/InetAddress.cpp
+++ b/node/InetAddress.cpp
@@ -287,6 +287,30 @@ InetAddress InetAddress::network() const
return r;
}
+#ifdef ZT_SDK
+ bool InetAddress::isEqualPrefix(const InetAddress &addr) const
+ {
+ if (addr.ss_family == ss_family) {
+ switch(ss_family) {
+ case AF_INET6: {
+ const InetAddress mask(netmask());
+ InetAddress addr_mask(addr.netmask());
+ const uint8_t *n = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&addr_mask)->sin6_addr.s6_addr);
+ const uint8_t *m = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&mask)->sin6_addr.s6_addr);
+ const uint8_t *a = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&addr)->sin6_addr.s6_addr);
+ const uint8_t *b = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
+ for(unsigned int i=0;i<16;++i) {
+ if ((a[i] & m[i]) != (b[i] & n[i]))
+ return false;
+ }
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+#endif
+
bool InetAddress::containsAddress(const InetAddress &addr) const
{
if (addr.ss_family == ss_family) {
diff --git a/node/InetAddress.hpp b/node/InetAddress.hpp
index 0975a9cf..4cb9a4dc 100644
--- a/node/InetAddress.hpp
+++ b/node/InetAddress.hpp
@@ -355,6 +355,16 @@ struct InetAddress : public sockaddr_storage
*/
InetAddress network() const;
+#ifdef ZT_SDK
+ /**
+ * Test whether this IPv6 prefix matches the prefix of a given IPv6 address
+ *
+ * @param addr Address to check
+ * @return True if this IPv6 prefix matches the prefix of a given IPv6 address
+ */
+ bool isEqualPrefix(const InetAddress &addr) const;
+#endif
+
/**
* Test whether this IP/netmask contains this address
*