Search for consecutive ports (#22979) (#22984)

(cherry picked from commit 514aab46d9)

Co-authored-by: sakridge <sakridge@gmail.com>
This commit is contained in:
mergify[bot]
2022-02-07 18:45:02 +00:00
committed by GitHub
parent 41142a7d76
commit 432eafd730
2 changed files with 34 additions and 10 deletions

View File

@@ -503,6 +503,34 @@ pub fn bind_common(
.and_then(|_| TcpListener::bind(&addr).map(|listener| (sock.into(), listener)))
}
pub fn bind_two_consecutive_in_range(
ip_addr: IpAddr,
range: PortRange,
) -> io::Result<((u16, UdpSocket), (u16, UdpSocket))> {
let mut first: Option<UdpSocket> = None;
for port in range.0..range.1 {
if let Ok(bind) = bind_to(ip_addr, port, false) {
match first {
Some(first_bind) => {
return Ok((
(first_bind.local_addr().unwrap().port(), first_bind),
(bind.local_addr().unwrap().port(), bind),
));
}
None => {
first = Some(bind);
}
}
} else {
first = None;
}
}
Err(io::Error::new(
io::ErrorKind::Other,
"couldn't find two consecutive ports in range".to_string(),
))
}
pub fn find_available_port_in_range(ip_addr: IpAddr, range: PortRange) -> io::Result<u16> {
let (start, end) = range;
let mut tries_left = end - start;