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.socksx.v4;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.handler.codec.DecoderException;
21  import io.netty.handler.codec.DecoderResult;
22  import io.netty.handler.codec.ReplayingDecoder;
23  import io.netty.handler.codec.socksx.v4.Socks4ClientDecoder.State;
24  import io.netty.util.NetUtil;
25  
26  import java.util.List;
27  
28  /**
29   * Decodes a single {@link Socks4CommandResponse} from the inbound {@link ByteBuf}s.
30   * On successful decode, this decoder will forward the received data to the next handler, so that
31   * other handler can remove this decoder later.  On failed decode, this decoder will discard the
32   * received data, so that other handler closes the connection later.
33   */
34  public class Socks4ClientDecoder extends ReplayingDecoder<State> {
35  
36      enum State {
37          START,
38          SUCCESS,
39          FAILURE
40      }
41  
42      public Socks4ClientDecoder() {
43          super(State.START);
44          setSingleDecode(true);
45      }
46  
47      @Override
48      protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
49          try {
50              switch (state()) {
51              case START: {
52                  final int version = in.readUnsignedByte();
53                  if (version != 0) {
54                      throw new DecoderException("unsupported reply version: " + version + " (expected: 0)");
55                  }
56  
57                  final Socks4CommandStatus status = Socks4CommandStatus.valueOf(in.readByte());
58                  final int dstPort = in.readUnsignedShort();
59                  final String dstAddr = NetUtil.intToIpAddress(in.readInt());
60  
61                  out.add(new DefaultSocks4CommandResponse(status, dstAddr, dstPort));
62                  checkpoint(State.SUCCESS);
63              }
64              case SUCCESS: {
65                  int readableBytes = actualReadableBytes();
66                  if (readableBytes > 0) {
67                      out.add(in.readSlice(readableBytes).retain());
68                  }
69                  break;
70              }
71              case FAILURE: {
72                  in.skipBytes(actualReadableBytes());
73                  break;
74              }
75              }
76          } catch (Exception e) {
77              fail(out, e);
78          }
79      }
80  
81      private void fail(List<Object> out, Throwable cause) {
82          if (!(cause instanceof DecoderException)) {
83              cause = new DecoderException(cause);
84          }
85  
86          Socks4CommandResponse m = new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED);
87          m.setDecoderResult(DecoderResult.failure(cause));
88          out.add(m);
89  
90          checkpoint(State.FAILURE);
91      }
92  }