From e103c4acb9e85cef2807ee36703a29798f65dacf Mon Sep 17 00:00:00 2001 From: emcho Date: Sun, 25 Apr 2010 14:45:03 +0000 Subject: [PATCH] Improves handling of MessageIntegrity, Fingerprint, and Software attributes --- src/org/ice4j/attribute/SoftwareAttribute.java | 18 ++++++++++++------ src/org/ice4j/message/Message.java | 10 +++++----- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/org/ice4j/attribute/SoftwareAttribute.java b/src/org/ice4j/attribute/SoftwareAttribute.java index 4fe0906..64b0512 100644 --- a/src/org/ice4j/attribute/SoftwareAttribute.java +++ b/src/org/ice4j/attribute/SoftwareAttribute.java @@ -25,8 +25,14 @@ import org.ice4j.*; public class SoftwareAttribute extends Attribute { + /** + * The value that this SoftwareAttribute is transporting. + */ private byte[] software = null; + /** + * Creates a new SoftwareAttribute. + */ protected SoftwareAttribute () { super(SOFTWARE); @@ -37,14 +43,12 @@ public class SoftwareAttribute * attributeValue. * * @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 - * 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. - * @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) { software = new byte[length]; System.arraycopy(attributeValue, offset, software, 0, length); @@ -58,7 +62,9 @@ public class SoftwareAttribute public byte[] encode() { 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 binValue[0] = (byte)(type>>8); diff --git a/src/org/ice4j/message/Message.java b/src/org/ice4j/message/Message.java index 5823db7..c4df688 100644 --- a/src/org/ice4j/message/Message.java +++ b/src/org/ice4j/message/Message.java @@ -256,12 +256,12 @@ public abstract class Message for (Attribute att : attributes.values()) { - length += att.getDataLength() + Attribute.HEADER_LENGTH; - } + int attLen = att.getDataLength() + Attribute.HEADER_LENGTH; - if((length % 4) > 0) - { - length += (4 - (length % 4)); + //take attribute padding into account: + attLen += (4 - attLen%4)%4; + + length += attLen; } return length;