Source code formating for the attribute classes, fix buggy attribute length calculation, add attributes from _old_ TURN draft specification.

This commit is contained in:
seb 2011-06-03 14:39:08 +00:00
parent 768db27837
commit b5e2234962
36 changed files with 765 additions and 408 deletions

View File

@ -100,7 +100,7 @@ abstract class AddressAttribute extends Attribute
|| type == SOURCE_ADDRESS || type == CHANGED_ADDRESS || type == SOURCE_ADDRESS || type == CHANGED_ADDRESS
|| type == REFLECTED_FROM || type == XOR_MAPPED_ADDRESS || type == REFLECTED_FROM || type == XOR_MAPPED_ADDRESS
|| type == ALTERNATE_SERVER || type == XOR_PEER_ADDRESS || type == ALTERNATE_SERVER || type == XOR_PEER_ADDRESS
|| type == XOR_RELAYED_ADDRESS); || type == XOR_RELAYED_ADDRESS || type == DESTINATION_ADDRESS);
} }
/** /**
@ -199,18 +199,18 @@ abstract class AddressAttribute extends Attribute
byte binValue[] = new byte[HEADER_LENGTH + getDataLength()]; byte binValue[] = new byte[HEADER_LENGTH + getDataLength()];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
//Not used //Not used
binValue[4] = 0x00; binValue[4] = 0x00;
//Family //Family
binValue[5] = getFamily(); binValue[5] = getFamily();
//port //port
binValue[6] = (byte)(getPort()>>8); binValue[6] = (byte)(getPort() >> 8);
binValue[7] = (byte)(getPort()&0x00FF); binValue[7] = (byte)(getPort() & 0x00FF);
//address //address
if(getFamily() == ADDRESS_FAMILY_IPV6){ if(getFamily() == ADDRESS_FAMILY_IPV6){
@ -262,7 +262,6 @@ abstract class AddressAttribute extends Attribute
return ADDRESS_FAMILY_IPV6; return ADDRESS_FAMILY_IPV6;
else else
return ADDRESS_FAMILY_IPV4; return ADDRESS_FAMILY_IPV4;
} }
/** /**
@ -296,14 +295,16 @@ abstract class AddressAttribute extends Attribute
//port //port
char port = ((char)((attributeValue[offset++] << 8 ) char port = ((char)((attributeValue[offset++] << 8 )
| (attributeValue[offset++]&0xFF) )); | (attributeValue[offset++] & 0xFF) ));
//address //address
byte address[] = null; byte address[] = null;
if(family == ADDRESS_FAMILY_IPV6){ if(family == ADDRESS_FAMILY_IPV6)
{
address = new byte[16]; address = new byte[16];
} }
else{ else
{
//ipv4 //ipv4
address = new byte[4]; address = new byte[4];
} }
@ -317,7 +318,5 @@ abstract class AddressAttribute extends Attribute
{ {
throw new StunException(e); throw new StunException(e);
} }
} }
} }

View File

@ -30,7 +30,6 @@ package org.ice4j.attribute;
* *
* @author Sebastien Vincent * @author Sebastien Vincent
*/ */
public class AlternateServerAttribute extends AddressAttribute public class AlternateServerAttribute extends AddressAttribute
{ {
/** /**

View File

@ -65,41 +65,182 @@ import org.ice4j.*;
public abstract class Attribute public abstract class Attribute
{ {
/* STUN attributes */ /* STUN attributes */
/**
* Mapped address attribute.
*/
public static final char MAPPED_ADDRESS = 0x0001; public static final char MAPPED_ADDRESS = 0x0001;
/**
* Response address attribute.
*/
public static final char RESPONSE_ADDRESS = 0x0002; public static final char RESPONSE_ADDRESS = 0x0002;
/**
* Change request attribute.
*/
public static final char CHANGE_REQUEST = 0x0003; public static final char CHANGE_REQUEST = 0x0003;
/**
* Source address attribute.
*/
public static final char SOURCE_ADDRESS = 0x0004; public static final char SOURCE_ADDRESS = 0x0004;
/**
* Changed address attribute.
*/
public static final char CHANGED_ADDRESS = 0x0005; public static final char CHANGED_ADDRESS = 0x0005;
/**
* Username attribute.
*/
public static final char USERNAME = 0x0006; public static final char USERNAME = 0x0006;
/**
* Password attribute.
*/
public static final char PASSWORD = 0x0007; public static final char PASSWORD = 0x0007;
/**
* Message integrity attribute.
*/
public static final char MESSAGE_INTEGRITY = 0x0008; public static final char MESSAGE_INTEGRITY = 0x0008;
/**
* Error code attribute.
*/
public static final char ERROR_CODE = 0x0009; public static final char ERROR_CODE = 0x0009;
/**
* Unknown attributes attribute.
*/
public static final char UNKNOWN_ATTRIBUTES = 0x000a; public static final char UNKNOWN_ATTRIBUTES = 0x000a;
/**
* Reflected from attribute.
*/
public static final char REFLECTED_FROM = 0x000b; public static final char REFLECTED_FROM = 0x000b;
/**
* Realm attribute.
*/
public static final char REALM = 0x0014; public static final char REALM = 0x0014;
/**
* Nonce attribute.
*/
public static final char NONCE = 0x0015; public static final char NONCE = 0x0015;
/**
* XOR Mapped address attribute.
*/
public static final char XOR_MAPPED_ADDRESS = 0x0020; public static final char XOR_MAPPED_ADDRESS = 0x0020;
/**
* XOR only attribute.
*/
public static final char XOR_ONLY = 0x0021; public static final char XOR_ONLY = 0x0021;
/**
* Software attribute.
*/
public static final char SOFTWARE = 0x8022; public static final char SOFTWARE = 0x8022;
/**
* Alternate server attribute.
*/
public static final char ALTERNATE_SERVER = 0x8023; public static final char ALTERNATE_SERVER = 0x8023;
/**
* Fingerprint attribute.
*/
public static final char FINGERPRINT = 0x8028; public static final char FINGERPRINT = 0x8028;
/**
* Unknown optional attribute.
*/
public static final char UNKNOWN_OPTIONAL_ATTRIBUTE = 0x8000; public static final char UNKNOWN_OPTIONAL_ATTRIBUTE = 0x8000;
/* TURN attributes */ /* TURN attributes */
/**
* Channel number attribute.
*/
public static final char CHANNEL_NUMBER = 0x000c; public static final char CHANNEL_NUMBER = 0x000c;
/**
* Lifetime attribute.
*/
public static final char LIFETIME = 0x000d; public static final char LIFETIME = 0x000d;
/**
* XOR peer address attribute.
*/
public static final char XOR_PEER_ADDRESS = 0x0012; public static final char XOR_PEER_ADDRESS = 0x0012;
/**
* Data attribute.
*/
public static final char DATA = 0x0013; public static final char DATA = 0x0013;
/**
* XOR relayed address attribute.
*/
public static final char XOR_RELAYED_ADDRESS = 0x0016; public static final char XOR_RELAYED_ADDRESS = 0x0016;
/**
* Even port attribute.
*/
public static final char EVEN_PORT = 0x0018; public static final char EVEN_PORT = 0x0018;
/**
* Requested transport attribute.
*/
public static final char REQUESTED_TRANSPORT = 0x0019; public static final char REQUESTED_TRANSPORT = 0x0019;
/**
* Don't fragment attribute.
*/
public static final char DONT_FRAGMENT = 0x001a; public static final char DONT_FRAGMENT = 0x001a;
/**
* Reservation token attribute.
*/
public static final char RESERVATION_TOKEN = 0x0022; public static final char RESERVATION_TOKEN = 0x0022;
/* Old TURN attributes */
/**
* Magic cookie attribute.
*/
public static final char MAGIC_COOKIE = 0x000f;
/**
* Destination address attribute.
*/
public static final char DESTINATION_ADDRESS = 0x0011;
/**
* Destination address attribute.
*/
public static final char REMOTE_ADDRESS = 0x0012;
/* ICE attributes */ /* ICE attributes */
/**
* Priority attribute.
*/
public static final char PRIORITY = 0x0024; public static final char PRIORITY = 0x0024;
/**
* Use candidate attribute.
*/
public static final char USE_CANDIDATE = 0x0025; public static final char USE_CANDIDATE = 0x0025;
/**
* ICE controlled attribute.
*/
public static final char ICE_CONTROLLED = 0x8029; public static final char ICE_CONTROLLED = 0x8029;
/**
* ICE controlling attribute.
*/
public static final char ICE_CONTROLLING = 0x802a; public static final char ICE_CONTROLLING = 0x802a;
/** /**

View File

@ -42,8 +42,10 @@ public class AttributeDecoder
} }
//Discover attribute type //Discover attribute type
char attributeType = (char)((bytes[offset]<<8)|bytes[offset + 1]); char attributeType = (char)((bytes[offset] << 8) | bytes[offset + 1]);
char attributeLength = (char)((bytes[offset + 2]<<8)|bytes[offset + 3]); int len1 = bytes[offset + 2] & 0xff;
int len2 = bytes[offset + 3] & 0xff;
char attributeLength = (char)((len1 << 8) | len2);
if(attributeLength > bytes.length - offset ) if(attributeLength > bytes.length - offset )
throw new StunException( StunException.ILLEGAL_ARGUMENT, throw new StunException( StunException.ILLEGAL_ARGUMENT,

View File

@ -30,7 +30,7 @@ public class AttributeFactory
public static ChangeRequestAttribute createChangeRequestAttribute() public static ChangeRequestAttribute createChangeRequestAttribute()
{ {
return createChangeRequestAttribute(false, false); return createChangeRequestAttribute(false, false);
}; }
/** /**
* Creates a ChangeRequestAttribute with the specified flag values. * Creates a ChangeRequestAttribute with the specified flag values.
@ -48,7 +48,7 @@ public class AttributeFactory
attribute.setChangePortFlag(changePort); attribute.setChangePortFlag(changePort);
return attribute; return attribute;
}; }
/** /**
* Creates a changedAddressAttribute of the specified type and with the * Creates a changedAddressAttribute of the specified type and with the
@ -65,8 +65,7 @@ public class AttributeFactory
attribute.setAddress(address); attribute.setAddress(address);
return attribute; return attribute;
}
};
/** /**
* Creates an ErrorCodeAttribute with the specified error class and number * Creates an ErrorCodeAttribute with the specified error class and number
@ -83,7 +82,7 @@ public class AttributeFactory
throws StunException throws StunException
{ {
return createErrorCodeAttribute(errorClass, errorNumber, null); return createErrorCodeAttribute(errorClass, errorNumber, null);
}; }
/** /**
* Creates an ErrorCodeAttribute with the specified error class, number and * Creates an ErrorCodeAttribute with the specified error class, number and
@ -114,7 +113,7 @@ public class AttributeFactory
: reasonPhrase); : reasonPhrase);
return attribute; return attribute;
}; }
/** /**
* Creates an ErrorCodeAttribute with the specified error code and a default * Creates an ErrorCodeAttribute with the specified error code and a default
@ -129,7 +128,7 @@ public class AttributeFactory
throws StunException throws StunException
{ {
return createErrorCodeAttribute(errorCode, null); return createErrorCodeAttribute(errorCode, null);
}; }
/** /**
* Creates an ErrorCodeAttribute with the specified error code and reason * Creates an ErrorCodeAttribute with the specified error code and reason
@ -157,7 +156,7 @@ public class AttributeFactory
: reasonPhrase); : reasonPhrase);
return attribute; return attribute;
}; }
/** /**
* Creates a MappedAddressAttribute of the specified type and with the * Creates a MappedAddressAttribute of the specified type and with the
@ -174,8 +173,7 @@ public class AttributeFactory
attribute.setAddress(address); attribute.setAddress(address);
return attribute; return attribute;
}
};
/** /**
* Creates a ReflectedFromAddressAttribute of the specified type and with * Creates a ReflectedFromAddressAttribute of the specified type and with
@ -192,8 +190,7 @@ public class AttributeFactory
attribute.setAddress(address); attribute.setAddress(address);
return attribute; return attribute;
}
};
/** /**
* Creates a ResponseFromAddressAttribute of the specified type and with the * Creates a ResponseFromAddressAttribute of the specified type and with the
@ -210,8 +207,7 @@ public class AttributeFactory
attribute.setAddress(address); attribute.setAddress(address);
return attribute; return attribute;
}
};
/** /**
* Creates a SourceFromAddressAttribute of the specified type and with the * Creates a SourceFromAddressAttribute of the specified type and with the
@ -228,8 +224,7 @@ public class AttributeFactory
attribute.setAddress(address); attribute.setAddress(address);
return attribute; return attribute;
}
};
/** /**
* Creates an empty UnknownAttributesAttribute. * Creates an empty UnknownAttributesAttribute.
@ -241,7 +236,7 @@ public class AttributeFactory
UnknownAttributesAttribute attribute = new UnknownAttributesAttribute(); UnknownAttributesAttribute attribute = new UnknownAttributesAttribute();
return attribute; return attribute;
}; }
/** /**
* Creates a XorRelayedAddressAttribute of the specified type and with the * Creates a XorRelayedAddressAttribute of the specified type and with the
@ -299,8 +294,7 @@ public class AttributeFactory
attribute.setAddress(address, tranID); attribute.setAddress(address, tranID);
return attribute; return attribute;
}
};
/** /**
* Create a UsernameAttribute. * Create a UsernameAttribute.
@ -357,7 +351,7 @@ public class AttributeFactory
attribute.setUsername(username); attribute.setUsername(username);
return attribute; return attribute;
}; }
/** /**
* Creates an empty <tt>FingerprintAttribute</tt> with a 0 check sum. Once * Creates an empty <tt>FingerprintAttribute</tt> with a 0 check sum. Once
@ -371,7 +365,7 @@ public class AttributeFactory
FingerprintAttribute attribute = new FingerprintAttribute(); FingerprintAttribute attribute = new FingerprintAttribute();
return attribute; return attribute;
}; }
/** /**
* Create a ChannelNumberAttribute. * Create a ChannelNumberAttribute.
@ -467,7 +461,8 @@ public class AttributeFactory
public static RequestedTransportAttribute createRequestedTransportAttribute( public static RequestedTransportAttribute createRequestedTransportAttribute(
byte protocol) byte protocol)
{ {
RequestedTransportAttribute attribute = new RequestedTransportAttribute(); RequestedTransportAttribute attribute =
new RequestedTransportAttribute();
attribute.setRequestedTransport(protocol); attribute.setRequestedTransport(protocol);
return attribute; return attribute;
@ -562,4 +557,33 @@ public class AttributeFactory
return attribute; return attribute;
} }
/**
* Creates a MagicCookieAttribute.
*
* @return the created MagicCookieAttribute
*/
public static MagicCookieAttribute createMagicCookieAttribute()
{
MagicCookieAttribute attribute = new MagicCookieAttribute();
return attribute;
}
/**
* Creates a DestinationFromAddressAttribute of the specified type and with
* the specified address and port
*
* @param address the address value of the address attribute
* @return the newly created address attribute.
*/
public static DestinationAddressAttribute createDestinationAddressAttribute(
TransportAddress address)
{
DestinationAddressAttribute attribute =
new DestinationAddressAttribute();
attribute.setAddress(address);
return attribute;
}
} }

