Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions library/std/src/net/socket_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,6 @@ impl ToSocketAddrs for (Ipv6Addr, u16) {
}
}

fn lookup_host(host: &str, port: u16) -> io::Result<vec::IntoIter<SocketAddr>> {
let addrs = crate::sys::net::lookup_host(host, port)?;
Ok(Vec::from_iter(addrs).into_iter())
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ToSocketAddrs for (&str, u16) {
type Iter = vec::IntoIter<SocketAddr>;
Expand All @@ -207,7 +202,7 @@ impl ToSocketAddrs for (&str, u16) {
}

// Otherwise, make the system look it up.
lookup_host(host, port)
crate::sys::net::lookup_host(host, port).map(|addrs| Vec::from_iter(addrs).into_iter())
}
}

Expand All @@ -229,16 +224,8 @@ impl ToSocketAddrs for str {
return Ok(vec![addr].into_iter());
}

// Otherwise, split the string by ':' and convert the second part to u16...
let Some((host, port_str)) = self.rsplit_once(':') else {
return Err(io::const_error!(io::ErrorKind::InvalidInput, "invalid socket address"));
};
let Ok(port) = port_str.parse::<u16>() else {
return Err(io::const_error!(io::ErrorKind::InvalidInput, "invalid port value"));
};

// ... and make the system look up the host.
lookup_host(host, port)
// Otherwise, make the system look it up.
crate::sys::net::lookup_host_string(self).map(|addrs| Vec::from_iter(addrs).into_iter())
}
}

Expand Down
19 changes: 19 additions & 0 deletions library/std/src/sys/net/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,22 @@ where
None => Err(Error::NO_ADDRESSES),
}
}

// Default implementation, may be overridden by platform-specific implementations.
#[cfg(not(all(target_vendor = "fortanix", target_env = "sgx")))]
pub(crate) fn lookup_host_string(
addr: &str,
) -> crate::io::Result<impl Iterator<Item = crate::net::SocketAddr>> {
use crate::io;

// Split the string by ':' and convert the second part to u16...
let Some((host, port_str)) = addr.rsplit_once(':') else {
return Err(io::const_error!(io::ErrorKind::InvalidInput, "invalid socket address"));
};
let Ok(port) = port_str.parse::<u16>() else {
return Err(io::const_error!(io::ErrorKind::InvalidInput, "invalid port value"));
};

// ... and make the system look up the host.
crate::sys::net::lookup_host(host, port)
}
22 changes: 18 additions & 4 deletions library/std/src/sys/net/connection/sgx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,23 @@ impl Iterator for LookupHost {
}
}

pub(crate) fn lookup_host_string(addr: impl Into<String>) -> io::Result<LookupHost> {
Err(io::Error::new(io::ErrorKind::Uncategorized, NonIpSockAddr { host: addr.into() }))
}

pub fn lookup_host(host: &str, port: u16) -> io::Result<LookupHost> {
Err(io::Error::new(
io::ErrorKind::Uncategorized,
NonIpSockAddr { host: format!("{host}:{port}") },
))
lookup_host_string(format!("{host}:{port}"))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn unparseable_sockaddr() {
let addr = "local";
let error = addr.to_socket_addrs().unwrap_err();
let non_ip_addr = error.downcast::<NonIpSockAddr>().unwrap();
assert_eq!(addr, non_ip_addr.host);
}
}
Loading