Correctly detect InternetProtocolFamily when EpollDatagramChannel is created with existing FileDescriptor (#9185)

Motivation:

When EpollDatagramChannel is created with an existing FileDescriptor we should detect the correct InternetProtocolFamily.

Modifications:

Obtain the InternetProtocolFamily from the given FD

Result:

Use correct InternetProtocolFamily when EpollDatagramChannel is created via existing FileDescriptor
This commit is contained in:
Norman Maurer 2019-05-26 20:22:55 +02:00
parent 33d1a91083
commit f966a0a041
3 changed files with 13 additions and 20 deletions

View File

@ -61,7 +61,6 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements
StringUtil.simpleClassName(InetSocketAddress.class) + ">, " +
StringUtil.simpleClassName(ByteBuf.class) + ')';
final InternetProtocolFamily family;
private final EpollDatagramChannelConfig config;
private volatile boolean connected;
@ -70,7 +69,7 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements
* on the Operation Systems default which will be chosen.
*/
public EpollDatagramChannel(EventLoop eventLoop) {
this(eventLoop, null);
this(eventLoop, (InternetProtocolFamily) null);
}
/**
@ -78,17 +77,8 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements
* on the Operation Systems default which will be chosen.
*/
public EpollDatagramChannel(EventLoop eventLoop, InternetProtocolFamily family) {
super(eventLoop, family == null ?
this(eventLoop, family == null ?
newSocketDgram(Socket.isIPv6Preferred()) : newSocketDgram(family == InternetProtocolFamily.IPv6));
this.family = internetProtocolFamily(family);
config = new EpollDatagramChannelConfig(this);
}
private static InternetProtocolFamily internetProtocolFamily(InternetProtocolFamily family) {
if (family == null) {
return Socket.isIPv6Preferred() ? InternetProtocolFamily.IPv6 : InternetProtocolFamily.IPv4;
}
return family;
}
/**
@ -96,12 +86,11 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements
* on the Operation Systems default which will be chosen.
*/
public EpollDatagramChannel(EventLoop eventLoop, int fd) {
this(eventLoop, new LinuxSocket(fd), null);
this(eventLoop, new LinuxSocket(fd));
}
private EpollDatagramChannel(EventLoop eventLoop, LinuxSocket fd, InternetProtocolFamily family) {
private EpollDatagramChannel(EventLoop eventLoop, LinuxSocket fd) {
super(null, eventLoop, fd, true);
this.family = internetProtocolFamily(family);
config = new EpollDatagramChannelConfig(this);
}

View File

@ -367,7 +367,7 @@ public final class EpollDatagramChannelConfig extends EpollChannelConfig impleme
public EpollDatagramChannelConfig setNetworkInterface(NetworkInterface networkInterface) {
try {
EpollDatagramChannel datagramChannel = (EpollDatagramChannel) channel;
datagramChannel.socket.setNetworkInterface(networkInterface, datagramChannel.family);
datagramChannel.socket.setNetworkInterface(networkInterface);
return this;
} catch (IOException e) {
throw new ChannelException(e);

View File

@ -44,6 +44,10 @@ final class LinuxSocket extends Socket {
super(fd);
}
private InternetProtocolFamily family() {
return ipv6 ? InternetProtocolFamily.IPv6 : InternetProtocolFamily.IPv4;
}
int sendmmsg(NativeDatagramPacketArray.NativeDatagramPacket[] msgs,
int offset, int len) throws IOException {
return Native.sendmmsg(intValue(), ipv6, msgs, offset, len);
@ -58,10 +62,10 @@ final class LinuxSocket extends Socket {
setInterface(intValue(), ipv6, a.address(), a.scopeId(), interfaceIndex(address));
}
void setNetworkInterface(NetworkInterface netInterface, InternetProtocolFamily family) throws IOException {
InetAddress address = deriveInetAddress(netInterface, family == InternetProtocolFamily.IPv6);
if (address.equals(family == InternetProtocolFamily.IPv4 ? INET_ANY : INET6_ANY)) {
throw new IOException("NetworkInterface does not support " + family);
void setNetworkInterface(NetworkInterface netInterface) throws IOException {
InetAddress address = deriveInetAddress(netInterface, family() == InternetProtocolFamily.IPv6);
if (address.equals(family() == InternetProtocolFamily.IPv4 ? INET_ANY : INET6_ANY)) {
throw new IOException("NetworkInterface does not support " + family());
}
final NativeInetAddress nativeAddress = NativeInetAddress.newInstance(address);
setInterface(intValue(), ipv6, nativeAddress.address(), nativeAddress.scopeId(), interfaceIndex(netInterface));