5518N/A/*
5518N/A * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
5518N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5518N/A *
5518N/A * This code is free software; you can redistribute it and/or modify it
5518N/A * under the terms of the GNU General Public License version 2 only, as
5518N/A * published by the Free Software Foundation.
5518N/A *
5518N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5518N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5518N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5518N/A * version 2 for more details (a copy is included in the LICENSE file that
5518N/A * accompanied this code).
5518N/A *
5518N/A * You should have received a copy of the GNU General Public License version
5518N/A * 2 along with this work; if not, write to the Free Software Foundation,
5518N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5518N/A *
5518N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5518N/A * or visit www.oracle.com if you need additional information or have any
5518N/A * questions.
5518N/A */
5518N/A
5518N/A/*
5518N/A * Portions Copyright (c) 2012 IBM Corporation
5518N/A */
5518N/A
5518N/A/* @test
5518N/A * @bug 7163874
5518N/A * @summary InetAddress.isReachable is returning false
5518N/A * for InetAdress 0.0.0.0 and ::0
5518N/A * @run main PingThis
5518N/A * @run main/othervm -Djava.net.preferIPv4Stack=true PingThis
5518N/A */
5518N/A
5518N/Aimport java.net.Inet6Address;
5518N/Aimport java.net.InetAddress;
5518N/Aimport java.net.NetworkInterface;
5518N/Aimport java.util.ArrayList;
5518N/Aimport java.util.Collections;
5518N/Aimport java.util.Iterator;
5518N/Aimport java.util.List;
5518N/A
5518N/Apublic class PingThis {
5518N/A private static boolean hasIPv6() throws Exception {
5518N/A List<NetworkInterface> nics = Collections.list(NetworkInterface
5518N/A .getNetworkInterfaces());
5518N/A for (NetworkInterface nic : nics) {
5518N/A List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
5518N/A for (InetAddress addr : addrs) {
5518N/A if (addr instanceof Inet6Address)
5518N/A return true;
5518N/A }
5518N/A }
5518N/A
5518N/A return false;
5518N/A }
5518N/A
5518N/A public static void main(String args[]) throws Exception {
5518N/A if (System.getProperty("os.name").startsWith("Windows")) {
5518N/A return;
5518N/A }
5518N/A
5518N/A boolean preferIPv4Stack = "true".equals(System
5518N/A .getProperty("java.net.preferIPv4Stack"));
5518N/A List<String> addrs = new ArrayList<String>();
5518N/A InetAddress inetAddress = null;
5518N/A
5518N/A addrs.add("0.0.0.0");
5518N/A if (!preferIPv4Stack) {
5518N/A if (hasIPv6()) {
5518N/A addrs.add("::0");
5518N/A }
5518N/A }
5518N/A
5518N/A for (String addr : addrs) {
5518N/A inetAddress = InetAddress.getByName(addr);
5518N/A System.out.println("The target ip is "
5518N/A + inetAddress.getHostAddress());
5518N/A boolean isReachable = inetAddress.isReachable(3000);
5518N/A System.out.println("the target is reachable: " + isReachable);
5518N/A if (isReachable) {
5518N/A System.out.println("Test passed ");
5518N/A } else {
5518N/A System.out.println("Test failed ");
5518N/A throw new Exception("address " + inetAddress.getHostAddress()
5518N/A + " can not be reachable!");
5518N/A }
5518N/A }
5518N/A }
5518N/A}