blob: 6b4f2d5acd00dd788f24784cbb56a2d9de424a5c [file] [log] [blame]
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -07001/*
2 * Copyright (C) 2014 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;
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +090021import android.util.Pair;
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -070022
23import java.net.InetAddress;
24import java.net.UnknownHostException;
25import java.util.Arrays;
26
27/**
28 * This class represents an IP prefix, i.e., a contiguous block of IP addresses aligned on a
29 * power of two boundary (also known as an "IP subnet"). A prefix is specified by two pieces of
30 * information:
31 *
32 * <ul>
33 * <li>A starting IP address (IPv4 or IPv6). This is the first IP address of the prefix.
34 * <li>A prefix length. This specifies the length of the prefix by specifing the number of bits
35 * in the IP address, starting from the most significant bit in network byte order, that
36 * are constant for all addresses in the prefix.
37 * </ul>
38 *
39 * For example, the prefix <code>192.0.2.0/24</code> covers the 256 IPv4 addresses from
40 * <code>192.0.2.0</code> to <code>192.0.2.255</code>, inclusive, and the prefix
41 * <code>2001:db8:1:2</code> covers the 2^64 IPv6 addresses from <code>2001:db8:1:2::</code> to
42 * <code>2001:db8:1:2:ffff:ffff:ffff:ffff</code>, inclusive.
43 *
44 * Objects of this class are immutable.
45 */
Robert Greenwalt0cbe94a2014-06-12 12:57:19 -070046public final class IpPrefix implements Parcelable {
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -070047 private final byte[] address; // network byte order
48 private final int prefixLength;
49
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +090050 private void checkAndMaskAddressAndPrefixLength() {
51 if (address.length != 4 && address.length != 16) {
52 throw new IllegalArgumentException(
53 "IpPrefix has " + address.length + " bytes which is neither 4 nor 16");
54 }
55 NetworkUtils.maskRawAddress(address, prefixLength);
56 }
57
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -070058 /**
59 * Constructs a new {@code IpPrefix} from a byte array containing an IPv4 or IPv6 address in
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +090060 * network byte order and a prefix length. Silently truncates the address to the prefix length,
61 * so for example {@code 192.0.2.1/24} is silently converted to {@code 192.0.2.0/24}.
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -070062 *
63 * @param address the IP address. Must be non-null and exactly 4 or 16 bytes long.
64 * @param prefixLength the prefix length. Must be &gt;= 0 and &lt;= (32 or 128) (IPv4 or IPv6).
65 *
66 * @hide
67 */
68 public IpPrefix(byte[] address, int prefixLength) {
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -070069 this.address = address.clone();
70 this.prefixLength = prefixLength;
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +090071 checkAndMaskAddressAndPrefixLength();
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -070072 }
73
74 /**
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +090075 * Constructs a new {@code IpPrefix} from an IPv4 or IPv6 address and a prefix length. Silently
76 * truncates the address to the prefix length, so for example {@code 192.0.2.1/24} is silently
77 * converted to {@code 192.0.2.0/24}.
78 *
79 * @param address the IP address. Must be non-null.
80 * @param prefixLength the prefix length. Must be &gt;= 0 and &lt;= (32 or 128) (IPv4 or IPv6).
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -070081 * @hide
82 */
83 public IpPrefix(InetAddress address, int prefixLength) {
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +090084 // We don't reuse the (byte[], int) constructor because it calls clone() on the byte array,
85 // which is unnecessary because getAddress() already returns a clone.
86 this.address = address.getAddress();
87 this.prefixLength = prefixLength;
88 checkAndMaskAddressAndPrefixLength();
89 }
90
91 /**
92 * Constructs a new IpPrefix from a string such as "192.0.2.1/24" or "2001:db8::1/64".
93 * Silently truncates the address to the prefix length, so for example {@code 192.0.2.1/24}
94 * is silently converted to {@code 192.0.2.0/24}.
95 *
96 * @param prefix the prefix to parse
97 *
98 * @hide
99 */
100 public IpPrefix(String prefix) {
101 // We don't reuse the (InetAddress, int) constructor because "error: call to this must be
102 // first statement in constructor". We could factor out setting the member variables to an
103 // init() method, but if we did, then we'd have to make the members non-final, or "error:
104 // cannot assign a value to final variable address". So we just duplicate the code here.
105 Pair<InetAddress, Integer> ipAndMask = NetworkUtils.parseIpAndMask(prefix);
106 this.address = ipAndMask.first.getAddress();
107 this.prefixLength = ipAndMask.second;
108 checkAndMaskAddressAndPrefixLength();
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700109 }
110
111 /**
112 * Compares this {@code IpPrefix} object against the specified object in {@code obj}. Two
113 * objects are equal if they have the same startAddress and prefixLength.
114 *
115 * @param obj the object to be tested for equality.
116 * @return {@code true} if both objects are equal, {@code false} otherwise.
117 */
118 @Override
119 public boolean equals(Object obj) {
120 if (!(obj instanceof IpPrefix)) {
121 return false;
122 }
123 IpPrefix that = (IpPrefix) obj;
124 return Arrays.equals(this.address, that.address) && this.prefixLength == that.prefixLength;
125 }
126
127 /**
128 * Gets the hashcode of the represented IP prefix.
129 *
130 * @return the appropriate hashcode value.
131 */
132 @Override
133 public int hashCode() {
134 return Arrays.hashCode(address) + 11 * prefixLength;
135 }
136
137 /**
138 * Returns a copy of the first IP address in the prefix. Modifying the returned object does not
139 * change this object's contents.
140 *
141 * @return the address in the form of a byte array.
142 */
143 public InetAddress getAddress() {
144 try {
145 return InetAddress.getByAddress(address);
146 } catch (UnknownHostException e) {
147 // Cannot happen. InetAddress.getByAddress can only throw an exception if the byte
148 // array is the wrong length, but we check that in the constructor.
149 return null;
150 }
151 }
152
153 /**
154 * Returns a copy of the IP address bytes in network order (the highest order byte is the zeroth
155 * element). Modifying the returned array does not change this object's contents.
156 *
157 * @return the address in the form of a byte array.
158 */
159 public byte[] getRawAddress() {
160 return address.clone();
161 }
162
163 /**
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900164 * Returns the prefix length of this {@code IpPrefix}.
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700165 *
166 * @return the prefix length.
167 */
168 public int getPrefixLength() {
169 return prefixLength;
170 }
171
172 /**
Erik Klineacc8c092015-04-13 15:33:34 +0900173 * Determines whether the prefix contains the specified address.
174 *
175 * @param address An {@link InetAddress} to test.
176 * @return {@code true} if the prefix covers the given address.
177 */
178 public boolean contains(InetAddress address) {
179 byte[] addrBytes = (address == null) ? null : address.getAddress();
180 if (addrBytes == null || addrBytes.length != this.address.length) {
181 return false;
182 }
183 NetworkUtils.maskRawAddress(addrBytes, prefixLength);
184 return Arrays.equals(this.address, addrBytes);
185 }
186
187 /**
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900188 * Returns a string representation of this {@code IpPrefix}.
189 *
Lorenzo Colitti4b0f8e62014-09-19 01:49:05 +0900190 * @return a string such as {@code "192.0.2.0/24"} or {@code "2001:db8:1:2::/64"}.
Lorenzo Colitti8c6c2c32014-06-12 13:41:17 +0900191 */
192 public String toString() {
193 try {
194 return InetAddress.getByAddress(address).getHostAddress() + "/" + prefixLength;
195 } catch(UnknownHostException e) {
196 // Cosmic rays?
197 throw new IllegalStateException("IpPrefix with invalid address! Shouldn't happen.", e);
198 }
199 }
200
201 /**
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700202 * Implement the Parcelable interface.
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700203 */
204 public int describeContents() {
205 return 0;
206 }
207
208 /**
209 * Implement the Parcelable interface.
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700210 */
211 public void writeToParcel(Parcel dest, int flags) {
212 dest.writeByteArray(address);
213 dest.writeInt(prefixLength);
214 }
215
216 /**
217 * Implement the Parcelable interface.
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700218 */
219 public static final Creator<IpPrefix> CREATOR =
220 new Creator<IpPrefix>() {
221 public IpPrefix createFromParcel(Parcel in) {
222 byte[] address = in.createByteArray();
223 int prefixLength = in.readInt();
224 return new IpPrefix(address, prefixLength);
225 }
226
227 public IpPrefix[] newArray(int size) {
228 return new IpPrefix[size];
229 }
230 };
231}