1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.socks;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19
20
21
22
23
24
25
26 public final class SocksCmdResponse extends SocksResponse {
27 private final CmdStatus cmdStatus;
28
29 private final AddressType addressType;
30
31 private static final byte[] IPv4_HOSTNAME_ZEROED = {0x00, 0x00, 0x00, 0x00};
32 private static final byte[] IPv6_HOSTNAME_ZEROED = {0x00, 0x00, 0x00, 0x00,
33 0x00, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0x00, 0x00,
35 0x00, 0x00, 0x00, 0x00};
36
37 public SocksCmdResponse(CmdStatus cmdStatus, AddressType addressType) {
38 super(SocksResponseType.CMD);
39 if (cmdStatus == null) {
40 throw new NullPointerException("cmdStatus");
41 }
42 if (addressType == null) {
43 throw new NullPointerException("addressType");
44 }
45 this.cmdStatus = cmdStatus;
46 this.addressType = addressType;
47 }
48
49
50
51
52
53
54 public CmdStatus getCmdStatus() {
55 return cmdStatus;
56 }
57
58
59
60
61
62
63 public AddressType getAddressType() {
64 return addressType;
65 }
66
67 @Override
68 public void encodeAsByteBuf(ChannelBuffer channelBuffer) {
69 channelBuffer.writeByte(getProtocolVersion().getByteValue());
70 channelBuffer.writeByte(cmdStatus.getByteValue());
71 channelBuffer.writeByte(0x00);
72 channelBuffer.writeByte(addressType.getByteValue());
73 switch (addressType) {
74 case IPv4: {
75 channelBuffer.writeBytes(IPv4_HOSTNAME_ZEROED);
76 channelBuffer.writeShort(0);
77 break;
78 }
79 case DOMAIN: {
80 channelBuffer.writeByte(1);
81 channelBuffer.writeByte(0);
82 channelBuffer.writeShort(0);
83 break;
84 }
85 case IPv6: {
86 channelBuffer.writeBytes(IPv6_HOSTNAME_ZEROED);
87 channelBuffer.writeShort(0);
88 break;
89 }
90 }
91 }
92 }