hpip 0.2.0

Host These Things Please - a modern async HTTP file server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use std::net::{IpAddr, SocketAddr};

use tokio::net::TcpListener;

/// Attempt to find a free port in the range [from, up_to] inclusive.
pub async fn find_port(addr: IpAddr, from: u16, up_to: u16) -> Option<u16> {
    for port in from..=up_to {
        let socket = SocketAddr::new(addr, port);
        if TcpListener::bind(socket).await.is_ok() {
            return Some(port);
        }
    }
    None
}