# HG changeset patch # User dfuchs # Date 1570639138 -3600 # Node ID 705c3f88a4098b96edd6e9787510f152a96c0b3d # Parent 252e7f4c4d926fc92f2945f5058a8e373760ecdd 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx) Summary: DatagramChannel::disconnect will attempt to rebind to the original port if the local port switches back to 0 after the association is disolved by the system. Reviewed-by: alanb, chegar, fweimer diff -r 252e7f4c4d92 -r 705c3f88a409 src/java.base/share/classes/java/nio/channels/DatagramChannel.java --- a/src/java.base/share/classes/java/nio/channels/DatagramChannel.java Wed Oct 09 09:23:22 2019 -0700 +++ b/src/java.base/share/classes/java/nio/channels/DatagramChannel.java Wed Oct 09 17:38:58 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2019, 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 @@ -328,6 +328,10 @@ *

If this channel's socket is not connected, or if the channel is * closed, then invoking this method has no effect.

* + * @apiNote If this method throws an IOException, the channel's socket + * may be left in an unspecified state. It is strongly recommended that + * the channel be closed when disconnect fails. + * * @return This datagram channel * * @throws IOException diff -r 252e7f4c4d92 -r 705c3f88a409 src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java --- a/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java Wed Oct 09 09:23:22 2019 -0700 +++ b/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java Wed Oct 09 17:38:58 2019 +0100 @@ -875,6 +875,11 @@ if (state == ST_CONNECTED) throw new AlreadyConnectedException(); + // ensure that the socket is bound + if (localAddress == null) { + bindInternal(null); + } + int n = Net.connect(family, fd, isa.getAddress(), @@ -932,8 +937,21 @@ remoteAddress = null; state = ST_UNCONNECTED; - // refresh local address - localAddress = Net.localAddress(fd); + // check whether rebind is needed + InetSocketAddress isa = Net.localAddress(fd); + if (isa.getPort() == 0) { + // On Linux, if bound to ephemeral port, + // disconnect does not preserve that port. + // In this case, try to rebind to the previous port. + int port = localAddress.getPort(); + localAddress = isa; // in case Net.bind fails + Net.bind(family, fd, isa.getAddress(), port); + isa = Net.localAddress(fd); // refresh address + assert isa.getPort() == port; + } + + // refresh localAddress + localAddress = isa; } } finally { writeLock.unlock(); diff -r 252e7f4c4d92 -r 705c3f88a409 test/jdk/java/nio/channels/DatagramChannel/AddressesAfterDisconnect.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/jdk/java/nio/channels/DatagramChannel/AddressesAfterDisconnect.java Wed Oct 09 17:38:58 2019 +0100 @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2019, 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 + * @library /test/lib + * @summary Test DatagramChannel local address after disconnect. + * @requires (os.family != "mac") + * @run testng/othervm AddressesAfterDisconnect + * @run testng/othervm -Djava.net.preferIPv6Addresses=true AddressesAfterDisconnect + * @run testng/othervm -Djava.net.preferIPv4Stack=true AddressesAfterDisconnect + */ + +import jdk.test.lib.net.IPSupport; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.net.StandardProtocolFamily; +import java.nio.channels.DatagramChannel; + +import org.testng.annotations.Test; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.assertFalse; + +public class AddressesAfterDisconnect { + + public static void main(String[] args) throws IOException { + new AddressesAfterDisconnect().execute(); + } + + @Test + public void execute() throws IOException { + IPSupport.throwSkippedExceptionIfNonOperational(); + boolean preferIPv6 = Boolean.getBoolean("java.net.preferIPv6Addresses"); + + // test with default protocol family + try (DatagramChannel dc = DatagramChannel.open()) { + System.out.println("Test with default"); + dc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); + test(dc); + test(dc); + } + + if (IPSupport.hasIPv6()) { + // test with IPv6 only + System.out.println("Test with IPv6 only"); + try (DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET6)) { + dc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); + test(dc); + test(dc); + } + } + + if (IPSupport.hasIPv4() && !preferIPv6) { + // test with IPv4 only + System.out.println("Test with IPv4 only"); + try (DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET)) { + dc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); + test(dc); + test(dc); + } + } + } + + /** + * Connect DatagramChannel to a server, write a datagram and disconnect. Invoke + * a second or subsequent time with the same DatagramChannel instance to check + * that disconnect works as expected. + */ + static void test(DatagramChannel dc) throws IOException { + SocketAddress local = dc.getLocalAddress(); + try (DatagramChannel server = DatagramChannel.open()) { + server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); + SocketAddress remote = server.getLocalAddress(); + dc.connect(remote); + assertTrue(dc.isConnected()); + // comment the following two lines on OS X to see JDK-8231259 + assertEquals(dc.getLocalAddress(), local, "local address after connect"); + assertEquals(dc.getRemoteAddress(), remote, "remote address after connect"); + dc.disconnect(); + assertFalse(dc.isConnected()); + assertEquals(dc.getLocalAddress(), local, "local address after disconnect"); + assertEquals(dc.getRemoteAddress(), null, "remote address after disconnect"); + } + } + +}