diff --git a/src/org/ice4j/socket/StunDatagramPacketFilter.java b/src/org/ice4j/socket/StunDatagramPacketFilter.java index 11812a6..2999e0e 100644 --- a/src/org/ice4j/socket/StunDatagramPacketFilter.java +++ b/src/org/ice4j/socket/StunDatagramPacketFilter.java @@ -78,29 +78,17 @@ public class StunDatagramPacketFilter if ((stunServer != null) && !stunServer.equals(p.getSocketAddress())) return false; - /* - * All STUN messages MUST start with a 20-byte header followed by - * zero or more Attributes. - */ - if (p.getLength() >= 20) + // If this is a STUN packet. + if (StunDatagramPacketFilter.isStunPacket(p)) { - /* - * The most significant 2 bits of every STUN message MUST be - * zeroes. This can be used to differentiate STUN packets from - * other protocols when STUN is multiplexed with other protocols - * on the same port. - */ byte[] data = p.getData(); int offset = p.getOffset(); + byte b0 = data[offset]; + byte b1 = data[offset + 1]; + char method = (char) ((b0 & 0xFE) | (b1 & 0xEF)); - if ((b0 & 0xC0) == 0) - { - byte b1 = data[offset + 1]; - char method = (char) ((b0 & 0xFE) | (b1 & 0xEF)); - - return acceptMethod(method); - } + return acceptMethod(method); } return false; } @@ -179,20 +167,44 @@ public class StunDatagramPacketFilter */ public static boolean isStunPacket(DatagramPacket p) { - /* - * The most significant 2 bits of every STUN message MUST be - * zeroes. This can be used to differentiate STUN packets from - * other protocols when STUN is multiplexed with other protocols - * on the same port. - */ boolean isStunPacket = false; byte[] data = p.getData(); int offset = p.getOffset(); + int length = p.getLength(); - if(data.length - offset > 0) + // All STUN messages MUST start with a 20-byte header followed by zero + // or more Attributes. + if(length >= 20) { - byte b0 = data[offset]; - isStunPacket = ((b0 & 0xC0) == 0); + // If the MAGIC COOKIE is present this is a STUN packet (RFC5389 + // compliant). + if(data[offset + 4] == Message.MAGIC_COOKIE[0] + && data[offset + 5] == Message.MAGIC_COOKIE[1] + && data[offset + 6] == Message.MAGIC_COOKIE[2] + && data[offset + 7] == Message.MAGIC_COOKIE[3]) + { + isStunPacket = true; + } + // Else, this packet may be a STUN packet (RFC3489 compliant). To + // determine this, we must continue the checks. + else + { + // The most significant 2 bits of every STUN message MUST be + // zeroes. This can be used to differentiate STUN packets from + // other protocols when STUN is multiplexed with other protocols + // on the same port. + byte b0 = data[offset]; + boolean areFirstTwoBitsValid = ((b0 & 0xC0) == 0); + + // Checks if the length of the data correspond to the length + // field of the STUN header. The message length field of the + // STUN header does not include the 20-byte of the STUN header. + int total_header_length + = ((((int)data[2]) & 0xff) << 8) + (((int) data[3]) & 0xff); + boolean isHeaderLengthValid = (length == total_header_length); + + isStunPacket = areFirstTwoBitsValid && isHeaderLengthValid; + } } return isStunPacket; }