Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Hugo Benichi | ce7b23c | 2017-06-23 10:07:08 +0900 | [diff] [blame] | 19 | import static android.system.OsConstants.IFA_F_DADFAILED; |
| 20 | import static android.system.OsConstants.IFA_F_DEPRECATED; |
| 21 | import static android.system.OsConstants.IFA_F_OPTIMISTIC; |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 22 | import static android.system.OsConstants.IFA_F_PERMANENT; |
Hugo Benichi | ce7b23c | 2017-06-23 10:07:08 +0900 | [diff] [blame] | 23 | import static android.system.OsConstants.IFA_F_TENTATIVE; |
| 24 | import static android.system.OsConstants.RT_SCOPE_HOST; |
| 25 | import static android.system.OsConstants.RT_SCOPE_LINK; |
| 26 | import static android.system.OsConstants.RT_SCOPE_SITE; |
| 27 | import static android.system.OsConstants.RT_SCOPE_UNIVERSE; |
| 28 | |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 29 | import android.annotation.IntRange; |
Jack Yu | 3ee7ba1 | 2019-03-01 12:04:50 -0800 | [diff] [blame] | 30 | import android.annotation.NonNull; |
paulhu | 9bb0480 | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 31 | import android.annotation.Nullable; |
Jack Yu | 13fefdd | 2018-11-14 22:04:17 -0800 | [diff] [blame] | 32 | import android.annotation.SystemApi; |
Artur Satayev | 0e45d78 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 33 | import android.compat.annotation.UnsupportedAppUsage; |
Mathew Inwood | ac5968e | 2018-12-20 15:30:45 +0000 | [diff] [blame] | 34 | import android.os.Build; |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 35 | import android.os.Parcel; |
| 36 | import android.os.Parcelable; |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 37 | import android.os.SystemClock; |
Lorenzo Colitti | 2e9b123 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 38 | import android.util.Pair; |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 39 | |
Robert Greenwalt | 7fae12c | 2011-02-11 17:01:02 -0800 | [diff] [blame] | 40 | import java.net.Inet4Address; |
Erik Kline | c17b528 | 2014-10-20 19:46:56 +0900 | [diff] [blame] | 41 | import java.net.Inet6Address; |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 42 | import java.net.InetAddress; |
| 43 | import java.net.InterfaceAddress; |
| 44 | import java.net.UnknownHostException; |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 45 | import java.util.Objects; |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 48 | * Identifies an IP address on a network link. |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 49 | * |
| 50 | * A {@code LinkAddress} consists of: |
| 51 | * <ul> |
| 52 | * <li>An IP address and prefix length (e.g., {@code 2001:db8::1/64} or {@code 192.0.2.1/24}). |
| 53 | * The address must be unicast, as multicast addresses cannot be assigned to interfaces. |
Lorenzo Colitti | 48a7da0 | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 54 | * <li>Address flags: A bitmask of {@code OsConstants.IFA_F_*} values representing properties |
| 55 | * of the address (e.g., {@code android.system.OsConstants.IFA_F_OPTIMISTIC}). |
| 56 | * <li>Address scope: One of the {@code OsConstants.IFA_F_*} values; defines the scope in which |
| 57 | * the address is unique (e.g., |
| 58 | * {@code android.system.OsConstants.RT_SCOPE_LINK} or |
| 59 | * {@code android.system.OsConstants.RT_SCOPE_UNIVERSE}). |
| 60 | * </ul> |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 61 | */ |
| 62 | public class LinkAddress implements Parcelable { |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Indicates the deprecation or expiration time is unknown |
| 66 | * @hide |
| 67 | */ |
| 68 | @SystemApi |
| 69 | public static final long LIFETIME_UNKNOWN = -1; |
| 70 | |
| 71 | /** |
| 72 | * Indicates this address is permanent. |
| 73 | * @hide |
| 74 | */ |
| 75 | @SystemApi |
| 76 | public static final long LIFETIME_PERMANENT = Long.MAX_VALUE; |
| 77 | |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 78 | /** |
| 79 | * IPv4 or IPv6 address. |
| 80 | */ |
Mathew Inwood | ac5968e | 2018-12-20 15:30:45 +0000 | [diff] [blame] | 81 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Lorenzo Colitti | 6efd389 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 82 | private InetAddress address; |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 85 | * Prefix length. |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 86 | */ |
Mathew Inwood | ac5968e | 2018-12-20 15:30:45 +0000 | [diff] [blame] | 87 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Lorenzo Colitti | 6efd389 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 88 | private int prefixLength; |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 89 | |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 90 | /** |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 91 | * Address flags. A bitmask of {@code IFA_F_*} values. Note that {@link #getFlags()} may not |
| 92 | * return these exact values. For example, it may set or clear the {@code IFA_F_DEPRECATED} |
| 93 | * flag depending on the current preferred lifetime. |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 94 | */ |
| 95 | private int flags; |
| 96 | |
| 97 | /** |
| 98 | * Address scope. One of the RT_SCOPE_* constants. |
| 99 | */ |
| 100 | private int scope; |
| 101 | |
| 102 | /** |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 103 | * The time, as reported by {@link SystemClock#elapsedRealtime}, when this LinkAddress will be |
Jack Yu | 7782a0d | 2020-01-26 15:52:11 -0800 | [diff] [blame] | 104 | * or was deprecated. At the time existing connections can still use this address until it |
| 105 | * expires, but new connections should use the new address. {@link #LIFETIME_UNKNOWN} indicates |
| 106 | * this information is not available. {@link #LIFETIME_PERMANENT} indicates this |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 107 | * {@link LinkAddress} will never be deprecated. |
| 108 | */ |
| 109 | private long deprecationTime; |
| 110 | |
| 111 | /** |
| 112 | * The time, as reported by {@link SystemClock#elapsedRealtime}, when this {@link LinkAddress} |
| 113 | * will expire and be removed from the interface. {@link #LIFETIME_UNKNOWN} indicates this |
| 114 | * information is not available. {@link #LIFETIME_PERMANENT} indicates this {@link LinkAddress} |
| 115 | * will never expire. |
| 116 | */ |
| 117 | private long expirationTime; |
| 118 | |
| 119 | /** |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 120 | * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and |
| 121 | * RFC 6724 section 3.2. |
| 122 | * @hide |
| 123 | */ |
Hugo Benichi | 3054e10 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 124 | private static int scopeForUnicastAddress(InetAddress addr) { |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 125 | if (addr.isAnyLocalAddress()) { |
| 126 | return RT_SCOPE_HOST; |
| 127 | } |
| 128 | |
| 129 | if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) { |
| 130 | return RT_SCOPE_LINK; |
| 131 | } |
| 132 | |
| 133 | // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2 |
| 134 | // says that they are assigned global scope. |
| 135 | if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) { |
| 136 | return RT_SCOPE_SITE; |
| 137 | } |
| 138 | |
| 139 | return RT_SCOPE_UNIVERSE; |
| 140 | } |
| 141 | |
| 142 | /** |
Erik Kline | c17b528 | 2014-10-20 19:46:56 +0900 | [diff] [blame] | 143 | * Utility function to check if |address| is a Unique Local IPv6 Unicast Address |
| 144 | * (a.k.a. "ULA"; RFC 4193). |
| 145 | * |
| 146 | * Per RFC 4193 section 8, fc00::/7 identifies these addresses. |
| 147 | */ |
paulhu | 9bb0480 | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 148 | private boolean isIpv6ULA() { |
| 149 | if (isIpv6()) { |
Erik Kline | c17b528 | 2014-10-20 19:46:56 +0900 | [diff] [blame] | 150 | byte[] bytes = address.getAddress(); |
Erik Kline | 4ab3a3a | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 151 | return ((bytes[0] & (byte)0xfe) == (byte)0xfc); |
Erik Kline | c17b528 | 2014-10-20 19:46:56 +0900 | [diff] [blame] | 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | /** |
Hugo Benichi | 3054e10 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 157 | * @return true if the address is IPv6. |
| 158 | * @hide |
| 159 | */ |
Remi NGUYEN VAN | 5af2e29 | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 160 | @SystemApi |
paulhu | 9bb0480 | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 161 | public boolean isIpv6() { |
Hugo Benichi | 3054e10 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 162 | return address instanceof Inet6Address; |
| 163 | } |
| 164 | |
| 165 | /** |
paulhu | 9bb0480 | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 166 | * For backward compatibility. |
| 167 | * This was annotated with @UnsupportedAppUsage in P, so we can't remove the method completely |
| 168 | * just yet. |
| 169 | * @return true if the address is IPv6. |
| 170 | * @hide |
| 171 | */ |
| 172 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 173 | public boolean isIPv6() { |
| 174 | return isIpv6(); |
| 175 | } |
| 176 | |
| 177 | /** |
Hugo Benichi | 3054e10 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 178 | * @return true if the address is IPv4 or is a mapped IPv4 address. |
| 179 | * @hide |
| 180 | */ |
Remi NGUYEN VAN | 5af2e29 | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 181 | @SystemApi |
paulhu | 9bb0480 | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 182 | public boolean isIpv4() { |
Hugo Benichi | 3054e10 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 183 | return address instanceof Inet4Address; |
| 184 | } |
| 185 | |
| 186 | /** |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 187 | * Utility function for the constructors. |
| 188 | */ |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 189 | private void init(InetAddress address, int prefixLength, int flags, int scope, |
| 190 | long deprecationTime, long expirationTime) { |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 191 | if (address == null || |
| 192 | address.isMulticastAddress() || |
| 193 | prefixLength < 0 || |
Hugo Benichi | 3054e10 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 194 | (address instanceof Inet4Address && prefixLength > 32) || |
Robert Greenwalt | 7fae12c | 2011-02-11 17:01:02 -0800 | [diff] [blame] | 195 | (prefixLength > 128)) { |
| 196 | throw new IllegalArgumentException("Bad LinkAddress params " + address + |
Lorenzo Colitti | 6efd389 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 197 | "/" + prefixLength); |
Robert Greenwalt | 7fae12c | 2011-02-11 17:01:02 -0800 | [diff] [blame] | 198 | } |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 199 | |
| 200 | // deprecation time and expiration time must be both provided, or neither. |
| 201 | if ((deprecationTime == LIFETIME_UNKNOWN) != (expirationTime == LIFETIME_UNKNOWN)) { |
| 202 | throw new IllegalArgumentException( |
| 203 | "Must not specify only one of deprecation time and expiration time"); |
| 204 | } |
| 205 | |
| 206 | // deprecation time needs to be a positive value. |
| 207 | if (deprecationTime != LIFETIME_UNKNOWN && deprecationTime < 0) { |
| 208 | throw new IllegalArgumentException("invalid deprecation time " + deprecationTime); |
| 209 | } |
| 210 | |
| 211 | // expiration time needs to be a positive value. |
| 212 | if (expirationTime != LIFETIME_UNKNOWN && expirationTime < 0) { |
| 213 | throw new IllegalArgumentException("invalid expiration time " + expirationTime); |
| 214 | } |
| 215 | |
| 216 | // expiration time can't be earlier than deprecation time |
| 217 | if (deprecationTime != LIFETIME_UNKNOWN && expirationTime != LIFETIME_UNKNOWN |
| 218 | && expirationTime < deprecationTime) { |
| 219 | throw new IllegalArgumentException("expiration earlier than deprecation (" |
| 220 | + deprecationTime + ", " + expirationTime + ")"); |
| 221 | } |
| 222 | |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 223 | this.address = address; |
Irfan Sheriff | e0b2c0f | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 224 | this.prefixLength = prefixLength; |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 225 | this.flags = flags; |
| 226 | this.scope = scope; |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 227 | this.deprecationTime = deprecationTime; |
| 228 | this.expirationTime = expirationTime; |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with |
| 233 | * the specified flags and scope. Flags and scope are not checked for validity. |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 234 | * |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 235 | * @param address The IP address. |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 236 | * @param prefixLength The prefix length. Must be >= 0 and <= (32 or 128) (IPv4 or IPv6). |
Robert Greenwalt | 6024002 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 237 | * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address. |
| 238 | * @param scope An integer defining the scope in which the address is unique (e.g., |
| 239 | * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}). |
| 240 | * @hide |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 241 | */ |
Remi NGUYEN VAN | 155b7b7 | 2019-01-23 21:35:52 +0900 | [diff] [blame] | 242 | @SystemApi |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 243 | public LinkAddress(@NonNull InetAddress address, @IntRange(from = 0, to = 128) int prefixLength, |
| 244 | int flags, int scope) { |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 245 | init(address, prefixLength, flags, scope, LIFETIME_UNKNOWN, LIFETIME_UNKNOWN); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Constructs a new {@code LinkAddress} from an {@code InetAddress}, prefix length, with |
| 250 | * the specified flags, scope, deprecation time, and expiration time. Flags and scope are not |
| 251 | * checked for validity. The value of the {@code IFA_F_DEPRECATED} and {@code IFA_F_PERMANENT} |
| 252 | * flag will be adjusted based on the passed-in lifetimes. |
| 253 | * |
| 254 | * @param address The IP address. |
| 255 | * @param prefixLength The prefix length. Must be >= 0 and <= (32 or 128) (IPv4 or IPv6). |
| 256 | * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address. |
| 257 | * @param scope An integer defining the scope in which the address is unique (e.g., |
| 258 | * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}). |
| 259 | * @param deprecationTime The time, as reported by {@link SystemClock#elapsedRealtime}, when |
Jack Yu | 7782a0d | 2020-01-26 15:52:11 -0800 | [diff] [blame] | 260 | * this {@link LinkAddress} will be or was deprecated. At the time |
| 261 | * existing connections can still use this address until it expires, but |
| 262 | * new connections should use the new address. {@link #LIFETIME_UNKNOWN} |
| 263 | * indicates this information is not available. |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 264 | * {@link #LIFETIME_PERMANENT} indicates this {@link LinkAddress} will |
| 265 | * never be deprecated. |
| 266 | * @param expirationTime The time, as reported by {@link SystemClock#elapsedRealtime}, when this |
| 267 | * {@link LinkAddress} will expire and be removed from the interface. |
| 268 | * {@link #LIFETIME_UNKNOWN} indicates this information is not available. |
| 269 | * {@link #LIFETIME_PERMANENT} indicates this {@link LinkAddress} will |
| 270 | * never expire. |
| 271 | * @hide |
| 272 | */ |
| 273 | @SystemApi |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 274 | public LinkAddress(@NonNull InetAddress address, @IntRange(from = 0, to = 128) int prefixLength, |
| 275 | int flags, int scope, long deprecationTime, long expirationTime) { |
| 276 | init(address, prefixLength, flags, scope, deprecationTime, expirationTime); |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 279 | /** |
| 280 | * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length. |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 281 | * The flags are set to zero and the scope is determined from the address. |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 282 | * @param address The IP address. |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 283 | * @param prefixLength The prefix length. Must be >= 0 and <= (32 or 128) (IPv4 or IPv6). |
Robert Greenwalt | 6024002 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 284 | * @hide |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 285 | */ |
Jack Yu | 13fefdd | 2018-11-14 22:04:17 -0800 | [diff] [blame] | 286 | @SystemApi |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 287 | public LinkAddress(@NonNull InetAddress address, |
| 288 | @IntRange(from = 0, to = 128) int prefixLength) { |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 289 | this(address, prefixLength, 0, 0); |
| 290 | this.scope = scopeForUnicastAddress(address); |
Lorenzo Colitti | 6efd389 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 291 | } |
| 292 | |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 293 | /** |
| 294 | * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}. |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 295 | * The flags are set to zero and the scope is determined from the address. |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 296 | * @param interfaceAddress The interface address. |
Robert Greenwalt | 6024002 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 297 | * @hide |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 298 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 299 | public LinkAddress(@NonNull InterfaceAddress interfaceAddress) { |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 300 | this(interfaceAddress.getAddress(), |
Lorenzo Colitti | 6efd389 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 301 | interfaceAddress.getNetworkPrefixLength()); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 306 | * "2001:db8::1/64". The flags are set to zero and the scope is determined from the address. |
Jack Yu | 3ee7ba1 | 2019-03-01 12:04:50 -0800 | [diff] [blame] | 307 | * @param address The string to parse. |
Robert Greenwalt | 6024002 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 308 | * @hide |
Lorenzo Colitti | 6efd389 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 309 | */ |
Jack Yu | 13fefdd | 2018-11-14 22:04:17 -0800 | [diff] [blame] | 310 | @SystemApi |
Jack Yu | 3ee7ba1 | 2019-03-01 12:04:50 -0800 | [diff] [blame] | 311 | public LinkAddress(@NonNull String address) { |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 312 | this(address, 0, 0); |
| 313 | this.scope = scopeForUnicastAddress(this.address); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or |
| 318 | * "2001:db8::1/64", with the specified flags and scope. |
Jack Yu | 3ee7ba1 | 2019-03-01 12:04:50 -0800 | [diff] [blame] | 319 | * @param address The string to parse. |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 320 | * @param flags The address flags. |
| 321 | * @param scope The address scope. |
Robert Greenwalt | 6024002 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 322 | * @hide |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 323 | */ |
Remi NGUYEN VAN | a5225ea | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 324 | @SystemApi |
paulhu | 9bb0480 | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 325 | public LinkAddress(@NonNull String address, int flags, int scope) { |
Lorenzo Colitti | 2e9b123 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 326 | // This may throw an IllegalArgumentException; catching it is the caller's responsibility. |
Hugo Benichi | 3054e10 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 327 | // TODO: consider rejecting mapped IPv4 addresses such as "::ffff:192.0.2.5/24". |
paulhu | cbe7381 | 2021-03-03 22:15:11 +0800 | [diff] [blame^] | 328 | Pair<InetAddress, Integer> ipAndMask = NetworkUtils.legacyParseIpAndMask(address); |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 329 | init(ipAndMask.first, ipAndMask.second, flags, scope, LIFETIME_UNKNOWN, LIFETIME_UNKNOWN); |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 332 | /** |
| 333 | * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64". |
| 334 | * The string representation does not contain the flags and scope, just the address and prefix |
| 335 | * length. |
| 336 | */ |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 337 | @Override |
| 338 | public String toString() { |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 339 | return address.getHostAddress() + "/" + prefixLength; |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 343 | * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if |
Lorenzo Colitti | 48a7da0 | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 344 | * their address, prefix length, flags and scope are equal. Thus, for example, two addresses |
| 345 | * that have the same address and prefix length are not equal if one of them is deprecated and |
| 346 | * the other is not. |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 347 | * |
| 348 | * @param obj the object to be tested for equality. |
| 349 | * @return {@code true} if both objects are equal, {@code false} otherwise. |
| 350 | */ |
| 351 | @Override |
Roman Kalukiewicz | 1f69a5e | 2020-10-14 15:59:06 -0700 | [diff] [blame] | 352 | public boolean equals(@Nullable Object obj) { |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 353 | if (!(obj instanceof LinkAddress)) { |
| 354 | return false; |
| 355 | } |
| 356 | LinkAddress linkAddress = (LinkAddress) obj; |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 357 | return this.address.equals(linkAddress.address) |
| 358 | && this.prefixLength == linkAddress.prefixLength |
| 359 | && this.flags == linkAddress.flags |
| 360 | && this.scope == linkAddress.scope |
| 361 | && this.deprecationTime == linkAddress.deprecationTime |
| 362 | && this.expirationTime == linkAddress.expirationTime; |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 365 | /** |
| 366 | * Returns a hashcode for this address. |
John Wang | 3e567d5 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 367 | */ |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 368 | @Override |
John Wang | 3e567d5 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 369 | public int hashCode() { |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 370 | return Objects.hash(address, prefixLength, flags, scope, deprecationTime, expirationTime); |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /** |
Robert Greenwalt | 6024002 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 374 | * Determines whether this {@code LinkAddress} and the provided {@code LinkAddress} |
| 375 | * represent the same address. Two {@code LinkAddresses} represent the same address |
| 376 | * if they have the same IP address and prefix length, even if their properties are |
| 377 | * different. |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 378 | * |
| 379 | * @param other the {@code LinkAddress} to compare to. |
| 380 | * @return {@code true} if both objects have the same address and prefix length, {@code false} |
| 381 | * otherwise. |
Lorenzo Colitti | 48a7da0 | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 382 | * @hide |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 383 | */ |
Remi NGUYEN VAN | 5af2e29 | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 384 | @SystemApi |
paulhu | 9bb0480 | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 385 | public boolean isSameAddressAs(@Nullable LinkAddress other) { |
| 386 | if (other == null) { |
| 387 | return false; |
| 388 | } |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 389 | return address.equals(other.address) && prefixLength == other.prefixLength; |
John Wang | 3e567d5 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 392 | /** |
Robert Greenwalt | 6024002 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 393 | * Returns the {@link InetAddress} of this {@code LinkAddress}. |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 394 | */ |
| 395 | public InetAddress getAddress() { |
| 396 | return address; |
| 397 | } |
| 398 | |
| 399 | /** |
Robert Greenwalt | 6024002 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 400 | * Returns the prefix length of this {@code LinkAddress}. |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 401 | */ |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 402 | @IntRange(from = 0, to = 128) |
Lorenzo Colitti | 48a7da0 | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 403 | public int getPrefixLength() { |
Irfan Sheriff | e0b2c0f | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 404 | return prefixLength; |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /** |
Lorenzo Colitti | 48a7da0 | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 408 | * Returns the prefix length of this {@code LinkAddress}. |
| 409 | * TODO: Delete all callers and remove in favour of getPrefixLength(). |
| 410 | * @hide |
| 411 | */ |
Mathew Inwood | 0b8f861 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 412 | @UnsupportedAppUsage |
paulhu | 6f2c1b5 | 2019-03-27 22:26:37 +0800 | [diff] [blame] | 413 | @IntRange(from = 0, to = 128) |
Lorenzo Colitti | 48a7da0 | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 414 | public int getNetworkPrefixLength() { |
| 415 | return getPrefixLength(); |
| 416 | } |
| 417 | |
| 418 | /** |
Robert Greenwalt | 6024002 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 419 | * Returns the flags of this {@code LinkAddress}. |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 420 | */ |
| 421 | public int getFlags() { |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 422 | int flags = this.flags; |
| 423 | if (deprecationTime != LIFETIME_UNKNOWN) { |
| 424 | if (SystemClock.elapsedRealtime() >= deprecationTime) { |
| 425 | flags |= IFA_F_DEPRECATED; |
| 426 | } else { |
| 427 | // If deprecation time is in the future, or permanent. |
| 428 | flags &= ~IFA_F_DEPRECATED; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (expirationTime == LIFETIME_PERMANENT) { |
| 433 | flags |= IFA_F_PERMANENT; |
| 434 | } else if (expirationTime != LIFETIME_UNKNOWN) { |
Jack Yu | 7782a0d | 2020-01-26 15:52:11 -0800 | [diff] [blame] | 435 | // If we know this address expired or will expire in the future, then this address |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 436 | // should not be permanent. |
| 437 | flags &= ~IFA_F_PERMANENT; |
| 438 | } |
| 439 | |
| 440 | // Do no touch the original flags. Return the adjusted flags here. |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 441 | return flags; |
| 442 | } |
| 443 | |
| 444 | /** |
Robert Greenwalt | 6024002 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 445 | * Returns the scope of this {@code LinkAddress}. |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 446 | */ |
| 447 | public int getScope() { |
| 448 | return scope; |
| 449 | } |
| 450 | |
| 451 | /** |
Jack Yu | 7782a0d | 2020-01-26 15:52:11 -0800 | [diff] [blame] | 452 | * Get the deprecation time, as reported by {@link SystemClock#elapsedRealtime}, when this |
| 453 | * {@link LinkAddress} will be or was deprecated. At the time existing connections can still use |
| 454 | * this address until it expires, but new connections should use the new address. |
| 455 | * |
| 456 | * @return The deprecation time in milliseconds. {@link #LIFETIME_UNKNOWN} indicates this |
| 457 | * information is not available. {@link #LIFETIME_PERMANENT} indicates this {@link LinkAddress} |
| 458 | * will never be deprecated. |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 459 | * |
| 460 | * @hide |
| 461 | */ |
| 462 | @SystemApi |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 463 | public long getDeprecationTime() { |
| 464 | return deprecationTime; |
| 465 | } |
| 466 | |
| 467 | /** |
Jack Yu | 7782a0d | 2020-01-26 15:52:11 -0800 | [diff] [blame] | 468 | * Get the expiration time, as reported by {@link SystemClock#elapsedRealtime}, when this |
| 469 | * {@link LinkAddress} will expire and be removed from the interface. |
| 470 | * |
| 471 | * @return The expiration time in milliseconds. {@link #LIFETIME_UNKNOWN} indicates this |
| 472 | * information is not available. {@link #LIFETIME_PERMANENT} indicates this {@link LinkAddress} |
| 473 | * will never expire. |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 474 | * |
| 475 | * @hide |
| 476 | */ |
| 477 | @SystemApi |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 478 | public long getExpirationTime() { |
| 479 | return expirationTime; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Returns true if this {@code LinkAddress} is global scope and preferred (i.e., not currently |
| 484 | * deprecated). |
| 485 | * |
Robert Greenwalt | 6024002 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 486 | * @hide |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 487 | */ |
Remi NGUYEN VAN | 5af2e29 | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 488 | @SystemApi |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 489 | public boolean isGlobalPreferred() { |
Erik Kline | c17b528 | 2014-10-20 19:46:56 +0900 | [diff] [blame] | 490 | /** |
| 491 | * Note that addresses flagged as IFA_F_OPTIMISTIC are |
| 492 | * simultaneously flagged as IFA_F_TENTATIVE (when the tentative |
| 493 | * state has cleared either DAD has succeeded or failed, and both |
| 494 | * flags are cleared regardless). |
| 495 | */ |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 496 | int flags = getFlags(); |
paulhu | 9bb0480 | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 497 | return (scope == RT_SCOPE_UNIVERSE |
| 498 | && !isIpv6ULA() |
| 499 | && (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED)) == 0L |
| 500 | && ((flags & IFA_F_TENTATIVE) == 0L || (flags & IFA_F_OPTIMISTIC) != 0L)); |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /** |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 504 | * Implement the Parcelable interface. |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 505 | */ |
| 506 | public int describeContents() { |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Implement the Parcelable interface. |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 512 | */ |
| 513 | public void writeToParcel(Parcel dest, int flags) { |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 514 | dest.writeByteArray(address.getAddress()); |
| 515 | dest.writeInt(prefixLength); |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 516 | dest.writeInt(this.flags); |
| 517 | dest.writeInt(scope); |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 518 | dest.writeLong(deprecationTime); |
| 519 | dest.writeLong(expirationTime); |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Implement the Parcelable interface. |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 524 | */ |
Jeff Sharkey | 9286f91 | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 525 | public static final @android.annotation.NonNull Creator<LinkAddress> CREATOR = |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 526 | new Creator<LinkAddress>() { |
| 527 | public LinkAddress createFromParcel(Parcel in) { |
| 528 | InetAddress address = null; |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 529 | try { |
| 530 | address = InetAddress.getByAddress(in.createByteArray()); |
| 531 | } catch (UnknownHostException e) { |
| 532 | // Nothing we can do here. When we call the constructor, we'll throw an |
| 533 | // IllegalArgumentException, because a LinkAddress can't have a null |
| 534 | // InetAddress. |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 535 | } |
Lorenzo Colitti | 64eb7fd | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 536 | int prefixLength = in.readInt(); |
Lorenzo Colitti | 4ea70b7 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 537 | int flags = in.readInt(); |
| 538 | int scope = in.readInt(); |
Jack Yu | 1fe4b30 | 2020-01-17 15:28:50 -0800 | [diff] [blame] | 539 | long deprecationTime = in.readLong(); |
| 540 | long expirationTime = in.readLong(); |
| 541 | return new LinkAddress(address, prefixLength, flags, scope, deprecationTime, |
| 542 | expirationTime); |
Irfan Sheriff | fb7c964 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | public LinkAddress[] newArray(int size) { |
| 546 | return new LinkAddress[size]; |
| 547 | } |
| 548 | }; |
| 549 | } |