Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 1 | package org.onlab.packet; |
| 2 | |
| 3 | import java.util.Arrays; |
| 4 | |
| 5 | /** |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 6 | * A class representing an IPv4 prefix. |
| 7 | * <p/> |
| 8 | * A prefix consists of an IP address and a subnet mask. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 9 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 10 | public final class IpPrefix { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 11 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 12 | // TODO a comparator for netmasks? E.g. for sorting by prefix match order. |
| 13 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 14 | //IP Versions |
| 15 | public enum Version { INET, INET6 }; |
| 16 | |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 17 | //lengths of address, in bytes |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 18 | public static final int INET_LEN = 4; |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 19 | public static final int INET6_LEN = 16; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 20 | |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 21 | //maximum CIDR value |
| 22 | public static final int MAX_INET_MASK = 32; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 23 | //no mask (no network), e.g. a simple address |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 24 | public static final int DEFAULT_MASK = 0; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 25 | |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Default value indicating an unspecified address. |
| 28 | */ |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 29 | static final byte[] ANY = new byte [] {0, 0, 0, 0}; |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 30 | |
| 31 | protected Version version; |
| 32 | |
| 33 | protected byte[] octets; |
| 34 | protected int netmask; |
| 35 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 36 | private IpPrefix(Version ver, byte[] octets, int netmask) { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 37 | this.version = ver; |
| 38 | this.octets = Arrays.copyOf(octets, INET_LEN); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 39 | this.netmask = netmask; |
| 40 | } |
| 41 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 42 | private IpPrefix(Version ver, byte[] octets) { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 43 | this.version = ver; |
| 44 | this.octets = Arrays.copyOf(octets, INET_LEN); |
| 45 | this.netmask = DEFAULT_MASK; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Converts a byte array into an IP address. |
| 50 | * |
| 51 | * @param address a byte array |
| 52 | * @return an IP address |
| 53 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 54 | public static IpPrefix valueOf(byte [] address) { |
| 55 | return new IpPrefix(Version.INET, address); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 59 | * Converts a byte array into an IP address. |
| 60 | * |
| 61 | * @param address a byte array |
| 62 | * @param netmask the CIDR value subnet mask |
| 63 | * @return an IP address |
| 64 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 65 | public static IpPrefix valueOf(byte [] address, int netmask) { |
| 66 | return new IpPrefix(Version.INET, address, netmask); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Helper to convert an integer into a byte array. |
| 71 | * |
| 72 | * @param address the integer to convert |
| 73 | * @return a byte array |
| 74 | */ |
| 75 | private static byte [] bytes(int address) { |
| 76 | byte [] bytes = new byte [INET_LEN]; |
| 77 | for (int i = 0; i < INET_LEN; i++) { |
| 78 | bytes[i] = (byte) ((address >> (INET_LEN - (i + 1)) * 8) & 0xff); |
| 79 | } |
| 80 | |
| 81 | return bytes; |
| 82 | } |
| 83 | |
| 84 | /** |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 85 | * Converts an integer into an IPv4 address. |
| 86 | * |
| 87 | * @param address an integer representing an IP value |
| 88 | * @return an IP address |
| 89 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 90 | public static IpPrefix valueOf(int address) { |
| 91 | return new IpPrefix(Version.INET, bytes(address)); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 95 | * Converts an integer into an IPv4 address. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 96 | * |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 97 | * @param address an integer representing an IP value |
| 98 | * @param netmask the CIDR value subnet mask |
| 99 | * @return an IP address |
| 100 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 101 | public static IpPrefix valueOf(int address, int netmask) { |
| 102 | return new IpPrefix(Version.INET, bytes(address), netmask); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Converts a dotted-decimal string (x.x.x.x) into an IPv4 address. The |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 107 | * string can also be in CIDR (slash) notation. If the netmask is omitted, |
| 108 | * it will be set to DEFAULT_MASK (0). |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 109 | * |
| 110 | * @param address a IP address in string form, e.g. "10.0.0.1", "10.0.0.1/24" |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 111 | * @return an IP address |
| 112 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 113 | public static IpPrefix valueOf(String address) { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 114 | |
| 115 | final String [] parts = address.split("\\/"); |
| 116 | if (parts.length > 2) { |
| 117 | throw new IllegalArgumentException("Malformed IP address string; " |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 118 | + "Address must take form \"x.x.x.x\" or \"x.x.x.x/y\""); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int mask = DEFAULT_MASK; |
| 122 | if (parts.length == 2) { |
| 123 | mask = Integer.valueOf(parts[1]); |
| 124 | if (mask > MAX_INET_MASK) { |
| 125 | throw new IllegalArgumentException( |
| 126 | "Value of subnet mask cannot exceed " |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 127 | + MAX_INET_MASK); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | final String [] net = parts[0].split("\\."); |
| 132 | if (net.length != INET_LEN) { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 133 | throw new IllegalArgumentException("Malformed IP address string; " |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 134 | + "Address must have four decimal values separated by dots (.)"); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 135 | } |
| 136 | final byte [] bytes = new byte[INET_LEN]; |
| 137 | for (int i = 0; i < INET_LEN; i++) { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 138 | bytes[i] = (byte) Short.parseShort(net[i], 10); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 139 | } |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 140 | return new IpPrefix(Version.INET, bytes, mask); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns the IP version of this address. |
| 145 | * |
| 146 | * @return the version |
| 147 | */ |
| 148 | public Version version() { |
| 149 | return this.version; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns the IP address as a byte array. |
| 154 | * |
| 155 | * @return a byte array |
| 156 | */ |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 157 | public byte[] toOctets() { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 158 | return Arrays.copyOf(this.octets, INET_LEN); |
| 159 | } |
| 160 | |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 161 | /** |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 162 | * Returns the IP address prefix length. |
| 163 | * |
| 164 | * @return prefix length |
| 165 | */ |
| 166 | public int prefixLength() { |
| 167 | return netmask; |
| 168 | } |
| 169 | |
| 170 | /** |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 171 | * Returns the integral value of this IP address. |
| 172 | * |
| 173 | * @return the IP address's value as an integer |
| 174 | */ |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 175 | public int toInt() { |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 176 | int address = 0; |
| 177 | for (int i = 0; i < INET_LEN; i++) { |
| 178 | address |= octets[i] << ((INET_LEN - (i + 1)) * 8); |
| 179 | } |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 180 | return address; |
| 181 | } |
| 182 | |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Helper for computing the mask value from CIDR. |
| 185 | * |
| 186 | * @return an integer bitmask |
| 187 | */ |
| 188 | private int mask() { |
| 189 | int shift = MAX_INET_MASK - this.netmask; |
| 190 | return ((Integer.MAX_VALUE >>> (shift - 1)) << shift); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Returns the subnet mask in IpAddress form. The netmask value for |
| 195 | * the returned IpAddress is 0, as the address itself is a mask. |
| 196 | * |
| 197 | * @return the subnet mask |
| 198 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 199 | public IpPrefix netmask() { |
| 200 | return new IpPrefix(Version.INET, bytes(mask())); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Returns the network portion of this address as an IpAddress. |
| 205 | * The netmask of the returned IpAddress is the current mask. If this |
| 206 | * address doesn't have a mask, this returns an all-0 IpAddress. |
| 207 | * |
| 208 | * @return the network address or null |
| 209 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 210 | public IpPrefix network() { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 211 | if (netmask == DEFAULT_MASK) { |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 212 | return new IpPrefix(version, ANY, DEFAULT_MASK); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | byte [] net = new byte [4]; |
| 216 | byte [] mask = bytes(mask()); |
| 217 | for (int i = 0; i < INET_LEN; i++) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 218 | net[i] = (byte) (octets[i] & mask[i]); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 219 | } |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 220 | return new IpPrefix(version, net, netmask); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Returns the host portion of the IPAddress, as an IPAddress. |
| 225 | * The netmask of the returned IpAddress is the current mask. If this |
| 226 | * address doesn't have a mask, this returns a copy of the current |
| 227 | * address. |
| 228 | * |
| 229 | * @return the host address |
| 230 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 231 | public IpPrefix host() { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 232 | if (netmask == DEFAULT_MASK) { |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 233 | new IpPrefix(version, octets, netmask); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | byte [] host = new byte [INET_LEN]; |
| 237 | byte [] mask = bytes(mask()); |
| 238 | for (int i = 0; i < INET_LEN; i++) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 239 | host[i] = (byte) (octets[i] & ~mask[i]); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 240 | } |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 241 | return new IpPrefix(version, host, netmask); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 242 | } |
| 243 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 244 | public boolean isMasked() { |
| 245 | return mask() != 0; |
| 246 | } |
| 247 | |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 248 | /** |
| 249 | * Determines whether a given address is contained within this IpAddress' |
| 250 | * network. |
| 251 | * |
| 252 | * @param other another IP address that could be contained in this network |
| 253 | * @return true if the other IP address is contained in this address' |
| 254 | * network, otherwise false |
| 255 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 256 | public boolean contains(IpPrefix other) { |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 257 | if (this.netmask <= other.netmask) { |
| 258 | // Special case where they're both /32 addresses |
| 259 | if (this.netmask == MAX_INET_MASK) { |
| 260 | return Arrays.equals(octets, other.octets); |
| 261 | } |
| 262 | |
| 263 | // Mask the other address with our network mask |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 264 | IpPrefix otherMasked = |
| 265 | IpPrefix.valueOf(other.octets, netmask).network(); |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 266 | |
| 267 | return network().equals(otherMasked); |
| 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 272 | @Override |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 273 | public int hashCode() { |
| 274 | final int prime = 31; |
| 275 | int result = 1; |
| 276 | result = prime * result + netmask; |
| 277 | result = prime * result + Arrays.hashCode(octets); |
| 278 | result = prime * result + ((version == null) ? 0 : version.hashCode()); |
| 279 | return result; |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public boolean equals(Object obj) { |
| 284 | if (this == obj) { |
| 285 | return true; |
| 286 | } |
| 287 | if (obj == null) { |
| 288 | return false; |
| 289 | } |
| 290 | if (getClass() != obj.getClass()) { |
| 291 | return false; |
| 292 | } |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 293 | IpPrefix other = (IpPrefix) obj; |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 294 | if (netmask != other.netmask) { |
| 295 | return false; |
| 296 | } |
| 297 | if (!Arrays.equals(octets, other.octets)) { |
| 298 | return false; |
| 299 | } |
| 300 | if (version != other.version) { |
| 301 | return false; |
| 302 | } |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | /* |
| 308 | * (non-Javadoc) |
| 309 | * format is "x.x.x.x" for non-masked (netmask 0) addresses, |
| 310 | * and "x.x.x.x/y" for masked addresses. |
| 311 | * |
| 312 | * @see java.lang.Object#toString() |
| 313 | */ |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 314 | public String toString() { |
| 315 | final StringBuilder builder = new StringBuilder(); |
| 316 | for (final byte b : this.octets) { |
| 317 | if (builder.length() > 0) { |
| 318 | builder.append("."); |
| 319 | } |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 320 | builder.append(String.format("%d", b & 0xff)); |
| 321 | } |
| 322 | if (netmask != DEFAULT_MASK) { |
| 323 | builder.append("/"); |
| 324 | builder.append(String.format("%d", netmask)); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 325 | } |
| 326 | return builder.toString(); |
| 327 | } |
| 328 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 329 | } |