diff --git a/src/main/java/org/ice4j/StackProperties.java b/src/main/java/org/ice4j/StackProperties.java index 7e1b622..b533aaa 100644 --- a/src/main/java/org/ice4j/StackProperties.java +++ b/src/main/java/org/ice4j/StackProperties.java @@ -48,6 +48,15 @@ public class StackProperties */ public static final int BIND_RETRIES_DEFAULT_VALUE = 50; + /** + * The name of the property that tells if we should bind to the + * wildcard address instead of the (usually more specific) harvest candidate + * addresses. + * + * The wildcard is a special local IP address. It usually means "any". + */ + public static final String BIND_WILDCARD = "org.ice4j.BIND_WILDCARD"; + /** * How often a STUN Binding request used for consent freshness check will be * sent(value in milliseconds). diff --git a/src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java b/src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java index 2a78b86..49520c8 100644 --- a/src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java +++ b/src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java @@ -406,15 +406,44 @@ public abstract class AbstractTcpListener protected void init() throws IOException { + boolean bindWildcard = !StackProperties.getBoolean( + StackProperties.BIND_WILDCARD, + false); + + // Use a set to filter out any duplicates. + Set addressesToBind = new HashSet<>(); + for (TransportAddress transportAddress : localAddresses) { - ServerSocketChannel channel - = MuxServerSocketChannelFactory - .openAndBindMuxServerSocketChannel( + addressesToBind.add( new InetSocketAddress( + bindWildcard ? null : transportAddress.getAddress(), + transportAddress.getPort() + ) ); + } + + for (InetSocketAddress addressToBind : addressesToBind ) + { + addSocketChannel( addressToBind ); + } + + acceptThread = new AcceptThread(); + acceptThread.start(); + + readThread = new ReadThread(); + readThread.start(); + } + + /** + * Initializes one of the channels in {@link #serverSocketChannels}, + * @throws IOException if an I/O error occurs + */ + private void addSocketChannel(InetSocketAddress address) + throws IOException + { + ServerSocketChannel channel = MuxServerSocketChannelFactory + .openAndBindMuxServerSocketChannel( /* properties */ null, - new InetSocketAddress( - transportAddress.getAddress(), - transportAddress.getPort()), + address, /* backlog */ 0, new DatagramPacketFilter() { @@ -428,14 +457,7 @@ public abstract class AbstractTcpListener } }); - serverSocketChannels.add(channel); - } - - acceptThread = new AcceptThread(); - acceptThread.start(); - - readThread = new ReadThread(); - readThread.start(); + serverSocketChannels.add(channel); } /** diff --git a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java index daf9049..ad2cd08 100644 --- a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java +++ b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java @@ -205,8 +205,24 @@ public abstract class AbstractUdpListener protected AbstractUdpListener(TransportAddress localAddress) throws IOException { - this.localAddress = localAddress; - socket = new DatagramSocket(localAddress); + boolean bindWildcard = !StackProperties.getBoolean( + StackProperties.BIND_WILDCARD, + false); + + if (bindWildcard) + { + this.localAddress = new TransportAddress( + (InetAddress) null, + localAddress.getPort(), + localAddress.getTransport() + ); + } + else + { + this.localAddress = localAddress; + } + + socket = new DatagramSocket( localAddress ); int receiveBufferSize = StackProperties.getInt(SO_RCVBUF_PNAME, -1); if (receiveBufferSize > 0)