View Javadoc
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.handler.codec.socksx.v5;
18  
19  import io.netty.buffer.ByteBuf;
20  import io.netty.buffer.ByteBufUtil;
21  import io.netty.handler.codec.DecoderException;
22  import io.netty.util.CharsetUtil;
23  import io.netty.util.NetUtil;
24  
25  /**
26   * Decodes a SOCKS5 address field into its string representation.
27   *
28   * @see Socks5CommandRequestDecoder
29   * @see Socks5CommandResponseDecoder
30   */
31  public interface Socks5AddressDecoder {
32  
33      Socks5AddressDecoder DEFAULT = new Socks5AddressDecoder() {
34  
35          private static final int IPv6_LEN = 16;
36  
37          @Override
38          public String decodeAddress(Socks5AddressType addrType, ByteBuf in) throws Exception {
39              if (addrType == Socks5AddressType.IPv4) {
40                  return NetUtil.intToIpAddress(ByteBufUtil.readIntBE(in));
41              }
42              if (addrType == Socks5AddressType.DOMAIN) {
43                  final int length = in.readUnsignedByte();
44                  final String domain = in.toString(in.readerIndex(), length, CharsetUtil.US_ASCII);
45                  in.skipBytes(length);
46                  return domain;
47              }
48              if (addrType == Socks5AddressType.IPv6) {
49                  if (in.hasArray()) {
50                      final int readerIdx = in.readerIndex();
51                      in.readerIndex(readerIdx + IPv6_LEN);
52                      return NetUtil.bytesToIpAddress(in.array(), in.arrayOffset() + readerIdx, IPv6_LEN);
53                  } else {
54                      byte[] tmp = new byte[IPv6_LEN];
55                      in.readBytes(tmp);
56                      return NetUtil.bytesToIpAddress(tmp);
57                  }
58              } else {
59                  throw new DecoderException("unsupported address type: " + (addrType.byteValue() & 0xFF));
60              }
61          }
62      };
63  
64      /**
65       * Decodes a SOCKS5 address field into its string representation.
66       *
67       * @param addrType the type of the address
68       * @param in the input buffer which contains the SOCKS5 address field at its reader index
69       */
70      String decodeAddress(Socks5AddressType addrType, ByteBuf in) throws Exception;
71  }