blob: 7f50327464310bcc7885fee98d3dbbdbf7d4a4e3 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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 */
Charles Chanaedabfd2016-02-26 09:31:48 -080046 public static final IpPrefix IPV4_MULTICAST_PREFIX = IpPrefix.valueOf("224.0.0.0/4");
Charles Chan4ca8e602016-02-25 18:05:59 -080047 /**
48 * An IpPrefix that contains all IPv6 multicast addresses.
49 */
Charles Chanaedabfd2016-02-26 09:31:48 -080050 public static final IpPrefix IPV6_MULTICAST_PREFIX = 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 /**
Pier Ventre48ca5192016-11-28 16:52:24 -080069 * Default constructor for Kryo serialization.
70 */
71 protected IpPrefix() {
72 this.address = null;
73 this.prefixLength = 0;
74 }
75
76 /**
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070077 * Returns the IP version of the prefix.
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070078 *
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070079 * @return the IP version of the prefix
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070080 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070081 public IpAddress.Version version() {
82 return address.version();
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070083 }
84
85 /**
Pavlin Radoslavov34ffe722015-03-10 12:48:55 -070086 * Tests whether the IP version of this prefix is IPv4.
87 *
88 * @return true if the IP version of this prefix is IPv4, otherwise false.
89 */
90 public boolean isIp4() {
91 return address.isIp4();
92 }
93
94 /**
95 * Tests whether the IP version of this prefix is IPv6.
96 *
97 * @return true if the IP version of this prefix is IPv6, otherwise false.
98 */
99 public boolean isIp6() {
100 return address.isIp6();
101 }
102
103 /**
Charles Chanaedabfd2016-02-26 09:31:48 -0800104 * Check if this IP prefix is a multicast prefix.
105 *
106 * @return true if this prefix a multicast prefix
107 */
108 public boolean isMulticast() {
109 return isIp4() ?
110 IPV4_MULTICAST_PREFIX.contains(this.getIp4Prefix()) :
111 IPV6_MULTICAST_PREFIX.contains(this.getIp6Prefix());
112 }
113
114 /**
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700115 * Returns the IP address value of the prefix.
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700116 *
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700117 * @return the IP address value of the prefix
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700118 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700119 public IpAddress address() {
120 return address;
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700121 }
122
Ayaka Koshibe16698a32014-09-13 22:19:02 -0700123 /**
Yuta HIGUCHI10681f62014-09-21 17:49:46 -0700124 * Returns the IP address prefix length.
125 *
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700126 * @return the IP address prefix length
Yuta HIGUCHI10681f62014-09-21 17:49:46 -0700127 */
128 public int prefixLength() {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700129 return prefixLength;
Yuta HIGUCHI10681f62014-09-21 17:49:46 -0700130 }
131
132 /**
Pavlin Radoslavov34c81642014-11-04 16:21:38 -0800133 * Gets the {@link Ip4Prefix} view of the IP prefix.
134 *
135 * @return the {@link Ip4Prefix} view of the IP prefix if it is IPv4,
136 * otherwise null
137 */
138 public Ip4Prefix getIp4Prefix() {
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700139 if (!isIp4()) {
Pavlin Radoslavov34c81642014-11-04 16:21:38 -0800140 return null;
141 }
142
143 // Return this object itself if it is already instance of Ip4Prefix
144 if (this instanceof Ip4Prefix) {
145 return (Ip4Prefix) this;
146 }
147 return Ip4Prefix.valueOf(address.getIp4Address(), prefixLength);
148 }
149
150 /**
151 * Gets the {@link Ip6Prefix} view of the IP prefix.
152 *
153 * @return the {@link Ip6Prefix} view of the IP prefix if it is IPv6,
154 * otherwise null
155 */
156 public Ip6Prefix getIp6Prefix() {
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700157 if (!isIp6()) {
Pavlin Radoslavov34c81642014-11-04 16:21:38 -0800158 return null;
159 }
160
161 // Return this object itself if it is already instance of Ip6Prefix
162 if (this instanceof Ip6Prefix) {
163 return (Ip6Prefix) this;
164 }
165 return Ip6Prefix.valueOf(address.getIp6Address(), prefixLength);
166 }
167
168 /**
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800169 * Converts an integer and a prefix length into an IPv4 prefix.
170 *
171 * @param address an integer representing the IPv4 address
172 * @param prefixLength the prefix length
173 * @return an IP prefix
174 * @throws IllegalArgumentException if the prefix length value is invalid
175 */
176 public static IpPrefix valueOf(int address, int prefixLength) {
177 return new IpPrefix(IpAddress.valueOf(address), prefixLength);
178 }
179
180 /**
181 * Converts a byte array and a prefix length into an IP prefix.
182 *
183 * @param version the IP address version
184 * @param address the IP address value stored in network byte order
185 * @param prefixLength the prefix length
186 * @return an IP prefix
187 * @throws IllegalArgumentException if the prefix length value is invalid
188 */
189 public static IpPrefix valueOf(IpAddress.Version version, byte[] address,
190 int prefixLength) {
191 return new IpPrefix(IpAddress.valueOf(version, address), prefixLength);
192 }
193
194 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800195 * Converts an IP address and a prefix length into an IP prefix.
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800196 *
197 * @param address the IP address
198 * @param prefixLength the prefix length
199 * @return an IP prefix
200 * @throws IllegalArgumentException if the prefix length value is invalid
201 */
202 public static IpPrefix valueOf(IpAddress address, int prefixLength) {
203 return new IpPrefix(address, prefixLength);
204 }
205
206 /**
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800207 * Converts a CIDR (slash) notation string (e.g., "10.1.0.0/16" or
208 * "1111:2222::/64") into an IP prefix.
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800209 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800210 * @param address an IP prefix in string form (e.g. "10.1.0.0/16" or
211 * "1111:2222::/64")
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800212 * @return an IP prefix
213 * @throws IllegalArgumentException if the arguments are invalid
214 */
215 public static IpPrefix valueOf(String address) {
216 final String[] parts = address.split("/");
217 if (parts.length != 2) {
Pavlin Radoslavovd7b45842015-03-20 15:40:59 -0700218 String msg = "Malformed IP prefix string: " + address + ". " +
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800219 "Address must take form \"x.x.x.x/y\" or " +
220 "\"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/y\"";
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800221 throw new IllegalArgumentException(msg);
222 }
223 IpAddress ipAddress = IpAddress.valueOf(parts[0]);
224 int prefixLength = Integer.parseInt(parts[1]);
225
226 return new IpPrefix(ipAddress, prefixLength);
227 }
228
229 /**
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700230 * Determines whether a given IP prefix is contained within this prefix.
Ayaka Koshibe16698a32014-09-13 22:19:02 -0700231 *
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700232 * @param other the IP prefix to test
233 * @return true if the other IP prefix is contained in this prefix,
234 * otherwise false
Jonathan Hartb7a2ac32014-09-19 10:42:27 -0700235 */
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700236 public boolean contains(IpPrefix other) {
Pavlin Radoslavovdbeab4c2015-02-23 09:37:49 -0800237 if (version() != other.version()) {
238 return false;
239 }
240
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700241 if (this.prefixLength > other.prefixLength) {
242 return false; // This prefix has smaller prefix size
Jonathan Hartb7a2ac32014-09-19 10:42:27 -0700243 }
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700244
245 //
246 // Mask the other address with my prefix length.
247 // If the other prefix is within this prefix, the masked address must
248 // be same as the address of this prefix.
249 //
250 IpAddress maskedAddr =
251 IpAddress.makeMaskedAddress(other.address, this.prefixLength);
252 return this.address.equals(maskedAddr);
Jonathan Hartb7a2ac32014-09-19 10:42:27 -0700253 }
254
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700255 /**
256 * Determines whether a given IP address is contained within this prefix.
257 *
258 * @param other the IP address to test
259 * @return true if the IP address is contained in this prefix, otherwise
260 * false
261 */
262 public boolean contains(IpAddress other) {
Pavlin Radoslavovdbeab4c2015-02-23 09:37:49 -0800263 if (version() != other.version()) {
264 return false;
265 }
266
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700267 //
268 // Mask the other address with my prefix length.
269 // If the other prefix is within this prefix, the masked address must
270 // be same as the address of this prefix.
271 //
272 IpAddress maskedAddr =
273 IpAddress.makeMaskedAddress(other, this.prefixLength);
274 return this.address.equals(maskedAddr);
Jonathan Hart70da5122014-10-01 16:37:42 -0700275 }
276
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700277 @Override
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -0700278 public int hashCode() {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700279 return Objects.hash(address, prefixLength);
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -0700280 }
281
282 @Override
283 public boolean equals(Object obj) {
284 if (this == obj) {
285 return true;
286 }
Pavlin Radoslavov50b70672014-11-05 11:22:25 -0800287 if ((obj == null) || (!(obj instanceof IpPrefix))) {
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -0700288 return false;
289 }
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700290 IpPrefix other = (IpPrefix) obj;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700291 return ((prefixLength == other.prefixLength) &&
292 address.equals(other.address));
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -0700293 }
294
295 @Override
296 /*
297 * (non-Javadoc)
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700298 * The format is "x.x.x.x/y" for IPv4 prefixes.
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -0700299 *
300 * @see java.lang.Object#toString()
301 */
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700302 public String toString() {
303 final StringBuilder builder = new StringBuilder();
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700304 builder.append(address.toString());
305 builder.append("/");
306 builder.append(String.format("%d", prefixLength));
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700307 return builder.toString();
308 }
Pavlin Radoslavov34c921a2014-11-03 15:41:22 -0800309
310 /**
311 * Checks whether the prefix length is valid.
312 *
313 * @param version the IP address version
314 * @param prefixLength the prefix length value to check
315 * @throws IllegalArgumentException if the prefix length value is invalid
316 */
317 private static void checkPrefixLength(IpAddress.Version version,
318 int prefixLength) {
319 int maxPrefixLen = 0;
320
321 switch (version) {
322 case INET:
323 maxPrefixLen = MAX_INET_MASK_LENGTH;
324 break;
325 case INET6:
326 maxPrefixLen = MAX_INET6_MASK_LENGTH;
327 break;
328 default:
329 String msg = "Invalid IP version " + version;
330 throw new IllegalArgumentException(msg);
331 }
332
333 if ((prefixLength < 0) || (prefixLength > maxPrefixLen)) {
334 String msg = "Invalid prefix length " + prefixLength + ". " +
335 "The value must be in the interval [0, " +
336 maxPrefixLen + "]";
337 throw new IllegalArgumentException(msg);
338 }
339 }
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -0700340}