Adds connection ID and requested address family attributes to be used by TURN server. Patch by Aakash Garg.

This commit is contained in:
paweldomas 2014-05-10 13:58:02 +00:00
parent a63f66ac12
commit 92b64183d6
5 changed files with 394 additions and 3 deletions

View File

@ -61,6 +61,7 @@ import org.ice4j.*;
* @author Emil Ivov
* @author Sebastien Vincent
* @author Namal Senarathne
* @author Aakash Garg
*/
public abstract class Attribute
{
@ -149,7 +150,7 @@ public abstract class Attribute
* Alternate server attribute.
*/
public static final char ALTERNATE_SERVER = 0x8023;
/**
* Fingerprint attribute.
*/
@ -185,6 +186,11 @@ public abstract class Attribute
* XOR relayed address attribute.
*/
public static final char XOR_RELAYED_ADDRESS = 0x0016;
/**
* Requested Address Family attribute.
*/
public static final char REQUESTED_ADDRESS_FAMILY = 0X0017;
/**
* Even port attribute.
@ -205,6 +211,12 @@ public abstract class Attribute
* Reservation token attribute.
*/
public static final char RESERVATION_TOKEN = 0x0022;
/**
* Connection Id attribute.
* TURN TCP support attribute
*/
public static final char CONNECTION_ID = 0x002a;
/* Old TURN attributes */
/**
@ -249,7 +261,7 @@ public abstract class Attribute
protected char attributeType = 0;
/**
* The size of an atribute header in bytes = len(TYPE) + len(LENGTH) = 4
* The size of an attribute header in bytes = len(TYPE) + len(LENGTH) = 4
*/
public static final char HEADER_LENGTH = 4;
@ -315,6 +327,7 @@ public abstract class Attribute
* @return true if the attributes are equal and false otherwise.
*/
@Override
public abstract boolean equals(Object obj);
/**

View File

@ -14,6 +14,7 @@ import org.ice4j.*;
*
* @author Emil Ivov
* @author Sebastien Vincent
* @author Aakash Garg
*/
public class AttributeDecoder
{
@ -117,7 +118,10 @@ public class AttributeDecoder
decodedAttribute = new IceControlledAttribute(); break;
case Attribute.USE_CANDIDATE:
decodedAttribute = new UseCandidateAttribute(); break;
case Attribute.REQUESTED_ADDRESS_FAMILY:
decodedAttribute = new RequestedAddressFamilyAttribute(); break;
case Attribute.CONNECTION_ID:
decodedAttribute = new ConnectionIdAttribute(); break;
//According to rfc3489 we should silently ignore unknown attributes.
default: decodedAttribute
= new OptionalAttribute( Attribute.UNKNOWN_OPTIONAL_ATTRIBUTE);

View File

@ -18,6 +18,7 @@ import org.ice4j.*;
* @author Emil Ivov
* @author Sebastien Vincent
* @author Namal Senarathne
* @author Aakash Garg
*/
public class AttributeFactory
{
@ -600,4 +601,42 @@ public class AttributeFactory
return attribute;
}
/**
* Creates a new RequestedAddressFamilyAttribute of the specified family
*
* @param family address family value as specified in the RFC
* @return the newly created RequestedAddressFamily attribute if family is
* IPv4/IPv6 otherwise <tt>null</tt>.
*/
public static RequestedAddressFamilyAttribute
createRequestedAddressFamilyAttribute(char family)
{
RequestedAddressFamilyAttribute attribute
= new RequestedAddressFamilyAttribute();
boolean isSet = attribute.setFamily(family);
if(!isSet)
{
attribute = null;
}
return attribute;
}
/**
* Creates a new ConnectionIdAttribute of the specified connectionIdValue
*
* @param connectionIdValue the connection ID value.
* @return the newly created ConnectionId attribute.
*/
public static ConnectionIdAttribute createConnectionIdAttribute(
int connectionIdValue)
{
ConnectionIdAttribute attribute = new ConnectionIdAttribute();
attribute.setConnectionIdValue(connectionIdValue);
return attribute;
}
}

View File

@ -0,0 +1,158 @@
/*
* 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.StunException;
/**
* The connection ID attribute defined in RFC 6062:
* Traversal Using Relays around NAT (TURN) Extensions for TCP Allocations.
*
* @author Aakash Garg
*/
public class ConnectionIdAttribute
extends Attribute
{
/**
* Attribute Name.
*/
public static final String NAME = "CONNECTION-ID";
/**
* The length of the data contained in this attribute.
*/
public static final char DATA_LENGTH = 4;
/**
* The connection Id value.
*/
private int connectionIdValue;
/**
* Constructor.
*/
protected ConnectionIdAttribute()
{
super(CONNECTION_ID);
}
/**
* Returns the length of this attribute body.
* @return the length of this attribute value (4 bytes).
*/
@Override
public char getDataLength()
{
return DATA_LENGTH;
}
/**
* Returns the human readable name of this attribute. Attribute names do
* not really matter from the protocol point of view. They are only used
* for debugging and readability.
* @return this attribute's name.
*/
@Override
public String getName()
{
return NAME;
}
/**
* Compares two TURN 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.
*/
@Override
public boolean equals(Object obj)
{
if (! (obj instanceof ConnectionIdAttribute))
return false;
if (obj == this)
return true;
ConnectionIdAttribute att = (ConnectionIdAttribute) obj;
if (att.getAttributeType() != getAttributeType()
|| att.getDataLength() != getDataLength()
/* compare data */
|| att.connectionIdValue != this.connectionIdValue
)
return false;
return true;
}
/**
* Returns a binary representation of this attribute.
* @return a binary representation of this attribute.
*/
@Override
public byte[] encode()
{
byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH];
//Type
binValue[0] = (byte)(getAttributeType() >> 8);
binValue[1] = (byte)(getAttributeType() & 0x00FF);
//Length
binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength() & 0x00FF);
//Data
binValue[4] = (byte) (connectionIdValue >> 24);
binValue[5] = (byte) ((connectionIdValue & 0x00ff0000) >> 16);
binValue[6] = (byte) ((connectionIdValue & 0x0000ff00) >> 8);
binValue[7] = (byte) (connectionIdValue & 0x000000ff);
return binValue;
}
/**
* 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 binary array.
* @throws StunException if attributeValue contains invalid data.
*/
@Override
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{
if(length != 4)
{
throw new StunException("length invalid");
}
connectionIdValue = attributeValue[0] & 0xff;
connectionIdValue = connectionIdValue*256 + (attributeValue[1] & 0xff);
connectionIdValue = connectionIdValue*256 + (attributeValue[2] & 0xff);
connectionIdValue = connectionIdValue*256 + (attributeValue[3] & 0xff);
}
/**
* Gets the Connection-Id Value
* @return Connection-Id Value
*/
public int getConnectionIdValue()
{
return connectionIdValue;
}
/**
* Sets the Connection-Id Value
* @param connectionIdValue the connection Id value.
*/
public void setConnectionIdValue(int connectionIdValue)
{
this.connectionIdValue = connectionIdValue;
}
}

