1 /* Inet6Address.java --
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.util
.Arrays
;
44 * Written using on-line Java Platform 1.4 API Specification and
45 * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt)
47 * @author Michael Koch
48 * @status Believed complete and correct.
50 public final class Inet6Address
extends InetAddress
52 static final long serialVersionUID
= 6880410070516793377L;
55 * Needed for serialization
60 * Create an Inet6Address object
62 * @param addr The IP address
63 * @param host The hostname
65 Inet6Address(byte[] addr
, String host
)
68 // Super constructor clones the addr. Get a reference to the clone.
69 this.ipaddress
= this.addr
;
73 * Utility routine to check if the InetAddress is an IP multicast address
77 public boolean isMulticastAddress()
79 return ipaddress
[0] == 0xFF;
83 * Utility routine to check if the InetAddress in a wildcard address
87 public boolean isAnyLocalAddress()
89 byte[] anylocal
= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
91 return Arrays
.equals(ipaddress
, anylocal
);
95 * Utility routine to check if the InetAddress is a loopback address
99 public boolean isLoopbackAddress()
101 byte[] loopback
= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
103 return Arrays
.equals(ipaddress
, loopback
);
107 * Utility routine to check if the InetAddress is an link local address
111 public boolean isLinkLocalAddress()
113 return ipaddress
[0] == 0xFA;
117 * Utility routine to check if the InetAddress is a site local address
121 public boolean isSiteLocalAddress()
123 return ipaddress
[0] == 0xFB;
127 * Utility routine to check if the multicast address has global scope
131 public boolean isMCGlobal()
133 if (! isMulticastAddress())
136 return (ipaddress
[1] & 0x0F) == 0xE;
140 * Utility routine to check if the multicast address has node scope
144 public boolean isMCNodeLocal()
146 if (! isMulticastAddress())
149 return (ipaddress
[1] & 0x0F) == 0x1;
153 * Utility routine to check if the multicast address has link scope
157 public boolean isMCLinkLocal()
159 if (! isMulticastAddress())
162 return (ipaddress
[1] & 0x0F) == 0x2;
166 * Utility routine to check if the multicast address has site scope
170 public boolean isMCSiteLocal()
172 if (! isMulticastAddress())
175 return (ipaddress
[1] & 0x0F) == 0x5;
179 * Utility routine to check if the multicast address has organization scope
183 public boolean isMCOrgLocal()
185 if (! isMulticastAddress())
188 return (ipaddress
[1] & 0x0F) == 0x8;
192 * Returns the raw IP address of this InetAddress object. The result is in
193 * network byte order: the highest order byte of the address is i
196 public byte[] getAddress()
198 return (byte[]) ipaddress
.clone();
202 * Returns the IP address string in textual presentation
204 public String
getHostAddress()
206 StringBuffer sbuf
= new StringBuffer(40);
208 for (int i
= 0; i
< 16; i
+= 2)
210 int x
= ((ipaddress
[i
] & 0xFF) << 8) | (ipaddress
[i
+ 1] & 0xFF);
215 sbuf
.append(Integer
.toHexString(x
));
218 return sbuf
.toString();
222 * Returns a hashcode for this IP address
224 public int hashCode()
226 return super.hashCode();
230 * Compares this object against the specified object
232 public boolean equals(Object obj
)
234 if (! (obj
instanceof Inet6Address
))
237 // this.ipaddress is never set in this class except to
238 // the value of the super class' addr. The super classes
239 // equals(Object) will do the compare.
240 return super.equals(obj
);
244 * Utility routine to check if the InetAddress is an
245 * IPv4 compatible IPv6 address
249 public boolean isIPv4CompatibleAddress()
251 if (ipaddress
[0] != 0x00 || ipaddress
[1] != 0x00 || ipaddress
[2] != 0x00
252 || ipaddress
[3] != 0x00 || ipaddress
[4] != 0x00
253 || ipaddress
[5] != 0x00 || ipaddress
[6] != 0x00
254 || ipaddress
[7] != 0x00 || ipaddress
[8] != 0x00
255 || ipaddress
[9] != 0x00 || ipaddress
[10] != 0x00
256 || ipaddress
[11] != 0x00)