OSDN Git Service

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