feat: Support harvesting with ephemeral ports. (#250)
This commit is contained in:
parent
86f1eaede2
commit
a9ba80efb9
@ -500,6 +500,8 @@ public class Agent
|
|||||||
* Creates a new {@link Component} for the specified <tt>stream</tt> and
|
* Creates a new {@link Component} for the specified <tt>stream</tt> and
|
||||||
* allocates potentially all local candidates that should belong to it.
|
* allocates potentially all local candidates that should belong to it.
|
||||||
*
|
*
|
||||||
|
* If 0, 0, 0 are specified for preferred, min and max port, an ephemeral port will be used instead.
|
||||||
|
*
|
||||||
* @param stream the {@link IceMediaStream} that the new {@link Component}
|
* @param stream the {@link IceMediaStream} that the new {@link Component}
|
||||||
* should belong to.
|
* should belong to.
|
||||||
* @param preferredPort the port number that should be tried first when
|
* @param preferredPort the port number that should be tried first when
|
||||||
@ -557,6 +559,31 @@ public class Agent
|
|||||||
return component;
|
return component;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link Component} for the specified {@code stream} and allocates potentially all local
|
||||||
|
* candidates that should belong to it.
|
||||||
|
*
|
||||||
|
* If dynamic ports are enabled, an ephemeral port will be used.
|
||||||
|
*
|
||||||
|
* @param stream the {@link IceMediaStream} that the new {@link Component} should belong to.
|
||||||
|
* @param keepAliveStrategy the keep-alive strategy, which dictates which
|
||||||
|
* candidates pairs are going to be kept alive.
|
||||||
|
* @param useComponentSocket whether to use the component socket mode (in
|
||||||
|
* which case the socket is available through the {@link Component} directly),
|
||||||
|
* or not (in which case the socket is available through the selected {@link CandidatePair}).
|
||||||
|
*
|
||||||
|
* @return the newly created {@link Component} and with a list containing all and only local candidates.
|
||||||
|
* @throws IOException if an error occurs while the underlying resolver lib is using sockets.
|
||||||
|
*/
|
||||||
|
public Component createComponent(
|
||||||
|
IceMediaStream stream,
|
||||||
|
KeepAliveStrategy keepAliveStrategy,
|
||||||
|
boolean useComponentSocket)
|
||||||
|
throws IllegalArgumentException, IOException
|
||||||
|
{
|
||||||
|
return createComponent(stream, 0, 0, 0, keepAliveStrategy, useComponentSocket);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new {@link CandidatePair} instance from a
|
* Initializes a new {@link CandidatePair} instance from a
|
||||||
* {@link LocalCandidate} and a {@link RemoteCandidate}. The method
|
* {@link LocalCandidate} and a {@link RemoteCandidate}. The method
|
||||||
@ -595,6 +622,8 @@ public class Agent
|
|||||||
* you've registered all harvesters that you would want to use before
|
* you've registered all harvesters that you would want to use before
|
||||||
* calling it.
|
* calling it.
|
||||||
* </p>
|
* </p>
|
||||||
|
* If 0, 0, 0 are specified for preferred, min and max port, an ephemeral port will be used instead.
|
||||||
|
*
|
||||||
* @param component the <tt>Component</tt> that we'd like to gather
|
* @param component the <tt>Component</tt> that we'd like to gather
|
||||||
* candidates for.
|
* candidates for.
|
||||||
* @param preferredPort the port number that should be tried first when
|
* @param preferredPort the port number that should be tried first when
|
||||||
@ -622,9 +651,7 @@ public class Agent
|
|||||||
|
|
||||||
if (useDynamicPorts)
|
if (useDynamicPorts)
|
||||||
{
|
{
|
||||||
hostCandidateHarvester.harvest(
|
hostCandidateHarvester.harvest(component, preferredPort, minPort, maxPort, Transport.UDP);
|
||||||
component,
|
|
||||||
preferredPort, minPort, maxPort, Transport.UDP);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@ -276,6 +276,8 @@ public class HostCandidateHarvester
|
|||||||
* If that fails we will move through all ports between <tt>minPort</tt> and
|
* If that fails we will move through all ports between <tt>minPort</tt> and
|
||||||
* <tt>maxPort</tt> and give up if still can't find a free port.
|
* <tt>maxPort</tt> and give up if still can't find a free port.
|
||||||
*
|
*
|
||||||
|
* If 0, 0, 0 are specified for preferred, min and max port, an ephemeral port will be used instead.
|
||||||
|
*
|
||||||
* @param component the {@link Component} that we'd like to gather candidate
|
* @param component the {@link Component} that we'd like to gather candidate
|
||||||
* addresses for.
|
* addresses for.
|
||||||
* @param preferredPort the port number that should be tried first when
|
* @param preferredPort the port number that should be tried first when
|
||||||
@ -439,7 +441,7 @@ public class HostCandidateHarvester
|
|||||||
* <tt>org.ice4j.ice.harvest.BLOCKED_INTERFACES</tt> list. It returns
|
* <tt>org.ice4j.ice.harvest.BLOCKED_INTERFACES</tt> list. It returns
|
||||||
* <tt>false</tt> otherwise.
|
* <tt>false</tt> otherwise.
|
||||||
*/
|
*/
|
||||||
static boolean isInterfaceAllowed(NetworkInterface iface)
|
public static boolean isInterfaceAllowed(NetworkInterface iface)
|
||||||
{
|
{
|
||||||
if (iface == null)
|
if (iface == null)
|
||||||
throw new IllegalArgumentException("iface cannot be null");
|
throw new IllegalArgumentException("iface cannot be null");
|
||||||
@ -519,6 +521,8 @@ public class HostCandidateHarvester
|
|||||||
* <tt>localAddress</tt> and a port in the range specified by the
|
* <tt>localAddress</tt> and a port in the range specified by the
|
||||||
* <tt>minPort</tt> and <tt>maxPort</tt> parameters.
|
* <tt>minPort</tt> and <tt>maxPort</tt> parameters.
|
||||||
*
|
*
|
||||||
|
* If 0, 0, 0 are specified for preferred, min and max port, an ephemeral port will be used instead.
|
||||||
|
*
|
||||||
* @param laddr the address that we'd like to bind the socket on.
|
* @param laddr the address that we'd like to bind the socket on.
|
||||||
* @param preferredPort the port number that we should try to bind to first.
|
* @param preferredPort the port number that we should try to bind to first.
|
||||||
* @param minPort the port number where we should first try to bind before
|
* @param minPort the port number where we should first try to bind before
|
||||||
@ -545,7 +549,18 @@ public class HostCandidateHarvester
|
|||||||
BindException
|
BindException
|
||||||
{
|
{
|
||||||
// make sure port numbers are valid
|
// make sure port numbers are valid
|
||||||
this.checkPorts(preferredPort, minPort, maxPort);
|
boolean ephemeral = checkPorts(preferredPort, minPort, maxPort);
|
||||||
|
if (ephemeral)
|
||||||
|
{
|
||||||
|
ServerSocket socket = new ServerSocket();
|
||||||
|
socket.setReuseAddress(true);
|
||||||
|
socket.bind(new InetSocketAddress(laddr, 0));
|
||||||
|
if (logger.isLoggable(Level.FINEST))
|
||||||
|
{
|
||||||
|
logger.finest("Bound using an ephemeral port to " + socket.getLocalSocketAddress());
|
||||||
|
}
|
||||||
|
return new IceTcpServerSocketWrapper(new DelegatingServerSocket(socket), component);
|
||||||
|
}
|
||||||
|
|
||||||
int bindRetries = StackProperties.getInt(
|
int bindRetries = StackProperties.getInt(
|
||||||
StackProperties.BIND_RETRIES,
|
StackProperties.BIND_RETRIES,
|
||||||
@ -589,8 +604,7 @@ public class HostCandidateHarvester
|
|||||||
port = minPort;
|
port = minPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new BindException("Could not bind to any port between "
|
throw new BindException("Could not bind to any port between " + minPort + " and " + (port - 1));
|
||||||
+ minPort + " and " + (port - 1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -605,6 +619,8 @@ public class HostCandidateHarvester
|
|||||||
* start over again at <tt>minPort</tt> and keep going until we run out of
|
* start over again at <tt>minPort</tt> and keep going until we run out of
|
||||||
* retries.
|
* retries.
|
||||||
*
|
*
|
||||||
|
* If 0, 0, 0 are specified for preferred, min and max port, an ephemeral port will be used instead.
|
||||||
|
*
|
||||||
* @param laddr the address that we'd like to bind the socket on.
|
* @param laddr the address that we'd like to bind the socket on.
|
||||||
* @param preferredPort the port number that we should try to bind to first.
|
* @param preferredPort the port number that we should try to bind to first.
|
||||||
* @param minPort the port number where we should first try to bind before
|
* @param minPort the port number where we should first try to bind before
|
||||||
@ -632,7 +648,16 @@ public class HostCandidateHarvester
|
|||||||
BindException
|
BindException
|
||||||
{
|
{
|
||||||
// make sure port numbers are valid.
|
// make sure port numbers are valid.
|
||||||
this.checkPorts(preferredPort, minPort, maxPort);
|
boolean ephemeral = checkPorts(preferredPort, minPort, maxPort);
|
||||||
|
if (ephemeral)
|
||||||
|
{
|
||||||
|
DatagramSocket socket = new MultiplexingDatagramSocket(0, laddr);
|
||||||
|
if (logger.isLoggable(Level.FINEST))
|
||||||
|
{
|
||||||
|
logger.finest("Bound using ephemeral port to " + socket.getLocalSocketAddress());
|
||||||
|
}
|
||||||
|
return new IceUdpSocketWrapper(socket);
|
||||||
|
}
|
||||||
|
|
||||||
int bindRetries = StackProperties.getInt(
|
int bindRetries = StackProperties.getInt(
|
||||||
StackProperties.BIND_RETRIES,
|
StackProperties.BIND_RETRIES,
|
||||||
@ -694,8 +719,10 @@ public class HostCandidateHarvester
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the different ports are correctly set. If not, throws an
|
* Checks if the different ports are correctly set. If not, throws {@link IllegalArgumentException}. The
|
||||||
* IllegalArgumentException.
|
* special values 0, 0, 0 for the parameters are interpreted as "use an ephemeral port".
|
||||||
|
*
|
||||||
|
* @return {@code true} if the params specify that an ephemeral port should be used, and {@code false} otherwise.
|
||||||
*
|
*
|
||||||
* @param preferredPort the port number that we should try to bind to first.
|
* @param preferredPort the port number that we should try to bind to first.
|
||||||
* @param minPort the port number where we should first try to bind before
|
* @param minPort the port number where we should first try to bind before
|
||||||
@ -707,9 +734,14 @@ public class HostCandidateHarvester
|
|||||||
* <tt>maxPort</tt> is not a valid port number or if <tt>minPort</tt> is
|
* <tt>maxPort</tt> is not a valid port number or if <tt>minPort</tt> is
|
||||||
* greater than <tt>maxPort</tt>.
|
* greater than <tt>maxPort</tt>.
|
||||||
*/
|
*/
|
||||||
private void checkPorts(int preferredPort, int minPort, int maxPort)
|
private static boolean checkPorts(int preferredPort, int minPort, int maxPort)
|
||||||
throws IllegalArgumentException
|
throws IllegalArgumentException
|
||||||
{
|
{
|
||||||
|
if (preferredPort == 0 && minPort == 0 && maxPort == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// make sure port numbers are valid
|
// make sure port numbers are valid
|
||||||
if (!NetworkUtils.isValidPortNumber(minPort)
|
if (!NetworkUtils.isValidPortNumber(minPort)
|
||||||
|| !NetworkUtils.isValidPortNumber(maxPort))
|
|| !NetworkUtils.isValidPortNumber(maxPort))
|
||||||
@ -734,6 +766,8 @@ public class HostCandidateHarvester
|
|||||||
+") must be between minPort (" + minPort
|
+") must be between minPort (" + minPort
|
||||||
+ ") and maxPort (" + maxPort + ")");
|
+ ") and maxPort (" + maxPort + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user