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