pub struct MessageHeader { /* private fields */ }Expand description
The fixed length header of a STUN message.
Allows reading the message header for a quick check if this message is a valid STUN message. Can also be used to expose the length of the complete message without needing to receive the entire message.
Implementations§
Source§impl MessageHeader
impl MessageHeader
Sourcepub fn from_bytes(data: &[u8]) -> Result<Self, StunParseError>
pub fn from_bytes(data: &[u8]) -> Result<Self, StunParseError>
Deserialize a MessageHeader.
If less than MessageHeader::LENGTH bytes are provided, the bytes that are available
are still validated. StunParseError::NotStun is returned as soon as the data cannot be
the start of a STUN message, and StunParseError::Truncated is only returned when the
data may be the start of a STUN message. This allows a STUN message that is received in
multiple pieces (e.g. over TCP) to be rejected as early as possible.
§Examples
let msg_data = [0, 1, 0, 8, 33, 18, 164, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 232];
let message = MessageHeader::from_bytes(&msg_data).unwrap();
assert_eq!(message.get_type(), MessageType::from_class_method(MessageClass::Request, BINDING));
assert_eq!(message.transaction_id(), 1000.into());
assert_eq!(message.data_length(), 8);
// The start of a STUN message header.
assert!(matches!(
MessageHeader::from_bytes(&msg_data[..6]),
Err(StunParseError::Truncated { expected: 20, actual: 6 })
));
// Not the start of a STUN message header, the magic cookie does not match.
assert!(matches!(
MessageHeader::from_bytes(&[0, 1, 0, 8, 33, 0]),
Err(StunParseError::NotStun)
));Sourcepub fn data_length(&self) -> u16
pub fn data_length(&self) -> u16
The number of bytes of content in this MessageHeader. Adding both data_length()
and MessageHeader::LENGTH will result in the size of the complete STUN message.
Sourcepub fn transaction_id(&self) -> TransactionId
pub fn transaction_id(&self) -> TransactionId
The TransactionId of this MessageHeader.
Sourcepub fn get_type(&self) -> MessageType
pub fn get_type(&self) -> MessageType
The MessageType of this MessageHeader.