diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 634510b..5aa1f01 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -37,6 +37,18 @@ public class Agent private static final Logger logger = Logger.getLogger(Agent.class.getName()); + /** + * The name of the System property that allows us to set a custom maximum + * for check list sizes. + */ + public static final String PNAME_MAX_CHECK_LIST_SIZE + = "org.ice4j.MAX_CHECK_LIST_SIZE"; + + /** + * The default maximum size for check lists. + */ + public static final int DEFAULT_MAX_CHECK_LIST_SIZE = 100; + /** * The LinkedHashMap used to store the media streams * This map preserves the insertion order of the media streams. @@ -58,18 +70,6 @@ public class Agent private final List harvesters = new ArrayList(); - /** - * The name of the System property that allows us to set a custom maximum - * for check list sizes. - */ - public static final String PNAME_MAX_CHECK_LIST_SIZE - = "org.ice4j.MAX_CHECK_LIST_SIZE"; - - /** - * The default maximum size for check lists. - */ - public static final int DEFAULT_MAX_CHECK_LIST_SIZE = 100; - /** * We use the FoundationsRegistry to keep track of the foundations * we assign within a session (i.e. the entire life time of an @@ -77,17 +77,6 @@ public class Agent */ private FoundationsRegistry foundationsRegistry = new FoundationsRegistry(); - /** - * The CHECK-LIST for this agent described in the ICE specification. Note - * that even though this may sound a bit unclear in the spec, - * checkLists are supposed to be created on a per-agent basis so - * that we could, for example, freeze candidates with common foundations - * across streams and not only across components, and so that we could - * limit the total number of candidate pairs for all streams. - */ - private final List checkList - = new LinkedList(); - /** * The user fragment that we should use for the ice-ufrag attribute. */ @@ -238,112 +227,33 @@ public class Agent component.selectDefaultCandidate(); } - /** - * Returns the list of CandidatePairs to be used in checks for - * this agent. - * - * @return the list of CandidatePairs to be used in checks for - * this agent. - */ - public List getCheckList() - { - return checkList; - } - /** * Initializes all stream check lists and begins the checks. */ public void startChecks() { - initCheckList(); + initCheckLists(); } /** * Creates, initializes and orders the list of candidate pairs that would * be used for the connectivity checks for all components in this stream. */ - protected void initCheckList() + protected void initCheckLists() { //first init the check list. - synchronized(checkList) - { - checkList.clear(); - List streams = getStreams(); - - for(IceMediaStream stream : streams) - { - stream.createCheckList(checkList); - } - - orderCheckList(); - pruneCheckList(); - } - } - - /** - * Orders this stream's pair check list in decreasing order of pair - * priority. If two pairs have identical priority, the ordering amongst - * them is arbitrary. - */ - private void orderCheckList() - { - Collections.sort(checkList, CandidatePair.comparator); - } - - /** - * Removes or, as per the ICE spec, "prunes" pairs that we don't need to - * run checks for. For example, since we cannot send requests directly - * from a reflexive candidate, but only from its base, we go through the - * sorted list of candidate pairs and in every pair where the local - * candidate is server reflexive, we replace the local server reflexive - * candidate with its base. Once this has been done, we remove each pair - * where the local and remote candidates are identical to the local and - * remote candidates of a pair higher up on the priority list. - *

- * In addition, in order to limit the attacks described in Section 18.5.2 - * of the ICE spec, we limit the total number of pairs and hence - * (connectivity checks) to a specific value, (100 by default). - */ - private void pruneCheckList() - { - //a list that we only use for storing pairs that we've already gone - //through. The list is destroyed at the end of this method. - List tmpCheckList - = new ArrayList(checkList.size()); - - Iterator ckListIter = checkList.iterator(); + List streams = getStreams(); + //init the maximum number of check list entries per stream. int maxCheckListSize = Integer.getInteger( - PNAME_MAX_CHECK_LIST_SIZE, DEFAULT_MAX_CHECK_LIST_SIZE); + PNAME_MAX_CHECK_LIST_SIZE, DEFAULT_MAX_CHECK_LIST_SIZE); - while(ckListIter.hasNext()) + int maxPerStreamSize = maxCheckListSize / streams.size(); + + for(IceMediaStream stream : streams) { - CandidatePair pair = ckListIter.next(); - - //drop all pairs above MAX_CHECK_LIST_SIZE. - if(tmpCheckList.size() > maxCheckListSize) - { - ckListIter.remove(); - continue; - } - - //replace local server reflexive candidates with their base. - LocalCandidate localCnd = pair.getLocalCandidate(); - if( localCnd.getType() - == CandidateType.SERVER_REFLEXIVE_CANDIDATE) - { - pair.setLocalCandidate(localCnd.getBase()); - - //if the new pair corresponds to another one with a higher - //priority, then remove it. - if(tmpCheckList.contains(pair)) - { - ckListIter.remove(); - continue; - } - } - - tmpCheckList.add(pair); + stream.setMaxCheckListSize(maxPerStreamSize); + stream.initCheckList(); } } diff --git a/src/org/ice4j/ice/IceMediaStream.java b/src/org/ice4j/ice/IceMediaStream.java index f8be55c..ff57102 100644 --- a/src/org/ice4j/ice/IceMediaStream.java +++ b/src/org/ice4j/ice/IceMediaStream.java @@ -48,11 +48,30 @@ public class IceMediaStream */ private int lastComponentID = 0; + /** + * The CHECK-LIST for this agent described in the ICE specification: There + * is one check list per in-use media stream resulting from the offer/answer + * exchange. + */ + private final List checkList + = new LinkedList(); + /** * The agent that this media stream belongs to. */ private final Agent parentAgent; + /** + * The maximum number of candidate pairs that we should have in our check + * list. This value depends on the total number of media streams which is + * why it should be set by the agent: + * In addition, in order to limit the attacks described in Section 18.5.2, + * an agent MUST limit the total number of connectivity checks they perform + * across all check lists to a specific value, adn this value MUST be + * configurable. A default of 100 is RECOMMENDED. + */ + private int maxCheckListSize = Agent.DEFAULT_MAX_CHECK_LIST_SIZE; + /** * Initializes a new IceMediaStream object. * @@ -244,6 +263,87 @@ public class IceMediaStream } } + /** + * Creates, initializes and orders the list of candidate pairs that would + * be used for the connectivity checks for all components in this stream. + */ + protected void initCheckList() + { + //first init the check list. + synchronized(checkList) + { + checkList.clear(); + createCheckList(checkList); + + orderCheckList(); + pruneCheckList(); + } + } + + /** + * Orders this stream's pair check list in decreasing order of pair + * priority. If two pairs have identical priority, the ordering amongst + * them is arbitrary. + */ + private void orderCheckList() + { + Collections.sort(checkList, CandidatePair.comparator); + } + + /** + * Removes or, as per the ICE spec, "prunes" pairs that we don't need to + * run checks for. For example, since we cannot send requests directly + * from a reflexive candidate, but only from its base, we go through the + * sorted list of candidate pairs and in every pair where the local + * candidate is server reflexive, we replace the local server reflexive + * candidate with its base. Once this has been done, we remove each pair + * where the local and remote candidates are identical to the local and + * remote candidates of a pair higher up on the priority list. + *

+ * In addition, in order to limit the attacks described in Section 18.5.2 + * of the ICE spec, we limit the total number of pairs and hence + * (connectivity checks) to a specific value, (a total of 100 by default). + */ + private void pruneCheckList() + { + //a list that we only use for storing pairs that we've already gone + //through. The list is destroyed at the end of this method. + List tmpCheckList + = new ArrayList(checkList.size()); + + Iterator ckListIter = checkList.iterator(); + + while(ckListIter.hasNext()) + { + CandidatePair pair = ckListIter.next(); + + //drop all pairs above MAX_CHECK_LIST_SIZE. + if(tmpCheckList.size() > maxCheckListSize) + { + ckListIter.remove(); + continue; + } + + //replace local server reflexive candidates with their base. + LocalCandidate localCnd = pair.getLocalCandidate(); + if( localCnd.getType() + == CandidateType.SERVER_REFLEXIVE_CANDIDATE) + { + pair.setLocalCandidate(localCnd.getBase()); + + //if the new pair corresponds to another one with a higher + //priority, then remove it. + if(tmpCheckList.contains(pair)) + { + ckListIter.remove(); + continue; + } + } + + tmpCheckList.add(pair); + } + } + /** * Creates, initializes and orders the list of candidate pairs that would * be used for the connectivity checks for all components in this stream. @@ -287,4 +387,26 @@ public class IceMediaStream } } } + + /** + * Returns the list of CandidatePairs to be used in checks for + * this stream. + * + * @return the list of CandidatePairs to be used in checks for + * this stream. + */ + public List getCheckList() + { + return checkList; + } + + /** + * Sets the maximum number of pairs that we should have in our check list. + * + * @param nSize the size of our check list. + */ + protected void setMaxCheckListSize(int nSize) + { + this.maxCheckListSize = nSize; + } } diff --git a/test/test/Ice.java b/test/test/Ice.java index fc79631..6c870d0 100644 --- a/test/test/Ice.java +++ b/test/test/Ice.java @@ -48,7 +48,10 @@ public class Ice localAgent.startChecks(); - System.out.println("Local audio clist:\n" + localAgent.getCheckList()); + System.out.println("Local audio clist:\n" + + localAgent.getStream("audio").getCheckList()); + System.out.println("Local audio clist:\n" + + localAgent.getStream("video").getCheckList()); System.out.println("Total execution time: " + (endTime - startTime) + "ms"); @@ -196,7 +199,7 @@ public class Ice //simultaneously with the others. //rtp - Component rtpComp = agent.createComponent( + agent.createComponent( stream, Transport.UDP, rtpPort, rtpPort, rtpPort + 100); long endTime = System.currentTimeMillis(); @@ -204,7 +207,7 @@ public class Ice + (endTime - startTime) +" ms"); startTime = endTime; //rtcpComp - Component rtcpComp = agent.createComponent( + agent.createComponent( stream, Transport.UDP, rtpPort + 1, rtpPort + 1, rtpPort + 101); endTime = System.currentTimeMillis();