View File

@ -0,0 +1,177 @@
/*
* 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.StunException;
/**
* The requested address family attribute defined in RFC 6156.
*
* @author Aakash Garg
*/
public class RequestedAddressFamilyAttribute extends Attribute
{
/**
* Attribute Name.
*/
public static final String NAME = "REQUESTED-ADDRESS-FAMILY";
/**
* The length of the data contained in this attribute.
*/
public static final char DATA_LENGTH = 1;
/**
* The IPv4 family type.
*/
public static final char IPv4 = 0x01;
/**
* The IPv6 family type.
*/
public static final char IPv6 = 0x02;
/**
* The address family value.
*/
char family = IPv4;
/**
* Constructor.
*/
protected RequestedAddressFamilyAttribute()
{
super(REQUESTED_ADDRESS_FAMILY);
}
/**
* Returns the length of this attribute's body.
* @return the length of this attribute's value (1 byte).
*/
@Override
public char getDataLength()
{
return DATA_LENGTH;
}
/**
* Returns the human readable name of this attribute. Attribute names do
* not really matter from the protocol point of view. They are only used
* for debugging and readability.
* @return this attribute's name.
*/
@Override
public String getName()
{
return NAME;
}
/**
* Compares two TURN 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.
*/
@Override
public boolean equals(Object obj)
{
if (! (obj instanceof RequestedAddressFamilyAttribute))
return false;
if (obj == this)
return true;
RequestedAddressFamilyAttribute att
= (RequestedAddressFamilyAttribute) obj;
if (att.getAttributeType() != getAttributeType()
|| att.getDataLength() != getDataLength()
/* compare data */
|| att.family != family
)
return false;
return true;
}
/**
* Returns a binary representation of this attribute.
* @return a binary representation of this attribute.
*/
@Override
public byte[] encode()
{
byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH];
//Type
binValue[0] = (byte)(getAttributeType() >> 8);
binValue[1] = (byte)(getAttributeType() & 0x00FF);
//Length
binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength() & 0x00FF);
//Data
binValue[4] = (byte) family;
return binValue;
}
/**
* 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 binary array.
* @throws StunException if attrubteValue contains invalid data.
*/
@Override
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{
if(length != 4)
{
throw new StunException("length invalid");
}
family = (char)(attributeValue[0] & 0xff);
if(family != IPv4 && family != IPv6)
{
// instead throw TurnException
throw new StunException("invalid family value");
}
}
/**
* Gets the address family value
* @return family the address family value
*/
public char getFamily()
{
return family;
}
/**
* Sets the address family value
* @param family the address family value to set
* @return true if argument is IPv4 or IPv6 otherwise false
*/
public boolean setFamily(char family)
{
if(family == IPv4 || family == IPv6)
{
this.family = family;
return true;
}
else
{
return false;
}
}
}