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 org.jboss.netty.handler.codec.socks;
17  
18  final class SocksCommonUtils {
19      public static final SocksRequest UNKNOWN_SOCKS_REQUEST = new UnknownSocksRequest();
20      public static final SocksResponse UNKNOWN_SOCKS_RESPONSE = new UnknownSocksResponse();
21  
22      private static final int SECOND_ADDRESS_OCTET_SHIFT = 16;
23      private static final int FIRST_ADDRESS_OCTET_SHIFT = 24;
24      private static final int THIRD_ADDRESS_OCTET_SHIFT = 8;
25      private static final int XOR_DEFAULT_VALUE = 0xff;
26  
27      /**
28       * A constructor to stop this class being constructed.
29       */
30      private SocksCommonUtils() {
31          //NOOP
32      }
33  
34      public static String intToIp(int i) {
35          return String.valueOf(i >> FIRST_ADDRESS_OCTET_SHIFT & XOR_DEFAULT_VALUE) + '.' +
36                 (i >> SECOND_ADDRESS_OCTET_SHIFT & XOR_DEFAULT_VALUE) + '.' +
37                 (i >> THIRD_ADDRESS_OCTET_SHIFT & XOR_DEFAULT_VALUE) + '.' +
38                 (i & XOR_DEFAULT_VALUE);
39      }
40  
41      private static final char[] ipv6conseqZeroFiller = {':', ':'};
42      private static final char ipv6hextetSeparator = ':';
43  
44      /*
45      * Convert numeric IPv6 to compressed format, where
46      * the longest sequence of 0's (with 2 or more 0's) is replaced with "::"
47      */
48      public static String ipv6toCompressedForm(byte[] src) {
49          assert src.length == 16;
50          //Find the longest sequence of 0's
51          //start of compressed region (hextet index)
52          int cmprHextet = -1;
53          //length of compressed region
54          int cmprSize = 0;
55          for (int hextet = 0; hextet < 8;) {
56              int curByte = hextet * 2;
57              int size = 0;
58              while (curByte < src.length && src[curByte] == 0
59                      && src[curByte + 1] == 0) {
60                  curByte += 2;
61                  size++;
62              }
63              if (size > cmprSize) {
64                  cmprHextet = hextet;
65                  cmprSize = size;
66              }
67              hextet = curByte / 2 + 1;
68          }
69          if (cmprHextet == -1 || cmprSize < 2) {
70              //No compression can be applied
71              return ipv6toStr(src);
72          }
73          StringBuilder sb = new StringBuilder(39);
74          ipv6toStr(sb, src, 0, cmprHextet);
75          sb.append(ipv6conseqZeroFiller);
76          ipv6toStr(sb, src, cmprHextet + cmprSize, 8);
77          return sb.toString();
78      }
79  
80      /*
81      * Convert numeric IPv6 to standard (non-compressed) format.
82      *
83      * Borrowed from Inet6Address.java #numericToTextFormat(byte[])
84      * Changed StringBuffer -> StringBuilder and ":" -> ':' for performance.
85      */
86      public static String ipv6toStr(byte[] src) {
87          assert src.length == 16;
88          StringBuilder sb = new StringBuilder(39);
89          ipv6toStr(sb, src, 0, 8);
90          return sb.toString();
91      }
92  
93      private static void ipv6toStr(StringBuilder sb, byte[] src,
94                                    int fromHextet, int toHextet) {
95          for (int i = fromHextet; i < toHextet; i++) {
96              sb.append(Integer.toHexString(src[i << 1] << 8 & 0xff00
97                      | src[(i << 1) + 1] & 0xff));
98              if (i < toHextet - 1) {
99                  sb.append(ipv6hextetSeparator);
100             }
101         }
102     }
103 }