Refactors the implementation of the ICE-CONTROLLING and ICE-CONRTOLLED attributes to avoir the 95% duplication between the two

This commit is contained in:
emcho 2010-05-08 21:34:35 +00:00
parent 6747c7cdcd
commit 63aac5e6f9
8 changed files with 344 additions and 369 deletions

View File

@ -109,6 +109,10 @@ public class AttributeDecoder
decodedAttribute = new ReservationTokenAttribute(); break;
case Attribute.PRIORITY:
decodedAttribute = new PriorityAttribute(); break;
case Attribute.ICE_CONTROLLING:
decodedAttribute = new IceControllingAttribute(); break;
case Attribute.ICE_CONTROLLED:
decodedAttribute = new IceControlledAttribute(); break;
//According to rfc3489 we should silently ignore unknown attributes.
default: decodedAttribute

View File

@ -499,26 +499,12 @@ public class AttributeFactory
*
* @param tieBreaker the tie-breaker value to be used
* @return the created IceControlledAttribute
* @throws IllegalArgumentException if the value in tie-breaker is not valid
*/
public static IceControlledAttribute createIceControlledAttribute(
long tieBreaker)
throws IllegalArgumentException
{
// generating the network ordered byte stream of the tieBreaker long
// variable
byte[] tieBreakerBytes = new byte[8];
tieBreakerBytes[0] = (byte) (tieBreaker >> 56);
tieBreakerBytes[1] = (byte) ((tieBreaker & 0x00FF000000000000L) >> 48);
tieBreakerBytes[2] = (byte) ((tieBreaker & 0x0000FF0000000000L) >> 40);
tieBreakerBytes[3] = (byte) ((tieBreaker & 0x000000FF00000000L) >> 32);
tieBreakerBytes[4] = (byte) ((tieBreaker & 0x00000000FF000000L) >> 24);
tieBreakerBytes[5] = (byte) ((tieBreaker & 0x0000000000FF0000L) >> 16);
tieBreakerBytes[6] = (byte) ((tieBreaker & 0x000000000000FF00L) >> 8);
tieBreakerBytes[7] = (byte) ((tieBreaker & 0x00000000000000FFL));
IceControlledAttribute attribute = new IceControlledAttribute();
attribute.setTieBreaker(tieBreakerBytes);
attribute.setTieBreaker(tieBreaker);
return attribute;
}
@ -558,26 +544,12 @@ public class AttributeFactory
* @param tieBreaker the tie-breaker value to be used
*
* @return the created IceControllingAttribute
* @throws IllegalArgumentException of tie-breaker value is not valid
*/
public static IceControllingAttribute createIceControllingAttribute(
long tieBreaker)
throws IllegalArgumentException
{
// generating the network ordered byte stream of the tieBreaker long
// variable
byte[] tieBreakerBytes = new byte[8];
tieBreakerBytes[0] = (byte) (tieBreaker >> 56);
tieBreakerBytes[1] = (byte) ((tieBreaker & 0x00FF000000000000L) >> 48);
tieBreakerBytes[2] = (byte) ((tieBreaker & 0x0000FF0000000000L) >> 40);
tieBreakerBytes[3] = (byte) ((tieBreaker & 0x000000FF00000000L) >> 32);
tieBreakerBytes[4] = (byte) ((tieBreaker & 0x00000000FF000000L) >> 24);
tieBreakerBytes[5] = (byte) ((tieBreaker & 0x0000000000FF0000L) >> 16);
tieBreakerBytes[6] = (byte) ((tieBreaker & 0x000000000000FF00L) >> 8);
tieBreakerBytes[7] = (byte) ((tieBreaker & 0x00000000000000FFL));
IceControllingAttribute attribute = new IceControllingAttribute();
attribute.setTieBreaker(tieBreakerBytes);
attribute.setTieBreaker(tieBreaker);
return attribute;
}

View File

