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