Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/net/Inet6AddressImpl.java
38829 views
/*1* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package java.net;25import java.io.IOException;2627/*28* Package private implementation of InetAddressImpl for dual29* IPv4/IPv6 stack.30* <p>31* If InetAddress.preferIPv6Address is true then anyLocalAddress(),32* loopbackAddress(), and localHost() will return IPv6 addresses,33* otherwise IPv4 addresses.34*35* @since 1.436*/3738class Inet6AddressImpl implements InetAddressImpl {39public native String getLocalHostName() throws UnknownHostException;40public native InetAddress[]41lookupAllHostAddr(String hostname) throws UnknownHostException;42public native String getHostByAddr(byte[] addr) throws UnknownHostException;43private native boolean isReachable0(byte[] addr, int scope, int timeout, byte[] inf, int ttl, int if_scope) throws IOException;4445public boolean isReachable(InetAddress addr, int timeout, NetworkInterface netif, int ttl) throws IOException {46byte[] ifaddr = null;47int scope = -1;48int netif_scope = -1;49if (netif != null) {50/*51* Let's make sure we bind to an address of the proper family.52* Which means same family as addr because at this point it could53* be either an IPv6 address or an IPv4 address (case of a dual54* stack system).55*/56java.util.Enumeration<InetAddress> it = netif.getInetAddresses();57InetAddress inetaddr = null;58while (it.hasMoreElements()) {59inetaddr = it.nextElement();60if (inetaddr.getClass().isInstance(addr)) {61ifaddr = inetaddr.getAddress();62if (inetaddr instanceof Inet6Address) {63netif_scope = ((Inet6Address) inetaddr).getScopeId();64}65break;66}67}68if (ifaddr == null) {69// Interface doesn't support the address family of70// the destination71return false;72}73}74if (addr instanceof Inet6Address)75scope = ((Inet6Address) addr).getScopeId();76return isReachable0(addr.getAddress(), scope, timeout, ifaddr, ttl, netif_scope);77}7879public synchronized InetAddress anyLocalAddress() {80if (anyLocalAddress == null) {81if (InetAddress.preferIPv6Address) {82anyLocalAddress = new Inet6Address();83anyLocalAddress.holder().hostName = "::";84} else {85anyLocalAddress = (new Inet4AddressImpl()).anyLocalAddress();86}87}88return anyLocalAddress;89}9091public synchronized InetAddress loopbackAddress() {92if (loopbackAddress == null) {93if (InetAddress.preferIPv6Address) {94byte[] loopback =95{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,960x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};97loopbackAddress = new Inet6Address("localhost", loopback);98} else {99loopbackAddress = (new Inet4AddressImpl()).loopbackAddress();100}101}102return loopbackAddress;103}104105private InetAddress anyLocalAddress;106private InetAddress loopbackAddress;107}108109110