Path: blob/master/src/java.base/share/classes/java/net/Inet6AddressImpl.java
41152 views
/*1* Copyright (c) 2002, 2019, 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;2526import java.io.IOException;2728import static java.net.InetAddress.IPv6;29import static java.net.InetAddress.PREFER_IPV6_VALUE;30import static java.net.InetAddress.PREFER_SYSTEM_VALUE;3132/*33* Package private implementation of InetAddressImpl for dual34* IPv4/IPv6 stack.35* <p>36* If InetAddress.preferIPv6Address is true then anyLocalAddress()37* and localHost() will return IPv6 addresses, otherwise IPv4 addresses.38*39* loopbackAddress() will return the first valid loopback address in40* [IPv6 loopback, IPv4 loopback] if InetAddress.preferIPv6Address is true,41* else [IPv4 loopback, IPv6 loopback].42* If neither are valid it will fallback to the first address tried.43*44* @since 1.445*/46class Inet6AddressImpl implements InetAddressImpl {4748public native String getLocalHostName() throws UnknownHostException;4950public native InetAddress[] lookupAllHostAddr(String hostname)51throws UnknownHostException;5253public native String getHostByAddr(byte[] addr) throws UnknownHostException;5455private native boolean isReachable0(byte[] addr, int scope, int timeout,56byte[] inf, int ttl, int if_scope)57throws IOException;5859public boolean isReachable(InetAddress addr, int timeout,60NetworkInterface netif, int ttl)61throws IOException62{63byte[] ifaddr = null;64int scope = -1;65int netif_scope = -1;66if (netif != null) {67/*68* Let's make sure we bind to an address of the proper family.69* Which means same family as addr because at this point it could70* be either an IPv6 address or an IPv4 address (case of a dual71* stack system).72*/73java.util.Enumeration<InetAddress> it = netif.getInetAddresses();74InetAddress inetaddr;75while (it.hasMoreElements()) {76inetaddr = it.nextElement();77if (inetaddr.getClass().isInstance(addr)) {78ifaddr = inetaddr.getAddress();79if (inetaddr instanceof Inet6Address) {80netif_scope = ((Inet6Address) inetaddr).getScopeId();81}82break;83}84}85if (ifaddr == null) {86// Interface doesn't support the address family of87// the destination88return false;89}90}91if (addr instanceof Inet6Address)92scope = ((Inet6Address) addr).getScopeId();93return isReachable0(addr.getAddress(), scope, timeout, ifaddr, ttl, netif_scope);94}9596public synchronized InetAddress anyLocalAddress() {97if (anyLocalAddress == null) {98if (InetAddress.preferIPv6Address == PREFER_IPV6_VALUE ||99InetAddress.preferIPv6Address == PREFER_SYSTEM_VALUE) {100anyLocalAddress = new Inet6Address();101anyLocalAddress.holder().hostName = "::";102} else {103anyLocalAddress = (new Inet4AddressImpl()).anyLocalAddress();104}105}106return anyLocalAddress;107}108109public synchronized InetAddress loopbackAddress() {110if (loopbackAddress == null) {111boolean preferIPv6Address =112InetAddress.preferIPv6Address == PREFER_IPV6_VALUE ||113InetAddress.preferIPv6Address == PREFER_SYSTEM_VALUE;114115for (int i = 0; i < 2; i++) {116InetAddress address;117// Order the candidate addresses by preference.118if (i == (preferIPv6Address ? 0 : 1)) {119address = new Inet6Address("localhost",120new byte[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,1210x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01});122} else {123address = new Inet4Address("localhost", new byte[]{ 0x7f,0x00,0x00,0x01 });124}125if (i == 0) {126// In case of failure, default to the preferred address.127loopbackAddress = address;128}129try {130if (!NetworkInterface.isBoundInetAddress(address)) {131continue;132}133} catch (SocketException e) {134continue;135}136loopbackAddress = address;137break;138}139}140return loopbackAddress;141}142143private InetAddress anyLocalAddress;144private InetAddress loopbackAddress;145}146147148