Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 18 | import java.util.Objects; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 19 | |
| 20 | /** |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 21 | * A class representing an IP prefix. A prefix consists of an IP address and |
| 22 | * a subnet mask. |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 23 | * This class is immutable. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 24 | * <p> |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 25 | * NOTE: The stored IP address in the result IP prefix is masked to |
| 26 | * contain zeroes in all bits after the prefix length. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 27 | * </p> |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 28 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 29 | public class IpPrefix { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 30 | // Maximum network mask length |
| 31 | public static final int MAX_INET_MASK_LENGTH = IpAddress.INET_BIT_LENGTH; |
| 32 | public static final int MAX_INET6_MASK_LENGTH = IpAddress.INET6_BIT_LENGTH; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 33 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 34 | public static final IpPrefix MULTICAST_RANGE = IpPrefix.valueOf("224.0.0.0/4"); |
| 35 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 36 | private final IpAddress address; |
| 37 | private final short prefixLength; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 38 | |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 39 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 40 | * Constructor for given IP address, and a prefix length. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 41 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 42 | * @param address the IP address |
| 43 | * @param prefixLength the prefix length |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 44 | * @throws IllegalArgumentException if the prefix length value is invalid |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 45 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 46 | protected IpPrefix(IpAddress address, int prefixLength) { |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 47 | checkPrefixLength(address.version(), prefixLength); |
| 48 | this.address = IpAddress.makeMaskedAddress(address, prefixLength); |
| 49 | this.prefixLength = (short) prefixLength; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 53 | * Returns the IP version of the prefix. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 54 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 55 | * @return the IP version of the prefix |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 56 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 57 | public IpAddress.Version version() { |
| 58 | return address.version(); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
Pavlin Radoslavov | 34ffe72 | 2015-03-10 12:48:55 -0700 | [diff] [blame] | 62 | * Tests whether the IP version of this prefix is IPv4. |
| 63 | * |
| 64 | * @return true if the IP version of this prefix is IPv4, otherwise false. |
| 65 | */ |
| 66 | public boolean isIp4() { |
| 67 | return address.isIp4(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Tests whether the IP version of this prefix is IPv6. |
| 72 | * |
| 73 | * @return true if the IP version of this prefix is IPv6, otherwise false. |
| 74 | */ |
| 75 | public boolean isIp6() { |
| 76 | return address.isIp6(); |
| 77 | } |
| 78 | |
| 79 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 80 | * Returns the IP address value of the prefix. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 81 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 82 | * @return the IP address value of the prefix |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 83 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 84 | public IpAddress address() { |
| 85 | return address; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 88 | /** |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 89 | * Returns the IP address prefix length. |
| 90 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 91 | * @return the IP address prefix length |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 92 | */ |
| 93 | public int prefixLength() { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 94 | return prefixLength; |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
Pavlin Radoslavov | 34c8164 | 2014-11-04 16:21:38 -0800 | [diff] [blame] | 98 | * Gets the {@link Ip4Prefix} view of the IP prefix. |
| 99 | * |
| 100 | * @return the {@link Ip4Prefix} view of the IP prefix if it is IPv4, |
| 101 | * otherwise null |
| 102 | */ |
| 103 | public Ip4Prefix getIp4Prefix() { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 104 | if (!isIp4()) { |
Pavlin Radoslavov | 34c8164 | 2014-11-04 16:21:38 -0800 | [diff] [blame] | 105 | return null; |
| 106 | } |
| 107 | |
| 108 | // Return this object itself if it is already instance of Ip4Prefix |
| 109 | if (this instanceof Ip4Prefix) { |
| 110 | return (Ip4Prefix) this; |
| 111 | } |
| 112 | return Ip4Prefix.valueOf(address.getIp4Address(), prefixLength); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Gets the {@link Ip6Prefix} view of the IP prefix. |
| 117 | * |
| 118 | * @return the {@link Ip6Prefix} view of the IP prefix if it is IPv6, |
| 119 | * otherwise null |
| 120 | */ |
| 121 | public Ip6Prefix getIp6Prefix() { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 122 | if (!isIp6()) { |
Pavlin Radoslavov | 34c8164 | 2014-11-04 16:21:38 -0800 | [diff] [blame] | 123 | return null; |
| 124 | } |
| 125 | |
| 126 | // Return this object itself if it is already instance of Ip6Prefix |
| 127 | if (this instanceof Ip6Prefix) { |
| 128 | return (Ip6Prefix) this; |
| 129 | } |
| 130 | return Ip6Prefix.valueOf(address.getIp6Address(), prefixLength); |
| 131 | } |
| 132 | |
| 133 | /** |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 134 | * Converts an integer and a prefix length into an IPv4 prefix. |
| 135 | * |
| 136 | * @param address an integer representing the IPv4 address |
| 137 | * @param prefixLength the prefix length |
| 138 | * @return an IP prefix |
| 139 | * @throws IllegalArgumentException if the prefix length value is invalid |
| 140 | */ |
| 141 | public static IpPrefix valueOf(int address, int prefixLength) { |
| 142 | return new IpPrefix(IpAddress.valueOf(address), prefixLength); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Converts a byte array and a prefix length into an IP prefix. |
| 147 | * |
| 148 | * @param version the IP address version |
| 149 | * @param address the IP address value stored in network byte order |
| 150 | * @param prefixLength the prefix length |
| 151 | * @return an IP prefix |
| 152 | * @throws IllegalArgumentException if the prefix length value is invalid |
| 153 | */ |
| 154 | public static IpPrefix valueOf(IpAddress.Version version, byte[] address, |
| 155 | int prefixLength) { |
| 156 | return new IpPrefix(IpAddress.valueOf(version, address), prefixLength); |
| 157 | } |
| 158 | |
| 159 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 160 | * Converts an IP address and a prefix length into an IP prefix. |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 161 | * |
| 162 | * @param address the IP address |
| 163 | * @param prefixLength the prefix length |
| 164 | * @return an IP prefix |
| 165 | * @throws IllegalArgumentException if the prefix length value is invalid |
| 166 | */ |
| 167 | public static IpPrefix valueOf(IpAddress address, int prefixLength) { |
| 168 | return new IpPrefix(address, prefixLength); |
| 169 | } |
| 170 | |
| 171 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 172 | * Converts a CIDR (slash) notation string (e.g., "10.1.0.0/16" or |
| 173 | * "1111:2222::/64") into an IP prefix. |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 174 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 175 | * @param address an IP prefix in string form (e.g. "10.1.0.0/16" or |
| 176 | * "1111:2222::/64") |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 177 | * @return an IP prefix |
| 178 | * @throws IllegalArgumentException if the arguments are invalid |
| 179 | */ |
| 180 | public static IpPrefix valueOf(String address) { |
| 181 | final String[] parts = address.split("/"); |
| 182 | if (parts.length != 2) { |
Pavlin Radoslavov | d7b4584 | 2015-03-20 15:40:59 -0700 | [diff] [blame] | 183 | String msg = "Malformed IP prefix string: " + address + ". " + |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 184 | "Address must take form \"x.x.x.x/y\" or " + |
| 185 | "\"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/y\""; |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 186 | throw new IllegalArgumentException(msg); |
| 187 | } |
| 188 | IpAddress ipAddress = IpAddress.valueOf(parts[0]); |
| 189 | int prefixLength = Integer.parseInt(parts[1]); |
| 190 | |
| 191 | return new IpPrefix(ipAddress, prefixLength); |
| 192 | } |
| 193 | |
| 194 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 195 | * Determines whether a given IP prefix is contained within this prefix. |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 196 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 197 | * @param other the IP prefix to test |
| 198 | * @return true if the other IP prefix is contained in this prefix, |
| 199 | * otherwise false |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 200 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 201 | public boolean contains(IpPrefix other) { |
Pavlin Radoslavov | dbeab4c | 2015-02-23 09:37:49 -0800 | [diff] [blame] | 202 | if (version() != other.version()) { |
| 203 | return false; |
| 204 | } |
| 205 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 206 | if (this.prefixLength > other.prefixLength) { |
| 207 | return false; // This prefix has smaller prefix size |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 208 | } |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 209 | |
| 210 | // |
| 211 | // Mask the other address with my prefix length. |
| 212 | // If the other prefix is within this prefix, the masked address must |
| 213 | // be same as the address of this prefix. |
| 214 | // |
| 215 | IpAddress maskedAddr = |
| 216 | IpAddress.makeMaskedAddress(other.address, this.prefixLength); |
| 217 | return this.address.equals(maskedAddr); |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 220 | /** |
| 221 | * Determines whether a given IP address is contained within this prefix. |
| 222 | * |
| 223 | * @param other the IP address to test |
| 224 | * @return true if the IP address is contained in this prefix, otherwise |
| 225 | * false |
| 226 | */ |
| 227 | public boolean contains(IpAddress other) { |
Pavlin Radoslavov | dbeab4c | 2015-02-23 09:37:49 -0800 | [diff] [blame] | 228 | if (version() != other.version()) { |
| 229 | return false; |
| 230 | } |
| 231 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 232 | // |
| 233 | // Mask the other address with my prefix length. |
| 234 | // If the other prefix is within this prefix, the masked address must |
| 235 | // be same as the address of this prefix. |
| 236 | // |
| 237 | IpAddress maskedAddr = |
| 238 | IpAddress.makeMaskedAddress(other, this.prefixLength); |
| 239 | return this.address.equals(maskedAddr); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 242 | @Override |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 243 | public int hashCode() { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 244 | return Objects.hash(address, prefixLength); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public boolean equals(Object obj) { |
| 249 | if (this == obj) { |
| 250 | return true; |
| 251 | } |
Pavlin Radoslavov | 50b7067 | 2014-11-05 11:22:25 -0800 | [diff] [blame] | 252 | if ((obj == null) || (!(obj instanceof IpPrefix))) { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 253 | return false; |
| 254 | } |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 255 | IpPrefix other = (IpPrefix) obj; |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 256 | return ((prefixLength == other.prefixLength) && |
| 257 | address.equals(other.address)); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
| 261 | /* |
| 262 | * (non-Javadoc) |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 263 | * The format is "x.x.x.x/y" for IPv4 prefixes. |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 264 | * |
| 265 | * @see java.lang.Object#toString() |
| 266 | */ |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 267 | public String toString() { |
| 268 | final StringBuilder builder = new StringBuilder(); |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 269 | builder.append(address.toString()); |
| 270 | builder.append("/"); |
| 271 | builder.append(String.format("%d", prefixLength)); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 272 | return builder.toString(); |
| 273 | } |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame] | 274 | |
| 275 | /** |
| 276 | * Checks whether the prefix length is valid. |
| 277 | * |
| 278 | * @param version the IP address version |
| 279 | * @param prefixLength the prefix length value to check |
| 280 | * @throws IllegalArgumentException if the prefix length value is invalid |
| 281 | */ |
| 282 | private static void checkPrefixLength(IpAddress.Version version, |
| 283 | int prefixLength) { |
| 284 | int maxPrefixLen = 0; |
| 285 | |
| 286 | switch (version) { |
| 287 | case INET: |
| 288 | maxPrefixLen = MAX_INET_MASK_LENGTH; |
| 289 | break; |
| 290 | case INET6: |
| 291 | maxPrefixLen = MAX_INET6_MASK_LENGTH; |
| 292 | break; |
| 293 | default: |
| 294 | String msg = "Invalid IP version " + version; |
| 295 | throw new IllegalArgumentException(msg); |
| 296 | } |
| 297 | |
| 298 | if ((prefixLength < 0) || (prefixLength > maxPrefixLen)) { |
| 299 | String msg = "Invalid prefix length " + prefixLength + ". " + |
| 300 | "The value must be in the interval [0, " + |
| 301 | maxPrefixLen + "]"; |
| 302 | throw new IllegalArgumentException(msg); |
| 303 | } |
| 304 | } |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 305 | } |