blob: 8d779aaa23129a7fc9e95712cd15a96d24305920 [file] [log] [blame]
Irfan Sheriffed5d7d12010-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 Benichifd31b9d2017-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;
22import static android.system.OsConstants.IFA_F_TENTATIVE;
23import static android.system.OsConstants.RT_SCOPE_HOST;
24import static android.system.OsConstants.RT_SCOPE_LINK;
25import static android.system.OsConstants.RT_SCOPE_SITE;
26import static android.system.OsConstants.RT_SCOPE_UNIVERSE;
27
Jack Yu4f956e02018-11-14 22:04:17 -080028import android.annotation.SystemApi;
Remi NGUYEN VAN31f1d0c2019-01-20 12:52:43 +090029import android.annotation.TestApi;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010030import android.annotation.UnsupportedAppUsage;
Mathew Inwood55418ea2018-12-20 15:30:45 +000031import android.os.Build;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070032import android.os.Parcel;
33import android.os.Parcelable;
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +090034import android.util.Pair;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070035
Robert Greenwaltb979f792011-02-11 17:01:02 -080036import java.net.Inet4Address;
Erik Klinebefe7782014-10-20 19:46:56 +090037import java.net.Inet6Address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070038import java.net.InetAddress;
39import java.net.InterfaceAddress;
40import java.net.UnknownHostException;
41
42/**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090043 * Identifies an IP address on a network link.
Lorenzo Colitti64483942013-11-15 18:43:52 +090044 *
45 * A {@code LinkAddress} consists of:
46 * <ul>
47 * <li>An IP address and prefix length (e.g., {@code 2001:db8::1/64} or {@code 192.0.2.1/24}).
48 * The address must be unicast, as multicast addresses cannot be assigned to interfaces.
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +090049 * <li>Address flags: A bitmask of {@code OsConstants.IFA_F_*} values representing properties
50 * of the address (e.g., {@code android.system.OsConstants.IFA_F_OPTIMISTIC}).
51 * <li>Address scope: One of the {@code OsConstants.IFA_F_*} values; defines the scope in which
52 * the address is unique (e.g.,
53 * {@code android.system.OsConstants.RT_SCOPE_LINK} or
54 * {@code android.system.OsConstants.RT_SCOPE_UNIVERSE}).
55 * </ul>
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070056 */
57public class LinkAddress implements Parcelable {
58 /**
59 * IPv4 or IPv6 address.
60 */
Mathew Inwood55418ea2018-12-20 15:30:45 +000061 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090062 private InetAddress address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070063
64 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090065 * Prefix length.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070066 */
Mathew Inwood55418ea2018-12-20 15:30:45 +000067 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090068 private int prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070069
Lorenzo Colitti64483942013-11-15 18:43:52 +090070 /**
71 * Address flags. A bitmask of IFA_F_* values.
72 */
73 private int flags;
74
75 /**
76 * Address scope. One of the RT_SCOPE_* constants.
77 */
78 private int scope;
79
80 /**
81 * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and
82 * RFC 6724 section 3.2.
83 * @hide
84 */
Hugo Benichi1dfb6b62017-08-08 13:06:04 +090085 private static int scopeForUnicastAddress(InetAddress addr) {
Lorenzo Colitti64483942013-11-15 18:43:52 +090086 if (addr.isAnyLocalAddress()) {
87 return RT_SCOPE_HOST;
88 }
89
90 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) {
91 return RT_SCOPE_LINK;
92 }
93
94 // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2
95 // says that they are assigned global scope.
96 if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) {
97 return RT_SCOPE_SITE;
98 }
99
100 return RT_SCOPE_UNIVERSE;
101 }
102
103 /**
Erik Klinebefe7782014-10-20 19:46:56 +0900104 * Utility function to check if |address| is a Unique Local IPv6 Unicast Address
105 * (a.k.a. "ULA"; RFC 4193).
106 *
107 * Per RFC 4193 section 8, fc00::/7 identifies these addresses.
108 */
109 private boolean isIPv6ULA() {
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900110 if (isIPv6()) {
Erik Klinebefe7782014-10-20 19:46:56 +0900111 byte[] bytes = address.getAddress();
Erik Kline1eb8c692016-07-08 17:21:26 +0900112 return ((bytes[0] & (byte)0xfe) == (byte)0xfc);
Erik Klinebefe7782014-10-20 19:46:56 +0900113 }
114 return false;
115 }
116
117 /**
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900118 * @return true if the address is IPv6.
119 * @hide
120 */
Remi NGUYEN VAN31f1d0c2019-01-20 12:52:43 +0900121 @TestApi
122 @SystemApi
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900123 public boolean isIPv6() {
124 return address instanceof Inet6Address;
125 }
126
127 /**
128 * @return true if the address is IPv4 or is a mapped IPv4 address.
129 * @hide
130 */
Remi NGUYEN VAN31f1d0c2019-01-20 12:52:43 +0900131 @TestApi
132 @SystemApi
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900133 public boolean isIPv4() {
134 return address instanceof Inet4Address;
135 }
136
137 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900138 * Utility function for the constructors.
139 */
140 private void init(InetAddress address, int prefixLength, int flags, int scope) {
141 if (address == null ||
142 address.isMulticastAddress() ||
143 prefixLength < 0 ||
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900144 (address instanceof Inet4Address && prefixLength > 32) ||
Robert Greenwaltb979f792011-02-11 17:01:02 -0800145 (prefixLength > 128)) {
146 throw new IllegalArgumentException("Bad LinkAddress params " + address +
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900147 "/" + prefixLength);
Robert Greenwaltb979f792011-02-11 17:01:02 -0800148 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700149 this.address = address;
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700150 this.prefixLength = prefixLength;
Lorenzo Colitti64483942013-11-15 18:43:52 +0900151 this.flags = flags;
152 this.scope = scope;
153 }
154
155 /**
156 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with
157 * the specified flags and scope. Flags and scope are not checked for validity.
158 * @param address The IP address.
159 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700160 * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address.
161 * @param scope An integer defining the scope in which the address is unique (e.g.,
162 * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
163 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900164 */
Remi NGUYEN VAN299a7cc2019-01-23 21:35:52 +0900165 @SystemApi
166 @TestApi
Lorenzo Colitti64483942013-11-15 18:43:52 +0900167 public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) {
168 init(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700169 }
170
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900171 /**
172 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900173 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900174 * @param address The IP address.
175 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700176 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900177 */
Jack Yu4f956e02018-11-14 22:04:17 -0800178 @SystemApi
Remi NGUYEN VAN5c5f1ba2019-01-29 12:08:43 +0900179 @TestApi
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900180 public LinkAddress(InetAddress address, int prefixLength) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900181 this(address, prefixLength, 0, 0);
182 this.scope = scopeForUnicastAddress(address);
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900183 }
184
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900185 /**
186 * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900187 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900188 * @param interfaceAddress The interface address.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700189 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900190 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700191 public LinkAddress(InterfaceAddress interfaceAddress) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900192 this(interfaceAddress.getAddress(),
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900193 interfaceAddress.getNetworkPrefixLength());
194 }
195
196 /**
197 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
Lorenzo Colitti64483942013-11-15 18:43:52 +0900198 * "2001:db8::1/64". The flags are set to zero and the scope is determined from the address.
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900199 * @param string The string to parse.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700200 * @hide
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900201 */
Jack Yu4f956e02018-11-14 22:04:17 -0800202 @SystemApi
Remi NGUYEN VAN5c5f1ba2019-01-29 12:08:43 +0900203 @TestApi
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900204 public LinkAddress(String address) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900205 this(address, 0, 0);
206 this.scope = scopeForUnicastAddress(this.address);
207 }
208
209 /**
210 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
211 * "2001:db8::1/64", with the specified flags and scope.
212 * @param string The string to parse.
213 * @param flags The address flags.
214 * @param scope The address scope.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700215 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900216 */
Remi NGUYEN VAN5c5f1ba2019-01-29 12:08:43 +0900217 @SystemApi
218 @TestApi
Lorenzo Colitti64483942013-11-15 18:43:52 +0900219 public LinkAddress(String address, int flags, int scope) {
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900220 // This may throw an IllegalArgumentException; catching it is the caller's responsibility.
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900221 // TODO: consider rejecting mapped IPv4 addresses such as "::ffff:192.0.2.5/24".
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900222 Pair<InetAddress, Integer> ipAndMask = NetworkUtils.parseIpAndMask(address);
223 init(ipAndMask.first, ipAndMask.second, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700224 }
225
Lorenzo Colitti64483942013-11-15 18:43:52 +0900226 /**
227 * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64".
228 * The string representation does not contain the flags and scope, just the address and prefix
229 * length.
230 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700231 @Override
232 public String toString() {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900233 return address.getHostAddress() + "/" + prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700234 }
235
236 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900237 * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900238 * their address, prefix length, flags and scope are equal. Thus, for example, two addresses
239 * that have the same address and prefix length are not equal if one of them is deprecated and
240 * the other is not.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700241 *
242 * @param obj the object to be tested for equality.
243 * @return {@code true} if both objects are equal, {@code false} otherwise.
244 */
245 @Override
246 public boolean equals(Object obj) {
247 if (!(obj instanceof LinkAddress)) {
248 return false;
249 }
250 LinkAddress linkAddress = (LinkAddress) obj;
251 return this.address.equals(linkAddress.address) &&
Lorenzo Colitti64483942013-11-15 18:43:52 +0900252 this.prefixLength == linkAddress.prefixLength &&
253 this.flags == linkAddress.flags &&
254 this.scope == linkAddress.scope;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700255 }
256
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900257 /**
258 * Returns a hashcode for this address.
John Wang4e900092011-04-04 12:35:42 -0700259 */
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900260 @Override
John Wang4e900092011-04-04 12:35:42 -0700261 public int hashCode() {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900262 return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope;
263 }
264
265 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700266 * Determines whether this {@code LinkAddress} and the provided {@code LinkAddress}
267 * represent the same address. Two {@code LinkAddresses} represent the same address
268 * if they have the same IP address and prefix length, even if their properties are
269 * different.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900270 *
271 * @param other the {@code LinkAddress} to compare to.
272 * @return {@code true} if both objects have the same address and prefix length, {@code false}
273 * otherwise.
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900274 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900275 */
Remi NGUYEN VAN31f1d0c2019-01-20 12:52:43 +0900276 @TestApi
277 @SystemApi
Lorenzo Colitti64483942013-11-15 18:43:52 +0900278 public boolean isSameAddressAs(LinkAddress other) {
279 return address.equals(other.address) && prefixLength == other.prefixLength;
John Wang4e900092011-04-04 12:35:42 -0700280 }
281
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700282 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700283 * Returns the {@link InetAddress} of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700284 */
285 public InetAddress getAddress() {
286 return address;
287 }
288
289 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700290 * Returns the prefix length of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700291 */
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900292 public int getPrefixLength() {
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700293 return prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700294 }
295
296 /**
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900297 * Returns the prefix length of this {@code LinkAddress}.
298 * TODO: Delete all callers and remove in favour of getPrefixLength().
299 * @hide
300 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100301 @UnsupportedAppUsage
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900302 public int getNetworkPrefixLength() {
303 return getPrefixLength();
304 }
305
306 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700307 * Returns the flags of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900308 */
309 public int getFlags() {
310 return flags;
311 }
312
313 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700314 * Returns the scope of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900315 */
316 public int getScope() {
317 return scope;
318 }
319
320 /**
321 * Returns true if this {@code LinkAddress} is global scope and preferred.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700322 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900323 */
Remi NGUYEN VAN31f1d0c2019-01-20 12:52:43 +0900324 @TestApi
325 @SystemApi
Lorenzo Colitti64483942013-11-15 18:43:52 +0900326 public boolean isGlobalPreferred() {
Erik Klinebefe7782014-10-20 19:46:56 +0900327 /**
328 * Note that addresses flagged as IFA_F_OPTIMISTIC are
329 * simultaneously flagged as IFA_F_TENTATIVE (when the tentative
330 * state has cleared either DAD has succeeded or failed, and both
331 * flags are cleared regardless).
332 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900333 return (scope == RT_SCOPE_UNIVERSE &&
Erik Klinebefe7782014-10-20 19:46:56 +0900334 !isIPv6ULA() &&
335 (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED)) == 0L &&
336 ((flags & IFA_F_TENTATIVE) == 0L || (flags & IFA_F_OPTIMISTIC) != 0L));
Lorenzo Colitti64483942013-11-15 18:43:52 +0900337 }
338
339 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900340 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700341 */
342 public int describeContents() {
343 return 0;
344 }
345
346 /**
347 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700348 */
349 public void writeToParcel(Parcel dest, int flags) {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900350 dest.writeByteArray(address.getAddress());
351 dest.writeInt(prefixLength);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900352 dest.writeInt(this.flags);
353 dest.writeInt(scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700354 }
355
356 /**
357 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700358 */
359 public static final Creator<LinkAddress> CREATOR =
360 new Creator<LinkAddress>() {
361 public LinkAddress createFromParcel(Parcel in) {
362 InetAddress address = null;
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900363 try {
364 address = InetAddress.getByAddress(in.createByteArray());
365 } catch (UnknownHostException e) {
366 // Nothing we can do here. When we call the constructor, we'll throw an
367 // IllegalArgumentException, because a LinkAddress can't have a null
368 // InetAddress.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700369 }
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900370 int prefixLength = in.readInt();
Lorenzo Colitti64483942013-11-15 18:43:52 +0900371 int flags = in.readInt();
372 int scope = in.readInt();
373 return new LinkAddress(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700374 }
375
376 public LinkAddress[] newArray(int size) {
377 return new LinkAddress[size];
378 }
379 };
380}