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  *   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, 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.internal.PlatformDependent;
19 import io.netty.util.internal.SocketUtils;
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.SocketException;
37 import java.net.UnknownHostException;
38 import java.security.AccessController;
39 import java.security.PrivilegedAction;
40 import java.util.ArrayList;
41 import java.util.Enumeration;
42 import java.util.List;
43
44 import static io.netty.util.AsciiString.indexOf;
45
46 /**
47  * A class that holds a number of network-related constants.
48  * <p/>
49  * This class borrowed some of its methods from a  modified fork of the
50  * <a href="http://svn.apache.org/repos/asf/harmony/enhanced/java/branches/java6/classlib/modules/luni/
51  * src/main/java/org/apache/harmony/luni/util/Inet6Util.java">Inet6Util class</a> which was part of Apache Harmony.
52  */

53 public final class NetUtil {
54
55     /**
56      * The {@link Inet4Address} that represents the IPv4 loopback address '127.0.0.1'
57      */

58     public static final Inet4Address LOCALHOST4;
59
60     /**
61      * The {@link Inet6Address} that represents the IPv6 loopback address '::1'
62      */

63     public static final Inet6Address LOCALHOST6;
64
65     /**
66      * The {@link InetAddress} that represents the loopback address. If IPv6 stack is available, it will refer to
67      * {@link #LOCALHOST6}.  Otherwise, {@link #LOCALHOST4}.
68      */

69     public static final InetAddress LOCALHOST;
70
71     /**
72      * The loopback {@link NetworkInterface} of the current machine
73      */

74     public static final NetworkInterface LOOPBACK_IF;
75
76     /**
77      * The SOMAXCONN value of the current machine.  If failed to get the value,  {@code 200}  is used as a
78      * default value for Windows or {@code 128} for others.
79      */

80     public static final int SOMAXCONN;
81
82     /**
83      * This defines how many words (represented as ints) are needed to represent an IPv6 address
84      */

85     private static final int IPV6_WORD_COUNT = 8;
86
87     /**
88      * The maximum number of characters for an IPV6 string with no scope
89      */

90     private static final int IPV6_MAX_CHAR_COUNT = 39;
91
92     /**
93      * Number of bytes needed to represent and IPV6 value
94      */

95     private static final int IPV6_BYTE_COUNT = 16;
96
97     /**
98      * Maximum amount of value adding characters in between IPV6 separators
99      */

100     private static final int IPV6_MAX_CHAR_BETWEEN_SEPARATOR = 4;
101
102     /**
103      * Minimum number of separators that must be present in an IPv6 string
104      */

105     private static final int IPV6_MIN_SEPARATORS = 2;
106
107     /**
108      * Maximum number of separators that must be present in an IPv6 string
109      */

110     private static final int IPV6_MAX_SEPARATORS = 8;
111
112     /**
113      * Maximum amount of value adding characters in between IPV4 separators
114      */

115     private static final int IPV4_MAX_CHAR_BETWEEN_SEPARATOR = 3;
116
117     /**
118      * Number of separators that must be present in an IPv4 string
119      */

120     private static final int IPV4_SEPARATORS = 3;
121
122     /**
123      * {@code trueif IPv4 should be used even if the system supports both IPv4 and IPv6.
124      */

125     private static final boolean IPV4_PREFERRED = SystemPropertyUtil.getBoolean("java.net.preferIPv4Stack"false);
126
127     /**
128      * {@code trueif an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address.
129      */

130     private static final boolean IPV6_ADDRESSES_PREFERRED =
131             SystemPropertyUtil.getBoolean("java.net.preferIPv6Addresses"false);
132
133     /**
134      * The logger being used by this class
135      */

136     private static final InternalLogger logger = InternalLoggerFactory.getInstance(NetUtil.class);
137
138     static {
139         logger.debug("-Djava.net.preferIPv4Stack: {}", IPV4_PREFERRED);
140         logger.debug("-Djava.net.preferIPv6Addresses: {}", IPV6_ADDRESSES_PREFERRED);
141
142         byte[] LOCALHOST4_BYTES = {127, 0, 0, 1};
143         byte[] LOCALHOST6_BYTES = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
144
145         // Create IPv4 loopback address.
146         Inet4Address localhost4 = null;
147         try {
148             localhost4 = (Inet4Address) InetAddress.getByAddress("localhost", LOCALHOST4_BYTES);
149         } catch (Exception e) {
150             // We should not get here as long as the length of the address is correct.
151             PlatformDependent.throwException(e);
152         }
153         LOCALHOST4 = localhost4;
154
155         // Create IPv6 loopback address.
156         Inet6Address localhost6 = null;
157         try {
158             localhost6 = (Inet6Address) InetAddress.getByAddress("localhost", LOCALHOST6_BYTES);
159         } catch (Exception e) {
160             // We should not get here as long as the length of the address is correct.
161             PlatformDependent.throwException(e);
162         }
163         LOCALHOST6 = localhost6;
164
165         // Retrieve the list of available network interfaces.
166         List<NetworkInterface> ifaces = new ArrayList<NetworkInterface>();
167         try {
168             Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
169             if (interfaces != null) {
170                 while (interfaces.hasMoreElements()) {
171                     NetworkInterface iface = interfaces.nextElement();
172                     // Use the interface with proper INET addresses only.
173                     if (SocketUtils.addressesFromNetworkInterface(iface).hasMoreElements()) {
174                         ifaces.add(iface);
175                     }
176                 }
177             }
178         } catch (SocketException e) {
179             logger.warn("Failed to retrieve the list of available network interfaces", e);
180         }
181
182         // Find the first loopback interface available from its INET address (127.0.0.1 or ::1)
183         // Note that we do not use NetworkInterface.isLoopback() in the first place because it takes long time
184         // on a certain environment. (e.g. Windows with -Djava.net.preferIPv4Stack=true)
185         NetworkInterface loopbackIface = null;
186         InetAddress loopbackAddr = null;
187         loop: for (NetworkInterface iface: ifaces) {
188             for (Enumeration<InetAddress> i = SocketUtils.addressesFromNetworkInterface(iface); i.hasMoreElements();) {
189                 InetAddress addr = i.nextElement();
190                 if (addr.isLoopbackAddress()) {
191                     // Found
192                     loopbackIface = iface;
193                     loopbackAddr = addr;
194                     break loop;
195                 }
196             }
197         }
198
199         // If failed to find the loopback interface from its INET address, fall back to isLoopback().
200         if (loopbackIface == null) {
201             try {
202                 for (NetworkInterface iface: ifaces) {
203                     if (iface.isLoopback()) {
204                         Enumeration<InetAddress> i = SocketUtils.addressesFromNetworkInterface(iface);
205                         if (i.hasMoreElements()) {
206                             // Found the one with INET address.
207                             loopbackIface = iface;
208                             loopbackAddr = i.nextElement();
209                             break;
210                         }
211                     }
212                 }
213
214                 if (loopbackIface == null) {
215                     logger.warn("Failed to find the loopback interface");
216                 }
217             } catch (SocketException e) {
218                 logger.warn("Failed to find the loopback interface", e);
219             }
220         }
221
222         if (loopbackIface != null) {
223             // Found the loopback interface with an INET address.
224             logger.debug(
225                     "Loopback interface: {} ({}, {})",
226                     loopbackIface.getName(), loopbackIface.getDisplayName(), loopbackAddr.getHostAddress());
227         } else {
228             // Could not find the loopback interface, but we can't leave LOCALHOST as null.
229             // Use LOCALHOST6 or LOCALHOST4, preferably the IPv6 one.
230             if (loopbackAddr == null) {
231                 try {
232                     if (NetworkInterface.getByInetAddress(LOCALHOST6) != null) {
233                         logger.debug("Using hard-coded IPv6 localhost address: {}", localhost6);
234                         loopbackAddr = localhost6;
235                     }
236                 } catch (Exception e) {
237                     // Ignore
238                 } finally {
239                     if (loopbackAddr == null) {
240                         logger.debug("Using hard-coded IPv4 localhost address: {}", localhost4);
241                         loopbackAddr = localhost4;
242                     }
243                 }
244             }
245         }
246
247         LOOPBACK_IF = loopbackIface;
248         LOCALHOST = loopbackAddr;
249
250         // As a SecurityManager may prevent reading the somaxconn file we wrap this in a privileged block.
251         //
252         // See https://github.com/netty/netty/issues/3680
253         SOMAXCONN = AccessController.doPrivileged(new PrivilegedAction<Integer>() {
254             @Override
255             public Integer run() {
256                 // Determine the default somaxconn (server socket backlog) value of the platform.
257                 // The known defaults:
258                 // - Windows NT Server 4.0+: 200
259                 // - Linux and Mac OS X: 128
260                 int somaxconn = PlatformDependent.isWindows() ? 200 : 128;
261                 File file = new File("/proc/sys/net/core/somaxconn");
262                 BufferedReader in = null;
263                 try {
264                     // file.exists() may throw a SecurityException if a SecurityManager is used, so execute it in the
265                     // try / catch block.
266                     // See https://github.com/netty/netty/issues/4936
267                     if (file.exists()) {
268                         in = new BufferedReader(new FileReader(file));
269                         somaxconn = Integer.parseInt(in.readLine());
270                         if (logger.isDebugEnabled()) {
271                             logger.debug("{}: {}", file, somaxconn);
272                         }
273                     } else {
274                         // Try to get from sysctl
275                         Integer tmp = null;
276                         if (SystemPropertyUtil.getBoolean("io.netty.net.somaxconn.trySysctl"false)) {
277                             tmp = sysctlGetInt("kern.ipc.somaxconn");
278                             if (tmp == null) {
279                                 tmp = sysctlGetInt("kern.ipc.soacceptqueue");
280                                 if (tmp != null) {
281                                     somaxconn = tmp;
282                                 }
283                             } else {
284                                 somaxconn = tmp;
285                             }
286                         }
287
288                         if (tmp == null) {
289                             logger.debug("Failed to get SOMAXCONN from sysctl and file {}. Default: {}", file,
290                                          somaxconn);
291                         }
292                     }
293                 } catch (Exception e) {
294                     if (logger.isDebugEnabled()) {
295                         logger.debug("Failed to get SOMAXCONN from sysctl and file {}. Default: {}",
296                                 file, somaxconn, e);
297                     }
298                 } finally {
299                     if (in != null) {
300                         try {
301                             in.close();
302                         } catch (Exception e) {
303                             // Ignored.
304                         }
305                     }
306                 }
307                 return somaxconn;
308             }
309         });
310     }
311
312     /**
313      * This will execute <a href ="https://www.freebsd.org/cgi/man.cgi?sysctl(8)">sysctl</a> with the {@code sysctlKey}
314      * which is expected to return the numeric value for for {@code sysctlKey}.
315      * @param sysctlKey The key which the return value corresponds to.
316      * @return The <a href ="https://www.freebsd.org/cgi/man.cgi?sysctl(8)">sysctl</a> value for {@code sysctlKey}.
317      */

318     private static Integer sysctlGetInt(String sysctlKey) throws IOException {
319         Process process = new ProcessBuilder("sysctl", sysctlKey).start();
320         try {
321             InputStream is = process.getInputStream();
322             InputStreamReader isr = new InputStreamReader(is);
323             BufferedReader br = new BufferedReader(isr);
324             try {
325                 String line = br.readLine();
326                 if (line != null && line.startsWith(sysctlKey)) {
327                     for (int i = line.length() - 1; i > sysctlKey.length(); --i) {
328                         if (!Character.isDigit(line.charAt(i))) {
329                             return Integer.valueOf(line.substring(i + 1));
330                         }
331                     }
332                 }
333                 return null;
334             } finally {
335                 br.close();
336             }
337         } finally {
338             if (process != null) {
339                 process.destroy();
340             }
341         }
342     }
343
344     /**
345      * Returns {@code trueif IPv4 should be used even if the system supports both IPv4 and IPv6. Setting this
346      * property to {@code true} will disable IPv6 support. The default value of this property is {@code false}.
347      *
348      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html">Java SE
349      *      networking properties</a>
350      */

351     public static boolean isIpV4StackPreferred() {
352         return IPV4_PREFERRED;
353     }
354
355     /**
356      * Returns {@code trueif an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6
357      * address. The default value of this property is {@code false}.
358      *
359      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html">Java SE
360      *      networking properties</a>
361      */

362     public static boolean isIpV6AddressesPreferred() {
363         return IPV6_ADDRESSES_PREFERRED;
364     }
365
366     /**
367      * Creates an byte[] based on an ipAddressString. No error handling is performed here.
368      */

369     public static byte[] createByteArrayFromIpAddressString(String ipAddressString) {
370
371         if (isValidIpV4Address(ipAddressString)) {
372             return validIpV4ToBytes(ipAddressString);
373         }
374
375         if (isValidIpV6Address(ipAddressString)) {
376             if (ipAddressString.charAt(0) == '[') {
377                 ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1);
378             }
379
380             int percentPos = ipAddressString.indexOf('%');
381             if (percentPos >= 0) {
382                 ipAddressString = ipAddressString.substring(0, percentPos);
383             }
384
385             return getIPv6ByName(ipAddressString, true);
386         }
387         return null;
388     }
389
390     private static int decimalDigit(String str, int pos) {
391         return str.charAt(pos) - '0';
392     }
393
394     private static byte ipv4WordToByte(String ip, int from, int toExclusive) {
395         int ret = decimalDigit(ip, from);
396         from++;
397         if (from == toExclusive) {
398             return (byte) ret;
399         }
400         ret = ret * 10 + decimalDigit(ip, from);
401         from++;
402         if (from == toExclusive) {
403             return (byte) ret;
404         }
405         return (byte) (ret * 10 + decimalDigit(ip, from));
406     }
407
408     // visible for tests
409     static byte[] validIpV4ToBytes(String ip) {
410         int i;
411         return new byte[] {
412                 ipv4WordToByte(ip, 0, i = ip.indexOf('.', 1)),
413                 ipv4WordToByte(ip, i + 1, i = ip.indexOf('.', i + 2)),
414                 ipv4WordToByte(ip, i + 1, i = ip.indexOf('.', i + 2)),
415                 ipv4WordToByte(ip, i + 1, ip.length())
416         };
417     }
418
419     /**
420      * Convert {@link Inet4Address} into {@code int}
421      */

