From 559ac5f1622d7dfdd8b8d25bd5386f5109f29891 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Wed, 2 Aug 2023 15:13:23 -0400 Subject: [PATCH] fix server port allocation --- .../network_manager/native/start_protocols.rs | 16 ++++++-------- veilid-server/src/settings.rs | 22 ++++++++++++------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/veilid-core/src/network_manager/native/start_protocols.rs b/veilid-core/src/network_manager/native/start_protocols.rs index 25286249..416225c4 100644 --- a/veilid-core/src/network_manager/native/start_protocols.rs +++ b/veilid-core/src/network_manager/native/start_protocols.rs @@ -164,9 +164,9 @@ impl Network { ///////////////////////////////////////////////////// - fn find_available_udp_port(&self) -> EyreResult { + fn find_available_udp_port(&self, start_port: u16) -> EyreResult { // If the address is empty, iterate ports until we find one we can use. - let mut udp_port = 5150u16; + let mut udp_port = start_port; loop { if BAD_PORTS.contains(&udp_port) { continue; @@ -182,9 +182,9 @@ impl Network { Ok(udp_port) } - fn find_available_tcp_port(&self) -> EyreResult { + fn find_available_tcp_port(&self, start_port: u16) -> EyreResult { // If the address is empty, iterate ports until we find one we can use. - let mut tcp_port = 5150u16; + let mut tcp_port = start_port; loop { if BAD_PORTS.contains(&tcp_port) { continue; @@ -203,7 +203,7 @@ impl Network { async fn allocate_udp_port(&self, listen_address: String) -> EyreResult<(u16, Vec)> { if listen_address.is_empty() { // If listen address is empty, find us a port iteratively - let port = self.find_available_udp_port()?; + let port = self.find_available_udp_port(5150)?; let ip_addrs = vec![ IpAddr::V4(Ipv4Addr::UNSPECIFIED), IpAddr::V6(Ipv6Addr::UNSPECIFIED), @@ -218,9 +218,7 @@ impl Network { bail!("No valid listen address: {}", listen_address); } let port = sockaddrs[0].port(); - if !self.bind_first_udp_port(port) { - bail!("Could not find free udp port to listen on"); - } + Ok((port, sockaddrs.iter().map(|s| s.ip()).collect())) } } @@ -228,7 +226,7 @@ impl Network { async fn allocate_tcp_port(&self, listen_address: String) -> EyreResult<(u16, Vec)> { if listen_address.is_empty() { // If listen address is empty, find us a port iteratively - let port = self.find_available_tcp_port()?; + let port = self.find_available_tcp_port(5150)?; let ip_addrs = vec![ IpAddr::V4(Ipv4Addr::UNSPECIFIED), IpAddr::V6(Ipv6Addr::UNSPECIFIED), diff --git a/veilid-server/src/settings.rs b/veilid-server/src/settings.rs index 3e41eac3..701e92d3 100644 --- a/veilid-server/src/settings.rs +++ b/veilid-server/src/settings.rs @@ -120,38 +120,38 @@ core: application: https: enabled: false - listen_address: ':5150' + listen_address: ':443' path: 'app' - # url: 'https://localhost:5150' + # url: 'https://localhost' http: enabled: false - listen_address: ':5150' + listen_address: ':80' path: 'app' - # url: 'http://localhost:5150' + # url: 'http://localhost' protocol: udp: enabled: true socket_pool_size: 0 - listen_address: ':5150' + listen_address: '' # public_address: '' tcp: connect: true listen: true max_connections: 32 - listen_address: ':5150' + listen_address: '' #'public_address: '' ws: connect: true listen: true max_connections: 16 - listen_address: ':5150' + listen_address: '' path: 'ws' # url: 'ws://localhost:5150/ws' wss: connect: true listen: false max_connections: 16 - listen_address: ':5150' + listen_address: '' path: 'ws' # url: '' "#, @@ -351,6 +351,12 @@ pub struct NamedSocketAddrs { impl FromStr for NamedSocketAddrs { type Err = std::io::Error; fn from_str(s: &str) -> Result { + if s.is_empty() { + return Ok(NamedSocketAddrs { + name: String::new(), + addrs: vec![], + }); + } let addr_iter = listen_address_to_socket_addrs(s) .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?; Ok(NamedSocketAddrs {