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.channel.ChannelHandlerContext;
20  import io.netty.handler.codec.ReplayingDecoder;
21  import io.netty.handler.codec.socks.SocksCmdResponseDecoder.State;
22  import io.netty.util.NetUtil;
23  
24  import java.util.List;
25  
26  /**
27   * Decodes {@link ByteBuf}s into {@link SocksCmdResponse}.
28   * Before returning SocksResponse decoder removes itself from pipeline.
29   */
30  public class SocksCmdResponseDecoder extends ReplayingDecoder<State> {
31  
32      private SocksCmdStatus cmdStatus;
33      private SocksAddressType addressType;
34  
35      public SocksCmdResponseDecoder() {
36          super(State.CHECK_PROTOCOL_VERSION);
37      }
38  
39      @Override
40      protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
41          switch (state()) {
42              case CHECK_PROTOCOL_VERSION: {
43                  if (byteBuf.readByte() != SocksProtocolVersion.SOCKS5.byteValue()) {
44                      out.add(SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE);
45                      break;
46                  }
47                  checkpoint(State.READ_CMD_HEADER);
48              }
49              case READ_CMD_HEADER: {
50                  cmdStatus = SocksCmdStatus.valueOf(byteBuf.readByte());
51                  byteBuf.skipBytes(1); // reserved
52                  addressType = SocksAddressType.valueOf(byteBuf.readByte());
53                  checkpoint(State.READ_CMD_ADDRESS);
54              }
55              case READ_CMD_ADDRESS: {
56                  switch (addressType) {
57                      case IPv4: {
58                          String host = NetUtil.intToIpAddress(byteBuf.readInt());
59                          int port = byteBuf.readUnsignedShort();
60                          out.add(new SocksCmdResponse(cmdStatus, addressType, host, port));
61                          break;
62                      }
63                      case DOMAIN: {
64                          int fieldLength = byteBuf.readByte();
65                          String host = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
66                          int port = byteBuf.readUnsignedShort();
67                          out.add(new SocksCmdResponse(cmdStatus, addressType, host, port));
68                          break;
69                      }
70                      case IPv6: {
71                          byte[] bytes = new byte[16];
72                          byteBuf.readBytes(bytes);
73                          String host = SocksCommonUtils.ipv6toStr(bytes);
74                          int port = byteBuf.readUnsignedShort();
75                          out.add(new SocksCmdResponse(cmdStatus, addressType, host, port));
76                          break;
77                      }
78                      case UNKNOWN: {
79                          out.add(SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE);
80                          break;
81                      }
82                      default: {
83                          throw new Error();
84                      }
85                  }
86                  break;
87              }
88              default: {
89                  throw new Error();
90              }
91          }
92          ctx.pipeline().remove(this);
93      }
94  
95      enum State {
96          CHECK_PROTOCOL_VERSION,
97          READ_CMD_HEADER,
98          READ_CMD_ADDRESS
99      }
100 }