Crates.io | rustun |
lib.rs | rustun |
version | 0.5.0 |
source | src |
created_at | 2017-02-27 10:36:47.851537 |
updated_at | 2024-02-23 00:52:11.457186 |
description | A library for implementing STUN server and client asynchronously |
homepage | https://github.com/sile/rustun |
repository | https://github.com/sile/rustun |
max_upload_size | |
id | 8710 |
size | 103,577 |
A Rust library for implementing STUN server and client asynchronously.
The STUN protocol is defined in RFC 5389.
An example that issues a BINDING
request:
use fibers_transport::UdpTransporter;
use futures::Future;
use rustun::channel::Channel;
use rustun::client::Client;
use rustun::message::Request;
use rustun::server::{BindingHandler, UdpServer};
use rustun::transport::StunUdpTransporter;
use rustun::Error;
use stun_codec::{rfc5389, MessageDecoder, MessageEncoder};
let addr = "127.0.0.1:0".parse().unwrap();
// Starts UDP server
let server = fibers_global::execute(UdpServer::start(fibers_global::handle(), addr, BindingHandler))?;
let server_addr = server.local_addr();
fibers_global::spawn(server.map(|_| ()).map_err(|e| panic!("{}", e)));
// Sents BINDING request
let response = UdpTransporter::<MessageEncoder<_>, MessageDecoder<_>>::bind(addr)
.map_err(Error::from)
.map(StunUdpTransporter::new)
.map(Channel::new)
.and_then(move |channel| {
let client = Client::new(&fibers_global::handle(), channel);
let request = Request::<rfc5389::Attribute>::new(rfc5389::methods::BINDING);
client.call(server_addr, request)
});
// Waits BINDING response
let response = fibers_global::execute(response)?;
assert!(response.is_ok());
You can run example server and client which handle Binding
method as follows:
// Starts the STUN server in a shell.
$ cargo run --example binding_srv
// Executes a STUN client in another shell.
$ cargo run --example binding_cli -- 127.0.0.1
Ok(SuccessResponse(Message {
class: SuccessResponse,
method: Method(1),
transaction_id: TransactionId(0x344A403694972F5E53B69465),
attributes: [Known { inner: XorMappedAddress(XorMappedAddress(V4(127.0.0.1:54754))),
padding: Some(Padding([])) }]
}))