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