01 /*
02 * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
03 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04 *
05 * This code is free software; you can redistribute it and/or modify it
06 * under the terms of the GNU General Public License version 2 only, as
07 * published by the Free Software Foundation. Sun designates this
08 * particular file as subject to the "Classpath" exception as provided
09 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25 package java.net;
26
27 import java.io.IOException;
28
29 /*
30 * Package private implementation of InetAddressImpl for IPv4.
31 *
32 * @since 1.4
33 */
34 class Inet4AddressImpl implements InetAddressImpl {
35 public native String getLocalHostName() throws UnknownHostException;
36
37 public native InetAddress[] lookupAllHostAddr(String hostname)
38 throws UnknownHostException;
39
40 public native String getHostByAddr(byte[] addr)
41 throws UnknownHostException;
42
43 private native boolean isReachable0(byte[] addr, int timeout,
44 byte[] ifaddr, int ttl) throws IOException;
45
46 public synchronized InetAddress anyLocalAddress() {
47 if (anyLocalAddress == null) {
48 anyLocalAddress = new Inet4Address(); // {0x00,0x00,0x00,0x00}
49 anyLocalAddress.hostName = "0.0.0.0";
50 }
51 return anyLocalAddress;
52 }
53
54 public synchronized InetAddress loopbackAddress() {
55 if (loopbackAddress == null) {
56 byte[] loopback = { 0x7f, 0x00, 0x00, 0x01 };
57 loopbackAddress = new Inet4Address("localhost", loopback);
58 }
59 return loopbackAddress;
60 }
61
62 public boolean isReachable(InetAddress addr, int timeout,
63 NetworkInterface netif, int ttl) throws IOException {
64 byte[] ifaddr = null;
65 if (netif != null) {
66 /*
67 * Let's make sure we use an address of the proper family
68 */
69 java.util.Enumeration it = netif.getInetAddresses();
70 InetAddress inetaddr = null;
71 while (!(inetaddr instanceof Inet4Address)
72 && it.hasMoreElements())
73 inetaddr = (InetAddress) it.nextElement();
74 if (inetaddr instanceof Inet4Address)
75 ifaddr = inetaddr.getAddress();
76 }
77 return isReachable0(addr.getAddress(), timeout, ifaddr, ttl);
78 }
79
80 private InetAddress anyLocalAddress;
81 private InetAddress loopbackAddress;
82 }
|