blob: d48b8c71f495f7985f10d4f17b2e2d1d04599642 [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
Robert Greenwalt7fae12c2011-02-11 17:01:02 -080040import java.net.Inet4Address;
Erik Klinec17b5282014-10-20 19:46:56 +090041import java.net.Inet6Address;
Irfan Sherifffb7c9642010-10-01 16:08:28 -070042import java.net.InetAddress;
43import java.net.InterfaceAddress;
44import java.net.UnknownHostException;
Jack Yu1fe4b302020-01-17 15:28:50 -080045import java.util.Objects;
Irfan Sherifffb7c9642010-10-01 16:08:28 -070046
47/**
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +090048 * Identifies an IP address on a network link.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +090049 *
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 Colitti48a7da02014-06-09 22:58:46 +090054 * <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 Sherifffb7c9642010-10-01 16:08:28 -070061 */
62public class LinkAddress implements Parcelable {
Jack Yu1fe4b302020-01-17 15:28:50 -080063
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 Sherifffb7c9642010-10-01 16:08:28 -070078 /**
79 * IPv4 or IPv6 address.
80 */
Mathew Inwoodac5968e2018-12-20 15:30:45 +000081 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Lorenzo Colitti6efd3892013-08-08 19:24:09 +090082 private InetAddress address;
Irfan Sherifffb7c9642010-10-01 16:08:28 -070083
84 /**
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +090085 * Prefix length.
Irfan Sherifffb7c9642010-10-01 16:08:28 -070086 */
Mathew Inwoodac5968e2018-12-20 15:30:45 +000087 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Lorenzo Colitti6efd3892013-08-08 19:24:09 +090088 private int prefixLength;
Irfan Sherifffb7c9642010-10-01 16:08:28 -070089
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +090090 /**
Jack Yu1fe4b302020-01-17 15:28:50 -080091 * 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 Colitti4ea70b72013-11-15 18:43:52 +090094 */
95 private int flags;
96
97 /**
98 * Address scope. One of the RT_SCOPE_* constants.
99 */
100 private int scope;
101
102 /**
Jack Yu1fe4b302020-01-17 15:28:50 -0800103 * The time, as reported by {@link SystemClock#elapsedRealtime}, when this LinkAddress will be
Jack Yu7782a0d2020-01-26 15:52:11 -0800104 * 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 Yu1fe4b302020-01-17 15:28:50 -0800107 * {@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 Colitti4ea70b72013-11-15 18:43:52 +0900120 * 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 Benichi3054e102017-08-08 13:06:04 +0900124 private static int scopeForUnicastAddress(InetAddress addr) {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900125 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 Klinec17b5282014-10-20 19:46:56 +0900143 * 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 */
paulhu9bb04802019-03-08 16:35:20 +0800148 private boolean isIpv6ULA() {
149 if (isIpv6()) {
Erik Klinec17b5282014-10-20 19:46:56 +0900150 byte[] bytes = address.getAddress();
Erik Kline4ab3a3a2016-07-08 17:21:26 +0900151 return ((bytes[0] & (byte)0xfe) == (byte)0xfc);
Erik Klinec17b5282014-10-20 19:46:56 +0900152 }
153 return false;
154 }
155
156 /**
Hugo Benichi3054e102017-08-08 13:06:04 +0900157 * @return true if the address is IPv6.
158 * @hide
159 */
Remi NGUYEN VAN5af2e292019-01-20 12:52:43 +0900160 @SystemApi
paulhu9bb04802019-03-08 16:35:20 +0800161 public boolean isIpv6() {
Hugo Benichi3054e102017-08-08 13:06:04 +0900162 return address instanceof Inet6Address;
163 }
164
165 /**
paulhu9bb04802019-03-08 16:35:20 +0800166 * 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 Benichi3054e102017-08-08 13:06:04 +0900178 * @return true if the address is IPv4 or is a mapped IPv4 address.
179 * @hide
180 */
Remi NGUYEN VAN5af2e292019-01-20 12:52:43 +0900181 @SystemApi
paulhu9bb04802019-03-08 16:35:20 +0800182 public boolean isIpv4() {
Hugo Benichi3054e102017-08-08 13:06:04 +0900183 return address instanceof Inet4Address;
184 }
185
186 /**
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900187 * Utility function for the constructors.
188 */
Jack Yu1fe4b302020-01-17 15:28:50 -0800189 private void init(InetAddress address, int prefixLength, int flags, int scope,
190 long deprecationTime, long expirationTime) {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900191 if (address == null ||
192 address.isMulticastAddress() ||
193 prefixLength < 0 ||
Hugo Benichi3054e102017-08-08 13:06:04 +0900194 (address instanceof Inet4Address && prefixLength > 32) ||
Robert Greenwalt7fae12c2011-02-11 17:01:02 -0800195 (prefixLength > 128)) {
196 throw new IllegalArgumentException("Bad LinkAddress params " + address +
Lorenzo Colitti6efd3892013-08-08 19:24:09 +0900197 "/" + prefixLength);
Robert Greenwalt7fae12c2011-02-11 17:01:02 -0800198 }
Jack Yu1fe4b302020-01-17 15:28:50 -0800199
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 Sherifffb7c9642010-10-01 16:08:28 -0700223 this.address = address;
Irfan Sheriffe0b2c0f2010-10-05 16:12:25 -0700224 this.prefixLength = prefixLength;
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900225 this.flags = flags;
226 this.scope = scope;
Jack Yu1fe4b302020-01-17 15:28:50 -0800227 this.deprecationTime = deprecationTime;
228 this.expirationTime = expirationTime;
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900229 }
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 Yu1fe4b302020-01-17 15:28:50 -0800234 *
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900235 * @param address The IP address.
paulhu6f2c1b52019-03-27 22:26:37 +0800236 * @param prefixLength The prefix length. Must be &gt;= 0 and &lt;= (32 or 128) (IPv4 or IPv6).
Robert Greenwalt60240022014-05-18 09:39:18 -0700237 * @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 Colitti4ea70b72013-11-15 18:43:52 +0900241 */
Remi NGUYEN VAN155b7b72019-01-23 21:35:52 +0900242 @SystemApi
paulhu6f2c1b52019-03-27 22:26:37 +0800243 public LinkAddress(@NonNull InetAddress address, @IntRange(from = 0, to = 128) int prefixLength,
244 int flags, int scope) {
Jack Yu1fe4b302020-01-17 15:28:50 -0800245 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 &gt;= 0 and &lt;= (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 Yu7782a0d2020-01-26 15:52:11 -0800260 * 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 Yu1fe4b302020-01-17 15:28:50 -0800264 * {@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 Yu1fe4b302020-01-17 15:28:50 -0800274 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 Sherifffb7c9642010-10-01 16:08:28 -0700277 }
278
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900279 /**
280 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900281 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900282 * @param address The IP address.
paulhu6f2c1b52019-03-27 22:26:37 +0800283 * @param prefixLength The prefix length. Must be &gt;= 0 and &lt;= (32 or 128) (IPv4 or IPv6).
Robert Greenwalt60240022014-05-18 09:39:18 -0700284 * @hide
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900285 */
Jack Yu13fefdd2018-11-14 22:04:17 -0800286 @SystemApi
paulhu6f2c1b52019-03-27 22:26:37 +0800287 public LinkAddress(@NonNull InetAddress address,
288 @IntRange(from = 0, to = 128) int prefixLength) {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900289 this(address, prefixLength, 0, 0);
290 this.scope = scopeForUnicastAddress(address);
Lorenzo Colitti6efd3892013-08-08 19:24:09 +0900291 }
292
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900293 /**
294 * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900295 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900296 * @param interfaceAddress The interface address.
Robert Greenwalt60240022014-05-18 09:39:18 -0700297 * @hide
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900298 */
paulhu6f2c1b52019-03-27 22:26:37 +0800299 public LinkAddress(@NonNull InterfaceAddress interfaceAddress) {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900300 this(interfaceAddress.getAddress(),
Lorenzo Colitti6efd3892013-08-08 19:24:09 +0900301 interfaceAddress.getNetworkPrefixLength());
302 }
303
304 /**
305 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900306 * "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 -0800307 * @param address The string to parse.
Robert Greenwalt60240022014-05-18 09:39:18 -0700308 * @hide
Lorenzo Colitti6efd3892013-08-08 19:24:09 +0900309 */
Jack Yu13fefdd2018-11-14 22:04:17 -0800310 @SystemApi
Jack Yu3ee7ba12019-03-01 12:04:50 -0800311 public LinkAddress(@NonNull String address) {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900312 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 Yu3ee7ba12019-03-01 12:04:50 -0800319 * @param address The string to parse.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900320 * @param flags The address flags.
321 * @param scope The address scope.
Robert Greenwalt60240022014-05-18 09:39:18 -0700322 * @hide
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900323 */
Remi NGUYEN VANa5225ea2019-01-29 12:08:43 +0900324 @SystemApi
paulhu9bb04802019-03-08 16:35:20 +0800325 public LinkAddress(@NonNull String address, int flags, int scope) {
Lorenzo Colitti2e9b1232014-06-12 13:41:17 +0900326 // This may throw an IllegalArgumentException; catching it is the caller's responsibility.
Hugo Benichi3054e102017-08-08 13:06:04 +0900327 // TODO: consider rejecting mapped IPv4 addresses such as "::ffff:192.0.2.5/24".
paulhucbe73812021-03-03 22:15:11 +0800328 Pair<InetAddress, Integer> ipAndMask = NetworkUtils.legacyParseIpAndMask(address);
Jack Yu1fe4b302020-01-17 15:28:50 -0800329 init(ipAndMask.first, ipAndMask.second, flags, scope, LIFETIME_UNKNOWN, LIFETIME_UNKNOWN);
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700330 }
331
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900332 /**
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 Sherifffb7c9642010-10-01 16:08:28 -0700337 @Override
338 public String toString() {
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900339 return address.getHostAddress() + "/" + prefixLength;
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700340 }
341
342 /**
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900343 * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900344 * 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 Sherifffb7c9642010-10-01 16:08:28 -0700347 *
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 Kalukiewicz1f69a5e2020-10-14 15:59:06 -0700352 public boolean equals(@Nullable Object obj) {
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700353 if (!(obj instanceof LinkAddress)) {
354 return false;
355 }
356 LinkAddress linkAddress = (LinkAddress) obj;
Jack Yu1fe4b302020-01-17 15:28:50 -0800357 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 Sherifffb7c9642010-10-01 16:08:28 -0700363 }
364
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900365 /**
366 * Returns a hashcode for this address.
John Wang3e567d52011-04-04 12:35:42 -0700367 */
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900368 @Override
John Wang3e567d52011-04-04 12:35:42 -0700369 public int hashCode() {
Jack Yu1fe4b302020-01-17 15:28:50 -0800370 return Objects.hash(address, prefixLength, flags, scope, deprecationTime, expirationTime);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900371 }
372
373 /**
Robert Greenwalt60240022014-05-18 09:39:18 -0700374 * 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 Colitti4ea70b72013-11-15 18:43:52 +0900378 *
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 Colitti48a7da02014-06-09 22:58:46 +0900382 * @hide
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900383 */
Remi NGUYEN VAN5af2e292019-01-20 12:52:43 +0900384 @SystemApi
paulhu9bb04802019-03-08 16:35:20 +0800385 public boolean isSameAddressAs(@Nullable LinkAddress other) {
386 if (other == null) {
387 return false;
388 }
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900389 return address.equals(other.address) && prefixLength == other.prefixLength;
John Wang3e567d52011-04-04 12:35:42 -0700390 }
391
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700392 /**
Robert Greenwalt60240022014-05-18 09:39:18 -0700393 * Returns the {@link InetAddress} of this {@code LinkAddress}.
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700394 */
395 public InetAddress getAddress() {
396 return address;
397 }
398
399 /**
Robert Greenwalt60240022014-05-18 09:39:18 -0700400 * Returns the prefix length of this {@code LinkAddress}.
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700401 */
paulhu6f2c1b52019-03-27 22:26:37 +0800402 @IntRange(from = 0, to = 128)
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900403 public int getPrefixLength() {
Irfan Sheriffe0b2c0f2010-10-05 16:12:25 -0700404 return prefixLength;
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700405 }
406
407 /**
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900408 * Returns the prefix length of this {@code LinkAddress}.
409 * TODO: Delete all callers and remove in favour of getPrefixLength().
410 * @hide
411 */
Mathew Inwood0b8f8612018-08-08 14:52:47 +0100412 @UnsupportedAppUsage
paulhu6f2c1b52019-03-27 22:26:37 +0800413 @IntRange(from = 0, to = 128)
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900414 public int getNetworkPrefixLength() {
415 return getPrefixLength();
416 }
417
418 /**
Robert Greenwalt60240022014-05-18 09:39:18 -0700419 * Returns the flags of this {@code LinkAddress}.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900420 */
421 public int getFlags() {
Jack Yu1fe4b302020-01-17 15:28:50 -0800422 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 Yu7782a0d2020-01-26 15:52:11 -0800435 // If we know this address expired or will expire in the future, then this address
Jack Yu1fe4b302020-01-17 15:28:50 -0800436 // should not be permanent.
437 flags &= ~IFA_F_PERMANENT;
438 }
439
440 // Do no touch the original flags. Return the adjusted flags here.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900441 return flags;
442 }
443
444 /**
Robert Greenwalt60240022014-05-18 09:39:18 -0700445 * Returns the scope of this {@code LinkAddress}.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900446 */
447 public int getScope() {
448 return scope;
449 }
450
451 /**
Jack Yu7782a0d2020-01-26 15:52:11 -0800452 * 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 Yu1fe4b302020-01-17 15:28:50 -0800459 *
460 * @hide
461 */
462 @SystemApi
Jack Yu1fe4b302020-01-17 15:28:50 -0800463 public long getDeprecationTime() {
464 return deprecationTime;
465 }
466
467 /**
Jack Yu7782a0d2020-01-26 15:52:11 -0800468 * 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 Yu1fe4b302020-01-17 15:28:50 -0800474 *
475 * @hide
476 */
477 @SystemApi
Jack Yu1fe4b302020-01-17 15:28:50 -0800478 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 Greenwalt60240022014-05-18 09:39:18 -0700486 * @hide
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900487 */
Remi NGUYEN VAN5af2e292019-01-20 12:52:43 +0900488 @SystemApi
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900489 public boolean isGlobalPreferred() {
Erik Klinec17b5282014-10-20 19:46:56 +0900490 /**
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 Yu1fe4b302020-01-17 15:28:50 -0800496 int flags = getFlags();
paulhu9bb04802019-03-08 16:35:20 +0800497 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 Colitti4ea70b72013-11-15 18:43:52 +0900501 }
502
503 /**
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900504 * Implement the Parcelable interface.
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700505 */
506 public int describeContents() {
507 return 0;
508 }
509
510 /**
511 * Implement the Parcelable interface.
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700512 */
513 public void writeToParcel(Parcel dest, int flags) {
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900514 dest.writeByteArray(address.getAddress());
515 dest.writeInt(prefixLength);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900516 dest.writeInt(this.flags);
517 dest.writeInt(scope);
Jack Yu1fe4b302020-01-17 15:28:50 -0800518 dest.writeLong(deprecationTime);
519 dest.writeLong(expirationTime);
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700520 }
521
522 /**
523 * Implement the Parcelable interface.
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700524 */
Jeff Sharkey9286f912019-02-28 12:06:45 -0700525 public static final @android.annotation.NonNull Creator<LinkAddress> CREATOR =
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700526 new Creator<LinkAddress>() {
527 public LinkAddress createFromParcel(Parcel in) {
528 InetAddress address = null;
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900529 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 Sherifffb7c9642010-10-01 16:08:28 -0700535 }
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900536 int prefixLength = in.readInt();
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900537 int flags = in.readInt();
538 int scope = in.readInt();
Jack Yu1fe4b302020-01-17 15:28:50 -0800539 long deprecationTime = in.readLong();
540 long expirationTime = in.readLong();
541 return new LinkAddress(address, prefixLength, flags, scope, deprecationTime,
542 expirationTime);
Irfan Sherifffb7c9642010-10-01 16:08:28 -0700543 }
544
545 public LinkAddress[] newArray(int size) {
546 return new LinkAddress[size];
547 }
548 };
549}