From d33d8b87e47b7d34ac196b64ae814d051bb2fc4a Mon Sep 17 00:00:00 2001 From: Etienne CHAMPETIER Date: Mon, 18 Apr 2016 17:13:54 +0200 Subject: [PATCH 1/3] ChannelData: rework padding getDataLength() was returning data length without padding, so keep current behavior and update the javadoc. (spotted with findbugs because length in getDataLength was a dead store) Introduce padTo4() to round up a number to the next multiple of 4. Deprecate encode() and introduce encode(boolean pad) so we can let the caller decide if he wants padding (mandatory when using tcp or tls) See https://tools.ietf.org/html/rfc5766#section-11.5 Signed-off-by: Etienne CHAMPETIER --- .../java/org/ice4j/message/ChannelData.java | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/ice4j/message/ChannelData.java b/src/main/java/org/ice4j/message/ChannelData.java index 5e9454e..c3b3bb3 100644 --- a/src/main/java/org/ice4j/message/ChannelData.java +++ b/src/main/java/org/ice4j/message/ChannelData.java @@ -100,27 +100,26 @@ public class ChannelData } /** - * Get the data length. + * Get the data length (without padding). * * @return data length */ public char getDataLength() { - char length = 0; if(data == null) - { - return length; - } - - /* pad to a multiple of four */ - if((length % 4) > 0) - { - length += (4 - (length % 4)); - } + return 0; return (char)data.length; } + /** + * @return num padded to 4 + */ + private static int padTo4(int num) + { + return (num + 3) & ~3; + } + /** * Determines whether a specific channel number is in the valid channel * number range defined by the TURN RFC. @@ -136,13 +135,27 @@ public class ChannelData } /** - * Returns a binary representation of this message. - * @return a binary representation of this message. + * Returns a non padded binary representation of this message. + * @return a non padded binary representation of this message. * @throws StunException if the channel number is invalid + * @deprecated */ public byte[] encode() throws StunException { - char dataLength = getDataLength(); + return encode(false); + } + + /** + * Returns a binary representation of this message. + * @param pad determine if we pad this message + * @return a binary representation of this message. + * @throws StunException if the channel number is invalid + */ + public byte[] encode(boolean pad) throws StunException + { + int dataLength = getDataLength(); + if (pad) + dataLength = padTo4(dataLength); byte binMsg[] = new byte[HEADER_LENGTH + dataLength]; int offset = 0; From 84bd07c665c938b9c4d3463f11acab8260afbf9f Mon Sep 17 00:00:00 2001 From: Etienne CHAMPETIER Date: Mon, 25 Apr 2016 11:38:08 +0200 Subject: [PATCH 2/3] Use new ChannelData.encode(pad) Padding is mandatory for TCP and TLS See https://tools.ietf.org/html/rfc5766#section-11.5 Signed-off-by: Etienne CHAMPETIER --- src/main/java/org/ice4j/stack/NetAccessManager.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/ice4j/stack/NetAccessManager.java b/src/main/java/org/ice4j/stack/NetAccessManager.java index b4cef9f..8851da8 100644 --- a/src/main/java/org/ice4j/stack/NetAccessManager.java +++ b/src/main/java/org/ice4j/stack/NetAccessManager.java @@ -587,7 +587,9 @@ class NetAccessManager throws IllegalArgumentException, IOException, StunException { - sendMessage(channelData.encode(), srcAddr, remoteAddr); + boolean pad = srcAddr.getTransport() == Transport.TCP + || srcAddr.getTransport() == Transport.TLS; + sendMessage(channelData.encode(pad), srcAddr, remoteAddr); } /** From b76f9abfc96885126e69f9baa148a70740e8ddde Mon Sep 17 00:00:00 2001 From: Etienne CHAMPETIER Date: Mon, 25 Apr 2016 12:27:51 +0200 Subject: [PATCH 3/3] ChannelData: fix decode() I haven't found any user of this method. This method was only working if offset == 0 and binMessage isn't a padded message. arrayLen is useless as we have msgLen, so remove it, and introduce a deprecated decode() to keep same api Signed-off-by: Etienne CHAMPETIER --- .../java/org/ice4j/message/ChannelData.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/ice4j/message/ChannelData.java b/src/main/java/org/ice4j/message/ChannelData.java index c3b3bb3..5c5a2e7 100644 --- a/src/main/java/org/ice4j/message/ChannelData.java +++ b/src/main/java/org/ice4j/message/ChannelData.java @@ -188,15 +188,29 @@ public class ChannelData * @return a Message object constructed from the binMessage array * @throws StunException ILLEGAL_ARGUMENT if one or more of the arguments * have invalid values. + * @deprecated */ public static ChannelData decode(byte binMessage[], char offset, char arrayLen) throws StunException + { + return decode(binMessage, offset); + } + + /** + * Constructs a message from its binary representation. + * @param binMessage the binary array that contains the encoded message + * @param offset the index where the message starts. + * @return a Message object constructed from the binMessage array + * @throws StunException ILLEGAL_ARGUMENT if one or more of the arguments + * have invalid values. + */ + public static ChannelData decode(byte binMessage[], char offset) throws StunException { char msgLen = 0; char channelNumber = 0; ChannelData channelData = null; byte data[] = null; - if(arrayLen < 4) + if((binMessage.length - offset) < HEADER_LENGTH) { throw new StunException(StunException.ILLEGAL_ARGUMENT, "Size too short"); } @@ -209,7 +223,7 @@ public class ChannelData } msgLen = (char)((binMessage[offset++]<<8) | (binMessage[offset++]&0xFF)); - if(msgLen != (binMessage.length - 4)) + if (msgLen > (binMessage.length - offset)) { throw new StunException(StunException.ILLEGAL_ARGUMENT, "Size mismatch"); }