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  import org.jboss.netty.buffer.ChannelBuffer;
19  
20  /**
21   * An socks cmd response.
22   *
23   * @see SocksCmdRequest
24   * @see SocksCmdResponseDecoder
25   */
26  public final class SocksCmdResponse extends SocksResponse {
27      private final CmdStatus cmdStatus;
28  
29      private final AddressType addressType;
30      // All arrays are initialized on construction time to 0/false/null remove array Initialization
31      private static final byte[] IPv4_HOSTNAME_ZEROED = {0x00, 0x00, 0x00, 0x00};
32      private static final byte[] IPv6_HOSTNAME_ZEROED = {0x00, 0x00, 0x00, 0x00,
33              0x00, 0x00, 0x00, 0x00,
34              0x00, 0x00, 0x00, 0x00,
35              0x00, 0x00, 0x00, 0x00};
36  
37      public SocksCmdResponse(CmdStatus cmdStatus, AddressType addressType) {
38          super(SocksResponseType.CMD);
39          if (cmdStatus == null) {
40              throw new NullPointerException("cmdStatus");
41          }
42          if (addressType == null) {
43              throw new NullPointerException("addressType");
44          }
45          this.cmdStatus = cmdStatus;
46          this.addressType = addressType;
47      }
48  
49      /**
50       * Returns the {@link CmdStatus} of this {@link SocksCmdResponse}
51       *
52       * @return The {@link CmdStatus} of this {@link SocksCmdResponse}
53       */
54      public CmdStatus getCmdStatus() {
55          return cmdStatus;
56      }
57  
58      /**
59       * Returns the {@link AddressType} of this {@link SocksCmdResponse}
60       *
61       * @return The {@link AddressType} of this {@link SocksCmdResponse}
62       */
63      public AddressType getAddressType() {
64          return addressType;
65      }
66  
67      @Override
68      public void encodeAsByteBuf(ChannelBuffer channelBuffer) {
69          channelBuffer.writeByte(getProtocolVersion().getByteValue());
70          channelBuffer.writeByte(cmdStatus.getByteValue());
71          channelBuffer.writeByte(0x00);
72          channelBuffer.writeByte(addressType.getByteValue());
73          switch (addressType) {
74              case IPv4: {
75                  channelBuffer.writeBytes(IPv4_HOSTNAME_ZEROED);
76                  channelBuffer.writeShort(0);
77                  break;
78              }
79              case DOMAIN: {
80                  channelBuffer.writeByte(1);   // domain length
81                  channelBuffer.writeByte(0);   // domain value
82                  channelBuffer.writeShort(0);  // port value
83                  break;
84              }
85              case IPv6: {
86                  channelBuffer.writeBytes(IPv6_HOSTNAME_ZEROED);
87                  channelBuffer.writeShort(0);
88                  break;
89              }
90          }
91      }
92  }