portpicker-0.1.1/.cargo_vcs_info.json0000644000000001120000000000000132060ustar { "git": { "sha1": "47842740f9a42423701b8b041de2e4a219cb6684" } } portpicker-0.1.1/.gitignore000064400000000000000000000000360000000000000137510ustar 00000000000000/target **/*.rs.bk Cargo.lock portpicker-0.1.1/Cargo.toml0000644000000015740000000000000112210ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "portpicker" version = "0.1.1" authors = ["Hannes Karppila "] description = "Pick a free unused port" readme = "README.md" keywords = ["port", "network"] categories = ["network-programming"] license = "Unlicense" repository = "https://github.com/Dentosal/portpicker-rs" [dependencies.rand] version = "0.8" portpicker-0.1.1/Cargo.toml.orig000064400000000000000000000005540000000000000146550ustar 00000000000000[package] name = "portpicker" description = "Pick a free unused port" version = "0.1.1" authors = ["Hannes Karppila "] edition = "2018" repository = "https://github.com/Dentosal/portpicker-rs" readme = "README.md" keywords = ["port", "network"] categories = ["network-programming"] license = "Unlicense" [dependencies] rand = "0.8" portpicker-0.1.1/LICENSE000075500000000000000000000023200000000000000127670ustar 00000000000000This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information, please refer to portpicker-0.1.1/README.md000075500000000000000000000002670000000000000132510ustar 00000000000000# Portpicker-rs - Pick a free unused port Picks a free port, that is unused on both TCP and UDP. Usage: ```rust portpicker::pick_unused_port().expect("No ports free") ``` portpicker-0.1.1/src/lib.rs000064400000000000000000000044050000000000000136700ustar 00000000000000use rand::prelude::*; use std::net::{ Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, TcpListener, ToSocketAddrs, UdpSocket, }; pub type Port = u16; // Try to bind to a socket using UDP fn test_bind_udp(addr: A) -> Option { Some(UdpSocket::bind(addr).ok()?.local_addr().ok()?.port()) } // Try to bind to a socket using TCP fn test_bind_tcp(addr: A) -> Option { Some(TcpListener::bind(addr).ok()?.local_addr().ok()?.port()) } /// Check if a port is free on UDP pub fn is_free_udp(port: Port) -> bool { let ipv4 = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port); let ipv6 = SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, port, 0, 0); test_bind_udp(ipv6).is_some() && test_bind_udp(ipv4).is_some() } /// Check if a port is free on TCP pub fn is_free_tcp(port: Port) -> bool { let ipv4 = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port); let ipv6 = SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, port, 0, 0); test_bind_tcp(ipv6).is_some() && test_bind_tcp(ipv4).is_some() } /// Check if a port is free on both TCP and UDP pub fn is_free(port: Port) -> bool { is_free_tcp(port) && is_free_udp(port) } /// Asks the OS for a free port fn ask_free_tcp_port() -> Option { let ipv4 = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0); let ipv6 = SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0); test_bind_tcp(ipv6).or_else(|| test_bind_tcp(ipv4)) } /// Picks an available port that is available on both TCP and UDP /// ```rust /// use portpicker::pick_unused_port; /// let port: u16 = pick_unused_port().expect("No ports free"); /// ``` pub fn pick_unused_port() -> Option { let mut rng = rand::thread_rng(); // Try random port first for _ in 0..10 { let port = rng.gen_range(15000..25000); if is_free(port) { return Some(port); } } // Ask the OS for a port for _ in 0..10 { if let Some(port) = ask_free_tcp_port() { // Test that the udp port is free as well if is_free_udp(port) { return Some(port); } } } // Give up None } #[cfg(test)] mod tests { use super::pick_unused_port; #[test] fn it_works() { assert!(pick_unused_port().is_some()); } }