422     public static int ipv4AddressToInt(Inet4Address ipAddress) {
423         byte[] octets = ipAddress.getAddress();
424
425         return  (octets[0] & 0xff) << 24 |
426                 (octets[1] & 0xff) << 16 |
427                 (octets[2] & 0xff) << 8 |
428                  octets[3] & 0xff;
429     }
430
431     /**
432      * Converts a 32-bit integer into an IPv4 address.
433      */

434     public static String intToIpAddress(int i) {
435         StringBuilder buf = new StringBuilder(15);
436         buf.append(i >> 24 & 0xff);
437         buf.append('.');
438         buf.append(i >> 16 & 0xff);
439         buf.append('.');
440         buf.append(i >> 8 & 0xff);
441         buf.append('.');
442         buf.append(i & 0xff);
443         return buf.toString();
444     }
445
446     /**
447      * Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.
448      *
449      * @throws IllegalArgumentException
450      *         if {@code length} is not {@code 4} nor {@code 16}
451      */

452     public static String bytesToIpAddress(byte[] bytes) {
453         return bytesToIpAddress(bytes, 0, bytes.length);
454     }
455
456     /**
457      * Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.
458      *
459      * @throws IllegalArgumentException
460      *         if {@code length} is not {@code 4} nor {@code 16}
461      */

