blob: bcfe938823750130e28bd0790426263bc18c8109 [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
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070028import android.os.Parcel;
29import android.os.Parcelable;
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +090030import android.util.Pair;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070031
Robert Greenwaltb979f792011-02-11 17:01:02 -080032import java.net.Inet4Address;
Erik Klinebefe7782014-10-20 19:46:56 +090033import java.net.Inet6Address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070034import java.net.InetAddress;
35import java.net.InterfaceAddress;
36import java.net.UnknownHostException;
37
38/**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090039 * Identifies an IP address on a network link.
Lorenzo Colitti64483942013-11-15 18:43:52 +090040 *
41 * A {@code LinkAddress} consists of:
42 * <ul>
43 * <li>An IP address and prefix length (e.g., {@code 2001:db8::1/64} or {@code 192.0.2.1/24}).
44 * The address must be unicast, as multicast addresses cannot be assigned to interfaces.
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +090045 * <li>Address flags: A bitmask of {@code OsConstants.IFA_F_*} values representing properties
46 * of the address (e.g., {@code android.system.OsConstants.IFA_F_OPTIMISTIC}).
47 * <li>Address scope: One of the {@code OsConstants.IFA_F_*} values; defines the scope in which
48 * the address is unique (e.g.,
49 * {@code android.system.OsConstants.RT_SCOPE_LINK} or
50 * {@code android.system.OsConstants.RT_SCOPE_UNIVERSE}).
51 * </ul>
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070052 */
53public class LinkAddress implements Parcelable {
54 /**
55 * IPv4 or IPv6 address.
56 */
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090057 private InetAddress address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070058
59 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090060 * Prefix length.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070061 */
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090062 private int prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070063
Lorenzo Colitti64483942013-11-15 18:43:52 +090064 /**
65 * Address flags. A bitmask of IFA_F_* values.
66 */
67 private int flags;
68
69 /**
70 * Address scope. One of the RT_SCOPE_* constants.
71 */
72 private int scope;
73
74 /**
75 * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and
76 * RFC 6724 section 3.2.
77 * @hide
78 */
Hugo Benichi1dfb6b62017-08-08 13:06:04 +090079 private static int scopeForUnicastAddress(InetAddress addr) {
Lorenzo Colitti64483942013-11-15 18:43:52 +090080 if (addr.isAnyLocalAddress()) {
81 return RT_SCOPE_HOST;
82 }
83
84 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) {
85 return RT_SCOPE_LINK;
86 }
87
88 // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2
89 // says that they are assigned global scope.
90 if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) {
91 return RT_SCOPE_SITE;
92 }
93
94 return RT_SCOPE_UNIVERSE;
95 }
96
97 /**
Erik Klinebefe7782014-10-20 19:46:56 +090098 * Utility function to check if |address| is a Unique Local IPv6 Unicast Address
99 * (a.k.a. "ULA"; RFC 4193).
100 *
101 * Per RFC 4193 section 8, fc00::/7 identifies these addresses.
102 */
103 private boolean isIPv6ULA() {
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900104 if (isIPv6()) {
Erik Klinebefe7782014-10-20 19:46:56 +0900105 byte[] bytes = address.getAddress();
Erik Kline1eb8c692016-07-08 17:21:26 +0900106 return ((bytes[0] & (byte)0xfe) == (byte)0xfc);
Erik Klinebefe7782014-10-20 19:46:56 +0900107 }
108 return false;
109 }
110
111 /**
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900112 * @return true if the address is IPv6.
113 * @hide
114 */
115 public boolean isIPv6() {
116 return address instanceof Inet6Address;
117 }
118
119 /**
120 * @return true if the address is IPv4 or is a mapped IPv4 address.
121 * @hide
122 */
123 public boolean isIPv4() {
124 return address instanceof Inet4Address;
125 }
126
127 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900128 * Utility function for the constructors.
129 */
130 private void init(InetAddress address, int prefixLength, int flags, int scope) {
131 if (address == null ||
132 address.isMulticastAddress() ||
133 prefixLength < 0 ||
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900134 (address instanceof Inet4Address && prefixLength > 32) ||
Robert Greenwaltb979f792011-02-11 17:01:02 -0800135 (prefixLength > 128)) {
136 throw new IllegalArgumentException("Bad LinkAddress params " + address +
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900137 "/" + prefixLength);
Robert Greenwaltb979f792011-02-11 17:01:02 -0800138 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700139 this.address = address;
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700140 this.prefixLength = prefixLength;
Lorenzo Colitti64483942013-11-15 18:43:52 +0900141 this.flags = flags;
142 this.scope = scope;
143 }
144
145 /**
146 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with
147 * the specified flags and scope. Flags and scope are not checked for validity.
148 * @param address The IP address.
149 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700150 * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address.
151 * @param scope An integer defining the scope in which the address is unique (e.g.,
152 * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
153 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900154 */
155 public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) {
156 init(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700157 }
158
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900159 /**
160 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900161 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900162 * @param address The IP address.
163 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700164 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900165 */
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900166 public LinkAddress(InetAddress address, int prefixLength) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900167 this(address, prefixLength, 0, 0);
168 this.scope = scopeForUnicastAddress(address);
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900169 }
170
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900171 /**
172 * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}.
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 interfaceAddress The interface address.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700175 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900176 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700177 public LinkAddress(InterfaceAddress interfaceAddress) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900178 this(interfaceAddress.getAddress(),
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900179 interfaceAddress.getNetworkPrefixLength());
180 }
181
182 /**
183 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
Lorenzo Colitti64483942013-11-15 18:43:52 +0900184 * "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 +0900185 * @param string The string to parse.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700186 * @hide
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900187 */
188 public LinkAddress(String address) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900189 this(address, 0, 0);
190 this.scope = scopeForUnicastAddress(this.address);
191 }
192
193 /**
194 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
195 * "2001:db8::1/64", with the specified flags and scope.
196 * @param string The string to parse.
197 * @param flags The address flags.
198 * @param scope The address scope.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700199 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900200 */
201 public LinkAddress(String address, int flags, int scope) {
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900202 // This may throw an IllegalArgumentException; catching it is the caller's responsibility.
Hugo Benichi1dfb6b62017-08-08 13:06:04 +0900203 // TODO: consider rejecting mapped IPv4 addresses such as "::ffff:192.0.2.5/24".
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900204 Pair<InetAddress, Integer> ipAndMask = NetworkUtils.parseIpAndMask(address);
205 init(ipAndMask.first, ipAndMask.second, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700206 }
207
Lorenzo Colitti64483942013-11-15 18:43:52 +0900208 /**
209 * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64".
210 * The string representation does not contain the flags and scope, just the address and prefix
211 * length.
212 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700213 @Override
214 public String toString() {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900215 return address.getHostAddress() + "/" + prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700216 }
217
218 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900219 * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900220 * their address, prefix length, flags and scope are equal. Thus, for example, two addresses
221 * that have the same address and prefix length are not equal if one of them is deprecated and
222 * the other is not.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700223 *
224 * @param obj the object to be tested for equality.
225 * @return {@code true} if both objects are equal, {@code false} otherwise.
226 */
227 @Override
228 public boolean equals(Object obj) {
229 if (!(obj instanceof LinkAddress)) {
230 return false;
231 }
232 LinkAddress linkAddress = (LinkAddress) obj;
233 return this.address.equals(linkAddress.address) &&
Lorenzo Colitti64483942013-11-15 18:43:52 +0900234 this.prefixLength == linkAddress.prefixLength &&
235 this.flags == linkAddress.flags &&
236 this.scope == linkAddress.scope;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700237 }
238
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900239 /**
240 * Returns a hashcode for this address.
John Wang4e900092011-04-04 12:35:42 -0700241 */
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900242 @Override
John Wang4e900092011-04-04 12:35:42 -0700243 public int hashCode() {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900244 return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope;
245 }
246
247 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700248 * Determines whether this {@code LinkAddress} and the provided {@code LinkAddress}
249 * represent the same address. Two {@code LinkAddresses} represent the same address
250 * if they have the same IP address and prefix length, even if their properties are
251 * different.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900252 *
253 * @param other the {@code LinkAddress} to compare to.
254 * @return {@code true} if both objects have the same address and prefix length, {@code false}
255 * otherwise.
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900256 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900257 */
258 public boolean isSameAddressAs(LinkAddress other) {
259 return address.equals(other.address) && prefixLength == other.prefixLength;
John Wang4e900092011-04-04 12:35:42 -0700260 }
261
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700262 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700263 * Returns the {@link InetAddress} of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700264 */
265 public InetAddress getAddress() {
266 return address;
267 }
268
269 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700270 * Returns the prefix length of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700271 */
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900272 public int getPrefixLength() {
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700273 return prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700274 }
275
276 /**
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900277 * Returns the prefix length of this {@code LinkAddress}.
278 * TODO: Delete all callers and remove in favour of getPrefixLength().
279 * @hide
280 */
281 public int getNetworkPrefixLength() {
282 return getPrefixLength();
283 }
284
285 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700286 * Returns the flags of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900287 */
288 public int getFlags() {
289 return flags;
290 }
291
292 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700293 * Returns the scope of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900294 */
295 public int getScope() {
296 return scope;
297 }
298
299 /**
300 * Returns true if this {@code LinkAddress} is global scope and preferred.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700301 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900302 */
303 public boolean isGlobalPreferred() {
Erik Klinebefe7782014-10-20 19:46:56 +0900304 /**
305 * Note that addresses flagged as IFA_F_OPTIMISTIC are
306 * simultaneously flagged as IFA_F_TENTATIVE (when the tentative
307 * state has cleared either DAD has succeeded or failed, and both
308 * flags are cleared regardless).
309 */
Lorenzo Colitti64483942013-11-15 18:43:52 +0900310 return (scope == RT_SCOPE_UNIVERSE &&
Erik Klinebefe7782014-10-20 19:46:56 +0900311 !isIPv6ULA() &&
312 (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED)) == 0L &&
313 ((flags & IFA_F_TENTATIVE) == 0L || (flags & IFA_F_OPTIMISTIC) != 0L));
Lorenzo Colitti64483942013-11-15 18:43:52 +0900314 }
315
316 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900317 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700318 */
319 public int describeContents() {
320 return 0;
321 }
322
323 /**
324 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700325 */
326 public void writeToParcel(Parcel dest, int flags) {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900327 dest.writeByteArray(address.getAddress());
328 dest.writeInt(prefixLength);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900329 dest.writeInt(this.flags);
330 dest.writeInt(scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700331 }
332
333 /**
334 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700335 */
336 public static final Creator<LinkAddress> CREATOR =
337 new Creator<LinkAddress>() {
338 public LinkAddress createFromParcel(Parcel in) {
339 InetAddress address = null;
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900340 try {
341 address = InetAddress.getByAddress(in.createByteArray());
342 } catch (UnknownHostException e) {
343 // Nothing we can do here. When we call the constructor, we'll throw an
344 // IllegalArgumentException, because a LinkAddress can't have a null
345 // InetAddress.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700346 }
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900347 int prefixLength = in.readInt();
Lorenzo Colitti64483942013-11-15 18:43:52 +0900348 int flags = in.readInt();
349 int scope = in.readInt();
350 return new LinkAddress(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700351 }
352
353 public LinkAddress[] newArray(int size) {
354 return new LinkAddress[size];
355 }
356 };
357}