blob: d07c0b61a13bf2d416a8aaebc1988428185232fa [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.
Robert Greenwaltfd202e62014-05-18 09:39:18 -070042 * <li>Address flags: A bitmask of {@code IFA_F_*} values representing properties
43 * of the address.
Lorenzo Colitti64483942013-11-15 18:43:52 +090044 * <li>Address scope: An integer defining the scope in which the address is unique (e.g.,
45 * {@code RT_SCOPE_LINK} or {@code RT_SCOPE_SITE}).
46 * <ul>
47 *<p>
48 * When constructing a {@code LinkAddress}, the IP address and prefix are required. The flags and
49 * scope are optional. If they are not specified, the flags are set to zero, and the scope will be
50 * determined based on the IP address (e.g., link-local addresses will be created with a scope of
Robert Greenwaltfd202e62014-05-18 09:39:18 -070051 * {@code RT_SCOPE_LINK}, global addresses with {@code RT_SCOPE_UNIVERSE},
52 * etc.) If they are specified, they are not checked for validity.
Lorenzo Colitti64483942013-11-15 18:43:52 +090053 *
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070054 */
55public class LinkAddress implements Parcelable {
56 /**
57 * IPv4 or IPv6 address.
58 */
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 */
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +090064 private int prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070065
Lorenzo Colitti64483942013-11-15 18:43:52 +090066 /**
67 * Address flags. A bitmask of IFA_F_* values.
68 */
69 private int flags;
70
71 /**
72 * Address scope. One of the RT_SCOPE_* constants.
73 */
74 private int scope;
75
76 /**
77 * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and
78 * RFC 6724 section 3.2.
79 * @hide
80 */
81 static int scopeForUnicastAddress(InetAddress addr) {
82 if (addr.isAnyLocalAddress()) {
83 return RT_SCOPE_HOST;
84 }
85
86 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) {
87 return RT_SCOPE_LINK;
88 }
89
90 // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2
91 // says that they are assigned global scope.
92 if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) {
93 return RT_SCOPE_SITE;
94 }
95
96 return RT_SCOPE_UNIVERSE;
97 }
98
99 /**
100 * Utility function for the constructors.
101 */
102 private void init(InetAddress address, int prefixLength, int flags, int scope) {
103 if (address == null ||
104 address.isMulticastAddress() ||
105 prefixLength < 0 ||
Robert Greenwaltb979f792011-02-11 17:01:02 -0800106 ((address instanceof Inet4Address) && prefixLength > 32) ||
107 (prefixLength > 128)) {
108 throw new IllegalArgumentException("Bad LinkAddress params " + address +
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900109 "/" + prefixLength);
Robert Greenwaltb979f792011-02-11 17:01:02 -0800110 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700111 this.address = address;
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700112 this.prefixLength = prefixLength;
Lorenzo Colitti64483942013-11-15 18:43:52 +0900113 this.flags = flags;
114 this.scope = scope;
115 }
116
117 /**
118 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with
119 * the specified flags and scope. Flags and scope are not checked for validity.
120 * @param address The IP address.
121 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700122 * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address.
123 * @param scope An integer defining the scope in which the address is unique (e.g.,
124 * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
125 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900126 */
127 public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) {
128 init(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700129 }
130
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900131 /**
132 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900133 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900134 * @param address The IP address.
135 * @param prefixLength The prefix length.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700136 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900137 */
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900138 public LinkAddress(InetAddress address, int prefixLength) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900139 this(address, prefixLength, 0, 0);
140 this.scope = scopeForUnicastAddress(address);
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900141 }
142
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900143 /**
144 * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900145 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900146 * @param interfaceAddress The interface address.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700147 * @hide
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900148 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700149 public LinkAddress(InterfaceAddress interfaceAddress) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900150 this(interfaceAddress.getAddress(),
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900151 interfaceAddress.getNetworkPrefixLength());
152 }
153
154 /**
155 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
Lorenzo Colitti64483942013-11-15 18:43:52 +0900156 * "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 +0900157 * @param string The string to parse.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700158 * @hide
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900159 */
160 public LinkAddress(String address) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900161 this(address, 0, 0);
162 this.scope = scopeForUnicastAddress(this.address);
163 }
164
165 /**
166 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
167 * "2001:db8::1/64", with the specified flags and scope.
168 * @param string The string to parse.
169 * @param flags The address flags.
170 * @param scope The address scope.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700171 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900172 */
173 public LinkAddress(String address, int flags, int scope) {
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900174 InetAddress inetAddress = null;
175 int prefixLength = -1;
176 try {
177 String [] pieces = address.split("/", 2);
178 prefixLength = Integer.parseInt(pieces[1]);
179 inetAddress = InetAddress.parseNumericAddress(pieces[0]);
180 } catch (NullPointerException e) { // Null string.
181 } catch (ArrayIndexOutOfBoundsException e) { // No prefix length.
182 } catch (NumberFormatException e) { // Non-numeric prefix.
183 } catch (IllegalArgumentException e) { // Invalid IP address.
184 }
185
186 if (inetAddress == null || prefixLength == -1) {
187 throw new IllegalArgumentException("Bad LinkAddress params " + address);
188 }
189
Lorenzo Colitti64483942013-11-15 18:43:52 +0900190 init(inetAddress, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700191 }
192
Lorenzo Colitti64483942013-11-15 18:43:52 +0900193 /**
194 * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64".
195 * The string representation does not contain the flags and scope, just the address and prefix
196 * length.
197 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700198 @Override
199 public String toString() {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900200 return address.getHostAddress() + "/" + prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700201 }
202
203 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900204 * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if
205 * their address, prefix length, flags and scope are equal.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700206 *
207 * @param obj the object to be tested for equality.
208 * @return {@code true} if both objects are equal, {@code false} otherwise.
209 */
210 @Override
211 public boolean equals(Object obj) {
212 if (!(obj instanceof LinkAddress)) {
213 return false;
214 }
215 LinkAddress linkAddress = (LinkAddress) obj;
216 return this.address.equals(linkAddress.address) &&
Lorenzo Colitti64483942013-11-15 18:43:52 +0900217 this.prefixLength == linkAddress.prefixLength &&
218 this.flags == linkAddress.flags &&
219 this.scope == linkAddress.scope;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700220 }
221
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900222 /**
223 * Returns a hashcode for this address.
John Wang4e900092011-04-04 12:35:42 -0700224 */
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900225 @Override
John Wang4e900092011-04-04 12:35:42 -0700226 public int hashCode() {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900227 return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope;
228 }
229
230 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700231 * Determines whether this {@code LinkAddress} and the provided {@code LinkAddress}
232 * represent the same address. Two {@code LinkAddresses} represent the same address
233 * if they have the same IP address and prefix length, even if their properties are
234 * different.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900235 *
236 * @param other the {@code LinkAddress} to compare to.
237 * @return {@code true} if both objects have the same address and prefix length, {@code false}
238 * otherwise.
239 */
240 public boolean isSameAddressAs(LinkAddress other) {
241 return address.equals(other.address) && prefixLength == other.prefixLength;
John Wang4e900092011-04-04 12:35:42 -0700242 }
243
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700244 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700245 * Returns the {@link InetAddress} of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700246 */
247 public InetAddress getAddress() {
248 return address;
249 }
250
251 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700252 * Returns the prefix length of this {@code LinkAddress}.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700253 */
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700254 public int getNetworkPrefixLength() {
255 return prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700256 }
257
258 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700259 * Returns the flags of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900260 */
261 public int getFlags() {
262 return flags;
263 }
264
265 /**
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700266 * Returns the scope of this {@code LinkAddress}.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900267 */
268 public int getScope() {
269 return scope;
270 }
271
272 /**
273 * Returns true if this {@code LinkAddress} is global scope and preferred.
Robert Greenwaltfd202e62014-05-18 09:39:18 -0700274 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900275 */
276 public boolean isGlobalPreferred() {
277 return (scope == RT_SCOPE_UNIVERSE &&
278 (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED | IFA_F_TENTATIVE)) == 0L);
279 }
280
281 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900282 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700283 * @hide
284 */
285 public int describeContents() {
286 return 0;
287 }
288
289 /**
290 * Implement the Parcelable interface.
291 * @hide
292 */
293 public void writeToParcel(Parcel dest, int flags) {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900294 dest.writeByteArray(address.getAddress());
295 dest.writeInt(prefixLength);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900296 dest.writeInt(this.flags);
297 dest.writeInt(scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700298 }
299
300 /**
301 * Implement the Parcelable interface.
302 * @hide
303 */
304 public static final Creator<LinkAddress> CREATOR =
305 new Creator<LinkAddress>() {
306 public LinkAddress createFromParcel(Parcel in) {
307 InetAddress address = null;
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900308 try {
309 address = InetAddress.getByAddress(in.createByteArray());
310 } catch (UnknownHostException e) {
311 // Nothing we can do here. When we call the constructor, we'll throw an
312 // IllegalArgumentException, because a LinkAddress can't have a null
313 // InetAddress.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700314 }
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900315 int prefixLength = in.readInt();
Lorenzo Colitti64483942013-11-15 18:43:52 +0900316 int flags = in.readInt();
317 int scope = in.readInt();
318 return new LinkAddress(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700319 }
320
321 public LinkAddress[] newArray(int size) {
322 return new LinkAddress[size];
323 }
324 };
325}