port_check
A simple rust library to get a free local port or to check if a port somewhere is reachable.
To be clear, checking whether a port is open does not guarantee that it will still be open by the time you attempt to use it. Make sure your code is designed to handle potential time-of-check to time-of-use (TOCTOU) issues.
To help mitigate this, the library provides a retry-based approach via the with_free_port functions.
Example:
use *;
use *;
use Duration;
// --------------------------------------------------------------------
// If not specified, all port checks are performed for IPv4 addresses.
// --------------------------------------------------------------------
// get a free local port
let free_port = free_local_port.unwrap;
// get a free local port between 10000 and 15000
let free_port_in_range = free_local_port_in_range;
// check whether a remote port is reachable
let is_reachable = is_port_reachable;
// or
let is_reachable = is_port_reachable_with_timeout;
// --------------------------------------------------------------------
// IPv6 checks are supported too
// --------------------------------------------------------------------
let free_ipv6_port = free_local_ipv6_port.unwrap;
let is_ipv6_port_free = is_local_port_free;
// or
let is_ipv6_port_free = is_local_ipv6_port_free;
// --------------------------------------------------------------------
// Retry-based approach
//
// The library provides a retry-based approach via the with_free_port(...) function
// to help mitigate TOC/TOU race conditions.
//
// --------------------------------------------------------------------
let = .unwrap;
let = .unwrap;
let = .unwrap;