]> gcc.gnu.org Git - gcc.git/blob - libjava/java/net/Inet4Address.java
[multiple changes]
[gcc.git] / libjava / java / net / Inet4Address.java
1 /* Inet4Address.java
2 Copyright (C) 2002, 2003 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.ObjectStreamException;
41 import java.util.Arrays;
42
43
44 /**
45 * @author Michael Koch
46 * @date August 3, 2002.
47 */
48
49 /*
50 * Written using on-line Java Platform 1.4 API Specification and
51 * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt),
52 * RFC 1918 (http://www.ietf.org/rfc/rfc1918.txt),
53 * RFC 2365 (http://www.ietf.org/rfc/rfc2365.txt)
54 * Status: Believed complete and correct.
55 */
56 public final class Inet4Address extends InetAddress
57 {
58 /**
59 * For compatability with Sun's JDK 1.4.2 rev. 5
60 */
61 static final long serialVersionUID = 3286316764910316507L;
62
63 /**
64 * needed for serialization
65 */
66 private Object writeReplace() throws ObjectStreamException
67 {
68 return new InetAddress(addr, hostName);
69 }
70
71 /**
72 * Creates a Inet4Address
73 *
74 * @param addr The IP address
75 * @param host The Hostname
76 */
77 Inet4Address(byte[] addr, String host)
78 {
79 super(addr, host);
80 }
81
82 /**
83 * Checks if the address is a multicast address
84 *
85 * @since 1.1
86 */
87 public boolean isMulticastAddress()
88 {
89 return (addr[0] & 0xF0) == 0xE0;
90 }
91
92 /**
93 * Checks if this address is a loopback address
94 */
95 public boolean isLoopbackAddress()
96 {
97 return addr[0] == 0x7F;
98 }
99
100 /**
101 * Checks if this address is a wildcard address
102 *
103 * @since 1.4
104 */
105 public boolean isAnyLocalAddress()
106 {
107 byte[] anylocal = { 0, 0, 0, 0 };
108
109 return Arrays.equals(addr, anylocal);
110 }
111
112 /**
113 * Checks if this address is a link local address
114 *
115 * @since 1.4
116 */
117 public boolean isLinkLocalAddress()
118 {
119 // XXX: This seems to not exist with IPv4 addresses
120 return false;
121 }
122
123 /**
124 * Checks if this address is a site local address
125 *
126 * @since 1.4
127 */
128 public boolean isSiteLocalAddress()
129 {
130 // 10.0.0.0/8
131 if (addr[0] == 0x0A)
132 return true;
133
134 // XXX: Suns JDK 1.4.1 (on Linux) seems to have a bug here:
135 // it says 172.16.0.0 - 172.255.255.255 are site local addresses
136 //
137 // 172.16.0.0/12
138 if (addr[0] == 0xAC && (addr[1] & 0xF0) == 0x01)
139 return true;
140
141 // 192.168.0.0/16
142 if (addr[0] == 0xC0 && addr[1] == 0xA8)
143 return true;
144
145 // XXX: Do we need to check more addresses here ?
146 return false;
147 }
148
149 /**
150 * Checks if this multicast address has global scope
151 *
152 * @since 1.4
153 */
154 public boolean isMCGlobal()
155 {
156 // XXX: This seems to net exist with IPv4 addresses
157 return false;
158 }
159
160 /**
161 * Checks if this multicast address has node scope
162 *
163 * @since 1.4
164 */
165 public boolean isMCNodeLocal()
166 {
167 // XXX: This seems to net exist with IPv4 addresses
168 return false;
169 }
170
171 /**
172 * Checks if this multicast address has link scope
173 *
174 * @since 1.4
175 */
176 public boolean isMCLinkLocal()
177 {
178 if (! isMulticastAddress())
179 return false;
180
181 return (addr[0] == 0xE0) && (addr[1] == 0x00) && (addr[2] == 0x00);
182 }
183
184 /**
185 * Checks if this multicast address has site scope
186 *
187 * @since 1.4
188 */
189 public boolean isMCSiteLocal()
190 {
191 // XXX: This seems to net exist with IPv4 addresses
192 return false;
193 }
194
195 /**
196 * Checks if this multicast address has organization scope
197 *
198 * @since 1.4
199 */
200 public boolean isMCOrgLocal()
201 {
202 // XXX: This seems to net exist with IPv4 addresses
203 return false;
204 }
205
206 /**
207 * Returns the address of the current instance
208 */
209 public byte[] getAddress()
210 {
211 return addr;
212 }
213
214 /**
215 * Returns the address as string
216 *
217 * @since 1.0.2
218 */
219 public String getHostAddress()
220 {
221 StringBuffer sbuf = new StringBuffer(40);
222 int len = addr.length;
223 int i = 0;
224
225 for (;;)
226 {
227 sbuf.append(addr[i] & 0xFF);
228 i++;
229
230 if (i == len)
231 break;
232
233 sbuf.append('.');
234 }
235
236 return sbuf.toString();
237 }
238
239 /**
240 * Computes the hashcode of the instance
241 */
242 public int hashCode()
243 {
244 int hash = 0;
245 int len = addr.length;
246 int i = len > 4 ? len - 4 : 0;
247
248 for (; i < len; i++)
249 hash = (hash << 8) | (addr[i] & 0xFF);
250
251 return hash;
252 }
253
254 /**
255 * Compare the current Inet4Address instance with obj
256 *
257 * @param obj Object to compare with
258 */
259 public boolean equals(Object obj)
260 {
261 if (! (obj instanceof InetAddress))
262 return false;
263
264 byte[] addr1 = addr;
265 byte[] addr2 = ((InetAddress) obj).addr;
266
267 if (addr1.length != addr2.length)
268 return false;
269
270 for (int i = addr1.length; --i >= 0;)
271 if (addr1[i] != addr2[i])
272 return false;
273
274 return true;
275 }
276 } // class Inet4Address
This page took 0.048301 seconds and 5 git commands to generate.