1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * Portions Copyright (c) 2012 IBM Corporation 26 */ 27 28 /* @test 29 * @bug 7163874 8133015 30 * @summary InetAddress.isReachable is returning false 31 * for InetAdress 0.0.0.0 and ::0 32 * @run main PingThis 33 * @run main/othervm -Djava.net.preferIPv4Stack=true PingThis 34 */ 35 36 import java.net.Inet6Address; 37 import java.net.InetAddress; 38 import java.net.NetworkInterface; 39 import java.util.ArrayList; 40 import java.util.Collections; 41 import java.util.Iterator; 42 import java.util.List; 43 44 public class PingThis { hasIPv6()45 private static boolean hasIPv6() throws Exception { 46 List<NetworkInterface> nics = Collections.list(NetworkInterface 47 .getNetworkInterfaces()); 48 for (NetworkInterface nic : nics) { 49 List<InetAddress> addrs = Collections.list(nic.getInetAddresses()); 50 for (InetAddress addr : addrs) { 51 if (addr instanceof Inet6Address) 52 return true; 53 } 54 } 55 56 return false; 57 } 58 main(String args[])59 public static void main(String args[]) throws Exception { 60 if (System.getProperty("os.name").startsWith("Windows")) { 61 return; 62 } 63 64 boolean preferIPv4Stack = "true".equals(System 65 .getProperty("java.net.preferIPv4Stack")); 66 List<String> addrs = new ArrayList<String>(); 67 InetAddress inetAddress = null; 68 69 addrs.add("0.0.0.0"); 70 if (!preferIPv4Stack) { 71 if (hasIPv6()) { 72 addrs.add("::0"); 73 } 74 } 75 76 for (String addr : addrs) { 77 inetAddress = InetAddress.getByName(addr); 78 System.out.println("The target ip is " 79 + inetAddress.getHostAddress()); 80 boolean isReachable = inetAddress.isReachable(3000); 81 System.out.println("the target is reachable: " + isReachable); 82 if (isReachable) { 83 System.out.println("Test passed "); 84 } else { 85 System.out.println("Test failed "); 86 throw new Exception("address " + inetAddress.getHostAddress() 87 + " can not be reachable!"); 88 } 89 } 90 } 91 } 92