]>
Commit | Line | Data |
---|---|---|
1 | // INetAddress.java -- An Internet Protocol (IP) address. | |
2 | ||
3 | /* Copyright (C) 1998, 1999 Cygnus Solutions | |
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 | /** | |
14 | * @author Per Bothner | |
15 | * @date January 6, 1999. | |
16 | */ | |
17 | ||
18 | /* | |
19 | * Written using on-line Java Platform 1.2 API Specification, as well | |
20 | * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). | |
21 | * (The latter turns out to have some errors ...) | |
22 | * Status: Believed complete and correct. | |
23 | */ | |
24 | ||
25 | public final class InetAddress | |
26 | { | |
27 | String hostname; | |
28 | byte[] address; | |
29 | ||
30 | InetAddress (byte[] address, String hostname) | |
31 | { | |
32 | this.address = address; | |
33 | this.hostname = hostname; | |
34 | } | |
35 | ||
36 | public boolean isMulticastAddress () | |
37 | { | |
38 | int len = address.length; | |
39 | if (len == 4) | |
40 | return (address[0] & 0xF0) == 0xE0; | |
41 | if (len == 16) | |
42 | return address[0] == (byte) 0xFF; | |
43 | return false; | |
44 | } | |
45 | ||
46 | public String getHostName () | |
47 | { | |
48 | if (hostname == null) | |
49 | lookup (null, this, false); | |
50 | return hostname; | |
51 | } | |
52 | ||
53 | public byte[] getAddress () | |
54 | { | |
55 | // An experiment shows that JDK1.2 returns a different byte array each | |
56 | // time. This makes sense, in terms of security. | |
57 | return (byte[]) address.clone(); | |
58 | } | |
59 | ||
60 | /* Helper function due to a CNI limitation. */ | |
61 | private static InetAddress[] allocArray (int count) | |
62 | { | |
63 | return new InetAddress[count]; | |
64 | } | |
65 | ||
66 | /* Helper function due to a CNI limitation. */ | |
67 | private static SecurityException checkConnect (String hostname) | |
68 | { | |
69 | SecurityManager s = System.getSecurityManager(); | |
70 | if (s == null) | |
71 | return null; | |
72 | try | |
73 | { | |
74 | s.checkConnect(hostname, -1); | |
75 | return null; | |
76 | } | |
77 | catch (SecurityException ex) | |
78 | { | |
79 | return ex; | |
80 | } | |
81 | } | |
82 | ||
83 | public String getHostAddress () | |
84 | { | |
85 | StringBuffer sbuf = new StringBuffer(40); | |
86 | int len = address.length; | |
87 | int i = 0; | |
88 | if (len == 16) | |
89 | { // An IPv6 address. | |
90 | for (; ; i += 2) | |
91 | { | |
92 | if (i >= 16) | |
93 | return sbuf.toString(); | |
94 | int x = ((address[i] & 0xFF) << 8) | (address[i+1] & 0xFF); | |
95 | boolean empty = sbuf.length() == 0; | |
96 | if (empty) | |
97 | { | |
98 | if (i == 10 && x == 0xFFFF) | |
99 | { // IPv4-mapped IPv6 address. | |
100 | sbuf.append(":FFFF:"); | |
101 | break; // Continue as IPv4 address; | |
102 | } | |
103 | else if (i == 12) | |
104 | { // IPv4-compatible IPv6 address. | |
105 | sbuf.append(':'); | |
106 | break; // Continue as IPv4 address. | |
107 | } | |
108 | else if (i > 0) | |
109 | sbuf.append("::"); | |
110 | } | |
111 | else | |
112 | sbuf.append(':'); | |
113 | if (x != 0 || i >= 14) | |
114 | sbuf.append(Integer.toHexString(x).toUpperCase()); | |
115 | } | |
116 | } | |
117 | for ( ; ; ) | |
118 | { | |
119 | sbuf.append(address[i] & 0xFF); | |
120 | i++; | |
121 | if (i == len) | |
122 | break; | |
123 | sbuf.append('.'); | |
124 | } | |
125 | return sbuf.toString(); | |
126 | } | |
127 | ||
128 | public int hashCode() | |
129 | { | |
130 | // There hashing algorithm is not specified, but a simple experiment | |
131 | // shows that it is equal to the address, as a 32-bit big-endian integer. | |
132 | int hash = 0; | |
133 | int len = address.length; | |
134 | int i = len > 4 ? len - 4 : 0; | |
135 | for ( ; i < len; i++) | |
136 | hash = (hash << 8) | (address[i] & 0xFF); | |
137 | return hash; | |
138 | } | |
139 | ||
140 | public boolean equals (Object obj) | |
141 | { | |
142 | if (obj == null || ! (obj instanceof InetAddress)) | |
143 | return false; | |
144 | // "The Java Class Libraries" 2nd edition says "If a machine has | |
145 | // multiple names instances of InetAddress for different name of | |
146 | // that same machine are not equal. This is because they have | |
147 | // different host names." This violates the description in the | |
148 | // JDK 1.2 API documentation. A little experiementation | |
149 | // shows that the latter is correct. | |
150 | byte[] addr1 = address; | |
151 | byte[] addr2 = ((InetAddress) obj).address; | |
152 | if (addr1.length != addr2.length) | |
153 | return false; | |
154 | for (int i = addr1.length; --i >= 0; ) | |
155 | if (addr1[i] != addr2[i]) | |
156 | return false; | |
157 | return true; | |
158 | } | |
159 | ||
160 | public String toString() | |
161 | { | |
162 | return getHostName()+'/'+getHostAddress(); | |
163 | } | |
164 | ||
165 | /** If host is a valid numeric IP address, return the numeric address. | |
166 | * Otherwise, return null. */ | |
167 | private static native byte[] aton (String host); | |
168 | ||
169 | private static native InetAddress[] lookup | |
170 | (String hostname, InetAddress addr, boolean all); | |
171 | ||
172 | public static InetAddress getByName (String host) | |
173 | throws UnknownHostException | |
174 | { | |
175 | if (host == null) | |
176 | return getLocalHost(); | |
177 | byte[] address = aton(host); | |
178 | if (address != null) | |
179 | return new InetAddress(address, null); | |
180 | InetAddress iaddr = new InetAddress(null, host); | |
181 | lookup(host, iaddr, false); | |
182 | return iaddr; | |
183 | } | |
184 | ||
185 | public static InetAddress[] getAllByName (String host) | |
186 | throws UnknownHostException | |
187 | { | |
188 | byte[] address = aton(host); | |
189 | if (address != null) | |
190 | { | |
191 | InetAddress[] result = new InetAddress[1]; | |
192 | result[0] = new InetAddress(address, null); | |
193 | return result; | |
194 | } | |
195 | return lookup(host, null, true); | |
196 | } | |
197 | ||
198 | private static final byte[] localhostAddress = { 127, 0, 0, 1 }; | |
199 | ||
200 | private static native String getLocalHostname (); | |
201 | ||
202 | private static InetAddress localhost = null; | |
203 | ||
204 | public static InetAddress getLocalHost() throws UnknownHostException | |
205 | { | |
206 | SecurityManager s = System.getSecurityManager(); | |
207 | // Experimentation shows that JDK1.2 does cache the result. | |
208 | // However, if there is a security manager, and the cached result | |
209 | // is other than "localhost", we need to check again. | |
210 | if (localhost == null | |
211 | || (s != null && localhost.address != localhostAddress)) | |
212 | getLocalHost(s); | |
213 | return localhost; | |
214 | } | |
215 | ||
216 | private static synchronized void getLocalHost(SecurityManager s) | |
217 | throws UnknownHostException | |
218 | { | |
219 | // Check the localhost cache again, now that we've synchronized. | |
220 | if (s == null && localhost != null) | |
221 | return; | |
222 | String hostname = getLocalHostname(); | |
223 | if (s != null) | |
224 | { | |
225 | // "The Java Class Libraries" suggests that if the security | |
226 | // manager disallows getting the local host name, then | |
227 | // we use the loopback host. | |
228 | // However, the JDK 1.2 API claims to throw SecurityException, | |
229 | // which seems to suggest SecurityException is *not* caught. | |
230 | // In this case, experimentation shows that former is correct. | |
231 | try | |
232 | { | |
233 | // This is wrong, if the name returned from getLocalHostname() | |
234 | // is not a fully qualified name. FIXME. | |
235 | s.checkConnect(hostname, -1); | |
236 | } | |
237 | catch (SecurityException ex) | |
238 | { | |
239 | hostname = null; | |
240 | } | |
241 | } | |
242 | if (hostname != null) | |
243 | { | |
244 | try | |
245 | { | |
246 | localhost = new InetAddress(null, null); | |
247 | lookup(hostname, localhost, false); | |
248 | } | |
249 | catch (Exception ex) | |
250 | { | |
251 | } | |
252 | } | |
253 | if (localhost == null) | |
254 | localhost = new InetAddress (localhostAddress, "localhost"); | |
255 | } | |
256 | } |