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                  if (IDN.toASCII(host).length() > 255) {
55                      throw new IllegalArgumentException(host + " IDN: " + IDN.toASCII(host) + " exceeds 255 char limit");
56                  }
57                  break;
58              case IPv6:
59                  if (!NetUtil.isValidIpV6Address(host)) {
60                      throw new IllegalArgumentException(host + " is not a valid IPv6 address");
61                  }
62                  break;
63              case UNKNOWN:
64                  break;
65          }
66          if (port <= 0 || port >= 65536) {
67              throw new IllegalArgumentException(port + " is not in bounds 0 < x < 65536");
68          }
69          this.cmdType = cmdType;
70          this.addressType = addressType;
71          this.host = IDN.toASCII(host);
72          this.port = port;
73      }
74  
75      /**
76       * Returns the {@link SocksCmdType} of this {@link SocksCmdRequest}
77       *
78       * @return The {@link SocksCmdType} of this {@link SocksCmdRequest}
79       */
80      public SocksCmdType cmdType() {
81          return cmdType;
82      }
83  
84      /**
85       * Returns the {@link SocksAddressType} of this {@link SocksCmdRequest}
86       *
87       * @return The {@link SocksAddressType} of this {@link SocksCmdRequest}
88       */
89      public SocksAddressType addressType() {
90          return addressType;
91      }
92  
93      /**
94       * Returns host that is used as a parameter in {@link SocksCmdType}
95       *
96       * @return host that is used as a parameter in {@link SocksCmdType}
97       */
98      public String host() {
99          return IDN.toUnicode(host);
100     }
101 
102     /**
103      * Returns port that is used as a parameter in {@link SocksCmdType}
104      *
105      * @return port that is used as a parameter in {@link SocksCmdType}
106      */
107     public int port() {
108         return port;
109     }
110 
111     @Override
112     public void encodeAsByteBuf(ByteBuf byteBuf) {
113         byteBuf.writeByte(protocolVersion().byteValue());
114         byteBuf.writeByte(cmdType.byteValue());
115         byteBuf.writeByte(0x00);
116         byteBuf.writeByte(addressType.byteValue());
117         switch (addressType) {
118             case IPv4: {
119                 byteBuf.writeBytes(NetUtil.createByteArrayFromIpAddressString(host));
120                 byteBuf.writeShort(port);
121                 break;
122             }
123 
124             case DOMAIN: {
125                 byteBuf.writeByte(host.length());
126                 byteBuf.writeBytes(host.getBytes(CharsetUtil.US_ASCII));
127                 byteBuf.writeShort(port);
128                 break;
129             }
130 
131             case IPv6: {
132                 byteBuf.writeBytes(NetUtil.createByteArrayFromIpAddressString(host));
133                 byteBuf.writeShort(port);
134                 break;
135             }
136         }
137     }
138 }