blob: 8376963857bb3bd36925fd38cc554d33a027dd7a [file] [log] [blame]
Irfan Sherifffb7c9642010-10-01 16:08:28 -07001/*
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
17package android.net;
18
Hugo Benichice7b23c2017-06-23 10:07:08 +090019import static android.system.OsConstants.IFA_F_DADFAILED;
20import static android.system.OsConstants.IFA_F_DEPRECATED;
21import static android.system.OsConstants.IFA_F_OPTIMISTIC;
Jack Yu1fe4b302020-01-17 15:28:50 -080022import static android.system.OsConstants.IFA_F_PERMANENT;
Hugo Benichice7b23c2017-06-23 10:07:08 +090023import static android.system.OsConstants.IFA_F_TENTATIVE;
24import static android.system.OsConstants.RT_SCOPE_HOST;
25import static android.system.OsConstants.RT_SCOPE_LINK;
26import static android.system.OsConstants.RT_SCOPE_SITE;
27import static android.system.OsConstants.RT_SCOPE_UNIVERSE;
28
paulhu6f2c1b52019-03-27 22:26:37 +080029import android.annotation.IntRange;
Jack Yu3ee7ba12019-03-01 12:04:50 -080030import android.annotation.NonNull;
paulhu9bb04802019-03-08 16:35:20 +080031import android.annotation.Nullable;
Jack Yu13fefdd2018-11-14 22:04:17 -080032import android.annotation.SystemApi;
Artur Satayev0e45d782019-12-10 17:47:52 +000033import android.compat.annotation.UnsupportedAppUsage;
Mathew Inwoodac5968e2018-12-20 15:30:45 +000034import android.os.Build;
Irfan Sherifffb7c9642010-10-01 16:08:28 -070035import android.os.Parcel;
36import android.os.Parcelable;
Jack Yu1fe4b302020-01-17 15:28:50 -080037import android.os.SystemClock;
Lorenzo Colitti2e9b1232014-06-12 13:41:17 +090038import android.util.Pair;
Irfan Sherifffb7c9642010-10-01 16:08:28 -070039
Xiao Maa74b0062023-07-18 19:14:51 +090040import com.android.net.module.util.ConnectivityUtils;
41
Robert Greenwalt7fae12c2011-02-11 17:01:02 -080042import java.net.Inet4Address;
Erik Klinec17b5282014-10-20 19:46:56 +090043import java.net.Inet6Address;
Irfan Sherifffb7c9642010-10-01 16:08:28 -070044import java.net.InetAddress;
45import java.net.InterfaceAddress;
46import java.net.UnknownHostException;
Jack Yu1fe4b302020-01-17 15:28:50 -080047import java.util.Objects;
Irfan Sherifffb7c9642010-10-01 16:08:28 -070048
49/**
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +090050 * Identifies an IP address on a network link.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +090051 *
52 * A {@code LinkAddress} consists of:
53 * <ul>
54 * <li>An IP address and prefix length (e.g., {@code 2001:db8::1/64} or {@code 192.0.2.1/24}).
55 * The address must be unicast, as multicast addresses cannot be assigned to interfaces.
Lorenzo Colitti48a7da02014-06-09 22:58:46 +090056 * <li>Address flags: A bitmask of {@code OsConstants.IFA_F_*} values representing properties
57 * of the address (e.g., {@code android.system.OsConstants.IFA_F_OPTIMISTIC}).
58 * <li>Address scope: One of the {@code OsConstants.IFA_F_*} values; defines the scope in which
59 * the address is unique (e.g.,
60 * {@code android.system.OsConstants.RT_SCOPE_LINK} or
61 * {@code android.system.OsConstants.RT_SCOPE_UNIVERSE}).
62 * </ul>
Irfan Sherifffb7c9642010-10-01 16:08:28 -070063 */
64public class LinkAddress implements Parcelable {
Jack Yu1fe4b302020-01-17 15:28:50 -080065
66 /**
67 * Indicates the deprecation or expiration time is unknown
68 * @hide
69 */
70 @SystemApi
71 public static final long LIFETIME_UNKNOWN = -1;
72
73 /**
74 * Indicates this address is permanent.
75 * @hide
76 */
77 @SystemApi
78 public static final long LIFETIME_PERMANENT = Long.MAX_VALUE;
79
Irfan Sherifffb7c9642010-10-01 16:08:28 -070080 /**
81 * IPv4 or IPv6 address.
82 */
Mathew Inwoodac5968e2018-12-20 15:30:45 +000083 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Lorenzo Colitti6efd3892013-08-08 19:24:09 +090084 private InetAddress address;
Irfan Sherifffb7c9642010-10-01 16:08:28 -070085
86 /**
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +090087 * Prefix length.
Irfan Sherifffb7c9642010-10-01 16:08:28 -070088 */
Mathew Inwoodac5968e2018-12-20 15:30:45 +000089 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Lorenzo Colitti6efd3892013-08-08 19:24:09 +090090 private int prefixLength;
Irfan Sherifffb7c9642010-10-01 16:08:28 -070091
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +090092 /**
Jack Yu1fe4b302020-01-17 15:28:50 -080093 * Address flags. A bitmask of {@code IFA_F_*} values. Note that {@link #getFlags()} may not
94 * return these exact values. For example, it may set or clear the {@code IFA_F_DEPRECATED}
95 * flag depending on the current preferred lifetime.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +090096 */
97 private int flags;
98
99 /**
100 * Address scope. One of the RT_SCOPE_* constants.
101 */
102 private int scope;
103
104 /**
Jack Yu1fe4b302020-01-17 15:28:50 -0800105 * The time, as reported by {@link SystemClock#elapsedRealtime}, when this LinkAddress will be
Jack Yu7782a0d2020-01-26 15:52:11 -0800106 * or was deprecated. At the time existing connections can still use this address until it
107 * expires, but new connections should use the new address. {@link #LIFETIME_UNKNOWN} indicates
108 * this information is not available. {@link #LIFETIME_PERMANENT} indicates this
Jack Yu1fe4b302020-01-17 15:28:50 -0800109 * {@link LinkAddress} will never be deprecated.
110 */
111 private long deprecationTime;
112
113 /**
114 * The time, as reported by {@link SystemClock#elapsedRealtime}, when this {@link LinkAddress}
115 * will expire and be removed from the interface. {@link #LIFETIME_UNKNOWN} indicates this
116 * information is not available. {@link #LIFETIME_PERMANENT} indicates this {@link LinkAddress}
117 * will never expire.
118 */
119 private long expirationTime;
120
121 /**
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900122 * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and
123 * RFC 6724 section 3.2.
124 * @hide
125 */
Hugo Benichi3054e102017-08-08 13:06:04 +0900126 private static int scopeForUnicastAddress(InetAddress addr) {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900127 if (addr.isAnyLocalAddress()) {
128 return RT_SCOPE_HOST;
129 }
130
131 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) {
132 return RT_SCOPE_LINK;
133 }
134
135 // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2
136 // says that they are assigned global scope.
137 if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) {
138 return RT_SCOPE_SITE;
139 }
140
141 return RT_SCOPE_UNIVERSE;
142 }
143
144 /**
Erik Klinec17b5282014-10-20 19:46:56 +0900145 * Utility function to check if |address| is a Unique Local IPv6 Unicast Address
146 * (a.k.a. "ULA"; RFC 4193).
147 *
148 * Per RFC 4193 section 8, fc00::/7 identifies these addresses.
149 */
paulhu9bb04802019-03-08 16:35:20 +0800150 private boolean isIpv6ULA() {
Xiao Maa74b0062023-07-18 19:14:51 +0900151 return ConnectivityUtils.isIPv6ULA(address);
Erik Klinec17b5282014-10-20 19:46:56 +0900152 }
153
154 /**
Hugo Benichi3054e102017-08-08 13:06:04 +0900155 * @return true if the address is IPv6.
156 * @hide
157 */
Remi NGUYEN VAN5af2e292019-01-20 12:52:43 +0900158 @SystemApi
paulhu9bb04802019-03-08 16:35:20 +0800159 public boolean isIpv6() {
Hugo Benichi3054e102017-08-08 13:06:04 +0900160 return address instanceof Inet6Address;
161 }
162
163 /**
paulhu9bb04802019-03-08 16:35:20 +0800164 * For backward compatibility.
165 * This was annotated with @UnsupportedAppUsage in P, so we can't remove the method completely
166 * just yet.
167 * @return true if the address is IPv6.
168 * @hide
169 */
170 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
171 public boolean isIPv6() {
172 return isIpv6();
173 }
174
175 /**
Hugo Benichi3054e102017-08-08 13:06:04 +0900176 * @return true if the address is IPv4 or is a mapped IPv4 address.
177 * @hide
178 */
Remi NGUYEN VAN5af2e292019-01-20 12:52:43 +0900179 @SystemApi
paulhu9bb04802019-03-08 16:35:20 +0800180 public boolean isIpv4() {
Hugo Benichi3054e102017-08-08 13:06:04 +0900181 return address instanceof Inet4Address;
182 }
183
184 /**
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900185 * Utility function for the constructors.
186 */
Jack Yu1fe4b302020-01-17 15:28:50 -0800187 private void init(InetAddress address, int prefixLength, int flags, int scope,
188 long deprecationTime, long expirationTime) {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900189 if (address == null ||
190 address.isMulticastAddress() ||
191 prefixLength < 0 ||
Hugo Benichi3054e102017-08-08 13:06:04 +0900192 (address instanceof Inet4Address && prefixLength > 32) ||
Robert Greenwalt7fae12c2011-02-11 17:01:02 -0800193 (prefixLength > 128)) {
194 throw new IllegalArgumentException("Bad LinkAddress params " + address +
Lorenzo Colitti6efd3892013-08-08 19:24:09 +0900195 "/" + prefixLength);
Robert Greenwalt7fae12c2011-02-11 17:01:02 -0800196 }
Jack Yu1fe4b302020-01-17 15:28:50 -0800197
198 // deprecation time and expiration time must be both provided, or neither.
199 if ((deprecationTime == LIFETIME_UNKNOWN) != (expirationTime == LIFETIME_UNKNOWN)) {
200 throw new IllegalArgumentException(
201 "Must not specify only one of deprecation time and expiration time");
202 }
203
204 // deprecation time needs to be a positive value.
205 if (deprecationTime != LIFETIME_UNKNOWN && deprecationTime < 0) {
206 throw new IllegalArgumentException("invalid deprecation time " + deprecationTime);
207 }
208
209 // expiration time needs to be a positive value.
210 if (expirationTime != LIFETIME_UNKNOWN && expirationTime < 0) {
211 throw new IllegalArgumentException("invalid expiration time " + expirationTime);
212 }
213
214 // expiration time can't be earlier than deprecation time
215 if (deprecationTime != LIFETIME_UNKNOWN && expirationTime != LIFETIME_UNKNOWN
216 && expirationTime < deprecationTime) {
217 throw new IllegalArgumentException("expiration earlier than deprecation ("
218 + deprecationTime + ", " + expirationTime + ")");
219 }
220
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700221 this.address = address;
Irfan Sheriffe0b2c0f2010-10-05 16:12:25 -0700222 this.prefixLength = prefixLength;
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900223 this.flags = flags;
224 this.scope = scope;
Jack Yu1fe4b302020-01-17 15:28:50 -0800225 this.deprecationTime = deprecationTime;
226 this.expirationTime = expirationTime;
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900227 }
228
229 /**
230 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with
231 * the specified flags and scope. Flags and scope are not checked for validity.
Jack Yu1fe4b302020-01-17 15:28:50 -0800232 *
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900233 * @param address The IP address.
paulhu6f2c1b52019-03-27 22:26:37 +0800234 * @param prefixLength The prefix length. Must be &gt;= 0 and &lt;= (32 or 128) (IPv4 or IPv6).
Robert Greenwalt60240022014-05-18 09:39:18 -0700235 * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address.
236 * @param scope An integer defining the scope in which the address is unique (e.g.,
237 * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
238 * @hide
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900239 */
Remi NGUYEN VAN155b7b72019-01-23 21:35:52 +0900240 @SystemApi
paulhu6f2c1b52019-03-27 22:26:37 +0800241 public LinkAddress(@NonNull InetAddress address, @IntRange(from = 0, to = 128) int prefixLength,
242 int flags, int scope) {
Jack Yu1fe4b302020-01-17 15:28:50 -0800243 init(address, prefixLength, flags, scope, LIFETIME_UNKNOWN, LIFETIME_UNKNOWN);
244 }
245
246 /**
247 * Constructs a new {@code LinkAddress} from an {@code InetAddress}, prefix length, with
248 * the specified flags, scope, deprecation time, and expiration time. Flags and scope are not
249 * checked for validity. The value of the {@code IFA_F_DEPRECATED} and {@code IFA_F_PERMANENT}
250 * flag will be adjusted based on the passed-in lifetimes.
251 *
252 * @param address The IP address.
253 * @param prefixLength The prefix length. Must be &gt;= 0 and &lt;= (32 or 128) (IPv4 or IPv6).
254 * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address.
255 * @param scope An integer defining the scope in which the address is unique (e.g.,
256 * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
257 * @param deprecationTime The time, as reported by {@link SystemClock#elapsedRealtime}, when
Jack Yu7782a0d2020-01-26 15:52:11 -0800258 * this {@link LinkAddress} will be or was deprecated. At the time
259 * existing connections can still use this address until it expires, but
260 * new connections should use the new address. {@link #LIFETIME_UNKNOWN}
261 * indicates this information is not available.
Jack Yu1fe4b302020-01-17 15:28:50 -0800262 * {@link #LIFETIME_PERMANENT} indicates this {@link LinkAddress} will
263 * never be deprecated.
264 * @param expirationTime The time, as reported by {@link SystemClock#elapsedRealtime}, when this
265 * {@link LinkAddress} will expire and be removed from the interface.
266 * {@link #LIFETIME_UNKNOWN} indicates this information is not available.
267 * {@link #LIFETIME_PERMANENT} indicates this {@link LinkAddress} will
268 * never expire.
269 * @hide
270 */
271 @SystemApi
Jack Yu1fe4b302020-01-17 15:28:50 -0800272 public LinkAddress(@NonNull InetAddress address, @IntRange(from = 0, to = 128) int prefixLength,
273 int flags, int scope, long deprecationTime, long expirationTime) {
274 init(address, prefixLength, flags, scope, deprecationTime, expirationTime);
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700275 }
276
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900277 /**
278 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900279 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900280 * @param address The IP address.
paulhu6f2c1b52019-03-27 22:26:37 +0800281 * @param prefixLength The prefix length. Must be &gt;= 0 and &lt;= (32 or 128) (IPv4 or IPv6).
Robert Greenwalt60240022014-05-18 09:39:18 -0700282 * @hide
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900283 */
Jack Yu13fefdd2018-11-14 22:04:17 -0800284 @SystemApi
paulhu6f2c1b52019-03-27 22:26:37 +0800285 public LinkAddress(@NonNull InetAddress address,
286 @IntRange(from = 0, to = 128) int prefixLength) {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900287 this(address, prefixLength, 0, 0);
288 this.scope = scopeForUnicastAddress(address);
Lorenzo Colitti6efd3892013-08-08 19:24:09 +0900289 }
290
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900291 /**
292 * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900293 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900294 * @param interfaceAddress The interface address.
Robert Greenwalt60240022014-05-18 09:39:18 -0700295 * @hide
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900296 */
paulhu6f2c1b52019-03-27 22:26:37 +0800297 public LinkAddress(@NonNull InterfaceAddress interfaceAddress) {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900298 this(interfaceAddress.getAddress(),
Lorenzo Colitti6efd3892013-08-08 19:24:09 +0900299 interfaceAddress.getNetworkPrefixLength());
300 }
301
302 /**
303 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900304 * "2001:db8::1/64". The flags are set to zero and the scope is determined from the address.
Jack Yu3ee7ba12019-03-01 12:04:50 -0800305 * @param address The string to parse.
Robert Greenwalt60240022014-05-18 09:39:18 -0700306 * @hide
Lorenzo Colitti6efd3892013-08-08 19:24:09 +0900307 */
Jack Yu13fefdd2018-11-14 22:04:17 -0800308 @SystemApi
Jack Yu3ee7ba12019-03-01 12:04:50 -0800309 public LinkAddress(@NonNull String address) {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900310 this(address, 0, 0);
311 this.scope = scopeForUnicastAddress(this.address);
312 }
313
314 /**
315 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
316 * "2001:db8::1/64", with the specified flags and scope.
Jack Yu3ee7ba12019-03-01 12:04:50 -0800317 * @param address The string to parse.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900318 * @param flags The address flags.
319 * @param scope The address scope.
Robert Greenwalt60240022014-05-18 09:39:18 -0700320 * @hide
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900321 */
Remi NGUYEN VANa5225ea2019-01-29 12:08:43 +0900322 @SystemApi
paulhu9bb04802019-03-08 16:35:20 +0800323 public LinkAddress(@NonNull String address, int flags, int scope) {
Lorenzo Colitti2e9b1232014-06-12 13:41:17 +0900324 // This may throw an IllegalArgumentException; catching it is the caller's responsibility.
Hugo Benichi3054e102017-08-08 13:06:04 +0900325 // TODO: consider rejecting mapped IPv4 addresses such as "::ffff:192.0.2.5/24".
paulhucbe73812021-03-03 22:15:11 +0800326 Pair<InetAddress, Integer> ipAndMask = NetworkUtils.legacyParseIpAndMask(address);
Jack Yu1fe4b302020-01-17 15:28:50 -0800327 init(ipAndMask.first, ipAndMask.second, flags, scope, LIFETIME_UNKNOWN, LIFETIME_UNKNOWN);
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700328 }
329
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900330 /**
331 * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64".
332 * The string representation does not contain the flags and scope, just the address and prefix
333 * length.
334 */
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700335 @Override
336 public String toString() {
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900337 return address.getHostAddress() + "/" + prefixLength;
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700338 }
339
340 /**
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900341 * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900342 * their address, prefix length, flags and scope are equal. Thus, for example, two addresses
343 * that have the same address and prefix length are not equal if one of them is deprecated and
344 * the other is not.
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700345 *
346 * @param obj the object to be tested for equality.
347 * @return {@code true} if both objects are equal, {@code false} otherwise.
348 */
349 @Override
Roman Kalukiewicz1f69a5e2020-10-14 15:59:06 -0700350 public boolean equals(@Nullable Object obj) {
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700351 if (!(obj instanceof LinkAddress)) {
352 return false;
353 }
354 LinkAddress linkAddress = (LinkAddress) obj;
Jack Yu1fe4b302020-01-17 15:28:50 -0800355 return this.address.equals(linkAddress.address)
356 && this.prefixLength == linkAddress.prefixLength
357 && this.flags == linkAddress.flags
358 && this.scope == linkAddress.scope
359 && this.deprecationTime == linkAddress.deprecationTime
360 && this.expirationTime == linkAddress.expirationTime;
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700361 }
362
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900363 /**
364 * Returns a hashcode for this address.
John Wang3e567d52011-04-04 12:35:42 -0700365 */
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900366 @Override
John Wang3e567d52011-04-04 12:35:42 -0700367 public int hashCode() {
Jack Yu1fe4b302020-01-17 15:28:50 -0800368 return Objects.hash(address, prefixLength, flags, scope, deprecationTime, expirationTime);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900369 }
370
371 /**
Robert Greenwalt60240022014-05-18 09:39:18 -0700372 * Determines whether this {@code LinkAddress} and the provided {@code LinkAddress}
373 * represent the same address. Two {@code LinkAddresses} represent the same address
374 * if they have the same IP address and prefix length, even if their properties are
375 * different.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900376 *
377 * @param other the {@code LinkAddress} to compare to.
378 * @return {@code true} if both objects have the same address and prefix length, {@code false}
379 * otherwise.
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900380 * @hide
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900381 */
Remi NGUYEN VAN5af2e292019-01-20 12:52:43 +0900382 @SystemApi
paulhu9bb04802019-03-08 16:35:20 +0800383 public boolean isSameAddressAs(@Nullable LinkAddress other) {
384 if (other == null) {
385 return false;
386 }
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900387 return address.equals(other.address) && prefixLength == other.prefixLength;
John Wang3e567d52011-04-04 12:35:42 -0700388 }
389
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700390 /**
Robert Greenwalt60240022014-05-18 09:39:18 -0700391 * Returns the {@link InetAddress} of this {@code LinkAddress}.
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700392 */
393 public InetAddress getAddress() {
394 return address;
395 }
396
397 /**
Robert Greenwalt60240022014-05-18 09:39:18 -0700398 * Returns the prefix length of this {@code LinkAddress}.
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700399 */
paulhu6f2c1b52019-03-27 22:26:37 +0800400 @IntRange(from = 0, to = 128)
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900401 public int getPrefixLength() {
Irfan Sheriffe0b2c0f2010-10-05 16:12:25 -0700402 return prefixLength;
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700403 }
404
405 /**
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900406 * Returns the prefix length of this {@code LinkAddress}.
407 * TODO: Delete all callers and remove in favour of getPrefixLength().
408 * @hide
409 */
Mathew Inwood0b8f8612018-08-08 14:52:47 +0100410 @UnsupportedAppUsage
paulhu6f2c1b52019-03-27 22:26:37 +0800411 @IntRange(from = 0, to = 128)
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900412 public int getNetworkPrefixLength() {
413 return getPrefixLength();
414 }
415
416 /**
Robert Greenwalt60240022014-05-18 09:39:18 -0700417 * Returns the flags of this {@code LinkAddress}.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900418 */
419 public int getFlags() {
Jack Yu1fe4b302020-01-17 15:28:50 -0800420 int flags = this.flags;
421 if (deprecationTime != LIFETIME_UNKNOWN) {
422 if (SystemClock.elapsedRealtime() >= deprecationTime) {
423 flags |= IFA_F_DEPRECATED;
424 } else {
425 // If deprecation time is in the future, or permanent.
426 flags &= ~IFA_F_DEPRECATED;
427 }
428 }
429
430 if (expirationTime == LIFETIME_PERMANENT) {
431 flags |= IFA_F_PERMANENT;
432 } else if (expirationTime != LIFETIME_UNKNOWN) {
Jack Yu7782a0d2020-01-26 15:52:11 -0800433 // If we know this address expired or will expire in the future, then this address
Jack Yu1fe4b302020-01-17 15:28:50 -0800434 // should not be permanent.
435 flags &= ~IFA_F_PERMANENT;
436 }
437
438 // Do no touch the original flags. Return the adjusted flags here.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900439 return flags;
440 }
441
442 /**
Robert Greenwalt60240022014-05-18 09:39:18 -0700443 * Returns the scope of this {@code LinkAddress}.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900444 */
445 public int getScope() {
446 return scope;
447 }
448
449 /**
Jack Yu7782a0d2020-01-26 15:52:11 -0800450 * Get the deprecation time, as reported by {@link SystemClock#elapsedRealtime}, when this
451 * {@link LinkAddress} will be or was deprecated. At the time existing connections can still use
452 * this address until it expires, but new connections should use the new address.
453 *
454 * @return The deprecation time in milliseconds. {@link #LIFETIME_UNKNOWN} indicates this
455 * information is not available. {@link #LIFETIME_PERMANENT} indicates this {@link LinkAddress}
456 * will never be deprecated.
Jack Yu1fe4b302020-01-17 15:28:50 -0800457 *
458 * @hide
459 */
460 @SystemApi
Jack Yu1fe4b302020-01-17 15:28:50 -0800461 public long getDeprecationTime() {
462 return deprecationTime;
463 }
464
465 /**
Jack Yu7782a0d2020-01-26 15:52:11 -0800466 * Get the expiration time, as reported by {@link SystemClock#elapsedRealtime}, when this
467 * {@link LinkAddress} will expire and be removed from the interface.
468 *
469 * @return The expiration time in milliseconds. {@link #LIFETIME_UNKNOWN} indicates this
470 * information is not available. {@link #LIFETIME_PERMANENT} indicates this {@link LinkAddress}
471 * will never expire.
Jack Yu1fe4b302020-01-17 15:28:50 -0800472 *
473 * @hide
474 */
475 @SystemApi
Jack Yu1fe4b302020-01-17 15:28:50 -0800476 public long getExpirationTime() {
477 return expirationTime;
478 }
479
480 /**
481 * Returns true if this {@code LinkAddress} is global scope and preferred (i.e., not currently
482 * deprecated).
483 *
Robert Greenwalt60240022014-05-18 09:39:18 -0700484 * @hide
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900485 */
Remi NGUYEN VAN5af2e292019-01-20 12:52:43 +0900486 @SystemApi
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900487 public boolean isGlobalPreferred() {
paulhu9bb04802019-03-08 16:35:20 +0800488 return (scope == RT_SCOPE_UNIVERSE
489 && !isIpv6ULA()
Yuyang Huang700778b2023-03-08 16:17:05 +0900490 && isPreferred());
491 }
492
493 /**
494 * Checks if the address is a preferred address.
495 *
496 * @hide
497 */
498 public boolean isPreferred() {
499 // Note that addresses flagged as IFA_F_OPTIMISTIC are simultaneously flagged as
500 // IFA_F_TENTATIVE (when the tentative state has cleared either DAD has succeeded or
501 // failed, and both flags are cleared regardless).
502 int flags = getFlags();
503 return (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED)) == 0L
504 && ((flags & IFA_F_TENTATIVE) == 0L || (flags & IFA_F_OPTIMISTIC) != 0L);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900505 }
506
507 /**
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900508 * Implement the Parcelable interface.
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700509 */
510 public int describeContents() {
511 return 0;
512 }
513
514 /**
515 * Implement the Parcelable interface.
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700516 */
517 public void writeToParcel(Parcel dest, int flags) {
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900518 dest.writeByteArray(address.getAddress());
519 dest.writeInt(prefixLength);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900520 dest.writeInt(this.flags);
521 dest.writeInt(scope);
Jack Yu1fe4b302020-01-17 15:28:50 -0800522 dest.writeLong(deprecationTime);
523 dest.writeLong(expirationTime);
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700524 }
525
526 /**
527 * Implement the Parcelable interface.
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700528 */
Jeff Sharkey9286f912019-02-28 12:06:45 -0700529 public static final @android.annotation.NonNull Creator<LinkAddress> CREATOR =
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700530 new Creator<LinkAddress>() {
531 public LinkAddress createFromParcel(Parcel in) {
532 InetAddress address = null;
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900533 try {
534 address = InetAddress.getByAddress(in.createByteArray());
535 } catch (UnknownHostException e) {
536 // Nothing we can do here. When we call the constructor, we'll throw an
537 // IllegalArgumentException, because a LinkAddress can't have a null
538 // InetAddress.
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700539 }
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900540 int prefixLength = in.readInt();
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900541 int flags = in.readInt();
542 int scope = in.readInt();
Jack Yu1fe4b302020-01-17 15:28:50 -0800543 long deprecationTime = in.readLong();
544 long expirationTime = in.readLong();
545 return new LinkAddress(address, prefixLength, flags, scope, deprecationTime,
546 expirationTime);
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700547 }
548
549 public LinkAddress[] newArray(int size) {
550 return new LinkAddress[size];
551 }
552 };
553}