Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
paulhu | 895a741 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 19 | import android.annotation.IntRange; |
paulhu | cbbc3db | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Roman Kalukiewicz | 384a8c6 | 2020-10-14 15:59:06 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Remi NGUYEN VAN | d9f7586 | 2019-01-23 21:35:52 +0900 | [diff] [blame] | 22 | import android.annotation.SystemApi; |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Lorenzo Colitti | 174bab2 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 25 | import android.util.Pair; |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 26 | |
Chiachang Wang | f2c81e6 | 2021-02-03 18:40:42 +0800 | [diff] [blame] | 27 | import com.android.net.module.util.NetUtils; |
| 28 | |
Hugo Benichi | 8253be9 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 29 | import java.net.Inet4Address; |
| 30 | import java.net.Inet6Address; |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 31 | import java.net.InetAddress; |
| 32 | import java.net.UnknownHostException; |
| 33 | import java.util.Arrays; |
Chalard Jean | 9cbc882 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 34 | import java.util.Comparator; |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * This class represents an IP prefix, i.e., a contiguous block of IP addresses aligned on a |
| 38 | * power of two boundary (also known as an "IP subnet"). A prefix is specified by two pieces of |
| 39 | * information: |
| 40 | * |
| 41 | * <ul> |
| 42 | * <li>A starting IP address (IPv4 or IPv6). This is the first IP address of the prefix. |
| 43 | * <li>A prefix length. This specifies the length of the prefix by specifing the number of bits |
| 44 | * in the IP address, starting from the most significant bit in network byte order, that |
| 45 | * are constant for all addresses in the prefix. |
| 46 | * </ul> |
| 47 | * |
| 48 | * For example, the prefix <code>192.0.2.0/24</code> covers the 256 IPv4 addresses from |
| 49 | * <code>192.0.2.0</code> to <code>192.0.2.255</code>, inclusive, and the prefix |
| 50 | * <code>2001:db8:1:2</code> covers the 2^64 IPv6 addresses from <code>2001:db8:1:2::</code> to |
| 51 | * <code>2001:db8:1:2:ffff:ffff:ffff:ffff</code>, inclusive. |
| 52 | * |
| 53 | * Objects of this class are immutable. |
| 54 | */ |
Robert Greenwalt | 4998e95 | 2014-06-12 12:57:19 -0700 | [diff] [blame] | 55 | public final class IpPrefix implements Parcelable { |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 56 | private final byte[] address; // network byte order |
| 57 | private final int prefixLength; |
| 58 | |
Lorenzo Colitti | 174bab2 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 59 | private void checkAndMaskAddressAndPrefixLength() { |
| 60 | if (address.length != 4 && address.length != 16) { |
| 61 | throw new IllegalArgumentException( |
| 62 | "IpPrefix has " + address.length + " bytes which is neither 4 nor 16"); |
| 63 | } |
Chiachang Wang | f2c81e6 | 2021-02-03 18:40:42 +0800 | [diff] [blame] | 64 | NetUtils.maskRawAddress(address, prefixLength); |
Lorenzo Colitti | 174bab2 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 65 | } |
| 66 | |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Constructs a new {@code IpPrefix} from a byte array containing an IPv4 or IPv6 address in |
Lorenzo Colitti | 174bab2 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 69 | * network byte order and a prefix length. Silently truncates the address to the prefix length, |
| 70 | * so for example {@code 192.0.2.1/24} is silently converted to {@code 192.0.2.0/24}. |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 71 | * |
| 72 | * @param address the IP address. Must be non-null and exactly 4 or 16 bytes long. |
| 73 | * @param prefixLength the prefix length. Must be >= 0 and <= (32 or 128) (IPv4 or IPv6). |
| 74 | * |
| 75 | * @hide |
| 76 | */ |
paulhu | 895a741 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 77 | public IpPrefix(@NonNull byte[] address, @IntRange(from = 0, to = 128) int prefixLength) { |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 78 | this.address = address.clone(); |
| 79 | this.prefixLength = prefixLength; |
Lorenzo Colitti | 174bab2 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 80 | checkAndMaskAddressAndPrefixLength(); |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Lorenzo Colitti | 174bab2 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 84 | * Constructs a new {@code IpPrefix} from an IPv4 or IPv6 address and a prefix length. Silently |
| 85 | * truncates the address to the prefix length, so for example {@code 192.0.2.1/24} is silently |
| 86 | * converted to {@code 192.0.2.0/24}. |
| 87 | * |
| 88 | * @param address the IP address. Must be non-null. |
| 89 | * @param prefixLength the prefix length. Must be >= 0 and <= (32 or 128) (IPv4 or IPv6). |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 90 | */ |
paulhu | 895a741 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 91 | public IpPrefix(@NonNull InetAddress address, @IntRange(from = 0, to = 128) int prefixLength) { |
Lorenzo Colitti | 174bab2 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 92 | // We don't reuse the (byte[], int) constructor because it calls clone() on the byte array, |
| 93 | // which is unnecessary because getAddress() already returns a clone. |
| 94 | this.address = address.getAddress(); |
| 95 | this.prefixLength = prefixLength; |
| 96 | checkAndMaskAddressAndPrefixLength(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Constructs a new IpPrefix from a string such as "192.0.2.1/24" or "2001:db8::1/64". |
| 101 | * Silently truncates the address to the prefix length, so for example {@code 192.0.2.1/24} |
| 102 | * is silently converted to {@code 192.0.2.0/24}. |
| 103 | * |
| 104 | * @param prefix the prefix to parse |
| 105 | * |
| 106 | * @hide |
| 107 | */ |
Remi NGUYEN VAN | c7fe99f | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 108 | @SystemApi |
paulhu | cbbc3db | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 109 | public IpPrefix(@NonNull String prefix) { |
Lorenzo Colitti | 174bab2 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 110 | // We don't reuse the (InetAddress, int) constructor because "error: call to this must be |
| 111 | // first statement in constructor". We could factor out setting the member variables to an |
| 112 | // init() method, but if we did, then we'd have to make the members non-final, or "error: |
| 113 | // cannot assign a value to final variable address". So we just duplicate the code here. |
paulhu | 1013c81 | 2021-03-03 22:15:11 +0800 | [diff] [blame] | 114 | Pair<InetAddress, Integer> ipAndMask = NetworkUtils.legacyParseIpAndMask(prefix); |
Lorenzo Colitti | 174bab2 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 115 | this.address = ipAndMask.first.getAddress(); |
| 116 | this.prefixLength = ipAndMask.second; |
| 117 | checkAndMaskAddressAndPrefixLength(); |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Compares this {@code IpPrefix} object against the specified object in {@code obj}. Two |
| 122 | * objects are equal if they have the same startAddress and prefixLength. |
| 123 | * |
| 124 | * @param obj the object to be tested for equality. |
| 125 | * @return {@code true} if both objects are equal, {@code false} otherwise. |
| 126 | */ |
| 127 | @Override |
Roman Kalukiewicz | 384a8c6 | 2020-10-14 15:59:06 -0700 | [diff] [blame] | 128 | public boolean equals(@Nullable Object obj) { |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 129 | if (!(obj instanceof IpPrefix)) { |
| 130 | return false; |
| 131 | } |
| 132 | IpPrefix that = (IpPrefix) obj; |
| 133 | return Arrays.equals(this.address, that.address) && this.prefixLength == that.prefixLength; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Gets the hashcode of the represented IP prefix. |
| 138 | * |
| 139 | * @return the appropriate hashcode value. |
| 140 | */ |
| 141 | @Override |
| 142 | public int hashCode() { |
| 143 | return Arrays.hashCode(address) + 11 * prefixLength; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns a copy of the first IP address in the prefix. Modifying the returned object does not |
| 148 | * change this object's contents. |
| 149 | * |
| 150 | * @return the address in the form of a byte array. |
| 151 | */ |
paulhu | 895a741 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 152 | public @NonNull InetAddress getAddress() { |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 153 | try { |
| 154 | return InetAddress.getByAddress(address); |
| 155 | } catch (UnknownHostException e) { |
| 156 | // Cannot happen. InetAddress.getByAddress can only throw an exception if the byte |
| 157 | // array is the wrong length, but we check that in the constructor. |
paulhu | 895a741 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 158 | throw new IllegalArgumentException("Address is invalid"); |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns a copy of the IP address bytes in network order (the highest order byte is the zeroth |
| 164 | * element). Modifying the returned array does not change this object's contents. |
| 165 | * |
| 166 | * @return the address in the form of a byte array. |
| 167 | */ |
paulhu | 895a741 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 168 | public @NonNull byte[] getRawAddress() { |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 169 | return address.clone(); |
| 170 | } |
| 171 | |
| 172 | /** |
Lorenzo Colitti | 174bab2 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 173 | * Returns the prefix length of this {@code IpPrefix}. |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 174 | * |
| 175 | * @return the prefix length. |
| 176 | */ |
paulhu | 895a741 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 177 | @IntRange(from = 0, to = 128) |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 178 | public int getPrefixLength() { |
| 179 | return prefixLength; |
| 180 | } |
| 181 | |
| 182 | /** |
Erik Kline | b98afb0 | 2015-04-13 15:33:34 +0900 | [diff] [blame] | 183 | * Determines whether the prefix contains the specified address. |
| 184 | * |
| 185 | * @param address An {@link InetAddress} to test. |
paulhu | 895a741 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 186 | * @return {@code true} if the prefix covers the given address. {@code false} otherwise. |
Erik Kline | b98afb0 | 2015-04-13 15:33:34 +0900 | [diff] [blame] | 187 | */ |
paulhu | 895a741 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 188 | public boolean contains(@NonNull InetAddress address) { |
| 189 | byte[] addrBytes = address.getAddress(); |
Erik Kline | b98afb0 | 2015-04-13 15:33:34 +0900 | [diff] [blame] | 190 | if (addrBytes == null || addrBytes.length != this.address.length) { |
| 191 | return false; |
| 192 | } |
Chiachang Wang | f2c81e6 | 2021-02-03 18:40:42 +0800 | [diff] [blame] | 193 | NetUtils.maskRawAddress(addrBytes, prefixLength); |
Erik Kline | b98afb0 | 2015-04-13 15:33:34 +0900 | [diff] [blame] | 194 | return Arrays.equals(this.address, addrBytes); |
| 195 | } |
| 196 | |
| 197 | /** |
Chalard Jean | 9cbc882 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 198 | * Returns whether the specified prefix is entirely contained in this prefix. |
| 199 | * |
| 200 | * Note this is mathematical inclusion, so a prefix is always contained within itself. |
| 201 | * @param otherPrefix the prefix to test |
| 202 | * @hide |
| 203 | */ |
paulhu | 895a741 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 204 | public boolean containsPrefix(@NonNull IpPrefix otherPrefix) { |
Chalard Jean | 9cbc882 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 205 | if (otherPrefix.getPrefixLength() < prefixLength) return false; |
| 206 | final byte[] otherAddress = otherPrefix.getRawAddress(); |
Chiachang Wang | f2c81e6 | 2021-02-03 18:40:42 +0800 | [diff] [blame] | 207 | NetUtils.maskRawAddress(otherAddress, prefixLength); |
Chalard Jean | 9cbc882 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 208 | return Arrays.equals(otherAddress, address); |
| 209 | } |
| 210 | |
| 211 | /** |
Hugo Benichi | 8253be9 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 212 | * @hide |
| 213 | */ |
| 214 | public boolean isIPv6() { |
| 215 | return getAddress() instanceof Inet6Address; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @hide |
| 220 | */ |
| 221 | public boolean isIPv4() { |
| 222 | return getAddress() instanceof Inet4Address; |
| 223 | } |
| 224 | |
| 225 | /** |
Lorenzo Colitti | 174bab2 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 226 | * Returns a string representation of this {@code IpPrefix}. |
| 227 | * |
Lorenzo Colitti | 2129034 | 2014-09-19 01:49:05 +0900 | [diff] [blame] | 228 | * @return a string such as {@code "192.0.2.0/24"} or {@code "2001:db8:1:2::/64"}. |
Lorenzo Colitti | 174bab2 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 229 | */ |
| 230 | public String toString() { |
| 231 | try { |
| 232 | return InetAddress.getByAddress(address).getHostAddress() + "/" + prefixLength; |
| 233 | } catch(UnknownHostException e) { |
| 234 | // Cosmic rays? |
| 235 | throw new IllegalStateException("IpPrefix with invalid address! Shouldn't happen.", e); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 240 | * Implement the Parcelable interface. |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 241 | */ |
| 242 | public int describeContents() { |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Implement the Parcelable interface. |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 248 | */ |
| 249 | public void writeToParcel(Parcel dest, int flags) { |
| 250 | dest.writeByteArray(address); |
| 251 | dest.writeInt(prefixLength); |
| 252 | } |
| 253 | |
| 254 | /** |
Chalard Jean | 9cbc882 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 255 | * Returns a comparator ordering IpPrefixes by length, shorter to longer. |
| 256 | * Contents of the address will break ties. |
| 257 | * @hide |
| 258 | */ |
| 259 | public static Comparator<IpPrefix> lengthComparator() { |
| 260 | return new Comparator<IpPrefix>() { |
| 261 | @Override |
| 262 | public int compare(IpPrefix prefix1, IpPrefix prefix2) { |
| 263 | if (prefix1.isIPv4()) { |
| 264 | if (prefix2.isIPv6()) return -1; |
| 265 | } else { |
| 266 | if (prefix2.isIPv4()) return 1; |
| 267 | } |
| 268 | final int p1len = prefix1.getPrefixLength(); |
| 269 | final int p2len = prefix2.getPrefixLength(); |
| 270 | if (p1len < p2len) return -1; |
| 271 | if (p2len < p1len) return 1; |
| 272 | final byte[] a1 = prefix1.address; |
| 273 | final byte[] a2 = prefix2.address; |
| 274 | final int len = a1.length < a2.length ? a1.length : a2.length; |
| 275 | for (int i = 0; i < len; ++i) { |
| 276 | if (a1[i] < a2[i]) return -1; |
| 277 | if (a1[i] > a2[i]) return 1; |
| 278 | } |
| 279 | if (a2.length < len) return 1; |
| 280 | if (a1.length < len) return -1; |
| 281 | return 0; |
| 282 | } |
| 283 | }; |
| 284 | } |
| 285 | |
| 286 | /** |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 287 | * Implement the Parcelable interface. |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 288 | */ |
Jeff Sharkey | f852528 | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 289 | public static final @android.annotation.NonNull Creator<IpPrefix> CREATOR = |
Sreeram Ramachandran | 887d7b1 | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 290 | new Creator<IpPrefix>() { |
| 291 | public IpPrefix createFromParcel(Parcel in) { |
| 292 | byte[] address = in.createByteArray(); |
| 293 | int prefixLength = in.readInt(); |
| 294 | return new IpPrefix(address, prefixLength); |
| 295 | } |
| 296 | |
| 297 | public IpPrefix[] newArray(int size) { |
| 298 | return new IpPrefix[size]; |
| 299 | } |
| 300 | }; |
| 301 | } |