Frames | No Frames |
1: /* Inet4Address.java -- 2: Copyright (C) 2002, 2003, 2004, 2005, 2006 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 gnu.java.lang.CPStringBuilder; 42: 43: import java.io.ObjectStreamException; 44: 45: /* 46: * Written using on-line Java Platform 1.4 API Specification and 47: * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt), 48: * RFC 1918 (http://www.ietf.org/rfc/rfc1918.txt), 49: * RFC 2365 (http://www.ietf.org/rfc/rfc2365.txt) 50: * 51: * @author Michael Koch 52: * @status Believed complete and correct. 53: */ 54: public final class Inet4Address extends InetAddress 55: { 56: /** 57: * For compatability with Sun's JDK 1.4.2 rev. 5 58: */ 59: static final long serialVersionUID = 3286316764910316507L; 60: 61: /** 62: * The address family of these addresses (used for serialization). 63: */ 64: private static final int AF_INET = 2; 65: 66: /** 67: * Inet4Address objects are serialized as InetAddress objects. 68: */ 69: private Object writeReplace() throws ObjectStreamException 70: { 71: return new InetAddress(addr, hostName, AF_INET); 72: } 73: 74: /** 75: * Initializes this object's addr instance variable from the passed in 76: * byte array. Note that this constructor is protected and is called 77: * only by static methods in this class. 78: * 79: * @param addr The IP number of this address as an array of bytes 80: * @param host The hostname of this IP address. 81: */ 82: Inet4Address(byte[] addr, String host) 83: { 84: super(addr, host, AF_INET); 85: } 86: 87: /** 88: * Checks if the address is a multicast address 89: * 90: * @since 1.1 91: */ 92: public boolean isMulticastAddress() 93: { 94: return (addr[0] & 0xf0) == 0xe0; 95: } 96: 97: /** 98: * Checks if this address is a loopback address 99: */ 100: public boolean isLoopbackAddress() 101: { 102: return (addr[0] & 0xff) == 0x7f; 103: } 104: 105: /** 106: * Checks if this address is a wildcard address 107: * 108: * @since 1.4 109: */ 110: public boolean isAnyLocalAddress() 111: { 112: return equals(InetAddress.ANY_IF); 113: } 114: 115: /** 116: * Checks if this address is a link local address 117: * 118: * @since 1.4 119: */ 120: public boolean isLinkLocalAddress() 121: { 122: return false; 123: } 124: 125: /** 126: * Checks if this address is a site local address 127: * 128: * @since 1.4 129: */ 130: public boolean isSiteLocalAddress() 131: { 132: // 10.0.0.0/8 133: if ((addr[0] & 0xff) == 0x0a) 134: return true; 135: 136: // 172.16.0.0/12 137: if ((addr[0] & 0xff) == 0xac && (addr[1] & 0xf0) == 0x10) 138: return true; 139: 140: // 192.168.0.0/16 141: if ((addr[0] & 0xff) == 0xc0 && (addr[1] & 0xff) == 0xa8) 142: return true; 143: 144: return false; 145: } 146: 147: /** 148: * Checks if this multicast address has global scope 149: * 150: * @since 1.4 151: */ 152: public boolean isMCGlobal() 153: { 154: return false; 155: } 156: 157: /** 158: * Checks if this multicast address has node scope 159: * 160: * @since 1.4 161: */ 162: public boolean isMCNodeLocal() 163: { 164: return false; 165: } 166: 167: /** 168: * Checks if this multicast address has link scope 169: * 170: * @since 1.4 171: */ 172: public boolean isMCLinkLocal() 173: { 174: if (! isMulticastAddress()) 175: return false; 176: 177: return ((addr[0] & 0xff) == 0xe0 178: && (addr[1] & 0xff) == 0x00 179: && (addr[2] & 0xff) == 0x00); 180: } 181: 182: /** 183: * Checks if this multicast address has site scope 184: * 185: * @since 1.4 186: */ 187: public boolean isMCSiteLocal() 188: { 189: return false; 190: } 191: 192: /** 193: * Checks if this multicast address has organization scope 194: * 195: * @since 1.4 196: */ 197: public boolean isMCOrgLocal() 198: { 199: return false; 200: } 201: 202: /** 203: * Returns the address of the current instance 204: */ 205: public byte[] getAddress() 206: { 207: return (byte[]) addr.clone(); 208: } 209: 210: /** 211: * Returns the address as string 212: * 213: * @since 1.0.2 214: */ 215: public String getHostAddress() 216: { 217: CPStringBuilder sb = new CPStringBuilder(40); 218: 219: int len = addr.length; 220: int i = 0; 221: 222: for ( ; ; ) 223: { 224: sb.append(addr[i] & 0xff); 225: i++; 226: 227: if (i == len) 228: break; 229: 230: sb.append('.'); 231: } 232: 233: return sb.toString(); 234: } 235: 236: /** 237: * Computes the hashcode of the instance 238: */ 239: public int hashCode() 240: { 241: int hash = 0; 242: int len = addr.length; 243: int i = len > 4 ? len - 4 : 0; 244: 245: for (; i < len; i++) 246: hash = (hash << 8) | (addr[i] & 0xFF); 247: 248: return hash; 249: } 250: 251: /** 252: * Compare the current Inet4Address instance with obj 253: * 254: * @param obj Object to compare with 255: */ 256: public boolean equals(Object obj) 257: { 258: if (! (obj instanceof InetAddress)) 259: return false; 260: 261: byte[] addr1 = addr; 262: byte[] addr2 = ((InetAddress) obj).addr; 263: 264: if (addr1.length != addr2.length) 265: return false; 266: 267: for (int i = addr1.length; --i >= 0;) 268: if (addr1[i] != addr2[i]) 269: return false; 270: 271: return true; 272: } 273: }