| 1 | /* Inet6Address.java
|
|---|
| 2 | Copyright (C) 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 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)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 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.
|
|---|
| 15 |
|
|---|
| 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
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 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
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 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. */
|
|---|
| 37 |
|
|---|
| 38 | package java.net;
|
|---|
| 39 |
|
|---|
| 40 | import java.io.IOException;
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * @author Michael Koch
|
|---|
| 44 | * @date August 3, 2002.
|
|---|
| 45 | */
|
|---|
| 46 |
|
|---|
| 47 | /*
|
|---|
| 48 | * Written using on-line Java Platform 1.4 API Specification and
|
|---|
| 49 | * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt)
|
|---|
| 50 | * Status: Believed complete and correct.
|
|---|
| 51 | */
|
|---|
| 52 |
|
|---|
| 53 | public final class Inet6Address extends InetAddress
|
|---|
| 54 | {
|
|---|
| 55 | static final long serialVersionUID = 6880410070516793377L;
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Needed for serialization
|
|---|
| 59 | */
|
|---|
| 60 | byte[] ipaddress;
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Create an Inet6Address object
|
|---|
| 64 | *
|
|---|
| 65 | * @param addr The IP address
|
|---|
| 66 | * @param host The hostname
|
|---|
| 67 | */
|
|---|
| 68 | protected Inet6Address (byte[] addr, String host)
|
|---|
| 69 | {
|
|---|
| 70 | super (addr, host);
|
|---|
| 71 | this.ipaddress = addr;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Utility routine to check if the InetAddress is an IP multicast address
|
|---|
| 76 | *
|
|---|
| 77 | * @since 1.1
|
|---|
| 78 | */
|
|---|
| 79 | public boolean isMulticastAddress ()
|
|---|
| 80 | {
|
|---|
| 81 | return ipaddress [0] == 0xFF;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * Utility routine to check if the InetAddress in a wildcard address
|
|---|
| 86 | *
|
|---|
| 87 | * @since 1.4
|
|---|
| 88 | */
|
|---|
| 89 | public boolean isAnyLocalAddress ()
|
|---|
| 90 | {
|
|---|
| 91 | byte[] anylocal = { 0, 0, 0, 0, 0, 0, 0, 0,
|
|---|
| 92 | 0, 0, 0, 0, 0, 0, 0, 0 };
|
|---|
| 93 |
|
|---|
| 94 | return ipaddress == anylocal;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * Utility routine to check if the InetAddress is a loopback address
|
|---|
| 99 | *
|
|---|
| 100 | * @since 1.4
|
|---|
| 101 | */
|
|---|
| 102 | public boolean isLoopbackAddress ()
|
|---|
| 103 | {
|
|---|
| 104 | byte[] loopback = { 0, 0, 0, 0, 0, 0, 0, 0,
|
|---|
| 105 | 0, 0, 0, 0, 0, 0, 0, 1 };
|
|---|
| 106 |
|
|---|
| 107 | return ipaddress == loopback;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /**
|
|---|
| 111 | * Utility routine to check if the InetAddress is an link local address
|
|---|
| 112 | *
|
|---|
| 113 | * @since 1.4
|
|---|
| 114 | */
|
|---|
| 115 | public boolean isLinkLocalAddress ()
|
|---|
| 116 | {
|
|---|
| 117 | return ipaddress [0] == 0xFA;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /**
|
|---|
| 121 | * Utility routine to check if the InetAddress is a site local address
|
|---|
| 122 | *
|
|---|
| 123 | * @since 1.4
|
|---|
| 124 | */
|
|---|
| 125 | public boolean isSiteLocalAddress ()
|
|---|
| 126 | {
|
|---|
| 127 | return ipaddress [0] == 0xFB;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /**
|
|---|
| 131 | * Utility routine to check if the multicast address has global scope
|
|---|
| 132 | *
|
|---|
| 133 | * @since 1.4
|
|---|
| 134 | */
|
|---|
| 135 | public boolean isMCGlobal ()
|
|---|
| 136 | {
|
|---|
| 137 | if (!isMulticastAddress ())
|
|---|
| 138 | return false;
|
|---|
| 139 |
|
|---|
| 140 | return (ipaddress [1] & 0x0F) == 0xE;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /**
|
|---|
| 144 | * Utility routine to check if the multicast address has node scope
|
|---|
| 145 | *
|
|---|
| 146 | * @since 1.4
|
|---|
| 147 | */
|
|---|
| 148 | public boolean isMCNodeLocal ()
|
|---|
| 149 | {
|
|---|
| 150 | if (!isMulticastAddress ())
|
|---|
| 151 | return false;
|
|---|
| 152 |
|
|---|
| 153 | return (ipaddress [1] & 0x0F) == 0x1;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /**
|
|---|
| 157 | * Utility routine to check if the multicast address has link scope
|
|---|
| 158 | *
|
|---|
| 159 | * @since 1.4
|
|---|
| 160 | */
|
|---|
| 161 | public boolean isMCLinkLocal ()
|
|---|
| 162 | {
|
|---|
| 163 | if (!isMulticastAddress ())
|
|---|
| 164 | return false;
|
|---|
| 165 |
|
|---|
| 166 | return (ipaddress [1] & 0x0F) == 0x2;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /**
|
|---|
| 170 | * Utility routine to check if the multicast address has site scope
|
|---|
| 171 | *
|
|---|
| 172 | * @since 1.4
|
|---|
| 173 | */
|
|---|
| 174 | public boolean isMCSiteLocal ()
|
|---|
| 175 | {
|
|---|
| 176 | if (!isMulticastAddress ())
|
|---|
| 177 | return false;
|
|---|
| 178 |
|
|---|
| 179 | return (ipaddress [1] & 0x0F) == 0x5;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | * Utility routine to check if the multicast address has organization scope
|
|---|
| 184 | *
|
|---|
| 185 | * @since 1.4
|
|---|
| 186 | */
|
|---|
| 187 | public boolean isMCOrgLocal ()
|
|---|
| 188 | {
|
|---|
| 189 | if (!isMulticastAddress ())
|
|---|
| 190 | return false;
|
|---|
| 191 |
|
|---|
| 192 | return (ipaddress [1] & 0x0F) == 0x8;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Returns the raw IP address of this InetAddress object. The result is in
|
|---|
| 197 | * network byte order: the highest order byte of the address is i
|
|---|
| 198 | * n getAddress()[0]
|
|---|
| 199 | */
|
|---|
| 200 | public byte[] getAddress ()
|
|---|
| 201 | {
|
|---|
| 202 | return ipaddress;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /**
|
|---|
| 206 | * Returns the IP address string in textual presentation
|
|---|
| 207 | */
|
|---|
| 208 | public String getHostAddress ()
|
|---|
| 209 | {
|
|---|
| 210 | StringBuffer sbuf = new StringBuffer (40);
|
|---|
| 211 |
|
|---|
| 212 | for (int i = 0; i < 16; i += 2)
|
|---|
| 213 | {
|
|---|
| 214 | int x = ((ipaddress [i] & 0xFF) << 8) | (ipaddress [i + 1] & 0xFF);
|
|---|
| 215 | boolean empty = sbuf.length () == 0;
|
|---|
| 216 |
|
|---|
| 217 | if (empty)
|
|---|
| 218 | {
|
|---|
| 219 | if (i > 0)
|
|---|
| 220 | sbuf.append ("::");
|
|---|
| 221 | }
|
|---|
| 222 | else
|
|---|
| 223 | sbuf.append (':');
|
|---|
| 224 |
|
|---|
| 225 | if (x != 0 || i >= 14)
|
|---|
| 226 | sbuf.append (Integer.toHexString (x));
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | return sbuf.toString ();
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | /**
|
|---|
| 233 | * Returns a hashcode for this IP address
|
|---|
| 234 | */
|
|---|
| 235 | public int hashCode ()
|
|---|
| 236 | {
|
|---|
| 237 | return super.hashCode ();
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | /**
|
|---|
| 241 | * Compares this object against the specified object
|
|---|
| 242 | */
|
|---|
| 243 | public boolean equals (Object obj)
|
|---|
| 244 | {
|
|---|
| 245 | if (obj == null || ! (obj instanceof Inet6Address))
|
|---|
| 246 | return false;
|
|---|
| 247 |
|
|---|
| 248 | Inet6Address tmp = (Inet6Address) obj;
|
|---|
| 249 |
|
|---|
| 250 | return super.equals (tmp)
|
|---|
| 251 | && this.ipaddress == tmp.ipaddress;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /**
|
|---|
| 255 | * Utility routine to check if the InetAddress is an
|
|---|
| 256 | * IPv4 compatible IPv6 address
|
|---|
| 257 | *
|
|---|
| 258 | * @since 1.4
|
|---|
| 259 | */
|
|---|
| 260 | public boolean isIPv4CompatibleAddress ()
|
|---|
| 261 | {
|
|---|
| 262 | if (ipaddress [0] != 0x00 || ipaddress [1] != 0x00 ||
|
|---|
| 263 | ipaddress [2] != 0x00 || ipaddress [3] != 0x00 ||
|
|---|
| 264 | ipaddress [4] != 0x00 || ipaddress [5] != 0x00 ||
|
|---|
| 265 | ipaddress [6] != 0x00 || ipaddress [7] != 0x00 ||
|
|---|
| 266 | ipaddress [8] != 0x00 || ipaddress [9] != 0x00 ||
|
|---|
| 267 | ipaddress [10] != 0x00 || ipaddress [11] != 0x00)
|
|---|
| 268 | return false;
|
|---|
| 269 |
|
|---|
| 270 | return true;
|
|---|
| 271 | }
|
|---|
| 272 | }
|
|---|