KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > Inet4Address


1 /*
2  * @(#)Inet4Address.java 1.28 04/02/12
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.net;
9
10 import java.security.AccessController JavaDoc;
11 import java.io.ObjectStreamException JavaDoc;
12 import sun.security.action.*;
13
14 /**
15  * This class represents an Internet Protocol version 4 (IPv4) address.
16  * Defined by <a HREF="http://www.ietf.org/rfc/rfc790.txt">
17  * <i>RFC&nbsp;790: Assigned Numbers</i></a>,
18  * <a HREF="http://www.ietf.org/rfc/rfc1918.txt">
19  * <i>RFC&nbsp;1918: Address Allocation for Private Internets</i></a>,
20  * and <a HREF="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC&nbsp;2365:
21  * Administratively Scoped IP Multicast</i></a>
22  *
23  * <h4> <A NAME="format">Textual representation of IP addresses</a> </h4>
24  *
25  * Textual representation of IPv4 address used as input to methods
26  * takes one of the following forms:
27  *
28  * <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
29  * <tr><td><tt>d.d.d.d</tt></td></tr>
30  * <tr><td><tt>d.d.d</tt></td></tr>
31  * <tr><td><tt>d.d</tt></td></tr>
32  * <tr><td><tt>d</tt></td></tr>
33  * </table></blockquote>
34  *
35  * <p> When four parts are specified, each is interpreted as a byte of
36  * data and assigned, from left to right, to the four bytes of an IPv4
37  * address.
38
39  * <p> When a three part address is specified, the last part is
40  * interpreted as a 16-bit quantity and placed in the right most two
41  * bytes of the network address. This makes the three part address
42  * format convenient for specifying Class B net- work addresses as
43  * 128.net.host.
44  *
45  * <p> When a two part address is supplied, the last part is
46  * interpreted as a 24-bit quantity and placed in the right most three
47  * bytes of the network address. This makes the two part address
48  * format convenient for specifying Class A network addresses as
49  * net.host.
50  *
51  * <p> When only one part is given, the value is stored directly in
52  * the network address without any byte rearrangement.
53  *
54  * <p> For methods that return a textual representation as output
55  * value, the first form, i.e. a dotted-quad string, is used.
56  *
57  * <h4> The Scope of a Multicast Address </h4>
58  *
59  * Historically the IPv4 TTL field in the IP header has doubled as a
60  * multicast scope field: a TTL of 0 means node-local, 1 means
61  * link-local, up through 32 means site-local, up through 64 means
62  * region-local, up through 128 means continent-local, and up through
63  * 255 are global. However, the administrative scoping is preferred.
64  * Please refer to <a HREF="http://www.ietf.org/rfc/rfc2365.txt">
65  * <i>RFC&nbsp;2365: Administratively Scoped IP Multicast</i></a>
66  */

