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
This commit is contained in:
Norman Maurer 2020-09-09 10:44:46 +02:00
parent 319a4bc3ba
commit 6c5a6dba46
3 changed files with 21 additions and 3 deletions

View File

@ -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();

View File

@ -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);

View File

@ -45,7 +45,7 @@ final class LinuxSocket extends Socket {
super(fd);
}
private InternetProtocolFamily family() {
InternetProtocolFamily family() {
return ipv6 ? InternetProtocolFamily.IPv6 : InternetProtocolFamily.IPv4;
}