blob: 75b5b2f48ec243933d746c79a46842090bc8ccde [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
Jonathan Hartdec62d42014-09-22 15:59:04 -070016package org.onlab.packet;
17
Pier Ventre48ca5192016-11-28 16:52:24 -080018import com.google.common.net.InetAddresses;
19import com.google.common.primitives.UnsignedBytes;
20
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070021import java.net.Inet4Address;
22import java.net.Inet6Address;
Pier Ventre48ca5192016-11-28 16:52:24 -080023import java.net.InetAddress;
24import java.net.UnknownHostException;
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070025import java.nio.ByteBuffer;
Jonathan Hartdec62d42014-09-22 15:59:04 -070026import java.util.Arrays;
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070027import java.util.Objects;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070028
Jonathan Hartdec62d42014-09-22 15:59:04 -070029
30/**
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070031 * A class representing an IP address.
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080032 * This class is immutable.
Jonathan Hartdec62d42014-09-22 15:59:04 -070033 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080034public class IpAddress implements Comparable<IpAddress> {
Aaron Kruglikovafdf4de2015-06-24 09:28:22 -070035 private static final int BIT_MASK = 0x000000ff;
36
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070037 // IP Versions
Jon Hall8c7b06a2017-02-22 13:37:33 -080038 public enum Version { INET, INET6 }
Jonathan Hartdec62d42014-09-22 15:59:04 -070039
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070040 // 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 Hartdec62d42014-09-22 15:59:04 -070045
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070046 private final Version version;
47 private final byte[] octets;
Jonathan Hartdec62d42014-09-22 15:59:04 -070048
49 /**
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070050 * Constructor for given IP address version and address octets.
51 *
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070052 * @param version the IP address version
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070053 * @param value the IP address value stored in network byte order
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070054 * (i.e., the most significant byte first)
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070055 * @throws IllegalArgumentException if the arguments are invalid
Jonathan Hartdec62d42014-09-22 15:59:04 -070056 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -080057 protected IpAddress(Version version, byte[] value) {
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070058 checkArguments(version, value, 0);
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070059 this.version = version;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -070060 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 Radoslavov49e159a2014-10-29 16:22:13 -070071 }
Jonathan Hartdec62d42014-09-22 15:59:04 -070072 }
73
74 /**
Pier Ventre48ca5192016-11-28 16:52:24 -080075 * Default constructor for Kryo serialization.
76 */
77 protected IpAddress() {
78 this.version = null;
79 this.octets = null;
80 }
81
82 /**
Jonathan Hartdec62d42014-09-22 15:59:04 -070083 * 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 Radoslavov34ffe722015-03-10 12:48:55 -070092 * 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 Radoslavov34c81642014-11-04 16:21:38 -0800110 * 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 Radoslavov87dd9302015-03-10 13:53:24 -0700116 if (!isIp4()) {
Pavlin Radoslavov34c81642014-11-04 16:21:38 -0800117 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 Radoslavov87dd9302015-03-10 13:53:24 -0700134 if (!isIp6()) {
Pavlin Radoslavov34c81642014-11-04 16:21:38 -0800135 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 Hartdec62d42014-09-22 15:59:04 -0700146 * Returns the IP address as a byte array.
147 *
148 * @return a byte array
149 */
150 public byte[] toOctets() {
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700151 return Arrays.copyOf(octets, octets.length);
Jonathan Hartdec62d42014-09-22 15:59:04 -0700152 }
153
154 /**
Yuta HIGUCHIc012dda2016-08-17 00:43:46 -0700155 * 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 Radoslavovd0e32d72014-10-31 18:11:43 -0700169 * 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 Radoslavovaf5ff792014-10-31 20:51:47 -0700233 * 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 Radoslavovd0e32d72014-10-31 18:11:43 -0700260 * 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 Radoslavovaf5ff792014-10-31 20:51:47 -0700268 InetAddress inetAddress = null;
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700269 try {
Pavlin Radoslavovaf5ff792014-10-31 20:51:47 -0700270 inetAddress = InetAddresses.forString(value);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700271 } catch (IllegalArgumentException e) {
272 final String msg = "Invalid IP address string: " + value;
273 throw new IllegalArgumentException(msg);
274 }
Pavlin Radoslavovaf5ff792014-10-31 20:51:47 -0700275 return valueOf(inetAddress);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700276 }
277
278 /**
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700279 * Creates an IP network mask prefix.
280 *
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700281 * @param version the IP address version
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700282 * @param prefixLength the length of the mask prefix. Must be in the
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700283 * interval [0, 32] for IPv4, or [0, 128] for IPv6
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700284 * @return a new IP address that contains a mask prefix of the
285 * specified length
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700286 * @throws IllegalArgumentException if the arguments are invalid
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700287 */
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700288 public static IpAddress makeMaskPrefix(Version version, int prefixLength) {
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800289 byte[] mask = makeMaskPrefixArray(version, prefixLength);
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700290 return new IpAddress(version, mask);
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700291 }
292
293 /**
294 * Creates an IP address by masking it with a network mask of given
295 * mask length.
296 *
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800297 * @param address the address to mask
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700298 * @param prefixLength the length of the mask prefix. Must be in the
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700299 * interval [0, 32] for IPv4, or [0, 128] for IPv6
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700300 * @return a new IP address that is masked with a mask prefix of the
301 * specified length
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700302 * @throws IllegalArgumentException if the prefix length is invalid
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700303 */
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800304 public static IpAddress makeMaskedAddress(final IpAddress address,
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700305 int prefixLength) {
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800306 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 Radoslavov49e159a2014-10-29 16:22:13 -0700315 }
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700316 }
317
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800318 /**
319 * Check if this IP address is zero.
320 *
Thomas Vachuskae7966102015-09-09 17:33:33 -0700321 * @return true if this address is zero
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800322 */
323 public boolean isZero() {
324 for (byte b : octets) {
325 if (b != 0) {
326 return false;
327 }
328 }
329 return true;
330 }
331
Thomas Vachuskae7966102015-09-09 17:33:33 -0700332 /**
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 Chan4ca8e602016-02-25 18:05:59 -0800341 /**
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 Chanaedabfd2016-02-26 09:31:48 -0800348 Ip4Prefix.IPV4_MULTICAST_PREFIX.contains(this.getIp4Address()) :
349 Ip6Prefix.IPV6_MULTICAST_PREFIX.contains(this.getIp6Address());
Charles Chan4ca8e602016-02-25 18:05:59 -0800350 }
351
Jonathan Hartdec62d42014-09-22 15:59:04 -0700352 @Override
Jonathan Hartab63aac2014-10-16 08:52:55 -0700353 public int compareTo(IpAddress o) {
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700354 // 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 Hartab63aac2014-10-16 08:52:55 -0700366 }
367
368 @Override
Jonathan Hartdec62d42014-09-22 15:59:04 -0700369 public int hashCode() {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700370 return Objects.hash(version, Arrays.hashCode(octets));
Jonathan Hartdec62d42014-09-22 15:59:04 -0700371 }
372
373 @Override
374 public boolean equals(Object obj) {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700375 if (this == obj) {
Jonathan Hartdec62d42014-09-22 15:59:04 -0700376 return true;
377 }
Pavlin Radoslavov50b70672014-11-05 11:22:25 -0800378 if ((obj == null) || (!(obj instanceof IpAddress))) {
Jonathan Hartdec62d42014-09-22 15:59:04 -0700379 return false;
380 }
381 IpAddress other = (IpAddress) obj;
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700382 return (version == other.version) &&
383 Arrays.equals(octets, other.octets);
Jonathan Hartdec62d42014-09-22 15:59:04 -0700384 }
385
386 @Override
387 /*
388 * (non-Javadoc)
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700389 * The string representation of the IP address: "x.x.x.x" for IPv4
390 * addresses, or ':' separated string for IPv6 addresses.
Jonathan Hartdec62d42014-09-22 15:59:04 -0700391 *
392 * @see java.lang.Object#toString()
393 */
394 public String toString() {
Brian O'Connor041515f2015-02-19 15:36:17 -0800395 // FIXME InetAddress is super slow
396 switch (version) {
397 case INET:
398 return String.format("%d.%d.%d.%d", octets[0] & 0xff,
Aaron Kruglikovafdf4de2015-06-24 09:28:22 -0700399 octets[1] & 0xff,
400 octets[2] & 0xff,
401 octets[3] & 0xff);
Brian O'Connor041515f2015-02-19 15:36:17 -0800402 case INET6:
403 default:
Aaron Kruglikovafdf4de2015-06-24 09:28:22 -0700404 return ipv6ToStringHelper();
Jonathan Hartdec62d42014-09-22 15:59:04 -0700405 }
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700406 }
407
408 /**
Pingping Line28ae4c2015-03-13 11:37:03 -0700409 * 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 Radoslavovd0e32d72014-10-31 18:11:43 -0700425 * 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 Radoslavovf182f012014-11-04 15:03:18 -0800451 static void checkArguments(Version version, byte[] value, int offset) {
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700452 // 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 Hartdec62d42014-09-22 15:59:04 -0700468 }
Pavlin Radoslavovf182f012014-11-04 15:03:18 -0800469
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 Kruglikovafdf4de2015-06-24 09:28:22 -0700532
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 HIGUCHI2dce08a2017-04-20 21:57:48 -0700540 StringBuilder buff = new StringBuilder();
Aaron Kruglikovafdf4de2015-06-24 09:28:22 -0700541 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 Hartdec62d42014-09-22 15:59:04 -0700594}