Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | */ |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
Pier Ventre | 48ca519 | 2016-11-28 16:52:24 -0800 | [diff] [blame] | 18 | import com.google.common.net.InetAddresses; |
| 19 | import com.google.common.primitives.UnsignedBytes; |
| 20 | |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 21 | import java.net.Inet4Address; |
| 22 | import java.net.Inet6Address; |
Pier Ventre | 48ca519 | 2016-11-28 16:52:24 -0800 | [diff] [blame] | 23 | import java.net.InetAddress; |
| 24 | import java.net.UnknownHostException; |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 25 | import java.nio.ByteBuffer; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 26 | import java.util.Arrays; |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 27 | import java.util.Objects; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 28 | |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 31 | * A class representing an IP address. |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 32 | * This class is immutable. |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 33 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 34 | public class IpAddress implements Comparable<IpAddress> { |
Aaron Kruglikov | afdf4de | 2015-06-24 09:28:22 -0700 | [diff] [blame] | 35 | private static final int BIT_MASK = 0x000000ff; |
| 36 | |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 37 | // IP Versions |
Jon Hall | 8c7b06a | 2017-02-22 13:37:33 -0800 | [diff] [blame] | 38 | public enum Version { INET, INET6 } |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 39 | |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 40 | // lengths of address, in bytes |
| 41 | public static final int INET_BYTE_LENGTH = 4; |
| 42 | public static final int INET_BIT_LENGTH = INET_BYTE_LENGTH * Byte.SIZE; |
| 43 | public static final int INET6_BYTE_LENGTH = 16; |
| 44 | public static final int INET6_BIT_LENGTH = INET6_BYTE_LENGTH * Byte.SIZE; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 45 | |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 46 | private final Version version; |
| 47 | private final byte[] octets; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 50 | * Constructor for given IP address version and address octets. |
| 51 | * |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 52 | * @param version the IP address version |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 53 | * @param value the IP address value stored in network byte order |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 54 | * (i.e., the most significant byte first) |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 55 | * @throws IllegalArgumentException if the arguments are invalid |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 56 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 57 | protected IpAddress(Version version, byte[] value) { |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 58 | checkArguments(version, value, 0); |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 59 | this.version = version; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 60 | switch (version) { |
| 61 | case INET: |
| 62 | this.octets = Arrays.copyOf(value, INET_BYTE_LENGTH); |
| 63 | break; |
| 64 | case INET6: |
| 65 | this.octets = Arrays.copyOf(value, INET6_BYTE_LENGTH); |
| 66 | break; |
| 67 | default: |
| 68 | // Should not be reached |
| 69 | this.octets = null; |
| 70 | break; |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 71 | } |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Pier Ventre | 48ca519 | 2016-11-28 16:52:24 -0800 | [diff] [blame] | 75 | * Default constructor for Kryo serialization. |
| 76 | */ |
| 77 | protected IpAddress() { |
| 78 | this.version = null; |
| 79 | this.octets = null; |
| 80 | } |
| 81 | |
| 82 | /** |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 83 | * Returns the IP version of this address. |
| 84 | * |
| 85 | * @return the version |
| 86 | */ |
| 87 | public Version version() { |
| 88 | return this.version; |
| 89 | } |
| 90 | |
| 91 | /** |
Pavlin Radoslavov | 34ffe72 | 2015-03-10 12:48:55 -0700 | [diff] [blame] | 92 | * Tests whether the IP version of this address is IPv4. |
| 93 | * |
| 94 | * @return true if the IP version of this address is IPv4, otherwise false. |
| 95 | */ |
| 96 | public boolean isIp4() { |
| 97 | return (version() == Ip4Address.VERSION); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Tests whether the IP version of this address is IPv6. |
| 102 | * |
| 103 | * @return true if the IP version of this address is IPv6, otherwise false. |
| 104 | */ |
| 105 | public boolean isIp6() { |
| 106 | return (version() == Ip6Address.VERSION); |
| 107 | } |
| 108 | |
| 109 | /** |
Pavlin Radoslavov | 34c8164 | 2014-11-04 16:21:38 -0800 | [diff] [blame] | 110 | * Gets the {@link Ip4Address} view of the IP address. |
| 111 | * |
| 112 | * @return the {@link Ip4Address} view of the IP address if it is IPv4, |
| 113 | * otherwise null |
| 114 | */ |
| 115 | public Ip4Address getIp4Address() { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 116 | if (!isIp4()) { |
Pavlin Radoslavov | 34c8164 | 2014-11-04 16:21:38 -0800 | [diff] [blame] | 117 | return null; |
| 118 | } |
| 119 | |
| 120 | // Return this object itself if it is already instance of Ip4Address |
| 121 | if (this instanceof Ip4Address) { |
| 122 | return (Ip4Address) this; |
| 123 | } |
| 124 | return Ip4Address.valueOf(octets); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Gets the {@link Ip6Address} view of the IP address. |
| 129 | * |
| 130 | * @return the {@link Ip6Address} view of the IP address if it is IPv6, |
| 131 | * otherwise null |
| 132 | */ |
| 133 | public Ip6Address getIp6Address() { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 134 | if (!isIp6()) { |
Pavlin Radoslavov | 34c8164 | 2014-11-04 16:21:38 -0800 | [diff] [blame] | 135 | return null; |
| 136 | } |
| 137 | |
| 138 | // Return this object itself if it is already instance of Ip6Address |
| 139 | if (this instanceof Ip6Address) { |
| 140 | return (Ip6Address) this; |
| 141 | } |
| 142 | return Ip6Address.valueOf(octets); |
| 143 | } |
| 144 | |
| 145 | /** |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 146 | * Returns the IP address as a byte array. |
| 147 | * |
| 148 | * @return a byte array |
| 149 | */ |
| 150 | public byte[] toOctets() { |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 151 | return Arrays.copyOf(octets, octets.length); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
Yuta HIGUCHI | c012dda | 2016-08-17 00:43:46 -0700 | [diff] [blame] | 155 | * Returns the IP address as InetAddress. |
| 156 | * |
| 157 | * @return InetAddress |
| 158 | */ |
| 159 | public InetAddress toInetAddress() { |
| 160 | try { |
| 161 | return InetAddress.getByAddress(octets); |
| 162 | } catch (UnknownHostException e) { |
| 163 | // Should never reach here |
| 164 | return null; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 169 | * Computes the IP address byte length for a given IP version. |
| 170 | * |
| 171 | * @param version the IP version |
| 172 | * @return the IP address byte length for the IP version |
| 173 | * @throws IllegalArgumentException if the IP version is invalid |
| 174 | */ |
| 175 | public static int byteLength(Version version) { |
| 176 | switch (version) { |
| 177 | case INET: |
| 178 | return INET_BYTE_LENGTH; |
| 179 | case INET6: |
| 180 | return INET6_BYTE_LENGTH; |
| 181 | default: |
| 182 | String msg = "Invalid IP version " + version; |
| 183 | throw new IllegalArgumentException(msg); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Converts an integer into an IPv4 address. |
| 189 | * |
| 190 | * @param value an integer representing an IPv4 address value |
| 191 | * @return an IP address |
| 192 | */ |
| 193 | public static IpAddress valueOf(int value) { |
| 194 | byte[] bytes = |
| 195 | ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(value).array(); |
| 196 | return new IpAddress(Version.INET, bytes); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Converts a byte array into an IP address. |
| 201 | * |
| 202 | * @param version the IP address version |
| 203 | * @param value the IP address value stored in network byte order |
| 204 | * (i.e., the most significant byte first) |
| 205 | * @return an IP address |
| 206 | * @throws IllegalArgumentException if the arguments are invalid |
| 207 | */ |
| 208 | public static IpAddress valueOf(Version version, byte[] value) { |
| 209 | return new IpAddress(version, value); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Converts a byte array and a given offset from the beginning of the |
| 214 | * array into an IP address. |
| 215 | * <p> |
| 216 | * The IP address is stored in network byte order (i.e., the most |
| 217 | * significant byte first). |
| 218 | * </p> |
| 219 | * @param version the IP address version |
| 220 | * @param value the value to use |
| 221 | * @param offset the offset in bytes from the beginning of the byte array |
| 222 | * @return an IP address |
| 223 | * @throws IllegalArgumentException if the arguments are invalid |
| 224 | */ |
| 225 | public static IpAddress valueOf(Version version, byte[] value, |
| 226 | int offset) { |
| 227 | checkArguments(version, value, offset); |
| 228 | byte[] bc = Arrays.copyOfRange(value, offset, value.length); |
| 229 | return IpAddress.valueOf(version, bc); |
| 230 | } |
| 231 | |
| 232 | /** |
Pavlin Radoslavov | af5ff79 | 2014-10-31 20:51:47 -0700 | [diff] [blame] | 233 | * Converts an InetAddress into an IP address. |
| 234 | * |
| 235 | * @param inetAddress the InetAddress value to use |
| 236 | * @return an IP address |
| 237 | * @throws IllegalArgumentException if the argument is invalid |
| 238 | */ |
| 239 | public static IpAddress valueOf(InetAddress inetAddress) { |
| 240 | byte[] bytes = inetAddress.getAddress(); |
| 241 | if (inetAddress instanceof Inet4Address) { |
| 242 | return new IpAddress(Version.INET, bytes); |
| 243 | } |
| 244 | if (inetAddress instanceof Inet6Address) { |
| 245 | return new IpAddress(Version.INET6, bytes); |
| 246 | } |
| 247 | // Use the number of bytes as a hint |
| 248 | if (bytes.length == INET_BYTE_LENGTH) { |
| 249 | return new IpAddress(Version.INET, bytes); |
| 250 | } |
| 251 | if (bytes.length == INET6_BYTE_LENGTH) { |
| 252 | return new IpAddress(Version.INET6, bytes); |
| 253 | } |
| 254 | final String msg = "Unrecognized IP version address string: " + |
| 255 | inetAddress.toString(); |
| 256 | throw new IllegalArgumentException(msg); |
| 257 | } |
| 258 | |
| 259 | /** |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 260 | * Converts an IPv4 or IPv6 string literal (e.g., "10.2.3.4" or |
| 261 | * "1111:2222::8888") into an IP address. |
| 262 | * |
| 263 | * @param value an IP address value in string form |
| 264 | * @return an IP address |
| 265 | * @throws IllegalArgumentException if the argument is invalid |
| 266 | */ |
| 267 | public static IpAddress valueOf(String value) { |
Pavlin Radoslavov | af5ff79 | 2014-10-31 20:51:47 -0700 | [diff] [blame] | 268 | InetAddress inetAddress = null; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 269 | try { |
Pavlin Radoslavov | af5ff79 | 2014-10-31 20:51:47 -0700 | [diff] [blame] | 270 | inetAddress = InetAddresses.forString(value); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 271 | } catch (IllegalArgumentException e) { |
| 272 | final String msg = "Invalid IP address string: " + value; |
| 273 | throw new IllegalArgumentException(msg); |
| 274 | } |
Pavlin Radoslavov | af5ff79 | 2014-10-31 20:51:47 -0700 | [diff] [blame] | 275 | return valueOf(inetAddress); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 279 | * Creates an IP network mask prefix. |
| 280 | * |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 281 | * @param version the IP address version |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 282 | * @param prefixLength the length of the mask prefix. Must be in the |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 283 | * interval [0, 32] for IPv4, or [0, 128] for IPv6 |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 284 | * @return a new IP address that contains a mask prefix of the |
| 285 | * specified length |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 286 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 287 | */ |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 288 | public static IpAddress makeMaskPrefix(Version version, int prefixLength) { |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 289 | byte[] mask = makeMaskPrefixArray(version, prefixLength); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 290 | return new IpAddress(version, mask); |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Creates an IP address by masking it with a network mask of given |
| 295 | * mask length. |
| 296 | * |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 297 | * @param address the address to mask |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 298 | * @param prefixLength the length of the mask prefix. Must be in the |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 299 | * interval [0, 32] for IPv4, or [0, 128] for IPv6 |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 300 | * @return a new IP address that is masked with a mask prefix of the |
| 301 | * specified length |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 302 | * @throws IllegalArgumentException if the prefix length is invalid |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 303 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 304 | public static IpAddress makeMaskedAddress(final IpAddress address, |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 305 | int prefixLength) { |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 306 | if (address instanceof Ip4Address) { |
| 307 | Ip4Address ip4a = (Ip4Address) address; |
| 308 | return Ip4Address.makeMaskedAddress(ip4a, prefixLength); |
| 309 | } else if (address instanceof Ip6Address) { |
| 310 | Ip6Address ip6a = (Ip6Address) address; |
| 311 | return Ip6Address.makeMaskedAddress(ip6a, prefixLength); |
| 312 | } else { |
| 313 | byte[] net = makeMaskedAddressArray(address, prefixLength); |
| 314 | return IpAddress.valueOf(address.version(), net); |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 315 | } |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 318 | /** |
| 319 | * Check if this IP address is zero. |
| 320 | * |
Thomas Vachuska | e796610 | 2015-09-09 17:33:33 -0700 | [diff] [blame] | 321 | * @return true if this address is zero |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 322 | */ |
| 323 | public boolean isZero() { |
| 324 | for (byte b : octets) { |
| 325 | if (b != 0) { |
| 326 | return false; |
| 327 | } |
| 328 | } |
| 329 | return true; |
| 330 | } |
| 331 | |
Thomas Vachuska | e796610 | 2015-09-09 17:33:33 -0700 | [diff] [blame] | 332 | /** |
| 333 | * Check if this IP address is self-assigned. |
| 334 | * |
| 335 | * @return true if this address is self-assigned |
| 336 | */ |
| 337 | public boolean isSelfAssigned() { |
| 338 | return isIp4() && octets[0] == (byte) 169; |
| 339 | } |
| 340 | |
Charles Chan | 4ca8e60 | 2016-02-25 18:05:59 -0800 | [diff] [blame] | 341 | /** |
| 342 | * Check if this IP address is a multicast address. |
| 343 | * |
| 344 | * @return true if this address a multicast address |
| 345 | */ |
| 346 | public boolean isMulticast() { |
| 347 | return isIp4() ? |
Charles Chan | aedabfd | 2016-02-26 09:31:48 -0800 | [diff] [blame] | 348 | Ip4Prefix.IPV4_MULTICAST_PREFIX.contains(this.getIp4Address()) : |
| 349 | Ip6Prefix.IPV6_MULTICAST_PREFIX.contains(this.getIp6Address()); |
Charles Chan | 4ca8e60 | 2016-02-25 18:05:59 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 352 | @Override |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 353 | public int compareTo(IpAddress o) { |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 354 | // Compare first the version |
| 355 | if (this.version != o.version) { |
| 356 | return this.version.compareTo(o.version); |
| 357 | } |
| 358 | |
| 359 | // Compare the bytes, one-by-one |
| 360 | for (int i = 0; i < this.octets.length; i++) { |
| 361 | if (this.octets[i] != o.octets[i]) { |
| 362 | return UnsignedBytes.compare(this.octets[i], o.octets[i]); |
| 363 | } |
| 364 | } |
| 365 | return 0; // Equal |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | @Override |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 369 | public int hashCode() { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 370 | return Objects.hash(version, Arrays.hashCode(octets)); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public boolean equals(Object obj) { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 375 | if (this == obj) { |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 376 | return true; |
| 377 | } |
Pavlin Radoslavov | 50b7067 | 2014-11-05 11:22:25 -0800 | [diff] [blame] | 378 | if ((obj == null) || (!(obj instanceof IpAddress))) { |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 379 | return false; |
| 380 | } |
| 381 | IpAddress other = (IpAddress) obj; |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 382 | return (version == other.version) && |
| 383 | Arrays.equals(octets, other.octets); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | @Override |
| 387 | /* |
| 388 | * (non-Javadoc) |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 389 | * The string representation of the IP address: "x.x.x.x" for IPv4 |
| 390 | * addresses, or ':' separated string for IPv6 addresses. |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 391 | * |
| 392 | * @see java.lang.Object#toString() |
| 393 | */ |
| 394 | public String toString() { |
Brian O'Connor | 041515f | 2015-02-19 15:36:17 -0800 | [diff] [blame] | 395 | // FIXME InetAddress is super slow |
| 396 | switch (version) { |
| 397 | case INET: |
| 398 | return String.format("%d.%d.%d.%d", octets[0] & 0xff, |
Aaron Kruglikov | afdf4de | 2015-06-24 09:28:22 -0700 | [diff] [blame] | 399 | octets[1] & 0xff, |
| 400 | octets[2] & 0xff, |
| 401 | octets[3] & 0xff); |
Brian O'Connor | 041515f | 2015-02-19 15:36:17 -0800 | [diff] [blame] | 402 | case INET6: |
| 403 | default: |
Aaron Kruglikov | afdf4de | 2015-06-24 09:28:22 -0700 | [diff] [blame] | 404 | return ipv6ToStringHelper(); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 405 | } |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /** |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 409 | * Generates an IP prefix. |
| 410 | * |
| 411 | * @return the IP prefix of the IP address |
| 412 | */ |
| 413 | public IpPrefix toIpPrefix() { |
| 414 | |
| 415 | if (isIp4()) { |
| 416 | return IpPrefix.valueOf(new IpAddress(Version.INET, octets), |
| 417 | Ip4Address.BIT_LENGTH); |
| 418 | } else { |
| 419 | return IpPrefix.valueOf(new IpAddress(Version.INET6, octets), |
| 420 | Ip6Address.BIT_LENGTH); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /** |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 425 | * Gets the IP address name for the IP address version. |
| 426 | * |
| 427 | * @param version the IP address version |
| 428 | * @return the IP address name for the IP address version |
| 429 | */ |
| 430 | private static String addressName(Version version) { |
| 431 | switch (version) { |
| 432 | case INET: |
| 433 | return "IPv4"; |
| 434 | case INET6: |
| 435 | return "IPv6"; |
| 436 | default: |
| 437 | break; |
| 438 | } |
| 439 | return "UnknownIP(" + version + ")"; |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Checks whether the arguments are valid. |
| 444 | * |
| 445 | * @param version the IP address version |
| 446 | * @param value the IP address value stored in a byte array |
| 447 | * @param offset the offset in bytes from the beginning of the byte |
| 448 | * array with the address |
| 449 | * @throws IllegalArgumentException if any of the arguments is invalid |
| 450 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 451 | static void checkArguments(Version version, byte[] value, int offset) { |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 452 | // Check the offset and byte array length |
| 453 | int addrByteLength = byteLength(version); |
| 454 | if ((offset < 0) || (offset + addrByteLength > value.length)) { |
| 455 | String msg; |
| 456 | if (value.length < addrByteLength) { |
| 457 | msg = "Invalid " + addressName(version) + |
| 458 | " address array: array length: " + value.length + |
| 459 | ". Must be at least " + addrByteLength; |
| 460 | } else { |
| 461 | msg = "Invalid " + addressName(version) + |
| 462 | " address array: array offset: " + offset + |
| 463 | ". Must be in the interval [0, " + |
| 464 | (value.length - addrByteLength) + "]"; |
| 465 | } |
| 466 | throw new IllegalArgumentException(msg); |
| 467 | } |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 468 | } |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 469 | |
| 470 | /** |
| 471 | * Creates a byte array for IP network mask prefix. |
| 472 | * |
| 473 | * @param version the IP address version |
| 474 | * @param prefixLength the length of the mask prefix. Must be in the |
| 475 | * interval [0, 32] for IPv4, or [0, 128] for IPv6 |
| 476 | * @return a byte array that contains a mask prefix of the |
| 477 | * specified length |
| 478 | * @throws IllegalArgumentException if the arguments are invalid |
| 479 | */ |
| 480 | static byte[] makeMaskPrefixArray(Version version, int prefixLength) { |
| 481 | int addrByteLength = byteLength(version); |
| 482 | int addrBitLength = addrByteLength * Byte.SIZE; |
| 483 | |
| 484 | // Verify the prefix length |
| 485 | if ((prefixLength < 0) || (prefixLength > addrBitLength)) { |
| 486 | final String msg = "Invalid IP prefix length: " + prefixLength + |
| 487 | ". Must be in the interval [0, " + addrBitLength + "]."; |
| 488 | throw new IllegalArgumentException(msg); |
| 489 | } |
| 490 | |
| 491 | // Number of bytes and extra bits that should be all 1s |
| 492 | int maskBytes = prefixLength / Byte.SIZE; |
| 493 | int maskBits = prefixLength % Byte.SIZE; |
| 494 | byte[] mask = new byte[addrByteLength]; |
| 495 | |
| 496 | // Set the bytes and extra bits to 1s |
| 497 | for (int i = 0; i < maskBytes; i++) { |
| 498 | mask[i] = (byte) 0xff; // Set mask bytes to 1s |
| 499 | } |
| 500 | for (int i = maskBytes; i < addrByteLength; i++) { |
| 501 | mask[i] = 0; // Set remaining bytes to 0s |
| 502 | } |
| 503 | if (maskBits > 0) { |
| 504 | mask[maskBytes] = (byte) (0xff << (Byte.SIZE - maskBits)); |
| 505 | } |
| 506 | return mask; |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Creates a byte array that represents an IP address masked with |
| 511 | * a network mask of given mask length. |
| 512 | * |
| 513 | * @param addr the address to mask |
| 514 | * @param prefixLength the length of the mask prefix. Must be in the |
| 515 | * interval [0, 32] for IPv4, or [0, 128] for IPv6 |
| 516 | * @return a byte array that represents the IP address masked with |
| 517 | * a mask prefix of the specified length |
| 518 | * @throws IllegalArgumentException if the prefix length is invalid |
| 519 | */ |
| 520 | static byte[] makeMaskedAddressArray(final IpAddress addr, |
| 521 | int prefixLength) { |
| 522 | byte[] mask = IpAddress.makeMaskPrefixArray(addr.version(), |
| 523 | prefixLength); |
| 524 | byte[] net = new byte[mask.length]; |
| 525 | |
| 526 | // Mask each byte |
| 527 | for (int i = 0; i < net.length; i++) { |
| 528 | net[i] = (byte) (addr.octets[i] & mask[i]); |
| 529 | } |
| 530 | return net; |
| 531 | } |
Aaron Kruglikov | afdf4de | 2015-06-24 09:28:22 -0700 | [diff] [blame] | 532 | |
| 533 | /** |
| 534 | * Creates a string based on the IPv6 recommendations for canonical representations found here: |
| 535 | * https://tools.ietf.org/html/rfc5952#section-1. |
| 536 | * @return A properly formatted IPv6 canonical representation. |
| 537 | */ |
| 538 | private String ipv6ToStringHelper() { |
| 539 | //Populate a buffer with the string of the full address with leading zeros stripped |
Yuta HIGUCHI | 2dce08a | 2017-04-20 21:57:48 -0700 | [diff] [blame] | 540 | StringBuilder buff = new StringBuilder(); |
Aaron Kruglikov | afdf4de | 2015-06-24 09:28:22 -0700 | [diff] [blame] | 541 | buff.append(String.format("%x:%x:%x:%x:%x:%x:%x:%x", |
| 542 | (((octets[0] & BIT_MASK) << 8) | (octets[1] & BIT_MASK)), |
| 543 | (((octets[2] & BIT_MASK) << 8) | (octets[3] & BIT_MASK)), |
| 544 | (((octets[4] & BIT_MASK) << 8) | (octets[5] & BIT_MASK)), |
| 545 | (((octets[6] & BIT_MASK) << 8) | (octets[7] & BIT_MASK)), |
| 546 | (((octets[8] & BIT_MASK) << 8) | (octets[9] & BIT_MASK)), |
| 547 | (((octets[10] & BIT_MASK) << 8) | (octets[11] & BIT_MASK)), |
| 548 | (((octets[12] & BIT_MASK) << 8) | (octets[13] & BIT_MASK)), |
| 549 | (((octets[14] & BIT_MASK) << 8) | (octets[15] & BIT_MASK)))); |
| 550 | //Initialize variables for tracking longest zero subsequence, tiebreaking by first occurence |
| 551 | int longestSeqStart, longestSeqLen, currSeqStart, currSeqLen; |
| 552 | longestSeqStart = 0; |
| 553 | longestSeqLen = 0; |
| 554 | currSeqStart = 0; |
| 555 | currSeqLen = 0; |
| 556 | |
| 557 | for (int index = 0; index < buff.length(); index++) { |
| 558 | if (buff.charAt(index) == ':') { |
| 559 | if (currSeqLen != 0 && buff.charAt(index + 1) == '0') { |
| 560 | currSeqLen += 1; |
| 561 | } |
| 562 | } else if (buff.charAt(index) == '0' && ((index == 0) || (buff.charAt(index - 1) == ':'))) { |
| 563 | if (currSeqLen == 0) { |
| 564 | currSeqStart = index; |
| 565 | } |
| 566 | currSeqLen += 1; |
| 567 | } else { |
| 568 | if (currSeqLen > longestSeqLen) { |
| 569 | longestSeqStart = currSeqStart; |
| 570 | longestSeqLen = currSeqLen; |
| 571 | } |
| 572 | currSeqLen = 0; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | if (currSeqLen > longestSeqLen) { |
| 577 | longestSeqLen = currSeqLen; |
| 578 | longestSeqStart = currSeqStart; |
| 579 | } |
| 580 | if (longestSeqLen > 1) { |
| 581 | if (buff.length() == (longestSeqStart + longestSeqLen)) { |
| 582 | buff.append(':'); |
| 583 | } |
| 584 | |
| 585 | buff.delete(longestSeqStart, longestSeqStart + longestSeqLen); |
| 586 | |
| 587 | if (longestSeqStart == 0) { |
| 588 | buff.insert(0, ':'); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | return buff.toString(); |
| 593 | } |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 594 | } |