fix: Match the base when finding a local canidate for a remote address (fixes #205). (#208)

* fix: Match the base when finding a local canidate for a remote address (fixes #205).
This commit is contained in:
bgrozev 2020-06-10 14:18:33 -05:00 committed by GitHub
parent 9a580a9401
commit ed63ae8e11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 9 deletions

View File

@ -1476,10 +1476,30 @@ public class Agent
* streams or <tt>null</tt> if it doesn't. * streams or <tt>null</tt> if it doesn't.
*/ */
public LocalCandidate findLocalCandidate(TransportAddress localAddress) public LocalCandidate findLocalCandidate(TransportAddress localAddress)
{
return findLocalCandidate(localAddress, null);
}
/**
* Returns the local <tt>LocalCandidate</tt> with the specified
* <tt>localAddress</tt> if it belongs to any of this {@link Agent}'s
* streams or <tt>null</tt> 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 <tt>LocalCandidate</tt> with the specified
* <tt>localAddress</tt> if it belongs to any of this {@link Agent}'s
* streams or <tt>null</tt> if it doesn't.
*/
public LocalCandidate findLocalCandidate(
TransportAddress localAddress,
LocalCandidate base)
{ {
for (IceMediaStream stream : mediaStreams.values()) for (IceMediaStream stream : mediaStreams.values())
{ {
LocalCandidate cnd = stream.findLocalCandidate(localAddress); LocalCandidate cnd = stream.findLocalCandidate(localAddress, base);
if (cnd != null) if (cnd != null)
{ {

View File

@ -919,11 +919,48 @@ public class Component
* if it doesn't. * if it doesn't.
*/ */
public LocalCandidate findLocalCandidate(TransportAddress localAddress) public LocalCandidate findLocalCandidate(TransportAddress localAddress)
{
return findLocalCandidate(localAddress, null);
}
/**
* Returns the local <tt>LocalCandidate</tt> with the specified
* <tt>localAddress</tt> if it belongs to this component or <tt>null</tt>
* 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 <tt>LocalCandidate</tt> with the specified
* <tt>localAddress</tt> if it belongs to this component or <tt>null</tt>
* if it doesn't.
*/
public LocalCandidate findLocalCandidate(TransportAddress localAddress, LocalCandidate base)
{ {
for (LocalCandidate localCnd : localCandidates) for (LocalCandidate localCnd : localCandidates)
{ {
if (localCnd.getTransportAddress().equals(localAddress)) 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 localCnd;
}
} }
return null; return null;

View File

@ -539,9 +539,12 @@ class ConnectivityCheckClient
Transport.TCP); Transport.TCP);
} }
LocalCandidate validLocalCandidate = null; // 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
validLocalCandidate = parentAgent.findLocalCandidate(mappedAddress); // the socket we received the response on.
LocalCandidate base = checkedPair.getLocalCandidate().getBase();
LocalCandidate validLocalCandidate
= parentAgent.findLocalCandidate(mappedAddress, base);
RemoteCandidate validRemoteCandidate = checkedPair.getRemoteCandidate(); RemoteCandidate validRemoteCandidate = checkedPair.getRemoteCandidate();

View File

@ -508,22 +508,26 @@ public class IceMediaStream
/** /**
* Returns the local <tt>LocalCandidate</tt> with the specified * Returns the local <tt>LocalCandidate</tt> with the specified
* <tt>localAddress</tt> if it belongs to any of this stream's components * <tt>localAddress</tt> if it belongs to any of this stream's components
* or <tt>null</tt> otherwise. * or <tt>null</tt> 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 localAddress the {@link TransportAddress} we are looking for.
* @param base an optional base to match.
* *
* @return the local <tt>LocalCandidate</tt> with the specified * @return the local <tt>LocalCandidate</tt> with the specified
* <tt>localAddress</tt> if it belongs to any of this stream's components * <tt>localAddress</tt> if it belongs to any of this stream's components
* or <tt>null</tt> otherwise. * or <tt>null</tt> 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 cnd;
}
} }
return null; return null;