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