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.handler.codec.DecoderResult;
19  import io.netty.util.NetUtil;
20  import io.netty.util.internal.StringUtil;
21  
22  /**
23   * The default {@link Socks4CommandResponse}.
24   */
25  public class DefaultSocks4CommandResponse extends AbstractSocks4Message implements Socks4CommandResponse {
26  
27      private final Socks4CommandStatus status;
28      private final String dstAddr;
29      private final int dstPort;
30  
31      /**
32       * Creates a new instance.
33       *
34       * @param status the status of the response
35       */
36      public DefaultSocks4CommandResponse(Socks4CommandStatus status) {
37          this(status, null, 0);
38      }
39  
40      /**
41       * Creates a new instance.
42       *
43       * @param status the status of the response
44       * @param dstAddr the {@code DSTIP} field of the response
45       * @param dstPort the {@code DSTPORT} field of the response
46       */
47      public DefaultSocks4CommandResponse(Socks4CommandStatus status, String dstAddr, int dstPort) {
48          if (status == null) {
49              throw new NullPointerException("cmdStatus");
50          }
51          if (dstAddr != null) {
52              if (!NetUtil.isValidIpV4Address(dstAddr)) {
53                  throw new IllegalArgumentException(
54                          "dstAddr: " + dstAddr + " (expected: a valid IPv4 address)");
55              }
56          }
57          if (dstPort < 0 || dstPort > 65535) {
58              throw new IllegalArgumentException("dstPort: " + dstPort + " (expected: 0~65535)");
59          }
60  
61          this.status = status;
62          this.dstAddr = dstAddr;
63          this.dstPort = dstPort;
64      }
65  
66      @Override
67      public Socks4CommandStatus status() {
68          return status;
69      }
70  
71      @Override
72      public String dstAddr() {
73          return dstAddr;
74      }
75  
76      @Override
77      public int dstPort() {
78          return dstPort;
79      }
80  
81      @Override
82      public String toString() {
83          StringBuilder buf = new StringBuilder(96);
84          buf.append(StringUtil.simpleClassName(this));
85  
86          DecoderResult decoderResult = decoderResult();
87          if (!decoderResult.isSuccess()) {
88              buf.append("(decoderResult: ");
89              buf.append(decoderResult);
90              buf.append(", dstAddr: ");
91          } else {
92              buf.append("(dstAddr: ");
93          }
94          buf.append(dstAddr());
95          buf.append(", dstPort: ");
96          buf.append(dstPort());
97          buf.append(')');
98  
99          return buf.toString();
100     }
101 }