STUN Coder is a STUN protocol encoder and decoder for Rust. The implementation is done according to Session Traversal Utilities for NAT (STUN). STUN extensions specified by the Interactive Connectivity Establishment (ICE) protocol are also supported.
An example of creating and encoding a STUN binding request:
```rust
// Create a request message let message = stuncoder::StunMessage::createrequest() .addattribute(stuncoder::StunAttribute::Software { description: String::from("rust-stun-coder"), }) .addmessageintegrity() .add_fingerprint();
// Encode it into bytes let encodedmessage = message.encode(Some("TESTPASS")).unwrap();
println!("{:#X?}", encoded_message);
```
An example that decodes a sample request with Long-Term Authentication
```rust
// Encoded message
let msg_bytes: Vec
// Integrity key used for verification let integrity_key = Some("VOkJxbRl1RmTxUk/WvJxBt");
// Decode the message let decodedmsg = stuncoder::StunMessage::decode(&msgbytes, integritykey).unwrap();
println!("{:?}", decoded_msg); ```
Example function that fetches the server reflexive address of all the local interfaces:
```rust use std::io::{Error, ErrorKind}; use std::net::{SocketAddr, UdpSocket};
// Fetches mapped address of a local Socket
fn getmappedaddr(bindingaddr: SocketAddr) -> Result
// Create a binding message
let binding_msg = stun_coder::StunMessage::create_request()
.add_attribute(stun_coder::StunAttribute::Software {
description: String::from("rust-stun-coder"),
}) // Add software attribute
.add_message_integrity() // Add message integrity attribute
.add_fingerprint(); // Add fingerprint attribute
let integrity_pass = "STUN_CODER_PASS"; // Integrity password to use
// Encode the binding_msg
let bytes = binding_msg.encode(Some(integrity_pass)).unwrap();
// Open a UDP socket
let udp_socket = UdpSocket::bind(binding_addr)?;
// Connect to the STUN server
udp_socket.connect(stun_server.clone())?;
// Send the binding request message
udp_socket.send(&bytes)?;
// Wait for a response
let mut response_buf = [0; 32];
udp_socket.recv(&mut response_buf)?;
// Decode the response
let stun_response =
stun_coder::StunMessage::decode(&response_buf, Some(integrity_pass)).unwrap();
// Find the XorMappedAddress attribute in the response
// It will contain our reflexive transport address
for attr in stun_response.get_attributes() {
if let stun_coder::StunAttribute::XorMappedAddress { socket_addr } = attr {
return Ok(*socket_addr);
}
}
Err(Error::new(
ErrorKind::InvalidData,
"No XorMappedAddress has been set in response.",
))
}
// Fetches server reflexive addresses of local interfaces fn getmappedaddresses() { // Gather local interfaces let localinterfaces = getifaddrs::getif_addrs().unwrap();
// Attempt to get a mapped address for each one of them
for interface in local_interfaces.iter() {
// Exclude loopback interfaces
if interface.is_loopback() {
continue;
}
// Form a local socket for the interface on port 2000
let host_addr = interface.ip();
let binding_addr = SocketAddr::new(host_addr, 2000);
match get_mapped_addr(binding_addr) {
Ok(mapped_socket_addr) => {
println!(
"Mapped host address {} to remote {}.",
binding_addr, mapped_socket_addr
);
}
Err(err) => {
println!(
"Failed to map host address {}. Error: {}.",
binding_addr, err
);
}
}
}
} ```
Ruben Harutyunyan (@Vagr9K)