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.NetUtil;
21  
22  import java.net.IDN;
23  
24  /**
25   * An socks cmd request.
26   *
27   * @see SocksCmdResponse
28   * @see SocksCmdRequestDecoder
29   */
30  public final class SocksCmdRequest extends SocksRequest {
31      private final SocksCmdType cmdType;
32      private final SocksAddressType addressType;
33      private final String host;
34      private final int port;
35  
36      public SocksCmdRequest(SocksCmdType cmdType, SocksAddressType addressType, String host, int port) {
37          super(SocksRequestType.CMD);
38          if (cmdType == null) {
39              throw new NullPointerException("cmdType");
40          }
41          if (addressType == null) {
42              throw new NullPointerException("addressType");
43          }
44          if (host == null) {
45              throw new NullPointerException("host");
46          }
47          switch (addressType) {
48              case IPv4:
49                  if (!NetUtil.isValidIpV4Address(host)) {
50                      throw new IllegalArgumentException(host + " is not a valid IPv4 address");
51                  }
52                  break;
53              case DOMAIN:
54                  String asciiHost = IDN.toASCII(host);
55                  if (asciiHost.length() > 255) {
56                      throw new IllegalArgumentException(host + " IDN: " + asciiHost + " exceeds 255 char limit");
57                  }
58                  host = asciiHost;
59                  break;
60              case IPv6:
61                  if (!NetUtil.isValidIpV6Address(host)) {
62                      throw new IllegalArgumentException(host + " is not a valid IPv6 address");
63                  }
64                  break;
65              case UNKNOWN:
66                  break;
67          }
68          if (port <= 0 || port >= 65536) {
69              throw new IllegalArgumentException(port + " is not in bounds 0 < x < 65536");
70          }
71          this.cmdType = cmdType;
72          this.addressType = addressType;
73          this.host = host;
74          this.port = port;
75      }
76  
77      /**
78       * Returns the {@link SocksCmdType} of this {@link SocksCmdRequest}
79       *
80       * @return The {@link SocksCmdType} of this {@link SocksCmdRequest}
81       */
82      public SocksCmdType cmdType() {
83          return cmdType;
84      }
85  
86      /**
87       * Returns the {@link SocksAddressType} of this {@link SocksCmdRequest}
88       *
89       * @return The {@link SocksAddressType} of this {@link SocksCmdRequest}
90       */
91      public SocksAddressType addressType() {
92          return addressType;
93      }
94  
95      /**
96       * Returns host that is used as a parameter in {@link SocksCmdType}
97       *
98       * @return host that is used as a parameter in {@link SocksCmdType}
99       */
100     public String host() {
101         return addressType == SocksAddressType.DOMAIN ? IDN.toUnicode(host) : host;
102     }
103 
104     /**
105      * Returns port that is used as a parameter in {@link SocksCmdType}
106      *
107      * @return port that is used as a parameter in {@link SocksCmdType}
108      */
109     public int port() {
110         return port;
111     }
112 
113     @Override
114     public void encodeAsByteBuf(ByteBuf byteBuf) {
115         byteBuf.writeByte(protocolVersion().byteValue());
116         byteBuf.writeByte(cmdType.byteValue());
117         byteBuf.writeByte(0x00);
118         byteBuf.writeByte(addressType.byteValue());
119         switch (addressType) {
120             case IPv4: {
121                 byteBuf.writeBytes(NetUtil.createByteArrayFromIpAddressString(host));
122                 byteBuf.writeShort(port);
123                 break;
124             }
125 
126             case DOMAIN: {
127                 byteBuf.writeByte(host.length());
128                 byteBuf.writeCharSequence(host, CharsetUtil.US_ASCII);
129                 byteBuf.writeShort(port);
130                 break;
131             }
132 
133             case IPv6: {
134                 byteBuf.writeBytes(NetUtil.createByteArrayFromIpAddressString(host));
135                 byteBuf.writeShort(port);
136                 break;
137             }
138         }
139     }
140 }