Implements server side role conflict detection and resolution (RFC 5245: Section 7.2.1.1)

This commit is contained in:
emcho 2010-05-08 21:34:56 +00:00
parent 63aac5e6f9
commit cc51f9ff21
9 changed files with 90 additions and 43 deletions

View File

@ -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;

View File

@ -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 <tt>487 Role Conflict</tt>
* response or by changing this server's parent agent role.
*
* @param evt the {@link StunMessageEvent} containing the
* <tt>ICE-CONTROLLING</tt> or <tt>ICE-CONTROLLED</tt> 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.
* */
}
/**

View File

@ -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);
}
/**

View File

@ -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);
}

View File

@ -185,7 +185,7 @@ class StunClientTransaction
try
{
request.setTransactionID(transactionID.getTransactionID());
request.setTransactionID(transactionID.getBytes());
}
catch (StunException ex)
{

View File

@ -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;
}

View File

@ -561,7 +561,7 @@ t.printStackTrace();
try
{
sendResponse(event.getTransactionID().getTransactionID(),
sendResponse(event.getTransactionID().getBytes(),
error, event.getLocalAddress(),
event.getRemoteAddress());
}

View File

@ -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;
}

View File

@ -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();