blob: 524607822db89c155b443b75c9a6ab1b84d571bf [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
19import android.os.Parcel;
20import android.os.Parcelable;
21
Robert Greenwaltb979f792011-02-11 17:01:02 -080022import java.net.Inet4Address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070023import java.net.InetAddress;
24import java.net.InterfaceAddress;
25import java.net.UnknownHostException;
26
Elliott Hughes34385d32014-04-28 11:11:32 -070027import static android.system.OsConstants.IFA_F_DADFAILED;
28import static android.system.OsConstants.IFA_F_DEPRECATED;
29import static android.system.OsConstants.IFA_F_TENTATIVE;
30import static android.system.OsConstants.RT_SCOPE_HOST;
31import static android.system.OsConstants.RT_SCOPE_LINK;
32import static android.system.OsConstants.RT_SCOPE_SITE;
33import static android.system.OsConstants.RT_SCOPE_UNIVERSE;
Lorenzo Colitti64483942013-11-15 18:43:52 +090034
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070035/**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090036 * Identifies an IP address on a network link.
Lorenzo Colitti64483942013-11-15 18:43:52 +090037 *
38 * A {@code LinkAddress} consists of:
39 * <ul>
40 * <li>An IP address and prefix length (e.g., {@code 2001:db8::1/64} or {@code 192.0.2.1/24}).
41 * The address must be unicast, as multicast addresses cannot be assigned to interfaces.
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +090042 * <li>Address flags: A bitmask of {@code OsConstants.IFA_F_*} values representing properties
43 * of the address (e.g., {@code android.system.OsConstants.IFA_F_OPTIMISTIC}).
44 * <li>Address scope: One of the {@code OsConstants.IFA_F_*} values; defines the scope in which
45 * the address is unique (e.g.,
46 * {@code android.system.OsConstants.RT_SCOPE_LINK} or
47 * {@code android.system.OsConstants.RT_SCOPE_UNIVERSE}).
48 * </ul>
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070049 */
50public class LinkAddress implements Parcelable {
51 /**
52 * IPv4 or IPv6 address.
53 */
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090054 private InetAddress address;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070055
56 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +090057 * Prefix length.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070058 */
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090059 private int prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070060
Lorenzo Colitti64483942013-11-15 18:43:52 +090061 /**
62 * Address flags. A bitmask of IFA_F_* values.
63 */
64 private int flags;
65
66 /**
67 * Address scope. One of the RT_SCOPE_* constants.
68 */
69 private int scope;
70
71 /**
72 * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and
73 * RFC 6724 section 3.2.
74 * @hide
75 */
76 static int scopeForUnicastAddress(InetAddress addr) {
77 if (addr.isAnyLocalAddress()) {
78 return RT_SCOPE_HOST;
79 }
80
81 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) {
82 return RT_SCOPE_LINK;
83 }
84
85 // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2
86 // says that they are assigned global scope.
87 if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) {
88 return RT_SCOPE_SITE;
89 }
90
91 return RT_SCOPE_UNIVERSE;
92 }
93
94 /**
95 * Utility function for the constructors.
96 */
97 private void init(InetAddress address, int prefixLength, int flags, int scope) {
98 if (address == null ||
99 address.isMulticastAddress() ||
100 prefixLength < 0 ||
Robert Greenwaltb979f792011-02-11 17:01:02 -0800101 ((address instanceof Inet4Address) && prefixLength > 32) ||
102 (prefixLength > 128)) {
103 throw new IllegalArgumentException("Bad LinkAddress params " + address +
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900104 "/" + prefixLength);
Robert Greenwaltb979f792011-02-11 17:01:02 -0800105 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700106 this.address = address;
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700107 this.prefixLength = prefixLength;
Lorenzo Colitti64483942013-11-15 18:43:52 +0900108 this.flags = flags;
109 this.scope = scope;
110 }
111
112 /**
113 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with
114 * the specified flags and scope. Flags and scope are not checked for validity.
115 * @param address The IP address.
116 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700117 * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address.
118 * @param scope An integer defining the scope in which the address is unique (e.g.,
119 * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
120 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900121 */
122 public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) {
123 init(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700124 }
125
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900126 /**
127 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900128 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900129 * @param address The IP address.
130 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700131 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900132 */
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900133 public LinkAddress(InetAddress address, int prefixLength) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900134 this(address, prefixLength, 0, 0);
135 this.scope = scopeForUnicastAddress(address);
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900136 }
137
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900138 /**
139 * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900140 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900141 * @param interfaceAddress The interface address.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700142 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900143 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700144 public LinkAddress(InterfaceAddress interfaceAddress) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900145 this(interfaceAddress.getAddress(),
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900146 interfaceAddress.getNetworkPrefixLength());
147 }
148
149 /**
150 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
Lorenzo Colitti64483942013-11-15 18:43:52 +0900151 * "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 +0900152 * @param string The string to parse.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700153 * @hide
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900154 */
155 public LinkAddress(String address) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900156 this(address, 0, 0);
157 this.scope = scopeForUnicastAddress(this.address);
158 }
159
160 /**
161 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
162 * "2001:db8::1/64", with the specified flags and scope.
163 * @param string The string to parse.
164 * @param flags The address flags.
165 * @param scope The address scope.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700166 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900167 */
168 public LinkAddress(String address, int flags, int scope) {
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900169 InetAddress inetAddress = null;
170 int prefixLength = -1;
171 try {
172 String [] pieces = address.split("/", 2);
173 prefixLength = Integer.parseInt(pieces[1]);
174 inetAddress = InetAddress.parseNumericAddress(pieces[0]);
175 } catch (NullPointerException e) { // Null string.
176 } catch (ArrayIndexOutOfBoundsException e) { // No prefix length.
177 } catch (NumberFormatException e) { // Non-numeric prefix.
178 } catch (IllegalArgumentException e) { // Invalid IP address.
179 }
180
181 if (inetAddress == null || prefixLength == -1) {
182 throw new IllegalArgumentException("Bad LinkAddress params " + address);
183 }
184
Lorenzo Colitti64483942013-11-15 18:43:52 +0900185 init(inetAddress, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700186 }
187
Lorenzo Colitti64483942013-11-15 18:43:52 +0900188 /**
189 * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64".
190 * The string representation does not contain the flags and scope, just the address and prefix
191 * length.
192 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700193 @Override
194 public String toString() {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900195 return address.getHostAddress() + "/" + prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700196 }
197
198 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900199 * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900200 * their address, prefix length, flags and scope are equal. Thus, for example, two addresses
201 * that have the same address and prefix length are not equal if one of them is deprecated and
202 * the other is not.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700203 *
204 * @param obj the object to be tested for equality.
205 * @return {@code true} if both objects are equal, {@code false} otherwise.
206 */
207 @Override
208 public boolean equals(Object obj) {
209 if (!(obj instanceof LinkAddress)) {
210 return false;
211 }
212 LinkAddress linkAddress = (LinkAddress) obj;
213 return this.address.equals(linkAddress.address) &&
Lorenzo Colitti64483942013-11-15 18:43:52 +0900214 this.prefixLength == linkAddress.prefixLength &&
215 this.flags == linkAddress.flags &&
216 this.scope == linkAddress.scope;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700217 }
218
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900219 /**
220 * Returns a hashcode for this address.
John Wang4e900092011-04-04 12:35:42 -0700221 */
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900222 @Override
John Wang4e900092011-04-04 12:35:42 -0700223 public int hashCode() {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900224 return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope;
225 }
226
227 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700228 * Determines whether this {@code LinkAddress} and the provided {@code LinkAddress}
229 * represent the same address. Two {@code LinkAddresses} represent the same address
230 * if they have the same IP address and prefix length, even if their properties are
231 * different.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900232 *
233 * @param other the {@code LinkAddress} to compare to.
234 * @return {@code true} if both objects have the same address and prefix length, {@code false}
235 * otherwise.
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900236 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900237 */
238 public boolean isSameAddressAs(LinkAddress other) {
239 return address.equals(other.address) && prefixLength == other.prefixLength;
John Wang4e900092011-04-04 12:35:42 -0700240 }
241
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700242 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700243 * Returns the {@link InetAddress} of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700244 */
245 public InetAddress getAddress() {
246 return address;
247 }
248
249 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700250 * Returns the prefix length of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700251 */
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900252 public int getPrefixLength() {
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700253 return prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700254 }
255
256 /**
Lorenzo Colitti7dc78cf2014-06-09 22:58:46 +0900257 * Returns the prefix length of this {@code LinkAddress}.
258 * TODO: Delete all callers and remove in favour of getPrefixLength().
259 * @hide
260 */
261 public int getNetworkPrefixLength() {
262 return getPrefixLength();
263 }
264
265 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700266 * Returns the flags of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900267 */
268 public int getFlags() {
269 return flags;
270 }
271
272 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700273 * Returns the scope of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900274 */
275 public int getScope() {
276 return scope;
277 }
278
279 /**
280 * Returns true if this {@code LinkAddress} is global scope and preferred.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700281 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900282 */
283 public boolean isGlobalPreferred() {
284 return (scope == RT_SCOPE_UNIVERSE &&
285 (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED | IFA_F_TENTATIVE)) == 0L);
286 }
287
288 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900289 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700290 * @hide
291 */
292 public int describeContents() {
293 return 0;
294 }
295
296 /**
297 * Implement the Parcelable interface.
298 * @hide
299 */
300 public void writeToParcel(Parcel dest, int flags) {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900301 dest.writeByteArray(address.getAddress());
302 dest.writeInt(prefixLength);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900303 dest.writeInt(this.flags);
304 dest.writeInt(scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700305 }
306
307 /**
308 * Implement the Parcelable interface.
309 * @hide
310 */
311 public static final Creator<LinkAddress> CREATOR =
312 new Creator<LinkAddress>() {
313 public LinkAddress createFromParcel(Parcel in) {
314 InetAddress address = null;
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900315 try {
316 address = InetAddress.getByAddress(in.createByteArray());
317 } catch (UnknownHostException e) {
318 // Nothing we can do here. When we call the constructor, we'll throw an
319 // IllegalArgumentException, because a LinkAddress can't have a null
320 // InetAddress.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700321 }
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900322 int prefixLength = in.readInt();
Lorenzo Colitti64483942013-11-15 18:43:52 +0900323 int flags = in.readInt();
324 int scope = in.readInt();
325 return new LinkAddress(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700326 }
327
328 public LinkAddress[] newArray(int size) {
329 return new LinkAddress[size];
330 }
331 };
332}