blob: a725becd74c619fdb9b679b9a74fd75ea2c26fff [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.
42 * <li>Address flags: A bitmask of {@code IFA_F_*} values representing properties of the address.
43 * <li>Address scope: An integer defining the scope in which the address is unique (e.g.,
44 * {@code RT_SCOPE_LINK} or {@code RT_SCOPE_SITE}).
45 * <ul>
46 *<p>
47 * When constructing a {@code LinkAddress}, the IP address and prefix are required. The flags and
48 * scope are optional. If they are not specified, the flags are set to zero, and the scope will be
49 * determined based on the IP address (e.g., link-local addresses will be created with a scope of
50 * {@code RT_SCOPE_LINK}, global addresses with {@code RT_SCOPE_UNIVERSE}, etc.) If they are
51 * specified, they are not checked for validity.
52 *
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070053 * @hide
54 */
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.
122 */
123 public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) {
124 init(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700125 }
126
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900127 /**
128 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900129 * The flags are set to zero and the scope is determined from the address.
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900130 * @param address The IP address.
131 * @param prefixLength The prefix length.
132 */
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.
142 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700143 public LinkAddress(InterfaceAddress interfaceAddress) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900144 this(interfaceAddress.getAddress(),
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900145 interfaceAddress.getNetworkPrefixLength());
146 }
147
148 /**
149 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
Lorenzo Colitti64483942013-11-15 18:43:52 +0900150 * "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 +0900151 * @param string The string to parse.
152 */
153 public LinkAddress(String address) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900154 this(address, 0, 0);
155 this.scope = scopeForUnicastAddress(this.address);
156 }
157
158 /**
159 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
160 * "2001:db8::1/64", with the specified flags and scope.
161 * @param string The string to parse.
162 * @param flags The address flags.
163 * @param scope The address scope.
164 */
165 public LinkAddress(String address, int flags, int scope) {
Lorenzo Colitti6eb8a622013-08-08 19:24:09 +0900166 InetAddress inetAddress = null;
167 int prefixLength = -1;
168 try {
169 String [] pieces = address.split("/", 2);
170 prefixLength = Integer.parseInt(pieces[1]);
171 inetAddress = InetAddress.parseNumericAddress(pieces[0]);
172 } catch (NullPointerException e) { // Null string.
173 } catch (ArrayIndexOutOfBoundsException e) { // No prefix length.
174 } catch (NumberFormatException e) { // Non-numeric prefix.
175 } catch (IllegalArgumentException e) { // Invalid IP address.
176 }
177
178 if (inetAddress == null || prefixLength == -1) {
179 throw new IllegalArgumentException("Bad LinkAddress params " + address);
180 }
181
Lorenzo Colitti64483942013-11-15 18:43:52 +0900182 init(inetAddress, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700183 }
184
Lorenzo Colitti64483942013-11-15 18:43:52 +0900185 /**
186 * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64".
187 * The string representation does not contain the flags and scope, just the address and prefix
188 * length.
189 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700190 @Override
191 public String toString() {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900192 return address.getHostAddress() + "/" + prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700193 }
194
195 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900196 * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if
197 * their address, prefix length, flags and scope are equal.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700198 *
199 * @param obj the object to be tested for equality.
200 * @return {@code true} if both objects are equal, {@code false} otherwise.
201 */
202 @Override
203 public boolean equals(Object obj) {
204 if (!(obj instanceof LinkAddress)) {
205 return false;
206 }
207 LinkAddress linkAddress = (LinkAddress) obj;
208 return this.address.equals(linkAddress.address) &&
Lorenzo Colitti64483942013-11-15 18:43:52 +0900209 this.prefixLength == linkAddress.prefixLength &&
210 this.flags == linkAddress.flags &&
211 this.scope == linkAddress.scope;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700212 }
213
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900214 /**
215 * Returns a hashcode for this address.
John Wang4e900092011-04-04 12:35:42 -0700216 */
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900217 @Override
John Wang4e900092011-04-04 12:35:42 -0700218 public int hashCode() {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900219 return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope;
220 }
221
222 /**
223 * Determines whether this {@code LinkAddress} and the provided {@code LinkAddress} represent
224 * the same address. Two LinkAddresses represent the same address if they have the same IP
225 * address and prefix length, even if their properties are different.
226 *
227 * @param other the {@code LinkAddress} to compare to.
228 * @return {@code true} if both objects have the same address and prefix length, {@code false}
229 * otherwise.
230 */
231 public boolean isSameAddressAs(LinkAddress other) {
232 return address.equals(other.address) && prefixLength == other.prefixLength;
John Wang4e900092011-04-04 12:35:42 -0700233 }
234
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700235 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900236 * Returns the InetAddress of this address.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700237 */
238 public InetAddress getAddress() {
239 return address;
240 }
241
242 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900243 * Returns the prefix length of this address.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700244 */
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700245 public int getNetworkPrefixLength() {
246 return prefixLength;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700247 }
248
249 /**
Lorenzo Colitti64483942013-11-15 18:43:52 +0900250 * Returns the flags of this address.
251 */
252 public int getFlags() {
253 return flags;
254 }
255
256 /**
257 * Returns the scope of this address.
258 */
259 public int getScope() {
260 return scope;
261 }
262
263 /**
264 * Returns true if this {@code LinkAddress} is global scope and preferred.
265 */
266 public boolean isGlobalPreferred() {
267 return (scope == RT_SCOPE_UNIVERSE &&
268 (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED | IFA_F_TENTATIVE)) == 0L);
269 }
270
271 /**
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900272 * Implement the Parcelable interface.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700273 * @hide
274 */
275 public int describeContents() {
276 return 0;
277 }
278
279 /**
280 * Implement the Parcelable interface.
281 * @hide
282 */
283 public void writeToParcel(Parcel dest, int flags) {
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900284 dest.writeByteArray(address.getAddress());
285 dest.writeInt(prefixLength);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900286 dest.writeInt(this.flags);
287 dest.writeInt(scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700288 }
289
290 /**
291 * Implement the Parcelable interface.
292 * @hide
293 */
294 public static final Creator<LinkAddress> CREATOR =
295 new Creator<LinkAddress>() {
296 public LinkAddress createFromParcel(Parcel in) {
297 InetAddress address = null;
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900298 try {
299 address = InetAddress.getByAddress(in.createByteArray());
300 } catch (UnknownHostException e) {
301 // Nothing we can do here. When we call the constructor, we'll throw an
302 // IllegalArgumentException, because a LinkAddress can't have a null
303 // InetAddress.
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700304 }
Lorenzo Colittie1ad1842013-11-27 15:03:10 +0900305 int prefixLength = in.readInt();
Lorenzo Colitti64483942013-11-15 18:43:52 +0900306 int flags = in.readInt();
307 int scope = in.readInt();
308 return new LinkAddress(address, prefixLength, flags, scope);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700309 }
310
311 public LinkAddress[] newArray(int size) {
312 return new LinkAddress[size];
313 }
314 };
315}