diff --git a/src/org/ice4j/attribute/ErrorCodeAttribute.java b/src/org/ice4j/attribute/ErrorCodeAttribute.java index f112729..a8830a3 100644 --- a/src/org/ice4j/attribute/ErrorCodeAttribute.java +++ b/src/org/ice4j/attribute/ErrorCodeAttribute.java @@ -85,6 +85,7 @@ public class ErrorCodeAttribute extends Attribute public static final char INTEGRITY_CHECK_FAILURE = 431; public static final char MISSING_USERNAME = 432; public static final char USE_TLS = 433; + public static final char ROLE_CONFLICT = 487; public static final char SERVER_ERROR = 500; public static final char GLOBAL_FAILURE = 600; diff --git a/src/org/ice4j/ice/checks/ConnectivityCheckServer.java b/src/org/ice4j/ice/checks/ConnectivityCheckServer.java index 5151d4e..8e80979 100644 --- a/src/org/ice4j/ice/checks/ConnectivityCheckServer.java +++ b/src/org/ice4j/ice/checks/ConnectivityCheckServer.java @@ -6,6 +6,7 @@ */ package org.ice4j.ice.checks; +import java.io.*; import java.util.*; import java.util.logging.*; @@ -98,15 +99,13 @@ public class ConnectivityCheckServer String remoteUfrag = username.substring(0, colon); //detect role conflicts -/* if( ( parentAgent.isControlling() + if( ( parentAgent.isControlling() && request.contains(Attribute.ICE_CONTROLLING)) || ( ! parentAgent.isControlling() - && request.contains(Attribute.ICE_CONTROLLED)))*/ + && request.contains(Attribute.ICE_CONTROLLED))) { - System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!tb="+ - ((IceControllingAttribute)request.getAttribute(Attribute.ICE_CONTROLLING)).getTieBreaker()); repairRoleConflict(evt); - //return; + return; } long priority = extractPriority(request); @@ -121,7 +120,7 @@ public class ConnectivityCheckServer try { - stunStack.sendResponse(evt.getTransactionID().getTransactionID(), + stunStack.sendResponse(evt.getTransactionID().getBytes(), response, evt.getLocalAddress(), evt.getRemoteAddress()); } catch (Exception e) @@ -169,6 +168,14 @@ public class ConnectivityCheckServer return priorityAttr.getPriority(); } + /** + * Resolves a role conflicts by either sendinf a 487 Role Conflict + * response or by changing this server's parent agent role. + * + * @param evt the {@link StunMessageEvent} containing the + * ICE-CONTROLLING or ICE-CONTROLLED attribute that + * allowed us to detect the role conflict. + */ private void repairRoleConflict(StunMessageEvent evt) { Message req = evt.getMessage(); @@ -183,17 +190,43 @@ public class ConnectivityCheckServer req.getAttribute(Attribute.ICE_CONTROLLING); long theirTieBreaker = controlling.getTieBreaker(); -System.out.println("############################################ TB=" + theirTieBreaker); + // If the agent's tie-breaker is larger than or equal to the // contents of the ICE-CONTROLLING attribute, the agent generates // a Binding error response and includes an ERROR-CODE attribute // with a value of 487 (Role Conflict) but retains its role. - //if() + if( ourTieBreaker >= theirTieBreaker) + { + Response response = MessageFactory.createBindingErrorResponse( + ErrorCodeAttribute.ROLE_CONFLICT); + try + { + StunStack.getInstance().sendResponse( + evt.getTransactionID().getBytes(), + response, evt.getLocalAddress(), + evt.getRemoteAddress()); + } + catch(Exception exc) + { + //rethrow so that we would send a 500 response instead. + throw new RuntimeException("Failed to send a 487", exc); + } + } //If the agent's tie-breaker is less than the contents of the - //ICE-CONTROLLING attribute, the agent switches to the controlled role. + //ICE-CONTROLLING attribute, the agent switches to the controlled + //role. + else + { + logger.finest( + "Swithing to controlled because theirTieBreaker=" + + theirTieBreaker + " and ourTieBreaker=" + + ourTieBreaker); + parentAgent.setControlling(false); + } } - + // If the agent is in the controlled role, and the ICE-CONTROLLED + // attribute is present in the request: else if(!parentAgent.isControlling() && req.contains(Attribute.ICE_CONTROLLED)) { @@ -201,25 +234,41 @@ System.out.println("############################################ TB=" + theirTie req.getAttribute(Attribute.ICE_CONTROLLED); long theirTieBreaker = controlled.getTieBreaker(); -System.out.println("############################################ TB=" + theirTieBreaker); + + //If the agent's tie-breaker is larger than or equal to the + //contents of the ICE-CONTROLLED attribute, the agent switches to + //the controlling role. + if(ourTieBreaker >= theirTieBreaker) + { + logger.finest( + "Swithing to controlling because theirTieBreaker=" + + theirTieBreaker + " and ourTieBreaker=" + + ourTieBreaker); + parentAgent.setControlling(true); + } + // If the agent's tie-breaker is less than the contents of the + // ICE-CONTROLLED attribute, the agent generates a Binding error + // response and includes an ERROR-CODE attribute with a value of + // 487 (Role Conflict) but retains its role. + else + { + Response response = MessageFactory.createBindingErrorResponse( + ErrorCodeAttribute.ROLE_CONFLICT); + + try + { + StunStack.getInstance().sendResponse( + evt.getTransactionID().getBytes(), + response, evt.getLocalAddress(), + evt.getRemoteAddress()); + } + catch(Exception exc) + { + //rethrow so that we would send a 500 response instead. + throw new RuntimeException("Failed to send a 487", exc); + } + } } -/* - - - o If the agent is in the controlled role, and the ICE-CONTROLLED - attribute is present in the request: - - * If the agent's tie-breaker is larger than or equal to the - contents of the ICE-CONTROLLED attribute, the agent switches to - the controlling role. - - * If the agent's tie-breaker is less than the contents of the - ICE-CONTROLLED attribute, the agent generates a Binding error - response and includes an ERROR-CODE attribute with a value of - 487 (Role Conflict) but retains its role. - - - * */ } /** diff --git a/src/org/ice4j/message/Message.java b/src/org/ice4j/message/Message.java index f816eae..20c3b42 100644 --- a/src/org/ice4j/message/Message.java +++ b/src/org/ice4j/message/Message.java @@ -287,8 +287,7 @@ public abstract class Message public void addAttribute(Attribute attribute) throws IllegalArgumentException { - Character attributeType = new Character(attribute.getAttributeType()); - if (getAttributePresentity(attributeType.charValue()) == N_A) + if (getAttributePresentity(attribute.getAttributeType()) == N_A) { throw new IllegalArgumentException( "The attribute " @@ -297,7 +296,7 @@ public abstract class Message + getName()); } - attributes.put(attributeType, attribute); + attributes.put(attribute.getAttributeType(), attribute); } /** @@ -311,7 +310,7 @@ public abstract class Message */ public boolean contains(char attributeType) { - return attributes.containsKey(new Character(attributeType)); + return attributes.containsKey(attributeType); } /** @@ -324,7 +323,7 @@ public abstract class Message */ public Attribute getAttribute(char attributeType) { - return attributes.get(new Character(attributeType)); + return attributes.get(attributeType); } /** @@ -346,7 +345,7 @@ public abstract class Message */ public Attribute removeAttribute(char attributeType) { - return attributes.remove(new Character(attributeType)); + return attributes.remove(attributeType); } /** diff --git a/src/org/ice4j/message/MessageFactory.java b/src/org/ice4j/message/MessageFactory.java index f98472b..c1eae18 100644 --- a/src/org/ice4j/message/MessageFactory.java +++ b/src/org/ice4j/message/MessageFactory.java @@ -304,14 +304,11 @@ public class MessageFactory * * @param errorCode the error code to encapsulate in this message attributes * that had not been recognized. - * @throws StunException INVALID_ARGUMENTS if one or more of the given - * parameters had an invalid value. * * @return a binding error response message containing an error code and a * UNKNOWN-ATTRIBUTES header */ public static Response createBindingErrorResponse(char errorCode) - throws StunException { return createBindingErrorResponse(errorCode, null, null); } diff --git a/src/org/ice4j/stack/StunClientTransaction.java b/src/org/ice4j/stack/StunClientTransaction.java index 0298ea0..e09511e 100644 --- a/src/org/ice4j/stack/StunClientTransaction.java +++ b/src/org/ice4j/stack/StunClientTransaction.java @@ -185,7 +185,7 @@ class StunClientTransaction try { - request.setTransactionID(transactionID.getTransactionID()); + request.setTransactionID(transactionID.getBytes()); } catch (StunException ex) { diff --git a/src/org/ice4j/stack/StunServerTransaction.java b/src/org/ice4j/stack/StunServerTransaction.java index 93ba862..ddb14fd 100644 --- a/src/org/ice4j/stack/StunServerTransaction.java +++ b/src/org/ice4j/stack/StunServerTransaction.java @@ -176,7 +176,7 @@ class StunServerTransaction this.response = response; //the transaction id might already have been set, but its our job //to make sure of that - response.setTransactionID(this.transactionID.getTransactionID()); + response.setTransactionID(this.transactionID.getBytes()); this.localSendingAddress = sendThrough; this.responseDestination = sendTo; } diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index 7d393bf..6de476b 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -561,7 +561,7 @@ t.printStackTrace(); try { - sendResponse(event.getTransactionID().getTransactionID(), + sendResponse(event.getTransactionID().getBytes(), error, event.getLocalAddress(), event.getRemoteAddress()); } diff --git a/src/org/ice4j/stack/TransactionID.java b/src/org/ice4j/stack/TransactionID.java index 8081ed5..f331168 100644 --- a/src/org/ice4j/stack/TransactionID.java +++ b/src/org/ice4j/stack/TransactionID.java @@ -124,7 +124,7 @@ public class TransactionID * Returns the transaction id byte array (length 12). * @return the transaction ID byte array. */ - public byte[] getTransactionID() + public byte[] getBytes() { return transactionID; } diff --git a/test/test/Ice.java b/test/test/Ice.java index 8fc8934..41038ce 100644 --- a/test/test/Ice.java +++ b/test/test/Ice.java @@ -35,8 +35,9 @@ public class Ice Agent localAgent = createAgent(9090); Agent remotePeer = createAgent(6060); - localAgent.setControlling(true); - remotePeer.setControlling(false); + //let them fight ... fights forge character. + //localAgent.setControlling(true); + //remotePeer.setControlling(false); long endTime = System.currentTimeMillis();