Use StandardSocketOptions#IP_MULTICAST_IF as default source when joing multicast groups (#11585)

Motivation:

We should use StandardSocketOptions#IP_MULTICAST_IF as default source when joing multicast groups and only try to use the localAddress if this returns null.

Modifications:

First check if StandardSocketOptions#IP_MULTICAST_IF was set and if so use the network interface when joining mulicast groups

Result:

Fixes https://github.com/netty/netty/issues/11541
This commit is contained in:
Norman Maurer 2021-08-18 12:11:48 +02:00
parent 8e4465d14c
commit 766fefa0f9
3 changed files with 15 additions and 10 deletions

View File

@ -144,9 +144,11 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements
@Override
public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise promise) {
try {
return joinGroup(
multicastAddress,
NetworkInterface.getByInetAddress(localAddress().getAddress()), null, promise);
NetworkInterface iface = config().getNetworkInterface();
if (iface == null) {
iface = NetworkInterface.getByInetAddress(localAddress().getAddress());
}
return joinGroup(multicastAddress, iface, null, promise);
} catch (IOException e) {
promise.setFailure(e);
}

View File

@ -101,10 +101,11 @@ public final class KQueueDatagramChannel extends AbstractKQueueDatagramChannel i
@Override
public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise promise) {
try {
return joinGroup(
multicastAddress,
NetworkInterface.getByInetAddress(localAddress().getAddress()),
null, promise);
NetworkInterface iface = config().getNetworkInterface();
if (iface == null) {
iface = NetworkInterface.getByInetAddress(localAddress().getAddress());
}
return joinGroup(multicastAddress, iface, null, promise);
} catch (SocketException e) {
promise.setFailure(e);
}

View File

@ -359,10 +359,12 @@ public final class NioDatagramChannel
@Override
public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise promise) {
try {
NetworkInterface iface = config.getNetworkInterface();
if (iface == null) {
iface = NetworkInterface.getByInetAddress(localAddress().getAddress());
}
return joinGroup(
multicastAddress,
NetworkInterface.getByInetAddress(localAddress().getAddress()),
null, promise);
multicastAddress, iface, null, promise);
} catch (SocketException e) {
promise.setFailure(e);
}