Add IPPrefix.contains()
Add helper methods to check whether the specified prefix is
entirely contained in this prefix, or the specified address
is contained in this prefix.
Bug: 184750836
Test: atest IPPrefixTest
Change-Id: I838510b347ac5741c589ee8f3983111209155588
diff --git a/staticlibs/netd/libnetdutils/include/netdutils/InternetAddresses.h b/staticlibs/netd/libnetdutils/include/netdutils/InternetAddresses.h
index d5cbe2b..d10cec7 100644
--- a/staticlibs/netd/libnetdutils/include/netdutils/InternetAddresses.h
+++ b/staticlibs/netd/libnetdutils/include/netdutils/InternetAddresses.h
@@ -221,6 +221,12 @@
in_addr addr4() const noexcept { return mData.ip.v4; }
in6_addr addr6() const noexcept { return mData.ip.v6; }
constexpr int length() const noexcept { return mData.cidrlen; }
+ bool contains(const IPPrefix& other) {
+ return length() <= other.length() && IPPrefix(other.ip(), length()).ip() == ip();
+ }
+ bool contains(const IPAddress& other) {
+ return IPPrefix(other, length()).ip() == ip();
+ }
bool isUninitialized() const noexcept;
std::string toString() const noexcept;