1 /*
2 * Copyright 2012 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.util;
17
18 import io.netty.util.NetUtilInitializations.NetworkIfaceAndInetAddress;
19 import io.netty.util.internal.PlatformDependent;
20 import io.netty.util.internal.StringUtil;
21 import io.netty.util.internal.SystemPropertyUtil;
22 import io.netty.util.internal.logging.InternalLogger;
23 import io.netty.util.internal.logging.InternalLoggerFactory;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.net.Inet4Address;
32 import java.net.Inet6Address;
33 import java.net.InetAddress;
34 import java.net.InetSocketAddress;
35 import java.net.NetworkInterface;
36 import java.net.UnknownHostException;
37 import java.security.AccessController;
38 import java.security.PrivilegedAction;
39 import java.util.Arrays;
40 import java.util.Collection;
41
42 import static io.netty.util.AsciiString.indexOf;
43
44 /**
45 * A class that holds a number of network-related constants.
46 * <p/>
47 * This class borrowed some of its methods from a modified fork of the
48 * <a href="https://svn.apache.org/repos/asf/harmony/enhanced/java/branches/java6/classlib/modules/luni/
49 * src/main/java/org/apache/harmony/luni/util/Inet6Util.java">Inet6Util class</a> which was part of Apache Harmony.
50 */
51 public final class NetUtil {
52
53 /**
54 * The {@link Inet4Address} that represents the IPv4 loopback address '127.0.0.1'
55 */
56 public static final Inet4Address LOCALHOST4;
57
58 /**
59 * The {@link Inet6Address} that represents the IPv6 loopback address '::1'
60 */
61 public static final Inet6Address LOCALHOST6;
62
63 /**
64 * The {@link InetAddress} that represents the loopback address. If IPv6 stack is available, it will refer to
65 * {@link #LOCALHOST6}. Otherwise, {@link #LOCALHOST4}.
66 */
67 public static final InetAddress LOCALHOST;
68
69 /**
70 * The loopback {@link NetworkInterface} of the current machine
71 */
72 public static final NetworkInterface LOOPBACK_IF;
73
74 /**
75 * An unmodifiable Collection of all the interfaces on this machine.
76 */
77 public static final Collection<NetworkInterface> NETWORK_INTERFACES;
78
79 /**
80 * The SOMAXCONN value of the current machine. If failed to get the value, {@code 200} is used as a
81 * default value for Windows and {@code 128} for others.
82 */
83 public static final int SOMAXCONN;
84
85 /**
86 * This defines how many words (represented as ints) are needed to represent an IPv6 address
87 */
88 private static final int IPV6_WORD_COUNT = 8;
89
90 /**
91 * The maximum number of characters for an IPV6 string with no scope
92 */
93 private static final int IPV6_MAX_CHAR_COUNT = 39;
94
95 /**
96 * Number of bytes needed to represent an IPV6 value
97 */
98 private static final int IPV6_BYTE_COUNT = 16;
99
100 /**
101 * Maximum amount of value adding characters in between IPV6 separators
102 */
103 private static final int IPV6_MAX_CHAR_BETWEEN_SEPARATOR = 4;
104
105 /**
106 * Minimum number of separators that must be present in an IPv6 string
107 */
108 private static final int IPV6_MIN_SEPARATORS = 2;
109
110 /**
111 * Maximum number of separators that must be present in an IPv6 string
112 */
113 private static final int IPV6_MAX_SEPARATORS = 8;
114
115 /**
116 * Maximum amount of value adding characters in between IPV4 separators
117 */
118 private static final int IPV4_MAX_CHAR_BETWEEN_SEPARATOR = 3;
119
120 /**
121 * Number of separators that must be present in an IPv4 string
122 */
123 private static final int IPV4_SEPARATORS = 3;
124
125 /**
126 * {@code true} if IPv4 should be used even if the system supports both IPv4 and IPv6.
127 */
128 private static final boolean IPV4_PREFERRED = SystemPropertyUtil.getBoolean("java.net.preferIPv4Stack", false);
129
130 /**
131 * {@code true} if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address.
132 */
133 private static final boolean IPV6_ADDRESSES_PREFERRED;
134
135 /**
136 * The logger being used by this class
137 */
138 private static final InternalLogger logger = InternalLoggerFactory.getInstance(NetUtil.class);
139
140 static {
141 String prefer = SystemPropertyUtil.get("java.net.preferIPv6Addresses", "false");
142 if ("true".equalsIgnoreCase(prefer.trim())) {
143 IPV6_ADDRESSES_PREFERRED = true;
144 } else {
145 // Let's just use false in this case as only true is "forcing" ipv6.
146 IPV6_ADDRESSES_PREFERRED = false;
147 }
148 logger.debug("-Djava.net.preferIPv4Stack: {}", IPV4_PREFERRED);
149 logger.debug("-Djava.net.preferIPv6Addresses: {}", prefer);
150
151 NETWORK_INTERFACES = NetUtilInitializations.networkInterfaces();
152
153 // Create IPv4 loopback address.
154 LOCALHOST4 = NetUtilInitializations.createLocalhost4();
155
156 // Create IPv6 loopback address.
157 LOCALHOST6 = NetUtilInitializations.createLocalhost6();
158
159 NetworkIfaceAndInetAddress loopback =
160 NetUtilInitializations.determineLoopback(NETWORK_INTERFACES, LOCALHOST4, LOCALHOST6);
161 LOOPBACK_IF = loopback.iface();
162 LOCALHOST = loopback.address();
163
164 // As a SecurityManager may prevent reading the somaxconn file we wrap this in a privileged block.
165 //
166 // See https://github.com/netty/netty/issues/3680
167 SOMAXCONN = AccessController.doPrivileged(new SoMaxConnAction());
168 }
169
170 private static final class SoMaxConnAction implements PrivilegedAction<Integer> {
171 @Override
172 public Integer run() {
173 // Determine the default somaxconn (server socket backlog) value of the platform.
174 // The known defaults:
175 // - Windows NT Server 4.0+: 200
176 // - Linux and Mac OS X: 128
177 int somaxconn = PlatformDependent.isWindows() ? 200 : 128;
178 File file = new File("/proc/sys/net/core/somaxconn");
179 BufferedReader in = null;
180 try {
181 // file.exists() may throw a SecurityException if a SecurityManager is used, so execute it in the
182 // try / catch block.
183 // See https://github.com/netty/netty/issues/4936
184 if (file.exists()) {
185 in = new BufferedReader(new FileReader(file));
186 somaxconn = Integer.parseInt(in.readLine());
187 if (logger.isDebugEnabled()) {
188 logger.debug("{}: {}", file, somaxconn);
189 }
190 } else {
191 // Try to get from sysctl
192 Integer tmp = null;
193 if (SystemPropertyUtil.getBoolean("io.netty.net.somaxconn.trySysctl", false)) {
194 tmp = sysctlGetInt("kern.ipc.somaxconn");
195 if (tmp == null) {
196 tmp = sysctlGetInt("kern.ipc.soacceptqueue");
197 if (tmp != null) {
198 somaxconn = tmp;
199 }
200 } else {
201 somaxconn = tmp;
202 }
203 }
204
205 if (tmp == null) {
206 logger.debug("Failed to get SOMAXCONN from sysctl and file {}. Default: {}", file,
207 somaxconn);
208 }
209 }
210 } catch (Exception e) {
211 if (logger.isDebugEnabled()) {
212 logger.debug("Failed to get SOMAXCONN from sysctl and file {}. Default: {}",
213 file, somaxconn, e);
214 }
215 } finally {
216 if (in != null) {
217 try {
218 in.close();
219 } catch (Exception e) {
220 // Ignored.
221 }
222 }
223 }
224 return somaxconn;
225 }
226 }
227 /**
228 * This will execute <a href ="https://www.freebsd.org/cgi/man.cgi?sysctl(8)">sysctl</a> with the {@code sysctlKey}
229 * which is expected to return the numeric value for for {@code sysctlKey}.
230 * @param sysctlKey The key which the return value corresponds to.
231 * @return The <a href ="https://www.freebsd.org/cgi/man.cgi?sysctl(8)">sysctl</a> value for {@code sysctlKey}.
232 */
233 private static Integer sysctlGetInt(String sysctlKey) throws IOException {
234 Process process = new ProcessBuilder("sysctl", sysctlKey).start();
235 try {
236 // Suppress warnings about resource leaks since the buffered reader is closed below
237 InputStream is = process.getInputStream();
238 InputStreamReader isr = new InputStreamReader(is);
239 BufferedReader br = new BufferedReader(isr);
240 try {
241 String line = br.readLine();
242 if (line != null && line.startsWith(sysctlKey)) {
243 for (int i = line.length() - 1; i > sysctlKey.length(); --i) {
244 if (!Character.isDigit(line.charAt(i))) {
245 return Integer.valueOf(line.substring(i + 1));
246 }
247 }
248 }
249 return null;
250 } finally {
251 br.close();
252 }
253 } finally {
254 // No need of 'null' check because we're initializing
255 // the Process instance in first line. Any exception
256 // raised will directly lead to throwable.
257 process.destroy();
258 }
259 }
260
261 /**
262 * Returns {@code true} if IPv4 should be used even if the system supports both IPv4 and IPv6. Setting this
263 * property to {@code true} will disable IPv6 support. The default value of this property is {@code false}.
264 *
265 * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html">Java SE
266 * networking properties</a>
267 */
268 public static boolean isIpV4StackPreferred() {
269 return IPV4_PREFERRED;
270 }
271
272 /**
273 * Returns {@code true} if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6
274 * address. The default value of this property is {@code false}.
275 *
276 * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html">Java SE
277 * networking properties</a>
278 */
279 public static boolean isIpV6AddressesPreferred() {
280 return IPV6_ADDRESSES_PREFERRED;
281 }
282
283 /**
284 * Creates an byte[] based on an ipAddressString. No error handling is performed here.
285 */
286 public static byte[] createByteArrayFromIpAddressString(String ipAddressString) {
287
288 if (isValidIpV4Address(ipAddressString)) {
289 return validIpV4ToBytes(ipAddressString);
290 }
291
292 if (isValidIpV6Address(ipAddressString)) {
293 if (ipAddressString.charAt(0) == '[') {
294 ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1);
295 }
296
297 int percentPos = ipAddressString.indexOf('%');
298 if (percentPos >= 0) {
299 ipAddressString = ipAddressString.substring(0, percentPos);
300 }
301
302 return getIPv6ByName(ipAddressString, true);
303 }
304 return null;
305 }
306
307 /**
308 * Creates an {@link InetAddress} based on an ipAddressString or might return null if it can't be parsed.
309 * No error handling is performed here.
310 */
311 public static InetAddress createInetAddressFromIpAddressString(String ipAddressString) {
312 if (isValidIpV4Address(ipAddressString)) {
313 byte[] bytes = validIpV4ToBytes(ipAddressString);
314 try {
315 return InetAddress.getByAddress(bytes);
316 } catch (UnknownHostException e) {
317 // Should never happen!
318 throw new IllegalStateException(e);
319 }
320 }
321
322 if (isValidIpV6Address(ipAddressString)) {
323 if (ipAddressString.charAt(0) == '[') {
324 ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1);
325 }
326
327 int percentPos = ipAddressString.indexOf('%');
328 if (percentPos >= 0) {
329 try {
330 int scopeId = Integer.parseInt(ipAddressString.substring(percentPos + 1));
331 ipAddressString = ipAddressString.substring(0, percentPos);
332 byte[] bytes = getIPv6ByName(ipAddressString, true);
333 if (bytes == null) {
334 return null;
335 }
336 try {
337 return Inet6Address.getByAddress(null, bytes, scopeId);
338 } catch (UnknownHostException e) {
339 // Should never happen!
340 throw new IllegalStateException(e);
341 }
342 } catch (NumberFormatException e) {
343 return null;
344 }
345 }
346 byte[] bytes = getIPv6ByName(ipAddressString, true);
347 if (bytes == null) {
348 return null;
349 }
350 try {
351 return InetAddress.getByAddress(bytes);
352 } catch (UnknownHostException e) {
353 // Should never happen!
354 throw new IllegalStateException(e);
355 }
356 }
357 return null;
358 }
359
360 private static int decimalDigit(String str, int pos) {
361 return str.charAt(pos) - '0';
362 }
363
364 private static byte ipv4WordToByte(String ip, int from, int toExclusive) {
365 int ret = decimalDigit(ip, from);
366 from++;
367 if (from == toExclusive) {
368 return (byte) ret;
369 }
370 ret = ret * 10 + decimalDigit(ip, from);
371 from++;
372 if (from == toExclusive) {
373 return (byte) ret;
374 }
375 return (byte) (ret * 10 + decimalDigit(ip, from));
376 }
377
378 // visible for tests
379 static byte[] validIpV4ToBytes(String ip) {
380 int i;
381 return new byte[] {
382 ipv4WordToByte(ip, 0, i = ip.indexOf('.', 1)),
383 ipv4WordToByte(ip, i + 1, i = ip.indexOf('.', i + 2)),
384 ipv4WordToByte(ip, i + 1, i = ip.indexOf('.', i + 2)),
385 ipv4WordToByte(ip, i + 1, ip.length())
386 };
387 }
388
389 /**
390 * Convert {@link Inet4Address} into {@code int}
391 */
392 public static int ipv4AddressToInt(Inet4Address ipAddress) {
393 byte[] octets = ipAddress.getAddress();
394
395 return (octets[0] & 0xff) << 24 |
396 (octets[1] & 0xff) << 16 |
397 (octets[2] & 0xff) << 8 |
398 octets[3] & 0xff;
399 }
400
401 /**
402 * Converts a 32-bit integer into an IPv4 address.
403 */
404 public static String intToIpAddress(int i) {
405 StringBuilder buf = new StringBuilder(15);
406 buf.append(i >> 24 & 0xff);
407 buf.append('.');
408 buf.append(i >> 16 & 0xff);
409 buf.append('.');
410 buf.append(i >> 8 & 0xff);
411 buf.append('.');
412 buf.append(i & 0xff);
413 return buf.toString();
414 }
415
416 /**
417 * Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.
418 *
419 * @throws IllegalArgumentException
420 * if {@code length} is not {@code 4} nor {@code 16}
421 */
422 public static String bytesToIpAddress(byte[] bytes) {
423 return bytesToIpAddress(bytes, 0, bytes.length);
424 }
425
426 /**
427 * Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.
428 *
429 * @throws IllegalArgumentException
430 * if {@code length} is not {@code 4} nor {@code 16}
431 */
432 public static String bytesToIpAddress(byte[] bytes, int offset, int length) {
433 switch (length) {
434 case 4: {
435 return new StringBuilder(15)
436 .append(bytes[offset] & 0xff)
437 .append('.')
438 .append(bytes[offset + 1] & 0xff)
439 .append('.')
440 .append(bytes[offset + 2] & 0xff)
441 .append('.')
442 .append(bytes[offset + 3] & 0xff).toString();
443 }
444 case 16:
445 return toAddressString(bytes, offset, false);
446 default:
447 throw new IllegalArgumentException("length: " + length + " (expected: 4 or 16)");
448 }
449 }
450
451 public static boolean isValidIpV6Address(String ip) {
452 return isValidIpV6Address((CharSequence) ip);
453 }
454
455 public static boolean isValidIpV6Address(CharSequence ip) {
456 int end = ip.length();
457 if (end < 2) {
458 return false;
459 }
460
461 // strip "[]"
462 int start;
463 char c = ip.charAt(0);
464 if (c == '[') {
465 end--;
466 if (ip.charAt(end) != ']') {
467 // must have a close ]
468 return false;
469 }
470 start = 1;
471 c = ip.charAt(1);
472 } else {
473 start = 0;
474 }
475
476 int colons;
477 int compressBegin;
478 if (c == ':') {
479 // an IPv6 address can start with "::" or with a number
480 if (ip.charAt(start + 1) != ':') {
481 return false;
482 }
483 colons = 2;
484 compressBegin = start;
485 start += 2;
486 } else {
487 colons = 0;
488 compressBegin = -1;
489 }
490
491 int wordLen = 0;
492 loop:
493 for (int i = start; i < end; i++) {
494 c = ip.charAt(i);
495 if (isValidHexChar(c)) {
496 if (wordLen < 4) {
497 wordLen++;
498 continue;
499 }
500 return false;
501 }
502
503 switch (c) {
504 case ':':
505 if (colons > 7) {
506 return false;
507 }
508 if (ip.charAt(i - 1) == ':') {
509 if (compressBegin >= 0) {
510 return false;
511 }
512 compressBegin = i - 1;
513 } else {
514 wordLen = 0;
515 }
516 colons++;
517 break;
518 case '.':
519 // case for the last 32-bits represented as IPv4 x:x:x:x:x:x:d.d.d.d
520
521 // check a normal case (6 single colons)
522 if (compressBegin < 0 && colons != 6 ||
523 // a special case ::1:2:3:4:5:d.d.d.d allows 7 colons with an
524 // IPv4 ending, otherwise 7 :'s is bad
525 (colons == 7 && compressBegin >= start || colons > 7)) {
526 return false;
527 }
528
529 // Verify this address is of the correct structure to contain an IPv4 address.
530 // It must be IPv4-Mapped or IPv4-Compatible
531 // (see https://tools.ietf.org/html/rfc4291#section-2.5.5).
532 int ipv4Start = i - wordLen;
533 int j = ipv4Start - 2; // index of character before the previous ':'.
534 if (isValidIPv4MappedChar(ip.charAt(j))) {
535 if (!isValidIPv4MappedChar(ip.charAt(j - 1)) ||
536 !isValidIPv4MappedChar(ip.charAt(j - 2)) ||
537 !isValidIPv4MappedChar(ip.charAt(j - 3))) {
538 return false;
539 }
540 j -= 5;
541 }
542
543 for (; j >= start; --j) {
544 char tmpChar = ip.charAt(j);
545 if (tmpChar != '0' && tmpChar != ':') {
546 return false;
547 }
548 }
549
550 // 7 - is minimum IPv4 address length
551 int ipv4End = indexOf(ip, '%', ipv4Start + 7);
552 if (ipv4End < 0) {
553 ipv4End = end;
554 }
555 return isValidIpV4Address(ip, ipv4Start, ipv4End);
556 case '%':
557 // strip the interface name/index after the percent sign
558 end = i;
559 break loop;
560 default:
561 return false;
562 }
563 }
564
565 // normal case without compression
566 if (compressBegin < 0) {
567 return colons == 7 && wordLen > 0;
568 }
569
570 return compressBegin + 2 == end ||
571 // 8 colons is valid only if compression in start or end
572 wordLen > 0 && (colons < 8 || compressBegin <= start);
573 }
574
575 private static boolean isValidIpV4Word(CharSequence word, int from, int toExclusive) {
576 int len = toExclusive - from;
577 char c0, c1, c2;
578 if (len < 1 || len > 3 || (c0 = word.charAt(from)) < '0') {
579 return false;
580 }
581 if (len == 3) {
582 return (c1 = word.charAt(from + 1)) >= '0' &&
583 (c2 = word.charAt(from + 2)) >= '0' &&
584 (c0 <= '1' && c1 <= '9' && c2 <= '9' ||
585 c0 == '2' && c1 <= '5' && (c2 <= '5' || c1 < '5' && c2 <= '9'));
586 }
587 return c0 <= '9' && (len == 1 || isValidNumericChar(word.charAt(from + 1)));
588 }
589
590 private static boolean isValidHexChar(char c) {
591 return c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';
592 }
593
594 private static boolean isValidNumericChar(char c) {
595 return c >= '0' && c <= '9';
596 }
597
598 private static boolean isValidIPv4MappedChar(char c) {
599 return c == 'f' || c == 'F';
600 }
601
602 private static boolean isValidIPv4MappedSeparators(byte b0, byte b1, boolean mustBeZero) {
603 // We allow IPv4 Mapped (https://tools.ietf.org/html/rfc4291#section-2.5.5.1)
604 // and IPv4 compatible (https://tools.ietf.org/html/rfc4291#section-2.5.5.1).
605 // The IPv4 compatible is deprecated, but it allows parsing of plain IPv4 addressed into IPv6-Mapped addresses.
606 return b0 == b1 && (b0 == 0 || !mustBeZero && b1 == -1);
607 }
608
609 private static boolean isValidIPv4Mapped(byte[] bytes, int currentIndex, int compressBegin, int compressLength) {
610 final boolean mustBeZero = compressBegin + compressLength >= 14;
611 return currentIndex <= 12 && currentIndex >= 2 && (!mustBeZero || compressBegin < 12) &&
612 isValidIPv4MappedSeparators(bytes[currentIndex - 1], bytes[currentIndex - 2], mustBeZero) &&
613 PlatformDependent.isZero(bytes, 0, currentIndex - 3);
614 }
615
616 /**
617 * Takes a {@link CharSequence} and parses it to see if it is a valid IPV4 address.
618 *
619 * @return true, if the string represents an IPV4 address in dotted
620 * notation, false otherwise
621 */
622 public static boolean isValidIpV4Address(CharSequence ip) {
623 return isValidIpV4Address(ip, 0, ip.length());
624 }
625
626 /**
627 * Takes a {@link String} and parses it to see if it is a valid IPV4 address.
628 *
629 * @return true, if the string represents an IPV4 address in dotted
630 * notation, false otherwise
631 */
632 public static boolean isValidIpV4Address(String ip) {
633 return isValidIpV4Address(ip, 0, ip.length());
634 }
635
636 private static boolean isValidIpV4Address(CharSequence ip, int from, int toExcluded) {
637 return ip instanceof String ? isValidIpV4Address((String) ip, from, toExcluded) :
638 ip instanceof AsciiString ? isValidIpV4Address((AsciiString) ip, from, toExcluded) :
639 isValidIpV4Address0(ip, from, toExcluded);
640 }
641
642 @SuppressWarnings("DuplicateBooleanBranch")
643 private static boolean isValidIpV4Address(String ip, int from, int toExcluded) {
644 int len = toExcluded - from;
645 int i;
646 return len <= 15 && len >= 7 &&
647 (i = ip.indexOf('.', from + 1)) > 0 && isValidIpV4Word(ip, from, i) &&
648 (i = ip.indexOf('.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) &&
649 (i = ip.indexOf('.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) &&
650 isValidIpV4Word(ip, i + 1, toExcluded);
651 }
652
653 @SuppressWarnings("DuplicateBooleanBranch")
654 private static boolean isValidIpV4Address(AsciiString ip, int from, int toExcluded) {
655 int len = toExcluded - from;
656 int i;
657 return len <= 15 && len >= 7 &&
658 (i = ip.indexOf('.', from + 1)) > 0 && isValidIpV4Word(ip, from, i) &&
659 (i = ip.indexOf('.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) &&
660 (i = ip.indexOf('.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) &&
661 isValidIpV4Word(ip, i + 1, toExcluded);
662 }
663
664 @SuppressWarnings("DuplicateBooleanBranch")
665 private static boolean isValidIpV4Address0(CharSequence ip, int from, int toExcluded) {
666 int len = toExcluded - from;
667 int i;
668 return len <= 15 && len >= 7 &&
669 (i = indexOf(ip, '.', from + 1)) > 0 && isValidIpV4Word(ip, from, i) &&
670 (i = indexOf(ip, '.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) &&
671 (i = indexOf(ip, '.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) &&
672 isValidIpV4Word(ip, i + 1, toExcluded);
673 }
674
675 /**
676 * Returns the {@link Inet6Address} representation of a {@link CharSequence} IP address.
677 * <p>
678 * This method will treat all IPv4 type addresses as "IPv4 mapped" (see {@link #getByName(CharSequence, boolean)})
679 * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address}
680 * @return {@link Inet6Address} representation of the {@code ip} or {@code null} if not a valid IP address.
681 */
682 public static Inet6Address getByName(CharSequence ip) {
683 return getByName(ip, true);
684 }
685
686 /**
687 * Returns the {@link Inet6Address} representation of a {@link CharSequence} IP address.
688 * <p>
689 * The {@code ipv4Mapped} parameter specifies how IPv4 addresses should be treated.
690 * "IPv4 mapped" format as
691 * defined in <a href="https://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> is supported.
692 * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address}
693 * @param ipv4Mapped
694 * <ul>
695 * <li>{@code true} To allow IPv4 mapped inputs to be translated into {@link Inet6Address}</li>
696 * <li>{@code false} Consider IPv4 mapped addresses as invalid.</li>
697 * </ul>
698 * @return {@link Inet6Address} representation of the {@code ip} or {@code null} if not a valid IP address.
699 */
700 public static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped) {
701 byte[] bytes = getIPv6ByName(ip, ipv4Mapped);
702 if (bytes == null) {
703 return null;
704 }
705 try {
706 return Inet6Address.getByAddress(null, bytes, -1);
707 } catch (UnknownHostException e) {
708 throw new RuntimeException(e); // Should never happen
709 }
710 }
711
712 /**
713 * Returns the byte array representation of a {@link CharSequence} IP address.
714 * <p>
715 * The {@code ipv4Mapped} parameter specifies how IPv4 addresses should be treated.
716 * "IPv4 mapped" format as
717 * defined in <a href="https://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> is supported.
718 * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address}
719 * @param ipv4Mapped
720 * <ul>
721 * <li>{@code true} To allow IPv4 mapped inputs to be translated into {@link Inet6Address}</li>
722 * <li>{@code false} Consider IPv4 mapped addresses as invalid.</li>
723 * </ul>
724 * @return byte array representation of the {@code ip} or {@code null} if not a valid IP address.
725 */
726 // visible for test
727 static byte[] getIPv6ByName(CharSequence ip, boolean ipv4Mapped) {
728 final byte[] bytes = new byte[IPV6_BYTE_COUNT];
729 final int ipLength = ip.length();
730 int compressBegin = 0;
731 int compressLength = 0;
732 int currentIndex = 0;
733 int value = 0;
734 int begin = -1;
735 int i = 0;
736 int ipv6Separators = 0;
737 int ipv4Separators = 0;
738 int tmp;
739 for (; i < ipLength; ++i) {
740 final char c = ip.charAt(i);
741 switch (c) {
742 case ':':
743 ++ipv6Separators;
744 if (i - begin > IPV6_MAX_CHAR_BETWEEN_SEPARATOR ||
745 ipv4Separators > 0 || ipv6Separators > IPV6_MAX_SEPARATORS ||
746 currentIndex + 1 >= bytes.length) {
747 return null;
748 }
749 value <<= (IPV6_MAX_CHAR_BETWEEN_SEPARATOR - (i - begin)) << 2;
750
751 if (compressLength > 0) {
752 compressLength -= 2;
753 }
754
755 // The value integer holds at most 4 bytes from right (most significant) to left (least significant).
756 // The following bit shifting is used to extract and re-order the individual bytes to achieve a
757 // left (most significant) to right (least significant) ordering.
758 bytes[currentIndex++] = (byte) (((value & 0xf) << 4) | ((value >> 4) & 0xf));
759 bytes[currentIndex++] = (byte) ((((value >> 8) & 0xf) << 4) | ((value >> 12) & 0xf));
760 tmp = i + 1;
761 if (tmp < ipLength && ip.charAt(tmp) == ':') {
762 ++tmp;
763 if (compressBegin != 0 || (tmp < ipLength && ip.charAt(tmp) == ':')) {
764 return null;
765 }
766 ++ipv6Separators;
767 compressBegin = currentIndex;
768 compressLength = bytes.length - compressBegin - 2;
769 ++i;
770 }
771 value = 0;
772 begin = -1;
773 break;
774 case '.':
775 ++ipv4Separators;
776 tmp = i - begin; // tmp is the length of the current segment.
777 if (tmp > IPV4_MAX_CHAR_BETWEEN_SEPARATOR
778 || begin < 0
779 || ipv4Separators > IPV4_SEPARATORS
780 || (ipv6Separators > 0 && (currentIndex + compressLength < 12))
781 || i + 1 >= ipLength
782 || currentIndex >= bytes.length
783 || ipv4Separators == 1 &&
784 // We also parse pure IPv4 addresses as IPv4-Mapped for ease of use.
785 ((!ipv4Mapped || currentIndex != 0 && !isValidIPv4Mapped(bytes, currentIndex,
786 compressBegin, compressLength)) ||
787 (tmp == 3 && (!isValidNumericChar(ip.charAt(i - 1)) ||
788 !isValidNumericChar(ip.charAt(i - 2)) ||
789 !isValidNumericChar(ip.charAt(i - 3))) ||
790 tmp == 2 && (!isValidNumericChar(ip.charAt(i - 1)) ||
791 !isValidNumericChar(ip.charAt(i - 2))) ||
792 tmp == 1 && !isValidNumericChar(ip.charAt(i - 1))))) {
793 return null;
794 }
795 value <<= (IPV4_MAX_CHAR_BETWEEN_SEPARATOR - tmp) << 2;
796
797 // The value integer holds at most 3 bytes from right (most significant) to left (least significant).
798 // The following bit shifting is to restructure the bytes to be left (most significant) to
799 // right (least significant) while also accounting for each IPv4 digit is base 10.
800 begin = (value & 0xf) * 100 + ((value >> 4) & 0xf) * 10 + ((value >> 8) & 0xf);
801 if (begin > 255) {
802 return null;
803 }
804 bytes[currentIndex++] = (byte) begin;
805 value = 0;
806 begin = -1;
807 break;
808 default:
809 if (!isValidHexChar(c) || (ipv4Separators > 0 && !isValidNumericChar(c))) {
810 return null;
811 }
812 if (begin < 0) {
813 begin = i;
814 } else if (i - begin > IPV6_MAX_CHAR_BETWEEN_SEPARATOR) {
815 return null;
816 }
817 // The value is treated as a sort of array of numbers because we are dealing with
818 // at most 4 consecutive bytes we can use bit shifting to accomplish this.
819 // The most significant byte will be encountered first, and reside in the right most
820 // position of the following integer
821 value += StringUtil.decodeHexNibble(c) << ((i - begin) << 2);
822 break;
823 }
824 }
825
826 final boolean isCompressed = compressBegin > 0;
827 // Finish up last set of data that was accumulated in the loop (or before the loop)
828 if (ipv4Separators > 0) {
829 if (begin > 0 && i - begin > IPV4_MAX_CHAR_BETWEEN_SEPARATOR ||
830 ipv4Separators != IPV4_SEPARATORS ||
831 currentIndex >= bytes.length) {
832 return null;
833 }
834 if (!(ipv6Separators == 0 || ipv6Separators >= IPV6_MIN_SEPARATORS &&
835 (!isCompressed && (ipv6Separators == 6 && ip.charAt(0) != ':') ||
836 isCompressed && (ipv6Separators < IPV6_MAX_SEPARATORS &&
837 (ip.charAt(0) != ':' || compressBegin <= 2))))) {
838 return null;
839 }
840 value <<= (IPV4_MAX_CHAR_BETWEEN_SEPARATOR - (i - begin)) << 2;
841
842 // The value integer holds at most 3 bytes from right (most significant) to left (least significant).
843 // The following bit shifting is to restructure the bytes to be left (most significant) to
844 // right (least significant) while also accounting for each IPv4 digit is base 10.
845 begin = (value & 0xf) * 100 + ((value >> 4) & 0xf) * 10 + ((value >> 8) & 0xf);
846 if (begin > 255) {
847 return null;
848 }
849 bytes[currentIndex++] = (byte) begin;
850 } else {
851 tmp = ipLength - 1;
852 if (begin > 0 && i - begin > IPV6_MAX_CHAR_BETWEEN_SEPARATOR ||
853 ipv6Separators < IPV6_MIN_SEPARATORS ||
854 !isCompressed && (ipv6Separators + 1 != IPV6_MAX_SEPARATORS ||
855 ip.charAt(0) == ':' || ip.charAt(tmp) == ':') ||
856 isCompressed && (ipv6Separators > IPV6_MAX_SEPARATORS ||
857 (ipv6Separators == IPV6_MAX_SEPARATORS &&
858 (compressBegin <= 2 && ip.charAt(0) != ':' ||
859 compressBegin >= 14 && ip.charAt(tmp) != ':'))) ||
860 currentIndex + 1 >= bytes.length ||
861 begin < 0 && ip.charAt(tmp - 1) != ':' ||
862 compressBegin > 2 && ip.charAt(0) == ':') {
863 return null;
864 }
865 if (begin >= 0 && i - begin <= IPV6_MAX_CHAR_BETWEEN_SEPARATOR) {
866 value <<= (IPV6_MAX_CHAR_BETWEEN_SEPARATOR - (i - begin)) << 2;
867 }
868 // The value integer holds at most 4 bytes from right (most significant) to left (least significant).
869 // The following bit shifting is used to extract and re-order the individual bytes to achieve a
870 // left (most significant) to right (least significant) ordering.
871 bytes[currentIndex++] = (byte) (((value & 0xf) << 4) | ((value >> 4) & 0xf));
872 bytes[currentIndex++] = (byte) ((((value >> 8) & 0xf) << 4) | ((value >> 12) & 0xf));
873 }
874
875 if (currentIndex < bytes.length) {
876 int toBeCopiedLength = currentIndex - compressBegin;
877 int targetIndex = bytes.length - toBeCopiedLength;
878 System.arraycopy(bytes, compressBegin, bytes, targetIndex, toBeCopiedLength);
879 // targetIndex is also the `toIndex` to fill 0
880 Arrays.fill(bytes, compressBegin, targetIndex, (byte) 0);
881 }
882
883 if (ipv4Separators > 0) {
884 // We only support IPv4-Mapped addresses [1] because IPv4-Compatible addresses are deprecated [2].
885 // [1] https://tools.ietf.org/html/rfc4291#section-2.5.5.2
886 // [2] https://tools.ietf.org/html/rfc4291#section-2.5.5.1
887 bytes[10] = bytes[11] = (byte) 0xff;
888 }
889
890 return bytes;
891 }
892
893 /**
894 * Returns the {@link String} representation of an {@link InetSocketAddress}.
895 * <p>
896 * The output does not include Scope ID.
897 * @param addr {@link InetSocketAddress} to be converted to an address string
898 * @return {@code String} containing the text-formatted IP address
899 */
900 public static String toSocketAddressString(InetSocketAddress addr) {
901 String port = String.valueOf(addr.getPort());
902 final StringBuilder sb;
903
904 if (addr.isUnresolved()) {
905 String hostname = getHostname(addr);
906 sb = newSocketAddressStringBuilder(hostname, port, !isValidIpV6Address(hostname));
907 } else {
908 InetAddress address = addr.getAddress();
909 String hostString = toAddressString(address);
910 sb = newSocketAddressStringBuilder(hostString, port, address instanceof Inet4Address);
911 }
912 return sb.append(':').append(port).toString();
913 }
914
915 /**
916 * Returns the {@link String} representation of a host port combo.
917 */
918 public static String toSocketAddressString(String host, int port) {
919 String portStr = String.valueOf(port);
920 return newSocketAddressStringBuilder(
921 host, portStr, !isValidIpV6Address(host)).append(':').append(portStr).toString();
922 }
923
924 private static StringBuilder newSocketAddressStringBuilder(String host, String port, boolean ipv4) {
925 int hostLen = host.length();
926 if (ipv4) {
927 // Need to include enough space for hostString:port.
928 return new StringBuilder(hostLen + 1 + port.length()).append(host);
929 }
930 // Need to include enough space for [hostString]:port.
931 StringBuilder stringBuilder = new StringBuilder(hostLen + 3 + port.length());
932 if (hostLen > 1 && host.charAt(0) == '[' && host.charAt(hostLen - 1) == ']') {
933 return stringBuilder.append(host);
934 }
935 return stringBuilder.append('[').append(host).append(']');
936 }
937
938 /**
939 * Returns the {@link String} representation of an {@link InetAddress}.
940 * <ul>
941 * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li>
942 * <li>Inet6Address results adhere to
943 * <a href="https://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li>
944 * </ul>
945 * <p>
946 * The output does not include Scope ID.
947 * @param ip {@link InetAddress} to be converted to an address string
948 * @return {@code String} containing the text-formatted IP address
949 */
950 public static String toAddressString(InetAddress ip) {
951 return toAddressString(ip, false);
952 }
953
954 /**
955 * Returns the {@link String} representation of an {@link InetAddress}.
956 * <ul>
957 * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li>
958 * <li>Inet6Address results adhere to
959 * <a href="https://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a> if
960 * {@code ipv4Mapped} is false. If {@code ipv4Mapped} is true then "IPv4 mapped" format
961 * from <a href="https://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> will be supported.
962 * The compressed result will always obey the compression rules defined in
963 * <a href="https://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li>
964 * </ul>
965 * <p>
966 * The output does not include Scope ID.
967 * @param ip {@link InetAddress} to be converted to an address string
968 * @param ipv4Mapped
969 * <ul>
970 * <li>{@code true} to stray from strict rfc 5952 and support the "IPv4 mapped" format
971 * defined in <a href="https://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> while still
972 * following the updated guidelines in
973 * <a href="https://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li>
974 * <li>{@code false} to strictly follow rfc 5952</li>
975 * </ul>
976 * @return {@code String} containing the text-formatted IP address
977 */
978 public static String toAddressString(InetAddress ip, boolean ipv4Mapped) {
979 if (ip instanceof Inet4Address) {
980 return ip.getHostAddress();
981 }
982 if (!(ip instanceof Inet6Address)) {
983 throw new IllegalArgumentException("Unhandled type: " + ip);
984 }
985
986 return toAddressString(ip.getAddress(), 0, ipv4Mapped);
987 }
988
989 private static String toAddressString(byte[] bytes, int offset, boolean ipv4Mapped) {
990 final int[] words = new int[IPV6_WORD_COUNT];
991 for (int i = 0; i < words.length; ++i) {
992 int idx = (i << 1) + offset;
993 words[i] = ((bytes[idx] & 0xff) << 8) | (bytes[idx + 1] & 0xff);
994 }
995
996 // Find longest run of 0s, tie goes to first found instance
997 int currentStart = -1;
998 int currentLength;
999 int shortestStart = -1;
1000 int shortestLength = 0;
1001 for (int i = 0; i < words.length; ++i) {
1002 if (words[i] == 0) {
1003 if (currentStart < 0) {
1004 currentStart = i;
1005 }
1006 } else if (currentStart >= 0) {
1007 currentLength = i - currentStart;
1008 if (currentLength > shortestLength) {
1009 shortestStart = currentStart;
1010 shortestLength = currentLength;
1011 }
1012 currentStart = -1;
1013 }
1014 }
1015 // If the array ends on a streak of zeros, make sure we account for it
1016 if (currentStart >= 0) {
1017 currentLength = words.length - currentStart;
1018 if (currentLength > shortestLength) {
1019 shortestStart = currentStart;
1020 shortestLength = currentLength;
1021 }
1022 }
1023 // Ignore the longest streak if it is only 1 long
1024 if (shortestLength == 1) {
1025 shortestLength = 0;
1026 shortestStart = -1;
1027 }
1028
1029 // Translate to string taking into account longest consecutive 0s
1030 final int shortestEnd = shortestStart + shortestLength;
1031 final StringBuilder b = new StringBuilder(IPV6_MAX_CHAR_COUNT);
1032 if (shortestEnd < 0) { // Optimization when there is no compressing needed
1033 b.append(Integer.toHexString(words[0]));
1034 for (int i = 1; i < words.length; ++i) {
1035 b.append(':');
1036 b.append(Integer.toHexString(words[i]));
1037 }
1038 } else { // General case that can handle compressing (and not compressing)
1039 // Loop unroll the first index (so we don't constantly check i==0 cases in loop)
1040 final boolean isIpv4Mapped;
1041 if (inRangeEndExclusive(0, shortestStart, shortestEnd)) {
1042 b.append("::");
1043 isIpv4Mapped = ipv4Mapped && (shortestEnd == 5 && words[5] == 0xffff);
1044 } else {
1045 b.append(Integer.toHexString(words[0]));
1046 isIpv4Mapped = false;
1047 }
1048 for (int i = 1; i < words.length; ++i) {
1049 if (!inRangeEndExclusive(i, shortestStart, shortestEnd)) {
1050 if (!inRangeEndExclusive(i - 1, shortestStart, shortestEnd)) {
1051 // If the last index was not part of the shortened sequence
1052 if (!isIpv4Mapped || i == 6) {
1053 b.append(':');
1054 } else {
1055 b.append('.');
1056 }
1057 }
1058 if (isIpv4Mapped && i > 5) {
1059 b.append(words[i] >> 8);
1060 b.append('.');
1061 b.append(words[i] & 0xff);
1062 } else {
1063 b.append(Integer.toHexString(words[i]));
1064 }
1065 } else if (!inRangeEndExclusive(i - 1, shortestStart, shortestEnd)) {
1066 // If we are in the shortened sequence and the last index was not
1067 b.append("::");
1068 }
1069 }
1070 }
1071
1072 return b.toString();
1073 }
1074
1075 /**
1076 * Returns {@link InetSocketAddress#getHostString()} if Java >= 7,
1077 * or {@link InetSocketAddress#getHostName()} otherwise.
1078 * @param addr The address
1079 * @return the host string
1080 */
1081 public static String getHostname(InetSocketAddress addr) {
1082 return PlatformDependent.javaVersion() >= 7 ? addr.getHostString() : addr.getHostName();
1083 }
1084
1085 /**
1086 * Does a range check on {@code value} if is within {@code start} (inclusive) and {@code end} (exclusive).
1087 * @param value The value to checked if is within {@code start} (inclusive) and {@code end} (exclusive)
1088 * @param start The start of the range (inclusive)
1089 * @param end The end of the range (exclusive)
1090 * @return
1091 * <ul>
1092 * <li>{@code true} if {@code value} if is within {@code start} (inclusive) and {@code end} (exclusive)</li>
1093 * <li>{@code false} otherwise</li>
1094 * </ul>
1095 */
1096 private static boolean inRangeEndExclusive(int value, int start, int end) {
1097 return value >= start && value < end;
1098 }
1099
1100 /**
1101 * A constructor to stop this class being constructed.
1102 */
1103 private NetUtil() {
1104 // Unused
1105 }
1106 }