View File

@ -34,7 +34,7 @@ import org.ice4j.*;
*/ */
public class ChangeRequestAttribute public class ChangeRequestAttribute
extends org.ice4j.attribute.Attribute extends Attribute
{ {
/** /**
* Attribute name. * Attribute name.
@ -126,21 +126,21 @@ public class ChangeRequestAttribute
byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH]; byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH];
//Type //Type
binValue[0] = (byte)(getAttributeType()>>8); binValue[0] = (byte)(getAttributeType() >> 8);
binValue[1] = (byte)(getAttributeType()&0x00FF); binValue[1] = (byte)(getAttributeType() & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
//Data //Data
binValue[4] = 0x00; binValue[4] = 0x00;
binValue[5] = 0x00; binValue[5] = 0x00;
binValue[6] = 0x00; binValue[6] = 0x00;
binValue[7] = (byte)((getChangeIpFlag()?4:0) + (getChangePortFlag()?2:0)); binValue[7] = (byte)((getChangeIpFlag() ? 4 : 0) +
(getChangePortFlag() ? 2 : 0));
return binValue; return binValue;
} }
//========================= set/get methods //========================= set/get methods
/** /**
* Sets the value of the changeIpFlag. The "change IP" flag, if true, * Sets the value of the changeIpFlag. The "change IP" flag, if true,
@ -155,7 +155,7 @@ public class ChangeRequestAttribute
} }
/** /**
* Returns tha value of the changeIpFlag. The "change IP" flag, if true, * Returns the value of the changeIpFlag. The "change IP" flag, if true,
* requests the server to send the Binding Response with a different IP * requests the server to send the Binding Response with a different IP
* address than the one the Binding Request was received on. * address than the one the Binding Request was received on.
* *
@ -179,7 +179,7 @@ public class ChangeRequestAttribute
} }
/** /**
* Returns the value of the changePortFlag. The "change port" flag. If true, * Returns the value of the changePortFlag. The "change port" flag. If true,
* requests the server to send the Binding Response with a different port * requests the server to send the Binding Response with a different port
* than the one the Binding Request was received on. * than the one the Binding Request was received on.
* *
@ -201,14 +201,11 @@ public class ChangeRequestAttribute
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attrubteValue contains invalid data. * @throws StunException if attrubteValue contains invalid data.
*/ */
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws void decodeAttributeBody(byte[] attributeValue, char offset, char length)
StunException throws StunException
{ {
offset += 3; // first three bytes of change req att are not used offset += 3; // first three bytes of change req att are not used
setChangeIpFlag((attributeValue[offset]&4)>0); setChangeIpFlag((attributeValue[offset] & 4) > 0);
setChangePortFlag((attributeValue[offset] & 0x2)>0); setChangePortFlag((attributeValue[offset] & 0x2) > 0);
} }
} }

