OpenPort - Find a Free Unused Port
A simple, lightweight library for finding available network ports.
Overview
OpenPort provides a minimal set of functions to:
- Find available ports within a specified range
- Check if specific ports are free on TCP and/or UDP
- Reserve and manage port allocation to prevent conflicts (with
reservationfeature, enabled by default) - Optionally find random available ports (with
randfeature)
Installation
As a Library
[]
= { = "../openport" }
# Or with random port selection feature
# openport = { path = "../openport", features = ["rand"] }
Usage
Basic Usage
use pick_unused_port;
Command Line Usage
# Find any available port (requires 'cli' and 'rand' features)
# Find port in specific range (exclusive) (requires 'cli' feature)
# Find port in inclusive range (requires 'cli' feature)
Random Port Selection (requires rand feature)
use pick_random_unused_port;
Checking Port Availability
use ;
Port Reservation (requires reservation feature, enabled by default)
use PortReservation;
Integration with Web Servers
use pick_unused_port;
use SocketAddr;
Testing Utilities
use pick_unused_port;
API Reference
Functions
/// Find an unused port within the specified range.
/// The port will be available on both TCP and UDP.
/// Returns None if no ports are available in the range.
;
/// Find a random unused port in the range 15000-25000.
/// The port will be available on both TCP and UDP.
/// Returns None if no ports are available after several attempts.
/// Requires the `rand` feature.
;
/// Check if a port is free on both TCP and UDP.
;
/// Check if a port is free on TCP.
;
/// Check if a port is free on UDP.
;
Types
/// Port number type alias
pub type Port = u16;
/// Port reservation system for managing port allocation
/// Requires the `reservation` feature (enabled by default)
pub type PortReservation = PortReservation;
/// Trait for port range types (Range<u16> and RangeInclusive<u16>)
PortReservation Methods (requires reservation feature)
Features
reservation(default): Enables thePortReservationtype for managing port allocation and preventing duplicate port usagecli: Enables the command-line binary for finding available portsrand: Enablespick_random_unused_port()function for finding random ports in the 15000-25000 rangefail-on-warnings: Treats compiler warnings as errors (for development)
Implementation Details
- Ports are checked by attempting to bind on both IPv4 (
0.0.0.0) and IPv6 ([::]) unspecified addresses - A port is considered free only if it can be bound on both address families
- For
pick_unused_port(), ports in the range are checked sequentially until a free one is found - For
pick_random_unused_port(), the function tries 10 random ports first, then asks the OS for free ports
Limitations
- Sequential port checking (no concurrent checks)
- No timeout configuration
- Binding to low ports (<1024) requires root/administrator privileges on most systems
- Race condition: A port that tests as "free" may be taken before you can bind to it (mitigated by using
PortReservation)
Examples
Development Server with Fallback Ports
use pick_unused_port;
License
openport is provided under the MPL v2.0 license. Please refer to the LICENSE file for more details.