blob: 1bc0d327abde23dd02378b441dfe1832964c7588 [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
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010028import android.annotation.UnsupportedAppUsage;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070029import android.os.Parcel;
30import android.os.Parcelable;
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +090031import android.util.Pair;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070032
Robert Greenwaltb979f792011-02-11 17:01:02 -080033import java.net.Inet4Address;
Erik Klinebefe7782014-10-20 19:46:56 +090034import java.net.Inet6Address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070035import java.net.InetAddress;
36import java.net.InterfaceAddress;
37import java.net.UnknownHostException;
38
39/**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090040 * Identifies an IP address on a network link.
Lorenzo Colitti64483942013-11-15 18:43:52 +090041 *
42 * A {@code LinkAddress} consists of:
43 * <ul>
44 * <li>An IP address and prefix length (e.g., {@code 2001:db8::1/64} or {@code 192.0.2.1/24}).
45 * The address must be unicast, as multicast addresses cannot be assigned to interfaces.
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +090046 * <li>Address flags: A bitmask of {@code OsConstants.IFA_F_*} values representing properties
47 * of the address (e.g., {@code android.system.OsConstants.IFA_F_OPTIMISTIC}).
48 * <li>Address scope: One of the {@code OsConstants.IFA_F_*} values; defines the scope in which
49 * the address is unique (e.g.,
50 * {@code android.system.OsConstants.RT_SCOPE_LINK} or
51 * {@code android.system.OsConstants.RT_SCOPE_UNIVERSE}).
52 * </ul>
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070053 */
54public class LinkAddress implements Parcelable {
55 /**
56 * IPv4 or IPv6 address.
57 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010058 @UnsupportedAppUsage
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090059 private InetAddress address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070060
61 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090062 * Prefix length.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070063 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010064 @UnsupportedAppUsage
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090065 private int prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070066
Lorenzo Colitti64483942013-11-15 18:43:52 +090067 /**
68 * Address flags. A bitmask of IFA_F_* values.
69 */
70 private int flags;
71
72 /**
73 * Address scope. One of the RT_SCOPE_* constants.
74 */
75 private int scope;
76
77 /**
78 * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and
79 * RFC 6724 section 3.2.
80 * @hide
81 */
Hugo Benichi1dfb6b62017-08-08 13:06:04 +090082 private static int scopeForUnicastAddress(InetAddress addr) {
Lorenzo Colitti64483942013-11-15 18:43:52 +090083 if (addr.isAnyLocalAddress()) {
84 return RT_SCOPE_HOST;
85 }
86
87 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) {
88 return RT_SCOPE_LINK;
89 }
90
91 // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2
92 // says that they are assigned global scope.
93 if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) {
94 return RT_SCOPE_SITE;
95 }
96
97 return RT_SCOPE_UNIVERSE;
98 }
99
100 /**
Erik Klinebefe7782014-10-20 19:46:56 +0900101 * Utility function to check if |address| is a Unique Local IPv6 Unicast Address
102 * (a.k.a. "ULA"; RFC 4193).
103 *
104 * Per RFC 4193 section 8, fc00::/7 identifies these addresses.
105 */
106 private boolean isIPv6ULA() {
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900107 if (isIPv6()) {
Erik Klinebefe7782014-10-20 19:46:56 +0900108 byte[] bytes = address.getAddress();
Erik Kline1eb8c692016-07-08 17:21:26 +0900109 return ((bytes[0] & (byte)0xfe) == (byte)0xfc);
Erik Klinebefe7782014-10-20 19:46:56 +0900110 }
111 return false;
112 }
113
114 /**
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900115 * @return true if the address is IPv6.
116 * @hide
117 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100118 @UnsupportedAppUsage
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900119 public boolean isIPv6() {
120 return address instanceof Inet6Address;
121 }
122
123 /**
124 * @return true if the address is IPv4 or is a mapped IPv4 address.
125 * @hide
126 */
127 public boolean isIPv4() {
128 return address instanceof Inet4Address;
129 }
130
131 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900132 * Utility function for the constructors.
133 */
134 private void init(InetAddress address, int prefixLength, int flags, int scope) {
135 if (address == null ||
136 address.isMulticastAddress() ||
137 prefixLength < 0 ||
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900138 (address instanceof Inet4Address && prefixLength > 32) ||
Robert Greenwaltb979f792011-02-11 17:01:02 -0800139 (prefixLength > 128)) {
140 throw new IllegalArgumentException("Bad LinkAddress params " + address +
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900141 "/" + prefixLength);
Robert Greenwaltb979f792011-02-11 17:01:02 -0800142 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700143 this.address = address;
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700144 this.prefixLength = prefixLength;
Lorenzo Colitti64483942013-11-15 18:43:52 +0900145 this.flags = flags;
146 this.scope = scope;
147 }
148
149 /**
150 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with
151 * the specified flags and scope. Flags and scope are not checked for validity.
152 * @param address The IP address.
153 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700154 * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address.
155 * @param scope An integer defining the scope in which the address is unique (e.g.,
156 * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
157 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900158 */
159 public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) {
160 init(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700161 }
162
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900163 /**
164 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900165 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900166 * @param address The IP address.
167 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700168 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900169 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100170 @UnsupportedAppUsage
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900171 public LinkAddress(InetAddress address, int prefixLength) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900172 this(address, prefixLength, 0, 0);
173 this.scope = scopeForUnicastAddress(address);
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900174 }
175
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900176 /**
177 * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900178 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900179 * @param interfaceAddress The interface address.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700180 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900181 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700182 public LinkAddress(InterfaceAddress interfaceAddress) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900183 this(interfaceAddress.getAddress(),
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900184 interfaceAddress.getNetworkPrefixLength());
185 }
186
187 /**
188 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
Lorenzo Colitti64483942013-11-15 18:43:52 +0900189 * "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 +0900190 * @param string The string to parse.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700191 * @hide
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900192 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100193 @UnsupportedAppUsage
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900194 public LinkAddress(String address) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900195 this(address, 0, 0);
196 this.scope = scopeForUnicastAddress(this.address);
197 }
198
199 /**
200 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
201 * "2001:db8::1/64", with the specified flags and scope.
202 * @param string The string to parse.
203 * @param flags The address flags.
204 * @param scope The address scope.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700205 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900206 */
207 public LinkAddress(String address, int flags, int scope) {
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900208 // This may throw an IllegalArgumentException; catching it is the caller's responsibility.
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900209 // TODO: consider rejecting mapped IPv4 addresses such as "::ffff:192.0.2.5/24".
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900210 Pair<InetAddress, Integer> ipAndMask = NetworkUtils.parseIpAndMask(address);
211 init(ipAndMask.first, ipAndMask.second, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700212 }
213
Lorenzo Colitti64483942013-11-15 18:43:52 +0900214 /**
215 * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64".
216 * The string representation does not contain the flags and scope, just the address and prefix
217 * length.
218 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700219 @Override
220 public String toString() {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900221 return address.getHostAddress() + "/" + prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700222 }
223
224 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900225 * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900226 * their address, prefix length, flags and scope are equal. Thus, for example, two addresses
227 * that have the same address and prefix length are not equal if one of them is deprecated and
228 * the other is not.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700229 *
230 * @param obj the object to be tested for equality.
231 * @return {@code true} if both objects are equal, {@code false} otherwise.
232 */
233 @Override
234 public boolean equals(Object obj) {
235 if (!(obj instanceof LinkAddress)) {
236 return false;
237 }
238 LinkAddress linkAddress = (LinkAddress) obj;
239 return this.address.equals(linkAddress.address) &&
Lorenzo Colitti64483942013-11-15 18:43:52 +0900240 this.prefixLength == linkAddress.prefixLength &&
241 this.flags == linkAddress.flags &&
242 this.scope == linkAddress.scope;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700243 }
244
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900245 /**
246 * Returns a hashcode for this address.
John Wang4e900092011-04-04 12:35:42 -0700247 */
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900248 @Override
John Wang4e900092011-04-04 12:35:42 -0700249 public int hashCode() {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900250 return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope;
251 }
252
253 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700254 * Determines whether this {@code LinkAddress} and the provided {@code LinkAddress}
255 * represent the same address. Two {@code LinkAddresses} represent the same address
256 * if they have the same IP address and prefix length, even if their properties are
257 * different.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900258 *
259 * @param other the {@code LinkAddress} to compare to.
260 * @return {@code true} if both objects have the same address and prefix length, {@code false}
261 * otherwise.
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900262 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900263 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100264 @UnsupportedAppUsage
Lorenzo Colitti64483942013-11-15 18:43:52 +0900265 public boolean isSameAddressAs(LinkAddress other) {
266 return address.equals(other.address) && prefixLength == other.prefixLength;
John Wang4e900092011-04-04 12:35:42 -0700267 }
268
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700269 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700270 * Returns the {@link InetAddress} of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700271 */
272 public InetAddress getAddress() {
273 return address;
274 }
275
276 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700277 * Returns the prefix length of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700278 */
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900279 public int getPrefixLength() {
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700280 return prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700281 }
282
283 /**
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900284 * Returns the prefix length of this {@code LinkAddress}.
285 * TODO: Delete all callers and remove in favour of getPrefixLength().
286 * @hide
287 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100288 @UnsupportedAppUsage
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900289 public int getNetworkPrefixLength() {
290 return getPrefixLength();
291 }
292
293 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700294 * Returns the flags of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900295 */
296 public int getFlags() {
297 return flags;
298 }
299
300 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700301 * Returns the scope of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900302 */
303 public int getScope() {
304 return scope;
305 }
306
307 /**
308 * Returns true if this {@code LinkAddress} is global scope and preferred.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700309 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900310 */
311 public boolean isGlobalPreferred() {
Erik Klinebefe7782014-10-20 19:46:56 +0900312 /**
313 * Note that addresses flagged as IFA_F_OPTIMISTIC are
314 * simultaneously flagged as IFA_F_TENTATIVE (when the tentative
315 * state has cleared either DAD has succeeded or failed, and both
316 * flags are cleared regardless).
317 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900318 return (scope == RT_SCOPE_UNIVERSE &&
Erik Klinebefe7782014-10-20 19:46:56 +0900319 !isIPv6ULA() &&
320 (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED)) == 0L &&
321 ((flags & IFA_F_TENTATIVE) == 0L || (flags & IFA_F_OPTIMISTIC) != 0L));
Lorenzo Colitti64483942013-11-15 18:43:52 +0900322 }
323
324 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900325 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700326 */
327 public int describeContents() {
328 return 0;
329 }
330
331 /**
332 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700333 */
334 public void writeToParcel(Parcel dest, int flags) {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900335 dest.writeByteArray(address.getAddress());
336 dest.writeInt(prefixLength);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900337 dest.writeInt(this.flags);
338 dest.writeInt(scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700339 }
340
341 /**
342 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700343 */
344 public static final Creator<LinkAddress> CREATOR =
345 new Creator<LinkAddress>() {
346 public LinkAddress createFromParcel(Parcel in) {
347 InetAddress address = null;
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900348 try {
349 address = InetAddress.getByAddress(in.createByteArray());
350 } catch (UnknownHostException e) {
351 // Nothing we can do here. When we call the constructor, we'll throw an
352 // IllegalArgumentException, because a LinkAddress can't have a null
353 // InetAddress.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700354 }
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900355 int prefixLength = in.readInt();
Lorenzo Colitti64483942013-11-15 18:43:52 +0900356 int flags = in.readInt();
357 int scope = in.readInt();
358 return new LinkAddress(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700359 }
360
361 public LinkAddress[] newArray(int size) {
362 return new LinkAddress[size];
363 }
364 };
365}