Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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. |
| 23 | * <p> |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 24 | * NOTE: The stored IP address in the result IP prefix is masked to |
| 25 | * contain zeroes in all bits after the prefix length. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 26 | * </p> |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 27 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 28 | public final class IpPrefix { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 29 | // Maximum network mask length |
| 30 | public static final int MAX_INET_MASK_LENGTH = IpAddress.INET_BIT_LENGTH; |
| 31 | public static final int MAX_INET6_MASK_LENGTH = IpAddress.INET6_BIT_LENGTH; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 32 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 33 | private final IpAddress address; |
| 34 | private final short prefixLength; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 35 | |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 36 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 37 | * Constructor for given IP address, and a prefix length. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 38 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 39 | * @param address the IP address |
| 40 | * @param prefixLength the prefix length |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 41 | * @throws IllegalArgumentException if the prefix length value is invalid |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 42 | */ |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame^] | 43 | private IpPrefix(IpAddress address, int prefixLength) { |
| 44 | checkPrefixLength(address.version(), prefixLength); |
| 45 | this.address = IpAddress.makeMaskedAddress(address, prefixLength); |
| 46 | this.prefixLength = (short) prefixLength; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 50 | * Returns the IP version of the prefix. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 51 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 52 | * @return the IP version of the prefix |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 53 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 54 | public IpAddress.Version version() { |
| 55 | return address.version(); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 59 | * Returns the IP address value of the prefix. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 60 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 61 | * @return the IP address value of the prefix |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 62 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 63 | public IpAddress address() { |
| 64 | return address; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 67 | /** |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 68 | * Returns the IP address prefix length. |
| 69 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 70 | * @return the IP address prefix length |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 71 | */ |
| 72 | public int prefixLength() { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 73 | return prefixLength; |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame^] | 77 | * Converts an integer and a prefix length into an IPv4 prefix. |
| 78 | * |
| 79 | * @param address an integer representing the IPv4 address |
| 80 | * @param prefixLength the prefix length |
| 81 | * @return an IP prefix |
| 82 | * @throws IllegalArgumentException if the prefix length value is invalid |
| 83 | */ |
| 84 | public static IpPrefix valueOf(int address, int prefixLength) { |
| 85 | return new IpPrefix(IpAddress.valueOf(address), prefixLength); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Converts a byte array and a prefix length into an IP prefix. |
| 90 | * |
| 91 | * @param version the IP address version |
| 92 | * @param address the IP address value stored in network byte order |
| 93 | * @param prefixLength the prefix length |
| 94 | * @return an IP prefix |
| 95 | * @throws IllegalArgumentException if the prefix length value is invalid |
| 96 | */ |
| 97 | public static IpPrefix valueOf(IpAddress.Version version, byte[] address, |
| 98 | int prefixLength) { |
| 99 | return new IpPrefix(IpAddress.valueOf(version, address), prefixLength); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Converts an IP address and a prefix length into IP prefix. |
| 104 | * |
| 105 | * @param address the IP address |
| 106 | * @param prefixLength the prefix length |
| 107 | * @return an IP prefix |
| 108 | * @throws IllegalArgumentException if the prefix length value is invalid |
| 109 | */ |
| 110 | public static IpPrefix valueOf(IpAddress address, int prefixLength) { |
| 111 | return new IpPrefix(address, prefixLength); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Converts a CIDR (slash) notation string (e.g., "10.1.0.0/16") into an |
| 116 | * IP prefix. |
| 117 | * |
| 118 | * @param address an IP prefix in string form, e.g. "10.1.0.0/16" |
| 119 | * @return an IP prefix |
| 120 | * @throws IllegalArgumentException if the arguments are invalid |
| 121 | */ |
| 122 | public static IpPrefix valueOf(String address) { |
| 123 | final String[] parts = address.split("/"); |
| 124 | if (parts.length != 2) { |
| 125 | String msg = "Malformed IP prefix string: " + address + "." + |
| 126 | "Address must take form \"x.x.x.x/y\""; |
| 127 | throw new IllegalArgumentException(msg); |
| 128 | } |
| 129 | IpAddress ipAddress = IpAddress.valueOf(parts[0]); |
| 130 | int prefixLength = Integer.parseInt(parts[1]); |
| 131 | |
| 132 | return new IpPrefix(ipAddress, prefixLength); |
| 133 | } |
| 134 | |
| 135 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 136 | * Determines whether a given IP prefix is contained within this prefix. |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 137 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 138 | * @param other the IP prefix to test |
| 139 | * @return true if the other IP prefix is contained in this prefix, |
| 140 | * otherwise false |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 141 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 142 | public boolean contains(IpPrefix other) { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 143 | if (this.prefixLength > other.prefixLength) { |
| 144 | return false; // This prefix has smaller prefix size |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 145 | } |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 146 | |
| 147 | // |
| 148 | // Mask the other address with my prefix length. |
| 149 | // If the other prefix is within this prefix, the masked address must |
| 150 | // be same as the address of this prefix. |
| 151 | // |
| 152 | IpAddress maskedAddr = |
| 153 | IpAddress.makeMaskedAddress(other.address, this.prefixLength); |
| 154 | return this.address.equals(maskedAddr); |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 157 | /** |
| 158 | * Determines whether a given IP address is contained within this prefix. |
| 159 | * |
| 160 | * @param other the IP address to test |
| 161 | * @return true if the IP address is contained in this prefix, otherwise |
| 162 | * false |
| 163 | */ |
| 164 | public boolean contains(IpAddress other) { |
| 165 | // |
| 166 | // Mask the other address with my prefix length. |
| 167 | // If the other prefix is within this prefix, the masked address must |
| 168 | // be same as the address of this prefix. |
| 169 | // |
| 170 | IpAddress maskedAddr = |
| 171 | IpAddress.makeMaskedAddress(other, this.prefixLength); |
| 172 | return this.address.equals(maskedAddr); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 175 | @Override |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 176 | public int hashCode() { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 177 | return Objects.hash(address, prefixLength); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public boolean equals(Object obj) { |
| 182 | if (this == obj) { |
| 183 | return true; |
| 184 | } |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 185 | if ((obj == null) || (getClass() != obj.getClass())) { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 186 | return false; |
| 187 | } |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 188 | IpPrefix other = (IpPrefix) obj; |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 189 | return ((prefixLength == other.prefixLength) && |
| 190 | address.equals(other.address)); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | /* |
| 195 | * (non-Javadoc) |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 196 | * The format is "x.x.x.x/y" for IPv4 prefixes. |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 197 | * |
| 198 | * @see java.lang.Object#toString() |
| 199 | */ |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 200 | public String toString() { |
| 201 | final StringBuilder builder = new StringBuilder(); |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 202 | builder.append(address.toString()); |
| 203 | builder.append("/"); |
| 204 | builder.append(String.format("%d", prefixLength)); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 205 | return builder.toString(); |
| 206 | } |
Pavlin Radoslavov | 34c921a | 2014-11-03 15:41:22 -0800 | [diff] [blame^] | 207 | |
| 208 | /** |
| 209 | * Checks whether the prefix length is valid. |
| 210 | * |
| 211 | * @param version the IP address version |
| 212 | * @param prefixLength the prefix length value to check |
| 213 | * @throws IllegalArgumentException if the prefix length value is invalid |
| 214 | */ |
| 215 | private static void checkPrefixLength(IpAddress.Version version, |
| 216 | int prefixLength) { |
| 217 | int maxPrefixLen = 0; |
| 218 | |
| 219 | switch (version) { |
| 220 | case INET: |
| 221 | maxPrefixLen = MAX_INET_MASK_LENGTH; |
| 222 | break; |
| 223 | case INET6: |
| 224 | maxPrefixLen = MAX_INET6_MASK_LENGTH; |
| 225 | break; |
| 226 | default: |
| 227 | String msg = "Invalid IP version " + version; |
| 228 | throw new IllegalArgumentException(msg); |
| 229 | } |
| 230 | |
| 231 | if ((prefixLength < 0) || (prefixLength > maxPrefixLen)) { |
| 232 | String msg = "Invalid prefix length " + prefixLength + ". " + |
| 233 | "The value must be in the interval [0, " + |
| 234 | maxPrefixLen + "]"; |
| 235 | throw new IllegalArgumentException(msg); |
| 236 | } |
| 237 | } |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 238 | } |