1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.kerberos.shared.messages.value;
21
22
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
25 import java.nio.BufferOverflowException;
26 import java.nio.ByteBuffer;
27
28 import org.apache.directory.server.kerberos.shared.messages.value.types.HostAddrType;
29 import org.apache.directory.shared.asn1.AbstractAsn1Object;
30 import org.apache.directory.shared.asn1.ber.tlv.TLV;
31 import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
32 import org.apache.directory.shared.asn1.ber.tlv.Value;
33 import org.apache.directory.shared.asn1.codec.EncoderException;
34 import org.apache.directory.shared.ldap.util.StringTools;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class HostAddress extends AbstractAsn1Object
53 {
54
55 private static final Logger LOG = LoggerFactory.getLogger( HostAddress.class );
56
57
58 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 private HostAddrType addrType;
74
75
76 private byte[] address;
77
78
79 private transient int addrTypeLength;
80 private transient int addressLength;
81 private transient int hostAddressLength;
82 private transient int hostAddressSeqLength;
83
84
85
86
87
88
89
90
91 public HostAddress( HostAddrType addrType, byte[] address )
92 {
93 this.addrType = addrType;
94 this.address = address;
95 }
96
97
98
99
100
101
102
103 public HostAddress( InetAddress internetAddress )
104 {
105 addrType = HostAddrType.ADDRTYPE_INET;
106 byte[] newAddress = internetAddress.getAddress();
107 address = new byte[newAddress.length];
108 System.arraycopy( newAddress, 0, address, 0, newAddress.length );
109 }
110
111
112
113
114
115
116
117
118 public boolean equals( Object that )
119 {
120 if ( this == that )
121 {
122 return true;
123 }
124
125 if ( !(that instanceof HostAddress ) )
126 {
127 return false;
128 }
129
130 HostAddress hostAddress = (HostAddress)that;
131
132 if ( addrType != hostAddress.addrType || ( address != null && hostAddress.address == null )
133 || ( address == null && hostAddress.address != null ) )
134 {
135 return false;
136 }
137
138 if ( address != null && hostAddress.address != null )
139 {
140 if ( address.length != hostAddress.address.length )
141 {
142 return false;
143 }
144
145 for ( int ii = 0; ii < address.length; ii++ )
146 {
147 if ( address[ii] != hostAddress.address[ii] )
148 {
149 return false;
150 }
151 }
152 }
153
154 return true;
155 }
156
157
158
159
160
161
162
163 public byte[] getAddress()
164 {
165 return address;
166 }
167
168
169
170
171
172
173
174 public void setAddress( byte[] addresse )
175 {
176 this.address = addresse;
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public int computeLength()
202 {
203
204 addrTypeLength = 1 + 1 + Value.getNbBytes( addrType.getOrdinal() );
205 hostAddressLength = 1 + TLV.getNbBytes( addrTypeLength ) + addrTypeLength;
206
207
208 if ( address == null )
209 {
210 addressLength = 1 + 1;
211 }
212 else
213 {
214 addressLength = 1 + TLV.getNbBytes( address.length ) + address.length;
215 }
216
217 hostAddressLength += 1 + TLV.getNbBytes( addressLength ) + addressLength;
218
219
220 hostAddressSeqLength = 1 + Value.getNbBytes( hostAddressLength ) + hostAddressLength;
221
222 return hostAddressSeqLength;
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
242 {
243 if ( buffer == null )
244 {
245 throw new EncoderException( "Cannot put a PDU in a null buffer !" );
246 }
247
248 try
249 {
250
251 buffer.put( UniversalTag.SEQUENCE_TAG );
252 buffer.put( TLV.getBytes( hostAddressLength ) );
253
254
255 buffer.put( ( byte ) 0xA0 );
256 buffer.put( TLV.getBytes( addrTypeLength ) );
257 Value.encode( buffer, addrType.getOrdinal() );
258
259
260 buffer.put( ( byte ) 0xA1 );
261 buffer.put( TLV.getBytes( addressLength ) );
262 Value.encode( buffer, address );
263 }
264 catch ( BufferOverflowException boe )
265 {
266 LOG.error(
267 "Cannot encode the HostAddress object, the PDU size is {} when only {} bytes has been allocated", 1
268 + TLV.getNbBytes( hostAddressLength ) + hostAddressLength, buffer.capacity() );
269 throw new EncoderException( "The PDU buffer size is too small !" );
270 }
271
272 if ( IS_DEBUG )
273 {
274 LOG.debug( "Checksum encoding : {}", StringTools.dumpBytes( buffer.array() ) );
275 LOG.debug( "Checksum initial value : {}", toString() );
276 }
277
278 return buffer;
279 }
280
281
282
283
284
285
286
287 public HostAddrType getAddrType()
288 {
289 return addrType;
290 }
291
292
293
294
295
296
297
298 public void setAddrType( HostAddrType addrType )
299 {
300 this.addrType = addrType;
301 }
302
303
304
305
306
307
308
309 public void setAddrType( int addrType )
310 {
311 this.addrType = HostAddrType.getTypeByOrdinal( addrType );
312 }
313
314
315
316
317
318 public String toString()
319 {
320 String result = "";
321
322 try
323 {
324 result = InetAddress.getByAddress( address ).getHostAddress();
325 }
326 catch ( UnknownHostException uhe )
327 {
328
329 }
330
331 return result;
332 }
333 }