diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java
index b4efb31..432ecd0 100644
--- a/src/main/java/org/ice4j/ice/Agent.java
+++ b/src/main/java/org/ice4j/ice/Agent.java
@@ -1476,10 +1476,30 @@ public class Agent
* streams or null if it doesn't.
*/
public LocalCandidate findLocalCandidate(TransportAddress localAddress)
+ {
+ return findLocalCandidate(localAddress, null);
+ }
+
+ /**
+ * Returns the local LocalCandidate with the specified
+ * localAddress if it belongs to any of this {@link Agent}'s
+ * streams or null if it doesn't. If {@code base} is also specified,
+ * tries to find a candidate whose base matches {@code base}.
+ *
+ * @param localAddress the {@link TransportAddress} we are looking for.
+ * @param base an optional base to match.
+ *
+ * @return the local LocalCandidate with the specified
+ * localAddress if it belongs to any of this {@link Agent}'s
+ * streams or null if it doesn't.
+ */
+ public LocalCandidate findLocalCandidate(
+ TransportAddress localAddress,
+ LocalCandidate base)
{
for (IceMediaStream stream : mediaStreams.values())
{
- LocalCandidate cnd = stream.findLocalCandidate(localAddress);
+ LocalCandidate cnd = stream.findLocalCandidate(localAddress, base);
if (cnd != null)
{
diff --git a/src/main/java/org/ice4j/ice/Component.java b/src/main/java/org/ice4j/ice/Component.java
index 72e65f8..a91d371 100644
--- a/src/main/java/org/ice4j/ice/Component.java
+++ b/src/main/java/org/ice4j/ice/Component.java
@@ -919,11 +919,48 @@ public class Component
* if it doesn't.
*/
public LocalCandidate findLocalCandidate(TransportAddress localAddress)
+ {
+ return findLocalCandidate(localAddress, null);
+ }
+
+ /**
+ * Returns the local LocalCandidate with the specified
+ * localAddress if it belongs to this component or null
+ * if it doesn't. If {@code base} is also specified, tries to find a
+ * candidate whose base matches {@code base}.
+ *
+ * @param localAddress the {@link TransportAddress} we are looking for.
+ * @param base an optional base to match.
+ *
+ * @return the local LocalCandidate with the specified
+ * localAddress if it belongs to this component or null
+ * if it doesn't.
+ */
+ public LocalCandidate findLocalCandidate(TransportAddress localAddress, LocalCandidate base)
{
for (LocalCandidate localCnd : localCandidates)
{
if (localCnd.getTransportAddress().equals(localAddress))
+ {
+ if (base == null || base.equals(localCnd.getBase()))
+ {
+ return localCnd;
+ }
+ }
+ }
+ // In case the above loop failed to find a result because `base` was
+ // specified, fallback to the original behavior and return the first
+ // candidate matching `localAddress` regardless of `base`.
+ for (LocalCandidate localCnd : localCandidates)
+ {
+ if (localCnd.getTransportAddress().equals(localAddress))
+ {
+ logger.warn("Returning a candidate matching the address, "
+ + "while no candidates match both address ("
+ + localAddress + ") and base (" + base +"): " + localCnd
+ + " with base " + localCnd.getBase());
return localCnd;
+ }
}
return null;
diff --git a/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java b/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java
index 6a34d35..4535c1b 100644
--- a/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java
+++ b/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java
@@ -539,9 +539,12 @@ class ConnectivityCheckClient
Transport.TCP);
}
- LocalCandidate validLocalCandidate = null;
-
- validLocalCandidate = parentAgent.findLocalCandidate(mappedAddress);
+ // In some situations we may have more than one local candidate matching
+ // the mapped address. In this case we want to find the that matches
+ // the socket we received the response on.
+ LocalCandidate base = checkedPair.getLocalCandidate().getBase();
+ LocalCandidate validLocalCandidate
+ = parentAgent.findLocalCandidate(mappedAddress, base);
RemoteCandidate validRemoteCandidate = checkedPair.getRemoteCandidate();
diff --git a/src/main/java/org/ice4j/ice/IceMediaStream.java b/src/main/java/org/ice4j/ice/IceMediaStream.java
index a223c5a..be2e8a6 100644
--- a/src/main/java/org/ice4j/ice/IceMediaStream.java
+++ b/src/main/java/org/ice4j/ice/IceMediaStream.java
@@ -508,22 +508,26 @@ public class IceMediaStream
/**
* Returns the local LocalCandidate with the specified
* localAddress if it belongs to any of this stream's components
- * or null otherwise.
+ * or null otherwise. If {@code base} is also specified, tries to
+ * find a candidate whose base matches {@code base}.
*
* @param localAddress the {@link TransportAddress} we are looking for.
+ * @param base an optional base to match.
*
* @return the local LocalCandidate with the specified
* localAddress if it belongs to any of this stream's components
* or null otherwise.
*/
- public LocalCandidate findLocalCandidate(TransportAddress localAddress)
+ public LocalCandidate findLocalCandidate(TransportAddress localAddress, LocalCandidate base)
{
- for( Component cmp : components.values())
+ for (Component cmp : components.values())
{
- LocalCandidate cnd = cmp.findLocalCandidate(localAddress);
+ LocalCandidate cnd = cmp.findLocalCandidate(localAddress, base);
- if(cnd != null)
+ if (cnd != null)
+ {
return cnd;
+ }
}
return null;