462     public static String bytesToIpAddress(byte[] bytes, int offset, int length) {
463         switch (length) {
464             case 4: {
465                 return new StringBuilder(15)
466                         .append(bytes[offset] & 0xff)
467                         .append('.')
468                         .append(bytes[offset + 1] & 0xff)
469                         .append('.')
470                         .append(bytes[offset + 2] & 0xff)
471                         .append('.')
472                         .append(bytes[offset + 3] & 0xff).toString();
473             }
474             case 16:
475                 return toAddressString(bytes, offset, false);
476             default:
477                 throw new IllegalArgumentException("length: " + length + " (expected: 4 or 16)");
478         }
479     }
480
481     public static boolean isValidIpV6Address(String ip) {
482         return isValidIpV6Address((CharSequence) ip);
483     }
484
485     public static boolean isValidIpV6Address(CharSequence ip) {
486         int end = ip.length();
487         if (end < 2) {
488             return false;
489         }
490
491         // strip "[]"
492         int start;
493         char c = ip.charAt(0);
494         if (c == '[') {
495             end--;
496             if (ip.charAt(end) != ']') {
497                 // must have a close ]
498                 return false;
499             }
500             start = 1;
501             c = ip.charAt(1);
502         } else {
503             start = 0;
504         }
505
506         int colons;
507         int compressBegin;
508         if (c == ':') {
509             // an IPv6 address can start with "::" or with a number
510             if (ip.charAt(start + 1) != ':') {
511                 return false;
512             }
513             colons = 2;
514             compressBegin = start;
515             start += 2;
516         } else {
517             colons = 0;
518             compressBegin = -1;
519         }
520
521         int wordLen = 0;
522         loop:
523         for (int i = start; i < end; i++) {
524             c = ip.charAt(i);
525             if (isValidHexChar(c)) {
526                 if (wordLen < 4) {
527                     wordLen++;
528                     continue;
529                 }
530                 return false;
531             }
532
533             switch (c) {
534             case ':':
535                 if (colons > 7) {
536                     return false;
537                 }
538                 if (ip.charAt(i - 1) == ':') {
539                     if (compressBegin >= 0) {
540                         return false;
541                     }
542                     compressBegin = i - 1;
543                 } else {
544                     wordLen = 0;
545                 }
546                 colons++;
547                 break;
548             case '.':
549                 // case for the last 32-bits represented as IPv4 x:x:x:x:x:x:d.d.d.d
550
551                 // check a normal case (6 single colons)
552                 if (compressBegin < 0 && colons != 6 ||
553                     // a special case ::1:2:3:4:5:d.d.d.d allows 7 colons with an
554                     // IPv4 ending, otherwise 7 :'s is bad
555                     (colons == 7 && compressBegin >= start || colons > 7)) {
556                     return false;
557                 }
558
559                 // Verify this address is of the correct structure to contain an IPv4 address.
560                 // It must be IPv4-Mapped or IPv4-Compatible
561                 // (see https://tools.ietf.org/html/rfc4291#section-2.5.5).
562                 int ipv4Start = i - wordLen;
563                 int j = ipv4Start - 2; // index of character before the previous ':'.
564                 if (isValidIPv4MappedChar(ip.charAt(j))) {
565                     if (!isValidIPv4MappedChar(ip.charAt(j - 1)) ||
566                         !isValidIPv4MappedChar(ip.charAt(j - 2)) ||
567                         !isValidIPv4MappedChar(ip.charAt(j - 3))) {
568                         return false;
569                     }
570                     j -= 5;
571                 }
572
573                 for (; j >= start; --j) {
574                     char tmpChar = ip.charAt(j);
575                     if (tmpChar != '0' && tmpChar != ':') {
576                         return false;
577                     }
578                 }
579
580                 // 7 - is minimum IPv4 address length
581                 int ipv4End = indexOf(ip, '%', ipv4Start + 7);
582                 if (ipv4End < 0) {
583                     ipv4End = end;
584                 }
585                 return isValidIpV4Address(ip, ipv4Start, ipv4End);
586             case '%':
587                 // strip the interface name/index after the percent sign
588                 end = i;
589                 break loop;
590             default:
591                 return false;
592             }
593         }
594
595         // normal case without compression
596         if (compressBegin < 0) {
597             return colons == 7 && wordLen > 0;
598         }
599
600         return compressBegin + 2 == end ||
601                // 8 colons is valid only if compression in start or end
602                wordLen > 0 && (colons < 8 || compressBegin <= start);
603     }
604
605     private static boolean isValidIpV4Word(CharSequence word, int from, int toExclusive) {
606         int len = toExclusive - from;
607         char c0, c1, c2;
608         if (len < 1 || len > 3 || (c0 = word.charAt(from)) < '0') {
609             return false;
610         }
611         if (len == 3) {
612             return (c1 = word.charAt(from + 1)) >= '0' &&
613                    (c2 = word.charAt(from + 2)) >= '0' &&
614                    (c0 <= '1' && c1 <= '9' && c2 <= '9' ||
615                     c0 == '2' && c1 <= '5' && (c2 <= '5' || c1 < '5' && c2 <= '9'));
616         }
617         return c0 <= '9' && (len == 1 || isValidNumericChar(word.charAt(from + 1)));
618     }
619
620     private static boolean isValidHexChar(char c) {
621         return c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';
622     }
623
624     private static boolean isValidNumericChar(char c) {
625         return c >= '0' && c <= '9';
626     }
627
628     private static boolean isValidIPv4MappedChar(char c) {
629         return c == 'f' || c == 'F';
630     }
631
632     private static boolean isValidIPv4MappedSeparators(byte b0, byte b1, boolean mustBeZero) {
633         // We allow IPv4 Mapped (https://tools.ietf.org/html/rfc4291#section-2.5.5.1)
634         // and IPv4 compatible (https://tools.ietf.org/html/rfc4291#section-2.5.5.1).
635         // The IPv4 compatible is deprecated, but it allows parsing of plain IPv4 addressed into IPv6-Mapped addresses.
636         return b0 == b1 && (b0 == 0 || !mustBeZero && b1 == -1);
637     }
638
639     private static boolean isValidIPv4Mapped(byte[] bytes, int currentIndex, int compressBegin, int compressLength) {
640         final boolean mustBeZero = compressBegin + compressLength >= 14;
641         return currentIndex <= 12 && currentIndex >= 2 && (!mustBeZero || compressBegin < 12) &&
642                 isValidIPv4MappedSeparators(bytes[currentIndex - 1], bytes[currentIndex - 2], mustBeZero) &&
643                 PlatformDependent.isZero(bytes, 0, currentIndex - 3);
644     }
645
646     /**
647      * Takes a {@link CharSequence} and parses it to see if it is a valid IPV4 address.
648      *
649      * @return trueif the string represents an IPV4 address in dotted
650      *         notation, false otherwise
651      */

