Contents
- Description
- Nested Class Summary
- Field Summary
- Constructor Summary
- Method Summary
- Field Details
- MAGIC_COOKIE
- HEADER_LENGTH
- TRANSACTION_ID_LENGTH
- BINDING_REQUEST
- BINDING_INDICATION
- BINDING_RESPONSE
- BINDING_ERROR_RESPONSE
- ATTR_MAPPED_ADDRESS
- ATTR_USERNAME
- ATTR_MESSAGE_INTEGRITY
- ATTR_ERROR_CODE
- ATTR_XOR_MAPPED_ADDRESS
- ATTR_PRIORITY
- ATTR_USE_CANDIDATE
- ATTR_FINGERPRINT
- ATTR_ICE_CONTROLLED
- ATTR_ICE_CONTROLLING
- MESSAGE_INTEGRITY_LENGTH
- FINGERPRINT_LENGTH
- FINGERPRINT_XOR
- Constructor Details
- Method Details
- request()
- response(byte[])
- errorResponse(byte[], int, String)
- getMessageType()
- getTransactionId()
- isRequest()
- isResponse()
- getAttribute(int)
- hasAttribute(int)
- getUsername()
- getPriority()
- getMappedAddress()
- addAttribute(int, byte[])
- addUsername(String)
- addPriority(long)
- addUseCandidate()
- addIceRole(boolean, long)
- addXorMappedAddress(InetSocketAddress)
- appendMessageIntegrity(byte[])
- appendFingerprint()
- verifyMessageIntegrity(byte[], int, byte[])
- verifyMessageIntegrity(ByteBuffer, byte[])
- toBytes()
- parse(byte[], int)
- parse(ByteBuffer)
- looksLikeStun(byte[], int)
- looksLikeStun(ByteBuffer)
Class StunMessage
java.lang.Object
com.hypixel.hytale.server.core.io.ice.StunMessage
STUN message codec, covering what ICE connectivity checks need.
Mirrors the client codec in HytaleClient/Networking/NatTraversal. The two are verified
against the same RFC 5769 sample messages, which is what keeps them from drifting apart on the wire.
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intA 4-byte TLV header plus a 4-byte CRC-32.static final intValue the FINGERPRINT CRC is XORed with, so STUN cannot be confused with another protocol.static final intstatic final intstatic final intA 4-byte TLV header plus a 20-byte HMAC-SHA1 digest.static final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAttribute(int type, byte[] value) voidaddIceRole(boolean controlling, long tiebreaker) voidaddPriority(long priority) voidvoidaddUsername(String username) voidaddXorMappedAddress(InetSocketAddress address) Adds XOR-MAPPED-ADDRESS, which tells the peer the address its check arrived from.voidAppends FINGERPRINT, which must be the final attribute.voidappendMessageIntegrity(byte[] key) Appends MESSAGE-INTEGRITY over every attribute added so far.static StunMessageerrorResponse(byte[] transactionId, int code, String reason) byte[]getAttribute(int type) Reads the address a Binding response reports this sender to have.intlongbyte[]booleanhasAttribute(int type) booleanbooleanstatic booleanlooksLikeStun(byte[] datagram, int length) Whether a datagram is STUN rather than QUIC.static booleanlooksLikeStun(ByteBuffer datagram) static StunMessageparse(byte[] datagram, int length) Parses a datagram, rejecting anything malformed rather than trusting the declared lengths.static StunMessageparse(ByteBuffer datagram) Parses the remaining bytes without copying the received datagram or changing its position.static StunMessagerequest()static StunMessageresponse(byte[] transactionId) byte[]toBytes()static booleanverifyMessageIntegrity(byte[] datagram, int length, byte[] key) Verifies MESSAGE-INTEGRITY against the bytes as received.static booleanverifyMessageIntegrity(ByteBuffer datagram, byte[] key) Verifies MESSAGE-INTEGRITY without copying the received datagram.
-
Field Details
-
MAGIC_COOKIE
public static final int MAGIC_COOKIE- See Also:
-
HEADER_LENGTH
public static final int HEADER_LENGTH- See Also:
-
TRANSACTION_ID_LENGTH
public static final int TRANSACTION_ID_LENGTH- See Also:
-
BINDING_REQUEST
public static final int BINDING_REQUEST- See Also:
-
BINDING_INDICATION
public static final int BINDING_INDICATION- See Also:
-
BINDING_RESPONSE
public static final int BINDING_RESPONSE- See Also:
-
BINDING_ERROR_RESPONSE
public static final int BINDING_ERROR_RESPONSE- See Also:
-
ATTR_MAPPED_ADDRESS
public static final int ATTR_MAPPED_ADDRESS- See Also:
-
ATTR_USERNAME
public static final int ATTR_USERNAME- See Also:
-
ATTR_MESSAGE_INTEGRITY
public static final int ATTR_MESSAGE_INTEGRITY- See Also:
-
ATTR_ERROR_CODE
public static final int ATTR_ERROR_CODE- See Also:
-
ATTR_XOR_MAPPED_ADDRESS
public static final int ATTR_XOR_MAPPED_ADDRESS- See Also:
-
ATTR_PRIORITY
public static final int ATTR_PRIORITY- See Also:
-
ATTR_USE_CANDIDATE
public static final int ATTR_USE_CANDIDATE- See Also:
-
ATTR_FINGERPRINT
public static final int ATTR_FINGERPRINT- See Also:
-
ATTR_ICE_CONTROLLED
public static final int ATTR_ICE_CONTROLLED- See Also:
-
ATTR_ICE_CONTROLLING
public static final int ATTR_ICE_CONTROLLING- See Also:
-
MESSAGE_INTEGRITY_LENGTH
public static final int MESSAGE_INTEGRITY_LENGTHA 4-byte TLV header plus a 20-byte HMAC-SHA1 digest.- See Also:
-
FINGERPRINT_LENGTH
public static final int FINGERPRINT_LENGTHA 4-byte TLV header plus a 4-byte CRC-32.- See Also:
-
FINGERPRINT_XOR
public static final int FINGERPRINT_XORValue the FINGERPRINT CRC is XORed with, so STUN cannot be confused with another protocol.- See Also:
-
-
Constructor Details
-
StunMessage
public StunMessage(int messageType, @Nonnull byte[] transactionId)
-
-
Method Details
-
request
-
response
-
errorResponse
public static StunMessage errorResponse(@Nonnull byte[] transactionId, int code, @Nonnull String reason) -
getMessageType
public int getMessageType() -
getTransactionId
public byte[] getTransactionId() -
isRequest
public boolean isRequest() -
isResponse
public boolean isResponse() -
getAttribute
@Nullable public byte[] getAttribute(int type) -
hasAttribute
public boolean hasAttribute(int type) -
getUsername
-
getPriority
public long getPriority() -
getMappedAddress
Reads the address a Binding response reports this sender to have.XOR-MAPPED-ADDRESS wins over MAPPED-ADDRESS, which only servers predating RFC 5389 send.
- Returns:
- the reported address, or null when no attribute carries a well-formed one
-
addAttribute
public void addAttribute(int type, @Nonnull byte[] value) -
addUsername
-
addPriority
public void addPriority(long priority) -
addUseCandidate
public void addUseCandidate() -
addIceRole
public void addIceRole(boolean controlling, long tiebreaker) -
addXorMappedAddress
Adds XOR-MAPPED-ADDRESS, which tells the peer the address its check arrived from. That is how a peer behind a symmetric NAT learns the mapping its own NAT created. -
appendMessageIntegrity
public void appendMessageIntegrity(@Nonnull byte[] key) Appends MESSAGE-INTEGRITY over every attribute added so far.The HMAC covers the message with the header length field already counting the attribute that does not exist yet, per RFC 5389 section 15.4. Must be added after every other attribute except FINGERPRINT.
-
appendFingerprint
public void appendFingerprint()Appends FINGERPRINT, which must be the final attribute. -
verifyMessageIntegrity
public static boolean verifyMessageIntegrity(@Nonnull byte[] datagram, int length, @Nonnull byte[] key) Verifies MESSAGE-INTEGRITY against the bytes as received. Re-serializing would not reproduce the sender's digest, so the original datagram is required. -
verifyMessageIntegrity
Verifies MESSAGE-INTEGRITY without copying the received datagram. -
toBytes
public byte[] toBytes() -
parse
Parses a datagram, rejecting anything malformed rather than trusting the declared lengths.- Returns:
- the parsed message, or null when the bytes are not a well-formed STUN message
-
parse
Parses the remaining bytes without copying the received datagram or changing its position.- Returns:
- the parsed message, or null when the bytes are not a well-formed STUN message
-
looksLikeStun
public static boolean looksLikeStun(@Nonnull byte[] datagram, int length) Whether a datagram is STUN rather than QUIC.RFC 7983 assigns 0..3 to STUN and QUIC sets the fixed bit, but RFC 9287 lets a negotiated endpoint clear that bit, so the cookie and the length are checked too.
-
looksLikeStun
-