#stun #asynchronous

rustun

A library for implementing STUN server and client asynchronously

18 releases

0.3.7 Sep 11, 2020
0.3.6 Sep 10, 2020
0.3.5 Oct 23, 2018
0.3.4 Sep 17, 2018
0.1.0 Feb 27, 2017

#118 in Asynchronous

Download history 9/week @ 2020-11-13 13/week @ 2020-11-20 37/week @ 2020-11-27 28/week @ 2020-12-04 10/week @ 2020-12-11 8/week @ 2020-12-18 9/week @ 2020-12-25 29/week @ 2021-01-01 12/week @ 2021-01-08 35/week @ 2021-01-15 15/week @ 2021-01-22 13/week @ 2021-01-29 27/week @ 2021-02-05 69/week @ 2021-02-12 34/week @ 2021-02-19 15/week @ 2021-02-26

388 downloads per month
Used in rusturn

MIT license

78KB
1.5K SLoC

rustun

Crates.io: rustun Documentation Actions Status Coverage Status 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:

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([])) }]
}))

Dependencies

~2.5MB
~56K SLoC