View File

@ -6,7 +6,6 @@
*/ */
package org.ice4j.attribute; package org.ice4j.attribute;
/** /**
* The CHANGED-ADDRESS attribute indicates the IP address and port where * The CHANGED-ADDRESS attribute indicates the IP address and port where
* responses would have been sent from if the "change IP" and "change * responses would have been sent from if the "change IP" and "change
@ -32,5 +31,4 @@ public class ChangedAddressAttribute extends AddressAttribute
{ {
super(CHANGED_ADDRESS); super(CHANGED_ADDRESS);
} }
} }

View File

@ -94,8 +94,8 @@ public class ChannelNumberAttribute extends Attribute
byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH]; byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH];
//Type //Type
binValue[0] = (byte)(getAttributeType()>>8); binValue[0] = (byte)(getAttributeType() >> 8);
binValue[1] = (byte)(getAttributeType()&0x00FF); binValue[1] = (byte)(getAttributeType() & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength() >> 8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength() & 0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
@ -118,14 +118,16 @@ public class ChannelNumberAttribute extends Attribute
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attrubteValue contains invalid data. * @throws StunException if attrubteValue contains invalid data.
*/ */
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws StunException void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{ {
if(length != 4) if(length != 4)
{ {
throw new StunException("length invalid"); throw new StunException("length invalid");
} }
channelNumber = ((char)((attributeValue[0] << 8 ) | (attributeValue[1]&0xFF) )); channelNumber =
((char)((attributeValue[0] << 8 ) | (attributeValue[1] & 0xFF) ));
} }
/** /**
@ -146,4 +148,3 @@ public class ChannelNumberAttribute extends Attribute
return channelNumber; return channelNumber;
} }
} }

View File

@ -51,8 +51,8 @@ public class DataAttribute
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attributeValue contains invalid data. * @throws StunException if attributeValue contains invalid data.
*/ */
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws void decodeAttributeBody(byte[] attributeValue, char offset, char length)
StunException throws StunException
{ {
data = new byte[length]; data = new byte[length];
System.arraycopy(attributeValue, offset, data, 0, length); System.arraycopy(attributeValue, offset, data, 0, length);
@ -65,15 +65,16 @@ public class DataAttribute
public byte[] encode() public byte[] encode()
{ {
char type = getAttributeType(); char type = getAttributeType();
byte binValue[] = new byte[HEADER_LENGTH + getDataLength() + (getDataLength() % 4)]; byte binValue[] = new byte[HEADER_LENGTH + getDataLength() +
(getDataLength() % 4)];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type>>8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
//data //data
System.arraycopy(data, 0, binValue, 4, getDataLength()); System.arraycopy(data, 0, binValue, 4, getDataLength());
@ -129,8 +130,8 @@ public class DataAttribute
} }
/** /**
* Compares two STUN Attributes. Two attributes are considered equal when they * Compares two STUN Attributes. Two attributes are considered equal when
* have the same type length and value. * they have the same type length and value.
* @param obj the object to compare this attribute with. * @param obj the object to compare this attribute with.
* @return true if the attributes are equal and false otherwise. * @return true if the attributes are equal and false otherwise.
*/ */
@ -153,4 +154,3 @@ public class DataAttribute
return true; return true;
} }
} }

View File

@ -0,0 +1,30 @@
/*
* 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;
/**
* The DESTINATION-ADDRESS is present in Send Requests of old TURN versions.
* It specifies the address and port where the data is to be sent. It is encoded
* in the same way as MAPPED-ADDRESS.
*
* @author Sebastien Vincent
*/
public class DestinationAddressAttribute extends AddressAttribute
{
/**
* Attribute name.
*/
public static final String NAME = "DESTINATION-ADDRESS";
/**
* Constructor.
*/
DestinationAddressAttribute()
{
super(DESTINATION_ADDRESS);
}
}

View File

