1 /* 2 * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.net; 26 import java.io.IOException; 27 28 /* 29 * Package private implementation of InetAddressImpl for dual 30 * IPv4/IPv6 stack. 31 * <p> 32 * If InetAddress.preferIPv6Address is true then anyLocalAddress(), 33 * loopbackAddress(), and localHost() will return IPv6 addresses, 34 * otherwise IPv4 addresses. 35 * 36 * @since 1.4 37 */ 38 39 class Inet6AddressImpl implements InetAddressImpl { 40 public native String getLocalHostName() throws UnknownHostException; 41 public native InetAddress[] 42 lookupAllHostAddr(String hostname) throws UnknownHostException; 43 public native String getHostByAddr(byte[] addr) throws UnknownHostException; 44 private native boolean isReachable0(byte[] addr, int scope, int timeout, byte[] inf, int ttl, int if_scope) throws IOException; 45 46 public boolean isReachable(InetAddress addr, int timeout, NetworkInterface netif, int ttl) throws IOException { 47 byte[] ifaddr = null; 48 int scope = -1; 49 int netif_scope = -1; 50 if (netif != null) { 51 /* 52 * Let's make sure we bind to an address of the proper family. 53 * Which means same family as addr because at this point it could 54 * be either an IPv6 address or an IPv4 address (case of a dual 55 * stack system). 56 */ 57 java.util.Enumeration it = netif.getInetAddresses(); 58 InetAddress inetaddr = null; 59 while (it.hasMoreElements()) { 60 inetaddr = (InetAddress) it.nextElement(); 61 if (inetaddr.getClass().isInstance(addr)) { 62 ifaddr = inetaddr.getAddress(); 63 if (inetaddr instanceof Inet6Address) { 64 netif_scope = ((Inet6Address) inetaddr).getScopeId(); 65 } 66 break; 67 } 68 } 69 if (ifaddr == null) { 70 // Interface doesn't support the address family of 71 // the destination 72 return false; 73 } 74 } 75 if (addr instanceof Inet6Address) 76 scope = ((Inet6Address) addr).getScopeId(); 77 return isReachable0(addr.getAddress(), scope, timeout, ifaddr, ttl, netif_scope); 78 } 79 80 public synchronized InetAddress anyLocalAddress() { 81 if (anyLocalAddress == null) { 82 if (InetAddress.preferIPv6Address) { 83 anyLocalAddress = new Inet6Address(); 84 anyLocalAddress.holder().hostName = "::"; 85 } else { 86 anyLocalAddress = (new Inet4AddressImpl()).anyLocalAddress(); 87 } 88 } 89 return anyLocalAddress; 90 } 91 92 public synchronized InetAddress loopbackAddress() { 93 if (loopbackAddress == null) { 94 if (InetAddress.preferIPv6Address) { 95 byte[] loopback = 96 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 97 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; 98 loopbackAddress = new Inet6Address("localhost", loopback); 99 } else { 100 loopbackAddress = (new Inet4AddressImpl()).loopbackAddress(); 101 } 102 } 103 return loopbackAddress; 104 } 105 106 private InetAddress anyLocalAddress; 107 private InetAddress loopbackAddress; 108 }