From a9ba80efb9870713e5e1d3a842de7f5bb44aded8 Mon Sep 17 00:00:00 2001
From: bgrozev
Date: Wed, 5 Jan 2022 13:46:58 -0700
Subject: [PATCH] feat: Support harvesting with ephemeral ports. (#250)
---
src/main/java/org/ice4j/ice/Agent.java | 33 ++++++++++--
.../ice/harvest/HostCandidateHarvester.java | 50 ++++++++++++++++---
2 files changed, 72 insertions(+), 11 deletions(-)
diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java
index cb5fa10..0a2273f 100644
--- a/src/main/java/org/ice4j/ice/Agent.java
+++ b/src/main/java/org/ice4j/ice/Agent.java
@@ -500,6 +500,8 @@ public class Agent
* Creates a new {@link Component} for the specified stream and
* 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}
* should belong to.
* @param preferredPort the port number that should be tried first when
@@ -557,6 +559,31 @@ public class Agent
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
* {@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
* calling it.
*
+ * If 0, 0, 0 are specified for preferred, min and max port, an ephemeral port will be used instead.
+ *
* @param component the Component that we'd like to gather
* candidates for.
* @param preferredPort the port number that should be tried first when
@@ -622,9 +651,7 @@ public class Agent
if (useDynamicPorts)
{
- hostCandidateHarvester.harvest(
- component,
- preferredPort, minPort, maxPort, Transport.UDP);
+ hostCandidateHarvester.harvest(component, preferredPort, minPort, maxPort, Transport.UDP);
}
else
{
diff --git a/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java b/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java
index 8f3b052..170d221 100644
--- a/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java
+++ b/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java
@@ -276,6 +276,8 @@ public class HostCandidateHarvester
* If that fails we will move through all ports between minPort and
* maxPort 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
* addresses for.
* @param preferredPort the port number that should be tried first when
@@ -439,7 +441,7 @@ public class HostCandidateHarvester
* org.ice4j.ice.harvest.BLOCKED_INTERFACES list. It returns
* false otherwise.
*/
- static boolean isInterfaceAllowed(NetworkInterface iface)
+ public static boolean isInterfaceAllowed(NetworkInterface iface)
{
if (iface == null)
throw new IllegalArgumentException("iface cannot be null");
@@ -519,6 +521,8 @@ public class HostCandidateHarvester
* localAddress and a port in the range specified by the
* minPort and maxPort 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 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
@@ -545,7 +549,18 @@ public class HostCandidateHarvester
BindException
{
// 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(
StackProperties.BIND_RETRIES,
@@ -589,8 +604,7 @@ public class HostCandidateHarvester
port = minPort;
}
- throw new BindException("Could not bind to any port between "
- + minPort + " and " + (port - 1));
+ throw new BindException("Could not bind to any port between " + minPort + " and " + (port - 1));
}
/**
@@ -605,6 +619,8 @@ public class HostCandidateHarvester
* start over again at minPort and keep going until we run out of
* 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 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
@@ -632,7 +648,16 @@ public class HostCandidateHarvester
BindException
{
// 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(
StackProperties.BIND_RETRIES,
@@ -694,8 +719,10 @@ public class HostCandidateHarvester
}
/**
- * Checks if the different ports are correctly set. If not, throws an
- * IllegalArgumentException.
+ * Checks if the different ports are correctly set. If not, throws {@link IllegalArgumentException}. The
+ * 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 minPort the port number where we should first try to bind before
@@ -707,9 +734,14 @@ public class HostCandidateHarvester
* maxPort is not a valid port number or if minPort is
* greater than maxPort.
*/
- private void checkPorts(int preferredPort, int minPort, int maxPort)
+ private static boolean checkPorts(int preferredPort, int minPort, int maxPort)
throws IllegalArgumentException
{
+ if (preferredPort == 0 && minPort == 0 && maxPort == 0)
+ {
+ return true;
+ }
+
// make sure port numbers are valid
if (!NetworkUtils.isValidPortNumber(minPort)
|| !NetworkUtils.isValidPortNumber(maxPort))
@@ -734,6 +766,8 @@ public class HostCandidateHarvester
+") must be between minPort (" + minPort
+ ") and maxPort (" + maxPort + ")");
}
+
+ return false;
}
/**