blob: 77562dbd7bfa51279f169c6c4987cc5dc0361d9d [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;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010029import android.annotation.UnsupportedAppUsage;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070030import android.os.Parcel;
31import android.os.Parcelable;
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +090032import android.util.Pair;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070033
Robert Greenwaltb979f792011-02-11 17:01:02 -080034import java.net.Inet4Address;
Erik Klinebefe7782014-10-20 19:46:56 +090035import java.net.Inet6Address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070036import java.net.InetAddress;
37import java.net.InterfaceAddress;
38import java.net.UnknownHostException;
39
40/**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090041 * Identifies an IP address on a network link.
Lorenzo Colitti64483942013-11-15 18:43:52 +090042 *
43 * A {@code LinkAddress} consists of:
44 * <ul>
45 * <li>An IP address and prefix length (e.g., {@code 2001:db8::1/64} or {@code 192.0.2.1/24}).
46 * The address must be unicast, as multicast addresses cannot be assigned to interfaces.
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +090047 * <li>Address flags: A bitmask of {@code OsConstants.IFA_F_*} values representing properties
48 * of the address (e.g., {@code android.system.OsConstants.IFA_F_OPTIMISTIC}).
49 * <li>Address scope: One of the {@code OsConstants.IFA_F_*} values; defines the scope in which
50 * the address is unique (e.g.,
51 * {@code android.system.OsConstants.RT_SCOPE_LINK} or
52 * {@code android.system.OsConstants.RT_SCOPE_UNIVERSE}).
53 * </ul>
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070054 */
55public class LinkAddress implements Parcelable {
56 /**
57 * IPv4 or IPv6 address.
58 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010059 @UnsupportedAppUsage
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090060 private InetAddress address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070061
62 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090063 * Prefix length.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070064 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010065 @UnsupportedAppUsage
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090066 private int prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070067
Lorenzo Colitti64483942013-11-15 18:43:52 +090068 /**
69 * Address flags. A bitmask of IFA_F_* values.
70 */
71 private int flags;
72
73 /**
74 * Address scope. One of the RT_SCOPE_* constants.
75 */
76 private int scope;
77
78 /**
79 * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and
80 * RFC 6724 section 3.2.
81 * @hide
82 */
Hugo Benichi1dfb6b62017-08-08 13:06:04 +090083 private static int scopeForUnicastAddress(InetAddress addr) {
Lorenzo Colitti64483942013-11-15 18:43:52 +090084 if (addr.isAnyLocalAddress()) {
85 return RT_SCOPE_HOST;
86 }
87
88 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) {
89 return RT_SCOPE_LINK;
90 }
91
92 // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2
93 // says that they are assigned global scope.
94 if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) {
95 return RT_SCOPE_SITE;
96 }
97
98 return RT_SCOPE_UNIVERSE;
99 }
100
101 /**
Erik Klinebefe7782014-10-20 19:46:56 +0900102 * Utility function to check if |address| is a Unique Local IPv6 Unicast Address
103 * (a.k.a. "ULA"; RFC 4193).
104 *
105 * Per RFC 4193 section 8, fc00::/7 identifies these addresses.
106 */
107 private boolean isIPv6ULA() {
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900108 if (isIPv6()) {
Erik Klinebefe7782014-10-20 19:46:56 +0900109 byte[] bytes = address.getAddress();
Erik Kline1eb8c692016-07-08 17:21:26 +0900110 return ((bytes[0] & (byte)0xfe) == (byte)0xfc);
Erik Klinebefe7782014-10-20 19:46:56 +0900111 }
112 return false;
113 }
114
115 /**
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900116 * @return true if the address is IPv6.
117 * @hide
118 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100119 @UnsupportedAppUsage
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900120 public boolean isIPv6() {
121 return address instanceof Inet6Address;
122 }
123
124 /**
125 * @return true if the address is IPv4 or is a mapped IPv4 address.
126 * @hide
127 */
128 public boolean isIPv4() {
129 return address instanceof Inet4Address;
130 }
131
132 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900133 * Utility function for the constructors.
134 */
135 private void init(InetAddress address, int prefixLength, int flags, int scope) {
136 if (address == null ||
137 address.isMulticastAddress() ||
138 prefixLength < 0 ||
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900139 (address instanceof Inet4Address && prefixLength > 32) ||
Robert Greenwaltb979f792011-02-11 17:01:02 -0800140 (prefixLength > 128)) {
141 throw new IllegalArgumentException("Bad LinkAddress params " + address +
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900142 "/" + prefixLength);
Robert Greenwaltb979f792011-02-11 17:01:02 -0800143 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700144 this.address = address;
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700145 this.prefixLength = prefixLength;
Lorenzo Colitti64483942013-11-15 18:43:52 +0900146 this.flags = flags;
147 this.scope = scope;
148 }
149
150 /**
151 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with
152 * the specified flags and scope. Flags and scope are not checked for validity.
153 * @param address The IP address.
154 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700155 * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address.
156 * @param scope An integer defining the scope in which the address is unique (e.g.,
157 * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
158 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900159 */
160 public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) {
161 init(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700162 }
163
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900164 /**
165 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900166 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900167 * @param address The IP address.
168 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700169 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900170 */
Jack Yu4f956e02018-11-14 22:04:17 -0800171 @SystemApi
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900172 public LinkAddress(InetAddress address, int prefixLength) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900173 this(address, prefixLength, 0, 0);
174 this.scope = scopeForUnicastAddress(address);
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900175 }
176
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900177 /**
178 * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900179 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900180 * @param interfaceAddress The interface address.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700181 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900182 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700183 public LinkAddress(InterfaceAddress interfaceAddress) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900184 this(interfaceAddress.getAddress(),
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900185 interfaceAddress.getNetworkPrefixLength());
186 }
187
188 /**
189 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
Lorenzo Colitti64483942013-11-15 18:43:52 +0900190 * "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 +0900191 * @param string The string to parse.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700192 * @hide
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900193 */
Jack Yu4f956e02018-11-14 22:04:17 -0800194 @SystemApi
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900195 public LinkAddress(String address) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900196 this(address, 0, 0);
197 this.scope = scopeForUnicastAddress(this.address);
198 }
199
200 /**
201 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
202 * "2001:db8::1/64", with the specified flags and scope.
203 * @param string The string to parse.
204 * @param flags The address flags.
205 * @param scope The address scope.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700206 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900207 */
208 public LinkAddress(String address, int flags, int scope) {
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900209 // This may throw an IllegalArgumentException; catching it is the caller's responsibility.
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900210 // TODO: consider rejecting mapped IPv4 addresses such as "::ffff:192.0.2.5/24".
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900211 Pair<InetAddress, Integer> ipAndMask = NetworkUtils.parseIpAndMask(address);
212 init(ipAndMask.first, ipAndMask.second, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700213 }
214
Lorenzo Colitti64483942013-11-15 18:43:52 +0900215 /**
216 * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64".
217 * The string representation does not contain the flags and scope, just the address and prefix
218 * length.
219 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700220 @Override
221 public String toString() {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900222 return address.getHostAddress() + "/" + prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700223 }
224
225 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900226 * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900227 * their address, prefix length, flags and scope are equal. Thus, for example, two addresses
228 * that have the same address and prefix length are not equal if one of them is deprecated and
229 * the other is not.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700230 *
231 * @param obj the object to be tested for equality.
232 * @return {@code true} if both objects are equal, {@code false} otherwise.
233 */
234 @Override
235 public boolean equals(Object obj) {
236 if (!(obj instanceof LinkAddress)) {
237 return false;
238 }
239 LinkAddress linkAddress = (LinkAddress) obj;
240 return this.address.equals(linkAddress.address) &&
Lorenzo Colitti64483942013-11-15 18:43:52 +0900241 this.prefixLength == linkAddress.prefixLength &&
242 this.flags == linkAddress.flags &&
243 this.scope == linkAddress.scope;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700244 }
245
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900246 /**
247 * Returns a hashcode for this address.
John Wang4e900092011-04-04 12:35:42 -0700248 */
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900249 @Override
John Wang4e900092011-04-04 12:35:42 -0700250 public int hashCode() {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900251 return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope;
252 }
253
254 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700255 * Determines whether this {@code LinkAddress} and the provided {@code LinkAddress}
256 * represent the same address. Two {@code LinkAddresses} represent the same address
257 * if they have the same IP address and prefix length, even if their properties are
258 * different.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900259 *
260 * @param other the {@code LinkAddress} to compare to.
261 * @return {@code true} if both objects have the same address and prefix length, {@code false}
262 * otherwise.
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900263 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900264 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100265 @UnsupportedAppUsage
Lorenzo Colitti64483942013-11-15 18:43:52 +0900266 public boolean isSameAddressAs(LinkAddress other) {
267 return address.equals(other.address) && prefixLength == other.prefixLength;
John Wang4e900092011-04-04 12:35:42 -0700268 }
269
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700270 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700271 * Returns the {@link InetAddress} of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700272 */
273 public InetAddress getAddress() {
274 return address;
275 }
276
277 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700278 * Returns the prefix length of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700279 */
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900280 public int getPrefixLength() {
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700281 return prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700282 }
283
284 /**
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900285 * Returns the prefix length of this {@code LinkAddress}.
286 * TODO: Delete all callers and remove in favour of getPrefixLength().
287 * @hide
288 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100289 @UnsupportedAppUsage
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900290 public int getNetworkPrefixLength() {
291 return getPrefixLength();
292 }
293
294 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700295 * Returns the flags of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900296 */
297 public int getFlags() {
298 return flags;
299 }
300
301 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700302 * Returns the scope of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900303 */
304 public int getScope() {
305 return scope;
306 }
307
308 /**
309 * Returns true if this {@code LinkAddress} is global scope and preferred.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700310 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900311 */
312 public boolean isGlobalPreferred() {
Erik Klinebefe7782014-10-20 19:46:56 +0900313 /**
314 * Note that addresses flagged as IFA_F_OPTIMISTIC are
315 * simultaneously flagged as IFA_F_TENTATIVE (when the tentative
316 * state has cleared either DAD has succeeded or failed, and both
317 * flags are cleared regardless).
318 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900319 return (scope == RT_SCOPE_UNIVERSE &&
Erik Klinebefe7782014-10-20 19:46:56 +0900320 !isIPv6ULA() &&
321 (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED)) == 0L &&
322 ((flags & IFA_F_TENTATIVE) == 0L || (flags & IFA_F_OPTIMISTIC) != 0L));
Lorenzo Colitti64483942013-11-15 18:43:52 +0900323 }
324
325 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900326 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700327 */
328 public int describeContents() {
329 return 0;
330 }
331
332 /**
333 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700334 */
335 public void writeToParcel(Parcel dest, int flags) {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900336 dest.writeByteArray(address.getAddress());
337 dest.writeInt(prefixLength);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900338 dest.writeInt(this.flags);
339 dest.writeInt(scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700340 }
341
342 /**
343 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700344 */
345 public static final Creator<LinkAddress> CREATOR =
346 new Creator<LinkAddress>() {
347 public LinkAddress createFromParcel(Parcel in) {
348 InetAddress address = null;
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900349 try {
350 address = InetAddress.getByAddress(in.createByteArray());
351 } catch (UnknownHostException e) {
352 // Nothing we can do here. When we call the constructor, we'll throw an
353 // IllegalArgumentException, because a LinkAddress can't have a null
354 // InetAddress.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700355 }
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900356 int prefixLength = in.readInt();
Lorenzo Colitti64483942013-11-15 18:43:52 +0900357 int flags = in.readInt();
358 int scope = in.readInt();
359 return new LinkAddress(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700360 }
361
362 public LinkAddress[] newArray(int size) {
363 return new LinkAddress[size];
364 }
365 };
366}