| 1 | // INetAddress.java -- An Internet Protocol (IP) address.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation
|
|---|
| 4 |
|
|---|
| 5 | This file is part of libgcj.
|
|---|
| 6 |
|
|---|
| 7 | This software is copyrighted work licensed under the terms of the
|
|---|
| 8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 9 | details. */
|
|---|
| 10 |
|
|---|
| 11 | package java.net;
|
|---|
| 12 |
|
|---|
| 13 | import java.io.ObjectInputStream;
|
|---|
| 14 | import java.io.ObjectOutputStream;
|
|---|
| 15 | import java.io.IOException;
|
|---|
| 16 | import java.io.Serializable;
|
|---|
| 17 | import java.io.ObjectStreamException;
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * @author Per Bothner
|
|---|
| 21 | * @date January 6, 1999.
|
|---|
| 22 | */
|
|---|
| 23 |
|
|---|
| 24 | /*
|
|---|
| 25 | * Written using on-line Java Platform 1.2 API Specification, as well
|
|---|
| 26 | * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
|
|---|
| 27 | * (The latter turns out to have some errors ...)
|
|---|
| 28 | * Status: Believed complete and correct.
|
|---|
| 29 | *
|
|---|
| 30 | * @specnote This class is not final since JK 1.4
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | public class InetAddress implements Serializable
|
|---|
| 34 | {
|
|---|
| 35 | // The Serialized Form specifies that an int 'address' is saved/restored.
|
|---|
| 36 | // This class uses a byte array internally so we'll just do the conversion
|
|---|
| 37 | // at serialization time and leave the rest of the algorithm as is.
|
|---|
| 38 | private int address;
|
|---|
| 39 | transient byte[] addr;
|
|---|
| 40 | String hostName;
|
|---|
| 41 | // The field 'family' seems to be the AF_ value.
|
|---|
| 42 | // FIXME: Much of the code in the other java.net classes does not make
|
|---|
| 43 | // use of this family field. A better implementation would be to make
|
|---|
| 44 | // use of getaddrinfo() and have other methods just check the family
|
|---|
| 45 | // field rather than examining the length of the address each time.
|
|---|
| 46 | int family;
|
|---|
| 47 | private static final long serialVersionUID = 3286316764910316507L;
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Needed for serialization
|
|---|
| 51 | */
|
|---|
| 52 | private void readResolve () throws ObjectStreamException
|
|---|
| 53 | {
|
|---|
| 54 | // FIXME: implement this
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | private void readObject(ObjectInputStream ois)
|
|---|
| 58 | throws IOException, ClassNotFoundException
|
|---|
| 59 | {
|
|---|
| 60 | ois.defaultReadObject();
|
|---|
| 61 | addr = new byte[4];
|
|---|
| 62 | addr[3] = (byte) address;
|
|---|
| 63 | for (int i = 2; i >= 0; --i)
|
|---|
| 64 | addr[i] = (byte) (address >>= 8);
|
|---|
| 65 | // Ignore family from serialized data. Since the saved address is 32 bits
|
|---|
| 66 | // the deserialized object will have an IPv4 address i.e. AF_INET family.
|
|---|
| 67 | // FIXME: An alternative is to call the aton method on the deserialized
|
|---|
| 68 | // hostname to get a new address. The Serialized Form doc is silent
|
|---|
| 69 | // on how these fields are used.
|
|---|
| 70 | family = getFamily (addr);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | private void writeObject(ObjectOutputStream oos) throws IOException
|
|---|
| 74 | {
|
|---|
| 75 | // Build a 32 bit address from the last 4 bytes of a 4 byte IPv4 address
|
|---|
| 76 | // or a 16 byte IPv6 address.
|
|---|
| 77 | int len = addr.length;
|
|---|
| 78 | int i = len - 4;
|
|---|
| 79 | for (; i < len; i++)
|
|---|
| 80 | address = address << 8 | (((int) addr[i]) & 0xFF);
|
|---|
| 81 | oos.defaultWriteObject();
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | private static native int getFamily (byte[] address);
|
|---|
| 85 |
|
|---|
| 86 | InetAddress (byte[] address, String hostname)
|
|---|
| 87 | {
|
|---|
| 88 | addr = address;
|
|---|
| 89 | hostName = hostname;
|
|---|
| 90 | if (address != null)
|
|---|
| 91 | family = getFamily (address);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * Utility routine to check if the InetAddress is an IP multicast address
|
|---|
| 96 | *
|
|---|
| 97 | * @since 1.1
|
|---|
| 98 | */
|
|---|
| 99 | public boolean isMulticastAddress ()
|
|---|
| 100 | {
|
|---|
| 101 | int len = addr.length;
|
|---|
| 102 | if (len == 4)
|
|---|
| 103 | return (addr[0] & 0xF0) == 0xE0;
|
|---|
| 104 | if (len == 16)
|
|---|
| 105 | return addr[0] == (byte) 0xFF;
|
|---|
| 106 | return false;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * Utility routine to check if the InetAddress in a wildcard address
|
|---|
| 111 | *
|
|---|
| 112 | * @since 1.4
|
|---|
| 113 | */
|
|---|
| 114 | public boolean isAnyLocalAddress ()
|
|---|
| 115 | {
|
|---|
| 116 | // This is the IPv4 implementation.
|
|---|
| 117 | // Any class derived from InetAddress should override this.
|
|---|
| 118 | return addr == zeros;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /**
|
|---|
| 122 | * Utility routine to check if the InetAddress is a loopback address
|
|---|
| 123 | *
|
|---|
| 124 | * @since 1.4
|
|---|
| 125 | */
|
|---|
| 126 | public boolean isLoopbackAddress ()
|
|---|
| 127 | {
|
|---|
| 128 | // This is the IPv4 implementation.
|
|---|
| 129 | // Any class derived from InetAddress should override this.
|
|---|
| 130 |
|
|---|
| 131 | return addr[0] == 0x7F;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Utility routine to check if InetAddress is a link local address
|
|---|
| 136 | *
|
|---|
| 137 | * @since 1.4
|
|---|
| 138 | */
|
|---|
| 139 | public boolean isLinkLocalAddress ()
|
|---|
| 140 | {
|
|---|
| 141 | // This is the IPv4 implementation.
|
|---|
| 142 | // Any class derived from InetAddress should override this.
|
|---|
| 143 |
|
|---|
| 144 | // XXX: This seems to not exist with IPv4 addresses
|
|---|
| 145 | return false;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * Utility routine to check if InetAddress is a site local address
|
|---|
| 150 | *
|
|---|
| 151 | * @since 1.4
|
|---|
| 152 | */
|
|---|
| 153 | public boolean isSiteLocalAddress ()
|
|---|
| 154 | {
|
|---|
| 155 | // This is the IPv4 implementation.
|
|---|
| 156 | // Any class derived from InetAddress should override this.
|
|---|
| 157 |
|
|---|
| 158 | // 10.0.0.0/8
|
|---|
| 159 | if (addr[0] == 0x0A)
|
|---|
| 160 | return true;
|
|---|
| 161 |
|
|---|
| 162 | // XXX: Suns JDK 1.4.1 (on Linux) seems to have a bug here:
|
|---|
| 163 | // it says 172.16.0.0 - 172.255.255.255 are site local addresses
|
|---|
| 164 |
|
|---|
| 165 | // 172.16.0.0/12
|
|---|
| 166 | if (addr[0] == 0xAC && (addr[1] & 0xF0) == 0x01)
|
|---|
| 167 | return true;
|
|---|
| 168 |
|
|---|
| 169 | // 192.168.0.0/16
|
|---|
| 170 | if (addr[0] == 0xC0 && addr[1] == 0xA8)
|
|---|
| 171 | return true;
|
|---|
| 172 |
|
|---|
| 173 | // XXX: Do we need to check more addresses here ?
|
|---|
| 174 | return false;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /**
|
|---|
| 178 | * Utility routine to check if InetAddress is a global multicast address
|
|---|
| 179 | *
|
|---|
| 180 | * @since 1.4
|
|---|
| 181 | */
|
|---|
| 182 | public boolean isMCGlobal ()
|
|---|
| 183 | {
|
|---|
| 184 | // This is the IPv4 implementation.
|
|---|
| 185 | // Any class derived from InetAddress should override this.
|
|---|
| 186 |
|
|---|
| 187 | // XXX: This seems to not exist with IPv4 addresses
|
|---|
| 188 | return false;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /**
|
|---|
| 192 | * Utility reoutine to check if InetAddress is a node local multicast address
|
|---|
| 193 | *
|
|---|
| 194 | * @since 1.4
|
|---|
| 195 | */
|
|---|
| 196 | public boolean isMCNodeLocal ()
|
|---|
| 197 | {
|
|---|
| 198 | // This is the IPv4 implementation.
|
|---|
| 199 | // Any class derived from InetAddress should override this.
|
|---|
| 200 |
|
|---|
| 201 | // XXX: This seems to not exist with IPv4 addresses
|
|---|
|
|---|