From 169e56c22f06f2a9d85df555289483088150bec5 Mon Sep 17 00:00:00 2001 From: seb Date: Thu, 6 Oct 2011 14:57:05 +0000 Subject: [PATCH] Fixes two deadlocks. Also do not answer to a Google's binding request if we do not know yet the local/remote ufrag (this case can happen if we receive binding checks before we start ICE connectivity checks) because it will in certain circumstances choose a different active candidates than the remote peer.. --- src/org/ice4j/ice/Agent.java | 20 ++++++- src/org/ice4j/ice/Candidate.java | 4 +- src/org/ice4j/ice/CandidatePair.java | 27 +++++++++ .../ice4j/ice/ConnectivityCheckClient.java | 44 +++++++++++--- .../ice4j/ice/ConnectivityCheckServer.java | 12 +++- src/org/ice4j/ice/DefaultNominator.java | 59 +++++++++++-------- src/org/ice4j/stack/NetAccessManager.java | 2 +- 7 files changed, 130 insertions(+), 38 deletions(-) diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index e9bb9fa..9dedf26 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -462,6 +462,8 @@ public class Agent //trigger a check for those candidate pairs. if(this.preDiscoveredPairsQueue.size() > 0) { + logger.info("Trigger checks for pairs that were received " + + "before running state"); Iterator it = preDiscoveredPairsQueue.iterator(); while(it.hasNext()) @@ -1256,6 +1258,8 @@ public class Agent return; } + useCandidate = pair.useCandidateReceived(); + remoteCandidate = new RemoteCandidate( remoteAddress, parentComponent, CandidateType.PEER_REFLEXIVE_CANDIDATE, @@ -1277,6 +1281,9 @@ public class Agent CandidatePair triggeredPair = new CandidatePair(localCandidate, remoteCandidate); + + logger.info("set use-candidate " + useCandidate + " for pair " + + triggeredPair.toShortString()); if(useCandidate) { triggeredPair.setUseCandidateReceived(); @@ -1288,6 +1295,9 @@ public class Agent { //we are started, which means we have the remote candidates //so it's now safe to go and see whether this is a new PR cand. + logger.info("Receive check from " + + triggeredPair.toShortString() + + "triggered a check"); triggerCheck(triggeredPair); } else @@ -1323,8 +1333,7 @@ public class Agent { //if the incoming request contained a USE-CANDIDATE attribute then //make sure we don't lose this piece of info. - if (triggerPair.useCandidateReceived() || - compatibilityMode == CompatibilityMode.GTALK) + if (triggerPair.useCandidateReceived()) knownPair.setUseCandidateReceived(); triggerPair = knownPair; @@ -1337,6 +1346,7 @@ public class Agent //7.2.1.5. Updating the Nominated Flag if (!isControlling() && triggerPair.useCandidateReceived()) { + logger.info("update nominated flag"); // If the Binding request received by the agent had the // USE-CANDIDATE attribute set, and the agent is in the // controlled role, the agent looks at the state of the @@ -1354,6 +1364,12 @@ public class Agent //ICE processing state. checkListStatesUpdated(); } + else if(!isControlling()) + { + //next time we will see a request it will be considered as + // having USE-CANDIDATE + triggerPair.setUseCandidateReceived(); + } return; } diff --git a/src/org/ice4j/ice/Candidate.java b/src/org/ice4j/ice/Candidate.java index 18c3962..cd7d5d9 100644 --- a/src/org/ice4j/ice/Candidate.java +++ b/src/org/ice4j/ice/Candidate.java @@ -381,7 +381,7 @@ public abstract class Candidate */ if(candidateType == CandidateType.HOST_CANDIDATE) { - priority += 1 * 1000 - + priority += 0.95 * 1000 - (this.getBase().getTransport() == Transport.TCP ? 200 : 0); } else if(candidateType == CandidateType.PEER_REFLEXIVE_CANDIDATE) @@ -404,7 +404,7 @@ public abstract class Candidate // IPv6 has better priority than IPv4 if(addr instanceof Inet6Address) - priority += 10; + priority += 50; return priority; } diff --git a/src/org/ice4j/ice/CandidatePair.java b/src/org/ice4j/ice/CandidatePair.java index a0c715c..f9e8569 100644 --- a/src/org/ice4j/ice/CandidatePair.java +++ b/src/org/ice4j/ice/CandidatePair.java @@ -45,6 +45,13 @@ public class CandidatePair */ private boolean useCandidate = false; + /** + * A flag indicating whether we have sent a check request that + * contained the USE-CANDIDATE attribute for this pair. It is used in + * GTalk compatibility mode as it lacks USE-CANDIDATE. + */ + private boolean useCandidateSent = false; + /** * Indicates whether this CandidatePair is on any of this agent's * valid pair lists. @@ -511,6 +518,26 @@ public class CandidatePair return connCheckTranID; } + /** + * Raises the useCandidateSent flag for this pair. + */ + public void setUseCandidateSent() + { + this.useCandidateSent = true; + } + + /** + * Returns true if someone has previously raised this pair's + * useCandidateSent flag and false otherwise. + * + * @return true if someone has previously raised this pair's + * useCandidate flag and false otherwise. + */ + public boolean useCandidateSent() + { + return useCandidateSent; + } + /** * Raises the useCandidate flag for this pair. */ diff --git a/src/org/ice4j/ice/ConnectivityCheckClient.java b/src/org/ice4j/ice/ConnectivityCheckClient.java index 7847845..1bba663 100644 --- a/src/org/ice4j/ice/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/ConnectivityCheckClient.java @@ -182,6 +182,16 @@ class ConnectivityCheckClient AttributeFactory.createUsernameAttribute(localUserName); request.addAttribute(unameAttr); + + if (parentAgent.isControlling()) + { + //if we are the controlling agent then we need to indicate our + //nominated pairs. + if(candidatePair.isNominated()) + { + candidatePair.setUseCandidateSent(); + } + } } else { @@ -497,9 +507,24 @@ class ConnectivityCheckClient Transport.TCP); } - LocalCandidate validLocalCandidate = parentAgent - .findLocalCandidate(mappedAddress); - // XXX GTALK, find local candidate with ufrag rather than mapped address + LocalCandidate validLocalCandidate = null; + + if(parentAgent.getCompatibilityMode() == CompatibilityMode.GTALK) + { + // find local candidate with ufrag rather than mapped address + UsernameAttribute uname = (UsernameAttribute)request + .getAttribute(Attribute.USERNAME); + String username = new String(uname.getUsername()); + String ufrag = username.substring(16, 32); + validLocalCandidate = parentAgent.findLocalCandidate(mappedAddress, + ufrag); + } + else if(parentAgent.getCompatibilityMode() == CompatibilityMode.RFC5245) + { + validLocalCandidate = parentAgent + .findLocalCandidate(mappedAddress); + } + Candidate validRemoteCandidate = checkedPair.getRemoteCandidate(); // RFC 5245: The agent checks the mapped address from the STUN @@ -648,7 +673,8 @@ class ConnectivityCheckClient if((parentAgent.isControlling() && request.containsAttribute(Attribute.USE_CANDIDATE)) || ((parentAgent.getCompatibilityMode() == - CompatibilityMode.GTALK) && validPair.isNominated())) + CompatibilityMode.GTALK) && //validPair.isNominated())) + checkedPair.useCandidateSent())) { if(validPair.getParentComponent().getSelectedPair() == null) { @@ -667,8 +693,9 @@ class ConnectivityCheckClient //itself had the USE-CANDIDATE attribute. This case is described in //Section 7.2.1.5, and may now result in setting the nominated flag for //the pair learned from the original request. - else if(checkedPair.useCandidateReceived() - && ! checkedPair.isNominated()) + else if(!parentAgent.isControlling() && + checkedPair.useCandidateReceived() && + !checkedPair.isNominated()) { if(checkedPair.getParentComponent().getSelectedPair() == null) { @@ -939,7 +966,10 @@ class ConnectivityCheckClient { synchronized (paceMakers) { - paceMakers.remove(this); + synchronized(this) + { + paceMakers.remove(this); + } } } } diff --git a/src/org/ice4j/ice/ConnectivityCheckServer.java b/src/org/ice4j/ice/ConnectivityCheckServer.java index 89f17f1..d17da2a 100644 --- a/src/org/ice4j/ice/ConnectivityCheckServer.java +++ b/src/org/ice4j/ice/ConnectivityCheckServer.java @@ -121,7 +121,8 @@ class ConnectivityCheckServer /* Google Talk ICE dialect considers every request to have * USE-CANDIDATE behavior. */ - useCandidate = true; + // it will be set in Agent.incomingCheckReceived + useCandidate = false; //true; /* Google Talk STUN request does not contains PRIORITY attribute * set it to the peer reflexive value (0.9 * 1000); @@ -142,6 +143,15 @@ class ConnectivityCheckServer remoteUfrag = username.substring(0, colon); } + if(parentAgent.getCompatibilityMode() == CompatibilityMode.GTALK && + parentAgent.findCandidatePair(localUFrag, remoteUfrag) == null) + { + logger.info("No candidate pair that match local and remote ufrag"); + // no candidate pair for the moment so do not send response + // XXX maybe sent an error + return; + } + //tell our address handler we saw a new remote address; parentAgent.incomingCheckReceived(evt.getRemoteAddress(), evt.getLocalAddress(), priority, remoteUfrag, localUFrag, diff --git a/src/org/ice4j/ice/DefaultNominator.java b/src/org/ice4j/ice/DefaultNominator.java index 0401056..1e7cc68 100644 --- a/src/org/ice4j/ice/DefaultNominator.java +++ b/src/org/ice4j/ice/DefaultNominator.java @@ -189,53 +189,62 @@ public class DefaultNominator * @param evt the {@link PropertyChangeEvent} containing the pair which * has been validated. */ - private synchronized void strategyNominateFirstHostOrReflexiveValid( + private void strategyNominateFirstHostOrReflexiveValid( PropertyChangeEvent evt) { if(IceMediaStream.PROPERTY_PAIR_VALIDATED.equals(evt.getPropertyName())) { CandidatePair validPair = (CandidatePair)evt.getSource(); - TimerTask task = validatedCandidates.get( - validPair.getParentComponent().toShortString()); + TimerTask task = null; boolean isRelayed = (validPair.getLocalCandidate() instanceof RelayedCandidate) || validPair.getLocalCandidate().getType().equals( CandidateType.RELAYED_CANDIDATE) || validPair.getRemoteCandidate().getType().equals( CandidateType.RELAYED_CANDIDATE); + boolean nominate = false; - if(isRelayed && task == null) + synchronized(validatedCandidates) { - /* armed a timer and see if a host or server reflexive pair - * gets nominated. Otherwise nominate the relayed candidate pair - */ - Timer timer = new Timer(); - task = new RelayedCandidateTask(validPair); + task = validatedCandidates.get( + validPair.getParentComponent().toShortString()); - logger.info("Wait timeout to nominate relayed candidate"); - timer.schedule(task, 0); - synchronized(validatedCandidates) + if(isRelayed && task == null) { + /* armed a timer and see if a host or server reflexive pair + * gets nominated. Otherwise nominate the relayed candidate + * pair + */ + Timer timer = new Timer(); + task = new RelayedCandidateTask(validPair); + + logger.info("Wait timeout to nominate relayed candidate"); + timer.schedule(task, 0); validatedCandidates.put( - validPair.getParentComponent().toShortString(), + validPair.getParentComponent().toShortString(), task); } - } - else if(!isRelayed) - { - // host or server reflexive candidate pair - if(task != null) + else if(!isRelayed) { - task.cancel(); - logger.info("Found a better candidate pair to nominate for " - + validPair.getParentComponent().toShortString()); - } + // host or server reflexive candidate pair + if(task != null) + { + task.cancel(); + logger.info( + "Found a better candidate pair to nominate for " + + validPair.getParentComponent(). + toShortString()); + } - logger.info("Nominate (first highest valid): " + - validPair.toShortString()); - parentAgent.nominate(validPair); + logger.info("Nominate (first highest valid): " + + validPair.toShortString()); + nominate = true; + } } + + if(nominate) + parentAgent.nominate(validPair); } } diff --git a/src/org/ice4j/stack/NetAccessManager.java b/src/org/ice4j/stack/NetAccessManager.java index 4e8810f..0335461 100644 --- a/src/org/ice4j/stack/NetAccessManager.java +++ b/src/org/ice4j/stack/NetAccessManager.java @@ -310,7 +310,7 @@ class NetAccessManager } /** - * Starts all message processors + * Stops message processors until processors count equals newSize. * * @param newSize the new thread pool size */