@ -82,8 +82,8 @@ public class DontFragmentAttribute extends Attribute
byte binValue[] = new byte[HEADER_LENGTH]; byte binValue[] = new byte[HEADER_LENGTH];
//Type //Type
binValue[0] = (byte)(getAttributeType()>>8); binValue[0] = (byte)(getAttributeType() >> 8);
binValue[1] = (byte)(getAttributeType()&0x00FF); binValue[1] = (byte)(getAttributeType() & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength() >> 8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength() & 0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
@ -101,7 +101,8 @@ public class DontFragmentAttribute extends Attribute
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attrubteValue contains invalid data. * @throws StunException if attrubteValue contains invalid data.
*/ */
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws StunException void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{ {
if(length != 0) if(length != 0)
{ {

View File

@ -157,9 +157,9 @@ public class ErrorCodeAttribute extends Attribute
/** /**
* A convenience method that sets error class and number according to the * A convenience method that sets error class and number according to the
* specified errorCode.The class represents the hundreds digit of the error code. * specified errorCode.The class represents the hundreds digit of the error
* The value MUST be between 1 and 6. The number represents the response * code. The value MUST be between 1 and 6. The number represents the
* code modulo 100, and its value MUST be between 0 and 99. * response code modulo 100, and its value MUST be between 0 and 99.
* *
* @param errorCode the errorCode that this class encapsulates. * @param errorCode the errorCode that this class encapsulates.
* @throws IllegalArgumentException if errorCode is not a valid error code. * @throws IllegalArgumentException if errorCode is not a valid error code.
@ -167,7 +167,7 @@ public class ErrorCodeAttribute extends Attribute
public void setErrorCode(char errorCode) public void setErrorCode(char errorCode)
throws IllegalArgumentException throws IllegalArgumentException
{ {
setErrorClass((byte)(errorCode/100)); setErrorClass((byte)(errorCode / 100));
setErrorNumber((byte)(errorCode % 100)); setErrorNumber((byte)(errorCode % 100));
} }
@ -178,7 +178,7 @@ public class ErrorCodeAttribute extends Attribute
*/ */
public char getErrorCode() public char getErrorCode()
{ {
return (char)(getErrorClass()*100 + getErrorNumber()); return (char)(getErrorClass() * 100 + getErrorNumber());
} }
/** /**
@ -190,9 +190,11 @@ public class ErrorCodeAttribute extends Attribute
public void setErrorNumber(byte errorNumber) public void setErrorNumber(byte errorNumber)
throws IllegalArgumentException throws IllegalArgumentException
{ {
if(errorNumber < 0 || errorNumber > 99) /*
if(errorNumber < 0 || errorNumber > 9999)
throw new IllegalArgumentException( throw new IllegalArgumentException(
errorNumber + " is not a valid error number!"); errorNumber + " is not a valid error number!");
*/
this.errorNumber = errorNumber; this.errorNumber = errorNumber;
} }
@ -312,8 +314,8 @@ public class ErrorCodeAttribute extends Attribute
*/ */
public char getDataLength() public char getDataLength()
{ {
char len = (char)( 4 //error code numbers char len = (char)(4 //error code numbers
+ (char)(reasonPhrase == null? 0:reasonPhrase.length)); + (char)(reasonPhrase == null ? 0 : reasonPhrase.length));
return len; return len;
} }
@ -326,7 +328,7 @@ public class ErrorCodeAttribute extends Attribute
{ {
byte binValue[] = new byte[HEADER_LENGTH + getDataLength() byte binValue[] = new byte[HEADER_LENGTH + getDataLength()
//add padding //add padding
+ (4 - getDataLength()%4)%4]; + (4 - getDataLength() % 4) % 4];
//Type //Type
binValue[0] = (byte) (getAttributeType() >> 8); binValue[0] = (byte) (getAttributeType() >> 8);
@ -404,6 +406,5 @@ public class ErrorCodeAttribute extends Attribute
System.arraycopy(attributeValue, offset, reasonBytes, System.arraycopy(attributeValue, offset, reasonBytes,
0, reasonBytes.length); 0, reasonBytes.length);
setReasonPhrase(new String(reasonBytes)); setReasonPhrase(new String(reasonBytes));
} }
} }

View File

