rustun

Crates.io: rustun Documentation Build Status Code Coverage License: MIT

A Rust library for implementing STUN server and client asynchronously.

Documentation

The STUN protocol is defined in RFC 5389.

Examples

An example that issues a BINDING request:

```rust use fiberstransport::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 stuncodec::{rfc5389, MessageDecoder, MessageEncoder};

let addr = "127.0.0.1:0".parse().unwrap();

// Starts UDP server let server = fibersglobal::execute(UdpServer::start(fibersglobal::handle(), addr, BindingHandler))?; let serveraddr = server.localaddr(); fibersglobal::spawn(server.map(|| ()).map_err(|e| panic!("{}", e)));

// Sents BINDING request let response = UdpTransporter::, MessageDecoder<_>>::bind(addr) .maperr(Error::from) .map(StunUdpTransporter::new) .map(Channel::new) .andthen(move |channel| { let client = Client::new(&fibersglobal::handle(), channel); let request = Request::::new(rfc5389::methods::BINDING); client.call(serveraddr, request) });

// Waits BINDING response let response = fibersglobal::execute(response)?; assert!(response.isok()); ```

You can run example server and client which handle Binding method as follows:

```console // Starts the STUN server in a shell. $ cargo run --example binding_srv

// Executes a STUN client in another shell. $ cargo run --example bindingcli -- 127.0.0.1 Ok(SuccessResponse(Message { class: SuccessResponse, method: Method(1), transactionid: TransactionId(0x344A403694972F5E53B69465), attributes: [Known { inner: XorMappedAddress(XorMappedAddress(V4(127.0.0.1:54754))), padding: Some(Padding([])) }] })) ```