652     public static boolean isValidIpV4Address(CharSequence ip) {
653         return isValidIpV4Address(ip, 0, ip.length());
654     }
655
656     /**
657      * Takes a {@link String} and parses it to see if it is a valid IPV4 address.
658      *
659      * @return trueif the string represents an IPV4 address in dotted
660      *         notation, false otherwise
661      */

662     public static boolean isValidIpV4Address(String ip) {
663         return isValidIpV4Address(ip, 0, ip.length());
664     }
665
666     private static boolean isValidIpV4Address(CharSequence ip, int from, int toExcluded) {
667         return ip instanceof String ? isValidIpV4Address((String) ip, from, toExcluded) :
668                 ip instanceof AsciiString ? isValidIpV4Address((AsciiString) ip, from, toExcluded) :
669                         isValidIpV4Address0(ip, from, toExcluded);
670     }
671
672     @SuppressWarnings("DuplicateBooleanBranch")
673     private static boolean isValidIpV4Address(String ip, int from, int toExcluded) {
674         int len = toExcluded - from;
675         int i;
676         return len <= 15 && len >= 7 &&
677                 (i = ip.indexOf('.', from + 1)) > 0 && isValidIpV4Word(ip, from, i) &&
678                 (i =  ip.indexOf('.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) &&
679                 (i =  ip.indexOf('.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) &&
680                 isValidIpV4Word(ip, i + 1, toExcluded);
681     }
682
683     @SuppressWarnings("DuplicateBooleanBranch")
684     private static boolean isValidIpV4Address(AsciiString ip, int from, int toExcluded) {
685         int len = toExcluded - from;
686         int i;
687         return len <= 15 && len >= 7 &&
688                 (i = ip.indexOf('.', from + 1)) > 0 && isValidIpV4Word(ip, from, i) &&
689                 (i =  ip.indexOf('.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) &&
690                 (i =  ip.indexOf('.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) &&
691                 isValidIpV4Word(ip, i + 1, toExcluded);
692     }
693
694     @SuppressWarnings("DuplicateBooleanBranch")
695     private static boolean isValidIpV4Address0(CharSequence ip, int from, int toExcluded) {
696         int len = toExcluded - from;
697         int i;
698         return len <= 15 && len >= 7 &&
699                 (i = indexOf(ip, '.', from + 1)) > 0 && isValidIpV4Word(ip, from, i) &&
700                 (i =  indexOf(ip, '.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) &&
701                 (i =  indexOf(ip, '.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) &&
702                 isValidIpV4Word(ip, i + 1, toExcluded);
703     }
704
705     /**
706      * Returns the {@link Inet6Address} representation of a {@link CharSequence} IP address.
707      * <p>
708      * This method will treat all IPv4 type addresses as "IPv4 mapped" (see {@link #getByName(CharSequence, boolean)})
709      * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address}
710      * @return {@link Inet6Address} representation of the {@code ip} or {@code nullif not a valid IP address.
711      */

712     public static Inet6Address getByName(CharSequence ip) {
713         return getByName(ip, true);
714     }
715
716     /**
717      * Returns the {@link Inet6Address} representation of a {@link CharSequence} IP address.
718      * <p>
719      * The {@code ipv4Mapped} parameter specifies how IPv4 addresses should be treated.
720      * "IPv4 mapped" format as
721      * defined in <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> is supported.
722      * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address}
723      * @param ipv4Mapped
724      * <ul>
725      * <li>{@code true} To allow IPv4 mapped inputs to be translated into {@link Inet6Address}</li>
726      * <li>{@code false} Consider IPv4 mapped addresses as invalid.</li>
727      * </ul>
728      * @return {@link Inet6Address} representation of the {@code ip} or {@code nullif not a valid IP address.
729      */

730     public static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped) {
731         byte[] bytes = getIPv6ByName(ip, ipv4Mapped);
732         if (bytes == null) {
733             return null;
734         }
735         try {
736             return Inet6Address.getByAddress(null, bytes, -1);
737         } catch (UnknownHostException e) {
738             throw new RuntimeException(e); // Should never happen
739         }
740     }
741
742     /**
743      * Returns the byte array representation of a {@link CharSequence} IP address.
744      * <p>
745      * The {@code ipv4Mapped} parameter specifies how IPv4 addresses should be treated.
746      * "IPv4 mapped" format as
747      * defined in <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> is supported.
748      * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address}
749      * @param ipv4Mapped
750      * <ul>
751      * <li>{@code true} To allow IPv4 mapped inputs to be translated into {@link Inet6Address}</li>
752      * <li>{@code false} Consider IPv4 mapped addresses as invalid.</li>
753      * </ul>
754      * @return byte array representation of the {@code ip} or {@code nullif not a valid IP address.
755      */

756     private static byte[] getIPv6ByName(CharSequence ip, boolean ipv4Mapped) {
757         final byte[] bytes = new byte[IPV6_BYTE_COUNT];
758         final int ipLength = ip.length();
759         int compressBegin = 0;
760         int compressLength = 0;
761         int currentIndex = 0;
762         int value = 0;
763         int begin = -1;
764         int i = 0;
765         int ipv6Separators = 0;
766         int ipv4Separators = 0;
767         int tmp;
768         boolean needsShift = false;
769         for (; i < ipLength; ++i) {
770             final char c = ip.charAt(i);
771             switch (c) {
772             case ':':
773                 ++ipv6Separators;
774                 if (i - begin > IPV6_MAX_CHAR_BETWEEN_SEPARATOR ||
775                         ipv4Separators > 0 || ipv6Separators > IPV6_MAX_SEPARATORS ||
776                         currentIndex + 1 >= bytes.length) {
777                     return null;
778                 }
779                 value <<= (IPV6_MAX_CHAR_BETWEEN_SEPARATOR - (i - begin)) << 2;
780
781                 if (compressLength > 0) {
782                     compressLength -= 2;
783                 }
784
785                 // The value integer holds at most 4 bytes from right (most significant) to left (least significant).
786                 // The following bit shifting is used to extract and re-order the individual bytes to achieve a
787                 // left (most significant) to right (least significant) ordering.
788                 bytes[currentIndex++] = (byte) (((value & 0xf) << 4) | ((value >> 4) & 0xf));
789                 bytes[currentIndex++] = (byte) ((((value >> 8) & 0xf) << 4) | ((value >> 12) & 0xf));
790                 tmp = i + 1;
791                 if (tmp < ipLength && ip.charAt(tmp) == ':') {
792                     ++tmp;
793                     if (compressBegin != 0 || (tmp < ipLength && ip.charAt(tmp) == ':')) {
794                         return null;
795                     }
796                     ++ipv6Separators;
797                     needsShift = ipv6Separators == 2 && value == 0;
798                     compressBegin = currentIndex;
799                     compressLength = bytes.length - compressBegin - 2;
800                     ++i;
801                 }
802                 value = 0;
803                 begin = -1;
804                 break;
805             case '.':
806                 ++ipv4Separators;
807                 tmp = i - begin; // tmp is the length of the current segment.
808                 if (tmp > IPV4_MAX_CHAR_BETWEEN_SEPARATOR
809                         || begin < 0
810                         || ipv4Separators > IPV4_SEPARATORS
811                         || (ipv6Separators > 0 && (currentIndex + compressLength < 12))
812                         || i + 1 >= ipLength
813                         || currentIndex >= bytes.length
814                         || ipv4Separators == 1 &&
815                             // We also parse pure IPv4 addresses as IPv4-Mapped for ease of use.
816                             ((!ipv4Mapped || currentIndex != 0 && !isValidIPv4Mapped(bytes, currentIndex,
817                                                                                      compressBegin, compressLength)) ||
818                                 (tmp == 3 && (!isValidNumericChar(ip.charAt(i - 1)) ||
819                                               !isValidNumericChar(ip.charAt(i - 2)) ||
820                                               !isValidNumericChar(ip.charAt(i - 3))) ||
821                                  tmp == 2 && (!isValidNumericChar(ip.charAt(i - 1)) ||
822                                               !isValidNumericChar(ip.charAt(i - 2))) ||
823                                  tmp == 1 && !isValidNumericChar(ip.charAt(i - 1))))) {
824                     return null;
825                 }
826                 value <<= (IPV4_MAX_CHAR_BETWEEN_SEPARATOR - tmp) << 2;
827
828                 // The value integer holds at most 3 bytes from right (most significant) to left (least significant).
829                 // The following bit shifting is to restructure the bytes to be left (most significant) to
830                 // right (least significant) while also accounting for each IPv4 digit is base 10.
831                 begin = (value & 0xf) * 100 + ((value >> 4) & 0xf) * 10 + ((value >> 8) & 0xf);
832                 if (begin < 0 || begin > 255) {
833                     return null;
834                 }
835                 bytes[currentIndex++] = (byte) begin;
836                 value = 0;
837                 begin = -1;
838                 break;
839             default:
840                 if (!isValidHexChar(c) || (ipv4Separators > 0 && !isValidNumericChar(c))) {
841                     return null;
842                 }
843                 if (begin < 0) {
844                     begin = i;
845                 } else if (i - begin > IPV6_MAX_CHAR_BETWEEN_SEPARATOR) {
846                     return null;
847                 }
848                 // The value is treated as a sort of array of numbers because we are dealing with
849                 // at most 4 consecutive bytes we can use bit shifting to accomplish this.
850                 // The most significant byte will be encountered first, and reside in the right most
851                 // position of the following integer
852                 value += StringUtil.decodeHexNibble(c) << ((i - begin) << 2);
853                 break;
854             }
855         }
856
857         final boolean isCompressed = compressBegin > 0;
858         // Finish up last set of data that was accumulated in the loop (or before the loop)
859         if (ipv4Separators > 0) {
860             if (begin > 0 && i - begin > IPV4_MAX_CHAR_BETWEEN_SEPARATOR ||
861                     ipv4Separators != IPV4_SEPARATORS ||
862                     currentIndex >= bytes.length) {
863                 return null;
864             }
865             if (ipv6Separators == 0) {
866                 compressLength = 12;
867             } else if (ipv6Separators >= IPV6_MIN_SEPARATORS &&
868                            (!isCompressed && (ipv6Separators == 6 && ip.charAt(0) != ':') ||
869                             isCompressed && (ipv6Separators < IPV6_MAX_SEPARATORS &&
870                                              (ip.charAt(0) != ':' || compressBegin <= 2)))) {
871                 compressLength -= 2;
872             } else {
873                 return null;
874             }
875             value <<= (IPV4_MAX_CHAR_BETWEEN_SEPARATOR - (i - begin)) << 2;
876
877             // The value integer holds at most 3 bytes from right (most significant) to left (least significant).
878             // The following bit shifting is to restructure the bytes to be left (most significant) to
879             // right (least significant) while also accounting for each IPv4 digit is base 10.
880             begin = (value & 0xf) * 100 + ((value >> 4) & 0xf) * 10 + ((value >> 8) & 0xf);
881             if (begin < 0 || begin > 255) {
882                 return null;
883             }
884             bytes[currentIndex++] = (byte) begin;
885         } else {
886             tmp = ipLength - 1;
887             if (begin > 0 && i - begin > IPV6_MAX_CHAR_BETWEEN_SEPARATOR ||
888                     ipv6Separators < IPV6_MIN_SEPARATORS ||
889                     !isCompressed && (ipv6Separators + 1 != IPV6_MAX_SEPARATORS  ||
890                                       ip.charAt(0) == ':' || ip.charAt(tmp) == ':') ||
891                     isCompressed && (ipv6Separators > IPV6_MAX_SEPARATORS ||
892                         (ipv6Separators == IPV6_MAX_SEPARATORS &&
893                           (compressBegin <= 2 && ip.charAt(0) != ':' ||
894                            compressBegin >= 14 && ip.charAt(tmp) != ':'))) ||
895                     currentIndex + 1 >= bytes.length ||
896                     begin < 0 && ip.charAt(tmp - 1) != ':' ||
897                     compressBegin > 2 && ip.charAt(0) == ':') {
898                 return null;
899             }
900             if (begin >= 0 && i - begin <= IPV6_MAX_CHAR_BETWEEN_SEPARATOR) {
901                 value <<= (IPV6_MAX_CHAR_BETWEEN_SEPARATOR - (i - begin)) << 2;
902             }
903             // The value integer holds at most 4 bytes from right (most significant) to left (least significant).
904             // The following bit shifting is used to extract and re-order the individual bytes to achieve a
905             // left (most significant) to right (least significant) ordering.
906             bytes[currentIndex++] = (byte) (((value & 0xf) << 4) | ((value >> 4) & 0xf));
907             bytes[currentIndex++] = (byte) ((((value >> 8) & 0xf) << 4) | ((value >> 12) & 0xf));
908         }
909
910         i = currentIndex + compressLength;
911         if (needsShift || i >= bytes.length) {
912             // Right shift array
913             if (i >= bytes.length) {
914                 ++compressBegin;
915             }
916             for (i = currentIndex; i < bytes.length; ++i) {
917                 for (begin = bytes.length - 1; begin >= compressBegin; --begin) {
918                     bytes[begin] = bytes[begin - 1];
919                 }
920                 bytes[begin] = 0;
921                 ++compressBegin;
922             }
923         } else {
924             // Selectively move elements
925             for (i = 0; i < compressLength; ++i) {
926                 begin = i + compressBegin;
927                 currentIndex = begin + compressLength;
928                 if (currentIndex < bytes.length) {
929                     bytes[currentIndex] = bytes[begin];
930                     bytes[begin] = 0;
931                 } else {
932                     break;
933                 }
934             }
935         }
936
937         if (ipv4Separators > 0) {
938             // We only support IPv4-Mapped addresses [1] because IPv4-Compatible addresses are deprecated [2].
939             // [1] https://tools.ietf.org/html/rfc4291#section-2.5.5.2
940             // [2] https://tools.ietf.org/html/rfc4291#section-2.5.5.1
941             bytes[10] = bytes[11] = (byte) 0xff;
942         }
943
944         return bytes;
945     }
946
947     /**
948      * Returns the {@link String} representation of an {@link InetSocketAddress}.
949      * <p>
950      * The output does not include Scope ID.
951      * @param addr {@link InetSocketAddress} to be converted to an address string
952      * @return {@code String} containing the text-formatted IP address
953      */

954     public static String toSocketAddressString(InetSocketAddress addr) {
955         String port = String.valueOf(addr.getPort());
956         final StringBuilder sb;
957
958         if (addr.isUnresolved()) {
959             String hostname = getHostname(addr);
960             sb = newSocketAddressStringBuilder(hostname, port, !isValidIpV6Address(hostname));
961         } else {
962             InetAddress address = addr.getAddress();
963             String hostString = toAddressString(address);
964             sb = newSocketAddressStringBuilder(hostString, port, address instanceof Inet4Address);
965         }
966         return sb.append(':').append(port).toString();
967     }
968
969     /**
970      * Returns the {@link String} representation of a host port combo.
971      */

972     public static String toSocketAddressString(String host, int port) {
973         String portStr = String.valueOf(port);
974         return newSocketAddressStringBuilder(
975                 host, portStr, !isValidIpV6Address(host)).append(':').append(portStr).toString();
976     }
977
978     private static StringBuilder newSocketAddressStringBuilder(String host, String port, boolean ipv4) {
979         int hostLen = host.length();
980         if (ipv4) {
981             // Need to include enough space for hostString:port.
982             return new StringBuilder(hostLen + 1 + port.length()).append(host);
983         }
984         // Need to include enough space for [hostString]:port.
985         StringBuilder stringBuilder = new StringBuilder(hostLen + 3 + port.length());
986         if (hostLen > 1 && host.charAt(0) == '[' && host.charAt(hostLen - 1) == ']') {
987             return stringBuilder.append(host);
988         }
989         return stringBuilder.append('[').append(host).append(']');
990     }
991
992     /**
993      * Returns the {@link String} representation of an {@link InetAddress}.
994      * <ul>
995      * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li>
996      * <li>Inet6Address results adhere to
997      * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li>
998      * </ul>
999      * <p>
1000      * The output does not include Scope ID.
1001      * @param ip {@link InetAddress} to be converted to an address string
1002      * @return {@code String} containing the text-formatted IP address
1003      */

1004     public static String toAddressString(InetAddress ip) {
1005         return toAddressString(ip, false);
1006     }
1007
1008     /**
1009      * Returns the {@link String} representation of an {@link InetAddress}.
1010      * <ul>
1011      * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li>
1012      * <li>Inet6Address results adhere to
1013      * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a> if
1014      * {@code ipv4Mapped} is false.  If {@code ipv4Mapped} is true then "IPv4 mapped" format
1015      * from <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> will be supported.
1016      * The compressed result will always obey the compression rules defined in
1017      * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li>
1018      * </ul>
1019      * <p>
1020      * The output does not include Scope ID.
1021      * @param ip {@link InetAddress} to be converted to an address string
1022      * @param ipv4Mapped
1023      * <ul>
1024      * <li>{@code true} to stray from strict rfc 5952 and support the "IPv4 mapped" format
1025      * defined in <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> while still
1026      * following the updated guidelines in
1027      * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li>
1028      * <li>{@code false} to strictly follow rfc 5952</li>
1029      * </ul>
1030      * @return {@code String} containing the text-formatted IP address
1031      */

1032     public static String toAddressString(InetAddress ip, boolean ipv4Mapped) {
1033         if (ip instanceof Inet4Address) {
1034             return ip.getHostAddress();
1035         }
1036         if (!(ip instanceof Inet6Address)) {
1037             throw new IllegalArgumentException("Unhandled type: " + ip);
1038         }
1039
1040         return toAddressString(ip.getAddress(), 0, ipv4Mapped);
1041     }
1042
1043     private static String toAddressString(byte[] bytes, int offset, boolean ipv4Mapped) {
1044         final int[] words = new int[IPV6_WORD_COUNT];
1045         int i;
1046         final int end = offset + words.length;
1047         for (i = offset; i < end; ++i) {
1048             words[i] = ((bytes[i << 1] & 0xff) << 8) | (bytes[(i << 1) + 1] & 0xff);
1049         }
1050
1051         // Find longest run of 0s, tie goes to first found instance
1052         int currentStart = -1;
1053         int currentLength;
1054         int shortestStart = -1;
1055         int shortestLength = 0;
1056         for (i = 0; i < words.length; ++i) {
1057             if (words[i] == 0) {
1058                 if (currentStart < 0) {
1059                     currentStart = i;
1060                 }
1061             } else if (currentStart >= 0) {
1062                 currentLength = i - currentStart;
1063                 if (currentLength > shortestLength) {
1064                     shortestStart = currentStart;
1065                     shortestLength = currentLength;
1066                 }
1067                 currentStart = -1;
1068             }
1069         }
1070         // If the array ends on a streak of zeros, make sure we account for it
1071         if (currentStart >= 0) {
1072             currentLength = i - currentStart;
1073             if (currentLength > shortestLength) {
1074                 shortestStart = currentStart;
1075                 shortestLength = currentLength;
1076             }
1077         }
1078         // Ignore the longest streak if it is only 1 long
1079         if (shortestLength == 1) {
1080             shortestLength = 0;
1081             shortestStart = -1;
1082         }
1083
1084         // Translate to string taking into account longest consecutive 0s
1085         final int shortestEnd = shortestStart + shortestLength;
1086         final StringBuilder b = new StringBuilder(IPV6_MAX_CHAR_COUNT);
1087         if (shortestEnd < 0) { // Optimization when there is no compressing needed
1088             b.append(Integer.toHexString(words[0]));
1089             for (i = 1; i < words.length; ++i) {
1090                 b.append(':');
1091                 b.append(Integer.toHexString(words[i]));
1092             }
1093         } else { // General case that can handle compressing (and not compressing)
1094             // Loop unroll the first index (so we don't constantly check i==0 cases in loop)
1095             final boolean isIpv4Mapped;
1096             if (inRangeEndExclusive(0, shortestStart, shortestEnd)) {
1097                 b.append("::");
1098                 isIpv4Mapped = ipv4Mapped && (shortestEnd == 5 && words[5] == 0xffff);
1099             } else {
1100                 b.append(Integer.toHexString(words[0]));
1101                 isIpv4Mapped = false;
1102             }
1103             for (i = 1; i < words.length; ++i) {
1104                 if (!inRangeEndExclusive(i, shortestStart, shortestEnd)) {
1105                     if (!inRangeEndExclusive(i - 1, shortestStart, shortestEnd)) {
1106                         // If the last index was not part of the shortened sequence
1107                         if (!isIpv4Mapped || i == 6) {
1108                             b.append(':');
1109                         } else {
1110                             b.append('.');
1111                         }
1112                     }
1113                     if (isIpv4Mapped && i > 5) {
1114                         b.append(words[i] >> 8);
1115                         b.append('.');
1116                         b.append(words[i] & 0xff);
1117                     } else {
1118                         b.append(Integer.toHexString(words[i]));
1119                     }
1120                 } else if (!inRangeEndExclusive(i - 1, shortestStart, shortestEnd)) {
1121                     // If we are in the shortened sequence and the last index was not
1122                     b.append("::");
1123                 }
1124             }
1125         }
1126
1127         return b.toString();
1128     }
1129
1130     /**
1131      * Returns {@link InetSocketAddress#getHostString()} if Java >= 7,
1132      * or {@link InetSocketAddress#getHostName()} otherwise.
1133      * @param addr The address
1134      * @return the host string
1135      */

1136     public static String getHostname(InetSocketAddress addr) {
1137         return PlatformDependent.javaVersion() >= 7 ? addr.getHostString() : addr.getHostName();
1138     }
1139
1140     /**
1141      * Does a range check on {@code value} if is within {@code start} (inclusive) and {@code end} (exclusive).
1142      * @param value The value to checked if is within {@code start} (inclusive) and {@code end} (exclusive)
1143      * @param start The start of the range (inclusive)
1144      * @param end The end of the range (exclusive)
1145      * @return
1146      * <ul>
1147      * <li>{@code trueif {@code value} if is within {@code start} (inclusive) and {@code end} (exclusive)</li>
1148      * <li>{@code false} otherwise</li>
1149      * </ul>
1150      */

1151     private static boolean inRangeEndExclusive(int value, int start, int end) {
1152         return value >= start && value < end;
1153     }
1154
1155     /**
1156      * A constructor to stop this class being constructed.
1157      */

1158     private NetUtil() {
1159         // Unused
1160     }
1161 }
1162