Source for java.net.Inet6Address

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