7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
authorchegar
Mon, 26 Sep 2011 11:48:37 -0700
changeset 10693 6e9ebed2e783
parent 10612 841791b02746
child 10694 cf59e2badd14
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected Summary: Use family of connected IP address to retrieve desired local address of the datagram socket Reviewed-by: chegar Contributed-by: kurchi.subhra.hazra@oracle.com
jdk/src/share/classes/java/net/AbstractPlainDatagramSocketImpl.java
jdk/src/windows/classes/java/net/TwoStacksPlainDatagramSocketImpl.java
jdk/src/windows/native/java/net/TwoStacksPlainDatagramSocketImpl.c
jdk/test/java/net/DatagramSocket/ChangingAddress.java
--- a/jdk/src/share/classes/java/net/AbstractPlainDatagramSocketImpl.java	Mon Sep 26 15:04:59 2011 +0100
+++ b/jdk/src/share/classes/java/net/AbstractPlainDatagramSocketImpl.java	Mon Sep 26 11:48:37 2011 -0700
@@ -44,7 +44,7 @@
     int timeout = 0;
     boolean connected = false;
     private int trafficClass = 0;
-    private InetAddress connectedAddress = null;
+    protected InetAddress connectedAddress = null;
     private int connectedPort = -1;
 
     /* cached socket options */
--- a/jdk/src/windows/classes/java/net/TwoStacksPlainDatagramSocketImpl.java	Mon Sep 26 15:04:59 2011 +0100
+++ b/jdk/src/windows/classes/java/net/TwoStacksPlainDatagramSocketImpl.java	Mon Sep 26 11:48:37 2011 -0700
@@ -99,10 +99,11 @@
         }
 
         if (optID == SO_BINDADDR) {
-            if (fd != null && fd1 != null) {
+            if ((fd != null && fd1 != null) && !connected) {
                 return anyLocalBoundAddr;
             }
-            return socketGetOption(optID);
+            int family = connectedAddress == null ? -1 : connectedAddress.family;
+            return socketLocalAddress(family);
         } else
             return super.getOption(optID);
     }
@@ -161,6 +162,8 @@
 
     protected native void connect0(InetAddress address, int port) throws SocketException;
 
+    protected native Object socketLocalAddress(int family) throws SocketException;
+
     protected native void disconnect0(int family);
 
     /**
--- a/jdk/src/windows/native/java/net/TwoStacksPlainDatagramSocketImpl.c	Mon Sep 26 15:04:59 2011 +0100
+++ b/jdk/src/windows/native/java/net/TwoStacksPlainDatagramSocketImpl.c	Mon Sep 26 11:48:37 2011 -0700
@@ -2181,30 +2181,6 @@
         return getMulticastInterface(env, this, fd, fd1, opt);
     }
 
-    if (opt == java_net_SocketOptions_SO_BINDADDR) {
-        /* find out local IP address */
-        SOCKETADDRESS him;
-        int len = 0;
-        int port;
-        jobject iaObj;
-
-        len = sizeof (struct sockaddr_in);
-
-        if (fd == -1) {
-            fd = fd1; /* must be IPv6 only */
-            len = sizeof (struct SOCKADDR_IN6);
-        }
-
-        if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) {
-            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
-                           "Error getting socket name");
-            return NULL;
-        }
-        iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
-
-        return iaObj;
-    }
-
     /*
      * Map the Java level socket option to the platform specific
      * level and option name.
@@ -2253,6 +2229,61 @@
 }
 
 /*
+ * Returns local address of the socket.
+ *
+ * Class:     java_net_TwoStacksPlainDatagramSocketImpl
+ * Method:    socketLocalAddress
+ * Signature: (I)Ljava/lang/Object;
+ */
+JNIEXPORT jobject JNICALL
+Java_java_net_TwoStacksPlainDatagramSocketImpl_socketLocalAddress(JNIEnv *env, jobject this,
+                                                      jint family) {
+
+    int fd=-1, fd1=-1;
+    SOCKETADDRESS him;
+    int len = 0;
+    int port;
+    jobject iaObj;
+    int ipv6_supported = ipv6_available();
+
+    fd = getFD(env, this);
+    if (ipv6_supported) {
+        fd1 = getFD1(env, this);
+    }
+
+    if (fd < 0 && fd1 < 0) {
+        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
+                        "Socket closed");
+        return NULL;
+    }
+
+    /* find out local IP address */
+
+    len = sizeof (struct sockaddr_in);
+
+    /* family==-1 when socket is not connected */
+    if ((family == IPv6) || (family == -1 && fd == -1)) {
+        fd = fd1; /* must be IPv6 only */
+        len = sizeof (struct SOCKADDR_IN6);
+    }
+
+    if (fd == -1) {
+        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
+                        "Socket closed");
+        return NULL;
+    }
+
+    if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) {
+        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
+                       "Error getting socket name");
+        return NULL;
+    }
+    iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
+
+    return iaObj;
+}
+
+/*
  * Class:     java_net_TwoStacksPlainDatagramSocketImpl
  * Method:    setTimeToLive
  * Signature: (I)V
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/net/DatagramSocket/ChangingAddress.java	Mon Sep 26 11:48:37 2011 -0700
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2011, 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 7084030
+ * @summary Tests that DatagramSocket.getLocalAddress returns the right local
+ *          address after connect/disconnect.
+ */
+import java.net.*;
+
+public class ChangingAddress {
+
+    static void check(DatagramSocket ds, InetAddress expected) {
+        InetAddress actual = ds.getLocalAddress();
+        if (!expected.equals(actual)) {
+            throw new RuntimeException("Expected:"+expected+" Actual"+
+                                       actual);
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+        InetAddress lh = InetAddress.getLocalHost();
+        SocketAddress remote = new InetSocketAddress(lh, 1234);
+        InetAddress wildcard = InetAddress.getByAddress
+                               ("localhost", new byte[]{0,0,0,0});
+        try (DatagramSocket ds = new DatagramSocket()) {
+            check(ds, wildcard);
+
+            ds.connect(remote);
+            check(ds, lh);
+
+            ds.disconnect();
+            check(ds, wildcard);
+       }
+    }
+}