Skip to content

Commit

Permalink
Stun example: continue after error
Browse files Browse the repository at this point in the history
  • Loading branch information
inetic committed Oct 18, 2024
1 parent 67c8faf commit b38d8e5
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions net/examples/stun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ async fn main() -> io::Result<()> {
)?);

for server_name in options.servers {
for server_addr in net::lookup_host(server_name).await? {
println!("STUN server {server_addr}:");
for server_addr in net::lookup_host(server_name.clone()).await? {
println!("STUN server {server_name} ({server_addr}):");

let client = match server_addr {
SocketAddr::V4(_) => &client_v4,
SocketAddr::V6(_) => &client_v6,
};

let result = client.external_addr(server_addr).await;
println!(" external address: {result:?}");
println!(" external address: {}", format_display(result));

let result = client.nat_filtering(server_addr).await?;
println!(" NAT filtering behavior: {result:?}");
let result = client.nat_filtering(server_addr).await;
println!(" NAT filtering behavior: {}", format_debug(result));

let result = client.nat_mapping(server_addr).await?;
println!(" NAT mapping behavior: {result:?}");
let result = client.nat_mapping(server_addr).await;
println!(" NAT mapping behavior: {}", format_debug(result));

println!();
}
Expand All @@ -50,3 +50,17 @@ struct Options {
#[arg(short, long, default_value = "stun1.l.google.com:19305")]
servers: Vec<String>,
}

fn format_debug<T: std::fmt::Debug>(result: io::Result<T>) -> String {
match result {
Ok(value) => format!("{value:?}"),
Err(err) => format!("Error: {err}"),
}
}

fn format_display<T: std::fmt::Display>(result: io::Result<T>) -> String {
match result {
Ok(value) => format!("{value}"),
Err(err) => format!("Error: {err}"),
}
}

0 comments on commit b38d8e5

Please sign in to comment.