@ -98,8 +98,8 @@ public class EvenPortAttribute extends Attribute
byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH]; byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH];
//Type //Type
binValue[0] = (byte)(getAttributeType()>>8); binValue[0] = (byte)(getAttributeType() >> 8);
binValue[1] = (byte)(getAttributeType()&0x00FF); binValue[1] = (byte)(getAttributeType() & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength() >> 8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength() & 0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
@ -119,7 +119,8 @@ public class EvenPortAttribute extends Attribute
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attrubteValue contains invalid data. * @throws StunException if attrubteValue contains invalid data.
*/ */
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws StunException void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{ {
if(length != 4) if(length != 4)
{ {
@ -147,4 +148,3 @@ public class EvenPortAttribute extends Attribute
return rFlag; return rFlag;
} }
} }

View File

@ -61,7 +61,6 @@ public class FingerprintAttribute
*/ */
private byte[] crc; private byte[] crc;
/** /**
* Creates a <tt>FingerPrintAttribute</tt> instance. * Creates a <tt>FingerPrintAttribute</tt> instance.
*/ */
@ -81,6 +80,7 @@ public class FingerprintAttribute
{ {
return crc; return crc;
} }
/** /**
* Returns the length of this attribute's body. * Returns the length of this attribute's body.
* *
@ -140,7 +140,7 @@ public class FingerprintAttribute
{ {
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
"ContentDependentAttributes should be encoded " "ContentDependentAttributes should be encoded "
+"through the contend-dependent encode method"); + "through the contend-dependent encode method");
} }
/** /**
@ -165,11 +165,11 @@ public class FingerprintAttribute
byte binValue[] = new byte[HEADER_LENGTH + getDataLength()]; byte binValue[] = new byte[HEADER_LENGTH + getDataLength()];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
//calculate the check sum //calculate the check sum
byte[] xorCrc32 = calculateXorCRC32(content, offset, length); byte[] xorCrc32 = calculateXorCRC32(content, offset, length);
@ -246,4 +246,3 @@ public class FingerprintAttribute
return xorCRC32; return xorCRC32;
} }
} }

View File

@ -59,11 +59,11 @@ public abstract class IceControlAttribute
void decodeAttributeBody(byte[] attributeValue, char offset, char length) void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException throws StunException
{ {
// array used to hold the intermediate long values reconstructed from the // array used to hold the intermediate long values reconstructed from
// attributeValue array // the attributeValue array
// Reading in the network byte order (Big-Endian) // Reading in the network byte order (Big-Endian)
tieBreaker= ((attributeValue[offset++] & 0xffl) << 56) tieBreaker = ((attributeValue[offset++] & 0xffl) << 56)
| ((attributeValue[offset++] & 0xffl) << 48) | ((attributeValue[offset++] & 0xffl) << 48)
| ((attributeValue[offset++] & 0xffl) << 40) | ((attributeValue[offset++] & 0xffl) << 40)
| ((attributeValue[offset++] & 0xffl) << 32) | ((attributeValue[offset++] & 0xffl) << 32)
@ -84,12 +84,12 @@ public abstract class IceControlAttribute
byte[] binValue = new byte[HEADER_LENGTH + getDataLength()]; byte[] binValue = new byte[HEADER_LENGTH + getDataLength()];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
//Tie-Breaker //Tie-Breaker
binValue[4] = (byte)((tieBreaker & 0xFF00000000000000L) >> 56); binValue[4] = (byte)((tieBreaker & 0xFF00000000000000L) >> 56);

View File

@ -14,7 +14,8 @@ import org.ice4j.*;
* *
* @author Sebastien Vincent * @author Sebastien Vincent
*/ */
public class LifetimeAttribute extends Attribute public class LifetimeAttribute
extends Attribute
{ {
/** /**
* Attribute name. * Attribute name.
@ -55,8 +56,8 @@ public class LifetimeAttribute extends Attribute
return true; return true;
LifetimeAttribute att = (LifetimeAttribute) obj; LifetimeAttribute att = (LifetimeAttribute) obj;
if (att.getAttributeType() != getAttributeType() if (att.getAttributeType() != getAttributeType()
|| att.getDataLength() != getDataLength() || att.getDataLength() != getDataLength()
/* compare data */ /* compare data */
|| att.lifetime != lifetime || att.lifetime != lifetime
) )
@ -94,8 +95,8 @@ public class LifetimeAttribute extends Attribute
byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH]; byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH];
//Type //Type
binValue[0] = (byte)(getAttributeType()>>8); binValue[0] = (byte)(getAttributeType() >> 8);
binValue[1] = (byte)(getAttributeType()&0x00FF); binValue[1] = (byte)(getAttributeType() & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength() >> 8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength() & 0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
@ -118,15 +119,18 @@ public class LifetimeAttribute extends Attribute
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attrubteValue contains invalid data. * @throws StunException if attrubteValue contains invalid data.
*/ */
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws StunException void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{ {
if(length != 4) if(length != 4)
{ {
throw new StunException("length invalid"); throw new StunException("length invalid");
} }
lifetime = ((attributeValue[0] << 24) & 0xff000000) + ((attributeValue[1] << 16) & 0x00ff0000) lifetime = ((attributeValue[0] << 24) & 0xff000000) +
+ ((attributeValue[2] << 8) & 0x0000ff00) + (attributeValue[3] & 0x000000ff); ((attributeValue[1] << 16) & 0x00ff0000) +
((attributeValue[2] << 8) & 0x0000ff00) +
(attributeValue[3] & 0x000000ff);
} }
/** /**
@ -147,4 +151,3 @@ public class LifetimeAttribute extends Attribute
return lifetime; return lifetime;
} }
} }

View File

@ -0,0 +1,141 @@
/*
* 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.*;
/**
* The MAGIC-COOKIE attribute.
*
* It is used with old version of TURN (Google, Live messenger variant, ...).
*
* @author Sebastien Vincent
*/
public class MagicCookieAttribute
extends Attribute
{
/**
* Attribute name.
*/
public static final String NAME = "MAGIC-COOKIE";
/**
* The length of the data contained by this attribute.
*/
public static final char DATA_LENGTH = 4;
/**
* Magic cookie value.
*/
private int value = 0x72c64bc6;
/**
* Constructor.
*/
MagicCookieAttribute()
{
super(MAGIC_COOKIE);
}
/**
* 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.
*/
public String getName()
{
return NAME;
}
/**
* Returns the length of this attribute's body.
*
* @return the length of this attribute's value (8 bytes).
*/
public char getDataLength()
{
return DATA_LENGTH;
}
/**
* 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 LifetimeAttribute)
|| obj == null)
return false;
if (obj == this)
return true;
MagicCookieAttribute att = (MagicCookieAttribute) obj;
if (att.getAttributeType() != getAttributeType()
|| att.getDataLength() != getDataLength()
/* compare data */
|| att.value != value
)
return false;
return true;
}
/**
* Returns a binary representation of this attribute.
*
* @return a binary representation of this attribute.
*/
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)((value >> 24) & 0xff);
binValue[5] = (byte)((value >> 16) & 0xff);
binValue[6] = (byte)((value >> 8) & 0xff);
binValue[7] = (byte)((value) & 0xff);
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.
*/
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{
if(length != 4)
{
throw new StunException("length invalid");
}
value = ((attributeValue[0] << 24) & 0xff000000) +
((attributeValue[1] << 16) & 0x00ff0000) +
((attributeValue[2] << 8) & 0x0000ff00) +
(attributeValue[3] & 0x000000ff);
}
}

View File

@ -27,7 +27,6 @@ package org.ice4j.attribute;
* *
* @author Emil Ivov * @author Emil Ivov
*/ */
public class MappedAddressAttribute extends AddressAttribute public class MappedAddressAttribute extends AddressAttribute
{ {
/** /**
@ -35,9 +34,11 @@ public class MappedAddressAttribute extends AddressAttribute
*/ */
public static final String NAME = "MAPPED-ADDRESS"; public static final String NAME = "MAPPED-ADDRESS";
/**
* Constructor.
*/
MappedAddressAttribute() MappedAddressAttribute()
{ {
super(MAPPED_ADDRESS); super(MAPPED_ADDRESS);
} }
} }

View File

@ -82,7 +82,6 @@ public class MessageIntegrityAttribute
*/ */
public static final String NAME = "MESSAGE_INTEGRITY"; public static final String NAME = "MESSAGE_INTEGRITY";
/** /**
* The HMAC-SHA1 algorithm. * The HMAC-SHA1 algorithm.
*/ */
@ -239,7 +238,7 @@ public class MessageIntegrityAttribute
{ {
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
"ContentDependentAttributes should be encoded " "ContentDependentAttributes should be encoded "
+"through the contend-dependent encode method"); + "through the contend-dependent encode method");
} }
/** /**
@ -264,12 +263,12 @@ public class MessageIntegrityAttribute
byte binValue[] = new byte[HEADER_LENGTH + getDataLength()]; byte binValue[] = new byte[HEADER_LENGTH + getDataLength()];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
byte[] key = null; byte[] key = null;
char msgType = (char)((content[0] << 8) + content[1]); char msgType = (char)((content[0] << 8) + content[1]);
@ -340,4 +339,3 @@ public class MessageIntegrityAttribute
return true; return true;
} }
} }

View File

@ -63,12 +63,12 @@ public class NonceAttribute extends Attribute
= new byte[HEADER_LENGTH + getDataLength() + (getDataLength() % 4)]; = new byte[HEADER_LENGTH + getDataLength() + (getDataLength() % 4)];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
/* nonce */ /* nonce */
System.arraycopy(nonce, 0, binValue, 4, (int)getDataLength()); System.arraycopy(nonce, 0, binValue, 4, (int)getDataLength());

View File

