pub trait MessageWriteExt: MessageWrite {
// 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_fingerprint(&mut self) -> Result<(), StunWriteError> { ... }
fn add_attribute(
&mut self,
attr: &dyn AttributeWrite,
) -> Result<(), StunWriteError> { ... }
}
Expand description
Extension trait for MessageWrite
providing helper functions.
Provided Methods§
Sourcefn get_type(&self) -> MessageType
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);
Sourcefn class(&self) -> MessageClass
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);
Sourcefn has_class(&self, cls: MessageClass) -> bool
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));
Sourcefn is_response(&self) -> bool
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);
Sourcefn has_method(&self, method: Method) -> bool
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_eq!(message.has_method(BINDING), true);
assert_eq!(message.has_method(Method::new(0)), false);
Sourcefn transaction_id(&self) -> TransactionId
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);
Sourcefn add_message_integrity(
&mut self,
credentials: &MessageIntegrityCredentials,
algorithm: IntegrityAlgorithm,
) -> Result<(), StunWriteError>
fn add_message_integrity( &mut self, credentials: &MessageIntegrityCredentials, algorithm: IntegrityAlgorithm, ) -> Result<(), StunWriteError>
Adds MESSAGE_INTEGRITY attribute to a Message
using the provided credentials
§Errors
- If a
MessageIntegrity
attribute is already present - If a
MessageIntegritySha256
attribute is already present - If a
Fingerprint
attribute is already present
§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());
Sourcefn add_fingerprint(&mut self) -> Result<(), StunWriteError>
fn add_fingerprint(&mut self) -> Result<(), StunWriteError>
Adds Fingerprint
attribute to a Message
§Errors
- If a
Fingerprint
attribute is already present
§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());
Sourcefn add_attribute(
&mut self,
attr: &dyn AttributeWrite,
) -> Result<(), StunWriteError>
fn add_attribute( &mut self, attr: &dyn AttributeWrite, ) -> Result<(), StunWriteError>
Add a Attribute
to this Message
. Only one AttributeType
can be added for each
Attribute. Attempting to add multiple
Atributes of the same
AttributeType` will fail.
§Errors
- If the attribute already exists within the message
- If attempting to add attributes when
MessageIntegrity
,MessageIntegritySha256
orFingerprint
atributes already exist.
§Panics
- if a
MessageIntegrity
orMessageIntegritySha256
attribute is attempted to be added. UseMessage::add_message_integrity
instead. - if a
Fingerprint
attribute is attempted to be added. UseMessage::add_fingerprint
instead.
§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());
assert!(message.add_attribute(&attr).is_err());