001 /*
002 * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package java.net;
026
027 import java.io.IOException;
028
029 /*
030 * Package private implementation of InetAddressImpl for dual
031 * IPv4/IPv6 stack.
032 * <p>
033 * If InetAddress.preferIPv6Address is true then anyLocalAddress(),
034 * loopbackAddress(), and localHost() will return IPv6 addresses,
035 * otherwise IPv4 addresses.
036 *
037 * @since 1.4
038 */
039
040 class Inet6AddressImpl implements InetAddressImpl {
041 public native String getLocalHostName() throws UnknownHostException;
042
043 public native InetAddress[] lookupAllHostAddr(String hostname)
044 throws UnknownHostException;
045
046 public native String getHostByAddr(byte[] addr)
047 throws UnknownHostException;
048
049 private native boolean isReachable0(byte[] addr, int scope,
050 int timeout, byte[] inf, int ttl, int if_scope)
051 throws IOException;
052
053 public boolean isReachable(InetAddress addr, int timeout,
054 NetworkInterface netif, int ttl) throws IOException {
055 byte[] ifaddr = null;
056 int scope = -1;
057 int netif_scope = -1;
058 if (netif != null) {
059 /*
060 * Let's make sure we bind to an address of the proper family.
061 * Which means same family as addr because at this point it could
062 * be either an IPv6 address or an IPv4 address (case of a dual
063 * stack system).
064 */
065 java.util.Enumeration it = netif.getInetAddresses();
066 InetAddress inetaddr = null;
067 while (it.hasMoreElements()) {
068 inetaddr = (InetAddress) it.nextElement();
069 if (inetaddr.getClass().isInstance(addr)) {
070 ifaddr = inetaddr.getAddress();
071 if (inetaddr instanceof Inet6Address) {
072 netif_scope = ((Inet6Address) inetaddr)
073 .getScopeId();
074 }
075 break;
076 }
077 }
078 if (ifaddr == null) {
079 // Interface doesn't support the address family of
080 // the destination
081 return false;
082 }
083 }
084 if (addr instanceof Inet6Address)
085 scope = ((Inet6Address) addr).getScopeId();
086 return isReachable0(addr.getAddress(), scope, timeout, ifaddr,
087 ttl, netif_scope);
088 }
089
090 public synchronized InetAddress anyLocalAddress() {
091 if (anyLocalAddress == null) {
092 if (InetAddress.preferIPv6Address) {
093 anyLocalAddress = new Inet6Address();
094 anyLocalAddress.hostName = "::";
095 } else {
096 anyLocalAddress = (new Inet4AddressImpl())
097 .anyLocalAddress();
098 }
099 }
100 return anyLocalAddress;
101 }
102
103 public synchronized InetAddress loopbackAddress() {
104 if (loopbackAddress == null) {
105 if (InetAddress.preferIPv6Address) {
106 byte[] loopback = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
107 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108 0x00, 0x01 };
109 loopbackAddress = new Inet6Address("localhost",
110 loopback);
111 } else {
112 loopbackAddress = (new Inet4AddressImpl())
113 .loopbackAddress();
114 }
115 }
116 return loopbackAddress;
117 }
118
119 private InetAddress anyLocalAddress;
120 private InetAddress loopbackAddress;
121 }
|