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 org.jboss.netty.handler.codec.socks;
17  
18  import org.jboss.netty.buffer.ChannelBuffer;
19  
20  /**
21   * An abstract class that defines a SocksMessage, providing common properties for
22   * {@link SocksRequest} and {@link SocksResponse}.
23   *
24   * @see SocksRequest
25   * @see SocksResponse
26   */
27  
28  public abstract class SocksMessage {
29      private final MessageType messageType;
30      private final ProtocolVersion protocolVersion = ProtocolVersion.SOCKS5;
31  
32      protected SocksMessage(MessageType messageType) {
33          if (messageType == null) {
34              throw new NullPointerException("messageType");
35          }
36          this.messageType = messageType;
37      }
38  
39      /**
40       * Returns the {@link MessageType} of this {@link SocksMessage}
41       *
42       * @return The {@link MessageType} of this {@link SocksMessage}
43       */
44      public MessageType getMessageType() {
45          return messageType;
46      }
47  
48      public enum MessageType {
49          REQUEST,
50          RESPONSE,
51          UNKNOWN
52      }
53  
54      public enum AuthScheme {
55          NO_AUTH((byte) 0x00),
56          AUTH_GSSAPI((byte) 0x01),
57          AUTH_PASSWORD((byte) 0x02),
58          UNKNOWN((byte) 0xff);
59  
60          private final byte b;
61  
62          AuthScheme(byte b) {
63              this.b = b;
64          }
65  
66          public static AuthScheme fromByte(byte b) {
67              for (AuthScheme code : values()) {
68                  if (code.b == b) {
69                      return code;
70                  }
71              }
72              return UNKNOWN;
73          }
74  
75          public byte getByteValue() {
76              return b;
77          }
78      }
79  
80      public enum CmdType {
81          CONNECT((byte) 0x01),
82          BIND((byte) 0x02),
83          UDP((byte) 0x03),
84          UNKNOWN((byte) 0xff);
85  
86          private final byte b;
87  
88          CmdType(byte b) {
89              this.b = b;
90          }
91  
92          public static CmdType fromByte(byte b) {
93              for (CmdType code : values()) {
94                  if (code.b == b) {
95                      return code;
96                  }
97              }
98              return UNKNOWN;
99          }
100 
101         public byte getByteValue() {
102             return b;
103         }
104     }
105 
106     public enum AddressType {
107         IPv4((byte) 0x01),
108         DOMAIN((byte) 0x03),
109         IPv6((byte) 0x04),
110         UNKNOWN((byte) 0xff);
111 
112         private final byte b;
113 
114         AddressType(byte b) {
115             this.b = b;
116         }
117 
118         public static AddressType fromByte(byte b) {
119             for (AddressType code : values()) {
120                 if (code.b == b) {
121                     return code;
122                 }
123             }
124             return UNKNOWN;
125         }
126 
127         public byte getByteValue() {
128             return b;
129         }
130     }
131 
132     public enum AuthStatus {
133         SUCCESS((byte) 0x00),
134         FAILURE((byte) 0xff);
135 
136         private final byte b;
137 
138         AuthStatus(byte b) {
139             this.b = b;
140         }
141 
142         public static AuthStatus fromByte(byte b) {
143             for (AuthStatus code : values()) {
144                 if (code.b == b) {
145                     return code;
146                 }
147             }
148             return FAILURE;
149         }
150 
151         public byte getByteValue() {
152             return b;
153         }
154     }
155 
156     public enum CmdStatus {
157         SUCCESS((byte) 0x00),
158         FAILURE((byte) 0x01),
159         FORBIDDEN((byte) 0x02),
160         NETWORK_UNREACHABLE((byte) 0x03),
161         HOST_UNREACHABLE((byte) 0x04),
162         REFUSED((byte) 0x05),
163         TTL_EXPIRED((byte) 0x06),
164         COMMAND_NOT_SUPPORTED((byte) 0x07),
165         ADDRESS_NOT_SUPPORTED((byte) 0x08),
166         UNASSIGNED((byte) 0xff);
167 
168         private final byte b;
169 
170         CmdStatus(byte b) {
171             this.b = b;
172         }
173 
174         public static CmdStatus fromByte(byte b) {
175             for (CmdStatus code : values()) {
176                 if (code.b == b) {
177                     return code;
178                 }
179             }
180             return UNASSIGNED;
181         }
182 
183         public byte getByteValue() {
184             return b;
185         }
186     }
187 
188     public enum ProtocolVersion {
189         SOCKS4a((byte) 0x04),
190         SOCKS5((byte) 0x05),
191         UNKNOWN((byte) 0xff);
192 
193         private final byte b;
194 
195         ProtocolVersion(byte b) {
196             this.b = b;
197         }
198 
199         public static ProtocolVersion fromByte(byte b) {
200             for (ProtocolVersion code : values()) {
201                 if (code.b == b) {
202                     return code;
203                 }
204             }
205             return UNKNOWN;
206         }
207 
208         public byte getByteValue() {
209             return b;
210         }
211     }
212 
213     public enum SubnegotiationVersion {
214         AUTH_PASSWORD((byte) 0x01),
215         UNKNOWN((byte) 0xff);
216 
217         private final byte b;
218 
219         SubnegotiationVersion(byte b) {
220             this.b = b;
221         }
222 
223         public static SubnegotiationVersion fromByte(byte b) {
224             for (SubnegotiationVersion code : values()) {
225                 if (code.b == b) {
226                     return code;
227                 }
228             }
229             return UNKNOWN;
230         }
231 
232         public byte getByteValue() {
233             return b;
234         }
235     }
236 
237     /**
238      * Returns the {@link ProtocolVersion} of this {@link SocksMessage}
239      *
240      * @return The {@link ProtocolVersion} of this {@link SocksMessage}
241      */
242     public ProtocolVersion getProtocolVersion() {
243         return protocolVersion;
244     }
245 
246     /**
247      * Encode socks message into its byte representation and write it into byteBuf
248      *
249      * @see ChannelBuffer
250      */
251     public abstract void encodeAsByteBuf(ChannelBuffer channelBuffer) throws Exception;
252 }