@ -12,13 +12,12 @@ import org.ice4j.*;
/** /**
* This class is used for representing attributes not explicitly supported by * This class is used for representing attributes not explicitly supported by
* the stack. Such attributes will generally be kept in binary form and won't be * the stack. Such attributes will generally be kept in binary form and won't
* subdued to any processing by the stack. One could use this class for both * be subdued to any processing by the stack. One could use this class for both
* dealing with attributes in received messages, and generating messages * dealing with attributes in received messages, and generating messages
* containing attributes not explicitly supported by the stack. * containing attributes not explicitly supported by the stack.
* *
* @author Emil Ivov * @author Emil Ivov
*
*/ */
public class OptionalAttribute public class OptionalAttribute
extends Attribute extends Attribute
@ -44,7 +43,8 @@ public class OptionalAttribute
throws StunException throws StunException
{ {
this.attributeValue = new byte[length]; this.attributeValue = new byte[length];
System.arraycopy(attributeValue, offset, this.attributeValue, 0, length); System.arraycopy(attributeValue, offset, this.attributeValue, 0,
length);
} }
/** /**
@ -59,11 +59,11 @@ public class OptionalAttribute
byte binValue[] = new byte[HEADER_LENGTH + attributeValue.length]; byte binValue[] = new byte[HEADER_LENGTH + attributeValue.length];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
System.arraycopy(attributeValue, 0, System.arraycopy(attributeValue, 0,
binValue, HEADER_LENGTH, attributeValue.length); binValue, HEADER_LENGTH, attributeValue.length);
@ -114,7 +114,6 @@ public class OptionalAttribute
System.arraycopy(body, offset, this.attributeValue, 0, length); System.arraycopy(body, offset, this.attributeValue, 0, length);
} }
/** /**
* Compares two STUN Attributes. Two attributes are considered equal when they * Compares two STUN Attributes. Two attributes are considered equal when they
* have the same type length and value. * have the same type length and value.

View File

@ -57,8 +57,8 @@ public class PriorityAttribute
throws StunException throws StunException
{ {
// array used to hold the intermediate long values reconstructed from the // array used to hold the intermediate long values reconstructed from
// attributeValue array // the attributeValue array
long[] values = new long[4]; long[] values = new long[4];
// Reading in the network byte order (Big-Endian) // Reading in the network byte order (Big-Endian)
@ -82,12 +82,12 @@ public class PriorityAttribute
byte[] binValue = new byte[HEADER_LENGTH + getDataLength()]; byte[] binValue = new byte[HEADER_LENGTH + getDataLength()];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
//Priority //Priority
binValue[4] = (byte)((priority & 0xFF000000L) >> 24); binValue[4] = (byte)((priority & 0xFF000000L) >> 24);
@ -96,12 +96,11 @@ public class PriorityAttribute
binValue[7] = (byte)(priority & 0x000000FFL); binValue[7] = (byte)(priority & 0x000000FFL);
return binValue; return binValue;
} }
/** /**
* Compares two STUN Attributes. Two attributes are considered equal when they * Compares two STUN Attributes. Two attributes are considered equal when
* have the same type length and value. * they have the same type length and value.
* *
* @param obj the object to compare this attribute with. * @param obj the object to compare this attribute with.
* @return true if the attributes are equal and false otherwise. * @return true if the attributes are equal and false otherwise.
@ -173,5 +172,4 @@ public class PriorityAttribute
else else
this.priority = priority; this.priority = priority;
} }
} }

View File

@ -11,7 +11,7 @@ import java.util.*;
import org.ice4j.*; import org.ice4j.*;
/** /**
* The REALM attribute contains a text whcih meets the grammar for * The REALM attribute contains a text which meets the grammar for
* "realm value" as described in RFC3261 but without the double quotes. * "realm value" as described in RFC3261 but without the double quotes.
* *
* @author Sebastien Vincent * @author Sebastien Vincent
@ -46,12 +46,12 @@ public class RealmAttribute extends Attribute
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attributeValue contains invalid data. * @throws StunException if attributeValue contains invalid data.
*/ */
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws void decodeAttributeBody(byte[] attributeValue, char offset, char length)
StunException throws StunException
{ {
realm = new byte[length]; realm = new byte[length];
System.arraycopy(attributeValue, offset, realm, 0, length); System.arraycopy(attributeValue, offset, realm, 0, length);
} }
/** /**
* Returns a binary representation of this attribute. * Returns a binary representation of this attribute.
@ -60,15 +60,16 @@ public class RealmAttribute extends Attribute
public byte[] encode() public byte[] encode()
{ {
char type = getAttributeType(); char type = getAttributeType();
byte binValue[] = new byte[HEADER_LENGTH + getDataLength() + (getDataLength() % 4)]; byte binValue[] = new byte[HEADER_LENGTH + getDataLength() +
(getDataLength() % 4)];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
/* realm */ /* realm */
System.arraycopy(realm, 0, binValue, 4, getDataLength()); System.arraycopy(realm, 0, binValue, 4, getDataLength());
@ -144,4 +145,3 @@ public class RealmAttribute extends Attribute
return true; return true;
} }
} }

View File

