#stun #nat #ip-address #sockets #rfc5389

stunclient

Simple STUN client for resolving external IP address and port of a UDP socket

9 releases

0.4.1 May 21, 2024
0.4.0 Dec 30, 2022
0.3.1 Oct 29, 2021
0.3.0 Jul 25, 2021
0.1.0 Jan 31, 2019

#1263 in Network programming

Download history 169/week @ 2024-07-22 663/week @ 2024-07-29 578/week @ 2024-08-05 628/week @ 2024-08-12 514/week @ 2024-08-19 372/week @ 2024-08-26 1126/week @ 2024-09-02 1859/week @ 2024-09-09 1652/week @ 2024-09-16 1514/week @ 2024-09-23 796/week @ 2024-09-30 1020/week @ 2024-10-07 998/week @ 2024-10-14 720/week @ 2024-10-21 1357/week @ 2024-10-28 290/week @ 2024-11-04

3,478 downloads per month
Used in turnhammer

MIT/Apache

14KB
230 lines

Simple UDP-only STUN client for resolving external IP address:port behind NAT.

Supports both sync and async.

Example (sync):

use std::net::UdpSocket;
use stunclient::StunClient;
use std::net::{SocketAddr,ToSocketAddrs};
let local_addr : SocketAddr = "0.0.0.0:0".parse().unwrap();
let stun_addr = "stun.l.google.com:19302".to_socket_addrs().unwrap().filter(|x|x.is_ipv4()).next().unwrap();
let udp = UdpSocket::bind(local_addr).unwrap();

let c = StunClient::new(stun_addr);

let my_external_addr = c.query_external_address(&udp).unwrap();

Example (async):

use stunclient::StunClient;
use std::net::{SocketAddr,ToSocketAddrs};

let local_addr : SocketAddr = "0.0.0.0:0".parse().unwrap();
let stun_addr = "stun.l.google.com:19302".to_socket_addrs().unwrap().filter(|x|x.is_ipv4()).next().unwrap();
let udp = tokio::net::udp::UdpSocket::bind(&local_addr).unwrap();

let c = StunClient::new(stun_addr);
let f = c.query_external_address_async(&udp);
let my_external_addr = f.await.unwrap();

Old version (0.1) of stunclient is almost the same, but is for Tokio 0.1.

Dependencies

~3–11MB
~123K SLoC