فهرست منبع

added isEqualPrefix to InetAddress

Joseph Henry 8 سال پیش
والد
کامیت
ceeb8ee0bc
2فایلهای تغییر یافته به همراه34 افزوده شده و 0 حذف شده
  1. 24 0
      node/InetAddress.cpp
  2. 10 0
      node/InetAddress.hpp

+ 24 - 0
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) {

+ 10 - 0
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
 	 *