Improves handling of MessageIntegrity, Fingerprint, and Software attributes

This commit is contained in:
emcho 2010-04-25 14:45:03 +00:00
parent 2a13730d4e
commit e103c4acb9
2 changed files with 17 additions and 11 deletions

View File

@ -25,8 +25,14 @@ import org.ice4j.*;
public class SoftwareAttribute public class SoftwareAttribute
extends Attribute extends Attribute
{ {
/**
* The value that this <tt>SoftwareAttribute</tt> is transporting.
*/
private byte[] software = null; private byte[] software = null;
/**
* Creates a new <tt>SoftwareAttribute</tt>.
*/
protected SoftwareAttribute () protected SoftwareAttribute ()
{ {
super(SOFTWARE); super(SOFTWARE);
@ -37,14 +43,12 @@ public class SoftwareAttribute
* attributeValue. * attributeValue.
* *
* @param attributeValue a binary array containing this attribute's * @param attributeValue a binary array containing this attribute's
* field values and NOT containing the attribute header. * field values and NOT containing the attribute header.
* @param offset the position where attribute values begin (most often * @param offset the position where attribute values begin (most often
* offset is equal to the index of the first byte after length) * offset is equal to the index of the first byte after length)
* @param length the length of the binary array. * @param length the length of the binary array.
* @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
{ {
software = new byte[length]; software = new byte[length];
System.arraycopy(attributeValue, offset, software, 0, length); System.arraycopy(attributeValue, offset, software, 0, length);
@ -58,7 +62,9 @@ public class SoftwareAttribute
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()
//add padding
+ (4 - getDataLength()%4)%4];
//Type //Type
binValue[0] = (byte)(type>>8); binValue[0] = (byte)(type>>8);

View File

@ -256,12 +256,12 @@ public abstract class Message
for (Attribute att : attributes.values()) for (Attribute att : attributes.values())
{ {
length += att.getDataLength() + Attribute.HEADER_LENGTH; int attLen = att.getDataLength() + Attribute.HEADER_LENGTH;
}
if((length % 4) > 0) //take attribute padding into account:
{ attLen += (4 - attLen%4)%4;
length += (4 - (length % 4));
length += attLen;
} }
return length; return length;