@ -0,0 +1,175 @@
/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the SIP Communicator community (http://sip-communicator.org).
*
* Distributable under LGPL license. See terms of license at gnu.org.
*/
package org.ice4j.attribute;
import org.ice4j.*;
/**
*
* @author Emil Ivov
*/
public abstract class IceControlAttribute
extends Attribute
{
/**
* The length of the data contained in this attribute
*/
static final char DATA_LENGTH_ICE_CONTROL = 8;
/**
* The tie-breaker value stored in this attribute
*/
long tieBreaker;
/**
* Indicates whether this is an <tt>ICE-CONTROLLING</tt> or an
* <tt>ICE-CONTROLLED</tt> attribute.
*/
boolean isControlling;
/**
* Constructs an ICE-CONTROLLING or an ICE-CONTROLLED attribute depending
* on the value of <tt>isControlling</tt>.
*
* @param isControlling indicates the kind of attribute we are trying to
* create
*/
IceControlAttribute(boolean isControlling)
{
super(isControlling ? ICE_CONTROLLING : ICE_CONTROLLED);
this.isControlling = isControlling;
}
/**
* Sets this attribute's fields according to attributeValue array.
*
* @param attributeValue a binary array containing this attribute's field
* values and NOT containing the attribute header.
* @param offset the position where attribute values begin (most often
* offset is equal to the index of the first byte after
* length)
* @param length the length of the attribute data.
*
* @throws StunException if attrubteValue contains invalid data.
*/
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{
// array used to hold the intermediate long values reconstructed from the
// attributeValue array
// Reading in the network byte order (Big-Endian)
tieBreaker= ((attributeValue[offset++] & 0xffl) << 56)
| ((attributeValue[offset++] & 0xffl) << 48)
| ((attributeValue[offset++] & 0xffl) << 40)
| ((attributeValue[offset++] & 0xffl) << 32)
| ((attributeValue[offset++] & 0xffl) << 24)
| ((attributeValue[offset++] & 0xffl) << 16)
| ((attributeValue[offset++] & 0xffl) << 8)
| (attributeValue[offset++] & 0xffl);
}
/**
* Returns a binary representation of this attribute.
*
* @return a binary representation of this attribute.
*/
public byte[] encode()
{
char type = getAttributeType();
byte[] binValue = new byte[HEADER_LENGTH + getDataLength()];
//Type
binValue[0] = (byte)(type>>8);
binValue[1] = (byte)(type&0x00FF);
//Length
binValue[2] = (byte)(getDataLength()>>8);
binValue[3] = (byte)(getDataLength()&0x00FF);
//Tie-Breaker
binValue[4] = (byte)((tieBreaker & 0xFF00000000000000L) >> 56);
binValue[5] = (byte)((tieBreaker & 0x00FF000000000000L) >> 48);
binValue[6] = (byte)((tieBreaker & 0x0000FF0000000000L) >> 40);
binValue[7] = (byte)((tieBreaker & 0x000000FF00000000L) >> 32);
binValue[8] = (byte)((tieBreaker & 0x00000000FF000000L) >> 24);
binValue[9] = (byte)((tieBreaker & 0x0000000000FF0000L) >> 16);
binValue[10] = (byte)((tieBreaker & 0x000000000000FF00L) >> 8);
binValue[11] = (byte)( tieBreaker & 0x00000000000000FFL);
return binValue;
}
/**
* Compares two STUN Attributes. Attributes are considered equal when their
* type, length, and all data are the same.
*
* @param obj the object to compare this attribute with.
*
* @return true if the attributes are equal and false otherwise.
*/
public boolean equals(Object obj)
{
if(!(obj instanceof IceControlAttribute)
|| obj == null)
return false;
if(obj == this)
return true;
IceControlAttribute iceControlAtt = (IceControlAttribute)obj;
if(iceControlAtt.getAttributeType() != getAttributeType()
|| iceControlAtt.isControlling != isControlling
|| iceControlAtt.getDataLength() != DATA_LENGTH_ICE_CONTROL
|| getTieBreaker() != iceControlAtt.getTieBreaker())
{
return false;
}
return true;
}
/**
* Returns the data length of this attribute
*
* @return the data length of this attribute
*/
public char getDataLength()
{
return DATA_LENGTH_ICE_CONTROL;
}
/**
* Returns the human readable name of this attribute.
*
* @return this attribute's name.
*/
public String getName()
{
return isControlling ? "ICE-CONTROLLING" : "ICE-CONTROLLED";
}
/**
* Sets the tie-breaker value.
*
* @param tieBreaker the the tie-breaker value
*/
public void setTieBreaker(long tieBreaker)
{
this.tieBreaker = tieBreaker;
}
/**
* Returns the value of the tie-breaker.
*
* @return the value of the tie-breaker.
*/
public long getTieBreaker()
{
return tieBreaker;
}
}

View File

@ -1,157 +1,23 @@
package org.ice4j.attribute;
import java.util.Arrays;
import org.ice4j.*;
public class IceControlledAttribute
extends Attribute
{
/**
* The length of the data contained in this attribute
*/
private static final char DATA_LENGTH_ICE_CONTROLLED = 8;
/**
* The tie-breaker value represented in this attribute
*/
byte[] tieBreaker = null;
/**
* Constructs an ICE-CONTROLLED attribute
*/
protected IceControlledAttribute()
{
super(ICE_CONTROLLED);
}
/**
* Sets this attribute's fields according to attributeValue array.
*
* @param attributeValue a binary array containing this attribute's field
* values and NOT containing the attribute header.
* @param offset the position where attribute values begin (most often
* offset is equal to the index of the first byte after
* length)
* @param length the length of the attribute data.
*
* @throws StunException if attributeValue contains invalid data.
*/
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{
// initializing the byte arrays which holds the 64 bit value
// after decoding
tieBreaker = new byte[length];
System.arraycopy(attributeValue, offset, tieBreaker, 0, length);
}
/**
* Returns a binary representation of this attribute.
*
* @return a binary representation of this attribute.
*/
public byte[] encode() {
char type = getAttributeType();
byte[] binValue = new byte[HEADER_LENGTH + DATA_LENGTH_ICE_CONTROLLED];
// Type
binValue[0] = (byte)(type>>8);
binValue[1] = (byte)(type&0x00FF);
// Length
binValue[2] = (byte)(DATA_LENGTH_ICE_CONTROLLED>>8);
binValue[3] = (byte)(DATA_LENGTH_ICE_CONTROLLED&0x00FF);
// Tie-Breaker, 64 bits stored in network byte order
System.arraycopy(tieBreaker, 0, binValue, 4, tieBreaker.length);
return binValue;
}
/**
* Compares two STUN Attributes. Attributes are considered equal when their
* type, length, and all data are the same.
*
* @param obj the object to compare this attribute with.
*
* @return true if the attributes are equal and false otherwise.
*/
public boolean equals(Object obj)
{
if(!(obj instanceof IceControlledAttribute)
|| obj == null)
return false;
if(obj == this)
return true;
IceControlledAttribute iceControlledAtt = (IceControlledAttribute)obj;
if(iceControlledAtt.getAttributeType() != getAttributeType()
|| iceControlledAtt.getDataLength() != DATA_LENGTH_ICE_CONTROLLED
|| (tieBreaker != null
&& !Arrays.equals(tieBreaker, iceControlledAtt.tieBreaker))
)
return false;
return true;
}
/**
* Returns the data length of this attribute
*
* @return the data length of this attribute
*/
public char getDataLength()
{
return DATA_LENGTH_ICE_CONTROLLED;
}
/**
* Returns the human readable name of this attribute.
*
* @return this attribute's name.
*/
public String getName()
{
return "ICE-CONTROLLED";
}
/**
* Sets the <tt>tieBreaker</tt> byte array.
*
* @param tieBreaker the byte array containing the 64 bit tie-breaker
* value
*
* @throws IllegalArgumentException if tieBreaker contains invalid data
*/
public void setTieBreaker(byte[] tieBreaker)
throws IllegalArgumentException
{
if(tieBreaker.length != DATA_LENGTH_ICE_CONTROLLED)
{
throw new IllegalArgumentException(
"The supplied byte array does not contain the correct number"
+" of bytes");
}
else
{
this.tieBreaker = tieBreaker;
}
}
/**
* Returns a copy of the Tie Breaker byte array
*
* @return an exact copy of the Tie Breaker byte array
*/
public byte[] getTieBreaker()
{
return tieBreaker;
}
}
/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the SIP Communicator community (http://sip-communicator.org).
*
* Distributable under LGPL license. See terms of license at gnu.org.
*/
package org.ice4j.attribute;
/**
* An {@link IceControlAttribute} implementation representing the
* ICE-CONTROLLED ICE {@link Attribute}s.
*/
public final class IceControlledAttribute
extends IceControlAttribute
{
/**
* Constructs an ICE-CONTROLLING attribute.
*/
public IceControlledAttribute()
{
super(false);
}
}

View File

@ -1,159 +1,23 @@
/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the SIP Communicator community (http://sip-communicator.org).
*
* Distributable under LGPL license. See terms of license at gnu.org.
*/
package org.ice4j.attribute;
import java.util.Arrays;
import org.ice4j.*;
public class IceControllingAttribute
extends Attribute
{
/**
* The length of the data contained in this attribute
*/
private static final char DATA_LENGTH_ICE_CONTROLLING = 8;
/**
* The tie-breaker value stored in this attribute
*/
byte[] tieBreaker = null;
/**
* Constructs an ICE-CONTROLLING attribute
*/
protected IceControllingAttribute()
{
super(ICE_CONTROLLING);
}
/**
* Sets this attribute's fields according to attributeValue array.
*
* @param attributeValue a binary array containing this attribute's field
* values and NOT containing the attribute header.
* @param offset the position where attribute values begin (most often
* offset is equal to the index of the first byte after
* length)
* @param length the length of the attribute data.
*
* @throws StunException if attrubteValue contains invalid data.
*/
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException {
// initializing the byte arrays which holds the 64 bit value after decoding
tieBreaker = new byte[length];
System.arraycopy(attributeValue, offset, tieBreaker, 0, length);
}
/**
* Returns a binary representation of this attribute.
*
* @return a binary representation of this attribute.
*/
public byte[] encode() {
char type = getAttributeType();
byte[] binValue = new byte[HEADER_LENGTH + DATA_LENGTH_ICE_CONTROLLING];
// Type
binValue[0] = (byte)(type>>8);
binValue[1] = (byte)(type&0x00FF);
// Length
binValue[2] = (byte)(DATA_LENGTH_ICE_CONTROLLING>>8);
binValue[3] = (byte)(DATA_LENGTH_ICE_CONTROLLING&0x00FF);
// Tie-Breaker, 64 bits stored in network byte order
System.arraycopy(tieBreaker, 0, binValue, 4, tieBreaker.length);
return binValue;
}
/**
* Compares two STUN Attributes. Attributes are considered equal when their
* type, length, and all data are the same.
*
* @param obj the object to compare this attribute with.
*
* @return true if the attributes are equal and false otherwise.
*/
public boolean equals(Object obj)
{
if(!(obj instanceof IceControllingAttribute)
|| obj == null)
return false;
if(obj == this)
return true;
IceControllingAttribute iceControllingAtt = (IceControllingAttribute)obj;
if(iceControllingAtt.getAttributeType() != getAttributeType()
|| iceControllingAtt.getDataLength() != DATA_LENGTH_ICE_CONTROLLING
|| !(Arrays.equals(getTieBreaker(),
iceControllingAtt.getTieBreaker())))
return false;
return true;
}
/**
* Returns the data length of this attribute
*
* @return the data length of this attribute
*/
public char getDataLength()
{
return DATA_LENGTH_ICE_CONTROLLING;
}
/**
* Returns the human readable name of this attribute.
*
* @return this attribute's name.
*/
public String getName()
{
return "ICE-CONTROLLING";
}
/**
* Sets the Tie Breaker byte array to the specified byte array
* An exact copy of the specified byte array is made
*
* @param tieBreaker the byte array containing the 64 bit tie-breaker value
*
* @throws IllegalArgumentException if tieBreaker contains invalid data
*/
public void setTieBreaker(byte[] tieBreaker)
throws IllegalArgumentException
{
if(tieBreaker.length != DATA_LENGTH_ICE_CONTROLLING)
{
throw new IllegalArgumentException(
"The supplied byte array does not contain the correct number "
+"of bytes");
}
this.tieBreaker = tieBreaker;
}
/**
* Returns a copy of the Tie Breaker byte array
*
* @return an exact copy of the Tie Breaker byte array
*/
public byte[] getTieBreaker()
{
if(tieBreaker == null)
return null;
return tieBreaker;
}
}
/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the SIP Communicator community (http://sip-communicator.org).
*
* Distributable under LGPL license. See terms of license at gnu.org.
*/
package org.ice4j.attribute;
/**
* An {@link IceControlAttribute} implementation representing the
* ICE-CONTROLLING ICE {@link Attribute}s.
*/
public final class IceControllingAttribute
extends IceControlAttribute
{
/**
* Constructs an ICE-CONTROLLING attribute.
*/
public IceControllingAttribute()
{
super(true);
}
}

View File

@ -45,15 +45,6 @@ public class ConnectivityCheckServer
*/
private StunStack stunStack = StunStack.getInstance();
/**
* The <tt>triggeredCheckQueue</tt> is a FIFO queue containing candidate
* pairs for which checks are to be sent at the next available opportunity.
* A pair would get into a triggered check queue as soon as we receive
* a check on its local candidate.
*/
private final List<CandidatePair> triggeredCheckQueue
= new Vector<CandidatePair>();
/**
* Creates a new <tt>ConnectivityCheckServer</tt> setting
* <tt>parentAgent</tt> as the agent that will be used for retrieving
@ -106,23 +97,19 @@ public class ConnectivityCheckServer
//caller gave us the entire username.
String remoteUfrag = username.substring(0, colon);
//make sure we have a priority attribute and ignore otherwise.
PriorityAttribute priorityAttr
= (PriorityAttribute)request.getAttribute(Attribute.PRIORITY);
//extract priority
if(priorityAttr == null)
//detect role conflicts
/* if( ( parentAgent.isControlling()
&& request.contains(Attribute.ICE_CONTROLLING))
|| ( ! parentAgent.isControlling()
&& request.contains(Attribute.ICE_CONTROLLED)))*/
{
if(logger.isLoggable(Level.FINE))
{
logger.log(Level.FINE, "Received a connectivity ckeck with"
+"no PRIORITY attribute. Discarting.");
}
throw new IllegalArgumentException("Missing PRIORITY attribtue!");
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!tb="+
((IceControllingAttribute)request.getAttribute(Attribute.ICE_CONTROLLING)).getTieBreaker());
repairRoleConflict(evt);
//return;
}
long priority = priorityAttr.getPriority();
long priority = extractPriority(request);
//tell our address handler we saw a new remote address;
addressHandler.incomingCheckReceived(evt.getRemoteAddress(),
@ -144,6 +131,97 @@ public class ConnectivityCheckServer
}
}
/**
* Returns the value of the {@link PriorityAttribute} in <tt>request</tt> if
* there is one or throws an <tt>IllegalArgumentException</tt> with the
* corresponding message.
*
* @param request the {@link Request} whose priority we'd like to obtain.
*
* @return the value of the {@link PriorityAttribute} in <tt>request</tt> if
* there is one
*
* @throws IllegalArgumentException if the request does not contain a
* PRIORITY attribute and the stack needs to respond with a 400 Bad Request
* {@link Response}.
*/
private long extractPriority(Request request)
throws IllegalArgumentException
{
//make sure we have a priority attribute and ignore otherwise.
PriorityAttribute priorityAttr
= (PriorityAttribute)request.getAttribute(Attribute.PRIORITY);
//apply tie-breaking
//extract priority
if(priorityAttr == null)
{
if(logger.isLoggable(Level.FINE))
{
logger.log(Level.FINE, "Received a connectivity ckeck with"
+"no PRIORITY attribute. Discarting.");
}
throw new IllegalArgumentException("Missing PRIORITY attribtue!");
}
return priorityAttr.getPriority();
}
private void repairRoleConflict(StunMessageEvent evt)
{
Message req = evt.getMessage();
long ourTieBreaker = parentAgent.getTieBreaker();
// If the agent is in the controlling role, and the
// ICE-CONTROLLING attribute is present in the request:
if(parentAgent.isControlling()
&& req.contains(Attribute.ICE_CONTROLLING))
{
IceControllingAttribute controlling = (IceControllingAttribute)
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 the agent's tie-breaker is less than the contents of the
//ICE-CONTROLLING attribute, the agent switches to the controlled role.
}
else if(!parentAgent.isControlling()
&& req.contains(Attribute.ICE_CONTROLLED))
{
IceControllingAttribute controlled = (IceControllingAttribute)
req.getAttribute(Attribute.ICE_CONTROLLED);
long theirTieBreaker = controlled.getTieBreaker();
System.out.println("############################################ TB=" + theirTieBreaker);
}
/*
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.
* */
}
/**
* Verifies whether <tt>username</tt> is currently known to this server
* and returns <tt>true</tt> if so. Returns <tt>false</tt> otherwise.

View File

@ -300,6 +300,20 @@ public abstract class Message
attributes.put(attributeType, attribute);
}
/**
* Returns <tt>true</tt> if the this <tt>Message</tt> contains an attribute
* with the specified type or <tt>false</tt> otherwise.
*
* @param attributeType the type whose presence we need to determine.
*
* @return <tt>true</tt> if the this <tt>Message</tt> contains an attribute
* with the specified type or <tt>false</tt> otherwise.
*/
public boolean contains(char attributeType)
{
return attributes.containsKey(new Character(attributeType));
}
/**
* Returns the attribute with the specified type or null if no such
* attribute exists.

View File

@ -543,6 +543,8 @@ public class StunStack
catch (Throwable t)
{
Response error;
t.printStackTrace();
logger.log(Level.FINER, "Received an invalid request.", t);
if(t instanceof IllegalArgumentException)
{