Crate stun_rs
This crate provides a simple but high effective framework to manage STUN protocol messages. The implementation is based on:
RFC8489. Session Traversal Utilities for NAT (STUN).RFC8445. Interactive Connectivity Establishment (ICE).RFC8656. Traversal Using Relays around NAT (TURN)RFC5769. Test Vectors for Session Traversal Utilities for NAT (STUN).
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!;
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