blob: 78120458a6e879383c360dd8382eaa62097a5257 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070016package org.onlab.packet;
17
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070018import java.util.Objects;
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070019
20/**
Thomas Vachuska4b420772014-10-30 16:46:17 -070021 * A class representing an IP prefix. A prefix consists of an IP address and
22 * a subnet mask.
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080023 * This class is immutable.
Thomas Vachuska4b420772014-10-30 16:46:17 -070024 * <p>
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070025 * NOTE: The stored IP address in the result IP prefix is masked to
26 * contain zeroes in all bits after the prefix length.
Thomas Vachuska4b420772014-10-30 16:46:17 -070027 * </p>
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070028 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080029public class IpPrefix {
Charles Chan4ca8e602016-02-25 18:05:59 -080030 /**
31 * Longest IPv4 network prefix.
32 */
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070033 public static final int MAX_INET_MASK_LENGTH = IpAddress.INET_BIT_LENGTH;
Charles Chan4ca8e602016-02-25 18:05:59 -080034 /**
35 * Longest IPv6 network prefix.
36 */
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070037 public static final int MAX_INET6_MASK_LENGTH = IpAddress.INET6_BIT_LENGTH;
Charles Chan4ca8e602016-02-25 18:05:59 -080038 /**
39 * An IpPrefix that contains all IPv4 multicast addresses.
40 */
41 @Deprecated
Jonathan Hart7f4bc522016-02-20 11:32:43 -080042 public static final IpPrefix MULTICAST_RANGE = IpPrefix.valueOf("224.0.0.0/4");
Charles Chan4ca8e602016-02-25 18:05:59 -080043 /**
44 * An IpPrefix that contains all IPv4 multicast addresses.
45 */
46 public static final IpPrefix IPV4_MULTICAST_RANGE = IpPrefix.valueOf("224.0.0.0/4");
47 /**
48 * An IpPrefix that contains all IPv6 multicast addresses.
49 */
50 public static final IpPrefix IPV6_MULTICAST_RANGE = IpPrefix.valueOf("ff00::/8");
Jonathan Hart7f4bc522016-02-20 11:32:43 -080051
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070052 private final IpAddress address;
53 private final short prefixLength;
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070054
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070055 /**
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070056 * Constructor for given IP address, and a prefix length.
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070057 *
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070058 * @param address the IP address
59 * @param prefixLength the prefix length
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070060 * @throws IllegalArgumentException if the prefix length value is invalid
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070061 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080062 protected IpPrefix(IpAddress address, int prefixLength) {
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -080063 checkPrefixLength(address.version(), prefixLength);
64 this.address = IpAddress.makeMaskedAddress(address, prefixLength);
65 this.prefixLength = (short) prefixLength;
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070066 }
67
68 /**
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070069 * Returns the IP version of the prefix.
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070070 *
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070071 * @return the IP version of the prefix
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070072 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070073 public IpAddress.Version version() {
74 return address.version();
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070075 }
76
77 /**
Pavlin Radoslavov34ffe722015-03-10 12:48:55 -070078 * Tests whether the IP version of this prefix is IPv4.
79 *
80 * @return true if the IP version of this prefix is IPv4, otherwise false.
81 */
82 public boolean isIp4() {
83 return address.isIp4();
84 }
85
86 /**
87 * Tests whether the IP version of this prefix is IPv6.
88 *
89 * @return true if the IP version of this prefix is IPv6, otherwise false.
90 */
91 public boolean isIp6() {
92 return address.isIp6();
93 }
94
95 /**
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070096 * Returns the IP address value of the prefix.
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070097 *
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070098 * @return the IP address value of the prefix
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070099 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700100 public IpAddress address() {
101 return address;
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700102 }
103
Ayaka Koshibe16698a32014-09-13 22:19:02 -0700104 /**
Yuta HIGUCHI10681f62014-09-21 17:49:46 -0700105 * Returns the IP address prefix length.
106 *
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700107 * @return the IP address prefix length
Yuta HIGUCHI10681f62014-09-21 17:49:46 -0700108 */
109 public int prefixLength() {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700110 return prefixLength;
Yuta HIGUCHI10681f62014-09-21 17:49:46 -0700111 }
112
113 /**
Pavlin Radoslavov34c81642014-11-04 16:21:38 -0800114 * Gets the {@link Ip4Prefix} view of the IP prefix.
115 *
116 * @return the {@link Ip4Prefix} view of the IP prefix if it is IPv4,
117 * otherwise null
118 */
119 public Ip4Prefix getIp4Prefix() {
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700120 if (!isIp4()) {
Pavlin Radoslavov34c81642014-11-04 16:21:38 -0800121 return null;
122 }
123
124 // Return this object itself if it is already instance of Ip4Prefix
125 if (this instanceof Ip4Prefix) {
126 return (Ip4Prefix) this;
127 }
128 return Ip4Prefix.valueOf(address.getIp4Address(), prefixLength);
129 }
130
131 /**
132 * Gets the {@link Ip6Prefix} view of the IP prefix.
133 *
134 * @return the {@link Ip6Prefix} view of the IP prefix if it is IPv6,
135 * otherwise null
136 */
137 public Ip6Prefix getIp6Prefix() {
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700138 if (!isIp6()) {
Pavlin Radoslavov34c81642014-11-04 16:21:38 -0800139 return null;
140 }
141
142 // Return this object itself if it is already instance of Ip6Prefix
143 if (this instanceof Ip6Prefix) {
144 return (Ip6Prefix) this;
145 }
146 return Ip6Prefix.valueOf(address.getIp6Address(), prefixLength);
147 }
148
149 /**
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800150 * Converts an integer and a prefix length into an IPv4 prefix.
151 *
152 * @param address an integer representing the IPv4 address
153 * @param prefixLength the prefix length
154 * @return an IP prefix
155 * @throws IllegalArgumentException if the prefix length value is invalid
156 */
157 public static IpPrefix valueOf(int address, int prefixLength) {
158 return new IpPrefix(IpAddress.valueOf(address), prefixLength);
159 }
160
161 /**
162 * Converts a byte array and a prefix length into an IP prefix.
163 *
164 * @param version the IP address version
165 * @param address the IP address value stored in network byte order
166 * @param prefixLength the prefix length
167 * @return an IP prefix
168 * @throws IllegalArgumentException if the prefix length value is invalid
169 */
170 public static IpPrefix valueOf(IpAddress.Version version, byte[] address,
171 int prefixLength) {
172 return new IpPrefix(IpAddress.valueOf(version, address), prefixLength);
173 }
174
175 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800176 * Converts an IP address and a prefix length into an IP prefix.
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800177 *
178 * @param address the IP address
179 * @param prefixLength the prefix length
180 * @return an IP prefix
181 * @throws IllegalArgumentException if the prefix length value is invalid
182 */
183 public static IpPrefix valueOf(IpAddress address, int prefixLength) {
184 return new IpPrefix(address, prefixLength);
185 }
186
187 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800188 * Converts a CIDR (slash) notation string (e.g., "10.1.0.0/16" or
189 * "1111:2222::/64") into an IP prefix.
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800190 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800191 * @param address an IP prefix in string form (e.g. "10.1.0.0/16" or
192 * "1111:2222::/64")
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800193 * @return an IP prefix
194 * @throws IllegalArgumentException if the arguments are invalid
195 */
196 public static IpPrefix valueOf(String address) {
197 final String[] parts = address.split("/");
198 if (parts.length != 2) {
Pavlin Radoslavovd7b45842015-03-20 15:40:59 -0700199 String msg = "Malformed IP prefix string: " + address + ". " +
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800200 "Address must take form \"x.x.x.x/y\" or " +
201 "\"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/y\"";
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800202 throw new IllegalArgumentException(msg);
203 }
204 IpAddress ipAddress = IpAddress.valueOf(parts[0]);
205 int prefixLength = Integer.parseInt(parts[1]);
206
207 return new IpPrefix(ipAddress, prefixLength);
208 }
209
210 /**
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700211 * Determines whether a given IP prefix is contained within this prefix.
Ayaka Koshibe16698a32014-09-13 22:19:02 -0700212 *
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700213 * @param other the IP prefix to test
214 * @return true if the other IP prefix is contained in this prefix,
215 * otherwise false
Jonathan Hartb7a2ac32014-09-19 10:42:27 -0700216 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700217 public boolean contains(IpPrefix other) {
Pavlin Radoslavovdbeab4c2015-02-23 09:37:49 -0800218 if (version() != other.version()) {
219 return false;
220 }
221
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700222 if (this.prefixLength > other.prefixLength) {
223 return false; // This prefix has smaller prefix size
Jonathan Hartb7a2ac32014-09-19 10:42:27 -0700224 }
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700225
226 //
227 // Mask the other address with my prefix length.
228 // If the other prefix is within this prefix, the masked address must
229 // be same as the address of this prefix.
230 //
231 IpAddress maskedAddr =
232 IpAddress.makeMaskedAddress(other.address, this.prefixLength);
233 return this.address.equals(maskedAddr);
Jonathan Hartb7a2ac32014-09-19 10:42:27 -0700234 }
235
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700236 /**
237 * Determines whether a given IP address is contained within this prefix.
238 *
239 * @param other the IP address to test
240 * @return true if the IP address is contained in this prefix, otherwise
241 * false
242 */
243 public boolean contains(IpAddress other) {
Pavlin Radoslavovdbeab4c2015-02-23 09:37:49 -0800244 if (version() != other.version()) {
245 return false;
246 }
247
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700248 //
249 // Mask the other address with my prefix length.
250 // If the other prefix is within this prefix, the masked address must
251 // be same as the address of this prefix.
252 //
253 IpAddress maskedAddr =
254 IpAddress.makeMaskedAddress(other, this.prefixLength);
255 return this.address.equals(maskedAddr);
Jonathan Hart70da5122014-10-01 16:37:42 -0700256 }
257
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700258 @Override
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -0700259 public int hashCode() {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700260 return Objects.hash(address, prefixLength);
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -0700261 }
262
263 @Override
264 public boolean equals(Object obj) {
265 if (this == obj) {
266 return true;
267 }
Pavlin Radoslavov50b70672014-11-05 11:22:25 -0800268 if ((obj == null) || (!(obj instanceof IpPrefix))) {
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -0700269 return false;
270 }
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700271 IpPrefix other = (IpPrefix) obj;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700272 return ((prefixLength == other.prefixLength) &&
273 address.equals(other.address));
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -0700274 }
275
276 @Override
277 /*
278 * (non-Javadoc)
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700279 * The format is "x.x.x.x/y" for IPv4 prefixes.
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -0700280 *
281 * @see java.lang.Object#toString()
282 */
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700283 public String toString() {
284 final StringBuilder builder = new StringBuilder();
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700285 builder.append(address.toString());
286 builder.append("/");
287 builder.append(String.format("%d", prefixLength));
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700288 return builder.toString();
289 }
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800290
291 /**
292 * Checks whether the prefix length is valid.
293 *
294 * @param version the IP address version
295 * @param prefixLength the prefix length value to check
296 * @throws IllegalArgumentException if the prefix length value is invalid
297 */
298 private static void checkPrefixLength(IpAddress.Version version,
299 int prefixLength) {
300 int maxPrefixLen = 0;
301
302 switch (version) {
303 case INET:
304 maxPrefixLen = MAX_INET_MASK_LENGTH;
305 break;
306 case INET6:
307 maxPrefixLen = MAX_INET6_MASK_LENGTH;
308 break;
309 default:
310 String msg = "Invalid IP version " + version;
311 throw new IllegalArgumentException(msg);
312 }
313
314 if ((prefixLength < 0) || (prefixLength > maxPrefixLen)) {
315 String msg = "Invalid prefix length " + prefixLength + ". " +
316 "The value must be in the interval [0, " +
317 maxPrefixLen + "]";
318 throw new IllegalArgumentException(msg);
319 }
320 }
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700321}