Skip to content

Commit

Permalink
simplify parse_host
Browse files Browse the repository at this point in the history
  • Loading branch information
grooviegermanikus committed Sep 22, 2024
1 parent 0b10ea0 commit ad46f15
Showing 1 changed file with 3 additions and 25 deletions.
28 changes: 3 additions & 25 deletions net-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The `net_utils` module assists with networking
#![allow(clippy::arithmetic_side_effects)]

use std::net::Ipv6Addr;
use std::net::{AddrParseError, Ipv6Addr};
use std::str::FromStr;
use {
crossbeam_channel::unbounded,
Expand Down Expand Up @@ -344,30 +344,8 @@ pub fn parse_port_range(port_range: &str) -> Option<PortRange> {
}

pub fn parse_host(host: &str) -> Result<IpAddr, String> {

println!("SKIP CHECK: HOST={}", host);
if true {
return Ok(SocketAddr::from_str(&format!("{}:0", host)).expect("valid IP").ip());
}

// First, check if the host syntax is valid. This check is needed because addresses
// such as `("localhost:1234", 0)` will resolve to IPs on some networks.
let parsed_url = Url::parse(&format!("http://{host}")).map_err(|e| e.to_string())?;
if parsed_url.port().is_some() {
return Err(format!("Expected port in URL: {host}"));
}

// Next, check to see if it resolves to an IP address
let ips: Vec<_> = (host, 0)
.to_socket_addrs()
.map_err(|err| err.to_string())?
.map(|socket_address| socket_address.ip())
.collect();
if ips.is_empty() {
Err(format!("Unable to resolve host: {host}"))
} else {
Ok(ips[0])
}
IpAddr::from_str(host)
.map_err(|err| format!("Unable to parse host bind address <{}>: {}", host, err))
}

pub fn is_host(string: String) -> Result<(), String> {
Expand Down

0 comments on commit ad46f15

Please sign in to comment.