src/java.base/share/classes/java/net/Inet6AddressImpl.java
author chegar
Fri, 18 Oct 2019 21:25:01 +0100
branchdatagramsocketimpl-branch
changeset 58697 e3ff12d14d43
parent 55596 d01b345865d7
permissions -rw-r--r--
datagramsocketimpl-branch: minor refactoring
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55596
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
     2
 * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.net;
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    26
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
55596
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
    28
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
    29
import static java.net.InetAddress.IPv6;
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    30
import static java.net.InetAddress.PREFER_IPV6_VALUE;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    31
import static java.net.InetAddress.PREFER_SYSTEM_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Package private implementation of InetAddressImpl for dual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * IPv4/IPv6 stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
54945
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
    37
 * If InetAddress.preferIPv6Address is true then anyLocalAddress()
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
    38
 * and localHost() will return IPv6 addresses, otherwise IPv4 addresses.
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
    39
 *
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
    40
 * loopbackAddress() will return the first valid loopback address in
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
    41
 * [IPv6 loopback, IPv4 loopback] if InetAddress.preferIPv6Address is true,
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
    42
 * else [IPv4 loopback, IPv6 loopback].
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
    43
 * If neither are valid it will fallback to the first address tried.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    47
class Inet6AddressImpl implements InetAddressImpl {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public native String getLocalHostName() throws UnknownHostException;
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    50
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    51
    public native InetAddress[] lookupAllHostAddr(String hostname)
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    52
        throws UnknownHostException;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    53
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public native String getHostByAddr(byte[] addr) throws UnknownHostException;
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    55
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    56
    private native boolean isReachable0(byte[] addr, int scope, int timeout,
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    57
                                        byte[] inf, int ttl, int if_scope)
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    58
        throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    60
    public boolean isReachable(InetAddress addr, int timeout,
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    61
                               NetworkInterface netif, int ttl)
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    62
        throws IOException
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    63
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        byte[] ifaddr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int scope = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int netif_scope = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (netif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
             * Let's make sure we bind to an address of the proper family.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
             * Which means same family as addr because at this point it could
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
             * be either an IPv6 address or an IPv4 address (case of a dual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
             * stack system).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
             */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    74
            java.util.Enumeration<InetAddress> it = netif.getInetAddresses();
55596
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
    75
            InetAddress inetaddr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            while (it.hasMoreElements()) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    77
                inetaddr = it.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                if (inetaddr.getClass().isInstance(addr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    ifaddr = inetaddr.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    if (inetaddr instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                        netif_scope = ((Inet6Address) inetaddr).getScopeId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (ifaddr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                // Interface doesn't support the address family of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                // the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (addr instanceof Inet6Address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            scope = ((Inet6Address) addr).getScopeId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return isReachable0(addr.getAddress(), scope, timeout, ifaddr, ttl, netif_scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public synchronized InetAddress anyLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (anyLocalAddress == null) {
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
    99
            if (InetAddress.preferIPv6Address == PREFER_IPV6_VALUE ||
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
   100
                InetAddress.preferIPv6Address == PREFER_SYSTEM_VALUE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                anyLocalAddress = new Inet6Address();
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 10596
diff changeset
   102
                anyLocalAddress.holder().hostName = "::";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                anyLocalAddress = (new Inet4AddressImpl()).anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return anyLocalAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public synchronized InetAddress loopbackAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (loopbackAddress == null) {
54945
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
   112
            boolean preferIPv6Address =
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
   113
                InetAddress.preferIPv6Address == PREFER_IPV6_VALUE ||
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
   114
                InetAddress.preferIPv6Address == PREFER_SYSTEM_VALUE;
55596
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   115
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   116
            for (int i = 0; i < 2; i++) {
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   117
                InetAddress address;
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   118
                // Order the candidate addresses by preference.
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   119
                if (i == (preferIPv6Address ? 0 : 1)) {
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   120
                    address = new Inet6Address("localhost",
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   121
                            new byte[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   122
                                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01});
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   123
                } else {
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   124
                    address = new Inet4Address("localhost", new byte[]{ 0x7f,0x00,0x00,0x01 });
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   125
                }
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   126
                if (i == 0) {
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   127
                    // In case of failure, default to the preferred address.
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   128
                    loopbackAddress = address;
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   129
                }
54945
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
   130
                try {
55596
d01b345865d7 8225239: Refactor NetworkInterface lookups
redestad
parents: 54945
diff changeset
   131
                    if (!NetworkInterface.isBoundInetAddress(address)) {
54945
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
   132
                        continue;
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
   133
                    }
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
   134
                } catch (SocketException e) {
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
   135
                    continue;
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
   136
                }
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
   137
                loopbackAddress = address;
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
   138
                break;
cc92a45f4c73 8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents: 47216
diff changeset
   139
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return loopbackAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
   144
    private InetAddress anyLocalAddress;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 25859
diff changeset
   145
    private InetAddress loopbackAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}