From 6c5a6dba468b385448fa2fdbed9e006c9254b25f Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 9 Sep 2020 10:44:46 +0200 Subject: [PATCH] Fix regression when trying to bind an EpollDatagramChannel with port (#10552) only Motivation: 4b7dba1 introduced a change which was not 100 % complete and so introduce a regression when a user specified to use InetProtocolFamily.IPv4 and trying to bind to a port (without specify the ip). Modifications: - Fix regression by respect the InetProtocolFamily - Add unit test Result: Fix regression when binding to port explicit --- .../transport/socket/DatagramUnicastTest.java | 16 ++++++++++++++++ .../channel/epoll/EpollDatagramChannel.java | 6 ++++-- .../java/io/netty/channel/epoll/LinuxSocket.java | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java b/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java index afbc664760..d79ae6caa1 100644 --- a/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java +++ b/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java @@ -21,6 +21,7 @@ import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; @@ -50,6 +51,21 @@ public class DatagramUnicastTest extends AbstractDatagramTest { NONE, DUP, SLICE, READ_ONLY } + @Test + public void testBindWithPortOnly() throws Throwable { + run(); + } + + public void testBindWithPortOnly(Bootstrap sb, Bootstrap cb) throws Throwable { + Channel channel = null; + try { + cb.handler(new ChannelHandlerAdapter() { }); + channel = cb.bind(0).sync().channel(); + } finally { + closeChannel(channel); + } + } + @Test public void testSimpleSendDirectByteBuf() throws Throwable { run(); diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java index 0c0d0f81d5..f355770edf 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java @@ -276,8 +276,10 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements if (localAddress instanceof InetSocketAddress) { InetSocketAddress socketAddress = (InetSocketAddress) localAddress; if (socketAddress.getAddress().isAnyLocalAddress() && - socketAddress.getAddress() instanceof Inet4Address && Socket.isIPv6Preferred()) { - localAddress = new InetSocketAddress(LinuxSocket.INET6_ANY, socketAddress.getPort()); + socketAddress.getAddress() instanceof Inet4Address) { + if (socket.family() == InternetProtocolFamily.IPv6) { + localAddress = new InetSocketAddress(LinuxSocket.INET6_ANY, socketAddress.getPort()); + } } } super.doBind(localAddress); diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/LinuxSocket.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/LinuxSocket.java index db2d2469c8..96d3da441e 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/LinuxSocket.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/LinuxSocket.java @@ -45,7 +45,7 @@ final class LinuxSocket extends Socket { super(fd); } - private InternetProtocolFamily family() { + InternetProtocolFamily family() { return ipv6 ? InternetProtocolFamily.IPv6 : InternetProtocolFamily.IPv4; }