Expand description
Encoders and decoders for STUN (RFC 5389) and its extensions.
§Examples
use bytecodec::{DecodeExt, EncodeExt, Error};
use stun_codec::{Message, MessageClass, MessageDecoder, MessageEncoder, TransactionId};
use stun_codec::rfc5389::{attributes::Software, methods::BINDING, Attribute};
// Creates a message
let mut message = Message::new(MessageClass::Request, BINDING, TransactionId::new([3; 12]));
message.add_attribute(Attribute::Software(Software::new("foo".to_owned())?));
// Encodes the message
let mut encoder = MessageEncoder::new();
let bytes = encoder.encode_into_bytes(message.clone())?;
assert_eq!(
bytes,
[
0, 1, 0, 8, 33, 18, 164, 66, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 128, 34, 0, 3,
102, 111, 111, 0
]
);
// Decodes the message
let mut decoder = MessageDecoder::<Attribute>::new();
let decoded = decoder.decode_from_bytes(&bytes)?.map_err(Error::from)?;
assert_eq!(decoded.class(), message.class());
assert_eq!(decoded.method(), message.method());
assert_eq!(decoded.transaction_id(), message.transaction_id());
assert!(decoded.attributes().eq(message.attributes()));
§Define your own attribute enum
By using define_attribute_enums!
macro,
you can easily define an enum that includes arbitrary attributes.
The following is an example taken from rusturn
crate:
#[macro_use] extern crate trackable;
use stun_codec::define_attribute_enums;
use stun_codec::rfc5389::attributes::*;
use stun_codec::rfc5766::attributes::*;
define_attribute_enums!(
Attribute, AttributeDecoder, AttributeEncoder,
[
// RFC 5389
MappedAddress, Username, MessageIntegrity, ErrorCode,
UnknownAttributes, Realm, Nonce, XorMappedAddress,
Software, AlternateServer, Fingerprint,
// RFC 5766
ChannelNumber, Lifetime, XorPeerAddress, Data,
XorRelayAddress, EvenPort, RequestedTransport,
DontFragment, ReservationToken
]
);
§References
- RFC 5389 - Session Traversal Utilities for NAT (STUN)
- RFC 5769 - Test Vectors for Session Traversal Utilities for NAT (STUN)
- RFC 5245 - Interactive Connectivity Establishment (ICE)
- RFC 5780 - NAT Behavior Discovery Using Session Traversal Utilities for NAT
- RFC 3489 - Simple Traversal of User Datagram Protocol Through Network Address Translators
Modules§
- convert
- Conversion traits.
- net
- Socket address related components.
- rfc3489
- RFC 3489 specific components.
- rfc5245
- RFC 5245(ICE) specific components.
- rfc5389
- RFC 5389(STUN) specific components.
- rfc5766
- RFC 5766(TURN) specific components.
- rfc5780
- RFC 5780(NAT Behavior Discovery) specific components.
Macros§
- define_
attribute_ enums - Defines an aggregated attribute type and its decoder and encoder.
Structs§
- Attribute
Type - Attribute type.
- Broken
Message - STUN message of which
MessageDecoder
could not decode the attribute part. - Message
- STUN message.
- Message
Decoder Message
decoder.- Message
Encoder Message
encoder.- Method
- STUN method.
- RawAttribute
- An
Attribute
implementation that has raw value bytes. - RawAttribute
Decoder RawAttribute
decoder.- RawAttribute
Encoder RawAttribute
encoder.- Transaction
Id - Transaction ID.
Enums§
- Message
Class - The class of a message.
Traits§
- Attribute
- STUN attribute.
Type Aliases§
- Decoded
Message - Message decoded by
MessageDecoder
.