blob: 78a4e7293947958abce41cbafd6b1c10290957fb [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 Yu8eb6ca82019-03-01 12:04:50 -080028import android.annotation.NonNull;
Jack Yu4f956e02018-11-14 22:04:17 -080029import android.annotation.SystemApi;
Remi NGUYEN VAN31f1d0c2019-01-20 12:52:43 +090030import android.annotation.TestApi;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010031import android.annotation.UnsupportedAppUsage;
Mathew Inwood55418ea2018-12-20 15:30:45 +000032import android.os.Build;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070033import android.os.Parcel;
34import android.os.Parcelable;
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +090035import android.util.Pair;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070036
Robert Greenwaltb979f792011-02-11 17:01:02 -080037import java.net.Inet4Address;
Erik Klinebefe7782014-10-20 19:46:56 +090038import java.net.Inet6Address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070039import java.net.InetAddress;
40import java.net.InterfaceAddress;
41import java.net.UnknownHostException;
42
43/**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090044 * Identifies an IP address on a network link.
Lorenzo Colitti64483942013-11-15 18:43:52 +090045 *
46 * A {@code LinkAddress} consists of:
47 * <ul>
48 * <li>An IP address and prefix length (e.g., {@code 2001:db8::1/64} or {@code 192.0.2.1/24}).
49 * The address must be unicast, as multicast addresses cannot be assigned to interfaces.
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +090050 * <li>Address flags: A bitmask of {@code OsConstants.IFA_F_*} values representing properties
51 * of the address (e.g., {@code android.system.OsConstants.IFA_F_OPTIMISTIC}).
52 * <li>Address scope: One of the {@code OsConstants.IFA_F_*} values; defines the scope in which
53 * the address is unique (e.g.,
54 * {@code android.system.OsConstants.RT_SCOPE_LINK} or
55 * {@code android.system.OsConstants.RT_SCOPE_UNIVERSE}).
56 * </ul>
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070057 */
58public class LinkAddress implements Parcelable {
59 /**
60 * IPv4 or IPv6 address.
61 */
Mathew Inwood55418ea2018-12-20 15:30:45 +000062 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090063 private InetAddress address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070064
65 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090066 * Prefix length.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070067 */
Mathew Inwood55418ea2018-12-20 15:30:45 +000068 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090069 private int prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070070
Lorenzo Colitti64483942013-11-15 18:43:52 +090071 /**
72 * Address flags. A bitmask of IFA_F_* values.
73 */
74 private int flags;
75
76 /**
77 * Address scope. One of the RT_SCOPE_* constants.
78 */
79 private int scope;
80
81 /**
82 * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and
83 * RFC 6724 section 3.2.
84 * @hide
85 */
Hugo Benichi1dfb6b62017-08-08 13:06:04 +090086 private static int scopeForUnicastAddress(InetAddress addr) {
Lorenzo Colitti64483942013-11-15 18:43:52 +090087 if (addr.isAnyLocalAddress()) {
88 return RT_SCOPE_HOST;
89 }
90
91 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) {
92 return RT_SCOPE_LINK;
93 }
94
95 // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2
96 // says that they are assigned global scope.
97 if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) {
98 return RT_SCOPE_SITE;
99 }
100
101 return RT_SCOPE_UNIVERSE;
102 }
103
104 /**
Erik Klinebefe7782014-10-20 19:46:56 +0900105 * Utility function to check if |address| is a Unique Local IPv6 Unicast Address
106 * (a.k.a. "ULA"; RFC 4193).
107 *
108 * Per RFC 4193 section 8, fc00::/7 identifies these addresses.
109 */
110 private boolean isIPv6ULA() {
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900111 if (isIPv6()) {
Erik Klinebefe7782014-10-20 19:46:56 +0900112 byte[] bytes = address.getAddress();
Erik Kline1eb8c692016-07-08 17:21:26 +0900113 return ((bytes[0] & (byte)0xfe) == (byte)0xfc);
Erik Klinebefe7782014-10-20 19:46:56 +0900114 }
115 return false;
116 }
117
118 /**
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900119 * @return true if the address is IPv6.
120 * @hide
121 */
Remi NGUYEN VAN31f1d0c2019-01-20 12:52:43 +0900122 @TestApi
123 @SystemApi
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900124 public boolean isIPv6() {
125 return address instanceof Inet6Address;
126 }
127
128 /**
129 * @return true if the address is IPv4 or is a mapped IPv4 address.
130 * @hide
131 */
Remi NGUYEN VAN31f1d0c2019-01-20 12:52:43 +0900132 @TestApi
133 @SystemApi
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900134 public boolean isIPv4() {
135 return address instanceof Inet4Address;
136 }
137
138 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900139 * Utility function for the constructors.
140 */
141 private void init(InetAddress address, int prefixLength, int flags, int scope) {
142 if (address == null ||
143 address.isMulticastAddress() ||
144 prefixLength < 0 ||
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900145 (address instanceof Inet4Address && prefixLength > 32) ||
Robert Greenwaltb979f792011-02-11 17:01:02 -0800146 (prefixLength > 128)) {
147 throw new IllegalArgumentException("Bad LinkAddress params " + address +
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900148 "/" + prefixLength);
Robert Greenwaltb979f792011-02-11 17:01:02 -0800149 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700150 this.address = address;
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700151 this.prefixLength = prefixLength;
Lorenzo Colitti64483942013-11-15 18:43:52 +0900152 this.flags = flags;
153 this.scope = scope;
154 }
155
156 /**
157 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with
158 * the specified flags and scope. Flags and scope are not checked for validity.
159 * @param address The IP address.
160 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700161 * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address.
162 * @param scope An integer defining the scope in which the address is unique (e.g.,
163 * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
164 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900165 */
Remi NGUYEN VAN299a7cc2019-01-23 21:35:52 +0900166 @SystemApi
167 @TestApi
Lorenzo Colitti64483942013-11-15 18:43:52 +0900168 public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) {
169 init(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700170 }
171
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900172 /**
173 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900174 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900175 * @param address The IP address.
176 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700177 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900178 */
Jack Yu4f956e02018-11-14 22:04:17 -0800179 @SystemApi
Remi NGUYEN VAN5c5f1ba2019-01-29 12:08:43 +0900180 @TestApi
Jack Yu8eb6ca82019-03-01 12:04:50 -0800181 public LinkAddress(@NonNull InetAddress address, int prefixLength) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900182 this(address, prefixLength, 0, 0);
183 this.scope = scopeForUnicastAddress(address);
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900184 }
185
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900186 /**
187 * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900188 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900189 * @param interfaceAddress The interface address.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700190 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900191 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700192 public LinkAddress(InterfaceAddress interfaceAddress) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900193 this(interfaceAddress.getAddress(),
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900194 interfaceAddress.getNetworkPrefixLength());
195 }
196
197 /**
198 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
Lorenzo Colitti64483942013-11-15 18:43:52 +0900199 * "2001:db8::1/64". The flags are set to zero and the scope is determined from the address.
Jack Yu8eb6ca82019-03-01 12:04:50 -0800200 * @param address The string to parse.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700201 * @hide
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900202 */
Jack Yu4f956e02018-11-14 22:04:17 -0800203 @SystemApi
Remi NGUYEN VAN5c5f1ba2019-01-29 12:08:43 +0900204 @TestApi
Jack Yu8eb6ca82019-03-01 12:04:50 -0800205 public LinkAddress(@NonNull String address) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900206 this(address, 0, 0);
207 this.scope = scopeForUnicastAddress(this.address);
208 }
209
210 /**
211 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
212 * "2001:db8::1/64", with the specified flags and scope.
Jack Yu8eb6ca82019-03-01 12:04:50 -0800213 * @param address The string to parse.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900214 * @param flags The address flags.
215 * @param scope The address scope.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700216 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900217 */
Remi NGUYEN VAN5c5f1ba2019-01-29 12:08:43 +0900218 @SystemApi
219 @TestApi
Lorenzo Colitti64483942013-11-15 18:43:52 +0900220 public LinkAddress(String address, int flags, int scope) {
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900221 // This may throw an IllegalArgumentException; catching it is the caller's responsibility.
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900222 // TODO: consider rejecting mapped IPv4 addresses such as "::ffff:192.0.2.5/24".
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900223 Pair<InetAddress, Integer> ipAndMask = NetworkUtils.parseIpAndMask(address);
224 init(ipAndMask.first, ipAndMask.second, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700225 }
226
Lorenzo Colitti64483942013-11-15 18:43:52 +0900227 /**
228 * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64".
229 * The string representation does not contain the flags and scope, just the address and prefix
230 * length.
231 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700232 @Override
233 public String toString() {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900234 return address.getHostAddress() + "/" + prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700235 }
236
237 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900238 * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900239 * their address, prefix length, flags and scope are equal. Thus, for example, two addresses
240 * that have the same address and prefix length are not equal if one of them is deprecated and
241 * the other is not.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700242 *
243 * @param obj the object to be tested for equality.
244 * @return {@code true} if both objects are equal, {@code false} otherwise.
245 */
246 @Override
247 public boolean equals(Object obj) {
248 if (!(obj instanceof LinkAddress)) {
249 return false;
250 }
251 LinkAddress linkAddress = (LinkAddress) obj;
252 return this.address.equals(linkAddress.address) &&
Lorenzo Colitti64483942013-11-15 18:43:52 +0900253 this.prefixLength == linkAddress.prefixLength &&
254 this.flags == linkAddress.flags &&
255 this.scope == linkAddress.scope;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700256 }
257
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900258 /**
259 * Returns a hashcode for this address.
John Wang4e900092011-04-04 12:35:42 -0700260 */
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900261 @Override
John Wang4e900092011-04-04 12:35:42 -0700262 public int hashCode() {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900263 return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope;
264 }
265
266 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700267 * Determines whether this {@code LinkAddress} and the provided {@code LinkAddress}
268 * represent the same address. Two {@code LinkAddresses} represent the same address
269 * if they have the same IP address and prefix length, even if their properties are
270 * different.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900271 *
272 * @param other the {@code LinkAddress} to compare to.
273 * @return {@code true} if both objects have the same address and prefix length, {@code false}
274 * otherwise.
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900275 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900276 */
Remi NGUYEN VAN31f1d0c2019-01-20 12:52:43 +0900277 @TestApi
278 @SystemApi
Lorenzo Colitti64483942013-11-15 18:43:52 +0900279 public boolean isSameAddressAs(LinkAddress other) {
280 return address.equals(other.address) && prefixLength == other.prefixLength;
John Wang4e900092011-04-04 12:35:42 -0700281 }
282
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700283 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700284 * Returns the {@link InetAddress} of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700285 */
286 public InetAddress getAddress() {
287 return address;
288 }
289
290 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700291 * Returns the prefix length of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700292 */
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900293 public int getPrefixLength() {
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700294 return prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700295 }
296
297 /**
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900298 * Returns the prefix length of this {@code LinkAddress}.
299 * TODO: Delete all callers and remove in favour of getPrefixLength().
300 * @hide
301 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100302 @UnsupportedAppUsage
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900303 public int getNetworkPrefixLength() {
304 return getPrefixLength();
305 }
306
307 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700308 * Returns the flags of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900309 */
310 public int getFlags() {
311 return flags;
312 }
313
314 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700315 * Returns the scope of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900316 */
317 public int getScope() {
318 return scope;
319 }
320
321 /**
322 * Returns true if this {@code LinkAddress} is global scope and preferred.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700323 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900324 */
Remi NGUYEN VAN31f1d0c2019-01-20 12:52:43 +0900325 @TestApi
326 @SystemApi
Lorenzo Colitti64483942013-11-15 18:43:52 +0900327 public boolean isGlobalPreferred() {
Erik Klinebefe7782014-10-20 19:46:56 +0900328 /**
329 * Note that addresses flagged as IFA_F_OPTIMISTIC are
330 * simultaneously flagged as IFA_F_TENTATIVE (when the tentative
331 * state has cleared either DAD has succeeded or failed, and both
332 * flags are cleared regardless).
333 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900334 return (scope == RT_SCOPE_UNIVERSE &&
Erik Klinebefe7782014-10-20 19:46:56 +0900335 !isIPv6ULA() &&
336 (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED)) == 0L &&
337 ((flags & IFA_F_TENTATIVE) == 0L || (flags & IFA_F_OPTIMISTIC) != 0L));
Lorenzo Colitti64483942013-11-15 18:43:52 +0900338 }
339
340 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900341 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700342 */
343 public int describeContents() {
344 return 0;
345 }
346
347 /**
348 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700349 */
350 public void writeToParcel(Parcel dest, int flags) {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900351 dest.writeByteArray(address.getAddress());
352 dest.writeInt(prefixLength);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900353 dest.writeInt(this.flags);
354 dest.writeInt(scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700355 }
356
357 /**
358 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700359 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700360 public static final @android.annotation.NonNull Creator<LinkAddress> CREATOR =
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700361 new Creator<LinkAddress>() {
362 public LinkAddress createFromParcel(Parcel in) {
363 InetAddress address = null;
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900364 try {
365 address = InetAddress.getByAddress(in.createByteArray());
366 } catch (UnknownHostException e) {
367 // Nothing we can do here. When we call the constructor, we'll throw an
368 // IllegalArgumentException, because a LinkAddress can't have a null
369 // InetAddress.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700370 }
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900371 int prefixLength = in.readInt();
Lorenzo Colitti64483942013-11-15 18:43:52 +0900372 int flags = in.readInt();
373 int scope = in.readInt();
374 return new LinkAddress(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700375 }
376
377 public LinkAddress[] newArray(int size) {
378 return new LinkAddress[size];
379 }
380 };
381}