Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions library/std/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ pub enum Shutdown {
#[stable(feature = "rust1", since = "1.0.0")]
Both,
}

// allow(unused_imports): This function is only used on some targets
#[allow(unused_imports)]
pub(crate) use socket_addr::lookup_host_string;
34 changes: 20 additions & 14 deletions library/std/src/net/socket_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,23 @@ 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())
// Default implementation, this shouldn't be called directly by the function's
// path in this module. Instead, use the platform-specific implementation
// (which may be this one).
//
// allow(dead_code): This function is only used on some targets
#[allow(dead_code)]
pub(crate) fn lookup_host_string(addr: &str) -> io::Result<impl Iterator<Item = SocketAddr>> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this go into sys/common or similar? I feel like that's how we usually treat generic code?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's probably true. @jethrogb?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mark-Simulacrum @joshtriplett I moved the implementation of lookup_host_string to sys/net/connection and this function is overriden by sgx only now. Let'me know if this change is aligned with your suggestion.

// 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)
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -207,7 +221,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 +243,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
2 changes: 2 additions & 0 deletions library/std/src/sys/net/connection/motor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ pub struct LookupHost {
addresses: alloc::collections::VecDeque<netc::sockaddr>,
}

pub(crate) use crate::net::lookup_host_string;

pub fn lookup_host(host: &str, port: u16) -> io::Result<LookupHost> {
let (_port, addresses) = moto_rt::net::lookup_host(host, port).map_err(map_motor_error)?;
Ok(LookupHost { addresses })
Expand Down
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 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);
}
}
2 changes: 2 additions & 0 deletions library/std/src/sys/net/connection/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ impl Drop for LookupHost {
}
}

pub(crate) use crate::net::lookup_host_string;

pub fn lookup_host(host: &str, port: u16) -> io::Result<LookupHost> {
init();
run_with_cstr(host.as_bytes(), &|c_host| {
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/sys/net/connection/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ impl Iterator for LookupHost {
}
}

pub(crate) use crate::net::lookup_host_string;

pub fn lookup_host(_host: &str, _port: u16) -> io::Result<LookupHost> {
unsupported()
}
2 changes: 2 additions & 0 deletions library/std/src/sys/net/connection/unsupported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ impl Iterator for LookupHost {
}
}

pub(crate) use crate::net::lookup_host_string;

pub fn lookup_host(_host: &str, _port: u16) -> io::Result<LookupHost> {
unsupported()
}
2 changes: 2 additions & 0 deletions library/std/src/sys/net/connection/wasip1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ impl Iterator for LookupHost {
}
}

pub(crate) use crate::net::lookup_host_string;

pub fn lookup_host(_host: &str, _port: u16) -> io::Result<LookupHost> {
unsupported()
}
2 changes: 2 additions & 0 deletions library/std/src/sys/net/connection/xous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ pub struct GetAddress {
}

pub use dns::lookup_host;

pub(crate) use crate::net::lookup_host_string;
Loading