67
68 public final
69 class Inet4Address extends InetAddress JavaDoc {
70     final static int INADDRSZ = 4;
71
72     /** use serialVersionUID from InetAddress, but Inet4Address instance
73      * is always replaced by an InetAddress instance before being
74      * serialized */

75     private static final long serialVersionUID = 3286316764910316507L;
76
77     /*
78      * Perform initializations.
79      */

80     static {
81         init();
82     }
83    
84     Inet4Address() {
85     super();
86     hostName = null;
87     address = 0;
88     family = IPv4;
89     }
90
91     Inet4Address(String JavaDoc hostName, byte addr[]) {
92     this.hostName = hostName;
93     this.family = IPv4;
94     if (addr != null) {
95         if (addr.length == INADDRSZ) {
96         address = addr[3] & 0xFF;
97         address |= ((addr[2] << 8) & 0xFF00);
98         address |= ((addr[1] << 16) & 0xFF0000);
99         address |= ((addr[0] << 24) & 0xFF000000);
100         }
101     }
102     }
103     Inet4Address(String JavaDoc hostName, int address) {
104     this.hostName = hostName;
105     this.family = IPv4;
106     this.address = address;
107     }
108
109     /**
110      * Replaces the object to be serialized with an InetAddress object.
111      *
112      * @return the alternate object to be serialized.
113      *
114      * @throws ObjectStreamException if a new object replacing this
115      * object could not be created
116      */

117     private Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc {
118     // will replace the to be serialized 'this' object
119
InetAddress JavaDoc inet = new InetAddress JavaDoc();
120     inet.hostName = this.hostName;
121     inet.address = this.address;
122
123     /**
124      * Prior to 1.4 an InetAddress was created with a family
125      * based on the platform AF_INET value (usually 2).
126          * For compatibility reasons we must therefore write the
127      * the InetAddress with this family.
128      */

129     inet.family = 2;
130
131     return inet;
132     }
133
134     /**
135      * Utility routine to check if the InetAddress is an
136      * IP multicast address. IP multicast address is a Class D
137      * address i.e first four bits of the address are 1110.
138      * @return a <code>boolean</code> indicating if the InetAddress is
139      * an IP multicast address
140      * @since JDK1.1
141      */

142     public boolean isMulticastAddress() {
143     return ((address & 0xf0000000) == 0xe0000000);
144     }
145
146     /**
147      * Utility routine to check if the InetAddress in a wildcard address.
148      * @return a <code>boolean</code> indicating if the Inetaddress is
149      * a wildcard address.
150      * @since 1.4
151      */

152     public boolean isAnyLocalAddress() {
153     return address == 0;
154     }
155
156     /**
157      * Utility routine to check if the InetAddress is a loopback address.
158      *
159      * @return a <code>boolean</code> indicating if the InetAddress is
160      * a loopback address; or false otherwise.
161      * @since 1.4
162      */

163     private static final int loopback = 2130706433; /* 127.0.0.1 */
164     public boolean isLoopbackAddress() {
165     /* 127.x.x.x */
166     byte[] byteAddr = getAddress();
167     return byteAddr[0] == 127;
168     }
169
170     /**
171      * Utility routine to check if the InetAddress is an link local address.
172      *
173      * @return a <code>boolean</code> indicating if the InetAddress is
174      * a link local address; or false if address is not a link local unicast address.
175      * @since 1.4
176      */

177     public boolean isLinkLocalAddress() {
178     // link-local unicast in IPv4 (169.254.0.0/16)
179
// defined in "Documenting Special Use IPv4 Address Blocks
180
// that have been Registered with IANA" by Bill Manning
181
// draft-manning-dsua-06.txt
182
return (((address >>> 24) & 0xFF) == 169)
183         && (((address >>> 16) & 0xFF) == 254);
184     }
185
186     /**
187      * Utility routine to check if the InetAddress is a site local address.
188      *
189      * @return a <code>boolean</code> indicating if the InetAddress is
190      * a site local address; or false if address is not a site local unicast address.
191      * @since 1.4
192      */

193     public boolean isSiteLocalAddress() {
194     // refer to RFC 1918
195
// 10/8 prefix
196
// 172.16/12 prefix
197
// 192.168/16 prefix
198
return (((address >>> 24) & 0xFF) == 10)
199         || ((((address >>> 24) & 0xFF) == 172)
200         && (((address >>> 16) & 0xF0) == 16))
201         || ((((address >>> 24) & 0xFF) == 192)
202         && (((address >>> 16) & 0xFF) == 168));
203     }
204
205     /**
206      * Utility routine to check if the multicast address has global scope.
207      *
208      * @return a <code>boolean</code> indicating if the address has
209      * is a multicast address of global scope, false if it is not
210      * of global scope or it is not a multicast address
211      * @since 1.4
212      */

213     public boolean isMCGlobal() {
214     // 224.0.1.0 to 238.255.255.255
215
byte[] byteAddr = getAddress();
216     return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) &&
217         !((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 &&
218           byteAddr[2] == 0);
219     }
220
221     /**
222      * Utility routine to check if the multicast address has node scope.
223      *
224      * @return a <code>boolean</code> indicating if the address has
225      * is a multicast address of node-local scope, false if it is not
226      * of node-local scope or it is not a multicast address
227      * @since 1.4
228      */

229     public boolean isMCNodeLocal() {
230     // unless ttl == 0
231
return false;
232     }
233
234     /**
235      * Utility routine to check if the multicast address has link scope.
236      *
237      * @return a <code>boolean</code> indicating if the address has
238      * is a multicast address of link-local scope, false if it is not
239      * of link-local scope or it is not a multicast address
240      * @since 1.4
241      */

242     public boolean isMCLinkLocal() {
243     // 224.0.0/24 prefix and ttl == 1
244
return (((address >>> 24) & 0xFF) == 224)
245         && (((address >>> 16) & 0xFF) == 0)
246         && (((address >>> 8) & 0xFF) == 0);
247     }
248
249     /**
250      * Utility routine to check if the multicast address has site scope.
251      *
252      * @return a <code>boolean</code> indicating if the address has
253      * is a multicast address of site-local scope, false if it is not
254      * of site-local scope or it is not a multicast address
255      * @since 1.4
256      */

257     public boolean isMCSiteLocal() {
258     // 239.255/16 prefix or ttl < 32
259
return (((address >>> 24) & 0xFF) == 239)
260         && (((address >>> 16) & 0xFF) == 255);
261     }
262
263     /**
264      * Utility routine to check if the multicast address has organization scope.
265      *
266      * @return a <code>boolean</code> indicating if the address has
267      * is a multicast address of organization-local scope,
268      * false if it is not of organization-local scope
269      * or it is not a multicast address
270      * @since 1.4
271      */

272     public boolean isMCOrgLocal() {
273     // 239.192 - 239.195
274
return (((address >>> 24) & 0xFF) == 239)
275         && (((address >>> 16) & 0xFF) >= 192)
276         && (((address >>> 16) & 0xFF) <= 195);
277     }
278
279     /**
280      * Returns the raw IP address of this <code>InetAddress</code>
281      * object. The result is in network byte order: the highest order
282      * byte of the address is in <code>getAddress()[0]</code>.
283      *
284      * @return the raw IP address of this object.
285      */

286     public byte[] getAddress() {
287     byte[] addr = new byte[INADDRSZ];
288
289     addr[0] = (byte) ((address >>> 24) & 0xFF);
290     addr[1] = (byte) ((address >>> 16) & 0xFF);
291     addr[2] = (byte) ((address >>> 8) & 0xFF);
292     addr[3] = (byte) (address & 0xFF);
293     return addr;
294     }
295
296     /**
297      * Returns the IP address string in textual presentation form.
298      *
299      * @return the raw IP address in a string format.
300      * @since JDK1.0.2
301      */

302     public String JavaDoc getHostAddress() {
303     return numericToTextFormat(getAddress());
304     }
305
306     /**
307      * Returns a hashcode for this IP address.
308      *
309      * @return a hash code value for this IP address.
310      */

311     public int hashCode() {
312     return address;
313     }
314     
315     /**
316      * Compares this object against the specified object.
317      * The result is <code>true</code> if and only if the argument is
318      * not <code>null</code> and it represents the same IP address as
319      * this object.
320      * <p>
321      * Two instances of <code>InetAddress</code> represent the same IP
322      * address if the length of the byte arrays returned by
323      * <code>getAddress</code> is the same for both, and each of the
324      * array components is the same for the byte arrays.
325      *
326      * @param obj the object to compare against.
327      * @return <code>true</code> if the objects are the same;
328      * <code>false</code> otherwise.
329      * @see java.net.InetAddress#getAddress()
330      */

331     public boolean equals(Object JavaDoc obj) {
332     return (obj != null) && (obj instanceof Inet4Address JavaDoc) &&
333         (((InetAddress JavaDoc)obj).address == address);
334     }
335
336     // Utilities
337
/*
338      * Converts IPv4 binary address into a string suitable for presentation.
339      *
340      * @param src a byte array representing an IPv4 numeric address
341      * @return a String representing the IPv4 address in
342      * textual representation format
343      * @since 1.4
344      */

345     
346     static String JavaDoc numericToTextFormat(byte[] src)
347     {
348     return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);
349     }
350     
351     /**
352      * Perform class load-time initializations.
353      */

354     private static native void init();
355 }
356
Popular Tags