Skip to content

Commit

Permalink
Correctly detect InternetProtocolFamily when EpollDatagramChannel is …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
normanmaurer committed May 26, 2019
1 parent 70731bf commit e17ce93
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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;

Expand All @@ -68,38 +67,28 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements
* on the Operation Systems default which will be chosen.
*/
public EpollDatagramChannel() {
this(null);
this((InternetProtocolFamily) null);
}

/**
* Create a new instance using the given {@link InternetProtocolFamily}. If {@code null} is used it will depend
* on the Operation Systems default which will be chosen.
*/
public EpollDatagramChannel(InternetProtocolFamily family) {
super(family == null ?
this(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;
}

/**
* Create a new instance which selects the {@link InternetProtocolFamily} to use depending
* on the Operation Systems default which will be chosen.
*/
public EpollDatagramChannel(int fd) {
this(new LinuxSocket(fd), null);
this(new LinuxSocket(fd));
}

private EpollDatagramChannel(LinuxSocket fd, InternetProtocolFamily family) {
private EpollDatagramChannel(LinuxSocket fd) {
super(null, fd, true);
this.family = internetProtocolFamily(family);
config = new EpollDatagramChannelConfig(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public NetworkInterface getNetworkInterface() {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -58,10 +62,10 @@ void setInterface(InetAddress address) throws IOException {
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));
Expand Down

0 comments on commit e17ce93

Please sign in to comment.