1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::net::{Ipv4Addr, SocketAddrV4, TcpListener, TcpStream, ToSocketAddrs};
use std::time::Duration;

/// Attempts a TCP connection to an address and returns whether it succeeded
pub fn is_port_reachable<A: ToSocketAddrs>(address: A) -> bool {
    TcpStream::connect(address).is_ok()
}

/// Attempts a TCP connection to an address and returns whether it succeeded
pub fn is_port_reachable_with_timeout<A: ToSocketAddrs>(address: A, timeout: Duration) -> bool {
    match address.to_socket_addrs() {
        Ok(addrs) => {
            for address in addrs {
                if TcpStream::connect_timeout(&address, timeout).is_ok() {
                    return true;
                }
            };
            false
        },
        Err(_err) => {
            false
        }
    }
}

/// Returns whether a port is available on the localhost
pub fn is_local_port_free(port: u16) -> bool {
    let ipv4 = SocketAddrV4::new(Ipv4Addr::LOCALHOST, port);
    TcpListener::bind(ipv4).is_ok()
}

/// Returns an available localhost port within the specified range.
///
/// 'min' and 'max' values are included in the range
///
pub fn free_local_port_in_range(min: u16, max: u16) -> Option<u16> {
    (min..max).find(|port| is_local_port_free(*port))
}

/// Returns an available localhost port
pub fn free_local_port() -> Option<u16> {
    let socket = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0);
    TcpListener::bind(socket)
        .and_then(|listener| listener.local_addr())
        .and_then(|addr| Ok(addr.port()))
        .ok()
}

#[cfg(test)]
mod tests {

    use super::*;
    use std::net::{Ipv4Addr, SocketAddrV4, TcpListener};
    use std::time::Instant;
    use std::{thread, time::Duration};

    #[test]
    fn should_return_an_unused_port() {
        let result = free_local_port();
        assert!(result.is_some());
        assert!(is_local_port_free(result.unwrap()));
    }

    #[test]
    fn should_return_an_unused_port_in_range() {
        let free_port = free_local_port().unwrap();
        let min = free_port - 100;
        let max = free_port;
        let port_found = free_local_port_in_range(min, max).unwrap();
        assert!(port_found >= min);
        assert!(port_found <= max);
    }

    #[test]
    fn a_free_port_should_not_be_reachable() {
        let available_port = free_local_port().unwrap();
        assert!(!is_port_reachable(&format!("127.0.0.1:{}", available_port)));
    }

    #[test]
    fn free_port_should_resolve_domain_name() {
        let available_port = free_local_port().unwrap();
        assert!(!is_port_reachable(&format!("localhost:{}", available_port)));
    }

    #[test]
    fn an_open_port_should_be_reachable() {
        let socket = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0);
        let listener = TcpListener::bind(socket).unwrap();
        let listener_port = listener.local_addr().unwrap().to_string();

        thread::spawn(move || loop {
            match listener.accept() {
                Ok(_) => {
                    println!("Connection received!");
                }
                Err(_) => {
                    println!("Error in received connection!");
                }
            }
        });

        let mut port_reachable = false;
        while !port_reachable {
            println!("Check for available connections on {}", &listener_port);
            port_reachable = is_port_reachable(&listener_port);
            thread::sleep(Duration::from_millis(10));
        }
        assert!(port_reachable)
    }

    #[test]
    fn is_port_reachable_should_respect_timeout() {
        let timeout = 100;
        let start = Instant::now();

        assert!(!is_port_reachable_with_timeout(
            "198.19.255.255:1",
            Duration::from_millis(timeout)
        ));

        let elapsed = (start.elapsed().subsec_nanos() / 1000000) as u64;
        println!("Millis elapsed {}", elapsed);
        assert!(elapsed >= timeout);
        assert!(elapsed < 2 * timeout);
    }

    #[test]
    fn free_port_with_timeout_should_resolve_domain_name() {
        let available_port = free_local_port().unwrap();
        assert!(!is_port_reachable_with_timeout(
            &format!("localhost:{}", available_port),
            Duration::from_millis(10)
        ));
    }

    #[test]
    fn an_open_port_should_be_reachable_with_timeout() {
        let socket = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0);
        let listener = TcpListener::bind(socket).unwrap();
        let listener_port = listener.local_addr().unwrap().to_string();

        thread::spawn(move || loop {
            match listener.accept() {
                Ok(_) => {
                    println!("Connection received!");
                }
                Err(_) => {
                    println!("Error in received connection!");
                }
            }
        });

        let mut port_reachable = false;
        while !port_reachable {
            println!("Check for available connections on {}", &listener_port);
            port_reachable = is_port_reachable_with_timeout(&listener_port, Duration::from_secs(1));
            thread::sleep(Duration::from_millis(10));
        }
        assert!(port_reachable)
    }
}