@ -18,7 +18,6 @@ package org.ice4j.attribute;
* *
* @author Emil Ivov * @author Emil Ivov
*/ */
public class ReflectedFromAttribute extends AddressAttribute public class ReflectedFromAttribute extends AddressAttribute
{ {
/** /**
@ -33,5 +32,4 @@ public class ReflectedFromAttribute extends AddressAttribute
{ {
super(REFLECTED_FROM); super(REFLECTED_FROM);
} }
} }

View File

@ -0,0 +1,30 @@
/*
* 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;
/**
* The REMOTE-ADDRESS is present in Data Indication of old TURN versions.
* It specifies the address and port where the data is sent. It is encoded
* in the same way as MAPPED-ADDRESS.
*
* @author Sebastien Vincent
*/
public class RemoteAddressAttribute extends AddressAttribute
{
/**
* Attribute name.
*/
public static final String NAME = "REMOTE-ADDRESS";
/**
* Constructor.
*/
RemoteAddressAttribute()
{
super(REMOTE_ADDRESS);
}
}

View File

@ -100,8 +100,8 @@ public class RequestedTransportAttribute extends Attribute
byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH]; byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH];
//Type //Type
binValue[0] = (byte)(getAttributeType()>>8); binValue[0] = (byte)(getAttributeType() >> 8);
binValue[1] = (byte)(getAttributeType()&0x00FF); binValue[1] = (byte)(getAttributeType() & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength() >> 8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength() & 0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
@ -124,7 +124,8 @@ public class RequestedTransportAttribute extends Attribute
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attrubteValue contains invalid data. * @throws StunException if attrubteValue contains invalid data.
*/ */
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws StunException void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{ {
if(length != 4) if(length != 4)
{ {
@ -152,4 +153,3 @@ public class RequestedTransportAttribute extends Attribute
return transportProtocol; return transportProtocol;
} }
} }

View File

@ -17,145 +17,144 @@ import org.ice4j.*;
* @author Sebastien Vincent * @author Sebastien Vincent
*/ */
public class ReservationTokenAttribute public class ReservationTokenAttribute
extends Attribute extends Attribute
{ {
/** /**
* Attribute name. * Attribute name.
*/ */
public static final String NAME = "RESERVATION-TOKEN"; public static final String NAME = "RESERVATION-TOKEN";
/** /**
* ReservationToken value. * ReservationToken value.
*/ */
private byte reservationToken[] = null; private byte reservationToken[] = null;
/** /**
* Constructor. * Constructor.
*/ */
protected ReservationTokenAttribute () protected ReservationTokenAttribute ()
{
super(RESERVATION_TOKEN);
}
/**
* Copies the value of the reservationToken attribute from the specified
* attributeValue.
* @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 reservationToken.
*/
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws
StunException
{ {
if(length != 8) super(RESERVATION_TOKEN);
{ }
throw new StunException("Length mismatch!");
/**
* Copies the value of the reservationToken attribute from the specified
* attributeValue.
* @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 reservationToken.
*/
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{
if(length != 8)
{
throw new StunException("Length mismatch!");
}
reservationToken = new byte[8];
System.arraycopy(attributeValue, offset, reservationToken, 0, 8);
}
/**
* 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 + 8];
//Type
binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type & 0x00FF);
//Length
binValue[2] = (byte)(8 >> 8);
binValue[3] = (byte)(8 & 0x00FF);
//reservationToken
System.arraycopy(reservationToken, 0, binValue, 4, 8);
return binValue;
} }
reservationToken = new byte[8]; /**
System.arraycopy(attributeValue, offset, reservationToken, 0, 8); * Returns the human readable name of this attribute.
} *
* @return this attribute's name.
/** */
* Returns a binary representation of this attribute. public String getName()
* @return a binary representation of this attribute.
*/
public byte[] encode()
{
char type = getAttributeType();
byte binValue[] = new byte[HEADER_LENGTH + 8];
//Type
binValue[0] = (byte)(type>>8);
binValue[1] = (byte)(type&0x00FF);
//Length
binValue[2] = (byte)(8 >> 8);
binValue[3] = (byte)(8 & 0x00FF);
//reservationToken
System.arraycopy(reservationToken, 0, binValue, 4, 8);
return binValue;
}
/**
* Returns the human readable name of this attribute.
*
* @return this attribute's name.
*/
public String getName()
{
return NAME;
}
/**
* Returns a (cloned) byte array containg the reservationToken value of the reservationToken
* attribute.
* @return the binary array containing the reservationToken.
*/
public byte[] getReservationToken()
{
if (reservationToken == null)
return null;
byte[] copy = new byte[reservationToken.length];
System.arraycopy(reservationToken, 0, copy, 0, reservationToken.length);
return reservationToken;
}
/**
* Copies the specified binary array into the the reservationToken value of the reservationToken
* attribute.
* @param reservationToken the binary array containing the reservationToken.
*/
public void setReservationToken(byte[] reservationToken)
{
if (reservationToken == null)
{ {
this.reservationToken = null; return NAME;
return;
} }
this.reservationToken = new byte[reservationToken.length]; /**
System.arraycopy(reservationToken, 0, this.reservationToken, 0, reservationToken.length); * Returns a (cloned) byte array containing the reservationToken value of
} * the reservationToken attribute.
* @return the binary array containing the reservationToken.
*/
public byte[] getReservationToken()
{
if (reservationToken == null)
return null;
/** byte[] copy = new byte[reservationToken.length];
* Returns the length of this attribute's body. System.arraycopy(reservationToken, 0, copy, 0, reservationToken.length);
* @return the length of this attribute's value. return reservationToken;
*/ }
public char getDataLength()
{
return (char)reservationToken.length;
}
/** /**
* Compares two STUN Attributes. Two attributes are considered equal when they * Copies the specified binary array into the the reservationToken value of
* have the same type length and value. * the reservationToken attribute.
* @param obj the object to compare this attribute with. * @param reservationToken the binary array containing the reservationToken.
* @return true if the attributes are equal and false otherwise. */
*/ public void setReservationToken(byte[] reservationToken)
{
if (reservationToken == null)
{
this.reservationToken = null;
return;
}
public boolean equals(Object obj) this.reservationToken = new byte[reservationToken.length];
{ System.arraycopy(reservationToken, 0, this.reservationToken, 0,
if (! (obj instanceof ReservationTokenAttribute) reservationToken.length);
|| obj == null) }
return false;
if (obj == this) /**
return true; * Returns the length of this attribute's body.
* @return the length of this attribute's value.
*/
public char getDataLength()
{
return (char)reservationToken.length;
}
ReservationTokenAttribute att = (ReservationTokenAttribute) obj; /**
if (att.getAttributeType() != getAttributeType() * Compares two STUN Attributes. Two attributes are considered equal when
|| att.getDataLength() != getDataLength() * they have the same type length and value.
|| !Arrays.equals( att.reservationToken, reservationToken)) * @param obj the object to compare this attribute with.
return false; * @return true if the attributes are equal and false otherwise.
*/
public boolean equals(Object obj)
{
if (! (obj instanceof ReservationTokenAttribute)
|| obj == null)
return false;
return true; if (obj == this)
} return true;
ReservationTokenAttribute att = (ReservationTokenAttribute) obj;
if (att.getAttributeType() != getAttributeType()
|| att.getDataLength() != getDataLength()
|| !Arrays.equals( att.reservationToken, reservationToken))
return false;
return true;
}
} }

View File

@ -28,5 +28,4 @@ public class ResponseAddressAttribute extends AddressAttribute
{ {
super(RESPONSE_ADDRESS); super(RESPONSE_ADDRESS);
} }
} }

View File

@ -62,15 +62,15 @@ public class SoftwareAttribute
char type = getAttributeType(); char type = getAttributeType();
byte binValue[] = new byte[HEADER_LENGTH + getDataLength() byte binValue[] = new byte[HEADER_LENGTH + getDataLength()
//add padding //add padding
+ (4 - getDataLength()%4)%4]; + (4 - getDataLength() % 4) % 4];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
//software //software
System.arraycopy(software, 0, binValue, 4, getDataLength()); System.arraycopy(software, 0, binValue, 4, getDataLength());
@ -99,7 +99,7 @@ public class SoftwareAttribute
} }
/** /**
* Returns a (cloned) byte array containg the data value of the software * Returns a (cloned) byte array containing the data value of the software
* attribute. * attribute.
* @return the binary array containing the software. * @return the binary array containing the software.
*/ */
@ -132,13 +132,12 @@ public class SoftwareAttribute
} }
/** /**
* Compares two STUN Attributes. Two attributes are considered equal when they * Compares two STUN Attributes. Two attributes are considered equal when
* have the same type length and value. * they have the same type length and value.
* *
* @param obj the object to compare this attribute with. * @param obj the object to compare this attribute with.
* @return true if the attributes are equal and false otherwise. * @return true if the attributes are equal and false otherwise.
*/ */
public boolean equals(Object obj) public boolean equals(Object obj)
{ {
if (! (obj instanceof SoftwareAttribute) if (! (obj instanceof SoftwareAttribute)
@ -156,5 +155,4 @@ public class SoftwareAttribute
return true; return true;
} }
} }

View File

