Skip to content

Commit

Permalink
Update listen_endpoint.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeny committed Dec 27, 2024
1 parent 195018c commit 30201f8
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions warpgate-common/src/types/listen_endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::Debug;
use std::io::ErrorKind;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs};

use futures::stream::{iter, FuturesUnordered};
Expand All @@ -14,22 +15,29 @@ use crate::WarpgateError;
pub struct ListenEndpoint(SocketAddr);

impl ListenEndpoint {
pub fn addresses_to_listen_on(&self) -> Vec<SocketAddr> {
pub fn addresses_to_listen_on(&self) -> Result<Vec<SocketAddr>, WarpgateError> {
// For [::], explicitly return both addresses so that we are not affected
// by the state of the ipv6only sysctl.
if self.0.ip() == Ipv6Addr::UNSPECIFIED {
vec![
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), self.0.port()),
SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), self.0.port()),
]
let addr6 = SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), self.0.port());
let addr4 = SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), self.0.port());
let listener6 = std::net::TcpListener::bind(addr6)?;
let listener4 = std::net::TcpListener::bind(addr4);
let result = match listener4 {
Ok(_) => vec![addr4, addr6],
Err(e) if e.kind() == ErrorKind::AddrInUse => vec![addr6],
Err(e) => return Err(WarpgateError::Io(e)),
};
drop(listener6);
Ok(result)
} else {
vec![self.0]
Ok(vec![self.0])
}
}

pub async fn tcp_listeners(&self) -> Result<Vec<TcpListener>, WarpgateError> {
Ok(self
.addresses_to_listen_on()
.addresses_to_listen_on()?
.into_iter()
.map(TcpListener::bind)
.collect::<FuturesUnordered<_>>()
Expand All @@ -38,7 +46,7 @@ impl ListenEndpoint {
}

pub async fn poem_listener(&self) -> Result<poem::listener::BoxListener, WarpgateError> {
let addrs = self.addresses_to_listen_on();
let addrs = self.addresses_to_listen_on()?;
#[allow(clippy::unwrap_used)] // length known >=1
let (first, rest) = addrs.split_first().unwrap();
let mut listener: poem::listener::BoxListener =
Expand Down

0 comments on commit 30201f8

Please sign in to comment.