use netsem::{check_bind_tcp, check_bind_udp, check_connect_tcp, check_connect_udp};
#[cfg(feature = "check")]
use std::time::Duration;
fn main() {
#[cfg(not(feature = "check"))]
{
println!("This example requires the 'check' feature to perform OS operations.");
println!("Please run with: cargo run --example os_check --features check");
}
#[cfg(feature = "check")]
{
let localhost = "127.0.0.1".parse().unwrap();
println!("Checking if we can bind TCP to 127.0.0.1:0...");
match check_bind_tcp(localhost, 0) {
Ok(_) => println!("Successfully verified TCP bind capability on an ephemeral port."),
Err(e) => eprintln!("TCP Bind check failed: {e}"),
}
println!("Checking if we can bind UDP to 127.0.0.1:0...");
match check_bind_udp(localhost, 0) {
Ok(_) => println!("Successfully verified UDP bind capability on an ephemeral port."),
Err(e) => eprintln!("UDP Bind check failed: {e}"),
}
let dns_ip = "8.8.8.8".parse().unwrap();
println!("\nChecking TCP connection to Google DNS (8.8.8.8:53) with 2s timeout...");
match check_connect_tcp(dns_ip, 53, Some(Duration::from_secs(2))) {
Ok(_) => println!("Successfully connected to 8.8.8.8:53."),
Err(e) => eprintln!("TCP Connection check failed (this is normal if offline): {e}"),
}
println!("\nChecking UDP 'connection' to Google DNS (8.8.8.8:53)...");
match check_connect_udp(dns_ip, 53) {
Ok(_) => println!("Successfully set default UDP destination to 8.8.8.8:53."),
Err(e) => eprintln!("UDP Connect check failed: {e}"),
}
}
}