let the kernel pick my UDP ports
This commit is contained in:
parent
4ad767e5c4
commit
bb6f534626
|
@ -18,15 +18,6 @@ version = "0.5.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1"
|
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]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
version = "0.1.10"
|
version = "0.1.10"
|
||||||
|
@ -60,23 +51,11 @@ version = "0.3.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
|
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]]
|
[[package]]
|
||||||
name = "hptp"
|
name = "hptp"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"rand",
|
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
@ -212,12 +191,6 @@ version = "0.1.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "237844750cfbb86f67afe27eee600dfbbcb6188d734139b534cbfbf4f96792ae"
|
checksum = "237844750cfbb86f67afe27eee600dfbbcb6188d734139b534cbfbf4f96792ae"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ppv-lite86"
|
|
||||||
version = "0.2.6"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.9"
|
version = "1.0.9"
|
||||||
|
@ -236,47 +209,6 @@ dependencies = [
|
||||||
"proc-macro2",
|
"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]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.1.56"
|
version = "0.1.56"
|
||||||
|
@ -350,12 +282,6 @@ version = "0.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
|
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]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.2.8"
|
version = "0.2.8"
|
||||||
|
|
|
@ -17,8 +17,6 @@ enum Error {
|
||||||
source: tokio::io::Error,
|
source: tokio::io::Error,
|
||||||
backtrace: std::backtrace::Backtrace,
|
backtrace: std::backtrace::Backtrace,
|
||||||
},
|
},
|
||||||
#[error("no UDP port available for listening")]
|
|
||||||
NoPortAvail,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn entry() -> Result<(), Error> {
|
fn entry() -> Result<(), Error> {
|
||||||
|
@ -38,9 +36,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn start(log: &mut Logger) -> Result<(), Error> {
|
async fn start(log: &mut Logger) -> Result<(), Error> {
|
||||||
let sock = hptp::udp_retrying_bind(16, hptp::random_port)
|
let sock = tokio::net::UdpSocket::bind((std::net::Ipv4Addr::LOCALHOST, 0)).await?;
|
||||||
.await
|
|
||||||
.map_err(|_| Error::NoPortAvail)?;
|
|
||||||
log.bound(sock.local_addr()?.port()).await;
|
log.bound(sock.local_addr()?.port()).await;
|
||||||
let mut out = tokio::io::stdout();
|
let mut out = tokio::io::stdout();
|
||||||
let mut peer = Peer::new(sock);
|
let mut peer = Peer::new(sock);
|
||||||
|
|
|
@ -21,8 +21,6 @@ enum Error {
|
||||||
},
|
},
|
||||||
#[error("invalid command-line arguments")]
|
#[error("invalid command-line arguments")]
|
||||||
InvalidArgs,
|
InvalidArgs,
|
||||||
#[error("did not find an available local port")]
|
|
||||||
NoAvailPort,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -51,9 +49,7 @@ fn entry() -> Result<(), Error> {
|
||||||
|
|
||||||
async fn start(log: &mut Logger) -> Result<(), Error> {
|
async fn start(log: &mut Logger) -> Result<(), Error> {
|
||||||
let targ_addr = parse_args(std::env::args())?;
|
let targ_addr = parse_args(std::env::args())?;
|
||||||
let sock = hptp::udp_retrying_bind(16, hptp::random_port)
|
let sock = tokio::net::UdpSocket::bind((std::net::Ipv4Addr::LOCALHOST, 0)).await?;
|
||||||
.await
|
|
||||||
.map_err(|_| Error::NoAvailPort)?;
|
|
||||||
log.debug_msg(format!("bound on {}", sock.local_addr()?))
|
log.debug_msg(format!("bound on {}", sock.local_addr()?))
|
||||||
.await;
|
.await;
|
||||||
let mut out = tokio::io::stdin();
|
let mut out = tokio::io::stdin();
|
||||||
|
|
|
@ -9,6 +9,5 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = {version = "0.2.*", features = ["io-std", "io-util", "udp"]}
|
tokio = {version = "0.2.*", features = ["io-std", "io-util", "udp"]}
|
||||||
rand = "0.7.*"
|
|
||||||
thiserror = "*"
|
thiserror = "*"
|
||||||
chrono = "0.4.*"
|
chrono = "0.4.*"
|
|
@ -1,4 +1,3 @@
|
||||||
extern crate rand;
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate thiserror;
|
extern crate thiserror;
|
||||||
extern crate chrono;
|
extern crate chrono;
|
||||||
|
@ -6,24 +5,3 @@ extern crate chrono;
|
||||||
pub mod logger;
|
pub mod logger;
|
||||||
pub mod msg;
|
pub mod msg;
|
||||||
pub mod peer;
|
pub mod peer;
|
||||||
|
|
||||||
use std::net::Ipv4Addr;
|
|
||||||
use tokio::net::UdpSocket;
|
|
||||||
|
|
||||||
pub async fn udp_retrying_bind<F>(max_tries: usize, mut pick_port: F) -> Result<UdpSocket, ()>
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue