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    *   https://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  import io.netty.util.internal.ObjectUtil;
22  
23  import java.net.IDN;
24  
25  /**
26   * A socks cmd response.
27   *
28   * @see SocksCmdRequest
29   * @see SocksCmdResponseDecoder
30   */
31  public final class SocksCmdResponse extends SocksResponse {
32      private final SocksCmdStatus cmdStatus;
33  
34      private final SocksAddressType addressType;
35      private final String host;
36      private final int port;
37  
38      // All arrays are initialized on construction time to 0/false/null remove array Initialization
39      private static final byte[] DOMAIN_ZEROED = {0x00};
40      private static final byte[] IPv4_HOSTNAME_ZEROED = {0x00, 0x00, 0x00, 0x00};
41      private static final byte[] IPv6_HOSTNAME_ZEROED = {0x00, 0x00, 0x00, 0x00,
42              0x00, 0x00, 0x00, 0x00,
43              0x00, 0x00, 0x00, 0x00,
44              0x00, 0x00, 0x00, 0x00};
45  
46      public SocksCmdResponse(SocksCmdStatus cmdStatus, SocksAddressType addressType) {
47          this(cmdStatus, addressType, null, 0);
48      }
49  
50      /**
51       * Constructs new response and includes provided host and port as part of it.
52       *
53       * @param cmdStatus status of the response
54       * @param addressType type of host parameter
55       * @param host host (BND.ADDR field) is address that server used when connecting to the target host.
56       *             When null a value of 4/8 0x00 octets will be used for IPv4/IPv6 and a single 0x00 byte will be
57       *             used for domain addressType. Value is converted to ASCII using {@link IDN#toASCII(String)}.
58       * @param port port (BND.PORT field) that the server assigned to connect to the target host
59       * @throws NullPointerException in case cmdStatus or addressType are missing
60       * @throws IllegalArgumentException in case host or port cannot be validated
61       * @see IDN#toASCII(String)
62       */
63      public SocksCmdResponse(SocksCmdStatus cmdStatus, SocksAddressType addressType, String host, int port) {
64          super(SocksResponseType.CMD);
65          ObjectUtil.checkNotNull(cmdStatus, "cmdStatus");
66          ObjectUtil.checkNotNull(addressType, "addressType");
67          if (host != null) {
68              switch (addressType) {
69                  case IPv4:
70                      if (!NetUtil.isValidIpV4Address(host)) {
71                          throw new IllegalArgumentException(host + " is not a valid IPv4 address");
72                      }
73                      break;
74                  case DOMAIN:
75                      String asciiHost = IDN.toASCII(host);
76                      if (asciiHost.length() > 255) {
77                          throw new IllegalArgumentException(host + " IDN: " + asciiHost + " exceeds 255 char limit");
78                      }
79                      host = asciiHost;
80                      break;
81                  case IPv6:
82                      if (!NetUtil.isValidIpV6Address(host)) {
83                          throw new IllegalArgumentException(host + " is not a valid IPv6 address");
84                      }
85                      break;
86                  case UNKNOWN:
87                      break;
88              }
89          }
90          if (port < 0 || port > 65535) {
91              throw new IllegalArgumentException(port + " is not in bounds 0 <= x <= 65535");
92          }
93          this.cmdStatus = cmdStatus;
94          this.addressType = addressType;
95          this.host = host;
96          this.port = port;
97      }
98  
99      /**
100      * Returns the {@link SocksCmdStatus} of this {@link SocksCmdResponse}
101      *
102      * @return The {@link SocksCmdStatus} of this {@link SocksCmdResponse}
103      */
104     public SocksCmdStatus cmdStatus() {
105         return cmdStatus;
106     }
107 
108     /**
109      * Returns the {@link SocksAddressType} of this {@link SocksCmdResponse}
110      *
111      * @return The {@link SocksAddressType} of this {@link SocksCmdResponse}
112      */
113     public SocksAddressType addressType() {
114         return addressType;
115     }
116 
117     /**
118      * Returns host that is used as a parameter in {@link SocksCmdType}.
119      * Host (BND.ADDR field in response) is address that server used when connecting to the target host.
120      * This is typically different from address which client uses to connect to the SOCKS server.
121      *
122      * @return host that is used as a parameter in {@link SocksCmdType}
123      *         or null when there was no host specified during response construction
124      */
125     public String host() {
126         return host != null && addressType == SocksAddressType.DOMAIN ? IDN.toUnicode(host) : host;
127     }
128 
129     /**
130      * Returns port that is used as a parameter in {@link SocksCmdType}.
131      * Port (BND.PORT field in response) is port that the server assigned to connect to the target host.
132      *
133      * @return port that is used as a parameter in {@link SocksCmdType}
134      */
135     public int port() {
136         return port;
137     }
138 
139     @Override
140     public void encodeAsByteBuf(ByteBuf byteBuf) {
141         byteBuf.writeByte(protocolVersion().byteValue());
142         byteBuf.writeByte(cmdStatus.byteValue());
143         byteBuf.writeByte(0x00);
144         byteBuf.writeByte(addressType.byteValue());
145         switch (addressType) {
146             case IPv4: {
147                 byte[] hostContent = host == null ?
148                         IPv4_HOSTNAME_ZEROED : NetUtil.createByteArrayFromIpAddressString(host);
149                 byteBuf.writeBytes(hostContent);
150                 byteBuf.writeShort(port);
151                 break;
152             }
153             case DOMAIN: {
154                 if (host != null) {
155                     byteBuf.writeByte(host.length());
156                     byteBuf.writeCharSequence(host, CharsetUtil.US_ASCII);
157                 } else {
158                     byteBuf.writeByte(DOMAIN_ZEROED.length);
159                     byteBuf.writeBytes(DOMAIN_ZEROED);
160                 }
161                 byteBuf.writeShort(port);
162                 break;
163             }
164             case IPv6: {
165                 byte[] hostContent = host == null
166                         ? IPv6_HOSTNAME_ZEROED : NetUtil.createByteArrayFromIpAddressString(host);
167                 byteBuf.writeBytes(hostContent);
168                 byteBuf.writeShort(port);
169                 break;
170             }
171         }
172     }
173 }