jdk/src/share/classes/java/net/Inet6AddressImpl.java
author xdono
Thu, 02 Oct 2008 19:58:32 -0700
changeset 1247 b4c26443dee5
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6754988: Update copyright year Summary: Update for files that have been modified starting July 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Package private implementation of InetAddressImpl for dual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * IPv4/IPv6 stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * If InetAddress.preferIPv6Address is true then anyLocalAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * loopbackAddress(), and localHost() will return IPv6 addresses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * otherwise IPv4 addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
class Inet6AddressImpl implements InetAddressImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public native String getLocalHostName() throws UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public native InetAddress[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        lookupAllHostAddr(String hostname) throws UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public native String getHostByAddr(byte[] addr) throws UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private native boolean isReachable0(byte[] addr, int scope, int timeout, byte[] inf, int ttl, int if_scope) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public boolean isReachable(InetAddress addr, int timeout, NetworkInterface netif, int ttl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        byte[] ifaddr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        int scope = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        int netif_scope = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        if (netif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
             * Let's make sure we bind to an address of the proper family.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
             * Which means same family as addr because at this point it could
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
             * be either an IPv6 address or an IPv4 address (case of a dual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
             * stack system).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            java.util.Enumeration it = netif.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            InetAddress inetaddr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            while (it.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                inetaddr = (InetAddress) it.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                if (inetaddr.getClass().isInstance(addr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                    ifaddr = inetaddr.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    if (inetaddr instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                        netif_scope = ((Inet6Address) inetaddr).getScopeId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (ifaddr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                // Interface doesn't support the address family of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                // the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (addr instanceof Inet6Address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            scope = ((Inet6Address) addr).getScopeId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return isReachable0(addr.getAddress(), scope, timeout, ifaddr, ttl, netif_scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public synchronized InetAddress anyLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (anyLocalAddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (InetAddress.preferIPv6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                anyLocalAddress = new Inet6Address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                anyLocalAddress.hostName = "::";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                anyLocalAddress = (new Inet4AddressImpl()).anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return anyLocalAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public synchronized InetAddress loopbackAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (loopbackAddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
             if (InetAddress.preferIPv6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                 byte[] loopback =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                 loopbackAddress = new Inet6Address("localhost", loopback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
             } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                loopbackAddress = (new Inet4AddressImpl()).loopbackAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return loopbackAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private InetAddress      anyLocalAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private InetAddress      loopbackAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
}