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