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.v5;
17  
18  import io.netty.handler.codec.DecoderResult;
19  import io.netty.util.NetUtil;
20  import io.netty.util.internal.StringUtil;
21  
22  import java.net.IDN;
23  
24  /**
25   * The default {@link Socks5CommandResponse}.
26   */
27  public final class DefaultSocks5CommandResponse extends AbstractSocks5Message implements Socks5CommandResponse {
28  
29      private final Socks5CommandStatus status;
30      private final Socks5AddressType bndAddrType;
31      private final String bndAddr;
32      private final int bndPort;
33  
34      public DefaultSocks5CommandResponse(Socks5CommandStatus status, Socks5AddressType bndAddrType) {
35          this(status, bndAddrType, null, 0);
36      }
37  
38      public DefaultSocks5CommandResponse(
39              Socks5CommandStatus status, Socks5AddressType bndAddrType, String bndAddr, int bndPort) {
40  
41          if (status == null) {
42              throw new NullPointerException("status");
43          }
44          if (bndAddrType == null) {
45              throw new NullPointerException("bndAddrType");
46          }
47          if (bndAddr != null) {
48              if (bndAddrType == Socks5AddressType.IPv4) {
49                  if (!NetUtil.isValidIpV4Address(bndAddr)) {
50                      throw new IllegalArgumentException("bndAddr: " + bndAddr + " (expected: a valid IPv4 address)");
51                  }
52              } else if (bndAddrType == Socks5AddressType.DOMAIN) {
53                  bndAddr = IDN.toASCII(bndAddr);
54                  if (bndAddr.length() > 255) {
55                      throw new IllegalArgumentException("bndAddr: " + bndAddr + " (expected: less than 256 chars)");
56                  }
57              } else if (bndAddrType == Socks5AddressType.IPv6) {
58                  if (!NetUtil.isValidIpV6Address(bndAddr)) {
59                      throw new IllegalArgumentException("bndAddr: " + bndAddr + " (expected: a valid IPv6 address)");
60                  }
61              }
62          }
63  
64          if (bndPort < 0 || bndPort > 65535) {
65              throw new IllegalArgumentException("bndPort: " + bndPort + " (expected: 0~65535)");
66          }
67          this.status = status;
68          this.bndAddrType = bndAddrType;
69          this.bndAddr = bndAddr;
70          this.bndPort = bndPort;
71      }
72  
73      @Override
74      public Socks5CommandStatus status() {
75          return status;
76      }
77  
78      @Override
79      public Socks5AddressType bndAddrType() {
80          return bndAddrType;
81      }
82  
83      @Override
84      public String bndAddr() {
85          return bndAddr;
86      }
87  
88      @Override
89      public int bndPort() {
90          return bndPort;
91      }
92  
93      @Override
94      public String toString() {
95          StringBuilder buf = new StringBuilder(128);
96          buf.append(StringUtil.simpleClassName(this));
97  
98          DecoderResult decoderResult = decoderResult();
99          if (!decoderResult.isSuccess()) {
100             buf.append("(decoderResult: ");
101             buf.append(decoderResult);
102             buf.append(", status: ");
103         } else {
104             buf.append("(status: ");
105         }
106         buf.append(status());
107         buf.append(", bndAddrType: ");
108         buf.append(bndAddrType());
109         buf.append(", bndAddr: ");
110         buf.append(bndAddr());
111         buf.append(", bndPort: ");
112         buf.append(bndPort());
113         buf.append(')');
114 
115         return buf.toString();
116     }
117 }