#stun #nat #ip-address #rfc5389

stunclient

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

8 releases

0.4.0 Dec 30, 2022
0.3.1 Oct 29, 2021
0.3.0 Jul 25, 2021
0.2.1 Jul 24, 2021
0.1.0 Jan 31, 2019

#16 in #stun

Download history 415/week @ 2023-12-12 229/week @ 2023-12-19 186/week @ 2023-12-26 330/week @ 2024-01-02 496/week @ 2024-01-09 493/week @ 2024-01-16 528/week @ 2024-01-23 633/week @ 2024-01-30 808/week @ 2024-02-06 686/week @ 2024-02-13 603/week @ 2024-02-20 555/week @ 2024-02-27 550/week @ 2024-03-05 571/week @ 2024-03-12 492/week @ 2024-03-19 414/week @ 2024-03-26

2,108 downloads per month
Used in turnhammer

MIT/Apache

13KB
210 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

~2–13MB
~122K SLoC