Crate stun-rs
This crate provides a simple but high effective framework to manage STUN protocol messages. The implementation is based on:
RFC5769
. Test Vectors for Session Traversal Utilities for NAT (STUN).RFC5780
. NAT Behavior Discovery Using Session Traversal Utilities for NAT (STUN).RFC8016
. Mobility with Traversal Using Relays around NAT (TURN).RFC8445
. Interactive Connectivity Establishment (ICE).RFC8489
. Session Traversal Utilities for NAT (STUN).RFC8656
. Traversal Using Relays around NAT (TURN).
Usage
Example that creates and encodes a STUN Binding request
// Create attributes
let username = new?;
let nonce = new?;
let realm = new?;
let password = "TheMatrIX";
let algorithm = from;
let key = new_long_term?;
let integrity = new;
// Create the message
let msg = new
.with_attribute
.with_attribute
.with_attribute
.with_attribute
.build;
// Create an encoder to encode the message into a buffer
let encoder = default.build;
let mut buffer: = ;
let size = encoder.encode?;
assert_eq!;
Example that decodes a STUN Binding response and fetches some attributes.
// This response uses the following parameter:
// Password: `VOkJxbRl1RmTxUk/WvJxBt` (without quotes)
// Software name: "test vector" (without quotes)
// Mapped address: 192.0.2.1 port 32853
let sample_ipv4_response = ;
// Create a STUN decoder context using the password as a short credential
// mechanism and force validation of MESSAGE-INTEGRITY and FINGERPRINT
let ctx = default
.with_key
.with_validation
.build;
let decoder = default.with_context.build;
let = decoder.decode?;
assert_eq!;
// Check message method is a BINDING response
assert_eq!;
assert_eq!;
let software = msg.
.ok_or?
.as_software?;
assert_eq!;
let xor_addr = msg.
.ok_or?
.as_xor_mapped_address?;
let socket = xor_addr.socket_address;
assert_eq!;
assert_eq!;
assert!;
Common features
This crate defines next feature flags that can be enabled:
- discovery: Extends support for parsing attributes defined in
RFC5780
. NAT Behavior Discovery Using Session Traversal Utilities for NAT (STUN). - mobility: Extends support for parsing attributes defined in
RFC8016
. Mobility with Traversal Using Relays around NAT (TURN). - turn: Extends support for parsing attributes defined in
RFC8656
. Traversal Using Relays around NAT (TURN). - ice: Extends support for parsing attributes defined in
RFC8445
. Interactive Connectivity Establishment (ICE). - experiments: This flag can be set to adjust some behavior of the library, such as default padding. When testing protocols, we can use this flag to force the library to keep the data associated with unknown attributes. By default, unknown attributes store no data to save memory consumption.
Contributing
Patches and feedback are welcome.
Donations
If you find this project helpful, you may consider making a donation:
License
This project is licensed under either of