Crate port_check

Source
Expand description

§port_check

crates.io Build Status codecov

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 port_check::*;
use std::net::*;
use std::time::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(10000..=15000);

// check whether a remote port is reachable
let is_reachable = is_port_reachable("192.0.2.0:8080");
// or
let is_reachable = is_port_reachable_with_timeout("192.0.2.0:8080", Duration::from_millis(10_000));



// --------------------------------------------------------------------
// IPv6 checks are supported too
// --------------------------------------------------------------------

let free_ipv6_port = free_local_ipv6_port().unwrap();

let is_ipv6_port_free = is_local_port_free(Port::ipv6(free_ipv6_port));
// or
let is_ipv6_port_free = is_local_ipv6_port_free(free_ipv6_port);



// --------------------------------------------------------------------
// Retry-based approach
//
// The library provides a retry-based approach via the with_free_port(...) function
// to help mitigate TOC/TOU race conditions.
//
// --------------------------------------------------------------------


let (server, port) = with_free_port::<_, std::io::Error, _>(|port| {
    // Trying to use the port. If it fails, we try again with another port.
    let listener = TcpListener::bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port))?;
    Ok(listener)
}).unwrap();

let (server, port) = with_free_ipv4_port::<_, std::io::Error, _>(|port| {
    // Trying to use the port. If it fails, we try again with another port.
    let listener = TcpListener::bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port))?;
    Ok(listener)
}).unwrap();


let (server, port) = with_free_ipv6_port::<_, std::io::Error, _>(|port| {
    // Trying to use the port. If it fails, we try again with another port.
    let listener = TcpListener::bind(SocketAddrV6::new(Ipv6Addr::LOCALHOST, port, 0, 0))?;
    Ok(listener)
}).unwrap();

Enums§

Port
Represents a port for an IP address
Ports
Represents a port range for an IP address

Functions§

free_local_ipv4_port
Returns an available localhost port for IPv4.
free_local_ipv4_port_in_range
Returns an available localhost port within the specified range for IPv4.
free_local_ipv6_port
Returns an available localhost port for IPv6.
free_local_ipv6_port_in_range
Returns an available localhost port within the specified range for IPv6.
free_local_port
Returns an available localhost port for IPv4.
free_local_port_in_range
Returns an available localhost port within the specified range. If the IP version is not specified, it defaults to IPv4. This happens when the port range is specified as a range.
is_local_ipv4_port_free
Returns whether a port is available on the localhost for IPv4.
is_local_ipv6_port_free
Returns whether a port is available on the localhost for IPv6
is_local_port_free
Returns whether a port is available on the localhost If the IP version is not specified, it defaults to IPv4. This happens when the port is specified as a number.
is_port_reachable
Attempts a TCP connection to an address and returns whether it succeeded.
is_port_reachable_with_timeout
Attempts a TCP connection to an address and returns whether it succeeded
with_free_ipv4_port
Attempts to find a free IPv4 port on localhost and applies the provided function f to it.
with_free_ipv6_port
Attempts to find a free IPv6 port on localhost and applies the provided function f to it.
with_free_port
Alias for with_free_ipv4_port-