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; 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 * How often a STUN Binding request used for consent freshness check will be
* sent(value in milliseconds). * sent(value in milliseconds).

View File

@ -406,15 +406,44 @@ public abstract class AbstractTcpListener
protected void init() protected void init()
throws IOException 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) for (TransportAddress transportAddress : localAddresses)
{ {
ServerSocketChannel channel addressesToBind.add( new InetSocketAddress(
= MuxServerSocketChannelFactory 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( .openAndBindMuxServerSocketChannel(
/* properties */ null, /* properties */ null,
new InetSocketAddress( address,
transportAddress.getAddress(),
transportAddress.getPort()),
/* backlog */ 0, /* backlog */ 0,
new DatagramPacketFilter() new DatagramPacketFilter()
{ {
@ -431,13 +460,6 @@ public abstract class AbstractTcpListener
serverSocketChannels.add(channel); serverSocketChannels.add(channel);
} }
acceptThread = new AcceptThread();
acceptThread.start();
readThread = new ReadThread();
readThread.start();
}
/** /**
* Accepts a session. * Accepts a session.
* @param socket the {@link Socket} for the session. * @param socket the {@link Socket} for the session.

View File

@ -204,9 +204,25 @@ public abstract class AbstractUdpListener
*/ */
protected AbstractUdpListener(TransportAddress localAddress) protected AbstractUdpListener(TransportAddress localAddress)
throws IOException throws IOException
{
boolean bindWildcard = !StackProperties.getBoolean(
StackProperties.BIND_WILDCARD,
false);
if (bindWildcard)
{
this.localAddress = new TransportAddress(
(InetAddress) null,
localAddress.getPort(),
localAddress.getTransport()
);
}
else
{ {
this.localAddress = localAddress; this.localAddress = localAddress;
socket = new DatagramSocket(localAddress); }
socket = new DatagramSocket( localAddress );
int receiveBufferSize = StackProperties.getInt(SO_RCVBUF_PNAME, -1); int receiveBufferSize = StackProperties.getInt(SO_RCVBUF_PNAME, -1);
if (receiveBufferSize > 0) if (receiveBufferSize > 0)