From bb6f534626d53b31524eecf28ce41967d3d56f71 Mon Sep 17 00:00:00 2001 From: Milo Turner Date: Mon, 9 Mar 2020 17:18:08 -0400 Subject: [PATCH] let the kernel pick my UDP ports --- Cargo.lock | 74 ------------------------------------------- hptp-recv/src/main.rs | 6 +--- hptp-send/src/main.rs | 6 +--- hptp/Cargo.toml | 1 - hptp/src/lib.rs | 22 ------------- 5 files changed, 2 insertions(+), 107 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 66ef86b..2880c79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,15 +18,6 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1" -[[package]] -name = "c2-chacha" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb" -dependencies = [ - "ppv-lite86", -] - [[package]] name = "cfg-if" version = "0.1.10" @@ -60,23 +51,11 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" -[[package]] -name = "getrandom" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" -dependencies = [ - "cfg-if", - "libc", - "wasi", -] - [[package]] name = "hptp" version = "0.1.0" dependencies = [ "chrono", - "rand", "thiserror", "tokio", ] @@ -212,12 +191,6 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "237844750cfbb86f67afe27eee600dfbbcb6188d734139b534cbfbf4f96792ae" -[[package]] -name = "ppv-lite86" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" - [[package]] name = "proc-macro2" version = "1.0.9" @@ -236,47 +209,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" -dependencies = [ - "getrandom", - "libc", - "rand_chacha", - "rand_core", - "rand_hc", -] - -[[package]] -name = "rand_chacha" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03a2a90da8c7523f554344f921aa97283eadf6ac484a6d2a7d0212fa7f8d6853" -dependencies = [ - "c2-chacha", - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" -dependencies = [ - "getrandom", -] - -[[package]] -name = "rand_hc" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" -dependencies = [ - "rand_core", -] - [[package]] name = "redox_syscall" version = "0.1.56" @@ -350,12 +282,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" -[[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" - [[package]] name = "winapi" version = "0.2.8" diff --git a/hptp-recv/src/main.rs b/hptp-recv/src/main.rs index f0bc835..c12ee10 100644 --- a/hptp-recv/src/main.rs +++ b/hptp-recv/src/main.rs @@ -17,8 +17,6 @@ enum Error { source: tokio::io::Error, backtrace: std::backtrace::Backtrace, }, - #[error("no UDP port available for listening")] - NoPortAvail, } fn entry() -> Result<(), Error> { @@ -38,9 +36,7 @@ fn main() { } async fn start(log: &mut Logger) -> Result<(), Error> { - let sock = hptp::udp_retrying_bind(16, hptp::random_port) - .await - .map_err(|_| Error::NoPortAvail)?; + let sock = tokio::net::UdpSocket::bind((std::net::Ipv4Addr::LOCALHOST, 0)).await?; log.bound(sock.local_addr()?.port()).await; let mut out = tokio::io::stdout(); let mut peer = Peer::new(sock); diff --git a/hptp-send/src/main.rs b/hptp-send/src/main.rs index 7c0236e..c8367ff 100644 --- a/hptp-send/src/main.rs +++ b/hptp-send/src/main.rs @@ -21,8 +21,6 @@ enum Error { }, #[error("invalid command-line arguments")] InvalidArgs, - #[error("did not find an available local port")] - NoAvailPort, } fn main() { @@ -51,9 +49,7 @@ fn entry() -> Result<(), Error> { async fn start(log: &mut Logger) -> Result<(), Error> { let targ_addr = parse_args(std::env::args())?; - let sock = hptp::udp_retrying_bind(16, hptp::random_port) - .await - .map_err(|_| Error::NoAvailPort)?; + let sock = tokio::net::UdpSocket::bind((std::net::Ipv4Addr::LOCALHOST, 0)).await?; log.debug_msg(format!("bound on {}", sock.local_addr()?)) .await; let mut out = tokio::io::stdin(); diff --git a/hptp/Cargo.toml b/hptp/Cargo.toml index 2a654b3..3d53c90 100644 --- a/hptp/Cargo.toml +++ b/hptp/Cargo.toml @@ -9,6 +9,5 @@ edition = "2018" [dependencies] tokio = {version = "0.2.*", features = ["io-std", "io-util", "udp"]} -rand = "0.7.*" thiserror = "*" chrono = "0.4.*" \ No newline at end of file diff --git a/hptp/src/lib.rs b/hptp/src/lib.rs index a9412ce..18ba7aa 100644 --- a/hptp/src/lib.rs +++ b/hptp/src/lib.rs @@ -1,4 +1,3 @@ -extern crate rand; #[macro_use] extern crate thiserror; extern crate chrono; @@ -6,24 +5,3 @@ extern crate chrono; pub mod logger; pub mod msg; pub mod peer; - -use std::net::Ipv4Addr; -use tokio::net::UdpSocket; - -pub async fn udp_retrying_bind(max_tries: usize, mut pick_port: F) -> Result -where - F: FnMut() -> u16, -{ - for _ in 0..max_tries { - let port = pick_port(); - if let Ok(sock) = UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).await { - return Ok(sock); - } - } - Err(()) -} - -pub fn random_port() -> u16 { - use rand::distributions::{Distribution, Uniform}; - Uniform::new(1025, 65535u16).sample(&mut rand::thread_rng()) -}