View Javadoc
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.StringUtil;
20  import io.netty.util.internal.logging.InternalLogger;
21  import io.netty.util.internal.logging.InternalLoggerFactory;
22  
23  import java.io.BufferedReader;
24  import java.io.File;
25  import java.io.FileReader;
26  import java.net.Inet4Address;
27  import java.net.Inet6Address;
28  import java.net.InetAddress;
29  import java.net.NetworkInterface;
30  import java.net.SocketException;
31  import java.net.UnknownHostException;
32  import java.util.ArrayList;
33  import java.util.Enumeration;
34  import java.util.List;
35  import java.util.StringTokenizer;
36  
37  /**
38   * A class that holds a number of network-related constants.
39   * <p/>
40   * This class borrowed some of its methods from a  modified fork of the
41   * <a href="http://svn.apache.org/repos/asf/harmony/enhanced/java/branches/java6/classlib/modules/luni/
42   * src/main/java/org/apache/harmony/luni/util/Inet6Util.java">Inet6Util class</a> which was part of Apache Harmony.
43   */
44  public final class NetUtil {
45  
46      /**
47       * The {@link Inet4Address} that represents the IPv4 loopback address '127.0.0.1'
48       */
49      public static final Inet4Address LOCALHOST4;
50  
51      /**
52       * The {@link Inet6Address} that represents the IPv6 loopback address '::1'
53       */
54      public static final Inet6Address LOCALHOST6;
55  
56      /**
57       * The {@link InetAddress} that represents the loopback address. If IPv6 stack is available, it will refer to
58       * {@link #LOCALHOST6}.  Otherwise, {@link #LOCALHOST4}.
59       */
60      public static final InetAddress LOCALHOST;
61  
62      /**
63       * The loopback {@link NetworkInterface} of the current machine
64       */
65      public static final NetworkInterface LOOPBACK_IF;
66  
67      /**
68       * The SOMAXCONN value of the current machine.  If failed to get the value,  {@code 200}  is used as a
69       * default value for Windows or {@code 128} for others.
70       */
71      public static final int SOMAXCONN;
72  
73      /**
74       * This defines how many words (represented as ints) are needed to represent an IPv6 address
75       */
76      private static final int IPV6_WORD_COUNT = 8;
77  
78      /**
79       * The maximum number of characters for an IPV6 string with no scope
80       */
81      private static final int IPV6_MAX_CHAR_COUNT = 39;
82  
83      /**
84       * Number of bytes needed to represent and IPV6 value
85       */
86      private static final int IPV6_BYTE_COUNT = 16;
87  
88      /**
89       * Maximum amount of value adding characters in between IPV6 separators
90       */
91      private static final int IPV6_MAX_CHAR_BETWEEN_SEPARATOR = 4;
92  
93      /**
94       * Minimum number of separators that must be present in an IPv6 string
95       */
96      private static final int IPV6_MIN_SEPARATORS = 2;
97  
98      /**
99       * Maximum number of separators that must be present in an IPv6 string
100      */
101     private static final int IPV6_MAX_SEPARATORS = 8;
102 
103     /**
104      * Number of bytes needed to represent and IPV4 value
105      */
106     private static final int IPV4_BYTE_COUNT = 4;
107 
108     /**
109      * Maximum amount of value adding characters in between IPV4 separators
110      */
111     private static final int IPV4_MAX_CHAR_BETWEEN_SEPARATOR = 3;
112 
113     /**
114      * Number of separators that must be present in an IPv4 string
115      */
116     private static final int IPV4_SEPARATORS = 3;
117 
118     /**
119      * The logger being used by this class
120      */
121     private static final InternalLogger logger = InternalLoggerFactory.getInstance(NetUtil.class);
122 
123     static {
124         byte[] LOCALHOST4_BYTES = {127, 0, 0, 1};
125         byte[] LOCALHOST6_BYTES = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
126 
127         // Create IPv4 loopback address.
128         Inet4Address localhost4 = null;
129         try {
130             localhost4 = (Inet4Address) InetAddress.getByAddress(LOCALHOST4_BYTES);
131         } catch (Exception e) {
132             // We should not get here as long as the length of the address is correct.
133             PlatformDependent.throwException(e);
134         }
135         LOCALHOST4 = localhost4;
136 
137         // Create IPv6 loopback address.
138         Inet6Address localhost6 = null;
139         try {
140             localhost6 = (Inet6Address) InetAddress.getByAddress(LOCALHOST6_BYTES);
141         } catch (Exception e) {
142             // We should not get here as long as the length of the address is correct.
143             PlatformDependent.throwException(e);
144         }
145         LOCALHOST6 = localhost6;
146 
147         // Retrieve the list of available network interfaces.
148         List<NetworkInterface> ifaces = new ArrayList<NetworkInterface>();
149         try {
150             for (Enumeration<NetworkInterface> i = NetworkInterface.getNetworkInterfaces(); i.hasMoreElements();) {
151                 NetworkInterface iface = i.nextElement();
152                 // Use the interface with proper INET addresses only.
153                 if (iface.getInetAddresses().hasMoreElements()) {
154                     ifaces.add(iface);
155                 }
156             }
157         } catch (SocketException e) {
158             logger.warn("Failed to retrieve the list of available network interfaces", e);
159         }
160 
161         // Find the first loopback interface available from its INET address (127.0.0.1 or ::1)
162         // Note that we do not use NetworkInterface.isLoopback() in the first place because it takes long time
163         // on a certain environment. (e.g. Windows with -Djava.net.preferIPv4Stack=true)
164         NetworkInterface loopbackIface = null;
165         InetAddress loopbackAddr = null;
166         loop: for (NetworkInterface iface: ifaces) {
167             for (Enumeration<InetAddress> i = iface.getInetAddresses(); i.hasMoreElements();) {
168                 InetAddress addr = i.nextElement();
169                 if (addr.isLoopbackAddress()) {
170                     // Found
171                     loopbackIface = iface;
172                     loopbackAddr = addr;
173                     break loop;
174                 }
175             }
176         }
177 
178         // If failed to find the loopback interface from its INET address, fall back to isLoopback().
179         if (loopbackIface == null) {
180             try {
181                 for (NetworkInterface iface: ifaces) {
182                     if (iface.isLoopback()) {
183                         Enumeration<InetAddress> i = iface.getInetAddresses();
184                         if (i.hasMoreElements()) {
185                             // Found the one with INET address.
186                             loopbackIface = iface;
187                             loopbackAddr = i.nextElement();
188                             break;
189                         }
190                     }
191                 }
192 
193                 if (loopbackIface == null) {
194                     logger.warn("Failed to find the loopback interface");
195                 }
196             } catch (SocketException e) {
197                 logger.warn("Failed to find the loopback interface", e);
198             }
199         }
200 
201         if (loopbackIface != null) {
202             // Found the loopback interface with an INET address.
203             logger.debug(
204                     "Loopback interface: {} ({}, {})",
205                     loopbackIface.getName(), loopbackIface.getDisplayName(), loopbackAddr.getHostAddress());
206         } else {
207             // Could not find the loopback interface, but we can't leave LOCALHOST as null.
208             // Use LOCALHOST6 or LOCALHOST4, preferably the IPv6 one.
209             if (loopbackAddr == null) {
210                 try {
211                     if (NetworkInterface.getByInetAddress(LOCALHOST6) != null) {
212                         logger.debug("Using hard-coded IPv6 localhost address: {}", localhost6);
213                         loopbackAddr = localhost6;
214                     }
215                 } catch (Exception e) {
216                     // Ignore
217                 } finally {
218                     if (loopbackAddr == null) {
219                         logger.debug("Using hard-coded IPv4 localhost address: {}", localhost4);
220                         loopbackAddr = localhost4;
221                     }
222                 }
223             }
224         }
225 
226         LOOPBACK_IF = loopbackIface;
227         LOCALHOST = loopbackAddr;
228 
229         // Determine the default somaxconn (server socket backlog) value of the platform.
230         // The known defaults:
231         // - Windows NT Server 4.0+: 200
232         // - Linux and Mac OS X: 128
233         int somaxconn = PlatformDependent.isWindows() ? 200 : 128;
234         File file = new File("/proc/sys/net/core/somaxconn");
235         if (file.exists()) {
236             BufferedReader in = null;
237             try {
238                 in = new BufferedReader(new FileReader(file));
239                 somaxconn = Integer.parseInt(in.readLine());
240                 if (logger.isDebugEnabled()) {
241                     logger.debug("{}: {}", file, somaxconn);
242                 }
243             } catch (Exception e) {
244                 logger.debug("Failed to get SOMAXCONN from: {}", file, e);
245             } finally {
246                 if (in != null) {
247                     try {
248                         in.close();
249                     } catch (Exception e) {
250                         // Ignored.
251                     }
252                 }
253             }
254         } else {
255             if (logger.isDebugEnabled()) {
256                 logger.debug("{}: {} (non-existent)", file, somaxconn);
257             }
258         }
259 
260         SOMAXCONN = somaxconn;
261     }
262 
263     /**
264      * Creates an byte[] based on an ipAddressString. No error handling is
265      * performed here.
266      */
267     public static byte[] createByteArrayFromIpAddressString(String ipAddressString) {
268 
269         if (isValidIpV4Address(ipAddressString)) {
270             StringTokenizer tokenizer = new StringTokenizer(ipAddressString, ".");
271             String token;
272             int tempInt;
273             byte[] byteAddress = new byte[IPV4_BYTE_COUNT];
274             for (int i = 0; i < IPV4_BYTE_COUNT; i ++) {
275                 token = tokenizer.nextToken();
276                 tempInt = Integer.parseInt(token);
277                 byteAddress[i] = (byte) tempInt;
278             }
279 
280             return byteAddress;
281         }
282 
283         if (isValidIpV6Address(ipAddressString)) {
284             if (ipAddressString.charAt(0) == '[') {
285                 ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1);
286             }
287 
288             int percentPos = ipAddressString.indexOf('%');
289             if (percentPos >= 0) {
290                 ipAddressString = ipAddressString.substring(0, percentPos);
291             }
292 
293             StringTokenizer tokenizer = new StringTokenizer(ipAddressString, ":.", true);
294             ArrayList<String> hexStrings = new ArrayList<String>();
295             ArrayList<String> decStrings = new ArrayList<String>();
296             String token = "";
297             String prevToken = "";
298             int doubleColonIndex = -1; // If a double colon exists, we need to
299             // insert 0s.
300 
301             // Go through the tokens, including the seperators ':' and '.'
302             // When we hit a : or . the previous token will be added to either
303             // the hex list or decimal list. In the case where we hit a ::
304             // we will save the index of the hexStrings so we can add zeros
305             // in to fill out the string
306             while (tokenizer.hasMoreTokens()) {
307                 prevToken = token;
308                 token = tokenizer.nextToken();
309 
310                 if (":".equals(token)) {
311                     if (":".equals(prevToken)) {
312                         doubleColonIndex = hexStrings.size();
313                     } else if (!prevToken.isEmpty()) {
314                         hexStrings.add(prevToken);
315                     }
316                 } else if (".".equals(token)) {
317                     decStrings.add(prevToken);
318                 }
319             }
320 
321             if (":".equals(prevToken)) {
322                 if (":".equals(token)) {
323                     doubleColonIndex = hexStrings.size();
324                 } else {
325                     hexStrings.add(token);
326                 }
327             } else if (".".equals(prevToken)) {
328                 decStrings.add(token);
329             }
330 
331             // figure out how many hexStrings we should have
332             // also check if it is a IPv4 address
333             int hexStringsLength = 8;
334 
335             // If we have an IPv4 address tagged on at the end, subtract
336             // 4 bytes, or 2 hex words from the total
337             if (!decStrings.isEmpty()) {
338                 hexStringsLength -= 2;
339             }
340 
341             // if we hit a double Colon add the appropriate hex strings
342             if (doubleColonIndex != -1) {
343                 int numberToInsert = hexStringsLength - hexStrings.size();
344                 for (int i = 0; i < numberToInsert; i ++) {
345                     hexStrings.add(doubleColonIndex, "0");
346                 }
347             }
348 
349             byte[] ipByteArray = new byte[IPV6_BYTE_COUNT];
350 
351             // Finally convert these strings to bytes...
352             for (int i = 0; i < hexStrings.size(); i ++) {
353                 convertToBytes(hexStrings.get(i), ipByteArray, i << 1);
354             }
355 
356             // Now if there are any decimal values, we know where they go...
357             for (int i = 0; i < decStrings.size(); i ++) {
358                 ipByteArray[i + 12] = (byte) (Integer.parseInt(decStrings.get(i)) & 255);
359             }
360             return ipByteArray;
361         }
362         return null;
363     }
364 
365     /**
366      * Converts a 4 character hex word into a 2 byte word equivalent
367      */
368     private static void convertToBytes(String hexWord, byte[] ipByteArray, int byteIndex) {
369 
370         int hexWordLength = hexWord.length();
371         int hexWordIndex = 0;
372         ipByteArray[byteIndex] = 0;
373         ipByteArray[byteIndex + 1] = 0;
374         int charValue;
375 
376         // high order 4 bits of first byte
377         if (hexWordLength > 3) {
378             charValue = getIntValue(hexWord.charAt(hexWordIndex ++));
379             ipByteArray[byteIndex] |= charValue << 4;
380         }
381 
382         // low order 4 bits of the first byte
383         if (hexWordLength > 2) {
384             charValue = getIntValue(hexWord.charAt(hexWordIndex ++));
385             ipByteArray[byteIndex] |= charValue;
386         }
387 
388         // high order 4 bits of second byte
389         if (hexWordLength > 1) {
390             charValue = getIntValue(hexWord.charAt(hexWordIndex ++));
391             ipByteArray[byteIndex + 1] |= charValue << 4;
392         }
393 
394         // low order 4 bits of the first byte
395         charValue = getIntValue(hexWord.charAt(hexWordIndex));
396         ipByteArray[byteIndex + 1] |= charValue & 15;
397     }
398 
399     private static int getIntValue(char c) {
400         switch (c) {
401             case '0':
402                 return 0;
403             case '1':
404                 return 1;
405             case '2':
406                 return 2;
407             case '3':
408                 return 3;
409             case '4':
410                 return 4;
411             case '5':
412                 return 5;
413             case '6':
414                 return 6;
415             case '7':
416                 return 7;
417             case '8':
418                 return 8;
419             case '9':
420                 return 9;
421         }
422 
423         c = Character.toLowerCase(c);
424         switch (c) {
425             case 'a':
426                 return 10;
427             case 'b':
428                 return 11;
429             case 'c':
430                 return 12;
431             case 'd':
432                 return 13;
433             case 'e':
434                 return 14;
435             case 'f':
436                 return 15;
437         }
438         return 0;
439     }
440 
441     /**
442      * Converts a 32-bit integer into an IPv4 address.
443      */
444     public static String intToIpAddress(int i) {
445         StringBuilder buf = new StringBuilder(15);
446         buf.append(i >> 24 & 0xff);
447         buf.append('.');
448         buf.append(i >> 16 & 0xff);
449         buf.append('.');
450         buf.append(i >> 8 & 0xff);
451         buf.append('.');
452         buf.append(i & 0xff);
453         return buf.toString();
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         if (length == 4) {
464             StringBuilder buf = new StringBuilder(15);
465 
466             buf.append(bytes[offset ++] >> 24 & 0xff);
467             buf.append('.');
468             buf.append(bytes[offset ++] >> 16 & 0xff);
469             buf.append('.');
470             buf.append(bytes[offset ++] >> 8 & 0xff);
471             buf.append('.');
472             buf.append(bytes[offset] & 0xff);
473 
474             return buf.toString();
475         }
476 
477         if (length == 16) {
478             final StringBuilder sb = new StringBuilder(39);
479             final int endOffset = offset + 14;
480 
481             for (; offset < endOffset; offset += 2) {
482                 StringUtil.toHexString(sb, bytes, offset, 2);
483                 sb.append(':');
484             }
485             StringUtil.toHexString(sb, bytes, offset, 2);
486 
487             return sb.toString();
488         }
489 
490         throw new IllegalArgumentException("length: " + length + " (expected: 4 or 16)");
491     }
492 
493     public static boolean isValidIpV6Address(String ipAddress) {
494         int length = ipAddress.length();
495         boolean doubleColon = false;
496         int numberOfColons = 0;
497         int numberOfPeriods = 0;
498         StringBuilder word = new StringBuilder();
499         char c = 0;
500         char prevChar;
501         int startOffset = 0; // offset for [] ip addresses
502         int endOffset = ipAddress.length();
503 
504         if (endOffset < 2) {
505             return false;
506         }
507 
508         // Strip []
509         if (ipAddress.charAt(0) == '[') {
510             if (ipAddress.charAt(endOffset - 1) != ']') {
511                 return false; // must have a close ]
512             }
513 
514             startOffset = 1;
515             endOffset --;
516         }
517 
518         // Strip the interface name/index after the percent sign.
519         int percentIdx = ipAddress.indexOf('%', startOffset);
520         if (percentIdx >= 0) {
521             endOffset = percentIdx;
522         }
523 
524         for (int i = startOffset; i < endOffset; i ++) {
525             prevChar = c;
526             c = ipAddress.charAt(i);
527             switch (c) {
528                 // case for the last 32-bits represented as IPv4 x:x:x:x:x:x:d.d.d.d
529                 case '.':
530                     numberOfPeriods ++;
531                     if (numberOfPeriods > 3) {
532                         return false;
533                     }
534                     if (!isValidIp4Word(word.toString())) {
535                         return false;
536                     }
537                     if (numberOfColons != 6 && !doubleColon) {
538                         return false;
539                     }
540                     // a special case ::1:2:3:4:5:d.d.d.d allows 7 colons with an
541                     // IPv4 ending, otherwise 7 :'s is bad
542                     if (numberOfColons == 7 && ipAddress.charAt(startOffset) != ':' &&
543                         ipAddress.charAt(1 + startOffset) != ':') {
544                         return false;
545                     }
546                     word.delete(0, word.length());
547                     break;
548 
549                 case ':':
550                     // FIX "IP6 mechanism syntax #ip6-bad1"
551                     // An IPV6 address cannot start with a single ":".
552                     // Either it can starti with "::" or with a number.
553                     if (i == startOffset && (ipAddress.length() <= i || ipAddress.charAt(i + 1) != ':')) {
554                         return false;
555                     }
556                     // END FIX "IP6 mechanism syntax #ip6-bad1"
557                     numberOfColons ++;
558                     if (numberOfColons > 7) {
559                         return false;
560                     }
561                     if (numberOfPeriods > 0) {
562                         return false;
563                     }
564                     if (prevChar == ':') {
565                         if (doubleColon) {
566                             return false;
567                         }
568                         doubleColon = true;
569                     }
570                     word.delete(0, word.length());
571                     break;
572 
573                 default:
574                     if (word != null && word.length() > 3) {
575                         return false;
576                     }
577                     if (!isValidHexChar(c)) {
578                         return false;
579                     }
580                     word.append(c);
581             }
582         }
583 
584         // Check if we have an IPv4 ending
585         if (numberOfPeriods > 0) {
586             // There is a test case with 7 colons and valid ipv4 this should resolve it
587             if (numberOfPeriods != 3 || !(isValidIp4Word(word.toString()) && numberOfColons < 7)) {
588                 return false;
589             }
590         } else {
591             // If we're at then end and we haven't had 7 colons then there is a
592             // problem unless we encountered a doubleColon
593             if (numberOfColons != 7 && !doubleColon) {
594                 return false;
595             }
596 
597             // If we have an empty word at the end, it means we ended in either
598             // a : or a .
599             // If we did not end in :: then this is invalid
600             if (word.length() == 0 && ipAddress.charAt(length - 1 - startOffset) == ':' &&
601                 ipAddress.charAt(length - 2 - startOffset) != ':') {
602                 return false;
603             }
604         }
605 
606         return true;
607     }
608 
609     private static boolean isValidIp4Word(String word) {
610         char c;
611         if (word.length() < 1 || word.length() > 3) {
612             return false;
613         }
614         for (int i = 0; i < word.length(); i ++) {
615             c = word.charAt(i);
616             if (!(c >= '0' && c <= '9')) {
617                 return false;
618             }
619         }
620         return Integer.parseInt(word) <= 255;
621     }
622 
623     private static boolean isValidHexChar(char c) {
624         return c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';
625     }
626 
627     private static boolean isValidNumericChar(char c) {
628         return c >= '0' && c <= '9';
629     }
630 
631     /**
632      * Takes a string and parses it to see if it is a valid IPV4 address.
633      *
634      * @return true, if the string represents an IPV4 address in dotted
635      *         notation, false otherwise
636      */
637     public static boolean isValidIpV4Address(String value) {
638 
639         int periods = 0;
640         int i;
641         int length = value.length();
642 
643         if (length > 15) {
644             return false;
645         }
646         char c;
647         StringBuilder word = new StringBuilder();
648         for (i = 0; i < length; i ++) {
649             c = value.charAt(i);
650             if (c == '.') {
651                 periods ++;
652                 if (periods > 3) {
653                     return false;
654                 }
655                 if (word.length() == 0) {
656                     return false;
657                 }
658                 if (Integer.parseInt(word.toString()) > 255) {
659                     return false;
660                 }
661                 word.delete(0, word.length());
662             } else if (!Character.isDigit(c)) {
663                 return false;
664             } else {
665                 if (word.length() > 2) {
666                     return false;
667                 }
668                 word.append(c);
669             }
670         }
671 
672         if (word.length() == 0 || Integer.parseInt(word.toString()) > 255) {
673             return false;
674         }
675 
676         return periods == 3;
677     }
678 
679     /**
680      * Returns the {@link Inet6Address} representation of a {@link CharSequence} IP address.
681      * <p>
682      * This method will treat all IPv4 type addresses as "IPv4 mapped" (see {@link #getByName(CharSequence, boolean)})
683      * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address}
684      * @return {@link Inet6Address} representation of the {@code ip} or {@code null} if not a valid IP address.
685      */
686     public static Inet6Address getByName(CharSequence ip) {
687         return getByName(ip, true);
688     }
689 
690     /**
691      * Returns the {@link Inet6Address} representation of a {@link CharSequence} IP address.
692      * <p>
693      * The {@code ipv4Mapped} parameter specifies how IPv4 addresses should be treated.
694      * "IPv4 mapped" format as
695      * defined in <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> is supported.
696      * @param ip {@link CharSequence} IP address to be converted to a {@link Inet6Address}
697      * @param ipv4Mapped
698      * <ul>
699      * <li>{@code true} To allow IPv4 mapped inputs to be translated into {@link Inet6Address}</li>
700      * <li>{@code false} Don't turn IPv4 addressed to mapped addresses</li>
701      * </ul>
702      * @return {@link Inet6Address} representation of the {@code ip} or {@code null} if not a valid IP address.
703      */
704     public static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped) {
705         final byte[] bytes = new byte[IPV6_BYTE_COUNT];
706         final int ipLength = ip.length();
707         int compressBegin = 0;
708         int compressLength = 0;
709         int currentIndex = 0;
710         int value = 0;
711         int begin = -1;
712         int i = 0;
713         int ipv6Seperators = 0;
714         int ipv4Seperators = 0;
715         int tmp;
716         boolean needsShift = false;
717         for (; i < ipLength; ++i) {
718             final char c = ip.charAt(i);
719             switch (c) {
720             case ':':
721                 ++ipv6Seperators;
722                 if (i - begin > IPV6_MAX_CHAR_BETWEEN_SEPARATOR ||
723                         ipv4Seperators > 0 || ipv6Seperators > IPV6_MAX_SEPARATORS ||
724                         currentIndex + 1 >= bytes.length) {
725                     return null;
726                 }
727                 value <<= (IPV6_MAX_CHAR_BETWEEN_SEPARATOR - (i - begin)) << 2;
728 
729                 if (compressLength > 0) {
730                     compressLength -= 2;
731                 }
732 
733                 // The value integer holds at most 4 bytes from right (most significant) to left (least significant).
734                 // The following bit shifting is used to extract and re-order the individual bytes to achieve a
735                 // left (most significant) to right (least significant) ordering.
736                 bytes[currentIndex++] = (byte) (((value & 0xf) << 4) | ((value >> 4) & 0xf));
737                 bytes[currentIndex++] = (byte) ((((value >> 8) & 0xf) << 4) | ((value >> 12) & 0xf));
738                 tmp = i + 1;
739                 if (tmp < ipLength && ip.charAt(tmp) == ':') {
740                     ++tmp;
741                     if (compressBegin != 0 || (tmp < ipLength && ip.charAt(tmp) == ':')) {
742                         return null;
743                     }
744                     ++ipv6Seperators;
745                     needsShift = ipv6Seperators == 2 && value == 0;
746                     compressBegin = currentIndex;
747                     compressLength = bytes.length - compressBegin - 2;
748                     ++i;
749                 }
750                 value = 0;
751                 begin = -1;
752                 break;
753             case '.':
754                 ++ipv4Seperators;
755                 if (i - begin > IPV4_MAX_CHAR_BETWEEN_SEPARATOR
756                         || ipv4Seperators > IPV4_SEPARATORS
757                         || (ipv6Seperators > 0 && (currentIndex + compressLength < 12))
758                         || i + 1 >= ipLength
759                         || currentIndex >= bytes.length
760                         || begin < 0
761                         || (begin == 0 && (i == 3 && (!isValidNumericChar(ip.charAt(2)) ||
762                                                       !isValidNumericChar(ip.charAt(1)) ||
763                                                       !isValidNumericChar(ip.charAt(0))) ||
764                                            i == 2 && (!isValidNumericChar(ip.charAt(1)) ||
765                                                       !isValidNumericChar(ip.charAt(0))) ||
766                                            i == 1 && !isValidNumericChar(ip.charAt(0))))) {
767                     return null;
768                 }
769                 value <<= (IPV4_MAX_CHAR_BETWEEN_SEPARATOR - (i - begin)) << 2;
770 
771                 // The value integer holds at most 3 bytes from right (most significant) to left (least significant).
772                 // The following bit shifting is to restructure the bytes to be left (most significant) to
773                 // right (least significant) while also accounting for each IPv4 digit is base 10.
774                 begin = (value & 0xf) * 100 + ((value >> 4) & 0xf) * 10 + ((value >> 8) & 0xf);
775                 if (begin < 0 || begin > 255) {
776                     return null;
777                 }
778                 bytes[currentIndex++] = (byte) begin;
779                 value = 0;
780                 begin = -1;
781                 break;
782             default:
783                 if (!isValidHexChar(c) || (ipv4Seperators > 0 && !isValidNumericChar(c))) {
784                     return null;
785                 }
786                 if (begin < 0) {
787                     begin = i;
788                 } else if (i - begin > IPV6_MAX_CHAR_BETWEEN_SEPARATOR) {
789                     return null;
790                 }
791                 // The value is treated as a sort of array of numbers because we are dealing with
792                 // at most 4 consecutive bytes we can use bit shifting to accomplish this.
793                 // The most significant byte will be encountered first, and reside in the right most
794                 // position of the following integer
795                 value += getIntValue(c) << ((i - begin) << 2);
796                 break;
797             }
798         }
799 
800         final boolean isCompressed = compressBegin > 0;
801         // Finish up last set of data that was accumulated in the loop (or before the loop)
802         if (ipv4Seperators > 0) {
803             if (begin > 0 && i - begin > IPV4_MAX_CHAR_BETWEEN_SEPARATOR ||
804                     ipv4Seperators != IPV4_SEPARATORS ||
805                     currentIndex >= bytes.length) {
806                 return null;
807             }
808             if (ipv6Seperators == 0) {
809                 compressLength = 12;
810             } else if (ipv6Seperators >= IPV6_MIN_SEPARATORS &&
811                            ip.charAt(ipLength - 1) != ':' &&
812                            (!isCompressed && (ipv6Seperators == 6 && ip.charAt(0) != ':') ||
813                             isCompressed && (ipv6Seperators + 1 < IPV6_MAX_SEPARATORS &&
814                                              (ip.charAt(0) != ':' || compressBegin <= 2)))) {
815                 compressLength -= 2;
816             } else {
817                 return null;
818             }
819             value <<= (IPV4_MAX_CHAR_BETWEEN_SEPARATOR - (i - begin)) << 2;
820 
821             // The value integer holds at most 3 bytes from right (most significant) to left (least significant).
822             // The following bit shifting is to restructure the bytes to be left (most significant) to
823             // right (least significant) while also accounting for each IPv4 digit is base 10.
824             begin = (value & 0xf) * 100 + ((value >> 4) & 0xf) * 10 + ((value >> 8) & 0xf);
825             if (begin < 0 || begin > 255) {
826                 return null;
827             }
828             bytes[currentIndex++] = (byte) begin;
829         } else {
830             tmp = ipLength - 1;
831             if (begin > 0 && i - begin > IPV6_MAX_CHAR_BETWEEN_SEPARATOR ||
832                     ipv6Seperators < IPV6_MIN_SEPARATORS ||
833                     !isCompressed && (ipv6Seperators + 1 != IPV6_MAX_SEPARATORS  ||
834                                       ip.charAt(0) == ':' || ip.charAt(tmp) == ':') ||
835                     isCompressed && (ipv6Seperators > IPV6_MAX_SEPARATORS ||
836                         (ipv6Seperators == IPV6_MAX_SEPARATORS &&
837                           (compressBegin <= 2 && ip.charAt(0) != ':' ||
838                            compressBegin >= 14 && ip.charAt(tmp) != ':'))) ||
839                     currentIndex + 1 >= bytes.length) {
840                 return null;
841             }
842             if (begin >= 0 && i - begin <= IPV6_MAX_CHAR_BETWEEN_SEPARATOR) {
843                 value <<= (IPV6_MAX_CHAR_BETWEEN_SEPARATOR - (i - begin)) << 2;
844             }
845             // The value integer holds at most 4 bytes from right (most significant) to left (least significant).
846             // The following bit shifting is used to extract and re-order the individual bytes to achieve a
847             // left (most significant) to right (least significant) ordering.
848             bytes[currentIndex++] = (byte) (((value & 0xf) << 4) | ((value >> 4) & 0xf));
849             bytes[currentIndex++] = (byte) ((((value >> 8) & 0xf) << 4) | ((value >> 12) & 0xf));
850         }
851 
852         i = currentIndex + compressLength;
853         if (needsShift || i >= bytes.length) {
854             // Right shift array
855             if (i >= bytes.length) {
856                 ++compressBegin;
857             }
858             for (i = currentIndex; i < bytes.length; ++i) {
859                 for (begin = bytes.length - 1; begin >= compressBegin; --begin) {
860                     bytes[begin] = bytes[begin - 1];
861                 }
862                 bytes[begin] = 0;
863                 ++compressBegin;
864             }
865         } else {
866             // Selectively move elements
867             for (i = 0; i < compressLength; ++i) {
868                 begin = i + compressBegin;
869                 currentIndex = begin + compressLength;
870                 if (currentIndex < bytes.length) {
871                     bytes[currentIndex] = bytes[begin];
872                     bytes[begin] = 0;
873                 } else {
874                     break;
875                 }
876             }
877         }
878 
879         if (ipv4Mapped && ipv4Seperators > 0 &&
880                 bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0 && bytes[3] == 0 && bytes[4] == 0 &&
881                 bytes[5] == 0 && bytes[6] == 0 && bytes[7] == 0 && bytes[8] == 0 && bytes[9] == 0) {
882             bytes[10] = bytes[11] = (byte) 0xff;
883         }
884 
885         try {
886             return Inet6Address.getByAddress(null, bytes, -1);
887         } catch (UnknownHostException e) {
888             throw new RuntimeException(e); // Should never happen
889         }
890     }
891 
892     /**
893      * Returns the {@link String} representation of an {@link InetAddress}.
894      * <ul>
895      * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li>
896      * <li>Inet6Address results adhere to
897      * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li>
898      * </ul>
899      * <p>
900      * The output does not include Scope ID.
901      * @param ip {@link InetAddress} to be converted to an address string
902      * @return {@code String} containing the text-formatted IP address
903      */
904     public static String toAddressString(InetAddress ip) {
905         return toAddressString(ip, false);
906     }
907 
908     /**
909      * Returns the {@link String} representation of an {@link InetAddress}.
910      * <ul>
911      * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li>
912      * <li>Inet6Address results adhere to
913      * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a> if
914      * {@code ipv4Mapped} is false.  If {@code ipv4Mapped} is true then "IPv4 mapped" format
915      * from <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> will be supported.
916      * The compressed result will always obey the compression rules defined in
917      * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li>
918      * </ul>
919      * <p>
920      * The output does not include Scope ID.
921      * @param ip {@link InetAddress} to be converted to an address string
922      * @param ipv4Mapped
923      * <ul>
924      * <li>{@code true} to stray from strict rfc 5952 and support the "IPv4 mapped" format
925      * defined in <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5">rfc 4291 section 2</a> while still
926      * following the updated guidelines in
927      * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li>
928      * <li>{@code false} to strictly follow rfc 5952</li>
929      * </ul>
930      * @return {@code String} containing the text-formatted IP address
931      */
932     public static String toAddressString(InetAddress ip, boolean ipv4Mapped) {
933         if (ip instanceof Inet4Address) {
934             return ip.getHostAddress();
935         }
936         if (!(ip instanceof Inet6Address)) {
937             throw new IllegalArgumentException("Unhandled type: " + ip.getClass());
938         }
939 
940         final byte[] bytes = ip.getAddress();
941         final int[] words = new int[IPV6_WORD_COUNT];
942         int i;
943         for (i = 0; i < words.length; ++i) {
944             words[i] = ((bytes[i << 1] & 0xff) << 8) | (bytes[(i << 1) + 1] & 0xff);
945         }
946 
947         // Find longest run of 0s, tie goes to first found instance
948         int currentStart = -1;
949         int currentLength = 0;
950         int shortestStart = -1;
951         int shortestLength = 0;
952         for (i = 0; i < words.length; ++i) {
953             if (words[i] == 0) {
954                 if (currentStart < 0) {
955                     currentStart = i;
956                 }
957             } else if (currentStart >= 0) {
958                 currentLength = i - currentStart;
959                 if (currentLength > shortestLength) {
960                     shortestStart = currentStart;
961                     shortestLength = currentLength;
962                 }
963                 currentStart = -1;
964             }
965         }
966         // If the array ends on a streak of zeros, make sure we account for it
967         if (currentStart >= 0) {
968             currentLength = i - currentStart;
969             if (currentLength > shortestLength) {
970                 shortestStart = currentStart;
971                 shortestLength = currentLength;
972             }
973         }
974         // Ignore the longest streak if it is only 1 long
975         if (shortestLength == 1) {
976             shortestLength = 0;
977             shortestStart = -1;
978         }
979 
980         // Translate to string taking into account longest consecutive 0s
981         final int shortestEnd = shortestStart + shortestLength;
982         final StringBuilder b = new StringBuilder(IPV6_MAX_CHAR_COUNT);
983         if (shortestEnd < 0) { // Optimization when there is no compressing needed
984             b.append(Integer.toHexString(words[0]));
985             for (i = 1; i < words.length; ++i) {
986                 b.append(':');
987                 b.append(Integer.toHexString(words[i]));
988             }
989         } else { // General case that can handle compressing (and not compressing)
990             // Loop unroll the first index (so we don't constantly check i==0 cases in loop)
991             final boolean isIpv4Mapped;
992             if (inRangeEndExclusive(0, shortestStart, shortestEnd)) {
993                 b.append("::");
994                 isIpv4Mapped = ipv4Mapped && (shortestEnd == 5 && words[5] == 0xffff);
995             } else {
996                 b.append(Integer.toHexString(words[0]));
997                 isIpv4Mapped = false;
998             }
999             for (i = 1; i < words.length; ++i) {
1000                 if (!inRangeEndExclusive(i, shortestStart, shortestEnd)) {
1001                     if (!inRangeEndExclusive(i - 1, shortestStart, shortestEnd)) {
1002                         // If the last index was not part of the shortened sequence
1003                         if (!isIpv4Mapped || i == 6) {
1004                             b.append(':');
1005                         } else {
1006                             b.append('.');
1007                         }
1008                     }
1009                     if (isIpv4Mapped && i > 5) {
1010                         b.append(words[i] >> 8);
1011                         b.append('.');
1012                         b.append(words[i] & 0xff);
1013                     } else {
1014                         b.append(Integer.toHexString(words[i]));
1015                     }
1016                 } else if (!inRangeEndExclusive(i - 1, shortestStart, shortestEnd)) {
1017                     // If we are in the shortened sequence and the last index was not
1018                     b.append("::");
1019                 }
1020             }
1021         }
1022 
1023         return b.toString();
1024     }
1025 
1026     /**
1027      * Does a range check on {@code value} if is within {@code start} (inclusive) and {@code end} (exclusive).
1028      * @param value The value to checked if is within {@code start} (inclusive) and {@code end} (exclusive)
1029      * @param start The start of the range (inclusive)
1030      * @param end The end of the range (exclusive)
1031      * @return
1032      * <ul>
1033      * <li>{@code true} if {@code value} if is within {@code start} (inclusive) and {@code end} (exclusive)</li>
1034      * <li>{@code false} otherwise</li>
1035      * </ul>
1036      */
1037     private static boolean inRangeEndExclusive(int value, int start, int end) {
1038         return value >= start && value < end;
1039     }
1040 
1041     /**
1042      * A constructor to stop this class being constructed.
1043      */
1044     private NetUtil() {
1045         // Unused
1046     }
1047 }