1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package openr66.protocol.localhandler.packet;
22
23 import goldengate.common.digest.FilesystemBasedDigest;
24 import openr66.protocol.configuration.Configuration;
25 import openr66.protocol.exception.OpenR66ProtocolNoSslException;
26 import openr66.protocol.exception.OpenR66ProtocolPacketException;
27
28 import org.jboss.netty.buffer.ChannelBuffer;
29 import org.jboss.netty.buffer.ChannelBuffers;
30
31
32
33
34
35
36
37
38 public class AuthentPacket extends AbstractLocalPacket {
39 private static final byte ASKVALIDATE = 0;
40
41 private static final byte ANSWERVALIDATE = 1;
42
43 private final Integer localId;
44
45 private byte way;
46
47 private String hostId;
48
49 private byte[] key;
50
51
52
53
54
55
56
57
58
59 public static AuthentPacket createFromBuffer(int headerLength,
60 int middleLength, int endLength, ChannelBuffer buf)
61 throws OpenR66ProtocolPacketException {
62 if (headerLength - 1 <= 0) {
63 throw new OpenR66ProtocolPacketException("Not enough data");
64 }
65 if (middleLength <= 0) {
66 throw new OpenR66ProtocolPacketException("Not enough data");
67 }
68 if (endLength < 5) {
69 throw new OpenR66ProtocolPacketException("Not enough data");
70 }
71 final byte[] bheader = new byte[headerLength - 1];
72 final byte[] bmiddle = new byte[middleLength];
73 if (headerLength - 1 > 0) {
74 buf.readBytes(bheader);
75 }
76 if (middleLength > 0) {
77 buf.readBytes(bmiddle);
78 }
79 Integer newId = buf.readInt();
80 byte valid = buf.readByte();
81 final String sheader = new String(bheader);
82 return new AuthentPacket(sheader, bmiddle, newId, valid);
83 }
84
85
86
87
88
89
90
91 private AuthentPacket(String hostId, byte[] key, Integer newId, byte valid) {
92 this.hostId = hostId;
93 this.key = key;
94 localId = newId;
95 way = valid;
96 }
97
98
99
100
101
102
103 public AuthentPacket(String hostId, byte[] key, Integer newId) {
104 this.hostId = hostId;
105 this.key = key;
106 localId = newId;
107 way = ASKVALIDATE;
108 }
109
110
111
112
113
114
115 @Override
116 public void createEnd() throws OpenR66ProtocolPacketException {
117 end = ChannelBuffers.buffer(5);
118 end.writeInt(localId);
119 end.writeByte(way);
120 }
121
122
123
124
125
126
127
128 @Override
129 public void createHeader() throws OpenR66ProtocolPacketException {
130 if (hostId == null) {
131 throw new OpenR66ProtocolPacketException("Not enough data");
132 }
133 header = ChannelBuffers.wrappedBuffer(hostId.getBytes());
134 }
135
136
137
138
139
140
141
142 @Override
143 public void createMiddle() throws OpenR66ProtocolPacketException {
144 if (key == null) {
145 throw new OpenR66ProtocolPacketException("Not enough data");
146 }
147 middle = ChannelBuffers.wrappedBuffer(key);
148 }
149
150 @Override
151 public byte getType() {
152 return LocalPacketFactory.AUTHENTPACKET;
153 }
154
155
156
157
158
159
160 @Override
161 public String toString() {
162 return "AuthentPacket: " + hostId + " " + localId + " " + way;
163 }
164
165
166
167
168 public String getHostId() {
169 return hostId;
170 }
171
172
173
174
175 public byte[] getKey() {
176 return key;
177 }
178
179
180
181
182 public Integer getLocalId() {
183 return localId;
184 }
185
186
187
188
189 public boolean isToValidate() {
190 return way == ASKVALIDATE;
191 }
192
193
194
195
196 public void validate(boolean isSSL) {
197 way = ANSWERVALIDATE;
198 try {
199 hostId = Configuration.configuration.getHostId(isSSL);
200 } catch (OpenR66ProtocolNoSslException e) {
201 hostId = Configuration.configuration.HOST_ID;
202 }
203 key = FilesystemBasedDigest.passwdCrypt(Configuration.configuration.HOST_AUTH.getHostkey());
204 header = null;
205 middle = null;
206 end = null;
207 }
208 }