8179559: Solaris MulticastSocket issues
authormichaelm
Mon, 08 May 2017 14:04:27 +0100
changeset 44927 d554736d963e
parent 44926 32eeee1c0abd
child 44928 1a0cba08a6cb
8179559: Solaris MulticastSocket issues Reviewed-by: chegar
jdk/src/java.base/unix/native/libnet/NetworkInterface.c
jdk/test/java/net/MulticastSocket/SetLoopbackMode.java
jdk/test/java/net/MulticastSocket/Test.java
jdk/test/java/net/NetworkInterface/NetworkInterfaceRetrievalTests.java
--- a/jdk/src/java.base/unix/native/libnet/NetworkInterface.c	Fri May 05 04:52:02 2017 +0000
+++ b/jdk/src/java.base/unix/native/libnet/NetworkInterface.c	Mon May 08 14:04:27 2017 +0100
@@ -361,7 +361,11 @@
                         ((struct sockaddr_in6*)addrP->addr)->sin6_addr);
                     jbyte caddr[16];
                     int i;
+                    unsigned int scopeid;
                     getInet6Address_ipaddress(env, iaObj, (char *)caddr);
+                    scopeid = (unsigned int)getInet6Address_scopeid(env, iaObj);
+                    if (scopeid != 0 && scopeid != ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id)
+                        break;
                     i = 0;
                     while (i < 16) {
                         if (caddr[i] != bytes[i]) {
--- a/jdk/test/java/net/MulticastSocket/SetLoopbackMode.java	Fri May 05 04:52:02 2017 +0000
+++ b/jdk/test/java/net/MulticastSocket/SetLoopbackMode.java	Mon May 08 14:04:27 2017 +0100
@@ -25,9 +25,14 @@
  * @test
  * @bug 4686717
  * @summary Test MulticastSocket.setLoopbackMode
+ * @library /lib/testlibrary
+ * @build jdk.testlibrary.NetworkConfiguration
+ * @run main/othervm SetLoopbackMode
  */
 import java.net.*;
 import java.io.IOException;
+import java.util.Enumeration;
+import jdk.testlibrary.NetworkConfiguration;
 
 public class SetLoopbackMode {
 
@@ -85,8 +90,13 @@
         return PASSED;
     }
 
+    private static boolean canUseIPv6(NetworkConfiguration nc) {
+        return nc.ip6MulticastInterfaces().toArray().length > 0;
+    }
+
     public static void main (String args[]) throws Exception {
         int failures = 0;
+        NetworkConfiguration nc = NetworkConfiguration.probe();
 
         MulticastSocket mc = new MulticastSocket();
         InetAddress grp = InetAddress.getByName("224.80.80.80");
@@ -97,11 +107,12 @@
          * to workaround Linux IPv6 bug whereby !IPV6_MULTICAST_LOOP
          * doesn't prevent loopback of IPv4 multicast packets.
          */
-        InetAddress lb = InetAddress.getByName("::1");
-        if (NetworkInterface.getByInetAddress(lb) != null) {
+
+        if (canUseIPv6(nc)) {
             grp = InetAddress.getByName("ff01::1");
         }
 
+        //mc.setNetworkInterface(NetworkInterface.getByInetAddress(lb));
         System.out.println("\nTest will use multicast group: " + grp);
         mc.joinGroup(grp);
 
--- a/jdk/test/java/net/MulticastSocket/Test.java	Fri May 05 04:52:02 2017 +0000
+++ b/jdk/test/java/net/MulticastSocket/Test.java	Mon May 08 14:04:27 2017 +0100
@@ -35,6 +35,10 @@
     static int count = 0;
     static int failures = 0;
 
+    static boolean isSolaris = System.getProperty("os.name")
+        .toLowerCase()
+        .startsWith("sunos");
+
     void doTest(String address) throws Exception {
         boolean failed = false;
 
@@ -123,6 +127,14 @@
         }
     }
 
+    static boolean isValidIpv6Address(InetAddress addr) {
+        if (! (addr instanceof Inet6Address))
+            return false;
+        if (!isSolaris)
+            return true;
+        return !(addr.isAnyLocalAddress() || addr.isLoopbackAddress());
+    }
+
     void allTests() throws Exception {
 
         /*
@@ -146,7 +158,7 @@
             while (addrs.hasMoreElements()) {
                 InetAddress ia = (InetAddress)addrs.nextElement();
 
-                if (ia instanceof Inet6Address) {
+                if (isValidIpv6Address(ia)) {
                     has_ipv6 = true;
                     if (ia.isLinkLocalAddress()) has_linklocaladdress = true;
                     if (ia.isSiteLocalAddress()) has_siteaddress = true;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/net/NetworkInterface/NetworkInterfaceRetrievalTests.java	Mon May 08 14:04:27 2017 +0100
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8179559
+ */
+
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.util.Enumeration;
+
+public class NetworkInterfaceRetrievalTests {
+    public static void main(String[] args) throws Exception {
+        int checkFailureCount = 0;
+
+        try {
+            Enumeration<NetworkInterface> en = NetworkInterface
+                    .getNetworkInterfaces();
+            while (en.hasMoreElements()) {
+                NetworkInterface ni = en.nextElement();
+                Enumeration<InetAddress> addrs = ni.getInetAddresses();
+                System.out.println("############ Checking network interface + "
+                        + ni + " #############");
+                while (addrs.hasMoreElements()) {
+                    InetAddress addr = addrs.nextElement();
+                    System.out.println("************ Checking address  + "
+                            + addr + " *************");
+                    NetworkInterface addrNetIf = NetworkInterface
+                            .getByInetAddress(addr);
+                    if (addrNetIf.equals(ni)) {
+                        System.out.println("Retreived net if " + addrNetIf
+                                + " equal to owning net if " + ni);
+                    } else {
+                        System.out.println("Retreived net if " + addrNetIf
+                                + "NOT  equal to owning net if " + ni
+                                + "***********");
+                        checkFailureCount++;
+                    }
+
+                }
+            }
+
+        } catch (Exception ex) {
+
+        }
+
+        if (checkFailureCount > 0) {
+            throw new RuntimeException(
+                    "NetworkInterface lookup by address didn't match owner network interface");
+        }
+    }
+}