diff options
author | Erik Kline <ek@google.com> | 2015-04-13 15:33:34 +0900 |
---|---|---|
committer | Erik Kline <ek@google.com> | 2015-04-14 16:19:11 +0900 |
commit | acc8c09d3a12d00b565a76acc26061172c3dd3c6 (patch) | |
tree | 6f4077bf87d609de10b44bf3d52da1ec0b097b94 /core/java/android/net | |
parent | c1c6528d2b686d5a17e577e7864a319dcba7c224 (diff) | |
download | frameworks_base-acc8c09d3a12d00b565a76acc26061172c3dd3c6.zip frameworks_base-acc8c09d3a12d00b565a76acc26061172c3dd3c6.tar.gz frameworks_base-acc8c09d3a12d00b565a76acc26061172c3dd3c6.tar.bz2 |
Add android.net.IpPrefix#contains()
This was originally to avoid RuntimeException in RouteInfo#matches():
When an IPv6 prefix with a length greater than the max permitted
for IPv4 is matched against an Inet4Address, the call to
NetworkUtils throws RuntimeException.
Change-Id: I92e2bd19a4e7d656cf682fd27678da07e211850d
Diffstat (limited to 'core/java/android/net')
-rw-r--r-- | core/java/android/net/IpPrefix.java | 15 | ||||
-rw-r--r-- | core/java/android/net/RouteInfo.java | 8 |
2 files changed, 16 insertions, 7 deletions
diff --git a/core/java/android/net/IpPrefix.java b/core/java/android/net/IpPrefix.java index b268986..6b4f2d5 100644 --- a/core/java/android/net/IpPrefix.java +++ b/core/java/android/net/IpPrefix.java @@ -170,6 +170,21 @@ public final class IpPrefix implements Parcelable { } /** + * Determines whether the prefix contains the specified address. + * + * @param address An {@link InetAddress} to test. + * @return {@code true} if the prefix covers the given address. + */ + public boolean contains(InetAddress address) { + byte[] addrBytes = (address == null) ? null : address.getAddress(); + if (addrBytes == null || addrBytes.length != this.address.length) { + return false; + } + NetworkUtils.maskRawAddress(addrBytes, prefixLength); + return Arrays.equals(this.address, addrBytes); + } + + /** * Returns a string representation of this {@code IpPrefix}. * * @return a string such as {@code "192.0.2.0/24"} or {@code "2001:db8:1:2::/64"}. diff --git a/core/java/android/net/RouteInfo.java b/core/java/android/net/RouteInfo.java index cfd20a0..90a2460 100644 --- a/core/java/android/net/RouteInfo.java +++ b/core/java/android/net/RouteInfo.java @@ -367,13 +367,7 @@ public final class RouteInfo implements Parcelable { * @return {@code true} if the destination and prefix length cover the given address. */ public boolean matches(InetAddress destination) { - if (destination == null) return false; - - // match the route destination and destination with prefix length - InetAddress dstNet = NetworkUtils.getNetworkPart(destination, - mDestination.getPrefixLength()); - - return mDestination.getAddress().equals(dstNet); + return mDestination.contains(destination); } /** |