Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ public void close() throws IOException {
socket.setSendBufferSize(this.size());
socket.setReceiveBufferSize(64 * 1024);
socket.setBroadcast(true);
socket.setReuseAddress(true);
if (multicast != null) {
((MulticastSocket)socket).setLoopbackMode(false);
}

if (multicast == null) {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.InetSocketAddress;
import java.net.InterfaceAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Logger;

import org.apache.cxf.Bus;
Expand Down Expand Up @@ -172,8 +168,12 @@ protected void activate() {
socket.setReceiveBufferSize(64 * 1024);
socket.setSendBufferSize(64 * 1024);
socket.setTimeToLive(1);
socket.setLoopbackMode(false);
socket.bind(new InetSocketAddress(isa.getPort()));
socket.setNetworkInterface(findNetworkInterface());
NetworkInterface ni = findNetworkInterface();
if (ni != null) {
socket.setNetworkInterface(ni);
}
socket.joinGroup(isa.getAddress());
mcast = socket;
queue.execute(new MCastListener());
Expand All @@ -194,31 +194,15 @@ protected void activate() {
throw new RuntimeException(ex);
}
}

private NetworkInterface findNetworkInterface() throws SocketException {
String name = (String)this.getEndpointInfo().getProperty(UDPDestination.NETWORK_INTERFACE);
//Allow configuring a default interface, but do not try to guess.
//Instead delegate this to the OS routing table.
String name = (String) this.getEndpointInfo().getProperty(UDPDestination.NETWORK_INTERFACE);
NetworkInterface ret = null;
if (!StringUtils.isEmpty(name)) {
ret = NetworkInterface.getByName(name);
}
if (ret == null) {
Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
List<NetworkInterface> possibles = new ArrayList<NetworkInterface>();
while (ifcs.hasMoreElements()) {
NetworkInterface ni = ifcs.nextElement();
if (ni.supportsMulticast()
&& ni.isUp()) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
if (ia.getAddress() instanceof java.net.Inet4Address
&& !ia.getAddress().isLoopbackAddress()
&& !ni.getDisplayName().startsWith("vnic")) {
possibles.add(ni);
}
}
}
}
ret = possibles.isEmpty() ? null : possibles.get(possibles.size() - 1);

}
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebService;
Expand Down Expand Up @@ -59,25 +56,6 @@
public final class WSDiscoveryClientTest {
public static final String PORT = TestUtil.getPortNumber(WSDiscoveryClientTest.class);

static NetworkInterface findIpv4Interface() throws Exception {
Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
List<NetworkInterface> possibles = new ArrayList<NetworkInterface>();
while (ifcs.hasMoreElements()) {
NetworkInterface ni = ifcs.nextElement();
if (ni.supportsMulticast()
&& ni.isUp()) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
if (ia.getAddress() instanceof java.net.Inet4Address
&& !ia.getAddress().isLoopbackAddress()
&& !ni.getDisplayName().startsWith("vnic")) {
possibles.add(ni);
}
}
}
}
return possibles.isEmpty() ? null : possibles.get(possibles.size() - 1);
}

@Test
public void testMultiResponses() throws Exception {
// Disable the test on Redhat Enterprise Linux which doesn't enable the UDP broadcast by default
Expand All @@ -91,7 +69,7 @@ public void testMultiResponses() throws Exception {
int count = 0;
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (!networkInterface.isUp() || networkInterface.isLoopback()) {
if (!networkInterface.isUp()) {
continue;
}
count++;
Expand All @@ -109,7 +87,8 @@ public void run() {
InetAddress address = InetAddress.getByName("239.255.255.250");
MulticastSocket s = new MulticastSocket(Integer.parseInt(PORT));
s.setBroadcast(true);
s.setNetworkInterface(findIpv4Interface());
s.setLoopbackMode(false);
s.setReuseAddress(true);
s.joinGroup(address);
s.setReceiveBufferSize(64 * 1024);
s.setSoTimeout(5000);
Expand Down