@ -13,7 +13,6 @@ package org.ice4j.attribute;
* *
* @author Emil Ivov * @author Emil Ivov
*/ */
public class SourceAddressAttribute extends AddressAttribute public class SourceAddressAttribute extends AddressAttribute
{ {
/** /**

View File

@ -76,7 +76,7 @@ public class UnknownAttributesAttribute extends Attribute
if( (len % 2 ) != 0 ) if( (len % 2 ) != 0 )
len++; len++;
return (char)(len*2); return (char)(len * 2);
} }
/** /**
@ -111,7 +111,6 @@ public class UnknownAttributesAttribute extends Attribute
public Iterator<Character> getAttributes() public Iterator<Character> getAttributes()
{ {
return unknownAttributes.iterator(); return unknownAttributes.iterator();
} }
/** /**
@ -152,9 +151,10 @@ public class UnknownAttributesAttribute extends Attribute
Iterator<Character> attributes = getAttributes(); Iterator<Character> attributes = getAttributes();
while (attributes.hasNext()) { while (attributes.hasNext())
{
char att = attributes.next().charValue(); char att = attributes.next().charValue();
binValue[offset++] = (byte)(att>>8); binValue[offset++] = (byte)(att >> 8);
binValue[offset++] = (byte)(att & 0x00FF); binValue[offset++] = (byte)(att & 0x00FF);
} }
@ -168,7 +168,6 @@ public class UnknownAttributesAttribute extends Attribute
binValue[offset++] = (byte) (att & 0x00FF); binValue[offset++] = (byte) (att & 0x00FF);
} }
return binValue; return binValue;
} }
@ -196,7 +195,6 @@ public class UnknownAttributesAttribute extends Attribute
return false; return false;
return true; return true;
} }
/** /**
@ -210,17 +208,17 @@ public class UnknownAttributesAttribute extends Attribute
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attrubteValue contains invalid data. * @throws StunException if attrubteValue contains invalid data.
*/ */
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws void decodeAttributeBody(byte[] attributeValue, char offset, char length)
StunException throws StunException
{ {
if( (length % 2 ) != 0) if( (length % 2 ) != 0)
throw new StunException("Attribute IDs are 2 bytes long and the " throw new StunException("Attribute IDs are 2 bytes long and the "
+"passed binary array has an odd length value."); + "passed binary array has an odd length " +
"value.");
char originalOffset = offset; char originalOffset = offset;
for(int i = offset; i < originalOffset + length; i+=2) for(int i = offset; i < originalOffset + length; i += 2)
{ {
char attributeID = (char)( (attributeValue[offset++]<<8) | char attributeID = (char)( (attributeValue[offset++] << 8) |
(attributeValue[offset++]) ); (attributeValue[offset++]) );
addAttributeID(attributeID); addAttributeID(attributeID);
} }

View File

@ -11,16 +11,22 @@ import org.ice4j.*;
* so the data length is zero * so the data length is zero
*/ */
public class UseCandidateAttribute public class UseCandidateAttribute
extends Attribute extends Attribute
{ {
private static final char DATA_LENGTH_USE_CANDIDATE = 0; /**
* Data length.
*/
private static final char DATA_LENGTH_USE_CANDIDATE = 0;
protected UseCandidateAttribute() /**
{ * Constructor.
super(USE_CANDIDATE); */
} protected UseCandidateAttribute()
{
super(USE_CANDIDATE);
}
/** /**
* Decodes the USE-CANDIDATE attribute's body, which is empty * Decodes the USE-CANDIDATE attribute's body, which is empty
* *
* @param attributeValue a binary array containing this attribute's * @param attributeValue a binary array containing this attribute's
@ -30,75 +36,74 @@ public class UseCandidateAttribute
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attrubteValue contains invalid data. * @throws StunException if attrubteValue contains invalid data.
*/ */
void decodeAttributeBody(byte[] attributeValue, char offset, char length) void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException throws StunException
{ {
// Do nothing, empty attribute body // Do nothing, empty attribute body
} }
/** /**
* Returns a binary representation of this attribute. * Returns a binary representation of this attribute.
* *
* @return a binary representation of this attribute. * @return a binary representation of this attribute.
*/ */
public byte[] encode() public byte[] encode()
{ {
char type = getAttributeType(); char type = getAttributeType();
byte[] binValue = new byte[HEADER_LENGTH + DATA_LENGTH_USE_CANDIDATE]; byte[] binValue = new byte[HEADER_LENGTH + DATA_LENGTH_USE_CANDIDATE];
// Type // Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
// Length // Length
binValue[2] = (byte)(DATA_LENGTH_USE_CANDIDATE>>8); binValue[2] = (byte)(DATA_LENGTH_USE_CANDIDATE >> 8);
binValue[3] = (byte)(DATA_LENGTH_USE_CANDIDATE&0x00FF); binValue[3] = (byte)(DATA_LENGTH_USE_CANDIDATE & 0x00FF);
return binValue; return binValue;
} }
/** /**
* Compares two STUN Attributes. Two attributes are considered equal when they * Compares two STUN Attributes. Two attributes are considered equal when
* have the same type, length and value. * they have the same type, length and value.
* *
* @param obj the object to compare this attribute with. * @param obj the object to compare this attribute with.
* @return true if the attributes are equal and false otherwise. * @return true if the attributes are equal and false otherwise.
*/ */
public boolean equals(Object obj) public boolean equals(Object obj)
{ {
if(!(obj instanceof UseCandidateAttribute) if(!(obj instanceof UseCandidateAttribute)
|| obj == null) || obj == null)
return false; return false;
if(obj == this) if(obj == this)
return true; return true;
UseCandidateAttribute useCandidateAtt = (UseCandidateAttribute)obj; UseCandidateAttribute useCandidateAtt = (UseCandidateAttribute)obj;
if(useCandidateAtt.getAttributeType() != getAttributeType() if(useCandidateAtt.getAttributeType() != getAttributeType()
|| useCandidateAtt.getDataLength() != getDataLength()) || useCandidateAtt.getDataLength() != getDataLength())
return false; return false;
return true; return true;
} }
/** /**
* Returns the length of this attribute's body. * Returns the length of this attribute's body.
* *
* @return the length of this attribute's value. * @return the length of this attribute's value.
*/ */
public char getDataLength() public char getDataLength()
{ {
return DATA_LENGTH_USE_CANDIDATE; return DATA_LENGTH_USE_CANDIDATE;
} }
/** /**
* Returns the human readable name of this attribute. * Returns the human readable name of this attribute.
* *
* @return this attribute's name. * @return this attribute's name.
*/ */
public String getName() public String getName()
{ {
return "USE-CANDIDATE"; return "USE-CANDIDATE";
} }
} }

View File

@ -61,15 +61,15 @@ public class UsernameAttribute extends Attribute
char type = getAttributeType(); char type = getAttributeType();
byte binValue[] = new byte[HEADER_LENGTH + getDataLength() byte binValue[] = new byte[HEADER_LENGTH + getDataLength()
//add padding //add padding
+ (4 - getDataLength()%4)%4]; + (4 - getDataLength() % 4) % 4];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
//username //username
System.arraycopy(username, 0, binValue, 4, getDataLength()); System.arraycopy(username, 0, binValue, 4, getDataLength());
@ -151,4 +151,3 @@ public class UsernameAttribute extends Attribute
return true; return true;
} }
} }

View File

@ -24,7 +24,9 @@ import org.ice4j.*;
public class XorOnlyAttribute public class XorOnlyAttribute
extends Attribute extends Attribute
{ {
/**
* Constructor.
*/
protected XorOnlyAttribute() protected XorOnlyAttribute()
{ {
super(Attribute.XOR_ONLY); super(Attribute.XOR_ONLY);
@ -57,12 +59,12 @@ public class XorOnlyAttribute
byte binValue[] = new byte[HEADER_LENGTH + getDataLength()]; byte binValue[] = new byte[HEADER_LENGTH + getDataLength()];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type >> 8);
binValue[1] = (byte)(type&0x00FF); binValue[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength()>>8); binValue[2] = (byte)(getDataLength() >> 8);
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength() & 0x00FF);
return binValue; return binValue;
} }

View File

@ -16,17 +16,16 @@ package org.ice4j.attribute;
*/ */
public class XorPeerAddressAttribute extends XorMappedAddressAttribute public class XorPeerAddressAttribute extends XorMappedAddressAttribute
{ {
/** /**
* Attribute name. * Attribute name.
*/ */
public static final String NAME = "XOR-PEER-ADDRESS"; public static final String NAME = "XOR-PEER-ADDRESS";
/** /**
* Constructor. * Constructor.
*/ */
XorPeerAddressAttribute() XorPeerAddressAttribute()
{ {
super(XOR_PEER_ADDRESS); super(XOR_PEER_ADDRESS);
} }
} }

View File

@ -16,17 +16,16 @@ package org.ice4j.attribute;
*/ */
public class XorRelayedAddressAttribute extends XorMappedAddressAttribute public class XorRelayedAddressAttribute extends XorMappedAddressAttribute
{ {
/** /**
* Attribute name. * Attribute name.
*/ */
public static final String NAME = "XOR-RELAYED-ADDRESS"; public static final String NAME = "XOR-RELAYED-ADDRESS";
/** /**
* Constructor. * Constructor.
*/ */
XorRelayedAddressAttribute() XorRelayedAddressAttribute()
{ {
super(XOR_RELAYED_ADDRESS); super(XOR_RELAYED_ADDRESS);
} }
} }