Allow bind to wildcard (any) local address.

This commit allows the project to bind to any local address, by setting
a property.
This commit is contained in:
Guus der Kinderen 2017-11-14 14:37:56 +01:00 committed by bgrozev
parent a2e5ca1d4c
commit 48956f82ba
3 changed files with 63 additions and 16 deletions

View File

@ -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).

View File

@ -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<InetSocketAddress> 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);
}
/**

View File

@ -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)