diff --git a/src/org/ice4j/attribute/AddressAttribute.java b/src/org/ice4j/attribute/AddressAttribute.java index 00ce3e1..77970d9 100644 --- a/src/org/ice4j/attribute/AddressAttribute.java +++ b/src/org/ice4j/attribute/AddressAttribute.java @@ -100,7 +100,7 @@ abstract class AddressAttribute extends Attribute || type == SOURCE_ADDRESS || type == CHANGED_ADDRESS || type == REFLECTED_FROM || type == XOR_MAPPED_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()]; //Type - binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); + binValue[0] = (byte)(type >> 8); + binValue[1] = (byte)(type & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); //Not used binValue[4] = 0x00; //Family binValue[5] = getFamily(); //port - binValue[6] = (byte)(getPort()>>8); - binValue[7] = (byte)(getPort()&0x00FF); + binValue[6] = (byte)(getPort() >> 8); + binValue[7] = (byte)(getPort() & 0x00FF); //address if(getFamily() == ADDRESS_FAMILY_IPV6){ @@ -262,7 +262,6 @@ abstract class AddressAttribute extends Attribute return ADDRESS_FAMILY_IPV6; else return ADDRESS_FAMILY_IPV4; - } /** @@ -296,14 +295,16 @@ abstract class AddressAttribute extends Attribute //port char port = ((char)((attributeValue[offset++] << 8 ) - | (attributeValue[offset++]&0xFF) )); + | (attributeValue[offset++] & 0xFF) )); //address byte address[] = null; - if(family == ADDRESS_FAMILY_IPV6){ + if(family == ADDRESS_FAMILY_IPV6) + { address = new byte[16]; } - else{ + else + { //ipv4 address = new byte[4]; } @@ -317,7 +318,5 @@ abstract class AddressAttribute extends Attribute { throw new StunException(e); } - } - } diff --git a/src/org/ice4j/attribute/AlternateServerAttribute.java b/src/org/ice4j/attribute/AlternateServerAttribute.java index 28cfeb7..3d4825b 100644 --- a/src/org/ice4j/attribute/AlternateServerAttribute.java +++ b/src/org/ice4j/attribute/AlternateServerAttribute.java @@ -30,7 +30,6 @@ package org.ice4j.attribute; * * @author Sebastien Vincent */ - public class AlternateServerAttribute extends AddressAttribute { /** diff --git a/src/org/ice4j/attribute/Attribute.java b/src/org/ice4j/attribute/Attribute.java index cab11ca..7ea7731 100644 --- a/src/org/ice4j/attribute/Attribute.java +++ b/src/org/ice4j/attribute/Attribute.java @@ -65,41 +65,182 @@ import org.ice4j.*; public abstract class Attribute { /* STUN attributes */ + /** + * Mapped address attribute. + */ public static final char MAPPED_ADDRESS = 0x0001; + + /** + * Response address attribute. + */ public static final char RESPONSE_ADDRESS = 0x0002; + + /** + * Change request attribute. + */ public static final char CHANGE_REQUEST = 0x0003; + + /** + * Source address attribute. + */ public static final char SOURCE_ADDRESS = 0x0004; + + /** + * Changed address attribute. + */ public static final char CHANGED_ADDRESS = 0x0005; + + /** + * Username attribute. + */ public static final char USERNAME = 0x0006; + + /** + * Password attribute. + */ public static final char PASSWORD = 0x0007; + + /** + * Message integrity attribute. + */ public static final char MESSAGE_INTEGRITY = 0x0008; + + /** + * Error code attribute. + */ public static final char ERROR_CODE = 0x0009; + + /** + * Unknown attributes attribute. + */ public static final char UNKNOWN_ATTRIBUTES = 0x000a; + + /** + * Reflected from attribute. + */ public static final char REFLECTED_FROM = 0x000b; + + /** + * Realm attribute. + */ public static final char REALM = 0x0014; + + /** + * Nonce attribute. + */ public static final char NONCE = 0x0015; + + /** + * XOR Mapped address attribute. + */ public static final char XOR_MAPPED_ADDRESS = 0x0020; + + /** + * XOR only attribute. + */ public static final char XOR_ONLY = 0x0021; + + /** + * Software attribute. + */ public static final char SOFTWARE = 0x8022; + + /** + * Alternate server attribute. + */ public static final char ALTERNATE_SERVER = 0x8023; + + /** + * Fingerprint attribute. + */ public static final char FINGERPRINT = 0x8028; + + /** + * Unknown optional attribute. + */ public static final char UNKNOWN_OPTIONAL_ATTRIBUTE = 0x8000; /* TURN attributes */ + /** + * Channel number attribute. + */ public static final char CHANNEL_NUMBER = 0x000c; + + /** + * Lifetime attribute. + */ public static final char LIFETIME = 0x000d; + + /** + * XOR peer address attribute. + */ public static final char XOR_PEER_ADDRESS = 0x0012; + + /** + * Data attribute. + */ public static final char DATA = 0x0013; + + /** + * XOR relayed address attribute. + */ public static final char XOR_RELAYED_ADDRESS = 0x0016; + + /** + * Even port attribute. + */ public static final char EVEN_PORT = 0x0018; + + /** + * Requested transport attribute. + */ public static final char REQUESTED_TRANSPORT = 0x0019; + + /** + * Don't fragment attribute. + */ public static final char DONT_FRAGMENT = 0x001a; + + /** + * Reservation token attribute. + */ 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 */ + /** + * Priority attribute. + */ public static final char PRIORITY = 0x0024; + + /** + * Use candidate attribute. + */ public static final char USE_CANDIDATE = 0x0025; + + /** + * ICE controlled attribute. + */ public static final char ICE_CONTROLLED = 0x8029; + + /** + * ICE controlling attribute. + */ public static final char ICE_CONTROLLING = 0x802a; /** diff --git a/src/org/ice4j/attribute/AttributeDecoder.java b/src/org/ice4j/attribute/AttributeDecoder.java index c29643e..4e8a1eb 100644 --- a/src/org/ice4j/attribute/AttributeDecoder.java +++ b/src/org/ice4j/attribute/AttributeDecoder.java @@ -42,8 +42,10 @@ public class AttributeDecoder } //Discover attribute type - char attributeType = (char)((bytes[offset]<<8)|bytes[offset + 1]); - char attributeLength = (char)((bytes[offset + 2]<<8)|bytes[offset + 3]); + char attributeType = (char)((bytes[offset] << 8) | bytes[offset + 1]); + int len1 = bytes[offset + 2] & 0xff; + int len2 = bytes[offset + 3] & 0xff; + char attributeLength = (char)((len1 << 8) | len2); if(attributeLength > bytes.length - offset ) throw new StunException( StunException.ILLEGAL_ARGUMENT, diff --git a/src/org/ice4j/attribute/AttributeFactory.java b/src/org/ice4j/attribute/AttributeFactory.java index 11a605d..d1d984a 100644 --- a/src/org/ice4j/attribute/AttributeFactory.java +++ b/src/org/ice4j/attribute/AttributeFactory.java @@ -30,7 +30,7 @@ public class AttributeFactory public static ChangeRequestAttribute createChangeRequestAttribute() { return createChangeRequestAttribute(false, false); - }; + } /** * Creates a ChangeRequestAttribute with the specified flag values. @@ -48,7 +48,7 @@ public class AttributeFactory attribute.setChangePortFlag(changePort); return attribute; - }; + } /** * Creates a changedAddressAttribute of the specified type and with the @@ -65,8 +65,7 @@ public class AttributeFactory attribute.setAddress(address); return attribute; - - }; + } /** * Creates an ErrorCodeAttribute with the specified error class and number @@ -83,7 +82,7 @@ public class AttributeFactory throws StunException { return createErrorCodeAttribute(errorClass, errorNumber, null); - }; + } /** * Creates an ErrorCodeAttribute with the specified error class, number and @@ -114,7 +113,7 @@ public class AttributeFactory : reasonPhrase); return attribute; - }; + } /** * Creates an ErrorCodeAttribute with the specified error code and a default @@ -129,7 +128,7 @@ public class AttributeFactory throws StunException { return createErrorCodeAttribute(errorCode, null); - }; + } /** * Creates an ErrorCodeAttribute with the specified error code and reason @@ -157,7 +156,7 @@ public class AttributeFactory : reasonPhrase); return attribute; - }; + } /** * Creates a MappedAddressAttribute of the specified type and with the @@ -174,8 +173,7 @@ public class AttributeFactory attribute.setAddress(address); return attribute; - - }; + } /** * Creates a ReflectedFromAddressAttribute of the specified type and with @@ -192,8 +190,7 @@ public class AttributeFactory attribute.setAddress(address); return attribute; - - }; + } /** * Creates a ResponseFromAddressAttribute of the specified type and with the @@ -210,8 +207,7 @@ public class AttributeFactory attribute.setAddress(address); return attribute; - - }; + } /** * Creates a SourceFromAddressAttribute of the specified type and with the @@ -228,8 +224,7 @@ public class AttributeFactory attribute.setAddress(address); return attribute; - - }; + } /** * Creates an empty UnknownAttributesAttribute. @@ -241,7 +236,7 @@ public class AttributeFactory UnknownAttributesAttribute attribute = new UnknownAttributesAttribute(); return attribute; - }; + } /** * Creates a XorRelayedAddressAttribute of the specified type and with the @@ -299,8 +294,7 @@ public class AttributeFactory attribute.setAddress(address, tranID); return attribute; - - }; + } /** * Create a UsernameAttribute. @@ -357,7 +351,7 @@ public class AttributeFactory attribute.setUsername(username); return attribute; - }; + } /** * Creates an empty FingerprintAttribute with a 0 check sum. Once @@ -371,7 +365,7 @@ public class AttributeFactory FingerprintAttribute attribute = new FingerprintAttribute(); return attribute; - }; + } /** * Create a ChannelNumberAttribute. @@ -467,7 +461,8 @@ public class AttributeFactory public static RequestedTransportAttribute createRequestedTransportAttribute( byte protocol) { - RequestedTransportAttribute attribute = new RequestedTransportAttribute(); + RequestedTransportAttribute attribute = + new RequestedTransportAttribute(); attribute.setRequestedTransport(protocol); return attribute; @@ -562,4 +557,33 @@ public class AttributeFactory 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; + } } diff --git a/src/org/ice4j/attribute/ChangeRequestAttribute.java b/src/org/ice4j/attribute/ChangeRequestAttribute.java index b92e0db..9043d50 100644 --- a/src/org/ice4j/attribute/ChangeRequestAttribute.java +++ b/src/org/ice4j/attribute/ChangeRequestAttribute.java @@ -34,7 +34,7 @@ import org.ice4j.*; */ public class ChangeRequestAttribute - extends org.ice4j.attribute.Attribute + extends Attribute { /** * Attribute name. @@ -126,21 +126,21 @@ public class ChangeRequestAttribute byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH]; //Type - binValue[0] = (byte)(getAttributeType()>>8); - binValue[1] = (byte)(getAttributeType()&0x00FF); + binValue[0] = (byte)(getAttributeType() >> 8); + binValue[1] = (byte)(getAttributeType() & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); //Data binValue[4] = 0x00; binValue[5] = 0x00; binValue[6] = 0x00; - binValue[7] = (byte)((getChangeIpFlag()?4:0) + (getChangePortFlag()?2:0)); + binValue[7] = (byte)((getChangeIpFlag() ? 4 : 0) + + (getChangePortFlag() ? 2 : 0)); return binValue; } - //========================= set/get methods /** * 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 * 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 * than the one the Binding Request was received on. * @@ -201,14 +201,11 @@ public class ChangeRequestAttribute * @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 + void decodeAttributeBody(byte[] attributeValue, char offset, char length) + throws StunException { offset += 3; // first three bytes of change req att are not used - setChangeIpFlag((attributeValue[offset]&4)>0); - setChangePortFlag((attributeValue[offset] & 0x2)>0); - + setChangeIpFlag((attributeValue[offset] & 4) > 0); + setChangePortFlag((attributeValue[offset] & 0x2) > 0); } - - } diff --git a/src/org/ice4j/attribute/ChangedAddressAttribute.java b/src/org/ice4j/attribute/ChangedAddressAttribute.java index a7c81ea..2bd6404 100644 --- a/src/org/ice4j/attribute/ChangedAddressAttribute.java +++ b/src/org/ice4j/attribute/ChangedAddressAttribute.java @@ -6,7 +6,6 @@ */ package org.ice4j.attribute; - /** * The CHANGED-ADDRESS attribute indicates the IP address and port where * responses would have been sent from if the "change IP" and "change @@ -32,5 +31,4 @@ public class ChangedAddressAttribute extends AddressAttribute { super(CHANGED_ADDRESS); } - } diff --git a/src/org/ice4j/attribute/ChannelNumberAttribute.java b/src/org/ice4j/attribute/ChannelNumberAttribute.java index 3d9463b..cabc9d9 100644 --- a/src/org/ice4j/attribute/ChannelNumberAttribute.java +++ b/src/org/ice4j/attribute/ChannelNumberAttribute.java @@ -94,8 +94,8 @@ public class ChannelNumberAttribute extends Attribute byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH]; //Type - binValue[0] = (byte)(getAttributeType()>>8); - binValue[1] = (byte)(getAttributeType()&0x00FF); + binValue[0] = (byte)(getAttributeType() >> 8); + binValue[1] = (byte)(getAttributeType() & 0x00FF); //Length binValue[2] = (byte)(getDataLength() >> 8); binValue[3] = (byte)(getDataLength() & 0x00FF); @@ -118,14 +118,16 @@ public class ChannelNumberAttribute extends Attribute * @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 + void decodeAttributeBody(byte[] attributeValue, char offset, char length) + throws StunException { if(length != 4) { throw new StunException("length invalid"); } - channelNumber = ((char)((attributeValue[0] << 8 ) | (attributeValue[1]&0xFF) )); + channelNumber = + ((char)((attributeValue[0] << 8 ) | (attributeValue[1] & 0xFF) )); } /** @@ -145,5 +147,4 @@ public class ChannelNumberAttribute extends Attribute { return channelNumber; } -} - +} \ No newline at end of file diff --git a/src/org/ice4j/attribute/DataAttribute.java b/src/org/ice4j/attribute/DataAttribute.java index 9be8885..d91517d 100644 --- a/src/org/ice4j/attribute/DataAttribute.java +++ b/src/org/ice4j/attribute/DataAttribute.java @@ -51,8 +51,8 @@ public class DataAttribute * @param length the length of the binary array. * @throws StunException if attributeValue contains invalid data. */ - void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws - StunException + void decodeAttributeBody(byte[] attributeValue, char offset, char length) + throws StunException { data = new byte[length]; System.arraycopy(attributeValue, offset, data, 0, length); @@ -65,15 +65,16 @@ public class DataAttribute public byte[] encode() { char type = getAttributeType(); - byte binValue[] = new byte[HEADER_LENGTH + getDataLength() + (getDataLength() % 4)]; + byte binValue[] = new byte[HEADER_LENGTH + getDataLength() + + (getDataLength() % 4)]; //Type binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); + binValue[1] = (byte)(type & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); //data 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 - * have the same type length and value. + * Compares two STUN Attributes. Two attributes are considered equal when + * they have the same type length and value. * @param obj the object to compare this attribute with. * @return true if the attributes are equal and false otherwise. */ @@ -152,5 +153,4 @@ public class DataAttribute return true; } -} - +} \ No newline at end of file diff --git a/src/org/ice4j/attribute/DestinationAddressAttribute.java b/src/org/ice4j/attribute/DestinationAddressAttribute.java new file mode 100644 index 0000000..913c129 --- /dev/null +++ b/src/org/ice4j/attribute/DestinationAddressAttribute.java @@ -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); + } +} \ No newline at end of file diff --git a/src/org/ice4j/attribute/DontFragmentAttribute.java b/src/org/ice4j/attribute/DontFragmentAttribute.java index 92106ea..97d8c6e 100644 --- a/src/org/ice4j/attribute/DontFragmentAttribute.java +++ b/src/org/ice4j/attribute/DontFragmentAttribute.java @@ -82,8 +82,8 @@ public class DontFragmentAttribute extends Attribute byte binValue[] = new byte[HEADER_LENGTH]; //Type - binValue[0] = (byte)(getAttributeType()>>8); - binValue[1] = (byte)(getAttributeType()&0x00FF); + binValue[0] = (byte)(getAttributeType() >> 8); + binValue[1] = (byte)(getAttributeType() & 0x00FF); //Length binValue[2] = (byte)(getDataLength() >> 8); binValue[3] = (byte)(getDataLength() & 0x00FF); @@ -101,7 +101,8 @@ public class DontFragmentAttribute extends Attribute * @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 + void decodeAttributeBody(byte[] attributeValue, char offset, char length) + throws StunException { if(length != 0) { diff --git a/src/org/ice4j/attribute/ErrorCodeAttribute.java b/src/org/ice4j/attribute/ErrorCodeAttribute.java index b62c5dd..f32b148 100644 --- a/src/org/ice4j/attribute/ErrorCodeAttribute.java +++ b/src/org/ice4j/attribute/ErrorCodeAttribute.java @@ -157,9 +157,9 @@ public class ErrorCodeAttribute extends Attribute /** * A convenience method that sets error class and number according to the - * specified errorCode.The class represents the hundreds digit of the error code. - * The value MUST be between 1 and 6. The number represents the response - * code modulo 100, and its value MUST be between 0 and 99. + * specified errorCode.The class represents the hundreds digit of the error + * code. The value MUST be between 1 and 6. The number represents the + * response code modulo 100, and its value MUST be between 0 and 99. * * @param errorCode the errorCode that this class encapsulates. * @throws IllegalArgumentException if errorCode is not a valid error code. @@ -167,7 +167,7 @@ public class ErrorCodeAttribute extends Attribute public void setErrorCode(char errorCode) throws IllegalArgumentException { - setErrorClass((byte)(errorCode/100)); + setErrorClass((byte)(errorCode / 100)); setErrorNumber((byte)(errorCode % 100)); } @@ -178,7 +178,7 @@ public class ErrorCodeAttribute extends Attribute */ 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) throws IllegalArgumentException { - if(errorNumber < 0 || errorNumber > 99) + /* + if(errorNumber < 0 || errorNumber > 9999) throw new IllegalArgumentException( errorNumber + " is not a valid error number!"); + */ this.errorNumber = errorNumber; } @@ -312,8 +314,8 @@ public class ErrorCodeAttribute extends Attribute */ public char getDataLength() { - char len = (char)( 4 //error code numbers - + (char)(reasonPhrase == null? 0:reasonPhrase.length)); + char len = (char)(4 //error code numbers + + (char)(reasonPhrase == null ? 0 : reasonPhrase.length)); return len; } @@ -326,7 +328,7 @@ public class ErrorCodeAttribute extends Attribute { byte binValue[] = new byte[HEADER_LENGTH + getDataLength() //add padding - + (4 - getDataLength()%4)%4]; + + (4 - getDataLength() % 4) % 4]; //Type binValue[0] = (byte) (getAttributeType() >> 8); @@ -404,6 +406,5 @@ public class ErrorCodeAttribute extends Attribute System.arraycopy(attributeValue, offset, reasonBytes, 0, reasonBytes.length); setReasonPhrase(new String(reasonBytes)); - } } diff --git a/src/org/ice4j/attribute/EvenPortAttribute.java b/src/org/ice4j/attribute/EvenPortAttribute.java index eb5ee3f..e6941e4 100644 --- a/src/org/ice4j/attribute/EvenPortAttribute.java +++ b/src/org/ice4j/attribute/EvenPortAttribute.java @@ -98,8 +98,8 @@ public class EvenPortAttribute extends Attribute byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH]; //Type - binValue[0] = (byte)(getAttributeType()>>8); - binValue[1] = (byte)(getAttributeType()&0x00FF); + binValue[0] = (byte)(getAttributeType() >> 8); + binValue[1] = (byte)(getAttributeType() & 0x00FF); //Length binValue[2] = (byte)(getDataLength() >> 8); binValue[3] = (byte)(getDataLength() & 0x00FF); @@ -119,7 +119,8 @@ public class EvenPortAttribute extends Attribute * @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 + void decodeAttributeBody(byte[] attributeValue, char offset, char length) + throws StunException { if(length != 4) { @@ -146,5 +147,4 @@ public class EvenPortAttribute extends Attribute { return rFlag; } -} - +} \ No newline at end of file diff --git a/src/org/ice4j/attribute/FingerprintAttribute.java b/src/org/ice4j/attribute/FingerprintAttribute.java index 14f5e6e..0e1b9b7 100644 --- a/src/org/ice4j/attribute/FingerprintAttribute.java +++ b/src/org/ice4j/attribute/FingerprintAttribute.java @@ -61,7 +61,6 @@ public class FingerprintAttribute */ private byte[] crc; - /** * Creates a FingerPrintAttribute instance. */ @@ -81,6 +80,7 @@ public class FingerprintAttribute { return crc; } + /** * Returns the length of this attribute's body. * @@ -140,7 +140,7 @@ public class FingerprintAttribute { throw new UnsupportedOperationException( "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()]; //Type - binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); + binValue[0] = (byte)(type >> 8); + binValue[1] = (byte)(type & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); //calculate the check sum byte[] xorCrc32 = calculateXorCRC32(content, offset, length); @@ -246,4 +246,3 @@ public class FingerprintAttribute return xorCRC32; } } - diff --git a/src/org/ice4j/attribute/IceControlAttribute.java b/src/org/ice4j/attribute/IceControlAttribute.java index d6ccc01..026cdf6 100644 --- a/src/org/ice4j/attribute/IceControlAttribute.java +++ b/src/org/ice4j/attribute/IceControlAttribute.java @@ -59,11 +59,11 @@ public abstract class IceControlAttribute void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws StunException { - // array used to hold the intermediate long values reconstructed from the - // attributeValue array + // 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) + tieBreaker = ((attributeValue[offset++] & 0xffl) << 56) | ((attributeValue[offset++] & 0xffl) << 48) | ((attributeValue[offset++] & 0xffl) << 40) | ((attributeValue[offset++] & 0xffl) << 32) @@ -84,12 +84,12 @@ public abstract class IceControlAttribute byte[] binValue = new byte[HEADER_LENGTH + getDataLength()]; //Type - binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); + binValue[0] = (byte)(type >> 8); + binValue[1] = (byte)(type & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); //Tie-Breaker binValue[4] = (byte)((tieBreaker & 0xFF00000000000000L) >> 56); diff --git a/src/org/ice4j/attribute/LifetimeAttribute.java b/src/org/ice4j/attribute/LifetimeAttribute.java index 22b196c..fd0e1d7 100644 --- a/src/org/ice4j/attribute/LifetimeAttribute.java +++ b/src/org/ice4j/attribute/LifetimeAttribute.java @@ -14,7 +14,8 @@ import org.ice4j.*; * * @author Sebastien Vincent */ -public class LifetimeAttribute extends Attribute +public class LifetimeAttribute + extends Attribute { /** * Attribute name. @@ -55,8 +56,8 @@ public class LifetimeAttribute extends Attribute return true; LifetimeAttribute att = (LifetimeAttribute) obj; - if (att.getAttributeType() != getAttributeType() - || att.getDataLength() != getDataLength() + if (att.getAttributeType() != getAttributeType() + || att.getDataLength() != getDataLength() /* compare data */ || att.lifetime != lifetime ) @@ -94,8 +95,8 @@ public class LifetimeAttribute extends Attribute byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH]; //Type - binValue[0] = (byte)(getAttributeType()>>8); - binValue[1] = (byte)(getAttributeType()&0x00FF); + binValue[0] = (byte)(getAttributeType() >> 8); + binValue[1] = (byte)(getAttributeType() & 0x00FF); //Length binValue[2] = (byte)(getDataLength() >> 8); binValue[3] = (byte)(getDataLength() & 0x00FF); @@ -118,15 +119,18 @@ public class LifetimeAttribute extends Attribute * @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 + void decodeAttributeBody(byte[] attributeValue, char offset, char length) + throws StunException { if(length != 4) { throw new StunException("length invalid"); } - lifetime = ((attributeValue[0] << 24) & 0xff000000) + ((attributeValue[1] << 16) & 0x00ff0000) - + ((attributeValue[2] << 8) & 0x0000ff00) + (attributeValue[3] & 0x000000ff); + lifetime = ((attributeValue[0] << 24) & 0xff000000) + + ((attributeValue[1] << 16) & 0x00ff0000) + + ((attributeValue[2] << 8) & 0x0000ff00) + + (attributeValue[3] & 0x000000ff); } /** @@ -147,4 +151,3 @@ public class LifetimeAttribute extends Attribute return lifetime; } } - diff --git a/src/org/ice4j/attribute/MagicCookieAttribute.java b/src/org/ice4j/attribute/MagicCookieAttribute.java new file mode 100644 index 0000000..00131fa --- /dev/null +++ b/src/org/ice4j/attribute/MagicCookieAttribute.java @@ -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); + } +} diff --git a/src/org/ice4j/attribute/MappedAddressAttribute.java b/src/org/ice4j/attribute/MappedAddressAttribute.java index d78517f..1caa6de 100644 --- a/src/org/ice4j/attribute/MappedAddressAttribute.java +++ b/src/org/ice4j/attribute/MappedAddressAttribute.java @@ -27,7 +27,6 @@ package org.ice4j.attribute; * * @author Emil Ivov */ - public class MappedAddressAttribute extends AddressAttribute { /** @@ -35,9 +34,11 @@ public class MappedAddressAttribute extends AddressAttribute */ public static final String NAME = "MAPPED-ADDRESS"; + /** + * Constructor. + */ MappedAddressAttribute() { super(MAPPED_ADDRESS); } - } diff --git a/src/org/ice4j/attribute/MessageIntegrityAttribute.java b/src/org/ice4j/attribute/MessageIntegrityAttribute.java index 9c8a631..1c9c1fb 100644 --- a/src/org/ice4j/attribute/MessageIntegrityAttribute.java +++ b/src/org/ice4j/attribute/MessageIntegrityAttribute.java @@ -82,7 +82,6 @@ public class MessageIntegrityAttribute */ public static final String NAME = "MESSAGE_INTEGRITY"; - /** * The HMAC-SHA1 algorithm. */ @@ -239,7 +238,7 @@ public class MessageIntegrityAttribute { throw new UnsupportedOperationException( "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()]; //Type - binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); + binValue[0] = (byte)(type >> 8); + binValue[1] = (byte)(type & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); byte[] key = null; char msgType = (char)((content[0] << 8) + content[1]); @@ -340,4 +339,3 @@ public class MessageIntegrityAttribute return true; } } - diff --git a/src/org/ice4j/attribute/NonceAttribute.java b/src/org/ice4j/attribute/NonceAttribute.java index bd6dbd3..de1ad2b 100644 --- a/src/org/ice4j/attribute/NonceAttribute.java +++ b/src/org/ice4j/attribute/NonceAttribute.java @@ -63,12 +63,12 @@ public class NonceAttribute extends Attribute = new byte[HEADER_LENGTH + getDataLength() + (getDataLength() % 4)]; //Type - binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); + binValue[0] = (byte)(type >> 8); + binValue[1] = (byte)(type & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); /* nonce */ System.arraycopy(nonce, 0, binValue, 4, (int)getDataLength()); diff --git a/src/org/ice4j/attribute/OptionalAttribute.java b/src/org/ice4j/attribute/OptionalAttribute.java index 7aba3a2..15ffdb3 100644 --- a/src/org/ice4j/attribute/OptionalAttribute.java +++ b/src/org/ice4j/attribute/OptionalAttribute.java @@ -12,13 +12,12 @@ import org.ice4j.*; /** * 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 - * subdued to any processing by the stack. One could use this class for both + * the stack. Such attributes will generally be kept in binary form and won't + * be subdued to any processing by the stack. One could use this class for both * dealing with attributes in received messages, and generating messages * containing attributes not explicitly supported by the stack. * * @author Emil Ivov - * */ public class OptionalAttribute extends Attribute @@ -44,7 +43,8 @@ public class OptionalAttribute throws StunException { 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]; //Type - binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); + binValue[0] = (byte)(type >> 8); + binValue[1] = (byte)(type & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); System.arraycopy(attributeValue, 0, binValue, HEADER_LENGTH, attributeValue.length); @@ -114,7 +114,6 @@ public class OptionalAttribute System.arraycopy(body, offset, this.attributeValue, 0, length); } - /** * Compares two STUN Attributes. Two attributes are considered equal when they * have the same type length and value. diff --git a/src/org/ice4j/attribute/PriorityAttribute.java b/src/org/ice4j/attribute/PriorityAttribute.java index 9af8a48..ecc5896 100644 --- a/src/org/ice4j/attribute/PriorityAttribute.java +++ b/src/org/ice4j/attribute/PriorityAttribute.java @@ -57,8 +57,8 @@ public class PriorityAttribute throws StunException { - // array used to hold the intermediate long values reconstructed from the - // attributeValue array + // array used to hold the intermediate long values reconstructed from + // the attributeValue array long[] values = new long[4]; // Reading in the network byte order (Big-Endian) @@ -82,12 +82,12 @@ public class PriorityAttribute byte[] binValue = new byte[HEADER_LENGTH + getDataLength()]; //Type - binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); + binValue[0] = (byte)(type >> 8); + binValue[1] = (byte)(type & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); //Priority binValue[4] = (byte)((priority & 0xFF000000L) >> 24); @@ -96,12 +96,11 @@ public class PriorityAttribute binValue[7] = (byte)(priority & 0x000000FFL); return binValue; - } /** - * Compares two STUN Attributes. Two attributes are considered equal when they - * have the same type length and value. + * Compares two STUN Attributes. Two attributes are considered equal when + * they have the same type length and value. * * @param obj the object to compare this attribute with. * @return true if the attributes are equal and false otherwise. @@ -173,5 +172,4 @@ public class PriorityAttribute else this.priority = priority; } - } diff --git a/src/org/ice4j/attribute/RealmAttribute.java b/src/org/ice4j/attribute/RealmAttribute.java index c5d77fc..e43cfdf 100644 --- a/src/org/ice4j/attribute/RealmAttribute.java +++ b/src/org/ice4j/attribute/RealmAttribute.java @@ -11,7 +11,7 @@ import java.util.*; 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. * * @author Sebastien Vincent @@ -46,12 +46,12 @@ public class RealmAttribute extends Attribute * @param length the length of the binary array. * @throws StunException if attributeValue contains invalid data. */ - void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws - StunException - { - realm = new byte[length]; - System.arraycopy(attributeValue, offset, realm, 0, length); - } + void decodeAttributeBody(byte[] attributeValue, char offset, char length) + throws StunException + { + realm = new byte[length]; + System.arraycopy(attributeValue, offset, realm, 0, length); + } /** * Returns a binary representation of this attribute. @@ -60,15 +60,16 @@ public class RealmAttribute extends Attribute public byte[] encode() { char type = getAttributeType(); - byte binValue[] = new byte[HEADER_LENGTH + getDataLength() + (getDataLength() % 4)]; + byte binValue[] = new byte[HEADER_LENGTH + getDataLength() + + (getDataLength() % 4)]; //Type - binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); + binValue[0] = (byte)(type >> 8); + binValue[1] = (byte)(type & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); /* realm */ System.arraycopy(realm, 0, binValue, 4, getDataLength()); @@ -144,4 +145,3 @@ public class RealmAttribute extends Attribute return true; } } - diff --git a/src/org/ice4j/attribute/ReflectedFromAttribute.java b/src/org/ice4j/attribute/ReflectedFromAttribute.java index ea929dd..907d830 100644 --- a/src/org/ice4j/attribute/ReflectedFromAttribute.java +++ b/src/org/ice4j/attribute/ReflectedFromAttribute.java @@ -18,7 +18,6 @@ package org.ice4j.attribute; * * @author Emil Ivov */ - public class ReflectedFromAttribute extends AddressAttribute { /** @@ -33,5 +32,4 @@ public class ReflectedFromAttribute extends AddressAttribute { super(REFLECTED_FROM); } - } diff --git a/src/org/ice4j/attribute/RemoteAddressAttribute.java b/src/org/ice4j/attribute/RemoteAddressAttribute.java new file mode 100644 index 0000000..3d7c668 --- /dev/null +++ b/src/org/ice4j/attribute/RemoteAddressAttribute.java @@ -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); + } +} \ No newline at end of file diff --git a/src/org/ice4j/attribute/RequestedTransportAttribute.java b/src/org/ice4j/attribute/RequestedTransportAttribute.java index f206a53..445cd03 100644 --- a/src/org/ice4j/attribute/RequestedTransportAttribute.java +++ b/src/org/ice4j/attribute/RequestedTransportAttribute.java @@ -100,8 +100,8 @@ public class RequestedTransportAttribute extends Attribute byte binValue[] = new byte[HEADER_LENGTH + DATA_LENGTH]; //Type - binValue[0] = (byte)(getAttributeType()>>8); - binValue[1] = (byte)(getAttributeType()&0x00FF); + binValue[0] = (byte)(getAttributeType() >> 8); + binValue[1] = (byte)(getAttributeType() & 0x00FF); //Length binValue[2] = (byte)(getDataLength() >> 8); binValue[3] = (byte)(getDataLength() & 0x00FF); @@ -124,7 +124,8 @@ public class RequestedTransportAttribute extends Attribute * @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 + void decodeAttributeBody(byte[] attributeValue, char offset, char length) + throws StunException { if(length != 4) { @@ -152,4 +153,3 @@ public class RequestedTransportAttribute extends Attribute return transportProtocol; } } - diff --git a/src/org/ice4j/attribute/ReservationTokenAttribute.java b/src/org/ice4j/attribute/ReservationTokenAttribute.java index 68aa32e..e8baa9c 100644 --- a/src/org/ice4j/attribute/ReservationTokenAttribute.java +++ b/src/org/ice4j/attribute/ReservationTokenAttribute.java @@ -17,145 +17,144 @@ import org.ice4j.*; * @author Sebastien Vincent */ public class ReservationTokenAttribute -extends Attribute + extends Attribute { - /** - * Attribute name. - */ - public static final String NAME = "RESERVATION-TOKEN"; + /** + * Attribute name. + */ + public static final String NAME = "RESERVATION-TOKEN"; - /** - * ReservationToken value. - */ - private byte reservationToken[] = null; + /** + * ReservationToken value. + */ + private byte reservationToken[] = null; - /** - * Constructor. - */ - 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 + /** + * Constructor. + */ + protected ReservationTokenAttribute () { - if(length != 8) - { - throw new StunException("Length mismatch!"); + 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) + { + 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 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; - } - - /** - * 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) + /** + * Returns the human readable name of this attribute. + * + * @return this attribute's name. + */ + public String getName() { - this.reservationToken = null; - return; + return NAME; } - 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; - /** - * Returns the length of this attribute's body. - * @return the length of this attribute's value. - */ - public char getDataLength() - { - return (char)reservationToken.length; - } + byte[] copy = new byte[reservationToken.length]; + System.arraycopy(reservationToken, 0, copy, 0, reservationToken.length); + return reservationToken; + } - /** - * Compares two STUN Attributes. Two attributes are considered equal when they - * have the same type length and value. - * @param obj the object to compare this attribute with. - * @return true if the attributes are equal and false otherwise. - */ + /** + * 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; + } - public boolean equals(Object obj) - { - if (! (obj instanceof ReservationTokenAttribute) - || obj == null) - return false; + this.reservationToken = new byte[reservationToken.length]; + System.arraycopy(reservationToken, 0, this.reservationToken, 0, + reservationToken.length); + } - 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() - || att.getDataLength() != getDataLength() - || !Arrays.equals( att.reservationToken, reservationToken)) - return false; + /** + * Compares two STUN Attributes. Two attributes are considered equal when + * they have the same type length and value. + * @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 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; + } } - diff --git a/src/org/ice4j/attribute/ResponseAddressAttribute.java b/src/org/ice4j/attribute/ResponseAddressAttribute.java index 7aaeb4d..38f921a 100644 --- a/src/org/ice4j/attribute/ResponseAddressAttribute.java +++ b/src/org/ice4j/attribute/ResponseAddressAttribute.java @@ -28,5 +28,4 @@ public class ResponseAddressAttribute extends AddressAttribute { super(RESPONSE_ADDRESS); } - } diff --git a/src/org/ice4j/attribute/SoftwareAttribute.java b/src/org/ice4j/attribute/SoftwareAttribute.java index 4bb91db..13bf9fa 100644 --- a/src/org/ice4j/attribute/SoftwareAttribute.java +++ b/src/org/ice4j/attribute/SoftwareAttribute.java @@ -62,15 +62,15 @@ public class SoftwareAttribute char type = getAttributeType(); byte binValue[] = new byte[HEADER_LENGTH + getDataLength() //add padding - + (4 - getDataLength()%4)%4]; + + (4 - getDataLength() % 4) % 4]; //Type - binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); + binValue[0] = (byte)(type >> 8); + binValue[1] = (byte)(type & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); //software 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. * @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 - * have the same type length and value. + * Compares two STUN Attributes. Two attributes are considered equal when + * they have the same type length and value. * * @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 SoftwareAttribute) @@ -156,5 +155,4 @@ public class SoftwareAttribute return true; } - } diff --git a/src/org/ice4j/attribute/SourceAddressAttribute.java b/src/org/ice4j/attribute/SourceAddressAttribute.java index a7528e5..558d428 100644 --- a/src/org/ice4j/attribute/SourceAddressAttribute.java +++ b/src/org/ice4j/attribute/SourceAddressAttribute.java @@ -13,7 +13,6 @@ package org.ice4j.attribute; * * @author Emil Ivov */ - public class SourceAddressAttribute extends AddressAttribute { /** diff --git a/src/org/ice4j/attribute/UnknownAttributesAttribute.java b/src/org/ice4j/attribute/UnknownAttributesAttribute.java index 6ec9f09..1976fd9 100644 --- a/src/org/ice4j/attribute/UnknownAttributesAttribute.java +++ b/src/org/ice4j/attribute/UnknownAttributesAttribute.java @@ -76,7 +76,7 @@ public class UnknownAttributesAttribute extends Attribute if( (len % 2 ) != 0 ) len++; - return (char)(len*2); + return (char)(len * 2); } /** @@ -111,7 +111,6 @@ public class UnknownAttributesAttribute extends Attribute public Iterator getAttributes() { return unknownAttributes.iterator(); - } /** @@ -152,9 +151,10 @@ public class UnknownAttributesAttribute extends Attribute Iterator attributes = getAttributes(); - while (attributes.hasNext()) { + while (attributes.hasNext()) + { char att = attributes.next().charValue(); - binValue[offset++] = (byte)(att>>8); + binValue[offset++] = (byte)(att >> 8); binValue[offset++] = (byte)(att & 0x00FF); } @@ -168,7 +168,6 @@ public class UnknownAttributesAttribute extends Attribute binValue[offset++] = (byte) (att & 0x00FF); } - return binValue; } @@ -196,7 +195,6 @@ public class UnknownAttributesAttribute extends Attribute return false; return true; - } /** @@ -210,17 +208,17 @@ public class UnknownAttributesAttribute extends Attribute * @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 + void decodeAttributeBody(byte[] attributeValue, char offset, char length) + throws StunException { - if( (length % 2 ) != 0) 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; - 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++]) ); addAttributeID(attributeID); } diff --git a/src/org/ice4j/attribute/UseCandidateAttribute.java b/src/org/ice4j/attribute/UseCandidateAttribute.java index ccddc59..67a95fc 100644 --- a/src/org/ice4j/attribute/UseCandidateAttribute.java +++ b/src/org/ice4j/attribute/UseCandidateAttribute.java @@ -10,17 +10,23 @@ import org.ice4j.*; * This attribute serves as only a flag, it does not have any data * so the data length is zero */ -public class UseCandidateAttribute - extends Attribute +public class UseCandidateAttribute + extends Attribute { - private static final char DATA_LENGTH_USE_CANDIDATE = 0; + /** + * Data length. + */ + private static final char DATA_LENGTH_USE_CANDIDATE = 0; - protected UseCandidateAttribute() - { - super(USE_CANDIDATE); - } + /** + * Constructor. + */ + protected UseCandidateAttribute() + { + super(USE_CANDIDATE); + } - /** + /** * Decodes the USE-CANDIDATE attribute's body, which is empty * * @param attributeValue a binary array containing this attribute's @@ -30,75 +36,74 @@ public class UseCandidateAttribute * @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 - { - // Do nothing, empty attribute body - } + void decodeAttributeBody(byte[] attributeValue, char offset, char length) + throws StunException + { + // Do nothing, empty attribute body + } - /** + /** * 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_USE_CANDIDATE]; - - // Type - binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); - - // Length - binValue[2] = (byte)(DATA_LENGTH_USE_CANDIDATE>>8); - binValue[3] = (byte)(DATA_LENGTH_USE_CANDIDATE&0x00FF); - - return binValue; - } + public byte[] encode() + { + char type = getAttributeType(); + byte[] binValue = new byte[HEADER_LENGTH + DATA_LENGTH_USE_CANDIDATE]; - /** - * Compares two STUN Attributes. Two attributes are considered equal when they - * have the same type, length and value. + // Type + binValue[0] = (byte)(type >> 8); + binValue[1] = (byte)(type & 0x00FF); + + // Length + binValue[2] = (byte)(DATA_LENGTH_USE_CANDIDATE >> 8); + binValue[3] = (byte)(DATA_LENGTH_USE_CANDIDATE & 0x00FF); + + return binValue; + } + + /** + * Compares two STUN Attributes. Two attributes are considered equal when + * they have the same type, length and value. * * @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 UseCandidateAttribute) - || obj == null) - return false; - - if(obj == this) - return true; - - UseCandidateAttribute useCandidateAtt = (UseCandidateAttribute)obj; - if(useCandidateAtt.getAttributeType() != getAttributeType() - || useCandidateAtt.getDataLength() != getDataLength()) - return false; - - return true; - } + public boolean equals(Object obj) + { + if(!(obj instanceof UseCandidateAttribute) + || obj == null) + return false; - /** + if(obj == this) + return true; + + UseCandidateAttribute useCandidateAtt = (UseCandidateAttribute)obj; + if(useCandidateAtt.getAttributeType() != getAttributeType() + || useCandidateAtt.getDataLength() != getDataLength()) + return false; + + return true; + } + + /** * Returns the length of this attribute's body. * * @return the length of this attribute's value. */ - public char getDataLength() - { - return DATA_LENGTH_USE_CANDIDATE; - } + public char getDataLength() + { + return DATA_LENGTH_USE_CANDIDATE; + } - /** + /** * Returns the human readable name of this attribute. * * @return this attribute's name. */ - public String getName() - { - return "USE-CANDIDATE"; - } - + public String getName() + { + return "USE-CANDIDATE"; + } } diff --git a/src/org/ice4j/attribute/UsernameAttribute.java b/src/org/ice4j/attribute/UsernameAttribute.java index 6eee035..d3f9b66 100644 --- a/src/org/ice4j/attribute/UsernameAttribute.java +++ b/src/org/ice4j/attribute/UsernameAttribute.java @@ -61,15 +61,15 @@ public class UsernameAttribute extends Attribute char type = getAttributeType(); byte binValue[] = new byte[HEADER_LENGTH + getDataLength() //add padding - + (4 - getDataLength()%4)%4]; + + (4 - getDataLength() % 4) % 4]; //Type - binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); + binValue[0] = (byte)(type >> 8); + binValue[1] = (byte)(type & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); //username System.arraycopy(username, 0, binValue, 4, getDataLength()); @@ -151,4 +151,3 @@ public class UsernameAttribute extends Attribute return true; } } - diff --git a/src/org/ice4j/attribute/XorOnlyAttribute.java b/src/org/ice4j/attribute/XorOnlyAttribute.java index 7e6ca5c..06fb504 100644 --- a/src/org/ice4j/attribute/XorOnlyAttribute.java +++ b/src/org/ice4j/attribute/XorOnlyAttribute.java @@ -24,7 +24,9 @@ import org.ice4j.*; public class XorOnlyAttribute extends Attribute { - + /** + * Constructor. + */ protected XorOnlyAttribute() { super(Attribute.XOR_ONLY); @@ -57,12 +59,12 @@ public class XorOnlyAttribute byte binValue[] = new byte[HEADER_LENGTH + getDataLength()]; //Type - binValue[0] = (byte)(type>>8); - binValue[1] = (byte)(type&0x00FF); + binValue[0] = (byte)(type >> 8); + binValue[1] = (byte)(type & 0x00FF); //Length - binValue[2] = (byte)(getDataLength()>>8); - binValue[3] = (byte)(getDataLength()&0x00FF); + binValue[2] = (byte)(getDataLength() >> 8); + binValue[3] = (byte)(getDataLength() & 0x00FF); return binValue; } diff --git a/src/org/ice4j/attribute/XorPeerAddressAttribute.java b/src/org/ice4j/attribute/XorPeerAddressAttribute.java index f8440bb..b0540d6 100644 --- a/src/org/ice4j/attribute/XorPeerAddressAttribute.java +++ b/src/org/ice4j/attribute/XorPeerAddressAttribute.java @@ -16,17 +16,16 @@ package org.ice4j.attribute; */ public class XorPeerAddressAttribute extends XorMappedAddressAttribute { - /** - * Attribute name. - */ - public static final String NAME = "XOR-PEER-ADDRESS"; + /** + * Attribute name. + */ + public static final String NAME = "XOR-PEER-ADDRESS"; - /** - * Constructor. - */ - XorPeerAddressAttribute() - { - super(XOR_PEER_ADDRESS); - } + /** + * Constructor. + */ + XorPeerAddressAttribute() + { + super(XOR_PEER_ADDRESS); + } } - diff --git a/src/org/ice4j/attribute/XorRelayedAddressAttribute.java b/src/org/ice4j/attribute/XorRelayedAddressAttribute.java index eb3dbbe..0429dec 100644 --- a/src/org/ice4j/attribute/XorRelayedAddressAttribute.java +++ b/src/org/ice4j/attribute/XorRelayedAddressAttribute.java @@ -16,17 +16,16 @@ package org.ice4j.attribute; */ public class XorRelayedAddressAttribute extends XorMappedAddressAttribute { - /** - * Attribute name. - */ - public static final String NAME = "XOR-RELAYED-ADDRESS"; + /** + * Attribute name. + */ + public static final String NAME = "XOR-RELAYED-ADDRESS"; - /** - * Constructor. - */ - XorRelayedAddressAttribute() - { - super(XOR_RELAYED_ADDRESS); - } + /** + * Constructor. + */ + XorRelayedAddressAttribute() + { + super(XOR_RELAYED_ADDRESS); + } } -