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 | */ |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 18 | import java.net.InetAddress; |
| 19 | import java.net.Inet4Address; |
| 20 | import java.net.Inet6Address; |
| 21 | import java.net.UnknownHostException; |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 22 | import java.nio.ByteBuffer; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 23 | import java.util.Arrays; |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 24 | import java.util.Objects; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 25 | |
| 26 | import com.google.common.net.InetAddresses; |
| 27 | import com.google.common.primitives.UnsignedBytes; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkState; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 32 | * A class representing an IP address. |
| 33 | * TODO: Add support for IPv6 as well. |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 34 | */ |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 35 | public final class IpAddress implements Comparable<IpAddress> { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 36 | // IP Versions |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 37 | public enum Version { INET, INET6 }; |
| 38 | |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 39 | // lengths of address, in bytes |
| 40 | public static final int INET_BYTE_LENGTH = 4; |
| 41 | public static final int INET_BIT_LENGTH = INET_BYTE_LENGTH * Byte.SIZE; |
| 42 | public static final int INET6_BYTE_LENGTH = 16; |
| 43 | public static final int INET6_BIT_LENGTH = INET6_BYTE_LENGTH * Byte.SIZE; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 44 | |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 45 | private final Version version; |
| 46 | private final byte[] octets; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 49 | * Constructor for given IP address version and address octets. |
| 50 | * |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 51 | * @param version the IP address version |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 52 | * @param value the IP address value stored in network byte order |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 53 | * (i.e., the most significant byte first) |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 54 | * @throws IllegalArgumentException if the arguments are invalid |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 55 | */ |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 56 | private IpAddress(Version version, byte[] value) { |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 57 | checkArguments(version, value, 0); |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 58 | this.version = version; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 59 | switch (version) { |
| 60 | case INET: |
| 61 | this.octets = Arrays.copyOf(value, INET_BYTE_LENGTH); |
| 62 | break; |
| 63 | case INET6: |
| 64 | this.octets = Arrays.copyOf(value, INET6_BYTE_LENGTH); |
| 65 | break; |
| 66 | default: |
| 67 | // Should not be reached |
| 68 | this.octets = null; |
| 69 | break; |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 70 | } |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the IP version of this address. |
| 75 | * |
| 76 | * @return the version |
| 77 | */ |
| 78 | public Version version() { |
| 79 | return this.version; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the IP address as a byte array. |
| 84 | * |
| 85 | * @return a byte array |
| 86 | */ |
| 87 | public byte[] toOctets() { |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 88 | return Arrays.copyOf(octets, octets.length); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the integral value of this IP address. |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 93 | * TODO: This method should be moved to Ip4Address. |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 94 | * |
| 95 | * @return the IP address's value as an integer |
| 96 | */ |
| 97 | public int toInt() { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 98 | ByteBuffer bb = ByteBuffer.wrap(octets); |
| 99 | return bb.getInt(); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 102 | /** |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 103 | * Computes the IP address byte length for a given IP version. |
| 104 | * |
| 105 | * @param version the IP version |
| 106 | * @return the IP address byte length for the IP version |
| 107 | * @throws IllegalArgumentException if the IP version is invalid |
| 108 | */ |
| 109 | public static int byteLength(Version version) { |
| 110 | switch (version) { |
| 111 | case INET: |
| 112 | return INET_BYTE_LENGTH; |
| 113 | case INET6: |
| 114 | return INET6_BYTE_LENGTH; |
| 115 | default: |
| 116 | String msg = "Invalid IP version " + version; |
| 117 | throw new IllegalArgumentException(msg); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Converts an integer into an IPv4 address. |
| 123 | * |
| 124 | * @param value an integer representing an IPv4 address value |
| 125 | * @return an IP address |
| 126 | */ |
| 127 | public static IpAddress valueOf(int value) { |
| 128 | byte[] bytes = |
| 129 | ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(value).array(); |
| 130 | return new IpAddress(Version.INET, bytes); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Converts a byte array into an IP address. |
| 135 | * |
| 136 | * @param version the IP address version |
| 137 | * @param value the IP address value stored in network byte order |
| 138 | * (i.e., the most significant byte first) |
| 139 | * @return an IP address |
| 140 | * @throws IllegalArgumentException if the arguments are invalid |
| 141 | */ |
| 142 | public static IpAddress valueOf(Version version, byte[] value) { |
| 143 | return new IpAddress(version, value); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Converts a byte array and a given offset from the beginning of the |
| 148 | * array into an IP address. |
| 149 | * <p> |
| 150 | * The IP address is stored in network byte order (i.e., the most |
| 151 | * significant byte first). |
| 152 | * </p> |
| 153 | * @param version the IP address version |
| 154 | * @param value the value to use |
| 155 | * @param offset the offset in bytes from the beginning of the byte array |
| 156 | * @return an IP address |
| 157 | * @throws IllegalArgumentException if the arguments are invalid |
| 158 | */ |
| 159 | public static IpAddress valueOf(Version version, byte[] value, |
| 160 | int offset) { |
| 161 | checkArguments(version, value, offset); |
| 162 | byte[] bc = Arrays.copyOfRange(value, offset, value.length); |
| 163 | return IpAddress.valueOf(version, bc); |
| 164 | } |
| 165 | |
| 166 | /** |
Pavlin Radoslavov | af5ff79 | 2014-10-31 20:51:47 -0700 | [diff] [blame] | 167 | * Converts an InetAddress into an IP address. |
| 168 | * |
| 169 | * @param inetAddress the InetAddress value to use |
| 170 | * @return an IP address |
| 171 | * @throws IllegalArgumentException if the argument is invalid |
| 172 | */ |
| 173 | public static IpAddress valueOf(InetAddress inetAddress) { |
| 174 | byte[] bytes = inetAddress.getAddress(); |
| 175 | if (inetAddress instanceof Inet4Address) { |
| 176 | return new IpAddress(Version.INET, bytes); |
| 177 | } |
| 178 | if (inetAddress instanceof Inet6Address) { |
| 179 | return new IpAddress(Version.INET6, bytes); |
| 180 | } |
| 181 | // Use the number of bytes as a hint |
| 182 | if (bytes.length == INET_BYTE_LENGTH) { |
| 183 | return new IpAddress(Version.INET, bytes); |
| 184 | } |
| 185 | if (bytes.length == INET6_BYTE_LENGTH) { |
| 186 | return new IpAddress(Version.INET6, bytes); |
| 187 | } |
| 188 | final String msg = "Unrecognized IP version address string: " + |
| 189 | inetAddress.toString(); |
| 190 | throw new IllegalArgumentException(msg); |
| 191 | } |
| 192 | |
| 193 | /** |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 194 | * Converts an IPv4 or IPv6 string literal (e.g., "10.2.3.4" or |
| 195 | * "1111:2222::8888") into an IP address. |
| 196 | * |
| 197 | * @param value an IP address value in string form |
| 198 | * @return an IP address |
| 199 | * @throws IllegalArgumentException if the argument is invalid |
| 200 | */ |
| 201 | public static IpAddress valueOf(String value) { |
Pavlin Radoslavov | af5ff79 | 2014-10-31 20:51:47 -0700 | [diff] [blame] | 202 | InetAddress inetAddress = null; |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 203 | try { |
Pavlin Radoslavov | af5ff79 | 2014-10-31 20:51:47 -0700 | [diff] [blame] | 204 | inetAddress = InetAddresses.forString(value); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 205 | } catch (IllegalArgumentException e) { |
| 206 | final String msg = "Invalid IP address string: " + value; |
| 207 | throw new IllegalArgumentException(msg); |
| 208 | } |
Pavlin Radoslavov | af5ff79 | 2014-10-31 20:51:47 -0700 | [diff] [blame] | 209 | return valueOf(inetAddress); |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 213 | * Creates an IP network mask prefix. |
| 214 | * |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 215 | * @param version the IP address version |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 216 | * @param prefixLength the length of the mask prefix. Must be in the |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 217 | * interval [0, 32] for IPv4, or [0, 128] for IPv6 |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 218 | * @return a new IP address that contains a mask prefix of the |
| 219 | * specified length |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 220 | * @throws IllegalArgumentException if the arguments are invalid |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 221 | */ |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 222 | public static IpAddress makeMaskPrefix(Version version, int prefixLength) { |
| 223 | int addrByteLength = byteLength(version); |
| 224 | int addrBitLength = addrByteLength * Byte.SIZE; |
| 225 | |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 226 | // Verify the prefix length |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 227 | if ((prefixLength < 0) || (prefixLength > addrBitLength)) { |
| 228 | final String msg = "Invalid IP prefix length: " + prefixLength + |
| 229 | ". Must be in the interval [0, " + addrBitLength + "]."; |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 230 | throw new IllegalArgumentException(msg); |
| 231 | } |
| 232 | |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 233 | // Number of bytes and extra bits that should be all 1s |
| 234 | int maskBytes = prefixLength / Byte.SIZE; |
| 235 | int maskBits = prefixLength % Byte.SIZE; |
| 236 | byte[] mask = new byte[addrByteLength]; |
| 237 | |
| 238 | // Set the bytes and extra bits to 1s |
| 239 | for (int i = 0; i < maskBytes; i++) { |
| 240 | mask[i] = (byte) 0xff; // Set mask bytes to 1s |
| 241 | } |
| 242 | for (int i = maskBytes; i < addrByteLength; i++) { |
| 243 | mask[i] = 0; // Set remaining bytes to 0s |
| 244 | } |
| 245 | if (maskBits > 0) { |
| 246 | mask[maskBytes] = (byte) (0xff << (Byte.SIZE - maskBits)); |
| 247 | } |
| 248 | return new IpAddress(version, mask); |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Creates an IP address by masking it with a network mask of given |
| 253 | * mask length. |
| 254 | * |
| 255 | * @param addr the address to mask |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 256 | * @param prefixLength the length of the mask prefix. Must be in the |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 257 | * interval [0, 32] for IPv4, or [0, 128] for IPv6 |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 258 | * @return a new IP address that is masked with a mask prefix of the |
| 259 | * specified length |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 260 | * @throws IllegalArgumentException if the prefix length is invalid |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 261 | */ |
| 262 | public static IpAddress makeMaskedAddress(final IpAddress addr, |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 263 | int prefixLength) { |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 264 | IpAddress mask = IpAddress.makeMaskPrefix(addr.version(), |
| 265 | prefixLength); |
| 266 | byte[] net = new byte[mask.octets.length]; |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 267 | |
| 268 | // Mask each byte |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 269 | for (int i = 0; i < net.length; i++) { |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 270 | net[i] = (byte) (addr.octets[i] & mask.octets[i]); |
| 271 | } |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 272 | return IpAddress.valueOf(addr.version(), net); |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 275 | @Override |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 276 | public int compareTo(IpAddress o) { |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 277 | // Compare first the version |
| 278 | if (this.version != o.version) { |
| 279 | return this.version.compareTo(o.version); |
| 280 | } |
| 281 | |
| 282 | // Compare the bytes, one-by-one |
| 283 | for (int i = 0; i < this.octets.length; i++) { |
| 284 | if (this.octets[i] != o.octets[i]) { |
| 285 | return UnsignedBytes.compare(this.octets[i], o.octets[i]); |
| 286 | } |
| 287 | } |
| 288 | return 0; // Equal |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | @Override |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 292 | public int hashCode() { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 293 | return Objects.hash(version, Arrays.hashCode(octets)); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public boolean equals(Object obj) { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 298 | if (this == obj) { |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 299 | return true; |
| 300 | } |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 301 | if ((obj == null) || (getClass() != obj.getClass())) { |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | IpAddress other = (IpAddress) obj; |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 305 | return (version == other.version) && |
| 306 | Arrays.equals(octets, other.octets); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Override |
| 310 | /* |
| 311 | * (non-Javadoc) |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 312 | * The string representation of the IP address: "x.x.x.x" for IPv4 |
| 313 | * addresses, or ':' separated string for IPv6 addresses. |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 314 | * |
| 315 | * @see java.lang.Object#toString() |
| 316 | */ |
| 317 | public String toString() { |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 318 | InetAddress inetAddr = null; |
| 319 | try { |
| 320 | inetAddr = InetAddress.getByAddress(octets); |
| 321 | } catch (UnknownHostException e) { |
| 322 | // Should never happen |
| 323 | checkState(false, "Internal error: Ip6Address.toString()"); |
| 324 | return "[Invalid IP Address]"; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 325 | } |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 326 | return InetAddresses.toAddrString(inetAddr); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Gets the IP address name for the IP address version. |
| 331 | * |
| 332 | * @param version the IP address version |
| 333 | * @return the IP address name for the IP address version |
| 334 | */ |
| 335 | private static String addressName(Version version) { |
| 336 | switch (version) { |
| 337 | case INET: |
| 338 | return "IPv4"; |
| 339 | case INET6: |
| 340 | return "IPv6"; |
| 341 | default: |
| 342 | break; |
| 343 | } |
| 344 | return "UnknownIP(" + version + ")"; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Checks whether the arguments are valid. |
| 349 | * |
| 350 | * @param version the IP address version |
| 351 | * @param value the IP address value stored in a byte array |
| 352 | * @param offset the offset in bytes from the beginning of the byte |
| 353 | * array with the address |
| 354 | * @throws IllegalArgumentException if any of the arguments is invalid |
| 355 | */ |
| 356 | private static void checkArguments(Version version, byte[] value, |
| 357 | int offset) { |
| 358 | // Check the offset and byte array length |
| 359 | int addrByteLength = byteLength(version); |
| 360 | if ((offset < 0) || (offset + addrByteLength > value.length)) { |
| 361 | String msg; |
| 362 | if (value.length < addrByteLength) { |
| 363 | msg = "Invalid " + addressName(version) + |
| 364 | " address array: array length: " + value.length + |
| 365 | ". Must be at least " + addrByteLength; |
| 366 | } else { |
| 367 | msg = "Invalid " + addressName(version) + |
| 368 | " address array: array offset: " + offset + |
| 369 | ". Must be in the interval [0, " + |
| 370 | (value.length - addrByteLength) + "]"; |
| 371 | } |
| 372 | throw new IllegalArgumentException(msg); |
| 373 | } |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 374 | } |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 375 | } |