Skip to main content

MessageWriteExt

Trait MessageWriteExt 

Source
pub trait MessageWriteExt: MessageWrite {
Show 13 methods // Provided methods fn get_type(&self) -> MessageType { ... } fn class(&self) -> MessageClass { ... } fn has_class(&self, cls: MessageClass) -> bool { ... } fn is_response(&self) -> bool { ... } fn method(&self) -> Method { ... } fn has_method(&self, method: Method) -> bool { ... } fn transaction_id(&self) -> TransactionId { ... } fn add_message_integrity( &mut self, credentials: &MessageIntegrityCredentials, algorithm: IntegrityAlgorithm, ) -> Result<(), StunWriteError> { ... } fn add_message_integrity_with_key( &mut self, key: &IntegrityKey, algorithm: IntegrityAlgorithm, ) -> Result<(), StunWriteError> { ... } unsafe fn add_message_integrity_with_key_unchecked( &mut self, key: &IntegrityKey, algorithm: IntegrityAlgorithm, ) { ... } fn add_fingerprint(&mut self) -> Result<(), StunWriteError> { ... } unsafe fn add_fingerprint_unchecked(&mut self) { ... } fn add_attribute( &mut self, attr: &dyn AttributeWrite, ) -> Result<(), StunWriteError> { ... }
}
Expand description

Extension trait for MessageWrite providing helper functions.

Provided Methods§

Source

fn get_type(&self) -> MessageType

Retrieve the MessageClass of a Message.

§Examples
let message = Message::builder_request(BINDING, MessageWriteVec::new()).finish();
let message = Message::from_bytes(&message).unwrap();
assert_eq!(message.class(), MessageClass::Request);
Source

fn class(&self) -> MessageClass

Retrieve the MessageClass of a Message.

§Examples
let message = Message::builder_request(BINDING, MessageWriteVec::new()).finish();
let message = Message::from_bytes(&message).unwrap();
assert_eq!(message.class(), MessageClass::Request);
Source

fn has_class(&self, cls: MessageClass) -> bool

Returns whether the Message is of the specified MessageClass.

§Examples
let message = Message::builder_request(BINDING, MessageWriteVec::new()).finish();
let message = Message::from_bytes(&message).unwrap();
assert!(message.has_class(MessageClass::Request));
Source

fn is_response(&self) -> bool

Returns whether the Message is a response.

This means that the Message has a class of either success or error.

§Examples
let message = Message::builder_request(BINDING, MessageWriteVec::new()).finish();
let message = Message::from_bytes(&message).unwrap();
assert_eq!(message.is_response(), false);

let error = Message::builder_error(&message, MessageWriteVec::new()).finish();
let error = Message::from_bytes(&error).unwrap();
assert_eq!(error.is_response(), true);

let success = Message::builder_success(&message, MessageWriteVec::new()).finish();
let success = Message::from_bytes(&success).unwrap();
assert_eq!(success.is_response(), true);
Source

fn method(&self) -> Method

Retrieves the method of the Message.

§Examples
let message = Message::builder_request(BINDING, MessageWriteVec::new()).finish();
let message = Message::from_bytes(&message).unwrap();
assert_eq!(message.method(), BINDING);
Source

fn has_method(&self, method: Method) -> bool

Returns whether the Message is of the specified method.

§Examples
let message = Message::builder_request(BINDING, MessageWriteVec::new()).finish();
let message = Message::from_bytes(&message).unwrap();
assert!(message.has_method(BINDING));
assert!(!message.has_method(Method::new(0)));
Source

fn transaction_id(&self) -> TransactionId

Retrieves the 96-bit transaction ID of the Message.

§Examples
let mtype = MessageType::from_class_method(MessageClass::Request, BINDING);
let transaction_id = TransactionId::generate();
let message = Message::builder(mtype, transaction_id, MessageWriteVec::new()).finish();
let message = Message::from_bytes(&message).unwrap();
assert_eq!(message.transaction_id(), transaction_id);
Source

fn add_message_integrity( &mut self, credentials: &MessageIntegrityCredentials, algorithm: IntegrityAlgorithm, ) -> Result<(), StunWriteError>

Adds MESSAGE_INTEGRITY attribute to a Message using the provided credentials.

§Errors
§Examples
let mut message = Message::builder_request(BINDING, MessageWriteVec::new());
let credentials = ShortTermCredentials::new("pass".to_owned()).into();
assert!(message.add_message_integrity(&credentials, IntegrityAlgorithm::Sha1).is_ok());

// duplicate MessageIntegrity is an error
assert!(matches!(
    message.add_message_integrity(&credentials, IntegrityAlgorithm::Sha1),
    Err(StunWriteError::AttributeExists(MessageIntegrity::TYPE)),
));

// both MessageIntegrity, and MessageIntegritySha256 are allowed, however Sha256 must be
// after Sha1
assert!(message.add_message_integrity(&credentials, IntegrityAlgorithm::Sha256).is_ok());

let data = message.finish();
let message = Message::from_bytes(&data).unwrap();
assert!(message.validate_integrity(&credentials).is_ok());
Source

fn add_message_integrity_with_key( &mut self, key: &IntegrityKey, algorithm: IntegrityAlgorithm, ) -> Result<(), StunWriteError>

Adds MESSAGE_INTEGRITY attribute to a Message using the provided credential key.

Source

unsafe fn add_message_integrity_with_key_unchecked( &mut self, key: &IntegrityKey, algorithm: IntegrityAlgorithm, )

Adds MESSAGE_INTEGRITY attribute to a Message using the provided credential key.

§Safety

The caller must ensure that:

  1. The provided writer has enough room to store the new allocated message integrity attribute applicable for the relevant algorithm.
  2. That another integrity algorithm attribute has not already been added to the message.
  3. That the Fingerprint attribute has not been added to the message.
Source

fn add_fingerprint(&mut self) -> Result<(), StunWriteError>

Adds Fingerprint attribute to a Message.

§Errors
§Examples
let mut message = Message::builder_request(BINDING, MessageWriteVec::new());
assert!(message.add_fingerprint().is_ok());

// duplicate FINGERPRINT is an error
assert!(message.add_fingerprint().is_err());
Source

unsafe fn add_fingerprint_unchecked(&mut self)

Adds Fingerprint attribute to a Message.

§Safety

The caller must ensure that:

  1. The provided writer has enough room to store the new allocated message integrity attribute applicable for the relevant algorithm.
  2. That the Fingerprint attribute has not been added to the message.
Source

fn add_attribute( &mut self, attr: &dyn AttributeWrite, ) -> Result<(), StunWriteError>

Add a Attribute to this Message.

§Errors
§Panics
§Examples

Add an Attribute.

let mut message = Message::builder_request(BINDING, MessageWriteVec::new());
let attr = RawAttribute::new(1.into(), &[3]);
assert!(message.add_attribute(&attr).is_ok());
// Duplicate attributes are allowed, however semantically, the protocol may not allow them.
assert!(message.add_attribute(&attr).is_ok());

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§